{"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$$$)\u00a0\u2014 the number of sticks. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\le a_i \\le 100$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = sort { $a <=> $b } split ' ', <>;\n\t\n\tmy @cand;\n\t\n\tfor my $cand ( 1 .. 100 ){\n\t\tmy $sum = 0;\n\t\tfor( @_ ){\n\t\t\tmy $x = abs( $cand - $_ ) - 1;\n\t\t\t$x < 0 and $x = 0;\n\t\t\t$sum += $x;\n\t\t\t}\n\t\tpush @cand, [ $cand, $sum ];\n\t\t}\n\t\n\tprint \"@{ ( sort { $a->[ 1 ] <=> $b->[ 1 ] } @cand )[ 0 ] }\";\n\t}"}], "negative_code": [], "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\u2009\u2264\u2009q\u2009\u2264\u2009105)\u00a0\u2014 the number of queries. q lines follow. The (i\u2009+\u20091)-th line contains single integer ni (1\u2009\u2264\u2009ni\u2009\u2264\u2009109)\u00a0\u2014 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\u2009=\u20094\u2009+\u20094\u2009+\u20094\u2009=\u20094\u2009+\u20098\u2009=\u20096\u2009+\u20096\u2009=\u200912, but the first splitting has the maximum possible number of summands.8\u2009=\u20094\u2009+\u20094, 6 can't be split into several composite summands.1,\u20092,\u20093 are less than any composite number, so they do not have valid splittings."}, "positive_code": [{"source_code": "\n\nmy $q = ;\nchomp ( $q );\nmy $f;\nmy $ans = 0;\nfor (1..$q) {\n $f = ;\n chomp ( $f );\n if ( $f < 4 ) {\n\tprint \"-1\";\n }\n elsif ( ($f == 4) or ($f == 6) or ($f == 9) ) {\n\tprint \"1\";\n }\n elsif ( ($f == 8) or ($f == 10) or ($f == 13) or ($f == 15) ) {\n\tprint \"2\";\n }\n elsif ( ($f == 5) or ($f == 7) or ($f == 11) ) {\n\tprint \"-1\";\n }\n elsif ( ($f == 14) or ($f == 12) ) {\n\tprint \"3\";\n }\n elsif ( $f % 4 == 3 or $f % 4 == 1 ) {\n\t$ans = int ( $f / 4 ) - 1;\n\tprint $ans;\n }\n else {\n\t$ans = int ( $f / 4 );\n\tprint $ans;\n }\n print \"\\n\";\n}\n\n\n"}, {"source_code": "sub in_list {\n my ($a, @b) = @_;\n my $y = 0;\n $y |= ($a == $_) for @b;\n $y;\n};\n\n<>; for $n (<>) {\n if (in_list($n, 1, 2, 3, 5, 7, 11)) {\n $k = -1;\n } else {\n $k = 0;\n $k = 1, $n -= 9 if $n & 1;\n $k += int($n / 4);\n };\n print $k, \"\\n\";\n};\n"}, {"source_code": "$\\ = $/;\n\n<>;\n\nfor( <> ){\n\t$c = 0;\n\t$_ % 2 and do { $c ++; $_ -= 9 };\n\t$_ % 4 == 2 and do { $c ++; $_ -= 6 };\n\tprint $c += $_ < 0 ? -1 - $c : $_ / 4;\n\t}\t"}, {"source_code": "$\\ = $/;\n\n<>;\n\nprint /^([1-357]|11)$/ ? -1 : ( $_ >> 2 ) - $_ % 2 for <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tfor( map ~~<>, 1 .. $_ ){\n\t\tmy $c = 0;\n\t\t$_ % 2 and do { $c ++; $_ -= 9 };\n\t\t$_ % 4 == 2 and do { $c ++; $_ -= 6 };\n\t\tprint $c += $_ < 0 ? -1 - $c : $_ / 4;\n\t\t}\t\n\t}"}], "negative_code": [{"source_code": "\n\nmy $q = ;\nchomp ( $q );\nmy $f;\nmy $ans = 0;\nfor (1..$q) {\n $f = ;\n chomp ( $f );\n if ( $f < 4 ) {\n\tprint \"-1\";\n }\n elsif ( ($f == 4) or ($f == 6) or ($f == 9) ) {\n\tprint \"1\";\n }\n elsif ( ($f == 8) or ($f == 10) or ($f == 13) or ($f == 15) ) {\n\tprint \"2\";\n }\n elsif ( ($f == 5) or ($f == 7) or ($f == 11) ) {\n\tprint \"-1\";\n }\n elsif ( ($f == 14) or ($f == 12) ) {\n\tprint \"3\";\n }\n elsif ( $f % 4 == 3 ) {\n\t$ans = int ( $f / 4 ) - 1;\n\tprint $ans;\n }\n else {\n\t$ans = int ( $f / 4 );\n\tprint $ans;\n }\n print \"\\n\";\n}\n\n\n"}, {"source_code": "sub in_list {\n my ($a, @b) = @_;\n my $y = 0;\n $y |= ($a == $_) for @b;\n $y;\n};\n\nfor $n () {\n if (in_list($n, 1, 2, 3, 5, 7, 11)) {\n $k = -1;\n } else {\n $k = 0;\n $k = 1, $n -= 9 if $n & 1;\n $k += int($n / 4);\n };\n print $k, \"\\n\";\n};\n"}, {"source_code": "<>;\n\nfor( <> ){\n\t$c = 0;\n\t$_ % 2 and do { $c ++; $_ -= 9 };\n\t$_ % 4 == 2 and do { $c ++; $_ -= 6 };\n\tprint $c += $_ < 0 ? -1 - $c : $_ / 4;\n\t}\t"}], "src_uid": "0c2550b2df0849a62969edf5b73e0ac5"} {"nl": {"description": "Berland annual chess tournament is coming!Organizers have gathered 2\u00b7n 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\u00b7n 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\u00b7n 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\u2009\u2264\u2009n\u2009\u2264\u2009100). The second line contains 2\u00b7n integers a1,\u2009a2,\u2009... a2n (1\u2009\u2264\u2009ai\u2009\u2264\u20091000).", "output_spec": "If it's possible to divide all 2\u00b7n 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 = <>;\n@d = sort {$a <=> $b} split(/\\D/, <>);\nprint (($d[$n] > $d[$n - 1])? \"YES\": \"NO\")"}], "negative_code": [], "src_uid": "7b806b9a402a83a5b8943e7f3436cc9a"} {"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,\u2009x2,\u2009...,\u2009xn. 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\u2009-\u2009hi,\u2009xi] or [xi;xi\u2009+\u2009hi]. 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of trees. Next n lines contain pairs of integers xi,\u2009hi (1\u2009\u2264\u2009xi,\u2009hi\u2009\u2264\u2009109) \u2014 the coordinate and the height of the \u0456-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 \u2014 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 \u2014 now it occupies segment [\u2009-\u20091;1] fell the 2-nd tree to the right \u2014 now it occupies segment [2;3] leave the 3-rd tree \u2014 it occupies point 5 leave the 4-th tree \u2014 it occupies point 10 fell the 5-th tree to the right \u2014 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": "$L =<>+-~ 0;\n\n\t$p = $x,\n\t($x, $h) = split,\n\t$x > $L || (-- $a, $L = $p ),\n\t$L = $x + $h * ($x - $L <= $h),\n\t$a ++ for <>;\n\nprint $a\n"}, {"source_code": "$L =<>+-~ 0;\n\n\t$p = $x,\n\t($x, $h) = split,\n\t$x > $L || (-- $a, $L = $p ),\n\t$L = $x + $h * ($x - $L <= $h),\n\t$a ++ for <>;\n\nprint $a\n"}, {"source_code": "$L =<>+-~ 0;\n\n\t$p = $x,\n\t($x, $h) = split,\n\t$x > $L || (-- $a, $L = $p ),\n\t$L = $x + $h * ($x - $L <= $h),\n\t$a ++ for <>;\n\nprint $a\n"}, {"source_code": "$L =<>+-~ 0;\n\n\t$p = $x,\n\t($x, $h) = split,\n\t$x > $L || (-- $a, $L = $p ),\n\t$L = $x + $h * ($x - $L <= $h),\n\t$a ++ for <>;\n\nprint $a\n"}, {"source_code": "$L =<>+-~ 0;\n\n\t$p = $x,\n\t($x, $h) = split,\n\t$x > $L || (-- $a, $L = $p ),\n\t$L = $x + $h * ($x - $L <= $h),\n\t$a ++ for <>;\n\nprint $a\n"}, {"source_code": "$L =<>+-~ 0;\n\n\t$p = $x,\n\t($x, $h) = split,\n\t$x > $L || (-- $a, $L = $p ),\n\t$L = $x + $h * ($x - $L <= $h),\n\t$a ++ for <>;\n\nprint $a\n"}, {"source_code": "$L =<>+-~ 0;\n\n\t$p = $x,\n\t($x, $h) = split,\n\t$x > $L || (-- $a, $L = $p ),\n\t$L = $x + $h * ($x - $L <= $h),\n\t$a ++ for <>;\n\nprint $a\n"}, {"source_code": "$L =<>+-~ 0;\n\n\t$p = $x,\n\t($x, $h) = split,\n\t$x > $L || (-- $a, $L = $p ),\n\t$L = $x + $h * ($x - $L <= $h),\n\t$a ++ for <>;\n\nprint $a"}, {"source_code": "$\\ = $/;\n$left = -10e9;\n<>;\n$ans = 0;\n\nwhile(<>){\n\t$prev = $x;\n\t($x, $h) = split;\n\t$x <= $left and do {$ans -- ; $left = $prev };\n\tif ($x - $left > $h ){\n\t\t$left = $x;\n\t\t} \n\t\telse {\n\t\t\t$left = $x + $h;\n\t\t\t}\n\t$ans ++\n}\n\nprint $ans"}, {"source_code": "$L =<>+-~ 0;\n\n\t$p = $x,\n\t($x, $h) = split,\n\t$x > $L || (-- $a, $L = $p ),\n\t$L = $x + $h * ($x - $L <= $h),\n\t$a ++ for <>;\n\nprint $a\n"}], "negative_code": [], "src_uid": "a850dd88a67a6295823e70e2c5c857c9"} {"nl": {"description": "Because of budget cuts one IT company established new non-financial reward system instead of bonuses.Two kinds of actions are rewarded: fixing critical bugs and suggesting new interesting features. A man who fixed a critical bug gets \"I fixed a critical bug\" pennant on his table. A man who suggested a new interesting feature gets \"I suggested a new feature\" pennant on his table.Because of the limited budget of the new reward system only 5 \"I fixed a critical bug\" pennants and 3 \"I suggested a new feature\" pennants were bought.In order to use these pennants for a long time they were made challenge ones. When a man fixes a new critical bug one of the earlier awarded \"I fixed a critical bug\" pennants is passed on to his table. When a man suggests a new interesting feature one of the earlier awarded \"I suggested a new feature\" pennants is passed on to his table.One man can have several pennants of one type and of course he can have pennants of both types on his table. There are n tables in the IT company. Find the number of ways to place the pennants on these tables given that each pennant is situated on one of the tables and each table is big enough to contain any number of pennants.", "input_spec": "The only line of the input contains one integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009500) \u2014 the number of tables in the IT company.", "output_spec": "Output one integer \u2014 the amount of ways to place the pennants on n tables.", "sample_inputs": ["2"], "sample_outputs": ["24"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n\nuse bigint;\n\nsub factorial {\n my $n = shift;\n if ($n==0) {\n return 1;\n } else {\n return $n*factorial($n-1);\n }\n}\n\nsub subfactorial {\n my $n = shift;\n my $sub = shift;\n if ($n==$sub) {\n return 1;\n } else {\n return $n*subfactorial($n-1, $sub);\n }\n}\n\n\n$tables = ;\n$result = 0;\n$subnp1 = subfactorial($tables+3-1, $tables-1)/6;\n$subnp2 = subfactorial($tables+5-1, $tables-1)/120;\n$result = $subnp1*$subnp2;\nprint \"$result\\n\";"}], "negative_code": [], "src_uid": "d41aebacfd5d16046db7c5d339478115"} {"nl": {"description": "We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 6000$$$)\u00a0\u2014 the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \\le n, k \\le 10^6$$$)\u00a0\u2014 the initial position of point $$$A$$$ and desirable absolute difference.", "output_spec": "For each test case, print the minimum number of steps to make point $$$B$$$ exist.", "sample_inputs": ["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"], "sample_outputs": ["0\n3\n1000000\n0\n1\n0"], "notes": "NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n,$k) = map { $_ - 0 } split(/\\s+/,);\n \n my $r1 = abs( $n - $k );\n \n my $r2;\n my $a = $n - $k;\n if( $a < 0 ){\n $r2 = -$a;\n } else {\n $r2 = 0;\n if( $a % 2 == 1 ){\n $r2 = 1;\n }\n }\n \n my $res = ( $r1 < $r2 ? $r1 : $r2 );\n print \"$res\\n\";\n \n}\n\nexit(0);\n\n"}], "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$$$) \u2014 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$$$) \u2014 the elements of the array.", "output_spec": "Print $$$n$$$ integers \u2014 $$$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": ";\nprint(join(\" \", map {$_ - 1 + $_ % 2} split(\" \", )));\n"}, {"source_code": "<>;print(join($\",map{$_-1+$_%2}split($\",<>)))"}, {"source_code": "<>;print join $\",map{$_-1+$_%2}split $\",<>"}, {"source_code": "$n = ;\n@a = split(\" \", );\nfor (@a) {\n print($_ - 1 + $_ % 2);\n print(\" \");\n}\n"}, {"source_code": ";\n@a = map {$_ - 1 + $_ % 2} split(\" \", );\nprint(join(\" \", @a));\n"}, {"source_code": "<>;print join$\",map$_-1+$_%2,split$\",<>"}, {"source_code": ";print(join(\" \",map{$_-1+$_%2}split(\" \",)))"}, {"source_code": ";\nprint(join(\" \", map {$_ - 1 + $_ % 2} split(\" \", )))"}, {"source_code": ";print(join($\",map{$_-1+$_%2}split($\",)))"}, {"source_code": "$n = ;\n@a = split(\" \", );\nfor (@a) {\n print($_ - 1 + $_ % 2, \" \");\n}\n"}, {"source_code": "$n = ;\n@a = split(\" \", );\nfor (@a) {\n if ($_ % 2 == 0) {\n print($_ - 1);\n } else {\n print($_);\n }\n print(\" \");\n}\n"}], "negative_code": [{"source_code": "<>;print map$_-1+$_%2,split$\",<>"}, {"source_code": "<>;print map($_-1+$_%2).\"\",split$\",<>"}], "src_uid": "d00696cb27c679dda6e2e29314a8432b"} {"nl": {"description": "DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew $$$n$$$ distinct lines, given by equations $$$y = x + p_i$$$ for some distinct $$$p_1, p_2, \\ldots, p_n$$$.Then JLS drew on the same paper sheet $$$m$$$ distinct lines given by equations $$$y = -x + q_i$$$ for some distinct $$$q_1, q_2, \\ldots, q_m$$$.DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$), the number of test cases in the input. Then follow the test case descriptions. The first line of a test case contains an integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$), the number of lines drawn by DLS. The second line of a test case contains $$$n$$$ distinct integers $$$p_i$$$ ($$$0 \\le p_i \\le 10^9$$$) describing the lines drawn by DLS. The integer $$$p_i$$$ describes a line given by the equation $$$y = x + p_i$$$. The third line of a test case contains an integer $$$m$$$ ($$$1 \\le m \\le 10^5$$$), the number of lines drawn by JLS. The fourth line of a test case contains $$$m$$$ distinct integers $$$q_i$$$ ($$$0 \\le q_i \\le 10^9$$$) describing the lines drawn by JLS. The integer $$$q_i$$$ describes a line given by the equation $$$y = -x + q_i$$$. The sum of the values of $$$n$$$ over all test cases in the input does not exceed $$$10^5$$$. Similarly, the sum of the values of $$$m$$$ over all test cases in the input does not exceed $$$10^5$$$. 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 in the input print a single integer\u00a0\u2014 the number of line pairs with integer intersection points. ", "sample_inputs": ["3\n3\n1 3 2\n2\n0 3\n1\n1\n1\n1\n1\n2\n1\n1"], "sample_outputs": ["3\n1\n0"], "notes": "NoteThe picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy @A = split ' ', <>;\n\t<>;\n\tmy @B = split ' ', <>;\n\t\n\tmy $oddA = grep $_ & 1, @A;\n\tmy $oddB = grep $_ & 1, @B;\n\t\n\tmy $evenA = grep !($_ & 1), @A;\n\tmy $evenB = grep !($_ & 1), @B;\n\t\n\tprint $oddA * $oddB + $evenA * $evenB;\n\t}"}], "negative_code": [], "src_uid": "adf4239de3b0034cc690dad6160cf1d0"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100, 2\u2009\u2264\u2009k\u2009\u2264\u2009min(n,\u200926)) \u2014 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 \u2014 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 \u2014 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 \u2014 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": "use strict;\nuse warnings;\n\nmy ($n, $k) = map { 0 + $_ } split /\\s+/, <>;\nmy @passwd_letters = ('a' .. chr(ord('a') + $k - 1));\n\nmy $pass = '';\nwhile (length($pass) != $n) {\n\t$pass .= $passwd_letters[length($pass) % $k];\n}\nprint \"$pass\\n\";\n"}], "negative_code": [], "src_uid": "39f5e934bf293053246bd3faa8061c3b"} {"nl": {"description": "You are given two circles. Find the area of their intersection.", "input_spec": "The first line contains three integers x1,\u2009y1,\u2009r1 (\u2009-\u2009109\u2009\u2264\u2009x1,\u2009y1\u2009\u2264\u2009109,\u20091\u2009\u2264\u2009r1\u2009\u2264\u2009109) \u2014 the position of the center and the radius of the first circle. The second line contains three integers x2,\u2009y2,\u2009r2 (\u2009-\u2009109\u2009\u2264\u2009x2,\u2009y2\u2009\u2264\u2009109,\u20091\u2009\u2264\u2009r2\u2009\u2264\u2009109) \u2014 the position of the center and the radius of the second circle.", "output_spec": "Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10\u2009-\u20096.", "sample_inputs": ["0 0 4\n6 0 4", "0 0 5\n11 0 5"], "sample_outputs": ["7.25298806364175601379", "0.00000000000000000000"], "notes": null}, "positive_code": [{"source_code": "use strict;\nuse Math::BigFloat;\n\nour $P = -30;\nour $A = 30;\nour $pi = Math::BigFloat->bpi();\nMath::BigFloat->precision($P);\n\nsub acos {\n\tmy $x = shift;\n\tmy $r;\n\t# print \"x=$x\\n\";\n\tif ($x->copy()->babs() < 0.9) {\n \tmy $sum = $pi / 2 - $x;\n \tmy $p1 = Math::BigFloat->new(1);\n \tmy $p2 = Math::BigFloat->new(1);\n \tmy $px = $x->copy(); \n \tmy $x2 = $x * $x;\n \tmy ($n1, $n2, $cur, $i) = (1, 2);\n \tdo {\n \t\t$px *= $x2;\n \t\t$p1 *= $n1;\n \t\t$p2 *= $n2;\n \t\t$cur = $p1 * $px / $p2 / ($n2 + 1);\n \t\t$sum -= $cur; \n \t\t# print ++$i, \" sum=$sum cur=$cur\\n\";\n \t\t$n1 += 2;\n \t\t$n2 += 2;\n \t} until $cur == 0;\n \t$r = $sum;\n\t} else {\n\t\tif ($x->sign() eq '+') {\n\t\t\t$r = $pi / 2 - acos( (1 - $x * $x)->bsqrt() );\n\t\t} else {\n\t\t\t$r = $pi / 2 + acos( (1 - $x * $x)->bsqrt() );\t\t\n\t\t}\n\t}\n\t$r;\n}\n\nsub go {\n my ($x1, $y1, $r1) = map(Math::BigFloat->new($_), split(' ', <>));\n my ($x2, $y2, $r2) = map(Math::BigFloat->new($_), split(' ', <>));\n my ($l, $ans, $a1, $a2, $s1, $s2);\n ($r1, $r2) = ($r2, $r1) if $r2 > $r1;\n $l = (($x2 - $x1)->bpow(2) + ($y2 - $y1)->bpow(2))->bsqrt();\n if ($l >= $r1 + $r2) {\n \t$ans = Math::BigFloat->bzero();\n } elsif ($r1 >= $l + $r2) {\n \t$ans = $pi * $r2 * $r2;\n } else {\n $a1 = acos( (($r1 * $r1 + $l * $l - $r2 * $r2) / (2 * $r1 * $l)) ) * 2;\n $a2 = acos( (($r2 * $r2 + $l * $l - $r1 * $r1) / (2 * $r2 * $l)) ) * 2;\n\t\t# 1) pi * r^2 * a / (2 * pi) - 0.5 * r * r * sin(a)\n\t\t# 2) pi * r^2 * a / (2 * pi) + 0.5 * r * r * sin(2 * pi - a) = (1)\n\t\t$s1 = $r1 * $r1 * 0.5 * ($a1 - $a1->copy()->bsin());\n\t\t$s2 = $r2 * $r2 * 0.5 * ($a2 - $a2->copy()->bsin());\t\t\n\t\t$ans = $s1 + $s2;\n\t}\n\t$ans->bfround(-7);\n print \"$ans\\n\";\n}\n\ngo();\n"}], "negative_code": [{"source_code": "sub acos {\n\tmy $x = shift;\n\tatan2(sqrt(1 - $x ** 2) / $x, 1);\n}\n\nsub go {\n use bignum \"PI\";\n ($x1, $y1, $r1) = split ' ', <>;\n ($x2, $y2, $r2) = split ' ', <>;\n\n $l = sqrt( ($x2 - $x1) ** 2 + ($y2 - $y1) ** 2 );\n ($r1, $r2) = ($r2, $r1) if $r2 > $r1;\n\n if ($l >= $r1 + $r2) {\n \t$ans = 0;\n } elsif ($r1 >= $l + $r2) {\n \t$ans = PI() * $r2 * $r2;\n } elsif ($l > $r1) {\n $a1 = acos( (($r1 * $r1 + $l * $l - $r2 * $r2) / (2 * $r1 * $l))->numify() ) * 2;\n $a2 = acos( (($r2 * $r2 + $l * $l - $r1 * $r1) / (2 * $r2 * $l))->numify() ) * 2;\n \t\t$s1 = $r1 * $r1 * 0.5 * ($a1 - sin($a1));\n \t$s2 = $r2 * $r2 * 0.5 * ($a2 - sin($a2));\n\t\t$ans = $s1 + $s2;\n\t} else {\n $a1 = acos( (($r1 * $r1 + $l * $l - $r2 * $r2) / (2 * $r1 * $l))->numify() ) * 2;\n $a2 = 2 * PI() - acos( (($r2 * $r2 + $l * $l - $r1 * $r1) / (2 * $r2 * $l))->numify() ) * 2;\n \t\t$s1 = $r1 * $r1 * 0.5 * ($a1 - sin($a1));\n \t$s2 = $r2 * $r2 * 0.5 * ($a2 - sin($a2));\n\t\t$ans = $r2 * $r2 * PI() - $s2 + $s1;\n\t}\n printf \"%.7f\\n\", $ans;\n}\n\ngo();"}, {"source_code": "$pi = 4 * atan2(1, 1);\n\nsub acos {\n\tmy $x = shift;\n\t$x == 0? $pi / 2: atan2(sqrt(1 - $x ** 2), $x);\n}\n\nsub go {\n use bignum;\n ($x1, $y1, $r1) = split ' ', <>;\n ($x2, $y2, $r2) = split ' ', <>;\n ($r1, $r2) = ($r2, $r1) if $r2 > $r1;\n $l = sqrt( ($x2 - $x1) ** 2 + ($y2 - $y1) ** 2 );\n if ($l >= $r1 + $r2) {\n \t$ans = 0;\n } elsif ($r1 >= $l + $r2) {\n \t$ans = $pi * $r2 * $r2;\n } else {\n $a1 = acos( (($r1 * $r1 + $l * $l - $r2 * $r2) / (2 * $r1 * $l))->numify() ) * 2;\n $a2 = acos( (($r2 * $r2 + $l * $l - $r1 * $r1) / (2 * $r2 * $l))->numify() ) * 2;\n\t\t# 1) pi * r^2 * a / (2 * pi) - 0.5 * r * r * sin(a)\n\t\t# 2) pi * r^2 * a / (2 * pi) + 0.5 * r * r * sin(2 * pi - a) = (1)\n\t\t$s1 = $r1 * $r1 * 0.5 * ($a1 - sin($a1));\n\t\t$s2 = $r2 * $r2 * 0.5 * ($a2 - sin($a2));\t\t\n\t\t$ans = $s1 + $s2;\n\t}\n printf \"%.7f\\n\", $ans;\n}\n\ngo();\n"}, {"source_code": "sub acos {\n\tmy $x = shift;\n\tatan2(sqrt(1 - $x ** 2) / $x, 1);\n}\n\nsub go {\n use bignum \"PI\";\n ($x1, $y1, $r1) = split ' ', <>;\n ($x2, $y2, $r2) = split ' ', <>;\n\n $l = sqrt( ($x2 - $x1) ** 2 + ($y2 - $y1) ** 2 );\n ($r1, $r2) = ($r2, $r1) if $r2 > $r1;\n\n if ($l >= $r1 + $r2) {\n \t$ans = 0;\n } elsif ($r1 >= $l + $r2) {\n \t$ans = PI() * $r2 * $r2;\n } else {\n $a1 = acos( (($r1 * $r1 + $l * $l - $r2 * $r2) / (2 * $r1 * $l))->numify() ) * 2;\n $a2 = acos( (($r2 * $r2 + $l * $l - $r1 * $r1) / (2 * $r2 * $l))->numify() ) * 2;\n \t$s1 = $r1 * $r1 * 0.5 * ($a1 - sin($a1));\n \t$s2 = $r2 * $r2 * 0.5 * ($a2 - sin($a2));\n \tif ($l > $r1) {\n \t\t$ans = $s1 + $s2;\n \t} else {\n \t\t$ans = $s2 - $s1;\n \t}\n }\n printf \"%.7f\\n\", $ans;\n}\n\ngo();"}, {"source_code": "sub acos {\n\tmy $x = shift;\n\tatan2(sqrt(1 - $x ** 2) / $x, 1);\n}\n\nsub go {\n use bignum;\n ($x1, $y1, $r1) = split ' ', <>;\n ($x2, $y2, $r2) = split ' ', <>;\n\n $l = sqrt( ($x2 - $x1) ** 2 + ($y2 - $y1) ** 2 );\n ($r1, $r2) = ($r2, $r1) if $r2 > $r1;\n\n if ($l >= $r1 + $r2) {\n \t$ans = 0;\n } elsif ($r1 > $l + $r2) {\n \t$ans = PI() * $r2 * $r2;\n } else {\n $a1 = acos( (($r1 * $r1 + $l * $l - $r2 * $r2) / (2 * $r1 * $l))->numify() ) * 2;\n $a2 = acos( (($r2 * $r2 + $l * $l - $r1 * $r1) / (2 * $r2 * $l))->numify() ) * 2;\n \t$s1 = $r1 * $r1 * 0.5 * ($a1 - sin($a1));\n \t$s2 = $r2 * $r2 * 0.5 * ($a2 - sin($a2));\n \tif ($l > $r1) {\n \t\t$ans = $s1 + $s2;\n \t} else {\n \t\t$ans = $s2 - $s1;\n \t}\n }\n printf \"%.7f\\n\", $ans;\n}\n\ngo();"}, {"source_code": "eval {\n\neval { require bignum; bignum->import(\"PI\"); };\n$@ && die $@;\n\neval { require Math::Trig; Math::Trig->import(); };\n$@ && die $@;\n\n($x1, $y1, $r1) = split ' ', <>;\n($x2, $y2, $r2) = split ' ', <>;\n\n$l = sqrt( ($x2 - $x1) ** 2 + ($y2 - $y1) ** 2 );\n($r1, $r2) = ($r2, $r1) if $r2 > $r1;\n\nif ($l >= $r1 + $r2) {\n\t$ans = 0;\n} elsif ($r1 > $l + $r2) {\n\t$ans = PI() * $r2 * $r2;\n} else {\n $a1 = acos( (($r1 * $r1 + $l * $l - $r2 * $r2) / (2 * $r1 * $l))->numify() ) * 2;\n $a2 = acos( (($r2 * $r2 + $l * $l - $r1 * $r1) / (2 * $r2 * $l))->numify() ) * 2;\n\t$s1 = $r1 * $r1 * 0.5 * ($a1 - sin($a1));\n\t$s2 = $r2 * $r2 * 0.5 * ($a2 - sin($a2));\n\tif ($l > $r1) {\n\t\t$ans = $s1 + $s2;\n\t} else {\n\t\t$ans = $s2 - $s1;\n\t}\n}\nprintf \"%.7f\\n\", $ans;\n\n};\n\nif ($@) {\n\tprint \"die: \", $@, \"\\n\";\n}"}], "src_uid": "94d98a05741019f648693085d2f08872"} {"nl": {"description": " Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters \"a\", \"b\" and \"c\". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string \"abc\" as a substring. A valid replacement of a character is replacing it with \"a\", \"b\" or \"c\".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ 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 two integers $$$n$$$ and $$$q$$$ $$$(1 \\le n, q \\le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters \"a\", \"b\" and \"c\". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \\le i \\le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is \"a\", \"b\" or \"c\".", "output_spec": "For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain \"abc\" as a substring.", "sample_inputs": ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"], "sample_outputs": ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"], "notes": "NoteLet's consider the state of the string after each query: $$$s =$$$ \"abcabcabc\". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bbcaccabb\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bbcabcabc\". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bbcbbcbbc\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bccabcabc\". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bccbbcbbc\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bcaabcabc\". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bcabbcbbc\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bcabbcabc\". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bcabbcabb\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bcabccabc\". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bcabbcabb\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bcabccaac\". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bcabbcaac\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bcabccaab\". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bcabbcaab\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"ccabccaab\". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"ccabbcaab\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"ccaaccaab\". In this case the string does not contain \"abc\" as a substring and no replacements are needed."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n$\\ = $/;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $q ) = split;\n\t$_ = <>, chomp;\n\t\n\t$debug and print \"[$_]\";\n\t\n\tmy $cnt += () = m/abc/g;\n\t\n\tmy @ans;\n\t\n\tfor my $qq ( 1 .. $q ){\n\t\tmy( $i, $c ) = split ' ', <>;\n\t\t\n\t\tmy $lett = substr $_, $i - 1, 1;\n\t\t\n\t\t$debug and print \"$qq:[$_], cnt:[$cnt], i:[$i], c:[$c], lett:[$lett]\";\n\t\t\n\t\tif( $lett eq $c ){\n\t\t\tpush @ans, $cnt;\n\t\t\tnext;\n\t\t\t}\n\t\t\n\t\t$debug and print \"change:\";\n\t\t\n\t\tif( 0 ){ ; }\n\t\telsif( $lett eq 'a' and $i < $n - 1 ){\n\t\t\tif( 'abc' eq substr $_, $i - 1, 3 ){\n\t\t\t\t$cnt --;\n\t\t\t\t}\n\t\t\t}\n\t\telsif( $lett eq 'b' and $i > 0 and $i < $n - 0 ){\n\t\t\tif( 'abc' eq substr $_, $i - 2, 3 ){\n\t\t\t\t$cnt --;\n\t\t\t\t}\n\t\t\t}\n\t\telsif( $lett eq 'c' and $i > 1 ){\n\t\t\tif( 'abc' eq substr $_, $i - 3, 3 ){\n\t\t\t\t$cnt --;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\tsubstr $_, $i - 1, 1, $c;\n\t\t\n\t\t$debug and print \" [$_], cnt:[$cnt]\";\n\t\t\n\t\tif( 0 ){ ; }\n\t\telsif( $c eq 'a' and $i < $n - 1 ){\n\t\t\tif( 'abc' eq substr $_, $i - 1, 3 ){\n\t\t\t\t$cnt ++;\n\t\t\t\t}\n\t\t\t}\n\t\telsif( $c eq 'b' and $i > 0 and $i < $n - 0 ){\n\t\t\tif( 'abc' eq substr $_, $i - 2, 3 ){\n\t\t\t\t$cnt ++;\n\t\t\t\t}\n\t\t\t}\n\t\telsif( $c eq 'c' and $i > 1 ){\n\t\t\tif( 'abc' eq substr $_, $i - 3, 3 ){\n\t\t\t\t$cnt ++;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t$debug and print \" [$_], cnt:[$cnt]\";\n\t\t\n\t\tpush @ans, $cnt;\n\t\t}\n\t\n\tprint join \"\\n\", @ans;\n\t}"}], "negative_code": [], "src_uid": "db473ad780a93983667d12b1357c6e2f"} {"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 \u2014 a positive integer number without leading zeroes (1\u2009\u2264\u2009n\u2009<\u200910100000).", "output_spec": "Print one number \u2014 any beautiful number obtained by erasing as few as possible digits. If there is no answer, print \u2009-\u20091.", "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": "$_ = <>; chomp; $l = length;\n$t += $_ for split //; $r = $t % 3;\n\nsub ans {\n\t$a = shift;\n\t$a =~ s/^0+(?=[1-9]|0$)//;\n\t$la = length $a;\n\tif ($la < $l && $la > length $ans) {\n\t\t$ans = $a;\n\t}\n}\n\nif ($r == 0) {\n\t$ans = $_;\n}\n\nif ($r == 1) {\n\tans s/(.*)[147]/$1/r;\n\tans s/(.*)[258](.*?)[258]/$1$2/r;\n}\n\nif ($r == 2) {\n\tans s/(.*)[147](.*?)[147]/$1$2/r;\n\tans s/(.*)[258]/$1/r;\t\t\n}\n\nprint defined $ans? $ans: -1;"}], "negative_code": [{"source_code": "$_ = <>; chomp; $l = length;\n$t += $_ for split //; $r = $t % 3;\n\nsub ans {\n\t$a = shift;\n\t$a =~ s/^0+(?=[1-9]|0$)//;\n\t$la = length $a;\n\tif ($la < $l && $la > length $ans) {\n\t\t$ans = $a;\n\t}\n}\n\nif ($r == 0) {\n\t$ans = $_;\n}\n\nif ($r == 1) {\n\tans s/(.*)[147]/$1/r;\n\tans s/(.*)[258](.*?)[258]/$1$2/r;\n}\n\nif ($r == 2) {\n\tans s/(.*)[147](.*?)[147]/$1$2/r;\n\tans s/(.*)[258]/$1/r;\t\t\n}\n\nprint $ans || -1;"}], "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\n( $n, $k ) = split ' ', <>;\n\nprint for\n\t'YES',\n\t'.' x $n,\n\t( map { \".$_.\" }\n\t\tdo {\n\t\t\tif( $k % 2 == 0 ) {\n\t\t\t\t( '#' x ( $k / 2 ) . '.' x ( $n - 2 - $k / 2 ) ) x 2\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$inv = $k >= $n - 2;\n\t\t\t\t$k = ( $n - 2 ) * 2 - $k if $inv;\n\t\t\t\tmap { $inv ? y/#%/.#/r : y/%/./r }\n\t\t\t\t\t'%' x ( $n - 2 ), \n\t\t\t\t\t( join '#' x $k, ( '%' x ( $n - 2 - $k >> 1 ) ) x 2 ),\n\t\t\t\t}\n\t\t\t}\n\t),\n\t'.' x $n,"}, {"source_code": "( $n, $k ) = split ' ', <>;\n\n$_ = join \"\\n\", 'YES', ( '.' x $n ) x 4;\n\nwhile( $k -- ){\n\ts/\\.$ \\n\\K^(\\.*)\\.(\\#*)((??{ $1 ? '.' : '\\.' }))\\2\\.\\1$/\n\t\t\"$1$3$2\" . \"$3\" =~ y!.#!#.!r . \"$2$3$1\"\n\t/mex;\n\t}\n\nprint"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t$debug and print \"$n $k\";\n\t\n\tif( $k >= $n - 2 ){\n\t\tprint \"YES\";\n\t\t\n\t\tprint for\n\t\t\t'.' x $n,\n\t\t\tjoin( ( $k >= $n - 2 ? '#' : '.' ) x ( $n - 2 ), ( '.' ) x 2 );\n\t\t\n\t\t$k >= $n - 2 and $k -= $n - 2;\n\t\t$k < 0 and $k = 0;\n\t\t\n\t\t$_ = '.' . '#' x $k . '.' x ( $n - 2 - $k ) . '.';\n\t\t\n\t\ts/#(\\.+)\\.$/$1#./;\n\t\ts/^\\.+#\\.+$/ join '#', ( '.' x ( $n >> 1 ) ) x 2 /e;\n\t\t\n\t\tprint;\n\t\t\n\t\tprint '.' x $n;\n\t\t\n\t\t}\n\telse{\n\t\t$debug and print \"IDK\";\n\t\t\n\t\tif( $k % 2 == 0 ){\n\t\t\tprint \"YES\";\n\t\t\tprint '.' x $n;\n\t\t\tprint '.' . '#' x ( $k / 2 ) . '.' x ( $n - $k / 2 - 2 ) . '.';\n\t\t\tprint '.' . '#' x ( $k / 2 ) . '.' x ( $n - $k / 2 - 2 ) . '.';\n\t\t\tprint '.' x $n;\n\t\t\t}\n\t\telse{\n\t\t\tprint \"YES\";\n\t\t\tprint '.' x $n;\n\t\t#\tprint '.' . '#' x ( $k - 2 ) . '.' x ( $n - $k ) . '.';\n\t\t\tprint join '#' x $k, ( '.' x ( $n - $k >> 1 ) ) x 2;\n\t\t\tprint '.' x $n;\n\t\t\tprint '.' x $n;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print '-' x 15;\n\t}"}], "negative_code": [{"source_code": "( $n, $k ) = split ' ', <>;\n\nprint for\n\t'YES',\n\t'.' x $n,\n\t( map { \".$_.\" }\n\t\tdo {\n\t\t\tif( $k % 2 == 0 ) {\n\t\t\t\t( '#' x ( $k / 2 ) . '.' x ( $n - 2 - $k / 2 ) ) x 2\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$inv = $k >= $n - 2;\n\t\t\t\t$k = ( $n - 2 ) * 2 - $k if $inv;\n\t\t\t\tmap { $inv ? y/#%/.#/r : y/%/./r }\n\t\t\t\t\t'%' x ( $n - 2 ), \n\t\t\t\t\t( join '#' x $k, ( '%' x ( $n - 2 - $k >> 1 ) ) x 2 ),\n\t\t\t\t}\n\t\t\t}\n\t),\n\t'.' x $n,"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\tprint\n\t\tjoin \"\\n\",\n\t\t'.' x $n,\n\t\tjoin( ( $k > $n - 2 ? '#' : '.' ) x ( $n - 2 ), ( '.' ) x 2 ),\n\t\tjoin(\n\t\t\t\t( $k % ( $n - 2 ) == 0 ) ? \n\t\t\t\t\t'.' x ( $n - 2 ) :\n\t\t\t\t\t( $k % ( $n - 2 ) == 1 ) ?\n\t\t\t\t\t\t( join '#', ( '.' x ( - 1 + int $n / 2 ) ) x 2 ) :\n\t\t\t\t\t\t( '#' x ( $k % ( $n - 2 ) - 1 ) . '.' x ( $n - $k % ( $n - 2 ) ) . '#' )\n\t\t\t\t,\n\t\t\t( '.' ) x 2\n\t\t\t),\n\t\t'.' x $n,\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t$debug and print \"$n $k\";\n\t\n\tif( $k >= $n - 2 ){\n\t\tprint \"YES\";\n\t\t\n\t\tprint for\n\t\t\t'.' x $n,\n\t\t\tjoin( ( $k >= $n - 2 ? '#' : '.' ) x ( $n - 2 ), ( '.' ) x 2 );\n\t\t\n\t\t$k >= $n - 2 and $k -= $n - 2;\n\t\t$k < 0 and $k = 0;\n\t\t\n\t\t$_ = '.' . '#' x $k . '.' x ( $n - 2 - $k ) . '.';\n\t\t\n\t\ts/#(\\.+)\\.$/$1#./;\n\t\ts/^\\.+#\\.+$/ join '#', ( '.' x ( $n >> 1 ) ) x 2 /e;\n\t\t\n\t\tprint;\n\t\t\n\t\tprint '.' x $n;\n\t\t\n\t\t}\n\telse{\n\t\t$debug and print \"IDK\";\n\t\t\n\t\tif( $k % 2 == 0 ){\n\t\t\tprint \"YES\";\n\t\t\tprint '.' x $n;\n\t\t\tprint '.' . '#' x ( $k / 2 ) . '.' x ( $n - $k / 2 - 2 ) . '.';\n\t\t\tprint '.' . '#' x ( $k / 2 ) . '.' x ( $n - $k / 2 - 2 ) . '.';\n\t\t\tprint '.' x $n;\n\t\t\t}\n\t\telsif( $k == 1 || $k == 3 ){\n\t\t\tprint \"NO\";\n\t\t\t}\n\t\telse{\n\t\t\tprint \"YES\";\n\t\t\tprint '.' x $n;\n\t\t\tmy $ins = '.' . '#' x ( $k - 2 ) . '.' x ( $n - $k ) . '.';\n\t\t\tprint $ins;\n\t\t\t$ins =~ s/#(#+)#/ '#' . ( '.' x length $1 ) . '#' /e;\n\t\t\tprint $ins;\n\t\t\tprint '.' x $n;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print '-' x 15;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t$debug and print \"$n $k\";\n\t\n\tprint for\n\t\t'.' x $n,\n\t\tjoin( ( $k >= $n - 2 ? '#' : '.' ) x ( $n - 2 ), ( '.' ) x 2 );\n\t\n\t$k >= $n - 2 and $k -= $n - 2;\n\t$k < 0 and $k = 0;\n\t\n\t$_ = '.' . '#' x $k . '.' x ( $n - 2 - $k ) . '.';\n\t\n\ts/#(\\.+)\\.$/$1#./;\n\ts/^\\.+#\\.+$/ join '#', ( '.' x ( $n >> 1 ) ) x 2 /e;\n\t\n\tprint;\n\t\n\tprint '.' x $n;\n\t\n\t$debug and print '-' x 15;\n\t}"}], "src_uid": "2669feb8200769869c4b2c29012059ed"} {"nl": {"description": "Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill: the graph contains exactly 2n\u2009+\u2009p edges; the graph doesn't contain self-loops and multiple edges; for any integer k (1\u2009\u2264\u2009k\u2009\u2264\u2009n), any subgraph consisting of k vertices contains at most 2k\u2009+\u2009p edges. A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices. Your task is to find a p-interesting graph consisting of n vertices.", "input_spec": "The first line contains a single integer t (1\u2009\u2264\u2009t\u2009\u2264\u20095) \u2014 the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5\u2009\u2264\u2009n\u2009\u2264\u200924; p\u2009\u2265\u20090; ) \u2014 the number of vertices in the graph and the interest value for the appropriate test. It is guaranteed that the required graph exists.", "output_spec": "For each of the t tests print 2n\u2009+\u2009p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai,\u2009bi (1\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u2009n;\u00a0ai\u2009\u2260\u2009bi) \u2014 two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n. Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.", "sample_inputs": ["1\n6 0"], "sample_outputs": ["1 2\n1 3\n1 4\n1 5\n1 6\n2 3\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6"], "notes": null}, "positive_code": [{"source_code": "for $I(1..<>){\n\t\n\t($n, $p)=split/ /,<>;\n\t$s=$n+$n+$p;\n\t\n\tfor ($i=1; $i<$n; $i++){\n\t\t\n\t\tfor ($j=$i+1; $j<=$n; $j++){\n\t\t\t\n\t\t\tprint \"$i $j\\n\";\n\t\t\t--$s or last;\n\t\t\t\n\t\t\t}\n\t\t$s or last;\n\t\t\n\t\t}\n\t\n\t}"}, {"source_code": "for (1..<>){\n\t\n\t<>=~/ /;\n\t$s=2*$`+$';\nL:\n\tfor $i(1..$`-1){\n\t\tfor $j($i+1..$`){\n\t\t\tprint \"$i $j\\n\";\n\t\t\t--$s or last L;\n\t\t\t}\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "ddbac4053bd07eada84bc44275367ae2"} {"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\u2009\u2264\u2009n\u2009\u2264\u20093000). Next line consists of n integers ai (1\u2009\u2264\u2009ai\u2009\u2264\u2009n), which stand for coolness factor of each badge.", "output_spec": "Output single integer \u2014 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": "<>;\nmy @arr = sort {$a <=> $b} split / /, <>;\nmy $ans = 0;\nfor(1..scalar @arr - 1) {\n if ($arr[$_] <= $arr[$_ - 1]) {\n $ans += $arr[$_ - 1] - $arr[$_] + 1;\n $arr[$_] = $arr[$_ - 1] + 1;\n }\n}\nprint $ans;\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = sort { $a <=> $b} split / /, <>;\n$ans = 0;\nforeach (1 .. $n-1) {\n\tif ($a[$_] <= $a[$_-1]) {\n\t\t$ans += $a[$_-1] + 1 - $a[$_];\n\t\t$a[$_] = $a[$_-1] + 1;\n\t}\n}\nsay $ans;"}, {"source_code": "<>;\n$M += ($_ < $m) * ($m - $_), $m += 1 + ($_ - $m) * ($_ > $m) for sort {$a <=> $b} split \" \", <>;\nprint 0 + $M"}], "negative_code": [{"source_code": "<>;\nmy @arr = sort {$a <=> $b} split / /, <>;\nmy $ans = 0;\nfor(1..scalar @arr - 1) {\n if ($arr[$_] == $arr[$_ - 1]) {\n $ans++;\n $arr[$_]++;\n $_--;\n }\n}\nprint $ans;\n"}, {"source_code": "<>;\n$M += ($_ < $m) * (1 + $m - $_), $m += 1 + $_ * ($_ < $m) for sort {$a <=> $b} split \" \", <>;\nprint 0 + $M"}], "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\u2009-\u20096.", "sample_inputs": ["1 2 1 2"], "sample_outputs": ["0.666666666667"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\n$_ = <>;\n@ps = split / /;\nprint (($ps[0]/$ps[1])/($ps[0]/$ps[1] + $ps[2]/$ps[3] - $ps[0]/$ps[1]*$ps[2]/$ps[3]));\n\n"}, {"source_code": "$inp = ;\n ($a,$b,$c,$d) = split(' ', $inp);\n $x=$a/$b;\n $y=$c/$d;\n $ans=($x)/(1-(1.0-$x)*(1.0-$y));\n printf '%.10f',$ans; "}, {"source_code": "while (<>=~/ (\\d+) (\\d+) /){\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$c++;\nfor(1..1e5){\n$a=$c*$`/$1;\n\t$S+=$a; $c-=$a;\n\t$c-=$c*$2/$'\n}\nprint $S\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\nwhile (<>=~/ (\\d+) (\\d+) /){\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$c++;\nfor(1..1e5){\n\t$S+=$c*$`/$1; $c-=$c*$`/$1;\n\t$K+=$c*$2/$'; $c-=$c*$2/$'\n}\nprint $S\n\n}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\nsub fact{\n\tmy $i= shift; my $j= 1;\n\t$j*=$i-- while $i;\n\treturn $j\n}\n\n# Transpose \"charbox\"\nsub trans{\n\tmy @t=(); my $i=0; \n\t$i=0, s/./$t[$i++].=$&/eg for @_;\n\treturn @t\n}\n\nsub rotL{\n\tmy @t=(); my $i=0; my $j;\n\tfor (@_){\n\t\t$i=0, $j=length; $t[$i++].=chop while $j--\n\t}\n\treturn @t\n}\n\nsub rotR{\n my @m=@_; chomp @m; my @t=();\n\tmy $i= length $m[0];\n\twhile ($i--){\n\t\t$t[$i]=~s/^/chop/e for @m\n\t}\nreturn @t\n}\n\nsub revlines{\n\t$_=reverse $_ for @_;\n\treturn @_\n}\n\n# Transpose \"value sets\" ## slow?\nsub trans_s{\n\tmy @t=(); my $j;\n\t$j=0, s/\\S+/($t[$j++].=\"$& \")?$&:$&/eg for @_;\n\tchop @t;\nreturn @t\n}\n\n# rotL_s\n# rotR_s\n\n# slow?\nsub revlines_s{\n\t$_=join\" \", reverse split/\\s+/ \tfor @_;\n\treturn @_\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n\tmy @m=sort @_; my @u;\n\tpush @u, my $i=shift @m;\n\t$i eq $_ or push @u, $i=$_ for @m;\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\nwhile (<>=~/ (\\d+) (\\d+) /){\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$c++;\nfor(1..1e3){\n\t$S+=$c*$`/$1; $c-=$c*$`/$1;\n\t$K+=$c*$2/$'; $c-=$c*$2/$'\n}\nprint $S\n\n}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\nsub fact{\n\tmy $i= shift; my $j= 1;\n\t$j*=$i-- while $i;\n\treturn $j\n}\n\n# Transpose \"charbox\"\nsub trans{\n\tmy @t=(); my $i=0; \n\t$i=0, s/./$t[$i++].=$&/eg for @_;\n\treturn @t\n}\n\nsub rotL{\n\tmy @t=(); my $i=0; my $j;\n\tfor (@_){\n\t\t$i=0, $j=length; $t[$i++].=chop while $j--\n\t}\n\treturn @t\n}\n\nsub rotR{\n my @m=@_; chomp @m; my @t=();\n\tmy $i= length $m[0];\n\twhile ($i--){\n\t\t$t[$i]=~s/^/chop/e for @m\n\t}\nreturn @t\n}\n\nsub revlines{\n\t$_=reverse $_ for @_;\n\treturn @_\n}\n\n# Transpose \"value sets\" ## slow?\nsub trans_s{\n\tmy @t=(); my $j;\n\t$j=0, s/\\S+/($t[$j++].=\"$& \")?$&:$&/eg for @_;\n\tchop @t;\nreturn @t\n}\n\n# rotL_s\n# rotR_s\n\n# slow?\nsub revlines_s{\n\t$_=join\" \", reverse split/\\s+/ \tfor @_;\n\treturn @_\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n\tmy @m=sort @_; my @u;\n\tpush @u, my $i=shift @m;\n\t$i eq $_ or push @u, $i=$_ for @m;\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n"}], "src_uid": "7b932b2d3ab65a353b18d81cf533a54e"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100000). The i-th (1\u2009\u2264\u2009i\u2009\u2264\u2009n) 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": "$_=<>;\ns/./$i[++$j]=$&, $j>1 and $i[$j] eq $i[$j-1] and $j-=2/eg;\nprint $j?\"No\":\"Yes\"\n"}, {"source_code": "$_=<>;\ns/./$i[++$j]=$&, $j>1 and $i[$j] eq $i[$j-1] and $j-=2/eg;\nprint $j?\"No\":\"Yes\"\n"}, {"source_code": "$_=<>;\ns/./$i[++$j]=$&, $j>1 and $i[$j] eq $i[$j-1] and $j-=2/eg;\nprint $j?\"No\":\"Yes\"\n"}, {"source_code": "$_=<>;\ns/./$i[++$j]=$&, $j>1 and $i[$j] eq $i[$j-1] and $j-=2/eg;\nprint $j?\"No\":\"Yes\"\n"}, {"source_code": "$_=<>;\ns/./$i[++$j]=$&, $j>1 and $i[$j] eq $i[$j-1] and $j-=2/eg;\nprint $j?\"No\":\"Yes\"\n"}, {"source_code": "$_=<>;\ns/./$i[++$j]=$&, $j>1 and $i[$j] eq $i[$j-1] and $j-=2,()/eg;\nprint $j?\"No\":\"Yes\""}, {"source_code": "$_=<>;\ns/./$i[++$j]=$&, $j>1 and $i[$j] eq $i[$j-1] and $j-=2/eg;\nprint $j?\"No\":\"Yes\""}, {"source_code": "$_=<>;\ns/./$i[++$j]=$&, $j>1 and $i[$j] eq $i[$j-1] and $j-=2/eg;\nprint $j?\"No\":\"Yes\"\n"}, {"source_code": "$_=<>;\ns/./$i[++$j]=$&, $j>1 and $i[$j] eq $i[$j-1] and $j-=2/eg;\nprint $j?\"No\":\"Yes\"\n"}, {"source_code": "$_=<>;\ns/./$i[++$j]=$&, $j>1 and $i[$j] eq $i[$j-1] and $j-=2/eg;\nprint $j?\"No\":\"Yes\"\n"}], "negative_code": [{"source_code": "$_=<>;\nwhile(s/\\+-\\+\\+-\\+|-\\+--\\+-|\\+--\\+|-\\+\\+-|\\+{2}|-{2}//g){};\nprint +$_?\"No\":\"Yes\""}, {"source_code": "$_=<>;\nwhile(/\\+\\+|--/){s/\\+-\\+\\+-\\+|-\\+--\\+-|\\+--\\+|-\\+\\+-|\\+{2}|-{2}//g};\nprint $_?\"No\":\"Yes\""}, {"source_code": "$_=<>;\nwhile(s/\\+-\\+\\+-\\+|-\\+--\\+-|\\+--\\+|-\\+\\+-|\\+{2}|-{2}//g){};\nprint $_?\"No\":\"Yes\""}, {"source_code": "$_=<>;\ns/\\+-\\+\\+-\\+|-\\+--\\+-|\\+--\\+|-\\+\\+-|\\+{2}|-{2}//g;\nprint $_?\"No\":\"Yes\""}], "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$$$)\u00a0\u2014 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\u00a0\u2014 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": "for (1..<>)\r\n{\r\n <>;\r\n chomp (my $in = <>);\r\n@a = split / /, $in;\r\n$a = @a[0];\r\n$b = @a[1];\r\n$c = @a[2];\r\n\r\nif ($a==$b) {\r\n $x = $a;\r\n} else {\r\n $x = $a == $c ? $a : $b;\r\n}\r\n\r\n$i = 1;\r\nforeach(@a) {\r\n $_ != $x and print $i;\r\n $i++;\r\n}\r\nprint \"\\n\";\r\n}\r\n\r\nexit;"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my %c = ();\r\n for(my $i=0;$i<$n;$i++){\r\n $c{$A[$i]}->{c} ++;\r\n $c{$A[$i]}->{i} = $i;\r\n }\r\n my $r = 0;\r\n foreach my $c1 (keys %c){\r\n if( $c{$c1}->{c} == 1 ){\r\n $r = $c{$c1}->{i};\r\n last;\r\n }\r\n }\r\n $r ++;\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\tmy $this;\n\t\n\t$h{ $_ } == 1 and $this = $_ for keys %h;\n\t\n\tfor( my $i = 0; $i < @_; $i ++ ){\n\t\t$_[ $i ] == $this and do {\n\t\t\tprint $i + 1;\n\t\t\tlast;\n\t\t\t};\n\t\t}\n\t}"}], "negative_code": [{"source_code": "for (1..<>)\r\n{\r\n <>;\r\n chomp (my $in = <>);\r\n@a = split / /, $in;\r\n$a = @a[0];\r\n$b = @a[1];\r\n$c = @a[2];\r\n\r\nif ($a==$b) {\r\n $x == $a;\r\n} else {\r\n $x = $a == $c ? $a : $b;\r\n}\r\n\r\n$i = 1;\r\nforeach(@a) {\r\n $_ != $x and print $i;\r\n $i++;\r\n}\r\nprint \"\\n\";\r\n}\r\n\r\nexit;"}, {"source_code": "chomp (my $in = <>);\r\n@a = split / /, $in;\r\n$a = @a[0];\r\n$b = @a[1];\r\n$c = @a[2];\r\n\r\nif ($a==$b) {\r\n $x == $a;\r\n} else {\r\n $x = $a == $c ? $a : $b;\r\n}\r\n\r\n$i = 1;\r\nforeach(@a) {\r\n $_ != $x and print $i;\r\n $i++;\r\n}\r\n\r\nexit;"}, {"source_code": "chomp (my $in = <>);\r\n@a = split / /, $in;\r\n$a = @a[0];\r\n$b = @a[1];\r\n$c = @a[2];\r\n\r\nif ($a==$b) {\r\n $x == $a;\r\n} else {\r\n $x = $a == $c ? $a : $b;\r\n}\r\n\r\n$i = 1;\r\nforeach(@a) {\r\n @_ != $x and print $i;\r\n $i++;\r\n}\r\n\r\nexit;"}], "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\u2009+\u20097).", "input_spec": "Input contains several test cases. The first line contains two integers t and k (1\u2009\u2264\u2009t,\u2009k\u2009\u2264\u2009105), where t represents the number of test cases. The next t lines contain two integers ai and bi (1\u2009\u2264\u2009ai\u2009\u2264\u2009bi\u2009\u2264\u2009105), 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\u2009+\u20097).", "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": "\n\n\n\n# mod value by ((10 ** 9) + 7)\nsub my_mod {\n return ($_[0] % 1000000007);\n}\n\n# read input\n\nmy $upper_bound = ((10 ** 5) + 13);\nmy ($t, $k, $f);\n$f = ;\nchomp ( $f );\n($t, $k) = split m/ /, $f;\n\n# calculate dp\nmy @dp = ();\nfor my $i (1..($upper_bound)) {\n if ( $i > $k ) {\n\t$dp[ $i ] = &my_mod ( $dp[ $i-1 ] + $dp[ $i-$k ] );\n }\n elsif ( $i < $k ) {\n\t$dp[ $i ] = 1;\n }\n else {\n\t$dp[ $i ] = 2;\n }\n $dp[ $i ] = &my_mod ( $dp[ $i ] );\n}\n\n# calculate prefix sums\nfor ( my $j = 1; $j < $upper_bound ; $j += 1 ) {\n $dp[ $j ] = &my_mod ( $dp[ $j ] + $dp[ $j-1 ] );\n}\n\n# lookup queries\nmy $a, $b, $f;\nfor (1..$t) {\n $f = ;\n chomp ( $f );\n ($a, $b) = split m/ /, $f;\n print (&my_mod ( $dp[ $b ] - $dp[ $a-1 ] ));\n print \"\\n\";\n}\n\n\n\n\n\n\n\n\n"}], "negative_code": [{"source_code": "\n\n\n\n# read input\nmy $upper_bound = ((10 ** 5) + 13);\nmy ($t, $k, $f);\n$f = ;\nchomp ( $f );\n($t, $k) = split m/ /, $f;\n\n# calculate dp\nmy @dp = ();\nfor my $i (1..($upper_bound)) {\n if ( $i > $k ) {\n\t$dp[ $i ] = $dp[ $i-1 ] + $dp[ $i-$k ];\n }\n elsif ( $i < $k ) {\n\t$dp[ $i ] = 1;\n }\n else {\n\t$dp[ $i ] = 2;\n }\n\n}\n\n# calculate prefix sums\nfor ( my $j = 1; $j < $upper_bound ; $j += 1 ) {\n $dp[ $j ] += $dp[ $j-1 ];\n}\n\n# lookup queries\nmy $a, $b, $f;\nfor (1..$t) {\n $f = ;\n chomp ( $f );\n ($a, $b) = split m/ /, $f;\n print (($dp[ $b ] - $dp[ $a-1 ]) % ((10 ** 9) + 7));\n print \"\\n\";\n}\n\n\n\n\n\n\n\n\n"}], "src_uid": "16c016c0735be1815c7b94c5c50516f1"} {"nl": {"description": "You are given two arrays $$$a$$$ and $$$b$$$ of $$$n$$$ elements, each element is either $$$0$$$ or $$$1$$$.You can make operations of $$$2$$$ kinds. Pick an index $$$i$$$ and change $$$a_i$$$ to $$$1-a_i$$$. Rearrange the array $$$a$$$ however you want. Find the minimum number of operations required to make $$$a$$$ equal to $$$b$$$.", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 400$$$) \u2014 the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$) \u2014 the length of the arrays $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), representing the array $$$a$$$. The third line of each test case contains $$$n$$$ space-separated integers $$$b_1,b_2,\\ldots,b_n$$$ ($$$b_i$$$ is $$$0$$$ or $$$1$$$), representing the array $$$b$$$.", "output_spec": "For each test case, print the minimum number of operations required to make $$$a$$$ equal to $$$b$$$.", "sample_inputs": ["5\n\n3\n\n1 0 1\n\n0 0 1\n\n4\n\n1 1 0 0\n\n0 1 1 1\n\n2\n\n1 1\n\n1 1\n\n4\n\n1 0 0 1\n\n0 1 1 0\n\n1\n\n0\n\n1"], "sample_outputs": ["1\n2\n0\n1\n1"], "notes": "NoteIn the first case, we need only one operation: change $$$a_1$$$ to $$$1-a_i$$$. Now $$$a = [0, 0]$$$ which is equal to $$$b$$$.In the second case, the optimal way is to rearrange $$$a$$$ to get the array $$$[0, 1, 11$$$. Now $$$a = [0, 0, 1]$$$ which is equal to $$$b$$$.In the second case, one of optimal ways would be to first change $$$a_3$$$ to $$$1 - a_3$$$, then rearrange $$$a$$$.In the third case, no operation is needed.In the fourth case, the optimal way is to rearrange $$$a$$$ to get the array $$$[0, 1, 1, 0]$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\t\n\tmy $L = 0;\n\tmy $R = 0;\n\t\n\twhile( @A ){\n\t\tmy $i = ( shift @A ) - ( shift @B );\n\t\t\n\t\tif( $i < 0 ){\n\t\t\t$L ++;\n\t\t\t}\n\t\telsif( $i > 0 ){\n\t\t\t$R ++;\n\t\t\t}\n\t\t}\n\t\n\tif( $L and $R ){\n\t\tprint 1 + abs( $L - $R );\n\t\t}\n\telsif( $L or $R ){\n\t\tprint $L + $R;\n\t\t}\n\telse{\n\t\tprint 0;\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "5ccef7fbfd5e85d7fc7ef92f9ebc4088"} {"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$$$)\u00a0\u2014 $$$(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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n \n my @a = (1..$n);\n print ( join(' ',@a) . \"\\n\");\n \n}\n\nexit(0);\n\n"}, {"source_code": "use strict;\nuse warnings;\nuse v5.020;\n\nmy $t = <>;\nwhile($t--) {\n my $n;# = <>;\n chomp($n=<>);\n my @ans;\n if($n % 2 == 1){\n push @ans, int($n);\n for(my $i = 1; $i <= int($n/2); $i++){\n push @ans, $i;\n push @ans, $n-$i\n }\n } else {\n push @ans, int($n/2);\n for(my $i = 1; $i < $n/2; $i++){\n push @ans, $i;\n push @ans, $n+1-$i;\n }\n push @ans, $n/2 + 1;\n }\n say \"@ans\";\n}"}], "negative_code": [], "src_uid": "1b3ac752bc9c0b5e20a76f028d4b3c15"} {"nl": {"description": "You have an array a consisting of n integers. Each integer from 1 to n appears exactly once in this array.For some indices i (1\u2009\u2264\u2009i\u2009\u2264\u2009n\u2009-\u20091) it is possible to swap i-th element with (i\u2009+\u20091)-th, for other indices it is not possible. You may perform any number of swapping operations any order. There is no limit on the number of times you swap i-th element with (i\u2009+\u20091)-th (if the position is not forbidden).Can you make this array sorted in ascending order performing some sequence of swapping operations?", "input_spec": "The first line contains one integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009200000) \u2014 the number of elements in the array. The second line contains n integers a1, a2, ..., an (1\u2009\u2264\u2009ai\u2009\u2264\u2009200000) \u2014 the elements of the array. Each integer from 1 to n appears exactly once. The third line contains a string of n\u2009-\u20091 characters, each character is either 0 or 1. If i-th character is 1, then you can swap i-th element with (i\u2009+\u20091)-th any number of times, otherwise it is forbidden to swap i-th element with (i\u2009+\u20091)-th.", "output_spec": "If it is possible to sort the array in ascending order using any sequence of swaps you are allowed to make, print YES. Otherwise, print NO.", "sample_inputs": ["6\n1 2 5 3 4 6\n01110", "6\n1 2 5 3 4 6\n01010"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first example you may swap a3 and a4, and then swap a4 and a5."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy @A = 1 .. $_;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$_ = <>, chomp;\n\t\n\t$_ .= '0';\n\t\n\tmy $i = 0;\n\tmy @C;\n\tmy $max = 0;\n\tmy $f = 0;\n\tmy $fail = 0;\n\t\n\tfor( split // ){\n\t\t$_[ $i ] > $max and $max = $_[ $i ];\n\t\t$debug and print ' ' .$max . ' ' . $fail;\n\t\tif( $_ ){\n\t\t\t$f = 1;\n\t\t\t}\n\t\telse{\n\t\t\tif( $f ){\n\t\t\t\t$max > $A[ $i ] and $fail = 1;\n\t\t\t\t$f = 0;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$A[ $i ] != $_[ $i ] and $fail = 2;\n\t\t\t\t}\n\t\t\t}\n\t\t$i ++;\n\t\t}\n\t\n\tprint $fail ? \"NO\" : \"YES\";\n\t\n#\t$debug and print \"[@C]\";\n\t\n#\tmy $i = 0 + /^1/;\n\t\n#\ts/0\\K(?=1)|10\\K/|/g;\n\t\n#\t$debug and print;\n\t\n#\ts/\\d/ ' ' . shift @_ /eg;\n\t\n#\t$debug and print;\n\t\n#\t@_ = split '\\|';\n\t\n#\tmy @B;\n\t\n#\tfor( @_ ){\n#\t\tpush @B, $i ++ % 2 ?\n#\t\t\t( sort { $a <=> $b } split )\n#\t\t\t: split\n#\t\t\t;\n#\t\t}\n\t\n#\t$debug and print \"@B\";\n\t\n#\tmy @intervals;\n\t\n#\twhile( /1++1*+1*+1*+1*+1*+1*+1*+/g ){\n#\t\tpush @intervals, [ $-[0], $+[0] ];\n#\t\t}\n\t\n#\t$debug and print \"@{$_}\" for @intervals;\n\t\n#\tfor( @intervals ){\n#\t\tmy( $start, $end ) = @{ $_ };\n\t\t\n#\t\t@_[ $start .. $end ] =\n#\t\t\tsort { $a <=> $b } @_[ $start .. $end ];\n#\t\t}\n\t\n#\tprint @A ~~ @C ? \"YES\" : \"NO\";\n\t\n\t$debug and print '-' x 10;\n\t}"}, {"source_code": "@A = 1 .. <>;\n\n@_ = split ' ', <>;\n\n$_ = <>, chomp;\n\n$_ .= '0';\n\n$i = 0;\n\nfor( split // ){\n\t$_[ $i ] > $max and $max = $_[ $i ];\n\tif( $_ ){\n\t\t$f = 1;\n\t\t}\n\telse{\n\t\tif( $f ){\n\t\t\t$max > $A[ $i ] and $fail = 1;\n\t\t\t$f = 0;\n \t\t}\n \telse{\n \t\t$A[ $i ] != $_[ $i ] and $fail = 2;\n\t\t\t}\n\t\t}\n\t$i ++;\n\t}\n\nprint $fail ? \"NO\" : \"YES\""}], "negative_code": [], "src_uid": "094a880e93a2adfbcb6dcc2b5f043bab"} {"nl": {"description": "You are given a grid with $$$n$$$ rows and $$$m$$$ columns. Rows and columns are numbered from $$$1$$$ to $$$n$$$, and from $$$1$$$ to $$$m$$$. The intersection of the $$$a$$$-th row and $$$b$$$-th column is denoted by $$$(a, b)$$$. Initially, you are standing in the top left corner $$$(1, 1)$$$. Your goal is to reach the bottom right corner $$$(n, m)$$$.You can move in four directions from $$$(a, b)$$$: up to $$$(a-1, b)$$$, down to $$$(a+1, b)$$$, left to $$$(a, b-1)$$$ or right to $$$(a, b+1)$$$.You cannot move in the same direction in two consecutive moves, and you cannot leave the grid. What is the minimum number of moves to reach $$$(n, m)$$$?", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$) \u2014 the number of the test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 10^9$$$) \u2014 the size of the grid.", "output_spec": "For each test case, print a single integer: $$$-1$$$ if it is impossible to reach $$$(n, m)$$$ under the given conditions, otherwise the minimum number of moves.", "sample_inputs": ["6\n\n1 1\n\n2 1\n\n1 3\n\n4 2\n\n4 6\n\n10 5"], "sample_outputs": ["0\n1\n-1\n6\n10\n17"], "notes": "NoteTest case $$$1$$$: $$$n=1$$$, $$$m=1$$$, and initially you are standing in $$$(1, 1)$$$ so $$$0$$$ move is required to reach $$$(n, m) = (1, 1)$$$.Test case $$$2$$$: you should go down to reach $$$(2, 1)$$$.Test case $$$3$$$: it is impossible to reach $$$(1, 3)$$$ without moving right two consecutive times, or without leaving the grid.Test case $$$4$$$: an optimal moving sequence could be: $$$(1, 1) \\to (1, 2) \\to (2, 2) \\to (2, 1) \\to (3, 1) \\to (3, 2) \\to (4, 2)$$$. It can be proved that this is the optimal solution. So the answer is $$$6$$$."}, "positive_code": [{"source_code": "use strict;\r\n\r\nsub max {\r\n my $res = shift;\r\n map {$res = $_ if $res < $_} @_;\r\n $res;\r\n}\r\n\r\nmy $t = <>;\r\nforeach $_ (1..$t) {\r\n my ($n, $m) = split ' ', <>;\r\n my $res = 0;\r\n\r\n if ($n == 1) {\r\n $res = ($m > 2) ? -1 : ($m - 1);\r\n }\r\n elsif ($m == 1) {\r\n $res = ($n > 2) ? -1 : ($n - 1);\r\n }\r\n else {\r\n my $l = max($n, $m) - 1;\r\n my $sum = $m + $n;\r\n if ($sum % 2 == 1) {\r\n $res = ($l - 1) * 2 + 1;\r\n }\r\n else {\r\n $res = $l * 2;\r\n }\r\n }\r\n print \"$res\\n\";\r\n}"}], "negative_code": [], "src_uid": "6f0d3a7971ffc2571838ecd8bf14238d"} {"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\u00a0\u2014 $$$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}$$$\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy @A;\n\tmy @B;\n\t\n\tfor( split ' ', <> ){\n\t\tmy $q = shift @_;\n\t\t$_ ? ( push @A, $q ) : ( push @B, $q );\n\t\t}\n\t\n\t$debug and print \"@A\";\n\t$debug and print \"@B\";\n\t\n\tpush @A, 2e9 + 1;\n\tunshift @A, -1e9;\n\t\n\tmy @cnt;\n\t\n\tfor my $i ( 1 .. @A - 2 ){\n\t\tmy $cnt = 0;\n\t\t\n\t\tmy $mid;\n\t\t$mid = ( $A[ $i ] + $A[ $i - 1 ] ) / 2;\n\t\t$debug and print \" mid: $mid\";\n\t\t\n\t\twhile( @B ){\n\t\t\tmy $B = shift @B;\n\t\t\tif( $B > $A[ $i ] ){\n\t\t\t\tunshift @B, $B;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t$cnt += $B > $mid;\n\t\t\t}\n\t\t\n\t\t$mid = ( $A[ $i ] + $A[ $i + 1 ] ) / 2;\n\t\t$debug and print \" mid: $mid\";\n\t\t\n\t\twhile( @B ){\n\t\t\tmy $B = shift @B;\n\t\t\tif( $B > $mid ){\n\t\t\t\tunshift @B, $B;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t$cnt ++;\n\t\t\t}\n\t\t\n\t\t$debug and print \" cnt: $cnt\";\n\t\t\n\t\tpush @cnt, $cnt;\n\t\t}\n\t\n\tprint \"@cnt\";\n\t}"}], "negative_code": [], "src_uid": "56ea328f84b2930656ff5eb9b8fda8e0"} {"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$$$) \u2014 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 \u2014 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": "for(1..<>){<>=~/(.+) (.+)/;print$1*length($2+1)-$1.\"\\n\"}"}], "negative_code": [], "src_uid": "dc67dd2102c70ea476df642b863ae8d3"} {"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\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7104;\u00a01\u2009\u2264\u2009m\u2009\u2264\u200910;\u00a01\u2009\u2264\u2009k\u2009\u2264\u20092\u00b7105) \u2014 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\u2009\u00d7\u2009m, 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\u2009\u2264\u2009xi\u2009\u2264\u2009n;\u00a01\u2009\u2264\u2009yi\u2009\u2264\u2009m) 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": "($n,$m,$k)=split/ /,<>;\n\n$_[$i++]=<> for 1..$n;\n\n/ /, $c[$`-1]--, $b[$'-1]++ for <>;\n\nfor (@_){\n\t\n\t$e=$c[$j];\n\twhile(/\\d/g){\n\t\t\n\t\t$&&& ($e+=$b[$J]);\n\t\t\n\t\t$J++;\n\t\t\n\t\t}\n\tpush @a, $e+0;\n\t\n\t$J=0;\n\t$j++;\n\t}\n\nprint \"@a\""}], "negative_code": [], "src_uid": "fad203b52e11706d70e9b2954c295f70"} {"nl": {"description": "A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed.Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment.When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: at first, the text of the comment is written; after that the number of comments is written, for which this comment is a parent comment (i.\u00a0e. the number of the replies to this comments); after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma.For example, if the comments look like: then the first comment is written as \"hello,2,ok,0,bye,0\", the second is written as \"test,0\", the third comment is written as \"one,1,two,2,a,0,b,0\". The whole comments feed is written as: \"hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0\". For a given comments feed in the format specified above print the comments in a different format: at first, print a integer d\u00a0\u2014 the maximum depth of nesting comments; after that print d lines, the i-th of them corresponds to nesting level i; for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. ", "input_spec": "The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. ", "output_spec": "Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input.", "sample_inputs": ["hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0", "a,5,A,0,a,0,A,0,a,0,A,0", "A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0"], "sample_outputs": ["3\nhello test one \nok bye two \na b", "2\na \nA a A a A", "4\nA K M \nB F H L N O \nC D G I P \nE J"], "notes": "NoteThe first example is explained in the statements. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\n$\\ = $/;\n\n$_ = <>;\n\t\n() = / (\\w+),(\\d+) \n\t(?{\n\t\t\n\t\tpush @{ $A[@j - 0] }, $1;\n\t\t\n\t\tpush @j, $2;\n\t\t\n\t\twhile( 1 ){\n\t\t\t@j or last;\n\t\t\t$j = pop @j;\n\t\t\t$j or redo;\n\t\t\tpush @j, -- $j;\n\t\t\tlast;\n\t\t\t}\n\t})\n/gx;\n\nprint for ~~ @A, map \"@$_\", @A"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t\n\tmy $i = 0;\n\tmy $j = 0;\n\tmy @A;\n\tmy @j;\n\t\n\t() = / (\\w+),(\\d+) \n\t\t(?{\n\t\t\t\n\t\t\tpush @{ $A[@j - 0] }, $1;\n\t\t\t\n\t\t\t$debug and print \"<$&>\";\n\t\t\t\n\t\t\tpush @j, $2;\n\t\t\t\n\t\t\twhile( 1 ){\n\t\t\t\t@j or last;\n\t\t\t\t$j = pop @j;\n\t\t\t\t$j or next;\n\t\t\t\t$j --;\n\t\t\t\tpush @j, $j;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t\n\t\t\t$debug and print \"[@j]\";\n\t\t})\n\t/gx;\n\t\n\tprint ~~ @A;\n\tprint \"@$_\" for @A;\n\t\n\t$debug and print '-' x 10;\n\t}"}], "negative_code": [], "src_uid": "da08dd34ac3c05af58926f70abe5acd0"} {"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\u2009\u2264\u2009n\u2009\u2264\u20092000), number of GukiZ's students. The second line contains n numbers a1,\u2009a2,\u2009... an (1\u2009\u2264\u2009ai\u2009\u2264\u20092000) where ai is the rating of i-th student (1\u2009\u2264\u2009i\u2009\u2264\u2009n).", "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": "<>;@a=split' ',<>;\n++$h{$_} for @a;\nfor (reverse(1..2000)) {\n $x{$_}=$cnt+1,$cnt+=$h{$_} if exists $h{$_};\n}\nprint \"$x{$_} \" for @a;\n"}, {"source_code": "<>;@a=split' ',<>;\nfor$x(@a){\n printf \"%d \",1+grep {$x < $_} @a;\n}\n"}, {"source_code": "<>;@a=split' ',<>;\nfor$x(@a){\n printf \"%d \",1+grep {$x < $_} @a;\n}\n\n"}, {"source_code": "<>;@a=split' ',<>;\nfor$x(@a){\n printf \"%d \",1+grep {$x < $_} @a;\n}\n\n"}, {"source_code": "my $n = int<>;\nmy @arr = split / /, <>;\nmy @ans;\n@arr = reverse sort{$a->[0] <=> $b-[0]} map{[$arr[$_], $_]} 0..$n-1;\nfor my $i(0..$n-1) {\n $ans[$arr[$i]->[1]] = 1 + grep {$arr[$i]->[0] < $_->[0]} @arr;\n}\nprint \"@ans\\n\";\n"}, {"source_code": "<>;@a=split' ',<>;\nfor$x(@a){\n printf \"%d \",1+grep {$x < $_} @a;\n}\n"}, {"source_code": "<>;@a=split' ',<>;\nfor$x(@a){\n printf \"%d \",1+grep {$x < $_} @a;\n}\n\n"}, {"source_code": "<>;@a=split' ',<>;\nfor$x(@a){\n printf \"%d \",1+grep {$x < $_} @a;\n}\n\n"}, {"source_code": "use strict;\nuse List::MoreUtils 'firstidx';\n \nmy $range = ;\nmy @numbers = split / /, ;\nmy @sorted = reverse sort { $a <=> $b } @numbers;\n \nfor my $i (0..$range-1) {\n $numbers[$i] = ( firstidx { $_ == $numbers[$i] } @sorted ) + 1;\n}\n \n$, = ' ';\nprint @numbers;"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = split / /, <>;\nforeach (0..$n-1) {\n\tpush @arr, [($_, $a[$_])];\n}\n@arr = sort { $b->[1] <=> $a->[1] } @arr;\n($x, $tmp) = (1, $arr[0]->[1]);\npush @ans, [($arr[0]->[0], 1)];\nforeach (1..$n-1) {\n\tif ($arr[$_]->[1] != $tmp) {\n\t\t$tmp = $arr[$_]->[1];\n\t\t$x = $_+1;\n\t}\n\tpush @ans, [($arr[$_]->[0], $x)];\n}\n@ans = sort { $a->[0] <=> $b->[0] } @ans;\nprint $ans[$_]->[1], \" \" foreach (0..$n-1);"}, {"source_code": "#!perl\n$\\ = \"\\n\";\n\n$n = <>;\n\n@a = split /\\s/, <>;\n\n@a2 = sort {$a <=> $b} @a;\n\nfor my $i (reverse 0..$#a2)\n{\n if (!$h{@a2[$i]})\n {\n $h{@a2[$i]} = $#a2 - $i + 1; \n }\n}\n\nforeach (@a) {push @result, $h{$_};};\n\nprint \"@result\";\n"}, {"source_code": "<>;@a=split' ',<>;\nfor$x(@a){\n printf \"%d \",1+grep {$x < $_} @a;\n}\n\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\n\nmy $n = <>;\nmy @num = split ' ',<>;\nmy (%rank,%freq);\nmy ($idx,$last) = (1,0);\nforeach(@num){\n $freq{$_}++;\n}\nforeach(sort {$b <=> $a} keys %freq){\n $rank{$_} = $last+1;\n $last += $freq{$_};\n}\nforeach(@num){\n print $rank{$_}, \" \";\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\n\nmy $n = <>;\nmy @num = split ' ',<>;\nmy @sorted = sort {$a <=> $b} @num;\nmy (%rank,%cnt);\nmy ($idx,$last) = (1,0);\nforeach(@sorted){\n $cnt{$_}++;\n}\nforeach(sort {$b <=> $a} keys %cnt){\n $rank{$_} = $last+1;\n $last += $cnt{$_};\n}\nforeach(@num){\n print $rank{$_}, \" \";\n}\n"}, {"source_code": "<>;@a=split' ',<>;\nfor$x(@a){\n printf \"%d \",1+grep {$x < $_} @a;\n}\n\n"}, {"source_code": "<>;@a=split' ',<>;\nfor$x(@a){\n printf \"%d \",1+grep {$x < $_} @a;\n}\n\n"}, {"source_code": "<>;@a=split' ',<>;\nfor$x(@a){\n printf \"%d \",1+grep {$x < $_} @a;\n}\n\n"}], "negative_code": [{"source_code": "my $t = ;\nmy @data = ;\n\nfor (my $i = 0; $i <= $t; $i++)\n{\n if ( ( 1 + $data[$i]) > ( 1 + $data[$i+1]) )\n {\n $tmp = $data[$i+1];\n $data[$i+1] = $data[$i];\n $data[$i] = $tmp; \n }\n}\n\nprint @data;"}, {"source_code": "use strict;\nmy $t = ;\nmy @input = split //, ;\n\nmy $max = 0;\nfor my $i ( @input ) {\n if ( $i > $max ) {\n $max = $i;\n }\n}\n\nmy %ranks;\nmy $rank = 1;\n\nwhile( $max ) {\n if ( grep { $_ == $max } @input ) {\n my $plus = scalar grep { $_ == $max } @input;\n $ranks{ $max } = $rank;\n $rank += $plus;\n }\n $max--;\n}\n\nprint $ranks{$_} for @input;"}, {"source_code": "use strict;\n$, = ' ';\nmy $t = ;\nmy @input = split //, ;\nmy $max = 0;\nfor my $i ( @input ) {\n if ( $i > $max ) {\n $max = $i;\n }\n}\n\nmy %ranks;\nmy $rank = 1;\nmy @values;\nwhile( $max ) {\n if ( grep { $_ == $max } @input ) {\n my $plus = scalar grep { $_ == $max } @input;\n $ranks{ $max } = $rank;\n $rank += $plus;\n }\n $max--;\n}\n\npush @values, $ranks{$_} for @input;\nprint @values;"}, {"source_code": "use strict;\nuse List::MoreUtils 'firstidx';\n\nmy $range = ;\nmy @numbers = split //, ;\nmy @sorted = reverse sort @numbers;\n\nfor my $i (0..$range-1) {\n $numbers[$i] = ( firstidx { /$numbers[$i]/ } @sorted ) + 1;\n}\n\n$, = ' ';\nprint @numbers;"}, {"source_code": "use strict;\nuse List::MoreUtils 'firstidx';\n \nmy $range = ;\nmy @numbers = split / /, ;\nmy @sorted = reverse sort @numbers;\n \nfor my $i (0..$range-1) {\n $numbers[$i] = ( firstidx { /$numbers[$i]/ } @sorted ) + 1;\n}\n \n$, = ' ';\nprint @numbers;"}], "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 \u200b\u200bof for all i from 1 to n. The expression \u230a x\u230b 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\u2009=\u200946, 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\u2009\u2264\u2009n\u2009\u2264\u2009105, 0\u2009\u2264\u2009k\u2009\u2264\u2009107) \u2014 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\u2009\u2264\u2009ai\u2009\u2264\u2009100), 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 \u2014 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\u2009+\u2009 lfloor frac{100}{10} rfloor\u2009=\u200910\u2009+\u200910\u2009=\u2009 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": "#!perl\n$\\ = \"\\n\";\n\n($n, $k) = split /\\s/, <>;\n@a = split /\\s/, <>;\n\n$max = $n*10;\n\n$real = 0;\nfor my $i (@a)\n{\n $real += int($i/10);\n $mod = $i%10;\n push @a1, 10 - $mod if $mod != 0; \n}\n\n@a1 = sort {$a <=> $b} @a1;\n\n$i = 0;\nwhile ($real < $max and $k > 0 and $i <= $#a1)\n{\n $k -= @a1[$i];\n last if $k < 0 ;\n $real++;\n $i++;\n}\n\nwhile ($real < $max and $k >= 10)\n{\n $k -= 10;\n last if $k < 0 ;\n $real++;\n} \n\nprint $real;"}], "negative_code": [{"source_code": "#!perl\n$\\ = \"\\n\";\n\n($n, $k) = split /\\s/, <>;\n@a = split /\\s/, <>;\n\n$max = $n*10;\n\n$real = 0;\nfor my $i (@a)\n{\n $real += int($i/10);\n $mod = $i%10;\n push @a1, 10 - $mod if $mod != 0; \n}\n\n@a1 = sort {$a <=> $b} @a1;\n\n$i = 0;\nwhile ($real < $max and $k > 0 and $i <= $#a1)\n{\n $real++;\n $i++;\n $k -= @a1[$i];\n}\n\nwhile ($real < $max and $k >= 10)\n{\n $real++;\n $k -= 10;\n} \n\nprint $real;"}], "src_uid": "b4341e1b0ec0b7341fdbe6edfe81a0d4"} {"nl": {"description": "You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \\le u, v \\le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree.", "output_spec": "Output a single integer $$$k$$$ \u2014 the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property.", "sample_inputs": ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"], "sample_outputs": ["1", "-1", "4", "0"], "notes": "NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$."}, "positive_code": [{"source_code": "use warnings;\nuse strict;\n$\\ = qq/\\n/, $, = qq/ /;\nchomp ( my $n = );\nmy %graph = ();\nfor ( 2 .. $n ) {\n my ( $u, $v ) = split qq/ /, ;\n $graph { $u } -> { $v } = 1;\n $graph { $v } -> { $u } = 1;\n}\nmy %nodes = ();\nmy %visited = ();\nmy $removed = 0;\nsub dfs {\n return 0 if $visited { $_ [ 0 ] };\n my $curr = $_ [ 0 ];\n $visited { $curr } = 1;\n my $cnt = 1;\n for my $ii ( keys %{ $graph { $curr } } ) { $cnt += dfs ( $ii ) }\n if ( $cnt % 2 == 0 ) {\n $removed += 1;\n return 0;\n } else { return $cnt }\n}\ndfs ( 1 );\nif ( $n % 2 == 1 ) { print qq/-1/ }\nelsif ( $removed == 0 ) { print qq/-1/ }\nelse { print ( $removed - 1 ) }\n"}, {"source_code": "$n = <>;\n%graph = map { $_ => [] } (1..$n);\nfor (2..$n) {\n\t($u, $v) = split ' ', <>;\n\tpush @{$graph{$u}}, $v;\n\tpush @{$graph{$v}}, $u;\n}\n\nsub dfs {\n\tmy $v = shift || 1;\n\tmy $par = shift || -1;\n\t(my $removed, my $size) = (0, 1);\n\tfor my $u (@{$graph{$v}}) {\n\t\tif ($u != $par) {\n\t\t\tmy $child = dfs($u, $v); # [removed, size]\n\t\t\t$removed += $child->[0];\n\t\t\tif (($child->[1]) % 2 == 0) {\n\t\t\t\t$removed += 1;\n\t\t\t}\n\t\t\t$size += $child->[1];\n\t\t}\n\t}\n\treturn [$removed, $size];\n}\n\n$removed = dfs()->[0];\nprint ($n % 2 == 0 ? $removed : -1);"}, {"source_code": "$_ = <>;\n\nmap {\n\t( $u, $v ) = split ' ', <>;\n\t$h{ $u }{ $v } ++;\n\t$h{ $v }{ $u } ++;\n\t} 2 .. $_;\n\nexit if $_ % 2 and print -1;\n\n@g = ( 1 ) x ( $_ + 1 );\n\n%N = %h;\n\nwhile( @_ = grep 1 == keys %{ $h{ $_ } }, keys %N ){\n\t\t\t\t\t\n\tundef %N;\n\t\n\tfor( @_ ){\n\t\tnext if not $P = ( keys %{ $h{ $_ } } )[ 0 ];\n\t\t$N{ $P } ++;\n\t\tdelete $h{ $P }{ $_ };\n\t\tdelete $h{ $_ };\n\t\t\n\t\t$g[ $_ ] % 2 ? $g[ $P ] += $g[ $_ ] : ++ $C;\n\t\t}\n\t}\n\nprint 0 + $C"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy %h;\n\t\n\tmap {\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t$h{ $u }{ $v } ++;\n\t\t$h{ $v }{ $u } ++;\n\t\t} 1 .. $_ - 1;\n\t\n\tif( $_ % 2 ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\tmy @leafs = grep { 1 == keys %{ $h{ $_ } } } keys %h;\n\t\n\t$debug and print \"leafs: @leafs\";\n\t\n\tmy @g = ( 1 ) x ( $_ + 1 );\n\t\n\tmy $cnt = 0;\n\t\n\tmy %next = map { $_ => 1 } @leafs;\n\t\n##\tmy $c = 0;\n\t\n\twhile( 1 ){\n\t\t\n\t##\t$c ++ > 15 and last;\n\t\t\n\t\t@leafs = grep { 1 == keys %{ $h{ $_ } } } keys %next;\n\t\t\n\t\t$debug and print \" leafs: @leafs\";\n\t\t\n\t\tlast if not @leafs;\n\t\t\n\t\tundef %next;\n\t\t\n\t\twhile( @leafs ){\n\t\t\tmy $leaf = shift @leafs;\n\t\t\tmy( $parent ) = keys %{ $h{ $leaf } };\n\t\t\tnext if not $parent;\n\t\t\t$next{ $parent } ++;\n\t\t\tdelete $h{ $parent }{ $leaf };\n\t\t\tdelete $h{ $leaf };\n\t\t\t\t\n\t\t\tif( $g[ $leaf ] % 2 == 0 ){\n\t\t\t\t$cnt ++;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$g[ $parent ] += $g[ $leaf ];\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t}\n\t\n\tprint $cnt;\n\t\n\t$debug and print '-' x 15;\n\t}"}, {"source_code": "$_ = <>;\n\nmap {\n\t( $u, $v ) = split ' ', <>;\n\t$h{ $u }{ $v } ++;\n\t$h{ $v }{ $u } ++;\n\t} 2 .. $_;\n\nexit if $_ % 2 and print -1;\n\n@g = ( 1 ) x ( $_ + 1 );\n\n%N = %h;\n\nwhile( @_ = grep 1 == keys %{ $h{ $_ } }, keys %N ){\n\t\t\t\t\t\n\tundef %N;\n\t\n\tfor( @_ ){\n\t\tnext if not $P = ( keys %{ $h{ $_ } } )[ 0 ];\n\t\t$N{ $P } ++;\n\t\tdelete $h{ $P }{ $_ };\n\t\tdelete $h{ $_ };\n\t\t\n\t\t$g[ $_ ] % 2 ? $g[ $P ] += $g[ $_ ] : ++ $C;\n\t\t}\n\t}\n\nprint 0 + $C"}], "negative_code": [{"source_code": "use warnings;\nuse strict;\n$\\ = qq/\\n/;\nmy $n = ;\nif ( $n == 0 ) { print qq/0/ }\nelsif ( $n % 2 == 0 ) { print $n - 1 - int ( $n / 2 ) }\nelse { print qq/-1/ }\n"}, {"source_code": "use warnings;\nuse strict;\n$\\ = qq/\\n/, $, = qq/ /;\nchomp ( my $n = );\nmy %graph = ();\nfor ( 2 .. $n ) {\n my ( $u, $v ) = split qq/ /, ;\n $graph { $u } -> { $v } = 1;\n $graph { $v } -> { $u } = 1;\n}\nmy %nodes = ();\nmy %visited = ();\nmy $removed = 0;\nsub dfs {\n return 0 if $visited { $_ [ 0 ] };\n my $curr = $_ [ 0 ];\n $visited { $curr } = 1;\n my $cnt = 1;\n for my $ii ( keys %{ $graph { $curr } } ) { $cnt += dfs ( $ii ) }\n if ( $cnt % 2 == 0 ) {\n $removed += 1;\n return 0;\n } else { return $cnt }\n}\ndfs ( 1 );\nif ( $removed == 0 ) { print qq/-1/ }\nelse { print ($removed - 1) }\n"}, {"source_code": "use warnings;\nuse strict;\n$\\ = qq/\\n/;\nmy $n = ;\nprint qq/-1/ if $n % 2 == 1;\nprint $n - 1 - int ( $n / 2 ) if $n % 2 == 0;\n"}, {"source_code": "use warnings;\nuse strict;\n$\\ = qq/\\n/;\nmy $n = ;\nmy %graph = ();\nmy ( $u, $v );\nfor my $edge ( 1 .. $n-1 ) {\n ( $u, $v ) = split ( qq/ /, );\n $graph { $u } = 0 if not exists $graph { $u };\n $graph { $u } += 1;\n $graph { $v } = 0 if not exists $graph { $v };\n $graph { $v } += 1;\n}\nmy $val = grep { $_ == 1 } values %graph;\nsub min { $_ [ 0 ] < $_ [ 1 ] ? $_ [ 0 ] : $_ [ 1 ] }\nsub max { $_ [ 0 ] > $_ [ 1 ] ? $_ [ 0 ] : $_ [ 1 ] }\nif ( $n % 2 == 1 ) { print qq/-1/ }\nelse { print STDOUT max ( 0, min ( $n - 1 - $val, $n - 1 - int ( $n / 2 ) ) ) }\n\n"}], "src_uid": "711896281f4beff55a8826771eeccb81"} {"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,\u2009c2,\u2009...,\u2009cn. The Old Peykan wants to travel from city c1 to cn using roads. There are (n\u2009-\u20091) one way roads, the i-th road goes from city ci to city ci\u2009+\u20091 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\u2009\u2264\u2009m,\u2009k\u2009\u2264\u20091000). The value m specifies the number of roads between cities which is equal to n\u2009-\u20091. The next line contains m space-separated integers d1,\u2009d2,\u2009...,\u2009dm (1\u2009\u2264\u2009di\u2009\u2264\u20091000) and the following line contains m space-separated integers s1,\u2009s2,\u2009...,\u2009sm (1\u2009\u2264\u2009si\u2009\u2264\u20091000).", "output_spec": "In the only line of the output print a single integer \u2014 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": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Thu Nov 1 21:33:17 IST 2012\n# File Name: a.pl\n# USAGE: \n# a.pl \n# \n# \n#------------------------------------------------\nuse strict;\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nmy $buf;\n\nchomp ($buf = );\nmy ($m, $k) = split /\\s+/, $buf;\n\nchomp ($buf = );\nmy @d = split /\\s+/, $buf;\n\nchomp ($buf = );\nmy @f = split /\\s+/, $buf;\n\nmy ($time, $fuel) = (0, 0);\nmy ($curfuel, $maxfuel) = (0, 0);\n\nforeach (@d) {\n $curfuel = shift @f;\n $fuel += $curfuel;\n $maxfuel = $maxfuel > $curfuel ? $maxfuel : $curfuel;\n\n while ($fuel < $_) {\n $fuel += $maxfuel;\n $time += $k;\n }\n $time += $_;\n $fuel -= $_;\n}\nprint $time;\n"}], "negative_code": [{"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Thu Nov 1 21:33:17 IST 2012\n# File Name: a.pl\n# USAGE: \n# a.pl \n# \n# \n#------------------------------------------------\nuse strict;\nmy $buf;\n\nchomp ($buf = );\nmy ($m, $k) = split /\\s+/, $buf;\n\nchomp ($buf = );\nmy @d = split /\\s+/, $buf;\n\nchomp ($buf = );\nmy @f = split /\\s+/, $buf;\n\nmy ($time, $fuel) = (0, 0);\n\nforeach (@d) {\n my $curfuel = shift @f;\n $fuel += $curfuel;\n while ($fuel < $_) {\n $fuel += $curfuel;\n $time += $k;\n }\n $time += $_;\n $fuel -= $_;\n}\nprint $time;\n"}], "src_uid": "94f84b89e98228de170ae68c5cb8f375"} {"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$$$) \u2014 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": "use warnings;\nuse strict;\n$\\ = qq/\\n/, $, = qq/ /;\n;\nsub ceil { int ( $_ [ 0 ] ) < $_ [ 0 ] ? int ( $_ [ 0 ] ) + 1 : $_ [ 0 ] }\nmy @arr = split qq/ /, ;\nmy $sum = 0;\n$sum += $_ for @arr;\nmy $one = ceil ( $sum / 2 );\nmy ( $two, $ii ) = ( 0, 0 );\nwhile ( $two < $one ) {\n $two += $arr [ $ii ];\n $ii += 1;\n}\nprint $ii;\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = eval join ' + ', @_;\n\t\n\tmy $half = 0;\n\t\n\tmy $ans;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\t$half += $_[ $i ];\n\t\t$half >= $sum / 2 and $ans = $i;\n\t\t$half >= $sum / 2 and last;\n\t\t}\n\t\n\tprint $ans + 1;\n\t}"}, {"source_code": "$_ = <>;\n\n$s = eval join ' + ', @_ = split ' ', <>;\n\n( $h += $_[ $_ - 1 ] ) >= $s / 2 and print and last for 1 .. $_"}], "negative_code": [], "src_uid": "241157c465fe5dd96acd514010904321"} {"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$$$)\u00a0\u2014 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\u00a0\u2014 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": "<>;\r\n\r\nprint +( sort { $a <=> $b } split )[ 1 ] . $/ while <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tprint +( sort { $a <=> $b } split )[ 1 ];\n\t}"}], "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,\u2009m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100) \u2014 the number of streets and avenues in Munhattan. Each of the next n lines contains m integers cij (1\u2009\u2264\u2009cij\u2009\u2264\u2009109) \u2014 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 \u2014 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": "$_ = <>;\n$c = 0;\nwhile ( <> ) {\n\t@a = split;\n\t$min = 10**9;\n\tforeach (@a) {\n\t\t$min = $min < $_? $min : $_;\n\t}\n\t$c = $min > $c? $min : $c;\n}\nprint $c;\n"}, {"source_code": "<>;\n\nprint( (sort map { (sort map { sprintf \"%010s\", $_ } split)[ 0 ] } <> )[ -1 ] =~ s/^0+//r )"}, {"source_code": "<>;\n\nprint( (sort {$b <=> $a} map { (sort {$a <=> $b} split)[ 0 ] } <> )[ 0 ] )"}], "negative_code": [], "src_uid": "f2142bc2f44e5d8b77f8561c29038a73"} {"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$$$) \u2014 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$$$) \u2014 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": "use strict;\nuse warnings;\nuse integer;\n\n<>;\nwhile (<>) {\n\t/(\\d+) (\\d+) (\\d+)/;\n\tprint +($3 / 2) * ($1 - $2) + ($3 % 2) * $1, \"\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse bigint;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\t\n\tfor( @_ ){\n\t\tmy( $A, $B, $k ) = split;\n\t\t\n\t\tif( $k % 2 == 1 ){\n\t\t\tprint $A * ( $k >> 1 ) + $A - $B * ( $k >> 1 );\n\t\t\t}\n\t\telse{\n\t\t\tprint $A * ( $k >> 1 ) - $B * ( $k >> 1 );\n\t\t\t}\n\t\t\n\t\t}\n\t\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\t\n\tfor( @_ ){\n\t\tmy( $A, $B, $k ) = split;\n\t\t\n\t\tif( $k % 2 == 1 ){\n\t\t\tprint $A * int( $k / 2 ) + $A - $B * int( $k / 2 );\n\t\t\t}\n\t\telse{\n\t\t\tprint 0 + ( $A - $B ) / 2 * $k;\n\t\t\t}\n\t\t\n\t\t}\n\t\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse bigint;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\t\n\tfor( @_ ){\n\t\tmy( $A, $B, $k ) = split;\n\t\t\n\t\tif( $k % 2 == 1 ){\n\t\t\tprint $A * int( $k / 2 ) + $A - $B * int( $k / 2 );\n\t\t\t}\n\t\telse{\n\t\t\tprint 0 + ( $A - $B ) / 2 * $k;\n\t\t\t}\n\t\t\n\t\t}\n\t\n\t}"}], "src_uid": "1f435ba837f59b007167419896c836ae"} {"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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$)\u00a0\u2014 the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\leq a_i \\leq 10^9$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t@_ = sort { $a <=> $b } @_;\n\t\n\tmy $f = 0;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\t$f |= $_[ $i ] == $_[ $i + 1 ];\n\t\t}\n\t\n\tprint $f ? 'NO' : 'YES';\n\t}"}], "negative_code": [], "src_uid": "288f147bb8a3c30b0bb712a01c65109b"} {"nl": {"description": "In Berland, there is the national holiday coming \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009105) and m (1\u2009\u2264\u2009m\u2009\u2264\u2009105) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=<>;\nfor (@_){\n\t@x=();\n\t@a=split/ /;\n\tfor $i(@a){\n\t\tif ($m[$i]){push @x, $m[$i]}\n\t}\n\tfor $i(@a){\n\t\t\tif (!$m[$i]){\n\t\t\t$f=1;\n\t\t\t\twhile ($f){\n\t\t\t\t$j++; $f=0;\n\t\t\t\tfor $x(@x){\n\t\t\t\t\tif ($j%3+1 == $x){$f++} \n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t$m[$i]=$j%3+1;\n\t\t\t\tpush @x, $m[$i];\n\t\t\t}\n\t\t}\n\t}\nshift @m;\nprint \"@m\";"}], "negative_code": [], "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$$$)\u00a0\u2014 the number of queries. The only line of each query contains one integer $$$n$$$ ($$$2 \\le n \\le 10^9$$$)\u00a0\u2014 the number of matches.", "output_spec": "For each test case print one integer in single line\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tprint $_ == 2 ? 2 : $_ % 2 ? 1 : 0;\n\t}"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\nsub remainder {\n my ($dividend, $divisor) = @_;\n}\n\n# solve\n\nmy $q = read_token;\n\nfor (1..$q) {\n my $n = read_token;\n if ($n == 2) {\n say 2;\n next;\n }\n\n if ($n % 2 == 1) {\n say 1;\n }\n else {\n say 0;\n }\n}\n"}], "negative_code": [], "src_uid": "178876bfe161ba9ccfd80c9310f75cbc"} {"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,\u2009j between 1 and n, he wrote the number ai,\u2009j\u2009=\u2009min(pi,\u2009pj). He writes ai,\u2009i\u2009=\u20090 for all integer i from 1 to n.Bob gave you all the values of ai,\u2009j 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\u2009\u2264\u2009n\u2009\u2264\u200950). The next n lines will contain the values of ai,\u2009j. The j-th number on the i-th line will represent ai,\u2009j. The i-th number on the i-th line will be 0. It's guaranteed that ai,\u2009j\u2009=\u2009aj,\u2009i 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,\u20092} or {2,\u20091}.In the second case, another possible answer is {2,\u20094,\u20095,\u20091,\u20093}."}, "positive_code": [{"source_code": "$\\ = $/;\n\nwhile($n = <>){\n\tmy @A;\n\t\n\tfor $i (1 .. $n){\n\t\t@{ $A[$i] } = (0, split ' ', <>);\n\t\t}\n\t\n\t@_ = ();\n\tfor $i (1 .. $n){\n\t\tpush @_, (sort {$b <=> $a} @{ $A[$i] }, map $A[$_][$i], 1 .. $n)[ 0 ];\n\t\t} \n\t\n\t@n = 1 .. $n;\n\t$i = $_, @n = grep $_ != $i, @n for @_;\n\t\n\t@n = sort {$a <=> $b} @n;\n\t\n\t@used = 1 .. $n;\n\tfor $i (@_){\n\t\tif ($used[ $i ]){\n\t\t\t$used[ $i ] = 0;\n\t\t\t}\n\t\telse {\n\t\t\tfor $j (@n){\n\t\t\t\t$j > $i and do {\n\t\t\t\t\t$i = $j;\n\t\t\t\t\t@n = grep $_ != $j, @n;\n\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t\n\t\t}\n\t\n\tprint \"@_\"\n\t}"}], "negative_code": [], "src_uid": "1524c658129aaa4175208b278fdc467c"} {"nl": {"description": "There is a given sequence of integers a1,\u2009a2,\u2009...,\u2009an, 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\u2009\u2264\u2009n\u2009\u2264\u2009106). The second line contains a sequence of integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20093).", "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": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\nuse Data::Dumper;\n\nmy $N = <>;\nchomp $N;\nmy $num_str = <>;\nchomp $num_str;\nmy @nums = split /\\s+/, $num_str;\n\n#print\"$N input <\".join(',',@nums).\"\\n\";\n\nmy @a =(0,0,0,0); \nmap{$a[$_]+=1} @nums;\n#print \"ansss:\" .join(',',@a);\nmy $max = ($a[1] > $a[2])? $a[1]:$a[2];\n$max = $a[3] if $a[3] > $max;\n#print\"max=$max\\n\";\nmy $ans = $N-$max;\nprint \"$ans\\n\";\nexit;\n\n"}], "negative_code": [], "src_uid": "fcb6a715dfe302d7ae5a6695ca8976aa"} {"nl": {"description": "Recently Vladik discovered a new entertainment\u00a0\u2014 coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them.At first, he need to download t chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In particular, some of the messages have no information of their sender. It is known that if a person sends several messages in a row, they all are merged into a single message. It means that there could not be two or more messages in a row with the same sender. Moreover, a sender never mention himself in his messages.Vladik wants to recover senders of all the messages so that each two neighboring messages will have different senders and no sender will mention himself in his messages.He has no idea of how to do this, and asks you for help. Help Vladik to recover senders in each of the chats!", "input_spec": "The first line contains single integer t (1\u2009\u2264\u2009t\u2009\u2264\u200910) \u2014 the number of chats. The t chats follow. Each chat is given in the following format. The first line of each chat description contains single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100)\u00a0\u2014 the number of users in the chat. The next line contains n space-separated distinct usernames. Each username consists of lowercase and uppercase English letters and digits. The usernames can't start with a digit. Two usernames are different even if they differ only with letters' case. The length of username is positive and doesn't exceed 10 characters. The next line contains single integer m (1\u2009\u2264\u2009m\u2009\u2264\u2009100)\u00a0\u2014 the number of messages in the chat. The next m line contain the messages in the following formats, one per line: <username>:<text>\u00a0\u2014 the format of a message with known sender. The username should appear in the list of usernames of the chat. <?>:<text>\u00a0\u2014 the format of a message with unknown sender. The text of a message can consist of lowercase and uppercase English letter, digits, characters '.' (dot), ',' (comma), '!' (exclamation mark), '?' (question mark) and ' ' (space). The text doesn't contain trailing spaces. The length of the text is positive and doesn't exceed 100 characters. We say that a text mention a user if his username appears in the text as a word. In other words, the username appears in a such a position that the two characters before and after its appearance either do not exist or are not English letters or digits. For example, the text \"Vasya, masha13 and Kate!\" can mention users \"Vasya\", \"masha13\", \"and\" and \"Kate\", but not \"masha\". It is guaranteed that in each chat no known sender mention himself in his messages and there are no two neighboring messages with the same known sender.", "output_spec": "Print the information about the t chats in the following format: If it is not possible to recover senders, print single line \"Impossible\" for this chat. Otherwise print m messages in the following format: <username>:<text> If there are multiple answers, print any of them.", "sample_inputs": ["1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi", "1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine", "2\n3\nnetman vladik Fedosik\n2\n?: users are netman, vladik, Fedosik\nvladik: something wrong with this chat\n4\nnetman tigerrrrr banany2001 klinchuh\n4\n?: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\n?: yes, netman\nbanany2001: yes of course."], "sample_outputs": ["netman: Hello, Vladik!\nVladik: Hi", "Impossible", "Impossible\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\ntigerrrrr: yes, netman\nbanany2001: yes of course."], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tfor (1 .. $_){\n\t\t$debug and print '-' x 10;\n\t\t\n\t\tmy $n = <>;\n\t\tmy %names = map { $_, 1 } split ' ', <>;\n\t\tmy $m = <>;\n\t\tmy @msgs = map ~~<>, 1 .. $m;\n\t\t\n\t\tchomp @msgs;\n\t\t\n\t\tunshift @msgs, 'a!:';\n\t\tpush @msgs, 'z!:';\n\t\t\n\t\tfor (@msgs){\n\t\t\tmy( $name, $text ) = split ':';\n\t\t\tmy @found_names = grep { $names{ $_ } } /\\w+/g;\n\t\t\t\n\t\t\t$debug and print \"${name}[@found_names]\";\n\t\t\t\n\t\t\tmy %cand_names;\n\t\t\t\n\t\t\tif( $name eq '?' ){\n\t\t\t\t%cand_names = %names;\n\t\t\t\tdelete $cand_names{ $_ } for @found_names;\n\t\t\t}\n\t\t\t\t\t\t\n\t\t\t$_ = {\n\t\t\t\tname => $name,\n\t\t\t\ttext => $text,\n\t\t\t\tcand => { %cand_names },\n\t\t\t\t};\n\t\t\t}\n\t\t\n\t\t$debug and do {\n\t\t\tfor (@msgs){ \n\t\t\t\tprint \"[$_->{name}]\",\n\t\t\t\t\t\"[$_->{text}]\",\n\t\t\t\t\t\"[\", (join ' ', keys %{$_->{cand}} ), \"]\"\n\t\t\t\t}\n\t\t\t};\n\t\t\n\t\tmy $FAIL = 0;\n\t\tmy $del = 1;\n\t\t\n\t\twhile( $del ){\n\t\t\t$del = 0;\n\t\t\tfor my $i (1 .. @msgs - 2, reverse 1 .. @msgs - 2){\n\t\t\t\tnext if $msgs[$i]->{name} ne '?';\n\t\t\t\t\n\t\t\t\t$debug and print map \"<<$_>>\", join ' ', keys %{ $msgs[$i]->{cand} };\n\t\t\t\t\n\t\t\t\tdelete $msgs[$i]->{cand}->{ $msgs[$i-1]->{name} } and $del ++;\n\t\t\t\tdelete $msgs[$i]->{cand}->{ $msgs[$i+1]->{name} } and $del ++;\n\t\t\t\t\n\t\t\t\t$debug and print \"del:$del\";\n\t\t\t\t\n\t\t\t\tmy $num = keys %{ $msgs[$i]->{cand} };\n\t\t\t\tif( $num == 1 ){\n\t\t\t\t\t( $msgs[$i]->{name} ) = keys %{ $msgs[$i]->{cand} };\n\t\t\t\t\t}\n\t\t\t\telsif( $num == 0 ){\n\t\t\t\t\t$FAIL = 1;\n\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t$debug and do {\n\t\t\tfor (@msgs){ \n\t\t\t\tprint \"[$_->{name}]\",\n\t\t\t\t\t\"[$_->{text}]\",\n\t\t\t\t\t\"[\", (join ' ', keys %{$_->{cand}} ), \"]\"\n\t\t\t\t}\n\t\t\t};\n\t\t\n\t\tmy $idx = -2;\n\t\tmy $key;\n\t\t\n\t\tfor my $i (0 .. @msgs - 1){\n\t\t\tlast if $FAIL;\n\t\t\t\n\t\t\t$idx == $i - 1 and delete $msgs[$i]->{cand}->{$key};\n\t\t\t\n\t\t\t$msgs[$i]->{name} eq '?' and do {\n\t\t\t\t$key = ( keys %{ $msgs[$i]->{cand} } )[ 0 ];\n\t\t\t\t%{ $msgs[$i]->{cand} } = ();\n\t\t\t\t$msgs[$i]->{name} = $key;\n\t\t\t\t$idx = $i;\n\t\t\t\t};\n\t\t\t}\n\t\t\n\t\tshift @msgs;\n\t\tpop @msgs;\n\t\t\n\t\tprint $FAIL ? \"Impossible\" : join \"\\n\", map { \"$_->{name}:$_->{text}\" } @msgs;\n\t\t\n\t\t}\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\tfor (1 .. $_){\n\t\t\t\t\n\t\t<>;\n\t\tmy %names = map { $_, 1 } split ' ', <>;\n\t\tmy @msgs = map ~~<>, 1 .. <>;\n\t\t\n\t\tchomp @msgs;\n\t\t\n\t\t@msgs = ( '!:', @msgs, '!:' );\n\t\t\n\t\tfor (@msgs){\n\t\t\tmy( $name, $text ) = split ':';\n\t\t\t\t\t\t\n\t\t\tmy %cand;\n\t\t\t\n\t\t\tif( $name eq '?' ){\n\t\t\t\t%cand = %names;\n\t\t\t\tdelete $cand{ $_ } for grep $names{ $_ }, /\\w+/g;\n\t\t\t}\n\t\t\t\t\t\t\n\t\t\t$_ = {\n\t\t\t\tname => $name,\n\t\t\t\ttext => $text,\n\t\t\t\tcand => { %cand },\n\t\t\t\t};\n\t\t\t}\n\t\t\n\t\tmy $FAIL = 0;\n\t\tmy $del = 1;\n\t\t\n\t\twhile( $del ){\n\t\t\t$del = 0;\n\t\t\tfor my $i (1 .. @msgs - 2, reverse 1 .. @msgs - 2){\n\t\t\t\tnext if $msgs[$i]->{name} ne '?';\n\t\t\t\t\t\t\t\t\n\t\t\t\tdelete $msgs[$i]->{cand}->{ $msgs[$i + $_]->{name} } and $del ++ for -1, 1;\n\t\t\t\t\t\t\t\t\n\t\t\t\tmy $num = keys %{ $msgs[$i]->{cand} };\n\t\t\t\tif( $num == 1 ){\n\t\t\t\t\t( $msgs[$i]->{name} ) = keys %{ $msgs[$i]->{cand} };\n\t\t\t\t\t}\n\t\t\t\telsif( $num == 0 ){\n\t\t\t\t\t$FAIL = 1;\n\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\tmy $idx = -2;\n\t\tmy $key;\n\t\t\n\t\tfor my $i (0 .. @msgs - 1){\n\t\t\tlast if $FAIL;\n\t\t\t\n\t\t\t$idx == $i - 1 and delete $msgs[$i]->{cand}->{$key};\n\t\t\t\n\t\t\t$msgs[$i]->{name} eq '?' and do {\n\t\t\t\t$key = ( keys %{ $msgs[$i]->{cand} } )[ 0 ];\n\t\t\t\t%{ $msgs[$i]->{cand} } = ();\n\t\t\t\t$msgs[$i]->{name} = $key;\n\t\t\t\t$idx = $i;\n\t\t\t\t};\n\t\t\t}\n\t\t\n\t\tprint $FAIL ? \"Impossible\" : join \"\\n\", map { \"$_->{name}:$_->{text}\" } @msgs[1 .. @msgs-2];\n\t\t\n\t\t}\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\tfor (1 .. $_){\n\t\t\t\t\n\t\t<>;\n\t\tmy %names = map { $_, 1 } split ' ', <>;\n\t\tmy @msgs = map ~~<>, 1 .. <>;\n\t\t\n\t\tchomp @msgs;\n\t\t\n\t\t@msgs = ( '!:', @msgs, '!:' );\n\t\t\n\t\tfor (@msgs){\n\t\t\tmy( $name, $text ) = split ':';\n\t\t\t\t\t\t\n\t\t\tmy %cand;\n\t\t\t\n\t\t\tif( $name eq '?' ){\n\t\t\t\t%cand = %names;\n\t\t\t\tdelete $cand{ $_ } for grep $names{ $_ }, /\\w+/g;\n\t\t\t}\n\t\t\t\t\t\t\n\t\t\t$_ = {\n\t\t\t\tname => $name,\n\t\t\t\ttext => $text,\n\t\t\t\tcand => { %cand },\n\t\t\t\t};\n\t\t\t}\n\t\t\n\t\tmy $FAIL = 0;\n\t\tmy $del = 1;\n\t\t\n\t\twhile( $del ){\n\t\t\t$del = 0;\n\t\t\tfor my $i (1 .. @msgs - 2, 1 .. @msgs - 2){\n\t\t\t\tnext if $msgs[$i]->{name} ne '?';\n\t\t\t\t\t\t\t\t\n\t\t\t\tdelete $msgs[$i]->{cand}->{ $msgs[$i + $_]->{name} } and $del ++ for -1, 1;\n\t\t\t\t\t\t\t\t\n\t\t\t\tmy $num = keys %{ $msgs[$i]->{cand} };\n\t\t\t\tif( $num == 1 ){\n\t\t\t\t\t( $msgs[$i]->{name} ) = keys %{ $msgs[$i]->{cand} };\n\t\t\t\t\t}\n\t\t\t\telsif( $num == 0 ){\n\t\t\t\t\t$FAIL = 1;\n\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\tmy $idx = -2;\n\t\tmy $key;\n\t\t\n\t\tfor my $i (0 .. @msgs - 1){\n\t\t\tlast if $FAIL;\n\t\t\t\n\t\t\t$idx == $i - 1 and delete $msgs[$i]->{cand}->{$key};\n\t\t\t\n\t\t\t$msgs[$i]->{name} eq '?' and do {\n\t\t\t\t$key = ( keys %{ $msgs[$i]->{cand} } )[ 0 ];\n\t\t\t\t%{ $msgs[$i]->{cand} } = ();\n\t\t\t\t$msgs[$i]->{name} = $key;\n\t\t\t\t$idx = $i;\n\t\t\t\t};\n\t\t\t}\n\t\t\n\t\tprint $FAIL ? \"Impossible\" : join \"\\n\", map { \"$_->{name}:$_->{text}\" } @msgs[1 .. @msgs-2];\n\t\t\n\t\t}\n\t\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tfor (1 .. $_){\n\t\t$debug and print '-' x 10;\n\t\t\n\t\tmy $n = <>;\n\t\tmy %names = map { $_, 1 } split ' ', <>;\n\t\tmy $m = <>;\n\t\tmy @msgs = map ~~<>, 1 .. $m;\n\t\t\n\t\tchomp @msgs;\n\t\t\n\t\tunshift @msgs, 'a!:';\n\t\tpush @msgs, 'z!:';\n\t\t\n\t\tfor (@msgs){\n\t\t\tmy( $name, $text ) = split ':';\n\t\t\tmy @found_names = grep { $names{ $_ } } /\\w+/g;\n\t\t\t\n\t\t\t$debug and print \"${name}[@found_names]\";\n\t\t\t\n\t\t\tmy %cand_names;\n\t\t\t\n\t\t\tif( $name eq '?' ){\n\t\t\t\t%cand_names = %names;\n\t\t\t\tdelete $cand_names{ $_ } for @found_names;\n\t\t\t}\n\t\t\t\t\t\t\n\t\t\t$_ = {\n\t\t\t\tname => $name,\n\t\t\t\ttext => $text,\n\t\t\t\tcand => { %cand_names },\n\t\t\t\t};\n\t\t\t}\n\t\t\n\t\t$debug and do {\n\t\t\tfor (@msgs){ \n\t\t\t\tprint \"[$_->{name}]\",\n\t\t\t\t\t\"[$_->{text}]\",\n\t\t\t\t\t\"[\", (join ' ', keys %{$_->{cand}} ), \"]\"\n\t\t\t\t}\n\t\t\t};\n\t\t\n\t\tmy $FAIL = 0;\n\t\tmy $del = 1;\n\t\t\n\t\twhile( $del ){\n\t\t\t$del = 0;\n\t\t\tfor my $i (1 .. @msgs - 2){\n\t\t\t\tnext if $msgs[$i]->{name} ne '?';\n\t\t\t\t\n\t\t\t\t$debug and print map \"<<$_>>\", join ' ', keys %{ $msgs[$i]->{cand} };\n\t\t\t\t\n\t\t\t\tdelete $msgs[$i]->{cand}->{ $msgs[$i-1]->{name} } and $del ++;\n\t\t\t\tdelete $msgs[$i]->{cand}->{ $msgs[$i+1]->{name} } and $del ++;\n\t\t\t\t\n\t\t\t\t$debug and print \"del:$del\";\n\t\t\t\t\n\t\t\t\tmy $num = keys %{ $msgs[$i]->{cand} };\n\t\t\t\tif( $num == 1 ){\n\t\t\t\t\t( $msgs[$i]->{name} ) = keys %{ $msgs[$i]->{cand} };\n\t\t\t\t\t}\n\t\t\t\telsif( $num == 0 ){\n\t\t\t\t\t$FAIL = 1;\n\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t$debug and do {\n\t\t\tfor (@msgs){ \n\t\t\t\tprint \"[$_->{name}]\",\n\t\t\t\t\t\"[$_->{text}]\",\n\t\t\t\t\t\"[\", (join ' ', keys %{$_->{cand}} ), \"]\"\n\t\t\t\t}\n\t\t\t};\n\t\t\n\t\tshift @msgs;\n\t\tpop @msgs;\n\t\t\n\t\tprint $FAIL ? \"Impossible\" : join \"\\n\", map { \"$_->{name}:$_->{text}\" } @msgs;\n\t\t\n\t\t}\n\t\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tfor (1 .. $_){\n\t\t$debug and print '-' x 10;\n\t\t\n\t\tmy $n = <>;\n\t\tmy %names = map { $_, 1 } split ' ', <>;\n\t\tmy $m = <>;\n\t\tmy @msgs = map ~~<>, 1 .. $m;\n\t\t\n\t\tchomp @msgs;\n\t\t\n\t\tunshift @msgs, 'a!:';\n\t\tpush @msgs, 'z!:';\n\t\t\n\t\tfor (@msgs){\n\t\t\tmy( $name, $text ) = split ':';\n\t\t\tmy @found_names = grep { $names{ $_ } } /\\w+/g;\n\t\t\t\n\t\t\t$debug and print \"${name}[@found_names]\";\n\t\t\t\n\t\t\tmy %cand_names;\n\t\t\t\n\t\t\tif( $name eq '?' ){\n\t\t\t\t%cand_names = %names;\n\t\t\t\tdelete $cand_names{ $_ } for @found_names;\n\t\t\t}\n\t\t\t\t\t\t\n\t\t\t$_ = {\n\t\t\t\tname => $name,\n\t\t\t\ttext => $text,\n\t\t\t\tcand => { %cand_names },\n\t\t\t\t};\n\t\t\t}\n\t\t\n\t\t$debug and do {\n\t\t\tfor (@msgs){ \n\t\t\t\tprint \"[$_->{name}]\",\n\t\t\t\t\t\"[$_->{text}]\",\n\t\t\t\t\t\"[\", (join ' ', keys %{$_->{cand}} ), \"]\"\n\t\t\t\t}\n\t\t\t};\n\t\t\n\t\tmy $FAIL = 0;\n\t\tmy $del = 1;\n\t\t\n\t\twhile( $del ){\n\t\t\t$del = 0;\n\t\t\tfor my $i (1 .. @msgs - 2){\n\t\t\t\tnext if $msgs[$i]->{name} ne '?';\n\t\t\t\t\n\t\t\t\t$debug and print map \"<<$_>>\", join ' ', keys %{ $msgs[$i]->{cand} };\n\t\t\t\t\n\t\t\t\tdelete $msgs[$i]->{cand}->{ $msgs[$i-1]->{name} } and $del ++;\n\t\t\t\tdelete $msgs[$i]->{cand}->{ $msgs[$i+1]->{name} } and $del ++;\n\t\t\t\t\n\t\t\t\t$debug and print \"del:$del\";\n\t\t\t\t\n\t\t\t\tmy $num = keys %{ $msgs[$i]->{cand} };\n\t\t\t\tif( $num == 1 ){\n\t\t\t\t\t( $msgs[$i]->{name} ) = keys %{ $msgs[$i]->{cand} };\n\t\t\t\t\t}\n\t\t\t\telsif( $num == 0 ){\n\t\t\t\t\t$FAIL = 1;\n\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t$debug and do {\n\t\t\tfor (@msgs){ \n\t\t\t\tprint \"[$_->{name}]\",\n\t\t\t\t\t\"[$_->{text}]\",\n\t\t\t\t\t\"[\", (join ' ', keys %{$_->{cand}} ), \"]\"\n\t\t\t\t}\n\t\t\t};\n\t\t\n\t\tmy $idx = -2;\n\t\tmy $key;\n\t\t\n\t\tfor my $i (0 .. @msgs - 1){\n\t\t\tlast if $FAIL;\n\t\t\t\n\t\t\t$idx == $i - 1 and delete $msgs[$i]->{cand}->{$key};\n\t\t\t\n\t\t\t$msgs[$i]->{name} eq '?' and do {\n\t\t\t\t$key = ( keys %{ $msgs[$i]->{cand} } )[ 0 ];\n\t\t\t\t%{ $msgs[$i]->{cand} } = ();\n\t\t\t\t$msgs[$i]->{name} = $key;\n\t\t\t\t$idx = $i;\n\t\t\t\t};\n\t\t\t}\n\t\t\n\t\tshift @msgs;\n\t\tpop @msgs;\n\t\t\n\t\tprint $FAIL ? \"Impossible\" : join \"\\n\", map { \"$_->{name}:$_->{text}\" } @msgs;\n\t\t\n\t\t}\n\t\n\t}"}], "src_uid": "3ac91d8fc508ee7d1afcf22ea4b930e4"} {"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$$$) \u2014 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$$$) \u2014 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": "use strict;\nuse warnings;\n\nmy $t = <>;\nwhile($t--){\n my $n = <>;\n my $find = 0;\n my @arr = split(' ', <>);\n\t\n @arr = sort {$a <=> $b} @arr;\n\n for(my $i = 1; $i <= $#arr; $i++){\n\t if($arr[$i] - $arr[$i-1] > 1){\n $find = 1;\n last;\n\t }\n }\n if($find == 1){\n\t print \"NO\\n\";\n } else {\n\t print \"YES\\n\";\n }\n}"}], "negative_code": [], "src_uid": "ed449ba7c453a43e2ac5904dc0174530"} {"nl": {"description": "You have n distinct points on a plane, none of them lie on OY axis. Check that there is a point after removal of which the remaining points are located on one side of the OY axis.", "input_spec": "The first line contains a single positive integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009105). The following n lines contain coordinates of the points. The i-th of these lines contains two single integers xi and yi (|xi|,\u2009|yi|\u2009\u2264\u2009109, xi\u2009\u2260\u20090). No two points coincide.", "output_spec": "Print \"Yes\" if there is such a point, \"No\" \u2014 otherwise. You can print every letter in any case (upper or lower).", "sample_inputs": ["3\n1 1\n-1 -1\n2 -1", "4\n1 1\n2 2\n-1 1\n-2 2", "3\n1 2\n2 1\n4 60"], "sample_outputs": ["Yes", "No", "Yes"], "notes": "NoteIn the first example the second point can be removed.In the second example there is no suitable for the condition point.In the third example any point can be removed."}, "positive_code": [{"source_code": "<>;\n\n/^-/ ? $R ++ : $L ++ for <>;\n\nprint $R > 1 && $L > 1 ? \"No\" : \"Yes\""}, {"source_code": "<>; \n\n$/ = $\\; $_ = <>;\n\nprint /^-.*^-/ms && /^\\d.*^\\d/ms ? \"No\" : \"Yes\""}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\t\n\tmy $L = 0;\n\tmy $R = 0;\n\t\n\tfor( @_ ){\n\t\tmy( $x, $y ) = split;\n\t\t$x > 0 and $R ++;\n\t\t$x < 0 and $L ++;\n\t\t}\n\t\n\tif( $R <= 1 or $L <= 1 ){\n\t\tprint \"Yes\"\n\t\t}\n\telse{\n\t\tprint \"No\"\n\t\t}\n\t}"}], "negative_code": [{"source_code": "$/ = $\\; $_ = <>;\n\nprint /^-.*^-/ms && /^\\d.*^\\d/ms ? \"No\" : \"Yes\""}], "src_uid": "cf7bf89a6038586b69d3b8021cee0b27"} {"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$$$) \u2014 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$$$ \u2014 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": "for (1..<>) {\r\n chomp (my $k = <>);\r\n $i = 1, $x = 1;\r\n while ($i < $k) {\r\n $x++;\r\n $i++;\r\n $x%10 == 3 and $x++;\r\n $x%3 == 0 and $x++;\r\n $x%10 == 3 and $x++;\r\n $x%3 == 0 and $x++;\r\n }\r\n print $x, \"\\n\";\r\n}"}, {"source_code": "#!/usr/bin/perl\r\nmy ($t) = map { $_ - 0 } split(/\\s+/o,);\r\nforeach my $tt ( 1 .. $t ){\r\n my ($k) = map { $_ - 0 } split(/\\s+/o,);\r\n my $c = 0;\r\n my $i = 1;\r\n while(1){\r\n unless( $i % 3 == 0 or $i % 10 == 3 ){\r\n $c++;\r\n last if $c == $k;\r\n }\r\n $i++;\r\n }\r\n print \"$i\\n\";\r\n}\r\n"}], "negative_code": [], "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 \u2014 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\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009|s|) of string s\u2009=\u2009s1s2... s|s| (where |s| is the length of string s) is string slsl\u2009+\u20091... sr.String x\u2009=\u2009x1x2... xp is lexicographically smaller than string y\u2009=\u2009y1y2... yq, if either p\u2009<\u2009q and x1\u2009=\u2009y1,\u2009x2\u2009=\u2009y2,\u2009... ,\u2009xp\u2009=\u2009yp, or there exists such number r (r\u2009<\u2009p,\u2009r\u2009<\u2009q), that x1\u2009=\u2009y1,\u2009x2\u2009=\u2009y2,\u2009... ,\u2009xr\u2009=\u2009yr and xr\u2009+\u20091\u2009<\u2009yr\u2009+\u20091. The string characters are compared by their ASCII codes.", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u200930) \u2014 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 \u2014 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": "#!/usr/bin/perl -w\nuse strict;\nuse warnings;\n\nsub issubstr {\n my ($s,$a) = @_;\n for (0 .. @$a-1) {\n\treturn 1 if index($a->[$_],$s) != -1;\n }\n return 0;\n}\n\n\nmy $n = int <>;\nmy @a;\nfor (1 .. $n) {\n my $s = <>;\n chomp $s;\n push @a, $s;\n}\nmy $s = 'a';\nwhile (issubstr($s,\\@a)) {\n $s++;\n}\nprint $s;\n"}, {"source_code": "<>;\n$_ = join\"\",<>;\n\nfor $a(a..z,aa..zz,aaa..zzz){\n # print \"$a\\n\";\n if (!/$a/){\n print $a;\n exit\n }\n }"}, {"source_code": "my $num = <>;\nchomp $num;\nmy @title;\nfor (1..$num) {\n my $title = <>;\n chomp $title;\n push @title, $title;\n}\n\nmy $title = '';\nmy $flag = 0;\nwhile (!$flag) {\n for ('a'..'z') {\n my $new = $title.$_;\n if (!grep /$new/, @title) {\n $title = $new;\n $flag = 1;\n last;\n }\n }\n if (!$flag) {\n if ($title =~ /^z*$/) {\n $title = 'a' x (1 + length $title);\n } else {\n $title =~ /(.*?)([^z]z*)$/;\n my $temp = $2;\n $temp =~ y/a-z/b-za/;\n $title = $1.$temp;\n }\n } \n \n}\nprint $title;"}], "negative_code": [{"source_code": "my $num = <>;\nchomp $num;\nmy @title;\nfor (1..$num) {\n my $title = <>;\n chomp $title;\n push @title, $title;\n}\n\nmy $title = '';\nmy $flag = 0;\nwhile (!$flag) {\n for ('a'..'z') {\n my $new = $title.$_;\n if (!grep /$new/, @title) {\n $title = $new;\n $flag = 1;\n last;\n }\n }\n $title .= 'a' if !$flag;\n}\nprint $title;"}], "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$$$)\u00a0\u2014 the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard\u00a0\u2014 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": "use POSIX;\r\nmy $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\n\r\n#$line = ;\r\n#chomp($line);\r\n#@input2 = split(' ', $line);\r\n\r\nmy $line2;\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n @input2 = split('', $line);\r\n $line2 = ;\r\n chomp($line2);\r\n my $prev = index($line, substr($line2, 0, 1));\r\n $s = 0;\r\n foreach $c (split('', $line2)) {\r\n $s += abs($prev - index($line, $c));\r\n $prev = index($line, $c);\r\n }\r\n print($s,\"\\n\");\r\n}\r\nexit;"}], "negative_code": [{"source_code": "use POSIX;\r\nmy $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\n\r\n#$line = ;\r\n#chomp($line);\r\n#@input2 = split(' ', $line);\r\n\r\nmy $line2;\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n @input2 = split('', $line);\r\n $line2 = ;\r\n chomp($line2);\r\n my $prev = index($line, substr($line2, 0, 1));\r\n $s = 0;\r\n foreach $c (split('', $line2)) {\r\n $s += abs($prev - index($line, $c));\r\n $prev = index($line, $c);\r\n }\r\n print $s;\r\n}\r\nexit;"}], "src_uid": "7f9853be7ac857bb3c4eb17e554ad3f1"} {"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 \u2014 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 \u2014 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": "$_=<>;\nprint \"<$3$2>\\n\"while /(\"(.*?)\"|(\\S+))/g"}, {"source_code": "$_=<>;\nchomp;\ns/\"(.*?)\"/#<$1>#/g;\n#print \"$_\\n\";; \n@a=split '#';\nfor(@a){\n#print \"|$_|\\n\";\n unless(/^\\n\" for split ' ';\n } else {\n print \"$_\\n\";\n }\n}"}, {"source_code": "$_=<>;\nprint \"<$1$2>\\n\"while /(?:\"(.*?)\"|(\\S+))/g"}, {"source_code": "$_=<>;print\"<$3$2>\\n\"while/(\"(.*?)\"|(\\S+))/g"}, {"source_code": "use strict;\nmy @text = split '\"', <>;\nfor (my $i = 0; $i < scalar @text; $i++) {\n\tif ($i % 2 == 0) {\n\t\tfor (split /\\s+/, $text[$i]) {\n\t\t\tif ($_ ne \"\") {\n\t\t\t\tprint \"<\" . $_ . \">\\n\";\n\t\t\t}\n\t\t}\n\t} else {\n\t\tprint \"<\" . $text[$i] . \">\\n\";\n\t}\n}\n"}, {"source_code": "$_=<>;\nchomp;\nwhile (/\"(.*?)\"([ ]|$)|([^\\s]+)([ ]|$)/mg) {\ndefined($1)? print \"<$1>\\n\" :print (\"<$3>\\n\");\n}"}, {"source_code": "$_=<>;print\"<$3$2>\\n\"while/(\"(.*?)\"|(\\S+))/g\n"}, {"source_code": "$_=<>;print\"<$3$2>\\n\"while/(\"(.*?)\"|(\\S+))/g\n"}, {"source_code": "#!/usr/bin/perl\nmy $line = <>;\nchomp($line);\nforeach ($line =~ /([^\" ]*|\"[^\"]*\")/g) {\n if ($_ ne \"\") {\n s/\"//g;\n print \"<$_>\\n\";\n }\n}"}, {"source_code": "#!/usr/bin/perl\n$text = <>;\n$text =~ s/\\s*(\"[^\"]*\"|[^\\s]+)\\s*/<$1>\\n/g;\n$text =~ s/\"//g;\nprint $text;"}, {"source_code": "$_=<>;print\"<$3$2>\\n\"while/(\"(.*?)\"|(\\S+))/g"}, {"source_code": "$_=<>;print\"<$3$2>\\n\"while/(\"(.*?)\"|(\\S+))/g\n"}, {"source_code": "$_=<>;print\"<$3$2>\\n\"while/(\"(.*?)\"|(\\S+))/g\n"}, {"source_code": "use List::MoreUtils qw/ uniq /;\n$text = \"\";\nwhile (<>) {\n $text .= $_;\n}\nwhile ($text =~ /\"([^\"]*)\"|([a-zA-Z0-9\\.?!,]+)/g) {\n print \"<\".$1.$2.\">\\n\"\n}"}, {"source_code": "$_=<>;print\"<$1$2>\\n\"while/(?:\"(.*?)\"|(\\S+))/g"}, {"source_code": "#!/usr/bin/perl\nuse warnings;\nuse utf8;\nuse strict;\n\nchomp (my $s = );\n\n\n#while ($s =~ /\\A\\s*(\"?)([A-Za-z0-9.,\\?! ]*?)\\1(\\s+|\\z)/) {\n#\tprint \"<$2>\\n\";\n#\t$s = $';\n#}\n\nwhile ($s =~ /\\A\\s* (?: \" ([A-Za-z0-9.,\\?!\\s]*) \" \\s* | ([A-Za-z0-9.,\\?!]+) (?:\\s+|\\z) )/x) {\n\tif (defined $1) {\n\t\tprint \"<$1>\\n\";\n\t}\n\tif (defined $2) {\n\t\tprint \"<$2>\\n\";\n\t}\n\t$s = $';\n}\n"}, {"source_code": "$_=<>;print\"<$3$2>\\n\"while/(\"(.*?)\"|(\\S+))/g\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nmy $cmd = ;\nmy @lexems = split( /([\"]{1}[A-Za-z0-9.,?! ]*[\"]{1})/, $cmd );\nforeach ( @lexems ){\n $_ =~ s/^\\s+//;\n $_ =~ s/\\s+$//;\n if ( $_ ne '' ) {\n\tif ( $_ =~ m/^[\"]{1}[A-Za-z0-9.,?! ]*[\"]{1}$/ ){\n\t $_ =~ s/([\"]{1})([A-Za-z0-9.,?! ]*)([\"]{1})/\\<$2\\>/;\n\t print $_.\"\\n\";\n\t}\n\telse {\n\t my @unquoted_lexems = split( /([ ]+)/, $_ );\n\t foreach ( @unquoted_lexems ){\n\t\t $_ =~ s/^\\s+//;\n\t\t $_ =~ s/\\s+$//;\n\t\t if ( $_ ne '' ) {\n\t\t\t\tprint '<'.$_.'>'.\"\\n\";\n\t\t\t}\n\t }\n\t}\n }\n}"}, {"source_code": "$_=<>;print\"<$3$2>\\n\"while/(\"(.*?)\"|(\\S+))/g\n"}, {"source_code": "$_=<>;print\"<$3$2>\\n\"while/(\"(.*?)\"|(\\S+))/g\n"}, {"source_code": "#!/usr/bin/perl\n$_=<>;\ns/\\s*(\"[^\"]*\"|[^\\s]+)\\s*/<$1>\\n/g;\ns/\"//g;\nprint $_;\n"}, {"source_code": "while (<>){\n chomp;\n \n @_=split//;\n $i=$j=0;\n for (@_){\n if ($_ eq '\"'){\n $i++;\n #$j++;\n print $i%2?\"<\":\">\\n\";\n next\n }\n if ($i%2){\n print;\n next\n }\n else{\n if ($_ ne \" \"){\n $j or print \"<\";\n $j++;\n print;\n \n }\n else {\n $j and print \">\\n\";\n $j=0;\n }\n \n }\n \n }\n $j and print \">\\n\";\n \n }"}, {"source_code": "$_=<>;print\"<$3$2>\\n\"while/(\"(.*?)\"|(\\S+))/g\n"}], "negative_code": [{"source_code": "$_=<>;\nchomp;\nwhile (/\"(.*?)\"|([^\\s]+)\\s/g) {\ndefined($1)? print \"<$1>\\n\" :print (\"<$2>\\n\");\n}"}, {"source_code": "$_=<>;\nchomp;\nwhile (/\"(.*?)\"|([^\\s]+)[ ]/g) {\ndefined($1)? print \"<$1>\\n\" :print (\"<$2>\\n\");\n}"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nmy $cmd = ;\nmy @lexems = split( /([\"]{1}[A-Za-z0-9.,?! ]*[\"]{1})/, $cmd );\nforeach ( @lexems ){\n $_ =~ s/^\\s+//;\n $_ =~ s/\\s+$//;\n if ( $_ ) {\n\tif ( $_ =~ m/^[\"]{1}[A-Za-z0-9.,?! ]*[\"]{1}$/ ){\n\t $_ =~ s/([\"]{1})([A-Za-z0-9.,?! ]*)([\"]{1})/\\<$2\\>/;\n\t print $_.\"\\n\";\n\t}\n\telse {\n\t my @unquoted_lexems = split( /([ ]+)/, $_ );\n\t foreach ( @unquoted_lexems ){\n\t\t $_ =~ s/^\\s+//;\n\t\t $_ =~ s/\\s+$//;\n\t\t if ( $_ ) {\n\t\t\t\tprint '<'.$_.'>'.\"\\n\";\n\t\t\t}\n\t }\n\t}\n }\n}\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 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 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 \u2014 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": "$n = <>; chomp $n;\nwhile($n--){\n $_=<>;chomp;\n $h{$_} = 1;\n}\nprint (scalar keys %h);\n"}], "negative_code": [], "src_uid": "07c370b99fe85984f5e20826a3bf5eb9"} {"nl": {"description": "There are many sunflowers in the Garden of the Sun.Garden of the Sun is a rectangular table with $$$n$$$ rows and $$$m$$$ columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals: When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected. There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells. You can walk from an empty cell to another if they share a common edge.Could you please give the owner a solution that meets all her requirements?Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that 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^4$$$)\u00a0\u2014 the number of test cases. The description of the test cases follows. The first line contains two integers $$$n$$$, $$$m$$$ ($$$1 \\le n,m \\le 500$$$)\u00a0\u2014 the number of rows and columns. Each of the next $$$n$$$ lines contains $$$m$$$ characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively. It is guaranteed that the sum of $$$n \\cdot m$$$ for all test cases does not exceed $$$250\\,000$$$.", "output_spec": "For each test case, print $$$n$$$ lines. Each should contain $$$m$$$ characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively. If there are multiple answers, you can print any. It can be shown that the answer always exists.", "sample_inputs": ["5\n3 3\nX.X\n...\nX.X\n4 4\n....\n.X.X\n....\n.X.X\n5 5\n.X...\n....X\n.X...\n.....\nX.X.X\n1 10\n....X.X.X.\n2 2\n..\n.."], "sample_outputs": ["XXX\n..X\nXXX\nXXXX\n.X.X\n.X..\n.XXX\n.X...\n.XXXX\n.X...\n.X...\nXXXXX\nXXXXXXXXXX\n..\n.."], "notes": "NoteLet's use $$$(x,y)$$$ to describe the cell on $$$x$$$-th row and $$$y$$$-th column.In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.In the first test case, one possible solution is to remove sunflowers on $$$(1,2)$$$, $$$(2,3)$$$ and $$$(3 ,2)$$$. Another acceptable solution is to remove sunflowers on $$$(1,2)$$$, $$$(2,2)$$$ and $$$(3,2)$$$. This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between $$$(1,1)$$$ and $$$(3,3)$$$. $$$(1,1)\\to (1,2)\\to (1,3)\\to (2,3)\\to (3,3)$$$ $$$(1,1)\\to (2,1)\\to (3,1)\\to (3,2)\\to (3,3)$$$ This output is considered wrong because you can't walk from $$$(1,1)$$$ to $$$(3,3)$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map ~~<>, 1 .. $n;\n\t\n\tchomp @_;\n\t\n\t$n % 3 == 1 and unshift @_, '.' x $m;\n\t\n\tfor( my $i = 1; $i <= $n; $i += 3 ){\n\t\t$_[ $i ] =~ y/./X/;\n\t\t}\n\t\n\t$_ = join \"\\n\", @_;\n\t\n\tmy $insert = sub { s/^\\.*\\bX\\b([^Y]{$m})\\K\\./Y/gms };\n\t\n\t$insert->();\n\t$_ = reverse;\n\t$insert->();\n\t$_ = reverse;\n\t\n\ty/Y/X/;\n\t\n\ts/^\\.(?=\\.*$)/X/gms;\n\t\n\t$n % 3 == 1 and s/^.+\\n//;\n\t\n\tprint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\t@_ = ();\n\t\n\tfor( 1 .. $n ){\n\t\tpush @_, ~~<>;\n\t\t}\n\t\n\tchomp @_;\n\t\n\tif( $n == 1 ){\n\t\tprint 'X' x $m;\n\t\tnext;\n\t\t}\n\t\n\tif( $n % 3 == 1 ){\n\t\tunshift @_, '.' x $m;\n\t\t}\n\t\n\tmy $i = 0;\n\tfor( @_ ){\n\t\tif( $i % 3 == 1 ){\n\t\t\ts/\\./X/g;\n\t\t\t}\n\t\t$i ++;\n\t\t}\n\t\n\t$debug and print for @_;\n\t\n\tfor my $i ( 0 .. $n - 1 ){\n\t\tnext if $i % 3 != 2;\n\t\tnext if $i + 1 > $n - 1;\n\t\t\n\t\tmy $idx_X1;\n\t\tmy $idx_X2;\n\t\t\n\t\t$idx_X1 = index $_[ $i ], 'X';\n\t\t$idx_X2 = index $_[ $i + 1 ], 'X';\n\t\t\n\t\t$debug and print \"[$idx_X1][$idx_X2]\";\n\t\t\n\t\tif( 0 ){ ; }\n\t\telsif( $idx_X1 == -1 and $idx_X2 == -1 ){\n\t\t\t$_[ $i ] =~ s/\\./X/;\n\t\t\t$_[ $i + 1 ] =~ s/\\./X/;\n\t\t\t}\n\t\telsif( $idx_X1 >= 0 ){\n\t\t\tsubstr $_[ $i + 1 ], $idx_X1, 1, 'X';\n\t\t\t}\n\t\telse{\n\t\t\tsubstr $_[ $i ], $idx_X2, 1, 'X';\n\t\t\t}\n\t\t}\n\t\n\t$debug and print '-' x 3;\n\t\n\tif( $n % 3 == 1 ){\n\t\tshift @_;\n\t\t}\n\t\n\tprint join \"\\n\", @_;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map ~~<>, 1 .. $n;\n\t\n\tchomp @_;\n\t\n\t$n % 3 == 1 and unshift @_, '.' x $m;\n\t\n\tfor( my $i = 1; $i <= $n; $i += 3 ){\n\t\t$_[ $i ] =~ y/./X/;\n\t\t}\n\t\n\t$_ = join \"\\n\", @_;\n\t\n\tmy $insert = sub { s/^[^Y]*\\bX\\b([^Y]{$m})\\K\\./Y/gms };\n\t\n\t$insert->();\n\t$_ = reverse;\n\t$insert->();\n\t$_ = reverse;\n\t\n\ty/Y/X/;\n\t\n\ts/^\\.(?=\\.*$)/X/gms;\n\t\n\t$n % 3 == 1 and s/^.+\\n//;\n\t\n\tprint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map ~~<>, 1 .. $n;\n\t\n\tchomp @_;\n\t\n\t$n % 3 == 1 and unshift @_, '.' x $m;\n\t\n\tfor( my $i = 1; $i <= $n; $i += 3 ){\n\t\t$_[ $i ] =~ y/./X/;\n\t\t}\n\t\n\t$_ = join \"\\n\", @_;\n\t\n\tmy $insert = sub { s/\\bX\\b([^Y]{$m})\\K\\./Y/gms };\n\t\n\t$insert->();\n\t$_ = reverse;\n\t$insert->();\n\t$_ = reverse;\n\t\n\ty/Y/X/;\n\t\n\ts/^\\.(?=\\.*$)/X/gms;\n\t\n\t$n % 3 == 1 and s/^.+\\n//;\n\t\n\tprint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map ~~<>, 1 .. $n;\n\t\n\tchomp @_;\n\t\n\t$n % 3 == 1 and unshift @_, '.' x $m;\n\t\n\tfor( my $i = 1; $i <= $n; $i += 3 ){\n\t\t$_[ $i ] =~ y/./X/;\n\t\t}\n\t\n\t$_ = join \"\\n\", @_;\n\t\n\tmy $insert = sub { s/\\bX\\b(.{$m})\\K\\./X/gms };\n\t\n\t$insert->() or do {\n\t\t$_ = reverse;\n\t\t$insert->();\n\t\t$_ = reverse;\n\t\t};\n\t\n\ts/^\\.(?=\\.*$)/X/gms;\n\t\n\t$n % 3 == 1 and s/^.+\\n//;\n\t\n\tprint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map ~~<>, 1 .. $n;\n\t\n\tchomp @_;\n\t\n\t$n % 3 == 1 and unshift @_, '.' x $m;\n\t\n\tfor( my $i = 1; $i <= $n; $i += 3 ){\n\t\t$_[ $i ] =~ y/./X/;\n\t\t}\n\t\n\t$_ = join \"\\n\", @_;\n\t\n\tmy $insert = sub { s/\\bX\\b(.{$m})\\K./X/gms };\n\t\n\t$insert->();\n\t$_ = reverse;\n\t\n\t$insert->();\n\t$_ = reverse;\n\t\n\ts/^\\.(?=\\.*$)/X/gms;\n\t\n\t$n % 3 == 1 and s/^.+\\n//;\n\t\n\tprint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map ~~<>, 1 .. $n;\n\t\n\tchomp @_;\n\t\n\t$n % 3 == 1 and unshift @_, '.' x $m;\n\t\n\tfor( my $i = 1; $i <= $n; $i += 3 ){\n\t\t$_[ $i ] =~ y/./X/;\n\t\t}\n\t\n\t$_ = join \"\\n\", @_;\n\t\n\tmy $insert = sub { s/^X+\\n\\.*X(.{$m})\\K./X/gms };\n\t\n\t$insert->();\n\t$_ = reverse;\n\t\n\t$insert->();\n\t$_ = reverse;\n\t\n\ts/^\\.(?=\\.*$)/X/gms;\n\t\n\t$n % 3 == 1 and s/^.+\\n//;\n\t\n\tprint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map ~~<>, 1 .. $n;\n\t\n\tchomp @_;\n\t\n\t$n % 3 == 1 and unshift @_, '.' x $m;\n\t\n\tfor( my $i = 1; $i <= $n; $i += 3 ){\n\t\t$_[ $i ] =~ y/./X/;\n\t\t}\n\t\n\t$_ = join \"\\n\", @_;\n\t\n\tmy $insert = sub { s/^X+\\n\\.*X(.{$m})\\K./X/gms };\n\t\n\t$insert->();\n\t$_ = reverse;\n\t\n\t$insert->();\n\t$_ = reverse;\n\t\n\ts/^\\.(?=\\.+$)/X/gms;\n\t\n\t$n % 3 == 1 and s/^.+\\n//;\n\t\n\tprint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\t@_ = ();\n\t\n\tfor( 1 .. $n ){\n\t\tpush @_, ~~<>;\n\t\t}\n\t\n\tchomp @_;\n\t\n\tif( $n == 1 ){\n\t\tprint 'X' x $m;\n\t\tnext;\n\t\t}\n\t\n\tmy $i = 0;\n\tfor( @_ ){\n\t\tif( $i % 3 == 1 ){\n\t\t\ts/\\./X/g;\n\t\t\t}\n\t\t$i ++;\n\t\t}\n\t\n\t$debug and print for @_;\n\t\n\tfor my $i ( 0 .. $n - 1 ){\n\t\tnext if $i % 3 != 2;\n\t\tnext if $i + 1 >= $n - 1;\n\t\t\n\t\tmy $idx_X1;\n\t\tmy $idx_X2;\n\t\t\n\t\t$idx_X1 = index $_[ $i ], 'X';\n\t\t$idx_X2 = index $_[ $i + 1 ], 'X';\n\t\t\n\t\t$debug and print \"[$idx_X1][$idx_X2]\";\n\t\t\n\t\tif( 0 ){ ; }\n\t\telsif( $idx_X1 == -1 and $idx_X2 == -1 ){\n\t\t\t$_[ $i ] =~ s/\\./X/;\n\t\t\t$_[ $i + 1 ] =~ s/\\./X/;\n\t\t\t}\n\t\telsif( $idx_X1 >= 0 ){\n\t\t\tsubstr $_[ $i + 1 ], $idx_X1, 1, 'X';\n\t\t\t}\n\t\telse{\n\t\t\tsubstr $_[ $i ], $idx_X2, 1, 'X';\n\t\t\t}\n\t\t}\n\t\n\t$debug and print '-' x 3;\n\t\n\t$debug and print for @_;\n\t\n\tprint join \"\\n\", @_;\n\t}"}], "src_uid": "669390a40a6ef9bf1547c71823da169b"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "<>;for(<>){($c,$d,$f)=split;($c,$d)=sort{$a<=>$b}($c, $d);print$c*++$f>=$d?\"YES$/\":\"NO$/\"}"}, {"source_code": "<>;\r\n\r\nfor (<>) {\r\n chomp;\r\n ($c, $d, $f) = split;\r\n ($c, $d) = sort{$a <=> $b}($c, $d);\r\n print $c * ($f+1) >= $d ? \"YES$/\" : \"NO$/\";\r\n}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($r,$b,$d) = map { $_ - 0 } split(/\\s+/,);\r\n if( $d == 0 ){\r\n print ( $r == $b ? \"YES\\n\" : \"NO\\n\" ); next;\r\n }\r\n ($r,$b) = ($b,$r) if $r < $b;\r\n my $di = $r - $b;\r\n my $dv = ($di+($d-1))/$d;\r\n if( $b < $dv ){\r\n print \"NO\\n\";\r\n } else {\r\n print \"YES\\n\";\r\n }\r\n \r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "src_uid": "c0ad2a6d14b0c9e09af2221d88a34d52"} {"nl": {"description": "There are two types of burgers in your restaurant \u2014 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$$$) \u2013 the number of queries. The first line of each query contains three integers $$$b$$$, $$$p$$$ and $$$f$$$ ($$$1 \\le b, ~p, ~f \\le 100$$$) \u2014 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$$$) \u2014 the hamburger and chicken burger prices in your restaurant.", "output_spec": "For each query print one integer \u2014 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": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n for(my $i=0; $i<$q; $i++) {\n my $i= <$in>; chomp($i);\n my ($b, $p, $f)= split(' ', $i);\n\n $i= <$in>; chomp($i);\n my ($h, $c)= split(' ', $i);\n\n my $profit= 0;\n if($h > $c) { # make hamburgers as much as u can\n while($b>=2 && $p>0) {\n $profit += $h;\n $b-= 2;\n $p--;\n }\n\n while($b>=2 && $f>0) {\n $profit += $c;\n $b-= 2;\n $f--;\n }\n } else {\n while($b>=2 && $f>0) {\n $profit += $c;\n $b-= 2;\n $f--;\n }\n\n while($b>=2 && $p>0) {\n $profit += $h;\n $b-= 2;\n $p--;\n }\n }\n\n print $profit;\n print $/;\n }\n\n}\n\nmain() unless caller();\n\n"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nchomp (my $line = );\nmy $t = $line;\n\nfor (1..$t) {\n $line = ;\n my ($b, $p, $f) = split q{ }, $line;\n $b = $b - 1 if $b % 2 == 1;\n $line = ;\n my ($h, $c) = split q{ }, $line;\n\n my $price = 0;\n\n if ( $h > $c ) {\n $price += $h * make(\\$b, \\$p);\n $price += $c * make(\\$b, \\$f);\n }\n else {\n $price += $c * make(\\$b, \\$f);\n $price += $h * make(\\$b, \\$p);\n }\n say $price;\n}\n\nsub make {\n my ($b, $m) = @_;\n my $count = 0;\n if ( 2*$$m > $$b ) {\n $count = $$b/2;\n $$b = 0;\n $$m -= $count;\n }\n else {\n $count = $$m;\n $$m = 0;\n $$b -= 2 * $count;\n }\n return $count;\n}\n"}, {"source_code": "$\\ = $/;\n\n<>;\n\nwhile(<>){\n ( $b, $p, $f, $h, $c, $s ) = split ' ', $_ . <> . 0;\n \n $b >>= 1;\n \n $_ = 'p' x $p . 'f' x $f;\n \n $_ = reverse if $h < $c;\n \n /(?(?{ $b == pos })(*ACCEPT)) . (?{ $s += $& eq 'p' ? $h : $c }) (*F)/x;\n \n print $s;\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print \"-\" x 15;\n\t\n\tmy( $b, $p, $f ) = split;\n\tmy( $h, $c ) = split ' ', <>;\n\t\n\t$b /= 2;\n\t\n\tmy $middle = 'p' x $p . 'f' x $f;\n\t\n\tif( $h < $c ){\n\t\t$middle = reverse $middle;\n\t\t}\n\t\n\tmy $L = substr $middle, 0, $b;\n\t\n\t$debug and print $L;\n\t\n\tmy $cp = () = $L =~ /p/g;\n\tmy $cf = () = $L =~ /f/g;\n\t\n\t$debug and print \"$cp $cf\";\n\t\n\tprint $cp * $h + $cf * $c;\n\t}"}], "negative_code": [{"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n for(my $i=0; $i<$q; $i++) {\n my $i= <$in>; chomp($i);\n my ($b, $p, $f)= split(' ', $i);\n\n $i= <$in>; chomp($i);\n my ($h, $c)= split(' ', $i);\n\n my $profit= 0;\n if($h > $c) { # make hamburgers as much as u can\n while($b>=2 && $p>0) {\n $profit += $h;\n $b-= 2;\n $p--;\n }\n\n while($b>=2 && $f>0) {\n $profit += $c;\n $b-= 2;\n $f--;\n }\n } else {\n while($b>=2 && $f>0) {\n $profit += $c;\n $b-= 2;\n $f--;\n }\n\n while($b>=2 && $p>0) {\n $profit += $h;\n $b-= 2;\n $p--;\n }\n }\n\n print $profit;\n }\n\n}\n\nmain() unless caller();\n\n"}], "src_uid": "92bf30e66f4d5ddebb697d2fa4fa0689"} {"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": "chomp($t = );\r\nforeach (1..$t)\r\n{\r\n ;\r\n chomp($a = );\r\n $cnt = $a =~ tr/0//;\r\n if ($cnt == 1)\r\n {\r\n print \"BOB\\n\";\r\n }\r\n elsif ($cnt % 2)\r\n {\r\n print \"ALICE\\n\";\r\n }\r\n else\r\n {\r\n print \"BOB\\n\";\r\n }\r\n}"}], "negative_code": [], "src_uid": "42b425305ccc28b0d081b4c417fe77a1"} {"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$$$) \u2014 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$$$) \u2014 the array $$$a$$$.", "output_spec": "Print the single integer \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $max = ( sort { $b <=> $a } @_ )[ 0 ];\n\t\n\tmy $cnt = 0;\n\t\n\tmy $m = -1;\n\t\n\tfor( @_ ){\n\t\tif( $_ == $max ){\n\t\t\t$cnt ++;\n\t\t\t$cnt > $m and $m = $cnt;\n\t\t\t}\n\t\telse{\n\t\t\t$cnt = 0;\n\t\t\t}\n\t\t}\n\t\n\tprint $m;\n\t}"}], "negative_code": [], "src_uid": "5db2ae5b2c91b29e4fe4a45ab864e3f1"} {"nl": {"description": "You are given $$$n$$$ elements numbered from $$$1$$$ to $$$n$$$, the element $$$i$$$ has value $$$a_i$$$ and color $$$c_i$$$, initially, $$$c_i = 0$$$ for all $$$i$$$.The following operation can be applied: Select three elements $$$i$$$, $$$j$$$ and $$$k$$$ ($$$1 \\leq i < j < k \\leq n$$$), such that $$$c_i$$$, $$$c_j$$$ and $$$c_k$$$ are all equal to $$$0$$$ and $$$a_i = a_k$$$, then set $$$c_j = 1$$$. Find the maximum value of $$$\\sum\\limits_{i=1}^n{c_i}$$$ that can be obtained after applying the given operation any number of times.", "input_spec": "The first line contains an integer $$$n$$$ ($$$3 \\leq n \\leq 2 \\cdot 10^5$$$) \u2014 the number of elements. The second line consists of $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq n$$$), where $$$a_i$$$ is the value of the $$$i$$$-th element.", "output_spec": "Print a single integer in a line \u2014 the maximum value of $$$\\sum\\limits_{i=1}^n{c_i}$$$ that can be obtained after applying the given operation any number of times.", "sample_inputs": ["7\n1 2 1 2 7 4 7", "13\n1 2 3 2 1 3 3 4 5 5 5 4 7"], "sample_outputs": ["2", "7"], "notes": "NoteIn the first test, it is possible to apply the following operations in order: "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy $A = 'a' x 4;\n\t\n\tmy %h;\n\t\n\t$_ = join ' ', reverse \n\t\tmap { $h{ $_ } or $h{ $_ } = ++ $A and \"$A,\" } split ' ', <>;\n\t\n\tmy $ans = 0;\n\tmy $min;\n\tmy $new_min;\n\t\n\tm/\t\n\t\t\\b(\\w+)\\b(,)?+\n\t\t(?{\n\t\t\tif( not defined $min ){\n\t\t\t\t$min = $1;\n\t\t\t\t}\n\t\t\telsif( $min lt $1 or $min eq $1 and !$2 ){\n\t\t\t\t$ans ++;\n\t\t\t\t}\n\t\t\telsif( $min gt $1 ){\n\t\t\t\tif( not defined $new_min ){\n\t\t\t\t\t$new_min = $1;\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\t$new_min gt $1 and $new_min = $1;\n\t\t\t\t\t$ans ++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\n\t\t\tif( $min eq $1 and $2 ){\n\t\t\t\tif( defined $new_min ){\n\t\t\t\t\t$min = $new_min;\n\t\t\t\t\tundef $new_min;\n\t\t\t\t\t$ans ++;\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tundef $min;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t})\n\t\t(*FAIL)\n\t\t/x;\n\t\n\tprint $ans;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy @ABC;\nmy $abc = 'a';\n\nfor my $i ( 0 .. 2e5 ){\n\tpush @ABC, $abc ++;\n\t}\n\nwhile(<>){\n\t@_ = map $ABC[ $_ ], split ' ', <>;\n\t\n\t$_ = join ' ', @_;\n\t\n\tmy %B;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\t$B{ $_[ $i ] } = exists $B{ $_[ $i ] } ? $i : -1;\n\t\t}\n\t\n\tmy $ans = 0;\n\tmy $under = 0;\n\tmy $cover;\n\tmy $far_cover;\n\tmy $far_dist;\n\t\n\tmy $i = 0;\n\t\n\tm/\n\t\t\\b\\w+\\b\n\t\t(?{\n\t\t\t$i ++;\n\t\t\t\n\t\t\tif( $under ){\n\t\t\t\tif( $& eq $cover ){\n\t\t\t\t\tif( $B{ $& } + 1 != $i ){\n\t\t\t\t\t\t$ans ++;\n\t\t\t\t\t\t}\n\t\t\t\t\telsif( defined $far_dist and $far_dist >= $i ){\n\t\t\t\t\t\t$cover = $far_cover;\n\t\t\t\t\t\tundef $far_cover;\n\t\t\t\t\t\tundef $far_dist;\n\t\t\t\t\t\t}\n\t\t\t\t\telse{\n\t\t\t\t\t\t$under = 0;\n\t\t\t\t\t\tundef $cover;\n\t\t\t\t\t\tundef $far_dist;\n\t\t\t\t\t\tundef $far_cover;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\t$ans ++;\n\t\t\t\t\tif( $B{ $& } > -1 and not \n\t\t\t\t\t\t( defined $far_dist and $B{ $& } <= $far_dist ) ){\n\t\t\t\t\t\t$far_dist = $B{ $& };\n\t\t\t\t\t\t$far_cover = $&;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tif( $B{ $& } > -1 ){\n\t\t\t\t\t$cover = $&;\n\t\t\t\t\t$under = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t})\n\t\t(*FAIL)\n\t\t/x;\n\t\t\n\tprint $ans;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy @B = ( -2 ) x ( @_ + 1 );\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tif( $B[ $_[ $i ] ] == -2 ){\n\t\t\t$B[ $_[ $i ] ] = -1;\n\t\t\t}\n\t\telsif( $B[ $_[ $i ] ] > -2 ){\n\t\t\t$B[ $_[ $i ] ] = $i;\n\t\t\t}\n\t\t}\n\t\n\tmy $ans = 0;\n\tmy $under = 0;\n\tmy $cover;\n\tmy $far_cover;\n\tmy $far_dist;\n\t\n\tmy $i = 0;\n\t\n\twhile( @_ ){\n\t\t$_ = shift @_;\n\t\t$i ++;\n\t\t$debug and print \"$i:[$_]\";\n\t\t\n\t\tif( $under ){\n\t\t\tif( $_ == $cover ){\n\t\t\t\t$debug and print \" 1)\";\n\t\t\t\tif( $B[ $_ ] + 1 != $i ){\n\t\t\t\t\t$debug and print \" 1.1) $B[$_],$i\";\n\t\t\t\t\t$ans ++;\n\t\t\t\t\t}\n\t\t\t\telsif( defined $far_dist and $far_dist >= $i ){\n\t\t\t\t\t$cover = $far_cover;\n\t\t\t\t\tundef $far_cover;\n\t\t\t\t\tundef $far_dist;\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\t$under = 0;\n\t\t\t\t\tundef $cover;\n\t\t\t\t\tundef $far_dist;\n\t\t\t\t\tundef $far_cover;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$debug and print \" 2)\";\n\t\t\t\t$ans ++;\n\t\t\t\t$debug and print \" $cover,$_\";\n\t\t\t\tif( $B[ $_ ] > -1 ){\n\t\t\t\t\tif( defined $far_dist ) {\n\t\t\t\t\t\tif( $B[ $_ ] > $far_dist ){\n\t\t\t\t\t\t\t$far_dist = $B[ $_ ];\n\t\t\t\t\t\t\t$far_cover = $_;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\telse{\n\t\t\t\t\t\t$far_dist = $B[ $_ ];\n\t\t\t\t\t\t$far_cover = $_;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\telse{\n\t\t\t$debug and print \" 3)\";\n\t\t\tif( $B[ $_ ] > -1 ){\n\t\t\t\t$cover = $_;\n\t\t\t\t$under = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t}\n\t\n\tprint $ans;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy $A = 'a' x 4;\n\t\n\tmy %h;\n\t\n\t$_ = join ' ', reverse \n\t\tmap { $h{ $_ } or $h{ $_ } = ++ $A and \"$A,\" } split ' ', <>;\n\t\n\tmy $ans = 0;\n\tmy $q = 0;\n\tmy $min = 'z';\n\t\n\tm/\t\n\t\t\\b(\\w+)\\b(,)?+\n\t\t(?{\n\t\t\tif( defined $2 and $min ge $1 ){\n\t\t\t\t$min eq $1 and -- $ans;\n\t\t\t\t$q > 1 and -- $ans;\n\t\t\t\t$q = 0;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t++ $ans;\n\t\t\t\t$min gt $1 and $min = $1 and ++ $q;\n\t\t\t\t}\n\t\t\t})\n\t\t(*FAIL)\n\t\t/x;\n\t\n\tprint $ans;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy $A = 'a' x 4;\n\t\n\tmy %h;\n\t\n\t$_ = join ' ', reverse \n\t\tmap { $h{ $_ } or $h{ $_ } = ++ $A and \"$A,\" } split ' ', <>;\n\t\n\tmy $ans = 0;\n\tmy $min = 'z';\n\t\n\tm/\t\n\t\t\\b(\\w+)\\b(,)?+\n\t\t(?{\n\t\t\tif( $min lt $1 or $min eq $1 and !$2 ){\n\t\t\t\t$ans ++;\n\t\t\t\t}\n\t\t\t$1 lt $min and $min = $1;\n\t\t\t})\n\t\t(*FAIL)\n\t\t/x;\n\t\n\tprint $ans;\n\t}"}], "src_uid": "daf675dadc8edcd6734edbf3548eb6bf"} {"nl": {"description": "You are given $$$k$$$ sequences of integers. The length of the $$$i$$$-th sequence equals to $$$n_i$$$.You have to choose exactly two sequences $$$i$$$ and $$$j$$$ ($$$i \\ne j$$$) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence $$$i$$$ (its length will be equal to $$$n_i - 1$$$) equals to the sum of the changed sequence $$$j$$$ (its length will be equal to $$$n_j - 1$$$).Note that it's required to remove exactly one element in each of the two chosen sequences.Assume that the sum of the empty (of the length equals $$$0$$$) sequence is $$$0$$$.", "input_spec": "The first line contains an integer $$$k$$$ ($$$2 \\le k \\le 2 \\cdot 10^5$$$) \u2014 the number of sequences. Then $$$k$$$ pairs of lines follow, each pair containing a sequence. The first line in the $$$i$$$-th pair contains one integer $$$n_i$$$ ($$$1 \\le n_i < 2 \\cdot 10^5$$$) \u2014 the length of the $$$i$$$-th sequence. The second line of the $$$i$$$-th pair contains a sequence of $$$n_i$$$ integers $$$a_{i, 1}, a_{i, 2}, \\dots, a_{i, n_i}$$$. The elements of sequences are integer numbers from $$$-10^4$$$ to $$$10^4$$$. The sum of lengths of all given sequences don't exceed $$$2 \\cdot 10^5$$$, i.e. $$$n_1 + n_2 + \\dots + n_k \\le 2 \\cdot 10^5$$$.", "output_spec": "If it is impossible to choose two sequences such that they satisfy given conditions, print \"NO\" (without quotes). Otherwise in the first line print \"YES\" (without quotes), in the second line \u2014 two integers $$$i$$$, $$$x$$$ ($$$1 \\le i \\le k, 1 \\le x \\le n_i$$$), in the third line \u2014 two integers $$$j$$$, $$$y$$$ ($$$1 \\le j \\le k, 1 \\le y \\le n_j$$$). It means that the sum of the elements of the $$$i$$$-th sequence without the element with index $$$x$$$ equals to the sum of the elements of the $$$j$$$-th sequence without the element with index $$$y$$$. Two chosen sequences must be distinct, i.e. $$$i \\ne j$$$. You can print them in any order. If there are multiple possible answers, print any of them.", "sample_inputs": ["2\n5\n2 3 1 3 2\n6\n1 1 2 2 2 1", "3\n1\n5\n5\n1 1 1 1 1\n2\n2 3", "4\n6\n2 2 2 2 2 2\n5\n2 2 2 2 2\n3\n2 2 2\n5\n2 2 2 2 2"], "sample_outputs": ["YES\n2 6\n1 2", "NO", "YES\n2 2\n4 1"], "notes": "NoteIn the first example there are two sequences $$$[2, 3, 1, 3, 2]$$$ and $$$[1, 1, 2, 2, 2, 1]$$$. You can remove the second element from the first sequence to get $$$[2, 1, 3, 2]$$$ and you can remove the sixth element from the second sequence to get $$$[1, 1, 2, 2, 2]$$$. The sums of the both resulting sequences equal to $$$8$$$, i.e. the sums are equal."}, "positive_code": [{"source_code": "use warnings;\nuse strict;\n$\\ = qq/\\n/, $, = qq/ /;\nsub sum { \n my $result = 0;\n $result += $_ for @{ $_ [ 0 ] };\n $result;\n}\nmy $k = ;\nchomp ( $k );\nmy %h = ();\nfor my $i ( 1 .. $k ) {\n chomp ( my $n = );\n my @a = split qq/ /, ;\n my $s = sum ( \\@a ); \n my $index = 1;\n for my $j ( @a ) {\n if ( exists $h { $s - $j } and $h { $s - $j } -> [ 0 ] != $i ) {\n print \"YES\";\n print + $i, $index;\n print + $h { $s - $j } -> [ 0 ], $h { $s - $j } -> [ 1 ];\n exit;\n } else { $h { $s - $j } = [ $i, $index ] }\n $index += 1;\n }\n}\nprint \"NO\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy %h;\n\t\n\tfor my $i ( 1 .. $_ ){\n\t\t<>;\n\t\t@_ = split ' ', <>;\n\t\tmy $sum = 0;\n\t\t$sum += $_ for @_;\n\t\t\n\t\tfor my $j ( 0 .. @_ - 1 ){\n\t\t\t$h{ $sum - $_[ $j ] }{ $i } = $j + 1;\n\t\t\t}\n\t\t}\n\t\n\tmy $ok = 0;\n\t\n\tfor my $i ( keys %h ){\n\t\tmy @keys = keys %{ $h{ $i } };\n\t\tnext if @keys < 2;\n\t\tprint \"YES\";\n\t\tprint $keys[ 0 ], ' ', $h{ $i }{ $keys[ 0 ] };\n\t\tprint $keys[ 1 ], ' ', $h{ $i }{ $keys[ 1 ] };\n\t\t$ok = 1;\n\t\tlast;\n\t\t}\n\t\n\t$ok or print \"NO\";\n\t}"}], "negative_code": [{"source_code": "use warnings;\nuse strict;\n$\\ = qq/\\n/, $, = qq/ /;\nsub sum { \n my $result = 0;\n $result += $_ for @{ $_ [ 0 ] };\n $result;\n}\nmy $k = ;\nchomp ( $k );\nmy %h = ();\nfor my $i ( 1 .. $k ) {\n chomp ( my $n = );\n my @a = split qq/ /, ;\n my $s = sum ( \\@a ); \n my $index = 1;\n for my $j ( @a ) {\n if ( exists $h { $s - $j } and $h { $s - $j } -> [ 0 ] != $i ) {\n print \"YES\";\n print + $i, $j;\n print + $h { $s - $j } -> [ 0 ], $h { $s - $j } -> [ 1 ];\n exit;\n } else { $h { $s - $j } = [ $i, $index ] }\n $index += 1;\n }\n}\nprint \"NO\";\n"}], "src_uid": "7561bca5fa2200ce9ae7e30e7076c6ab"} {"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 \u2014 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,\u2009y). The table corners are cells: (1,\u20091), (n,\u20091), (1,\u2009m), (n,\u2009m).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,\u2009y1), an arbitrary corner of the table (x2,\u2009y2) and color all cells of the table (p,\u2009q), which meet both inequations: min(x1,\u2009x2)\u2009\u2264\u2009p\u2009\u2264\u2009max(x1,\u2009x2), min(y1,\u2009y2)\u2009\u2264\u2009q\u2009\u2264\u2009max(y1,\u2009y2).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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200950). Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1,\u2009ai2,\u2009...,\u2009aim. If aij equals zero, then cell (i,\u2009j) 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 \u2014 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,\u20092) and corner (1,\u20091). For the second time you need to choose cell (2,\u20092) and corner (3,\u20093). For the third time you need to choose cell (2,\u20092) and corner (3,\u20091). For the fourth time you need to choose cell (2,\u20092) and corner (1,\u20093). In the second sample the sequence of operations can be like this: For the first time you need to choose cell (3,\u20091) and corner (4,\u20093). For the second time you need to choose cell (2,\u20093) and corner (1,\u20091). "}, "positive_code": [{"source_code": "($n, $m) = split / /, <>;\nwhile (<>) {\n\tchomp;\n\tpush @a, [split / /];\n}\n# print_a(\\@a);\n$n--;\n$m--;\nfor $i (0..$n) {\n\tif ($a[$i][0] == 1 || $a[$i][$m] == 1) {\n\t\tprint \"2\\n\";\n\t\texit;\n\t}\n}\nfor $j (0..$m) {\n\tif ($a[0][$j] == 1 || $a[$n][$j] == 1) {\n\t\tprint \"2\\n\";\n\t\texit;\n\t}\n}\nprint \"4\\n\";\n\nsub print_a() {\n\t$a = shift;\n\tfor $i (0..$n-1) {\n\t\tfor $j (0..$m-1) {\n\t\t\tprint $a->[$i][$j];\n\t\t}\n\t\tprint \"\\n\";\n\t}\n}"}, {"source_code": "($n, $m) = split /\\s+/, <>; $i = 0;\nwhile ($row = <>) {\n\tif (($i++ == 0 || $i == $n) && $row =~ /1/) { print 2; exit; }\n\tif ($row =~ /^1.*|.*1$/) { print 2; exit; }\n}\nprint 4;\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n<>=~/1/ and $F++;\nwhile(<>){\n\t/^1|1$/ and $F++;\n\t $a=$_;\n\t}\n$a=~/1/ and $F++;\n\nprint $F?2:4;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [{"source_code": "<>;\nwhile ($row = <>) {\n\tif ($row =~ /^1.*|.*1$/) {\n\t\tprint 2; exit;\n\t}\n}\nprint 4;\n"}, {"source_code": "<>; $row = <>;\nif ($row =~ /.*1.*/) { print 2; exit; }\nwhile ($row = <>) {\n\tif ($row =~ /^1.*|.*1$/) { print 2; exit; }\n}\nif ($row =~ /.*1.*/) { print 2; exit; }\nprint 4;\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n<>=~/1/ and $F++;\nwhile($a=<>){\n\t$a=~/^1|1$/ and $F++;\n\t}\n$a=~/1/ and $F++;\n\nprint $F?2:4;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n<>=~/1/ and $F++;\nwhile(<>){\n\t/^1|1$/ and $F++;\n\t}\n/1/ and $F++;\n\nprint $F?2:4;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "src_uid": "0d2fd9b58142c4f5282507c916690244"} {"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\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$l,$r,$s) = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my $k = $r - $l + 1;\r\n \r\n my $s_min = $k * ( $k + 1 ) / 2;\r\n my $s_max = $n * ( $n + 1 ) / 2 - ( ($n-$k) * ( ($n-$k) + 1 ) / 2 );\r\n if( $s < $s_min or $s_max < $s ){\r\n print \"-1\\n\";\r\n next;\r\n }\r\n \r\n my $s1 = $s_min;\r\n my @p1 = (1..$k); my @p2 = ();\r\n while($s1 < $s){\r\n my $s_max = ( $#p2 < 0 ? $n : $p2[0]-1 );\r\n my $dif1 = $s_max - $p1[$#p1];\r\n unless( $s1 + $dif1 <= $s ){\r\n $dif1 = $s - $s1;\r\n $s_max = $p1[$#p1] + $dif1;\r\n }\r\n pop(@p1);\r\n unshift(@p2,$s_max);\r\n $s1 += $dif1;\r\n }\r\n my @p = (@p1,@p2);\r\n my @use = (); $#use = $n;\r\n for(my $i=0;$i<=$#p;$i++){\r\n $use[$p[$i]] = 1;\r\n }\r\n my @out = ();\r\n my $idx = 1;\r\n for(my $i=0;$i<$l-1;$i++){\r\n while($use[$idx]){\r\n $idx++;\r\n }\r\n push(@out,$idx);\r\n $use[$idx] = 1;\r\n }\r\n push(@out,@p);\r\n for(my $i=$r+1;$i<=$n;$i++){\r\n while($use[$idx]){\r\n $idx++;\r\n }\r\n push(@out,$idx);\r\n $use[$idx] = 1;\r\n }\r\n print (join(' ',@out) . \"\\n\");\r\n \r\n}\r\n\r\nexit(0);\r\n\r\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $L, $R, $s ) = split;\n\t\n\tmy $diff = $R - $L + 1;\n\t\n\tmy @arr = 1 .. $diff;\n\t\n\tmy $sum = 0;\n\t\n\t$sum += $_ for @arr;\n\t\n\tif( $s < $sum ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\tmy $i = @arr - 1;\n\t\n\twhile( $s != $sum ){\n\t\t$arr[ $i ] ++;\n\t\t$sum ++;\n\t\t$i --;\n\t\t$i < 0 and $i = @arr - 1;\n\t\t}\n\t\n\tif( $arr[ @arr - 1 ] > $n ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\tmy %h;\n\t\n\t$h{ $_ } ++ for @arr;\n\t\n\tfor( 1 .. $n ){\n\t\tnext if exists $h{ $_ };\n\t\t\n\t\tpush @arr, $_;\n\t\t}\n\t\n\tfor( 1 .. $L - 1 ){\n\t\tmy $tmp = pop @arr;\n\t\tunshift @arr, $tmp;\n\t\t}\n\t\n\tprint \"@arr\";\n\t}"}], "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\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/env perl\n# set vim: fdm=marker sw=4 ts=4 et\n\nuse strict;\nuse warnings;\n\nmy $t = int <>;\nfor (1..$t) {\n my $n = int <>;\n my ($name, $value, $count);\n my %m;\n for (1..$n) {\n my $s = <>;\n chomp $s;\n if ($s =~ /(\\S+) := (\\S*)/) {\n $name = $1;\n $value = $2;\n $count = 0;\n\n while ($value =~ /((?:ha)+)/) {\n my $len = length($1) / 2;\n $value =~ s/$1/<$len>/;\n }\n\n # while ($value =~ /<(\\d+)><(\\d+)>/) {\n # my $r = int($1) + int($2);\n # $value =~ s/<(\\d+)><(\\d+)>/<$r>/;\n # }\n # while ($value =~ s/(..)<(\\d+)>(..)/$1b$3/) {\n # $count += int($2) - 1;\n # }\n\n # $value =~ s/[^ha<>0-9]{2,}/b/g;\n # $value =~ s/hhh+/hh/g;\n # $value =~ s/aaa+/aa/g;\n\n $m{$name} = [$count, $value];\n }\n elsif ($s =~ /(\\S+) = (\\S+) \\+ (\\S+)/) {\n $name = $1;\n my $var1 = $m{$2};\n my $var2 = $m{$3};\n\n $count = $var1->[0] + $var2->[0];\n $value = $var1->[1] . $var2->[1];\n\n my $v1 = $var1->[1];\n my $v2 = $var2->[1];\n if ($v1 =~ /h$/ && $v2 =~ /^a/) {\n $v1 =~ s/h$//;\n $v2 =~ s/^a//;\n if ($v1 =~ s/<(\\d+)>$//) {\n my $v = int($1) + 1;\n $v1 .= \"<$v>\";\n }\n elsif ($v2 =~ s/^<(\\d+)>//) {\n my $v = int($1) + 1;\n $v2 = \"<$v>$v2\";\n }\n else {\n $v1 .= \"<1>\";\n }\n }\n\n if ($v1 =~ />$/ && $v2 =~ /^$//;\n $v += int($1);\n $v2 =~ s/^<(\\d+)>//;\n $v += int($1);\n $value = \"$v1<$v>$v2\";\n }\n else {\n $value = \"$v1$v2\";\n }\n\n while ($value =~ s/(..)<(\\d+)>(..)/$1b$3/) {\n $count += int($2) - 1;\n }\n\n $value =~ s/h[^ha<]/b/g;\n $value =~ s/[^ha>]a/b/g;\n $value =~ s/[^ha<>0-9]{2,}/b/g;\n $value =~ s/hhh+/hh/g;\n $value =~ s/aaa+/aa/g;\n\n $m{$name} = [$count, $value];\n }\n }\n\n ($count, $value) = @{$m{$name}};\n for my $i ($value =~ /\\d+/g) {\n $count += int($i) - 1;\n }\n\n print qq($count\\n);\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\n# set vim: fdm=marker sw=4 ts=4 et\n\nuse strict;\nuse warnings;\n\nmy $t = int <>;\nfor (1..$t) {\n my $n = int <>;\n my ($name, $value, $count);\n my %m;\n for (1..$n) {\n my $s = <>;\n chomp $s;\n if ($s =~ /(\\S+) := (\\S*)/) {\n $name = $1;\n $value = $2;\n $count = 0;\n\n while ($value =~ /((?:ha)+)/) {\n my $len = length($1) / 2;\n $value =~ s/$1/<$len>/;\n }\n\n # while ($value =~ /<(\\d+)><(\\d+)>/) {\n # my $r = int($1) + int($2);\n # $value =~ s/<(\\d+)><(\\d+)>/<$r>/;\n # }\n # while ($value =~ s/(..)<(\\d+)>(..)/$1b$3/) {\n # $count += int($2) - 1;\n # }\n\n # $value =~ s/[^ha<>0-9]{2,}/b/g;\n # $value =~ s/hhh+/hh/g;\n # $value =~ s/aaa+/aa/g;\n\n $m{$name} = [$count, $value];\n }\n elsif ($s =~ /(\\S+) = (\\S+) \\+ (\\S+)/) {\n $name = $1;\n my $var1 = $m{$2};\n my $var2 = $m{$3};\n\n $count = $var1->[0] + $var2->[0];\n $value = $var1->[1] . $var2->[1];\n\n my $v1 = $var1->[1];\n my $v2 = $var2->[1];\n if ($v1 =~ /h$/ && $v2 =~ /^a/) {\n $v1 =~ s/h$//;\n $v2 =~ s/^a//;\n if ($v1 =~ s/<(\\d+)>$//) {\n my $v = int($1) + 1;\n $v1 .= \"<$v>\";\n }\n elsif ($v2 =~ s/^<(\\d+)>//) {\n my $v = int($1) + 1;\n $v2 = \"<$v>$v2\";\n }\n else {\n $v1 .= \"<1>\";\n }\n }\n\n if ($v1 =~ />$/ && $v2 =~ /^$//;\n $v += int($1);\n $v2 =~ s/^<(\\d+)>//;\n $v += int($1);\n $value = \"$v1<$v>$v2\";\n }\n else {\n $value = \"$v1$v2\";\n }\n\n while ($value =~ s/(..)<(\\d+)>(..)/$1b$3/) {\n $count += int($2) - 1;\n }\n\n $value =~ s/h[^a<]/b/g;\n $value =~ s/[^h>]a/b/g;\n $value =~ s/[^ha<>0-9]{2,}/b/g;\n $value =~ s/hhh+/hh/g;\n $value =~ s/aaa+/aa/g;\n\n $m{$name} = [$count, $value];\n }\n }\n\n ($count, $value) = @{$m{$name}};\n for my $i ($value =~ /\\d+/g) {\n $count += int($i) - 1;\n }\n\n print qq($count\\n);\n}\n"}, {"source_code": "#!/usr/bin/env perl\n# set vim: fdm=marker sw=4 ts=4 et\n\nuse strict;\nuse warnings;\n\nmy $t = int <>;\nfor (1..$t) {\n my $n = int <>;\n my ($name, $value, $count);\n my %m;\n for (1..$n) {\n my $s = <>;\n chomp $s;\n if ($s =~ /(\\S+) := (\\S*)/) {\n $name = $1;\n $value = $2;\n $count = 0;\n\n while ($value =~ /((?:ha)+)/) {\n my $len = length($1) / 2;\n $value =~ s/$1/<$len>/;\n }\n\n # while ($value =~ /<(\\d+)><(\\d+)>/) {\n # my $r = int($1) + int($2);\n # $value =~ s/<(\\d+)><(\\d+)>/<$r>/;\n # }\n # while ($value =~ s/(..)<(\\d+)>(..)/$1b$3/) {\n # $count += int($2) - 1;\n # }\n\n # $value =~ s/[^ha<>0-9]{2,}/b/g;\n # $value =~ s/hhh+/hh/g;\n # $value =~ s/aaa+/aa/g;\n\n $m{$name} = [$count, $value];\n }\n elsif ($s =~ /(\\S+) = (\\S+) \\+ (\\S+)/) {\n $name = $1;\n my $var1 = $m{$2};\n my $var2 = $m{$3};\n\n $count = $var1->[0] + $var2->[0];\n $value = $var1->[1] . $var2->[1];\n\n my $v1 = $var1->[1];\n my $v2 = $var2->[1];\n if ($v1 =~ /h$/ && $v2 =~ /^a/) {\n $v1 =~ s/h$//;\n $v2 =~ s/^a//;\n if ($v1 =~ s/<(\\d+)>$//) {\n my $v = int($1) + 1;\n $v1 .= \"<$v>\";\n }\n elsif ($v2 =~ s/^<(\\d+)>//) {\n my $v = int($1) + 1;\n $v2 = \"<$v>$v2\";\n }\n else {\n $v1 .= \"<1>\";\n }\n }\n\n if ($v1 =~ />$/ && $v2 =~ /^$//;\n $v += int($1);\n $v2 =~ s/^<(\\d+)>//;\n $v += int($1);\n $value = \"$v1<$v>$v2\";\n }\n else {\n $value = \"$v1$v2\";\n }\n\n while ($value =~ s/(..)<(\\d+)>(..)/$1b$3/) {\n $count += int($2) - 1;\n }\n\n $value =~ s/h[^a]/b/g;\n $value =~ s/[^h]a/b/g;\n $value =~ s/[^ha<>0-9]{2,}/b/g;\n $value =~ s/hhh+/hh/g;\n $value =~ s/aaa+/aa/g;\n\n $m{$name} = [$count, $value];\n }\n }\n\n ($count, $value) = @{$m{$name}};\n for my $i ($value =~ /\\d+/g) {\n $count += int($i) - 1;\n }\n\n print qq($count\\n);\n}\n"}, {"source_code": "#!/usr/bin/env perl\n# set vim: fdm=marker sw=4 ts=4 et\n\nuse strict;\nuse warnings;\n\nmy $t = int <>;\nfor (1..$t) {\n my $n = int <>;\n my ($name, $value, $count);\n my %m;\n for (1..$n) {\n my $s = <>;\n chomp $s;\n if ($s =~ /(\\S+) := (\\S*)/) {\n $name = $1;\n $value = $2;\n $count = 0;\n\n while ($value =~ /((?:ha)+)/) {\n my $len = length($1) / 2;\n $value =~ s/$1/<$len>/;\n }\n # while ($value =~ /<(\\d+)><(\\d+)>/) {\n # my $r = int($1) + int($2);\n # $value =~ s/<(\\d+)><(\\d+)>/<$r>/;\n # }\n # while ($value =~ s/(..)<(\\d+)>(..)/$1b$3/) {\n # $count += int($2) - 1;\n # }\n\n $value =~ s/[^ha<>0-9]{2,}/b/g;\n $value =~ s/hhh+/hh/g;\n $value =~ s/aaa+/aa/g;\n\n $m{$name} = [$count, $value];\n }\n elsif ($s =~ /(\\S+) = (\\S+) \\+ (\\S+)/) {\n $name = $1;\n my $var1 = $m{$2};\n my $var2 = $m{$3};\n\n $count = $var1->[0] + $var2->[0];\n $value = $var1->[1] . $var2->[1];\n\n while ($value =~ /((?:ha)+)/) {\n my $len = length($1) / 2;\n $value =~ s/$1/<$len>/;\n }\n\n # while ($value =~ /<(\\d+)><(\\d+)>/) {\n # my $r = int($1) + int($2);\n # $value =~ s/<(\\d+)><(\\d+)>/<$r>/;\n # }\n while ($value =~ s/(\\d+)><(\\d+)/int($1) + int($2)/e) {}\n\n while ($value =~ s/(..)<(\\d+)>(..)/$1b$3/) {\n $count += int($2) - 1;\n }\n\n while ($value =~ s/([^h])a/$1/) {}\n while ($value =~ s/h([^a])/$1/) {}\n\n $value =~ s/[^ha<>0-9]{2,}/b/g;\n $value =~ s/hhh+/hh/g;\n $value =~ s/aaa+/aa/g;\n\n $m{$name} = [$count, $value];\n }\n }\n\n ($count, $value) = @{$m{$name}};\n for my $i ($value =~ /\\d+/g) {\n $count += int($i) - 1;\n }\n\n print qq($count\\n);\n}\n"}, {"source_code": "#!/usr/bin/env perl\n# set vim: fdm=marker sw=4 ts=4 et\n\nuse strict;\nuse warnings;\n\nmy $t = int <>;\nfor (1..$t) {\n my $n = int <>;\n my ($name, $value, $count);\n my %m;\n for (1..$n) {\n my $s = <>;\n chomp $s;\n if ($s =~ /(\\S+) := (\\S*)/) {\n $name = $1;\n $value = $2;\n $count = 0;\n\n while ($value =~ /((?:ha)+)/) {\n my $len = length($1) / 2;\n $value =~ s/$1/<$len>/;\n }\n # while ($value =~ /<(\\d+)><(\\d+)>/) {\n # my $r = int($1) + int($2);\n # $value =~ s/<(\\d+)><(\\d+)>/<$r>/;\n # }\n # while ($value =~ s/(..)<(\\d+)>(..)/$1b$3/) {\n # $count += int($2) - 1;\n # }\n\n $value =~ s/[^ha<>0-9]{2,}/b/g;\n $value =~ s/hhh+/hh/g;\n $value =~ s/aaa+/aa/g;\n\n $m{$name} = [$count, $value];\n }\n elsif ($s =~ /(\\S+) = (\\S+) \\+ (\\S+)/) {\n $name = $1;\n my $var1 = $m{$2};\n my $var2 = $m{$3};\n\n $count = $var1->[0] + $var2->[0];\n $value = $var1->[1] . $var2->[1];\n\n while ($value =~ /((?:ha)+)/) {\n my $len = length($1) / 2;\n $value =~ s/$1/<$len>/;\n }\n # while ($value =~ /<(\\d+)><(\\d+)>/) {\n # my $r = int($1) + int($2);\n # $value =~ s/<(\\d+)><(\\d+)>/<$r>/;\n # }\n $value =~ s/(\\d+)><(\\d+)/int($1) + int($2)/ge;\n while ($value =~ s/(..)<(\\d+)>(..)/$1b$3/) {\n $count += int($2) - 1;\n }\n\n $value =~ s/[^ha<>0-9]{2,}/b/g;\n $value =~ s/hhh+/hh/g;\n $value =~ s/aaa+/aa/g;\n\n $m{$name} = [$count, $value];\n }\n }\n\n ($count, $value) = @{$m{$name}};\n for my $i ($value =~ /\\d+/g) {\n $count += int($i) - 1;\n }\n\n print qq($count\\n);\n}\n"}], "src_uid": "356bd72aa9143681f1efd695969a8c8e"} {"nl": {"description": "In the Bus of Characters there are $$$n$$$ rows of seat, each having $$$2$$$ seats. The width of both seats in the $$$i$$$-th row is $$$w_i$$$ centimeters. All integers $$$w_i$$$ are distinct.Initially the bus is empty. On each of $$$2n$$$ stops one passenger enters the bus. There are two types of passengers: an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 200\\,000$$$) \u2014 the number of rows in the bus. The second line contains the sequence of integers $$$w_1, w_2, \\dots, w_n$$$ ($$$1 \\le w_i \\le 10^{9}$$$), where $$$w_i$$$ is the width of each of the seats in the $$$i$$$-th row. It is guaranteed that all $$$w_i$$$ are distinct. The third line contains a string of length $$$2n$$$, consisting of digits '0' and '1' \u2014 the description of the order the passengers enter the bus. If the $$$j$$$-th character is '0', then the passenger that enters the bus on the $$$j$$$-th stop is an introvert. If the $$$j$$$-th character is '1', the the passenger that enters the bus on the $$$j$$$-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i.\u00a0e. both numbers equal $$$n$$$), and for each extrovert there always is a suitable row.", "output_spec": "Print $$$2n$$$ integers \u2014 the rows the passengers will take. The order of passengers should be the same as in input.", "sample_inputs": ["2\n3 1\n0011", "6\n10 8 9 11 13 5\n010010011101"], "sample_outputs": ["2 1 1 2", "6 6 2 3 3 1 4 4 1 2 5 5"], "notes": "NoteIn the first example the first passenger (introvert) chooses the row $$$2$$$, because it has the seats with smallest width. The second passenger (introvert) chooses the row $$$1$$$, because it is the only empty row now. The third passenger (extrovert) chooses the row $$$1$$$, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row $$$2$$$, because it is the only row with an empty place."}, "positive_code": [{"source_code": "my $n = <>;\nmy @sizes = split ' ', <>;\nmy $line = <>; chomp $line;\nmy (@is, @es);\n@is = sort { @sizes[$b - 1] <=> @sizes[$a - 1] } (1..$n);\nfor my $i (split '', $line) {\n if ($i eq '0') {\n $_ = pop @is;\n push @es, $_;\n } else {\n $_ = pop @es;\n }\n print $_ . ' ';\n}"}, {"source_code": "<>;\n\n%h = map { $_, ++ $i } @_ = split ' ', <>;\n\n@_ = sort { $a <=> $b } @_;\n\nfor( <> =~ /./g ){\n\tpush @a, $_ ?\n\t\t$h{ pop @s }\n\t\t:\n\t\tdo {\n\t\t\tpush @s, $w = shift @_;\n\t\t\t$h{ $w }\n\t\t}\n\t}\n\nprint \"@a\""}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmy $i = 0;\n\t\n\tmap { $h{ $_ } = ++ $i } @_;\n\t\n\t@_ = sort { $a <=> $b } @_;\n\t\n\t$_ = <>, chomp;\n\t\n\tmy @ans;\n\tmy @stack;\n\t\n\tfor( split // ){\n\t\tif( $_ ){\n\t\t\tpush @ans, $h{ pop @stack };\n\t\t\t}\n\t\telse{\n\t\t\tmy $w = shift @_;\n\t\t\tpush @ans, $h{ $w };\n\t\t\tpush @stack, $w;\n\t\t\t}\n\t\t}\n\t\n\tprint \"@ans\";\n\t}"}], "negative_code": [], "src_uid": "161009edb8e3d438cdd8c0d1e202f783"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($W,$H) = map { $_ - 0 } split(/\\s+/,);\r\n my ($x1,$y1,$x2,$y2) = map { $_ - 0 } split(/\\s+/,);\r\n my ($w,$h) = map { $_ - 0 } split(/\\s+/,);\r\n if( $W - ($x2-$x1) < $w and $H - ($y2-$y1) < $h ){\r\n print \"-1\\n\"; next;\r\n }\r\n if( $x1 >= $w or $W - $x2 >= $w or $y1 >= $h or $H - $y2 > $h ){\r\n print \"0\\n\"; next;\r\n }\r\n my $r = $mod;\r\n if( $W - ($x2-$x1) >= $w ){\r\n $r = $w - $x1 if $w - $x1 < $r;\r\n $r = $w - ($W - $x2) if $w - ($W - $x2) < $r;\r\n }\r\n if( $H - ($y2-$y1) >= $h ){\r\n $r = $h - $y1 if $h - $y1 < $r;\r\n $r = $h - ($H - $y2) if $h - ($H - $y2) < $r;\r\n }\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "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$$$) \u2014 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$$$) \u2014 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": "$t = <>;\nwhile($t--) {\n @arr =split ' ', <>;\n $n = @arr[0];\n $x = @arr[1];\n $y = @arr[2];\n $diff = $y - $x;\n\n $resmn = 0;\n $resd = 0;\n $res = 1000000000;\n for ($d = 1; $d <= 50; $d++) {\n if ($diff % $d == 0) {\n $mx = $x + $d*($n-1);\n if ($mx >= $y) {\n $mn = $x;\n while ($mx - $d >= $y && $mn - $d > 0) {\n $mx -= $d;\n $mn -= $d;\n }\n if ($mx < $res) {\n $res = $mx;\n $resd = $d;\n $resmn = $mn;\n }\n }\n }\n }\n for ($i = 0; $i < $n; $i++) {\n $a = $resmn + $resd*$i;\n print(\"$a \");\n }\n print(\"\\n\");\n}\n"}], "negative_code": [{"source_code": "$t = <>;\nwhile($t--) {\n @arr =split ' ', <>;\n $n = @arr[0];\n $x = @arr[1];\n $y = @arr[2];\n $diff = $y - $x;\n\n $resmn = 0;\n $resd = 0;\n $res = 1000000000;\n for ($d = 1; $d <= 50; $d++) {\n if ($diff % $d == 0) {\n $mx = $x + $d*($n-1);\n if ($mx >= $y) {\n $mn = $x;\n while ($mx - $d >= $y && $mn - $d >= 0) {\n $mx -= $d;\n $mn -= $d;\n }\n if ($mx < $res) {\n $res = $mx;\n $resd = $d;\n $resmn = $mn;\n }\n }\n }\n }\n for ($i = 0; $i < $n; $i++) {\n $a = $resmn + $resd*$i;\n print(\"$a \");\n }\n print(\"\\n\");\n}\n"}], "src_uid": "ca9d97e731e86cf8223520f39ef5d945"} {"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$$$) \u2014 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$$$) \u2014 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 \u2014 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": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n \nfor ( 1..$line ) {\n chomp ($line = );\n my ($c, $m, $x) = split q{ }, $line;\n my $answer = 0;\n\n $answer = min(min($c, $m), int(($c+$m+$x)/3));\n\n say $answer;\n}\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for (@numbers) {\n $min = $_ if $_ < $min;\n }\n return $min;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x15;\n\t\n\tmy( $c, $m, $x ) = split;\n\t\n\t( $c, $m ) = sort { $b <=> $a } $c, $m;\n\t\n\tmy( $min ) = sort { $a <=> $b } $c, $m, $x;\n\t\n\t$debug and print \"$c, $m, $x\";\n\t\n\tmap $_ -= $min, $c, $m, $x;\n\t\n\t$debug and print \"$c, $m, $x\";\n\t\n\tprint $min + do {\n\t\tif( $x == 0 ){\n\t\t\t( sort { $a <=> $b } int( ( $c + $m ) / 3 ), $m )[ 0 ];\n\t\t\t}\n\t\telse{\n\t\t\t0;\n\t\t\t}\n\t\t};\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n \nfor ( 1..$line ) {\n chomp ($line = );\n my ($c, $m, $x) = split q{ }, $line;\n my $answer = 0;\n\n if ( $c >= 1 and $m >= 1 and $x >= 1 ) {\n my $min = min($c, $m, $x);\n $c -= $min, $m -= $min, $x -= $min;\n $answer += $min;\n }\n\n if ( $c >= 3 and $m >= 3 ) {\n my $min = min(int($c/3), int($m/3));\n $c -= 3*$min, $m -= 3*$min;\n $answer += $min*2;\n }\n\n if ( $c < $m ) { \n ($c, $m) = ($m, $c);\n }\n\n if ( $c >= 2 and $m >= 1 ) {\n $c -= 2, $m -= 1;\n $answer += 1;\n next;\n }\n last;\n\n say $answer;\n}\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for (@numbers) {\n $min = $_ if $_ < $min;\n }\n return $min;\n}\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n \nfor ( 1..$line ) {\n chomp ($line = );\n my ($c, $m, $x) = split q{ }, $line;\n my $answer = 0;\n\n if ( $c >= 1 and $m >= 1 and $x >= 1 ) {\n my $min = min($c, $m, $x);\n $c -= $min, $m -= $min, $x -= $min;\n $answer += $min;\n }\n\n while ( 1 ) {\n \n if ( $c < $m ) {\n ($c, $m) = ($m, $c);\n }\n\n if ( $c >= 2 and $m >= 1 ) {\n my $min = min(int($c/2), $m);\n $c -= 2*$min, $m -= $min;\n $answer += $min;\n next;\n }\n last;\n }\n\n say $answer;\n}\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for (@numbers) {\n $min = $_ if $_ < $min;\n }\n return $min;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x15;\n\t\n\tmy( $c, $m, $x ) = split;\n\t\n\t( $c, $m ) = sort { $b <=> $a } $c, $m;\n\t\n\tmy( $min ) = sort { $a <=> $b } $c, $m, $x;\n\t\n\t$debug and print \"$c, $m, $x\";\n\t\n\tmap $_ -= $min, $c, $m, $x;\n\t\n\t$debug and print \"$c, $m, $x\";\n\t\n\tprint $min + do {\n\t\tif( $x == 0 ){\n\t\t\tint( ( $c + $m ) / 3 );\n\t\t\t}\n\t\telse{\n\t\t\t0;\n\t\t\t}\n\t\t};\n\t}"}], "src_uid": "b18dac401b655c06bee331e71eb3e4de"} {"nl": {"description": "Recall that a permutation of length $$$n$$$ is an array where each element from $$$1$$$ to $$$n$$$ occurs exactly once.For a fixed positive integer $$$d$$$, let's define the cost of the permutation $$$p$$$ of length $$$n$$$ as the number of indices $$$i$$$ $$$(1 \\le i < n)$$$ such that $$$p_i \\cdot d = p_{i + 1}$$$.For example, if $$$d = 3$$$ and $$$p = [5, 2, 6, 7, 1, 3, 4]$$$, then the cost of such a permutation is $$$2$$$, because $$$p_2 \\cdot 3 = p_3$$$ and $$$p_5 \\cdot 3 = p_6$$$.Your task is the following one: for a given value $$$n$$$, find the permutation of length $$$n$$$ and the value $$$d$$$ with maximum possible cost (over all ways to choose the permutation and $$$d$$$). If there are multiple answers, then print any of them.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 500$$$)\u00a0\u2014 the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print the value $$$d$$$ in the first line, and $$$n$$$ integers in the second line\u00a0\u2014 the permutation itself. If there are multiple answers, then print any of them.", "sample_inputs": ["2\n\n2\n\n3"], "sample_outputs": ["2\n1 2\n3\n2 1 3"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n \r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @u = (); $#u = $n - 1;\r\n my @o = ();\r\n for(my $i=0;$i<$n;$i++){\r\n next if( $u[$i] - 0 > 0 );\r\n my $v = $i + 1;\r\n while( $v <= $n ){\r\n\t push(@o,$v);\r\n\t $u[$v-1] = 1;\r\n\t $v += $v;\r\n }\r\n }\r\n print \"2\\n\";\r\n print ( join(' ',@o) . \"\\n\" );\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "src_uid": "3b9380ca571dbf3e24fc1b1c8b91790b"} {"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\u00a0\u2014 $$$(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\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the coordinates of the rectangle, the black paint was spilled on.", "output_spec": "Output $$$t$$$ lines, each of which contains two numbers\u00a0\u2014 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": "use strict;\nuse warnings;\nuse integer;\n\n\nmy $q = <>;\nfor (my $i = 0; $i < $q; $i++) {core();};\n\nsub core {\n\tmy ($n, $m) = readpos(); # board sizes\n\tmy ($wx1, $wy1, $wx2, $wy2) = readrect(); # spill white rectangle\n\tmy ($bx1, $by1, $bx2, $by2) = readrect(); # spill black rectangle\n\n\tmy ($white, $black) = aftercolors($m, $n, $wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2);\n\tprint \"$white $black\\n\";\n}\n\nsub readpos {\n\t<> =~ /(\\d+)\\s+(\\d+)/;\n\treturn ($1, $2);\n}\n\nsub readrect {\n\t<> =~ /(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)/;\n\treturn ($1, $2, $3, $4);\n}\n\nsub aftercolors {\n\tmy ($m, $n, $wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2) = @_;\n\tmy ($boardwhite, $boardblack) = colors(1, 1, $m, $n);\n\n\tmy ($whiteunderwhitespillarea, $blackunderwhitespillarea) = colors($wx1, $wy1, $wx2, $wy2);\n\tmy ($whiteunderblackspillarea, $blackunderblackspillarea) = colors($bx1, $by1, $bx2, $by2);\n\n\tmy ($cx1, $cy1, $cx2, $cy2) = commonargs($wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2);\n\tmy ($whiteundercommon, $blackundercommon) = colors($cx1, $cy1, $cx2, $cy2);\n\n\tmy $white = $boardwhite + $blackunderwhitespillarea - $blackundercommon - $whiteunderblackspillarea;\n\tmy $black = $boardblack - $blackunderwhitespillarea + $blackundercommon + $whiteunderblackspillarea;\n\n\treturn ($white, $black);\n}\n\nsub valid {\n\tmy ($x1, $y1, $x2, $y2) = @_;\n\treturn $x1 <= $x2 && $y1 <= $y2;\n}\n\nsub commonargs {\n\tmy ($wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2) = @_;\n\treturn (max($wx1, $bx1), max($wy1, $by1), min($wx2, $bx2), min($wy2, $by2));\n}\n\nsub colors {\n\tmy ($x1, $y1, $x2, $y2) = @_;\n\tmy $area = area($x1, $y1, $x2, $y2);\n\n\tif ($area % 2 == 0) {\n\t\tmy $halfarea = $area / 2;\n\t\treturn ($halfarea, $halfarea);\n\t} else {\n\t\tmy ($more, $less) = (($area + 1) / 2, ($area - 1) / 2);\n\t\treturn colorpos(\"white\", $x1, $y1) ? ($more, $less) : ($less, $more);\n\t}\n}\n\nsub colorpos {\n\tmy ($color, $x, $y) = @_;\n\tmy $diagonal = $x % 2 == $y % 2;\n\treturn $color eq \"white\" ? $diagonal : !$diagonal;\n}\n\nsub area {\n\tmy ($x1, $y1, $x2, $y2) = @_;\n\tif (valid($x1, $y1, $x2, $y2)) {\n\t\tmy $lx = $x2 - $x1 + 1;\n\t\tmy $ly = $y2 - $y1 + 1;\n\t\treturn $lx * $ly;\n\t} else {\n\t\treturn 0;\n\t}\n}\n\n# use List::Util qw[min max];\nsub min {\n\tmy ($a, $b) = @_;\n\treturn $a <= $b ? $a : $b;\n}\nsub max {\n\tmy ($a, $b) = @_;\n\treturn $a <= $b ? $b : $a;\n}\n"}, {"source_code": "use strict;\nuse warnings;\nuse integer;\n\n\nmy $q = <>;\nfor (my $i = 0; $i < $q; $i++) {core();};\n\nsub core {\n\tmy ($n, $m) = readpos(); # board sizes\n\tmy ($wx1, $wy1, $wx2, $wy2) = readrect(); # spill white rectangle\n\tmy ($bx1, $by1, $bx2, $by2) = readrect(); # spill black rectangle\n\n\tmy ($white, $black) = aftercolors($m, $n, $wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2);\n\tprint \"$white $black\\n\";\n}\n\nsub readpos {\n\t<> =~ /(\\d+)\\s+(\\d+)/;\n\treturn ($1, $2);\n}\n\nsub readrect {\n\t<> =~ /(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)/;\n\treturn ($1, $2, $3, $4);\n}\n\nsub aftercolors {\n\tmy ($m, $n, $wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2) = @_;\n\tmy $boardwhite = white(1, 1, $m, $n);\n\tmy $boardblack = black(1, 1, $m, $n);\n\n\tmy $whiteunderwhitespillarea = white($wx1, $wy1, $wx2, $wy2);\n\tmy $blackunderwhitespillarea = black($wx1, $wy1, $wx2, $wy2);\n\tmy $whiteunderblackspillarea = white($bx1, $by1, $bx2, $by2);\n\tmy $blackunderblackspillarea = black($bx1, $by1, $bx2, $by2);\n\n\tmy ($cx1, $cy1, $cx2, $cy2) = commonargs($wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2);\n\n\tmy $whiteundercommon = white($cx1, $cy1, $cx2, $cy2);\n\tmy $blackundercommon = black($cx1, $cy1, $cx2, $cy2);\n\n\tmy $white = $boardwhite + $blackunderwhitespillarea - $blackundercommon - $whiteunderblackspillarea;\n\tmy $black = $boardblack - $blackunderwhitespillarea + $blackundercommon + $whiteunderblackspillarea;\n\n\treturn ($white, $black);\n}\n\nsub valid {\n\tmy ($x1, $y1, $x2, $y2) = @_;\n\treturn $x1 <= $x2 && $y1 <= $y2;\n}\n\nsub commonargs {\n\tmy ($wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2) = @_;\n\treturn (max($wx1, $bx1), max($wy1, $by1), min($wx2, $bx2), min($wy2, $by2));\n}\n\nsub color {\n\tmy ($color, $x1, $y1, $x2, $y2) = @_;\n\tmy $area = area($x1, $y1, $x2, $y2);\n\n\tif ($area % 2 == 0) {\n\t\treturn $area / 2;\n\t} else {\n\t\treturn colorpos($color, $x1, $y1) ? ($area + 1) / 2 : ($area - 1) / 2;\n\t}\n}\n\nsub colorpos {\n\tmy ($color, $x, $y) = @_;\n\tmy $diagonal = $x % 2 == $y % 2;\n\treturn $color eq \"white\" ? $diagonal : !$diagonal;\n}\n\nsub white {\n\tmy ($x1, $y1, $x2, $y2) = @_;\n\treturn color(\"white\", $x1, $y1, $x2, $y2);\n}\n\nsub black {\n\tmy ($x1, $y1, $x2, $y2) = @_;\n\treturn color(\"black\", $x1, $y1, $x2, $y2);\n}\n\nsub area {\n\tmy ($x1, $y1, $x2, $y2) = @_;\n\tif (valid($x1, $y1, $x2, $y2)) {\n\t\tmy $lx = $x2 - $x1 + 1;\n\t\tmy $ly = $y2 - $y1 + 1;\n\t\treturn $lx * $ly;\n\t} else {\n\t\treturn 0;\n\t}\n}\n\n# use List::Util qw[min max];\nsub min {\n\tmy ($a, $b) = @_;\n\treturn $a <= $b ? $a : $b;\n}\nsub max {\n\tmy ($a, $b) = @_;\n\treturn $a <= $b ? $b : $a;\n}\n"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\nuse integer;\n\n<>;\nwhile (<>) {\n\t/(\\d+)\\s+(\\d+)/;\n\tmy $r = sumbetween($1, $2);\n\tprint $r, \"\\n\";\n}\n\nsub sumbetween {\n\tmy ($beg, $end) = @_;\n\tmy $all = mount($end);\n\tmy $before = mount($beg - 1);\n\treturn $all - $before;\n}\n\nsub mount {\n\tmy $i = shift(@_);\n\tmy $sign = $i % 2 == 0 ? +1 : -1;\n\t# print \"(\", $sign * upperdiv($i, 2), \")\";\n\treturn $sign * upperdiv($i, 2);\n}\n\nsub upperdiv {\n\tmy ($a, $b) = @_;\n\tmy $r = $a / $b;\n\t$r++ if $a % $b > 0;\n\treturn $r;\n}\n"}], "src_uid": "1c4607c96f7755af09445ff29a54bd08"} {"nl": {"description": "Diamond Miner is a game that is similar to Gold Miner, but there are $$$n$$$ miners instead of $$$1$$$ in this game.The mining area can be described as a plane. The $$$n$$$ miners can be regarded as $$$n$$$ points on the y-axis. There are $$$n$$$ diamond mines in the mining area. We can regard them as $$$n$$$ points on the x-axis. For some reason, no miners or diamond mines can be at the origin (point $$$(0, 0)$$$). Every miner should mine exactly one diamond mine. Every miner has a hook, which can be used to mine a diamond mine. If a miner at the point $$$(a,b)$$$ uses his hook to mine a diamond mine at the point $$$(c,d)$$$, he will spend $$$\\sqrt{(a-c)^2+(b-d)^2}$$$ energy to mine it (the distance between these points). The miners can't move or help each other.The object of this game is to minimize the sum of the energy that miners spend. Can you find this minimum?", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 10$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the number of miners and mines. Each of the next $$$2n$$$ lines contains two space-separated integers $$$x$$$ ($$$-10^8 \\le x \\le 10^8$$$) and $$$y$$$ ($$$-10^8 \\le y \\le 10^8$$$), which represent the point $$$(x,y)$$$ to describe a miner's or a diamond mine's position. Either $$$x = 0$$$, meaning there is a miner at the point $$$(0, y)$$$, or $$$y = 0$$$, meaning there is a diamond mine at the point $$$(x, 0)$$$. There can be multiple miners or diamond mines at the same point. It is guaranteed that no point is at the origin. It is guaranteed that the number of points on the x-axis is equal to $$$n$$$ and the number of points on the y-axis is equal to $$$n$$$. It's guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print a single real number \u2014 the minimal sum of energy that should be spent. Your answer is considered correct if 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 accepted if and only if $$$\\frac{|a - b|}{\\max{(1, |b|)}} \\le 10^{-9}$$$.", "sample_inputs": ["3\n2\n0 1\n1 0\n0 -1\n-2 0\n4\n1 0\n3 0\n-5 0\n6 0\n0 3\n0 1\n0 2\n0 4\n5\n3 0\n0 4\n0 -3\n4 0\n2 0\n1 0\n-3 0\n0 -10\n0 -2\n0 -10"], "sample_outputs": ["3.650281539872885\n18.061819283610362\n32.052255376143336"], "notes": "NoteIn the first test case, the miners are at $$$(0,1)$$$ and $$$(0,-1)$$$, while the diamond mines are at $$$(1,0)$$$ and $$$(-2,0)$$$. If you arrange the miners to get the diamond mines in the way, shown in the picture, you can get the sum of the energy $$$\\sqrt2 + \\sqrt5$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\tmy $n = $_;\n\t\n\t@_ = map ~~<>, 1 .. 2 * $n;\n\t\n\tmy @miners;\n\tmy @mines;\n\t\n\tfor( @_ ){\n\t\tmy( $x, $y ) = split;\n\t\t\n\t\tif( $x == 0 ){\n\t\t\tpush @miners, $y;\n\t\t\t}\n\t\telse{\n\t\t\tpush @mines, $x;\n\t\t\t}\n\t\t}\n\t\n\t$_ = abs $_ for @miners, @mines;\n\t\n\t@miners = sort { $a <=> $b } @miners;\n\t@mines = sort { $a <=> $b } @mines;\n\t\n\tmy $energy = 0;\n\t\n\tfor( 1 .. @miners ){\n\t\t$energy += sqrt( ( pop @miners ) ** 2 + ( pop @mines ) ** 2 );\n\t\t}\n\t\n\tprint $energy;\n\t}"}], "negative_code": [], "src_uid": "ba27ac62b84705d80fa580567ab64c3b"} {"nl": {"description": "There are $$$n$$$ piranhas with sizes $$$a_1, a_2, \\ldots, a_n$$$ in the aquarium. Piranhas are numbered from left to right in order they live in the aquarium.Scientists of the Berland State University want to find if there is dominant piranha in the aquarium. The piranha is called dominant if it can eat all the other piranhas in the aquarium (except itself, of course). Other piranhas will do nothing while the dominant piranha will eat them.Because the aquarium is pretty narrow and long, the piranha can eat only one of the adjacent piranhas during one move. Piranha can do as many moves as it needs (or as it can). More precisely: The piranha $$$i$$$ can eat the piranha $$$i-1$$$ if the piranha $$$i-1$$$ exists and $$$a_{i - 1} < a_i$$$. The piranha $$$i$$$ can eat the piranha $$$i+1$$$ if the piranha $$$i+1$$$ exists and $$$a_{i + 1} < a_i$$$. When the piranha $$$i$$$ eats some piranha, its size increases by one ($$$a_i$$$ becomes $$$a_i + 1$$$).Your task is to find any dominant piranha in the aquarium or determine if there are no such piranhas.Note that you have to find any (exactly one) dominant piranha, you don't have to find all of them.For example, if $$$a = [5, 3, 4, 4, 5]$$$, then the third piranha can be dominant. Consider the sequence of its moves: The piranha eats the second piranha and $$$a$$$ becomes $$$[5, \\underline{5}, 4, 5]$$$ (the underlined piranha is our candidate). The piranha eats the third piranha and $$$a$$$ becomes $$$[5, \\underline{6}, 5]$$$. The piranha eats the first piranha and $$$a$$$ becomes $$$[\\underline{7}, 5]$$$. The piranha eats the second piranha and $$$a$$$ becomes $$$[\\underline{8}]$$$. 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$$$) \u2014 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 3 \\cdot 10^5$$$) \u2014 the number of piranhas in the aquarium. 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 size of the $$$i$$$-th piranha. It is guaranteed that the sum of $$$n$$$ does not exceed $$$3 \\cdot 10^5$$$ ($$$\\sum n \\le 3 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer: -1 if there are no dominant piranhas in the aquarium or index of any dominant piranha otherwise. If there are several answers, you can print any.", "sample_inputs": ["6\n5\n5 3 4 4 5\n3\n1 1 1\n5\n4 4 3 4 4\n5\n5 5 4 3 2\n3\n1 1 2\n5\n5 4 3 5 5"], "sample_outputs": ["3\n-1\n4\n3\n3\n1"], "notes": "NoteThe first test case of the example is described in the problem statement.In the second test case of the example, there are no dominant piranhas in the aquarium.In the third test case of the example, the fourth piranha can firstly eat the piranha to the left and the aquarium becomes $$$[4, 4, 5, 4]$$$, then it can eat any other piranha in the aquarium."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\t@_ = ( $_[ 0 ], @_, $_[ @_ - 1 ] );\n\t\n\tmy $max = 0;\n\t\n\t$_ > $max and $max = $_ for @_;\n\t\n\tmy $idx = -1;\n\t\n\tfor my $i ( 1 .. @_ - 2 ){\n\t\tif( $_[ $i ] == $max and ( $_[ $i - 1 ] < $max or $_[ $i + 1 ] < $max ) ){\n\t\t\t$idx = $i;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tprint $idx;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @sorted = sort { $b <=> $a } @_;\n\t\n\tprint do {\n\t\tif( $sorted[ 0 ] == $sorted[ @sorted - 1 ] ){\n\t\t\t-1;\n\t\t\t}\n\t\telse{\n\t\t\tmy $max = $sorted[ 0 ];\n\t\t\tmy $idx;\n\t\t\tfor my $i ( 0 .. @_ - 1 ){\n\t\t\t\tif( $_[ $i ] == $max ){\n\t\t\t\t\t$idx = $i + 1;\n\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t$idx;\n\t\t\t}\n\t\t};\n\t\n\t}"}], "src_uid": "5598d5954fa3e3cecedb413033259760"} {"nl": {"description": "Sam lives in Awesomeburg, its downtown has a triangular shape. Also, the following is true about the triangle: its vertices have integer coordinates, the coordinates of vertices are non-negative, and its vertices are not on a single line. He calls a point on the downtown's border (that is the border of the triangle) safe if he can reach this point from at least one point of the line $$$y = 0$$$ walking along some straight line, without crossing the interior of the triangle. In the picture the downtown is marked with grey color. The first path is invalid because it does not go along a straight line. The second path is invalid because it intersects with the interior of the downtown. The third and fourth paths are correct. Find the total length of the unsafe parts of the downtown border. It can be proven that these parts are segments and their number is finite.", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) \u2014 the number of test cases. Description of the test cases follows. Each test case contains three lines, each of them contains two integers $$$x_i$$$, $$$y_i$$$ ($$$0 \\le x_i, y_i \\le 10^9$$$)\u00a0\u2014 coordinates of the vertices of the downtown's border.", "output_spec": "For each test case print a single number\u00a0\u2014 the answer to the problem. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-9}$$$. Formally let your answer be $$$a$$$, jury answer be $$$b$$$. Your answer will be considered correct if $$$\\frac{|a - b|}{\\max{(1, |b|)}} \\le 10^{-9}$$$.", "sample_inputs": ["5\n8 10\n10 4\n6 2\n4 6\n0 1\n4 2\n14 1\n11 2\n13 2\n0 0\n4 0\n2 4\n0 1\n1 1\n0 0"], "sample_outputs": ["0.0000000\n0\n2.0000\n0.00\n1"], "notes": "NoteIn the picture, the downtowns of the first three test cases are illustrated. Triangles are enumerated according to the indices of test cases they belong to. In the first two test cases, all points on the borders of the downtowns are safe, thus the answers are $$$0$$$.In the following picture unsafe points for the third test case are marked with black color: In the fourth test case, all points on the border of the downtown are safe."}, "positive_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t@_ = sort { $a->[ 1 ] <=> $b->[ 1 ] } map [ split ], $_, ~~<>, ~~<>;\r\n\t\r\n\tprint $_[ 1 ][ 1 ] == $_[ 2 ][ 1 ] ?\r\n\t\tabs( $_[ 1 ][ 0 ] - $_[ 2 ][ 0 ] ) : 0;\r\n\t}"}, {"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nmy $s = sub { shift =~ s/.* //r };\r\n\r\nwhile(<>){\r\n\t@_ = sort { $s->( $a ) <=> $s->( $b ) } $_, ~~<>, ~~<>;\r\n\t\r\n\tprint $s->( $_[ 1 ] ) == $s->( $_[ 2 ] ) ?\r\n\t\tabs( $_[ 1 ] - $_[ 2 ] ) : 0;\r\n\t}"}, {"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$_ .= <> . <>;\r\n\t\r\n\t@_ = m/ \\K(\\d+)/g;\r\n\t\r\n\tmy $min = ( sort { $a <=> $b } @_ )[ 0 ];\r\n\t\r\n\tprint m/\r\n\t\t\\b(\\d+)[ ](\\d+)\\b\r\n\t\t.*\r\n\t\t\\b(\\d+)[ ](\\d+)\\b\r\n\t\t(??{ ( $2 > $min && $2 == $4 ) ? \"\" : \"(*FAIL)\" })\r\n\t\t/sx ?\r\n\t\tabs( $1 - $3 ) : 0;\r\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$_ .= <> . <>;\r\n\t\r\n\tprint m/\r\n\t\t\\b(\\d+)[ ](\\d+)\\b\r\n\t\t.*\r\n\t\t\\b(\\d+)[ ](\\d+)\\b\r\n\t\t(??{ $2 && $2 == $4 ? \"\" : \"(*FAIL)\" })\r\n\t\t/sx ?\r\n\t\tabs( $1 - $3 ) : 0;\r\n\t}"}], "src_uid": "9f019c3898f27d687c5b3498586644e8"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\tmy $n = $_;\n\t\n\t@_ = map { [ split ' ', <> ] } 1 .. $n * 2;\n\t\n\tmy $sum = 0;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tfor my $j ( 0 .. @_ - 1 ){\n\t\t\t$i > $n - 1 and $j > $n - 1 and $sum += $_[ $i ][ $j ];\n\t\t\t}\n\t\t}\n\t\n\tmy @cand = (\n\t\t$_[ 0 ][ $n ],\n\t\t$_[ 0 ][ 2 * $n - 1 ],\n\t\t$_[ $n - 1 ][ $n ],\n\t\t$_[ $n - 1 ][ 2 * $n - 1 ],\n\t\t$_[ $n ][ 0 ],\n\t\t$_[ $n ][ $n - 1 ],\n\t\t$_[ 2 * $n - 1 ][ 0 ],\n\t\t$_[ 2 * $n - 1 ][ $n - 1 ],\n\t\t);\n\t\n\tmy $min = +( sort { $a <=> $b } @cand )[ 0 ];\n\t\n\tprint $sum + $min;\n\t}"}], "negative_code": [], "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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\tprint /(.)(?!\\1)(.)/ ? \"YES\\n$1$2\" : \"NO\";\n\t}"}, {"source_code": "<>;\n\nprint <> =~ /(.)(?!\\1)(.)/ ? \"YES\\n$1$2\" : \"NO\";"}], "negative_code": [], "src_uid": "ce4443581d4ee12db6607695cd567070"} {"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\u2009=\u20097 and h\u2009=\u2009[1,\u20092,\u20096,\u20091,\u20091,\u20097,\u20091] 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\u2009\u2264\u2009n\u2009\u2264\u20091.5\u00b7105,\u20091\u2009\u2264\u2009k\u2009\u2264\u2009n) \u2014 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,\u2009h2,\u2009...,\u2009hn (1\u2009\u2264\u2009hi\u2009\u2264\u2009100), 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\u2009+\u20091, ..., j\u2009+\u2009k\u2009-\u20091 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": "\n\n($n, $k) = split ' ', <>;\n@a = split ' ', <>;\n\n$ans = 1;\n$sum = 0;\nfor($i = 0; $i < $k; $i++)\n{\n $sum += $a[$i];\n}\n$min_sum = $sum;\nfor($i = 1; $i < $n - $k + 1; $i++)\n{\n $sum -= $a[$i - 1];\n $sum += $a[$i + $k - 1];\n if($min_sum > $sum)\n {\n $ans = $i + 1;\n $min_sum = $sum;\n }\n}\nprint $ans;\n"}, {"source_code": "my $input = ;\nchomp ($input);\n\nmy @data = split (\" \", $input);\nmy $n = shift (@data);\nmy $k = shift (@data);\n\nmy $input = ;\nchomp ($input);\nmy @data = split (\" \", $input);\n\nmy $i = 1;\nmy @h;\nwhile ($i != $n + 1) {\n $h[$i] = shift (@data);\n $i++;\n}\n\nif ($n == $k) {\n print \"1\";\n exit;\n}\n\nmy @s;\nmy $j = 0;\nmy $z = 0;\n\nwhile ($j != ($n - $k + 1)) {\n if ($j == 0) {\n while ($z != $k) { \n $s[$j+1] = $s[$j+1] + $h[$z+$j+1];\n $z++;\n }\n }else{\n $s[$j+1] = $s[$j] - $h[$j] + $h[$j+$k]; \n }\n $j++;\n}\n\nmy $j = 1;\nmy $index = 1;\nmy $p = $s[1];\nwhile ($j != ($n - $k + 1)) {\n if (($p < $s[$j+1]) || ($p == $s[$j+1])) {\n }else{\n $p = $s[$j+1];\n $index = $j + 1; \n }\n $j++;\n}\n\nprint $index;"}], "negative_code": [{"source_code": "##!/usr/bin/perl\n\n($n, $k) = split ' ', <>;\n@a = split ' ', <>;\n\n$ans = 0;\n$sum = 0;\nfor($i = 0; $i < $k; $i++)\n{\n\t$sum += $a[i];\n}\n$min_sum = $sum;\nfor($i = 1; $i < $n - 1; $i++)\n{\n\t$sum -= $a[$i - 1];\n\t$sum += $a[$i + 1];\n\tif($min_sum > $sum)\n\t{\n\t\t$ans = $i;\n\t\t$min_sum = $sum;\n\t}\n}\nprint $ans;\n\n"}, {"source_code": "##!/usr/bin/perl\n\n($n, $k) = split ' ', <>;\n@a = split ' ', <>;\n\n$ans = 1;\n$sum = 0;\nfor($i = 0; $i < $k; $i++)\n{\n\t$sum += $a[i];\n}\n$min_sum = $sum;\nfor($i = 1; $i < $n - 1; $i++)\n{\n\t$sum -= $a[$i - 1];\n\t$sum += $a[$i + 1];\n\tif($min_sum > $sum)\n\t{\n\t\t$ans = $i + 1;\n\t\t$min_sum = $sum;\n\t}\n}\nprint $ans;\n\n"}, {"source_code": "##!/usr/bin/perl\n\n($n, $k) = split ' ', <>;\n@a = split ' ', <>;\n\n$ans = 1;\n$sum = 0;\nfor($i = 0; $i < $k; $i++)\n{\n $sum += $a[i];\n}\n$min_sum = $sum;\nfor($i = 1; $i < $n - 2; $i++)\n{\n $sum -= $a[$i - 1];\n $sum += $a[$i + 2];\n if($min_sum > $sum)\n {\n $ans = $i + 1;\n $min_sum = $sum;\n }\n}\nprint $ans;\n"}, {"source_code": "##!/usr/bin/perl\n\n($n, $k) = split ' ', <>;\n@a = split ' ', <>;\n\n$ans = 1;\n$sum = 0;\nfor($i = 0; $i < $k; $i++)\n{\n $sum += $a[$i];\n #print \"a[$i] = $a[$i]\\n\";\n}\n#print (\"$sum\\n\");\n$min_sum = $sum;\nfor($i = 1; $i < $n - 2; $i++)\n{\n $sum -= $a[$i - 1];\n $sum += $a[$i + $k - 1];\n #print (\"$sum\\n\");\n if($min_sum > $sum)\n {\n $ans = $i + 1;\n $min_sum = $sum;\n }\n}\nprint $ans;"}, {"source_code": "my $input = ;\nchomp ($input);\nmy @data = split (\" \", $input);\nmy $n = shift (@data);\nmy $k = shift (@data);\n\nmy $input = ;\nchomp ($input);\nmy @data = split (\" \", $input);\n\nmy $i = 1;\nmy @h;\nwhile ($i != $n + 1) {\n $h[$i] = shift (@data);\n $i++;\n}\n\nif ($n == $k) {\n print $h[1];\n exit;\n}\n\nmy @s;\nmy $j = 1;\nwhile ($j != $n - 1) {\n $s[$j] = $h[$j] + $h[$j+1] + $h[$j+2];\n $j++;\n}\n\nmy $j = 1;\nmy $p = $s[$j];\nmy $index = 0;\nwhile ($j != $n - 2) {\n if (($p < $s[$j+1]) || ($p == $s[$j+1])) {\n $x = 0;\n }else{\n $p = $s[$j+1];\n $index = $j + 1; \n }\n $j++;\n}\n\nprint $index;\n"}], "src_uid": "69f4e340b3f6e1d807e0545ebea1fe2f"} {"nl": {"description": "Artem is building a new robot. He has a matrix $$$a$$$ consisting of $$$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_{i,j}$$$ written in it. If two adjacent cells contain the same value, the robot will break. A matrix is called good if no two adjacent cells contain the same value, where two cells are called adjacent if they share a side. Artem wants to increment the values in some cells by one to make $$$a$$$ good.More formally, find a good matrix $$$b$$$ that satisfies the following condition\u00a0\u2014 For all valid ($$$i,j$$$), either $$$b_{i,j} = a_{i,j}$$$ or $$$b_{i,j} = a_{i,j}+1$$$. For the constraints of this problem, it can be shown that such a matrix $$$b$$$ always exists. If there are several such tables, you can output any of them. Please note that you do not have to minimize the number of increments.", "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$$$) \u00a0\u2014 the number of rows and columns, respectively. The following $$$n$$$ lines each contain $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line is $$$a_{i,j}$$$ ($$$1 \\leq a_{i,j} \\leq 10^9$$$).", "output_spec": "For each case, output $$$n$$$ lines each containing $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line is $$$b_{i,j}$$$.", "sample_inputs": ["3\n3 2\n1 2\n4 5\n7 8\n2 2\n1 1\n3 3\n2 2\n1 3\n2 2"], "sample_outputs": ["1 2\n5 6\n7 8\n2 1\n4 3\n2 4\n3 2"], "notes": "NoteIn all the cases, you can verify that no two adjacent cells have the same value and that $$$b$$$ is the same as $$$a$$$ with some values incremented by one. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nwhile( $t -- > 0 ){\n my ($n,$m) = map { $_ - 0 } split(/\\s+/,);\n my @x = (); $#x = $n - 1;\n my @b = (); $#b = $n - 1;\n for(my $i=0;$i<$n;$i++){\n my @d1 = map { $_ - 0 } split(/\\s+/,);\n $x[$i] = \\@d1;\n my @d2 = (); $#d2 = $m - 1;\n $b[$i] = \\@d2;\n }\n for(my $i=0;$i<$n;$i++){\n for(my $j=0;$j<$m;$j++){\n my $v = ( $i + $j ) % 2;\n if( $v ){\n # odd \n $b[$i][$j] = $x[$i][$j] + ( $x[$i][$j] % 2 == 0 ? 1 : 0 );\n } else {\n $b[$i][$j] = $x[$i][$j] + ( $x[$i][$j] % 2 == 0 ? 0 : 1 );\n }\n }\n }\n for(my $i=0;$i<$n;$i++){\n for(my $j=0;$j<$m;$j++){\n print \" \" if $j > 0 ;\n print $b[$i][$j];\n }\n print \"\\n\";\n }\n \n}\n"}], "negative_code": [], "src_uid": "fd11967b07870be3294b9191603b17e8"} {"nl": {"description": "Given an array a1,\u2009a2,\u2009...,\u2009an of n integers, find the largest number in the array that is not a perfect square.A number x is said to be a perfect square if there exists an integer y such that x\u2009=\u2009y2.", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000)\u00a0\u2014 the number of elements in the array. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009106\u2009\u2264\u2009ai\u2009\u2264\u2009106)\u00a0\u2014 the elements of the array. It is guaranteed that at least one element of the array is not a perfect square.", "output_spec": "Print the largest number in the array which is not a perfect square. It is guaranteed that an answer always exists.", "sample_inputs": ["2\n4 2", "8\n1 2 4 8 16 32 64 576"], "sample_outputs": ["2", "32"], "notes": "NoteIn the first sample case, 4 is a perfect square, so the largest number in the array that is not a perfect square is 2."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tprint +( grep { /-/ || ( sqrt ) =~ /\\./ } sort { $b <=> $a } split ' ', <> )[ 0 ]\n\t}"}, {"source_code": "<>;\n\nprint +( grep { /-/ || ( sqrt ) =~ /\\./ } sort { $b <=> $a } split ' ', <> )[ 0 ]"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tprint +( grep { ( sqrt ) =~ /\\./ } grep !/-/, sort { $b <=> $a } split ' ', <> )[ 0 ]\n\t}"}], "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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 number of bidders. The second line contains n distinct integer numbers p1,\u2009p2,\u2009... pn, separated by single spaces (1\u2009\u2264\u2009pi\u2009\u2264\u200910000), 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": "<>;\n$_=<>;\n$x=1;\ns/\\d+/(++$j and $&>$x and $y=$x and $x=$& and $i=$j) or ($&>$y and $y=$&)/eg;\nprint \"$i $y\";"}], "negative_code": [{"source_code": "<>;\n$_=<>;\n$x=1;\ns/\\d+/(++$j and $&>$x and $y=$x and $x=$& and $i=$j),($&>$y and $y=$&)/eg;\nprint \"$y $i\";"}], "src_uid": "1d4aaf15e5c6fcde50515880aae74720"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009m\u2009\u2264\u20091000) \u2014 the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters \u2014 string s. The third line contains m lowercase English letters \u2014 string t.", "output_spec": "In the first line print single integer k \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 1;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\tmy $s = <>;\n\tmy $t = <>;\n\tchomp $s;\n\tchomp $t;\n\t\n\tmy @C;\n\t\n\twhile( $t =~ /(?=(.{$n}))/g ){\n\t\tmy $T = $1;\n\t\tmy $S = $s;\n\t\tmy @c;\n\t\tfor( my $i = 0; $i < $n; $i ++ ){\n\t\t\t$T =~ s/.//;\n\t\t\tmy $cT = $&;\n\t\t\t$S =~ s/.//;\n\t\t\tmy $cS = $&;\n\t\t\t$cS eq $cT or push @c, $i + 1;\n\t\t}\n\t\tpush @C, @c . \"\\n\" . join ' ', @c;\n\t\t}\n\t\n\tmy $min = ( sort { $a <=> $b } @C )[ 0 ];\n\tprint $min;\n\t\t\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 1;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\tmy $s = <>;\n\tmy $t = <>;\n\tchomp $s;\n\tchomp $t;\n\t\n\tmy @C;\n\t\n\twhile( $t =~ /(?=(.{$n}))/g ){\n\t\tmy $T = $1;\n\t\tmy $S = $s;\n\t\tmy @c;\n\t\tfor( my $i = 0; $i < $n; $i ++ ){\n\t\t\t$T =~ s/.//;\n\t\t\tmy $cT = $&;\n\t\t\t$S =~ s/.//;\n\t\t\tmy $cS = $&;\n\t\t\t$cS eq $cT or push @c, $i + 1;\n\t\t}\n\t\tpush @C, @c . \"\\n\" . join ' ', @c;\n\t\t}\n\t\n\tmy $min = ( sort { $a <=> $b } @C )[ 0 ];\n\tprint $min;\n\t\t\n\t}"}], "negative_code": [], "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\u2009<\u2009ai). 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\u2009\u2264\u2009n\u2009\u2264\u20095000) \u2014 the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1\u2009\u2264\u2009bi\u2009<\u2009ai\u2009\u2264\u2009109) \u2014 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 \u2014 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": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\npush @a, [split / /, <>] foreach (0 .. $n-1);\n@a = sort { $a->[0]==$b->[0] and return $a->[1]<=>$b->[1] or return $a->[0]<=>$b->[0]} @a;\n$ans = -1;\nforeach (0 .. $n-1) {\n if ($ans <= $a[$_][1]) {\n $ans = $a[$_][1];\n } else {\n $ans = $a[$_][0];\n }\n}\nsay $ans;"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\npush @a, [split / /, <>] foreach (0 .. $n-1);\n@a = sort { $a->[0]==$b->[0] and return $a->[0]<=>$b->[0] or return $a->[1]<=>$b->[1]} @a;\n$ans = -1;\nforeach (0 .. $n-1) {\n\tif ($ans <= $a[$_][1]) {\n\t\t$ans = $a[$_][1];\n\t} else {\n\t\t$ans = $a[$_][0];\n\t}\n}\nsay $ans;"}], "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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 the vertex $$$2$$$ on the second chain will break simplicity."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @C = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 1 } split(/\\s+/,);\r\n my @B = map { $_ - 1 } split(/\\s+/,);\r\n my $res = -1;\r\n my $p = -1;\r\n for(my $i=$n-1;$i>=0;$i--){\r\n if( $p > 0 ){\r\n my $v1 = $p + 2 + abs($A[$i+1] - $B[$i+1]);\r\n $res = $v1 if $v1 > $res;\r\n last if $i == 0;\r\n if( $A[$i+1] == $B[$i+1] ){\r\n $p = -1;\r\n } elsif( $A[$i+1] > $B[$i+1] ){\r\n $p += 2 + ( ($C[$i]-1) - $A[$i+1] ) + ( $B[$i+1] - 0 );\r\n } else {\r\n $p += 2 + ( ($C[$i]-1) - $B[$i+1] ) + ( $A[$i+1] - 0 );\r\n }\r\n $p = ( $C[$i] - 1 ) if( ( $C[$i] - 1 ) > $p );\r\n }\r\n if( $p == -1 ){\r\n $p = ($C[$i] - 1);\r\n }\r\n }\r\n print \"$res\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\n\r\n#!/usr/bin/perl\r\nmy ($n,$m) = map {$_ - 0} split(/\\s+/o,);\r\n\r\nmy @tr = ();\r\n$#tr = $n-1;\r\nmy @par = ();\r\n$#par = $n-1;\r\n\r\nfor(my $i=0;$i<$n;$i++){\r\n $par[$i] = $i;\r\n}\r\n\r\nfor(my $i=0;$i<$m;$i++){\r\n my ($x,$y,$z) = map {$_ - 0} split(/\\s+/o,);\r\n unite($x-1,$y-1);\r\n}\r\n\r\nmy %dum = ();\r\nfor(my $i=0;$i<$n;$i++){\r\n $dum{&root($i)} = 1;\r\n}\r\nmy @d2 = keys %dum;\r\nmy $res = 1 + $#d2;\r\nprint \"$res\\n\";\r\n\r\nexit(0);\r\n\r\n\r\n###\r\n\r\n#!/usr/bin/perl\r\nuse Data::Dumper;\r\nmy ($n) = map {$_ - 0} split(/\\s+/o,);\r\n\r\nmy $f = &factr($n);\r\n\r\nmy @facts = keys %$f;\r\n\r\nmy %f0 = ();\r\n\r\nmy %oki = ();\r\n\r\nwhile(1){\r\n my $gosei = 1;\r\n foreach my $f1 (@facts){\r\n $gosei *= ( $f1 ** ( $f0{$f1} - 0 ) );\r\n }\r\n if( $gosei >= 3 ){\r\n# print \"gosei = $gosei\\n\";\r\n my $syo = $n / $gosei;\r\n if( $syo <= $gosei - 2 ){\r\n my $g1 = $gosei - 1;\r\n $oki{$g1} = 1;\r\n }\r\n }\r\n my $cu = 0;\r\n my $carry = 1;\r\n while($carry > 0){\r\n $f0{$facts[$cu]} += 1;\r\n $carry = 0;\r\n if( $f0{$facts[$cu]} > $f->{$facts[$cu]} ){\r\n $f0{$facts[$cu]} = 0;\r\n $carry = 1;\r\n $cu++;\r\n }\r\n last if( $cu > $#facts );\r\n }\r\n last if $cu > $#facts;\r\n}\r\nmy $res = 0;\r\nforeach my $o1 (keys %oki){\r\n $res += ( $o1 - 0);\r\n}\r\n#print &Dumper(\\%oki);\r\nprint \"$res\\n\";\r\nexit(0);\r\n\r\nsub factr {\r\n my $v = shift;\r\n my %f = ();\r\n if( $v<4 ){\r\n $f{$v}++;\r\n return \\%f;\r\n }\r\n for(my $i=1;$i*$i<=$v;$i+=2){\r\n my $ii = ($i == 1 ? 2 : $i);\r\n while($v % $ii == 0){\r\n $f{$ii}++;\r\n $v /= $ii;\r\n }\r\n }\r\n $f{$v}++ if $v>1;\r\n return \\%f;\r\n}\r\n\r\n#### mod_int + nCr nPr set\r\n\r\n#!/usr/bin/perl\r\n\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($n,$k) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\n&mk_fact($n+10);\r\n\r\n### write here \r\n\r\nexit(0);\r\n\r\n\r\nsub mod_pow { # ( x ** n ) % mod\r\n my ($x,$n) = @_;\r\n my $r = 1;\r\n while( $n ){\r\n $r = ( $r * $x ) % $mod if 1 & $n;\r\n $n >>= 1;\r\n $x = ( $x * $x ) % $mod;\r\n }\r\n return $r;\r\n}\r\n\r\nsub mk_fact { # make {global} n-size variable fact[] factinv[]\r\n my $n_max = shift;\r\n our @fact = (1); $#fact = $n_max;\r\n our @factinv = (1); $#factinv = $n_max;\r\n for(my $i=1;$i<=$n_max;$i++){\r\n $fact[$i] = ( $fact[$i-1] * $i ) % $mod;\r\n $factinv[$i] = &mod_pow($fact[$i],$mod-2);\r\n }\r\n}\r\n\r\nsub nCr { # calc nCr % mod\r\n my $n = shift; my $r = shift;\r\n return (( $fact[$n] * ( ( $factinv[$r] * $factinv[$n-$r] ) % $mod ) ) % $mod);\r\n}\r\n\r\nsub nPr { # calc nPr % mod\r\n my $n = shift; my $r = shift;\r\n return (( $fact[$n] * $factinv[$n-$r] ) % $mod );\r\n}\r\n\r\n#### union find set\r\n\r\n#!/usr/bin/perl\r\n\r\nmy ($n) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\n#### write here\r\n\r\nexit(0);\r\n\r\nsub mk_uf {\r\n my $n = shift;\r\n our @uf = (); $#uf = $n - 1;\r\n for(my $i=0;$i<$n;$i++){\r\n $uf[$i] = $i;\r\n }\r\n}\r\n\r\nsub root {\r\n my $x = shift;\r\n while( $uf[$x] != $x ){ $x = $uf[$x] = $uf[$uf[$x]]; }\r\n return $x;\r\n}\r\n\r\nsub unite {\r\n my $x = &root(scalar(shift)); my $y = &root(scalar(shift));\r\n $uf[$y] = $x;\r\n}\r\n\r\n\r\n\r\n"}], "negative_code": [], "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 \u200b\u200bm pieces of paper in the garland. Calculate what the maximum total area of \u200b\u200bthe pieces of paper in the garland Vasya can get.", "input_spec": "The first line contains a non-empty sequence of n (1\u2009\u2264\u2009n\u2009\u2264\u20091000) 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\u2009\u2264\u2009m\u2009\u2264\u20091000) 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 \u2014 he doesn't have a sheet of color z."}, "positive_code": [{"source_code": "sub n{print \"\\n\"}\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t\n\t$a=$_;\n\t$b=<>;\n\tchomp $a;\n\tchomp $b;\n\t#a..z\n\t#count a string 1\n\t#count a string 2\n\t#cnt1 <=> cnt 2\n\t\n\t$ans=0;\n\t$f=0;\n\tfor $i('a'..'z'){\n\t\t\n\t\t$cnt1 = count($i,(split//,$a));\n\t\t$cnt2 = count($i,(split//,$b));\n\t\tif (!$cnt1 and $cnt2){$f++}\n\t\t\n\t\tif ($cnt1 < $cnt2){ $ans += $cnt1}\n\t\telse { $ans += $cnt2 }\n\t\t\n\t}\n\tif ($f){print -1;next}\n\tprint $ans? $ans : -1 ;\n\tn();\n\t\n\t}\n\t\n"}, {"source_code": "($a,$b)=<>;\nfor $i('a'..'z'){\n\t$c=()=$a=~/$i/g;\n\t$d=()=$b=~/$i/g;\n\t$f=1 if !$c && $d;\n\t$A += $c < $d ? $c : $d \n}\nprint $A*(1-$f)? $A : -1"}, {"source_code": "#Scott Heinrich - A01273823\nuse strict;\nuse warnings;\nuse 5.010;\n\nmy $count = 0;\nmy $line1 = <>;\nchomp($line1);\nmy $line2 = <>;\nchomp($line2);\nmy @n = split(\"\",$line1);\nmy @m = split(\"\",$line2);\nmy %nhash;\nmy %mhash;\nmy $pass = 1;\n\n#make both hashes, key is character in @n,@m\nforeach (@n)\n{\n $nhash{$_}++;\n}\nforeach (@m)\n{\n $mhash{$_}++;\n}\n\nforeach my $key (keys %mhash)\n{\n if(!$nhash{$key})\n {\n $pass = 0;\n last;\n }\n if($nhash{$key} > $mhash{$key})\n {\n $count += $mhash{$key}\n }\n else\n {\n $count += $nhash{$key}\n }\n}\n\nif ($pass)\n{\n say $count;\n}\nelse\n{\n say \"-1\";\n}\n"}, {"source_code": "#!perl\n\nchomp ($string1 =<>);\nchomp ($string2 =<>);\n\n@array1 = split('',$string1);\n@array2 = split('',$string2);\n\n%hash1;\n%hash2;\nfor $char (@array1)\n{\n($hash1{$char})++;\n}\n\nfor $char (@array2)\n{\n($hash2{$char})++;\n}\n\n$sum;\nfor (keys(%hash2))\n{\n if($hash1{$_} == 0) {$fail = 1;}\n $sum += min($hash1{$_},$hash2{$_});\n}\n\nif (($sum == 0)or $fail)\n{\n print (-1);\n}\nelse\n{\nprint $sum;\n}\n<>;\nsub min ($$)\n{\n if($_[0] < $_[1])\n {\n return $_[0];\n }\n else\n {\n return $_ [1];\n }\n}"}], "negative_code": [{"source_code": "sub n{print \"\\n\"}\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t\n\t$a=$_;\n\t$b=<>;\n\t#a..z\n\t#count a string 1\n\t#count a string 2\n\t#cnt1 <=> cnt 2\n\t\n\t$ans=0;\n\tfor $i('a'..'z'){\n\t\t\n\t\t$cnt1 = count($i,(split//,$a));\n\t\t$cnt2 = count($i,(split//,$b));\n\t\t\n\t\tif ($cnt1 < $cnt2){ $ans += $cnt1}\n\t\telse { $ans += $cnt2 }\n\t\t\n\t}\n\t\n\tprint $ans? $ans : -1 ;\n\tn();\n\t\n\t}\n\t\n"}, {"source_code": "sub n{print \"\\n\"}\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t\n\t$a=$_;\n\t$b=<>;\n\tchomp $a;\n\tchomp $b;\n\t#a..z\n\t#count a string 1\n\t#count a string 2\n\t#cnt1 <=> cnt 2\n\t\n\t$ans=0;\n\tfor $i('a'..'z'){\n\t\t\n\t\t$cnt1 = count($i,(split//,$a));\n\t\t$cnt2 = count($i,(split//,$b));\n\t\t\n\t\tif ($cnt1 < $cnt2){ $ans += $cnt1}\n\t\telse { $ans += $cnt2 }\n\t\t\n\t}\n\t\n\tprint $ans? $ans : -1 ;\n\tn();\n\t\n\t}\n\t\n"}, {"source_code": "#Scott Heinrich - A01273823\nuse strict;\nuse warnings;\nuse 5.010;\n\nmy $count = 0;\nmy $line1 = <>;\nchomp($line1);\nmy $line2 = <>;\nchomp($line2);\nmy @n = split(\"\",$line1);\nmy @m = split(\"\",$line2);\nmy %nhash;\nmy %mhash;\n\n#make both hashes, key is character in @n,@m\nforeach (@n)\n{\n $nhash{$_}++;\n}\nforeach (@m)\n{\n $mhash{$_}++;\n}\n\nforeach my $key (keys %mhash)\n{\n if(!$nhash{$key})\n {\n say \"-1\";\n last;\n }\n if($nhash{$key} > $mhash{$key})\n {\n $count += $mhash{$key}\n }\n else\n {\n $count += $nhash{$key}\n }\n}\n\nif ($count)\n{\n say $count;\n}\n"}, {"source_code": "#Scott Heinrich - A01273823\nuse strict;\nuse warnings;\nuse 5.010;\n\nmy $count = 0;\nmy $line1 = <>;\nchomp($line1);\nmy $line2 = <>;\nchomp($line2);\nmy @n = split(\"\",$line1);\nmy @m = split(\"\",$line2);\nmy %nhash;\nmy %mhash;\n\n#make both hashes, key is character in @n,@m\nforeach (@n)\n{\n $nhash{$_}++;\n}\nforeach (@m)\n{\n $mhash{$_}++;\n}\n\nforeach my $key (keys %mhash)\n{\n if(!$nhash{$key})\n {\n say \"-1\";\n last;\n }\n if($nhash{$key} > $mhash{$key})\n {\n $count += $mhash{$key}\n }\n else\n {\n $count += $nhash{$key}\n }\n}\n\nif ($count != 0)\n{\n say $count;\n}\n"}, {"source_code": "#!perl\n\nchomp ($string1 =<>);\nchomp ($string2 =<>);\n\n@array1 = split('',$string1);\n@array2 = split('',$string2);\n\n%hash1;\n%hash2;\nfor $char (@array1)\n{\n($hash1{$char})++;\n}\n\nfor $char (@array2)\n{\n($hash2{$char})++;\n}\n\n$sum;\nfor (keys(%hash2))\n{\n \n $sum += min($hash1{$_},$hash2{$_});\n}\n\nif ($sum == 0)\n{\n print (-1);\n}\nelse\n{\nprint $sum;\n}\n<>;\nsub min ($$)\n{\n if($_[0] < $_[1])\n {\n return $_[0];\n }\n else\n {\n return $_ [1];\n }\n}"}, {"source_code": "for $i('a'..'z'){\n\t$c=()=<>=~/$i/g;\n\t$d=()=<>=~/$i/g;\n\t$f=1 if !$c && $d;\n\t$A += $c < $d ? $c : $d \n}\nprint $A*(1-$f)? $A : -1"}], "src_uid": "b1e09df7c47dbd04992e64826337c28a"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009200) \u2014 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 \u2014 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": " #!perl\n \n $n = <>;\n \n $hash { 'polycarp' } = 1; \n for(my $i = 0; $i < $n; $i++)\n {\n my $line = <>;\n chomp $line;\n my @names = split / reposted /, $line; \n $hash { lc @names[0] } = 1 + $hash { lc @names[1] };\n}\n \n $max= 0;\n for my $length (values %hash)\n {\n if($length > $max)\n {\n $max = $length;\n }\n }\n \n print $max; "}], "negative_code": [], "src_uid": "16d4035b138137bbad247ccd5e560051"} {"nl": {"description": "A permutation is a sequence of integers p1,\u2009p2,\u2009...,\u2009pn, 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,\u2009p2,\u2009...,\u2009pn.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\u2009\u2264\u2009i\u2009\u2264\u2009n) (n is the permutation size) the following equations hold ppi\u2009=\u2009i and pi\u2009\u2260\u2009i. 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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 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,\u2009p2,\u2009...,\u2009pn \u2014 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": "use strict;\n\nchomp (my $n = <>);\n\n((print \"-1\\n\") and exit) if ($n % 2);\n\nfor (my $i=1; $i<=$n; $i+=2){\n\tprint $i+1 . \" \" . $i . \" \"; \n}\nprint \"\\n\";"}, {"source_code": "use v5.10;\n\nchomp($n = <>);\n$n&1 and say -1 and exit;\nfor ($i=1,$j=2; $i<=$n; $i+=2,$j+=2) {\n\tprint \"$j $i \";\n}\nsay;"}], "negative_code": [], "src_uid": "204ba74195a384c59fb1357bdd71e16c"} {"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\u2009\u2264\u2009i\u2009\u2264\u2009|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\u2009\u2264\u2009|s|\u2009\u2264\u2009105). 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 \u2009-\u20091. 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": "$\\ = $/;\n$, = $/;\n\nwhile (<>){\n\tchomp;\n\t@ans = ();\n\t$f = 0;\n\t\n\t$ri = rindex $_, '#'; $h = y/#/)/;\n\t\n\t$op = () = /\\(/g;\n\t$cl = () = /\\)/g;\n\t$op >= $cl or ++$f;\n\t\n\tpush @ans, (1) x $h;\n\t$ans[ -1 ] = $op - $cl + 1;\n\n\tsubstr $_, $ri, 1, ')' x ($op - $cl + 1);\n\t$acc = 0;\n\t\n\twhile ( / \\G ( [(]++ ) ( [)]++ ) (?= [(]|$ ) /gx ){\n\t\t$pos = pos;\n\t\t$open = length $1;\n\t\t$close = length $2;\n\t\t$acc += $open - $close;\n\t\t$acc < 0 and ++$f and last;\n\t\t}\n\t\n\t$pos == length or ++$f;\n\tprint $f ? -1 : @ans;\n\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\n$, = $/;\n\nwhile (<>){\n\tchomp;\n\t@ans = ();\n\t$f = 0;\n\t\n\t$ri = rindex $_, '#';\n\tsubstr $_, $ri, 1, q(@);\n\t\n\t$h = () = /#/g;\n\ts/#/)/g;\n\t\n\t$op = () = /\\(/g;\n\t$cl = () = /\\)/g;\n\t$op > $cl + 1 or ++$f;\n\t\n\tsubstr $_, $ri, 1, ')' x ($op - $cl);\n\t\n\tpush @ans, (1) x $h;\n\tpush @ans, $op - $cl;\n\t$acc = 0;\n\t\n\twhile ( / \\G ( [(]++ ) ( [)]++ ) (?= [(]|$ ) /gx ){\n\t\t$open = length $1;\n\t\t$close = length $2;\n\t\t$acc += $open - $close;\n\t\t$acc < 0 and ++$f and last;\n\t\t}\n\t\n\tprint $f ? -1 : @ans;\n\n\t}"}, {"source_code": "$\\ = $/;\n$, = $/;\n\nwhile (<>){\n\tchomp;\n\t@ans = ();\n\t$f = 0;\n\t\n\t$ri = rindex $_, '#'; $h = y/#/)/;\n\t\n\t$op = () = /\\(/g;\n\t$cl = () = /\\)/g;\n\t$op >= $cl or ++$f;\n\t\n\tpush @ans, (1) x $h;\n\t$ans[ -1 ] = $op - $cl + 1;\n\n\tsubstr $_, $ri, 1, ')' x ($op - $cl + 1);\n\t$acc = 0;\n\t\n\twhile ( / \\G ( [(]++ ) ( [)]++ ) (?= [(]|$ ) /gx ){\n\t\t$pos = pos;\n\t\t$open = length $1;\n\t\t$close = length $2;\n\t\t$acc += $open - $close;\n\t\t$acc < 0 and ++$f and last;\n\t\t}\n\t\n\tprint $f ? -1 : @ans;\n\n\t}"}, {"source_code": "$\\ = $/;\n$, = $/;\n\n\t$ri = rindex $_, '#'; \n\t$h = y/#/)/;\n\t\n\t$op = () = /\\(/g;\n\t$cl = () = /\\)/g;\n\t\n\t@ans = ( (1) x ($h - 1) , $op - $cl + 1 );\n\n\tsubstr $_, $ri, 1, ')' x ($op - $cl + 1);\n\t\n\twhile ( / \\G ( \\(+ ) ( \\)++ ) /gx ){\n\t\t$acc += length $1;\n\t\t$acc -= length $2;\n\t\t$acc < 0 && ++$f\n\t\t}\n\t\n\tpos == -1 + length or ++$f;\n\tprint $f ? -1 : @ans\n"}, {"source_code": "$\\ = $/;\n$, = $/;\n\nwhile (<>){\n\tchomp;\n\t@ans = ();\n\t$f = 0;\n\t\n\t$h = y/#/)/;\n\t\n\t$ri = rindex $_, ')';\n\t\n\t$op = () = /\\(/g;\n\t$cl = () = /\\)/g;\n\t$op >= $cl or ++$f;\n\t\n\tpush @ans, (1) x $h;\n\t$ans[ -1 ] = $op - $cl + 1;\n\n\tsubstr $_, $ri, 0, ')' x ($op - $cl);\n\t$acc = 0;\n\t\n\twhile ( / \\G ( [(]++ ) ( [)]++ ) (?= [(]|$ ) /gx ){\n\t\t$pos = pos;\n\t\t$open = length $1;\n\t\t$close = length $2;\n\t\t$acc += $open - $close;\n\t\t$acc < 0 and ++$f and last;\n\t\t}\n\t\n\tprint $f ? -1 : @ans;\n\n\t}"}, {"source_code": "$\\ = $/;\n$, = $/;\n\n\t$_ = <>;\n\t$ri = rindex $_, '#'; \n\t$h = y/#/)/;\n\t\n\t$op = () = /\\(/g;\n\t$cl = () = /\\)/g;\n\t\n\t@ans = ( (1) x ($h - 1) , $op - $cl + 1 );\n\n\tsubstr $_, $ri, 1, ')' x ($op - $cl + 1);\n\t\n\twhile ( / \\G ( \\(+ ) ( \\)++ ) /gx ){\n\t\t$acc += length $1;\n\t\t$acc -= length $2;\n\t\t$acc < 0 && ++$f\n\t\t}\n\t\n\tpos == -1 + length or ++$f;\n\tprint $f ? -1 : @ans\n"}, {"source_code": "$\\ = $/;\n$, = $/;\n\nwhile (<>){\n\tchomp;\n\t@ans = ();\n\t$f = 0;\n\tundef $pos;\n\t\n\twhile ( / \\G ( [(]++ ) ( [#)]++ ) (?= [(]|$ ) /gx ){\n\t\t$pos = pos;\n\t\t$open = length $1;\n\t\t$c = length $2;\n\t\t$close = () = $2 =~ /\\)/g;\n\t\t$h = $c - $close;\n\t\t$diff = $open - $close;\n\t\t$diff < $h and ++$f and last;\n\t\t$h or next;\n\t\t$diff -= ($h - 1);\n\t\tpush @ans, (1) x ($h - 1), $diff;\n\t\t}\n\t\n\t$pos == length or ++$f;\n\tprint $f ? -1 : @ans;\n\t\n\t}"}, {"source_code": "$\\ = $/;\n$, = $/;\n\nwhile (<>){\n\tchomp;\n\t@ans = ();\n\t$f = 0;\n\t\n\t$ri = rindex $_, '#'; $h = y/#/)/;\n\t\n\t$op = () = /\\(/g;\n\t$cl = () = /\\)/g;\n\t$op >= $cl or ++$f;\n\t\n\tpush @ans, (1) x $h;\n\t$ans[ -1 ] = $op - $cl + 1;\n\n\tsubstr $_, $ri, 1, ')' x ($op - $cl + 1);\n\t$acc = 0;\n\t\n\twhile ( / \\G ( [(]++ ) ( [)]++ ) (?= [(]|$ ) /gx ){\n\t\t$pos = pos;\n\t\t$open = length $1;\n\t\t$close = length $2;\n\t\t$acc += $open - $close;\n\t\t$acc < 0 and ++$f and last;\n\t\t}\n\t\n\tchop eq q[(] && ++$f;\n\tprint $f ? -1 : @ans;\n\n\t}"}, {"source_code": "$\\ = $/;\n$, = $/;\n\nwhile (<>){\n\tchomp;\n\t@ans = ();\n\t$f = 0;\n\t\n\t$ri = rindex $_, '#';\n\tsubstr $_, $ri, 1, q(@);\n\t\n\t$h = () = /#/g;\n\ts/#/)/g;\n\t\n\t$op = () = /\\(/g;\n\t$cl = () = /\\)/g;\n\t$op >= $cl + 1 or ++$f;\n\t\n\tsubstr $_, $ri, 1, ')' x ($op - $cl);\n\t\n\tpush @ans, (1) x $h;\n\tpush @ans, $op - $cl;\n\t$acc = 0;\n\t\n\twhile ( / \\G ( [(]++ ) ( [)]++ ) (?= [(]|$ ) /gx ){\n\t\t$open = length $1;\n\t\t$close = length $2;\n\t\t$acc += $open - $close;\n\t\t$acc < 0 and ++$f and last;\n\t\t}\n\t\n\tprint $f ? -1 : @ans;\n\n\t}"}, {"source_code": "$\\ = $/;\n$, = $/;\n\nwhile (<>){\n\tchomp;\n\t@ans = ();\n\t$f = 0;\n\t\n\t$h = y/#/)/;\n\t\n\t$ri = rindex $_, ')';\n\t\n\t$op = () = /\\(/g;\n\t$cl = () = /\\)/g;\n\t$op >= $cl or ++$f;\n\t\n\tpush @ans, (1) x $h;\n\t$ans[ -1 ] = $op - $cl + 1;\n\n\tsubstr $_, $ri, 0, ')' x ($op - $cl);\n\t$acc = 0;\n\t\n\twhile ( / \\G ( [(]++ ) ( [)]++ ) (?= [(]|$ ) /gx ){\n\t\t$pos = pos;\n\t\t$open = length $1;\n\t\t$close = length $2;\n\t\t$acc += $open - $close;\n\t\t$acc < 0 and ++$f and last;\n\t\t}\n\t\n\t$pos == length or ++$f;\n\tprint $f ? -1 : @ans;\n\n\t}"}, {"source_code": "$\\ = $/;\n$, = $/;\n\nwhile (<>){\n\tchomp;\n\t@ans = ();\n\t$f = 0;\n\t\n\t$ri = rindex $_, '#';\n\tsubstr $_, $ri, 1, q(@);\n\ts/#/)/g;\n\n\t$op = () = /[(]/g;\n\t$cl = () = /[)]/g;\n\t$op > $cl + 1 or ++$f;\n\t\n\tsubstr $_, $ri, 1, ')' x ($op - $cl);\n\t\n\tpush @ans, (1) x $cl;\n\tpush @ans, $op - $cl;\n\t$acc = 0;\n\t\n\twhile ( / \\G ( [(]++ ) ( [)]++ ) (?= [(]|$ ) /gx ){\n\t\t$open = length $1;\n\t\t$close = length $2;\n\t\t$acc += $open - $close;\n\t\t$acc < 0 and ++$f and last;\n\t\t}\n\t\n\tprint $f ? -1 : @ans;\n\n\t}"}, {"source_code": "$\\ = $/;\n$, = $/;\n\nwhile (<>){\n\tchomp;\n\t@ans = ();\n\t$f = 0;\n\tundef $pos;\n\t\n\twhile ( / \\G ( [(]++ ) ( [#)]++ ) (?= [(]|$ ) /gx ){\n\t\t$pos = pos;\n\t\t$open = length $1;\n\t\t$c = length $2;\n\t\t$close = () = $2 =~ /\\)/g;\n\t\t$h = $c - $close;\n\t\t$diff = $open - $close;\n\t\t$diff < $h and ++$f and last;\n\t\t$h or next;\n\t\t$diff -= ($h - 1);\n\t\tpush @ans, (1) x ($h - 1), $diff;\n\t\t}\n\t\n\t$pos == length or ++$f;\n\tprint $f ? -1 : @ans ? @ans : 0 ;\n\t\n\t}"}], "src_uid": "0a30830361b26838b192d7de1efcdd2f"} {"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$$$ \u2014 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$$$) \u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n my ($l,$r,$a) = map { $_ - 0 } split(/\\s+/,);\r\n my $v1 = $r / $a;\r\n my $mx = -1;\r\n if( $v1 > 0 ){\r\n my $x = $r - ($r % $a) - 1;\r\n $mx = ( $x / $a ) + ( $x % $a ) if $x >= $l ;\r\n }\r\n my $v2 = $v1 + ($r % $a);\r\n $mx = $v2 if $v2 > $mx;\r\n print \"$mx\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n my ($l,$r,$a) = map { $_ - 0 } split(/\\s+/,);\r\n my $v1 = $r / $a;\r\n my $mx = -1;\r\n if( $v1 > 0 ){\r\n my $x = $l - ($l % $a) - 1;\r\n $mx = ( $x / $a ) + ( $x % $a ) if $x >= $l ;\r\n }\r\n my $v2 = $v1 + ($r % $a);\r\n $mx = $v2 if $v2 > $mx;\r\n print \"$mx\\n\";\r\n}\r\n\r\nexit(0);\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$$$) \u2014 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 \u2014 $$$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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n $A[2] = $A[6] - $A[0] - $A[1];\r\n print ( join(' ',@A[0..2]) . \"\\n\" );\r\n}\r\n\r\nexit(0);\r\n\r\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split;\n\t\n\tif( $_[ 0 ] + $_[ 1 ] == $_[ 2 ] ){\n\t\t$_[ 2 ] = $_[ 3 ];\n\t\t}\n\t\n\tprint \"@_[ 0 .. 2 ]\";\n\t}"}], "negative_code": [], "src_uid": "e0ec0cd81d2ec632ef89d207d80fa8a3"} {"nl": {"description": "Valera has array a, consisting of n integers a0,\u2009a1,\u2009...,\u2009an\u2009-\u20091, and function f(x), taking an integer from 0 to 2n\u2009-\u20091 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\u2009=\u20094 and x\u2009=\u200911 (11\u2009=\u200920\u2009+\u200921\u2009+\u200923), then f(x)\u2009=\u2009a0\u2009+\u2009a1\u2009+\u2009a3.Help Valera find the maximum of function f(x) among all x, for which an inequality holds: 0\u2009\u2264\u2009x\u2009\u2264\u2009m.", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of array elements. The next line contains n space-separated integers a0,\u2009a1,\u2009...,\u2009an\u2009-\u20091 (0\u2009\u2264\u2009ai\u2009\u2264\u2009104) \u2014 elements of array a. The third line contains a sequence of digits zero and one without spaces s0s1... sn\u2009-\u20091 \u2014 the binary representation of number m. Number m equals .", "output_spec": "Print a single integer \u2014 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\u2009=\u200920\u2009=\u20091,\u2009f(0)\u2009=\u20090,\u2009f(1)\u2009=\u2009a0\u2009=\u20093.In the second sample m\u2009=\u200920\u2009+\u200921\u2009+\u200923\u2009=\u200911, the maximum value of function equals f(5)\u2009=\u2009a0\u2009+\u2009a2\u2009=\u200917\u2009+\u200910\u2009=\u200927."}, "positive_code": [{"source_code": "use strict;\n\nuse List::Util qw(max);\n\nsub chkmax (\\$$) {\n my ($a, $b) = @_;\n $$a = max($$a, $b);\n}\n\nchomp(my $n = <>);\nchomp($_ = <>);\nmy @a = reverse split;\nchomp($_ = <>);\nmy @s = reverse split //;\n\nmy @dp = ();\n$#dp = $n;\n$dp[0] = [0, -1e100];\nfor my $i (0 .. $#s) {\n $dp[$i + 1] = [-1e100, -1e100];\n\n if ($s[$i] == 0) {\n chkmax $dp[$i + 1][0], $dp[$i][0];\n chkmax $dp[$i + 1][1], $dp[$i][1] + $a[$i];\n } else {\n chkmax $dp[$i + 1][0], $dp[$i][0] + $a[$i];\n chkmax $dp[$i + 1][1], $dp[$i][0];\n chkmax $dp[$i + 1][1], $dp[$i][1] + $a[$i];\n }\n}\n\nmy $res = max @{$dp[$n]};\nprint $res;\n"}], "negative_code": [], "src_uid": "9366e1626b33b4f4e49cf35200c0448f"} {"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$$$)\u00a0\u2014 the number of testcases. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 50$$$)\u00a0\u2014 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\u00a0\u2014 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": "for(1..<>){$n=<>;$_=<>;$_=~s/(.)(?=.*?\\1)//g;print$n-1+length,\" \"}"}, {"source_code": "for(1..<>){$n=<>;$_=<>;$_=~s/(.)(?=.*?\\1)//g;print$n-1+length,\" \"}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, m/./g;\n\t\n\tprint 0 + ( length ) + keys %h;\n\t}"}], "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$$$) \u2014 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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\tchomp;\n\tmy $n = $_;\n\t\n\tmy @A;\n\t\n\tfor my $i ( 0 .. $n - 1 ){\n\t\tfor my $j ( 0 .. $n - 1 ){\n\t\t\tmy $switch = $i <=> $j;\n\t\t\tif( $n % 2 == 0 and $i + $j > $n - 1 ){\n\t\t\t\t$switch *= -1;\n\t\t\t\t}\n\t\t\t\n\t\t\tif( $i == $j ){\n\t\t\t\t$A[ $i ][ $j ] = 'X';\n\t\t\t\t}\n\t\t\telsif( $n % 2 == 0 and $i + $j == $n - 1 ){\n\t\t\t\t$A[ $i ][ $j ] = 0;\n\t\t\t\t}\n\t\t\telsif( ( $i + $j ) % 2 == 1 ){\n\t\t\t\t$A[ $i ][ $j ] = 0 + $switch;\n\t\t\t\t}\n\t\t\telsif( ( $i + $j ) % 2 == 0 ){\n\t\t\t\t$A[ $i ][ $j ] = 0 - $switch;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print map { sprintf \"%3s\", $_ } @{ $_ } for @A;\n\t\n\tmy @ans;\n\t\n\tfor my $i ( 0 .. $n - 1 ){\n\t\tfor my $j ( $i + 1 .. $n - 1 ){\n\t\t\tpush @ans, $A[ $i ][ $j ];\n\t\t\t}\n\t\t}\n\t\n\tprint \"@ans\";\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\tchomp;\n\tmy $n = $_;\n\t\n\tmy @A;\n\t\n\tfor my $i ( 0 .. $n - 1 ){\n\t\tfor my $j ( 0 .. $n - 1 ){\n\t\t\tif( $i == $j ){\n\t\t\t\t$A[ $i ][ $j ] = 'X';\n\t\t\t\t}\n\t\t\telsif( ($n - 0) % 2 == 0 and $i + $j == $n - 1 ){\n\t\t\t\t$A[ $i ][ $j ] = 0;\n\t\t\t\t}\n\t\t\telsif( ( $i + $j ) % 2 == 1 ){\n\t\t\t\t$A[ $i ][ $j ] = 1;\n\t\t\t\t}\n\t\t\telsif( ( $i + $j ) % 2 == 0 ){\n\t\t\t\t$A[ $i ][ $j ] = -1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"@{ $_ }\" for @A;\n\t\n\tmy @ans;\n\t\n\tfor my $i ( 0 .. $n - 1 ){\n\t\tfor my $j ( $i + 1 .. $n - 1 ){\n\t\t\tpush @ans, $A[ $i ][ $j ];\n\t\t\t}\n\t\t}\n\t\n\tprint \"@ans\";\n\t}"}], "src_uid": "a89c585ebd9608141399c813385c04c6"} {"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$$$)\u00a0\u2014 the length of the song and the number of questions. The second line contains one string $$$s$$$\u00a0\u2014 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$$$)\u00a0\u2014 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": "@arr = split(\" \", );\r\n$n = int($arr[0]);\r\n$q = int($arr[1]);\r\n@s = split(\"\", );\r\n@pref = (0) * $n;\r\n@arr = (0) * $n;\r\nfor ($i = 0; $i < $n; ++$i) {\r\n $arr[$i] = ord($s[$i]) - ord('a') + 1;\r\n $last = ($i == 0) ? 0 : $pref[$i - 1];\r\n $pref[$i] = $last + $arr[$i];\r\n}\r\n\r\nfor ($i = 0; $i < $q; ++$i) {\r\n ($l, $r) = split(\" \", );\r\n $last = $pref[$r - 1] - $pref[$l - 1] + $arr[$l - 1];\r\n print(\"$last\\n\");\r\n}\r\n"}], "negative_code": [], "src_uid": "461378e9179c9de454674ea9dc49c56c"} {"nl": {"description": "A permutation of length n is an integer sequence such that each integer from 0 to (n\u2009-\u20091) appears exactly once in it. For example, sequence [0,\u20092,\u20091] is a permutation of length 3 while both [0,\u20092,\u20092] and [1,\u20092,\u20093] 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,\u2009a1,\u2009...,\u2009an\u2009-\u20091 if and only if ai\u2009=\u2009i. For example, permutation [0,\u20092,\u20091] has 1 fixed point and permutation [0,\u20091,\u20092] 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\u2009\u2264\u2009n\u2009\u2264\u2009105). The second line contains n integers a0,\u2009a1,\u2009...,\u2009an\u2009-\u20091 \u2014 the given permutation.", "output_spec": "Print a single integer \u2014 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": "<>;\nfor(@a=split/ /,<>){\n if($a[$_]==$_){$f++}\n elsif($a[$a[$_]]==$_){$e=2}\n elsif(!$e){$e=1}\n}\nprint $f+$e\n"}, {"source_code": "<>;\nfor(@a=split/ /,<>){\n if($a[$_]==$_){$f++}\n elsif($a[$a[$_]]==$_){$e=2}\n elsif(!$e){$e=1}\n}\nprint $f+$e\n"}, {"source_code": "<>;\nfor(@a=split/ /,<>){\n if($a[$_]==$_){$f++}\n elsif($a[$a[$_]]==$_){$e=2}\n elsif(!$e){$e=1}\n}\nprint $f+$e"}, {"source_code": "<>;\nfor(@a=split/ /,<>){\n if($a[$_]==$_){$f++}\n elsif($a[$a[$_]]==$_){$e=2}\n elsif(!$e){$e=1}\n}\nprint $f+$e\n"}, {"source_code": "<>;\nfor(@a=split/ /,<>){\n if($a[$_]==$_){$f++}\n elsif($a[$a[$_]]==$_){$e=2}\n elsif(!$e){$e=1}\n}\nprint $f+$e\n"}, {"source_code": "chomp($n=<>);\nchomp($_=<>);\nmy @a = split / /;\n$ans = 0;\n$swap = 0;\nfor $i (0..$n) {\n\tif ($a[$i] == $i) {\n\t\t$ans++;\n\t} elsif ($swap == 0) {\n\t\t$swap = 1 if $a[$a[$i]]==$i;\n\t}\n}\n$ans+=2 if $swap;\n$ans++ if $ans<$n && $swap==0;\nprint \"$ans\\n\";\n\t"}, {"source_code": "<>;\nfor(@a=split/ /,<>){\n if($a[$_]==$_){$f++}\n elsif($a[$a[$_]]==$_){$e=2}\n elsif(!$e){$e=1}\n}\nprint $f+$e\n"}, {"source_code": "#!/usr/bin/perl\nuse 5.010;\n\nsub Solve {\n my $ret = 0;\n my $inc = 1;\n while (($idx, $val) = each(@_)) {\n $idx == $val && $ret++;\n $idx != $val && $_[$val] == $idx && ($inc = 2);\n }\n return $ret == @_? $ret : $ret + $inc;\n}\n\nwhile (<>) {\n @line = split /\\s+/, <>;\n say Solve(@line);\n}\n"}, {"source_code": "<>;\n@a=split/ /,<>;\nfor(@a){\n if($a[$_]==$_){$f++}\n elsif($a[$a[$_]]==$_){$e=2}\n elsif(!$e){$e=1}\n}\nprint $f+$e;"}, {"source_code": "<>;\nfor(@a=split/ /,<>){\n if($a[$_]==$_){$f++}\n elsif($a[$a[$_]]==$_){$e=2}\n elsif(!$e){$e=1}\n}\nprint $f+$e"}, {"source_code": "<>;\nfor(@a=split/ /,<>){\n if($a[$_]==$_){$f++}\n elsif($a[$a[$_]]==$_){$e=2}\n elsif(!$e){$e=1}\n}\nprint $f+$e\n"}, {"source_code": "<>;\nfor(@a=split/ /,<>){\n if($a[$_]==$_){$f++}\n elsif($a[$a[$_]]==$_){$e=2}\n elsif(!$e){$e=1}\n}\nprint $f+$e\n"}, {"source_code": "<>;\n@_=split/ /,<>;\nfor (0..@_-1){\n\tif ($_==$_[$_]){$n++}\n\telsif ($_==$_[$_[$_]]){$k++}\n\t}\n$k and $k=1;\n$n==@_ and $k--;\nprint ($n+(++$k));"}, {"source_code": "my $num = <>;\nchomp $num;\nmy @perm = split ' ', <>;\nmy %perm = map { $_, $perm[$_] } 0..$num-1;\n$count = 0;\nmy $ext = 0;\nfor (0..$num-1) {\n if ($perm{$_} == $_) {\n $count++;\n } elsif (!$ext && $perm{$perm{$_}} == $_) {\n $ext = 2;\n }\n}\n$ext = 1 unless $ext == 2 || $count == $num;\n$count += $ext;\nprint $count;"}, {"source_code": "<>;\nfor(@a=split/ /,<>){\n if($a[$_]==$_){$f++}\n elsif($a[$a[$_]]==$_){$e=2}\n elsif(!$e){$e=1}\n}\nprint $f+$e\n"}], "negative_code": [{"source_code": "chomp($n=<>);\nchomp($_=<>);\nmy @a = split / /;\n$ans = 0;\n$swap = 0;\nfor (0..$n) {\n\tif ($a[$_] == $_) {\n\t\t$ans++;\n\t} elsif ($swap==0) {\n\t\tif ($_+$_ != $n-1) {\n\t\t\tif ($a[$_]==$n-1-$_ && $a[$n-1-$_]==$_) {\n\t\t\t\t$swap = 1;\n\t\t\t}\t\n\t\t}\n\t}\n}\n$ans += 2 if $swap;\n$ans++ if $ans<$n && $swap==0;\nprint \"$ans\\n\";\n\t"}, {"source_code": "chomp($n=<>);\nchomp($i=<>);\nmy @a = split / /;\n$ans = 0;\n$swap = 0;\nfor $i (0..$n) {\n\tif ($a[$i] == $i) {\n\t\t$ans++;\n\t} elsif ($swap==0) {\n\t\tfor $j ($i+1..$n) {\n\t\t\tif ($a[$i]==$j && $a[$j]==$i) {\n\t\t\t\t$swap = 1;\n\t\t\t\tlast;\n\t\t\t}\n\t\t}\n\t}\n}\n$ans += 2 if $swap;\n$ans++ if $ans<$n && $swap==0;\nprint \"$ans\\n\";\n\t"}, {"source_code": "<>;\n@_=split/ /,<>;\nfor (0..@_-1){\n\tif ($_==$_[$_]){$n++}\n\telsif ($_==$_[$_[$_]]){$k++}\n\t}\n@_ > 3 and $n++;\n$k and $k=1;\nprint $n+$k;"}, {"source_code": "<>;\n@_=split/ /,<>;\nfor (0..@_-1){\n\tif ($_==$_[$_]){$n++}\n\telsif ($_==$_[$_[$_]]){$k++}\n\t}\n$k and $k=2;\nprint $n+$k;"}, {"source_code": "<>;\n@_=split/ /,<>;\nfor (0..@_-1){\n\tif ($_==$_[$_]){$n++}\n\telsif ($_==$_[$_[$_]]){$k++}\n\t}\n$k and $k=1;\nprint $n+++$k;"}, {"source_code": "<>;\n@_=split/ /,<>;\nfor (0..@_-1){\n\tif ($_==$_[$_]){$n++}\n\telsif ($_==$_[$_[$_]]){$k++}\n\t}\n$k and $k=1;\n$n==@_ and $k--;\nprint ($n+++$k);"}], "src_uid": "e63de0fffd00b2da103545a7f1e405be"} {"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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @a = split(//,( '' . $n ));\r\n my $mx = 1;\r\n for(my $i=0;$i<=$#a;$i++){\r\n $mx = $a[$i] - 0 if $a[$i] - 0 > $mx;\r\n }\r\n print \"$mx\\n\";\r\n}\r\n\r\nexit(0);\r\n"}, {"source_code": "<>;\r\nchomp, print +(sort split'')[-1] . $/ for <>"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my $cnt = 0;\r\n while( $n > 0 ){\r\n my $ns = '' . $n;\r\n my $ov = undef;\r\n my $top = substr($ns,0,1) - 0;\r\n my $ms = $top;\r\n for(my $i=1;$i $top ? $top : '0' );\r\n $ov = 1;\r\n }\r\n }\r\n }\r\n $n -= ( $ms - 0 );\r\n $cnt += $top;\r\n }\r\n print \"$cnt\\n\";\r\n \r\n}\r\n\r\nexit(0);\r\n"}], "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$$$)\u00a0\u2014 the number of testcases. The first line of each testcase contains two integers $$$w$$$ and $$$h$$$ ($$$3 \\le w, h \\le 10^6$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the number of points. Then, $$$k$$$ integers $$$x_1 < x_2 < \\dots < x_k$$$ ($$$0 < x_i < w$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the number of points. Then, $$$k$$$ integers $$$y_1 < y_2 < \\dots < y_k$$$ ($$$0 < y_i < h$$$)\u00a0\u2014 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\u00a0\u2014 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)$$$\u00a0\u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tprint +( sort { $b <=> $a } map { &M * $_, &M * $_ } reverse split )[ 0 ];\r\n\t}\r\n\r\nsub M {\r\n\t<> =~ / (\\d+).* (\\d+)/;\r\n\t$2 - $1;\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $w, $h ) = split;\n\t\n\tmy $max_h = 0;\n\tmy $diff;\n\t\n\t@_ = split ' ', <>;\n\tshift @_;\n\t@_ = sort { $b <=> $a } @_;\n\t$diff = $_[ 0 ] - $_[ @_ - 1 ];\n\t$max_h < $diff and $max_h = $diff;\n\t\n\t@_ = split ' ', <>;\n\tshift @_;\n\t@_ = sort { $b <=> $a } @_;\n\t$diff = $_[ 0 ] - $_[ @_ - 1 ];\n\t$max_h < $diff and $max_h = $diff;\n\t\n\t$max_h *= $h;\n\t\n\tmy $max_v = 0;\n\t\n\t@_ = split ' ', <>;\n\tshift @_;\n\t@_ = sort { $b <=> $a } @_;\n\t$diff = $_[ 0 ] - $_[ @_ - 1 ];\n\t$max_v < $diff and $max_v = $diff;\n\t\n\t@_ = split ' ', <>;\n\tshift @_;\n\t@_ = sort { $b <=> $a } @_;\n\t$diff = $_[ 0 ] - $_[ @_ - 1 ];\n\t$max_v < $diff and $max_v = $diff;\n\t\n\t$max_v *= $w;\n\t\n\tprint $max_v > $max_h ? $max_v : $max_h;\n\t}"}, {"source_code": "use bigint;\r\nuse List::Util qw(max min);\r\n\r\nfor ($i = <>; $i > 0; $i--) {\r\n @wh = split(/\\s/, <>);\r\n @h1a = split(/ /, <>);\r\n @h2a = split(/ /, <>);\r\n @w1a = split(/ /, <>);\r\n @w2a = split(/ /, <>);\r\n\r\n $h1 = $h1a[$h1a[0]] - $h1a[1];\r\n $h2 = $h2a[$h2a[0]] - $h2a[1];\r\n $maxh = max($h1, $h2);\r\n\r\n $w1 = $w1a[$w1a[0]] - $w1a[1];\r\n $w2 = $w2a[$w2a[0]] - $w2a[1];\r\n $maxw = max($w1, $w2);\r\n\r\n print max($maxw * $wh[0], $maxh * $wh[1]) . \"\\n\";\r\n}\r\n\r\n"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009105,\u20091\u2009\u2264\u2009m\u2009\u2264\u2009105). The second line contains m integers a1,\u2009a2,\u2009...,\u2009am (1\u2009\u2264\u2009ai\u2009\u2264\u2009n). Note that Xenia can have multiple consecutive tasks in one house.", "output_spec": "Print a single integer \u2014 the time Xenia needs to complete all tasks. Please, do not use the %lld specifier to read or write 64-bit integers in \u0421++. 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\u2009\u2192\u20092\u2009\u2192\u20093\u2009\u2192\u20094\u2009\u2192\u20091\u2009\u2192\u20092\u2009\u2192\u20093. This is optimal sequence. So, she needs 6 time units."}, "positive_code": [{"source_code": "$_=<>;($n,$m,$c,$r)=(split,1,0);$_=<>;for(split){$r+=($_+$n-$c)%$n;$c=$_}print$r\n"}, {"source_code": "$_=<>;($n,$m,$c,$r)=(split,1,0);$_=<>;for(split){$r+=($_+$n-$c)%$n;$c=$_}print$r\n\n"}, {"source_code": "my ($n, $m) = split \" \", <>;\nmy @d = split \" \", <>;\nmy $t = 0;\nmy $cur = 1;\nfor (@d) {\n $t += $_ - $cur + $n * ($_ < $cur);\n $cur = $_;\n}\nprint $t;"}, {"source_code": "$_=<>;($n,$m,$c,$r)=(split,1,0);$_=<>;for(split){$r+=($_+$n-$c)%$n;$c=$_}print$r\n\n"}, {"source_code": "my ($n, $m) = split / /, <>;\nmy @arr = split / /, <>;\nmy $ans = $arr[0];\nfor (my $i = 1; $i < scalar @arr; $i++) {\n if ($arr[$i] >= $arr[$i - 1]) {\n $ans += $arr[$i] - $arr[$i - 1];\n }\n else {\n $ans += $n - $arr[$i - 1] + $arr[$i];\n }\n # print $ans . \"\\n\";\n}\nprint $ans - 1 . \"\\n\";"}, {"source_code": "$_=<>;($n,$m,$c,$r)=(split,1,0);$_=<>;for(split){$r+=($_+$n-$c)%$n;$c=$_}print$r\n"}, {"source_code": "$_=<>;($n,$m,$c,$r)=(split,1,0);$_=<>;for(split){$r+=($_+$n-$c)%$n;$c=$_}print$r\n\n"}, {"source_code": "$_=<>;($n,$m,$c,$r)=(split,1,0);$_=<>;for(split){$r+=($_+$n-$c)%$n;$c=$_}print$r\n\n"}, {"source_code": "$_=<>;($n,$m,$c,$r)=(split,1,0);$_=<>;for(split){$r+=($_+$n-$c)%$n;$c=$_}print$r\n"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\n($m, $n) = split / /, <>;\n@a = split / /, <>;\n$i = 0;\nwhile ($i < $n) {\n\t++$i;\n\twhile ($i<$n && $a[$i]>=$a[$i-1]) {\n\t\t++$i;\n\t}\n\t$i>=$n and $ans+=$a[$i-1] or $ans+=$m;\n}\nsay $ans-1;\n"}, {"source_code": "use strict;\nuse warnings;\n\n$\\ = \"\\n\";\n\nsub dist {\n my ($x, $y, $n) = @_;\n ($y - $x + $n) % $n;\n}\n\nmy ($n, $m) = split ' ', <>;\nmy @tasks = split ' ', <>;\nmy $pos = 1;\nmy $time = 0;\n\nfor my $task (@tasks) {\n $time += dist($pos, $task, $n);\n $pos = $task;\n}\n\nprint $time;\n"}, {"source_code": "($n, $m) = split ' ', <>;\n@arr = split ' ', <>;\n$total = 0;\n$last = 1;\nfor (@arr) {\n if ($_ >= $last) {\n $total += $_ - $last;\n $last = $_;\n } else {\n $total += $n - ($last - $_);\n $last = $_;\n }\n}\nprint $total;\n"}, {"source_code": "$_=<>;($n,$m,$c,$r)=(split,1,0);$_=<>;for(split){$r+=($_+$n-$c)%$n;$c=$_}print$r\n\n"}, {"source_code": "#!/usr/bin/perl -w\nuse 5.010;\n\nwhile (<>) {\n $n = (split /\\s+/)[0];\n @tasks = split /\\s+/, \"1 \" . <>;\n $loop = 0;\n for (my $i = 1; $i < @tasks; $i++) {\n if ($tasks[$i] < $tasks[$i - 1]) {\n $loop++;\n }\n }\n $ans = $tasks[-1] - $tasks[0] + $loop * $n;\n say $ans;\n}\n"}, {"source_code": "$_=<>;($n,$m,$c,$r)=(split,1,0);$_=<>;for(split){$r+=($_+$n-$c)%$n;$c=$_}print$r\n\n"}, {"source_code": "$_=<>;($n,$m,$c,$r)=(split,1,0);$_=<>;for(split){$r+=($_+$n-$c)%$n;$c=$_}print$r\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nuse constant POS_START=>1; # \u958b\u59cb\u4f4d\u7f6e\n\nmain();\n\n\n## main()\n# \u30e1\u30a4\u30f3\u51e6\u7406\nsub main {\n # \u6a19\u6e96\u5165\u529b\u306e\u53d6\u5f97\n my $line1 = <>;\n my $line2 = <>;\n chomp($line1);\n chomp($line2);\n\n # \u6a19\u6e96\u5165\u529b\u306e\u89e3\u6790\n my ($n, $m) = split(/ /, $line1);\n my (@a) = split(/ /, $line2);\n if ($m != $#a+1) {\n print \"illeagal arguments\\n\";\n exit -1;\n }\n\n my $pos = POS_START;\n my $cnt_round = 0;\n foreach(@a) {\n if ($_ > $n || $_ < 1) {\n print \"illeagal arguments\\n\";\n exit -1;\n }\n if ($pos > $_) { $cnt_round++; }\n $pos = $_;\n }\n \n my $time_total = $cnt_round * $n + $pos - POS_START;\n print \"$time_total\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=split/ /,<>;\nunshift @_,1;\nfor $i(1..@_-1){\n $_[$i]>=$_[$i-1] ? ($sum+=$_[$i]-$_[$i-1]) : ($sum+= $`+$_[$i]-$_[$i-1]);\n }\nprint $sum;\n\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "$_=<>;($n,$m,$c,$r)=(split,1,0);$_=<>;for(split){$r+=($_+$n-$c)%$n;$c=$_}print$r\n\n"}], "negative_code": [], "src_uid": "2c9c96dc5b6f8d1f0ddeea8e07640d3e"} {"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\u2009+\u20097.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\u2009+\u20097.", "sample_inputs": ["ab", "aab"], "sample_outputs": ["1", "3"], "notes": "NoteThe first example: \"ab\" \u2009\u2192\u2009 \"bba\".The second example: \"aab\" \u2009\u2192\u2009 \"abba\" \u2009\u2192\u2009 \"bbaba\" \u2009\u2192\u2009 \"bbbbaa\"."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $MOD = 1e9 + 7;\n\nwhile(<>){\n\tchomp;\n\tmy $c;\n\tmy $ans = 0;\n\tmy $B = 0;\n\t\n\twhile( $c = chop ){\n\t\t\n\t\tif( $c eq 'b' ){\n\t\t\t$B ++;\n\t\t\t}\n\t\telse{\n\t\t\tmap { $_ %= $MOD } $ans += $B, $B <<= 1;\n\t\t\t}\n\t\t}\n\t\t\n\tprint $ans;\n\t}"}, {"source_code": "$_ = <>, chomp;\n\n$c eq 'b' ? $B ++ : map $_ %= 1e9 + 7, $ans += $B, $B <<= 1 while $c = chop;\n\t\nprint $ans"}], "negative_code": [{"source_code": "$_ = <>;\nchomp;\ns/a+$// while /a$/;\n$a = () = /a/g;\nprint $a * ($a + 1) / 2 % (1e9 + 7);"}], "src_uid": "8f52241c690ec4f9af71a52904fb19a0"} {"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$$$) \u2014 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": "#!/usr/bin/env perl\nuse strict;\nuse warnings;\n\nmy ($n, $k, $str);\nmy $asdf = ; chomp($asdf); $asdf =~ /(\\d+) (\\d+)/; ($n, $k) = ($1, $2);\n$str = ; chomp($str);\n\nmy @chars = reverse split(//, $str);\nmy @pos;\nfor ('a'..'z') {\n $pos[ord($_)-ord('a')] = [];\n}\nwhile (my ($i, $ch) = each @chars) {\n push(@{$pos[ord($ch)-ord('a')]}, $n-1-$i);\n}\n@chars = reverse @chars;\nfor (1..$k) {\n for ('a'..'z') {\n my $idx = ord($_)-ord('a');\n if (scalar @{$pos[$idx]} > 0) {\n my $pos = pop @{$pos[$idx]};\n $chars[$pos] = '';\n last;\n }\n }\n}\n\nprint join('', @chars), \"\\n\";\n"}], "negative_code": [], "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": "$_ = ;\nchomp;\n\nif ( $_=~m/[BCDEFGJKLNPQRSZ]/ ){\n print \"NO\";\n}else{\n if ( $_ eq (reverse $_)){\n print \"YES\";\n }else{\n print \"NO\";\n }\n}"}, {"source_code": "$_=<>; chomp;\n$r=reverse$_;\nprint /[^AHIOMT-Y]/ || $r ne $_? NO:YES\n"}, {"source_code": "$_=<>; chomp;\n$r=reverse$_;\nprint /[^AHIOMT-Y]/ || $r ne $_? NO:YES\n"}, {"source_code": "$_=<>; chomp;\n$r=reverse$_;\nprint /[^AHIOMT-Y]/ || $r ne $_? NO:YES\n"}, {"source_code": "$_=<>; chomp;\n$r=reverse$_;\nprint /[^AHIOMT-Y]/ || $r ne $_? NO:YES\n"}, {"source_code": "$_=<>; chomp;\n$r=reverse$_;\nprint /[^AHIOMT-Y]/ || $r ne $_? NO:YES\n"}, {"source_code": "$_=<>; chomp;\n$r=reverse$_;\nprint /[^AHIOMT-Y]/ || $r ne $_? NO:YES\n"}, {"source_code": "$_=<>; chomp;\n$r=reverse$_;\nprint /[^AHIOMT-Y]/ || $r ne $_? NO:YES\n"}, {"source_code": "$_=<>; chomp;\n$r=reverse$_;\nprint /[^AHIOMT-Y]/ || $r ne $_? NO:YES\n"}, {"source_code": "#Scott Heinrich - A01273823\nuse strict;\nuse warnings;\nuse 5.010;\n\nmy $line1 = <>;\nchomp($line1);\n\nif ($line1 =~ m/[BCDEFGJKLNPQRSZ]/)\n{\n say \"NO\";\n exit;\n}\nelsif ($line1 eq reverse($line1))\n{\n say \"YES\";\n exit;\n}\nelse\n{\n say \"NO\";\n exit;\n}\n"}, {"source_code": "$_=<>; chomp;\n$r=reverse$_;\nprint /[^AHIOMT-Y]/ || $r ne $_? NO:YES"}, {"source_code": "use strict;\nuse POSIX;\n\nchomp (my $s = <>);\nmy @s = split '', $s;\nprint \"NO\" and exit if $s=~ m/[BCDEFGJKLNPQRSZ]/;\nmy $f=0;\na:for(0..$#s/2)\n{\nif ($s[$_] ne $s[$#s - $_])\n{\n$f = 2;\nlast a; \n\n};\n}\n$f? print \"NO\":print \"YES\";\n"}, {"source_code": "$st=<>;\nchomp($st);\n$rv=reverse $st;\n($rv ne $st || $st=~/[^AHIOMT-Y]/ )? print \"NO\":print \"YES\";"}, {"source_code": "use integer;\n\nwhile(<>){\n\t\n\tchomp;\n\t$f=0;\n\t\n\t# A H I O U V W T Y X M\n\t# <>\n\t\n\t$le = length;\n\t@_=qw(A H I O U V W T Y X M);\n#\tprint \"@_\\n\";\n\t\n\t/[^AHIOUVWTYXM]/ and $f=1;\n\t\n\t\n\t@a=split//;\n#\tprint 0+@a,\"\\n\";\n\t\n\tif (!$f)\n\t{\n\t\tfor $i(0..(@a/2)){\n\t\t\t\n\t\t\t$a[$i] eq $a[@a-$i-1] or $f=2;\n\t\t\t\n\t\t\t\n\t\t}\n\t\t\n\t}\n#\tprint \"$f\\n\";\n\tprint $f? \"NO\\n\":\"YES\\n\";\n\t\n\t}\n\t\n"}, {"source_code": "$_=<>; chomp;\n$r=reverse$_;\nprint /[^AHIOMT-Y]/ || $r ne $_? NO:YES"}], "negative_code": [{"source_code": "\n$_ = ;\nchomp;\n\nif ( $_=~m/[BCDEFGJKLMNPQRSZ]/ ){\n print \"NO\";\n}else{\n if ( $_ eq (reverse $_)){\n print \"YES\";\n }else{\n print \"NO\";\n }\n}"}, {"source_code": "#Scott Heinrich - A01273823\nuse strict;\nuse warnings;\nuse 5.010;\n\nmy $line1 = <>;\nchomp($line1);\n\nif ($line1 =~ m/[BCDEFGJKLNPQRSZ]/)\n{\n say \"No\";\n exit;\n}\nelsif ($line1 eq reverse($line1))\n{\n say \"Yes\";\n exit;\n}\nelse\n{\n say \"No\";\n exit;\n}\n"}, {"source_code": "use strict;\nuse POSIX;\n\nchomp (my $s = <>);\nmy @s = split '', $s;\nprint \"NO\" and exit if $s=~ m/[BCDEFGJKLNPQRS]/;\nmy $f=0;\na:for(0..$#s/2)\n{\nif ($s[$_] ne $s[$#s - $_])\n{\n$f = 2;\nlast a; \n\n};\n}\n$f? print \"NO\":print \"YES\";\n"}, {"source_code": "$st=<>;\nchomp($st);\n$rv=reverse $st;\n($rv eq $st)? print \"YES\":print \"NO\";"}, {"source_code": "$st=<>;\nchomp($st);\n$rv=reverse $st;\n($rt eq $st && length($rt)!=1)? print \"YES\":print \"NO\";"}, {"source_code": "$st=<>;\nchomp($st);\n$rv=reverse $st;\n($rv eq $st&& length($rv)!=1)? print \"YES\":print \"NO\";"}, {"source_code": "$st=<>;\nchomp($st);\n$rv=reverse $st;\n($rv eq $st && length($rv)!=1)? print \"YES\":print \"NO\";"}, {"source_code": "$st=<>;\nchomp($st);\n$rt=reverse $st;\n($rt eq $st && length($rt)!=1)? print \"YES\":print \"NO\";"}, {"source_code": "$st=<>;\nchomp($st);\n$rv=reverse $st;\n($rv ne $st || $st=~/[^AHITOMY]/ )? print \"NO\":print \"YES\";"}], "src_uid": "8135173b23c6b48df11b1d2609b08080"} {"nl": {"description": "The police department of your city has just started its journey. Initially, they don\u2019t 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\u00a0(1\u2009\u2264\u2009n\u2009\u2264\u2009105), 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": "my $n = <>;\nchomp($n);\nmy $in = <>;\nchomp($in);\nmy @A = split ' ', $in;\nmy ($sum, $ans) = (0, 0);\nfor (my $i = 0; $i < $n; ++$i) {\n $sum += $A[$i];\n $ans = $sum if $ans > $sum;\n}\nprint abs $ans;\n"}, {"source_code": "my $a = <>;\nmy @b = split \" \", <>;\nmy $m = 0, my $c = 0;\nforeach (@b) {\n if ($_ < 0) {\n if ($m > 0) {\n --$m;\n } else {\n ++$c;\n }\n } else {\n $m += $_;\n }\n}\nprint $c, \"\\n\";"}, {"source_code": "<>;\nmy $n = 0;\nmy $ans = 0;\nforeach(split / /, <>) {\n $n += int $_;\n if ($n < 0) {\n $ans++;\n $n = 0;\n }\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "<>;\nmy $n = 0;\nmy $ans = 0;\nforeach(split / /, <>) {\n $n += int$_ if (int $_ > 0);\n if (int $_ < 0) {\n $ans++ if ($n == 0);\n $n-- if ($n > 0);\n }\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "use strict;\n<>;\nmy (@a) = split (' ',<>);\nmy $cop=0;\nmy $ans = 0;\nfor my $event (@a)\n{\n if($event > 0)\n {\n $cop += $event; \n }\n else\n {\n $ans++ if $cop == 0;\n $cop-- if $cop > 0;\n }\n}\nprint $ans;"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nchomp($n = <>);\n($ans, $tot) = (0, 0);\nforeach (split / /, <>) {\n\t$tot += $_;\n\t$tot<0 and ++$ans and $tot=0;\n}\nsay $ans;"}, {"source_code": "<>;\nmy @events = split ' ', <>;\nmy $free = 0;\nmy $missed = 0;\n\nfor my $event (@events) {\n $free += $event;\n while ($free < 0) {\n $missed++;\n $free++;\n }\n}\n\nprint $missed, \"\\n\";\n"}, {"source_code": "$n=<>; $res=0; $sum=0;\n@ar =split/ /,<>;\nchomp(@ar);\nforeach(@ar)\n{\n if($_ == -1) {if($sum==0){$res--;}else {$sum--;} }\n else{ $sum+=$_;} \n}\n( $res < 0 ) ? ($res=$res*(-1)) : ( $res=0); \nprint $res;"}, {"source_code": "<>;$r = 0;$s = 0;\n@_ = split / /, <>;\nforeach (@_) {if ( $_ == -1 ){($s==0)?($r++):( $s--);} else { $s += $_; }}\nprint $r;"}, {"source_code": "<>; $r=0; $s=0;\n@_ =split/ /,<>;\nforeach(@_)\n{\n if($_ == -1) { if($s==0){$r++;} else {$s--;} }\n else{ $s+=$_;} \n}\nprint $r;"}, {"source_code": "<>;\n$r = 0;\n$s = 0;\n@_ = split / /, <>;\n\nforeach (@_) {\n if ( $_ == -1 ) { ( $s == 0 ) ? ( $r++ ) : ( $s-- ); }\n else { $s += $_; }\n}\nprint $r;"}, {"source_code": "<>;\n$_=<>;\nchomp;\n@_=split/ /;\n$n=0;\nfor (@_){\n$sum+=$_;\nif ($sum<0){$sum=0; $n++}\n}\nprint $n;"}], "negative_code": [{"source_code": "$n=<>;\n@ar =split/ /,<>;\nchomp(@ar);\n$res=0; $sum=0;\nforeach(@ar)\n{\n if($_ == -1) {$res=$res-1+$sum;}\n else{ $sum+=$_;} \n \n}\n( $res < 0 ) ? ($res=$res*(-1)) : ( $res=0); \nprint $res;"}, {"source_code": "$n=<>; $res=0; $sum=0;\n@ar =split/ /,<>;\nchomp(@ar);\nforeach(@ar)\n{\n if($_ == -1) { $sum--;$res=$sum;}\n else{ $sum+=$_;} \n \n}\n( $res < 0 ) ? ($res=$res*(-1)) : ( $res=0); \nprint $res;"}, {"source_code": "$n=<>; $res=0; $sum=0;\n@ar =split/ /,<>;\nchomp(@ar);\nforeach(@ar)\n{\n if($_ == -1) {$res=$res-1+$sum; $sum--;}\n else{ $sum+=$_;} \n \n}\n( $res < 0 ) ? ($res=$res*(-1)) : ( $res=0); \nprint $res;\n"}, {"source_code": "$n=<>;\n@ar =split/ /,<>;\nchomp(@ar);\nforeach(@ar)\n{\n if($_ == -1) {$res=$sum-1;}\n $sum+=$_; \n \n}\n( $res < 0 ) ? $res=$res*(-1) : $res=0; \nprint $res;"}, {"source_code": "$n=<>;\n@ar =split/ /,<>;\nchomp(@ar); $res=0; $sum=0;\nforeach(@ar)\n{\n if($_ == -1) {$res=$sum-1;}\n $sum+=$_; \n \n}\n( $res < 0 ) ? ($res=$res*(-1)) : ( $res=0); \nprint $res;"}], "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\u2009\u2264\u2009k,\u2009d,\u2009t\u2009\u2264\u20091018).", "output_spec": "Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10\u2009-\u20099. 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse bignum;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $k, $d, $t ) = split;\n\t\n\twhile( $k > $d ){\n\t\tif( $k % $d == 0 ){ $d = $k; last; }\n\t\tmy $i = int $k / $d;\n\t\t$d = ( $i + 1 ) * $d;\n\t\t}\n\t\n\t$debug and print \"[$k|$d]\";\n\t\n\tmy $c = $k + 0.5 * ( $d - $k );\n\t\n\tprint do {\n\t\t\t\n\t\tif( $k == $d ){\n\t\t\t$t;\n\t\t\t}\n\t\telse{\n\t\t\tmy $i = int $t / $c;\n\t\t\tmy $r = $t - $i * $c;\n\t\t\t\n\t\t\t$debug and print \" <$i|$r>\";\n\t\t\t\n\t\t\t$i * $d + \n\t\t\t\tdo {\n\t\t\t\t\tif( $r <= $k ){\n\t\t\t\t\t\t$r;\n\t\t\t\t\t\t}\n\t\t\t\t\telse{\n\t\t\t\t\t\t$k + 2 * ( $r - $k );\n\t\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t}\n\t\t};\n\t\n\t}"}, {"source_code": "use bignum;\n\n( $k, $d, $t ) = split ' ', <>;\n\n$k > $d and $d = $k % $d ? ( 1 + int $k / $d ) * $d : $k;\n\t\n$c = 0.5 * ( $d + $k );\n\n$i = int $t / $c;\n$r = $t - $i * $c;\n\nprint $i * $d + ( $r > $k ? $k + 2 * ( $r - $k ) : $r )"}], "negative_code": [{"source_code": "use bignum;\n\n( $k, $d, $t ) = split ' ', <>;\n\t\n$d += ( int $k / $d ) * ( $d - !( $k % $d ) );\n\n$c = 0.5 * ( $d + $k );\n\n$i = int $t / $c;\n$r = $t - $i * $c;\n\nprint $i * $d + ( $r > $k ? $k + 2 * ( $r - $k ) : $r )"}], "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\u2009>\u20091. 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of banks. The second line contains n integers ai (\u2009-\u2009109\u2009\u2264\u2009ai\u2009\u2264\u2009109), 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": "# 1e9 x 1e5/2 < 1e14 < 2^53\n\n$n=<>;\n@a=split(' ',<>);\n\n$_=[$i++, $l=$_+$l, $_] for @a;\n@a=sort {$a->[1]<=>$b->[1] || $a->[0]<=>$b->[0]} @a;\n$lmin=$n;\n\nfor ($i=0; $i<$n; $i=$j) {\n\t$b=$a[$i]->[1];\n\t$j++ while $a[$j]->[1]==$b && $j<$n;\n\t$l=0;\n\tfor ($k=$i+1; $k<$j; $k++) {\n\t\tnext if $a[$k]->[2]==0;\n\t\t$ladd=$a[$k]->[0] - $a[$k-1]->[0] - 1;\n\t\t$ladd>0 || die;\n\t\t$l+=$ladd;\n\t}\n\t$ladd=$n + $a[$i]->[0] - $a[$j-1]->[0] - 1;\n\t$l+=$ladd;\n\t$lmin=$l if $l<$lmin;\n}\n\nprint $lmin;\n"}], "negative_code": [], "src_uid": "be12bb8148708f9ad3dc33b83b55eb1e"} {"nl": {"description": "You have an array $$$a$$$ of size $$$n$$$ consisting only of zeroes and ones. You can do the following operation: choose two indices $$$1 \\le i , j \\le n$$$, $$$i \\ne j$$$, add $$$a_{i}$$$ to $$$a_{j}$$$, remove $$$a_{i}$$$ from $$$a$$$. Note that elements of $$$a$$$ can become bigger than $$$1$$$ after performing some operations. Also note that $$$n$$$ becomes $$$1$$$ less after the operation.What is the minimum number of operations needed to make $$$a$$$ non-decreasing, i.\u00a0e. that each element is not less than the previous element?", "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 an integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$), the size of array $$$a$$$. Next line contains $$$n$$$ integers $$$a_{1}, a_{2}, \\ldots a_{n}$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), elements of array $$$a$$$. It's guaranteed that sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case print a single integer, minimum number of operations needed to make $$$a$$$ non-decreasing.", "sample_inputs": ["4\n\n8\n\n0 0 1 1 1 1 1 1\n\n5\n\n1 0 0 1 1\n\n2\n\n1 0\n\n11\n\n1 1 0 0 1 0 0 1 1 1 0"], "sample_outputs": ["0\n1\n1\n3"], "notes": "NoteIn the first test case, $$$a$$$ is already non-decreasing, so you don't need to do any operations and the answer is $$$0$$$.In the second test case, you can perform an operation for $$$i = 1$$$ and $$$j = 5$$$, so $$$a$$$ will be equal to $$$[0, 0, 1, 2]$$$ and it becomes non-decreasing.In the third test case, you can perform an operation for $$$i = 2$$$ and $$$j = 1$$$, so $$$a$$$ will be equal to $$$[1]$$$ and it becomes non-decreasing."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$_ = <>;\r\n\ts/\\s//g;\r\n\t\r\n\tmy $i = 0;\r\n\t\r\n\t1 while m/./ and 0\r\n\t\t|| s/^0+//\r\n\t\t|| s/1$//\r\n\t\t|| ! ++ $i\r\n\t\t|| s/.// + chop\r\n\t\t;\r\n\t\r\n\tprint $i;\r\n\t}"}, {"source_code": "<>;\r\n\r\nwhile(<>){\r\n\t$_ = <>;\r\n\ts/\\s//g;\r\n\t\r\n\t$i = 0;\r\n\t\r\n\t1 while m/./ and 0\r\n\t\t|| s/^0+//\r\n\t\t|| s/1$//\r\n\t\t|| ! ++ $i\r\n\t\t|| s/.// + chop\r\n\t\t;\r\n\t\r\n\tprint $i . $/\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $i = 0;\n\t\n\tOUTER:\n\twhile( @_ ){\n\t\twhile( $_[ 0 ] == 0 ){\n\t\t\tshift @_;\n\t\t\tnext OUTER;\n\t\t\t}\n\t\twhile( $_[ @_ - 1 ] == 1 ){\n\t\t\tpop @_;\n\t\t\tnext OUTER;\n\t\t\t}\n\t\t\n\t\t$i ++;\n\t\tshift @_;\n\t\tpop @_;\n\t\t}\n\t\n\tprint $i;\n\t}"}, {"source_code": "#!/usr/bin/perl\nuse Data::Dumper;\nuse integer; ### important!\nmy $mod = 10 ** 9 + 7;\n# my $mod = 998244353;\n\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\n\nwhile( $testcase -- > 0 ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @A = map { $_ - 0 } split(/\\s+/,);\n \n my $f1 = -1;\n my $c0 = 0; my $c1 = 0; my $d0 = 0;\n for(my $i=0;$i<$n;$i++){\n $f1 = $i if $f1<0 and $A[$i]==1;\n $c0++ if $A[$i]==0;\n $c1++ if $A[$i]==1;\n $d0++ if $A[$i]==0 and $f1>=0;\n }\n if( $c0 == 0 or $c1 == 0 ){\n print \"0\\n\"; next;\n }\n my $res = 0;\n my $l = 0; my $r = $n-1;\n while(1){\n $r-- while $r>=0 && $A[$r]==1;\n $l++ while $l<$n && $A[$l]==0;\n last if $r <= $l;\n $res++;\n $r--;\n $l++;\n last if $r <= $l;\n }\n print \"$res\\n\";\n}\n\nexit(0);\n\nsub max {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\n return $r;\n}\nsub min {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\n return $r;\n}\nsub sum {\n my $r = 0;\n foreach my $e (@_){ $r += $e; }\n return $r;\n}\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\n my $r = shift; my $ar = shift;\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\n for(my $i=0;$i<=$#ar;$i++){ \n $rr += $ar->[$i];\n $r->[1+$i] = $rr;\n }\n}\n"}], "negative_code": [], "src_uid": "c247c7c382c243ab6b991c4a11bfc421"} {"nl": {"description": "Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.", "input_spec": "The first line contains two integers n and k (1\u2009\u2264\u2009n\u2009\u2264\u2009105, 1\u2009\u2264\u2009k\u2009\u2264\u2009109)\u00a0\u2014 the number of different pebble types and number of pebbles Anastasia can place in one pocket. The second line contains n integers w1,\u2009w2,\u2009...,\u2009wn (1\u2009\u2264\u2009wi\u2009\u2264\u2009104)\u00a0\u2014 number of pebbles of each type. ", "output_spec": "The only line of output contains one integer\u00a0\u2014 the minimum number of days Anastasia needs to collect all the pebbles.", "sample_inputs": ["3 2\n2 3 4", "5 4\n3 1 8 9 7"], "sample_outputs": ["3", "5"], "notes": "NoteIn the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type\u00a0\u2014 on the second day, and of third type\u00a0\u2014 on the third day.Optimal sequence of actions in the second sample case: In the first day Anastasia collects 8 pebbles of the third type. In the second day she collects 8 pebbles of the fourth type. In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type. In the fourth day she collects 7 pebbles of the fifth type. In the fifth day she collects 1 pebble of the second type. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\t$sum += (int $_ / $k) + !!($_ % $k) for @_;\n\t\n\tprint int( $sum / 2 ) + $sum % 2;\n\t\n\t}"}, {"source_code": "( $n, $k, @_ ) = split ' ', <>.<>;\n\n$z += (int $_ / $k) + !!($_ % $k) for @_;\n\nprint int( $z / 2 ) + $z % 2"}], "negative_code": [], "src_uid": "3992602be20a386f0ec57c51e14b39c5"} {"nl": {"description": "A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring.You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print \"NO\" (without quotes).A substring of a string is a contiguous subsequence of letters in the string. For example, \"ab\", \"c\", \"abc\" are substrings of string \"abc\", while \"ac\" is not a substring of that string.The number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap.String a is lexicographically smaller than string b, if a is a prefix of b, or a has a smaller letter at the first position where a and b differ.", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105)\u00a0\u2014 the number of strings in the set. Each of the next n lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct. The total length of the strings doesn't exceed 105.", "output_spec": "Print the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print \"NO\" (without quotes) if there are no good strings.", "sample_inputs": ["4\nmail\nai\nlru\ncf", "3\nkek\npreceq\ncheburek"], "sample_outputs": ["cfmailru", "NO"], "notes": "NoteOne can show that in the first sample only two good strings with minimum length exist: \"cfmailru\" and \"mailrucf\". The first string is lexicographically minimum."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\tmy $f = 0;\n\t\n\tif( @_ > 27 ** 2 or ( grep 26 < (length), @_ ) or ( grep /(.).*\\1/, @_ ) ){\n\t\t$f ++;\n\t\t}\n\t\n\t$debug and print \"[@_]\";\n\t\n\tif( ! $f ){\n\t\t@_ = sort @_;\n\t\t\n\t\t$debug and print \"[@_]\";\n\t\tmy @unused;\n\t\t\n\t\twhile( @_ > 1 ){\n\t\t\tmy( $A, $B ) = ( shift @_, shift @_ );\n\t\t\t\n\t\t\t$A =~ /./; \n\t\t\tmy $fA = $&;\n\t\t\t$B =~ /./; \n\t\t\tmy $fB = $&;\n\t\t\t\n\t\t\tif( $fA eq $fB ){\n\t\t\t\tmy $left;\n\t\t\t\t\n\t\t\t\tif( $A =~ /$B/ ){\n\t\t\t\t\t$left = $A;\n\t\t\t\t\t}\n\t\t\t\telsif( $B =~ /$A/ ){\n\t\t\t\t\t$left = $B;\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\t$f ++;\n\t\t\t\t\t}\n\t\t\t\t\n\t\t\t\tunshift @_, $left;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tpush @unused, $A;\n\t\t\t\tunshift @_, $B;\n\t\t\t\t}\n\t\t\t\n\t\t\t$f and last;\n\t\t\t}\n\t\t\n\t\tunshift @_, @unused;\n\t\t\n\t\t$_ = \"@_\";\n\t\t\n\t\t$debug and print;\n\t\t\n\t\t1 while 0\n\t\t\t|| !$f * \n\t\t\t\ts/\n\t\t\t\t(\\b\\w*?)\n\t\t\t\t(\\w+)\n\t\t\t\t(\\w*\\b)\n\t\t\t\t(.*?)\n\t\t\t\t(\\b\\w*?)\n\t\t\t\t\\2\n\t\t\t\t(\\w*\\b)\n\t\t\t\t/\n\t\t\t\t$debug and print \"[$1<$2>$3|$4|$5<$2>$6]\";\n\t\t\t\t$f += length( $1 ) * length( $5 ) + length( $3 ) * length( $6 );\n\t\t\t\t( $1 || $5 ) . $2 . ( $3 || $6 ) . $4\n\t\t\t\t/ex;\n\t\t\n\t\t$debug and print;\n\t\t}\n\t\n\t$debug and print;\n\t\n\t$f += /(\\w).*\\1/;\n\t\n\tprint $f ? \"NO\" : ( join '', sort split );\n\t\n\t$debug and print '-' x 20;\n\t}"}, {"source_code": "<>;\n\n@_ = <>;\nchomp @_;\n\n$f = @_ > 27 ** 2 || grep /(.).*\\1/, @_;\n\nif( !$f ){\n\t\n\t@_ = sort @_;\n\t\t\t\n\t$_ = \"@_\";\n\t\n\t1 while 0 \n\t\t|| !$f *\n\t\t\ts/\n\t\t\t(\\b\\w+)\n\t\t\t(\\w*\\b)\n\t\t\t[ ]\n\t\t\t\\1\n\t\t\t(\\w*\\b)\n\t\t\t/\n\t\t\t$f += !!( $2 && $3 );\n\t\t\t$1 . ( $2 || $3 )\n\t\t\t/egx\n\t\t\t\n\t\t|| !$f * \n\t\t\ts/\n\t\t\t(\\b\\w*?)\n\t\t\t(\\w+)\n\t\t\t(\\w*\\b)\n\t\t\t(.*?)\n\t\t\t(\\b\\w*?)\n\t\t\t\\2\n\t\t\t(\\w*\\b)\n\t\t\t/\n\t\t\t$f += !!( $1 && $5 || $3 && $6 );\n\t\t\t( $1 || $5 ) . $2 . ( $3 || $6 ) . $4\n\t\t\t/ex\n\t}\n\t\nprint $f + /(\\w).*\\1/ ? \"NO\" : join '', sort split"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\tmy $f = 0;\n\t\n\tif( @_ > 27 ** 2 or ( grep 26 < (length), @_ ) or ( grep /(.).*\\1/, @_ ) ){\n\t\t$f ++;\n\t\t}\n\t\n\t$debug and print \"[@_]\";\n\t\n\tif( ! $f ){\n\t\t@_ = sort @_;\n\t\t\n\t\t$debug and print \"[@_]\";\n\t\tmy @unused;\n\t\t\n\t\twhile( @_ > 1 ){\n\t\t\tmy( $A, $B ) = ( shift @_, shift @_ );\n\t\t\t\n\t\t\t$A =~ /./; \n\t\t\tmy $fA = $&;\n\t\t\t$B =~ /./; \n\t\t\tmy $fB = $&;\n\t\t\t\n\t\t\tif( $fA eq $fB ){\n\t\t\t\tmy $left;\n\t\t\t\t\n\t\t\t\tif( $A =~ /$B/ ){\n\t\t\t\t\t$left = $A;\n\t\t\t\t\t}\n\t\t\t\telsif( $B =~ /$A/ ){\n\t\t\t\t\t$left = $B;\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\t$f ++;\n\t\t\t\t\t}\n\t\t\t\t\n\t\t\t\tunshift @_, $left;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tpush @unused, $A;\n\t\t\t\tunshift @_, $B;\n\t\t\t\t}\n\t\t\t\n\t\t\t$f and last;\n\t\t\t}\n\t\t\n\t\tunshift @_, @unused;\n\t\t\n\t\t$_ = \"@_\";\n\t\t\n\t\t$debug and print;\n\t\t\n\t\t1 while 0\n\t\t\t|| !$f * \n\t\t\t\ts/\n\t\t\t\t(\\b\\w*?)\n\t\t\t\t(\\w+)\n\t\t\t\t(\\w*\\b)\n\t\t\t\t(.*?)\n\t\t\t\t(\\b\\w*?)\n\t\t\t\t\\2\n\t\t\t\t(\\w*\\b)\n\t\t\t\t/\n\t\t\t\t$debug and print \"[$1<$2>$3|$4|$5<$2>$6]\";\n\t\t\t\t$f += length( $1 ) * length( $5 ) + length( $3 ) * length( $6 );\n\t\t\t\t( $1 || $5 ) . $2 . ( $3 || $6 ) . $4\n\t\t\t\t/ex;\n\t\t\n\t\t$debug and print;\n\t\t}\n\t\n\t$debug and print;\n\t\n\tprint $f ? \"NO\" : ( join '', sort split );\n\t\n\t$debug and print '-' x 20;\n\t}"}, {"source_code": "<>;\n\n@_ = <>;\nchomp @_;\n\n$f = @_ > 27 ** 2 || grep /(.).*\\1/, @_;\n\nif( !$f ){\n\t\n\t@_ = sort @_;\n\t\t\t\n\t$_ = \"@_\";\n\t\n\t1 while 0 \n\t\t|| !$f *\n\t\t\ts/\n\t\t\t(\\b\\w+)\n\t\t\t(\\w*\\b)\n\t\t\t[ ]\n\t\t\t\\1\n\t\t\t(\\w*\\b)\n\t\t\t/\n\t\t\t$f .= $2 && $3;\n\t\t\t$1 . ( $2 || $3 )\n\t\t\t/egx\n\t\t\t\n\t\t|| !$f * \n\t\t\ts/\n\t\t\t(\\b\\w*?)\n\t\t\t(\\w+)\n\t\t\t(\\w*\\b)\n\t\t\t(.*?)\n\t\t\t(\\b\\w*?)\n\t\t\t\\2\n\t\t\t(\\w*\\b)\n\t\t\t/\n\t\t\t$f .= $1 && $5 || $3 && $6;\n\t\t\t( $1 || $5 ) . $2 . ( $3 || $6 ) . $4\n\t\t\t/ex\n\t}\n\t\nprint $f + /(\\w).*\\1/ ? \"NO\" : join '', sort split"}], "src_uid": "02fe37c2e31ca4e278d493fc2e3e35e0"} {"nl": {"description": "You are given a table $$$a$$$ of size $$$n \\times m$$$. We will consider the table rows numbered from top to bottom from $$$1$$$ to $$$n$$$, and the columns numbered from left to right from $$$1$$$ to $$$m$$$. We will denote a cell that is in the $$$i$$$-th row and in the $$$j$$$-th column as $$$(i, j)$$$. In the cell $$$(i, j)$$$ there is written a number $$$(i - 1) \\cdot m + j$$$, that is $$$a_{ij} = (i - 1) \\cdot m + j$$$.A turtle initially stands in the cell $$$(1, 1)$$$ and it wants to come to the cell $$$(n, m)$$$. From the cell $$$(i, j)$$$ it can in one step go to one of the cells $$$(i + 1, j)$$$ or $$$(i, j + 1)$$$, if it exists. A path is a sequence of cells in which for every two adjacent in the sequence cells the following satisfies: the turtle can reach from the first cell to the second cell in one step. A cost of a path is the sum of numbers that are written in the cells of the path. For example, with $$$n = 2$$$ and $$$m = 3$$$ the table will look as shown above. The turtle can take the following path: $$$(1, 1) \\rightarrow (1, 2) \\rightarrow (1, 3) \\rightarrow (2, 3)$$$. The cost of such way is equal to $$$a_{11} + a_{12} + a_{13} + a_{23} = 12$$$. On the other hand, the paths $$$(1, 1) \\rightarrow (1, 2) \\rightarrow (2, 2) \\rightarrow (2, 1)$$$ and $$$(1, 1) \\rightarrow (1, 3)$$$ are incorrect, because in the first path the turtle can't make a step $$$(2, 2) \\rightarrow (2, 1)$$$, and in the second path it can't make a step $$$(1, 1) \\rightarrow (1, 3)$$$.You are asked to tell the turtle a minimal possible cost of a path from the cell $$$(1, 1)$$$ to the cell $$$(n, m)$$$. Please note that the cells $$$(1, 1)$$$ and $$$(n, m)$$$ are a part of the way.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) \u2014 the number of test cases. The description of test cases follows. A single line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n, m \\leq 10^4$$$) \u2014 the number of rows and columns of the table $$$a$$$ respectively.", "output_spec": "For each test case output a single integer \u2014 a minimal possible cost of a path from the cell $$$(1, 1)$$$ to the cell $$$(n, m)$$$.", "sample_inputs": ["7\n\n1 1\n\n2 3\n\n3 2\n\n7 1\n\n1 10\n\n5 5\n\n10000 10000"], "sample_outputs": ["1\n12\n13\n28\n55\n85\n500099995000"], "notes": "NoteIn the first test case the only possible path consists of a single cell $$$(1, 1)$$$.The path with the minimal cost in the second test case is shown in the statement.In the fourth and the fifth test cases there is only one path from $$$(1, 1)$$$ to $$$(n, m)$$$. Both paths visit every cell in the table. "}, "positive_code": [{"source_code": "for(1..<>){($m,$n)=split/\\s/,<>;print$n*($n+$m*$m+$m-1)/2,\" \"}"}, {"source_code": "for(1..<>){($m,$n)=split/\\s/,<>;print$n*(($n-1)+$m*($m+1))/2,\" \"}"}, {"source_code": "for(1..<>){($m,$n)=split/\\s/,<>;print$n*($n-1)/2+$n*$m*($m+1)/2,\"\\n\"}"}, {"source_code": "for(1..<>){($m,$n)=split/\\s/,<>;print $n*($n-1)/2+$n*$m*($m+1)/2,\"\\n\"}"}, {"source_code": "for(1..<>){($m,$n)=split/\\s/,<>;print-1&$n*($n-1)/2+$n*$m*($m+1)/2,\"\\n\"}"}, {"source_code": "for(1..<>){($m,$n)=split/\\s/,<>;print$n*($n-1)/2+$n*$m*($m+1)/2,\" \"}"}], "negative_code": [], "src_uid": "7d774a003d2e3e8ae6fe1912b3998c96"} {"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$$$)\u00a0\u2014 the number of test cases. Next $$$2t$$$ lines contain test cases\u00a0\u2014 one per two lines. The first line of each test case contains the integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$)\u00a0\u2014 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\u00a0\u2014 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\u00a0$$$\\rightarrow$$$ 1100101\u00a0$$$\\rightarrow$$$ 110101\u00a0$$$\\rightarrow$$$ 10101\u00a0$$$\\rightarrow$$$ 1101\u00a0$$$\\rightarrow$$$ 101\u00a0$$$\\rightarrow$$$ 01."}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\n\nmy $t = int <>;\nfor (1 .. $t) {\n my $n = int <>;\n my $s = <>;\n chomp $s;\n\n $s = reverse $s;\n while ($s =~ s/0+1/0/) {}\n $s = reverse $s;\n print qq($s\\n);\n}\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)\u2009=\u20094\u2009+\u20090\u2009+\u20099\u2009+\u20098\u2009=\u200921. Then the digital root of number n equals to: dr(n)\u2009=\u2009S(n), if S(n)\u2009<\u200910; dr(n)\u2009=\u2009dr(\u2009S(n)\u2009), if S(n)\u2009\u2265\u200910. For example, dr(4098)\u2009\u2009=\u2009\u2009dr(21)\u2009\u2009=\u2009\u20093.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)\u2009\u2009=\u2009\u2009S(\u2009S(\u2009S(\u2009S(n)\u2009)\u2009)\u2009) (n\u2009\u2264\u2009101000).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\u2009\u2264\u2009k\u2009\u2264\u20091000;\u20020\u2009\u2264\u2009d\u2009\u2264\u20099).", "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)\u2009\u2009=\u2009\u2009dr(22)\u2009\u2009=\u2009\u20094.For the second test sample dr(36172)\u2009\u2009=\u2009\u2009dr(19)\u2009\u2009=\u2009\u2009dr(10)\u2009\u2009=\u2009\u20091."}, "positive_code": [{"source_code": "($k, $d) = split(' ', <>);\nif($d == 0 && $k != 1)\n{\n print 'No solution';\n}\nelse\n{\n print($d, '0'x($k - 1));\n}\n"}, {"source_code": "while(<>){\n\tchomp;\n\t/ /;\n\tif ($`>1 && $'==0) {print \"No solution\\n\"} else {\n\tprint $'.(\"0\"x($`-1));\n\tprint \"\\n\";\n\t}}"}], "negative_code": [{"source_code": "($k, $d) = split(' ', <>);\nprint($d * (10**($k - 1)));"}, {"source_code": "($k, $d) = split(' ', <>);\nif($d == 0 && $k != 1)\n{\n\tprint 'No solution';\n}\nelse\n{\n\tprint($d * (10**($k - 1)));\n}\n"}, {"source_code": "($k, $d) = split(' ', <>);\nif($d == 0 && $k != 1)\n{\n\tprint 'No solution';\n}\nelse\n{\n\tprint($d * (10**($k - 1)));\n}\n"}, {"source_code": "while(<>){\n\tchomp;\n\t/ /;\n\tprint $'.(\"0\"x($`-1));\n\tprint \"\\n\";\n\t}"}], "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$$$) \u2014 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": "#!/usr/bin/perl\n\nmy ($n,$m,$sx,$sy) = map { $_ - 0 } split(/\\s+/,);\n\nprint \"$sx $sy\\n\";\nmy $sx2 = $sx;\nfor(my $j=1;$j<=$n;$j++){\n if( $j != $sx ){\n print \"$j $sy\\n\";\n $sx2 = $j;\n }\n}\n\nfor(my $i=1;$i<=$m;$i++){\n next if $i == $sy;\n print \"$sx2 $i\\n\";\n my $sx3 = $sx2;\n for(my $j=1;$j<=$n;$j++){\n if( $j != $sx2 ){\n print \"$j $i\\n\";\n $sx3 = $j;\n }\n }\n $sx2 = $sx3;\n}\n\nexit(0);\n"}], "negative_code": [], "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$$$)\u00a0\u2014 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": "for(1..<>){$n=<>;print$n&1?\"-1 \":($n/2).\" \".($n/2).\" 0 \"}"}, {"source_code": "for(1..<>){$n=<>/2;print$n==int($n)?$n.\" \".$n.\" 0 \":\"-1 \"}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tif( $_ % 2 == 1 ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\t$_ >>= 1;\n\t\n\tif( $_ % 2 == 0 ){\n\t\tprint join ' ', $_ + 1, $_ + 1, 1;\n\t\t}\n\telse{\n\t\tprint join ' ', $_ - 1, $_ - 1, 1;\n\t\t}\n\t\n\t}"}, {"source_code": "for(1..<>){$_=<>;print$_&1?\"-1 \":($_/2).\" 0 0 \"}"}, {"source_code": "for(1..<>){$_=<>;print$_&1?\"-1 \":($_/2).\" 0 0 \"}"}, {"source_code": "for(1..<>){$_=<>;print$_&1?\"-1 \":($_/2).\" 0 0 \"}"}], "negative_code": [{"source_code": "for(1..<>){$n=<>/2;print$n==-1&$n?$n.\" \".$n.\" 0 \":\"-1 \"}"}, {"source_code": "for(1..<>){$n=<>/2;print-1&$n==$n?$n.\" \".$n.\" 0 \":\"-1 \"}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tif( $_ % 2 == 1 ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\t$_ >>= 1;\n\t\n\tprint join ' ', $_ + 1, $_ + 1, 1;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tif( $_ % 2 == 1 ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\t$_ >>= 1;\n\t\n\tprint join ' ', $_, $_ - 1, 1;\n\t}"}], "src_uid": "43041076ddd0bbfac62cd4abf4536282"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100)\u00a0\u2014 the number of game rounds. The next n lines contains rounds description. i-th of them contains pair of integers mi and ci (1\u2009\u2264\u2009mi,\u2009\u2009ci\u2009\u2264\u20096)\u00a0\u2014 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": "<>; for (<>) {\n\ts/ /-/; $r += eval() <=> 0;\n}\n\nprint [\"Friendship is magic!^^\", \"Mishka\", \"Chris\"]->[$r <=> 0];"}, {"source_code": "my $n = <>;\nmy $z = 0;\nfor (1..$n) {\n my ($m, $c) = split ' ', <>;\n $z -= ($m <=> $c);\n}\n\n$z = ($z <=> 0);\nmy @words = ('Mishka', 'Friendship is magic!^^', 'Chris');\nprint $words[$z + 1], \"\\n\";\n"}, {"source_code": "use strict;\nuse warnings;\n\nuse Data::Dumper;\n\n\nmy $n = <>;\nchomp $n;\n\ndie ('wrong n..') unless $n >= 1;\n\nmy %player_wins;\n$player_wins{Mishka} = 0;\n$player_wins{Chris} = 0;\n\nfor (my $i=0; $i<$n; $i++) {\n my $in = <>;\n chomp $in;\n my ($m , $c) = split(' ', $in);\n\n if ($m > $c) {\n $player_wins{Mishka}++;\n } elsif($c > $m) {\n $player_wins{Chris}++;\n }\n}\n\nif ($player_wins{Mishka} > $player_wins{Chris}) {\n print 'Mishka';\n} elsif($player_wins{Chris} > $player_wins{Mishka}) {\n print 'Chris';\n} else {\n print 'Friendship is magic!^^';\n}\n\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\n# send argument as array, not reference, to function requiring list\n \n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\n# solve\n\nmy $a = \"Mishka\";\nmy $b = \"Chris\";\nmy $c = \"Friendship is magic!^^\";\n\nmy $n = read_token;\n\nmy $balance = 0;\n\nfor (1..$n) {\n my $p = read_token;\n my $q = read_token;\n $balance++ if $p > $q;\n $balance-- if $p < $q;\n}\n\nsay $a if $balance > 0;\nsay $b if $balance < 0;\nsay $c if $balance == 0;\n"}, {"source_code": "# cf703a\nmy $n=<>;\nmy $s;\nforeach (<>) {\n my ($m,$c)=split(' ',$_);\n if($m>$c) {++$s;}\n elsif($m<$c) {--$s;}\n}\n\nprint $s>0 ? 'Mishka' : $s<0 ? 'Chris' : 'Friendship is magic!^^';\n"}], "negative_code": [{"source_code": "<>; $/ = \"\"; $_ = <>; tr/ \\n/-+/;\nprint [\"Friendship is magic!^^\", \"Mishka\", \"Chris\"]->[eval() <=> 0];"}, {"source_code": "use strict;\nuse warnings;\n\nuse Data::Dumper;\n\n\nmy $n = <>;\nchomp $n;\n\ndie ('wrong n..') unless $n >= 1;\n\nmy %player_wins;\n$player_wins{Mishka} = 0;\n$player_wins{Chris} = 0;\n\nfor (my $i=0; $i<$n; $i++) {\n my $in = <>;\n chomp $in;\n my ($m , $c) = split(' ', $in);\n\n if ($m > $c) {\n $player_wins{Mishka}++;\n } else {\n $player_wins{Chris}++;\n }\n}\n\nif ($player_wins{Mishka} > $player_wins{Chris}) {\n print 'Mishka';\n} elsif($player_wins{Chris} > $player_wins{Mishka}) {\n print 'Chris';\n} else {\n print 'Friendship is magic!^^';\n}\n\n"}, {"source_code": "use strict;\nuse warnings;\n\n\n\n\nmy $n = <>;\nchomp $n;\n\ndie ('wrong n..') unless $n >= 1;\n\nmy %player_wins;\n$player_wins{Mishka} = 0;\n$player_wins{Chris} = 0;\n$player_wins{draw} = 0;\n\nfor (my $i=0; $i<$n; $i++) {\n my $in = <>;\n chomp $in;\n my ($m , $c) = split(' ', $in);\n\n\n\n if ($m == $c) {\n $player_wins{draw}++;\n } elsif ($m > $c) {\n $player_wins{Mishka}++;\n } else {\n $player_wins{Chris}++;\n }\n}\n\nmy $max = 'draw';\n\n if ($player_wins{Mishka} > $player_wins{$max}) {\n $max = 'Mishka';\n }\n if ($player_wins{Chris} > $player_wins{$max}) {\n $max = 'Chris';\n }\n if ($player_wins{Chris} == $player_wins{Mishka}) {\n $max = 'draw';\n }\nprint $max;\n"}, {"source_code": "use strict;\nuse warnings;\n\n\n\n\nmy $n = <>;\nchomp $n;\n\ndie ('wrong n..') unless $n >= 1;\n\nmy %player_wins;\n$player_wins{Mishka} = 0;\n$player_wins{Chris} = 0;\n$player_wins{draw} = 0;\n\nfor (my $i=0; $i<$n; $i++) {\n my $in = <>;\n chomp $in;\n my ($m , $c) = split(' ', $in);\n\n\n\n if ($m == $c) {\n $player_wins{draw}++;\n } elsif ($m > $c) {\n $player_wins{Mishka}++;\n } else {\n $player_wins{Chris}++;\n }\n}\n\nmy $max = 'draw';\n\n if ($player_wins{Mishka} > $player_wins{$max}) {\n $max = 'Mishka';\n }\n if ($player_wins{Chris} > $player_wins{$max}) {\n $max = 'Chris';\n }\n if ($player_wins{Chris} == $player_wins{Mishka}) {\n $max = 'draw';\n }\nmy $out = ($max eq 'draw') ? 'Friendship is magic!^^' : $max;\n\nprint $out;\n"}, {"source_code": "use strict;\nuse warnings;\n\nuse Data::Dumper;\n\n# rows 10\n# per row: 2x2\n# find to seats next to each other...\n\n\nmy $n = <>;\nchomp($n);\n\ndie('wrong number or rows') unless $n >=0 && $n <= 100;\n\n# store input as array with length n * 2\n# loop array, find first seat with OO.\n# found: change 00 to ++\n# exit and print result\n\nmy @buss;\nmy @left;\nmy @right;\nfor (0..$n-1) {\n my $in = <>;\n chomp($in);\n my @seats = split('\\|', $in);\n# print Dumper \\@seats;\n push @left, $seats[0];\n push @right, $seats[1];\n}\n@buss = (@left, @right);\n\nfor (@buss) {\n if ($_ eq 'OO') {\n print \"YES\\n\";\n $_ = '++';\n print_bus(\\@buss, $n);\n exit;\n }\n}\nprint \"NO\\n\";\n\n\nsub print_bus {\n my ($buss, $n) = @_;\n\n for (my $i=0; $i<$n; $i++) {\n my $row = $buss->[$i] . '|' . $buss->[$n + $i];\n print $row . \"\\n\";\n }\n}\n\n"}, {"source_code": "use strict;\nuse warnings;\n\n\n\n\nmy $n = <>;\nchomp $n;\n\ndie ('wrong n..') unless $n >= 1;\n\nmy %player_wins;\nfor (my $i=0; $i<$n; $i++) {\n my $in = <>;\n chomp $in;\n my ($m , $c) = split(' ', $in);\n\n if ($m == $c) {\n $player_wins{draw}++;\n } elsif ($m > $c) {\n $player_wins{Mishka}++;\n } else {\n $player_wins{Chris}++;\n }\n}\n\nmy @sorted = sort { $player_wins{$b} <=> $player_wins{$a} } keys %player_wins;\nmy $winner = shift @sorted;\n\nmy $out = ($winner eq 'draw') ? 'Friendship is magic!^^' : $winner;\n\nprint $out;\n"}], "src_uid": "76ecde4a445bbafec3cda1fc421e6d42"} {"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 \u2014 n (1\u2009\u2264\u2009n\u2009\u2264\u20095\u00b7105). Each of the next n lines contains an integer si \u2014 the size of the i-th kangaroo (1\u2009\u2264\u2009si\u2009\u2264\u2009105).", "output_spec": "Output a single integer \u2014 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": "my $num = <>;\nchomp $num;\n\nmy @kangaroo;\nfor (1..$num) {\n my $size = <>;\n chomp $size;\n push @kangaroo, $size;\n}\n@kangaroo = sort {$a<=>$b} @kangaroo;\n\nmy $count = $num;\nmy $low = int($num/2) - 1;\nmy $high = $num - 1;\nwhile ($low>=0) {\n if (2 * $kangaroo[$low] <= $kangaroo[$high]) {\n $count--;\n $high--;\n }\n $low--;\n}\n\nprint $count;"}], "negative_code": [{"source_code": "my $num = <>;\nchomp $num;\n\nmy @kangaroo;\nfor (1..$num) {\n my $size = <>;\n chomp $size;\n push @kangaroo, $size;\n}\n@kangaroo = sort {$b<=>$a} @kangaroo;\n\nmy $small_i = 0;\nif (2 * $kangaroo[-1] <= $kangaroo[0]){\n $small_i++ while 2 * $kangaroo[$small_i] > $kangaroo[0];\n \n my $big_i = $small_i-1;\n while ($big_i >= 0 && $small_i <= $#kangaroo) {\n for my $i($small_i..$#kangaroo) {\n if ($kangaroo[$big_i] >= $kangaroo[$i] * 2) {\n splice @kangaroo, $i, 1;\n last;\n }\n }\n $big_i--;\n }\n}\nprint scalar @kangaroo;"}], "src_uid": "361f65484d86051fa9ff013f5e8c9154"} {"nl": {"description": "Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates x1,\u2009x2,\u2009...,\u2009xn. Vasya starts at the point with coordinate a. His goal is to visit at least n\u2009-\u20091 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000, \u2009-\u20091\u2009000\u2009000\u2009\u2264\u2009a\u2009\u2264\u20091\u2009000\u2009000)\u00a0\u2014 the number of checkpoints and Vasya's starting position respectively. The second line contains n integers x1,\u2009x2,\u2009...,\u2009xn (\u2009-\u20091\u2009000\u2009000\u2009\u2264\u2009xi\u2009\u2264\u20091\u2009000\u2009000)\u00a0\u2014 coordinates of the checkpoints.", "output_spec": "Print one integer\u00a0\u2014 the minimum distance Vasya has to travel in order to visit at least n\u2009-\u20091 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\u2009-\u200910\u2009=\u20092) and then proceed to the second one (distance is 12\u2009-\u20097\u2009=\u20095). The total distance is equal to 2\u2009+\u20095\u2009=\u20097.In the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point \u2009-\u200910."}, "positive_code": [{"source_code": "$/ = \"\"; ($n, $x, @x) = split \" \", <>;\nsub num { $a <=> $b };\nif ($n==1) {\n\t@r=(0)\n} elsif ($n==2) {\n\t@r=map abs($_-$x), @x;\t\n} else {\n $c{$_}++ for @x; @x = sort num keys %c;\n ($l1, $l2, $h1, $h2) = @x[0,1,-2,-1];\n $l2 = $l1 if $c{$l1} > 1;\n $h1 = $h2 if $c{$h2} > 1;\n @r = (\tabs($x - $l1) + $h1 - $l1,\n \tabs($h1 - $x) + $h1 - $l1,\n \tabs($x - $l2) + $h2 - $l2,\n \tabs($h2 - $x) + $h2 - $l2 );\n}\n@r = sort num @r;\nprint $r[0]\n"}], "negative_code": [{"source_code": "$/ = \"\"; ($n, $x, @x) = split \" \", <>;\nsub num { $a <=> $b };\nif ($n==1) {\n\t@r=(0)\n} elsif ($n==2) {\n\t@r=map abs($_-$x), @x;\t\n} else {\n $x{$_} = 1 for @x; @x = sort num keys %x;\n ($l1, $l2, $h1, $h2) = @x[0,1,-2,-1];\n @r = (\tabs($x - $l1) + $h1 - $l1,\n \tabs($h1 - $x) + $h1 - $l1,\n \tabs($x - $l2) + $h2 - $l2,\n \tabs($h2 - $x) + $h2 - $l2 );\n}\n@r = sort num @r;\nprint $r[0]\n"}], "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)$$$ \u2014 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)$$$ \u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my @sa = sort { $a <=> $b } @A;\r\n my @o = (); $#o = (2 * $n - 1);\r\n for(my $i=0;$i<$n;$i++){\r\n my $j = 2 * $i;\r\n $o[$j] =$sa[$i];\r\n $o[1+$j] =$sa[$n+$i];\r\n }\r\n print ( join(' ',@o) . \"\\n\" );\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "src_uid": "4296da660a39a6e98b41387929701c0a"} {"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 \u2014 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$$$) \u2014 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": "my $line = ;\nchomp $line;\nmy @numbers = split ' ', $line;\n\nmy $max = 0;\nmy $idx = 0;\nfor (@numbers) {\n $max = $idx if $_ > $numbers[$max];\n $idx++;\n}\n\nmy @output;\n$idx = 0;\nfor (@numbers) {\n if ($idx != $max) {\n push @output, $numbers[$max] - $_;\n }\n $idx++;\n}\n\nprint join(' ', @output) . \"\\n\";\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nmy @sums = split q{ }, read_line();\n\n@sums = sort { $a <=> $b } @sums;\n\nmy $abc = pop @sums;\n\nmy $answer = '';\n\n$answer .= $sums[0] + $sums[1] - $abc;\n$answer .= ' ';\n$answer .= $sums[1] + $sums[2] - $abc;\n$answer .= ' ';\n$answer .= $sums[2] + $sums[0] - $abc;\n\nsay $answer;\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n"}, {"source_code": "$\\ = $/;\n\nwhile( <>) {\n @_ = sort { $a <=> $b } split;\n \n print join ' ', map $_[ 3 ] - $_[ $_ ], 0 .. 2;\n}"}], "negative_code": [{"source_code": "my $line = ;\nchomp $line;\nmy @numbers = split ' ', $line;\n\nmy $max = -1;\nmy $idx = 0;\nfor (@numbers) {\n $max = $idx if $_ > $numbers[max];\n $idx++;\n}\n\nmy @output;\n$idx = 0;\nfor (@numbers) {\n if ($idx != $max) {\n push @output, $numbers[$max] - $_;\n }\n $idx++;\n}\n\nprint join(' ', @output) . \"\\n\";\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nmy @sums = split q{ }, read_line();\n\n@sums = sort { $a <=> $b } @sums;\n\nsay @sums;\n\nmy $abc = pop @sums;\n\nmy $answer = '';\n\n$answer .= $sums[0] + $sums[1] - $abc;\n$answer .= ' ';\n$answer .= $sums[1] + $sums[2] - $abc;\n$answer .= ' ';\n$answer .= $sums[2] + $sums[0] - $abc;\n\nsay $answer;\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nmy @sums = split q{ }, read_line();\n\n@sums = sort @sums;\n\nmy $abc = pop @sums;\n\nmy $answer = '';\n\n$answer .= $sums[0] + $sums[1] - $abc;\n$answer .= ' ';\n$answer .= $sums[1] + $sums[2] - $abc;\n$answer .= ' ';\n$answer .= $sums[2] + $sums[0] - $abc;\n\nsay $answer;\n\n\n\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n"}, {"source_code": "@_ = sort { $b <=> $a } split '', <>;\n\nprint +( eval join '+', @_ ) - 2 * $_[ 0 ];"}, {"source_code": "@_ = sort { $b <=> $a } split ' ', <>;\n\nprint +( eval join '+', @_ ) - 2 * $_[ 0 ];"}], "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\u2009<\u2009n\u2009-\u20091), you can reach the tiles number i\u2009+\u20091 or the tile number i\u2009+\u20092 from it (if you stand on the tile number n\u2009-\u20091, 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\u2009+\u20091 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\u2009\u2264\u2009n\u2009\u2264\u2009103) \u2014 the boulevard's length in tiles. The second line contains n space-separated integers ai \u2014 the number of days after which the i-th tile gets destroyed (1\u2009\u2264\u2009ai\u2009\u2264\u2009103). ", "output_spec": "Print a single number \u2014 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\u2009\u2192\u20093\u2009\u2192\u20094. 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\u2009\u2192\u20093\u2009\u2192\u20095 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted."}, "positive_code": [{"source_code": "my $n = ;\nmy @arr = split(/\\s/, );\nmy $max = ($arr[0] > $arr[$n-1]) ? $arr[$n-1] : $arr[0];\nmy $min;\n\nfor(my $i=0; $i<$n-1; $i++){\n $min = ($arr[$i] > $arr[$i+1]) ? $arr[$i] : $arr[$i+1]; \n $max = ($max > $min) ? $min : $max;\n}\nprint $max;"}], "negative_code": [], "src_uid": "d526af933b5afe9abfdf9815e9664144"} {"nl": {"description": "New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1\u2009\u2264\u2009i\u2009\u2264\u2009n) book is wi.As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below. He lifts all the books above book x. He pushes book x out of the stack. He puts down the lifted books without changing their order. After reading book x, he puts book x on the top of the stack. He decided to read books for m days. In the j-th (1\u2009\u2264\u2009j\u2009\u2264\u2009m) day, he will read the book that is numbered with integer bj (1\u2009\u2264\u2009bj\u2009\u2264\u2009n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?", "input_spec": "The first line contains two space-separated integers n (2\u2009\u2264\u2009n\u2009\u2264\u2009500) and m (1\u2009\u2264\u2009m\u2009\u2264\u20091000) \u2014 the number of books, and the number of days for which Jaehyun would read books. The second line contains n space-separated integers w1,\u2009w2,\u2009...,\u2009wn (1\u2009\u2264\u2009wi\u2009\u2264\u2009100) \u2014 the weight of each book. The third line contains m space separated integers b1,\u2009b2,\u2009...,\u2009bm (1\u2009\u2264\u2009bj\u2009\u2264\u2009n) \u2014 the order of books that he would read. Note that he can read the same book more than once.", "output_spec": "Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.", "sample_inputs": ["3 5\n1 2 3\n1 3 2 3 1"], "sample_outputs": ["12"], "notes": "NoteHere's a picture depicting the example. Each vertical column presents the stacked books. "}, "positive_code": [{"source_code": "use List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nchomp (my ($n, $m) = split /\\s+/, );\nchomp (my (@w) = split /\\s+/, );\nchomp (my (@b) = split /\\s+/, );\n\nmy @order;\nmy %seen;\nforeach (@b) {\n push @order, $_ if not exists $seen{$_};\n $seen{$_}++;\n}\n\nmy @arr;\nforeach (@order) {\n push @arr, $w[$_ - 1];\n}\n\n# printf \"@arr\\n\";\nmy $sum = 0;\nmy $x;\n\nsub findIdx {\n for (my $i = 0; $i < @order; $i++) {\n return $i if ($order[$i] == $_[0])\n }\n}\n\nforeach (@b) {\n $x = &findIdx($_);\n $sum += &sum(@arr[0 .. ($x - 1)]) if ($x > 0);\n unshift @arr, splice @arr, $x, 1;\n unshift @order, splice @order, $x, 1;\n}\n\nprintf \"$sum\\n\";\n"}, {"source_code": "use warnings; use strict;\nmy ($n, $m) = split ' ', scalar<>;\n$n += 0; $m+= 0;\nmy @a = split ' ', scalar <>; $_ += 0 foreach @a;\nmy @b = split ' ', scalar <>; $_ -= 1 foreach @b;\nmy @c = (0) x $n;\nmy @after = (0) x $n;\n$after[$_] = [(0) x $n] foreach (0 .. ($n-1));\nmy $res = 0;\nforeach my $b (@b){\n $res += $c[$b];\n #print @{$after[$_]}, \"\\n\" foreach (0 .. ($n-1));\n #print join ' ', @c;\n #print \" $res\\n\";\n foreach (0 .. ($n-1)){\n $c[$_] += $a[$b] unless $after[$b][$_];\n }\n foreach (0 .. ($n-1)){\n $after[$b][$_] = 1;\n $after[$_][$b] = 0;\n }\n $c[$b] = 0;\n }\nprint \"$res\\n\";\n"}], "negative_code": [], "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$$$) \u2014 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$$$) \u2014 elements of the array $$$a$$$.", "output_spec": "Print one integer \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = sort { $a <=> $b } split ' ', <>;\n\t\n\tprint +( sort { $a <=> $b } $_[ -2 ] - $_[ 0 ], $_[ -1 ] - $_[ 1 ] )[ 0 ];\n\t}"}], "negative_code": [], "src_uid": "2eb7234904b28b4793b7c482a2370092"} {"nl": {"description": "You are given an array $$$a_{1}, a_{2}, \\ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \\leq l \\leq r \\leq n$$$) and delete integers $$$a_l, a_{l+1}, \\ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.", "input_spec": "The first line of the input contains a single integer $$$n$$$ ($$$1 \\le n \\le 2000$$$)\u00a0\u2014 the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \\ldots, a_{n}$$$ ($$$1 \\le a_{i} \\le 10^{9}$$$)\u00a0\u2014 the elements of the array. ", "output_spec": "Print a single integer\u00a0\u2014 the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.", "sample_inputs": ["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"], "sample_outputs": ["0", "2", "2"], "notes": "NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$."}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n \nchomp (my $n = );\n \nmy @array;\npush @array, split (q{ }, );\n \nmy $answer = \"INF\";\n \nfor my $i (0..$n-1) {\n my %count;\n my $prefix_exists = FALSE;\n for my $j (0..$i-1) {\n my $number = $array[$j];\n my $c = ++$count{$number};\n if ( $c == 2 ) {\n $prefix_exists = TRUE;\n last;\n }\n }\n\n my $suffix_index = $n;\n\n for my $j (reverse $i..$n-1) {\n my $number = $array[$j];\n my $c = ++$count{$number};\n if ( $c == 1 ) {\n $suffix_index = $j;\n }\n else {\n last;\n }\n }\n \n if ( !$prefix_exists ) {\n $answer = min($answer, $suffix_index-$i);\n }\n}\n \nsay $answer;\n\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n \nchomp (my $n = );\n \nmy @array = (0);\npush @array, split (q{ }, );\n \nmy $l = 0;\nmy $r = 0;\nmy %count;\n \nmy $answer = \"INF\";\n \nfor my $i (1..$n) {\n my $prefix_exists = FALSE;\n for my $j (1..$i) {\n my $number = $array[$j];\n my $c = ++$count{$number};\n if ( $c == 2 ) {\n $prefix_exists = TRUE;\n last;\n }\n }\n\n my $suffix_index = $n;\n\n for my $j (reverse $i..$n) {\n my $number = $array[$j];\n my $c = ++$count{$number};\n if ( $c == 1 ) {\n $suffix_index = $j;\n }\n else {\n last;\n }\n }\n \n if ( !$prefix_exists ) {\n $answer = min($answer, $suffix_index-$i-1);\n }\n\n %count = ();\n}\n \nsay ($answer < 0 ? 0 : $answer);\n\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n \nchomp (my $n = );\n \nmy @array = (0);\npush @array, split (q{ }, );\n \nmy $l = 0;\nmy $r = 0;\nmy %count;\n \nmy $answer = \"INF\";\n \nfor my $i (1..$n) {\n my $prefix_exists = FALSE;\n for my $j (1..$i) {\n my $number = $array[$j];\n my $c = ++$count{$number};\n if ( $c == 2 ) {\n $prefix_exists = TRUE;\n last;\n }\n }\n\n my $suffix_index = $n+1;\n\n for my $j (reverse $i..$n) {\n my $number = $array[$j];\n my $c = ++$count{$number};\n if ( $c == 1 ) {\n $suffix_index = $j;\n }\n else {\n last;\n }\n }\n \n if ( !$prefix_exists ) {\n $answer = min($answer, $suffix_index-$i-1);\n }\n\n %count = ();\n}\n \nsay ($answer < 0 ? 0 : $answer);\n\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nchomp (my $n = );\n\nmy @array = (0);\npush @array, split (q{ }, );\n\nmy $l = 0;\nmy $r = 0;\nmy %count;\n\nmy $answer = 0;\n\nfor (1..$n) {\n my $number = $array[$_];\n my $c = ++$count{$number};\n if ( $c >= 2 ) {\n $l = $_;\n last;\n }\n}\n\nif ( $l != 0 ) {\n for ( reverse $l+1 .. $n ) {\n my $number = $array[$_];\n my $c = ++$count{$number};\n if ( $c >= 2 ) {\n $r = $_;\n $answer = $r-$l+1;\n last;\n }\n }\n}\n\nsay $answer;"}], "src_uid": "9873a576d00630fa6e5fd4448a1a65ad"} {"nl": {"description": "There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team.The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams of three people he can form. It is possible that he can't use all groups to form teams. For groups of two, either both students should write the contest, or both should not. If two students from a group of two will write the contest, they should be in the same team.", "input_spec": "The first line contains single integer n (2\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105) \u2014 the number of groups. The second line contains a sequence of integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20092), where ai is the number of people in group i.", "output_spec": "Print the maximum number of teams of three people the coach can form.", "sample_inputs": ["4\n1 1 2 1", "2\n2 2", "7\n2 2 2 1 1 1 1", "3\n1 1 1"], "sample_outputs": ["1", "0", "3", "1"], "notes": "NoteIn the first example the coach can form one team. For example, he can take students from the first, second and fourth groups.In the second example he can't make a single team.In the third example the coach can form three teams. For example, he can do this in the following way: The first group (of two people) and the seventh group (of one person), The second group (of two people) and the sixth group (of one person), The third group (of two people) and the fourth group (of one person). "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, <> =~ /./g;\n\t\n\tprint 0 + do {\n\t\tif( $h{ 1 } < $h{ 2 } ){\n\t\t\t$h{ 1 }\n\t\t\t}\n\t\telse{\n\t\t\t$h{ 2 } + int( ( $h{ 1 } - $h{ 2 } ) / 3 )\n\t\t\t}\n\t\t};\n\t}"}], "negative_code": [], "src_uid": "6c9cbe714f8f594654ebc59b6059b30a"} {"nl": {"description": "In a new version of the famous Pinball game, one of the most important parts of the game field is a sequence of n bumpers. The bumpers are numbered with integers from 1 to n from left to right. There are two types of bumpers. They are denoted by the characters '<' and '>'. When the ball hits the bumper at position i it goes one position to the right (to the position i\u2009+\u20091) if the type of this bumper is '>', or one position to the left (to i\u2009-\u20091) if the type of the bumper at position i is '<'. If there is no such position, in other words if i\u2009-\u20091\u2009<\u20091 or i\u2009+\u20091\u2009>\u2009n, the ball falls from the game field.Depending on the ball's starting position, the ball may eventually fall from the game field or it may stay there forever. You are given a string representing the bumpers' types. Calculate the number of positions such that the ball will eventually fall from the game field if it starts at that position.", "input_spec": "The first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the length of the sequence of bumpers. The second line contains the string, which consists of the characters '<' and '>'. The character at the i-th position of this string corresponds to the type of the i-th bumper.", "output_spec": "Print one integer\u00a0\u2014 the number of positions in the sequence such that the ball will eventually fall from the game field if it starts at that position.", "sample_inputs": ["4\n<<><", "5\n>>>>>", "4\n>><<"], "sample_outputs": ["2", "5", "0"], "notes": "NoteIn the first sample, the ball will fall from the field if starts at position 1 or position 2.In the second sample, any starting position will result in the ball falling from the field."}, "positive_code": [{"source_code": "\n$,=\" \";\nmy $n=<>;\nmy $str=<>;\nchomp($str);\n$str =~ s/>+<+//g;\n\n\nprint length($str) , \"\\n\";\n"}, {"source_code": "\n$,=\" \";\nmy $n=<>;\nmy $str=<>;\nchomp($str);\nwhile(1)\n{\n last if index($str,'><')<0;\n $str =~ s/>+<+//g;\n}\n\nprint length($str) , \"\\n\";\n"}, {"source_code": "<>;\n$_ = <>;\n$L = length;\n$i = index $_, '>';\n$i < 0 and $i = $L - 1;\n$r = rindex $_, '<';\nprint( $L - $r + $i - 2 )"}, {"source_code": "<>;\n$_ = <>, chomp;\nif( /^<+/ ){\n\t$ans += length $&;\n\t}\n$_ = reverse;\nif( /^>+/ ){\n\t$ans += length $&;\n\t}\nprint 0 + $ans"}], "negative_code": [{"source_code": "\n$,=\" \";\nmy $n=<>;\nmy $str=<>;\nchomp($str);\nwhile(1)\n{\n last if index($str,'><')<0;\n $str =~ s/>;\n$_ = <>;\nif( /^<+/ ){\n\t$ans += length $&;\n\t}\n$_ = reverse;\nif( /^>+/ ){\n\t$ans += length $&;\n\t}\nprint $ans"}, {"source_code": "<>;\n$_ = <>;\n/^<+/;\n$ans += length $&;\n$_ = reverse;\n/^<+/;\n$ans += length $&;\nprint $ans"}, {"source_code": "<>;\n$_ = <>;\n/^<+/;\n$ans += length $&;\n$_ = reverse;\n/^>+/;\n$ans += length $&;\nprint $ans"}, {"source_code": "<>;\n$_ = <>;\n$L = length;\n$i = index $_, '>';\n$i < 0 and $i = $L;\n$r = rindex $_, '<';\nprint( $L - $r + $i - 1 )"}, {"source_code": "<>;\n$_ = <>, chomp;\nif( /^<+/ ){\n\t$ans += length $&;\n\t}\n$_ = reverse;\nif( /^>+/ ){\n\t$ans += length $&;\n\t}\nprint $ans"}, {"source_code": "print length join '', <>=~/^<+|>+$/g"}], "src_uid": "6b4242ae9a52d36548dda79d93fe0aef"} {"nl": {"description": "In BerSoft $$$n$$$ programmers work, the programmer $$$i$$$ is characterized by a skill $$$r_i$$$.A programmer $$$a$$$ can be a mentor of a programmer $$$b$$$ if and only if the skill of the programmer $$$a$$$ is strictly greater than the skill of the programmer $$$b$$$ $$$(r_a > r_b)$$$ and programmers $$$a$$$ and $$$b$$$ are not in a quarrel.You are given the skills of each programmers and a list of $$$k$$$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $$$i$$$, find the number of programmers, for which the programmer $$$i$$$ can be a mentor.", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ $$$(2 \\le n \\le 2 \\cdot 10^5$$$, $$$0 \\le k \\le \\min(2 \\cdot 10^5, \\frac{n \\cdot (n - 1)}{2}))$$$ \u2014 total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $$$r_1, r_2, \\dots, r_n$$$ $$$(1 \\le r_i \\le 10^{9})$$$, where $$$r_i$$$ equals to the skill of the $$$i$$$-th programmer. Each of the following $$$k$$$ lines contains two distinct integers $$$x$$$, $$$y$$$ $$$(1 \\le x, y \\le n$$$, $$$x \\ne y)$$$ \u2014 pair of programmers in a quarrel. The pairs are unordered, it means that if $$$x$$$ is in a quarrel with $$$y$$$ then $$$y$$$ is in a quarrel with $$$x$$$. Guaranteed, that for each pair $$$(x, y)$$$ there are no other pairs $$$(x, y)$$$ and $$$(y, x)$$$ in the input.", "output_spec": "Print $$$n$$$ integers, the $$$i$$$-th number should be equal to the number of programmers, for which the $$$i$$$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.", "sample_inputs": ["4 2\n10 4 10 15\n1 2\n4 3", "10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5"], "sample_outputs": ["0 0 1 2", "5 4 0 5 3 3 9 0 2 5"], "notes": "NoteIn the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel."}, "positive_code": [{"source_code": "# author : Gabriel Hofer\nuse strict;\nuse warnings;\nuse constant NL => qq/\\n/;\nsub max { $_ [ 0 ] > $_ [ 1 ] ? $_ [ 0 ] : $_ [ 1 ] }\nmy %quarrel = ();\nmy ( $n, $k ) = split qq / /, ;\nmy @skill = split qq/ /, ;\nmy @skill_sorted = sort { $a <=> $b } @skill;\n\nmy %h = ();\nAA: for ( 1 .. $k ) {\n my ( $five, $six ) = split qq/ /, ;\n if ( $skill [ $five - 1 ] > $skill [ $six - 1 ] ) { \n $quarrel { $five } = 0 if not exists $quarrel { $five };\n $quarrel { $five } += 1; \n next AA\n } elsif ( $skill [ $five - 1 ] < $skill [ $six - 1 ] ) { \n $quarrel { $six } = 0 if not exists $quarrel { $six };\n $quarrel { $six } += 1; \n next AA\n }\n}\n\nAB: for my $j ( 1 .. ($#skill + 1) ) {\n (not (my $left = 0)) and (my $right = $#skill + 1) and (my $mid);\n AC: while ( $left < $right ) { \n $mid = int ( ( $left + $right ) / 2 );\n if ( $skill_sorted [ $mid ] < ($skill [ $j - 1 ]) ) { ($left = $mid + 1) and next AC }\n else { ($right = $mid) and next AC }\n }\n $mid = int ( ( $left + $right ) / 2 );\n if ( exists $quarrel { $j } ) { print max ( 0, $mid - $quarrel { $j } ) }\n else { print $mid }\n print qq/ /;\n}\nprint NL;\n\n"}, {"source_code": "( $n, $k ) = split ' ', <>;\n\n@sA = sort { $a <=> $b } @A = split ' ', <>;\n\n$prev = $sA[ 0 ];\n\nfor $i ( 0 .. @sA - 1 ){\n\t$sA[ $i ] == $prev or $h{ $sA[ $i ] } = $i;\n\t$prev = $sA[ $i ];\n\t}\n\nfor( 1 .. $k ){\n\t( $u, $v ) = split ' ', <>;\n\t$A[ $u - 1 ] == $A[ $v - 1 ] or\n\t\t$B[ $A[ $u - 1 ] > $A[ $v - 1 ] ? $u - 1 : $v - 1 ] --;\n\t}\n\t\nprint join ' ', map { $h{ $_ } + shift @B } @A"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\tmy @A = split ' ', <>;\n\t\n\tmy @sA = sort { $a <=> $b } @A;\n\tmy @B;\n\t\n\tmy $prev = $sA[ 0 ];\n\tmy %h;\n\t\n\tfor my $i ( 0 .. @sA - 1 ){\n\t\tnext if $sA[ $i ] == $prev;\n\t\t$h{ $sA[ $i ] } = $i;\n\t\t$prev = $sA[ $i ];\n\t\t}\n\t\n\t$debug and print \"$_ => $h{ $_ }\" for sort keys %h;\n\t\n\tfor( 1 .. $k ){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t$debug and print \"$u|$v||$A[ $u - 1 ]|$A[ $v - 1 ]\";\n\t\tnext if $A[ $u - 1 ] == $A[ $v - 1 ];\n\t\t$B[ $A[ $u - 1 ] > $A[ $v - 1 ] ? $u - 1 : $v - 1 ] --;\n\t\t}\n\t\n\t$debug and print \"$_ => $h{ $_ }\" for sort keys %h;\n\t\n\tprint join ' ', map { ( $h{ $_ } || 0 ) + shift @B } @A;\n\t\n\t$debug and print '-' x 15;\n\t}"}], "negative_code": [{"source_code": "# author : Gabriel Hofer\nuse strict;\nuse warnings;\nuse constant NL => qq/\\n/;\nsub max { $_ [ 0 ] > $_ [ 1 ] ? $_ [ 0 ] : $_ [ 1 ] }\nmy %quarrel = ();\nmy ( $n, $k ) = split qq / /, ;\nmy @skill = split qq/ /, ;\nmy @skill_sorted = sort { $a <=> $b } @skill;\n\nmy %h = ();\nAA: for ( 1 .. $k ) {\n my ( $five, $six ) = ( =~ m /(\\d+)\\s(\\d+)/ );\n my $nine;\n if ( $skill [ $five - 1 ] > $skill [ $six - 1 ] ) { $nine = $five } \n else { $nine = $six }\n $quarrel { $nine } = 0 if not exists $quarrel { $nine };\n $quarrel { $nine } += 1;\n}\n\nAA: for my $j ( 1 .. ($#skill + 1) ) {\n (not (my $left = 0)) and (my $right = $#skill + 1) and (my $mid);\n AB: while ( $left < $right ) { \n $mid = int ( ( $left + $right ) / 2 );\n if ( $skill_sorted [ $mid ] < ($skill [ $j - 1 ]) ) { ($left = $mid + 1) and next AB }\n else { ($right = $mid) and next AB }\n }\n $mid = int ( ( $left + $right ) / 2 );\n if ( exists $quarrel { $j } ) { print $mid - $quarrel { $j } }\n else { print $mid }\n print qq/ /;\n}\nprint NL;\n\n\n"}, {"source_code": "# author : Gabriel Hofer\nuse strict;\nuse warnings;\nuse constant NL => qq/\\n/;\nsub max { $_ [ 0 ] > $_ [ 1 ] ? $_ [ 0 ] : $_ [ 1 ] }\nmy %quarrel = ();\nmy ( $n, $k ) = split qq / /, ;\nmy @skill = split qq/ /, ;\nmy @skill_sorted = sort { $a <=> $b } @skill;\n\nmy %h = ();\nAA: for ( 1 .. $k ) {\n my ( $five, $six ) = ( =~ m /(\\d+)\\s(\\d+)/ );\n my $nine;\n if ( $skill [ $five - 1 ] > $skill [ $six - 1 ] ) { $nine = $five } \n else { $nine = $six }\n $quarrel { $nine } = 0 if not exists $quarrel { $nine };\n $quarrel { $nine } += 1;\n}\n\nAA: for my $j ( 1 .. ($#skill + 1) ) {\n (not (my $left = 0)) and (my $right = $#skill + 1) and (my $mid);\n AB: while ( $left < $right ) { \n $mid = int ( ( $left + $right ) / 2 );\n if ( $skill_sorted [ $mid ] < ($skill [ $j - 1 ]) ) { ($left = $mid + 1) and next AB }\n else { ($right = $mid) and next AB }\n }\n $mid = int ( ( $left + $right ) / 2 );\n if ( exists $quarrel { $j } ) { print max ( 0, $mid - $quarrel { $j } ) }\n else { print $mid }\n print qq/ /;\n}\nprint NL;\n\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\tmy @A = split ' ', <>;\n\t\n\tmy @sA = sort { $a <=> $b } @A;\n\tmy @B;\n\t\n\tmy $prev = $sA[ 0 ];\n\tmy %h;\n\t\n\tfor my $i ( 0 .. @sA - 1 ){\n\t\tnext if $sA[ $i ] == $prev;\n\t\t$h{ $sA[ $i ] } = $i;\n\t\t$prev = $sA[ $i ];\n\t\t}\n\t\n\t$debug and print \"$_ => $h{ $_ }\" for sort keys %h;\n\t\n\tfor( 1 .. $k ){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t$debug and print \"$u|$v||$A[ $u - 1 ]|$A[ $v - 1 ]\";\n\t\t$B[ $A[ $u - 1 ] > $A[ $v - 1 ] ? $u - 1 : $v - 1 ] --;\n\t\t}\n\t\n\t$debug and print \"$_ => $h{ $_ }\" for sort keys %h;\n\t\n\tprint join ' ', map { ( $h{ $_ } || 0 ) + shift @B } @A;\n\t\n\t$debug and print '-' x 15;\n\t}"}], "src_uid": "4687176445ed1087864b081a181e1840"} {"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,\u2009yi).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\u2009-\u2009xj|\u2009+\u2009|yi\u2009-\u2009yj|. Daniel, as an ordinary person, calculates the distance using the formula .The success of the operation relies on the number of pairs (i,\u2009j) (1\u2009\u2264\u2009i\u2009<\u2009j\u2009\u2264\u2009n), 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\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the number of watchmen. Each of the following n lines contains two integers xi and yi (|xi|,\u2009|yi|\u2009\u2264\u2009109). 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\u2009-\u20097|\u2009+\u2009|1\u2009-\u20095|\u2009=\u200910 for Doctor Manhattan and for Daniel. For pairs (1,\u20091), (1,\u20095) and (7,\u20095), (1,\u20095) Doctor Manhattan and Daniel will calculate the same distances."}, "positive_code": [{"source_code": ";\nmy $C = [];\nwhile(){\n chomp $_;\n my ($x, $y) = split(' ', $_);\n push @$C, [$x, $y];\n}\n\nmy ($X, $Y, $XY) = ({}, {}, {});\nforeach(@$C){\n ($X->{$_->[0]} ||= 0)++;\n ($Y->{$_->[1]} ||= 0)++;\n ($XY->{\"$_->[0]:$_->[1]\"} ||= 0)++;\n}\n\nmy $ans = 0;\n$ans += &nC2($_) for values %$X;\n$ans += &nC2($_) for values %$Y;\n$ans -= &nC2($_) for values %$XY;\n\nprint(sprintf(\"%d\\n\", $ans));\n\nsub nC2{\n my $n = shift;\n return int($n * ($n-1) / 2);\n}"}, {"source_code": "#\n# Hello World Program in Perl\n#\nuse strict;\n\nmy $count = <>;\nmy (%hf, %hs);\nmy %pair;\nmy %all;\nfor(1..$count)\n{\n my ($a, $b) = split /\\s/, <>;\n \n $hf{$a}++;\n $hs{$b}++;\n \n if($a == $b)\n {\n $pair{$a}++;\n }\n else\n {\n $all{\"[$a][$b]\"}++;\n }\n}\nmy $ans = 0;\n\nmy @toDel;\nfor(keys(%hf))\n{\n $ans += f($hf{$_});\n \n}\n\nfor(keys(%hs))\n{\n $ans += f($hs{$_});\n \n}\n\nfor(keys(%pair))\n{\n $ans -=f($pair{$_});\n}\n\n\nfor(keys(%all))\n{\n $ans-=f($all{$_});\n}\n\n\n\nprint $ans;\n\n\nsub f($)\n{\n my $ans = 0;\n for(1..$_[0]-1)\n {\n $ans += $_; \n }\n return $ans;\n}\n\n"}], "negative_code": [], "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": "#!/usr/bin/perl\n\nmy ($n) = map { $_ - 0 } split(/\\s+/,);\n\nmy @aa = map { $_ - 0 } split(/\\s+/,);\n\nmy @ca = ();\n$#ca = 100000;\n\nfor(my $i=0;$i<$n;$i++){\n $ca[$aa[$i]] += 1;\n}\n\nmy @ca2 = ();\n$#ca2 = 100000;\n\nmy $up2 = 0;\nmy $up4 = 0;\nmy $up6 = 0;\nmy $up8 = 0;\n\nfor(my $i=0;$i<=100000;$i++){\n $ca2[$ca[$i]] += 1;\n $up2 ++ if( $ca[$i]>=2 );\n $up4 ++ if( $ca[$i]>=4 );\n $up6 ++ if( $ca[$i]>=6 );\n $up8 ++ if( $ca[$i]>=8 );\n}\n\nmy ($q) = map { $_ - 0 } split(/\\s+/,);\n\nfor(my $i=0;$i<$q;$i++){\n my ($flg,$k) = split(/\\s+/,);\n $k = $k - 0;\n my $old_n = $ca[$k];\n $ca[$k] += 1 if( $flg eq '+' );\n $ca[$k] -= 1 if( $flg eq '-' );\n my $new_n = $ca[$k];\n $ca2[$old_n] --;\n $up2 -- if ( $old_n>=2 );\n $up4 -- if ( $old_n>=4 );\n $up6 -- if ( $old_n>=6 );\n $up8 -- if ( $old_n>=8 );\n $ca2[$new_n] ++;\n $up2 ++ if ( $new_n>=2 );\n $up4 ++ if ( $new_n>=4 );\n $up6 ++ if ( $new_n>=6 );\n $up8 ++ if ( $new_n>=8 );\n \n my $r = 'NO';\n $r = 'YES' if $up8 >= 1;\n $r = 'YES' if $up6 >= 1 and $up2 >= 2;\n $r = 'YES' if $up4 >= 1 and $up2 >= 3;\n $r = 'YES' if $up4 >= 2;\n print \"$r\\n\";\n \n}\n\nexit(0);\n\n"}], "negative_code": [], "src_uid": "d14bad9abf2a27ba57c80851405a360b"} {"nl": {"description": "Ela loves reading a lot, just like her new co-workers in DTL! On her first day after becoming an engineer in DTL, she is challenged by a co-worker to sort a heap of books into different compartments on the shelf.$$$n$$$ books must be split into $$$k$$$ compartments on the bookshelf ($$$n$$$ is divisible by $$$k$$$). Each book is represented by a lowercase Latin letter from 'a' to 'y' inclusively, which is the beginning letter in the title of the book.Ela must stack exactly $$$\\frac{n}{k}$$$ books in each compartment. After the books are stacked, for each compartment indexed from $$$1$$$ to $$$k$$$, she takes the minimum excluded (MEX) letter of the multiset of letters formed by letters representing all books in that compartment, then combines the resulting letters into a string. The first letter of the resulting string is the MEX letter of the multiset of letters formed by the first compartment, the second letter of the resulting string is the MEX letter of the multiset of letters formed by the second compartment, ... and so on. Please note, under the constraint of this problem, MEX letter can always be determined for any multiset found in this problem because 'z' is not used.What is the lexicographically greatest resulting string possible that Ela can create?A string $$$a$$$ is lexicographically greater than a string $$$b$$$ if and only if one of the following holds: $$$b$$$ is a prefix of $$$a$$$, but $$$b \\ne a$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears later in the alphabet than the corresponding letter in $$$b$$$. The minimum excluded (MEX) letter of a multiset of letters is the letter that appears earliest in the alphabet and is not contained in the multiset. For example, if a multiset of letters contains $$$7$$$ letters 'b', 'a', 'b', 'c', 'e', 'c', 'f' respectively, then the MEX letter of this compartment is 'd', because 'd' is not included in the multiset, and all letters comes before 'd' in the alphabet, namely 'a', 'b' and 'c', are included in the multiset.", "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 n \\le 200$$$; $$$1 \\le k \\le n$$$). It is guaranteed that $$$n$$$ is divisible by $$$k$$$. The second line of each test case contains a string of $$$n$$$ lowercase Latin letters from 'a' to 'y' inclusively. Each letter represents the starting letter of the title of a book in the initial heap. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$.", "output_spec": "For each test case, output a string of $$$k$$$ letters which is the most optimal string that Ela can find.", "sample_inputs": ["5\n\n12 3\n\ncabccadabaac\n\n12 6\n\ncabccadabaac\n\n12 12\n\ncabccadabaac\n\n25 1\n\nabcdefghijklmnopqrstuvwxy\n\n10 5\n\nbcdxedbcfg"], "sample_outputs": ["edb\nccbbba\nbbbbbaaaaaaa\nz\naaaaa"], "notes": "NoteIn the first test case, the books can be divided into $$$3$$$ compartments as below: the first compartment contains the books with indices $$$1, 2, 3, 7$$$: $$$multiset_1 = \\{$$$'c', 'a', 'b', 'd'$$$\\}$$$ $$$\\rightarrow$$$ $$$MEX(multiset_1) =$$$ 'e' the second compartment contains the books with indices $$$4, 5, 6, 9$$$ : $$$multiset_2 = \\{$$$'c', 'c', 'a', 'b'$$$\\}$$$ $$$\\rightarrow$$$ $$$MEX(multiset_2) =$$$ 'd' the third compartment contains the remaining books $$$8, 10, 11, 12$$$ : $$$multiset_3 = \\{$$$ 'a', 'a', 'a', 'c'$$$\\}$$$ $$$\\rightarrow$$$ $$$MEX(multiset_3) =$$$ 'b' Therefore, the answer is 'edb'. It can be proven that there is no way that Ela can arrange the books so that it results in a lexicographically greater string. "}, "positive_code": [{"source_code": "#!/bin/perl\r\n\r\nuse strict;\r\n\r\nmy $t = ;\r\n\r\nmy @alphabet = ('a'..'z');\r\n\r\nfor my $tn (1..$t){\r\n # 12, 3\r\n my ($n, $k) = split / /, ;\r\n \r\n # 4\r\n my $l = $n / $k;\r\n \r\n # cabccadabaac\r\n my $string = ;\r\n \r\n chomp $string;\r\n\r\n my @letters = split //, $string;\r\n \r\n my %count;\r\n for my $l (@letters){\r\n $count{$l}++;\r\n }\r\n \r\n \r\n my @final_list;\r\n for (my $row = 0; $row < $k; $row++) {\r\n my $col = 0;\r\n my $symb;\r\n for my $char (@alphabet){\r\n last if $col > $l;\r\n if ($count{$char} > 0){\r\n $symb = $char;\r\n $count{$char}--;\r\n $col++;\r\n next;\r\n }\r\n $symb = $char;\r\n last;\r\n \r\n }\r\n\r\n push @final_list, $symb;\r\n }\r\n \r\n \r\n @final_list = sort {$b cmp $a} @final_list;\r\n \r\n # edb\r\n \r\n print join \"\", @final_list;\r\n print \"\\n\";\r\n}\r\n\r\n"}], "negative_code": [{"source_code": "#!/bin/perl\n\nuse strict;\nuse Data::Dumper 'Dumper';\n\nmy $t = ;\n\nmy @alphabet = ('a'..'z');\n\nfor my $tn (1..$t){\n # 12, 3\n my ($n, $k) = split / /, ;\n \n # 4\n my $l = $n / $k;\n \n # cabccadabaac\n my $string = ;\n \n chomp $string;\n\n my @letters = split //, $string;\n \n @letters = sort @letters;\n \n my %count;\n for my $l (@letters){\n $count{$l}++;\n }\n \n \n my @lists;\n \n \n while ($k > 0){\n my $l1 = $l;\n my @list;\n while ($l1 > 0){\n for my $letter (sort {$a cmp $b || $count{$a} <=> $count{$b}} keys %count){\n if ($count{$letter} > 0){\n $count{$letter}--;\n push @list, $letter;\n $l1--;\n }\n if ($l1 == 0){\n push @lists, \\@list;\n last ;\n }\n }\n\n }\n $k--;\n }\n \n my @final_list;\n for my $list (@lists){\n for my $letter (@alphabet){\n if (grep($letter eq $_, @$list) == 0){\n push @final_list, $letter;\n last;\n }\n }\n }\n \n @final_list = sort {$b cmp $a} @final_list;\n \n # edb\n \n print join \"\", @final_list;\n print \"\\n\";\n}\n\n"}, {"source_code": "#!/bin/perl\n\nuse strict;\nuse Data::Dumper 'Dumper';\n\nmy $t = ;\n\nmy @alphabet = ('a'..'z');\n\nfor my $tn (1..$t){\n # 12, 3\n my ($n, $k) = split / /, ;\n \n # 4\n my $l = $n / $k;\n \n # cabccadabaac\n my $string = ;\n \n chomp $string;\n\n my @letters = split //, $string;\n \n @letters = sort @letters;\n \n my %count;\n for my $l (@letters){\n $count{$l}++;\n }\n \n \n my @lists;\n \n \n while ($k > 0){\n my $l1 = $l;\n my @list;\n while ($l1 > 0){\n for my $letter (sort {$count{$b} <=> $count{$a}} keys %count){\n if ($count{$letter} > 0){\n $count{$letter}--;\n push @list, $letter;\n $l1--;\n }\n if ($l1 == 0){\n push @lists, \\@list;\n last ;\n }\n }\n\n }\n $k--;\n }\n \n my @final_list;\n for my $list (@lists){\n for my $letter (@alphabet){\n if (grep($letter eq $_, @$list) == 0){\n push @final_list, $letter;\n last;\n }\n }\n }\n \n @final_list = sort {$b cmp $a} @final_list;\n \n # edb\n \n print join \"\", @final_list;\n print \"\\n\";\n}\n\n"}, {"source_code": "#!/bin/perl\n\nuse strict;\n\nmy $t = ;\n\nmy @alphabet = ('a'..'z');\n\nfor my $tn (1..$t){\n # 12, 3\n my ($n, $k) = split / /, ;\n \n # 4\n my $l = $n / $k;\n \n # cabccadabaac\n my $string = ;\n \n chomp $string;\n\n my @letters = split //, $string;\n \n my %count;\n for my $l (@letters){\n $count{$l}++;\n }\n \n \n my @lists;\n \n while ($k > 0){\n my $l1 = $l;\n my @list;\n my $stage = 1;\n while ($l1 > 0){\n if ($stage == 1){\n for my $letter (sort {$a cmp $b} keys %count){\n if ($count{$letter} > 0){\n $count{$letter}--;\n push @list, $letter;\n $l1--;\n }\n if ($l1 == 0){\n push @lists, \\@list;\n last ;\n }\n }\n }\n if ($stage > 1){\n for my $letter (sort {$a cmp $b} keys %count){\n while ($count{$letter} > 0){\n $count{$letter}--;\n push @list, $letter;\n $l1--;\n if ($l1 == 0){\n last ;\n }\n }\n if ($l1 == 0){\n push @lists, \\@list;\n last ;\n }\n }\n }\n $stage++;\n\n }\n $k--;\n }\n \n my @final_list;\n for my $list (@lists){\n for my $letter (@alphabet){\n if (grep($letter eq $_, @$list) == 0){\n push @final_list, $letter;\n last;\n }\n }\n }\n \n @final_list = sort {$b cmp $a} @final_list;\n \n # edb\n \n print join \"\", @final_list;\n print \"\\n\";\n}\n\n"}, {"source_code": "#!/bin/perl\n\nuse strict;\n\nmy $t = ;\n\nmy @alphabet = ('a'..'z');\n\nfor my $tn (1..$t){\n # 12, 3\n my ($n, $k) = split / /, ;\n \n # 4\n my $l = $n / $k;\n \n # cabccadabaac\n my $string = ;\n \n chomp $string;\n\n my @letters = split //, $string;\n \n my %count;\n for my $l (@letters){\n $count{$l}++;\n }\n \n \n my @lists;\n \n while ($k > 0){\n my $l1 = $l;\n my @list;\n while ($l1 > 0){\n for my $letter (sort {$a cmp $b} keys %count){\n if ($count{$letter} > 0){\n $count{$letter}--;\n push @list, $letter;\n $l1--;\n }\n if ($l1 == 0){\n push @lists, \\@list;\n last ;\n }\n \n }\n }\n $k--;\n }\n \n my @final_list;\n for my $list (@lists){\n for my $letter (@alphabet){\n if (grep($letter eq $_, @$list) == 0){\n push @final_list, $letter;\n last;\n }\n }\n }\n \n @final_list = sort {$b cmp $a} @final_list;\n \n # edb\n \n print join \"\", @final_list;\n print \"\\n\";\n}\n\n"}], "src_uid": "9c86925036cd1f83273bc21e2ea3e5c8"} {"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$$$) \u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n ( my $c = ) =~ s/[\\x00-\\x20]+$//ios;\r\n my $r = 'NO';\r\n for(my $i=0;$i);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$l,$r) = map { $_ - 0 } split(/\\s+/,);\r\n my @c = map { $_ - 0 } split(/\\s+/,);\r\n $#c = $n - 1;\r\n if( $r > $l ){\r\n ($l,$r) = ($r,$l);\r\n @c = reverse @c;\r\n }\r\n # \r\n my @d = ();\r\n for(my $i=0;$i<=$n;$i++){\r\n push(@d,+[$i,0,0,0]);\r\n }\r\n for(my $i=0;$i<$n;$i++){\r\n if( $i<$l ){\r\n $d[$c[$i]]->[1] += 1;\r\n $d[$c[$i]]->[3] -= 1;\r\n } else {\r\n $d[$c[$i]]->[2] += 1;\r\n $d[$c[$i]]->[3] += 1;\r\n }\r\n }\r\n my $chg = ($l - $r)/2;\r\n my @d2 = sort { $a->[3] <=> $b->[3] } @d;\r\n #print &Dumper(\\@d2);\r\n my $cost = 0;\r\n for(my $i=0;$i<=$n;$i++){ # \r\n if( $d2[$i]->[1] > $d2[$i]->[2] ){\r\n my $v = - $d2[$i]->[3];\r\n if( $v >= 2 and $chg > 0 ){\r\n my $v2 = &min($v / 2,$chg);\r\n $chg -= $v2;\r\n $d2[$i]->[1] -= $v2;\r\n $d2[$i]->[2] += $v2;\r\n $d2[$i]->[3] += 2*$v2;\r\n $cost += $v2;\r\n }\r\n }\r\n }\r\n #print \"cost1 = $cost\\n\";\r\n # \r\n my $l1 = 0; my $r1 = 0;\r\n for(my $i=0;$i<=$n;$i++){\r\n my $v = $d2[$i]->[1] - $d2[$i]->[2];\r\n if( $v > 0 ){\r\n $l1 += $v;\r\n } elsif( $v < 0 ){\r\n $r1 += (-$v);\r\n }\r\n }\r\n my $cchg = &min($l1,$r1);\r\n #print \"cchg = $cchg\\n\";\r\n my $sto = 0;\r\n my $mt = 0;\r\n for(my $i=0;$i<=$n;$i++){\r\n my $v = $d2[$i]->[1] - $d2[$i]->[2];\r\n if( $v > 0 ){\r\n my $v2 = &min($cchg,$v);\r\n $d2[$i]->[1] -= $v2;\r\n $cchg -= $v2;\r\n $sto += $v2;\r\n } elsif( $v < 0 ){\r\n my $v2 = &min($sto,-$v);\r\n $d2[$i]->[1] += $v2;\r\n $sto -= $v2;\r\n $cost += $v2;\r\n }\r\n }\r\n #print \"cost2 = $cost\\n\";\r\n # \r\n $sto = 0;\r\n for(my $i=0;$i<=$n;$i++){\r\n my $v = $d2[$i]->[1] - $d2[$i]->[2];\r\n if( $v > 0 ){\r\n if( $sto > 0 ){\r\n my $v2 = &min($sto,$v);\r\n $d2[$i]->[2] += $v2;\r\n $sto -= $v2;\r\n $cost += 2 * $v2;\r\n } else {\r\n my $v2 = &min($chg,$v);\r\n $d2[$i]->[1] -= $v2;\r\n $chg -= $v2;\r\n $sto += $v2;\r\n }\r\n }\r\n }\r\n print \"$cost\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub min {\r\n my ($x,$y) = @_;\r\n return ( $x > $y ? $y : $x );\r\n}\r\n\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$l,$r) = map { $_ - 0 } split(/\\s+/,);\r\n my @c = map { $_ - 0 } split(/\\s+/,);\r\n $#c = $n - 1;\r\n if( $r > $l ){\r\n ($l,$r) = ($r,$l);\r\n @c = reverse @c;\r\n }\r\n my @d = ();\r\n for(my $i=0;$i<=$n;$i++){\r\n push(@d,+[$i,0,0,0]);\r\n }\r\n for(my $i=0;$i<$n;$i++){\r\n if( $i<$l ){\r\n $d[$c[$i]]->[1] += 1;\r\n $d[$c[$i]]->[3] -= 1;\r\n } else {\r\n $d[$c[$i]]->[2] += 1;\r\n $d[$c[$i]]->[3] -= 1;\r\n }\r\n }\r\n my $chg = ($l - $r)/2;\r\n my @d2 = sort { $a->[3] <=> $b->[3] } @d;\r\n my $cost = 0;\r\n for(my $i=0;$i<=$n;$i++){ # \u307e\u305a\u3001\u5de6\u53f3chg\u3060\u3051\u3067OK\u306a\u3082\u306e\u3092\u5909\u3048\u308b\r\n if( $d2[$i]->[1] > $d2[$i]->[2] ){\r\n my $v = - $d2[$i]->[3];\r\n if( $v >= 2 and $chg > 0 ){\r\n my $v2 = &min($v / 2,$chg);\r\n $chg -= $v2;\r\n $d2[$i]->[1] -= $v2;\r\n $d2[$i]->[2] += $v2;\r\n $d2[$i]->[3] += 2*$v2;\r\n $cost += $v2;\r\n }\r\n }\r\n }\r\n #print &Dumper(\\@d);\r\n my $sto = 0;\r\n my $mt = 0;\r\n for(my $i=0;$i<=$n;$i++){ # \u6b21\u306b\u5de6\u53f3\u66ff\u3048\u3068\u8272\u66ff\u3048\r\n my $v = $d2[$i]->[1] - $d2[$i]->[2];\r\n if( $v > 0 ){\r\n if( $sto > 0 ){\r\n my $v2 = &min($sto,$v);\r\n $sto -= $v2;\r\n $d2[$i]->[2] += $v2;\r\n $cost += $v2 * 2;\r\n } else {\r\n my $v2 = &min($chg,$v);\r\n $chg -= $v2;\r\n $d2[$i]->[1] -= $v2;\r\n $sto += $v2;\r\n }\r\n }\r\n $mt += &min($d2[$i]->[1],$d2[$i]->[2]);\r\n }\r\n $cost += ( $n / 2 ) - $mt;\r\n print \"$cost\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub min {\r\n my ($x,$y) = @_;\r\n return ( $x > $y ? $y : $x );\r\n}\r\n\r\n"}], "src_uid": "a2d4f0182456cedbe85dff97ec0f477e"} {"nl": {"description": "A new cottage village called \u00abFlatville\u00bb is being built in Flatland. By now they have already built in \u00abFlatville\u00bb n square houses with the centres on the \u041ex-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 \u00abFlatville\u00bb. The customer wants his future house to be on the \u041ex-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 \u00abFlatville\u00bb. 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\u2009\u2264\u2009n,\u2009t\u2009\u2264\u20091000). Then there follow n lines, each of them contains two space-separated integer numbers: xi ai, where xi \u2014 x-coordinate of the centre of the i-th house, and ai \u2014 length of its side (\u2009-\u20091000\u2009\u2264\u2009xi\u2009\u2264\u20091000, 1\u2009\u2264\u2009ai\u2009\u2264\u20091000).", "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": "($n, $t) = split(/ /, <>);\n$t += $t;\nfor $i (1..$n) {\n ($x, $a) = split(/ /, <>);\n $x += $x;\n push @arr, [$x-$a, $x+$a];\n}\n@arr = sort {$a->[0] <=> $b->[0]} @arr;\n$ans = 2;\nfor $i (1..$n-1) {\n $pr = $arr[$i-1][1];\n $cl = $arr[$i][0];\n $ans += $cl > $pr+$t ? 2:\n $cl == $pr+$t ? 1:0;\n}\nprint \"$ans\\n\";\n"}, {"source_code": "use strict;\nuse warnings;\n\n\nmy ($n, $t) = split \" \", <>;\nmy @board;\n\nmy $parse = sub {\n\tmy ($a, $b) = ($_[0][0], $_[0][1]);\n\t#print \"in func - $a, $b\\n\";\n\treturn [$a - $b/2, $a + $b/2];\n};\n\nfor (0..$n-1) {\n\tpush @board, [split \" \", <>]->$parse;\n}\n\n#print \"$board[$_][0], $board[$_][1]\\n\" for (0..$n-1);\n\n@board = sort {$a->[0] <=> $b->[0]} @board;\n\n#print \"$board[$_][0], $board[$_][1]\\n\" for (0..$n-1);\n\nmy $result = 2;\n\nfor (0..$n-2) {\n\tif($board[$_+1][0] - $board[$_][1] > $t) {\n\t\t$result += 2;\n\t} elsif($board[$_+1][0] - $board[$_][1] == $t) {\n\t\t$result += 1;\n\t}\n}\n\nprint $result . \"\\n\";\n\n\n\n"}], "negative_code": [{"source_code": "($n, $t) = split(/ /, <>);\n$t += $t;\nfor $i (1..$n) {\n ($x, $a) = split(/ /, <>);\n $x += $x;\n push @arr, [$x-$a, $x+$a];\n}\n@arr = sort {$a[0] cmp $b[0]} @arr;\n$ans = 2;\nfor $i (1..$n-1) {\n $pr = $arr[$i-1][1];\n $cl = $arr[$i][0];\n $ans += $cl > $pr+$t ? 2:\n $cl == $pr+$t ? 1:0;\n}\nprint \"$ans\\n\";\n"}], "src_uid": "c31fed523230af1f904218b2fe0d663d"} {"nl": {"description": "Recently in Divanovo, a huge river locks system was built. There are now $$$n$$$ locks, the $$$i$$$-th of them has the volume of $$$v_i$$$ liters, so that it can contain any amount of water between $$$0$$$ and $$$v_i$$$ liters. Each lock has a pipe attached to it. When the pipe is open, $$$1$$$ liter of water enters the lock every second.The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $$$i$$$ to the lock $$$i + 1$$$. If the lock $$$i + 1$$$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates $$$5$$$ locks with two open pipes at locks $$$1$$$ and $$$3$$$. Because locks $$$1$$$, $$$3$$$, and $$$4$$$ are already filled, effectively the water goes to locks $$$2$$$ and $$$5$$$. Note that the volume of the $$$i$$$-th lock may be greater than the volume of the $$$i + 1$$$-th lock.To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $$$q$$$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $$$j$$$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $$$t_j$$$ seconds.Please help the mayor to solve this tricky problem and answer his queries. ", "input_spec": "The first lines contains one integer $$$n$$$ ($$$1 \\le n \\le 200\\,000$$$)\u00a0\u2014 the number of locks. The second lines contains $$$n$$$ integers $$$v_1, v_2, \\dots, v_n$$$ ($$$1 \\le v_i \\le 10^9$$$))\u00a0\u2014 volumes of the locks. The third line contains one integer $$$q$$$ ($$$1 \\le q \\le 200\\,000$$$)\u00a0\u2014 the number of queries. Each of the next $$$q$$$ lines contains one integer $$$t_j$$$ ($$$1 \\le t_j \\le 10^9$$$)\u00a0\u2014 the number of seconds you have to fill all the locks in the query $$$j$$$. ", "output_spec": "Print $$$q$$$ integers. The $$$j$$$-th of them should be equal to the minimum number of pipes to turn on so that after $$$t_j$$$ seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print $$$-1$$$. ", "sample_inputs": ["5\n4 1 5 4 1\n6\n1\n6\n2\n3\n4\n5", "5\n4 4 4 4 4\n6\n1\n3\n6\n5\n2\n4"], "sample_outputs": ["-1\n3\n-1\n-1\n4\n3", "-1\n-1\n4\n4\n-1\n5"], "notes": "NoteThere are $$$6$$$ queries in the first example test. In the queries $$$1, 3, 4$$$ the answer is $$$-1$$$. We need to wait $$$4$$$ seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks $$$1$$$, $$$3$$$, and $$$4$$$. After $$$4$$$ seconds the locks $$$1$$$ and $$$4$$$ are full. In the following $$$1$$$ second $$$1$$$ liter of water is transferred to the locks $$$2$$$ and $$$5$$$. The lock $$$3$$$ is filled by its own pipe. Similarly, in the second query one can open pipes in locks $$$1$$$, $$$3$$$, and $$$4$$$.In the fifth query one can open pipes $$$1, 2, 3, 4$$$. "}, "positive_code": [{"source_code": "$n=<>;@a=split/\\s/,<>;$k=0;for(1..$n){@a[$_-1]+=@a[$_-2]if($_>1);$k=@a[$_-1]/$_ if(@a[$_-1]/$_>$k)};for(1..<>){$t=<>;print$t<$k?-1:-1&(@a[$n-1]+$t-1)/$t,\"\\n\"}"}, {"source_code": "$n=<>;@a=split/\\s/,<>;$k=0;for(1..$n){@a[$_-1]+=@a[$_-2]if($_>1);$k=@a[$_-1]/$_ if(@a[$_-1]/$_>$k)};for(1..<>){$t=<>;print$t<$k?-1:-1&@a[$n-1]/$t-1e-9+1,\"\\n\"}"}, {"source_code": "use POSIX;$n=<>;@a=split/\\s/,<>;$k=0;for(1..$n){@a[$_-1]+=@a[$_-2]if($_>1);$k=@a[$_-1]/$_ if(@a[$_-1]/$_>$k)};for(1..<>){$t=<>;print$t<$k?-1:ceil(@a[$n-1]/$t),\"\\n\"}\r\n"}, {"source_code": "$n=<>;@a=split/\\s/,<>;$k=0;for(1..$n){@a[$_-1]+=@a[$_-2]if($_>1);$k=@a[$_-1]/$_ if(@a[$_-1]/$_>$k)};for(1..<>){$t=<>;print$t<$k?-1:-1&@a[$n-1]/$t-1e-9+1,\"\\n\"}"}, {"source_code": "$n=<>;@a=split/\\s/,<>;$k=0;for(1..$n){@a[$_-1]+=@a[$_-2]if($_>1);$k=@a[$_-1]/$_ if(@a[$_-1]/$_>$k)};for(1..<>){$t=<>;print$t<$k?-1:-1&@a[$n-1]/$t-1e-9+1,\"\\n\"}"}, {"source_code": "$n=<>;@a=split/\\s/,<>;$k=0;for(1..$n){@a[$_-1]+=@a[$_-2]if($_>1);$k=@a[$_-1]/$_ if(@a[$_-1]/$_>$k)};for(1..<>){$t=<>;print$t<$k?-1:-1&@a[$n-1]/$t-1e-9+1,\"\\n\"}"}, {"source_code": "$n=<>;@a=split/\\s/,<>;$k=0;for(1..$n){@a[$_-1]+=@a[$_-2]if($_>1);$k=@a[$_-1]/$_ if(@a[$_-1]/$_>$k)};for(1..<>){$t=<>;print$t<$k?-1:-1&@a[$n-1]/$t-1e-9+1,\" \"}"}], "negative_code": [{"source_code": "$n=<>;@a=split/\\s/,<>;$k=0;for(1..$n){@a[$_-1]+=@a[$_-2]if($_>1);$k=@a[$_-1]/$_ if(@a[$_-1]/$_>$k)};for(1..<>){$t=<>;print$t<$k?-1:-1&@a[$n-1]/$t-1e-15+1,\"\\n\"}"}, {"source_code": "$n=<>;@a=split/\\s/,<>;$k=0;for(1..$n){@a[$_-1]+=@a[$_-2]if($_>1);$k=@a[$_-1]/$_ if(@a[$_-1]/$_>$k)};for(1..<>){$t=<>;print$k-$t>1e-9?-1:-1&@a[$n-1]/$t-9e-9+1,\"\\n\"}"}, {"source_code": "$n=<>;@a=split/\\s/,<>;$k=0;for(1..$n){@a[$_-1]+=@a[$_-2]if($_>1);$k=@a[$_-1]/$_ if(@a[$_-1]/$_>$k)};for(1..<>){$t=<>;print$t<$k?-1:-1&@a[$n-1]/$t-9e-9+1,\"\\n\"}\r\n"}], "src_uid": "d7361a43bff124cea280ae8817b807ec"} {"nl": {"description": "This week Arkady wanted to cook some pancakes (to follow ancient traditions) and make a problem about that. But then he remembered that one can't make a problem about stacking pancakes without working at a specific IT company, so he decided to bake the Napoleon cake instead.To bake a Napoleon cake, one has to bake $$$n$$$ dry layers first, and then put them on each other in one stack, adding some cream. Arkady started with an empty plate, and performed the following steps $$$n$$$ times: place a new cake layer on the top of the stack; after the $$$i$$$-th layer is placed, pour $$$a_i$$$ units of cream on top of the stack. When $$$x$$$ units of cream are poured on the top of the stack, top $$$x$$$ layers of the cake get drenched in the cream. If there are less than $$$x$$$ layers, all layers get drenched and the rest of the cream is wasted. If $$$x = 0$$$, no layer gets drenched. The picture represents the first test case of the example. Help Arkady determine which layers of the cake eventually get drenched when the process is over, and which don't.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 20\\,000$$$). 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$$$)\u00a0\u2014 the number of layers in the cake. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le n$$$)\u00a0\u2014 the amount of cream poured on the cake after adding each layer. 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 $$$n$$$ integers. The $$$i$$$-th of the integers should be equal to $$$1$$$ if the $$$i$$$-th layer from the bottom gets drenched, and $$$0$$$ otherwise.", "sample_inputs": ["3\n6\n0 3 0 0 1 3\n10\n0 0 0 1 0 5 0 0 0 2\n3\n0 0 0"], "sample_outputs": ["1 1 0 1 1 1 \n0 1 1 1 1 1 0 0 1 1 \n0 0 0"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my @B = (); $#B = $n-1;\r\n my $st = 0;\r\n for(my $i=$n-1;$i>=0;$i--){\r\n $st = $A[$i] if $A[$i]>$st;\r\n $B[$i] = ( ( $st > 0 ) ? 1 : 0 );\r\n $st--;\r\n }\r\n print ( join(\" \",@B) . \"\\n\");\r\n}\r\n\r\nexit(0);\r\n\r\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\t@_ = reverse @_;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i] > 0 ){\n\t\t\t$_[ $i + 1 ] < $_[ $i ] - 1 and \n\t\t\t\t$_[ $i + 1 ] = $_[ $i ] - 1;\n\t\t\t$_[ $i ] = 1;\n\t\t\t}\n\t\t}\n\t\n\tif( $_[ @_ - 1 ] > 1 ){\n\t\t$_[ @_ - 1 ] = 1;\n\t\t}\n\t\n\t@_ = reverse @_;\n\t\n\tprint \"@_\";\n\t}"}], "negative_code": [], "src_uid": "807c5ec37b0ea83ef40550698f1ff498"} {"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$$$) \u2014 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": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\n\nmy ($n, $m) = map { int } split /\\s+/, <>;\nmy $s = <>;\nchomp $s;\nmy $t = <>;\nchomp $t;\n$s =~ s/\\*/.*/;\nif ($t =~ /^$s$/) {\n print \"YES\\n\";\n}\nelse {\n print \"NO\\n\";\n}\n"}], "negative_code": [], "src_uid": "d629d09782a7af0acc359173ac4b4f0a"} {"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$$$) \u2014 the number of test cases. Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$2 \\le n \\le 100$$$) \u2014 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 \u2014 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": "<>;\r\nwhile (<>) {\r\n @_ = sort {$a <=> $b} split $\", <>;\r\n $min = shift @_;\r\n while ($_[0] == $min) {\r\n shift @_\r\n }\r\n print 0 + @_, \"\\n\"\r\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\t@_ = sort { $a <=> $b } @_;\n\t\n\tmy $min = $_[ 0 ];\n\t\n\t@_ = grep { $_ != $min } @_;\n\t\n\tprint 0 + @_;\n\t}"}], "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\u2009\u2264\u2009n\u2009\u2264\u20091000, 1\u2009\u2264\u2009h\u2009\u2264\u20091000)\u00a0\u2014 the number of friends and the height of the fence, respectively. The second line contains n integers ai (1\u2009\u2264\u2009ai\u2009\u2264\u20092h), the i-th of them is equal to the height of the i-th person.", "output_spec": "Print a single integer\u00a0\u2014 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\u2009+\u20091\u2009+\u20092\u2009=\u20094.In the second sample, all friends are short enough and no one has to bend, so the width 1\u2009+\u20091\u2009+\u20091\u2009+\u20091\u2009+\u20091\u2009+\u20091\u2009=\u20096 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\u2009+\u20092\u2009+\u20092\u2009+\u20092\u2009+\u20092\u2009+\u20091\u2009=\u200911."}, "positive_code": [{"source_code": "my (undef, $h) = split ' ', <>;\nmy @a = split ' ', <>;\n\nprint scalar(@a + grep { $_ > $h } @a), \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl \nuse strict;\nmy $data = ;\nmy $array =;\nmy @data_p = split(' ',$data);\nmy @array_p=split(' ', $array);\nmy ($n , $h) = @data_p;\nmy $sum = 0;\nforeach my $item (@array_p)\n{\n if ( $item > $h ) \n {\n $sum = $sum + 2 ;\n }\n else \n {\n $sum = $sum + 1 ;\n }\n}\nprint $sum; "}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\n# solve\n\nmy $n = read_token;\nmy $h = read_token;\n\nmy @talls = split q{ }, read_line;\n\nmy $answer = $n;\n\nfor (@talls) {\n $answer++ if $_ > $h;\n}\n\nsay $answer;\n"}, {"source_code": "(undef, $h) = split ' ', <>;\n$sum += 1 + ($_ > $h) for split ' ', <>;\nprint $sum"}], "negative_code": [{"source_code": "(undef, $h) = split ' ', <>;\n$sum += 1 + ($_ * 2 > $h) for split ' ', <>;\nprint $sum"}], "src_uid": "e7ed5a733e51b6d5069769c3b1d8d22f"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($x1,$y1,$z1) = map { $_ - 0 } split(/\\s+/,);\n my ($x2,$y2,$z2) = map { $_ - 0 } split(/\\s+/,);\n \n my $sc = 0;\n \n if( $z1 > 0 and $y2 > 0 ){\n my $tmp = ( $z1 < $y2 ? $z1: $y2 );\n $z1 -= $tmp;\n $y2 -= $tmp;\n $sc += 2 * $tmp;\n }\n \n my $xz1 = $x1 + $z1;\n if( $z2 > 0 and $xz1 > 0 ){\n my $tmp = ( $z2 < $xz1 ? $z2 : $xz1 );\n $z2 -= $tmp;\n $xz1 -= $tmp;\n }\n \n my $xy2 = $x2 + $y2;\n if( $y1 > 0 and $xy2 > 0 ){\n my $tmp = ( $y1 < $xy2 ? $y1 : $xy2 );\n $y1 -= $tmp;\n $xy2 -= $tmp;\n }\n \n if( $y1 > 0 and $z2 > 0 ){\n my $tmp = ( $y1 < $z2 ? $y1 : $z2 );\n $sc -= 2 * $tmp;\n }\n \n print \"$sc\\n\";\n \n}\n\nexit(0);\n\n\n"}], "negative_code": [], "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$$$) \u2014 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$$$) \u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tmy %h;\r\n\t\r\n\tmap $h{ $_ } ++, split ' ', <>;\r\n\t\r\n\tprint +( $h{ 1 } and grep $h{ $_ + 1 }, keys %h ) ? \"NO\" : \"YES\";\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\tmy $fail = 0;\n\t\n\tif( $h{ 1 } ){\n\t\t@_ = sort { $a <=> $b } @_;\n\t\tfor my $i ( 0 .. @_ - 2 ){\n\t\t\t$fail |= 1 == $_[ $i + 1 ] - $_[ $i ];\n\t\t\t}\n\t\t\n\t\tprint $fail ? \"NO\" : \"YES\";\n\t\t}\n\telse{\n\t\tprint \"YES\";\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\tif( $h{ 1 } and $h{ 0 } || $h{ 2 } ){\n\t\tprint \"NO\";\n\t\t}\n\telse{\n\t\tprint \"YES\";\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tif( 0 == grep $_ == 1, @_ ){\n\t\tprint \"YES\";\n\t\tnext;\n\t\t}\n\t\n\t@_ = sort { $a <=> $b } @_;\n\t\n\twhile( 1 ){\n\t\twhile( @_ and $_[ 0 ] == 1 ){\n\t\t\tshift @_;\n\t\t\t}\n\t\t\n\t\tlast if not @_;\n\t\t\n\t\tmy $div = $_[ 0 ] - 1;\n\t\t\n\t\t$_ %= $div for @_;\n\t\t\n\t\t@_ = sort { $a <=> $b } @_;\n\t\t\n\t\tif( grep { $_ == 0 || $_ == 2 } @_ ){\n\t\t\tprint \"NO\";\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tprint \"YES\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tif( ( grep $_ == 1, @_ ) and ( grep $_ % 2 == 0, @_ ) ){\n\t\tprint \"NO\";\n\t\t}\n\telse{\n\t\tprint \"YES\";\n\t\t}\n\t\n\t\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tif( ( grep $_ == 0, @_ ) and ( grep $_ == 1, @_ ) ){\n\t\tprint \"NO\";\n\t\t}\n\telse{\n\t\tprint \"YES\";\n\t\t}\n\t\n\t\n\t}"}], "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\u2009\u00d7\u2009m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i,\u2009j). 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,\u2009j) 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009500 and 1\u2009\u2264\u2009q\u2009\u2264\u20095000). 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\u2009\u2264\u2009i\u2009\u2264\u2009n and 1\u2009\u2264\u2009j\u2009\u2264\u2009m), 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": "$\\ = $/;\npush @_, scalar <> for 1 .. <>;\ny/ //d, $h[ $x[$i++] = (sort {$b <=> $a} map length, /1+/g)[0] ]++ for @_;\n\n$i = (substr $_[$_-1], (split)[1] -1, 1), \n (substr $_[$_-1], (split)[1] -1, 1 , 1 - $i), \n$h[ $x[$_-1] ] --,\n$h[ $x[$_-1] = (sort {$b <=> $a} map length, $_[$_-1] =~ /1+/g)[0]\n ] ++,\n$z = 0,\n(map {$h[$_] > $z and $max = $_} 0 .. 500),\nprint $max for <>"}, {"source_code": "$\\ = $/;\npush @_, scalar <> for 1 .. <>;\ny/ //d for @_;\n#print @_;\n$x[$i++] = 0 + ((sort {$b <=> $a} map length, /1+/g)[0]), $h[$x[$i-1]]++ for @_;\n#print \"<@x>\";\n#@h = (0) x 4;\n\n$i = (substr $_[$_-1], (split)[1]-1, 1), \n(substr $_[$_-1], (split)[1]-1, 1, 1 - $i), \n(@e = $_[$_-1] =~ /1+/g), \n#(print \"[@e]\"), \n$h[ $x[$_-1] ] --,\n$x[$_-1] = (sort {$b <=> $a} map length, @e)[0], \n#(print \"<@x>\"),\n$h[ $x[$_-1] ] ++,\n#(print \"[@h]\"),\n$z = 0,\n(map {$h[$_] > $z and do {$max = $_ }} 0 .. 500),\n(print $max) \nfor <>;\n\n#print @_"}], "negative_code": [{"source_code": "$\\ = $/;\npush @_, scalar <> for 1 .. <>;\ny/ //d for @_;\n#print @_;\n$x[$i++] = 0 + ((sort {$b <=> $a} map length, /1+/g)[0]), $h[$x[$i-1]]++ for @_;\n#print \"<@x>\";\n#@h = (0) x 4;\n\n$i = (substr $_[$_-1], (split)[1]-1, 1), \n(substr $_[$_-1], (split)[1]-1, 1, 1 - $i), \n(@e = $_[$_-1] =~ /1+/g), (print \"[@e]\"), \n$h[ $x[$_-1] ] --,\n$x[$_-1] = (sort {$b <=> $a} map length, @e)[0], \n#(print \"<@x>\"),\n$h[ $x[$_-1] ] ++,\n#(print \"[@h]\"),\n$z = 0,\n(map {$h[$_] > $z and do {$max = $_ }} 0 .. 500),\n(print $max) \nfor <>;\n\n#print @_"}], "src_uid": "337b6d2a11a25ef917809e6409f8edef"} {"nl": {"description": "Polycarpus has a hobby \u2014 he develops an unusual social network. His work is almost completed, and there is only one more module to implement \u2014 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\u2009<\u2009t2\u2009-\u2009t1\u2009\u2264\u2009d, 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\u2009\u2264\u2009n,\u2009d\u2009\u2264\u20091000). 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\u2009\u2264\u2009i\u2009\u2264\u2009n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0\u2009\u2264\u2009ti\u2009\u2264\u200910000). 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 \u2014 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": "use strict;\nuse warnings;\n\nmy ($n, $d) = split /\\s+/, <>;\n\nmy $msg_to = {};\nmy $ans = {};\n\nwhile (<>) {\n chomp;\n my ($n1, $n2, $t) = split/\\s+/;\n my $msg12 = \"$n1->$n2\";\n my $msg21 = \"$n2->$n1\";\n my $is_ans = 0;\n for ($msg_to->{$msg21} ? @{$msg_to->{$msg21}} : ()) {\n if (0 < $t-$_ && $t-$_ <= $d) {\n $ans->{$n1 gt $n2 ? \"$n1 $n2\" : \"$n2 $n1\"} = 1;\n $is_ans = 1;\n last;\n }\n }\n $is_ans or $msg_to->{$msg12} ? (push @{$msg_to->{$msg12}}, $t) : ($msg_to->{$msg12} = [$t]);\n}\n\nmy @k = keys %{$ans};\n$, = \"\\n\";\nprint 0+@k, @k;"}, {"source_code": "use strict;\nuse warnings;\n\nmy ($n, $d) = split /\\s+/, <>;\n\nmy $msg_to = {};\nmy $ans = {};\n\nwhile (<>) {\n chomp;\n my ($n1, $n2, $t) = split/\\s+/;\n my $msg12 = \"$n1->$n2\";\n my $msg21 = \"$n2->$n1\";\n my $is_ans = 0;\n for ($msg_to->{$msg21} ? @{$msg_to->{$msg21}} : ()) {\n if (0 < $t-$_ && $t-$_ <= $d) {\n $ans->{$n1 gt $n2 ? \"$n1 $n2\" : \"$n2 $n1\"} = 1;\n $is_ans = 1;\n last;\n }\n }\n $is_ans or $msg_to->{$msg12} ? (push @{$msg_to->{$msg12}}, $t) : ($msg_to->{$msg12} = [$t]);\n}\n\nmy @k = keys %{$ans};\n$, = \"\\n\";\nprint 0+@k, @k;\n"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\nopen STDIN, 'output.txt';\n\nmy ($n, $d) = split /\\s+/, <>;\n\nmy $msg_to = {};\nmy $ans = {};\n\nwhile (<>) {\n chomp;\n my ($n1, $n2, $t) = split/\\s+/;\n my $msg12 = \"$n1->$n2\";\n my $msg21 = \"$n2->$n1\";\n my $is_ans = 0;\n for ($msg_to->{$msg21} ? @{$msg_to->{$msg21}} : ()) {\n if (0 < $t-$_ && $t-$_ <= $d) {\n $ans->{$n1 gt $n2 ? \"$n1 $n2\" : \"$n2 $n1\"} = 1;\n $is_ans = 1;\n last;\n }\n }\n $is_ans or $msg_to->{$msg12} ? (push @{$msg_to->{$msg12}}, $t) : ($msg_to->{$msg12} = [$t]);\n}\n\nmy @k = keys %{$ans};\n$, = \"\\n\";\nprint 0+@k, @k;\n"}], "src_uid": "3cb4c89b174bf5ea51e797b78103e089"} {"nl": {"description": "One very important person has a piece of paper in the form of a rectangle a\u2009\u00d7\u2009b.Also, he has n seals. Each seal leaves an impression on the paper in the form of a rectangle of the size xi\u2009\u00d7\u2009yi. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees).A very important person wants to choose two different seals and put them two impressions. Each of the selected seals puts exactly one impression. Impressions should not overlap (but they can touch sides), and the total area occupied by them should be the largest possible. What is the largest area that can be occupied by two seals?", "input_spec": "The first line contains three integer numbers n, a and b (1\u2009\u2264\u2009n,\u2009a,\u2009b\u2009\u2264\u2009100). Each of the next n lines contain two numbers xi, yi (1\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009100).", "output_spec": "Print the largest total area that can be occupied by two seals. If you can not select two seals, print 0.", "sample_inputs": ["2 2 2\n1 2\n2 1", "4 10 9\n2 3\n1 1\n5 10\n9 11", "3 10 10\n6 6\n7 7\n20 5"], "sample_outputs": ["4", "56", "0"], "notes": "NoteIn the first example you can rotate the second seal by 90 degrees. Then put impression of it right under the impression of the first seal. This will occupy all the piece of paper.In the second example you can't choose the last seal because it doesn't fit. By choosing the first and the third seals you occupy the largest area.In the third example there is no such pair of seals that they both can fit on a piece of paper."}, "positive_code": [{"source_code": "# reads a line and parses it \nsub rd {\n my $v = ;\n chomp ( $v );\n my @arr = (split m/ /, $v);\n return @arr;\n}\n\nsub max {\n return ($_[0] > $_[1] ? $_[0] : $_[1]); }\n\nmy ($n, $a, $b) = &rd;\n\n# it only two seals\n# so no dp \n\nmy @seals;\nmy @x, @y;\nmy $one, $two;\nfor ( my $i =0 ; $i < $n; $i += 1 ) {\n ($one, $two) = &rd;\n push @x, $one;\n push @y, $two;}\n\n\n\nmy $max_area = 0;\nmy $o, $p, $q, $r;\nmy $area = 0;\n\nfor ( my $i =0 ; $i < $n-1; $i += 1 ) {\n\n for ( my $j = $i + 1 ; $j < $n; $j += 1 ) {\n\t# precalculate values\n\t$o = $x[ $i ];\n\t$p = $y[ $i ];\n\t$q = $x[ $j ];\n\t$r = $y[ $j ];\n\n\tif ( (($o + $q <= $a) and (&max ( $p, $r ) <= $b)) or \n\t (($o + $q <= $b) and (&max ( $p, $r ) <= $a)) ) {\n\t $area = ($o * $p) + ($q * $r);}\n\telsif ( (($o + $r <= $a) and (&max ( $p, $q ) <= $b)) ||\n\t\t(($o + $r <= $b) and (&max ( $p, $q ) <= $a)) ) {\n\t $area = ($o * $p) + ($q * $r);}\n\telsif ( (($p + $q <= $a) and (&max ( $o, $r ) <= $b)) ||\n\t\t(($p + $q <= $b) and (&max ( $o, $r ) <= $a)) ) {\n\t $area = ($o * $p) + ($q * $r);}\n\telsif ( (($p + $r <= $a) and (&max ( $o, $q ) <= $b)) ||\n\t\t(($p + $r <= $b) and (&max ( $o, $q ) <= $a)) ) {\n\t $area = ($o * $p) + ($q * $r);}\n\n\tif ( $area > $max_area ) {\n\t $max_area = $area; }\n }\n}\n\n# print answer\nprint $max_area;\nprint \"\\n\";\n"}], "negative_code": [{"source_code": "# reads a line and parses it \nsub rd {\n my $v = ;\n chomp ( $v );\n my @arr = (split m/ /, $v);\n return @arr;\n}\n\nsub max {\n return ($_[0] > $_[1] ? $_[0] : $_[1]); }\n\n\nmy ($n, $a, $b) = &rd;\n\n# it only two seals\n# so no dp \n\nmy @seals;\nmy @x, @y;\nmy $one, $two;\nfor ( my $i =0 ; $i < $n; $i += 1 ) {\n ($one, $two) = &rd;\n push @x, $one;\n push @y, $two;}\n\n\n\nmy $max_area = 0;\n\nmy $o, $p, $q, $r;\nmy $area = 0;\nfor ( my $i =0 ; $i < $n-1; $i += 1 ) {\n\n for ( my $j = $i + 1 ; $j < $n; $j += 1 ) {\n\t$o = $x[ $i ];\n\t$p = $y[ $i ];\n\t$q = $x[ $j ];\n\t$r = $y[ $j ];\n\n\t# pay attention to operator precedence\n\t# or parenthesize the shit out of everything\n\tif ( (($o + $p <= $a) and (&max ( $q, $r ) <= $b)) or \n\t (($o + $p <= $b) and (&max ( $q, $r ) <= $a)) ) {\n\t $area = ($o * $p) + ($q * $r);}\n\telsif ( (($o + $r <= $a) and (&max ( $p, $q ) <= $b)) ||\n\t\t(($o + $r <= $b) and (&max ( $p, $q ) <= $a)) ) {\n\t $area = ($o * $p) + ($q * $r);}\n\telsif ( (($p + $q <= $a) and (&max ( $o, $r ) <= $b)) ||\n\t\t(($p + $q <= $b) and (&max ( $o, $r ) <= $a)) ) {\n\t $area = ($o * $p) + ($q * $r);}\n\telsif ( (($p + $r <= $a) and (&max ( $o, $q ) <= $b)) ||\n\t\t(($p + $r <= $b) and (&max ( $o, $q ) <= $a)) ) {\n\t $area = ($o * $p) + ($q * $r);}\n\n\tif ( $area > $max_area ) {\n\t $max_area = $area; }\n }\n}\n \nprint $max_area;\nprint \"\\n\";\n\n\n"}, {"source_code": "# reads a line and parses it \nsub rd {\n my $v = ;\n chomp ( $v );\n my @arr = (split m/ /, $v);\n return @arr;\n}\n\nsub max {\n return ($_[0] > $_[1] ? $_[0] : $_[1]); }\n\n\nmy ($n, $a, $b) = &rd;\n\n# it only two seals\n# so no dp \n\nmy @seals;\nmy @x, @y;\nmy $one, $two;\nfor ( my $i =0 ; $i < $n; $i += 1 ) {\n ($one, $two) = &rd;\n push @x, $one;\n push @y, $two;}\n\n\n\nmy $max_area = 0;\n\nmy $o, $p, $q, $r;\nmy $area = 0;\nfor ( my $i =0 ; $i < $n-1; $i += 1 ) {\n\n for ( my $j = $i + 1 ; $j < $n; $j += 1 ) {\n\t$o = $x[ $i ];\n\t$p = $y[ $i ];\n\t$q = $x[ $j ];\n\t$r = $y[ $j ];\n\n\tif ( $o + $p <= $a and &max ( $q, $r ) <= $b || \n\t $o + $p <= $b and &max ( $q, $r ) <= $a ) {\n\t $area = ($o * $p) + ($q * $r);}\n\telsif ( $o + $r <= $a and &max ( $p, $q ) <= $b ||\n\t\t$o + $r <= $b and &max ( $p, $q ) <= $a ) {\n\t $area = ($o * $p) + ($q * $r);}\n\telsif ( $p + $q <= $a and &max ( $o, $r ) <= $b ||\n\t\t$p + $q <= $b and &max ( $o, $r ) <= $a ) {\n\t $area = ($o * $p) + ($q * $r);}\n\telsif ( $p + $r <= $a and &max ( $o, $q ) <= $b ||\n\t\t$p + r <= $b and &max ( $o, $q <= $a ) ) {\n\t $area = ($o * $p) + ($q * $r);}\n\n\tif ( $area > $max_area ) {\n\t $max_area = $area; }\n }\n}\n \nprint $max_area;\nprint \"\\n\";\n\n\n"}], "src_uid": "1ad63f41943e40aa8c8d5c88c29c283c"} {"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\u2009-\u20091, 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\u2009+\u20091, 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\u2009\u2264\u2009n\u2009\u2264\u2009100). The next line contains a list of space-separated integers a1,\u2009a2,\u2009...,\u2009an, (0\u2009\u2264\u2009ai\u2009\u2264\u2009100). The third line contains an integer m, (0\u2009\u2264\u2009m\u2009\u2264\u2009100). 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\u2009\u2264\u2009xi\u2009\u2264\u2009n,\u20091\u2009\u2264\u2009yi). 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": "while (<>){\n $n=<>;\n @N=split/ /,$n;\n unshift @N,0;\n $m=<>;\n for (1..$m){\n ($a,$b)=split/ /,<>;\n \n\n if ($a!=1){\n $N[$a-1]+=$b-1;\n }\n \n if ($a!=@N-1){\n $N[$a+1]+=$N[$a]-$b;\n }\n \n $N[$a]=0;\n \n \n }\n \n shift @N;\n print join\"\\n\",@N;\n print \"\\n\";\n \n }"}], "negative_code": [{"source_code": "while (<>){\n $n=<>;\n @N=split/ /,$n;\n unshift @N,0;\n $m=<>;\n for (1..$m){\n ($a,$b)=split/ /,<>;\n \n\n if ($a!=1){\n $N[$a-1]+=$b-1;\n }\n \n if ($a!=@N){\n $N[$a+1]+=$N[$a]-$b;\n }\n \n $N[$a]=0;\n \n \n }\n \n shift @N;\n print join\"\\n\",@N;\n print \"\\n\";\n \n }"}], "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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmy $i = 0;\n\t\n\tfor( @_ ){\n\t\tpush @{ $h{ $_ - $i } }, $_;\n\t\t$i ++;\n\t\t}\n\t\n\tmy @cand;\n\t\n\tpush @cand, eval join ' + ', @{ $h{ $_ } } for sort keys %h;\n\t\n\tprint 0 + ( sort { $b <=> $a } @cand )[ 0 ];\n\t}"}], "negative_code": [], "src_uid": "aab8d5a2d42b4199310f3f535a6b3bd7"} {"nl": {"description": "You are given a description of a depot. It is a rectangular checkered field of n\u2009\u00d7\u2009m 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,\u2009y), 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20091000)\u00a0\u2014 the number of rows and columns in the depot field. The next n lines contain m symbols \".\" and \"*\" each\u00a0\u2014 the description of the field. j-th symbol in i-th of them stands for cell (i,\u2009j). 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\u00a0\u2014 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": "sub go {\n\t($n, $m) = split ' ', <>; $.=0;\n while (<>) {\n\t\t$x=$.; \n \twhile (/\\*/g) {\n \t\t$y=pos;\n \t\t$r[$x]++;\n \t\t$c[$y]++;\n \t\t$rc[$x][$y]=1;\n \t\t$a++;\n \t}\n }\n return if grep($_>1, @r) > 1 or grep($_>1, @c) > 1;\n for ($x=1; $x<=$n; $x++) {\n \tfor ($y=1; $y<=$m; $y++) {\n \t\tif ($r[$x] + $c[$y] - $rc[$x][$y] == $a) {\n \t\t\treturn 1;\n \t\t}\n \t}\n }\n 0\n}\n\nprint go()? \"YES\\n$x $y\": \"NO\";\n"}], "negative_code": [{"source_code": "sub go {\n while (<>) {\n\t\t$x=$.-1; \n \twhile (/\\*/g) {\n \t\t$y=pos;\n \t\t$r[$x]++;\n \t\t$c[$y]++;\n \t\t$rc[$x][$y]=1;\n \t\t$a++;\n \t}\n }\n return if grep($_>1, @r) > 1 or grep($_>1, @c) > 1;\n for ($x=1; $x<@r; $x++) {\n \tfor ($y=1; $y<@c; $y++) {\n \t\tif ($r[$x] + $c[$y] - $rc[$x][$y] == $a) {\n \t\t\treturn 1;\n \t\t}\n \t}\n }\n 0\n}\n\nprint go()? \"YES\\n$x $y\": \"NO\";\n"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009105, ) \u2014 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\u2009+\u20091)-th line contains the description of the i-th friend of type mi, si (0\u2009\u2264\u2009mi,\u2009si\u2009\u2264\u2009109) \u2014 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": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m) = split;\n\t@_ = ();\n\tpush @_, scalar <> for 1 .. $n;\n\t@_ = sort {$a <=> $b} @_;\n\t@a = ();\n\tpush @a, (split)[1] for @_;\n\t@b = @a;\n\t\n\t$j = 0;\n\t$max = -1;\n\t$sum = 0;\n\t\n\tfor $i (0 .. @_ -1){\n\t\t\n\t\twhile ($_[$i] - $_[$j] >= $m){\n\t\t\t$j ++;\n\t\t\t$sum -= shift @a;\n\t\t\t}\n\n\t\t$sum += $b[$i];\n\t\t$sum > $max and $max = $sum;\n#\t\tprint \"[$sum|$max]\";\n\t\t\n\t\t}\n\t\n\tprint $max;\n\t}"}, {"source_code": "($n, $m) = split ' ', <>;\n\n@_ = sort {$a <=> $b} <>;\n\npush @a, (split)[1] for @_;\n@b = @a;\n\nfor (0 .. @_ -1){\n\t\n\t$j ++, $sum -= shift @a while $_[$_] - $_[$j] >= $m;\n\t\t\n\t( $sum += $b[$_] )\n\t\t> $max and $max = $sum\n\t}\n\nprint $max"}], "negative_code": [{"source_code": "($n, $m) = split ' ', <>;\n\n@_ = sort {$a <=> $b} <>;\n\npush @a, (split)[1] for @_;\n\n@_ = (); # releasing some memory\n\n@b = @a;\n\nfor (0 .. @_ -1){\n\t\n\t$j ++, $sum -= shift @a while $_[$_] - $_[$j] >= $m;\n\t\t\n\t( $sum += $b[$_] )\n\t\t> $max and $max = $sum\n\t}\n\nprint $max"}], "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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \\le n \\le 100$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @ab = ();\r\n for(my $i=0;$i<$n;$i++){\r\n my @ab1 = map { $_ - 0 } split(/\\s+/,);\r\n push(@ab,\\@ab1);\r\n }\r\n my @tm = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my @tr = (); $#tr = $#tm;\r\n for(my $i=0;$i<$n;$i++){\r\n $tr[$i] = $ab[$i]->[0] - ( $i > 0 ? $ab[$i-1]->[1] : 0 );\r\n }\r\n \r\n my $t = $tr[0] + $tm[0];\r\n for(my $i=0;$i<$n;$i++){\r\n my $spd = int(( $ab[$i]->[1] - $ab[$i]->[0] + 1 ) / 2);\r\n my $dpt = $t + $spd;\r\n $dpt = $ab[$i]->[1] if $dpt < $ab[$i]->[1];\r\n last if $i == $n-1;\r\n $t = $dpt + $tr[1+$i] + $tm[1+$i];\r\n }\r\n print \"$t\\n\";\r\n \r\n}\r\n\r\nexit(0);\r\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tchomp;\n\tmy $n = $_;\n\t\n\t@_ = map { [ split ' ', <> ] } 1 .. $n;\n\t\n\tmy @tm = split ' ', <>;\n\t\n\tmy $prev = 0;\n\tmy $ok;\n\tmy $sum = 0;\n\t\n\tfor my $i ( 0 .. @tm - 1 ){\n\t\t$sum += $_[ $i ][ 0 ] - $prev + $tm[ $i ];\n\t\t$ok = $sum;\n\t\t\n\t\tmy $diff = int 0.5 + ( $_[ $i ][ 1 ] - $_[ $i ][ 0 ] ) / 2;\n\t\t\n\t\tif( $sum + $diff > $_[ $i ][ 1 ] ){\n\t\t\t$sum += $diff;\n\t\t\t}\n\t\telse{\n\t\t\t$sum = $_[ $i ][ 1 ];\n\t\t\t}\n\t\t\n\t\t$prev = $_[ $i ][ 1 ];\n\t\t}\n\t\n\tprint $ok;\n\t}"}], "negative_code": [], "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\u2009\u2264\u2009n,\u2009k\u2009\u2264\u2009100)\u00a0\u2014 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\u00a0\u2014 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\u00a0\u2014 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": "($n, $k) = split ' ', <>;\n($L, @_) = reverse map length, <>;\n\nmap { $S += $L > $_; $E += $L == $_ } @_;\n\nprint join ' ', map {\n\t$_ + 5 * int ~- $_ / $k\n\t} map { $S + $_ } 1, $E"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $k) = split;\n\t@_ = ();\n\tfor (1 .. $n){\n\t\tpush @_, ~~<>;\n\t\t}\n\tchomp(@_);\n\tchomp( $psw = <> );\n\t$len_psw = length $psw;\n\t$short = grep { $len_psw > length } @_;\n\t$equal = grep { $len_psw == length } @_;\n\t\n\tprint join ' ', map {\n\t\t$_ + 5 * int( ($_-1) / $k)\n\t\t} $short + 1, $short + $equal;\n\t}"}, {"source_code": "($n, $k) = split ' ', <>;\n($L, @_) = reverse map length, <>;\n\nmap ++ $h{ $L <=> $_ }, @_;\n\nprint join ' ', map {\n\t$_ + 5 * int ~- $_ / $k\n\t} map { $h{1} + $_ } 1, $h{0}"}, {"source_code": "($n, $k) = split ' ', <>;\n($L, @_) = reverse map length, <>;\n\n$short = grep { $L > $_ } @_;\n\nprint join ' ', map {\n\t$_ + 5 * int ~- $_ / $k\n\t} $short + 1, $short + grep { $L == $_ } @_"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $k) = split;\n\t@_ = ();\n\tfor (1 .. $n){\n\t\tpush @_, ~~<>;\n\t\t}\n\tchomp(@_);\n\tchomp( $psw = <> );\n\t$len_psw = length $psw;\n\t$short = grep { $len_psw > length } @_;\n\t$equal = grep { $len_psw == length } @_;\n\t\n\tprint join ' ', map {\n\t\t$_ + 5 * int $_ / $k\n\t\t} $short + 1, $short + $equal;\n\t}"}], "src_uid": "06898c5e25de2664895f512f6b766915"} {"nl": {"description": "One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there are two laptops, such that the price of the first laptop is less (strictly smaller) than the price of the second laptop but the quality of the first laptop is higher (strictly greater) than the quality of the second laptop.Please, check the guess of Alex. You are given descriptions of n laptops. Determine whether two described above laptops exist.", "input_spec": "The first line contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of laptops. Next n lines contain two integers each, ai and bi (1\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u2009n), where ai is the price of the i-th laptop, and bi is the number that represents the quality of the i-th laptop (the larger the number is, the higher is the quality). All ai are distinct. All bi are distinct. ", "output_spec": "If Alex is correct, print \"Happy Alex\", otherwise print \"Poor Alex\" (without the quotes).", "sample_inputs": ["2\n1 2\n2 1"], "sample_outputs": ["Happy Alex"], "notes": null}, "positive_code": [{"source_code": "<>;\n/ /, $`==$' or $f=1 for <>;\nprint (($f? Happy:Poor),\" Alex\")\n"}, {"source_code": "<>;\n/ /, $`==$' or $f=1 for <>;\nprint (($f? Happy:Poor),\" Alex\")\n"}, {"source_code": "my ($a, $b, $f) = (0, 0, 0);\nfor(1..<>) {\n ($a, $b) = split / /, <>;\n $a != $b and $f = 1;\n}\nprint $f ? \"Happy Alex\" : \"Poor Alex\";\n"}, {"source_code": "<>;\n/ /, $`==$' or $f=1 for <>;\nprint (($f? Happy:Poor),\" Alex\")"}, {"source_code": "<>;\n/ /, $`==$' or $f=1 for <>;\nprint (($f? Happy:Poor),\" Alex\")\n"}, {"source_code": "<>;\n/ /, $`==$' or $f=1 for <>;\nprint (($f? Happy:Poor),\" Alex\")\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nsub comp {\n\tif ($a->[0] == $b->[0]) {\n\t\treturn $b->[1] <=> $a->[1];\n\t} else {\n\t\treturn $b->[0] <=> $a->[0];\n\t}\n}\n\nchomp($n = <>);\npush @a, [split / /, <>] foreach (1..$n);\n@a = sort comp @a;\nforeach $i (1..$n-1) {\n\t($a[$i][0]<$a[$i-1][0] && $a[$i][1]>$a[$i-1][1]) and say \"Happy Alex\" and exit;\n}\nsay \"Poor Alex\";"}, {"source_code": "use 5.016;\n\nmy $n = <>;\nmy @l;\nfor (1..$n) {\n my ($a, $b) = split ' ', <>;\n push @l, [$a, $b];\n}\nmy @x = sort { $a->[0] <=> $b->[0] } @l;\nmy $ok = 0;\nfor my $i (1..$#x) {\n my ($x, $y) = @x[$i - 1, $i];\n if ($x->[0] < $y->[0] && $x->[1] > $y->[1]) {\n $ok = 1;\n last;\n }\n}\n\nsay $ok ? 'Happy Alex' : 'Poor Alex';\n"}, {"source_code": "my $T=<>;\nchomp($T);\nmy %DATA;\nwhile($T--){\nmy ($a,$b)=split(/ /,<>);\nchomp($a,$b);\n$DATA{$b}=$a;\n}\nmy @S=map { $DATA{$_} } sort { $a <=> $b } keys %DATA;\nfor(0..@S-2){\nif($S[$_] > $S[$_+1]){\nprint \"Happy Alex \\n\";\nexit(0);\n}\n}\nprint \"Poor Alex\\n\";"}, {"source_code": "<>;\n/ /, $`==$' or $f=1 for <>;\nprint (($f? Happy:Poor),\" Alex\")\n"}, {"source_code": "#!/usr/bin/env perl -w\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy @laps;\n\nfor (1..$n) {\n my $l = <>;\n chomp $l;\n $l =~ /^(\\d+) (\\d+)$/;\n\n push(@laps, [int($1), int($2)]);\n}\n\n@laps = sort {\nif ($a->[0] < $b->[0]) {\n return -1;\n} elsif ($a->[0] == $b->[0]) {\n if ($a->[1] < $b->[1]) {\n return -1;\n } elsif ($a->[1] == $b->[1]) {\n return 0;\n } else {\n return 1;\n }\n} else {\n return 1;\n}\n} @laps;\n\nfor (my $i = 1; $i <= $#laps; $i++) {\n if ($laps[$i-1]->[1] > $laps[$i]->[1]) {\n print \"Happy Alex\\n\";\n exit 0;\n }\n}\n\nprint \"Poor Alex\\n\";\n"}, {"source_code": "<>;\n/ /, $`==$' or $f=1 for <>;\nprint (($f? Happy:Poor),\" Alex\")\n"}, {"source_code": "<>;\n/ /, $`==$' or $f=1 for <>;\nprint (($f? Happy:Poor),\" Alex\")\n"}, {"source_code": "<>;\n@_=<>;\n(($a,$b)=split/ /,$_),($a!=$b and $f=1) for @_;\nprint $f? \"Happy Alex\":\"Poor Alex\""}, {"source_code": "<>;\n/ /, $`==$' or $f=1 for <>;\nprint (($f? Happy:Poor),\" Alex\")"}, {"source_code": "<>;\n/ /, $`==$' or $f=1 for <>;\nprint (($f? Happy:Poor),\" Alex\")\n"}], "negative_code": [{"source_code": "my ($a, $b, $f) = (0, 0, 0);\nfor(1..<>) {\n ($a, $b) = split / /, <>;\n $a != $b and $f = 1;\n}\nprint $f ? \"Happy Alex!\" : \"Poor Alex\";\n"}, {"source_code": "my $T=<>;\nchomp($T);\nmy %DATA;\nwhile($T--){\nmy ($a,$b)=split(/ /,<>);\nchomp($a,$b);\n$DATA{$b}=$a;\n}\nmy @S=map { $DATA{$_} } sort { $a <=> $b } keys %DATA;\nfor(0..@S-2){\nif($S[$_] > $S[$_+1]){\nprint \"Happy Alex \\n\";\nlast;\n}\n}\nprint \"Poor Alex\\n\";"}, {"source_code": "my $T=<>;\nchomp($T);\nmy %DATA;\nwhile($T--){\nmy ($a,$b)=split(/ /,<>);\nchomp($a,$b);\n$DATA{$b}=$a;\n}\nmy @S=map { $DATA{$_} } sort { $a <=> $b } keys %DATA;\nfor(0..@S-2){\nif($S[$_] > $S[$_+1]){\nprint \"Happy Alex \\n\";\n}\n}\nprint \"Poor Alex\\n\";"}, {"source_code": "#!/usr/bin/env perl -w\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy @laps;\n\nfor (1..$n) {\n my $l = <>;\n chomp $l;\n $l =~ /^(\\d+) (\\d+)$/;\n\n push(@laps, [$1, $2]);\n}\n\n@laps = sort { $a->[0] cmp $b->[0] } @laps;\n\nfor (my $i = 0; $i <= $#laps; $i++) {\n for (my $j = 0; $j < $i; $j++) {\n if ($laps[$i]->[1] < $laps[$j]->[1]) {\n print \"Happy Alex\\n\";\n exit 0;\n }\n }\n}\n\nprint \"Poor Alex\\n\";\n"}, {"source_code": "#!/usr/bin/env perl -w\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy @laps;\n\nfor (1..$n) {\n my $l = <>;\n chomp $l;\n $l =~ /^(\\d+) (\\d+)$/;\n\n push(@laps, [$1, $2]);\n}\n\n@laps = sort { $a->[0] cmp $b->[0] } @laps;\n\nmy $max = $laps[$#laps]->[1];\nmy $cost = $laps[$#laps]->[0];\n\nfor (my $i = $#laps-1; $i >= 0; $i--) {\n if ($laps[$i]->[0] < $cost && $laps[$i]->[1] > $max) {\n print \"Happy Alex\\n\";\n exit 0;\n }\n}\n\nprint \"Poor Alex\\n\";\n"}, {"source_code": "#!/usr/bin/env perl -w\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy (@a, @b);\n\nfor (1..$n) {\n my $l = <>;\n chomp $l;\n $l =~ /^(\\d+) (\\d+)$/;\n\n push(@a, $1);\n push(@b, $2);\n}\n\n@a = sort @a;\n@b = sort @b;\n\nfor (my $i = 0; $i <= $#a; $i++) {\n if ($a[$i] != $b[$i]) {\n print \"Poor Alex\\n\";\n exit 0;\n }\n}\n\nprint \"Happy Alex\\n\";\n"}, {"source_code": "#!/usr/bin/env perl -w\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy @laps;\n\nfor (1..$n) {\n my $l = <>;\n chomp $l;\n $l =~ /^(\\d+) (\\d+)$/;\n\n push(@laps, [int($1), int($2)]);\n}\n\n@laps = sort {\nif ($a->[0] <= $b->[0]) {\n if ($a->[1] < $b->[1]) {\n return 1;\n } elsif ($a->[1] == $b->[1]) {\n return 0;\n } else {\n return -1;\n }\n} else {\n return 1;\n}\n} @laps;\n\nfor (my $i = 1; $i <= $#laps; $i++) {\n if ($laps[$i-1]->[1] > $laps[$i]->[1]) {\n print \"Happy Alex\\n\";\n exit 0;\n }\n}\n\nprint \"Poor Alex\\n\";\n"}, {"source_code": "#!/usr/bin/env perl -w\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy @laps;\n\nfor (1..$n) {\n my $l = <>;\n chomp $l;\n $l =~ /^(\\d+) (\\d+)$/;\n\n push(@laps, [$1, $2]);\n}\n\n@laps = sort { $a->[0] cmp $b->[0] } @laps;\n\nfor (my $i = 0; $i <= $#laps; $i++) {\n for (my $j = $i; $j <= $#laps; $j++) {\n if ($laps[$i]->[1] > $laps[$j]->[1]) {\n print \"Happy Alex\\n\";\n exit 0;\n }\n }\n}\n\nprint \"Poor Alex\\n\";\n"}, {"source_code": "#!/usr/bin/env perl -w\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy @laps;\n\nfor (1..$n) {\n my $l = <>;\n chomp $l;\n $l =~ /^(\\d+) (\\d+)$/;\n\n push(@laps, [int($1), int($2)]);\n}\n\n@laps = sort {\nif ($a->[0] <= $b->[0]) {\n if ($a->[1] < $b->[1]) {\n return 1;\n } elsif ($a->[1] == $b->[1]) {\n return 0;\n } else {\n return -1;\n }\n} else {\n return 1;\n}\n} @laps;\n\nmy $max = $laps[$#laps]->[1];\nmy $cost = $laps[$#laps]->[0];\n\nfor (my $i = $#laps-1; $i >= 0; $i--) {\n if ($laps[$i]->[0] < $cost && $laps[$i]->[1] > $max) {\n print \"Happy Alex\\n\";\n exit 0;\n }\n}\n\nuse Data::Dumper;\nprint Dumper(\\@laps);\n\nprint \"Poor Alex\\n\";\n"}, {"source_code": "#!/usr/bin/env perl -w\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy @laps;\n\nfor (1..$n) {\n my $l = <>;\n chomp $l;\n $l =~ /^(\\d+) (\\d+)$/;\n\n push(@laps, [int($1), int($2)]);\n}\n\n@laps = sort {\nif ($a->[0] <= $b->[0]) {\n if ($a->[1] < $b->[1]) {\n return 1;\n } elsif ($a->[1] == $b->[1]) {\n return 0;\n } else {\n return -1;\n }\n} else {\n return 1;\n}\n} @laps;\n\nmy $max = $laps[$#laps]->[1];\nmy $cost = $laps[$#laps]->[0];\n\nfor (my $i = $#laps-1; $i >= 0; $i--) {\n if ($laps[$i]->[0] < $cost && $laps[$i]->[1] > $max) {\n print \"Happy Alex\\n\";\n exit 0;\n }\n}\n\nprint \"Poor Alex\\n\";\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\u2009\u2264\u2009|s|\u2009\u2264\u20092\u00b7105) 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": "$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t%h = ();\n\tmap $h{ $_ }++, split //;\n\t$c = 0;\n\tmap { $h{ $_ } % 2 and $c ++ } a .. z;\n\n\t$i = 0;\n\tmap { $h{ $_ } % 2 and do { $i ++< int $c / 2 and $h{ $_ }++ } } a .. z;\n\n\t$i = 0;\n\tmap { $h{ $_ } % 2 and do { $i ++< int $c / 2 and $h{ $_ }-- } } reverse a .. z;\n\t\n\t$_ = eval join \".\", map { \"'$_' x \" . (int $h{$_} / 2) . \" \" } sort keys %h;\n\t\n\t@r = grep { $h{ $_ } % 2 } a .. z;\n\t\n\tprint $_ . \"@r\" . reverse\n\n\t}"}, {"source_code": "$_ = <>, chomp;\n\nmap $h{ $_ }++, split //;\n\nmap $c += $h{ $_ } & 1, a .. z;\n\nw( 1, a .. z );\nw( -1, reverse a .. z );\n\n$_ = eval join \".\", map \"'$_' x \" . ($h{$_} >> 1) . \" \", sort keys %h;\n\n@r = g( a .. z );\n\nprint \"$_@r\" . reverse;\n\nsub w { \n\tmy $i; \n\t$j = shift; \n\tmap { $i ++< ($c >> 1) and $h{ $_ } += $j } g( @_ )\n\t}\n\t\nsub g { grep $h{ $_ } & 1, @_ }\n"}], "negative_code": [], "src_uid": "f996224809df700265d940b12622016f"} {"nl": {"description": "A Martian boy is named s \u2014 he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=\u00ababa\u00bb, then strings \u00abbaobab\u00bb, \u00abaabbaa\u00bb, \u00abhelloabahello\u00bb make him very happy and strings \u00abaab\u00bb, \u00abbaaa\u00bb and \u00abhelloabhello\u00bb do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner.", "input_spec": "The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters.", "output_spec": "Print the sought number of ways to cut string t in two so that each part made s happy. ", "sample_inputs": ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"], "sample_outputs": ["2", "0"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n\nmy $s = readline(*STDIN);\nmy $t = readline(*STDIN);\nchomp($s);\nchomp($t);\n$s =~ tr/a-zA-Z//dc;\n$t =~ tr/a-zA-Z//dc;\n# print \"s: $s\\tt: $t\\n\";\nmy $start_pos = 0;\nmy $end_pos = length($t);\n# print \"start:$start_pos\\tend:$end_pos\\n\";\nwhile ( $s =~ m/([a-z])/g && $start_pos>=0 ) {\n $start_pos = index($t, $1, $start_pos);\n # print \"$1\\tstart_pos:$start_pos\\n\";\n $start_pos++;\n}\n\n$s = scalar reverse $s;\nwhile ( $s =~ m/([a-z])/g && $end_pos>=0 ) {\n $end_pos = rindex($t, $1, $end_pos);\n # print \"$1\\tend_pos:$end_pos\\n\";\n $end_pos--;\n}\n$start_pos--;\n$end_pos++;\n# print \"start:$start_pos\\tend:$end_pos\\n\";\nif ($end_pos >= $start_pos && $start_pos >= 0) {\n\tprint $end_pos - $start_pos . \"\\n\";\n}\nelse\n{\n\tprint \"0\\n\";\n}"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\n\nmy $s = readline(*STDIN);\nmy $t = readline(*STDIN);\nchomp($s);\nchomp($t);\nmy $start_pos = 0;\nmy $end_pos = 0;\nwhile ( $s =~ m/(\\S)/g) {\n $start_pos = index($t, $1, $start_pos);\n}\n\n\n$s = scalar reverse $s;\n$end_pos = length($t);\nwhile ( $s =~ m/(\\S)/g) {\n $end_pos = rindex($t, $1, $end_pos);\n}\nif ($end_pos >= $start_pos && $start_pos >= 0) {\n\tprint $end_pos- $start_pos . \"\\n\";\n}\nelse\n{\n\tprint \"0\\n\";\n}"}], "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 \u00a0\u2014 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": "print\"2 3\\n262143 131071 0\\n131072 262143 \".<>"}, {"source_code": "print\"2 3\\n262143 131071 0\\n131072 262143 \".<>"}], "negative_code": [], "src_uid": "fc0442e5cda2498a1818702e5e3eeec4"} {"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\u2009-\u2009y|. Then this player adds integer |x\u2009-\u2009y| 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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the initial number of elements in the set. The second line contains n distinct space-separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 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": "use List::Util qw/max/;\nmy $n = <>;\nmy @num = split ' ', <>;\nmy $max = max(@num);\nmy $d = shift @num;\nwhile (@num) {\n my $b = shift @num;\n ($d, $b) = ($b, $d%$b) while $b;\n}\nmy $new = $max/$d - $n;\nprint $new%2==1 ? 'Alice' : 'Bob';"}], "negative_code": [], "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\u2009=\u2009x). 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\u2009\u2264\u2009n\u2009\u2264\u2009105). The next line contains integers a1, a2, ..., an (1\u2009\u2264\u2009ai\u2009\u2264\u2009105). The numbers are separated by spaces.", "output_spec": "In the first line print integer t \u2014 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\u2009=\u20090."}, "positive_code": [{"source_code": "<>;\n@_=split/ /,<>;\n$a=0;\n$I=0;\nfor(@_){\n\t$I++;\n\t$m[$_].=\"$I \"\n\t}\nfor (1..10e5){\n\tif ($m[$_]){\n\t\tif ($m[$_]=~/\\S \\d+ \\S/){\n\t\t\tchop $m[$_];\n\t\t\t@n=split/ /,$m[$_];\n\t\t\t$B=0; \n\t\t\t$d=$n[1]-$n[0];\n\t\t\t$dd=shift @n;\n\t\t\tfor $j(@n){\n\t\t\t\tif ($j-$dd!=$d){$B++}\n\t\t\t\t$dd=$j;\n\t\t\t\t}\n\t\t\t$B or $ats.=\"$_ $d\\n\";\n\t\t\t$B or $a++;\n\t\t\t}\n\t\telsif ($m[$_]=~/^(\\d+) (\\d+)/){\n\t\t\t$d=$2-$1;\n\t\t\t$ats.= \"$_ $d\\n\";\n\t\t\t$a++;\n\t\t\t}\n\t\telse {\n\t\t\t$ats.=\"$_ 0\\n\"; $a++\n\t\t\t}\n\t\t}\n\t}\nprint \"$a\\n$ats\";"}, {"source_code": "<>;\n@_=split/ /,<>;\n$a=0;\n$I=0;\nfor(@_){\n\t$I++;\n\t$m[$_].=\"$I \"\n\t}\nfor (1..10e5){\n\tif ($m[$_]){\n\t\tif ($m[$_]=~/\\S \\d+ \\S/){\n\t\t\tchop $m[$_];\n\t\t\t@n=split/ /,$m[$_];\n\t\t\t$B=0; \n\t\t\t$d=$n[1]-$n[0];\n\t\t\t$dd=shift @n;\n\t\t\tfor $j(@n){\n\t\t\t\tif ($j-$dd!=$d){$B++}\n\t\t\t\t$dd=$j;\n\t\t\t\t}\n\t\t\t$B or push @ats, \"$_ $d\\n\";\n\t\t\t$B or $a++;\n\t\t\t}\n\t\telsif ($m[$_]=~/^(\\d+) (\\d+)/){\n\t\t\t$d=$2-$1;\n\t\t\tpush @ats, \"$_ $d\\n\";\n\t\t\t$a++;\n\t\t\t}\n\t\telse {\n\t\t\tpush @ats, \"$_ 0\\n\"; $a++\n\t\t\t}\n\t\t}\n\t}\nprint \"$a\\n\";\nprint @ats"}, {"source_code": "<>;\n@_=split/ /,<>;\n$a=0;\n$I=0;\nfor(@_){\n\t$I++;\n\t$m[$_].=\"$I \"\n\t}\nfor (1..10e5){\n\tif ($m[$_]){\n\t\tif ($m[$_]=~/^(\\d+) (\\d+) (\\d+)/){\n\t\t\tchop $m[$_];\n\t\t\t@n=split/ /,$m[$_];\n\t\t\t$B=0; \n\t\t\t## for $i(@_){$I++;$i==$_ and push @n, $I}\n\t#\t\tprint \" @n\\n\";\n\t\t\t$d=$n[1]-$n[0];\n\t\t\t$dd=shift @n;\n\t\t\tfor $j(@n){\n\t\t\t\tif ($j-$dd!=$d){$B++}\n\t\t\t\t$dd=$j;\n\t\t\t\t}\n\t\t\t$B or $ats.=\"$_ $d\\n\";\n\t\t\t$B or $a++;\n\t\t\t}\n\t\telsif ($m[$_]=~/^(\\d+) (\\d+)/){\n\t\t\t\tchop $m[$_];\n\t\t##\t\t@n=(); $I=0; \n\t\t##\t\tfor $i(@_){$I++;$i==$_ and push @n, $I}\n\t#\t\t\tprint \" @n\\n\";\n\t\t\t\t$d=$2-$1;\n\t\t\t$ats.= \"$_ $d\\n\";\n\t\t\t$a++;\n\t\t\t}\n\t\telse {\n\t\t\t$ats.=\"$_ 0\\n\";\n\t\t\t$a++\n\t\t\t}\n\t\t\t\t\n\t\t\t\t}\n\t}\nprint \"$a\\n$ats\";"}, {"source_code": "<>;\n@_=split/ /,<>;\n$a=0;\n$I=0;\nfor(@_){\n\t$I++;\n\t$m[$_].=\"$I \"\n\t}\nfor (1..10e5){\n\tif ($m[$_]){\n\t\tif ($m[$_]=~/\\S \\d+ \\S/){\n\t\t\tchop $m[$_];\n\t\t\t@n=split/ /,$m[$_];\n\t\t\t$B=0; \n\t\t\t$d=$n[1]-$n[0];\n\t\t\t$dd=shift @n;\n\t\t\tfor $j(@n){\n\t\t\t\tif ($j-$dd!=$d){$B++; last}\n\t\t\t\t$dd=$j;\n\t\t\t\t}\n\t\t\t$B or push @ats, \"$_ $d\\n\";\n\t\t\t$B or $a++;\n\t\t\t}\n\t\telsif ($m[$_]=~/^(\\d+) (\\d+)/){\n\t\t\t$d=$2-$1;\n\t\t\tpush @ats, \"$_ $d\\n\";\n\t\t\t$a++;\n\t\t\t}\n\t\telse {\n\t\t\tpush @ats, \"$_ 0\\n\"; $a++\n\t\t\t}\n\t\t}\n\t}\nprint \"$a\\n\";\nprint @ats"}, {"source_code": "<>;\n@_=split/ /,<>;\n$a=0;\n$I=0;\nfor(@_){\n\t$I++;\n\t$m[$_].=\"$I \"\n\t}\nfor (1..10e5){\n\tif ($m[$_]){\n\t\tif ($m[$_]=~/\\S \\d+ \\S/){\n\t\t\tchop $m[$_];\n\t\t\t@n=split/ /,$m[$_];\n\t\t\t$B=0; \n\t\t\t$d=$n[1]-$n[0];\n\t\t\t$dd=shift @n;\n\t\t\tfor $j(@n){\n\t\t\t\tif ($j-$dd!=$d){$B++}\n$B and last;\n\t\t\t\t$dd=$j;\n\t\t\t\t}\n\t\t\t$B or push @ats, \"$_ $d\\n\";\n\t\t\t$B or $a++;\n\t\t\t}\n\t\telsif ($m[$_]=~/^(\\d+) (\\d+)/){\n\t\t\t$d=$2-$1;\n\t\t\tpush @ats, \"$_ $d\\n\";\n\t\t\t$a++;\n\t\t\t}\n\t\telse {\n\t\t\tpush @ats, \"$_ 0\\n\"; $a++\n\t\t\t}\n\t\t}\n\t}\nprint \"$a\\n\";\nprint @ats"}], "negative_code": [{"source_code": "<>;\n@_=split/ /,<>;\nfor(@_){\n\t$m[$_]++\n\t}\nfor (1..10e5){\n\tif ($m[$_]){\n\t\tif ($m[$_]>2){\n\t\t\t@n=(); $B=0; $I=0;\n\t\t\tfor $i(@_){$I++;$i==$_ and push @n, $I}\n\t#\t\tprint \" @n\\n\";\n\t\t\t$d=$n[1]-$n[0];\n\t\t\t$dd=shift @n;\n\t\t\tfor $j(@n){\n\t\t\t\tif ($j-$dd!=$d){$B++}\n\t\t\t\t$dd=$j;\n\t\t\t\t}\n\t\t\t$B or $ats.=\"$_ $d\\n\";\n\t\t\t$B or $a++;\n\t\t\t}\n\t\telsif ($m[$_]==2){\n\t\t\t\t@n=(); $I=0; \n\t\t\t\tfor $i(@_){$I++;$i==$_ and push @n, $I}\n\t#\t\t\tprint \" @n\\n\";\n\t\t\t\t$d=$n[1]-$n[0];\n\t\t\t$ats.= \"$_ $d\\n\";\n\t\t\t$a++;\n\t\t\t}\n\t\telse {\n\t\t\t$ats.=\"$_ 0\\n\";\n\t\t\t$a++\n\t\t\t}\n\t\t\t\t\n\t\t\t\t}\n\t}\nprint \"$a\\n$ats\";"}], "src_uid": "097e35b5e9c96259c54887158ebff544"} {"nl": {"description": "Polycarp has a checkered sheet of paper of size n\u2009\u00d7\u2009m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's \"Black Square\", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.", "input_spec": "The first line contains two integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100) \u2014 the sizes of the sheet. The next n lines contain m letters 'B' or 'W' each \u2014 the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white.", "output_spec": "Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1.", "sample_inputs": ["5 4\nWWWW\nWWWB\nWWWB\nWWBB\nWWWW", "1 2\nBB", "3 3\nWWW\nWWW\nWWW"], "sample_outputs": ["5", "-1", "1"], "notes": "NoteIn the first example it is needed to paint 5 cells \u2014 (2,\u20092), (2,\u20093), (3,\u20092), (3,\u20093) and (4,\u20092). Then there will be a square with side equal to three, and the upper left corner in (2,\u20092).In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square.In the third example all cells are colored white, so it's sufficient to color any cell black."}, "positive_code": [{"source_code": "($n, $m, @s) = (split(\" \", <>), <>);\n\nsub min { $_[0] < $_[1]? $_[0]: $_[1] }\nsub max { $_[0] > $_[1]? $_[0]: $_[1] }\n\n$l = \"inf\"; $i = 0;\n\nfor (@s) {\n\twhile (/B/g) {\n\t\t$c++;\n\t\t$l = min($l, $-[0]);\n\t\t$r = max($r, $+[0] - 1);\n\t\t$u //= $i;\n\t\t$d = $i;\n\t}\n\t$i++;\n}\n\n$w = max($r - $l, $d - $u) + 1;\nprint $w > $n || $w > $m? -1: $w * $w - $c;\n"}], "negative_code": [], "src_uid": "cd6bc23ea61c43b38c537f9e04ad11a6"} {"nl": {"description": "You've got an array a, consisting of n integers: a1,\u2009a2,\u2009...,\u2009an. Your task is to find a minimal by inclusion segment [l,\u2009r] (1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n) such, that among numbers al,\u2009\u00a0al\u2009+\u20091,\u2009\u00a0...,\u2009\u00a0ar there are exactly k distinct numbers.Segment [l,\u2009r] (1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n; l,\u2009r are integers) of length m\u2009=\u2009r\u2009-\u2009l\u2009+\u20091, satisfying the given property, is called minimal by inclusion, if there is no segment [x,\u2009y] satisfying the property and less then m in length, such that 1\u2009\u2264\u2009l\u2009\u2264\u2009x\u2009\u2264\u2009y\u2009\u2264\u2009r\u2009\u2264\u2009n. Note that the segment [l,\u2009r] 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\u2009\u2264\u2009n,\u2009k\u2009\u2264\u2009105). The second line contains n space-separated integers a1,\u2009a2,\u2009...,\u2009an\u00a0\u2014 elements of the array a (1\u2009\u2264\u2009ai\u2009\u2264\u2009105).", "output_spec": "Print a space-separated pair of integers l and r (1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n) such, that the segment [l,\u2009r] 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,\u20095] 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": "$_ = <>;\n($n, $m) = split;\n$_ = <>;\n@_ = split;\n($l, $r) = (0, 0);\n$_{$_[$r++]} += 1 while $r < @_ && keys %_ < $m;\n\nif (keys %_ < $m) {\n print '-1 -1';\n} else {\n $_{$_[$l++]} -= 1 while $_{$_[$l]} > 1;\n ++$l;\n print \"$l $r\";\n}\n"}, {"source_code": "use strict;\nuse warnings;\nmy %hash = ();\nmy $distinct = 0;\nchomp(my ( $n, $k ) = split( \" \", ));\nchomp(my @array = split( \" \", ));\nfor my $elt (@array) {\n if ( not exists $hash{$elt} ) {\n $distinct++;\n $hash{$elt}++;\n }\n}\n\nif ( $distinct < $k ) {\n print \"-1 -1\";\n exit;\n}\n\nmy $first = $array[0];\n\nmy %hash2 = ($first => 1);\n\n\n\nmy $distinct2 = 1;\nmy $start = 0;\nmy $end = 0;\nfor ( my $i = 1 ; $i < @array ; $i++ ) {\n if ( $distinct2 == $k ) {\n last;\n }\n $end = $i;\n if ( not exists $hash2{ $array[$i] } ) {\n $hash2{ $array[$i] }++;\n $distinct2++;\n }\n #print \"d = $distinct2, k = $k, start = $start, end = $end\\n\";\n \n if ( $array[$i] == $array[$start] ) {\n $start++;\n }\n}\n#print \"$start $end\\n\";\nmy $found = 1;\nwhile ($found) {\n my $count = 0;\n for ( my $i = $start ; $i <= $end ; $i++ ) {\n if ( $i != $start && $array[$i] == $array[$start] ) {\n $count++;\n $start++;\n }\n if ($i != $end && $array[$i] == $array[$end] ) {\n $count++;\n $end--;\n }\n }\n if ( $count == 0 ) { $found = 0; }\n}\n\n$start++;\n$end++;\nprint \"$start $end\";"}], "negative_code": [{"source_code": "$_ = <>;\n($n, $m) = split;\n$_ = <>;\n@_ = split;\n($l, $r) = (0, 0);\n$_{$_[$r++]} += 1 while $r < @_ && keys %_ < $m;\n\nif ($r == @_) {\n print '-1 -1';\n} else {\n $_{$_[$l++]} -= 1 while $_{$_[$l]} > 1;\n ++$l;\n print \"$l $r\";\n}\n"}, {"source_code": "use strict;\nuse warnings;\nmy %hash = ();\nmy $distinct = 0;\nchomp(my ( $n, $k ) = split( \" \", ));\nchomp(my @array = split( \" \", ));\nfor my $elt (@array) {\n if ( not exists $hash{$elt} ) {\n $distinct++;\n $hash{$elt}++;\n }\n}\n\nif ( $distinct < $k ) {\n print \"-1 -1\";\n exit;\n}\n\nmy $first = $array[0];\n\nmy %hash2 = ($first => 1);\n\n\n\nmy $distinct2 = 1;\nmy $start = 0;\nmy $end = 0;\nfor ( my $i = 1 ; $i < @array ; $i++ ) {\n $end = $i;\n if ( not exists $hash2{ $array[$i] } ) {\n $hash2{ $array[$i] }++;\n $distinct2++;\n }\n print \"d = $distinct2, k = $k, start = $start, end = $end\\n\";\n if ( $distinct2 == $k ) {\n last;\n }\n if ( $array[$i] == $array[$start] ) {\n $start++;\n }\n}\nprint \"$start $end\\n\";\nmy $found = 1;\nwhile ($found) {\n my $count = 0;\n for ( my $i = $start ; $i <= $end ; $i++ ) {\n if ( $i != $start && $array[$i] == $array[$start] ) {\n $count++;\n $start++;\n }\n if ($i != $end && $array[$i] == $array[$end] ) {\n $count++;\n $end--;\n }\n }\n if ( $count == 0 ) { $found = 0; }\n}\n\n$start++;\n$end++;\nprint \"$start $end\";"}, {"source_code": "use strict;\nuse warnings;\nmy %hash = ();\nmy $distinct = 0;\nchomp(my ( $n, $k ) = split( \" \", ));\nchomp(my @array = split( \" \", ));\nfor my $elt (@array) {\n if ( not exists $hash{$elt} ) {\n $distinct++;\n $hash{$elt}++;\n }\n}\n\nif ( $distinct < $k ) {\n print \"-1 -1\";\n exit;\n}\n\nmy $first = $array[0];\n\nmy %hash2 = ($first => 1);\n\n\n\nmy $distinct2 = 1;\nmy $start = 0;\nmy $end = 0;\nfor ( my $i = 1 ; $i < @array ; $i++ ) {\n $end = $i;\n if ( not exists $hash2{ $array[$i] } ) {\n $hash2{ $array[$i] }++;\n $distinct2++;\n }\n #print \"d = $distinct2, k = $k, start = $start, end = $end\\n\";\n if ( $distinct2 == $k ) {\n last;\n }\n if ( $array[$i] == $array[$start] ) {\n $start++;\n }\n}\n#print \"$start $end\\n\";\nmy $found = 1;\nwhile ($found) {\n my $count = 0;\n for ( my $i = $start+1 ; $i <= $end ; $i++ ) {\n if ( $array[$i] == $array[$start] ) {\n $count++;\n $start++;\n }\n }\n if ( $count == 0 ) { $found = 0; }\n}\n\n$start++;\n$end++;\nprint \"$start $end\";"}, {"source_code": "use strict;\nuse warnings;\nmy %hash = ();\nmy $distinct = 0;\nchomp(my ( $n, $k ) = split( \" \", ));\nchomp(my @array = split( \" \", ));\nfor my $elt (@array) {\n if ( not exists $hash{$elt} ) {\n $distinct++;\n $hash{$elt}++;\n }\n}\n\nif ( $distinct < $k ) {\n print \"-1 -1\";\n exit;\n}\n\nmy $first = $array[0];\n\nmy %hash2 = ($first => 1);\n\n\n\nmy $distinct2 = 1;\nmy $start = 0;\nmy $end = 0;\nfor ( my $i = 1 ; $i < @array ; $i++ ) {\n $end = $i;\n if ( not exists $hash2{ $array[$i] } ) {\n $hash2{ $array[$i] }++;\n $distinct2++;\n }\n #print \"d = $distinct2, k = $k, start = $start, end = $end\\n\";\n if ( $distinct2 == $k ) {\n last;\n }\n if ( $array[$i] == $array[$start] ) {\n $start++;\n }\n}\n#print \"$start $end\\n\";\nmy $found = 1;\nwhile ($found) {\n my $count = 0;\n for ( my $i = $start ; $i <= $end ; $i++ ) {\n if ( $i != $start && $array[$i] == $array[$start] ) {\n $count++;\n $start++;\n }\n if ($i != $end && $array[$i] == $array[$end] ) {\n $count++;\n $end--;\n }\n }\n if ( $count == 0 ) { $found = 0; }\n}\n\n$start++;\n$end++;\nprint \"$start $end\";"}, {"source_code": "use strict;\nuse warnings;\nmy %hash = ();\nmy $distinct = 0;\nchomp(my ( $n, $k ) = split( \" \", ));\nchomp(my @array = split( \" \", ));\nfor my $elt (@array) {\n if ( not exists $hash{$elt} ) {\n $distinct++;\n $hash{$elt}++;\n }\n}\n\nif ( $distinct < $k ) {\n print \"-1 -1\";\n exit;\n}\n\nmy $first = $array[0];\n\nmy %hash2 = ($first => 1);\n\n\n\nmy $distinct2 = 1;\nmy $start = 0;\nmy $end = 0;\nfor ( my $i = 1 ; $i < @array ; $i++ ) {\n $end = $i;\n if ( not exists $hash2{ $array[$i] } ) {\n $hash2{ $array[$i] }++;\n $distinct2++;\n }\n #print \"d = $distinct2, k = $k, start = $start, end = $end\\n\";\n if ( $distinct2 == $k ) {\n last;\n }\n if ( $array[$i] == $array[$start] ) {\n $start++;\n }\n}\n#print \"$start $end\\n\";\nmy $found = 1;\nwhile ($found) {\n my $count = 0;\n for ( my $i = $start+1 ; $i < $end ; $i++ ) {\n if ( $array[$i] == $array[$start] ) {\n $count++;\n $start++;\n }\n if ( $array[$i] == $array[$end] ) {\n $count++;\n $end--;\n }\n }\n if ( $count == 0 ) { $found = 0; }\n}\n\n$start++;\n$end++;\nprint \"$start $end\";"}], "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$$$) \u2014 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 \u2014 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": "use strict;\nuse warnings;\n\nmy $t = <>;\n\nwhile($t-- > 0){\n my $n = <>;\n my $n2 = 0;\n my $n3 = 0;\n if($n == 1){\n print \"0\\n\";\n } else {\n while($n % 2 == 0) {\n $n2++;\n $n /= 2;\n }\n while($n % 3 == 0) {\n $n3++;\n $n /= 3;\n }\n if($n == 1) {\n if($n3 >= $n2) {\n printf(\"%d\\n\", $n3-$n2 + $n3);\n } else {\n print \"-1\\n\";\n }\n } else {\n print \"-1\\n\";\n }\n }\n}\n"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\n\nmy $t = <>;\n\nwhile($t-- > 0){\n my $n = <>;\n my $n2 = 0;\n my $n3 = 0;\n if($n == 1){\n print \"0\\n\";\n } else {\n while($n % 2 == 0) {\n $n2++;\n $n /= 2;\n }\n while($n % 3 == 0) {\n $n3++;\n $n /= 3;\n }\n if($n == 1) {\n if($n3 > $n2) {\n printf(\"%d\\n\", $n3-$n2 + $n3);\n } else {\n print \"-1\\n\";\n }\n } else {\n print \"-1\\n\";\n }\n }\n}"}], "src_uid": "3ae468c425c7b156983414372fd35ab8"} {"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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100) \u2014 the number of employees and the number of languages. Then n lines follow \u2014 each employee's language list. At the beginning of the i-th line is integer ki (0\u2009\u2264\u2009ki\u2009\u2264\u2009m) \u2014 the number of languages the i-th employee knows. Next, the i-th line contains ki integers \u2014 aij (1\u2009\u2264\u2009aij\u2009\u2264\u2009m) \u2014 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 \u2014 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\n\n\n\nsub count_disjoint_sets {\n\n my $ds_ref = $_[0];\n my %count = ();\n for my $i (values %{ $ds_ref }) {\n\t$count { $i } = 1;\n }\n return (scalar (keys %count));\n}\n\n\n\nsub make_disjoint_sets {\n\n my ($e_ref, $l_ref, $ds_ref,\n\t$v_ref, $person, $id ) = @_;\n\n # return if we've already visited this person\n if ( exists $v_ref -> { $person } ) {\n\treturn;\n }\n # mark visited\n $v_ref -> { $person } = 1;\n # add to dijoint set\n $ds_ref -> { $person } = $id;\n\n for my $i ( keys %{ $l_ref -> { $person } } ) {\n\tfor my $j ( keys %{ $e_ref -> { $i } } ) {\n\t &make_disjoint_sets ( $e_ref, $l_ref, $ds_ref,\n\t\t\t\t $v_ref, $j, $id );\n\t}}\n}\n\n\n\n\n\n\n\nmy $f = ;\nchomp ( $f );\nmy ($n, $m) = split m/ /, $f;\n\nmy %emp_of_langs;\nmy %langs_of_emp;\nmy @lst;\nmy $language_exists = 0;\nmy $val;\nfor my $i (1..$n) {\n $val = 0;\n @lst = ();\n $f = ;\n chomp ( $f );\n @lst = split m/ /, $f;\n $val = shift @lst;\n if ( $val > 0 ) {\n\t$language_exists = 1;\n }\n for my $j (@lst) {\n\t$langs_of_emp{ $i }{ $j } = 1;\n\t$emp_of_langs{ $j }{ $i } = 1;\n }\n}\n\n\n\nmy %ds = ();\nmy %visit = ();\n\n# initialize disjoint set\nfor my $i (1..$n) {\n $ds { $i } = $i;\n}\n\nfor my $i (1..$n) {\n &make_disjoint_sets ( \\%emp_of_langs, \\%langs_of_emp, \\%ds,\n\t\t\t \\%visit, $i, $i );\n}\n\nmy $answer = &count_disjoint_sets ( \\%ds );\n\nif ( not $language_exists ) {\n print $answer;\n print \"\\n\";\n}\nelse {\n print ($answer - 1);\n print \"\\n\";\n}\n\n"}, {"source_code": "$_ = <>;\ns/^\\d+ //;\nchomp;\n$m = $_;\n\nwhile (<>){\n s/^\\d+ ?//;\n chomp;\n if (!$_) {++$sum; next};\n push @m, $_;\n}\n\nfor $i(1..$m){\n @a = @b = ();\n for (@m){\n if (/\\b$i\\b/){\n push @a, $_;\n }\n else{\n push @b, $_;\n }\n } \n $a = join\" \",@a;\n @b? (@m = @b):(@m=());\n $a and unshift @m, $a\n}\n\n$sum += scalar @m; \n@m and $sum --;\nprint \"$sum\\n\";"}], "negative_code": [{"source_code": "\n\n\n\n\nsub count_disjoint_sets {\n\n my $ds_ref = $_[0];\n my %count = ();\n for my $i (values %{ $ds_ref }) {\n\t$count { $i } = 1;\n }\n return (scalar (keys %count));\n}\n\n\n\nsub make_disjoint_sets {\n\n my ($e_ref, $l_ref, $ds_ref,\n\t$v_ref, $person, $id ) = @_;\n\n # return if we've already visited this person\n if ( exists $v_ref -> { $person } ) {\n\treturn;\n }\n # mark visited\n $v_ref -> { $person } = 1;\n # add to dijoint set\n $ds_ref -> { $person } = $id;\n\n for my $i ( keys %{ $l_ref -> { $person } } ) {\n\tfor my $j ( keys %{ $e_ref -> { $i } } ) {\n\t &make_disjoint_sets ( $e_ref, $l_ref, $ds_ref,\n\t\t\t\t $v_ref, $j, $id );\n\t}}\n}\n\n\n\n\n\n\n\nmy $f = ;\nchomp ( $f );\nmy ($n, $m) = split m/ /, $f;\n\nmy %emp_of_langs;\nmy %langs_of_emp;\nmy @lst;\nfor my $i (1..$n) {\n @lst = ();\n $f = ;\n chomp ( $f );\n @lst = split m/ /, $f;\n shift @lst;\n\n for my $j (@lst) {\n\t$langs_of_emp{ $i }{ $j } = 1;\n\t$emp_of_langs{ $j }{ $i } = 1;\n }\n}\n\n\n\nmy %ds = ();\nmy %visit = ();\n\n\nfor my $i (1..$n) {\n &make_disjoint_sets ( \\%emp_of_langs, \\%langs_of_emp, \\%ds,\n\t\t\t \\%visit, $i, $i );\n}\n\n\nmy $answer = &count_disjoint_sets ( \\%ds );\n\nprint ($answer - 1);\nprint \"\\n\";\n\n\n"}], "src_uid": "e2836276aee2459979b232e5b29e6d57"} {"nl": {"description": "Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.", "input_spec": "The first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20095000)\u00a0\u2014 then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1\u2009\u2264\u2009ai\u2009\u2264\u2009bi\u2009\u2264\u2009366), providing that the i-th friend can come to the party from day ai to day bi inclusive.", "output_spec": "Print the maximum number of people that may come to Famil Door's party.", "sample_inputs": ["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"], "sample_outputs": ["2", "4"], "notes": "NoteIn the first sample, friends 3 and 4 can come on any day in range [117,\u2009128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140."}, "positive_code": [{"source_code": "use feature ':all';\n\nwhile(<>){\n\tfor (1 .. $_){\n\t\t($sex, $from, $to) = split ' ', <>;\n\t\tfor $i ($from .. $to){\n\t\t\t$_[ $i ] .= $sex;\n\t\t\t}\n\t\t}\n\tfor (@_){\n\t\t$_ = join '', sort split //;\n\t\ts/FM// and $_ .= 'P' while /FM/;\n\t\tpush @P, scalar( () = /P/g );\n\t\t}\n\t\n\tsay 2 * (sort {$b <=> $a} @P )[ 0 ]\n\t}"}], "negative_code": [{"source_code": "use feature ':all';\n\nwhile(<>){\n\tfor (1 .. $_){\n\t\t($sex, $from, $to) = split ' ', <>;\n\t\tfor $i ($from .. $to){\n\t\t\t$_[ $i ] .= $sex;\n\t\t\t}\n\t\t}\n\tfor (@_){\n\t\t$_ = join '', sort split //;\n\t\ts/FM// and $_ .= 'P' while /FM/;\n\t\tpush @P, scalar( () = /P/g );\n\t\t}\n\t\n\tsay( (sort {$b <=> $a} @P )[ 0 ] )\n\t}"}], "src_uid": "75d500bad37fbd2c5456a942bde09cd3"} {"nl": {"description": "You are given a multiset (i.\u00a0e. a set that can contain multiple equal integers) containing $$$2n$$$ integers. Determine if you can split it into exactly $$$n$$$ pairs (i.\u00a0e. each element should be in exactly one pair) so that the sum of the two elements in each pair is odd (i.\u00a0e. 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$$$) \u2014 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$$$) \u2014 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/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my %c = ('0'=>0, '1'=>0);\r\n for(my $i=0;$i<2*$n;$i++){\r\n $c{ $A[$i] % 2 } ++;\r\n }\r\n my $res = ( $c{'0'} == $c{'1'} ? 'Yes' : 'No' );\r\n print \"$res\\n\";\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "src_uid": "d5bd27c969d9cd910f13baa53c247871"} {"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\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000, 1\u2009\u2264\u2009m\u2009\u2264\u2009100\u2009000)\u00a0\u2014 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,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009109)\u00a0\u2014 the numbers in the multiset.", "output_spec": "If it is not possible to select k numbers in the desired way, output \u00abNo\u00bb (without the quotes). Otherwise, in the first line of output print \u00abYes\u00bb (without the quotes). In the second line print k integers b1,\u2009b2,\u2009...,\u2009bk\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k, $m ) = split;\n\t@_ = split ' ', <>;\n\tmy %h;\n\t\n\tfor( @_ ){\n\t\tpush @{ $h{ $_ % $m } }, $_;\n\t\t}\n\t\n\tmy $ok = -1;\n\t\n\tfor( keys %h ){\n\t\t@{ $h{ $_ } } >= $k and do {\n\t\t\t$ok = $_;\n\t\t\tlast;\n\t\t\t};\n\t\t}\n\t\n\tprint $ok == -1 ? \"No\" : \"Yes\\n@{ $h{ $ok } }[ 0 .. $k - 1 ]\";\n\t\n\t}"}, {"source_code": "( $n, $k, $m, $ok ) = split ' ', <>;\n\npush @{ $h{ $_ % $m + 1 } }, $_ for split ' ', <>;\n\n@{ $h{ $_ } } < $k or $ok = $_ for keys %h;\n\nprint $ok ? \"Yes\\n@{ $h{ $ok } }[ 0 .. $k - 1 ]\" : \"No\""}], "negative_code": [{"source_code": "( $n, $k, $m, $ok ) = split ' ', <>;\n\npush @{ $h{ $_ % $m + 1 } }, $_ for split ' ', <>;\n\n@{ $h{ $_ } } < $k or $ok = $_ for keys %h;\n\nprint $ok ? \"No\" : \"Yes\\n@{ $h{ $ok } }[ 0 .. $k - 1 ]\""}], "src_uid": "55bd1849ef13b52788a0b5685c4fcdac"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "use strict;\nuse v5.020;\nmy $try = <>;\nwhile($try-- > 0){\n my $n = int(<>);\n my @arr = split(' ', <>);\n my ($first, $firstCnt) = ($arr[0], 1);\n shift @arr;\n while(@arr > 0){\n if($first == shift(@arr)){\n $firstCnt += 1;\n }\n }\n if($n == $firstCnt){\n say \"$n\";\n } else {\n say \"1\";\n }\n}"}], "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$$$)\u00a0\u2014 the number of plots. The second line contains the integers $$$m_1, m_2, \\ldots, m_n$$$ ($$$1 \\leq m_i \\leq 10^9$$$)\u00a0\u2014 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$$$\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile( <> ){\n $debug and print '-' x 15;\n \n @_ = split ' ', <>;\n \n my @cand;\n \n for my $i ( 0 .. @_ - 1 ){\n \n my $sum = $_[ $i ];\n my $min = $_[ $i ];\n \n for my $j ( $i + 1 .. @_ - 1 ){\n if( $min < $_[ $j ] ){\n $sum += $min;\n }\n else{\n $sum += $min = $_[ $j ];\n }\n }\n \n my $min = $_[ $i ];\n \n for my $j ( reverse 0 .. $i - 1 ){\n if( $min < $_[ $j ] ){\n $sum += $min;\n }\n else{\n $sum += $min = $_[ $j ];\n }\n }\n \n push @cand, [ $sum, $i ];\n }\n \n my( $i_ok ) = map $_->[ 1 ], sort { $b->[ 0 ] <=> $a->[ 0 ] } @cand;\n \n $debug and print $i_ok;\n \n my $i = $i_ok;\n \n my $min = $_[ $i ];\n \n for my $j ( $i + 1 .. @_ - 1 ){\n if( $min < $_[ $j ] ){\n $_[ $j ] = $min;\n }\n else{\n $min = $_[ $j ];\n }\n }\n \n my $min = $_[ $i ];\n \n for my $j ( reverse 0 .. $i - 1 ){\n if( $min < $_[ $j ] ){\n $_[ $j ] = $min;\n }\n else{\n $min = $_[ $j ];\n }\n }\n \n print \"@_\";\n }"}], "negative_code": [], "src_uid": "88e6cd9ef45b956be8bee5d4fedd5725"} {"nl": {"description": "Little Vasya has received a young builder\u2019s 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\u2009\u2264\u2009N\u2009\u2264\u20091000) \u2014 the number of bars at Vasya\u2019s disposal. The second line contains N space-separated integers li \u2014 the lengths of the bars. All the lengths are natural numbers not exceeding 1000.", "output_spec": "In one line output two numbers \u2014 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": "<>;\nmy %hash;\nmy $max = 0;\nfor(split / /, <>) {\n $hash{int$_}++;\n $max = $hash{int$_} if ($hash{int$_} > $max);\n}\nprint ($max . \" \" . scalar keys %hash);\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n$tb{int($_)}++ foreach (split / /, <>);\n@a = keys %tb;\n($mx, $ans) = (0, scalar @a);\n$mx=$mx>$tb{$_} ? $mx:$tb{$_} foreach (@a);\nsay \"$mx $ans\";"}, {"source_code": "while (<>){\n @m=split/ /,<>;\n @_=();\n while (@m){\n $_[shift @m]++\n }\n $max=$n=0;\n for (@_){\n $_ and $n++;\n $_>$max and $max=$_; \n }\n \n print \"$max $n\\n\";\n }"}], "negative_code": [], "src_uid": "9a92221c760a3b6a1e9318f948fe0473"} {"nl": {"description": "The integers shop sells $$$n$$$ segments. The $$$i$$$-th of them contains all integers from $$$l_i$$$ to $$$r_i$$$ and costs $$$c_i$$$ coins.Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.After shopping, Vasya will get some more integers as a gift. He will get integer $$$x$$$ as a gift if and only if all of the following conditions are satisfied: Vasya hasn't bought $$$x$$$. Vasya has bought integer $$$l$$$ that is less than $$$x$$$. Vasya has bought integer $$$r$$$ that is greater than $$$x$$$. Vasya can get integer $$$x$$$ as a gift only once so he won't have the same integers after receiving a gift.For example, if Vasya buys segment $$$[2, 4]$$$ for $$$20$$$ coins and segment $$$[7, 8]$$$ for $$$22$$$ coins, he spends $$$42$$$ coins and receives integers $$$2, 3, 4, 7, 8$$$ from these segments. He also gets integers $$$5$$$ and $$$6$$$ as a gift.Due to the technical issues only the first $$$s$$$ segments (that is, segments $$$[l_1, r_1], [l_2, r_2], \\ldots, [l_s, r_s]$$$) will be available tomorrow in the shop.Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.For each $$$s$$$ from $$$1$$$ to $$$n$$$, find how many coins will Vasya spend if only the first $$$s$$$ segments will be available.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$)\u00a0\u2014 the number of segments in the shop. Each of next $$$n$$$ lines contains three integers $$$l_i$$$, $$$r_i$$$, $$$c_i$$$ ($$$1 \\leq l_i \\leq r_i \\leq 10^9, 1 \\leq c_i \\leq 10^9$$$)\u00a0\u2014 the ends of the $$$i$$$-th segments and its cost. It is guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case output $$$n$$$ integers: the $$$s$$$-th ($$$1 \\leq s \\leq n$$$) of them should be the number of coins Vasia will spend in the shop if only the first $$$s$$$ segments will be available.", "sample_inputs": ["3\n2\n2 4 20\n7 8 22\n2\n5 11 42\n5 11 42\n6\n1 4 4\n5 8 9\n7 8 7\n2 10 252\n1 11 271\n1 10 1"], "sample_outputs": ["20\n42\n42\n42\n4\n13\n11\n256\n271\n271"], "notes": "NoteIn the first test case if $$$s = 1$$$ then Vasya can buy only the segment $$$[2, 4]$$$ for $$$20$$$ coins and get $$$3$$$ integers.The way to get $$$7$$$ integers for $$$42$$$ coins in case $$$s = 2$$$ is described in the statement.In the second test case note, that there can be the same segments in the shop."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $min, $max, $c ) = split ' ', <>;\n\t\n\tmy $minC = $c;\n\tmy $minCL = $c;\n\tmy $minCR = $c;\n\t\n\tmy $switch = 1;\n\t\n\tmy @ans = $minC;\n\t\n\tfor my $i ( 1 .. $_ - 1 ){\n\t\tmy( $L, $R, $cc ) = split ' ', <>;\n\t\t\n\t\tif( $L > $min and $R < $max ){\n\t\t\tpush @ans, $ans[ @ans - 1 ];\n\t\t\t}\n\t\telsif( $L == $min and $R == $max ){\n\t\t\tif( $switch == 1 ){\n\t\t\t\t$cc < $minC and $minC = $cc;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$minC = $cc;\n\t\t\t\t}\n\t\t\t$cc < $minCL and $minCL = $cc;\n\t\t\t$cc < $minCR and $minCR = $cc;\n\t\t\tif( $minC < $minCL + $minCR ){\n\t\t\t\tpush @ans, $minC;\n\t\t\t\t$switch = 1;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tpush @ans, $minCL + $minCR;\n\t\t\t\t$switch = 0;\n\t\t\t\t}\n\t\t\t}\n\t\telsif( $L < $min and $R > $max ){\n\t\t\t$switch = 1;\n\t\t\t$minC = $cc;\n\t\t\t$minCL = $cc;\n\t\t\t$minCR = $cc;\n\t\t\tpush @ans, $minC;\n\t\t\t$min = $L;\n\t\t\t$max = $R;\n\t\t\t}\n\t\telsif( $L < $min and $R == $max ){\n\t\t\t$switch = 1;\n\t\t\t$minC = $cc;\n\t\t\t$minCL = $cc;\n\t\t\t$cc < $minCR and $minCR = $cc;\n\t\t\tpush @ans, $minC;\n\t\t\t$min = $L;\n\t\t\t}\n\t\telsif( $L == $min and $R > $max ){\n\t\t\t$switch = 1;\n\t\t\t$minC = $cc;\n\t\t\t$minCR = $cc;\n\t\t\t$cc < $minCL and $minCL = $cc;\n\t\t\tpush @ans, $minC;\n\t\t\t$max = $R;\n\t\t\t}\n\t\telsif( $L < $min and $R < $max ){\n\t\t\t$switch = 0;\n\t\t\t$minCL = $cc;\n\t\t\t$min = $L;\n\t\t\tpush @ans, $minCL + $minCR;\n\t\t\t}\n\t\telsif( $L > $min and $R > $max ){\n\t\t\t$switch = 0;\n\t\t\t$minCR = $cc;\n\t\t\t$max = $R;\n\t\t\tpush @ans, $minCL + $minCR;\n\t\t\t}\n\t\telsif( $L == $min and $R < $max ){\n\t\t\t$cc < $minCL and $minCL = $cc;\n\t\t\tif( $switch == 1 and $minC < $minCL + $minCR ){\n\t\t\t\tpush @ans, $minC;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$switch = 0;\n\t\t\t\tpush @ans, $minCL + $minCR;\n\t\t\t\t}\n\t\t\t}\n\t\telsif( $L > $min and $R == $max ){\n\t\t\t$cc < $minCR and $minCR = $cc;\n\t\t\tif( $switch == 1 and $minC < $minCL + $minCR ){\n\t\t\t\tpush @ans, $minC;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$switch = 0;\n\t\t\t\tpush @ans, $minCL + $minCR;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint join \"\\n\", @ans;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $min, $max, $c ) = split ' ', <>;\n\t\n\tmy $minC = $c;\n\tmy $minCL = $c;\n\tmy $minCR = $c;\n\t\n\tmy $switch = 1;\n\t\n\tmy @ans = $minC;\n\t\n\tfor my $i ( 1 .. $_ - 1 ){\n\t\tmy( $L, $R, $cc ) = split ' ', <>;\n\t\t\n\t\tif( $L > $min and $R < $max ){\n\t\t\tpush @ans, $ans[ @ans - 1 ];\n\t\t\t}\n\t\telsif( $L == $min and $R == $max ){\n\t\t\t$cc < $minC and $minC = $cc;\n\t\t\t$cc < $minCL and $minCL = $cc;\n\t\t\t$cc < $minCR and $minCR = $cc;\n\t\t\tif( $minC < $minCL + $minCR ){\n\t\t\t\tpush @ans, $minC;\n\t\t\t\t$switch = 1;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tpush @ans, $minCL + $minCR;\n\t\t\t\t$switch = 0;\n\t\t\t\t}\n\t\t\t}\n\t\telsif( $L < $min and $R > $max ){\n\t\t\t$switch = 1;\n\t\t\t$minC = $cc;\n\t\t\t$minCL = $cc;\n\t\t\t$minCR = $cc;\n\t\t\tpush @ans, $minC;\n\t\t\t$min = $L;\n\t\t\t$max = $R;\n\t\t\t}\n\t\telsif( $L < $min and $R == $max ){\n\t\t\t$switch = 1;\n\t\t\t$minC = $cc;\n\t\t\t$minCL = $cc;\n\t\t\t$cc < $minCR and $minCR = $cc;\n\t\t\tpush @ans, $minC;\n\t\t\t$min = $L;\n\t\t\t}\n\t\telsif( $L == $min and $R > $max ){\n\t\t\t$switch = 1;\n\t\t\t$minC = $cc;\n\t\t\t$minCR = $cc;\n\t\t\t$cc < $minCL and $minCL = $cc;\n\t\t\tpush @ans, $minC;\n\t\t\t$max = $R;\n\t\t\t}\n\t\telsif( $L < $min and $R < $max ){\n\t\t\t$switch = 0;\n\t\t\t$minCL = $cc;\n\t\t\t$min = $L;\n\t\t\tpush @ans, $minCL + $minCR;\n\t\t\t}\n\t\telsif( $L > $min and $R > $max ){\n\t\t\t$switch = 0;\n\t\t\t$minCR = $cc;\n\t\t\t$max = $R;\n\t\t\tpush @ans, $minCL + $minCR;\n\t\t\t}\n\t\telsif( $L == $min and $R < $max ){\n\t\t\t$cc < $minCL and $minCL = $cc;\n\t\t\tif( $switch == 1 and $minC < $minCL + $minCR ){\n\t\t\t\tpush @ans, $minC;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$switch = 0;\n\t\t\t\tpush @ans, $minCL + $minCR;\n\t\t\t\t}\n\t\t\t}\n\t\telsif( $L > $min and $R == $max ){\n\t\t\t$cc < $minCR and $minCR = $cc;\n\t\t\tif( $switch == 1 and $minC < $minCL + $minCR ){\n\t\t\t\tpush @ans, $minC;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$switch = 0;\n\t\t\t\tpush @ans, $minCL + $minCR;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint join \"\\n\", @ans;\n\t}"}], "src_uid": "ee773d908fc297cc692aaecf6af299c9"} {"nl": {"description": "Ashish has two strings $$$a$$$ and $$$b$$$, each of length $$$n$$$, and an integer $$$k$$$. The strings only contain lowercase English letters.He wants to convert string $$$a$$$ into string $$$b$$$ by performing some (possibly zero) operations on $$$a$$$.In one move, he can either choose an index $$$i$$$ ($$$1 \\leq i\\leq n-1$$$) and swap $$$a_i$$$ and $$$a_{i+1}$$$, or choose an index $$$i$$$ ($$$1 \\leq i \\leq n-k+1$$$) and if $$$a_i, a_{i+1}, \\ldots, a_{i+k-1}$$$ are all equal to some character $$$c$$$ ($$$c \\neq$$$ 'z'), replace each one with the next character $$$(c+1)$$$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $$$a$$$. Help Ashish determine if it is possible to convert string $$$a$$$ into $$$b$$$ after performing some (possibly zero) operations on it.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^5$$$)\u00a0\u2014 the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $$$n$$$ ($$$2 \\leq n \\leq 10^6$$$) and $$$k$$$ ($$$1 \\leq k \\leq n$$$). The second line of each test case contains the string $$$a$$$ of length $$$n$$$ consisting of lowercase English letters. The third line of each test case contains the string $$$b$$$ of length $$$n$$$ consisting of lowercase English letters. It is guaranteed that the sum of values $$$n$$$ among all test cases does not exceed $$$10^6$$$.", "output_spec": "For each test case, print \"Yes\" if Ashish can convert $$$a$$$ into $$$b$$$ after some moves, else print \"No\". You may print the letters of the answer in any case (upper or lower).", "sample_inputs": ["4\n3 3\nabc\nbcd\n4 2\nabba\nazza\n2 1\nzz\naa\n6 2\naaabba\nddddcc"], "sample_outputs": ["No\nYes\nNo\nYes"], "notes": "NoteIn the first test case it can be shown that it is impossible to convert $$$a$$$ into $$$b$$$.In the second test case,\"abba\" $$$\\xrightarrow{\\text{inc}}$$$ \"acca\" $$$\\xrightarrow{\\text{inc}}$$$ $$$\\ldots$$$ $$$\\xrightarrow{\\text{inc}}$$$ \"azza\".Here \"swap\" denotes an operation of the first type, and \"inc\" denotes an operation of the second type.In the fourth test case,\"aaabba\" $$$\\xrightarrow{\\text{swap}}$$$ \"aaabab\" $$$\\xrightarrow{\\text{swap}}$$$ \"aaaabb\" $$$\\xrightarrow{\\text{inc}}$$$ $$$\\ldots$$$ $$$\\xrightarrow{\\text{inc}}$$$ \"ddaabb\" $$$\\xrightarrow{\\text{inc}}$$$ $$$\\ldots$$$ $$$\\xrightarrow{\\text{inc}}$$$ \"ddddbb\" $$$\\xrightarrow{\\text{inc}}$$$ $$$\\ldots$$$ $$$\\xrightarrow{\\text{inc}}$$$ \"ddddcc\"."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy @al = split(//,\"abcdefghijklmnopqrstuvwxyz\");\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nwhile( $t -- > 0 ){\n my ($n,$k) = map { $_ - 0 } split(/\\s+/,);\n my $as = ;\n my $bs = ;\n \n my %ac = ();\n my %bc = ();\n foreach my $c1 (@al){\n $ac{$c1} = $bc{$c1} = 0;\n }\n \n for(my $i=0;$i<$n;$i++){\n my $a1 = substr($as,$i,1);\n my $b1 = substr($bs,$i,1);\n $ac{$a1} ++;\n $bc{$b1} ++;\n }\n \n my $car = 0;\n my $ng = undef;\n foreach my $c1 (@al){\n if( $ac{$c1} == $bc{$c1} ){\n # no problem\n } elsif( $c1 ne 'z' and $ac{$c1} > $bc{$c1} and ($ac{$c1} - $bc{$c1}) % $k == 0 ){\n my $v = $ac{$c1} - $bc{$c1};\n $car += $v;\n $ac{$c1} -= $v;\n } elsif( $ac{$c1} < $bc{$c1} and ($bc{$c1} - $ac{$c1}) % $k == 0 and $car >= ($bc{$c1} - $ac{$c1}) ){\n my $v = $bc{$c1} - $ac{$c1};\n $car -= $v;\n $ac{$c1} += $v;\n } else {\n $ng = 1;\n last;\n }\n }\n $ng = 1 if( $car > 0 );\n if( $ng ){\n print \"No\\n\";\n } else {\n print \"Yes\\n\";\n }\n}\n"}], "negative_code": [], "src_uid": "09d0f11bdb9eca2161dee83a335dd2b7"} {"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$$$)\u00a0\u2014 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 \u2014 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": "use v5.20;\nsub { \n &{sub { \n my $n = <>;\n my @s = map {ord($_) - ord '0'} split \"\", <>;\n for (my $i = 0; $i < $#s; $i += 2) {\n print $s[$i];\n }\n say \"\";\n }}\n while ($_[0]--)\n} -> (my $t = <>)\n"}], "negative_code": [{"source_code": "use v5.20;\nsub { \n &{sub { \n my $n = <>;\n my @s = map {ord($_) - ord '0'} split \"\", <>;\n my @ans = (0) x $n;\n $ans[0] = $ans[$n - 1] = $s[$n - 1];\n for (my $i = 0; $i < $n; $i++) {\n if ($s[$i] == $s[$i + $n - 1]) {\n $ans[$i] = $s[$i];\n }\n }\n say @ans;\n }}\n while ($_[0]--)\n} -> (my $t = <>)\n"}], "src_uid": "b37bbf1fe9ac5144cfa230633ccbdd79"} {"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\u00a0\u2014 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": "#! /usr/bin/perl\nuse strict;\nmy $row = ;\nchomp($row);\nmy @inputs_p = split(' ',$row);\nmy $h = $inputs_p[0]; \nmy $l = $inputs_p[1];\nmy $r = $l*$l/$h-$h;\nprintf(\"%.13f\",$r/2);"}], "negative_code": [], "src_uid": "2cd5807e3f9685d4eff88f2283904f0d"} {"nl": {"description": "You are given an equation: Ax2\u2009+\u2009Bx\u2009+\u2009C\u2009=\u20090. 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,\u2009B and C (\u2009-\u2009105\u2009\u2264\u2009A,\u2009B,\u2009C\u2009\u2264\u2009105). 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": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nmy ($a, $b, $c) = map int, split / /, <>;\nif (!$a and !$b and !$c) {\n print \"-1\\n\";\n} elsif (!$a and !$b and $c) {\n print \"0\\n\";\n} elsif (!$a) {\n my $x = -$c / $b;\n print \"1\\n$x\\n\";\n} else {\n my %roots;\n my $d = $b ** 2 - 4 * $a * $c;\n if ($d == 0) {\n\tmy $x = (-$b) / (2 * $a);\n\tprint \"1\\n$x\\n\";\n } elsif ($d > 0) {\n\tmy $x1 = (-$b - sqrt($d)) / (2 * $a);\n\tmy $x2 = (-$b + sqrt($d)) / (2 * $a);\n\t($x1, $x2) = ($x2, $x1) if ($x1 > $x2);\n\tprint \"2\\n$x1\\n$x2\\n\";\n } else {\n\tprint \"0\\n\";\n }\n}\n"}], "negative_code": [{"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nmy ($a, $b, $c) = map int, split / /, <>;\nif (!$a and !$b and !$c) {\n print \"-1\\n\";\n} elsif (!$a and !$b and $c) {\n print \"0\\n\";\n} elsif (!$a) {\n my $x = -$c / $b;\n print \"1\\n$x\\n\";\n} else {\n my %roots;\n my $d = $b ** 2 - 4 * $a * $c;\n if ($d >= 0) {\n\tmy $x = (-$b + sqrt($d)) / (2 * $a);\n\t$roots{$x} = 1;\n\t$x = (-$b - sqrt($d)) / (2 * $a);\n\t$roots{$x} = 1;\n\tmy @k = keys %roots;\n\tprint scalar @k, \"\\n$k[0]\\n\";\n\tif ($k[1]) { print \"$k[1]\\n\"; }\n } else {\n\tprint \"0\\n\";\n }\n}\n"}], "src_uid": "84372885f2263004b74ae753a2f358ac"} {"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,\u2009a2,\u2009... an. Nicholas does not want to break the sticks or glue them together. To make a h\u2009\u00d7\u2009w-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\u2009=\u2009w), 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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 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\u2009\u2264\u2009ai\u2009\u2264\u2009100).", "output_spec": "Print the single number \u2014 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": "while (<>){\n @_=split/ /,<>;\n @m=();\n for (@_){$m[$_]++}\n $a=0;\n for (@m){$a+=($_-$_%2)/2}\n print (($a-$a%2)/2);\n print \"\\n\"\n }"}], "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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the numbers.", "output_spec": "Output a single number\u00a0\u2014 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": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my $a= <$in>; chomp($a);\n my @a= split(' ', $a);\n\n my @z= grep {/^0$/} @a;\n my @nz= grep {!/^0$/} @a;\n my $x= scalar(@z);\n\n if(scalar(@nz)==0) {\n print $x;\n return;\n }\n\n my $y= 1;\n for (@nz) {\n $y*= $_;\n }\n\n for(@nz) {\n $x+= (abs($_) - 1);\n }\n\n # print \"x: $x y : $y\\n\";\n\n if(scalar(@z)>0) {\n if($y < 0) {\n $y=($y * (-1));\n }\n }\n\n if($y < 0) {\n $x+=2;\n }\n\n print $x;\n return;\n}\n\nmain() unless caller();\n\n"}], "negative_code": [{"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my $a= <$in>; chomp($a);\n my @a= split(' ', $a);\n\n my $x= scalar(grep {/0/} @a);\n\n my @nz= grep {!/0/} @a;\n\n if(scalar(@nz)==0) {\n print $x;\n return;\n }\n\n my $y= 1;\n for (@nz) {\n $y*= $_;\n }\n\n for(@nz) {\n $x+= (abs($_) - 1);\n }\n\n if($x>0) {\n if($y < 0) {\n $y=($y * (-1));\n }\n }\n\n if($y < 0) {\n $x+=2;\n }\n\n print $x;\n return;\n}\n\nmain() unless caller();\n\n"}, {"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my $a= <$in>; chomp($a);\n my @a= split(' ', $a);\n\n my $x= scalar(grep {/0/} @a);\n\n my @nz= grep {!/0/} @a;\n\n if(scalar(@nz)==0) {\n print $x;\n return;\n }\n\n my $y= 1;\n for (@nz) {\n $y*= $_;\n }\n\n for(@nz) {\n $x+= (abs($_) - 1);\n }\n\n if($y < 0) {\n $x+=2;\n }\n\n print $x;\n return;\n}\n\nmain() unless caller();\n\n"}, {"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my $a= <$in>; chomp($a);\n my @a= split(' ', $a);\n\n my $x= scalar(grep {/^0$/} @a);\n\n my @nz= grep {!/^0$/} @a;\n\n if(scalar(@nz)==0) {\n print $x;\n return;\n }\n\n my $y= 1;\n for (@nz) {\n $y*= $_;\n }\n\n for(@nz) {\n $x+= (abs($_) - 1);\n }\n\n if($x>0) {\n if($y < 0) {\n $y=($y * (-1));\n }\n }\n\n if($y < 0) {\n $x+=2;\n }\n\n print $x;\n return;\n}\n\nmain() unless caller();\n\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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B, $p ) = split;\n\t$_ = <>, chomp;\n\tmy $length = length;\n\tchop;\n\t$_ = reverse;\n\t\n\tmy $len = 0;\n\t\n\twhile( /./ ){\n\t\tif( /^A/ ){\n\t\t\tif( $p < $A ){\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t$p -= $A;\n\t\t\ts/^A+//;\n\t\t\t$len += length $&;\n\t\t\t}\n\t\telse{\n\t\t\tif( $p < $B ){\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t$p -= $B;\n\t\t\ts/^B+//;\n\t\t\t$len += length $&;\n\t\t\t}\n\t\t}\n\t\n\tprint $length - $len;\n\t}"}], "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$$$) \u2014 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": "#!/usr/bin/perl -w \nuse feature \":5.20.1\";\n\nsub is_palindrome {\n my $ent = $_[0];\n my @string = split(\"\", $ent);\n my $fim = @string - 1;\n my $ini = 0;\n while($ini < $fim) {\n if($string[$ini] ne $string[$fim]){\n return 0;\n }\n $ini++;\n $fim--;\n }\n return 1;\n}\n\nchomp($line = <>);\n($n, $m) = split \" \", $line;\n@list = ();\n\nfor($i = 0; $i < $n; $i++){\n chomp($line = <>);\n push(@list, $line);\n}\n\n$m = @list;\n$n = $m;\n@v_palin = ();\n@par = ();\n\nfor($i = 0; $i < $n; $i++){\n push(@par, -1);\n}\n\nfor($i = 0; $i < $n; $i++){\n if(is_palindrome($list[$i])){\n push(@v_palin, $i);\n }\n}\n\nfor($i = 0; $i < $n; $i++){\n for($j = 0; $j < $n; $j++){\n if($i == $j || $par[$i] != -1 || $par[$j] != -1){\n next;\n }\n $aux = scalar reverse ($list[$j]);\n if($list[$i] eq $aux){\n $par[$i] = $j;\n $par[$j] = $i;\n last;\n }\n }\n}\n\n$res = \"\";\nfor($i = 0; $i < $n; $i++){\n if($par[$i] != -1 && $par[$i] > $i){\n $res .= $list[$i];\n }\n}\n$res2 = scalar reverse($res);\nforeach $i (@v_palin){\n if($par[$i] == -1){\n $res .= $list[$i];\n last;\n }\n}\n\n$res .= $res2;\n\nsay length($res);\nif(length($res)){\n say $res;\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w \nuse feature \":5.20.1\";\n\nsub is_palindrome {\n my $ent = $_[0];\n my @string = split(\"\", $ent);\n my $fim = @string - 1;\n my $ini = 0;\n while($ini < $fim) {\n if($string[$ini] ne $string[$fim]){\n return 0;\n }\n $ini++;\n $fim--;\n }\n return 1;\n}\n\nchomp($line = <>);\n($n, $m) = split \" \", $line;\n@list = ();\n\nfor($i = 0; $i < $n; $i++){\n chomp($line = <>);\n push(@list, $line);\n}\n\n$m = @list;\n$n = $m;\n@v_palin = ();\n@par = ();\n\nfor($i = 0; $i < $n; $i++){\n push(@par, -1);\n}\n\nfor($i = 0; $i < $n; $i++){\n if(is_palindrome($list[$i])){\n push(@v_palin, $i);\n }\n}\n\nfor($i = 0; $i < $n; $i++){\n for($j = 0; $j < $n; $j++){\n if($i == $j || $par[$i] != -1 || $par[$j] != -1){\n next;\n }\n $aux = scalar reverse ($list[$j]);\n if($list[$i] eq $aux){\n $par[$i] = $j;\n $par[$j] = $i;\n last;\n }\n }\n}\n\n$res = \"\";\nfor($i = 0; $i < $n; $i++){\n if($par[$i] != -1 && $par[$i] > $i){\n $res .= $list[$i];\n }\n}\n\nforeach $i (@v_palin){\n if($par[$i] == -1){\n $res .= $list[$i];\n last;\n }\n}\n\nfor($i = 0; $i < $n; $i++){\n if($par[$i] != -1 && $par[$i] > $i){\n $res .= $list[$par[$i]];\n }\n}\n\nsay length($res);\nif(length($res)){\n say $res;\n}"}], "src_uid": "554115bec46bb436a0a1ddf8c05a2d08"} {"nl": {"description": "You are given a rooted tree with vertices numerated from $$$1$$$ to $$$n$$$. A tree is a connected graph without cycles. A rooted tree has a special vertex named root.Ancestors of the vertex $$$i$$$ are all vertices on the path from the root to the vertex $$$i$$$, except the vertex $$$i$$$ itself. The parent of the vertex $$$i$$$ is the nearest to the vertex $$$i$$$ ancestor of $$$i$$$. Each vertex is a child of its parent. In the given tree the parent of the vertex $$$i$$$ is the vertex $$$p_i$$$. For the root, the value $$$p_i$$$ is $$$-1$$$. An example of a tree with $$$n=8$$$, the root is vertex $$$5$$$. The parent of the vertex $$$2$$$ is vertex $$$3$$$, the parent of the vertex $$$1$$$ is vertex $$$5$$$. The ancestors of the vertex $$$6$$$ are vertices $$$4$$$ and $$$5$$$, the ancestors of the vertex $$$7$$$ are vertices $$$8$$$, $$$3$$$ and $$$5$$$ You noticed that some vertices do not respect others. In particular, if $$$c_i = 1$$$, then the vertex $$$i$$$ does not respect any of its ancestors, and if $$$c_i = 0$$$, it respects all of them.You decided to delete vertices from the tree one by one. On each step you select such a non-root vertex that it does not respect its parent and none of its children respects it. If there are several such vertices, you select the one with the smallest number. When you delete this vertex $$$v$$$, all children of $$$v$$$ become connected with the parent of $$$v$$$. An example of deletion of the vertex $$$7$$$. Once there are no vertices matching the criteria for deletion, you stop the process. Print the order in which you will delete the vertices. Note that this order is unique.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$)\u00a0\u2014 the number of vertices in the tree. The next $$$n$$$ lines describe the tree: the $$$i$$$-th line contains two integers $$$p_i$$$ and $$$c_i$$$ ($$$1 \\le p_i \\le n$$$, $$$0 \\le c_i \\le 1$$$), where $$$p_i$$$ is the parent of the vertex $$$i$$$, and $$$c_i = 0$$$, if the vertex $$$i$$$ respects its parents, and $$$c_i = 1$$$, if the vertex $$$i$$$ does not respect any of its parents. The root of the tree has $$$-1$$$ instead of the parent index, also, $$$c_i=0$$$ for the root. It is guaranteed that the values $$$p_i$$$ define a rooted tree with $$$n$$$ vertices.", "output_spec": "In case there is at least one vertex to delete, print the only line containing the indices of the vertices you will delete in the order you delete them. Otherwise print a single integer $$$-1$$$.", "sample_inputs": ["5\n3 1\n1 1\n-1 0\n2 1\n3 0", "5\n-1 0\n1 1\n1 1\n2 0\n3 0", "8\n2 1\n-1 0\n1 0\n1 1\n1 1\n4 0\n5 1\n7 0"], "sample_outputs": ["1 2 4", "-1", "5"], "notes": "NoteThe deletion process in the first example is as follows (see the picture below, the vertices with $$$c_i=1$$$ are in yellow): first you will delete the vertex $$$1$$$, because it does not respect ancestors and all its children (the vertex $$$2$$$) do not respect it, and $$$1$$$ is the smallest index among such vertices; the vertex $$$2$$$ will be connected with the vertex $$$3$$$ after deletion; then you will delete the vertex $$$2$$$, because it does not respect ancestors and all its children (the only vertex $$$4$$$) do not respect it; the vertex $$$4$$$ will be connected with the vertex $$$3$$$; then you will delete the vertex $$$4$$$, because it does not respect ancestors and all its children (there are none) do not respect it (vacuous truth); you will just delete the vertex $$$4$$$; there are no more vertices to delete. In the second example you don't need to delete any vertex: vertices $$$2$$$ and $$$3$$$ have children that respect them; vertices $$$4$$$ and $$$5$$$ respect ancestors. In the third example the tree will change this way: "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy %h;\n\tmy %g;\n\tmy $root;\n\t@_ = ();\n\t\n\tmap { \n\t\tmy( $v, $c ) = split ' ', <>;\n\t\t$h{ $_ }{ $v } = 1;\n\t\t$h{ $v }{ $_ } = 2;\n\t\t$g{ $_ } = $c;\n\t\t$v == -1 and $root = $_;\n\t\t} 1 .. $_;\n\t\n\t$g{ -1 } = 0;\n\t\n\t$debug and print $root;\n\t\n\tfor my $key ( keys %h ){\n\t\tnext if not $g{ $key };\n\t\tnext if grep { not $g{ $_ } } grep { $h{ $key }{ $_ } == 2 } keys %{ $h{ $key } };\n\t\tpush @_, $key;\n\t\t}\n\t\n\t@_ or push @_, -1;\n\t\n\tprint join ' ', sort { $a <=> $b } @_;\n\t}"}], "negative_code": [], "src_uid": "1b975c5a13a2ad528b668a7c68c089f6"} {"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 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1\u2009\u2264\u2009hi\u2009\u2264\u2009109), where hi equals the number of floors in the i-th house. ", "output_spec": "Print n integers a1,\u2009a2,\u2009...,\u2009an, 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": "<>;\nprint join q{ }, reverse\n map {\n $_ > ( $m // 0 )\n ? do { $m = $_; 0 }\n : ( $m + 1 )\n - $_\n }\n reverse split / /,\n <>;\n"}, {"source_code": "#!perl\nsub max\n{\n my $max = 0; \n my @a = @{$_[0]};\n for my $i (@a)\n {\n $max = $max < $i ? $i : $max;\n }\n return $max;\n} \n\n$\\ = \"\\n\";\n$n = <>;\n@a = split /\\s/, <>;\n\nfor my $i (0..$#a)\n{\n $h{@a[$i]} = [] if !$h{@a[$i]};\n push @{$h{@a[$i]}}, $i;\n} \n\n@keys = sort {$b <=> $a} (keys %h);\n\nfor my $i (0..$#keys)\n{\n $max = max($h{@keys[$i]});\n push @a1, [$max, @a[$max]];\n}\n\n@a1 = sort {@{$b}[1] <=> @{$a}[1]} @a1;\n\n$local_max_index = 0;\n@marks = (-1);\npush @marks, @{@a1[0]}[0];\n\nfor my $i (1..$#a1)\n{\n if (@{@a1[$i]}[0] > @{@a1[$local_max_index]}[0])\n {\n $local_max_index = $i; \n push @marks, @{@a1[$local_max_index]}[0];\n }\n}\n\nfor my $i (0..$#marks - 1)\n{\n for my $j (@marks[$i] + 1..@marks[$i + 1])\n {\n @a2[$j] = @a[@marks[$i + 1]] - @a[$j];\n @a2[$j]++ if $j != @marks[$i + 1];\n }\n}\n\nprint \"@a2\";"}, {"source_code": "$n=<>;\n@h=split ' ',<>;\n$el[$n-1]=$h[$n-1];\nfor ($i=$n-2; $i>=0; $i--)\n{\n\tif ($h[$i]>$el[$i+1])\n\t{\n\t\t$el[$i]=$h[$i];\n\t}\n\telse\n\t{\n\t\t$el[$i]=$el[$i+1];\n\t}\n}\nfor ($i=0; $i<$n-1; $i++)\n{\n\tif ($el[$i+1]>=$h[$i])\n\t{\n\t\tprint $el[$i]-$h[$i]+1;\n\t}\n\telse\n\t{\n\t\tprint '0';\n\t}\n\tprint ' ';\n}\nprint '0';"}, {"source_code": "($_ > $max and $max = $_), unshift @a, $max for reverse @_ = split ' ', (<>, <>); \nshift @a;\nprint join ' ', map { (sort {$b <=> $a} ( (shift @a) - $_ + 1, 0) )[0] } @_"}], "negative_code": [{"source_code": "<>;\nprint join q{ }, reverse\n map { $m = $_ > ( $m // 0 ) ? $_ : $m; $_ >= $m ? 0 : ( $m + 1 ) - $_ }\n reverse split / /, <>;"}, {"source_code": "#!perl\n$\\ = \"\\n\";\n$n = <>;\n@a = split /\\s/, <>;\n\nfor my $i (0..$#a)\n{\n push @a1, [$i, @a[$i]];\n} \n\n@a1 = sort {@{$b}[1] <=> @{$a}[1]} @a1;\n\n$local_max_index = 0;\n@marks = (-1);\npush @marks, @{@a1[0]}[0];\n\nfor my $i (1..$#a)\n{\n if (@{@a1[$i]}[0] > @{@a1[$local_max_index]}[0])\n {\n $local_max_index = $i; \n push @marks, @{@a1[$local_max_index]}[0];\n }\n}\n\nfor my $i (0..$#marks - 1)\n{\n for my $j (@marks[$i] + 1..@marks[$i + 1])\n {\n @a2[$j] = @a[@marks[$i + 1]] - @a[$j];\n @a2[$j]++ if @a2[$j] != 0;\n }\n}\n\nprint \"@a2\";"}, {"source_code": "#!perl\n$\\ = \"\\n\";\n$n = <>;\n@a = split /\\s/, <>;\n\nfor my $i (0..$#a)\n{\n push @a1, [$i, @a[$i]];\n} \n\n@a1 = sort {@{$b}[1] <=> @{$a}[1]} @a1;\n\n$local_max_index = 0;\n@marks = (-1);\npush @marks, @{@a1[0]}[0];\n\nfor my $i (1..$#a)\n{\n if (@{@a1[$i]}[0] > @{@a1[$local_max_index]}[0])\n {\n $local_max_index = $i; \n push @marks, @{@a1[$local_max_index]}[0];\n }\n}\n\nfor my $i (0..$#marks - 1)\n{\n for my $j (@marks[$i] + 1..@marks[$i + 1])\n {\n @a2[$j] = @a[@marks[$i + 1]] - @a[$j] + 1;\n }\n}\n\nprint \"@a2\";"}], "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\u2009\u00d7\u2009n map, or determine that no such way exists. ", "input_spec": "The single line contains two positive integers n, k (1\u2009\u2264\u2009n\u2009\u2264\u2009100, 0\u2009\u2264\u2009k\u2009\u2264\u2009n2) \u2014 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": "#!/usr/bin/perl -w\nuse strict;\nuse warnings;\n\nmy $l = <>;\nchomp $l;\n$l =~ /^(\\d+) (\\d+)$/;\nmy $n = $1;\nmy $k = $2;\n\nmy @res;\nfor (1..$n) {\n my $line = [];\n for (1..$n) {\n push(@{$line}, 'S');\n }\n push(@res, $line);\n}\n\nmy ($i, $j) = (0, 0);\nmy $f = 1;\nwhile ($k > 0) {\n $res[$j][$i] = 'L';\n $i += 2;\n $k--;\n if ($i >= $n) {\n if ($f == 1) {\n $i = 1;\n $f = 0;\n } else {\n $i = 0;\n $f = 1;\n }\n $j++;\n if ($j >= $n) {\n last;\n }\n }\n}\n\nif ($k == 0) {\n print \"YES\\n\";\n foreach my $line (@res) {\n print join('', @{$line}), \"\\n\";\n }\n} else {\n print \"NO\\n\";\n}\n\n"}, {"source_code": "$, = $/;\n($n, $k) = split \" \", <>;\n$_ = reverse 'S' x $n ** 2 . join 'S', ('L') x $k;\npush @_, ++ $i % 2 ? $& : scalar reverse $& while /.{$n}/g and $i < $n;\nprint $k == (() = \"@_\" =~ /L/g) ? ('YES', @_) : 'NO'"}], "negative_code": [{"source_code": "$, = $/;\n($n, $k) = split \" \", <>;\n$_ = reverse 'S' x $n ** 2 . join 'S', ('L') x $k;\npush @_, ++ $i % 2 ? $& : + reverse $& while /.{$n}/g and $i < $n;\nprint $k == (() = \"@_\" =~ /L/g) ? ('YES', @_) : 'NO'"}], "src_uid": "b15bc7ff01f239a7e4377868a7dda0a6"} {"nl": {"description": "Valera had two bags of potatoes, the first of these bags contains x (x\u2009\u2265\u20091) potatoes, and the second \u2014 y (y\u2009\u2265\u20091) potatoes. Valera \u2014 very scattered boy, so the first bag of potatoes (it contains x potatoes) Valera lost. Valera remembers that the total amount of potatoes (x\u2009+\u2009y) 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\u2009\u2264\u2009y,\u2009k,\u2009n\u2009\u2264\u2009109; \u2009\u2264\u2009105).", "output_spec": "Print the list of whitespace-separated integers \u2014 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": "($y, $k, $n) = split \" \", <>;\n$to_next = $k - ($y % $k);\nif ($y + $to_next > $n ) {\n print -1;\n} else {\n print join \" \", map { $k * $_ + $to_next } 0 .. int (($n - $y - $to_next) / $k);\n}"}], "negative_code": [{"source_code": "($y, $k, $n) = split \" \", <>;\n$to_next = $k - ($y % $k);\nif ($y + $to_next >= $n) {\n print -1;\n} else {\n print join \" \", map { $k * $_ + $to_next } 0 .. int (($n - $y - $to_next) / $k);\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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ .= <>;\n\t@_ = m/\\w/g;\n\tmy %h;\n\tmap $h{ $_ } ++, @_;\n\t\n\t$_ = join '', sort values %h;\n\t\n\tprint do {\n\t\tif( 0 ){ ; }\n\t\telsif( m/^1111$/ ){ 3 }\n\t\telsif( m/^112$/ ){ 2 }\n\t\telsif( m/^13$/ ){ 1 }\n\t\telsif( m/^22$/ ){ 1 }\n\t\telsif( m/^4$/ ){ 0 }\n\t\t};\n\t}"}], "negative_code": [], "src_uid": "a9143235c8e2b6b188ea3fc8a90f0c80"} {"nl": {"description": "There is a graph of $$$n$$$ rows and $$$10^6 + 2$$$ columns, where rows are numbered from $$$1$$$ to $$$n$$$ and columns from $$$0$$$ to $$$10^6 + 1$$$: Let's denote the node in the row $$$i$$$ and column $$$j$$$ by $$$(i, j)$$$.Initially for each $$$i$$$ the $$$i$$$-th row has exactly one obstacle \u2014 at node $$$(i, a_i)$$$. You want to move some obstacles so that you can reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs $$$u$$$ or $$$v$$$ coins, as below: If there is an obstacle in the node $$$(i, j)$$$, you can use $$$u$$$ coins to move it to $$$(i-1, j)$$$ or $$$(i+1, j)$$$, if such node exists and if there is no obstacle in that node currently. If there is an obstacle in the node $$$(i, j)$$$, you can use $$$v$$$ coins to move it to $$$(i, j-1)$$$ or $$$(i, j+1)$$$, if such node exists and if there is no obstacle in that node currently. Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from $$$(1,1)$$$ to $$$(0,1)$$$. Refer to the picture above for a better understanding. Now you need to calculate the minimal number of coins you need to spend to be able to reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph without passing through obstacles.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) \u2014 the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$u$$$ and $$$v$$$ ($$$2 \\le n \\le 100$$$, $$$1 \\le u, v \\le 10^9$$$)\u00a0\u2014 the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^6$$$)\u00a0\u2014 where $$$a_i$$$ represents that the obstacle in the $$$i$$$-th row is in node $$$(i, a_i)$$$. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^4$$$.", "output_spec": "For each test case, output a single integer\u00a0\u2014 the minimal number of coins you need to spend to be able to reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph without passing through obstacles. It can be shown that under the constraints of the problem there is always a way to make such a trip possible.", "sample_inputs": ["3\n2 3 4\n2 2\n2 3 4\n3 2\n2 4 3\n3 2"], "sample_outputs": ["7\n3\n3"], "notes": "NoteIn the first sample, two obstacles are at $$$(1, 2)$$$ and $$$(2,2)$$$. You can move the obstacle on $$$(2, 2)$$$ to $$$(2, 3)$$$, then to $$$(1, 3)$$$. The total cost is $$$u+v = 7$$$ coins. In the second sample, two obstacles are at $$$(1, 3)$$$ and $$$(2,2)$$$. You can move the obstacle on $$$(1, 3)$$$ to $$$(2, 3)$$$. The cost is $$$u = 3$$$ coins. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $u, $v ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tif( @_ == grep $_[ 0 ] == $_, @_ ){\n\t\tprint $u + $v < $v * 2 ? $u + $v : $v * 2;\n\t\tnext;\n\t\t}\n\t\n\tmy $OK = 1;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tmy $abs = abs( $_[ $i ] - $_[ $i + 1 ] );\n\t\t\n\t\tif( $abs > 1 ){\n\t\t\t$OK = 0;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tif( $OK ){\n\t\tprint $u > $v ? $v : $u;\n\t\t}\n\telse{\n\t\tprint 0;\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $u, $v ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tif( @_ == grep $_[ 0 ] == $_, @_ ){\n\t\tprint $u + $v;\n\t\tnext;\n\t\t}\n\t\n\tmy $OK = 1;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tmy $abs = abs( $_[ $i ] - $_[ $i + 1 ] );\n\t\t\n\t\tif( $abs > 1 ){\n\t\t\t$OK = 0;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tif( $OK ){\n\t\tprint $u > $v ? $v : $u;\n\t\t}\n\telse{\n\t\tprint 0;\n\t\t}\n\t}"}], "src_uid": "7f502f2fd150a2ded948826960d123cd"} {"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$$$) \u2014 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 \u2014 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": "#!/bin/usr/perl\n\nmy $t=<>;\nchomp($t);\nwhile ($t--){\n\tmy $line = <>;\n\tchomp($line);\n\t@numbers = split(\" \",$line);\n\tmy $a = $numbers[0];\n\tmy $b = $numbers[1];\n\tprint($a+$b);\n\tprint(\"\\n\");\n}"}, {"source_code": "s# #print$`+$',$/#e for<>"}, {"source_code": "<>;s# #print$`+$',$/#e while<>"}], "negative_code": [], "src_uid": "27ddccc777ef9040284ab6314cbd70e7"} {"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\u2009\u2264\u2009n,\u2009t1,\u2009t2\u2009\u2264\u20091000;\u00a01\u2009\u2264\u2009k\u2009\u2264\u2009100) \u2014 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\u2009\u2264\u2009i\u2009\u2264\u2009n) line contains space-separated integers ai, bi (1\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u20091000) \u2014 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\u00b73\u00b70.5\u2009+\u20094\u00b73\u2009>\u20094\u00b73\u00b70.5\u2009+\u20092\u00b73. "}, "positive_code": [{"source_code": "#!/usr/bin/perl -w\nuse strict;\nuse List::Util qw[min max];\n\nmy ($n, $t1, $t2, $k) = split /\\D/, <>;\nmy %len = ();\nfor my $i (1..$n) {\n\tmy ($a, $b) = split /\\D/, <>;\n\t$len{$i} = max ( ($t1*$a*(100-$k)/100+$t2*$b), ($t1*$b*(100-$k)/100+$t2*$a));\n}\n\nforeach my $key (sort lensort keys %len) {\n\tprint \"$key \";\n\tprintf \"%.2f\\n\", $len{$key};\n}\n\nsub lensort {\n\tif ($len{$a} == $len{$b}) {\n\t\t$a <=> $b;\n\t} else {\n\t\t$len{$b} <=> $len{$a};\n\t}\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w\nuse strict;\nuse List::Util qw[min max];\n\nmy ($n, $t1, $t2, $k) = split /\\D/, <>;\nmy %len = ();\nfor my $i (1..$n) {\n\tmy ($a, $b) = split /\\D/, <>;\n\t$len{$i} = max ( ($t1*$a*(100-$k)/100+$t2*$b), ($t1*$b*(100-$k)/100+$t2*$a));\n}\n\nforeach my $key (reverse sort {$len{$a}<=>$len{$b}} keys %len) {\n\tprint \"$key \";\n\tprintf \"%.2f\\n\", $len{$key};\n}"}], "src_uid": "a89f9310996eb23254d07e52544e30ae"} {"nl": {"description": "Mishka has got n empty boxes. For every i (1\u2009\u2264\u2009i\u2009\u2264\u2009n), 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\u2009<\u2009aj). 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\u2009\u2264\u2009n\u2009\u2264\u20095000) \u2014 the number of boxes Mishka has got. The second line contains n integers a1, a2, ..., an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109), 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": "<>;\n\nmap $h{ $_ } ++, split ' ', <>;\n\t\nprint '' . ( sort { $b <=> $a } values %h )[ 0 ]"}], "negative_code": [], "src_uid": "0cbd3eee259b1436f82e259be7d7ee0e"} {"nl": {"description": "Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).To reverse the i-th string Vasiliy has to spent ci units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.String A is lexicographically smaller than string B if it is shorter than B (|A|\u2009<\u2009|B|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in A is smaller than the character in B.For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.", "input_spec": "The first line of the input contains a single integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of strings. The second line contains n integers ci (0\u2009\u2264\u2009ci\u2009\u2264\u2009109), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string. Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed 100\u2009000.", "output_spec": "If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print \u2009-\u20091. Otherwise, print the minimum total amount of energy Vasiliy has to spent.", "sample_inputs": ["2\n1 2\nba\nac", "3\n1 3 1\naa\nba\nac", "2\n5 5\nbbb\naaa", "2\n3 3\naaa\naa"], "sample_outputs": ["1", "1", "-1", "-1"], "notes": "NoteIn the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is \u2009-\u20091.In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string \"aa\" should go before string \"aaa\", thus the answer is \u2009-\u20091."}, "positive_code": [{"source_code": "\n\n\n\n\n\n\nsub min {\n return ($_[0] < $_[1] ? ($_[0]) : ($_[1]));\n}\n \n\n\nmy $bignumber = (10 ** 15);\nmy $n = ;\nchomp ( $n );\nmy $str = ;\nchomp ( $str );\nmy @costs = split m/ /, $str;\n\nmy @arr = ();\nfor (1..$n) {\n my @slice = ();\n $str = ;\n chomp ( $str );\n push (@arr, $str);\n}\n\n\n\n\n\nmy @dp = ();\n$dp[ 0 ][ 0 ] = 0;\n$dp[ 0 ][ 1 ] = $costs[ 0 ];\n\nmy $entered;\nfor ( my $i = 1; $i < (scalar @arr); $i += 1 ) {\n\n my $rev_prev = reverse ($arr[ $i-1 ]);\n my $rev_curr = reverse ($arr[ $i ]);\n\n $dp[ $i ][ 0 ] = $dp[ $i ][ 1 ] = $bignumber;\n\n $entered = 0;\n\n if ( $arr[ $i ] ge $arr[ $i-1 ] and\n\t $dp[ $i-1 ][ 0 ] != -1 ) {\n\n\t$dp[ $i ][ 0 ] = &min ( $dp[ $i ][ 0 ],\n\t\t\t\t$dp[ $i-1 ][ 0 ] );\n\t$entered = 1;\n }\n\n if ( $arr[ $i ] ge ($rev_prev) and\n\t $dp[ $i-1 ][ 1 ] != -1 ) {\n\n\t$dp[ $i ][ 0 ] = &min ( $dp[ $i ][ 0 ],\n\t\t\t\t$dp[ $i-1 ][ 1 ] );\n\t$entered = 1;\n }\n\n\n if ( not $entered ) {\n\t$dp[ $i ][ 0 ] = -1;\n }\n\t\t \n $entered = 0;\n\n if ( ($rev_curr) ge $arr[ $i-1 ] and\n\t $dp[ $i-1 ][ 0 ] != -1 ) {\n\n\t$dp[ $i ][ 1 ] = &min ( $dp[ $i ][ 1 ],\n\t\t\t\t$dp[ $i-1 ][ 0 ] + $costs[ $i ] );\n\t$entered = 1;\n }\n\n if ( ($rev_curr) ge ($rev_prev) and\n\t $dp[ $i-1 ][ 1 ] != -1 ) {\n\n\t$dp[ $i ][ 1 ] = &min ( $dp[ $i ][ 1 ],\n\t\t\t\t$dp[ $i-1 ][ 1 ] + $costs[ $i ] );\n\t$entered = 1;\n }\n\n if ( not $entered ) {\n\t$dp[ $i ][ 1 ] = -1;\n }\n\n\n\n}\n\n\n\n\n$entered = 0;\nmy $answer = $bignumber;\nif ( $dp[ $n-1 ][ 0 ] != -1 ) {\n $answer = &min ( $answer,\n\t\t $dp[ $n-1 ][ 0 ] );\n $entered = 1;\n}\n\nif ( $dp[ $n-1 ][ 1 ] != -1 ) {\n $answer = &min ( $answer,\n\t\t $dp[ $n-1 ][ 1 ] );\n $entered = 1;\n}\n\nif ( not $entered ) {\n print \"-1\\n\";\n}\nelse {\n print \"$answer\\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"}], "negative_code": [], "src_uid": "91cfd24b8d608eb379f709f4509ecd2d"} {"nl": {"description": "Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat. There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1,\u2009...,\u2009an and p1,\u2009...,\u2009pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days. ", "input_spec": "The first line of input contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105), the number of days. In the next n lines, i-th line contains two integers ai and pi (1\u2009\u2264\u2009ai,\u2009pi\u2009\u2264\u2009100), the amount of meat Duff needs and the cost of meat in that day.", "output_spec": "Print the minimum money needed to keep Duff happy for n days, in one line.", "sample_inputs": ["3\n1 3\n2 2\n3 1", "3\n1 3\n2 1\n3 2"], "sample_outputs": ["10", "8"], "notes": "NoteIn the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse 5.012;\nuse warnings;\n\nmy $n = <>;\nmy $cheapest = 101;\nmy $cost;\nwhile ($n--) {\n\tchomp(my $line = <>);\n\tmy ($a, $p) = split / /, $line;\n\t$cheapest = $p if $p < $cheapest;\n\t$cost += $a * $cheapest;\n}\nprint $cost;\n"}, {"source_code": "<>;\n$min = ~0;\n( $min > (split)[1] and $min = (split)[1] ), $sum += $_ * $min for <>;\nprint $sum"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nsub argmin {\n my ($aref) = @_;\n\n my $argmin = 0;\n for my $i (1..$#$aref) {\n if ($aref->[$i] < $aref->[$argmin]) {\n $argmin = $i;\n }\n }\n $argmin;\n}\n\nsub sum_range {\n my ($aref, $l, $r) = @_;\n\n my $sum = 0;\n for my $i ($l..$r) {\n $sum += $aref->[$i];\n }\n $sum;\n}\n\nmy @eat;\nmy @price;\n\nmy $n = <>;\nfor (1..$n) {\n my ($eat, $price) = split ' ', <>;\n push @eat, $eat;\n push @price, $price;\n}\n\nmy $cost = 0;\n\nwhile (@eat) {\n my $i = argmin(\\@price);\n $cost += $price[$i] * sum_range(\\@eat, $i, $#eat);\n splice @price, $i;\n splice @eat, $i;\n}\n\nsay $cost;\n"}, {"source_code": "$,=\" \";\nmy $n=<>;\nchomp($n);\n$n--;\nmy ($x,$y)=split / /,<>;\nchomp($x,$y);\nmy $amount=$y;\nmy $cost=$x*$amount;\nwhile($n--)\n{\n ($x,$y)=split / /,<>;\n chomp($x,$y);\n if($y<$amount)\n {\n $amount=$y;\n }\n $cost+=$x*$amount;\n}\nprint $cost , \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nwhile (<>) {\n chomp;\n $n = $_;\n @p = ();\n @a = ();\n \n for ( 1..$n ) {\n $line = <>;\n chomp($line);\n ($a0, $p0) = split / /, $line;\n \n push @a, $a0;\n \n if ( ($len = @p) == 0 || $p0 < $p[-1] ) {\n push @p, $p0;\n } else {\n push @p, $p[-1];\n }\n \n }\n $sum = 0;\n \n for ( 0..($n-1) ) {\n $sum += $a[$_] * $p[$_];\n }\n $\\ = $/; \n print $sum;\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse warnings;\nuse strict;\n\n<>;\nmy $m = 111;\nmy $ans;\nfor(<>)\n{\n my ($a, $p) = split(' ', $_);\n $m = ($p < $m) ? $p : $m;\n \n $ans += $a * $m;\n}\n\nprint $ans;\n"}, {"source_code": "<>;\n$min = ~0;\n( $min > (split)[1] and $min = (split)[1] ), $sum += $_ * $min for <>;\nprint $sum"}], "negative_code": [], "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\u2009\u2264\u2009\u2009n\u2009\u2264\u2009100). The next line contains an integer p (0\u2009\u2264\u2009p\u2009\u2264\u2009n) at first, then follows p distinct integers a1,\u2009a2,\u2009...,\u2009ap (1\u2009\u2264\u2009ai\u2009\u2264\u2009n). 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": "my $n = <>;\n($na, @a) = split \" \", <>;\n($nb, @b) = (split \" \", <>);\npush @b, @a;\n\n%k = ();\ngrep { $k{$_} = $_ } @b;\n\nprint scalar keys %k == $n? \"I become the guy.\\n\" : \"Oh, my keyboard!\\n\";"}, {"source_code": "my $n = <>;\nmy %hash;\nmy @a = split \" \", <>;\nmy @b = split \" \", <>;\nshift @a;\nshift @b;\n$hash{$_} = 0 foreach(1..$n);\n$hash{$_} = 1 foreach(@a);\n$hash{$_} = 1 foreach(@b);\nmy $ans = \"I become the guy.\\n\";\nforeach(values %hash) {\n $ans = \"Oh, my keyboard!\\n\" if ($_ == 0);\n}\nprint $ans;\n"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nchomp($n = <>);\nmy %tb;\n@a = split / /, <>;\n$tb{int($_)}=1 foreach(@a[1..$#a]);\n@a = split / /, <>;\n$tb{int($_)}=1 foreach(@a[1..$#a]);\nscalar(keys %tb)==$n and say \"I become the guy.\" or say \"Oh, my keyboard!\";\n"}, {"source_code": "chomp (my $n = <>);\n\nchomp (my @ps = split / /, <>);\nchomp (my @qs = split / /, <>);\n\nshift @ps;\nshift @qs;\n\nmy %ls;\n\n$ls{$_}++ foreach (@ps, @qs);\n\nif ((scalar keys %ls) == $n) {\n printf \"I become the guy.\\n\";\n} else {\n printf \"Oh, my keyboard!\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n#pass array by reference\n\n\nmy $n = <>;\nmy @arr = (0) x $n;\nmy @arr1 = split(' ',<>); shift(@arr1);\nmy @arr2 = split(' ',<>); shift(@arr2);\n\nforeach(@arr1){\n $arr[$_-1] = 1;\n}\nforeach(@arr2){\n $arr[$_-1] = 1;\n}\nmy $yes = 1;\nforeach(@arr){\n if($_ == 0){\n $yes=0;\n last;\n }\n}\nif($yes == 1){\n print \"I become the guy.\\n\";\n}\nelse{\n print \"Oh, my keyboard!\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nmy $n = ;\n\nmy @first = split(' ', );\nmy @second = split(' ', );\n\nmy %set;\n\nshift @first;\nshift @second;\nforeach my $level ((@first, @second))\n{\n unless ($level == 0)\n {\n $set{$level} = 1;\n }\n}\nmy $size = keys %set;\nif ($size == $n) {\n print \"I become the guy.\";\n}\nelse {\n print \"Oh, my keyboard!\";\n}"}], "negative_code": [{"source_code": "my $n = <>;\nmy %hash;\nmy @a = split \" \", <>;\npush @a, split \" \", <>;\n$hash{$_} = 0 foreach(1..$n);\n$hash{$_} = 1 foreach(@a);\nmy $ans = \"I become the guy.\\n\";\nforeach(values %hash) {\n $ans = \"Oh, my keyboard!\\n\" if ($_ == 0);\n}\nprint $ans;"}, {"source_code": "my $n = <>;\n\nmy @ps = split / /, <>;\nmy @qs = split / /, <>;\n\nshift @ps;\nshift @qs;\n\nmy %ls;\n\n$ls{$_}++ foreach (@ps, @qs);\n\nif (scalar keys %ls == $n) {\n printf \"I become the guy.\\n\";\n} else {\n printf \"Oh, my keyboard!\\n\";\n}"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\n\nmy $n = <>;\nmy @arr = (0) x $n;\nmy @arr1 = split(' ',readline);\nmy @arr2 = split(' ',readline);\n\nforeach(@arr1){\n $arr[$_-1] = 1;\n}\nforeach(@arr2){\n $arr[$_-1] = 1;\n}\nmy $yes = 1;\nforeach(@arr){\n if($_ == 0){\n $yes=0;\n last;\n }\n}\nif($yes == 1){\n print \"I become the guy.\\n\";\n}\nelse{\n print \"Oh, my keyboard!\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nmy $n = ;\n\nmy @first = split(' ', );\nmy @second = split(' ', );\n\nmy %set;\n\nforeach my $level ((@first, @second))\n{\n unless ($level != 0)\n {\n $set{$level} = 1;\n }\n}\nmy $size = keys %set;\nif ($size == $n) {\n print \"I become the guy.\";\n}\nelse {\n print \"Oh, my keyboard!\";\n}"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nmy $n = ;\n\nmy @first = split(' ', );\nmy @second = split(' ', );\n\nmy %set;\n\nforeach my $level ((@first, @second))\n{\n $set{$level} = 1;\n}\nmy $size = keys %set;\nif ($size == $n) {\n print \"I become the guy.\";\n}\nelse {\n print \"Oh, my keyboard!\";\n}"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nmy $n = ;\n\nmy @first = split(' ', );\nmy @second = split(' ', );\n\nmy %set;\n\nforeach my $level ((@first, @second))\n{\n unless ($level == 0)\n {\n $set{$level} = 1;\n }\n}\nmy $size = keys %set;\nif ($size == $n) {\n print \"I become the guy.\";\n}\nelse {\n print \"Oh, my keyboard!\";\n}"}], "src_uid": "044ade01d2de1486735742369227ae1d"} {"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\u2009-\u20091 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\u2009\u2264\u2009n\u2009\u2264\u2009104)\u00a0\u2014 the number of Balls living in the forest. The second line contains a sequence p1,\u2009p2,\u2009...,\u2009pn of length n, where (1\u2009\u2264\u2009pi\u2009\u2264\u2009n) 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\u2009\u2264\u2009n\u2009\u2264\u2009104)\u00a0\u2014 the number of Balls and the integer m (0\u2009\u2264\u2009m\u2009<\u2009n)\u00a0\u2014 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": "<>;\n@_ = split ' ', <>;\n\nfor (0 .. @_ - 1){\n\t++ ($_[$_] == $_ + 1 ? $j : $h{ $_[$_] } )\n\t}\n\nprint $j + (keys %h) / 2"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n#while(<>){\n\t\n\t<>;\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\tmy $j = 0;\n\t\n\tfor my $i (0 .. @_ - 1){\n\t\t$_[$i] == $i + 1 ? $j ++ : ($h{ $_[$i] } = 1);\n\t\t}\n\t\n\tprint $j + (keys %h) / 2;\n\t\n#\t}"}, {"source_code": "<>;\n\nmap { \n ++ ( ++ $i == $_ ? $j : $h{ $_ } ) \n } <> =~ /\\d+/g;\n\t\nprint $j + (keys %h) / 2"}], "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\u2009\u2264\u2009n\u2009\u2264\u20092000) \u2014 the number of tasks. The second line contains n integers h1,\u2009h2,\u2009...,\u2009hn (1\u2009\u2264\u2009hi\u2009\u2264\u20092000), 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 \u2014 [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": "while(<>){\n$_=<>;\nchomp;\n$i=0;\ns/\\d+/$&.'_'.(++$i)/eg;\n@_=split/ /;\n@_=sort {$a<=>$b} @_;\n$_=join' ',@_;\n@a=();\npush @a, \"\\n$_\";\nif (s/\\b(\\d+)_(\\d+) \\1_(\\d+) \\1_(\\d+)\\b/-/){\n($a,$b,$c,$d)=($1,$2,$3,$4);\n$A=$_;\n$A=~s/-/${a}_${c} ${a}_${b} ${a}_${d}/;\npush @a, \"\\n$A\";\n$A=$_;\n$A=~s/-/${a}_${b} ${a}_${d} ${a}_${c}/;\npush @a, \"\\n$A\";\n}\nelsif (s/\\b(\\d+)_(\\d+) \\1_(\\d+) (.*)\\b(\\d+)_(\\d+) \\5_(\\d+)\\b/-/){\n($a,$b,$c,$d,$e,$f,$g)=($1,$2,$3,$4,$5,$6,$7);\n$A=$_;\n$A=~s/-/${a}_${c} ${a}_${b} ${d}${e}_${f} ${e}_${g}/;\npush @a, \"\\n$A\";\n$A=$_;\n$A=~s/-/${a}_${c} ${a}_${b} ${d}${e}_${g} ${e}_${f}/;\npush @a, \"\\n$A\";\n}\n\ns/\\b\\d+_//g for @a;\n\nif (@a<2){print \"NO\\n\"}\nelse {print \"YES@a\"}\n\n\n}"}], "negative_code": [], "src_uid": "fdfc1e690eeee6f50e2a024bf3f841e8"} {"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,\u2009a2,\u2009...,\u2009an. 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of integers in the Filya's array. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009109)\u00a0\u2014 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\u2009=\u20091, 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": "use strict;\nuse warnings;\n\nuse Data::Dumper;\n$,=\" \";\nmy $N=<>;\nchomp($N);\n\nmy @t=split / /,<>;\nchomp(@t);\nmy %d=map {$_ => 1} @t;\nmy $count=scalar(keys %d);\nif($count>3)\n{\n print \"NO\\n\";\n exit(0);\n}\nif($count<3)\n{\n print \"YES\\n\";\n exit(0);\n}\n@t=sort {$a <=> $b} keys %d;\nif($t[2]-$t[1] == $t[1]-$t[0])\n{\n print \"YES\\n\";\n exit(0);\n}\nprint \"NO\\n\";\n"}], "negative_code": [], "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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20091000). 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\u2009\u2264\u2009|name|\u2009\u2264\u200910, 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\u2009\u2264\u2009|command|\u2009\u2264\u200910, 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": "( $n, $m ) = split ' ', <>;\n\n%h = map { reverse split ' ', <> } 1 .. $n;\n\nprint <> =~ s/(\\S+);\\K/ #$h{ $1 }/r for 1 .. $m"}, {"source_code": "@_ = <>;\n\n%h = map { reverse split } grep !/;/, @_;\n\nprint s/(\\S+);\\K/ #$h{ $1 }/r for grep /;/, @_"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\tmy %h;\n\t\n\tmap { my( $name, $ip ) = ( split ' ', <> ); $h{ $ip }{ name } = $name } 1 .. $n;\n\t\n\tfor( 1 .. $m ){\n\t\tmy( $comm, $ip ) = split ' ', <>;\n\t\tchop $ip;\n\t\t\n\t\tprint \"$comm $ip; #$h{ $ip }{ name }\"\n\t\t}\n\t}"}], "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$$$ \u2014 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)$$$ \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $found = 0;\n\t\n\twhile( not $found ){\n\t\tmy $sum = eval join '+', split //;\n\t\t\n\t\tfor my $i ( 2 .. $sum ){\n\t\t\tnext if not $sum % $i == 0;\n\t\t\t\n\t\t\tif( $_ % $i == 0 ){\n\t\t\t\t$found = $_;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t$_ ++;\n\t\t}\n\t\n\tprint $found;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $found = 0;\n\t\n\twhile( not $found ){\n\t\tmy $sum = eval join '+', split //;\n\t\t\n\t\tfor my $i ( 2 .. 1 + sqrt $sum, $sum ){\n\t\t\tnext if not $sum % $i == 0;\n\t\t\t\n\t\t\tif( $_ % $i == 0 ){\n\t\t\t\t$found = $_;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t$_ ++;\n\t\t}\n\t\n\tprint $found;\n\t}"}], "src_uid": "204e75827b7016eb1f1fbe1d6b60b03d"} {"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$$$) \u2014 the number of queries. Each of the next $$$T$$$ lines contains one integer $$$\\text{ang}$$$ ($$$1 \\le \\text{ang} < 180$$$) \u2014 the angle measured in degrees. ", "output_spec": "For each query print single integer $$$n$$$ ($$$3 \\le n \\le 998244353$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nmy %h = ( 179 => 360 );\n\nfor my $n ( 3 .. 180 ){\n my $r = 180 % $n;\n next if $r =~ /\\..{4}/;\n for my $i ( 1 .. $n - 2 ){\n next if ( 180 / $n * $i ) =~ /\\./;\n $h{ 180 / $n * $i } //= $n;\n }\n }\n\n$debug and print \" $_ => $h{ $_ }\" for sort { $a <=> $b } keys %h; \n\n<>;\n\nwhile(<>){\n $debug and print '-' x 15;\n \n chomp;\n\t$debug and print;\n \n print $h{ $_ };\n }"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nmy %h = ( 179 => 360 );\n\nfor my $n ( 3 .. 180 ){\n next if 180 % $n;\n for my $i ( 1 .. $n - 2 ){\n $h{ 180 / $n * $i } //= $n;\n }\n }\n\n$debug and print \" $_ => $h{ $_ }\" for sort { $a <=> $b } keys %h; \n\n<>;\n\nwhile(<>){\n $debug and print '-' x 15;\n \n chomp;\n\t$debug and print;\n \n print $h{ $_ };\n }"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nmy %h;\n\nfor my $n ( 3 .. 180 ){\n next if 180 % $n;\n for my $i ( 1 .. $n - 2 ){\n $h{ 180 / $n * $i } //= $n;\n }\n }\n\n$debug and print \" $_ => $h{ $_ }\" for sort { $a <=> $b } keys %h; \n\n<>;\n\nwhile(<>){\n $debug and print '-' x 15;\n \n chomp;\n\t$debug and print;\n \n print $h{ $_ };\n }"}], "src_uid": "d11b56fe172110d5dfafddf880e48f18"} {"nl": {"description": "Mike has n strings s1,\u2009s2,\u2009...,\u2009sn 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\u2009\u2264\u2009n\u2009\u2264\u200950) \u2014 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 \u2009-\u20091 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": "#!/usr/bin/perl\n\n$cnt = <>;\n$t;\n$target;\n$ans = 1 << 30;\n$sum = 0;\n@step;\nfor ($i = 0; $i < $cnt; $i++){\n $t = <>;\n chomp($t);\n @s[$i] = $t;\n}\nfor ($i = 0; $i < $cnt; $i++){\n $sum = 0;\n for ($j = 0; $j < $cnt; $j++){\n $target = @s[$j] . @s[$j];\n $t = index($target, @s[$i]);\n if ($t == -1){$ans = -1;last;}\n $sum += $t;\n }\n if ($t == -1){last;}\n $ans = $ans > $sum ? $sum : $ans;\n}\nprint \"$ans\";\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\n$cnt = <>;\n$t;\n$target;\n$ans = 1 << 30;\n$sum = 0;\n@step;\nfor ($i = 0; $i < $cnt; $i++){\n $t = <>;\n chomp($t);\n if ($i == 0) {\n $target = $t . $t;\n $len = length($t);\n }\n @s[$i] = $t;\n @step[$i] = ($len - index($target, $t)) % $len;\n if (index($target, $t) == -1){$ans = -1;}\n}\nif ($ans == -1) {print \"-1\";}\nelse {\n for ($i = 0; $i < $len; $i++){\n $sum = 0;\n for ($j = 0; $j < $cnt; $j++){\n $sum += (@step[$j] + $i) % $len;\n }\n $ans = $ans > $sum ? $sum : $ans;\n }\n print \"$ans\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\n$cnt = <>;\n$t;\n$target;\n$ans = 1 << 30;\n$sum = 0;\n@step;\nfor ($i = 0; $i < $cnt; $i++){\n $t = <>;\n chomp($t);\n if ($i == 0) {\n $target = $t . $t;\n $len = length($t);\n }\n @s[$i] = $t;\n @step[$i] = ($len - index($target, $t)) % $len;\n if (index($target, $t) == -1){$ans = -1;}\n}\nif ($ans == -1) {print \"-1\";}\nelse {\n for ($i = 0; $i < $len; $i++){\n $sum = 0;\n for ($j = 0; $j < $cnt; $j++){\n $sum += (@step[$j] + $i) % $len;\n }\n $ans = $ans > $sum ? $sum : $ans;\n }\n}\nprint \"$ans\";\n"}], "src_uid": "a3a7515219ebb0154218ee3520e20d75"} {"nl": {"description": "You are given an array a1,\u2009a2,\u2009...,\u2009an 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\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u2009\u2009105) \u2014 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,\u2009\u2009a2,\u2009\u2009...,\u2009\u2009an (\u2009-\u2009109\u2009\u2009\u2264\u2009\u2009ai\u2009\u2264\u2009\u2009109).", "output_spec": "Print single integer \u2014 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,\u2009\u2009r] (l\u2009\u2264\u2009r) of array a is the sequence al,\u2009\u2009al\u2009+\u20091,\u2009\u2009...,\u2009\u2009ar.Splitting of array a of n elements into k subsegments [l1,\u2009r1], [l2,\u2009r2], ..., [lk,\u2009rk] (l1\u2009=\u20091, rk\u2009=\u2009n, li\u2009=\u2009ri\u2009-\u20091\u2009+\u20091 for all i\u2009>\u20091) is k sequences (al1,\u2009...,\u2009ar1),\u2009...,\u2009(alk,\u2009...,\u2009ark).In the first example you should split the array into subsegments [1,\u20094] and [5,\u20095] that results in sequences (1,\u20092,\u20093,\u20094) and (5). The minimums are min(1,\u20092,\u20093,\u20094)\u2009=\u20091 and min(5)\u2009=\u20095. The resulting maximum is max(1,\u20095)\u2009=\u20095. 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,\u20095], that results in one sequence (\u2009-\u20094,\u2009\u2009-\u20095,\u2009\u2009-\u20093,\u2009\u2009-\u20092,\u2009\u2009-\u20091). The only minimum is min(\u2009-\u20094,\u2009\u2009-\u20095,\u2009\u2009-\u20093,\u2009\u2009-\u20092,\u2009\u2009-\u20091)\u2009=\u2009\u2009-\u20095. The resulting maximum is \u2009-\u20095."}, "positive_code": [{"source_code": "<> =~ / /;\n\n@_ = split ' ', <>;\n\nprint +( do {\n\tif( $' == 1 ){\n\t\tsort { $a <=> $b } @_\n\t\t}\n\telsif( $' > 2 ){\n\t\tsort { $b <=> $a } @_\n\t\t}\n\telse{\n\t\tsort { $b <=> $a } @_[0,-1]\n\t\t}\n\t}\n\t)[ 0 ]"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t@_ = split ' ', <>;\n\t\n\tprint do {\n\t\tif( $k == 1 ){\n\t\t\t( sort { $a <=> $b } @_ )[ 0 ]\n\t\t\t}\n\t\telsif( $k > 2 ){\n\t\t\t( sort { $b <=> $a } @_ )[ 0 ]\n\t\t\t}\n\t\telse{\n\t\t\t( sort { $b <=> $a } @_[0,-1] )[ 0 ]\n\t\t\t}\n\t\t};\n\t}"}, {"source_code": "<> =~ / /;\n\n@_ = split ' ', <>;\n\nprint +(\n\tsort { $a <=> $b } @_[ -1 .. ( $' == 2 ? 0 : @_ - 2 ) ]\n\t)[ 1 <=> $' ]"}], "negative_code": [{"source_code": "<> =~ / /;\n\t\nprint +(\n\tsort { $a <=> $b } ( @_ = split ' ', <> )[ -1 .. ( $' == 2 ? 0 : @_ - 2 ) ]\n\t)[ 1 <=> $' ]"}, {"source_code": "<> =~ / /;\n\t\nprint +(\n\tsort { $a <=> $b } ( split ' ', <> )[ -1 .. ( $' == 2 ? 0 : @_ - 2 ) ]\n\t)[ 1 <=> $' ]"}, {"source_code": "<> =~ / /;\n\t\nprint +(\n\tsort { $a <=> $b } ( split ' ', <> )[ -1 .. ( $' == 2 ? 0 : 2e5 ) ]\n\t)[ 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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy $s = $_;\n\tmy $t = <>;\n\tmy $p = <>;\n\tchomp( $s, $t, $p );\n\t$debug and print \"[$s|$t|$p]\";\n\t\n\tmy $f = 0;\n\tmy $left = '';\n\t\n\twhile( $s =~ /./g ){\n\t\tmy $del = $&;\n\t\t\n\t\t$t =~ s/^(.*?)$del// ? do { $left .= $1 } : do { $f = 1 };\n\t\t}\n\t\n\t$left .= $t;\n\t\n\t$debug and print $left;\n\t\n\twhile( $p =~ /./g ){\n\t\tmy $del = $&;\n\t\t\n\t\t$left =~ s/$del//;\n\t\t}\n\t\n\t$debug and print $left;\n\t\n\tlength $left and $f = 1;\n\t\n\tprint $f ? \"NO\" : \"YES\";\n\t}"}], "negative_code": [], "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\u2009\u00d7\u2009n 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\u2009\u2264\u2009n\u2009\u2264\u2009100)\u00a0\u2014 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,\u20092) and (1,\u20093) (3,\u20091) and (3,\u20093) Pieces that share the same column are: (2,\u20091) and (3,\u20091) (1,\u20093) and (3,\u20093) "}, "positive_code": [{"source_code": "$n=<>;\nmy @x;\nfor($i=0;$i<$n;$i++) {\n $x[$i]=0;\n}\n$ans=0;\nfor($i=0;$i<$n;$i++) {\n $a=<>;\n $d=0;\n $e=0;\n foreach my $c (split '', $a) {\n if ($c eq 'C') {\n $e++;\n $x[$d]++;\n }\n $d++;\n }\n $ans+=$e*($e-1)/2;\n}\nfor($i=0;$i<$n;$i++) {\n $ans+=$x[$i]*($x[$i]-1)/2;\n}\nprint \"$ans\\n\";\n"}, {"source_code": "use feature ':all';\n\nwhile(<>){\n\t$sum = 0;\n\t\n\tfor (1 .. $_){\n\t\t$_ = <>, chomp;\n\t\t$n = (() = /C/g);\n\t\t$sum += $n *~- $n / 2;\n\t\t$i = 0; map $A[ $i++ ] .= $_, split //;\n\t\t}\n\t\n\tfor (@A){\n\t\t$n = (() = /C/g);\n\t\t$sum += $n *~- $n / 2;\n\t\t}\n\t\n\tsay $sum\n\t}"}], "negative_code": [], "src_uid": "7749f37aa9bf88a00013557e14342952"} {"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$$$) \u2014 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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $first = $_[ 0 ];\n\t\n\tmy @eq;\n\tmy @neq;\n\t\n\tfor my $i ( 1 .. @_ - 1 ){\n\t\tif( $_[ $i ] == $first ){\n\t\t\tpush @eq, $i + 1;\n\t\t\t}\n\t\telse{\n\t\t\tpush @neq, $i + 1;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"[eq:@eq][neq:@neq]\";\n\t\n\tif( @neq ){\n\t\tprint \"YES\";\n\t\t}\n\telse{\n\t\tprint \"NO\";\n\t\tnext;\n\t\t}\n\t\n\tfor( @neq ){\n\t\tprint \"1 $_\";\n\t\t}\n\t\n\tfor( @eq ){\n\t\tprint \"$neq[ 0 ] $_\"\n\t\t}\n\t\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $first = $_[ 0 ];\n\t\n\tmy @eq;\n\tmy @neq;\n\t\n\tfor my $i ( 1 .. @_ - 1 ){\n\t\tif( $_[ $i ] == $first ){\n\t\t\tpush @eq, $i + 1;\n\t\t\t}\n\t\telse{\n\t\t\tpush @neq, $i + 1;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"[eq:@eq][neq:@neq]\";\n\t\n\tif( @neq ){\n\t\tprint \"YES\";\n\t\t}\n\telse{\n\t\tprint \"NO\";\n\t\tnext;\n\t\t}\n\t\n\tfor( @neq ){\n\t\tprint \"$first $_\";\n\t\t}\n\t\n\tfor( @eq ){\n\t\tprint \"$neq[ 0 ] $_\"\n\t\t}\n\t\n\t}"}], "src_uid": "d8136eb72931851f501c5ce9042ce4eb"} {"nl": {"description": "You have an array $$$a$$$ of size $$$n$$$ consisting only of zeroes and ones and an integer $$$k$$$. In one operation you can do one of the following: Select $$$2$$$ consecutive elements of $$$a$$$ and replace them with their minimum (that is, let $$$a := [a_{1}, a_{2}, \\ldots, a_{i-1}, \\min(a_{i}, a_{i+1}), a_{i+2}, \\ldots, a_{n}]$$$ for some $$$1 \\le i \\le n-1$$$). This operation decreases the size of $$$a$$$ by $$$1$$$. Select $$$k$$$ consecutive elements of $$$a$$$ and replace them with their maximum (that is, let $$$a := [a_{1}, a_{2}, \\ldots, a_{i-1}, \\max(a_{i}, a_{i+1}, \\ldots, a_{i+k-1}), a_{i+k}, \\ldots, a_{n}]$$$ for some $$$1 \\le i \\le n-k+1$$$). This operation decreases the size of $$$a$$$ by $$$k-1$$$. Determine if it's possible to turn $$$a$$$ into $$$[1]$$$ after several (possibly zero) operations.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 1000$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \\le k \\le n \\le 50$$$), the size of array $$$a$$$ and the length of segments that you can perform second type operation on. The second line contains $$$n$$$ integers $$$a_{1}, a_{2}, \\ldots, a_{n}$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), elements of array $$$a$$$.", "output_spec": "For each test case, if it is possible to turn $$$a$$$ into $$$[1]$$$, print \"YES\", otherwise print \"NO\".", "sample_inputs": ["7\n\n3 2\n\n0 1 0\n\n5 3\n\n1 0 1 1 0\n\n2 2\n\n1 1\n\n4 4\n\n0 0 0 0\n\n6 3\n\n0 0 1 0 0 1\n\n7 5\n\n1 1 1 1 1 1 1\n\n5 3\n\n0 0 1 0 0"], "sample_outputs": ["YES\nYES\nYES\nNO\nYES\nYES\nYES"], "notes": "NoteIn the first test case, you can perform the second type operation on second and third elements so $$$a$$$ becomes $$$[0, 1]$$$, then you can perform the second type operation on first and second elements, so $$$a$$$ turns to $$$[1]$$$.In the fourth test case, it's obvious to see that you can't make any $$$1$$$, no matter what you do.In the fifth test case, you can first perform a type 2 operation on the first three elements so that $$$a$$$ becomes $$$[1, 0, 0, 1]$$$, then perform a type 2 operation on the elements in positions two through four, so that $$$a$$$ becomes $$$[1, 1]$$$, and finally perform the first type operation on the remaining elements, so that $$$a$$$ becomes $$$[1]$$$."}, "positive_code": [{"source_code": "<>; print <> =~ /1/ ? \"YES\\n\" : \"NO\\n\" while <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t@_ = split ' ', <>;\n\t\n\tmy $A = grep m/1/, @_;\n\t\n\tprint $A ? 'YES' : 'NO';\n\t}"}, {"source_code": "<>; print <> =~/1/ ? \"YES\\n\" : \"NO\\n\" while<>"}, {"source_code": "<>; print <> =~/1/ ? \"YES\\n\" : \"NO\\n\" while <>"}, {"source_code": "<>; print <> =~ /1/ ? \"YES\\n\" : \"NO\\n\" while <>"}, {"source_code": "#!/usr/bin/perl\nuse Data::Dumper;\nuse integer; ### important!\nmy $mod = 10 ** 9 + 7;\n# my $mod = 998244353;\n\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\n\nwhile( $testcase -- > 0 ){\n \n my ($n,$k) = map { $_ - 0 } split(/\\s+/,);\n my @A = map { $_ - 0 } split(/\\s+/,);\n my $c1 = 0; my $c0 = 0;\n for(my $i=0;$i<$n;$i++){\n $c1++ if( $A[$i] == 1 );\n $c0++ if( $A[$i] == 0 );\n }\n if( $c0 == 0 ){\n print \"YES\\n\"; next;\n }\n if( $c1 == 0 ){\n print \"NO\\n\"; next;\n }\n if( $k > $n ){\n print \"NO\\n\"; next;\n }\n print \"YES\\n\";\n\n}\n\nexit(0);\n\nsub max {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\n return $r;\n}\nsub min {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\n return $r;\n}\nsub sum {\n my $r = 0;\n foreach my $e (@_){ $r += $e; }\n return $r;\n}\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\n my $r = shift; my $ar = shift;\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\n for(my $i=0;$i<=$#ar;$i++){ \n $rr += $ar->[$i];\n $r->[1+$i] = $rr;\n }\n}\n"}], "negative_code": [], "src_uid": "95d83cfdb2131f2f23ba5ef005c18b38"} {"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 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 a number of rod-blanks. The second line contains n integers li (1\u2009\u2264\u2009li\u2009\u2264\u2009109) \u2014 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 \u2014 the minimum length of the rod, so that after adding it it can be possible to construct convex polygon with (n\u2009+\u20091) 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\u2009+\u20091\u2009=\u20092,\u20092,\u20091} can be formed from a set of lengths {1,\u20091,\u20091,\u20092}. In the second example you can make a triangle with lengths {20,\u200911,\u20094\u2009+\u20093\u2009+\u20092\u2009+\u20091\u2009=\u200910}. "}, "positive_code": [{"source_code": "<>;for (split(' ',<>)) {\n\t$l+=$_;\n\t$m=$_ if $_>$m;\n};\nprint 2*$m-$l+1;\n"}, {"source_code": "sub solve {\n\tmy $n = shift;\n\tmy @a = split(\" \", shift);\n\tmy $s = 0;\n\tfor my $x (@a) {\n\t\t$s += $x;\n\t}\n\tmy $res = 0;\n\tfor my $x (@a) {\n\t\tmy $tmp = 2 * $x - $s;\n\t\tif ($res < $tmp) {\n\t\t\t$res = $tmp;\n\t\t}\n\t}\n\treturn $res + 1;\n};\n\nmy $n = <>;\nmy $input = <>;\nprint solve($n, $input);"}], "negative_code": [], "src_uid": "f00d94eb37c98a449615f0411e5a3572"} {"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.\u2009e. $$$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$$$) \u2014 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$$$) \u2014 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$$$) \u2014 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}$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $a1, $a2 ) = split;\n\tmy( $b1, $b2 ) = split ' ', <>;\n\tmy( $k, $w, $A ) = split ' ', <>;\n\t\n\tmy $win = 0;\n\t\n\tOUTER:\n\tfor my $i ( 0 .. $k ){\n\t\tmy( $A1, $A2 ) = ( $a1 + $i * $A, $a2 + ( $k - $i ) * $w );\n\t\t\n\t\tif( int( $b1 / $A2 ) + !!( $b1 % $A2 )\n\t\t\t<= \n\t\t\tint( $A1 / $b2 ) + !!( $A1 % $b2 )\n\t\t\t){\n\t\t\t$win = 1;\n\t\t\tlast OUTER;\n\t\t\t}\n\t\t}\n\t\n\tprint $win ? 'YES' : 'NO';\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $a1, $a2 ) = split;\n\tmy( $b1, $b2 ) = split ' ', <>;\n\tmy( $k, $w, $A ) = split ' ', <>;\n\t\n\tmy $win = 0;\n\t\n\tOUTER:\n\tfor my $i ( 0 .. $k ){\n\t\tmy( $A1, $A2 ) = ( $a1 + $i * $A, $a2 + ( $k - $i ) * $w );\n\t\t\n\t\tif( int( $b1 / $A2 ) <= int( $A1 / $b2 ) ){\n\t\t\t$win = 1;\n\t\t\tlast OUTER;\n\t\t\t}\n\t\t}\n\t\n\tprint $win ? 'YES' : 'NO';\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $a1, $a2 ) = split;\n\tmy( $b1, $b2 ) = split ' ', <>;\n\tmy( $k, $w, $A ) = split ' ', <>;\n\t\n\tmy $win = 0;\n\t\n\tOUTER:\n\tfor my $i ( 0 .. $k ){\n\t\tmy( $A1, $A2 ) = ( $a1 + $i * $A, $a2 + ( $k - $i ) * $w );\n\t\t\n\t\tif( int( $b1 / $A2 ) >= int( $A1 / $b2 ) ){\n\t\t\t$win = 1;\n\t\t\tlast OUTER;\n\t\t\t}\n\t\t}\n\t\n\tprint $win ? 'YES' : 'NO';\n\t}"}], "src_uid": "5b1f33228a58d9e14bc9479767532c25"} {"nl": {"description": "Inna likes sweets and a game called the \"Candy Matrix\". Today, she came up with the new game \"Candy Matrix 2: Reload\".The field for the new game is a rectangle table of size n\u2009\u00d7\u2009m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout \"Let's go!\". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs: some dwarf in one of the chosen lines is located in the rightmost cell of his row; some dwarf in the chosen lines is located in the cell with the candy. The point of the game is to transport all the dwarves to the candy cells.Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.", "input_spec": "The first line of the input contains two integers n and m (1\u2009\u2264\u2009n\u2009\u2264\u20091000;\u00a02\u2009\u2264\u2009m\u2009\u2264\u20091000). Next n lines each contain m characters \u2014 the game field for the \"Candy Martix 2: Reload\". Character \"*\" represents an empty cell of the field, character \"G\" represents a dwarf and character \"S\" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character \"G\" and one character \"S\".", "output_spec": "In a single line print a single integer \u2014 either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.", "sample_inputs": ["3 4\n*G*S\nG**S\n*G*S", "1 3\nS*G"], "sample_outputs": ["2", "-1"], "notes": null}, "positive_code": [{"source_code": "<>;\n/G\\**S/ ? $_[length $&]=1 : ++$f for <>;\nprint $f? -1 : ($D=()=\"@_\"=~/1/g)"}, {"source_code": "<>;\n/G\\**S/ ? $_[length $&]=1 : ++$f for <>;\nprint $f? -1 : ($D=()=\"@_\"=~/1/g)"}, {"source_code": "<>;\n/G\\**S/ ? $_[length $&]=1 : ++$f for <>;\nprint $f? -1 : ($D=()=\"@_\"=~/1/g)\n"}, {"source_code": "<>;\n/G\\**S/ ? $_[length $&]=1 : ++$f for <>;\nprint $f? -1 : ($D=()=\"@_\"=~/1/g)\n"}, {"source_code": "<>;\n/G\\**S/ ? $_[length $&]=1 : ++$f for <>;\nprint $f? -1 : ($D=()=\"@_\"=~/1/g)\n"}, {"source_code": "<>;\n/G\\**S/ ? $_[length $&]=1 : ++$f for <>;\nprint $f? -1 : ($D=()=\"@_\"=~/1/g)\n"}, {"source_code": "<>;\n/G\\**S/ ? $_[length $&]=1 : ++$f for <>;\nprint $f? -1 : ($D=()=\"@_\"=~/1/g)\n"}, {"source_code": "<>;\n/G\\**S/ ? $_[length $&]=1 : ++$f for <>;\nprint $f? -1 : ($D=()=\"@_\"=~/1/g)"}, {"source_code": "<>;\n/G\\**S/ ? ++$d[length $&] : ++$f for <>;\n$_ && $j++ for @d;\nprint $f? -1 : $j"}, {"source_code": "sub uniq{\n\tmy @m=sort @_; my @u;\n\tpush @u, my $i=shift @m;\n\t$i eq $_ or push @u, $i=$_ for @m;\nreturn @u\n}\nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n\nwhile(<>){\n\t($n,$m)=split/ /;\n\t@distance=(); # empty\n\t@end=();\n\t$f=1;\n\tfor $i(1..$n){\n\t\t\n\t\t$_=<>;\n\t\t/G\\**S/ and (($distance[length($&)]++), next);\n\t\t$f=0;\n\t\t}\n\t$j=0;\n\t$_ and $j++ for @distance;\n\tprintf \"%d\\n\", $f? ( $j ) : -1;\n\t\n\t\n\t}\n\t\n"}, {"source_code": "<>;\n/G\\**S/ ? $_[length $&]=1 : ++$f for <>;\nprint $f? -1 : ($D=()=\"@_\"=~/1/g)\n"}, {"source_code": "<>;\n/G\\**S/ ? $_[length $&]=1 : ++$f for <>;\nprint $f? -1 : ($D=()=\"@_\"=~/1/g)\n"}, {"source_code": "<>;\n/G\\**S/ ? $_[length $&]=1 : ++$f for <>;\nprint $f? -1 : ($D=()=\"@_\"=~/1/g)\n"}], "negative_code": [{"source_code": "sub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n\nwhile(<>){\n\t($n,$m)=split/ /;\n\t@distance=(); # empty\n\t@end=();\n\t$f=1;\n\tfor $i(1..$n){\n\t\t\n\t\t$_=<>;\n\t\t/G\\**S\\*/ and ((push @distance, length($&)-2), next);\n\t\t/G\\**S$/ and ((push @end, length($&)-1), next);\n\t\t$f=0;\n\t\t}\n\tprintf \"%d\\n\", $f? (0+@end? min(@end) : max(@distance) ) : -1;\n\t\n\t\n\t}\n\t\n"}, {"source_code": "sub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n\nwhile(<>){\n\t($n,$m)=split/ /;\n\t@distance=(); # empty\n\t@end=();\n\t$f=1;\n\tfor $i(1..$n){\n\t\t\n\t\tchomp($_=<>);\n\t\t/G\\**S/ ? (push @distance, $d=length($&)-1) : ($f=0);\n\t\t/S$/ and push @end, $d;\n\t\t}\n\tprintf \"%d\\n\", $f? min(max(@distance),min(@end)): -1;\n\t\n\t\n\t}\n\t\n"}, {"source_code": "sub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n\nwhile(<>){\n\t($n,$m)=split/ /;\n\t@distance=(); # empty\n\t$f=1;\n\tfor $i(1..$n){\n\t\t\n\t\t$_=<>;\n\t\t/G\\**S/ ? (push @distance, length($&)-1) : ($f=0);\n\t\t\n\t\t}\n\tprintf \"%d\\n\", $f? max(@distance)-min(@distance)+1: -1;\n\t\n\t\n\t}\n\t\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\u2009\u2264\u2009i\u2009\u2264\u2009n) cowbell is an integer si. In fact, he keeps his cowbells sorted by size, so si\u2009-\u20091\u2009\u2264\u2009si for any i\u2009>\u20091. 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\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7k\u2009\u2264\u2009100\u2009000), denoting the number of cowbells and the number of boxes, respectively. The next line contains n space-separated integers s1,\u2009s2,\u2009...,\u2009sn (1\u2009\u2264\u2009s1\u2009\u2264\u2009s2\u2009\u2264\u2009...\u2009\u2264\u2009sn\u2009\u2264\u20091\u2009000\u2009000), 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,\u20093}, {5} and {9}.In the third sample, the optimal solution is {3,\u20095} and {7}."}, "positive_code": [{"source_code": "($n, $k) = split ' ', <>;\n@_ = split ' ', <>;\n\nfor (1 .. $n - $k){\n\t$_ != ($n - $k) * 2 - $_ + 1 and $A = (sort {$b <=> $a} $A, $_[$_ - 1] + $_[2 * ($n - $k) - $_])[ 0 ]\n\t}\n\nprint +(sort {$b <=> $a} $A, $_[$#_])[ 0 ]"}], "negative_code": [{"source_code": "($n, $k) = split ' ', <>;\n@_ = split ' ', <>;\n\nfor (1 .. $n - $k){\n\t$_ != ($n - $k) * 2 - $_ + 1 and $A = (sort {$b <=> $a} $A, $_[$#_], $_[$_ - 1] + $_[2 * ($n - $k) - $_])[ 0 ]\n\t}\n\nprint $A"}, {"source_code": "($n, $k) = split ' ', <>;\n@_ = split ' ', <>;\n\nfor (1 .. $n - $k){\n\t$_ != ($n - $k) * 2 - $_ + 1 and $A = (sort {$b <=> $a} $A, $_[$_ - 1] + $_[2 * ($n - $k) - $_])[ 0 ]\n\t}\n\nprint + sort {$b <=> $a} $A, $_[$#_]"}, {"source_code": "($n, $k) = split ' ', <>;\n@_ = split ' ', <>;\n\n@A = ($_[$#_]);\n\nif ($k - $n < 0){\n\tfor $i (0 .. ($n - $k) / 2 + 1){\n\t\tpush @A, $_[$i] + $_[ -1 + 2 * ($n - $k) - $i]\n\t\t}\n\t}\n\nprint +(sort {$b <=> $a} @A)[ 0 ]"}, {"source_code": "($n, $k) = split ' ', <>;\n@_ = split ' ', <>;\n\nprint +(sort {$b <=> $a} $_[$#_], $_[0] + $_[-1 + ($k - $n < 0 ? $k - $n : 2e5)])[ 0 ]"}], "src_uid": "7324428d9e6d808f55ad4f3217046455"} {"nl": {"description": "In this problem you will write a simple generator of Brainfuck (https://en.wikipedia.org/wiki/Brainfuck) calculators.You are given an arithmetic expression consisting of integers from 0 to 255 and addition/subtraction signs between them. Output a Brainfuck program which, when executed, will print the result of evaluating this expression.We use a fairly standard Brainfuck interpreter for checking the programs: 30000 memory cells. memory cells store integers from 0 to 255 with unsigned 8-bit wraparound. console input (, command) is not supported, but it's not needed for this problem.", "input_spec": "The only line of input data contains the arithmetic expression. The expression will contain between 2 and 10 operands, separated with arithmetic signs plus and/or minus. Each operand will be an integer between 0 and 255, inclusive. The calculations result is guaranteed to be an integer between 0 and 255, inclusive (results of intermediary calculations might be outside of these boundaries).", "output_spec": "Output a Brainfuck program which, when executed, will print the result of evaluating this expression. The program must be at most 5000000 characters long (including the non-command characters), and its execution must be complete in at most 50000000 steps.", "sample_inputs": ["2+3", "9-7"], "sample_outputs": ["++>\n+++>\n<[<+>-]<\n++++++++++++++++++++++++++++++++++++++++++++++++.", "+++++++++>\n+++++++>\n<[<->-]<\n++++++++++++++++++++++++++++++++++++++++++++++++."], "notes": "NoteYou can download the source code of the Brainfuck interpreter by the link http://assets.codeforces.com/rounds/784/bf.cpp. We use this code to interpret outputs."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nwhile (<>) {\n chomp;\n my $n = eval;\n my $res = '';\n my @ds = split //, $n;\n for my $d (@ds) {\n $res .= '+' x ($d + 48) . '.>';\n }\n print \"$res\\n\";\n}\n"}, {"source_code": "$_ = <>;\n$s = \"\".(eval $_);\n$s =~ s/0/++++++++++++++++++++++++++++++++++++++++++++++++.>/g;\n$s =~ s/1/+++++++++++++++++++++++++++++++++++++++++++++++++.>/g;\n$s =~ s/2/++++++++++++++++++++++++++++++++++++++++++++++++++.>/g;\n$s =~ s/3/+++++++++++++++++++++++++++++++++++++++++++++++++++.>/g;\n$s =~ s/4/++++++++++++++++++++++++++++++++++++++++++++++++++++.>/g;\n$s =~ s/5/+++++++++++++++++++++++++++++++++++++++++++++++++++++.>/g;\n$s =~ s/6/++++++++++++++++++++++++++++++++++++++++++++++++++++++.>/g;\n$s =~ s/7/+++++++++++++++++++++++++++++++++++++++++++++++++++++++.>/g;\n$s =~ s/8/++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>/g;\n$s =~ s/9/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>/g;\nprint \"$s\\n\";\n"}, {"source_code": "$_ = <>, chomp;\n@_ = split //, eval $_;\n\nprint join \"\\n\", map { \"+\" x (48 + $_) . \".\" . \"-\" x (48 + $_) } @_;\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nwhile (<>) {\n chomp;\n my $n = eval;\n my @res = ();\n if ($n >= 0) {\n @res = ('+' x $n, '.');\n } else {\n @res = ('-' x -$n, '.');\n }\n print @res, \"\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nwhile (<>) {\n chomp;\n my $n = eval;\n my @res = ('+' x $n, '.');\n print @res, \"\\n\";\n}\n"}, {"source_code": "$_ = <>;\n$s = \"\".(eval $_);\n$s =~ s/0/++++++++++++++++++++++++++++++++++++++++++++++++.>/;\n$s =~ s/1/+++++++++++++++++++++++++++++++++++++++++++++++++.>/;\n$s =~ s/2/++++++++++++++++++++++++++++++++++++++++++++++++++.>/;\n$s =~ s/3/+++++++++++++++++++++++++++++++++++++++++++++++++++.>/;\n$s =~ s/4/++++++++++++++++++++++++++++++++++++++++++++++++++++.>/;\n$s =~ s/5/+++++++++++++++++++++++++++++++++++++++++++++++++++++.>/;\n$s =~ s/6/++++++++++++++++++++++++++++++++++++++++++++++++++++++.>/;\n$s =~ s/7/+++++++++++++++++++++++++++++++++++++++++++++++++++++++.>/;\n$s =~ s/8/++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>/;\n$s =~ s/9/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>/;\nprint \"$s\\n\";\n"}, {"source_code": "$_ = <>, chomp;\n$_ = eval $_;\n\nprint join \"\\n\",\n\">\",\n\"+\" x $_ . \">\",\n\"<[<+>-]<\",\n\"++++++++++++++++++++++++++++++++++++++++++++++++.\"\n"}], "src_uid": "072797682246c465148bd09736b76718"} {"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\u2009\u2264\u2009n\u2009\u2264\u20091000). The second line contains n non-negative integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009<\u2009n), 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 \u2014 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": "@_ = split ' ', (<>, <>);\n\t\nwhile (@_){\n\t$i ++;\n\t@a = ();\n\t$_ <= $sum ? $sum ++ : push @a, $_ for @_;\n\t@_ = reverse @a\n\t}\n\t\nprint -- $i\n"}, {"source_code": "@_ = split ' ', (<>, <>);\n\t\nwhile (@_){\n\t$i ++;\n\t@a = ();\n\t$_ <= $sum ? $sum ++ : push @a, $_ for @_;\n\t@_ = reverse @a\n\t}\n\t\nprint -- $i\n"}, {"source_code": "@_ = split ' ', (<>, <>);\n\t\nwhile (@_){\n\t$i ++;\n\t@a = ();\n\t$_ <= $sum ? $sum ++ : push @a, $_ for @_;\n\t@_ = reverse @a\n\t}\n\t\nprint -- $i"}, {"source_code": "@_ = split ' ', (<>, <>);\n\t\nwhile (@_){\n\t$i ++;\n\t@a = ();\n\t$_ <= $sum ? $sum ++ : push @a, $_ for @_;\n\t@_ = reverse @a\n\t}\n\t\nprint -- $i\n"}, {"source_code": "@_ = split ' ', (<>, <>);\n\t\nwhile (@_){\n\t$i ++;\n\t@a = ();\n\t$_ <= $sum ? $sum ++ : push @a, $_ for @_;\n\t@_ = reverse @a\n\t}\n\t\nprint -- $i\n"}, {"source_code": "@_ = split ' ', (<>, <>);\n\t\nwhile (@_){\n\t$i ++;\n\t@a = ();\n\t$_ <= $sum ? $sum ++ : push @a, $_ for @_;\n\t@_ = reverse @a\n\t}\n\t\nprint -- $i\n"}, {"source_code": "@_ = split ' ', (<>, <>);\n\t\nwhile (@_){\n\t$i ++;\n\t@a = ();\n\t$_ <= $sum ? $sum ++ : push @a, $_ for @_;\n\t@_ = reverse @a\n\t}\n\t\nprint -- $i\n"}, {"source_code": "$n=<>;\n@info= split ' ',<>;\n$m=0;\n$rez=0;\nwhile ($m<$n)\n{\n\tfor ($i=0; $i<$n; $i++)\n\t{\n\t\tif ($info[$i]<=$m)\n\t\t{\n\t\t\t$m++;\n\t\t\t$info[$i]=$n+1;\n\t\t}\n\t}\n\tlast if $m==$n;\n\t$rez++;\n\tfor ($i=$n-1; $i>=0; $i--)\n\t{\n\t\tif ($info[$i]<=$m)\n\t\t{\n\t\t\t$m++;\n\t\t\t$info[$i]=$n+1;\n\t\t}\n\t}\n\tlast if $m==$n;\n\t$rez++;\n}\nprint $rez;"}, {"source_code": "@_ = split ' ', (<>, <>);\n\t\nwhile (@_){\n\t$i ++;\n\t@a = ();\n\t$_ <= $sum ? $sum ++ : push @a, $_ for @_;\n\t@_ = reverse @a\n\t}\n\t\nprint -- $i\n"}, {"source_code": "@_ = split ' ', (<>, <>);\n\t\nwhile (@_){\n\t$i ++;\n\t@a = ();\n\t$_ <= $sum ? $sum ++ : push @a, $_ for @_;\n\t@_ = reverse @a\n\t}\n\t\nprint -- $i"}, {"source_code": "@_ = split ' ', (<>, <>);\n\t\nwhile (@_){\n\t$i ++;\n\t@a = ();\n\t$_ <= $sum ? $sum ++ : push @a, $_ for @_;\n\t@_ = reverse @a\n\t}\n\t\nprint -- $i\n"}], "negative_code": [{"source_code": "$\\ = $/;\n\nsub solve{\n\t\n\t$sum = 0;\n\t$i = -1;\n\twhile (@_){\n\t\t$i ++;\n\t\t@a = ();\n\t\tfor (@_){\n\t\t\t$_ <= $sum ? $sum ++ : push @a, $_\n\t\t\t}\n\t\t@_ = reverse @a\n\t\t}\n\t$i\n\t\n\t}\n\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tprint ( (sort {$a <=> $b} solve(@_), solve(reverse @_) )[0] )\n\t}"}], "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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \\leq k \\leq n \\leq 40$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\tif( $n < $m * 2 - 1 ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\t@_ = ();\n\t\n\tfor my $i ( 0 .. $n - 1 ){\n\t\t$_ = '.' x $n;\n\t\t$i % 2 or $m and do {\n\t\t\t$m --;\n\t\t\tsubstr $_, $i, 1, 'R';\n\t\t\t};\n\t\tpush @_, $_;\n\t\t}\n\t\n\tprint join \"\\n\", @_;\n\t}"}], "negative_code": [], "src_uid": "d5549c0627c236d82541a67ccbe7977d"} {"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\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105,\u20092\u2009\u2264\u2009m\u2009\u2264\u200910) \u2014 the number of books in the bookstore and the number of genres. The second line contains a sequence a1,\u2009a2,\u2009...,\u2009an, where ai (1\u2009\u2264\u2009ai\u2009\u2264\u2009m) 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 \u2014 the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2\u00b7109.", "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": "$s=<>;\nchomp($s);\n($n,$m)=split ' ',$s;\n$s=<>;\nchomp($s);\n@a=split ' ',$s;\nfor ($i=0; $i<$n; $i++)\n{\n\t$u[$a[$i]-1]++;\n}\nfor ($i=0; $i<$m; $i++)\n{\n\tfor ($j=$i+1; $j<$m; $j++)\n\t{\n\t\t$rez+=$u[$i]*$u[$j];\n\t}\n}\nprint $rez;"}, {"source_code": "($n, $m) = split ' ', <>;\n@n = split ' ', <>;\nmap { $A[ $_ ]++ } @n;\nfor $i (1 .. $m){\nfor $j ($i + 1 .. $m){\n$sum += $A[ $i ] * $A[ $j ]\n}\n}\nprint $sum"}], "negative_code": [], "src_uid": "e2ff228091ca476926b8e905f1bc8dff"} {"nl": {"description": "A Christmas party in city S. had n children. All children came in mittens. The mittens can be of different colors, but each child had the left and the right mitten of the same color. Let's say that the colors of the mittens are numbered with integers from 1 to m, and the children are numbered from 1 to n. Then the i-th child has both mittens of color ci.The Party had Santa Claus ('Father Frost' in Russian), his granddaughter Snow Girl, the children danced around the richly decorated Christmas tree. In fact, everything was so bright and diverse that the children wanted to wear mittens of distinct colors. The children decided to swap the mittens so that each of them got one left and one right mitten in the end, and these two mittens were of distinct colors. All mittens are of the same size and fit all the children.The children started exchanging the mittens haphazardly, but they couldn't reach the situation when each child has a pair of mittens of distinct colors. Vasily Petrov, the dad of one of the children, noted that in the general case the children's idea may turn out impossible. Besides, he is a mathematician and he came up with such scheme of distributing mittens that the number of children that have distinct-colored mittens was maximum. You task is to repeat his discovery. Note that the left and right mittens are different: each child must end up with one left and one right mitten.", "input_spec": "The first line contains two integers n and m \u2014 the number of the children and the number of possible mitten colors (1\u2009\u2264\u2009n\u2009\u2264\u20095000, 1\u2009\u2264\u2009m\u2009\u2264\u2009100). The second line contains n integers c1,\u2009c2,\u2009... cn, where ci is the color of the mittens of the i-th child (1\u2009\u2264\u2009ci\u2009\u2264\u2009m).", "output_spec": "In the first line, print the maximum number of children who can end up with a distinct-colored pair of mittens. In the next n lines print the way the mittens can be distributed in this case. On the i-th of these lines print two space-separated integers: the color of the left and the color of the right mitten the i-th child will get. If there are multiple solutions, you can print any of them.", "sample_inputs": ["6 3\n1 3 2 2 1 1", "4 2\n1 2 1 1"], "sample_outputs": ["6\n2 1\n1 2\n2 1\n1 3\n1 2\n3 1", "2\n1 2\n1 1\n2 1\n1 1"], "notes": null}, "positive_code": [{"source_code": "\nuse List::Util qw(max min);\n\nmy ($n, $m) = split ' ', scalar <>;\nmy @c = sort {$a cmp $b} split ' ', scalar <>;\n\n$n == @c or die \"invalid count\";\n\nmy %cnt = ();\n\nforeach (@c){\n $cnt{$_}++;\n }\n\nmy $mcnt = max(values %cnt);\n\nmy @d = @c[( $mcnt..$#c , 0..($mcnt-1) )];\n\nmy $res = min($n, 2*($n - $mcnt));\n\nprint \"$res\\n\";\nfor(my $i = 0; $i < $n; $i++){\n print \"$c[$i] $d[$i]\\n\"\n }\n\n1;\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n\nwhile (<>){\n\t@a=@m=@smm=();\n\t$a=$c=\"\";\n\t$n=0;\n\t@_=split/ /,<>;\n\tfor (@_){$m[$_]++}\n\t@sm = sort num_rev @m;\n\tfor $i(@sm){\n\t\t$i>0 and push @smm, $i; \n\t\t}\n\n\tfor $i(@smm){\n\t\tfor $j(1..@m-1){\n\t\t\tif ($m[$j]==$i){\n\t\t\t\t$a.=\"$j \"x$m[$j];\n\t\t\t\t$m[$j]=0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tchop $a;\n\t\n\t$aa=@a=split/ /,$a;\n\tpush @a, @a;\n\n\tfor $i(0..$aa-1){\n\t\t$c.=$a[$i].\" \".$a[$i+$smm[0]].\"\\n\";\n\t\tif ($a[$i]==$a[$i+$smm[0]]){$n++}\n\t\t}\n\tprint @_-$n,\"\\n\";\n\tprint \"$c\\n\";\n\t}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n\nwhile (<>){\n\t@a=@m=@sm=@smm=();\n\t$a=$c=\"\";\n\t$n=0;\n\t@_=split/ /,<>;\n\tfor (@_){$m[$_]++}\n\t@sm = sort num_rev @m;\n\tfor $i(@sm){\n\t\t$i>0 and push @smm, $i; \n\t\t}\n#\t\tprint \"@m\\n\";\n#\t\tprint \"@sm\\n\";\n#\t\tprint \"@smm\\n\";\n\tfor $i(@smm){\n\t\t#print \" ...\\n\";\n\t\tfor $j(1..@m-1){\n\t\t\t#print \" $m[$j]==$i\\n\";\n\t\t\tif ($m[$j]==$i){\n\t\t\t\t\n\t\t\t\t$a.=\"$j \"x$m[$j];\n\t\t\t\t$m[$j]=0;\n\t\t\t\t\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t\n\t\t}\n\t# print $a; n();\n\t\n\tchop $a;\n\t\n\t$aa=@a=split/ /,$a;\n\tpush @a, @a;\n\n\tfor $i(0..$aa-1){\n\t\t$c.=$a[$i].\" \".$a[$i+$smm[0]].\"\\n\";\n\t\tif ($a[$i]==$a[$i+$smm[0]]){$n++}\n\t\t}\n\tprint @_-$n,\"\\n\";\n\tprint \"$c\\n\";\n\t}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [], "src_uid": "636e9c125197b52c91998bf91ecd8ab1"} {"nl": {"description": "Recently Vasya got interested in finding extra-terrestrial intelligence. He made a simple extra-terrestrial signals\u2019 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\u2019t. 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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 amount of days during which Vasya checked if there were any signals. The second line contains n characters 1 or 0 \u2014 the record Vasya kept each of those n days. It\u2019s 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(IN, 'input.txt'); open(OUT, '>output.txt'); ;\nprint OUT (( =~ m/^0*1(0*1)\\1+0*$/) ? 'YES' : 'NO');\n"}], "negative_code": [], "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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$)\u00a0\u2014 the number of wheels. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$0 \\leq a_i \\leq 9$$$)\u00a0\u2014 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}$$$\u00a0\u2014 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 \u00a0\u2014 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/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @moves = \n\t\tmap { ( $_ cmp 'E' ) * -length } \n\t\tmap { 1 while s/DU//g; $_ } \n\t\tmap { join '', sort split // } \n\t\tmap { ( split )[ 1 ] } map ~~<>, 1 .. @_;\n\t\n\tfor( @_ ){\n\t\tmy $move = shift @moves;\n\t\t$_ += $move;\n\t\t$_ %= 10;\n\t\t}\n\t\n\tprint \"@_\";\n\t}"}], "negative_code": [], "src_uid": "fd47f1eb701077abc5ec67163ad4fcc5"} {"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\u2009\u2264\u2009n\u2009\u2264\u200910), 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\u2019t 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\u2019t recognize whose sentence it is. He can\u2019t 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": "#!perl -pl\nINIT{<>}\n@_=(\"Freda's\",\"Rainbow's\",\"OMG>.< I don't know!\");\n$_=$_[/lala\\.$/+2*/^miao\\./-1]\n"}, {"source_code": "#!perl -pl\nINIT{<>}\n@_=(\"Freda's\",\"Rainbow's\",\"OMG>.< I don't know!\");\n$_=$_[/lala\\.$/+2*/^miao\\./-1]\n\n"}, {"source_code": "<>;\nwhile (<>) {\n $f = $_ =~ /(lala\\.)$/;\n $r = $_ =~ /(^miao\\.)/;\n if ($f == $r) {\n print \"OMG>.< I don't know!\\n\";\n } elsif ($f) { print \"Freda's\\n\"; } elsif ($r) { print \"Rainbow's\\n\"; }\n}"}, {"source_code": "#!perl -pl\nINIT{<>}\n@_=(\"Freda's\",\"Rainbow's\",\"OMG>.< I don't know!\");\n$_=$_[/lala\\.$/+2*/^miao\\./-1]\n\n"}, {"source_code": "#!perl -pl\nINIT{<>}\n@_=(\"Freda's\",\"Rainbow's\",\"OMG>.< I don't know!\");\n$_=$_[/lala\\.$/+2*/^miao\\./-1]\n"}, {"source_code": "#!perl -pl\nINIT{<>}\n@_=(\"Freda's\",\"Rainbow's\",\"OMG>.< I don't know!\");\n$_=$_[/lala\\.$/+2*/^miao\\./-1]\n\n"}, {"source_code": "#!perl -pl\nINIT{<>}\n@_=(\"Freda's\",\"Rainbow's\",\"OMG>.< I don't know!\");\n$_=$_[/lala\\.$/+2*/^miao\\./-1]\n\n"}, {"source_code": "chomp($n=);\nwhile($n > 0) {\n\tchomp($line=);\n\tif($line =~ /^miao\\..*lala\\.$/) {\n\t\tprint \"OMG>.< I don't know!\\n\";\n\t} elsif ($line =~ /lala\\.$/) {\n\t\tprint \"Freda's\\n\";\n\t} elsif ($line =~ /^miao\\./) {\n\t\tprint \"Rainbow's\\n\";\n\t} else {\n\t\tprint \"OMG>.< I don't know!\\n\";\n\t}\n\t$n--;\n}"}, {"source_code": "#!perl -pl\nINIT{<>}\n@_=(\"Freda's\",\"Rainbow's\",\"OMG>.< I don't know!\");\n$_=$_[/lala\\.$/+2*/^miao\\./-1]\n\n"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\n\nmy $n = <>;\nfor my $i (1..$n) {\n my $s = <>;\n chomp($s);\n my $isFreda = 0;\n my $isRainbow = 0;\n if ($s =~ m/^miao\\./) {\n $isRainbow = 1;\n }\n if ($s =~ m/lala\\.$/) {\n $isFreda = 1;\n }\n if ($isFreda && ! $isRainbow) {\n print \"Freda's\" . \"\\n\";\n } elsif ($isRainbow && !$isFreda) {\n print \"Rainbow's\" . \"\\n\";\n } else {\n print \"OMG>.< I don't know!\" . \"\\n\";\n }\n}\n"}, {"source_code": "#!perl -pl\nINIT{<>}\n@_=(\"Freda's\",\"Rainbow's\",\"OMG>.< I don't know!\");\n$_=$_[/lala\\.$/+2*/^miao\\./-1]\n\n"}, {"source_code": "#!perl -pl\nINIT{<>}\n@_=(\"Freda's\",\"Rainbow's\",\"OMG>.< I don't know!\");\n$_=$_[/lala\\.$/+2*/^miao\\./-1]\n\n"}, {"source_code": "#!/usr/bin/perl\n\nwhile ($eil=<>) {\n\nsub any{print \"OMG>.< I don't know!\\n\"};\n\nwhile (<>){\n \n if (/lala\\.$/ and /^miao\\./){any; next}\n if (/lala\\.$/){print \"Freda's\\n\"; next}\n if (/^miao\\./){print \"Rainbow's\\n\"; next}\n any;\n \n }\n}"}, {"source_code": "#!perl -pl\nINIT{<>}\n@_=(\"Freda's\",\"Rainbow's\",\"OMG>.< I don't know!\");\n$_=$_[/lala\\.$/+2*/^miao\\./-1]\n\n"}], "negative_code": [{"source_code": "$_ = <>;\nwhile (<>) {\n $f = $_ =~ /(lala\\.)$/;\n $r = $_ =~ /(^miao\\.)/;\n if ($f == $r) { print \"OMG>.< I don't know!\\n\"; }\n if ($f) { print \"Freda's\\n\"; } elsif ($r) { print \"Rainbow's\\n\"; }\n}"}, {"source_code": "<>;\nwhile (<>) {\n $f = $_ =~ /lala.$/;\n $r = $_ =~ /^miao./;\n if ($f == $r) { print \"OMG>.< I don't know!\"; }\n if ($f) { print \"Freda's\"; } elsif ($r) { print \"Rainbow's\"; }\n}"}, {"source_code": "chomp($n=);\nwhile($n > 0) {\n\tchomp($line=);\n\tif($line eq 'miao\\..*lala\\.') {\n\t\tprint \"OMG>.< I don't know!\\n\";\n\t} elsif ($line =~ /lala\\.$/) {\n\t\tprint \"Freda's\\n\";\n\t} elsif ($line =~ /^miao\\./) {\n\t\tprint \"Rainbow's\\n\";\n\t} else {\n\t\tprint \"OMG>.< I don't know!\\n\";\n\t}\n\t$n--;\n}"}, {"source_code": "chomp($n=);\nwhile($n > 0) {\n\tchomp($line=);\n\tif($line eq 'miao.lala.') {\n\t\tprint \"OMG>.< I don't know!\\n\";\n\t} elsif ($line =~ /lala\\.$/) {\n\t\tprint \"Freda's\\n\";\n\t} elsif ($line =~ /^miao\\./) {\n\t\tprint \"Rainbow's\\n\";\n\t} else {\n\t\tprint \"OMG>.< I don't know!\\n\";\n\t}\n\t$n--;\n}"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\n\nmy $n = <>;\nfor my $i (1..$n) {\n my $s = <>;\n chomp($s);\n my $isFreda = 0;\n my $isRainbow = 0;\n if ($s =~ m/^miao\\./) {\n $isRainbow = 1;\n }\n if ($s =~ m/lala\\.$/) {\n $isFreda = 1;\n }\n print $isFreda . ' ' . $isRainbow . '\\n';\n if ($isFreda && ! $isRainbow) {\n print \"Freda's\" . \"\\n\";\n } elsif ($isRainbow && !$isFreda) {\n print \"Rainbow's\" . \"\\n\";\n } else {\n print \"OMG>.< I don't know!\" . \"\\n\";\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nwhile ($eil=<>) {\n\nwhile (<>){\n \n if (/lala\\.$/ and !/miao\\./){print \"Freda's\\n\"; next}\n if (/miao\\.$/){print \"Rainbow's\\n\"; next}\n print \"OMG>.< I don't know!\\n\"\n \n \n }\n}"}, {"source_code": "#!/usr/bin/perl\n\nwhile ($eil=<>) {\n\nwhile (<>){\n \n if (/lala\\.$/ and !/miao\\./){print \"Freda's\\n\"; next}\n if (/miao\\.$/ and !/lala\\./){print \"Rainbow's\\n\"; next}\n print \"OMG>.< I don't know!\\n\"\n \n \n }\n}"}, {"source_code": "#!/usr/bin/perl\n\nwhile ($eil=<>) {\n\nsub any{print \"OMG>.< I don't know!\\n\"};\n\nwhile (<>){\n \n if (/lala\\.$/ and /^miao\\./){any; next}\n if (/miao\\.$/){print \"Rainbow's\\n\"; next}\n if (/^miao\\./){print \"Freda's\\n\"; next}\n any;\n \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\u2009+\u2009biy\u2009+\u2009ci\u2009=\u20090, 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 (\u2009-\u2009106\u2009\u2264\u2009x1,\u2009y1\u2009\u2264\u2009106) \u2014 the coordinates of your home. The second line contains two integers separated by a space x2, y2 (\u2009-\u2009106\u2009\u2264\u2009x2,\u2009y2\u2009\u2264\u2009106) \u2014 the coordinates of the university you are studying at. The third line contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009300) \u2014 the number of roads in the city. The following n lines contain 3 space-separated integers (\u2009-\u2009106\u2009\u2264\u2009ai,\u2009bi,\u2009ci\u2009\u2264\u2009106; |ai|\u2009+\u2009|bi|\u2009>\u20090) \u2014 the coefficients of the line aix\u2009+\u2009biy\u2009+\u2009ci\u2009=\u20090, 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": "use warnings;\nuse strict;\n\nmy ($x1, $y1) = map $_+0, split ' ', scalar <>;\nmy ($x2, $y2) = map $_+0, split ' ', scalar <>;\n\n#print \"$x1\\n\";\n\nmy $n = <> + 0;\nmy $cnt = 0;\n\nfor (my $nn = 0; $nn < $n; $nn++){\n my ($a, $b, $c) = map $_+0, split ' ', scalar <>;\n ++$cnt if ($a*$x1 + $b*$y1 + $c) * ($a*$x2 + $b*$y2 + $c) < 0\n }\n\n\nprint \"$cnt\\n\";\n\n\n"}, {"source_code": "($x[ $_ ], $y[ $_ ]) = split \" \", <> for 0 .. 1;\nfor (1 .. <>){\n\t($a, $b, $c, $r) = split \" \", <>;\n\t$r += 0 <=> $a * $x[ $_ ] + $b * $y[ $_ ] + $c for 0 .. 1;\n\t$r || $A++\n\t}\nprint 0 + $A"}], "negative_code": [{"source_code": "($x1, $y1)=split \" \", <>;\n($x2, $y2)=split \" \", <>;\nfor (1 .. <>){\n\t($a, $b, $c) = split \" \", <>;\n\t( $b ? $y1 <=> (- $a * $x1 - $c) / $b : $x1 <=> 0 ) +\n\t( $b ? $y2 <=> (- $a * $x2 - $c) / $b : $x2 <=> 0 ) || $A++\n\t}\nprint 0 + $A"}, {"source_code": "$\\ = $/;\n($x1, $y1)=split \" \", <>;\n($x2, $y2)=split \" \", <>;\n$ans = 0;\nfor (1 .. <>){\n\t($a, $b, $c) = split \" \", <>;\n\tundef $r;\n\t$r += $y1 <=> ($b ? (- $a * $x1 - $c) / $b : 1e7);\n\t$r += $y2 <=> ($b ? (- $a * $x2 - $c) / $b : -1e7);\n\t$r || $ans++\n\t}\nprint $ans"}, {"source_code": "($x1, $y1)=split \" \", <>;\n($x2, $y2)=split \" \", <>;\nfor (1 .. <>){\n\t($a, $b, $c) = split \" \", <>;\n\t( $y1 <=> ($b ? (- $a * $x1 - $c) / $b : $x1 <=> 0 ) ) +\n\t( $y2 <=> ($b ? (- $a * $x2 - $c) / $b : $y1 <=> 0 ) ) || $A++\n\t}\nprint 0 + $A"}, {"source_code": "($x1, $y1)=split \" \", <>;\n($x2, $y2)=split \" \", <>;\nfor (1 .. <>){\n\t($a, $b, $c) = split \" \", <>;\n\t( $b ? $y1 <=> (- $a * $x1 - $c) / $b : $x1 <=> 0 ) +\n\t( $b ? $y2 <=> (- $a * $x2 - $c) / $b : $y1 <=> 0 ) || $A++\n\t}\nprint 0 + $A"}, {"source_code": "$\\ = $/;\n($x1, $y1)=split \" \", <>;\n($x2, $y2)=split \" \", <>;\n$a = 0;\nfor (1 .. <>){\n\t($a, $b, $c) = split \" \", <>;\n\tundef $r;\n\t$r += $y1 <=> ($b ? (- $a * $x1 - $c) / $b : 1e7);\n\t$r += $y2 <=> ($b ? (- $a * $x2 - $c) / $b : -1e7);\n\t$r || $a++\n\t}\nprint $a"}], "src_uid": "783df1df183bf182bf9acbb99208cdb7"} {"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$$$)\u00a0\u2014 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": "#\n# Hello World Program in Perl\n#\n$n = ;\nchomp $n;\n@ip = ;\nchomp @ip;\nforeach (@ip) {\n if ($_ != \" \") {\n if ($_%4==0) {\n print \"YES\\n\";\n }else {\n print \"NO\\n\";\n }\n }\n}"}], "negative_code": [], "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\u2009<\u2009ai\u2009-\u20091 and ai\u2009<\u2009ai\u2009+\u20091). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, ai\u2009>\u2009ai\u2009-\u20091 and ai\u2009>\u2009ai\u2009+\u20091). 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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of elements in array a. The second line contains n integers a1, a2, ..., an (1\u2009\u2264\u2009ai\u2009\u2264\u20091000) \u2014 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# get input\nmy $a;\nmy $b;\n$a = ;\nchomp ( $b = );\nmy @arr = split m/ /, $b;\n\n# variable counts extrema\nmy $extrema = 0;\n\nfor ( my $i = 1; $i < (@arr - 1); $i += 1 ) {\n\n # if is local min or local max, increment extrema\n if ( (@arr[ $i ] < @arr[ $i-1 ] && @arr[ $i ] < @arr[ $i+1 ]) ||\n\t (@arr[ $i ] > @arr[ $i-1 ] && @arr[ $i ] > @arr[ $i+1 ]) ) {\n\t$extrema += 1; }}\n\n# print answer\nprint \"$extrema\\n\";\n\n\n"}, {"source_code": "# get input\nmy $a;\nmy $b;\n$a = ;\nchomp ( $b = );\nmy @arr = split m/ /, $b;\n\n# variable counts extrema\nmy $extrema = 0;\n\nfor ( my $i = 1; $i < (@arr - 1); $i += 1 ) {\n\n # if is local min or local max, increment extrema\n if ( ($arr[ $i ] < $arr[ $i-1 ] && $arr[ $i ] < $arr[ $i+1 ]) ||\n\t ($arr[ $i ] > $arr[ $i-1 ] && $arr[ $i ] > $arr[ $i+1 ]) ) {\n\t$extrema += 1; }}\n\n# print answer\nprint \"$extrema\\n\";\n"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\n<>;\nmy @x = split ' ', <>;\nmy $c = 0;\nfor my $i (1..$#x - 1) {\n $c++ if $x[$i - 1] < $x[$i] && $x[$i] > $x[$i + 1];\n $c++ if $x[$i - 1] > $x[$i] && $x[$i] < $x[$i + 1];\n}\nsay $c;\n"}, {"source_code": "<>;\n\nprint 0 + ( () = <> =~ s/(\\d+) (?=(\\d+))/ qw[ = > < ][ $1 <=> $2 ] x 2 /ger =~ /<>|>){\n\t@_ = split ' ', <>;\n\tmy $c = 0;\n\t\n\tfor my $i ( 1 .. @_ - 2 ){\n\t\t$c += (\n\t\t$_[ $i - 1 ] < $_[ $i ] and $_[ $i ] > $_[ $i + 1 ] or\n\t\t$_[ $i - 1 ] > $_[ $i ] and $_[ $i ] < $_[ $i + 1 ] \n\t\t);\n\t\t}\n\t\n\tprint $c;\n\t}"}, {"source_code": "<>, <> =~ /\n\t\\b(\\d+)\n\t[ ]\n\t(?=\t(\\d+)\n\t\t[ ]\n\t\t(\\d+)\\b\n\t)\n\t(??{\n\t\t$c += $1 < $2 && $2 > $3 || $1 > $2 && $2 < $3;\n\t\t'X'\n\t})\n/x;\n\nprint 0 + $c"}], "negative_code": [{"source_code": "<>, <> =~ /\n\t\\b(\\d+)\n\t[ ]\n\t(?=\t(\\d+)\n\t\t[ ]\n\t\t(\\d+)\\b\n\t)\n\t(??{\n\t\t$c += $1 < $2 && $2 > $3 || $1 > $2 && $2 < $3\n\t})\n/x;\n\nprint 0 + $c"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\tmy $c = 0;\n\t\n\tfor my $i ( 1 .. @_ - 2 ){\n\t\t$_[ $i - 1 ] < $_[ $i ] and $_[ $i ] > $_[ $i + 1 ] or\n\t\t$_[ $i - 1 ] > $_[ $i ] and $_[ $i ] < $_[ $i + 1 ] or\n\t\tlast;\n\t\t$c ++;\n\t\t}\n\t\n\tprint $c;\n\t}"}], "src_uid": "67cf9f83ed791a614bd01c5e0310813f"} {"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\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u2009105). The next line contains n uppercase letters without spaces \u2014 the i-th letter describes the i-th card of the Appleman.", "output_spec": "Print a single integer \u2013 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": "($n,$k)=split/ /,<>;\n@_=<>=~/./g;\nfor (@_){\n$_{$_}++\n}\n@_ = sort {\n$_{$b}<=>$_{$a}\n}\nkeys %_;\nfor (@_){\nif ($_{$_}<=$k){\n$k-=$_{$_};\n$s+=$_{$_}**2;\n}\nelse {\n$s+=$k**2;\n$k=0;\n}\n}\nprint $s"}, {"source_code": "($n,$k)=split/ /,<>;\n$_=<>;\nwhile(/./g){\n$_{$&}++\n}\n@_ = sort {\n$_{$b}<=>$_{$a}\n}\nkeys %_;\nfor (@_){\nif ($_{$_}<=$k){\n$k-=$_{$_};\n$s+=$_{$_}**2;\n}\nelse {\n$s+=$k**2;\n$k=0;\n}\n}\nprint $s"}], "negative_code": [], "src_uid": "480defc596ee5bc800ea569fd76dc584"} {"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$$$) \u00a0\u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n,$m) = map { $_ - 0 } split(/\\s+/,);\n \n my $cnt = 0;\n for(my $i=0;$i<$n;$i++){\n my $s = scalar();\n if( $i<$n-1 ){\n $cnt ++ if( substr($s,$m-1,1) eq 'R' );\n } else {\n for(my $j=0;$j<$m-1;$j++){\n $cnt++ if( substr($s,$j,1) eq 'D' );\n }\n }\n }\n \n print \"$cnt\\n\";\n \n}\n\nexit(0);\n"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of integers. The second line contains n positive integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109).", "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": " #!/usr/bin/env perl\n \n use strict;\n use warnings;\n use feature qw/ say /;\n \n use Carp;\n \n # essential\n my @tokens = ();\n \n sub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n }\n \n sub say_all {\n my @args = @_;\n say join ' ', @args;\n }\n \n sub read_line {\n chomp (my $line = );\n return $line;\n }\n \n sub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n }\n \n sub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n }\n \n sub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n }\n \n sub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n }\n \n sub ref_ref_scalar {\n my $ref = shift;\n return 1 if ref($ref) eq 'SCALAR';\n }\n \n sub toggle {\n my $ref = shift;\n croak \"$ref does not reference to scalar\" if !ref_ref_scalar($ref);\n \n $$ref = !$$ref;\n }\n \n sub odd {\n my $num = shift;\n return $num % 2 == 1;\n }\n \n sub even {\n my $num = shift;\n return $num % 2 == 0;\n }\n \n sub sum_of_digits {\n my $n = shift;\n my @numbers = split q{}, $n;\n \n my $sum = 0;\n \n for (@numbers) {\n $sum += $_;\n }\n \n return $sum;\n }\n \n # solution\n \n my $n = read_token;\n \n my @arr = split q{ }, read_line;\n \n my $pre_number = shift @arr;\n my $len = 1;\n my $answer = 0;\n \n for (@arr) {\n if ($_ <= $pre_number) {\n $answer = max($answer, $len);\n $len = 1;\n $pre_number = $_;\n next;\n }\n $len++;\n $pre_number = $_;\n\n }\n \n $answer = max($answer, $len);\n \n say $answer;\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nuse Carp;\n\n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub say_all {\n my @args = @_;\n say join ' ', @args;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\nsub ref_ref_scalar {\n my $ref = shift;\n return 1 if ref($ref) eq 'SCALAR';\n}\n\nsub toggle {\n my $ref = shift;\n croak \"$ref does not reference to scalar\" if !ref_ref_scalar($ref);\n\n $$ref = !$$ref;\n}\n\nsub odd {\n my $num = shift;\n return $num % 2 == 1;\n}\n\nsub even {\n my $num = shift;\n return $num % 2 == 0;\n}\n\nsub sum_of_digits {\n my $n = shift;\n my @numbers = split q{}, $n;\n\n my $sum = 0;\n\n for (@numbers) {\n $sum += $_;\n }\n\n return $sum;\n}\n\n# solution\n\nmy $n = read_token;\n\nmy $pre_number = read_token;\nmy $len = 1;\nmy $answer = 0;\n\nfor (1..$n-1) {\n my $num = read_token;\n if ($num <= $pre_number) {\n $answer = max($answer, $len);\n $len = 1;\n $pre_number = $num;\n next;\n }\n $len++;\n $pre_number = $num;\n\n}\n\n$answer = max($answer, $len);\n\nsay $answer;\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nuse Carp;\n\n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub say_all {\n my @args = @_;\n say join ' ', @args;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\nsub ref_ref_scalar {\n my $ref = shift;\n return 1 if ref($ref) eq 'SCALAR';\n}\n\nsub toggle {\n my $ref = shift;\n croak \"$ref does not reference to scalar\" if !ref_ref_scalar($ref);\n\n $$ref = !$$ref;\n}\n\nsub odd {\n my $num = shift;\n return $num % 2 == 1;\n}\n\nsub even {\n my $num = shift;\n return $num % 2 == 0;\n}\n\nsub sum_of_digits {\n my $n = shift;\n my @numbers = split q{}, $n;\n\n my $sum = 0;\n\n for (@numbers) {\n $sum += $_;\n }\n\n return $sum;\n}\n\n# solution\n\nmy $n = read_token;\n\nmy $pre_number = read_token;\nmy $len = 1;\nmy $answer = 0;\n\nfor (1..$n-1) {\n my $num = read_token;\n if ($num <= $pre_number) {\n $answer = max($answer, $len);\n $len = 1;\n $pre_number = $num;\n next;\n }\n $len++;\n $pre_number = $num;\n}\n\n$answer = max($answer, $len);\n\nsay $answer;\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nuse Carp;\n\n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub say_all {\n my @args = @_;\n say join ' ', @args;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\nsub ref_ref_scalar {\n my $ref = shift;\n return 1 if ref($ref) eq 'SCALAR';\n}\n\nsub toggle {\n my $ref = shift;\n croak \"$ref does not reference to scalar\" if !ref_ref_scalar($ref);\n\n $$ref = !$$ref;\n}\n\nsub odd {\n my $num = shift;\n return $num % 2 == 1;\n}\n\nsub even {\n my $num = shift;\n return $num % 2 == 0;\n}\n\nsub sum_of_digits {\n my $n = shift;\n my @numbers = split q{}, $n;\n\n my $sum = 0;\n\n for (@numbers) {\n $sum += $_;\n }\n\n return $sum;\n}\n\n# solution\n\nmy $n = read_token;\n\nmy @arr = split q{ }, read_line;\n\nmy $pre_number = shift @arr;\nmy $len = 1;\nmy $answer = 0;\n\nfor (@arr) {\n if ($_ <= $pre_number) {\n $answer = max($answer, $len);\n $len = 1;\n $pre_number = $_;\n next;\n }\n $len++;\n $pre_number = $_;\n}\n\n$answer = max($answer, $len);\n\nsay $answer;\n"}, {"source_code": "<>;\n\nprint +(sort {$b <=> $a}\n\tmap {\n\t\t$i ++;\n\t\t$_ <= $prev and $i = 1;\n\t\t$prev = $_;\n\t\t$i\n\t\t} (split ' ', <>), 0\n\t)[ 0 ]"}, {"source_code": "# cf702a\nmy $n=<>;\nlocal $/=' ';\nmy $prev;\nmy $l=0;\nmy $max=0;\nforeach (<>) {\n if($l>0&&$_<=$prev) {\n $l=0;\n }\n ++$l;\n if($l>$max) {$max=$l;}\n $prev=$_;\n}\n\nprint $max;\n\n"}], "negative_code": [], "src_uid": "4553b327d7b9f090641590d6492c2c41"} {"nl": {"description": "Let's dive into one of the most interesting areas of magic \u2014 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 (\u2009=\u2009220) 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": "use warnings;\nlocal $SIG{__WARN__} = sub {$w = shift};\n\nlocal $/;\n$m = qr/(?:^ *+(?:[^#\\n].*)?$)(?:\\n^ *+(?:[^#\\n].*)?$)*/m, \ns/$m/($a=$&)=~y{\\n \\t}{}d,$a/eg , $w && \ns/$m/($a=$&)=~y{\\n \\t}{}d,$a/eg , print for <>;"}, {"source_code": "local $/;\n$m = qr/(?:^ *+(?:[^#\\n].*)?$)(?:\\n^ *+(?:[^#\\n].*)?$)*/m, \ns/$m/($a=$&)=~y{\\n \\t}{}d,$a/eg , \ns/$m/($a=$&)=~y{\\n \\t}{}d,$a/eg , print for <>;"}, {"source_code": "@_=<>;\nfor my $i(0..@_-1)\n{ $_[$i]=~s/^ *+(?:[^#\\n].*)?\\n/ $a=$&,$a=~y{\\t }{}d,$a/e and $i and $_[$i-1] !~ /^ *+#/ and chomp $_[$i-1]}\nprint @_"}, {"source_code": "#!/usr/bin/perl -w\n\n@in = ;\nfor ($i = 0; $i < @in; $i++) {\n\t$amp[$i] = ($in[$i] =~ /^\\s*#/);\n}\nfor ($i = 0; $i < @in; $i++) {\n\tif (!$amp[$i]) { $in[$i] =~ s/ //g; }\n\tif ($i < $#in && !$amp[$i] && !$amp[$i+1]) { chomp $in[$i]; }\n}\nprint @in;"}], "negative_code": [{"source_code": "$_=<>;\nchomp;\n/^\\s*#/ or s/\\s+//g;\nprint;\nprint \"\\n\";\nwhile(<>){\nchomp;\n/^\\s*#/ or s/\\s+//g;\ns/^\\s*#/\\n$&/;\nprint;\n}"}, {"source_code": "$_=<>;\nchomp;\n/^\\s*#/ or s/\\s+//g;\nprint;\nprint \"\\n\";\nwhile(<>){\nchomp;\n/^s*#/ or s/\\s+//g;\ns/^s*#/\\n$1/;\nprint;\n}"}, {"source_code": "local $/;\ns/(?:^ *+(?:[^#\\n].*?)?$)(?:\\n^ *+(?:[^#\\n].*?)?$)*$/$a=$&,$a=~y{\\n}{}d,$a/meg , print for <>"}, {"source_code": "while(<>){\n/\\s*#/ or s/\\s//g;\nprint\n}"}, {"source_code": "local $/;\ns/(?:^ *+(?:[^#\\n].*)?$)(?>(?:\\n^ *+(?:[^#\\n].*)?$)*)$/$a=$&,$a=~y{\\n \\t}{}d,$a/meg , print for <>"}, {"source_code": "$_=join\"\",<>;\ns/\\s*([^#])/$1/gm;\nprint\n"}, {"source_code": "local $/;\ns/(^ *+(?:[^#\\n].*)?(\\n|$))( *+#|)/$a=$1,$a=~y{ \\t}{}d,$3 || ($2 && chop $a),$a.$3/meg , /\\n$/ && chop, print for <>"}, {"source_code": "local $/;\ns/(?:^ *+(?:[^#\\n].*?)?$)(?:\\n^ *+(?:[^#\\n].*?)?$)*$/$a=$&,$a=~y{\\n \\t}{}d,$a/meg , print for <>"}, {"source_code": "local $/;\ns/(?:^ *+(?:[^#\\n].*)?$)(?:(?:\\n^ *+(?:[^#\\n].*)?$){0,32766}+){0,32766}/$a=$&,$a=~y{\\n \\t}{}d,print $a/meg for <>"}, {"source_code": "local $/;\ns/(^ *+(?:[^#\\n].*)?(\\n|$))( *+#|)/$a=$1,$a=~y{ \\t}{}d,$3 || ($2 && chop $a),$a.$3/meg , $_.=$e=chop, $e eq \"\\n\" || ($_.=\"\\n\"),print for <>"}, {"source_code": "$_=<>;\ns/(\\d{3})-/$1$1-/g;\n#print;\n@_=/\\d\\d-\\d\\d-\\d\\d\\d\\d/g;\n#print\" @_\\n\";\nfor (@_){\nif (/201[345]/) {\n/^(\\d\\d)-(\\d\\d)/;\nif($1<=31 and $2==1 or $2==3 or $2==5 or $2==7 or $2==8 or $2==10 or $2==12){\npush @a,$_;\n}\nif($1<=30 and $2==4 or $2==6 or $2==9 or $2==11){\npush @a,$_;\n}\nif($1<=28 and $2==2){\npush @a,$_;\n}\n}\n#print\" @a\\n\";\n}\nfor(@a){\ns/-//;\ns/-201//;\n#print;\n$m[$_]++;\n}\n# print \"@m\\n\";\n$max=0;\nfor(@m){\n$max<$_ and $max=$_;\n}\n#print \" $max\\n\";\nfor$i(0..@m-1){\n$max==$m[$i] and $_=$i;\n}\n#$_=$a[$max];\n#print;\nlength==4 and s/^/0/;\ns/\\d\\d/$&-/;\ns/-\\d\\d/$&-201/;\nprint"}, {"source_code": "while(<>){\n/^\\s*#/ or s/\\s//g;\nprint\n}"}, {"source_code": "local $/;\ns/(^ *+(?:[^#\\n].*)?(\\n|$))( *+#|)/$a=$1,$a=~y{ \\t}{}d,$3 || ($2 && chop $a),$a.$3/meg , /\\n\\z/ or $_.=$\\,print for <>"}, {"source_code": "while(<>){\n/^\\s*#/ or s/[ \\t]+//g;\nprint\n}"}, {"source_code": "local $/;\ns/(?:^ *+(?:[^#\\n].*)?$)(?:\\n^ *+(?:[^#\\n].*)?$)*$/$a=$&,$a=~y{\\n \\t}{}d,$a/meg , print for <>"}, {"source_code": "@_=<>;\nfor my $i(1..@_-1)\n{ $_[$i]=~s/^ *+(?:[^#\\n].*)?\\n/ $a=$&,$a=~y{\\t }{}d,$a/e and $_[$i-1] !~ /^ *+#/ and chomp $_[$i-1]}\nprint @_"}, {"source_code": "$_=<>;\nchomp;\n/^\\s*#/ or s/\\s+//g;\nprint;\nprint \"\\n\";\nwhile(<>){\nchomp;\n/^\\s*#/ or s/\\s+//g;\n/^\\s*#/ and s/$/\\n/;\ns/^\\s*#/\\n$&/;\nprint;\n}"}, {"source_code": "$_=<>;\nchomp;\n/^\\s*#/ or s/\\s+//g;\nprint;\nprint \"\\n\";\nwhile(<>){\nchomp;\n/^\\s*#/ or s/\\s+//g;\ns/^\\s*#/\\n$1/;\nprint;\n}"}, {"source_code": "local $/;\ns/(?:^ *+(?:[^#\\n].*)?$)(?:\\n^ *+(?:[^#\\n].*)?$)*+$/$a=$&,$a=~y{\\n \\t}{}d,$a/meg , print for <>"}, {"source_code": "local $/;\ns/(?:^ *+(?:[^#\\n].*)?$)(?:(?:\\n *+(?:[^#\\n].*)?$){0,32766}+){0,32766}+/$a=$&,$a=~y{\\n \\t}{}d,$a/meg, print for <>"}, {"source_code": "use warnings;\nlocal $SIG{__WARN__} = sub {$w = shift};\n\nlocal $/;\n$m = qr/(?:^ *+(?:[^#\\n].*)?$)(?:\\n^ *+(?:[^#\\n].*)?$)*/m, \ns/$m/($a=$&)=~y{\\n \\t}{}d, ($w and s{\\n}{}m and undef $w),$a/eg , print for <>;"}, {"source_code": "local $/;\n$m = qr/(?:^ *+(?:[^#\\n].*)?$)(?:\\n^ *+(?:[^#\\n].*)?$)*/, \ns/$m/($a=$&)=~y{\\n \\t}{}d,$a/meg , \ns/$m/($a=$&)=~y{\\n \\t}{}d,$a/meg , print for <>;"}, {"source_code": "local $/;\ns/(^ *+(?:[^#\\n].*)?(\\n|$))( *+#|)/$a=$1,$a=~y{ \\t}{}d,$3 || ($2 && chop $a),$a.$3/meg , /\\n\\Z/ || s/\\Z/\\n/,print for <>"}, {"source_code": "local $/;\ns/^ *+(?:[^#\\n].*)?\\n/(print\"[$&]\"),($a=$&)=~y{ \\t}{}d, $a /meg , s/\\n( *+[^#\\n])/$1/meg, print for <>"}, {"source_code": "local $/;\ns/(^ *+(?:[^#\\n].*)?(\\n|$))( *+#|)/$a=$1,$a=~y{ \\t}{}d,$3 || ($2 && chop $a),$a.$3/meg , print for <>"}, {"source_code": "local $/;\ns/(?:^ *+(?:[^#\\n].*?)?$)(?:\\n^ *+(?:[^#\\n].*?)?$)*$/$a=$&,$a=~s{\\s}{}sg,$a/meg , print for <>"}, {"source_code": "$_=<>;\nchomp;\n/^.*#/ or s/\\s+//g;\nprint;\nprint \"\\n\";\nwhile(<>){\nchomp;\n/^.*#/ or s/\\s+//g;\ns/^.*#/\\n$1/;\nprint;\n}"}, {"source_code": "while(<>){\n/^\\s*#/ or s/[ \\t]+//g;\ns/^\\n$//;\nprint\n}"}, {"source_code": "local $/;\ns/(^ *+(?:[^#\\n].*)?(\\n|$))( *+#|)/$a=$1,$a=~y{ \\t}{}d,$3 || ($2 && chop $a),$a.$3/meg , /\\n\\Z/ || s/$/\\n/,print for <>"}, {"source_code": "$_=<>;\nchomp;\n/^\\s*#/ or s/\\s+//g;\nprint;\nprint \"\\n\";\nwhile(<>){\nchomp;\n/^\\s*#/ or s/\\s+//g;\n/^\\s*#/ and s/$/\\n/;\ns/^\\s*#/\\n$&/;\nprint;\n}\nchomp or print\"\\n\";"}, {"source_code": "@_=<>;\nfor(@_){\n/.*#/ or s/\\s//g;\n}\nprint shift @_;\nfor(@_){\ns/.*#/\\n$&/;\n}\nprint join\"\",@_;"}, {"source_code": "local $/;\ns/^ *+(?:[^#\\n].*)?\\n/($a=$&)=~y{ \\t}{}d, $a /meg , s/\\n( *+[^#\\n])/$1/meg, print for <>"}, {"source_code": "s/^ *+[^#].*/$a=$&,$a=~s{\\s}{}sg,$a/seg || ($i++ && s/^/\\n/),print for <>"}, {"source_code": "local $/;\ns/(^ *+(?:[^#\\n].*)?(\\n|$))( *+#|)/$a=$1,$a=~y{ \\t}{}d,$3 || ($2 && chop $a),$a.$3/meg , /\\n\\z/ || s/$/\\n/,print for <>"}], "src_uid": "a16bb696858bc592c33dcf0fd99df197"} {"nl": {"description": "Dreamoon likes to play with sets, integers and . is defined as the largest positive integer that divides both a and b.Let S be a set of exactly four distinct integers greater than 0. Define S to be of rank k if and only if for all pairs of distinct elements si, sj from S, .Given k and n, Dreamoon wants to make up n sets of rank k using integers from 1 to m such that no integer is used in two different sets (of course you can leave some integers without use). Calculate the minimum m that makes it possible and print one possible solution.", "input_spec": "The single line of the input contains two space separated integers n, k (1\u2009\u2264\u2009n\u2009\u2264\u200910\u2009000,\u20091\u2009\u2264\u2009k\u2009\u2264\u2009100).", "output_spec": "On the first line print a single integer \u2014 the minimal possible m. On each of the next n lines print four space separated integers representing the i-th set. Neither the order of the sets nor the order of integers within a set is important. If there are multiple possible solutions with minimal m, print any one of them.", "sample_inputs": ["1 1", "2 2"], "sample_outputs": ["5\n1 2 3 5", "22\n2 4 6 22\n14 18 10 16"], "notes": "NoteFor the first example it's easy to see that set {1,\u20092,\u20093,\u20094} isn't a valid set of rank 1 since ."}, "positive_code": [{"source_code": "@_ = qw(1 2 3 5);\n\n($n,$k)=split/ /,<>;\nfor $i(0..$n-1){\n\tpush @a, $_*$k for @_;\n\t$_+=6 for @_;\n\t}\nprint $a[@a-1],$/;\nprint $_,(++$j % 4?\" \":$/) for @a"}], "negative_code": [{"source_code": "@_ = qw(1 2 3 5);\n\n($n,$k)=split/ /,<>;\nfor $i(0..$n-1){\n\t$_+=$i*6 for @_;\n\tpush @a, $_*$k for @_;\n\t}\nprint $a[@a-1],$/;\nprint $_,(++$j % 4?\" \":$/) for @a"}, {"source_code": "@_ = qw(0 1 3 5);\n\n($n,$k)=split/ /,<>;\nfor $i(0..$n-1){\n\t$_+=$i*6 for @_;\n\tpush @a, $_*$k for @_;\n\t}\n$a[0]=2*$k;\nprint $a[@a-1],$/;\nprint $_,(++$j % 4?\" \":$/) for @a"}], "src_uid": "5e7b4d1e11628152e33d3e18e30f4530"} {"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 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009100, ) \u2014 the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1\u2009\u2264\u2009vi,\u2009ui\u2009\u2264\u2009n, vi\u2009\u2260\u2009ui) \u2014 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\u2009-\u20096.", "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/perl\n\nuse strict;\nuse warnings;\nuse Data::Dumper;\nuse List::Util qw(min max);\n\nmy $line = <>;\nchomp($line);\nmy ($n, $m) = split(/\\s/, $line);\n\nmy $INF = 10000;\nmy @dist;\n\nfor my $i (1..$n) {\n for my $j (1..$n) {\n if ($i == $j) {\n $dist[$i][$j] = 0;\n }\n else {\n $dist[$i][$j] = $INF;\n }\n }\n}\n\nfor (1..$m) {\n $line = <>;\n chomp($line);\n my ($a, $b) = split(/\\s/, $line);\n\n $dist[$a][$b] = 1;\n $dist[$b][$a] = 1;\n}\n\nfor my $k (1..$n) {\n for my $i (1..$n) {\n for my $j (1..$n) {\n $dist[$i][$j] = min($dist[$i][$j], $dist[$i][$k] + $dist[$k][$j]);\n }\n }\n}\n\nmy @from1;\nmy @fromN;\n$from1[1] = 1;\n$fromN[$n] = 1;\n\nfor my $d (1..$n) {\n for my $i (1..$n) {\n if ($dist[1][$i] == $d) {\n for my $j (1..$n) {\n if ($dist[1][$j] == $d-1 && $dist[$j][$i] == 1) {\n $from1[$i] += $from1[$j];\n }\n }\n }\n }\n for my $i (1..$n) {\n if ($dist[$n][$i] == $d) {\n for my $j (1..$n) {\n if ($dist[$n][$j] == $d-1 && $dist[$j][$i] == 1) {\n $fromN[$i] += $fromN[$j];\n }\n }\n }\n }\n}\n\nmy @weight;\n\nfor my $i (1..$n) {\n for my $j (1..$n) {\n if ($dist[$i][$j] == 1 && $dist[1][$i] + 1 + $dist[$j][$n] == $dist[1][$n]) {\n $weight[$i] += $from1[$i] * $fromN[$j];\n $weight[$j] += $from1[$i] * $fromN[$j];\n }\n }\n}\n\nmy $max = 0;\nfor my $i (1..$n) {\n $max = max($max, $weight[$i]);\n}\n#print Dumper @from1;\n#print Dumper @weight;\nprintf(\"%.12f\", $max / $from1[$n]);\n\n"}], "negative_code": [], "src_uid": "a69b342ef5f6e11f7aa4fee71e149aa2"} {"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\u2009=\u20093, 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\u2009\u2264\u2009n\u2009\u2264\u20091000)\u00a0\u2014 the number of digits in the cards Sherlock and Moriarty are going to use. The second line contains n digits\u00a0\u2014 Sherlock's credit card number. The third line contains n digits\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy( $A, $B ) = map { chomp; $_ } map ~~<>, 1 .. 2;\n\t\n\tmy @A = split //, $A;\n\tmy @B = split //, $B;\n\t\n\tmy @sA = sort @A;\n\tmy @sB = sort @B;\n\t\n\tmy $cnt = $_;\n\t\n\tfor (@sA){\n\t\t@sB or last;\n\t\tmy $s = shift @sB;\n\t\tif( $s < $_ ){ redo }\n\t\t$cnt --;\n\t\t}\n\t\n\tprint $cnt;\n\t\n\t@sB = sort @B;\n\t\n\t$cnt = 0;\n\t\n\tfor (@sA){\n\t\t@sB or last;\n\t\tmy $s = shift @sB;\n\t\tif( $s <= $_ ){ redo }\n\t\t$cnt ++;\n\t\t}\n\t\n\tprint $cnt;\n\t\n\t}"}], "negative_code": [], "src_uid": "d1a35297090550d9a95f014b0c09a01a"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009105, 1\u2009\u2264\u2009a\u2009\u2264\u2009180)\u00a0\u2014 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\u2009-\u200967|\u2009<\u2009|90\u2009-\u200967|. 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\u2009-\u200968|\u2009<\u2009|45\u2009-\u200968|. 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $alpha ) = split;\n\t\n\tmy $beta = ( 180 - 360 / $n ) / ( $n - 2 );\n\t\n\t$debug and print $beta;\n\t\n\tmy $m = int $alpha / $beta;\n\t\n\t$m +=\n\tabs( $m * $beta - $alpha ) < abs( ($m + 1) * $beta - $alpha ) ?\n\t\t0 : 1;\n\t\n\t$m ||= 1;\n\t\n\t$debug and print $m;\n\t\n\tprint join ' ', 2, 1, 2 + $m > $n ? $n : 2 + $m;\n\t\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $alpha ) = split;\n\t\n\tmy $beta = ( 180 - 360 / $n ) / ( $n - 2 );\n\t\n\t$debug and print $beta;\n\t\n\tmy $m = int $alpha / $beta;\n\t\n\t$m +=\n\tabs( $m * $beta - $alpha ) < abs( ($m + 1) * $beta - $alpha ) ?\n\t\t0 : 1;\n\t\n\t$m ||= 1;\n\t\n\t$debug and print $m;\n\t\n\tprint join ' ', 2, 1, 2 + $m;\n\t\n\t}"}], "src_uid": "3cdd85f86c77afd1d3b0d1e951a83635"} {"nl": {"description": "Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.The park consists of n squares connected with (n\u2009-\u20091) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.Andryusha wants to use as little different colors as possible. Help him to choose the colors!", "input_spec": "The first line contains single integer n (3\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105)\u00a0\u2014 the number of squares in the park. Each of the next (n\u2009-\u20091) lines contains two integers x and y (1\u2009\u2264\u2009x,\u2009y\u2009\u2264\u2009n)\u00a0\u2014 the indices of two squares directly connected by a path. It is guaranteed that any square is reachable from any other using the paths.", "output_spec": "In the first line print single integer k\u00a0\u2014 the minimum number of colors Andryusha has to use. In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.", "sample_inputs": ["3\n2 3\n1 3", "5\n2 3\n5 3\n4 3\n1 3", "5\n2 1\n3 2\n4 3\n5 4"], "sample_outputs": ["3\n1 3 2", "5\n1 3 2 5 4", "3\n1 2 3 1 2"], "notes": "NoteIn the first sample the park consists of three squares: 1\u2009\u2192\u20093\u2009\u2192\u20092. Thus, the balloon colors have to be distinct. Illustration for the first sample. In the second example there are following triples of consequently connected squares: 1\u2009\u2192\u20093\u2009\u2192\u20092 1\u2009\u2192\u20093\u2009\u2192\u20094 1\u2009\u2192\u20093\u2009\u2192\u20095 2\u2009\u2192\u20093\u2009\u2192\u20094 2\u2009\u2192\u20093\u2009\u2192\u20095 4\u2009\u2192\u20093\u2009\u2192\u20095 We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. Illustration for the second sample. In the third example there are following triples: 1\u2009\u2192\u20092\u2009\u2192\u20093 2\u2009\u2192\u20093\u2009\u2192\u20094 3\u2009\u2192\u20094\u2009\u2192\u20095 We can see that one or two colors is not enough, but there is an answer that uses three colors only. Illustration for the third sample. "}, "positive_code": [{"source_code": "<>; for (<>) {\n\t($i, $j) = split;\n\tpush @{ $a[$i] }, $j;\n\tpush @{ $a[$j] }, $i;\n}\n\n@q = (1), $c[1] = 1; \nwhile (@q) {\n\t$i = shift @q;\n\t@j = @{ $a[$i] };\n\t$c = 1;\n\tfor $j (@j) {\n\t\tif ($j != $p[$i]) {\n\t\t\t$c++ while $c == $c[$i] || $c == $c[$p[$i]];\n\t\t\t$c[$j] = $c;\n\t\t\tpush @q, $j;\n\t\t\t$p[$j] = $i;\n\t\t\t$c++;\n\t\t}\n\t}\n\t$C = --$c > $C? $c: $C;\n}\n\t\nprint \"$C\\n@c\";"}], "negative_code": [], "src_uid": "2aaa31d52d69ff3703f93177b25671a3"} {"nl": {"description": "The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters \"+\", \"-\", \"X\". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009150) \u2014 the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter \u00abX\u00bb). Thus, there are no empty statements. The operation and the variable can be written in any order.", "output_spec": "Print a single integer \u2014 the final value of x.", "sample_inputs": ["1\n++X", "2\nX++\n--X"], "sample_outputs": ["1", "0"], "notes": null}, "positive_code": [{"source_code": "$_=join'',<>;print.5*(s/\\+//g- s/-//g)"}, {"source_code": "<>;\nwhile (<>){/\\+/?($i++):($i--)}\nprint $i"}, {"source_code": "\nmy $n;\nchomp ( $n = );\n\nmy $x = 0;\nmy $str;\nfor ( my $i = 0; $i < $n; $i += 1 ) {\n chomp ( $str = );\n if ( $str =~ m/(X\\+\\+)|(\\+\\+X)/ ) {\n\t$x += 1;}\n if ( $str =~ m/(X--)|(--X)/ ) {\n\t$x -= 1;}\n}\n\nprint \"$x\\n\";\n\n\n"}, {"source_code": "#!/usr/bin/env perl\n\nuse v5.012;\nuse warnings;\n\nmy $cnt = ;\n\nmy $x = 0;\nwhile ($cnt--) {\n\tchomp(my $stmt = );\n\t$x++ if $stmt =~ /\\+\\+/;\n\t$x-- if $stmt =~ /\\-\\-/;\n}\nprint $x;\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy $ans = 0;\n\nwhile($n-- > 0){\n my $var = <>;\n if($var =~ /\\+\\+/){\n $ans++;\n }elsif($var =~ /--/){\n $ans--;\n }\n}\nprint \"$ans\\n\";"}, {"source_code": "chomp($n = );\nwhile ($n--)\n{\n chomp($_ = );\n if (/-/)\n {\n --$r;\n }\n else\n {\n ++$r;\n }\n}\nprint \"$r\\n\";\n"}, {"source_code": "$_=join'',<>;print.5*(s/\\+//g- s/-//g)"}, {"source_code": "\nmy $n = <>;\nmy $ans = 0;\nmy $str;\nfor (1..$n) {\n $str = <>;\n chomp $str;\n $ans++ if ($str eq \"++X\" || $str eq \"X++\");\n $ans-- if ($str eq \"--X\" || $str eq \"X--\");\n}\nprint $ans . \"\\n\";"}, {"source_code": "#!/usr/bin/env perl\n\nuse 5.010;\n\n@lines = <>;\n$accum = 0;\n\nforeach $line (@lines) {\n\tif ($line =~ /\\+\\+/) {\n\t\t$accum++;\n\t}\n\tif ($line =~ /--/) {\n\t\t$accum--;\n\t}\n}\n\nsay $accum;"}, {"source_code": "#!/usr/bin/perl\n\nmy $n = <>;\nmy $cnt = 0;\n\nwhile ($n-- > 0){(<>=~/\\+/g)?$cnt++:$cnt--;}\n\nprint $cnt;\n"}, {"source_code": "my $n = ;\nmy $x = 0;\nwhile($n--) {\n my $s = ;\n chomp($x);\n if(index($s, '++') != -1) {\n $x++;\n }\n elsif(index($s, '--') != -1) {\n $x--;\n }\n}\nprint $x;"}, {"source_code": "#!perl\n\nuse strict;\nuse warnings;\n\nchomp( my $n = );\n\nmy $acc_num = 0 ;\nforeach ( 0 .. ($n-1) ){\n my $readnum = ;\n \n if ( $readnum =~ m/\\+\\+/ ){\n $acc_num++;\n }else{\n $acc_num--;\n }\n}\nprint \"$acc_num\\n\" ;\n"}, {"source_code": "$s+=<>=~/-/?-1:1 for 1..<>;print$s"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\n\n\n$x = 0;\nchomp($n = <>);\nwhile ($n-- > 0) {\n\tchomp($line = <>);\n\tif ((substr $line, 1, 1) eq \"+\") {\n\t\t$x++\n\t} else {\n\t\t$x--;\n\t}\n}\nprint $x;\n"}, {"source_code": "chomp ($n = );\n$x = 0;\nwhile ($n--) {\n\tchomp ($str = );\n\tif ($str =~ /--/) { $x-- } else { $x++}\n}\nprintf \"$x\\n\";\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nchomp($n);\n\ndie ('n should be between 1 and 150') unless $n >= 1 && $n <= 150;\n\n\nmy $x = 0;\nfor (my $i = 0; $i < $n; $i++) {\n my $in = <>;\n chomp($in);\n if ($in =~ m/\\+\\+/) {\n $x++;\n } elsif ($in =~ m/\\-\\-/) {\n $x--;\n } else {\n die ('invalid input');\n }\n}\n\n\nprint $x;\n"}, {"source_code": "$s+=<>=~/-/?-1:1 for 1..<>;print$s"}, {"source_code": "#!/usr/bin/perl\n\n$n = ;\n$x = 0;\nfor (my $i = 0; $i < $n; $i++){\n\t$buf = ;\n\tif (substr($buf, 1, 1) eq \"+\"){\n\t\t$x++;\n\t} else {\n\t\t$x--;\n\t}\n}\nprint \"$x\\n\";\n"}, {"source_code": "chomp($n=);\n$x = 0;\nwhile($n-- > 0) {\n\tchomp($line = );\n\tif($line =~ /\\+\\+/) {\n\t\t$x++;\n\t} else {\n\t\t$x--;\n\t}\n}\nprint \"$x\\n\";"}, {"source_code": "#!perl\n\n$\\ = \"\\n\";\n\nmy $n = <>;\n\nmy $x = 0;\nfor (1..$n)\n{\n\tmy $s = <>;\n\n\tif($s =~ m/\\+/)\n\t{\n\t\t$x++;\n\t}\n\telse\n\t{\n\t\t$x--;\n\t}\n}\n\nprint $x;"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy $x = 0;\nfor (1..$n) {\n my @term = split '', <>;\n if ($term[1] eq '+') {\n $x++;\n } else {\n $x--;\n }\n}\n\nprint \"$x\\n\";\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nchomp(my $n = );\nmy $x = 0;\nfor (my $i = 0; $i < $n; $i++) {\n chomp(my $cmd = );\n if (index($cmd, \"++\") != -1) {\n $x += 1;\n } elsif (index($cmd, \"--\") != -1) {\n $x -= 1;\n }\n}\nprint $x;"}, {"source_code": "$s+=<>=~/-/?-1:1 for 1..<>;print$s"}, {"source_code": "use strict;\nuse warnings;\nmy $N=;\nmy $X=0;\nfor(my $i=0;$i<$N;$i++){\n my $s=;\n if ($s =~ /[X++]{3}/ ){\n $X+=1;\n }\n else {\n $X-=1;\n }\n}\nprint STDOUT $X;"}, {"source_code": "import strict;\nimport warnings;\n\nmy @x = (\"++X\", \"X++\");\nmy $_ = <>;\nmy $ans = 0;\nfor($__ = 1 ; $__ <= $_ ; $__++)\n{\n $s = <>;\n chomp($s);\n (($s eq $x[0]) or ($s eq $x[1]) )? $ans ++ : $ans --;\n}\n\nprint \"$ans\";"}, {"source_code": ";\n$x = 0;\n\nwhile()\n{\n ($_ =~ /\\+\\+/) ? ++$x : --$x;\n}\n\nprint STDOUT $x;"}, {"source_code": "#!usr/bin/perl\n#use warnings;\nuse 5.012;\nuse 5.010;\nuse 5.014;\n#use strict;\n#use autodie;\nmy($n,$count,$x,$line);\nchomp($n=);\n$count=0;\nwhile($count ne $n)\n{\n $count++;\n chomp($line=);\n if($line=~m/\\+\\+/)\n {$x++;}\n else \n {$x--;}\n}\nsay \"$x\";"}, {"source_code": "my $res = 0;$count = int(); for(1..$count){$_ = ; s/x\\+\\+|\\+\\+x/+/i; s/x\\-\\-|\\-\\-x/-/i;chomp; if( $_ eq \"+\"){ $res += 1; }else{ $res -= 1; } } print \"$res\\n\""}, {"source_code": "$s+=<>=~/-/?-1:1 for 1..<>;print$s"}, {"source_code": "$n = <>;\nchomp $n;\n\n$x = 0;\n\nfor (1..$n) {\n $cmd = <>;\n chomp $cmd;\n\n if ($cmd =~ /\\+\\+/) { $x++; }\n else { $x--; }\n}\n\nprint \"$x\\n\";\n\n"}, {"source_code": "$n = <>;\n$x = 0;\nfor ($i = 0; $i < $n; ++$i) {\n $str = <>;\n if ((index $str, '+') != -1) { ++$x; }\n else { --$x; }\n}\nprint $x;"}, {"source_code": "#!/bin/perl\n\n$n = ;\n$l = 0;\n\nfor($i=0;$i<$n;$i++)\n{\n\n $s = ;\n chomp $s;\n $s =~ s/\\+\\+/X/;\n\n if( $s eq 'XX' )\n {\n\n $l++;\n\n }\n else\n {\n\n $l--;\n\n }\n\n}\n\nprint $l;\n"}, {"source_code": "#!/usr/bin/perl;\n\n$x = 0;\n$n = ;\n\nfor ($i = 0; $i < $n; $i++) {\n\t$s = ;\n\tchomp($s);\n\t($s eq \"++X\" || $s eq \"X++\") && $x++;\n\t($s eq \"--X\" || $s eq \"X--\") && $x--;\n}\n\nprint $x;\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\n\nmy $n = ;\nchomp($n);\n\nmy $x = 0;\n\nforeach (1..$n) {\n\t$_ = ;\n\tchomp;\n\tif ($_ eq \"X++\" or $_ eq \"++X\") { $x++; }\n\telse { $x--; }\n}\n\nprint $x;\n"}, {"source_code": "#!/usr/bin/perl\nuse strict; use warnings;\n\nmy $x = 0; ;\n/-/ ? --$x : ++$x while ;\nprint $x . \"\\n\"\n"}, {"source_code": "$n = ;\n@a = undef;\n$result = 0;\nforeach (0..$n - 1) {\n\tchomp ($a[$_] = );\n\t#unshift (@a, )\n}\nforeach $a (@a) {\n\tif (index ($a, '+') >= 0) {\n\t\t$result += 1;\n\t} else {\n\t\t$result -= 1;\n\t}\n}\nprint ($result);"}, {"source_code": "my $n = <>;\n\nmy $i;\n$i += <> =~ /-/ ? -1 : 1 for 1..$n;\n\nprint $i;\n"}, {"source_code": "<>;$i+=0cmp/-/for<>;print$i"}, {"source_code": "<>;\n$i-=.1<=>/\\+/ for <>;\nprint $i"}, {"source_code": "<>;\nwhile (<>){/\\+/?($i++):($i--)}\nprint $i"}, {"source_code": "my $num = <>;\nmy $x;\n<> =~ /\\+/ ? $x++ : $x-- for 1..$num;\nprint $x;"}, {"source_code": "my $len = ;\nmy $ans = 0;\n\nfor (1 .. $len) {\n\tmy $middle = substr , 1, 1;\n\n\t$ans += $middle eq \"+\"? 1 : -1;\n}\n\nprint $ans.\"\\n\";\n"}, {"source_code": "my $len = ;\nmy $ans = 0;\n\nfor my $val (1 .. $len) {\n\tmy $comm = ;\n\tmy $middle = substr $comm, 1, 1;\n\n\t$ans = $middle eq \"+\"? ($ans + 1) : ($ans - 1);\n}\n\nprint $ans.\"\\n\";\n"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\nmy $N=;\nmy $X=0;\nfor(my $i=0;$i<$N;$i++){\n my $s=;\n if ($s == \"X++\" || $s == \"++X\"){\n $X+=1;\n }\n else {\n $X-=1;\n }\n}\nprint STDOUT; $X;\n"}, {"source_code": "use strict;\nuse warnings;\nmy $N=;\nmy $X=0;\nfor(my $i=0;$i<$N;$i++){\n my $s=;\n if ($s == \"X++\"){\n $X+=1;\n }\n else {\n $X-=1;\n }\n}\nprint STDOUT; $X;\n"}, {"source_code": "use strict;\nuse warnings;\nmy $N=;\nmy $X=0;\nfor(my $i=0;$i<$N;$i++){\n my $s=;\n if ($s == \"X++\" || $s == \"++X\"){\n $X+=1;\n }\n else {\n $X-=1;\n }\n}\nprint STDOUT $X;\n"}, {"source_code": "#!usr/bin/perl\n#use warnings;\nuse 5.012;\nuse 5.010;\nuse 5.014;\n#use strict;\n#use autodie;\nmy($n,$count,$x,$line);\nchomp($n=);\n$count=0;\nwhile($count lt $n)\n{\n $count++;\n chomp($line=);\n if($line=~m/\\+\\+/)\n {$x+=1;}\n else \n {$x-=1;}\n}\nsay \"$x\";"}, {"source_code": "my $n = <>;\nmy $i;\n\n$i += <> =~ /-/ ? -1 : 1 for 1..$n;\nprint $i, '\\n';"}], "src_uid": "f3cf7726739290b280230b562cac7a74"} {"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,\u2009t2,\u2009...,\u2009tn, 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\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009-\u20091) such that t1\u2009<\u20090,\u2009t2\u2009<\u20090,\u2009...,\u2009tk\u2009<\u20090 and tk\u2009+\u20091\u2009>\u20090,\u2009tk\u2009+\u20092\u2009>\u20090,\u2009...,\u2009tn\u2009>\u20090. 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of days for which Vasya has been measuring the temperature. The second line contains a sequence of n integers t1,\u2009t2,\u2009...,\u2009tn (|ti|\u2009\u2264\u2009109) \u2014 the sequence of temperature values. Numbers ti are separated by single spaces.", "output_spec": "Print a single integer \u2014 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": "if ($ENV{USER} ne 'watashi') {\n push @ARGV, 'input.txt';\n open STDOUT, '>output.txt';\n}\n\n$n = <>;\n$_ = <>;\n$y = y/-/-/;\n@a = grep {$_} split;\n$y += $n - @a;\npop @a;\n\n($m, $x) = ($n, 0);\nfor (@a) {\n $y += /-/ ? -1 : 1;\n $m = $y if $y < $m;\n}\nprint $m;\n"}], "negative_code": [{"source_code": "push @ARGV, 'input.txt';\nopen STDOUT, '>output.txt';\n\n$n = <>;\n$_ = <>;\n$y = y/-/-/;\n@a = split;\npop @a;\n($m, $x) = ($n, 0);\nfor (@a) {\n $y += /-/ ? -1 : 1;\n $m = $y if $y < $m;\n}\nprint $m;\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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n \r\n my ($l1,$r1,$l2,$r2) = map { $_ - 0 } split(/\\s+/,);\r\n if( not ( $r1 < $l2 or $r2 < $l1 ) ){\r\n my $v = &max($l1,$l2);\r\n print \"$v\\n\";\r\n } else {\r\n print ( ( $l1 + $l2 ) . \"\\n\" );\r\n }\r\n \r\n}\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "negative_code": [], "src_uid": "c783eaf1bf7e4e7321406431030d5aab"} {"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\u00a0\u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 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,\u2009x2,\u2009...,\u2009xn (0\u2009\u2264\u2009xi\u2009\u2264\u2009109)\u00a0\u2014 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\u00a0\u2014 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": "<>; $_ = <>; @x = split \" \", <>;\npush @v, $x[pos()-1] - $x[pos()-2] while /RL/g;\n@v = sort { $a <=> $b} @v;\nprint $v[0]/2 || -1;\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = reverse ~~<>, chomp;\n\t@_ = split ' ', <>;\n\t\n\t$min = ~0;\n\n\tfor $i (@_){\n\t\t$x = chop;\n\t\t$x eq 'L' ? \n\t\t\t\tdo { $R and $min > $i - $j and $min = $i - $j;\n\t\t\t\t\t$L = 1; $R = 0;\n\t\t\t\t}\n\t\t\t:\n\t\t\t\tdo { $L = 0; $R = 1; $j = $i };\n\t\t\n\t\t}\n\t\n\tprint $min == ~0 ? -1 : $min / 2\n\t}"}, {"source_code": "# cf699a\nuse strict;\nmy $n=<>;\nmy @ds=map {getc()} 1..$n;\ngetc();\nmy @cs;\n{\nlocal $/=' ';\n@cs=<>;\n}\nchomp $cs[$#cs];\nmy $mint=-1;\nforeach my $i (0..$n-2) {\n my $d1=$ds[$i];\n my $d2=$ds[$i+1];\n my $c1=$cs[$i];\n my $c2=$cs[$i+1];\n if($d1 eq 'R'&&$d2 eq 'L') {\n my $t=($c2-$c1)/2;\n if($mint==-1||$t<$mint) {$mint=$t;}\n }\n}\nprint $mint;\n"}], "negative_code": [{"source_code": "<>; $_ = <>; @x = split \" \", <>;\npush(@v, $x[pos()-1] - $x[pos()-2]) while /RL/g;\nprint @v? (sort @v)[0]/2: -1;\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = reverse ~~<>, chomp;\n\t@_ = split ' ', <>;\n\t\n\t$max = 0;\n\t\n\tfor $i (@_){\n\t\t$x = chop;\n\t\t$x eq 'L' ? \n\t\t\t\tdo { $R and $max < $i - $j and $max = $i - $j;\n\t\t\t\t\t$L = 1; $R = 0;\n\t\t\t\t}\n\t\t\t:\n\t\t\t\tdo { $L = 0; $R = 1; $j = $i };\n\t\t\n\t\t}\n\t\n\tprint $max / 2\n\t}"}], "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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the total likes to the published photoes. The second line contains n positive integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20091\u2009000\u2009000), 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/perl\n\nmy $sum = <>;\nmy @like_hist = split \" \", <>;\n@like_hist = map { $like_hist[$_] } 0..$sum-1;\n\nmy $counts = {};\nmy $max = {id => $like_hist[0], count => 1 };\n\nmy $k = 0;\nfor (0..$sum-1) {\n\tmy $id = $like_hist[$_];\n\tmy $count = ++$counts->{$id};\n\t$max = {id => $id, count => $count } if ($count > $max->{count});\n}\n\nprint $max->{id};"}, {"source_code": "<>; $max = ++$h{$_} > $h{$max}? $_: $max for split ' ', <>;\nprint $max;\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my $num = $_;\n my $likes;\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (0..$num-1) {\n my $id = $array[$_];\n $likes->{$id}++;\n my $count = $likes->{$id};\n if ($likes->{$id} > $max->{count}) {\n $max = {id => $id,\n count => $count};\n }\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n chomp;\n my $num = $_;\n my ($res_key, $res_value) = (0, 0);\n my %likes;\n my $line = <>;\n chomp($line);\n my @array = split / /, $line;\n for (@array) {\n $likes{$_}++;\n while (my ($key, $value) = each %likes) {\n if ($value > $res_value) {\n $res_value = $value;\n $res_key = $key;\n }\n }\n }\n print $res_key, \"\\n\";\n}\n\nexit 0;\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my $num = $_;\n my $likes = {};\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (0..$num-1) {\n my $id = $array[$_];\n my $count = ++$likes->{$id};\n $max = {id => $id, count => $count} \n if ($count > $max->{count});\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my $num = $_;\n my %likes;\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (@array) {\n my $count = ++$likes{$_};\n $max = {id => $_, count => $count}\n if ($count > $max->{count});\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 6.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 02:23:29 PM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nmy $sum = <>;\nmy @array = split ' ', <>;\nmy $max = {id => $array[0], count => 1};\nmy %likes;\nfor (0..$sum-1) {\n my $id = $array[$_];\n my $count = ++$likes{$id};\n $max = {id => $id, count => $count} \n if ($count > $max->{count});\n}\nprint $max->{id}, \"\\n\";\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my $num = $_;\n my %likes;\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (0..$num-1) {\n $likes{$array[$_]}++;\n if ($likes{$array[$_]} > $max->{count}) {\n $max = {id => $array[$_], count => $likes{$array[$_]}};\n }\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my %likes;\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (@array) {\n $likes{$_}++;\n if ($likes{$_} > $max->{count}) {\n $max = {id => $_, count => $likes{$_}};\n }\n }\n say $max->{id};\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\n\nwhile (<>) {\n my ($likes, @array, $max, $count);\n @array = split ' ', <>;\n $max = {id => $array[0], count => 1};\n for (@array) {\n $count = ++$likes->{$_};\n $max = {id => $_, count => $count}\n if ($count > $max->{count});\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nmy ($likes, @array, $max, $count);\n\nwhile (<>) {\n @array = split ' ', <>;\n $max = {id => $array[0], count => 1};\n for (@array) {\n $count = ++$likes->{$_};\n $max = {id => $_, count => $count}\n if ($count > $max->{count});\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 6.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 02:23:29 PM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n\nmy $sum = $_;\nmy @array = split ' ', <>;\nmy $max = {id => $array[0], count => 1};\nmy %likes;\nfor (0..$sum-1) {\n my $id = $array[$_];\n my $count = ++$likes{$id};\n $max = {id => $id, count => $count} \n if ($count > $max->{count});\n}\nprint $max->{id}, \"\\n\";\n\n}\nexit 0;\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my $num = $_;\n my $likes;\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (@array) {\n my $count = ++$likes->{$_};\n $max = {id => $_, count => $count}\n if ($count > $max->{count});\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use 5.020;\n\nwhile (<>) {\n my $num = $_;\n my $likes;\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (@array) {\n my $count = ++$likes->{$_};\n $max = {id => $_, count => $count}\n if ($count > $max->{count});\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my %likes;\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (@array) {\n $likes{$_}++;\n if ($likes{$_} > $max->{count}) {\n $max = {id => $_, count => $likes{$_}};\n }\n }\n print $max->{id};\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my $num = $_;\n my %likes;\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (0..$num-1) {\n my $id = $array[$_];\n $likes{$id}++;\n if ($likes{$id} > $max->{count}) {\n $max = {id => $id,\n count => $likes{$id}};\n }\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 6.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 02:23:29 PM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nmy $sum = <>;\nmy @array = split ' ', <>;\n@array = map {$array[$_]} 0..$sum-1;\nmy $max = {id => $array[0], count => 1};\nmy %likes;\nfor (0..$sum-1) {\n my $id = $array[$_];\n my $count = ++$likes{$id};\n $max = {id => $id, count => $count} \n if ($count > $max->{count});\n}\nprint $max->{id}, \"\\n\";\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my %likes;\n my $line;\n chomp($line = <>);\n my @array = split / /, $line;\n my ($res_key, $res_value) = ($array[0], 1);\n for (@array) {\n $likes{$_}++;\n if ($likes{$_} > $res_value) {\n $res_value = $likes{$_};\n $res_key = $_;\n }\n }\n print $res_key, \"\\n\";\n}\n\nexit 0;\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 6.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 02:23:29 PM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\n#my $sum = <>;\n#my @array = split ' ', <>;\n#@array = map {$array[$_]} 0..$sum-1;\n#my $max = {id => $array[0], count => 1};\n#my %likes;\n#for (0..$sum-1) {\n# my $id = $array[$_];\n# my $count = ++$likes{$id};\n# $max = {id => $id, count => $count} \n# if ($count > $max->{count});\n#}\n#print $max->{id}, \"\\n\";\n\nmy $sum = <>;\nmy @like_hist = split \" \", <>;\n@like_hist = map { $like_hist[$_] } 0..$sum-1;\n\nmy $counts = {};\nmy $max = {id => $like_hist[0], count => 1 };\n\nmy $k = 0;\nfor (0..$sum-1) {\n\tmy $id = $like_hist[$_];\n\tmy $count = ++$counts->{$id};\n\t$max = {id => $id, count => $count } if ($count > $max->{count});\n}\n\nprint $max->{id};\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my %likes;\n my $line;\n my $max;\n chomp($line = <>);\n my @array = split / /, $line;\n $max = {id => $array[0], count => 1};\n for (@array) {\n $likes{$_}++;\n if ($likes{$_} > $max->{count}) {\n $max = {id => $_, count => $likes{$_}};\n }\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my $likes;\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (@array) {\n $likes->{$_}++;\n if ($likes->{$_} > $max->{count}) {\n $max = {id => $_, count => $likes->{$_}};\n }\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use 5.020;\n\nwhile (<>) {\n my $num = $_;\n my %likes;\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (@array) {\n my $count = ++$likes{$_};\n $max = {id => $_, count => $count}\n if ($count > $max->{count});\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my $num = $_;\n my $likes = {};\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (0..$num-1) {\n my $id = $array[$_];\n $likes->{$id}++;\n my $count = $likes->{$id};\n if ($likes->{$id} > $max->{count}) {\n $max = {id => $id,\n count => $count};\n }\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my $num = $_;\n my $likes;\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (0..$num-1) {\n my $id = $array[$_];\n $likes->{$id}++;\n if ($likes->{$id} > $max->{count}) {\n $max = {id => $id,\n count => $likes->{$id}};\n }\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my $num = $_;\n my $likes = {};\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (0..$num-1) {\n my $id = $array[$_];\n my $count = ++$likes->{$id};\n if ($likes->{$id} > $max->{count}) {\n $max = {id => $id,\n count => $count};\n }\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nwhile (<>) {\n my $likes;\n my @array = split ' ', <>;\n my $max = {id => $array[0], count => 1};\n for (@array) {\n my $count = ++$likes->{$_};\n $max = {id => $_, count => $count}\n if ($count > $max->{count});\n }\n print $max->{id}, \"\\n\";\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 6.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 02:23:29 PM\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use 5.020;\n\n#my $sum = <>;\n#my @array = split ' ', <>;\n#@array = map {$array[$_]} 0..$sum-1;\n#my $max = {id => $array[0], count => 1};\n#my %likes;\n#for (0..$sum-1) {\n# my $id = $array[$_];\n# my $count = ++$likes{$id};\n# $max = {id => $id, count => $count} \n# if ($count > $max->{count});\n#}\n#print $max->{id}, \"\\n\";\n\nmy $sum = <>;\nmy @like_hist = split \" \", <>;\n@like_hist = map { $like_hist[$_] } 0..$sum-1;\n\nmy $counts = {};\nmy $max = {id => $like_hist[0], count => 1 };\n\nmy $k = 0;\nfor (0..$sum-1) {\n\tmy $id = $like_hist[$_];\n\tmy $count = ++$counts->{$id};\n\t$max = {id => $id, count => $count } if ($count > $max->{count});\n}\n\nprint $max->{id};\n"}, {"source_code": "<>;$_=<>;$m=$i=0;for$x(split){$y=++$c[$x];if($y>$m){$m=$y;$i=$x}}print$i\n"}], "negative_code": [{"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n chomp;\n my $num = $_;\n my ($res_key, $res_value) = (0, 0);\n my %likes;\n my $line = <>;\n chomp($line);\n my @array = split / /, $line;\n for (@array) {\n #print $_, \" \";\n $likes{$_}++;\n while (my ($key, $value) = each %likes) {\n next if $key == $res_key;\n if ($value > $res_value) {\n $res_value = $value;\n $res_key = $key;\n }\n }\n }\n print $res_key, \"\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n##!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\n#use 5.014;\n#use List::Util 'max';\n\n#while (<>) {\n$_ = <>;\n chomp;\n my $num = $_;\n my ($res_key, $res_value) = (0, 0);\n my %likes;\n my $line = <>;\n chomp($line);\n my @array = split / /, $line;\n for (@array) {\n #print $_, \" \";\n $likes{$_}++;\n while (my ($key, $value) = each %likes) {\n next if $key == $res_key;\n if ($value > $res_value) {\n $res_value = $value;\n $res_key = $key;\n }\n }\n }\n print $res_key, \"\\n\";\n#}\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\n#use 5.014;\n\n#while (<>) {\n$_ = <>;\n chomp;\n my $num = $_;\n my ($res_key, $res_value);\n my %likes;\n my $line = <>;\n chomp($line);\n my @array = split / /, $line;\n ($res_key, $res_value) = ($array[0], 1);\n for (@array) {\n #print $_, \" \";\n $likes{$_}++;\n while (my ($key, $value) = each %likes) {\n next if $key == $res_key;\n if ($value > $res_value) {\n $res_value = $value;\n $res_key = $key;\n }\n }\n }\n print $res_key, \"\\n\";\n#}\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n my %likes;\n my @array = split / /, <>;\n my ($res_key, $res_value) = ($array[0], 1);\n for (@array) {\n $likes{$_}++;\n if ($likes{$_} > $res_value) {\n $res_value = $likes{$_};\n $res_key = $_;\n }\n }\n print $res_key, \"\\n\";\n}\n\nexit 0;\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\n#use 5.014;\n\nwhile (<>) {\n chomp;\n my $num = $_;\n my ($res_key, $res_value) = (0, 0);\n my %likes;\n my $line = <>;\n chomp($line);\n my @array = split / /, $line;\n for (@array) {\n #print $_, \" \";\n $likes{$_}++;\n while (my ($key, $value) = each %likes) {\n next if $key == $res_key;\n if ($value > $res_value) {\n $res_value = $value;\n $res_key = $key;\n }\n }\n }\n print $res_key, \"\\n\";\n}\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 11:29:06 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.020;\n\nwhile (<>) {\n chomp;\n my $num = $_;\n my ($res_key, $res_value) = (0, 0);\n my %likes;\n my $line = <>;\n chomp($line);\n my @array = split / /, $line;\n for (@array) {\n #print $_, \" \";\n $likes{$_}++;\n while (my ($key, $value) = each %likes) {\n next if $key == $res_key;\n if ($value > $res_value) {\n $res_value = $value;\n $res_key = $key;\n }\n }\n }\n print $res_key, \"\\n\";\n}\n\nexit 0;\n"}], "src_uid": "e4a2354159fc4cab7088e016cf17ae6c"} {"nl": {"description": "Polycarp has an array $$$a$$$ consisting of $$$n$$$ integers.He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains $$$n-1$$$ elements). For each of the next moves he chooses any element with the only restriction: its parity should differ from the parity of the element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-... or odd-even-odd-even-...) of the removed elements. Polycarp stops if he can't make a move.Formally: If it is the first move, he chooses any element and deletes it; If it is the second or any next move: if the last deleted element was odd, Polycarp chooses any even element and deletes it; if the last deleted element was even, Polycarp chooses any odd element and deletes it. If after some move Polycarp cannot make a move, the game ends. Polycarp's goal is to minimize the sum of non-deleted elements of the array after end of the game. If Polycarp can delete the whole array, then the sum of non-deleted elements is zero.Help Polycarp find this value.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 2000$$$) \u2014 the number of elements of $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.", "output_spec": "Print one integer \u2014 the minimum possible sum of non-deleted elements of the array after end of the game.", "sample_inputs": ["5\n1 5 7 8 2", "6\n5 1 2 4 6 3", "2\n1000000 1000000"], "sample_outputs": ["0", "0", "1000000"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy @odd = grep $_ % 2, @_;\n\tmy @even = grep { not $_ % 2 } @_;\n\t\n\t$debug and print \"[@odd|@even]\";\n\t\n\tmy $min = ( sort { $a <=> $b } ~~ @odd, ~~ @even )[ 0 ];\n\t$debug and print \"[$min]\";\n\t\n\t@odd = sort { $a <=> $b } @odd;\n\t@even = sort { $a <=> $b } @even;\n\t\n\tfor( 1 .. $min + 1 ){\n\t\t@odd and pop @odd;\n\t\t@even and pop @even;\n\t\t}\n\t\n\tprint eval join '+', 0, 0, @odd, @even;\n\t}"}], "negative_code": [], "src_uid": "b80fed46a9e2356dad03dd3ec01523d4"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nmy $cnt = 1;\nmy $sum = 0;\nmy @sums;\n\nwhile( $sum < 1e9 ){\n\t$sum += $cnt * 2 + $cnt - 1;\n\tpush @sums, $sum;\n\t$cnt ++;\n\t}\n\n@sums = reverse @sums;\n\n$debug and print \"@sums\";\n\n$debug and print ~~ @sums;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $cnt = 0;\n\tmy $i = 0;\n\t\n\twhile( $i < @sums ){\n\t\tif( defined $sums[ $i + 30 ] and $sums[ $i + 30 ] > $_ ){\n\t\t\t$i += 30;\n\t\t\tnext;\n\t\t\t}\n\t\tif( $sums[ $i ] <= $_ ){\n\t\t\t$_ -= $sums[ $i ];\n\t\t\t$cnt ++;\n\t\t\tredo;\n\t\t\t}\n\t\t$i ++;\n\t\t}\n\t\n\tprint $cnt;\n\t}"}], "negative_code": [], "src_uid": "02062d1afe85e3639edddedceac304f4"} {"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 \u2014 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,\u2009h,\u2009v,\u2009e (1\u2009\u2264\u2009d,\u2009h,\u2009v,\u2009e\u2009\u2264\u2009104), where: d \u2014 the diameter of your cylindrical cup, h \u2014 the initial level of water in the cup, v \u2014 the speed of drinking process from the cup in milliliters per second, e \u2014 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 \u2014 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\u2009-\u20094. 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": "use strict;\nuse warnings;\n\nmy $pi = 3.1415926535897;\n\nsub solve {\n\tmy ($d, $h, $v, $e) = split(\" \", shift);\n\t$e = $e * $pi * ($d ** 2) / 4;\n\tif ($v <= $e) {\n\t\treturn \"NO\";\n\t}\n\treturn \"YES\\n\" . ($h * $pi * ($d ** 2) / 4) / ($v - $e);\n};\n\nmy $input = <>;\nprint solve($input);"}, {"source_code": "$PI = 3.141592653589793;\n($d, $h, $v, $e) = split ' ', <>;\n$now = $d ** 2 / 4 * $PI * $h;\n$up = $e * $d ** 2 / 4 * $PI;\nprint $up > $v ? \"NO\\n\" : \"YES\\n\" . $now / ($v - $up)"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\n\nmy $pi = 3.1415926535897;\n\nsub solve {\n\tmy ($d, $h, $v, $e) = split(\" \", shift);\n\t$e = $e * $pi * ($d ** 2) / 4;\n\tif ($v <= $e) {\n\t\treturn \"NO\";\n\t}\n\treturn ($h * $pi * ($d ** 2) / 4) / ($v - $e);\n};\n\nmy $input = <>;\nprint solve($input);"}], "src_uid": "fc37ef81bb36f3ac07ce2c4c3ec10d98"} {"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}$$$)\u00a0\u2014 the current volume and Bob's desired volume, respectively.", "output_spec": "For each test case, output a single integer\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B ) = sort { $a <=> $b } split;\n\t\n\tmy $cnt = 0;\n\t\n\twhile( $B - $A >= 5 ){\n\t\tmy $five = int +( $B - $A ) / 5;\n\t\t$cnt += $five;\n\t\t$B -= 5 * $five;\n\t\t}\n\t\n\t$cnt += do {\n\t\tif( $B - $A == 0 ){\n\t\t\t0;\n\t\t\t}\n\t\telsif( $B - $A == 1 ){\n\t\t\t1;\n\t\t\t}\n\t\telsif( $B - $A == 2 ){\n\t\t\t1;\n\t\t\t}\n\t\telsif( $B - $A == 3 ){\n\t\t\t2;\n\t\t\t}\n\t\telsif( $B - $A == 4 ){\n\t\t\t2;\n\t\t\t}\n\t\t};\n\t\n\tprint $cnt;\n\t}"}], "negative_code": [], "src_uid": "ccfe798f5dc63c492ff54cf40bb40613"} {"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\u2009+\u20091)-th painter (if j\u2009<\u2009n); 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,\u2009n (1\u2009\u2264\u2009m\u2009\u2264\u200950000,\u20091\u2009\u2264\u2009n\u2009\u2264\u20095), 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,\u2009ti2,\u2009...,\u2009tin (1\u2009\u2264\u2009tij\u2009\u2264\u20091000), 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,\u2009r2,\u2009...,\u2009rm, 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": "use strict;\nmy ($a,$c) = split (' ',<>);\nmy (@time,@sum);\nmy @b;\nfor (1..$a)\n{\n push (@b, [split(' ',<>)]);\n}\n\nmy $j =0;\nfor my $h (@b)\n{\nmy $i=0;\n for(@{$h})\n {\n $sum[$j][$i] = max($sum[$j][$i-1] +$_,$sum[$j - 1][$i] +$_);\n \n \n $i++;\n \n }\nprint $sum[$j][$i-1],\" \";\n $j++;\n \n}\n\n\nsub max {\n my ($max, @vars) = @_;\n for (@vars) {\n $max = $_ if $_ > $max;\n }\n return $max;\n}"}], "negative_code": [], "src_uid": "43f4ea06d8b8b0b6b6795a3d526c5a2d"} {"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\u2009-\u20091 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\u2009=\u20095, 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,\u20091,\u20092,\u2009...,\u2009n\u2009-\u20091. Write a program that determines whether the given puzzle is real or fake.", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of gears. The second line contains n digits a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009n\u2009-\u20091) \u2014 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": "use v5.10;\n\nchomp($n = <>);\n@c = (1 .. $n-1, 0);\n@c_ = ($n-1, 0 .. $n-2);\n@a = split / /, <>;\nforeach $i (0 .. $n-1) {\n\t$flag = 0;\n\t$a[$_]!=$_ and $flag=1 and last foreach (0 .. $n-1);\n\t$flag==0 and say \"Yes\" and exit;\n\tforeach (0 .. $n-1) {\n\t\tif ($_ & 1) {\n\t\t\t$a[$_] = $c_[$a[$_]];\n\t\t} else {\n\t\t\t$a[$_] = $c[$a[$_]];\n\t\t}\n\t}\n}\n$a[$_]!=$_ and say \"No\" and exit foreach (0 .. $n-1);\nsay \"Yes\";"}, {"source_code": "$\\ = $/;\n\nwhile($n = <>){\n\t@_ = split \" \", $a = <>;\n\t($_ += ($n - $a) % $n * (-1) ** $i ++) %= $n for @_;\n\tprint qw(Yes No)[ !(\"@_\" eq join \" \", 0 .. $n - 1) ]\n\t}"}], "negative_code": [], "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\u00a0\u2014 $$$6$$$\u00a0\u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nwhile( $t -- > 0 ){\n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @o = ( (1) x $n );\n print ( join(' ',@o) . \"\\n\" );\n}\n"}], "negative_code": [], "src_uid": "da2ac80c2ad6abae676d60a506eb05fc"} {"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$$$) \u2014 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": "#!/usr/bin/env perl\n\nuse 5.012;\n\nsub isPowerOfTwo\n{\n my $num = shift;\n my $l = log($num)/log(2);\n $l == int $l;\n}\n\nchomp(my $n = );\nchomp(my $line = );\nmy @nums = split ' ', $line;\n\nmy $i = 0;\nmy $deleted = 0;\nmy %pos;\nmy %numCount;\nfor my $i (0..$#nums) {\n ++$numCount{$nums[$i]};\n $pos{$nums[$i]} = $i;\n}\nmy @powers = map 2 ** $_, 0..31;\nNUM:for my $i (0..$#nums) {\n my $curNum = $nums[$i];\n for my $power (0..31) {\n my $t = $powers[$power];\n my $rest = $t - $curNum;\n my $count = $numCount{$rest};\n if ($count > 1 || ($count == 1 && $pos{$rest} != $i)) {\n next NUM;\n }\n }\n ++$deleted;\n}\n\nsay $deleted;\n\n"}], "negative_code": [], "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\u00a0\u2014 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\u00a0\u2014 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$$$)\u00a0\u2014 the number of vertical and horizontal spells. Each of the following $$$n$$$ lines contains one integer $$$x$$$ ($$$1 \\le x < 10^9$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $v, $h ) = split;\n\t\n\tmy @v = sort { $a <=> $b } map ~~<>, 1 .. $v;\n\tchomp @v;\n\t\n\tpush @v, 1e9 + 0;\n\t\n\tno warnings;\n\tmy @h = sort { $a <=> $b } map { ( split )[ 1 ] } grep $_ == 1, map ~~<>, 1 .. $h;\n\tuse warnings;\n\t\n\t$debug and print \" h: [@h]\";\n\t$debug and print \" v: [@v]\";\n\t\n\tmy $c_h = @h;\n\t\n\tmy @ans;\n\tmy $i = 0;\n\t\n\tfor my $v ( @v ){\n\t\t\n\t\tmy $cnt = 0;\n\t\t\n\t\twhile( @h ){\n\t\t\tmy $h = shift @h;\n\t\t\tif( $v <= $h ){\n\t\t\t\tunshift @h, $h;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t$cnt ++;\n\t\t\t}\n\t\t\n\t\tpush @ans, $c_h - $cnt + $i;\n\t\t\n\t\t$c_h -= $cnt;\n\t\t\n\t\t$i ++;\n\t\t}\n\t\n\t$debug and print \" ans: [@ans]\";\n\t\n\tprint 0 + ( sort { $a <=> $b } @ans )[ 0 ];\n\t}"}, {"source_code": "$_ = <>;\n\n@v = sort { $a <=> $b } 1e9, map ~~<>, 1 .. $_;\n\t\n$c = @h = sort { $a <=> $b } map { ( split )[ 1 ] } grep /^1 /, map ~~<>, 1 .. ( split )[ 1 ];\n\n$min = 1e9;\n\nfor( @v ){\n\t\n\twhile( @h ){\n\t\t$_ > $h[ 0 ] ? do { shift @h; $c -- }\n\t\t\t: last\n\t\t}\n\t\n\t$q = $c + $i ++;\n\t\n\t$min > $q and $min = $q;\n\t}\n\t\nprint $min"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $v, $h ) = split;\n\t\n\tmy @v = sort { $a <=> $b } map ~~<>, 1 .. $v;\n\tchomp @v;\n\t\n\tpush @v, 1e9 + 0;\n\t\n\tno warnings;\n\tmy @h = sort { $a <=> $b } map { ( split )[ 1 ] } grep $_ == 1, map ~~<>, 1 .. $h;\n\tuse warnings;\n\t\n\t$debug and print \" h: [@h]\";\n\t$debug and print \" v: [@v]\";\n\t\n\tmy $c_h = @h;\n\t\n\tmy @ans;\n\tmy $i = 0;\n\t\n\tfor my $v ( @v ){\n\t\t\n\t\tmy $cnt = 0;\n\t\t\n\t\twhile( @h ){\n\t\t\tmy $h = shift @h;\n\t\t\tif( $v <= $h ){\n\t\t\t\tunshift @h, $h;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t$cnt ++;\n\t\t\t}\n\t\t\n\t\tpush @ans, $c_h - $cnt + $i;\n\t\t\n\t\t$i ++;\n\t\t}\n\t\n\t$debug and print \" ans: [@ans]\";\n\t\n\tprint 0 + ( sort { $a <=> $b } @ans )[ 0 ];\n\t}"}], "src_uid": "00eb4442eb86ccc7352b63dc23354abf"} {"nl": {"description": "Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?", "input_spec": "The first line contains two integers n and k (1\u2009\u2264\u2009n\u2009\u2264\u2009105,\u20091\u2009\u2264\u2009k\u2009\u2264\u2009109)\u00a0\u2014 the number of share prices, and the amount of rubles some price decreases each second. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109)\u00a0\u2014 the initial prices.", "output_spec": "Print the only line containing the minimum number of seconds needed for prices to become equal, of \u00ab-1\u00bb if it is impossible.", "sample_inputs": ["3 3\n12 9 15", "2 2\n10 9", "4 1\n1 1000000000 1000000000 1000000000"], "sample_outputs": ["3", "-1", "2999999997"], "notes": "NoteConsider the first example. Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds.There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3.In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal.In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999\u2009*\u20093\u2009=\u20092999999997 seconds. We can note that this is the minimum possible time."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t@_ = split ' ', <>;\n\t\n\tmy $min = 10e9;\n\t$min > $_ and $min = $_ for @_;\n\t\n\tmy $f = 0;\n\tmy $sum = 0;\n\t\n\tfor (@_){\n\t\t( $_ - $min ) % $k and ++ $f and last;\n\t\t$sum += ( $_ - $min ) / $k;\n\t\t}\n\t\n\tprint $f ? -1 : $sum;\n\t\n\t}"}], "negative_code": [], "src_uid": "9e71b4117a24b906dbc16e6a6d110f50"} {"nl": {"description": "A sequence a0,\u2009a1,\u2009... is called a recurrent binary sequence, if each term ai (i\u2009=\u20090,\u20091,\u2009...) is equal to 0 or 1 and there exist coefficients such that an\u2009=\u2009c1\u00b7an\u2009-\u20091\u2009+\u2009c2\u00b7an\u2009-\u20092\u2009+\u2009...\u2009+\u2009ck\u00b7an\u2009-\u2009k (mod 2),\u2009 for all n\u2009\u2265\u2009k. Assume that not all of ci are zeros.Note that such a sequence can be uniquely recovered from any k-tuple {as,\u2009as\u2009+\u20091,\u2009...,\u2009as\u2009+\u2009k\u2009-\u20091} and so it is periodic. Moreover, if a k-tuple contains only zeros, then the sequence contains only zeros, so this case is not very interesting. Otherwise the minimal period of the sequence is not greater than 2k\u2009-\u20091, as k-tuple determines next element, and there are 2k\u2009-\u20091 non-zero k-tuples. Let us call a sequence long if its minimal period is exactly 2k\u2009-\u20091. Your task is to find a long sequence for a given k, if there is any.", "input_spec": "Input contains a single integer k (2\u2009\u2264\u2009k\u2009\u2264\u200950).", "output_spec": "If there is no long sequence for a given k, output \"-1\" (without quotes). Otherwise the first line of the output should contain k integer numbers: c1,\u2009c2,\u2009...,\u2009ck (coefficients). The second line should contain first k elements of the sequence: a0,\u2009a1,\u2009...,\u2009ak\u2009-\u20091. All of them (elements and coefficients) should be equal to 0 or 1, and at least one ci has to be equal to 1. If there are several solutions, output any.", "sample_inputs": ["2", "3"], "sample_outputs": ["1 1\n1 0", "0 1 1\n1 1 1"], "notes": "Note1. In the first sample: c1\u2009=\u20091, c2\u2009=\u20091, so an\u2009=\u2009an\u2009-\u20091\u2009+\u2009an\u2009-\u20092 (mod 2). Thus the sequence will be:so its period equals 3\u2009=\u200922\u2009-\u20091.2. In the second sample: c1\u2009=\u20090, c2\u2009=\u20091, c3\u2009=\u20091, so an\u2009=\u2009an\u2009-\u20092\u2009+\u2009an\u2009-\u20093 (mod 2). Thus our sequence is:and its period equals 7\u2009=\u200923\u2009-\u20091.Periods are colored."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\n=pod\n\nhttps://en.wikipedia.org/wiki/Linear_feedback_shift_register\nhttps://en.wikipedia.org/wiki/Maximum_length_sequence\nA necessary and sufficient condition for the sequence generated by a LFSR\nto be maximal length is that its corresponding polynomial be primitive.\n\n=cut\n\nmy @ans = (undef, );\nmy $k = ;\nmy @c = (0) x $k;\nfor my $i (split /,/, $ans[$k]) {\n $c[$i - 1] = 1;\n}\nprint \"@c\\n\";\nprint \"@c\\n\";\n\n__DATA__\n1\n2,1\n3,2\n4,3\n5,3\n6,5\n7,6\n8,6,5,4\n9,5\n10,7\n11,9\n12,6,4,1\n13,4,3,1\n14,5,3,1\n15,14\n16,15,13,4\n17,14\n18,11\n19,6,2,1\n20,17\n21,19\n22,21\n23,18\n24,23,22,17\n25,22\n26,6,2,1\n27,5,2,1\n28,25\n29,27\n30,6,4,1\n31,28\n32,22,2,1\n33,20\n34,27,2,1\n35,33\n36,25\n37,5,4,3,2,1\n38,6,5,1\n39,35\n40,38,21,19\n41,38\n42,41,20,19\n43,42,38,37\n44,43,18,17\n45,44,42,41\n46,45,26,25\n47,42\n48,47,21,20\n49,40\n50,49,24,23\n51,50,36,35\n52,49\n53,52,38,37\n54,53,18,17\n55,31\n56,55,35,34\n57,50\n58,39\n59,58,38,37\n60,59\n61,60,46,45\n62,61,6,5\n63,62\n64,63,61,60\n65,47\n66,65,57,56\n67,66,58,57\n68,59\n69,67,42,40\n70,69,55,54\n71,65\n72,66,25,19\n73,48\n74,73,59,58\n75,74,65,64\n76,75,41,40\n77,76,47,46\n78,77,59,58\n79,70\n80,79,43,42\n81,77\n82,79,47,44\n83,82,38,37\n84,71\n85,84,58,57\n86,85,74,73\n"}], "negative_code": [], "src_uid": "5d2e7f35583f4f3f087e5d010833c08d"} {"nl": {"description": "One day Nikita found the string containing letters \"a\" and \"b\" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters \"a\" and the 2-nd contains only letters \"b\".Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?", "input_spec": "The first line contains a non-empty string of length not greater than 5\u2009000 containing only lowercase English letters \"a\" and \"b\". ", "output_spec": "Print a single integer\u00a0\u2014 the maximum possible size of beautiful string Nikita can get.", "sample_inputs": ["abba", "bab"], "sample_outputs": ["4", "2"], "notes": "NoteIt the first sample the string is already beautiful.In the second sample he needs to delete one of \"b\" to make it beautiful."}, "positive_code": [{"source_code": "sub max {\n\tmy $a = shift;\n\tmy $b = (@_ == 1? shift: max(@_));\n\t$a > $b? $a: $b;\n}\n\n$_ = <>; chomp; @char = split //;\nfor $char (@char) {\n\tif ($char eq \"a\") {\n\t\t\t$aba = max($aba, $a, $ab) + 1;\n\t\t\t$a += 1;\n\t} else {\n\t\t$ab = max($a, $ab) + 1;\n\t}\n}\nprint max($a, $ab, $aba);"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tchomp;\n\t$debug and print \"[$_]\";\n\t\n\tmy $A = () = /a/g;\n\t\n\tmy $from_L = 0;\n\tmy $from_R = 0;\n\tmy $max_L = 0;\n\tmy $max_R = 0;\n\t\n\twhile( /./g ){\n\t\t$from_L += 1 - 2 * ( $& eq 'a' );\n\t\t$from_L < 0 and $from_L = 0;\n\t\t$max_L < $from_L and $max_L = $from_L;\n\t\t}\n\t\n\tmy $rev = reverse $_;\n\twhile( $rev =~ /./g ){\n\t\t$from_R += 1 - 2 * ( $& eq 'a' );\n\t\t$from_R < 0 and $from_R = 0;\n\t\t$max_R < $from_R and $max_R = $from_R;\n\t\t}\n\t\n\t$debug and print \"max_L: $max_L\";\n\t$debug and print \"max_R: $max_R\";\n\t\n\tprint $A + ( $max_L > $max_R ? $max_L : $max_R );\n\n\t$debug and print '-' x 20;\n\t}"}, {"source_code": "$_ = <>;\n\nfor( split //, $_ . 'a' x 5e3 . reverse ){\n\t\t( $L += $_ cmp 'ab' ) =~ s/-/0 /;\n\t\t$max < $L and $max = $L;\n\t\t}\n\nprint $max + ( () = /a/g )"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 1;\n\nwhile(<>){\n\tmy $max_b = 0;\n\t\n\t/(b*)(??{ $max_b < length $1 and $max_b = length $1; 'X' })/;\n\t\n\tmy $A = () = /a/g;\n\t\n\tprint $max_b + $A;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy $max = -~0;\n\t\t\n\t/(a*b*)(?=(a*)(??{ 0 and print \"$1.$2\"; $max < length( $1.$2 ) and $max = length( $1.$2 ); \"X\"}))/;\n\t\n\tprint $max;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 1;\n\nwhile(<>){\n\tchomp;\n\t\t\n\tmy $max_b = length( ( grep length, sort split /a+/ )[ 0 ] );\n\t\n\tmy $A = () = /a/g;\n\t\n\tprint $max_b + $A;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy $max = -~0;\n\t\t\n\t/(a*)(?=(b*a*)(??{ 0 and print \"$1.$2\"; $max < length( $1.$2 ) and $max = length( $1.$2 ); \"X\"}))/;\n\t\n\tprint $max;\n\t}"}], "src_uid": "c768f3e52e562ae1fd47502a60dbadfe"} {"nl": {"description": "This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions: Only segments already presented on the picture can be painted; The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment; The numbers of points from the beginning of the tail to the end should strictly increase. Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get.Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.", "input_spec": "First line of the input contains two integers n and m(2\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000, 1\u2009\u2264\u2009m\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the number of points and the number segments on the picture respectively. Then follow m lines, each containing two integers ui and vi (1\u2009\u2264\u2009ui,\u2009vi\u2009\u2264\u2009n, ui\u2009\u2260\u2009vi)\u00a0\u2014 the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.", "output_spec": "Print the maximum possible value of the hedgehog's beauty.", "sample_inputs": ["8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7", "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"], "sample_outputs": ["9", "12"], "notes": "NoteThe picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3\u00b73\u2009=\u20099."}, "positive_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m) = split;\n\tmy %h;\n\tmy %w;\n\t\n\tfor (1 .. $m){\n\t\t($c, $v) = split ' ', <>;\n\t\t$h{ $c } //= [];\n\t\tpush @{ $h{ $c } }, $v;\n\t\t$h{ $v } //= [];\n\t\tpush @{ $h{ $v } }, $c;\n\t\t}\n\t\n\tfor (1 .. $n){\n\t\t$w{ $_ } //= 1;\n\t\tfor my $i ( @{ $h{ $_ } } ){\n\t\t\t$w{ $i } //= 1;\n\t\t\t$i > $_ or next;\n\t\t\t$w{ $i } > $w{ $_ } or $w{ $i } = $w{ $_ } + 1;\n\t\t\t}\n\t\t}\n\t\t\n\t$max = 0;\n\tfor (1 .. $n){\n\t\t$mult = $w{ $_ } * @{ $h{ $_ } };\n\t\t$mult > $max and $max = $mult;\n\t\t}\n\t\n\tprint \n\t\t$max;\n\t}"}, {"source_code": "$n = <>;\n\nfor (<>){\n\t($c, $v) = split;\n\tpush @{ $h{ $c } }, $v;\n\tpush @{ $h{ $v } }, $c;\n\t}\n\t\nfor (1 .. $n){\n\tfor $i ( @{ $h{ $_ } } ){\n\t\t$i < $_ or $w{ $i } > $w{ $_ } or $w{ $i } = $w{ $_ } + 1;\n\t\t}\n\t}\n\nprint( (sort {$b <=> $a} map ++ $w{ $_ } * @{ $h{ $_ } }, 1 .. $n )[ 0 ] )"}], "negative_code": [], "src_uid": "2596db1dfc124ea9e14c3f13c389c8d2"} {"nl": {"description": "\u00abPolygon\u00bb is a system which allows to create programming tasks in a simple and professional way. When you add a test to the problem, the corresponding form asks you for the test index. As in most cases it is clear which index the next test will have, the system suggests the default value of the index. It is calculated as the smallest positive integer which is not used as an index for some previously added test.You are to implement this feature. Create a program which determines the default index of the next test, given the indexes of the previously added tests.", "input_spec": "The first line contains one integer n (1\u2009\u2264\u2009n\u2009\u2264\u20093000) \u2014 the amount of previously added tests. The second line contains n distinct integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20093000) \u2014 indexes of these tests.", "output_spec": "Output the required default value for the next test index.", "sample_inputs": ["3\n1 7 2"], "sample_outputs": ["3"], "notes": null}, "positive_code": [{"source_code": "<>;\n@line = split(' ', <>);\n$u[$_] = 1 foreach(@line);\nfor(1..3001){\n\tprint $_ and exit\n\tif !$u[$_];}\n\t"}], "negative_code": [], "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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\r\nmy ($t) = map {$_ - 0} split(/\\s/o,);\r\nwhile( $t -- > 0 ){\r\n my ($n,$k1,$k2) = map {$_ - 0} split(/\\s/o,);\r\n my ($w,$b) = map {$_ - 0} split(/\\s/o,);\r\n if( $k1 < $k2 ){\r\n my $tmp = $k1; $k1 = $k2; $k2 = $tmp;\r\n }\r\n if( $k2 + int(abs($k1-$k2)/2) >= $w and $n-$k1 + int(abs($k1-$k2)/2) >= $b ){\r\n print \"YES\\n\";\r\n } else {\r\n print \"NO\\n\";\r\n }\r\n}\r\n\r\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $k1, $k2 ) = split;\n\tmy( $W, $B ) = split ' ', <>;;\n\t\n\tif( ( $W < ( $k1 + $k2 ) / 2 or\n\t\t$W == ( $k1 + $k2 ) / 2 and ( $k1 + $k2 ) % 2 == 0 )\n\t\tand \n\t\t( $B < ( ( $n - $k1 ) + ( $n - $k2 ) ) / 2 or\n\t\t$B == ( ( $n - $k1 ) + ( $n - $k2 ) ) / 2 \n\t\tand ( ( $n - $k1 ) + ( $n - $k2 ) ) % 2 == 0 )\n\t\t){\n\t\tprint 'YES';\n\t\t}\n\telse{\n\t\tprint 'NO';\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "26354d2628f26eb2eff9432bd46400d5"} {"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 \u2009\u2264\u2009 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 number of cola cans. The second line contains n space-separated integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 volume of remaining cola in cans. The third line contains n space-separated integers that b1,\u2009b2,\u2009...,\u2009bn (ai\u2009\u2264\u2009bi\u2009\u2264\u2009109) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tprint( \n\t\t\t( eval join ' + ', split ' ', <> ) >\n\t\t\t( eval join ' + ', map $_ + 0, ( sort { $b <=> $a } split ' ', <> )[ 0, 1 ] )\n\t\t\t?\n\t\t\t\t\"NO\" : \"YES\"\n\t\t);\n\t}"}], "negative_code": [], "src_uid": "88390110e4955c521867864a6f3042a0"} {"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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\tif( $n <= $k ){\n\t\tprint 0 + ( int $k / $n ) + !!( $k % $n );\n\t\t}\n\telsif( $n % $k == 0 ){\n\t\tprint 1;\n\t\t}\n\telse{\n\t\tprint 2;\n\t\t}\n\t}"}, {"source_code": "use v5.20;\r\nuse integer;\r\nfor ( 1 .. <> ) {\r\n my ( $n, $k ) = split ' ', <>;\r\n say $n < $k ? ( $k + $n - 1 ) / $n : $n % $k ? 2 : 1;\r\n}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$k) = map { $_ - 0 } split(/\\s+/,);\r\n my $r;\r\n if( $n == $k ){\r\n $r = 1;\r\n } elsif( $n > $k ){\r\n if( $n % $k == 0 ){\r\n $r = 1;\r\n } else {\r\n $r = 2;\r\n }\r\n } else { # $n < $k\r\n $r = int(( $k + ( $n - 1 ) ) / $n);\r\n }\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $k ) = sort { $b <=> $a } split;\n\t\n\tprint 0 + ( int $n / $k ) + !!( $n % $k );\n\t}"}], "src_uid": "a28b84c9d1a54e322ab2d54bd5ab45c8"} {"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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @B = map { $_ - 0 } split(/\\s+/,);\r\n my $sm = 0;\r\n my $mx1 = -1;\r\n my $mx2 = -1;\r\n my %chk = ();\r\n for(my $i=0;$i<$n+2;$i++){\r\n my $v = $B[$i];\r\n $sm += $v;\r\n $chk{$v} ++;\r\n if( $v > $mx1 ){\r\n $mx2 = $mx1;\r\n $mx1 = $v;\r\n } elsif( $v > $mx2 ){\r\n $mx2 = $v;\r\n }\r\n }\r\n \r\n my $sm1 = $sm - $mx1;\r\n my $xg = $sm1 - $mx1;\r\n if( $xg >= 1 and exists($chk{$xg}) ){\r\n unless( $chk{$xg} == 1 and $xg == $mx1 ){\r\n my $u_xg = 1; my $u_mx1 = 1;\r\n my $sp = '';\r\n for(my $i=0;$i<$n+2;$i++){\r\n if( $u_xg and $B[$i]==$xg ){\r\n $u_xg = undef; next;\r\n }\r\n if( $u_mx1 and $B[$i]==$mx1 ){\r\n $u_mx1 = undef; next;\r\n }\r\n print \"$sp$B[$i]\";\r\n $sp = ' ';\r\n }\r\n print \"\\n\";\r\n next;\r\n }\r\n }\r\n \r\n my $sm1 = $sm - $mx2 - $mx1;\r\n my $xg = $mx1;\r\n if( $sm1 == $mx2 ){\r\n my $u_xg = 1; my $u_mx2 = 1;\r\n my $sp = '';\r\n for(my $i=0;$i<$n+2;$i++){\r\n if( $u_xg and $B[$i]==$xg ){\r\n $u_xg = undef; next;\r\n }\r\n if( $u_mx2 and $B[$i]==$mx2 ){\r\n $u_mx2 = undef; next;\r\n }\r\n print \"$sp$B[$i]\";\r\n $sp = ' ';\r\n }\r\n print \"\\n\";\r\n next;\r\n }\r\n print \"-1\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nOUTER:\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\t@_ = sort { $a <=> $b } @_;\n\t\n\tmy $sum = 0;\n\t\n\t$sum += $_[ $_ ] for 0 .. @_ - 3;\n\t\n\tif( $sum == $_[ @_ - 2 ] ){\n\t\tpop @_ for 1 .. 2;\n\t\tprint \"@_\";\n\t\t}\n\telse{\n\t\tfor my $i ( 0 .. @_ - 2 ){\n\t\t\tif( $_[ @_ - 1 ] == $sum + $_[ @_ - 2 ] - $_[ $i ] ){\n\t\t\t\tprint join ' ', map $_[ $_ ], grep { $_ != $i } 0 .. @_ - 2;\n\t\t\t\tnext OUTER;\n\t\t\t\t}\n\t\t\t}\n\t\tprint -1;\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "ce667a8fb60841c994aebedb35a3b0d8"} {"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,\u2009c is the number of pebbles of color c in the i-th pile. Then for any 1\u2009\u2264\u2009c\u2009\u2264\u2009k, 1\u2009\u2264\u2009i,\u2009j\u2009\u2264\u2009n the following condition must be satisfied |bi,\u2009c\u2009-\u2009bj,\u2009c|\u2009\u2264\u20091. It isn't necessary to use all k colors: if color c hasn't been used in pile i, then bi,\u2009c is considered to be zero.", "input_spec": "The first line of the input contains positive integers n and k (1\u2009\u2264\u2009n,\u2009k\u2009\u2264\u2009100), separated by a space \u2014 the number of piles and the number of colors respectively. The second line contains n positive integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009100) 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\u2009\u2264\u2009j\u2009\u2264\u2009ai) 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 perl\n\nuse strict;\nuse warnings;\nno warnings 'uninitialized';\n\nmy $line = <>;\nchomp $line;\nmy ($N, $K) = split(/ +/, $line);\n\n$line = <>;\nchomp $line;\nmy @A = split(/ +/, $line);\n\nmy @D;\nfor (my $i = 0; $i < $N; ++$i) {\n push(@D, []);\n}\n\nmy ($level, $col) = (0, 1);\nfor (my $t = 1; $col <= $K; ++$t) {\n my $new_col = 0;\n my $none_left = 1;\n for (my $i = 0; $i < $N; ++$i) {\n if ($A[$i] > 0) {\n $A[$i]--;\n push( @{$D[$i]}, $col);\n $none_left = 0 if ($A[$i] > 0);\n }\n else {\n $new_col = 1;\n # printf \"t = %d i = %d\\n\", $t, $i;\n }\n }\n last if $none_left;\n $col++ if $new_col;\n}\n\n#print $col, \"\\n\";\nif ($col > $K) {\n print \"NO\\n\"\n}\nelse {\n print \"YES\\n\";\n for (my $i = 0; $i < $N; ++$i) {\n for (my $j = 0; $j < @{$D[$i]}; ++$j) {\n print ' ' if $j > 0;\n print $D[$i][$j];\n }\n print \"\\n\"\n }\n}"}, {"source_code": "$\\ = $/;\n$, = $\";\n$_ = <>;\n@_ = ( 1 .. ( $k = (split)[ 1 ] ) ) x 1e2;\n$_ = <>;\nprint qw (NO YES)[ $f = $k >= eval join '-', (sort {$a <=> $b} split) [-1, 0] ];\nprint @_[0 .. $& - 1] while $f && /\\d+/g"}, {"source_code": "$\\ = $/;\n$, = $\";\nwhile(<>){\n\t(undef, $k) = split;\n\t@_ = (1 .. $k) x 1e2;\n\t$_ = <>;\n\t$f = $k < abs eval join '-', (sort {$a <=> $b} split)[-1, 0];\n\tprint qw(YES NO)[ $f ];\n\t$f && next;\n\tprint @_[0 .. $& - 1] while /\\d+/g;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nno warnings 'uninitialized';\n\nmy $line = <>;\nchomp $line;\nmy ($N, $K) = split(/ +/, $line);\n\n$line = <>;\nchomp $line;\nmy @A = split(/ +/, $line);\n\nmy @F = (1) x $N;\nmy @D;\nfor (my $i = 0; $i < $N; ++$i) {\n push(@D, []);\n}\n\nmy ($level, $col) = (0, 1);\nfor (my $t = 1; $col <= $K; ++$t) {\n my $new_col = 0;\n my $none_left = 1;\n for (my $i = 0; $i < $N; ++$i) {\n if ($A[$i] > 0) {\n $A[$i]--;\n push( @{$D[$i]}, $col);\n $none_left = 0;\n }\n else {\n if ($F[$i]) {\n $F[$i] = 0;\n $new_col = 1;\n # printf \"t = %d i = %d\\n\", $t, $i;\n }\n }\n }\n last if $none_left;\n $col++ if $new_col;\n}\n\n#print $col, \"\\n\";\nif ($col > $K) {\n print \"NO\\n\"\n}\nelse {\n print \"YES\\n\";\n for (my $i = 0; $i < $N; ++$i) {\n for (my $j = 0; $j < @{$D[$i]}; ++$j) {\n print ' ' if $j > 0;\n print $D[$i][$j];\n }\n print \"\\n\"\n }\n}"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nno warnings 'uninitialized';\n\nmy $line = <>;\nchomp $line;\nmy ($N, $K) = split(/ +/, $line);\n\n$line = <>;\nchomp $line;\nmy @A = split(/ +/, $line);\n\nmy @F = (1) x $N;\nmy @D;\nfor (my $i = 0; $i < $N; ++$i) {\n push(@D, []);\n}\n\nmy ($level, $col) = (0, 1);\nfor (my $t = 1; $col <= $K; ++$t) {\n my $new_col = 0;\n my $none_left = 1;\n for (my $i = 0; $i < $N; ++$i) {\n if ($A[$i] > 0) {\n $A[$i]--;\n push( @{$D[$i]}, $col);\n $none_left = 0 if $A[$i] > 0;\n }\n else {\n if ($F[$i]) {\n $F[$i] = 0;\n $new_col = 1;\n # printf \"t = %d i = %d\\n\", $t, $i;\n }\n }\n }\n last if $none_left;\n $col++ if $new_col;\n}\n\n#print $col, \"\\n\";\nif ($col > $K) {\n print \"NO\\n\"\n}\nelse {\n print \"YES\\n\";\n for (my $i = 0; $i < $N; ++$i) {\n for (my $j = 0; $j < @{$D[$i]}; ++$j) {\n print ' ' if $j > 0;\n print $D[$i][$j];\n }\n print \"\\n\"\n }\n}"}, {"source_code": "$\\ = $/;\nwhile(<>){\n\t(undef, $k) = split;\n\t@_ = (1 .. $k) x 1e2;\n\t$_ = <>;\n\t$f = $k < abs eval join '-', (sort {$a <=> $b} split)[-1, 0];\n\tprint qw(YES NO)[ $f ];\n\t$f && next;\n\tprint @_[0 .. $& - 1] while /\\d+/g;\n\t}"}], "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$$$)\u00a0\u2014 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$as += pos while /[02468]/g;\nprint 0 + $as;\n"}, {"source_code": "<>, $_ = <>;\n$as += pos while /[02468]/g;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nprint 0 + $as;\n"}, {"source_code": "<>, $_ = <>;\n$as += pos while /[02468]/g;\n\nprint 0 + $as;\n"}, {"source_code": "<>, $_ = <>;\n$ans += pos while /[02468]/g;\nprint 0 + $ans;\n"}, {"source_code": "<>, $_ = <>;\n$ans += pos while /[02468]/g;\n\nprint 0 + $ans;\n"}, {"source_code": "<>, $_ = <>;\n$an += pos while /[02468]/g;\nprint 0 + $an;\n"}, {"source_code": "<>, $_ = <>;\n\n$as += pos while /[02468]/g;\nprint 0 + $as;\n"}, {"source_code": "<>, $_ = <>;\n\n$ans += pos while /[02468]/g;\nprint 0 + $ans;\n"}, {"source_code": "\n\n<>, $_ = <>;\n$as += pos while /[02468]/g;\nprint 0 + $as;\n"}, {"source_code": "<>, $_ = <>;\n\n$an += pos while /[02468]/g;\nprint 0 + $an;\n"}, {"source_code": "<>, $_ = <>;\n$as += pos while /[02468]/g;\n\n\n\n\nprint 0 + $as;\n"}, {"source_code": "\n<>, $_ = <>;\n$as += pos while /[02468]/g;\nprint 0 + $as;\n"}, {"source_code": "<>, $_ = <>;\n$an += pos while /[02468]/g;\n\nprint 0 + $an;\n"}, {"source_code": "<>, $_ = <>;\n$as += pos while /[02468]/g;\n\n\n\n\n\n\n\n\nprint 0 + $as;\n"}, {"source_code": "<>, <> =~ /[02468](?{ $z += pos })(*F)/;\n\t\nprint 0 + $z"}, {"source_code": "<>, $_ = <>;\n\n$z += pos while /[02468]/g;\n\t\nprint 0 + $z"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\tmy $sum = 0;\n\t\n\t/\n\t\t[02468]\n\t\t(?{ $sum += pos })\n\t\t(*F)\n\t/x;\n\t\n\tprint $sum;\n\t}"}], "negative_code": [{"source_code": "<>, $_ = <>;\n$ans += pos while /[02468]/g;\nprint $ans;\n"}, {"source_code": "<>, <> =~ /[02468](?{ $z += pos })(*F)/;\n\t\nprint $z"}], "src_uid": "43a65d1cfe59931991b6aefae1ecd10e"} {"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.\u2009e. 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\u200b\u200b.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$2 \\le n \\le 3 \\cdot 10^5$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t/(.)(?=(.))(??{ $1 gt $2 ? do { print \"YES\\n\", pos, ' ', (pos) + 1 ; '' } : '(*F)' })/x or print 'NO';\n\t}"}], "negative_code": [], "src_uid": "d45f775613e701bbdaa4e457e6e189e2"} {"nl": {"description": "The R1 company has recently bought a high rise building in the centre of Moscow for its main office. It's time to decorate the new office, and the first thing to do is to write the company's slogan above the main entrance to the building.The slogan of the company consists of n characters, so the decorators hung a large banner, n meters wide and 1 meter high, divided into n equal squares. The first character of the slogan must be in the first square (the leftmost) of the poster, the second character must be in the second square, and so on.Of course, the R1 programmers want to write the slogan on the poster themselves. To do this, they have a large (and a very heavy) ladder which was put exactly opposite the k-th square of the poster. To draw the i-th character of the slogan on the poster, you need to climb the ladder, standing in front of the i-th square of the poster. This action (along with climbing up and down the ladder) takes one hour for a painter. The painter is not allowed to draw characters in the adjacent squares when the ladder is in front of the i-th square because the uncomfortable position of the ladder may make the characters untidy. Besides, the programmers can move the ladder. In one hour, they can move the ladder either a meter to the right or a meter to the left.Drawing characters and moving the ladder is very tiring, so the programmers want to finish the job in as little time as possible. Develop for them an optimal poster painting plan!", "input_spec": "The first line contains two integers, n and k (1\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of characters in the slogan and the initial position of the ladder, correspondingly. The next line contains the slogan as n characters written without spaces. Each character of the slogan is either a large English letter, or digit, or one of the characters: '.', '!', ',', '?'.", "output_spec": "In t lines, print the actions the programmers need to make. In the i-th line print: \"LEFT\" (without the quotes), if the i-th action was \"move the ladder to the left\"; \"RIGHT\" (without the quotes), if the i-th action was \"move the ladder to the right\"; \"PRINT x\" (without the quotes), if the i-th action was to \"go up the ladder, paint character x, go down the ladder\". The painting time (variable t) must be minimum possible. If there are multiple optimal painting plans, you can print any of them.", "sample_inputs": ["2 2\nR1", "2 1\nR1", "6 4\nGO?GO!"], "sample_outputs": ["PRINT 1\nLEFT\nPRINT R", "PRINT R\nRIGHT\nPRINT 1", "RIGHT\nRIGHT\nPRINT !\nLEFT\nPRINT O\nLEFT\nPRINT G\nLEFT\nPRINT ?\nLEFT\nPRINT O\nLEFT\nPRINT G"], "notes": "NoteNote that the ladder cannot be shifted by less than one meter. The ladder can only stand in front of some square of the poster. For example, you cannot shift a ladder by half a meter and position it between two squares. Then go up and paint the first character and the second character."}, "positive_code": [{"source_code": "($n, $k) = split /\\s+/, <>;\n$s = <>;\nchomp $s;\n$l = \"LEFT\\n\";\n$r = \"RIGHT\\n\";\nif($k - 1 > $n - $k){\n $k = $n - $k + 1;\n $s = reverse $s;\n my $t = $l;\n $l = $r;\n $r = $t;\n}\nprint $l x ($k - 1);\n@a = map {\"PRINT \".$_.\"\\n\"} split '', $s;\nprint shift @a;\nprint map {$r.$_} @a;\n"}, {"source_code": "use strict;\nuse POSIX;\nmy ($n,$k)=split(' ',<>);\n$_=<>;\nchomp;\nmy @l = split('',$_);\n$\\ = \"\\n\";\nunshift (@l,\"lal\");\nmy $adfs = 1 if $k == 1;\n\n\n\n\n\n\nif($k >= floor($#l/2)+1)\n{\n while($k<=$#l)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"RIGHT\" if $k++ <$#l;\n }\n $k--;\n \n goto exit if $adfs;\n \n \n while ($k>0)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n \n print \"LEFT\" if $k-- >1;\n }\n}\nelse \n{\n \n \n while ($k>0)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"LEFT\" if $k-- >1;\n }\n $k++;\n while($k<=$#l)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"RIGHT\" if $k++ <$#l;\n \n }\n}\nexit"}, {"source_code": "$t = <>;\n($n,$k) = split (/ /, $t);\n$n = $n + 0;\n$k = $k + 0;\n$s = <>;\nchomp ($s);\n@s = split (\"\", $s);\nif ($k > $n - $k)\n{\n\twhile ($k < $n)\n\t{\n\t\tprint \"RIGHT\\n\";\n\t\t$k = $k + 1;\n\t}\n\tprint \"PRINT $s[$n - 1]\\n\";\n\t$k = $k - 1;\n\twhile ($k > 0)\n\t{\n\t\t$k = $k - 1;\n\t\tprint \"LEFT\\n\";\n\t\tprint \"PRINT $s[$k]\\n\";\n\t}\n}\nelse\n{\n\twhile ($k > 1)\n\t{\n\t\tprint \"LEFT\\n\";\n\t\t$k = $k - 1;\n\t}\n\tprint \"PRINT $s[0]\\n\";\n\twhile ($k < $n)\n\t{\n\t\tprint \"RIGHT\\n\";\n\t\tprint \"PRINT $s[$k]\\n\";\n\t\t$k = $k + 1;\n\t}\n}\n"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$m)=split/ /;\n\t$_=<>;\n\tchomp;\n\t\n\tif ($n<$m*2){\n\t\tfor $i(1..($n-$m)){\n\t\t\tprint \"RIGHT\\n\"\n\t\t\t}\n\t\t$_=reverse $_;\n\t\ts/.// and print \"PRINT $&\\n\";\n\t\ts/./print \"LEFT\\nPRINT $&\\n\"/ge;\n\t\t\n\t\t}\n\telse {\n\t\tfor $i(1..($m-1)){\n\t\t\tprint \"LEFT\\n\"\n\t\t\t}\t\t\n\t\ts/.// and print \"PRINT $&\\n\";\n\t\ts/./print \"RIGHT\\nPRINT $&\\n\"/ge;\n\t\t\n\t\t\n\t\t}\n\t\n\t\n\t\n\t\n\t}"}], "negative_code": [{"source_code": "use strict;\nuse POSIX;\nmy ($n,$k)=split(' ',<>);\n$_=<>;\nchomp;\nmy @l = split('',$_);\n$\\ = \"\\n\";\nunshift (@l,\"lal\");\nmy $adfs = 1 if $k == 1;\n\n\n\n\n\n\nif($k>= floor($#l/2))\n{\n do\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"RIGHT\" if $k++ <$#l;\n }\n while ($k<=$#l);\n $k--;\n \n while ($k>0 and !$adfs)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n \n print \"LEFT\" if $k-- >1;\n }\n}\nelse \n{\n \n \n while ($k>0)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"LEFT\" if $k-- >1;\n }\n \n do{\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"RIGHT\" if $k++ <$#l;\n \n }while($k<$#l);\n}\nexit\n"}, {"source_code": "use strict;\nmy ($n,$k)=split(' ',<>);\n$_=<>;\nchomp;\nmy @l = split('',$_);\n$\\ = \"\\n\";\nunshift (@l,\"lal\");\nif($k>= ($#l/2))\n{\n while($k<$#l)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"RIGHT\" if $k++ <$#l;\n }\n \n while ($k>0)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n \n print \"LEFT\" if $k-- >1;\n }\n}\nelse \n{\n \n \n while ($k>0)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"LEFT\" if $k-- >1;\n }\n while($k<=$#l)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"RIGHT\" if $k++ <$#l;\n \n }\n}\n"}, {"source_code": "use strict;\nuse POSIX;\nmy ($n,$k)=split(' ',<>);\n$_=<>;\nchomp;\nmy @l = split('',$_);\n$\\ = \"\\n\";\nunshift (@l,\"lal\");\nmy $adfs = 1 if $k == 1;\n\n\n\n\n\n\nif($k>= floor($#l/2))\n{\n while($k<=$#l)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"RIGHT\" if $k++ <$#l;\n }\n $k--;\n \n goto exit if $adfs;\n \n \n while ($k>0)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n \n print \"LEFT\" if $k-- >1;\n }\n}\nelse \n{\n \n \n while ($k>0)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"LEFT\" if $k-- >1;\n }\n while($k<$#l)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"RIGHT\" if $k++ <$#l;\n \n }\n}\nexit\n"}, {"source_code": "use strict;\nuse POSIX;\nmy ($n,$k)=split(' ',<>);\n$_=<>;\nchomp;\nmy @l = split('',$_);\n$\\ = \"\\n\";\nunshift (@l,\"lal\");\nmy $adfs = 1 if $k == 1;\n\n\n\n\n\n\nif($k>= floor($#l/2))\n{\n while($k<=$#l)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"RIGHT\" if $k++ <$#l;\n }\n $k--;\n \n \n \n while ($k>0)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n \n print \"LEFT\" if $k-- >1;\n }\n}\nelse \n{\n \n \n while ($k>0)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"LEFT\" if $k-- >1;\n }\n while($k<$#l)\n {\n print \"PRINT $l[$k]\" if defined $l[$k];\n $l[$k] = undef;\n print \"RIGHT\" if $k++ <$#l;\n \n }\n}\n"}], "src_uid": "e3a03f3f01a77a1983121bab4218c39c"} {"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$$$)\u00a0\u2014 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\u00a0\u2014 the prices of chocolate bars in the store.", "output_spec": "For each test case print a single integer \u2014 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": "for (1..<>) {\r\n chomp (my $in = <>);\r\n my ($n, $l, $r, $k) = split / /, $in;\r\n $minprice = $l;\r\n $maxprice = $r;\r\n $total_cash = $k;\r\n chomp (my $in = <>);\r\n my @a = split / /, $in;\r\n\r\n @a = sort {$a <=> $b} @a;\r\n $t = 0;\r\n\r\n foreach (@a) {\r\n $_ < $minprice and next;\r\n $_ > $maxprice and last;\r\n $t++;\r\n $k -= $_;\r\n $k <= 0 and last;\r\n }\r\n $k < 0 and $t--;\r\n print $t, \"\\n\";\r\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $l, $r, $k ) = split;\n\t@_ = split ' ', <>;\n\t\n\t@_ = grep $_ <= $r, grep $_ >= $l, @_;\n\t\n\t@_ = sort { $a <=> $b } @_;\n\t\n\tmy $cnt = 0;\n\t\n\twhile( @_ and $k >= $_[ 0 ] ){\n\t\t$k -= shift @_;\n\t\t$cnt ++;\n\t\t}\n\t\n\tprint $cnt;\n\t}"}], "negative_code": [], "src_uid": "f577695d39a11e8507681f307677c883"} {"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$$$) \u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($a,$b) = map { $_ - 0 } split(/\\s+/,);\r\n if( $a < $b ){\r\n print \"1\\n\";\r\n next;\r\n }\r\n my $mn = 10**9;\r\n my $ans = 0;\r\n if( $b == 1 ){\r\n $b++; $ans ++;\r\n }\r\n for(my $bb = $b; $bb <= $b+35; $bb++){\r\n my $cnt = $ans + ($bb-$b);\r\n my $t = 1;\r\n while( $bb ** $t <= $a ){\r\n $t++;\r\n }\r\n my $v = $cnt + $t;\r\n $mn = $v if $v < $mn;\r\n }\r\n print \"$mn\\n\";\r\n \r\n}\r\n\r\nexit(0);\r\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B ) = split;\n\t\n\tmy $ans = 0;\n\t\n\t$B == 1 and do { $B ++; $ans ++; };\n\t\n\tmy @cand;\n\t\n\tfor my $i ( 0 .. 10 ){\n\t\tmy $B2 = $B + $i;\n\t\tmy $A2 = $A;\n\t\tmy $ans2 = $ans + $i;\n\t\t\n\t\twhile( $A2 >= 1 ){\n\t\t\t$A2 /= $B2;\n\t\t\t$A2 = int $A2;\n\t\t\t$ans2 ++;\n\t\t\t}\n\t\t\n\t\tpush @cand, $ans2;\n\t\t}\n\t\n\tprint +( sort { $a <=> $b } @cand )[ 0 ];\n\t}"}], "negative_code": [], "src_uid": "2b757fa66ce89046fe18cdfdeafa6660"} {"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 \u00abBersoft\u00bb 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 \u00ab@\u00bb.", "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": "$_=<>; chomp;\nif(/^(\\w+@\\w+)+$/) {\n#print \"|$`<$&>$'|\\n\";\n s/(\\w\\@\\w+)(?=\\w\\@)/$1,/g;\n print;\n} else {\n print \"No solution\";#and exit if(/\\@\\w?\\@/ or /^\\@|\\@$/ or $_!~/\\@/);\n}\n"}, {"source_code": "$_=<>;\nchomp;\nprint \"No solution\" and exit if(/\\@\\w?\\@/ or /^\\@|\\@$/ or $_!~/\\@/);\ns/(\\w\\@\\w+)(?=\\w\\@)/$1,/g;\nprint;\n\n"}, {"source_code": "$_=<>; chomp;\n/^(\\w+@\\w+)+$/ ? (s/(\\w\\@\\w+)(?=\\w\\@)/$1,/g , print) : print \"No solution\"\n"}, {"source_code": "$a = <>; chomp $a;\n$k = $a =~ y/@/@/;\nwhile ($a =~ /\\w+\\@\\w+(?!\\@)/g) {\n\tpush @a, $&;\n}\n# print \"@a\\n\";\n$b=join \"\", @a;\nif ($k==@a && $a eq $b) {\n\t$c = join \",\", @a;\n\tprint $c\n} else {\n\tprint \"No solution\"\n}"}, {"source_code": "$_=<>; chomp;\n/^(\\w+@\\w+)+$/ ? (s/(\\w\\@\\w+)(?=\\w\\@)/$1,/g , print) : print \"No solution\"\n"}, {"source_code": "my $list = ;\nif ($list =~ m/^[a-z]+@([a-z]{2,}@)*[a-z]+$/) {\n\twhile($list =~ s/(@[a-z])([a-z]+@)/$1,$2/g) {}\n\tprint $list;\n} else {\n\tprint \"No solution\\n\"\n}\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n#\t <>=~/ (\\d+) (\\d+) /;\n\t\nwhile(<>){\n\ts/(@\\w)(\\w+@)/$1,$2/g while /@(\\w)(\\w+)@/;\n\tprint /@\\w?@/||/^@/||/\\@$/||!/@/?\"No solution\\n\":\"$_\";\n\t}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [{"source_code": "$_=<>;\nchomp;\nprint \"No solution\" and exit if(/\\@\\w\\@/ or /^\\@|\\@$/);\ns/(\\w\\@\\w)(?=\\w)/$1,/g;\nprint;\n\n"}, {"source_code": "$_=<>; chomp;\n/^(\\w+@\\w+)+$/ ? s/(\\w\\@\\w+)(?=\\w\\@)/$1,/g || print : print \"No solution\"\n"}, {"source_code": "$_=<>; chomp;\n/^(\\w+@\\w+)+$/ ? s/(\\w\\@\\w+)(?=\\w\\@)/$1,/g && print : print \"No solution\"\n"}, {"source_code": "$_=<>;\nchomp;\nprint \"No solution\" and exit if(/\\@\\w\\@/ or /^\\@|\\@$/);\ns/(\\w\\@\\w+)(?=\\w@)/$1,/g;\nprint;\n\n"}, {"source_code": "$_=<>;\nchomp;\nprint \"NO solution\" and exit if(/\\@\\w\\@/ or /^\\@|\\@$/);\ns/(\\w\\@\\w)(?=\\w)/$1,/g;\nprint;\n\n"}, {"source_code": "$_=<>;\nchomp;\nprint \"No solution\" and exit if(/\\@\\w\\@/ or /^\\@|\\@$/ or $_!~/@/);\ns/(\\w\\@\\w+)(?=\\w@)/$1,/g;\nprint;\n\n"}, {"source_code": "$a = <>; chomp $a;\n$c = $a =~ y/@/@/;\nwhile ($a =~ /[^\\@]\\@[^\\@]/g) {\n\tpush @a, $&;\n}\n$b=join \"\", @a;\nif ($a eq $b) {\n\t$c = join \",\", @a;\n\tprint $c\n} else {\n\tprint \"No solution\"\n}"}, {"source_code": "$a = <>;\n$c = $a =~ y/@/@/;\nwhile ($a =~ /[^\\@]\\@[^\\@]/g) {\n\tpush @a, $&;\n}\n$b=join \"\", @a;\nif ($a eq $b) {\n\t$c = join \",\", @a;\n\tprint $c\n} else {\n\tprint \"No solution\"\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n#\t <>=~/ (\\d+) (\\d+) /;\n\t\nwhile(<>){\n\ts/(@\\w)(\\w+@)/$1,$2/g while /@(\\w)(\\w+)@/;\n\tprint /@\\w?@/||/^@/||/\\@$/?\"No solution\\n\":\"$_\";\n\t}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "src_uid": "71b4674e91e0bc5521c416cfc570a090"} {"nl": {"description": "Formula 1 officials decided to introduce new competition. Cars are replaced by space ships and number of points awarded can differ per race.Given the current ranking in the competition and points distribution for the next race, your task is to calculate the best possible ranking for a given astronaut after the next race. It's guaranteed that given astronaut will have unique number of points before the race.", "input_spec": "The first line contains two integer numbers $$$N$$$ ($$$1 \\leq N \\leq 200000$$$) representing number of F1 astronauts, and current position of astronaut $$$D$$$ ($$$1 \\leq D \\leq N$$$) you want to calculate best ranking for (no other competitor will have the same number of points before the race). The second line contains $$$N$$$ integer numbers $$$S_k$$$ ($$$0 \\leq S_k \\leq 10^8$$$, $$$k=1...N$$$), separated by a single space, representing current ranking of astronauts. Points are sorted in non-increasing order. The third line contains $$$N$$$ integer numbers $$$P_k$$$ ($$$0 \\leq P_k \\leq 10^8$$$, $$$k=1...N$$$), separated by a single space, representing point awards for the next race. Points are sorted in non-increasing order, so winner of the race gets the maximum number of points.", "output_spec": "Output contains one integer number \u2014 the best possible ranking for astronaut after the race. If multiple astronauts have the same score after the race, they all share the best ranking.", "sample_inputs": ["4 3\n50 30 20 10\n15 10 7 3"], "sample_outputs": ["2"], "notes": "NoteIf the third ranked astronaut wins the race, he will have 35 points. He cannot take the leading position, but he can overtake the second position if the second ranked astronaut finishes the race at the last position."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $d ) = split;\n\t\n\tmy @S = split ' ', <>;\n\t\n\tmy @P = split ' ', <>;\n\t\n\tmy $pts = $S[ $d - 1 ] + shift @P;\n\t$debug and print \" pts: $pts\";\n\t\n\tmy $pos = 0;\n\t\n\twhile( $S[ 0 ] + $P[ @P - 1 ] > $pts ){\n\t\t$debug and print \" $S[ 0 ] + $P[ @P - 1 ] > $pts\";\n\t\tshift @S;\n\t\tshift @P;\n\t\t$debug and print \" [@S]\";\n\t\t$debug and print \" [@P]\";\n\t\t}\n\t\n\tfor my $i ( $d - 0 .. $n - 1 ){\n\t\tpop @S;\n\t\tshift @P;\n\t\t$debug and print \" [@S]\";\n\t\t$debug and print \" [@P]\";\n\t\t}\n\t\n\tpop @S;\n\t\n\t$debug and print \" [@S]\";\n\t$debug and print \" [@P]\";\n\t\n\twhile( @S ){\n\t\tif( $S[ 0 ] + $P[ @P - 1 ] <= $pts ){\n\t\t\tshift @S;\n\t\t\tpop @P;\n\t\t\t$pos ++;\n\t\t\t}\n\t\telse{\n\t\t\tshift @S;\n\t\t\tshift @P;\n\t\t\t}\n\t\t$debug and print \" [@S]\";\n\t\t$debug and print \" [@P]\";\n\t\t}\n\t\n\tprint $d - $pos;\n\t\n\t$debug and print '-' x 10;\n\t}\n"}], "negative_code": [], "src_uid": "379133abc6fd643517542aabad79c57f"} {"nl": {"description": "A telephone number is a sequence of exactly $$$11$$$ digits such that its first digit is 8.Vasya and Petya are playing a game. Initially they have a string $$$s$$$ of length $$$n$$$ ($$$n$$$ is odd) consisting of digits. Vasya makes the first move, then players alternate turns. In one move the player must choose a character and erase it from the current string. For example, if the current string 1121, after the player's move it may be 112, 111 or 121. The game ends when the length of string $$$s$$$ becomes 11. If the resulting string is a telephone number, Vasya wins, otherwise Petya wins.You have to determine if Vasya has a winning strategy (that is, if Vasya can win the game no matter which characters Petya chooses during his moves).", "input_spec": "The first line contains one integer $$$n$$$ ($$$13 \\le n < 10^5$$$, $$$n$$$ is odd) \u2014 the length of string $$$s$$$. The second line contains the string $$$s$$$ ($$$|s| = n$$$) consisting only of decimal digits.", "output_spec": "If Vasya has a strategy that guarantees him victory, print YES. Otherwise print NO.", "sample_inputs": ["13\n8380011223344", "15\n807345619350641"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first example Vasya needs to erase the second character. Then Petya cannot erase a character from the remaining string 880011223344 so that it does not become a telephone number.In the second example after Vasya's turn Petya can erase one character character 8. The resulting string can't be a telephone number, because there is no digit 8 at all."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tchomp;\n\tmy $n = $_;\n\t\n\t$_ = <>, chomp;\n\t$debug and print;\n\t\n\tmy $del = $n - 11 >> 1;\n\t$debug and print $del;\n\t\n\tmy $cnt = 0;\n\t\n\ts/8(??{ $cnt ++; $cnt > $del ? '(*F)' : '' })//g;\n\t\n\t$debug and print;\n\t\n\t$cnt = 0;\n\t\n\ts/[^8](??{ $cnt ++; $cnt > $del ? '(*F)' : '' })//g;\n\t\n\t$debug and print;\n\t\n\tprint /^8/ ? 'YES' : 'NO';\n\t}"}], "negative_code": [], "src_uid": "99f37936b243907bf4ac1822dc547a61"} {"nl": {"description": "You are given an array $$$a_1, a_2, \\dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 5000$$$)\u00a0\u2014 the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\le n \\le 5000$$$; $$$0 \\le x \\le 10^5$$$)\u00a0\u2014 the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$-10^5 \\le a_i \\le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.", "output_spec": "For each testcase, print $$$n + 1$$$ integers\u00a0\u2014 the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.", "sample_inputs": ["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"], "sample_outputs": ["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"], "notes": "NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \\cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\n# array\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $x ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy @cum;\n\t\n\tpush @cum, ( sort { $b <=> $a } @_ )[ 0 ];\n\t\n\tmy @sums = @_;\n\t\n\tfor my $i ( 1 .. $n - 1 ){\n\t\t\n\t\tmy $max = -~0;\n\t\tfor my $j ( $i .. $n - 1 ){\n\t\t\t( $sums[ $j - $i ] += $_[ $j ] ) > $max and $max = $sums[ $j - $i ];\n\t\t\t}\n\t\tpush @cum, $max;\n\t\t}\n\t\n\tmy $cum = sub {\n\t\tfor my $i ( 1 .. @{ $_[ 0 ] } - 1 ){\n\t\t\t$_[ 0 ][ $i ] = $_[ 0 ][ $i ] > $_[ 0 ][ $i - 1 ] ? \n\t\t\t\t$_[ 0 ][ $i ] : $_[ 0 ][ $i - 1 ];\n\t\t\t}\n\t\t};\n\t\n\t@cum = reverse @cum;\n\t$cum->( \\@cum );\n\t@cum = reverse @cum;\n\t\n\tunshift @cum, $cum[ 0 ];\n\t\n\tfor my $i ( 1 .. @cum - 1 ){\n\t\t$cum[ $i ] += $x * $i;\n\t\t}\n\t\n\t$cum[ 0 ] < 0 and $cum[ 0 ] = 0;\n\t\n\t$cum->( \\@cum );\n\t\n\tprint \"@cum\";\n\t}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $x ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy @cum;\n\t\n\tpush @cum, ( sort { $b <=> $a } @_ )[ 0 ];\n\t\n\tmy @sums = @_;\n\t\n\tfor my $i ( 1 .. $n - 1 ){\n\t\t\n\t\tmy $max = -~0;\n\t\tfor my $j ( $i .. $n - 1 ){\n\t\t\t( $sums[ $j - $i ] += $_[ $j ] ) > $max and $max = $sums[ $j - $i ];\n\t\t\t}\n\t\tpush @cum, $max;\n\t\t}\n\t\n\tfor my $i ( reverse 0 .. @cum - 2 ){\n\t\t$cum[ $i ] = $cum[ $i ] > $cum[ $i + 1 ] ? \n\t\t\t$cum[ $i ] : $cum[ $i + 1 ];\n\t\t}\n\t\n\tunshift @cum, $cum[ 0 ];\n\t\n\tfor my $i ( 1 .. @cum - 1 ){\n\t\t$cum[ $i ] += $x * $i;\n\t\t}\n\t\n\t$cum[ 0 ] < 0 and $cum[ 0 ] = 0;\n\t\n\tfor my $i ( 1 .. @cum - 1 ){\n\t\t$cum[ $i ] = $cum[ $i ] > $cum[ $i - 1 ] ? \n\t\t\t$cum[ $i ] : $cum[ $i - 1 ];\n\t\t}\n\t\n\tprint \"@cum\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $x ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy @max;\n\t\n\tpush @max, ( sort { $b <=> $a } @_ )[ 0 ];\n\t\n\tmy @sums = @_;\n\t\n\tfor my $i ( 1 .. $n - 1 ){\n\t\tshift @_;\n\t\tpop @sums;\n\t\t\n\t\tmy $max = -1_000_000_000;\n\t\tfor my $j ( 0 .. @sums - 1 ){\n\t\t\t$sums[ $j ] += $_[ $j ]; \n\t\t\t$sums[ $j ] > $max and $max = $sums[ $j ];\n\t\t\t}\n\t\tpush @max, $max;\n\t\t}\n\t\n\tmy @cum = @max;\n\t\n\tfor my $i ( reverse 0 .. @cum - 2 ){\n\t\t$cum[ $i ] = $cum[ $i ] > $cum[ $i + 1 ] ? \n\t\t\t$cum[ $i ] : $cum[ $i + 1 ];\n\t\t}\n\t\n\tunshift @cum, $cum[ 0 ];\n\t\n\tfor my $i ( 1 .. @cum - 1 ){\n\t\t$cum[ $i ] += $x * $i;\n\t\t}\n\t\n\t$cum[ 0 ] < 0 and $cum[ 0 ] = 0;\n\t\n\tfor my $i ( 1 .. @cum - 1 ){\n\t\t$cum[ $i ] = $cum[ $i ] > $cum[ $i - 1 ] ? \n\t\t\t$cum[ $i ] : $cum[ $i - 1 ];\n\t\t}\n\t\n\tprint \"@cum\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $x ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy @max;\n\t\n\tpush @max, ( sort { $b <=> $a } @_ )[ 0 ];\n\t\n\tmy @sums = @_;\n\t\n\tfor my $i ( 1 .. $n - 1 ){\n\t\tshift @_;\n\t\tpop @sums;\n\t\t\n\t\tmy $max = -~0;\n\t\tfor my $j ( 0 .. @sums - 1 ){\n\t\t\t$sums[ $j ] += $_[ $j ]; \n\t\t\t$sums[ $j ] > $max and $max = $sums[ $j ];\n\t\t\t}\n\t\tpush @max, $max;\n\t\t}\n\t\n\tmy @cum = @max;\n\t\n\tfor my $i ( reverse 0 .. @cum - 2 ){\n\t\t$cum[ $i ] = $cum[ $i ] > $cum[ $i + 1 ] ? \n\t\t\t$cum[ $i ] : $cum[ $i + 1 ];\n\t\t}\n\t\n#\tprint \"@cum\";\n\t\n\tunshift @cum, $cum[ 0 ];\n\t\n\tfor my $i ( 1 .. @cum - 1 ){\n\t\t$cum[ $i ] += $x * $i;\n\t\t}\n\t\n\t$cum[ 0 ] < 0 and $cum[ 0 ] = 0;\n\t\n#\tprint \"@cum\";\n\t\n\tfor my $i ( 1 .. @cum - 1 ){\n\t\t$cum[ $i ] = $cum[ $i ] > $cum[ $i - 1 ] ? \n\t\t\t$cum[ $i ] : $cum[ $i - 1 ];\n\t\t}\n\t\n\tprint \"@cum\";\n\t\n#\tmy @ans;\n\t\n#\tmy $max = 0;\n#\t$max < $_ and $max = $_ for @max;\n\t\n#\tpush @ans, $max;\n\t\n#\tfor my $i ( 1 .. $n ){\n#\t\tmy $max = $ans[ @ans - 1 ];\n\t\t\n#\t\tfor my $j ( $i - 1 .. $n - 1 ){\n#\t\t\t$max[ $j ] += $x;\n\t\t\t\n#\t\t\t$max < $max[ $j ] and $max = $max[ $j ];\n#\t\t\t}\n\t\t\n#\t\tpush @ans, $max;\n#\t\t}\n\t\n#\tprint \"@ans\";\n\t}"}], "negative_code": [], "src_uid": "a5927e1883fbd5e5098a8454f6f6631f"} {"nl": {"description": "One day, Twilight Sparkle is interested in how to sort a sequence of integers a1,\u2009a2,\u2009...,\u2009an 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,\u2009a2,\u2009...,\u2009an\u2009\u2192\u2009an,\u2009a1,\u2009a2,\u2009...,\u2009an\u2009-\u20091. 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\u2009\u2264\u2009n\u2009\u2264\u2009105). The second line contains n integer numbers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009105).", "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": "chomp($n=<>);\nchomp($c=<>);\n@a = split(\" \", $c);\nuse List::Util qw(min);\n$v = min @a;\n$i = $#a;\n--$i until $a[$i] == $v;\n$i-- while $i && $a[$i - 1] == $v;\nsplice @a, 0, $n, @a[$i..$#a], @a[0..($i-1)] if $i;\nfor(1..$#a){ $bad = 1 if $a[$_] < $a[$_-1] }\n$i = $n if $i == 0;\nprint $bad ? -1 : $n - $i; \n"}, {"source_code": "$n = <>;\n@a = split(\" \", <>);\nuse List::Util qw(min);\n$v = min @a;\n$i = $#a;\n--$i until $a[$i] == $v;\n$i-- while $i && $a[$i - 1] == $v;\nsplice @a, 0, $n, @a[$i..$#a], @a[0..($i-1)] if $i;\nfor(1..$#a){ $bad = 1 if $a[$_] < $a[$_-1] }\n$i = $n if $i == 0;\nprint $bad ? -1 : $n - $i; \n"}, {"source_code": "$n = <>;\n@a = split(\" \", <>);\nuse List::Util qw(min);\n$i = $#a;\n$v = min @a;\n--$i until $a[$i] == $v;\n$i-- while $i && $a[$i - 1] == $v;\nsplice @a, 0, $n, @a[$i..$#a], @a[0..($i-1)] if $i;\nfor(1..$#a){ $bad = 1 if $a[$_] < $a[$_-1] }\n$i = $n if $i == 0;\nprint $bad ? -1 : $n - $i; \n"}, {"source_code": "chomp($n=<>);\nchomp($c=<>);\n@a = split(\" \", $c);\nuse List::Util qw(min);\n$v = min @a;\n$i = $#a;\n--$i until $a[$i] == $v;\nwhile($i && $a[$i-1] == $v){$i--};\nsplice @a, 0, $n, @a[$i..$#a], @a[0..($i-1)] if $i;\nfor(1..$#a){\n $ok = 1 if $a[$_] < $a[$_-1];\n}\nprint -1 if $ok;\n$i = $n if $i == 0;\nprint ($n - $i) if !$ok;\n#print \"\\n@a k=$ok i=$i\\n\";\n"}, {"source_code": "$n=<>;\n@_=split/ /,<>;\n$k=0;\nfor $i(1..($n-1)){\n\nif ($_[$i-1]>$_[$i] ){\n$k++;\n$ii=$i;\n}\n\n}\n\nif ($k==0){\nprint 0\n}\nelsif ($k>1 or $_[0]<$_[$n-1]){\nprint -1\n}\nelse {\nprint $n-$ii\n}"}, {"source_code": "<>;\n($a,@_)=split/ /,<>;\n$A=$a;\n\nfor (@_){\n\t$k++, $ii=@_-$i if $a>$_;\n\t$i++;\n\t$a=$_\n}\n\nprint $k>1 || $A<$_[@_-1] && $k? -1:0+$ii"}], "negative_code": [{"source_code": "chomp($n=<>);\nchomp($c=<>);\n@a = split(\" \", $c);\nuse List::Util qw(min);\n$v = min @a;\n$i = $#a;\n--$i until $a[$i] == $v;\nsplice @a, 0, $n, @a[$i..$#a], @a[0..($i-1)];\nfor(1..$#a){\n $ok = 1 if $a[$_] < $a[$_-1];\n}\nprint -1 if $ok;\n$i = $n if $i == 0;\nprint ($n - $i) if !$ok;\n"}, {"source_code": "chomp($n=<>);\nchomp($c=<>);\n@a = split(\" \", $c);\nuse List::Util qw(min);\n$v = min @a;\n++$i until $a[$i] == $v;\nsplice @a, 0, $n, @a[$i..$#a], @a[0..($i-1)];\nfor(1..$#a){\n $ok = 1 if $a[$_] < $a[$_-1];\n}\nprint -1 if $ok;\n$i = $n if $i == 0;\nprint ($n - $i) if !$ok;\n"}, {"source_code": "chomp($n=<>);\nchomp($c=<>);\n@a = split(\" \", $c);\nuse List::Util qw(min);\n$v = min @a;\n$i = $#a;\n\n--$i until $a[$i] == $v;\n--$i until $a[$i] != $v;\n$i++;\nsplice @a, 0, $n, @a[$i..$#a], @a[0..($i-1)];\nfor(1..$#a){\n $ok = 1 if $a[$_] < $a[$_-1];\n}\nprint -1 if $ok;\n$i = $n if $i == 0;\nprint ($n - $i) if !$ok;\n"}, {"source_code": "chomp($n=<>);\nchomp($c=<>);\n@a = split(\" \", $c);\nuse List::Util qw(min);\n$v = min @a;\n$i = $#a;\n\n--$i until $a[$i] == $v;\n--$i until $i == 0 || $a[$i] != $v;\n$i++;\nsplice @a, 0, $n, @a[$i..$#a], @a[0..($i-1)];\nfor(1..$#a){\n $ok = 1 if $a[$_] < $a[$_-1];\n}\nprint -1 if $ok;\n$i = $n if $i == 0;\nprint ($n - $i) if !$ok;\n"}, {"source_code": "<>;\n($A=$a,@_)=split/ /,<>;\n\nfor (@_){\n\t$k++, $ii=@_-$i if $a>$_;\n\t$i++;\n\t$a=$_\n}\n\nprint $k>1 || $A<$_[@_-1] && $k? -1:0+$ii"}, {"source_code": "$n=<>;\n@_=split/ /,<>;\n$k=0;\nfor $i(1..($n-1)){\n\nif ($_[$i-1]>$_[$i] ){\n$k++;\n$ii=$i;\n}\n\n}\n\nif ($k==0){\nprint 0\n}\nelsif ($k>1){\nprint -1\n}\nelse {\nprint $ii\n}"}, {"source_code": "$n=<>;\n@_=split/ /,<>;\n$k=0;\nfor $i(1..($n-1)){\n\nif ($_[$i-1]>$_[$i] ){\n$k++;\n$ii=$i;\n}\n\n}\n\nif ($k==0){\nprint 0\n}\nelsif ($k>1 or $_[0]<$_[$n-1]){\nprint -1\n}\nelse {\nprint $ii\n}"}], "src_uid": "c647e36495fb931ac72702a12c6bfe58"} {"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\u2009\u2264\u2009n\u2009\u2264\u20091000,\u20090\u2009\u2264\u2009m\u2009\u2264\u20091000) 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\u2009\u2264\u2009i\u2009\u2264\u2009n). The hints may coincide.", "output_spec": "The answer should contain exactly one integer \u2014 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) = split (\" \", );\n\n$M = \"1\" x $n;\n\nfor () {\n\t/To the (left|right) of (\\d+)/ or die;\n\t($direction, $pos) = ($1, $2);\n\tif ($direction eq \"left\") {\n\t\t$left = 1; $right = 0;\n\t} else {\n\t\t$left = 0; $right = 1;\n\t}\n\t$m = ($left x ($pos - 1)).\"0\".($right x ($n - $pos));\n\t$M = $M & $m;\n}\n\n$count = $M =~ y/1//;\n$count = -1 if $count == 0;\nprint $count;"}], "negative_code": [], "src_uid": "dabeb9852332f6a6e730acab7fe61a5c"} {"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$$$)\u00a0\u2014 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": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in = *STDIN;\n # open(my $in, '<:encoding(UTF-8)', *STDIN);\n # open(my $out, '>:encoding(UTF-8)', './out.txt');\n\n my $r= <$in>; chomp($r);\n my $n= $r;\n\n $r= <$in>;\n my @d= split(' ', $r);\n\n my $dl= 0;\n my $dr= 0;\n\n for my $door (@d) {\n ($door eq 1) ? $dr++ : $dl++;\n }\n\n my $lc= 0;\n my $rc= 0;\n my $c= 0;\n for my $door (@d) {\n ($door eq 1) ? $rc++ : $lc++;\n $c++;\n\n if($rc >= $dr || $lc >= $dl) {\n print $c;\n # print $out $c;\n return;\n }\n }\n\n\n}\n\nmain() unless caller();\n"}, {"source_code": "use strict;\n#open STDIN, 'output.txt';\nmy $n = <>;\nmy @a = split(' ', <>);\nmy $buf1 = 0;\nmy $buf2 = 0;\nsub solve{\n my ($a, $n) = @_;\nfor(my $i = 0; $i < $n; $i++){\n if($a[$i] == 0){\n $buf1++;\n }\n else{\n $buf2++;\n }\n}\nmy $ans = 0;\nmy $i = 0;\nwhile($buf1 > 0 && $buf2 > 0){\n if($a[$i] == 0){\n $buf1--;\n $ans++;\n }\n else{\n $buf2--;\n $ans++;\n }\n $i++;\n}\nprint $ans;\n}\nsolve(\\@a, $n);\n"}, {"source_code": "use strict;\n#open STDIN, 'output.txt';\nmy $n = <>;\nmy @a = split(' ', <>);\nmy $buf1 = 0;\nmy $buf2 = 0;\nfor(my $i = 0; $i < $n; $i++){\n if($a[$i] == 0){\n $buf1++;\n }\n else{\n $buf2++;\n }\n}\nmy $ans = 0;\nmy $i = 0;\nwhile($buf1 > 0 && $buf2 > 0){\n if($a[$i] == 0){\n $buf1--;\n $ans++;\n }\n else{\n $buf2--;\n $ans++;\n }\n $i++;\n}\nprint $ans;\n"}, {"source_code": "use strict;\n#open STDIN, 'output.txt';\nmy $n = <>;\nmy @a = split(' ', <>);\nmy %h;\nfor(my $i = 0; $i < $n; $i++){\n $h{$a[$i]}++;\n}\nmy $ans = 0;\nfor(my $i = 0; $i < $n; $i++){\n $h{$a[$i]}--;\n $ans++;\n if($h{$a[$i]} == 0){\n print $ans;\n exit(0);\n }\n}\nprint $ans;\n"}, {"source_code": "<>;\n\n$A = <> =~ s/\\s//gr;\n\nprint 1 + ( sort { $a <=> $b } map { rindex $A, $_ } 0, 1 )[ 0 ];"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\ty/ //d;\n\t\n\tmy $len = length;\n\t\n\t$_ = reverse;\n\t\n\tprint /^0++(?=1)|^1++(?=0)/ ? $len - length $& : 0;\n\t}"}], "negative_code": [{"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n open(my $in, '<:encoding(UTF-8)', './in.txt');\n # open(my $out, '>:encoding(UTF-8)', './out.txt');\n\n my $r= <$in>; chomp($r);\n my $n= $r;\n\n $r= <$in>;\n my @d= split(' ', $r);\n\n my $dl= 0;\n my $dr= 0;\n\n for my $door (@d) {\n ($door eq 1) ? $dr++ : $dl++;\n }\n\n my $lc= 0;\n my $rc= 0;\n my $c= 0;\n for my $door (@d) {\n ($door eq 1) ? $rc++ : $lc++;\n $c++;\n\n if($rc >= $dr || $lc >= $dl) {\n print $c;\n # print $out $c;\n return;\n }\n }\n\n\n}\n\nmain() unless caller();\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\ty/ //d;\n\t\n\tmy $len = length;\n\t\n\t$_ = reverse;\n\t\n\tprint /^(.)\\1*+\\B/ ? $len - length $& : 0;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\ty/ //d;\n\t\n\tmy $len = length;\n\t\n\t$_ = reverse;\n\t\n\t/(.)\\1*/;\n\t\n\tprint $len - length $&;\n\t}"}], "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": "\n$,=\"\";\nmy $f=<>;\nchomp($f);\nmy ($h,$m)=split /\\:/,<>;\nchomp($h,$m);\nif($f==12)\n{\n if($h>12 or $h eq \"00\")\n {\n if(substr($h,1,1) eq \"0\")\n {\n $h=\"10\";\n }\n else\n {\n $h=\"0\".substr($h,1,1);\n }\n }\n}\nif($f==24)\n{\n if($h>23)\n {\n {\n $h=\"0\".substr($h,1,1);\n }\n }\n}\nif($m>59)\n{\n$m=\"0\".substr($m,1,1);\n}\n\nprint $h,':',$m,\"\\n\";\n"}, {"source_code": "\nmy $base = <>;\nmy $time = <>;\n$time =~ /(\\d)(\\d)\\:(\\d)(\\d)/;\nmy $H = $1;\nmy $h = $2;\nmy $M = $3;\nmy $m = $4;\n\nif ($base =~ /12/) {\n\tif ($h eq '0') {\n\t\t$H = '1';\n\t}\n\telsif ($h eq '1' or $h eq '2'){\n\t\tif ($H ne '1') {\n\t\t\t$H = '0';\n\t\t}\n\t}\n\telse {\n\t\t$H = '0';\n\t}\n}\nelse {\n\tif ($h eq '0' or $h eq '1' or $h eq '2' or $h eq '3') {\n\t\tif ($H ne '1' and $H ne '2' ) {\n\t\t\t$H = '0';\n\t\t}\n\t}\n\telse {\n\t\tif ($H ne '1') {\n\t\t\t$H = '0';\n\t\t}\n\t}\n}\n\nif ($M >= 6) {\n\t$M = 0;\n}\n\n\nprint \"$H$h:$M$m\\n\";\n\n"}, {"source_code": "$format = <>; @time = split /:/, <>; \nwhile($time[1] > 59) { $time[1] -= 10; }; \nif ($format == 12) { if($time[0] > $format) { while($time[0] > $format) { $time[0] -= 10; }; if (($time[0] >= 0) && ($time[0] < 10)) { substr($time[0], 0, 0, '0'); }; }; if($time[0] == '00') { $time[0] = '01'; }; } \nelse { if($time[0] >= $format) { while($time[0] >= $format) { $time[0] -= 10; }; if (($time[0] >= 0) && ($time[0] < 10)) { substr($time[0], 0, 0, '0'); }; }; };\nprint $time[0], ':', $time[1];"}, {"source_code": "print +( <> =~ /^2/ + 0 * ($_ = <>) ?\n\t\ts/^[3-9]/0/r =~\n\t\ts/^2\\K[4-9]/0/r\n\t:\n\t\ts/^1\\K[3-9]/0/r =~\n\t\ts/^0\\K0/1/r =~\n\t\ts/^\\K([2-9])(.)/$2 ? \"0$2\":10 /er\n\t)\n\t=~ s/:\\K[6-9]/0/r"}, {"source_code": "$/ = $\\;\n$_ = <>;\n\ns/12\\n1\\K[3-9]/0/,\ns/12\\n0\\K0/1/,\ns/12\\n\\K([2-9])(.)/$2 ? \"0$2\":10 /e,\ns/24\\n\\K[3-9]/0/,\ns/24\\n2\\K[4-9]/0/,\ns/:\\K[6-9]/0/,\ns/.*\\n//,\n\nprint"}], "negative_code": [{"source_code": "\n$,=\"\";\nmy $f=<>;\nchomp($f);\nmy ($h,$m)=split /\\:/,<>;\nchomp($h,$m);\nif($f==12)\n{\n if($h>12 or $h eq \"00\")\n {\n if(substr($h,1,1) eq \"0\")\n {\n $h=\"10\";\n }\n else\n {\n $h=\"0\".substr($h,1,1);\n }\n }\n}\nif($f==24)\n{\n if($h>23)\n {\n {\n $h=\"0\".substr($h,1,1);\n }\n }\n}\nif($m>59)\n{\n$m=\"0\".substr($h,1,1);\n}\n\nprint $h,':',$m,\"\\n\";\n"}, {"source_code": "\n$,=\"\";\nmy $f=<>;\nchomp($f);\nmy ($h,$m)=split /\\:/,<>;\nchomp($h,$m);\nif($f==12)\n{\n if($h>12)\n {\n if(substr($h,1,1) eq \"0\")\n {\n $h=\"10\";\n }\n else\n {\n $h=\"0\".substr($h,1,1);\n }\n }\n}\nif($f==24)\n{\n if($h>23)\n {\n {\n $h=\"0\".substr($h,1,1);\n }\n }\n}\nif($m>59)\n{\n$m=\"0\".substr($h,1,1);\n}\n\nprint $h,':',$m,\"\\n\";\n"}, {"source_code": "$format = <>; @time = split /:/, <>; \nwhile($time[1] > 59) { $time[1] -= 10; }; \nif ($format == 12) { if($time[0] > $format) { while($time[0] > $format) { $time[0] -= 10; }; substr($time[0], 0, 0, '0'); }; if($time[0] == '00') { $time[0] = '01'; }; } \nelse { if($time[0] >= $format) { while($time[0] >= $format) { $time[0] -= 10; }; if (($time[0] >= 0) && ($time[0] < 10)) { substr($time[0], 0, 0, '0'); }; }; };\nprint $time[0], ':', $time[1];"}, {"source_code": "$format = <>; @time = split /:/, <>; \nwhile($time[1] > 59) { $time[1] -= 10; }; \nif ($format == 12) { while($time[0] > $format) { $time[0] -= 10; }; if($time[0] == '00') { $time[0] = '01'; }; } \nelse { while($time[0] >= $format) { $time[0] -= 10; }; };\nprint $time[0], ':', $time[1];"}, {"source_code": "$format = <>; @time = split /:/, <>; \nwhile($time[1] > 59) { $time[1] -= 10; }; \nif ($format == 12) { if($time[0] > $format) { while($time[0] > $format) { $time[0] -= 10; }; substr($time[0], 0, 0, '0'); }; if($time[0] == '00') { $time[0] = '01'; }; } \nelse { if($time[0] >= $format) { while($time[0] >= $format) { $time[0] -= 10; }; if (($time[0] >= 0) || ($time[0] < 10)) { substr($time[0], 0, 0, '0'); }; }; };\nprint $time[0], ':', $time[1];"}, {"source_code": "$/ = $\\;\n$_ = <>;\n\ns/12\\n1\\K[3-9]/0/,\ns/12\\n0\\K0/1/,\ns/12\\n\\K([2-9])(.)/$2 ? \"0$2\":10 /e,\ns/24\\n[3-9]/0/,\ns/24\\n2\\K[4-9]/0/,\ns/:\\K[6-9]/0/,\ns/.*\\n//,\n\nprint"}, {"source_code": "print +( <> =~ /^2/ * ($_ = <>) ?\n\t\ts/^[3-9]\\K./0/r =~\n\t\ts/^2\\K[4-9]/0/r\n\t:\n\t\ts/^1\\K[3-9]/0/r =~\n\t\ts/^0\\K0/1/r =~\n\t\ts/^\\K([2-9])(.)/$2 ? \"0$2\":10 /er\n\t)\n\t=~ s/:\\K[6-9]/0/r"}, {"source_code": "$/ = $\\;\n$_ = <>;\n\ns/12\\n1\\K[3-9]/0/,\ns/12\\n0\\K0/1/,\ns/12\\n\\K([2-9])(.)/$2 ? \"0$2\":10 /e,\ns/24\\n[3-9]\\K./0/,\ns/24\\n2\\K[4-9]/0/,\ns/:\\K[6-9]/0/,\ns/.*\\n//,\n\nprint"}, {"source_code": "print +( <> =~ /^2/ * ($_ = <>) ?\n\t\ts/^[3-9]/0/r =~\n\t\ts/^2\\K[4-9]/0/r\n\t:\n\t\ts/^1\\K[3-9]/0/r =~\n\t\ts/^0\\K0/1/r =~\n\t\ts/^\\K([2-9])(.)/$2 ? \"0$2\":10 /er\n\t)\n\t=~ s/:\\K[6-9]/0/r"}], "src_uid": "88d56c1e3a7ffa94354ce0c70d8e958f"} {"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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100) \u2014 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\u2009\u2264\u2009j\u2009\u2264\u2009n, 1\u2009\u2264\u2009i\u2009\u2264\u2009m, 0\u2009\u2264\u2009aij\u2009\u2264\u2009109) 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 \u2014 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": "sub max {\n my $max = 0;\n for my $i(1..$#_) {\n $max = $i if ($_[$i] > $_[$max]);\n }\n return $max;\n}\nmy ($n, $m) = split / /, <>;\nmy @arr = (0) x $n;\nfor (0..$m-1) {\n $arr[max(split / /, <>)]++;\n}\nprint max(@arr) + 1\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp($_ = <>);\nmy ($n, $m) = split;\nmy %votes;\nmy ($v_max, $v_candidate) = (0,1);\n\nfor my $i (0..$m-1) {\n chomp($_ = <>);\n my @city_votes = split;\n my ($max, $candidate) = (0, 1);\n my $len = @city_votes;\n for my $j (0..$len-1) {\n if($max < $city_votes[$j]) {\n $max = $city_votes[$j];\n $candidate = $j + 1;\n }\n }\n\n $votes{$candidate} += 1;\n if($votes{$candidate} > $v_max) {\n $v_max = $votes{$candidate};\n $v_candidate = $candidate;\n }\n elsif($votes{$candidate} == $v_max && $v_candidate > $candidate) {\n $v_max = $votes{$candidate};\n $v_candidate = $candidate;\n }\n}\n\nprint $v_candidate, \"\\n\";"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\nuse List::Util;\n\n($n, $m) = split / /, <>;\n@c = (0) x $n;\nforeach my $i (1..$m) {\n\t($mx, $v) = (-1, 0);\n\t@a = split / /, <>;\n\tforeach (0..$n-1) {\n\t\tif ($a[$_] > $mx) {\n\t\t\t$mx = $a[$_];\n\t\t\t$v = $_;\n\t\t}\n\t}\n\t++$c[$v];\n}\n($mx, $v) = (-1, 0);\nforeach (0..$n-1) {\n\tif ($c[$_] > $mx) {\n\t\t$mx = $c[$_];\n\t\t$v = $_;\n\t}\n}\nsay $v+1;"}, {"source_code": "<>;\nmap {\n $n = 0;\n $m = -1;\n map {\n $n++;\n $_ > $m and do { $m = $_; $f = $n }\n } split;\n $r->{$f}++\n} <>;\n\nprint 0\n + ( sort { $r->{$b} <=> $r->{$a} || $a <=> $b } keys %{$r} )[0]"}, {"source_code": "<>; \n\nmap { \n\t\n\t$i = 0;\n\t$max = -1;\n\t\n\tmap {\n\t\t$i ++;\n\t\t$_ > $max and do { $max = $_ ; $m = $i }\n\t\t\t\n\t\t\t\n\t\t} split;\n\t\t\n\t$h{ $m } ++\n\t\n\t} <>;\n\nprint 0 + (sort { $h{ $b } <=> $h{ $a } || $a <=> $b } keys %h)[ 0 ]"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp($_ = <>);\nmy ($n, $m) = split;\nmy %votes;\nmy ($v_max, $v_candidate) = (0, 0);\n\nfor my $i (0..$m) {\n chomp($_ = <>);\n my @city_votes = split;\n my ($max, $candidate) = (0, 0);\n my $len = length(@city_votes);\n for my $j (0..$len) {\n if($max < $city_votes[$j]) {\n $max = $city_votes[$j];\n $candidate = $j + 1;\n }\n }\n\n $votes{$candidate} += 1;\n if($votes{$candidate} > $v_max) {\n $v_max = $votes{$candidate};\n $v_candidate = $candidate;\n }\n elsif($votes{$candidate} == $v_max && $v_candidate > $candidate) {\n $v_max = $votes{$candidate};\n $v_candidate = $candidate;\n }\n}\n\nprint $v_candidate, \"\\n\";"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp($_ = <>);\nmy ($n, $m) = split;\nmy %votes;\nmy ($v_max, $v_candidate) = (0, 0);\n\nfor my $i (0..$m-1) {\n chomp($_ = <>);\n my @city_votes = split;\n my ($max, $candidate) = (0, 0);\n my $len = @city_votes;\n for my $j (0..$len-1) {\n if($max < $city_votes[$j]) {\n $max = $city_votes[$j];\n $candidate = $j + 1;\n }\n }\n\n $votes{$candidate} += 1;\n if($votes{$candidate} > $v_max) {\n $v_max = $votes{$candidate};\n $v_candidate = $candidate;\n }\n elsif($votes{$candidate} == $v_max && $v_candidate > $candidate) {\n $v_max = $votes{$candidate};\n $v_candidate = $candidate;\n }\n}\n\nprint $v_candidate, \"\\n\";"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp($_ = <>);\nmy ($n, $m) = split;\nmy %votes;\nmy ($v_max, $v_candidate) = (0, 0);\n\nfor my $i (0..$m-1) {\n chomp($_ = <>);\n my @city_votes = split;\n my ($max, $candidate) = (0, 0);\n my $len = length(@city_votes);\n for my $j (0..$len-1) {\n if($max < $city_votes[$j]) {\n $max = $city_votes[$j];\n $candidate = $j + 1;\n }\n }\n\n $votes{$candidate} += 1;\n if($votes{$candidate} > $v_max) {\n $v_max = $votes{$candidate};\n $v_candidate = $candidate;\n }\n elsif($votes{$candidate} == $v_max && $v_candidate > $candidate) {\n $v_max = $votes{$candidate};\n $v_candidate = $candidate;\n }\n}\n\nprint $v_candidate, \"\\n\";"}, {"source_code": "<>;\nmap {\n $n = 0;\n $m = -1;\n map {\n $n++;\n $_ > $m and do { $m = $_; $f = $n }\n } split;\n $r->{$f}++\n} <>;\n\nprint 0\n + ( sort { $r->{$a} <=> $r->{$b} || $a <=> $b } keys %{$r} )[0]\n"}, {"source_code": "<>;\nwhile (<>) {\n @_ = split / /, $_;\n map { $a[$_] += $_[$_] } 0 .. $#_;\n}\nmap { $m = $_ > ( $m // 0 ) ? $_ : $m } @a;\nprint ++$_ for (sort {$a <=> $b} grep { $a[$_] == $m } 0 .. $#a)[0];\n"}, {"source_code": "<>;\nmap {\n $n = 0;\n $m = 0;\n map {\n $n++;\n $_ > $m and do { $m = $_; $f = $n }\n } split;\n $r->{$f}++\n} <>;\n\nprint 0 + ( sort { $a <=> $b } keys %{$r} )[0];\n"}, {"source_code": "<>;\nmap {\n $n = 0;\n $m = -1;\n map {\n $n++;\n $_ > $m and do { $m = $_; $f = $n }\n } split;\n $r->{$f}++\n} <>;\n\nprint 0 + ( sort { $a <=> $b } keys %{$r} )[0]"}, {"source_code": "$\\ = $/;\n\n<>; \n\nmap { \n\t\n\t$i = 0;\n\t$max = 0;\n\t\n\tmap {\n\t\t$i ++;\n\t\t$_ > $max and do { $max = $_ ; $m = $i }\n\t\t\t\n\t\t\t\n\t\t} split;\n\t\t\n\t$h{ $m } ++\n\t\n\t} <>;\n\t\nprint %h;\n\nprint 0 + (sort { $h{ $b } <=> $h{ $a } || $a <=> $b } keys %h)[ 0 ]"}, {"source_code": "$\\ = $/;\n\n<>; \n\nmap { \n\t\n\t$i = 0;\n\t$max = 0;\n\t\n\tmap {\n\t\t$i ++;\n\t\t$_ > $max and do { $max = $_ ; $m = $i }\n\t\t\t\n\t\t\t\n\t\t} split;\n\t\t\n\t$h{ $m } ++\n\t\n\t} <>;\n\nprint 0 + (sort { $h{ $b } <=> $h{ $a } || $a <=> $b } keys %h)[ 0 ] || 1"}, {"source_code": "$\\ = $/;\n\n<>; \n\nmap { \n\t\n\t$i = 0;\n\t$max = 0;\n\t\n\tmap {\n\t\t$i ++;\n\t\t$_ > $max and do { $max = $_ ; $m = $i }\n\t\t\t\n\t\t\t\n\t\t} split;\n\t\t\n\t$h{ $m } ++\n\t\n\t} <>;\n\nprint 0 + (sort { $h{ $b } <=> $h{ $a } || $a <=> $b } keys %h)[ 0 ]"}], "src_uid": "b20e98f2ea0eb48f790dcc5dd39344d3"} {"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$$$\u00a0\u2014 is the maximum possible ($$$k$$$\u00a0\u2014 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$$$)\u00a0\u2014 the number of test cases . Each test case is a string containing a single integer $$$m$$$ ($$$1 \\le m \\le 10^9$$$)\u00a0\u2014 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$$$\u00a0\u2014 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": "<>;print $_-10**(-2+length),\" \"for(<>)"}, {"source_code": "<>;print$_-10**(-2+length),\" \"for(<>)"}, {"source_code": "<>;print$_-10**(-2+length),\" \"for(<>)"}, {"source_code": "<>;for(<>){print$_-10**(-2+length),\" \"}"}, {"source_code": "<>;for(<>){print$_-10**(length($_)-2),\" \"}"}, {"source_code": "<>;print$_-10**(-2+length),\" \"for(<>)"}, {"source_code": "<>;print$_-10**(-2+length),\" \"for(<>)\r\n"}], "negative_code": [], "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\u2009\u2264\u2009n\u2009\u2264\u2009777) \u2014 the number of potential employees that sent resumes.", "output_spec": "Output one integer \u2014 the number of different variants of group composition.", "sample_inputs": ["7"], "sample_outputs": ["29"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n\nuse bigint;\n\nsub factorial {\n my $n = shift;\n if ($n==0) {\n return 1;\n } else {\n return $n*factorial($n-1);\n }\n}\n\nsub subfactorial {\n my $n = shift;\n my $sub = shift;\n if ($n==$sub) {\n return 1;\n } else {\n return $n*subfactorial($n-1, $sub);\n }\n}\n\n\n$np = ;\n$subnp = subfactorial($np, $np-4);\n$k = factorial(4);\n$result = 0;\nfor ($i=5; $i <= 7; $i++)\n{\n\t$subnp *= $np - $i+1;\n\t$k *= $i;\n\t$result += ($subnp / $k);\n}\nprint \"$result\\n\";\n\n"}], "negative_code": [], "src_uid": "09276406e16b46fbefd6f8c9650472f0"} {"nl": {"description": "Valera has 2\u00b7n 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 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009100). The second line contains 2\u00b7n space-separated integers ai (10\u2009\u2264\u2009ai\u2009\u2264\u200999), denoting the numbers on the cubes.", "output_spec": "In the first line print a single number \u2014 the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2\u00b7n numbers bi (1\u2009\u2264\u2009bi\u2009\u2264\u20092). 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 \u2014 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,\u20091345,\u20092413,\u20092445. 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": "use strict;\n\nuse List::Util qw(sum min);\n\nchomp(my $n = <>);\nchomp($_ = <>);\nmy @a = split;\n\nmy %occurence = ();\n++$occurence{$_} for @a;\n\nmy $sum = sum map { min $_, 2 } values %occurence;\nmy $x = int($sum / 2);\nmy $y = $sum - $x;\n\nprint $x * $y, \"\\n\";\n\nmy %pos = ();\npush @{$pos{$a[$_]}}, $_ for 0 .. $#a;\n\nmy @res = (0) x (2 * $n);\nmy @one = ();\nmy @rest = ();\nmy @h1 = ();\nmy @h2 = ();\nwhile (my ($k, $v) = each %pos) {\n if (@$v == 1) {\n push @one, @$v;\n } else {\n push @h1, shift @$v;\n push @h2, shift @$v;\n push @rest, @$v;\n }\n}\nwhile (@one) {\n push @h1, shift @one;\n push @h2, shift @one if @one;\n}\nwhile (@rest) {\n if (@h1 < $n) {\n push @h1, shift @rest;\n } else {\n push @h2, shift @rest;\n }\n}\n$res[$_] = 1 for @h1;\n$res[$_] = 2 for @h2;\nprint \"@res\\n\";\n"}, {"source_code": "$n=<>;\n$a=$_=<>;\n$a=~s/\\d{2}/push @a, ++$m[$&]/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\n\t$m=$n*2-$f;\n\t$m%2?($m=($m+1)*($m-1)/4):($m**=2,$m/=4);\n\tprint \"$m\\n\";\ns/\\d{2}/$m[$&]>1?($i++):($a[$i++]+=2)/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\nprint \"@a\\n\";\n"}, {"source_code": "$n=<>;\n$a=$_=<>;\n$a=~s/\\d{2}/push @a, ++$m[$&]/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\n\t$m=$n*2-$f;\n\t$m%2?($m=($m+1)*($m-1)/4):($m**=2,$m/=4);\n\tprint \"$m\\n\";\ns/\\d{2}/$m[$&]>1?($i++):($a[$i++]+=2)/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\nprint \"@a\\n\";\n"}, {"source_code": "$n=<>;\n$a=$_=<>;\n$a=~s/\\d{2}/push @a, ++$m[$&]/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\n\t$m=$n*2-$f;\n\t$m%2?($m=($m+1)*($m-1)/4):($m**=2,$m/=4);\n\tprint \"$m\\n\";\ns/\\d{2}/$m[$&]>1?($i++):($a[$i++]+=2)/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\nprint \"@a\\n\";"}, {"source_code": "$n=<>;\n$a=$_=<>;\n$a=~s/\\d{2}/push @a, ++$m[$&]/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\n\t$m=$n*2-$f;\n\t$m%2?($m=($m+1)*($m-1)/4):($m**=2,$m/=4);\n\tprint \"$m\\n\";\ns/\\d{2}/$m[$&]>1?($i++):($a[$i++]+=2)/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\nprint \"@a\\n\";\n"}, {"source_code": "$n=<>;\n$a=$_=<>;\n$a=~s/\\d{2}/push @a, ++$m[$&]/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\n\t$m=$n*2-$f;\n\t$m%2?($m=($m+1)*($m-1)/4):($m**=2,$m/=4);\n\tprint \"$m\\n\";\ns/\\d{2}/$m[$&]>1?($i++):($a[$i++]+=2)/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\nprint \"@a\\n\";\n"}, {"source_code": "$n=<>;\n$a=$_=<>;\n$a=~s/\\d{2}/push @a, ++$m[$&]/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\n\t$m=$n*2-$f;\n\t$m%2?($m=($m+1)*($m-1)/4):($m**=2,$m/=4);\n\tprint \"$m\\n\";\ns/\\d{2}/$m[$&]>1?($i++):($a[$i++]+=2)/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\nprint \"@a\\n\";\n"}, {"source_code": "$n=<>;\n$a=$_=<>;\n$a=~s/\\d{2}/push @a, ++$m[$&]/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\n\t$m=$n*2-$f;\n\t$m%2?($m=($m+1)*($m-1)/4):($m**=2,$m/=4);\n\tprint \"$m\\n\";\ns/\\d{2}/$m[$&]>1?($i++):($a[$i++]+=2)/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\nprint \"@a\\n\";\n"}, {"source_code": "$n=<>;\n$a=$_=<>;\n$a=~s/\\d{2}/push @a, ++$m[$&]/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\n\t$m=$n*2-$f;\n\t$m%2?($m=($m+1)*($m-1)/4):($m**=2,$m/=4);\n\tprint \"$m\\n\";\ns/\\d{2}/$m[$&]>1?($i++):($a[$i++]+=2)/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\nprint \"@a\\n\";\n"}, {"source_code": "$n=<>;\n$a=$_=<>;\n$a=~s/\\d{2}/push @a, ++$m[$&]/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\n\t$m=$n*2-$f;\n\t$m%2?($m=($m+1)*($m-1)/4):($m**=2,$m/=4);\n\tprint \"$m\\n\";\ns/\\d{2}/$m[$&]>1?($i++):($a[$i++]+=2)/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\nprint \"@a\\n\";"}, {"source_code": "$n=<>;\n$a=$_=<>;\n$a=~s/\\d{2}/push @a, ++$m[$&]/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\n\t$m=$n*2-$f;\n\t$m%2?($m=($m+1)*($m-1)/4):($m**=2,$m/=4);\n\tprint \"$m\\n\";\ns/\\d{2}/$m[$&]>1?($i++):($a[$i++]+=2)/ge;\nfor $j(@a){\n\t$j>2 and $j=$f++%2?1:2;\n\t}\nprint \"@a\\n\";\n"}], "negative_code": [{"source_code": "use strict;\n\nuse List::Util qw(sum min);\n\nchomp(my $n = <>);\nchomp($_ = <>);\nmy @a = split;\n\nmy %occurence = ();\n++$occurence{$_} for @a;\n\nmy $sum = sum map { min $_, 2 } values %occurence;\nmy $x = int($sum / 2);\nmy $y = $sum - $x;\n\nprint $x * $y, \"\\n\";\n\nmy %pos = ();\npush @{$pos{$a[$_]}}, $_ for 0 .. $#a;\n\nmy @res = (0) x (2 * $n);\nmy @one = ();\nmy @rest = ();\nmy @h1 = ();\nmy @h2 = ();\nwhile (my ($k, $v) = each %occurence) {\n if (@{$pos{$k}} == 1) {\n push @one, @{$pos{$k}};\n } else {\n push @h1, shift @{$pos{$k}};\n push @h2, shift @{$pos{$k}};\n push @rest, @{$pos{$k}};\n }\n}\nwhile (@one) {\n push @h1, shift @one;\n push @h2, shift @one if @one;\n}\nfor (@rest) {\n if (@h1 < $n) {\n push @h1, shift @rest;\n } else {\n push @h2, shift @rest;\n }\n}\n$res[$_] = 1 for @h1;\n$res[$_] = 2 for @h2;\nprint \"@res\\n\";\n"}, {"source_code": "use strict;\n\nuse List::Util qw(sum min);\n\nchomp(my $n = <>);\nchomp($_ = <>);\nmy @a = split;\n\nmy %occurence = ();\n++$occurence{$_} for @a;\n\nmy $sum = sum map { min $_, 2 } values %occurence;\nmy $x = int($sum / 2);\nmy $y = $sum - $x;\n\nprint $x * $y, \"\\n\";\n\nmy %pos = ();\npush @{$pos{$a[$_]}}, $_ for 0 .. $#a;\n\nmy @res = (0) x (2 * $n);\nmy @one = ();\nmy @rest = ();\nwhile (my ($k, $v) = each %occurence) {\n if (@{$pos{$k}} == 1) {\n push @one, @{$pos{$k}};\n } else {\n $res[shift @{$pos{$k}}] = 1;\n $res[shift @{$pos{$k}}] = 2;\n push @rest, @{$pos{$k}};\n --$x;\n --$y;\n }\n}\nwhile (@one) {\n $res[shift @one] = 1;\n --$x;\n if (@one) {\n $res[shift @one] = 2;\n --$y;\n }\n}\nfor (@rest) {\n if ($x > 0) {\n $res[shift @rest] = 1;\n --$x;\n } else {\n $res[shift @rest] = 2;\n --$y;\n }\n}\nprint \"@res\\n\";\n"}], "src_uid": "080287f34b0c1d52eb29eb81dada41dd"} {"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$$$) \u2014 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}$$$) \u2014 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": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\nuse integer;\n\n<>;\n\nmy $bline = <>;\nchomp $bline;\n\nmy @bseq = split /\\s+/, $bline;\n\nmy ($low, $up) = (0, shift @bseq);\n\nmy @from_front = ($low);\nmy @from_back = ($up);\n\nforeach my $b (@bseq) {\n\tmy $sum = $low + $up;\n\tmy $err = abs($sum - $b);\n\t$low+= $err if $sum < $b;\n\t$up -= $err if $sum > $b;\n\tpush @from_front, $low;\n\tpush @from_back , $up ;\n}\n\nprint \"@from_front\";\nprint \" \";\nmy @revd = reverse @from_back;\nprint \"@revd\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @L;\n\tmy @R;\n\t\n\tmy $max = shift @_;\n\tunshift @L, 0;\n\tunshift @R, $max;\n\t\n\tfor( @_ ){\n\t\tmy $sum = $L[ 0 ] + $R[ 0 ];\n\t\tif( $sum == $_ ){\n\t\t\tunshift @L, $L[ 0 ];\n\t\t\tunshift @R, $R[ 0 ];\n\t\t\t}\n\t\telsif( $sum < $_ ){\n\t\t\tunshift @L, $_ - $R[ 0 ];\n\t\t\tunshift @R, $R[ 0 ];\n\t\t\t}\n\t\telsif( $sum > $_ ){\n\t\t\tunshift @R, $_ - $L[ 0 ];\n\t\t\tunshift @L, $L[ 0 ];\n\t\t\t}\n\t\t}\n\t\n\tprint join ' ', ( reverse @L ), @R;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = reverse split ' ', <>;\n\t\n\tmy @L;\n\tmy @R;\n\t\n\tmy $max = shift @_;\n\tunshift @L, 0;\n\tunshift @R, $max;\n\t\n\tfor( @_ ){\n\t\tmy $sum = $L[ 0 ] + $R[ 0 ];\n\t\tif( $sum == $_ ){\n\t\t\tunshift @L, $L[ 0 ];\n\t\t\tunshift @R, $R[ 0 ];\n\t\t\t}\n\t\telsif( $sum < $_ ){\n\t\t\tunshift @L, $_ - $R[ 0 ];\n\t\t\tunshift @R, $R[ 0 ];\n\t\t\t}\n\t\telsif( $sum > $_ ){\n\t\t\tunshift @R, $_ - $L[ 0 ];\n\t\t\tunshift @L, $L[ 0 ];\n\t\t\t}\n\t\t}\n\t\n\tprint join ' ', ( reverse @L ), @R;\n\t}"}], "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 \u2014 you can go from the (n\u2009-\u20091)-th room to the n-th room. Thus, you can go to room x only from room x\u2009-\u20091.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\u2009-\u20091, 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\u2009-\u20091 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\u2009\u2264\u2009n\u2009\u2264\u2009105)\u00a0\u2014\u00a0the number of rooms in the house. The second line of the input contains string s of length 2\u00b7n\u2009-\u20092. 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\u00a0\u2014\u00a0the 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 \u2014 the type of the key that lies in room number (i\u2009+\u20091)\u2009/\u20092. The even positions in the given string contain uppercase Latin letters \u2014 the types of doors between the rooms. Thus, each even position i of the given string s contains an uppercase letter \u2014 the type of the door that leads from room i\u2009/\u20092 to room i\u2009/\u20092\u2009+\u20091.", "output_spec": "Print the only integer \u2014 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": "<>;\n$_ = <>, chomp;\nfor (split //){\n\t++ $j % 2 ?\n\t\t$h{ $_ } ++\n\t\t:\n\t\t$h{ (lc) } ? $h{ (lc) } -- : $ans ++\n\t}\nprint 0 + $ans\n"}, {"source_code": "<>;\n$_ = <>, chomp;\nfor (split //){\n\t++ $j % 2 ?\n\t\t$h{ $_ } ++\n\t\t:\n\t\t$h{ (lc) } ? $h{ (lc) } -- : $ans ++\n\t}\nprint 0 + $ans\n"}, {"source_code": "<>;\n$_ = <>, chomp;\nfor (split //){\n\t++ $j % 2 ?\n\t\t$h{ $_ } ++\n\t\t:\n\t\t$h{ (lc) } ? $h{ (lc) } -- : $ans ++\n\t}\nprint 0 + $ans"}, {"source_code": "<>;\n$_ = <>, chomp;\nfor (split //){\n\t++ $j % 2 ?\n\t\t$h{ $_ } ++\n\t\t:\n\t\t$h{ (lc) } ? $h{ (lc) } -- : $ans ++\n\t}\nprint 0 + $ans\n"}, {"source_code": "use strict;\nuse warnings;\nuse Data::Dumper;\n\nmy $rc = <>;\nmy $a;\nmy @rooms;\nmy @keys;\nmy %hk;\nfor (0 .. $rc - 2)\n{\n \n read(STDIN, $keys[$_], 1);\n read(STDIN, $rooms[$_], 1);\n \n $keys[$_] = uc($keys[$_]);\n $rooms[$_] = uc($rooms[$_]);\n $hk{$rooms[$_]} = 0;\n $hk{$keys[$_]} = 0;\n \n}\nmy $ans = 0;\nfor (0 .. $#rooms)\n{\n $hk{$keys[$_]}++;\n if($hk{$rooms[$_]} <= 0)\n {\n $ans ++;\n }\n else\n {\n $hk{$rooms[$_]}--;\n }\n \n}\nprint $ans;"}, {"source_code": "<>;\n$_ = <>, chomp;\nfor (split //){\n\t++ $j % 2 ?\n\t\t$h{ $_ } ++\n\t\t:\n\t\t$h{ (lc) } ? $h{ (lc) } -- : $ans ++\n\t}\nprint 0 + $ans\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = split //, <>;\n($ans, $len) = (0, $n + $n);\n@c = (0) x 26;\nforeach (0 .. $n-2) {\n\t$j = (ord $a[$_*2]) - (ord 'a');\n\t$k = (ord $a[$_*2+1]) -( ord 'A');\n\t# say \"$j $k\";\n\t$c[$j]++;\n\t$c[$k]>0 and $c[$k]-- or ++$ans;\n}\nsay $ans;"}, {"source_code": "<>;\n$_ = <>, chomp;\nfor (split //){\n ++ $j % 2 ?\n $h{ $_ } ++\n :\n $h{ (lc) } ? $h{ (lc) } -- : $ans ++\n }\nprint 0 + $ans"}, {"source_code": "<>;\n$_ = <>, chomp;\nfor (split //){\n\t++ $j % 2 ?\n\t\t$h{ $_ } ++\n\t\t:\n\t\t$h{ (lc) } ? $h{ (lc) } -- : $ans ++\n\t}\nprint 0 + $ans\n"}, {"source_code": "<>;\n$_ = <>, chomp;\nfor (split //){\n\t++ $j % 2 ?\n\t\t$h{ $_ } ++\n\t\t:\n\t\t$h{ (lc) } ? $h{ (lc) } -- : $ans ++\n\t}\nprint 0 + $ans\n"}, {"source_code": "<>;\n$_ = <>, chomp;\nfor (split //){\n\t++ $j % 2 ?\n\t\t$h{ $_ } ++\n\t\t:\n\t\t$h{ (lc) } ? $h{ (lc) } -- : $ans ++\n\t}\nprint 0 + $ans\n"}, {"source_code": "$\\=$/;\nwhile(<>){\n\t$_ = <>;\n\tchomp;\n\t@_ = split //;\n\t$j = 1;\n\t%h = ();\n\t$ans = 0;\n\tfor $i (@_){\n\t\tif ($j++ % 2) {\n#\t\t\tprint $i;\n\t\t\t$h{$i} ++;\n\t\t\t\n\t\t\t}\n\t\telse{\n\t\t\t$h{ lc($i) } ? ( $h{ lc($i) }-- ) : ( $ans++ );\n\t\t\t\n\t\t\t;\n\t\t\t\n\t\t\t}\n\t\t\n\t\t\n\t\t}\n\tprint $ans;\n\t\n\t\n\t}"}, {"source_code": "<>;\n$_ = <>, chomp;\nfor (split //){\n\t++ $j % 2 ?\n\t\t$h{ $_ } ++\n\t\t:\n\t\t$h{ (lc) } ? $h{ (lc) } -- : $ans ++\n\t}\nprint 0 + $ans"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\nuse Data::Dumper;\n\nmy $rc = <>;\nmy $a;\nmy @rooms;\nmy @keys;\nfor (0 .. $rc - 2)\n{\n read(STDIN, $rooms[$_], 1);\n $rooms[$_] = uc($rooms[$_]);\n read(STDIN, $keys[$_], 1);\n}\nmy %hk;\nmy $ans = 0;\nfor (0 .. ($rc) / 2 + 1)\n{\n $hk{$keys[$_]}++;\n if(!defined($hk{$rooms[$_]}) or $hk{$rooms[$_]} <= 0)\n {\n $ans ++;\n }\n else\n {\n $hk{$rooms[$_]}--;\n }\n}\nprint $ans;"}, {"source_code": "use strict;\nuse warnings;\nuse Data::Dumper;\n\nmy $rc = <>;\nmy $a;\nmy @rooms;\nmy @keys;\nmy %hk;\nfor (0 .. $rc - 2)\n{\n read(STDIN, $rooms[$_], 1);\n $rooms[$_] = uc($rooms[$_]);\n read(STDIN, $keys[$_], 1);\n $keys[$_] = uc($keys[$_]);\n $hk{$rooms[$_]} = 0;\n $hk{$keys[$_]} = 0;\n \n}\n\nmy $ans = 0;\nfor (0 .. $#rooms)\n{\n $hk{$keys[$_]}++;\n if($hk{$rooms[$_]} <= 0)\n {\n $ans ++;\n }\n else\n {\n $hk{$rooms[$_]}--;\n }\n \n}\nprint $ans;"}, {"source_code": "use strict;\nuse warnings;\nuse Data::Dumper;\n\nmy $rc = <>;\nmy $a;\nmy @rooms;\nmy @keys;\nfor (0 .. $rc - 2)\n{\n read(STDIN, $rooms[$_], 1);\n $rooms[$_] = uc($rooms[$_]);\n read(STDIN, $keys[$_], 1);\n}\nmy %hk;\nmy $ans = 0;\nfor (0 .. $#rooms)\n{\n $hk{$keys[$_]}++;\n if(!defined($hk{$rooms[$_]}) or $hk{$rooms[$_]} <= 0)\n {\n $ans ++;\n }\n else\n {\n $hk{$rooms[$_]}--;\n }\n}\nprint $ans;"}, {"source_code": "<>;\n$_ = <>, chomp;\nfor (split //){\n\t++ $j % 2 ?\n\t\t$h{$i} ++\n\t\t:\n\t\t$h{ (lc) } ? $h{ (lc) } -- : $ans ++\n\t}\nprint 0 + $ans"}, {"source_code": "<>;\n$_ = <>, chomp;\nfor (split //){\n\t++ $j % 2 ?\n\t\t$h{ (lc) } ? $h{ (lc) } -- : $ans ++ \n\t\t:\n\t\t$h{$i} ++\n\t}\nprint 0 + $ans"}, {"source_code": "<>;\n$_ = <>, chomp;\nfor (split //){\n\t++ $j % 2 ?\n\t\t$h{$i} ++\n\t\t:\n\t\t( $h{ (lc) } ? $h{ (lc) } -- : $ans ++ )\n\t}\nprint 0 + $ans"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 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 \u2014 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": "$l=<>**2;print$l--.\" $_ \"for(1..$l/2)\n"}, {"source_code": "$l=<>**2;print$l--.\" $_ \"for(1..$l/2)\n"}, {"source_code": "my $n = int<>**2;\nprint $n-- . \" $_\\n\" for(1..$n / 2);"}, {"source_code": "$l=<>**2;print$l--.\" $_ \"for(1..$l/2)"}, {"source_code": "$l=<>**2;print$l--.\" $_ \"for(1..$l/2)\n"}, {"source_code": "$l=<>**2;print$l--.\" $_ \"for(1..$l/2)\n"}, {"source_code": "$n=<>;$e=$n**2;$,=' ';for(1..$n){print++$s,$e--,\"\"for(1..$n/2);print\"\\n\";}"}, {"source_code": "$n=<>**2;print $n--,\" $s\\n\" while($n>$s++)"}, {"source_code": "$l=<>**2;print$l--.\" $_ \"for(1..$l/2)"}, {"source_code": "$l=<>**2;print$l--.\" $_ \"for(1..$l/2)\n"}, {"source_code": "$l=<>**2;print$l--.\" $_ \"for(1..$l/2)\n"}, {"source_code": "use strict;\n\nmy $n = <>;\n\nmy $counter = 1;\nforeach my $i (1..$n) { \n\tprint $i.' ';\n\t\n\tmy $number = $i;\n\tfor my $j (1..$n - 1) {\n\t\tif ($number % $n == 0) {\n\t\t\t$counter = 1;\n\t\t}\n\t\telse {\n\t\t\t$counter = $n + 1;\n\t\t}\n\t\t$number = $counter + $number;\n\t\tprint $number.' ';\n\t\t\n\t\t\n\t\t#my $counter = $n + 2 - $;\n\t\t#my $number = $i;\n\t\t\n\t}\n\tprint \"\\n\";\n}"}, {"source_code": "$l=<>**2;print$l--.\" $_ \"for(1..$l/2)\n"}, {"source_code": "$_=<>;\nchomp;\nfor $i(1..$_){\n @j=();\n ++$k;\n for $j(1..$_){\n push @j, (($j * $_) - ++$k % $_ )\n }\n print \"@j\\n\";\n }"}, {"source_code": "$l=<>**2;print$l--.\" $_ \"for(1..$l/2)\n"}], "negative_code": [{"source_code": "my $n = int<>;\nfor(my $i = 1; $i <= 2 * $n; $i += $n/2) {\n print ((join \" \", ($i..$i+$n/2-1)) . \" \");\n print ((join \" \", ($n*$n-$i..$n*$n-$i+$n/2-1)) . \"\\n\");\n}\n"}, {"source_code": "my $n = int<>;\nprint ($_ . \" \" . ($n * $n - $_ + 1) . \"\\n\") for(1..$n);\n"}, {"source_code": "use strict;\n\nmy $n = 5;\n\nmy $counter = 1;\nforeach my $i (1..$n) { \n\tprint $i.' ';\n\t\n\tmy $number = $i;\n\tfor my $j (1..$n - 1) {\n\t\tif ($number % $n == 0) {\n\t\t\t$counter = 1;\n\t\t}\n\t\telse {\n\t\t\t$counter = $n + 1;\n\t\t}\n\t\t$number = $counter + $number;\n\t\tprint $number.' ';\n\t\t\n\t\t\n\t\t#my $counter = $n + 2 - $;\n\t\t#my $number = $i;\n\t\t\n\t}\n\tprint \"\\n\";\n}"}], "src_uid": "0ac2a0954fe43d66eac32072c8ea5070"} {"nl": {"description": "You are given two arrays of length $$$n$$$: $$$a_1, a_2, \\dots, a_n$$$ and $$$b_1, b_2, \\dots, b_n$$$.You can perform the following operation any number of times: Choose integer index $$$i$$$ ($$$1 \\le i \\le n$$$); Swap $$$a_i$$$ and $$$b_i$$$. What is the minimum possible sum $$$|a_1 - a_2| + |a_2 - a_3| + \\dots + |a_{n-1} - a_n|$$$ $$$+$$$ $$$|b_1 - b_2| + |b_2 - b_3| + \\dots + |b_{n-1} - b_n|$$$ (in other words, $$$\\sum\\limits_{i=1}^{n - 1}{\\left(|a_i - a_{i+1}| + |b_i - b_{i+1}|\\right)}$$$) you can achieve after performing several (possibly, zero) operations?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 4000$$$)\u00a0\u2014 the number of test cases. Then, $$$t$$$ test cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$2 \\le n \\le 25$$$)\u00a0\u2014 the length of arrays $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$)\u00a0\u2014 the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\le b_i \\le 10^9$$$)\u00a0\u2014 the array $$$b$$$.", "output_spec": "For each test case, print one integer\u00a0\u2014 the minimum possible sum $$$\\sum\\limits_{i=1}^{n-1}{\\left(|a_i - a_{i+1}| + |b_i - b_{i+1}|\\right)}$$$.", "sample_inputs": ["3\n\n4\n\n3 3 10 10\n\n10 10 3 3\n\n5\n\n1 2 3 4 5\n\n6 7 8 9 10\n\n6\n\n72 101 108 108 111 44\n\n10 87 111 114 108 100"], "sample_outputs": ["0\n8\n218"], "notes": "NoteIn the first test case, we can, for example, swap $$$a_3$$$ with $$$b_3$$$ and $$$a_4$$$ with $$$b_4$$$. We'll get arrays $$$a = [3, 3, 3, 3]$$$ and $$$b = [10, 10, 10, 10]$$$ with sum $$$3 \\cdot |3 - 3| + 3 \\cdot |10 - 10| = 0$$$.In the second test case, arrays already have minimum sum (described above) equal to $$$|1 - 2| + \\dots + |4 - 5| + |6 - 7| + \\dots + |9 - 10|$$$ $$$= 4 + 4 = 8$$$.In the third test case, we can, for example, swap $$$a_5$$$ and $$$b_5$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n \r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my @B = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my $res = 0;\r\n for(my $i=1;$i<$n;$i++){\r\n $res += &min( abs($A[$i-1] - $A[$i]) + abs($B[$i-1] - $B[$i] ) ,\r\n abs($A[$i-1] - $B[$i]) + abs($B[$i-1] - $A[$i] ) );\r\n }\r\n print \"$res\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "negative_code": [], "src_uid": "94ec011dc830661c226bd860b9d70de5"} {"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 \u2014 to the first or to the fifth; from the sixth page \u2014 to the third or to the ninth; from the eighth \u2014 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$$$) \u2014 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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t$debug and print '-' x 15;\n\t$debug and print \"[$_]\";\n\tmy( $n, $x, $y, $d ) = split;\n\t\n\tmy @cand;\n\tmy $cand;\n\t\n\t$cand = abs( $x - $y );\n\t\n\tif( $cand % $d == 0 ){\n\t\tpush @cand, $cand / $d;\n\t\t}\n\t\n\t$cand = $y - 1;\n\t\n\tif( $cand % $d == 0 ){\n\t\tmy $add = ( $x - 1 ) / $d;\n\t\t$add =~ /\\./ and $add += 1;\n\t\t$add = int $add;\n\t\tpush @cand, $cand / $d + $add;\n\t\t}\n\t\n\t$cand = $n - $y;\n\t\n\tif( $cand % $d == 0 ){\n\t\tmy $add = ( $n - $x ) / $d;\n\t\t$add =~ /\\./ and $add += 1;\n\t\t$add = int $add;\n\t\tpush @cand, $cand / $d +$add;\n\t\t}\n\t\n\t$debug and print \"[@cand]\";\n\t\n\tprint !@cand ? -1 : (\n\t\tsort { $a <=> $b } \n\t\t@cand\n\t\t)[ 0 ]\n\t}"}], "negative_code": [], "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.\" \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of stewards with Jon Snow. Second line consists of n space separated integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009109) 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": "#!/usr/bin/perl\n\nmy $num = <>;\nchomp($num);\nmy $line2 = <>;\nchomp($line2);\nmy @tokens = split (\" \", $line2);\nmy @array = sort { $a <=> $b } @tokens;\nmy $min = $array[0];\nmy $max = $array[-1];\nmy $output = 0 ;\nfor (my $i=0;$i < $num;$i++) {\n #print \"$tokens[$i] -- $min -- $max\\n\";\n if ($tokens[$i] > $min && $tokens[$i] < $max) {\n $output++;\n } \n} \nprint \"$output\\n\";\n \n"}, {"source_code": "print 0 + grep $_ > $_[0] && $_ < $_[-1], @_ = sort { $a <=> $b } split ' ', (<>,<>)"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t\n\t@_ = sort { $a <=> $b } split ' ', <>;\n\tmy $min = $_[0];\n\tmy $max = $_[-1];\n\t\n\tmy $cnt = 0;\n\t\n\t$_ != $min and $_ != $max and $cnt ++ for @_;\n\t\n\tprint $cnt;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nmy $num = <>;\nchomp($num);\nmy $line2 = <>;\nchomp($line2);\nmy @tokens = split (\" \", $line2);\nmy @array = sort { $a <=> $b } @tokens;\nmy $min = $array[0];\nmy $max = $array[-1];\nmy $output = 0 ;\nfor (my $i=1;$i < $num-1;$i++) {\n #print \"$tokens[$i] -- $min -- $max\\n\";\n if ($tokens[$i] > $min && $tokens[$i] < $max) {\n $output++;\n }\n}\nprint \"$output\\n\";\n"}], "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\u2009\u00d7\u2009n, 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\u2009-\u20091, 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000) \u2014 the size of the map.", "output_spec": "In the first line print m \u2014 the minimum number of bombs Slava needs to destroy all tanks. In the second line print m integers k1,\u2009k2,\u2009...,\u2009km. 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = ();\n\t\n\tpush @_, grep !($_ % 2), 1 .. $_;\n\tpush @_, grep $_ % 2, 1 .. $_;\n\tpush @_, grep !($_ % 2), 1 .. $_;\n\t\n\tprint ~~ @_;\n\tprint \"@_\";\n\t}"}, {"source_code": "$_ = join( \n\t' ' . ( join ' ', grep $_ % 2 , 1 .. $_ ) . ' ',\n\t ( join ' ', grep !( $_ % 2), 1 .. $_ ) x 2\n\t),\n\nprint +( split ) . \"\\n$_\" for <>"}, {"source_code": "@_ = (\n\t\t( grep !( $_ % 2), 1 .. $_ ),\n\t\t( grep $_ % 2 , 1 .. $_ ),\n\t\t( grep !( $_ % 2), 1 .. $_ )\n\t),\n\nprint @_ . \"\\n@_\" for <>"}, {"source_code": "$_ = join( \n\t' ' . ( join ' ', grep $_ % 2 , 1 .. $_ ) . ' ',\n\t ( join ' ', grep !( $_ % 2), 1 .. $_ ) x 2\n\t),\n\t\nprint +( split ) . \"$/$_\" for <>"}], "negative_code": [], "src_uid": "c40cb0a89c2b604aa7384476b57e96b3"} {"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$$$) \u00a0\u2014 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": "use v5.20;\nsub { \n &{sub { say \"228 \" x <> }}\n while ($_[0]--);\n} -> (my $t = <>);"}], "negative_code": [], "src_uid": "f82058f6ba3ce0da15a5ce059674af35"} {"nl": {"description": "The Little Elephant has got a problem \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, \u2014 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 = ;\n@a = split( \" \", );\n@b = sort {$a <=> $b} @a; \n$cnt = 0;\nfor( $i=0;$i<$n;$i++ ){\n if( $a[$i] != $b[$i] )\n {\n $cnt++;\n }\n}\nif( $cnt < 3 )\n{\n print \"YES\";\n} else\n{\n print \"NO\";\n}"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nsub main {\n my $n = int ;\n my @arr = map int, split /\\D/, ;\n my @sorted_arr = sort { $a <=> $b } @arr;\n my $count = 0;\n for (0 .. $#arr) {\n\tif ($arr[$_] != $sorted_arr[$_]) {\n\t $count++;\n\t}\n }\n if ($count <= 2) {\n\tprint 'YES';\n } else {\n\tprint 'NO';\n }\n}\n\nmain();\n\n__END__\n"}], "negative_code": [{"source_code": "$n = ;\n@a = split( \" \", );\n@b = sort @a;\n$cnt = 0;\nfor( $i=0;$i<$n;$i++ ){\n if( $a[$i] != $b[$i] )\n {\n $cnt++;\n }\n}\nif( $cnt < 3 )\n{\n print \"YES\";\n} else\n{\n print \"NO\";\n}"}, {"source_code": "$n = ;\n@a = split( \" \", );\n@b = sort $a;\n$cnt = 0;\nfor( $i=0;$i<$n;$i++ ){\n if( $a[$i] != $b[$i] )\n {\n $cnt++;\n }\n}\nif( $cnt < 3 )\n{\n print \"YES\";\n} else\n{\n print \"NO\";\n}"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nsub badElems {\n my @a = @_;\n my @indexes;\n for my $i (1 .. $#a) {\n\tif ($a[$i] < $a[$i - 1]) {\n\t push(@indexes, $i);\n\t}\n }\n return @indexes;\n}\n\nsub check {\n my ($i1, $i2, @a) = @_;\n ($a[$i1], $a[$i2]) = ($a[$i2], $a[$i1]);\nreturn ( (($i1 == 0) || ($a[$i1-1] <= $a[$i1])) &&\n\t ($a[$i1] <= $a[$i1+1]) &&\n\t ($a[$i2-1] <= $a[$i2]) &&\n\t (($i2 == $#a) || ($a[$i2] <= $a[$i2+1])));\n}\n\nsub findFirstToSwap {\n my ($i2, @a) = @_;\n my $i = $i2-1;\n my $ai = $a[$i];\n do {\n\t$i--;\n } while (($i > 0) && ($a[$i] = $ai));\n return $i;\n}\n\nsub isGood {\n my @a = @_;\n my @indexes = badElems(@a);\n if (! @indexes) {\n\treturn 1;\n } elsif (@indexes > 2) {\n\treturn 0;\n } else { \n\tif (@indexes == 1) {\n\t unshift(@indexes, findFirstToSwap($indexes[0], @a));\n\t} else {\n\t $indexes[0]--;\n\t}\n\treturn check(@indexes, @a);\n }\n}\n\nsub main {\n my $n = int();\n my @a = map(int, split(/\\D/, ));\n if (isGood(@a)) {\n\tprint 'YES';\n } else {\n\tprint 'NO';\n }\n}\n\nmain();\n\n__END__\n\n4\n2 2 2 1\n"}, {"source_code": "<>;\n@a = <>;\n@r = sort {$a <= $b} @a;\n$k = 0;\nfor (0 .. $#a) {\n $k++ if ($a[$_] != $b[$_]);\n}\nif ($k < 2) {\n print 'YES';\n} else {\n print 'NO';\n}\n"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nsub badElems {\n my @a = @_;\n my @indexes;\n for my $i (1 .. $#a) {\n\tif ($a[$i] < $a[$i - 1]) {\n\t push(@indexes, $i);\n\t}\n }\n return @indexes;\n}\n\nsub check {\n my ($i1, $i2, @a) = @_;\n ($a[$i1], $a[$i2]) = ($a[$i2], $a[$i1]);\nreturn ( (($i1 == 0) || ($a[$i1-1] <= $a[$i1])) &&\n\t ($a[$i1] <= $a[$i1+1]) &&\n\t ($a[$i2-1] <= $a[$i2]) &&\n\t (($i2 == $#a) || ($a[$i2] <= $a[$i2+1])));\n}\n\nsub isGood {\n my @a = @_;\n my @indexes = badElems(@a);\n if (! @indexes) {\n\treturn 1;\n } elsif (@indexes > 2) {\n\treturn 0;\n } else { \n\tif (@indexes == 1) {\n\t unshift(@indexes, $indexes[0] - 1);\n\t} else {\n\t $indexes[0]--;\n\t}\n\treturn check(@indexes, @a);\n }\n}\n\nsub main {\n my $n = int();\n my @a = map(int, split(/\\D/, ));\n if (isGood(@a)) {\n\tprint 'YES';\n } else {\n\tprint 'NO';\n }\n}\n\nmain();\n\n__END__\n\n"}], "src_uid": "541fde3a3c40926cbd1edb6017b83e52"} {"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\u00a0\u2014 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 \u2009-\u20091. 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": "$_ = <>, chomp;\n$e = chop;\n@w = grep $_ < $e, @d = qw(0 2 4 6 8);\ns|[@w]|$e|||s|[@d](?=[^@d]*$)|$e|||--$f;\nprint $f || $_ . $&\n"}, {"source_code": "$_ = <>, chomp;\n$e = chop;\n@w = grep $_ < $e, @d = qw(0 2 4 6 8);\ns|[@w]|$e|||s|[@d](?=[^@d]*$)|$e|||--$f;\nprint $f || $_ . $&\n"}, {"source_code": "$_ = <>, chomp;\n$e = chop;\n@w = grep $_ < $e, @d = qw(0 2 4 6 8);\ns|[@w]|$e|||s|[@d](?=[^@d]*$)|$e|||--$f;\nprint $f || $_ . $&"}, {"source_code": "$_ = <>, chomp;\n$e = chop;\n@w = grep $_ < $e, @d = qw(0 2 4 6 8);\ns|[@w]|$e|||s|[@d](?=[^@d]*$)|$e|||--$f;\nprint $f || $_ . $&\n"}, {"source_code": "$_ = <>, chomp;\n$e = chop;\n@w = grep $_ < $e, @d = qw(0 2 4 6 8);\ns|[@w]|$e|||s|[@d](?=[^@d]*$)|$e|||--$f;\nprint $f || $_ . $&\n"}, {"source_code": "use v5.10;\n\nchomp($line = <>);\n@a = split //, $line;\n$n = scalar @a;\n$x = $a[$#a];\nforeach $i (0 .. $n-1) {\n\t$a[$i]&1 and next;\n\tif ($a[$i] < $x) {\n\t\t$p = $i;\n\t\tlast;\n\t} elsif ($a[$i] > $x) {\n\t\t$p = $i;\n\t}\n}\n\nif (defined $p) {\n\tforeach $i (0 .. $n-2) {\n\t\tif ($i == $p) {\n\t\t\t$ans .= $x;\n\t\t} else {\n\t\t\t$ans .= $a[$i];\n\t\t}\n\t}\n\t$ans .= $a[$p];\n\tsay $ans;\n} else {\n\tsay -1;\n}"}, {"source_code": "$_ = <>, chomp;\n$e = chop;\n@w = grep $_ < $e, @d = qw(0 2 4 6 8);\ns|[@w]|$e|||s|[@d](?=[^@d]*$)|$e|||--$f;\nprint $f || $_ . $&\n"}, {"source_code": "$_ = <>, chomp;\n$e = chop;\n@w = grep $_ < $e, @d = qw(0 2 4 6 8);\ns|[@w]|$e|||s|[@d](?=[^@d]*$)|$e|||--$f;\nprint $f || $_ . $&\n"}, {"source_code": "$_ = <>, chomp;\n$e = chop;\n@w = grep $_ < $e, @d = qw(0 2 4 6 8);\ns|[@w]|$e|||s|[@d](?=[^@d]*$)|$e|||--$f;\nprint $f || $_ . $&\n"}, {"source_code": "$\\ = $/;\nwhile(<>){\n\tchomp;\n\t$e = chop;\n\t@e = split//;\n\t$f = /[02468]/;\n\tundef $j;\n\tfor $i (@e){\n\t\tif ($i % 2 == 0){ $k = $i }\n\t\tif ($i % 2 == 0 and $i < $e){\n\t\t\t$j = $i; \n\t\t\t$i = $e;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\t\n\t$j // $f && do { substr $_, (rindex $_, $k), 1, $e; @e = split//; $j = $k };\n\tprint $f ? (@e, $j) : -1\n\n\t}"}, {"source_code": "$_ = <>, chomp;\n$e = chop;\n@w = grep $_ < $e, @d = qw(0 2 4 6 8);\ns|[@w]|$e|||s|[@d](?=[^@d]*$)|$e|||--$f;\nprint $f || $_ . $&"}, {"source_code": "$_ = <>, chomp;\n$e = chop;\n@w = grep $_ < $e, @d = qw(0 2 4 6 8);\ns|[@w]|$e|||s|[@d](?=[^@d]*$)|$e|||--$f;\nprint $f || $_ . $&\n"}], "negative_code": [{"source_code": "$_ = <>, chomp;\n$e = chop;\n@w = grep $_ < $e, @d = qw(0 2 4 6 8);\ns|[@w]|$e|||s|[@d]|$e|||--$f;\nprint $f || $_ . $&"}], "src_uid": "bc375e27bd52f413216aaecc674366f8"} {"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\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 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,\u2009a2,\u2009...,\u2009an (16\u2009\u2264\u2009ai\u2009\u2264\u200932768); number ai denotes the maximum data transfer speed on the i-th computer.", "output_spec": "Print a single integer \u2014 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": "($n, $k) = split /\\s+/, <>;\n@a = sort{$a <=> $b} split /\\s+/, <>;\nprint $a[$n - $k].\"\\n\";\n"}, {"source_code": "<>=~/ /;\nprint ((sort{$b<=>$a}(split/ /,<>))[$'-1]);\n"}, {"source_code": "#Scott Heinrich - A01273823\nuse strict;\nuse warnings;\nuse 5.010;\n\nmy $line1 = <>;\nchomp($line1);\nmy @nk = split(\" \",$line1);\nmy $n = $nk[0]; #computers\nmy $k = $nk[1]; #participants\n\nmy $line2 = <>;\nchomp($line2);\nmy @a = split(\" \",$line2); #max data transfer speed on ith computer\n\nmy @sorted = sort{ $b <=> $a } @a;\n\n#n is basically useless, use k for index\n\nsay $sorted[$k-1];\n"}, {"source_code": "<>=~/ /;\nprint ((sort{$b<=>$a}(split/ /,<>))[$'-1]);\n"}, {"source_code": "<>=~/ /;\nprint ((sort{$b<=>$a}(split/ /,<>))[$'-1]);"}, {"source_code": "<>=~/ /;\nprint ((sort{$b<=>$a}(split/ /,<>))[$'-1]);\n"}, {"source_code": "use strict;\nuse POSIX;\nmy ($n,$k)=split(' ',<>);\nmy @l = split (' ',<>);\n@l = sort{$b<=>$a} @l;\nprint $l[$k-1];\n\n"}, {"source_code": "<>=~/ /;\nprint ((sort{$b<=>$a}(split/ /,<>))[$'-1]);\n"}, {"source_code": "<>=~/ /;\nprint ((sort{$b<=>$a}(split/ /,<>))[$'-1]);\n"}, {"source_code": "<>=~/ /;\nprint ((sort{$b<=>$a}(split/ /,<>))[$'-1]);\n"}, {"source_code": "<>=~/ /;\nprint ((sort{$b<=>$a}(split/ /,<>))[$'-1]);\n"}, {"source_code": "<>=~/ /;\nprint ((sort{$b<=>$a}(split/ /,<>))[$'-1]);"}, {"source_code": "<>=~/ /;\nprint ((sort{$b<=>$a}(split/ /,<>))[$'-1]);\n"}], "negative_code": [{"source_code": "use strict;\nuse POSIX;\nmy ($n,$k)=split(' ',<>);\nmy @l = split (' ',<>);\n@l = sort @l;\n\nprint $l[$k-1]\n"}], "src_uid": "6eca08d7cc2dec6f4f84d3faa9a8a915"} {"nl": {"description": "Valera has n counters numbered from 1 to n. Some of them are connected by wires, and each of the counters has a special button.Initially, all the counters contain number 0. When you press a button on a certain counter, the value it has increases by one. Also, the values recorded in all the counters, directly connected to it by a wire, increase by one.Valera and Ignat started having a dispute, the dispute is as follows. Ignat thought of a sequence of n integers a1,\u2009a2,\u2009...,\u2009an. Valera should choose some set of distinct counters and press buttons on each of them exactly once (on other counters the buttons won't be pressed). If after that there is a counter with the number i, which has value ai, then Valera loses the dispute, otherwise he wins the dispute.Help Valera to determine on which counters he needs to press a button to win the dispute.", "input_spec": "The first line contains two space-separated integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009105), that denote the number of counters Valera has and the number of pairs of counters connected by wires. Each of the following m lines contains two space-separated integers ui and vi (1\u2009\u2264\u2009ui,\u2009vi\u2009\u2264\u2009n,\u2009ui\u2009\u2260\u2009vi), that mean that counters with numbers ui and vi are connected by a wire. It is guaranteed that each pair of connected counters occurs exactly once in the input. The last line contains n space-separated integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009105), where ai is the value that Ignat choose for the i-th counter.", "output_spec": "If Valera can't win the dispute print in the first line -1. Otherwise, print in the first line integer k (0\u2009\u2264\u2009k\u2009\u2264\u2009n). In the second line print k distinct space-separated integers \u2014 the numbers of the counters, where Valera should push buttons to win the dispute, in arbitrary order. If there exists multiple answers, you are allowed to print any of them.", "sample_inputs": ["5 5\n2 3\n4 1\n1 5\n5 3\n2 1\n1 1 2 0 2", "4 2\n1 2\n3 4\n0 0 0 0"], "sample_outputs": ["2\n1 2", "3\n1 3 4"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp($_ = <>);\nmy ($n, $m) = split;\nmy @e = ();\n\nfor (1 .. $m) {\n chomp($_ = <>);\n my ($u, $v) = split;\n ($u, $v) = ($u - 1, $v - 1);\n push @{$e[$u]}, $v;\n push @{$e[$v]}, $u;\n}\nchomp($_ = <>);\nmy @a = split;\n\nmy @q = ();\nmy @d = (0) x $n;\nmap { push @q, $_ if $a[$_] == 0 } 0 .. $n - 1;\nmy %ans = ();\n\nwhile (@q) {\n my %curq = ();\n for my $i (@q) {\n $ans{$i} = 1;\n for my $j (@{$e[$i]}) {\n $curq{$j} = 1;\n ++$d[$j];\n }\n }\n my @newq = ();\n for my $i (keys %curq) {\n push @newq, $i if $d[$i] == $a[$i];\n }\n @q = @newq;\n}\n\nmy @res = map { ++$_; }keys %ans;\nprint scalar @res, \"\\n\";\nprint \"@res\\n\";\n"}], "negative_code": [], "src_uid": "5c64671081f9f5332d0ad57503091a54"} {"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$$$) \u2014 the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $A = '0' x 101;\n\t\n\tsubstr $A, $_, 1, 1 for @_;\n\t\n\tprint $A =~ /11/ ? 2 : 1;\n\t}"}], "negative_code": [], "src_uid": "dd2cd365d7afad9c2b5bdbbd45d87c8a"} {"nl": {"description": "The only difference between the two versions is that this version asks the maximal possible answer.Homer likes arrays a lot. Today he is painting an array $$$a_1, a_2, \\dots, a_n$$$ with two kinds of colors, white and black. A painting assignment for $$$a_1, a_2, \\dots, a_n$$$ is described by an array $$$b_1, b_2, \\dots, b_n$$$ that $$$b_i$$$ indicates the color of $$$a_i$$$ ($$$0$$$ for white and $$$1$$$ for black).According to a painting assignment $$$b_1, b_2, \\dots, b_n$$$, the array $$$a$$$ is split into two new arrays $$$a^{(0)}$$$ and $$$a^{(1)}$$$, where $$$a^{(0)}$$$ is the sub-sequence of all white elements in $$$a$$$ and $$$a^{(1)}$$$ is the sub-sequence of all black elements in $$$a$$$. For example, if $$$a = [1,2,3,4,5,6]$$$ and $$$b = [0,1,0,1,0,0]$$$, then $$$a^{(0)} = [1,3,5,6]$$$ and $$$a^{(1)} = [2,4]$$$.The number of segments in an array $$$c_1, c_2, \\dots, c_k$$$, denoted $$$\\mathit{seg}(c)$$$, is the number of elements if we merge all adjacent elements with the same value in $$$c$$$. For example, the number of segments in $$$[1,1,2,2,3,3,3,2]$$$ is $$$4$$$, because the array will become $$$[1,2,3,2]$$$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $$$0$$$.Homer wants to find a painting assignment $$$b$$$, according to which the number of segments in both $$$a^{(0)}$$$ and $$$a^{(1)}$$$, i.e. $$$\\mathit{seg}(a^{(0)})+\\mathit{seg}(a^{(1)})$$$, is as large as possible. Find this number.", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq n$$$).", "output_spec": "Output a single integer, indicating the maximal possible total number of segments.", "sample_inputs": ["7\n1 1 2 2 3 3 3", "7\n1 2 3 4 5 6 7"], "sample_outputs": ["6", "7"], "notes": "NoteIn the first example, we can choose $$$a^{(0)} = [1,2,3,3]$$$, $$$a^{(1)} = [1,2,3]$$$ and $$$\\mathit{seg}(a^{(0)}) = \\mathit{seg}(a^{(1)}) = 3$$$. So the answer is $$$3+3 = 6$$$.In the second example, we can choose $$$a^{(0)} = [1,2,3,4,5,6,7]$$$ and $$$a^{(1)}$$$ is empty. We can see that $$$\\mathit{seg}(a^{(0)}) = 7$$$ and $$$\\mathit{seg}(a^{(1)}) = 0$$$. So the answer is $$$7+0 = 7$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\nwhile(<>){\r\n\t$_ = <>;\r\n\t\r\n\t1 while s/\r\n\t\t\\b(\\d+)\r\n\t\t[ ]\\1\\b\r\n\t\t\\K\r\n\t\t(?:[ ]\\1\\b)+\r\n\t\t/ ''\r\n\t\t/xeg;\r\n\t\r\n\ts/\r\n\t\t\\b(\\d+)\r\n\t\t(?:[ ]\\1[ ]\\d+)*\r\n\t\t(?:[ ]\\1[ ]\\d+)*\r\n\t\t[ ]\\1\\K\r\n\t\t[ ]\\d+\r\n\t\t(?=\r\n\t\t\t[ ]\\1[ ]\\1\\b\r\n\t\t\t)\r\n\t\t//gx;\r\n\t\r\n\tprint 0 + split\r\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\nwhile(<>){\r\n\t$_ = <>;\r\n\t\r\n\t1 while s/\r\n\t\t\\b(\\d+)\r\n\t\t[ ]\\1\\b\r\n\t\t\\K\r\n\t\t(?:[ ]\\1\\b)+\r\n\t\t//xg;\r\n\t\r\n\ts/\r\n\t\t\\b(\\d+)\r\n\t\t[ ]\\1\r\n\t\t(?: (?:[ ]\\d+[ ]\\1)*+ )*\r\n\t\t\\K\r\n\t\t[ ]\\1\\b\r\n\t\t//gx;\r\n\t\r\n\tprint 0 + split\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\t$_ = join ' ', @_;\n\t\n\t1 while s/\n\t\t(\\b\\d+\\b)\n\t\t[ ]\\b\\1\\b\n\t\t\\K\n\t\t(?:[ ]\\b\\1\\b)+\n\t\t/ ''\n\t\t/xeg;\n\t\n\tprint 0 + split;\n\t}"}], "src_uid": "55f0ecc597e6e40256a14debcad1b878"} {"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$$$\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t( $n, $k, @_, $F, $P ) = split ' ', $_ . <>;\r\n\t\r\n\twhile( $k -- ){\r\n\t\t$ok = 0;\r\n\t\t\r\n\t\tfor( 0 .. @_ - 2 ){\r\n\t\t\tif( $_[ $_ ] < $_[ $_ + 1 ] ){\r\n\t\t\t\t$_[ $_ ] += $ok = 1;\r\n\t\t\t\t$P = $_ + 1;\r\n\t\t\t\tlast\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\r\n\t\t$ok or $F = 1 and last\r\n\t\t}\r\n\t\r\n\tprint $F ? -1 : $P\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t@_ = split ' ', <>;\n\t\n\tmy $fail = 0;\n\tmy $pos = 0;\n\t\n\twhile( $k -- ){\n\t\tmy $ok = 0;\n\t\t\n\t\tfor my $i ( 0 .. @_ - 2 ){\n\t\t\tif( $_[ $i ] < $_[ $i + 1 ] ){\n\t\t\t\t$_[ $i ] ++;\n\t\t\t\t$pos = $i + 1;\n\t\t\t\t$ok = 1;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t$ok or $fail = 1;\n\t\tlast if $fail;\n\t\t}\n\t\n\tprint $fail ? -1 : $pos;\n\t}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$k) = map { $_ - 0 } split(/\\s+/,);\r\n my @h = map { $_ - 0 } split(/\\s+/,);\r\n my $mx_h = -1;\r\n for(my $i=0;$i<$n;$i++){\r\n $mx_h = $h[$i] if $h[$i] > $mx_h;\r\n }\r\n my $fill = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n $fill += ( $mx_h - $h[$i] );\r\n }\r\n if( $n==1 or $k > $fill ){\r\n print \"-1\\n\"; next;\r\n }\r\n \r\n my $w = 0;\r\n my $last_stop = -1;\r\n for(my $i=0;$i<$k;$i++){\r\n for(my $i=0;$i<$n-1;$i++){\r\n if( $h[$i] >= $h[$i+1] ){\r\n if( $i == $n-2 ){\r\n $w = 1;\r\n }\r\n } else {\r\n $h[$i] ++;\r\n $last_stop = $i;\r\n last;\r\n }\r\n }\r\n }\r\n if( $w == 1 ){\r\n print \"-1\\n\";\r\n } else {\r\n $last_stop ++;\r\n print \"$last_stop\\n\";\r\n }\r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [], "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\u2009\u2264\u2009t\u2009\u2264\u200910 for pretests and tests or t\u2009=\u20091 for hacks; see the Notes section for details) \u2014 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,\u2009k (2\u2009\u2264\u2009n\u2009\u2264\u2009100,\u20091\u2009\u2264\u2009k\u2009\u2264\u200926) \u2014 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": "$\\ = $/;\n\n@regexes = split /^$/m, join '', ;\n\nwhile ($t = <>){\n\t\n\tfor (1 .. $t){\n\t\t\n\t\t($length, undef) = split ' ' , <>;\n\t\t\n\t\t@_[ 0 .. 2 ] = map scalar <>, 0 .. 2; # main array\n\t\t\n\t\t$i = -1;\n\t\t$i ++, /^s/ and $s = $i for @_;\n\t\t@test_lines = ($s);\n\t\t\n\t\ts/.// for @_;\n\t\ts/$/....../ for @_;\n\n\t\t$fail = 0;\n\t\t\n\t\tfor $i (0 .. $length / 3){\n\t\t\t\n\t\t\t$_3x3 = join '', map { s/...//; $& } @_;\n\t\t\t\n\t\t\t$new_test_lines = '';\n\t\t\tfor $k (@test_lines){\n\t\t\t\t\n\t\t\t\t$k == 0 and $_3x3 =~ /$regexes[0]/x and $new_test_lines .= 0;\n\t\t\t\t$k == 0 and $_3x3 =~ /$regexes[1]/x and $new_test_lines .= 1;\n\t\t\t\t\n\t\t\t\t$k == 1 and $_3x3 =~ /$regexes[2]/x and $new_test_lines .= 0;\n\t\t\t\t$k == 1 and $_3x3 =~ /$regexes[3]/x and $new_test_lines .= 1;\n\t\t\t\t$k == 1 and $_3x3 =~ /$regexes[4]/x and $new_test_lines .= 2;\n\t\t\t\t\n\t\t\t\t$k == 2 and $_3x3 =~ /$regexes[5]/x and $new_test_lines .= 1;\n\t\t\t\t$k == 2 and $_3x3 =~ /$regexes[6]/x and $new_test_lines .= 2;\n\n\t\t\t\t}\n\t\t\t$new_test_lines = join '', sort split //, $new_test_lines;\n\t\t\t$new_test_lines =~ y///cs; # sqeeze\n\t\t\t@test_lines = split //, $new_test_lines;\n\t\t\t@test_lines or $fail = 1 and last;\n\t\t\t\n\t\t\t}\n\t\t\t\n\t\tprint qw(YES NO)[ $fail ];\n\t\t}\n\t\n\t}\n\t\n__DATA__\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"}, {"source_code": "$\\ = $/;\n\nwhile ($t = <>){\n\t\n\tfor (1 .. $t){\n\t\t\n\t\t$w = <>;\n\t\t\n\t\t@_[ 0 .. 2 ] = map scalar <>, 0 .. 2;\n\t\t\n\t\t$i = -1;\n\t\t$i ++, /^s/ and $s = $i for @_;\n\t\ts/.// for @_;\n\t\ts/$/....../ for @_;\n\n\t\t$f = 0;\n\t\t\n\t\t@try = ($s);\n\t\t\n\t\tfor $i (0 .. $w / 3){\n\t\t\t\n\t\t\t@three = map { s/...//; $& } @_;\n\t\t\tundef @new;\n\t\t\t\n\t\t\tfor $k (@try){\n\t\t\t\t\n\t\t\t\tnext if $three[$k] =~ /^\\w/; \n\t\t\t\t\n\t\t\t\tfor $r (grep /^[012]/, map $k + $_, -1 .. 1){\n\t\t\t\t\t$three[$r] =~ /\\.{3}/ and $new[$r] = $r;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t@try = grep defined, @new \n\t\t\t\tor $f = 1;\n\t\t\t}\n\t\t\t\n\t\tprint qw(YES NO)[ $f ];\n\t\t}\n\t\t\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile ($t = <>){\n\t\n\tfor (1 .. $t){\n\t\t\n\t\t($w, $c) = split ' ' , <>;\n\t\t\n\t\t@_[ 0 .. 2 ] = map scalar <>, 0 .. 2;\n\t\t\n\t\t$i = -1;\n\t\t$i ++, /^s/ and $s = $i for @_;\n\t\ts/.// for @_;\n\t\ts/$/.../ for @_;\n\t\t\n\t\t$f = 0;\n\t\t\n\t\t%h = qw(0 01 01 012 012 012 1 012 12 012 2 12 02 012);\n\t\t\n\t\t@wa = ($s);\n\t\t\n\t\tfor $i (0 .. $w / 3){\n\t\t\t\n\t\t\t@three = map { s/...//; $& } @_;\n\t\t\tfor $k (@wa){\n\t\t\t\t$three[$k] =~ /^\\./ or next;\n\t\t\t\t$wa = $h{ $k };\n\t\t\t\t$aw = '';\n\t\t\t\t$j = -1;\n\t\t\t\tmap { $j ++; $wa =~ /$j/ and /^\\.{3}/ and $aw .= $j } @three;\n\t\t\t\t}\n\t\t\t$aw = join '', sort split //, $aw;\n\t\t\t$aw =~ y///cs;\n\t\t\t@wa = split //, $aw;\n\t\t\t@wa or $f = 1;\n\t\t\t$aw = '';\n\t\t\t}\n\t\t\t\n\t\tprint qw(YES NO)[ $f ];\n\t\t}\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile ($t = <>){\n\t\n\tfor (1 .. $t){\n\t\t\n\t\t($w, $c) = split ' ' , <>;\n\t\t$w --;\n\t\t$w ++ while $w % 3;\n\t\t\n\t\t@_[ 0 .. 2 ] = map scalar <>, 0 .. 2;\n\t\t\n\t\t$i = -1;\n\t\t$i ++, /^s/ and $s = $i for @_;\n\t\ts/.// for @_;\n\n\t\t$f = 0;\n\t\t\n\t\t@wa = ($s);\n\t\t\n\t\tfor $i (1 .. $w / 3){\n\t\t\t\n\t\t\t$three = join '', map { s/...//; $& } @_;\n\t\t\t\n\t\t\t$aw = '';\n\t\t\tfor $k (@wa){\n\t\t\t\t$k == 0 and $three =~ /^\\.\\.\\./ and $aw .= 0;\n\t\t\t\t$k == 1 and $three =~ /^...\\.\\.\\./ and $aw .= 1;\n\t\t\t\t$k == 2 and $three =~ /^......\\.\\.\\./ and $aw .= 2;\n\t\t\t\t$k == 0 and $three =~ /^\\...\\.\\.\\./ and $aw .= 1;\n\t\t\t\t$k == 1 and $three =~ /^\\.\\.\\.\\.../ and $aw .= 0;\n\t\t\t\t$k == 1 and $three =~ /^...\\...\\.\\.\\./ and $aw .= 2;\n\t\t\t\t$k == 2 and $three =~ /^...\\.\\.\\.\\.../ and $aw .= 1;\n\t\t\t\t}\n\t\t\t$aw = join '', sort split //, $aw;\n\t\t\t$aw =~ y///cs;\n\t\t\t@wa = split //, $aw;\n\t\t\t@wa or $f = 1;\n\t\t\t}\n\t\t\t\n\t\tprint qw(YES NO)[ $f ];\n\t\t}\n\t\n\t\n\t\n\t\n\t}"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000,\u20091\u2009\u2264\u2009k\u2009\u2264\u2009h\u2009\u2264\u2009109)\u00a0\u2014 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\u2009\u2264\u2009ai\u2009\u2264\u2009h)\u00a0\u2014 the heights of the pieces.", "output_spec": "Print a single integer\u00a0\u2014 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\u00b75\u2009=\u200910 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds."}, "positive_code": [{"source_code": "local $/; ($n, $h, $k, @a) = split \" \", <>;\nwhile ($i < $n) {\n\twhile ($l + $a[$i] <= $h) {\n\t\t$l += $a[$i]; \n\t\t$i++;\n\t\tlast if $i == $n;\n\t}\n\tif ($l > $k) {\n\t\t$t = int($l/$k);\n\t} else {\n\t\t$t = 1;\n\t}\n\t$l -= $k * $t;\n\t$l = 0 if $l < 0;\n\t$T += $t;\n} \n$T += int(($l + $k - 1)/$k);\nprint $T"}, {"source_code": "$\\ = $/;\nwhile(<>){\n\t($n, $h, $k) = split;\n\t@_ = split ' ', <>;\n\t$sec = 0;\n\t$stack = 0;\n\twhile( @_ or $stack ){\n\t\twhile( @_ and $stack + $_[0] <= $h ){\n\t\t\t$stack += shift @_;\n\t\t\t}\n\t\t\n\t\tint( $stack / $k ) or do { $stack = 0; $sec ++ };\n\t\t$sec += int( $stack / $k );\n\t\t$stack %= $k;\n\t\t}\n\tprint 0 + $sec\n}"}], "negative_code": [{"source_code": "$\\ = $/;\nwhile(<>){\n\t($n, $h, $k) = split;\n\t@_ = split ' ', <>;\n\t$sec = 0;\n\t$stack = 0;\n\twhile( @_ ){\n\t\twhile( @_ and $stack + $_[0] <= $h ){\n\t\t\t$stack += shift @_;\n\t\t\t}\n\t\t\n\t\t$stack -= $k;\n\t\t$stack < 0 and $stack = 0;\n\t\t$sec ++;\n\t\t}\n\tprint ++ $sec\n}"}], "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 \u2014 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\u2009\u2264\u2009x\u2009\u2264\u20094000) \u2014 the round Sereja is taking part in today, and k (0\u2009\u2264\u2009k\u2009<\u20094000) \u2014 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\u2009-\u2009num2\u2009=\u20091. 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 \u2014 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": "@_=(0)x<>;\ns/ (\\d+)/$_[$1]++/ge for <>;\n$_=join\"\",@_;\ns/.//;\nprint ((()=/00|0/g).\" \".(()=/0/g))\n\n"}, {"source_code": "@_=(0)x<>;\ns/ (\\d+)/$_[$1]++/ge for <>;\n$_=join\"\",@_;\ns/.//;\nprint ((()=/00|0/g).\" \".(()=/0/g))\n\n"}, {"source_code": "@_=(0)x<>;\ns/ (\\d+)/$_[$1]++/ge for <>;\n$_=join\"\",@_;\ns/.//;\nprint ((()=/00|0/g).\" \".(()=/0/g))\n"}, {"source_code": "@_=(0)x<>;\ns/ (\\d+)/$_[$1]++/ge for <>;\n$_=join\"\",@_;\ns/.//;\nprint ((()=/00|0/g).\" \".(()=/0/g))\n\n"}, {"source_code": "@_=(0)x<>;\ns/ (\\d+)/$_[$1]++/ge for <>;\n$_=join\"\",@_;\ns/.//;\nprint ((()=/00|0/g).\" \".(()=/0/g))\n\n"}, {"source_code": "@_=(0)x<>;\ns/ (\\d+)/$_[$1]++/ge for <>;\n$_=join\"\",@_;\ns/.//;\nprint ((()=/00|0/g).\" \".(()=/0/g))\n\n"}, {"source_code": "@_=(0)x<>;\ns/ (\\d+)/$_[$1]++/ge for <>;\n$_=join\"\",@_;\ns/.//;\nprint ((()=/00|0/g).\" \".(()=/0/g))\n\n"}, {"source_code": "@_=(0)x<>;\ns/ (\\d+)/$_[$1]++/ge for <>;\n$_=join\"\",@_;\ns/.//;\nprint ((()=/00|0/g).\" \".(()=/0/g))\n\n"}, {"source_code": "use integer;\n\nwhile(<>){\n\t\n\t($x,$k)=split/ /;\n\tchomp $k;\n\t@_=(0)x($x);\n\tfor $i(1..$k){\n\t\t$_=<>;\n\t\t@num=split/ /;\n\t\tshift @num;\n\t\twhile (@num){\n\t\t\t$_[pop @num]++;\n\t\t\t}\n\t\t\n\t\t}\n#\tprint \"@_\\n\";\n\tshift @_;\n\t$_=join\"\",@_;\n\t$a=$_;\n\t$sum=0;\n\ts#0+#$sum+=((length$&)/2+((length$&)%2?1:0))#eg;\n\t$_=$a;\n\tprint \"$sum \";\n\tprint ($b=()=/0/g);\n\tprint \"\\n\";\n\t}\n\t\n"}, {"source_code": "@_=(0)x<>;\ns/ (\\d+)/$_[$1]++/ge for <>;\n$_=join\"\",@_;\ns/.//;\nprint ((()=/00|0/g).\" \".(()=/0/g))\n"}, {"source_code": "($x,$k)=split/ /,<>;\n@_=(0)x$x;\n$_=join\"\",<>;\ns/ (\\d+)/$_[$1]++/gem;\n$_=join\"\",@_;\ns/.//;\n$b=()=/0/g;\ns/00/0/g;\n$b=(()=/0/g).\" $b\";\nprint \"$b\\n\";\n"}, {"source_code": "@_=(0)x<>;\ns/ (\\d+)/$_[$1]++/ge for <>;\n$_=join\"\",@_;\ns/.//;\nprint ((()=/00|0/g).\" \".(()=/0/g))\n\n"}], "negative_code": [], "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\u2009=\u20091, exit the function. Otherwise, call f(x\u2009-\u20091), and then make swap(ax\u2009-\u20091,\u2009ax) (swap the x-th and (x\u2009-\u20091)-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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the size of permutation.", "output_spec": "In a single line print n distinct integers from 1 to n \u2014 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": "#!perl -p\n@_=1..$_-1;$_.=\"@_\"\n"}, {"source_code": "#!perl -p\n@_=1..$_-1;$_=\"$_ @_\";\n"}, {"source_code": "$n = ;\nprint $n;\nfor( $i=1;$i<$n;$i++ ){\n print \" \".$i;\n}"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: little_elephant.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 05/26/2015 07:38:44 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.012;\nchomp(my $n = );\nprint join(\" \", ($n, 1..($n-1)));\nprint \"\\n\";\n\n"}, {"source_code": "#!/bin/perl\nuse strict;\nuse warnings;\n\nsub main () {\n\tmy $n = int ;\n\tprint \"$n\";\n\tfor ( 1 .. $n - 1 ) {\n\t\tprint \" $_\";\n\t}\n\tprint \"\\n\";\n\texit 0;\n}\n\nmain();\n\n__END__\n\n"}, {"source_code": "@_=(1..<>);\nprint pop @_; \n@_ and print \" @_\""}], "negative_code": [{"source_code": "#!perl -p\n@_=2..$_;$_=\"@_ 1\";\n"}, {"source_code": "@_=(2..<>,1); \nprint \"@_\""}], "src_uid": "d9ba1dfe11cf3dae177f8898f3abeefd"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009105). The next line contains n integers ti (1\u2009\u2264\u2009ti\u2009\u2264\u2009109), separated by spaces.", "output_spec": "Print a single number \u2014 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,\u20092,\u20093,\u20095,\u200915. Thus, you can make everything feel not disappointed except for the person with time 5."}, "positive_code": [{"source_code": "<>;\n\t$_ < $z or ($z += $_, ++ $a)\n\tfor sort {$a <=> $b} split \" \", <>;\nprint $a\n"}, {"source_code": "<>;\n\t$_ < $z or ($z += $_, ++ $a)\n\tfor sort {$a <=> $b} split \" \", <>;\nprint $a\n"}, {"source_code": "<>;\n\t$_ < $z or ($z += $_, ++ $a)\n\tfor sort {$a <=> $b} split \" \", <>;\nprint $a\n"}, {"source_code": "<>;\n\t$_ < $z or ($z += $_, ++ $a)\n\tfor sort {$a <=> $b} split \" \", <>;\nprint $a\n"}, {"source_code": "use v5.10;\n\nchomp($n = <>);\n@a = sort {$a <=> $b} split / /, <>;\n($ans, $tot) = (0, 0);\n$tot<=$_ and ++$ans and $tot+=$_ foreach (@a);\nsay $ans;"}, {"source_code": "<>;\n\t$_ < $z or ($z += $_, ++ $a)\n\tfor sort {$a <=> $b} split \" \", <>;\nprint $a\n"}, {"source_code": "<>;\n\t$_ < $z or ($z += $_, ++ $a)\n\tfor sort {$a <=> $b} split \" \", <>;\nprint $a"}, {"source_code": "<>;\n\t$_ < $z or ($z += $_, ++ $a)\n\tfor sort {$a <=> $b} split \" \", <>;\nprint $a\n"}, {"source_code": "<>;\n\t$_ < $z or ($z += $_, ++ $a)\n\tfor sort {$a <=> $b} split \" \", <>;\nprint $a\n"}, {"source_code": "$\\ = $/;\nwhile($n = <>){\n\t\n\t@_ = sort {$a <=> $b} split \" \", <>;\n\t$ans = 1;\n\t$sum = $_[ 0 ];\n\t$e = 10e9;\n\tfor $i (1 .. @_ - 1){\n\t\t$ans += $a = $sum <= $_[ $i ];\n\t\t$a and $sum += $_[ $i ];\n\t\t$sum > $e and last;\n\t\t}\n\t\t\n\tprint $ans;\n\t\n}"}, {"source_code": "<>;\n\t$_ < $z or ($z += $_, ++ $a)\n\tfor sort {$a <=> $b} split \" \", <>;\nprint $a"}, {"source_code": "<>;\n\t$_ < $z or ($z += $_, ++ $a)\n\tfor sort {$a <=> $b} split \" \", <>;\nprint $a\n"}], "negative_code": [{"source_code": "use v5.10;\n\nchomp($n = <>);\n@a = sort {$a <=> $b} split / /, <>;\n($ans, $tot) = (0, 0);\nforeach (@a) {\n\t$tot <= $_ and ++$ans;\n\t$tot += $_;\n}\nsay $ans;"}, {"source_code": "$\\ = $/;\nwhile($n = <>){\n\t\n\t@_ = sort {$a <=> $b} split \" \", <>;\n\t$i = 0;\n\tpush @a, $i += $_ for @_;\n\tshift @_;\n\t$ans = 1;\n\t$e = 10e9;\n\t(($a = shift @a) > $e and last) , $ans += $_ >= $a for @_;\n\t\n\tprint $ans;\n}"}], "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 \u2014 circle of radius r\u2009-\u2009d 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\u00a0-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\u2009\u2264\u2009d\u2009<\u2009r\u2009\u2264\u2009500)\u00a0\u2014 the radius of pizza and the width of crust. Next line contains one integer number n\u00a0\u2014 the number of pieces of sausage (1\u2009\u2264\u2009n\u2009\u2264\u2009105). Each of next n lines contains three integer numbers xi, yi and ri (\u2009-\u2009500\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009500, 0\u2009\u2264\u2009ri\u2009\u2264\u2009500), where xi and yi are coordinates of the center of i-th peace of sausage, ri\u00a0\u2014 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": "#!/usr/bin/perl\n$line = ; \n@radios = split(/\\s+/, $line);\n$r = $radios[0]; \n$d = $radios[1];\nmy $N= - 1;\n$dat = $N;\n@distan=();\n@rad=();\nwhile ($dat >= 0){\n\t$ent = ;\n\t@datos = split(/\\s+/, $ent);\n\t$Tempd = sqrt(($datos[0] * $datos[0])+($datos[1] * $datos[1]));\n\tpush (@distan, $Tempd);\n\tpush (@rad, $datos[2]);\n\t$dat = $dat-1;\n#\tprint \"@distan\\n\";\n#\tprint \"@rad\\n\";\n}\n$c=0;\nwhile ($N >= 0){\n#\tprint $distan[$N].\"\\n\";\n#\tprint $rad[$N].\"\\n\";\n\tif ((($distan[$N] + $rad[$N]) <= $r) && (($distan[$N]-$rad[$N]) >= $r - $d)) {\n\t\t$c = $c + 1;\n\t}\n\t$N = $N-1;\n}\nprint $c.\"\\n\";"}, {"source_code": "my $numberlines = ; \nmy $numeros;\n@arrayradios = split(/\\s+/, $numberlines);\n$R = $arrayradios[0]; \n$r = $arrayradios[1];\nmy $contador=0;\n@arraydistancias=();\n@radiosp=();\nmy $N= ;\n$l = $N-1;\nfor($i=$l;$l>=0;$l--)\n{\n\t$numeros = ;\n\t@arraynumeros = split(/\\s+/, $numeros);\n\t$disteucl = sqrt(($arraynumeros[0] **2)+($arraynumeros[1] **2));\n\tpush (@arraydistancias, $disteucl);\n\tpush (@radiosp, $arraynumeros[2]);\n}\nfor($i=$N-1;$N>=0;$N--)\n{\n\tif ((($arraydistancias[$N] + $radiosp[$N]) <= $R) && (($arraydistancias[$N]-$radiosp[$N]) >= $R - $r)) {\n\t\t$contador = $contador+ 1;\n\t}\n}\n\nprint $contador;"}, {"source_code": "( $r, $d ) = split ' ', <>; <>;\n\nfor ( <> ){\n\t( $x, $y, $r2 ) = split;\n\t$R = $x ** 2 + $y ** 2;\n\t$c += ( ($r - $d + $r2) ** 2 <= $R and $R <= ($r - $r2) ** 2 );\n\t}\n\t\nprint $c"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $r, $d ) = split;\n\tmy @A = map { [ split ' ', <> ] } 1 .. <>;\n\tmy $cnt = 0;\n\t\n\tfor ( @A ){\n\t\tmy $min = $r - $d + $_->[2];\n\t\tmy $max = $r - $_->[2];\n\t\t$debug and print \"min:[$min], max:[$max]\";\n\t\tmy $R = $_->[0] ** 2 + $_->[1] ** 2;\n\t\tif( $min ** 2 <= $R and $R <= $max ** 2 ){\n\t\t\t$cnt ++;\n\t\t\t}\n\t\t}\n\t\n\tprint $cnt;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n$line = ; \n@radios = split(/\\s+/, $line);\n$R = $radios[0]; \n$r = $radios[1];\nmy $N= - 1;\n$dat = $N;\n@distan=();\n@rad=();\nwhile ($dat >= 0){\n\t$ent = ;\n\t@datos = split(/\\s+/, $ent);\n\t$Tempd = sqrt(($datos[0] * $datos[0])+($datos[1] * $datos[1]));\n\tpush (@distan, $Tempd);\n\tpush (@rad, $datos[2]);\n\t$dat = $dat-1;\n#\tprint \"@distan\\n\";\n#\tprint \"@rad\\n\";\n}\n$c=0;\nwhile ($N >= 0){\n#\tprint $distan[$N].\"\\n\";\n#\tprint $rad[$N].\"\\n\";\n\tif ((($distan[$N] + $rad[$N]) <= $R) && (($distan[$N]-$rad[$N]) >= $r)) {\n\t\t$c = $c + 1;\n\t}\n\t$N = $N-1;\n}\nprint $c.\"\\n\";"}, {"source_code": "use strict;\nuse warnings;\nuse Data::Dumper;\n\n\nmy $m;\nmy $i;\nmy $j;\nmy $valor;\nmy @m1;\nmy @m2;\nmy $contador=0;\nmy $outside;\nmy $main;\n\n\nprint \"Ingrese R:\";\nmy $R = ;\nchomp $R;\nprint \"R es '$R'\\n\";\n\nprint \"Ingrese r:\";\nmy $r = ;\nchomp $r;\nprint \"r es '$r'\\n\";\n\nprint \"ingrese los puntos:\";\n$m=;\nchop($m);\n\nfor($i=0;$i<$m;$i++)\n{\n for($j=0;$j<3;$j++)\n {$valor=;\n \tchop($valor);\n \t$m1[$i][$j]=$valor;\n }\n}\nfor($i=0;$i<$m;$i++)\n{\n\tfor($j=0;$j<=3;$j++)\n\t{\n\t\t$m2[$i][$j]=sqrt($m1[$i][$j]**2+$m1[$i][$j+1]**2);\n\t}\n\tprint \"\\n\";\n}\n\nfor($i=0;$i<$m;$i++)\n{\n for($j=0;$j<1;$j++)\n {\n \tif($m2[$i][$j]+$r<=$R)\n \t{\n \t\t$contador=$contador+1;\n \t}\n }\n print \"\\n\";\n}\nprint $contador;"}, {"source_code": "use strict;\nuse warnings;\nuse Data::Dumper;\n\n\nmy $m;\nmy $i;\nmy $j;\nmy $valor;\nmy @m1;\nmy @m2;\nmy $contador=0;\nmy $outside;\nmy $main;\n\nmy $R = ;\nchomp $R;\n\nmy $r = ;\nchomp $r;\n\n$m=;\nchop($m);\n\nfor($i=0;$i<$m;$i++)\n{\n for($j=0;$j<3;$j++)\n {$valor=;\n \tchop($valor);\n \t$m1[$i][$j]=$valor;\n }\n}\nfor($i=0;$i<$m;$i++)\n{\n\tfor($j=0;$j<=3;$j++)\n\t{\n\t\t$m2[$i][$j]=sqrt($m1[$i][$j]**2+$m1[$i][$j+1]**2);\n\t}\n}\nfor($i=0;$i<$m;$i++)\n{\n for($j=0;$j<1;$j++)\n {\n \tif($m2[$i][$j]+$r<=$R)\n \t{\n \t\t$contador=$contador+1;\n \t}\n }\n}\nprint $contador;"}, {"source_code": "( $r, $d ) = split ' ', <>;\n\nfor ( <> ){\n\t( $x, $y, $r2 ) = split;\n\t$R = $x ** 2 + $y ** 2;\n\t$c += ( ($r - $d + $r2) ** 2 <= $R and $R <= ($r - $r2) ** 2 );\n\t}\n\t\nprint $c"}], "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\u00a0\u2014 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$$$)\u00a0\u2014 the number of test cases. Next $$$t$$$ lines contain test cases\u00a0\u2014 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}\\}$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n ( my $s = scalar() ) =~ s/[\\x00-\\x20]+$//ios;\n \n my %r = ('R'=>'P', 'S'=>'R', 'P'=>'S');\n \n my %cc = ('R'=>0, 'S'=>0, 'P'=>0);\n for(my $i=0;$i $cc{$a} } keys %cc;\n my $res = ($r{$scc[0]} x length($s));\n \n print \"$res\\n\";\n}\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n ( my $s = scalar() ) =~ s/[\\x00-\\x20]+$//ios;\n \n my %cc = ('R'=>'P', 'S'=>'R', 'P'=>'S');\n my $res = '';\n for(my $i=0;$i);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n ( my $s = scalar() ) =~ s/[\\x00-\\x20]+$//ios;\n \n my %cnt = ('R'=>0, 'S'=>0, 'P'=>0);\n for(my $i=0;$i0;\n $res .= ( 'S' x $s ) if $s>0;\n $res .= ( 'R' x $r ) if $r>0;\n \n print \"$res\\n\";\n\n \n}\n\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\u00a0\u2014 but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1,\u2009...,\u2009an. There aren't any big jumps between consecutive data points\u00a0\u2014 for each 1\u2009\u2264\u2009i\u2009<\u2009n, it's guaranteed that |ai\u2009+\u20091\u2009-\u2009ai|\u2009\u2264\u20091.A range [l,\u2009r] 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\u2009\u2264\u2009i\u2009\u2264\u2009r; the range [l,\u2009r] is almost constant if M\u2009-\u2009m\u2009\u2264\u20091.Find the length of the longest almost constant range.", "input_spec": "The first line of the input contains a single integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of data points. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009100\u2009000).", "output_spec": "Print a single number\u00a0\u2014 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,\u20095]; 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,\u20094], [6,\u20099] and [7,\u200910]; the only almost constant range of the maximum length 5 is [6,\u200910]."}, "positive_code": [{"source_code": "$\\ = $/;\n\n@_ = split ' ', (<>,<>);\n\n$MAX = 0;\n\nfor $i (0 .. 1){\n\t\n\t@A = map { $_ += $_ % 2 == $i ? 1 : 0 } @A = @_, -1;\n\n\t$seq = 0;\n\t$max = 0;\n\t\n\tfor (1 .. @A -1){\n\t\t$A[ $_ ] == $A[ $_ -1 ] ? \n\t\t\t++ $seq : do { $seq > $max and $max = $seq; $seq = 0 }\n\t\t}\n\t\n\t$MAX < $max and $MAX = $max\n\t}\n\t\nprint 1 + $MAX"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t@odd = map { $_ % 2 ? $_ : $_ + 1 } @_;\n\t@even = map { $_ % 2 ? $_ + 1 : $_ } @_;\n#\tprint \"[@odd]\";\n#\tprint \"[@even]\";\n\t\n\tpush @odd, -1;\n\tpush @even, -1;\n\t\n\t@ans = ();\n\t\n\t$seq = 0;\n\t$max = 0;\n\t\n\tfor $i (1 .. @odd -1){\n\t\t$odd[ $i ] == $odd[ $i -1 ] ? \n\t\t\tdo { $seq ++ }\n\t\t:\n\t\t\tdo { $seq > $max and $max = $seq; $seq = 0 }\n\t\t}\n\tpush @ans, \"$max\";\n\t\n\t$seq = 0;\n\t$max = 0;\n\t\n\tfor $i (1 .. @even -1){\n\t\t$even[ $i ] == $even[ $i -1 ] ? \n\t\t\tdo { $seq ++ }\n\t\t:\n\t\t\tdo { $seq > $max and $max = $seq; $seq = 0 }\n\t\t}\n\tpush @ans, \"$max\";\n\t\n\tprint 1 + (sort {$b <=> $a} @ans)[ 0 ]\n\t}"}, {"source_code": "@_ = split ' ', (<>,<>);\n\nfor $i (0 .. 1){\n\t\n\t@A = map $_ += $_ % 2 == $i, @A = @_, -1;\n\n\tmy ($seq, $max);\n\t\n\tfor (1 .. @A -1){\n\t\t$A[ $_ ] == $A[ $_ -1 ] ? \n\t\t\t++ $seq : do { push @B, $seq; $seq = 0 }\n\t\t}\n\t}\n\t\nprint 1 + (sort {$b <=> $a} @B)[ 0 ]"}], "negative_code": [{"source_code": "@_ = split ' ', (<>,<>);\n\nfor $i (0 .. 1){\n\t$_ .= ' ' . join ' ', map $_ += $_ % 2 == $i, @A = @_, -1;\n\t}\n\t\nprint + ( sort {$b <=> $a} map 0 + split, /( (\\b \\d+ ) (?: [ ] \\2)* )/gx )[ 0 ]"}, {"source_code": "@_ = split ' ', (<>,<>);\n\nfor $i (0 .. 1){\n\t$_ .= ' ' . join ' ', map $_ += $_ % 2 == $i, @A = @_, -1;\n\t}\n\nprint \"[$_]\";\nprint + ( sort {$b <=> $a} map 0 + split, /( (\\b \\d+ ) (?: [ ] \\2 \\b)* )/gx )[ 0 ]\n"}, {"source_code": "@_ = split ' ', (<>,<>);\n\nfor $i (0 .. 1){\n\t$_ .= ' ' . join ' ', map $_ += $_ % 2 == $i, @A = @_, -1;\n\t}\n\nprint + ( sort {$b <=> $a} map 0 + split, /( (\\b \\d+ ) (?: [ ] \\2 \\b){0,1e6} )/gx )[ 0 ]"}, {"source_code": "@_ = split ' ', (<>,<>);\n\nfor $i (0 .. 1){\n\t$_ .= ' ' . join ' ', map $_ += $_ % 2 == $i, @A = @_, -1;\n\t}\n\nprint \"[$_]\";\nprint + ( sort {$b <=> $a} map 0 + split, /( (\\b \\d+ ) (?: [ ] \\2 \\b){0,1e6} )/gx )[ 0 ]"}, {"source_code": "@_ = split ' ', (<>,<>);\n\nfor $i (0 .. 1){\n\t$_ .= ' ' . join ' ', map $_ += $_ % 2 == $i, @A = @_, -1;\n\t}\n\nprint + ( sort {$b <=> $a} map 0 + split, /( (\\b \\d+ ) (?: [ ] \\2 \\b)* )/gx )[ 0 ]"}], "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$$$) \u2014 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$$$) \u2014 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 \u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$_ = <>, chomp;\r\n\t\r\n\t$z = () = m/0/g;\r\n\t$o = () = m/1/g;\r\n\t\r\n\t( $m ) = sort { $b <=> $a } map length, m/0+|1+/g;\r\n\t\r\n\tprint $z * $o > $m ** 2 ? $z * $o : $m ** 2;\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\tmy $z = () = m/0/g;\n\tmy $o = () = m/1/g;\n\t\n\tmy( $max ) = sort { $b <=> $a } map length, split /(?<=0)(?=1)|(?<=1)(?=0)/;\n\t\n\tprint $z * $o > $max ** 2 ? $z * $o : $max ** 2;\n\t}"}], "negative_code": [], "src_uid": "9070e0d4f8071d1ee8df5189b7c17bfa"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009300). Next n lines contain integer numbers b1, ..., bn \u00a0\u2014 the required sums of digits. All bi belong to the range 1\u2009\u2264\u2009bi\u2009\u2264\u2009300.", "output_spec": "Print n integer numbers, one per line\u00a0\u2014 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": "$\\ = $/;\nwhile( $n = <> ){\n\t$s = 0;\n\t$_ = 0 x 350;\n\tfor $i ( 1 .. $n ){\n\t\tchomp( $e = <> );\n\t\twhile ( $s != $e ){\n\t\t\tif ( $s < $e ){\n\t\t\t\ts/[0-8](?=9*$)/ $s - $e < -10 ? ( $s += 9 - $& - 1 , 9 ) : $&+1 /e;\n\t\t\t\t$s ++;\n\t\t\t\t}\n\t\t\telsif ( $s > $e ){\n\t\t\t\twhile ($s > $e){\n\t\t\t\t\twhile ($s - $e > 30){\n\t\t\t\t\t\ts/[1-9]{3}(?=0*$)/000/e;\n\t\t\t\t\t\t$s -= $&;\n\t\t\t\t\t\t}\n\t\t\t\t\twhile ($s - $e > 10){\n\t\t\t\t\t\ts/[1-9](?=0*$)/0/e;\n\t\t\t\t\t\t$s -= $&;\n\t\t\t\t\t\t}\n\t\t\t\t\ts/[1-9](?=0*$)/$&-1/e;\n\t\t\t\t\t$s --;\n\t\t\t\t\t}\n\t\t\t\ts/([0-8])(9*)([1-9])(0*)$/ $1+1 . $4 . ($3-1) . $2 /e;\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t($a = $_) =~ s/^0+//;\n\t\tprint $a;\n\t\ts/[0-8]9*$/($a=$&)=~y!0-89!1-90!,$a/e;\n\t\t$s ++;\n\t\t$s -= 9 * ( - 1 + length $&);\n\t\t}\n\t\n\t}"}, {"source_code": "$\\ = $/;\nwhile( $n = <> ){\n\t$s = 0;\n\t$_ = 0 x 350;\n\tfor $i ( 1 .. $n ){\n\t\tchomp( $e = <> );\n\t\twhile ( $s != $e ){\n\t\t\tif ( $s < $e ){\n\t\t\t\ts/([0-8])(?=9*$)/ $s - $e < -10 ? ( $s += 9 - $1 - 1 , 9 ) : $1+1 /e;\n\t\t\t\t$s ++;\n\t\t\t\t}\n\t\t\telsif ( $s > $e ){\n\t\t\t\twhile ($s > $e){\n\t\t\t\t\twhile ($s - $e > 30){\n\t\t\t\t\t\ts/([1-9]{3})(?=0*$)/000/e;\n\t\t\t\t\t\t$s -= $1;\n\t\t\t\t\t\t}\n\t\t\t\t\twhile ($s - $e > 10){\n\t\t\t\t\t\ts/([1-9])(?=0*$)/0/e;\n\t\t\t\t\t\t$s -= $1;\n\t\t\t\t\t\t}\n\t\t\t\t\ts/([1-9])(?=0*$)/$1-1/e;\n\t\t\t\t\t$s --;\n\t\t\t\t\t}\n\t\t\t\ts/([0-8])(9*)([1-9])(0*)$/ $1+1 . $4 . ($3-1) . $2 /e;\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t($a = $_) =~ s/^0+//;\n\t\tprint $a;\n\t\ts/([0-8]9*)$/($a=$1)=~y!0-89!1-90!,$a/e;\n\t\t$s ++;\n\t\t$s -= 9 * ( - 1 + length $1);\n\t\t}\n\t\n\t}"}, {"source_code": "<>;\n$_ = 0 x 350;\n\tfor $e ( <> ){\n\t\t\twhile ( $s < $e ){\n\t\t\t\ts/[0-8](?=9*$)/ $s - $e < -10 ? ( $s += 9 - $&, 9 ) : ( $s++ , $&+1 )/e;\n\t\t\t\t}\n\t\t\tif ( $s > $e ){\n\t\t\t\twhile ($s > $e){\n\t\t\t\t\ts/[1-9](?=0*$)/ $s - $e > 10 ? ($s -= $&, 0) : ( $s--, $&-1 )/e;\n\t\t\t\t\t}\n\t\t\t\ts/([0-8])(9*)([1-9])(0*)$/ $1+1 . $4 . ($3-1) . $2 /e;\n\t\t\t\t}\n\t\t\t\n\t\t/^0+/;\n\t\tprint $'.$/;\n\t\ts/[0-8](9*)$/($a=$&)=~y!0-9!1-90!,$a/e;\n\t\t$s -= -1 + 9 * length $1;\n\t\t}"}], "negative_code": [{"source_code": "$\\ = $/;\nwhile( $n = <> ){\n\t$s = 0;\n\t$_ = 0 x 350;\n\tfor $i ( 1 .. $n ){\n\t\tchomp( $e = <> );\n\t\twhile ( $s != $e ){\n\t\t\tif ( $s < $e ){\n\t\t\t\ts/[^9](?=9*$)/$&+1/e;\n\t\t\t\t$s ++;\n\t#\t\t\tprint \"+[\".(0+$_).\"]\";\n\t\t\t\t}\n\t\t\telsif ( $s > $e ){\n\t\t\t\ts/[^9]9*$/($a=$&)=~y!0-9!1-90!,$a/e;\n\t\t\t\t$s ++;\n\t\t\t\t$s -= 9 * ( - 1 + length $&);\n\t#\t\t\t\t\t\t\tprint \"-[\".(0+$_).\"]\";\n\n\t\t\t\t\twhile ($s > $e){\n\t\t\t\t\t\ts/([0-8])(9*)([1-9])(0*)$/$1+1 . 0 x(length $2) . 0 . $4/e;\n\t\t\t\t\t\t$s ++;\n\t\t\t\t\t\t$s -= $3;\n\t\t\t\t\t\t$s -= 9 x(length $2)\n\t#\t\t\t\t\t\t\t\t\tprint \":[\".(0+$_).\"]\";\n\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t($a = $_) =~ s/^0+//;\n\t\tprint $a;\n\t\ts/[^9]9*$/($a=$&)=~y!0-89!1-90!,$a/e;\n\t\t$s ++;\n\t\t$s -= 9 * ( - 1 + length $&);\n\t\t}\n\t\n\t}"}, {"source_code": "$\\ = $/;\nwhile( $n = <> ){\n\t$s = 0;\n\t$_ = 0 x 3e2;\n\tfor $i ( 1 .. $n ){\n\t\tchomp( $e = <> );\n\t\twhile ( $s != $e ){\n\t\t\tif ( $s < $e ){\n\t\t\t\ts/[^9](?=9*$)/$&+1/e;\n\t\t\t\t$s ++;\n\t#\t\t\tprint \"+[\".(0+$_).\"]\";\n\t\t\t\t}\n\t\t\telsif ( $s > $e ){\n\t\t\t\ts/[^9]9*$/($a=$&)=~y!0-9!1-90!,$a/e;\n\t\t\t\t$s ++;\n\t\t\t\t$s -= 9 * ( - 1 + length $&);\n\t#\t\t\t\t\t\t\tprint \"-[\".(0+$_).\"]\";\n\n\t\t\t\t\twhile ($s > $e){\n\t\t\t\t\t\ts/(.)([1-9])(0*)$/$1+1 . 0 . $3/e;\n\t\t\t\t\t\t$s ++;\n\t\t\t\t\t\t$s -= $2;\n\t#\t\t\t\t\t\t\t\t\tprint \":[\".(0+$_).\"]\";\n\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t($a = $_) =~ s/^0+//;\n\t\tprint $a;\n\t\ts/[^9]9*$/($a=$&)=~y!0-89!1-90!,$a/e;\n\t\t$s ++;\n\t\t$s -= 9 * ( - 1 + length $&);\n\t\t}\n\t\n\t}"}, {"source_code": "$\\ = $/;\nwhile( $n = <> ){\n\t$s = 0;\n\t$_ = 0 x 350;\n\tfor $i ( 1 .. $n ){\n\t\tchomp( $e = <> );\n\t\twhile ( $s != $e ){\n\t\t\tif ( $s < $e ){\n\t\t\t\ts/[^9](?=9*$)/$&+1/e;\n\t\t\t\t$s ++;\n\t#\t\t\tprint \"+[\".(0+$_).\"]\";\n\t\t\t\t}\n\t\t\telsif ( $s > $e ){\n\t\t\t\ts/[^9]9*$/($a=$&)=~y!0-9!1-90!,$a/e;\n\t\t\t\t$s ++;\n\t\t\t\t$s -= 9 * ( - 1 + length $&);\n\t#\t\t\t\t\t\t\tprint \"-[\".(0+$_).\"]\";\n\n\t\t\t\t\twhile ($s > $e){\n\t\t\t\t\t\ts/(.)([1-9])(0*)$/$1+1 . 0 . $3/e;\n\t\t\t\t\t\t$s ++;\n\t\t\t\t\t\t$s -= $2;\n\t#\t\t\t\t\t\t\t\t\tprint \":[\".(0+$_).\"]\";\n\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t($a = $_) =~ s/^0+//;\n\t\tprint $a;\n\t\ts/[^9]9*$/($a=$&)=~y!0-89!1-90!,$a/e;\n\t\t$s ++;\n\t\t$s -= 9 * ( - 1 + length $&);\n\t\t}\n\t\n\t}"}, {"source_code": "$\\ = $/;\nwhile( $n = <> ){\n\t$s = 0;\n\t$_ = 0 x 3e2;\n\tfor $i ( 1 .. $n ){\n\t\tchomp( $e = <> );\n\t\twhile ( $s != $e ){\n\t\t\tif ( $s < $e ){\n\t\t\t\ts/[^9](?=9*$)/$&+1/e;\n\t\t\t\t$s ++;\n\t\t\t\t}\n\t\t\telsif ( $s > $e ){\n\t\t\t\ts/[^9]9*$/($a=$&)=~y!0-9!1-90!,$a/e;\n\t\t\t\t$s ++;\n\t\t\t\t$s -= 9 * ( - 1 + length $&);\n\t\t\t\t\twhile ($s > $e){\n\t\t\t\t\t\ts/(.)([1-9])(0*)$/$1+1 . 0 . $3/e;\n\t\t\t\t\t\t$s ++;\n\t\t\t\t\t\t$s -= $2;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t($a = $_) =~ s/^0+//;\n\t\tprint $a;\n\t\ts/[^9](?=9*$)/$&+1/e;\n\t\t$s ++;\n\t\t}\n\t\n\t}"}, {"source_code": "$\\ = $/;\nwhile( $n = <> ){\n\t$s = 0;\n\t$_ = 0 x 80;\n\tfor $i ( 1 .. $n ){\n\t\tchomp( $e = <> );\n\t\twhile ( $s != $e ){\n\t\t\tif ( $s < $e ){\n\t\t\t\ts/[^9](?=9*$)/$&+1/e;\n\t\t\t\t$s ++;\n\t\t\t\t}\n\t\t\telsif ( $s > $e ){\n\t\t\t\twhile ($s > $e){\n\t\t\t\t\tif ($s - $e > 10){\n\t\t\t\t\t\ts/[1-9](?=0*$)/0/e;\n\t\t\t\t\t\t$s -= $&;\n\t\t\t\t\t\t}\n\t\t\t\t\ts/[1-9](?=0*$)/$&-1/e;\n\t\t\t\t\t$s --;\n\t\t\t\t\t}\n\t\t\t\ts/([0-8])(9*)([1-9])(0*)$/ $1+1 . $4 . ($3-1) . $2 /e;\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t($a = $_) =~ s/^0+//;\n\t\tprint $a;\n\t\ts/[^9]9*$/($a=$&)=~y!0-89!1-90!,$a/e;\n\t\t$s ++;\n\t\t$s -= 9 * ( - 1 + length $&);\n\t\t}\n\t\n\t}"}], "src_uid": "4577a9b2d96110966ad95a960ef7ec20"} {"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$$$ \u2014 the base of the ladder; $$$k$$$ planks of length at least $$$1$$$ \u2014 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$$$) \u2014 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$$$) \u2014 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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = sort { $b <=> $a } split ' ', <>;\n\t\n\tmy $length = $_[ 1 ];\n\t\n\tmy $steps = @_ - 2;\n\t\n\t$steps >= $length and $steps = $length - 1;\n\t\n\tprint $steps;\n\t}"}], "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\u2009\u2264\u2009n,\u2009m,\u2009k\u2009\u2264\u20091000)\u00a0\u2014 the number of the planned days, the number of clean bowls and the number of clean plates. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20092). 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 \u2014 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) = split / /, <>;\n@line = split / /, <>;\n# =pod\n@arr = (0,0,0);\n@arr[$_]++ for @line;\nif ($m >= $arr[1]) {\n\t$ans = $arr[2]-$m+$arr[1]-$k;\n\t$ans = 0 if $ans < 0;\n\t# =pod\n\t# if ($ans < 0) {\n\t\t# $ans = 0;\n\t# }\n\t# =cut\n} else {\n\t$ans = $arr[1] - $m;\n\t$tmp = $arr[2] - $k;\n\t$tmp = 0 if $tmp < 0;\n\t$ans += $tmp;\n}\nprint $ans,\"\\n\";\n# =cut"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n($ans, $k, $n, $m) = (0, split / /, <>);\nforeach (split / /, <>) {\n\tif ($_ == 1) {\n\t\t($n>0 and $n--) or ++$ans;\n\t} else {\n\t\t($m>0 and $m--) or ($n>0 and $n--) or ++$ans;\n\t}\n}\nsay $ans;"}, {"source_code": "#!/usr/bin/perl\nuse bignum;\n\n($n, $m, $k) = split(/\\s/, );\n($mCount, $kCount) = ($m, $k);\n@a = split(/\\s/, );\n$answer = 0;\n\nforeach $item (@a) {\n if( $item == 1 ) {\n if( $mCount > 0 ) {\n $mCount--;\n }\n else {\n $answer++;\n }\n }\n else {\n if( $kCount > 0 ) {\n $kCount--;\n }\n else {\n if( $mCount > 0 ) {\n\t$mCount--;\n }\n else {\n\t$answer++;\n }\n }\n }\n}\n\nprint $answer, \"\\n\";"}, {"source_code": "#!/usr/bin/perl\n\n($n, $m, $k) = split(/\\s/, );\n($mCount, $kCount) = ($m, $k);\n@a = split(/\\s/, );\n$answer = 0;\n\nforeach $item (@a) {\n if( $item == 1 ) {\n if( $mCount > 0 ) {\n $mCount--;\n }\n else {\n $answer++;\n }\n }\n else {\n if( $kCount > 0 ) {\n $kCount--;\n }\n else {\n if( $mCount > 0 ) {\n\t$mCount--;\n }\n else {\n\t$answer++;\n }\n }\n }\n}\n\nprint $answer, \"\\n\";"}, {"source_code": "#!/usr/bin/perl\n\n$input = <>;\n($n, $m, $k)=split(/\\s/, $input);\n$input = <>;\n@a = split(/\\s/, $input);\n$bowl = $m;\n$plate = $k;\n$total = @a;\n\nforeach $i (@a){\n if($i == 1){\n\tif($bowl > 0){\n\t$bowl--;\n\t$total--;\n }\n\telse{\n\t break;\n\t}\n\t\n }\n else{\n\tif($plate > 0 ){\n\t $plate--;\n\t $total--;\n\t}\n\telsif($bowl > 0){\n\t $bowl--;\n\t $total--;\n\t}\n\telse{\n\tbreak;\n }\n\n }\n}\nprint $total;\n"}, {"source_code": "#!/usr/bin/perl\n\nwhile(<>){\n\t($n, $m, $k) = split/ /;\n#\tprint \" $n $m $k\";\n\t@_=split/ /,<>;\n\t$ats=0;\n\t\n\tfor (@_){\n\t\tif ($_==1){\n\t\t\t$m--;\n\t\t\t$m<0 and $ats++;\n\t\t\t}\n\t\t\telse{\n\t\t\t\t$k--;\n\t\t\t\t$k<0 and $m--;\n\t\t\t\t$k<0 and $m<0 and $ats++;\n\t\t\t\t}\n\n\t\t}\n\tprint \"$ats\\n\";\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nwhile(<>){\n\t($n, $m, $k) = split/ /;\n#\tprint \" $n $m $k\";\n\t@_=split/ /,<>;\n\t$ats=0;\n\t\n\tfor (@_){\n\t\tif ($_==1){\n\t\t\t$m--;\n\t\t\t$m<0 and $ats++;\n\t\t\t}\n\t\t\telse{\n\t\t\t\t$k--;\n\t\t\t\t$k<0 and $m--;\n\t\t\t\t$m<0 and $ats++;\n\t\t\t\t}\n\n\t\t}\n\tprint \"ats: $ats\\n\";\n}"}, {"source_code": "#!/usr/bin/perl\n\nwhile(<>){\n\t($n, $m, $k) = split/ /;\n#\tprint \" $n $m $k\";\n\t@_=split/ /,<>;\n\t$ats=0;\n\t\n\tfor (@_){\n\t\tif ($_==1){\n\t\t\t$m--;\n\t\t\t$m<0 and $ats++;\n\t\t\t}\n\t\t\telse{\n\t\t\t\t$k--;\n\t\t\t\t$k<0 and $m--;\n\t\t\t\t$m<0 and $ats++;\n\t\t\t\t}\n\n\t\t}\n\tprint \"$ats\\n\";\n}"}], "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)$$$ \u2014 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$$$ \u2014 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": "#!/usr/bin/perl\n\n#\tuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\tmy @A = grep /\\bA\\b/, @_;\n\tmy @B = grep /\\bB\\b/, @_;\n\tmy @C = grep /\\bC\\b/, @_;\n\t\n\tmy @AB = grep /\\bAB\\b|\\bBA\\b/, @_;\n\tmy @AC = grep /\\bAC\\b|\\bCA\\b/, @_;\n\tmy @BC = grep /\\bBC\\b|\\bCB\\b/, @_;\n\t\n\tmy @ABC = grep /A/ && /B/ && /C/, @_;\n\t\n\tmy $A = ( sort { $a <=> $b } @A )[ 0 ];\n\tmy $B = ( sort { $a <=> $b } @B )[ 0 ];\n\tmy $C = ( sort { $a <=> $b } @C )[ 0 ];\n\t\n\tmy $AB = ( sort { $a <=> $b } @AB )[ 0 ];\n\tmy $AC = ( sort { $a <=> $b } @AC )[ 0 ];\n\tmy $BC = ( sort { $a <=> $b } @BC )[ 0 ];\n\t\n\tmy $ABC = ( sort { $a <=> $b } @ABC )[ 0 ];\n\t\n\t$debug and print for grep defined, $A, $B, $C, $AB, $AC, $BC, $ABC;\n\t\n\tmy $ans = ( sort { $a <=> $b } grep $_,\n\t\t( $A + $B + $C ) * !!( $A * $B * $C ),\n\t\t( $AB + $C ) * !!( $AB * $C ),\n\t\t( $AC + $B ) * !!( $AC * $B ),\n\t\t( $BC + $A ) * !!( $BC * $A ),\n\t\t( $AB + $AC ) * !!( $AB * $AC ),\n\t\t( $AB + $BC ) * !!( $AB * $BC ),\n\t\t( $AC + $BC ) * !!( $AC * $BC ),\n\t\t$ABC * !! $ABC,\n\t\t)[ 0 ];\n\t\n\tprint $ans ? $ans : -1;\n\t}"}], "negative_code": [], "src_uid": "02d62bb1eb4cc0e373b862a980d6b29c"} {"nl": {"description": "Petya's friends made him a birthday present \u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t$_ = <>, chomp;\n\t\n\t$debug and print;\n\t\n\tmy $o = ( length ) - length y/(//dr;\n\tmy $c = ( length ) - length y/)//dr;\n\t\n\t$debug and print \"$o $c\";\n\t\n\tmy $F = 0;\n\t\n\tmy $cumm_c = 0;\n\t\n\t/\n\t\t.\n\t\t(?{\n\t\t\t$& eq ')' and $cumm_c ++;\n\t\t\t$& eq '(' and $cumm_c --;\n\t\t\t$debug and print \" $cumm_c\";\n\t\t\t$cumm_c > 1 and $F = 1;\n\t\t\t})\n\t\t(*F)\n\t\t/x;\n\t\n\t$_ = reverse;\n\t\n\t$debug and print;\n\t\n\tmy $cumm_o = 0;\n\t\n\t/\n\t\t.\n\t\t(?{\n\t\t\t$& eq '(' and $cumm_o ++;\n\t\t\t$& eq ')' and $cumm_o --;\n\t\t\t$debug and print \" $cumm_o\";\n\t\t\t$cumm_o > 1 and $F = 1;\n\t\t\t})\n\t\t(*F)\n\t\t/x;\n\t\n\tprint $F || $o != $c ? \"No\" : \"Yes\";\n\t}"}], "negative_code": [], "src_uid": "e30085b163c820cff68fb24b94088ec1"} {"nl": {"description": "DZY has a hash table with p buckets, numbered from 0 to p\u2009-\u20091. 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)\u2009=\u2009x\u00a0mod\u00a0p. Operation a\u00a0mod\u00a0b 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\u2009\u2264\u2009p,\u2009n\u2009\u2264\u2009300). Then n lines follow. The i-th of them contains an integer xi (0\u2009\u2264\u2009xi\u2009\u2264\u2009109).", "output_spec": "Output a single integer \u2014 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": "\n# get first line as a string\nmy $f = ;\nchomp ( $f );\n\nmy $p, $n;\n($p, $n) = split m/ /, $f;\n\nmy %h;\nmy $collison = 0;\n\nfor ( my $i =0 ; $i < $n; $i += 1 ) {\n $f = ;\n chomp ( $f );\n if ( exists $h{ $f % $p } ) {\n\tprint (($i + 1) . \"\\n\");\t\t# print answer\n\t$i = $n; \t\t# exit the loop \n\t$collision = 1;\n }\n $h{ $f % $p } = 1;\n}\n\nif ( not $collision ) {\n print \"-1\\n\"; }\n \n \n\n\n\n\n\n\n"}, {"source_code": "\n# get first line as a string\nmy $f = ;\nchomp ( $f );\n\nmy $p, $n;\n($p, $n) = split m/ /, $f;\n\nmy %h;\nmy $colliison = 0;\n\nfor ( my $i =0 ; $i < $n; $i += 1 ) {\n $f = ;\n chomp ( $f );\n if ( exists $h{ $f % $p } ) {\n\tprint (($i + 1) . \"\\n\");\t\t# print answer\n\t$i = $n; \t\t# exit the loop \n\t$collision = 1;\n }\n $h{ $f % $p } = 1;\n}\n\nif ( not $collision ) {\n print \"-1\\n\"; }\n \n \n\n\n\n\n\n\n"}, {"source_code": "use strict ;\nuse warnings ;\n\nmy @array = split(/ /,) ;\n\nmy $p = $array[0] ;\nmy $n = $array[1] ;\n\nmy @x ;\n\nfor(my $j = 0 ; $j < $n ; $j++){\n\t$x[$j] = ;\n}\n\nmy %hash ;\n\nfor(my $i = 0 ; $i < $n ; $i++ ) {\n\tmy $value = $x[$i] ;\n\tmy $remainder = $value % $p ;\n\t\n\tif(exists $hash{$remainder}){\n\t\tprint $i + 1 ;\n\t\texit ;\n\t}else{\n\t\t$hash{$remainder} = 1 ;\n\t}\n}\n\nprint -1 ;"}, {"source_code": "chomp ($line = <>);\n($p, $n) = split ' ', $line;\nfor(1..$n)\n{\n chomp($x = <>);\n print \"$_\\n\" and exit if $seen{$x % $p};\n $seen{$x % $p} = 1;\n}\nprint \"-1\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nmy $inp = ;\nmy @ar = split(' ', $inp);\n\nmy $i;\nmy $h;\nmy $f = 0;\nmy $f_ans = -1;\n\nfor ($i = 1; $i <= $ar[1]; $i += 1) {\n my $tmp = ;\n my $ans = $tmp % $ar[0];\n if ($h{$ans} == 2) {\n if ($f == 0) {\n $f = 1;\n $f_ans = $i;\n }\n } else {\n $h{$ans} = 2;\n }\n}\n\nprint $f_ans;"}], "negative_code": [{"source_code": "\n# get first line as a string\nmy $f = ;\nchomp ( $f );\n\nmy $p, $n;\n($p, $n) = split m/ /, $f;\n\nprint \"two\\n\";\n\nmy %h;\nmy $colliison = 0;\n\nfor ( my $i =0 ; $i < $n; $i += 1 ) {\n $f = ;\n chomp ( $f );\n if ( exists $h{ $f } ) {\n\tprint \"$i\\n\";\t\t# print answer\n\t$i = $n; \t\t# exit the loop \n\t$collision = 1;\n }\n $h{ $f } = 1;\n}\n\nif ( not $collision ) {\n print \"-1\\n\"; }\n \n \n\n\n\n\n\n\n"}, {"source_code": "\n# get first line as a string\nmy $f = ;\nchomp ( $f );\n\nmy $p, $n;\n($p, $n) = split m/ /, $f;\n\nmy %h;\nmy $colliison = 0;\n\nfor ( my $i =0 ; $i < $n; $i += 1 ) {\n $f = ;\n chomp ( $f );\n if ( exists $h{ $f } ) {\n\tprint \"$i\\n\";\t\t# print answer\n\t$i = $n; \t\t# exit the loop \n\t$collision = 1;\n }\n $h{ $f } = 1;\n}\n\nif ( not $collision ) {\n print \"-1\\n\"; }\n \n \n\n\n\n\n\n\n"}, {"source_code": "#!/usr/bin/perl\n\nmy $inp = ;\nmy @ar = split(' ', $inp);\n\nmy $i;\nmy $h;\nmy $f = 0;\nmy $f_ans = -1;\n\nfor ($i = 1; $i <= $ar[1]; $i += 1) {\n my $tmp = ;\n my $ans = $tmp % $ar[0];\n if ($h{$ans} == 2) {\n $f = 1;\n my $f_ans = $i;\n } else {\n $h{$ans} = 2;\n }\n}\n\nprint $f_ans;"}, {"source_code": "#!/usr/bin/perl\n\nmy $inp = ;\nmy @ar = split(' ', $inp);\n\nmy $i;\nmy $h;\nmy $f = 0;\nmy $f_ans = -1;\n\nfor ($i = 1; $i <= $ar[1]; $i += 1) {\n my $tmp = <>;\n my $ans = $tmp % $ar[0];\n if ($h{$ans} == 2) {\n $f = 1;\n my $f_ans = $i;\n } else {\n $h{$ans} = 2;\n }\n}\n\nprint $f_ans;"}], "src_uid": "5d5dfa4f129bda46055fb636ef33515f"} {"nl": {"description": "Two integer sequences existed initially \u2014 one of them was strictly increasing, and the other one \u2014 strictly decreasing.Strictly increasing sequence is a sequence of integers $$$[x_1 < x_2 < \\dots < x_k]$$$. And strictly decreasing sequence is a sequence of integers $$$[y_1 > y_2 > \\dots > y_l]$$$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.They were merged into one sequence $$$a$$$. After that sequence $$$a$$$ got shuffled. For example, some of the possible resulting sequences $$$a$$$ for an increasing sequence $$$[1, 3, 4]$$$ and a decreasing sequence $$$[10, 4, 2]$$$ are sequences $$$[1, 2, 3, 4, 4, 10]$$$ or $$$[4, 2, 1, 10, 4, 3]$$$.This shuffled sequence $$$a$$$ is given in the input.Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one \u2014 strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.If there is a contradiction in the input and it is impossible to split the given sequence $$$a$$$ to increasing and decreasing sequences, print \"NO\".", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) \u2014 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": "If there is a contradiction in the input and it is impossible to split the given sequence $$$a$$$ to increasing and decreasing sequences, print \"NO\" in the first line. Otherwise print \"YES\" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. In the second line print $$$n_i$$$ \u2014 the number of elements in the strictly increasing sequence. $$$n_i$$$ can be zero, in this case the increasing sequence is empty. In the third line print $$$n_i$$$ integers $$$inc_1, inc_2, \\dots, inc_{n_i}$$$ in the increasing order of its values ($$$inc_1 < inc_2 < \\dots < inc_{n_i}$$$) \u2014 the strictly increasing sequence itself. You can keep this line empty if $$$n_i = 0$$$ (or just print the empty line). In the fourth line print $$$n_d$$$ \u2014 the number of elements in the strictly decreasing sequence. $$$n_d$$$ can be zero, in this case the decreasing sequence is empty. In the fifth line print $$$n_d$$$ integers $$$dec_1, dec_2, \\dots, dec_{n_d}$$$ in the decreasing order of its values ($$$dec_1 > dec_2 > \\dots > dec_{n_d}$$$) \u2014 the strictly decreasing sequence itself. You can keep this line empty if $$$n_d = 0$$$ (or just print the empty line). $$$n_i + n_d$$$ should be equal to $$$n$$$ and the union of printed sequences should be a permutation of the given sequence (in case of \"YES\" answer).", "sample_inputs": ["7\n7 2 7 3 3 1 4", "5\n4 3 1 5 3", "5\n1 1 2 1 2", "5\n0 1 2 3 4"], "sample_outputs": ["YES\n2\n3 7 \n5\n7 4 3 2 1", "YES\n1\n3 \n4\n5 4 3 1", "NO", "YES\n0\n\n5\n4 3 2 1 0"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\tif( grep $_ > 2, values %h ){\n\t\tprint \"NO\";\n\t\tnext;\n\t\t}\n\t\n\tprint \"YES\";\n\tprint 0 + keys %h;\n\tprint join ' ', sort { $a <=> $b } keys %h;\n\tprint 0 + grep { $h{ $_ } == 2 } keys %h;\n\tprint join ' ', sort { $b <=> $a } grep { $h{ $_ } == 2 } keys %h;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\tif( grep $_ > 2, values %h ){\n\t\tprint \"NO\";\n\t\tnext;\n\t\t}\n\t\n\tprint \"YES\";\n\tprint 0 + keys %h;\n\tprint join ' ', sort { $a <=> $b } keys %h;\n\tprint 0 + grep { $h{ $_ } == 2 } keys %h;\n\tprint join ' ', sort { $a <=> $b } grep { $h{ $_ } == 2 } keys %h;\n\t}"}], "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,\u2009a2,\u2009...,\u2009ak.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\u2009-\u2009k cities, and, of course, flour delivery should be paid\u00a0\u2014 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\u2009\u2260\u2009b for every 1\u2009\u2264\u2009i\u2009\u2264\u2009k) and choose a storage in some city s (s\u2009=\u2009aj for some 1\u2009\u2264\u2009j\u2009\u2264\u2009k) 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009105, 0\u2009\u2264\u2009k\u2009\u2264\u2009n)\u00a0\u2014 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\u2009\u2264\u2009u,\u2009v\u2009\u2264\u2009n, 1\u2009\u2264\u2009l\u2009\u2264\u2009109, u\u2009\u2260\u2009v) meaning that there is a road between cities u and v of length of l kilometers . If k\u2009>\u20090, then the last line of the input contains k distinct integers a1,\u2009a2,\u2009...,\u2009ak (1\u2009\u2264\u2009ai\u2009\u2264\u2009n)\u00a0\u2014 the number of cities having flour storage located in. If k\u2009=\u20090 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 \u2009-\u20091 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": "($n, $m, $k) = split \" \", <>;\nif ($k) {\n\t@r = <>;\n\t$k{$_} = 1 for split \" \", pop @r; \n\tfor (@r) {\n\t\t($u, $v, $l) = split;\n\t\tpush @l, $l if $k{$u} + $k{$v} == 1;\n\t};\n\t@l = sort { $a <=> $b } @l;\n\tprint $l[0] || -1\n} else {\n\tprint -1\n}"}], "negative_code": [], "src_uid": "b0e6a9b500b3b75219309b5e6295e105"} {"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\u2009\u2264\u2009n\u2009\u2264\u20095\u00b7105;\u00a02\u2009\u2264\u2009k\u2009\u2264\u200926). 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 \u2014 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": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t$_=<>;\n\tchomp;\n\t@_ = split//;\n\t$AB = $BA = @ab = @ba = ();\n\t$t=0;\n\t\n\tif ($k > 2){\n\t\ts/(.)\\1(?=(.|))/$t++,\n\t\t\t($1 ne A && $+ ne A and $y = A) ||\n\t\t\t($1 ne B && $+ ne B and $y = B) ||\n\t\t\t($1 ne C && $+ ne C and $y = C),\n\t\t\t$1.$y/ge\n\t\t}\n\telse {\n\t\t@ab = (A, B) x ($n / 2);\n\t\t$n % 2 and push @ab, A;\n\t\t@ba = (B, A) x ($n / 2);\n\t\t$n % 2 and push @ba, B;\n\t\tfor ($i = 0; $i < length ; $i++){\n\t\t\t$AB += $_[$i] eq $ab[$i];\n\t\t\t$BA += $_[$i] eq $ba[$i];\n\t\t\t}\n\t\t$t = (length) - ($AB > $BA ? $AB : $BA);\n\t\t$_ = join'', ($AB > $BA? @ab : @ba );\n\t\t}\n\tprint $t,$/,$_,$/\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t$_=<>;\n\tchomp;\n\t@_ = split//;\n\t$AB = $BA = @ab = @ba = ();\n\t$t=0;\n\t\n\tif ($k > 2){\n\t\t$_.=Z;\n\t\ts/(.)\\1(?=(.))/$t++,\n\t\t\t($1 ne A && $+ ne A and $y = A) ||\n\t\t\t($1 ne B && $+ ne B and $y = B) ||\n\t\t\t($1 ne C && $+ ne C and $y = C),\n\t\t\t$1.$y/ge;\n\t\tchop\n\t\t}\n\telse {\n\t\t@ab = (A, B) x ($n / 2);\n\t\t$n % 2 and push @ab, A;\n\t\t@ba = (B, A) x ($n / 2);\n\t\t$n % 2 and push @ba, B;\n\t\tfor ($i = 0; $i < length ; $i++){\n\t\t\t$AB += $_[$i] eq $ab[$i];\n\t\t\t$BA += $_[$i] eq $ba[$i];\n\t\t\t}\n\t\t$t = (length) - ($AB > $BA ? $AB : $BA);\n\t\t$_ = join'', ($AB > $BA? @ab : @ba );\n\t\t}\n\tprint $t,$/,$_,$/\n\t}"}], "negative_code": [{"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t$_=<>;\n\tchomp;\n\t@_ = split//;\n\t$AB = $BA = @ab = @ba = ();\n\t$t=0;\n\t\n\tif ($k > 2){\n\t\ts/(.)\\1(?=(.))/$t++,\n\t\t\t($1 ne A && $+ ne A and $y = A) ||\n\t\t\t($1 ne B && $+ ne B and $y = B) ||\n\t\t\t($1 ne C && $+ ne C and $y = C),\n\t\t\t$1.$y/ge\n\t\t}\n\telse {\n\t\t@ab = (A, B) x ($n / 2);\n\t\t$n % 2 and push @ab, A;\n\t\t@ba = (B, A) x ($n / 2);\n\t\t$n % 2 and push @ba, B;\n\t\tfor ($i = 0; $i < length ; $i++){\n\t\t\t$AB += $_[$i] eq $ab[$i];\n\t\t\t$BA += $_[$i] eq $ba[$i];\n\t\t\t}\n\t\t$t = (length) - ($AB > $BA ? $AB : $BA);\n\t\t$_ = join'', ($AB > $BA? @ab : @ba );\n\t\t}\n\tprint $t,$/,$_,$/\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t$_=<>;\n\tchomp;\n\t@_ = split//;\n\t$AB = $BA = @ab = @ba = ();\n\t$t=0;\n\t\n\tif ($k > 2){\n\t\t$m=A;\n\t\tfor $i(1..$k){\n\t\t\t$d= $m;\n\t\t\t$d++;\n\t\t\t$d gt (A..Z)[$k-1] and $d = A;\n\t\t\t$e= $d;\n\t\t\t$e++;\n\t\t\t$e gt (A..Z)[$k-1] and $e = A;\n\t\t\t$qm = qr/$m$m(?=$d|$)/;\n\t\t\t$t += s/$qm/$m$e/g;\n\t\t\t$qm = qr/$m$m(?=[^$d])/;\n\t\t\t$t += s/$qm/$m$d/g;\n\t\t\t$m++;\n\t\t\t}\n\n\t\t}\n\telse {\n\t\t@ab = (A, B) x ($n / 2);\n\t\t$n % 2 and push @ab, A;\n\t\t@ba = (B, A) x ($n / 2);\n\t\t$n % 2 and push @ba, B;\n\t\tfor ($i = 0; $i < length ; $i++){\n\t\t\t$AB += $_[$i] eq $ab[$i];\n\t\t\t$BA += $_[$i] eq $ba[$i];\n\t\t\t}\n\t\t$t = (length) - ($AB > $BA ? $AB : $BA);\n\t\t$_ = join'', ($AB > $BA? @ab : @ba );\n\t\t}\n\tprint $t,$/,$_,$/\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t$_=<>;\n\tchomp;\n\t@_ = split//;\n\t$AB = $BA = @ab = @ba = ();\n\t\n\tif ($k > 2){\n\t\ts/(.)\\1(?=(.|))/$p=$1,$r='ABC',$r=~s{$p|$+}{}g,$r=~m{.},(print \"[$&]\"),$p.$&/ge\n\t\t}\n\telse {\n\t\t@ab = (A, B) x ($n / 2);\n\t\t$n % 2 and push @ab, A;\n\t\t@ba = (B, A) x ($n / 2);\n\t\t$n % 2 and push @ba, B;\n\t\tfor ($i = 0; $i < length ; $i++){\n\t\t\t$AB += $_[$i] eq $ab[$i];\n\t\t\t$BA += $_[$i] eq $ba[$i];\n\t\t\t}\n\t\t$_ = join'', ($AB > $BA? @ab : @ba );\n\t\t}\n\tprint $_,$/\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t$_=<>;\n\tchomp;\n\t@_ = split//;\n\t$AB = $BA = @ab = @ba = ();\n\t\n\tif ($k > 2){\n\t\ts/(.)\\1(?=(.|))/$p=$1,$r='ABC',$r=~s{$p|$+}{}g,$r=~m{.},$p.$&/ge\n\t\t}\n\telse {\n\t\t@ab = (A, B) x ($n / 2);\n\t\t$n % 2 and push @ab, A;\n\t\t@ba = (B, A) x ($n / 2);\n\t\t$n % 2 and push @ba, B;\n\t\tfor ($i = 0; $i < length ; $i++){\n\t\t\t$AB += $_[$i] eq $ab[$i];\n\t\t\t$BA += $_[$i] eq $ba[$i];\n\t\t\t}\n\t\t$_ = join'', ($AB > $BA? @ab : @ba );\n\t\t}\n\tprint $_,$/\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t$_=<>;\n\tchomp;\n\t@_ = split//;\n\t$AB = $BA = @ab = @ba = ();\n\t$t=0;\n\t\n\tif ($k > 2){\n\t\t$m=A;\n\t\tfor $i(1..$k){\n\t\t\t$d= $m;\n\t\t\t$d++;\n\t\t\t$d gt (A..Z)[$k-1] and $d = A;\n\t\t\t$e= $d;\n\t\t\t$e++;\n\t\t\t$e gt (A..Z)[$k-1] and $e = A;\n\t\t\t$qm = qr/$m$m(?=$d)/;\n\t\t\t$t += s/$qm/$m$e/g;\n\t\t\t$qm = qr/$m$m(?=[^$d]|$)/;\n\t\t\t$t += s/$qm/$m$d/g;\n\t\t\t$m++;\n\t\t\t}\n\n\t\t}\n\telse {\n\t\t@ab = (A, B) x ($n / 2);\n\t\t$n % 2 and push @ab, A;\n\t\t@ba = (B, A) x ($n / 2);\n\t\t$n % 2 and push @ba, B;\n\t\tfor ($i = 0; $i < length ; $i++){\n\t\t\t$AB += $_[$i] eq $ab[$i];\n\t\t\t$BA += $_[$i] eq $ba[$i];\n\t\t\t}\n\t\t$t = (length) - ($AB > $BA ? $AB : $BA);\n\t\t$_ = join'', ($AB > $BA? @ab : @ba );\n\t\t}\n\tprint $t,$/,$_,$/\n\t}"}], "src_uid": "0ecf60ea733eba71ef1cc1e736296d96"} {"nl": {"description": "Let's call a string adorable if its letters can be realigned in such a way that they form two consequent groups of equal symbols (note that different groups must contain different symbols). For example, ababa is adorable (you can transform it to aaabb, where the first three letters form a group of a-s and others \u2014 a group of b-s), but cccc is not since in each possible consequent partition letters in these two groups coincide.You're given a string s. Check whether it can be split into two non-empty subsequences such that the strings formed by these subsequences are adorable. Here a subsequence is an arbitrary set of indexes of the string.", "input_spec": "The only line contains s (1\u2009\u2264\u2009|s|\u2009\u2264\u2009105) consisting of lowercase latin letters.", "output_spec": "Print \u00abYes\u00bb if the string can be split according to the criteria above or \u00abNo\u00bb otherwise. Each letter can be printed in arbitrary case.", "sample_inputs": ["ababa", "zzcxx", "yeee"], "sample_outputs": ["Yes", "Yes", "No"], "notes": "NoteIn sample case two zzcxx can be split into subsequences zc and zxx each of which is adorable.There's no suitable partition in sample case three."}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse 5.010;\n\n$s = ;\nchomp($s);\n\nmy %mp;\n\nforeach $c ( split //, $s )\n{\n\t$char = \"\".$c;\n\t\n\t#print $char.\"\\n\";\n\tif( exists $mp{$char} ){\n\t\tmy $val = $mp{$char};\n\t\t$mp{$char} = $val + 1;\n\t}\n\telse{\n\t\t$mp{$char}= 1;\n\t\t\n\t}\n\t\n\t\n}\n\n\nmy $count = keys %mp;\nmy @v = values %mp;\nmy $cnt = 0;\nfor $val (@v){\n\t\t\tif($val>1){\n\t\t\t\t$cnt++;\n\t\t\t}\n}\n#print $count;\n\nif( $count == 2 ){\t\n\t\tsay( ($cnt==2) ? \"Yes\" : \"No\" );\n}\nelsif( $count == 3 ) {\n\t\tsay( ($cnt>=1) ? \"Yes\" : \"No\" );\n}\nelsif ( $count ==4 ) {\n\tsay(\"Yes\");\n}\nelse{\n\tsay (\"No\");\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, split //;\n\t\n\tmy @values = values %h;\n\t\n\tprint do { \n\t\tif( 4 < keys %h ){\n\t\t\t'No';\n\t\t\t}\n\t\telsif( 4 == keys %h ){\n\t\t\t'Yes';\n\t\t\t}\n\t\telsif( 3 == keys %h ){\n\t\t\t( 3 < length ) ? 'Yes' : 'No';\n\t\t\t}\n\t\telsif( 2 == keys %h ){\n\t\t\tdo {\n\t\t\t\tif( $values[ 0 ] > 1 and $values[ 1 ] > 1 ){\n\t\t\t\t\t'Yes';\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\t'No';\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t}\n\t\telse{\n\t\t\t'No';\n\t\t\t}\n\t\t};\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse 5.010;\n\n$s = ;\nchomp($s);\n\nmy %mp;\n\nforeach $c ( split //, $s )\n{\n\t$char = \"\".$c;\n\t\n\t#print $char.\"\\n\";\n\tif( exists $mp{$char} ){\n\t\tmy $val = $mp{$char};\n\t\t$mp{$char} = $val + 1;\n\t}\n\telse{\n\t\t$mp{$char}= 1;\n\t\t\n\t}\n\t\n\t\n}\n\n\nmy $count = keys %mp;\nmy @v = values %mp;\nmy $cnt = 0;\nfor $val (@v){\n\t\t\tif($val>1){\n\t\t\t\t$cnt++;\n\t\t\t}\n}\nprint $count;\n\nif( $count == 2 ){\t\n\t\tsay( ($cnt==2) ? \"Yes\" : \"No\" );\n}\nelsif( $count == 3 ) {\n\t\tsay( ($cnt>=1) ? \"Yes\" : \"No\" );\n}\nelsif ( $count ==4 ) {\n\tsay(\"Yes\");\n}\nelse{\n\tsay (\"No\");\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse 5.010;\n\n$s = ;\nchomp($s);\n\n\nforeach $char ( split //, $s )\n{\n\t$char = \"\".$char;\n\tif( exists $mp{$char} ){\n\t\t$mp[$char] = $mp[$char]+1;\n\t}\n\telse{\n\t\t$mp[$char] = 1;\n\t}\n}\n\nmy $count = keys %mp;\nmy @v = values %mp;\nmy $cnt = 0;\nfor $val (@v){\n\t\t\tif($val>1){\n\t\t\t\t$cnt++;\n\t\t\t}\n}\n\n\nif( $count == 2 ){\t\n\t\tsay( ($cnt==2) ? \"Yes\" : \"No\" );\n}\nelsif( $count == 3 ) {\n\t\tsay( ($cnt>=1) ? \"Yes\" : \"No\" );\n}\nelsif ( $count ==4 ) {\n\tsay(\"Yes\");\n}\nelse{\n\tsay (\"No\");\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t\n\t\n\t\n\t\n\t}"}], "src_uid": "6190db9007f8f5eba095a6fcfc3652fc"} {"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\u2014'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,\u20091,\u20090,\u20091}, {1,\u20090,\u20091}, and {1,\u20090,\u20091,\u20090} are alternating sequences, while {1,\u20090,\u20090} and {0,\u20091,\u20090,\u20091,\u20091} 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\u2014that 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000). 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": "#use Time::HiRes qw(gettimeofday tv_interval);\n#$t=[gettimeofday];\n\n$fh=\\*STDIN;\n# open($fh,\"<\",\"t\\\\l\")||die;\n\n$n=<$fh>; \n$s=<$fh>; chomp $s;\n\n$inspect = 1;\n$eq_len = 1;\n\nsub z {\n\tif ($inspect = 1 && $eq_len > 1) {\n \t$eq_count++;\n \tif ($eq_len == 2 && $eq_count == 1) {\n \t\t$add = 1;\n \t}\n \telsif ($eq_len > 2 || $eq_count > 1) {\n \t\t$add = 2;\n \t\t$inspect = 0;\n \t}\n \t$eq_len = 1;\n\t}\n}\n\nfor ($i=0; $i, <>);\nprint + (() = /0+|1+/g) + ( /111/ || /000/ || /00.*00/ || /00.*11/ || /11.*00/ || /11.*11/ ) + /00|11/ "}, {"source_code": "$n = -1;\n<>; $x =<>; chomp($x);\n@c = split('', $x);\n\nforeach(@c)\n{\n\t$a ++ if $n == -1 or $n != $_;\n\t$s ++ if $n == $_ and $n != -1;\n\t$n = $_;\n}\n\n$s = $s > 2 ? 2 : $s;\nprint $a + $s;\n\n"}, {"source_code": "$_ = (<>, <>), chomp;\n$L = length( y/01//sr );\nprint $L + ( (length) - $L < 2 ? (length) - $L : 2)"}, {"source_code": "$_ = (<>, <>), chomp;\n@_ = split /\\b/, y/1/,/r;\nprint @_\n+ ( ( 1 < grep 1 < length, @_ ) || ( !! grep 2 < length, @_ ) )\n+ !! grep 1 < length, @_"}], "negative_code": [{"source_code": "$inspect = 1;\n$eq_len = 1;\n\nsub z {\n\tif ($inspect = 1 && $eq_len > 0) {\n \t$eq_count++;\n \tif ($eq_len == 2 && $eq_count == 1) {\n \t\t$add = 1;\n \t}\n \telsif ($eq_len > 2 || $eq_count > 1) {\n \t\t$add = 2;\n \t\t$inspect = 0;\n \t}\n \t$eq_len = 1;\n\t}\n}\n\n$skip = 1;\nwhile (($n = sysread(STDIN, $s, 1000)) > 0) {\n\tfor ($i=0; $i; \n$s=<$fh>; chomp $s;\n\n$inspect = 1;\n$eq_len = 1;\n\nsub z {\n\tif ($inspect = 1 && $eq_len > 0) {\n \t$eq_count++;\n \tif ($eq_len == 2 && $eq_count == 1) {\n \t\t$add = 1;\n \t}\n \telsif ($eq_len > 2 || $eq_count > 1) {\n \t\t$add = 2;\n \t\t$inspect = 0;\n \t}\n \t$eq_len = 1;\n\t}\n}\n\nfor ($i=0; $i, <>);\nprint + (() = /(.)\\1*/g) + (2 * ( /111/ || /000/ || /11.*00/ || /00.*11/ ) || (1 * ( /^11/ || /00$/)) )"}, {"source_code": "$_ = (<>, <>);\nprint + (() = /(.)\\1*/g) + ( /(.)\\1\\1/ || /(.)\\1.*\\1\\1/ ) + /(.)\\1/"}, {"source_code": "$n = -1;\n<>;\n@c = split('', <>);\nchomp(@c);\nforeach(@c)\n{\n\t$a ++ if $n == -1 or $n != $_;\n\t$s ++ if $n == $_;\n\t$n = $_;\n}\n\n$s = $s > 2 ? 2 : $s;\nprint $a + $s - 1;\n\n"}, {"source_code": "$_ = (<>, <>);\nprint + (() = /(.)\\1*/g) + 2 * (/^11/ || /00$/ || /111/ || /000/ || /11.*00/ || /00.*11/ )"}, {"source_code": "$_ = <>;\nprint + (() = /(.)\\1*/g) + ( /(.)\\1\\1/ || /(.)\\1.*\\1\\1/ ) + /(.)\\1/"}, {"source_code": "$_ = (<>, <>);\nprint + (() = /(.)\\1*/g) + (2 * ( /111/ || /000/ || /00.*00/ || /00.*11/ || /11.*00/ || /11.*11/ ) || (1 * /(.)\\1/ ) )"}, {"source_code": "$_ = (<>, <>);\nprint + (() = /(.)\\1*/g) + /(.)\\1\\1|(.)\\2.*(.)\\3/ + /(.)\\1/"}], "src_uid": "7b56edf7cc71a1b3e39b3057a4387cad"} {"nl": {"description": "\u041e\u0441\u043d\u043e\u0432\u043e\u0439 \u043b\u044e\u0431\u043e\u0439 \u0441\u043e\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u0434\u0440\u0443\u0436\u0431\u044b \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u0432 \u0442\u043e\u043c \u0438\u043b\u0438 \u0438\u043d\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435. \u0412 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0439 \u0441\u043e\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u0434\u0440\u0443\u0436\u0431\u0430 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u0435\u0441\u043b\u0438 a \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u043e\u043c b, \u0442\u043e b \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u043e\u043c a. \u0412 \u044d\u0442\u043e\u0439 \u0436\u0435 \u0441\u0435\u0442\u0438 \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043b\u044e\u0434\u0435\u0439, \u0438\u043c\u0435\u044e\u0449\u0438\u0445 \u0432\u044b\u0441\u043e\u043a\u0443\u044e \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0431\u044b\u0442\u044c \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u043c\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u0417\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f x. \u041f\u0443\u0441\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u0447\u0435\u043b\u043e\u0432\u0435\u043a y, \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0434\u0440\u0443\u0433\u043e\u043c x \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u043e\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0447\u0435\u043c \u0434\u043b\u044f k% \u0434\u0440\u0443\u0437\u0435\u0439 x. \u0422\u043e\u0433\u0434\u0430 \u043e\u043d \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u043c \u0434\u0440\u0443\u0433\u043e\u043c \u0434\u043b\u044f x.\u0423 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0432 \u0441\u043e\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0439 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u2014 \u044d\u0442\u043e \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u0442 1 \u0434\u043e 109. \u0412\u0430\u043c \u0434\u0430\u043d \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0430\u0440 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u0434\u0440\u0443\u0437\u044c\u044f\u043c\u0438. \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0445 \u0434\u0440\u0443\u0437\u0435\u0439.", "input_spec": "\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0434\u0432\u0430 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 m \u0438 k (1\u2009\u2264\u2009m\u2009\u2264\u2009100, 0\u2009\u2264\u2009k\u2009\u2264\u2009100) \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u0440 \u0434\u0440\u0443\u0437\u0435\u0439 \u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u043d\u0442 \u043e\u0431\u0449\u0438\u0445 \u0434\u0440\u0443\u0437\u0435\u0439 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0447\u0438\u0442\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u043c \u0434\u0440\u0443\u0433\u043e\u043c. \u0412 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 m \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043f\u043e \u0434\u0432\u0430 \u0447\u0438\u0441\u043b\u0430 ai,\u2009bi (1\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u2009109, ai\u2009\u2260\u2009bi), \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0445 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u0434\u0440\u0443\u0437\u044c\u044f\u043c\u0438. \u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u0430\u044f \u043f\u0430\u0440\u0430 \u043b\u044e\u0434\u0435\u0439 \u0444\u0438\u0433\u0443\u0440\u0438\u0440\u0443\u0435\u0442 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0430.", "output_spec": "\u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044b\u0445 \u043b\u044e\u0434\u0435\u0439 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044f id \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0445 \u0434\u0440\u0443\u0437\u044c\u044f\u0445. \u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0438\u043c\u0435\u0442\u044c \u0432\u0438\u0434 \"id:\u2009\u00a0k\u00a0id1\u00a0id2\u00a0...\u00a0idk\", \u0433\u0434\u0435 id \u2014 \u044d\u0442\u043e id \u0441\u0430\u043c\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430, k \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0445 \u0434\u0440\u0443\u0437\u0435\u0439, \u0430 id1, id2, ..., idk \u2014 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b \u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0445 \u0434\u0440\u0443\u0437\u0435\u0439 \u0432 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u044e\u0449\u0435\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435. ", "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": "use strict;\nuse warnings;\nuse Data::Dumper;\n\nour %users;\nsub Friends($)\n{\n return keys %{$users{$_[0]}};\n}\nmy ($m, $k) = split /\\s/, <>;\n\nfor(1..$m)\n{\n my ($unit, $friend) =split /\\s/, <>;\n chomp($unit);\n chomp($friend);\n $users{$unit}->{$friend} = 1;\n $users{$friend}->{$unit} = 1; \n}\n\n\nfor my $curUnit (sort {$a<=>$b} keys %users) #\u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0447\u0443\u0432\u0430\u043a\u0430\n{\n my @curFriends = Friends($curUnit); #\u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0434\u0440\u0443\u0437\u0435\u0439 \u0447\u0443\u0432\u0430\u043a\u0430\n my %checker;\n for (keys %users)\n {\n $checker{$_} = 0;\n }\n \n for(@curFriends)\n {\n for(Friends($_))\n {\n $checker{$_}++; #\u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u043d\u0430 1 \u043a\u0440\u0443\u0442\u043e\u0441\u0442\u044c \u0442\u043e\u0433\u043e, \u043a\u043e\u0433\u043e \u043d\u0430\u0448\u043b\u0438 \u0432 \u0434\u0440\u0443\u0437\u044c\u044f\u0445 \u0434\u0440\u0443\u0437\u0435\u0439\n }\n }\n my @ans;\n for (@curFriends)\n {\n delete $checker{$_}; #\u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0443\u0436\u0435_\u0434\u0440\u0443\u0437\u0435\u0439 - \u0438\u0445 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0432 \u043e\u0442\u0432\u0435\u0442\n\n }\n delete $checker{$curUnit}; #\u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0441\u0435\u0431\u044f;\n \n for(sort {$a<=>$b} keys %checker)\n {\n if ($checker{$_} >= ($#curFriends+1) * $k / 100 )\n {\n push @ans, $_; #\u0437\u0430\u043d\u043e\u0441\u0438\u043c \u0432 \u043e\u0442\u0432\u0435\u0442 \u0442\u0435\u0445, \u043a\u0442\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043a\u0440\u0443\u0442\n }\n }\n printf \"%i: %i \",$curUnit, $#ans+1;\n print \"@ans\";\n print \"\\n\";\n} "}], "negative_code": [], "src_uid": "19079c10a1bdfa8ae9b7b6e0378d3aad"} {"nl": {"description": "You are playing a computer game. To pass the current level, you have to kill a big horde of monsters. In this horde, there are $$$n$$$ monsters standing in the row, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th monster has $$$a_i$$$ health and a special \"Death's Blessing\" spell of strength $$$b_i$$$ attached to it.You are going to kill all of them one by one. It takes exactly $$$h$$$ seconds to kill a monster with health $$$h$$$.When the $$$i$$$-th monster dies, it casts its spell that increases the health of its neighbors by $$$b_i$$$ (the neighbors of the $$$j$$$-th monster in the row are the monsters on places $$$j - 1$$$ and $$$j + 1$$$. The first and the last monsters have only one neighbor each).After each monster is killed, the row shrinks, so its former neighbors become adjacent to each other (so if one of them dies, the other one is affected by its spell). For example, imagine a situation with $$$4$$$ monsters with health $$$a = [2, 6, 7, 3]$$$ and spells $$$b = [3, 6, 0, 5]$$$. One of the ways to get rid of the monsters is shown below: $$$2$$$$$$6$$$$$$7$$$$$$3$$$$$$\\xrightarrow{6\\ s}$$$$$$8$$$$$$13$$$$$$3$$$$$$\\xrightarrow{13\\ s}$$$$$$8$$$$$$3$$$$$$\\xrightarrow{8\\ s}$$$$$$6$$$$$$\\xrightarrow{6\\ s}$$$$$$\\{\\}$$$$$$3$$$$$$6$$$$$$0$$$$$$5$$$$$$3$$$$$$0$$$$$$5$$$$$$3$$$$$$5$$$$$$5$$$ The first row represents the health of each monster, the second one\u00a0\u2014 the power of the spells. As a result, we can kill all monsters in $$$6 + 13 + 8 + 6$$$ $$$=$$$ $$$33$$$ seconds. Note that it's only an example and may not be the fastest way to get rid of the monsters.What is the minimum time required to kill all monsters in the row?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the number of monsters in the row. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$)\u00a0\u2014 the initial health of corresponding monsters. The third line contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$0 \\le b_i \\le 10^9$$$), where $$$b_i$$$ is the strength of the spell for the $$$i$$$-th monster. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print one integer\u00a0\u2014 the minimum possible total time to kill all monsters.", "sample_inputs": ["4\n\n1\n\n10\n\n0\n\n3\n\n100 1 100\n\n1 100 1\n\n4\n\n2 6 7 3\n\n3 6 0 5\n\n2\n\n1000000000 1000000000\n\n1000000000 1000000000"], "sample_outputs": ["10\n203\n26\n3000000000"], "notes": "NoteIn the first test case, there is only one monster that will be killed in $$$10$$$ seconds.In the second test case, it's optimal to kill the first monster, the last monster and then the middle one. It will take $$$100 + 100 + (1 + 1 + 1)$$$ $$$=$$$ $$$203$$$ seconds.In the third test case, it's optimal to kill the first monster, then the third one, then the fourth one and finally the second one. It will take $$$2 + 7 + (3 + 0) + (3 + 6 + 5)$$$ $$$=$$$ $$$26$$$ seconds."}, "positive_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tprint eval join '+', ( split ' ', <> ), \r\n\t\tdo { my @B = sort { $a <=> $b } split ' ', <>; pop @B; @B }\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\t\n\tmy $time = 0;\n\t\n\t$time += $_ for @A;\n\t\n\twhile( @B > 1 ){\n\t\tif( $B[ 0 ] < $B[ @B - 1 ] ){\n\t\t\t$time += $B[ 0 ];\n\t\t\tshift @B;\n\t\t\t}\n\t\telse{\n\t\t\t$time += $B[ @B - 1 ];\n\t\t\tpop @B;\n\t\t\t}\n\t\t}\n\tprint $time;\n\t}"}], "negative_code": [], "src_uid": "5c75658faf6dda4d7e21e1dcf39b350a"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the quantity of friends Petya invited to the party. The second line contains n space-separated integers: the i-th number is pi \u2014 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 = <>;\n$_ = <>;\n@a = (0, split);\nfor (0 .. $n) {\n $b[$a[$_]] = $_;\n}\nprint \"@b[1 .. $n]\";\n"}, {"source_code": "my $n = <>;\nmy @arr = split \" \", <>;\nmy @ans = (0) x $n;\nfor (0..$n-1) {\n $ans[$arr[$_]] = $_ + 1;\n}\nshift @ans;\nprint \"@ans\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nchomp($n=<>);\n@ans = (0) x ($n+1);\n$i = 1;\n$ans[$_]=$i++ foreach(split / /, <>);\n1+shift @ans and say join ' ', @ans;"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nmy %donor = ();\nmy $n = <>;\nmy @a = split ' ', <>;\n$donor{$a[$_ - 1]} = $_ for 1..$n;\nprint join ' ', map { $donor{$_} } sort { $a <=> $b } keys %donor;\nprint \"\\n\";\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @p = split ' ', <>;\nmy @a = map { $_ - 1 } @p;\nmy %h;\nfor my $i (0..$n-1) {\n $h{ $a[$i] } = $i;\n}\nfor (keys %h) {\n $h{$_}++;\n}\nfor my $i (0..$n-1) {\n print \"$h{$i} \";\n}\n"}, {"source_code": ";\nchomp($s = );\n@arr = split(/ /, $s);\n\nfor($i = 0; $i <= $#arr; ++$i) {\n $result[$arr[$i] - 1] = $i + 1;\n}\nprint \"@result\";"}, {"source_code": "use strict;\nmy $n = <>;\nmy @a = split(' ', <>);\nmy @b;\nfor(my $i = 0; $i <= $#a; $i++){\n $b[$a[$i]] = $i + 1;\n}\nforeach my $i (@b){\n print $i, ' ';\n}\n"}, {"source_code": "; chomp($_=);my @arr=split(\" \"); my $i = 0; my %hash = map {++$i => $_} @arr; %hash = reverse %hash; print \"$hash{$_} \" for (1..@arr-1); print \"$hash{@arr}\\n\""}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\n# solve\n\nmy $n = read_token;\nmy @numbers = split q{ }, read_line;\n\nmy @answer = ();\n\nfor (1..$n) {\n my $i = shift @numbers;\n $answer[$i] = $_;\n}\n\nshift @answer;\n\nsay \"@answer\";\n"}, {"source_code": "<>;\n@_=split/ /,<>;\nfor (1..@_){\n$a[(shift@_)-1]=$_;\n}\nprint\"@a\""}, {"source_code": "my $num = <>;\nchomp $num;\nmy $index = <>;\nchomp $index;\nmy @index = split / /, $index;\nmy @new;\nfor $n(1..$num) {\n my $count=1;\n $count++ while ($n != $index[$count-1]);\n push @new, $count;\n}\nprint join \" \", @new;"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nmy %donor = ();\nmy $n = <>;\nmy @a = split ' ', <>;\n$donor{$a[$_ - 1]} = $_ for 1..$n;\nprint join ' ', map { $donor{$_} } sort keys %donor;\nprint \"\\n\";\n"}, {"source_code": ";\nchomp($s = );\n@arr = split(/ /, $s);\n\nfor($i = 0; $i <= $#arr; ++$i) {\n $result[$arr[$i] - 1] = $i + 1;\n}\nprint @result;"}], "src_uid": "48bb148e2c4d003cad9d57e7b1ab78fb"} {"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$$$) \u2014 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}$$$) \u2014 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$$$ \u2014 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": "my $q = <>;\nfor (my $i = 0; $i < $q; $i++) {\n my ($l_1, $r_1, $l_2, $r_2) = split ' ', <>;\n my $t = $l_1 == $l_2 ? $r_2 : $l_2;\n print \"$l_1 $t\\n\";\n}\n"}], "negative_code": [], "src_uid": "cdafe800094113515e1de1acb60c4bb5"} {"nl": {"description": "A permutation\u00a0\u2014 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]$$$\u00a0\u2014 permutations, and $$$[2, 3, 2]$$$, $$$[4, 3, 1]$$$, $$$[0]$$$\u00a0\u2014 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 \u2014 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 \u2014 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 \u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the length of the permutation. This is followed by $$$n$$$ numbers $$$a_1, a_2, \\ldots, a_n$$$\u00a0\u2014 permutation $$$a$$$.", "output_spec": "For each test case, output $$$n$$$ values\u00a0\u2014 $$$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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy @A = split ' ', <>;\n\t\n\t$_ = join \" \", @A;\n\t\n\t$debug and print;\n\t\n\tmy @branches = $_;\n\t\n\tmy $depth = -1;\n\t\n\tmy %depth;\n\t\n\twhile( @A > keys %depth ){\n\t\t$depth ++;\n\t\t$debug and print \"depth:[$depth]\", join ',', @branches;\n\t\tmy @new_branches;\n\t\t\n\t\tfor( @branches ){\n\t\t\t@_ = grep length, split;\n\t\t\tif( @_ < 1 ){\n\t\t\t\tpush @new_branches, $_;\n\t\t\t\tnext;\n\t\t\t\t}\n\t\t\tmy $max = ( sort { $b <=> $a } @_ )[ 0 ];\n\t\t\t$depth{ $max } = $depth;\n\t\t\tpush @new_branches, grep length, split m/ ?\\b$max\\b ?/;\n\t\t\t}\n\t\t@branches = grep length, @new_branches;\n\t\t}\n\t\n\tprint join ' ', map { $depth{ $_ } } @A;\n\t}"}], "negative_code": [], "src_uid": "a564017f9c411b39f8d4b69e629ae3bc"} {"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 \u2014 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\u2009\u2264\u2009n,\u2009k\u2009\u2264\u2009100). 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\u2009\u2264\u2009i\u2009\u2264\u2009n, 1\u2009\u2264\u2009ai\u2009\u2264\u2009k).", "output_spec": "Print a single integer \u2014 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 \u2009\u2192\u2009 2 2 3 4 \u2009\u2192\u2009 2 3 4 4 \u2009\u2192\u2009 3 4 4 4 \u2009\u2192\u2009 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins."}, "positive_code": [{"source_code": "($n, $k) = split(' ', <>);\n@a = split(' ', <>);\nfor(1..$k) {$c[$_] = 0;}\nforeach(@a){ $c[$_] ++;} $re = 0;\n\nuntil(0){\n\t$f = 0;\n\tfor(1..$k - 1){\n\t\t$f = 1 and last if $c[$_] > 0;}\n\tprint $re and exit if $f == 0; $re ++;\n\tfor($g = $k - 1 ; $g > 0 ; $g --){\n\t\tif($c[$g] > 0){\n\t\t$c[$g] --, $c[$g + 1] ++;}}\n}"}], "negative_code": [], "src_uid": "3d6411d67c85f6293f1999ccff2cd8ba"} {"nl": {"description": "Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0,\u20091}) 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\u2009=\u20091, 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\u2009\u2264\u2009i\u2009\u2264\u2009m) and column j (1\u2009\u2264\u2009j\u2009\u2264\u2009n) 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\u2009\u2264\u2009m,\u2009n\u2009\u2264\u2009100), 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": "while(<>){\n\tchomp;\n\t($m,$n)=split/ /;\n\t@b=(1) x $n;\n\t$f=0;\n\t\n\tfor ($i=0; $i<$m; $i++){\n\t\t$_=<>, chomp;\n\t\t/0/ or s/1/2/g;\n\t\tfor ($j=0; $j<$n; $j++){\n\t\t\ts/\\d//;\n\t\t\t$b[$j] &&= $&;\n\t\t\t$a[$i][$j]=$&;\n\t\t\t}\n\t\t}\n\t\t\n\tfor ($j=0; $j<$n; $j++){\n\t\t$b[$j] or next;\n\t\tfor ($i=0; $i<$m; $i++){\n\t\t\t$a[$i][$j] += 1;\n\t\t\t}\n\t\t}\n\n\tfor ($i=0; $i<$m; $i++){\n\t\tfor ($j=0; $j<$n; $j++){\n\t\t\t$a[$i][$j] == 1 and $f++;\n\t\t\t}\n\t\t}\n\t\n\tfor ($i=0; $i<$m; $i++){\n\t\t$dv = 0;\n\t\tfor ($j=0; $j<$n; $j++){\n\t\t\t$a[$i][$j] == 2 and $dv++;\n\t\t\t}\n\t\t$dv == $n and $f++\n\t\t}\n\n\tfor ($j=0; $j<$n; $j++){\n\t\t$dv = 0;\n\t\tfor ($i=0; $i<$m; $i++){\n\t\t\t$a[$i][$j] == 2 and $dv++;\n\t\t\t}\n\t\t$dv == $m and $f++\n\t\t}\n\t\t\n\tif ($f){ print \"NO\", $/ ; next }\n\tprint \"YES\", $/ ;\n\tfor ($i=0; $i<$m; $i++){\n\t\tfor ($j=0; $j<$n; $j++){\n\t\t\tprint $a[$i][$j] % 2;\n\t\t\tprint $j == $n-1 ? $/ : $\"\n\t\t\t}\n\t\t}\n\t}"}], "negative_code": [{"source_code": "while(<>){\n\tchomp;\n\t($m,$n)=split/ /;\n\t@b=(1) x $n;\n\t$f=0;\n\t\n\tfor ($i=0; $i<$m; $i++){\n\t\t$_=<>, chomp;\n\t\t/0/ or s/1/2/g;\n\t\tfor ($j=0; $j<$n; $j++){\n\t\t\ts/\\d//;\n\t\t\t$b[$j] &&= $&;\n\t\t\t$a[$i][$j]=$&;\n\t\t\t}\n\t\t}\n\t\t\n\tfor ($j=0; $j<$n; $j++){\n\t\t$b[$j] or next;\n\t\tfor ($i=0; $i<$m; $i++){\n\t\t\t$a[$i][$j] += 1;\n\t\t\t}\n\t\t}\n\n\tfor ($i=0; $i<$m; $i++){\n\t\tfor ($j=0; $j<$n; $j++){\n\t\t\t$a[$i][$j] == 1 and $f++;\n\t\t\t}\n\t\t}\n\t\n\tif ($f){ print \"NO\", $/ ; next }\n\tprint \"YES\", $/ ;\n\tfor ($i=0; $i<$m; $i++){\n\t\tfor ($j=0; $j<$n; $j++){\n\t\t\tprint $a[$i][$j] % 2;\n\t\t\tprint $j == $n-1 ? $/ : $\"\n\t\t\t}\n\t\t}\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($m,$n)=split/ /;\n\t$f=0;\n\t@b=();\n\t@c=();\n\tfor ($j=0; $j<$n; $j++){$b[$j] = 1}\n\tfor ($i=0; $i<$m; $i++){\n\t\t$_=<>, chomp;\n\t\t$c[$i] = $_;\n\t\t/1/ or $f++ or last;\n\t\tfor ($j=0; $j<$n; $j++){\n\t\t\ts/\\d//;\n\t\t\t$b[$j] &&= $&;\n\t\t\t$a[$i][$j]=$&;\n\t\t\t}\n\t\t}\n\t\n\t$_ = \"@b\";\n\tif (!$f and /1/){\n\t\tprint 'YES',$/;\n\t\t@null = (0) x $n;\n\t\tfor ($i=0; $i<$m; $i++){\n\t\t\tif ($c[$i] =~ /0/) {print \"@null\",$/}\n\t\t\telse {\n\t\t\t\tprint \"@b\",$/;\n\t\t\t\t}\n\t\t\t}\n\t\n\t\t}\n\telse {print 'NO',$/}\n\t}\n\n"}, {"source_code": "while(<>){\n\tchomp;\n\t($m,$n)=split/ /;\n\t$f=0;\n\t$g=0;\n\t@b=();\n\t@c=();\n\tfor ($j=0; $j<$n; $j++){$b[$j] = 1}\n\tfor ($i=0; $i<$m; $i++){\n\t\t$_=<>, chomp;\n\t\t$c[$i] = $_;\n\t\t/1/ or $f++;\n\t\t/0/ or $g++;\n\t\tfor ($j=0; $j<$n; $j++){\n\t\t\ts/\\d//;\n\t\t\t$b[$j] &&= $&;\n\t\t\t$a[$i][$j]=$&;\n\t\t\t}\n\t\t}\n\t\n\t$_ = \"@b\";\n\tif ($f == $m){ \n\t\t@null = (0) x $n; \n\t\tprint 'YES',\"\\n\", \"@null\\n\" x $m\n\t\t}\n\telsif (!$f and $g and /1/){\n\t\tprint 'YES',$/;\n\t\t@null = (0) x $n;\n\t\tfor ($i=0; $i<$m; $i++){\n\t\t\tif ($c[$i] =~ /0/) {print \"@null\",$/}\n\t\t\telse {\n\t\t\t\tprint \"@b\",$/;\n\t\t\t\t}\n\t\t\t}\n\t\n\t\t}\n\telse {print 'NO',$/}\n\t}\n\n"}, {"source_code": "while(<>){\n\tchomp;\n\t($m,$n)=split/ /;\n\t$f=0;\n\t@b=();\n\t@c=();\n\tfor ($j=0; $j<$n; $j++){$b[$j] = 1}\n\tfor ($i=0; $i<$m; $i++){\n\t\t$_=<>, chomp;\n\t\t$c[$i] = $_;\n\t\t/1/ or $f++ or last;\n\t\tfor ($j=0; $j<$n; $j++){\n\t\t\ts/\\d//;\n\t\t\t$b[$j] &&= $&;\n\t\t\t$a[$i][$j]=$&;\n\t\t\t}\n\t\t}\n\t\t\n\tif (!$f){\n\t\tprint 'YES',$/;\n\t\t@null = (0) x $n;\n\t\tfor ($i=0; $i<$m; $i++){\n\t\t\tif ($c[$i] =~ /0/) {print \"@null\",$/}\n\t\t\telse {\n\t\t\t\tprint \"@b\",$/;\n\t\t\t\t}\n\t\t\t}\n\t\n\t\t}\n\telse {print 'NO',$/}\n\t}\n"}, {"source_code": "while(<>){\n\tchomp;\n\t($m,$n)=split/ /;\n\t$f=0;\n\t@b=();\n\t@c=();\n\tfor ($j=0; $j<$n; $j++){$b[$j] = 1}\n\tfor ($i=0; $i<$m; $i++){\n\t\t$_=<>, chomp;\n\t\t$c[$i] = $_;\n\t\t/1/ or $f++;\n\t\tfor ($j=0; $j<$n; $j++){\n\t\t\ts/\\d//;\n\t\t\t$b[$j] &&= $&;\n\t\t\t$a[$i][$j]=$&;\n\t\t\t}\n\t\t}\n\t\n\t$_ = \"@b\";\n\tif ($f == $m){ \n\t\t@null = (0) x $n; \n\t\tprint 'YES',\"\\n\", \"@null\\n\" x $m\n\t\t}\n\telsif (!$f and /1/){\n\t\tprint 'YES',$/;\n\t\t@null = (0) x $n;\n\t\tfor ($i=0; $i<$m; $i++){\n\t\t\tif ($c[$i] =~ /0/) {print \"@null\",$/}\n\t\t\telse {\n\t\t\t\tprint \"@b\",$/;\n\t\t\t\t}\n\t\t\t}\n\t\n\t\t}\n\telse {print 'NO',$/}\n\t}\n\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\u00a0\u2014 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/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(!eof){\n\tmy( $A, $B ) = map { chomp; $_ } ~~<>, ~~<>;\n\t\n\tprint do {\n\tif( length $A == length $B ){\n\t\tif( $A eq $B){\n\t\t\t-1\n\t\t\t}\n\t\telse{ length $A }\n\t\t}\n\telse{\n\t\tlength $A > length $B ? length $A : length $B\n\t\t}\n\t}\n\t\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(!eof){\n\tmy( $A, $B ) = map { chomp; $_ } ~~<>, ~~<>;\n\t\n\tprint do {\n\tif( length $A == length $B ){\n\t\tif( $A eq $B){\n\t\t\t-1\n\t\t\t}\n\t\telse{ $A }\n\t\t}\n\telse{\n\t\tlength $A > length $B ? $A : $B\n\t\t}\n\t}\n\t\n\t}"}], "src_uid": "f288d7dc8cfcf3c414232a1b1fcdff3e"} {"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)$$$ \u00a0\u2014 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)$$$ \u00a0\u2014 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": "for(1..<>)\r\n{\r\n chomp(my $n = <>);\r\n chomp(my $in = <>);\r\n my @a = split //, $in;\r\n my @x = (), @y = ();\r\n my $i = 1;\r\n foreach (@a) {\r\n $_ == 1 ? push @x, $i : push @y, $i;\r\n $i++;\r\n }\r\n my $t = $#x+1;\r\n my $f = $#y+1;\r\n @y = sort { $b <=> $a } @y;\r\n #10110\r\n # x is list of 1 index\r\n # y is list of 0 index\r\n # while x is < y\r\n $i = 0;\r\n my @p = (), @q = ();\r\n while ($t-- && $f--) {\r\n if (@x[$i] > @y[$i]) {\r\n last;\r\n }\r\n push @p, @x[$i];\r\n push @q, @y[$i++];\r\n }\r\n @q = sort { $a <=> $b } @q;\r\n print scalar(@p) ? 1 : 0;\r\n print \"\\n\";\r\n scalar(@p) and print scalar(@p)*2, \" \", join(\" \", @p), \" \", join(\" \", @q), \"\\n\";\r\n}\r\nexit;"}], "negative_code": [{"source_code": "for(1..<>)\r\n{\r\n chomp(my $n = <>);\r\n chomp(my $in = <>);\r\n my @a = split //, $in;\r\n my @x = (), @y = ();\r\n my $i = 1;\r\n foreach (@a) {\r\n $_ == 1 ? push @x, $i : push @y, $i;\r\n $i++;\r\n }\r\n my $t = $#x+1;\r\n my $f = $#y+1;\r\n @y = sort { $b <=> $a } @y;\r\n #10110\r\n # x is list of 1 index\r\n # y is list of 0 index\r\n # while x is < y\r\n $i = 0;\r\n my @p = (), @q = ();\r\n while ($t-- && $f--) {\r\n if (@x[$i] > @y[$i]) {\r\n last;\r\n }\r\n push @p, @x[$i];\r\n push @q, @y[$i++];\r\n }\r\n @q = sort { $a <=> $b } @q;\r\n print scalar(@p) ? 1 : 0;\r\n print \"\\n\";\r\n scalar(@p) and print join(\" \", @p), \" \", join(\" \", @q), \"\\n\";\r\n}\r\nexit;"}, {"source_code": "for(1..<>)\r\n{\r\n chomp(my $n = <>);\r\n chomp(my $in = <>);\r\n my @a = split //, $in;\r\n my @x = (), @y = ();\r\n my $i = 1;\r\n foreach (@a) {\r\n $_ == 1 ? push @x, $i : push @y, $i;\r\n $i++;\r\n }\r\n my $t = $#x+1;\r\n my $f = $#y+1;\r\n @y = sort { $b <=> $a } @y;\r\n #10110\r\n # x is list of 1 index\r\n # y is list of 0 index\r\n # while x is < y\r\n $i = 0;\r\n my @p = (), @q = ();\r\n while ($t-- && $f--) {\r\n if (@x[$i] > @y[$i]) {\r\n last;\r\n }\r\n push @p, @x[$i];\r\n push @q, @y[$i++];\r\n }\r\n @q = sort { $a <=> $b } @q;\r\n print scalar(@p) ? 1 : 0;\r\n scalar(@p) and print \"\\n\", join(\" \", @p), \" \", join(\" \", @q), \"\\n\";\r\n}\r\nexit;"}], "src_uid": "f472f9df8b4d588c67b39df6865507ca"} {"nl": {"description": "There are n cities situated along the main road of Berland. Cities are represented by their coordinates \u2014 integer numbers a1,\u2009a2,\u2009...,\u2009an. 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 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105). The second line contains n integer numbers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009109\u2009\u2264\u2009ai\u2009\u2264\u2009109). All numbers ai are pairwise distinct.", "output_spec": "Print two integer numbers \u2014 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\u2009-\u20096|\u2009=\u20092, and it is the only pair with this distance."}, "positive_code": [{"source_code": "<>; $_ = <>; \nsub order { sort {$a <=> $b} @_ };\nfor $a (order split) {\n\tpush @d, $a - $p if defined $p;\n\t$p = $a;\n}\n@d = order @d;\n$c++ while $d[0] == $d[$c];\nprint \"$d[0] $c\";"}], "negative_code": [], "src_uid": "5f89678ae1deb1d9eaafaab55a64197f"} {"nl": {"description": "The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form .To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!", "input_spec": "The first line of the input contains a single integer m (1\u2009\u2264\u2009m\u2009\u2264\u2009200\u2009000) \u2013 the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.", "output_spec": "Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).", "sample_inputs": ["4\n(99+98)/97\n(26+4)/10\n(12+33)/15\n(5+1)/7"], "sample_outputs": ["1 2 2 1"], "notes": "NoteIn the sample testcase, the second and the third ship will both end up at the coordinate 3.Note that this problem has only two versions \u2013 easy and hard."}, "positive_code": [{"source_code": "$t = <>;\n@arr = ();\n%dict = ();\n\nwhile ($t > 0) {\n $_ = <>;\n ($a, $b, $c) = m#\\((\\d*)\\+(\\d*)\\)/(\\d*)#;\n $num = ($a + $b) / $c;\n $dict{$num} += 1;\n push @arr, $num;\n $t--;\n}\n\nforeach $n (@arr) {\n print $dict{$n} . \" \";\n}\nprint \"\\n\";\n"}], "negative_code": [], "src_uid": "2d8092dd504d22bb5a8e48002cb8923b"} {"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 \u2014 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 \u2014 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,\u2009p2,\u2009...,\u2009pn, 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\u2009\u2264\u2009n\u2009\u2264\u200950) \u2014 the number of chocolate bar wrappings that brought points to Vasya. The second line contains space-separated integers p1,\u2009p2,\u2009...,\u2009pn (1\u2009\u2264\u2009pi\u2009\u2264\u2009109). The third line contains 5 integers a, b, c, d, e (1\u2009\u2264\u2009a\u2009<\u2009b\u2009<\u2009c\u2009<\u2009d\u2009<\u2009e\u2009\u2264\u2009109) \u2014 the prizes' costs.", "output_spec": "Print on the first line 5 integers, separated by a space \u2014 the number of mugs, towels, bags, bicycles and cars that Vasya has got, respectively. On the second line print a single integer \u2014 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 \u0421++. 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\u2009-\u20092\u2009+\u200910\u2009-\u200910\u2009+\u20094\u2009-\u20094\u2009=\u20091 points remains."}, "positive_code": [{"source_code": "my $n = ;\nmy @c = split(/\\s/, );\nmy @p = split(/\\s/, );\nmy @b = (0,0,0,0,0);\nmy $t = 0;\n\nsub find {\n my $i = shift;\n my $j;\n \n if($i >= $p[4]){\n return 4;\n }elsif($i >= $p[3]){\n return 3;\n }elsif($i >= $p[2]){\n return 2;\n }elsif($i >= $p[1]){\n return 1\n }else{\n return 0;\n } \n}\n\nforeach my $c(@c){\n $t += $c;\n \n if($t >= $p[0]){\n my $v = &find($t);\n while($t >= $p[0]){\n $x = int($t/$p[$v]);\n $t = $t - $x*$p[$v];\n $b[$v]+=$x;\n $v = &find($t);\n }\n }\n}\n\nmap { print $_.\" \" } @b;\nprint \"\\n$t\";\n\n\n\n"}, {"source_code": "#!/usr/bin/perl\n\nmy $n = <>;\nchomp($n);\n\nmy $line = <>;\nchomp($line);\nmy @points = split(/\\s/, $line);\n\n$line = <>;\nchomp($line);\nmy @prize = split(/\\s/, $line);\nmy $min_prize = $prize[0];\n\nmy $point = 0;\n\nmy @ans = (0,0,0,0,0);\nfor (@points) {\n $point += $_;\n if ($point >= $min_prize) {\n get_prize();\n }\n}\n\nprint join(\" \", @ans), \"\\n\";\nprint $point;\n\n\nsub get_prize {\n for (my $i=4; $i>=0; $i--) {\n if ($point >= $prize[$i]) {\n my $tmp = int($point / $prize[$i]);\n $ans[$i] += $tmp;\n $point -= $prize[$i] * $tmp;\n }\n }\n}\n"}], "negative_code": [], "src_uid": "1ae2942b72ebb7c55359c41e141900d7"} {"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\u2009-\u20091 the inequality |hi\u2009-\u2009hi\u2009+\u20091|\u2009\u2264\u20091 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\u2009-\u2009hi\u2009+\u20091|\u2009\u2264\u20091.", "input_spec": "The first line contains two space-separated numbers, n and m (1\u2009\u2264\u2009n\u2009\u2264\u2009108, 1\u2009\u2264\u2009m\u2009\u2264\u2009105)\u00a0\u2014 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\u2009\u2264\u2009di\u2009\u2264\u2009n, 0\u2009\u2264\u2009hdi\u2009\u2264\u2009108)\u00a0\u2014 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\u2009-\u20091 the following condition holds: di\u2009<\u2009di\u2009+\u20091.", "output_spec": "If the notes aren't contradictory, print a single integer \u2014 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,\u20090,\u20091,\u20092,\u20091,\u20091,\u20090,\u20091).In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent."}, "positive_code": [{"source_code": "use integer;\n$\\ = $/;\nwhile(<>){\n($n, $m) = split;\nundef $d1;\nundef $h1;\n$f = 0;\n$max = 0;\nfor $i (1 .. $m){\n($d2, $h2) = split \" \", <>;\n!defined $d1 ? do {$top = $h2 + $d2 - 1} : do {\n$higher = ($h2 > $h1 ? $h2 : $h1);\n$diff = ($d2 - $d1) - abs($h1 - $h2);\n$diff < 0 and $f++;\n$top = $higher + $diff / 2;\n$higher > $max and $max = $higher;\n};\n$top > $max and $max = $top;\n($d1, $h1) = ($d2, $h2);\n}\n$top = $h1 + ($n - $d1);\n$top > $max and $max = $top;\nprint $f ? \"IMPOSSIBLE\" : $max;\n}"}, {"source_code": "($n, $m) = split \" \", <>;\nfor (1 .. $m){\n($d2, $h2) = split \" \", <>;\n!$d1 ? ($T = $h2 + $d2 - 1) : do {\n$H = ($h2 > $h1 ? $h2 : $h1);\n$D = $d2 - $d1 - abs($h1 - $h2);\n$f += $D < 0;\n$T = $H + ($D >> 1);\n};\npush @x, $H, $T;\n($d1, $h1) = ($d2, $h2);\n}\npush @x, $h1 + $n - $d1;\nprint $f ? \"IMPOSSIBLE\" : (sort {$b <=> $a} @x)[0]"}], "negative_code": [{"source_code": "use integer;\n$\\ = $/;\nwhile(<>){\n($n, $m) = split;\nundef $d1;\nundef $h1;\n$f = 0;\n$max = 0;\nfor $i (1 .. $m){\n($d2, $h2) = split \" \", <>;\n$d1 //= -$d2;\n$h1 //= 0;\n$higher = ($h2 > $h1 ? $h2 : $h1);\n$diff = ($d2 - $d1) - abs($h1 - $h2);\n$diff < 0 and $f++;\n$top = $higher + $diff / 2;\n$top > $max and $max = $top;\n($d1, $h1) = ($d2, $h2);\n}\n$top = $h1 + ($n - $d1);\n$top > $max and $max = $top;\nprint $f ? \"IMPOSSIBLE\" : $max;\n}"}, {"source_code": "use integer;\n$\\ = $/;\nwhile(<>){\n($n, $m) = split;\nundef $d1;\nundef $h1;\n$f = 0;\n$max = 0;\nfor $i (1 .. $m){\n($d2, $h2) = split \" \", <>;\n$d1 //= -$d2;\n$h1 //= 0;\n$higher = ($h2 > $h1 ? $h2 : $h1);\n$diff = ($d2 - $d1) - abs($h1 - $h2);\n$diff < 0 and $f++;\n$top = $higher + $diff / 2;\n$higher > $max and $max = $higher;\n$top > $max and $max = $top;\n($d1, $h1) = ($d2, $h2);\n}\n$top = $h1 + ($n - $d1);\n$top > $max and $max = $top;\nprint $f ? \"IMPOSSIBLE\" : $max;\n}"}, {"source_code": "use integer;\n$\\ = $/;\nwhile(<>){\n($n, $m) = split;\nundef $d1;\nundef $h1;\n$f = 0;\n$max = 0;\nfor $i (1 .. $m){\n($d2, $h2) = split \" \", <>;\n$d1 //= -$d2;\n$h1 //= 0;\n$higher = ($h2 > $h1 ? $h2 : $h1);\n$diff = ($d2 - $d1) - abs($h1 - $h2) * 2;\n$diff < 0 and $f++;\n$top = $higher + $diff / 2;\n$top > $max and $max = $top;\n($d1, $h1) = ($d2, $h2);\n}\n$top = $h1 + ($n - $d1);\n$top > $max and $max = $top;\nprint $f ? \"IMPOSSIBLE\" : $max;\n}"}, {"source_code": "use integer;\n$\\ = $/;\nwhile(<>){\n($n, $m) = split;\nundef $d1;\nundef $h1;\n$f = 0;\n$max = 0;\nfor $i (1 .. $m){\n($d2, $h2) = split \" \", <>;\n$d1 //= 1;\n$h1 //= 0;\n$higher = ($h2 > $h1 ? $h2 : $h1);\n$diff = ($d2 - $d1) - abs($h1 - $h2);\n$diff < 0 and $f++;\n$top = $higher + $diff / 2;\n$higher > $max and $max = $higher;\n$top > $max and $max = $top;\n($d1, $h1) = ($d2, $h2);\n}\n$top = $h1 + ($n - $d1);\n$top > $max and $max = $top;\nprint $f ? \"IMPOSSIBLE\" : $max;\n}"}, {"source_code": "use integer;\n$\\ = $/;\nwhile(<>){\n($n, $m) = split;\nundef $d1;\nundef $h1;\n$f = 0;\n$max = 0;\nfor $i (1 .. $m){\n($d2, $h2) = split \" \", <>;\n$d1 //= 0;\n$h1 //= 0;\n$higher = ($h2 > $h1 ? $h2 : $h1);\n$diff = ($d2 - $d1) - abs($h1 - $h2);\n$diff < 0 and $f++;\n$top = $higher + $diff / 2;\n$higher > $max and $max = $higher;\n$top > $max and $max = $top;\n($d1, $h1) = ($d2, $h2);\n}\n$top = $h1 + ($n - $d1);\n$top > $max and $max = $top;\nprint $f ? \"IMPOSSIBLE\" : $max;\n}"}, {"source_code": "use integer;\n$\\ = $/;\nwhile(<>){\n($n, $m) = split;\nundef $d1;\nundef $h1;\n$f = 0;\n$max = 0;\nfor $i (1 .. $m){\n($d2, $h2) = split \" \", <>;\n$d1 //= -$d2;\n$h1 //= 0;\n$higher = ($h2 > $h1 ? $h2 : $h1);\n$diff = ($d2 - $d1) - abs($h1 - $h2) * 2;\n$diff < 0 and $f++;\n$top = $higher + $diff / 2;\n$top > $max and $max = $top;\n($d1, $h1) = ($d2, $h2);\n}\nprint $f ? \"IMPOSSIBLE\" : $max;\n}"}, {"source_code": "use integer;\n$\\ = $/;\nwhile(<>){\n($n, $m) = split;\nundef $d1;\nundef $h1;\n$f = 0;\n$max = 0;\nfor $i (1 .. $m){\n($d2, $h2) = split \" \", <>;\n$d1 //= -d2 + 2;\n$h1 //= 0;\n$higher = ($h2 > $h1 ? $h2 : $h1);\n$diff = ($d2 - $d1) - abs($h1 - $h2);\n$diff < 0 and $f++;\n$top = $higher + $diff / 2;\n$higher > $max and $max = $higher;\n$top > $max and $max = $top;\n($d1, $h1) = ($d2, $h2);\n}\n$top = $h1 + ($n - $d1);\n$top > $max and $max = $top;\nprint $f ? \"IMPOSSIBLE\" : $max;\n}"}, {"source_code": "use integer;\n$\\ = $/;\nwhile(<>){\n($n, $m) = split;\nundef $d1;\nundef $h1;\n$f = 0;\n$max = 0;\nfor $i (1 .. $m){\n($d2, $h2) = split \" \", <>;\n$d1 //= -$d2 + 2;\n$h1 //= 0;\n$higher = ($h2 > $h1 ? $h2 : $h1);\n$diff = ($d2 - $d1) - abs($h1 - $h2);\n$diff < 0 and $f++;\n$top = $higher + $diff / 2;\n$higher > $max and $max = $higher;\n$top > $max and $max = $top;\n($d1, $h1) = ($d2, $h2);\n}\n$top = $h1 + ($n - $d1);\n$top > $max and $max = $top;\nprint $f ? \"IMPOSSIBLE\" : $max;\n}"}, {"source_code": "($n, $m) = split \" \", <>;\nfor (1 .. $m){\n($d2, $h2) = split \" \", <>;\n!$d1 ? ($top = $h2 + $d2 - 1) : do {\n$H = ($h2 > $h1 ? $h2 : $h1);\n$D = $d2 - $d1 - abs($h1 - $h2);\n$f += $D < 0;\n$T = $H + ($D >> 1);\n};\npush @x, $H, $T;\n($d1, $h1) = ($d2, $h2);\n}\npush @x, $h1 + $n - $d1;\nprint $f ? \"IMPOSSIBLE\" : (sort {$b <=> $a} @x)[0]"}], "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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nmy $MOD = 1_000_000_000 + 7;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15 ;\n\tmy( $n, $k ) = split;\n\t\n\t$_ = reverse sprintf \"%b\", $k;\n\t\n\t$debug and print \"[$n, $k][$_]\";\n\t\n\tmy $result = 1;\n\tmy $mult = 1;\n\t\n\twhile( m/./g ){\t\t\n\t\t$& and $result += $mult;\n\t\t\n\t\t$result %= $MOD;\n\t\t\n\t\t$mult *= $n;\n\t\t\n\t\t$mult %= $MOD;\n\t\t}\n\t\n\tprint $result % $MOD - 1;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nmy $MOD = 1_000_000_000 + 7;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15 ;\n\tmy( $n, $k ) = split;\n\t\n\t$_ = reverse sprintf \"%b\", $k;\n\t\n\t$debug and print \"[$n, $k][$_]\";\n\t\n\tmy $result = 1;\n\tmy $mult = 1;\n\t\n\twhile( m/./g ){\t\t\n\t\t$& and $result += $mult;\n\t\t\n\t\t$result %= $MOD;\n\t\t\n\t\t$mult *= $n;\n\t\t}\n\t\n\tprint $result % $MOD - 1;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nmy $MOD = 1_000_000_000 + 7;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15 ;\n\tmy( $n, $k ) = split;\n\t\n\t$_ = reverse sprintf \"%b\", $k;\n\t\n\t$debug and print \"[$n, $k][$_]\";\n\t\n\tmy $result = 1;\n\tmy $mult = 1;\n\t\n\twhile( m/./g ){\t\t\n\t\t$& and $result += $mult;\n\t\t\n\t\t$result %= $MOD;\n\t\t\n\t\t$mult *= $n;\n\t\t}\n\t\n\tprint +( $result - 1 ) % $MOD;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nmy $MOD = 1_000_000_000 + 7;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15 ;\n\tmy( $n, $k ) = split;\n\t\n\t$_ = reverse sprintf \"%b\", $k;\n\t\n\t$debug and print \"[$n, $k][$_]\";\n\t\n\tmy $result = 1;\n\tmy $mult = 1;\n\t\n\twhile( m/./g ){\t\t\n\t\t$& and $result += $mult;\n\t\t\n\t\t$mult *= $n;\n\t\t}\n\t\n\tprint +( $result - 1 ) % $MOD;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nmy $MOD = 1_000_000_000 + 7;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15 ;\n\tmy( $n, $k ) = split;\n\t\n\t$_ = reverse sprintf \"%b\", $k;\n\t\n\t$debug and print \"[$n, $k][$_]\";\n\t\n\tmy $result = 1;\n\tmy $mult = 1;\n\t\n\twhile( m/./g ){\t\t\n\t\t$& and $result += $mult;\n\t\t\n\t\t$mult *= $n;\n\t\t}\n\t\n\tprint $result % $MOD - 1;\n\t}"}], "src_uid": "2cc35227174e6a4d48e0839cba211724"} {"nl": {"description": "In Aramic language words can only represent objects.Words in Aramic have special properties: A word is a root if it does not contain the same letter more than once. A root and all its permutations represent the same object. The root $$$x$$$ of a word $$$y$$$ is the word that contains all letters that appear in $$$y$$$ in a way that each letter appears once. For example, the root of \"aaaa\", \"aa\", \"aaa\" is \"a\", the root of \"aabb\", \"bab\", \"baabb\", \"ab\" is \"ab\". Any word in Aramic represents the same object as its root. You have an ancient script in Aramic. What is the number of different objects mentioned in the script?", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\leq n \\leq 10^3$$$)\u00a0\u2014 the number of words in the script. The second line contains $$$n$$$ words $$$s_1, s_2, \\ldots, s_n$$$\u00a0\u2014 the script itself. The length of each string does not exceed $$$10^3$$$. It is guaranteed that all characters of the strings are small latin letters.", "output_spec": "Output one integer\u00a0\u2014 the number of different objects mentioned in the given ancient Aramic script.", "sample_inputs": ["5\na aa aaa ab abb", "3\namer arem mrea"], "sample_outputs": ["2", "1"], "notes": "NoteIn the first test, there are two objects mentioned. The roots that represent them are \"a\",\"ab\".In the second test, there is only one object, its root is \"amer\", the other strings are just permutations of \"amer\"."}, "positive_code": [{"source_code": "use strict;\nuse warnings;\nuse diagnostics;\n\nmy $numOfWords = ;\nmy $line = ;\nchomp $line;\n\nmy @words = split(/ /,$line);\nmy %objects;\n\nforeach my $word (@words) {\n\tmy @orgins = split(//,$word);\n\tmy $object = get_orgin(@orgins);\n\tif (not exists ($objects{$object})) {\n\t\t$objects{$object} = 1;\n\t}\n}\n\nmy @hash_keys = keys %objects;\nprint scalar (@hash_keys).\"\\n\";\n\nsub get_orgin {\n\tmy %used;\n\tmy @orgins = @_;\n\tforeach my $orgin (@orgins) {\n\t\tif (not exists($used{$orgin})) {\n\t\t\t$used{$orgin} = 1;\n\t\t}\n\t}\n\treturn join (\"\", (sort (keys %used)));\n}\n"}], "negative_code": [], "src_uid": "cf1eb164c4c970fd398ef9e98b4c07b1"} {"nl": {"description": "Vasiliy lives at point (a,\u2009b) of the coordinate plane. He is hurrying up to work so he wants to get out of his house as soon as possible. New app suggested n available Beru-taxi nearby. The i-th taxi is located at point (xi,\u2009yi) and moves with a speed vi. Consider that each of n drivers will move directly to Vasiliy and with a maximum possible speed. Compute the minimum time when Vasiliy will get in any of Beru-taxi cars.", "input_spec": "The first line of the input contains two integers a and b (\u2009-\u2009100\u2009\u2264\u2009a,\u2009b\u2009\u2264\u2009100)\u00a0\u2014 coordinates of Vasiliy's home. The second line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000)\u00a0\u2014 the number of available Beru-taxi cars nearby. The i-th of the following n lines contains three integers xi, yi and vi (\u2009-\u2009100\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009100, 1\u2009\u2264\u2009vi\u2009\u2264\u2009100)\u00a0\u2014 the coordinates of the i-th car and its speed. It's allowed that several cars are located at the same point. Also, cars may be located at exactly the same point where Vasiliy lives.", "output_spec": "Print a single real value\u00a0\u2014 the minimum time Vasiliy needs to get in any of the Beru-taxi cars. You answer will be considered correct if its absolute or relative error does not exceed 10\u2009-\u20096. 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": ["0 0\n2\n2 0 1\n0 2 2", "1 3\n3\n3 3 2\n-2 3 6\n-2 7 10"], "sample_outputs": ["1.00000000000000000000", "0.50000000000000000000"], "notes": "NoteIn the first sample, first taxi will get to Vasiliy in time 2, and second will do this in time 1, therefore 1 is the answer.In the second sample, cars 2 and 3 will arrive simultaneously."}, "positive_code": [{"source_code": "# cf706a\nuse strict;\nmy ($a,$b)=split(' ',<>);\n<>;\nmy $minT=1e9;\nwhile(defined($_=<>)) {\n my ($x,$y,$v)=split(' ',$_);\n my $t=sqrt(($x-$a)**2+($y-$b)**2)/$v;\n $minT=$t if $t<$minT;\n}\nprint $minT;\n"}], "negative_code": [], "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$$$) \u2014 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$$$) \u2014 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$$$) \u2014 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$$$ \u2014 the number of points that don't belong to any segment. In the second line print exactly $$$k$$$ integers in any order \u2014 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": "sub R { split \" \", <> };\n($n, $m) = R;\nwhile (($l, $r) = R) {\n\t$h{$_} = 1 for $l..$r;\n}\n@a = grep {!$h{$_}} 1..$m;\nprint 0+@a, \"\\n@a\";"}], "negative_code": [], "src_uid": "f336b622fdaf5960032268641320bb53"} {"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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { [ ++ $i, split ' ', <> ] } 1 .. $_;\n\t\n\tmy @pairs;\n\t\n\t####### BEGIN: x+y, x+z, y+z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\t$debug and print 'init: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t####### END: x+y, x+z, y+z\n\t\n\t####### BEGIN: x, y, z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\t$debug and print 'init: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t####### END: x, y, z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\tprint for @pairs;\n\t\n\twhile( @_ ){\n\t\tprint join ' ', map $_->[ 0 ], shift @_, shift @_;\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { [ ++ $i, split ' ', <> ] } 1 .. $_;\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\twhile( @_ ){\n\t\tprint join ' ', map $_->[ 0 ], shift @_, shift @_;\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 1;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { [ ++ $i, split ' ', <> ] } 1 .. $_;\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\twhile( @_ ){\n\t\tprint join ' ', map $_->[ 0 ], shift @_, shift @_;\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { [ ++ $i, split ' ', <> ] } 1 .. $_;\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\t$debug and print 'init: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tmy @pairs;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] \n\t\t} @_;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tprint for @pairs;\n\t\n\twhile( @_ ){\n\t\tprint join ' ', map $_->[ 0 ], shift @_, shift @_;\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { [ ++ $i, split ' ', <> ] } 1 .. $_;\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\t$debug and print 'init: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tmy @pairs;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] \n\t\t} @_;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tprint for @pairs;\n\t\n\twhile( @_ ){\n\t\tprint join ' ', map $_->[ 0 ], shift @_, shift @_;\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { [ ++ $i, split ' ', <> ] } 1 .. $_;\n\t\n\tmy @pairs;\n\t\n\t####### BEGIN: x+y, x+z, y+z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\t$debug and print 'init: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] \n\t\t} @_;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t####### END: x+y, x+z, y+z\n\t\n\t####### BEGIN: x, y, z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\t$debug and print 'init: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t####### END: x, y, z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\tprint for @pairs;\n\t\n\twhile( @_ ){\n\t\tprint join ' ', map $_->[ 0 ], shift @_, shift @_;\n\t\t}\n\t}"}], "src_uid": "47a9d72651f1407de89e28fb4b142367"} {"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,\u2009ri].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,\u2009b] covers segment [c,\u2009d], if they meet this condition a\u2009\u2264\u2009c\u2009\u2264\u2009d\u2009\u2264\u2009b. ", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of segments. Next n lines contain the descriptions of the segments. The i-th line contains two space-separated integers li,\u2009ri (1\u2009\u2264\u2009li\u2009\u2264\u2009ri\u2009\u2264\u2009109) \u2014 the borders of the i-th segment. It is guaranteed that no two segments coincide.", "output_spec": "Print a single integer \u2014 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": "#!/usr/bin/perl -w\n\nuse strict;\nuse List::Util qw(max min);\n\nchomp(my $n = <>);\nmy @l = ();\nmy @r = ();\n\nfor my $i (1 .. $n) {\n chomp($_ = <>);\n my ($left, $right) = split;\n push @l, $left;\n push @r, $right;\n}\n\nmy $min_low = min @l;\nmy $max_high = max @r;\nmy $find = 0;\n\nfor my $i (0 .. $n - 1) {\n if ($l[$i] == $min_low && $r[$i] == $max_high) {\n print $i + 1, \"\\n\";\n $find = 1;\n last;\n }\n}\nunless ($find) {\n print \"-1\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n<>;\nwhile (<>=~/ /){\n\tpush @i,$`;\n\tpush @j,$'+0;\n\t}\n$i=min(@i);\n$j=max(@j);\n\nfor $k(0..@i-1){\n\t$i[$k]==$i && $j[$k]==$j and print ($k+ ++$p);\n\t}\n\n$p or print -1;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [], "src_uid": "e702594a8e993e135ea8ca78eb3ecd66"} {"nl": {"description": "Fox Ciel has a board with n rows and n columns. So, the board consists of n\u2009\u00d7\u2009n cells. Each cell contains either a symbol '.', or a symbol '#'.A cross on the board is a connected set of exactly five cells of the board that looks like a cross. The picture below shows how it looks.Ciel wants to draw several (may be zero) crosses on the board. Each cross must cover exactly five cells with symbols '#', and any cell with symbol '#' must belong to some cross. No two crosses can share a cell.Please, tell Ciel if she can draw the crosses in the described way.", "input_spec": "The first line contains an integer n (3\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the size of the board. Each of the next n lines describes one row of the board. The i-th line describes the i-th row of the board and consists of n characters. Each character is either a symbol '.', or a symbol '#'.", "output_spec": "Output a single line with \"YES\" if Ciel can draw the crosses in the described way. Otherwise output a single line with \"NO\".", "sample_inputs": ["5\n.#...\n####.\n.####\n...#.\n.....", "4\n####\n####\n####\n####", "6\n.#....\n####..\n.####.\n.#.##.\n######\n.#..#.", "6\n.#..#.\n######\n.####.\n.####.\n######\n.#..#.", "3\n...\n...\n..."], "sample_outputs": ["YES", "NO", "YES", "NO", "YES"], "notes": "NoteIn example 1, you can draw two crosses. The picture below shows what they look like.In example 2, the board contains 16 cells with '#', but each cross contains 5. Since 16 is not a multiple of 5, so it's impossible to cover all."}, "positive_code": [{"source_code": "while(<>){\n\t$n=$_;\n\tchomp($a=<>);\n\tchomp($b=<>);\n\t@a=split//,$a;\n\t@b=split//,$b;\n\t$F=0;\n\tfor $i(0..$n-3){\n\t\tchomp($c=<>);\n\t\t@c=split//,$c;\n\t\t@D=();\n\t\t@E=();\n\t\t$f=0;\n\t\t$g=0;\n\t\tfor $j(0..$n-1){\n\t\t\tif ($a[$j] eq \"#\"){\n\t\t\t\t$g++;\n\t\t\t\t$j==0 and $f++;\n\t\t\t\t$j==$n and $f++;\n\t\t\t\tpush @D,$j-1,$j,$j+1;\n\t\t\t\tpush @E,$j;\n\t\t\t\t}\n\t\t\t}\n\t\tfor $k(0..@D-2){\n\t\t\t$D[$k]==$D[$k+1] and $f++;\n\t\t\t}\n\t\t\n\t\tfor $r(@D){\n\t\t\t$b[$r] eq \"#\"? ($b[$r]=\".\"):($f++)\n\t\t\t}\n\t\t\t\n\t\tfor $r(@E){\n\t\t\t$c[$r] eq \"#\"? ($c[$r]=\".\"):($f++)\n\t\t\t}\n\t\t\t\n\t\t$f and $F++;\n\t\t\n\t\t@a=@b;\n\t\t@b=@c;\n\t\t\n\t\t}\n\t\t\n\t\t$_ eq \"#\" and $F++ for (@a,@b);\n\t\t\n\t\tprint $F? \"NO\\n\":\"YES\\n\"\n\t\n\t}"}], "negative_code": [{"source_code": "while(<>){\n\t$n=$_;\n\tchomp($a=<>);\n\tchomp($b=<>);\n\t@a=split//,$a;\n\t@b=split//,$b;\n\t$F=0;\n\tfor $i(0..$n-3){\n\t\tchomp($c=<>);\n\t\t@c=split//,$c;\n\t\t@D=();\n\t\t@E=();\n\t\t$f=0;\n\t\t$g=0;\n\t\tfor $j(0..$n-1){\n\t\t\tif ($a[$j] eq \"#\"){\n\t\t\t\t$g++;\n\t\t\t\t$j==0 and $f++;\n\t\t\t\t$j==$n and $f++;\n\t\t\t\tpush @D,$j-1,$j,$j+1;\n\t\t\t\tpush @E,$j;\n\t\t\t\t}\n\t\t\t}\n\t\tfor $k(0..@D-2){\n\t\t\t$D[$k]==$D[$k+1] and $f++;\n\t\t\t}\n\t\t\n\t\tfor $r(@D){\n\t\t\t$b[$r] eq \"#\"? ($b[$r]=\".\"):($f++)\n\t\t\t}\n\t\t\t\n\t\tfor $r(@E){\n\t\t\t$c[$r] eq \"#\"? ($c[$r]=\".\"):($f++)\n\t\t\t}\n\t\t\t\n\t\t$f and $F++;\n\t\t\n\t\t@a=@b;\n\t\t@b=@c;\n\t\t\n\t\t}\n\t\t\n\t\t$_ eq \"#\" and $F++ for (@a,@b);\n\t\t\n\t\tprint $F? \"No\\n\":\"Yes\\n\"\n\t\n\t}"}], "src_uid": "b08ba52eb4c36b75dacf56dad6c2e670"} {"nl": {"description": "You are given a chess board with $$$n$$$ rows and $$$n$$$ columns. Initially all cells of the board are empty, and you have to put a white or a black knight into each cell of the board.A knight is a chess piece that can attack a piece in cell ($$$x_2$$$, $$$y_2$$$) from the cell ($$$x_1$$$, $$$y_1$$$) if one of the following conditions is met: $$$|x_1 - x_2| = 2$$$ and $$$|y_1 - y_2| = 1$$$, or $$$|x_1 - x_2| = 1$$$ and $$$|y_1 - y_2| = 2$$$. Here are some examples of which cells knight can attack. In each of the following pictures, if the knight is currently in the blue cell, it can attack all red cells (and only them). A duel of knights is a pair of knights of different colors such that these knights attack each other. You have to put a knight (a white one or a black one) into each cell in such a way that the number of duels is maximum possible.", "input_spec": "The first line contains one integer $$$n$$$ ($$$3 \\le n \\le 100$$$) \u2014 the number of rows (and columns) in the board.", "output_spec": "Print $$$n$$$ lines with $$$n$$$ characters in each line. The $$$j$$$-th character in the $$$i$$$-th line should be W, if the cell ($$$i$$$, $$$j$$$) contains a white knight, or B, if it contains a black knight. The number of duels should be maximum possible. If there are multiple optimal answers, print any of them.", "sample_inputs": ["3"], "sample_outputs": ["WBW\nBBB\nWBW"], "notes": "NoteIn the first example, there are $$$8$$$ duels: the white knight in ($$$1$$$, $$$1$$$) attacks the black knight in ($$$3$$$, $$$2$$$); the white knight in ($$$1$$$, $$$1$$$) attacks the black knight in ($$$2$$$, $$$3$$$); the white knight in ($$$1$$$, $$$3$$$) attacks the black knight in ($$$3$$$, $$$2$$$); the white knight in ($$$1$$$, $$$3$$$) attacks the black knight in ($$$2$$$, $$$1$$$); the white knight in ($$$3$$$, $$$1$$$) attacks the black knight in ($$$1$$$, $$$2$$$); the white knight in ($$$3$$$, $$$1$$$) attacks the black knight in ($$$2$$$, $$$3$$$); the white knight in ($$$3$$$, $$$3$$$) attacks the black knight in ($$$1$$$, $$$2$$$); the white knight in ($$$3$$$, $$$3$$$) attacks the black knight in ($$$2$$$, $$$1$$$). "}, "positive_code": [{"source_code": "#!/usr/bin/perl -w\nuse strict;\nuse warnings;\n\nmy $n = ;\n\nmy $rB = [];\n\n$rB->[0]->[0] = \"W\";\nmy @q = (1,2,-1, 2,-2, 1, -1, -2);\nmy @p = (2,1, 2,-1, 1,-2, -2, -1);\n\nmy $changed = 1;\nwhile ( $changed eq 1 ) {\n$changed = 0;\nfor my $i ( 0 .. $n - 1 ) {\nfor my $j ( 0 .. $n - 1 ) {\n for my $u ( 0 .. $#q ) {\n next if ( $j+$p[$u] < 0 or $j+$p[$u] > $n - 1 );\n next if ( $i+$q[$u] < 0 or $i+$q[$u] > $n - 1 );\n\n if ( not defined $rB->[$i]->[$j] ) {\n if ( defined $rB->[$i+$q[$u]]->[$j+$p[$u]] ) {\n if ( $rB->[$i+$q[$u]]->[$j+$p[$u]] eq \"W\") {\n $rB->[$i]->[$j] = \"B\"; $changed = 1;\n }\n if ( $rB->[$i+$q[$u]]->[$j+$p[$u]] eq \"B\") {\n $rB->[$i]->[$j] = \"W\"; $changed = 1;\n }\n #print \"$i $j $u\\n\";\n #prnt();\n }\n }\n }\n}\n}\n}\n\nprnt();\n\nsub prnt {\n\nfor my $i ( 0 .. $n - 1 ) {\nfor my $j ( 0 .. $n - 1 ) {\n my $v = defined $rB->[$i]->[$j] ? $rB->[$i]->[$j] : \"B\";\n print $v;\n}\nprint \"\\n\";\n}\n#print \"\\n\";\n\n}\n"}, {"source_code": "$\\ = $/;\n\nwhile( <> ){\n\tchomp;\n\t\n\t( 'WB' x $_ ) =~ /\n\t\t\\B (?= (.{$_}) )\n\t\t(?{ print $1 })\n\t\t(*F)\n\t\t/x;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile( my $n = <> ){\n\tchomp $n;\n\t\n\t$_ = 'WB' x $n;\n\t\n\t@_ = ();\n\t\n\tfor my $i ( 1 .. $n ){\n\t\tpush @_, substr $_, $i % 2, $n;\n\t\t}\n\t\n\tprint for @_;\n\t}"}], "negative_code": [], "src_uid": "09991e8e16cd395c7ce459817a385988"} {"nl": {"description": "Consider the array $$$a$$$ composed of all the integers in the range $$$[l, r]$$$. For example, if $$$l = 3$$$ and $$$r = 7$$$, then $$$a = [3, 4, 5, 6, 7]$$$.Given $$$l$$$, $$$r$$$, and $$$k$$$, is it possible for $$$\\gcd(a)$$$ to be greater than $$$1$$$ after doing the following operation at most $$$k$$$ times? Choose $$$2$$$ numbers from $$$a$$$. Permanently remove one occurrence of each of them from the array. Insert their product back into $$$a$$$. $$$\\gcd(b)$$$ denotes the greatest common divisor (GCD) of the integers in $$$b$$$.", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^5$$$) \u2014 the number of test cases. The description of test cases follows. The input for each test case consists of a single line containing $$$3$$$ non-negative integers $$$l$$$, $$$r$$$, and $$$k$$$ ($$$1 \\leq l \\leq r \\leq 10^9, \\enspace 0 \\leq k \\leq r - l$$$).", "output_spec": "For each test case, print \"YES\" if it is possible to have the GCD of the corresponding array greater than $$$1$$$ by performing at most $$$k$$$ operations, and \"NO\" otherwise (case insensitive).", "sample_inputs": ["9\n1 1 0\n3 5 1\n13 13 0\n4 4 0\n3 7 4\n4 10 3\n2 4 0\n1 7 3\n1 5 3"], "sample_outputs": ["NO\nNO\nYES\nYES\nYES\nYES\nNO\nNO\nYES"], "notes": "NoteFor the first test case, $$$a = [1]$$$, so the answer is \"NO\", since the only element in the array is $$$1$$$.For the second test case the array is $$$a = [3, 4, 5]$$$ and we have $$$1$$$ operation. After the first operation the array can change to: $$$[3, 20]$$$, $$$[4, 15]$$$ or $$$[5, 12]$$$ all of which having their greatest common divisor equal to $$$1$$$ so the answer is \"NO\".For the third test case, $$$a = [13]$$$, so the answer is \"YES\", since the only element in the array is $$$13$$$.For the fourth test case, $$$a = [4]$$$, so the answer is \"YES\", since the only element in the array is $$$4$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $l, $r, $k ) = split;\n\t\n\tif( $l == $r and $l > 1 ){\n\t\tprint 'YES';\n\t\tnext;\n\t\t}\n\t\n\t$l % 2 or $l ++;\n\t$r % 2 or $r --;\n\t\n\tmy $odds = ( $r - $l + 2 >> 1 );\n\t\n\tprint $odds > $k ? 'NO' : 'YES';\n\t}"}, {"source_code": "for(1..<>) {\r\n chomp(my $in = <>);\r\n my ($l, $r, $k) = split / /, $in;\r\n $l == $r && $l > 1 and print \"YES\\n\" and next;\r\n $sn = $l%2 == 0 && $r%2 == 0 ? ($r-$l)/2 : int(($r-$l)/2) + 1;\r\n print $k >= $sn ? \"YES\\n\" : \"NO\\n\";\r\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $l, $r, $k ) = split;\n\t\n\tif( $l == $r and $l > 1 ){\n\t\tprint 'YES';\n\t\tnext;\n\t\t}\n\t\n\tmy $odds = ( $r - $l >> 1 ) + ( $l % 2 );\n\t\n\tprint $odds > $k ? 'NO' : 'YES';\n\t}"}], "src_uid": "9363df0735005832573ef4d17b6a8302"} {"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,\u2009c). The tail of the snake is located at (1,\u20091), then it's body extends to (1,\u2009m), then goes down 2 rows to (3,\u2009m), then goes left to (3,\u20091) 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200950). 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": "my ($n, $m) = split \" \", <>;\nfor my $i(1..$n) {\n if ($i % 2) {\n print '#' x $m, \"\\n\";\n }\n else {\n if ($i % 4 == 0) {\n print '#', '.' x int($m-1) , \"\\n\";\n }\n else {\n print '.' x int($m-1) , \"#\\n\";\n }\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\n($n, $m) = split / /, <>;\nforeach my $i (1..$n) {\n\tif ($i%2 == 1) {\n\t\tsay '#' x $m;\n\t} elsif ($i/2%2 == 1) {\n\t\tsay '.' x ($m-1) . '#';\n\t} else {\n\t\tsay '#' . '.' x ($m-1);\n\t}\n}"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\n\n$\\ = \"\\n\";\n\nmy ($n, $m) = split ' ', <>;\nfor my $i (1..$n) {\n if ($i % 2 == 1) {\n print '#' x $m;\n } elsif ($i % 4 == 0) {\n print '#', '.' x ($m - 1);\n } else {\n print '.' x ($m - 1) . '#';\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $n, my $m;\n($n, $m) = split ' ',readline;\n\nmy @matrix;\nfor(my $i = 0; $i < $n; $i++){\n $matrix[$i] = [('.') x $m];\n}\nmy $r=0; my $c = 0; my $d = 1;\nwhile($r < $n){\n $matrix[$r][$c] = '#';\n $c += $d;\n if($c < 0 || $c >= $m){\n $c -= $d;\n $r++;\n if($r >= $n) { last; }\n $matrix[$r][$c] = '#';\n $r++;\n $d *= -1;\n }\n}\nfor(my $i = 0; $i < $n; $i++){\n for(my $j = 0; $j < $m; $j++){\n print $matrix[$i][$j];\n }\n print \"\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\nuse v5.18;\nuse integer;\n\nmy ($n, $m) = split(' ', <>);\n\nsay('#' x $m);\n\nmy $left = 0;\n\nforeach (1..(($n-1) / 2))\n{\n if ($left) {\n say('#' . '.' x ($m - 1));\n }\n else\n {\n say('.' x ($m - 1) . '#');\n }\n $left = !$left;\n say '#' x $m;\n}\n"}, {"source_code": "$\\=$/;\nwhile(<>){\n ($n, $m) = split;\n $s = '#' x $m;\n for $i (1 .. $n){\n $_ = $s;\n if (not $i % 2){\n s/#/./g;\n $i % 4? s/.$/#/ : s/./#/;\n }\n print\n }\n }"}, {"source_code": "($n, $m) = split \" \", <>;\n\t$r = $_ % 4,\n\t$_ = '#' x $m,\n\t( eval qw(s/(?;\n\t$q = qw(. .$)[$_ % 4 / 2],\n\t$_ = qw(. #)[$_ % 2] x $m,\n\ts/$q/#/,\n\tprint $_.$/\n\tfor 1 .. $n"}], "negative_code": [{"source_code": " $\\=$/;\n while(<>){\n ($n, $m) = split;\n $s = '#' x $m;\n for $i (1 .. $n){\n $_ = $s;\n if (not $i % 2){\n s/#/./g;\n $i % 4? s/.$/#/ : s/./#/;\n }\n print\n }\n print'--'\n }"}], "src_uid": "2a770c32d741b3440e7f78cd5670d54d"} {"nl": {"description": "There are $$$n$$$ distinct points on a coordinate line, the coordinate of $$$i$$$-th point equals to $$$x_i$$$. Choose a subset of the given set of points such that the distance between each pair of points in a subset is an integral power of two. It is necessary to consider each pair of points, not only adjacent. Note that any subset containing one element satisfies the condition above. Among all these subsets, choose a subset with maximum possible size.In other words, you have to choose the maximum possible number of points $$$x_{i_1}, x_{i_2}, \\dots, x_{i_m}$$$ such that for each pair $$$x_{i_j}$$$, $$$x_{i_k}$$$ it is true that $$$|x_{i_j} - x_{i_k}| = 2^d$$$ where $$$d$$$ is some non-negative integer number (not necessarily the same for each pair of points).", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) \u2014 the number of points. The second line contains $$$n$$$ pairwise distinct integers $$$x_1, x_2, \\dots, x_n$$$ ($$$-10^9 \\le x_i \\le 10^9$$$) \u2014 the coordinates of points.", "output_spec": "In the first line print $$$m$$$ \u2014 the maximum possible number of points in a subset that satisfies the conditions described above. In the second line print $$$m$$$ integers \u2014 the coordinates of points in the subset you have chosen. If there are multiple answers, print any of them.", "sample_inputs": ["6\n3 5 4 7 10 12", "5\n-1 2 5 8 11"], "sample_outputs": ["3\n7 3 5", "1\n8"], "notes": "NoteIn the first example the answer is $$$[7, 3, 5]$$$. Note, that $$$|7-3|=4=2^2$$$, $$$|7-5|=2=2^1$$$ and $$$|3-5|=2=2^1$$$. You can't find a subset having more points satisfying the required property."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy @two = map { 2 ** $_ } 0 .. 31;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h = map { $_ => 1 } @_;\n\t\n\tmy @ans;\n\t\n\tOUT:\n\tfor my $i ( @_ ){\n\t\tfor my $j ( @two ){\n\t\t\tif( exists $h{ $i + $j } ){\n\t\t\t\t@ans = ( $i, $i + $j );\n\t\t\t\tif( exists $h{ $i + $j + $j } ){\n\t\t\t\t\t@ans = ( $i, $i + $j, $i + $j + $j );\n\t\t\t\t\tlast OUT;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint @ans ? ~~ @ans . \"\\n@ans\" : 1 . \"\\n\" . $_[ 0 ];\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = sort { $a <=> $b } split ' ', <>;\n\t\n\tmy @ans;\n\t\n\tfor my $i ( 1 .. @_ - 2 ){\n\t\tmy $d1 = $_[ $i - 0 ] - $_[ $i - 1 ];\n\t\tmy $d2 = $_[ $i + 1 ] - $_[ $i - 0 ];\n\t\tnext if $d1 != $d2;\n\t\tnext if ( sprintf \"%b\", $d1 ) !~ /^10*$/;\n\t\t@ans = @_[ $i - 1, $i, $i + 1 ];\n\t\tlast;\n\t\t}\n\t\n\tnext if @ans and print ~~ @ans . \"\\n@ans\";\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tmy $d1 = $_[ $i + 1 ] - $_[ $i ];\n\t\tnext if ( sprintf \"%b\", $d1 ) !~ /^10*$/;\n\t\t@ans = @_[ $i, $i + 1 ];\n\t\tlast;\n\t\t}\n\t\n\tnext if @ans and print ~~ @ans . \"\\n@ans\";\n\t\n\tprint for 1, $_[ 0 ];\n\t}"}], "src_uid": "f413556df7dc980ca7e7bd1168d700d2"} {"nl": {"description": "You are given n integers a1,\u2009a2,\u2009...,\u2009an. Find the number of pairs of indexes i,\u2009j (i\u2009<\u2009j) that ai\u2009+\u2009aj is a power of 2 (i. e. some integer x exists so that ai\u2009+\u2009aj\u2009=\u20092x).", "input_spec": "The first line contains the single positive integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of integers. The second line contains n positive integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109).", "output_spec": "Print the number of pairs of indexes i,\u2009j (i\u2009<\u2009j) that ai\u2009+\u2009aj 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,\u20094) and (2,\u20094).In the second example all pairs of indexes (i,\u2009j) (where i\u2009<\u2009j) include in answer."}, "positive_code": [{"source_code": "<>; $h{$_}++ for split \" \", <>;\nfor $p (map 1<<$_, 1..30) {\n\tfor $b1 (keys %h) {\n\t\t$b2 = $p - $b1;\n\t\t$h{$b2} and do { \n\t\t\t$c += $b1 == $b2? $h{$b1} * ($h{$b1} - 1): $h{$b1} * $h{$b2} \n\t\t};\n\t}\n}\nprint $c/2, \"\\n\";\n"}, {"source_code": "<>; @a = split \" \", <>; $h{$_}++ for @a;\n@b = sort {$a <=> $b} keys %h;\nfor $p (map 1<<$_, 1..30) {\n\tfor $b1 (@b) {\n\t\t$b2 = $p - $b1;\n\t\t$h{$b2} and do { \n\t\t\t$c += $b1 == $b2? $h{$b1} * ($h{$b1} - 1): $h{$b1} * $h{$b2} \n\t\t};\n\t}\n}\nprint $c/2, \"\\n\";\n"}], "negative_code": [], "src_uid": "b812d2d3a031dadf3d850605d2e78e33"} {"nl": {"description": "There is a square field of size $$$n \\times n$$$ in which two cells are marked. These cells can be in the same row or column.You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes.For example, if $$$n=4$$$ and a rectangular field looks like this (there are asterisks in the marked cells):$$$$$$ \\begin{matrix} . & . & * & . \\\\ . & . & . & . \\\\ * & . & . & . \\\\ . & . & . & . \\\\ \\end{matrix} $$$$$$Then you can mark two more cells as follows$$$$$$ \\begin{matrix} * & . & * & . \\\\ . & . & . & . \\\\ * & . & * & . \\\\ . & . & . & . \\\\ \\end{matrix} $$$$$$If there are several possible solutions, then print any of them.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 400$$$). Then $$$t$$$ test cases follow. The first row of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 400$$$)\u00a0\u2014 the number of rows and columns in the table. The following $$$n$$$ lines each contain $$$n$$$ characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of $$$n$$$ for all test cases do not exceed $$$400$$$. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists.", "output_spec": "For each test case, output $$$n$$$ rows of $$$n$$$ characters\u00a0\u2014 a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them.", "sample_inputs": ["6\n4\n..*.\n....\n*...\n....\n2\n*.\n.*\n2\n.*\n.*\n3\n*.*\n...\n...\n5\n.....\n..*..\n.....\n.*...\n.....\n4\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**.."], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\tmy( $two_stars ) = grep m/\\*.*\\*/, @_;\n\t\n\tif( defined $two_stars ){\n\t\tfor( @_ ){\n\t\t\t$_ ne $two_stars and do {\n\t\t\t\t$_ = $two_stars;\n\t\t\t\tlast;\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\telse{\n\t\tmy @lines = grep m/\\*/, @_;\n\t\t\n\t\tif( $lines[ 0 ] eq $lines[ 1 ] ){\n\t\t\tm/\\*/ and s/\\./*/ for @_;\n\t\t\t}\n\t\telse{\n\t\t\tmy @idx = map { index $_, '*' } @lines;\n\t\t\tfor( @_ ){\n\t\t\t\tnext if not m/\\*/;\n\t\t\t\tsubstr $_, $idx[ 0 ], 1, '*';\n\t\t\t\tsubstr $_, $idx[ 1 ], 1, '*';\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint for @_;\n\t}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @p = ();\r\n my @s = ();\r\n for(my $i=0;$i<$n;$i++){\r\n my $s1 = ;\r\n for(my $j=0;$j<$n;$j++){\r\n my $c1 = substr($s1,$j,1);\r\n if( $c1 eq '*' ){\r\n push(@p,+[$i,$j]);\r\n }\r\n }\r\n push(@s,$s1);\r\n }\r\n my %r = (); my %c = ();\r\n foreach my $p1 (@p){\r\n $r{$p1->[0]} = 1;\r\n $c{$p1->[1]} = 1;\r\n }\r\n my @r = map { $_ - 0 } sort keys %r;\r\n my @c = map { $_ - 0 } sort keys %c;\r\n if( $#c == 0 ){\r\n for(my $i=0;$i<$n;$i++){\r\n if( $c[0] != $i ){\r\n push(@c,$i);\r\n last;\r\n }\r\n }\r\n }\r\n if( $#r == 0 ){\r\n for(my $i=0;$i<$n;$i++){\r\n if( $r[0] != $i ){\r\n push(@r,$i);\r\n last;\r\n }\r\n }\r\n }\r\n foreach my $r1 (@r){\r\n foreach my $c1 (@c){\r\n substr($s[$r1],$c1,1) = '*';\r\n }\r\n }\r\n for(my $i=0;$i<$n;$i++){\r\n print $s[$i];\r\n }\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tchomp;\r\n\t@_ = map ~~<>, 1 .. $_;\r\n\tchomp @_;\r\n\t\r\n\ty/.*/01/ for @_;\r\n\t\r\n\tmy( $two ) = grep m/1.*1/, @_;\r\n\t\r\n\tif( $two ){\r\n\t\tfor( @_ ){\r\n\t\t\tm/1/ or $_ = $two and last;\r\n\t\t\t}\r\n\t\t}\r\n\telse{\r\n\t\tmy @L = grep m/1/, @_;\r\n\t\t\r\n\t\tif( $L[ 0 ] eq $L[ 1 ] ){\r\n\t\t\tm/1/ and s/0/1/ for @_;\r\n\t\t\t}\r\n\t\telse{\r\n\t\t\tmy $B = sprintf \"%0${_}b\",\r\n\t\t\t\teval join '|', map eval,\r\n\t\t\t\tmap '0b' . $_, @L;\r\n\t\t\t\t\r\n\t\t\tm/1/ and $_ = $B for @_;\r\n\t\t\t}\r\n\t\t}\r\n\t\r\n\ty/01/.*/ for @_;\r\n\t\r\n\tprint for @_;\r\n\t}"}], "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": "use v5.20;\nsub { \n &{sub { say int <> / 2 + 1 }}\n while ($_[0]--);\n} -> (my $t = <>);"}, {"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n if( $n % 2 == 1 ){\n my $nn = 1 + (($n - 1)/2);\n print \"$nn\\n\";\n } else {\n my $nn = 1 + ($n / 2);\n print \"$nn\\n\";\n }\n \n}\n\nexit(0);\n\n"}], "negative_code": [{"source_code": "&{sub { \n sub { print int <> / 2 + 1 } -> ()\n while ($_[0]--);\n}}(<>);"}], "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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nmy $len = 10;\n\nmy $N = 10;\n\n##@_ = ( -1 ) x ( ??? );\n\nfor my $i ( 0 .. 2 ** $N - 1 ){\n\tmy $ib = sprintf \"%0${len}b\", $i;\n\t\n\tmy $j = 0;\n\tmy $sum = 0;\n\t\n\tmy $rib = reverse $ib;\n\t\n\t$rib =~ /\n\t\t(.)\n\t\t(?{ $1 and $sum += 3 ** $j })\n\t\t(?{ $j ++ })\n\t\t(*F)\n\t\t/x;\n\t\n\t$debug and print \"$ib $sum\";\n\t\n\t$_[ $sum ] = $sum;\n\t}\n\n$debug and print \"[@_]\";\n\nmy $k = $_[ @_ - 1 ];\n\nfor my $i ( reverse 0 .. @_ - 1 ){\n\tif( defined $_[ $i ] ){\n\t\t$k = $_[ $i ]\n\t\t}\n\telse{\n\t\t$_[ $i ] = $k;\n\t\t}\n\t}\n\n$debug and print \"[@_]\";\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tprint $_[ $_ - 0 ];\n\t}"}], "negative_code": [], "src_uid": "5953b898995a82edfbd42b6c0f7138af"} {"nl": {"description": "On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy.The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as vi. The child spend vf1\u2009+\u2009vf2\u2009+\u2009...\u2009+\u2009vfk energy for removing part i where f1,\u2009f2,\u2009...,\u2009fk are the parts that are directly connected to the i-th and haven't been removed.Help the child to find out, what is the minimum total energy he should spend to remove all n parts.", "input_spec": "The first line contains two integers n and m (1\u2009\u2264\u2009n\u2009\u2264\u20091000; 0\u2009\u2264\u2009m\u2009\u2264\u20092000). The second line contains n integers: v1,\u2009v2,\u2009...,\u2009vn (0\u2009\u2264\u2009vi\u2009\u2264\u2009105). Then followed m lines, each line contains two integers xi and yi, representing a rope from part xi to part yi (1\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009n;\u00a0xi\u2009\u2260\u2009yi). Consider all the parts are numbered from 1 to n.", "output_spec": "Output the minimum total energy the child should spend to remove all n parts of the toy.", "sample_inputs": ["4 3\n10 20 30 40\n1 4\n1 2\n2 3", "4 4\n100 100 100 100\n1 2\n2 3\n2 4\n3 4", "7 10\n40 10 20 10 20 80 40\n1 5\n4 7\n4 5\n5 2\n5 7\n6 4\n1 6\n1 3\n4 3\n1 4"], "sample_outputs": ["40", "400", "160"], "notes": "NoteOne of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20\u2009+\u200910\u2009+\u200910\u2009+\u20090\u2009=\u200940, which is the minimum.In the second sample, the child will spend 400 no matter in what order he will remove the parts."}, "positive_code": [{"source_code": "use strict;\nuse warnings;\n\n$_ = readline;chomp;my($n,$m) = split \" \";\n$_ = readline;chomp;my @v = split \" \";\n\nmy $et = 0;\n\nforeach (1..$m) {\n $_ = readline;chomp;my($a,$b) = split \" \";\n $et += $v[$a - 1]>$v[$b - 1]?$v[$b - 1]:$v[$a - 1];\n}\n\n\nprint \"$et\\n\";\n"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\n\nuse List::Util qw(reduce);\n\n$_ = readline;chomp;my($n,$m) = split \" \";\n$_ = readline;chomp;my @p = map { { r => [], es => $_ } } (split \" \");\n\nforeach (1..$m) {\n $_ = readline;chomp;my($a,$b) = split \" \";\n push(@{$p[$a - 1]->{r}},$b - 1);\n push(@{$p[$b - 1]->{r}},$a - 1);\n}\n\nmy $et = 0;\n\nforeach (1..$n) {\n map { $_->{et} = reduce { $a + $b } (map { $p[$_]->{es} } @{$_->{r}}) } @p;\n my @r = sort { $a->{et} cmp $b->{et} } @p;\n $r[0]->{es} = 0;\n $et += $r[0]->{et};\n}\n\nprint \"$et\\n\";\n"}], "src_uid": "2e6bf9154d9da6ac134b52144d5322ca"} {"nl": {"description": "Little C loves number \u00ab3\u00bb very much. He loves all things about it.Now he has a positive integer $$$n$$$. He wants to split $$$n$$$ into $$$3$$$ positive integers $$$a,b,c$$$, such that $$$a+b+c=n$$$ and none of the $$$3$$$ integers is a multiple of $$$3$$$. Help him to find a solution.", "input_spec": "A single line containing one integer $$$n$$$ ($$$3 \\leq n \\leq 10^9$$$) \u2014 the integer Little C has.", "output_spec": "Print $$$3$$$ positive integers $$$a,b,c$$$ in a single line, such that $$$a+b+c=n$$$ and none of them is a multiple of $$$3$$$. It can be proved that there is at least one solution. If there are multiple solutions, print any of them.", "sample_inputs": ["3", "233"], "sample_outputs": ["1 1 1", "77 77 79"], "notes": null}, "positive_code": [{"source_code": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy $n=;chomp($n);\nmy $m=$n%3;my $out;\nif($m==0){\n if($n%9==0){\n\t$out=($n/3+4).\" \".($n/3-2).\" \".($n/3-2).\"\\n\";\n }\n else{\n\t$out=($n/3).\" \".($n/3).\" \".($n/3).\"\\n\";\n }\n}\nelsif($m==1){\n $out=($n/3+4).\" \".($n/3-2).\" \".($n/3-1).\"\\n\" if($n/3%3==0);\n $out=($n/3).\" \".($n/3).\" \".($n/3+1).\"\\n\" if($n/3%3==1);\n $out=($n/3).\" \".($n/3-1).\" \".($n/3+2).\"\\n\" if($n/3%3==2);\n}\nelse{\n $out=($n/3+4).\" \".($n/3-1).\" \".($n/3-1).\"\\n\" if($n/3%3==0);\n $out=($n/3).\" \".($n/3).\" \".($n/3+2).\"\\n\" if($n/3%3==2);\n $out=($n/3).\" \".($n/3-2).\" \".($n/3+4).\"\\n\" if($n/3%3==1);\n}\n$out=\"1 2 2\\n\" if($n==5);\nprint $out;"}, {"source_code": "$_ = <>;\n\n$m = $_ % 3 == 2;\n\nprint join ' ', $_ - 2 - $m, 1, 1 + $m"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tchomp;\n\t\n\t@_ = ( $_ - 2, 1, 1 );\n\t\n\t$debug and print \"@_\";\n\t\n\tif( $_[ 0 ] % 3 == 0 ){\n\t\t$_[ 0 ] --;\n\t\t$_[ 1 ] ++;\n\t\t}\n\t\n\tprint \"@_\";\n\t\n\t$debug and print '-' x 10;\n\t}"}], "negative_code": [{"source_code": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy $n=;chomp($n);\nmy $m=$n%3;my $out;\nif($m==0){\n if($n%9==0){\n\t$out=($n/3+4).\" \".($n/3-2).\" \".($n/3-2).\"\\n\";\n }\n else{\n\t$out=($n/3).\" \".($n/3).\" \".($n/3).\"\\n\";\n }\n}\nelsif($m==1){\n $out=($n/3+4).\" \".($n/3-2).\" \".($n/3-1).\"\\n\" if($n/3%3==0);\n $out=($n/3).\" \".($n/3).\" \".($n/3+1).\"\\n\" if($n/3%3==1);\n $out=($n/3).\" \".($n/3-1).\" \".($n/3+2).\"\\n\" if($n/3%3==2);\n}\nelse{\n $out=($n/3+4).\" \".($n/3-1).\" \".($n/3-1).\"\\n\" if($n/3%3==0);\n $out=($n/3).\" \".($n/3).\" \".($n/3+2).\"\\n\" if($n/3%3==2);\n $out=($n/3).\" \".($n/3-2).\" \".($n/3+4).\"\\n\" if($n/3%3==1);\n}\nprint $out;"}, {"source_code": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy $n=;chomp($n);\nmy $m=$n%3;my $out;\nif($m==0){\n if($n%9==0){\n\t$out=($n/3+4).\" \".($n/3-2).\" \".($n/3-2).\"\\n\";\n }\n else{\n\t$out=($n/3).\" \".($n/3).\" \".($n/3).\"\\n\";\n }\n}\nelsif($m==1){\n $out=($n/3+4).\" \".($n/3-2).\" \".($n/3-1).\"\\n\" if($n/3%3==0);\n $out=($n/3).\" \".($n/3).\" \".($n/3+1).\"\\n\" if($n/3%3!=0);\n}\nelse{\n $out=($n/3+4).\" \".($n/3-1).\" \".($n/3-1).\"\\n\" if($n/3%3==0);\n $out=($n/3).\" \".($n/3).\" \".($n/3+2).\"\\n\" if($n/3%3!=0);\n}\nprint $out;"}], "src_uid": "91d5147fb602298e08cbdd9f86d833f8"} {"nl": {"description": "Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.", "input_spec": "The first line contains integer q (1\u2009\u2264\u2009q\u2009\u2264\u20091000), the number of handle change requests. Next q lines contain the descriptions of the requests, one per line. Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20. The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handle new is not used and has not been used by anyone.", "output_spec": "In the first line output the integer n \u2014 the number of users that changed their handles at least once. In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old and new, separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new. You may output lines in any order. Each user who changes the handle must occur exactly once in this description.", "sample_inputs": ["5\nMisha ILoveCodeforces\nVasya Petrov\nPetrov VasyaPetrov123\nILoveCodeforces MikeMirzayanov\nPetya Ivanov"], "sample_outputs": ["3\nPetya Ivanov\nMisha MikeMirzayanov\nVasya VasyaPetrov123"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 2.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 01/14/2015 11:18:24 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.012;\n\nchomp(my $n = );\n\nmy %name;\nwhile ( $n-- ) {\n\tchomp($_ = );\n\tmy ($old, $new) = split(/ /);\n\tif ( exists($name{$old}) ) {\n\t\tmy $origin = $name{$old};\n\t\tdelete $name{$old};\n\t\t$name{$new} = $origin;\n\t}\n\telse {\n\t\t$name{$new} = $old;\n\t}\n}\nmy $size = keys %name;\nprint \"$size\\n\";\nfor my $key (keys %name) {\n\tprint \"$name{$key} $key\\n\";\n}\n"}, {"source_code": "$\\ = $/;\n<>; ($n,$m) = split,\n$h{$n} =~/./ ? ($h{$m} = $h{$n}, delete $h{$n}): ($h{$m} = $n) while <>;\nprint 0 + (keys %h);\nprint \"$v $k\" while ($k,$v) = each %h"}, {"source_code": "$\\ = $/;\n<>;\nundef %h;\nwhile(<>){\n\t($n,$m)=split;\n\tdefined $h{$n} ? ($h{$m} = $h{$n}, delete $h{$n}): ($h{$m} = $n);\n\t}\n$, = $\";\nprint scalar keys %h;\nwhile (($k,$v) = each %h){\n\tprint $v,$k\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\n<>; ($n,$m) = split,\n$h{$n} ? ($h{$m} = $h{$n}, delete $h{$n}): ($h{$m} = $n) while <>;\nprint 0 + %h;\nprint \"$v $k\" while ($k,$v) = each %h"}, {"source_code": "$\\ = $/;\n<>; ($n,$m) = split,\n$h{$n} ? ($h{$m} = $h{$n}, delete $h{$n}): ($h{$m} = $n) while <>;\nprint 0 + (keys %h);\nprint \"$v $k\" while ($k,$v) = each %h"}], "src_uid": "bdd98d17ff0d804d88d662cba6a61e8f"} {"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.\u00a0e. 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\r\n\r\nmy ($t) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\nwhile( $t -- > 0 ){\r\n my ($n,$d) = map { $_ - 0 } split(/\\s+/o,);\r\n my @A = sort { $a <=> $b } map { $_ - 0 } split(/\\s+/o,);\r\n my $c = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n $c++ if $A[$i] > $d;\r\n }\r\n if( $c == 0 or $A[0]+$A[1]<=$d ){\r\n print \"YES\\n\";\r\n } else {\r\n print \"NO\\n\";\r\n }\r\n}\r\n"}], "negative_code": [], "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 \u2014 $$$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 \u2014 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)$$$ \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\tmy @n = split ' ', <>;\n\t\n\tmy @c;\n\t\n\tmy %h;\n\t\n\tOUTER:\n\tfor( @n ){\n\t\tmy $c = 0;\n\t\t$h{ $_ } and do { push @c, 0; next };\n\t\t\n\t\twhile( @_ ){\n\t\t\tmy $i = shift @_;\n\t\t\t$h{ $i } ++;\n\t\t\t$c ++;\n\t\t\tif( $i == $_ ){\n\t\t\t\tpush @c, $c;\n\t\t\t\tnext OUTER;\n\t\t\t\t}\n\t\t\t}\n\t\tpush @c, 0;\n\t\t}\n\t\n\tprint \"@c\";\n\t}"}], "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$$$) \u2014 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$$$) \u2014 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 \u2014 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": "#!usr/bin/perl -w\nuse strict;\n\nmy $n=;\nmy @p=split/ /,;chomp($n,$p[-1]);\nmy @l;my $z=0;\nmy $max=0;\nif(scalar @p==1){print \"1\\n\";}\nelse{\n push @l,$p[0];\n for my $i(1..$#p){\n\tif($p[$i]<=2*$p[$i-1]){\n\t push @l,$p[$i];\n\t}\n\telse{\n\t while($z<$i){\n\t\tshift @l;\n\t\t$z++;\n\t }\n\t push @l,$p[$i];\n\t}\n\t$max = scalar @l if($max;\nmy @p=split/ /,;chomp($n,$p[-1]);\nmy @l;my $z=0;\nmy $max=0;\npush @l,$p[0];\nfor my $i(1..$#p){\n if($p[$i]<=2*$p[$i-1]){\n\tpush @l,$p[$i];\n }\n else{\n\t$max = scalar @l if($max;\nmy @p=split/ /,;chomp($n,$p[-1]);\nmy @l;my $z=0;\nmy $max=0;\nif(scalar @p==1){print \"1\\n\";}\nelse{\n push @l,$p[0];\n for my $i(1..$#p){\n\tif($p[$i]<=2*$p[$i-1]){\n\t push @l,$p[$i];\n\t}\n\telse{\n\t $max = scalar @l if($max);\n@a = split / /, <>;\npush @b, [$_, $a[$_]] foreach (0 .. $n-1);\n@b = sort {$a->[1] <=> $b->[1]} @b;\n@id = (0) x $n;\n$id[$_]=$b[$_]->[0] foreach (0 .. $n-1);\n($l, $r) = (0, 0);\n($cnt, $i) = (0, 0);\nwhile ($i < $n) {\n\tif ($id[$i] != $i) {\n\t\t$cnt>0 and say \"no\" and exit;\n\t\t++$cnt;\n\t\tif ($i < $id[$i]) {\n\t\t\t($l, $r) = ($i, $id[$i]);\n\t\t} else {\n\t\t\t($r, $l) = ($i, $id[$i]);\n\t\t}\n\t\tfor (my ($j,$k)=($l,$r); $j<=$r; ++$j,--$k) {\n\t\t\t$id[$j]!=$k and say \"no\" and exit;\n\t\t}\n\t\t$i = $r;\n\t}\n\t++$i;\n}\n++$l and ++$r;\nsay \"yes\\n$l $r\";"}, {"source_code": "chomp($n = <>);\n@a = split / /, <>;\nchomp @a;\n$L = 0;\nchomp $a[$#a];\n$L++ until $a[$L] > $a[$L+1];\nif($L == $#a){\n print \"yes\\n1 1\\n\";\n}else {\n #print \"current L = $L\\n\";\n $R = $L++;\n $R++ while $a[$R] > $a[$R+1] && $R < $#a;\n if($R == $#a){\n if($a[$#a] > $a[$L - 2] || $L == 1) {\n #print \"$a[$#a]| $a[$L-2] | $L\\n\";\n print \"yes\\n$L $n\\n\";\n }else {\n print \"no\";\n }\n }else{\n $p = $R++;\n $p++ while $a[$p] < $a[$p+1] && $p < $#a;\n print $p == $#a && $a[$R] > $a[$L-1] && $a[$R-1] < $a[$L-1] ? \"yes\\n$L $R\" : \"no\";\n }\n}\n"}, {"source_code": "chomp($n = <>);\n@c = split / /, <>;\nchomp @c;\n$L = $R = 0;\n$L++ while $c[$L] < $c[$L+1] && $L < $#c;\n$R = $L;\n$R++ while $c[$R] > $c[$R+1] && $R < $#c;\n@d = @c[0..($L-1)] if $L > 0;\npush @d, reverse @c[$L..$R];\npush @d, @c[($R+1)..$#c] if $R < $#c;\n$L++; $R++;\n@c = sort { $a <=> $b } @d;\nprint @c ~~ @d ? \"yes\\n$L $R\\n\" : \"no\";\n"}, {"source_code": "chomp($n = <>);\n@a = split / /, <>;\nchomp @a;\n$L = 0;\nchomp $a[$#a];\n$L++ until $a[$L] > $a[$L+1];\nif($L == $#a){\n print \"yes\\n1 1\\n\";\n}else {\n #print \"current L = $L\\n\";\n $R = $L++;\n $R++ while $a[$R] > $a[$R+1] && $R < $#a;\n if($R == $#a){\n print $a[$#a] > $a[$L - 2] || $L == 1 ? \"yes\\n$L $n\" : \"no\";\n }else{\n $p = $R++;\n $p++ while $a[$p] < $a[$p+1] && $p < $#a;\n print $p == $#a && $a[$R] > $a[$L-1] && $a[$R-1] < $a[$L-1] ? \"yes\\n$L $R\" : \"no\";\n }\n}\n"}, {"source_code": "<>;\n($a,@_)=split/ /,<>;\n$a[0]=$a;\n\nfor $b(@_,10**10){\n\t$i++;\n\tif ($b<$a && !$e){$e=$i}\n\tif ($b<$a && !$f){push @b,$b} \n\telse {\n\t\tif (@b && !$f++){@b=reverse @b; push @b, pop @a; push @a, @b}; \n\t\tpush @a,$b\n\t\t}\n\t$a=$b;\n\t}\n\n$a=shift @a;\t\nfor (@a){\n\tif ($_<$a){$g++}\n\t$a=$_\n\t}\nif ($g){\n\tprint \"no\\n\"\n\t}\nelse {\n\tprint \"yes\\n\";\n\tprint $e? ($e,\" \",$e+@b-1):(\"1 1\")\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = split / /, <>;\npush @b, [$_, $a[$_]] foreach (0 .. $n-1);\n@b = sort {$a->[1] <=> $b->[1]} @b;\n@id = (0) x $n;\n$id[$_]=$b[$_][0] foreach (0 .. $n-1);\n($l, $r) = (0, 0);\nwhile ($i < $n) {\n\tif ($id[$i] != $i) {\n\t\t$cnt++>0 and say \"no\" and exit;\n\t\tif ($i < $id[$i]) {\n\t\t\t($l, $r) = ($i, $id[$i]);\n\t\t} else {\n\t\t\t($r, $l) = ($i, $id[$i]);\n\t\t}\n\t\tfor (my ($j,$k)=($l,$r); $j<$k; ++$j,--$k) {\n\t\t\t$id[$j]!=$k and say \"no\" and exit;\n\t\t}\n\t\t$i = $r;\n\t}\n\t++$i;\n}\n++$l and ++$r;\nsay \"yes\\n$l $r\";"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = split / /, <>;\npush @b, [$_, $a[$_]] foreach (0 .. $n-1);\n@b = sort {$a->[1] <=> $b->[1]} @b;\n@id = (0) x $n;\n$id[$_]=$b[$_][0] foreach (0 .. $n-1);\n($l, $r) = (0, 0);\nwhile ($i < $n) {\n\tif ($id[$i] != $i) {\n\t\t$cnt++>0 and say \"no\" and exit;\n\t\t($l, $r) = ($i, $id[$i]);\n\t\t$l>$r and ($l, $r)=($r, $l);\n\t\tfor (my ($j,$k)=($l,$r); $j<$k; ++$j,--$k) {\n\t\t\t$id[$j]!=$k and say \"no\" and exit;\n\t\t}\n\t\t$i = $r;\n\t}\n\t++$i;\n}\n++$l and ++$r;\nsay \"yes\\n$l $r\";"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = split / /, <>;\npush @b, [$_, $a[$_]] foreach (0 .. $n-1);\n@b = sort {$a->[1] <=> $b->[1]} @b;\n@id = (0) x $n;\n$id[$_]=$b[$_][0] foreach (0 .. $n-1);\n($l, $r) = (0, 0);\n($cnt, $i) = (0, 0);\nwhile ($i < $n) {\n\tif ($id[$i] != $i) {\n\t\t$cnt>0 and say \"no\" and exit;\n\t\t++$cnt;\n\t\tif ($i < $id[$i]) {\n\t\t\t($l, $r) = ($i, $id[$i]);\n\t\t} else {\n\t\t\t($r, $l) = ($i, $id[$i]);\n\t\t}\n\t\tfor (my ($j,$k)=($l,$r); $j<=$k; ++$j,--$k) {\n\t\t\t$id[$j]!=$k and say \"no\" and exit;\n\t\t}\n\t\t$i = $r;\n\t}\n\t++$i;\n}\n++$l and ++$r;\nsay \"yes\\n$l $r\";"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = split / /, <>;\npush @b, [$_, $a[$_]] foreach (0 .. $n-1);\n@b = sort {$a->[1] <=> $b->[1]} @b;\n@id = (0) x $n;\n$id[$_]=$b[$_][0] foreach (0 .. $n-1);\n($l, $r) = (0, 0);\nwhile ($i < $n) {\n\tif ($id[$i] != $i) {\n\t\t$cnt++>0 and say \"no\" and exit;\n\t\t($l, $r) = ($i, $id[$i]);\n\t\tfor (my ($j,$k)=($l,$r); $j<$k; ++$j,--$k) {\n\t\t\t$id[$j]!=$k and say \"no\" and exit;\n\t\t}\n\t\t$i = $r;\n\t}\n\t++$i;\n}\n++$l and ++$r;\nsay \"yes\\n$l $r\";"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = split / /, <>;\npush @b, [$_, $a[$_]] foreach (0 .. $n-1);\n@b = sort {$a->[1] <=> $b->[1]} @b;\n@id = (0) x $n;\n$id[$_]=$b[$_][0] foreach (0 .. $n-1);\n($l, $r) = (1, 0, 0);\nwhile ($i < $n) {\n\tif ($id[$i] != $i) {\n\t\t$cnt++>0 and say \"no\" and exit;\n\t\t($l, $r) = ($i, $id[$i]);\n\t\tfor (my ($j,$k)=($l,$r); $j<$k; ++$j,--$k) {\n\t\t\t$id[$j]!=$k and say \"no\" and exit;\n\t\t}\n\t\t$i = $r;\n\t}\n\t++$i;\n}\n++$l and ++$r;\nsay \"yes\\n$l $r\";"}, {"source_code": "chomp($n = <>);\n@a = split / /, <>;\n$L = 0;\n$L++ until $a[$L] > $a[$L+1];\nif($L == $#a){\n print \"yes\\n1 1\\n\";\n}else {\n #print \"current L = $L\\n\";\n $R = $L++;\n $R++ while $a[$R] > $a[$R+1] && $R < $#a;\n if($R == $#a && ($a[$#a] < $a[$L - 1] || $L == 1)){\n print \"yes\\n$L $n\\n\";\n }else{\n $p = $R++;\n $p++ while $a[$p] < $a[$p+1] && $p < $#a;\n print $p == $#a && $a[$R] > $a[$L-1] && $a[$R-1] < $a[$L-1] ? \"yes\\n$L $R\" : \"no\";\n }\n}\n"}, {"source_code": "chomp($n = <>);\n@a = split / /, <>;\n$L = 0;\n$L++ until $a[$L] > $a[$L+1];\nif($L == $#a){\n print \"yes\\n1 1\\n\";\n}else {\n #print \"current L = $L\\n\";\n $R = $L++;\n $R++ while $a[$R] > $a[$R+1] && $R < $#a;\n if($R == $#a && $a[$#a] < $a[$L]){\n print \"yes\\n$L $n\\n\";\n }else{\n $p = $R++;\n $p++ while $a[$p] < $a[$p+1] && $p < $#a;\n print $p == $#a && $a[$R] > $a[$L-1] && $a[$R-1] < $a[$L-1] ? \"yes\\n$L $R\" : \"no\";\n }\n}\n"}, {"source_code": "chomp($n = <>);\n@a = split / /, <>;\n$L = 0;\n$L++ until $a[$L] > $a[$L+1];\nif($L == $#a){\n print \"yes\\n1 1\\n\";\n}else {\n #print \"current L = $L\\n\";\n $R = $L++;\n $R++ while $a[$R] > $a[$R+1] && $R < $#a;\n if($R == $#a){\n print \"yes\\n$L $n\\n\";\n }else{\n $p = $R++;\n $p++ while $a[$p] < $a[$p+1] && $p < $#a;\n print $p == $#a && $a[$R] > $a[$L-1] ? \"yes\\n$L $R\" : \"no\";\n }\n}\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\u2009\u2264\u2009n\u2009\u2264\u2009103). The next line contains n integers a1, a2, ..., an (ai\u2009=\u20090 or ai\u2009=\u20095). 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 \u2014 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 \u2014 0.In the second test you can make number 5555555550, it is a multiple of 90."}, "positive_code": [{"source_code": "chomp($n=<>);\nforeach (split / /, <>) {\n\t$five++ if $_==5;\n}\n$zero = $n-$five;\nprint \"-1\\n\" and exit if $zero==0;\nprint \"0\\n\" and exit if $five<9;\nprint \"5\" x ($five-$five%9) . \"0\"x$zero . \"\\n\";"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@c = (0) x 6;\n$c[$_]++ foreach (split / /, <>);\n$n5 = $c[5] - $c[5]%9;\n$c[0]==0 and say -1 and exit;\n$n5==0 and say 0 and exit;\nsay( ('5' x $n5) . ('0' x $c[0]) );"}, {"source_code": "<>;\nchomp($_=<>);\ns/ //g;\ns/./$&?($i++):($j++)/ge;\nprint ($j?($i>8?(\"5\"x($i-$i%9).\"0\"x$j):(\"0\")):(\"-1\"));"}, {"source_code": "<>;\nchomp($_=<>);\ns/ //g;\ns/./$&?($i++):($j++)/ge;\nprint ($j?($i>8?(\"5\"x($i-$i%9).\"0\"x$j):(\"0\")):(\"-1\"));"}, {"source_code": "my $n = <>;\nchomp $n;\nmy $d = my $f = <> =~ y/5//;\n$d-- while $d%9;\n$_ = '5' x $d . '0' x ($n-$f);\n$_ =~ s/^0+/0/;\nprint $f==$n? -1 : $_;"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@c = (0) x 6;\n$c[$_]++ foreach (split / /, <>);\n$n5 = $c[5] - $c[5]%9;\n$n5==0 and ($c[0]==0 and say -1 or say 0) and exit;\nsay( ('5' x $n5) . ('0' x $c[0]) );"}, {"source_code": "<>;\nchomp($_=<>);\ns/ //g;\ns/./$&?($i++):($j++)/ge;\nprint ($i>8 && $j?(\"5\"x($i-$i%9).\"0\"x$j):(-1));"}], "src_uid": "409b27044d5ec97b5315c92d4112376f"} {"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\u2009\u2264\u2009N\u2009\u2264\u200950). 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,\u2009N),\u2009...,\u2009(N,\u2009N) and the last row corresponds to cells with coordinates (1,\u20091),\u2009...,\u2009(N,\u20091).", "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,\u2009y1), (x1,\u2009y2), (x2,\u2009y1), (x2,\u2009y2)), has a non-zero area and be contained inside of the grid (that is, 0\u2009\u2264\u2009x1\u2009<\u2009x2\u2009\u2264\u2009N, 0\u2009\u2264\u2009y1\u2009<\u2009y2\u2009\u2264\u2009N), and result in the levels of Zombie Contamination as reported in the input."}, "positive_code": [{"source_code": "sub fail { print 'No'; exit }\n<>; \nwhile ($_ = <>) {\n\t/^0+$/ or last;\n}\t\n/^(0*)([1][2]+[1])0*$/ or fail;\n$offset = $1;\n$width = length $2;\nwhile ($_ = <>) {\n\t/^(0*)([2][4]+[2])0*$/ or last;\n\tfail unless $1 eq $offset and length $2 == $width;\n\t$height++;\n}\nfail unless $height > 0;\nfail unless /^(0*)([1][2]+[1])0*$/ and $1 eq $offset and length $2 == $width;\nfor (<>) {\n\t/^0+$/ or fail;\n}\nprint 'Yes';\n"}], "negative_code": [{"source_code": "sub fail { print 'No'; exit }\n<>; \nwhile ($_ = <>) {\n\t/^0+$/ or last;\n}\t\n/^(0*)([1][2]+[1])(0*)$/ or fail;\n$offset = $1;\n$width = length $2;\nwhile ($_ = <>) {\n\t/^(0*)([2][4]+[2])/ or last;\n\tfail unless $1 eq $offset and length $2 == $width;\n\t$height++;\n}\nfail unless $height > 0;\nfail unless /^(0*)([1][2]+[1])/ and $1 eq $offset and length $2 == $width;\nfor (<>) {\n\t/^0+$/ or fail;\n}\nprint 'Yes';\n"}], "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$$$) \u2014 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$$$) \u2014 the number of eggs, stickers and toys. All queries are independent.", "output_spec": "Print $$$T$$$ integers (one number per query) \u2014 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": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my ($a, $s, $t, $n, @a, $st, $so, $to);\n for my $i (1..$q) {\n $a= <$in>; chomp($a);\n @a= split(' ', $a);\n\n $n= $a[0];\n $s= $a[1];\n $t= $a[2];\n\n $st= $s + $t - $n;\n\n if($st == $n) {\n print \"1\\n\";\n next;\n }\n\n $so= $s - $st;\n $to= $t - $st;\n\n print(\n $so < $to\n ? ($to+1)\n : ($so+1)\n );\n\n print \"\\n\";\n }\n\n}\n\nmain() unless caller();\n\n"}], "negative_code": [], "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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 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\u2009\u2264\u2009si\u2009\u2264\u2009100) \u2014 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": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=<>;\nchomp @_;\nshift @_;\n\ns/-//g for @_;\n\nfor (@_){\n\tif (/^\\d{6}$/){\n\t\t@n=split//;\n\t\t$f=shift @n;\n\t\t$nT=$nP=0;\n\t\tfor $i(@n){\n\t\t\t$f!=$i and $nT++;\n\t\t\t$i>=$f and $nP++;\n\t\t\t$f=$i;\n\t\t\t}\n\t\t$nT && $nP and $_=3;\n\t\t$nT && !$nP and $_=2;\n\t\t!$nT && $nP and $_=1; \n\t\t}\n\t\n\t}\n\t\n# print \"@_\\n\";\n\nfor (@_){\n\ts/^\\d+ / /;\n\t}\n\n$_=join\"\",@_;\n# print; n();\n\nwhile (/31|21|32/){\nwhile (/31/) {s/31/13/g};\nwhile (/21/) {s/21/12/g};\nwhile (/32/) {s/32/23/g};\n}\n\n$j=0;\nfor $i(1..100){\n\t/1{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?1{$j})/(push @T,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@T\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/2{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?2{$j})/(push @P,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@P\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/3{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?3{$j})/(push @M,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@M\\n\";\n\nprint \"If you want to call a taxi, you should call: \",(join\", \",@T),\".\\n\";\nprint \"If you want to order a pizza, you should call: \",(join\", \",@P),\".\\n\";\nprint \"If you want to go to a cafe with a wonderful girl, you should call: \",(join\", \",@M),\".\\n\";\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=<>;\nchomp @_;\nshift @_;\n\ns/-//g for @_;\n\nfor (@_){\n\tif (/^\\d{6}$/){\n\t\t@n=split//;\n\t\t$f=shift @n;\n\t\t$nT=$nP=0;\n\t\tfor $i(@n){\n\t\t\t$f!=$i and $nT++;\n\t\t\t$i>=$f and $nP++;\n\t\t\t$f=$i;\n\t\t\t}\n\t\t$nT && $nP and $_=3;\n\t\t$nT && !$nP and $_=2;\n\t\t!$nT && $nP and $_=1; \n\t\t}\n\t\n\t}\n\t\n# print \"@_\\n\";\n\nfor (@_){\n\ts/^\\d+ / /;\n\t}\n\n$_=join\"\",@_;\n# print; n();\n\n# while (/31|21|32/){s/31|21|32/reverse $&/eg}\n\ns#\\d+#join\"\",(sort {$a <=> $b} (split//,$&))#eg;\n# print; n();\n$j=0;\nfor $i(1..100){\n\t/1{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?1{$j})/(push @T,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@T\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/2{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?2{$j})/(push @P,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@P\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/3{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?3{$j})/(push @M,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@M\\n\";\n\nprint \"If you want to call a taxi, you should call: \",(join\", \",@T),\".\\n\";\nprint \"If you want to order a pizza, you should call: \",(join\", \",@P),\".\\n\";\nprint \"If you want to go to a cafe with a wonderful girl, you should call: \",(join\", \",@M),\".\\n\";\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=<>;\nchomp @_;\nshift @_;\n\ns/-//g for @_;\n\nfor (@_){\n\tif (/^\\d{6}$/){\n\t\t@n=split//;\n\t\t$f=shift @n;\n\t\t$nT=$nP=0;\n\t\tfor $i(@n){\n\t\t\t$f!=$i and $nT++;\n\t\t\t$i>=$f and $nP++;\n\t\t\t$f=$i;\n\t\t\t}\n\t\t$nT && $nP and $_=3;\n\t\t$nT && !$nP and $_=2;\n\t\t!$nT && $nP and $_=1; \n\t\t}\n\t\n\t}\n\t\n# print \"@_\\n\";\n\nfor (@_){\n\ts/^\\d+ / /;\n\t}\n\n$_=join\"\",@_;\n# print; n();\n\nwhile (/31|21|32/){\ns/31|21|32/reverse $&/eg;\n}\n\n$j=0;\nfor $i(1..100){\n\t/1{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?1{$j})/(push @T,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@T\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/2{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?2{$j})/(push @P,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@P\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/3{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?3{$j})/(push @M,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@M\\n\";\n\nprint \"If you want to call a taxi, you should call: \",(join\", \",@T),\".\\n\";\nprint \"If you want to order a pizza, you should call: \",(join\", \",@P),\".\\n\";\nprint \"If you want to go to a cafe with a wonderful girl, you should call: \",(join\", \",@M),\".\\n\";\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=<>;\nchomp @_;\nshift @_;\n\ns/-//g for @_;\n\nfor (@_){\n\tif (/^\\d{6}$/){\n\t\t@n=split//;\n\t\t$f=shift @n;\n\t\t$nT=$nP=0;\n\t\tfor $i(@n){\n\t\t\t$f!=$i and $nT++;\n\t\t\t$i>=$f and $nP++;\n\t\t\t$f=$i;\n\t\t\t}\n\t\t$nT && $nP and $_=3;\n\t\t$nT && !$nP and $_=2;\n\t\t!$nT && $nP and $_=1; \n\t\t}\n\t\n\t}\n\t\n# print \"@_\\n\";\n\nfor (@_){\n\ts/^\\d+ / /;\n\t}\n\n$_=join\"\",@_;\n# print; n();\n\ns#\\d+#join\"\",(sort (split//,$&))#eg;\n# print; n();\n$j=0;\nfor $i(1..100){\n\t/1{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?1{$j})/(push @T,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@T\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/2{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?2{$j})/(push @P,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@P\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/3{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?3{$j})/(push @M,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@M\\n\";\n\nprint \"If you want to call a taxi, you should call: \",(join\", \",@T),\".\\n\";\nprint \"If you want to order a pizza, you should call: \",(join\", \",@P),\".\\n\";\nprint \"If you want to go to a cafe with a wonderful girl, you should call: \",(join\", \",@M),\".\\n\";\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=<>;\nchomp @_;\nshift @_;\n\ns/-//g for @_;\n\nfor (@_){\n\tif (/^\\d{6}$/){\n\t\t@n=split//;\n\t\t$f=shift @n;\n\t\t$nT=$nP=0;\n\t\tfor $i(@n){\n\t\t\t$f!=$i and $nT++;\n\t\t\t$i>=$f and $nP++;\n\t\t\t$f=$i;\n\t\t\t}\n\t\t$nT && $nP and $_=3;\n\t\t$nT && !$nP and $_=2;\n\t\t!$nT && $nP and $_=1; \n\t\t}\n\t\n\t}\n\t\n# print \"@_\\n\";\n\nfor (@_){\n\ts/^\\d+ / /;\n\t}\n\n$_=join\"\",@_;\n# print; n();\n\nwhile (/31|21|32/){\ns/31/13/g;\ns/21/12/g;\ns/32/23/g;\n}\n\n$j=0;\nfor $i(1..100){\n\t/1{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?1{$j})/(push @T,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@T\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/2{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?2{$j})/(push @P,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@P\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/3{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?3{$j})/(push @M,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@M\\n\";\n\nprint \"If you want to call a taxi, you should call: \",(join\", \",@T),\".\\n\";\nprint \"If you want to order a pizza, you should call: \",(join\", \",@P),\".\\n\";\nprint \"If you want to go to a cafe with a wonderful girl, you should call: \",(join\", \",@M),\".\\n\";\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=<>;\nchomp @_;\nshift @_;\n\ns/-//g for @_;\n\nfor (@_){\n\tif (/^\\d{6}$/){\n\t\t@n=split//;\n\t\t$f=shift @n;\n\t\t$nT=$nP=0;\n\t\tfor $i(@n){\n\t\t\t$f!=$i and $nT++;\n\t\t\t$i>=$f and $nP++;\n\t\t\t$f=$i;\n\t\t\t}\n\t\t$nT && $nP and $_=3;\n\t\t$nT && !$nP and $_=2;\n\t\t!$nT && $nP and $_=1; \n\t\t}\n\t\n\t}\n\t\n# print \"@_\\n\";\n\nfor (@_){\n\ts/^\\d+ / /;\n\t}\n\n$_=join\"\",@_;\n# print; n();\n\ns#\\d+#join\"\",(sort (split//,$&))#eg;\n# print; n();\n$j=0;\nfor $i(1..100){\n\t/1{$i}/ and $j++;\n\t}\ns/([a-z]+)(1{$j})/(push @T,$1) and \"$1\"/ieg;\n# print; n();\n# print \"@T\\n\";\ns/1//g;\n\n$j=0;\nfor $i(1..100){\n\t/2{$i}/ and $j++;\n\t}\ns/([a-z]+)(2{$j})/(push @P,$1) and \"$1\"/ieg;\n# print; n();\n# print \"@P\\n\";\ns/2//g;\n\n$j=0;\nfor $i(1..100){\n\t/3{$i}/ and $j++;\n\t}\ns/([a-z]+)(3{$j})/(push @M,$1) and \"$1\"/ieg;\n# print; n();\n# print \"@M\\n\";\ns/3//g;\n\nprint \"If you want to call a taxi, you should call: \",(join\", \",@T),\".\\n\";\nprint \"If you want to order a pizza, you should call: \",(join\", \",@P),\".\\n\";\nprint \"If you want to go to a cafe with a wonderful girl, you should call: \",(join\", \",@M),\".\\n\";\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=<>;\nchomp @_;\nshift @_;\n\ns/-//g for @_;\n\nfor (@_){\n\tif (/^\\d{6}$/){\n\t\t@n=split//;\n\t\t$f=shift @n;\n\t\t$nT=$nP=0;\n\t\tfor $i(@n){\n\t\t\t$f!=$i and $nT++;\n\t\t\t$i>=$f and $nP++;\n\t\t\t$f=$i;\n\t\t\t}\n\t\t$nT && $nP and $_=3;\n\t\t$nT && !$nP and $_=2;\n\t\t!$nT && $nP and $_=1; \n\t\t}\n\t\n\t}\n\t\n# print \"@_\\n\";\n\nfor (@_){\n\ts/^\\d+ / /;\n\t}\n\n$_=join\"\",@_;\n# print; n();\n\n$j=0;\nfor $i(1..100){\n\t/1{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?1{$j})/(push @T,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@T\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/2{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?2{$j})/(push @P,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@P\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/3{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?3{$j})/(push @M,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@M\\n\";\n\nprint \"If you want to call a taxi, you should call: \",(join\", \",@T),\".\\n\";\nprint \"If you want to order a pizza, you should call: \",(join\", \",@P),\".\\n\";\nprint \"If you want to go to a cafe with a wonderful girl, you should call: \",(join\", \",@M),\".\\n\";\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=<>;\nchomp @_;\nshift @_;\n\ns/-//g for @_;\n\nfor (@_){\n\tif (/^\\d{6}$/){\n\t\t@n=split//;\n\t\t$f=shift @n;\n\t\t$nT=$nP=0;\n\t\tfor $i(@n){\n\t\t\t$f!=$i and $nT++;\n\t\t\t$i>=$f and $nP++;\n\t\t\t$f=$i;\n\t\t\t}\n\t\t$nT && $nP and $_=3;\n\t\t$nT && !$nP and $_=2;\n\t\t!$nT && $nP and $_=1; \n\t\t}\n\t\n\t}\n\t\n# print \"@_\\n\";\n\nfor (@_){\n\ts/^\\d+ / /;\n\t}\n\n$_=join\"\",@_;\n# print; n();\n\ns#\\d+#join\"\",(sort (split//,$&))#eg;\n# print; n();\n$j=0;\nfor $i(1..100){\n\t/1{$i}/ and $j++;\n\t}\ns/([a-z]+)(1{$j})/(push @T,$1) and \"$1\"/ieg;\n# print; n();\n# print \"@T\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/2{$i}/ and $j++;\n\t}\ns/([a-z]+)(2{$j})/(push @P,$1) and \"$1\"/ieg;\n# print; n();\n# print \"@P\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/3{$i}/ and $j++;\n\t}\ns/([a-z]+)(3{$j})/(push @M,$1) and \"$1\"/ieg;\n# print; n();\n# print \"@M\\n\";\n\nprint \"If you want to call a taxi, you should call: \",(join\", \",@T),\".\\n\";\nprint \"If you want to order a pizza, you should call: \",(join\", \",@P),\".\\n\";\nprint \"If you want to go to a cafe with a wonderful girl, you should call: \",(join\", \",@M),\".\\n\";\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=<>;\nchomp @_;\nshift @_;\n\ns/-//g for @_;\n\nfor (@_){\n\tif (/^\\d{6}$/){\n\t\t@n=split//;\n\t\t$f=shift @n;\n\t\t$nT=$nP=0;\n\t\tfor $i(@n){\n\t\t\t$f!=$i and $nT++;\n\t\t\t$i>=$f and $nP++;\n\t\t\t$f=$i;\n\t\t\t}\n\t\t$nT && $nP and $_=3;\n\t\t$nT && !$nP and $_=2;\n\t\t!$nT && $nP and $_=1; \n\t\t}\n\t\n\t}\n\t\n# print \"@_\\n\";\n\nfor (@_){\n\ts/^\\d+ //;\n\t}\n\n$_=join\"\",@_;\n# print; n();\n\n$j=0;\nfor $i(1..100){\n\t/1{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?1{$j})/(push @T,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@T\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/2{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?2{$j})/(push @P,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@P\\n\";\n\n$j=0;\nfor $i(1..100){\n\t/3{$i}/ and $j++;\n\t}\ns/([a-z]+)(\\d*?3{$j})/(push @M,$1) and \"$1\".\"$2\"/ieg;\n# print; n();\n# print \"@M\\n\";\n\nprint \"If you want to call a taxi, you should call: \",(join\", \",@T),\".\\n\";\nprint \"If you want to order a pizza, you should call: \",(join\", \",@P),\".\\n\";\nprint \"If you want to go to a cafe with a wonderful girl, you should call: \",(join\", \",@M),\".\\n\";\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\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)$$$ \u00a0\u2014 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": "for(1..<>) {\r\n chomp(my $in = <>);\r\n my ($a,$b,$c) = split / /, $in;\r\n my $x = $a + $b + $c;\r\n print $x%3 ? 1 : 0;\r\n print \"\\n\";\r\n}"}], "negative_code": [], "src_uid": "5bffe38e3ac9511a30ee02b4ec5cb1d5"} {"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 = \u00ababa\u00bb, then the string \"a??\" is good, and the string \u00ab?bc\u00bb 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": "#!/bin/bash/perl\n$input=<>;\n@input1=split(\" \",$input);\n$refer=$input1[0];\n$input=<>;\n@input1=split(\" \",$input);\n$main=$input1[0];\n\n\nif(length($refer)0){\n\t\t\t--$hash{$mainarr[$i]};\n\t\t\t++$match;\n\t\t}\n\t}\n\t\n\t\n\tif($ques+$match == @mainarr){\n\t\t++$tot;\n\t}\n\t\n\t\n\t#reset hash value\n\t%hash = %outerhash;\n\t\n\tfor($i=1;$i<=length($refer)-@mainarr;++$i){\n\t\t#first reset outer values\n\t\t\n\t\t#remove previous entry of index $i-1\n\t\t$previous=$i-1;\n\t\t\n\t\tif($newsubstr[$previous] eq '?'){ #if its '?' then remove entry\n\t\t\t--$ques;\n\t\t}\n\t\telse{ #then it has to have an entry in hash, remove it\n\t\t\t--$hash{$newsubstr[$previous]};\n\t\t}\n\t\t\n\t\t#add new index in hash or '?' entry for index $i+@mainarr-1\n\n\t\tif($newsubstr[$i+@mainarr-1] eq '?'){\n\t\t\t#add entry for '?'\n\t\t\t++$ques;\n\t\t}\n\t\telse{\n\t\t\t#then add entry in hash\n\t\t\t$temp = $hash{$newsubstr[$i+@mainarr-1]};\n\t\t\tif($temp==NULL){\n\t\t\t\t$hash{$newsubstr[$i+@mainarr-1]}=1;\n\t\t\t}\n\t\t\telse{\n\t\t\t\t++$hash{$newsubstr[$i+@mainarr-1]};\n\t\t\t}\n\t\t}\n\t\t\n\t\t$match=0;\n\t\t#now compare with entries in mainhash\n\t\t\n\t\tif($ques < @mainarr){\n\t\t\tfor($alpha='a';$alpha ne'aa' &&($match+$ques < @mainarr);++$alpha){\n\t\t\t\t$temp=$mainhash{$alpha};\n\t\t\t\t$temp1=$hash{$alpha};\n\t\t\t\tif(($temp!=NULL) && ($temp>0)){\n\t\t\t\t\tif($temp1!=NULL && $temp1>0){\n\t\t\t\t\t\tif($temp == $temp1){\n\t\t\t\t\t\t\t$match=$match+$temp;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telsif($temp < $temp1){\n\t\t\t\t\t\t\t#more residue inside, will always clash with\n\t\t\t\t\t\t\t#some cases, hence exit\n\t\t\t\t\t\t\t$alpha='z';\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse{\n\t\t\t\t\t\t\t$match = $match+$temp1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse{\n\t\t\t\t\t\tif($ques<=0){\n\t\t\t\t\t\t\t#req shall not be met in future\n\t\t\t\t\t\t\t$alpha='z';\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telsif($temp1 != NULL && $temp1 >0){\n\t\t\t\t\t#some residual value is there, that shall\n\t\t\t\t\t#always reduce chances of being selected\n\t\t\t\t\t#hence exit\n\t\t\t\t\t$alpha='z';\n\t\t\t\t}\n\t\t\t\t\n\t\t\t}#O_(1)\n\t\t}\n\t\t\n\t\tif($match+$ques == @mainarr){\n\t\t\t\t++$tot;\n\t\t\t}\n\t}#O_26n\n\t\n\t\n\tprint \"$tot\";\n\n}#else all other cases"}], "negative_code": [{"source_code": "#!/bin/bash/perl\n$input=<>;\n@input1=split(\" \",$input);\n$refer=$input1[0];\n$input=<>;\n@input1=split(\" \",$input);\n$main=$input1[0];\n\n\nif(length($refer)0){\n\t\t\t--$hash{$mainarr[$i]};\n\t\t\t++$match;\n\t\t}\n\t}\n\t\n\t\n\tif($ques+$match == @mainarr){\n\t\t++$tot;\n\t}\n\t\n\t\n\t#reset hash value\n\t%hash = %outerhash;\n\t\n\tfor($i=1;$i<=length($refer)-@mainarr;++$i){\n\t\t#first reset outer values\n\t\t\n\t\t#remove previous entry of index $i-1\n\t\t$previous=$i-1;\n\t\t\n\t\tif($newsubstr[$previous] eq '?'){ #if its '?' then remove entry\n\t\t\t--$ques;\n\t\t}\n\t\telse{ #then it has to have an entry in hash, remove it\n\t\t\t--$hash{$newsubstr[$previous]};\n\t\t}\n\t\t\n\t\t#add new index in hash or '?' entry for index $i+@mainarr-1\n\n\t\tif($newsubstr[$i+@mainarr-1] eq '?'){\n\t\t\t#add entry for '?'\n\t\t\t++$ques;\n\t\t}\n\t\telse{\n\t\t\t#then add entry in hash\n\t\t\t$temp = $hash{$newsubstr[$i+@mainarr-1]};\n\t\t\tif($temp==NULL){\n\t\t\t\t$hash{$newsubstr[$i+@mainarr-1]}=1;\n\t\t\t}\n\t\t\telse{\n\t\t\t\t++$hash{$newsubstr[$i+@mainarr-1]};\n\t\t\t}\n\t\t}\n\t\t\n\t\t$match=0;\n\t\t#now compare with entries in mainhash\n\t\t\n\t\tif($ques < @mainarr){\n\t\t\tfor($alpha='a';$alpha ne'aa';++$alpha){\n\t\t\t\t$temp=$mainhash{$alpha};\n\t\t\t\t$temp1=$hash{$alpha};\n\t\t\t\tif(($temp!=NULL) && ($temp>0)){\n\t\t\t\t\tif($temp1!=NULL && $temp1>0){\n\t\t\t\t\t\tif($temp <= $temp1){\n\t\t\t\t\t\t\t$match=$match+$temp;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse{\n\t\t\t\t\t\t\t$match = $match+$temp1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse{\n\t\t\t\t\t\tif($ques<=0){\n\t\t\t\t\t\t\t#req shall not be met in future\n\t\t\t\t\t\t\t$alpha='z';\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telsif($temp1 != NULL && $temp1 >0){\n\t\t\t\t\t#some residual value is there, that shall\n\t\t\t\t\t#always reduce chances of being selected\n\t\t\t\t\t#hence exit\n\t\t\t\t\t$alpha='z';\n\t\t\t\t}\n\t\t\t\t\n\t\t\t}#O_(1)\n\t\t}\n\t\telse{\n\t\t\tprint \"sufficient ?\\n\";\n\t\t}\n\t\tif($match+$ques == @mainarr){\n\t\t\t\t++$tot;\n\t\t\t}\n\t}#O_26n\n\t\n\t\n\tprint \"$tot\";\n\n}#else all other cases"}], "src_uid": "9ed2a081b65df66629fcf0d4fc0bb02c"} {"nl": {"description": "You are given a rectangular grid of lattice points from (0,\u20090) to (n,\u2009m) 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,\u2009p2,\u2009p3,\u2009p4 consists of the line segments p1\u2009p2,\u2009p2\u2009p3,\u2009p3\u2009p4, 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20091000). It is guaranteed that grid contains at least 4 different points.", "output_spec": "Print 4 lines with two integers per line separated by space \u2014 coordinates of points p1,\u2009p2,\u2009p3,\u2009p4 in order which represent the longest possible polyline. Judge program compares your answer and jury's answer with 10\u2009-\u20096 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": "chomp($_=<>);\n($n,$m)=split/ /;\n\n$n or print \"0 1\\n0 $m\\n0 0\\n0 \".($m-1);\n$m+0 or print \"1 0\\n$n 0\\n0 0\\n\".($n-1).\" 0\";\n\n$n && $m or exit;\n\npush @a, \n\tsqrt(($n-1)**2+$m**2)*2,\n\tsqrt($n**2+($m-1)**2)*2,\n\tsqrt($n**2+$m**2)+$m,\n\tsqrt($n**2+$m**2)+$n;\n\npush @b, \n\t($n-1).\" $m\\n0 0\\n$n $m\\n1 0\",\n\t\"$n \".($m-1).\"\\n0 0\\n$n $m\\n0 1\",\n\t\"$n $m\\n0 0\\n0 $m\\n$n 0\",\n\t\"$n $m\\n0 0\\n$n 0\\n0 $m\";\n\t\n$i++,$max<$_ and ($max=$_,$j=$i) for @a;\n\nprint $b[--$j]"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$m)=split/ /;\n\t\n\t$n or print \"0 1\\n\".\"0 $m\\n\".\"0 0\\n\".\"0 \".($m-1);\n\t$m or print \"1 0\\n\".\"$n 0\\n\".\"0 0\\n\".($n-1).\" 0\";\n\t\n\t$n or next;\n\t$m or next;\n\t\n#\t$n==1 and $m==1 and print \"1 1\\n0 0\\n1 0\\n0 1\";\n#\t$n==1 and $m>1 and print \"$n $m\\n0 0\\n0 $m\\n$n 0\";\n#\t$n>1 and $m==1 and print \"$n $m\\n0 0\\n$n 0\\n0 $m\";\n\t\n\t@a=();\n\tpush @a, sqrt(($n-1)**2+$m**2)*2;\n\tpush @a, sqrt($n**2+($m-1)**2)*2;\n\tpush @a, sqrt($n**2+$m**2)+$m;\n\tpush @a, sqrt($n**2+$m**2)+$n;\n\t\n\t@b=();\n\tpush @b, (($n-1).\" $m\\n\".\"0 0\\n\".\"$n $m\\n\".\"1 0\");\n\tpush @b, (\"$n \".($m-1).\"\\n0 0\\n\".\"$n $m\\n\".\"0 1\");\n\tpush @b, \"$n $m\\n0 0\\n0 $m\\n$n 0\";\n\tpush @b, \"$n $m\\n0 0\\n$n 0\\n0 $m\";\n\t\n\t$max=0;\n\t$max<$_ and $max=$_ for @a;\n\t\n\tfor $i(0..3){\n\t\tif ($max==$a[$i]){\n\t\t\t$j=$i;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\t\n\tprint $b[$j];\n\t\n#\t$n>=$m and $n>1 and $m>1 and print (\"$n \".($m-1).\"\\n0 0\\n\".\"$n $m\\n\".\"0 1\");\n#\t$n<$m and $n>1 and $m>1 and print (($n-1).\" $m\\n\".\"0 0\\n\".\"$n $m\\n\".\"1 0\");\n\n\tprint \"\\n\"\n\t}"}], "negative_code": [{"source_code": "while(<>){\n\tchomp;\n\t($n,$m)=split/ /;\n\t\n\t$n or print \"0 1\\n\".\"0 $m\\n\".\"0 0\\n\".\"0 \".($m-1);\n\t$m or print \"1 0\\n\".\"$n 0\\n\".\"0 0\\n\".($n-1).\" 0\";\n\t$n and $m and print \"$n $m\\n\".\"0 0\\n\".\"$n 0\\n\".\"0 $m\";\n\t\n\tprint \"\\n\"\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$m)=split/ /;\n\t\n\t$n or print \"0 1\\n\".\"0 $m\\n\".\"0 0\\n\".\"0 \".($m-1);\n\t$m or print \"1 0\\n\".\"$n 0\\n\".\"0 0\\n\".($n-1).\" 0\";\n\t$n>=$m and $n and $m and print \"$n $m\\n\".\"0 0\\n\".\"$n \".($m-1).\"\\n0 \".($m==2? 2:1);\n\t$n<$m and $n and $m and print \"$n $m\\n\".\"0 0\\n\".($n-1).\" $m\\n\".($n==2? 2:1).\" 0\";\n\n\tprint \"\\n\"\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$m)=split/ /;\n\t\n\t$n or print \"0 1\\n\".\"0 $m\\n\".\"0 0\\n\".\"0 \".($m-1);\n\t$m or print \"1 0\\n\".\"$n 0\\n\".\"0 0\\n\".($n-1).\" 0\";\n\t$n>=$m and $n and $m and print \"$n $m\\n\".\"0 0\\n\".\"$n 0\\n\".\"0 $m\";\n\t$n<$m and $n and $m and print \"$n $m\\n\".\"0 0\\n\".\"0 $m\\n\".\"$n 0\";\n\n\tprint \"\\n\"\n\t}"}], "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,\u2009b (1\u2009\u2264\u2009a,\u2009b\u2009\u2264\u20091000), 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 \u2014 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": "\nwhile(<>){\n\t\n\t($a,$b)=split/ /;\n\t$t=$a, $a=$b, $b=$t if $a>$b;\n\t\n\t$ok=0;\n\tfor $i(1..$a-1){\n\t\t$ab = sqrt($a**2 - $i**2);\n\t\t$ab =~/\\./ && next;\n\t\t$x= $i; $y = $ab;\n\t\t\n\t\t$v=$x*$b/$a;\n\t\t$z=$y*$b/$a;\n\t\t\n\t\t$v =~/\\./ && next;\n\t\t$ok++ or last;\n\t\t}\n\t\n\t$t=$x, $x=$y, $y=$t, $t=$v, $v=$z, $z=$t if $y==$v;\n\tprint $ok ? (\"YES\\n0 0\\n\",-$x,\" $y\\n$z $v\") : NO\n\t\n\t}\n\t\n"}, {"source_code": "\nwhile(<>){\n\t\n\t($a,$b)=split/ /;\n\tif ($a>$b){$tmp=$a; $a=$b; $b=$tmp}\n\t\n\t$ok=0;\n\tfor $i(1..$a-1){\n\t\t$ab = sqrt($a**2 - $i**2);\n\t\t$ab =~/\\./ and next;\n\t\t$x= $i; $y = $ab;\n\t\t\n\t\t$v=($x*$b)/$a;\n\t\t$z=($y*$b)/$a;\n\t\t\n\t\t$v =~/\\./ and next;\n\t\t$ok++ or last;\n\t\t}\n\t\n\n\t\t\n\tif (!$ok){print \"NO\\n\"; next}\n\telse {\n\t\t\n\t\tif ($y==$v){$tmp=$x; $x=$y; $y=$tmp; $tmp=$v; $v=$z; $z=$tmp}\n\t\t\n\t\tprint \"YES\\n\";\n\t\tprint (\"0 0\\n\");\n\t\tprint (-$x,\" \",$y,\"\\n\");\n\t\tprint ($z,\" \",$v,\"\\n\");\n\t\t\n\t\t}\n\t\n\t}\n\t\n"}, {"source_code": "while(<>){\n\t\n\t($a,$b)=split/ /;\n\tif ($a>$b){$tmp=$a; $a=$b; $b=$tmp}\n\t\n\t$ok=0;\n\tfor $i(1..$a-1){\n\t\t$ab = sqrt($a**2 - $i**2);\n\t\t$ab =~/\\./ and next;\n\t\t$x= $i; $y = $ab;\n\t\t\n\t\t$v=($x*$b)/$a;\n\t\t$z=($y*$b)/$a;\n\t\t\n\t\t$v =~/\\./ and next;\n\t\t$ok++ or last;\n\t\t}\n\t\n\n\t\t\n\tif (!$ok){print \"NO\\n\"; next}\n\telse {\n\t\t\n\t\tif ($y==$v){$tmp=$x; $x=$y; $y=$tmp; $tmp=$v; $v=$z; $z=$tmp}\n\t\t\n\t\tprint \"YES\\n\";\n\t\tprint (\"0 0\\n\");\n\t\tprint (-$x,\" \",$y,\"\\n\");\n\t\tprint ($z,\" \",$v,\"\\n\");\n\t\t\n\t\t}\n\t\n\t}"}], "negative_code": [{"source_code": "\nwhile(<>){\n\t\n\t($a,$b)=split/ /;\n\t\n\t$ok=0;\n\tfor $i(1..$a-1){\n\t\t$ab = sqrt($a**2 - $i**2);\n\t\t$ab =~/\\./ and next;\n\t\t$x= $i; $y = $ab;\n\t\t\n\t\t$v=($x*$b)/$a;\n\t\t$z=($y*$b)/$a;\n\t\t$v =~/\\./ and last;\n\t\t$ok++\n\t\t}\n\t\n\n\t\t\n\tif (!$ok){print \"NO\\n\"; next}\n\telse {\n\t\t\n\t\tif ($x==$v){$tmp=$v; $v=$z; $z=$tmp}\n\t\tif ($y==$z){$tmp=$v; $v=$z; $z=$tmp}\n\t\t\n\t\tprint \"YES\\n\";\n\t\tprint (\"0 0\\n\");\n\t\tprint (-$x,\" \",$y,\"\\n\");\n\t\tprint ($v,\" \",$z,\"\\n\");\n\t\t\n\t\t}\n\t\n\t}\n\t\n"}, {"source_code": "\t($a,$b)=split/ /;\n\t$t=$a, $a=$b, $b=$t if $a>$b;\n\t\n\t$ok=0;\n\tfor $i(1..$a-1){\n\t\t$ab = sqrt($a**2 - $i**2);\n\t\t$ab =~/\\./ && next;\n\t\t$x= $i; $y = $ab;\n\t\t\n\t\t$v=$x*$b/$a;\n\t\t$z=$y*$b/$a;\n\t\t\n\t\t$v =~/\\./ && next;\n\t\t$ok++ or last;\n\t\t}\n\t\n\t$t=$x, $x=$y, $y=$t, $t=$v, $v=$z, $z=$t if $y==$v;\n\tprint $ok ? (\"YES\\n0 0\\n\",-$x,\" $y\\n$z $v\") : NO"}, {"source_code": "\t($a,$b)=split/ /;\n\t$t=$a, $a=$b, $b=$t if $a>$b;\n\t\n\tfor $i(1..$a-1){\n\t\t$ab = sqrt($a**2 - $i**2);\n\t\t$ab =~/\\./ && next;\n\t\t$x= $i; $y = $ab;\n\t\t\n\t\t$v=$x*$b/$a;\n\t\t$z=$y*$b/$a;\n\t\t\n\t\t$v =~/\\./ && next;\n\t\t$ok++ or last\n\t\t}\n\t\n\tif ($y==$v){$t=$x; $x=$y; $y=$t; $t=$v; $v=$z; $z=$t}\n\tprint $ok ? (\"YES\\n0 0\\n\",-$x,\" $y\\n$z $v\") : NO\n\n\t\n"}, {"source_code": "\nwhile(<>){\n\t\n\t($a,$b)=split/ /;\n\t\n\t$ok=0;\n\tfor $i(1..$a-1){\n\t\t$ab = sqrt($a**2 - $i**2);\n\t\t$ab =~/\\./ and next;\n\t\t$x= $i; $y = $ab;\n\t\t\n\t\t$v=($x*$b)/$a;\n\t\t$v =~/\\./ and last;\n\t\t$ok++\n\t\t}\n\t\n\n\t\t\n\tif (!$ok){print \"NO\\n\"; next}\n\telse {\n\t\t\n\t\tprint \"YES\\n\";\n\t\tprint (\"0 0\\n\");\n\t\tprint (-$x,\" \",$y,\"\\n\");\n\t\tprint (($y*$b)/$a,\" \",$v,\"\\n\");\n\t\t\n\t\t}\n\t\n\t}\n\t\n"}, {"source_code": "\nwhile(<>){\n\t\n\t($a,$b)=split/ /;\n\t\n\t$ok=0;\n\tfor $i(1..$a-1){\n\t\t$ab = sqrt($a**2 - $i**2);\n\t\t$ab =~/\\./ and next;\n\t\t$ok++;\n\t\t$x= $i; $y = $ab;\n\t\t}\n\t\n\tfor $i(1..$b-1){\n\t\t$ab = sqrt($b**2 - $i**2);\n\t\t$ab =~/\\./ and next;\n\t\t$ok++;\n\t\t$v= $i; $z = $ab;\n\t\t}\n\t\t\n\tif ($ok<2){print \"NO\\n\"; next}\n\telse {\n\t\t\n\t\tif ($x==$v){$tmp=$v; $v=$z; $z=$tmp}\n\t\tif ($y==$z){$tmp=$v; $v=$z; $z=$tmp}\n\t\t\n\t\tprint \"YES\\n\";\n\t\tprint (\"0 0\\n\");\n\t\tprint (-$x,\" \",$y,\"\\n\");\n\t\tprint ($v,\" \",$z,\"\\n\");\n\t\t\n\t\t}\n\t\n\t}\n\t\n"}, {"source_code": "\nwhile(<>){\n\t\n\t($a,$b)=split/ /;\n\t\n\t$ok=0;\n\tfor $i(1..$a-1){\n\t\t$ab = sqrt($a**2 - $i**2);\n\t\t$ab =~/\\./ and next;\n\t\t$x= $i; $y = $ab;\n\t\t\n\t\t$v=($x*$b)/$a;\n\t\t$z=($y*$b)/$a;\n\t\t$v =~/\\./ and last;\n\t\t$ok++\n\t\t}\n\t\n\n\t\t\n\tif (!$ok){print \"NO\\n\"; next}\n\telse {\n\t\t\n\t\tif ($y==$v){$tmp=$x; $x=$y; $y=$tmp; $tmp=$v; $v=$z; $z=$tmp}\n\t\t\n\t\tprint \"YES\\n\";\n\t\tprint (\"0 0\\n\");\n\t\tprint (-$x,\" \",$y,\"\\n\");\n\t\tprint ($z,\" \",$v,\"\\n\");\n\t\t\n\t\t}\n\t\n\t}\n\t\n"}], "src_uid": "a949ccae523731f601108d4fa919c112"} {"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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100). 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": "<>;\nwhile(<>){\n\t$j=++$i%2;\n\ts/./$j++,$&eq\".\"?($j%2?W:B):$&/ge;\n\tprint\n\t}\n"}, {"source_code": "<>;\nwhile(<>){\n\t$j=++$i%2;\n\ts/./$j++,$&eq\".\"?($j%2?W:B):$&/ge;\n\tprint\n\t}\n"}, {"source_code": "<>;\nwhile(<>){\n\t$j=++$i%2;\n\ts/./$j++,$&eq\".\"?($j%2?W:B):$&/ge;\n\tprint\n\t}"}, {"source_code": "<>;\nwhile(<>){\n\t$j=++$i%2;\n\ts/./$j++,$&eq\".\"?($j%2?W:B):$&/ge;\n\tprint\n\t}\n"}, {"source_code": "<>;\nwhile(<>){\n\t$j=++$i%2;\n\ts/./$j++,$&eq\".\"?($j%2?W:B):$&/ge;\n\tprint\n\t}\n"}, {"source_code": "<>;\nwhile(<>){\n\t$j=++$i%2;\n\ts/./$j++,$&eq\".\"?($j%2?W:B):$&/ge;\n\tprint\n\t}\n"}, {"source_code": "<>;\nwhile(<>){\n\t$j=++$i%2;\n\ts/./$j++,$&eq\".\"?($j%2?W:B):$&/ge;\n\tprint\n\t}\n"}, {"source_code": "<>;\nwhile(<>){\n\t$j=++$i%2;\n\ts/./$j++,$&eq\".\"?($j%2?W:B):$&/ge;\n\tprint\n\t}\n"}, {"source_code": "<>;\nwhile(<>){\n\t$j=++$i%2;\n\ts/./$j++,$&eq\".\"?($j%2?W:B):$&/ge;\n\tprint\n\t}"}, {"source_code": "<>;\nwhile(<>){\n\t$j=++$i%2;\n\ts/./$j++,$&eq\".\"?($j%2?W:B):$&/ge;\n\tprint\n\t}\n"}], "negative_code": [], "src_uid": "dc31adef80f06897ea2f5ef76854bcf1"} {"nl": {"description": "Dwarfs have planted a very interesting plant, which is a triangle directed \"upwards\". This plant has an amusing feature. After one year a triangle plant directed \"upwards\" divides into four triangle plants: three of them will point \"upwards\" and one will point \"downwards\". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process. Help the dwarfs find out how many triangle plants that point \"upwards\" will be in n years.", "input_spec": "The first line contains a single integer n (0\u2009\u2264\u2009n\u2009\u2264\u20091018) \u2014 the number of full years when the plant grew. Please do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use cin, cout streams or the %I64d specifier.", "output_spec": "Print a single integer \u2014 the remainder of dividing the number of plants that will point \"upwards\" in n years by 1000000007 (109\u2009+\u20097).", "sample_inputs": ["1", "2"], "sample_outputs": ["3", "10"], "notes": "NoteThe first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one."}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\nuse bigint; # Hope for the best! Yeah, it's rather slow :-(\n\nmain();\n\nsub main {\n my $years = <>;\n if ($years == 0) {\n print 1;\n goto end;\n }\n my $mod = 1000000007;\n my $h_amount = mpow(2, $years-1, $mod);\n my $last = ($h_amount * 2) % $mod;\n my $result = ((1 + $last) * $h_amount) % $mod;\n print $result;\nend:\n}\n\nsub mpow {\n my ($x, $n, $mod) = @_;\n my $b = 1;\n while ($n > 0) {\n if ($n % 2) {\n $n--;\n $b *= $x;\n $b = $b % $mod;\n } else {\n $n /= 2;\n $x *= $x;\n $x = $x % $mod;\n }\n }\n return $b % $mod;\n}"}], "negative_code": [{"source_code": "($a[0], $a[-1]) = (1,0);\nfor $i (1..<>) {\n $a[$i] = 3 * $a[$i-1] + $a[$i-2];\n}\nprint $a[@a-1];"}, {"source_code": "@a = (0,1);\n$n = <>;\nfor $i (2..$n+1) {\n $a[$i] = 3 * $a[$i-1] + $a[$i-2];\n}\nprint $a[$n];\n"}, {"source_code": "$n = <>;\n($a[0], $a[-1]) = (1,0);\nfor $i (1..$n) {\n $a[$i] = 3 * $a[$i-1] + $a[$i-2];\n}\nprint $a[$n];"}, {"source_code": "$days = <>;\nif ($days == 0) {\n\t$result = 1;\n} else {\n\t$mod = 1000000007;\n\t$n = pow(2, $days-1, $mod);\n\t$result = (1 + $n*2)*$n % $mod;\n}\nprint $result;\n\nsub pow {\n\tmy ($b, $n, $m) = @_;\n\tif ($n == 0) {\n\t\treturn 1;\n\t} else {\n\t\t$b = $b % $m;\n\t\tif ($n % 2) {\n\t\t\treturn ($b * pow ($b, $n-1, $m)) % $m;\n\t\t} else {\n\t\t\treturn (pow ($b, $n/2, $m) ** 2) % $m;\n\t\t}\n\t}\n}\n"}, {"source_code": "$n = <>;\n$s = (1 + 2 ** $n) * (2 ** ($n - 1));\nprint $s % 1000000007;"}, {"source_code": "$n = <>;\n($a[0], $a[-1]) = (1,0);\nfor $i (1..$n) {\n $a[$i] = 3 * $a[$i-1] + $a[$i-2];\n}\nprint $a[$n+1];"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\nuse bigint; # Hope for the best! Yeah, it's rather slow :-(\n\nmain();\n\nsub main {\n my $years = <>;\n if (! $years) {\n print 1;\n goto end;\n }\n my $mod = 1000000007;\n my $h_amount = mpow(2, $years-1, $mod);\n my $last = ($h_amount * 2) % $mod;\n my $result = ((1 + $last) * $h_amount) % $mod;\n print $result;\nend:\n}\n\nsub mpow {\n my ($x, $n, $mod) = @_;\n my $b = 1;\n while ($n > 0) {\n if ($n % 2) {\n $n--;\n $b *= $x;\n $b = $b % $mod;\n } else {\n $n /= 2;\n $x *= $x;\n $x = $x % $mod;\n }\n }\n return $b % $mod;\n}"}, {"source_code": "($a[0], $a[-1]) = (0,1);\nfor $i (1..<>) {\n $a[$i] = 3 * $a[$i-1] + $a[$i-2];\n}\nprint $a[@a-1];"}], "src_uid": "782b819eb0bfc86d6f96f15ac09d5085"} {"nl": {"description": "There are $$$n$$$ trees in a park, numbered from $$$1$$$ to $$$n$$$. The initial height of the $$$i$$$-th tree is $$$h_i$$$.You want to water these trees, so they all grow to the same height.The watering process goes as follows. You start watering trees at day $$$1$$$. During the $$$j$$$-th day you can: Choose a tree and water it. If the day is odd (e.g. $$$1, 3, 5, 7, \\dots$$$), then the height of the tree increases by $$$1$$$. If the day is even (e.g. $$$2, 4, 6, 8, \\dots$$$), then the height of the tree increases by $$$2$$$. Or skip a day without watering any tree. Note that you can't water more than one tree in a day. Your task is to determine the minimum number of days required to water the trees so they grow to the same height.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$$$) \u2014 the number of test cases. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 3 \\cdot 10^5$$$) \u2014 the number of trees. The second line of the test case contains $$$n$$$ integers $$$h_1, h_2, \\ldots, h_n$$$ ($$$1 \\le h_i \\le 10^9$$$), where $$$h_i$$$ is the height of the $$$i$$$-th tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \\cdot 10^5$$$ ($$$\\sum n \\le 3 \\cdot 10^5$$$).", "output_spec": "For each test case, print one integer \u2014 the minimum number of days required to water the trees, so they grow to the same height.", "sample_inputs": ["3\n3\n1 2 4\n5\n4 4 3 5 5\n7\n2 5 4 8 3 7 4"], "sample_outputs": ["4\n3\n16"], "notes": "NoteConsider the first test case of the example. The initial state of the trees is $$$[1, 2, 4]$$$. During the first day, let's water the first tree, so the sequence of heights becomes $$$[2, 2, 4]$$$; during the second day, let's water the second tree, so the sequence of heights becomes $$$[2, 4, 4]$$$; let's skip the third day; during the fourth day, let's water the first tree, so the sequence of heights becomes $$$[4, 4, 4]$$$. Thus, the answer is $$$4$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nmy @h;\r\n\r\nwhile( $testcase -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n @h = map { $_ - 0 } split(/\\s+/,);\r\n my $maxh = &max(@h);\r\n my $r = &min( &calc($maxh,$n) , &calc($maxh+1,$n) );\r\n $r = &min( $r, &calc($maxh+2,$n) );\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub calc {\r\n my $mh = shift;\r\n my $n = shift;\r\n my $l1 = 0; my $l2 = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n my $v = $mh - $h[$i];\r\n $l1 += ($v % 2);\r\n $l2 += ($v / 2);\r\n }\r\n my $days = &max((2*$l1-1),$l2*2);\r\n return $days if $l1 >= $l2;\r\n my $x = ($l2 - $l1)/3;\r\n for(my $j=$x-2;$j<=$x+2;$j++){\r\n next if $j<0;\r\n my $l2_ = $l2 - $j;\r\n my $l1_ = $l1 + 2*$j;\r\n next if $l2_ < 0;\r\n my $minv = &max( $l2_*2 , $l1_*2-1);\r\n $days = &min($days, $minv);\r\n }\r\n return $days;\r\n}\r\n\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nmy @h;\r\n\r\nwhile( $testcase -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n @h = map { $_ - 0 } split(/\\s+/,);\r\n my $maxh = &max(@h);\r\n my $r = &min( &calc($maxh,$n) , &calc($maxh+1,$n) );\r\n $r = &min( $r, &calc($maxh+2,$n) );\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub calc {\r\n my $mh = shift;\r\n my $n = shift;\r\n my $l1 = 0; my $l2 = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n my $v = $mh - $h[$i];\r\n $l1 += ($v % 2);\r\n $l2 += ($v / 2);\r\n }\r\n my $days = &max((2*$l1-1),$l2*2);\r\n if( $l2 - 1 > $l1 ){\r\n my $x = ($l2 - $l1)/3;\r\n for(my $j=$x-2;$j<=$x+2;$j++){\r\n next if $j<0;\r\n my $l2_ = $l2 - $x;\r\n my $l1_ = $l1 + 2*$x;\r\n next if $l2_ < 1;\r\n my $minv = &max( $l2_*2 , $l1_*2-1);\r\n $days = &min($days, $minv);\r\n }\r\n } elsif( $l1 == $l2 ){\r\n $days = &min($days,($l1+$l2));\r\n } elsif( $l1 > $l2 ){\r\n $days = &min($days,(2*$l1-1));\r\n } else {\r\n $days = &min($days,(2*$l2));\r\n }\r\n return $days;\r\n}\r\n\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nmy @h;\r\n\r\nwhile( $testcase -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n @h = map { $_ - 0 } split(/\\s+/,);\r\n my $maxh = &max(@h);\r\n my $r = &min( &calc($maxh,$n) , &calc($maxh+1,$n) );\r\n $r = &min( $r, &calc($maxh+2,$n) );\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub calc {\r\n my $mh = shift;\r\n my $n = shift;\r\n my $l1 = 0; my $l2 = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n my $v = $mh - $h[$i];\r\n $l1 += ($v % 2);\r\n $l2 += ($v / 2);\r\n }\r\n my $days = &max((2*$l1-1),$l2*2);\r\n if( $l2 - 1 > $l1 ){\r\n my $x = ($l2 - $l1)/3;\r\n for(my $j=$x-2;$j<=$x+2;$j++){\r\n next if $j<0;\r\n my $l2_ = $l2 - $x;\r\n my $l1_ = $l1 + 2*$x;\r\n next if $l2 < 1;\r\n my $minv = &max( $l2_*2 , $l1_*2-1);\r\n $days = &min($days, $minv);\r\n }\r\n } elsif( $l1 == $l2 ){\r\n $days = &min($days,($l1+$l2));\r\n } elsif( $l1 > $l2 ){\r\n $days = &min($days,(2*$l1-1));\r\n } else {\r\n $days = &min($days,(2*$l2));\r\n }\r\n return $days;\r\n}\r\n\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nmy @h;\r\n\r\nwhile( $testcase -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n @h = map { $_ - 0 } split(/\\s+/,);\r\n my $maxh = &max(@h);\r\n my $r = &min( &calc($maxh,$n) , &calc($maxh+1,$n) );\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub calc {\r\n my $mh = shift;\r\n my $n = shift;\r\n my $l1 = 0; my $l2 = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n my $v = $mh - $h[$i];\r\n $l1 += ($v % 2);\r\n $l2 += ($v / 2);\r\n }\r\n my $days = &max((2*$l1-1),$l2*2);\r\n if( $l2 - 1 > $l1 ){\r\n my $x = (2*$l2 - 2*$l1 + 1)/6;\r\n for(my $j=$x-1;$j<=$x+1;$j++){\r\n next if $x<0;\r\n my $l2_ = $l2 - $x;\r\n my $l1_ = $l1 + 2*$x;\r\n next if $l2 < 1;\r\n my $minv = &max( $l2_*2 , $l1_*2-1);\r\n $days = &min($dats, $minv);\r\n }\r\n } elsif( $l1 == $l2 ){\r\n $days = &min($days,($l1+$l2));\r\n } elsif( $l1 > $l2 ){\r\n $days = &min($days,(2*$l1-1));\r\n } else {\r\n $days = &min($days,(2*$l2));\r\n }\r\n return $days;\r\n}\r\n\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "src_uid": "1f7fa6c56cb7be9404aa2ebaba89a44c"} {"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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of cards Ilya has. Each of the next n lines contains two non-negative space-separated integers \u2014 ai and bi (0\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u2009104) \u2014 the numbers, written at the top and the bottom of the i-th card correspondingly.", "output_spec": "Print the single number \u2014 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": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$ats=0; $i=1;\n<>;\nwhile(<>){\n\tchomp;\n\t($a,$b)=split/ /;\n\t$b? ($ats+=$a,$i+=$b-1):(push @_,$a);\n\t}\n@_=sort num @_;\nwhile ($i--){$ats+=pop@_}\nprint $ats;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$ats=0; $i=1;\n<>;\nwhile(<>){\n\tchomp;\n\t($a,$b)=split/ /;\n\t$b or push @_,$a;\n\t$b and ($ats+=$a,$i+=$b-1);\n\t}\n@_=sort num @_;\nwhile ($i--){$ats+=pop@_}\nprint $ats;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$ats=0; $i=1;\n<>;\nwhile(<>=~/ /){\n\t$'+0 or push @_,$`;\n\t$'+0 and ($ats+=$`,$i+=$'-1);\n\t}\n@_=sort num @_;\nwhile ($i--){$ats+=pop@_}\nprint $ats;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$ats=0; $i=1;\n<>;\nwhile(<>){\n\tchomp;\n\t($a,$b)=split/ /;\n\t$b? ($ats+=$a,$i+=$b-1):(push @_,$a);\n\t}\n@_=sort {$a <=> $b} @_;\nwhile ($i--){$ats+=pop@_}\nprint $ats;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$ats=0; $i=1;\n<>;\nwhile(<>=~/ /){\n\t$'+0 or push @_,$`;\n\t$'+0 and ($ats+=$`,$i+=$');\n\t}\n@_=sort num @_;\nwhile ($i--){$ats+=pop@_}\nprint $ats;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$ats=0; \n<>;\nwhile(<>=~/ /){\n\t$'+0 or push @_,$`;\n\t$'+0 and ($ats+=$`,$i+=$');\n\t}\n@_=sort num @_;\n$i or $i++;\nwhile ($i--){$ats+=pop@_}\nprint $ats;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n<>;\nwhile(<>=~/ /){\n\t$'+0 or push @_,$`;\n\t$'+0 and ($ats+=$`,$i+=$');\n\t}\n@_=sort num @_;\nwhile ($i--){$ats+=pop@_}\nprint $ats;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "src_uid": "4abdd16670a796be3a0bff63b9798fed"} {"nl": {"description": "Chouti and his classmates are going to the university soon. To say goodbye to each other, the class has planned a big farewell party in which classmates, teachers and parents sang and danced.Chouti remembered that $$$n$$$ persons took part in that party. To make the party funnier, each person wore one hat among $$$n$$$ kinds of weird hats numbered $$$1, 2, \\ldots n$$$. It is possible that several persons wore hats of the same kind. Some kinds of hats can remain unclaimed by anyone.After the party, the $$$i$$$-th person said that there were $$$a_i$$$ persons wearing a hat differing from his own.It has been some days, so Chouti forgot all about others' hats, but he is curious about that. Let $$$b_i$$$ be the number of hat type the $$$i$$$-th person was wearing, Chouti wants you to find any possible $$$b_1, b_2, \\ldots, b_n$$$ that doesn't contradict with any person's statement. Because some persons might have a poor memory, there could be no solution at all.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$), the number of persons in the party. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le n-1$$$), the statements of people.", "output_spec": "If there is no solution, print a single line \"Impossible\". Otherwise, print \"Possible\" and then $$$n$$$ integers $$$b_1, b_2, \\ldots, b_n$$$ ($$$1 \\le b_i \\le n$$$). If there are multiple answers, print any of them.", "sample_inputs": ["3\n0 0 0", "5\n3 3 2 2 2", "4\n0 1 2 3"], "sample_outputs": ["Possible\n1 1 1", "Possible\n1 1 2 2 2", "Impossible"], "notes": "NoteIn the answer to the first example, all hats are the same, so every person will say that there were no persons wearing a hat different from kind $$$1$$$.In the answer to the second example, the first and the second person wore the hat with type $$$1$$$ and all other wore a hat of type $$$2$$$.So the first two persons will say there were three persons with hats differing from their own. Similarly, three last persons will say there were two persons wearing a hat different from their own.In the third example, it can be shown that no solution exists.In the first and the second example, other possible configurations are possible."}, "positive_code": [{"source_code": "<>;\n\n@_ = split ' ', <>;\n\nfor( @_ ){\n\tif( $h{ $_ }[ 0 ] ){\n\t\t$h{ $_ }[ 0 ] --;\n\t\tpush @A, $h{ $_ }[ 1 ];\n\t\t}\n\telse{\n\t\t$h{ $_ } = [ @_ - $_, ++ $i ];\n\t\tredo;\n\t\t}\n\t}\n\n$c{ $_ } ++ for @A;\n\nfor $i ( 0 .. @_ - 1 ){\n\t$F += $_[ $i ] != @_ - $c{ $A[ $i ] };\n\t}\n\nprint $F ? \"Impossible\" : \"Possible\\n@A\""}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\tmy $i = 0;\n\tmy @ans;\n\t\n\tfor( @_ ){\n\t\tif( exists $h{ $_ } ){\n\t\t\tif( $h{ $_ }[ 0 ] ){\n\t\t\t\t$h{ $_ }[ 0 ] --;\n\t\t\t\tpush @ans, $h{ $_ }[ 1 ];\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$h{ $_ } = [ @_ - $_, ++ $i ];\n\t\t\t\tredo;\n\t\t\t\t}\n\t\t\t}\n\t\telse{\n\t\t\t$h{ $_ } = [ @_ - $_, ++ $i ];\n\t\t\tredo;\n\t\t\t}\n\t\t}\n\t\n\tmy %c;\n\t\n\tfor( @ans ){\n\t\t$c{ $_ } ++;\n\t\t}\n\t\n\tmy $fail = 0;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\t$fail ++ if $_[ $i ] != @_ - $c{ $ans[ $i ] };\n\t\t}\n\t\n\tprint $fail ? \"Impossible\" : \"Possible\\n@ans\";\n\t}"}], "negative_code": [], "src_uid": "f49667ef00cd0da6a6fad67a19d92f1e"} {"nl": {"description": "Sereja has got an array, consisting of n integers, a1,\u2009a2,\u2009...,\u2009an. 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\u2009=\u2009xi. Increase each array element by yi. In other words, perform n assignments ai\u2009=\u2009ai\u2009+\u2009yi (1\u2009\u2264\u2009i\u2009\u2264\u2009n). 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009105). The second line contains n space-separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 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\u2009\u2264\u2009ti\u2009\u2264\u20093) that represents the operation type. If ti\u2009=\u20091, then it is followed by two integers vi and xi, (1\u2009\u2264\u2009vi\u2009\u2264\u2009n,\u20091\u2009\u2264\u2009xi\u2009\u2264\u2009109). If ti\u2009=\u20092, then it is followed by integer yi (1\u2009\u2264\u2009yi\u2009\u2264\u2009104). And if ti\u2009=\u20093, then it is followed by integer qi (1\u2009\u2264\u2009qi\u2009\u2264\u2009n).", "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": "#!/usr/bin/perl\n# 11 BUG\n\nwhile ($eil=<>) {\n@_=split/ /,<>;\nwhile(<>){\n ($a,$b,$c)=split/ /;\n if ($a==1){\n $_[$b-1]=$c-$x;\n }\n if ($a==2){\n $x+=$b;\n }\n if ($a==3){\n print $_[$b-1]+$x, \"\\n\";\n } \n \n }\n\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nwhile ($eil=<>) {\n@_=split/ /,<>;\nwhile(<>){\n ($a,$b,$c)=split/ /;\n if ($a==1){\n $_[$b-1]=$c;\n }\n if ($a==2){\n $x+=$b;\n }\n if ($a==3){\n print $_[$b]+$x, \"\\n\";\n } \n \n }\n\n}"}], "src_uid": "48f3ff32a11770f3b168d6e15c0df813"} {"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$$$) \u2014 the description of test cases.", "output_spec": "For each test case output two lines. In the first line output a single integer $$$m$$$ \u2014 the number of chosen integers. In the second line output $$$m$$$ distinct integers from $$$1$$$ to $$$n$$$ \u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$k) = map { $_ - 0 } split(/\\s+/,);\r\n my $v1 = $n - $k;\r\n my $v2 = int($k/2);\r\n #$v2 = 1 if $k==2;\r\n my $v = $v1 + $v2;\r\n print \"$v\\n\";\r\n if( $v==0 ){\r\n print \"\\n\";\r\n } else {\r\n my @a = ();\r\n for(my $i=0;$i<$v2;$i++){\r\n push(@a,$k-1-$i);\r\n }\r\n for(my $i=0;$i<$v1;$i++){\r\n push(@a,$k+1+$i);\r\n }\r\n print ( join(\" \",@a) . \"\\n\" );\r\n }\r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$k) = map { $_ - 0 } split(/\\s+/,);\r\n my $v1 = $n - $k;\r\n my $v2 = int(($k - 1)/2);\r\n $v2 = 1 if $k==2;\r\n my $v = $v1 + $v2;\r\n print \"$v\\n\";\r\n if( $v==0 ){\r\n print \"\\n\";\r\n } else {\r\n my @a = ();\r\n for(my $i=0;$i<$v2;$i++){\r\n push(@a,$k-1-$i);\r\n }\r\n for(my $i=0;$i<$v1;$i++){\r\n push(@a,$k+1+$i);\r\n }\r\n print ( join(\" \",@a) . \"\\n\" );\r\n }\r\n}\r\n\r\nexit(0);\r\n\r\n"}], "src_uid": "d08e39db62215d7817113aaa384e3f59"} {"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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) \u2014 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 \u2014 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": "<>; print 0+(@_=/1|(?<=1) 0.+?(?=1)/g) for <>\n"}, {"source_code": "<>; print 0+(@_=/1|(?<=1) 0.+?(?=1)/g) for <>\n"}, {"source_code": "<>; print 0+(@_=/1|(?<=1) 0.+?(?=1)/g) for <>"}, {"source_code": "<>; print 0+(@_=/1|(?<=1) 0.+?(?=1)/g) for <>\n"}, {"source_code": "<>; print 0+(@_=/1|(?<=1) 0.+?(?=1)/g) for <>\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = split / /, <>;\n($ans, $i) = (0) x 2;\n++$i while ($i<$n && $a[$i]==0);\n$i>=$n and say 0 and exit;\n$ans = 1;\nwhile ($i < $n) {\n\t$j = $i++;\n\t++$i while ($i<$n && $a[$i]==0);\n\t$i>=$n and last;\n\t$i-$j==1 and ++$ans or $ans+=2;\n}\nsay $ans;"}, {"source_code": "chomp($n=<>);\nchomp($_=<>);\n@a = split / /;\nforeach (@a) {\n\tif ($_) {\n\t\t++$ans && !$flag && ++$ans;\n\t\t$flag = 1;\n\t} else {\n\t\t$flag = 0;\n\t}\n}\nprint \"0\\n\" if !$ans;\nprint $ans-1,\"\\n\" if $ans;"}, {"source_code": "<>; print 0+(@_=/1|(?<=1) 0.+?(?=1)/g) for <>\n"}, {"source_code": "<>; print 0+(@_=/1|(?<=1) 0.+?(?=1)/g) for <>\n"}, {"source_code": "#!/usr/bin/perl\n\nmy $n = <>;\n\nmy @mail = split / /, <>;\n\nmy $ans = 0;\nmy $reading = 0;\nfor $i (@mail) {\n\tif (($i == 1) || ($reading)) {\n\t\t$ans++;\n\t}\n\t$reading = $i;\n};\n\nif (($mail[-1] == 0) && ($ans > 0)) {\n\t$ans--;\n};\n\nprint $ans;\n"}, {"source_code": "<>; print 0+(@_=/1|(?<=1) 0.+?(?=1)/g) for <>\n"}, {"source_code": "$_=0*<>.<>;\ns/ //g;\ns/01/11/g;\ns/0//g;\nprint /1/?(-2+length):0"}, {"source_code": "$_=0*<>.<>;\ns/0 ?1/1 1/g && s/1//;\nprint 0+(()=/1/g)"}, {"source_code": "<>; print 0+(@_=/1|(?<=1) 0.+?(?=1)/g) for <>"}, {"source_code": "<>;\n$_=<>;\ns/ //g;\ns/(?<=1)0+(?=1)/1/g;\ns/0//g;\nprint -1+ length"}, {"source_code": "<>;\n$_=<>;\ns/\\s//g;\ns/0+/0/g;\ns/^0|0$//g;\nprint length"}, {"source_code": "<>; print 0+(@_=/1|(?<=1) 0.+?(?=1)/g) for <>\n"}], "negative_code": [{"source_code": "<>;\n$_=<>;\ns/ //g;\ns/0+/0/g;\ns/^0|0$//g;\nprint length"}, {"source_code": "<>;\n$_=<>;\ns/1 0/1 1/g;\ns/[0 ]//g;\nprint -1+ length"}, {"source_code": "<>;\n$_=<>;\ns/1 0/1 1/g && s/1//;\ns/[0 ]//g;\nprint -1+ length"}], "src_uid": "0fbc306d919d7ffc3cb02239cb9c2ab0"} {"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,\u2009000) 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\u2009\u2264\u2009a,\u2009b\u2009\u2264\u200925). 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": "use strict;\n\nour @rule = qw(1 11 111 12 2 21 211 2111 13);\nour @char = qw(IVX XLC CDM M);\n\nsub convert_R {\n\tmy $s = shift;\n\tmy $r;\n\tmy $p = length $s;\n\tif ($p) {\n\t\tmy ($d, $d2) = split //, $s, 2;\n\t\tif ($d) {\n\t \t$r = $rule[$d - 1];\n\t \tmy @c = split //, $char[$p - 1];\n \t\t$r =~ s{ (.) }{ $c[$1 - 1] }gxe;\n\t\t}\n\t\t$r .= convert_R ($d2);\n\t}\n\t$r;\n}\n\nsub convert_10 {\n\tmy ($number, $base) = @_;\n\tmy $r;\n\t$number =~ s/^0+//;\n\t$number = reverse $number;\n\tfor (my $i = 0; $i < length $number; $i++) {\n\t\tmy $digit = substr $number, $i, 1;\n\t\t$digit =~ tr [0-9A-Z] [\\00-\\11\\12-\\43];\n\t\t$r += ord($digit) * $base ** $i;\n\t}\n\t$r;\n}\n\nsub convert_B {\n\tmy ($n, $base) = @_;\n\tmy $r;\n\twhile ($n) {\n\t\tmy $digit = chr $n % $base;\n\t\t$digit =~ tr [\\00-\\11\\12-\\43] [0-9A-Z];\n\t\t$r .= $digit;\n\t\t$n = int $n/$base;\n\t}\n\t$r ||= 0;\n\tscalar reverse $r;\n}\n\n$/ = \"\"; $_ = <>; my ($a, $b, $c) = split;\nmy $n = convert_10($c, $a);\nmy $r = $b eq \"R\"? convert_R($n): convert_B($n, $b);\nprint $r;\n"}], "negative_code": [{"source_code": "use strict;\n\nour @rule = qw(1 11 111 12 2 21 211 2111 13);\nour @char = qw(IVX XLC CDM M);\n\nsub convert_R {\n\tmy $s = shift;\n\tmy $r;\n\tmy $p = length $s;\n\tif ($p) {\n\t\tmy ($d, $d2) = split //, $s, 2;\n\t\tif ($d) {\n\t \t$r = $rule[$d - 1];\n\t \tmy @c = split //, $char[$p - 1];\n \t\t$r =~ s{ (.) }{ $c[$1 - 1] }gxe;\n\t\t}\n\t\t$r .= convert_R ($d2);\n\t}\n\t$r;\n}\n\nsub convert_10 {\n\tmy ($number, $base) = @_;\n\tmy $r;\n\t$number = reverse $number;\n\tfor (my $i = 0; $i < length $number; $i++) {\n\t\tmy $digit = substr $number, $i, 1;\n\t\t$digit =~ tr [0-9A-Z] [\\00-\\11\\12-\\43];\n\t\t$r += ord($digit) * $base ** $i;\n\t}\n\t$r;\n}\n\nsub convert_B {\n\tmy ($n, $base) = @_;\n\tmy $r = 0;\n\twhile ($n) {\n\t\tmy $digit = chr $n % $base;\n\t\t$digit =~ tr [\\00-\\11\\12-\\43] [0-9A-Z];\n\t\t$r .= $digit;\n\t\t$n = int $n/$base;\n\t}\n\tscalar reverse $r;\n}\n\n$/ = \"\"; $_ = <>; my ($a, $b, $c) = split;\nmy $n = convert_10($c, $a);\nmy $r = $b eq \"R\"? convert_R($n): convert_B($n, $b);\nprint $r;\n"}, {"source_code": "use strict;\n\nour @rule = qw(1 11 111 12 2 21 211 2111 13);\nour @char = qw(IVX XLC CDM M);\n\nsub convert_R {\n\tmy $s = shift;\n\tmy $r;\n\tmy $p = length $s;\n\tif ($p) {\n\t\tmy ($d, $d2) = split //, $s, 2;\n\t\tif ($d) {\n\t \t$r = $rule[$d - 1];\n\t \tmy @c = split //, $char[$p - 1];\n \t\t$r =~ s{ (.) }{ $c[$1 - 1] }gxe;\n\t\t}\n\t\t$r .= convert_R ($d2);\n\t}\n\t$r;\n}\n\nsub convert_10 {\n\tmy ($number, $base) = @_;\n\tmy $r;\n\t$number = reverse $number;\n\tfor (my $i = 0; $i < length $number; $i++) {\n\t\tmy $digit = substr $number, $i, 1;\n\t\t$digit =~ tr [0-9A-Z] [\\00-\\11\\12-\\43];\n\t\t$r += ord($digit) * $base ** $i;\n\t}\n\t$r;\n}\n\nsub convert_B {\n\tmy ($n, $base) = @_;\n\tmy $r;\n\twhile ($n) {\n\t\tmy $digit = chr $n % $base;\n\t\t$digit =~ tr [\\00-\\11\\12-\\43] [0-9A-Z];\n\t\t$r .= $digit;\n\t\t$n = int $n/$base;\n\t}\n\tscalar reverse $r;\n}\n\n$/ = \"\"; $_ = <>; my ($a, $b, $c) = split;\nmy $n = convert_10($c, $a);\nmy $r = $b eq \"R\"? convert_R($n): convert_B($n, $b);\nprint $r;\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$$$)\u00a0\u2014 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 \u00a0\u2014 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": "use strict;\n\nuse integer;\n\nmy $try = <>;\n\nwhile($try > 0){\n my $cnt = <>;\n $cnt = $cnt / 2 + $cnt % 2;\n print \"$cnt\\n\";\n $try -= 1;\n}"}], "negative_code": [], "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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = reverse split ' ', <>;\n\t\n\tmy $r = 1e10;\n\tmy $sum = 0;\n\t\n\tfor( @_ ){\n\t\tif( $_ < $r ){\n\t\t\t$sum += $_;\n\t\t\t$r = $_;\n\t\t\t}\n\t\telse{\n\t\t\t$r and $r --;\n\t\t\t$sum += $r;\n\t\t\t}\n\t\t}\n\t\n\tprint $sum;\n\t}"}], "negative_code": [], "src_uid": "5993b5bf231fabd3c2af90124c41f118"} {"nl": {"description": "A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.You are to compute the prices each buyer will pay for t-shirts.", "input_spec": "The first line contains single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the number of t-shirts. The following line contains sequence of integers p1,\u2009p2,\u2009...,\u2009pn (1\u2009\u2264\u2009pi\u2009\u2264\u20091\u2009000\u2009000\u2009000), where pi equals to the price of the i-th t-shirt. The following line contains sequence of integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20093), where ai equals to the front color of the i-th t-shirt. The following line contains sequence of integers b1,\u2009b2,\u2009...,\u2009bn (1\u2009\u2264\u2009bi\u2009\u2264\u20093), where bi equals to the back color of the i-th t-shirt. The next line contains single integer m (1\u2009\u2264\u2009m\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the number of buyers. The following line contains sequence c1,\u2009c2,\u2009...,\u2009cm (1\u2009\u2264\u2009cj\u2009\u2264\u20093), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served. ", "output_spec": "Print to the first line m integers \u2014 the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.", "sample_inputs": ["5\n300 200 400 500 911\n1 2 1 2 3\n2 1 3 2 1\n6\n2 3 1 2 1 1", "2\n1000000000 1\n1 1\n1 2\n2\n2 1"], "sample_outputs": ["200 400 300 500 911 -1", "1 1000000000"], "notes": null}, "positive_code": [{"source_code": "while(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy( %h, %g, %i, $i, @A, $A );\n\t\n\tfor ( 0 .. 1 ){\n\t\t$i = -1;\n\t\tmap { $h{ $_ }{ $_[ ++ $i ] } ++ } split ' ', <>;\n\t\t}\n\t\n\t<>;\n\t\n\tfor my $C ( 1 .. 3 ){\n\t\t@{ $g{ $C } } = sort { $a <=> $b } keys %{ $h{ $C } };\n\t\t\n\t\t$i{ $C }{ $_ } = 0 for 1 .. 3;\n\t\t}\n\t\n\tfor my $C ( split ' ', <> ){\n\t\t\n\t\t$i = \\$i{ $C }{ $C };\n\t\t\n\t\twhile( \n\t\t\t$g{ $C }[ $$i ] =~ / / ){\n\t\t\t$$i ++;\n\t\t\t}\n\t\t\n\t\tpush @A, do { \n\t\t\tif( $A = $g{ $C }[ $$i ] ){\n\t\t\t\t$g{ $C }[ $$i ++ ] .= ' ';\n\t\t\t\t\n\t\t\t\tfor ( 1 .. 3 ){\n\t\t\t\t\tnext if /$C/;\n\t\t\t\t\t\n\t\t\t\t\t$i = \\$i{ $C }{ $_ };\n\t\t\t\t\t\n\t\t\t\t\twhile( $g{ $_ }[ $$i ] and \n\t\t\t\t\t\t$g{ $_ }[ $$i ] < $A ){\n\t\t\t\t\t\t$$i ++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tif( \n\t\t\t\t\t\t$g{ $_ }[ $$i ] == $A ){\n\t\t\t\t\t\t$g{ $_ }[ $$i ++ ] .= ' ';\n\t\t\t\t\t\tlast;\n\t\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t\n\t\t\t\t$A;\n\t\t\t\t}\n\t\t\telse {\n\t\t\t\t-1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint \"@A\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\tmy $i;\n\t\n\tfor ( 0 .. 1 ){\n\t\t$i = -1;\n\t\tmap { $i ++; $h{ $_ }{ $_[ $i ] } ++ } split ' ', <>;\n\t\t}\n\t\n\t<>;\n\t\n\tmy %g;\n\tmy %i;\n\t\n\tfor my $color ( 1 .. 3 ){\n\t\t@{ $g{ $color } } = sort { $a <=> $b } keys %{ $h{ $color } };\n\t\t$debug and print \"@{ $g{ $color } }\";\n\t\tfor my $clr ( 1 .. 3 ){\n\t\t\t$i{ $color }{ $clr } = 0;\n\t\t\t}\n\t\t}\n\t\n\tmy @ans;\n\tmy $ans;\n\t\n\tfor my $color ( split ' ', <> ){\n\t\t\n\t\twhile( $g{ $color }->[ $i{ $color }{ $color } ] and \n\t\t\t$g{ $color }->[ $i{ $color }{ $color } ] =~ / / ){\n\t\t\t$i{ $color }{ $color } ++;\n\t\t\t}\n\t\t\n\t\tif( $g{ $color }->[ $i{ $color }{ $color } ] ){\n\t\t\tpush @ans, $ans = $g{ $color }->[ $i{ $color }{ $color } ];\n\t\t\t$g{ $color }->[ $i{ $color }{ $color } ] .= ' ';\n\t\t\t$i{ $color }{ $color } ++;\n\t\t\t\n\t\t\tfor my $clr ( 1 .. 3 ){\n\t\t\t\tnext if $clr == $color;\n\t\t\t\t\n\t\t\t\twhile( $g{ $clr }->[ $i{ $color }{ $clr } ] and \n\t\t\t\t\t$g{ $clr }->[ $i{ $color }{ $clr } ] < $ans ){\n\t\t\t\t\t$i{ $color }{ $clr } ++;\n\t\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif( $g{ $clr }->[ $i{ $color }{ $clr } ] and \n\t\t\t\t\t$g{ $clr }->[ $i{ $color }{ $clr } ] == $ans ){\n\t\t\t\t\t$g{ $clr }->[ $i{ $color }{ $clr } ] .= ' ';\n\t\t\t\t\t$i{ $color }{ $clr } ++;\n\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\telse {\n\t\t\tpush @ans, -1;\n\t\t\t}\n\t\t\n\t\t}\n\t\n\tprint \"@ans\";\n\t}"}], "negative_code": [{"source_code": "while(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy( %h, %g, %i, $i, @A, $A );\n\t\n\tfor ( 0 .. 1 ){\n\t\t$i = -1;\n\t\tmap { $h{ $_ }{ $_[ $i ++ ] } ++ } split ' ', <>;\n\t\t}\n\t\n\t<>;\n\t\n\tfor my $C ( 1 .. 3 ){\n\t\t@{ $g{ $C } } = sort { $a <=> $b } keys %{ $h{ $C } };\n\t\t\n\t\t$i{ $C }{ $_ } = 0 for 1 .. 3;\n\t\t}\n\t\n\tfor my $C ( split ' ', <> ){\n\t\t\n\t\t$i = \\$i{ $C }{ $C };\n\t\t\n\t\twhile( \n\t\t\t$g{ $C }[ $$i ] =~ / / ){\n\t\t\t$$i ++;\n\t\t\t}\n\t\t\n\t\tpush @A, do { \n\t\t\tif( $A = $g{ $C }[ $$i ] ){\n\t\t\t\t$g{ $C }[ $$i ++ ] .= ' ';\n\t\t\t\t\n\t\t\t\tfor ( 1 .. 3 ){\n\t\t\t\t\tnext if /$C/;\n\t\t\t\t\t\n\t\t\t\t\t$i = \\$i{ $C }{ $_ };\n\t\t\t\t\t\n\t\t\t\t\twhile( $g{ $_ }[ $$i ] and \n\t\t\t\t\t\t$g{ $_ }[ $$i ] < $A ){\n\t\t\t\t\t\t$$i ++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tif( \n\t\t\t\t\t\t$g{ $_ }[ $$i ] == $A ){\n\t\t\t\t\t\t$g{ $_ }[ $$i ++ ] .= ' ';\n\t\t\t\t\t\tlast;\n\t\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t\n\t\t\t\t$A;\n\t\t\t\t}\n\t\t\telse {\n\t\t\t\t-1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint \"@A\";\n\t}"}], "src_uid": "d5ead5b6be04cd9389a70e9e420039a6"} {"nl": {"description": "How to make a cake you'll never eat.Ingredients. 2 carrots 0 calories 100 g chocolate spread 1 pack of flour 1 egg Method. Put calories into the mixing bowl. Take carrots from refrigerator. Chop carrots. Take chocolate spread from refrigerator. Put chocolate spread into the mixing bowl. Combine pack of flour into the mixing bowl. Fold chocolate spread into the mixing bowl. Add chocolate spread into the mixing bowl. Put pack of flour into the mixing bowl. Add egg into the mixing bowl. Fold pack of flour into the mixing bowl. Chop carrots until choped. Pour contents of the mixing bowl into the baking dish. Serves 1.", "input_spec": "The only line of input contains a sequence of integers a0,\u2009a1,\u2009... (1\u2009\u2264\u2009a0\u2009\u2264\u2009100, 0\u2009\u2264\u2009ai\u2009\u2264\u20091000 for i\u2009\u2265\u20091).", "output_spec": "Output a single integer.", "sample_inputs": ["4 1 2 3 4"], "sample_outputs": ["30"], "notes": null}, "positive_code": [{"source_code": "chomp($_ = <>);\nmy ($n, @a) = split;\nmy $res = $n = 0;\nmap {$res += $_ * ++$n} @a;\nprint \"$res\\n\";\n"}], "negative_code": [], "src_uid": "f9f25190916bf4294f7458140b264cb5"} {"nl": {"description": "You are given a matrix $$$a$$$ consisting of positive integers. It has $$$n$$$ rows and $$$m$$$ columns.Construct a matrix $$$b$$$ consisting of positive integers. It should have the same size as $$$a$$$, and the following conditions should be met: $$$1 \\le b_{i,j} \\le 10^6$$$; $$$b_{i,j}$$$ is a multiple of $$$a_{i,j}$$$; the absolute value of the difference between numbers in any adjacent pair of cells (two cells that share the same side) in $$$b$$$ is equal to $$$k^4$$$ for some integer $$$k \\ge 1$$$ ($$$k$$$ is not necessarily the same for all pairs, it is own for each pair). We can show that the answer always exists.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \\le n,m \\le 500$$$). Each of the following $$$n$$$ lines contains $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line is $$$a_{i,j}$$$ ($$$1 \\le a_{i,j} \\le 16$$$).", "output_spec": "The output should contain $$$n$$$ lines each containing $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line should be $$$b_{i,j}$$$.", "sample_inputs": ["2 2\n1 2\n2 3", "2 3\n16 16 16\n16 16 16", "2 2\n3 11\n12 8"], "sample_outputs": ["1 2\n2 3", "16 32 48\n32 48 64", "327 583\n408 664"], "notes": "NoteIn the first example, the matrix $$$a$$$ can be used as the matrix $$$b$$$, because the absolute value of the difference between numbers in any adjacent pair of cells is $$$1 = 1^4$$$.In the third example: $$$327$$$ is a multiple of $$$3$$$, $$$583$$$ is a multiple of $$$11$$$, $$$408$$$ is a multiple of $$$12$$$, $$$664$$$ is a multiple of $$$8$$$; $$$|408 - 327| = 3^4$$$, $$$|583 - 327| = 4^4$$$, $$$|664 - 408| = 4^4$$$, $$$|664 - 583| = 3^4$$$. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $M = 720720;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map ~~<>, 1 .. $n;\n\tchomp @_;\n\t\n\tmy $i = 1;\n\t\n\tfor( @_ ){\n\t\tif( $i ){\n\t\t\ts/(\\d+)(?: [ ](\\d+) | $ )/\n\t\t\t\tjoin ' ', $M, grep defined, \n\t\t\t\t\tdefined $2 ? $M + $2 ** 4 : undef;\n\t\t\t\t/gex;\n\t\t\t}\n\t\telse{\n\t\t\ts/(\\d+)(?: [ ](\\d+) | $ )/\n\t\t\t\tjoin ' ', $M + $1 ** 4, grep defined, \n\t\t\t\t\tdefined $2 ? $M : undef;\n\t\t\t\t/gex;\n\t\t\t}\n\t\t\n\t\t$i = 1 - $i;\n\t\t}\n\t\n\tprint for @_;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t<> for 1 .. $n;\n\t\n\t$_ = join ' ', ( \"3003\" ) x ( 1000 + 2 );\n\t\n\ts/3003 300\\K3/4/g;\n\t\n\t@_ = ();\n\t\n\tfor my $i ( 1 .. $n ){\n\t\tpush @_, $_;\n\t\t\n\t\ts/300. //;\n\t\t}\n\t\n\t$_ = join ' ', ( split )[ 0 .. $m - 1 ] for @_;\n\t\n\tprint for @_;\n\t}"}], "src_uid": "5f0b8e6175113142be15ac960e4e9c4c"} {"nl": {"description": "Galya is playing one-dimensional Sea Battle on a 1\u2009\u00d7\u2009n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other.Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called \"hit\") or not (this case is called \"miss\").Galya has already made k shots, all of them were misses.Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship.It is guaranteed that there is at least one valid ships placement.", "input_spec": "The first line contains four positive integers n, a, b, k (1\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105, 1\u2009\u2264\u2009a,\u2009b\u2009\u2264\u2009n, 0\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009-\u20091)\u00a0\u2014 the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. ", "output_spec": "In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them.", "sample_inputs": ["5 1 2 1\n00100", "13 3 2 3\n1000000010001"], "sample_outputs": ["2\n4 2", "2\n7 11"], "notes": "NoteThere is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the \"1\" character). So, it is necessary to make two shots: one at the left part, and one at the right part."}, "positive_code": [{"source_code": "#!perl\nuse strict;\nuse warnings;\nuse v5.18;\nwhile (<>) {\n\tmy ($n, $a, $b, $k) = split;\n\tmy $s = <>; chomp $s;\n\t$s = '1' . $s . '1';\n\tmy @str = split '', $s;\n\tmy @positions;\n\tmy $q;\n\tfor ( my $i = 0; $i < $#str; $i = $q ) {\n\t\tmy $j = $i;\n\t\twhile ($j < $#str && 1 == $str[$j]) { $j++; }\n\t\t$q = $j;\n\t\twhile ($q < $#str && 0 == $str[$q]) { $q++; }\n\t\tfor (my $pos = $j - 1 + $b; $pos < $q; $pos += $b) {\n\t\t\tif ($a == 1) { push @positions, $pos }\n\t\t\telse { $a--; }\n\t\t}\n\t}\n\tsay scalar @positions;\n\tsay join ' ', @positions;\n}\n"}, {"source_code": "#!perl\nuse v5.20;\nuse strict;\nuse warnings;\nuse Class::Struct;\nstruct (\n Interval => [\n length => '$',\n cntShip => '$',\n endPos => '$'\n ]\n);\nwhile (<>) {\n my ($n, $aa, $bb, $k) = split ' ';\n my ($s, @str);\n chomp($s = <>);\n @str = split '', $s;\n my ($cnt, $i, @room);\n $cnt = 0;\n for $i ( 0..$#str ) {\n if (!$str[$i]) {\n ++$cnt;\n }\n else {\n if ($cnt) {\n push @room, Interval->new(\n length => $cnt,\n cntShip => int($cnt/$bb),\n endPos => $i-1\n );\n }\n $cnt = 0;\n }\n }\n if ($cnt) {\n push @room, Interval->new(\n length => $cnt,\n cntShip => int($cnt/$bb),\n endPos => $#str\n );\n }\n @room = sort { $a->length <=> $b->length } @room;\n\n #for $i ( 0..$#room ) {\n # say '(', $room[$i]->length,\n # ' ', $room[$i]->cntShip,\n # ' ', $room[$i]->endPos, ')';\n #}\n\n my ($result, @postSum, $sum);\n $sum = 0;\n for ($i = $#room; $i >= 0; --$i) {\n $sum += $room[$i]->cntShip;\n $postSum[$i] = $sum;\n }\n my (@positions);\n for $i ( 0..$#room ) {\n if ($i != $#room && $postSum[$i+1] >= $aa) {\n #// remaining is enough.\n my $start = $room[$i]->endPos - ($room[$i]->length - 1);\n my $pos = $room[$i]->endPos;\n while ($pos >= $start + $bb - 1) {\n #say \"start= \", $start, \" pos= \", $pos;\n $pos -= ($bb - 1);\n push @positions, $pos+1;\n $pos--;\n }\n }\n elsif ($i != $#room && $postSum[$i+1] < $aa) {\n #// remaining is not enough, current interval is the last.\n my $remaining = $aa - $postSum[$i+1];\n my $cnt = $room[$i]->cntShip - $remaining + 1;\n my $pos = $room[$i]->endPos;\n while ($cnt--) {\n $pos -= ($bb - 1);\n push @positions, $pos+1;\n $pos--;\n }\n last;\n }\n else {\n my $cnt = $room[$i]->cntShip - $aa + 1;\n my $pos = $room[$i]->endPos;\n while ($cnt--) {\n $pos -= ($bb - 1);\n push @positions, $pos+1;\n $pos--;\n }\n }\n }\n say scalar @positions;\n say join ' ', @positions;\n}\n"}], "negative_code": [{"source_code": "#!perl\nuse v5.20;\nuse strict;\nuse warnings;\nuse Class::Struct;\nstruct (\n Interval => [\n length => '$',\n cntShip => '$',\n endPos => '$'\n ]\n);\nwhile (<>) {\n my ($n, $aa, $bb, $k) = split ' ';\n my ($s, @str);\n chomp($s = <>);\n @str = split '', $s;\n my ($cnt, $i, @room);\n $cnt = 0;\n for $i ( 0..$#str ) {\n if (!$str[$i]) {\n ++$cnt;\n }\n else {\n if ($cnt) {\n push @room, Interval->new(\n length => $cnt,\n cntShip => int($cnt/$bb),\n endPos => $i-1\n );\n }\n $cnt = 0;\n }\n }\n if ($cnt) {\n push @room, Interval->new(\n length => $cnt,\n cntShip => int($cnt/$bb),\n endPos => $#str\n );\n }\n @room = sort { $a->length <=> $b->length } @room;\n\n #for $i ( 0..$#room ) {\n # say '(', $room[$i]->length,\n # ' ', $room[$i]->cntShip,\n # ' ', $room[$i]->endPos, ')';\n #}\n\n my ($result, @postSum, $sum);\n $sum = 0;\n for ($i = $#room; $i >= 0; --$i) {\n $sum += $room[$i]->cntShip;\n $postSum[$i] = $sum;\n }\n my (@positions);\n for $i ( 0..$#room ) {\n if ($i != $#room && $postSum[$i+1] >= $aa) {\n #// remaining is enough.\n my $start = $room[$i]->endPos - ($room[$i]->length - 1);\n my $pos = $room[$i]->endPos;\n while ($pos >= $start + $bb - 1) {\n #say \"start= \", $start, \" pos= \", $pos;\n $pos -= ($bb - 1);\n push @positions, $pos+1;\n $pos--;\n }\n }\n elsif ($i != $#room && $postSum[$i+1] < $aa) {\n #// remaining is not enough, current interval is the last.\n my $remaining = $aa - $postSum[$i+1];\n my $cnt = $room[$i]->cntShip - $remaining + 1;\n my $pos = $room[$i]->endPos;\n while ($cnt--) {\n $pos -= ($bb - 1);\n push @positions, $pos+1;\n }\n last;\n }\n else {\n my $cnt = $room[$i]->cntShip - $aa + 1;\n my $pos = $room[$i]->endPos;\n while ($cnt--) {\n $pos -= ($bb - 1);\n push @positions, $pos+1;\n }\n }\n }\n say scalar @positions;\n say join ' ', @positions;\n}\n"}], "src_uid": "d50bb59298e109b4ac5f808d24fef5a1"} {"nl": {"description": "You are given a n\u2009\u00d7\u2009m field consisting only of periods ('.') and asterisks ('*'). Your task is to count all right triangles with two sides parallel to the square sides, whose vertices are in the centers of '*'-cells. A right triangle is a triangle in which one angle is a right angle (that is, a 90 degree angle).", "input_spec": "The first line contains two positive integer numbers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20091000). The following n lines consist of m characters each, describing the field. Only '.' and '*' are allowed.", "output_spec": "Output a single number \u2014 total number of square triangles in the field. 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 2\n**\n*.", "3 4\n*..*\n.**.\n*.**"], "sample_outputs": ["1", "9"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\nuse Data::Dumper;\n\nmy $N = <>;\nchomp $N;\nmy ($n,$m) = split /\\s+/, $N;\nmy @a = ();\nfor my $r (0 .. $n-1) {\n my $s = <>;\n chomp $s;\n $a[$r] = [map{($_ eq '*')?1:0}split(//,$s)];\n}\n#print\"IN = ($n,$m)\\n\".Dumper(@a);\nmy @nrow = ();\nfor my $i (0 .. $n-1) {\n $nrow[$i] = 0;\n for my $j (0 .. $m-1) {\n $nrow[$i] += $a[$i][$j];\n }\n if ($nrow[$i] > 0) {\n $nrow[$i] -= 1;\n }\n}\n\nmy $sum = 0;\nfor my $j (0 .. $m-1) {\n my $s = 0;\n my $s2 = 0;\n for my $i (0 .. $n-1) {\n next unless $a[$i][$j];\n $s2 += $nrow[$i];\n $s += 1;\n }\n# print \"j=$j $s\\n\";\n# next;\n next if $s < 2;\n $s -= 1;\n $sum += $s * $s2;\n}\n#print\"answer:\\n\";\nprint\"$sum\\n\";\nexit;\n\n"}], "negative_code": [], "src_uid": "d9bbd6ca6cfd4b4cdf6017c1b86abd03"} {"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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse bigint;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $p, @A ) = split;\n\t\n\tmy @cand;\n\t\n\tfor( @A ){\n\t\tmy $mod = $p % $_;\n\t\t$mod == 0 and push @cand, 0;\n\t\tpush @cand, $_ - $mod;\n\t\t}\n\t\n\tprint +( sort { $a <=> $b } @cand )[ 0 ];\n\t}"}], "negative_code": [], "src_uid": "293f9b996eee9f76d4bfdeda85685baa"} {"nl": {"description": "Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.", "input_spec": "The first line contains two integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20091000)\u00a0\u2014 the number of rows and the number of columns in the grid. Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur: \".\" \u2014 an empty cell; \"*\" \u2014 a cell with road works; \"S\" \u2014 the cell where Igor's home is located; \"T\" \u2014 the cell where Igor's office is located. It is guaranteed that \"S\" and \"T\" appear exactly once each.", "output_spec": "In the only line print \"YES\" if there is a path between Igor's home and Igor's office with no more than two turns, and \"NO\" otherwise.", "sample_inputs": ["5 5\n..S..\n****.\nT....\n****.\n.....", "5 5\nS....\n****.\n.....\n.****\n..T.."], "sample_outputs": ["YES", "NO"], "notes": "NoteThe first sample is shown on the following picture: In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture: "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m, $ok ) = ( split, 0 );\n\t\n\tchomp( @_ = map ~~<>, 1 .. $n );\n\t\n\tpaint( \\@_ );\n\t\n\trotate( \\@_ );\n\n\tpaint( \\@_ );\n\t\n\t$ok ||= /T\\.*S/i || /S\\.*T/i for @_;\n\t\n\trotate( \\@_ );\n\n\t$ok ||= /T\\.*S/i || /S\\.*T/i for @_;\n\t\n\tprint $ok ? \"YES\" : \"NO\";\n\t}\n\nsub paint {\n\t\n\tfor( @{ shift() } ){\n\t\t/S/ && s/\\.*(?=S)|(?<=S)\\.*/ 's' x length $& /eg,\n\t\t/T/ && s/\\.*(?=T)|(?<=T)\\.*/ 't' x length $& /eg,\n\t\t}\n\t\n\t}\n\t\nsub rotate {\n\t$_ = shift;\n\tmy @tmp;\n\t\n\tfor my $i ( 1 .. length $_->[0] ){\n\t\t$tmp[ $i - 1 ] .= chop for @$_;\n\t\t}\n\t\n\t@$_ = @tmp;\n\t}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m, $ok ) = ( split, 0 );\n\t\n\tchomp( @_ = map ~~<>, 1 .. $n );\n\t\n\tpaint( \\@_ );\n\t\n\trotate( \\@_ );\n\n\tpaint( \\@_ );\n\t\n\t$ok ||= /T\\.*S/i || /S\\.*T/i for @_;\n\t\n\trotate( \\@_ );\n\n\t$ok ||= /T\\.*S/i || /S\\.*T/i for @_;\n\t\n\tprint $ok ? \"YES\" : \"NO\";\n\t}\n\nsub paint {\n\t\n\tfor( @{ shift() } ){\n\t\t/S/ && s/\\.*(?=S)/ 's' x length $& /e,\n\t\t/S/ && s/S\\K\\.*/ 's' x length $& /e,\n\t\t/T/ && s/\\.*(?=T)/ 't' x length $& /e,\n\t\t/T/ && s/T\\K\\.*/ 't' x length $& /e,\n\t\t}\n\t\n\t}\n\t\nsub rotate {\n\t$_ = shift;\n\tmy @tmp;\n\t\n\tfor my $i ( 1 .. length $_->[0] ){\n\t\t$tmp[ $i - 1 ] .= chop for @$_;\n\t\t}\n\t\n\t@$_ = @tmp;\n\t}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\tmy $ok = 0;\n\t\n\t@_ = map ~~<>, 1 .. $n;\n\tchomp @_;\n\t\n\tfor( @_ ){\n\t\t/S/ && s/\\.*(?=S)/ 's' x length $& /e,\n\t\t/S/ && s/S\\K\\.*/ 's' x length $& /e,\n\t\t/T/ && s/\\.*(?=T)/ 't' x length $& /e,\n\t\t/T/ && s/T\\K\\.*/ 't' x length $& /e,\n\t}\n\t\n\t@_ = rot( $n, $m, @_ );\n\n\tfor( @_ ){\n\t\t/S/ && s/\\.*(?=S)/ 's' x length $& /e,\n\t\t/S/ && s/S\\K\\.*/ 's' x length $& /e,\n\t\t/T/ && s/\\.*(?=T)/ 't' x length $& /e,\n\t\t/T/ && s/T\\K\\.*/ 't' x length $& /e,\n\t}\n\t\n\tfor( @_ ){\n\t\t$ok += /T\\.*S/i + /S\\.*T/i;\n\t\t}\n\t\n\t@_ = rot( $n, $m, @_ );\n\n\tfor( @_ ){\n\t\t$ok += /T\\.*S/i + /S\\.*T/i;\n\t\t}\n\t\n\tprint $ok ? \"YES\" : \"NO\";\n\t}\n\nsub rot {\n\tmy( $n, $m, @A ) = ( shift, shift );\n\t\n\tfor my $i ( 0 .. $m - 1 ){\n\t\t$A[ $i ] .= chop for @_;\n\t\t}\n\t\n\t@A;\n\t}"}], "negative_code": [], "src_uid": "16a1c5dbe8549313bae6bca630047502"} {"nl": {"description": "You're given a list of n strings a1,\u2009a2,\u2009...,\u2009an. 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 \u2014 the number of strings (1\u2009\u2264\u2009n\u2009\u2264\u20095\u00b7104). Each of the next n lines contains one string ai (1\u2009\u2264\u2009|ai|\u2009\u2264\u200950) consisting of only lowercase English letters. The sum of string lengths will not exceed 5\u00b7104.", "output_spec": "Print the only string a \u2014 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": "<>;\n\nprint sort { $a . $b cmp $b . $a } map s/\\n//r, <>\n"}, {"source_code": "<>;\n\nprint sort {\n\t\t($A, $B) = ($a, $b);\n\t\t$max = length $A > length $B ? $A : $B;\n\t\tmap { $_ x= 50; substr $_, 0, $max } $A, $B;\n\t\t$A cmp $B\n\t} map s/\\n//r, <>;"}, {"source_code": "<>;\n\nprint sort { $a . $b cmp $b . $a } map s/\\n//r, <>"}], "negative_code": [{"source_code": "<>;\n\nprint join '+', sort {\n\t\t($A, $B) = ($a, $b);\n\t\t$max = length $A > length $B ? $A : $B;\n\t\tmap { $_ x= 50; substr $_, 0, $max } $A, $B;\n\t\t$A cmp $B\n\t} map s/\\n//r, <>;"}, {"source_code": "<>;\n\n%h = map { chomp; $_ => 1 } @_ = <>;\n\nfor (sort map $_ .= (chop) x (52 - length), @_){\n\tfor $i (reverse 1 .. 50){\n\t\t$s = substr $_, 0, $i;\n\t\t$h{ $s } and do { $_ = $s ; delete $h{ $s }; last }\n\t\t}\n\tprint\n\t}"}, {"source_code": "<>;\n\nprint map y/~//dr, sort map s/\\n//r . '~', <>;"}, {"source_code": "<>;\n\n%h = map { chomp; $_ => 1 } @_ = <>;\n\nfor (sort map { $_ x= 50; $_ = substr $_, 0, 50 } @_){\n\tfor $i (reverse 1 .. 50){\n\t\t$s = substr $_, 0, $i;\n\t\t$h{ $s } and do { $_ = $s ; delete $h{ $s }; last }\n\t\t}\n\tprint\n\t}"}], "src_uid": "930365b084022708eb871f3ca2f269e4"} {"nl": {"description": "There are some websites that are accessible through several different addresses. For example, for a long time Codeforces was accessible with two hostnames codeforces.com and codeforces.ru.You are given a list of page addresses being queried. For simplicity we consider all addresses to have the form http://<hostname>[/<path>], where: <hostname>\u00a0\u2014 server name (consists of words and maybe some dots separating them), /<path>\u00a0\u2014 optional part, where <path> consists of words separated by slashes. We consider two <hostname> to correspond to one website if for each query to the first <hostname> there will be exactly the same query to the second one and vice versa\u00a0\u2014 for each query to the second <hostname> there will be the same query to the first one. Take a look at the samples for further clarifications.Your goal is to determine the groups of server names that correspond to one website. Ignore groups consisting of the only server name.Please note, that according to the above definition queries http://<hostname> and http://<hostname>/ are different.", "input_spec": "The first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of page queries. Then follow n lines each containing exactly one address. Each address is of the form http://<hostname>[/<path>], where: <hostname> consists of lowercase English letters and dots, there are no two consecutive dots, <hostname> doesn't start or finish with a dot. The length of <hostname> is positive and doesn't exceed 20. <path> consists of lowercase English letters, dots and slashes. There are no two consecutive slashes, <path> doesn't start with a slash and its length doesn't exceed 20. Addresses are not guaranteed to be distinct.", "output_spec": "First print k\u00a0\u2014 the number of groups of server names that correspond to one website. You should count only groups of size greater than one. Next k lines should contain the description of groups, one group per line. For each group print all server names separated by a single space. You are allowed to print both groups and names inside any group in arbitrary order.", "sample_inputs": ["10\nhttp://abacaba.ru/test\nhttp://abacaba.ru/\nhttp://abacaba.com\nhttp://abacaba.com/test\nhttp://abacaba.de/\nhttp://abacaba.ru/test\nhttp://abacaba.de/test\nhttp://abacaba.com/\nhttp://abacaba.com/t\nhttp://abacaba.com/test", "14\nhttp://c\nhttp://ccc.bbbb/aba..b\nhttp://cba.com\nhttp://a.c/aba..b/a\nhttp://abc/\nhttp://a.c/\nhttp://ccc.bbbb\nhttp://ab.ac.bc.aa/\nhttp://a.a.a/\nhttp://ccc.bbbb/\nhttp://cba.com/\nhttp://cba.com/aba..b\nhttp://a.a.a/aba..b/a\nhttp://abc/aba..b/a"], "sample_outputs": ["1\nhttp://abacaba.de http://abacaba.ru", "2\nhttp://cba.com http://ccc.bbbb \nhttp://a.a.a http://a.c http://abc"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nno warnings 'uninitialized';\n\n\nmy $N = <> + 0;\n\nmy $npaths = 0;\nmy %map_paths = ();\n\nmy %host_paths = ();\n\nfor (my $i = 0; $i < $N; ++$i) {\n my $url = <>;\n chomp $url;\n $url =~ m{http://([.a-z]+)(.*)};\n my ($hostname, $path) = ($1, $2);\n #print $hostname, ' ', $path, \"\\n\";\n\n my $id;\n if ( not exists $map_paths{ $path } ) {\n $id = $map_paths{ $path } = $npaths++\n }\n else {\n $id = $map_paths{ $path }\n }\n $host_paths{ $hostname }{$id} = 1\n}\n\nmy %paths_hosts = ();\nwhile (my ($key, $path_set) = each %host_paths) {\n my $paths = join(':', sort keys %$path_set);\n #print $key, ' ', $paths, \"\\n\";\n push( @{$paths_hosts{ $paths }}, $key )\n}\n\nmy @res;\nwhile (my ($key, $list) = each %paths_hosts) {\n next if @$list <= 1;\n push(@res, join(' ', map('http://' . $_, @$list)))\n}\n\nprint scalar @res, \"\\n\";\nprint join(\"\\n\", @res), \"\\n\"\n"}, {"source_code": "use strict;\n\nchomp (my $n = );\n\nmy $data;\n\nwhile (<>) {\n chomp;\n # if (m#http://([a-z.0-9]+)(/[a-z./]*)?$#) {\n if (m#http://([a-z.]+)(/[a-z./]*)?$#) {\n if (length $2) {\n $data->{$1}->{$2}++\n } else {\n $data->{$1}->{'-'}++\n }\n # } else {\n # die;\n }\n}\n\nmy $hoq;\n\nforeach (keys %{$data}) {\n $hoq->{join \"|\", sort keys %{$data->{$_}}}->{$_}++;\n}\n\nmy @sol;\n\nforeach (keys %{$hoq}) {\n push @sol, join \" \", map {\"http://\" . $_} keys %{$hoq->{$_}} if ((scalar keys %{$hoq->{$_}}) > 1);\n}\n\nprint sprintf \"%d\\n%s\", (scalar @sol), (join \"\\n\", @sol);\n"}, {"source_code": "use warnings;\nuse strict;\n\nmy %host2path;\nmy %path2host;\n\nmy $n = <>;\nchomp $n;\n$n += 0;\n\nfor (my $i = 0; $i < $n; $i++){\n my $url = <>;\n $url =~ m!^(http://[\\w\\.]+)(/[\\w\\./]*)?\\n$! or die \"bad url\";\n my $host = $1;\n my $path = $2 || '';\n #print STDERR \"host = \\'$host\\' path = \\'$path\\'\\n\";\n if ($host2path{$host}) {\n ${$host2path{$host}}{$path} = undef;\n }\n else {\n $host2path{$host} = { $path => undef };\n }\n }\n\nwhile (my ($host, $pathes) = each %host2path){\n my $ppp = join '#', sort keys %$pathes;\n #print STDERR \"ppp = \\'$ppp\\' host = \\'$host\\'\\n\";\n if ($path2host{$ppp}){\n push @{$path2host{$ppp}}, $host;\n }\n else {\n $path2host{$ppp} = [ $host ]\n }\n }\n \nmy @res = ();\nwhile (my ($ppp, $hosts) = each %path2host){\n push @res, $hosts if (scalar(@$hosts) > 1);\n }\n\nprint scalar(@res), \"\\n\";\nprint join(' ', @$_), \"\\n\" foreach (@res);\n\nexit(0);\n"}, {"source_code": "$\\ = $/, <>;\n\nm!\\b(?:/.*)?$!, ++ $h{ $` }{ $& } for <>;\n\npush @{ $g{ join ' ', sort keys %{ $h{ $_ } } } }, $_ for keys %h;\n@ans = grep / /, map { join ' ', @{ $g{ $_ } } } keys %g;\n\nmap { print } ~~ @ans, @ans\n"}, {"source_code": "$_ = <>, chomp, m!http://[\\w.]+!, undef $h{ $& }{ $' } for 1 .. <>;\n\npush @{ $g{ join ' ', sort keys %{ $h{ $_ } } } }, $_ for keys %h;\npush @ans, grep / /, join ' ', @{ $g{ $_ } } for keys %g;\n\nprint join $/, ~~ @ans, @ans"}, {"source_code": "$\\ = $/, <>;\n\nm!\\b(?:/.*)?$!, ++ $h{ $` }{ $& } for <>;\n\npush @{ $g{ join ' ', sort keys %{ $h{ $_ } } } }, $_ for keys %h;\n@ans = grep / /, map { join ' ', @{ $g{ $_ } } } keys %g;\n\nmap { print } ~~ @ans, @ans"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nno warnings 'uninitialized';\n\n\nmy $N = <> + 0;\n\nmy $npaths = 0;\nmy %map_paths = ();\n\nmy %host_paths = ();\n\nfor (my $i = 0; $i < $N; ++$i) {\n my $url = <>;\n chomp $url;\n $url =~ m{http://([.a-z]+)(.*)};\n my ($hostname, $path) = ($1, $2);\n #print $hostname, ' ', $path, \"\\n\";\n\n my $id;\n if ( not exists $map_paths{ $path } ) {\n $id = $map_paths{ $path } = $npaths++\n }\n else {\n $id = $map_paths{ $path }\n }\n push( @{$host_paths{ $hostname }}, $id )\n}\n\nmy %paths_hosts = ();\nwhile (my ($key, $list) = each %host_paths) {\n my $paths = join(':', sort @$list);\n# print $key, ' ', $paths, \"\\n\"\n push( @{$paths_hosts{ $paths }}, $key )\n}\n\nmy @res;\nwhile (my ($key, $list) = each %paths_hosts) {\n next if @$list <= 1;\n push(@res, join(' ', map('http://' . $_, @$list)))\n}\n\nprint scalar @res, \"\\n\";\nprint join(\"\\n\", @res), \"\\n\"\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\tmy %h;\n\tmy %g;\n\tmy @ans;\n\t\n\tfor (1 .. $_){\n\t\t$_ = <>, chomp;\n\t\tm!/\\K[\\w.]+!;\n\t\tundef $h{ $& }{ $' };\n\t\t}\n\t\n\tpush @{ $g{ join ' ', sort keys %{ $h{ $_ } } } }, $_ for keys %h;\n\t@{ $g{ $_ } } > 1 and push @ans, join ' ', @{ $g{ $_ } } for keys %g;\n\t\n\tprint ~~ @ans;\n\tprint for @ans;\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\tmy %h;\n\tmy %g;\n\tmy @ans;\n\t\n\tfor (1 .. $_){\n\t\t$_ = <>, chomp;\n\t\tm!/\\K[\\w.]+!;\n\t\tundef $h{ $& }{ $' };\n\t\t}\n\t\n\tpush @{ $g{ join ' ', sort keys %{ $h{ $_ } } } }, $_ for keys %h;\n\t@{ $g{ $_ } } > 1 and push @ans, join ' ', @{ $g{ $_ } } for keys %g;\n\t\n\tprint ~~ @ans;\n\tprint s!\\b(?=\\w)!http://!gr for @ans;\n\t}"}], "src_uid": "9b35f7df9e21162858a8fac8ee2837a4"} {"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)$$$ \u2014 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 \u2014 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": "$\\ = $/;\r\n \r\n<>;\r\n \r\nprint eval <> =~ y/ /|/r while <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tprint 0 + eval join '|', @_;\n\t}"}, {"source_code": "$\\ = $/;\n \n<>;\n \nprint eval <> =~ y/ /|/r while <>"}], "negative_code": [], "src_uid": "7fca54a73076ddfeb30b921b9e341f26"} {"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$$$) \u2014 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": "use strict;\nmy $try = <>;\nwhile($try > 0){\n my @arr = split(' ', <>);\n if($arr[1] >= 2*$arr[0]){\n printf(\"%d %d\\n\", $arr[0], 2*$arr[0]);\n } else {\n print \"-1 -1\\n\";\n }\n $try--;\n}"}], "negative_code": [], "src_uid": "e8ba3fb95800806465386ecbfbe924e9"} {"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\u2009-\u20091 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,\u2009k\u00a0(1\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u2009105). The second line contains n integers a1,\u2009a2,\u2009...,\u2009an\u00a0(1\u2009\u2264\u2009ai\u2009\u2264\u2009103), 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": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=split/ /,<>;\nfor (@_){\n\t$a[$i++ % $']+=$_;\n\t}\n$min=min(@a);\n\nfor (@a){\n\t$j++;\n\tif ($_==$min){\n\t\tprint $j;\n\t\tlast\n\t\t}\n\t}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "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,\u2009j), that i\u2009<\u2009j 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\u2009\u2264\u2009n\u2009\u2264\u2009105)\u00a0\u2014 the number of strings in robot's memory. Next n lines contain the strings t1,\u2009t2,\u2009...,\u2009tn, 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\t\n\tmy $H = 0;\n\t\n\t$_ = join '', \n\tmap { $_->[ 0 ] }\n\tsort { $a->[ 1 ] <=> $b->[ 1 ] } \n\tmap { \n\t\t$s = () = /s/g;\n\t\t$H += $h = () = /h/g;\n\t\t$debug and print \" [s:$s|h:$h]\";\n\t\t$debug and print \" \", $h / ( $s || 1 / ~0 );\n\t\t[ $_ => $h / ( $s || 1 / ~0 ) ]\n\t\t} @_;\n\t\n\t$debug and print;\n\t\t\n\t$sum = 0;\n\t\n\t$& eq 'h' ? $H -- : ( $sum += $H ) while /./g;\n\t\n\tprint $sum;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\t$_ = join '', \n\tmap { $_->[ 0 ] }\n\tsort { $a->[ 1 ] <=> $b->[ 1 ] } \n\tmap { \n\t\tmy $s = () = /s/g;\n\t\tmy $h = () = /h/g;\n\t\t$debug and print \" [s:$s|h:$h]\";\n\t\t$debug and print \" \", $h / ( $s || 1 / ~0 );\n\t\t[ $_ => $h / ( $s || 1 / ~0 ) ]\n\t\t} @_;\n\t\n\t$debug and print;\n\t\n\tmy $h = () = /h/g;\n\t\n\tmy $sum = 0;\n\t\n\twhile( /./g ){\n\t\t$& eq 'h' ? do {\n\t\t\t\t$h --;\n\t\t\t}\n\t\t\t: do {\n\t\t\t\t$sum += $h;\n\t\t\t\t}\n\t\t\n\t\t}\n\t\n\tprint $sum;\n\t}"}, {"source_code": "<>;\n\n$_ = join '', \nsort { $a <=> $b } \nmap { \n\t$H += $h = () = /h/g;\n\t$h / ( ( () = /s/g ) || 1 / ~0 ) . \" $_\"\n\t} <>;\n\ns/[^sh]//g;\n\n$& eq 'h' ? $H -- : ( $s += $H ) while /./g;\n\nprint 0 + $s"}], "negative_code": [], "src_uid": "f88cf470095f250ffeb57feae7697e36"} {"nl": {"description": "AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 50$$$) \u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) \u2014 the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq 10^5$$$) \u2014 the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, if there exists a possible sequence of operations, print \"YES\" (without quotes); otherwise, print \"NO\" (without quotes). You can print each letter in any case (upper or lower).", "sample_inputs": ["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"], "sample_outputs": ["YES\nYES\nNO"], "notes": "NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my @di = (); $#di = $n - 1; # 0=right , 1 =left\r\n \r\n my @a1 = (); $#a1 = $n - 1;\r\n my @nc =(); $#nc = 100000;\r\n \r\n for(my $i=0;$i<$n;$i++){\r\n $a1[$i] = +[$A[$i],$i];\r\n $nc[$A[$i]]++;\r\n }\r\n \r\n my @sa = sort { $a->[0] <=> $b->[0] } @a1;\r\n \r\n my $i=0;\r\n my $res = 'YES';\r\n while($i<$n){\r\n if( $nc[$sa[$i]->[0]] == 1 ){\r\n if( abs($i - $sa[$i]->[1]) % 2 != 0 ){\r\n $res = 'NO'; last;\r\n }\r\n $i++;\r\n } else {\r\n my $v = $sa[$i]->[0];\r\n my $c1 = $nc[$v];\r\n \r\n my %ca = ('0'=>0,'1'=>0); my %cb = ('0'=>0,'1'=>0);\r\n for(my $j=0;$j<$c1;$j++){\r\n $ca{ ($i+$j)%2 }++;\r\n $cb{ $sa[$i+$j]->[1] % 2 }++;\r\n }\r\n unless( $ca{'0'} == $cb{'0'} and $ca{'1'} == $cb{'1'} ){\r\n $res = 'NO'; last;\r\n }\r\n $i+=$c1;\r\n }\r\n }\r\n print \"$res\\n\";\r\n \r\n}\r\n\r\nexit(0);\r\n\r\n#### mod_int + nCr nPr set\r\n\r\n#!/usr/bin/perl\r\n\r\nmy ($n,$k) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\n&mk_fact($n+10); # << \r\n\r\n### write here \r\n\r\nexit(0);\r\n\r\nsub mod_pow { # ( x ** n ) % mod\r\n my ($x,$n) = @_;\r\n my $r = 1;\r\n while( $n ){\r\n $r = ( $r * $x ) % $mod if 1 & $n;\r\n $n >>= 1;\r\n $x = ( $x * $x ) % $mod;\r\n }\r\n return $r;\r\n}\r\n\r\nsub mk_fact { # make {global} n-size variable fact[] factinv[]\r\n my $n_max = shift;\r\n our @fact = (1); $#fact = $n_max;\r\n our @factinv = (1); $#factinv = $n_max;\r\n for(my $i=1;$i<=$n_max;$i++){\r\n $fact[$i] = ( $fact[$i-1] * $i ) % $mod;\r\n $factinv[$i] = &mod_pow($fact[$i],$mod-2);\r\n }\r\n}\r\n\r\nsub nCr { # calc nCr % mod\r\n my $n = shift; my $r = shift;\r\n return (( $fact[$n] * ( ( $factinv[$r] * $factinv[$n-$r] ) % $mod ) ) % $mod);\r\n}\r\nsub nPr { # calc nPr % mod\r\n my $n = shift; my $r = shift;\r\n return (( $fact[$n] * $factinv[$n-$r] ) % $mod );\r\n}\r\n\r\nsub gcd { # gcd\r\n my ($x,$y) = @_;\r\n return $x if $y == 0;\r\n return &gcd($y, $x % $y);\r\n}\r\nsub egcd { # ax+by=gcd(a,b) -> x,y return gcd(a,b)\r\n my ($a,$b,$x,$y) = @_;\r\n if( $b == 0 ){\r\n $$x = 1;\r\n $$y = 0;\r\n return $a;\r\n }\r\n my $d = &egcd($b,$a % $b,$y,$x);\r\n $$y -= int($a/$b) * $$x;\r\n return $d;\r\n}\r\nsub modinv { # required 'use integer'\r\n my $b = $mod; my $u = 1; my $v = 0;\r\n while($b){\r\n my $t = $a / $b;\r\n ($a,$b) = ($b, $a - $t * $b);\r\n ($u,$v) = ($v, $u - $t * $v);\r\n }\r\n $u %= $mod;\r\n $u += $mod if $u < 0;\r\n return $u;\r\n}\r\nsub factr { # factorialize\r\n my ($v) = @_;\r\n my %f = ();\r\n if( $v<4 ){\r\n $f{$v}++;\r\n return \\%f;\r\n }\r\n for(my $i=1;$i*$i<=$v;$i+=2){\r\n my $ii = ($i == 1 ? 2 : $i);\r\n while($v % $ii == 0){\r\n $f{$ii}++;\r\n $v /= $ii;\r\n }\r\n }\r\n $f{$v}++ if $v>1;\r\n return \\%f;\r\n}\r\n\r\n#### union find set\r\n\r\n#!/usr/bin/perl\r\n\r\nmy ($n) = map { $_ - 0 } split(/\\s+/o,);\r\n&mk_uf($n);\r\n#### write here\r\n\r\nexit(0);\r\n\r\nsub mk_uf {\r\n my $n = shift;\r\n our @uf = (); $#uf = $n - 1;\r\n for(my $i=0;$i<$n;$i++){\r\n $uf[$i] = $i;\r\n }\r\n}\r\n\r\nsub root {\r\n my $x = shift;\r\n while( $uf[$x] != $x ){ $x = $uf[$x] = $uf[$uf[$x]]; }\r\n return $x;\r\n}\r\n\r\nsub unite {\r\n my $x = &root(scalar(shift)); my $y = &root(scalar(shift));\r\n return if $x == $y;\r\n $uf[$y] = $x;\r\n}\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){\r\n $r = $e if !defined($r) or $e > $r;\r\n }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){\r\n $r = $e if !defined($r) or $e < $r;\r\n }\r\n return $r;\r\n}\r\n\r\n"}], "negative_code": [], "src_uid": "1d27d6d736d891b03b7476f8a7209291"} {"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$$$)\u00a0\u2014 number of planets. The second line contains the only integer $$$m$$$ ($$$1 \\le m \\le 1000$$$)\u00a0\u2014 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": "#!usr/bin/perl -w\nuse strict;\n\nmy $n=;my $m=;my $a=;my $b=;\nchomp($n,$m,$a,$b);my $s=$m;\nif($a=~/\\A1 /||$a=~/ 1\\Z/||$a=~/ 1 /||$b=~/\\A1 /||$b=~/ 1 /||$b=~/ 1\\Z/){print \"-1\\n\";}\nelse{\n my @al=split/ /,$a;my @bl=split/ /,$b;\n my $i=0;my $j=0;\n while($j<=$#al){\n\t$s+=$s/($bl[$#bl-$i]-1);$i++;\n\t$s+=$s/($al[$#al-$j]-1);$j++;\n }\n print ($s-$m,\"\\n\");\n}"}], "negative_code": [], "src_uid": "d9bd63e03bf51ed87ba73cd15e8ce58d"} {"nl": {"description": "According to a new ISO standard, a flag of every country should have a chequered field n\u2009\u00d7\u2009m, each square should be of one of 10 colours, and the flag should be \u00abstriped\u00bb: 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100), n \u2014 the amount of rows, m \u2014 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": "#!/usr/bin/perl\n($n,$m) = split /\\D/, <>;\nflag:for (1..$n) {\n chomp($str = <>);\n @arr = split(//, $str);\n @arr = sort { $a <=> $b } @arr;\n if ($arr[0] == $arr[$#arr]) {\n $color = $arr[0];\n if (!defined $colors{$color}) {\n $colors{$color} = $color;\n if ( (keys %colors) > 10) {\n print \"NO\";\n goto end;\n } \n }\n if (defined $pcolor) {\n if ($pcolor eq $color) {\n print \"NO\";\n goto end;\n }\n }\n $pcolor = $color;\n } else {\n print \"NO\";\n goto end;\n }\n}\nprint \"YES\";\nend:\n__END__"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\nwhile(<>=~/ /){\n \t$t=-1; $f=0;\n\tfor $i(1..$`+0){\n\t\t$_=<>;\n\t\tchomp;\n\t\t/./;\n\t\t$t==$& and $f++;\n\t\t$t=$&;\n\t\ts/(.)\\1*//;\n\t\t$_ and $f++;\n\t\t}\n\tprint ($f?\"NO\\n\":\"YES\\n\");\n\t}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n($n,$m) = split /\\D/, <>;\nflag:for (1..$n) {\n chomp($str = <>);\n @arr = split(//, $str);\n @arr = sort { a <=> b } @arr;\n if ($arr[0] == $arr[$#arr]) {\n $color = $arr[0];\n if (!defined $colors{$color}) {\n $colors{$color} = $color;\n if ( (keys %colors) > 10) {\n print \"NO\";\n goto end;\n } \n }\n if (defined $pcolor) {\n if ($pcolor eq $color) {\n print \"NO\";\n goto end;\n }\n }\n $pcolor = $color;\n } else {\n print \"NO\";\n goto end;\n }\n}\nprint \"YES\";\nend:\n__END__"}], "src_uid": "80d3da5aba371f4d572ea928025c5bbd"} {"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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $B;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ] >= $_[ $i + 1 ] ){\n\t\t\t$B .= 0;\n\t\t\t}\n\t\telse{\n\t\t\t$B .= 1;\n\t\t\t}\n\t\t}\n\t\n\tmy @cand = map 1 + length, $B =~ /1+/g;\n\t\n\twhile( $B =~ / 1+ (0) (?= (1+) ) /xg ){\n\t\t\n\t\tif( $_[ $-[ 1 ] - 1 ] < $_[ $-[ 1 ] + 1 ] or $_[ $-[ 1 ] - 0 ] < $_[ $-[ 1 ] + 2 ] ){\n\t\t\tpush @cand, length $& . $2;\n\t\t\t}\n\t\t}\n\t\n\tprint +( sort { $b <=> $a } 1, @cand )[ 0 ];\n\t}"}, {"source_code": "<>;\n\n@_ = split ' ', <>;\n\n$_ = join '', map 0 + ( $_[ $_ ] < $_[ $_ + 1 ] ), 0 .. @_ - 2;\n\nmy @cand = map 1 + length, /1+/g;\n\nwhile( / 1+ () 0 (?= (1+) ) /xg ){\n\t\n\tif( grep $_[ $-[ 1 ] - 1 + $_ ] < $_[ $-[ 1 ] + 1 + $_ ], 0, 1 ){\n\t\tpush @cand, length $& . $2;\n\t\t}\n\t}\n\nprint +( sort { $b <=> $a } 1, @cand )[ 0 ]"}], "negative_code": [{"source_code": "<>;\n\n@_ = split ' ', <>;\n\n$_ = join '', map $_[ $_ ] < $_[ $_ + 1 ], 0 .. @_ - 2;\n\nmy @cand = map 1 + length, /1+/g;\n\nwhile( / 1+ 0 (?= 1+ ) /xg ){\n\t\n\tif( grep $_[ ( pos ) - 2 + $_ ] < $_[ ( pos ) + 0 + $_ ], 0, 1 ){\n\t\tpush @cand, length $&;\n\t\t}\n\t}\n\nprint +( sort { $b <=> $a } 1, @cand )[ 0 ]"}, {"source_code": "<>;\n\n@_ = split ' ', <>;\n\n$_ = join '', map $_[ $_ ] < $_[ $_ + 1 ], 0 .. @_ - 2;\n\nmy @cand = map 1 + length, /1+/g;\n\nwhile( / 1+ 0 (?= (1+) ) /xg ){\n\t\n\tif( grep $_[ ( pos ) - 2 + $_ ] < $_[ ( pos ) + 0 + $_ ], 0, 1 ){\n\t\tpush @cand, length $& . $1;\n\t\t}\n\t}\n\nprint +( sort { $b <=> $a } 1, @cand )[ 0 ]"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $B;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ] >= $_[ $i + 1 ] ){\n\t\t\t$B .= 0;\n\t\t\t}\n\t\telse{\n\t\t\t$B .= 1;\n\t\t\t}\n\t\t}\n\t\n\tmy @cand = map 1 + length, $B =~ /1+/g;\n\t\n\twhile( $B =~ / 1+ (0) ( (?=1+) ) /xg ){\n\t\t\n\t\tif( $_[ $-[ 1 ] - 1 ] < $_[ $-[ 1 ] + 1 ] or $_[ $-[ 1 ] - 0 ] < $_[ $-[ 1 ] + 2 ] ){\n\t\t\tpush @cand, 1 + length $& . $2;\n\t\t\t}\n\t\t}\n\t\n\tprint +( sort { $b <=> $a } @cand )[ 0 ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $B;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( $_[ $i ] >= $_[ $i + 1 ] ){\n\t\t\t$B .= 0;\n\t\t\t}\n\t\telse{\n\t\t\t$B .= 1;\n\t\t\t}\n\t\t}\n\t\n\tmy @cand = map 1 + length, $B =~ /1+/g;\n\t\n\twhile( $B =~ / 1+ (0) ( (?=1+) ) /xg ){\n\t\t\n\t\tif( $_[ $-[ 1 ] - 1 ] < $_[ $-[ 1 ] + 1 ] or $_[ $-[ 1 ] - 0 ] < $_[ $-[ 1 ] + 2 ] ){\n\t\t\tpush @cand, 1 + length $& . $2;\n\t\t\t}\n\t\t}\n\t\n\tprint +( sort { $b <=> $a } 1, @cand )[ 0 ];\n\t}"}], "src_uid": "87b8dccfc0e5a63cd209c37cf8aebef0"} {"nl": {"description": "In this problem at each moment you have a set of intervals. You can move from interval (a,\u2009b) from our set to interval (c,\u2009d) from our set if and only if c\u2009<\u2009a\u2009<\u2009d or c\u2009<\u2009b\u2009<\u2009d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2.Your program should handle the queries of the following two types: \"1 x y\" (x\u2009<\u2009y) \u2014 add the new interval (x,\u2009y) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals. \"2 a b\" (a\u2009\u2260\u2009b) \u2014 answer the question: is there a path from a-th (one-based) added interval to b-th (one-based) added interval? Answer all the queries. Note, that initially you have an empty set of intervals.", "input_spec": "The first line of the input contains integer n denoting the number of queries, (1\u2009\u2264\u2009n\u2009\u2264\u2009105). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed 109 by their absolute value. It's guaranteed that all queries are correct.", "output_spec": "For each query of the second type print \"YES\" or \"NO\" on a separate line depending on the answer.", "sample_inputs": ["5\n1 1 5\n1 5 11\n2 1 2\n1 2 9\n2 1 2"], "sample_outputs": ["NO\nYES"], "notes": null}, "positive_code": [{"source_code": "#!usr/bin/perl\n#use warnings;\n#use strict;\n#use autodie;\n#use diagnostics;\nuse utf8;\nuse 5.010;\nuse 5.012;\nuse 5.014;\nmy($t,$x,$y,$count,$pos,$s,@a,@b,@check,$c);\nchomp($t=);\n$count=0;\n$pos=0;\nsub dfs\n{\n my($n,$i);\n $n=pop @_;\n $check[$n]=1;\n for($i=0;$i<$pos;$i++)\n {\n if(((($a[$i]<$a[$n]) && ($a[$n]<$b[$i])) || (($a[$i]<$b[$n]) && ($b[$n]<$b[$i]))) && ($check[$i]==0))\n {\n dfs($i);\n }\n }\n}\nwhile($count++ != $t)\n{\n my $i;\n chomp($s=);\n ($c,$x,$y)=split / /,$s;\n if($c==1)\n {\n $a[$pos]=$x;\n $b[$pos]=$y;\n $pos++;\n }\n else\n {\n for($i=0;$i<$pos;$i++)\n {\n $check[$i]=0;\n }\n dfs($x-1);\n if($check[$y-1] == 1)\n {\n say \"YES\";\n }\n else\n {\n say \"NO\";\n }\n }\n}"}], "negative_code": [], "src_uid": "c686c3592542b70a3b617eb639c0e3f4"} {"nl": {"description": "\u00abBersoft\u00bb company is working on a new version of its most popular text editor \u2014 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 \u00abstandardization\u00bb 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\u2009+\u20091\u2009<\u2009li\u2009+\u20091 for all i from 1 to k\u2009-\u20091, and li\u2009\u2264\u2009ri. 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\u2009=\u2009ri, this element should be output as li, and not as \u00abli\u2009-\u2009li\u00bb.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": "#!/usr/bin/env perl\npackage Codeforces::Task::_;\nuse v5.10;\nuse strict;\nuse warnings;\n\nsub min{my($a,$b)=@_;if($a<$b){return$a;}else{return$b;}}\nsub max{my($a,$b)=@_;if($a>$b){return$a;}else{return$b;}}\n\nsub out {\n my @x = @_;\n print(shift(@x), \" \") while (scalar(@x));\n print(\"\\n\");\n}\n\nsub solve {\n my @ans = ();\n my $prev = -1;\n foreach my $c (sub {\n my %s = ();\n my $input = <>; chomp $input;\n foreach my $el (split \",\", $input) {\n $s{$el} = 1;\n }\n return sort {$a <=> $b} keys %s;\n }->()) {\n if ($prev + 1 == $c) {\n my ($beg) = $ans[$#ans] =~ m/(\\d*)(-|$)/;\n $ans[$#ans] = \",$beg-$c\";\n } else {\n push(@ans, \",$c\");\n }\n $prev = $c;\n \t}\n print substr join(\"\", @ans), 1;\n}\n\nsub main {\n my $t = 1;\n# $t = int(<>);\n while ($t--) {\n solve();\n }\n return 0;\n}\n\nexit(main(@ARGV));\n"}], "negative_code": [], "src_uid": "3969ba3e3eb55a896663d2c5a5bc4a84"} {"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\u00a0\u2014 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\u2009+\u2009aj.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\u00a0\n9\n\u00a0\n7\n\u00a0\n9\n\u00a0\n11\n\u00a0\n6"], "sample_outputs": ["? 1 5\n\u00a0\n? 2 3\n\u00a0\n? 4 1\n\u00a0\n? 5 2\n\u00a0\n? 3 4\n\u00a0\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\u2009\u2264\u2009n\u2009\u2264\u20095000)\u00a0\u2014 the length of the array. The second line contains n numbers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009105)\u00a0\u2014 the elements of the array to guess. "}, "positive_code": [{"source_code": "$| = 1; $n = <>;\nsub get { print \"? @_\\n\"; +<> }\n$m[$_] = get($_-1, $_) for 2..$n;\n$m[1] = ($m[2] - $m[3] + get(1,3))/2;\n$m[$_] -= $m[$_-1] for 2..$n;\nprint \"!@m\\n\";\n\n"}], "negative_code": [], "src_uid": "1898e591b40670173e4c33e08ade48ba"} {"nl": {"description": "Permutation p is an ordered set of integers p1,\u2009\u2009p2,\u2009\u2009...,\u2009\u2009pn, 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,\u2009\u2009p2,\u2009\u2009...,\u2009\u2009pn.The decreasing coefficient of permutation p1,\u2009p2,\u2009...,\u2009pn is the number of such i (1\u2009\u2264\u2009i\u2009<\u2009n), that pi\u2009>\u2009pi\u2009+\u20091.You have numbers n and k. Your task is to print the permutation of length n with decreasing coefficient k.", "input_spec": "The single line contains two space-separated integers: n,\u2009k (1\u2009\u2264\u2009n\u2009\u2264\u2009105,\u20090\u2009\u2264\u2009k\u2009<\u2009n) \u2014 the permutation length and the decreasing coefficient.", "output_spec": "In a single line print n space-separated integers: p1,\u2009p2,\u2009...,\u2009pn \u2014 the permutation of length n with decreasing coefficient k. If there are several permutations that meet this condition, print any of them. It is guaranteed that the permutation with the sought parameters exists.", "sample_inputs": ["5 2", "3 0", "3 2"], "sample_outputs": ["1 5 2 4 3", "1 2 3", "3 2 1"], "notes": null}, "positive_code": [{"source_code": "$in = <>; chomp $in;\n$in =~ /(\\d+) (\\d+)/;\n$n = $1; $k = $2;\n\n$i = $n;\n\nfor (1..($k)) {\n print \"$i \";\n $i--;\n}\n$i = 1;\nfor ($k..($n-1)) {\n print \"$i \";\n $i++;\n}\n\nprint \"\\n\";\n"}, {"source_code": "while (<>){\n @m=();\n @n=();\n $i=0;\n ($n, $k) = split/ /,$_;\n \n for (1..$n-$k-1){\n $m[$i++]=$_ ;\n \n }\n \n $i=0;\n for ($n-$k..$n){\n $n[$i++]=$_;\n \n }\n @n = reverse @n;\n \n print \"@m\";\n print \" \" if @m;\n print \"@n\\n\";\n }"}], "negative_code": [{"source_code": "$in = <>; chomp $in;\n$in =~ /(\\d+) (\\d+)/;\n$n = $1; $k = $2;\n\n$i = $n;\n\nfor (1..($k)) {\n print \"$i \";\n $i--;\n}\nfor ($k..($n-1)) {\n print \"$i \";\n}\n\nprint \"\\n\";\n"}], "src_uid": "75cc5b55c51217966cbdd639a68f0724"} {"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\u00a0\u2014 $$$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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n \r\n my ($n,$x) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my @sa = sort { $a <=> $b } @A;\r\n my $r = 0;\r\n my $s = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n $s += $sa[$i];\r\n last if $s > $x;\r\n my $v = 1 + ( $x - $s ) / (1+$i);\r\n $r += $v;\r\n }\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\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\u2009+\u20097.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\u2009\u2264\u2009|s|\u2009\u2264\u2009100\u2009000), consisting of digits, lowercase and uppercase English letters, characters '-' and '_'.", "output_spec": "Print a single integer\u00a0\u2014 the number of possible pairs of words, such that their bitwise AND is equal to string s modulo 109\u2009+\u20097.", "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&_\u2009=\u200961&63\u2009=\u200961\u2009=\u2009z _&z\u2009=\u200963&61\u2009=\u200961\u2009=\u2009z z&z\u2009=\u200961&61\u2009=\u200961\u2009=\u2009z "}, "positive_code": [{"source_code": "for $i (0..63) {\n\tfor $b (0..5) {\n\t\t$z[$i]++ unless $i & (1<<$b);\n\t}\n}\n\nchomp($s = <>);\n$s =~ y%0-9A-Za-z\\-_%\\0-?%;\n$c = 0; $c += $z[ vec($s, $_, 8) ] for 0..length($s)-1;\n\nuse bigint; print 3->bmodpow($c, 10**9 + 7);\n"}], "negative_code": [], "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\u2009+\u20091) 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\u2009\u2264\u2009n\u2009\u2264\u20095000) \u2014 the number of students at an exam.", "output_spec": "In the first line print integer k \u2014 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,\u2009a2,\u2009...,\u2009ak (1\u2009\u2264\u2009ai\u2009\u2264\u2009n), 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\u2009-\u2009ai\u2009+\u20091|\u2009\u2260\u20091 for all i from 1 to k\u2009-\u20091. 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": "\nmy $n = int<>;\n\nif ($n >= 5) {\n print \"$n\\n\";\n print join ' ', map {2 * $_ - 1} (1..int(($n+1)/2));\n print ' ';\n print join ' ', map {2 * $_} (1..int(($n)/2));\n} else {\n if ($n == 4) {\n print \"4\\n3 1 4 2\";\n } elsif ($n == 3) {\n print \"2\\n1 3\";\n } else {\n print \"1\\n1\";\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\nuse integer;\n\nchomp($n = <>);\n$n<=2 and say \"1\\n1\" and exit;\n$n==3 and say \"2\\n1 3\" and exit;\n$n==4 and say \"4\\n3 1 4 2\" and exit;\nsay $n;\nfor ($i=1; $i<=$n; $i+=2) {\n\tprint \"$i \";\n}\nfor ($i=2; $i<=$n; $i+=2) {\n\tprint \"$i \";\n}"}, {"source_code": "#!perl\n\n$n = <>;\nchomp $n;\n\n$,= \"\\n\";\n$\\= \"\\n\";\n\nif($n == 1 || $n == 2)\n{\n print 1, 1; \n}\nelsif($n == 3)\n{\n print 2, \"1 3\";\n}\nelse\n{\n map {$_%2 == 0 ? push @a, $_ : push @b, $_} (1..$n); \n \n print $n, \"@a @b\";\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\n$n = int($n);\n\nif ($n == 1 || $n == 2) {\n print \"1\\n\";\n print \"1\\n\";\n} elsif ($n == 3) {\n print \"2\\n\";\n print \"1 3\\n\";\n} elsif ($n == 4) {\n print \"4\\n\";\n print \"3 1 4 2\\n\";\n} else {\n print \"$n\\n\";\n print \"1\";\n for (my $odds = 3; $odds <= $n; $odds += 2) {\n print \" $odds\";\n }\n for (my $evens = 2; $evens <= $n; $evens += 2) {\n print \" $evens\";\n }\n print \"\\n\";\n}\n\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = \n\t(\n\t\t(grep 1 - $_ % 2, 1 .. $_)\n\t,\n\t\t(grep $_ % 2, 1 .. $_)\n\t)\n\t;\n\t$_ = \"@_\";\n\ts/^2 1/1/;\n\t\n\tprint 0 + split;\n\tprint\n}"}, {"source_code": "$\\ = $/;\nwhile(<>){\n\tchomp;\n@_ = \n(\n\t(grep $_ % 2 == 0, 1 .. $_)\n,\n\t(grep $_ % 2 == 1, 1 .. $_)\n)\n;\n$_ = \"@_\";\ns/\\b2 (?=1\\b)//;\n\t($n, $m) = split;\nprint 0 + split;\n\tprint\n\t\n\t\n\t}"}], "negative_code": [{"source_code": "my $n = int<>;\n\nif ($n >= 5) {\n print join ' ', map {2 * $_ - 1} (1..int(($n+1)/2));\n print ' ';\n print join ' ', map {2 * $_} (1..int(($n)/2));\n} else {\n if ($n == 4) {\n print \"3 1 4 2\";\n } elsif ($n == 3) {\n print \"1 3\";\n } else {\n print \"1\";\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\nuse integer;\n\nchomp($n = <>);\n$n<=2 and say \"1\\n1\" and exit;\n$n==3 and say \"2\\n1 3\" and exit;\n$n==4 and say \"3\\n1 4 2\" and exit;\nsay $n;\nfor ($i=1; $i<=$n; $i+=2) {\n\tprint \"$i \";\n}\nfor ($i=2; $i<=$n; $i+=2) {\n\tprint \"$i \";\n}"}, {"source_code": "#!perl\n\n$n = <>;\n\n$,= \"\\n\";\n$\\= \"\\n\";\n\nif($n == 1 || $n == 2)\n{\n print 1, 1; \n}\nelsif($n == 3)\n{\n print 2, \"1 3\";\n}\nelse\n{\n map {$_%2 == 0 ? push @a, $_ : push @b, $_} (1..$n); \n \n print \"@a @b\";\n}"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\n$n = int($n);\n\nif ($n == 1 || $n == 2) {\n print \"1\\n\";\n print \"1\\n\";\n} elsif ($n == 3) {\n print \"2\\n\";\n print \"1 3\\n\";\n} elsif ($n == 4) {\n print \"3\\n\";\n print \"1 4 2\\n\";\n} else {\n print \"$n\\n\";\n print \"1\";\n for (my $odds = 3; $odds <= $n; $odds += 2) {\n print \" $odds\";\n }\n for (my $evens = 2; $evens <= $n; $evens += 2) {\n print \" $evens\";\n }\n print \"\\n\";\n}\n\n"}], "src_uid": "a52ceb8a894809b570cbb74dc5ef76e1"} {"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\u2009\u2264\u2009mi\u2009\u2264\u2009119) 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\u2009\u2264\u2009wi\u2009\u2264\u200910) is Kevin's number of wrong submissions on problem i. The last line contains two space-separated integers hs and hu (0\u2009\u2264\u2009hs,\u2009hu\u2009\u2264\u200920), 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\u00b7100\u2009=\u20091000 points from hacks, his total score becomes 3930\u2009+\u20091000\u2009=\u20094930."}, "positive_code": [{"source_code": "@x = qw(500 1000 1500 2000 2500);\n@m = split / +/, <>;\n@w = split / +/, <>;\n($hs,$hu) = split / +/, <>;\n\nsub max {\n\tmy ($a,$b)=@_;\n\t$a>$b? $a: $b;\n}\n\nfor (0..4) {\n\t$s += max(0.3*$x[$_], $x[$_]-$x[$_]/250*$m[$_]-50*$w[$_]);\n}\n$s += 100*$hs;\n$s -= 50*$hu;\n\nprint $s, \"\\n\";"}, {"source_code": "#!/usr/bin/perl\n\nsub max {\n return $_[0] > $_[1] ? $_[0]: $_[1];\n}\n\n@m = split(' ', );\n@w = split(' ', );\n@h = split(' ', );\n$i = 1;\n$sum = 0;\nforeach (@m) {\n $a = 0.3 * 500 * $i;\n $b = (500 - 2 * $m[$i-1]) * $i - 50 * $w[$i-1];\n $t = max($a, $b);\n $i ++;\n $sum += $t;\n}\n\n$sum += $h[0] * 100 - $h[1] * 50;\nprint $sum . \"\\n\";\n\n"}, {"source_code": "@m = split ' ', <>;\n@f = split ' ', <>;\n($s, $u) = split ' ', <>;\n\n$sum += (sort {$b <=> $a} 0.3 * 500 * ++ $x, 500 * $x - (shift @m) * $x * 2 - 50 * shift @f)[ 0 ] while @m;\n\t\nprint $sum + $s * 100 - $u * 50"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@mins = split;\n\t@fails = split ' ', <>;\n\t($sh, $uh) = split ' ', <>;\n\t\n\t$x = 0;\n\t$sum = 0;\n\t\n\tfor (@mins){\n\t\t$x ++;\n\t\t$sum += (sort {$b <=> $a} 0.3 * 500 * $x, $x * 500 - $_ * $x * 2 - 50 * $fails[ $x - 1 ])[ 0 ];\n\t\t}\n\t\n\tprint $sum + $sh * 100 - $uh * 50;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nsub max {\n return $_[1] > $_[2] ? $_[1]: $_[2];\n}\n\n@m = split(' ', );\n@w = split(' ', );\n@h = split(' ', );\n$i = 1;\n$sum = 0;\nforeach (@m) {\n $a = 0.3 * 500 * $i;\n $b = (1 - $_ / 250) * 500 * $i - 50 * $w[$i];\n $t = max($a, $b);\n $i ++;\n $sum += $t;\n}\n\n$sum += $h[0] * 100 - $h[1] * 50;\nprint $sum . \"\\n\";\n\n"}, {"source_code": "#!/usr/bin/perl\n\nsub max {\n return $_[1] > $_[2] ? $_[1]: $_[2];\n}\n\n@m = split(' ', );\n@w = split(' ', );\n@h = split(' ', );\n$i = 1;\n$sum = 0;\nforeach (@m) {\n $a = 0.3 * 500 * $i;\n $b = (1 - $m[$i-1] / 250) * 500 * $i - 50 * $w[$i-1];\n $t = max($a, $b);\n $i ++;\n $sum += $t;\n}\n\n$sum += $h[0] * 100 - $h[1] * 50;\nprint $sum . \"\\n\";\n\n"}], "src_uid": "636a30a2b0038ee1731325a5fc2df73a"} {"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$$$) \u2014 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$$$) \u2014 park sizes.", "output_spec": "Print $$$t$$$ answers to the test cases. Each answer must be a single integer \u2014 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": "$n = ;\nchomp $n;\n@streets = ;\nchomp @streets;\n\nforeach (@streets) {\n if ($_ != \" \") {\n @str = split(' ', $_);\n $str_num = $str[0] * $str[1];\n use integer;\n $ans = ($str_num / 2) + ($str_num%2.0);\n push(@array, \"$ans\\n\");\n \n #print \"$street\";\n }\n}\nprint @array;"}], "negative_code": [], "src_uid": "19df5f3b8b31b763162c5331c1499529"} {"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\u2009\u2264\u2009i,\u2009j\u2009\u2264\u2009N2. S[i]\u2009=\u2009S[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,\u2009y) and (y,\u2009x) should be considered different, i.e. the ordered pairs count.", "sample_inputs": ["great10", "aaaaaaaaaa"], "sample_outputs": ["7", "100"], "notes": null}, "positive_code": [{"source_code": "#!perl -nl\n++$h{$_} for split'',$_;\n$a+=$h{$_}**2 for keys %h;\nprint $a;\n"}, {"source_code": "while(<>){\n\t$a=@_=();\n\tchomp;\n\ts/./$_[ord($&)]++/ge;\n\t$a+=$_**2 for @_;\n\tprint \"$a\\n\";\n\t}"}, {"source_code": "chomp($_=<>);\n@a=split//;\n$_[ord $_]++ for @a;\n$a+=$_**2 for @_;\nprint $a"}, {"source_code": "map {$_[ord $_]++} split//,<>;\n$_ && $i++ and $a+=$_**2 for @_;\nprint $a"}, {"source_code": "map {/./ && $_[ord $_]++} split//,<>;\n$a+=$_**2 for @_;\nprint $a"}], "negative_code": [{"source_code": "map {$_[ord $_]++} split//,<>;\n$a+=$_**2 for @_;\nprint $a"}], "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\u00a0\u2014 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": "#!/usr/bin/env perl\npackage Codeforces::Task::_;\nuse v5.10;\nuse strict;\n#use warnings;\n\nsub min{my($a,$b)=@_;if($a<$b){return$a;}else{return$b;}}\nsub max{my($a,$b)=@_;if($a>$b){return$a;}else{return$b;}}\n\nsub solve {\n my @p = (<> =~ m/([\\d\\.]+)/gx);\n my $r = 0;\n foreach my $c (@p) {\n $c =~ s/(?:(?=\\.\\d{3})\\.|)//gx;\n $r += $c;\n }\n $r = sprintf(\"%.02f\", $r);\n $r =~ s/\\.00$//;\n while ($r =~ s/(\\d+)(\\d{3})/$1\\.$2/gx)\n {};\n print \"$r\\n\";\n}\n\nsub main {\n my $t = 1;\n# $t = int(<>);\n while ($t--) {\n solve();\n }\n return 0;\n}\n\nexit(main(@ARGV));\n"}, {"source_code": "$input = ;\n$input =~ s{ \\. (\\d{3}) } [$1]gx;\n\nwhile ($input =~ /[\\d+\\.]+/g) {\n\t$sum += $&;\n}\n\n$sum = sprintf \"%.02f\", $sum;\n$sum =~ s/\\.00$//;\nwhile ( $sum =~ s{ (\\d+) (\\d{3}) (\\.|$) } [$1\\.$2$3]gx ) {};\n\nprint $sum; "}, {"source_code": "$,=\" \";\nmy $s=<>;\nchomp($s);\n$s =~ s/\\.([0-9][0-9][0-9])/$1/g;\nprint STDERR $s. \"\\n\";\n$s =~ s/[a-z]+/,/g;\nprint STDERR $s. \"\\n\";\nmy $sum = 0;\nmy @temp=split /,/, $s;\nprint STDERR scalar(@temp). \"\\n\";\nfor(@temp)\n{\n $sum+=$_ if($_);\n}\nprint STDERR $s , \"\\n\";\nmy @f=split /\\./,$sum;\nprint STDERR \"@f \\n\";\nmy @c = unpack(\"(A3)*\", reverse $f[0]);\n$f[0] = reverse join '.', @c;\nif($f[1])\n{\n if(length($f[1])==1)\n {\n $f[1]=$f[1].\"0\";\n }\n}\nmy $ans = join \".\",@f;\nprint $ans . \"\\n\";\n"}, {"source_code": "print ~~ reverse(\n\t\n\t( reverse eval join ' + ', map s/\\.(?=\\d{3})//gr, <> =~ /[.\\d]+/g\n\t)\n\t\t=~ s/\\d{3}\\K(?!$)/./gr\n\t\t\n) =~ s/\\..\\K$/0/r"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\npackage Codeforces::Task::_;\nuse v5.10;\nuse strict;\n#use warnings;\n\nsub min{my($a,$b)=@_;if($a<$b){return$a;}else{return$b;}}\nsub max{my($a,$b)=@_;if($a>$b){return$a;}else{return$b;}}\n\nsub solve {\n my @p = (<> =~ m/([\\d\\.]+)/g);\n my $r = 0;\n foreach my $c (@p) {\n $c =~ s/(?:(?=\\.\\d{3})\\.|)//g;\n $r += $c;\n }\n $r =~ s/(\\d+)(\\d{3})/$1\\.$2/g;\n print \"$r\\n\";\n}\n\nsub main {\n my $t = 1;\n# $t = int(<>);\n while ($t--) {\n solve();\n }\n return 0;\n}\n\nexit(main(@ARGV));\n"}, {"source_code": "#!/usr/bin/env perl\npackage Codeforces::Task::_;\nuse v5.10;\nuse strict;\n#use warnings;\n\nsub min{my($a,$b)=@_;if($a<$b){return$a;}else{return$b;}}\nsub max{my($a,$b)=@_;if($a>$b){return$a;}else{return$b;}}\n\nsub solve {\n my @p = (<> =~ m/([\\d\\.]+)/gx);\n my $r = 0;\n foreach my $c (@p) {\n $c =~ s/(?:(?=\\.\\d{3})\\.|)//gx;\n $r += $c;\n }\n while ($r =~ s/(\\d+)(\\d{3})/$1\\.$2/gx)\n {};\n print \"$r\\n\";\n}\n\nsub main {\n my $t = 1;\n# $t = int(<>);\n while ($t--) {\n solve();\n }\n return 0;\n}\n\nexit(main(@ARGV));\n"}, {"source_code": "$input = ;\n$input =~ s{ \\. (\\d{3}) }{$1}x;\n\nwhile ($input =~ /[\\d+\\.]+/g) {\n\t$sum += $&;\n}\n\n$sum = sprintf \"%.02f\", $sum;\n$sum =~ s/\\.00$//;\n$sum =~ s{ (\\d+) (\\d{3}) (\\.|$) } [$1\\.$2$3]x;\n\nprint $sum; "}, {"source_code": "$,=\" \";\nmy $s=<>;\nchomp($s);\n$s =~ s/\\.([0-9][0-9][0-9])/$1/g;\nprint STDERR $s. \"\\n\";\n$s =~ s/[a-z]+/,/g;\nprint STDERR $s. \"\\n\";\nmy $sum = 0;\nmy @temp=split /,/, $s;\nprint STDERR scalar(@temp). \"\\n\";\nfor(@temp)\n{\n $sum+=$_ if($_);\n}\nprint STDERR $s , \"\\n\";\nmy @f=split /\\./,$sum;\nprint STDERR \"@f \\n\";\nmy @c = unpack(\"(A3)*\", reverse $f[0]);\n$f[0] = reverse join '.', @c;\nmy $ans = join \".\",@f;\nprint $ans . \"\\n\";\n"}, {"source_code": "$,=\" \";\nmy $s=<>;\nchomp($s);\n$s =~ s/.([0-9][0-9][0-9])/$1/g;\nmy $sum = 0;\n#print $s , \"\\n\";\nmap {$sum += $_ } split /[a-z]+/,$s;\nmy @f=split /\\./,$sum;\nmy @c = unpack(\"(A3)*\", reverse $f[0]);\n$sum = reverse join '.', @c;\n$sum =$sum . '.' .$f[1];\nprint $sum , \"\\n\";\n"}, {"source_code": "$,=\" \";\nmy $s=<>;\nchomp($s);\n$s =~ s/.([0-9][0-9][0-9])/$1/g;\nmy $sum = 0;\nmap {$sum += $_ if($_) } split /[a-z]+/,$s;\nprint STDERR $sum , \"\\n\";\nmy @f=split /\\./,$sum;\nprint STDERR \"@f \\n\";\nmy @c = unpack(\"(A3)*\", reverse $f[0]);\n$f[0] = reverse join '.', @c;\nmy $ans = join \".\",@f;\nprint $ans . \"\\n\";\n"}, {"source_code": "$,=\" \";\nmy $s=<>;\nchomp($s);\n$s =~ s/.([0-9][0-9][0-9])/$1/g;\nmy $sum = 0;\nmap {$sum += $_ if($_) } split /[a-z]+/,$s;\nmy @f=split /\\./,$sum;\nmy @c = unpack(\"(A3)*\", reverse $f[0]);\n$f[0] = reverse join '.', @c;\n$f[0] =$f[0] . '.' .$f[1] if($f[1]);\nprint $f[0] , \"\\n\";\n"}, {"source_code": "$,=\" \";\nmy $s=<>;\nchomp($s);\n$s =~ s/.([0-9][0-9][0-9])/$1/g;\nmy $sum = 0;\nmap {$sum += $_ } split /[a-z]+/,$s;\nmy @f=split /\\./,$sum;\nmy @c = unpack(\"(A3)*\", reverse $f[0]);\n$sum = reverse join '.', @c;\n$sum =$sum . '.' .$f[1] if($f[1]);\nprint $sum , \"\\n\";\n"}, {"source_code": "$,=\" \";\nmy $s=<>;\nchomp($s);\n$s =~ s/\\.([0-9][0-9][0-9])/$1/g;\nprint STDERR $s. \"\\n\";\n$s =~ s/[a-z]+/,/g;\nprint STDERR $s. \"\\n\";\nmy $sum = 0;\nmy @temp=split /,/, $s;\nprint STDERR scalar(@temp). \"\\n\";\nfor(@temp)\n{\n $sum+=$_ if($_);\n}\nprint STDERR $s , \"\\n\";\nmy @f=split /\\./,$sum;\nprint STDERR \"@f \\n\";\nmy @c = unpack(\"(A3)*\", reverse $f[0]);\n$f[0] = reverse join '.', @c;\nif($f[1])\n{\n $f[1]=sprintf(\"%.02d\",$f[1]);\n}\nmy $ans = join \".\",@f;\nprint $ans . \"\\n\";\n"}, {"source_code": "$,=\" \";\nmy $s=<>;\nchomp($s);\n$s =~ s/.([0-9][0-9][0-9])/$1/g;\n$s =~ s/[a-z]+/,/g;\nmy $sum = 0;\nmy @temp=split /,/, $s;\nprint STDERR scalar(@temp). \"\\n\";\nfor(@temp)\n{\n $sum+=$_ if($_);\n}\nprint STDERR $s , \"\\n\";\nmy @f=split /\\./,$sum;\nprint STDERR \"@f \\n\";\nmy @c = unpack(\"(A3)*\", reverse $f[0]);\n$f[0] = reverse join '.', @c;\nmy $ans = join \".\",@f;\nprint $ans . \"\\n\";\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t$_ .= 'a';\n\t@_ = map y/./_/r, grep length, split /[a-z]+/;\n\t@kop = grep /_..$/, @_;\n\t@rub = grep !/_..$/, @_;\n\t\n\t@kop = map s/_(..)$/.$1/r, @kop;\n\t\n\t$sum = 0 + eval join ' + ', @kop, @rub;\n\tprint scalar reverse( (reverse $sum) =~ s/(\\d{3})(?!$)/$1./gr );\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t$_ .= 'a';\n\t@_ = map y/./_/r, grep length, split /[a-z]+/;\n\t@kop = grep /_..$/, @_;\n\t@rub = grep !/_..$/, @_;\n\t\n\t@kop = map s/_(..)$/.$1/r, @kop;\n\t\n\t$sum = 0 + eval join ' + ', @kop, @rub;\n\tprint scalar reverse( (reverse $sum) =~ s/(?<=\\d{3})(?!$)/./gr );\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t$_ .= 'a';\n\t@_ = map y/./_/r, grep length, split /[a-z]+/;\n\t@kop = grep /_..$/, @_;\n\t@rub = grep !/_..$/, @_;\n\t\n\t@kop = map s/_(..)$/.$1/r, @kop;\n\t\n\t$sum = 0 + eval join ' + ', @kop, @rub;\n\tprint scalar reverse( (reverse $sum) =~ s/(?<=\\d{3})(?!$)/./r );\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t$_ .= 'a';\n\t@_ = map y/./_/r, grep length, split /[a-z]+/;\n\t@kop = grep /_..$/, @_;\n\t@rub = grep !/_..$/, @_;\n\t\n\t@kop = map s/_(..)$/.$1/r, @kop;\n\t\n\t$sum = 0 + eval join ' + ', @kop, @rub;\n\tprint scalar reverse( (reverse $sum) =~ s/(?<=\\d{3})/./r );\n\t}"}], "src_uid": "8da703549a3002bf9131d6929ec026a2"} {"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 \u2014 a non-empty string consisting of lowercase Latin letters. Each directory (except for the root) has a parent directory \u2014 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. \u00ab..\u00bb 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\u2009\u2264\u2009n\u2009\u2264\u200950) \u2014 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": "#!/usr/bin/perl\n# Codeforces Practice\n\nuse warnings;\nuse strict;\n\nmy $n = ;\nchomp($n);\n\nmy @currPath = ();\nmy $currCmd = \"\";\n\nfor(1..$n){\n $currCmd = ;\n if($currCmd eq \"pwd\\n\"){\n if (@currPath == 0){print \"/\\n\";}\n else{\n my $printPath = join(\"/\",@currPath);\n print \"/$printPath/\\n\";}\n } else {\n my @cdPath = split(\"/\",(split(/\\s+/,$currCmd))[1]);\n foreach my $dir (@cdPath){\n if($dir eq \"\"){\n @currPath = ();\n } elsif ($dir eq \"..\") {\n pop(@currPath);\n } else {\n push(@currPath,$dir);\n }\n }\n }\n}"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\nuse Data::Dumper;\n\nmy $n = <>;\nmy @wd = ();\n\nfor (1 .. $n) {\n my ($cmd, $arg) = split ' ', <>;\n if ($cmd eq 'pwd') {\n print '/', (map { $_ . '/' } @wd), \"\\n\";\n } else {\n if ($arg =~ m|^/|) {\n @wd = ();\n $arg = substr $arg, 1;\n }\n for (split '/', $arg) {\n push @wd, $_;\n }\n my @work = ();\n for (@wd) {\n if ($_ eq '..') {\n pop @work;\n } else {\n push @work, $_;\n }\n }\n @wd = @work;\n }\n}\n"}, {"source_code": "use 5.010;\nuse strict;\nuse warnings;\n\nmy $n = <>;\nmy $path = '/';\nwhile ($n--) {\n chomp( $_ = <> );\n given ($_) {\n when ( 'pwd' ) { say $path }\n when ( m|^cd (/.*)| ) { $path = $1 }\n when ( m|^cd (.*)| ) { $path .= $1 }\n }\n $path =~ s,^//,/,;\n 1 while $path =~ s,/\\w+/\\.\\.,, ;\n $path .= '/' unless $path =~ m|/$|;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nmy $n=4;\nmy @command;\nchomp($n=<>);\nfor (my $i=0;$i<$n;$i++)\n{\n chomp($command[$i]=<>);\n}\nmy $pwd=\"/\";\n\nfor (my $i=0;$i<$n;$i++)\n{\n if ($command[$i]=~m/^cd/)\n {\n $command[$i]=~s/\\n//g;\n $command[$i]=~s/^cd //g;\n if ($command[$i]=~m/^\\//)\n {\n $pwd=\"/\";\n $command[$i]=~s/^\\///g;\n }\n my @temp=split(/\\//,$command[$i]);\n for (my $j=0;$j<=$#temp;$j++)\n {\n if ($temp[$j] eq \"..\")\n {\n if ($pwd ne \"/\") {$pwd=~s/\\w{1,}\\/$//g;}\n } else\n {\n $pwd=\"$pwd$temp[$j]/\";\n }\n }\n } elsif ($command[$i]=~m/^pwd/)\n {\n print \"$pwd\\n\";\n }\n}"}, {"source_code": "\n$\\=$/;\n\n$path=\"/\";\n$n=;\nmy @paths;\nmy $temp;\nforeach $i(1 .. $n){\n ($comm,$temp)=split(\" \",);\n #print (index $temp,\"/\").\"\\n\".$temp;\n if ($comm eq \"cd\"){\n if ((index $temp,\"/\") eq 0){\n $path=\"/\";\n #print \"absolute path\\n\";\n }\n @paths=split(\"/\",$temp);\n foreach $dir (@paths){\n if ($dir eq \"..\"){\n #print \"step out\\n\";\n substr($path,rindex($path,\"/\"))=\"\";\n substr($path,rindex($path,\"/\"))=\"/\";\n }else{\n #print \"adding path\\ndir=$dir\\n\";\n $path.=$dir.\"/\" if ($dir =~/[a-z]/);\n }\n }\n \n }else{\n #$ans.=$path;\n print $path;\n }\n}\n#print $ans;\n"}, {"source_code": "$n = <>;\n$path = \"\\/\";\nfor($i=0;$i<$n;$i++) {\n $_ = <>;\n if(/^pwd$/) {\n 1while($path =~ s/[a-z]+\\/\\.\\.\\///g);\n $path =~ s/^\\/\\.\\.//g;\n print $path , \"\\n\";\n } else {\n s/^cd //;\n s/\\r|\\n//g;\n $_.=\"\\/\"unless(/\\/$/);\n if(/^\\//) {\n $path = $_\n } else {\n $path .= $_\n }\n }\n}"}, {"source_code": "#!/usr/bin/perl\n$root->{'..'} = '';\n$root->{''} = \\%root;\n$root->{'.'} = '/';\n$current_dir = $root;\n\nfor (1..<>) {\n chomp($s = <>);\n ($cmd, $dir) = split(/ /, $s);\n if ($cmd eq 'pwd') {\n @name = ();\n $temp_dir = $current_dir;\n while ($temp_dir->{'.'}) {\n push @name, $temp_dir->{'.'};\n $temp_dir = $temp_dir->{'..'}; # Not a HASH reference\n }\n print ( (reverse @name), \"\\n\");\n } else {\n if ($dir =~ m/^\\//) {\n $current_dir = $root;\n }\n $dir =~ s/^\\///s;\n $dir =~ s/\\/$//s;\n @dirs = split(/\\//, $dir);\n for $dir (@dirs) {\n if (!defined $current_dir->{$dir}) {\n $current_dir->{$dir}->{'.'} = \"$dir\\/\";\n $current_dir->{$dir}->{'..'} = $current_dir;\n $current_dir->{$dir}->{''} = $current_dir->{$dir};\n }\n $current_dir = $current_dir->{$dir};\n }\n } \n}\n\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = <>;\n\nmy @cmd;\npush @cmd, <> for ($n);\n\nmy $curdir = \"/\";\nmy @dirstack = ();\n\nsub pwd() { print $curdir, \"\\n\"; }\n\nsub cd($) {\n my @path = split '/', shift;\n\n $#dirstack = -1 if (!@path[0]);\n\n foreach my $dir (@path)\n {\n pop @dirstack if ($dir eq \"..\");\n push @dirstack, $dir if ($dir ne \"..\");\n }\n\n $curdir = (join \"/\", @dirstack) . \"/\";\n $curdir = \"/$curdir\" if ((substr $curdir,0,1) ne \"/\");\n}\n\nforeach (@cmd)\n{\n my ($cmd, $arg) = split /\\s/, $_;\n\n cd ($arg) if ($cmd eq \"cd\");\n pwd() if ($cmd eq \"pwd\");\n}\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy @curpath = ();\n\nfor (1..$n) {\n my $cmd = <>;\n chomp $cmd;\n if ($cmd =~ s/^cd //) {\n my @dirs = split(/\\//, $cmd);\n if ($dirs[0] eq \"\") {\n @curpath = ();\n shift @dirs;\n }\n foreach my $dir (@dirs) {\n if ($dir eq \"..\") {\n pop @curpath\n } else {\n push @curpath, $dir;\n }\n }\n } elsif ($cmd =~ s/^pwd//) {\n if (scalar @curpath == 0) {\n print \"/\\n\";\n } else {\n print \"/\", join(\"/\", @curpath), \"/\\n\";\n }\n }\n}\n\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy $curr = '/';\n\nwhile (<>) {\n chomp;\n print \"$curr\\n\" and next if $_ eq 'pwd';\n $_ =~ s/cd\\s+//;\n my @d = split '/';\n unless ($d[0]) { $curr = '/'; shift @d; }\n for (@d) {\n $_ eq '..' ? ($curr =~ s|\\w+/$||) : ($curr .= \"$_/\");\n }\n}\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @curr = ();\nmy @tmp = ();\n\nsub print_ans {\n print '/' . (join '/', @curr) . (0+@curr ? \"/\" : \"\"). \"\\n\";\n}\n\n\nsub get_curr {\n @curr = ();\n $_ eq '..' ? (pop @curr and next) : (push @curr, $_) for @tmp;\n}\n\nwhile (<>) {\n chomp;\n print_ans and next if $_ =~ 'pwd';\n $_ =~s/cd\\s+//;\n if ($_ =~ '^/') {\n @tmp = split '/';\n shift @tmp;\n } else {\n @tmp = (@curr, split '/');\n }\n get_curr;\n}\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy $curr = '/';\n\nwhile (<>) {\n chomp;\n print \"$curr\\n\" and next if $_ eq 'pwd';\n $_ =~ s/cd\\s+//;\n my @d = split '/';\n unless ($d[0]) { $curr = '/' ; shift @d; };\n $_ eq '..' ? ($curr =~ s|\\w+/$||) : ($curr .= \"$_/\") for @d;\n}\n"}, {"source_code": "use warnings;\nuse strict;\n\nuse feature ':5.10';\n\nmy $n = ;\nmy $cd = '/';\n\nfor(my $i = 0; $i < $n; ++$i) {\n my $cmd = ;\n chomp $cmd;\n if ($cmd eq 'pwd') { say $cd }\n else {\n my ($dummy, $path) = split / /, $cmd;\n if ($path =~ m&^/&) { $cd = $path.'/' }\n else { $cd .= $path.'/' }\n $cd =~ s&/[a-z]+/\\.\\.&&g while( $cd =~ m&\\.\\.&);\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\n$n = ;\n@pol;\n@pol = (\"\");\n$i=0;\n$b=1;\n$j=0;\nfor ($i=0; $i<$n; $i++)\n{\n\tchomp($a = );\n\tif ($a =~ s/^cd (\\/?)/\\//)\n\t{\n\t\t@mas = split /[\\/]/, $a;\n\t\t$sla=$1;\n\t\tif ($sla=~/\\//)\n\t\t{\n\t\t\t@pol = (\"\");\n\t\t\t$b=1;\n#\t\t\tprint \"1\";\n\t\t\t$sla='';\n\t\t}\n\t\t$q = @mas;\n\n\t\tfor ($j=1; $j<$q; $j++)\n\t\t{\t\n\t\t\tif (($mas[$j] =~ /^\\.\\.$/))\n\t\t\t{\n\t\t\t\t\n\t\t\t\t$b-=1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\t\n\t\t\t\t\n\t\t\t\t$pol[$b+1]= $mas[$j];\n\t\t\t\t$b++;\n\t\t\t\t\n\t\t\t}\n\t\t};\n\t\t\n\t}\n\n\tif ($a =~ /^pwd/)\n\t{\n\n\t\t\n\t#\tprint \"/\";\n\t\tfor ($j = 1; $j<=$b; $j++)\n\t\t{\n\t\t\tprint \"$pol[$j]\\/\";\n\t\t}\n\t\tprint \"\\n\";\n\t}\n\t\n};\n"}, {"source_code": "<>;\n$p = '/';\ns!.* (/?)(.*)$/!($1||$p).\"$2/\"!e ? do{ 0 while s|//+|/|||s|\\w+/\\.+|| ; $p = $_} : print $p.$/ for <>"}, {"source_code": "<>;\n$p = '/';\nchomp, s!^cd !! ? do{ $p .= \"/$_/\", m!^/! and $p = \"$_/\"; 0 while $p =~ s!//+!/!g || $p =~ s!\\w+/\\.\\.!! } : print $p.$/ for <>"}, {"source_code": "<>;\n$p = '/';\ns!^cd (/?)(.*)\\n!($1?$1:$p).\"$2/\"!e ? do{ 0 while s!//+!/!g || s!\\w+/\\.\\.!! ; $p = $_} : print $p.$/ for <>"}, {"source_code": "#!/usr/bin/perl\n\nmy $n=4;\nmy @command;\nchomp($n=<>);\nfor (my $i=0;$i<$n;$i++)\n{\n chomp($command[$i]=<>);\n}\nmy $pwd=\"/\";\n\nfor (my $i=0;$i<$n;$i++)\n{\n if ($command[$i]=~m/^cd/)#sd\n {\n $command[$i]=~s/\\n//g;\n $command[$i]=~s/^cd //g;\n if ($command[$i]=~m/^\\//)\n {\n $pwd=\"/\";\n $command[$i]=~s/^\\///g;\n }\n #\n #comment\n #\n my @temp=split(/\\//,$command[$i]);\n for (my $j=0;$j<=$#temp;$j++)\n {\n if ($temp[$j] eq \"..\")\n {\n if ($pwd ne \"/\") {$pwd=~s/\\w{1,}\\/$//g;}\n } else\n {\n $pwd=\"$pwd$temp[$j]/\";\n }\n }\n } elsif ($command[$i]=~m/^pwd/)\n {\n print \"$pwd\\n\";\n }\n}"}, {"source_code": "#!/usr/bin/perl\n\nmy $n=4;\nmy @command;\nchomp($n=<>);\nfor (my $i=0;$i<$n;$i++)\n{\n chomp($command[$i]=<>);\n}\nmy $pwd=\"/\";\n\nfor (my $i=0;$i<$n;$i++)\n{\n if ($command[$i]=~m/^cd/)#sd\n {\n $command[$i]=~s/\\n//g;\n $command[$i]=~s/^cd //g;\n if ($command[$i]=~m/^\\//)\n {\n $pwd=\"/\";\n $command[$i]=~s/^\\///g;\n }\n #s\n #comment\n #\n my @temp=split(/\\//,$command[$i]);\n for (my $j=0;$j<=$#temp;$j++)\n {\n if ($temp[$j] eq \"..\")\n {\n if ($pwd ne \"/\") {$pwd=~s/\\w{1,}\\/$//g;}\n } else\n {\n $pwd=\"$pwd$temp[$j]/\";\n }\n }\n } elsif ($command[$i]=~m/^pwd/)\n {\n print \"$pwd\\n\";\n }\n}"}, {"source_code": "# http://codeforces.com/problemset/problem/158/C\n\n\n\nmy $n = ;\nchomp ( $n );\n\nmy @path;\n\nmy $f;\nmy @arr;\nmy $cd, $what;\nmy $str;\nfor ( my $i = 0; $i < $n ; $i += 1 ) {\n $f = ;\n chomp ( $f );\n\n if ( $f eq \"pwd\" ) {\t# print current path\n\tif ( @path ) {\t\t# \n\t $str = \"/\" . (join \"/\", @path) . \"/\\n\";\n\t if ( $str =~ s/(\\/+)(.*)/\\/$2/ ) {}\n\t print $str;\n\t}\n\telse {\t\t\t# if path is empty ( at root )\n\t print \"/\\n\"; }\n }\n else {\n\t($cd, $what) = split m/ /, $f; # parse line of input \n\t@arr = split m/\\//, $what; # what == path\n\n\tif ( $what =~ m/\\A\\// ) {\n\t @path = (); }\n\n\twhile ( @arr > 0 ) {\n\t if ( $arr[ 0 ] eq \"..\" ) {\n\t\tpop @path;\n\t\tshift @arr;\n\t }\n\t else {\n\t\tmy $top = (shift @arr);\n\t\tpush @path, $top;\n\t }\n\t}\n }\n}\n\n\n"}, {"source_code": "#!perl\nour $cwd = [];\nsub cd{\n\tmy($cd) = @_;\n\t@cd = $cd eq \"/\" ? (\"\") : split '/', $cd;\n\tfor (my $i = 0; $i <= $#cd; $i++) {\n\t\t$_ = $cd[$i];\n\t\tif ($i == 0 && $_ eq \"\") {\n\t\t\t$#$cwd = -1;\n\t\t} elsif ($_ eq \"..\") {\n\t\t\tpop @$cwd;\n\t\t} else {\n\t\t\tpush @$cwd, $_;\n\t\t}\n\t}\n}\nsub pwd{\n\tif($#$cwd < 0){\n\t\tprint \"/\\n\";\n\t\treturn;\n\t}\n\tprint \"/\" . join(\"/\", @$cwd) . \"/\\n\";\n}\n%sh = (\n\tcd => \\&cd,\n\tpwd => \\&pwd\n);\nmy $k = int ;\nwhile ($k--) {\n\t$_ = ;\n\ts/[\\r\\n]$//g;\n\tmy ($cmd, $prm, $h) = split / +/, $_, 2;\n\tnext if !$cmd;\n\tif (($h = $sh{$cmd})) {\n\t\t$h->($prm);\n\t} else {\n\t\tdie \"unknown command '$cmd'\\n\";\n\t}\n}\n"}, {"source_code": "chomp($n = );\nwhile ($n--)\n{\n chomp($_ = );\n push @commands, $_;\n}\n\nforeach (@commands)\n{\n if (/^pwd/)\n {\n print \"/\";\n print \"$_/\" foreach (@dir);\n print \"\\n\";\n }\n if (/^cd/)\n {\n s#^cd\\s+?##;\n @dir = () if s#^/##;\n @cd = split /\\//, $_;\n foreach (@cd)\n {\n pop @dir if /\\.\\./;\n push @dir, $_ unless /\\.\\./;\n }\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nmy $n=4;\nmy @list;\nchomp($n=<>);\nfor (my $i=0;$i<$n;$i++) {chomp($list[$i]=<>);}\nmy $dir=\"/\";\nfor (my $i=0;$i<$n;$i++) {\n\tif ($list[$i]=~m/^cd/) {\n\t\t$list[$i]=~s/\\n//g;\n\t\t$list[$i]=~s/^cd //g;\n\t\tif ($list[$i]=~m/^\\//) {\n\t\t\t$dir=\"/\";\n\t\t\t$list[$i]=~s/^\\///g;\n\t\t}\n\t\tmy @temp=split(/\\//,$list[$i]);\n\t\tfor (my $j=0;$j<=$#temp;$j++) {\n\t\t\tif ($temp[$j] eq \"..\") {\n\t\t\t\tif ($dir ne \"/\") {$dir=~s/\\w{1,}\\/$//g;}\n\t\t\t} else {\n\t\t\t\t$dir=\"$dir$temp[$j]/\";\n\t\t\t}\n\t\t}\n\t} elsif ($list[$i]=~m/^pwd/) {\n\t\tprint \"$dir\\n\";\n\t}\n}\n"}, {"source_code": "use strict;\n\nmy $n = <>;\nmy $x = '/';\n\nwhile (<>) {\n chomp;\n\n if ($_ eq 'pwd') {\n\n print $x, \"\\n\";\n\n } else {\n\n $_ = substr $_, 3;\n\n if (index ($_, '/') == 0) {\n $x = '/';\n $_ = substr $_, 1;\n }\n\n my @a = split /\\//;\n\n for (@a) {\n if ($_ eq '..') {\n $x =~ s#\\w*/$##;\n } else {\n $x .= $_ . '/';\n }\n }\n\n }\n}"}, {"source_code": "chomp(my $n = );\nmy @path = ();\n\nforeach (1 .. $n) {\n $_ = ;\n my @arg = split;\n\n if ($arg[0] eq 'cd') {\n\tif ($arg[1] =~ /\\A\\//) {\n\t @path = ();\n\t $arg[1] = substr $arg[1], 1\n\t}\n\n\t@arg = split('/', $arg[1]);\n\n\tforeach (@arg) {\n\t if ($_ eq '..') {\n\t\tpop @path;\n\t } else {\n\t\tpush @path, $_;\n\t }\n\t}\n } else {\n\tforeach (@path) {\n\t print \"/\" . $_;\n\t}\n\n\tprint \"/\\n\";\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nmy $n=4;\nmy @command;\nchomp($n=<>);\nfor (my $i=0;$i<$n;$i++) {chomp($command[$i]=<>);}\nmy $pwd=\"/\";\n\nfor (my $i=0;$i<$n;$i++) {\n if ($command[$i]=~m/^cd/) {\n $command[$i]=~s/\\n//g;\n $command[$i]=~s/^cd //g;\n if ($command[$i]=~m/^\\//) {\n $pwd=\"/\";\n $command[$i]=~s/^\\///g;\n }\n my @temp=split(/\\//,$command[$i]);\n for (my $j=0;$j<=$#temp;$j++) {\n if ($temp[$j] eq \"..\") {\n if ($pwd ne \"/\") {$pwd=~s/\\w{1,}\\/$//g;}\n } else {\n $pwd=\"$pwd$temp[$j]/\";\n }\n }\n } elsif ($command[$i]=~m/^pwd/) {\n print \"$pwd\\n\";\n }\n}\n"}, {"source_code": "use strict;\nuse warnings;\n#open STDIN, 'output.txt';\n\nmy $n = <>;\nmy @curr = ();\nmy @tmp = ();\n\nsub print_ans {\n\tprint '/' . (join '/', @curr) . (0+@curr ? \"/\" : \"\"). \"\\n\";\n}\n\n\nsub get_curr {\n\t@curr = ();\n\t$_ eq '..' ? (pop @curr and next) : (push @curr, $_) for @tmp;\n}\n\nwhile (<>) {\n\tchomp;\n\tprint_ans and next if $_ =~ 'pwd';\n\t$_ =~s/cd\\s+//;\n\tif ($_ =~ '^/') {\n\t\t@tmp = split '/';\n\t\tshift @tmp;\n\t} else {\n\t\t@tmp = (@curr, split '/');\n\t}\n\tget_curr;\n}\n"}, {"source_code": "sub parent($) {\n $_[0] =~ s/\\/[a-z]*$//;\n return $_[0];\n}\n\nsub func($) {\n\twhile($_[0] =~ s/[a-z][a-z]*\\/\\.\\.//) {\n\t\t$_[0] =~ s/\\/\\//\\//; # \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0434\u0432\u043e\u0439\u043d\u044b\u0435 \u0441\u043b\u0435\u0448\u0438\n\t}\n\t$_[0] =~ s/^\\///;\n\t$_[0] =~ s/\\/$//;\n\treturn $_[0];\n}\n\n$n = ;\nchomp($n);\n$PATH = '/';\nfor($i = 0; $i < $n; $i++) {\n\t$s = ;\n\tchomp($s);\n\tif($s =~ s/^cd //) {\n\t\tlabel: if( $s =~ m/\\.\\./ ) {\n\t\t\tif( $s =~ s/^\\///) {\n\t\t\t\t$s = '/'.func($s);\n\t\t\t\t$s =~ s/\\/\\//\\//g; #govnokod:)\n\t\t\t}\n\t\t\telse {\n\t\t\t\t$s = func($s);\n\t\t\t}\n\t\t\t#print($s);\n\t\t\tif($s =~ s/^\\.\\.//) {\n\t\t\t $p = parent($PATH);\n\t\t\t $s = $p.$s;\n\n\t\t\t goto label; #govnokod2:)\n\n\t\t\t}\n\t\t}\n\n\n\t\tif( $s =~ m/^\\// ) { \n\t\t\t$PATH = $s;\n\t\t}\n\t\telse { \n\t\t\t$PATH =~ s/\\/$//g;\n\t\t\t$PATH = $PATH.'/'.$s; \n\t\t}\n\n\t}\n\telse {\n\t\t$PATH =~ s/\\/$//;\n\t\tprint($PATH.\"/\\n\");\n\t}\n}\n"}], "negative_code": [{"source_code": "sub parent($) {\n $_[0] =~ s/\\/[a-z]*$//;\n return $_[0];\n}\n\n$n = ;\nchomp($n);\n$PATH = '/';\nfor($i = 0; $i < $n; $i++) {\n\t$s = ;\n\tchomp($s);\n\tif(length($s) > 3) {\n\t\t$s =~ s/cd //;\n\t\tif( $s =~ m/\\.\\./ ) {\n\t\t\twhile($s =~ s/[a-z]*\\/\\.\\.//) {\n\t\t\t\t$s =~ s/\\/\\//\\//g;\n\t\t\t\t$s =~ s/^\\///g;\n\t\t\t}\n\t\t\tif($s =~ s/^\\.\\.//) {\n\t\t\t $p = parent($PATH);\n\t\t\t $s = $p.$s;\n\t\t\t}\n\t\t}\n\n\n\t\tif( $s =~ m/^\\// ) { $PATH = $s; }\n\t\telse { \n\t\t\t$PATH =~ s/\\/$//g;\n\t\t\t$PATH = $PATH.'/'.$s; \n\t\t}\n\t}\n\telse {\n\t\t$PATH =~ s/\\/$//;\n\t\tprint($PATH.\"/\\n\");\n\t}\n}\n"}, {"source_code": "sub parent($) {\n $_[0] =~ s/\\/[a-z]*$//;\n return $_[0];\n}\n\n$n = ;\nchomp($n);\n$PATH = '/';\nfor($i = 0; $i < $n; $i++) {\n\t$s = ;\n\tchomp($s);\n\tif(length($s) > 3) {\n\t\t$s =~ s/cd //;\n\t\tif( $s =~ m/\\.\\./ ) {\n\t\t\tif($s =~ s/[a-z]*\\/\\.\\.//g) {\n\t\t\t\t$s =~ s/^\\///g;\n\t\t\t}\n\t\t\tif($s =~ s/^\\.\\.//) {\n\t\t\t $p = parent($PATH);\n\t\t\t $s = $p.$s;\n\t\t\t}\n\t\t}\n\n\n\t\tif( $s =~ m/^\\// ) { $PATH = $s; }\n\t\telse { \n\t\t\t$PATH =~ s/\\/$//g;\n\t\t\t$PATH = $PATH.'/'.$s; \n\t\t}\n\t}\n\telse {\n\t\t$PATH =~ s/\\/$//;\n\t\tprint($PATH.\"/\\n\");\n\t}\n}\n"}, {"source_code": "sub parent($) {\n $_[0] =~ s/\\/[a-z]*$//;\n return $_[0];\n}\n\nsub func($) {\n\twhile($_[0] =~ s/[a-z]*\\/\\.\\.//) {\n\t\t$_[0] =~ s/\\/\\//\\//; # \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0434\u0432\u043e\u0439\u043d\u044b\u0435 \u0441\u043b\u0435\u0448\u0438\n\t}\n\treturn $_[0];\n}\n\n$n = ;\nchomp($n);\n$PATH = '/';\nfor($i = 0; $i < $n; $i++) {\n\t$s = ;\n\tchomp($s);\n\tif(length($s) > 3) {\n\t\t$s =~ s/cd //;\n\t\tif( $s =~ m/\\.\\./ ) {\n\t\t\tif( $s =~ s/^\\///) {\n\t\t\t\t$s = '/'.func($s);\n\t\t\t\t$s =~ s/\\/\\//\\//g; #govnokod:)\n\t\t\t}\n\t\t\telse {\n\t\t\t\t$s = func($s);\n\t\t\t}\n\n\t\t\tif($s =~ s/^\\.\\.//) {\n\t\t\t $p = parent($PATH);\n\t\t\t $s = $p.$s;\n\t\t\t}\n\t\t}\n\n\n\t\tif( $s =~ m/^\\// ) { \n\t\t\t$PATH = $s;\n\t\t}\n\t\telse { \n\t\t\t$PATH =~ s/\\/$//g;\n\t\t\t$PATH = $PATH.'/'.$s; \n\t\t}\n\t}\n\telse {\n\t\t$PATH =~ s/\\/$//;\n\t\tprint($PATH.\"/\\n\");\n\t}\n}\n"}, {"source_code": "sub parent($) {\n $_[0] =~ s/\\/[a-z]*$//;\n return $_[0];\n}\n\n$n = ;\nchomp($n);\n$PATH = '/';\nfor($i = 0; $i < $n; $i++) {\n\t$s = ;\n\tchomp($s);\n\tif(length($s) > 3) {\n\t\t$s =~ s/cd //;\n\t\tif( $s =~ m/\\.\\./ ) {\n\t\t\t$s =~ s/^\\/// if $s =~ s/[a-z]*\\/\\.\\.//g;\n\t\t\tif($s =~ s/^\\.\\.//) {\n\t\t\t $p = parent($PATH);\n\t\t\t $s = $p.$s;\n\t\t\t #\n\t\t\t}\n\t\t}\n\t\tif( $s =~ m/^\\// ) { $PATH = $s; }\n\t\telse { \n\t\t\t$PATH =~ s/\\/$//;\n\t\t\t$PATH = $PATH.'/'.$s; \n\t\t}\n\t}\n\telse {\n\t\t$PATH =~ s/\\/$//;\n\t\tprint($PATH.\"/\\n\");\n\t}\n}\n"}, {"source_code": "sub parent($) {\n $_[0] =~ s/\\/[a-z]*$//;\n return $_[0];\n}\n\nsub func($) {\n\twhile($_[0] =~ s/[a-z]*\\/\\.\\.//) {\n\t\t$_[0] =~ s/\\/\\//\\//; # \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0434\u0432\u043e\u0439\u043d\u044b\u0435 \u0441\u043b\u0435\u0448\u0438\n\t}\n\t$_[0] =~ s/^\\///;\n\t$_[0] =~ s/\\/$//;\n\treturn $_[0];\n}\n\n$n = ;\nchomp($n);\n$PATH = '/';\nfor($i = 0; $i < $n; $i++) {\n\t$s = ;\n\tchomp($s);\n\tif($s =~ s/^cd //) {\n\t\tif( $s =~ m/\\.\\./ ) {\n\t\t\tif( $s =~ s/^\\///) {\n\t\t\t\t$s = '/'.func($s);\n\t\t\t\t$s =~ s/\\/\\//\\//g; #govnokod:)\n\t\t\t}\n\t\t\telse {\n\t\t\t\t$s = func($s);\n\t\t\t}\n\n\t\t\tif($s =~ s/^\\.\\.//) {\n\t\t\t $p = parent($PATH);\n\t\t\t $s = $p.$s;\n\t\t\t}\n\t\t}\n\n\n\t\tif( $s =~ m/^\\// ) { \n\t\t\t$PATH = $s;\n\t\t}\n\t\telse { \n\t\t\t$PATH =~ s/\\/$//g;\n\t\t\t$PATH = $PATH.'/'.$s; \n\t\t}\n\t}\n\telse {\n\t\t$PATH =~ s/\\/$//;\n\t\tprint($PATH.\"/\\n\");\n\t}\n}\n"}, {"source_code": "sub parent($) {\n $_[0] =~ s/\\/[a-z]*$//;\n return $_[0];\n}\n\n$n = ;\nchomp($n);\n$PATH = '/';\nfor($i = 0; $i < $n; $i++) {\n\t$s = ;\n\tchomp($s);\n\tif(length($s) > 3) {\n\t\t$s =~ s/cd //;\n\t\tif( $s =~ m/\\.\\./ ) {\n\t\t\tif($s =~ s/[a-z]*\\/\\.\\.//g) {\n\t\t\t\t$s =~ s/^\\/*[^\\/]//;\n\t\t\t}\n\t\t\tif($s =~ s/^\\.\\.//) {\n\t\t\t $p = parent($PATH);\n\t\t\t $s = $p.$s;\n\t\t\t}\n\t\t}\n\n\n\t\tif( $s =~ m/^\\// ) { $PATH = $s; }\n\t\telse { \n\t\t\t$PATH =~ s/\\/$//g;\n\t\t\t$PATH = $PATH.'/'.$s; \n\t\t}\n\t}\n\telse {\n\t\t$PATH =~ s/\\/$//;\n\t\tprint($PATH.\"/\\n\");\n\t}\n}\n"}, {"source_code": "#!/usr/bin/perl\n# Codeforces Practice\n\nuse warnings;\nuse strict;\n\nmy $n = ;\nchomp($n);\n\n\nmy @currPath;\nmy $currCmd = \"\";\n\nfor(1..$n){\n $currCmd = ;\n if($currCmd eq \"pwd\"){\n my $printPath = join(\"/\",@currPath);\n print \"/$printPath/\\n\";\n } else {\n my @cdPath = split(\"/\",(split(/\\s+/,$currCmd))[1]);\n foreach my $dir (@cdPath){\n if($dir eq \"\"){\n @currPath = ();\n } elsif ($dir eq \"..\") {\n pop(@currPath);\n } else {\n push(@currPath,$dir);\n }\n }\n }\n}"}, {"source_code": "$\\=$/;\n\n$path=\"/\";\n$n=;\nmy @paths;\nmy $temp;\nforeach $i(1 .. $n){\n ($comm,$temp)=split(\" \",);\n #print (index $temp,\"/\").\"\\n\".$temp;\n if ($comm eq \"cd\"){\n if ((index $temp,\"/\") eq 0){\n $path=$temp.\"/\";\n #print \"absolute path\\n\";\n }else{\n @paths=split(\"/\",$temp);\n foreach $dir (@paths){\n if ($dir eq \"..\"){\n #print \"step out\\n\";\n substr($path,rindex($path,\"/\"))=\"\";\n substr($path,rindex($path,\"/\"))=\"/\";\n }else{\n #print \"adding path\\n\";\n $path.=$dir.\"/\";\n }\n }\n }\n }else{\n #$ans.=$path;\n print $path;\n }\n}\n#print $ans;\n"}, {"source_code": "$n = <>;\n$path = \"\\/\";\nfor($i=0;$i<$n;$i++) {\n $_ = <>;\n if(/^pwd$/) {\n $path =~ s/[a-z]+\\/\\.\\.\\///g;\n $path =~ s/^\\/\\.\\.//g;\n print $path , \"\\n\";\n } else {\n s/^cd //;\n s/\\r|\\n//g;\n $_.=\"\\/\"unless(/\\/$/);\n if(/^\\//) {\n $path = $_\n } else {\n $path .= $_\n }\n }\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = <>;\n\nmy @cmd;\npush @cmd, <> for ($n);\n\nmy $curdir = \"/\";\nmy @dirstack = ();\n\nsub pwd() { print $curdir, \"\\n\"; }\n\nsub cd($) {\n my @path = split '/', shift;\n\n foreach my $dir (@path)\n {\n pop @dirstack if ($dir eq \"..\");\n push @dirstack, $dir if ($dir ne \"..\");\n }\n\n $curdir = join \"/\", @dirstack;\n}\n\nforeach (@cmd)\n{\n my ($cmd, $arg) = split /\\s/, $_;\n\n cd ($arg) if ($cmd eq \"cd\");\n pwd() if ($cmd eq \"pwd\");\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = <>;\n\nmy @cmd;\npush @cmd, <> for ($n);\n\nmy $curdir = \"/\";\nmy @dirstack = ();\n\nsub pwd() { print $curdir, \"\\n\"; }\n\nsub cd($) {\n my @path = split '/', shift;\n\n foreach my $dir (@path)\n {\n pop @dirstack if ($dir eq \"..\");\n push @dirstack, $dir if ($dir ne \"..\");\n }\n\n $curdir = (join \"/\", @dirstack) . \"/\";\n $curdir = \"/$curdir\" if ((substr $curdir,0,1) ne \"/\");\n}\n\nforeach (@cmd)\n{\n my ($cmd, $arg) = split /\\s/, $_;\n\n cd ($arg) if ($cmd eq \"cd\");\n pwd() if ($cmd eq \"pwd\");\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = <>;\n\nmy @cmd;\npush @cmd, <> for ($n);\n\nmy $curdir = \"/\";\nmy @dirstack = ();\n\nsub pwd() { print $curdir, \"\\n\"; }\n\nsub cd($) {\n my @path = split '/', shift;\n\n foreach my $dir (@path)\n {\n pop @dirstack if ($dir eq \"..\");\n push @dirstack, $dir if ($dir ne \"..\");\n }\n\n $curdir = (join \"/\", @dirstack) . \"/\";\n}\n\nforeach (@cmd)\n{\n my ($cmd, $arg) = split /\\s/, $_;\n\n cd ($arg) if ($cmd eq \"cd\");\n pwd() if ($cmd eq \"pwd\");\n}\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy @curpath = (\"\");\n\nfor (1..$n) {\n my $cmd = <>;\n chomp $cmd;\n if ($cmd =~ s/^cd //) {\n my @dirs = split(/\\//, $cmd);\n if ($dirs[0] eq \"\") {\n @curpath = @dirs;\n } else {\n foreach my $dir (@dirs) {\n if ($dir eq \"..\") {\n pop @curpath\n } else {\n push @curpath, $dir;\n }\n }\n }\n } elsif ($cmd =~ s/^pwd//) {\n if (scalar @curpath == 1) {\n print \"/\\n\";\n } else {\n print join(\"/\", @curpath), \"/\\n\";\n }\n }\n}\n\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy @curpath = ();\n\nfor (1..$n) {\n my $cmd = <>;\n chomp $cmd;\n if ($cmd =~ s/^cd //) {\n my @dirs = split(/\\//, $cmd);\n if ($dirs[0] eq \"\") {\n @curpath = ();\n }\n foreach my $dir (@dirs) {\n if ($dir eq \"..\") {\n pop @curpath\n } else {\n push @curpath, $dir;\n }\n }\n } elsif ($cmd =~ s/^pwd//) {\n if (scalar @curpath == 0) {\n print \"/\\n\";\n } else {\n print join(\"/\", @curpath), \"/\\n\";\n }\n }\n}\n\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @curr = ();\nmy @tmp = ();\n\nsub print_ans {\n print '/' . (join '/', @curr) . \"/\\n\";\n}\n\n\nsub get_curr {\n @curr = ();\n $_ eq '..' ? (pop @curr and next) : (push @curr, $_) for @tmp;\n}\n\n\nwhile (<>) {\n chomp;\n print_ans and next if $_ =~ 'pwd';\n $_ =~s/cd\\s+//;\n if ($_ =~ '^/') {\n @tmp = split '/';\n shift @tmp;\n } else {\n @tmp = (@curr, split '/');\n }\n get_curr;\n}\n"}, {"source_code": "use warnings;\nuse strict;\n\nuse feature ':5.10';\n\nmy $n = ;\nmy $cd = '/';\n\nfor(my $i = 0; $i < $n; ++$i) {\n my $cmd = ;\n chomp $cmd;\n if ($cmd eq 'pwd') { say $cd }\n else {\n my ($dummy, $path) = split / /, $cmd;\n if ($path =~ m&^/&) { $cd = $path }\n else { $cd .= '/'.$path }\n $cd =~ s&/[a-z]+/\\.\\.&&g\n }\n}"}, {"source_code": "use warnings;\nuse strict;\n\nuse feature ':5.10';\n\n#my $n = ;\n#my $cd = '/';\n#\n#for(my $i = 0; $i < $n; ++$i) {\n# my $cmd = ;\n# chomp $cmd;\n# if ($cmd eq 'pwd') { say $cd }\n# else {\n# my ($dummy, $path) = split / /, $cmd;\n# if ($path =~ m&^/&) { $cd = $path }\n# else { $cd .= '/'.$path }\n# $cd =~ s&/[a-z]+/\\.\\.&&g\n# }\n#}\n\nsay '/\n/home/vasya/\n/home/\n/home/petya/'"}, {"source_code": "#!/usr/bin/perl\n$n = ;\n@pol;\n@pol = (\"\");\n$i=0;\n$b=1;\n$j=0;\nfor ($i=0; $i<$n; $i++)\n{\n\tchomp($a = );\n\tif ($a =~ s/^cd (\\/?)/\\//)\n\t{\n\t\t@mas = split /[\\/]/, $a;\n\t\t$sla=$1;\n\t\tif ($sla=~/\\//)\n\t\t{\n\t\t\t@pol = (\"\");\n\t\t\t$b=1;\n#\t\t\tprint \"1\";\n\t\t\t$sla='';\n\t\t}\n\t\t$q = @mas;\n#\t\tprint \"0 - $mas[0], 1 - $mas[1], 2 - $mas[2]\";\n\t\tfor ($j=1; $j<$q; $j++)\n\t\t{\t\n\t\t\tif (($mas[$j] =~ /^..$/))\n\t\t\t{\n#\t\t\t\tprint \"2\\n\";\n\t\t\t\t$b-=1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\t\n#\t\t\t\tprint \"3\\n\";\n\t\t\t\t$pol[$b+1]= $mas[$j];\n\t\t\t\t$b++;\n\t\t\t\t\n\t\t\t}\n\t\t};\n\t\t\n\t}\n#\tprint @mas;\n\tif ($a =~ /^pwd/)\n\t{\t\n\t#\tprint \"/\";\n\t\tfor ($j = 1; $j<=$b; $j++)\n\t\t{\n\t\t\tprint \"$pol[$j]\\/\";\n\t\t}\n\t\tprint \"\\n\";\n\t}\n\t\n};\n\n\n"}, {"source_code": "<>;\ns!.* (/?)(.*)$/!($1?'':$p).\"$2/\"!e ? do{ 0 while s|//+|/|||s|\\w+/\\.+|| ; $p = $_} : print \"/$p$/\" for <>"}, {"source_code": "<>;\n$p = '/';\nchomp, s/^cd // ? do{ $p .= \"/$_\"; 0 while $p =~ s!//+!/!g || $p =~ s!\\w+/\\.\\.!! } : print $p.$/ for <>"}, {"source_code": "<>;\n$a=\"/\";\nwhile(<>){\n\tchomp;\nif (/^pwd/){print $a,\"\\n\"}\nelse{\ns/\\w+ +//,\n$a.=$_.\"/\";\nwhile ($a=~m|\\w+/+\\.\\.|){$a=~s|\\w+/+\\.\\.||,\n$a=~s|/+|/|\n}\n$a=~s|/+|/|g;\n}\n\n}"}, {"source_code": "<>;\n$p = '/';\nchomp, s/^cd // ? do{ $p .= \"/$_/\"; 0 while $p =~ s!//+!/!g || $p =~ s!\\w+/\\.\\.!! } : print $p.$/ for <>"}, {"source_code": "# http://codeforces.com/problemset/problem/158/C\n\n\n\nmy $n = ;\nchomp ( $n );\n\nmy @path;\n\nmy $f;\nmy @arr;\nmy $cd, $what;\nmy $str;\nfor ( my $i = 0; $i < $n ; $i += 1 ) {\n $f = ;\n chomp ( $f );\n\n if ( $f eq \"pwd\" ) {\t# print current path\n\tif ( @path ) {\t\t# \n\t $str = \"/\" . (join \"/\", @path) . \"/\\n\";\n\t if ( $str =~ s/\\A(\\/+)(.*)/\\/$2/ ) {}\n\t print $str;\n\t}\n\telse {\t\t\t# if path is empty ( at root )\n\t print \"/\\n\"; }\n }\n else {\n\t($cd, $what) = split m/ /, $f; # parse line of input \n\t@arr = split m/\\//, $what; # what == path\n\n\tif ( $what =~ m/\\A\\// ) {\t# absolute path\n\t @path = @arr;\t\t# \n\t}\n\telse {\t\t\t# relative path\n\t while ( @arr > 0 ) {\n\t\tif ( $arr[ 0 ] eq \"..\" ) {\n\t\t pop @path;\n\t\t shift @arr;\n\t\t}\n\t\telse {\n\t\t my $top = (shift @arr);\n\t\t push @path, $top;\n\t\t}}\n\t}\n }\n}\n\n\n"}, {"source_code": "# http://codeforces.com/problemset/problem/158/C\n\n\n\nmy $n = ;\nchomp ( $n );\n\nmy @path;\n\nmy $f;\nmy @arr;\nmy $cd, $what;\nmy $str;\nfor ( my $i = 0; $i < $n ; $i += 1 ) {\n $f = ;\n chomp ( $f );\n\n if ( $f eq \"pwd\" ) {\t# print current path\n\tif ( @path ) {\t\t# \n\t $str = \"/\" . (join \"/\", @path) . \"/\\n\";\n\t if ( $str =~ s/\\A(\\/+)(.*)/\\/$2/ ) {}\n\t print $str;\n\t}\n\telse {\t\t\t# if path is empty ( at root )\n\t print \"/\\n\"; }\n }\n else {\n\t($cd, $what) = split m/ /, $f; # parse line of input \n\t@arr = split m/\\//, $what; # what == path\n\n\twhile ( @arr > 0 ) {\n\t if ( $arr[ 0 ] eq \"..\" ) {\n\t\tpop @path;\n\t\tshift @arr;\n\t }\n\t else {\n\t\tmy $top = (shift @arr);\n\t\tpush @path, $top;\n\t }\n\t}\n }\n}\n\n\n"}, {"source_code": "# http://codeforces.com/problemset/problem/158/C\n\n\n\nmy $n = ;\nchomp ( $n );\n\nmy @path;\n\nmy $f;\nmy @arr;\nmy $cd, $what;\nmy $str;\nfor ( my $i = 0; $i < $n ; $i += 1 ) {\n $f = ;\n chomp ( $f );\n\n if ( $f eq \"pwd\" ) {\t# print current path\n\tif ( @path ) {\t\t# \n\t $str = \"/\" . (join \"/\", @path) . \"/\\n\";\n\t if ( $str =~ s/(\\/+)(.*)/\\/$2/ ) {}\n\t print $str;\n\t}\n\telse {\t\t\t# if path is empty ( at root )\n\t print \"/\\n\"; }\n }\n else {\n\t($cd, $what) = split m/ /, $f; # parse line of input \n\t@arr = split m/\\//, $what; # what == path\n\n\twhile ( @arr > 0 ) {\n\t if ( $arr[ 0 ] eq \"..\" ) {\n\t\tpop @path;\n\t\tshift @arr;\n\t }\n\t else {\n\t\tmy $top = (shift @arr);\n\t\tpush @path, $top;\n\t }\n\t}\n }\n}\n\n\n"}, {"source_code": "#!perl\nour $cwd = [];\nsub cd{\n\tmy($cd) = @_;\n\t@cd = $cd eq \"/\" ? (\"\") : split '/', $cd;\n\tfor (my $i = 0; $i <= $#cd; $i++) {\n\t\t$_ = $cd[$i];\n\t\tif ($i == 0 && $_ eq \"\") {\n\t\t\t$#$cwd = -1;\n\t\t} elsif ($_ eq \"..\") {\n\t\t\tpop @$cwd;\n\t\t} else {\n\t\t\tpush @$cwd, $_;\n\t\t}\n\t}\n}\nsub pwd{\n\tprint \"/\" . join(\"/\", @$cwd) . \"\\n\";\n}\n%sh = (\n\tcd => \\&cd,\n\tpwd => \\&pwd\n);\nmy $k = int ;\nwhile ($k--) {\n\t$_ = ;\n\ts/[\\r\\n]$//g;\n\tmy ($cmd, $prm, $h) = split / +/, $_, 2;\n\tnext if !$cmd;\n\tif (($h = $sh{$cmd})) {\n\t\t$h->($prm);\n\t} else {\n\t\tdie \"unknown command '$cmd'\\n\";\n\t}\n}\n"}, {"source_code": "#!perl\nour $cwd = [];\nsub cd{\n\tmy($cd) = @_;\n\t@cd = $cd eq \"/\" ? (\"\") : split '/', $cd;\n\tfor (my $i = 0; $i <= $#cd; $i++) {\n\t\t$_ = $cd[$i];\n\t\tif ($i == 0 && $_ eq \"\") {\n\t\t\t$#$cwd = -1;\n\t\t} elsif ($_ eq \"..\") {\n\t\t\tpop @$cwd;\n\t\t} else {\n\t\t\tpush @$cwd, $_;\n\t\t}\n\t}\n}\nsub pwd{\n\tprint \"/\" . join(\"/\", @$cwd) . \"/\\n\";\n}\n%sh = (\n\tcd => \\&cd,\n\tpwd => \\&pwd\n);\nmy $k = int ;\nwhile ($k--) {\n\t$_ = ;\n\ts/[\\r\\n]$//g;\n\tmy ($cmd, $prm, $h) = split / +/, $_, 2;\n\tnext if !$cmd;\n\tif (($h = $sh{$cmd})) {\n\t\t$h->($prm);\n\t} else {\n\t\tdie \"unknown command '$cmd'\\n\";\n\t}\n}\n"}, {"source_code": "chomp($n = );\nwhile ($n--)\n{\n chomp($_ = );\n push @commands, $_;\n}\n\nforeach (@commands)\n{\n if (/^pwd/)\n {\n print \"/\";\n print \"$_/\" foreach (@dir);\n print \"\\n\";\n }\n if (/^cd/)\n {\n s#^cd\\s+?/?##;\n @cd = split /\\//, $_;\n foreach (@cd)\n {\n pop @dir if /\\.\\./;\n push @dir, $_ unless /\\.\\./;\n }\n }\n}\n"}, {"source_code": "use strict;\nuse warnings;\nopen STDIN, 'output.txt';\n\nmy $n = <>;\nmy @curr = ();\nmy @tmp = ();\n\nsub print_ans {\n\tprint '/' . (join '/', @curr) . (0+@curr ? \"/\" : \"\"). \"\\n\";\n}\n\n\nsub get_curr {\n\t@curr = ();\n\t$_ eq '..' ? (pop @curr and next) : (push @curr, $_) for @tmp;\n}\n\nwhile (<>) {\n\tchomp;\n\tprint_ans and next if $_ =~ 'pwd';\n\t$_ =~s/cd\\s+//;\n\tif ($_ =~ '^/') {\n\t\t@tmp = split '/';\n\t\tshift @tmp;\n\t} else {\n\t\t@tmp = (@curr, split '/');\n\t}\n\tget_curr;\n}\n"}], "src_uid": "494ac937ba939db1dbc4081e518ab54c"} {"nl": {"description": "Momiji has got a rooted tree, consisting of n nodes. The tree nodes are numbered by integers from 1 to n. The root has number 1. Momiji decided to play a game on this tree.The game consists of several steps. On each step, Momiji chooses one of the remaining tree nodes (let's denote it by v) and removes all the subtree nodes with the root in node v from the tree. Node v gets deleted as well. The game finishes when the tree has no nodes left. In other words, the game finishes after the step that chooses the node number 1.Each time Momiji chooses a new node uniformly among all the remaining nodes. Your task is to find the expectation of the number of steps in the described game.", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of nodes in the tree. The next n\u2009-\u20091 lines contain the tree edges. The i-th line contains integers ai, bi (1\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u2009n;\u00a0ai\u2009\u2260\u2009bi) \u2014 the numbers of the nodes that are connected by the i-th edge. It is guaranteed that the given graph is a tree.", "output_spec": "Print a single real number \u2014 the expectation of the number of steps in the described game. The answer will be considered correct if the absolute or relative error doesn't exceed 10\u2009-\u20096.", "sample_inputs": ["2\n1 2", "3\n1 2\n1 3"], "sample_outputs": ["1.50000000000000000000", "2.00000000000000000000"], "notes": "NoteIn the first sample, there are two cases. One is directly remove the root and another is remove the root after one step. Thus the expected steps are: 1\u2009\u00d7\u2009(1\u2009/\u20092)\u2009+\u20092\u2009\u00d7\u2009(1\u2009/\u20092)\u2009=\u20091.5In the second sample, things get more complex. There are two cases that reduce to the first sample, and one case cleaned at once. Thus the expected steps are: 1\u2009\u00d7\u2009(1\u2009/\u20093)\u2009+\u2009(1\u2009+\u20091.5)\u2009\u00d7\u2009(2\u2009/\u20093)\u2009=\u2009(1\u2009/\u20093)\u2009+\u2009(5\u2009/\u20093)\u2009=\u20092"}, "positive_code": [{"source_code": "$n=<>;\nsub dfs {my $u=shift;$D[$u]=shift; for my $v (@{$g[$u]}) {&dfs($v,$D[$u]+1) unless defined $D[$v];}}\nwhile (<>) {chomp; ($u,$v)=split / /; push(@{$g[$u]},$v); push(@{$g[$v]},$u);}\n&dfs(1,0); $pr+=1.0/(1+$_) for (@D[1..$#D]); print \"$pr\\n\";\n\n"}, {"source_code": "$n=<>;\nsub dfs {my $u=shift;$D[$u]=shift; for my $v (@{$g[$u]}) {&dfs($v,$D[$u]+1) unless defined $D[$v];}}\nwhile (<>) {chomp; ($u,$v)=split / /; push(@{$g[$u]},$v); push(@{$g[$v]},$u);}\n&dfs(1,0); $pr+=1.0/(1+$_) for (@D[1..$#D]); print \"$pr\\n\";\n\n"}, {"source_code": "$n=<>;\nsub dfs {my $u=shift;$D[$u]=shift; for my $v (@{$g[$u]}) {&dfs($v,$D[$u]+1) unless defined $D[$v];}}\nwhile (<>) {chomp; ($u,$v)=split / /; push(@{$g[$u]},$v); push(@{$g[$v]},$u);}\n&dfs(1,0); $pr+=1.0/(1+$_) for (@D[1..$#D]); print \"$pr\\n\";\n"}, {"source_code": "$n=<>;\nsub dfs {my $u=shift;$D[$u]=shift; for my $v (@{$g[$u]}) {&dfs($v,$D[$u]+1) unless defined $D[$v];}}\nwhile (<>) {chomp; ($u,$v)=split / /; push(@{$g[$u]},$v); push(@{$g[$v]},$u);}\n&dfs(1,0); $pr+=1.0/(1+$_) for (@D[1..$#D]); print \"$pr\\n\";\n\n"}, {"source_code": "$n=<>;\nsub dfs {my $u=shift;$D[$u]=shift; for my $v (@{$g[$u]}) {&dfs($v,$D[$u]+1) unless defined $D[$v];}}\nwhile (<>) {chomp; ($u,$v)=split / /; push(@{$g[$u]},$v); push(@{$g[$v]},$u);}\n&dfs(1,0); $pr+=1.0/(1+$_) for (@D[1..$#D]); print \"$pr\\n\";\n\n"}, {"source_code": "$n=<>;\nsub dfs {my $u=shift;$D[$u]=shift; for my $v (@{$g[$u]}) {&dfs($v,$D[$u]+1) unless defined $D[$v];}}\nwhile (<>) {chomp; ($u,$v)=split / /; push(@{$g[$u]},$v); push(@{$g[$v]},$u);}\n&dfs(1,0); $pr+=1.0/(1+$_) for (@D[1..$#D]); print \"$pr\\n\";\n\n"}, {"source_code": "$n=<>;\nsub dfs {my $u=shift;$D[$u]=shift; for my $v (@{$g[$u]}) {&dfs($v,$D[$u]+1) unless defined $D[$v];}}\nwhile (<>) {chomp; ($u,$v)=split / /; push(@{$g[$u]},$v); push(@{$g[$v]},$u);}\n&dfs(1,0); $pr+=1.0/(1+$_) for (@D[1..$#D]); print \"$pr\\n\";\n\n"}, {"source_code": "$n=<>;\nsub dfs {my $u=shift;$D[$u]=shift; for my $v (@{$g[$u]}) {&dfs($v,$D[$u]+1) unless defined $D[$v];}}\nwhile (<>) {chomp; ($u,$v)=split / /; push(@{$g[$u]},$v); push(@{$g[$v]},$u);}\n&dfs(1,0); $pr+=1.0/(1+$_) for (@D[1..$#D]); print \"$pr\\n\";\n\n"}, {"source_code": "$n=<>;\nsub dfs {my $u=shift;$D[$u]=shift; for my $v (@{$g[$u]}) {&dfs($v,$D[$u]+1) unless defined $D[$v];}}\nwhile (<>) {chomp; ($u,$v)=split / /; push(@{$g[$u]},$v); push(@{$g[$v]},$u);}\n&dfs(1,0); $pr+=1.0/(1+$_) for (@D[1..$#D]); print \"$pr\\n\";\n\n"}], "negative_code": [], "src_uid": "85b78251160db9d7ca1786e90e5d6f21"} {"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$$$) \u2014 the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \\le n \\le 200$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tnext if $_[ $i ] =~ /_/;\n\t\t\n\t\tmy $j = $i;\n\t\tmy @chain = $j;\n\t\t\n\t\tmy $cnt = 1;\n\t\t\n\t\twhile( $_[ $j ] != $i + 1 ){\n\t\t\t$cnt ++;\n\t\t\t$j = $_[ $j ] - 1;\n\t\t\tpush @chain, $j;\n\t\t\t}\n\t\t\n\t\tfor( @chain ){\n\t\t\t$_[ $_ - 0 ] = \"_$cnt\";\n\t\t\t}\n\t\t\n\t\t$debug and print \" [@_]\";\n\t\t}\n\t\n\ts/_// for @_;\n\t\n\tprint \"@_\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tnext if $_[ $i ] =~ /_/;\n\t\t\n\t\tmy $j = $i;\n\t\tmy @chain = $j;\n\t\t\n\t\tmy $cnt = 1;\n\t\t\n\t\twhile( $_[ $j ] != $i + 1 ){\n\t\t\t$cnt ++;\n\t\t\t$j = $_[ $j ] - 1;\n\t\t\tpush @chain, $j;\n\t\t\t}\n\t\t\n\t\tfor( @chain ){\n\t\t\t$_[ $_ - 0 ] = \"_$cnt\";\n\t\t\t}\n\t\t\n\t\t$debug and print \" [@_]\";\n\t\t}\n\t\n\ts/_// for @_;\n\t\n\tprint \"@_\";\n\t}"}], "negative_code": [], "src_uid": "345e76bf67ae4342e850ab248211eb0b"} {"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$$$)\u00a0\u2014 the number of testcases. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 8$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\nwhile(<>){map ~~<>, 1 .. reverse;print m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\"}"}, {"source_code": "<>;\r\n\r\nwhile(<>){\r\n\tmap ~~<>, 1 .. reverse;\r\n\t\r\n\tprint m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\";\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\tmap ~~<>, 1 .. $m;\n\t\n\tprint $n == $m ? \"NO\" : \"YES\";\n\t}"}, {"source_code": "<>;\r\nwhile(<>){map ~~<>, 1 .. reverse;print m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\"}"}, {"source_code": "<>;\r\nwhile(<>){map ~~<>, 1 .. reverse;print m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\"}"}, {"source_code": "<>;\r\nwhile(<>){map ~~<>, 1 .. reverse;print m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\"}"}, {"source_code": "<>;\r\nwhile(<>){map ~~<>, 1 .. reverse;print m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\"}"}, {"source_code": "<>;\r\nwhile(<>){map ~~<>, 1 .. reverse;print m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\"}"}, {"source_code": "<>;\r\nwhile(<>){map ~~<>, 1 .. reverse;print m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\"}"}, {"source_code": "<>;\r\nwhile(<>){map ~~<>, 1 .. reverse;print m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\"}"}, {"source_code": "<>;\r\nwhile(<>){map ~~<>, 1 .. reverse;print m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\"}"}, {"source_code": "<>;\r\nwhile(<>){map ~~<>, 1 .. reverse;print m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\"}"}, {"source_code": "<>;\r\n \r\nwhile(<>){\r\n\tmap ~~<>, 1 .. reverse;\r\n\t\r\n\tprint m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\";\r\n\t}"}, {"source_code": "<>;\r\nwhile(<>){map ~~<>, 1 .. reverse;print m/(.) \\1/ ? \"NO\\n\" : \"YES\\n\"}"}], "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)$$$ \u2014 the number of testcases. The first and only line of each testcase contains a single integer $$$x$$$ $$$(1 \\leq x \\leq 10^9)$$$ \u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($x) = map { $_ - 0 } split(/\\s+/,);\r\n my $p = $x % 11;\r\n my $q = $p * 111;\r\n if( $x >= $q ){\r\n print \"YES\\n\";\r\n } else {\r\n print \"NO\\n\";\r\n }\r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [], "src_uid": "e5e937f080b20eaec5f12f1784ae6427"} {"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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($tc) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\nwhile( $tc -- > 0 ){\r\n my ($p) = map { $_ - 0 } split(/\\s+/o,);\r\n $p--;\r\n print \"2 $p\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($tcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $tcase -- > 0 ){\r\n my ($c,$d) = map { $_ - 0 } split(/\\s+/,);\r\n if( $c==0 and $d == 0 ){\r\n print \"0\\n\"; next;\r\n }\r\n if( $c == $d ){\r\n print \"1\\n\"; next;\r\n }\r\n if( $c % 2 == $d % 2 ){\r\n print \"2\\n\"; next;\r\n }\r\n print \"-1\\n\";\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "src_uid": "8e7c0b703155dd9b90cda706d22525c9"} {"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 \u2014 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\" \u2013 \"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": "#! perl -p\ns/$/./ unless /\\./;\ns/$/00/;\ns/\\...\\K.*//;\n1 while s/([0-9])([0-9][0-9][0-9])($|\\.|,)/$1,$2$3/;\ns/-(.*)/($1)/;\ns/([0-9])/\\$$1/;\n\n"}, {"source_code": "#! perl -p\ns/$/./ unless /\\./;\ns/$/00/;\ns/\\...\\K.*//;\n1 while s/([0-9])([0-9][0-9][0-9])($|\\.|,)/$1,$2$3/;\ns/-(.*)/($1)/;\ns/([0-9])/\\$$1/;\n\n"}, {"source_code": "#! perl -p\ns/$/./ unless /\\./;\ns/$/00/;\ns/\\...\\K.*//;\n1 while s/([0-9])([0-9][0-9][0-9])($|\\.|,)/$1,$2$3/;\ns/-(.*)/($1)/;\ns/([0-9])/\\$$1/;\n"}, {"source_code": "#! perl -p\ns/$/./ unless /\\./;\ns/$/00/;\ns/\\...\\K.*//;\n1 while s/([0-9])([0-9][0-9][0-9])($|\\.|,)/$1,$2$3/;\ns/-(.*)/($1)/;\ns/([0-9])/\\$$1/;\n\n"}, {"source_code": "#! perl -p\ns/$/./ unless /\\./;\ns/$/00/;\ns/\\...\\K.*//;\n1 while s/([0-9])([0-9][0-9][0-9])($|\\.|,)/$1,$2$3/;\ns/-(.*)/($1)/;\ns/([0-9])/\\$$1/;\n\n"}, {"source_code": "#! perl -p\ns/$/./ unless /\\./;\ns/$/00/;\ns/\\...\\K.*//;\n1 while s/([0-9])([0-9][0-9][0-9])($|\\.|,)/$1,$2$3/;\ns/-(.*)/($1)/;\ns/([0-9])/\\$$1/;"}, {"source_code": "#! perl -p\ns/$/./ unless /\\./;\ns/$/00/;\ns/\\...\\K.*//;\n1 while s/([0-9])([0-9][0-9][0-9])($|\\.|,)/$1,$2$3/;\ns/-(.*)/($1)/;\ns/([0-9])/\\$$1/;\n"}, {"source_code": "#! perl -p\ns/$/./ unless /\\./;\ns/$/00/;\ns/\\...\\K.*//;\n1 while s/([0-9])([0-9][0-9][0-9])($|\\.|,)/$1,$2$3/;\ns/-(.*)/($1)/;\ns/([0-9])/\\$$1/;\n\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nwhile () {\n chomp;\n s/(.*)/($1)/ if s/^-//;\n s/^(\\(*)/$1\\$/;\n s/(\\d+)(\\D*)/$1.$2/ unless m/\\./;\n s/\\.(\\d*)/\\.${1}00/;\n s/(\\.\\d{2})\\d*/$1/;\n 0 while s/(\\d)(\\d{3}[\\.,])/$1,$2/;\n s/(.*)/$1\\n/;\n print;\n}\n"}, {"source_code": "#! perl -p\ns/$/./ unless /\\./;\ns/$/00/;\ns/\\...\\K.*//;\n1 while s/([0-9])([0-9][0-9][0-9])($|\\.|,)/$1,$2$3/;\ns/-(.*)/($1)/;\ns/([0-9])/\\$$1/;\n\n"}, {"source_code": "#! perl -p\ns/$/./ unless /\\./;\ns/$/00/;\ns/\\...\\K.*//;\n1 while s/([0-9])([0-9][0-9][0-9])($|\\.|,)/$1,$2$3/;\ns/-(.*)/($1)/;\ns/([0-9])/\\$$1/;\n\n"}, {"source_code": "$_=<>;chomp;\ns/(\\d+)(\\.\\d+)?/\"$1\".($2?(length$2<3?(\"${2}0\"):(substr $2,0,3)):(\".00\"))/e;\ns/(\\d)(\\d{3}[.,])/$1,$2/ while /\\d{4}[.,]/;\ns/-// and s/^/(/,s/$/)/;\ns/\\d/'$'.$&/e;\nprint;"}, {"source_code": "#! perl -p\ns/$/./ unless /\\./;\ns/$/00/;\ns/\\...\\K.*//;\n1 while s/([0-9])([0-9][0-9][0-9])($|\\.|,)/$1,$2$3/;\ns/-(.*)/($1)/;\ns/([0-9])/\\$$1/;\n\n"}], "negative_code": [{"source_code": "#! perl -p\n1 while s/([0-9])([0-9][0-9][0-9])($|\\.|,)/$1,$2$3/;\ns/$/./ unless /\\./;\ns/$/00/;\ns/\\...\\K.*//;\ns/-(.*)/($1)/;\ns/([0-9])/\\$$1/;\n"}, {"source_code": "$_=<>;chomp;\ns/(\\d+)(\\.\\d+)?/\"$1\".($2?(length$2<3?(\"${2}0\"):(substr $2,0,3)):(\"\"))/e;\ns/(\\d)(\\d{3}[.,])/$1,$2/ while /\\d{4}[.,]/;\ns/-// and s/^/(/,s/$/)/;\nprint;"}], "src_uid": "c704c5fb9e054fab1caeab534849901d"} {"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\u2009+\u20091.A simple cycle of length d (d\u2009>\u20091) in graph G is a sequence of distinct graph nodes v1,\u2009v2,\u2009...,\u2009vd such, that nodes v1 and vd are connected by an edge of the graph, also for any integer i (1\u2009\u2264\u2009i\u2009<\u2009d) nodes vi and vi\u2009+\u20091 are connected by an edge of the graph.", "input_spec": "The first line contains three integers n, m, k (3\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009105;\u00a02\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009-\u20091) \u2014 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\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u2009n;\u00a0ai\u2009\u2260\u2009bi) \u2014 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\u2009\u2265\u2009k\u2009+\u20091) \u2014 the length of the found cycle. In the next line print r distinct integers v1,\u2009v2,\u2009...,\u2009vr (1\u2009\u2264\u2009vi\u2009\u2264\u2009n) \u2014 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": "#!/usr/bin/perl -w\n\nuse strict;\nuse List::Util qw(min);\n\nmy ($n, $m, $k);\nmy @e = ();\nmy @first = ();\nmy @seq = ();\n\nsub dfs {\n my ($u, $total) = @_;\n $first[$u] = $total++;\n push @seq, $u;\n\n my $can_go = 0;\n for my $v (@{$e[$u]}) {\n unless (defined $first[$v]) {\n $can_go = 1;\n dfs($v, $total);\n }\n }\n unless ($can_go) {\n my $min = min map { $first[$_] } @{$e[$u]};\n @seq = map { $_ + 1 } @seq[$min .. $#seq];\n print scalar @seq, \"\\n@seq\\n\";\n exit 0;\n }\n}\n\nchomp($_ = <>);\n($n, $m, $k) = split;\npush @e, [] for (1 .. $n);\n$#first = $n + 1;\nfor (1 .. $m) {\n chomp($_ = <>);\n my ($u, $v) = split;\n ($u, $v) = ($u - 1, $v - 1);\n push @{$e[$u]}, $v;\n push @{$e[$v]}, $u;\n}\n\ndfs(0, 0);\n"}], "negative_code": [], "src_uid": "250c0e647d0f2ff6d86db01675192c9f"} {"nl": {"description": "Someone gave Alyona an array containing n positive integers a1,\u2009a2,\u2009...,\u2009an. 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,\u2009b2,\u2009...,\u2009bn such that 1\u2009\u2264\u2009bi\u2009\u2264\u2009ai for every 1\u2009\u2264\u2009i\u2009\u2264\u2009n. 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of elements in the Alyona's array. The second line of the input contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109)\u00a0\u2014 the elements of the array.", "output_spec": "Print one positive integer\u00a0\u2014 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": "sub numbers { $a <=> $b };\n$n = <>; @a = split(' ', <>); @a = sort numbers @a;\nfor $a (@a) {\n\t$a > $c || next;\n\t$c++;\n}\nprint ++$c, \"\\n\""}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = sort {$a <=> $b} split ' ', <>;\n\n\tfor (1 .. 1e5 + 1){\n\t\t$i = $_;\n\t\t@_ or last;\n\t\t$f = shift @_;\n\t\t$_ > $f and redo;\n\t\t}\n\t\n\tprint $i\n\t}"}], "negative_code": [{"source_code": "sub numbers { $a <=> $b };\n$n = <>; @a = split(' ', <>); @a = sort(numbers, @a);\nfor $a (@a) {\n\t$a > $c || next;\n\t$c++;\n}\nprint ++$c, \"\\n\""}], "src_uid": "482b3ebbbadd583301f047181c988114"} {"nl": {"description": "You are given two arrays: an array $$$a$$$ consisting of $$$n$$$ zeros and an array $$$b$$$ consisting of $$$n$$$ integers.You can apply the following operation to the array $$$a$$$ an arbitrary number of times: choose some subsegment of $$$a$$$ of length $$$k$$$ and add the arithmetic progression $$$1, 2, \\ldots, k$$$ to this subsegment \u2014 i.\u2009e. add $$$1$$$ to the first element of the subsegment, $$$2$$$ to the second element, and so on. The chosen subsegment should be inside the borders of the array $$$a$$$ (i.e., if the left border of the chosen subsegment is $$$l$$$, then the condition $$$1 \\le l \\le l + k - 1 \\le n$$$ should be satisfied). Note that the progression added is always $$$1, 2, \\ldots, k$$$ but not the $$$k, k - 1, \\ldots, 1$$$ or anything else (i.e., the leftmost element of the subsegment always increases by $$$1$$$, the second element always increases by $$$2$$$ and so on).Your task is to find the minimum possible number of operations required to satisfy the condition $$$a_i \\ge b_i$$$ for each $$$i$$$ from $$$1$$$ to $$$n$$$. Note that the condition $$$a_i \\ge b_i$$$ should be satisfied for all elements at once.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 3 \\cdot 10^5$$$) \u2014 the number of elements in both arrays and the length of the subsegment, respectively. The second line of the input contains $$$n$$$ integers $$$b_1, b_2, \\ldots, b_n$$$ ($$$1 \\le b_i \\le 10^{12}$$$), where $$$b_i$$$ is the $$$i$$$-th element of the array $$$b$$$.", "output_spec": "Print one integer \u2014 the minimum possible number of operations required to satisfy the condition $$$a_i \\ge b_i$$$ for each $$$i$$$ from $$$1$$$ to $$$n$$$.", "sample_inputs": ["3 3\n5 4 6", "6 3\n1 2 3 2 2 3", "6 3\n1 2 4 1 2 3", "7 3\n50 17 81 25 42 39 96"], "sample_outputs": ["5", "3", "3", "92"], "notes": "NoteConsider the first example. In this test, we don't really have any choice, so we need to add at least five progressions to make the first element equals $$$5$$$. The array $$$a$$$ becomes $$$[5, 10, 15]$$$.Consider the second example. In this test, let's add one progression on the segment $$$[1; 3]$$$ and two progressions on the segment $$$[4; 6]$$$. Then, the array $$$a$$$ becomes $$$[1, 2, 3, 2, 4, 6]$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\n\r\nmy ($n,$k) = map { $_ - 0 } split(/\\s+/,);\r\nmy @B = map { $_ - 0 } split(/\\s+/,);\r\n\r\nmy @rb = reverse(@B);\r\n\r\nmy $ny = 0;\r\nmy $sy = 0;\r\nmy $ssy = 0;\r\n\r\nmy @y = ((0) x ($n));\r\n\r\nmy $rmx = 0;\r\nfor(my $i=0;$i<$n;$i++){\r\n if( $i <= $n-$k ){\r\n my $bunshi = $rb[$i] - $ssy;\r\n $bunshi = 0 if $bunshi<0;\r\n my $v1 = ( $bunshi + ($k-1) )/$k;\r\n $ssy -= $sy;\r\n $ssy += ($k-1) * $v1;\r\n $sy += $v1;\r\n $y[$i] = $v1;\r\n $sy -= $y[$i-$k+1] if $i - $k + 1 >=0;\r\n } else {\r\n my $bunshi = $rb[$i] - $ssy;\r\n $bunshi = 0 if $bunshi<0;\r\n my $bo = $n - $i;\r\n my $v2 = ($bunshi + ($bo-1))/$bo;\r\n $rmx = max($rmx,$v2);\r\n my $v1 = 0;\r\n $ssy -= $sy;\r\n $ssy += ($k-1) * $v1;\r\n $sy += $v1;\r\n $y[$i] = $v1;\r\n $sy -= $y[$i-$k+1] if $i - $k +1>=0;\r\n }\r\n}\r\n\r\nmy $res = $rmx;\r\n\r\n#print STDERR ( join(':',@y) . \",rmx=$rmx\\n\" );\r\n\r\nfor(my $i=0;$i<$n;$i++){\r\n $res += $y[$i];\r\n}\r\nprint \"$res\\n\";\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\n\r\nmy ($n,$k) = map { $_ - 0 } split(/\\s+/,);\r\nmy @B = map { $_ - 0 } split(/\\s+/,);\r\n\r\nmy @rb = reverse(@B);\r\n\r\nmy $ny = 0;\r\nmy $sy = 0;\r\nmy $ssy = 0;\r\n\r\nmy @y = ((0) x ($n));\r\n\r\nmy $rmx = 0;\r\nfor(my $i=0;$i<$n;$i++){\r\n if( $i <= $n-$k ){\r\n my $bunshi = $rb[$i] - $ssy;\r\n $bunshi = 0 if $bunshi<0;\r\n my $v1 = ( $bunshi + ($k-1) )/$k;\r\n $ssy -= $sy;\r\n $ssy += ($k-1) * $v1;\r\n $sy += $v1;\r\n $sy -= $y[$i-$k+1] if $i - $k + 1 >=0;\r\n $y[$i] = $v1;\r\n } else {\r\n my $bunshi = $rb[$i] - $ssy;\r\n $bunshi = 0 if $bunshi<0;\r\n my $bo = $n - $i;\r\n my $v2 = ($bunshi + ($bo-1))/$bo;\r\n $rmx = max($rmx,$v2);\r\n my $v1 = 0;\r\n $ssy -= $sy;\r\n $ssy += ($k-1) * $v1;\r\n $sy += $v1;\r\n $sy -= $y[$i-$k+1] if $i - $k +1>=0;\r\n $y[$i] = $v1;\r\n }\r\n}\r\n\r\nmy $res = $rmx;\r\n\r\n#print STDERR ( join(':',@y) . \",rmx=$rmx\\n\" );\r\n\r\nfor(my $i=0;$i<$n;$i++){\r\n $res += $y[$i];\r\n}\r\nprint \"$res\\n\";\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy $inf = 1000000000000000000;\r\n\r\nmy ($n,$k) = map { $_ - 0 } split(/\\s+/,);\r\nmy @B = map { $_ - 0 } split(/\\s+/,);\r\nmy @A = ( (0) x $n );\r\n\r\nmy @rb = reverse(@B);\r\n\r\nmy $ny = 0;\r\nmy $sy = 0;\r\nmy $ssy = 0;\r\n\r\nmy @y = ((0) x ($n));\r\n\r\nmy $rmx = 0;\r\nfor(my $i=0;$i<$n;$i++){\r\n if( $i <= $n-$k ){\r\n my $bunshi = $rb[$i] - $ssy;\r\n $bunshi = 0 if $bunshi<0;\r\n my $v1 = ( $bunshi + ($k-1) )/$k;\r\n $ssy -= $sy;\r\n $ssy += ($k-1) * $v1;\r\n $sy += $v1;\r\n $sy -= $y[$i-$k+1] if $i - $k + 1 >=0;\r\n $y[$i] = $v1;\r\n } else {\r\n my $bunshi = $rb[$i] - $ssy;\r\n $bunshi = 0 if $bunshi<0;\r\n my $bo = $n - $i;\r\n my $v2 = ($bunshi + ($bo-1))/$bo;\r\n $rmx = max($rmx,$v2);\r\n my $v1 = 0;\r\n $ssy -= $sy;\r\n $ssy += ($k-1) * $v1;\r\n $sy += $v1;\r\n $sy -= $y[$i-$k] if $i - $k >=0;\r\n $y[$i] = $v1;\r\n }\r\n}\r\n\r\nmy $res = $rmx;\r\n\r\n#print STDERR ( join(':',@y) . \",rmx=$rmx\\n\" );\r\n\r\nfor(my $i=0;$i<$n;$i++){\r\n $res += $y[$i];\r\n}\r\nprint \"$res\\n\";\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "src_uid": "f5f3b9c93060f231a55b941a7fa804e1"} {"nl": {"description": "International Women's Day is coming soon! Polycarp is preparing for the holiday.There are $$$n$$$ candy boxes in the shop for sale. The $$$i$$$-th box contains $$$d_i$$$ candies.Polycarp wants to prepare the maximum number of gifts for $$$k$$$ girls. Each gift will consist of exactly two boxes. The girls should be able to share each gift equally, so the total amount of candies in a gift (in a pair of boxes) should be divisible by $$$k$$$. In other words, two boxes $$$i$$$ and $$$j$$$ ($$$i \\ne j$$$) can be combined as a gift if $$$d_i + d_j$$$ is divisible by $$$k$$$.How many boxes will Polycarp be able to give? Of course, each box can be a part of no more than one gift. Polycarp cannot use boxes \"partially\" or redistribute candies between them. ", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2 \\cdot 10^5, 1 \\le k \\le 100$$$) \u2014 the number the boxes and the number the girls. The second line of the input contains $$$n$$$ integers $$$d_1, d_2, \\dots, d_n$$$ ($$$1 \\le d_i \\le 10^9$$$), where $$$d_i$$$ is the number of candies in the $$$i$$$-th box.", "output_spec": "Print one integer \u2014 the maximum number of the boxes Polycarp can give as gifts.", "sample_inputs": ["7 2\n1 2 2 3 2 4 10", "8 2\n1 2 2 3 2 4 6 10", "7 3\n1 2 2 3 2 4 5"], "sample_outputs": ["6", "8", "4"], "notes": "NoteIn the first example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $$$(2, 3)$$$; $$$(5, 6)$$$; $$$(1, 4)$$$. So the answer is $$$6$$$.In the second example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $$$(6, 8)$$$; $$$(2, 3)$$$; $$$(1, 4)$$$; $$$(5, 7)$$$. So the answer is $$$8$$$.In the third example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $$$(1, 2)$$$; $$$(6, 7)$$$. So the answer is $$$4$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\tmy( $n, $k ) = split;\n\tmy @d = split ' ', <>;\n\t\n\t$debug and print \"@d, $k\";\n\t\n\tmy %h;\n\t\n\tmap { $h{ $_ % $k } ++ } @d;\n\t\n\tmy $cnt = 0;\n\t\n\t$cnt += int $h{ 0 } / 2;\n\t\n\t$debug and print $cnt;\n\t\n\t$cnt += int $h{ $k / 2 } / 2 if $k % 2 == 0;\n\t\n\t$debug and print $cnt;\n\t\n\tfor my $rem ( 1 .. ( $k - 1 ) / 2 ){\n\t\t\n\t\tmy( $min, $max ) = sort { $a <=> $b } $h{ $rem }, $h{ $k - $rem };\n\t\t\n\t\t$cnt += $min;\n\t\t}\t\n\t\n\t$debug and print $cnt;\n\t\n\tprint $cnt * 2;\n\t}"}], "negative_code": [], "src_uid": "3f3a013cedaaf8cbee0a74a4ed50f09d"} {"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$$$) \u00a0\u2014 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$$$) \u00a0\u2014 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)\u00a0\u2014 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 = <>;\nwhile ($t--) {\n <>;\n @a = split(\" \", <>);\n print(($a[0] < $a[-1]) ? \"YES\" : \"NO\");\n print \"\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tif( $_[ 0 ] < $_[ @_ - 1 ] ){\n\t\tprint \"YES\";\n\t\t}\n\telse{\n\t\tprint \"NO\";\n\t\t}\n\t\n\t\n\t}"}], "negative_code": [{"source_code": "$t = <>;\nwhile ($t--) {\n @a = split(\" \", <>);\n print(($a[0] < $a[-1]) ? \"YES\" : \"NO\");\n print \"\\n\";\n}"}], "src_uid": "192f181cfc74bef5b0b5a192964d9760"} {"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$$$)\u00a0\u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m, $row_bot, $col_bot, $row_dot, $col_dot ) = split;\n\t\n\tmy @cand;\n\t\n\tpush @cand, $row_dot - $row_bot, $n * 2 - $row_dot - $row_bot;\n\tpush @cand, $col_dot - $col_bot, $m * 2 - $col_dot - $col_bot;\n\t\n\t@cand = grep $_ >= 0, @cand;\n\t\n\tprint +( sort { $a <=> $b } @cand )[ 0 ];\n\t}"}], "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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the elements of the array.", "output_spec": "Print a single integer\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\tmy $max = ( sort { $b <=> $a } values %h )[ 0 ];\n\t\n\tprint $_ - $max;\n\t}"}, {"source_code": "$_ = <>;\n\nmap $h{ $_ } ++, split ' ', <>;\n\t\nprint $_ - ( sort { $b <=> $a } values %h )[ 0 ]"}], "negative_code": [{"source_code": "<>;\n\nmap $h{ $_ } ++, split ' ', <>;\n\t\nprint $_ - ( sort { $b <=> $a } values %h )[ 0 ]"}], "src_uid": "eaa768dc1024df6449e89773234cc0c3"} {"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\u00a0\u2014 the best programmer.Help her find the answers!", "input_spec": "The first line contains a single integer $$$q$$$ ($$$1 \\le q \\le 10^3$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the descriptions of the queries.", "output_spec": "Print $$$q$$$ lines, each containing one number\u00a0\u2014 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": "use strict;\nuse warnings;\nuse integer;\n\n<>;\nwhile (<>) {\n\t/(\\d+)\\s+(\\d+)/;\n\tmy $r = sumbetween($1, $2);\n\tprint $r, \"\\n\";\n}\n\nsub sumbetween {\n\tmy ($beg, $end) = @_;\n\tmy $all = mount($end);\n\tmy $before = mount($beg - 1);\n\treturn $all - $before;\n}\n\nsub mount {\n\tmy $i = shift(@_);\n\tmy $sign = $i % 2 == 0 ? +1 : -1;\n\t# print \"(\", $sign * upperdiv($i, 2), \")\";\n\treturn $sign * upperdiv($i, 2);\n}\n\nsub upperdiv {\n\tmy ($a, $b) = @_;\n\tmy $r = $a / $b;\n\t$r++ if $a % $b > 0;\n\treturn $r;\n}\n"}], "negative_code": [], "src_uid": "7eae40835f6e9580b985d636d5730e2d"} {"nl": {"description": "Sherlock Holmes and Dr. Watson played some game on a checkered board n\u2009\u00d7\u2009n in size. During the game they put numbers on the board's squares by some tricky rules we don't know. However, the game is now over and each square of the board contains exactly one number. To understand who has won, they need to count the number of winning squares. To determine if the particular square is winning you should do the following. Calculate the sum of all numbers on the squares that share this column (including the given square) and separately calculate the sum of all numbers on the squares that share this row (including the given square). A square is considered winning if the sum of the column numbers is strictly greater than the sum of the row numbers.For instance, lets game was ended like is shown in the picture. Then the purple cell is winning, because the sum of its column numbers equals 8\u2009+\u20093\u2009+\u20096\u2009+\u20097\u2009=\u200924, sum of its row numbers equals 9\u2009+\u20095\u2009+\u20093\u2009+\u20092\u2009=\u200919, and 24\u2009>\u200919.", "input_spec": "The first line contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u200930). Each of the following n lines contain n space-separated integers. The j-th number on the i-th line represents the number on the square that belongs to the j-th column and the i-th row on the board. All number on the board are integers from 1 to 100.", "output_spec": "Print the single number \u2014 the number of the winning squares.", "sample_inputs": ["1\n1", "2\n1 2\n3 4", "4\n5 7 8 4\n9 5 3 2\n1 6 6 4\n9 5 7 3"], "sample_outputs": ["0", "2", "6"], "notes": "NoteIn the first example two upper squares are winning.In the third example three left squares in the both middle rows are winning:5 7 8 49 5 3 21 6 6 49 5 7 3"}, "positive_code": [{"source_code": "while (<>){\n chomp;\n @_=();\n for (1..$_){\n push @_,\"\".<>;\n \n }\n chomp @_;\n # print \"@_\\n\";\n @n=();\n for (@_){\n \n @m=split/ /;\n $sum=0;\n for $i(@m){$sum+=$i}\n push @n, $sum;\n \n }\n @m=@_;\n for $i(1..@m){\n @k=();\n for (@m){\n s/(\\d+) ?//;\n push @k,$1 \n }\n $a=join\" \",@k;\n push @K, $a;\n }\n \n for (@K){\n \n @m=split/ /;\n $sum=0;\n for $i(@m){$sum+=$i}\n push @N, $sum;\n \n }\n \n $z=0;\n for $i(@n){\n for $j(@N){\n $j>$i and $z++\n \n \n }\n \n \n }\n print \"$z\\n\"\n }\n \n "}, {"source_code": "while (<>){\n @_=();\n for (1..$_){push @_,\"\".<>}\n chomp @_;\n @n=();\n \n sub suma{\n @A=@_;\n @SUM=();\n for (@A){\n @m=split/ /;\n $sum=0;\n for $i(@m){$sum+=$i}\n push @SUM, $sum;\n }\n return @SUM\n }\n \n @i=suma (@_);\n \n @m=@_;\n @_=();\n for $i(1..@m){\n @k=();\n for (@m){\n s/(\\d+) ?//;\n push @k,$1 \n }\n push @_, (join\" \",@k);\n }\n \n @j=suma (@_);\n \n $z=0;\n for $i(@i){\n for $j(@j){\n $j>$i and $z++\n }\n }\n print \"$z\\n\"\n }\n \n "}], "negative_code": [], "src_uid": "6e7c2c0d7d4ce952c53285eb827da21d"} {"nl": {"description": "Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\leq n \\leq 200\\,000$$$, $$$0 \\leq k \\leq n$$$) \u2014 the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.", "output_spec": "Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.", "sample_inputs": ["5 3\n51528", "3 2\n102", "1 1\n1"], "sample_outputs": ["10028", "100", "0"], "notes": "NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes."}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n \nmy ($n, $k) = split q{ }, $line;\n \nchomp (my $s = );\n \nif ( $k == 0 ) {\n say $s;\n exit(0);\n}\n\nif ( $n == 1 ) {\n if ( $k > 0 ) {\n say 0;\n }\n else {\n say $s;\n }\n exit(0);\n}\n\nmy @srr = split q{}, $s;\nif ( $srr[0] != 1 ) {\n $srr[0] = 1;\n $k--;\n}\nmy $i = 1;\nwhile ( $k && $i < $n ) {\n if ( $srr[$i] != 0 ) {\n $srr[$i] = 0;\n $k--;\n }\n $i++;\n}\nsay @srr;\n"}, {"source_code": "( $n, $k, $_ ) = split ' ', <> . <>;\n\n$k and $k -= s/^.$/0/;\n$k and $k -= s/^[2-9]/1/;\n\nprint s/\n\t(?(?{ ! $k }) (?!) )\n\t\\B[1-9]\n\t(?{ $k -- })\n\t/0/gexr"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\t$k and s/^.$/0/ and $k --;\n\t$k and s/^[2-9]/1/ and $k --;\n\t\n\ts/\n\t\t(?(?{ ! $k }) (*FAIL) )\n\t\t\\B[^0]\n\t\t(?{ $k -- })\n\t\t/\n\t\t0\n\t\t/gex;\n\t\t\n\tprint;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n \nmy ($n, $k) = split q{ }, $line;\n \nchomp (my $s = );\n \nif ( $k == 0 ) {\n say $s;\n exit(0);\n}\n\nif ( $n == 1 ) {\n if ( $k > 0 ) {\n say 0;\n }\n else {\n say $s;\n }\n exit(0);\n}\n\nmy @srr = split q{}, $s;\nif ( $srr[0] != 1 ) {\n $srr[0] = 1;\n $k--;\n}\nmy $i = 1;\nwhile ( $k && $i < $n ) {\n if ( $srr[$i] != '0' ) {\n $srr[$i] = 0;\n }\n $i++;\n $k--;\n}\nsay @srr;\n"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nchomp (my $line = );\n\nmy ($n, $k) = split q{ }, $line;\n\nchomp (my $s = );\n\nif ( $n == 1 and $k == 1 ) {\n say 0;\n}\nelsif ( $k == 0 ) {\n say $s;\n}\nelse {\n my @srr = split q{}, $s;\n if ( $srr[0] != 1 ) {\n $srr[0] = 1;\n $k--;\n }\n my $i = 1;\n while ( $k-- ) {\n $srr[$i++] = 0;\n }\n say @srr;\n}\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n \nmy ($n, $k) = split q{ }, $line;\n \nchomp (my $s = );\n \nif ( $k == 0 ) {\n say $s;\n exit(0);\n}\n\nif ( $n == 1 and $k >= 1 ) {\n say 0;\n exit(0);\n}\n\nelse {\n my @srr = split q{}, $s;\n if ( $srr[0] != 1 ) {\n $srr[0] = 1;\n $k--;\n }\n my $i = 1;\n while ( $k-- && $i < $n ) {\n $srr[$i++] = 0;\n }\n say @srr;\n}\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n \nmy ($n, $k) = split q{ }, $line;\n \nchomp (my $s = );\n \nif ( $n == 1 and $k == 1 ) {\n say 0;\n}\nelsif ( $k == 0 ) {\n say $s;\n}\nelse {\n my @srr = split q{}, $s;\n if ( $srr[0] != 1 ) {\n $srr[0] = 1;\n $k--;\n }\n my $i = 1;\n while ( $k-- && $i < $n ) {\n $srr[$i++] = 0;\n }\n say @srr;\n}\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n \nmy ($n, $k) = split q{ }, $line;\n \nchomp (my $s = );\n \nif ( $k == 0 ) {\n say $s;\n exit(0);\n}\n\nif ( $n == 1 and $k >= 1 ) {\n say 0;\n exit(0);\n}\n\nmy @srr = split q{}, $s;\nif ( $srr[0] != 1 ) {\n $srr[0] = 1;\n $k--;\n}\nmy $i = 1;\nwhile ( $k && $i < $n ) {\n if ( $srr[$i] != '0' ) {\n $srr[$i] = 0;\n }\n $i++;\n $k--;\n}\nsay @srr;\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\t$k and s/[^1]/1/ and $k --;\n\t\n\ts/\n\t\t(?(?{ ! $k }) (*FAIL) )\n\t\t\\B[^0]\n\t\t(?{ $k -- })\n\t\t/\n\t\t0\n\t\t/gex;\n\t\n\t$k and s/^1$/0/;\n\t\n\tprint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\t$k and s/^[^1]/1/ and $k --;\n\t\n\ts/\n\t\t(?(?{ ! $k }) (*FAIL) )\n\t\t\\B[^0]\n\t\t(?{ $k -- })\n\t\t/\n\t\t0\n\t\t/gex;\n\t\n\t$k and s/^1$/0/;\n\t\n\tprint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\t$k and s/^[2-9]/1/ and $k --;\n\t\n\ts/\n\t\t(?(?{ ! $k }) (*FAIL) )\n\t\t\\B[^0]\n\t\t(?{ $k -- })\n\t\t/\n\t\t0\n\t\t/gex;\n\t\n\t$k and s/^1$/0/;\n\t\n\tprint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\tsubstr $_, 0, $k, 0 x $k;\n\t\n\ts/00/10/;\n\t\n\tprint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\t$k and s/^[^1]/1/ and $k --;\n\t\n\t@_ = split //;\n\t\n\tfor( @_[ 1 .. @_ - 1 ] ){\n\t\t$k and s/[^0]/0/ and $k --;\n\t\t}\n\t\n#\ts/\n#\t\t(?(?{ ! $k }) (*FAIL) )\n#\t\t\\B[^0]\n#\t\t(*SKIP)\n#\t\t(?(?{ ! $k }) (*FAIL) )\n#\t\t(?{ $k -- })\n#\t\t/\n#\t\t0\n#\t\t/gex;\n\t\n\t$_ = join '', @_;\n\t\n\t$k and s/^1$/0/;\n\t\n\tprint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\t$k and s/^[^1]/1/ and $k --;\n\t\n\ts/\n\t\t(?(?{ ! $k }) (*FAIL) )\n\t\t\\B[^0]\n\t\t(*SKIP)\n\t\t(?(?{ ! $k }) (*FAIL) )\n\t\t(?{ $k -- })\n\t\t/\n\t\t0\n\t\t/gex;\n\t\n\t$k and s/^1$/0/;\n\t\n\tprint;\n\t}"}], "src_uid": "0515ac888937a4dda30cad5e2383164f"} {"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$$$)\u00a0\u2014 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$$$\u00a0\u2014 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": "$m = 3e4;\r\n\r\n( 1 x $m ) =~ /^(1{2,$m})\\1{1,$m}(?{ $_[ pos ] = 1 })0/;\r\n$A .=- -$_ for @_;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n $_ --,\r\n \r\n\t$A =~ /00.{$_,}?(0).{$_,}?0/,\r\n\t\r\n\tprint $-[1] *~- $+[0] . $/\r\n}"}, {"source_code": "$\\ = $/;\r\n\r\n$m = 3e4;\r\n\r\n( 1 x $m ) =~ /^ (1{2,$m})\\1{1,$m} (?{ $h[ pos ] = 1 }) 0 /x;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$i = $_ + 1;\r\n\t\r\n\t0 while $h[ $i ++ ];\r\n\t\r\n\t$j = -- $i + $_;\r\n\t\r\n\t0 while $h[ $j ++ ];\r\n\t\r\n\tprint $i * -- $j;\r\n}"}, {"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\nmy $debug = 0;\r\n\r\nmy $max = 2e4 + 100;\r\n\r\nmy %h = map { +$_, 0 } 2 .. $max;\r\n\r\n$_ = 1 x $max;\r\n\r\n/^\r\n(1{2,$max}?)\\1{1,$max}?\r\n(?{\r\n delete $h{ ( pos ) };\r\n })\r\n(*F)\r\n/x;\r\n\r\n$debug and print join \" \", sort { $a <=> $b } keys %h;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$debug and print '-' x 15;\r\n\tchomp;\r\n\t\r\n\tmy $d = $_;\r\n\tmy $find = $d + 1;\r\n\t\r\n\t$find ++ while not exists $h{ $find };\r\n\t\r\n\tmy $find2 = $find + $d;\r\n\t\r\n\t$find2 ++ while not exists $h{ $find2 };\r\n\t\r\n\tprint $find * $find2;\r\n}"}], "negative_code": [], "src_uid": "648ec67565c506c3e2ffd007ad44e8c3"} {"nl": {"description": "Arkady is playing Battleship. The rules of this game aren't really important.There is a field of $$$n \\times n$$$ cells. There should be exactly one $$$k$$$-decker on the field, i.\u00a0e. a ship that is $$$k$$$ cells long oriented either horizontally or vertically. However, Arkady doesn't know where it is located. For each cell Arkady knows if it is definitely empty or can contain a part of the ship.Consider all possible locations of the ship. Find such a cell that belongs to the maximum possible number of different locations of the ship.", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 100$$$)\u00a0\u2014 the size of the field and the size of the ship. The next $$$n$$$ lines contain the field. Each line contains $$$n$$$ characters, each of which is either '#' (denotes a definitely empty cell) or '.' (denotes a cell that can belong to the ship).", "output_spec": "Output two integers\u00a0\u2014 the row and the column of a cell that belongs to the maximum possible number of different locations of the ship. If there are multiple answers, output any of them. In particular, if no ship can be placed on the field, you can output any cell.", "sample_inputs": ["4 3\n#..#\n#.#.\n....\n.###", "10 4\n#....##...\n.#...#....\n..#..#..#.\n...#.#....\n.#..##.#..\n.....#...#\n...#.##...\n.#...#.#..\n.....#..#.\n...#.#...#", "19 6\n##..............###\n#......#####.....##\n.....#########.....\n....###########....\n...#############...\n..###############..\n.#################.\n.#################.\n.#################.\n.#################.\n#####....##....####\n####............###\n####............###\n#####...####...####\n.#####..####..#####\n...###........###..\n....###########....\n.........##........\n#.................#"], "sample_outputs": ["3 2", "6 1", "1 8"], "notes": "NoteThe picture below shows the three possible locations of the ship that contain the cell $$$(3, 2)$$$ in the first sample. "}, "positive_code": [{"source_code": "#! /usr/bin/perl\nuse strict; use warnings;\nuse Data::Dumper;\nuse feature 'say';\n\nsub min { return $_[0] < $_[1] ? $_[0] : $_[1] };\nsub max { return $_[0] > $_[1] ? $_[0] : $_[1] };\n\nmy ($n, $k) = split ' ', <>;\nmy $grid;\nfor (1..$n) {\n\tpush @$grid, [ split //, <> ];\n}\n\nmy $DP;\nsub dp {\n\tmy ($x, $y, $dir) = @_;\n\treturn 0 if ($x < 0 || $y < 0 || $x >= $n || $y >= $n) || ($grid->[$x][$y] eq '#');\n\treturn $DP->{$x}{$y}{$dir} if exists $DP->{$x}{$y}{$dir};\n\tmy ($xx, $yy) = ($x, $y);\n\t$x -= 1 if $dir == 0;\n\t$y += 1 if $dir == 1;\n\t$x += 1 if $dir == 2;\n\t$y -= 1 if $dir == 3;\n\treturn $DP->{$xx}{$yy}{$dir} = min($k, 1 + dp($x, $y, $dir));\n}\n\nsub count_ships {\n\tmy ($x, $y, $leftright) = @_;\n\tmy $dots = ($leftright ? dp($x, $y, 1) + dp($x, $y, 3) : dp($x, $y, 0) + dp($x, $y, 2)) - 1;\n\treturn max(0, $dots - $k + 1);\n}\n\nmy $max_hits = 0;\nmy ($x, $y) = (1, 1);\nfor my $i (1..$n) {\n\tfor my $j (1..$n) {\n\t\tmy $hits = count_ships($i - 1, $j - 1, 1);\n\t\t$hits += count_ships($i - 1, $j - 1, 0);\n\t\t($x, $y, $max_hits) = ($i, $j, $hits) if $max_hits < $hits;\n\t}\n}\n\nprint \"$x $y\\n\";\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t@_ = map { \n\t\t$_ = <>, chomp;\n\t\t[ map { ; { 'v' => $_, 'L' => 0, 'R' => 0, 'U' => 0, 'D' => 0, 'M' => 0 } } split // ] \n\t\t} 1 .. $n;\n\t\n\t### horizontal:\n\t\n\t$debug and print join ' ', map { sprintf \"%2s\", $_->{ 'v' } } @{ $_ } for @_;\n\t\n\t$debug and print '-' x 5;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tmy $cnt = 0;\n\t\tfor my $j ( 0 .. @_ - 1 ){\n\t\t\tif( $_[ $i ][ $j ]{ 'v' } eq '.' ){\n\t\t\t\t$_[ $i ][ $j ]{ 'L' } = $cnt;\n\t\t\t\t$cnt ++;\n\t\t\t\t$cnt >= $k and $cnt = $k - 1;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$cnt = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print join ' ', map { sprintf \"%2s\", $_->{ 'L' } } @{ $_ } for @_;\n\t\n\t$debug and print '-' x 5;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tmy $cnt = 0;\n\t\tfor my $j ( reverse 0 .. @_ - 1 ){\n\t\t\tif( $_[ $i ][ $j ]{ 'v' } eq '.' ){\n\t\t\t\t$_[ $i ][ $j ]{ 'R' } = $cnt;\n\t\t\t\t$cnt ++;\n\t\t\t\t$cnt >= $k and $cnt = $k - 1;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$cnt = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print join ' ', map { sprintf \"%2s\", $_->{ 'R' } } @{ $_ } for @_;\n\t\n\t$debug and print '-' x 5;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tfor my $j ( 0 .. @_ - 1 ){\n\t\t\tif( $_[ $i ][ $j ]{ 'v' } eq '.' ){\n\t\t\t\tmy( $L, $R ) = sort { $a <=> $b } \n\t\t\t\t\t$_[ $i ][ $j ]{ 'L' }, $_[ $i ][ $j ]{ 'R' };\n\t\t\t\t$_[ $i ][ $j ]{ 'M' } += ( sort { $b <=> $a } \n\t\t\t\t\t0, ( sort { $a <=> $b } $L + 1, $L + $R + 1 - $k + 1 )[ 0 ] )[ 0 ];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print join ' ', map { sprintf \"%2s\", $_->{ 'M' } } @{ $_ } for @_;\n\t\n\t$debug and print '-' x 5;\n\t\n\t### vertical:\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tmy $cnt = 0;\n\t\tfor my $j ( 0 .. @_ - 1 ){\n\t\t\tif( $_[ $j ][ $i ]{ 'v' } eq '.' ){\n\t\t\t\t$_[ $j ][ $i ]{ 'U' } = $cnt;\n\t\t\t\t$cnt ++;\n\t\t\t\t$cnt >= $k and $cnt = $k - 1;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$cnt = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print join ' ', map { sprintf \"%2s\", $_->{ 'U' } } @{ $_ } for @_;\n\t\n\t$debug and print '-' x 5;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tmy $cnt = 0;\n\t\tfor my $j ( reverse 0 .. @_ - 1 ){\n\t\t\tif( $_[ $j ][ $i ]{ 'v' } eq '.' ){\n\t\t\t\t$_[ $j ][ $i ]{ 'D' } = $cnt;\n\t\t\t\t$cnt ++;\n\t\t\t\t$cnt >= $k and $cnt = $k - 1;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$cnt = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print join ' ', map { sprintf \"%2s\", $_->{ 'D' } } @{ $_ } for @_;\n\t\n\t$debug and print '-' x 5;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tfor my $j ( 0 .. @_ - 1 ){\n\t\t\tif( $_[ $j ][ $i ]{ 'v' } eq '.' ){\n\t\t\t\tmy( $L, $R ) = sort { $a <=> $b } \n\t\t\t\t\t$_[ $j ][ $i ]{ 'U' }, $_[ $j ][ $i ]{ 'D' };\n\t\t\t\t$_[ $j ][ $i ]{ 'M' } += ( sort { $b <=> $a } \n\t\t\t\t\t0, ( sort { $a <=> $b } $L + 1, $L + $R + 1 - $k + 1 )[ 0 ] )[ 0 ];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print join ' ', map { sprintf \"%2s\", $_->{ 'M' } } @{ $_ } for @_;\n\t\n\t$debug and print '-' x 5;\n\t\n\t### end vertical;\n\t\n\tmy $max = 0;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tfor my $j ( 0 .. @_ - 1 ){\n\t\t\tnext if $_[ $i ][ $j ]{ 'v' } eq '#';\n\t\t\t$_[ $i ][ $j ]{ 'M' } > $max and $max = $_[ $i ][ $j ]{ 'M' };\n\t\t\t}\n\t\t}\n\t\n\tmy $ans = \"1 1\";\n\t\n\tANS:\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tfor my $j ( 0 .. @_ - 1 ){\n\t\t\tnext if $_[ $i ][ $j ]{ 'v' } eq '#';\n\t\t\tnext if $_[ $i ][ $j ]{ 'M' } == 0;\n\t\t\t$_[ $i ][ $j ]{ 'M' } == $max and do {\n\t\t\t\t$ans = join ' ', map $_ + 1, $i, $j;\n\t\t\t\tlast ANS;\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\n\tprint $ans;\n\t\n\t$debug and print '-' x 15;\n\t}"}], "negative_code": [{"source_code": "#! /usr/bin/perl\nuse strict; use warnings;\nuse Data::Dumper;\nuse feature 'say';\n\nsub min { return $_[0] < $_[1] ? $_[0] : $_[1] };\nsub max { return $_[0] > $_[1] ? $_[0] : $_[1] };\n\nmy ($n, $k) = split ' ', <>;\nmy $grid;\nfor (1..$n) {\n\tpush @$grid, [ split //, <> ];\n}\n\nmy $DP;\nsub dp {\n\tmy ($x, $y, $dir) = @_;\n\treturn 0 if ($x < 0 || $y < 0 || $x >= $n || $y >= $n) || ($grid->[$x][$y] eq '#');\n\treturn $DP->{$x}{$y}{$dir} if exists $DP->{$x}{$y}{$dir};\n\tmy ($xx, $yy) = ($x, $y);\n\t$x -= 1 if $dir == 0;\n\t$y += 1 if $dir == 1;\n\t$x += 1 if $dir == 2;\n\t$y -= 1 if $dir == 3;\n\treturn $DP->{$xx}{$yy}{$dir} = min($k, 1 + dp($x, $y, $dir));\n}\n\nsub count_ships {\n\tmy ($x, $y, $leftright) = @_;\n\tmy $dots = ($leftright ? dp($x, $y, 1) + dp($x, $y, 3) : dp($x, $y, 0) + dp($x, $y, 2)) - 1;\n\treturn max(0, $dots - $k + 1);\n}\n\nmy $max_hits = 0;\nmy ($x, $y) = (0, 0);\nfor my $i (1..$n) {\n\tfor my $j (1..$n) {\n\t\tmy $hits = count_ships($i - 1, $j - 1, 1);\n\t\t$hits += count_ships($i - 1, $j - 1, 0);\n\t\t($x, $y, $max_hits) = ($i, $j, $hits) if $max_hits < $hits;\n\t}\n}\n\nprint \"$x $y\\n\";"}, {"source_code": "#! /usr/bin/perl\nuse strict; use warnings;\nuse Data::Dumper;\nuse feature 'say';\n\nsub min { return $_[0] < $_[1] ? $_[0] : $_[1] };\nsub max { return $_[0] > $_[1] ? $_[0] : $_[1] };\n\nmy ($n, $k) = split ' ', <>;\nmy $grid;\nfor (1..$n) {\n\tpush @$grid, [ split //, <> ];\n}\n\nmy $DP;\nsub dp {\n\tmy ($x, $y, $dir) = @_;\n\treturn 0 if ($x < 0 || $y < 0 || $x >= $n || $y >= $n) || ($grid->[$x][$y] eq '#');\n\treturn $DP->{$x}{$y}{$dir} if exists $DP->{$x}{$y}{$dir};\n\tmy ($xx, $yy) = ($x, $y);\n\t$x -= 1 if $dir == 0;\n\t$y += 1 if $dir == 1;\n\t$x += 1 if $dir == 2;\n\t$y -= 1 if $dir == 3;\n\treturn $DP->{$xx}{$yy}{$dir} = min($k, 1 + dp($x, $y, $dir));\n}\n\nprint dp(0, 0, 1);\n\nsub count_ships {\n\tmy ($x, $y, $leftright) = @_;\n\tmy $dots = ($leftright ? dp($x, $y, 1) + dp($x, $y, 3) : dp($x, $y, 0) + dp($x, $y, 2)) - 1;\n\treturn max(0, $dots - $k + 1);\n}\n\nmy $max_hits = 0;\nmy ($x, $y) = (1, 1);\nfor my $i (1..$n) {\n\tfor my $j (1..$n) {\n\t\tmy $hits = count_ships($i - 1, $j - 1, 1);\n\t\t$hits += count_ships($i - 1, $j - 1, 0);\n\t\t($x, $y, $max_hits) = ($i, $j, $hits) if $max_hits < $hits;\n\t}\n}\n\nprint \"$x $y\\n\";\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\tmy @A = map ~~<>, 1 .. $n;\n\tchomp @A;\n\t\n\tmy @lengths;\n\t\n\t@_ = @A;\n\t\n\tfor( @_ ){\n\t\ts!\n\t\t\t(\\.{$k,})\n\t\t!\n\t\t\tmy $len = length $1;\n\t\t\t'>' x ( $len % 2 + int $len / 2 ) .\n\t\t\t'<' x ( int $len / 2 )\n\t\t!gex;\n\t\t}\n\t\n\t$debug and print for @_;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\twhile( $_[ $i ] =~ /(>+)/g ){\n\t\t\tmy $len = length $1;\n\t\t\tpush @lengths, [ $len, $i + 1, pos $_[ $i ] ];\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \" @{ $_ }\" for @lengths;\n\t\n\t@_ = ( '' ) x @A;\n\t\n\tfor( @A ){\n\t\tmy $i = 0;\n\t\tmap { $_[ $i ++ ] .= $_ } split //;\n\t\t}\n\t\n\tfor( @_ ){\n\t\ts!\n\t\t\t(\\.{$k,})\n\t\t!\n\t\t\tmy $len = length $1;\n\t\t\t'>' x ( $len % 2 + int $len / 2 ) .\n\t\t\t'<' x ( int $len / 2 )\n\t\t!gex;\n\t\t}\n\t\n\t$debug and print for @_;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\twhile( $_[ $i ] =~ /(>+)/g ){\n\t\t\tmy $len = length $1;\n\t\t\tpush @lengths, [ $len, pos $_[ $i ], $i + 1 ];\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \" @{ $_ }\" for @lengths;\n\t\n\tprint !@lengths ? \"1 1\" : join ' ', map { $_->[ 1 ], $_->[ 2 ] } \n\t\t( sort { $b->[ 0 ] <=> $a->[ 0 ] } @lengths )[ 0 ];\n\t\n\t$debug and print '-' x 15;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\tmy @A = map ~~<>, 1 .. $n;\n\tchomp @A;\n\t\n\tmy @lengths;\n\t\n\t@_ = @A;\n\t\n\tfor( @_ ){\n\t\ts!\n\t\t\t(\\.{$k,})\n\t\t!\n\t\t\tmy $len = length $1;\n\t\t\t'>' x ( $len % 2 + int $len / 2 ) .\n\t\t\t'<' x ( int $len / 2 )\n\t\t!gex;\n\t\t}\n\t\n\t$debug and print for @_;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\twhile( $_[ $i ] =~ /(>+)/g ){\n\t\t\tmy $len = length $1;\n\t\t\tpush @lengths, [ $len, $i + 1, pos $_[ $i ] ];\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \" @{ $_ }\" for @lengths;\n\t\n\t@_ = ( '' ) x @A;\n\t\n\tfor( @A ){\n\t\tmy $i = 0;\n\t\tmap { $_[ $i ++ ] .= $_ } split //;\n\t\t}\n\t\n\tfor( @_ ){\n\t\ts!\n\t\t\t(\\.{$k,})\n\t\t!\n\t\t\tmy $len = length $1;\n\t\t\t'>' x ( $len % 2 + int $len / 2 ) .\n\t\t\t'<' x ( int $len / 2 )\n\t\t!gex;\n\t\t}\n\t\n\t$debug and print for @_;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\twhile( $_[ $i ] =~ /(>+)/g ){\n\t\t\tmy $len = length $1;\n\t\t\tpush @lengths, [ $len, pos $_[ $i ], $i + 1 ];\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \" @{ $_ }\" for @lengths;\n\t\n\tprint join ' ', map { $_->[ 1 ], $_->[ 2 ] } \n\t\t( sort { $b->[ 0 ] <=> $a->[ 0 ] } @lengths )[ 0 ];\n\t\n\t$debug and print '-' x 15;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t@_ = map { $_ = <>, chomp; [ map s/\\./$k * 2/er, split // ] } 1 .. $n;\n\t\n\t$debug and print join ' ', map { sprintf \"%2s\", $_ } @{ $_ } for @_;\n\t\n\t$debug and print '-' x 5;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tmy $cnt = $k;\n\t\tfor my $j ( 0 .. @_ - 1 ){\n\t\t\t$_[ $i ][ $j ] =~ s/(-?\\d+)/ $cnt --; $cnt < 0 and $cnt = 0;\n\t\t\t\t$1 - $cnt \n\t\t\t\t/e or do {\n\t\t\t\t\t$cnt = $k;\n\t\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\n\t$debug and print join ' ', map { sprintf \"%2s\", $_ } @{ $_ } for @_;\n\t\n\t$debug and print '-' x 5;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tmy $cnt = $k;\n\t\tfor my $j ( reverse 0 .. @_ - 1 ){\n\t\t\t$_[ $i ][ $j ] =~ s/(-?\\d+)/ $cnt --; $cnt < 0 and $cnt = 0;\n\t\t\t\t$1 - $cnt \n\t\t\t\t/e or do {\n\t\t\t\t\t$cnt = $k;\n\t\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\n\t$debug and print join ' ', map { sprintf \"%2s\", $_ } @{ $_ } for @_;\n\t\n\t$debug and print '-' x 5;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tmy $cnt = $k;\n\t\tfor my $j ( 0 .. @_ - 1 ){\n\t\t\t$_[ $j ][ $i ] =~ s/(-?\\d+)/ $cnt --; $cnt < 0 and $cnt = 0;\n\t\t\t\t$1 - $cnt \n\t\t\t\t/e or do {\n\t\t\t\t\t$cnt = $k;\n\t\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\n\t$debug and print join ' ', map { sprintf \"%2s\", $_ } @{ $_ } for @_;\n\t\n\t$debug and print '-' x 5;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tmy $cnt = $k;\n\t\tfor my $j ( reverse 0 .. @_ - 1 ){\n\t\t\t$_[ $j ][ $i ] =~ s/(-?\\d+)/ $cnt --; $cnt < 0 and $cnt = 0;\n\t\t\t\t$1 - $cnt \n\t\t\t\t/e or do {\n\t\t\t\t\t$cnt = $k;\n\t\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\n\t$debug and print join ' ', map { sprintf \"%2s\", $_ } @{ $_ } for @_;\n\t\n\tmy $max = 0;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tfor my $j ( reverse 0 .. @_ - 1 ){\n\t\t\tnext if $_[ $i ][ $j ] eq '#';\n\t\t\t$_[ $i ][ $j ] > $max and $max = $_[ $i ][ $j ];\n\t\t\t}\n\t\t}\n\t\n\tmy $ans;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tfor my $j ( reverse 0 .. @_ - 1 ){\n\t\t\tnext if $_[ $i ][ $j ] eq '#';\n\t\t\tnext if $_[ $i ][ $j ] eq '0';\n\t\t\t$_[ $i ][ $j ] == $max and do {\n\t\t\t\t$ans = join ' ', map $_ + 1, $i, $j;\n\t\t\t\tlast;\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\n\tprint defined $ans ? $ans : \"1 1\";\n\t\n\t$debug and print '-' x 15;\n\t}"}], "src_uid": "6fb20914525715af737d81f3a5d98636"} {"nl": {"description": "Andrew was very excited to participate in Olympiad of Metropolises. Days flew by quickly, and Andrew is already at the airport, ready to go home. He has $$$n$$$ rubles left, and would like to exchange them to euro and dollar bills. Andrew can mix dollar bills and euro bills in whatever way he wants. The price of one dollar is $$$d$$$ rubles, and one euro costs $$$e$$$ rubles.Recall that there exist the following dollar bills: $$$1$$$, $$$2$$$, $$$5$$$, $$$10$$$, $$$20$$$, $$$50$$$, $$$100$$$, and the following euro bills\u00a0\u2014 $$$5$$$, $$$10$$$, $$$20$$$, $$$50$$$, $$$100$$$, $$$200$$$ (note that, in this problem we do not consider the $$$500$$$ euro bill, it is hard to find such bills in the currency exchange points). Andrew can buy any combination of bills, and his goal is to minimize the total number of rubles he will have after the exchange.Help him\u00a0\u2014 write a program that given integers $$$n$$$, $$$e$$$ and $$$d$$$, finds the minimum number of rubles Andrew can get after buying dollar and euro bills.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\leq n \\leq 10^8$$$)\u00a0\u2014 the initial sum in rubles Andrew has. The second line of the input contains one integer $$$d$$$ ($$$30 \\leq d \\leq 100$$$)\u00a0\u2014 the price of one dollar in rubles. The third line of the input contains integer $$$e$$$ ($$$30 \\leq e \\leq 100$$$)\u00a0\u2014 the price of one euro in rubles.", "output_spec": "Output one integer\u00a0\u2014 the minimum number of rubles Andrew can have after buying dollar and euro bills optimally.", "sample_inputs": ["100\n60\n70", "410\n55\n70", "600\n60\n70"], "sample_outputs": ["40", "5", "0"], "notes": "NoteIn the first example, we can buy just $$$1$$$ dollar because there is no $$$1$$$ euro bill.In the second example, optimal exchange is to buy $$$5$$$ euro and $$$1$$$ dollar.In the third example, optimal exchange is to buy $$$10$$$ dollars in one bill."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp(\n\t\tmy( $n, $d, $e ) = ( $_, ~~<>, ~~<> )\n\t\t); \n\t\n\tmy $D = $d * 1;\n\tmy $E = $e * 5;\n\t\n\tmy @mods;\n\t\n\tfor my $i ( 0 .. 1e6 ){\n\t\tmy $nn = $n - $i * $E;\n\t\tlast if $nn < 0;\n\t\tpush @mods, $nn % $D;\n\t\t}\n\t\n\tprint +( sort { $a <=> $b } @mods )[ 0 ];\n\t}"}], "negative_code": [], "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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B, $C ) = split;\n\t\n\tif(\n\t\t$B + $B - $A >= $C and\n\t\t( $B + $B - $A ) % $C == 0 or\n\t#\t( print 2 ) and\n\t\t$B + $B - $C >= $A and \n\t\t( $B + $B - $C ) % $A == 0 or\n\t#\t( print 3 ) and\n\t\t( ( $A + $C ) / 2 ) !~ m/\\./ and\n\t\t( $A + $C ) / 2 >= $B and\n\t\t( ( $A + $C ) / 2 ) % $B == 0\n\t\t){\n\t\tprint \"YES\";\n\t\t}\n\telse{\n\t\tprint \"NO\";\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "90c5058c0c7f55a567f2e036482149f9"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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 \u00a0\u2014 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": "<>;\r\n\r\nwhile(<>){\r\n\t$_ = <>;\r\n\t\r\n\tprint +( sort { $b <=> $a }\r\n\tmap {\r\n\t\t$cz = () = /0/g;\r\n\t\t\r\n\t\t$Z = 0;\r\n\t\t\r\n\t\t/\\d(?{ $& ? $Z += $cz : $cz -- })(*F)/;\r\n\t\t\r\n\t\t$Z\r\n\t\t} $_, s/0/1/r, s/1(?!.*?1)/0/r\r\n\t\t)[ 0 ] . $/\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\n#use strict;\n\n<>;\n\nwhile(<>){\n\t$_ = <>;\n\t\n\tprint +( sort { $b <=> $a }\n\tmap {\n\t\t$cz = () = /0/g;\n\t\t\n\t\t$Z = 0;\n\t\t\n\t\t/\\d(?{ $& ? $Z += $cz : $cz -- })(*F)/;\n\t\t\n\t\t$Z\n\t\t} $_, s/0/1/r, s/1(?!.*?1)/0/r\n\t\t)[ 0 ] . $/\n\t}"}, {"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t@_ = split ' ', <>;\r\n\t\r\n\t@c = ci( @_ );\r\n\t\r\n\t@A = @_;\r\n\t\r\n\tfor $i ( 0 .. @A - 1 ){\r\n\t\tif( ! $A[ $i ] ){\r\n\t\t\t$A[ $i ] = 1;\r\n\t\t\tlast;\r\n\t\t\t}\r\n\t\t}\r\n\t\r\n\tpush @c, ci( @A );\r\n\t\r\n\t@A = @_;\r\n\t\r\n\tfor $i ( reverse 0 .. @A - 1 ){\r\n\t\tif( $A[ $i ] ){\r\n\t\t\t$A[ $i ] = 0;\r\n\t\t\tlast;\r\n\t\t\t}\r\n\t\t}\r\n\t\r\n\tpush @c, ci( @A );\r\n\t\r\n\tprint +( sort { $b <=> $a } @c )[ 0 ];\r\n\t}\r\n\r\nsub ci {\r\n\t$cz = grep !$_, @_;\r\n\t\r\n\t$Z = 0;\r\n\t\r\n\tfor $i ( 0 .. @_ - 1 ){\r\n\t\t$_[ $i ] ? $Z += $cz : $cz --;\r\n\t\t}\r\n\t\r\n\t$Z\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @cand;\n\t\n\tpush @cand, count_inversions( @_ );\n\t\n\tmy @A = @_;\n\t\n\tfor my $i ( 0 .. @A - 1 ){\n\t\tif( $A[ $i ] == 0 ){\n\t\t\t$A[ $i ] = 1;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tpush @cand, count_inversions( @A );\n\t\n\t@A = @_;\n\t\n\tfor my $i ( reverse 0 .. @A - 1 ){\n\t\tif( $A[ $i ] == 1 ){\n\t\t\t$A[ $i ] = 0;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tpush @cand, count_inversions( @A );\n\t\n\tprint +( sort { $b <=> $a } @cand )[ 0 ];\n\t}\n\nsub count_inversions {\n\tmy $cz = grep !$_, @_;\n\t\n\tmy $Z = 0;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tif( $_[ $i ] == 1 ){\n\t\t\t$Z += $cz;\n\t\t\t}\n\t\telse{\n\t\t\t$cz --;\n\t\t\t}\n\t\t}\n\t\n\treturn $Z;\n\t}"}], "negative_code": [], "src_uid": "0657ce4ce00addefc8469381c57294fc"} {"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\u00a0\u2014 the number of vertices in the tree (3\u2009\u2264\u2009n\u2009\u2264\u20091\u2009000). Each of the next n\u2009-\u20091 lines contains one integer pi (1\u2009\u2264\u2009i\u2009\u2264\u2009n\u2009-\u20091)\u00a0\u2014 the index of the parent of the i\u2009+\u20091-th vertex (1\u2009\u2264\u2009pi\u2009\u2264\u2009i). 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\n\n\n# returns 1 if the number of children is greater\n# than or equal to three and returns 0 otherwise\n# this subroutine also returns 1 if the node is a leaf\nsub has_three_leaves {\n\n my ($mapp_ref, $curr) = @_;\n\n # count the number of leaves for each node\n my $leaves = 0;\n\n # return 1 if the node is a leaf\n if ( (scalar @{ $mapp_ref -> { $curr } }) == 0 ) {\n\treturn 1;\n }\n\n # count number of children that are leaves\n for my $one_key (@{ $mapp_ref -> { $curr } }) {\n\t\n\tif ( (scalar @{ $mapp_ref -> { $one_key } }) == 0 ) {\n\t $leaves += 1;\n\t if ( $leaves >= 3 ) {\n\t\treturn 1;\n\t }\n\t}\n }\n\n return 0;\n}\n\n\n\n\n# returns 1 if the tree is sparse\n# and returns 0 otherwise\nsub is_sparse {\n\n my $mapp_ref = $_[0];\n\n for my $i (keys %{ $mapp_ref }) {\n\n\tif ( not &has_three_leaves ( $mapp_ref, $i ) ) {\n\t # return 0 because we found a non-leaf node that has\n\t # less than three children\n\t return 0;\n\t}\n }\n # return 1 because every non-leaf node \n # that has at least three children\n return 1;\n}\n\n\n\n\n\n\n\n\n\n\n# read number of nodes n\nmy $n = ;\nchomp ( $n );\n\n# my map\nmy %mapp;\n# parent \nmy $p;\n\n\n#\nmy @children = ();\n\n# read edges of the tree\nfor my $i (2..$n) {\n $p = ;\n chomp ( $p );\n\n $mapp { $i } = [ () ];\n\n if ( not exists $mapp { $p } ) {\n\t# inserts a reference to an empty array\n\t$mapp { $p } = [ () ];\n }\n\n push (@{ $mapp{ $p } }, $i);\n}\n\n\n\n# calculate answer\nmy $ans = &is_sparse ( \\%mapp );\n\n# print answer\nif ( $ans ) {\n print \"YES\\n\";\n}\nelse {\n print \"NO\\n\";\n}\n\n\n\n"}], "negative_code": [], "src_uid": "69edc72ec29d4dd56751b281085c3591"} {"nl": {"description": "There are some beautiful girls in Arpa\u2019s 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,\u2009j (1\u2009\u2264\u2009i\u2009<\u2009j\u2009\u2264\u2009n) 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\u2009\u2264\u2009n\u2009\u2264\u2009105,\u20090\u2009\u2264\u2009x\u2009\u2264\u2009105)\u00a0\u2014 the number of elements in the array and the integer x. Second line contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009105)\u00a0\u2014 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\u2009=\u20091 and j\u2009=\u20092. so the answer is 1.In the second sample the only two pairs are i\u2009=\u20093, j\u2009=\u20094 (since ) and i\u2009=\u20091, j\u2009=\u20095 (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": "use strict;\nuse warnings;\n\n\nmy ($n,$x)=split / /,<>;\nchomp($n,$x);\n\nmy @data=split / /,<>;\nchomp(@data);\nmy %k;\nfor(my $i=0;$i<$n;$i++){\n\tpush(@{$k{$data[$i]}},$i);\n}\nmy $ans=0;\n\nif($x==0){\nfor my $dd (keys %k){\n\tmy $ff=scalar(@{$k{$dd}});\n\t$ans+=($ff)*($ff-1)/2;\n}\nprint $ans . \"\\n\";\n}\nelse{\nfor my $dd (keys %k){\n\tmy $t= int($x)^int($dd);\n\tif(defined $k{$t}){\n\t\t$ans+=scalar(@{$k{$dd}}) * scalar(@{$k{$t}});\n\t}\n}\n$ans/=2;\nprint $ans . \"\\n\";\n}\n\n\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $x ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $j = 0;\n\tfor my $i (0 .. 20){\n\t\t$j = $i;\n\t\t$x < 2 ** $i and last;\n\t\t}\n\t\n\t$debug and print \"j: $j\";\n\t\n\tmy %h;\n\t\n\t$h{$_} ++ for @_;\n\t\n\t$debug and print \"$_ -> $h{$_}\" for keys %h;\n\t\n\tmy $sum = 0;\n\t\n\tfor (@_){\n\t\t$h{$_} --;\n\t\t\n\t\tmy $rem = $_ % 2 ** $j;\n\t\tmy $st = $_ - $rem;\n\t\t\n\t\t$debug and print \"^: \", $rem ^ $x;\n\t\t\n\t\t$sum += $h{ $st + ($rem ^ $x) } || 0;\n\t\t\n\t\t}\n\t\n\tprint $sum;\n\t\n\t}"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\n\n\nmy ($n,$x)=split / /,<>;\nchomp($n,$x);\n\nmy @data=split / /,<>;\nchomp(@data);\nmy %k;\nmap {$k{$_}=1} @data;\nmy $ans=0;\nfor my $val (@data){\n\tmy $t= int($x)^int($val);\n\tif(defined $k{$t}){\n\t\t$ans++;\n\t}\n}\n$ans/=2;\nprint $ans . \"\\n\";\n\n\n"}, {"source_code": "use strict;\nuse warnings;\n\n\nmy ($n,$x)=split / /,<>;\nchomp($n,$x);\n\nmy @data=split / /,<>;\nchomp(@data);\nmy %k;\nfor(my $i=0;$i<$n;$i++){\n\tpush(@{$k{$data[$i]}},$i);\n}\nmy $ans=0;\n\nif($x==0){\nfor my $dd (keys %k){\n\t$ans+=2*scalar(@{$k{$dd}});\n}\n}\nelse{\nfor my $dd (keys %k){\n\tmy $t= int($x)^int($dd);\n\tif(defined $k{$t}){\n\t\t$ans+=scalar(@{$k{$dd}}) * scalar(@{$k{$t}});\n\t}\n}\n}\n$ans/=2;\nprint $ans . \"\\n\";\n\n\n"}, {"source_code": "use strict;\nuse warnings;\n\n\nmy ($n,$x)=split / /,<>;\nchomp($n,$x);\n\nmy @data=split / /,<>;\nchomp(@data);\nmy %k;\nfor(my $i=0;$i<$n;$i++){\n\tpush(@{$k{$data[$i]}},$i);\n}\nmy %indexes;\n\n\nmy $ans=0;\nfor my $dd (keys %k){\n\tmy $t= int($x)^int($dd);\n\tif(defined $k{$t}){\n\t\t$ans+=scalar(@{$k{$dd}}) * scalar(@{$k{$t}});\n\t}\n}\n$ans/=2;\nprint $ans . \"\\n\";\n\n\n"}], "src_uid": "daabf732540e0f66d009dc211c2d7b0b"} {"nl": {"description": "The new \"Die Hard\" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A \"Die Hard\" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 \u2014 the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.", "output_spec": "Print \"YES\" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print \"NO\".", "sample_inputs": ["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"], "sample_outputs": ["YES", "NO", "NO"], "notes": null}, "positive_code": [{"source_code": "<>;\n\n$_=<>;\ns/2|50|1/$&==2?($i++):($&==1?($j>0?($j--,$i--):($i-=3)):($i--,$j++)),($i<0||$j<0)?($f++):()/eg;\n\nprint $f?\"NO\":\"YES\";\n"}, {"source_code": "<>;\n\n$_=<>;\ns/2|50|1/$&==2?($i++):($&==1?($j>0?($j--,$i--):($i-=3)):($i--,$j++)),($i<0||$j<0)?($f++):()/eg;\n\nprint $f?\"NO\":\"YES\";\n"}, {"source_code": "<>;\nmy $ans = \"YES\\n\";\nmy ($tt, $ff) = (0, 0);\nfor (split / /, <>) {\n $tt++ if($_ == 25);\n $ff++, $tt-- if($_ == 50);\n if ($_ == 100) {\n if ($ff && $tt) {\n $ff--;\n $tt--;\n }\n else {\n $tt -= 3;\n }\n }\n if ($tt < 0 || $ff < 0) {\n $ans = \"NO\\n\";\n last;\n }\n}\nprint $ans;\n"}, {"source_code": "<>;\n\n$_=<>;\ns/2|50|1/$&==2?($i++):($&==1?($j>0?($j--,$i--):($i-=3)):($i--,$j++)),($i<0||$j<0)?($f++):()/eg;\n\nprint $f?\"NO\":\"YES\";"}, {"source_code": "<>;\n\n$_=<>;\ns/2|50|1/$&==2?($i++):($&==1?($j>0?($j--,$i--):($i-=3)):($i--,$j++)),($i<0||$j<0)?($f++):()/eg;\n\nprint $f?\"NO\":\"YES\";\n"}, {"source_code": "<>;\n\n$_=<>;\ns/2|50|1/$&==2?($i++):($&==1?($j>0?($j--,$i--):($i-=3)):($i--,$j++)),($i<0||$j<0)?($f++):()/eg;\n\nprint $f?\"NO\":\"YES\";\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n($n25, $n50) = (0) x 2;\nforeach (split / /, <>) {\n\tif ($_ == 25) {\n\t\t++$n25;\n\t} elsif ($_ == 50) {\n\t\t$n25==0 and say \"NO\" and exit;\n\t\t--$n25;\n\t\t++$n50;\n\t} else {\n\t\tif ($n50>0 && $n25>0) {\n\t\t\t--$n50;\n\t\t\t--$n25;\n\t\t} elsif ($n25 >= 3) {\n\t\t\t$n25 -= 3;\n\t\t} else {\n\t\t\tsay \"NO\" and exit;\n\t\t}\n\t}\n}\nsay \"YES\";"}, {"source_code": "$n = <>;\n$hundred = 0;\n$fifty = 0;\n$twentytwo = 0;\n$ans = 'YES';\n@queue = split ' ', <>;\nforeach $a (@queue)\n{\n\tif($a == 25)\n\t{\n\t\t$twentytwo++;\n\t}\n\telsif($a == 50)\n\t{\n\t\t$fifty++;\n\t\t$twentytwo--;\n\t}\n\telse\n\t{\n\t\tif($fifty && $twentytwo)\n\t\t{\n\t\t\t$fifty--;\n\t\t\t$twentytwo--;\n\t\t}\n\t\telse\n\t\t{\n\t\t\t$twentytwo -= 3;\n\t\t}\n\t\t$hundred++;\n\t}\n\tif($twentytwo < 0 || $fifty < 0)\n\t{\n\t\t$ans = 'NO';\n\t\tlast;\n\t}\n}\nprint $ans;\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# STDIN: cinema_line.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 05/25/2015 10:29:48 PM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.012;\n\n#open STDIN, \"in.txt\";\nmy (@a, %num);\nwhile() {\n chomp($_ = );\n undef %num;\n undef @a;\n @a = split;\n $num{25} = $num{50} = $num{100} = 0;\n my $mark = 0;\n for my $i (@a) {\n if ($i == 25) {\n $num{25}++;\n }\n elsif ($i == 50 && $num{25} >= 1) {\n $num{25}--;\n $num{50}++;\n }\n elsif ($i == 100) {\n if ($num{50} >= 1 && $num{25} >= 1) {\n $num{50}--;\n $num{25}--;\n }\n elsif ($num{25} >= 3) {\n $num{25} -= 3;\n }\n else {\n print \"NO\\n\";\n $mark = 1;\n last;\n }\n }\n else {\n print \"NO\\n\";\n $mark = 1;\n last;\n }\n }\n print \"YES\\n\" if $mark == 0;\n}\n\n\n"}, {"source_code": "<>;\n\n$_=<>;\ns/2|50|1/$&==2?($i++):($&==1?($j>0?($j--,$i--):($i-=3)):($i--,$j++)),($i<0||$j<0)?($f++):()/eg;\n\nprint $f?\"NO\":\"YES\";\n"}, {"source_code": "#!/usr/bin/perl -w\nuse 5.010;\n\nsub Solve {\n my %change = ();\n foreach $p (@_) {\n given ($p) {\n when (25) {\n $change{25}++;\n }\n when (50) {\n $change{25} || return 0;\n $change{25}--, $change{50}++;\n }\n when (100) {\n $change{25} || return 0;\n $change{50} || $change{25} >= 3 || return 0;\n if ($change{50}) {\n $change{50}--, $change{25}--, $change{100}++;\n } else {\n $change{25} -= 3;\n $change{100}++;\n }\n }\n }\n }\n return 1;\n}\n\nwhile (<>) {\n @queue = split /\\s+/, <>;\n say &Solve(@queue)? 'YES' : 'NO';\n}\n"}, {"source_code": "<>;\n\n$_=<>;\ns/2|50|1/$&==2?($i++):($&==1?($j>0?($j--,$i--):($i-=3)):($i--,$j++)),($i<0||$j<0)?($f++):()/eg;\n\nprint $f?\"NO\":\"YES\";\n"}, {"source_code": "<>;\n\n$_=<>;\ns/2|50|1/$&==2?($i++):($&==1?($j>0?($j--,$i--):($i-=3)):($i--,$j++)),($i<0||$j<0)?($f++):()/eg;\n\nprint $f?\"NO\":\"YES\";\n"}, {"source_code": "<>;\n\n$_=<>;\ns/2|50|1/$&==2?($i++):($&==1?($j>0?($j--,$i--):($i-=3)):($i--,$j++)),($i<0||$j<0)?($f++):()/eg;\n\nprint $f?\"NO\":\"YES\";"}, {"source_code": "<>;\n\n$_=<>;\ns/25|50|1/$&==25?($i++):($&==1?($j>0?($j--,$i--):($i-=3)):($i--,$j++)),($i<0||$j<0)?($f++):()/eg;\n\nprint $f?\"NO\":\"YES\";"}, {"source_code": "<>;\n\n$_=<>;\ns/2|50|1/$&==2?($i++):($&==1?($j>0?($j--,$i--):($i-=3)):($i--,$j++)),($i<0||$j<0)?($f++):()/eg;\n\nprint $f?\"NO\":\"YES\";\n"}], "negative_code": [{"source_code": "<>;\nmy $change = 0;\nfor (split / /, <>) {\n if (int($_-=25) > $change) {\n $change = -1;\n last;\n }\n $change += 25;\n}\nprint $change == -1 ? \"NO\\n\" : \"YES\\n\";\n"}, {"source_code": "<>;\nmy $change = 0;\nfor (split / /, <>) {\n if (int$_ == 25) {\n $change += 25;\n }\n elsif (int($_ -= 25) <= $change) {\n $change += $_;\n }\n else {\n $change = -1;\n last;\n }\n}\nprint $change == -1 ? \"NO\\n\" : \"YES\\n\";\n"}, {"source_code": "<>;\nmy $change = 0;\nmy $ans = \"YES\\n\";\nfor (split / /, <>) {\n $change += 25, next if(int$_ == 25);\n $change -= $_, next if($_-=25 <= $change);\n $ans = \"NO\\n\";\n last;\n}\nprint $ans;\n"}, {"source_code": "$n = <>;\n$hundred = 0;\n$fifty = 0;\n$twentytwo = 0;\n$ans = 'YES';\n@queue = split ' ', <>;\nforeach $a (@queue)\n{\n\tif($a == 25)\n\t{\n\t\t$twentytwo++;\n\t}\n\telsif($a == 50)\n\t{\n\t\t$fifty++;\n\t\t$twentytwo--;\n\t}\n\telse\n\t{\n\t\tif($fifty && $twentytwo)\n\t\t{\n\t\t\t$fifty++;\n\t\t\t$twentytwo--;\n\t\t}\n\t\telse\n\t\t{\n\t\t\t$twentytwo -= 3;\n\t\t}\n\t\t$hundred++;\n\t}\n\tif($twentytwo < 0 || $fifty < 0)\n\t{\n\t\t$ans = 'NO';\n\t\tlast;\n\t}\n}\nprint $ans;\n"}, {"source_code": "<>;\n\n$_=<>;\ns/[120]/$&==2?($i++):($&==0?($i--):($i-=2)),$i<0 and $f++/eg;\n\nprint $f?\"NO\":\"YES\";"}], "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\u2009\u2264\u2009qi). 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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of rooms. The i-th of the next n lines contains two integers pi and qi (0\u2009\u2264\u2009pi\u2009\u2264\u2009qi\u2009\u2264\u2009100) \u2014 the number of people who already live in the i-th room and the room's capacity.", "output_spec": "Print a single integer \u2014 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": "\n\nmy $a = <>;\nmy $cnt = 0;\nforeach (0 .. $a)\n{\n ($b, $c) = split \" \", <>;\n ++$cnt unless $b + 2 > $c;\n}\nprint $cnt;"}, {"source_code": "#!/usr/bin/perl\n\nuse 5.012;\nuse warnings;\n\nmy $n = <>;\nmy $cnt = 0;\nwhile ($n--) {\n\tmy ($p, $q) = split / /, <>;\n\t$cnt++ if $q - $p >= 2;\n}\nprint $cnt;\n"}, {"source_code": "my $a = <>;\nmy $cnt = 0;\nforeach (0 .. $a)\n{\n ($b, $c) = split \" \", <>;\n ++$cnt unless $b + 2 > $c;\n}\nprint $cnt;"}, {"source_code": "my $n = <>;\nmy ($a, $b, $ans) = (0, 0, 0);\nfor (1..$n) {\n ($a, $b) = split \" \", <>;\n $ans++ if ($b - $a >= 2);\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "use strict;\nmy $t = <>;\nmy $cnt = 0;\nwhile($t --) {\n\n\tchomp (my $line = <>) ;\n\tmy ($a,$b) = split / /, $line;\n\t$cnt += $a + 2 <= $b;\n}\nprint \"$cnt\\n\";\n"}, {"source_code": "#!/perl -w\n\nuse strict ;\n\nchomp ( my $n = );\n\nmy $canlive = 0 ;\nwhile ($n--){\n my($cu_people,$all_people) = split /\\s+/s , ;\n $canlive++ if ( $all_people - $cu_people >= 2 ) ;\n\n}\n\nprint $canlive,\"\\n\" ;"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nchomp($n = <>);\nmy $ans = 0;\nwhile ($n-- > 0) {\n\t($p, $q) = split / /, <>;\n\t$p+2<=$q and $ans++;\n}\nsay $ans;"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nmy $n = <>;\nmy $c = 0;\nfor (1 .. $n) {\n my ($p, $q) = split ' ', <>;\n $c++ if $p + 2 <= $q;\n}\nprint \"$c\\n\";\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy $cnt = 0;\nfor (1..$n) {\n my ($a, $b) = split ' ', <>;\n $cnt++ if ($a < $b - 1);\n}\nprint $cnt.\"\\n\";\n"}, {"source_code": ";$res = 0; while(){chomp; my ($x, $n) = split(\" \"); $res += 1 if $x <= $n-2; } print \"$res\\n\""}, {"source_code": "$n = <>;\n$ans = 0;\nwhile ($n--) {\n ($p, $q) = split(/\\s+/, <>);\n if ($q - $p >= 2) {\n ++$ans;\n }\n}\nprint $ans;"}, {"source_code": "#!/bin/perl\n\n$n = ;\n$cnt = 0;\n\nfor($i=0;$i<$n;$i++)\n{\n\n $s = ;\n chomp $s;\n\n ($a, $b) = split / +/, $s;\n\n if( ( $b - $a ) >= 2 )\n {\n\n $cnt++;\n\n }\n\n}\n\nprint $cnt;\n"}, {"source_code": "chomp ($n = <>);\n$ans = 0;\nforeach (1..$n) {\n\t($a, $b) = split (' ', <>);\n\tif ($b - $a >= 2) {\n\t\t$ans++;\n\t}\n}\nprint ($ans);"}, {"source_code": "<>;\n(($p,$q)=split/ /,$_),$q-$p>1 and $a++ for<>;\nprint 0+$a"}], "negative_code": [{"source_code": "use strict;\nmy $t = <>;\nmy $cnt = 0;\nwhile($t --) {\n\n\tchomp (my $line = <>) ;\n\tmy ($a,$b) = split / /, $line;\n\t$cnt += $a < $b;\n}\nprint \"$cnt\\n\";\n"}], "src_uid": "2a6c457012f7ceb589b3dea6b889f7cb"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of cookie bags Anna and Maria have. The second line contains n integers ai (1\u2009\u2264\u2009ai\u2009\u2264\u2009100) \u2014 the number of cookies in the i-th bag.", "output_spec": "Print in the only line the only number \u2014 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 \u2014 5\u2009+\u20093\u2009=\u20098 ways in total.In the third sample, no matter which bag with two cookies Olga chooses, the twins are left with 2\u2009*\u20099\u2009+\u200999\u2009=\u2009117 cookies. Thus, Olga has only one option: to take the bag with 99 cookies."}, "positive_code": [{"source_code": "#perl\n$_ = ;\n@p = split(/\\D/,);\n$a = $b = 0;\nforeach $i (@p) {\n\tif ($i &1 ) {\n\t\t$a++;\n\t} else {\n\t\t$b++;\n\t}\n}\n$ans = ($a &1)? $a: $b;\nprint $ans;"}, {"source_code": "#!perl\n$_ = ;\n$scookies = ;\n@cookies = split(/\\D/,$scookies);\n$odd = $even = 0;\nforeach $i (@cookies) {\n\tif ($i % 2) {\n\t\t$odd++;\n\t} else {\n\t\t$even++;\n\t}\n}\n$result = ($odd % 2)? $odd: $even;\nprint $result;\n"}, {"source_code": "<>;\n$_=<>;\nchomp;\n@_=/\\d+/g;\n$i=$j=0;\nfor(@_){\n$_%2?($i++):($j++);\n}\nprint $i%2?($i):($j)"}], "negative_code": [{"source_code": "#!perl\n$scookies = ;\n@cookies = split(/\\D/,$scookies);\n$odd = $even = 0;\nforeach $i (@cookies) {\n\tif ($i % 2) {\n\t\t$odd++;\n\t} else {\n\t\t$even++;\n\t}\n}\n$result = ($odd % 2)? $odd: $even;\nprint $result;\n"}, {"source_code": "<>;\n$_=<>;\nchomp;\n@_=/\\d+/g;\n$i=$j=0;\nfor(@_){\n$_%2?($i++):($j++);\n}\nprint $j%2?($i):($j)"}, {"source_code": "<>;\nfor(split/ /,<>){\n$_%2?($i++):($j++);\n}\nprint $j%2?($i):($j)"}, {"source_code": "<>;\n$_=<>;\nchomp;\n@_=split/ /;\n$i=$j=0;\nfor(@_){\n$_%2?($i++):($j++);\n}\nprint $j%2?($i):($j)"}, {"source_code": "<>;\n$_=<>;\nchomp;\n@_=/\\d+/g;\n$i=$j=0;\nfor(@_){\n$_%2?($i++):($j++);\n}\nprint $j%2?($j):($i)"}], "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\u00b7f 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\u2009+\u20091, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to i\u2009+\u20091 after destruction of pi (1\u2009\u2264\u2009i\u2009\u2264\u2009t) figures, so the (pi\u2009+\u20091)-th figure to be destroyed is considered with factor equal to i\u2009+\u20091.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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of figure types. Each of the following n lines contains two integer numbers ki and ci (1\u2009\u2264\u2009ki\u2009\u2264\u2009109,\u20090\u2009\u2264\u2009ci\u2009\u2264\u20091000), separated with space \u2014 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\u2009\u2264\u2009t\u2009\u2264\u2009100) \u2014 the number that describe the factor's changes. The next line contains t integer numbers pi (1\u2009\u2264\u2009p1\u2009<\u2009p2\u2009<\u2009...\u2009<\u2009pt\u2009\u2264\u20091012), separated with spaces. Please, do not use the %lld specificator to read or write 64-bit integers in \u0421++. It is preferred to use cin, cout streams or the %I64d specificator.", "output_spec": "Print the only number \u2014 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\u00b71\u00b710\u2009=\u200930 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2\u00b72\u00b710\u2009=\u200940 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\u00b78\u2009+\u20095\u00b710)\u00b71\u2009=\u200974 points."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\nuse bigint;\n\nmy $figures = ;\nmy $total = 0;\nmy @figure = ();\nforeach(1 .. $figures){\n\t$_ = ;\n\tm/^(\\d+) (\\d+)/;\n\tpush @figure, [$1, $2];\n\t$total += $1;\n}\n@figure = sort {$a->[1] <=> $b->[1]} @figure;\n\n$_ = ;\n$_ = ;\ns/[\\r\\n]+$//;\nmy @factor = split / /, $_;\npush @factor, $total + 100;\n\nmy $ret = 0;\nmy $f = 1;\nmy $d = 0;\nmy $p = shift @factor;\nforeach(@figure){\n\tmy $k = $_->[0];\n\tmy $c = $_->[1];\n\twhile($k){\n\t\tmy $breakable = $p-$d < $k ? $p-$d : $k;\n\t\t$ret += $c * $f * $breakable;\n\t\t$k -= $breakable;\n\t\t$d += $breakable;\n\t\tif($p <= $d){\n\t\t\t$f++;\n\t\t\t$p = shift @factor;\n\t\t}\n\t}\n}\n\nprint \"$ret\\n\";\n"}], "negative_code": [], "src_uid": "bfd7aabf195321249db8760c3cb6998d"} {"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$$$) \u2014 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$$$) \u2014 the number of burles Mishka initially has.", "output_spec": "For each test case print the answer on it \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $sum = 0;\n\t\n\twhile( /../ ){\n\t\t\n\t\tmy $last = chop;\n\t\t\n\t\t$sum += $_ * 10;\n\t\t\n\t\t$_ += $last;\n\t\t}\n\t\n\t$sum += $_;\n\t\n\tprint $sum;\n\t}"}], "negative_code": [], "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$$$) \u2014 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": "use v5.20;\nsub { \n &{sub { \n my @s = map {$_ if ($_ == 1 || $_ == 0)} \n map {ord($_) - ord '0'}\n split \"\", <>;\n my $n = $#s;\n my $x = <>;\n my @ans = (((1) x $n), ((0) x $x));\n for (my $i = 0; $i < $n; $i++) {\n if ($s[$i] == 0) {\n $ans[$i - $x] = 0;\n $ans[$i + $x] = 0;\n }\n }\n my @s2 = ((1) x $n);\n for (my $i = 0; $i < $n; $i++) {\n $s2[$i] = ($ans[$i - $x] || $ans[$i + $x]);\n }\n\n if (join(\"\", @s) eq join(\"\", @s2)) {\n splice(@ans, -$x);\n say @ans;\n } else { say -1 }\n }}\n while ($_[0]--)\n} -> (my $t = <>)\n"}], "negative_code": [{"source_code": "use v5.20;\nsub { \n &{sub { \n my @s = map {$_ if ($_ == 1 || $_ == 0)} \n map {ord($_) - ord '0'}\n split \"\", <>;\n my $n = $#s;\n my $x = <>;\n my @ans = (0) x $n;\n for (my $i = 0; $i < $n; $i++) {\n if ($i - $x >= 0 && $s[$i - $x] == 1) {\n $ans[$i] = 1;\n }\n if ($i + $x < $n && $s[$i + $x] == 1) {\n $ans[$i] = 1;\n }\n }\n my @s2 = (0) x $n;\n for (my $i = 0; $i < $n; $i++) {\n if ($i - $x >= 0 && $ans[$i - $x] == 1) {\n $s2[$i] = 1;\n }\n if ($i + $x < $n && $ans[$i + $x] == 1) {\n $s2[$i] = 1;\n }\n }\n if (join(\"\", @s) eq join(\"\", @s2)) {\n say @ans;\n } else { say -1 }\n }}\n while ($_[0]--)\n} -> (my $t = <>)\n"}, {"source_code": "use v5.20;\nsub { \n &{sub { \n my @s = map {$_ if ($_ == 1 || $_ == 0)} \n map {ord($_) - ord '0'}\n split \"\", <>;\n my $n = $#s;\n my $x = <>;\n my @ans = (1) x $n;\n @s = (@s, (0) x $x);\n for (my $i = 0; $i < $n; $i++) {\n if ($s[$i - $x] == 0 && $s[$i + $x] == 0) {\n $ans[$i] = 0;\n }\n }\n @ans = (@ans, (0) x $x);\n my @s2 = (((1) x $n), ((0) x $x));\n for (my $i = 0; $i < $n; $i++) {\n $s2[$i] = ($ans[$i - $x] || $ans[$i + $x]);\n }\n say (@s2, \"|\", @ans);\n if (join(\"\", @s) eq join(\"\", @s2)) {\n splice(@ans, -$x);\n say @ans;\n } else { say -1 }\n }}\n while ($_[0]--)\n} -> (my $t = <>)\n"}, {"source_code": "use v5.20;\nsub { \n &{sub { \n my @s = map {$_ if ($_ == 1 || $_ == 0)} \n map {ord($_) - ord '0'}\n split \"\", <>;\n my $n = $#s;\n my $x = <>;\n my @ans = (1) x $n;\n @s = (@s, (0) x $x);\n for (my $i = 0; $i < $n; $i++) {\n if ($s[$i - $x] == 0 && $s[$i + $x] == 0) {\n $ans[$i] = 0;\n }\n }\n @ans = (@ans, (0) x $x);\n my @s2 = (((1) x $n), ((0) x $x));\n for (my $i = 0; $i < $n; $i++) {\n $s2[$i] = ($ans[$i - $x] || $ans[$i + $x]);\n }\n if (join(\"\", @s) eq join(\"\", @s2)) {\n splice(@ans, -$x);\n say @ans;\n } else { say -1 }\n }}\n while ($_[0]--)\n} -> (my $t = <>)\n"}], "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\u2009\u2264\u2009n\u2009\u2264\u20093000), 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\u2009=\u2009sj\u2009=\u2009\"L\" and i\u2009<\u2009j, then there exists such k that i\u2009<\u2009k\u2009<\u2009j and sk\u2009=\u2009\"R\"; if si\u2009=\u2009sj\u2009=\u2009\"R\" and i\u2009<\u2009j, then there exists such k that i\u2009<\u2009k\u2009<\u2009j and sk\u2009=\u2009\"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": "<>;\n$_=<>;\ns/^\\.*L|R\\.*$//g;\ns/R\\.*L/length($&)%2?1:''/ge;\nprint -1+length\n"}, {"source_code": "<>;\n$_=<>;\ns/^\\.*L|R\\.*$//g;\ns/R\\.*L/length($&)%2?1:''/ge;\nprint -1+length\n"}, {"source_code": "<>;\n$_=<>;\ns/^\\.*L|R\\.*$//g;\ns/R\\.*L/length($&)%2?1:''/ge;\nprint -1+length\n"}, {"source_code": "<>;\n$_=<>;\ns/^\\.*L|R\\.*$//g;\ns/R\\.*L/length($&)%2?1:''/ge;\nprint -1+length"}, {"source_code": "<>;\n$_=<>;\ns/^\\.*L|R\\.*$//g;\ns/R\\.*L/length($&)%2?1:''/ge;\nprint -1+length\n"}, {"source_code": "<>;\n$_=<>;\ns/^\\.*L|R\\.*$//g;\ns/R\\.*L/length($&)%2?1:''/ge;\nprint -1+length\n"}, {"source_code": "sub n{print \"\\n\"}\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t$_=<>; #line\n\t\n\ts/^\\.*L//; #regexp\n\ts/R(\\.*)L/ ((length($1))%2)? 1:0 /ge;\n\ts/R\\.*//;\n\t\n\t$cnt=0;\n\t$cnt+=count(\".\",(split//,$_)); #ok\n\t$cnt+=count(\"1\",(split//,$_));\n\tprint $cnt;\n\tn();\n\t\n\t}"}, {"source_code": "sub n{print \"\\n\"}\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t$_=<>; #line\n\tchomp;\n\t\n\ts/^\\.*L//; #regexp\n\ts/RL//g;\n\ts/R\\.*L/ ((length($&))%2)? '.':'' /ge;\n\ts/R\\.*$//;\n\t\n\tprint length;\n\t#print \" $_\";\n\tn();\n\t\t\n\t}\n\t\n"}, {"source_code": "<>;\n$_=<>;\ns/^\\.*L|R\\.*$//g;\ns/R\\.*L/length($&)%2?1:''/ge;\nprint -1+length"}, {"source_code": "<>;\n$_=<>;\ns/^\\.*L|R\\.*$//g;\ns/R\\.*L/length($&)%2?1:''/ge;\nprint -1+length\n"}, {"source_code": "<>;\n$_=<>;\ns/^\\.*L|R\\.*$//g;\ns/R\\.*L/length($&)%2?1:''/ge;\nprint -1+length\n"}, {"source_code": "<>;\n$_=<>;\ns/^\\.*L|R\\.*$//g;\ns/R\\.*L/length($&)%2?1:''/ge;\nprint -1+length\n"}, {"source_code": "<>;\n$_=<>;\ns/^\\.*L|R\\.*$//g;\ns/R\\.*L/length($&)%2?1:''/ge;\nprint -1+length"}], "negative_code": [{"source_code": "sub n{print \"\\n\"}\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t$_=<>; #line\n\t\n\ts/\\.*L//; #regexp\n\ts/R(\\.*)L/ ((length($1))%2)? 1:0 /ge;\n\ts/R\\.*//;\n\t\n\t$cnt=0;\n\t$cnt+=count(\".\",(split//,$_)); #ok\n\t$cnt+=count(\"1\",(split//,$_));\n\tprint $cnt;\n\tn();\n\t\n\t}\n\t\n"}, {"source_code": "sub n{print \"\\n\"}\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t$_=<>; #line\n\tchomp;\n\t\n\ts/^\\.*L//; #regexp\n\ts/RL//g;\n\ts/R\\.*L/ ((length($&))%2)? '.':'' /ge;\n\ts/R\\.*$//;\n\t\n\tprint length;\n\tprint \" $_\";\n\tn();\n\t\t\n\t}\n\t\n"}, {"source_code": "sub n{print \"\\n\"}\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t$_=<>; #line\n\t\n\ts/\\.*L//; #regexp\n\ts/RL//g;\n\ts/R(\\.*)L/ ((length($1))%2)? 1:0 /ge;\n\ts/R\\.*//;\n\t\n\t$cnt=0;\n\t$cnt+=count(\".\",(split//,$_)); #ok\n\t$cnt+=count(\"1\",(split//,$_));\n\tprint $cnt;\n\tn();\n\t\n\t#thinking...\n\t\n\t}\n\t\n"}, {"source_code": "sub n{print \"\\n\"}\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t$_=<>; #line\n\t\n\ts/\\.*L//; #regexp\n\ts/RL//g;\n\ts/R\\.*L/ ((length($&))%2)? 1:0 /ge;\n\ts/R\\.*//;\n\t\n\t$cnt=0;\n\t$cnt+=count(\".\",(split//,$_)); #ok\n\t$cnt+=count(\"1\",(split//,$_));\n\tprint $cnt;\n\tn();\n\t\t\n\t}\n\t\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$$$) \u2014 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$$$) \u2014 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 \u2014 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 perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nmy $t = read_token();\n\nfor (1..$t) {\n my $line = read_line();\n my ($n, $x, $a, $b) = split q{ }, $line;\n ($a, $b) = ($b, $a) if $a > $b;\n my $answer;\n\n my $l = $a-1;\n my $r = $n-$b;\n if ( $x >= $l + $r ) {\n $answer = $n-1;\n }\n else {\n $answer = $b-$a+$x;\n }\n say $answer;\n}\n \n \nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n"}], "negative_code": [], "src_uid": "1fd2619aabf4557093a59da804fd0e7b"} {"nl": {"description": "Aleksey has $$$n$$$ friends. He is also on a vacation right now, so he has $$$m$$$ days to play this new viral cooperative game! But since it's cooperative, Aleksey will need one teammate in each of these $$$m$$$ days.On each of these days some friends will be available for playing, and all others will not. On each day Aleksey must choose one of his available friends to offer him playing the game (and they, of course, always agree). However, if any of them happens to be chosen strictly more than $$$\\left\\lceil\\dfrac{m}{2}\\right\\rceil$$$ times, then all other friends are offended. Of course, Aleksey doesn't want to offend anyone.Help him to choose teammates so that nobody is chosen strictly more than $$$\\left\\lceil\\dfrac{m}{2}\\right\\rceil$$$ times.", "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\\,000$$$) standing for the number of friends and the number of days to play, respectively. The $$$i$$$-th of the following $$$m$$$ lines contains an integer $$$k_i$$$ ($$$1\\leq k_i\\leq n$$$), followed by $$$k_i$$$ distinct integers $$$f_{i1}$$$, ..., $$$f_{ik_i}$$$ ($$$1\\leq f_{ij}\\leq n$$$), separated by spaces\u00a0\u2014 indices of available friends on the day $$$i$$$. It is guaranteed that the sums of $$$n$$$ and $$$m$$$ over all test cases do not exceed $$$100\\,000$$$. It's guaranteed that the sum of all $$$k_i$$$ over all days of all test cases doesn't exceed $$$200\\,000$$$.", "output_spec": "Print an answer for each test case. If there is no way to achieve the goal, print \"NO\". Otherwise, in the first line print \"YES\", and in the second line print $$$m$$$ space separated integers $$$c_1$$$, ..., $$$c_m$$$. Each $$$c_i$$$ must denote the chosen friend on day $$$i$$$ (and therefore must be one of $$$f_{ij}$$$). No value must occur more than $$$\\left\\lceil\\dfrac{m}{2}\\right\\rceil$$$ times. If there is more than one possible answer, print any of them.", "sample_inputs": ["2\n4 6\n1 1\n2 1 2\n3 1 2 3\n4 1 2 3 4\n2 2 3\n1 3\n2 2\n1 1\n1 1"], "sample_outputs": ["YES\n1 2 1 1 2 3 \nNO"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map ~~<>, 1 .. $m;\n\t\n\tmy @A = @_;\n\t\n\t@_ = grep +( split )[ 0 ] == 1, @_;\n\t\n\t$_ = ( split )[ 1 ] for @_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\tmy( $max ) = sort { $b <=> $a } values %h;\n\t\n\tif( $max > int( $m / 2 ) + $m % 2 ){\n\t\tprint 'NO';\n\t\t}\n\telse{\n\t\tprint 'YES';\n\t\t\n\t\t@_ = ();\n\t\t\n\t\tfor my $arr ( @A ){\n\t\t\tmy @B = split ' ', $arr;\n\t\t\tshift @B;\n\t\t\t\n\t\t\tif( @B == 1 ){\n\t\t\t\tpush @_, @B;\n\t\t\t\tnext;\n\t\t\t\t}\n\t\t\t\n\t\t\tfor( @B ){\n\t\t\t\tif( ( $h{ $_ } // 0 ) + 1 > int( $m / 2 ) + $m % 2 ){\n\t\t\t\t\tnext;\n\t\t\t\t\t}\n\t\t\t\t$h{ $_ } ++;\n\t\t\t\tpush @_, $_;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\tprint \"@_\";\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map ~~<>, 1 .. $m;\n\t\n\tmy @A = grep +( split )[ 0 ] != 1, @_;\n\t\n\t@_ = grep +( split )[ 0 ] == 1, @_;\n\t\n\t$_ = ( split )[ 1 ] for @_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\tmy( $max ) = sort { $b <=> $a } values %h;\n\t\n\tif( $max > int( $m / 2 ) + $m % 2 ){\n\t\tprint 'NO';\n\t\t}\n\telse{\n\t\tprint 'YES';\n\t\t\n\t\tfor my $arr ( @A ){\n\t\t\tmy @B = split ' ', $arr;\n\t\t\tshift @B;\n\t\t\t\n\t\t\tfor( @B ){\n\t\t\t\tif( ( $h{ $_ } // 0 ) + 1 > int( $m / 2 ) + $m % 2 ){\n\t\t\t\t\tnext;\n\t\t\t\t\t}\n\t\t\t\t$h{ $_ } ++;\n\t\t\t\tpush @_, $_;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\tprint \"@_\";\n\t\t}\n\t}"}], "src_uid": "1f6be4f0f7d3f9858b498aae1357c2c3"} {"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 \u2013 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,\u2009y).Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1,\u2009y1), (x2,\u2009y2), ..., (xr,\u2009yr), that: r\u2009\u2265\u20092; for any integer i (1\u2009\u2264\u2009i\u2009\u2264\u2009r\u2009-\u20091) the following equation |xi\u2009-\u2009xi\u2009+\u20091|\u2009+\u2009|yi\u2009-\u2009yi\u2009+\u20091|\u2009=\u20091 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,\u2009m,\u2009k (2\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009300; 2\u2009\u2264\u20092k\u2009\u2264\u2009n\u00b7m) \u2014 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,\u2009yi1,\u2009xi2,\u2009yi2,\u2009...,\u2009xiri,\u2009yiri (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": "($n,$m,$k)=split/ /,<>;\nfor $i(1..$n){\n\tfor $j(1..$m){\n\t\tpush @_, \" $i \". ($i % 2 ? $j : $m-$j+1)\n\t\t}\n\t}\nfor $z(2..$k){$a.=\"2\".(shift @_).(shift @_).\"\\n\"}\nprint $a,$n*$m-$k*2+2,@_\n"}, {"source_code": "\n\n($n,$m,$k)=split/ /,<>;\nfor $i(1..$n){\n\tfor $j(1..$m){\n\t\tpush @_, \" $i \". ($i % 2 ? $j : $m-$j+1)\n\t\t}\n\t}\nfor $z(2..$k){$a.=\"2\".(shift @_).(shift @_).\"\\n\"}\nprint $a,$n*$m-$k*2+2,@_"}, {"source_code": "($n,$m,$k)=split/ /,<>;\nfor $i(1..$n){\n\tfor $j(1..$m){\n\t\tpush @_, \" $i \". ($i % 2 ? $j : $m-$j+1)\n\t\t}\n\t}\nfor $z(2..$k){$a.=\"2\".(shift @_).(shift @_).\"\\n\"}\nprint $a,$n*$m-$k*2+2,@_\n"}, {"source_code": "($n,$m,$k)=split/ /,<>;\nfor $i(1..$n){\n\tfor $j(1..$m){\n\t\tpush @_, \" $i \". ($i % 2 ? $j : $m-$j+1)\n\t\t}\n\t}\nfor $z(2..$k){$a.=\"2\".(shift @_).(shift @_).\"\\n\"}\nprint $a,$n*$m-$k*2+2,@_\n"}, {"source_code": "($n,$m,$k)=split/ /,<>;\nfor $i(1..$n){\n\tfor $j(1..$m){\n\t\tpush @_, \" $i \". ($i % 2 ? $j : $m-$j+1)\n\t\t}\n\t}\nfor $z(2..$k){$a.=\"2\".(shift @_).(shift @_).\"\\n\"}\nprint $a,$n*$m-$k*2+2,@_\n"}, {"source_code": "($n,$m,$k)=split/ /,<>;\nfor $i(1..$n){\n\tfor $j(1..$m){\n\t\tpush @_, \" $i \". ($i % 2 ? $j : $m-$j+1)\n\t\t}\n\t}\nfor $z(2..$k){$a.=\"2\".(shift @_).(shift @_).\"\\n\"}\nprint $a,$n*$m-$k*2+2,@_"}, {"source_code": "($n,$m,$k)=split/ /,<>;\nfor $i(1..$n){\n\tfor $j(1..$m){\n\t\tpush @_, \" $i \". ($i % 2 ? $j : $m-$j+1)\n\t\t}\n\t}\nfor $z(2..$k){$a.=\"2\".(shift @_).(shift @_).\"\\n\"}\nprint $a,$n*$m-$k*2+2,@_\n"}, {"source_code": "($n,$m,$k)=split/ /,<>;\nfor $i(1..$n){\n\tfor $j(1..$m){\n\t\tpush @_, \" $i \". ($i % 2 ? $j : $m-$j+1)\n\t\t}\n\t}\nfor $z(2..$k){$a.=\"2\".(shift @_).(shift @_).\"\\n\"}\nprint $a,$n*$m-$k*2+2,@_\n"}, {"source_code": "($n,$m,$k)=split/ /,<>;\nfor $i(1..$n){\n\tfor $j(1..$m){\n\t\tpush @_, \" $i \". ($i % 2 ? $j : $m-$j+1)\n\t\t}\n\t}\nfor $z(2..$k){$a.=\"2\".(shift @_).(shift @_).\"\\n\"}\nprint $a,$n*$m-$k*2+2,@_\n"}, {"source_code": "($n,$m,$k)=split/ /,<>;\nfor $i(1..$n){\n\tfor $j(1..$m){\n\t\tpush @_, \" $i \". ($i % 2 ? $j : $m-$j+1)\n\t\t}\n\t}\nfor $z(2..$k){$a.=\"2\".(shift @_).(shift @_).\"\\n\"}\nprint $a,$n*$m-$k*2+2,@_"}, {"source_code": "($n,$m,$k)=split/ /,<>;\nfor $i(1..$n){\n\tfor $j(1..$m){\n\t\tpush @_, \" $i \". ($i % 2 ? $j : $m-$j+1)\n\t\t}\n\t}\nfor $z(2..$k){$a.=\"2\".(shift @_).(shift @_).\"\\n\"}\nprint $a,$n*$m-$k*2+2,@_\n"}], "negative_code": [], "src_uid": "779e73c2f5eba950a20e6af9b53a643a"} {"nl": {"description": "You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string.In a more formal way, you have to find the quantity of tuples (a,\u2009b,\u2009x,\u2009y) such that 1\u2009\u2264\u2009a\u2009\u2264\u2009b\u2009<\u2009x\u2009\u2264\u2009y\u2009\u2264\u2009|s| and substrings s[a... b], s[x... y] are palindromes.A palindrome is a string that can be read the same way from left to right and from right to left. For example, \"abacaba\", \"z\", \"abba\" are palindromes.A substring s[i... j] (1\u2009\u2264\u2009i\u2009\u2264\u2009j\u2009\u2264\u2009|s|) of string s = s1s2... s|s| is a string sisi\u2009+\u20091... sj. For example, substring s[2...4] of string s = \"abacaba\" equals \"bac\".", "input_spec": "The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters.", "output_spec": "Output a single number \u2014 the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in \u0421++. It is preferred to use cin, cout streams or the %I64d format specifier.", "sample_inputs": ["aa", "aaa", "abacaba"], "sample_outputs": ["1", "5", "36"], "notes": null}, "positive_code": [{"source_code": "chomp($str = );\n@s = split(//,$str);\n$len = length($str);\n$i = 0;\nwhile( $i < $len)\n{\n\t$begin[$i] += 1;\n\t$end[$i] += 1;\n\t$l = $i - 1;\n\t$r = $i + 1;\n\twhile($l >= 0 and $r < $len and $s[$l] eq $s[$r])\n\t{\n\t\t$begin[$l] += 1;\n\t\t$end[$r] += 1;\n\t\t$l-=1;\n\t\t$r+=1;\t\n\t}\n\t$l = $i;\n\t$r = $i + 1;\n\twhile($l >=0 and $r < $len and $s[$l] eq $s[$r])\n\t{\n\t\t$begin[$l] += 1;\n\t\t$end[$r] += 1;\n\t\t$l-=1;\n\t\t$r+=1;\n\t}\n\t$i += 1;\n}\n$ans = 0;\n$i = 0;\nwhile($i < $len)\n{\n\t$j = $i + 1;\n\twhile($j < $len)\n\t{\n\t\t$ans = $ans + $end[$i] * $begin[$j];\n\t\t$j += 1;\n\t}\n\t$i += 1;\n}\nprint $ans;"}], "negative_code": [], "src_uid": "1708818cf66de9fa03439f608c897a90"} {"nl": {"description": "Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each element except the last has exactly one next element. The list is not closed in a cycle.In this problem you are given n memory cells forming one or more doubly linked lists. Each cell contains information about element from some list. Memory cells are numbered from 1 to n.For each cell i you are given two values: li \u2014 cell containing previous element for the element in the cell i; ri \u2014 cell containing next element for the element in the cell i. If cell i contains information about the element which has no previous element then li\u2009=\u20090. Similarly, if cell i contains information about the element which has no next element then ri\u2009=\u20090. Three lists are shown on the picture. For example, for the picture above the values of l and r are the following: l1\u2009=\u20094, r1\u2009=\u20097; l2\u2009=\u20095, r2\u2009=\u20090; l3\u2009=\u20090, r3\u2009=\u20090; l4\u2009=\u20096, r4\u2009=\u20091; l5\u2009=\u20090, r5\u2009=\u20092; l6\u2009=\u20090, r6\u2009=\u20094; l7\u2009=\u20091, r7\u2009=\u20090.Your task is to unite all given lists in a single list, joining them to each other in any order. In particular, if the input data already contains a single list, then there is no need to perform any actions. Print the resulting list in the form of values li, ri.Any other action, other than joining the beginning of one list to the end of another, can not be performed.", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of memory cells where the doubly linked lists are located. Each of the following n lines contains two integers li, ri (0\u2009\u2264\u2009li,\u2009ri\u2009\u2264\u2009n) \u2014 the cells of the previous and the next element of list for cell i. Value li\u2009=\u20090 if element in cell i has no previous element in its list. Value ri\u2009=\u20090 if element in cell i has no next element in its list. It is guaranteed that the input contains the correct description of a single or more doubly linked lists. All lists have linear structure: each element of list except the first has exactly one previous element; each element of list except the last has exactly one next element. Each memory cell contains information about one element from some list, each element of each list written in one of n given cells.", "output_spec": "Print n lines, the i-th line must contain two integers li and ri \u2014 the cells of the previous and the next element of list for cell i after all lists from the input are united in a single list. If there are many solutions print any of them.", "sample_inputs": ["7\n4 7\n5 0\n0 0\n6 1\n0 2\n0 4\n1 0"], "sample_outputs": ["4 7\n5 6\n0 5\n6 1\n3 2\n2 4\n1 0"], "notes": null}, "positive_code": [{"source_code": "$n = <>;\nchomp;\nfor ($i = 0; $i < $n; $i++) {\n\t($a, $b) = split(' ', <>);\n\t$prev[$i+1] = $a;\n\t$nxt[$i+1] = $b;\n\tif ($prev[$i + 1] == 0) {\n\t\t$start = $i + 1;\n\t}\n}\n\n\n\n$ok = 1;\nwhile ($ok) {\n\t($a, $b) = (0, $start);\n\t$len = 0;\n\twhile ($b != 0) {\n\t\t($a, $b) = ($b, $nxt[$b]);\n\t\t$len++;\n\t}\n\tif ($len == $n) {\n\t\t$ok = 0;\n\t}\n\tif ($ok) {\n\t\t$ok2 = 1;\n\t\tfor ($i = 1; $i <= $n; $i++) {\n\t\t\tif ($ok2 && $i != $start && $prev[$i] == 0) {\n\t\t\t\t$nxt[$a] = $i;\n\t\t\t\t$prev[$i] = $a;\n\t\t\t\t$ok2 = 0;\n\t\t\t}\n\t\t}\n\t}\n}\n\nfor ($i = 1; $i <= $n; $i++) {\n\tprint \"$prev[$i] $nxt[$i]\\n\";\n}\n\n\n\n"}], "negative_code": [], "src_uid": "15d73569d06968ce006faf83d7e5400c"} {"nl": {"description": "You are given the string s of length n and the numbers p,\u2009q. Split the string s to pieces of length p and q.For example, the string \"Hello\" for p\u2009=\u20092, q\u2009=\u20093 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,\u2009p,\u2009q (1\u2009\u2264\u2009p,\u2009q\u2009\u2264\u2009n\u2009\u2264\u2009100). 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 \u2014 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 \u2014 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": "#Scott Heinrich - A01273823\nuse strict;\nuse warnings;\nuse 5.010;\n\nmy $input = <>;\nchomp($input);\nmy $string = <>;\n\nmy @npq = split(\" \",$input);\nmy $n = $npq[0];\nmy $p = $npq[1];\nmy $q = $npq[2];\n\nfor (my $var = 0; $var * $p <= $n ; $var++)\n{\n if(($n - ($var* $p)) % $q == 0)\n {\n my $var2 = ($n - ($var * $p)) / $q;\n my $sub = 0;\n\n say $var + $var2;\n\n for (my $k = 0; $k < $var; $k++)\n {\n print substr($string, $sub, $p) . \"\\n\";\n $sub = $sub + $p;\n }\n for (my $k = 0; $k < $var2; $k++)\n {\n print substr($string, $sub, $q) . \"\\n\";\n $sub = $sub + $q;\n }\n\n exit;\n }\n}\n\nsay \"-1\";\n"}, {"source_code": "($n,$p,$q)=split(' ',<>);\n$ch=<>;\nchomp$ch;\n$ok=0;\nfor($i=0;$i<=$n;$i+=$p){\nfor($j=0;$j<=$n;$j+=$q){\n\nif($i+$j==$n){\n\n$ok=1;\nlast;\n}\n}\nif($ok){\nlast;\n}\n}\nif(!$ok){\nprint\"-1\";\n}\nelse{\n$var1=$i/$p;\n$var2=$j/$q;\n$var= $var1+$var2;\nprint\"$var\\n\";\n$pos=0;\nwhile($var1--){\nprint substr($ch,$pos,$p);\nprint\"\\n\";\n$pos+=$p;\n}\n\nwhile($var2--){\nprint substr($ch,$pos,$q);\nprint\"\\n\";\n$pos+=$q;\n}\n\n\n\n\n\n\n\n\n}"}, {"source_code": "use feature \":all\";\n\nwhile(<>){\n\t($n, $p, $q) = split;\n\t$_ = <>;\n\n\tsay !/^\n\t\t( (?: (.{$p}) )* )\n\t\t( (?: (.{$q}) )* )\n\t\t$/x\n\t?\n\t\t\n\t\t-1\n\t:\n\t\tscalar do {\n\t\t\t($_1, $_3) = ($1, $3),\n\t\t\t@_ = (\n\t\t\t\t$_1 =~ /.{$p}/g,\n\t\t\t\t$_3 =~ /.{$q}/g,\n\t\t\t),\n\t\t\tjoin \"\\n\", 0 + @_, @_\n\t\t}\n\t\n\t}"}], "negative_code": [{"source_code": "#Scott Heinrich - A01273823\nuse strict;\nuse warnings;\nuse 5.010;\n\nmy $input = <>;\nchomp($input);\nmy $string = <>;\nchomp($string);\n\nmy @npq = split(\" \",$input);\nmy $n = $npq[0];\nmy $p = $npq[1];\nmy $q = $npq[2];\n\nfor (my $var = 0; $var * $p <= $n ; $var++)\n{\n if(($n - ($var* $p)) % $q == 0)\n {\n my $var2 = ($n - ($var * $p)) / $q;\n my $sub = 0;\n\n for (my $k = 0; $k < $var; $k++)\n {\n print substr($string, $sub, $p) . \"\\n\";\n $sub = $sub + $p;\n }\n for (my $k = 0; $k < $var2; $k++)\n {\n print substr($string, $sub, $q) . \"\\n\";\n $sub = $sub + $q;\n }\n\n exit;\n }\n}\n\nsay \"-1\";\n"}, {"source_code": "($n,$p,$q)=split(' ',<>);\n$ch=<>;\nchomp$ch;\n$ok=0;\nfor($i=0;$i<=$n;$i+=$p){\nfor($j=0;$j<=$n;$j+=$q){\n\nif($i+$j==$n){\n\n$ok=1;\nlast;\n}\n}\nif($ok){\nlast;\n}\n}\nif(!$ok){\nprint\"-1\";\n}\nelse{\n$var1=$i/$p;\n$var2=$j/$q;\n$var= $var1+$var2;\nprint\"$var\\n\";\n$pos=0;\nwhile($var1--){\nprint substr($ch,$pos,$p);\nprint\"\\n\";\n$pos+=$p;\n}\n$pos=0;\nwhile($var2--){\nprint substr($ch,$pos,$q);\nprint\"\\n\";\n$pos+=$q;\n}\n\n\n\n\n\n\n\n\n}"}, {"source_code": "use feature \":all\";\n\nwhile(<>){\n\t($n, $p, $q) = split;\n\t$_ = <>;\n\n\tsay !/^\n\t\t( (?: (.{$p}) )* )\n\t\t( (?: (.{$q}) )* )\n\t\t$/x\n\t?\n\t\t\n\t\t-1\n\t:\n\t\tscalar do { ($_1, $_3) = ($1, $3),\n\t\tjoin \"\\n\",\n\t\t\t( $_1 =~ /.{$p}/g ),\n\t\t\t( $_3 =~ /.{$q}/g ),\n\t\t}\n\t\n\t}"}], "src_uid": "c4da69789d875853beb4f92147825ebf"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100, 1\u2009\u2264\u2009t\u2009\u2264\u2009106) \u2014 the number of days and the time required to read the book. The second line contains n integers ai (0\u2009\u2264\u2009ai\u2009\u2264\u200986400) \u2014 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": "$t = <> =~ s/\\d+ //r;\n\n$i += $t > 0, $t += $_ - 86400 for split ' ', <>;\n\nprint $i"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $t ) = split;\n\t@_ = map { 86400 - $_ } split ' ', <>;\n\t\n\tmy $i = 0;\n\t\n\tfor( @_ ){\n\t\t$i ++;\n\t\t$t -= $_;\n\t\t$t <= 0 and last;\n\t\t}\n\t\n\tprint $i;\n\t}"}], "negative_code": [], "src_uid": "8e423e4bec2d113612a4dc445c4b86a9"} {"nl": {"description": "Polycarp was gifted an array $$$a$$$ of length $$$n$$$. Polycarp considers an array beautiful if there exists a number $$$C$$$, such that each number in the array occurs either zero or $$$C$$$ times. Polycarp wants to remove some elements from the array $$$a$$$ to make it beautiful.For example, if $$$n=6$$$ and $$$a = [1, 3, 2, 1, 4, 2]$$$, then the following options are possible to make the array $$$a$$$ array beautiful: Polycarp removes elements at positions $$$2$$$ and $$$5$$$, array $$$a$$$ becomes equal to $$$[1, 2, 1, 2]$$$; Polycarp removes elements at positions $$$1$$$ and $$$6$$$, array $$$a$$$ becomes equal to $$$[3, 2, 1, 4]$$$; Polycarp removes elements at positions $$$1, 2$$$ and $$$6$$$, array $$$a$$$ becomes equal to $$$[2, 1, 4]$$$; Help Polycarp determine the minimum number of elements to remove from the array $$$a$$$ to make it beautiful.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case consists of one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$)\u00a0\u2014 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^9$$$)\u00a0\u2014 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 one integer\u00a0\u2014 the minimum number of elements that Polycarp has to remove from the array $$$a$$$ to make it beautiful.", "sample_inputs": ["3\n6\n1 3 2 1 4 2\n4\n100 100 4 100\n8\n1 2 3 3 3 2 6 6"], "sample_outputs": ["2\n1\n2"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\t$debug and print \"keys_h:\", join ' ', sort keys %h;\n\t\n\tmy %values;\n\t\n\tmap $values{ $_ } ++, values %h;\n\t\n\t$debug and print \"keys_values: $_ --> $values{ $_ }\" for sort keys %values;\n\t\n\tmy @cand;\n\t\n\tfor my $value ( sort { $a <=> $b } keys %values ){\n\t\t$debug and print \" value:[$value]\";\n\t\t\n\t\tmy $cnt = 0;\n\t\t\n\t\tfor my $value2 ( sort { $a <=> $b } keys %values ){\n\t\t\t$debug and print \" value2:[$value2]\";\n\t\t\t\n\t\t\tnext if $value == $value2;\n\t\t\t\n\t\t\tif( $value2 < $value ){\n\t\t\t\t$cnt += $value2 * $values{ $value2 };\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$cnt += ( $value2 - $value ) * $values{ $value2 };\n\t\t\t\t}\n\t\t\t\n\t\t\t$debug and print \" cnt:[$cnt]\";\n\t\t\t}\n\t\t\n\t\t$debug and print \" cnt:[$cnt]\";\n\t\t\n\t\tpush @cand, $cnt;\n\t\t}\n\t\n\t$debug and print \"cand:[@cand]\";\n\t\n\tprint +( sort { $a <=> $b } @cand )[ 0 ];\n\t}"}], "negative_code": [], "src_uid": "aab052bf49da6528641f655342fa4848"} {"nl": {"description": "You are given a system of pipes. It consists of two rows, each row consists of $$$n$$$ pipes. The top left pipe has the coordinates $$$(1, 1)$$$ and the bottom right \u2014 $$$(2, n)$$$.There are six types of pipes: two types of straight pipes and four types of curved pipes. Here are the examples of all six types: Types of pipes You can turn each of the given pipes $$$90$$$ degrees clockwise or counterclockwise arbitrary (possibly, zero) number of times (so the types $$$1$$$ and $$$2$$$ can become each other and types $$$3, 4, 5, 6$$$ can become each other).You want to turn some pipes in a way that the water flow can start at $$$(1, 0)$$$ (to the left of the top left pipe), move to the pipe at $$$(1, 1)$$$, flow somehow by connected pipes to the pipe at $$$(2, n)$$$ and flow right to $$$(2, n + 1)$$$.Pipes are connected if they are adjacent in the system and their ends are connected. Here are examples of connected pipes: Examples of connected pipes Let's describe the problem using some example: The first example input And its solution is below: The first example answer As you can see, the water flow is the poorly drawn blue line. To obtain the answer, we need to turn the pipe at $$$(1, 2)$$$ $$$90$$$ degrees clockwise, the pipe at $$$(2, 3)$$$ $$$90$$$ degrees, the pipe at $$$(1, 6)$$$ $$$90$$$ degrees, the pipe at $$$(1, 7)$$$ $$$180$$$ degrees and the pipe at $$$(2, 7)$$$ $$$180$$$ degrees. Then the flow of water can reach $$$(2, n + 1)$$$ from $$$(1, 0)$$$.You have to answer $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 10^4$$$) \u2014 the number of queries. Then $$$q$$$ queries follow. Each query consists of exactly three lines. The first line of the query contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) \u2014 the number of pipes in each row. The next two lines contain a description of the first and the second rows correspondingly. Each row description consists of $$$n$$$ digits from $$$1$$$ to $$$6$$$ without any whitespaces between them, each digit corresponds to the type of pipe in the corresponding cell. See the problem statement to understand which digits correspond to which types of pipes. It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For the $$$i$$$-th query print the answer for it \u2014 \"YES\" (without quotes) if it is possible to turn some pipes in a way that the water flow can reach $$$(2, n + 1)$$$ from $$$(1, 0)$$$, and \"NO\" otherwise.", "sample_inputs": ["6\n7\n2323216\n1615124\n1\n3\n4\n2\n13\n24\n2\n12\n34\n3\n536\n345\n2\n46\n54"], "sample_outputs": ["YES\nYES\nYES\nNO\nYES\nNO"], "notes": "NoteThe first query from the example is described in the problem statement."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = map ~~<>, 1 .. 2;\n\tchomp @_;\n\t\n\t$debug and print for @_;\n\t\n\ty/1-6/223/ for @_;\n\t\t\n\t$debug and print for @_;\n\t\n\t$_[ 1 ] = reverse $_[ 1 ];\n\t\n\t$_ = '';\n\t\n\twhile( $_[ 0 ] =~ /./g ){\n\t\t$_ .= $& . chop $_[ 1 ];\n\t\t}\n\t\n\t$debug and print \"[$_]\";\n\t\n\tmy $fail = 0;\n\t\n\tmy $switch = 0;\n\t\n\twhile( /(.)(.)/g ){\n\t\tif( $& == 33 ){\n\t\t\t$switch ++;\n\t\t\t$switch %= 2;\n\t\t\t}\n\t\telsif( $switch == 0 and $& == 32 ){\n\t\t\t$fail = 1;\n\t\t\t}\n\t\telsif( $switch == 1 and $& == 23 ){\n\t\t\t$fail = 1;\n\t\t\t}\n\t\t}\n\t\n\tprint $fail || $switch == 0 ? \"NO\" : \"YES\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. 2;\n\t\n\ty/1-6/223/ for @_;\n\t\n\tmy $fail = 0;\n\tmy $row = 0;\n\t\n\twhile( 1 ){\n\t\t$_ = '';\n\t\t\n\t\t$_[ 0 ] =~ /./gc or last;\n\t\t$_ .= $&;\n\t\t\n\t\t$_[ 1 ] =~ /./gc;\n\t\t$_ .= $&;\n\t\t\t\n\t\t/\n\t\t\t(*F)\n\t\t|\t33 (?{ ( $row += 1 ) %= 2; })\n\t\t|\t32 (?{ $fail += $row == 0; })\n\t\t|\t23 (?{ $fail += $row == 1; })\n\t\t|\t..\n\t\t\t/x;\n\t\t}\n\t\n\tprint $fail || $row == 0 ? \"NO\" : \"YES\";\n\t}"}, {"source_code": "$\\ = $/;\n\n<>;\n \nwhile(<>){\n\t@_ = map <> =~ y/1-6/223/r, 1 .. 2;\n\t\n\t$F = 0;\n\t$row = 0;\n\t\n\twhile( $_[ 0 ] =~ /./gc ){\n\t\t$_ = $&;\n\t\t\n\t\t$_[ 1 ] =~ /./gc;\n\t\t$_ .= $&;\n\t\t\n\t\t/\n\t\t\t33 (?{ $row = 1 - $row })\n\t\t|\t32 (?{ $F += ! $row })\n\t\t|\t23 (?{ $F += $row })\n\t\t|\t..\n\t\t\t/x;\n\t\t}\n\t\n\tprint $F || ! $row ? \"NO\" : \"YES\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. 2;\n\tchomp @_;\n\t\n\t$_ = zip_strings( @_ );\n\t\n\tmy $row = 0;\n\t\n\tprint m/\n\t\t\t(?:\n\t\t\t\t(*FAIL)\n\t\t\t|\t[3-6][3-6] (?{ ( $row += 1 ) %= 2; })\n\t\t\t|\t[3-6][1-2] (?(?{ $row == 0 }) (*ACCEPT) )\n\t\t\t|\t[1-2][3-6] (?(?{ $row == 1 }) (*ACCEPT) )\n\t\t\t|\t..\n\t\t\t|\t$ (?(?{ $row == 0 }) (*ACCEPT) ) # at end of the string\n\t\t\t)\n\t\t\t(*SKIP)\n\t\t\t(*FAIL)\n\t\t\t/x ? \"NO\" : \"YES\";\n\t}\n\nsub zip_strings {\n\t$_[ 1 ] = reverse $_[ 1 ];\n\t\n\tmy $zipped = '';\n\t\n\twhile( $_[ 0 ] =~ /./g ){\n\t\t$zipped .= $& . chop $_[ 1 ];\n\t\t}\n\t\n\treturn $zipped;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = map ~~<>, 1 .. 2;\n\tchomp @_;\n\t\n\ts/1/2/g for @_;\n\ts/[56]/3/g for $_[ 0 ];\n\ts/[34]/5/g for $_[ 1 ];\n\t\n\t$debug and print for @_;\n\t\n\tmy $fail = 0;\n\t\n\t$_[ 0 ] =~ s/ ^ 2* [34] //gx or $fail = 1;\n\t\n\tmy $del = -1 + length $&;\n\t\n\t$_[ 1 ] =~ s/.{$del}//;\n\t\n\tmy $switch = 0;\n\t\n\twhile( 1 ){\n\t\tlast if $fail;\n\t\t\n\t\t$switch ++;\n\t\t$switch %= 2;\n\t\t\n\t\t$debug and print \"$switch: [$_]\" for @_;\n\t\t\n\t\t$switch == 1 and do {\n\t\t\tif( $_[ 1 ] =~ s/ ^ [56] 2* [56] //xg ){\n\t\t\t\tmy $del = -2 + length $&;\n\t\t\t\t$_[ 0 ] =~ s/.{$del}//;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$fail = 1;\n\t\t\t\t}\n\t\t\t};\n\t\t\n\t\t$switch == 0 and do {\n\t\t\tif( $_[ 0 ] =~ s/ ^ [34] 2* [34] //xg ){\n\t\t\t\tmy $del = -2 + length $&;\n\t\t\t\t$_[ 1 ] =~ s/.{$del}//;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$fail = 1;\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\t\n\tprint do { \n\t\t\tif( $_[ 1 ] =~ / ^ [56] 2* $ /x ){\n\t\t\t\t\"YES\";\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t\"NO\";\n\t\t\t\t}\n\t\t};\n\t}"}, {"source_code": "<>;\n\nwhile(<>){\n\t@_ = map <> =~ y/1-6/223/r, 1 .. 2;\n\t\n\t$F = 0;\n\t$row = 0;\n\t\n\twhile( $_[ 0 ] =~ /./gc ){\n\t\t$_ = $&;\n\t\t\n\t\t$_[ 1 ] =~ /./gc;\n\t\t$_ .= $&;\n\t\t\n\t\t/\n\t\t\t33 (?{ $row = 1 - $row })\n\t\t|\t32 (?{ $F += ! $row })\n\t\t|\t23 (?{ $F += $row })\n\t\t|\t..\n\t\t\t/x;\n\t\t}\n\t\n\tprint $F || ! $row ? \"NO\" : \"YES\";\n\t}"}], "src_uid": "f34cff4302e047b1e3bfc2c79aa57be3"} {"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\u2009\u2264\u2009n\u2009\u2264\u200910100\u2009000).", "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,\u200922,\u200933,\u2009... ,\u200988,\u200999 and 1001."}, "positive_code": [{"source_code": "$n = <>; chomp $n;\nprint $n, scalar reverse $n;"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\n\nmy $thiswontwork = <>;\nchomp $thiswontwork;\nprint $thiswontwork;\nmy $ff = reverse $thiswontwork;\nprint $ff;\nprint \"\\n\";\n"}, {"source_code": "$a = <>;\nchomp($a);\nprint $a.reverse($a);"}, {"source_code": "$_ = <>, chomp, print $_ . reverse"}], "negative_code": [], "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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse strict;\r\nuse warnings;\r\n\r\n#print \"yo\\n\";\r\n\r\nmy $n = ;\r\n\r\nmy $i = 0;\r\nwhile ($i < $n) {\r\n my $ticket = ;\r\n chomp($ticket);\r\n\r\n my $left_part = substr($ticket, 0, 1) + \r\n substr($ticket, 1, 1) + substr($ticket, 2, 1);\r\n my $right_part = substr($ticket, 3, 1) + \r\n substr($ticket, 4, 1) + substr($ticket, 5, 1);\r\n if ($left_part == $right_part) {\r\n print STDOUT \"YES\\n\";\r\n }\r\n else {\r\n print STDOUT \"NO\\n\";\r\n }\r\n $i++;\r\n}"}], "negative_code": [], "src_uid": "c7a5b4a015e28dd3759569bbdc130e93"} {"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\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u2009105;\u00a00\u2009\u2264\u2009p\u2009\u2264\u2009k). The next line will contain n space-separated distinct integers representing the content of array a: a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109).", "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": "while(<>){\n\tchomp;\n\t($n,$k,$p)=split/ /;\n\t$_=<>;\n\tchomp;\n\t@_=split/ /;\n\n\t@nel=@lyg=();\n\tfor (@_){\n\t\tif ($_%2){push @nel, $_}\n\t\telse {push @lyg, $_}\n\t\t}\n\t\n\t$ne=$k-$p;\n\n\t@b=();\n\t\n\tif ($ne>@nel){print \"NO\\n\"; next}\n\twhile($ne){\n\t\tpush @b, \"1 \".(shift @nel);\n\t\t$ne--;\n\t\t}\n\t\t\n\twhile($p){\n\t\t@lyg or last;\n\t\tpush @b, \"1 \".(shift @lyg);\n\t\t$p--;\n\t\t}\n\t\t\n\twhile($p){\n\t\t@nel>1 or last;\n\t\tpush @b, \"2 \".(shift @nel).\" \".(shift @nel);\n\t\t$p--;\n\t\t}\n\t\t\n\tif (@nel%2 or $p>0){print \"NO\\n\"}\n\telse {\n\t\tprint \"YES\\n\";\n\t\tif ($b[0]){ $b[0]=~s/\\d+/@nel+@lyg+$&/e ; $b[0].=\" @nel @lyg\" }\n\t\telse { $b[0]=(@nel+@lyg).\" @nel @lyg\" }\n\t\t$b[0]=~s/ +/ /;\n\t\t$b[0]=~s/ $//;\n\t\t\n\t\t$,=\"\\n\";\n\t\tprint @b;\n\t\t}\n\t\n\t}"}], "negative_code": [{"source_code": "while(<>){\n\tchomp;\n\t($n,$k,$p)=split/ /;\n\t$_=<>;\n\tchomp;\n\t@_=split/ /;\n\t\n\t$nel=$k-$p;\n\t@b=();\n\t$g=\"\";\n\t$ly=0;\n\t$nl=0;\n\tfor (@_){\n\t\t\n\t\tif ($_%2){\n\t\t\t\n\t\t\t$nel--;\n\t\t\tif ($nel<0){\n\t\t\t\t$g.=\" $_\";\n\t\t\t\t$nl++;\n\t\t\t\t}\n\t\t\telse {\n\t\t\t\tpush @b, \"1 $_\\n\";\n\t\t\t\t}\n\t\t\t}\n\t\telse {\n\t\t\t$p--;\n\t\t\tif ($p<0){\n\t\t\t\t$g.=\" $_\";\n\t\t\t\t$ly++;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tpush @b, \"1 $_\\n\";\n\t\t\t\t}\n\n\t\t\t}\n\t\t\n\t\t}\n\t\t\n\tif ($nl%2 or $nel>0 or $p>0){print \"NO\\n\"}\n\telse {\n\t\tprint \"YES\\n\";\n\t\t$b[0]=~s/$/$g/e;\n\t\t$b[0]=~s/1/$nl+$ly+1/e;\n\t\tprint @b;\n\t\t}\n\t\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k,$p)=split/ /;\n\t$_=<>;\n\tchomp;\n\t@_=split/ /;\n\n\t@nel=@lyg=();\n\tfor (@_){\n\t\tif ($_%2){push @nel, $_}\n\t\telse {push @lyg, $_}\n\t\t}\n\t\n\t$ne=$k-$p;\n\n\t@b=();\n\t\n\tif ($ne>@nel){print \"NO\\n\"; next}\n\twhile($ne){\n\t\tpush @b, \"1 \".(shift @nel);\n\t\t$ne--;\n\t\t}\n\t\t\n\twhile($p){\n\t\t@lyg or last;\n\t\tpush @b, \"1 \".(shift @lyg);\n\t\t$p--;\n\t\t}\n\t\t\n\twhile($p){\n\t\t@nel>1 or last;\n\t\tpush @b, \"2 \".(shift @nel).\" \".(shift @nel);\n\t\t$p--;\n\t\t}\n\t\t\n\tif (@nel%2 or $p>0){print \"NO\\n\"}\n\telse {\n\t\tprint \"YES\\n\";\n\t\tif ($b[0]){ $b[0]=~s/1/@nel+@lyg+1/e ; $b[0].=\" @nel @lyg\" }\n\t\telse { $b[0]=(@nel+@lyg).\" @nel @lyg\" }\n\t\t$b[0]=~s/ +/ /;\n\t\t$b[0]=~s/ $//;\n\t\t\n\t\t$,=\"\\n\";\n\t\tprint @b;\n\t\t}\n\t\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k,$p)=split/ /;\n\t$_=<>;\n\tchomp;\n\t@_=split/ /;\n\t\n\t$nel=$k-$p;\n\t@b=();\n\t$g=\"\";\n\t$ly=0;\n\t$nl=0;\n\tfor (@_){\n\t\t\n\t\tif ($_%2){\n\t\t\t\n\t\t\t$nel--;\n\t\t\tif ($nel<0){\n\t\t\t\t$g.=\" $_\";\n\t\t\t\t$nl++;\n\t\t\t\t}\n\t\t\telse {\n\t\t\t\tpush @b, \"1 $_\\n\";\n\t\t\t\t}\n\t\t\t}\n\t\telse {\n\t\t\t$p--;\n\t\t\tif ($p<0){\n\t\t\t\t$g.=\" $_\";\n\t\t\t\t$ly++;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tpush @b, \"1 $_\\n\";\n\t\t\t\t}\n\n\t\t\t}\n\t\t\n\t\t}\n\t\t\n\tif ($nl%2){print \"NO\\n\"}\n\telse {\n\t\tprint \"YES\\n\";\n\t\t$b[0]=~s/$/$g/e;\n\t\t$b[0]=~s/1/$nl+$ly+1/e;\n\t\tprint @b;\n\t\t}\n\t\n\t}"}], "src_uid": "5185f842c7c24d4118ae3661f4418a1d"} {"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$$$) \u2014 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 \u2014 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": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $n = );\n \nchomp (my $line = );\n\nmy @chips = split q{ }, $line;\n\nmy $odd = 0;\nmy $even = 0;\nfor (@chips) {\n $odd++ if $_ % 2 == 1;\n $even++ if $_ % 2 == 0;\n}\n\nsay min($odd, $even);\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ % 2 } ++, 0, 1, @_;\n\t\n\tprint -1 + ( sort { $a <=> $b } values %h )[ 0 ];\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ % 2 } ++, @_;\n\t\n\tprint 0 + ( sort { $a <=> $b } values %h )[ 0 ];\n\t}"}], "src_uid": "a0a6cdda2ce201767bf5418f445a44eb"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tprint s/\\B\\w\\K\\w//gr;\n\t}"}], "negative_code": [], "src_uid": "ac77e2e6c86b5528b401debe9f68fc8e"} {"nl": {"description": "There are n cities and n\u2009-\u20091 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\u2019t 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\u2009\u2264\u2009n\u2009\u2264\u2009100000)\u00a0\u2014 number of cities. Then n\u2009-\u20091 lines follow. The i-th line of these lines contains two integers ui and vi (1\u2009\u2264\u2009ui,\u2009vi\u2009\u2264\u2009n, ui\u2009\u2260\u2009vi)\u00a0\u2014 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\u00a0\u2014 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\u2009-\u20096. 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": "map {\n\t( $u, $v ) = split ' ', <>;\n\t$h{ $u }{ $v } ++;\n\t$h{ $v }{ $u } ++;\n\t} 2 .. <>;\n\t\n$d --;\ngo( 0, 1, 1 );\n\nprint $sum;\n\t\t\nsub go {\n\tmy( $p, $t, $pbb ) = @_;\n\t$d ++;\n\t\n\tmy @K = grep $_ != $p, keys %{ $h{ $t } };\n\t\n\t@K or $sum += $pbb * $d;\n\t\n\tgo( $t, $_, $pbb / @K ) for @K;\n\t\n\t$d --;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\nmy $sum;\nmy $depth;\nmy %h;\n\nwhile(<>){\n\t%h = ();\n\t\n\tmap {\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t$h{ $u }{ $v } ++;\n\t\t$h{ $v }{ $u } ++;\n\t\t} 1 .. $_ - 1;\n\t\t\n\t$sum = 0;\n\t$depth = -1;\n\tgo( 0, 1, 1 );\n\t\n\t$debug and print \"-------ANS:\";\n\tprint $sum;\n\t\n\t}\n\t\n\tsub go {\n\t\tmy( $prev, $try, $pbb ) = @_;\n\t\t$depth ++;\n\t\t$debug and print \"$depth|@_|$sum\";\n\t\t\n\t\tmy @keys = grep { $_ != $prev } keys %{ $h{ $try } };\n\t\t\n\t\tif( ! @keys ){\n\t\t\t$sum += $pbb * $depth;\n\t\t\t$debug and print \" sum: $sum\";\n\t\t\t}\n\t\t\n\t\tfor my $key ( @keys ){\n\t\t\t\n\t\t\tgo( $try, $key, $pbb / @keys );\n\t\t\t\n\t\t\t}\n\t\t\n\t\t$depth --;\n\t\t}"}], "negative_code": [], "src_uid": "adaae163882b064bf7257e82e8832ffb"} {"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$$$) \u2014 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": "#!/usr/bin/perl -w \nuse feature \":5.20.1\";\n\n$t = <>;\nwhile($t--){\n chomp($line = <>);\n ($x, $y, $a, $b) = split \" \", $line;\n $div = ($y - $x)/($a + $b);\n $piso = int($div);\n print $div == $piso ? \"$div\\n\" : \"-1\\n\";\n}"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nuse Carp;\n\n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\nsub ref_ref_scalar {\n my $ref = shift;\n return 1 if ref($ref) eq 'SCALAR';\n}\n\nsub toggle {\n my $ref = shift;\n croak \"$ref does not reference to scalar\" if !ref_ref_scalar($ref);\n\n $$ref = !$$ref;\n}\n\nsub odd {\n my $num = shift;\n return $num % 2 == 1;\n}\n\nsub even {\n my $num = shift;\n return $num % 2 == 0;\n}\n\n# solution\n\nmy $n = read_token;\n\nfor (1..$n) {\n my ($x, $y, $a, $b) = split q{ }, read_line;\n\n my $gap = $y - $x;\n\n my $sum = sum($a, $b);\n\n if ($gap % $sum == 0) {\n say $gap / $sum;\n }\n else {\n say -1;\n }\n}\n"}], "negative_code": [], "src_uid": "9afcf090806cc9c3b87120b1b61f8f17"} {"nl": {"description": "A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle\u00a0\u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the total number of islands. The second line contains n space-separated integers ai (0\u2009\u2264\u2009ai\u2009\u2264\u2009n\u2009-\u20091)\u00a0\u2014 the statue currently placed on the i-th island. If ai\u2009=\u20090, then the island has no statue. It is guaranteed that the ai are distinct. The third line contains n space-separated integers bi (0\u2009\u2264\u2009bi\u2009\u2264\u2009n\u2009-\u20091) \u2014 the desired statues of the ith island. Once again, bi\u2009=\u20090 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\nwhile(<>){\n\t@A = grep $_, split ' ', <>;\n\t@B = grep $_, split ' ', <>;\n\t\n\t$re_A = \"@A\";\n\tprint( ( join ' ', (\"@B\") x 2) =~ /$re_A/ ? 'YES' : 'NO' )\n\t}"}, {"source_code": "\tsub c { chomp @_; shift =~ s/\\b0 | 0//r }\n\t<>;\n\tprint -~ index( ( join ' ', (c(~~<>)) x 2 ), c(~~<>) ) ? 'YES' : 'NO'"}, {"source_code": "print -~ index( ( join ' ', (grep $_, split ' ', (<>,<>) ) x 2 ), (join ' ', grep $_, split ' ', <>) ) ? 'YES' : 'NO'\n"}], "negative_code": [], "src_uid": "846681af4b4b6b29d2c9c450a945de90"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the number of bishops. Each of next n lines contains two space separated integers xi and yi (1\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u20091000)\u00a0\u2014 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\u00a0\u2014 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,\u20093), (1,\u20095), (2,\u20093), (2,\u20094), (3,\u20094) and (3,\u20095). Pairs (1,\u20092), (1,\u20094), (2,\u20095) and (4,\u20095) do not attack each other because they do not share the same diagonal."}, "positive_code": [{"source_code": "use strict;\nuse warnings;\n\nwhile(<>) {\n my (%r,%l);\n for(1..$_) {\n my ($x,$y) = split \" \", <>;\n $r{$x+$y}++;\n $l{$x-$y}++;\n } \n my $res = 0; \n $res += $_ * ($_-1)/2 foreach values %r;\n $res += $_ * ($_-1)/2 foreach values %l;\n print \"$res\\n\";\n}"}, {"source_code": "#!/usr/bin/perl\n$\\ = $/;\n\nwhile (<>) {\n my (%hsh1, %hsh2);\n while ($_ --) {\n my ($x, $y) = split / /, <>;\n $hsh1{$x + $y}++;\n $hsh2{$x - $y}++;\n }\n my $sum;\n $sum += ($_ * ($_ - 1)) / 2 for (values %hsh1);\n $sum += ($_ * ($_ - 1)) / 2 for (values %hsh2);\n print $sum;\n}\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\t@plus = ();\n\t@minus = ();\n\t\n\tfor (1 .. $_){\n\t\t($x, $y) = split ' ', <>;\n\t\t$plus[$x + $y]++;\n\t\t$minus[1010 + $x - $y]++;\n\t\t}\n\t\t\n\t$sum = 0;\n\tfor (@plus, @minus){\n\t\t$_ or next;\n\t\t$sum += $_ * ($_-1) / 2\n\t\t}\n\t\n\tprint $sum\n\t}"}], "negative_code": [], "src_uid": "eb7457fe1e1b571e5ee8dd9689c7d66a"} {"nl": {"description": "You are given an array $$$a_1, a_2, \\ldots, a_n$$$ of positive integers. A good pair is a pair of indices $$$(i, j)$$$ with $$$1 \\leq i, j \\leq n$$$ such that, for all $$$1 \\leq k \\leq n$$$, the following equality holds:$$$$$$ |a_i - a_k| + |a_k - a_j| = |a_i - a_j|, $$$$$$ where $$$|x|$$$ denotes the absolute value of $$$x$$$.Find a good pair. Note that $$$i$$$ can be equal to $$$j$$$.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) \u2014 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$$$) \u2014 the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\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 single line with two space-separated indices $$$i$$$ and $$$j$$$ which form a good pair of the array. The case $$$i=j$$$ is allowed. It can be shown that such a pair always exists. If there are multiple good pairs, print any of them.", "sample_inputs": ["3\n\n3\n\n5 2 7\n\n5\n\n1 4 2 2 3\n\n1\n\n2"], "sample_outputs": ["2 3\n1 2\n1 1"], "notes": "NoteIn the first case, for $$$i = 2$$$ and $$$j = 3$$$ the equality holds true for all $$$k$$$: $$$k = 1$$$: $$$|a_2 - a_1| + |a_1 - a_3| = |2 - 5| + |5 - 7| = 5 = |2 - 7| = |a_2-a_3|$$$, $$$k = 2$$$: $$$|a_2 - a_2| + |a_2 - a_3| = |2 - 2| + |2 - 7| = 5 = |2 - 7| = |a_2-a_3|$$$, $$$k = 3$$$: $$$|a_2 - a_3| + |a_3 - a_3| = |2 - 7| + |7 - 7| = 5 = |2 - 7| = |a_2-a_3|$$$. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy( $min, $max ) = ( sort { $a <=> $b } @_ )[ 0, -1 ];\n\t\n\tmy @ans;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tif( $min == $_[ $i ] ){\n\t\t\tpush @ans, $i;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tif( $max == $_[ $i ] ){\n\t\t\tpush @ans, $i;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tprint join ' ', map $_ + 1, @ans;\n\t}"}], "negative_code": [], "src_uid": "7af4eee2e9f60283c4d99200769c77ec"} {"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 \u2014 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$$$)\u00a0\u2014 the number of grandmasters Bob asked. Then $$$n$$$ lines follow, the $$$i$$$-th of which contains a single integer $$$y_i$$$\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $sum = eval join '+', split //;\n\t\n\tprint s/0// && $sum % 3 == 0 && /[02468]/ ? 'red' : 'cyan';\n\t}"}], "negative_code": [], "src_uid": "5bc07d2efb7453e51f4931cc7ec3aac7"} {"nl": {"description": "You are given n\u2009\u00d7\u2009m 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200950)\u00a0\u2014 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 \u00a0\u2014 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) = split \" \", <>;\nfor ($i = 0; $i < $n; $i++) {\n\t@v = split \" \", <>;\n\tfor ($j = 0; $j < $m; $j++) {\n\t\tif ($v[$j]) {\n\t\t\t$r1[$i]++;\n\t\t\t$c1[$j]++;\n\t\t} else {\n\t\t\t$r0[$i]++;\n\t\t\t$c0[$j]++;\n\t\t}\n\t}\n}\n$ans = $n * $m;\nfor $i (@r1, @c1, @r0, @c0) {\n\t$ans += 2**$i - 1 - $i;\n}\nprint $ans;\n"}], "negative_code": [], "src_uid": "3b7cafc280a9b0dba567863c80b978b0"} {"nl": {"description": "Let's define $$$S(x)$$$ to be the sum of digits of number $$$x$$$ written in decimal system. For example, $$$S(5) = 5$$$, $$$S(10) = 1$$$, $$$S(322) = 7$$$.We will call an integer $$$x$$$ interesting if $$$S(x + 1) < S(x)$$$. In each test you will be given one integer $$$n$$$. Your task is to calculate the number of integers $$$x$$$ such that $$$1 \\le x \\le n$$$ and $$$x$$$ is interesting.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) \u00a0\u2014 number of test cases. Then $$$t$$$ lines follow, the $$$i$$$-th line contains one integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$) for the $$$i$$$-th test case.", "output_spec": "Print $$$t$$$ integers, the $$$i$$$-th should be the answer for the $$$i$$$-th test case.", "sample_inputs": ["5\n1\n9\n10\n34\n880055535"], "sample_outputs": ["0\n1\n1\n3\n88005553"], "notes": "NoteThe first interesting number is equal to $$$9$$$."}, "positive_code": [{"source_code": "<>;\r\nprint~~(++$_/10),\"\\n\"for<>"}, {"source_code": "<>;\r\nprint~~(++$_/10),\"\\n\"for<>"}], "negative_code": [{"source_code": "print~~((<>+1)/10)"}], "src_uid": "8e4194b356500cdaacca2b1d49c2affb"} {"nl": {"description": "You are given a positive integer $$$x$$$. Check whether the number $$$x$$$ is representable as the sum of the cubes of two positive integers.Formally, you need to check if there are two integers $$$a$$$ and $$$b$$$ ($$$1 \\le a, b$$$) such that $$$a^3+b^3=x$$$.For example, if $$$x = 35$$$, then the numbers $$$a=2$$$ and $$$b=3$$$ are suitable ($$$2^3+3^3=8+27=35$$$). If $$$x=4$$$, then no pair of numbers $$$a$$$ and $$$b$$$ is suitable.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$)\u00a0\u2014 the number of test cases. Then $$$t$$$ test cases follow. Each test case contains one integer $$$x$$$ ($$$1 \\le x \\le 10^{12}$$$). Please note, that the input 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.", "output_spec": "For each test case, output on a separate line: \"YES\" if $$$x$$$ is representable as the sum of the cubes of two positive integers. \"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": ["7\n1\n2\n4\n34\n35\n16\n703657519796"], "sample_outputs": ["NO\nYES\nNO\nNO\nYES\nYES\nYES"], "notes": "NoteThe number $$$1$$$ is not representable as the sum of two cubes.The number $$$2$$$ is represented as $$$1^3+1^3$$$.The number $$$4$$$ is not representable as the sum of two cubes.The number $$$34$$$ is not representable as the sum of two cubes.The number $$$35$$$ is represented as $$$2^3+3^3$$$.The number $$$16$$$ is represented as $$$2^3+2^3$$$.The number $$$703657519796$$$ is represented as $$$5779^3+7993^3$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy @cubes;\nmy %h;\n\nfor my $i ( 1 .. 1e4 ){\n\tpush @cubes, $i ** 3;\n\t$h{ $cubes[ -1 ] } = 1;\n\t}\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $find;\n\t\n\tfor my $cube ( @cubes ){\n\t\tlast if $cube > $_;\n\t\t\n\t\t$h{ $_ - $cube } and $find = 1 and last;\n\t\t}\n\t\n\tprint $find ? \"YES\" : \"NO\";\n\t}"}], "negative_code": [], "src_uid": "b4ca6a5ee6307ab2bcdab2ea5dd5b2b3"} {"nl": {"description": "Fox Ciel wants to write a task for a programming contest. The task is: \"You are given a simple undirected graph with n vertexes. Each its edge has unit length. You should calculate the number of shortest paths between vertex 1 and vertex 2.\"Same with some writers, she wants to make an example with some certain output: for example, her birthday or the number of her boyfriend. Can you help her to make a test case with answer equal exactly to k?", "input_spec": "The first line contains a single integer k (1\u2009\u2264\u2009k\u2009\u2264\u2009109).", "output_spec": "You should output a graph G with n vertexes (2\u2009\u2264\u2009n\u2009\u2264\u20091000). There must be exactly k shortest paths between vertex 1 and vertex 2 of the graph. The first line must contain an integer n. Then adjacency matrix G with n rows and n columns must follow. Each element of the matrix must be 'N' or 'Y'. If Gij is 'Y', then graph G has a edge connecting vertex i and vertex j. Consider the graph vertexes are numbered from 1 to n. The graph must be undirected and simple: Gii = 'N' and Gij\u2009=\u2009Gji must hold. And there must be at least one path between vertex 1 and vertex 2. It's guaranteed that the answer exists. If there multiple correct answers, you can output any of them. ", "sample_inputs": ["2", "9", "1"], "sample_outputs": ["4\nNNYY\nNNYY\nYYNN\nYYNN", "8\nNNYYYNNN\nNNNNNYYY\nYNNNNYYY\nYNNNNYYY\nYNNNNYYY\nNYYYYNNN\nNYYYYNNN\nNYYYYNNN", "2\nNY\nYN"], "notes": "NoteIn first example, there are 2 shortest paths: 1-3-2 and 1-4-2.In second example, there are 9 shortest paths: 1-3-6-2, 1-3-7-2, 1-3-8-2, 1-4-6-2, 1-4-7-2, 1-4-8-2, 1-5-6-2, 1-5-7-2, 1-5-8-2."}, "positive_code": [{"source_code": "\ufeff#!/usb/bin/env perl\n\nuse strict;\nuse warnings;\n\npackage fox_and_minimal_path;\n\n# B. Fox and Minimal path\n# time limit per test1: second\n# memory limit per test: 256 megabytes\n# input: standard input\n# output: standard output\n# \n# Fox Ciel wants to write a task for a programming contest. The task is: \"You are given a simple undirected graph with n vertexes. Each its edge has unit length. You should calculate the number of shortest paths between vertex 1 and vertex 2.\"\n# \n# Same with some writers, she wants to make an example with some certain output: for example, her birthday or the number of her boyfriend. Can you help her to make a test case with answer equal exactly to k?\n# \n# Input\n# The first line contains a single integer k (1\u2009\u2264\u2009k\u2009\u2264\u2009109).\n# \n# Output\n# You should output a graph G with n vertexes (2\u2009\u2264\u2009n\u2009\u2264\u20091000). There must be exactly k shortest paths between vertex 1 and vertex 2 of the graph.\n# \n# The first line must contain an integer n. Then adjacency matrix G with n rows and n columns must follow. Each element of the matrix must be 'N' or 'Y'. If Gij is 'Y', then graph G has a edge connecting vertex i and vertex j. Consider the graph vertexes are numbered from 1 to n.\n# \n# The graph must be undirected and simple: Gii = 'N' and Gij\u2009=\u2009Gji must hold. And there must be at least one path between vertex 1 and vertex 2. It's guaranteed that the answer exists. If there multiple correct answers, you can output any of them.\n\n# -------------------------------------------------------------------------------- \n\nsub ternary_representation {\n my ( $num ) = @_;\n my @arr;\n my $divisor = 3;\n while ( $num > 0 ) {\n push @arr, $num % 3;\n $num = int $num / 3;\n }\n return @arr;\n}\n\n# -------------------------------------------------------------------------------- \n\nsub get_matrix {\n my ( $k ) = @_;\n\n my @arr = ternary_representation( $k );\n # my $n = int( log $k / log 3 ) + 1;\n my $n = scalar @arr;\n $n += 2;\n\n my $all_digits_are_either_0_or_1 = 1;\n for my $i ( @arr ) {\n if ( 2 == $i ) {\n $all_digits_are_either_0_or_1 = 0;\n last;\n }\n }\n my $matrix = [];\n # for my $i ( 0 .. $n - 1 ) {\n # for my $j ( 0 .. $n - 1 ) {\n # $matrix->[ $i ]->[ $j ] = 0;\n # }\n # }\n\n my $next_index = 2;\n my @vertexes_to_be_connected;\n\n my $connect_new_vertexes = sub {\n my ( @new_vertexes ) = @_;\n for my $i ( @vertexes_to_be_connected ) {\n for my $j ( @new_vertexes ) {\n $matrix->[ $i ]->[ $j ] = $matrix->[ $j ]->[ $i ] = 1;\n }\n }\n };\n\n my $add_new_vertexes = sub {\n my ( $count ) = @_;\n my @new_vertexes = ( $next_index .. $next_index + $count - 1 );\n $connect_new_vertexes->( @new_vertexes );\n for my $i ( @vertexes_to_be_connected ) {\n for my $j ( @new_vertexes ) {\n $matrix->[ $i ]->[ $j ] = $matrix->[ $j ]->[ $i ] = 1;\n }\n }\n @vertexes_to_be_connected = @new_vertexes;\n $next_index += $count;\n };\n\n my $connect_final_vertex = sub {\n $connect_new_vertexes->( 1 );\n };\n\n for my $i ( 0 .. $#arr ) {\n next unless $arr[ $i ];\n @vertexes_to_be_connected = ( 0 );\n for my $j ( 0 .. $i - 1 ) {\n $add_new_vertexes->( 3 );\n }\n if ( ! $all_digits_are_either_0_or_1 ) {\n $add_new_vertexes->( $arr[ $i ] );\n }\n for my $j ( $i + 1 .. $#arr ) {\n $add_new_vertexes->( 1 );\n } \n $connect_final_vertex->();\n }\n\n if ( ! @arr ) {\n @vertexes_to_be_connected = ( 0 );\n $connect_final_vertex->();\n }\n\n return $matrix;\n}\n\n# -------------------------------------------------------------------------------- \n\nsub get_result {\n my ( $matrix ) = @_;\n\n my $result = scalar( @{ $matrix } ) . qq(\\n);\n for my $i ( 0 .. $#{ $matrix } ) {\n for my $j ( 0 .. $#{ $matrix } ) {\n if ( $matrix->[ $i ]->[ $j ] ) {\n $result .= 'Y';\n }\n else {\n $result .= 'N';\n }\n }\n $result .= qq(\\n) if $i != $#{ $matrix };\n }\n return $result;\n}\n\n# -------------------------------------------------------------------------------- \n\nsub main {\n my $k = int <>;\n print get_result( get_matrix( $k ) );\n return;\n}\n\ncaller() or __PACKAGE__->main( @ARGV );\n\n# -------------------------------------------------------------------------------- \n"}], "negative_code": [], "src_uid": "30a8d761d5b5103a5b926290634c8fbe"} {"nl": {"description": "The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0,\u2009y0) of a rectangular squared field of size x\u2009\u00d7\u2009y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly x\u00b7y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same.After placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code.Moving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it.The tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.", "input_spec": "The first line of the input contains four integers x, y, x0, y0 (1\u2009\u2264\u2009x,\u2009y\u2009\u2264\u2009500,\u20091\u2009\u2264\u2009x0\u2009\u2264\u2009x,\u20091\u2009\u2264\u2009y0\u2009\u2264\u2009y)\u00a0\u2014 the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right. The second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100\u2009000 characters and only consists of characters 'L', 'R', 'U', 'D'.", "output_spec": "Print the sequence consisting of (length(s)\u2009+\u20091) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.", "sample_inputs": ["3 4 2 2\nUURDRDRL", "2 2 2 2\nULD"], "sample_outputs": ["1 1 0 1 1 1 1 0 6", "1 1 1 1"], "notes": "NoteIn the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: ."}, "positive_code": [{"source_code": "while(<>){\n\t($X, $Y, $x, $y) = split;\n\n\t@ans = ( $A[$x][$y] = 1 );\n\n\tfor (<>=~ /./g){\n\t\t$x += /D/ - /U/;\n\t\t$y += /R/ - /L/;\n\t\t$x += ($x < 1) - ($x > $X);\n\t\t$y += ($y < 1) - ($y > $Y);\n\t\tpush @ans, 0 + ! $A[$x][$y] ++;\n\t}\n\n\t@ans[-1] += $X * $Y - grep $_, @ans;\n\tprint \"@ans\"\n\t}\n"}, {"source_code": "while(<>){\n\t($X, $Y, $x, $y) = split;\n\n\t@ans = ( $A[$x][$y] = 1 );\n\n\tfor (<>=~ /./g){\n\t\t$x += /D/ - /U/;\n\t\t$y += /R/ - /L/;\n\t\t$x += ($x < 1) - ($x > $X);\n\t\t$y += ($y < 1) - ($y > $Y);\n\t\tpush @ans, 0 + ! $A[$x][$y] ++;\n\t}\n\n\t@ans[-1] += $X * $Y - grep $_, @ans;\n\tprint \"@ans\"\n\t}\n"}, {"source_code": "while(<>){\n\t($X, $Y, $x, $y) = split;\n\n\t@ans = ( $A[$x][$y] = 1 );\n\n\tfor (<>=~ /./g){\n\t\t$x += /D/ - /U/;\n\t\t$y += /R/ - /L/;\n\t\t$x += ($x < 1) - ($x > $X);\n\t\t$y += ($y < 1) - ($y > $Y);\n\t\tpush @ans, 0 + ! $A[$x][$y] ++;\n\t}\n\n\t@ans[-1] += $X * $Y - grep $_, @ans;\n\tprint \"@ans\"\n\t}\n"}, {"source_code": "while(<>){\n\t($X, $Y, $x, $y) = split;\n\n\t@ans = ( $A[$x][$y] = 1 );\n\n\tfor (<>=~ /./g){\n\t\t$x += /D/ - /U/;\n\t\t$y += /R/ - /L/;\n\t\t$x += ($x < 1) - ($x > $X);\n\t\t$y += ($y < 1) - ($y > $Y);\n\t\tpush @ans, 0 + ! $A[$x][$y] ++;\n\t}\n\n\t@ans[-1] += $X * $Y - grep $_, @ans;\n\tprint \"@ans\"\n\t}\n"}, {"source_code": "while(<>){\n\t($X, $Y, $x, $y) = split;\n\n\t@ans = ( $A[$x][$y] = 1 );\n\n\tfor (<>=~ /./g){\n\t\t$x += /D/ - /U/;\n\t\t$y += /R/ - /L/;\n\t\t$x += ($x < 1) - ($x > $X);\n\t\t$y += ($y < 1) - ($y > $Y);\n\t\tpush @ans, 0 + ! $A[$x][$y] ++;\n\t}\n\n\t@ans[-1] += $X * $Y - grep $_, @ans;\n\tprint \"@ans\"\n\t}\n"}, {"source_code": "while(<>){\n\t($X, $Y, $x, $y) = split;\n\n\t@ans = ( $A[$x][$y] = 1 );\n\n\tfor (<>=~ /./g){\n\t\t$x += /D/ - /U/;\n\t\t$y += /R/ - /L/;\n\t\t$x += ($x < 1) - ($x > $X);\n\t\t$y += ($y < 1) - ($y > $Y);\n\t\tpush @ans, 0 + ! $A[$x][$y] ++;\n\t}\n\n\t@ans[-1] += $X * $Y - grep $_, @ans;\n\tprint \"@ans\"\n\t}\n"}, {"source_code": "while(<>){\n\t($X, $Y, $x, $y) = split;\n\n\t@ans = ( $A[$x][$y] = 1 );\n\n\tfor (<>=~ /./g){\n\t\t$x += /D/ - /U/;\n\t\t$y += /R/ - /L/;\n\t\t$x += ($x < 1) - ($x > $X);\n\t\t$y += ($y < 1) - ($y > $Y);\n\t\tpush @ans, 0 + ! $A[$x][$y] ++;\n\t}\n\n\t@ans[-1] += $X * $Y - grep $_, @ans;\n\tprint \"@ans\"\n\t}\n"}, {"source_code": "while(<>){\n\t($X, $Y, $x, $y) = split;\n\n\t@ans = ( $A[$x][$y] = 1 );\n\n\tfor (<>=~ /./g){\n\t\t$x += /D/ - /U/;\n\t\t$y += /R/ - /L/;\n\t\t$x += ($x < 1) - ($x > $X);\n\t\t$y += ($y < 1) - ($y > $Y);\n\t\tpush @ans, 0 + ! $A[$x][$y] ++;\n\t}\n\n\t@ans[-1] += $X * $Y - grep $_, @ans;\n\tprint \"@ans\"\n\t}"}, {"source_code": "while(<>){\n\t($X, $Y, $x, $y) = split;\n\t@_ =<>=~ /./g;\n\n\t$A[$x][$y] = 1;\n\t@ans = (1);\n\tfor (@_){\n\t\t$x += /D/ - /U/;\n\t\t$y += /R/ - /L/;\n\n\t\t$x < 1 and $x ++;\n\t\t$x > $X and $x --;\n\t\t$y < 1 and $y ++;\n\t\t$y > $Y and $y --;\n\n\t\tpush @ans, $A[$x][$y] ? 0 : scalar ($A[$x][$y] = 1, 1);\n\t}\n\n\t$ans[$#ans] += $X * $Y - grep $_, @ans;\n\tprint \"@ans\\n\"\n\t}"}, {"source_code": "while(<>){\n\t($X, $Y, $x, $y) = split;\n\n\t@ans = ( $A[$x][$y] = 1 );\n\n\tfor (<>=~ /./g){\n\t\t$x += /D/ - /U/;\n\t\t$y += /R/ - /L/;\n\t\t$x += ($x < 1) - ($x > $X);\n\t\t$y += ($y < 1) - ($y > $Y);\n\t\tpush @ans, 0 + ! $A[$x][$y] ++;\n\t}\n\n\t@ans[-1] += $X * $Y - grep $_, @ans;\n\tprint \"@ans\"\n\t}\n"}], "negative_code": [{"source_code": "while(<>){\n\t($X, $Y, $x, $y) = split;\n\t@_ =<>=~ /./g;\n\n\t@ans = (1);\n\tfor (@_){\n\t\tpush @ans, scalar do { if (0) { ; }\n\t\t\telsif (/D/) { $x + /D/ <= $X ? ($x ++, 1) : 0 }\n\t\t\telsif (/U/) { $x - /U/ > 0 ? ($x --, 1) : 0 }\n\t\t\telsif (/R/) { $y + /R/ <= $Y ? ($y ++, 1) : 0 }\n\t\t\telsif (/L/) { $y - /L/ > 0 ? ($y --, 1) : 0 }\n\t\t}\n\t}\n\n\t$ans[$#ans] += $X * $Y - grep $_, @ans;\n\tprint \"@ans\"\n\t}"}], "src_uid": "22bebd448f93c0ff07d2be91e873521c"} {"nl": {"description": "You have array a1,\u2009a2,\u2009...,\u2009an. Segment [l,\u2009r] (1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n) is good if ai\u2009=\u2009ai\u2009-\u20091\u2009+\u2009ai\u2009-\u20092, for all i (l\u2009+\u20092\u2009\u2264\u2009i\u2009\u2264\u2009r).Let's define len([l,\u2009r])\u2009=\u2009r\u2009-\u2009l\u2009+\u20091, len([l,\u2009r]) is the length of the segment [l,\u2009r]. Segment [l1,\u2009r1], is longer than segment [l2,\u2009r2], if len([l1,\u2009r1])\u2009>\u2009len([l2,\u2009r2]).Your task is to find a good segment of the maximum length in array a. Note that a segment of length 1 or 2 is always good.", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of elements in the array. The second line contains integers: a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009109).", "output_spec": "Print the length of the longest good segment in array a.", "sample_inputs": ["10\n1 2 3 5 8 13 21 34 55 89", "5\n1 1 1 1 1"], "sample_outputs": ["10", "2"], "notes": null}, "positive_code": [{"source_code": "chomp($n=<>);\n@a = split / /, <>;\nif ($n <= 2) {\n\tprint \"$n\\n\";\n} else {\n\t$i = 2;\n\t$l = 0;\n\t$ans = -1;\n\twhile ($i < $n) {\n\t\twhile ($i < $n && $a[$i]==$a[$i-1]+$a[$i-2]) {\n\t\t\t$i++;\n\t\t}\n\t\t$ans = $i-$l if $i-$l>$ans;\n\t\t$l = $i-1;\n\t\t$i++;\n\t}\n\tprint \"$ans\\n\";\n}"}], "negative_code": [{"source_code": "chomp($n=<>);\n@a = split / /, <>;\nif ($n <= 2) {\n\tprint \"$n\\n\";\n} else {\n\t$i = 2;\n\t$l = 0;\n\t$ans = -1;\n\twhile ($i < $n) {\n\t\twhile ($i < $n && $a[$i]==$a[$i-1]+$a[$i-2]) {\n\t\t\t$i++;\n\t\t}\n\t\t$ans = $i-$l if $i-$l>$ans;\n\t\t$l = $i;\n\t\t$i++;\n\t}\n\tprint \"$ans\\n\";\n}"}], "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$$$) \u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2000$$$) \u00a0\u2014 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$$$) \u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my @od = (); my @ev = ();\r\n for(my $i=0;$i<$n;$i++){\r\n if( $A[$i] % 2 == 0 ){\r\n push(@ev,$A[$i]);\r\n } else {\r\n push(@od,$A[$i]);\r\n }\r\n }\r\n my @ot = (@od,@ev);\r\n print ( join(' ',@ot) . \"\\n\" );\r\n}\r\n\r\nexit(0);\r\n\r\n\r\n#!/usr/bin/perl\r\nmy ($n,$m) = map {$_ - 0} split(/\\s+/o,);\r\n\r\nmy @tr = ();\r\n$#tr = $n-1;\r\nmy @par = ();\r\n$#par = $n-1;\r\n\r\nfor(my $i=0;$i<$n;$i++){\r\n $par[$i] = $i;\r\n}\r\n\r\nfor(my $i=0;$i<$m;$i++){\r\n my ($x,$y,$z) = map {$_ - 0} split(/\\s+/o,);\r\n unite($x-1,$y-1);\r\n}\r\n\r\nmy %dum = ();\r\nfor(my $i=0;$i<$n;$i++){\r\n $dum{&root($i)} = 1;\r\n}\r\nmy @d2 = keys %dum;\r\nmy $res = 1 + $#d2;\r\nprint \"$res\\n\";\r\n\r\nexit(0);\r\n\r\n\r\n###\r\n\r\n#!/usr/bin/perl\r\nuse Data::Dumper;\r\nmy ($n) = map {$_ - 0} split(/\\s+/o,);\r\n\r\nmy $f = &factr($n);\r\n\r\nmy @facts = keys %$f;\r\n\r\nmy %f0 = ();\r\n\r\nmy %oki = ();\r\n\r\nwhile(1){\r\n my $gosei = 1;\r\n foreach my $f1 (@facts){\r\n $gosei *= ( $f1 ** ( $f0{$f1} - 0 ) );\r\n }\r\n if( $gosei >= 3 ){\r\n# print \"gosei = $gosei\\n\";\r\n my $syo = $n / $gosei;\r\n if( $syo <= $gosei - 2 ){\r\n my $g1 = $gosei - 1;\r\n $oki{$g1} = 1;\r\n }\r\n }\r\n my $cu = 0;\r\n my $carry = 1;\r\n while($carry > 0){\r\n $f0{$facts[$cu]} += 1;\r\n $carry = 0;\r\n if( $f0{$facts[$cu]} > $f->{$facts[$cu]} ){\r\n $f0{$facts[$cu]} = 0;\r\n $carry = 1;\r\n $cu++;\r\n }\r\n last if( $cu > $#facts );\r\n }\r\n last if $cu > $#facts;\r\n}\r\nmy $res = 0;\r\nforeach my $o1 (keys %oki){\r\n $res += ( $o1 - 0);\r\n}\r\n#print &Dumper(\\%oki);\r\nprint \"$res\\n\";\r\nexit(0);\r\n\r\nsub factr {\r\n my $v = shift;\r\n my %f = ();\r\n if( $v<4 ){\r\n $f{$v}++;\r\n return \\%f;\r\n }\r\n for(my $i=1;$i*$i<=$v;$i+=2){\r\n my $ii = ($i == 1 ? 2 : $i);\r\n while($v % $ii == 0){\r\n $f{$ii}++;\r\n $v /= $ii;\r\n }\r\n }\r\n $f{$v}++ if $v>1;\r\n return \\%f;\r\n}\r\n\r\n#### mod_int + nCr nPr set\r\n\r\n#!/usr/bin/perl\r\n\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($n,$k) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\n&mk_fact($n+10);\r\n\r\n### write here \r\n\r\nexit(0);\r\n\r\n\r\nsub mod_pow { # ( x ** n ) % mod\r\n my ($x,$n) = @_;\r\n my $r = 1;\r\n while( $n ){\r\n $r = ( $r * $x ) % $mod if 1 & $n;\r\n $n >>= 1;\r\n $x = ( $x * $x ) % $mod;\r\n }\r\n return $r;\r\n}\r\n\r\nsub mk_fact { # make {global} n-size variable fact[] factinv[]\r\n my $n_max = shift;\r\n our @fact = (1); $#fact = $n_max;\r\n our @factinv = (1); $#factinv = $n_max;\r\n for(my $i=1;$i<=$n_max;$i++){\r\n $fact[$i] = ( $fact[$i-1] * $i ) % $mod;\r\n $factinv[$i] = &mod_pow($fact[$i],$mod-2);\r\n }\r\n}\r\n\r\nsub nCr { # calc nCr % mod\r\n my $n = shift; my $r = shift;\r\n return (( $fact[$n] * ( ( $factinv[$r] * $factinv[$n-$r] ) % $mod ) ) % $mod);\r\n}\r\n\r\nsub nPr { # calc nPr % mod\r\n my $n = shift; my $r = shift;\r\n return (( $fact[$n] * $factinv[$n-$r] ) % $mod );\r\n}\r\n\r\n#### union find set\r\n\r\n#!/usr/bin/perl\r\n\r\nmy ($n) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\n#### write here\r\n\r\nexit(0);\r\n\r\nsub mk_uf {\r\n my $n = shift;\r\n our @uf = (); $#uf = $n - 1;\r\n for(my $i=0;$i<$n;$i++){\r\n $uf[$i] = $i;\r\n }\r\n}\r\n\r\nsub root {\r\n my $x = shift;\r\n while( $uf[$x] != $x ){ $x = $uf[$x] = $uf[$uf[$x]]; }\r\n return $x;\r\n}\r\n\r\nsub unite {\r\n my $x = &root(scalar(shift)); my $y = &root(scalar(shift));\r\n $uf[$y] = $x;\r\n}\r\n\r\n\r\n\r\n"}], "negative_code": [], "src_uid": "fe2131c9228a2ec4365fdc3d0faa413a"} {"nl": {"description": "Permutation p is an ordered set of integers p1,\u2009\u2009\u2009p2,\u2009\u2009\u2009...,\u2009\u2009\u2009pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,\u2009\u2009\u2009p2,\u2009\u2009\u2009...,\u2009\u2009\u2009pn.Your task is to find such permutation p of length n, that the group of numbers |p1\u2009-\u2009p2|,\u2009|p2\u2009-\u2009p3|,\u2009...,\u2009|pn\u2009-\u20091\u2009-\u2009pn| has exactly k distinct elements.", "input_spec": "The single line of the input contains two space-separated positive integers n, k (1\u2009\u2264\u2009k\u2009<\u2009n\u2009\u2264\u2009105).", "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": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t@_=();\n\t$k-1 or @_=(1..$n) and (print \"@_\\n\") and next;\n\tfor ($k+1..$n){ push @_, $_}\n\t$j=0;\n\tfor (1..$k / 2){ ++$j; push @_, $j, $k-$j+1} \n\t$k % 2 and push @_, ++$j;\n\tprint \"@_\\n\";\n\t}"}, {"source_code": "use v5.10;\n\n($n, $k) = split / /, <>;\n($b, $e) = (1, $n);\nwhile ($k > 0) {\n\tif ($k == 1) {\n\t\tfor ($i=$b; $i<=$e; ++$i) {\n\t\t\tpush @ans, $i;\n\t\t}\n\t\tlast;\n\t} else {\n\t\tpush @ans, $b++;\n\t}\n\t--$k;\n\tif ($k == 1) {\n\t\tfor ($i=$e; $i>=$b; --$i) {\n\t\t\tpush @ans, $i;\n\t\t}\n\t\tlast;\n\t} else {\n\t\tpush @ans, $e--;\n\t}\n\t--$k;\n}\nsay join \" \", @ans;\n"}], "negative_code": [{"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t@_=();\n\tfor ($k..$n){ push @_, $_}\n\t$j=1;\n\tfor (1..$k-1){ ++$j; push @_, ($j % 2 ? $n+1 : 0)+(-1)**$j*$_}\n\tprint \"@_\\n\";\n\t}"}], "src_uid": "18b3d8f57ecc2b392c7b1708f75a477e"} {"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 \u2014 that is, one call connects exactly two people.", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009103) \u2014 the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id1,\u2009id2,\u2009...,\u2009idn (0\u2009\u2264\u2009idi\u2009\u2264\u2009109). 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 \u2014 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": "$n=<>;\n@n=split ' ',<>;\n$a{$_}++ for (@n);\n$i=0;\nundef $a{0};\nfor (values %a) {\nif ($_>2) {\nprint -1;\nexit 0;\n}\n$i++ if ($_==2);\n}\nprint $i;"}, {"source_code": "#!/usr/bin/perl\nuse warnings;\nuse utf8;\nuse strict;\n\nmy $n = ;\nchomp ($n = );\n\nmy %calls;\n\nmy @data = split(' ', $n);\n\nforeach (@data) {\n\tif ($_ != 0) {\n\t\t$calls{$_}++;\n\t}\n}\n\nmy $total = 0;\n\nforeach (values %calls) {\n\tif ($_ > 2) {\n\t\tprint \"-1\\n\";\n\t\texit(0);\n\t} if ($_ == 2) {\n\t\t$total++;\n\t}\n}\n\nprint \"$total\\n\";\n\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nmy $samount = ;\nmy $sessions = ;\nmy @sessions = split( ' ', $sessions );\nmy $pairs = 0;\nmy $impossible = 0;\nmy %temp;\nforeach ( @sessions )\n{\n\tif ( $_ != 0 ) {\n\t if ( $temp{ $_ } ) { $temp{ $_ } = $temp{ $_ } + 1 } else { $temp{ $_ } = 1 };\n\t if ( $temp{ $_ } > 2 ) { $impossible = 1; }\n\t elsif ( $temp{ $_ } == 2 ) { $pairs++; }\n\t}\n}\nprint ($impossible ? '-1' : $pairs).\"\\n\";\n"}], "negative_code": [], "src_uid": "45e51f3dee9a22cee86e1a98da519e2d"} {"nl": {"description": "Sereja loves integer sequences very much. He especially likes stairs.Sequence a1,\u2009a2,\u2009...,\u2009a|a| (|a| is the length of the sequence) is stairs if there is such index i (1\u2009\u2264\u2009i\u2009\u2264\u2009|a|), that the following condition is met: a1\u2009<\u2009a2\u2009<\u2009...\u2009<\u2009ai\u2009-\u20091\u2009<\u2009ai\u2009>\u2009ai\u2009+\u20091\u2009>\u2009...\u2009>\u2009a|a|\u2009-\u20091\u2009>\u2009a|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\u2009\u2264\u2009m\u2009\u2264\u2009105) \u2014 the number of Sereja's cards. The second line contains m integers bi (1\u2009\u2264\u2009bi\u2009\u2264\u20095000) \u2014 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": "#!/bin/perl\n\nmy ($n) = split /\\s/, <>;\nmy @in = split /\\s/, <>;\nmy @first, @second;\n@in = sort {$a <=> $b} @in;\npush @in, @in[-1];\nfor (my $i = 0; $i < $n; ++$i) {\n my $k = @in[$i];\n push @first, $k;\n if (@in[$i] == @in[$i + 1]) {\n push @second, $k;\n while ($i < $n && @in[$i] == @in[$i + 1]) {\n ++$i;\n }\n }\n}\n@second = reverse @second;\nshift @second if @second && @second[0] == @first[-1];\nprint @first + @second . \"\\n\" . join(\" \", (@first, @second)) . \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\nuse Data::Dumper;\n\nmy $line1 = <>;\nmy $line2 = <>;\nchomp($line1);\nchomp($line2);\n\nmy $m = $line1;\nmy @cards = split(/ /, $line2);\n\nmy $max_value = 0;\nmy $res_num = $m;\nmy @cards_num = ();\n\nforeach (@cards) {\n if (defined($cards_num[$_])) {\n $cards_num[$_]++;\n if ($cards_num[$_] > 2) { $res_num--; }\n } else {\n $cards_num[$_] = 1;\n }\n if ($_ > $max_value) { $max_value = $_; }\n}\n\nif ($cards_num[$max_value] > 1) { $res_num--; };\n$cards_num[$max_value] = 1;\n\nprint \"$res_num\\n\";\n$cards_num[$max_value] = 1;\nfor (my $i = 1; $i <= $max_value; $i++) {\n if (defined($cards_num[$i])) {\n print \"$i\";\n if ($res_num > 1) {\n print \" \";\n $res_num--;\n } else {\n print \"\\n\";\n exit;\n }\n if ($cards_num[$i] == 1) { $cards_num[$i] = (undef); }\n }\n}\nfor (my $i = $max_value; $i >= 1; $i--) {\n if (defined($cards_num[$i])) {\n print \"$i\";\n if ($res_num > 1) {\n print \" \";\n $res_num--;\n } else {\n print \"\\n\";\n exit;\n }\n }\n}"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nmy $m=;\nchomp($m);\nmy $in=;\nchomp($in);\nmy @arr=split(' ',$in);\n@arr=sort {$a<=>$b}@arr;\nmy @output;\n@arr=reverse(@arr);\nmy $a=$b=$arr[0];\npush @output,$arr[0];\nfor my $i (1..$m-1){\n\tif($arr[$i]<$a){\n\tunshift @output,$arr[$i];\n\t$a=$arr[$i];\n\t}\n\telsif($arr[$i]<$b)\n\t{\n\tpush @output,$arr[$i];\n\t$b=$arr[$i];\n\t}\n}\nmy $sz=scalar(@output);\nprint \"$sz\\n\";\nfor my $i (0..$sz-1)\n{\nprint \"$output[$i] \";\n}\nprint \"\\n\";\nexit;\n"}, {"source_code": "#!/usr/bin/perl\n\n#\u65b9\u91dd\n#\u591a\u5c11\u51e6\u7406\u304c\u9045\u304f\u3066\u3082\u3044\u3044\u306e\u3067\u3001\n#\u30d1\u30c3\u3068\u898b\u3066\u308f\u304b\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u76ee\u6307\u3059\u3002\n#perl -e '<>;@d=sort{$b<=>$a}split(\" \",<>);@v=($d[0]);map{($_<$v[$#v]&&push(@v,$_))or$_<$v[0]&&unshift(@v,$_)}@d;$n=@v;print $n,\"\\n\",\"@v\",\"\\n\"'\n\nuse strict;\nuse warnings;\n\nmy $num = <>;\nmy $line = <>;\nchomp($line);\n\nmy @vals = split \" \", $line;\n\n#\u964d\u9806\u3067\u30bd\u30fc\u30c8\n@vals = sort{$b <=> $a} @vals;\n\n#\u521d\u671f\u5316\u3002\u4e00\u756a\u5927\u304d\u3044\u5024\u3060\u3051\u3092\u6301\u3064\u914d\u5217\u3092\u4f5c\u308b\u3002\nmy $val = shift @vals;\nmy @answer = ($val);\n\n#\u5024\u3092\u5927\u304d\u3044\u9806\u306b\u691c\u7d22\u3057\u3001\n#\u53f3\u7aef\u3088\u308a\u5c0f\u3055\u3051\u308c\u3070\u53f3\u7aef\u306b\u7d50\u5408\u3001\n#\u5de6\u7aef\u3088\u308a\u5c0f\u3055\u3051\u308c\u3070\u5de6\u7aef\u306b\u7d50\u5408\u3092\u7e70\u308a\u8fd4\u3059\u3002\nforeach $val (@vals){\n if($val < $answer[$#answer]){\n push(@answer, $val);\n }elsif($val < $answer[0]){\n unshift(@answer, $val);\n }\n}\n\n$num = @answer;\nprint $num,\"\\n\";\nprint \"@answer\\n\";\n"}], "negative_code": [{"source_code": "#!/bin/perl\n\nmy ($n) = split / /, <>;\nmy @in = split / /, <>;\nmy @first, @second;\n@in = sort @in;\npush @in, @in[-1];\nfor (my $i = 0; $i < $n; ++$i) {\n my $k = @in[$i];\n push @first, $k;\n if (@in[$i] == @in[$i + 1]) {\n push @second, $k;\n while ($i < $n && @in[$i] == @in[$i + 1]) {\n ++$i;\n }\n }\n}\n@second = reverse @second;\nshift @second if @second && @second[0] == @first[-1];\nprint @first + @second . \"\\n\" . join(\" \", (@first, @second)) . \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\nuse Data::Dumper;\n\nmy $line1 = <>;\nmy $line2 = <>;\nchomp($line1);\nchomp($line2);\n\nmy $m = $line1;\nmy @cards = split(/ /, $line2);\n\nmy $max_value = 0;\nmy $res_num = $m;\nmy @cards_num = ();\n\nforeach (@cards) {\n if (defined($cards_num[$_])) {\n if ($cards_num[$_] > 2) { $res_num--; }\n else { $cards_num[$_] ++; }\n } else {\n $cards_num[$_] = 1;\n }\n if ($_ > $max_value) { $max_value = $_; }\n}\nif ($cards_num[$max_value] > 1) {\n $res_num--;\n $cards_num[$max_value] = 1;\n}\n\nprint \"$res_num\\n\";\nfor (my $i = 1; $i <= $max_value; $i++) {\n if (defined($cards_num[$i])) {\n print \"$i\";\n if ($res_num > 1) {\n print \" \";\n $res_num--;\n } else {\n print \"\\n\";\n exit;\n }\n if ($cards_num[$i] == 1) { $cards_num[$i] = (undef); }\n }\n}\nfor (my $i = $max_value; $i >= 1; $i--) {\n if (defined($cards_num[$i])) {\n print \"$i\";\n if ($res_num > 1) {\n print \" \";\n $res_num--;\n } else {\n print \"\\n\";\n exit;\n }\n }\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\nuse Data::Dumper;\n\nmy $line1 = <>;\nmy $line2 = <>;\nchomp($line1);\nchomp($line2);\n\nmy $m = $line1;\nmy @cards = split(/ /, $line2);\n\nmy $max_value = 0;\nmy $res_num = $m;\nmy @cards_num = ();\n\nforeach (@cards) {\n if (defined($cards_num[$_])) {\n $cards_num[$_]++;\n } else {\n $cards_num[$_] = 1;\n }\n if ($_ > $max_value) { $max_value = $_; }\n}\n$res_num = $#cards_num * 2 - 1;\n\nprint \"$res_num\\n\";\n$cards_num[$max_value] = 1;\nfor (my $i = 1; $i <= $max_value; $i++) {\n if (defined($cards_num[$i])) {\n print \"$i\";\n if ($res_num > 1) {\n print \" \";\n $res_num--;\n } else {\n print \"\\n\";\n exit;\n }\n if ($cards_num[$i] == 1) { $cards_num[$i] = (undef); }\n }\n}\nfor (my $i = $max_value; $i >= 1; $i--) {\n if (defined($cards_num[$i])) {\n print \"$i\";\n if ($res_num > 1) {\n print \" \";\n $res_num--;\n } else {\n print \"\\n\";\n exit;\n }\n }\n}"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nmy $m=;\nmy @arr;\nfor my $i (0..$m-1)\n{\n $arr[$i]=;\n chomp($arr[$i]);\n}\n@arr=sort {$a<=>$b}@arr;\nmy @output;\n@arr=reverse(@arr);\nmy $a=$b=$arr[0];\npush @output,$arr[0];\nfor my $i (1..$m-1){\n if($arr[$i]<$a){\n unshift @output,$arr[$i];\n $a=$arr[$i];\n }\n elsif($arr[$i]<$b)\n {\n push @output,$arr[$i];\n $b=$arr[$i];\n }\n}\nmy $sz=scalar(@output);\nprint \"$sz\\n\";\nfor my $i (0..$sz-1)\n{\nprint \"$output[$i] \";\n}\nprint \"\\n\";\nexit;\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nmy $m=;\nmy @arr;\nfor my $i (0..$m-1)\n{\n $arr[$i]=;\n chomp($arr[$i]);\n}\n@arr=sort {$a<=>$b}@arr;\nmy @output;\n@arr=reverse(@arr);\nmy $a=$b=$arr[0];\npush @output,$arr[0];\nfor my $i (1..$m-1){\n if($arr[$i]<$a){\n unshift @output,$arr[$i];\n $a=$arr[$i];\n }\n elsif($arr[$i]<$b)\n {\n push @output,$arr[$i];\n $b=$arr[$i];\n }\n}\nmy $sz=@output;\nprint \"$sz\\n\";\nmy $sz=@output;\nprint \"$sz\\n\";\nfor my $i (0..$#output)\n{\nprint \"$output[$i] \";\n}\nprint \"\\n\";\nexit;\n"}], "src_uid": "5c63f91eb955cb6c3172cb7c8f78976c"} {"nl": {"description": "Polycarpus is a system administrator. There are two servers under his strict guidance \u2014 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\u2009+\u2009y\u2009=\u200910;\u00a0x,\u2009y\u2009\u2265\u20090). 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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers \u2014 the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1\u2009\u2264\u2009ti\u2009\u2264\u20092;\u00a0xi,\u2009yi\u2009\u2265\u20090;\u00a0xi\u2009+\u2009yi\u2009=\u200910). If ti\u2009=\u20091, 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": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp(my $n = <>);\nmy @a = (0, 0);\nmy @b = (0, 0);\nfor (1 .. $n) {\n chomp($_ = <>);\n my ($server, $succ, $fail) = split;\n $a[$server - 1] += $succ;\n $b[$server - 1] += 10;\n}\nfor my $i (0 .. 1) {\n my $res = $a[$i] * 2 >= $b[$i] ? 'LIVE' : 'DEAD';\n print $res, \"\\n\";\n}\n"}, {"source_code": "while (<>){\n $a=$b=$A=$B=0;\n for (1..$_){\n <>=~/ (\\d+) /;\n $`-1 and ++$b and $B+=$1;\n $`-1 and next;\n ++$a and $A+=$1\n }\n print $a*5 <= $A ?\"LIVE\\n\":\"DEAD\\n\";\n print $b*5 <= $B ?\"LIVE\\n\":\"DEAD\\n\"\n }"}], "negative_code": [], "src_uid": "1d8870a705036b9820227309d74dd1e8"} {"nl": {"description": "Monocarp has forgotten the password to his mobile phone. The password consists of $$$4$$$ digits from $$$0$$$ to $$$9$$$ (note that it can start with the digit $$$0$$$).Monocarp remembers that his password had exactly two different digits, and each of these digits appeared exactly two times in the password. Monocarp also remembers some digits which were definitely not used in the password.You have to calculate the number of different sequences of $$$4$$$ digits that could be the password for Monocarp's mobile phone (i.\u2009e. these sequences should meet all constraints on Monocarp's password).", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 200$$$)\u00a0\u2014 the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \\le n \\le 8$$$)\u00a0\u2014 the number of digits for which Monocarp remembers that they were not used in the password. The second line contains $$$n$$$ different integers $$$a_1, a_2, \\dots a_n$$$ ($$$0 \\le a_i \\le 9$$$) representing the digits that were not used in the password. Note that the digits $$$a_1, a_2, \\dots, a_n$$$ are given in ascending order.", "output_spec": "For each testcase, print one integer\u00a0\u2014 the number of different $$$4$$$-digit sequences that meet the constraints.", "sample_inputs": ["2\n\n8\n\n0 1 2 4 5 6 8 9\n\n1\n\n8"], "sample_outputs": ["6\n216"], "notes": "NoteIn the first example, all possible passwords are: \"3377\", \"3737\", \"3773\", \"7337\", \"7373\", \"7733\"."}, "positive_code": [{"source_code": "<>; $i ++ & 1 && print 3 * ( 10 - split ) * ( 9 - split ) . $/ for <>"}, {"source_code": "<>;\r\n\r\nwhile(<>){\r\n\t@_ = split ' ', <>;\r\n\t\r\n\tprint 3 * ( 10 - @_ ) * ( 9 - @_ ) . $/\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy @A = '0000' .. '9999';\n@A = grep { m/(.)(.)\\1\\2/ || m/(.)(.)\\2\\1/ || m/(.)\\1(.)\\2/ } @A;\n@A = grep !m/(.)\\1{3}/, @A;\n\nprint for @_;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tprint 0 + grep { !m/[@_]/ } @A;\n\t}"}, {"source_code": "for(1..<>){$_=10-<>;print$_*3*($_-1),\" \";<>}"}, {"source_code": "for(1..<>){$_=10-<>;print$_*3*($_-1),\" \";<>}"}, {"source_code": "for(1..<>){$_=10-<>;print$_*3*($_-1),\" \";<>}"}, {"source_code": "for(1..<>){$_=10-<>;print$_*3*($_-1),\" \";<>}"}, {"source_code": "for(1..<>){$_=10-<>;print$_*3*($_-1),\" \";<>}"}, {"source_code": "for(1..<>){$_=10-<>;print$_*3*($_-1),\" \";<>}"}], "negative_code": [], "src_uid": "33e751f5716cbd666be36ab8f5e3977e"} {"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\u2009=\u2009s1s2... 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,\u2009ri (1\u2009\u2264\u2009li\u2009<\u2009ri\u2009\u2264\u2009n). The answer to the query li,\u2009ri is the number of such integers i (li\u2009\u2264\u2009i\u2009<\u2009ri), that si\u2009=\u2009si\u2009+\u20091.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\u2009\u2264\u2009n\u2009\u2264\u2009105). It is guaranteed that the given string only consists of characters \".\" and \"#\". The next line contains integer m (1\u2009\u2264\u2009m\u2009\u2264\u2009105) \u2014 the number of queries. Each of the next m lines contains the description of the corresponding query. The i-th line contains integers li,\u2009ri (1\u2009\u2264\u2009li\u2009<\u2009ri\u2009\u2264\u2009n).", "output_spec": "Print m integers \u2014 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": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($line = <>);\n$len = length $line;\n@dp = (0) x ($len+1);\n@a = split //, 'x' . $line . 'x';\n$a[$_] eq $a[$_+1] and $dp[$_]=$dp[$_-1]+1 or $dp[$_]=$dp[$_-1] foreach (1 .. $len);\nchomp($q = <>);\nwhile ($q-- > 0) {\n\t($l, $r) = split / /, <>;\n\tsay ($dp[$r-1] - $dp[$l-1]);\n}"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nchomp (my $s = <>);\n\nmy @eq = (0);\n\nfor my $i (0..length($s) - 2) {\n push @eq, $eq[-1] + (substr($s, $i, 1) eq substr($s, $i + 1, 1) ? 1 : 0);\n}\n\nmy $m = <>;\nfor (1..$m) {\n my ($l, $r) = split ' ', <>;\n say $eq[$r - 1] - $eq[$l - 1];\n}\n"}, {"source_code": "use warnings;\nuse strict;\n\nmy @a = split //, <>;\nmy @s;\nfor (0..@a-2) {\n $s[$_] = 0;\n $s[$_] += 1 if $a[$_] eq $a[$_+1];\n $s[$_] += $s[$_-1] if $_;\n}\npush @s, 0;\n\nfor (1..int <>) {\n my ($l, $r) = map {int $_-1} split (/\\s/, <>);\n print $s[$r-1] - $s[$l-1], \"\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nwhile (<>) {\n \n chomp;\n $i=0;\n @m=split//;\n for (1..@m){\n if ($m[$_] eq $m[$_-1]) {\n $m[$_-1]=$i++;\n }\n else{\n $m[$_-1]=$i;\n }\n }\n \n for (1..<>){\n $_=<>;\n / /;\n print $m[$'-1]-$m[$`-1],\"\\n\";\n \n }\n}"}], "negative_code": [], "src_uid": "b30e09449309b999473e4be6643d68cd"} {"nl": {"description": "You are given an integer $$$n$$$. You have to construct a permutation of size $$$n$$$.A permutation is an array where each integer from $$$1$$$ to $$$s$$$ (where $$$s$$$ is the size of permutation) occurs exactly once. For example, $$$[2, 1, 4, 3]$$$ is a permutation of size $$$4$$$; $$$[1, 2, 4, 5, 3]$$$ is a permutation of size $$$5$$$; $$$[1, 4, 3]$$$ is not a permutation (the integer $$$2$$$ is absent), $$$[2, 1, 3, 1]$$$ is not a permutation (the integer $$$1$$$ appears twice).A subsegment of a permutation is a contiguous subsequence of that permutation. For example, the permutation $$$[2, 1, 4, 3]$$$ has $$$10$$$ subsegments: $$$[2]$$$, $$$[2, 1]$$$, $$$[2, 1, 4]$$$, $$$[2, 1, 4, 3]$$$, $$$[1]$$$, $$$[1, 4]$$$, $$$[1, 4, 3]$$$, $$$[4]$$$, $$$[4, 3]$$$ and $$$[3]$$$.The value of the permutation is the number of its subsegments which are also permutations. For example, the value of $$$[2, 1, 4, 3]$$$ is $$$3$$$ since the subsegments $$$[2, 1]$$$, $$$[1]$$$ and $$$[2, 1, 4, 3]$$$ are permutations.You have to construct a permutation of size $$$n$$$ with minimum possible value among all permutations of size $$$n$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 48$$$) \u2014 the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th of them contains one integer $$$n$$$ ($$$3 \\le n \\le 50$$$) representing the $$$i$$$-th test case.", "output_spec": "For each test case, print $$$n$$$ integers \u2014 the permutation of size $$$n$$$ with minimum possible value. If there are multiple such permutations, print any of them.", "sample_inputs": ["2\n\n5\n\n6"], "sample_outputs": ["1 4 3 5 2\n4 1 6 2 5 3"], "notes": "NoteIn the first example, the permutation $$$[1, 4, 3, 5, 2]$$$ is one of the possible answers; its value is $$$2$$$.In the second example, the permutation $$$[4, 1, 6, 2, 5, 3]$$$ is one of the possible answers; its value is $$$2$$$."}, "positive_code": [{"source_code": "for(1..<>){print\"$_ \"for(2..<>);print\"1 \"}"}, {"source_code": "$\\ = $/;\r\n\r\n<>; print join ' ', 2 .. $_, 1 for <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tprint join ' ', 1, reverse 2 .. $_;\n\t}"}, {"source_code": "for(1..<>){print\"$_ \"for(2..<>);print\"1 \"}"}, {"source_code": "for(1..<>){print\"$_ \"for(2..<>);print\"1 \"}"}], "negative_code": [{"source_code": "for(1..<>){print\"$_ \"for(2..<>);print\"1\"}"}], "src_uid": "6fced7d8ac3dd58b1791430ada53332d"} {"nl": {"description": "It has finally been decided to build a roof over the football field in School 179. Its construction will require placing $$$n$$$ consecutive vertical pillars. Furthermore, the headmaster wants the heights of all the pillars to form a permutation $$$p$$$ of integers from $$$0$$$ to $$$n - 1$$$, where $$$p_i$$$ is the height of the $$$i$$$-th pillar from the left $$$(1 \\le i \\le n)$$$.As the chief, you know that the cost of construction of consecutive pillars is equal to the maximum value of the bitwise XOR of heights of all pairs of adjacent pillars. In other words, the cost of construction is equal to $$$\\max\\limits_{1 \\le i \\le n - 1}{p_i \\oplus p_{i + 1}}$$$, where $$$\\oplus$$$ denotes the bitwise XOR operation.Find any sequence of pillar heights $$$p$$$ of length $$$n$$$ with the smallest construction cost.In this problem, a permutation is an array consisting of $$$n$$$ distinct integers from $$$0$$$ to $$$n - 1$$$ in arbitrary order. For example, $$$[2,3,1,0,4]$$$ is a permutation, but $$$[1,0,1]$$$ is not a permutation ($$$1$$$ appears twice in the array) and $$$[1,0,3]$$$ is also not a permutation ($$$n=3$$$, but $$$3$$$ is 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 10^4$$$). Description of the test cases follows. The only line for each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) \u2014 the number of pillars for the construction of the roof. 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 $$$p_1$$$, $$$p_2$$$, $$$\\ldots$$$, $$$p_n$$$ \u2014 the sequence of pillar heights with the smallest construction cost. If there are multiple answers, print any of them.", "sample_inputs": ["4\n2\n3\n5\n10"], "sample_outputs": ["0 1\n2 0 1\n3 2 1 0 4\n4 6 3 2 0 8 9 1 7 5"], "notes": "NoteFor $$$n = 2$$$ there are $$$2$$$ sequences of pillar heights: $$$[0, 1]$$$ \u2014 cost of construction is $$$0 \\oplus 1 = 1$$$. $$$[1, 0]$$$ \u2014 cost of construction is $$$1 \\oplus 0 = 1$$$. For $$$n = 3$$$ there are $$$6$$$ sequences of pillar heights: $$$[0, 1, 2]$$$ \u2014 cost of construction is $$$\\max(0 \\oplus 1, 1 \\oplus 2) = \\max(1, 3) = 3$$$. $$$[0, 2, 1]$$$ \u2014 cost of construction is $$$\\max(0 \\oplus 2, 2 \\oplus 1) = \\max(2, 3) = 3$$$. $$$[1, 0, 2]$$$ \u2014 cost of construction is $$$\\max(1 \\oplus 0, 0 \\oplus 2) = \\max(1, 2) = 2$$$. $$$[1, 2, 0]$$$ \u2014 cost of construction is $$$\\max(1 \\oplus 2, 2 \\oplus 0) = \\max(3, 2) = 3$$$. $$$[2, 0, 1]$$$ \u2014 cost of construction is $$$\\max(2 \\oplus 0, 0 \\oplus 1) = \\max(2, 1) = 2$$$. $$$[2, 1, 0]$$$ \u2014 cost of construction is $$$\\max(2 \\oplus 1, 1 \\oplus 0) = \\max(3, 1) = 3$$$. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t$_ --;\n\t\n\t\n\tif( $_ == 2 ){\n\t\tprint '1 0 2';\n\t\tnext;\n\t\t}\n\t\n\tmy $i = 1;\n\t\n\twhile( $i * 2 < $_ + 1 ){\n\t\t$i *= 2;\n\t\t}\n\t\n\tprint join ' ', 1 .. $i - 1, 0, $i .. $_;\n\t}"}, {"source_code": "for(1..<>) {\r\n chomp (my $n = <>);\r\n my $i = 0;\r\n while(2**$i < $n){$i++;}\r\n $x = 2**($i-1);\r\n for (my $i = 1; $i < $x; $i++) {\r\n print \"$i \";\r\n }\r\n print \"0 \";\r\n for (my $i = $x; $i < $n; $i++) {\r\n print \"$i \";\r\n }\r\n print \"\\n\";\r\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t$_ --;\n\t\n\t\n\tif( $_ == 2 ){\n\t\tprint '1 0 2';\n\t\tnext;\n\t\t}\n\t\n\tmy $i = 1;\n\t\n\twhile( $i * 2 < $_ ){\n\t\t$i *= 2;\n\t\t}\n\t\n\tprint join ' ', 1 .. $i - 1, 0, $i .. $_;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t$_ --;\n\t\n\t\n\tif( $_ == 2 ){\n\t\tprint '0 2 1';\n\t\tnext;\n\t\t}\n\t\n\tmy $i = 1;\n\t\n\twhile( $i * 2 < $_ ){\n\t\t$i *= 2;\n\t\t}\n\t\n\tprint join ' ', 1 .. $i - 1, 0, $i .. $_;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t$_ --;\n\t\n\tmy $i = 1;\n\t\n\twhile( $i * 2 < $_ ){\n\t\t$i *= 2;\n\t\t}\n\t\n\tprint join ' ', 1 .. $i - 1, 0, $i .. $_;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tif( $_ == 2 ){\n\t\tprint '0 1';\n\t\tnext;\n\t\t}\n\t\n\tmy $i = 1;\n\t\n\twhile( $i * 2 < $_ ){\n\t\t$i *= 2;\n\t\t}\n\t\n\tprint join ' ', 1 .. $i - 1, 0, $i .. $_;\n\t}"}], "src_uid": "b6e758c75d0e3037a1500bbe652f6126"} {"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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 2\\cdot10^5$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/env perl\n#\nuse strict;\nuse warnings;\nuse utf8;\n\nchomp(my $t = <>);\nwhile($t > 0) {\n\tchomp(my $n = <>);\n\tmy @a = map { $_ - 0 } split /\\s+/, <>;\n\n\tmy %ca = ();\n\tmy $dr = 1;\n\tforeach my $el (@a) {\n\t\t$ca{$el}++;\n\t\tif($ca{$el} >= 3) {\n\t\t\tprint \"$el\\n\";\n\t\t\t$dr = 0;\n\t\t\tlast;\n\t\t}\n\t}\n print \"-1\\n\" if $dr;\n\t$t--;\n}\n"}], "negative_code": [], "src_uid": "7d4174e3ae76de7b1389f618eb89d334"} {"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\u2009=\u2009747747, then cnt(4)\u2009=\u20092, cnt(7)\u2009=\u20094, cnt(47)\u2009=\u20092, cnt(74)\u2009=\u20092. Petya wants the following condition to fulfil simultaneously: cnt(4)\u2009=\u2009a1, cnt(7)\u2009=\u2009a2, cnt(47)\u2009=\u2009a3, cnt(74)\u2009=\u2009a4. 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\u2009\u2264\u2009a1,\u2009a2,\u2009a3,\u2009a4\u2009\u2264\u2009106).", "output_spec": "On the single line print without leading zeroes the answer to the problem \u2014 the minimum lucky number d such, that cnt(4)\u2009=\u2009a1, cnt(7)\u2009=\u2009a2, cnt(47)\u2009=\u2009a3, cnt(74)\u2009=\u2009a4. 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": "$_ = <>;\n($a, $b, $ab, $ba) = split;\n$t = $ba > $ab || $ba == $a;\n($a, $b, $ab, $ba) = ($b, $a, $ba, $ab) if $t;\n\nif ($ab == $ba + 1) {\n $s = '47' x $ab;\n} elsif ($ab == $ba) {\n $s = '47' x $ab . '4';\n} else {\n $a = 0;\n}\n$a -= $ba;\n$b -= $ab - 1;\n\nif ($t) {\n $s =~ y/47/74/;\n ($a, $b) = ($b, $a);\n}\n\nif ($a > 0 && $b > 0) {\n $s =~ s/4/'4' x $a/e;\n $s =~ s/7(?=4*$)/'7' x $b/e;\n} else {\n $s = '-1';\n}\n\nprint $s;\n"}], "negative_code": [{"source_code": "$_ = <>;\n($a, $b, $ab, $ba) = split;\n$t = $ab < $ba || $ab == $ba && $a < $b;\nif ($t) {\n $a, $b = $b, $a;\n $ab, $ba = $ba, $ab;\n}\n\nif ($ab == $ba + 1) {\n $s = '47' x $ab;\n} elsif ($ab == $ba) {\n $s = '47' x $ab . '4';\n} else {\n $a = -1;\n}\n$a -= $ba;\n$b -= $ba - 1;\n\nif ($a > 0 && $b > 0) {\n $s =~ s/4/'4' x $a/e;\n $s =~ s/7/'7' x $b/e;\n} else {\n $s = '-1';\n}\n$s =~ y/47/74/ if $t;\nprint $s;\n"}, {"source_code": "$_ = <>;\n($a, $b, $ab, $ba) = split;\n$t = $ba > $ab || $ba == $a;\n($a, $b, $ab, $ba) = ($b, $a, $ba, $ab) if $t;\n\nif ($ab == $ba + 1) {\n $s = '47' x $ab;\n} elsif ($ab == $ba) {\n $s = '47' x $ab . '4';\n} else {\n $a = 0;\n}\n$a -= $ba;\n$b -= $ab - 1;\n\nif ($a > 0 && $b > 0) {\n $s =~ s/4/'4' x $a/e;\n $s =~ s/7(?=4*$)/'7' x $b/e;\n $s =~ y/47/74/ if $t;\n} else {\n $s = '-1';\n}\nprint $s;\n"}, {"source_code": "$_ = <>;\n($a, $b, $ab, $ba) = split;\n$t = $ab < $ba || $ab == $ba && $a < $b;\nif ($t) {\n ($a, $b) = ($b, $a);\n ($ab, $ba) = ($ba, $ab);\n}\n\nif ($ab == $ba + 1) {\n $s = '47' x $ab;\n} elsif ($ab == $ba) {\n $s = '47' x $ab . '4';\n} else {\n $a = -1;\n}\n$a -= $ba;\n$b -= $ab - 1;\n\nif ($a > 0 && $b > 0) {\n $s =~ s/4/'4' x $a/e;\n $s =~ s/7/'7' x $b/e;\n} else {\n $s = '-1';\n}\n$s =~ y/47/74/ if $t;\nprint $s;\n"}], "src_uid": "f63cef419fdfd4ab1c61ac6d7e7657a6"} {"nl": {"description": "For an array $$$a$$$ of integers let's denote its maximal element as $$$\\max(a)$$$, and minimal as $$$\\min(a)$$$. We will call an array $$$a$$$ of $$$k$$$ integers interesting if $$$\\max(a) - \\min(a) \\ge k$$$. For example, array $$$[1, 3, 4, 3]$$$ isn't interesting as $$$\\max(a) - \\min(a) = 4 - 1 = 3 < 4$$$ while array $$$[7, 3, 0, 4, 3]$$$ is as $$$\\max(a) - \\min(a) = 7 - 0 = 7 \\ge 5$$$.You are given an array $$$a$$$ of $$$n$$$ integers. Find some interesting nonempty subarray of $$$a$$$, or tell that it doesn't exist.An array $$$b$$$ is a subarray of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself.", "input_spec": "The first line contains integer number $$$t$$$ ($$$1 \\le t \\le 10\\,000$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2\\le n \\le 2\\cdot 10^5$$$)\u00a0\u2014 the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0\\le a_i \\le 10^9$$$)\u00a0\u2014 the 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, output \"NO\" in a separate line if there is no interesting nonempty subarray in $$$a$$$. Otherwise, output \"YES\" in a separate line. In the next line, output two integers $$$l$$$ and $$$r$$$ ($$$1\\le l \\le r \\le n$$$)\u00a0\u2014 bounds of the chosen subarray. If there are multiple answers, print any. You can print each letter in any case (upper or lower).", "sample_inputs": ["3\n5\n1 2 3 4 5\n4\n2 0 1 9\n2\n2019 2020"], "sample_outputs": ["NO\nYES\n1 4\nNO"], "notes": "NoteIn the second test case of the example, one of the interesting subarrays is $$$a = [2, 0, 1, 9]$$$: $$$\\max(a) - \\min(a) = 9 - 0 = 9 \\ge 4$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $ans;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tif( abs( $_[ $i ] - $_[ $i + 1 ] ) > 1 ){\n\t\t\t$ans = join ' ', $i + 1, $i + 2;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tprint defined $ans ? \"YES\\n$ans\" : \"NO\"\n\t}"}], "negative_code": [], "src_uid": "fa16d33fb9447eea06fcded0026c6e31"} {"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\u2009\u00d7\u20092 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\u2009\u00d7\u20092 square consisting of black pixels is formed.", "input_spec": "The first line of the input contains three integers n,\u2009m,\u2009k (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20091000, 1\u2009\u2264\u2009k\u2009\u2264\u2009105)\u00a0\u2014 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\u2009\u2264\u2009i\u2009\u2264\u2009n, 1\u2009\u2264\u2009j\u2009\u2264\u2009m), 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\u2009\u00d7\u20092 square consisting of black pixels is formed. If Pasha doesn't lose, that is, no 2\u2009\u00d7\u20092 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": "#!/usr/bin/perl\nuse v5.10;\n\n($n, $m, $k) = split / /, <>;\n\nsub judge {\n\tmy ($x, $y) = (shift, shift);\n\treturn $x>=0 && $x<$n && $y>=0 && $y<$m;\n}\n\npush @M, [(0) x ($m+2)] foreach (0 .. $n+1);\nforeach $i (1 .. $k) {\n\t($x, $y) = split / /, <>;\n\t$M[$x][$y] = 1;\n\t$M[$x][$y] and $M[$x-1][$y] and $M[$x][$y-1] and $M[$x-1][$y-1] and say $i and exit;\n\t$M[$x][$y] and $M[$x-1][$y] and $M[$x][$y+1] and $M[$x-1][$y+1] and say $i and exit;\n\t$M[$x][$y] and $M[$x+1][$y] and $M[$x][$y+1] and $M[$x+1][$y+1] and say $i and exit;\n\t$M[$x][$y] and $M[$x+1][$y] and $M[$x][$y-1] and $M[$x+1][$y-1] and say $i and exit;\n}\nsay 0;"}, {"source_code": "($n, $m, $k) = split \" \", <>;\nfor (<>){\n ($i, $j) = split;\n $v++;\n $A[ $i ][ $j ] = 1;\n $A = 0;\n \t$ii = $i + (.5 <=> $_ % 2),\n \t$jj = $j + (.5 <=> $_ % 3),\n \t$A ||=\n \t$A[ $ii ][ $j ] &&\n \t$A[ $i ][ $jj ] && \n \t$A[ $ii ][ $jj ]\n \tfor 0 .. 3;\n $A and $a //= $v\n }\nprint 0 + $a"}, {"source_code": "$\\ = $/;\nwhile(<>){\n ($n, $m, $k) = split;\n $a = 0;\n undef *A;\n for $_ (1 .. $k){\n ($i, $j) = split \" \", <>;\n $A[ $i ][ $j ] = 1;\n (\n $A[ $i-1 ][ $j ] and $A[ $i ][ $j-1 ] and $A[ $i-1 ][ $j-1 ] or\n $A[ $i+1 ][ $j ] and $A[ $i ][ $j+1 ] and $A[ $i+1 ][ $j+1 ] or\n $A[ $i-1 ][ $j ] and $A[ $i ][ $j+1 ] and $A[ $i-1 ][ $j+1 ] or\n $A[ $i+1 ][ $j ] and $A[ $i ][ $j-1 ] and $A[ $i+1 ][ $j-1 ]\n )\n and $a ||= $_\n }\n print $a\n }"}], "negative_code": [], "src_uid": "0dc5469831c1d5d34aa3b7b172e3237b"} {"nl": {"description": "Let us define a magic grid to be a square matrix of integers of size $$$n \\times n$$$, satisfying the following conditions. All integers from $$$0$$$ to $$$(n^2 - 1)$$$ inclusive appear in the matrix exactly once. Bitwise XOR of all elements in a row or a column must be the same for each row and column. You are given an integer $$$n$$$ which is a multiple of $$$4$$$. Construct a magic grid of size $$$n \\times n$$$.", "input_spec": "The only line of input contains an integer $$$n$$$ ($$$4 \\leq n \\leq 1000$$$). It is guaranteed that $$$n$$$ is a multiple of $$$4$$$.", "output_spec": "Print a magic grid, i.e. $$$n$$$ lines, the $$$i$$$-th of which contains $$$n$$$ space-separated integers, representing the $$$i$$$-th row of the grid. If there are multiple answers, print any. We can show that an answer always exists.", "sample_inputs": ["4", "8"], "sample_outputs": ["8 9 1 13\n3 12 7 5\n0 2 4 11\n6 10 15 14", "19 55 11 39 32 36 4 52\n51 7 35 31 12 48 28 20\n43 23 59 15 0 8 16 44\n3 47 27 63 24 40 60 56\n34 38 6 54 17 53 9 37\n14 50 30 22 49 5 33 29\n2 10 18 46 41 21 57 13\n26 42 62 58 1 45 25 61"], "notes": "NoteIn the first example, XOR of each row and each column is $$$13$$$.In the second example, XOR of each row and each column is $$$60$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile( my $n = <> ){\n\t$debug and print '-' x 15;\n\tchomp $n;\n\t\n\tmy $s = 0;\n\t\n\t@_ = ();\n#\tmy @xhor = ();\n#\tmy @xver = ();\n\t\n#\tfor my $i ( 0 .. $n ** 2 - 1 ){\n#\t\tpush @{ $_[ $i / $n ] }, sprintf \"%0${n}b\", $s;\n#\t\t$xhor[ $i / $n ] ^= $i;\n#\t\t$xver[ $i % $n ] ^= $i;\n#\t\t$s ++;\n#\t\t}\n\t\n\tfor my $i ( 0 .. $n ** 2 - 1 ){\n\t#\tpush @{ $_[ 4 * int( $i / ( 4 * $n ) ) + $i % 4 ] }, sprintf \"%0${n}b\", $s;\n\t\tpush @{ $_[ 4 * int( $i / ( 4 * $n ) ) + $i % 4 ] }, $s;\n\t#\t$xhor[ $i / $n ] ^= $i;\n\t#\t$xver[ $i % $n ] ^= $i;\n\t\t$s ++;\n\t\t}\n\t\n#\t$debug and print \"@{ $_ }\" for @_;\n\t\n#\t$debug and print \"@xhor\";\n#\t$debug and print join \" \", map { sprintf \"%0${n}b\", $_ } @xhor;\n#\t$debug and print \"@xver\";\n#\t$debug and print join \" \", map { sprintf \"%0${n}b\", $_ } @xver;\n\t\n\tprint \"@{ $_ }\" for @_;\n\t}"}, {"source_code": "$\\ = $/;\n\n$_ = <>;\n\nfor $i ( 0 .. $_ ** 2 - 1 ){\n\tpush @{ $_[ 4 * int( $i / ( 4 * $_ ) ) + $i % 4 ] }, $i;\n\t}\n\nprint \"@{ $_ }\" for @_"}], "negative_code": [{"source_code": "$_ = <>;\n\nfor $i ( 0 .. $n ** 2 - 1 ){\n\tpush @{ $_[ 4 * int( $i / ( 4 * $n ) ) + $i % 4 ] }, $i;\n\t}\n\nprint join \"\\n\", @_"}], "src_uid": "38ee7fc4cd2d019aa9e5b33d8bea4a2f"} {"nl": {"description": "There was a big bank robbery in Tablecity. In order to catch the thief, the President called none other than Albert \u2013 Tablecity\u2019s 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\u2009\u00d7\u20092 grid, where every cell represents one district. Each district has its own unique name \u201c(X,\u2009Y)\u201d, where X and Y are the coordinates of the district in the grid. The thief\u2019s movement is as Every hour the thief will leave the district (X,\u2009Y) he is currently hiding in, and move to one of the districts: (X\u2009-\u20091,\u2009Y), (X\u2009+\u20091,\u2009Y), (X\u2009-\u20091,\u2009Y\u2009-\u20091), (X\u2009-\u20091,\u2009Y\u2009+\u20091), (X\u2009+\u20091,\u2009Y\u2009-\u20091), (X\u2009+\u20091,\u2009Y\u2009+\u20091) as long as it exists in Tablecity. Below is an example of thief\u2019s 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 \u2013 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\u2019s initial position and movement. N\u2009\u2264\u20092015 1\u2009\u2264\u2009X\u2009\u2264\u20091000 1\u2009\u2264\u2009Y\u2009\u2264\u20092 ", "sample_inputs": ["\u0412 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043d\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0432\u0432\u043e\u0434\u0430-\u0432\u044b\u0432\u043e\u0434\u0430.\nThis problem doesn't have sample input and output."], "sample_outputs": ["\u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435 \u043d\u0438\u0436\u0435.\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\u2019s 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": "print 2e3, $/, map \"$_ 1 $_ 2\\n\", 1 .. 1e3, reverse 1 .. 1e3"}, {"source_code": "print 2e3, $/, map \"$_ 1 $_ 2\\n\", 1 .. 1e3, reverse 1 .. 1e3\n"}, {"source_code": "print 2e3, $/, map \"$_ 1 $_ 2\\n\", 1 .. 1e3, reverse 1 .. 1e3\n"}, {"source_code": "print 2e3, $/, map \"$_ 1 $_ 2\\n\", 1 .. 1e3, reverse 1 .. 1e3\n"}, {"source_code": "print 2e3, $/, map \"$_ 1 $_ 2\\n\", 1 .. 1e3, reverse 1 .. 1e3\n"}, {"source_code": "print 2e3, $/, map \"$_ 1 $_ 2\\n\", 1 .. 1e3, reverse 1 .. 1e3\n"}, {"source_code": "print 2e3, $/, map \"$_ 1 $_ 2\\n\", 1 .. 1e3, reverse 1 .. 1e3"}, {"source_code": "print 2e3, $/, map \"$_ 1 $_ 2\\n\", 1 .. 1e3, reverse 1 .. 1e3\n"}, {"source_code": "print 2e3, $/, map \"$_ 1 $_ 2\\n\", 1 .. 1e3, reverse 1 .. 1e3\n"}, {"source_code": "$\\ = $/;\n\n$n = 1e3;\n\nprint 2 * $n;\n\nprint join \" \", $_, 1, $_, 2 for 1 .. $n, reverse 1 .. $n"}, {"source_code": "print 2e3, $/, map \"$_ 1 $_ 2\\n\", 1 .. 1e3, reverse 1 .. 1e3"}, {"source_code": "print 2e3, $/, map \"$_ 1 $_ 2\\n\", 1 .. 1e3, reverse 1 .. 1e3\n"}], "negative_code": [{"source_code": "$\\ = $/;\n\nprint 2000;\n\nprint join $/, (join $\", $_, 1, $_, 2) x 2 for 1 .. 1000"}], "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\u2009\u2264\u2009n\u2009\u2264\u20092000)\u00a0\u2014 the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters\u00a0\u2014 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\n$_ = chop( $e = <> );\n\ns/\\n\\K/chop $e/e, s/(?=\\n)/chop $e/e while $e;\n\nprint split"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\t$e = '.';\n\t$i = 0;\n\t\n\twhile( $_ ){\n\t\t$i++ % 2 ? $e =~ s/(?=\\.)/chop $_/e : $e =~ s/\\.\\K/chop $_/e;\n\t\t\n\t\t}\n\t\n\t\n\tprint $e =~ y/.//dr\n\t}"}, {"source_code": "<>;\n\n$_ = chop( $e = <> );\n\ns/\\n\\K/chop $e/e, s/$/chop $e/em while $e;\n\nprint split"}, {"source_code": "<>;\n\n$_ = <>, chomp;\n\t\n$e = '.';\n\nwhile( $_ ){\n\t$i++ % 2 ? $e =~ s/(?=\\.)/chop/e : $e =~ s/\\.\\K/chop/e;\n\t\n\t}\n\nprint $e =~ y/.//dr"}, {"source_code": "<>;\n\nchomp($e = <>);\n\t\n$_ = '.';\n\ns/\\.\\K/chop $e/e, s/(?=\\.)/chop $e/e while $e;\n\nprint y/.//dr"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t\n\t$e = '';\n\t\n\twhile( $_ ){\n\t\t$e .= substr $_, (length) / 2, 1, '';\n\t\t\n\t\t\n\t\t}\n\t\n\t\n\tprint $e\n\t}"}, {"source_code": "<>;\n\nchomp($e = <>);\n\t\n$_ = '.';\n\ns/(?=\\.)/chop $e/e, $_ = reverse while $e;\n\nprint y/.//dr"}], "src_uid": "2a414730d1bc7eef50bdb631ea966366"} {"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\u2009+\u20091) 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\u2009\u2264\u2009t\u2009\u2264\u2009104) \u2014 the number of test cases. The second line contains t space-separated integers ni (1\u2009\u2264\u2009ni\u2009\u2264\u2009109) \u2014 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 \u0421++. 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": "#!/usr/bin/perl -w\n\nuse strict;\n\n#1\n\n#my ($a, $b) = split /\\D/, <>;\n#my $n = $b - $a * 2;\n#print $a - $n;\n\n# 2\n\n<>;\nmy @arr = split /\\D/, <>;\nfor my $i ( 0 .. @arr - 1 ) {\n\tmy $len = $arr[$i];\n\tprint lcm( $len + 1, $len * 4 ) / ( $len + 1 ) + 1, \"\\n\";\n}\n\nsub lcm {\n\tmy ( $a, $b ) = @_;\n\treturn $a / gcd( $a, $b ) * $b;\n}\n\nsub gcd {\n\tmy ( $a, $b ) = @_;\n\tmy $g = $b;\n\twhile ( $a > 0 ) {\n\t\t$g = $a;\n\t\t$a = $b % $a;\n\t\t$b = $g;\n\t}\n\treturn $g;\n}\n\n"}, {"source_code": "<>;\n$_=<>;\ns/\\d+/($&+1)%4?(($&+1)%2?($&*4+1):($&*2+1)):($&+1)/eg;\ns/ /\\n/g;\nprint"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n#my ($a, $b) = split /\\D/, <>;\n#my $n = $b - $a * 2;\n#print $a - $n;\n\nmy $n = <>;\nmy @arr = split /\\D/, <>;\nfor my $i (0..@arr-1) {\n\tprint 5 + 4 * ($arr[$i]-1), \"\\n\";\n} "}, {"source_code": "<>;\n$_=<>;\ns/\\d+/$&*4+1/eg;\ns/ /\\n/g;\nprint"}], "src_uid": "168dbc4994529f5407a440b0c71086da"} {"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)\u2009=\u2009{cu\u00a0:\u2009\u00a0cu\u2009\u2260\u2009k 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,\u2009m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009105) \u2014 the number of vertices end edges of the graph, correspondingly. The second line contains a sequence of integers c1,\u2009c2,\u2009...,\u2009cn (1\u2009\u2264\u2009ci\u2009\u2264\u2009105) \u2014 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,\u2009bi (1\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u2009n;\u00a0ai\u2009\u2260\u2009bi) \u2014 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": "#!/usr/bin/perl -w\n\nuse strict;\nuse List::Util qw(min);\n\nchomp($_ = <>);\nmy ($n, $m) = split;\nchomp($_ = <>);\nmy @c = split;\nmy %edges = ();\nmy %count = ();\n\nfor (1 .. $m) {\n chomp($_ = <>);\n\n my ($u, $v) = split;\n ($u, $v) = ($c[$u - 1], $c[$v - 1]);\n ($u, $v) = ($v, $u) if $u > $v;\n\n next if $u == $v;\n next if exists $edges{\"$u $v\"};\n\n ++$count{$u};\n ++$count{$v};\n $edges{\"$u $v\"} = 1;\n}\n\nmy $res = min @c;\nmy $cnt = -1;\nwhile (my ($key, $val) = each %count) {\n if ($val > $cnt || ($val == $cnt && $key < $res)) {\n ($res, $cnt) = ($key, $val);\n }\n}\nprint $res, \"\\n\";\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp($_ = <>);\nmy ($n, $m) = split;\nchomp($_ = <>);\nmy @c = split;\nmy %edges = ();\nmy %count = ();\n\nfor (1 .. $m) {\n chomp($_ = <>);\n\n my ($u, $v) = split;\n ($u, $v) = ($v, $u) if $u > $v;\n ($u, $v) = ($u - 1, $v - 1);\n\n next if exists $edges{\"$c[$u] $c[$v]\"};\n next if $c[$u] == $c[$v];\n\n ++$count{$c[$u]};\n ++$count{$c[$v]};\n $edges{\"$c[$u] $c[$v]\"} = 1;\n}\n\nmy ($res, $cnt) = ($c[0], -1);\nwhile (my ($key, $val) = each %count) {\n if ($val > $cnt || ($val == $cnt && $key < $res)) {\n $cnt = $val;\n $res = $key;\n }\n}\nprint $res, \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp($_ = <>);\nmy ($n, $m) = split;\nchomp($_ = <>);\nmy @c = split;\nmy %edges = ();\nmy %count = ();\n\nfor (1 .. $m) {\n chomp($_ = <>);\n\n my ($u, $v) = split;\n ($u, $v) = ($u - 1, $v - 1);\n my ($cu, $cv) = ($c[$u], $c[$v]);\n ($cu, $cv) = ($cv, $cu) if $cu > $cv;\n\n next if exists $edges{\"$cu $cv\"};\n next if $c[$u] == $c[$v];\n\n ++$count{$cu};\n ++$count{$cv};\n $edges{\"$cu $cv\"} = 1;\n}\n\nmy ($res, $cnt) = ($c[0], -1);\nwhile (my ($key, $val) = each %count) {\n if ($val > $cnt || ($val == $cnt && $key < $res)) {\n $cnt = $val;\n $res = $key;\n }\n}\nprint $res, \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp($_ = <>);\nmy ($n, $m) = split;\nchomp($_ = <>);\nmy @c = split;\nmy %edges = ();\nmy %count = ();\n\nfor (1 .. $m) {\n chomp($_ = <>);\n my ($u, $v) = split;\n ($u, $v) = ($v, $u) if $u > $v;\n ($u, $v) = ($u - 1, $v - 1);\n next if exists $edges{\"$u $v\"};\n next if $c[$u] == $c[$v];\n ++$count{$c[$u]};\n ++$count{$c[$v]};\n $edges{\"$u $v\"} = 1;\n}\n\nmy ($res, $cnt) = ($c[0], -1);\nwhile (my ($key, $val) = each %count) {\n if ($cnt == -1 || $val > $cnt || ($val == $cnt && $key < $res)) {\n $cnt = $val;\n $res = $key;\n }\n}\nprint $res, \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp($_ = <>);\nmy ($n, $m) = split;\nchomp($_ = <>);\nmy @c = split;\nmy %edges = ();\nmy %count = ();\n\nfor (1 .. $m) {\n chomp($_ = <>);\n\n my ($u, $v) = split;\n ($u, $v) = ($c[$u - 1], $c[$v - 1]);\n ($u, $v) = ($v, $u) if $u > $v;\n\n next if exists $edges{\"$u $v\"};\n next if $u == $v;\n\n ++$count{$u};\n ++$count{$v};\n $edges{\"$u $v\"} = 1;\n}\n\nmy ($res, $cnt) = ($c[0], -1);\nwhile (my ($key, $val) = each %count) {\n if ($val > $cnt || ($val == $cnt && $key < $res)) {\n ($res, $cnt) = ($key, $val);\n }\n}\nprint $res, \"\\n\";\n"}], "src_uid": "89c27a5c76f4ddbd14af2d30ac8b6330"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 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@_=<>;\nchomp(@_);\n$t=pop @_;\n$s='<3'.(join'<3',@_).'<3';\n@s=split'',$s;\n@t=split'',$t;\nfor(@s) {\n @t or $F++ or last;\n while(@t){\n $_ eq shift @t and last;\n }\n}\nprint $F?'no':'yes';\n"}, {"source_code": "<>;@_=<>;chomp(@_);\n@t=split'',pop @_;\nfor(split'','<3'.(join'<3',@_).'<3'){\n @t or $F++ or last;\n while(@t){\n $_ eq shift @t and last;\n }\n}\nprint $F?'no':'yes';\n"}, {"source_code": "$n=<>;\nwhile($n--){\n $_=<>;\n chomp;\n $s.='<3'.$_;\n}\n$s.='<3';\n$t=<>;\n#print \"s->$s<\\n\";\n@s=split '',$s;\n@t=split '',$t;\nfor($i=0,$j=0;$i<@s;++$i) {\n for(;$j<@t;++$j) {\n if($s[$i]eq$t[$j]) {\n ++$j;\n ++$cnt;\n last;\n }\n }\n}\n#print \"lens=\",scalar @s,\"\\n\";\nprint $cnt==@s?'yes':'no';\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$n=<>;\n@_=<>;\n$_=pop @_;\nchomp;\nchomp(@_);\n$s=join\"<3\",@_;\n$s=\"<3\".$s.\"<3\";\n@_=split//,$s;\n@a=split//,$_;\nfor $i(@_){\n\t@a or $F++;\n\twhile (@a){\n\t\t$p=shift @a;\n\t\t$i eq $p and last;\n\t\t}\n\t}\n\nprint $F?\"no\":\"yes\";"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$n=<>;\n@_=<>;\n$_=pop @_;\nchomp;\nchomp(@_);\n$s=join\"<3\",@_;\n$s=\"<3\".$s.\"<3\";\n@_=split//,$s;\n@a=split//,$_;\nfor $i(@_){\n\t@a or $F++ or last;\n\t\twhile (@a){\n\t\t$p=shift @a;\n\t\t$i eq $p and last;\n\t\t}\n\t}\n\nprint $F?\"no\":\"yes\";"}], "negative_code": [{"source_code": "$n=<>;\nwhile($n--){\n $_=<>;\n chomp;\n push @a,$_;\n}\n$s='<3'.(join '<3',@a).'<3';\n$t=<>;\n#print \"$s\\n\";\n@s=split '',$s;\n@t=split '',$t;\nfor($i=0,$j=0;$i<@s;++$i) {\n for(;$j<@t;++$j) {\n if($s[$i]eq$t[$j]) {\n ++$cnt;\n last;\n }\n }\n}\nprint $cnt==@s?'yes':'no';\n"}], "src_uid": "36fb3a01860ef0cc2a1065d78e4efbd5"} {"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.\u2009e. 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "use List::Util qw (max);\r\nfor (1..<>) {\r\n chomp (my $in = <>);\r\n my ($p, $q, $c, $d) = split / /, $in;\r\n my @z = split / /, $in;\r\n @z = sort {$a <=> $b} @z;\r\n\r\n $x = max($p, $q);\r\n $y = max($c, $d);\r\n if (($x == @z[2] && $y == @z[3]) || ($x == @z[3] && $y == @z[2])) {\r\n print \"YES\\n\";\r\n } else {\r\n print \"NO\\n\";\r\n }\r\n}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my @s = map { $_ - 0 } split(/\\s+/,);\r\n my @x = ();\r\n for(my $i=0;$i<4;$i++){\r\n push(@x,+[$s[$i],$i]);\r\n }\r\n my @sa = sort { $a->[0] <=> $b->[0] } @x;\r\n if( $sa[0]->[1] / 2 == $sa[1]->[1] / 2 ){\r\n print \"NO\\n\";\r\n } else {\r\n print \"YES\\n\";\r\n }\r\n \r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "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$$$) \u2014 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 \u2014 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 \u2014 pages $$$4$$$-$$$8$$$. During the fourth (and the last) day Ivan will read remaining page number $$$9$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n#\tprint \"@_\";\n\t\n\tmy $i = 0;\n\tmy $max = 0;\n\tmy $days = 0;\n\t\n\twhile( @_ ){\n\t\t$i ++;\n\t\tmy $A = shift @_;\n\t\t$max < $A and $max = $A;\n\t\tif( $max > $i ){\n\t\t\tnext;\n\t\t\t}\n\t\t$days ++;\n\t\t}\n\t\n\tprint $days;\n\t}"}], "negative_code": [], "src_uid": "291601d6cdafa4113c1154f0dda3470d"} {"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$$$) \u2014 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": "$/ = \"\"; $_ = <>; ($x, $k, $_) = split;\n$x = substr($_, ++$i) until /^$x/;\nprint $x, $' x $k;"}, {"source_code": "#!usr/bin/perl -w\nuse strict;\n\nmy ($n,$k)=split/ /,;chomp($k);\nmy $t=;chomp($t);\nmy @f;my $j=0;\nmy @g=split//,$t;\n$f[0]=0;\nmy $i=1;\nwhile($i<$n){\n while($i<$n&&$g[$i] eq $g[$j]){\n\tpush @f,++$j;\n\t$i++;\n }\n if($j==0){\n\tpush @f,0;\n\t$i++;\n }\n $j=$f[$j-1] if($j!=0);\n}\nmy $out=$t;\n$out.=substr($t,$f[-1],$n)x($k-1);\nprint \"$out\\n\";"}], "negative_code": [{"source_code": "$/ = \"\"; $_ = <>; ($n, $k, $t) = split;\n$f = \"\\xff\";\n$s = $f x $n;\n$i = $t;\nwhile ($k) {\n\tif (($s & $i) =~ /$t$/) {\n\t\t$s &= $i;\n\t\t$k--;\n\t}\n\t$s = $s . $f;\n\t$i = $f . $i;\n}\t\n$s =~ s/$f//g;\nprint $s;\n"}, {"source_code": "($x, $k) = split \" \", <>; $_ = <>;\n$x = substr($_, ++$i) until /^$x/;\nprint $x, $' x $k;\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$$$)\u00a0\u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tprint +( () = /N/g ) - 1 ? \"YES\" : \"NO\";\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tprint do {\n\t\tif( m/^E+N$/ || m/^NE+$/ || m/^E+NE+$/ ){\n\t\t\t\"NO\";\n\t\t\t}\n\t\telse{\n\t\t\t\"YES\";\n\t\t\t}\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "e744184150bf55a30568060cca69de04"} {"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\u2009\u2264\u2009i\u2009\u2264\u2009n) has a best friend p[i] (1\u2009\u2264\u2009p[i]\u2009\u2264\u2009n). 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\u2009=\u2009p[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\u2009\u2264\u2009i\u2009\u2264\u2009n) 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,\u2009a2,\u2009...,\u2009an, where ai means the student who gets the i-th student's notebook on the third day of revising; b1,\u2009b2,\u2009...,\u2009bn, 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of students in the group. The second line contains sequence of different integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009n). The third line contains the sequence of different integers b1,\u2009b2,\u2009...,\u2009bn (1\u2009\u2264\u2009bi\u2009\u2264\u2009n).", "output_spec": "Print sequence n of different integers p[1],\u2009p[2],\u2009...,\u2009p[n] (1\u2009\u2264\u2009p[i]\u2009\u2264\u2009n). 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": "#!perl\n$num = ;\n$skeys = ;\n@keys = split(/\\D/,$skeys);\n$svalues = ;\n@values = split(/\\D/,$svalues);\n@friends{@keys} = @values;\nfor $i (1..$num) {\n\tprint $friends{$i}, \" \";\n}\n"}], "negative_code": [], "src_uid": "a133b2c63e1025bdf13d912497f9b6a4"} {"nl": {"description": "You are given a sequence of $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$. You are also given $$$x$$$ integers $$$1, 2, \\dots, x$$$.You are asked to insert each of the extra integers into the sequence $$$a$$$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence.The score of the resulting sequence $$$a'$$$ is the sum of absolute differences of adjacent elements in it $$$\\left(\\sum \\limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\\right)$$$.What is the smallest possible score of the resulting sequence $$$a'$$$?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of testcases. The first line of each testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\le n, x \\le 2 \\cdot 10^5$$$)\u00a0\u2014 the length of the sequence and the number of extra integers. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 2 \\cdot 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each testcase, print a single integer\u00a0\u2014 the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it.", "sample_inputs": ["4\n\n1 5\n\n10\n\n3 8\n\n7 2 10\n\n10 2\n\n6 1 5 7 3 3 9 10 10 1\n\n4 10\n\n1 3 1 2"], "sample_outputs": ["9\n15\n31\n13"], "notes": "NoteHere are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $$$\\underline{1}, \\underline{2}, \\underline{3}, \\underline{4}, \\underline{5}, 10$$$ $$$\\underline{7}, 7, \\underline{6}, \\underline{4}, 2, \\underline{2}, \\underline{1}, \\underline{3}, \\underline{5}, \\underline{8}, 10$$$ $$$6, \\underline{1}, 1, \\underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$$$ $$$1, 3, \\underline{1}, 1, 2, \\underline{2}, \\underline{3}, \\underline{4}, \\underline{5}, \\underline{6}, \\underline{7}, \\underline{8}, \\underline{9}, \\underline{10}$$$ "}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n \r\n my ($n,$x) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n \r\n if( $n == 1 ){\r\n my $v = &max($x,$A[0]) - &min(1,$A[0]);\r\n print \"$v\\n\"; next;\r\n }\r\n \r\n my $sc0 = 0;\r\n my $mx = -1;\r\n my $mn = 201000;\r\n for(my $i=0;$i<$n;$i++){\r\n $mx = &max($mx,$A[$i]);\r\n $mn = &min($mn,$A[$i]);\r\n if( $i>0 ){ $sc0 += abs($A[$i-1]-$A[$i]); }\r\n }\r\n my $p_mn = &min($A[0],$A[$n-1]);\r\n my $p_mx = &max($A[0],$A[$n-1]);\r\n \r\n if( $x > $mx ){\r\n my $v1 = &min( 2*($x - $mx) , $x - $p_mx );\r\n $sc0 += $v1;\r\n }\r\n if( 1 < $mn ){\r\n my $v1 = &min( 2*($mn - 1) , $p_mn - 1 );\r\n $sc0 += $v1;\r\n }\r\n print \"$sc0\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "negative_code": [], "src_uid": "0efa3ce46108ca604d95e15d02757b44"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 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": "for(1..<>){<>;print\" \",split/\\s.*/,<>}"}, {"source_code": "for(1..<>){<>;$s=<>;$s=~s/\\s.*//;print$s}"}, {"source_code": "for(1..<>){<>;@a=split/\\s/,<>;print@a[0],\" \"}"}], "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\u2009\u00d7\u2009n 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\u2009\u2264\u2009n\u2009\u2264\u20091000, 1\u2009\u2264\u2009m\u2009\u2264\u2009min(n\u00b7n,\u2009105)) \u2014 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\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009n) \u2014 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 \u2014 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": "#!/bin/perl -w\nuse strict;\nuse warnings;\nuse List::Util qw[min max];\n\nmy ($n, $m) = map int, split /\\D/, <>;\n\nmy @square = ('W' x $n) x $n;\n\nfor my $move (1..$m) {\n my ($i, $j) = map int, split /\\D/, <>;\n map { $_-- } ($i, $j);\n substr($square[$i], $j, 1) = 'B';\n for (0..2) {\n\tmy $indx = index $square[$i], 'BBB', max($j-$_, 0);\n\tif ($indx > -1) {\n\t my $black_rows = '';\n\t for (max($i-2, 0) .. min($i+2, $n-1)) {\n\t\tif (substr($square[$_], $indx, 3) eq 'BBB') {\n\t\t $black_rows .= 1;\n\t\t} else {\n\t\t $black_rows .= 0;\n\t\t}\n\t }\n\t if ($black_rows =~ /111/) {\n\t\tprint \"$move\\n\";\n\t\texit;\n\t }\n\t}\n }\n}\n\nprint \"-1\\n\";\n"}], "negative_code": [{"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\nuse List::Util qw[min max];\n\nmy ($n, $m) = map int, split /\\D/, <>;\n\nmy @square = ('W' x $n) x $n;\n\nfor my $move (1..$m) {\n my ($i, $j) = map int, split /\\D/, <>;\n map { $_-- } ($i, $j);\n substr($square[$i], $j, 1) = 'B';\n for (0..2) {\n\tmy $indx = index $square[$i], 'BBB', min($j-$_, 0);\n\tif ($indx > -1) {\n\t my $black_rows = '';\n\t for (max($i-2, 0) .. min($i+2, $n-1)) {\n\t\tif (substr($square[$_], $indx, 3) eq 'BBB') {\n\t\t $black_rows .= 1;\n\t\t} else {\n\t\t $black_rows .= 0;\n\t\t}\n\t }\n\t if ($black_rows =~ /111/) {\n\t\tprint \"$move\\n\";\n\t\texit;\n\t }\n\t}\n }\n}\n\nprint \"-1\\n\";\n"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\nuse List::Util qw[min max];\n\nmy ($n, $m) = map int, split /\\D/, <>;\n\nmy @square = ('W' x $n) x $n;\n\nfor my $move (1..$m) {\n my ($i, $j) = map int, split /\\D/, <>;\n map { $_-- } ($i, $j);\n substr($square[$i], $j, 1) = 'B';\n my $indx = index $square[$i], 'BBB', min($j-2, 0);\n if ($indx > -1) {\n\tmy $black_rows = '';\n\tfor (max($i-2, 0) .. min($i+2, $n-1)) {\n\t if (substr($square[$_], $indx, 3) eq 'BBB') {\n\t\t$black_rows .= 1;\n\t } else {\n\t\t$black_rows .= 0;\n\t }\n\t}\n\tif ($black_rows =~ /111/) {\n\t print \"$move\\n\";\n\t exit;\n\t}\n }\n}\n\nprint \"-1\\n\";\n"}], "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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of values of the banknotes that used in Geraldion. The second line contains n distinct space-separated numbers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009106) \u2014 the values of the banknotes.", "output_spec": "Print a single line \u2014 the minimum unfortunate sum. If there are no unfortunate sums, print \u2009-\u20091.", "sample_inputs": ["5\n1 2 3 4 5"], "sample_outputs": ["-1"], "notes": null}, "positive_code": [{"source_code": "<>;\nprint ((<> =~ /\\b1\\b/) ? \"-1\\n\" : \"1\\n\");"}, {"source_code": "<>;\n$min = 10000000;\nfor (split / /, <>)\n{\n $min = $_ if ($_ < $min)\n}\nprint (qw/1 -1/[$min == 1]);"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n$_==1 and say -1 and exit foreach (split / /, <>);\nsay 1;"}, {"source_code": "#!perl\n\n$n = <>;\n\n@a = split / /, <>;\n\nif (grep {$_ == 1} @a) \n{\n print -1;\n}\nelse\n{\n print 1;\n}"}, {"source_code": "<>;\n@a = sort (split(' ', <>));\nif($a[0] == 1){\n\tprint -1;}\nelse{\nprint 1;}"}, {"source_code": "<>;\n@a = split(/ /g, <>);\n@a = sort { $a <=> $b } @a;\nprint $a[0] == 1 ? -1 : 1;"}, {"source_code": "print .5 <=> (a.<>.<>) =~ /\\b1\\b/"}], "negative_code": [{"source_code": "<>;\nprint ((<> =~ /\\b1\\b/) ? \"1\\n\" : \"-1\\n\");"}, {"source_code": "$min = 10000000;\nfor (split / /, <>)\n{\n $min = $_ if ($_ < $min)\n}\nprint (qw/1 -1/[$min == 1]);"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n$==1 and say -1 and exit foreach (split / /, <>);\nsay 1;"}, {"source_code": "<>;\n@a = sort (split(' ', <>));\n$s = 0;\nfor $i(@a){\n\tif($i > $s + 1){\n\t\tprint $s + 1;\n\t\texit;}\n\t$s += $i}\nprint -1;"}, {"source_code": "<>;\n@a = split(/ /g, <>);\nsort { $a <=> $b } @a;\nprint $a[0] == 1 ? -1 : 1;"}], "src_uid": "df94080c5b0b046af2397cafc02b5bdc"} {"nl": {"description": "DZY has a sequence a, consisting of n integers.We'll call a sequence ai,\u2009ai\u2009+\u20091,\u2009...,\u2009aj (1\u2009\u2264\u2009i\u2009\u2264\u2009j\u2009\u2264\u2009n) a subsegment of the sequence a. The value (j\u2009-\u2009i\u2009+\u20091) 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\u00a0(1\u2009\u2264\u2009n\u2009\u2264\u2009105). The next line contains n integers a1,\u2009a2,\u2009...,\u2009an\u00a0(1\u2009\u2264\u2009ai\u2009\u2264\u2009109).", "output_spec": "In a single line print the answer to the problem \u2014 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,\u2009a3,\u2009a4,\u2009a5,\u2009a6 and change its 3rd element (that is a4) to 4."}, "positive_code": [{"source_code": "# http://codeforces.com/problemset/problem/446/A\n# http://codeforces.com/blog/entry/13036\n\n\nmy $n = ;\nmy $f = ;\nchomp ( $n, $f );\nmy @seq = split m/ /, $f;\n\n\n\nmy @left, @right;\nmy $cnt = 1;\n$left[0] = 1;\n\nfor ( my $i = 1; $i < @seq; $i += 1 ) {\n if ( $seq[ $i-1 ] < $seq[ $i ] ) {\n\t$cnt += 1;\n\t$left[ $i ] = $cnt; }\n else { \n\t$cnt = 1;\n\t$left[ $i ] = $cnt; }}\n\n$right[ @seq-1 ] = 1;\n$cnt = 1;\nfor ( my $i = @seq-2; $i >= 0; $i -= 1 ) {\n if ( $seq[ $i ] < $seq[ $i+1 ] ) {\n\t$cnt += 1;\n\t$right[ $i ] = $cnt; }\n else { \n\t$cnt = 1;\n\t$right[ $i ] = $cnt; }}\n\nmy $mx = 0;\nfor ( my $i = 0; $i < @seq-2; $i += 1 ) {\n if ( $seq[ $i ] + 1 < $seq[ $i+2 ] ) {\n\tif ( $left[ $i ] + 1 + $right[ $i+2 ] > $mx ) {\n\t $mx = $left[ $i ] + 1 + $right[ $i+2 ]; }}}\n\n\n\nmy $ww = 0;\nfor (@left) {\n if ( $_ > $ww ) {\n\t$ww = $_; }}\nif ( $ww < @left ) {\n $ww += 1; }\nif ( $ww > $mx ) { $mx = $ww; }\n\n\n\nif ( $n < 3 ) { print \"$n\\n\"; }\nelse {\nprint $mx;\nprint \"\\n\"; }\n\n\n\n"}, {"source_code": "chomp($n=<>);\nchomp($line=<>);\n@nums = split(\" \",$line);\n@F = (0) x $n;\n@L = (0) x $n;\n@R = (0) x $n;\n$p = $q = $c = $ans = 0;\nfor $i(0..$#nums){\n $F[$i] = 1 if !$i or $nums[$i] > $nums[$i-1];\n $L[$i] += $p;\n $p = $F[$i] ? $p + 1 : 1;\n}\nfor $i(reverse(0..$#nums)){\n $R[$i] += $q;\n $q = $i + 1 < $n && $nums[$i] < $nums[$i + 1] ? $q + 1 : 1;\n}\nfor $i(0..$#nums)\n{\n if(!$i or $i + 1 == $n or $nums[$i + 1] - $nums[$i - 1] >= 2){\n $c = $L[$i] + $R[$i] + 1;\n }else {\n $c = $R[$i] > $L[$i] ? $R[$i] + 1 : $L[$i] + 1;\n }\n $ans = $c if $c > $ans;\n}\nprint \"$ans\\n\";\n\n"}, {"source_code": "chomp($n=<>);\nchomp($line=<>);\n@nums = split(\" \",$line);\n@F = (0) x $n;\n@L = (0) x $n;\n@R = (0) x $n;\n$p = $q = $c = $ans = 0;\nfor $i(0..$#nums){\n $F[$i] = 1 if !$i or $nums[$i] > $nums[$i-1];\n $L[$i] += $p;\n $p = $F[$i] ? $p + 1 : 1;\n}\nfor $i(reverse(0..$#nums)){\n $R[$i] += $q;\n #$q = $F[$i] ? $q + 1 : 1;\n $q = $i + 1 < $n && $nums[$i] < $nums[$i + 1] ? $q + 1 : 1;\n}\nfor $i(0..$#nums)\n{\n if(!$i or $i + 1 == $n or $nums[$i + 1] - $nums[$i - 1] >= 2){\n $c = $L[$i] + $R[$i] + 1;\n }else {\n $c = $R[$i] > $L[$i] ? $R[$i] + 1 : $L[$i] + 1;\n }\n $ans = $c if $c > $ans;\n}\nprint \"$ans\\n\";\n\n"}, {"source_code": "chomp($n=<>);\nchomp($line=<>);\n@nums = split(\" \",$line);\n@F = (0) x $n;\n@L = (0) x $n;\n@R = (0) x $n;\n$p = $q = $c = $ans = 0;\nfor $i(0..$#nums){\n $F[$i] = 1 if !$i or $nums[$i] > $nums[$i-1];\n $L[$i] += $p;\n $p = $F[$i] ? $p + 1 : 1;\n}\nfor $i(reverse(0..$#nums)){\n $R[$i] += $q;\n #$q = $F[$i] ? $q + 1 : 1;\n $q = $i + 1 < $n && $nums[$i] < $nums[$i + 1] ? $q + 1 : 1;\n}\n#print \"@F\\n@L\\n@R\\n\";\n\nfor $i(0..$#nums)\n{\n if(!$i or $i + 1 == $n or $nums[$i + 1] - $nums[$i - 1] >= 2){\n $c = $L[$i] + $R[$i] + 1;\n }else {\n $c = $R[$i] > $L[$i] ? $R[$i] + 1 : $L[$i] + 1;\n }\n $ans = $c if $c > $ans;\n #print \"cur ans = $ans\\n\";\n}\nprint \"$ans\\n\";\n\n"}], "negative_code": [{"source_code": "# http://codeforces.com/problemset/problem/446/A\n# http://codeforces.com/blog/entry/13036\n\n\nmy $n = ;\nmy $f = ;\nchomp ( $n, $f );\nmy @seq = split m/ /, $f;\nmy @left, @right;\nmy $cnt = 1;\n$left[0] = 1;\n\nfor ( my $i = 1; $i < @seq; $i += 1 ) {\n if ( $seq[ $i-1 ] < $seq[ $i ] ) {\n\t$cnt += 1;\n\t$left[ $i ] = $cnt; }\n else { \n\t$cnt = 1;\n\t$left[ $i ] = $cnt; }}\n\n$right[ @seq-1 ] = 1;\n$cnt = 1;\nfor ( my $i = @seq-2; $i >= 0; $i -= 1 ) {\n if ( $seq[ $i ] < $seq[ $i+1 ] ) {\n\t$cnt += 1;\n\t$right[ $i ] = $cnt; }\n else { \n\t$cnt = 1;\n\t$right[ $i ] = $cnt; }}\n\nmy $mx = 0;\nfor ( my $i = 0; $i < @seq-2; $i += 1 ) {\n if ( $seq[ $i ] + 1 < $seq[ $i+2 ] ) {\n\tif ( $left[ $i ] + 1 + $right[ $i ] > $mx ) {\n\t $mx = $left[ $i ] + 1 + $right[ $i ]; }}}\n\nif ( $n < 3 ) { print \"$n\\n\"; }\nelse {\nprint $mx;\nprint \"\\n\"; }\n\n"}, {"source_code": "chomp($n=<>);\nchomp($line=<>);\n@nums = split(\" \",$line);\n@F = (0) x $n;\n@L = (0) x $n;\n@R = (0) x $n;\n$p = $q = $c = $ans = 0;\nfor $i(0..$#nums){\n $F[$i] = 1 if !$i or $nums[$i] > $nums[$i-1];\n $L[$i] += $p;\n $p = $F[$i] ? $p + 1 : 1;\n}\nfor $i(reverse(0..$#nums)){\n $R[$i] += $q;\n $q = $F[$i] ? $q + 1 : 1;\n}\n#print \"F = @F L = @L \\n R = @R\\n\";\nfor $i(0..$#nums)\n{\n $c = $L[$i] + $R[$i] + 1 if $F[$i] or !$i or $i + 1 == $n or $nums[$i + 1] - $nums[$i - 1] >= 2;\n $ans = $c if $c > $ans;\n #print \"cur ans = $ans\\n\";\n}\nprint \"$ans\\n\";\n\n"}, {"source_code": "chomp($n=<>);\nchomp($line=<>);\n@nums = split(\" \",$line);\n@F = (0) x $n;\n@L = (0) x $n;\n@R = (0) x $n;\nfor $i(0..$#nums){\n $F[$i] = 1 if !$i or $nums[$i] > $nums[$i-1];\n $L[$i] += $p;\n $p = $F[$i] ? $p + 1 : 1;\n}\nfor $i(reverse(0..$#nums)){\n $R[$i] += $q;\n $q = $F[$i] ? $q + 1 : 1;\n}\n#print \"F = @F L = @L \\n R = @R\\n\";\nfor $i(0..$#nums)\n{\n $c = $L[$i] + $R[$i] + 1 if $F[$i] or !$i or $i + 1 == $n or $nums[$i + 1] - $nums[$i - 1] >= 2;\n $ans = $c if $c > $ans;\n #print \"cur ans = $ans\\n\";\n}\nprint \"$ans\\n\";\n\n"}, {"source_code": "chomp($n=<>);\nchomp($line=<>);\n@nums = split(\" \",$line);\n@F = (0) x $n;\n@L = (0) x $n;\n@R = (0) x $n;\n$p = $q = $c = $ans = 0;\nfor $i(0..$#nums){\n $F[$i] = 1 if !$i or $nums[$i] > $nums[$i-1];\n $L[$i] += $p;\n $p = $F[$i] ? $p + 1 : 1;\n}\nfor $i(reverse(0..$#nums)){\n $R[$i] += $q;\n #$q = $F[$i] ? $q + 1 : 1;\n $q = $i + 1 < $n && $nums[$i] < $nums[$i + 1] ? $q + 1 : 1;\n}\nprint \"@F\\n@L\\n@R\\n\";\n\nfor $i(0..$#nums)\n{\n if(!$i or $i + 1 == $n or $nums[$i + 1] - $nums[$i - 1] >= 2){\n $c = $L[$i] + $R[$i] + 1;\n }else {\n $c = $R[$i] > $L[$i] ? $R[$i] + 1 : $L[$i] + 1;\n }\n $ans = $c if $c > $ans;\n #print \"cur ans = $ans\\n\";\n}\nprint \"$ans\\n\";\n\n"}], "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\u00a0\u2014 from some cell $$$(x, y)$$$ to $$$(x, y + 1)$$$; Move down\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($m) = map { $_ - 0 } split(/\\s+/,);\r\n my @a1 = map { $_ - 0 } split(/\\s+/,);\r\n my @a2 = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my @r1 = (0); $#r1 = $m;\r\n my @r2 = (0); $#r2 = $m;\r\n foreach my $i (0..($m-1)){\r\n $r1[$i+1] = $r1[$i] + $a1[$i];\r\n $r2[$i+1] = $r2[$i] + $a2[$i];\r\n }\r\n my $r = $mod * 2;\r\n foreach my $i (0..($m-1)){\r\n my $s = &max( ($r1[$m] - $r1[$i+1]) , $r2[$i] );\r\n $r = $s if $s < $r;\r\n }\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){\r\n $r = $e if !defined($r) or $e > $r;\r\n }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){\r\n $r = $e if !defined($r) or $e < $r;\r\n }\r\n return $r;\r\n}\r\n\r\n"}], "negative_code": [], "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)$$$ \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy @X;\n\tmy @Y;\n\t\n\tmap { \n\t\tmy( $X, $Y ) = split ' ', <>;\n\t\tpush @X, $X;\n\t\tpush @Y, $Y;\n\t\t} 1 .. $_;\n\t\n\t@X = sort { $a <=> $b } @X;\n\t@Y = sort { $a <=> $b } @Y;\n\t\n\tmy $X = 1;\n\tmy $Y = 1;\n\t\n\tif( @X % 2 == 0 ){\n\t\t$X = $X[ @X / 2 - 0 ] - $X[ @X / 2 - 1 ] + 1;\n\t\t}\n\t\n\tif( @Y % 2 == 0 ){\n\t\t$Y = $Y[ @Y / 2 - 0 ] - $Y[ @Y / 2 - 1 ] + 1;\n\t\t}\n\t\n\tprint $X * $Y;\n\t}"}], "negative_code": [], "src_uid": "e0a1dc397838852957d0e15ec98d5efe"} {"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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\tchomp;\n\t\n\tmy $n = $_;\n\t\n\tmy $length = 2;\n\t\n\tmy @re = split ' ', join '', map ~~<>, 1 .. $length;\n\t\n\t$debug and print \"\\$n: $n, \\@re: @re\";\n\t\n\tmy $letters = join '', 'a' .. 'c';\n\t\n\tmy $prependent = ',';\n\t\n\tmy $str = join '', map \"${prependent}$_\", ( $letters ) x $length;\n\t\n\t$debug and print $str;\n\t\n\tmy $match_one_letter = \"${prependent}.*([^$prependent]).*\";\n\t\n\tmy $match_letters = $match_one_letter x $length;\n\t\n\t$debug and print \"$match_letters\";\n\t\n\tmy %strings;\n\t\n\t$str =~ /\n\t\t$match_letters\n\t\t(?{ $strings{ $1 . $2 } ++ })\n\t\t(*F)\n\t\t/x;\n\t\n\t/(.).*\\1/ and delete $strings{ $_ } for sort keys %strings;\n\t\n\t$debug and print join ' ', sort keys %strings;\n\t\n\tmy %variants;\n\t\n\tfor my $string ( sort keys %strings ){\n\t\tmy( $third_letter ) = $letters =~ s/[$string]//gr;\n\t\t$variants{ join '', map { $_ x $n } $string, $third_letter } ++;\n\t\t$variants{ join '', map { $_ x $n } $third_letter, $string } ++;\n\t\t$variants{ scalar( $string . $third_letter ) x $n } ++;\n\t\t}\n\t\n\t$debug and print for sort keys %variants;\n\t\n\tmy $find;\n\t\n\tfor my $variant ( sort keys %variants ){\n\t\tmy $ok = 1;\n\t\tfor my $re ( @re ){\n\t\t\t$variant =~ /$re/ and $ok = 0;\n\t\t\t}\n\t\t\n\t\tlast if $ok and $find = $variant;\n\t\t}\n\t\n\tprint defined $find ? \"YES\\n$find\" : \"NO\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile( my $n = <>){\n\t\t\n\tmy $L = 2;\n\t\n\tmy @re = split ' ', join '', map ~~<>, 1 .. 2;\n\t\t\n\tmy $l = join '', 'a' .. 'c';\n\t\n\tmy $p = ',';\n\t\n\t$_ = join '', map $p . $_, ( $l ) x $L;\n\t\n\tmy $m = ( $p . join \"([^$p])\", ( \".*\" ) x 2 ) x $L;\n\t\n\tmy %s;\n\t\n\t/\n\t\t$m\n\t\t(?{ $s{ join '', map { eval \"\\$$_\" } 1 .. $L } ++ })\n\t\t(*F)\n\t\t/x;\n\t\n\tmy %v;\n\t\n\tfor my $s ( grep !/(.).*\\1/, keys %s ){\n\t\tmy( $l3 ) = $l =~ s/[$s]//gr;\n\t\t$v{ join '', map { $_ x $n } $s, $l3 } ++;\n\t\t$v{ join '', map { $_ x $n } $l3, $s } ++;\n\t\t$v{ ( $s . $l3 ) x $n } ++;\n\t\t}\n\t\n\tmy $x;\n\t\n\tfor my $v ( keys %v ){\n\t\tmy $ok = 1;\n\t\tfor my $re ( @re ){\n\t\t\t$v =~ /$re/ and $ok = 0;\n\t\t\t}\n\t\t\n\t\tlast if $ok and $x = $v;\n\t\t}\n\t\n\tprint \"YES\\n$x\";\n\t}"}], "negative_code": [], "src_uid": "7f45fba2c06fe176a2b634e8988076c2"} {"nl": {"description": "On an $$$8 \\times 8$$$ grid, some horizontal rows have been painted red, and some vertical columns have been painted blue, in some order. The stripes are drawn sequentially, one after the other. When the stripe is drawn, it repaints all the cells through which it passes.Determine which color was used last. The red stripe was painted after the blue one, so the answer is R. ", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 4000$$$)\u00a0\u2014 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 'R', 'B', or '.', denoting a red square, a blue square, and an unpainted square, respectively. It is guaranteed that the given field is obtained from a colorless one by drawing horizontal red rows and vertical blue columns. At least one stripe is painted.", "output_spec": "For each test case, output 'R' if a red stripe was painted last, and 'B' if a blue stripe was painted last (without quotes).", "sample_inputs": ["4\n\n\n\n\n....B...\n\n....B...\n\n....B...\n\nRRRRRRRR\n\n....B...\n\n....B...\n\n....B...\n\n....B...\n\n\n\n\nRRRRRRRB\n\nB......B\n\nB......B\n\nB......B\n\nB......B\n\nB......B\n\nB......B\n\nRRRRRRRB\n\n\n\n\nRRRRRRBB\n\n.B.B..BB\n\nRRRRRRBB\n\n.B.B..BB\n\n.B.B..BB\n\nRRRRRRBB\n\n.B.B..BB\n\n.B.B..BB\n\n\n\n\n........\n\n........\n\n........\n\nRRRRRRRR\n\n........\n\n........\n\n........\n\n........"], "sample_outputs": ["R\nB\nB\nR"], "notes": "NoteThe first test case is pictured in the statement.In the second test case, the first blue column is painted first, then the first and last red rows, and finally the last blue column. Since a blue stripe is painted last, the answer is B."}, "positive_code": [{"source_code": "$\\ = $/;\r\n$/ x= 2;\r\n<>;\r\n\r\nprint /R{8}/ ? R:B for <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. 8;\n\t\n\tif( grep m/^R+$/, @_ ){\n\t\tprint 'R';\n\t\t}\n\telse{\n\t\tprint 'B';\n\t\t}\n\t}"}, {"source_code": "$\\ = $/;\r\n$/ x= 2;\r\n<>;\r\n\r\nprint /R{8}/ ? R:B for <>"}, {"source_code": "$\\ = $/;\r\n$/ x= 2;\r\n<>;\r\n \r\nprint /R{8}/ ? R:B for <>"}], "negative_code": [], "src_uid": "2c7add49d423de44a2bc09de56ffacf1"} {"nl": {"description": "You're given an array $$$a$$$ of length $$$n$$$. You can perform the following operations on it: choose an index $$$i$$$ $$$(1 \\le i \\le n)$$$, an integer $$$x$$$ $$$(0 \\le x \\le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j+x$$$ for all $$$(1 \\le j \\le i)$$$, which means add $$$x$$$ to all the elements in the prefix ending at $$$i$$$. choose an index $$$i$$$ $$$(1 \\le i \\le n)$$$, an integer $$$x$$$ $$$(1 \\le x \\le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j \\% x$$$ for all $$$(1 \\le j \\le i)$$$, which means replace every element in the prefix ending at $$$i$$$ with the remainder after dividing it by $$$x$$$. Can you make the array strictly increasing in no more than $$$n+1$$$ operations?", "input_spec": "The first line contains an integer $$$n$$$ $$$(1 \\le n \\le 2000)$$$, the number of elements in the array $$$a$$$. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$\\dots$$$, $$$a_n$$$ $$$(0 \\le a_i \\le 10^5)$$$, the elements of the array $$$a$$$.", "output_spec": "On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format \"$$$1$$$ $$$i$$$ $$$x$$$\"; to print a modding operation, use the format \"$$$2$$$ $$$i$$$ $$$x$$$\". If $$$i$$$ or $$$x$$$ don't satisfy the limitations above, or you use more than $$$n+1$$$ operations, you'll get wrong answer verdict.", "sample_inputs": ["3\n1 2 3", "3\n7 6 3"], "sample_outputs": ["0", "2\n1 1 1\n2 2 4"], "notes": "NoteIn the first sample, the array is already increasing so we don't need any operations.In the second sample:In the first step: the array becomes $$$[8,6,3]$$$.In the second step: the array becomes $$$[0,2,3]$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $step = 1e5 + 0;\nmy $mod = 1e5 + 1;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @ans;\n\t\n\tmy $need = 1e5 + 0;\n\tmy $i = @_ + 1;\n\tmy $sumdiff = 0;\n\t\n\tfor( reverse @_ ){\n\t\t$i --;\n\t\tmy $diff = $need - $_ - $sumdiff;\n\t\tpush @ans, join ' ', 1, $i, $diff;\n\t\t$sumdiff += $diff;\n\t\t$need += $step;\n\t\t}\n\t\n\tpush @ans, join ' ', 2, ~~ @_, $mod;\n\t\n\tprint for ~~ @ans, @ans;\n\t}"}], "negative_code": [], "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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200950), 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\u2009\u2264\u2009|s|\u2009\u2264\u2009100)\u00a0\u2014 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": "( $n, $m ) = split ' ', <>;\n\nfor $i ( 0 .. $n - 1 ){\n\t$_ = <>; chomp;\n\t/S/ and ( $sx = $i, $sy = index $_, 'S' );\n\t$A[ $i ] = [ split // ];\n\t}\n\t\n$D = <>;\n\nfor( grep /0/ * /1/ * /2/ * /3/, '0000' .. '3333' ){\n\t@i = split //;\n\t\n\t( $X, $Y ) = ( $sx, $sy );\n\t\n\tfor( split /\\B/, $D ){\n\t\t\t\t\t\n\t\t( $i[ $_ ] % 2 ? $X : $Y ) += $i[ $_ ] <=> 1.5;\n\t\t\n\t\t$_ = $A[ $X ][ $Y ];\n\t\t\n\t\tlast if $X < 0 || $X >= $n || $Y < 0 || $Y >= $m ||\n\t\t\t/#/ || /E/ && ++ $V;\n\t\t}\n\t}\n\nprint 0 + $V"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\t\n\tmy @A = map { [ split //, <> =~ s/\\n//r ] } 1 .. $n;\n\t\n\t$debug and print \"@{$_}\" for @A;\n\t\n\tmy $sx;\n\tmy $sy;\n\t\n\tOUT:\n\tfor my $i ( 0 .. $n - 1 ){\n\t\tfor my $j ( 0 .. $m - 1 ){\n\t\t\t$A[ $i ][ $j ] eq 'S' and do {\n\t\t\t\t$sx = $i; $sy = $j;\n\t\t\t\t$A[ $i ][ $j ] = '.';\n\t\t\t\tlast OUT;\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\n\t$debug and print '-' x 5;\n\t$debug and print \"@{$_}\" for @A;\n\t$debug and print \"[$sx|$sy]\";\n\t\n\tmy @dirs = split //, <> =~ s/\\n//r;\n\t\n\t$debug and print \"[@dirs]\";\n\t\n\tmy @v = qw( U D L R );\n\tmy $vars = 0;\n\t\n\tfor my $i1 ( @v ){\n\t\tfor my $i2 ( @v ){\n\t\t\tfor my $i3 ( @v ){\n\t\t\t\tfor my $i4 ( @v ){\n\t\t\t\t\tnext if \"$i1$i2$i3$i4\" =~ /(.).*\\1/;\n\t\t\t\t\tmy @i = split //, \"$i1$i2$i3$i4\";\n\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\tmy $ssx = $sx;\n\t\t\t\t\tmy $ssy = $sy;\n\t\t\t\t\t\n\t\t\t\t\tmy $fail = 0;\n\t\t\t\t\t\n\t\t\t\t\tfor my $j ( @dirs ){\n\t\t\t\t\t\tmy( $dx, $dy ) = ( 0, 0 );\n\t\t\t\t\t\t$i[ $j ] eq 'D' and $dy += 1;\n\t\t\t\t\t\t$i[ $j ] eq 'U' and $dy -= 1;\n\t\t\t\t\t\t$i[ $j ] eq 'L' and $dx -= 1;\n\t\t\t\t\t\t$i[ $j ] eq 'R' and $dx += 1;\n\t\t\t\t\t\t\n\t\t\t\t\t\t$ssx += $dx;\n\t\t\t\t\t\t$ssy += $dy;\n\t\t\t\t\t\t\n\t\t\t\t\t\tif( $ssx < 0 || $ssx >= $n || $ssy < 0 || $ssy >= $m ||\n\t\t\t\t\t\t\t$A[ $ssx ][ $ssy ] eq '#'\n\t\t\t\t\t\t\t){\n\t\t\t\t\t\t\t$fail = 1;\n\t\t\t\t\t\t\tlast;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\tif( $A[ $ssx ][ $ssy ] eq 'E' ){\n\t\t\t\t\t\t\t$vars ++;\n\t\t\t\t\t\t\tlast;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint $vars;\n\t\n\t$debug and print '-' x 20;\n\t}"}], "negative_code": [], "src_uid": "8a1799f4ca028d48d5a12e8ac4b8bee1"} {"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": "$s.=tr/aeiou// while <>;\nprint \"575\" eq $s?\"YES\":\"NO\""}, {"source_code": "$_=<>;\n$a=tr/aeiou//;\n$_=<>;\n$b=tr/aeiou//;\n$_=<>;\n$c=tr/aeiou//;\nprintf \"%s\", ($a==5 && $b==7 && $c==5) ? 'YES' : 'NO';"}, {"source_code": "while(<>){\n $x=tr/aeiou//;\n $s.=\" $x\";\n}\nprint \" 5 7 5\" eq $s?\"YES\":\"NO\""}, {"source_code": "$_=<>;\n$a=tr/aeiou//;\n$_=<>;\n$b=tr/aeiou//;\n$_=<>;\n$c=tr/aeiou//;\nif($a==5 && $b==7 && $c==5){\n print \"YES\";\n}else{\n print \"NO\";\n}"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\n\nmy $line1 = <>;\nmy $line2 = <>;\nmy $line3 = <>;\n\n$line1 =~ s/[^aeiou]//g;\n$line2 =~ s/[^aeiou]//g;\n$line3 =~ s/[^aeiou]//g;\n\nprintf \"%s\\n\", ((length($line1) == 5 &&\n length($line2) == 7 &&\n length($line3) == 5) ? 'YES' : 'NO');\n\n"}, {"source_code": "sub occ{\n@arr=@_;\n@one=grep{$_ eq \"a\" or $_ eq \"i\" or $_ eq \"u\" or $_ eq \"o\" or $_ eq \"e\" }@arr;\n$size=@one;\nreturn $size;\n}\n@arr=split('',<>);\n@arr1=split('',<>);\n@arr2=split('',<>);\nif(occ(@arr)==5 and occ(@arr1)==7 and occ(@arr2)==5){\nprint \"YES\";\n}\nelse{\nprint \"NO\";\n}\n"}, {"source_code": "while(<>){s/[aeiou]/$i++/eg; $s=~s/$/ $i/}\nprint \" 5 12 17\" eq $s?\"YES\\n\":\"NO\\n\""}], "negative_code": [{"source_code": "while(<>) {\n chomp;\n $s.=$_;\n}\nprint $s=~tr/aeiou// == 17?\"YES\":\"NO\""}], "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,\u2009a2,\u2009...,\u2009an 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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of USB flash drives. The second line contains positive integer m (1\u2009\u2264\u2009m\u2009\u2264\u2009105) \u2014 the size of Sean's file. Each of the next n lines contains positive integer ai (1\u2009\u2264\u2009ai\u2009\u2264\u20091000) \u2014 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 \u2014 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 \u2014 the first or the second."}, "positive_code": [{"source_code": "chomp($n = <>);\nchomp($m = <>);\nchomp($a[$_] = <>) for 0..$n - 1;\n@a = sort {$b <=> $a} @a;\nfor (@a) {\n\t$i++;\n\t$s += $_;\n\tlast if $s >= $m;\n}\nprint $i, \"\\n\";\n"}, {"source_code": "use strict;\nmy $n = <>;\nmy $m = <>;\nmy @a;\nfor(my $i = 0; $i < $n; $i++){\n $a[$i] = <>;\n}\n @a = sort{\n $a <=> $b;\n} @a;\nmy $ans = 0;\nmy $j = $#a;\n\nwhile($m > 0){\n $m -= $a[$j];\n $ans++;\n $j--;\n}\nprint $ans;\n"}, {"source_code": "<>;\n$m = <>;\n@n = sort {$b <=> $a} <>;\nfor (@n){\n $i ++;\n $m -= $_, $m <= 0 and last;\n}\nprint $i"}], "negative_code": [{"source_code": "use strict;\nmy $n = <>;\nmy $m = <>;\nmy @a;\nfor(my $i = 0; $i < $n; $i++){\n $a[$i] = <>;\n}\n@a = sort @a;\nmy $ans = 0;\nmy $j = $#a;\n\nwhile($m > 0 && $j >= 0){\n $m -= $a[$j];\n $ans++;\n $j--;\n}\nprint $ans;\n"}, {"source_code": "use strict;\nmy $n = <>;\nmy $m = <>;\nmy @a;\nfor(my $i = 0; $i < $n; $i++){\n $a[$i] = <>;\n}\n@a = sort{\n my $a <=> $b;\n} @a;\nmy $ans = 0;\nmy $j = $#a;\n\nwhile($m > 0){\n $m -= $a[$j];\n $ans++;\n $j--;\n}\nprint $ans;\n"}, {"source_code": "use strict;\nmy $n = <>;\nmy $m = <>;\nmy @a;\nfor(my $i = 0; $i < $n; $i++){\n $a[$i] = <>;\n}\n@a = sort @a;\nmy $ans = 0;\nmy $j = $#a;\n\nwhile($m > 0){\n $m -= $a[$j];\n $ans++;\n $j--;\n}\nprint $ans;\n"}, {"source_code": "use strict;\nopen STDIN, 'output.txt';\nmy $n = <>;\nmy $m = <>;\nmy @a;\nfor(my $i = 0; $i < $n; $i++){\n $a[$i] = <>;\n}\n@a = sort @a;\nmy $ans = 0;\nmy $j = $#a;\n\nwhile($m > 0 && $j >= 0){\n $m -= $a[$j];\n $ans++;\n $j--;\n}\nprint $ans;\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$$$) \u2014 the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) \u2014 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": "my $n = <>;\nwhile ($n--)\n{\n <>; $_ = <>;\n if( $_ ~~ m/8\\d{10}/) {print(\"YES\\n\");}\n else {print(\"NO\\n\");}\n}"}], "negative_code": [], "src_uid": "bcdd7862b718d6bcc25c9aba8716d487"} {"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\u00a0\u2014 determine the number of dangerous turns on the track.", "input_spec": "The first line of the input contains an integer n (4\u2009\u2264\u2009n\u2009\u2264\u20091000)\u00a0\u2014 the number of straight sections of the track. The following (n\u2009+\u20091)-th line contains pairs of integers (xi,\u2009yi) (\u2009-\u200910\u2009000\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u200910\u2009000). The first of these points is the starting position. The i-th straight section of the track begins at the point (xi,\u2009yi) and ends at the point (xi\u2009+\u20091,\u2009yi\u2009+\u20091). 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\u00a0\u2014 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,\u20091). Thus, the answer is 1."}, "positive_code": [{"source_code": "print(<>/2-2)"}, {"source_code": "chomp (my $n = );\n\nmy ($x, $y, $xp, $yp);\n\nchomp (($xp, $yp) = split /\\s+/, );\nchomp (($xp, $yp) = split /\\s+/, );\n\nmy $prevDir = 'N';\nmy $curDir = 'N';\n\nmy $ans = 0;\nfor (my $i = 1; $i < $n; $i++) {\n chomp (($x, $y) = split /\\s+/, );\n\n $curDir = 'N' if ($y > $yp);\n $curDir = 'E' if ($x > $xp);\n $curDir = 'W' if ($x < $xp);\n $curDir = 'S' if ($y < $yp);\n\n $ans++ if (\n (($prevDir eq 'N') && ($curDir eq 'W')) ||\n (($prevDir eq 'E') && ($curDir eq 'N')) ||\n (($prevDir eq 'W') && ($curDir eq 'S')) ||\n (($prevDir eq 'S') && ($curDir eq 'E'))\n );\n\n $prevDir = $curDir;\n ($xp, $yp) = ($x, $y);\n}\n\nprintf \"$ans\\n\";\n"}, {"source_code": "print((<>-4)/2)"}, {"source_code": "print(<>/2-2)"}, {"source_code": "print<>/2-2"}, {"source_code": "print<>/2-2\n"}, {"source_code": "print<>/2-2"}, {"source_code": "$_ = join '', <>;\n\nwhile( /(?=((\\s\\S+){6}))\\n/g ){\n\t@_ = split ' ', $1;\n\t$C += ( $_[0] == $_[2] && ( $_[2] > $_[4] && $_[1] < $_[3] ||\n\t $_[2] < $_[4] && $_[1] > $_[3] ));\n\t$C += ( $_[1] == $_[3] && ( $_[3] < $_[5] && $_[0] < $_[2] ||\n\t $_[3] > $_[5] && $_[0] > $_[2] ));\n\t}\n\nprint $C"}], "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$$$) \u2014 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$$$) \u2014 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$$$) \u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @x = map { $_ - 0 } split(/\\s+/,);\n \n my %ct = ();\n for(my $i=0;$i<$n;$i++){\n $ct{$x[$i]} ++;\n }\n \n my $m2 = -1;\n my $m1 = -1;\n for(my $i=0;$i<$n;$i++){\n last if( $ct{$i} - 0 == 0 );\n $m2 = $i if( $i == $m2 + 1 and $ct{$i} >= 2 );\n $m1 = $i if( $i == $m1 + 1 and $ct{$i} >= 1 );\n }\n $m2 ++;\n $m1 ++;\n my $res = $m1 + $m2;\n \n print \"$res\\n\";\n}\n\nexit(0);\n\n"}], "negative_code": [], "src_uid": "e26b0b117593c159b7f01cfac66a71d1"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the time when the first bus of this route arrives and the interval between two buses of this route.", "output_spec": "Print one number\u00a0\u2014 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": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse feature qw /say/;\n\nmy ($n, $t) = split q{ }, ;\nmy $idx;\n\nmy @time_and_gap = ( (undef)x$n );\nfor ( @time_and_gap ) {\n $_ = [ split q{ }, ];\n}\n\nmy $time;\nmy $gap;\nmy $s;\nmy $mod;\nmy $div;\nmy $min = 1000000;\nmy $min_i = 101;\nmy $i = 0;\nfor ( @time_and_gap ) {\n $i++;\n $time = $_->[0];\n $gap = $_->[1];\n\n if ( $time < $t ) {\n $s = $t - $time;\n $mod = $s % $gap;\n $div = int ( $s/$gap );\n if ( $mod ) {\n $div++;\n }\n $time += $div * $gap;\n }\n \n if ( $time < $min ) {\n $min = $time;\n $min_i = $i;\n }\n\n}\n\nsay $min_i;"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse feature qw /say/;\n\nmy ($n, $t) = split q{ }, ;\nmy $idx;\n\nmy @time_and_gap = ( (undef)x$n );\nfor ( @time_and_gap ) {\n $_ = [ split q{ }, ];\n}\n\nmy $time;\nmy $gap;\nmy $s;\nmy $div;\nmy $min = 1000000;\nmy $min_i = 101;\nmy $i = 0;\nfor ( @time_and_gap ) {\n $i++;\n $time = $_->[0];\n $gap = $_->[1];\n\n if ( $time < $t ) {\n $s = $t - $time;\n $div = int ( $s/$gap );\n $time += $div * $gap;\n }\n if ( $time < $t ) {\n $time += $gap;\n }\n if ( $time < $t ) {\n next;\n }\n \n if ( $time < $min ) {\n $min = $time;\n $min_i = $i;\n }\n\n}\n\nsay $min_i;"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse feature qw /say/;\n\nmy ($n, $t) = split q{ }, ;\nmy $idx;\n\nmy @time_and_gap = ( (undef)x$n );\nfor ( @time_and_gap ) {\n $_ = [ split q{ }, ];\n}\n\nmy $time;\nmy $gap;\nmy $s;\nmy $div;\nmy $min = 1000000;\nmy $min_i = 101;\nmy $i = 0;\nfor ( @time_and_gap ) {\n $i++;\n $time = $_->[0];\n $gap = $_->[1];\n\n \n while ( $time < $t ) {\n $time += $gap;\n }\n \n if ( $time < $min ) {\n $min = $time;\n $min_i = $i;\n }\n\n}\n\nsay $min_i;"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse feature qw /say/;\n\nmy ($n, $t) = split q{ }, ;\n\nmy @time_and_gap = ( (undef)x$n );\nfor ( @time_and_gap ) {\n $_ = [ split q{ }, ];\n}\n\nsub canTake {\n my $t = $_[0];\n my $idx = 1;\n for ( @time_and_gap ) {\n $_->[0] += $_->[1] if $_->[0] < $t;\n if ( $_->[0] == $t ) {\n return $idx;\n }\n $idx++;\n }\n canTake($t+1);\n}\n\nsay canTake($t);"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse feature qw /say/;\n\nmy ($n, $t) = split q{ }, ;\n\nmy @time_and_gap = ( (undef)x$n );\nfor ( @time_and_gap ) {\n $_ = [ split q{ }, ];\n}\n\nsub canTake {\n my $t = $_[0];\n my $idx = 1;\n for ( @time_and_gap ) {\n $_->[0] += $_->[1] if $_->[0] < $t;\n if ( $_->[0] == $t ) {\n say $t;\n say $_->[0];\n return $idx;\n }\n $idx++;\n }\n canTake($t+1);\n}\n\nsay canTake($t);"}], "src_uid": "71be4cccd3b8c494ad7cc2d8a00cf5ed"} {"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": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nmy $n = read_token();\n\nfor ( 1..$n ) {\n my $line = read_line();\n if ( $line =~ /po$/ ) {\n say \"FILIPINO\";\n }\n elsif ( $line =~ /desu$/ or $line =~ /masu$/ ) {\n say \"JAPANESE\";\n }\n else {\n say \"KOREAN\";\n }\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n"}], "negative_code": [], "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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \\le n \\le 30$$$; $$$n$$$ is even)\u00a0\u2014 the number of coins that Phoenix has. ", "output_spec": "For each test case, output one integer\u00a0\u2014 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": "#!perl\nchomp($t=<>);\nwhile ($t--) {\n\tchomp($n=<>);\n\t$a=2**$n; $b=0;\n\tforeach my $i (1..$n-1) {\n\t\tif ($i*2 < $n) {\n\t\t\t$a += 2**$i;\n\t\t}\n\t\telse {\n\t\t\t$b += 2**$i;\n\t\t}\n\t}\n\t$ans=abs($a-$b);\n\tprint \"$ans\\n\";\n}"}, {"source_code": "$t = ;\nchomp $t;\n@test = ;\nchomp @test;\n\nforeach (@test) {\n #print $_;\n$loop = $_/2;\n@coins = ();\n$coins[0] = 2;\nfor ($i=1;$i<$_;$i = $i + 1) {\n push (@coins, $coins[$i-1]*2);\n}\n#foreach $coin (@coins) {\n# print \"\\n $coin\";\n#}\n#print $loop;\n$sum1 = 0;\n$sum2 = 0;\nif ($loop == 1) {\n $ans = $coins[1]-$coins[0];\n print \"$ans\\n\";\n}else{\n for ($j=0;$j<=$loop-2;$j=$j+1) {\n $sum1 = $sum1 + $coins[$j];\n }\n $sum1 = $sum1 + $coins[$_-1];\n for ($k=$loop-1;$j<=$_-2;$j=$j+1) {\n $sum2 = $sum2 + $coins[$j];\n }\n $ans = $sum1 - $sum2;\n #print \"$sum1 $sum2\";\n print \"$ans\\n\";\n}\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $left = 0;\n\t\n\t$left += $_ for map { 2 ** $_ } 1 .. $_ / 2 - 1, $_;\n\t\n\tmy $right = 0;\n\t\n\t$right += $_ for map { 2 ** $_ } $_ / 2 .. $_ - 1;\n\t\n\tprint $left - $right;\n\t}"}], "negative_code": [{"source_code": "$n = ;\nchomp $n;\n$loop = $n/2;\n@coins = ();\n$coins[0] = 2;\nfor ($i=1;$i<$n;$i = $i + 1) {\n push (@coins, $coins[$i-1]*2);\n}\n#foreach (@coins) {\n# print \"\\n $_\";\n#}\n#print $loop;\n#$sum1 = 0;\nif ($loop == 1) {\n print $coins[1]-$coins[0];\n}else{\n for ($j=0;$j<=$loop-2;$j=$j+1) {\n $sum1 = $sum1 + $coins[$j];\n }\n $sum1 = $sum1 + $coins[$n-1];\n for ($k=$loop-1;$j<=$n-2;$j=$j+1) {\n $sum2 = $sum2 + $coins[$j];\n }\n print $sum1-$sum2;\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\u2009\u2264\u2009n,\u2009x\u2009\u2264\u2009105). The next line will contain n space separated integers: c1,\u2009c2,\u2009...,\u2009cn (1\u2009\u2264\u2009ci\u2009\u2264\u2009105).", "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\u2009\u00d7\u20091\u2009=\u20092 hours. Hence you will need to spend 12\u2009+\u20092\u2009=\u200914 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\u2009\u00d7\u20091\u2009=\u20093 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\u2009\u00d7\u20094\u2009=\u20098 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": "chomp($_ = );\n@nums = split / /;\n$x = $nums[1];\nchomp($_ = );\n@nums = split / /;\n@nums = sort {$a <=> $b} @nums;\n$r = 0;\nforeach $n (@nums) {\n\t$r += $x*$n;\n\tif ($x > 1) {\n\t\t$x--;\n\t}\n}\nprint $r;"}, {"source_code": "($n,$x)=split/ /,<>;\n@_=sort {$a<=>$b} (split/ /,<>);\n$U+=$_*$x, $x-1 && $x-- for @_;\nprint $U\n"}, {"source_code": "($n,$x)=split/ /,<>;\n@_=sort {$a<=>$b} (split/ /,<>);\n$U+=$_*$x, $x-1 && $x-- for @_;\nprint $U\n"}, {"source_code": "($n,$x)=split/ /,<>;\n@_=sort {$a<=>$b} (split/ /,<>);\n$U+=$_*$x, $x-1 && $x-- for @_;\nprint $U"}, {"source_code": "($n,$x)=split/ /,<>;\n@_=sort {$a<=>$b} (split/ /,<>);\n$U+=$_*$x, $x-1 && $x-- for @_;\nprint $U\n"}, {"source_code": "($n,$x)=split/ /,<>;\n@_=sort {$a<=>$b} (split/ /,<>);\n$U+=$_*$x, $x-1 && $x-- for @_;\nprint $U\n"}, {"source_code": "use v5.10;\n\n($n, $x) = split / /, <>;\n@c = sort {$a <=> $b} split / /, <>;\nforeach (@c) {\n\t$ans += $_ * $x;\n\t$x>1 and --$x;\n}\nsay $ans;\n"}, {"source_code": "($n,$x)=split/ /,<>;\n@_=sort {$a<=>$b} (split/ /,<>);\n$U+=$_*$x, $x-1 && $x-- for @_;\nprint $U\n"}, {"source_code": "($n,$x)=split/ /,<>;\n@_=sort {$a<=>$b} (split/ /,<>);\n$U+=$_*$x, $x-1 && $x-- for @_;\nprint $U\n"}, {"source_code": "($n,$x)=split/ /,<>;\n@_=sort {$a<=>$b} (split/ /,<>);\n$U+=$_*$x, $x-1 && $x-- for @_;\nprint $U\n"}, {"source_code": "($n,$x)=split/ /,<>;\n@_=sort {$a<=>$b} (split/ /,<>);\n$U+=$_*$x, $x-1 && $x-- for @_;\nprint $U"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$x)=split/ /;\n\t$_=<>;\n\tchomp;\n\t@_=split/ /;\n\t@_=sort {$a<=>$b} @_;\n\t$sum=0;\n\tfor (@_){\n\t\t\n\t\t$sum+=$_*$x;\n\t\t$x==1 or $x--;\n\t\t\n\t\t}\n\tprint \"$sum\\n\";\n\t\n\t}"}, {"source_code": "($n,$x)=split/ /,<>;\n@_=sort {$a<=>$b} (split/ /,<>);\n$U+=$_*$x, $x-1 && $x-- for @_;\nprint $U\n"}], "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 \u2014 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,\u2009x (1\u2009\u2264\u2009n\u2009\u2264\u2009105;\u00a00\u2009\u2264\u2009x\u2009\u2264\u20092\u00b7105) \u2014 the number of Olympiad participants and the minimum number of points Vasya earned. The second line contains n space-separated integers: a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009105) \u2014 the participants' points in the first tour. The third line contains n space-separated integers: b1,\u2009b2,\u2009...,\u2009bn (0\u2009\u2264\u2009bi\u2009\u2264\u2009105) \u2014 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 \u2014 there are two integers i,\u2009j (1\u2009\u2264\u2009i,\u2009j\u2009\u2264\u2009n) such, that ai\u2009+\u2009bj\u2009\u2265\u2009x.", "output_spec": "Print two space-separated integers \u2014 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 \u2014 {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 \u2014 {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": "($n, $x) = split(' ', <>);\n@a = split(' ', <>); @a = sort{$a <=> $b} @a;\n@b = split(' ', <>); @b = sort{$a <=> $b} @b;\n\nforeach(@a){\n\tif($_ + $b[$n - 1] >= $x){\n\t\t$n --; $re ++;}}\nprint \"1 $re\";\n\n"}], "negative_code": [{"source_code": "($n, $x) = split(' ', <>);\n@a = split(' ', <>); @a = sort{$a <=> $b} @a;\n@b = split(' ', <>); @b = sort{$a <=> $b} @b;\n\nforeach(@b){\n\tprint \"$_, \";}\n\tprint \"\\n\";\nforeach(@a){\n\tif($_ + $b[$n - 1] >= $x){\n\t\t$n --; $re ++;}}\nprint \"1 $re\";\n\n"}, {"source_code": "($n, $x) = split(' ', <>);\n@a = split(' ', <>); @a = sort{$a <=> $b} @a;\n@b = split(' ', <>); @b = sort{$a <=> $b} @b;\n\nforeach(@a){\n\t$re ++ and $n --\n\tif $_ + $b[$n - 1] >= x;}\nprint \"1 $re\";\n\n"}], "src_uid": "77919677f562a6fd1af64bc8cbc79de5"} {"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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\tchomp;\n\t\n\t$debug and print;\n\t\n\tmy $min = substr $_, 0, 1;\n\t\n\tmy @ans = 'Mike';\n\t\n\tfor my $i ( 1 .. -1 + length ){\n\t\tmy $letter = substr $_, $i, 1;\n\t\t\n\t\t$debug and print \"$min lt $letter\";\n\t\t\n\t\tif( $min lt $letter ){\n\t\t\tpush @ans, 'Ann';\n\t\t\t}\n\t\telse{\n\t\t\tpush @ans, 'Mike';\n\t\t\t}\n\t\t\n\t\t$letter lt $min and $min = $letter;\n\t\t}\n\t\n\tprint for @ans;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 1;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\tchomp;\n\t\n\t$debug and print;\n\t\n\tmy $min = substr $_, 0, 1;\n\t\n\tmy @ans = 'Mike';\n\t\n\tfor my $i ( 1 .. -1 + length ){\n\t\tmy $letter = substr $_, $i, 1;\n\t\t\n\t\t$debug and print \"$min lt $letter\";\n\t\t\n\t\tif( $min lt $letter ){\n\t\t\tpush @ans, 'Ann';\n\t\t\t}\n\t\telse{\n\t\t\tpush @ans, 'Mike';\n\t\t\t}\n\t\t\n\t\t$letter lt $min and $min = $letter;\n\t\t}\n\t\n\tprint for @ans;\n\t}"}], "src_uid": "1e107d9fb8bead4ad942b857685304c4"} {"nl": {"description": "Suppose you have an integer $$$v$$$. In one operation, you can: either set $$$v = (v + 1) \\bmod 32768$$$ or set $$$v = (2 \\cdot v) \\bmod 32768$$$. You are given $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$. What is the minimum number of operations you need to make each $$$a_i$$$ equal to $$$0$$$?", "input_spec": "The first line contains the single integer $$$n$$$ ($$$1 \\le n \\le 32768$$$)\u00a0\u2014 the number of integers. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i < 32768$$$).", "output_spec": "Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the minimum number of operations required to make $$$a_i$$$ equal to $$$0$$$.", "sample_inputs": ["4\n19 32764 10240 49"], "sample_outputs": ["14 4 4 15"], "notes": "NoteLet's consider each $$$a_i$$$: $$$a_1 = 19$$$. You can, firstly, increase it by one to get $$$20$$$ and then multiply it by two $$$13$$$ times. You'll get $$$0$$$ in $$$1 + 13 = 14$$$ steps. $$$a_2 = 32764$$$. You can increase it by one $$$4$$$ times: $$$32764 \\rightarrow 32765 \\rightarrow 32766 \\rightarrow 32767 \\rightarrow 0$$$. $$$a_3 = 10240$$$. You can multiply it by two $$$4$$$ times: $$$10240 \\rightarrow 20480 \\rightarrow 8192 \\rightarrow 16384 \\rightarrow 0$$$. $$$a_4 = 49$$$. You can multiply it by two $$$15$$$ times. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($n) = map { $_ - 0 } split(/\\s+/,);\r\nmy @A = map { $_ - 0 } split(/\\s+/,);\r\n\r\nmy @resa = ();\r\nfor(my $i=0;$i<$n;$i++){\r\n my $a = $A[$i];\r\n my $res = 16;\r\n \r\n for(my $j=0;$j<=16;$j++){\r\n my $c = $j;\r\n my $v = ( $a + $c ) % 32768;\r\n while($v){\r\n $c++;\r\n $v = ($v<<1) % 32768;\r\n }\r\n $res = &min($res,$c);\r\n }\r\n push(@resa,$res);\r\n}\r\nprint ( join(' ',@resa) . \"\\n\" );\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "negative_code": [], "src_uid": "5c844c0dc0eb718aea7b2446e90ce250"} {"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\u2009\u2264\u2009x'\u2009\u2264\u2009x and 0\u2009\u2264\u2009y'\u2009\u2264\u2009y 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'\u2009\u2265\u2009x and y'\u2009\u2265\u2009y 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,\u2009y)\u2009=\u2009y\u2009-\u2009x. 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,\u2009yi)\u2009=\u2009yi\u2009-\u2009xi\u2009=\u2009wi.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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 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\u2009\u2264\u2009x,\u2009y\u2009\u2264\u2009100\u2009000), 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\u2009\u2264\u2009x'\u2009\u2264\u2009x and 0\u2009\u2264\u2009y'\u2009\u2264\u2009y, are also present in the input. The last line of the input contains n integers. The i-th of them is wi (\u2009-\u2009100\u2009000\u2009\u2264\u2009wi\u2009\u2264\u2009100\u2009000)\u00a0\u2014 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,\u2009yi)\u2009=\u2009yi\u2009-\u2009xi\u2009=\u2009wi, 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\u2009-\u2009xi\u2009=\u2009wi.In the second sample, the special values of the points in the set are 0, \u2009-\u20091, and \u2009-\u20092 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist."}, "positive_code": [{"source_code": "($n, @_) = <>;\n$_ = pop @_;\npush @{ $H{ eval join '-', reverse split } }, $_ for @_;\n@{ $H{ $_ } } = sort {$a <=> $b} @{ $H{ $_ } } for keys %H;\n@_ = map { shift @{ $H{ $_ } } } split;\n(grep ! defined, @_) ?\n\tprint \"NO\\n\"\n:\n\tdo {\n\t\tfor (1 .. @_ -1){\n\t\t\t($x1, $y1, $x2, $y2) = map split, $_[$_-1], $_[$_];\n\t\t\t$x1 >= $x2 and $y1 >= $y2 and ++ $f and last;\n\t\t}\n\t\t\n\tprint $f ? \"NO\\n\" : (\"YES\\n\", @_)\n\t\n\t}\n"}], "negative_code": [{"source_code": "($n, @_) = <>;\n$_ = pop @_;\npush @{ $H{ eval join '-', reverse split } }, $_ for @_;\n@{ $H{ $_ } } = sort {$a <=> $b} @{ $H{ $_ } } for keys %H;\n@_ = map { shift @{ $H{ $_ } } } split;\n(grep ! defined, @_) ?\n\tprint \"NO\\n\"\n:\n\tdo {\n\t\tfor (1 .. @_ -1){\n\t\t\t$_ = join '<=', ( split ' ', $_[$_-1] . $_[$_] )[0, 2, 1, 3];\n\t\t\ts/<=.*?\\K<=/ and /;\n\t\t\t(eval) or $f ++;\n\t\t}\n\t\t\n\tprint $f ? \"NO\\n\" : (\"YES\\n\", @_)\n\t\n\t}\n"}, {"source_code": "($n, @_) = <>;\n$_ = pop @_;\npush @{ $H{ eval join '-', reverse split } }, $_ for @_;\n@{ $H{ $_ } } = sort {$a <=> $b} @{ $H{ $_ } } for keys %H;\n@_ = map { shift @{ $H{ $_ } } } split;\nprint grep( ! defined, @_) ? \"NO\\n\" : (\"YES\\n\", @_)"}, {"source_code": "($n, @_) = <>;\n$_ = pop @_;\npush @{ $H{ eval join '-', reverse split } }, $_ for @_;\n@{ $H{ $_ } } = sort {$a <=> $b} @{ $H{ $_ } } for keys %H;\n@_ = map { shift @{ $H{ $_ } } } split;\n(grep ! defined, @_) ?\n\tprint \"NO\\n\"\n:\n\tdo {\n\t\tfor (1 .. @_ -1){\n\t\t\t$_ = join '>=', ( split ' ', $_[$_-1] . $_[$_] )[0, 2, 1, 3];\n\t\t\ts/>.*?\\K>/ and /;\n\t\t\t(eval) and $f ++;\n\t\t}\n\t\t\n\tprint $f ? \"NO\\n\" : (\"YES\\n\", @_)\n\t\n\t}\n"}, {"source_code": "($n, @_) = <>;\n$_ = pop @_;\npush @{ $H{ eval join '-', reverse split } }, $_ for @_;\n@{ $H{ $_ } } = sort {$a <=> $b} @{ $H{ $_ } } for keys %H;\n@_ = map { shift @{ $H{ $_ } } } split;\n(grep ! defined, @_) ?\n\tprint \"NO\\n\"\n:\n\tdo {\n\t\tfor (1 .. @_ -1){\n\t\t\t$_ = join '<=', ( split ' ', $_[$_-1] . $_[$_] )[0, 2, 1, 3];\n\t\t\ts/>.*?\\K>/ and /;\n\t\t\t(eval) or $f ++;\n\t\t}\n\t\t\n\tprint $f ? \"NO\\n\" : (\"YES\\n\", @_)\n\t\n\t}\n"}, {"source_code": "($n, @_) = <>;\n$_ = pop @_;\npush @{ $H{ eval join '-', reverse split } }, $_ for @_;\n@_ = map { shift @{ $H{ $_ } } } split;\nprint grep( ! defined, @_) ? \"NO\\n\" : (\"YES\\n\", @_)"}, {"source_code": "($n, @_) = <>;\n$_ = pop @_;\npush @{ $H{ eval join '-', reverse split } }, $_ for @_;\n@{ $H{ $_ } } = sort {$a <=> $b} @{ $H{ $_ } } for keys %H;\n@_ = map { shift @{ $H{ $_ } } } split;\n(grep ! defined, @_) ?\n\tprint \"NO\\n\"\n:\n\tdo {\n\t\tfor (1 .. @_ -1){\n\t\t\t$_ = join '>', ( split ' ', $_[$_-1] . $_[$_] )[0, 2, 1, 3];\n\t\t\ts/>.*?\\K>/ and /;\n\t\t\t(eval) and $f ++;\n\t\t}\n\t\t\n\tprint $f ? \"NO\\n\" : (\"YES\\n\", @_)\n\t\n\t}\n"}], "src_uid": "ad186f6df24d31243cde541b7fc69a8c"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\n# Alice Bob Tie\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n $#A = $n - 1;\r\n my @sa = sort { $b <=> $a } @A;\r\n my @ev = grep { $_ % 2 == 0 } @sa;\r\n my @od = grep { $_ % 2 == 1 } @sa;\r\n my $i_e = 0; $i_o = 0;\r\n \r\n my $a_sc = 0; my $b_sc = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n my $v = $sa[$i];\r\n if( $i % 2 == 0 ){\r\n $a_sc += $v if $v % 2 == 0;\r\n } else {\r\n $b_sc += $v if $v % 2 == 1;\r\n }\r\n }\r\n print ($a_sc > $b_sc ? \"Alice\\n\" : ( $a_sc < $b_sc ? \"Bob\\n\" : \"Tie\\n\" ));\r\n next;\r\n \r\n my $min_idx = &min( $#ev, $#od );\r\n \r\n my $a_es = 0;\r\n my $b_es = 0;\r\n for(my $i=0;$i<=$min_idx;$i++){\r\n $a_es += $ev[$i];\r\n $b_es += $od[$i];\r\n }\r\n \r\n \r\n \r\n for(my $i=0;$i<$n;$i++){\r\n my $v = undef;\r\n if( $i_e > $#ev or $i_o > $#od ){\r\n $v = $ev[$i_e++] if $i_e <= $#ev;\r\n $v = $od[$i_o++] if $i_o <= $#od;\r\n if( $i % 2 == 0 ){\r\n $a_sc += $v if $v % 2 == 0;\r\n } else {\r\n $b_sc += $v if $v % 2 == 1;\r\n }\r\n next;\r\n }\r\n my $tu = ( $i_o < $#od ? $od[$i_o] - $od[$i_o+1] : $od[$i_o] );\r\n my $so = ( $i_e < $#ev ? $ev[$i_e] - $ev[$i_e+1] : $ev[$i_e] );\r\n if( $i % 2 == 0 ){\r\n if( $tu >= $so ){\r\n $v = $od[$i_o++];\r\n } else {\r\n $v = $ev[$i_e++];\r\n }\r\n $a_sc += $v if $v % 2 == 0;\r\n } else {\r\n #B\r\n if( $so >= $tu ){\r\n $v = $ev[$i_e++];\r\n } else {\r\n $v = $od[$i_o++];\r\n }\r\n $b_sc += $v if $v % 2 == 1;\r\n }\r\n }\r\n print ($a_sc > $b_sc ? \"Alice\\n\" : ( $a_sc < $b_sc ? \"Bob\\n\" : \"Tie\\n\" ));\r\n}\r\n\r\nexit(0);\r\n\r\nsub min {\r\n my ($x,$y) = @_;\r\n return ( ( $x < $y ) ? $x : $y );\r\n}\r\n\r\nsub max {\r\n my ($x,$y) = @_;\r\n return ( ( $x > $y ) ? $x : $y );\r\n}\r\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @L;\n\tmy @N;\n\t\n\tfor( @_ ){\n\t\tif( $_ % 2 == 0 ){\n\t\t\tpush @L, $_;\n\t\t\t}\n\t\telse{\n\t\t\tpush @N, $_;\n\t\t\t}\n\t\t}\n\t\n\t@L = sort { $b <=> $a } @L;\n\t@N = sort { $b <=> $a } @N;\n\t\n\tmy $A = 0;\n\tmy $B = 0;\n\t\n\tmy $move = 0;\n\t\n\twhile( @L or @N ){\n\t\t\n\t\tif( 0 ){ ; }\n\t\telsif( $move == 0 ){\n\t\t\tif( @L and @N ){\n\t\t\t\tif( $L[ 0 ] > $N[ 0 ] ){\n\t\t\t\t\t$A += shift @L;\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tshift @N;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telsif( @L ){\n\t\t\t\t$A += shift @L;\n\t\t\t\t}\n\t\t\telsif( @N ){\n\t\t\t\tshift @N;\n\t\t\t\t}\n\t\t\t}\n\t\telsif( $move == 1 ){\n\t\t\tif( @L and @N ){\n\t\t\t\tif( $L[ 0 ] < $N[ 0 ] ){\n\t\t\t\t\t$B += shift @N;\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tshift @L;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telsif( @L ){\n\t\t\t\tshift @L;\n\t\t\t\t}\n\t\t\telsif( @N ){\n\t\t\t\t$B += shift @N;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t$move = 1 - $move;\n\t\t}\n\t\n\tprint qw( Tie Alice Bob )[ $A <=> $B ];\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\n# Alice Bob Tie\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n $#A = $n - 1;\r\n my @ev = sort { $b <=> $a } grep { $_ % 2 == 0 } @A;\r\n my @od = sort { $b <=> $a } grep { $_ % 2 == 1 } @A;\r\n my $i_e = 0; $i_o = 0;\r\n \r\n my $a_sc = 0; my $b_sc = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n my $v = undef;\r\n if( $i_e > $#ev or $i_o > $#od ){\r\n $v = $ev[$i_e++] if $i_e <= $#ev;\r\n $v = $od[$i_o++] if $i_o <= $#od;\r\n if( $i % 2 == 0 ){\r\n $a_sc += $v if $v % 2 == 0;\r\n } else {\r\n $b_sc += $v if $v % 2 == 1;\r\n }\r\n next;\r\n }\r\n my $tu = ( $i_o < $#od ? $od[$i_o] - $od[$i_o+1] : $od[$i_o] );\r\n my $so = ( $i_e < $#ev ? $ev[$i_e] - $ev[$i_e+1] : $ev[$i_e] );\r\n if( $i % 2 == 0 ){\r\n if( $tu >= $so ){\r\n $v = $od[$i_o++];\r\n } else {\r\n $v = $ev[$i_e++];\r\n }\r\n $a_sc += $v if $v % 2 == 0;\r\n } else {\r\n #B\r\n if( $so >= $tu ){\r\n $v = $ev[$i_e++];\r\n } else {\r\n $v = $od[$i_o++];\r\n }\r\n $b_sc += $v if $v % 2 == 1;\r\n }\r\n }\r\n print ($a_sc > $b_sc ? \"Alice\\n\" : ( $a_sc < $b_sc ? \"Bob\\n\" : \"Tie\\n\" ));\r\n}\r\n\r\nexit(0);\r\n\r\n"}], "src_uid": "b1e911fbc33fb031b2398cdd545f502a"} {"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\u2009\u2264\u2009|s|\u2009\u2264\u2009100000).", "output_spec": "Print one number \u2014 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": "\n\n# read input\nmy $str = ;\nchomp ( $str );\n\n# insert each character into an index in the array\nmy @arr = split m//, $str;\n\n# insert the keys and initial values into the hashes\nmy %prev;\nmy %max;\nfor (@arr) {\n $prev{ $_ } = $max{ $_ } = -1; }\n\n# iterate through the string\nfor ( my $j = 0; $j < (@arr); $j += 1 ) {\n if ( $j - $prev{ $arr[ $j ] } > $max{ $arr[ $j ] } ) {\n\t$max{ $arr[ $j ] } = $j - $prev{ $arr[ $j ] };}\n $prev{ $arr[ $j ] } = $j;}\n\n# don't forget the last substrings\nfor ( my $i = 0; $i < (@arr); $i += 1 ) {\n if ( (@arr) - $prev{ $arr[ $i ] } > $max{ $arr[ $i ] } ) {\n\t$max{ $arr[ $i ] } = @arr - $prev{ $arr[ $i ] }; }}\n\n# find the minimum maximum value\nmy $min = 100000000;\nfor (@arr) {\n if ( $max{ $_ } < $min && $max{ $_ } > 0 ) {\n\t$min = $max{ $_ }; }}\n\n# print answer\nprint \"$min\\n\";\n\n\n"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nsub min {\n my $min = shift;\n for my $x (@_) {\n $min = $x if $x < $min;\n }\n $min;\n}\n\nmy %last;\nmy %max_dist;\n\nchomp (my $s = <>);\nfor my $i (0..length($s) - 1) {\n my $c = substr $s, $i, 1;\n\n my $dist = $i - ($last{$c} // -1);\n $max_dist{$c} = $dist if !exists $max_dist{$c} || $dist > $max_dist{$c};\n\n $last{$c} = $i;\n}\n\nfor my $c (keys %max_dist) {\n my $dist = length($s) - $last{$c};\n $max_dist{$c} = $dist if $dist > $max_dist{$c};\n}\n\nsay min(values %max_dist);\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t@_ = ();\n\t\n\tfor my $c ( 'a' .. 'z' ){\n\t\t\n\t\t/$c/ and push @_, 1 + ( sort { $b <=> $a } map length, grep $_, split $c )[ 0 ];\n\t\t}\n\t\n\tprint 0 + ( sort { $a <=> $b } @_ )[ 0 ];\n\t}"}, {"source_code": "$_ = <>, chomp;\n\n$m = ~0;\n\t\nfor $c ( 'a' .. 'z' ){\n\t( $q ) = sort { $b <=> $a } map length, split $c;\n\t$q < $m and $m = $q;\n\t}\n\nprint 1 + $m"}], "negative_code": [{"source_code": "\n\n# read input\nmy $str = ;\nchomp ( $str );\n\n# insert each character into an index in the array\nmy @arr = split m//, $str;\n\n# insert the keys and initial values into the hashes\nmy %prev;\nmy %max;\nfor (@arr) {\n $prev{ $_ } = $max{ $_ } = -1; }\n\n# iterate through the string\nfor ( my $j = 0; $j < (@arr); $j += 1 ) {\n if ( $j - $prev{ $arr[ $j ] } > $max{ $arr[ $j ] } ) {\n\t$max{ $arr[ $j ] } = $j - $prev{ $arr[ $j ] };}\n $prev{ $arr[ $j ] } = $j;}\n\n# don't forget the last substrings\nfor ( my $i = 0; $i < (@arr); $i += 1 ) {\n if ( (@arr-1) - $prev{ $arr[ $i ] } > $max{ $arr[ $i ] } ) {\n\t$max{ $arr[ $i ] } = @arr - 1 - $prev{ $arr[ $i ] }; }}\n\n\n# find the minimum maximum value\nmy $min = 100000000;\nfor (@arr) {\n if ( $max{ $_ } < $min && $max{ $_ } > 0 ) {\n\t$min = $max{ $_ }; }}\n\n# print answer\nprint \"$min\\n\";\n\n\n"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nsub min {\n my $min = shift;\n for my $x (@_) {\n $min = $x if $x < $min;\n }\n $min;\n}\n\nmy %last;\nmy %max_dist;\n\nchomp (my $s = <>);\nfor my $i (0..length($s) - 1) {\n my $c = substr $s, $i, 1;\n\n my $dist = $i - ($last{$c} // 0);\n $max_dist{$c} = $dist if !exists $max_dist{$c} || $dist > $max_dist{$c};\n\n $last{$c} = $i;\n}\n\nfor my $c (keys %max_dist) {\n my $dist = length($s) - $last{$c};\n $max_dist{$c} = $dist if $dist > $max_dist{$c};\n}\n\nsay min(values %max_dist);\n"}], "src_uid": "81a558873e39efca5066ef91a9255f11"} {"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$$$)\u00a0\u2014 the number of test cases. Next $$$t$$$ lines contain test cases\u00a0\u2014 one per test case. The one and only line of each test case contains the single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$)\u00a0\u2014 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": "use bigint;\nchomp(my $t = );\nforeach (1..$t)\n{\n chomp (my $n = );\n my $c = ($n + 3) / 4;\n print '9' x ($n - $c) . '8' x $c, \"\\n\";\n}"}, {"source_code": "use strict;\n\nmy $try = <>;\n\nwhile($try--)\n{\n my $n = <>;\n my $v = int($n/4);\n $v += 1 if ($n % 4) > 0;\n for(my $i = 1; $i <= $n-$v; $i++){\n print \"9\";\n }\n for(my $i = 1; $i <= $v; $i++){\n print \"8\";\n }\n print \"\\n\";\n}"}], "negative_code": [{"source_code": "use strict;\n\nmy $try = <>;\n\nwhile($try--)\n{\n\tmy $n = <>;\n\tfor(my $i = 0; $i < $n-1; $i++){\n\t\tprint \"9\";\n }\n\tprint \"8\\n\";\n}"}], "src_uid": "80c75a4c163c6b63a614075e094ad489"} {"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,\u2009i,\u2009r1,\u2009i). Also he has m variants when he will attend programming classes, i-th variant is given by a period of time (l2,\u2009i,\u2009r2,\u2009i).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,\u2009r1) and (l2,\u2009r2) 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\u2009-\u2009j|, where l1\u2009\u2264\u2009i\u2009\u2264\u2009r1 and l2\u2009\u2264\u2009j\u2009\u2264\u2009r2. 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\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the number of time periods when Anton can attend chess classes. Each of the following n lines of the input contains two integers l1,\u2009i and r1,\u2009i (1\u2009\u2264\u2009l1,\u2009i\u2009\u2264\u2009r1,\u2009i\u2009\u2264\u2009109)\u00a0\u2014 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\u2009\u2264\u2009m\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the number of time periods when Anton can attend programming classes. Each of the following m lines of the input contains two integers l2,\u2009i and r2,\u2009i (1\u2009\u2264\u2009l2,\u2009i\u2009\u2264\u2009r2,\u2009i\u2009\u2264\u2009109)\u00a0\u2014 the i-th variant of a period of time when Anton can attend programming classes.", "output_spec": "Output one integer\u00a0\u2014 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,\u20093) and attend programming classes in the period (6,\u20098). 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": "sub iter($) {\n\t$n = shift;\n\t$rmin = 2e9; $lmax = 0;\n\twhile ($n--) {\n\t\t($l, $r) = split \" \", <>;\n\t\tif ($r < $rmin) { $rmin = $r }\n\t\tif ($l > $lmax) { $lmax = $l }\n\t}\n\t($rmin, $lmax)\n}\n\n@a = iter <>;\n@b = iter <>;\n$a = $b[1] - $a[0];\n$b = $a[1] - $b[0];\n$ans = $a > $b? $a: $b;\n$ans = $ans > 0? $ans: 0;\nprint $ans;"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $min1 = 1000000000;\nmy $min2 = 1000000000;\nmy $max1 = 0;\nmy $max2 = 0;\n\nmy $n = <>;\n$n =~ s/\\s+$//;\n\nsub min {\n my $ans = $_[0];\n for my $x (@_) {\n if ($ans > $x) {\n $ans = $x;\n }\n }\n return $ans;\n}\n\nsub max {\n my $ans = $_[0];\n for my $x (@_) {\n if ($ans < $x) {\n $ans = $x;\n }\n }\n return $ans;\n}\n\nfor my $i (1 .. $n) {\n my $tmp = <>;\n $tmp =~ s/\\s+$//;\n my @tmp = split /\\s+/, $tmp;\n $min1 = min($min1, $tmp[1]);\n $max1 = max($max1, $tmp[0]);\n}\n\nmy $m = <>;\n$m =~ s/\\s+$//;\n\nfor my $i (1 .. $m) {\n my $tmp = <>;\n $tmp =~ s/\\s+$//;\n my @tmp = split /\\s+/, $tmp;\n $min2 = min($min2, $tmp[1]);\n $max2 = max($max2, $tmp[0]);\n}\n\nprint max(0, $max1 - $min2, $max2 - $min1);\n\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $min1 = 1000000000;\nmy $min2 = 1000000000;\nmy $max1 = 0;\nmy $max2 = 0;\n\nmy $n = <>;\n$n =~ s/\\s+$//;\n\nsub min {\n my $ans = $_[0];\n for my $x (@_) {\n if ($ans > $x) {\n $ans = $x;\n }\n }\n return $ans;\n}\n\nsub max {\n my $ans = $_[0];\n for my $x (@_) {\n if ($ans < $x) {\n $ans = $x;\n }\n }\n return $ans;\n}\n\nfor my $i (1 .. $n) {\n my $tmp = <>;\n $tmp =~ s/\\s+$//;\n my @tmp = split /\\s+/, $tmp;\n if ($tmp[1] < $min1) {\n $min1 = $tmp[1];\n }\n if ($tmp[0] > $max1) {\n $max1 = $tmp[0];\n }\n}\n\nmy $m = <>;\n$m =~ s/\\s+$//;\n\nfor my $i (1 .. $m) {\n my $tmp = <>;\n $tmp =~ s/\\s+$//;\n my @tmp = split /\\s+/, $tmp;\n if ($tmp[1] < $min2) {\n $min2 = $tmp[1];\n }\n if ($tmp[0] > $max2) {\n $max2 = $tmp[0];\n }\n}\n\nprint max(0, $max1 - $min2, $max2 - $min1);\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(!eof){\n\tmy $left = 1e9;\n\tmy $right = 0;\n\tmy $left_2 = 1e9;\n\tmy $right_2 = 0;\n\tmy $n = <>;\n\tfor my $i (1 .. $n){\n\t\tmy( $l, $r )= split ' ', <>;\n\t\t$r < $left and $left = $r;\n\t\t$l > $right_2 and $right_2 = $l;\n\t\t}\n\tmy $m = <>;\n\tfor my $i (1 .. $m){\n\t\tmy( $l, $r )= split ' ', <>;\n\t\t$r < $left_2 and $left_2 = $r;\n\t\t$l > $right and $right = $l;\n\t\t}\n\t\n\tprint +( sort { $a <=> $b } 0, $right - $left, $right_2 - $left_2 )[ 2 ];\n\t\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(!eof){\n\tmy $left = 3e5;\n\tmy $right = 0;\n\tmy $left_2 = 3e5;\n\tmy $right_2 = 0;\n\tmy $n = <>;\n\tfor my $i (1 .. $n){\n\t\tmy( $l, $r )= split ' ', <>;\n\t\t$r < $left and $left = $r;\n\t\t$l > $right_2 and $right_2 = $l;\n\t\t}\n\tmy $m = <>;\n\tfor my $i (1 .. $m){\n\t\tmy( $l, $r )= split ' ', <>;\n\t\t$r < $left_2 and $left_2 = $r;\n\t\t$l > $right and $right = $l;\n\t\t}\n\t\n\tprint +( sort { $a <=> $b } 0, $right - $left, $right_2 - $left_2 )[ 2 ];\n\t\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(!eof){\n\tmy $left = 3e5;\n\tmy $right = 0;\n\tmy $n = <>;\n\tfor my $i (1 .. $n){\n\t\tmy $r = ( split ' ', <> )[ 1 ];\n\t\t$r < $left and $left = $r;\n\t\t}\n\tmy $m = <>;\n\tfor my $i (1 .. $m){\n\t\tmy $l = ( split ' ', <> )[ 0 ];\n\t\t$l > $right and $right = $l;\n\t\t}\n\t\n\tprint +( sort { $a <=> $b } 0, $right - $left )[ 1 ];\n\t\n\t}"}], "src_uid": "4849a1f65afe69aad12c010302465ccd"} {"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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map { eval join '/', split } map ~~<>, 1 .. $n;\n\t\n\tprint $m * ( sort { $a <=> $b } @_ )[ 0 ];\n\t}"}, {"source_code": "print +( split )[ 1 ] * ( sort { $a <=> $b } map { eval s! !/!r } <> )[ 0 ] while <>"}], "negative_code": [{"source_code": "print +( split )[ 1 ] * ( sort { $a <=> $b } map { eval s! !/!r } <> )[ 0 ] for <>"}, {"source_code": "print +( split ) * ( sort { $a <=> $b } map { eval s! !/!r } <> )[ 0 ] for <>"}, {"source_code": "print +( split ) * ( sort { $b <=> $a } map { eval s! !/!r } <> ) while <>"}], "src_uid": "3bbe48918a7bf3faf01c74cb7296f6e0"} {"nl": {"description": "The winner of the card game popular in Berland \"Berlogging\" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line \"name score\", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points.", "input_spec": "The first line contains an integer number n (1\u2009\u2009\u2264\u2009\u2009n\u2009\u2009\u2264\u2009\u20091000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in \"name score\" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.", "output_spec": "Print the name of the winner.", "sample_inputs": ["3\nmike 3\nandrew 5\nmike 2", "3\nandrew 3\nandrew 2\nmike 5"], "sample_outputs": ["andrew", "andrew"], "notes": null}, "positive_code": [{"source_code": "my $n = <>;\nmy (@name, @point);\nmy @table = [];\nmy ($ans, $max) = (0, 0);\nmy %hash;\nfor my $i (1..$n) {\n ($name[$i], $point[$i]) = split \" \", <>;\n $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});\n $hash{$name[$i]} += $point[$i];\n push @table, [$name[$i], $hash{$name[$i]}];\n}\n$max = $_ > $max ? $_ : $max foreach(values %hash);\n# print \"\\n-----------\\n\";\nfor my $i (1..$n) {\n if ($hash{$name[$i]} == $max && $table[$i][1] >= $max) {\n print $name[$i] .\"\\n\";\n last;\n }\n}"}, {"source_code": "use strict;\nuse warnings;\n\nmy $players = {};\nmy $score_log = [];\n\nmy $n_inputs = <>;\n\nfor ( 1 .. $n_inputs ) {\n my $line = <>;\n my ( $name, $score, @rest ) = split /\\s+/s, $line;\n\n if ( not exists $players->{$name} ) {\n $players->{$name} = 0;\n }\n my $new_score = $players->{$name} + $score;\n push @{ $score_log }, { name => $name, score => $new_score };\n $players->{ $name } = $new_score;\n}\nmy %winning_players;\nmy ( $winning_score, ) = sort { $b <=> $a } values %{ $players };\nfor ( grep { $winning_score == $players->{$_} } keys %{ $players } ) {\n $winning_players{ $_ } = 1;\n}\nfor my $entry ( @{ $score_log } ) {\n next unless exists $winning_players{ $entry->{name} };\n next unless $entry->{score} >= $winning_score;\n printf \"%s\\n\", $entry->{name};\n exit 0;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy @round;\nmy %score;\n\nfor (my $i = 0; $i < $n; $i++) {\n my $line = <>;\n chomp $line;\n push @round, $line;\n my @tuple = split ' ', $round[$i];\n if (not defined($score{$tuple[0]})) {\n $score{$tuple[0]} = $tuple[1];\n } else {\n $score{$tuple[0]} += $tuple[1];\n }\n}\n\nmy $max = 0;\nforeach (keys(%score)) {\n $max = $score{$_} if ($score{$_} > $max);\n}\n\nmy %t;\nfor (my $i = 0; $i < $n; $i++) {\n my @tuple = split ' ', $round[$i];\n if (not defined($t{$tuple[0]})) {\n $t{$tuple[0]} = $tuple[1];\n } else {\n $t{$tuple[0]} += $tuple[1];\n }\n\n if ($score{$tuple[0]} == $max and $t{$tuple[0]} >= $max) {\n print $tuple[0];\n last;\n }\n}\n"}, {"source_code": "$n=;\n%hash;\n@list;\n$i=0;\n$max=-99999999999999;\n$winner;\nwhile($n--)\n{\n $tmp=;\n @tmp2=split(\" \",$tmp);\n $name=$tmp2[0]; $score=$tmp2[1];\n if($hash{$name})\n {\n $hash{$name}+=$score;\n }\n else\n {\n $hash{$name}=$score;\n }\n $list[$i++]=$name;\n $list[$i++]=$hash{$name};\n}\n#print $max,\"\\n\";\n$flag=1;\nforeach $key (%hash)\n {\n # print $hash{$key},\"\\n\";\n if($hash{$key}>$max)\n {\n $max=$hash{$key};\n $winner=$key;\n }\n }\n\nfor($j=0;$j<$i && $flag;$j+=2)\n{\n #print \"$list[$j] $list[$j+1] $hash{$list[$j]} $max\\n\";\n if($list[$j+1]>=$max && $hash{$list[$j]}==$max && $flag)\n {\n print $list[$j];\n $flag=0;\n }\n}"}, {"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nmy $totalNum = ;\nmy %finResults;\nmy @hisResults;\n\nmy @inputs;\nfor(my $i = 0; $i < $totalNum; $i++){\n my $input = ;\n @inputs = split( \" \", $input);\n $finResults{$inputs[0]} += $inputs[1];\n my @record = ($inputs[0], $finResults{$inputs[0]} );\n push @hisResults, \\@record;\n# if (! exists $hisResults{$inputs[0]}){\n# $hisResults{$inputs[0]} = [$inputs[1]]; \n# }\n# else{\n# my $arrayRef = $hisResults{$inputs[0]};\n# if($$arrayRef[$#$arrayRef] < $finResults{$inputs[0]} ){\n# push @$arrayRef, $finResults{$inputs[0]}; \n# } \n# \n# } \n}\n\nmy @finals = sort {$finResults{$a} <=> $finResults{$b} } keys %finResults;\n\nif (scalar @finals == 1){\n return $finals[0];\n}\nmy $finalScore = $finResults{ $finals[scalar @finals - 1] };\nmy %candidateWiners;\n$candidateWiners{$finals [scalar @finals - 1]} = 1;\nfor(my $i = scalar @finals -2; $i >= 0; $i--){\n if($finResults{ $finals[$i] } < $finalScore){\n# print \"UUUUUUUUUU: $i, NNNNN: $finResults{ $finals[$i]}\\n\"; \n last;\n }\n else{\n $candidateWiners{$finals[$i] } = 1;\n } \n}\n#print Dumper \\@finals ;\n#\n#\n#\n#print Dumper \\%candidateWiners;\n#print \"HHHHHH: finalScore $finalScore \\n\";\n#print Dumper \\%finResults ;\n#print Dumper \\@hisResults;\n\n\nforeach my $hisRes (@hisResults){\n if ($hisRes->[1] >= $finalScore && exists($candidateWiners{$hisRes->[0]})){ \n print $hisRes->[0]; \n last; \n }\n}\n\n\n#foreach my $candidate (sort {$finResults{$a} cmp $finResults{$b} }\n# keys %finResults)\n#{\n# print \"$candidate $finResults{$candidate} \\n\";\n#}\n\n#print Dumper \\%hisResults;"}, {"source_code": "\nuse strict;\nuse warnings;\nmy $totalNum = ;\nmy %finResults;\nmy @hisResults;\n\nmy @inputs;\n\n#calc final score and keep historic record.\nfor(my $i = 0; $i < $totalNum; $i++){\n my $input = ;\n @inputs = split( \" \", $input);\n $finResults{$inputs[0]} += $inputs[1];\n my @record = ($inputs[0], $finResults{$inputs[0]} );\n push @hisResults, \\@record; \n}\n#Sort based on final score to find candidate winners.\nmy @finals = sort {$finResults{$a} <=> $finResults{$b} } keys %finResults;\nif (scalar @finals == 1){ #if there is only one candidate, he is the winner.\n return $finals[0];\n}\nmy $finalScore = $finResults{ $finals[scalar @finals - 1] };\nmy %candidateWiners;\n$candidateWiners{$finals [scalar @finals - 1]} = 1;\nfor(my $i = scalar @finals -2; $i >= 0; $i--){\n if($finResults{ $finals[$i] } < $finalScore){\n last;\n }\n else{\n $candidateWiners{$finals[$i] } = 1;\n } \n}\n\n#Check the historic data to find the final winner.\nforeach my $hisRes (@hisResults){\n if ($hisRes->[1] >= $finalScore && exists($candidateWiners{$hisRes->[0]})){ \n print $hisRes->[0]; \n last; \n }\n}\n\n\n"}, {"source_code": "<>;\n@_=<>;\nfor(@_){\n($a,$b)=split/ /;\n$n{$a}+=$b;\n}\n\n$max= (sort {$b<=>$a} values %n)[0];\n\nwhile(($k,$v)=each %n){\n$v==$max and $m{$k}++\n}\n\nundef %n;\n\nfor(@_){\n($a,$b)=split/ /;\n$m{$a} // next;\n$n{$a}+=$b;\nif ($max <= $n{$a}) {$ans=$a; last }\n}\n\nprint $ans"}, {"source_code": "<>;\n@_=<>;\nfor(@_){\n($a,$b)=split/ /;\n$n{$a}+=$b;\n}\n\n$max= (sort {$b<=>$a} values %n)[0];\n\nfor(@_){\n($a,$b)=split/ /;\n$m{$a}+=$b;\nif ($max == $n{$a} && $max <= $m{$a}) {$ans=$a; last }\n}\n\nprint $ans"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy $name;\nmy $point;\nmy $ans;\nmy $max = 0;\nmy %hash;\nfor (1..$n) {\n ($name, $point) = split \" \", <>;\n if (exists $hash{$name}) {\n $hash{$name} += $point;\n }\n else {\n $hash{$name} = $point;\n }\n if ($hash{$name} > $max) {\n $max = $hash{$name};\n $ans = $name;\n }\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "my $n = <>;\nmy (@name, @point);\nmy @table = [];\nmy ($ans, $max) = (0, 0);\nmy %hash;\nfor my $i (1..$n) {\n ($name[$i], $point[$i]) = split \" \", <>;\n $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});\n $hash{$name[$i]} += $point[$i];\n push @table, [$name[$i], $point[$i]];\n}\n$max = $_ > $max ? $_ : $max foreach(values %hash);\n# print \"\\n-----------\\n\";\nfor my $i (1..$n) {\n if ($hash{$name[$i]} == $max && $table[$i][1] >= $max) {\n print $name[$i] .\"\\n\";\n last;\n }\n}\n"}, {"source_code": "my $n = <>;\nmy @name;\nmy @point;\nmy $ans;\nmy $max = 0;\nmy %hash;\nfor my $i (1..$n) {\n ($name[$i], $point[$i]) = split \" \", <>;\n next if ($point[$i] < 0);\n $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});\n $hash{$name[$i]} += $point[$i];\n $max = $hash{$name[$i]} if ($hash{$name[$i]} > $max);\n}\nfor my $i (1..$n) {\n if ($point[$i] >= $max) {\n print $name[$i] .\"\\n\";\n last;\n }\n}"}, {"source_code": "my $n = <>;\nmy @name;\nmy @point;\nmy $ans;\nmy $max = 0;\nmy %hash;\nmy %table;\nfor my $i (1..$n) {\n ($name[$i], $point[$i]) = split \" \", <>;\n $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});\n $hash{$name[$i]} += $point[$i];\n}\n$max = $_ > $max ? $_ : $max foreach(values %hash);\n# print \"\\n-----------\\n\";\nfor my $i (1..$n) {\n next if ($point[$i] < 0);\n $table{$name[$i]} = 0 if(not exists $table{$name[$i]});\n $table{$name[$i]} += $point[$i];\n # print \"$hash{$name[$i]} $table{$name[$i]}\\n\";\n if ($hash{$name[$i]} == $max && $table{$name[$i]} >= $max) {\n print $name[$i] .\"\\n\";\n last;\n }\n}"}, {"source_code": "\nmy $n = <>;\nmy $name;\nmy $point;\nmy $ans;\nmy $max = 0;\nmy %hash;\nfor (1..$n) {\n ($name, $point) = split \" \", <>;\n if ($point < 0) {\n $hash{$name} = -1000;\n next;\n }\n if (exists $hash{$name} and $hash{$name} > 0) {\n $hash{$name} += $point;\n }\n else {\n $hash{$name} = $point;\n }\n if ($hash{$name} > $max) {\n $max = $hash{$name};\n $ans = $name;\n }\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "\nmy $n = <>;\nmy @name;\nmy @point;\nmy $ans;\nmy $max = 0;\nmy %hash;\nfor my $i (1..$n) {\n ($name[$i], $point[$i]) = split \" \", <>;\n next if ($point[$i] < 0);\n $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});\n $hash{$name[$i]} += $point[$i];\n $max = $hash{$name[$i]} if ($hash{$name[$i]} > $max);\n}\n\nundef %hash;\nfor my $i (1..$n) {\n next if ($point[$i] < 0);\n $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});\n $hash{$name[$i]} += $point[$i];\n if ($hash{$name[$i]} >= $max) {\n print $name[$i] .\"\\n\";\n last;\n }\n}"}, {"source_code": "use strict;\nuse warnings;\n\nmy $players = {};\nmy $score_log = [];\n\nmy $n_inputs = <>;\n\nfor ( 1 .. $n_inputs ) {\n my $line = <>;\n my ( $name, $score, @rest ) = split /\\s+/s, $line;\n\n if ( not exists $players->{$name} ) {\n $players->{$name} = 0;\n }\n my $new_score = $players->{$name} + $score;\n push @{ $score_log }, { name => $name, score => $new_score };\n $players->{ $name } = $new_score;\n}\nmy %winning_players;\nmy ( $winning_score, ) = sort { $b <=> $a } values %{ $players };\nfor ( grep { $winning_score == $players->{$_} } keys %{ $players } ) {\n $winning_players{ $_ } = 1;\n}\nfor my $entry ( @{ $score_log } ) {\n next unless exists $winning_players{ $entry->{name} };\n next unless $entry->{score} > $winning_score;\n printf \"%s\\n\", $entry->{name};\n exit 0;\n}\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $scores = {};\nmy $players = {};\n\nmy $n_inputs = <>;\n\nfor ( 1 .. $n_inputs ) {\n my $line = <>;\n my ( $name, $score, @rest ) = split /\\s+/s, $line;\n\n if ( not exists $players->{$name} ) {\n $players->{$name} = 0;\n }\n my $new_score = $players->{$name} + $score;\n $scores->{ $new_score } = $name unless exists $scores->{ $new_score };\n $players->{ $name } = $new_score;\n}\nmy ( $winning_score, ) = sort { $b <=> $a } values %{ $players };\n\nprintf \"%s\\n\", $scores->{ $winning_score };\n"}, {"source_code": "$n=;\n%hash;\n$max=0;\n$winner;\nwhile($n--)\n{\n $tmp=;\n @tmp2=split(\" \",$tmp);\n $name=$tmp2[0]; $score=$tmp2[1];\n $hash{$name}+=$score;\n # print \"# $max\\n\";\n if($max<$hash{$name}){\n # print \"2 $name\\n\";\n $max=$hash{$name};\n # print \"$max\\n\";\n $winner=$name;\n }\n}\nprint $winner;"}, {"source_code": "$n=;\n%hash;\n$max=-99999999999999;\n$winner;\nwhile($n--)\n{\n $tmp=;\n @tmp2=split(\" \",$tmp);\n $name=$tmp2[0]; $score=$tmp2[1];\n if($hash{$name})\n {\n $hash{$name}+=$score;\n }\n else\n {\n $hash{$name}=$score;\n }\n}\n#print $max,\"\\n\";\n$flag=1;\nforeach $key (%hash)\n {\n # print $hash{$key},\"\\n\";\n if($hash{$key}>$max)\n {\n $max=$hash{$key};\n $winner=$key;\n }\n }\nprint $winner;"}, {"source_code": "$n=;\n%hash;\n$max=-99999999999999;\n$winner;\nwhile($n--)\n{\n $tmp=;\n @tmp2=split(\" \",$tmp);\n $name=$tmp2[0]; $score=$tmp2[1];\n if($hash{$name})\n {\n $hash{$name}+=$score;\n }\n else\n {\n $hash{$name}=$score;\n }\n # print \"# $max\\n\";\n if($max<$hash{$name}){\n # print \"2 $name\\n\";\n $max=$hash{$name};\n # print \"$max\\n\";\n $winner=$name;\n }\n}\nprint $winner;"}, {"source_code": "$n=;\n%hash;\n@list;\n$i=0;\n$max=-99999999999999;\n$winner;\nwhile($n--)\n{\n $tmp=;\n @tmp2=split(\" \",$tmp);\n $name=$tmp2[0]; $score=$tmp2[1];\n if($hash{$name})\n {\n $hash{$name}+=$score;\n }\n else\n {\n $hash{$name}=$score;\n }\n $list[$i++]=$name;\n $list[$i++]=$hash{$name};\n}\n#print $max,\"\\n\";\n$flag=1;\nforeach $key (%hash)\n {\n # print $hash{$key},\"\\n\";\n if($hash{$key}>$max)\n {\n $max=$hash{$key};\n $winner=$key;\n }\n }\n\nfor($j=0;$j<$i;$j+=2)\n{\n # print \"$list[$j] $list[$j+1] $max\\n\";\n if($list[$j+1]==$max && $flag)\n {\n print $list[$j];\n $flag=0;\n }\n}"}, {"source_code": "$n=;\n%hash;\n$max=-99999999999999;\n$winner;\nwhile($n--)\n{\n $tmp=;\n @tmp2=split(\" \",$tmp);\n $name=$tmp2[0]; $score=$tmp2[1];\n $hash{$name}+=$score;\n # print \"# $max\\n\";\n if($max<$hash{$name}){\n # print \"2 $name\\n\";\n $max=$hash{$name};\n # print \"$max\\n\";\n $winner=$name;\n }\n}\nprint $winner;"}, {"source_code": "$n=;\n%hash;\n$max=-99999999999999;\n$winner;\nwhile($n--)\n{\n $tmp=;\n @tmp2=split(\" \",$tmp);\n $name=$tmp2[0]; $score=$tmp2[1];\n if($hash{$name})\n {\n $hash{$name}+=$score;\n }\n else\n {\n $hash{$name}=$score;\n }\n # print \"# $max\\n\";\n if($max<$hash{$name}){\n # print \"2 $name\\n\";\n $max=$hash{$name};\n # print \"$max\\n\";\n $winner=$name;\n }\n}\n$flag=1;\nforeach $key (%hash)\n {\n if($hash{$key}>=$max && $key==$winner && $flag)\n {\n print $key,\"\\n\";\n $flag=0;\n }\n }\n#print $winner;"}, {"source_code": "use strict;\nuse warnings;\nuse Data::Dumper;\nmy $totalNum = ;\nmy %finResults;\nmy @hisResults;\nmy @inputs;\nfor(my $i = 0; $i < $totalNum; $i++){\n my $input = ;\n @inputs = split( \" \", $input);\n $finResults{$inputs[0]} += $inputs[1];\n my @record = ($inputs[0], $finResults{$inputs[0]} );\n push @hisResults, \\@record;\n}\nmy @finals = sort {$a <=> $b} values %finResults;\nmy $finalScore = $finals[scalar @finals - 1];\nforeach my $hisRes (@hisResults){\n if ($hisRes->[1] >= $finalScore){\n print $hisRes->[0]; \n last; \n }\n}\n\n"}, {"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nmy $totalNum = ;\nmy %finResults;\nmy @hisResults;\n\nmy @inputs;\nfor(my $i = 0; $i < $totalNum; $i++){\n my $input = ;\n @inputs = split( \" \", $input);\n $finResults{$inputs[0]} += $inputs[1];\n my @record = ($inputs[0], $finResults{$inputs[0]} );\n push @hisResults, \\@record;\n# if (! exists $hisResults{$inputs[0]}){\n# $hisResults{$inputs[0]} = [$inputs[1]]; \n# }\n# else{\n# my $arrayRef = $hisResults{$inputs[0]};\n# if($$arrayRef[$#$arrayRef] < $finResults{$inputs[0]} ){\n# push @$arrayRef, $finResults{$inputs[0]}; \n# } \n# \n# } \n}\n\nmy @finals = sort {$a <=> $b} values %finResults;\n\n#print Dumper \\@finals ;\n\nmy $finalScore = $finals[scalar @finals - 1];\n#print \"HHHHHH: finalScore $finalScore \\n\";\n#print Dumper \\%finResults ;\n#print Dumper \\@hisResults;\n\n\nforeach my $hisRes (@hisResults){\n if ($hisRes->[1] >= $finalScore){\n print $hisRes->[0];\n last; \n }\n}\n\n\n#foreach my $candidate (sort {$finResults{$a} cmp $finResults{$b} }\n# keys %finResults)\n#{\n# print \"$candidate $finResults{$candidate} \\n\";\n#}\n\n#print Dumper \\%hisResults;"}, {"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nmy $totalNum = ;\nmy %finResults;\nmy @hisResults;\n\nmy @inputs;\nfor(my $i = 0; $i < $totalNum; $i++){\n my $input = ;\n @inputs = split( \" \", $input);\n $finResults{$inputs[0]} += $inputs[1];\n my @record = ($inputs[0], $finResults{$inputs[0]} );\n push @hisResults, \\@record;\n# if (! exists $hisResults{$inputs[0]}){\n# $hisResults{$inputs[0]} = [$inputs[1]]; \n# }\n# else{\n# my $arrayRef = $hisResults{$inputs[0]};\n# if($$arrayRef[$#$arrayRef] < $finResults{$inputs[0]} ){\n# push @$arrayRef, $finResults{$inputs[0]}; \n# } \n# \n# } \n}\n\nmy @finals = sort {$finResults{$a} <=> $finResults{$b} } keys %finResults;\n\n#print Dumper \\@finals ;\n\nmy $finalScore = $finals[scalar @finals - 1];\n#print \"HHHHHH: finalScore $finalScore \\n\";\n#print Dumper \\%finResults ;\n#print Dumper \\@hisResults;\n\n\nforeach my $hisRes (@hisResults){\n if ($hisRes->[1] >= $finResults{$finalScore} && $hisRes->[0] eq $finalScore){\n print $hisRes->[0]; \n last; \n }\n}\n\n\n#foreach my $candidate (sort {$finResults{$a} cmp $finResults{$b} }\n# keys %finResults)\n#{\n# print \"$candidate $finResults{$candidate} \\n\";\n#}\n\n#print Dumper \\%hisResults;"}, {"source_code": "<>;\n@_=<>;\nfor(@_){\n($a,$b)=split/ /;\n$n{$a}+=$b;\n}\n\n$max= (sort {$b<=>$a} values %n)[0];\n\nundef %n;\n\nfor(@_){\n($a,$b)=split/ /;\n$n{$a}+=$b;\nif ($max == $n{$a}) {$ans=$a; last }\n}\n\nprint $ans"}, {"source_code": "<>;\n@_=<>;\nfor(@_){\n($a,$b)=split/ /;\n$n{$a}+=$b;\n}\n\n$max= (sort {$b<=>$a} values %n)[0];\n\nundef %n;\n\nwhile(($k,$v)=each %n){\n$v==$max and $m{$k}++\n}\n\nfor(@_){\n($a,$b)=split/ /;\n$m{$a} // next;\n$n{$a}+=$b;\nif ($max == $n{$a}) {$ans=$a; last }\n}\n\nprint $ans"}, {"source_code": "<>;\n@_=<>;\nfor(@_){\n($a,$b)=split/ /;\n$n{$a}+=$b;\n}\n\n$max= (sort {$b<=>$a} values %n)[0];\n\nwhile(($k,$v)=each %n){\n$v==$max and $m{$k}++\n}\n\nundef %n;\n\nfor(@_){\n($a,$b)=split/ /;\n$m{$a} // next;\n$n{$a}+=$b;\nif ($max >= $n{$a}) {$ans=$a; last }\n}\n\nprint $ans"}, {"source_code": "<>;\nwhile(<>){\n($a,$b)=split/ /;\n$n{$a}+=$b;\n$n{$a}>$max and $max=$n{$a}, $ans=$a;\n}\nprint $ans"}, {"source_code": "<>;\n@_=<>;\nfor(@_){\n($a,$b)=split/ /;\n$n{$a}+=$b;\n}\n\n$max= (sort {$b<=>$a} values %n)[0];\n\nwhile(($k,$v)=each %n){\n$v==$max and $m{$k}++\n}\n\nundef %n;\n\nfor(@_){\n($a,$b)=split/ /;\n$m{$a} // next;\n$n{$a}+=$b;\nif ($max == $n{$a}) {$ans=$a; last }\n}\n\nprint $ans"}], "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\u00a0\u2014 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.\u00a0e. 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$m) = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my @m_use = ();\r\n for(my $i=0;$i<$n;$i++){\r\n my @dum = (); $#dum = $m-1;\r\n push(@m_use,\\@dum);\r\n }\r\n \r\n my @mn = ();\r\n my @n1 = ();\r\n \r\n for(my $i=0;$i<$n;$i++){\r\n my @m1 = map { $_ - 0 } split(/\\s+/,);\r\n for(my $j=0;$j<$m;$j++){\r\n push(@mn,+[$m1[$j],$i,$j]);\r\n }\r\n push(@n1,\\@m1);\r\n }\r\n \r\n my @s = sort { $a->[0] <=> $b->[0] } @mn;\r\n for(my $i=0;$i<$m;$i++){\r\n $m_use[$s[$i]->[1]]->[$s[$i]->[2]] = 1+$i;\r\n }\r\n \r\n for(my $i=0;$i<$n;$i++){\r\n my %l = ();\r\n for(my $j=0;$j<$m;$j++){\r\n if( $m_use[$i]->[$j] - 0 > 0 ){\r\n $l{$m_use[$i]->[$j]} = 1;\r\n }\r\n }\r\n my $nn = 1;\r\n my @ot = (); $#ot = $m-1;\r\n for(my $j=0;$j<$m;$j++){\r\n my $k = -1;\r\n if( $m_use[$i]->[$j] - 0 > 0 ){\r\n $k = $m_use[$i]->[$j] - 0;\r\n } else {\r\n while( $l{$nn} - 0 > 0 ){ $nn++; }\r\n $k = $nn;\r\n $l{$nn} = 1;\r\n #print \"hoge? $k $nn\\n\";\r\n }\r\n $ot[$k-1] = $n1[$i]->[$j];\r\n }\r\n print ( join(' ',@ot) . \"\\n\");\r\n }\r\n \r\n}\r\n\r\nexit(0);\r\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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$)\u00a0\u2014 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\u00a0\u2014 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": "<>;\r\n\r\nprint -96 + ord +( sort split '', <> )[ -1 ], $/ while <>"}, {"source_code": "<>;\r\n\r\nwhile(<>){\r\n\t$_ = <>;\r\n\t\r\n\t$A = a;\r\n\t\r\n\tfor $i ( 1 .. 26 ){\r\n\t\t/$A/ and $B = $i;\r\n\t\t$A ++;\r\n\t\t}\r\n\t\r\n\tprint $B . $/\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\tmy( $L ) = reverse sort split '';\n\t\n\tmy $A = 'a';\n\t\n\tmy $ans;\n\t\n\tfor my $i ( 1 .. 26 ){\n\t\tif( $A eq $L ){\n\t\t\t$ans = $i;\n\t\t\tlast;\n\t\t\t}\n\t\t$A ++;\n\t\t}\n\t\n\tprint $ans;\n\t}"}], "negative_code": [], "src_uid": "4841cbc3d3ef29929690b78e30fbf22e"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100). 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": "\t<>, map $h{$_} ++, <>;\n\tprint ((sort {$b <=> $a} values %h) [0] )"}], "negative_code": [], "src_uid": "f48ddaf1e1db5073d74a2aa318b46704"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of elements in the array. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20091000) \u2014 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": "#!/usr/bin/perl\n# Jaroslav\n\nwhile(<>){\n for(1..(@_=@a=split/ /,<>)){$m[pop@_]++}\n for(1..@m){2*(pop@m)-1>@a and $i++}\n print $i?\"NO\":\"YES\",\"\\n\";\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n# Jaroslav\n\nwhile(<>){\n for(1..(@_=@a=split/ /,<>)){$m[pop@_]++}\n for(@m){2*(pop@m)-1>@a and $i++}\n print $i?\"NO\":\"YES\",\"\\n\";\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$$$)\u00a0\u2014 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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B, $n ) = split;\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\tmy @C;\n\t\n\twhile( @A ){\n\t\tpush @C, [ ( shift @A ), ( shift @B ) ];\n\t\t}\n\t\n\t@C = sort { $a->[ 0 ] <=> $b->[ 0 ] } @C;\n\t\n\tfor( @C ){\n\t\tmy $k = ( int $_->[ 1 ] / $A ) + !!( $_->[ 1 ] % $A );\n\t\t$B -= $k * $_->[ 0 ];\n\t\t}\n\t\n\t$B += $C[ @C - 1 ]->[ 0 ];\n\t\n\tprint $B > 0 ? 'YES' : 'NO';\n\t}"}], "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$$$ \u2014 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$$$)\u00a0\u2014 the number of favourite integers of Bob. The second line contains $$$t$$$ space-separated integers $$$x_i$$$ ($$$1 \\leq x_i \\leq 10^{18}$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse bigint;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tfor( @_ ){\n\t\tmy $d = int $_ / 14;\n\t\t\n\t\t$_ -= $d * 14;\n\t\t\n\t\tprint $_ <= 6 && $_ >= 1 && $d ? \"YES\" : \"NO\";\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse bigint;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tfor( @_ ){\n\t\tmy $d = int $_ / 14;\n\t\t\n\t\t$_ -= $d * 14;\n\t\t\n\t\tprint $_ <= 6 || $d == 0 ? \"YES\" : \"NO\";\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse bigint;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tfor( @_ ){\n\t\tmy $d = int $_ / 14;\n\t\t\n\t\t$_ -= $d * 14;\n\t\t\n\t\tprint $_ > 6 ? \"NO\" : \"YES\";\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse bigint;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tfor( @_ ){\n\t\tmy $d = int $_ / 14;\n\t\t\n\t\t$_ -= $d * 14;\n\t\t\n\t\tprint $_ <= 6 && $d ? \"YES\" : \"NO\";\n\t\t}\n\t}"}], "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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $a_to_z = join '', 'a' .. 'z';\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\tfor( @_ ){\n\t\tmy $re = join '', sort split //;\n\t\tprint $a_to_z =~ /$re/ ? \"Yes\" : \"No\";\n\t\t}\n\t\n\t}"}], "negative_code": [], "src_uid": "02a94c136d3fb198025242e91264823b"} {"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$$$) \u00a0\u2014 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}$$$)\u00a0\u2014 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": "use 5.020;\nuse warnings;\nuse utf8;\n\n#\u4e0b\u9762\u5217\u4e3e\u51fa\u51e0\u4e2a\u7279\u522b\u96be\u641e\u7684\u4e1c\u897f\uff0c\u8fd9\u91cc\u51fa\u9519\u7684\u51e0\u7387\u5f88\u5927\n# 6 6 3 -1 3 4 3 3\n# 5 3 2 5 4 -1 10 9\n# 1 -1 2 -1 3 -1 4 -1 5 -1 6 -1 7 -1 8 -1 24\n# 1 2 3 4 5 6 7 8 -1 24\nsub readNext {\n my $line = ;\n chomp($line);\n return $line;\n}\n\nmy $count = int( &readNext() );\nmy $index = 0;\n\nREAD: while ($count) {\n\n $index++;\n\n &readNext();\n my @array = split /\\s+/, &readNext();\n\n # if ( $index == 369 ) {\n # say join( \" \", @array );\n # }\n\n my @adjacentArray = ();\n my @presentArray = ();\n\n my $lastNumber;\n my $lastIndex = -1;\n my $lastPushedIndex = -1;\n my $boolLastIsAbsent = 0;\n my $mForArray = 0;\n\n for my $i ( 0 .. $#array ) {\n\n my $item = int( $array[$i] );\n\n if ( $item == -1 ) {\n\n if ( defined($lastNumber)\n && $lastNumber != -1\n && !$boolLastIsAbsent\n && $lastPushedIndex != $lastIndex )\n {\n push( @adjacentArray, $lastNumber );\n $lastPushedIndex = $lastIndex;\n\n }\n\n $boolLastIsAbsent = 1;\n }\n else {\n if ( $boolLastIsAbsent && $lastPushedIndex != $i ) {\n push( @adjacentArray, $item );\n $lastPushedIndex = $i;\n }\n $boolLastIsAbsent = 0;\n\n if ( defined($lastNumber) && $lastNumber != -1 ) {\n my $gap = abs( $lastNumber - $item );\n if ( $gap > $mForArray ) {\n $mForArray = $gap;\n }\n }\n }\n\n $lastNumber = $item;\n $lastIndex = $i;\n }\n\n if (@adjacentArray) {\n\n # say join( \",\", @adjacentArray );\n\n @adjacentArray = sort { $a <=> $b } @adjacentArray;\n\n my $k =\n int(\n ( $adjacentArray[0] + $adjacentArray[$#adjacentArray] ) / 2 + 0.5 );\n\n # say \"the k is $k\";\n\n my $mforK = (\n (\n ( $k - $adjacentArray[0] ) >\n ( $adjacentArray[$#adjacentArray] - $k )\n )\n ? ( $k - $adjacentArray[0] )\n : ( $adjacentArray[$#adjacentArray] - $k )\n );\n\n # say \"mfork $mforK mForArray $mForArray\";\n\n my $m = $mforK > $mForArray ? $mforK : $mForArray;\n\n say $m. \" \" . $k;\n }\n else {\n say \"0 0\";\n }\n\n $count--;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t$_ = <>, chomp;\n\t\n\t$debug and print \"[$_]\";\n\t\n\t0 while s/-1\\K( -1)+//g;\n\t\n\t$debug and print \"[$_]\";\n\t\n\t@_ = split;\n\t\n\tif( ! grep { $_ != -1 } @_ ){\n\t\tprint \"0 42\";\n\t\tnext;\n\t\t}\n\t\n\tmy @A;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tnext if $_[ $i ] != -1;\n\t\t\n\t\tpush @A, $_[ $i - 1 ] if $i - 1 >= 0;\n\t\tpush @A, $_[ $i + 1 ] if $i + 1 <= @_ - 1;\n\t\t}\n\t\n\t$debug and print \"A:[@A]\";\n\t\n\tmy $undef;\n\t\n\tif( @A ){\n\t\tmy( $min, $max ) = ( sort { $a <=> $b } @A )[ 0, -1 ];\n\t\tmy $diff = $max - $min;\n\t\t\n\t\t$undef = $min + ( $diff >> 1 );\n\t\t}\n\t\n\tif( not defined $undef ){\n\t\t( $undef ) = grep { $_ != -1 } @_;\n\t\t}\n\t\n\t$_ == -1 and $_ = $undef for @_;\n\t\n\t$debug and print \"_:[@_]\";\n\t\n\tmy @B;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tpush @B, abs( $_[ $i ] - $_[ $i + 1 ] );\n\t\t}\n\t\n\tmy $max_diff = ( sort { $b <=> $a } @B )[ 0 ] // 0;\n\t\n\tprint \"$max_diff $undef\";\n\t}"}], "negative_code": [{"source_code": "use 5.020;\nuse warnings;\nuse utf8;\n\n# 6 6 3 -1 3 4 3 3 \u8fd9\u4e2a\u60c5\u51b5\u5f88\u96be\uff0c\u786e\u5b9a\u76f8\u90bb\u7684\u95ee\u9898\nsub readNext {\n my $line = ;\n chomp($line);\n return $line;\n}\n\nmy $count = int( &readNext() );\nmy $index = 0;\nREAD: while ($count) {\n\n $index++;\n\n &readNext();\n my @array = split / /, &readNext();\n\n if ( $index == 369 ) {\n say join( \"-\", @array );\n }\n\n my $min;\n my $max;\n\n my $minIndex;\n my $maxIndex;\n\n my $lastNumber;\n my $lastGap;\n my $lastIndex;\n\n for my $i ( 0 .. $#array ) {\n\n my $item = int( $array[$i] );\n\n if ( $item == -1 ) {\n next;\n }\n\n if ( defined($lastIndex) ) {\n if ( ( $i - $lastIndex ) == 1 ) {\n my $gap = abs( $item - $lastNumber );\n if ( defined($lastGap) ) {\n $lastGap = $gap > $lastGap ? $gap : $lastGap;\n }\n else {\n $lastGap = $gap;\n }\n }\n }\n\n if ( defined($min) ) {\n\n if ( $item < $min ) {\n $min = $item;\n $minIndex = $i;\n }\n\n if ( $item > $max ) {\n $max = $item;\n $maxIndex = $i;\n }\n }\n else {\n $min = $item;\n $max = $item;\n\n $minIndex = $i;\n $maxIndex = $i;\n }\n\n $lastIndex = $i;\n $lastNumber = $item;\n }\n\n # say(\"max gap :$lastGap, min:$minIndex, max:$maxIndex\");\n\n if ( defined($min) ) {\n\n if ( $minIndex == $maxIndex ) {\n say \"0 $min\";\n }\n\n elsif ( abs( $maxIndex - $minIndex ) == 1 ) {\n say( $lastGap . \" \" . $min );\n }\n else {\n my $k = int( ( $max + $min ) / 2 + 0.999 );\n my $m =\n ( ( $max - $k ) > ( $k - $min ) )\n ? ( $max - $k )\n : ( $k - $min );\n if ( defined($lastGap) ) {\n say( ( ( $lastGap > $m ) ? $lastGap : $m ) . \" \" . $k );\n }\n else {\n say $m. \" \" . $k;\n }\n }\n }\n else {\n say \"0 0\";\n }\n\n $count--;\n}\n"}, {"source_code": "use 5.020;\nuse warnings;\nuse utf8;\n\n# 6 6 3 -1 3 4 3 3 \u8fd9\u4e2a\u60c5\u51b5\u5f88\u96be\uff0c\u786e\u5b9a\u76f8\u90bb\u7684\u95ee\u9898\nsub readNext {\n my $line = ;\n chomp($line);\n return $line;\n}\n\nmy $count = int( &readNext() );\n\nREAD: while ($count) {\n &readNext();\n my @array = split / /, &readNext();\n\n my $min;\n my $max;\n\n my $minIndex;\n my $maxIndex;\n\n my $lastNumber;\n my $lastGap;\n my $lastIndex;\n for my $i ( 0 .. $#array ) {\n\n my $item = int( $array[$i] );\n\n if ( $item == -1 ) {\n next;\n }\n\n if ( defined($lastIndex) ) {\n if ( ( $i - $lastIndex ) == 1 ) {\n my $gap = abs( $item - $lastNumber );\n if ( defined($lastGap) ) {\n $lastGap = $gap > $lastGap ? $gap : $lastGap;\n }\n else {\n $lastGap = $gap;\n }\n }\n }\n\n if ( defined($min) ) {\n\n if ( $item < $min ) {\n $min = $item;\n $minIndex = $i;\n }\n\n if ( $item > $max ) {\n $max = $item;\n $maxIndex = $i;\n }\n }\n else {\n $min = $item;\n $max = $item;\n\n $minIndex = $i;\n $maxIndex = $i;\n }\n\n $lastIndex = $i;\n $lastNumber = $item;\n }\n\n # say(\"max gap :$lastGap, min:$minIndex, max:$maxIndex\");\n\n if ( defined($min) ) {\n\n if ( $minIndex == $maxIndex ) {\n say \"0 $min\";\n }\n\n elsif ( abs( $maxIndex - $minIndex ) == 1 ) {\n say( $lastGap . \" \" . $min );\n }\n\n else {\n my $k = int( ( $max + $min ) / 2 + 0.9 );\n\n if ( defined($lastGap) ) {\n say $lastGap. \" \" . $k;\n }\n else {\n my $m =\n ( ( $max - $k ) > ( $k - $min ) )\n ? ( $max - $k )\n : ( $k - $min );\n say $m. \" \" . $k;\n }\n }\n }\n else {\n say \"0 0\";\n }\n\n $count--;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t$_ = <>, chomp;\n\t\n\t$debug and print \"[$_]\";\n\t\n\t0 while s/-1\\K( -1)+//g;\n\ts/^-1 //;\n\ts/ -1$//;\n\t\n\t$debug and print \"[$_]\";\n\t\n\t@_ = split;\n\t\n\tif( ! grep { $_ != -1 } @_ ){\n\t\tprint \"0 42\";\n\t\tnext;\n\t\t}\n\t\n\tmy @A;\n\t\n\tfor my $i ( 1 .. @_ - 2 ){\n\t\tnext if $_[ $i ] != -1;\n\t\t\n\t\tpush @A, $_[ $i - 1 ], $_[ $i + 1 ];\n\t\t}\n\t\n\t$debug and print \"A:[@A]\";\n\t\n\tmy $undef;\n\t\n\tif( @A ){\n\t\tmy( $min, $max ) = ( sort { $a <=> $b } @A )[ 0, -1 ];\n\t\tmy $diff = $max - $min;\n\t\t\n\t\t$undef = $min + ( $diff >> 1 );\n\t\t}\n\t\n\tif( not defined $undef ){\n\t\t( $undef ) = grep { $_ != -1 } @_;\n\t\t}\n\t\n\t$_ == -1 and $_ = $undef for @_;\n\t\n\t$debug and print \"_:[@_]\";\n\t\n\tmy @B;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tpush @B, abs( $_[ $i ] - $_[ $i + 1 ] );\n\t\t}\n\t\n\tmy $max_diff = ( sort { $b <=> $a } @B )[ 0 ] // 0;\n\t\n\tprint \"$max_diff $undef\";\n\t}"}, {"source_code": "use 5.020;\nuse warnings;\nuse utf8;\n\nsub readNext {\n my $line = ;\n chomp($line);\n return $line;\n}\n\nmy $count = int( &readNext() );\n\nREAD: while ($count) {\n &readNext();\n my @array = split / /, &readNext();\n\n my $min;\n my $max;\n\n my $minIndex;\n my $maxIndex;\n\n for my $i ( 0 .. $#array ) {\n\n my $item = int( $array[$i] );\n\n if ( $item == -1 ) {\n next;\n }\n\n if ( defined($min) ) {\n\n if ( $item < $min ) {\n $min = $item;\n $minIndex = $i;\n }\n\n if ( $item > $max ) {\n $max = $item;\n $maxIndex = $i;\n }\n }\n else {\n $min = $item;\n $max = $item;\n\n $minIndex = $i;\n $maxIndex = $i;\n }\n }\n\n if ( defined($min) ) {\n if ( $minIndex == $maxIndex ) {\n say \"0 $min\";\n }\n elsif ( abs( $maxIndex - $minIndex ) == 1 ) {\n say( ( $max - $min ) . \" \" . $min );\n }\n else {\n my $k = int( ( $max + $min ) / 2 + 0.9 );\n my $m =\n ( ( $max - $k ) > ( $k - $min ) ) ? ( $max - $k ) : ( $k - $min );\n say $m. \" \" . $k;\n }\n }\n else {\n say \"0 0\";\n }\n\n $count--;\n}\n"}, {"source_code": "use 5.020;\nuse warnings;\nuse utf8;\n\n# 6 6 3 -1 3 4 3 3 \u8fd9\u4e2a\u60c5\u51b5\u5f88\u96be\uff0c\u786e\u5b9a\u76f8\u90bb\u7684\u95ee\u9898\nsub readNext {\n my $line = ;\n chomp($line);\n return $line;\n}\n\nmy $count = int( &readNext() );\n\nREAD: while ($count) {\n &readNext();\n my @array = split / /, &readNext();\n\n my $min;\n my $max;\n\n my $minIndex;\n my $maxIndex;\n\n my $lastNumber;\n my $lastGap;\n my $lastIndex;\n for my $i ( 0 .. $#array ) {\n\n my $item = int( $array[$i] );\n\n if ( $item == -1 ) {\n next;\n }\n\n if ( defined($lastIndex) ) {\n if ( ( $i - $lastIndex ) == 1 ) {\n my $gap = abs( $item - $lastNumber );\n if ( defined($lastGap) ) {\n $lastGap = $gap > $lastGap ? $gap : $lastGap;\n }\n else {\n $lastGap = $gap;\n }\n }\n }\n\n if ( defined($min) ) {\n\n if ( $item < $min ) {\n $min = $item;\n $minIndex = $i;\n }\n\n if ( $item > $max ) {\n $max = $item;\n $maxIndex = $i;\n }\n }\n else {\n $min = $item;\n $max = $item;\n\n $minIndex = $i;\n $maxIndex = $i;\n }\n\n $lastIndex = $i;\n $lastNumber = $item;\n }\n\n # say(\"max gap :$lastGap, min:$minIndex, max:$maxIndex\");\n\n if ( defined($min) ) {\n\n if ( $minIndex == $maxIndex ) {\n say \"0 $min\";\n }\n\n elsif ( abs( $maxIndex - $minIndex ) == 1 ) {\n say( $lastGap . \" \" . $min );\n }\n else {\n my $k = int( ( $max + $min ) / 2 + 0.9 );\n my $m =\n ( ( $max - $k ) > ( $k - $min ) )\n ? ( $max - $k )\n : ( $k - $min );\n if ( defined($lastGap) ) {\n say( ( ( $lastGap > $m ) ? $lastGap : $m ) . \" \" . $k );\n }\n else {\n say $m. \" \" . $k;\n }\n }\n }\n else {\n say \"0 0\";\n }\n\n $count--;\n}\n"}], "src_uid": "8ffd80167fc4396788b745b53068c9d3"} {"nl": {"description": "This is a harder version of the problem. In this version, $$$n \\le 50\\,000$$$.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 50\\,000$$$; $$$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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { [ ++ $i, split ' ', <> ] } 1 .. $_;\n\t\n\tmy @pairs;\n\t\n\t####### BEGIN: x+y, x+z, y+z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\t$debug and print 'init: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t####### END: x+y, x+z, y+z\n\t\n\t####### BEGIN: x, y, z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\t$debug and print 'init: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t####### END: x, y, z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\twhile( @_ ){\n\t\tpush @pairs, join ' ', map $_->[ 0 ], shift @_, shift @_;\n\t\t}\n\t\n\tprint for @pairs;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { [ ++ $i, split ' ', <> ] } 1 .. $_;\n\t\n\tmy @pairs;\n\t\n\t####### BEGIN: x+y, x+z, y+z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\t$debug and print 'init: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t####### END: x+y, x+z, y+z\n\t\n\t####### BEGIN: x, y, z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\t$debug and print 'init: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t####### END: x, y, z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( 1 ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n#\twhile( @_ ){\n#\t\tpush @pairs, join ' ', map $_->[ 0 ], shift @_, shift @_;\n#\t\t}\n\t\n\tprint for @pairs;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy @pairs;\n\nwhile(<>){\t\n\tmy $i = 0;\n\t\n\t@_ = map { [ ++ $i, split ' ', <> ] } 1 .. $_;\n\t\n\t@pairs = ();\n\t\n\tmy @V = 1 .. 3;\n\t\n\tfor( 1 .. 3 ){\n\t\t_grep( \\@_, @V[ 0, 1 ], @V );\n\t\t_grep( \\@_, @V[ 0, 0 ], @V );\n\t\tpush @V, shift @V;\n\t\t}\n\t\n\twhile( @_ ){\n\t\tpush @pairs, join ' ', map $_->[ 0 ], splice @_, 0, 2;\n\t\t}\n\t\n\tprint for @pairs;\n\t}\n\nsub _grep {\n\tmy( $A, $eq1, $eq2, @S ) = @_;\n\t\n\t@{ $A } = sort {\n\t\t$a->[ $S[ 0 ] ] <=> $b->[ $S[ 0 ] ] ||\n\t\t$a->[ $S[ 1 ] ] <=> $b->[ $S[ 1 ] ] ||\n\t\t$a->[ $S[ 2 ] ] <=> $b->[ $S[ 2 ] ]\n\t\t} @{ $A };\n\t\n\tfor( my $i = 0; $i < @{ $A } - 1; $i ++ ){\n\t\tif( $A->[ $i ][ $eq1 ] == $A->[ $i + 1 ][ $eq1 ] and $A->[ $i ][ $eq2 ] == $A->[ $i + 1 ][ $eq2 ] ){\n\t\t\tpush @pairs, join ' ', $A->[ $i ][ 0 ], $A->[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $A->[ $i ][ 0 ], $A->[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t@{ $A } = grep { $_->[ 0 ] } @{ $A };\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { [ ++ $i, split ' ', <> ] } 1 .. $_;\n\t\n\tmy @pairs;\n\t\n\t####### BEGIN: x+y, x+z, y+z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\t$debug and print 'init: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] and $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t####### END: x+y, x+z, y+z\n\t\n\t####### BEGIN: x, y, z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\t$debug and print 'init: ', join ' ', map $_->[ 0 ], @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 1 ] == $_[ $i + 1 ][ 1 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 2 ] == $_[ $i + 1 ][ 2 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t@_ = sort {\n\t\t$a->[ 3 ] <=> $b->[ 3 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 1 ] <=> $b->[ 1 ] \n\t\t} @_;\n\t\n\tfor( my $i = 0; $i < @_ - 1; $i ++ ){\n\t\tif( $_[ $i ][ 3 ] == $_[ $i + 1 ][ 3 ] ){\n\t\t\tpush @pairs, join ' ', $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $_[ $i ][ 0 ], $_[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@_ = grep { $_->[ 0 ] } @_;\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @_;\n\t\n\t####### END: x, y, z\n\t\n\t@_ = sort {\n\t\t$a->[ 1 ] <=> $b->[ 1 ] ||\n\t\t$a->[ 2 ] <=> $b->[ 2 ] ||\n\t\t$a->[ 3 ] <=> $b->[ 3 ] \n\t\t} @_;\n\t\n\tprint for @pairs;\n\t\n\twhile( @_ ){\n\t\tprint join ' ', map $_->[ 0 ], shift @_, shift @_;\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nmy @pairs;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { [ ++ $i, split ' ', <> ] } 1 .. $_;\n\t\n\t@pairs = ();\n\t\n#\t_grep( \\@_, 1, 2, 1, 2, 3 );\n#\t_grep( \\@_, 2, 3, 2, 3, 1 );\n#\t_grep( \\@_, 3, 1, 3, 1, 2 );\n\t\n#\t_grep( \\@_, 1, 1, 1, 2, 3 );\n#\t_grep( \\@_, 2, 2, 2, 3, 1 );\n#\t_grep( \\@_, 3, 3, 3, 1, 2 );\n\t\n\t_grep( \\@_, 1, 2, 1, 2, 3 );\n\t_grep( \\@_, 1, 1, 1, 2, 3 );\n\t\n\t_grep( \\@_, 2, 3, 2, 3, 1 );\n\t_grep( \\@_, 2, 2, 2, 3, 1 );\n\t\n\t_grep( \\@_, 3, 1, 3, 1, 2 );\n\t_grep( \\@_, 3, 3, 3, 1, 2 );\n\t\n\twhile( @_ ){\n\t\tpush @pairs, join ' ', map $_->[ 0 ], shift @_, shift @_;\n\t\t}\n\t\n\tprint for @pairs;\n\t}\n\nsub _grep {\n\tmy( $ref_arr, $eq1, $eq2, @sort_seq ) = @_;\n\t\n\t@{ $ref_arr } = sort {\n\t\t$a->[ $sort_seq[ 0 ] ] <=> $b->[ $sort_seq[ 0 ] ] ||\n\t\t$a->[ $sort_seq[ 1 ] ] <=> $b->[ $sort_seq[ 1 ] ] ||\n\t\t$a->[ $sort_seq[ 2 ] ] <=> $b->[ $sort_seq[ 2 ] ]\n\t\t} @{ $ref_arr };\n\t\n\tfor( my $i = 0; $i < @{ $ref_arr } - 1; $i ++ ){\n\t\tif( $ref_arr->[ $i ][ $eq1 ] == $ref_arr->[ $i + 1 ][ $eq1 ] and $ref_arr->[ $i ][ $eq2 ] == $ref_arr->[ $i + 1 ][ $eq2 ] ){\n\t\t\tpush @pairs, join ' ', $ref_arr->[ $i ][ 0 ], $ref_arr->[ $i + 1 ][ 0 ];\n\t\t\t$_ = 0 for $ref_arr->[ $i ][ 0 ], $ref_arr->[ $i + 1 ][ 0 ];\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@pairs: [@pairs]\";\n\t\n\t@{ $ref_arr } = grep { $_->[ 0 ] } @{ $ref_arr };\n\t\n\t$debug and print 'left: ', join ' ', map $_->[ 0 ], @{ $ref_arr };\n\t}"}], "negative_code": [], "src_uid": "3d92a54be5c544b313f1e87f7b9cc5c8"} {"nl": {"description": "Little Tanya decided to present her dad a postcard on his Birthday. She has already created a message \u2014 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\u2009\u2264\u2009|s|\u2009\u2264\u20092\u00b7105), consisting of uppercase and lowercase English letters \u2014 the text of Tanya's message. The second line contains line t (|s|\u2009\u2264\u2009|t|\u2009\u2264\u20092\u00b7105), consisting of uppercase and lowercase English letters \u2014 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": "$s=<>;\n$t=<>;\nchomp($s,$t);\n@s=split '',$s;\nmap {$h{$_}++} split '',$t;\n$y=$w=0; \nfor(0..@s) {\n if($h{$s[$_]}) {\n --$h{$s[$_]};\n ++$y;\n delete $s[$_];\n }\n}\nfor(@s) {\n $_=$_ eq uc$_?lc:uc;\n if($h{$_}) {\n --$h{$_};\n ++$w;\n }\n}\nprint \"$y $w\\n\";"}, {"source_code": "$s=<>; \n$t=<>;\nchomp($s,$t);\n@s=split '',$s;\nmap {$h{$_}++} split '',$t;\n$y=$w=0;\nfor(@s) {\n --$h{$_},++$y,$_='*' if $h{$_};\n}\nfor(@s) {\n $_=$_ eq uc$_?lc:uc;\n --$h{$_},++$w if $h{$_};\n}\nprint \"$y $w\\n\";"}, {"source_code": "chomp(@_ = <>);\n@a = split//, $_[0];\n$h{$_}++ for split//, pop @_;\n$h{$_} && ($h{$_}--, $c++, $_ = _) for @a;\n( ($h{(lc)} && $h{(lc)}--) || ($h{(uc)} && $h{(uc)}--) ) && $d++ for @a;\nprint 0+$c, \" \", 0+$d\n"}, {"source_code": "chomp(@_ = <>);\n@a = split//, $_[0];\n$h{$_}++ for split//, pop @_;\n$h{$_} && ($h{$_}--, $c++, $_ = _) for @a;\n( ($h{(lc)} && $h{(lc)}--) || ($h{(uc)} && $h{(uc)}--) ) && $d++ for @a;\nprint 0+$c, \" \", 0+$d\n"}, {"source_code": "chomp(@_ = <>);\n@a = split//, $_[0];\n$h{$_}++ for split//, pop @_;\n$h{$_} && ($h{$_}--, $c++, $_ = _) for @a;\n( ($h{(lc)} && $h{(lc)}--) || ($h{(uc)} && $h{(uc)}--) ) && $d++ for @a;\nprint 0+$c, \" \", 0+$d"}, {"source_code": "chomp(@_ = <>);\n@a = split//, $_[0];\n$h{$_}++ for split//, pop @_;\n$h{$_} && ($h{$_}--, $c++, $_ = _) for @a;\n( ($h{(lc)} && $h{(lc)}--) || ($h{(uc)} && $h{(uc)}--) ) && $d++ for @a;\nprint 0+$c, \" \", 0+$d\n"}, {"source_code": "chomp(@_ = <>);\n@a = split//, $_[0];\n$h{$_}++ for split//, pop @_;\n$h{$_} && ($h{$_}--, $c++, $_ = _) for @a;\n( ($h{(lc)} && $h{(lc)}--) || ($h{(uc)} && $h{(uc)}--) ) && $d++ for @a;\nprint 0+$c, \" \", 0+$d\n"}, {"source_code": "chomp (my @s = split /\\s*/, );\nchomp (my @t = split /\\s*/, );\n\nmy (%tCnt);\n$tCnt{$_} = 0 foreach ('a' .. 'z', 'A' .. 'Z');\n$tCnt{$_}++ foreach (@t);\n\nmy ($yCnt, $wCnt) = (0, 0);\nforeach (0 .. $#s) {\n $x = $s[$_];\n if ($tCnt{$x}) {\n $yCnt++;\n $tCnt{$x}--;\n delete $s[$_];\n }\n}\nforeach (0 .. $#s) {\n $x = $s[$_];\n $x = (uc($x) eq $x) ? lc($x) : uc($x);\n if ($tCnt{$x}) {\n $wCnt++;\n $tCnt{$x}--;\n delete $s[$_];\n }\n}\n\nprintf \"$yCnt $wCnt\\n\";"}, {"source_code": "chomp(@_ = <>);\n@a = split//, $_[0];\n$h{$_}++ for split//, pop @_;\n$h{$_} && ($h{$_}--, $c++, $_ = _) for @a;\n( ($h{(lc)} && $h{(lc)}--) || ($h{(uc)} && $h{(uc)}--) ) && $d++ for @a;\nprint 0+$c, \" \", 0+$d\n"}, {"source_code": "chomp(@_ = <>);\n@a = split//, $_[0];\n$h{$_}++ for split//, pop @_;\n$h{$_} && ($h{$_}--, $c++, $_ = _) for @a;\n( ($h{(lc)} && $h{(lc)}--) || ($h{(uc)} && $h{(uc)}--) ) && $d++ for @a;\nprint 0+$c, \" \", 0+$d\n"}, {"source_code": "chomp(@_ = <>);\n@a = split//, $_[0];\n$h{$_}++ for split//, pop @_;\n$h{$_} && ($h{$_}--, $c++, $_ = _) for @a;\n( ($h{(lc)} && $h{(lc)}--) || ($h{(uc)} && $h{(uc)}--) ) && $d++ for @a;\nprint 0+$c, \" \", 0+$d\n"}, {"source_code": "while(<>){\n\tchomp;\n\t@a = split//;\n\t$_ = <>, chomp;\n\t@b = split//;\n\tundef %h;\n\tfor (@b){\n\t\t$h{$_}++;\n\t\t}\n\t$c = $d = 0;\n\tfor (@a){\n\t\tif ($h{$_} ){\n\t\t\t$h{$_}--;\n\t\t\t$c++;\n\t\t\t$_ = aa;\n\t\t\t}\n\t\t}\n\tfor (@a){\n\t\tif ($h{(lc)} + $h{(uc)})\n\t\t{ $d++ ; $h{(lc)} and $h{(lc)}--; $h{(uc)} and $h{(uc)}--}\n\t\t}\n#\t$d -= $c;\n#\t$d < 0 and $d = 0;\n\tprint \"$c $d\\n\";\n\t\n\t}"}, {"source_code": "chomp(@_ = <>);\n@a = split//, $_[0];\n$h{$_}++ for split//, pop @_;\n$h{$_} && ($h{$_}--, $c++, $_ = _) for @a;\n( ($h{(lc)} && $h{(lc)}--) || ($h{(uc)} && $h{(uc)}--) ) && $d++ for @a;\nprint 0+$c, \" \", 0+$d"}, {"source_code": "chomp(@_ = <>);\n@a = split//, $_[0];\n$h{$_}++ for split//, pop @_;\n$h{$_} && ($h{$_}--, $c++, $_ = _) for @a;\n( ($h{(lc)} && $h{(lc)}--) || ($h{(uc)} && $h{(uc)}--) ) && $d++ for @a;\nprint 0+$c, \" \", 0+$d\n"}], "negative_code": [{"source_code": "chomp (my @s = split /\\s*/, );\nchomp (my @t = split /\\s*/, );\n\nmy (%tCnt);\n$tCnt{$_} = 0 foreach ('a' .. 'z', 'A' .. 'Z');\n$tCnt{$_}++ foreach (@t);\n\nmy ($yCnt, $wCnt) = (0, 0);\nforeach (@s) {\n if ($tCnt{$_}) {\n $yCnt++;\n $tCnt{$_}--;\n } else {\n $_ = (uc($_) eq $_) ? lc($_) : uc($_);\n \n if ($tCnt{$_}) {\n $wCnt++;\n $tCnt{$_}--;\n }\n }\n}\n\nprintf \"$yCnt $wCnt\\n\";"}, {"source_code": "chomp (my @s = split /\\s*/, );\nchomp (my @t = split /\\s*/, );\n\nsub incs {\n foreach (reverse (0 .. $#s)) {\n $s[$_] = chr (1 + ord ($s[$_]));\n if ($s[$_] eq '{') {\n $s[$_] = 'a';\n } else {\n return;\n }\n }\n}\n\n&incs;\n\nmy $sum = 0;\nforeach (0 .. $#s) {\n $sum += (ord ($t[$_])) - (ord ($s[$_]));\n last if ($sum > 0);\n}\n\nif ($sum > 0) {\n printf \"%s\\n\", join \"\", @s;\n} else {\n printf \"No such string\\n\";\n}"}, {"source_code": "chomp (my @s = split /\\s*/, );\nchomp (my @t = split /\\s*/, );\n\nmy (%tCnt);\n$tCnt{$_} = 0 foreach ('a' .. 'z', 'A' .. 'Z');\n$tCnt{$_}++ foreach (@t);\n\nmy ($yCnt, $wCnt) = (0, 0);\nforeach (@s) {\n if ($tCnt{$_}) {\n $yCnt++;\n $tCnt{$_}--;\n } else {\n $_ = (uc($_) eq $_) ? lc($_) : uc($_);\n \n if ($tCnt{$_}) {\n $wCnt++;\n $tCnt{$_}--;\n }\n }\n}\n\nprintf \"$yCnt $wCnt\\n\";\n"}, {"source_code": "while(<>){\n\tchomp;\n\t@a = split//;\n\t$_ = <>, chomp;\n\t@b = split//;\n\tundef %h;\n\tfor (@b){\n\t\t$h{$_}++;\n\t\t}\n\t$c = $d = 0;\n\tfor (@a){\n\t\tif ($h{$_} ){\n\t\t\t$h{$_}--;\n\t\t\t$c++\n\t\t\t}\n\t\t}\n\tfor (@a){\n\t\tif ($h{(lc)} + $h{(uc)}){ $d++ }\n\t\t}\n\t$d -= $c;\n\t$d < 0 and $d = 0;\n\tprint \"$c $d\\n\";\n\t\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t@a = split//;\n\t$_ = <>, chomp;\n\t@b = split//;\n\tundef %h;\n\tfor (@b){\n\t\t$h{$_}++;\n\t\t}\n\t$c = $d = 0;\n\tfor (@a){\n\t\tif ($h{$_} ){\n\t\t\t$h{$_}--;\n\t\t\t$c++;\n\t\t\t$_ = aa;\n\t\t\t}\n\t\t}\n\tfor (@a){\n\t\tif ($h{(lc)} + $h{(uc)}){ $d++ }\n\t\t}\n#\t$d -= $c;\n#\t$d < 0 and $d = 0;\n\tprint \"$c $d\\n\";\n\t\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t@a = split//;\n\t$_ = <>, chomp;\n\t@b = split//;\n\tundef %h;\n\tfor (@b){\n\t\t$h{$_}++;\n\t\t}\n\t$c = $d = 0;\n\tfor (@a){\n\t\tif ($h{$_} ){\n\t\t\t$h{$_}--;\n\t\t\t$c++\n\t\t\t}\n\t\t\telsif ($h{(lc)} + $h{(uc)}){\n\t\t\t\t$d++\n\t\t\t\t}\n\t\t}\n\tprint \"$c $d\\n\";\n\t\n\t}"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000) \u2014 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": "$a = <>;\n$ans = '';\nif ($a%2==1)\n{\n$ans = '7';\n$a-=3;\n}\nprint $ans;\nfor(my $i=0;$i<$a;$i+=2){\nprint '1';\n}"}, {"source_code": "$a = ;\n\nif ($a % 2 == 0) {\n print '1';\n}\nelse {\n print '7';\n}\nprint '1' x ($a / 2 - 1)\n"}, {"source_code": "chomp($n=);\nif ($n % 2 == 1){\n print 7;\n $n -= 3;\n}\nwhile ($n > 0){\n print 1;\n $n -= 2;\n}"}, {"source_code": "$a = ;\n\nif($a%2==1){\n\t$s = \"7\";\n\t$a-=3;\n}\n\nwhile($a>0){\n\t$s=$s.\"1\";\n\t$a-=2;\n}\t\n\nprint \"$s\\n\"\n"}, {"source_code": "$n=;\nchomp($n);\nif($n % 2 ==1) {\n print 7;\n $n=$n-3\n}\nwhile($n>0) {\n print 1;\n $n=$n-2;\n}\n\n"}, {"source_code": "#use strict;\nuse warnings;\n\n$n=;\nif($n%2==1) {\n print \"7\";\n}\nprint \"1\"x(($n-$n%2*3)/2);"}, {"source_code": "$_ = ; my ($n) = split;\nif ($n % 2 == 0) {\n for (my $i = 0; 2 * $i < $n; $i++) {\n print \"1\";\n }\n}\nelse {\n print \"7\";\n $n = $n - 3;\n for (my $i = 0; 2 * $i < $n; $i++) {\n print \"1\";\n }\n}\nprint \"\\n\";"}, {"source_code": "$_ = ; my ($n) = split;\nif ($n % 2 == 0) {\n while ($n) {\n print(1);\n $n -= 2;\n }\n} else {\n print(7);\n $n -= 3;\n while ($n) {\n print(1);\n $n -= 2;\n }\n}\nprint(\"\\n\");"}, {"source_code": " $amount=;\n $one = $amount/2;\n if ($amount%2!=0){\n print(7);\n $one--;\n }\n for($i= 1;$i<=$one;$i++){\n print (1);\n }"}, {"source_code": "$a=0;\nwhile()\n{\n\tchomp;\n\t$a+=$_;\n}\n\nif ($a % 2 == 1) {\n print \"7\";\n\t$a-=3;\n} else {\n\tprint \"1\";\n\t$a-=2;\n}\n\nwhile($a > 0) {\n\tprint \"1\";\n\t$a-=2;\n}"}, {"source_code": "#!/usr/bin/perl -w\nmy $x = ;\nmy $a = '';\nmy $b = 2;\nmy $t = 3;\nif ($x % $b != 0){\n $x = $x - $t;\n print '7';\n}\nwhile ($x > 0){\n $x = $x - $b;\n print '1';\n}"}, {"source_code": "my $n = ;\n\nif ($n % 2 == 1) {\n print '7';\n $n -= 3;\n}\n\nprint '1' x ($n/2);"}], "negative_code": [{"source_code": "$a = ;\n\nwhile ($a >= 6) {\n print \"9\";\n $a -= 6;\n}\n\nif ($a >= 3) {\n print \"7\";\n}\nelsif ($a >= 2) {\n print \"1\";\n}\n"}, {"source_code": "$a = ;\n\nif ($a % 2 == 0) {\n print '1' x ($a / 2 - 1) . '1';\n}\nelse {\n print '1' x ($a / 2 - 1) . '7';\n}\n"}, {"source_code": "$a = ;\n\nif($a%2==1){\n\t$s = \"7\";\n\t$a-=3;\n}\n\nfor($a>0){\n\t$s=$s.\"1\";\n\t$a-=2;\n}\t\n\nprint \"$s\\n\"\n"}, {"source_code": "$a = ;\n\nif($a%2==1){\n\t$s = \"7\";\n\t$a-=3;\n\t$s=$s.\"1\";\n\t$a-=2;\n}\n\nfor($a>0){\n\t$s=$s.\"1\";\n\t$a-=2;\n}\t\n\nprint \"$s\\n\"\n"}, {"source_code": "#use strict;\nuse warnings;\n\n$n=;\nif($n>=6) {\n print 9;\n} else {\n if($n>=3) {\n print \"7\";\n } else {\n if($n>=2) {\n print \"1\";\n }\n }\n}"}, {"source_code": "#use strict;\nuse warnings;\n\n$n=;\nif($n>=6)\n{\n print \"9\\n\";\n} else {\n if($n>=3) {\n print \"7\\n\";\n } else {\n if($n>=2) {\n print \"1\\n\";\n }\n }\n}"}, {"source_code": "#use strict;\nuse warnings;\n\n$n=;\nif($n>=6)\n{\n print \"9\";\n} else {\n if($n>=3) {\n print \"7\";\n } else {\n if($n>=2) {\n print \"1\";\n }\n }\n}"}, {"source_code": " $amount=;\n $nine = $amount/6;\n $amount = $amount%6;\n $seven = $amount/3;\n $amount = $amount%3;\n $one = $amount/2;\n for($i= 1;$i<=$nine;$i++){\n print (9);\n }\n for($i= 1;$i<=$seven;$i++){\n print(7);\n }\n for($i= 1;$i<=$one;$i++){\n print (1);\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.\u00a0e. 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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 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.\u00a0e. 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": ";\nmy @arr;\nforeach $line ( ) {\n chomp( $line );\n my @values = split(':', $line);\n push(@arr, $values[0]*60 + $values[1]);\n}\n\nmy @sorterArr = sort {$a <=> $b} @arr;\n\nmy $prev = 0;\nmy @diff;\n\nforeach $time (@sorterArr) {\n push(@diff, $time - $prev);\n $prev = $time;\n}\n\nmy $res = $diff[0] + 24*60-$prev-1;\nforeach $x (@diff) {\n if ($res < $x-1) {\n $res = $x-1;\n }\n}\nmy $hour = $res/60;\nmy $mins = $res%60;\nprintf(\"%02d:%02d\\n\",$hour,$mins);"}, {"source_code": "my $n = ;\n@arr = ();\nfor (my $i=0; $i < $n; $i++) {\n my $line = ;\n chomp $line;\n ($a,$b)= split /:/, $line;\n $a = 60*$a + 1*$b;\n push(@arr, $a);\n}\n\nfor (my $i=0; $i < $n-1; $i++) {\n for (my $j=$i+1; $j < $n; $j++) {\n if ($arr[$i] > $arr[$j]) {\n ($arr[$i], $arr[$j]) = ($arr[$j], $arr[$i]);\n }\n }\n}\nmy $max = 0;\nfor (my $i=1; $i < $n; $i++) {\n if ($arr[$i] - $arr[$i-1] - 1 > $max) {\n $max = $arr[$i] - $arr[$i-1] - 1;\n }\n}\nif (24*60 - $arr[$n-1] + $arr[0] - 1 > $max) {\n $max = 24*60 - $arr[$n-1] + $arr[0] - 1;\n}\nmy $h = int($max / 60);\nmy $m = $max - $h*60;\nif ($h < 10){\n print 0;\n}\nprint $h . \":\";\nif ($m < 10) {\n print 0;\n}\nprint $m;"}], "negative_code": [], "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,\u2009l,\u2009r) as a bitwise OR of integers xl,\u2009xl\u2009+\u20091,\u2009...,\u2009xr, 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,\u2009l,\u2009r)\u2009+\u2009f(b,\u2009l,\u2009r) among all possible 1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n. ", "input_spec": "The first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000)\u00a0\u2014 the length of the arrays. The second line contains n integers ai (0\u2009\u2264\u2009ai\u2009\u2264\u2009109). The third line contains n integers bi (0\u2009\u2264\u2009bi\u2009\u2264\u2009109).", "output_spec": "Print a single integer\u00a0\u2014 the maximum value of sum f(a,\u2009l,\u2009r)\u2009+\u2009f(b,\u2009l,\u2009r) among all possible 1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n.", "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\u2009=\u2009a 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\u2009=\u20092 and r\u2009=\u20094, because f(a,\u20092,\u20094)\u2009+\u2009f(b,\u20092,\u20094)\u2009=\u2009(2 OR 4 OR 3)\u2009+\u2009(3 OR 3 OR 12)\u2009=\u20097\u2009+\u200915\u2009=\u200922. Other ways to get maximum value is to choose l\u2009=\u20091 and r\u2009=\u20094, l\u2009=\u20091 and r\u2009=\u20095, l\u2009=\u20092 and r\u2009=\u20094, l\u2009=\u20092 and r\u2009=\u20095, l\u2009=\u20093 and r\u2009=\u20094, or l\u2009=\u20093 and r\u2009=\u20095.In the second sample, the maximum value is obtained for l\u2009=\u20091 and r\u2009=\u20099."}, "positive_code": [{"source_code": "<>;while(<>){$a=0;map{$a|=$_}split;$b+=$a}print$b\n"}, {"source_code": "<>;while(<>){$a=0;map{$a|=$_}split;$b+=$a}print$b\n\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\u2009\u2264\u2009n\u2009\u2264\u2009105), the size of the array. The second line contains n integers ai (1\u2009\u2264\u2009ai\u2009\u2264\u2009106), representing elements of the array.", "output_spec": "Output two integers l,\u2009r (1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n), 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\u2009-\u2009l\u2009+\u20091 where Bi\u2009=\u2009Al\u2009+\u2009i\u2009-\u20091 for all 1\u2009\u2264\u2009i\u2009\u2264\u2009r\u2009-\u2009l\u2009+\u20091"}, "positive_code": [{"source_code": "$\\ = $/;\n\n<>;\n@_ = split \" \", <>;\n$h{$_}++ for @_;\n\n@deg = sort {$h{$b} <=> $h{$a}} keys %h;\n\n@cand = grep $h{$deg[0]} == $h{$_}, @deg;\n\n$i = 0;\n++ $i, $L{$_} //= $i, $R{$_} = $i for @_;\n\n$min = ~0;\n\nfor (@cand){\n$R{$_} - $L{$_} < $min and ($min = $R{$_} - $L{$_}, $p = \"$L{$_} $R{$_}\")\n}\n\nprint $p"}, {"source_code": "<>; ++ $h{$_} for @_ = split \" \", <>;\n\n$R{$_} = ++ $i, $L{$_} //= $i for @_;\n\n$min = ~0;\n\n($r, $l) = ($R{$_}, $L{$_}), $r - $l < $min and ($min = $r - $l, $p = \"$l $r\") \n\tfor grep { ( $j //= $h{$_} ) == $h{$_} } sort {$h{$b} <=> $h{$a}} keys %h;\n\nprint $p"}], "negative_code": [], "src_uid": "ecd9bbc05b97f3cd43017dd0eddd014d"} {"nl": {"description": "Recently, Valery have come across an entirely new programming language. Most of all the language attracted him with template functions and procedures. Let us remind you that templates are tools of a language, designed to encode generic algorithms, without reference to some parameters (e.g., data types, buffer sizes, default values).Valery decided to examine template procedures in this language in more detail. The description of a template procedure consists of the procedure name and the list of its parameter types. The generic type T parameters can be used as parameters of template procedures.A procedure call consists of a procedure name and a list of variable parameters. Let's call a procedure suitable for this call if the following conditions are fulfilled: its name equals to the name of the called procedure; the number of its parameters equals to the number of parameters of the procedure call; the types of variables in the procedure call match the corresponding types of its parameters. The variable type matches the type of a parameter if the parameter has a generic type T or the type of the variable and the parameter are the same. You are given a description of some set of template procedures. You are also given a list of variables used in the program, as well as direct procedure calls that use the described variables. For each call you need to count the number of procedures that are suitable for this call.", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of template procedures. The next n lines contain the description of the procedures specified in the following format: \"void procedureName (type_1, type_2, ..., type_t)\" (1\u2009\u2264\u2009t\u2009\u2264\u20095), where void is the keyword, procedureName is the procedure name, type_i is the type of the next parameter. Types of language parameters can be \"int\", \"string\", \"double\", and the keyword \"T\", which denotes the generic type. The next line contains a single integer m (1\u2009\u2264\u2009m\u2009\u2264\u20091000) \u2014 the number of used variables. Next m lines specify the description of the variables in the following format: \"type variableName\", where type is the type of variable that can take values \"int\", \"string\", \"double\", variableName \u2014 the name of the variable. The next line contains a single integer k (1\u2009\u2264\u2009k\u2009\u2264\u20091000) \u2014 the number of procedure calls. Next k lines specify the procedure calls in the following format: \"procedureName (var_1, var_2, ..., var_t)\" (1\u2009\u2264\u2009t\u2009\u2264\u20095), where procedureName is the name of the procedure, var_i is the name of a variable. The lines describing the variables, template procedures and their calls may contain spaces at the beginning of the line and at the end of the line, before and after the brackets and commas. Spaces may be before and after keyword void. The length of each input line does not exceed 100 characters. The names of variables and procedures are non-empty strings of lowercase English letters and numbers with lengths of not more than 10 characters. Note that this is the only condition at the names. Only the specified variables are used in procedure calls. The names of the variables are distinct. No two procedures are the same. Two procedures are the same, if they have identical names and identical ordered sets of types of their parameters.", "output_spec": "On each of k lines print a single number, where the i-th number stands for the number of suitable template procedures for the i-th call.", "sample_inputs": ["4\nvoid f(int,T)\nvoid f(T, T)\n void foo123 ( int, double, string,string ) \n void p(T,double)\n3\nint a\n string s\ndouble x123 \n5\nf(a, a)\n f(s,a )\nfoo (a,s,s)\n f ( s ,x123)\nproc(a)", "6\nvoid f(string,double,int)\nvoid f(int)\n void f ( T )\nvoid procedure(int,double)\nvoid f (T, double,int) \nvoid f(string, T,T)\n4\n int a\n int x\nstring t\ndouble val \n5\nf(t, a, a)\nf(t,val,a)\nf(val,a, val)\n solve300(val, val)\nf (x)"], "sample_outputs": ["2\n1\n0\n1\n0", "1\n3\n0\n0\n2"], "notes": null}, "positive_code": [{"source_code": "chomp(my $n = <>);\nmy %procs;\nfor(1..$n) {\n my ($name, $params) = (<> =~ /void\\s+(\\w+)\\s*\\(([^\\)]*)\\)/);\n my (@params) = ($params =~ /\\w+/g);\n $procs{$name}{join(\"\\f\", @params)} = undef;\n}\nchomp(my $m = <>);\nmy %vars;\nfor(1..$m) {\n my ($type, $name) = (<> =~ /(\\w+)\\s+(\\w+)/);\n $vars{$name} = $type;\n}\nchomp(my $k = <>);\nfor(1..$k) {\n my ($name, $params) = (<> =~ /(\\w+)\\s*\\(([^\\)]*)\\)/);\n my (@params) = map { $vars{$_} } ($params =~ /(\\w+)/g);\n if (exists $procs{$name}) {\n my (@temps) = keys %{ $procs{$name} };\n my $res = 0;\n for my $temp (@temps) {\n my @temp_params = split /\\f/, $temp;\n (scalar @params) eq (scalar @temp_params) or next;\n my $b = 1;\n for (my $i = 0; $i < @params; $i++) {\n ($temp_params[$i] eq 'T') || ($temp_params[$i] eq $params[$i]) or ($b = 0);\n }\n $res += $b;\n }\n print $res . \"\\n\";\n } else {\n print \"0\\n\";\n }\n}"}, {"source_code": "chomp(my $n = <>);\nmy %procs;\nfor(1..$n) {\n my ($name, $params) = (<> =~ /void\\s+(\\w+)\\s*\\(([^\\)]*)\\)/);\n my (@params) = ($params =~ /\\w+/g);\n $procs{$name}{join(\"\\f\", @params)} = undef;\n}\n\nchomp(my $m = <>);\nmy %vars;\nfor(1..$m) {\n my ($type, $name) = (<> =~ /(\\w+)\\s+(\\w+)/);\n $vars{$name} = $type;\n}\n\nchomp(my $k = <>);\nfor(1..$k) {\n my ($name, $params) = (<> =~ /(\\w+)\\s*\\(([^\\)]*)\\)/);\n my (@params) = map { $vars{$_} } ($params =~ /(\\w+)/g);\n if (exists $procs{$name}) {\n my (@temps) = keys %{ $procs{$name} };\n my $res = 0;\n for my $temp (@temps) {\n my @temp_params = split /\\f/, $temp;\n (scalar @params) eq (scalar @temp_params) or next;\n my $b = 1;\n for (my $i = 0; $i < @params; $i++) {\n ($temp_params[$i] eq 'T') || ($temp_params[$i] eq $params[$i]) or ($b = 0);\n }\n $res += $b;\n }\n print $res . \"\\n\";\n } else {\n print \"0\\n\";\n }\n}"}], "negative_code": [], "src_uid": "d5e3136b236f0e84ed6b88e43acd4205"} {"nl": {"description": "Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water.It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; Pasha pours the same amount of water to each girl; Pasha pours the same amount of water to each boy; if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does.Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends.", "input_spec": "The first line of the input contains two integers, n and w (1\u2009\u2264\u2009n\u2009\u2264\u2009105, 1\u2009\u2264\u2009w\u2009\u2264\u2009109)\u00a0\u2014 the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1\u2009\u2264\u2009ai\u2009\u2264\u2009109, 1\u2009\u2264\u2009i\u2009\u2264\u20092n)\u00a0\u2014\u00a0the capacities of Pasha's tea cups in milliliters.", "output_spec": "Print a single real number \u2014 the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10\u2009-\u20096.", "sample_inputs": ["2 4\n1 1 1 1", "3 18\n4 4 4 2 2 2", "1 5\n2 3"], "sample_outputs": ["3", "18", "4.5"], "notes": "NotePasha also has candies that he is going to give to girls but that is another task..."}, "positive_code": [{"source_code": "#!perl\n\n$\\ = \"\\n\";\n\n($n, $w) = split / /, <>;\n@a = split / /, <>;\n\n@a = sort {$a <=> $b} @a;\n$portion = $w/$n/3;\n\nif(2*$portion > @a[$n])\n{\n $portion = @a[$n]/2;\n}\n\nif($portion > @a[0])\n{\n $portion = @a[0];\n}\n\nprint 3*$n*$portion;"}], "negative_code": [], "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,\u2009a2,\u2009...,\u2009an.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,\u2009b2,\u2009...,\u2009bn. 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\u2009\u2264\u2009n\u2009\u2264\u2009105, 1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n) \u2014 the number of Vasya's cubes and the positions told by Stepan. The second line contains the sequence a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009n) \u2014 the sequence of integers written on cubes in the Vasya's order. The third line contains the sequence b1,\u2009b2,\u2009...,\u2009bn (1\u2009\u2264\u2009bi\u2009\u2264\u2009n) \u2014 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": "$_ = ; my ($n, $l, $r) = split;\n$_ = ; my @A = split;\n$_ = ; my @B = split;\n\nmy $arr = [];\n\nfor my $i (0..100100) {\n push @$arr, 0;\n}\n\nfor ($i = $l - 1; $i < $r; $i = $i + 1) {\n\t$cnt[$A[$i]]++;\n\t$cnt[$B[$i]]--;\n}\n\n$flag = 1;\nfor( $a = 0; $a < 100010; $a = $a + 1 ){\n \tif ($cnt[$a] != 0) {\n \t\t$flag = 0;\n \t}\n}\nif ($flag == 0) {\n\tprint 'LIE';\n}\nelse {\n\t# \n\t$flag = 1;\n\tfor ($i = 0; $i < $n; $i = $i + 1) {\n\t\tif ( ($i < $l - 1) || ($i > $r - 1) ) {\n\t\t\tif ($A[$i] != $B[$i]) {\n\t\t\t\t$flag = 0;\n\t\t\t}\t\n\t\t}\t\n\t}\n\t\n\tif ($flag == 0) {\n\t\tprint 'LIE';\n\t}\n\telse {\n\t\tprint 'TRUTH';\n\t}\n}\n"}], "negative_code": [{"source_code": "$_ = ; my ($n, $l, $r) = split;\n$_ = ; my @A = split;\n$_ = ; my @B = split;\n\nmy $arr = [];\n\nfor my $i (0..100100) {\n push @$arr, 0;\n}\n\nfor ($i = $l - 1; $i < $r; $i = $i + 1) {\n\t$cnt[$A[$i]]++;\n\t$cnt[$B[$i]]--;\n}\n\n$flag = 1;\nfor( $a = 0; $a < 100010; $a = $a + 1 ){\n \tif ($cnt[$a] != 0) {\n \t\t$flag = 0;\n \t}\n}\nif ($flag == 0) {\n\tprint 'LIE';\n}\nelse {\n\t# \n\tfor ($i = 0; $i < $n; $i = $i + 1) {\n\t\tif ( ($i < $l - 1) || ($i > $r - 1) ) {\n\t\t\t$cnt[$A[$i]]++;\n\t\t\t$cnt[$B[$i]]--;\t\n\t\t}\t\n\t}\n\t$flag = 1;\n\tfor( $a = 0; $a < 100010; $a = $a + 1 ){\n \t\tif ($cnt[$a] != 0) {\n \t\t\t$flag = 0;\n \t\t}\n\t}\n\tif ($flag == 0) {\n\t\tprint 'LIE';\n\t}\n\telse {\n\t\tprint 'TRUTH';\n\t}\n}\n"}, {"source_code": "$_ = ; my ($n, $l, $r) = split;\n$_ = ; my @A = split;\n$_ = ; my @B = split;\n\n$l--;\n\nmy $arr = [];\nfor my $i (0..100100) {\n push @$arr, 0;\n}\nwhile ($l < $r) {\n\t$cnt[$A[$l]]++;\n\t$cnt[$B[$l]]--;\n\t$l++;\n}\n$flag = 1;\nfor( $a = 0; $a < 100010; $a = $a + 1 ){\n \tif ($cnt[$a] != 0) {\n \t\t$flag = 0;\n \t}\n}\nif ($flag == 0) {\n\tprint 'LIE';\n}\nelse {\n\tprint 'TRUTH';\n}"}], "src_uid": "1951bf085050c7e32fcf713132b30605"} {"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\u2009\u2264\u2009i\u2009\u2264\u2009n\u2009-\u20091), such that ai\u2009+\u20091\u2009>\u2009ai.", "input_spec": "The first line of the input contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000)\u00a0\u2014 the number of painting. The second line contains the sequence a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20091000), where ai means the beauty of the i-th painting.", "output_spec": "Print one integer\u00a0\u2014 the maximum possible number of neighbouring pairs, such that ai\u2009+\u20091\u2009>\u2009ai, 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,\u200920,\u200930,\u200940,\u200950.In the second sample, the optimal order is: 100,\u2009200,\u2009100,\u2009200."}, "positive_code": [{"source_code": "<>; @a = split ' ', <>;\nwhile (@a) {\n my (%h, @b);\n for $a (@a) {\n \tpush(@b, $a) if $h{$a};\n \t$h{$a} = 1;\n }\n $c += keys(%h) - 1;\n @a = @b;\n}\nprint $c;"}, {"source_code": ";\nmy $A = [split ' ', ];\nmy ($h, $c, $ans) = ({}, {}, 0);\n($h->{$_} ||= 0)++ for @$A;\n\nforeach my $k (keys %$h){\n my $v = $h->{$k};\n for(my $i=1; $i<=$v; $i++){\n ($c->{$i} ||= 0)++;\n }\n}\n\n$ans += $_ - 1 for values %$c;\nprint sprintf(\"%d\\n\", $ans);\n"}, {"source_code": "$\\ = $/;\nwhile (<>) {\n $n = $_; $h->{$_}++ for split ' ', <>;\n print $n - (sort {$b <=> $a} values %{$h})[0];\n}"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: \n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $n = $_;\n my @array = sort split ' ', <>;\n my $len = @array;\n my @brrby;# = map 0, (0..$len);\n my $cnt = 0;\n for (1..$#array) {\n $brrby[$cnt]++;\n $cnt++ unless ($array[$_] == $array[$_-1]);\n }\n $brrby[$cnt]++;\n my $sum = 0;\n my $max = $brrby[0];\n for (1..$cnt) {\n if ($brrby[$_] > $max) {\n $sum += $max;\n $max = $brrby[$_];\n } else {$sum += $brrby[$_];}\n }\n print $sum;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: \n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $n = $_;\n my @array = sort split ' ', <>;\n my %ash;\n for (@array) {\n $ash{$_}++;\n }\n @array = uniq @array;\n my $sum = 0;\n my $max = $ash{$array[0]};\n shift @array;\n for (@array) {\n my $tmp = $ash{$_};\n if ($tmp > $max) {\n $sum += $max;\n $max = $tmp;\n } else {\n $sum += $tmp;\n }\n }\n print $sum;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: \n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\nuse List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $n = $_;\n my @array = sort split ' ', <>;\n my %ash;\n for (@array) {\n $ash{$_}++;\n }\n @array = uniq @array;\n my $sum = 0;\n my $max = $ash{$array[0]};\n shift @array;\n for (@array) {\n my $tmp = $ash{$_};\n if ($tmp > $max) {\n $sum += $max;\n $max = $tmp;\n } else {\n $sum += $tmp;\n }\n }\n print $sum;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "$\\ = $/;\n\nwhile (<>) {\n $n = $_;\n $h->{$_}++ for split ' ', <>;\n print $n - (sort {$b <=> $a} values %{$h})[0];\n}\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: Solution 1.\n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $ar = [split ' ', <>];\n my $h;\n $h->{$_}++ for @$ar;\n #@$ar = (1..1000);\n my @myvalues = sort {$b <=> $a} values %{$h};\n print @$ar - $myvalues[0];\n# if (@$ar == 1000) {\n# my @myvalues = sort {$b <=> $a} values %{$h};\n# print \"print myvalues\";\n# print \"@myvalues\";\n# \n# } else {\n# print @$ar - (sort {$b <=> $a} values %{$h})[0];\n# }\n\n# my @array = split ' ', $_;\n# my $sum = 0;\n# $sum += $_ for @array;\n# print $sum;\n# print $sum - $array[0];\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: Solution 1.\n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $ar = [split ' ', <>];\n my $h;\n $h->{$_}++ for @$ar;\n# my @myvalues = sort {$b <=> $a} values %{$h};\n# print @$ar - $myvalues[0];\n# print @$ar - (sort {$b <=> $a} values %{$h})[0];\n# my @array = (1..100);\n# my @brrby = (1..20);\n# $brrby[$#brrby] = 30;\n# print @array - (sort {$b <=> $a} @brrby)[0];\n print @$ar - (sort {$b <=> $a} values %{$h})[0];\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: Solution 1.\n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n#use List::Util qw(max);\n$\\ = $/;\nwhile (<>) {\n $n = $_; $h->{$_}++ for split ' ', <>;\n print $n - (sort {$b <=> $a} values %{$h})[0];\n}\n\nexit 0;\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: \n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $n = $_;\n my @array = sort split ' ', <>;\n my $len = @array;\n my @brrby;\n my $cnt = 0;\n for (1..$#array) {\n $brrby[$cnt]++;\n $cnt++ unless ($array[$_] == $array[$_-1]);\n }\n $brrby[$cnt]++;\n my $sum = 0;\n my $max = $brrby[0];\n for (1..$cnt) {\n if ($brrby[$_] > $max) {\n $sum += $max;\n $max = $brrby[$_];\n } else {$sum += $brrby[$_];}\n }\n print $sum;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: Solution 1.\n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $ar = [split ' ', <>];\n my $h;\n $h->{$_}++ for @$ar;\n# my @myvalues = sort {$b <=> $a} values %{$h};\n# print @$ar - $myvalues[0];\n print @$ar - (sort {$b <=> $a} values %{$h})[0];\n# my @array = (1..100);\n# my @brrby = (1..20);\n# $brrby[$#brrby] = 30;\n# print @array - (sort {$b <=> $a} @brrby)[0];\n# print @$ar - (sort {$b <=> $a} values %{$h})[0];\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}], "negative_code": [{"source_code": ";\nmy $A = [split ' ', ];\nmy ($h, $ans) = ({}, undef);\n$h->{$_} = 1 for @$A;\n$ans = scalar(keys %$h);\nprint sprintf(\"%d\\n\", $ans);\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: Solution 1.\n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $a = [split ' ', <>];\n my $h;\n $h->{$_}++ for @$a;\n print @$a - (sort {$a <=> $b} values %{$h})[0];\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: Solution 1.\n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $a = [split ' ', <>];\n my $h;\n $h->{$_}++ for @$a;\n if (@a == 1000) {\n my @values = sort {$b <=> $a} values %{$h};\n print \"@values\";\n } else {\n print @$a - (sort {$b <=> $a} values %{$h})[0];\n }\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: \n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $n = $_;\n my @array = split ' ', <>;\n @array = sort @array;\n my ($cnt, $pos) = (0, 0);\n\n for (1..$n) {\n if ($array[$pos] < $array[$pos+1]) {\n ++$cnt; shift @array;\n } elsif ($array[$pos] == $array[$pos+1]) {\n if ($array[$pos] == $array[$#array]) {\n last;\n } else {\n ++$cnt; pop @array; shift @array;\n }\n }\n last if (@array == 1 or @array == 0);\n }\n print $cnt;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: Solution 1.\n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $a = [split ' ', <>];\n my $h;\n $h->{$_}++ for @$a;\n print @$a - (sort {$b <=> $a} values %{$h})[0];\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: Solution 1.\n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $a = [split ' ', <>];\n my $h;\n $h->{$_}++ for @$a;\n my @values = sort {$a <=> $b} values %{$h};\n pop @values;\n my $sum = 0;\n $sum += $_ for @values[0..$#values];\n print $sum;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: Solution 1.\n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $ar = [split ' ', <>];\n my $h;\n $h->{$_}++ for @$ar;\n #@$ar = (1..1000);\n if (@$ar == 1000) {\n my @myvalues = sort {$b <=> $a} values %{$h};\n print \"print myvalues\";\n print \"@myvalues\";\n } else {\n print @$ar - (sort {$b <=> $a} values %{$h})[0];\n }\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: Solution 1.\n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $a = [split ' ', <>];\n my $h;\n $h->{$_}++ for @$a;\n my @values = sort values %{$h};\n pop @values;\n my $sum = 0;\n $sum += $_ for @values[0..$#values];\n print $sum;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: Solution 1.\n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $a = [split ' ', <>];\n my $h;\n $h->{$_}++ for @$a;\n my @values = sort values %{$h};\n shift @values;\n my $sum = 0;\n $sum += $_ for @values[0..$#values];\n print $sum;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: 2.pl\n#\n# DESCRIPTION: B. Beautiful Paintings\n#\n# FILES: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com\n# ORGANIZATION: Solution 1.\n# VERSION: 1.0\n# CREATED: 2016/03/17 21\u65f631\u520648\u79d2\n# REVISION: ---\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use List::MoreUtils qw(uniq);\n#use 5.020;\n$\\ = $/;\n\nwhile (<>) {\n my $a = [split ' ', <>];\n my $h;\n $h->{$_}++ for @$a;\n print @$a - (sort {$b <=> $a} values %{$h})[0];\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of strings\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (str_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: num_array_in_array_of_array\n# PURPOSE: find if an array is equal to one of a array of arrays\n# PARAMETERS: two array reference\n# RETURNS: return 1 if find an equal array, 0 otherwise\n# DESCRIPTION: all arrays are array of numbers\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_in_array_of_array {\n my ($small_ar, $ar_ar) = @_;\n my $mark = 0;\n for (@{$ar_ar}) {\n $mark = 1;\n unless (num_array_cmp($small_ar, $_)) {\n $mark = 0;\n next;\n }\n last if $mark;\n }\n $mark;\n}\n\n#=== FUNCTION ================================================================\n# NAME: str_array_cmp\n# PURPOSE: compare two array of strings\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub str_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }\n return 1;\n} ## --- end sub str_array_cmp\n\n#=== FUNCTION ================================================================\n# NAME: num_array_cmp\n# PURPOSE: compare two array of numbers\n# PARAMETERS: two array reference\n# RETURNS: return 1 if two array are equal, 0 otherwise\n# DESCRIPTION: ????\n# THROWS: no exceptions\n# COMMENTS: none\n# SEE ALSO: n/a\n#===============================================================================\nsub num_array_cmp {\n return 0 if @{$_[0]} != @{$_[1]};\n my $len = @{$_[0]};\n for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }\n return 1;\n} ## --- end sub num_array_cmp\n\nexit 0;\n\n\n"}], "src_uid": "30ad5bdc019fcd8a4e642c90decca58f"} {"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\u2009\u2264\u2009fi\u2009\u2264\u2009n and fi\u2009\u2260\u2009i.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\u2009\u2264\u2009n\u2009\u2264\u20095000)\u00a0\u2014 the number of planes. The second line contains n integers f1,\u2009f2,\u2009...,\u2009fn (1\u2009\u2264\u2009fi\u2009\u2264\u2009n, fi\u2009\u2260\u2009i), meaning that the i-th plane likes the fi-th.", "output_spec": "Output \u00abYES\u00bb if there is a love triangle consisting of planes on Earth. Otherwise, output \u00abNO\u00bb. 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $i = 0;\n\t\n\tmy %h = map { ++ $i => $_ } @_;\n\t\n#\tprint \"$_ -> $h{$_}\" for sort keys %h;\n\t\n\tmy %g = map { $_ => 1 } @_;\n\tmy @g = sort keys %g;\n\t\n#\tprint \"[@g]\";\n\t\n\tmy $ok = 0;\n\t\n\twhile( @g ){\n\t\tmy $try = shift @g;\n\t\tif( $h{ $h{ $h{ $try } } } == $try ){\n\t\t\t$ok = 1;\n\t\t\t}\n\t\t}\n\t\n\tprint $ok ? \"YES\" : \"NO\";\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $i = 0;\n\t\n\tmy %h = map { $_ => ++ $i } @_;\n\t\n#\tprint \"$_ -> $h{$_}\" for sort keys %h;\n\t\n\tmy %g = map { $_ => 1 } @_;\n\tmy @g = sort keys %g;\n\t\n#\tprint \"[@g]\";\n\t\n\tmy $ok = 0;\n\t\n\twhile( @g ){\n\t\tmy $try = shift @g;\n\t\tif( $h{ $h{ $h{ $try } } } == $try ){\n\t\t\t$ok = 1;\n\t\t\t}\n\t\t}\n\t\n\tprint $ok ? \"YES\" : \"NO\";\n\t}"}], "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$$$\u00a0\u2014that 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$$$)\u00a0\u2014the 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\t$debug and print '-' x 15;\n\t\n\tmy $n = $_;\n\t\n\t@_ = split ' ', <>;\n\t\n\tfor( @_ ){\n\t\twhile( $_ > $n ){\n\t\t\t$_ >>= 1;\n\t\t\t}\n\t\t}\n\t\n\t@_ = sort { $b <=> $a } @_;\n\t\n\t$debug and print \"@_\";\n\t\n\tmy $fail = 0;\n\t\n\tfor my $i ( reverse 1 .. $n ){\n\t\t\n\t\t@_ = sort { $b <=> $a } @_;\n\t\t\n\t\t$debug and print \"$i: @_\"; \n\t\t\n\t\tif( $_[ 0 ] == $i ){\n\t\t\tshift @_;\n\t\t\t\n\t\t\tmy $j = 0;\n\t\t\t\n\t\t\twhile( $_[ $j ] and $_[ $j ] == $i ){\n\t\t\t\t$_[ $j ] >>= 1;\n\t\t\t\t$j ++;\n\t\t\t\t}\n\t\t\t}\n\t\telse{\n\t\t\t$fail = 1;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tprint $fail ? \"NO\" : \"YES\";\n\t}"}], "negative_code": [], "src_uid": "645459e0a41ec63b13648ea8dbe0f053"} {"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$$$)\u00a0\u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nprint sort m/./g while <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, split //;\n\t\n\t$_ = join '', sort grep { $h{ $_ } == 1 } keys %h;\n\t\n\t$_ .= join '', sort grep { $h{ $_ } == 2 } keys %h;\n\t$_ .= join '', sort grep { $h{ $_ } == 2 } keys %h;\n\t\n\tprint;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m, $r, $c ) = split;\n\t\n\t$debug and print \"[$n, $m, $r, $c]\";\n\t\n\tmy %row;\n\tmy %col;\n\t\n\tmy $ans;\n\t\n\tfor my $i ( 0 .. $n - 1 ){\n\t\t$_ = <>, chomp;\n\t\t\n\t\tnext if defined $ans;\n\t\t\n\t\tm/B/ and $row{ $i } = 1;\n\t\t\n\t\twhile( m/B/g ){\n\t\t\t$col{ $-[ 0 ] } = 1;\n\t\t\t\n\t\t\tif( $r - 1 == $i and $-[ 0 ] == $c - 1 ){\n\t\t\t\t$ans = 0;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tif( defined $ans ){\n\t\tprint $ans;\n\t\t}\n\telsif( %row or %col ){\n\t\tif( $row{ $r - 1 } or $col{ $c - 1 } ){\n\t\t\tprint 1;\n\t\t\t}\n\t\telse{\n\t\t\tprint 2;\n\t\t\t}\n\t\t}\n\telse{\n\t\tprint -1;\n\t\t}\n\t}"}], "src_uid": "28102f75e0798960740e5a2625393c8f"} {"nl": {"description": "Given simple (without self-intersections) n-gon. It is not necessary convex. Also you are given m lines. For each line find the length of common part of the line and the n-gon.The boundary of n-gon belongs to polygon. It is possible that n-gon contains 180-degree angles.", "input_spec": "The first line contains integers n and m (3\u2009\u2264\u2009n\u2009\u2264\u20091000;1\u2009\u2264\u2009m\u2009\u2264\u2009100). The following n lines contain coordinates of polygon vertices (in clockwise or counterclockwise direction). All vertices are distinct. The following m lines contain line descriptions. Each of them contains two distict points of a line by their coordinates. All given in the input coordinates are real numbers, given with at most two digits after decimal point. They do not exceed 105 by absolute values.", "output_spec": "Print m lines, the i-th line should contain the length of common part of the given n-gon and the i-th line. The answer will be considered correct if the absolute or relative error doesn't exceed 10\u2009-\u20096.", "sample_inputs": ["4 3\n0 0\n1 0\n1 1\n0 1\n0 0 1 1\n0 0 0 1\n0 0 1 -1"], "sample_outputs": ["1.41421356237309514547\n1.00000000000000000000\n0.00000000000000000000"], "notes": null}, "positive_code": [{"source_code": "use strict;\n\npackage calc;\n\nsub new {\n\tmy $self = { len => 0, start => undef, in_border => 0, inside => 0 };\n\tbless $self;\n}\n\nsub enter {\n\tmy ($calc, $coord) = @_;\n\t$calc->{start} = $coord;\n $calc->{inside} = 1;\n}\n\nsub leave {\n\tmy ($calc, $coord) = @_;\n\t$calc->{len} += abs($coord - $calc->{start});\n $calc->{start} = undef;\n $calc->{inside} = 0;\n}\n\nsub enter_leave {\n\tmy ($calc, $coord) = @_;\n\tif ($calc->{inside}) {\t\n\t\t$calc->leave($coord);\n\t} else {\t\t\n\t\t$calc->enter($coord);\n\t}\n}\n\nsub enter_border {\n\tmy ($calc, $coord, $level) = @_;\n\t$calc->{border_level} = $level;\n\t$calc->{border_inside} = $calc->{inside};\n\t$calc->{in_border} = 1;\n\t$calc->enter($coord) unless $calc->{inside};\n}\n\nsub leave_border {\n\tmy ($calc, $coord, $level) = @_;\n\tmy $s = $calc->{border_level} * $level;\n\tmy $i = $calc->{border_inside};\n\tif (($i == 0 && $s > 0) || ($i == 1 && $s < 0)) {\n\t\t$calc->leave($coord);\n\t}\n\t$calc->{in_border} = 0;\n}\n\nsub in_border {\n\tshift->{in_border};\n}\n\nsub ans {\n\tshift->{len};\n}\n\npackage main;\n\nsub fz {\n\tmy $f = shift;\n\tabs($f) < 1E-10? 0: $f;\n}\n\nsub line {\n\tmy ($x1, $y1, $x2, $y2) = @_;\n\tmy ($k, $b, $v, $x, $sin, $cos);\n\t$v = ($x1 == $x2);\n\tif ($v) {\n\t\t$x = $x1;\n\t\t$sin = 1;\n\t\t$cos = 0;\n\t} else {\n\t\tmy $dx = ($x2 - $x1);\n\t\tmy $dy = ($y2 - $y1);\n\t\tif ($dx < 0) {\n\t\t\t$dx = -$dx;\n\t\t\t$dy = -$dy;\n\t\t}\n\t\t$k = $dy / $dx;\n\t\t$b = $y1 - $k * $x1;\n\t\tmy $l = sqrt($dx * $dx + $dy * $dy);\n\t\t$sin = $dy / $l;\n\t\t$cos = $dx / $l;\n\t}\n\t{ k => $k, b => $b, v => $v, x => $x, sin => $sin, cos => $cos }\n}\n\nsub newxy {\n\tmy ($m, $nref, $xref, $yref) = @_;\n\tmy $n = @$nref;\n\tmy ($xshift, $yshift);\n\tif ($m->{v}) {\n\t\t$xshift = $m->{x};\n\t} else {\n\t\t$yshift = $m->{b};\n\t}\n\tmy $sin = $m->{sin};\n\tmy $cos = $m->{cos};\n\tfor (my $i = 0; $i < $n; $i++) {\n\t\tmy $x = $$nref[$i][0] - $xshift;\n\t\tmy $y = $$nref[$i][1] - $yshift;\n\t\t$$xref[$i] = $x * $cos + $y * $sin;\n\t\t$$yref[$i] = fz($y * $cos - $x * $sin);\n\t}\n}\n\nsub entry_points {\n\tmy ($xref, $yref) = @_;\n\tmy $n = @$xref;\n\tmy @i;\n\tfor (my $i = 0; $i < $n; $i++) {\n\t\tmy $y = $yref->[$i];\n\t\tmy $yp = $yref->[$i - 1];\n\t\tmy $yn = $yref->[($i + 1) % $n];\n\t\tif ($y == 0) {\n\t\t\tif ($yp * $yn < 0 || ($yp * $yn == 0 && $yp + $yn != 0)) {\n\t\t\t\tpush(@i, [$i, $xref->[$i], $y, $yp, $yn]);\n\t\t\t}\n\t\t} elsif ($y * $yp < 0) {\n\t\t\tmy $x = $xref->[$i] + ($xref->[$i - 1] - $xref->[$i])/(1 - $yp / $y);\n \t\tpush(@i, [$i, $x, $y, $yp, $yn]);\n\t\t}\n\t}\n\t@i = sort { $a->[1] <=> $b->[1] } @i;\n\t@i;\n}\n\nsub calc_line {\n\tmy ($m, $nref) = @_;\n\tmy $n = @$nref;\n\tmy $c = calc->new();\n\n\t# new (x, y)\n\tmy (@x, @y);\n\tnewxy($m, $nref, \\@x, \\@y);\n\n\t# entry points\n\tmy @i = entry_points(\\@x, \\@y);\n\n\t# enter, leave, calc\n\tfor my $i (@i) {\n\t\tmy (undef, $x, $y, $yp, $yn) = @$i;\n\t\tif ($y == 0) {\n\t\t\tif ($c->in_border) {\n\t\t\t\t$c->leave_border($x, $yp + $yn);\n\t\t\t} elsif ($yp * $yn == 0) {\n\t\t\t\t$c->enter_border($x, $yp + $yn);\n\t\t\t} elsif ($yp * $yn < 0) {\n\t\t\t\t$c->enter_leave($x) \n\t\t\t}\n\t\t} else {\n\t\t\t# y <> 0\n\t\t\t$c->enter_leave($x);\n\t\t}\n\t}\n\t$c->ans();\n}\n\nsub input {\n\tmy ($fh, $nref, $mref) = @_;\n\tmy $input = sub { split(' ', <$fh>) };\n\tmy ($n, $m) = &$input();\n\t# n: x0 y0\n\t$nref->[$_] = [ &$input() ] for 0 .. $n - 1;\n\t# m: x0 y0 x1 y1\n\t$mref->[$_] = [ &$input() ] for 0 .. $m - 1;\n}\n\nsub go {\n\tmy (@n, @m);\n\tinput(\\*STDIN, \\@n, \\@m);\n\tfor my $m (@m) {\n\t\tmy $ans = calc_line(line(@$m), \\@n);\n\t\tprint $ans, \" \";\n\t}\n\tprint \"\\n\";\n}\n\ngo() unless exists $INC{\"Test/More.pm\"};\n\n1;\n"}], "negative_code": [{"source_code": "use strict;\nuse constant DEV => $0 eq '1.pl' && 0;\n# use Time::HiRes qw(time);\n\nour (@n, @m, $pstart, $inside, $inside_edge, $enter_edge);\n\nsub trace {\n\tprint STDERR \"[trace] \", @_, \"\\n\" if DEV;\n}\n\nsub since {\n#\tmy $t0 = shift;\n#\tsprintf(\"%.3f\", time() - $t0) * 1000;\n\t0;\n}\n\nsub max {\n\tmy ($a, $b) = @_;\n\t$a > $b? $a: $b;\n}\n\nsub f2 {\n\tsprintf \"%.2f\", shift;\n}\n\nsub fe {\n\tmy ($a, $b) = @_;\n\treturn abs($a - $b) < 1E-12? 1: 0;\n}\n\nsub fle {\n\tmy ($a, $b) = @_;\n\treturn 1 if fe($a, $b);\n\treturn $a <= $b;\n}\n\nsub p {\n\tref($_[0])?\n\t\t{ x=>$_[0]->[0], y=>$_[0]->[1] }:\n\t\t{ x=>$_[0], y=>$_[1] }\n}\n\nsub pstr {\n\tmy $p = shift || $_;\n\t\"{x=\".f2($p->{x}).\",y=\".f2($p->{y}).\"}\";\n}\n\nsub pxy {\n\tmy $p = shift;\n\t($p->{x}, $p->{y});\n}\n\nsub line {\n\tmy @p = @_;\n\tif ($p[0]->{x} > $p[1]->{x}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy $v = $p[0]->{x}==$p[1]->{x}? 1: 0;\n\t!$v || $p[0]->{y}!=$p[1]->{y} || die;\n\tif ($v && $p[0]->{y} > $p[1]->{y}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy ($k,$b);\n\tif (!$v) {\n\t\t$k=($p[1]->{y} - $p[0]->{y}) / ($p[1]->{x} - $p[0]->{x});\n\t\t$b=$p[0]->{y}-$k*$p[0]->{x};\n\t}\n\t{ p=>\\@p, k=>$k, b=>$b, v=>$v, x=>$p[0]->{x} }\n}\n\nsub linep {\n\tmy $l = shift;\n\t@{ $l->{p} };\n}\n\nsub linestr {\n\tmy $l = shift;\n\t\"{line:\".join(\",\",map(pstr,linep($l))).\"}\";\n}\n\nsub linestr2 {\n\tmy $l = shift;\n\t\"{\".join(\",\",map {$_->{x}.\",\".$_->{y}} linep($l)).\"}\";\n}\n\nsub yx {\n\tmy ($l,$x)=@_;\n\t!$l->{v} || die;\n\t$l->{k}*$x+$l->{b};\n}\n\nsub peq {\n\tfe($_[0]->{x}, $_[1]->{x}) && fe($_[0]->{y}, $_[1]->{y});\n}\n\nsub line_ab {\n\tmy ($a, $b) = @_;\n\tmy ($rv, $rv2);\n\n\tif ($a->{v} && $b->{v} && $a->{x}!=$b->{x}) {\n\t\t$rv=[0];\n\t} elsif ($a->{v} && $b->{v} && $a->{x}==$b->{x}) {\n\t\t$rv=[3];\n\t} elsif ($a->{v} && !$b->{v}) {\n\t\t$rv=[1, p($a->{x}, yx($b,$a->{x}))];\n\t} elsif (!$a->{v} && $b->{v}) {\n\t\t$rv=[1, p($b->{x}, yx($a,$b->{x}))];\n\t} elsif ($a->{k}==$b->{k} && $a->{b}==$b->{b}) {\n\t\t$rv=[3];\t\t\n\t} elsif ($a->{k}==$b->{k} && $a->{b}!=$b->{b}) {\n\t\t$rv=[0];\n\t} elsif ($a->{k}!=$b->{k}) {\n\t\tmy $x = ($b->{b} - $a->{b}) / ($a->{k} - $b->{k});\n\t\t$rv=[1, p($x, yx($a,$x))];\n\t} else {\n\t\tdie;\n\t}\n\n\tmy ($n, $e) = @$rv;\n\tif ($n == 1) {\n\t\tif (abc($b->{p}->[0], $e, $b->{p}->[1])) {\n\t\t\tif (peq($e, $b->{p}->[0]) || peq($e, $b->{p}->[1])) {\n\t\t\t\t# vertex\n\t\t\t\t$rv = [2, $e, $b];\n\t\t\t} else {\n\t\t\t\t# edge\n\t\t\t\t$rv = [1, $e, $b];\n\t\t\t}\n\t\t} else {\n\t\t\t$rv = [0];\n\t\t}\n\t} elsif ($n == 3) {\n\t\t# whole edge\n\t\t$rv = [3, $b->{p}->[0], $b];\n\t\t$rv2 = [3, $b->{p}->[1], $b];\n\t}\n\n\tif ($rv->[0] == 0) {\n\t\t$rv = undef;\n\t}\n\n\twantarray? ($rv, $rv2): $rv->[0];\n}\n\nsub abstr {\n\tmy $ab = shift;\n\tdefined($ab)? \"{n=\".$ab->[0].\",\".pstr($ab->[1]).\",\".(-2+@$ab).\"}\": \"{undef}\";\n}\n\nsub abc {\n\tmy ($a,$b,$c)=@_;\n\tmy ($x1, $x2, $x3) = ($a->{x}, $b->{x}, $c->{x});\n\tmy ($y1, $y2, $y3) = ($a->{y}, $b->{y}, $c->{y});\n\tif ($x1 > $x3) {\n\t\t($x1, $x3) = ($x3, $x1);\n\t}\n\tif ($y1 > $y3) {\n\t\t($y1, $y3) = ($y3, $y1);\n\t}\n\tfle($x1,$x2) && fle($x2,$x3) && fle($y1,$y2) && fle($y2,$y3)? 1: 0;\n}\n\nsub len {\n\tmy ($a, $b) = @_;\n\tsqrt( ($a->{x} - $b->{x})**2 + ($a->{y} - $b->{y})**2 );\n}\n\nsub get_2points {\n\tmy ($points, $exclude) = @_;\n\tmy @found;\n\tP: foreach my $p (@$points) {\n\t\tforeach my $e (@$exclude) {\n\t\t\tpeq($p, $e) && next P;\n\t\t}\n\t\tforeach my $f (@found) {\n\t\t\tpeq($p, $f) && next P;\n\t\t}\n\t\tpush @found, $p;\n\t}\n\t@found == 2 || die 0+@found;\n\t@found;\n}\n\nsub enter_leave {\n\tmy ($p, $s) = @_;\n\tif ($inside) {\t# leave\n \tmy $sadd = len($pstart, $p);\n\t\t$s += $sadd;\n \ttrace(\"leave \", pstr($p),\" pstart=\", pstr($pstart), \" sadd=\", f2($sadd), \" s=\", f2($s));\n \t$pstart = undef;\n \t$inside = 0;\n\t} else {\t\t# enter\n \t$pstart = $p;\n \t$inside = 1;\n \ttrace(\"enter \", pstr($pstart));\n\t}\n\t$s;\n}\n\nsub calc_line {\n\tmy ($m_num, $m) = @_;\n\tmy (@ab, @ab1, $s);\n\ttrace(\"--- calc_line $m_num \".linestr($m));\n\n\t# intersections\n\tforeach my $n (@n) {\n\t\tmy ($ab1, $ab2) = line_ab($m, $n);\n\t\ttrace(linestr($m),\" \",linestr($n), \" \", abstr($ab1), \" \", abstr($ab2));\n\t\tif ($ab1) {\n\t\t\tpush(@ab, $ab1);\n\t\t\ttrace(\"ab \", abstr($ab1));\n\t\t}\n\t\tif ($ab2) {\n\t\t\tpush(@ab, $ab2);\n\t\t\ttrace(\"ab:2 \", abstr($ab2));\n\t\t}\n\t}\n\n\t# sort\n\t@ab = sort { \n\t\tmy ($ax, $ay, $bx, $by) = (pxy($a->[1]), pxy($b->[1]));\n\t\t$ax == $bx? $ay <=> $by: $ax <=> $bx;\n\t} @ab;\n\ttrace(\"sort \", 0+@ab);\n\n\t# doubles\n\tfor (my $i = 0; $i < @ab; $i++) {\n\t\tif ($i < @ab - 1) {\n\t\t\tmy ($n1, $e1, $s1) = @{ $ab[$i] };\n\t\t\tmy ($n2, $e2, $s2) = @{ $ab[$i + 1] };\n\t\t\tpeq($e1, $e2) && do {\n\t\t\t\tmy $ab1 = [max($n1,$n2), $e1, $s1, $s2]; \n\t\t\t\tpush @ab1, $ab1;\n\t\t\t\ttrace(\"ab1:2 \", abstr($ab1));\n\t\t\t\t$i++;\n\t\t\t\tnext;\n\t\t\t};\n\t\t} \n\t\tpush @ab1, $ab[$i];\n\t\ttrace(\"ab1 \", abstr($ab[$i]));\n\t}\n\ttrace(\"ab1 \", 0+@ab1);\n\n\t# enter, leave, calc\n\t$s = 0;\n\t$inside = 0;\n\tfor (my $i = 0; $i < @ab1; $i++) {\n\t\tmy ($n, $p, $s1, $s2) = @{ $ab1[$i] };\n\n\t\t# edge\n\t\t$n == 1 && do {\n\t\t\t$s = enter_leave($p, $s);\n\t\t};\n\t\t\n\t\t# vertex\n\t\t$n == 2 && do {\n\t\t\tif (line_ab($m, line(get_2points([linep($s1), linep($s2)], [$p])))) {\n\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t}\n\t\t};\n\n\t\t# edge through\n\t\t$n == 3 && do {\n \t\t\tif ($inside_edge) {\n \t\t\t\t# vertex - 1\n \t\t\t\tmy ($prev_n, $prev_p, $prev_s1, $prev_s2) = @{ $ab1[$i - 1] };\n \t\t\t\t@n == 3 && do {\n \t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t};\n\t\t\t\t@n > 3 && do {\n\t\t\t\t\tmy $l = line(get_2points([linep($s1), linep($s2), linep($prev_s1), linep($prev_s2)], [$p, $prev_p]));\n\t\t\t\t\tmy $ml = line_ab($m, $l);\n\t\t\t\t\t$ml && $enter_edge && do {\n\t\t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t\t};\n\t\t\t\t\t!$ml && !$enter_edge && do {\n\t\t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t\t};\n \t\t\t\t};\n \t\t\t\t$inside_edge = 0;\n \t\t\t} else {\n\t\t\t\t$enter_edge = $inside;\n \t\t\t\tif (!$inside) {\n\t \t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t}\n \t\t\t\t$inside_edge = 1;\n \t\t\t}\n\t\t};\n\t}\n\n\ttrace(\"retval \", $s);\n\t$s;\n}\n\nsub input {\n\tmy $fh = shift;\n\tmy $s = sub { [split(' ',<$fh>)] };\n\tmy ($n,$m) = @{&$s()};\n\ttrace(\"n=$n m=$m\");\n\n\tif ($n==842 && $m==25) {\n\t\t$n[$_] = &$s() for 0..$n-1;\n\t\t$m[$_] = &$s() for 0..$m-1;\n\t\tprint \"$n,$m \";\n\t\tprint join(\",\",@{$m[0]}), \" \";\n\t\tfor (my $i = $n - 1; $i >= 0; $i--) {\n\t\t\tprint join(\",\", @{$n[$i]}), \" \";\n\t\t}\n\t\texit(0);\n\t}\n\n\t# n: x0 y0\n\tmy @n_;\n\t$n_[$_]=&$s() for 0..$n-1; push @n_,$n_[0];\n\t$n[$_] = line(p($n_[$_]), p($n_[$_+1])) for 0..$n-1;\n\t\n\t# m: x0 y0 x1 y1\n\tfor (0..$m-1) {\n\t\tmy @p = @{ &$s() };\n\t\t$m[$_] = line(p(@p[0,1]), p(@p[2,3]));\n\t}\n}\n\nsub calc {\n\t$| = 1;\n\tfor (my $i = 0; $i < @m; $i++) {\n\t\tmy $t = time();\n\t\teval {\n\t\t\tmy $s = calc_line($i + 1, $m[$i], \\@n);\n\t\t\tif (@n == 842) {\n\t\t\t\tprint $i+1, linestr2($m[$i]), \"=\",since($t), \" \";\n\t\t\t} else {\n\t\t\t\tprint \"$s \";\n\t\t\t}\n\t\t\t$inside == 0 || die;\n\t\t};\n\t\tif ($@) {\n\t\t\tprint \"calc \", $i+1, \" \", linestr($m[$i]), \" \", since($t), \" \", $@, \"\\n\";\n\t\t\texit(0);\n\t\t}\n\t}\t\n\tprint \"\\n\";\n}\n\nif (DEV) {\n\tuse File::Spec::Functions;\n\tmy $t; open($t,'<',catfile('t','e8'));\n\tinput($t);\n} else {\n\tinput(\\*STDIN);\n}\ncalc();\n\n__DATA__\n4 1\n0 0\n4 0\n4 4\n0 4\n2 0 2 4"}, {"source_code": "use strict;\nuse constant DEV => $0 eq '1.pl' && 0;\n\nour (@n, @m, $pstart, $inside, $inside_edge, $enter_edge);\n\nsub trace {\n\tprint STDERR \"[trace] \", @_, \"\\n\" if DEV;\n}\n\nsub max {\n\tmy ($a, $b) = @_;\n\t$a > $b? $a: $b;\n}\n\nsub f2 {\n\tsprintf \"%.2f\", shift;\n}\n\nsub fe {\n\tmy ($a, $b) = @_;\n\treturn abs($a - $b) < 1E-12? 1: 0;\n}\n\nsub fle {\n\tmy ($a, $b) = @_;\n\treturn 1 if fe($a, $b);\n\treturn $a <= $b;\n}\n\nsub p {\n\tref($_[0])?\n\t\t{ x=>$_[0]->[0], y=>$_[0]->[1] }:\n\t\t{ x=>$_[0], y=>$_[1] }\n}\n\nsub pstr {\n\tmy $p = shift || $_;\n\t\"{x=\".f2($p->{x}).\",y=\".f2($p->{y}).\"}\";\n}\n\nsub pxy {\n\tmy $p = shift;\n\t($p->{x}, $p->{y});\n}\n\nsub line {\n\tmy @p = @_;\n\tif ($p[0]->{x} > $p[1]->{x}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy $v = $p[0]->{x}==$p[1]->{x}? 1: 0;\n\t!$v || $p[0]->{y}!=$p[1]->{y} || die;\n\tif ($v && $p[0]->{y} > $p[1]->{y}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy ($k,$b);\n\tif (!$v) {\n\t\t$k=($p[1]->{y} - $p[0]->{y}) / ($p[1]->{x} - $p[0]->{x});\n\t\t$b=$p[0]->{y}-$k*$p[0]->{x};\n\t}\n\t{ p=>\\@p, k=>$k, b=>$b, v=>$v, x=>$p[0]->{x} }\n}\n\nsub linep {\n\tmy $l = shift;\n\t@{ $l->{p} };\n}\n\nsub linestr {\n\tmy $l = shift;\n\t\"{line:\".join(\",\",map(pstr,linep($l))).\"}\";\n}\n\nsub yx {\n\tmy ($l,$x)=@_;\n\t!$l->{v} || die;\n\t$l->{k}*$x+$l->{b};\n}\n\nsub peq {\n\tfe($_[0]->{x}, $_[1]->{x}) && fe($_[0]->{y}, $_[1]->{y});\n}\n\nsub line_ab {\n\tmy ($a, $b) = @_;\n\tmy ($rv, $rv2);\n\n\tif ($a->{v} && $b->{v} && $a->{x}!=$b->{x}) {\n\t\t$rv=[0];\n\t} elsif ($a->{v} && $b->{v} && $a->{x}==$b->{x}) {\n\t\t$rv=[3];\n\t} elsif ($a->{v} && !$b->{v}) {\n\t\t$rv=[1, p($a->{x}, yx($b,$a->{x}))];\n\t} elsif (!$a->{v} && $b->{v}) {\n\t\t$rv=[1, p($b->{x}, yx($a,$b->{x}))];\n\t} elsif ($a->{k}==$b->{k} && $a->{b}==$b->{b}) {\n\t\t$rv=[3];\t\t\n\t} elsif ($a->{k}==$b->{k} && $a->{b}!=$b->{b}) {\n\t\t$rv=[0];\n\t} elsif ($a->{k}!=$b->{k}) {\n\t\tmy $x = ($b->{b} - $a->{b}) / ($a->{k} - $b->{k});\n\t\t$rv=[1, p($x, yx($a,$x))];\n\t} else {\n\t\tdie;\n\t}\n\n\tmy ($n, $e) = @$rv;\n\tif ($n == 1) {\n\t\tif (abc($b->{p}->[0], $e, $b->{p}->[1])) {\n\t\t\tif (peq($e, $b->{p}->[0]) || peq($e, $b->{p}->[1])) {\n\t\t\t\t# vertex\n\t\t\t\t$rv = [2, $e, $b];\n\t\t\t} else {\n\t\t\t\t# edge\n\t\t\t\t$rv = [1, $e, $b];\n\t\t\t}\n\t\t} else {\n\t\t\t$rv = [0];\n\t\t}\n\t} elsif ($n == 3) {\n\t\t# whole edge\n\t\t$rv = [3, $b->{p}->[0], $b];\n\t\t$rv2 = [3, $b->{p}->[1], $b];\n\t}\n\n\tif ($rv->[0] == 0) {\n\t\t$rv = undef;\n\t}\n\n\twantarray? ($rv, $rv2): $rv->[0];\n}\n\nsub abstr {\n\tmy $ab = shift;\n\tdefined($ab)? \"{n=\".$ab->[0].\",\".pstr($ab->[1]).\",\".(-2+@$ab).\"}\": \"{undef}\";\n}\n\nsub abc {\n\tmy ($a,$b,$c)=@_;\n\tmy ($x1, $x2, $x3) = ($a->{x}, $b->{x}, $c->{x});\n\tmy ($y1, $y2, $y3) = ($a->{y}, $b->{y}, $c->{y});\n\tif ($x1 > $x3) {\n\t\t($x1, $x3) = ($x3, $x1);\n\t}\n\tif ($y1 > $y3) {\n\t\t($y1, $y3) = ($y3, $y1);\n\t}\n\tfle($x1,$x2) && fle($x2,$x3) && fle($y1,$y2) && fle($y2,$y3)? 1: 0;\n}\n\nsub len {\n\tmy ($a, $b) = @_;\n\tsqrt( ($a->{x} - $b->{x})**2 + ($a->{y} - $b->{y})**2 );\n}\n\nsub get_2points {\n\tmy ($points, $exclude) = @_;\n\tmy @found;\n\tP: foreach my $p (@$points) {\n\t\tforeach my $e (@$exclude) {\n\t\t\tpeq($p, $e) && next P;\n\t\t}\n\t\tforeach my $f (@found) {\n\t\t\tpeq($p, $f) && next P;\n\t\t}\n\t\tpush @found, $p;\n\t}\n\t@found == 2 || die 0+@found;\n\t@found;\n}\n\nsub enter_leave {\n\tmy ($p, $s) = @_;\n\tif ($inside) {\t# leave\n \tmy $sadd = len($pstart, $p);\n\t\t$s += $sadd;\n \ttrace(\"leave \", pstr($p),\" pstart=\", pstr($pstart), \" sadd=\", f2($sadd), \" s=\", f2($s));\n \t$pstart = undef;\n \t$inside = 0;\n\t} else {\t\t# enter\n \t$pstart = $p;\n \t$inside = 1;\n \ttrace(\"enter \", pstr($pstart));\n\t}\n\t$s;\n}\n\nsub calc_line {\n\tmy ($m_num, $m) = @_;\n\tmy (@ab, @ab1, $s);\n\ttrace(\"--- calc_line $m_num \".linestr($m));\n\n\t# intersections\n\tforeach my $n (@n) {\n\t\tmy ($ab1, $ab2) = line_ab($m, $n);\n\t\ttrace(linestr($m),\" \",linestr($n), \" \", abstr($ab1), \" \", abstr($ab2));\n\t\tif ($ab1) {\n\t\t\tpush(@ab, $ab1);\n\t\t\ttrace(\"ab \", abstr($ab1));\n\t\t}\n\t\tif ($ab2) {\n\t\t\tpush(@ab, $ab2);\n\t\t\ttrace(\"ab:2 \", abstr($ab2));\n\t\t}\n\t}\n\n\t# sort\n\t@ab = sort { \n\t\tmy ($ax, $ay, $bx, $by) = (pxy($a->[1]), pxy($b->[1]));\n\t\t$ax == $bx? $ay <=> $by: $ax <=> $bx;\n\t} @ab;\n\ttrace(\"sort \", 0+@ab);\n\n\t# doubles\n\tfor (my $i = 0; $i < @ab; $i++) {\n\t\tif ($i < @ab - 1) {\n\t\t\tmy ($n1, $e1, $s1) = @{ $ab[$i] };\n\t\t\tmy ($n2, $e2, $s2) = @{ $ab[$i + 1] };\n\t\t\tpeq($e1, $e2) && do {\n\t\t\t\tmy $ab1 = [max($n1,$n2), $e1, $s1, $s2]; \n\t\t\t\tpush @ab1, $ab1;\n\t\t\t\ttrace(\"ab1:2 \", abstr($ab1));\n\t\t\t\t$i++;\n\t\t\t\tnext;\n\t\t\t};\n\t\t} \n\t\tpush @ab1, $ab[$i];\n\t\ttrace(\"ab1 \", abstr($ab[$i]));\n\t}\n\ttrace(\"ab1 \", 0+@ab1);\n\n\t# enter, leave, calc\n\t$s = 0;\n\t$inside = 0;\n\tfor (my $i = 0; $i < @ab1; $i++) {\n\t\tmy ($n, $p, $s1, $s2) = @{ $ab1[$i] };\n\n\t\t# edge\n\t\t$n == 1 && do {\n\t\t\t$s = enter_leave($p, $s);\n\t\t};\n\t\t\n\t\t# vertex\n\t\t$n == 2 && do {\n\t\t\tif (line_ab($m, line(get_2points([linep($s1), linep($s2)], [$p])))) {\n\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t}\n\t\t};\n\n\t\t# edge through\n\t\t$n == 3 && do {\n \t\t\tif ($inside_edge) {\n \t\t\t\t# vertex - 1\n \t\t\t\tmy ($prev_n, $prev_p, $prev_s1, $prev_s2) = @{ $ab1[$i - 1] };\n \t\t\t\t@n == 3 && do {\n \t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t};\n\t\t\t\t@n > 3 && do {\n\t\t\t\t\tmy $l = line(get_2points([linep($s1), linep($s2), linep($prev_s1), linep($prev_s2)], [$p, $prev_p]));\n\t\t\t\t\tmy $ml = line_ab($m, $l);\n\t\t\t\t\t$ml && $enter_edge && do {\n\t\t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t\t};\n\t\t\t\t\t!$ml && !$enter_edge && do {\n\t\t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t\t};\n \t\t\t\t};\n \t\t\t\t$inside_edge = 0;\n \t\t\t} else {\n\t\t\t\t$enter_edge = $inside;\n \t\t\t\tif (!$inside) {\n\t \t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t}\n \t\t\t\t$inside_edge = 1;\n \t\t\t}\n\t\t};\n\t}\n\n\ttrace(\"retval \", $s);\n\t$s;\n}\n\nsub input {\n\tmy $fh = shift;\n\tmy $s = sub { [split(' ',<$fh>)] };\n\tmy ($n,$m) = @{&$s()};\n\ttrace(\"n=$n m=$m\");\n\n\tif ($n==842 && $m==25) {\n\t\t$n[$_] = &$s() for 0..$n-1;\n\t\t$m[$_] = &$s() for 0..$m-1;\n\t\tprint \"$n,$m \";\n\t\tprint join(\",\",@{$m[0]}), \" \";\n\t\tfor (my $i = $n - 1; $i >= 0; $i--) {\n\t\t\tprint join(\",\", @{$n[$i]}), \" \";\n\t\t}\n\t\texit(0);\n\t}\n\n\t# n: x0 y0\n\tmy @n_;\n\t$n_[$_]=&$s() for 0..$n-1; push @n_,$n_[0];\n\t$n[$_] = line(p($n_[$_]), p($n_[$_+1])) for 0..$n-1;\n\t\n\t# m: x0 y0 x1 y1\n\tfor (0..$m-1) {\n\t\tmy @p = @{ &$s() };\n\t\t$m[$_] = line(p(@p[0,1]), p(@p[2,3]));\n\t}\n}\n\nsub calc {\n\tfor (my $i = 0; $i < @m; $i++) {\n\t\teval {\n\t\t\tmy $s = calc_line($i + 1, $m[$i], \\@n);\n\t\t\tprint \"$s \";\n\t\t\t$inside == 0 || die;\n\t\t};\n\t\tif ($@) {\n\t\t\tprint \"calc \", $i+1, \" \", linestr($m[$i]), \" \", $@, \"\\n\";\n\t\t\texit(0);\n\t\t}\n\t}\t\n\tprint \"\\n\";\n}\n\nif (DEV) {\n\tuse File::Spec::Functions;\n\tmy $t; open($t,'<',catfile('t','e8'));\n\tinput($t);\n} else {\n\tinput(\\*STDIN);\n}\ncalc();\n\n__DATA__\n4 1\n0 0\n4 0\n4 4\n0 4\n2 0 2 4"}, {"source_code": "use strict;\n\npackage calc;\n\nsub new {\n\tmy $self = { len => 0, start => undef, in_border => 0, inside => 0 };\n\tbless $self;\n}\n\nsub enter {\n\tmy ($calc, $coord) = @_;\n\t$calc->{start} = $coord;\n $calc->{inside} = 1;\n}\n\nsub leave {\n\tmy ($calc, $coord) = @_;\n\t$calc->{len} += abs($coord - $calc->{start});\n $calc->{start} = undef;\n $calc->{inside} = 0;\n}\n\nsub enter_leave {\n\tmy ($calc, $coord) = @_;\n\tif ($calc->{inside}) {\t\n\t\t$calc->leave($coord);\n\t} else {\t\t\n\t\t$calc->enter($coord);\n\t}\n}\n\nsub enter_border {\n\tmy ($calc, $coord, $level) = @_;\n\t$calc->{border_level} = $level;\n\t$calc->{border_inside} = $calc->{inside};\n\t$calc->{in_border} = 1;\n\t$calc->enter($coord) unless $calc->{inside};\n}\n\nsub leave_border {\n\tmy ($calc, $coord, $level) = @_;\n\tmy $s = $calc->{border_level} * $level;\n\tmy $i = $calc->{border_inside};\n\tif (($i == 0 && $s > 0) || ($i == 1 && $s < 0)) {\n\t\t$calc->leave($coord);\n\t}\n\t$calc->{in_border} = 0;\n}\n\nsub in_border {\n\tshift->{in_border};\n}\n\nsub ans {\n\tshift->{len};\n}\n\npackage main;\n\nsub fz {\n\tmy $f = shift;\n\tabs($f) < 1E-12? 0: $f;\n}\n\nsub line {\n\tmy ($x1, $y1, $x2, $y2) = @_;\n\tmy ($k, $b, $v, $x, $sin, $cos);\n\t$v = ($x1 == $x2);\n\tif ($v) {\n\t\t$x = $x1;\n\t\t$sin = 1;\n\t\t$cos = 0;\n\t} else {\n\t\tmy $dx = ($x2 - $x1);\n\t\tmy $dy = ($y2 - $y1);\n\t\tif ($dx < 0) {\n\t\t\t$dx = -$dx;\n\t\t\t$dy = -$dy;\n\t\t}\n\t\t$k = $dy / $dx;\n\t\t$b = $y1 - $k * $x1;\n\t\tmy $l = sqrt($dx * $dx + $dy * $dy);\n\t\t$sin = $dy / $l;\n\t\t$cos = $dx / $l;\n\t}\n\t{ k => $k, b => $b, v => $v, x => $x, sin => $sin, cos => $cos }\n}\n\nsub newxy {\n\tmy ($m, $nref, $xref, $yref) = @_;\n\tmy $n = @$nref;\n\tmy ($xshift, $yshift);\n\tif ($m->{v}) {\n\t\t$xshift = $m->{x};\n\t} else {\n\t\t$yshift = $m->{b};\n\t}\n\tmy $sin = $m->{sin};\n\tmy $cos = $m->{cos};\n\tfor (my $i = 0; $i < $n; $i++) {\n\t\tmy $x = $$nref[$i][0] - $xshift;\n\t\tmy $y = $$nref[$i][1] - $yshift;\n\t\t$$xref[$i] = $x * $cos + $y * $sin;\n\t\t$$yref[$i] = fz($y * $cos - $x * $sin);\n\t}\n}\n\nsub entry_points {\n\tmy ($xref, $yref) = @_;\n\tmy $n = @$xref;\n\tmy @i;\n\tfor (my $i = 0; $i < $n; $i++) {\n\t\tmy $y = $yref->[$i];\n\t\tmy $yp = $yref->[$i - 1];\n\t\tmy $yn = $yref->[($i + 1) % $n];\n\t\tif ($y == 0 && $yp * $yn <= 0) {\n\t\t\tpush(@i, [$i, $xref->[$i], $y, $yp, $yn]);\n\t\t} elsif ($y * $yp < 0) {\n\t\t\tmy $x = $xref->[$i] + ($xref->[$i - 1] - $xref->[$i])/(1 - $yp / $y);\n \t\tpush(@i, [$i, $x, $y, $yp, $yn]);\n\t\t}\n\t}\n\t@i = sort { $a->[1] <=> $b->[1] } @i;\n\t@i;\n}\n\nsub calc_line {\n\tmy ($m, $nref) = @_;\n\tmy $n = @$nref;\n\tmy $c = calc->new();\n\n\t# new (x, y)\n\tmy (@x, @y);\n\tnewxy($m, $nref, \\@x, \\@y);\n\n\t# entry points\n\tmy @i = entry_points(\\@x, \\@y);\n\n\t# enter, leave, calc\n\tfor my $i (@i) {\n\t\tmy (undef, $x, $y, $yp, $yn) = @$i;\n\t\tif ($y == 0) {\n\t\t\tif ($c->in_border) {\n\t\t\t\t$c->leave_border($x, $yp + $yn);\n\t\t\t} elsif ($yp * $yn == 0) {\n\t\t\t\t$c->enter_border($x, $yp + $yn);\n\t\t\t} elsif ($yp * $yn < 0) {\n\t\t\t\t$c->enter_leave($x) \n\t\t\t}\n\t\t} else {\n\t\t\t# y <> 0\n\t\t\t$c->enter_leave($x);\n\t\t}\n\t}\n\t$c->ans();\n}\n\nsub input {\n\tmy ($fh, $nref, $mref) = @_;\n\tmy $input = sub { split(' ', <$fh>) };\n\tmy ($n, $m) = &$input();\n\t# n: x0 y0\n\t$nref->[$_] = [ &$input() ] for 0 .. $n - 1;\n\t# m: x0 y0 x1 y1\n\t$mref->[$_] = [ &$input() ] for 0 .. $m - 1;\n}\n\nsub go {\n\tmy (@n, @m);\n\tinput(\\*STDIN, \\@n, \\@m);\n\tfor my $m (@m) {\n\t\tmy $ans = calc_line(line(@$m), \\@n);\n\t\tprint $ans, \" \";\n\t}\n\tprint \"\\n\";\n}\n\ngo() unless exists $INC{\"Test/More.pm\"};\n\n1;\n"}, {"source_code": "use strict;\n\nour (@n, @m, $pstart, $inside);\n\nsub p {\n\tref($_[0])?\n\t\t{ x=>$_[0]->[0], y=>$_[0]->[1] }:\n\t\t{ x=>$_[0], y=>$_[1] }\n}\n\nsub pstr {\n\tmy $p = shift;\n\t\"{x=\".$p->{x}.\",y=\".$p->{y}.\"}\";\n}\n\nsub pxy {\n\tmy $p = shift;\n\t($p->{x}, $p->{y});\n}\n\nsub line {\n\tmy @p=@_;\n\tif ($p[0]->{x} > $p[1]->{x}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy $v=$p[0]->{x}==$p[1]->{x}?1:0;\n\t!$v || $p[0]->{y}!=$p[1]->{y} || die;\n\tif ($v && $p[0]->{y} > $p[1]->{y}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy ($k,$b);\n\tif (!$v) {\n\t\t$k=($p[1]->{y} - $p[0]->{y}) / ($p[1]->{x} - $p[0]->{x});\n\t\t$b=$p[0]->{y}-$k*$p[0]->{x};\n\t}\n\t{ p=>\\@p, k=>$k, b=>$b, v=>$v, x=>$p[0]->{x} }\n}\n\nsub yx {\n\tmy ($l,$x)=@_;\n\t!$l->{v} || die;\n\t$l->{k}*$x+$l->{b};\n}\n\nsub peq {\n\t$_[0]->{x}==$_[1]->{x} && $_[0]->{y}==$_[1]->{y};\n}\n\nsub line_ab {\n\tmy ($a, $b) = @_;\n\tmy $rv;\n\tif ($a->{v} && $b->{v} && $a->{x}!=$b->{x}) {\n\t\t$rv=[0];\n\t} elsif ($a->{v} && $b->{v} && $a->{x}==$b->{x}) {\n\t\t$rv=[2];\n\t} elsif ($a->{v} && !$b->{v}) {\n\t\t$rv=[1, p($a->{x}, yx($b,$a->{x}))];\n\t} elsif (!$a->{v} && $b->{v}) {\n\t\t$rv=[1, p($b->{x}, yx($a,$b->{x}))];\n\t} elsif ($a->{k}==$b->{k} && $a->{b}==$b->{b}) {\n\t\t$rv=[2];\t\t\n\t} elsif ($a->{k}==$b->{k} && $a->{b}!=$b->{b}) {\n\t\t$rv=[0];\n\t} elsif ($a->{k}!=$b->{k}) {\n\t\tmy $x = ($b->{b} - $a->{b}) / ($a->{k} - $b->{k});\n\t\t$rv=[1, p($x, yx($a,$x))];\n\t} else {\n\t\tdie;\n\t}\n\t$rv;\n}\n\nsub abc {\n\tmy ($a,$b,$c)=@_;\n\tmy ($x1, $x2, $x3) = ($a->{x}, $b->{x}, $c->{x});\n\tmy ($y1, $y2, $y3) = ($a->{y}, $b->{y}, $c->{y});\n\tif ($x1 > $x3) {\n\t\t($x1, $x3) = ($x3, $x1);\n\t}\n\tif ($y1 > $y3) {\n\t\t($y1, $y3) = ($y3, $y1);\n\t}\n\t$x1<=$x2 && $x2<=$x3 && $y1<=$y2 && $y2<=$y3? 1: 0;\n}\n\nsub len {\n\tmy ($a, $b) = @_;\n\tsqrt( ($a->{x} - $b->{x})**2 + ($a->{y} - $b->{y})**2 );\n}\n\nsub line_seg_ab {\n\tmy ($a, $b) = @_;\n\tmy ($n, $e) = @{ line_ab($a, $b) };\n\tmy ($rv1, $rv2);\n\tif ($n==1) {\n\t\t$rv1 = abc($b->{p}->[0], $e, $b->{p}->[1])? \n\t\t\t[$e, $b]: undef;\n\t} elsif ($n==2) {\n\t\t$rv1 = [$b->{p}->[0], $b];\n\t\t$rv2 = [$b->{p}->[1], $b];\n\t}\n\twantarray? ($rv1, $rv2): $rv1?1:0+$rv2?1:0;\n}\n\nsub enter_leave {\n\tmy ($p, $s) = @_;\n\tif ($inside) {\t# leave\n \t$s += len($pstart, $p);\n \ttrace(\"leave pstart=\", pstr($pstart), \" p=\", pstr($p), \" s=$s\");\n \t$pstart = undef;\n \t$inside = 0;\n\t} else {\t\t# enter\n \t$pstart = $p;\n \t$inside = 1;\n \ttrace(\"enter \", pstr($pstart));\n\t}\n\t$s;\n}\n\nsub calc_line {\n\tmy ($m_num, $m) = @_;\n\tmy (@ab, @ab1, $s);\n\ttrace(\"--- calc_line $m_num\");\n\n\t# intersections\n\tforeach my $n (@n) {\n\t\tmy ($ab1, $ab2) = line_seg_ab($m, $n);\n\t\tif ($ab1) {\n\t\t\tpush(@ab, $ab1);\n\t\t\ttrace(\"ab \", pstr($ab1->[0]));\n\t\t}\n\t\tif ($ab2) {\n\t\t\tpush(@ab, $ab2);\n\t\t\ttrace(\"ab:2 \", pstr($ab2->[0]));\n\t\t}\n\t}\n\n\t# sort\n\t@ab = sort { \n\t\tmy ($ax, $ay, $bx, $by) = (pxy($a->[0]), pxy($b->[0]));\n\t\t$ax == $bx? $ay <=> $by: $ax <=> $bx;\n\t} @ab;\n\ttrace(\"sort \", 0+@ab);\n\n\t# doubles\n\tfor (my $i = 0; $i < @ab; $i++) {\n\t\tif ($i == @ab - 1 || !peq($ab[$i]->[0], $ab[$i + 1]->[0])) {\n\t\t\t# edge\n\t\t\tpush @ab1, [1, @{$ab[$i]}]; \n\t\t} else {\n\t\t\t# vertex\n\t\t\tpush @ab1, [2, @{$ab[$i]}, $ab[$i + 1]->[1]]; \n\t\t\t$i++;\n\t\t}\n\t}\n\ttrace(\"ab1 \", 0+@ab1);\n\n\t# enter, leave, calc\n\t$s = 0;\n\tfor (my $i = 0; $i < @ab1; $i++) {\n\t\tmy ($n, $p, $s1, $s2) = @{ $ab1[$i] };\n\t\t$n == 1 && do {\n\t\t\t$s = enter_leave($p, $s);\n\t\t};\n\t\t$n == 2 && do {\n\t\t\tmy ($p1, $p2, $p3, $p4) = (@{$s1->{p}}, @{$s2->{p}});\n\t\t\tif (line_seg_ab($m, line(peq($p,$p1)?$p2:$p1, peq($p,$p3)?$p4:$p3))) {\n\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t}\n\t\t};\n\t}\n\n\t$s;\n}\n\nsub input {\n\tmy $fh = shift;\n\tmy $s = sub { [split(' ',<$fh>)] };\n\tmy ($n,$m) = @{&$s()};\n\ttrace(\"n=$n m=$m\");\n\n\t# n: x0 y0\n\tmy @n_;\n\t$n_[$_]=&$s() for 0..$n-1; push @n_,$n_[0];\n\t$n[$_] = line(p($n_[$_]), p($n_[$_+1])) for 0..$n-1;\n\t\n\t# m: x0 y0 x1 y1\n\tfor (0..$m-1) {\n\t\tmy @p = @{ &$s() };\n\t\t$m[$_] = line(p(@p[0,1]), p(@p[2,3]));\n\t}\n}\n\nsub trace {\n\tprint STDERR \"[trace] \", @_, \"\\n\" if $^O ne 'MSWin32';\n}\n\nsub calc {\n\tfor (my $i = 0; $i < @m; $i++) {\n\t\tprint calc_line($i + 1, $m[$i], \\@n), \" \";\n\t}\t\n\tprint \"\\n\";\n}\n\ninput($^O eq 'MSWin32'? \\*STDIN: \\*DATA);\ncalc();\ntrace(\"2 6 9\");\n\n__DATA__\n10 3\n0 1\n2 6\n3 3\n5 9\n6 10\n9 1\n9 7\n12 -2\n2 3\n0 -1\n0 1 0 -1\n0 1 14 1\n6 0 6 1\n"}, {"source_code": "use strict;\nuse constant DEV => $0 eq '1.pl' && 0;\n\nour (@n, @m, $pstart, $inside, $inside_edge, $enter_edge);\n\nsub trace {\n\tprint STDERR \"[trace] \", @_, \"\\n\" if DEV;\n}\n\nsub max {\n\tmy ($a, $b) = @_;\n\t$a > $b? $a: $b;\n}\n\nsub f2 {\n\tsprintf \"%.2f\", shift;\n}\n\nsub fe {\n\tmy ($a, $b) = @_;\n\treturn abs($a - $b) < 1E-12? 1: 0;\n}\n\nsub fle {\n\tmy ($a, $b) = @_;\n\treturn 1 if fe($a, $b);\n\treturn $a <= $b;\n}\n\nsub p {\n\tref($_[0])?\n\t\t{ x=>$_[0]->[0], y=>$_[0]->[1] }:\n\t\t{ x=>$_[0], y=>$_[1] }\n}\n\nsub pstr {\n\tmy $p = shift || $_;\n\t\"{x=\".f2($p->{x}).\",y=\".f2($p->{y}).\"}\";\n}\n\nsub pxy {\n\tmy $p = shift;\n\t($p->{x}, $p->{y});\n}\n\nsub line {\n\tmy @p = @_;\n\tif ($p[0]->{x} > $p[1]->{x}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy $v = $p[0]->{x}==$p[1]->{x}? 1: 0;\n\t!$v || $p[0]->{y}!=$p[1]->{y} || die;\n\tif ($v && $p[0]->{y} > $p[1]->{y}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy ($k,$b);\n\tif (!$v) {\n\t\t$k=($p[1]->{y} - $p[0]->{y}) / ($p[1]->{x} - $p[0]->{x});\n\t\t$b=$p[0]->{y}-$k*$p[0]->{x};\n\t}\n\t{ p=>\\@p, k=>$k, b=>$b, v=>$v, x=>$p[0]->{x} }\n}\n\nsub linep {\n\tmy $l = shift;\n\t@{ $l->{p} };\n}\n\nsub linestr {\n\tmy $l = shift;\n\t\"{line:\".join(\",\",map(pstr,linep($l))).\"}\";\n}\n\nsub yx {\n\tmy ($l,$x)=@_;\n\t!$l->{v} || die;\n\t$l->{k}*$x+$l->{b};\n}\n\nsub peq {\n\tfe($_[0]->{x}, $_[1]->{x}) && fe($_[0]->{y}, $_[1]->{y});\n}\n\nsub line_ab {\n\tmy ($a, $b) = @_;\n\tmy ($rv, $rv2);\n\n\tif ($a->{v} && $b->{v} && $a->{x}!=$b->{x}) {\n\t\t$rv=[0];\n\t} elsif ($a->{v} && $b->{v} && $a->{x}==$b->{x}) {\n\t\t$rv=[3];\n\t} elsif ($a->{v} && !$b->{v}) {\n\t\t$rv=[1, p($a->{x}, yx($b,$a->{x}))];\n\t} elsif (!$a->{v} && $b->{v}) {\n\t\t$rv=[1, p($b->{x}, yx($a,$b->{x}))];\n\t} elsif ($a->{k}==$b->{k} && $a->{b}==$b->{b}) {\n\t\t$rv=[3];\t\t\n\t} elsif ($a->{k}==$b->{k} && $a->{b}!=$b->{b}) {\n\t\t$rv=[0];\n\t} elsif ($a->{k}!=$b->{k}) {\n\t\tmy $x = ($b->{b} - $a->{b}) / ($a->{k} - $b->{k});\n\t\t$rv=[1, p($x, yx($a,$x))];\n\t} else {\n\t\tdie;\n\t}\n\n\tmy ($n, $e) = @$rv;\n\tif ($n == 1) {\n\t\tif (abc($b->{p}->[0], $e, $b->{p}->[1])) {\n\t\t\tif (peq($e, $b->{p}->[0]) || peq($e, $b->{p}->[1])) {\n\t\t\t\t# vertex\n\t\t\t\t$rv = [2, $e, $b];\n\t\t\t} else {\n\t\t\t\t# edge\n\t\t\t\t$rv = [1, $e, $b];\n\t\t\t}\n\t\t} else {\n\t\t\t$rv = [0];\n\t\t}\n\t} elsif ($n == 3) {\n\t\t# whole edge\n\t\t$rv = [3, $b->{p}->[0], $b];\n\t\t$rv2 = [3, $b->{p}->[1], $b];\n\t}\n\n\tif ($rv->[0] == 0) {\n\t\t$rv = undef;\n\t}\n\n\twantarray? ($rv, $rv2): $rv->[0];\n}\n\nsub abstr {\n\tmy $ab = shift;\n\tdefined($ab)? \"{n=\".$ab->[0].\",\".pstr($ab->[1]).\",\".(-2+@$ab).\"}\": \"{undef}\";\n}\n\nsub abc {\n\tmy ($a,$b,$c)=@_;\n\tmy ($x1, $x2, $x3) = ($a->{x}, $b->{x}, $c->{x});\n\tmy ($y1, $y2, $y3) = ($a->{y}, $b->{y}, $c->{y});\n\tif ($x1 > $x3) {\n\t\t($x1, $x3) = ($x3, $x1);\n\t}\n\tif ($y1 > $y3) {\n\t\t($y1, $y3) = ($y3, $y1);\n\t}\n\tfle($x1,$x2) && fle($x2,$x3) && fle($y1,$y2) && fle($y2,$y3)? 1: 0;\n}\n\nsub len {\n\tmy ($a, $b) = @_;\n\tsqrt( ($a->{x} - $b->{x})**2 + ($a->{y} - $b->{y})**2 );\n}\n\nsub get_2points {\n\tmy ($points, $exclude) = @_;\n\tmy @found;\n\tP: foreach my $p (@$points) {\n\t\tforeach my $e (@$exclude) {\n\t\t\tpeq($p, $e) && next P;\n\t\t}\n\t\tforeach my $f (@found) {\n\t\t\tpeq($p, $f) && next P;\n\t\t}\n\t\tpush @found, $p;\n\t}\n\t@found == 2 || die 0+@found;\n\t@found;\n}\n\nsub enter_leave {\n\tmy ($p, $s) = @_;\n\tif ($inside) {\t# leave\n \tmy $sadd = len($pstart, $p);\n\t\t$s += $sadd;\n \ttrace(\"leave \", pstr($p),\" pstart=\", pstr($pstart), \" sadd=\", f2($sadd), \" s=\", f2($s));\n \t$pstart = undef;\n \t$inside = 0;\n\t} else {\t\t# enter\n \t$pstart = $p;\n \t$inside = 1;\n \ttrace(\"enter \", pstr($pstart));\n\t}\n\t$s;\n}\n\nsub calc_line {\n\tmy ($m_num, $m) = @_;\n\tmy (@ab, @ab1, $s);\n\ttrace(\"--- calc_line $m_num \".linestr($m));\n\n\t# intersections\n\tforeach my $n (@n) {\n\t\tmy ($ab1, $ab2) = line_ab($m, $n);\n\t\ttrace(linestr($m),\" \",linestr($n), \" \", abstr($ab1), \" \", abstr($ab2));\n\t\tif ($ab1) {\n\t\t\tpush(@ab, $ab1);\n\t\t\ttrace(\"ab \", abstr($ab1));\n\t\t}\n\t\tif ($ab2) {\n\t\t\tpush(@ab, $ab2);\n\t\t\ttrace(\"ab:2 \", abstr($ab2));\n\t\t}\n\t}\n\n\t# sort\n\t@ab = sort { \n\t\tmy ($ax, $ay, $bx, $by) = (pxy($a->[1]), pxy($b->[1]));\n\t\t$ax == $bx? $ay <=> $by: $ax <=> $bx;\n\t} @ab;\n\ttrace(\"sort \", 0+@ab);\n\n\t# doubles\n\tfor (my $i = 0; $i < @ab; $i++) {\n\t\tif ($i < @ab - 1) {\n\t\t\tmy ($n1, $e1, $s1) = @{ $ab[$i] };\n\t\t\tmy ($n2, $e2, $s2) = @{ $ab[$i + 1] };\n\t\t\tpeq($e1, $e2) && do {\n\t\t\t\tmy $ab1 = [max($n1,$n2), $e1, $s1, $s2]; \n\t\t\t\tpush @ab1, $ab1;\n\t\t\t\ttrace(\"ab1:2 \", abstr($ab1));\n\t\t\t\t$i++;\n\t\t\t\tnext;\n\t\t\t};\n\t\t} \n\t\tpush @ab1, $ab[$i];\n\t\ttrace(\"ab1 \", abstr($ab[$i]));\n\t}\n\ttrace(\"ab1 \", 0+@ab1);\n\n\t# enter, leave, calc\n\t$s = 0;\n\t$inside = 0;\n\tfor (my $i = 0; $i < @ab1; $i++) {\n\t\tmy ($n, $p, $s1, $s2) = @{ $ab1[$i] };\n\n\t\t# edge\n\t\t$n == 1 && do {\n\t\t\t$s = enter_leave($p, $s);\n\t\t};\n\t\t\n\t\t# vertex\n\t\t$n == 2 && do {\n\t\t\tif (line_ab($m, line(get_2points([linep($s1), linep($s2)], [$p])))) {\n\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t}\n\t\t};\n\n\t\t# edge through\n\t\t$n == 3 && do {\n \t\t\tif ($inside_edge) {\n \t\t\t\t# vertex - 1\n \t\t\t\tmy ($prev_n, $prev_p, $prev_s1, $prev_s2) = @{ $ab1[$i - 1] };\n \t\t\t\t@n == 3 && do {\n \t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t};\n\t\t\t\t@n > 3 && do {\n\t\t\t\t\tmy $l = line(get_2points([linep($s1), linep($s2), linep($prev_s1), linep($prev_s2)], [$p, $prev_p]));\n\t\t\t\t\tmy $ml = line_ab($m, $l);\n\t\t\t\t\t$ml && $enter_edge && do {\n\t\t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t\t};\n\t\t\t\t\t!$ml && !$enter_edge && do {\n\t\t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t\t};\n \t\t\t\t};\n \t\t\t\t$inside_edge = 0;\n \t\t\t} else {\n\t\t\t\t$enter_edge = $inside;\n \t\t\t\tif (!$inside) {\n\t \t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t}\n \t\t\t\t$inside_edge = 1;\n \t\t\t}\n\t\t};\n\t}\n\n\ttrace(\"retval \", $s);\n\t$s;\n}\n\nsub input {\n\tmy $fh = shift;\n\tmy $s = sub { [split(' ',<$fh>)] };\n\tmy ($n,$m) = @{&$s()};\n\ttrace(\"n=$n m=$m\");\n\n\tif ($n==842 && $m==25) {\n\t\t$n[$_] = &$s() for 0..$n-1;\n\t\t$m[$_] = &$s() for 0..$m-1;\n\t\tprint join(\",\",@{$m[0]}), \" \";\n\t\tfor (my $i = $n - 1; $i >= 0; $i--) {\n\t\t\tprint join(\",\", @{$n[$i]}), \" \";\n\t\t}\n\t\texit(0);\n\t}\n\n\t# n: x0 y0\n\tmy @n_;\n\t$n_[$_]=&$s() for 0..$n-1; push @n_,$n_[0];\n\t$n[$_] = line(p($n_[$_]), p($n_[$_+1])) for 0..$n-1;\n\t\n\t# m: x0 y0 x1 y1\n\tfor (0..$m-1) {\n\t\tmy @p = @{ &$s() };\n\t\t$m[$_] = line(p(@p[0,1]), p(@p[2,3]));\n\t}\n}\n\nsub calc {\n\tfor (my $i = 0; $i < @m; $i++) {\n\t\teval {\n\t\t\tmy $s = calc_line($i + 1, $m[$i], \\@n);\n\t\t\tprint \"$s \";\n\t\t\t$inside == 0 || die;\n\t\t};\n\t\tif ($@) {\n\t\t\tprint \"calc \", $i+1, \" \", linestr($m[$i]), \" \", $@, \"\\n\";\n\t\t\texit(0);\n\t\t}\n\t}\t\n\tprint \"\\n\";\n}\n\nif (DEV) {\n\tuse File::Spec::Functions;\n\tmy $t; open($t,'<',catfile('t','e8'));\n\tinput($t);\n} else {\n\tinput(\\*STDIN);\n}\ncalc();\n\n__DATA__\n4 1\n0 0\n4 0\n4 4\n0 4\n2 0 2 4"}, {"source_code": "use strict;\nuse constant DEV => 0;\n\nour (@n, @m, $pstart, $inside, $inside_edge);\n\nsub trace {\n\tprint STDERR \"[trace] \", @_, \"\\n\" if DEV;\n}\n\nsub max {\n\tmy ($a, $b) = @_;\n\t$a > $b? $a: $b;\n}\n\nsub p {\n\tref($_[0])?\n\t\t{ x=>$_[0]->[0], y=>$_[0]->[1] }:\n\t\t{ x=>$_[0], y=>$_[1] }\n}\n\nsub pstr {\n\tmy $p = shift || $_;\n\t\"{x=\".$p->{x}.\",y=\".$p->{y}.\"}\";\n}\n\nsub pxy {\n\tmy $p = shift;\n\t($p->{x}, $p->{y});\n}\n\nsub line {\n\tmy @p = @_;\n\tif ($p[0]->{x} > $p[1]->{x}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy $v = $p[0]->{x}==$p[1]->{x}? 1: 0;\n\t!$v || $p[0]->{y}!=$p[1]->{y} || die;\n\tif ($v && $p[0]->{y} > $p[1]->{y}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy ($k,$b);\n\tif (!$v) {\n\t\t$k=($p[1]->{y} - $p[0]->{y}) / ($p[1]->{x} - $p[0]->{x});\n\t\t$b=$p[0]->{y}-$k*$p[0]->{x};\n\t}\n\t{ p=>\\@p, k=>$k, b=>$b, v=>$v, x=>$p[0]->{x} }\n}\n\nsub linep {\n\tmy $l = shift;\n\t@{ $l->{p} };\n}\n\nsub linestr {\n\tmy $l = shift;\n\t\"{line:\".join(\",\",map(pstr,linep($l))).\"}\";\n}\n\nsub yx {\n\tmy ($l,$x)=@_;\n\t!$l->{v} || die;\n\t$l->{k}*$x+$l->{b};\n}\n\nsub peq {\n\t$_[0]->{x}==$_[1]->{x} && $_[0]->{y}==$_[1]->{y};\n}\n\nsub line_ab {\n\tmy ($a, $b) = @_;\n\tmy ($rv, $rv2);\n\n\tif ($a->{v} && $b->{v} && $a->{x}!=$b->{x}) {\n\t\t$rv=[0];\n\t} elsif ($a->{v} && $b->{v} && $a->{x}==$b->{x}) {\n\t\t$rv=[3];\n\t} elsif ($a->{v} && !$b->{v}) {\n\t\t$rv=[1, p($a->{x}, yx($b,$a->{x}))];\n\t} elsif (!$a->{v} && $b->{v}) {\n\t\t$rv=[1, p($b->{x}, yx($a,$b->{x}))];\n\t} elsif ($a->{k}==$b->{k} && $a->{b}==$b->{b}) {\n\t\t$rv=[3];\t\t\n\t} elsif ($a->{k}==$b->{k} && $a->{b}!=$b->{b}) {\n\t\t$rv=[0];\n\t} elsif ($a->{k}!=$b->{k}) {\n\t\tmy $x = ($b->{b} - $a->{b}) / ($a->{k} - $b->{k});\n\t\t$rv=[1, p($x, yx($a,$x))];\n\t} else {\n\t\tdie;\n\t}\n\n\tmy ($n, $e) = @$rv;\n\tif ($n == 1) {\n\t\tif (abc($b->{p}->[0], $e, $b->{p}->[1])) {\n\t\t\tif (peq($e, $b->{p}->[0]) || peq($e, $b->{p}->[1])) {\n\t\t\t\t# vertex\n\t\t\t\t$rv = [2, $e, $b];\n\t\t\t} else {\n\t\t\t\t# edge\n\t\t\t\t$rv = [1, $e, $b];\n\t\t\t}\n\t\t} else {\n\t\t\t$rv = [0];\n\t\t}\n\t} elsif ($n == 3) {\n\t\t# whole edge\n\t\t$rv = [3, $b->{p}->[0], $b];\n\t\t$rv2 = [3, $b->{p}->[1], $b];\n\t}\n\n\tif ($rv->[0] == 0) {\n\t\t$rv = undef;\n\t}\n\n\twantarray? ($rv, $rv2): $rv->[0];\n}\n\nsub abstr {\n\tmy $ab = shift;\n\t\"{n=\".$ab->[0].\",\".pstr($ab->[1]).\",\".(-2+@$ab).\"}\";\n}\n\nsub abc {\n\tmy ($a,$b,$c)=@_;\n\tmy ($x1, $x2, $x3) = ($a->{x}, $b->{x}, $c->{x});\n\tmy ($y1, $y2, $y3) = ($a->{y}, $b->{y}, $c->{y});\n\tif ($x1 > $x3) {\n\t\t($x1, $x3) = ($x3, $x1);\n\t}\n\tif ($y1 > $y3) {\n\t\t($y1, $y3) = ($y3, $y1);\n\t}\n\t$x1<=$x2 && $x2<=$x3 && $y1<=$y2 && $y2<=$y3? 1: 0;\n}\n\nsub len {\n\tmy ($a, $b) = @_;\n\tsqrt( ($a->{x} - $b->{x})**2 + ($a->{y} - $b->{y})**2 );\n}\n\nsub get_2points {\n\tmy ($points, $exclude) = @_;\n\tmy @found;\n\tP: foreach my $p (@$points) {\n\t\tforeach my $e (@$exclude) {\n\t\t\tpeq($p, $e) && next P;\n\t\t}\n\t\tforeach my $f (@found) {\n\t\t\tpeq($p, $f) && next P;\n\t\t}\n\t\tpush @found, $p;\n\t}\n\t@found == 2 || die 0+@found;\n\t@found;\n}\n\nsub enter_leave {\n\tmy ($p, $s) = @_;\n\tif ($inside) {\t# leave\n \tmy $sadd = len($pstart, $p);\n\t\t$s += $sadd;\n \ttrace(\"leave \", pstr($p),\" pstart=\", pstr($pstart), \" sadd=$sadd s=$s\");\n \t$pstart = undef;\n \t$inside = 0;\n\t} else {\t\t# enter\n \t$pstart = $p;\n \t$inside = 1;\n \ttrace(\"enter \", pstr($pstart));\n\t}\n\t$s;\n}\n\nsub calc_line {\n\tmy ($m_num, $m) = @_;\n\tmy (@ab, @ab1, $s);\n\ttrace(\"--- calc_line $m_num \".linestr($m));\n\n\t# intersections\n\tforeach my $n (@n) {\n\t\tmy ($ab1, $ab2) = line_ab($m, $n);\n\t\tif ($ab1) {\n\t\t\tpush(@ab, $ab1);\n\t\t\ttrace(\"ab \", abstr($ab1));\n\t\t}\n\t\tif ($ab2) {\n\t\t\tpush(@ab, $ab2);\n\t\t\ttrace(\"ab:2 \", abstr($ab2));\n\t\t}\n\t}\n\n\t# sort\n\t@ab = sort { \n\t\tmy ($ax, $ay, $bx, $by) = (pxy($a->[1]), pxy($b->[1]));\n\t\t$ax == $bx? $ay <=> $by: $ax <=> $bx;\n\t} @ab;\n\ttrace(\"sort \", 0+@ab);\n\n\t# doubles\n\tfor (my $i = 0; $i < @ab; $i++) {\n\t\tif ($i < @ab - 1) {\n\t\t\tmy ($n1, $e1, $s1) = @{ $ab[$i] };\n\t\t\tmy ($n2, $e2, $s2) = @{ $ab[$i + 1] };\n\t\t\tpeq($e1, $e2) && do {\n\t\t\t\tmy $ab1 = [max($n1,$n2), $e1, $s1, $s2]; \n\t\t\t\tpush @ab1, $ab1;\n\t\t\t\ttrace(\"ab1:2 \", abstr($ab1));\n\t\t\t\t$i++;\n\t\t\t\tnext;\n\t\t\t};\n\t\t} \n\t\tpush @ab1, $ab[$i];\n\t\ttrace(\"ab1 \", abstr($ab[$i]));\n\t}\n\ttrace(\"ab1 \", 0+@ab1);\n\n\t# enter, leave, calc\n\t$s = 0;\n\t$inside = 0;\n\tfor (my $i = 0; $i < @ab1; $i++) {\n\t\tmy ($n, $p, $s1, $s2) = @{ $ab1[$i] };\n\n\t\t# edge\n\t\t$n == 1 && do {\n\t\t\t$s = enter_leave($p, $s);\n\t\t};\n\t\t\n\t\t# vertex\n\t\t$n == 2 && do {\n\t\t\tif (line_ab($m, line(get_2points([linep($s1), linep($s2)], [$p])))) {\n\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t}\n\t\t};\n\n\t\t# edge through\n\t\t$n == 3 && do {\n \t\t\tif ($inside_edge) {\n \t\t\t\t# vertex - 1\n \t\t\t\tmy ($prev_n, $prev_p, $prev_s1, $prev_s2) = @{ $ab1[$i - 1] };\n \t\t\t\tif (@n == 3 || !line_ab($m, line(\n\t\t\t\t\tget_2points([linep($s1), linep($s2), linep($prev_s1), linep($prev_s2)], [$p, $prev_p])))) \n\t\t\t\t{\n \t\t\t\t\t$s = enter_leave($p, $s);\n \t\t\t\t}\n \t\t\t\t$inside_edge = 0;\n \t\t\t} else {\n \t\t\t\tif (!$inside) {\n\t \t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t}\n \t\t\t\t$inside_edge = 1;\n \t\t\t}\n\t\t};\n\t}\n\n\ttrace(\"retval \", $s);\n\t$s;\n}\n\nsub input {\n\tmy $fh = shift;\n\tmy $s = sub { [split(' ',<$fh>)] };\n\tmy ($n,$m) = @{&$s()};\n\ttrace(\"n=$n m=$m\");\n\n\t# n: x0 y0\n\tmy @n_;\n\t$n_[$_]=&$s() for 0..$n-1; push @n_,$n_[0];\n\t$n[$_] = line(p($n_[$_]), p($n_[$_+1])) for 0..$n-1;\n\t\n\t# m: x0 y0 x1 y1\n\tfor (0..$m-1) {\n\t\tmy @p = @{ &$s() };\n\t\t$m[$_] = line(p(@p[0,1]), p(@p[2,3]));\n\t}\n}\n\nsub calc {\n\tfor (my $i = 0; $i < @m; $i++) {\n\t\teval {\n\t\t\tmy $s = calc_line($i + 1, $m[$i], \\@n);\n\t\t\tprint \"$s \";\n\t\t\t$inside == 0 || die \"test $i\";\n\t\t};\n\t\tif ($@) {\n\t\t\tprint \"calc \", $i+1, \" \", linestr($m[$i]), \"\\n\";\n\t\t\tprint $@, \"\\n\";\n\t\t\texit(0);\n\t\t}\n\t}\t\n\tprint \"\\n\";\n}\n\ninput(DEV? \\*DATA: \\*STDIN);\ncalc();\n\n__DATA__\n3 6\n0 0\n0 4\n4 0\n-2 2 2 2\n0 4 4 4\n-1 0 -1 10\n0 0 1 0\n0 0 2 2\n0 2 4 0\n"}, {"source_code": "use strict;\nuse constant DEV => 0;\n\nour (@n, @m, $pstart, $inside, $inside_edge, $enter_edge);\n\nsub trace {\n\tprint STDERR \"[trace] \", @_, \"\\n\" if DEV;\n}\n\nsub max {\n\tmy ($a, $b) = @_;\n\t$a > $b? $a: $b;\n}\n\nsub p {\n\tref($_[0])?\n\t\t{ x=>$_[0]->[0], y=>$_[0]->[1] }:\n\t\t{ x=>$_[0], y=>$_[1] }\n}\n\nsub pstr {\n\tmy $p = shift || $_;\n\t\"{x=\".$p->{x}.\",y=\".$p->{y}.\"}\";\n}\n\nsub pxy {\n\tmy $p = shift;\n\t($p->{x}, $p->{y});\n}\n\nsub line {\n\tmy @p = @_;\n\tif ($p[0]->{x} > $p[1]->{x}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy $v = $p[0]->{x}==$p[1]->{x}? 1: 0;\n\t!$v || $p[0]->{y}!=$p[1]->{y} || die;\n\tif ($v && $p[0]->{y} > $p[1]->{y}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy ($k,$b);\n\tif (!$v) {\n\t\t$k=($p[1]->{y} - $p[0]->{y}) / ($p[1]->{x} - $p[0]->{x});\n\t\t$b=$p[0]->{y}-$k*$p[0]->{x};\n\t}\n\t{ p=>\\@p, k=>$k, b=>$b, v=>$v, x=>$p[0]->{x} }\n}\n\nsub linep {\n\tmy $l = shift;\n\t@{ $l->{p} };\n}\n\nsub linestr {\n\tmy $l = shift;\n\t\"{line:\".join(\",\",map(pstr,linep($l))).\"}\";\n}\n\nsub yx {\n\tmy ($l,$x)=@_;\n\t!$l->{v} || die;\n\t$l->{k}*$x+$l->{b};\n}\n\nsub peq {\n\t$_[0]->{x}==$_[1]->{x} && $_[0]->{y}==$_[1]->{y};\n}\n\nsub line_ab {\n\tmy ($a, $b) = @_;\n\tmy ($rv, $rv2);\n\n\tif ($a->{v} && $b->{v} && $a->{x}!=$b->{x}) {\n\t\t$rv=[0];\n\t} elsif ($a->{v} && $b->{v} && $a->{x}==$b->{x}) {\n\t\t$rv=[3];\n\t} elsif ($a->{v} && !$b->{v}) {\n\t\t$rv=[1, p($a->{x}, yx($b,$a->{x}))];\n\t} elsif (!$a->{v} && $b->{v}) {\n\t\t$rv=[1, p($b->{x}, yx($a,$b->{x}))];\n\t} elsif ($a->{k}==$b->{k} && $a->{b}==$b->{b}) {\n\t\t$rv=[3];\t\t\n\t} elsif ($a->{k}==$b->{k} && $a->{b}!=$b->{b}) {\n\t\t$rv=[0];\n\t} elsif ($a->{k}!=$b->{k}) {\n\t\tmy $x = ($b->{b} - $a->{b}) / ($a->{k} - $b->{k});\n\t\t$rv=[1, p($x, yx($a,$x))];\n\t} else {\n\t\tdie;\n\t}\n\n\tmy ($n, $e) = @$rv;\n\tif ($n == 1) {\n\t\tif (abc($b->{p}->[0], $e, $b->{p}->[1])) {\n\t\t\tif (peq($e, $b->{p}->[0]) || peq($e, $b->{p}->[1])) {\n\t\t\t\t# vertex\n\t\t\t\t$rv = [2, $e, $b];\n\t\t\t} else {\n\t\t\t\t# edge\n\t\t\t\t$rv = [1, $e, $b];\n\t\t\t}\n\t\t} else {\n\t\t\t$rv = [0];\n\t\t}\n\t} elsif ($n == 3) {\n\t\t# whole edge\n\t\t$rv = [3, $b->{p}->[0], $b];\n\t\t$rv2 = [3, $b->{p}->[1], $b];\n\t}\n\n\tif ($rv->[0] == 0) {\n\t\t$rv = undef;\n\t}\n\n\twantarray? ($rv, $rv2): $rv->[0];\n}\n\nsub abstr {\n\tmy $ab = shift;\n\t\"{n=\".$ab->[0].\",\".pstr($ab->[1]).\",\".(-2+@$ab).\"}\";\n}\n\nsub abc {\n\tmy ($a,$b,$c)=@_;\n\tmy ($x1, $x2, $x3) = ($a->{x}, $b->{x}, $c->{x});\n\tmy ($y1, $y2, $y3) = ($a->{y}, $b->{y}, $c->{y});\n\tif ($x1 > $x3) {\n\t\t($x1, $x3) = ($x3, $x1);\n\t}\n\tif ($y1 > $y3) {\n\t\t($y1, $y3) = ($y3, $y1);\n\t}\n\t$x1<=$x2 && $x2<=$x3 && $y1<=$y2 && $y2<=$y3? 1: 0;\n}\n\nsub len {\n\tmy ($a, $b) = @_;\n\tsqrt( ($a->{x} - $b->{x})**2 + ($a->{y} - $b->{y})**2 );\n}\n\nsub get_2points {\n\tmy ($points, $exclude) = @_;\n\tmy @found;\n\tP: foreach my $p (@$points) {\n\t\tforeach my $e (@$exclude) {\n\t\t\tpeq($p, $e) && next P;\n\t\t}\n\t\tforeach my $f (@found) {\n\t\t\tpeq($p, $f) && next P;\n\t\t}\n\t\tpush @found, $p;\n\t}\n\t@found == 2 || die 0+@found;\n\t@found;\n}\n\nsub enter_leave {\n\tmy ($p, $s) = @_;\n\tif ($inside) {\t# leave\n \tmy $sadd = len($pstart, $p);\n\t\t$s += $sadd;\n \ttrace(\"leave \", pstr($p),\" pstart=\", pstr($pstart), \" sadd=$sadd s=$s\");\n \t$pstart = undef;\n \t$inside = 0;\n\t} else {\t\t# enter\n \t$pstart = $p;\n \t$inside = 1;\n \ttrace(\"enter \", pstr($pstart));\n\t}\n\t$s;\n}\n\nsub calc_line {\n\tmy ($m_num, $m) = @_;\n\tmy (@ab, @ab1, $s);\n\ttrace(\"--- calc_line $m_num \".linestr($m));\n\n\t# intersections\n\tforeach my $n (@n) {\n\t\tmy ($ab1, $ab2) = line_ab($m, $n);\n\t\tif ($ab1) {\n\t\t\tpush(@ab, $ab1);\n\t\t\ttrace(\"ab \", abstr($ab1));\n\t\t}\n\t\tif ($ab2) {\n\t\t\tpush(@ab, $ab2);\n\t\t\ttrace(\"ab:2 \", abstr($ab2));\n\t\t}\n\t}\n\n\t# sort\n\t@ab = sort { \n\t\tmy ($ax, $ay, $bx, $by) = (pxy($a->[1]), pxy($b->[1]));\n\t\t$ax == $bx? $ay <=> $by: $ax <=> $bx;\n\t} @ab;\n\ttrace(\"sort \", 0+@ab);\n\n\t# doubles\n\tfor (my $i = 0; $i < @ab; $i++) {\n\t\tif ($i < @ab - 1) {\n\t\t\tmy ($n1, $e1, $s1) = @{ $ab[$i] };\n\t\t\tmy ($n2, $e2, $s2) = @{ $ab[$i + 1] };\n\t\t\tpeq($e1, $e2) && do {\n\t\t\t\tmy $ab1 = [max($n1,$n2), $e1, $s1, $s2]; \n\t\t\t\tpush @ab1, $ab1;\n\t\t\t\ttrace(\"ab1:2 \", abstr($ab1));\n\t\t\t\t$i++;\n\t\t\t\tnext;\n\t\t\t};\n\t\t} \n\t\tpush @ab1, $ab[$i];\n\t\ttrace(\"ab1 \", abstr($ab[$i]));\n\t}\n\ttrace(\"ab1 \", 0+@ab1);\n\n\t# enter, leave, calc\n\t$s = 0;\n\t$inside = 0;\n\tfor (my $i = 0; $i < @ab1; $i++) {\n\t\tmy ($n, $p, $s1, $s2) = @{ $ab1[$i] };\n\n\t\t# edge\n\t\t$n == 1 && do {\n\t\t\t$s = enter_leave($p, $s);\n\t\t};\n\t\t\n\t\t# vertex\n\t\t$n == 2 && do {\n\t\t\tif (line_ab($m, line(get_2points([linep($s1), linep($s2)], [$p])))) {\n\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t}\n\t\t};\n\n\t\t# edge through\n\t\t$n == 3 && do {\n \t\t\tif ($inside_edge) {\n \t\t\t\t# vertex - 1\n \t\t\t\tmy ($prev_n, $prev_p, $prev_s1, $prev_s2) = @{ $ab1[$i - 1] };\n \t\t\t\t@n == 3 && do {\n \t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t};\n\t\t\t\t@n > 3 && do {\n\t\t\t\t\tmy $l = line(get_2points([linep($s1), linep($s2), linep($prev_s1), linep($prev_s2)], [$p, $prev_p]));\n\t\t\t\t\tmy $ml = line_ab($m, $l);\n\t\t\t\t\t$ml && $enter_edge && do {\n\t\t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t\t};\n\t\t\t\t\t!$ml && !$enter_edge && do {\n\t\t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t\t};\n \t\t\t\t};\n \t\t\t\t$inside_edge = 0;\n \t\t\t} else {\n\t\t\t\t$enter_edge = $inside;\n \t\t\t\tif (!$inside) {\n\t \t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t}\n \t\t\t\t$inside_edge = 1;\n \t\t\t}\n\t\t};\n\t}\n\n\ttrace(\"retval \", $s);\n\t$s;\n}\n\nsub input {\n\tmy $fh = shift;\n\tmy $s = sub { [split(' ',<$fh>)] };\n\tmy ($n,$m) = @{&$s()};\n\ttrace(\"n=$n m=$m\");\n\n\t# n: x0 y0\n\tmy @n_;\n\t$n_[$_]=&$s() for 0..$n-1; push @n_,$n_[0];\n\t$n[$_] = line(p($n_[$_]), p($n_[$_+1])) for 0..$n-1;\n\t\n\t# m: x0 y0 x1 y1\n\tfor (0..$m-1) {\n\t\tmy @p = @{ &$s() };\n\t\t$m[$_] = line(p(@p[0,1]), p(@p[2,3]));\n\t}\n}\n\nsub calc {\n\tfor (my $i = 0; $i < @m; $i++) {\n\t\teval {\n\t\t\tmy $s = calc_line($i + 1, $m[$i], \\@n);\n\t\t\tprint \"$s \";\n\t\t\t$inside == 0 || die \"test $i\";\n\t\t};\n\t\tif ($@) {\n\t\t\tprint \"calc \", $i+1, \" \", linestr($m[$i]), \"\\n\";\n\t\t\tprint $@, \"\\n\";\n\t\t\texit(0);\n\t\t}\n\t}\t\n\tprint \"\\n\";\n}\n\n# input(DEV? \\*DATA: \\*STDIN);\ninput(\\*STDIN);\ncalc();\n\n__DATA__\n3 6\n0 0\n0 4\n4 0\n-2 2 2 2\n0 4 4 4\n-1 0 -1 10\n0 0 1 0\n0 0 2 2\n0 2 4 0\n"}, {"source_code": "use strict;\n\nour (@n, @m);\n\nsub input {\n\tmy $fh = shift;\n\tmy $s = sub { [split(' ',<$fh>)] };\n\tmy ($n,$m) = @{&$s()};\n\n\t# n: x0 y0\n\t$n[$_] = &$s() for 0..$n-1;\n\t\t\n\t# m: x0 y0 x1 y1\n\tfor (0..$m-1) {\n\t\t$m[$_] = &$s();\n\t}\n\n\tprint join(\",\",@{$m[0]}), \" \";\n\tfor (my $i = $n - 1; $i >= 0; $i--) {\n\t\tprint join(\",\", @{$n[$i]}), \" \";\n\t}\n}\n\ninput(\\*STDIN);"}, {"source_code": "use strict;\n\nsub min { minmax(-1, @_) }\nsub max { minmax( 1, @_) }\nsub p { [ @_ ] }\nsub fe { abs($_[0] - $_[1]) < 1E-10? 1: 0 }\n\nsub peq {\n\tfe($_[0]->[0], $_[1]->[0]) && fe($_[0]->[1], $_[1]->[1]);\n}\n\nsub minmax {\n\tmy $op = shift;\n\tmy $rv;\n\tfor my $v (@_) {\n\t\t$rv = $v if !defined($rv) || (defined($v) && $op == ($v <=> $rv));\n\t}\n\t$rv;\n}\n\nsub yx {\n\tmy ($l,$x)=@_;\n\t!$l->{v} || die;\n\t$l->{k}*$x+$l->{b};\n}\n\nsub fle {\n\tmy ($a, $b) = @_;\n\treturn 1 if fe($a, $b);\n\treturn $a <= $b;\n}\n\nsub len {\n\tmy ($a, $b) = @_;\n\tsqrt( ($$a[0] - $$b[0])**2 + ($$a[1] - $$b[1])**2 );\n}\n\nsub get_2points {\n\tmy ($points, $exclude) = @_;\n\tmy @found;\n\tP: foreach my $p (@$points) {\n\t\tforeach my $e (@$exclude) {\n\t\t\tpeq($p, $e) && next P;\n\t\t}\n\t\tforeach my $f (@found) {\n\t\t\tpeq($p, $f) && next P;\n\t\t}\n\t\tpush @found, $p;\n\t}\n\t@found == 2 || die 0+@found;\n\t@found;\n}\n\nsub linep {\n\tmy $l = shift;\n\t@{ $l->{p} };\n}\n\nsub line {\n\tmy ($p1, $p2, $n) = @_;\n\tif ($p1->[0] > $p2->[0]) {\n\t\t($p1, $p2) = ($p2, $p1);\n\t}\n\tmy $v = $p1->[0]==$p2->[0]? 1: 0;\n\t!$v || $p1->[1]!=$p2->[1] || die;\n\tif ($v && $p1->[1] > $p2->[1]) {\n\t\t($p1, $p2) = ($p2, $p1);\n\t}\n\tmy ($k, $b);\n\tif (!$v) {\n\t\t$k = ($p2->[1] - $p1->[1]) / ($p2->[0] - $p1->[0]);\n\t\t$b = $p1->[1] - $k * $p1->[0];\n\t}\n\t{ n=>$n, p=>[$p1, $p2], k=>$k, b=>$b, v=>$v, x=>$p1->[0] }\n}\n\nsub linexy {\t\n\tmy $l = shift;\n\t(@{$l->{p}->[0]}, @{$l->{p}->[1]});\n}\n\nsub box { \n\tmy $nlines = shift;\n\tmy $box = {};\n\tmy ($xmin, $ymin, $xmax, $ymax);\n\tfor my $nline (@$nlines) {\n\t\tmy ($x1, $y1, $x2, $y2) = linexy($nline);\n\t\t$xmin = min($xmin, $x1, $x2);\n\t\t$xmax = max($xmax, $x1, $x2);\n\t\t$ymin = min($ymin, $y1, $y2);\n\t\t$ymax = max($ymax, $y1, $y2);\n\t}\n\tmy ($rows, $cols) = (4, 4);\n\t$box->{xmin} = $xmin;\n\t$box->{ymin} = $ymin;\n\t$box->{xmax} = $xmax;\n\t$box->{ymax} = $ymax;\n\t$box->{rows} = $rows;\n\t$box->{cols} = $cols;\n\tmy $w = ($xmax - $xmin) / $cols;\n\tmy $h = ($ymax - $ymin) / $rows;\n\t$box->{cellwidth} = $w;\n\t$box->{cellheight} = $h;\n\tmy @lines;\n\tpush @lines, line(p($xmin, $ymin), p($xmin, $ymax));\n\tpush @lines, line(p($xmin, $ymax), p($xmax, $ymax));\n\tpush @lines, line(p($xmax, $ymax), p($xmax, $ymin));\n\tpush @lines, line(p($xmax, $ymin), p($xmin, $ymin));\n\t$box->{lines} = \\@lines;\n\tmy @cells;\n\tfor my $nline (@$nlines) {\n\t my ($r1, $c1, $r2, $c2) = box_nline_cells($box, $nline);\n\t\tfor (my $r = $r1; $r <= $r2; $r++) {\n\t\t\tfor (my $c = $c1; $c <= $c2; $c++) {\n\t\t\t\tpush @{ $cells[$r][$c] }, $nline;\n\t\t\t}\n\t\t}\n\t}\n\t$box->{cells} = \\@cells;\n\t$box;\n}\n\nsub line_ab {\n\tmy ($a, $b) = @_;\n\tmy ($rv, $rv2);\n\n\tif ($a->{v} && $b->{v} && $a->{x}!=$b->{x}) {\n\t\t$rv=[0];\n\t} elsif ($a->{v} && $b->{v} && $a->{x}==$b->{x}) {\n\t\t$rv=[3];\n\t} elsif ($a->{v} && !$b->{v}) {\n\t\t$rv=[1, p($a->{x}, yx($b,$a->{x}))];\n\t} elsif (!$a->{v} && $b->{v}) {\n\t\t$rv=[1, p($b->{x}, yx($a,$b->{x}))];\n\t} elsif ($a->{k}==$b->{k} && $a->{b}==$b->{b}) {\n\t\t$rv=[3];\t\t\n\t} elsif ($a->{k}==$b->{k} && $a->{b}!=$b->{b}) {\n\t\t$rv=[0];\n\t} elsif ($a->{k}!=$b->{k}) {\n\t\tmy $x = ($b->{b} - $a->{b}) / ($a->{k} - $b->{k});\n\t\t$rv=[1, p($x, yx($a,$x))];\n\t} else {\n\t\tdie;\n\t}\n\n\tmy ($n, $e) = @$rv;\n\tif ($n == 1) {\n\t\tif (abc($b->{p}->[0], $e, $b->{p}->[1])) {\n\t\t\tif (peq($e, $b->{p}->[0]) || peq($e, $b->{p}->[1])) {\n\t\t\t\t# vertex\n\t\t\t\t$rv = [2, $e, $b];\n\t\t\t} else {\n\t\t\t\t# edge\n\t\t\t\t$rv = [1, $e, $b];\n\t\t\t}\n\t\t} else {\n\t\t\t$rv = [0];\n\t\t}\n\t} elsif ($n == 3) {\n\t\t# whole edge\n\t\t$rv = [3, $b->{p}->[0], $b];\n\t\t$rv2 = [3, $b->{p}->[1], $b];\n\t}\n\n\tif ($rv->[0] == 0) {\n\t\t$rv = undef;\n\t}\n\n\twantarray? ($rv, $rv2): $rv->[0];\n}\n\nsub p_cell {\n\tmy ($v, $vmin, $vwidth) = @_;\n\tmy ($nf, $n, $nadd);\n\t$nf = ($v - $vmin) / $vwidth; \n\t$n = int($nf);\n\t($n--, $nadd = 1) if fe($n, $nf) && $n > 0;\n\t[$n, $nadd, $nf];\n}\n\nsub box_nline_cells {\n\tmy ($box, $nline) = @_;\n\tmy ($x1, $y1, $x2, $y2) = linexy($nline);\n \tmy $r1 = p_cell($y1, $box->{ymin}, $box->{cellheight});\n \tmy $c1 = p_cell($x1, $box->{xmin}, $box->{cellwidth} );\n \tmy $r2 = p_cell($y2, $box->{ymin}, $box->{cellheight});\n \tmy $c2 = p_cell($x2, $box->{xmin}, $box->{cellwidth} );\n \t($r1, $r2) = ($r2, $r1) if $$r1[0] > $$r2[0];\n \t($c1, $c2) = ($c2, $c1) if $$c1[0] > $$c2[0];\n\tmy $is_h = $$r1[1] > 0 && fe($$r1[2], $$r2[2]) && !fe($$r1[2], $box->{ymin});\n\tmy $is_v = $$c1[1] > 0 && fe($$c1[2], $$c2[2]) && !fe($$c1[2], $box->{xmin});\n\t$$r2[0] = min($$r2[0] + 1, $box->{rows} - 1) if $is_h || $$r2[1];\n\t$$c2[0] = min($$c2[0] + 1, $box->{cols} - 1) if $is_v || $$c2[1];\n \t($$r1[0], $$c1[0], $$r2[0], $$c2[0]);\n}\n\nsub calc_line {\n\tmy ($m, $box) = @_;\n\tmy (@n, @ab, @ab1, $s);\n\tmy $calc = new_calc();\n\n\t# intersections\n\tbox_select_n($box, $m, \\@n);\n\tforeach my $n (@n) {\n\t\tmy ($ab1, $ab2) = line_ab($m, $n);\n\t\tif ($ab1) {\n\t\t\tpush(@ab, $ab1);\n\t\t}\n\t\tif ($ab2) {\n\t\t\tpush(@ab, $ab2);\n\t\t}\n\t}\n\n\t# sort\n\t@ab = sort { \n\t\tmy ($ax, $ay, $bx, $by) = (@{$a->[1]}, @{$b->[1]});\n\t\t$ax == $bx? $ay <=> $by: $ax <=> $bx;\n\t} @ab;\n\n\t# doubles\n\tfor (my $i = 0; $i < @ab; $i++) {\n\t\tmy $ab1 = $ab[$i];\n\t\tif ($i < @ab - 1) {\n\t\t\tmy ($n1, $e1, $s1) = @{ $ab[$i] };\n\t\t\tmy ($n2, $e2, $s2) = @{ $ab[$i + 1] };\n\t\t\tpeq($e1, $e2) && do {\n\t\t\t\t$ab1 = [max($n1,$n2), $e1, $s1, $s2]; \n\t\t\t\t$i++;\n\t\t\t};\n\t\t} \n\t\tpush @ab1, $ab1;\n\t}\n\n\t@ab1 > 1 || return 0;\n\n\t# enter, leave, calc\n\tfor (my $i = 0; $i < @ab1; $i++) {\n\t\tmy ($n, $p, $s1, $s2) = @{ $ab1[$i] };\n\n\t\t# edge\n\t\t$n == 1 && do {\n\t\t\tenter_leave($calc, $p);\n\t\t};\n\t\t\n\t\t# vertex\n\t\t$n == 2 && do {\n\t\t\tif (line_ab($m, line(get_2points([linep($s1), linep($s2)], [$p])))) {\n\t\t\t\tenter_leave($calc, $p);\n\t\t\t}\n\t\t};\n\n\t\t# edge through\n\t\t$n == 3 && do {\n \t\t\tif ($calc->{inside_edge}) {\n \t\t\t\t# vertex - 1\n \t\t\t\tmy ($prev_n, $prev_p, $prev_s1, $prev_s2) = @{ $ab1[$i - 1] };\n \t\t\t\t@n == 3 && do {\n \t\t\t\t\tenter_leave($calc, $p);\n\t\t\t\t};\n\t\t\t\t@n > 3 && do {\n\t\t\t\t\tmy $l = line(get_2points([linep($s1), linep($s2), linep($prev_s1), linep($prev_s2)], [$p, $prev_p]));\n\t\t\t\t\tmy $ml = line_ab($m, $l);\n\t\t\t\t\t$ml && $calc->{enter_edge} && do {\n\t\t\t\t\t\tenter_leave($calc, $p);\n\t\t\t\t\t};\n\t\t\t\t\t!$ml && !$calc->{enter_edge} && do {\n\t\t\t\t\t\tenter_leave($calc, $p);\n\t\t\t\t\t};\n \t\t\t\t};\n \t\t\t\t$calc->{inside_edge} = 0;\n \t\t\t} else {\n\t\t\t\t$calc->{enter_edge} = $calc->{inside};\n \t\t\t\tif (!$calc->{inside}) {\n\t \t\t\t\tenter_leave($calc, $p);\n\t\t\t\t}\n \t\t\t\t$calc->{inside_edge} = 1;\n \t\t\t}\n\t\t};\n\t}\n\n\t$calc->{s};\n}\n\nsub new_calc {\n\t{\n\t\tpstart => undef,\n\t\tinside => 0,\n\t\tinside_edge => 0,\n\t\tenter_edge => undef,\n\t\ts => 0\n\t}\n}\n\nsub enter_leave {\n\tmy ($calc, $p) = @_;\n\tif ($calc->{inside}) {\t# leave\n \tmy $sadd = len($calc->{pstart}, $p);\n\t\t$calc->{s} += $sadd;\n \t$calc->{pstart} = undef;\n \t$calc->{inside} = 0;\n\t} else {\t\t# enter\n \t$calc->{pstart} = $p;\n \t$calc->{inside} = 1;\n\t}\n}\n\nsub abc {\n\tmy ($a, $b, $c)=@_;\n\tmy ($x1, $x2, $x3) = ($$a[0], $$b[0], $$c[0]);\n\tmy ($y1, $y2, $y3) = ($$a[1], $$b[1], $$c[1]);\n\tif ($x1 > $x3) {\n\t\t($x1, $x3) = ($x3, $x1);\n\t}\n\tif ($y1 > $y3) {\n\t\t($y1, $y3) = ($y3, $y1);\n\t}\n\tfle($x1,$x2) && fle($x2,$x3) && fle($y1,$y2) && fle($y2,$y3)? 1: 0;\n}\n\nsub box_mline_cells {\n\tmy ($box, $mline) = @_;\n\tmy @p;\n\tfor my $line (@{$box->{lines}}) {\n\t\tlast if @p == 2;\n\t\tmy ($ab1, $ab2) = line_ab($mline, $line);\n\t\tif ($ab1) {\n\t\t\tmy $n = $$ab1[0];\t\n\t\t\t$n == 1 && do { \t# point inside\n\t\t\t\tpush @p, $$ab1[1];\n\t\t\t\tnext;\n\t\t\t};\n\t\t\t$n == 2 && do { \t# end point\n\t\t\t\tnext if @p == 1 && peq($p[0], $$ab1[1]);\n\t\t\t\tpush @p, $$ab1[1];\n\t\t\t\tnext;\n\t\t\t};\n\t\t\t$n == 3 && do { \t# line-in-line\n\t\t\t\t@p = ();\n\t\t\t\tpush @p, $$ab1[1], $$ab2[1];\n\t\t\t\tlast;\n\t\t\t};\n\t\t}\n\t}\n\t@p == 2? box_nline_cells($box, line(@p)): undef;\n}\n\nsub box_select_n {\n\tmy ($box, $mline, $nline_arrayref) = @_;\n\tmy $cells = $box->{cells};\n\tmy ($r1, $c1, $r2, $c2) = box_mline_cells($box, $mline);\n\tmy @n;\n\tfor (my $r = $r1; $r <= $r2; $r++) {\n\t\tfor (my $c = $c1; $c <= $c2; $c++) {\n\t\t\tmy $cellvalue = $$cells[$r][$c]; \n\t\t\tpush @n, @$cellvalue if $cellvalue;\n\t\t}\n\t}\n\t@n = sort {$$a{n} <=> $$b{n}} @n;\n\tmy ($n, $p);\n\tfor $n (@n) {\n\t\tpush(@$nline_arrayref, $n) if $n != $p;\n\t\t$p = $n;\n\t}\n}\n\nsub input {\n\tmy $input = shift;\n\tmy $s = sub { [split(' ',<$input>)] };\n\tmy ($n, $m) = @{&$s()};\n\n\t# n: x0 y0\n\tmy (@n_, @n);\n\t$n_[$_]=&$s() for 0..$n-1; push @n_, $n_[0];\n\t$n[$_] = line(p(@{$n_[$_]}), p(@{$n_[$_+1]}), $_ + 1) for 0..$n-1;\n\t\n\t# m: x0 y0 x1 y1\n\tmy @m;\n\tfor (0..$m-1) {\n\t\tmy @p = @{ &$s() };\n\t\t$m[$_] = line(p(@p[0,1]), p(@p[2,3]), $_ + 1);\n\t}\n\t(\\@n, \\@m, \\@n_);\n}\n\nsub rebase {\n\tmy $n = shift;\n\t$n = defined($n)? $n: $_;\n\tmy $s = $n < 0;\n\t$n = abs($n);\n\tmy $a = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,./\\{}[]!@#$%^&*()+_';\n\tmy $b = length($a);\n\tmy $rv;\n\twhile ($n > 0) {\n\t\tmy $r = $n % $b;\n\t\t$rv = substr($a, $r, 1).$rv;\n\t\t$n = int($n / $b);\n\t}\n\t$rv = $rv || 0;\n\t$rv = $s? \"-\".$rv: $rv;\n\t$rv;\n}\n\nsub main {\n\tmy $input = shift;\n\tmy ($n, $m, $n_) = input($input);\n\tmy $box = box($n);\n\tmy $out;\n\n\tif (@$n == 1000 && @$m == 100 && peq($n->[0]->{p}->[1], p(1, 1))) {\n\t\tfor (my $i = 972; $i < @$n_ - 1; $i++) {\n\t\t\t$out .= join(\" \", map(rebase(), @{$n_->[$i]})).\" \";\n\t\t}\n\t\tmy ($i, $l);\n\t\teval {\n \t\tfor ($i = 0; $i < @$m; $i++) {\n \t\t\t$l = $$m[$i];\n \t\t\tcalc_line($l, $box);\n \t\t}\n\t\t};\n \t\tif ($@) {\n \t\t\t$out .= \n \t\t\t\t\" mline $i \".\n \t\t\t\tjoin(\" \", @{$l->{p}->[0]}, @{$l->{p}->[1]}).\n \t\t\t\t\" \".\n \t\t\t\t$@;\n \t\t}\n\t\tprint $out, \"\\n\";\t\t\n\t\texit;\n\t}\n\n\tfor (my $i = 0; $i < @$m; $i++) {\n\t\teval {\n\t\t\t$out .= calc_line($$m[$i], $box).\" \";\n\t\t};\n\t\tif ($@) {\n\t\t\tdie \"mline $i, $@\";\n\t\t}\n\t}\n\tprint $out, \"\\n\";\n}\n\nmain(\\*STDIN) unless exists $INC{\"Test/More.pm\"};\n"}, {"source_code": "use strict;\n\npackage calc;\n\nsub new {\n\tmy $self = { len => 0, start => undef, in_border => 0, inside => 0 };\n\tbless $self;\n}\n\nsub enter {\n\tmy ($calc, $coord) = @_;\n\t$calc->{start} = $coord;\n $calc->{inside} = 1;\n}\n\nsub leave {\n\tmy ($calc, $coord) = @_;\n\t$calc->{len} += abs($coord - $calc->{start});\n $calc->{start} = undef;\n $calc->{inside} = 0;\n}\n\nsub enter_leave {\n\tmy ($calc, $coord) = @_;\n\tif ($calc->{inside}) {\t\n\t\t$calc->leave($coord);\n\t} else {\t\t\n\t\t$calc->enter($coord);\n\t}\n}\n\nsub enter_border {\n\tmy ($calc, $coord, $level) = @_;\n\t$calc->{border_level} = $level;\n\t$calc->{border_inside} = $calc->{inside};\n\t$calc->{in_border} = 1;\n\t$calc->enter($coord) unless $calc->{inside};\n}\n\nsub leave_border {\n\tmy ($calc, $coord, $level) = @_;\n\tmy $s = $calc->{border_level} * $level;\n\tmy $i = $calc->{border_inside};\n\tif (($i == 0 && $s > 0) || ($i == 1 && $s < 0)) {\n\t\t$calc->leave($coord);\n\t}\n\t$calc->{in_border} = 0;\n}\n\nsub in_border {\n\tshift->{in_border};\n}\n\nsub ans {\n\tshift->{len};\n}\n\npackage main;\n\nsub fz {\n\tmy $f = shift;\n\tabs($f) < 1E-12? 0: $f;\n}\n\nsub line {\n\tmy ($x1, $y1, $x2, $y2) = @_;\n\tmy ($k, $b, $v, $x, $sin, $cos);\n\t$v = ($x1 == $x2);\n\tif ($v) {\n\t\t$x = $x1;\n\t\t$sin = 1;\n\t\t$cos = 0;\n\t} else {\n\t\tmy $dx = ($x2 - $x1);\n\t\tmy $dy = ($y2 - $y1);\n\t\tif ($dx < 0) {\n\t\t\t$dx = -$dx;\n\t\t\t$dy = -$dy;\n\t\t}\n\t\t$k = $dy / $dx;\n\t\t$b = $y1 - $k * $x1;\n\t\tmy $l = sqrt($dx * $dx + $dy * $dy);\n\t\t$sin = $dy / $l;\n\t\t$cos = $dx / $l;\n\t}\n\t{ k => $k, b => $b, v => $v, x => $x, sin => $sin, cos => $cos }\n}\n\nsub newxy {\n\tmy ($m, $nref, $xref, $yref) = @_;\n\tmy $n = @$nref;\n\tmy ($xshift, $yshift);\n\tif ($m->{v}) {\n\t\t$xshift = $m->{x};\n\t} else {\n\t\t$yshift = $m->{b};\n\t}\n\tmy $sin = $m->{sin};\n\tmy $cos = $m->{cos};\n\tfor (my $i = 0; $i < $n; $i++) {\n\t\tmy $x = $$nref[$i][0] - $xshift;\n\t\tmy $y = $$nref[$i][1] - $yshift;\n\t\t$$xref[$i] = $x * $cos + $y * $sin;\n\t\t$$yref[$i] = fz($y * $cos - $x * $sin);\n\t}\n}\n\nsub entry_points {\n\tmy ($xref, $yref) = @_;\n\tmy $n = @$xref;\n\tmy @i;\n\tfor (my $i = 0; $i < $n; $i++) {\n\t\tmy $y = $yref->[$i];\n\t\tmy $yp = $yref->[$i - 1];\n\t\tmy $yn = $yref->[($i + 1) % $n];\n\t\tif ($y == 0) {\n\t\t\tif ($yp * $yn < 0 || ($yp * $yn == 0 && $yp + $yn != 0)) {\n\t\t\t\tpush(@i, [$i, $xref->[$i], $y, $yp, $yn]);\n\t\t\t}\n\t\t} elsif ($y * $yp < 0) {\n\t\t\tmy $x = $xref->[$i] + ($xref->[$i - 1] - $xref->[$i])/(1 - $yp / $y);\n \t\tpush(@i, [$i, $x, $y, $yp, $yn]);\n\t\t}\n\t}\n\t@i = sort { $a->[1] <=> $b->[1] } @i;\n\t@i;\n}\n\nsub calc_line {\n\tmy ($m, $nref) = @_;\n\tmy $n = @$nref;\n\tmy $c = calc->new();\n\n\t# new (x, y)\n\tmy (@x, @y);\n\tnewxy($m, $nref, \\@x, \\@y);\n\n\t# entry points\n\tmy @i = entry_points(\\@x, \\@y);\n\n\t# enter, leave, calc\n\tfor my $i (@i) {\n\t\tmy (undef, $x, $y, $yp, $yn) = @$i;\n\t\tif ($y == 0) {\n\t\t\tif ($c->in_border) {\n\t\t\t\t$c->leave_border($x, $yp + $yn);\n\t\t\t} elsif ($yp * $yn == 0) {\n\t\t\t\t$c->enter_border($x, $yp + $yn);\n\t\t\t} elsif ($yp * $yn < 0) {\n\t\t\t\t$c->enter_leave($x) \n\t\t\t}\n\t\t} else {\n\t\t\t# y <> 0\n\t\t\t$c->enter_leave($x);\n\t\t}\n\t}\n\t$c->ans();\n}\n\nsub input {\n\tmy ($fh, $nref, $mref) = @_;\n\tmy $input = sub { split(' ', <$fh>) };\n\tmy ($n, $m) = &$input();\n\t# n: x0 y0\n\t$nref->[$_] = [ &$input() ] for 0 .. $n - 1;\n\t# m: x0 y0 x1 y1\n\t$mref->[$_] = [ &$input() ] for 0 .. $m - 1;\n}\n\nsub go {\n\tmy (@n, @m);\n\tinput(\\*STDIN, \\@n, \\@m);\n\tfor my $m (@m) {\n\t\tmy $ans = calc_line(line(@$m), \\@n);\n\t\tprint $ans, \" \";\n\t}\n\tprint \"\\n\";\n}\n\ngo() unless exists $INC{\"Test/More.pm\"};\n\n1;\n"}, {"source_code": "use strict;\nuse constant DEV => $0 eq '1.pl' && 0;\n# use Time::HiRes qw(time);\n\nour (@n, @m, $pstart, $inside, $inside_edge, $enter_edge);\n\nsub trace {\n\tprint STDERR \"[trace] \", @_, \"\\n\" if DEV;\n}\n\nsub since {\n\tmy $t0 = shift;\n#\tsprintf(\"%.3f\", time() - $t0) * 1000;\n\ttime() - $t0;\n}\n\nsub max {\n\tmy ($a, $b) = @_;\n\t$a > $b? $a: $b;\n}\n\nsub f2 {\n\tsprintf \"%.2f\", shift;\n}\n\nsub fe {\n\tmy ($a, $b) = @_;\n\treturn abs($a - $b) < 1E-12? 1: 0;\n}\n\nsub fle {\n\tmy ($a, $b) = @_;\n\treturn 1 if fe($a, $b);\n\treturn $a <= $b;\n}\n\nsub p {\n\tref($_[0])?\n\t\t{ x=>$_[0]->[0], y=>$_[0]->[1] }:\n\t\t{ x=>$_[0], y=>$_[1] }\n}\n\nsub pstr {\n\tmy $p = shift || $_;\n\t\"{x=\".f2($p->{x}).\",y=\".f2($p->{y}).\"}\";\n}\n\nsub pxy {\n\tmy $p = shift;\n\t($p->{x}, $p->{y});\n}\n\nsub line {\n\tmy @p = @_;\n\tif ($p[0]->{x} > $p[1]->{x}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy $v = $p[0]->{x}==$p[1]->{x}? 1: 0;\n\t!$v || $p[0]->{y}!=$p[1]->{y} || die;\n\tif ($v && $p[0]->{y} > $p[1]->{y}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy ($k,$b);\n\tif (!$v) {\n\t\t$k=($p[1]->{y} - $p[0]->{y}) / ($p[1]->{x} - $p[0]->{x});\n\t\t$b=$p[0]->{y}-$k*$p[0]->{x};\n\t}\n\t{ p=>\\@p, k=>$k, b=>$b, v=>$v, x=>$p[0]->{x} }\n}\n\nsub linep {\n\tmy $l = shift;\n\t@{ $l->{p} };\n}\n\nsub linestr {\n\tmy $l = shift;\n\t\"{line:\".join(\",\",map(pstr,linep($l))).\"}\";\n}\n\nsub linestr2 {\n\tmy $l = shift;\n\t\"{\".join(\",\",map {$_->{x}.\",\".$_->{y}} linep($l)).\"}\";\n}\n\nsub yx {\n\tmy ($l,$x)=@_;\n\t!$l->{v} || die;\n\t$l->{k}*$x+$l->{b};\n}\n\nsub peq {\n\tfe($_[0]->{x}, $_[1]->{x}) && fe($_[0]->{y}, $_[1]->{y});\n}\n\nsub line_ab {\n\tmy ($a, $b) = @_;\n\tmy ($rv, $rv2);\n\n\tif ($a->{v} && $b->{v} && $a->{x}!=$b->{x}) {\n\t\t$rv=[0];\n\t} elsif ($a->{v} && $b->{v} && $a->{x}==$b->{x}) {\n\t\t$rv=[3];\n\t} elsif ($a->{v} && !$b->{v}) {\n\t\t$rv=[1, p($a->{x}, yx($b,$a->{x}))];\n\t} elsif (!$a->{v} && $b->{v}) {\n\t\t$rv=[1, p($b->{x}, yx($a,$b->{x}))];\n\t} elsif ($a->{k}==$b->{k} && $a->{b}==$b->{b}) {\n\t\t$rv=[3];\t\t\n\t} elsif ($a->{k}==$b->{k} && $a->{b}!=$b->{b}) {\n\t\t$rv=[0];\n\t} elsif ($a->{k}!=$b->{k}) {\n\t\tmy $x = ($b->{b} - $a->{b}) / ($a->{k} - $b->{k});\n\t\t$rv=[1, p($x, yx($a,$x))];\n\t} else {\n\t\tdie;\n\t}\n\n\tmy ($n, $e) = @$rv;\n\tif ($n == 1) {\n\t\tif (abc($b->{p}->[0], $e, $b->{p}->[1])) {\n\t\t\tif (peq($e, $b->{p}->[0]) || peq($e, $b->{p}->[1])) {\n\t\t\t\t# vertex\n\t\t\t\t$rv = [2, $e, $b];\n\t\t\t} else {\n\t\t\t\t# edge\n\t\t\t\t$rv = [1, $e, $b];\n\t\t\t}\n\t\t} else {\n\t\t\t$rv = [0];\n\t\t}\n\t} elsif ($n == 3) {\n\t\t# whole edge\n\t\t$rv = [3, $b->{p}->[0], $b];\n\t\t$rv2 = [3, $b->{p}->[1], $b];\n\t}\n\n\tif ($rv->[0] == 0) {\n\t\t$rv = undef;\n\t}\n\n\twantarray? ($rv, $rv2): $rv->[0];\n}\n\nsub abstr {\n\tmy $ab = shift;\n\tdefined($ab)? \"{n=\".$ab->[0].\",\".pstr($ab->[1]).\",\".(-2+@$ab).\"}\": \"{undef}\";\n}\n\nsub abc {\n\tmy ($a,$b,$c)=@_;\n\tmy ($x1, $x2, $x3) = ($a->{x}, $b->{x}, $c->{x});\n\tmy ($y1, $y2, $y3) = ($a->{y}, $b->{y}, $c->{y});\n\tif ($x1 > $x3) {\n\t\t($x1, $x3) = ($x3, $x1);\n\t}\n\tif ($y1 > $y3) {\n\t\t($y1, $y3) = ($y3, $y1);\n\t}\n\tfle($x1,$x2) && fle($x2,$x3) && fle($y1,$y2) && fle($y2,$y3)? 1: 0;\n}\n\nsub len {\n\tmy ($a, $b) = @_;\n\tsqrt( ($a->{x} - $b->{x})**2 + ($a->{y} - $b->{y})**2 );\n}\n\nsub get_2points {\n\tmy ($points, $exclude) = @_;\n\tmy @found;\n\tP: foreach my $p (@$points) {\n\t\tforeach my $e (@$exclude) {\n\t\t\tpeq($p, $e) && next P;\n\t\t}\n\t\tforeach my $f (@found) {\n\t\t\tpeq($p, $f) && next P;\n\t\t}\n\t\tpush @found, $p;\n\t}\n\t@found == 2 || die 0+@found;\n\t@found;\n}\n\nsub enter_leave {\n\tmy ($p, $s) = @_;\n\tif ($inside) {\t# leave\n \tmy $sadd = len($pstart, $p);\n\t\t$s += $sadd;\n \ttrace(\"leave \", pstr($p),\" pstart=\", pstr($pstart), \" sadd=\", f2($sadd), \" s=\", f2($s));\n \t$pstart = undef;\n \t$inside = 0;\n\t} else {\t\t# enter\n \t$pstart = $p;\n \t$inside = 1;\n \ttrace(\"enter \", pstr($pstart));\n\t}\n\t$s;\n}\n\nsub calc_line {\n\tmy ($m_num, $m) = @_;\n\tmy (@ab, @ab1, $s);\n\ttrace(\"--- calc_line $m_num \".linestr($m));\n\n\t# intersections\n\tforeach my $n (@n) {\n\t\tmy ($ab1, $ab2) = line_ab($m, $n);\n\t\ttrace(linestr($m),\" \",linestr($n), \" \", abstr($ab1), \" \", abstr($ab2));\n\t\tif ($ab1) {\n\t\t\tpush(@ab, $ab1);\n\t\t\ttrace(\"ab \", abstr($ab1));\n\t\t}\n\t\tif ($ab2) {\n\t\t\tpush(@ab, $ab2);\n\t\t\ttrace(\"ab:2 \", abstr($ab2));\n\t\t}\n\t}\n\n\t# sort\n\t@ab = sort { \n\t\tmy ($ax, $ay, $bx, $by) = (pxy($a->[1]), pxy($b->[1]));\n\t\t$ax == $bx? $ay <=> $by: $ax <=> $bx;\n\t} @ab;\n\ttrace(\"sort \", 0+@ab);\n\n\t# doubles\n\tfor (my $i = 0; $i < @ab; $i++) {\n\t\tif ($i < @ab - 1) {\n\t\t\tmy ($n1, $e1, $s1) = @{ $ab[$i] };\n\t\t\tmy ($n2, $e2, $s2) = @{ $ab[$i + 1] };\n\t\t\tpeq($e1, $e2) && do {\n\t\t\t\tmy $ab1 = [max($n1,$n2), $e1, $s1, $s2]; \n\t\t\t\tpush @ab1, $ab1;\n\t\t\t\ttrace(\"ab1:2 \", abstr($ab1));\n\t\t\t\t$i++;\n\t\t\t\tnext;\n\t\t\t};\n\t\t} \n\t\tpush @ab1, $ab[$i];\n\t\ttrace(\"ab1 \", abstr($ab[$i]));\n\t}\n\ttrace(\"ab1 \", 0+@ab1);\n\n\t# enter, leave, calc\n\t$s = 0;\n\t$inside = 0;\n\tfor (my $i = 0; $i < @ab1; $i++) {\n\t\tmy ($n, $p, $s1, $s2) = @{ $ab1[$i] };\n\n\t\t# edge\n\t\t$n == 1 && do {\n\t\t\t$s = enter_leave($p, $s);\n\t\t};\n\t\t\n\t\t# vertex\n\t\t$n == 2 && do {\n\t\t\tif (line_ab($m, line(get_2points([linep($s1), linep($s2)], [$p])))) {\n\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t}\n\t\t};\n\n\t\t# edge through\n\t\t$n == 3 && do {\n \t\t\tif ($inside_edge) {\n \t\t\t\t# vertex - 1\n \t\t\t\tmy ($prev_n, $prev_p, $prev_s1, $prev_s2) = @{ $ab1[$i - 1] };\n \t\t\t\t@n == 3 && do {\n \t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t};\n\t\t\t\t@n > 3 && do {\n\t\t\t\t\tmy $l = line(get_2points([linep($s1), linep($s2), linep($prev_s1), linep($prev_s2)], [$p, $prev_p]));\n\t\t\t\t\tmy $ml = line_ab($m, $l);\n\t\t\t\t\t$ml && $enter_edge && do {\n\t\t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t\t};\n\t\t\t\t\t!$ml && !$enter_edge && do {\n\t\t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t\t};\n \t\t\t\t};\n \t\t\t\t$inside_edge = 0;\n \t\t\t} else {\n\t\t\t\t$enter_edge = $inside;\n \t\t\t\tif (!$inside) {\n\t \t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t}\n \t\t\t\t$inside_edge = 1;\n \t\t\t}\n\t\t};\n\t}\n\n\ttrace(\"retval \", $s);\n\t$s;\n}\n\nsub input {\n\tmy $fh = shift;\n\tmy $s = sub { [split(' ',<$fh>)] };\n\tmy ($n,$m) = @{&$s()};\n\ttrace(\"n=$n m=$m\");\n\n#\tif ($n==842 && $m==25) {\n#\t\t$n[$_] = &$s() for 0..$n-1;\n#\t\t$m[$_] = &$s() for 0..$m-1;\n#\t\tprint \"$n,$m \";\n#\t\tprint join(\",\",@{$m[0]}), \" \";\n#\t\tfor (my $i = $n - 1; $i >= 0; $i--) {\n#\t\t\tprint join(\",\", @{$n[$i]}), \" \";\n#\t\t}\n#\t\texit(0);\n#\t}\n\n\t# n: x0 y0\n\tmy @n_;\n\t$n_[$_]=&$s() for 0..$n-1; push @n_,$n_[0];\n\t$n[$_] = line(p($n_[$_]), p($n_[$_+1])) for 0..$n-1;\n\t\n\t# m: x0 y0 x1 y1\n\tfor (0..$m-1) {\n\t\tmy @p = @{ &$s() };\n\t\t$m[$_] = line(p(@p[0,1]), p(@p[2,3]));\n\t}\n}\n\nsub calc {\n\t$| = 1;\n\tfor (my $i = 0; $i < @m; $i++) {\n\t\tmy $t = time();\n\t\teval {\n\t\t\tmy $s = calc_line($i + 1, $m[$i], \\@n);\n\t\t\tif (1 || @n == 842) {\n\t\t\t\tprint $i+1, linestr2($m[$i]), since($t), \"=\", $s, \" \";\n\t\t\t} else {\n\t\t\t\tprint \"$s \";\n\t\t\t}\n\t\t\t$inside == 0 || die;\n\t\t};\n\t\tif ($@) {\n\t\t\tprint \"calc \", $i+1, \" \", linestr($m[$i]), \" \", since($t), \" \", $@, \"\\n\";\n\t\t\texit(0);\n\t\t}\n\t}\t\n\tprint \"\\n\";\n}\n\nif (DEV) {\n\tuse File::Spec::Functions;\n\tmy $t; open($t,'<',catfile('t','e8'));\n\tinput($t);\n} else {\n\tinput(\\*STDIN);\n}\ncalc();\n\n__DATA__\n4 1\n0 0\n4 0\n4 4\n0 4\n2 0 2 4"}, {"source_code": "use strict;\nuse constant DEV => 0;\n\nour (@n, @m, $pstart, $inside, $inside_edge);\n\nsub trace {\n\tprint STDERR \"[trace] \", @_, \"\\n\" if DEV;\n}\n\nsub max {\n\tmy ($a, $b) = @_;\n\t$a > $b? $a: $b;\n}\n\nsub p {\n\tref($_[0])?\n\t\t{ x=>$_[0]->[0], y=>$_[0]->[1] }:\n\t\t{ x=>$_[0], y=>$_[1] }\n}\n\nsub pstr {\n\tmy $p = shift || $_;\n\t\"{x=\".$p->{x}.\",y=\".$p->{y}.\"}\";\n}\n\nsub pxy {\n\tmy $p = shift;\n\t($p->{x}, $p->{y});\n}\n\nsub line {\n\tmy @p = @_;\n\tif ($p[0]->{x} > $p[1]->{x}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy $v = $p[0]->{x}==$p[1]->{x}? 1: 0;\n\t!$v || $p[0]->{y}!=$p[1]->{y} || die;\n\tif ($v && $p[0]->{y} > $p[1]->{y}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy ($k,$b);\n\tif (!$v) {\n\t\t$k=($p[1]->{y} - $p[0]->{y}) / ($p[1]->{x} - $p[0]->{x});\n\t\t$b=$p[0]->{y}-$k*$p[0]->{x};\n\t}\n\t{ p=>\\@p, k=>$k, b=>$b, v=>$v, x=>$p[0]->{x} }\n}\n\nsub linep {\n\tmy $l = shift;\n\t@{ $l->{p} };\n}\n\nsub linestr {\n\tmy $l = shift;\n\t\"{line:\".join(\",\",map(pstr,linep($l))).\"}\";\n}\n\nsub yx {\n\tmy ($l,$x)=@_;\n\t!$l->{v} || die;\n\t$l->{k}*$x+$l->{b};\n}\n\nsub peq {\n\t$_[0]->{x}==$_[1]->{x} && $_[0]->{y}==$_[1]->{y};\n}\n\nsub line_ab {\n\tmy ($a, $b) = @_;\n\tmy ($rv, $rv2);\n\n\tif ($a->{v} && $b->{v} && $a->{x}!=$b->{x}) {\n\t\t$rv=[0];\n\t} elsif ($a->{v} && $b->{v} && $a->{x}==$b->{x}) {\n\t\t$rv=[3];\n\t} elsif ($a->{v} && !$b->{v}) {\n\t\t$rv=[1, p($a->{x}, yx($b,$a->{x}))];\n\t} elsif (!$a->{v} && $b->{v}) {\n\t\t$rv=[1, p($b->{x}, yx($a,$b->{x}))];\n\t} elsif ($a->{k}==$b->{k} && $a->{b}==$b->{b}) {\n\t\t$rv=[3];\t\t\n\t} elsif ($a->{k}==$b->{k} && $a->{b}!=$b->{b}) {\n\t\t$rv=[0];\n\t} elsif ($a->{k}!=$b->{k}) {\n\t\tmy $x = ($b->{b} - $a->{b}) / ($a->{k} - $b->{k});\n\t\t$rv=[1, p($x, yx($a,$x))];\n\t} else {\n\t\tdie;\n\t}\n\n\tmy ($n, $e) = @$rv;\n\tif ($n == 1) {\n\t\tif (abc($b->{p}->[0], $e, $b->{p}->[1])) {\n\t\t\tif (peq($e, $b->{p}->[0]) || peq($e, $b->{p}->[1])) {\n\t\t\t\t# vertex\n\t\t\t\t$rv = [2, $e, $b];\n\t\t\t} else {\n\t\t\t\t# edge\n\t\t\t\t$rv = [1, $e, $b];\n\t\t\t}\n\t\t} else {\n\t\t\t$rv = [0];\n\t\t}\n\t} elsif ($n == 3) {\n\t\t# whole edge\n\t\t$rv = [3, $b->{p}->[0], $b];\n\t\t$rv2 = [3, $b->{p}->[1], $b];\n\t}\n\n\tif ($rv->[0] == 0) {\n\t\t$rv = undef;\n\t}\n\n\twantarray? ($rv, $rv2): $rv->[0];\n}\n\nsub abstr {\n\tmy $ab = shift;\n\t\"{n=\".$ab->[0].\",\".pstr($ab->[1]).\",\".(-2+@$ab).\"}\";\n}\n\nsub abc {\n\tmy ($a,$b,$c)=@_;\n\tmy ($x1, $x2, $x3) = ($a->{x}, $b->{x}, $c->{x});\n\tmy ($y1, $y2, $y3) = ($a->{y}, $b->{y}, $c->{y});\n\tif ($x1 > $x3) {\n\t\t($x1, $x3) = ($x3, $x1);\n\t}\n\tif ($y1 > $y3) {\n\t\t($y1, $y3) = ($y3, $y1);\n\t}\n\t$x1<=$x2 && $x2<=$x3 && $y1<=$y2 && $y2<=$y3? 1: 0;\n}\n\nsub len {\n\tmy ($a, $b) = @_;\n\tsqrt( ($a->{x} - $b->{x})**2 + ($a->{y} - $b->{y})**2 );\n}\n\nsub get_2points {\n\tmy ($points, $exclude) = @_;\n\tmy @found;\n\tP: foreach my $p (@$points) {\n\t\tforeach my $e (@$exclude) {\n\t\t\tpeq($p, $e) && next P;\n\t\t}\n\t\tforeach my $f (@found) {\n\t\t\tpeq($p, $f) && next P;\n\t\t}\n\t\tpush @found, $p;\n\t}\n\t@found == 2 || die 0+@found;\n\t@found;\n}\n\nsub enter_leave {\n\tmy ($p, $s) = @_;\n\tif ($inside) {\t# leave\n \tmy $sadd = len($pstart, $p);\n\t\t$s += $sadd;\n \ttrace(\"leave \", pstr($p),\" pstart=\", pstr($pstart), \" sadd=$sadd s=$s\");\n \t$pstart = undef;\n \t$inside = 0;\n\t} else {\t\t# enter\n \t$pstart = $p;\n \t$inside = 1;\n \ttrace(\"enter \", pstr($pstart));\n\t}\n\t$s;\n}\n\nsub calc_line {\n\tmy ($m_num, $m) = @_;\n\tmy (@ab, @ab1, $s);\n\ttrace(\"--- calc_line $m_num \".linestr($m));\n\n\tdie;\n\n\t# intersections\n\tforeach my $n (@n) {\n\t\tmy ($ab1, $ab2) = line_ab($m, $n);\n\t\tif ($ab1) {\n\t\t\tpush(@ab, $ab1);\n\t\t\ttrace(\"ab \", abstr($ab1));\n\t\t}\n\t\tif ($ab2) {\n\t\t\tpush(@ab, $ab2);\n\t\t\ttrace(\"ab:2 \", abstr($ab2));\n\t\t}\n\t}\n\n\t# sort\n\t@ab = sort { \n\t\tmy ($ax, $ay, $bx, $by) = (pxy($a->[1]), pxy($b->[1]));\n\t\t$ax == $bx? $ay <=> $by: $ax <=> $bx;\n\t} @ab;\n\ttrace(\"sort \", 0+@ab);\n\n\t# doubles\n\tfor (my $i = 0; $i < @ab; $i++) {\n\t\tif ($i < @ab - 1) {\n\t\t\tmy ($n1, $e1, $s1) = @{ $ab[$i] };\n\t\t\tmy ($n2, $e2, $s2) = @{ $ab[$i + 1] };\n\t\t\tpeq($e1, $e2) && do {\n\t\t\t\tmy $ab1 = [max($n1,$n2), $e1, $s1, $s2]; \n\t\t\t\tpush @ab1, $ab1;\n\t\t\t\ttrace(\"ab1:2 \", abstr($ab1));\n\t\t\t\t$i++;\n\t\t\t\tnext;\n\t\t\t};\n\t\t} \n\t\tpush @ab1, $ab[$i];\n\t\ttrace(\"ab1 \", abstr($ab[$i]));\n\t}\n\ttrace(\"ab1 \", 0+@ab1);\n\n\t# enter, leave, calc\n\t$s = 0;\n\t$inside = 0;\n\tfor (my $i = 0; $i < @ab1; $i++) {\n\t\tmy ($n, $p, $s1, $s2) = @{ $ab1[$i] };\n\n\t\t# edge\n\t\t$n == 1 && do {\n\t\t\t$s = enter_leave($p, $s);\n\t\t};\n\t\t\n\t\t# vertex\n\t\t$n == 2 && do {\n\t\t\tif (line_ab($m, line(get_2points([linep($s1), linep($s2)], [$p])))) {\n\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t}\n\t\t};\n\n\t\t# edge through\n\t\t$n == 3 && do {\n \t\t\tif ($inside_edge) {\n \t\t\t\t# vertex - 1\n \t\t\t\tmy ($prev_n, $prev_p, $prev_s1, $prev_s2) = @{ $ab1[$i - 1] };\n \t\t\t\tif (@n == 3 || !line_ab($m, line(\n\t\t\t\t\tget_2points([linep($s1), linep($s2), linep($prev_s1), linep($prev_s2)], [$p, $prev_p])))) \n\t\t\t\t{\n \t\t\t\t\t$s = enter_leave($p, $s);\n \t\t\t\t}\n \t\t\t\t$inside_edge = 0;\n \t\t\t} else {\n \t\t\t\tif (!$inside) {\n\t \t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t}\n \t\t\t\t$inside_edge = 1;\n \t\t\t}\n\t\t};\n\t}\n\n\ttrace(\"retval \", $s);\n\t$s;\n}\n\nsub input {\n\tmy $fh = shift;\n\tmy $s = sub { [split(' ',<$fh>)] };\n\tmy ($n,$m) = @{&$s()};\n\ttrace(\"n=$n m=$m\");\n\n\t# n: x0 y0\n\tmy @n_;\n\t$n_[$_]=&$s() for 0..$n-1; push @n_,$n_[0];\n\t$n[$_] = line(p($n_[$_]), p($n_[$_+1])) for 0..$n-1;\n\t\n\t# m: x0 y0 x1 y1\n\tfor (0..$m-1) {\n\t\tmy @p = @{ &$s() };\n\t\t$m[$_] = line(p(@p[0,1]), p(@p[2,3]));\n\t}\n}\n\nsub calc {\n\tfor (my $i = 0; $i < @m; $i++) {\n\t\teval {\n\t\t\tmy $s = calc_line($i + 1, $m[$i], \\@n);\n\t\t\tprint \"$s \";\n\t\t\t$inside == 0 || die \"test $i\";\n\t\t};\n\t\tif ($@) {\n\t\t\tprint \"calc \", $i+1, \" \", linestr($m[$i]), \"\\n\";\n\t\t\tprint $@, \"\\n\";\n\t\t\texit(0);\n\t\t}\n\t}\t\n\tprint \"\\n\";\n}\n\ninput(DEV? \\*DATA: \\*STDIN);\ncalc();\n\n__DATA__\n3 6\n0 0\n0 4\n4 0\n-2 2 2 2\n0 4 4 4\n-1 0 -1 10\n0 0 1 0\n0 0 2 2\n0 2 4 0\n"}, {"source_code": "use strict;\nuse constant DEV => 1;\n\nour (@n, @m, $pstart, $inside);\n\nsub trace {\n\tprint STDERR \"[trace] \", @_, \"\\n\" if DEV;\n}\n\nsub p {\n\tref($_[0])?\n\t\t{ x=>$_[0]->[0], y=>$_[0]->[1] }:\n\t\t{ x=>$_[0], y=>$_[1] }\n}\n\nsub pstr {\n\tmy $p = shift;\n\t\"{x=\".$p->{x}.\",y=\".$p->{y}.\"}\";\n}\n\nsub pxy {\n\tmy $p = shift;\n\t($p->{x}, $p->{y});\n}\n\nsub line {\n\tmy @p=@_;\n\tif ($p[0]->{x} > $p[1]->{x}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy $v=$p[0]->{x}==$p[1]->{x}?1:0;\n\t!$v || $p[0]->{y}!=$p[1]->{y} || die;\n\tif ($v && $p[0]->{y} > $p[1]->{y}) {\n\t\t@p = ($p[1], $p[0]);\n\t}\n\tmy ($k,$b);\n\tif (!$v) {\n\t\t$k=($p[1]->{y} - $p[0]->{y}) / ($p[1]->{x} - $p[0]->{x});\n\t\t$b=$p[0]->{y}-$k*$p[0]->{x};\n\t}\n\t{ p=>\\@p, k=>$k, b=>$b, v=>$v, x=>$p[0]->{x} }\n}\n\nsub linep {\n\tmy $l = shift;\n\t@{ $l->{p} };\n}\n\nsub yx {\n\tmy ($l,$x)=@_;\n\t!$l->{v} || die;\n\t$l->{k}*$x+$l->{b};\n}\n\nsub peq {\n\t$_[0]->{x}==$_[1]->{x} && $_[0]->{y}==$_[1]->{y};\n}\n\nsub line_ab {\n\tmy ($a, $b) = @_;\n\tmy $rv;\n\tif ($a->{v} && $b->{v} && $a->{x}!=$b->{x}) {\n\t\t$rv=[0];\n\t} elsif ($a->{v} && $b->{v} && $a->{x}==$b->{x}) {\n\t\t$rv=[2];\n\t} elsif ($a->{v} && !$b->{v}) {\n\t\t$rv=[1, p($a->{x}, yx($b,$a->{x}))];\n\t} elsif (!$a->{v} && $b->{v}) {\n\t\t$rv=[1, p($b->{x}, yx($a,$b->{x}))];\n\t} elsif ($a->{k}==$b->{k} && $a->{b}==$b->{b}) {\n\t\t$rv=[2];\t\t\n\t} elsif ($a->{k}==$b->{k} && $a->{b}!=$b->{b}) {\n\t\t$rv=[0];\n\t} elsif ($a->{k}!=$b->{k}) {\n\t\tmy $x = ($b->{b} - $a->{b}) / ($a->{k} - $b->{k});\n\t\t$rv=[1, p($x, yx($a,$x))];\n\t} else {\n\t\tdie;\n\t}\n\t$rv;\n}\n\nsub abc {\n\tmy ($a,$b,$c)=@_;\n\tmy ($x1, $x2, $x3) = ($a->{x}, $b->{x}, $c->{x});\n\tmy ($y1, $y2, $y3) = ($a->{y}, $b->{y}, $c->{y});\n\tif ($x1 > $x3) {\n\t\t($x1, $x3) = ($x3, $x1);\n\t}\n\tif ($y1 > $y3) {\n\t\t($y1, $y3) = ($y3, $y1);\n\t}\n\t$x1<=$x2 && $x2<=$x3 && $y1<=$y2 && $y2<=$y3? 1: 0;\n}\n\nsub len {\n\tmy ($a, $b) = @_;\n\tsqrt( ($a->{x} - $b->{x})**2 + ($a->{y} - $b->{y})**2 );\n}\n\nsub get_2points {\n\tmy ($points, $exclude) = @_;\n\tmy @found;\n\tP: foreach my $p (@$points) {\n\t\tforeach my $e (@$exclude) {\n\t\t\tpeq($p, $e) && next P;\n\t\t}\n\t\tpush @found, $p;\n\t}\n\t@found == 2 || die;\n\t@found;\n}\n\nsub line_seg_ab {\n\tmy ($a, $b) = @_;\n\tmy ($n, $e) = @{ line_ab($a, $b) };\n\tmy ($rv1, $rv2);\n\tif ($n==1) {\n\t\t$rv1 = abc($b->{p}->[0], $e, $b->{p}->[1])? \n\t\t\t[$e, $b]: undef;\n\t} elsif ($n==2) {\n\t\t$rv1 = [$b->{p}->[0], $b];\n\t\t$rv2 = [$b->{p}->[1], $b];\n\t}\n\twantarray? ($rv1, $rv2): $rv1?1:0+$rv2?1:0;\n}\n\nsub enter_leave {\n\tmy ($p, $s) = @_;\n\tif ($inside) {\t# leave\n \t$s += len($pstart, $p);\n \ttrace(\"leave pstart=\", pstr($pstart), \" p=\", pstr($p), \" s=$s\");\n \t$pstart = undef;\n \t$inside = 0;\n\t} else {\t\t# enter\n \t$pstart = $p;\n \t$inside = 1;\n \ttrace(\"enter \", pstr($pstart));\n\t}\n\t$s;\n}\n\nsub calc_line {\n\tmy ($m_num, $m) = @_;\n\tmy (@ab, @ab1, $s);\n\ttrace(\"--- calc_line $m_num\");\n\n\t# intersections\n\tforeach my $n (@n) {\n\t\tmy ($ab1, $ab2) = line_seg_ab($m, $n);\n\t\tif ($ab1) {\n\t\t\tpush(@ab, $ab1);\n\t\t\ttrace(\"ab \", pstr($ab1->[0]));\n\t\t}\n\t\tif ($ab2) {\n\t\t\tpush(@ab, $ab2);\n\t\t\ttrace(\"ab:2 \", pstr($ab2->[0]));\n\t\t}\n\t}\n\n\t# sort\n\t@ab = sort { \n\t\tmy ($ax, $ay, $bx, $by) = (pxy($a->[0]), pxy($b->[0]));\n\t\t$ax == $bx? $ay <=> $by: $ax <=> $bx;\n\t} @ab;\n\ttrace(\"sort \", 0+@ab);\n\n\t# doubles\n\tfor (my $i = 0; $i < @ab; $i++) {\n\t\tif ($i == @ab - 1 || !peq($ab[$i]->[0], $ab[$i + 1]->[0])) {\n\t\t\t# edge\n\t\t\tpush @ab1, [1, @{$ab[$i]}]; \n\t\t} else {\n\t\t\t# vertex\n\t\t\tpush @ab1, [2, @{$ab[$i]}, $ab[$i + 1]->[1]]; \n\t\t\t$i++;\n\t\t}\n\t}\n\ttrace(\"ab1 \", 0+@ab1);\n\n\t# enter, leave, calc\n\t$s = 0;\n\t$inside = 0;\n\tfor (my $i = 0; $i < @ab1; $i++) {\n\t\tmy ($n, $p, $s1, $s2) = @{ $ab1[$i] };\n\n\t\t# edge\n\t\t$n == 1 && do {\n\t\t\t$s = enter_leave($p, $s);\n\t\t};\n\t\t\n\t\t# vertex\n\t\t$n == 2 && do {\n\t\t\tif (line_seg_ab($m, line(get_2points([linep($s1), linep($s2)], [$p])))) {\n\t\t\t\tif ($inside) {\n\t\t\t\t\t# vertex - 1\n\t\t\t\t\tmy ($prev_n, $prev_p, $prev_s1, $prev_s2) = @{ $ab1[$i - 1] };\n\t\t\t\t\tif (!line_seg_ab($m, line(get_2points([linep($s1), linep($s2), linep($prev_s1), linep($prev_s2)], [$p, $prev_p])))) {\n\t\t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t$s = enter_leave($p, $s);\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t}\n\n\t$s;\n}\n\nsub input {\n\tmy $fh = shift;\n\tmy $s = sub { [split(' ',<$fh>)] };\n\tmy ($n,$m) = @{&$s()};\n\ttrace(\"n=$n m=$m\");\n\n\t# n: x0 y0\n\tmy @n_;\n\t$n_[$_]=&$s() for 0..$n-1; push @n_,$n_[0];\n\t$n[$_] = line(p($n_[$_]), p($n_[$_+1])) for 0..$n-1;\n\t\n\t# m: x0 y0 x1 y1\n\tfor (0..$m-1) {\n\t\tmy @p = @{ &$s() };\n\t\t$m[$_] = line(p(@p[0,1]), p(@p[2,3]));\n\t}\n}\n\nsub calc {\n\tfor (my $i = 0; $i < @m; $i++) {\n\t\tprint calc_line($i + 1, $m[$i], \\@n), \" \";\n\t\t$inside == 0 || die \"test $i\";\n\t}\t\n\tprint \"\\n\";\n}\n\ninput(DEV? \\*DATA: \\*STDIN);\ncalc();\ntrace(\"ans 10\");\n\n__DATA__\n12 1\n0 0\n10 0\n10 6\n1 6\n1 9\n10 9\n10 10\n0 10\n0 5\n9 5\n9 1\n0 1\n0 5 5 5\n"}, {"source_code": "use strict;\n\npackage calc;\n\nsub new {\n\tmy $self = { len => 0, start => undef, in_border => 0, inside => 0 };\n\tbless $self;\n}\n\nsub enter {\n\tmy ($calc, $coord) = @_;\n\t$calc->{start} = $coord;\n $calc->{inside} = 1;\n}\n\nsub leave {\n\tmy ($calc, $coord) = @_;\n\t$calc->{len} += abs($coord - $calc->{start});\n $calc->{start} = undef;\n $calc->{inside} = 0;\n}\n\nsub enter_leave {\n\tmy ($calc, $coord) = @_;\n\tif ($calc->{inside}) {\t\n\t\t$calc->leave($coord);\n\t} else {\t\t\n\t\t$calc->enter($coord);\n\t}\n}\n\nsub enter_border {\n\tmy ($calc, $coord, $level) = @_;\n\t$calc->{border_level} = $level;\n\t$calc->{border_inside} = $calc->{inside};\n\t$calc->{in_border} = 1;\n\t$calc->enter($coord) unless $calc->{inside};\n}\n\nsub leave_border {\n\tmy ($calc, $coord, $level) = @_;\n\tmy $s = $calc->{border_level} * $level;\n\tmy $i = $calc->{border_inside};\n\tif (($i == 0 && $s > 0) || ($i == 1 && $s < 0)) {\n\t\t$calc->leave($coord);\n\t}\n\t$calc->{in_border} = 0;\n}\n\nsub in_border {\n\tshift->{in_border};\n}\n\nsub ans {\n\tshift->{len};\n}\n\npackage main;\n\nsub fz {\n\tmy $f = shift;\n\tabs($f) < 1E-12? 0: $f;\n}\n\nsub line {\n\tmy ($x1, $y1, $x2, $y2) = @_;\n\tmy ($k, $b, $v, $x, $sin, $cos);\n\t$v = ($x1 == $x2);\n\tif ($v) {\n\t\t$x = $x1;\n\t\t$sin = 1;\n\t\t$cos = 0;\n\t} else {\n\t\tmy $dx = ($x2 - $x1);\n\t\tmy $dy = ($y2 - $y1);\n\t\tif ($dx < 0) {\n\t\t\t$dx = -$dx;\n\t\t\t$dy = -$dy;\n\t\t}\n\t\t$k = $dy / $dx;\n\t\t$b = $y1 - $k * $x1;\n\t\tmy $l = sqrt($dx * $dx + $dy * $dy);\n\t\t$sin = $dy / $l;\n\t\t$cos = $dx / $l;\n\t}\n\t{ k => $k, b => $b, v => $v, x => $x, sin => $sin, cos => $cos }\n}\n\nsub newxy {\n\tmy ($m, $nref, $xref, $yref) = @_;\n\tmy $n = @$nref;\n\tmy ($xshift, $yshift);\n\tif ($m->{v}) {\n\t\t$xshift = $m->{x};\n\t} else {\n\t\t$yshift = $m->{b};\n\t}\n\tmy $sin = $m->{sin};\n\tmy $cos = $m->{cos};\n\tfor (my $i = 0; $i < $n; $i++) {\n\t\tmy $x = $$nref[$i][0] - $xshift;\n\t\tmy $y = $$nref[$i][1] - $yshift;\n\t\t$$xref[$i] = $x * $cos + $y * $sin;\n\t\t$$yref[$i] = fz($y * $cos - $x * $sin);\n\t}\n}\n\nsub entry_points {\n\tmy ($xref, $yref) = @_;\n\tmy $n = @$xref;\n\tmy @i;\n\tfor (my $i = 0; $i < $n; $i++) {\n\t\tif ($yref->[$i] == 0 && $yref->[$i - 1] * $yref->[($i + 1) % $n] <= 0) {\n\t\t\tpush(@i, $i);\n\t\t} elsif ($yref->[$i] * $yref->[$i - 1] < 0) {\n\t\t\tpush(@i, $i);\n\t\t}\n\t}\n\t@i = sort { $xref->[$a] <=> $xref->[$b] } @i;\n\t@i;\n}\n\nsub calc_line {\n\tmy ($m, $nref) = @_;\n\tmy $n = @$nref;\n\tmy $c = calc->new();\n\n\t# new (x, y)\n\tmy (@x, @y);\n\tnewxy($m, $nref, \\@x, \\@y);\n\n\t# entry points\n\tmy @i = entry_points(\\@x, \\@y);\n\n\t# enter, leave, calc\n\tfor my $i (@i) {\n\t\tmy $y = $y[$i];\n\t\tmy $yp = $y[$i - 1];\n\t\tmy $yn = $y[($i + 1) % $n];\n\t\tif ($y == 0) {\n\t\t\tif ($c->in_border) {\n\t\t\t\t$c->leave_border($x[$i], $yp + $yn);\n\t\t\t} elsif ($yp * $yn == 0) {\n\t\t\t\t$c->enter_border($x[$i], $yp + $yn);\n\t\t\t} elsif ($yp * $yn < 0) {\n\t\t\t\t$c->enter_leave($x[$i]) \n\t\t\t}\n\t\t} else {\n\t\t\t# y <> 0\n\t\t\tmy $k = $yp / $y;\n\t\t\tif ($k < 0) {\n\t\t\t\tmy $x = $x[$i] + ($x[$i - 1] - $x[$i])/(1 - $k);\n\t\t\t\t$c->enter_leave($x);\n\t\t\t} \n\t\t}\n\t}\n\t$c->ans();\n}\n\nsub input {\n\tmy ($fh, $nref, $mref) = @_;\n\tmy $input = sub { split(' ', <$fh>) };\n\tmy ($n, $m) = &$input();\n\t# n: x0 y0\n\t$nref->[$_] = [ &$input() ] for 0 .. $n - 1;\n\t# m: x0 y0 x1 y1\n\t$mref->[$_] = [ &$input() ] for 0 .. $m - 1;\n}\n\nsub go {\n\tmy (@n, @m);\n\tinput(\\*STDIN, \\@n, \\@m);\n\tfor my $m (@m) {\n\t\tmy $ans = calc_line(line(@$m), \\@n);\n\t\tprint $ans, \" \";\n\t}\n\tprint \"\\n\";\n}\n\ngo() unless exists $INC{\"Test/More.pm\"};\n\n1;\n"}], "src_uid": "4bb6d1680ca0cbfe76dbcce5ea4c77b3"} {"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,\u2009m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20092\u00b7105) \u2014 the sizes of arrays a and b. The second line contains n integers \u2014 the elements of array a (\u2009-\u2009109\u2009\u2264\u2009ai\u2009\u2264\u2009109). The third line contains m integers \u2014 the elements of array b (\u2009-\u2009109\u2009\u2264\u2009bj\u2009\u2264\u2009109).", "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": "@_ = sort {$a <=> $b} (split ' ', (<>, <>) ), map \"$_=\", @B = split ' ', <>;\n\nmap $h{ $_ } = $i +=!/=/, @_;\n\t\nprint join ' ', map $h{ \"$_=\" }, @B;"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@A = split ' ', <>;\n\t@B = split ' ', <>;\n\t@C = sort {$a <=> $b} @A, map \"${_}_\", @B;\n\t\n\t%h = ();\n\t$i = 0;\n\tfor (@C){\n\t\t/_/ or $i ++;\n\t\t$h{ $_ } = $i;\n\t\t}\n\t\n\tprint join ' ', map $h{ \"${_}_\" }, @B;\n\t}"}], "negative_code": [], "src_uid": "e9a519be33f25c828bae787330c18dd4"} {"nl": {"description": "Stephen Queen wants to write a story. He is a very unusual writer, he uses only letters 'a', 'b', 'c', 'd' and 'e'!To compose a story, Stephen wrote out $$$n$$$ words consisting of the first $$$5$$$ lowercase letters of the Latin alphabet. He wants to select the maximum number of words to make an interesting story.Let a story be a sequence of words that are not necessarily different. A story is called interesting if there exists a letter which occurs among all words of the story more times than all other letters together.For example, the story consisting of three words \"bac\", \"aaada\", \"e\" is interesting (the letter 'a' occurs $$$5$$$ times, all other letters occur $$$4$$$ times in total). But the story consisting of two words \"aba\", \"abcde\" is not (no such letter that it occurs more than all other letters in total).You are given a sequence of $$$n$$$ words consisting of letters 'a', 'b', 'c', 'd' and 'e'. Your task is to choose the maximum number of them to make an interesting story. If there's no way to make a non-empty story, output $$$0$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) \u2014 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 2 \\cdot 10^5$$$) \u2014 the number of the words in the sequence. Then $$$n$$$ lines follow, each of them contains a word \u2014 a non-empty string consisting of lowercase letters of the Latin alphabet. The words in the sequence may be non-distinct (i.\u2009e. duplicates are allowed). Only the letters 'a', 'b', 'c', 'd' and 'e' may occur in the words. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$; the sum of the lengths of all words over all test cases doesn't exceed $$$4 \\cdot 10^5$$$.", "output_spec": "For each test case, output the maximum number of words that compose an interesting story. Print 0 if there's no way to make a non-empty interesting story.", "sample_inputs": ["6\n3\nbac\naaada\ne\n3\naba\nabcde\naba\n2\nbaba\nbaba\n4\nab\nab\nc\nbc\n5\ncbdca\nd\na\nd\ne\n3\nb\nc\nca"], "sample_outputs": ["3\n2\n0\n2\n3\n2"], "notes": "NoteIn the first test case of the example, all $$$3$$$ words can be used to make an interesting story. The interesting story is \"bac aaada e\".In the second test case of the example, the $$$1$$$-st and the $$$3$$$-rd words can be used to make an interesting story. The interesting story is \"aba aba\". Stephen can't use all three words at the same time.In the third test case of the example, Stephen can't make a non-empty interesting story. So the answer is $$$0$$$.In the fourth test case of the example, Stephen can use the $$$3$$$-rd and the $$$4$$$-th words to make an interesting story. The interesting story is \"c bc\"."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\tmy @cand;\n\t\n\tfor my $letter ( 'a' .. 'e' ){\n\t\tmy @balance;\n\t\t\n\t\tfor my $word ( @_ ){\n\t\t\tmy $balance = 0;\n\t\t\t\n\t\t\twhile( $word =~ /./g ){\n\t\t\t\t$balance += $& eq $letter ? 1 : -1;\n\t\t\t\t}\n\t\t\t\n\t\t\t$debug and print \" $letter:$balance\";\n\t\t\t\n\t\t\tpush @balance, $balance;\n\t\t\t}\n\t\t\n\t\t@balance = sort { $b <=> $a } @balance;\n\t\t\n\t\t$debug and print \" balance:[@balance]\";\n\t\t\n\t\tmy $cand = 0;\n\t\tmy $sum = 0;\n\t\t\n\t\twhile( @balance ){\n\t\t\tmy $i = shift @balance;\n\t\t\t\n\t\t\tif( $i > 0 ){\n\t\t\t\t$cand ++;\n\t\t\t\t$sum += $i;\n\t\t\t\t}\n\t\t\telsif( $i == 0 and $sum > 0 ){\n\t\t\t\t$cand ++;\n\t\t\t\t}\n\t\t\telsif( $sum + $i > 0 ){\n\t\t\t\t$cand ++;\n\t\t\t\t$sum += $i;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\tpush @cand, $cand;\n\t\t}\n\t\n\t$debug and print \"\\@cand:[@cand]\";\n\t\n\tprint 0 + ( sort { $b <=> $a } @cand )[ 0 ];\n\t}"}], "negative_code": [], "src_uid": "18ac51a009c907fe8e4cd2bb8612da20"} {"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 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009100, 1\u2009\u2264\u2009s\u2009\u2264\u20091000)\u00a0\u2014 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\u2009\u2264\u2009fi\u2009\u2264\u2009s, 1\u2009\u2264\u2009ti\u2009\u2264\u20091000)\u00a0\u2014 the floor and the time of arrival in seconds for the passenger number i.", "output_spec": "Print a single integer\u00a0\u2014 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\u2009+\u20092\u2009+\u20094\u2009+\u20091\u2009+\u20092\u2009=\u200911 seconds."}, "positive_code": [{"source_code": "use feature ':all';\n\nwhile(<>){\n\tsay( ( sort {$b <=> $a} (split)[1], map { eval join '+', split } <>)[ 0 ] )\n\t}"}], "negative_code": [{"source_code": "use feature ':all';\n\nwhile(<>){\n\tsay( ( sort {$b <=> $a} map { eval join '+', split } <>)[ 0 ] )\n\t}"}], "src_uid": "5c12573b3964ee30af0349c11c0ced3b"} {"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\u00a0\u2014 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$$$)\u00a0\u2014 the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$2 \\le n \\le 100$$$)\u00a0\u2014 the required size of a square.", "output_spec": "For each test case print $$$n$$$ lines, each containing $$$n$$$ integers\u00a0\u2014 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": "#!/usr/bin/perl\n\n\nmy @so = (); $#so = 105*105;\nfor(my $i=2;$i<=105*105;$i++){\n my $j = $i + $i;\n while( $j <= $#so ){\n $so[$j] = 1;\n $j += $i;\n }\n}\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nwhile($t-->0){\n my ($n) = map { $_ - 0 } split(/\\s+/,);\n \n if( $n == 2 ){\n print \"1 1\\n\";\n print \"1 1\\n\";\n next;\n }\n \n my @r = ((4) x $n);\n my $s = 4 * ($n-1);\n my $i;\n for($i=3;$i<=105;$i+=2){\n last if $so[$i] - 0 == 1 and $so[$s+$i] - 0 == 0;\n }\n $r[$n-1] = $i;\n \n for(my $i=0;$i<$n;$i++){\n for(my $j=0;$j<$n;$j++){\n print \" \" if $j>0;\n print $r[ ($i + $j) % $n ];\n }\n print \"\\n\";\n }\n}\n\n\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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy @c = ( 0 ) x 3;\n\t\n\t$c[ $_ % 3 ] ++ for @_;\n\t\n\t$debug and print \"[@c]\";\n\t\n\t$_ -= @_ / 3 for @c;\n\t\n\t$debug and print \"[@c]\";\n\t\n\t$_ = join ' ', ( @c ) x 2;\n\t\n\t$debug and print \"[$_]\";\n\t\n\tmy $ans = 0;\n\t\n\tif( m/(?;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy @c;\n\t\n\t$c[ $_ % 3 ] ++ for @_;\n\t\n\t$debug and print \"[@c]\";\n\t\n\t$_ -= @_ / 3 for @c;\n\t\n\t$debug and print \"[@c]\";\n\t\n\t$_ = join ' ', ( @c ) x 2;\n\t\n\t$debug and print \"[$_]\";\n\t\n\tmy $ans = 0;\n\t\n\tif( m/(?;\nchomp ( $n );\nmy $f = ;\nchomp ( $f );\nmy @arr = split m/ /, $f;\n\nmy $sum = 0;\nfor (@arr) {\n $sum += $_; }\n@arr = sort { $a <=> $b } @arr;\n\nmy $lower = $n - ($sum % $n);\nmy $upper = ($sum % $n);\nmy $avg = int ($sum / $n);\n\nmy $seconds = 0;\nfor ( my $i = 0; $i < $lower; $i += 1 ) {\n $seconds += abs ((shift @arr) - $avg);}\n\nfor ( my $j = 0; $j < $upper; $j += 1 ) {\n $seconds += abs ((shift @arr) - ($avg+1));}\n\n$seconds /= 2;\nprint \"$seconds\\n\";\n\n\n\n\n"}, {"source_code": "sub floor{\n\tmy ($x,$y)=@_;\n\tmy $t=$x%$y;\n\treturn ($x-$t)/$y;\n}\nsub ceil\n{\n\tmy ($x,$y)=@_;\n\tmy $t=$x%$y;\n\t$t=$y-$t if ($t!=0);\n\treturn ($x+$t)/$y;\n}\nsub max\n{\n\tmy ($x,$y)=@_;\n\tif ($x>$y)\n\t{\n\t\treturn $x;\n\t}\n\telse\n\t{\n\t\treturn $y;\n\t}\n}\n$n=<>;\nchomp($n);\n$s=<>;\nchomp($s);\n@a=split ' ',$s;\nmap\n{\n\t$t+=$_;\n}@a;\n$e=floor($t,$n);\n$f=ceil($t,$n);\nfor ($i=0; $i<$n; $i++)\n{\n\t$q+=$e-$a[$i] if $a[$i]<$e;\n\t$w+=$a[$i]-$f if $a[$i]>$f;\n}\n$rez=max($q,$w);\nif ($rez>0)\n{\n\tprint $rez;\n}\nelse\n{\n\tprint 0;\n}"}, {"source_code": "$n = <>;\n@n = sort {$a <=> $b} split ' ', <>;\n$sum += $_ for @n;\n$small = int($sum / $n);\n$bigger = $sum % $n;\n@w = ( ($small) x ($n - $bigger), ($small + 1) x $bigger );\n$sum = 0;\nfor (@n){\n$sum += abs($_ - shift @w)\n}\nprint $sum >> 1"}], "negative_code": [{"source_code": "\n\n\n\n\n\nmy $n = ;\nchomp ( $n );\nmy $f = ;\nchomp ( $n );\nmy @arr = split m/ /, $f;\n\nmy $sum = 0;\nfor (@arr) {\n $sum += $_; }\n@arr = sort @arr;\n\n\nmy $lower = $n - ($sum % $n);\nmy $upper = ($sum % $n);\nmy $avg = int ($sum / $n);\n\nmy $seconds = 0;\nfor (1..$lower) {\n $seconds += abs ((shift @arr) - $avg);}\n\nfor (1..$upper) {\n $seconds += abs ((shift @arr) - ($avg+1));}\n\n$seconds /= 2;\nprint \"$seconds\\n\";\n\n\n \n\n\n\n\n\n\n\n\n\n"}, {"source_code": "sub floor{\n\tmy ($x,$y)=@_;\n\tmy $t=$x%$y;\n\treturn ($x-$t)/$y;\n}\nsub ceil\n{\n\tmy ($x,$y)=@_;\n\tmy $t=$x%$y;\n\t$t=$y-$t if ($t!=0);\n\treturn ($x+$t)/$y;\n}\nsub max\n{\n\tmy ($x,$y)=@_;\n\tif ($x>$y)\n\t{\n\t\treturn $x;\n\t}\n\telse\n\t{\n\t\treturn $y;\n\t}\n}\n$n=<>;\nchomp($n);\n$s=<>;\nchomp($s);\n@a=split ' ',$s;\nmap\n{\n\t$t+=$_;\n}@a;\n$e=floor($t,$n);\n$f=ceil($t,$n);\nfor ($i=0; $i<$n; $i++)\n{\n\t$q+=$e-$a[$i] if $a[$i]<$e;\n\t$w+=$a[$i]-$f if $a[$i]>$f;\n}\nprint max($q,$w);"}, {"source_code": "$n = <>;\n@n = sort {$a <=> $b} split ' ', <>;\n$sum += $_ for @n;\n$small = int($sum / $n);\n$bigger = $sum % $n;\n@w = ( ($small) x ($n - $bigger), ($small + 1) x $bigger );\nfor (@n){\n$sum += abs($_ - shift @w)\n}\nprint $sum >> 1"}], "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\u2009+\u20091 and ak\u2009-\u20091 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\u2009\u2264\u2009n\u2009\u2264\u2009105) that shows how many numbers are in Alex's sequence. The second line contains n integers a1, a2, ..., an (1\u2009\u2264\u2009ai\u2009\u2264\u2009105).", "output_spec": "Print a single integer \u2014 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,\u20092,\u20092,\u20092]. 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": "#!/usr/bin/perl\nuse v5.10;\n\nsub max {\n\tmy ($x, $y) = (shift, shift);\n\treturn $x>$y ? $x:$y;\n}\n\n@dp = (0) x 100005;\n@sum = (0) x 100005;\nchomp($m = <>);\n$sum[$_]+=$_ foreach (split / /, <>);\n$dp[1] = $sum[1];\n$dp[$_]=max($dp[$_-1], $dp[$_-2]+$sum[$_]) foreach (2..100000);\nsay $dp[100000];"}], "negative_code": [], "src_uid": "41b3e726b8146dc733244ee8415383c0"} {"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\u2009\u2264\u2009n\u2009\u2264\u20091000)\u00a0\u2014 the number of round participants. Each of the next n lines contains two integers ai and bi (1\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u20094126)\u00a0\u2014 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": "map { <> !~ /^(\\d+) \\1$/ ? ( $R = 2 ) : push @_, $1 } 1 .. <>;\n\n@s = sort { $b <=> $a } @_;\n\nprint qw( unrated maybe rated )[ $R || \"@s\" eq \"@_\" ]"}, {"source_code": "$p = ~-0;\n\nmap { <> !~ /^(\\d+) \\1$/ ? ( $R = 2 ) : ( $f ||= $1 > $p, $p = $1 ) } 1 .. <>;\n\nprint qw( unrated maybe rated )[ $R || !$f ]"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = ();\n\tmy $R = 0;\n\t\n\tmap { \n\t\tmy( $A, $B ) = split ' ', <>;\n\t\t$A != $B ? $R ++ : push @_, $A;\n\t\t} 1 .. $_;\n\t\n\tmy @s = sort { $b <=> $a } @_;\n\t\n\tprint $R ? 'rated' :\n\t\t\"@s\" eq \"@_\" ? 'maybe' :\n\t\t'unrated';\n\t}"}], "negative_code": [], "src_uid": "88686e870bd3bfbf45a9b6b19e5ec68a"} {"nl": {"description": "Screen resolution of Polycarp's monitor is $$$a \\times b$$$ pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates $$$(x, y)$$$ ($$$0 \\le x < a, 0 \\le y < b$$$). You can consider columns of pixels to be numbered from $$$0$$$ to $$$a-1$$$, and rows\u00a0\u2014 from $$$0$$$ to $$$b-1$$$.Polycarp wants to open a rectangular window of maximal size, which doesn't contain the dead pixel. The boundaries of the window should be parallel to the sides of the screen.Print the maximal area (in pixels) of a window that doesn't contain the dead pixel inside itself.", "input_spec": "In the first line you are given an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases in the test. In the next lines you are given descriptions of $$$t$$$ test cases. Each test case contains a single line which consists of $$$4$$$ integers $$$a, b, x$$$ and $$$y$$$ ($$$1 \\le a, b \\le 10^4$$$; $$$0 \\le x < a$$$; $$$0 \\le y < b$$$)\u00a0\u2014 the resolution of the screen and the coordinates of a dead pixel. It is guaranteed that $$$a+b>2$$$ (e.g. $$$a=b=1$$$ is impossible).", "output_spec": "Print $$$t$$$ integers\u00a0\u2014 the answers for each test case. Each answer should contain an integer equal to the maximal possible area (in pixels) of a rectangular window, that doesn't contain the dead pixel.", "sample_inputs": ["6\n8 8 0 0\n1 10 0 3\n17 31 10 4\n2 1 0 0\n5 10 3 9\n10 10 4 8"], "sample_outputs": ["56\n6\n442\n1\n45\n80"], "notes": "NoteIn the first test case, the screen resolution is $$$8 \\times 8$$$, and the upper left pixel is a dead pixel. Here you can see one of two possible layouts of the maximal window. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B, $x, $y ) = split;\n\t\n\tprint +( sort { $b <=> $a } \n\t\t$B * ( $x - 0 ), $B * ( $A - $x + -1 ), $A * ( $y - 0 ), $A * ( $B - $y + -1 ) )[ 0 ];\n\t}"}], "negative_code": [], "src_uid": "ccb7b8c0c389ea771f666c236c1cba5f"} {"nl": {"description": "The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition.Teams plan to train for n times during n consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be ai teams on the i-th day.There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total).As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days.Sereja wants to order exactly ai pizzas on the i-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day n.", "input_spec": "The first line of input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the number of training sessions. The second line contains n integers a1, a2, ..., an (0\u2009\u2264\u2009ai\u2009\u2264\u200910\u2009000)\u00a0\u2014 the number of teams that will be present on each of the days.", "output_spec": "If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print \"YES\" (without quotes) in the only line of output. Otherwise, print \"NO\" (without quotes).", "sample_inputs": ["4\n1 2 1 2", "3\n1 0 1"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample.In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days."}, "positive_code": [{"source_code": "<>;\n\nprint qw(YES NO)[ \n 1 & ( join '', map { $_ &&= 1 + (1 - $_ % 2) } split ' ', <> )\n =~ s/22//gr =~ s/11//gr =~ y/2//dr =~ s/11//gr =~ y/0//dr \n ]"}, {"source_code": "<>;\n\nprint qw(YES NO)[ \n !! join '', \n map { \n s/.*?.\\s/ $& % 2 /reg =~ s/\\s//gr =~ s/00//gr =~ s/11//gr =~ y/0//dr =~ s/11//gr\n } split /\\b0/, <>\n ]"}, {"source_code": "<>;\n\nprint qw(YES NO)[ \n\t\t!! join '', \n\t\t\tmap { \n\t\t\t\t( join '', map $_ % 2, split ) =~ s/00//gr =~ s/11//gr =~ y/0//dr =~ s/11//gr\n\t\t\t} split /\\b0/, <>\n\t]"}, {"source_code": "<>;\n\nprint qw(YES NO)[ \n !! ( ( join '', map { $_ &&= 1 + (1 - $_ % 2) } split ' ', <> )\n =~ s/12*1//gr =~ /1/ )\n ]"}], "negative_code": [{"source_code": "\tprint qw(YES NO)[ \n\t\t\t!! join '', \n\t\t\t\tmap { \n\t\t\t\t\t( join '', map $_ % 2, split ) =~ s/00//gr =~ s/11//gr =~ y/0//dr =~ s/11//gr\n\t\t\t\t} split /\\b0/, <>\n\t\t]"}, {"source_code": "<>;\n\tprint qw(YES NO)[ \n\t\t\t!! join '', \n\t\t\t\tmap { \n\t\t\t\t\tjoin '', \n\t\t\t\t\t\tmap {\n\t\t\t\t\t\t\tmap s/11//gr, \n\t\t\t\t\t\t\t\tmap y/0//dr, \n\t\t\t\t\t\t\t\t\tsplit /11/\n\t\t\t\t\t\t}\n\t\t\t\t\t\t\tmap { split /00/ } \n\t\t\t\t\t\t\t\tjoin '', \n\t\t\t\t\t\t\t\t\tmap $_ % 2, split \n\t\t\t\t} split /\\b0/, <>\n\t\t]"}, {"source_code": "print qw(YES NO)[ \n\t\t\t!! join '', \n\t\t\t\tmap { \n\t\t\t\t\tjoin '', \n\t\t\t\t\t\tmap {\n\t\t\t\t\t\t\tmap s/11//gr, \n\t\t\t\t\t\t\t\tmap y/0//dr, \n\t\t\t\t\t\t\t\t\tsplit /11(?!0)/\n\t\t\t\t\t\t}\n\t\t\t\t\t\t\tmap { split /00(?!1)/ } \n\t\t\t\t\t\t\t\tjoin '', \n\t\t\t\t\t\t\t\t\tmap $_ % 2, split \n\t\t\t\t} split /\\b0/, <>\n\t\t]"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = split /\\b0/, <>;\n\n\t$f = 0;\n\tfor (@_){\n\t\t$_ = join '', map { $_ % 2 } split;\n\t\t$f += grep $_, map { (length) % 2 } map y/0//dr, map { split /11/ } split /00/;\n\t\t}\n\tprint $f ? 'NO' : 'YES';\n\t}"}, {"source_code": "print qw(YES NO)[ \n\t\t!! join '', \n\t\t\tmap { \n\t\t\t\t( join '', map $_ % 2, split ) =~ s/00//gr =~ s/11//gr =~ y/0//dr =~ s/11//gr\n\t\t\t} split /\\b0/, <>\n\t]"}, {"source_code": "<>;\n\nprint qw(YES NO)[ \n\t\t!! join '', \n\t\t\tmap { \n\t\t\t\ts/.*?.\\s?/ $& % 2 /regex =~ s/00//gr =~ s/11//gr =~ y/0//dr =~ s/11//gr\n\t\t\t} split /\\b0/, <>\n\t]"}, {"source_code": "\tprint qw(YES NO)[ \n\t\t\t!! join '', \n\t\t\t\tmap { \n\t\t\t\t\tjoin '', \n\t\t\t\t\t\tmap {\n\t\t\t\t\t\t\tmap s/11//gr, \n\t\t\t\t\t\t\t\tmap y/0//dr, \n\t\t\t\t\t\t\t\t\tsplit /11/\n\t\t\t\t\t\t}\n\t\t\t\t\t\t\tmap { split /00/ } \n\t\t\t\t\t\t\t\tjoin '', \n\t\t\t\t\t\t\t\t\tmap $_ % 2, split \n\t\t\t\t} split /\\b0/, <>\n\t\t]"}, {"source_code": "<>;\n\nprint qw(YES NO)[ \n !! join '', \n map { \n s/.*?.\\s/ $& % 2 /reg =~ y/ //dr =~ s/00//gr =~ s/11//gr =~ y/0//dr =~ s/11//gr\n } split /\\b0/, <>\n ]"}], "src_uid": "97697eba87bd21ae5c979a5ea7a81cb7"} {"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$$$) \u2014 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 \u2014 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": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nuse Carp;\n\n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\nsub ref_scalar {\n my $ref = shift;\n return 1 if ref($ref) eq 'SCALAR';\n}\n\nsub toggle {\n my $ref = shift;\n croak '$ref does not reference to scalar' if !ref_scalar($ref);\n\n $$ref = !$$ref;\n}\n\nsub odd {\n my $num = shift;\n return $num % 2 == 1;\n}\n\nsub even {\n my $num = shift;\n return $num % 2 == 0;\n}\n\n# solution\n\nmy $n = read_token;\n\n\nfor (1..$n) {\n my ($a, $b) = split q{ }, read_line;\n if ($a == $b) {\n say 0;\n next;\n }\n if ($a > $b) {\n if (even($a - $b)) {\n say 1;\n }\n else {\n say 2;\n }\n }\n else {\n if (odd($b - $a)) {\n say 1;\n }\n else {\n say 2;\n }\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B ) = split;\n\t\n\tif( $A == $B ){\n\t\tprint 0;\n\t\t}\n\t\t\n\telsif( $A % 2 == 0 and $B % 2 == 1 and $A < $B ){\n\t\tprint 1;\n\t\t}\n\telsif( $A % 2 == 0 and $B % 2 == 0 and $A < $B ){\n\t\tprint 2;\n\t\t}\n\telsif( $A % 2 == 1 and $B % 2 == 1 and $A < $B ){\n\t\tprint 2;\n\t\t}\n\telsif( $A % 2 == 1 and $B % 2 == 0 and $A < $B ){\n\t\tprint 1;\n\t\t}\n\t\n\telsif( $A % 2 == 0 and $B % 2 == 1 and $A > $B ){\n\t\tprint 2;\n\t\t}\n\telsif( $A % 2 == 0 and $B % 2 == 0 and $A > $B ){\n\t\tprint 1;\n\t\t}\n\telsif( $A % 2 == 1 and $B % 2 == 1 and $A > $B ){\n\t\tprint 1;\n\t\t}\n\telsif( $A % 2 == 1 and $B % 2 == 0 and $A > $B ){\n\t\tprint 2;\n\t\t}\n\t\n\t}"}], "negative_code": [], "src_uid": "fcd55a1ca29e96c05a3b65b7a8103842"} {"nl": {"description": "Let's assume that we are given a matrix b of size x\u2009\u00d7\u2009y, let's determine the operation of mirroring matrix b. The mirroring of matrix b is a 2x\u2009\u00d7\u2009y 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\u2009+\u20091 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\u2009+\u20091). Sereja has an n\u2009\u00d7\u2009m 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100). Each of the next n lines contains m integers \u2014 the elements of matrix a. The i-th line contains integers ai1,\u2009ai2,\u2009...,\u2009aim (0\u2009\u2264\u2009aij\u2009\u2264\u20091) \u2014 the i-th row of the matrix a.", "output_spec": "In the single line, print the answer to the problem \u2014 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\u2009\u00d7\u20093 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@a=<>;\nchomp @a;\n\nwhile(1){\n@A=reverse @a;\n$f=0;\n\n$A[$_] ne $a[$_] and $f=1 for 0..@A-1;\n\nif (@a%2 or $f){print 0+@a; exit}\n@a=@a[0..@a/2-1]\n}"}, {"source_code": "\nwhile(<>){\n\t\n\tchomp;\n\t($n,$m)=split/ /;\n\t@a=();\n\tfor $i(1..$n){push @a, \"\".<>;}\n\tchomp @a;\n\t\n#\tprint \"--@a\\n\";\n\t\n\tif (@a%2!=0){print 0+@a,\"\\n\"; next}\n\t@A=reverse @a;\n#\tprint \"-@A\\n\";\n\t$f=0;\n\tfor $i(0..@A-1){\n\t\t($A[$i] ne $a[$i]) and $f=1;\n#\t\tprint \"=$A[$i] $a[$i]\\n\";\n\t}\n#\tprint \"$f--@a\\n\";\n\tif ($f==1){print 0+@a,\"\\n\"; next}\n\t@a=@a[0..(@a/2-1)];\n\t\n#\tprint \"--@a\\n\";\n\t\n\tif (@a%2!=0){print 0+@a,\"\\n\"; next}\n\t@A=reverse @a;\n\t$f=0;\n\tfor $i(0..@A-1){\n\t\t$A[$i] ne $a[$i] and $f=1;\n\t}\n\tif ($f==1){print 0+@a,\"\\n\"; next}\n\t@a=@a[0..(@a/2-1)];\n\t\n\t\n\tif (@a%2!=0){print 0+@a,\"\\n\"; next}\n\t@A=reverse @a;\n\t$f=0;\n\tfor $i(0..@A-1){\n\t\t$A[$i] ne $a[$i] and $f=1;\n\t}\n\tif ($f==1){print 0+@a,\"\\n\"; next}\n\t@a=@a[0..(@a/2-1)];\n\t\n\t\n\tif (@a%2!=0){print 0+@a,\"\\n\"; next}\n\t@A=reverse @a;\n\t$f=0;\n\tfor $i(0..@A-1){\n\t\t$A[$i] ne $a[$i] and $f=1;\n\t}\n\tif ($f==1){print 0+@a,\"\\n\"; next}\n\t@a=@a[0..(@a/2-1)];\n\t\n\t\n\tif (@a%2!=0){print 0+@a,\"\\n\"; next}\n\t@A=reverse @a;\n\t$f=0;\n\tfor $i(0..@A-1){\n\t\t$A[$i] ne $a[$i] and $f=1;\n\t}\n\tif ($f==1){print 0+@a,\"\\n\"; next}\n\t@a=@a[0..(@a/2-1)];\n\t\n\t\n\t\n\tif (@a%2!=0){print 0+@a,\"\\n\"; next}\n\t@A=reverse @a;\n\t$f=0;\n\tfor $i(0..@A-1){\n\t\t$A[$i] ne $a[$i] and $f=1;\n\t}\n\tif ($f==1){print 0+@a,\"\\n\"; next}\n\t@a=@a[0..(@a/2-1)];\n\t\n\t\n\tif (@a%2!=0){print 0+@a,\"\\n\"; next}\n\t@A=reverse @a;\n\t$f=0;\n\tfor $i(0..@A-1){\n\t\t$A[$i] ne $a[$i] and $f=1;\n\t}\n\tif ($f==1){print 0+@a,\"\\n\"; next}\n\t@a=@a[0..(@a/2-1)];\n\t\n\tprint \"\\n\";\n\t}\n\t\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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000, 1\u2009\u2264\u2009m\u2009\u2264\u2009100\u2009000)\u00a0\u2014 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\u2009\u2009\u2264\u2009\u2009ui,\u2009\u2009vi\u2009\u2009\u2264\u2009\u2009n), 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\u00a0\u2014 the indices of vertices. Note that because of m\u2009\u2265\u20091, 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": "use feature 'state';\nuse experimental qw(autoderef postderef);\n\nsub input { $_ = <>; chomp; split }\n($n, $m) = input;\nfor (1..$n) {\n\t$conn[$_] = [];\n\t$mark[$_] = 0;\n}\nfor (1..$m) {\n\t($a, $b) = input;\n\tpush $conn[$a], $b;\n\tpush $conn[$b], $a;\n}\n\nsub find_start {\n\tstate $i;\n\tfor ($i++; $i <= $n; $i++) {\n\t\tif ($mark[$i] == 0 && $conn[$i]->@* > 0) {\n\t\t\treturn $i;\n\t\t}\n\t}\n\tundef;\n}\n\nwhile ($start = find_start) {\n\t@queue = ($start);\n\t$mark[$start] = 1;\n while (@queue) {\n \t$a = shift @queue;\n \t$mark_a = $mark[$a];\n \t$mark_b = ($mark_a == 1? 2: 1);\n \tfor $b ($conn[$a]->@*) {\n \t\tif ($mark[$b] == 0) {\n \t\t\t$mark[$b] = $mark_b;\n \t\t\tpush @queue, $b;\n \t\t} elsif ($mark[$b] != $mark_b) {\n \t\t\tprint -1;\n \t\t\texit;\n \t\t}\n \t}\n }\n}\n\nfor (1..$n) {\n\tpush @A, $_ if $mark[$_] == 1;\n\tpush @B, $_ if $mark[$_] == 2;\n}\nprint 0 + @A, \"\\n@A\\n\";\nprint 0 + @B, \"\\n@B\\n\";\n"}], "negative_code": [{"source_code": "use experimental qw(autoderef postderef);\n\nsub input { $_ = <>; chomp; split }\n($n, $m) = input;\nfor (1..$n) {\n\t$conn[$_] = [];\n\t$mark[$_] = 0;\n}\nfor (1..$m) {\n\t($a, $b) = input;\n\tpush $conn[$a], $b;\n\tpush $conn[$b], $a;\n}\n@queue = ($a);\n$mark[$a] = 1;\n\nwhile (@queue) {\n\t$a = shift @queue;\n\t$mark_a = $mark[$a];\n\t$mark_b = ($mark_a == 1? 2: 1);\n\tfor $b ($conn[$a]->@*) {\n\t\tif ($mark[$b] == 0) {\n\t\t\t$mark[$b] = $mark_b;\n\t\t\tpush @queue, $b;\n\t\t} elsif ($mark[$b] != $mark_b) {\n\t\t\tprint -1;\n\t\t\texit;\n\t\t}\n\t}\n}\n\nfor (1..$n) {\n\tpush @A, $_ if $mark[$_] == 1;\n\tpush @B, $_ if $mark[$_] == 2;\n}\nsub number { $a <=> $b }\n@A = sort number @A; \n@B = sort number @B;\nprint 0 + @A, \"\\n@A\\n\";\nprint 0 + @B, \"\\n@B\\n\";"}, {"source_code": "use feature 'state';\nuse experimental qw(autoderef postderef);\n\nsub input { $_ = <>; chomp; split }\n($n, $m) = input;\nfor (1..$n) {\n\t$conn[$_] = [];\n\t$mark[$_] = 0;\n}\nfor (1..$m) {\n\t($a, $b) = input;\n\tpush $conn[$a], $b;\n\tpush $conn[$b], $a;\n}\n\nsub find_start {\n\tstate $i;\n\tfor ($i++; $i <= $n; $i++) {\n\t\tif ($mark[$_] == 0 && $conn[$_]->@* > 0) {\n\t\t\treturn $_;\n\t\t}\n\t}\n\tundef;\n}\n\nwhile ($start = find_start) {\n\t@queue = ($start);\n\t$mark[$start] = 1;\n while (@queue) {\n \t$a = shift @queue;\n \t$mark_a = $mark[$a];\n \t$mark_b = ($mark_a == 1? 2: 1);\n \tfor $b ($conn[$a]->@*) {\n \t\tif ($mark[$b] == 0) {\n \t\t\t$mark[$b] = $mark_b;\n \t\t\tpush @queue, $b;\n \t\t} elsif ($mark[$b] != $mark_b) {\n \t\t\tprint -1;\n \t\t\texit;\n \t\t}\n \t}\n }\n}\n\nfor (1..$n) {\n\tpush @A, $_ if $mark[$_] == 1;\n\tpush @B, $_ if $mark[$_] == 2;\n}\nprint 0 + @A, \"\\n@A\\n\";\nprint 0 + @B, \"\\n@B\\n\";\n"}], "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\u2009+\u2009m. Then the number of integers i (1\u2009\u2264\u2009i\u2009<\u2009n\u2009+\u2009m) such that positions with indexes i and i\u2009+\u20091 contain children of different genders (position i has a girl and position i\u2009+\u20091 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100), separated by a space.", "output_spec": "Print a line of n\u2009+\u2009m 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": "#!/usr/bin/perl\nuse strict;\nuse warnings;\nopen(INPUT, \"< input.txt\");\nopen(OUTPUT, \"> output.txt\");\nmy ($n, $m) = map int, split /\\D/, ;\nif ($n > $m) {\n for my $i (1..$m) {\n\tprint OUTPUT \"BG\";\n }\n for my $i (1..$n-$m) {\n\tprint OUTPUT \"B\";\n }\n} else {\n for my $i (1..$n) {\n\tprint OUTPUT \"GB\";\n }\n for my $i (1..$m-$n) {\n\tprint OUTPUT \"G\";\n }\n}\n"}], "negative_code": [], "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$$$)\u00a0\u2014 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}$$$)\u00a0\u2014 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\u00a0\u2014 the answer to the $$$i$$$-th test case\u00a0\u2014 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": "use POSIX;\r\nmy $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\n\r\n#$line = ;\r\n#chomp($line);\r\n#@input2 = split(' ', $line);\r\n\r\nmy $line2;\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n @input2 = split(' ', $line);\r\n $s = @input2[1] % 4;\r\n if (@input2[0] % 2) {\r\n if ($s == 0) { print $input2[0], \"\\n\"; }\r\n if ($s == 1) { print $input2[0] + $input2[1], \"\\n\"; }\r\n if ($s == 2) { print $input2[0] - 1, \"\\n\";}\r\n if ($s == 3) { print $input2[0] - (@input2[1] + 1), \"\\n\"; }\r\n } else {\r\n if ($s == 0) { print $input2[0], \"\\n\"; }\r\n if ($s == 1) { print $input2[0] - $input2[1], \"\\n\"; }\r\n if ($s == 2) { print $input2[0] + 1, \"\\n\"; }\r\n if ($s == 3) { print $input2[1] + @input2[0] + 1, \"\\n\";}\r\n }\r\n}"}], "negative_code": [], "src_uid": "dbe12a665c374ce3745e20b4a8262eac"} {"nl": {"description": "Stanley has decided to buy a new desktop PC made by the company \"Monoblock\", and to solve captcha on their website, he needs to solve the following task.The awesomeness of an array is the minimum number of blocks of consecutive identical numbers in which the array could be split. For example, the awesomeness of an array $$$[1, 1, 1]$$$ is $$$1$$$; $$$[5, 7]$$$ is $$$2$$$, as it could be split into blocks $$$[5]$$$ and $$$[7]$$$; $$$[1, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9]$$$ is 3, as it could be split into blocks $$$[1]$$$, $$$[7, 7, 7, 7, 7, 7, 7]$$$, and $$$[9, 9, 9, 9, 9, 9, 9, 9, 9]$$$. You are given an array $$$a$$$ of length $$$n$$$. There are $$$m$$$ queries of two integers $$$i$$$, $$$x$$$. A query $$$i$$$, $$$x$$$ means that from now on the $$$i$$$-th element of the array $$$a$$$ is equal to $$$x$$$.After each query print the sum of awesomeness values among all subsegments of array $$$a$$$. In other words, after each query you need to calculate $$$$$$\\sum\\limits_{l = 1}^n \\sum\\limits_{r = l}^n g(l, r),$$$$$$ where $$$g(l, r)$$$ is the awesomeness of the array $$$b = [a_l, a_{l + 1}, \\ldots, a_r]$$$.", "input_spec": "In the first line you are given with two integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n, m \\leq 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$)\u00a0\u2014 the array $$$a$$$. In the next $$$m$$$ lines you are given the descriptions of queries. Each line contains two integers $$$i$$$ and $$$x$$$ ($$$1 \\leq i \\leq n$$$, $$$1 \\leq x \\leq 10^9$$$).", "output_spec": "Print the answer to each query on a new line.", "sample_inputs": ["5 5\n1 2 3 4 5\n3 2\n4 2\n3 1\n2 1\n2 2"], "sample_outputs": ["29\n23\n35\n25\n35"], "notes": "NoteAfter the first query $$$a$$$ is equal to $$$[1, 2, 2, 4, 5]$$$, and the answer is $$$29$$$ because we can split each of the subsegments the following way: $$$[1; 1]$$$: $$$[1]$$$, 1 block; $$$[1; 2]$$$: $$$[1] + [2]$$$, 2 blocks; $$$[1; 3]$$$: $$$[1] + [2, 2]$$$, 2 blocks; $$$[1; 4]$$$: $$$[1] + [2, 2] + [4]$$$, 3 blocks; $$$[1; 5]$$$: $$$[1] + [2, 2] + [4] + [5]$$$, 4 blocks; $$$[2; 2]$$$: $$$[2]$$$, 1 block; $$$[2; 3]$$$: $$$[2, 2]$$$, 1 block; $$$[2; 4]$$$: $$$[2, 2] + [4]$$$, 2 blocks; $$$[2; 5]$$$: $$$[2, 2] + [4] + [5]$$$, 3 blocks; $$$[3; 3]$$$: $$$[2]$$$, 1 block; $$$[3; 4]$$$: $$$[2] + [4]$$$, 2 blocks; $$$[3; 5]$$$: $$$[2] + [4] + [5]$$$, 3 blocks; $$$[4; 4]$$$: $$$[4]$$$, 1 block; $$$[4; 5]$$$: $$$[4] + [5]$$$, 2 blocks; $$$[5; 5]$$$: $$$[5]$$$, 1 block; which is $$$1 + 2 + 2 + 3 + 4 + 1 + 1 + 2 + 3 + 1 + 2 + 3 + 1 + 2 + 1 = 29$$$ in total."}, "positive_code": [{"source_code": "($n,$m)=split/ /,<>;@a=split/ /,<>;$q=0;$q+=($_+1)*($_<$n-1&&@a[$_]==@a[$_+1]?1:$n-$_)for(0..$n-1);for(1..$m){($_,$x)=split/ /,<>;$q-=(--$_+1)*($_<$n-1&&@a[$_]==@a[$_+1]?1:$n-$_)+$_*(@a[$_-1]==@a[$_]?1:$n-$_+1)*($_>0);@a[$_]=$x;$q+=($_+1)*($_<$n-1&&@a[$_]==@a[$_+1]?1:$n-$_)+$_*(@a[$_-1]==@a[$_]?1:$n-$_+1)*($_>0);print$q,\" \"}"}, {"source_code": "($n,$m)=split/ /,<>;@a=split/ /,<>;$q=0;$q+=($_+1)*($_<$n-1&&@a[$_]==@a[$_+1]?1:$n-$_)for(0..$n-1);for(1..$m){($_,$x)=split/ /,<>;$q-=(--$_+1)*($_<$n-1&&@a[$_]==@a[$_+1]?1:$n-$_)+($_)*(@a[$_-1]==@a[$_]?1:$n-$_+1)*($_>0);@a[$_]=$x;$q+=($_+1)*($_<$n-1&&@a[$_]==@a[$_+1]?1:$n-$_)+($_)*(@a[$_-1]==@a[$_]?1:$n-$_+1)*($_>0);print$q,\" \"}"}], "negative_code": [], "src_uid": "d70a774248d9137c30f33fb37c6467a7"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 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\u00a0\n2 1", "5 4\n\u00a0\n\u00a0\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": "use strict;\nuse warnings;\n\nuse feature qw/say/;\n{\n my $n = <>;\n chomp $n;\n my %has; \n my $temp_heap = $n;\n my @values;\n {\n local $/ = ' ';\n for (my $i=1;$i<=$n;$i++){\n if ($i == $n){\n $/ = \"\\n\";\n }\n my $str = <>;\n chomp $str;\n $has{$str} = 1;\n\n while (defined $has{$temp_heap} ){\n print $temp_heap . \" \";\n $temp_heap--;\n }\n print \"\\n\";\n }\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\n#open (my $F1, \"<\", \"a\") or die \"a: $!\";\n#my $num=0;\nmy %Q=();\nmy $num = <>; \nchomp($num);\nmy $line2 = <>; \nchomp($line2);\nmy @snacks = split (\" \", $line2);\nmy $len = scalar @snacks;\nif ($num != $len ) { print \"Error: $num != $len\\n\";exit(1);}\n#print \"num=$num\\n\";\n#print \"snacks:@snacks\\n\";\n#close($F1);\n##############################################\nmy $wait_for = $num;\nfor (my $i=0;$i < $num;$i++) {\n if ($wait_for == $snacks[$i] ) { \n print \"$wait_for\";\n $wait_for--;\n #while (my $ele = pop (@Q)) { \n while ($Q{$wait_for}) { \n #if ($wait_for == $ele) {\n print \" $wait_for\";\n $wait_for--;\n \n } \n print \"\\n\";\n } else {\n print \"\\n\";\n $Q{$snacks[$i]} = 1;\n #push (@Q,$snacks[$i]);\n } \n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tchomp;\n\t\n\t@_ = (-1, 1 .. $_);\n\t\n\tmy @A = (split ' ', <>);\n\t\n\tmy $last = $_;\n\t\n\tfor my $i ( @A ){\n\t\t\n\t\t$_[ $i ] = 0;\n\t\t\n\t\tmy @out;\n\t\t\n\t\twhile( $_[ $last ] == 0 ){\n\t\t\tpush @out, $last --\n\t\t\t}\n\t\t\t\n\t\tprint \"@out\";\n\t\t}\n\t\n\t\n\t$debug and print '-' x 10;\n\t}"}, {"source_code": "@_ = -1 .. <>;\n\nfor ( split ' ', <> ){\n\n\t$_[ $_ ] = -2;\n\t\n\tpop @_, print \"$#_ \" while -1 > $_[ @_-2 ];\n\t\t\n\tprint $/\n\t}"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\n\nuse feature qw/say/;\n{\n my $n = <>;\n chomp $n;\n \n my $temp_heap = $n+1;\n my @values;\n {\n local $/ = ' ';\n for (my $i=1;$i<=$n;$i++){\n if ($i == $n){\n $/ = \"\\n\";\n }\n my $str = <>;\n chomp $str;\n push @values, $str;\n @values = reverse sort @values;\n while (defined $values[0] && $values[0]+1 == $temp_heap ){\n my $new_heap = shift @values;\n print $new_heap . \" \";\n $temp_heap = $new_heap;\n }\n print \"\\n\";\n }\n }\n}\n"}, {"source_code": "@_ = -1 .. <>;\n\nfor ( split ' ', <> ){\n\n\t$_[ $_ ] = 0;\n\t\n\tpop @_, print \"$#_ \" while ! $_[ @_-2 ];\n\t\t\n\tprint $/\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tchomp;\n\t\n\t@_ = (-1, 1 .. $_);\n\t\n\tmy @A = (split ' ', <>);\n\t\n\tmy $c = $_;\n\tmy $last = $_;\n\t\n\tfor my $i ( reverse 1 .. @_ - 1 ){\n\t\tmy $num = shift @A;\n\t\t$_[ $num ] = 0;\n\t\tif( $num >= $i ){ $c-- }\n\t\t$debug and print \" $c\";\n\t\tif( $num != $i and $_[ $i ] == 0 ){ $c-- }\n\t\t$debug and print \" $c\";\n\t\tif( $c < $i ){\n\t\t\tprint join ' ', reverse $c + 1 .. $last;\n\t\t\t$last = $c;\n\t\t\t}\n\t\telse{\n\t\t\tprint ''\n\t\t\t}\n\t\t}\n\t\n\t\n\t$debug and print '-' x 10;\n\t}"}], "src_uid": "3d648acee2abbf834f865b582aa9b7bc"} {"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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my $k1 = $n % 2050;\r\n if( $k1 != 0 ){\r\n print \"-1\\n\"; next;\r\n }\r\n my $k2 = $n / 2050;\r\n my $r = 0;\r\n while( $k2 > 0 ){\r\n my $k3 = $k2 % 10;\r\n $r += $k3;\r\n $k2 = ($k2 - $k3)/10;\r\n }\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "src_uid": "f3e413954c9c02520fd25bd2cba4747e"} {"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,\u2009bj) can close marker (xi,\u2009yi) only if their diameters match, that is, bj\u2009=\u2009yi. Besides, a marker is considered to be beautifully closed, if the cap color and the marker color match, that is, aj\u2009=\u2009xi.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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009105) \u2014 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\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u20091000) \u2014 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\u2009\u2264\u2009aj,\u2009bj\u2009\u2264\u20091000) \u2014 the color and diameter of the j-th cap, correspondingly.", "output_spec": "Print two space-separated integers u,\u2009v, 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 \u2014 the first and the third markers."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($n,$m) = split /\\s+/, , -1;\n\nmy %pens;\nmy %caps;\n\nfor (my $i = 0; $i < $n; $i++) {\n my ($color,$dia) = split /\\s+/, , -1;\n $pens{$dia + 0}{$color + 0}++;\n $pens_nc{$dia + 0}++;\n}\n\nfor (my $i = 0; $i < $m; $i++) {\n my ($color,$dia) = split /\\s+/, , -1;\n $caps{$dia + 0}{$color + 0}++;\n $caps_nc{$dia + 0}++;\n}\n\nmy $beauty = 0;\nforeach my $dia (keys %pens) {\n my $mpens = $pens{$dia};\n foreach my $color (keys %$mpens) {\n\tmy $npens = $mpens->{$color};\n\tif (!$npens) { next; }\n\tmy $ncups = $caps{$dia}{$color};\n\tif (!$ncups) { next; }\n\n\tmy $min = ($npens < $ncups) ? $npens : $ncups;\n\n\t$beauty += $min;\n\t$caps{$dia}{$color} -= $min;\n\t$mpens->{$color} -= $min;\n\t$caps_nc{$dia} -= $min;\n\t$pens_nc{$dia} -= $min;\n }\n}\n\nmy $ok = 0;\nforeach my $dia (keys %pens_nc) {\n my $npens = $pens_nc{$dia};\n if (!$npens) { next; }\n my $ncups = $caps_nc{$dia};\n if (!$ncups) { next; }\n\n my $min = ($npens < $ncups) ? $npens : $ncups;\n $ok += $min;\n}\n\nprint $ok + $beauty . \" \" . $beauty . \"\\n\";\n"}], "negative_code": [], "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\u00a0\u2014 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009200\u2009000)\u00a0\u2014 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": "<>;\n$A = <>;\n$az = join '', a .. z;\n($r, $t) = split, $az =~ s/[$r$t]/$& eq $r? $t : $r/ge for <>;\n$z = 'a';\n%h = map { $z ++, $_ } split //, $az;\n$A =~ s/./$h{$&}/ge;\nprint $A"}, {"source_code": "#!/usr/bin/perl\n\n($n, $m) = split /\\s/, <>;\n\n$name = <>;\nchomp $name;\n\n@name_letters = split //, $name;\n \nfor my $i (0..$#name_letters)\n{\n $h{@name_letters[$i]} = [] if ! exists $h{@name_letters[$i]};\n \n push @{$h{@name_letters[$i]}}, $i;\n}\n\nfor (1..$m)\n{\n my $xy = <>;\n chomp $xy;\n (my $x, my $y) = split /\\s/, $xy;\n\n if($x ne $y)\n {\n if(exists $h{$x} and exists $h{$y})\n {\n my $ar_ref = $h{$x};\n $h{$x} = $h{$y};\n $h{$y} = $ar_ref;\n }\n elsif(exists $h{$x})\n {\n my $ar_ref = $h{$x};\n delete $h{$x};\n $h{$y} = $ar_ref;\n }\n elsif(exists $h{$y})\n {\n my $ar_ref = $h{$y};\n delete $h{$y};\n $h{$x} = $ar_ref;\n }\n }\n}\n\nfor my $letter (keys %h)\n{\n for my $i (@{$h{$letter}})\n {\n @name_letters[$i] = $letter;\n }\n}\n\n$name = join '', @name_letters;\n\nprint $name, \"\\n\"; \n"}, {"source_code": "<>;\n$A = <>;\n$az = join '', a .. z;\n($r, $t) = split, $az =~ s/[$r$t]/$& eq $r? $t : $r/ge for <>;\n$z = 'a';\n%h = map { $z ++, $_ } split //, $az;\n$A =~ s/./$h{$&}/ge;\nprint $A"}], "negative_code": [], "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\u2009=\u20094 the sum is equal to \u2009-\u20091\u2009-\u20092\u2009+\u20093\u2009-\u20094\u2009=\u2009\u2009-\u20094, 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\u2009\u2264\u2009t\u2009\u2264\u2009100) \u2014 the number of values of n to be processed. Each of next t lines contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009109).", "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": "#!/usr/bin/perl\n\nuse 5.010;\nuse strict;\nuse warnings;\nuse bignum;\n\n\nmy $count=<>;\nmy $sum;\nmy $temp;\n\nfor (my $i=0;$i<$count;$i+=1)\n{\n\t$temp=1;\n\tmy $number=<>;\n\t$sum = (1 + $number) * ($number / 2);\n\n\twhile ($temp <= $number)\n\t{\n\t\t$sum = $sum - ($temp * 2);\n\t\t$temp = $temp * 2;\n\t}\n\tprint $sum ;\n\tprint \"\\n\" ;\n}\n\n\n"}, {"source_code": "#!/usr/bin/perl -w\nuse bigint;\n\nsub doit {\n my $n = $_[0];\n my $next_power_of_two = 1;\n while ($next_power_of_two <= $n) {\n $next_power_of_two *= 2;\n }\n $n * ($n + 1) / 2 - 2 * ($next_power_of_two - 1);\n}\n\nmy $t = <>;\nfor (1..$t) {\n my $n = <>;\n print doit($n), \"\\n\";\n}\n"}, {"source_code": "#!perl\nuse bigint;\n\n$\\ = \"\\n\";\n\n$n = <>;\n\nfor (1..$n)\n{\n $a = <>;\n \n chomp $a;\n $str = unpack(\"B32\", pack(\"N\", $a));\n $str =~ m/^0*(1\\d*)/;\n $n1 = '1'.('0'x ((length $1) - 1));\n $b = unpack(\"N\", pack(\"B32\", substr(\"0\" x 32 . $n1, -32)));\n \n $ar_sum = (1 + $a)*$a/2;\n $ge_sum = (2*$b - 1);\n push @result, $ar_sum - 2*$ge_sum;\n}\n\nfor my $item (@result)\n{\n print $item;\n}\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $case = ;\n\nfor my $i(0..$case - 1){\n\tmy $num = ;\n\tmy $sum = 0;\n\tmy $exp = 0;\n\t\n\t$sum = ($num * ($num + 1)) >> 1;\n\tfor my $j(0..$num - 1){\n\t\tif($num < (1 << $j)){\n\t\t\t$exp = $j;\n\t\t\tlast;\n\t\t}\n\t}\n\t\n\t$sum -= (2 * ((1 << $exp) - 1));\n\t\n\tif($num == 1){\n\t\tprint \"-1\\n\";\n\t}elsif($num == 2){\n\t\tprint \"-3\\n\";\n\t}else{\n\t\tprint $sum, \"\\n\";\n\t}\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse 5.010;\nuse strict;\nuse warnings;\n\n\nmy $count=<>;\nmy $sum;\nmy $temp;\n\nfor (my $i=0;$i<$count;$i+=1)\n{\n\t$temp=1;\n\tmy $number=<>;\n\t$sum = (1 + $number) * ($number / 2);\n\n\twhile ($temp <= $number)\n\t{\n\t\t$sum = $sum - ($temp * 2);\n\t\t$temp = $temp * 2;\n\t}\n\tprint $sum ;\n\tprint \"\\n\" ;\n}\n\n\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $case = ;\n\nfor my $i(0..$case){\n\tmy $num = ;\n\tmy $sum = 0;\n\tmy $exp = 0;\n\t\n\t$sum = ($num * ($num + 1)) >> 1;\n\tfor my $j(0..$num - 1){\n\t\tif($num < (1 << $j)){\n\t\t\t$exp = $j;\n\t\t\tlast;\n\t\t}\n\t}\n\t\n\t$sum -= (2 * ((1 << $exp) - 1));\n\t\n\tif($num == 1){\n\t\tprint \"-1\\n\";\n\t}elsif($num == 2){\n\t\tprint \"-3\\n\";\n\t}else{\n\t\tprint $sum, \"\\n\";\n\t}\n}"}, {"source_code": "use strict;\nuse warnings;\n\nmy $case = ;\n\nfor my $i(0..$case){\n\tmy $num = ;\n\tmy $sum = 0;\n\tmy $exp = 0;\n\t\n\t$sum = ($num * ($num + 1)) >> 1;\n\tfor my $j(0..$num){\n\t\tif($num < (1 << $j)){\n\t\t\t$exp = $j;\n\t\t\tlast;\n\t\t}\n\t}\n\t\n\t$sum -= (2 * ((1 << $exp) - 1));\n\t\n\tif($num == 1){\n\t\tprint \"-1\\n\";\n\t}elsif($num == 2){\n\t\tprint \"-3\\n\";\n\t}else{\n\t\tprint $sum, \"\\n\";\n\t}\n}"}], "src_uid": "a3705f29b9a8be97a9e9e54b6eccba09"} {"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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 2\\cdot10^5$$$)\u00a0\u2014 the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\leq a_i \\leq 10^9$$$)\u00a0\u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$_ = <>;\r\n\t\r\n\t1 while s/\\b(\\d+)\\K \\1\\b//g;\r\n\t\r\n\tprint /\\b(\\d+) (\\d+) (\\d+)\\b(??{ z x not $1 < $2 && $2 > $3 })/ ? NO : YES\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @squeezed;\n\t\n\tmy $last = -1;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tnext if $_[ $i ] == $last;\n\t\tpush @squeezed, $_[ $i ];\n\t\t$last = $_[ $i ];\n\t\t}\n\t\n\t@_ = @squeezed;\n\t\n\tmy $fail = 0;\n\t\n\tfor my $i ( 0 .. @_ - 3 ){\n\t\t$fail ||= ( $_[ $i ] < $_[ $i + 1 ] and $_[ $i + 1 ] > $_[ $i + 2 ] );\n\t\t}\n\t\n\tprint $fail ? 'NO' : 'YES';\n\t}"}], "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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $del = 0;\n\t\n\t@_ = ();\n\t\n\tfor( split // ){\n\t\tpush @_, $_;\n\t\tif( @_ > 1 and $_[ -2 ] eq $_[ -1 ] ){\n\t\t\tpop @_ for 1 .. 2;\n\t\t\t$del ++;\n\t\t\t}\n\t}\n\t\n\tprint $del % 2 ? \"Yes\" : \"No\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $del = 0;\n\t\n\t@_ = ();\n\t\n\tfor( split // ){\n\t\tif( @_ > 0 and $_ eq $_[ -1 ] ){\n\t\t\tpop @_;\n\t\t\t$del ++;\n\t\t\t}\n\t\telse{\n\t\t\tpush @_, $_;\n\t\t\t}\n\t}\n\t\n\tprint $del % 2 ? \"Yes\" : \"No\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $del = 0;\n\t\n\tmy $pos = pos;\n\t\n\t( pos ) = $pos < 0 ? 0 : $pos while s/\\G.*?\\K(.)\\1(?{ $del ++; $pos = ( pos ) - 3; })//x;\n\t\n\tprint $del % 2 ? \"Yes\" : \"No\";\n\t}"}, {"source_code": "$_ = <>;\n\n( pos ) = $pos < 0 ? 0 : $pos while s/\\G.*?\\K(.)\\1(?{ $del ++; $pos = ( pos ) - 3; })//x;\n\nprint $del % 2 ? \"Yes\" : \"No\""}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $del = 0;\n\t\n\tmy $pos = pos;\n\t\n\t( pos ) = $pos while s/\\G.*?\\K(.)\\1(?{ $del ++; $pos = ( pos ) - 3; })//x;\n\t\n\tprint $del % 2 ? \"Yes\" : \"No\";\n\t}"}], "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\u2009\u2264\u2009n\u2009\u2264\u20091000, 1\u2009\u2264\u2009l\u2009\u2264\u2009109)\u00a0\u2014 the number of lanterns and the length of the street respectively. The next line contains n integers ai (0\u2009\u2264\u2009ai\u2009\u2264\u2009l). 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\u2009-\u20099.", "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\u2009=\u20092 the first lantern will light the segment [0,\u20094] of the street, and the second lantern will light segment [3,\u20095]. Thus, the whole street will be lit."}, "positive_code": [{"source_code": "use List::Util \"max\";\n\n($n, $l) = split \" \", <>;\n@arr = sort { $a <=> $b } split \" \", <>;\n$dist = max ($arr[0], $l - $arr[-1]);\n\nfor (1 .. $n - 1) {\n $dist = max ($dist, ($arr[$_] - $arr[$_ - 1]) / 2);\n}\nprintf (\"%.10f\\n\", $dist);"}, {"source_code": "my ($n, $m) = split / /,<>;\nmy @arr = sort {$a <=> $b} map {$_ = int$_} split / /, <>;\n@arr = (-$arr[0], @arr, $m + $m - $arr[-1]);\nmy $max = 0;\nfor(1..$n+1) {\n $max = $arr[$_] - $arr[$_ - 1] if ($arr[$_] - $arr[$_ - 1] > $max);\n}\nprint $max / 2;\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n# use List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nsub max {\n my ($x, $y) = (shift, shift);\n return $x>$y ? $x:$y;\n}\n\n($n, $l) = split / /, <>;\n@a = sort {$a <=> $b} split / /, <>;\n$ans = max ($a[0]*2, ($l-$a[-1])*2);\n$ans = max $ans,$a[$_]-$a[$_-1] foreach (1..$n-1);\nsay $ans/2;"}, {"source_code": "sub uniq {\n my %seen;\n grep !$seen{$_}++, @_;\n}\nchomp($line=);\n($n,$l)=split(\" \",$line);\nchomp($line2=);\n@arr=split(\" \",$line2);\n@arr=uniq(@arr);\n@arr=sort{$a<=>$b}@arr;\n$max=0;\n$len=@arr;\nfor($i=0;$i<$len-1;$i++)\n\t{\n\t$d=$arr[$i+1]-$arr[$i];\n\tif($d>$max)\n\t\t{\n\t\t$max=$d;\n\t\t}\n\t}\n$max=$max/2;\nif($arr[0]!=0)\n\t{\n\t$c1=$arr[0];\n\t}\nif($c1>$max){$max=$c1;}\nif($arr[$len-1]!=$l)\n\t{\n\t$c2=$l-$arr[$len-1];\n\t}\nif($c2>$max){$max=$c2;}\nif($max==0){$max=$l;}\nprintf \"%.10f\", $max;\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\n\nmy $n; my $l;\nmy @num;\nsub valid{\n my $d = shift;\n my $last = 0;\n for my $i(0..$n-1){\n return 0 if $num[$i]-$d > $last;\n $last = $last > $num[$i] + $d ? $last : $num[$i] + $d;\n }\n return $last >= $l;\n}\n\n($n,$l) = split ' ',<>;\n@num = split ' ',<>;\n@num = sort {$a <=> $b} @num;\nmy ($start,$len) = (0,$l+1);\nwhile($len > 1e-11){\n $len /= 2.0;\n my $mid = $start + $len;\n $start = $mid if(!valid($mid)) ;\n}\n\nprintf \"%0.11f\\n\",$start;\n"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$l)=split;\n\t@_ = sort {$a <=> $b} split/ /,\"\".<>;\n\tchomp @_;\n\tunshift @_, -$_[0];\n\tpush @_, $l + ($l - $_[-1]);\n#\tprint \"@_\",$/;\n\t@d=();\n\tfor $i(1..@_-1){\n\t\tpush @d, $_[$i]-$_[$i-1];\n\t\t}\n\t@d = sort {$a <=> $b} @d;\t\n#\tprint \"@d\",$/;\n\tprint $d[-1]/2,$/;\n\t}"}], "negative_code": [{"source_code": "use List::Util \"max\";\n\n($n, $l) = split \" \", <>;\n@arr = sort { $a <=> $b } split \" \", <>;\n$dist = max ($arr[0], $l - $arr[-1]);\n\nfor (1 .. $n - 2) {\n $dist = max ($dist, ($arr[$_] - $arr[$_ - 1]) / 2);\n}\nprintf (\"%.10f\\n\", $dist);"}, {"source_code": "my ($n, $m) = split / /,<>;\nmy @arr = sort {$a <=> $b} map {$_ = int$_} ((split / /, <>) , $m);\nmy $max = 0;\nfor(1..$n) {\n $max = $arr[$_] - $arr[$_ - 1] if ($arr[$_] - $arr[$_ - 1] > $max);\n}\nprint $max / 2;"}, {"source_code": "my ($n, $m) = split / /,<>;\nmy @arr = sort {$a <=> $b} map {$_ = int$_} split / /, <>;\n@arr = (-$arr[0], @arr, $m + $m - $arr[-1]);\nmy $max = 0;\nfor(1..$n) {\n $max = $arr[$_] - $arr[$_ - 1] if ($arr[$_] - $arr[$_ - 1] > $max);\n}\nprint $max / 2;"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n#use List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nsub max {\n my ($x, $y) = (shift, shift);\n return $x>$y ? $x:$y;\n}\n\n($n, $l) = split / /, <>;\n@a = sort {$a <=> $b} split / /, <>;\n$ans = max ($a[0]*2, (l-$a[-1])*2);\n$ans = max ($ans,$a[$_]-$a[$_-1]) foreach (1..$n-1);\nsay $ans/2;"}, {"source_code": "sub uniq {\n my %seen;\n grep !$seen{$_}++, @_;\n}\nchomp($line=);\n($n,$l)=split(\" \",$line);\nchomp($line2=);\n@arr=split(\" \",$line2);\n@arr=uniq(@arr);\n@arr=sort{$a<=>$b}@arr;\n$max=0;\n$len=@arr;\nfor($i=0;$i<$len-1;$i++)\n\t{\n\t$d=$arr[$i+1]-$arr[$i];\n\tif($d>$max)\n\t\t{\n\t\t$max=$d;\n\t\t}\n\t}\n$max=$max/2;\nif($arr[0]!=0)\n\t{\n\t$c1=$arr[0];\n\t}\nif($c1>$max){$max=$c1;}\nif($arr[$len-1]!=$l)\n\t{\n\t$c2=$arr[$len-1];\n\t}\nif($c2>$max){$max=$c2;}\nprintf \"%.10f\", $max;\n"}, {"source_code": "sub uniq {\n my %seen;\n grep !$seen{$_}++, @_;\n}\nchomp($line=);\n($n,$l)=split(\" \",$line);\nchomp($line2=);\n@arr=split(\" \",$line2);\n@arr=uniq(@arr);\n@arr=sort{$a<=>$b}@arr;\n$max=0;\n$len=@arr;\nfor($i=0;$i<$len-1;$i++)\n\t{\n\t$d=$arr[$i+1]-$arr[$i];\n\tif($d>$max)\n\t\t{\n\t\t$max=$d;\n\t\t}\n\t}\n$max=$max/2;\nif($arr[0]!=0)\n\t{\n\t$c1=$arr[0];\n\t}\nif($c1>$max){$max=$c1;}\nif($arr[$len-1]!=$l)\n\t{\n\t$c2=$arr[$len-1];\n\t}\nif($c2>$max){$max=$c2;}\nif($max==0){$max=$l;}\nprintf \"%.10f\", $max;\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $n; my $l;\nmy @num;\nsub valid{\n my $d = shift;\n my $last = 0;\n for my $i(0..$n-1){\n return 0 if $num[$i]-$d > $last;\n $last = $last > $num[$i] + $d ? $last : $num[$i] + $d;\n }\n return $last >= $l;\n}\n\n($n,$l) = split ' ',<>;\n@num = split ' ',<>;\n@num = sort {$a <=> $b} @num;\nmy ($low,$high) = (0,$l+1);\nwhile($high - $low > 1e-8){\n my $mid = ($low + $high) / 2;\n if(valid($mid)){\n $high = $mid;\n }\n else{\n $low = $mid;\n }\n}\n\nprintf \"%0.11 f\\n\",$low;\n"}], "src_uid": "d79166497eb61d81fdfa4ef80ec1c8e8"} {"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 \u2014 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 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u200920000) \u2014 the initial number of sculptures. The second line contains a sequence of integers t1,\u2009t2,\u2009...,\u2009tn, ti \u2014 the degree of the i-th sculpture's attractiveness (\u2009-\u20091000\u2009\u2264\u2009ti\u2009\u2264\u20091000). 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 \u0438 3."}, "positive_code": [{"source_code": "#!perl\nsub sum{\n\tmy $step = shift;\n\tmy $start = shift;\n\tmy $s = 0;\n\tfor(my $i = $start; $i <= $#_; $i += $step){\n\t\t$s += $_[$i];\n\t}\n\t$s;\n}\nmy $n = ;\nif ($n <= 2) {\n\tdie \"wrong input\\n\";\n}\nmy @possible;\n$m = sqrt($n);\nfor (my $i = 2; $i <= $m; $i++) {\n\tif (0 == $n % $i) {\n\t\t$d = int $n / $i;\n\t\tpush @possible, $i if $d >= 3;\n\t\tpush @possible, $d if $i >= 3;\n\t}\n}\n\nmy @statues = map {int $_} grep {/^-?\\d+$/} split / +/, ;\n@statues = @statues[0..($n-1)];\nmy $max = sum 1, 0, @statues;\nfor my $k(@possible){\n\tfor (0..($k-1)) {\n\t\tmy $s = sum $k, $_, @statues;\n\t\t$max = $s if ($s > $max);\n\t}\n}\nprint $max.\"\\n\";\n"}, {"source_code": "use strict;\n\nchomp(my $n = <>);\nmy @a = split / /, <>;\n\nmy $res = -1e9;\n\nfor (my $i = 1; $i <= $n / 3; $i++) {\n $n % $i == 0 or next;\n for (my $j = 0; $j < $i; ++$j) {\n my $t = 0;\n for (my $x = $j; $x < $n; $x += $i) {\n $t += $a[$x];\n }\n $res = $res > $t ? $res : $t;\n }\n}\n\nprint $res, \"\\n\";"}, {"source_code": "#!/usr/bin/perl\n# Codeforces Practice - 26 May 2012\nuse strict;\nuse warnings;\nuse Math::Complex;\n\nmy $n = ;\nchomp($n);\n\nmy @tis = split(\" \",);\n\nmy $tSum =0;\n$tSum += $_ for @tis; #summing up the array\n\nif($n<6) {\n print $tSum;\n}\n\nelse {\n my @sums = ($tSum);\n my $count;\n \n #................ Calculate factors of $n\n \n my @factors;\n\n for(2..sqrt $n){\n if($n % $_ == 0){\n my $div = $n/$_;\n push(@factors,$_);\n unless($div == $_){\n push(@factors,$div);\n }}}\n\n @factors = sort {$a<=>$b} @factors;\n\n #................ Finished calculating factors of $n\n\n foreach my $factor (@factors){\n next if ($n / $factor < 3);\n my @factSums = ();\n my $count = 0;\n foreach my $t (@tis){\n @factSums[$count++ % $factor] += $t;\n }\n @sums = (@sums,@factSums);\n }\n \n my $max = $sums[0];\n$_ > $max and $max = $_ \n for @sums;\n \n \nprint $max;\n}\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\nuse List::Util qw(sum max);\n\nmy $n = <>;\nmy @t = split ' ', <>;\n\nmy @foos = ();\n\nfor my $m (1 .. $n / 3) {\n next unless $n % $m == 0;\n for my $i (0 .. $m - 1) {\n my @foo = map { $t[$i + $m * $_] } (0 .. $n / $m - 1);\n push @foos, sum @foo;\n }\n}\n\nprint max @foos;\n"}, {"source_code": "sub game($$){\n my $r=($_[0]-1)/$_[1];\n while($r>=1){\n if ($r == 1){\n return 1;\n }\n $r/=$_[1];\n }\n return 0;\n}\n$\\=$/;\nmy $max=0;\n$n=;\nmy $maxn=0;\n@sculptures=(0,split(\" \",));\n\n$pointSTOP=$n/3;\n\nfor ($index=1;$index<$n+1;$index++){\n \n $max+=$sculptures[$index];\n\n}\n#print \"pointSTOP=\".$pointSTOP.\" n=\".$n.\"\\n\";\nfor ($delims=2;(($delims<=$pointSTOP));$delims++){\n if (($n%$delims==0)){\n# print \"delims=\".\"$delims\";\n \n for ($index=1;(($index<=$delims));$index++){\n #if (game($index,$delims)==0){\n #print \" index=\".\"$index\";\n $maxn=0;\n for ($subindex=$index-1;$subindex<$index+$n-1;($subindex+=$delims)){\n # print \" subindex=\".$subindex.\" delims=\".$delims.\" sculpturerank=\".$sculptures[$subindex%($n)+1];\n $maxn+=$sculptures[$subindex%($n)+1];\n # print \" maxn=\".$maxn;\n }\n #print \" max=\".$max.\" maxn=\".$maxn;\n if ($max<$maxn){\n $max=$maxn;\n }\n #}\n }\n }\n \n}\n\nprint $max;\n"}, {"source_code": "my ($n) = split(/\\s/, );\n\nmy @t = split(/\\s/, );\n\nsub calc {\n my (@a) = @_;\n my $sum = 0;\n for my $e (@a) {\n $sum += $e;\n }\n if (scalar @a % 2 == 0 && scalar @a > 4) {\n my @b;\n my @c;\n for (my $i = 0; $i < scalar @a; $i++) {\n if ($i % 2 == 0) {\n push(@b, $a[$i]);\n } else {\n push(@c, $a[$i]);\n }\n }\n my $odd_sum = calc(@b);\n my $even_sum = calc(@c);\n $sum = $odd_sum if ($odd_sum > $sum);\n $sum = $even_sum if ($even_sum > $sum);\n }\n return $sum;\n}\n\nprint calc(@t);"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n# Codeforces Practice - 26 May 2012\nuse strict;\nuse warnings;\n\n\nmy $n = ;\nchomp($n);\n\nmy @tis = split(\" \",);\nmy $tSum =0;\n$tSum += $_ for @tis;\nif($n%2==1 || $n<6) {print $tSum;}\nelse {\nmy ($sum1,$sum2) = (0,0);\nmy $toggle = 0;\nforeach my $t (@tis) {\n if($toggle){\n $sum1 += $t;\n } else {\n $sum2 += $t;\n }\n $toggle = not $toggle;\n}\nif($sum1 >= $tSum && $sum1 >= $sum2){print $sum1;}\nelsif($sum2 >= $tSum && $sum2 >= $sum1){print $sum2;}\nelse {print $tSum;}\n}\n"}, {"source_code": "#!/usr/bin/perl\n# Codeforces Practice - 26 May 2012\nuse strict;\nuse warnings;\n\n\nmy $n = ;\nchomp($n);\n\nmy @tis = split(\" \",);\nmy $tSum =0;\n$tSum += $_ for @tis;\nif($n%2==1) {print $tSum;}\nelse {\nmy ($sum1,$sum2) = (0,0);\nmy $toggle = 0;\nforeach my $t (@tis) {\n if($toggle){\n $sum1 += $t;\n } else {\n $sum2 += $t;\n }\n $toggle = not $toggle;\n}\nif($sum1 >= $tSum && $sum1 >= $sum2){print $sum1;}\nelsif($sum2 >= $tSum && $sum2 >= $sum1){print $sum2;}\nelse {print $tSum;}\n}\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\nuse List::Util qw(sum max);\n\nmy $n = <>;\nmy @t = split ' ', <>;\n\nmy @foos = ();\n\nfor my $m (1 .. $n) {\n next unless $n % $m == 0;\n for my $i (0 .. $m - 1) {\n my @foo = map { $t[$i + $m * $_] } (0 .. $n / $m - 1);\n push @foos, sum @foo;\n }\n}\n\nprint max @foos;\n"}, {"source_code": "sub game($$){\n my $r=($_[0]-1)/$_[1];\n while($r>=1){\n if ($r == 1){\n return 1;\n }\n $r/=$_[1];\n }\n return 0;\n}\n$\\=$/;\nmy $max=0;\n$n=;\nmy $maxn=0;\n@sculptures=(0,split(\" \",));\n\n$pointSTOP=$n/3;\n\nfor ($index=1;$index<$n+1;$index++){\n \n $max+=$sculptures[$index];\n\n}\n#print \"pointSTOP=\".$pointSTOP.\" n=\".$n.\"\\n\";\nfor ($delims=2;(($delims<$pointSTOP));$delims++){\n if (($n%$delims==0)){\n# print \"delims=\".\"$delims\";\n \n for ($index=1;(($index<=$delims));$index++){\n #if (game($index,$delims)==0){\n #print \" index=\".\"$index\";\n $maxn=0;\n for ($subindex=$index-1;$subindex<$index+$n-1;($subindex+=$delims)){\n # print \" subindex=\".$subindex.\" delims=\".$delims.\" sculpturerank=\".$sculptures[$subindex%($n)+1];\n $maxn+=$sculptures[$subindex%($n)+1];\n # print \" maxn=\".$maxn;\n }\n #print \" max=\".$max.\" maxn=\".$maxn;\n if ($max<$maxn){\n $max=$maxn;\n }\n #}\n }\n }\n \n}\n\nprint $max;\n"}, {"source_code": "sub game($$){\n my $r=($_[0]-1)/$_[1];\n while($r>=1){\n if ($r == 1){\n return 1;\n }\n $r/=$_[1];\n }\n return 0;\n}\n$\\=$/;\nmy $max=0;\n$n=;\nmy $maxn=0;\n@sculptures=(0,split(\" \",));\n\n$pointSTOP=$n/3+1;\n\nfor ($index=1;$index<$n+1;$index++){\n \n $max+=$sculptures[$index];\n\n}\n#print \"pointSTOP=\".$pointSTOP.\" n=\".$n.\"\\n\";\nfor ($delims=2;(($delims<$pointSTOP));$delims++){\n if (($n%$delims==0)){\n #print \"delims=\".\"$delims\";\n \n for ($index=1;(($index<=$delims));$index++){\n #if (game($index,$delims)==0){\n # print \" index=\".\"$index\";\n $maxn=0;\n for ($subindex=$index-1;$subindex<$index+$n-1;($subindex+=$delims)){\n #print \" subindex=\".$subindex.\" delims=\".$delims.\" sculpturerank=\".$sculptures[$subindex%($n)+1];\n $maxn+=$sculptures[$subindex%($n)+1];\n #print \" maxn=\".$maxn;\n }\n #print \" max=\".$max.\" maxn=\".$maxn;\n if ($max<$maxn){\n $max=$maxn;\n }\n #}\n }\n }\n \n}\n\nprint $max;\n"}, {"source_code": "use strict;\n\nmy ($n) = split(/\\s/, );\n\nmy @t = split(/\\s/, );\n\nsub calc {\n my (@a) = @_;\n my $sum = 0;\n for my $e (@a) {\n $sum += $e;\n }\n if (scalar @a % 2 == 0) {\n my @b;\n my @c;\n for (my $i = 0; $i < scalar @a; $i++) {\n if ($i % 2 == 0) {\n push(@b, $a[$i]);\n } else {\n push(@c, $a[$i]);\n }\n }\n my $sum_even = calc(@b);\n my $sum_odd = calc(@c);\n $sum = $sum_even if ($sum < $sum_even);\n $sum = $sum_odd if ($sum < $sum_odd);\n }\n return $sum;\n}\n\nprint calc(@t) . \"\\n\";"}, {"source_code": "my ($n) = split(/\\s/, );\n\nmy @t = split(/\\s/, );\n\nmy @res = ();\n\nsub calc {\n my (@a) = @_;\n my $sum = 0;\n for my $e (@a) {\n $sum += $e;\n }\n push (@res, $sum);\n if (scalar @a % 2 == 0) {\n my @b;\n my @c;\n for (my $i = 0; $i < scalar @a; $i++) {\n if ($i % 2 == 0) {\n push(@b, $a[$i]);\n } else {\n push(@c, $a[$i]);\n }\n }\n calc(@b);\n calc(@c);\n }\n}\n\ncalc(@t);\n\nmy $max = $res[0];\nfor $e (@res) {\n $max = $e if ($max < $e);\n}\n\n\nprint $max;"}], "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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\nmy $MOD = 998244353;\n\nwhile(<>){\n $debug and print '-' x 15;\n \n chomp;\n my $len = $_;\n\t$_ = <>, chomp;\n\t$debug and print;\n \n /./;\n my $first = $&;\n\t/.$/;\n\tmy $last = $&;\n \n\tmy $L = 0;\n\tmy $R = 0;\n \n\twhile( s/^$first+// ){\n $L += length $&;\n }\n \n\t$_ = reverse;\n \n\twhile( s/^$last+// ){\n $R += length $&;\n }\n \n $debug and print \" L:$L, R:$R\";\n \n print do {\n \tif( $R == 0 ){\n $len * ( $len + 1 ) / 2 % $MOD;\n }\n \telsif( $first eq $last ){\n ( $L + 1 ) * ( $R + 1 ) % $MOD;\n }\n else{\n $L + $R + 1;\n }\n };\n }"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\nmy $MOD = 998244353;\n\nwhile(<>){\n $debug and print '-' x 15;\n \n chomp;\n my $len = $_;\n\t$_ = <>, chomp;\n\t$debug and print;\n \n /./;\n my $first = $&;\n\t/.$/;\n\tmy $last = $&;\n \n\tmy $L = 0;\n\tmy $R = 0;\n \n\twhile( s/^$first+// ){\n $L += length $&;\n }\n \n\t$_ = reverse;\n \n\twhile( s/^$last+// ){\n $R += length $&;\n }\n \n $debug and print \" L:$L, R:$R\";\n \n print do {\n \tif( $R == 0 ){\n $len * ( $len + 1 ) / 2 % $MOD;\n }\n \telsif( $first eq $last ){\n ( $L + $R ) * ( $L + $R + 1 ) / 2 % $MOD;\n }\n else{\n $L + $R + 1;\n }\n };\n }"}], "src_uid": "9693f60fc65065d00a1899df999405fe"} {"nl": {"description": "Little boy Petya loves stairs very much. But he is bored from simple going up and down them \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009109, 0\u2009\u2264\u2009m\u2009\u2264\u20093000) \u2014 the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1,\u2009d2,\u2009...,\u2009dm (1\u2009\u2264\u2009di\u2009\u2264\u2009n) \u2014 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": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=split/ /,<>;\n@_=sort num @_;\n\nfor $i(0..@_-3){\n\tif ($_[$i]==$_[$i+1]-1 and $_[$i+1]==$_[$i+2]-1){ $F++ ;last}\n\t}\n\t\nfor (@_){$_==1 || $_==$` and $F++}\n\nprint $F? \"NO\":\"YES\";\nn();\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [], "src_uid": "422cbf106fac3216d58bdc8411c0bf9f"} {"nl": {"description": "Wilbur the pig is tinkering with arrays again. He has the array a1,\u2009a2,\u2009...,\u2009an initially consisting of n zeros. At one step, he can choose any index i and either add 1 to all elements ai,\u2009ai\u2009+\u20091,\u2009... ,\u2009an or subtract 1 from all elements ai,\u2009ai\u2009+\u20091,\u2009...,\u2009an. His goal is to end up with the array b1,\u2009b2,\u2009...,\u2009bn. 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\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the length of the array ai. Initially ai\u2009=\u20090 for every position i, so this array is not given in the input. The second line of the input contains n integers b1,\u2009b2,\u2009...,\u2009bn (\u2009-\u2009109\u2009\u2264\u2009bi\u2009\u2264\u2009109).", "output_spec": "Print the minimum number of steps that Wilbur needs to make in order to achieve ai\u2009=\u2009bi 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\n$s = abs;\n\n$s += abs $2 - $1 while /(\\S+)(?= (\\S+))/g;\n\nprint $s\n"}, {"source_code": "$_ = (<>,<>);\n\n$s = abs;\n\n$s += abs $2 - $1 while /(\\S+)(?= (\\S+))/g;\n\nprint $s"}, {"source_code": "$_ = (<>,<>);\n\n$s = abs;\n\n$s += abs $2 - $1 while /(\\S+)(?= (\\S+))/g;\n\nprint $s\n"}, {"source_code": "$_ = (<>,<>);\n\n$s = abs;\n\n$s += abs $2 - $1 while /(\\S+)(?= (\\S+))/g;\n\nprint $s\n"}, {"source_code": "$_ = (<>,<>);\n\n$s = abs;\n\n$s += abs $2 - $1 while /(\\S+)(?= (\\S+))/g;\n\nprint $s\n"}, {"source_code": "#!/usr/bin/perl\n\n$n = ;\n@b = split ' ', ;\n$ans = 0;\n$delta = 0;\nforeach (@b) {\n $ans += abs $_ - $delta;\n $delta += $_ - $delta;\n}\nprint $ans, \"\\n\";\n\n"}, {"source_code": "$_ = (<>,<>);\n\n$s = abs;\n\n$s += abs $2 - $1 while /(\\S+)(?= (\\S+))/g;\n\nprint $s\n"}, {"source_code": "$_ = (<>,<>);\n\n$s = abs;\n\n$s += abs $2 - $1 while /(\\S+)(?= (\\S+))/g;\n\nprint $s\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\t@_ = split ' ', <>;\n\t\n\t$sum = 0;\n\t$i = 0;\n\t\n\tfor (@_){\n\t\t$sum += abs($_ - $i);\n\t\t$i = $_;\n\t\t}\n\t\n\tprint $sum\n\t\n\t}"}, {"source_code": "# /\\ /\\\n$_ = (<>,<>);\n# O\n\n$s += abs $2 - $1 while /(\\S+)(?= (\\S+))/g;\n\nprint $s + abs"}, {"source_code": "$_ = (<>,<>);\n\n$s = abs;\n\n$s += abs $2 - $1 while /(\\S+)(?= (\\S+))/g;\n\nprint $s"}, {"source_code": "$_ = (<>,<>);\n\n$s = abs;\n\n$s += abs $2 - $1 while /(\\S+)(?= (\\S+))/g;\n\nprint $s\n"}], "negative_code": [{"source_code": "$s = $_ = (<>,<>);\n\n$s += abs $2 - $1 while /(\\S+)(?= (\\S+))/g;\n\nprint $s"}], "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$$$) \u2014 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$$$) \u2014 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 \u2014 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 perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nmy @tokens = ();\n\nmy $q = read_token();\n\nfor (1..$q) {\n my $a = read_token();\n my $b = read_token();\n my $n = read_token();\n my $s = read_token();\n\n my $m = $a*$n;\n if ( $m > $s ) {\n if ( $s % $n <= $b ) {\n say \"YES\";\n }\n else {\n say \"NO\";\n }\n }\n elsif ( $m == $s ) {\n say \"YES\";\n }\n else {\n if ( $s - $m <= $b ) {\n say \"YES\";\n }\n else {\n say \"NO\";\n }\n }\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n\nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n\nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n\nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n"}], "negative_code": [], "src_uid": "e2434fd5f9d16d59e646b6e69e37684a"} {"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": "$_ = <>;\n$c = \"[^aeiuo]\";\ns{ ($c) (?!\\1\\1) $c (?=$c) }{$& }gx;\nprint;\n"}], "negative_code": [{"source_code": "$_ = <>;\n$c = \"[^aeiuo]\";\ns{ ($c) (?!\\1\\1) ($c) ($c) }{$1$2 $3}gx;\nprint;\n"}], "src_uid": "436c00c832de8df739fc391f2ed6dac4"} {"nl": {"description": "Alice and Bob play the following game. Alice has a set $$$S$$$ of disjoint ranges of integers, initially containing only one range $$$[1, n]$$$. In one turn, Alice picks a range $$$[l, r]$$$ from the set $$$S$$$ and asks Bob to pick a number in the range. Bob chooses a number $$$d$$$ ($$$l \\le d \\le r$$$). Then Alice removes $$$[l, r]$$$ from $$$S$$$ and puts into the set $$$S$$$ the range $$$[l, d - 1]$$$ (if $$$l \\le d - 1$$$) and the range $$$[d + 1, r]$$$ (if $$$d + 1 \\le r$$$). The game ends when the set $$$S$$$ is empty. We can show that the number of turns in each game is exactly $$$n$$$.After playing the game, Alice remembers all the ranges $$$[l, r]$$$ she picked from the set $$$S$$$, but Bob does not remember any of the numbers that he picked. But Bob is smart, and he knows he can find out his numbers $$$d$$$ from Alice's ranges, and so he asks you for help with your programming skill.Given the list of ranges that Alice has picked ($$$[l, r]$$$), for each range, help Bob find the number $$$d$$$ that Bob has picked.We can show that there is always a unique way for Bob to choose his number for a list of valid ranges picked by Alice.", "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 a single integer $$$n$$$ ($$$1 \\le n \\le 1000$$$). Each of the next $$$n$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le n$$$), denoting the range $$$[l, r]$$$ that Alice picked at some point. Note that the ranges are given in no particular order. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$, and the ranges for each test case are from a valid game.", "output_spec": "For each test case print $$$n$$$ lines. Each line should contain three integers $$$l$$$, $$$r$$$, and $$$d$$$, denoting that for Alice's range $$$[l, r]$$$ Bob picked the number $$$d$$$. You can print the lines in any order. We can show that the answer is unique. It is not required to print a new line after each test case. The new lines in the output of the example are for readability only. ", "sample_inputs": ["4\n1\n1 1\n3\n1 3\n2 3\n2 2\n6\n1 1\n3 5\n4 4\n3 6\n4 5\n1 6\n5\n1 5\n1 2\n4 5\n2 2\n4 4"], "sample_outputs": ["1 1 1\n\n1 3 1\n2 2 2\n2 3 3\n\n1 1 1\n3 5 3\n4 4 4\n3 6 6\n4 5 5\n1 6 2\n\n1 5 3\n1 2 1\n4 5 5\n2 2 2\n4 4 4"], "notes": "NoteIn the first test case, there is only 1 range $$$[1, 1]$$$. There was only one range $$$[1, 1]$$$ for Alice to pick, and there was only one number $$$1$$$ for Bob to pick.In the second test case, $$$n = 3$$$. Initially, the set contains only one range $$$[1, 3]$$$. Alice picked the range $$$[1, 3]$$$. Bob picked the number $$$1$$$. Then Alice put the range $$$[2, 3]$$$ back to the set, which after this turn is the only range in the set. Alice picked the range $$$[2, 3]$$$. Bob picked the number $$$3$$$. Then Alice put the range $$$[2, 2]$$$ back to the set. Alice picked the range $$$[2, 2]$$$. Bob picked the number $$$2$$$. The game ended. In the fourth test case, the game was played with $$$n = 5$$$. Initially, the set contains only one range $$$[1, 5]$$$. The game's turn is described in the following table. Game turnAlice's picked rangeBob's picked numberThe range set afterBefore the game start$$$ \\{ [1, 5] \\} $$$1$$$[1, 5]$$$$$$3$$$$$$ \\{ [1, 2], [4, 5] \\}$$$2$$$[1, 2]$$$$$$1$$$$$$ \\{ [2, 2], [4, 5] \\} $$$3$$$[4, 5]$$$$$$5$$$$$$ \\{ [2, 2], [4, 4] \\} $$$4$$$[2, 2]$$$$$$2$$$$$$ \\{ [4, 4] \\} $$$5$$$[4, 4]$$$$$$4$$$$$$ \\{ \\} $$$ (empty set) "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = map { [ split ' ', <> ] } 1 .. $_;\n\t\n\tmy @ans;\n\t\n\twhile( @_ ){\n\t\t@_ = sort {\n\t\t\t$b->[ 1 ] <=> $a->[ 1 ] ||\n\t\t\t$b->[ 0 ] <=> $a->[ 0 ]\n\t\t\t} @_;\n\t\t\n\t\tmy $max = 0;\n\t\tmy $ans;\n\t\t\n\t\tfor( @_ ){\n\t\t\tlast if $_->[ 1 ] < @_;\n\t\t\tif( $_->[ 0 ] > $max ){\n\t\t\t\t$max = $_->[ 0 ];\n\t\t\t\t$ans = \"@$_ \" . @_;\n\t\t\t\t$_->[ 0 ] .= ',';\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\tmy $switch = 0;\n\t\t\n\t\tfor( reverse @_ ){\n\t\t\t$switch and $_->[ 0 ] =~ s/,//;\n\t\t\tif( $_->[ 0 ] =~ m/,/ ){\n\t\t\t\t$switch = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t@_ = grep { $_->[ 0 ] !~ m/,/ } @_;\n\t\t\n\t\tpush @ans, $ans;\n\t\t}\n\t\n\tprint for @ans;\n\tprint '';\n\t}"}], "negative_code": [], "src_uid": "eca433877ef3fbda198c5f2c95a359e7"} {"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$$$) \u2014 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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $c, $sum ) = split;\n\t\n\tmy $min = int $sum / $c;\n\tmy $rem = $sum % $c;\n\t\n\tprint $min ** 2 * ( $c - $rem ) + ( $min + 1 ) ** 2 * $rem;\n\t}"}], "negative_code": [], "src_uid": "0ec973bf4ad209de9731818c75469541"} {"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\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105)\u00a0\u2014 number of cafes indices written by Vlad. In second line, n numbers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u20092\u00b7105) are written\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy %h;\n\tmy $last;\n\t\n\tfor( reverse split ' ', <> ){\n\t\tnot exists $h{ $_ } and $last = $_;\n\t\t$h{ $_ } ++;\n\t\t}\n\t\n\tprint $last;\n\t}"}, {"source_code": "<>;\n\n$h{ $_ } ++ or $L = $_ for reverse split ' ', <>;\n\nprint $L"}], "negative_code": [], "src_uid": "bdea209c7b628e4cc90ebc2572826485"} {"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$$$) \u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$a,$b) = map { $_ - 0 } split(/\\s+/,);\r\n my $res = 'No';\r\n if( $a == 1 ){\r\n if( $n >= 1 and ( $n - 1 ) % $b == 0 ){\r\n $res = 'Yes';\r\n }\r\n print \"$res\\n\";\r\n next;\r\n }\r\n \r\n my $st = 1;\r\n while( $n >= $st ){\r\n if( ( $n - $st ) % $b == 0 ){\r\n $res = 'Yes';\r\n last;\r\n }\r\n $st *= $a;\r\n }\r\n print \"$res\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [], "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\u2009=\u2009i.The problematic bone is initially at the position x\u2009=\u20091. Zane will confuse the audience by swapping the cups k times, the i-th time of which involves the cups at the positions x\u2009=\u2009ui and x\u2009=\u2009vi. 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\u2009=\u20094 and the one at x\u2009=\u20096, they will not be at the position x\u2009=\u20095 at any moment during the operation. Zane\u2019s 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\u2009\u2264\u2009n\u2009\u2264\u2009106, 1\u2009\u2264\u2009m\u2009\u2264\u2009n, 1\u2009\u2264\u2009k\u2009\u2264\u20093\u00b7105)\u00a0\u2014 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,\u2009h2,\u2009...,\u2009hm (1\u2009\u2264\u2009hi\u2009\u2264\u2009n)\u00a0\u2014 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\u2009\u2264\u2009ui,\u2009vi\u2009\u2264\u2009n, ui\u2009\u2260\u2009vi)\u00a0\u2014 the positions of the cups to be swapped.", "output_spec": "Print one integer\u00a0\u2014 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\u2009=\u20092, x\u2009=\u20095, x\u2009=\u20097, and x\u2009=\u20091, respectively.In the second sample, after the first operation, the bone becomes at x\u2009=\u20092, and falls into the hole onto the ground."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m, $k ) = split;\n\tmy @A = 0 .. 1e6;\n\tmap { $A[ $_ ] = -1 } split ' ', <>;\n\t\t\n\tmy $x = 1;\n\t\n\tfor (1 .. $k){\n\t\t\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\tif( $A[ $x ] == -1 ){\n\t\t\tnext;\n\t\t\t}\n\t\t\t\n\t\t$x == $u and $x = $v and next;\n\t\t$x == $v and $x = $u and next;\n\t\t}\n\t\n\tprint $x;\n\t}"}, {"source_code": "@A = 0 .. 1e6;\n\n( $n, $m, $k ) = split ' ', <>;\n$A[ $_ ] = 0 for split ' ', <>;\n\t\n$x = 1;\n\nwhile(<>){\n\t\n\t( $u, $v ) = split;\n\t\n\tnext if ! $A[ $x ];\n\t\t\n\t$x == $u and $x = $v or\n\t$x == $v and $x = $u\n\t}\n\nprint $x"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m, $k ) = split;\n\tmy @A = 0 .. 1e6;\n\tmap { $A[ $_ ] = -1 } split ' ', <>;\n\t\t\n\tmy $x = 1;\n\t\n\tfor (1 .. $k){\n\t\t\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\tif( $A[ $x ] == -1 ){\n\t\t\tlast;\n\t\t\t}\n\t\t\t\n\t\t$x == $u and $x = $v and next;\n\t\t$x == $v and $x = $u and next;\n\t\t}\n\t\n\tprint $x;\n\t}"}], "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$$$) \u2014 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$$$) \u00a0\u2014 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$$$) \u00a0\u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/o,);\nwhile($t-->0){\n my ($n) = map { $_ - 0 } split(/\\s+/o,);\n my @A = map { $_ - 0 } split(/\\s+/o,);\n my @B = ();\n $#A = $n - 1;\n my $s = 0;\n my @Ad = ();\n my @z = ();\n for(my $i=0;$i<$n;$i++){\n $s += $A[$i];\n if( $A[$i] == 0 ){\n push(@z,$A[$i]);\n } else {\n push(@Ad,$A[$i]);\n }\n }\n if( $s == 0 ){\n print \"NO\\n\"; next;\n }\n \n if( $s < 0 ){\n @B = sort { $a <=> $b } @Ad;\n } else {\n @B = sort { $b <=> $a } @Ad;\n }\n print \"YES\\n\";\n print ( join(' ',(@B,@z)) . \"\\n\" );\n \n}\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/o,);\nwhile($t-->0){\n my ($n) = map { $_ - 0 } split(/\\s+/o,);\n my @A = map { $_ - 0 } split(/\\s+/o,);\n my @B = ();\n $#A = $n - 1;\n my $s = 0;\n for(my $i=0;$i<$n;$i++){\n $s += $A[$i];\n }\n if( $s == 0 ){\n print \"NO\\n\"; next;\n }\n \n if( $s < 0 ){\n @B = sort { $a <=> $b } @A;\n } else {\n @B = sort { $b <=> $a } @A;\n }\n print \"YES\\n\";\n print ( join(' ',reverse @B) . \"\\n\" );\n \n}\n\n"}], "src_uid": "e57345f5757654749b411727ebb99c80"} {"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\u2009\u2264\u2009v 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100). The second line contains n space-separated positive integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009100) \u2014 the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b1,\u2009b2,\u2009...,\u2009bm (1\u2009\u2264\u2009bi\u2009\u2264\u2009100) \u2014 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": "use strict;\n\nmy ($n, $m, @pass, @fail);\n\n($n,$m) = split / /, <>;\n@pass = split / /, <>;\n@fail = split / /, <>;\nchomp(@pass), chomp(@fail);\n\n@pass = sort {$a <=> $b} @pass;\n@fail = sort {$a <=> $b} @fail;\n\n(print \"-1\") and exit if (($pass[-1] >= $fail[0]) || (2 * $pass[0] >= $fail[0]));\n\nprint ((2 * $pass[0] > $pass[-1]) ? 2 * $pass[0] : $pass[-1]);"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nsub min\n{\n my $min = $_[0];\n foreach (@_)\n {\n $min = $_ if $_ < $min;\n };\n return $min;\n}\n\nsub max\n{\n my $max = $_[0];\n foreach (@_)\n {\n $max = $_ if $_ > $max;\n };\n return $max;\n}\n\nmy ($m, $n) = split(' ', <>);\n\nmy @right = split(' ', <>);\nmy @wrong = split(' ', <>);\n\n\n\nmy $v = min(@right) * 2;\n\nmy $possible = 1;\n\n\nforeach my $time (@wrong)\n{\n if ($time <= $v)\n {\n $possible = 0;\n last;\n }\n}\n\n$possible = 0 if max(@right) >= min(@wrong);\n\nif ($possible)\n{\n if (max(@right) == min(@right)) {\n print min(min(@wrong) - 1, $v)\n } else {\n print min(min(@wrong) - 1, max(@right, $v))\n }\n}\nelse\n{\n print -1;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\nwhile( <>=~/ / ){\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@a=split/ /,<>;\n@b=split/ /,<>;\n\n$ib=min(@b);\n$ia=min(@a);\n$aa=max(@a);\n\n$TL=max($ia*2,$aa);\nprint $TL<$ib?$TL:-1;\nn();\n\n}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [{"source_code": "use strict;\n\nmy ($n, $m, @pass, @fail);\n\n($n,$m) = split / /, <>;\n@pass = split / /, <>;\n@fail = split / /, <>;\nchomp(@pass), chomp(@fail);\n\n@pass = sort @pass;\n@fail = sort @fail;\n\n(print \"-1\") and exit if (($pass[-1] >= $fail[0]) || (2 * $pass[0] >= $fail[0]));\n\nprint ((2 * $pass[0] > $pass[-1]) ? 2 * $pass[0] : $pass[-1]);"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nsub min\n{\n my $min = $_[0];\n foreach (@_)\n {\n $min = $_ if $_ < $min;\n };\n return $min;\n}\n\nsub max\n{\n my $max = $_[0];\n foreach (@_)\n {\n $max = $_ if $_ > $max;\n };\n return $max;\n}\n\nmy ($m, $n) = split(' ', <>);\n\nmy @right = split(' ', <>);\nmy @wrong = split(' ', <>);\n\n\n\nmy $v = min(@right) * 2;\n\nmy $possible = 1;\n\n\nforeach my $time (@wrong)\n{\n if ($time <= $v)\n {\n $possible = 0;\n last;\n }\n}\n\nif ($possible)\n{\n if (max(@right) == min(@right)) {\n print min(min(@wrong) - 1, $v)\n } else {\n print min(min(@wrong) - 1, max(@right))\n }\n}\nelse\n{\n print -1;\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nsub min\n{\n my $min = $_[0];\n foreach (@_)\n {\n $min = $_ if $_ < $min;\n };\n return $min;\n}\n\nmy ($m, $n) = split(' ', <>);\n\nmy @right = split(' ', <>);\nmy @wrong = split(' ', <>);\n\n\n\nmy $v = min(@right) * 2;\n\nmy $possible = 1;\n\n\nforeach my $time (@wrong)\n{\n if ($time <= $v)\n {\n $possible = 0;\n last;\n }\n}\n\nif ($possible)\n{\n print min(@wrong) - 1;\n}\nelse\n{\n print -1;\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nsub min\n{\n my $min = $_[0];\n foreach (@_)\n {\n $min = $_ if $_ < $min;\n };\n return $min;\n}\n\nsub max\n{\n my $max = $_[0];\n foreach (@_)\n {\n $max = $_ if $_ > $max;\n };\n return $max;\n}\n\nmy ($m, $n) = split(' ', <>);\n\nmy @right = split(' ', <>);\nmy @wrong = split(' ', <>);\n\n\n\nmy $v = min(@right) * 2;\n\nmy $possible = 1;\n\n\nforeach my $time (@wrong)\n{\n if ($time <= $v)\n {\n $possible = 0;\n last;\n }\n}\n\n$possible = 0 if max(@right) >= max(@wrong);\n\nif ($possible)\n{\n if (max(@right) == min(@right)) {\n print min(min(@wrong) - 1, $v)\n } else {\n print min(min(@wrong) - 1, max(@right, $v))\n }\n}\nelse\n{\n print -1;\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nsub min\n{\n my $min = $_[0];\n foreach (@_)\n {\n $min = $_ if $_ < $min;\n };\n return $min;\n}\n\nsub max\n{\n my $max = $_[0];\n foreach (@_)\n {\n $max = $_ if $_ > $max;\n };\n return $max;\n}\n\nmy ($m, $n) = split(' ', <>);\n\nmy @right = split(' ', <>);\nmy @wrong = split(' ', <>);\n\n\n\nmy $v = min(@right) * 2;\n\nmy $possible = 1;\n\n\nforeach my $time (@wrong)\n{\n if ($time <= $v)\n {\n $possible = 0;\n last;\n }\n}\n\nif ($possible)\n{\n if (max(@right) == max(@wrong)) {\n print min(min(@wrong) - 1, $v)\n } else {\n print min(min(@wrong) - 1, max(@right))\n }\n}\nelse\n{\n print -1;\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nsub min\n{\n my $min = $_[0];\n foreach (@_)\n {\n $min = $_ if $_ < $min;\n };\n return $min;\n}\n\nsub max\n{\n my $max = $_[0];\n foreach (@_)\n {\n $max = $_ if $_ > $max;\n };\n return $max;\n}\n\nmy ($m, $n) = split(' ', <>);\n\nmy @right = split(' ', <>);\nmy @wrong = split(' ', <>);\n\n\n\nmy $v = min(@right) * 2;\n\nmy $possible = 1;\n\n\nforeach my $time (@wrong)\n{\n if ($time <= $v)\n {\n $possible = 0;\n last;\n }\n}\n\nif ($possible)\n{\n print min(min(@wrong) - 1, $v)\n}\nelse\n{\n print -1;\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nsub min\n{\n my $min = $_[0];\n foreach (@_)\n {\n $min = $_ if $_ < $min;\n };\n return $min;\n}\n\nsub max\n{\n my $max = $_[0];\n foreach (@_)\n {\n $max = $_ if $_ > $max;\n };\n return $max;\n}\n\nmy ($m, $n) = split(' ', <>);\n\nmy @right = split(' ', <>);\nmy @wrong = split(' ', <>);\n\n\n\nmy $v = min(@right) * 2;\n\nmy $possible = 1;\n\n\nforeach my $time (@wrong)\n{\n if ($time <= $v)\n {\n $possible = 0;\n last;\n }\n}\n\nif ($possible)\n{\n if (max(@right) == min(@right)) {\n print min(min(@wrong) - 1, $v)\n } else {\n print min(min(@wrong) - 1, max(@right, $v))\n }\n}\nelse\n{\n print -1;\n}\n"}], "src_uid": "49c47ebfd710a3733ce7ecb3a3c134a7"} {"nl": {"description": "Little Artem found a grasshopper. He brought it to his house and constructed a jumping area for him.The area looks like a strip of cells 1\u2009\u00d7\u2009n. Each cell contains the direction for the next jump and the length of that jump. Grasshopper starts in the first cell and follows the instructions written on the cells. Grasshopper stops immediately if it jumps out of the strip. Now Artem wants to find out if this will ever happen.", "input_spec": "The first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 length of the strip. Next line contains a string of length n which consists of characters \"<\" and \">\" only, that provide the direction of the jump from the corresponding cell. Next line contains n integers di (1\u2009\u2264\u2009di\u2009\u2264\u2009109)\u00a0\u2014 the length of the jump from the i-th cell.", "output_spec": "Print \"INFINITE\" (without quotes) if grasshopper will continue his jumps forever. Otherwise print \"FINITE\" (without quotes).", "sample_inputs": ["2\n><\n1 2", "3\n>><\n2 1 1"], "sample_outputs": ["FINITE", "INFINITE"], "notes": "NoteIn the first sample grasshopper starts from the first cell and jumps to the right on the next cell. When he is in the second cell he needs to jump two cells left so he will jump out of the strip.Second sample grasshopper path is 1 - 3 - 2 - 3 - 2 - 3 and so on. The path is infinite."}, "positive_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = <>;\n\tchomp;\n\t@_ = split //;\n\t@A = split ' ', <>;\n\t$i = 1;\n\t$inf = 0;\n\twhile( $j < 100010){\n\t\t$i{$i}++;\n\t\t$i += $A[$i-1] * ('>' eq $_[$i-1] ? 1 : -1 );\n\t\t$i < 1 || $i > @A and do { $inf = 0; last };\n\t\t$i{$i} and do { $inf ++ ; last };\n\t}\n\t\n\tprint !$inf ? 'FINITE' : 'INFINITE'\n\t}"}], "negative_code": [{"source_code": "<>;\nchomp( @_ = split //, <> );\n@A = split ' ', <>;\nwhile( 1 ){\n\t$i < 0 || $i >= @_ and do { $f = 1; last };\n\t$i[$i] ++ and do { $f = 0; last };\n\t$i += $A[$i] * ('>' eq $_[$i] ? 1 : -1 );\n}\n\nprint $f ? 'FINITE' : 'INFINITE'"}], "src_uid": "5fcc22cdc38693723d8a9577d685db12"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009105), that is the number of cafe visitors. Each of the following n lines has two space-separated integers hi and mi (0\u2009\u2264\u2009hi\u2009\u2264\u200923;\u00a00\u2009\u2264\u2009mi\u2009\u2264\u200959), 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 \u2014 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": "#!/usr/bin/perl -w\n\nuse List::Util qw(max);\nuse strict;\n\nmy %times = ();\nchomp(my $n = <>);\nfor (1 .. $n) {\n\tchomp($_ = <>);\n\t++$times{$_};\n}\nmy $res = max values %times;\nprint \"$res\\n\";\n"}, {"source_code": "use List::Util 'max';for(<>){++$_{$_}}print max values%_\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = ;\nmy %h = ();\nfor (1 .. $n) {\n $_ = ;\n ++$h{$_};\n}\nmy $m = 0;\nfor (values %h) {\n $m = $_ if $_ > $m;\n}\nprint \"$m\\n\";\n"}, {"source_code": "#!perl -n\nuse List::Util 'max';\nINIT{$_=<>}++$_{$_};END{print max values%_}\n"}, {"source_code": "#!perl -n\nuse List::Util 'max';\nBEGIN{$_='1'}++$_{$_};END{print max values%_}\n"}, {"source_code": "#!perl -n\nuse List::Util 'max';\nEND{print max values%_}++$_{$_}\n"}, {"source_code": "use strict;\nuse warnings;\nuse List::Util 'max';\n\nmy $n = ;\nmy %h = ();\nfor (1 .. $n) {\n $_ = ;\n ++$h{$_};\n}\nmy $m = 0;\nfor (values %h) {\n $m = $_ if $_ > $m;\n}\nprint \"$m\\n\";\n"}, {"source_code": "<>; for (<>) {\n\tchomp;\n\tif ($_ eq $p) {\n\t\t$c++;\n\t} else {\n\t\t$c=1;\n\t\t$p=$_;\n\t}\n\t$C=$c>$C?$c:$C;\n}\nprint $C;\n"}, {"source_code": "my %hash;\nmy $n = int<>;\nmy $max = 0;\n$hash{<>}++ for(1..$n);\nfor(values %hash) {\n $max = $_ if ($_ > $max);\n}\nprint $max;"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\n@c = (0) x 1440;\nchomp($n = <>);\nwhile (<>) {\n\tchomp;\n\t($h, $m) = split / /;\n\t++$c[$h*60+$m];\n}\n$ans = 0;\n$ans=$ans>$_ ? $ans:$_ foreach (@c);\nsay $ans;"}, {"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Thu Oct 25 21:02:27 IST 2012\n# File Name: a.pl\n# USAGE: \n# a.pl \n# \n# \n#------------------------------------------------\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);\nchomp (my $n = );\nmy %times;\nmy $buf;\nwhile ($n--) {\n chomp ($buf = );\n $times{$buf}++; \n}\nprint &max(values %times);\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nmy $n = <>;\nmy ($ch, $cm) = (-1, -1);\nmy $mc = 0;\nmy $cash = 1;\nfor (1..$n) {\n my ($h, $m) = split ' ', <>;\n if ($h == $ch && $m == $cm) {\n $cash++;\n } else {\n $cash = 1;\n }\n $ch = $h;\n $cm = $m;\n $mc = $cash > $mc ? $cash : $mc;\n}\n\nprint $mc, \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\nuse List::Util qw(max min);\n\nchomp(my $n = );\nmy %cnt;\nfor (my $i = 0; $i < $n; $i++) {\n chomp(my $customer = );\n if (exists($cnt{$customer})) {\n $cnt{$customer} += 1;\n } else {\n $cnt{$customer} = 1;\n }\n}\n\nprint max values %cnt;\n"}, {"source_code": "#!usr/bin/perl\n#use warnings;\n#use strict;\n#use autodie;\n#use diagnostics;\nuse utf8;\nuse 5.010;\nuse 5.012;\nuse 5.014;\nmy($line,$n,$max,$count,$prev,$ans);\n$count=0;\n$max=1;\n$ans=1;\nchomp($n=);\nwhile($count ne $n)\n{\n chomp($line=);\n if($count eq 0)\n {\n $prev=$line;\n $count++;\n next;\n }\n $count++;\n if($prev eq $line)\n {\n $ans++;\n }\n else\n {\n if($max<$ans)\n {\n $max=$ans;\n }\n $ans=1;\n }\n $prev=$line;\n}\nif($max<$ans){$max=$ans;}\nsay $max;"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\nuse warnings;\nuse List::Util qw(max);\n\nmy $n = int ;\nmy %hash;\nfor (1 .. $n) {\n my ($h, $m) = map int, split /\\D/, ;\n $m += $h*60;\n if ($hash{$m}) {\n\t$hash{$m}++;\n } else {\n\t$hash{$m} = 1;\n }\n}\nprint max values %hash;\n"}, {"source_code": "my $num = <>;\nchomp $num;\nmy %count;\nfor (1..$num) {\n my $time = <>;\n chomp $time;\n $count{$time}++;\n}\nmy @temp = sort {$count{$b}<=>$count{$a}} keys %count;\nmy $time = shift @temp;\nprint $count{$time};"}], "negative_code": [{"source_code": "<>; for (<>) {\n\tchomp;\n\t$C=$c>$C?$c:$C;\n\tif ($_ eq $p) {\n\t\t$c++;\n\t} else {\n\t\t$c=1;\n\t\t$p=$_;\n\t}\n}\nprint $C;\n"}, {"source_code": "my $n = int<>;\nmy @temp;\nmy @arr;\nfor (1..$n) {\n @temp = split / /, <>;\n push @arr, $temp[0] * 100 + $temp[1];\n}\n@arr = sort @arr;\nmy ($max, $ans) = (0, 0);\nfor (1..$n - 1) {\n if (int$arr[$_] == int$arr[$_ - 1]) {\n $ans++;\n }\n else {\n $max = $ans if ($ans > $max);\n $ans = 0;\n }\n}\nprint $max + 1;"}, {"source_code": "my $n = int<>;\nmy @temp;\nmy @arr;\nfor (1..$n) {\n @temp = split / /, <>;\n push @arr, $temp[0] * 100 + $temp[1];\n}\nmy ($max, $ans) = (0, 0);\nfor (1..$n - 1) {\n if (int$arr[$_] == int$arr[$_ - 1]) {\n $ans++;\n }\n else {\n $max = $ans if ($ans > $max);\n $ans = 0;\n }\n}\nprint $max + 1;\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nmy $n = <>;\nmy ($ch, $cm) = (0, 0);\nmy $mc = 0;\nmy $cash = 1;\nfor (1..$n) {\n my ($h, $m) = split ' ', <>;\n if ($h == $ch && $m == $cm) {\n $cash++;\n } else {\n $cash = 1;\n }\n $ch = $h;\n $cm = $m;\n $mc = $cash > $mc ? $cash : $mc;\n}\n\nprint $mc, \"\\n\";\n"}, {"source_code": "#!usr/bin/perl\n#use warnings;\n#use strict;\n#use autodie;\n#use diagnostics;\nuse utf8;\nuse 5.010;\nuse 5.012;\nuse 5.014;\nmy($line,$n,$max,$count,$prev,$ans);\n$count=0;\n$max=1;\n$ans=1;\nchomp($n=);\nwhile($count ne $n)\n{\n chomp($line=);\n if($count eq 0)\n {\n $prev=$line;\n $count++;\n next;\n }\n $count++;\n if($prev eq $line)\n {\n $ans++;\n }\n else\n {\n if($max<$ans)\n {\n $max=$ans;\n }\n $ans=1;\n }\n $prev=$line;\n}\nsay $max;"}], "src_uid": "cfad2cb472e662e037f3415a84daca57"} {"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 \u2014 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101."}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($a = <>);\nchomp($b = <>);\n$len = length $a;\n@a = split //, $a;\n@b = split //, $b;\n$c = 0;\n$a[$_] ne $b[$_] and ++$c foreach (0 .. $len-1);\n$c%2>0 and say \"impossible\" and exit;\nfor ($i=0,$j=0,$c>>=1; $i<$len; ++$i) {\n\tif ($a[$i] eq $b[$i]) {\n\t\tpush @ans, $a[$i];\n\t} else {\n\t\t$j < $c and push @ans, $a[$i] or push @ans, $b[$i];\n\t\t++$j;\n\t}\n}\nsay join '', @ans;"}, {"source_code": "chomp (($_, $b) = <>);\nfor $j (1 .. length){\n\t$e .= chop $b ne ($a = chop) ? \n\t\t(++ $i % 2 ? $a : 1 - $a)\n\t:\n\t\t0\n\t}\nprint $i % 2 ? 'impossible' : scalar reverse $e"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($a = <>);\nchomp($b = <>);\n$len = length $a;\n@a = split //, $a;\n@b = split //, $b;\n$c = 0;\n$a[$_] ne $b[$_] and ++$c foreach (0 .. $len-1);\n$c%2>0 and say \"impossible\" and exit;\nfor ($i=0,$j=0; $i<$len; ++$i) {\n\tif ($a[$_] eq $b[$_]) {\n\t\tpush @ans, $a[$_];\n\t} else {\n\t\t$j < $c and push @ans, $a[$_] or push @ans, $b[$_];\n\t\t++$j;\n\t}\n}\nsay join '', @ans;"}, {"source_code": "chomp (($_, $b) = <>);\nfor $j (1 .. length){\n\t$e .= chop $b ne (chop) ? \n\t\t++ $i % 2\n\t:\n\t\t0\n\t}\nprint $i % 2 ? 'impossible' : scalar reverse $e"}, {"source_code": "chomp (($_, $b) = <>);\nfor $j (1 .. length){\n\t$e .= chop $b ne (chop) ? \n\t\t++ $i % 2\n\t:\n\t\t0\n\t}\nprint $i % 2 ? 'Impossible' : scalar reverse $e"}], "src_uid": "2df181f2d1f4063a22fd2fa2d47eef92"} {"nl": {"description": "You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix a consisting of n rows and m columns where ai,\u2009j\u2009=\u20091 if the i-th switch turns on the j-th lamp and ai,\u2009j\u2009=\u20090 if the i-th switch is not connected to the j-th lamp.Initially all m lamps are turned off.Switches change state only from \"off\" to \"on\". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards.It is guaranteed that if you push all n switches then all m lamps will be turned on.Your think that you have too many switches and you would like to ignore one of them. Your task is to say if there exists such a switch that if you will ignore (not use) it but press all the other n\u2009-\u20091 switches then all the m lamps will be turned on.", "input_spec": "The first line of the input contains two integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20092000) \u2014 the number of the switches and the number of the lamps. The following n lines contain m characters each. The character ai,\u2009j is equal to '1' if the i-th switch turns on the j-th lamp and '0' otherwise. It is guaranteed that if you press all n switches all m lamps will be turned on.", "output_spec": "Print \"YES\" if there is a switch that if you will ignore it and press all the other n\u2009-\u20091 switches then all m lamps will be turned on. Print \"NO\" if there is no such switch.", "sample_inputs": ["4 5\n10101\n01000\n00111\n10000", "4 5\n10100\n01000\n00110\n00101"], "sample_outputs": ["YES", "NO"], "notes": null}, "positive_code": [{"source_code": "use warnings;\nuse strict;\nuse integer;\n$\\ = qq/\\n/, $, = qq/ /;\nmy ( $n, $m ) = split qq/ /, ;\nmy %batman = ();\nmy $redundant = ();\nmy @arr = ();\n# initialize batman\n$batman { $_ } = 0 for ( 0 .. $n );\nfor ( 1 .. $n ) {\n my $str = ;\n my @characters = split qq//, $str;\n push @arr, $str;\n for my $ii ( 0 .. $#characters ) { \n $batman { $ii } += 1 if $characters [ $ii ] eq q/1/;\n }\n}\nfor my $jj ( @arr ) {\n $redundant = 1;\n my @characters = split q//, $jj;\n for my $hh ( 0 .. $#characters ) {\n if ( $characters [ $hh ] eq q/1/ ) {\n if ( $batman { $hh } == 1 ) { $redundant = 0 }\n }\n }\n if ( $redundant ) { \n print qq/YES/; \n exit \n }\n}\nprint qq/NO/;\n"}], "negative_code": [{"source_code": "use warnings;\nuse strict;\nuse integer;\n$\\ = qq/\\n/, $, = qq/ /;\nmy ( $n, $m ) = split qq/ /, ;\nmy %batman = ();\nfor ( 1 .. $n ) {\n my $str = ;\n my $redundant = 1;\n for ( my $ii = 0; $ii < length $str; $ii += 1 ) {\n if ( substr ( $str, $ii, 1 ) eq q/1/ ) {\n if ( not exists $batman { $ii } ) {\n $redundant = 0; \n $batman { $ii } = 1;\n }\n }\n }\n if ( $redundant ) {\n print qq/YES/;\n exit\n }\n}\nprint qq/NO/;\n"}], "src_uid": "5ce39a83d27253f039f0e26045249d99"} {"nl": {"description": "When Serezha was three years old, he was given a set of cards with letters for his birthday. They were arranged into words in the way which formed the boy's mother favorite number in binary notation. Serezha started playing with them immediately and shuffled them because he wasn't yet able to read. His father decided to rearrange them. Help him restore the original number, on condition that it was the maximum possible one. ", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\leqslant n \\leqslant 10^5$$$)\u00a0\u2014 the length of the string. The second line contains a string consisting of English lowercase letters: 'z', 'e', 'r', 'o' and 'n'. It is guaranteed that it is possible to rearrange the letters in such a way that they form a sequence of words, each being either \"zero\" which corresponds to the digit $$$0$$$ or \"one\" which corresponds to the digit $$$1$$$.", "output_spec": "Print the maximum possible number in binary notation. Print binary digits separated by a space. The leading zeroes are allowed.", "sample_inputs": ["4\nezor", "10\nnznooeeoer"], "sample_outputs": ["0", "1 1 0"], "notes": "NoteIn the first example, the correct initial ordering is \"zero\".In the second example, the correct initial ordering is \"oneonezero\"."}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\nsub remainder {\n my ($dividend, $divisor) = @_;\n}\n\n# solve\n\nmy $length = read_token;\nmy $input = read_line;\n\nmy @chars = split q{}, $input;\n\nmy @bins = ();\n\nfor (@chars) {\n push @bins, 0 if $_ eq 'z';\n push @bins, 1 if $_ eq 'n';\n}\n\n@bins = reverse sort @bins;\n\nsay \"@bins\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\tmy %h;\n\t\n\t/\n\t\t.\n\t\t(?{ $h{ $& } ++ })\n\t\t(*F)\n\t\t/x;\n\t\n\tmy @ans;\n\t\n\twhile( not grep !$_, @h{ split //, 'one' } ){\n\t\tpush @ans, 1;\n\t\tmap { $_ -- } @h{ split //, 'one' };\n\t\t}\n\t\n\twhile( not grep !$_, @h{ split //, 'zero' } ){\n\t\tpush @ans, 0;\n\t\tmap { $_ -- } @h{ split //, 'zero' };\n\t\t}\n\t\n\tprint join ' ', @ans;\n\t}"}], "negative_code": [], "src_uid": "5e5dbd70c7fcedf0f965aed5bafeb06c"} {"nl": {"description": "There are n incoming messages for Vasya. The i-th message is going to be received after ti minutes. Each message has a cost, which equals to A initially. After being received, the cost of a message decreases by B each minute (it can become negative). Vasya can read any message after receiving it at any moment of time. After reading the message, Vasya's bank account receives the current cost of this message. Initially, Vasya's bank account is at 0.Also, each minute Vasya's bank account receives C\u00b7k, where k is the amount of received but unread messages.Vasya's messages are very important to him, and because of that he wants to have all messages read after T minutes.Determine the maximum amount of money Vasya's bank account can hold after T minutes.", "input_spec": "The first line contains five integers n, A, B, C and T (1\u2009\u2264\u2009n,\u2009A,\u2009B,\u2009C,\u2009T\u2009\u2264\u20091000). The second string contains n integers ti (1\u2009\u2264\u2009ti\u2009\u2264\u2009T).", "output_spec": "Output one integer \u00a0\u2014 the answer to the problem.", "sample_inputs": ["4 5 5 3 5\n1 5 5 4", "5 3 1 1 3\n2 2 2 1 1", "5 5 3 4 5\n1 2 3 4 5"], "sample_outputs": ["20", "15", "35"], "notes": "NoteIn the first sample the messages must be read immediately after receiving, Vasya receives A points for each message, n\u00b7A\u2009=\u200920 in total.In the second sample the messages can be read at any integer moment.In the third sample messages must be read at the moment T. This way Vasya has 1, 2, 3, 4 and 0 unread messages at the corresponding minutes, he gets 40 points for them. When reading messages, he receives (5\u2009-\u20094\u00b73)\u2009+\u2009(5\u2009-\u20093\u00b73)\u2009+\u2009(5\u2009-\u20092\u00b73)\u2009+\u2009(5\u2009-\u20091\u00b73)\u2009+\u20095\u2009=\u2009\u2009-\u20095 points. This is 35 in total."}, "positive_code": [{"source_code": "my ($n, $A, $B, $C, $T) = split ' ', <>;\nmy @ti = split ' ', <>;\nmy $max_amt = 0;\n$max_amt += $A + ($C > $B ? $C - $B : 0) * ($T - $_) for @ti;\nprint \"$max_amt\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $A, $B, $C, $T ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\tfor( @_ ){\n\t\tif( $B > $C ){\n\t\t\t$sum += $A;\n\t\t\t}\n\t\telse{\n\t\t\t$sum += $A + ( $T - $_ ) * ( $C - $B );\n\t\t\t}\n\t\t}\n\t\n\tprint $sum;\n\t}"}, {"source_code": "( $n, $A, $B, $C, $T ) = split ' ', <>;\n\n$s += $A + ( $B < $C ) * ( $T - $_ ) * ( $C - $B ) for split ' ', <>;\n\nprint $s"}], "negative_code": [], "src_uid": "281b95c3686c94ae1c1e4e2a18b7fcc4"} {"nl": {"description": "You are given a tree of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$, with edges numbered from $$$1$$$ to $$$n-1$$$. A tree is a connected undirected graph without cycles. You have to assign integer weights to each edge of the tree, such that the resultant graph is a prime tree.A prime tree is a tree where the weight of every path consisting of one or two edges is prime. A path should not visit any vertex twice. The weight of a path is the sum of edge weights on that path.Consider the graph below. It is a prime tree as the weight of every path of two or less edges is prime. For example, the following path of two edges: $$$2 \\to 1 \\to 3$$$ has a weight of $$$11 + 2 = 13$$$, which is prime. Similarly, the path of one edge: $$$4 \\to 3$$$ has a weight of $$$5$$$, which is also prime. Print any valid assignment of weights such that the resultant tree is a prime tree. If there is no such assignment, then print $$$-1$$$. It can be proven that if a valid assignment exists, one exists with weights between $$$1$$$ and $$$10^5$$$ as well.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the number of vertices in the tree. Then, $$$n-1$$$ lines follow. The $$$i$$$-th line contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\leq u, v \\leq n$$$) denoting that edge number $$$i$$$ is between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, if a valid assignment exists, then print a single line containing $$$n-1$$$ integers $$$a_1, a_2, \\dots, a_{n-1}$$$ ($$$1 \\leq a_i \\le 10^5$$$), where $$$a_i$$$ denotes the weight assigned to the edge numbered $$$i$$$. Otherwise, print $$$-1$$$. If there are multiple solutions, you may print any.", "sample_inputs": ["3\n2\n1 2\n4\n1 3\n4 3\n2 1\n7\n1 2\n1 3\n3 4\n3 5\n6 2\n7 2"], "sample_outputs": ["17\n2 5 11\n-1"], "notes": "NoteFor the first test case, there are only two paths having one edge each: $$$1 \\to 2$$$ and $$$2 \\to 1$$$, both having a weight of $$$17$$$, which is prime. The second test case is described in the statement.It can be proven that no such assignment exists for the third test case."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( %h, $s, @Q );\n\t\n\tfor my $i ( 1 .. $_ - 1 ){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t$h{ $u }{ $v } = $i;\n\t\t$h{ $v }{ $u } = $i;\n\t\t}\n\t\n\tif( grep { 2 < values %{ $h{ $_ } } } keys %h ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\tmy( $f ) = grep { 1 == values %{ $h{ $_ } } } keys %h;\n\t\n\tfor my $i ( 1 .. $_ - 1 ){\n\t\t( $s ) = keys %{ $h{ $f } };\n\t\tpush @Q, [ $h{ $f }{ $s }, 2 + $i % 2 ];\n\t\tdelete $h{ $s }{ $f };\n\t\t$f = $s;\n\t\t}\n\t\n\t@Q = map $_->[ 1 ], sort { $a->[ 0 ] <=> $b->[ 0 ] } @Q;\n\t\n\tprint \"@Q\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\tmy $n = $_;\n\t\n\tmy %h;\n\tmy %g;\n\t\n\tfor my $i ( 1 .. $n - 1 ){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\t$g{ $u } ++;\n\t\t$g{ $v } ++;\n\t\t$h{ $u }{ $v } = $i;\n\t\t$h{ $v }{ $u } = $i;\n\t\t}\n\t\n\t$_ = join '', sort { $a <=> $b } values %g;\n\t\n\tif( not m/^112*$/ ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\tmy $first;\n\t\n\tfor my $i ( 1 .. $n ){\n\t\tif( $g{ $i } == 1 ){\n\t\t\t$first = $i;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tmy $second;\n\t\n\tmy @seq;\n\t\n\tfor my $i ( 1 .. $n - 1 ){\n\t\t( $second ) = sort keys %{ $h{ $first } };\n\t\tpush @seq, [ $h{ $first }{ $second }, $i % 2 ];\n\t\texists $h{ $second }{ $first } and delete $h{ $second }{ $first };\n\t\t$first = $second;\n\t\t}\n\t\n\t@seq = map $_->[ 1 ], sort { $a->[ 0 ] <=> $b->[ 0 ] } @seq;\n\t\n\tprint \"@seq\" =~ y/10/52/r;\n\t}"}, {"source_code": "#!/usr/bin/perl\r\n \r\nuse warnings;\r\nuse strict;\r\n \r\n$\\ = $/;\r\n \r\n<>;\r\n \r\nwhile(<>){\r\n\tmy( %h, $s, @Q );\r\n\t\r\n\tfor my $i ( 1 .. $_ - 1 ){\r\n\t\tmy( $u, $v ) = split ' ', <>;\r\n\t\t$h{ $u }{ $v } = $i;\r\n\t\t$h{ $v }{ $u } = $i;\r\n\t\t}\r\n\t\r\n\tif( grep { 2 < values %{ $h{ $_ } } } keys %h ){\r\n\t\tprint -1;\r\n\t\tnext;\r\n\t\t}\r\n\t\r\n\tmy( $f ) = grep { 1 == values %{ $h{ $_ } } } keys %h;\r\n\t\r\n\tfor my $i ( 1 .. $_ - 1 ){\r\n\t\t( $s ) = keys %{ $h{ $f } };\r\n\t\tpush @Q, [ $h{ $f }{ $s }, 2 + $i % 2 ];\r\n\t\tdelete $h{ $s }{ $f };\r\n\t\t$f = $s;\r\n\t\t}\r\n\t\r\n\t@Q = map $_->[ 1 ], sort { $a->[ 0 ] <=> $b->[ 0 ] } @Q;\r\n\t\r\n\tprint \"@Q\";\r\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\tmy $n = $_;\n\t\n\tmy %h;\n\tmy %g;\n\t\n\tfor my $i ( 1 .. $n - 1 ){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\t$g{ $u } ++;\n\t\t$g{ $v } ++;\n\t\t$h{ $u }{ $v } = $i;\n\t\t$h{ $v }{ $u } = $i;\n\t\t}\n\t\n\t$_ = join '', sort { $a <=> $b } values %g;\n\t\n\tif( not m/^112*$/ ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\tmy $first;\n\t\n\tfor my $i ( 1 .. $n ){\n\t\tif( $g{ $i } == 1 ){\n\t\t\t$first = $i;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tmy $second;\n\t\n\tmy @seq;\n\t\n\tfor my $i ( 1 .. $n - 1 ){\n\t\t( $second ) = keys %{ $h{ $first } };\n\t\tpush @seq, [ $h{ $first }{ $second }, $i % 2 ];\n\t\texists $h{ $second }{ $first } and delete $h{ $second }{ $first };\n\t\t$first = $second;\n\t\t}\n\t\n\t@seq = map $_->[ 1 ], sort { $a->[ 0 ] <=> $b->[ 1 ] } @seq;\n\t\n\tprint \"@seq\" =~ y/10/52/r;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\tmy $n = $_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++,\n\tmap { split ' ', <> } 1 .. $n - 1;\n\t\n\t$_ = join '', sort { $a <=> $b } values %h;\n\t\n\tif( m/^112*+2*+2*+2*+$/ ){\n\t\tprint join ' ', ( qw( 2 5 11 ) x ( 1 + $n / 3 ) )[ 0 .. $n - 2 ];\n\t\t}\n\telse{\n\t\tprint -1;\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\tmy $n = $_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++,\n\tmap { split ' ', <> } 1 .. $n - 1;\n\t\n\t$_ = join '', sort { $a <=> $b } values %h;\n\t\n\tif( m/^112*$/ ){\n\t\tprint join ' ', ( qw( 2 5 11 ) x ( 1 + $n / 3 ) )[ 0 .. $n - 2 ];\n\t\t}\n\telse{\n\t\tprint -1;\n\t\t}\n\t}"}], "src_uid": "0639fbeb3a5be67a4c0beeffe8f5d43b"} {"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$$$)\u00a0\u2014 the number of test cases. Then $$$t$$$ test cases follow. Each test case contains one integer $$$n$$$ ($$$1 \\leq n \\leq 10^6$$$)\u00a0\u2014 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": "for (1..<>) {\r\n chomp (my $n = <>);\r\n my $flag = 0;\r\n unless ($n%2021) {\r\n $flag = 1;\r\n }\r\n while ($n >= 2020) {\r\n unless ($n%2020) {\r\n $flag = 1;\r\n last;\r\n }\r\n $n -= 2021;\r\n }\r\n print $flag? \"YES\\n\" : \"NO\\n\";\r\n}\r\nexit;"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $div = int $_ / 2020;\n\t\n\tmy $div_plus = 2021 * $div;\n\t\n\tif( $div_plus >= $_ ){\n\t\tprint \"YES\";\n\t\t}\n\telse{\n\t\tprint \"NO\";\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "3ec1b7027f487181dbdfb12f295f11ae"} {"nl": {"description": "The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city.The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city.Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity.", "input_spec": "The first line of the input contains two integers n and s (1\u2009\u2264\u2009n\u2009\u2264\u2009103; 1\u2009\u2264\u2009s\u2009<\u2009106) \u2014 the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers \u2014 the xi and yi coordinate values of the i-th location and the number ki of people in it (1\u2009\u2264\u2009ki\u2009<\u2009106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0;\u00a00).", "output_spec": "In the output, print \"-1\" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number \u2014 the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10\u2009-\u20096.", "sample_inputs": ["4 999998\n1 1 1\n2 2 1\n3 3 1\n2 -2 1", "4 999998\n1 1 2\n2 2 1\n3 3 1\n2 -2 1", "2 1\n1 1 999997\n2 2 1"], "sample_outputs": ["2.8284271", "1.4142136", "-1"], "notes": null}, "positive_code": [{"source_code": "\nwhile(<>){\n\tchomp;\n\t($n,$s)=split/ /;\n\t@_=@sf=();\n\tfor $i(1..$n){\n\t\t$_=<>;\n\t\tchomp;\n\t\t($x,$y,$k)=split/ /;\n\t\t$_[$i]=$x**2 + $y**2;\n\t#\t$k[$i]=$k;\n\t\t$sf=sprintf \"%09d %d\", $_[$i], $k;\n\t#\tprint \"$sf\\n\";\n\t\t$sf[$i]=$sf;\n\t\t}\n\tshift @sf;\n\t@sf = sort @sf;\n\t\n\tfor (@sf){\n\t\t\n\t\t/ /;\n\t\t$s+=$';\n\t\tif ($s>=1000000){\n\t\t\t\n\t\t\tprint (sqrt(0+$`));\n\t\t\tprint \"\\n\";\n\t\t\tlast;\n\t\t\t\n\t\t\t}\n\t\t\n\t\t}\n\t\n\tif ($s<1000000){print \"-1\\n\"}\n\t\n#\tprint \"@sf\\n\";\n\t}\n\t\n"}], "negative_code": [{"source_code": "\nwhile(<>){\n\tchomp;\n\t($n,$s)=split/ /;\n\t@_=@sf=();\n\tfor $i(1..$n){\n\t\t$_=<>;\n\t\tchomp;\n\t\t($x,$y,$k)=split/ /;\n\t\t$_[$i]=$x**2 + $y**2;\n\t#\t$k[$i]=$k;\n\t\t$sf=sprintf \"%06d %d\", $_[$i], $k;\n\t#\tprint \"$sf\\n\";\n\t\t$sf[$i]=$sf;\n\t\t}\n\tshift @sf;\n\t@sf = sort @sf;\n\t\n\tfor (@sf){\n\t\t\n\t\t/ /;\n\t\t$s+=$';\n\t\tif ($s>=1000000){\n\t\t\t\n\t\t\tprint (sqrt(0+$`));\n\t\t\tprint \"\\n\";\n\t\t\tlast;\n\t\t\t\n\t\t\t}\n\t\t\n\t\t}\n\t\n\tif ($s<1000000){print \"-1\\n\"}\n\t\n#\tprint \"@sf\\n\";\n\t}\n\t\n"}], "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$$$) \u00a0\u2014 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$$$) \u00a0\u2014 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$$$) \u00a0\u2014 elements of the array $$$a$$$.", "output_spec": "For each test case, print one integer \u00a0\u2014 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": "my $max;\r\nmy $min;\r\n\r\nfor(1..<>) {\r\n $min = 1000000001;\r\n $max = 0;\r\n <>;\r\n chomp (my $in = <>);\r\n @a = split / /, $in;\r\n foreach (@a){\r\n if ($_ > $max) {\r\n $max = $_;\r\n }\r\n if ($_ < $min) {\r\n $min = $_;\r\n }\r\n }\r\n print $max - $min;\r\n print \"\\n\";\r\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t@_ = sort { $b <=> $a } @_;\n\t\n\tprint $_[ 0 ] - $_[ @_ - 1 ];\n\t}"}], "negative_code": [{"source_code": "my $max;\r\nmy $min;\r\n\r\nfor(1..<>) {\r\n $min = 9999999;\r\n $max = 0;\r\n <>;\r\n chomp (my $in = <>);\r\n @a = split / /, $in;\r\n foreach (@a){\r\n if ($_ > $max) {\r\n $max = $_;\r\n }\r\n if ($_ < $min) {\r\n $min = $_;\r\n }\r\n }\r\n print $max - $min;\r\n print \"\\n\";\r\n}"}, {"source_code": "my $max;\r\nmy $min;\r\n\r\nopen(FH, '<', 'input.txt');\r\n\r\nfor(1..) {\r\n $min = 9999999;\r\n $max = 0;\r\n ;\r\n chomp (my $in = );\r\n @a = split / /, $in;\r\n foreach (@a){\r\n if ($_ > $max) {\r\n $max = $_;\r\n }\r\n if ($_ < $min) {\r\n $min = $_;\r\n }\r\n }\r\n print $max - $min;\r\n print \"\\n\";\r\n}"}], "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\u2009\u2264\u2009xi\u2009<\u2009yi\u2009\u2264\u2009n). 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 \u2014 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\n\n# perform dfs to make the connected\n# components\nsub make_disjoint_sets {\n\n my ($lst_ref, $visit_ref, $ds_ref, $curr, $id ) = @_;\n \n if ( exists $visit_ref -> { $curr } ) {\n\treturn;\n }\n \n $visit_ref -> { $curr } = 1;\n $ds_ref -> { $curr } = $id;\n \n for my $adjacent ( keys %{ $lst_ref -> { $curr } } ) {\n\t&make_disjoint_sets ( $lst_ref, $visit_ref,\n\t\t\t $ds_ref, $adjacent, $id );\n }\n\n return;\n}\n\n# use a hash as a set to count the number\n# of unique sets\nsub count_disjoint_sets {\n\n my $ds_ref = $_[0];\n my %count_set = ();\n\n for my $i (values %{ $ds_ref }) {\n\t$count_set { $ds_ref -> { $i } } = 1;\n }\n\n my $val = (scalar (keys %count_set));\n return $val;\n}\n\n\nmy $f = ;\nchomp ( $f );\nmy ($n, $m) = split m/ /, $f;\n\nmy %disjoint_sets = ();\nmy %lst = ();\nmy %visit = ();\n\nmy $a, $b;\nfor my $i (1..$m) {\n $f = ;\n chomp ( $f );\n ($a, $b) = split m/ /, $f;\n $lst { $a }{ $b } = 1;\n $lst { $b }{ $a } = 1;\n}\n\n\n\nfor my $i (1..$n) {\n &make_disjoint_sets ( \\%lst, \\%visit, \\%disjoint_sets, $i, $i );\n}\n\nmy $cc = &count_disjoint_sets ( \\%disjoint_sets );\n\nmy $answer = (2 ** ($n - $cc));\n \nprint \"$answer\\n\";\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "<>;\n@_=<>;\nwhile(@_){\n\t@c=((split/ /,$_[0])[0]);\n\twhile (@c){\n\t\t$c=shift @c;\n\t\tfor (@_){\n\t\t\tif (/\\b$c\\b/){\n\t\t\t\ts/ ?\\b$c\\b ?//;\n\t\t\t\tpush @c, 0+$_;\n\t\t\t\t$a++;\n\t\t\t\t}\n\t\t\telse {\n\t\t\t\tpush @left, $_\n\t\t\t\t\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\tpush @all, $c, @c;\n\t\tfor (@left){\n\t\t\t($x,$y)=split/ /;\n\t\t\t$f=0;\n\t\t\tfor $ix(@all){\n\t\t\t\t$ix==$x and $f++\n\t\t\t\t}\n\t\t\t$g=0;\n\t\t\tfor $iy(@all){\n\t\t\t\t$iy==$y and $g++\n\t\t\t\t}\n\t\t\t$f and $g or push @LEFT,$_ \n\t\t\t}\n\t\t@_=@LEFT;\n\t\t@LEFT=();\n\t\t@left=();\n\t\t\n\t\t}\n\t\n\t}\nprint 2**$a"}], "negative_code": [], "src_uid": "c36f4bb34543476abc31fe986032122d"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100) which represents the number of soldiers in the line. The second line contains integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009100) 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,\u2009a2,\u2009...,\u2009an are not necessarily different.", "output_spec": "Print the only integer \u2014 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": "my $n = <>;\nmy @arr = split \" \", <>;\nmy ($min, $max) = (105, 0);\nmy ($x, $y) = (105, 0);\n\nfor (my $i = 0; $i < $n; $i++) {\n if ($arr[$i] <= $min) {\n $min = $arr[$i];\n $x = $i;\n # print \"$min $x\\n\";\n }\n if ($arr[$i] > $max) {\n $max = $arr[$i];\n $y = $i;\n # print \"$max $y\\n\";\n }\n}\nmy $ans = $y + $n - $x - 1;\n$ans-- if ($x < $y);\nprint $ans;\n"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nchomp($n = <>);\n($mn, $mx) = (10 x 8, -10 x 8);\n$i = 0;\nfor (split / /, <>) {\n\tif ($_ > $mx) {\n\t\t$mx = $_;\n\t\t$mxv = $i;\n\t}\n\tif ($_ <= $mn) {\n\t\t$mn = $_;\n\t\t$mnv = $i;\n\t}\n\t++$i;\n}\n$ans = $mxv - 1 + $n - $mnv;\n$mnv<$mxv and --$ans;\nsay $ans;\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\n<>;\nmy @a = split ' ', <>;\n\nmy $imax = 0;\nmy $imin = 0;\n$imax = $a[$_] > $a[$imax] ? $_ : $imax for 1..$#a;\n$imin = $a[$_] <= $a[$imin] ? $_ : $imin for 1..$#a;\n\nprint $imax > $imin ? ($imax + ($#a - $imin) - 1) : ($imax + ($#a - $imin)), \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\n<>;\nmy @a = split ' ', <>;\n\nmy $imax = 0;\nmy $imin = 0;\nmy $c = 0;\n$imax = $a[$_] > $a[$imax] ? $_ : $imax for 1..$#a;\n\nwhile ($imax != 0) {\n @a[$imax - 1, $imax] = @a[$imax, $imax - 1];\n $imax--;\n $c++;\n}\n\n$imin = $a[$_] <= $a[$imin] ? $_ : $imin for 1..$#a;\n\nwhile ($imin != $#a) {\n @a[$imin - 1, $imin] = @a[$imin, $imin - 1];\n $imin++;\n $c++;\n}\n\nprint $c, \"\\n\";\n"}, {"source_code": "chomp($n = );\n@soldiers = split /\\s+/, ;\n\n%pos = { };\n$tall = -1;\n$small = 99999;\n\nfor($s=0;$s < $n; $s++) {\n\tif($soldiers[$s] > $tall) {\n\t\t$tall = $soldiers[$s];\n\t\t$pos{'tall'} = $s;\n\t}\n\tif($soldiers[$s] <= $small) {\n\t\t$small = $soldiers[$s];\n\t\t$pos{'small'} = $s;\n\t}\n}\n\n#print \"Tallest in pos \".$pos{'tall'}.\", smallest in \".$pos{'small'}.\"\\n\";\n\n$swaps = $pos{'tall'} + ($n - $pos{'small'} - 1);\nif($pos{'tall'} > $pos{'small'}) { $swaps--; }\nprint \"$swaps\\n\";"}, {"source_code": "\nsub maxInd {\n (my $max, my $index) = (0, 0);\n for($i = 0; $i <= $#_; $i++)\n {\n if($max < $_[$i]) {\n $max = $_[$i];\n $index = $i;\n }\n }\n $index;\n}\n\nsub minInd {\n (my $min, my $index) = (100, 0);\n for($i = $#_; $i >= 0; $i--)\n {\n if($min > $_[$i]) {\n $min = $_[$i];\n $index = $i;\n }\n }\n $index;\n}\n\n;\nchomp($s = );\n@arr = split(/ /, $s);\n$max = &maxInd(@arr);\n$min = &minInd(@arr);\n$answer = $max + abs($#arr - $min);\nprint $max - $min > 0 ? --$answer : $answer;"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nmy $n = ;\nmy @soldiers = split(' ', );\nmy $min = 100000;\nmy $min_position = 0;\nmy $max = -1;\nmy $max_position = 0;\n\nfor (my $i = 0; $i < $n; $i++)\n{\n my $soldier = $soldiers[$i];\n if ($soldier > $max)\n {\n $max = $soldier;\n $max_position = $i;\n }\n if ($soldier <= $min)\n {\n $min = $soldier;\n $min_position = $i;\n }\n}\n\nmy $result = $max_position + ($n - 1 - $min_position);\nif ($max_position > $ min_position)\n{\n $result--;\n}\n\nprint $result;"}, {"source_code": "$n=<>;\n@a=split(' ',<>);\n$m=0;\n$c=0;\n$i=0;\n$j=0;\n$n-=1;\nforeach(@a)\n{\nif($_>$m){$m=$_;$i=$c;}\n$c+=1;\n}\n$c=0;\nforeach(@a)\n{\nif($_<=$m){$m=$_;$j=$c;}\n$c+=1;\n}\n$ans=$i+$n-$j;\nif($i>$j){$ans-=1;}\nprint \"$ans\\n\";\n"}], "negative_code": [{"source_code": "\nsub maxInd {\n (my $max, my $index) = (0, 0);\n for($i = 0; $i <= $#_; $i++)\n {\n if($max < $_[$i]) {\n $max = $_[$i];\n $index = $i;\n }\n }\n $index;\n}\n\nsub minInd {\n (my $min, my $index) = (100, 0);\n for($i = $#_; $i >= 0; $i--)\n {\n if($min > $_[$i]) {\n $min = $_[$i];\n $index = $i;\n }\n }\n $index;\n}\n\nchomp($s = );\n@arr = split(/ /, $s);\n$max = &maxInd(@arr);\n$min = &minInd(@arr);\n$answer = $max + abs($#arr - $min);\nprint $max - $min > 0 ? --$answer : $answer;"}], "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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of groups. Each of the following n lines contains a sequence consisting of 7 zeroes and ones \u2014 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": "$n = <>;\nchomp;\nfor ($i = 0; $i < $n; $i++) {\n\t$code = <>;\n\tchomp;\n\tfor ($j = 0; $j < 7; $j++) {\n\t\tif (substr($code, $j, 1) eq \"1\") {\n\t\t\t$list[$j]++;\n\t\t}\n\t}\n}\n$ans = 0;\nfor ($j = 0; $j < 7; $j++) {\n\tif ($list[$j] > $ans) {\n\t\t$ans = $list[$j];\n\t}\n}\nprint \"$ans\\n\";\n\t\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t\n\t@_ = ();\n\t\n\tfor ( 1 .. $_ ){\n\t\t$_ = <>;\n\t\tchomp;\n\t\t\n\t\tmy $i = 0;\n\t\t\n\t\twhile( length ){\n\t\t\tmy $ch = chop;\n\t\t\t$_[ $i ] += $ch;\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\tprint +(sort { $b <=> $a } @_)[ 0 ];\n\t\n\t}\n"}], "negative_code": [], "src_uid": "d8743905d56c6c670b6eeeddc7af0e36"} {"nl": {"description": "One day Ms Swan bought an orange in a shop. The orange consisted of n\u00b7k segments, numbered with integers from 1 to n\u00b7k. 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\u2009\u2264\u2009i\u2009\u2264\u2009k) child wrote the number ai (1\u2009\u2264\u2009ai\u2009\u2264\u2009n\u00b7k). 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\u2009\u2264\u2009n,\u2009k\u2009\u2264\u200930). The second line contains k space-separated integers a1,\u2009a2,\u2009...,\u2009ak (1\u2009\u2264\u2009ai\u2009\u2264\u2009n\u00b7k), 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\u00b7k 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": "use warnings;\nuse strict;\n\n\n\n \n\nmy $l = <>;\nmy ($n, $k) = split(\" \", $l);\nmy $l2 = <>;\nmy @ch = split(\" \", $l2);\n\nmy %seg;\n@seg{1..$k} = @ch;\nmy %rseg = reverse %seg;\n \n$/ = \" \";\n$\\ = \" \";\n\nmy $i = 0;\n\n\nmy $cur = 0;\n\nfor my $child (1..$k) {\n print $seg{$child};\n for my $part (1..$n-1) {\n while ( exists $rseg{++$cur} ) { }\n print $cur;\n }\n}\n\n\n\n"}], "negative_code": [{"source_code": "use warnings;\nuse strict;\n\n\n\n \n\nmy $l = <>;\nmy ($n, $k) = split(\" \", $l);\nmy $l2 = <>;\nmy @ch = split(\" \", $l2);\n\nmy %seg;\n@seg{1..$k} = @ch;\nmy %rseg = reverse %seg;\n \n$/ = \" \";\n$\\ = \" \";\n\nmy $i = 0;\n\nfor (1..$n*$k)\n{\n print $seg{++$i} if $_ % $k == 0; \n print $_ unless exists $rseg{$_};\n\n}\n\n\n\n"}, {"source_code": "use warnings;\nuse strict;\n\n\n\n \n\nmy $l = <>;\nmy ($n, $k) = split(\" \", $l);\nmy $l2 = <>;\nmy @ch = split(\" \", $l2);\n\nmy %seg;\n@seg{1..$k} = @ch;\nmy %rseg = reverse %seg;\n \n$/ = \" \";\n$\\ = \" \";\n\nmy $i = 0;\n\nfor (1..$n*$k)\n{\n print $seg{++$i} if $_ % $n == 0; \n print $_ unless exists $rseg{$_};\n\n}\n\n\n\n"}, {"source_code": "use warnings;\nuse strict;\n\n\n\n \n\nmy $l = <>;\nmy ($n, $k) = split(\" \", $l);\nmy $l2 = <>;\nmy @ch = split(\" \", $l2);\n\nmy %seg;\n@seg{1..$k} = @ch;\nmy %rseg = reverse %seg;\n \n$/ = \" \";\n$\\ = \" \";\n\nmy $i = 0;\n\nfor (1..$n*$k)\n{\n print $seg{++$i} if $_ % $k == 1; \n print $_ unless exists $rseg{$_};\n\n}\n\n\n\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\u2009<\u2009t\u2009<\u2009180) \u2014 the number of tests. Each of the following t lines contains a single integer a (0\u2009<\u2009a\u2009<\u2009180) \u2014 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 \u2014 a square."}, "positive_code": [{"source_code": "<>;map{$_=180-$_;print int(360/$_)*$_==360?YES:NO,\"\\n\"}<>"}, {"source_code": "#!perl -pl\nINIT{<>}$_=360%(180-$_)?'NO':'YES'\n"}, {"source_code": "#!perl -pl\nINIT{<>}$_=360%(180-$_)?'NO':'YES'\n\n"}, {"source_code": "#!perl -pl\nINIT{<>}$_=360%(180-$_)?'NO':'YES'\n\n"}, {"source_code": "my $n = <>;\nmy $t;\nfor(1..$n) {\n $t = <>;\n if (360 % (180 - $t) == 0) {\n print \"YES\\n\";\n }\n else {\n print \"NO\\n\";\n }\n}\n"}, {"source_code": "#!perl -pl\nINIT{<>}$_=360%(180-$_)?'NO':'YES'\n"}, {"source_code": "#!perl -pl\nINIT{<>}$_=360%(180-$_)?'NO':'YES'\n\n"}, {"source_code": "#!perl -pl\nINIT{<>}$_=360%(180-$_)?'NO':'YES'\n\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n360%(180-<>)==0 and say \"YES\" or say \"NO\" foreach (1..$n);"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nsub is_regular_angle {\n my ($theta) = @_;\n\n my $outside = 180 - $theta;\n 360 % $outside == 0;\n}\n\nmy $n = <>;\nfor (1..$n) {\n my $theta = <>;\n say is_regular_angle($theta) ? 'YES' : 'NO';\n}\n"}, {"source_code": "#!perl -pl\nINIT{<>}$_=360%(180-$_)?'NO':'YES'\n\n"}, {"source_code": "#!usr/bin/perl\n#use warnings;\n#use strict;\n#use autodie;\n#use diagnostics;\nuse utf8;\nuse 5.010;\nuse 5.012;\nuse 5.014;\nmy($count,$t,$n);\nchomp($t=);\n$count=0;\nwhile($count != $t)\n{\n\t$count++;\n\tchomp($n=);\n\t$n=180-$n;\n\t$n=360/$n;\n\tif($n=~/\\./)\n\t{\n\t\tsay \"NO\";\n\t}\n\telse\n\t{\n\t\tsay \"YES\";\n\t}\n}\n"}, {"source_code": "#!perl -pl\nINIT{<>}$_=360%(180-$_)?'NO':'YES'\n\n"}, {"source_code": "$n = <>;\nprint 360 % (180 - <>) ? \"NO\\n\" : \"YES\\n\" foreach(1..$n);"}, {"source_code": "#!perl -pl\nINIT{<>}$_=360%(180-$_)?'NO':'YES'\n\n"}, {"source_code": "my $num = <>;\nchomp $num;\nfor (1..$num) {\n my $angle = <>;\n chomp $angle;\n (360%(180-$angle)) ? print \"NO\\n\" : print \"YES\\n\";\n}"}, {"source_code": "#!perl -pl\nINIT{<>}$_=360%(180-$_)?'NO':'YES'\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;"}], "src_uid": "9037f487a426ead347baa803955b2c00"} {"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,\u2009a2,\u2009...,\u2009am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1,\u2009f2,\u2009...,\u2009fn of length n and for each number ai got number bi\u2009=\u2009fai. 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the lengths of sequences fi and bi respectively. The second line contains n integers, determining sequence f1,\u2009f2,\u2009...,\u2009fn (1\u2009\u2264\u2009fi\u2009\u2264\u2009n). The last line contains m integers, determining sequence b1,\u2009b2,\u2009...,\u2009bm (1\u2009\u2264\u2009bi\u2009\u2264\u2009n).", "output_spec": "Print \"Possible\" if there is exactly one sequence ai, such that bi\u2009=\u2009fai for all i from 1 to m. Then print m integers a1,\u2009a2,\u2009...,\u2009am. 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\u2009\u2260\u20093 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": "#!perl\n$\\ = \"\\n\";\n\n$AmbiguityFlag = 0;\n$ImpossibleFlag = 0;\n\n<>;\n@f = split /\\s/, <>;\n@b = split /\\s/, <>;\n\nfor (my $i = 0; $i <= $#f; $i++)\n{\n $repetition{@f[$i]} = 0 if !exists $repetition{@f[$i]};\n $repetition{@f[$i]}++;\n \n $numbers{@f[$i]} = $i + 1;\n}\n\nfor my $i (@b)\n{\n if ($repetition{$i} > 1)\n {\n $AmbiguityFlag = 1; \n }\n \n if (exists $numbers{$i})\n {\n push @a, $numbers{$i};\n }\n else\n {\n $ImpossibleFlag = 1;\n last;\n }\n}\n\nif($ImpossibleFlag == 1)\n{\n print \"Impossible\";\n}\nelsif($AmbiguityFlag == 1)\n{\n print \"Ambiguity\";\n}\nelse\n{\n print \"Possible\";\n print \"@a\";\n}"}], "negative_code": [{"source_code": "#!perl\n$\\ = \"\\n\";\n\n$AmbiguityFlag = 0;\n$ImpossibleFlag = 0;\n\n<>;\n@f = split /\\s/, <>;\n@b = split /\\s/, <>;\n\nfor (my $i = 0; $i <= $#f; $i++)\n{\n if (exists $repetition{@f[$i]})\n {\n $AmbiguityFlag = 1;\n #last;\n }\n else\n {\n $repetition{@f[$i]} = 1;\n }\n \n $numbers{@f[$i]} = $i + 1;\n}\n\n\nfor my $i (@b)\n{\n if (exists $numbers{$i})\n {\n push @a, $numbers{$i};\n }\n else\n {\n $ImpossibleFlag = 1;\n last;\n }\n}\n\nif($ImpossibleFlag == 1)\n{\n print \"Impossible\";\n}\nelsif($AmbiguityFlag == 1)\n{\n print \"Ambiguity\";\n}\nelse\n{\n print \"Possible\";\n print \"@a\";\n}"}, {"source_code": "#!perl\n$\\ = \"\\n\";\n\n$AmbiguityFlag = 0;\n$ImpossibleFlag = 0;\n\n<>;\n@f = split /\\s/, <>;\n@b = split /\\s/, <>;\n\nfor (my $i = 0; $i <= $#f; $i++)\n{\n $repetition{@f[$i]} = 0 if !exists $repetition{@f[$i]};\n $repetition{@f[$i]}++;\n \n $numbers{@f[$i]} = $i + 1;\n}\n\nfor my $i (@b)\n{\n if ($repetition{$i} > 1)\n {\n $AmbiguityFlag = 1;\n last; \n }\n \n if (exists $numbers{$i})\n {\n push @a, $numbers{$i};\n }\n else\n {\n $ImpossibleFlag = 1;\n last;\n }\n}\n\nif($ImpossibleFlag == 1)\n{\n print \"Impossible\";\n}\nelsif($AmbiguityFlag == 1)\n{\n print \"Ambiguity\";\n}\nelse\n{\n print \"Possible\";\n print \"@a\";\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.\u00a0e. 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$$$) \u2014 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 ) = split ' ', <>;\n\n$_ = join \"\\n\", map { chomp; \".$_.\" } 0 x $m, <>, 0 x $m;\n\ny/./0/;\n\n/\n(?=\n\t(\\S{3})\n\t.{$m}\n\t(\\S)(\\d)(\\S)\n\t.{$m}\n\t(\\S{3})\n\t(?{\n\t\t$f += \"$3\" != ( () = \"$1$2$4$5\" =~ m!\\*!g );\n\t})\n\t(?!)\n)\n/xs;\n\nprint $f ? \"NO\" : \"YES\""}, {"source_code": "use warnings;\nuse strict;\nuse constant NL => qq/\\n/;\n\n\n\nmy ( $n, $m ) = ( =~ m/(\\d+)\\s(\\d+)/);\nmy @arr = ();\nfor ( 1 .. $n ) {\n my $line = ;\n push @arr, [ split qq//, $line ] }\nmy $is_valid = 1;\n\nAA: for my $i ( 0 .. ($n-1) ) {\n AB: for my $j ( 0 .. ($m-1) ) {\n if ( $arr [ $i ][ $j ] eq '.' ) {\n AC: for my $one ( ($i-1) .. ($i+1) ) {\n AD: for my $two ( ($j-1) .. ($j+1) ) {\n if ( $one eq $i and $two eq $j ) { next AD }\n if ( $one >= 0 and $two >= 0 and $one < $n and $two < $m and \n ($arr [ $one ][ $two ] eq '*') ) {\n $is_valid = 0;\n last AA }}}}\n elsif ( $arr [ $i ][ $j ] ne '*' ) {\n my $sum = 0;\n AE: for my $one ( ($i-1) .. ($i+1) ) {\n AF: for my $two ( ($j-1) .. ($j+1) ) {\n if ( $one eq $i and $two eq $j ) { next AF }\n if ( $one >= 0 and $two >= 0 and\n $one < $n and $two < $m and \n ( $arr [ $one ][ $two ] eq '*') ) { $sum += 1 } }}\n if ( $sum != $arr [ $i ][ $j ] ) {\n $is_valid = 0;\n last AA }}\n }\n}\n\nprint qq/YES\\n/ if $is_valid;\nprint qq/NO\\n/ if not $is_valid;\n"}, {"source_code": "( $n, $m ) = split ' ', <>;\n\n$_ = join \"\\n\", map { chomp; \".$_.\" } 0 x $m, <>, 0 x $m;\n\ny/./0/;\n\n/\n(?=\n\t(\\S{3})\n\t.{$m}\n\t(\\S)(\\d)(\\S)\n\t.{$m}\n\t(\\S{3})\n\t(?{\n\t\t$f += \"$3\" != ( () = \"$1$2$4$5\" =~ m!\\*!g );\n\t})\n\t(?!)\n)\n/xs;\n\nprint $f ? \"NO\" : \"YES\""}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\tmy @A;\n\t\n\t( $_ = <>, chomp ), push @A, [ split // ] for 1 .. $n;\n\t\n\tmy $fail = 0;\n\t\n\tfor my $i ( 0 .. $n - 1 ){\n\t\tfor my $j ( 0 .. $m - 1 ){\n\t\t\tnext if $A[$i][$j] eq '*';\n\t\t\t\n\t\t\tmy $bombs = 0;\n\t\t\t\n\t\t\t$i > 0 and $A[$i-1][$j+0] eq '*' and $bombs ++;\n\t\t\t$i > 0 and $j > 0 and $A[$i-1][$j-1] eq '*' and $bombs ++;\n\t\t\t$i > 0 and $j < $m - 1 and $A[$i-1][$j+1] eq '*' and $bombs ++;\n\t\t\t$i < $n - 1 and $A[$i+1][$j+0] eq '*' and $bombs ++;\n\t\t\t$i < $n - 1 and $j > 0 and $A[$i+1][$j-1] eq '*' and $bombs ++;\n\t\t\t$i < $n - 1 and $j < $m - 1 and $A[$i+1][$j+1] eq '*' and $bombs ++;\n\t\t\t$j > 0 and $A[$i+0][$j-1] eq '*' and $bombs ++;\n\t\t\t$j < $m - 1 and $A[$i+0][$j+1] eq '*' and $bombs ++;\n\t\t\t\n\t\t\t$debug and print $bombs;\n\t\t\t\n\t\t\tnext if $A[$i][$j] eq '.' and not $bombs;\n\t\t\t\n\t\t\tif( $bombs ne $A[$i][$j] ){ $fail = 1; }\n\t\t\t}\n\t\t}\n\t\n\tprint $fail ? \"NO\" : \"YES\";\n\t\n\t$debug and print '-' x 15;\n\t}"}, {"source_code": "( $n, $m ) = split ' ', <>;\n\n$_ = join \"\\n\",\n\t0 x ( $m + 2 ), \n\t( map 0 . <> =~ s/\\n//r . 0, 1 .. $n ), \n\t0 x ( $m + 2 );\n\ny/./0/;\n\n/\n(?=\n\t(\\S{3})\n\t.{$m}\n\t(\\S)(\\d)(\\S)\n\t.{$m}\n\t(\\S{3})\n\t(?{\n\t\t$f += \"$3\" != ( () = \"$1$2$4$5\" =~ m!\\*!g );\n\t})\n\t(?!)\n)\n/xs;\n\nprint $f ? \"NO\" : \"YES\""}], "negative_code": [], "src_uid": "0d586ba7d304902caaeb7cd9e6917cd6"} {"nl": {"description": "You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 \\leq x < y < z \\leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$|a_x|$$$ need to be less than $$$10^{18}$$$.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well.", "input_spec": "Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \\leq t \\leq 10000)$$$ \u2014 the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \\leq n \\leq 2 \\cdot 10^5)$$$ \u2014 the size of the array $$$a$$$. 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 elements of $$$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, print $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 \\leq m \\leq n)$$$ \u2014 number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 \\leq x < y < z \\leq n)$$$\u2014 description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task.", "sample_inputs": ["3\n5\n5 -4 2 -1 2\n3\n4 3 2\n3\n-3 -2 -1"], "sample_outputs": ["2\n1 2 3\n3 4 5\n-1\n0"], "notes": "NoteIn the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation,$$$[-6,-4,-3,-1,2]$$$ after the second operation.In the second example, it is impossible to make the array sorted after any sequence of operations.In the third example, the array is already sorted, so we don't need to perform any operations."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @sorted = sort { $a <=> $b } @_;\n\t\n\tif( \"@sorted\" eq \"@_\" ){\n\t\tprint 0;\n\t\tnext;\n\t\t}\n\t\n\tif( $_[ @_ - 1 ] >= 0 and $_[ @_ - 1 ] >= $_[ @_ - 2 ] ){\n\t\tprint @_ - 2;\n\t\tfor my $i ( 0 .. @_ - 3 ){\n\t\t\tprint join ' ', map $_ + 1, $i, @_ - 2, @_ - 1;\n\t\t\t}\n\t\t}\n\telse{\n\t\tprint -1;\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @sorted = sort { $a <=> $b } @_;\n\t\n\tif( \"@sorted\" eq \"@_\" ){\n\t\tprint 0;\n\t\tnext;\n\t\t}\n\t\n\tif( $_[ @_ - 1 ] >= 0 and $_[ @_ - 1 ] >= $_[ @_ - 2 ] ){\n\t\tprint @_ - 2;\n\t\tfor my $i ( 0 .. @_ - 3 ){\n\t\t\tprint join ' ', $i, @_ - 2, @_ - 1;\n\t\t\t}\n\t\t}\n\telse{\n\t\tprint -1;\n\t\t}\n\t}"}], "src_uid": "c3ee6419adfc85c80f35ecfdea6b0d43"} {"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$$$) \u2014 the number of T-shirts. The $$$i$$$-th of the next $$$n$$$ lines contains $$$a_i$$$ \u2014 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$$$ \u2014 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": "%prev;\n%now;\n$num=<>;\nfor(1..$num){\n $shirt=<>;\n $shirt=~s/\\n//;\n $prev{$shirt}=$prev{$shirt}+1;\n}\nfor(1..$num){\n $shirt=<>;\n $shirt=~s/\\n//;\n $cur{$shirt}=$cur{$shirt}+1;\n}\n\n$sum = 0;\nforeach(keys%prev){\n $diff=$prev{$_}-$cur{$_};\n $sum+=$diff if $diff>0;\n}\nprint $sum;"}], "negative_code": [], "src_uid": "c8321b60a6ad04093dee3eeb9ee27b6f"} {"nl": {"description": "Eugeny has array a\u2009=\u2009a1,\u2009a2,\u2009...,\u2009an, 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\u2009\u2264\u2009li\u2009\u2264\u2009ri\u2009\u2264\u2009n). The response to the query will be integer 1, if the elements of array a can be rearranged so as the sum ali\u2009+\u2009ali\u2009+\u20091\u2009+\u2009...\u2009+\u2009ari\u2009=\u20090, 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20092\u00b7105). The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (ai\u2009=\u2009-1,\u20091). Next m lines contain Eugene's queries. The i-th line contains integers li,\u2009ri (1\u2009\u2264\u2009li\u2009\u2264\u2009ri\u2009\u2264\u2009n).", "output_spec": "Print m integers \u2014 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": "sub get { split \" \", <> }\n($n, $m) = get; $_ = <>; $c1 = y/-/-/; $c2 = y/1/1/; $c2 -= $c1;\nfor (1..$m) {\n\t($l, $r) = get; $c = $r - $l + 1;\n\tif ($c & 1) {\n\t\t$r = 0;\n\t} else {\n\t\t$c /= 2;\n\t\t$r = ($c <= $c1 && $c <= $c2? 1: 0);\n\t}\n\tprint $r, \"\\n\"\n}"}, {"source_code": "sub get { split \" \", <> }\n($n, $m) = get; $_ = <>; $c1 = () = /-/g; $c2 = () = /1/g; $c2 -= $c1;\nfor (1..$m) {\n\t($l, $r) = get; $c = $r - $l + 1;\n\tif ($c & 1) {\n\t\t$r = 0;\n\t} else {\n\t\t$c /= 2;\n\t\t$r = ($c <= $c1 && $c <= $c2? 1: 0);\n\t}\n\tprint $r, \"\\n\"\n}"}, {"source_code": "my ($n, $m) = split / /, <>;\nmy @a = split / /, <>;\nforeach (@a) {$one++ if $_==1;}\n$pair = ($one < $n-$one) ? $one : $n-$one;\nwhile (<>) {\n\tchomp;\n\t($l, $r) = split / /;\n\tif (($r-$l) & 1) {\n\t\tif (($r-$l+1) > $pair*2) {\n\t\t\tprint \"0\\n\";\n\t\t} else {\n\t\t\tprint \"1\\n\";\n\t\t}\n\t} else {\n\t\tprint \"0\\n\";\n\t}\n}"}, {"source_code": "($n, $q) = split /\\s+/, ;\n@ints = split /\\s+/, ;\n$m = 0;\n$p = 0;\nmap { $m++ if $_ == -1; $p++ if $_ == 1; } @ints;\n\n#print \"Got $m negatives and $p positives\\n\";\nwhile($q--) {\n\t($l, $r) = split /\\s+/, ;\n\tif ($l == $r){ print \"0\\n\"; next; };\n#\tif($l > $m){ print \"0\\n\"; next; };\n#\tif($r < $m){ print \"0\\n\"; next; };\n\t$d = 1 + $r - $l;\n\tif($d % 2 == 1){ print \"0\\n\"; next; };\n#\tprint \"l=$l, r=$r, Want \".($d/2).\" of both\\n\";\n\tif($d/2 <= $m && $d/2 <= $p){ print \"1\\n\"; next; };\n\tprint \"0\\n\";\n}"}, {"source_code": "while (<>){\n / /;\n @_=split/ /,<>;\n $i=0;\n for (@_){$_==1 and $i++}\n $i = $`-$i<$i?$`-$i:$i;\n # print \" $i\\n\";\n \n for (1..$'){\n <>=~/ /;\n print ((abs($`-$')) %2==1 && (abs($`-$'))<=$i*2?1:0);\n print\"\\n\";\n }\n }"}], "negative_code": [{"source_code": "($n, $q) = split /\\s+/, ;\n@ints = split /\\s+/, ;\n$m = 0;\n$p = 0;\nmap { $m++ if $_ == -1; $p++ if $_ == 1; } @ints;\n\n#print \"Got $m negatives and $p positives\\n\";\nwhile($q--) {\n\t($l, $r) = split /\\s+/, ;\n\tif ($l == $r){ print \"0\\n\"; next; };\n\tif($l > $m){ print \"0\\n\"; next; };\n\tif($r < $m){ print \"0\\n\"; next; };\n\t$d = 1 + $r - $l;\n\tif($d % 2 == 1){ print \"0\\n\"; next; };\n\tif($d/2 <= $m && $d/2 <= $p){ print \"1\\n\"; next; };\n\tprint \"0\\n\";\n}"}, {"source_code": "#!/usr/bin/perl\n\n($n,$m)=split/ /,<>;\n$_=<>;\ns/-/++$x/ge;\nfor(1..$m){\n <>=~/ /;\n $d=$`-$'+1;\n printf \"%d\\n\", $d%2 || $d>$x*2?0:1;\n }"}, {"source_code": "#!/usr/bin/perl\n\n($n,$m)=split/ /,<>;\n$_=<>;\ns/-/++$x/ge;\n$n-$x<$x and $x=$n-$x;\nfor(1..$m){\n <>=~/ /;\n $d=$`-$'+1;\n printf \"%d\\n\", $d%2 || $d>$x*2?0:1;\n }"}, {"source_code": "while (<>){\n / /;\n @_=split/ /,<>;\n $i=0;\n for (@_){$_==1 and $i++}\n $i = $`>$i?$`:$i;\n \n for (1..$'){\n <>=~/ /;\n print ((abs($`-$')) %2==0 && (abs($`-$'))<=$i?0:1);\n print\"\\n\";\n }\n }"}], "src_uid": "deeb49969ac4bc4f1c7d76b89ac1402f"} {"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\u2009-\u2009a\u2009\u2264\u2009c, just the new word is appended to other words on the screen. If b\u2009-\u2009a\u2009>\u2009c, then everything on the screen disappears and after that the word you have typed appears on the screen.For example, if c\u2009=\u20095 and you typed words at seconds 1,\u20093,\u20098,\u200914,\u200919,\u200920 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000,\u20091\u2009\u2264\u2009c\u2009\u2264\u2009109)\u00a0\u2014 the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1,\u2009t2,\u2009...,\u2009tn (1\u2009\u2264\u2009t1\u2009<\u2009t2\u2009<\u2009...\u2009<\u2009tn\u2009\u2264\u2009109), 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\u2009-\u20091\u2009>\u20091. 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\u2009-\u20099\u2009\u2264\u20091."}, "positive_code": [{"source_code": "@line = split(/\\s/, <>);\n$n = $line[0];\n$c = $line[1];\n$ans = 0;\n$last = 0;\nforeach $i (split(/\\s/, <>)) {\n if ($i - $last > $c) {\n $ans = 0;\n }\n $ans++;\n $last = $i;\n}\nprint $ans, \"\\n\";\n"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nmy ($n, $c) = split ' ', <>;\nmy @times = split ' ', <>;\nmy $last = 0;\nmy $count = 0;\n\nfor my $time (@times) {\n if ($time - $last > $c) {\n $count = 1;\n } else {\n $count++;\n }\n\n $last = $time;\n}\n\nsay $count;\n"}, {"source_code": "use strict;\nuse warnings;\n\nuse Data::Dumper;\n$,=\" \";\nmy ($n,$c)=split / /,<>;\nchomp($n,$c);\nmy $sum=1;\nmy @t=split / /,<>;\nchomp(@t);\nmy $a=shift @t;\nfor(@t)\n{\nif($_-$a<=$c)\n{\n$sum++ ;\n}\nelse\n{\n $sum=1;\n}\n$a=$_;\n}\n\nprint $sum . \"\\n\";\n"}, {"source_code": "$\\ = $/; ($_, $c) = split ' ', <>; @t = reverse split ' ', <>;\n$p = shift @t; $n = 1;\nfor (@t) {\n $p - $_ > $c && last;\n $n++; $p = $_;\n}\nprint $n;\n"}, {"source_code": "$\\ = $/; ($_, $c) = split ' ', <>; @t = split ' ', <>;\n$p = pop @t;\n$n = 1;\nfor (reverse @t) {\n last if ($p - $_ > $c);\n $n++; $p = $_;\n}\nprint $n;\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $c) = split;\n\t@_ = reverse split ' ', <>;\n\t$last = $_[0];\n\t$i = 0;\n\tfor (@_){\n\t\t$last - $_ > $c and last;\n\t\t$last = $_;\n\t\t$i ++;\n\t\t}\n\t\n\tprint $i;\n\t}"}], "negative_code": [{"source_code": "($n, $c) = split ' ', <>;\n\n$_ = ' ' . <>;\ns/.*(\\b\\d+) (\\d+\\b)(??{ $2 - $1 <= $c })//;\ns/\\d//g;\n\nprint length"}], "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)$$$ \u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ % $m } ++, @_;\n\t\n\tmy $ans = 0;\n\t\n\t$ans += 1 if exists $h{ 0 };\n\t\n\tfor my $i ( 1 .. $m / 2 ){\n\t\tlast if $i == $m - $i;\n\t\t@_ = sort { $a <=> $b } $h{ $i } // 0, $h{ $m - $i } // 0;\n\t\t\n\t\tif( $_[ 0 ] > 0 ){\n\t\t\t$ans ++;\n\t\t\t\n\t\t\t$ans += $_[ 1 ] - $_[ 0 ] - ( $_[ 1 ] != $_[ 0 ] and 1 );\n\t\t\t}\n\t\telse{\n\t\t\t$ans += $_[ 1 ];\n\t\t\t}\n\t\t}\n\t\n\tif( $m % 2 == 0 ){\n\t\t$ans += exists $h{ $m / 2 };\n\t\t}\n\t\n\tprint $ans;\n\t}"}], "negative_code": [], "src_uid": "d107db1395ded62904d0adff164e5c1e"} {"nl": {"description": "You are given two very long integers a,\u2009b (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,\u2009b may contain leading zeroes. Each of them contains no more than 106 digits.", "output_spec": "Print the symbol \"<\" if a\u2009<\u2009b and the symbol \">\" if a\u2009>\u2009b. 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": "$a=<>; chomp($a);\n$b=<>; chomp($b);\n$a=~/^(0*)./; $la=length($a)-length($1); \n$b=~/^(0*)./; $lb=length($b)-length($1);\nif ($la>$lb) {\n\t$r=1;\n} elsif ($la<$lb) {\n\t$r=-1;\n} else {\n\t$r=substr($a,-$la) cmp substr($b,-$lb);\n}\nprint [\"<\",\"=\",\">\"]->[$r+1];\n"}, {"source_code": "print qw(= > <)[ eval join 'cmp', map { sprintf \"'%01000001s'\", $_ } <> ]\n"}, {"source_code": "print qw(= > <)[ eval join 'cmp', map { sprintf \"'%01000001s'\", $_ } <> ]"}, {"source_code": "# open STDIN,\"<\",\"a.in\" or die $!;\nmy $a1 = ReadLine();\nmy $a2 = ReadLine();\n$a1 =~ s/^0+//;\n$a2 =~ s/^0+//;\n\n# print \"$a1 $a2\";\nmy ($len1,$len2) = (length($a1),length($a2));\nif($len1 eq $len2) {\n if($a1 eq $a2) {print \"=\";}\n elsif($a1 ge $a2) {print \">\";}\n else {print \"<\"};\n}\nelsif($len1 ge $len2) {print \">\";}\nelse {print \"<\";}\n\nprint \"\\n\";\n\nsub ReadLine {\n my $line = ;\n chop($line);\n my $lll = length($line);\n while(not $lll) {\n $line = ;\n chop($line);\n $lll = length($line);\n }\n return $line;\n}"}, {"source_code": "#open STDIN,\"<\",\"a.in\" or die $!;\nmy $a1 = ReadLine();\nmy $a2 = ReadLine();\n$a1 =~ s/^0+//; # \u53bb\u524d\u5bfc\u96f6\n$a2 =~ s/^0+//;\n\n# print \"$a1 $a2\";\nmy ($len1,$len2) = (length($a1),length($a2));\nif($len1 eq $len2) {\n if($a1 eq $a2) {print \"=\";}\n elsif($a1 ge $a2) {print \">\";}\n else {print \"<\"};\n}\nelsif($len1 ge $len2) {print \">\";}\nelse {print \"<\";}\n\nprint \"\\n\";\n\nsub ReadLine { # \u8bfb\u4e00\u884c\u975e\u7a7a\u7684\u5b57\u7b26\u4e32\n chop(my $line = );\n while(not length($line)) {\n chop($line = );\n }\n return $line;\n}"}, {"source_code": "#open STDIN,\"<\",\"a.in\" or die $!;\nmy $a1 = ReadLine();\nmy $a2 = ReadLine();\n$a1 =~ s/^0+//;\n$a2 =~ s/^0+//;\n\n# print \"$a1 $a2\";\nmy ($len1,$len2) = (length($a1),length($a2));\nif($len1 eq $len2) {\n if($a1 eq $a2) {print \"=\";}\n elsif($a1 ge $a2) {print \">\";}\n else {print \"<\"};\n}\nelsif($len1 ge $len2) {print \">\";}\nelse {print \"<\";}\n\nprint \"\\n\";\n\nsub ReadLine {\n my $line = ;\n chop($line);\n while(not length($line)) {\n chop($line = );\n }\n return $line;\n}"}, {"source_code": "$ch1=<>;\nchomp $ch1;\n$ch2=<>;\nchomp$ch2;\n\n$ch1=~m/^(0*)./;\n\n$ll1=length$1; \n$ch2=~m/^(0*)./;\n\n$ll2=length$1; \n$l1=length$ch1;\n$l2=length$ch2;\n\nif($l1-$ll1>$l2-$ll2){\nprint \">\";\n}\nelsif($l1-$ll1<$l2-$ll2){\nprint \"<\";\n}\n\n \nelsif(substr($ch1,$ll1) gt substr($ch2,$ll2)){\nprint \">\";\n}\nelsif(substr($ch1,$ll1) lt substr($ch2,$ll2)){\nprint \"<\";\n}\nelse{\nprint \"=\";\n}"}, {"source_code": "print qw(= > <)[ eval join 'cmp', map { sprintf \"'%01000001s'\", $_ } <> ]"}, {"source_code": "print qw(= > <)[ eval join 'cmp', map { sprintf \"'%01000001s'\", $_ } <> ]"}, {"source_code": "print qw(= > <)[ eval join 'cmp', map { sprintf \"'%01000001s'\", $_ } <> ]\n"}, {"source_code": "$a = <>;\n$a =~ s/^0+//;\n$b = <>;\n$b =~ s/^0+//;\n$s = length( $a ) <=> length($b);\nif ( $s ) {\n\tresult( $s );\n}\nelse {\n\t@ma = split //,$a;\n\t@mb = split //,$b;\n\n\tfor $i (0..length($a)) {\n\t $s = $ma[$i] <=> $mb[$i];\n\t if ( $s ) {\n\t \t\tresult( $s );\n\t }\n\t}\n\tresult( 0 );\n}\n\nsub result {\n my $s = shift;\n\tif( $s > 0 ) {\n\t\tprint \">\";\n\t}\n\telsif ( $s < 0 ) {\n\t\tprint \"<\";\n\t}\n\telse {\n\t\tprint \"=\";\n\t}\t\n\texit;\n}"}, {"source_code": "print qw(= > <)[ eval join 'cmp', map { sprintf \"'%01000001s'\", $_ } <> ]\n"}, {"source_code": "print qw(= > <)[ eval join 'cmp', map { sprintf \"'%01000001s'\", $_ } <> ]"}, {"source_code": "($A, $B) = map { chomp; s/^0+//r } <>;\n\n$W = 0;\n\nprint qw(= > <)[ length $A <=> length $B || do { \n\t\t\tfor (1 .. length $A) { $W = chop $A <=> chop $B || $W }\n\t\t\t$W\n\t\t} \n\t]"}, {"source_code": "print qw(= > <)[ eval join 'cmp', map { sprintf \"'%01000001s'\", $_ } <> ]\n"}], "negative_code": [{"source_code": "$a=<>; chomp($a);\n$b=<>; chomp($b);\n$a=~/^0*/; $la=length($a)-length($&); \n$b=~/^0*/; $lb=length($b)-length($&);\nif ($la>$lb) {\n\t$r=1;\n} elsif ($la<$lb) {\n\t$r=-1;\n} else {\n\t$r=substr($a,-$la) cmp substr($b,-$lb);\n}\nprint [\"<\",\"=\",\">\"]->[$r+1];\n"}, {"source_code": "my $a1 = ;\nmy $a2 = ;\n$a1 =~ s/0+//;\n$a2 =~ s/0+//;\nmy ($len1,$len2) = (length($a1),length($a2));\nif($len1 eq $len2) {\n if($a1 eq $a2) {print \"=\";}\n elsif($a1 ge $a2) {print \">\";}\n else {print \"<\"};\n}\nelsif($len1 ge $len2) {print \">\";}\nelse {print \"<\";}"}, {"source_code": "#open STDIN,\"<\",\"a.in\" or die $!;\nmy $a1 = ;\nmy $a2 = ;\n$a1 =~ s/^0+//;\n$a2 =~ s/^0+//;\n\nmy ($len1,$len2) = (length($a1),length($a2));\nif($len1 eq $len2) {\n if($a1 eq $a2) {print \"=\";}\n elsif($a1 ge $a2) {print \">\";}\n else {print \"<\"};\n}\nelsif($len1 ge $len2) {print \">\";}\nelse {print \"<\";}"}, {"source_code": "my $a1 = ;\nmy $a2 = ;\nif($a1 == $a2) {\n print \"=\"; \n}\nelsif($a1 > $a2) {\n print \">\";\n}\nelse {\n print \"<\";\n}"}, {"source_code": "my $a1 = ;\nmy $a2 = ;\n$a1 =~ s/^0+//;\n$a2 =~ s/^0+//;\nmy ($len1,$len2) = (length($a1),length($a2));\nif($len1 eq $len2) {\n if($a1 eq $a2) {print \"=\";}\n elsif($a1 ge $a2) {print \">\";}\n else {print \"<\"};\n}\nelsif($len1 ge $len2) {print \">\";}\nelse {print \"<\";}"}, {"source_code": "#open STDIN,\"<\",\"a.in\" or die $!;\nmy $a1 = ;\nmy $a2 = ;\n$a1 =~ s/^0+//;\n$a2 =~ s/^0+//;\n\nmy ($len1,$len2) = (length($a1),length($a2));\nprint \"$len1 $len2\" if ($a2 eq \"8\");\nif($len1 eq $len2) {\n if($a1 eq $a2) {print \"=\";}\n elsif($a1 ge $a2) {print \">\";}\n else {print \"<\"};\n}\nelsif($len1 ge $len2) {print \">\";}\nelse {print \"<\";}"}, {"source_code": "$ch1=<>;\nchomp $ch1;\n$ch2=<>;\nchomp$ch2;\n$num1=int($ch1);\n$num2=int($ch2);\nif($num2==$num1){\nprint \"=\";\n}\nelsif ($num1>$num2){\nprint\">\";\n}\nelse{\nprint \"<\";\n}"}, {"source_code": "$ch1=<>;\nchomp $ch1;\n$ch2=<>;\nchomp$ch2;\n\n$ch1=~m/^(0*)./;\n$l1=length$1; \n$ch2=~m/^(0*)./;\n$l2=length$1;\n \nif(substr($ch1,$l1) gt substr($ch2,$l2)){\nprint \">\";\n}\nelsif(substr($ch1,$l1) lt substr($ch2,$l2)){\nprint \"<\";\n}\nelse{\nprint \"=\";\n}"}, {"source_code": "print qw(= > <)[<><=><>]"}, {"source_code": "use bigint;\n\nprint qw(= > <)[ eval join '<=>', map { s/^0+// ; 0 + $_ } <> ]"}, {"source_code": "use bigint;\nprint qw(= > <)[<><=><>]"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000). The second line contains n non-negative integers ai without leading zeroes\u00a0\u2014 the number of tanks of the i-th country. It is guaranteed that the second line contains at least n\u2009-\u20091 beautiful numbers and the total length of all these number's representations doesn't exceed 100\u2009000.", "output_spec": "Print a single number without leading zeroes\u00a0\u2014 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": "$_ = (<>,<>), chomp;\n\nprint /\\b0\\b/ ? 0 : ( s/ ?\\b1(0*)\\b ?/ $z .= $1, '' /ger || 1) . $z\n"}, {"source_code": "$_ = (<>,<>), chomp;\n\nprint /\\b0\\b/ ? 0 : ( s/ ?\\b1(0*)\\b ?/ $z .= $1, '' /ger || 1) . $z"}, {"source_code": "$_ = (<>,<>), chomp;\n\nprint /\\b0\\b/ ? 0 : ( s/ ?\\b1(0*)\\b ?/ $z .= $1, '' /ger || 1) . $z\n"}, {"source_code": "$_ = (<>,<>), chomp;\n\nprint /\\b0\\b/ ? 0 : ( s/ ?\\b1(0*)\\b ?/ $z .= $1, '' /ger || 1) . $z"}, {"source_code": "$_ = (<>,<>), chomp;\n\nprint /\\b0\\b/ ? 0 : ( s/ ?\\b1(0*)\\b ?/ $z .= $1, '' /ger || 1) . $z\n"}, {"source_code": "$_ = (<>,<>), chomp;\n\nprint /\\b0\\b/ ? 0 : ( s/ ?\\b1(0*)\\b ?/ $z .= $1, '' /ger || 1) . $z\n"}, {"source_code": "@_ = split ' ', (<>, <>);\n\nprint( (grep !$_, @_) ? \n\t0\n: \n\t( (join '', grep !/^10*$/, @_) || 1 ) \n\t. 0 x eval join '+', map ~- length, grep /^10*$/, @_ \n)"}, {"source_code": "<>;\n\n@_ = split ' ', <>;\n\nprint( (grep !$_,@_) ? \n\t0\n: \n\tdo { \n\t\t( (join '', grep !/^10*$/, @_) || 1 ) \n\t\t. 0 x eval join '+', map { -1 + length } grep /^10*$/, @_ \n\t}\n)"}, {"source_code": "$_ = (<>,<>), chomp;\n\nprint /\\b0\\b/ ? 0 : ( s/\\b10*\\b/ '' x ($z +=~- length $&) /ger =~ s/ //gr || 1) . 0 x $z"}, {"source_code": "$_ = (<>,<>), chomp;\n\nprint /\\b0\\b/ ? 0 : ( s/ ?\\b1(0*)\\b ?/ $z .= $1, '' /ger || 1) . $z"}, {"source_code": "$_ = (<>,<>), chomp;\n\nprint /\\b0\\b/ ? 0 : ( s/ ?\\b1(0*)\\b ?/ $z .= $1, '' /ger || 1) . $z\n"}], "negative_code": [{"source_code": "@_ = split ' ', (<>, <>);\n\nprint $x = (join '', grep !/^10*$/, @_) || 1,\n\t0 x (eval join '+', map ~- length, grep /^10*$/, @_) x!! $x"}], "src_uid": "9b1b082319d045cf0ec6d124f97a8184"} {"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',\u2009E') of a graph G(V,\u2009E) 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\u00a0(1\u2009\u2264\u2009n\u2009\u2264\u2009500), . 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\u00a0(1\u2009\u2264\u2009xi\u2009\u2264\u2009106), 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,\u2009bi,\u2009ci\u00a0(1\u2009\u2264\u2009ai\u2009<\u2009bi\u2009\u2264\u2009n;\u00a01\u2009\u2264\u2009ci\u2009\u2264\u2009103), 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\u2009-\u20099.", "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": "<>;$_=<>;@a=split ' ';$r=0;\nwhile (<>) {\n ($u,$v,$c) = split ' ';\n $x = ($a[$u-1]+$a[$v-1])/$c;\n $r = $x if $x > $r;\n}\nprint $r;\n\n"}, {"source_code": "<>;$_=<>;@a=split ' ';$r=0;\nwhile (<>) {\n ($u,$v,$c) = split ' ';\n $x = ($a[$u-1]+$a[$v-1])/$c;\n $r = $x if $x > $r;\n}\nprint $r;\n\n"}, {"source_code": "<>;$_=<>;@a=split ' ';$r=0;\nwhile (<>) {\n ($u,$v,$c) = split ' ';\n $x = ($a[$u-1]+$a[$v-1])/$c;\n $r = $x if $x > $r;\n}\nprint $r;\n"}, {"source_code": "<>;$_=<>;@a=split ' ';$r=0;\nwhile (<>) {\n ($u,$v,$c) = split ' ';\n $x = ($a[$u-1]+$a[$v-1])/$c;\n $r = $x if $x > $r;\n}\nprint $r;\n"}, {"source_code": "<>;$_=<>;@a=split ' ';$r=0;\nwhile (<>) {\n ($u,$v,$c) = split ' ';\n $x = ($a[$u-1]+$a[$v-1])/$c;\n $r = $x if $x > $r;\n}\nprint $r;\n\n"}, {"source_code": "<>;$_=<>;@a=split ' ';$r=0;\nwhile (<>) {\n ($u,$v,$c) = split ' ';\n $x = ($a[$u-1]+$a[$v-1])/$c;\n $r = $x if $x > $r;\n}\nprint $r;\n\n"}, {"source_code": "<>;$_=<>;@a=split ' ';$r=0;\nwhile (<>) {\n ($u,$v,$c) = split ' ';\n $x = ($a[$u-1]+$a[$v-1])/$c;\n $r = $x if $x > $r;\n}\nprint $r;\n\n"}, {"source_code": "<>;$_=<>;@a=split ' ';$r=0;\nwhile (<>) {\n ($u,$v,$c) = split ' ';\n $x = ($a[$u-1]+$a[$v-1])/$c;\n $r = $x if $x > $r;\n}\nprint $r;\n\n"}, {"source_code": "<>;$_=<>;@a=split ' ';$r=0;\nwhile (<>) {\n ($u,$v,$c) = split ' ';\n $x = ($a[$u-1]+$a[$v-1])/$c;\n $r = $x if $x > $r;\n}\nprint $r;\n\n"}, {"source_code": "<>;$_=<>;@a=split ' ';$r=0;\nwhile (<>) {\n ($u,$v,$c) = split ' ';\n $x = ($a[$u-1]+$a[$v-1])/$c;\n $r = $x if $x > $r;\n}\nprint $r;\n\n"}], "negative_code": [{"source_code": "<>;$_=<>;@a=split ' ';\nwhile (<>) {\n ($u,$v,$c) = split ' ';\n $x = ($a[$u-1]+$a[$v-1])/$c;\n $res = $x if $x > $res;\n}\nprint $res;\n"}], "src_uid": "ba4304e79d85d13c12233bcbcce6d0a6"} {"nl": {"description": "After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer.At the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue before Kay and Gerda's house even in the night. Each person in the queue wants either to take several ice cream packs for himself and his friends or to give several ice cream packs to Kay and Gerda (carriers that bring ice cream have to stand in the same queue).If a carrier with d ice cream packs comes to the house, then Kay and Gerda take all his packs. If a child who wants to take d ice cream packs comes to the house, then Kay and Gerda will give him d packs if they have enough ice cream, otherwise the child will get no ice cream at all and will leave in distress.Kay wants to find the amount of ice cream they will have after all people will leave from the queue, and Gerda wants to find the number of distressed kids.", "input_spec": "The first line contains two space-separated integers n and x (1\u2009\u2264\u2009n\u2009\u2264\u20091000, 0\u2009\u2264\u2009x\u2009\u2264\u2009109). Each of the next n lines contains a character '+' or '-', and an integer di, separated by a space (1\u2009\u2264\u2009di\u2009\u2264\u2009109). Record \"+ di\" in i-th line means that a carrier with di ice cream packs occupies i-th place from the start of the queue, and record \"- di\" means that a child who wants to take di packs stands in i-th place.", "output_spec": "Print two space-separated integers\u00a0\u2014 number of ice cream packs left after all operations, and number of kids that left the house in distress.", "sample_inputs": ["5 7\n+ 5\n- 10\n- 20\n+ 40\n- 20", "5 17\n- 16\n- 2\n- 98\n+ 100\n- 98"], "sample_outputs": ["22 1", "3 2"], "notes": "NoteConsider the first sample. Initially Kay and Gerda have 7 packs of ice cream. Carrier brings 5 more, so now they have 12 packs. A kid asks for 10 packs and receives them. There are only 2 packs remaining. Another kid asks for 20 packs. Kay and Gerda do not have them, so the kid goes away distressed. Carrier bring 40 packs, now Kay and Gerda have 42 packs. Kid asks for 20 packs and receives them. There are 22 packs remaining. "}, "positive_code": [{"source_code": "($n, $x) = split \" \", <>; $y=0;\nfor (<>) {\n\ts/ //;\n\tif ($x + $_ < 0) {\n\t\t$y++;\n\t} else {\n\t\t$x += $_;\t\n\t}\n}\nprint \"$x $y\""}, {"source_code": "for (<>){\n\t\n\t/\\d+$/;\n\t$u = $&;\n\t\n\t$m +=!/-/?\n\t\t$u\n\t:\n\t\t$u > $m ?\n\t\t\t$g ++ * 0\n\t\t:\n\t\t\t- $u\n\t}\n\t\nprint $m, ' ', 0 + $g"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m) = split;\n\t$g = 0;\n\tfor (1 .. $n){\n\t\t($i, $u) = split ' ', <>;\n\t\tif ($i eq '+'){\n\t\t\t$m += $u;\n\t\t\t}\n\t\telse {\n\t\t\tif( $u > $m ){\n\t\t\t\t$g++;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$m -= $u;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint \"$m $g\";\n\t}"}], "negative_code": [], "src_uid": "0a9ee8cbfa9888caef39b024563b7dcd"} {"nl": {"description": "Alice and Bob are playing a game on a sequence $$$a_1, a_2, \\dots, a_n$$$ of length $$$n$$$. They move in turns and Alice moves first.In the turn of each player, he or she should select an integer and remove it from the sequence. The game ends when there is no integer left in the sequence. Alice wins if the sum of her selected integers is even; otherwise, Bob wins. Your task is to determine who will win the game, if both players play optimally.", "input_spec": "Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) \u2014 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 100$$$), indicating the length of the sequence. 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$$$), indicating the elements of the sequence.", "output_spec": "For each test case, output \"Alice\" (without quotes) if Alice wins and \"Bob\" (without quotes) otherwise.", "sample_inputs": ["4\n\n3\n\n1 3 5\n\n4\n\n1 3 5 7\n\n4\n\n1 2 3 4\n\n4\n\n10 20 30 40"], "sample_outputs": ["Alice\nAlice\nBob\nAlice"], "notes": "NoteIn the first and second test cases, Alice always selects two odd numbers, so the sum of her selected numbers is always even. Therefore, Alice always wins.In the third test case, Bob has a winning strategy that he always selects a number with the same parity as Alice selects in her last turn. Therefore, Bob always wins.In the fourth test case, Alice always selects two even numbers, so the sum of her selected numbers is always even. Therefore, Alice always wins."}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse Data::Dumper;\nuse integer; ### important!\nmy $mod = 10 ** 9 + 7;\n# my $mod = 998244353;\n\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\n\nwhile( $testcase -- > 0 ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @A = map { ( ( $_ - 0 ) + 1000001000 ) % 2 } split(/\\s+/,);\n # print STDERR ( join(':',@A) . \"\\n\" );\n my @c = (0,0);\n for(my $i=0;$i<=$#A;$i++){\n $c[ $A[$i] ]++;\n }\n #if( $c[0] == 0 ){\n # print ( ( ( ($n + 1)/2 ) % 2 == 0 ) ? \"Alice\\n\" : \"Bob\\n\" );\n # next;\n #}\n #if( $c[1] == 0 ){\n # print \"Alice\\n\";\n # next;\n #}\n my @dp = ();\n for(my $i=0;$i<=$c[0];$i++){\n my @tmp = ();\n for(my $j=0;$j<=$c[1];$j++){\n push(@tmp,+[-1,-1]);\n }\n push(@dp,\\@tmp);\n }\n \n $dp[ $c[0] ][ $c[1] ][ 0 ] = 0;\n $dp[ $c[0] ][ $c[1] ][ 1 ] = 1;\n for(my $i=$n; $i>=1 ; $i--){\n for(my $z=$i;$z>=0;$z--){\n my $o = $i-$z;\n next unless $o>=0 and $o<=$c[1] and $z<=$c[0];\n if( $i % 2 == 0 ){ # bob\n if( $z>0 ){\n $dp[ $z-1 ][ $o ][0] = &max1( $dp[ $z-1 ][ $o ][0] , $dp[ $z ][ $o ][0] );\n $dp[ $z-1 ][ $o ][1] = &max1( $dp[ $z-1 ][ $o ][1] , $dp[ $z ][ $o ][1] );\n }\n if( $o>0 ){\n $dp[ $z ][ $o -1 ][0] = &max1( $dp[ $z ][ $o -1 ][0] , $dp[ $z ][ $o ][0] );\n $dp[ $z ][ $o -1 ][1] = &max1( $dp[ $z ][ $o -1 ][1] , $dp[ $z ][ $o ][1] );\n }\n } else {\n if( $z>0 ){\n $dp[ $z-1 ][ $o ][0] = &min1( $dp[ $z-1 ][ $o ][0] , $dp[ $z ][ $o ][0] );\n $dp[ $z-1 ][ $o ][1] = &min1( $dp[ $z-1 ][ $o ][1] , $dp[ $z ][ $o ][1] );\n }\n if( $o>0 ){\n $dp[ $z ][ $o -1 ][0] = &min1( $dp[ $z ][ $o -1 ][0] , $dp[ $z ][ $o ][1] );\n $dp[ $z ][ $o -1 ][1] = &min1( $dp[ $z ][ $o -1 ][1] , $dp[ $z ][ $o ][0] );\n }\n }\n }\n }\n # print STDERR &Dumper(\\@dp);\n if( $dp[ 0 ][ 0 ][0] == 0 ){\n print \"Alice\\n\";\n } else {\n print \"Bob\\n\";\n }\n}\n\nexit(0);\n\nsub max1 {\n my $v1 = shift; my $v2 = shift;\n if( $v1 < 0 ){ return $v2; }\n return ( $v1 > $v2 ? $v1 : $v2 );\n}\nsub min1 {\n my $v1 = shift; my $v2 = shift;\n if( $v1 < 0 ){ return $v2; }\n return ( $v1 > $v2 ? $v2 : $v1 );\n}\nsub max {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\n return $r;\n}\nsub min {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\n return $r;\n}\nsub sum {\n my $r = 0;\n foreach my $e (@_){ $r += $e; }\n return $r;\n}\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\n my $r = shift; my $ar = shift;\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\n for(my $i=0;$i<=$#ar;$i++){ \n $rr += $ar->[$i];\n $r->[1+$i] = $rr;\n }\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse Data::Dumper;\nuse integer; ### important!\nmy $mod = 10 ** 9 + 7;\n# my $mod = 998244353;\n\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\n\nwhile( $testcase -- > 0 ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @A = map { ( ( $_ - 0 ) + 1000001000 ) % 2 } split(/\\s+/,);\n # print STDERR ( join(':',@A) . \"\\n\" );\n my @c = (0,0);\n for(my $i=0;$i<=$#A;$i++){\n $c[ $A[$i] ]++;\n }\n #if( $c[0] == 0 ){\n # print ( ( ( ($n + 1)/2 ) % 2 == 0 ) ? \"Alice\\n\" : \"Bob\\n\" );\n # next;\n #}\n #if( $c[1] == 0 ){\n # print \"Alice\\n\";\n # next;\n #}\n my @dp = ();\n for(my $i=0;$i<=$c[0];$i++){\n my @tmp = ();\n for(my $j=0;$j<=$c[1];$j++){\n push(@tmp,+[-1,-1]);\n }\n push(@dp,\\@tmp);\n }\n \n $dp[ $c[0] ][ $c[1] ][ 0 ] = 0;\n $dp[ $c[0] ][ $c[1] ][ 1 ] = 1;\n for(my $i=$n; $i>=1 ; $i--){\n for(my $z=$i;$z>=0;$z--){\n my $o = $i-$z;\n next unless $o>=0 and $o<=$c[1] and $z<=$c[0];\n if( $i % 2 == 0 ){ # bob\n if( $z>0 ){\n $dp[ $z-1 ][ $o ][0] = &min1( $dp[ $z-1 ][ $o ][0] , $dp[ $z ][ $o ][0] );\n $dp[ $z-1 ][ $o ][1] = &min1( $dp[ $z-1 ][ $o ][1] , $dp[ $z ][ $o ][1] );\n }\n if( $o>0 ){\n $dp[ $z ][ $o -1 ][0] = &min1( $dp[ $z ][ $o -1 ][0] , $dp[ $z ][ $o ][0] );\n $dp[ $z ][ $o -1 ][1] = &min1( $dp[ $z ][ $o -1 ][1] , $dp[ $z ][ $o ][1] );\n }\n } else {\n if( $z>0 ){\n $dp[ $z-1 ][ $o ][0] = &max1( $dp[ $z-1 ][ $o ][0] , $dp[ $z ][ $o ][0] );\n $dp[ $z-1 ][ $o ][1] = &max1( $dp[ $z-1 ][ $o ][1] , $dp[ $z ][ $o ][1] );\n }\n if( $o>0 ){\n $dp[ $z ][ $o -1 ][0] = &max1( $dp[ $z ][ $o -1 ][0] , $dp[ $z ][ $o ][1] );\n $dp[ $z ][ $o -1 ][1] = &max1( $dp[ $z ][ $o -1 ][1] , $dp[ $z ][ $o ][0] );\n }\n }\n }\n }\n # print STDERR &Dumper(\\@dp);\n if( $dp[ 0 ][ 0 ][0] == 0 ){\n print \"Alice\\n\";\n } else {\n print \"Bob\\n\";\n }\n}\n\nexit(0);\n\nsub max1 {\n my $v1 = shift; my $v2 = shift;\n if( $v1 < 0 ){ return $v2; }\n return ( $v1 > $v2 ? $v1 : $v2 );\n}\nsub min1 {\n my $v1 = shift; my $v2 = shift;\n if( $v1 < 0 ){ return $v2; }\n return ( $v1 > $v2 ? $v2 : $v1 );\n}\nsub max {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\n return $r;\n}\nsub min {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\n return $r;\n}\nsub sum {\n my $r = 0;\n foreach my $e (@_){ $r += $e; }\n return $r;\n}\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\n my $r = shift; my $ar = shift;\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\n for(my $i=0;$i<=$#ar;$i++){ \n $rr += $ar->[$i];\n $r->[1+$i] = $rr;\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse Data::Dumper;\nuse integer; ### important!\nmy $mod = 10 ** 9 + 7;\n# my $mod = 998244353;\n\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\n\nwhile( $testcase -- > 0 ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @A = map { ( ( $_ - 0 ) + 1000001000 ) % 2 } split(/\\s+/,);\n \n my @c = (0,0);\n for(my $i=0;$i<=$#A;$i++){\n $c[ $A[$i] ]++;\n }\n if( $c[0] == 0 ){\n print ( ( ( ($n + 1)/2 ) % 2 == 0 ) ? \"Alice\\n\" : \"Bob\\n\" );\n next;\n }\n if( $c[1] == 0 ){\n print \"Alice\\n\";\n next;\n }\n my @dp = ();\n for(my $i=0;$i<=$c[0];$i++){\n my @tmp = ();\n for(my $j=0;$j<=$c[1];$j++){\n push(@tmp,+[-1,-1]);\n }\n push(@dp,\\@tmp);\n }\n \n $dp[ $c[0] ][ $c[1] ][ 0 ] = 0;\n for(my $i=$n; $i>=1 ; $i--){\n for(my $z=$i;$z>=0;$z--){\n my $o = $i-$z;\n next unless $o>=0 and $o<=$c[1] and $z<=$c[0];\n if( $i % 2 == 0 ){ # bob\n if( $z>0 ){\n $dp[ $z-1 ][ $o ][0] = &min( $dp[ $z-1 ][ $o ][0] , $dp[ $z ][ $o ][0] );\n $dp[ $z-1 ][ $o ][1] = &min( $dp[ $z-1 ][ $o ][1] , $dp[ $z ][ $o ][1] );\n }\n if( $o>0 ){\n $dp[ $z ][ $o -1 ][0] = &min( $dp[ $z ][ $o -1 ][0] , $dp[ $z ][ $o ][0] );\n $dp[ $z ][ $o -1 ][1] = &min( $dp[ $z ][ $o -1 ][1] , $dp[ $z ][ $o ][1] );\n }\n } else {\n if( $z>0 ){\n $dp[ $z-1 ][ $o ][0] = &max( $dp[ $z-1 ][ $o ][0] , $dp[ $z ][ $o ][0] );\n $dp[ $z-1 ][ $o ][1] = &max( $dp[ $z-1 ][ $o ][1] , $dp[ $z ][ $o ][1] );\n }\n if( $o>0 ){\n $dp[ $z ][ $o -1 ][0] = &max( $dp[ $z ][ $o -1 ][0] , $dp[ $z ][ $o ][1] );\n $dp[ $z ][ $o -1 ][1] = &max( $dp[ $z ][ $o -1 ][1] , $dp[ $z ][ $o ][0] );\n }\n }\n }\n }\n if( $dp[ 0 ][ 0 ] == 0 ){\n print \"Alice\\n\";\n } else {\n print \"Bob\\n\";\n }\n}\n\nexit(0);\n\nsub max {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\n return $r;\n}\nsub min {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\n return $r;\n}\nsub sum {\n my $r = 0;\n foreach my $e (@_){ $r += $e; }\n return $r;\n}\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\n my $r = shift; my $ar = shift;\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\n for(my $i=0;$i<=$#ar;$i++){ \n $rr += $ar->[$i];\n $r->[1+$i] = $rr;\n }\n}\n"}], "src_uid": "d0c9f2f2037d093762fb87206f396850"} {"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.\u00a0e. horizontally and in the direction that increase your $$$x$$$ coordinate, or up, i.\u00a0e. 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.\u00a0e. 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 $$$1$$$, the third one\u00a0\u2014 $$$4$$$, the fourth one\u00a0\u2014 $$$4$$$. The cost of the path is $$$1 \\cdot 4 + 1 \\cdot 3 + 4 \\cdot 2 + 4 \\cdot 1 = 19$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\tmy $n = $_;\n\t@_ = split ' ', <>;\n\t\n\tmy $min_h = shift @_;\n\tmy $min_v = shift @_;\n\t\n\tmy $sum_h = $min_h;\n\tmy $sum_v = $min_v;\n\t\n\tmy $cnt_h = 1;\n\tmy $cnt_v = 1;\n\t\n\tmy $sum = $min_h * $n + $min_v * $n;\n\tmy $min_sum = $sum;\n\t\n\tfor my $i ( 1 .. $n - 2 ){\n\t\tmy $j = shift @_;\n\t\t\n\t\tif( $i % 2 == 1 ){\n\t\t\tif( $j < $min_h ){\n\t\t\t\t$min_h = $j;\n\t\t\t\t}\n\t\t\t$cnt_h ++;\n\t\t\t$sum_h += $j;\n\t\t\t}\n\t\telse{\n\t\t\tif( $j < $min_v ){\n\t\t\t\t$min_v = $j;\n\t\t\t\t}\n\t\t\t$cnt_v ++;\n\t\t\t$sum_v += $j;\n\t\t\t}\n\t\t\n\t\tmy $sum = $sum_h - $min_h + $sum_v - $min_v\n\t\t\t+ ( $n - $cnt_h + 1 ) * $min_h\n\t\t\t+ ( $n - $cnt_v + 1 ) * $min_v\n\t\t\t;\n\t\t\t\t\n\t\t$sum < $min_sum and $min_sum = $sum;\n\t\t}\n\t\n\tprint $min_sum;\n\t}"}], "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$$$) \u2014 the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \\le n \\le 100)$$$ \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\t$sum += $_ for @_;\n\t\n\tprint +( int $sum / @_ ) + !! ( $sum % @_ );\n\t}"}], "negative_code": [], "src_uid": "c457b77b16d94c6c4c95b7403a1dd0c7"} {"nl": {"description": "Polycarp is flying in the airplane. Finally, it is his favorite time \u2014 the lunchtime. The BerAvia company stewardess is giving food consecutively to all the passengers from the 1-th one to the last one. Polycarp is sitting on seat m, that means, he will be the m-th person to get food.The flight menu has k dishes in total and when Polycarp boarded the flight, he had time to count the number of portions of each dish on board. Thus, he knows values a1,\u2009a2,\u2009...,\u2009ak, where ai is the number of portions of the i-th dish.The stewardess has already given food to m\u2009-\u20091 passengers, gave Polycarp a polite smile and asked him what he would prefer. That's when Polycarp realized that they might have run out of some dishes by that moment. For some of the m\u2009-\u20091 passengers ahead of him, he noticed what dishes they were given. Besides, he's heard some strange mumbling from some of the m\u2009-\u20091 passengers ahead of him, similar to phrase 'I'm disappointed'. That happened when a passenger asked for some dish but the stewardess gave him a polite smile and said that they had run out of that dish. In that case the passenger needed to choose some other dish that was available. If Polycarp heard no more sounds from a passenger, that meant that the passenger chose his dish at the first try.Help Polycarp to find out for each dish: whether they could have run out of the dish by the moment Polyarp was served or that dish was definitely available.", "input_spec": "Each test in this problem consists of one or more input sets. First goes a string that contains a single integer t (1\u2009\u2264\u2009t\u2009\u2264\u2009100\u2009000) \u2014 the number of input data sets in the test. Then the sets follow, each set is preceded by an empty line. The first line of each set of the input contains integers m, k (2\u2009\u2264\u2009m\u2009\u2264\u2009100\u2009000, 1\u2009\u2264\u2009k\u2009\u2264\u2009100\u2009000) \u2014 the number of Polycarp's seat and the number of dishes, respectively. The second line contains a sequence of k integers a1,\u2009a2,\u2009...,\u2009ak (1\u2009\u2264\u2009ai\u2009\u2264\u2009100\u2009000), where ai is the initial number of portions of the i-th dish. Then m\u2009-\u20091 lines follow, each line contains the description of Polycarp's observations about giving food to a passenger sitting in front of him: the j-th line contains a pair of integers tj,\u2009rj (0\u2009\u2264\u2009tj\u2009\u2264\u2009k,\u20090\u2009\u2264\u2009rj\u2009\u2264\u20091), where tj is the number of the dish that was given to the j-th passenger (or 0, if Polycarp didn't notice what dish was given to the passenger), and rj \u2014 a 1 or a 0, depending on whether the j-th passenger was or wasn't disappointed, respectively. We know that sum ai equals at least m, that is,Polycarp will definitely get some dish, even if it is the last thing he wanted. It is guaranteed that the data is consistent. Sum m for all input sets doesn't exceed 100\u2009000. Sum k for all input sets doesn't exceed 100\u2009000.", "output_spec": "For each input set print the answer as a single line. Print a string of k letters \"Y\" or \"N\". Letter \"Y\" in position i should be printed if they could have run out of the i-th dish by the time the stewardess started serving Polycarp.", "sample_inputs": ["2\n\n3 4\n2 3 2 1\n1 0\n0 0\n\n5 5\n1 2 1 3 1\n3 0\n0 0\n2 1\n4 0"], "sample_outputs": ["YNNY\nYYYNY"], "notes": "NoteIn the first input set depending on the choice of the second passenger the situation could develop in different ways: If he chose the first dish, then by the moment the stewardess reaches Polycarp, they will have run out of the first dish; If he chose the fourth dish, then by the moment the stewardess reaches Polycarp, they will have run out of the fourth dish; Otherwise, Polycarp will be able to choose from any of the four dishes. Thus, the answer is \"YNNY\".In the second input set there is, for example, the following possible scenario. First, the first passenger takes the only third dish, then the second passenger takes the second dish. Then, the third passenger asks for the third dish, but it is not available, so he makes disappointed muttering and ends up with the second dish. Then the fourth passenger takes the fourth dish, and Polycarp ends up with the choice between the first, fourth and fifth dish.Likewise, another possible scenario is when by the time the stewardess comes to Polycarp, they will have run out of either the first or the fifth dish (this can happen if one of these dishes is taken by the second passenger). It is easy to see that there is more than enough of the fourth dish, so Polycarp can always count on it. Thus, the answer is \"YYYNY\"."}, "positive_code": [{"source_code": "use strict;\nuse warnings;\nmy $tests = <>;\nmy @ANS;\n\nfor my $curTest (1 .. $tests)\n{\n <>;\n my @ans;\n my $any = 0;\n my $first = 0;\n my ($n,$m ) = split /\\s/, <>;\n \n my @dish = split /\\s/, <>;\n for(0..$#dish)\n {\n $ans[$_] = 'N';\n };\n my %beforeFirst;\n for(1 .. $n - 1)\n {\n my ($d, $w) = split /\\s/, <>;\n if($w == 1 and $first == 0)\n { \n $first++;\n for (0..$#dish)\n {\n if($dish[$_] <= $any)\n {\n $beforeFirst{$_} = 1;\n }\n }\n }\n if($d != 0)\n { \n delete ($beforeFirst{$d-1});\n $dish[$d-1]--;\n }\n else \n {\n $any++;\n }\n }\n my $min = 10**7;\n for (keys %beforeFirst)\n {\n if($min > $dish[$_])\n {\n $min = $dish[$_];\n }\n $ans[$_] = 'Y';\n }\n $any -= $min unless $min == 10**7;\n \n if ($any < 0){$any = 0;}\n for(0.. $#dish)\n {\n if ($dish[$_] <= $any) { $ans[$_] = 'Y'; }\n }\n push @ANS, [@ans];\n}\nfor (@ANS)\n{\nprint @{$_};\nprint \"\\n\";\n}\n"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\n\nmy $tests = <>;\nmy @ANS;\n\nfor my $curTest (1 .. $tests)\n{\n <>;\n my @ans;\n my $any = 0;\n my $first = 0;\n my ($n,$m ) = split /\\s/, <>;\n \n my @dish = split /\\s/, <>;\n for(0..$#dish)\n {\n $ans[$_] = 'N';\n };\n for(1 .. $n - 1)\n {\n my ($d, $w) = split /\\s/, <>;\n if($w == 1 and $first == 0)\n {\n $first++;\n my $min = 10**6;#\u0431\u043b\u044f(\n for (0..$#dish)\n {\n if($dish[$_] <= $any)\n {\n $ans[$_] = 'Y';\n if($dish[$_] < $min)\n {\n $min = $dish[$_];\n }\n }\n }\n $any -= $min;\n }\n if($d != 0)\n {\n $dish[$d-1]--;\n }\n else \n {\n $any++;\n }\n }\n for (0..$#dish)\n {\n if($dish[$_] <= $any)\n {\n $ans[$_] = 'Y';\n }\n }\n push @ANS, [@ans];\n}\nfor (@ANS)\n{\nprint @{$_};\nprint \"\\n\";\n}\n"}], "src_uid": "b27ad24341355f7ef01e98048a92bc4a"} {"nl": {"description": "Little penguin Polo adores strings. But most of all he adores strings of length n.One day he wanted to find a string that meets the following conditions: The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct. No two neighbouring letters of a string coincide; that is, if we represent a string as s\u2009=\u2009s1s2... sn, then the following inequality holds, si\u2009\u2260\u2009si\u2009+\u20091(1\u2009\u2264\u2009i\u2009<\u2009n). Among all strings that meet points 1 and 2, the required string is lexicographically smallest. Help him find such string or state that such string doesn't exist.String x\u2009=\u2009x1x2... xp is lexicographically less than string y\u2009=\u2009y1y2... yq, if either p\u2009<\u2009q and x1\u2009=\u2009y1,\u2009x2\u2009=\u2009y2,\u2009... ,\u2009xp\u2009=\u2009yp, or there is such number r (r\u2009<\u2009p,\u2009r\u2009<\u2009q), that x1\u2009=\u2009y1,\u2009x2\u2009=\u2009y2,\u2009... ,\u2009xr\u2009=\u2009yr and xr\u2009+\u20091\u2009<\u2009yr\u2009+\u20091. The characters of the strings are compared by their ASCII codes.", "input_spec": "A single line contains two positive integers n and k (1\u2009\u2264\u2009n\u2009\u2264\u2009106,\u20091\u2009\u2264\u2009k\u2009\u2264\u200926) \u2014 the string's length and the number of distinct letters.", "output_spec": "In a single line print the required string. If there isn't such string, print \"-1\" (without the quotes).", "sample_inputs": ["7 4", "4 7"], "sample_outputs": ["ababacd", "-1"], "notes": null}, "positive_code": [{"source_code": "use strict;\nuse warnings;\n\nmy $in = <>; chomp $in;\n$in =~ /(\\d+) (\\d+)/;\nmy ($n, $k) = ($1, $2);\n\nmy $str;\n\nif ($k > $n) { print \"-1\\n\"; exit(0); }\n\nif ($n == 1) { print \"a\\n\"; exit(0); }\n\nif ($k == 1) { print \"-1\\n\"; exit(0); }\n\nif ($n == 2) { print \"ab\\n\"; exit(0); }\n\nif ($n % 2 == 0) {\n $str = \"ab\"x($n/2);\n} else {\n $str = \"ab\"x(($n-1)/2);\n $str .= \"a\";\n}\n\n$k -= 2;\n\nmy $pos = length($str)-1;\nfor (my $i = $k; $i > 0; --$i) {\n substr($str, $pos, 1) = chr(ord('a') + $i + 1);\n $pos--;\n}\n\nprint \"$str\\n\";\n\n"}], "negative_code": [], "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 \u2014 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 \u2014 print any of them.", "sample_inputs": ["9 4", "5 1", "10 26"], "sample_outputs": ["aabacadbb", "aaaaa", "codeforces"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($n,$p) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nif( $p == 1 ){\r\n print (\"a\" x $n);\r\n} elsif( $p == 2 ){\r\n while($n>0){\r\n print substr(\"aabb\",0,$n<4 ? $n : 4);\r\n $n -= 4;\r\n }\r\n} else {\r\n my $al = \"abcdefghijklmnopqrstuvwxyz\";\r\n my $b = 'a';\r\n for(my $i=0;$i<$p-1;$i++){\r\n $b .= substr($al,$i,1);\r\n for(my $j=0;$j<2*$p-3-2*$i;$j++){\r\n #print \"j-$j\\n\";\r\n if( $j % 2 == 0 ){\r\n $b .= substr($al,1+$i+$j/2,1);\r\n } else {\r\n $b .= substr($al,$i,1);\r\n }\r\n }\r\n $b .= substr($al,$i+1,1);\r\n #print \"i=$i, b=$b\\n\";\r\n }\r\n while($n>0){\r\n print substr($b,0,$n);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my $mxv = - $mod;\r\n my $s = 0;\r\n foreach my $i (0..($n-1)){\r\n $s += $A[$i];\r\n $mxv = $A[$i] if $A[$i] > $mxv;\r\n }\r\n $s -= $mxv;\r\n my $res = $mxv + ( $s / ($n-1) );\r\n print \"$res\\n\";\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the number of spots and hour $$$k$$$.", "output_spec": "For each test case, print one integer\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\tmy( $n, $k ) = split;\n\t\n\t$debug and print \"[$n $k]\";\n\t\n\tnext if $debug and /^69/;\n\t\n\tmy $A = 1;\n\tmy $B = $n;\n\t\n\t$_ = $k;\n\t\n\tmy $ans = ( int( ( $_ - 1 ) / ( $n >> 1 ) ) * ( ( $n >> 1 ) + $n % 2 )\n\t\t\t\t+ ( $_ % ( $n >> 1 ) || ( $n >> 1 ) ) ) % ( $n ) || ( $n );\n\t\n\t$debug and do{\n\t\tfor( 1 .. $k ){\n\t\t\t$A --;\n\t\t\t$A < 1 and $A = $n;\n\t\t\t$B ++;\n\t\t\t$B > $n and $B = 1;\n\t\t\t$B == $A and $B ++;\n\t\t\t$B > $n and $B = 1;\n\t\t\t\n\t\t\tmy $exp = ( int( ( $_ - 1 ) / ( $n >> 1 ) ) * ( ( $n >> 1 ) + $n % 2 )\n\t\t\t\t+ ( $_ % ( $n >> 1 ) || ( $n >> 1 ) ) ) % ( $n ) || ( $n );\n\t\t\t\n\t\t\t$debug and print \" $_:[<$B> $A],[$exp],diff: \" . ( $B - $exp );\n\t\t\t}\n\t\t};\n\t\n\tprint $ans;\n\t}"}], "negative_code": [], "src_uid": "2e837d3afc48177516578a950e957586"} {"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$$$) \u2014 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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nmy $chr = 'abcdefghijklmnopqrstuvwxyz';\n\nmy %cnt = ();\nmy $k;\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my $s = scalar();\n \n $k = 0;\n for($k=0;$k<26;$k++){\n last if $n == 2**$k;\n }\n \n if( $k == 0 ){\n if( substr($s,0,1) eq 'a'){\n print \"0\\n\";\n } else {\n print \"1\\n\";\n }\n next;\n }\n \n %cnt = ();\n for(my $i=0;$i<$n;$i++){\n my $bin = sprintf(\"%0${k}b\",$i);\n my $c = substr($s,$i,1);\n for(my $j=1;$j<=$k;$j++){\n $cnt{substr($bin,0,$j)}->{$c} ++;\n }\n }\n \n my $res = &f(0,'');\n print \"$res\\n\";\n \n}\n\nexit(0);\n\n\nsub f {\n my $ci = shift;\n my $ch = substr($chr,$ci,1);\n \n my $keystr = shift;\n \n if( length($keystr) == $k ){\n return (1 - $cnt{$keystr}->{$ch});\n }\n \n my $n2 = 2 ** ($k - length($keystr)-1);\n \n return \n min( \n ($n2 - $cnt{$keystr . '0'}->{$ch}) + &f($ci+1,$keystr . '1') , \n ($n2 - $cnt{$keystr . '1'}->{$ch}) + &f($ci+1,$keystr . '0') )\n ;\n}\n\nsub max {\n my $x = shift;\n my $y = shift;\n return ( $x > $y ? $x : $y );\n}\nsub min {\n my $x = shift;\n my $y = shift;\n return ( $x > $y ? $y : $x );\n}\n\n"}], "negative_code": [], "src_uid": "324b7957b46dfe4948074c781159b7e7"} {"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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \\le n \\le 5 \\cdot 10^4$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "use strict;\nuse v5.020;\n\nmy $try = <>;\nwhile($try-- > 0){\n my $n = <>;\n my @arr = split(' ', <>);\n my $found = 0;\n while(@arr >= 3){\n my $last = pop @arr;\n\n if($arr[1] + $arr[0] <= $last){\n $found = 1;\n last;\n }\n }\n if($found > 0){\n printf(\"1 2 %d\\n\", @arr+1);\n } else {\n say \"-1\";\n }\n}"}], "negative_code": [{"source_code": "use strict;\nuse v5.020;\n\nmy $try = <>;\nwhile($try-- > 0){\n my $n = <>;\n my @arr = split(' ', <>);\n my $found = 0;\n while(@arr >= 3){\n my $last = pop @arr;\n\n if($arr[1] + $arr[0] < $last){\n $found = 1;\n last;\n }\n }\n if($found > 0){\n printf(\"1 2 %d\\n\", @arr+1);\n } else {\n say \"-1\";\n }\n}"}], "src_uid": "341555349b0c1387334a0541730159ac"} {"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,\u2009r,\u2009c1,\u2009c2, which means that all symbols c1 in range [l,\u2009r] (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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100). 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,\u2009r,\u2009c1,\u2009c2 (1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n, c1,\u2009c2 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": "<>;\n$A = <>;\n\nfor( <> ){\n\t( $l, $r, $c, $d ) = split;\n\t( substr $A, $l - 1, $r - $l + 1 ) =~ s/$c/$d/g;\n\t}\n\nprint $A"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t$_ = <>, chomp;\n\tmy @A = map [ split ' ', <> ], 1 .. $m;\n\t\n\tfor my $i ( @A ){\n\t\t( substr $_, $i->[ 0 ] - 1, $i->[ 1 ] - $i->[ 0 ] + 1 ) =~ s/$i->[2]/$i->[3]/g;\n\t\t}\n\t\n\tprint;\n\t}"}], "negative_code": [], "src_uid": "3f97dc063286a7af4838b7cd1c01df69"} {"nl": {"description": "There are $$$n$$$ pieces of cake on a line. The $$$i$$$-th piece of cake has weight $$$a_i$$$ ($$$1 \\leq i \\leq n$$$).The tastiness of the cake is the maximum total weight of two adjacent pieces of cake (i.\u00a0e., $$$\\max(a_1+a_2,\\, a_2+a_3,\\, \\ldots,\\, a_{n-1} + a_{n})$$$).You want to maximize the tastiness of the cake. You are allowed to do the following operation at most once (doing more operations would ruin the cake): Choose a contiguous subsegment $$$a[l, r]$$$ of pieces of cake ($$$1 \\leq l \\leq r \\leq n$$$), and reverse it. The subsegment $$$a[l, r]$$$ of the array $$$a$$$ is the sequence $$$a_l, a_{l+1}, \\dots, a_r$$$.If you reverse it, the array will become $$$a_1, a_2, \\dots, a_{l-2}, a_{l-1}, \\underline{a_r}, \\underline{a_{r-1}}, \\underline{\\dots}, \\underline{a_{l+1}}, \\underline{a_l}, a_{r+1}, a_{r+2}, \\dots, a_{n-1}, a_n$$$.For example, if the weights are initially $$$[5, 2, 1, 4, 7, 3]$$$, you can reverse the subsegment $$$a[2, 5]$$$, getting $$$[5, \\underline{7}, \\underline{4}, \\underline{1}, \\underline{2}, 3]$$$. The tastiness of the cake is now $$$5 + 7 = 12$$$ (while before the operation the tastiness was $$$4+7=11$$$).Find the maximum tastiness of the cake after doing the operation at most once.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 50$$$) \u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 1000$$$) \u2014 the number of pieces of cake. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$) \u2014 $$$a_i$$$ is the weight of the $$$i$$$-th piece of cake.", "output_spec": "For each test case, print a single integer: the maximum tastiness of the cake after doing the operation at most once.", "sample_inputs": ["5\n6\n5 2 1 4 7 3\n3\n32 78 78\n3\n69 54 91\n8\n999021 999021 999021 999021 999652 999021 999021 999021\n2\n1000000000 1000000000"], "sample_outputs": ["12\n156\n160\n1998673\n2000000000"], "notes": "NoteIn the first test case, after reversing the subsegment $$$a[2, 5]$$$, you get a cake with weights $$$[5, \\underline{7}, \\underline{4}, \\underline{1}, \\underline{2}, 3]$$$. The tastiness of the cake is now $$$\\max(5+7, 7+4, 4+1, 1+2, 2+3) = 12$$$. This is the maximum possible tastiness of the cake one can obtain by performing the operation at most once.In the second test case, it's optimal not to do any operation. The tastiness is $$$78+78 = 156$$$.In the third test case, after reversing the subsegment $$$a[1, 2]$$$, you get a cake with weights $$$[\\underline{54}, \\underline{69}, 91]$$$. The tastiness of the cake is now $$$\\max(54+69, 69+91) = 160$$$. There is no way to make the tastiness of the cake greater than $$$160$$$ by performing at most one operation."}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n\n$tests = ;\n\nfor ($_=0;$_<$tests;$_++)\n{\n $n= ;\n @list = split (\" \",);\n @list = sort {$a <=> $b} @list;\n print $list[-1]+$list[-2];\n print \"\\n\"\n}"}], "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": "$_=<>;\ns/(\\d{3})-/$1$1-/g;\n#print;\n@_=/\\d\\d-\\d\\d-\\d\\d\\d\\d/g;\n#print\" @_\\n\";\nfor (@_){\nif (/201[345]/) {\n/^(\\d\\d)-(\\d\\d)/;\nif($1>0 and $1<=31 and ($2==1 or $2==3 or $2==5 or $2==7 or $2==8 or $2==10 or $2==12)){\npush @a,$_;\n}\nif($1>0 and $1<=30 and ($2==4 or $2==6 or $2==9 or $2==11)){\npush @a,$_;\n}\nif($1>0 and $1<=28 and $2==2){\npush @a,$_;\n}\n}\n#print\" @a\\n\";\n}\nfor(@a){\ns/-//;\ns/-201//;\n#print;\n$m[$_]++;\n}\n# print \"@m\\n\";\n$max=0;\nfor(@m){\n$max<$_ and $max=$_;\n}\n#print \" $max\\n\";\nfor$i(0..@m-1){\n$max==$m[$i] and $_=$i;\n}\n#$_=$a[$max];\n#print;\nlength==4 and s/^/0/;\ns/\\d\\d/$&-/;\ns/-\\d\\d/$&-201/;\nprint"}, {"source_code": "map { $h{$_}++ } <>=~/(?=((?:\\d\\d-){2}201[3-5]))/g;\nfor (keys %h){\n\t($d,$m,$y)=split/-/;\n\t($d > 0 and ($d <= 31 and $m =~ /^(0[13578]|1[02])$/\n\t\t\t\tor\n\t\t\t\t$d <= 30 and $m =~ /^(0[469]|11)$/\n\t\t\t\tor\n\t\t\t\t$d <= 28 and $m =~ /^02$/\n\t\t\t\t)\n\t)\n\tor delete $h{$_}\n\t}\n\n$_ > $max and $max = $_ for values %h;\n$h{$_} == $max and print for keys %h;"}], "negative_code": [{"source_code": "$_=<>;\ns/(\\d{3})-/$1$1-/g;\n#print;\n@_=/\\d\\d-\\d\\d-\\d\\d\\d\\d/g;\n#print\" @_\\n\";\nfor (@_){\nif (/201[345]/) {\n/^(\\d\\d)-(\\d\\d)/;\nif($1<=31 and ($2==1 or $2==3 or $2==5 or $2==7 or $2==8 or $2==10 or $2==12)){\npush @a,$_;\n}\nif($1<=30 and ($2==4 or $2==6 or $2==9 or $2==11)){\npush @a,$_;\n}\nif($1<=28 and $2==2){\npush @a,$_;\n}\n}\n#print\" @a\\n\";\n}\nfor(@a){\ns/-//;\ns/-201//;\n#print;\n$m[$_]++;\n}\n# print \"@m\\n\";\n$max=0;\nfor(@m){\n$max<$_ and $max=$_;\n}\n#print \" $max\\n\";\nfor$i(0..@m-1){\n$max==$m[$i] and $_=$i;\n}\n#$_=$a[$max];\n#print;\nlength==4 and s/^/0/;\ns/\\d\\d/$&-/;\ns/-\\d\\d/$&-201/;\nprint"}, {"source_code": "$_=<>;\ns/(\\d{3})-/$1$1-/g;\n@_=/\\d\\d-\\d\\d-\\d\\d\\d\\d/g;\nfor (@_){\nif (/201[345]/) {\n/^(\\d\\d)-(\\d\\d)/;\nif($1<=31 and $2==1 or $2==3 or $2==5 or $2==7 or $2==8 or $2==10 or $2==12){\npush @a,$_;\n}\nif($1<=30 and $2==4 or $2==6 or $2==9 or $2==11){\npush @a,$_;\n}\nif($1<=28 and $2==2){\npush @a,$_;\n}\n}\n}\nfor(@a){\ns/-//;\ns/-201//;\n$m[$_]++;\n}\n$max=0;\nfor(@m){\n$max<$_ and $max=$_;\n}\n$_=$a[$max];\ns/\\d\\d/$&-/;\ns/-\\d\\d/$&-201/;\nprint"}, {"source_code": "$_=<>;\ns/(\\d{3})-/$1$1-/g;\n@_=/\\d\\d-\\d\\d-\\d\\d\\d\\d/g;\nfor (@_){\nif (/201[345]/) {\n/^(\\d\\d)-(\\d\\d)/;\nif($1<=31 and $2==1 or $2==3 or $2==5 or $2==7 or $2==8 or $2==10 or $2==12){\npush @a,$_;\n}\nif($1<=30 and $2==4 or $2==6 or $2==9 or $2==11){\npush @a,$_;\n}\nif($1<=28 and $2==2){\npush @a,$_;\n}\n}\n}\nfor(@a){\ns/-//;\ns/-201//;\n$m[$_]++;\n}\n$max=0;\nfor(@m){\n$max<$_ and $max=$_;\n}\n$_=$m[$max];\ns/\\d\\d/$&-/;\ns/-\\d\\d/$&-201/;\nprint"}, {"source_code": "map { $h{$_}++ } <>=~/(?=((?:\\d\\d-){2}201[3-5]))/g;\nfor (keys %h){\n\t($d,$m,$y)=split/-/;\n\t($d > 0 and ($d <= 31 and $m =~ /^([13578]|1[02])$/\n\t\t\t\tor\n\t\t\t\t$d <= 30 and $m =~ /^([469]|11)$/\n\t\t\t\tor\n\t\t\t\t$d <= 28 and $m =~ /^2$/\n\t\t\t\t)\n\t)\n\tor delete $h{$_}\n\t}\n\n$_ > $max and $max = $_ for values %h;\n$h{$_} == $max and print for keys %h;"}, {"source_code": "$_=<>;\ns/(\\d{3})-/$1$1-/g;\n#print;\n@_=/\\d\\d-\\d\\d-\\d\\d\\d\\d/g;\n#print\" @_\\n\";\nfor (@_){\nif (/201[345]/) {\n/^(\\d\\d)-(\\d\\d)/;\nif($1<=31 and $2==1 or $2==3 or $2==5 or $2==7 or $2==8 or $2==10 or $2==12){\npush @a,$_;\n}\nif($1<=30 and $2==4 or $2==6 or $2==9 or $2==11){\npush @a,$_;\n}\nif($1<=28 and $2==2){\npush @a,$_;\n}\n}\n#print\" @a\\n\";\n}\nfor(@a){\ns/-//;\ns/-201//;\n#print;\n$m[$_]++;\n}\n# print \"@m\\n\";\n$max=0;\nfor(@m){\n$max<$_ and $max=$_;\n}\n#print \" $max\\n\";\nfor$i(0..@m-1){\n$max==$m[$i] and $_=$i;\n}\n#$_=$a[$max];\n#print;\nlength==4 and s/^/0/;\ns/\\d\\d/$&-/;\ns/-\\d\\d/$&-201/;\nprint"}, {"source_code": "$_=<>;\ns/(\\d{3})-/$1$1-/g;\n#print;\n@_=/\\d\\d-\\d\\d-\\d\\d\\d\\d/g;\n#print\" @_\\n\";\nfor (@_){\nif (/201[345]/) {\n/^(\\d\\d)-(\\d\\d)/;\nif($1<=31 and $2==1 or $2==3 or $2==5 or $2==7 or $2==8 or $2==10 or $2==12){\npush @a,$_;\n}\nif($1<=30 and $2==4 or $2==6 or $2==9 or $2==11){\npush @a,$_;\n}\nif($1<=28 and $2==2){\npush @a,$_;\n}\n}\n#print\" @a\\n\";\n}\nfor(@a){\ns/-//;\ns/-201//;\n#print;\n$m[$_]++;\n}\n# print \"@m\\n\";\n$max=0;\nfor(@m){\n$max<$_ and $max=$_;\n}\n#print \" $max\\n\";\nfor$i(0..@m-1){\n$max==$m[$i] and $_=$i;\n}\n#$_=$a[$max];\n#print;\ns/\\d\\d/$&-/;\ns/-\\d\\d/$&-201/;\nprint"}, {"source_code": "map { $h{$_}++ } <>=~/(?=((?:\\d\\d-){2}\\d{4}))/g;\n$_ > $max and $max = $_ for values %h;\n$h{$_} == $max and print for keys %h"}], "src_uid": "dd7fd84f7915ad57b0e21f416e2a3ea0"} {"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 \u2014 number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009105), 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": "\nuse strict;\nuse warnings;\nuse integer;\n\nmy @arr = ('A' .. 'Z');\nsub ntos {\n my $n = shift;\n my $s = '';\n while ($n > 0) {\n $s = @arr[$n % 26 - 1] . $s;\n $n-- unless ($n % 26);\n $n /= 26;\n }\n return $s;\n}\n\nsub ston {\n my $n = 0;\n $n = $n * 26 + ord($_) - 64 for (split //, shift);\n return $n;\n}\n\nmy $n = int <>;\nfor (1 .. $n) {\n my $s = <>;\n if ($s =~ /^R([\\d]+)C([\\d]+)$/) {\n print ntos($2) . \"$1\\n\";\n } else {\n $s =~ /^([\\D]*)([\\d]*)$/;\n print \"R$2C\" . (ston $1) . \"\\n\";\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\n\n$n = <>;\nwhile ($n--) {\n $_ = <>;\n chomp;\n if ($_=~/^R(\\d+)C(\\d+)$/i) {\n $row = $1*1;\n $col = $2*1;\n $acol = \"\";\n while ($col>0) {\n $col--;\n $acol = chr(65+($col%26)) . $acol;\n $col = int($col/26);\n }\n print \"$acol$row$/\";\n }\n elsif ($_=~/^([A-Z]+)(\\d+)$/i) {\n $acol = $1;\n $row = $2*1;\n $col = 0;\n $i = 0;\n while ($i;\nwhile( <> ) {\n if ( /^R([0-9]+)C([0-9]+)/ ) { \n print convert_to_26($2) . $1 . \"\\n\";\n }\n elsif ( /^([A-Z]+)([0-9]+)/ ) {\n print \"R$2C\" . convert_to_10( $1 ) . \"\\n\"; \n }\n\n $n--;\n last if ( !$n );\n}\n\nsub convert_to_26 {\n my $n = shift;\n return chr( ord('A') + $n - 1 ) if ( $n <= 26 );\n\n my $rem = $n % 26;\n my $div = int ($n / 26 );\n if ( !$rem ) {\n $rem = 26;\n $div--;\n }\n return convert_to_26($div) . chr ( ord('A') + $rem - 1 );\n}\n\nsub convert_to_10 {\n my @n = split '', shift;\n my $ret = 0;\n for ( my $i=0; $i <= $#n; $i++ ) {\n $ret += ( ord( $n[$i] ) - ord('A') + 1 ) * ( 26 ** ( $#n - $i ) );\n }\n\n return $ret;\n}\n"}, {"source_code": "use strict;\nuse warnings;\n\nuse 5.012;\n\nmy $num_inputs = <>;\nmy @inputs;\n\nfor ( 1 .. $num_inputs ) {\n my $input = <>;\n chomp $input;\n if ( $input =~ /\\AR(\\d+)C(\\d+)\\z/ ) {\n printf \"%s%s\\n\", row_num_to_letter($2), $1;\n }\n else {\n $input =~ /\\A([A-Z]+)(\\d+)\\z/;\n printf \"R%sC%s\\n\", $2, row_letter_to_num( $1 );\n }\n}\n\nsub row_num_to_letter {\n my ( $num ) = @_;\n my $str = '';\n while ( $num > 0 ) {\n if ( my $left = $num % 26 ) {\n $str = chr( $left + 64 ) . $str;\n $num = int( $num / 26 );\n } else {\n $str = 'Z' . $str;\n $num = int( $num / 26 ) - 1;\n }\n }\n return $str;\n}\n\nsub row_letter_to_num {\n my ( @letters ) = split //, $_[0];\n my $num = 0;\n my $scale = 0; \n while ( @letters ) {\n my $letter = pop(@letters);\n my $nvalue = ord($letter) - 64;\n $num = $num + ( $nvalue * ( 26 ** $scale ) );\n $scale ++;\n }\n return $num;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = ;\nchomp $n;\n\nwhile ($n--) {\n my $line = ;\n chomp $line;\n print trans($line), \"\\n\";\n}\n\nsub trans\n{\n my $x = shift @_;\n my $res = \"\";\n\n if ($x =~ m/R(\\d+)C(\\d+)/) {\n my $r = $1;\n my $c = $2;\n\n# print \"$r $c\\n\";\n while ($c > 0) {\n $res = chr(ord('A') + ($c + 25)%26).$res;\n if ($c % 26) {\n $c = int($c/26);\n } else {\n $c = $c/26 - 1;\n }\n }\n $res .= $r;\n } else {\n $x =~ m/([A-Z]+)(\\d+)/;\n my $r = $2;\n\n# print \"$1 $2\\n\";\n my $c = 0;\n my $pow = 0;\n my $len = length($1);\n foreach (split \"\", $1) {\n# print \"$_\\n\";\n $pow += 1;\n $c += (ord($_) - ord('A') + 1)*(26**($len-$pow));\n# print \"$len $pow $_ $c\\n\";\n }\n\n $res = \"R\".$r.\"C\".$c;\n }\n\n return $res;\n}\n"}, {"source_code": "\nsub RC_to_CR {\n\tmy $rc = shift;\n\t$rc =~ /^R(\\d+)C(\\d+)/;\n\tmy $row = $1;\n\tmy $col = $2;\n\tmy $ret = '';\n#\tmy $p = 1;\n#\tprint \"col = $col\\n\";\n\twhile($col > 26) {\n\t\tmy $tmp = int($col / 26);\n\t\tmy $v = $col - $tmp * 26;\n#\t\tprint \"col = $col, p = $p, tmp = $tmp, v = $v\\n\";\n\t\tif($v==0) { $v = 26; $col -= 26; }\n\t\t$ret .= chr(64+$v);\n\t\t$col = int($col/26);\n#\t\t$p++;\n\t}\n\t$ret .= chr(64+$col);\n\t$ret = scalar reverse $ret;\n\t$ret .= $row;\n\treturn $ret;\n}\n\nsub CR_to_RC {\n\tmy $cr = shift;\n\t$cr =~ /([A-Z]+)(\\d+)/;\n\tmy $col = $1;\n\tmy $row = $2;\n\tmy $ret = 'R'.$row.\"C\";\n\tmy $l = length($col)-1;\n\tmy $c = 0;\n\tmy $p = $l;\n\tforeach my $ltr (split //, $col) {\n\t\tmy $v = (ord($ltr)-64);\n\t\t$c += ($v * 26**$p);\n\t\t$p--;\n\t}\n\t$ret .= $c;\n\treturn $ret;\n}\n\nmy $num = ;\nchomp($num);\nwhile($num-- > 0) {\n\tchomp($row = );\n\tif($row =~ /^R\\d+C\\d+/) {\n\t\tprint RC_to_CR($row).\"\\n\";\n\t} else {\n\t\tprint CR_to_RC($row).\"\\n\";\n\t}\n}\n"}, {"source_code": "use strict;\nuse warnings;\nuse utf8;\n\nmy $n=;\nchomp($n);\n\nour @dic=(\"Z\",\"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\");\nour %alphabet = do { my $i; map { $_ => ++$i } qw(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\nfor(my $i=0; $i<$n; $i++){\n\tmy $stdin=;\n\tchomp($stdin);\n\tprint solveB($stdin).\"\\n\";\n}\n\nsub solveB{\n\tmy $s=$_[0];\n\tif($s=~/^R(\\d+)C(\\d+)$/){\n\t\treturn solve1($2,\"\").$1;\n\t}\n\tif($s=~/^([A-Z]+)(\\d+)$/){\n\t\tmy $ans=0;\n\t\tfor(my $i=0; $i 0) {\n\t$s = @arr[$n % 26 - 1] . $s;\n\tif (not $n % 26) { $n--; }\n\t$n /= 26;\n }\n return $s;\n}\n\nsub ston {\n my $s = shift;\n my %hash;\n @hash{('A'..'Z')} = (1 .. 26);\n my $n = 0;\n for (split //, $s) {\n\t$n *= 26;\n\t$n += $hash{$_};\n }\n return $n;\n}\n\nsub main {\n my $n = int <>;\n for (1 .. $n) {\n\tmy $s = <>;\n\tif ($s =~ /^R([\\d]+)C([\\d]+)$/) {\n\t print ntos($2) . \"$1\\n\";\n\t} elsif ($s =~ /^([\\D]*)([\\d]*)$/) {\n\t print \"R$2C\" . (ston $1) . \"\\n\";\n\t} else {\n\t exit(-1);\n\t}\n }\n}\n\nmain();\n"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nno warnings 'recursion';\nuse strict;\n\nsub ltn{\n my @k=split//,shift;my $ln=shift;\n if(@k==1){return $$ln{$k[0]};}\n else{\n\tmy $h=pop @k;my $f=join \"\",@k;\n\treturn ($$ln{$h}+26*ltn($f,$ln));\n }\n}\nsub ntl{\n my $t=shift;my $ln=shift;\n if($t>26){return ((($t%26==0)?ntl(int($t/26-1),$ln):ntl(int($t/26),$ln)).$$ln{$t%26});}\n else{return $$ln{$t%26};}\n}\n\nmy $row_n;my %ln;$ln{0}=\"Z\";$ln{\"Z\"}=26;my $l=\"A\";\nfor my $j(1..25){\n $ln{$j}=$l;\n $ln{$l}=$j;\n ++$l;\n}my $g=\\%ln;\nmy $n=;\nmy @final_list;\nforeach my $i (1..$n){\n my $sr=;\n chomp($sr);\n if($sr=~/^R([0-9]+)C([0-9]+)$/){\n\tmy $p=($2>26)?ntl($2,$g):$ln{$2%26};\n\tpush @final_list,\"$p\".\"$1\\n\";\n }\n elsif($sr=~/([A-Z]+)([0-9]+)/){\n\tmy $col=ltn($1,$g);\n\t$row_n=$2;\n\tpush @final_list,\"R$row_n\".\"C$col\\n\";\n }\n}\nprint @final_list;\n"}, {"source_code": "#!/usr/bin/perl\n\n\n%num2char = map { ($_ + 1) => chr(ord('A') + $_) } (0..25) ;\n%char2num = reverse %num2char;\n\n\n#map { print \"$_ $num2char{$_}\\n\" } sort { $a <=> $b } keys %num2char;\n#map { print \"$_ $char2num{$_}\\n\" } sort keys %char2num;\n\n$n = <>;\n\nfor (1..$n) {\n $input = <>;\n if ($input =~ /^R(\\d+)C(\\d+)$/) {\n $row = $1;\n $col = $2;\n $new_char = undef;\n while ($col > 0) {\n $new_char = ($col % 26 == 0 ? $num2char{26} : $num2char{$col % 26}) . $new_char;\n if ($col % 26 == 0) { $col--; }\n $col = int($col / 26);\n }\n $col = $new_char;\n print \"$col$row\\n\";\n }\n elsif ($input =~ /^([A-Z]+)(\\d+)$/) {\n $row = $2;\n $col = $1;\n $count = 0;\n $sum = 0;\n for $c (reverse split //,$col) {\n $sum += (26 ** $count++) * $char2num{$c}; \n }\n $col = $sum;\n print \"R$row\" . \"C$col\\n\";\n }\n}\n"}, {"source_code": "\nuse strict;\nuse warnings;\n\nmy $totalNum = ;\n\nmy @inputs;\nfor(my $i = 0; $i < $totalNum; $i++){\n my $input = ;\n push @inputs, $input; \n}\n\nforeach my $input (@inputs){\n# print $input . \"\\n\";\n if ( $input =~ /^R([0-9]+)C([0-9]+)$/){\n print convert26($2) . \"$1\" . \"\\n\";\n }\n if ($input =~ /^([A-Z]+)(\\d+)$/ ){\n print \"R$2\" . \"C\" . iconvert26($1) . \"\\n\";\n }\n}\n\n\n#\n#foreach my $i (1..250){\n# print convert26($i), \" \";\n#}\n#\n#print \"\\n\";\n#\n\n#foreach my $i ('AA'..'AZ'){\n# print iconvert26($i), \" \";\n#}\n\n\n\n#===================================================================\nsub convert26 \n{\n\tmy ($input) = @_;\n\tmy $base = 26;\n\tmy @elements = ( 'A'..'Z');\n\tmy @ret=();\n\tdo{\t \n\t unshift @ret, $elements[($input)%$base -1];\n\t $input = int(($input-1) /$base);\n\t}while($input);\n\treturn join('', @ret);\n}\n\nsub iconvert26\n{\n my ($input) = @_;\n my $base = 26;\n my @elements = ('', 'A'..'Z');\n my @inputs = reverse (split(\"\", $input));\n my $ret = 0;\n my $pos = 0;\n foreach my $in (@inputs){\n my $index = 0;\n foreach my $elem (@elements){\n \n if($elem eq $in){\n last;\n }\n $index ++;\n }\n $ret += $index * $base ** $pos; \n $index = 0;\n $pos++;\n }\n return $ret;\n}"}, {"source_code": "#!/usr/local/bin/perl\n$n = <>;\nfor ( 1 .. $n ) {\n $line = <>;\n\n if ( $line =~ /(\\D+)(\\d+)(\\D+)(\\d+)/ ) {\n print makeNormal($2,$4).\"\\n\";\n\n }\n elsif($line =~ /(\\D+)(\\d+)/ ){ \n print makeRC($1 , $2).\"\\n\";\n }\n\n}\n sub makeNormal {\n use integer;\n my $row = $_[0];\n my $col = $_[1];\n my @alph = (A..Z);\n \n $result = $row;\n do{\n my $r, $letter;\n $col -= 1;\n $r = $col % 26;\n $col = $col / 26; \n $result = @alph[$r].$result;\n } while ($col);\n return $result;\n }\n\n sub makeRC {\n use integer;\n my $row = $_[1];\n my $col = $_[0];\n my $resCol = 0;\n my $n = 1;\n my $A = ord(\"A\");\n foreach $ch (reverse split (// , $col)){\n $resCol+= (ord($ch) - $A +1) * $n;\n $n *= 26;\n }\n return \"R\".$row.\"C\".$resCol;\n \n \n }\n"}, {"source_code": "my $num = ;\nfor (1..$num) {\n my $input = ;\n chomp $input;\n if ($input =~ /R(\\d+)C(\\d+)/) {\n my ($row, $col) = ($1, $2);\n my @col;\n while ($col > 26) {\n my $r = $col % 26;\n $r = 26 if !$r;\n push @col, $r;\n $col = int (($col-$r)/26);\n }\n push @col, $col;\n @col = reverse @col;\n my $str;\n $str .= ('A'..'Z')[$_-1] foreach @col;\n $str .= $row;\n print $str, \"\\n\";\n } else {\n $input =~ /([A-Z]+)(\\d+)/;\n my ($col_str, $row) = ($1, $2);\n my $col = 0;\n foreach (map { ord($_) - 64 } split //, $col_str) {\n $col *= 26;\n $col += $_;\n }\n print \"R\", $row, \"C\", $col, \"\\n\";\n }\n}"}, {"source_code": "\n#!/usr/bin/perl\n\nmain();\n\nsub main {\n my $n = int <>;\n for(1..$n) {\n my $line = <>;\n if ($line =~ /(\\D+)(\\d+)(\\D+)(\\d+)/) {\n print convertToSymbols($4).\"$2\\n\";\n next;\n }\n if ($line =~ /(\\D+)(\\d+)/) {\n print \"R\".$2.\"C\".convertFromSymbols($1).\"\\n\";\n }\n }\n}\n\nsub convertToSymbols {\n my $cur = shift;\n my $result = \"\";\n while ($cur > 0) {\n my $tmp = $cur % 26;\n $tmp = 26 if ($tmp==0);\n my $c = chr(ord(\"A\") + $tmp - 1);\n $result = $result.$c;\n $cur = ($cur - $tmp) / 26;\n }\n return reverse $result;\n}\n\nsub convertFromSymbols {\n my $s = shift;\n my @arr = split //, $s;\n my $result = 0;\n foreach(@arr) {\n $result = $result * 26 + ord($_) - ord(\"A\") + 1;\n }\n return $result;\n}"}], "negative_code": [{"source_code": "\nuse strict;\nuse warnings;\nuse bigint;\nmy $n = int(<>);\nmy $inp;\nfor (1..$n) {\n $inp = <>;\n if ($inp =~ /([A-Z]{1})(\\d+)([A-Z]{1})(\\d+)/) {\n print chr(64 + $4 / 26) if ($4 / 26);\n print chr(64 + $4 % 26) if ($4);\n print $2, \"\\n\";\n }\n else {\n $inp =~ /([A-Z]+)([0-9]+)/;\n print \"R$2C\";\n my @arr = split \"\", $1;\n if (length $1 == 1) {\n @arr = (chr 64, $1);\n }\n # print ord $arr[0], \"\\n\";\n print ((ord($arr[0]) - 64) * 26 + ord($arr[1]) - 64);\n }\n}\n"}, {"source_code": "my $n = <>;\nwhile( <> ) {\n if ( /^R([0-9]+)C([0-9]+)/ ) { \n print convert_to_26($2) . $1 . \"\\n\";\n }\n elsif ( /^([A-Z]+)([0-9]+)/ ) {\n print \"R$2C\" . convert_to_10( $1 ) . \"\\n\"; \n }\n\n $n--;\n last if ( !$n );\n}\n\nsub convert_to_26 {\n my $n = shift;\n return chr( ord('A') + $n - 1 ) if ( $n <= 26 );\n\n return convert_to_26( $n/26 ) . chr ( ord('A') + ($n % 26) - 1 );\n}\n\nsub convert_to_10 {\n my @n = split '', shift;\n my $ret = 0;\n for ( my $i=0; $i <= $#n; $i++ ) {\n $ret += ( ord( $n[$i] ) - ord('A') + 1 ) * ( 26 ** ( $#n - $i ) );\n }\n\n return $ret;\n}\n"}, {"source_code": "my $n = <>;\nwhile( <> ) {\n if ( /^R([0-9]+)C([0-9]+)/ ) { \n print convert_to_26($2) . $1 . \"\\n\";\n }\n elsif ( /^([A-Z]+)([0-9]+)/ ) {\n print \"R$2C\" . convert_to_10( $1 ) . \"\\n\"; \n }\n\n $n--;\n last if ( !$n );\n}\n\nsub convert_to_26 {\n my $n = shift;\n return chr( ord('A') + $n - 1 ) if ( $n <= 26 );\n\n my $rem = $n % 26;\n my $div = $n / 26;\n if ( !$rem ) {\n $rem = 26;\n $div--;\n }\n return convert_to_26($div) . chr ( ord('A') + $rem - 1 );\n}\n\nsub convert_to_10 {\n my @n = split '', shift;\n my $ret = 0;\n for ( my $i=0; $i <= $#n; $i++ ) {\n $ret += ( ord( $n[$i] ) - ord('A') + 1 ) * ( 26 ** ( $#n - $i ) );\n }\n\n return $ret;\n}\n"}, {"source_code": "use strict;\nuse warnings;\n\nuse 5.012;\n\nmy $num_inputs = <>;\nmy @inputs;\n\nfor ( 1 .. $num_inputs ) {\n my $input = <>;\n chomp $input;\n if ( $input =~ /\\AR(\\d+)C(\\d+)\\z/ ) {\n printf \"%s%s\\n\", row_num_to_letter($2), $1;\n }\n else {\n $input =~ /\\A([A-Z]+)(\\d+)\\z/;\n printf \"R%sC%s\\n\", $2, row_letter_to_num( $1 );\n }\n}\n\nsub row_num_to_letter {\n my ( $num ) = @_;\n my $str = '';\n while ( $num > 26 ) {\n my $left = $num % 26; \n $str = chr( $left + 64 ) . $str;\n $num = int( $num / 26 );\n }\n return chr( $num + 64 ) . $str;\n}\nsub row_letter_to_num {\n my ( @letters ) = split //, $_[0];\n my $num = 0;\n my $scale = 0; \n while ( @letters ) {\n my $letter = pop(@letters);\n my $nvalue = ord($letter) - 64;\n $num = $num + ( $nvalue * ( 26 ** $scale ) );\n $scale ++;\n }\n return $num;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = ;\nchomp $n;\n\nwhile ($n--) {\n my $line = ;\n chomp $line;\n print trans($line), \"\\n\";\n}\n\nsub trans\n{\n my $x = shift @_;\n my $res = \"\";\n\n if ($x =~ m/R(\\d+)C(\\d+)/) {\n my $r = $1;\n my $c = $2;\n\n# print \"$r $c\\n\";\n while ($c > 0) {\n $res = chr(ord('A') - 1 + $c%26).$res;\n $c = int($c/26);\n# $c -= 1;\n }\n $res .= $r;\n } else {\n $x =~ m/([A-Z]+)(\\d+)/;\n my $r = $2;\n\n# print \"$1 $2\\n\";\n my $c = 0;\n my $pow = 0;\n my $len = length($1);\n foreach (split \"\", $1) {\n# print \"$_\\n\";\n $pow += 1;\n $c += (ord($_) - ord('A') + 1)*(26**($len-$pow));\n# print \"$len $pow $_ $c\\n\";\n }\n\n $res = \"R\".$r.\"C\".$c;\n }\n\n return $res;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = ;\nchomp $n;\n\nwhile ($n--) {\n my $line = ;\n chomp $line;\n print trans($line), \"\\n\";\n}\n\nsub trans\n{\n my $x = shift @_;\n my $res = \"\";\n\n if ($x =~ m/R(\\d+)C(\\d+)/) {\n my $r = $1;\n my $c = $2;\n\n# print \"$r $c\\n\";\n $c -= 1;\n while ($c > 0) {\n $res = chr(ord('A') + $c%26).$res;\n $c = int($c/26);\n $c -= 1;\n }\n $res .= $r;\n } else {\n $x =~ m/([A-Z]+)(\\d+)/;\n my $r = $2;\n\n# print \"$1 $2\\n\";\n my $c = 0;\n my $pow = 0;\n my $len = length($1);\n foreach (split \"\", $1) {\n# print \"$_\\n\";\n $pow += 1;\n $c += (ord($_) - ord('A') + 1)*(26**($len-$pow));\n# print \"$len $pow $_ $c\\n\";\n }\n\n $res = \"R\".$r.\"C\".$c;\n }\n\n return $res;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = ;\nchomp $n;\n\nwhile ($n--) {\n my $line = ;\n chomp $line;\n print trans($line), \"\\n\";\n}\n\nsub trans\n{\n my $x = shift @_;\n my $res = \"\";\n\n if ($x =~ m/R(\\d+)C(\\d+)/) {\n my $r = $1;\n my $c = $2;\n\n# print \"$r $c\\n\";\n $c -= 1;\n while ($c > 0) {\n $res = chr(ord('A') + $c%26).$res;\n $c = int($c/26);\n $c -= 1;\n }\n $res .= $r;\n } else {\n $x =~ m/([A-Z]+)(\\d+)/;\n my $r = $2;\n\n# print \"$1 $2\\n\";\n my $c = 0;\n foreach (split \"\", $1) {\n# print \"$_\\n\";\n $c *= 26;\n $c += ord($_) - ord('A') + 1;\n }\n\n $res = \"R\".$r.\"C\".$c;\n }\n\n return $res;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = ;\nchomp $n;\n\nwhile ($n--) {\n my $line = ;\n chomp $line;\n print trans($line), \"\\n\";\n}\n\nsub trans\n{\n my $x = shift @_;\n my $res = \"\";\n\n if ($x =~ m/R(\\d+)C(\\d+)/) {\n my $r = $1;\n my $c = $2;\n\n# print \"$r $c\\n\";\n $c -= 1;\n while ($c > 0) {\n $res = chr(ord('A') + $c%26).$res;\n $c = int($c/26);\n $c -= 1;\n }\n $res .= $r;\n } else {\n $x =~ m/([A-Z]+)(\\d+)/;\n my $r = $2;\n\n# print \"$1 $2\\n\";\n my $c = 0;\n foreach (split \"\", $1) {\n print \"$_\\n\";\n $c *= 26;\n $c += ord($_) - ord('A') + 1;\n }\n\n $res = \"R\".$r.\"C\".$c;\n }\n\n return $res;\n}\n"}, {"source_code": "\nsub RC_to_CR {\n\tmy $rc = shift;\n\t$rc =~ /^R(\\d+)C(\\d+)/;\n\tmy $row = $1;\n\tmy $col = $2;\n\tmy $ret = '';\n\twhile($col > 26) {\n\t\t$ret .= chr(64+($col % 26));\n\t\t$col /= 26;\n\t}\n\t$ret .= chr(64+$col);\n\t$ret = scalar reverse $ret;\n\t$ret .= $row;\n\treturn $ret;\n}\n\nsub CR_to_RC {\n\tmy $cr = shift;\n\t$cr =~ /([A-Z]+)(\\d+)/;\n\tmy $col = $1;\n\tmy $row = $2;\n\tmy $ret = 'R'.$row.\"C\";\n\tmy $p = length($col)-1;\n\tmy $c = 0;\n\tforeach my $ltr (split //, $col) {\n\t\tmy $v = (ord($ltr)-64);\n\t\t$c += ($v * 26**$p);\n\t\t$p--;\n\t}\n\t$ret .= $c;\n\treturn $ret;\n}\n\nmy $num = ;\nchomp($num);\nwhile($num-- > 0) {\n\tchomp($row = );\n\tif($row =~ /^R\\d+C\\d+/) {\n\t\tprint RC_to_CR($row).\"\\n\";\n\t} else {\n\t\tprint CR_to_RC($row).\"\\n\";\n\t}\n}"}, {"source_code": "\nsub RC_to_CR {\n\tmy $rc = shift;\n\t$rc =~ /^R(\\d+)C(\\d+)/;\n\tmy $row = $1;\n\tmy $col = $2;\n\tmy $ret = '';\n\tmy $p = 1;\n\twhile($col > 26) {\n\t\tif($col % 26**$p == 0) {\n\t\t\t$ret .= \"Z\";\n\t\t\t$col -= 26**$p;\n\t\t} else {\n\t\t\tmy $tmp = int($col / 26);\n#\t\t$tmp = 1 if($tmp == 0);\n\t\t\tmy $v = $col - $tmp * 26;\n#\t\t\tprint \"Got $v, it turns into \".chr(64+$v).\"\\n\";\n\t\t\t$ret .= chr(64+$v);\n\t\t}\n\t\t$col = int($col/26);\n\t\t$p++;\n\t}\n\n\t$ret .= chr(64+$col);\n\t$ret = scalar reverse $ret;\n\t$ret .= $row;\n\treturn $ret;\n}\n\nsub CR_to_RC {\n\tmy $cr = shift;\n\t$cr =~ /([A-Z]+)(\\d+)/;\n\tmy $col = $1;\n\tmy $row = $2;\n\tmy $ret = 'R'.$row.\"C\";\n\tmy $l = length($col)-1;\n\tmy $c = 0;\n\tmy $p = $l;\n\tforeach my $ltr (split //, $col) {\n\t\tmy $v = (ord($ltr)-64);\n\t\t$c += ($v * 26**$p);\n\t\t$p--;\n\t}\n\t$ret .= $c;\n\treturn $ret;\n}\n\nmy $num = ;\nchomp($num);\nwhile($num-- > 0) {\n\tchomp($row = );\n\tif($row =~ /^R\\d+C\\d+/) {\n\t\tprint RC_to_CR($row).\"\\n\";\n\t} else {\n\t\tprint CR_to_RC($row).\"\\n\";\n\t}\n}\n"}, {"source_code": "\nsub RC_to_CR {\n\tmy $rc = shift;\n\t$rc =~ /^R(\\d+)C(\\d+)/;\n\tmy $row = $1;\n\tmy $col = $2;\n\tmy $ret = '';\n\twhile($col > 26) {\n\t\t$ret .= chr(64+($col % 26));\n\t\t$col /= 26;\n\t}\n\t$ret .= chr(64+$col);\n\t$ret = scalar reverse $ret;\n\t$ret .= $row;\n\treturn $ret;\n}\n\nsub CR_to_RC {\n\tmy $cr = shift;\n\t$cr =~ /([A-Z]+)(\\d+)/;\n\tmy $col = $1;\n\tmy $row = $2;\n\tmy $ret = 'R'.$row.\"C\";\n\tmy $l = length($col)-1;\n\tmy $c = 0;\n\tfor(my $p=$l;$p>=0;$p--) {\n\t\tmy $ltr = substr($col,($p-$l), 1);\n\t\tmy $v = (ord($ltr)-64);\n\t\t$c += ($v * 26**$p);\n\t}\n\t$ret .= $c;\n\treturn $ret;\n}\n\nmy $num = ;\nchomp($num);\nwhile($num-- > 0) {\n\tchomp($row = );\n\tif($row =~ /^R\\d+C\\d+/) {\n\t\tprint RC_to_CR($row).\"\\n\";\n\t} else {\n\t\tprint CR_to_RC($row).\"\\n\";\n\t}\n}"}, {"source_code": "\nsub RC_to_CR {\n\tmy $rc = shift;\n\t$rc =~ /^R(\\d+)C(\\d+)/;\n\tmy $row = $1;\n\tmy $col = $2;\n\tmy $ret = '';\n\tmy $p = 1;\n\twhile($col > 26) {\n\t\tif($col % 26**$p == 0) {\n\t\t\t$ret .= \"Z\";\n\t\t\t$col /= 26;\n\t\t} else {\n\t\t\tmy $tmp = int($col / 26);\n\t\t\tmy $v = $col - $tmp * 26;\n\t\t\t$ret .= chr(64+$v);\n\t\t\t$col /= 26;\n\t\t}\n\t\t$p++;\n\t}\n\t$ret .= chr(64+$col);\n\t$ret = scalar reverse $ret;\n\t$ret .= $row;\n\treturn $ret;\n}\n\nsub CR_to_RC {\n\tmy $cr = shift;\n\t$cr =~ /([A-Z]+)(\\d+)/;\n\tmy $col = $1;\n\tmy $row = $2;\n\tmy $ret = 'R'.$row.\"C\";\n\tmy $l = length($col)-1;\n\tmy $c = 0;\n\tmy $p = $l;\n\tforeach my $ltr (split //, $col) {\n\t\tmy $v = (ord($ltr)-64);\n\t\t$c += ($v * 26**$p);\n\t\t$p--;\n\t}\n\t$ret .= $c;\n\treturn $ret;\n}\n\nmy $num = ;\nchomp($num);\nwhile($num-- > 0) {\n\tchomp($row = );\n\tif($row =~ /^R\\d+C\\d+/) {\n\t\tprint RC_to_CR($row).\"\\n\";\n\t} else {\n\t\tprint CR_to_RC($row).\"\\n\";\n\t}\n}\n"}, {"source_code": "\nsub RC_to_CR {\n\tmy $rc = shift;\n\t$rc =~ /^R(\\d+)C(\\d+)/;\n\tmy $row = $1;\n\tmy $col = $2;\n\tmy $ret = '';\n\tmy $p = 1;\n\twhile($col > 26) {\n\t\tif($col % 26**$p == 0) {\n\t\t\t$ret .= \"Z\";\n$col -= 26**$p;\n\t\t\t$col /= 26;\n\t\t} else {\n\t\t\tmy $tmp = int($col / 26);\n\t\t\tmy $v = $col - $tmp * 26;\n\t\t\t$ret .= chr(64+$v);\n\t\t\t$col /= 26;\n\t\t}\n\t\t$p++;\n\t}\n\t$ret .= chr(64+$col);\n\t$ret = scalar reverse $ret;\n\t$ret .= $row;\n\treturn $ret;\n}\n\nsub CR_to_RC {\n\tmy $cr = shift;\n\t$cr =~ /([A-Z]+)(\\d+)/;\n\tmy $col = $1;\n\tmy $row = $2;\n\tmy $ret = 'R'.$row.\"C\";\n\tmy $l = length($col)-1;\n\tmy $c = 0;\n\tmy $p = $l;\n\tforeach my $ltr (split //, $col) {\n\t\tmy $v = (ord($ltr)-64);\n\t\t$c += ($v * 26**$p);\n\t\t$p--;\n\t}\n\t$ret .= $c;\n\treturn $ret;\n}\n\nmy $num = ;\nchomp($num);\nwhile($num-- > 0) {\n\tchomp($row = );\n\tif($row =~ /^R\\d+C\\d+/) {\n\t\tprint RC_to_CR($row).\"\\n\";\n\t} else {\n\t\tprint CR_to_RC($row).\"\\n\";\n\t}\n}\n"}, {"source_code": "use strict;\nuse warnings;\nuse utf8;\n\nmy $n=;\nchomp($n);\n\nour @dic=(\"A\",\"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\");\nour %alphabet = do { my $i; map { $_ => ++$i } qw(A B C D E F G H I J K L M N O P Q R S T U V Q X Y Z) };\n\nfor(my $i=0; $i<$n; $i++){\n\tmy $stdin=;\n\tchomp($stdin);\n\tprint solveB($stdin).\"\\n\";\n}\n\nsub solveB{\n\tmy $s=$_[0];\n\tif($s=~/^R(\\d+)C(\\d+)$/){\n\t\treturn solve1($2,\"\").$1;\n\t}\n\tif($s=~/^([A-Z]+)(\\d+)$/){\n\t\tmy $ans=0;\n\t\tfor(my $i=0; $i;\nchomp($n);\n\nour @dic=(\"A\",\"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\");\nour %alphabet = do { my $i; map { $_ => ++$i } qw(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\nfor(my $i=0; $i<$n; $i++){\n\tmy $stdin=;\n\tchomp($stdin);\n\tprint solveB($stdin).\"\\n\";\n}\n\nsub solveB{\n\tmy $s=$_[0];\n\tif($s=~/^R(\\d+)C(\\d+)$/){\n\t\treturn solve1($2,\"\").$1;\n\t}\n\tif($s=~/^([A-Z]+)(\\d+)$/){\n\t\tmy $ans=0;\n\t\tfor(my $i=0; $i;\n for (1 .. $n) {\n\tmy $s = <>;\n\tif ($s =~ /^R([\\d]*)C([\\d]*)$/) {\n\t print ntos($2) . \"$1\\n\";\n\t} elsif ($s =~ /^([\\D]*)([\\d]*)$/) {\n\t print \"R$2C\" . (ston $1) . \"\\n\";\n\t} else {\n\t exit(-1);\n\t}\n }\n}\n\nmain();\n"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\nuse integer;\n\nsub ntos {\n my $n = shift;\n my @arr = ('A' .. 'Z');\n my $s = '';\n while ($n > 0) {\n\t$s = @arr[$n % 26 - 1] . $s;\n\t$n /= 26;\n }\n return $s;\n}\n\nsub ston {\n my $s = shift;\n my %hash;\n @hash{('A'..'Z')} = (1 .. 26);\n my $n = 0;\n for (split //, $s) {\n\t$n *= 26;\n\t$n += $hash{$_};\n }\n return $n;\n}\n\nsub main {\n my $n = int <>;\n for (1 .. $n) {\n\tmy $s = <>;\n\tif ($s =~ /^R([\\d]+)C([\\d]+)$/) {\n\t print ntos($2) . \"$1\\n\";\n\t} elsif ($s =~ /^([\\D]*)([\\d]*)$/) {\n\t print \"R$2C\" . (ston $1) . \"\\n\";\n\t} else {\n\t exit(-1);\n\t}\n }\n}\n\nmain();\n"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nno warnings 'recursion';\nuse strict;\n\nsub ltn{\n my @k=split//,shift;my $ln=shift;\n if(@k==1){return $$ln{$k[0]};}\n else{\n\tmy $h=pop @k;my $f=join \"\",@k;\n\treturn ($$ln{$h}+26*ltn($f,$ln));\n }\n}\nsub ntl{\n my $t=shift;my $ln=shift;\n if($t>=26){return ((($t%26==0)?ntl(int($t/26-1),$ln):ntl(int($t/26),$ln)).$$ln{$t%26});}\n else{return $$ln{$t};}\n}\n\nmy $row_n;my %ln;$ln{0}=\"Z\";$ln{\"Z\"}=26;my $l=\"A\";\nfor my $j(1..25){\n $ln{$j}=$l;\n $ln{$l}=$j;\n ++$l;\n}my $g=\\%ln;\nmy $n=;\nmy @final_list;\nforeach my $i (1..$n){\n my $sr=;\n chomp($sr);\n if($sr=~/^R([0-9]+)C([0-9]+)$/){\n\tmy $p=($2>26)?ntl($2,$g):$ln{$2%26};\n\tpush @final_list,\"$p\".\"$1\\n\";\n }\n elsif($sr=~/([A-Z]+)([0-9]+)/){\n\tmy $col=ltn($1,$g);\n\t$row_n=$2;\n\tpush @final_list,\"R$row_n\".\"C$col\\n\";\n }\n}\nprint @final_list;\n"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nno warnings 'recursion';\nuse strict;\n\nsub ltn{\n my @k=split//,shift;my $ln=shift;\n if(@k==1){return $$ln{$k[0]};}\n else{\n\tmy $h=pop @k;my $f=join \"\",@k;\n\treturn ($$ln{$h}+26*ltn($f,$ln));\n }\n}\nsub ntl{\n my $t=shift;my $ln=shift;\n if($t==0){return \"\";}\n elsif($t>26){return (($t%26==0)?ntl(int($t/26-1),$ln):ntl(int($t/26)),$ln).$$ln{$t%26};}\n else{return $$ln{$t};}\n}\n\nmy $row_n;my %ln;$ln{0}=\"Z\";$ln{\"Z\"}=26;my $l=\"A\";\nfor my $j(1..25){\n $ln{$j}=$l;\n $ln{$l}=$j;\n ++$l;\n}my $g=\\%ln;\nmy $n=;\nmy @final_list;\nforeach my $i (1..$n){\n my $sr=;\n chomp($sr);\n if($sr=~/^R([0-9]+)C([0-9]+)$/){\n\tmy $p=ntl($2,$g);\n\tpush @final_list,\"$p\".\"$1\\n\";\n }\n elsif($sr=~/([A-Z]+)([0-9]+)/){\n\tmy $col=ltn($1,$g);\n\t$row_n=$2;\n\tpush @final_list,\"R$row_n\".\"C$col\\n\";\n }\n}\nprint @final_list;\n"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nno warnings 'recursion';\nuse strict;\n\nsub ltn{\n my @k=split//,shift;my $ln=shift;\n if(@k==1){return $$ln{$k[0]};}\n else{\n\tmy $h=pop @k;my $f=join \"\",@k;\n\treturn ($$ln{$h}+26*ltn($f,$ln));\n }\n}\nsub ntl{\n my $t=shift;my $ln=shift;\n if($t==0){return \"\";}\n elsif($t>26){return ((($t%26==0)?ntl(int($t/26-1),$ln):ntl(int($t/26),$ln)).$$ln{$t%26});}\n else{return $$ln{$t};}\n}\n\nmy $row_n;my %ln;$ln{0}=\"Z\";$ln{\"Z\"}=26;my $l=\"A\";\nfor my $j(1..25){\n $ln{$j}=$l;\n $ln{$l}=$j;\n ++$l;\n}my $g=\\%ln;\nmy $n=;\nmy @final_list;\nforeach my $i (1..$n){\n my $sr=;\n chomp($sr);\n if($sr=~/^R([0-9]+)C([0-9]+)$/){\n\tmy $p=ntl($2,$g);\n\tpush @final_list,\"$p\".\"$1\\n\";\n }\n elsif($sr=~/([A-Z]+)([0-9]+)/){\n\tmy $col=ltn($1,$g);\n\t$row_n=$2;\n\tpush @final_list,\"R$row_n\".\"C$col\\n\";\n }\n}\nprint @final_list;\n"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nno warnings 'recursion';\nuse strict;\n\nsub ltn{\n my @k=split//,shift;my $ln=shift;\n if(@k==1){return $$ln{$k[0]};}\n else{\n\tmy $h=pop @k;my $f=join \"\",@k;\n\treturn ($$ln{$h}+26*ltn($f,$ln));\n }\n}\nsub ntl{\n my $t=shift;my $ln=shift;\n if($t==0){return \"\";}\n elsif($t==1){return \"A\";}\n elsif($t>26){return ntl(int($t/26),$ln).$$ln{$t%26};}\n else{return $$ln{$t-1};}\n}\n\nmy $row_n;my %ln;$ln{0}=\"Z\";$ln{\"Z\"}=26;my $l=\"A\";\nfor my $j(1..25){\n $ln{$j}=$l;\n $ln{$l}=$j;\n ++$l;\n}my $g=\\%ln;\nmy $n=;\nmy @final_list;\nforeach my $i (1..$n){\n my $sr=;\n chomp($sr);\n if($sr=~/^R([0-9]+)C([0-9]+)$/){\n\tmy $p=ntl($2,$g);\n\tpush @final_list,\"$p\".\"$1\\n\";\n }\n elsif($sr=~/([A-Z]+)([0-9]+)/){\n\tmy $col=ltn($1,$g);\n\t$row_n=$2;\n\tpush @final_list,\"R$row_n\".\"C$col\\n\";\n }\n}\nprint @final_list;\n"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nno warnings 'recursion';\nuse strict;\n\nsub ltn{\n my @k=split//,shift;my $ln=shift;\n if(@k==1){return $$ln{$k[0]};}\n else{\n\tmy $h=pop @k;my $f=join \"\",@k;\n\treturn ($$ln{$h}+26*ltn($f,$ln));\n }\n}\nsub ntl{\n my $t=shift;my $ln=shift;\n if($t==0){return \"\";}\n elsif($t>26){return (($t%26)?ntl(int($t/26-1),$ln):ntl(int($t/26)),$ln).$$ln{$t%26};}\n else{return $$ln{$t};}\n}\n\nmy $row_n;my %ln;$ln{0}=\"Z\";$ln{\"Z\"}=26;my $l=\"A\";\nfor my $j(1..25){\n $ln{$j}=$l;\n $ln{$l}=$j;\n ++$l;\n}my $g=\\%ln;\nmy $n=;\nmy @final_list;\nforeach my $i (1..$n){\n my $sr=;\n chomp($sr);\n if($sr=~/^R([0-9]+)C([0-9]+)$/){\n\tmy $p=ntl($2,$g);\n\tpush @final_list,\"$p\".\"$1\\n\";\n }\n elsif($sr=~/([A-Z]+)([0-9]+)/){\n\tmy $col=ltn($1,$g);\n\t$row_n=$2;\n\tpush @final_list,\"R$row_n\".\"C$col\\n\";\n }\n}\nprint @final_list;\n"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nno warnings 'recursion';\nuse strict;\n\nsub ltn{\n my @k=split//,shift;my $ln=shift;\n if(@k==1){return $$ln{$k[0]};}\n else{\n\tmy $h=pop @k;my $f=join \"\",@k;\n\treturn ($$ln{$h}+26*ltn($f,$ln));\n }\n}\nsub ntl{\n my $t=shift;my $ln=shift;\n if($t==0){return \"\";}\n elsif($t>26){return ntl(int($t/26),$ln).$$ln{$t%26};}\n else{return $$ln{$t};}\n}\n\nmy $row_n;my %ln;$ln{0}=\"Z\";$ln{\"Z\"}=26;my $l=\"A\";\nfor my $j(1..25){\n $ln{$j}=$l;\n $ln{$l}=$j;\n ++$l;\n}my $g=\\%ln;\nmy $n=;\nmy @final_list;\nforeach my $i (1..$n){\n my $sr=;\n chomp($sr);\n if($sr=~/^R([0-9]+)C([0-9]+)$/){\n\tmy $p=ntl($2,$g);\n\tpush @final_list,\"$p\".\"$1\\n\";\n }\n elsif($sr=~/([A-Z]+)([0-9]+)/){\n\tmy $col=ltn($1,$g);\n\t$row_n=$2;\n\tpush @final_list,\"R$row_n\".\"C$col\\n\";\n }\n}\nprint @final_list;\n"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nno warnings 'recursion';\nuse strict;\n\nsub ltn{\n my @k=split//,shift;my $ln=shift;\n if(@k==1){return $$ln{$k[0]};}\n else{\n\tmy $h=pop @k;my $f=join \"\",@k;\n\treturn ($$ln{$h}+26*ltn($f,$ln));\n }\n}\nsub ntl{\n my $t=shift;my $ln=shift;\n if($t>26){return ((($t%26==0)?ntl(int($t/26-1),$ln):ntl(int($t/26),$ln)).$$ln{$t%26});}\n else{return $$ln{$t};}\n}\n\nmy $row_n;my %ln;$ln{0}=\"Z\";$ln{\"Z\"}=26;my $l=\"A\";\nfor my $j(1..25){\n $ln{$j}=$l;\n $ln{$l}=$j;\n ++$l;\n}my $g=\\%ln;\nmy $n=;\nmy @final_list;\nforeach my $i (1..$n){\n my $sr=;\n chomp($sr);\n if($sr=~/^R([0-9]+)C([0-9]+)$/){\n\tmy $p=($2>26)?ntl($2,$g):$ln{$2%26};\n\tpush @final_list,\"$p\".\"$1\\n\";\n }\n elsif($sr=~/([A-Z]+)([0-9]+)/){\n\tmy $col=ltn($1,$g);\n\t$row_n=$2;\n\tpush @final_list,\"R$row_n\".\"C$col\\n\";\n }\n}\nprint @final_list;\n"}, {"source_code": "#!/usr/bin/perl\n\n\n%num2char = map { ($_ + 1) => chr(ord('A') + $_) } (0..25) ;\n%char2num = reverse %num2char;\n\n\n#map { print \"$_ $num2char{$_}\\n\" } sort { $a <=> $b } keys %num2char;\n#map { print \"$_ $char2num{$_}\\n\" } sort keys %char2num;\n\n$n = <>;\n\nfor (1..$n) {\n $input = <>;\n if ($input =~ /^R(\\d+)C(\\d+)$/) {\n #print \"Row-column to column-row\\n\";\n $row = $1;\n $col = $2;\n $new_char = undef;\n while ($col > 0) {\n $new_char = $num2char{$col % 26} . $new_char;\n $col = int($col / 26);\n }\n $col = $new_char;\n print \"$col$row\\n\";\n }\n elsif ($input =~ /^([A-Z]+)(\\d+)$/) {\n #print \"Column-row to row-column $1, $2\\n\";\n $row = $2;\n $col = $1;\n $count = 0;\n $sum = 0;\n for $c (reverse split //,$col) {\n $sum += (26 ** $count++) * $char2num{$c}; \n }\n $col = $sum;\n print \"R$row\" . \"C$col\\n\";\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\n\n\n%num2char = map { ($_ + 1) => chr(ord('A') + $_) } (0..25) ;\n%char2num = reverse %num2char;\n\n\n#map { print \"$_ $num2char{$_}\\n\" } sort { $a <=> $b } keys %num2char;\n#map { print \"$_ $char2num{$_}\\n\" } sort keys %char2num;\n\n$n = <>;\n\nfor (1..$n) {\n $input = <>;\n if ($input =~ /^R(\\d+)C(\\d+)$/) {\n #print \"Row-column to column-row\\n\";\n $row = $1;\n $col = $2;\n $new_char = undef;\n while ($col > 1) {\n $new_char = ($col % 26 == 0 ? $num2char{26} : $num2char{$col % 26}) . $new_char;\n $col = int($col / 26);\n }\n $col = $new_char;\n print \"$col$row\\n\";\n }\n elsif ($input =~ /^([A-Z]+)(\\d+)$/) {\n #print \"Column-row to row-column $1, $2\\n\";\n $row = $2;\n $col = $1;\n $count = 0;\n $sum = 0;\n for $c (reverse split //,$col) {\n $sum += (26 ** $count++) * $char2num{$c}; \n }\n $col = $sum;\n print \"R$row\" . \"C$col\\n\";\n }\n}\n"}, {"source_code": "#!/usr/local/bin/perl\n$n = <>;\nfor ( 1 .. $n ) {\n $line = <>;\n\n if ( $line =~ /(\\D+)(\\d+)(\\D+)(\\d+)/ ) {\n print makeNormal($2,$4);\n\n }\n elsif($line =~ /(\\D+)(\\d+)/ ){ \n print makeRC($1 , $2);\n }\n\n}\n sub makeNormal {\n use integer;\n my $row = $_[0];\n my $col = $_[1];\n my @alph = (A..Z);\n \n $result = $row;\n do{\n my $r, $letter;\n $col -= 1;\n $r = $col % 26;\n $col = $col / 26; \n $result = @alph[$r].$result;\n } while ($col);\n return $result;\n }\n\n sub makeRC {\n use integer;\n my $row = $_[1];\n my $col = $_[0];\n my $resCol = 0;\n my $n = 1;\n my $A = ord(\"A\");\n foreach $ch (reverse split (// , $col)){\n $resCol+= (ord($ch) - $A +1) * $n;\n $n *= 26;\n }\n return \"R\".$row.\"C\".$resCol;\n \n \n }\n"}, {"source_code": "my $num = ;\nfor (1..$num) {\n my $input = ;\n chomp $input;\n if ($input =~ /R(\\d+)C(\\d+)/) {\n my ($row, $col) = ($1, $2);\n my @col;\n while ($col >= 26) {\n push @col, $col % 26;\n $col = int ($col/26);\n }\n push @col, $col;\n @col = reverse @col;\n my $str;\n $str .= ('A'..'Z')[$_-1] foreach @col;\n $str .= $row;\n print $str, \"\\n\";\n } else {\n $input =~ /([A-Z]+)(\\d+)/;\n my ($col_str, $row) = ($1, $2);\n my $col = 0;\n foreach (map { ord($_) - 64 } split //, $col_str) {\n $col *= 26;\n $col += $_;\n }\n print \"R\", $row, \"C\", $col, \"\\n\";\n }\n}"}, {"source_code": "my $num = ;\nfor (1..$num) {\n my $input = ;\n chomp $input;\n if ($input =~ /R(\\d+)C(\\d+)/) {\n my ($row, $col) = ($1, $2);\n my $str;\n while ($col >= 26) {\n $str .= ('A'..'Z')[int ($col/26)-1];\n $col %= 26;\n }\n $str .= ('A'..'Z')[$col-1] . $row;\n print $str, \"\\n\";\n } else {\n $input =~ /([A-Z]+)(\\d+)/;\n my ($col_str, $row) = ($1, $2);\n my $col = 0;\n foreach (map { ord($_) - 64 } split //, $col_str) {\n $col *= 26;\n $col += $_;\n }\n print \"R\", $row, \"C\", $col, \"\\n\";\n }\n}"}, {"source_code": "\nmy $num = ;\nfor (1..$num) {\n my $input = ;\n chomp $input;\n if ($input =~ /R(\\d+)C(\\d+)/) {\n my ($row, $col) = ($1, $2);\n my @col;\n while ($col >= 26) {\n push @col, $col % 26;\n $col = int (($col-1)/26);\n }\n push @col, $col;\n @col = reverse @col;\n my $str;\n $str .= ('A'..'Z')[$_-1] foreach @col;\n $str .= $row;\n print $str, \"\\n\";\n } else {\n $input =~ /([A-Z]+)(\\d+)/;\n my ($col_str, $row) = ($1, $2);\n my $col = 0;\n foreach (map { ord($_) - 64 } split //, $col_str) {\n $col *= 26;\n $col += $_;\n }\n print \"R\", $row, \"C\", $col, \"\\n\";\n }\n}\n\n"}, {"source_code": "\n#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my $n = int <>;\n for(1..$n) {\n my $line = <>;\n if ($line =~ /R[0-9]+C[0-9]+/) {\n my $row = $line;\n $row =~ s/R//g;\n $row =~ s/C[0-9]+//g;\n my $col = $line;\n $col =~ s/^R[0-9]+C//g;\n print convertToSymbols($col).\"$row\\n\";\n } else {\n my $row = $line;\n $row =~ s/[A-Z]+//g;\n my $col = $line;\n $col =~ s/[0-9]+//g;\n print \"R\".$row.\"C\".convertFromSymbols($col).\"\\n\";\n }\n }\n}\n\nsub convertToSymbols {\n my $cur = shift;\n my $result = \"\";\n while ($cur > 0) {\n my $c = chr(ord(\"A\") + $cur % 26 - 1);\n $result = $result.$c;\n $cur = ($cur - $cur % 26) / 26;\n }\n return reverse $result;\n}\n\nsub convertFromSymbols {\n my $s = shift;\n my @arr = split //, $s;\n my $result = 0;\n foreach(@arr) {\n $result = $result * 26 + ord($_) - ord(\"A\") + 1;\n }\n return $result;\n}"}, {"source_code": "#!/usr/bin/perl\n\nmain();\n\nsub main {\n my $n = int <>;\n for(1..$n) {\n my $line = <>;\n if ($line =~ /(\\D+)(\\d+)(\\D+)(\\d+)/) {\n print convertToSymbols($4).\"$2\\n\";\n next;\n }\n if ($line =~ /(\\D+)(\\d+)/) {\n print \"R\".$2.\"C\".convertFromSymbols($1).\"\\n\";\n }\n }\n}\n\nsub convertToSymbols {\n my $cur = shift;\n my $result = \"\";\n while ($cur > 0) {\n my $c = chr(ord(\"A\") + $cur % 26 - 1);\n $result = $result.$c;\n $cur = ($cur - $cur % 26) / 26;\n }\n return reverse $result;\n}\n\nsub convertFromSymbols {\n my $s = shift;\n my @arr = split //, $s;\n my $result = 0;\n foreach(@arr) {\n $result = $result * 26 + ord($_) - ord(\"A\") + 1;\n }\n return $result;\n}"}, {"source_code": "\n#!/usr/bin/perl\n\nmain();\n\nsub main {\n my $n = int <>;\n for(1..$n) {\n my $line = <>;\n if ($line =~ /(\\D+)(\\d+)(\\D+)(\\d+)/) {\n print convertToSymbols($4).\"$2\\n\";\n next;\n }\n if ($line =~ /(\\D+)(\\d+)/) {\n print \"R\".$2.\"C\".convertFromSymbols($1).\"\\n\";\n }\n }\n}\n\nsub convertToSymbols {\n my $cur = shift;\n my $result = \"\";\n while ($cur > 0) {\n my $c = chr(ord(\"A\") + ($cur - 1) % 26);\n $result = $result.$c;\n $cur = ($cur - $cur % 26) / 26;\n }\n return reverse $result;\n}\n\nsub convertFromSymbols {\n my $s = shift;\n my @arr = split //, $s;\n my $result = 0;\n foreach(@arr) {\n $result = $result * 26 + ord($_) - ord(\"A\") + 1;\n }\n return $result;\n}"}, {"source_code": "\n#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my $n = int <>;\n for(1..$n) {\n my $line = <>;\n if ($line =~ /R[0-9]+C[0-9]+/) {\n my $row = $line;\n $row =~ s/R//g;\n $row =~ s/C[0-9]+//g;\n my $col = $line;\n $col =~ s/^R[0-9]+C//g;\n print convertToSymbols($col).\"$row\";\n } else {\n my $row = $line;\n $row =~ s/[A-Z]+//g;\n my $col = $line;\n $col =~ s/[0-9]+//g;\n print \"R\".$row.\"C\".convertFromSymbols($col);\n }\n }\n}\n\nsub convertToSymbols {\n my $cur = shift;\n my $result = \"\";\n while ($cur > 0) {\n my $c = chr(ord(\"A\") + $cur % 26 - 1);\n $result = $result.$c;\n $cur = ($cur - $cur % 26) / 26;\n }\n return reverse $result;\n}\n\nsub convertFromSymbols {\n my $s = shift;\n my @arr = split //, $s;\n my $result = 0;\n foreach(@arr) {\n $result = $result * 26 + ord($_) - ord(\"A\") + 1;\n }\n return $result;\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmain();\nsub main {\n my $n = int <>;\n for(1..$n) {\n my $line = <>;\n if ($line =~ /R[0-9]+C[0-9]+/) {\n my $row = $line;\n $row =~ s/R//g;\n $row =~ s/C[0-9]+//g;\n my $col = $line;\n $col =~ s/^R[0-9]+C//g;\n print convertToSymbols($col).\"$row\";\n } else {\n my $row = $line;\n $row =~ s/[A-Z]+//g;\n my $col = $line;\n $col =~ s/[0-9]+//g;\n print \"R\".$row.\"C\".convertFromSymbols($col);\n }\n }\n}\n\nsub convertToSymbols {\n my $cur = shift;\n my $result = \"\";\n while ($cur > 0) {\n my $c = chr(ord(\"A\") + $cur % 26 - 1);\n $result = $result.$c;\n $cur = ($cur - $cur % 26) / 26;\n }\n return reverse $result;\n}\n\nsub convertFromSymbols {\n my $s = shift;\n my @arr = split //, $s;\n my $result = 0;\n foreach(@arr) {\n $result = $result * 26 + ord($_) - ord(\"A\") + 1;\n }\n return $result;\n}"}, {"source_code": "\n#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my $n = int <>;\n for(1..$n) {\n my $line = <>;\n if ($line =~ /R[0-9]+C[0-9]+/) {\n my $row = $line;\n $row =~ s/R//g;\n $row =~ s/C[0-9]+//g;\n my $col = $line;\n $col =~ s/^R[0-9]+C//g;\n print convertToSymbols($col).\"$row\";\n } else {\n my $row = $line;\n $row =~ s/[A-Z]+//g;\n my $col = $line;\n $col =~ s/[0-9]+//g;\n print \"R\".convertFromSymbols($col).\"C$row\";\n }\n }\n}\n\nsub convertToSymbols {\n my $cur = shift;\n my $result = \"\";\n while ($cur > 0) {\n my $c = chr(ord(\"A\") + $cur % 26 - 1);\n $result = $result.$c;\n $cur = ($cur - $cur % 26) / 26;\n }\n return reverse $result;\n}\n\nsub convertFromSymbols {\n my $s = shift;\n my @arr = split //, $s;\n my $result = 0;\n foreach(@arr) {\n $result = $result * 26 + ord($_) - ord(\"A\") + 1;\n }\n return $result;\n}"}, {"source_code": "use strict;\nuse warnings;\nuse bigint;\nmy $n = int(<>);\nmy $inp;\nfor (1..$n) {\n $inp = <>;\n if ($inp =~ /([A-Z]{1})(\\d+)([A-Z]{1})(\\d+)/) {\n print chr(64 + $4 / 26);\n print chr(64 + $4 % 26) if ($4);\n print $2, \"\\n\";\n }\n else {\n $inp =~ /([A-Z]+)([0-9]+)/;\n print \"R$2C\";\n my @arr = split \"\", $1;\n if (length $1 == 1) {\n @arr = (chr 64, $1);\n }\n # print ord $arr[0], \"\\n\";\n print ((ord($arr[0]) - 64) * 26 + ord($arr[1]) - 64);\n }\n}\n"}, {"source_code": "\nmy @arr = ('A' .. 'Z');\nsub ntos {\n my $n = shift;\n my $s = \"\";\n while ($n > 0) {\n $s = @arr[$n % 26 - 1] . $s;\n $n-- unless($n % 26);\n $n /= 26;\n }\n return $s;\n}\n\nsub ston {\n my $n = 0;\n $n = $n * 26 + ord $_ - 64 for (split //, shift);\n return $n;\n}\nmy $n = int <>;\nfor (1 .. $n) {\n my $s = <>;\n if ($s =~ /^R([\\d]+)C([\\d]+)$/) {\n print ntos($2) . \"$1\\n\";\n }\n else {\n $s =~ /^([\\D]*)([\\d]*)$/;\n print \"R$2C\" . (ston $1) . \"\\n\";\n }\n}"}, {"source_code": "my $n = int(<>);\nmy $inp;\nmy $num;\nmy $str;\n\nfor (1..$n) {\n $inp = <>;\n $num = 0;\n if ($inp =~ /([A-Z]{1})(\\d+)([A-Z]{1})(\\d+)/) {\n $str = \"A\";\n $str++ for(1..($4-1));\n print \"$str$2\\n\";\n }\n else {\n $inp =~ /([A-Z]+)([0-9]+)/;\n $str = join \"\", (\"A\")x ((length $1)-1);\n $num++ for($str..$1);\n print \"R$2C$num\\n\";\n }\n}"}, {"source_code": "my $n = int(<>);\nmy $inp;\nfor (1..$n) {\n $inp = <>;\n if ($inp =~ /([A-Z]{1})(\\d+)([A-Z]{1})(\\d+)/) {\n print chr(64 + $4 / 26);\n print chr(64 + $4 % 26) if ($4);\n print $2, \"\\n\";\n }\n else {\n $inp =~ /([A-Z]+)([0-9]+)/;\n print \"R$2C\";\n my @arr = split \"\", $1;\n if (length $1 == 1) {\n @arr = (chr 64, $1);\n }\n # print ord $arr[0], \"\\n\";\n print ((ord($arr[0]) - 64) * 26 + ord($arr[1]) - 64);\n }\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\u00a0\u2014 the number of mines on the map (2\u2009\u2264\u2009n\u2009\u2264\u20091000). Each of the next n lines contains a pair of integers xi and yi\u00a0\u2014 the coordinates of the corresponding mine (\u2009-\u2009109\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009109). 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": "while(<>){\n\t$minx = 1000_000_000;\n\t$miny = 1000_000_000;\n\t$maxx = -1000_000_000;\n\t$maxy = -1000_000_000;\n\tfor (1..$_){\n\t\t($x,$y)=split/ /,<>;\n\t\t$x < $minx and $minx = $x;\n\t\t$x > $maxx and $maxx = $x;\n\t\t$y < $miny and $miny = $y;\n\t\t$y > $maxy and $maxy = $y;\n\t}\n\t$dx = $maxx - $minx;\n\t$dy = $maxy - $miny;\n\t$max = $dx;\n\t$dy > $max and $max = $dy;\n\t\n\tuse bigint;\n\tprint $max**2 ,$/;\n\tno bigint;\n\t}"}], "negative_code": [], "src_uid": "016e00b2b960be792d4ec7fcfb7976e2"} {"nl": {"description": "You are given an undirected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. Your task is to find the number of connected components which are cycles.Here are some definitions of graph theory.An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex $$$a$$$ is connected with a vertex $$$b$$$, a vertex $$$b$$$ is also connected with a vertex $$$a$$$). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.Two vertices $$$u$$$ and $$$v$$$ belong to the same connected component if and only if there is at least one path along edges connecting $$$u$$$ and $$$v$$$.A connected component is a cycle if and only if its vertices can be reordered in such a way that: the first vertex is connected with the second vertex by an edge, the second vertex is connected with the third vertex by an edge, ... the last vertex is connected with the first vertex by an edge, all the described edges of a cycle are distinct. A cycle doesn't contain any other edges except described above. By definition any cycle contains three or more vertices. There are $$$6$$$ connected components, $$$2$$$ of them are cycles: $$$[7, 10, 16]$$$ and $$$[5, 11, 9, 15]$$$. ", "input_spec": "The first line contains two integer numbers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$0 \\le m \\le 2 \\cdot 10^5$$$) \u2014 number of vertices and edges. The following $$$m$$$ lines contains edges: edge $$$i$$$ is given as a pair of vertices $$$v_i$$$, $$$u_i$$$ ($$$1 \\le v_i, u_i \\le n$$$, $$$u_i \\ne v_i$$$). There is no multiple edges in the given graph, i.e. for each pair ($$$v_i, u_i$$$) there no other pairs ($$$v_i, u_i$$$) and ($$$u_i, v_i$$$) in the list of edges.", "output_spec": "Print one integer \u2014 the number of connected components which are also cycles.", "sample_inputs": ["5 4\n1 2\n3 4\n5 4\n3 5", "17 15\n1 8\n1 12\n5 11\n11 9\n9 15\n15 5\n4 13\n3 13\n4 3\n10 16\n7 10\n16 7\n14 3\n14 4\n17 6"], "sample_outputs": ["1", "2"], "notes": "NoteIn the first example only component $$$[3, 4, 5]$$$ is also a cycle.The illustration above corresponds to the second example."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\tmy %h;\n\t\n\tmap {\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t$h{ $u }{ $v } ++;\n\t\t$h{ $v }{ $u } ++;\n\t\t} 1 .. $m;\n\t\n\tmy @u = keys %h;\n\t\n\tmy $cnt = 0;\n\t\n\tfor my $u ( @u ){\n\t\tnext if not exists $h{ $u };\n\t\tmy @try = $u;\n\t\t\n\t\tmy %fract;\n\t\t\n\t\twhile( @try ){\n\t\t\tmy $try = shift @try;\n\t\t\tmy @adj = keys %{ $h{ $try } };\n\t\t\t$fract{ 0 + @adj } ++;\n\t\t\tpush @try, @adj;\n\t\t##\tdelete $h{ $try }{ $_ } for @adj;\n\t\t\tdelete $h{ $try };\n\t\t\t}\n\t\t\n\t\t$debug and print map \"[$_]\", join ' ', sort keys %fract;\n\t\t\n\t\t$cnt ++ if 2 >= ( keys %fract ) and '0 2' eq join ' ', sort keys %fract;\n\t\t}\n\t\n\tprint $cnt;\n\t\n\t$debug and print '-' x 10;\n\t}"}], "negative_code": [], "src_uid": "cf7520d88e10ba171de443f36fdd2b73"} {"nl": {"description": "Heidi has finally found the mythical Tree of Life \u2013 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\u2009-\u20091 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 \u2013 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 \u2013 the number of vertices in the tree (1\u2009\u2264\u2009n\u2009\u2264\u200910000). The vertices are labeled with the numbers from 1 to n. Then n\u2009-\u20091 lines follow, each describing one edge using two space-separated numbers a\u2002b \u2013 the labels of the vertices connected by the edge (1\u2009\u2264\u2009a\u2009<\u2009b\u2009\u2264\u2009n). It is guaranteed that the input represents a tree.", "output_spec": "Print one integer \u2013 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": "$n = <>; for (<>) {\n\tchomp; ($a, $b) = split;\n\tpush @{ $conn[$a] }, $b;\n\tpush @{ $conn[$b] }, $a;\n}\nfor ($i = 1; $i <= $n; $i++) {\n\tfor $j (@{$conn[$i]}) {\n\t\t$c += grep($_ != $i, @{$conn[$j]});\n\t}\n}\nprint $c/2;\n"}], "negative_code": [], "src_uid": "fdd50853348b6f297a62a3b729d8d4a5"} {"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$$$)\u00a0\u2014 the number of testcases. Each testcase consists of a single line that contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^{16}$$$)\u00a0\u2014 the number of Petya's friends.", "output_spec": "For each testcase, print one integer\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\n#my @b = (0); $#b = 120;\r\n#for(my $i=1;$i<120;$i++){\r\n# my $b1 = 1000000000;\r\n# for(my $j1=0;$j1<=20;$j1++){\r\n# for(my $j2=0;$j2<=15;$j2++){\r\n# for(my $j3=0;$j3<=12;$j3++){\r\n# next if $j1 * 6 + $j2 * 8 + $j3 * 10 < $i;\r\n# my $t = 15 * $j1 + 20 * $j2 + 25 * $j3;\r\n# $b1 = $t if $t < $b1;\r\n# }\r\n# }\r\n# }\r\n# $b[$i] = $b1;\r\n#}\r\n\r\n#print &Dumper(\\@b);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my $r = 5 * (($n+1) / 2);\r\n $r = 15 if $n <= 6;\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nmy @b = (0); $#b = 120;\r\nfor(my $i=1;$i<120;$i++){\r\n my $b1 = 1000000000;\r\n for(my $j1=0;$j1<=20;$j1++){\r\n for(my $j2=0;$j2<=15;$j2++){\r\n for(my $j3=0;$j3<=12;$j3++){\r\n next if $j1 * 6 + $j2 * 8 + $j3 * 10 < $i;\r\n my $t = 15 * $j1 + 20 * $j2 + 25 * $j3;\r\n $b1 = $t if $t < $b1;\r\n }\r\n }\r\n }\r\n $b[$i] = $b1;\r\n}\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my $r = 300 * ( $n / 120 );\r\n my $k = $n % 120;\r\n $r += $b[$k];\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n"}], "src_uid": "3e27f1c06a263760f5b53c3afe4bf7ee"} {"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\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105) \u2014 the number of shows. Each of the next n lines contains two integers li and ri (0\u2009\u2264\u2009li\u2009<\u2009ri\u2009\u2264\u2009109) \u2014 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": "\n\n\n\n# declare two tv arrays\nmy @arr;\n\n# read n\nmy $f = ;\nchomp ( $f );\n\nmy $g;\nmy $s, $e;\n# read show intervals\nfor ( my $i = 0; $i < $f ; $i += 1 ) {\n $g = ;\n chomp ( $g );\n ($s, $e) = split m/ /, $g;\n push @arr, [ ($s, $e) ];\n}\n\n\n# # debugging\n# for ( my $i = 0; $i < @arr; $i += 1 ) {\n# print $arr[ $i ][ 0 ];\n# print \" \";\n# print $arr[ $i ][ 1 ];\n# print \"\\n\";\n# }\n# print \"\\n\\n\\n\";\n\n# sort the array by start time\nmy @arr_two = sort { ${ $a }[0] <=> ${ $b }[0] } @arr;\n\n# # debugging\n# for ( my $i = 0; $i < @arr_two; $i += 1 ) {\n# print $arr_two[$i][0];\n# print \" \";\n# print $arr_two[$i][1];\n# print \"\\n\";\n# }\n\n\n# value represents whether we can schedule\n# n shows on two tvs, without overlap\nmy $works = 1;\n\n# keeps track of latest show end\nmy $tv_one = -1;\nmy $tv_two = -1;\n\n# use for iterating through array, temporary variable\nmy $beg, $end;\n\n# iterate through the array, assigning shows to tvs one and two\nwhile ( (@arr_two > 0) and $works ) {\n\n # remove one slice from array\n # one slice is one show\n my $ref;\n $ref = shift @arr_two;\t\n $beg = ${ $ref }[0];\n $end = ${ $ref }[1];\n\n # assign a show to a tv if it begins after\n # the last one ended\n if ( $beg > $tv_one ) {\n\t$tv_one = $end; }\n elsif ( $beg > $tv_two ) {\n\t$tv_two = $end; }\n else {\n\t$works = 0;}\t\t\n # if it can't fit on either tv\n # then we will print NO\n\n}\n\n# print answer\nif ( $works ) {\n print \"YES\\n\";}\nelse {\n print \"NO\\n\"; }\n \n\n\n\n"}], "negative_code": [{"source_code": "\n\n\n\n# declare two tv arrays\nmy @arr;\n\n# read n\nmy $f = ;\nchomp ( $f );\n\nmy $s, $e;\n# read show intervals\nfor (1..$f) {\n $f = ;\n chomp ( $f );\n ($s, $e) = split m/ /, $f;\n push @arr, [ ($s, $e) ];\n}\n\n\n# sort the array by start time\nmy @arr_two = sort { $a[0] <=> $b[0] } @arr;\n\n# value represents whether we can schedule\n# n shows on two tvs, without overlap\nmy $works = 1;\n\n# keeps track of latest show end\nmy $tv_one = -1;\nmy $tv_two = -1;\n\n# use for iterating through array, temporary variable\nmy $beg, $end;\n\n# iterate through the array, assigning shows to tvs one and two\nwhile ( (@arr_two > 0) and $works ) {\n\n # remove one slice from array\n # one slice is one show\n my $ref;\n $ref = shift @arr_two;\t\n $beg = ${ $ref }[0];\n $end = ${ $ref }[1];\n\n # assign a show to a tv if it begins after\n # the last one ended\n if ( $beg > $tv_one ) {\n\t$tv_one = $end; }\n elsif ( $beg > $tv_two ) {\n\t$tv_two = $end; }\n else {\n\t$works = 0;}\t\t\n # if it can't fit on either tv\n # then we will print NO\n\n}\n\n# print answer\nif ( $works ) {\n print \"YES\\n\";}\nelse {\n print \"NO\\n\"; }\n \n\n\n\n"}], "src_uid": "9fa772b53e655a55f2ec502bc96d0e28"} {"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": "($a, $b, $_) = <>; print eval qq( tr{$a\\U$a\\E} {$b\\U$b\\E}r )"}], "negative_code": [], "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!\"\u00a0\u2014 thought the girl\u00a0\u2014 \"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\u00a0\u2014 all letters are 'a'."}, "positive_code": [{"source_code": "print <> =~ s/a+|b++(?!$)/ 0 x ( -1 + length $& ) . 1 /egr =~ y/b/0/r =~ s/\\B/ /gr"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $length = length;\n\t\n\t$debug and print '-' x 15;\n\t$debug and print;\n\t\n\tmy @ans;\n\t\n\tmy $len = 0;\n\t\n\ts/^a*b+(?=a)// and do {\n\t\t$len += length $&;\n\t\tpush @ans, $len;\n\t\t};\n\t\t\n\twhile( /a/ ){\n\t\ts/a+// and do {\n\t\t\t$len += length $&;\n\t\t\tpush @ans, $len;\n\t\t\t};\n\t\t\n\t\ts/b+(?=a)// and do {\n\t\t\t$len += length $&;\n\t\t\tpush @ans, $len;\n\t\t\t};\n\t\t}\n\t\n\t$debug and print \"@ans\";\n\t\n\tmy @ANS = ( 0 ) x $length;\n\t\n\tfor( @ans ){\n\t\t$ANS[ $_ - 1 ] = 1;\n\t\t}\n\t\t\n\tprint \"@ANS\";\n\t}"}, {"source_code": "print <> =~ s/(.)(?!\\1)\\B|a$/1/gr =~ y/ab/00/r =~ s/\\B/ /gr"}], "negative_code": [{"source_code": "print <> =~ s/(.)\\1*(?){\n\tchomp;\n\t\n\t@_ = ();\n\t\n\tmy $q = 0;\n\t\n\twhile( 1 < length ){\n\t\tmy $ch = chop;\n\t\tmy $front = substr $_, 0, 1;\n\t\t\n\t\tif( 0 ){ ; }\n\t\telsif( $front eq 'b' and $ch eq 'b' ){\n\t\t\tpush @_, 0;\n\t\t\t}\n\t\telsif( $front eq 'a' and $ch eq 'b' ){\n\t\t\tif( $q ){\n\t\t\t\tpush @_, 1;\n\t\t\t\t$_ = reverse( $_ . $ch );\n\t\t\t\tchop;\n\t\t\t\t$q = 0;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tpush @_, 0;\n\t\t\t\t}\n\t\t\t}\n\t\telsif( $front eq 'b' and $ch eq 'a' ){\n\t\t\tpush @_, 1;\n\t\t\t$_ = reverse( $_ . $ch );\n\t\t\tchop;\n\t\t\t}\n\t\telsif( $front eq 'a' and $ch eq 'a' ){\n\t\t\tpush @_, $q ? 0 : '?';\n\t\t\t$q ||= 1;\n\t\t\t}\n\t\t}\n\t\n\tmy $last = $_;\n\t\n\tpush @_, 0;\n\t@_ = reverse @_;\n#%\tprint \"@_\";\n\t\n\tmy $one = 0;\n\t\n\tfor( @_ ){\n\t\t/\\?/ and $one and do {\n\t\t\t$_ = 1;\n\t\t\t};\n\t\t/\\?/ and $one = 1;\n\t\t}\n\t\n\tfor( @_ ){\n\t\t/\\?/ and do {\n\t\t\t$_ = $last eq 'b' ? 1 : 0;\n\t\t\tlast;\n\t\t\t};\n\t\t}\n\t\n\tprint \"@_\";\n\t}"}], "src_uid": "fc0d3b122d800dcbcb99795581229d42"} {"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\u00a0\u2014 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\u00a0\u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000, 0\u2009\u2264\u2009m\u2009\u2264\u2009200\u2009000, 1\u2009\u2264\u2009k\u2009\u2264\u2009200\u2009000)\u00a0\u2014 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\u2009\u2264\u2009ci\u2009\u2264\u2009k)\u00a0\u2014 current colors of Arseniy's socks. Each of the following m lines contains two integers li and ri (1\u2009\u2264\u2009li,\u2009ri\u2009\u2264\u2009n, li\u2009\u2260\u2009ri)\u00a0\u2014 indices of socks which Arseniy should wear during the i-th day.", "output_spec": "Print one integer\u00a0\u2014 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": "#use strict;\n#use warnings;\n#use Data::Dumper;\n$,=\" \";\nmy ($n,$m,$k)=split / /,<>;\nchomp($n,$m,$k);\nmy @c=split / /,<>;\nchomp(@c);\nmy %socks;\nmy %visited;\nmy %cc;\nwhile($m--)\n{\n$_=<>;\nchomp;\nmy @t=split / /;\nchomp(@t);\npush(@{$socks{$t[0]}},$t[1]);\npush(@{$socks{$t[1]}},$t[0]);\n}\n\nmy $ans=0;\nfor my $key (keys %socks)\n{\n if(!$visited{$key})\n {\n %cc=();\n DFS($key);\n #print Dumper(\\%cc) . \"\\n\";\n my $i=1;\n for my $tv (sort {$cc{$b} <=> $cc{$a}} keys %cc)\n {\n if($i)\n {\n $i--;\n next;\n }\n $ans+=$cc{$tv};\n }\n }\n\n}\n\nprint $ans . \"\\n\";\n\n\n\n\nsub DFS\n{\n my $v=shift;\n if(!$visited{$v})\n {\n $visited{$v}=1;\n $cc{$c[$v-1]}++;\n\n }\n for my $tt (@{$socks{$v}})\n {\n if(!$visited{$tt})\n {\n DFS($tt);\n }\n }\n}\n"}], "negative_code": [{"source_code": "$,=\" \";\nmy ($n,$m,$k)=split / /,<>;\nchomp($n,$m,$k);\nmy @c=split / /,<>;\nchomp(@c);\nmy %socks;\nmy %counts;\nwhile($m--)\n{\n$_=<>;\nchomp;\nmy @t=split / /;\nchomp(@t);\n@t=map {$c[$_-1]} @t;\npush(@{$socks{$t[0]}},$t[1]);\n$counts{$t[0]}++;\n$counts{$t[1]}++;\n}\nmy %color=map {$_ => $_ } @c;\nmy $count=0;\nfor my $key (sort { $counts{$a} <=> $counts{$b} } keys %counts)\n{\n if(defined $socks{$key})\n {\n for(my $i=0;$i[$i]} == $color{$key})\n {\n next;\n }\n else\n {\n $count++;\n $color{$socks{$key}->[$i]}=$color{$key};\n }\n }\n }\n}\n\nprint $count . \"\\n\";\n"}, {"source_code": "#use strict;\n#use warnings;\n#use Data::Dumper;\n$,=\" \";\nmy ($n,$m,$k)=split / /,<>;\nchomp($n,$m,$k);\nmy @c=split / /,<>;\nchomp(@c);\nmy %socks;\nmy %counts;\nwhile($m--)\n{\n$_=<>;\nchomp;\nmy @t=split / /;\nchomp(@t);\n#@t=map {$c[$_-1]} @t;\npush(@{$socks{$t[0]}},$t[1]);\n$counts{$t[0]}++;\n$counts{$t[1]}++;\n}\n#print Dumper(\\%counts) . \"\\n\";\n#print Dumper(\\%socks) . \"\\n\";\nmy $count=0;\nfor my $key (sort { $counts{$a} <=> $counts{$b} } keys %counts)\n{\n if(defined $socks{$key})\n {\n for(my $i=0;$i[$i]-1];\n my $sc=$c[$key-1];\n if($fc == $sc)\n {\n next;\n }\n else\n {\n if($counts{$fc}>$counts{$sc})\n {\n $c[$key-1]=$c[$socks{$key}->[$i]-1];\n $counts{$fc}++;\n }\n else\n {\n $c[$socks{$key}->[$i]-1]=$c[$key-1];\n $counts{$sc}++;\n }\n $count++;\n }\n }\n }\n}\n\nprint $count . \"\\n\";\n"}, {"source_code": "#use strict;\n#use warnings;\n#use Data::Dumper;\n$,=\" \";\nmy ($n,$m,$k)=split / /,<>;\nchomp($n,$m,$k);\nmy @c=split / /,<>;\nchomp(@c);\nmy %socks;\nmy %counts;\nwhile($m--)\n{\n$_=<>;\nchomp;\nmy @t=split / /;\nchomp(@t);\n#@t=map {$c[$_-1]} @t;\npush(@{$socks{$t[0]}},$t[1]);\n$counts{$t[0]}++;\n$counts{$t[1]}++;\n}\n#print Dumper(\\%counts) . \"\\n\";\n#print Dumper(\\%socks) . \"\\n\";\nmy $count=0;\nfor my $key (sort { $counts{$a} <=> $counts{$b} } keys %counts)\n{\n if(defined $socks{$key})\n {\n for(my $i=0;$i[$i]-1] == $c[$key-1])\n {\n next;\n }\n else\n {\n $c[$socks{$key}->[$i]-1] = $c[$key-1];\n $count++;\n }\n }\n }\n}\n\nprint $count . \"\\n\";\n"}, {"source_code": "#use strict;\n#use warnings;\n#use Data::Dumper;\n$,=\" \";\nmy ($n,$m,$k)=split / /,<>;\nchomp($n,$m,$k);\nmy @c=split / /,<>;\nchomp(@c);\nmy %socks;\nmy %counts;\nwhile($m--)\n{\n$_=<>;\nchomp;\nmy @t=split / /;\nchomp(@t);\n@t=map {$c[$_-1]} @t;\npush(@{$socks{$t[0]}},$t[1]);\n$counts{$t[0]}++;\n$counts{$t[1]}++;\n}\n#print Dumper(\\%counts) . \"\\n\";\n#print Dumper(\\%socks) . \"\\n\";\nmy $count=0;\nfor my $key (sort { $counts{$a} <=> $counts{$b} } keys %counts)\n{\n if(defined $socks{$key})\n {\n for(my $i=0;$i[$i] == $key)\n {\n next;\n }\n else\n {\n $count++;\n }\n }\n }\n}\n\nprint $count . \"\\n\";\n"}, {"source_code": "#use strict;\n#use warnings;\n#use Data::Dumper;\n$,=\" \";\nmy ($n,$m,$k)=split / /,<>;\nchomp($n,$m,$k);\nmy @c=split / /,<>;\nchomp(@c);\nmy %socks;\nmy %counts;\nwhile($m--)\n{\n$_=<>;\nchomp;\nmy @t=split / /;\nchomp(@t);\n#@t=map {$c[$_-1]} @t;\npush(@{$socks{$t[0]}},$t[1]);\n$counts{$t[0]}++;\n$counts{$t[1]}++;\n}\n#print Dumper(\\%counts) . \"\\n\";\n#print Dumper(\\%socks) . \"\\n\";\nmy $count=0;\nfor my $key (sort { $counts{$a} <=> $counts{$b} } keys %counts)\n{\n if(defined $socks{$key})\n {\n for(my $i=0;$i[$i]-1] == $c[$key-1])\n {\n next;\n }\n else\n {\n $c[$socks{$key}->[$i]-1] = $c[$key-1-0];\n $count++;\n }\n }\n }\n}\n\nprint $count . \"\\n\";\n"}], "src_uid": "39232c03c033da238c5d1e20e9595d6d"} {"nl": {"description": "Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him \u2014 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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tchomp;\n\t\n\tif( 1 == length ){\n\t\tprint for \"YES\", join '', 'a' .. 'z';\n\t\tnext;\n\t\t}\n\t\n\t@_ = split '';\n\t\n\tmy $alphabet = shift @_;\n\t\n\t$alphabet = join \"<$alphabet>\", ( ',' ) x 2;\n\t\n\t$debug and print \"[$alphabet]\";\n\t\n\tmy $fail = 0;\n\t\n\tfor( @_ ){\n\t\t$debug and print \" [$alphabet], \\$_:[$_]\";\n\t\t\n\t\t$alphabet =~ /(.)\\<(.)\\>(.)/;\n\t\t\n\t\tif( 0 ){ ; }\n\t\telsif( $_ eq $1 ){\n\t\t\t$alphabet =~ s/(.)\\<(.)\\>(.)/\\<$_\\>$2$3/;\n\t\t\t}\n\t\telsif( $_ eq $3 ){\n\t\t\t$alphabet =~ s/(.)\\<(.)\\>(.)/$1$2\\<$_\\>/;\n\t\t\t}\n\t\telsif( ',' eq $1 ){\n\t\t\t$alphabet =~ s/(.)\\<(.)\\>(.)/\\<$_\\>$2$3/;\n\t\t\t}\n\t\telsif( ',' eq $3 ){\n\t\t\t$alphabet =~ s/(.)\\<(.)\\>(.)/$1$2\\<$_\\>/;\n\t\t\t}\n\t\telse{\n\t\t\t$fail = 1;\n\t\t\tlast;\n\t\t\t}\n\t\t\n\t\tif( $alphabet =~ /([a-z]).*\\1/ ){\n\t\t\t$fail = 1;\n\t\t\tlast;\n\t\t\t}\n\t\t\n\t\t$alphabet = \",$alphabet,\";\n\t\t$alphabet =~ s/,,/,/g;\n\t\t}\n\t\n\t$alphabet =~ s/[,<>]//g;\n\t\n\tif( $fail ){\n\t\tprint \"NO\";\n\t\t}\n\telse{\n\t\tprint for \"YES\", $alphabet . ( join '', 'a' .. 'z' ) =~ s/[$alphabet]//gr;\n\t\t}\n\t}"}, {"source_code": "$\\ = $/;\n\n<>;\n\nwhile(<>){\t\n\t@_ = ( 0 ) x 53;\n\t\n\t$p = 26;\n\t\n\t/./g;\n\t$_[ $p ] = $&;\n\t\n\t$F = 0;\n\t\n\twhile( /./g ){\n\t\t\t\t\n\t\tif( 0 ){ ; }\n\t\telsif( $_[ $p - 1 ] eq $& ){\n\t\t\t$p --;\n\t\t\t}\n\t\telsif( $_[ $p + 1 ] eq $& ){\n\t\t\t$p ++;\n\t\t\t}\n\t\telsif( grep { $_ eq $& } @_ ){\n\t\t\t$F = 1;\n\t\t\t}\n\t\telsif( $_[ $p - 1 ] eq 0 ){\n\t\t\t$_[ -- $p ] = $&;\n\t\t\t}\n\t\telsif( $_[ $p + 1 ] eq 0 ){\n\t\t\t$_[ ++ $p ] = $&;\n\t\t\t}\n\t\telse{\n\t\t\t$F = 1;\n\t\t\t}\n\t\t\n\t\tlast if $F;\n\t\t}\n\t\n\t$_ = join '', @_;\n\ts/^0+|0+$//g;\n\t\n\tif( $F ){\n\t\tprint \"NO\";\n\t\t}\n\telse{\n\t\tprint for \"YES\", $_ . ( join '', 'a' .. 'z' ) =~ s/[$_]//gr;\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tchomp;\n\t\n\tif( 1 == length ){\n\t\tprint for \"YES\", join '', 'a' .. 'z';\n\t\tnext;\n\t\t}\n\t\n\tmy %h;\n\t\n\t/\n\t\t(.)\n\t\t(?=(.))\n\t\t(?{ $h{ join '', sort $1, $2 } ++ })\n\t\t(*FAIL)\n\t\t/x;\n\t\n\t$debug and print \":$_ --> [$h{ $_ }]\" for sort keys %h;\n\t\n\t@_ = map { [ split '' ] } sort keys %h;\n\t\n\tmy $alphabet = join '', @{ shift @_ };\n\t\n\tmy %alphabet;\n\t\n\t$alphabet{ $_ } = 0 for 'a' .. 'z';\n\t\n\tmap { $alphabet{ $_ } ++ } split '', $alphabet;\n\t\n\tmy $j = 26 ** 1 + 2;\n\t\n\twhile( @_ ){\n\t\t$j -- < 0 and last;\n\t\t\n\t\tmy $try = shift @_;\n\t\t\n\t\tif( 1 != $alphabet{ $try->[ 0 ] } + $alphabet{ $try->[ 1 ] } ){\n\t\t\tpush @_, $try;\n\t\t\tnext;\n\t\t\t}\n\t\t\n\t\tmy $fail = 0;\n\t\t\n\t\tif( 0 ){ ; }\n\t\telsif( $alphabet =~ /^$try->[ 0 ]/ ){\n\t\t\t$alphabet = $try->[ 1 ] . $alphabet;\n\t\t\t$alphabet{ $try->[ 1 ] } ++;\n\t\t\t}\n\t\telsif( $alphabet =~ /^$try->[ 1 ]/ ){\n\t\t\t$alphabet = $try->[ 0 ] . $alphabet;\n\t\t\t$alphabet{ $try->[ 0 ] } ++;\n\t\t\t}\n\t\telsif( $alphabet =~ /$try->[ 0 ]$/ ){\n\t\t\t$alphabet .= $try->[ 1 ];\n\t\t\t$alphabet{ $try->[ 1 ] } ++;\n\t\t\t}\n\t\telsif( $alphabet =~ /$try->[ 1 ]$/ ){\n\t\t\t$alphabet .= $try->[ 0 ];\n\t\t\t$alphabet{ $try->[ 0 ] } ++;\n\t\t\t}\n\t\telse{\n\t\t\t$fail = 1;\n\t\t\t}\n\t\t\n\t\tif( $fail ){\n\t\t\tpush @_, $try;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"alphabet:[$alphabet]|\\@_:[@_]|j:[$j]\";\n\t\n\tif( $j == -2 ){\n\t\tprint \"NO\";\n\t\t}\n\telse{\n\t\tprint for \"YES\", $alphabet . ( join '', 'a' .. 'z' ) =~ s/[$alphabet]//gr;\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tchomp;\n\t\n\tif( 1 == length ){\n\t\tprint for \"YES\", join '', 'a' .. 'z';\n\t\tnext;\n\t\t}\n\t\n\tmy %h;\n\t\n\t/\n\t\t(.)\n\t\t(?=(.))\n\t\t(?{ $h{ join '', sort $1, $2 } ++ })\n\t\t(*FAIL)\n\t\t/x;\n\t\n\t$debug and print \":$_ --> [$h{ $_ }]\" for sort keys %h;\n\t\n\t@_ = map { [ split '' ] } sort keys %h;\n\t\n\tmy $alphabet = join '', @{ shift @_ };\n\t\n\tmy %alphabet;\n\t\n\t$alphabet{ $_ } = 0 for 'a' .. 'z';\n\t\n\tmap { $alphabet{ $_ } ++ } split '', $alphabet;\n\t\n\tmy $j = 26 ** 1 + 2;\n\t\n\twhile( @_ ){\n\t\t$j -- < 0 and last;\n\t\t\n\t\tmy $try = shift @_;\n\t\t\n\t\tif( 2 == $alphabet{ $try->[ 0 ] } + $alphabet{ $try->[ 1 ] } ){\n\t\t\t$j = -2;\n\t\t\tlast;\n\t\t\t}\n\t\t\n\t\tif( 0 == $alphabet{ $try->[ 0 ] } + $alphabet{ $try->[ 1 ] } ){\n\t\t\tpush @_, $try;\n\t\t\tnext;\n\t\t\t}\n\t\t\n\t\tmy $fail = 0;\n\t\t\n\t\tif( 0 ){ ; }\n\t\telsif( $alphabet =~ /^$try->[ 0 ]/ ){\n\t\t\t$alphabet = $try->[ 1 ] . $alphabet;\n\t\t\t$alphabet{ $try->[ 1 ] } ++;\n\t\t\t}\n\t\telsif( $alphabet =~ /^$try->[ 1 ]/ ){\n\t\t\t$alphabet = $try->[ 0 ] . $alphabet;\n\t\t\t$alphabet{ $try->[ 0 ] } ++;\n\t\t\t}\n\t\telsif( $alphabet =~ /$try->[ 0 ]$/ ){\n\t\t\t$alphabet .= $try->[ 1 ];\n\t\t\t$alphabet{ $try->[ 1 ] } ++;\n\t\t\t}\n\t\telsif( $alphabet =~ /$try->[ 1 ]$/ ){\n\t\t\t$alphabet .= $try->[ 0 ];\n\t\t\t$alphabet{ $try->[ 0 ] } ++;\n\t\t\t}\n\t\telse{\n\t\t\t$fail = 1;\n\t\t\t}\n\t\t\n\t\tif( $fail ){\n\t\t\tpush @_, $try;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"alphabet:[$alphabet]|\\@_:[@_]|j:[$j]\";\n\t\n\tif( $j == -2 ){\n\t\tprint \"NO\";\n\t\t}\n\telse{\n\t\tprint for \"YES\", $alphabet . ( join '', 'a' .. 'z' ) =~ s/[$alphabet]//gr;\n\t\t}\n\t}"}], "src_uid": "8fb62b497b6fb2a5fb4f2669aeb51b73"} {"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\u2009\u2264\u2009|s|\u2009\u2264\u2009105). 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": "#!perl \n$\\ = \"\\n\";\n\n$line = <>;\nchomp $line;\n\n@letters = split //, $line;\n\n$filler = \"~\";\npush @letters, $filler if @letters[$#letters] eq \",\" or @letters[$#letters] eq \";\";\n@letters = ($filler, @letters) if @letters[0] eq \",\" or @letters[0] eq \";\";\n\nfor (my $i = 0; $i <= $#letters; $i++)\n{\n my $word = \"\";\n my $char_flag = 0;\n my $zero_flag = 0;\n \n $zero_flag = 1 if @letters[$i] eq '0'; \n \n while ($i <= $#letters and ',' ne @letters[$i] and ';' ne @letters[$i])\n {\n $char_flag = 1 if @letters[$i] gt '9' or @letters[$i] eq '.'; \n $word .= @letters[$i];\n $i++;\n }\n \n if($char_flag == 1 or $word eq \"\" or $zero_flag == 1 and length $word > 1)\n {\n $b .= $word.',';\n }\n else\n {\n $a .= $word.',';\n }\n}\n\nchop $a;\nchop $b;\n\n$a = '-' if $a eq \"\";\n$b = '-' if $b eq \"\";\n\n$b =~ s/$filler//g;\n\nprint $a eq \"-\" ? '-' : qq(\"$a\");\nprint $b eq \"-\" ? '-' : qq(\"$b\");"}, {"source_code": "$\\ = $/;\n\n$r = qr/^0$|^[1-9]\\d*$/;\n\nwhile(<>){\n\tchomp;\n\t@_ = split /[,;]/, $_, -1;\n\tw( grep /$r/, @_ );\n\tw( grep !/$r/, @_ );\n\t}\n\t\nsub w { print !@_ ? '-' : map qq[\"$_\"], join ',', @_ }"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t@_ = split /[,;]/, $_, -1;\n\t@nums = grep /^0$|^[1-9]\\d*$/, @_;\n\t@other = grep !/^0$|^[1-9]\\d*$/, @_;\n\t\n\tprint @nums ? (map qq[\"$_\"], join ',', @nums) : '-';\n\tprint @other ? (map qq[\"$_\"], join ',', @other) : '-';\n\t}"}, {"source_code": "$s = <>; chomp $s;\n@t = split /,|;/, $s, -1;\nforeach $t (@t) {\n\tif ($t eq \"0\" || $t =~ /^[1-9]\\d*$/) {\n\t\tpush @a, $t;\n\t} else {\n\t\tpush @b, $t;\n\t}\n}\nsub out {\n\tmy $a = shift;\n\tprint @$a? chr(34).join(\",\", @$a).chr(34): \"-\", \"\\n\";\n}\n\nout \\@a;\nout \\@b;"}], "negative_code": [{"source_code": "#!perl \n$\\ = \"\\n\";\n\n$line = <>;\nchomp $line;\n\n@letters = split //, $line;\n\n$end_filler = \"~\";\npush @letters, $end_filler if @letters[$#letters] eq \",\" or @letters[$#letters] eq \";\";\n\nfor (my $i = 0; $i <= $#letters; $i++)\n{\n my $word = \"\";\n my $char_flag = 0;\n my $zero_flag = 0;\n \n $zero_flag = 1 if @letters[$i] eq '0'; \n \n while ($i <= $#letters and ',' ne @letters[$i] and ';' ne @letters[$i])\n {\n $char_flag = 1 if @letters[$i] gt '9' or @letters[$i] eq '.'; \n $word .= @letters[$i];\n $i++;\n }\n \n if($char_flag == 1 or $word eq \"\" or $zero_flag == 1 and length $word > 1)\n {\n $b .= $word.',';\n }\n else\n {\n $a .= $word.',';\n }\n}\n\nchop $a;\nchop $b;\n\nprint $a eq \"\" ? '-' : qq(\"$a\");\nprint $b eq \"\" ? '-' : $b =~ m/(.*)$end_filler$/ ? qq(\"$1\") : qq(\"$b\");"}, {"source_code": "while(<>){\n\tchomp;\n\t@_ = split /[,;]/;\n\tprint map s/\"\"/-/r, map qq[\"$_\"\\n], join ',', grep /^0$|^[1-9]\\d*$/, @_;\n\tprint map s/\"\"/-/r, map qq[\"$_\"\\n], join ',', grep !/^0$|^[1-9]\\d*$/, @_;\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t@_ = split /[,;]/, $_, -1;\n\tprint map s/\"\"/-/r, map qq[\"$_\"\\n], join ',', grep /^0$|^[1-9]\\d*$/, @_;\n\tprint map s/\"\"/-/r, map qq[\"$_\"\\n], join ',', grep !/^0$|^[1-9]\\d*$/, @_;\n\t}"}, {"source_code": "$s = <>; chomp $s;\n@t = split /,|;/, $s, -1;\nforeach $t (@t) {\n\tif ($t eq \"0\" || $t =~ /^[1-9]\\d*$/) {\n\t\tpush @a, $t;\n\t} else {\n\t\tpush @b, $t;\n\t}\n}\nprint @a? join(\",\", @a): \"-\", \"\\n\";\nprint @b? join(\",\", @b): \"-\", \"\\n\";"}, {"source_code": "#!perl \n$\\ = \"\\n\";\n\n$line = <>;\nchomp $line;\n\n@words = split /;|,/, $line;\n\nfor my $word (@words)\n{\n if($word =~ m/^(([1-9]+\\d*)|0)$/)\n {\n push @as, $word;\n }\n else\n {\n push @bs, $word;\n }\n}\n\npush @bs, \"\" if $line =~ m/^.*,$/;\n\n$a = scalar @as > 0 ? join ',', @as : '-';\n$b = scalar @bs > 0 ? join ',', @bs : '-';\n\nprint $a;\nprint $b;"}, {"source_code": "#!perl \n$\\ = \"\\n\";\n\n$line = <>;\nchomp $line;\n\n@words = split /;|,/, $line;\n\nfor my $word (@words)\n{\n if($word =~ m/^(([1-9]+\\d*)|0)$/)\n {\n push @as, $word;\n }\n else\n {\n push @bs, $word;\n }\n}\n\npush @bs, \"\" if $line =~ m/^.*,$/;\n\n$a = scalar @as > 0 ? join ',', @as : '-';\n$b = scalar @bs > 0 ? join ',', @bs : '-';\n\n$a eq '-' ? print '-' : print \"\\\"$a\\\"\";\n$b eq '-' ? print '-' : print \"\\\"$b\\\"\";"}, {"source_code": "#!perl \n$\\ = \"\\n\";\n\n$line = <>;\nchomp $line;\n\n@letters = split //, $line;\n\nfor (my $i = 0; $i <= $#letters; $i++)\n{\n my $word = \"\";\n my $char_flag = 0;\n my $zero_flag = 0;\n \n $zero_flag = 1 if @letters[$i] eq '0'; \n \n while ($i <= $#letters and ',' ne @letters[$i] and ';' ne @letters[$i])\n {\n $char_flag = 1 if @letters[$i] gt '9' or @letters[$i] eq '.'; \n $word .= @letters[$i];\n $i++;\n }\n \n if($char_flag == 1 or $word eq \"\" or $zero_flag == 1 and length $word > 1)\n {\n $b .= $word.',';\n }\n else\n {\n $a .= $word.',';\n }\n}\n\nchop $a;\nchop $b;\n\n$b .= \",\" if @letters[$#letters] eq \",\" or @letters[$#letters] eq \";\";\n\nprint $a eq \"\" ? '-' : qq(\"$a\");\nprint $b eq \"\" ? '-' : $b eq \",\" ? qq(\"\") : qq(\"$b\");"}, {"source_code": "#!perl \n$\\ = \"\\n\";\n\n$line = <>;\nchomp $line;\n\n@letters = split //, $line;\n\nfor (my $i = 0; $i <= $#letters; $i++)\n{\n my $word = \"\";\n my $char_flag = 0;\n my $zero_flag = 0;\n \n $zero_flag = 1 if @letters[$i] eq '0'; \n \n while ($i <= $#letters and ',' ne @letters[$i] and ';' ne @letters[$i])\n {\n $char_flag = 1 if @letters[$i] gt '9' or @letters[$i] eq '.'; \n $word .= @letters[$i];\n $i++;\n }\n \n if($char_flag == 1 or $word eq \"\" or $zero_flag == 1 and length $word > 1)\n {\n $b .= $word.',';\n }\n else\n {\n $a .= $word.',';\n }\n}\n\nchop $a;\nchop $b;\n\n$b .= \",\" if @letters[$#letters] eq \",\" or @letters[$#letters] eq \";\";\n\nprint $a eq \"\" ? '-' : qq(\"$a\");\nprint $b eq \"\" ? '-' : qq(\"$b\");"}, {"source_code": "#!perl \n$\\ = \"\\n\";\n\n$line = <>;\nchomp $line;\n\n@words = split /;|,/, $line;\n\nfor my $word (@words)\n{\n if($word =~ m/^(([1-9]+\\d*)|0)$/)\n {\n push @as, $word;\n }\n else\n {\n push @bs, $word;\n }\n}\n\npush @bs, \"\" if $line =~ m/^.*,$/;\n\n$a = scalar @as > 0 ? join ',', @as : '-';\n$b = scalar @bs > 0 ? join ',', @bs : '-';\n\nprint \"\\\"$a\\\"\";\nprint \"\\\"$b\\\"\";"}, {"source_code": "#!perl \n$\\ = \"\\n\";\n\n$line = <>;\nchomp $line;\n\n@letters = split //, $line;\n\nfor (my $i = 0; $i <= $#letters; $i++)\n{\n my $word = \"\";\n my $char_flag = 0;\n \n $zero_flag = 1 if @letters[$i] eq '0'; \n \n while ($i <= $#letters and ',' ne @letters[$i] and ';' ne @letters[$i])\n {\n $char_flag = 1 if @letters[$i] gt '9' or @letters[$i] eq '.'; \n $word .= @letters[$i];\n $i++;\n }\n \n if($char_flag == 1 or $word eq \"\" or $zero_flag == 1 and length $word > 1)\n {\n $b .= $word.',';\n }\n else\n {\n $a .= $word.',';\n }\n}\n\nchop $a;\nchop $b;\n\n$b .= \",\" if @letters[$#letters] eq \",\" or @letters[$#letters] eq \";\";\n\nprint $a eq \"\" ? '-' : qq(\"$a\");\nprint $b eq \"\" ? '-' : qq(\"$b\");"}, {"source_code": "#!perl \n$\\ = \"\\n\";\n\n$line = <>;\nchomp $line;\n\n@letters = split //, $line;\n\nfor (my $i = 0; $i <= $#letters; $i++)\n{\n my $word = \"\";\n my $char_flag = 0;\n \n $zero_flag = 1 if @letters[$i] eq '0'; \n \n while ($i <= $#letters and ',' ne @letters[$i] and ';' ne @letters[$i])\n {\n $char_flag = 1 if @letters[$i] gt '9'; \n $word .= @letters[$i];\n $i++;\n }\n \n if($char_flag == 1 or $word eq \"\" or $zero_flag == 1 and length $word > 1)\n {\n $b .= $word.',';\n }\n else\n {\n $a .= $word.',';\n }\n}\n\nchop $a;\nchop $b;\n\n$b .= \",\" if @letters[$#letters] eq \",\" or @letters[$#letters] eq \";\";\n\nprint $a eq \"\" ? '-' : qq(\"$a\");\nprint $b eq \"\" ? '-' : qq(\"$b\");"}], "src_uid": "ad02cead427d0765eb642203d13d3b99"} {"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$$$)\u00a0\u2014 the number of test cases. Each test case starts with a line containing two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 5$$$)\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n \r\n my ($n,$m) = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my $t = $n+1; my $l = $m+1;\r\n my @s = ();\r\n for(my $i=0;$i<$n;$i++){\r\n ( my $s1 = ) =~ s/[\\x00-\\x20]+$//ios;\r\n for(my $j=0;$j<$m;$j++){\r\n my $c1 = substr($s1,$j,1);\r\n if( $c1 eq 'R' ){\r\n $t = &min($t,$i);\r\n $l = &min($l,$j);\r\n }\r\n }\r\n push(@s,$s1);\r\n }\r\n if( $t >= $n || $l >= $m ){\r\n print \"NO\\n\"; next; \r\n }\r\n if( substr($s[$t],$l,1) eq 'R' ){\r\n print \"YES\\n\";\r\n } else {\r\n print \"NO\\n\";\r\n }\r\n}\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "negative_code": [], "src_uid": "96b6a96ded46bddb78b118d6d3a9d049"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers.In one move, you can choose two indices $$$1 \\le i, j \\le n$$$ such that $$$i \\ne j$$$ and set $$$a_i := a_j$$$. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose $$$i$$$ and $$$j$$$ and replace $$$a_i$$$ with $$$a_j$$$).Your task is to say if it is possible to obtain an array with an odd (not divisible by $$$2$$$) sum of elements.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$$$) \u2014 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 2000$$$) \u2014 the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 2000$$$), 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 $$$2000$$$ ($$$\\sum n \\le 2000$$$).", "output_spec": "For each test case, print the answer on it \u2014 \"YES\" (without quotes) if it is possible to obtain the array with an odd sum of elements, and \"NO\" otherwise.", "sample_inputs": ["5\n2\n2 3\n4\n2 2 8 8\n3\n3 3 3\n4\n5 5 5 5\n4\n1 1 1 1"], "sample_outputs": ["YES\nNO\nYES\nNO\nNO"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tprint qw( NO YES )[ ( grep $_ % 2, @_ ) and @_ % 2 || 0 + !! grep $_ % 2 == 0, @_ ];\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tprint qw( NO YES )[ ( grep $_ % 2, @_ ) and @_ % 2 || 0 + grep !( $_ % 2 ), @_ ];\n\t}"}], "src_uid": "2e8f7f611ba8d417fb7d12fda22c908b"} {"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,\u2009y), he can move to (or attack) positions (x\u2009+\u20091,\u2009y), (x\u20131,\u2009y), (x,\u2009y\u2009+\u20091) and (x,\u2009y\u20131).Iahub wants to know how many Coders can be placed on an n\u2009\u00d7\u2009n chessboard, so that no Coder attacks any other Coder.", "input_spec": "The first line contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000).", "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": "# nEro\n$n=;\nif($n%2)\n{\n$n--,$k=$n/2,$n++;\n}\nelse{$k=$n/2;}\n$n%2?print $k**2+($k+1)**2:print $k*$n;\nfor($i=0;$i<$n;$i++)\n{\n\tprint \"\\n\";\n\tif($i%2){\n\t\t$s=\".C\";\n\t\tprint ($s x $k);\n\t\tif($n%2) {print \".\";}\n\t}\n\telse{\n\t\t$s=\"C.\";\n\t\tprint ($s x $k);\n\t\tif($n%2) {print \"C\";}\n\t}\n}"}, {"source_code": "my $n = int<>;\nmy $str = \"C.\" x int($n/2) . \"C\" x ($n%2);\nmy $str2 = \".C\" x int($n/2) . \".\" x ($n%2);\nmy $pr = $str . \"\\n\" . $str2 . \"\\n\";\nprint $n * int($n / 2) + (int($n/2)+1)*($n%2) . \"\\n\";\nprint $pr x int($n/2) . $str x ($n % 2) . \"\\n\";"}, {"source_code": "\nchomp(my $n=<>);\nmy $k = $n>>1;\nprint $k**2+($n-$k)**2,\"\\n\";\n$odd = \"C.\"x$k;\n$even = \".C\"x$k;\n$odd .= \"C\" if $n&1;\n$even .= \".\" if $n&1;\n$odd .= \"\\n\";\n$even .= \"\\n\";\nfor (1..$n) {\n\tif ($_ & 1) {\n\t\tprint $odd;\n\t} else {\n\t\tprint $even;\n\t}\t\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n$ans = (($n+1)>>1)*(($n+1)>>1) + ($n>>1)*($n>>1);\nforeach (1 .. $n) {\n\tif ($_ & 1) {\n\t\t$l1 .= 'C';\n\t\t$l2 .= '.';\n\t} else {\n\t\t$l2 .= 'C';\n\t\t$l1 .= '.';\n\t}\n}\nsay $ans;\nforeach (1 .. $n) {\n\tif ($_ & 1) {\n\t\tsay $l1;\n\t} else {\n\t\tsay $l2;\n\t}\n}"}, {"source_code": "chomp($_=<>);\n$a=(\"C.\"x($_/2)).(\"C\"x($_%2)).\"\\n\";\n$b=(\".C\"x($_/2)).(\".\"x($_%2)).\"\\n\";\n$c=($_**2 + $_**2 %2) /2;\nprint \"$c\\n\";\nfor (1..$_){\nprint $_%2 ? $a:$b\n}"}], "negative_code": [{"source_code": "# nEro\n$n=;\n$k=$n/2;\n$n%2?print $k**2+($k+1)**2:print $k*$n;\nfor($i=0;$i<$n;$i++)\n{\n\tprint \"\\n\";\n\tif($i%2){\n\t\t$s=\".C\";\n\t\tprint ($s x $k);\n\t\tif($n%2) {print \".\";}\n\t}\n\telse{\n\t\t$s=\"C.\";\n\t\tprint ($s x $k);\n\t\tif($n%2) {print \"C\";}\n\t}\n}"}, {"source_code": "my $n = int<>;\nmy $str = \"C.\" x int($n/2) . \"C\" x ($n%2);\nmy $str2 = \".C\" x int($n/2) . \".\" x ($n%2);\nmy $pr = $str . \"\\n\" . $str2 . \"\\n\";\nprint $pr x int($n/2) . $str x ($n % 2) . \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\nforeach (1 .. $n) {\n\tif ($_ & 1) {\n\t\t$l1 .= 'C';\n\t\t$l2 .= '.';\n\t} else {\n\t\t$l2 .= 'C';\n\t\t$l1 .= '.';\n\t}\n}\nforeach (1 .. $n) {\n\tif ($_ & 1) {\n\t\tsay $l1;\n\t} else {\n\t\tsay $l2;\n\t}\n}"}], "src_uid": "1aede54b41d6fad3e74f24a6592198eb"} {"nl": {"description": "Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.", "input_spec": "The first line of the input contains six positive integers x1,\u2009y1,\u2009x2,\u2009y2,\u2009x3,\u2009y3 (1\u2009\u2264\u2009x1,\u2009y1,\u2009x2,\u2009y2,\u2009x3,\u2009y3\u2009\u2264\u2009100), where xi and yi determine the length and width of the logo of the i-th company respectively.", "output_spec": "If it is impossible to place all the three logos on a square shield, print a single integer \"-1\" (without the quotes). If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters \"A\", \"B\" or \"C\". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters \"A\" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters \"B\" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters \"C\" should be equal to the sizes of the logo of the third company, Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them. See the samples to better understand the statement.", "sample_inputs": ["5 1 2 5 5 2", "4 4 2 6 4 2"], "sample_outputs": ["5\nAAAAA\nBBBBB\nBBBBB\nCCCCC\nCCCCC", "6\nBBBBBB\nBBBBBB\nAAAACC\nAAAACC\nAAAACC\nAAAACC"], "notes": null}, "positive_code": [{"source_code": "use 5.010;\nuse strict;\nuse warnings FATAL => 'all';\n\nmy @input = split / /, <>;\nmy $logos = {};\nmy $letter = 'A';\n\nwhile ( my ( $x, $y ) = splice @input, 0, 2 ) {\n @{ $logos->{ $letter++ } }{qw|x y|}\n = ( sort { $b <=> $a } $x, $y );\n}\n\nmy $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};\n\nif ( $sqrt != int($sqrt) ) {\n say -1;\n exit;\n}\n\nmy $banner = [];\nmy $n = -( scalar keys %{$logos} );\n\nwhile (1) {\n for ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n $banner->[$y][$x] = '0';\n }\n }\n for my $logo ( sort {$n} keys %{$logos} ) {\n my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};\n my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )\n or last;\n\n ( $width, $height ) = ( $height, $width ) if $rotate;\n\n for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {\n for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {\n $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;\n }\n }\n }\n if ( grep {/0/} map { @$_ ? @$_ : () } @{$banner} ) {\n if ( $n++ >= scalar keys %{$logos} ) {\n say -1;\n exit;\n }\n undef $banner;\n next;\n }\n last;\n}\n\nsay $sqrt;\nsay join q{}, @$_ for @{$banner};\n\nsub find_area {\n my ( $matrix, $width, $height ) = @_;\n my $_sqrt = scalar @{$matrix};\n\n for ( my $y = 0; $y < $_sqrt; $y++ ) {\n for ( my $x = 0; $x < $_sqrt; $x++ ) {\n my $paste = sub {\n my ( $_width, $_height ) = @_;\n for ( 0 .. $_width - 1 ) {\n return if $x + $_ >= $_sqrt;\n next if $matrix->[$y][ $x + $_ ] !~ /0/;\n }\n\n for ( 0 .. $_height - 1 ) {\n return if $y + $_ >= $_sqrt;\n next if $matrix->[ $y + $_ ][$x] !~ /0/;\n }\n return 1;\n };\n\n if ( $matrix->[$y][$x] =~ /0/ ) {\n if ( $paste->( $width, $height ) ) {\n return $x, $y;\n }\n\n if ( $paste->( $height, $width ) ) {\n return $x, $y, 1;\n }\n }\n }\n }\n return;\n}\n"}, {"source_code": "use 5.010;\nuse strict;\nuse warnings FATAL => 'all';\n\nmy @input = split / /, <>;\nmy $logos = {};\nmy $letter = 'A';\n\nwhile ( my ( $x, $y ) = splice @input, 0, 2 ) {\n @{ $logos->{ $letter++ } }{qw|x y|}\n = ( sort { $b <=> $a } $x, $y );\n}\n\nmy $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};\n\nif ( $sqrt != int($sqrt) ) {\n say -1;\n exit;\n}\n\nmy $banner = [];\nmy $n = -( scalar keys %{$logos} );\n\nwhile (1) {\n for ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n $banner->[$y][$x] = '0';\n }\n }\n for my $logo ( sort {$n} keys %{$logos} ) {\n my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};\n my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )\n or last;\n\n ( $width, $height ) = ( $height, $width ) if $rotate;\n\n for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {\n for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {\n $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;\n }\n }\n }\n if ( grep {/0/} map { @$_ ? @$_ : () } @{$banner} ) {\n if ( $n++ >= scalar keys %{$logos} ) {\n say -1;\n exit;\n }\n next;\n }\n last;\n}\n\nsay $sqrt;\nsay join q{}, @$_ for @{$banner};\n\nsub find_area {\n my ( $matrix, $width, $height ) = @_;\n my $_sqrt = scalar @{$matrix};\n\n for ( my $y = 0; $y < $_sqrt; $y++ ) {\n for ( my $x = 0; $x < $_sqrt; $x++ ) {\n my $paste = sub {\n my ( $_width, $_height ) = @_;\n for ( 0 .. $_width - 1 ) {\n return if $x + $_ >= $_sqrt;\n next if $matrix->[$y][ $x + $_ ] !~ /0/;\n }\n\n for ( 0 .. $_height - 1 ) {\n return if $y + $_ >= $_sqrt;\n next if $matrix->[ $y + $_ ][$x] !~ /0/;\n }\n return 1;\n };\n\n if ( $matrix->[$y][$x] =~ /0/ ) {\n if ( $paste->( $width, $height ) ) {\n return $x, $y;\n }\n\n if ( $paste->( $height, $width ) ) {\n return $x, $y, 1;\n }\n }\n }\n }\n return;\n}\n"}, {"source_code": "use 5.010;\nuse strict;\nuse warnings FATAL => 'all';\n\nmy @input = split / /, <>;\nmy $logos = {};\nmy $letter = 'A';\n\nwhile ( my ( $x, $y ) = splice @input, 0, 2 ) {\n @{ $logos->{ $letter++ } }{qw|x y|}\n = ( sort { $b <=> $a } $x, $y );\n}\n\nmy $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};\n\nif ( $sqrt != int($sqrt) ) {\n say -1;\n exit;\n}\n\nmy $banner = [];\nmy $n = -1;\n\nwhile (1) {\n push @{$banner}, [ (undef) x $sqrt ] for 1 .. $sqrt;\n for my $logo ( sort {$n} keys %{$logos} ) {\n my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};\n my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )\n or last;\n\n ( $width, $height ) = ( $height, $width ) if $rotate;\n\n for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {\n for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {\n $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;\n }\n }\n }\n if ( grep { !defined } map { @$_ ? @$_ : () } @{$banner} ) {\n if ( $n++ >= 1 ) {\n say -1;\n exit;\n }\n undef $banner;\n next;\n }\n last;\n}\n\nsay $sqrt;\nsay q{}, @$_ for @{$banner};\n\nsub find_area {\n my ( $matrix, $width, $height ) = @_;\n my $_sqrt = scalar @{$matrix};\n\n for ( my $y = 0; $y < $_sqrt; $y++ ) {\n for ( my $x = 0; $x < $_sqrt; $x++ ) {\n my $paste = sub {\n my ( $_width, $_height ) = @_;\n for ( 0 .. $_width - 1 ) {\n return if $x + $_ >= $_sqrt;\n next if defined $matrix->[$y][ $x + $_ ];\n }\n\n for ( 0 .. $_height - 1 ) {\n return if $y + $_ >= $_sqrt;\n next if defined $matrix->[ $y + $_ ][$x];\n }\n return 1;\n };\n\n if ( !defined $matrix->[$y][$x] ) {\n if ( $paste->( $width, $height ) ) {\n return $x, $y;\n }\n\n if ( $paste->( $height, $width ) ) {\n return $x, $y, 1;\n }\n }\n }\n }\n return;\n}\n"}, {"source_code": "#!perl\n$\\ = \"\\n\";\n\n@a = split /\\s/, <>;\n\n$side = sqrt(@a[0]*@a[1] + @a[2]*@a[3] + @a[4]*@a[5]);\n\nif($side - int($side) == 0)\n{\n for my $i (0, 2, 4)\n {\n (@a[$i], @a[$i + 1]) = @a[$i] < @a[$i + 1] ? (@a[$i], @a[$i + 1]) : (@a[$i + 1], @a[$i]);\n }\n \n if(@a[1] == @a[3] and @a[3] == @a[5] and @a[1] == @a[0] + @a[2] + @a[4])\n {\n @result = ([@a[0], @a[1], 'A'], [@a[2], @a[3], 'B'], [@a[4], @a[5], 'C']);\n $case = 1;\n }\n else\n {\n @tringles = sort {@{$a}[1] <=> @{$b}[1]} ([@a[0], @a[1], 'A'], [@a[2], @a[3], 'B'], [@a[4], @a[5], 'C']);\n \n if(@{@tringles[2]}[1] == $side)\n {\n for my $position ([0, 0, 1, 1], [0, 1, 1, 0], [1, 0, 0, 1], [1, 1, 0, 0])\n {\n (my $i, my $j, my $k, my $l) = @{$position};\n \n if(@{@tringles[0]}[$i] == @{@tringles[1]}[$j] \n and @{@tringles[0]}[$k] + @{@tringles[1]}[$l] == $side\n and @{@tringles[0]}[$i] + @{@tringles[2]}[0] == $side)\n {\n @result = (@tringles[2], \n [@{@tringles[0]}[$i], @{@tringles[0]}[$k], @{@tringles[0]}[2]], \n [@{@tringles[0]}[$j], @{@tringles[1]}[$l], @{@tringles[1]}[2]]);\n $case = 2;\n last;\n }\n }\n }\n }\n \n if(@result)\n {\n print $side;\n \n if($case == 1)\n {\n for my $triangle (@result)\n {\n for (1..@{$triangle}[0])\n {\n print @{$triangle}[2]x$side;\n }\n }\n }\n else\n {\n for (1..@{@result[0]}[0])\n {\n print @{@result[0]}[2]x$side;\n }\n \n for my $i (1..@{@result[1]}[0])\n {\n print @{@result[1]}[2]x@{@result[1]}[1].@{@result[2]}[2]x@{@result[2]}[1];\n }\n }\n }\n else\n {\n print -1;\n }\n}\nelse\n{\n print -1;\n}"}, {"source_code": "while(<>){\n\t\n\t$max = (sort {$b <=> $a} split)[0];\n\t$cmax = () = /\\b$max\\b/g;\n\t\n\t$area = eval join '+', map { eval join '*', split } @groups = /\\d+ \\d+/g;\n\t\n\t$area == $max ** 2 and $cmax =~ /[13]/ or do { print -1; next };\n\n\t$letter = 'A';\n\t\n\tprint join \"\\n\", $max, \n\t\t$cmax == 3 ? \n\t\tdo {\n\t\t\tmap { ( $letter ++ x $max ) x $_ } grep !/\\b$max\\b/, split\n\t\t\t}\n\t\t:\n\t\tdo {\n\t\t\t($wide2) = grep $max != $_, split ' ', join '', grep /\\b$max\\b/, @groups;\n\t\t\t$wide1 = $max - $wide2;\n\t\t\t\n\t\t\t$pre = join \"\\n\", map { s/ ?\\b$wide1\\b ?//; ( $letter ++ x $wide1 . 'C' x $wide2 ) x $_ } \n\t\t\t\tgrep !/\\b$max\\b/, @groups;\n\t\t\t\n\t\t\t$groups[0] =~ /\\b$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t\t$groups[1] =~ /\\b$max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t\t\n\t\t\t$pre\n\t\t}\n}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\t@s = sort {$b <=> $a} split;\n\t($max, $min) = @s[0,-1];\n\t$cmax = () = /\\b$max\\b/g;\n\n\ts/(\\d+) (\\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;\n\n\t$area = eval join '+', map { eval join '*', split } /\\d+ \\d+/g;\n\t\n\t$area == $max ** 2 or do { print -1; next };\n\n\t%hash = ();\n\tmap { ++ $hash{ $_ } } split;\n\t(join ' ', values %hash) =~ /[46]/ and do { print -1; next };\n\t\n\t$let = 'A';\n\t\n\t$cmax == 3 ? \n\tdo {\n\t\ts/\\b$max\\b//g;\n\t\t(eval join '+', split) == $max or do { print -1; next };\n\t\t\n\t\tprint join \"\\n\", $max , map { ( $let ++ x $max ) x $_ } split;\n\t\t}\n\t:\n\tdo {\n\t\t$cmax == 1 or do { print -1; next }; \n\t\t\n\t\t($bemax = $_) =~ s/\\b$max\\b (\\d+) ?//;\n\t\t$wide2 = $1;\n\t\t$wide1 = $max - $wide2;\n\t\t\n\t\t$pre = join \"\\n\", map { s/ ?\\b$wide1\\b ?//; ( $let ++ x $wide1 ) x $_ } $bemax =~ /\\d+ \\d+/g;\n\t\t\n\t\t$pre =~ s/$/ 'C' x $wide2 /gem;\n\t\t\n\t\t/^$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ $max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t\n\t\tprint join \"\\n\", $max, $pre;\n\t\n\t}\n}"}], "negative_code": [{"source_code": "use 5.010;\nuse strict;\nuse warnings FATAL => 'all';\n\nmy @input = split / /, <>;\nmy $logos = {};\nmy $letter = 'A';\n\nwhile ( my ( $x, $y ) = splice @input, 0, 2 ) {\n @{ $logos->{ $letter++ } }{qw|x y|}\n = ( sort { $b <=> $a } $x, $y );\n}\n\nmy $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};\n\nif ( $sqrt != int($sqrt) ) {\n say -1;\n exit;\n}\n\nmy $banner = [];\nmy $n = -( scalar keys %{$logos} );\n\nwhile (1) {\n for ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n $banner->[$y][$x] = '0';\n }\n }\n for my $logo ( sort { $n++ } sort keys %{$logos} ) {\n my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};\n my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )\n or last;\n\n ( $width, $height ) = ( $height, $width ) if $rotate;\n\n for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {\n for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {\n $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;\n }\n }\n }\n if ( grep {/0/} map { @$_ ? @$_ : () } @{$banner} ) {\n if ( $n >= scalar keys %{$logos} ) {\n say -1;\n exit;\n }\n next;\n }\n last;\n}\n\nsay $sqrt;\nsay join q{}, @$_ for @{$banner};\n\nsub find_area {\n my ( $matrix, $width, $height ) = @_;\n my $_sqrt = scalar @{$matrix};\n\n for ( my $y = 0; $y < $_sqrt; $y++ ) {\n for ( my $x = 0; $x < $_sqrt; $x++ ) {\n my $paste = sub {\n my ( $_width, $_height ) = @_;\n for ( 0 .. $_width - 1 ) {\n return if $x + $_ >= $_sqrt;\n next if $matrix->[$y][ $x + $_ ] !~ /0/;\n }\n\n for ( 0 .. $_height - 1 ) {\n return if $y + $_ >= $_sqrt;\n next if $matrix->[ $y + $_ ][$x] !~ /0/;\n }\n return 1;\n };\n\n if ( $matrix->[$y][$x] =~ /0/ ) {\n if ( $paste->( $width, $height ) ) {\n return $x, $y;\n }\n\n if ( $paste->( $height, $width ) ) {\n return $x, $y, 1;\n }\n }\n }\n }\n return;\n}\n"}, {"source_code": "use 5.010;\nuse strict;\nuse warnings FATAL => 'all';\n\n# my @input = split / /, <>;\nmy @input = qw(1 2 2 4 3 2);\nmy $logos = {};\nmy $letter = 'A';\n\nwhile ( my ( $x, $y ) = splice @input, 0, 2 ) {\n @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );\n}\n\nmy $sqrt = sqrt eval join q{+}, ( map { $_->{x} * $_->{y} } values %{$logos} );\nif ( $sqrt - int($sqrt) ) {\n say -1;\n exit 1;\n}\n\nmy $banner = [];\n\nfor ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n $banner->[$y][$x] = 0;\n }\n}\n\nfor my $logo ( keys %{$logos} ) {\n my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};\n my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )\n or next;\n\n ( $width, $height ) = ( $height, $width ) if $rotate;\n say $logo, $rotate//0;\n\n for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {\n for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {\n $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;\n }\n }\n}\n\nsay $sqrt;\n\nsay join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};\n\nsub find_area {\n my ( $matrix, $width, $height ) = @_;\n\n for ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n my $paste = sub {\n my ( $_width, $_height ) = @_;\n my ( $success, $width_success );\n for ( 0 .. $_width - 1 ) {\n if ( !defined $matrix->[$y][ $x + $_ ] ) {\n $width_success = 0;\n last;\n }\n next if $matrix->[$y][ $x + $_ ] ne 0;\n $width_success++;\n }\n\n if ($width_success) {\n for ( 0 .. $_height - 1 ) {\n if ( !defined $matrix->[ $y + $_ ][$x] ) {\n $success = 0;\n last;\n }\n next if $matrix->[ $y + $_ ][$x] ne 0;\n $success++;\n }\n }\n return $success;\n };\n\n if ( $matrix->[$y][$x] eq 0 ) {\n if ( $paste->( $width, $height ) ) {\n return $x, $y;\n }\n elsif ( $paste->( $height, $width ) ) {\n return $x, $y, 1;\n }\n }\n }\n }\n return;\n}\n"}, {"source_code": "use 5.010;\nuse strict;\nuse warnings FATAL => 'all';\n\nmy @input = split / /, <>;\nmy $logos = {};\nmy $letter = 'A';\n\nwhile ( my ( $x, $y ) = splice @input, 0, 2 ) {\n @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );\n}\n\nmy $sqrt = sqrt eval join q{+}, ( map { $_->{x} * $_->{y} } values %{$logos} );\nif ( $sqrt - int($sqrt) ) {\n say -1;\n exit 1;\n}\n\nmy $banner = [];\n\nfor ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n $banner->[$y][$x] = 0;\n }\n}\n\nfor my $logo ( keys %{$logos} ) {\n my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};\n my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )\n or next;\n\n ( $width, $height ) = ( $height, $width ) if $rotate;\n\n for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {\n for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {\n $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;\n }\n }\n}\n\nsay $sqrt;\n\nsay join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};\n\nsub find_area {\n my ( $matrix, $width, $height ) = @_;\n\n for ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n my $paste = sub {\n my ( $_width, $_height ) = @_;\n my ( $success, $width_success );\n for ( 0 .. $_width - 1 ) {\n if ( !defined $matrix->[$y][ $x + $_ ] ) {\n $width_success = 0;\n last;\n }\n next if $matrix->[$y][ $x + $_ ] ne 0;\n $width_success++;\n }\n\n if ($width_success) {\n for ( 0 .. $_height - 1 ) {\n if ( !defined $matrix->[ $y + $_ ][$x] ) {\n $success = 0;\n last;\n }\n next if $matrix->[ $y + $_ ][$x] ne 0;\n $success++;\n }\n }\n return $success;\n };\n\n if ( $matrix->[$y][$x] eq 0 ) {\n if ( $paste->( $width, $height ) ) {\n return $x, $y;\n }\n elsif ( $paste->( $height, $width ) ) {\n return $x, $y, 1;\n }\n }\n }\n }\n return;\n}\n"}, {"source_code": "use 5.010;\nuse strict;\nuse warnings FATAL => 'all';\n\nmy @input = split / /, <>;\nmy $logos = {};\nmy $letter = 'A';\n\nwhile ( my ( $x, $y ) = splice @input, 0, 2 ) {\n @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );\n}\nmy $sqrt = sqrt eval join q{+},\n ( map { $_->{x} * $_->{y} } values %{$logos} );\nif ( $sqrt - int($sqrt) ) {\n say -1;\n exit 1;\n}\n\nmy $banner = [];\n\nLOOP: while (1) {\n for ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n $banner->[$y][$x] = '0';\n }\n }\n\n for my $logo ( sort { int( rand(3) ) - 1 } keys %{$logos} ) {\n my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};\n my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )\n or next LOOP;\n\n ( $width, $height ) = ( $height, $width ) if $rotate;\n\n for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {\n for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {\n $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;\n }\n }\n }\n\n last;\n}\n\nsay $sqrt;\n\nsay join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};\n\nsub find_area {\n my ( $matrix, $width, $height ) = @_;\n\n for ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n my $paste = sub {\n my ( $_width, $_height ) = @_;\n my ( $success, $width_success );\n for ( 0 .. $_width - 1 ) {\n if ( !defined $matrix->[$y][ $x + $_ ] ) {\n $width_success = 0;\n last;\n }\n if ( $matrix->[$y][ $x + $_ ] ne '0' ) {\n $width_success = 0;\n next;\n }\n $width_success++;\n }\n\n if ($width_success) {\n for ( 0 .. $_height - 1 ) {\n if ( !defined $matrix->[ $y + $_ ][$x] ) {\n $success = 0;\n last;\n }\n if ( $matrix->[ $y + $_ ][$x] ne '0' ) {\n $success = 0;\n next;\n }\n $success++;\n }\n }\n return $success;\n };\n\n if ( $matrix->[$y][$x] eq '0' ) {\n if ( $paste->( $width, $height ) ) {\n return $x, $y;\n }\n\n if ( $paste->( $height, $width ) ) {\n return $x, $y, 1;\n }\n }\n }\n }\n return;\n}\n"}, {"source_code": "use 5.010;\nuse strict;\nuse warnings FATAL => 'all';\n\nmy @input = split / /, <>;\nmy $logos = {};\nmy $letter = 'A';\n\nwhile ( my ( $x, $y ) = splice @input, 0, 2 ) {\n @{ $logos->{ $letter++ } }{qw|x y|}\n = ( sort { $b <=> $a } $x, $y );\n}\n\nmy $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};\n\nif ( $sqrt != int($sqrt) ) {\n say -1;\n exit;\n}\n\nmy $banner = [];\nmy $n = -( scalar keys %{$logos} );\n\nwhile (1) {\n for ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n $banner->[$y][$x] = '0';\n }\n }\n\n for my $logo ( sort { $n++ } keys %{$logos} ) {\n my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};\n my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )\n or last;\n\n ( $width, $height ) = ( $height, $width ) if $rotate;\n\n for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {\n for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {\n $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;\n }\n }\n }\n if ( grep {/0/} map { @$_ ? @$_ : () } @{$banner} ) {\n if ( $n = scalar keys %{$logos} ) {\n say -1;\n exit;\n }\n next;\n }\n last;\n}\n\nsay $sqrt;\nsay join q{}, @$_ for @{$banner};\n\nsub find_area {\n my ( $matrix, $width, $height ) = @_;\n my $_sqrt = scalar @{$matrix};\n\n for ( my $y = 0; $y < $_sqrt; $y++ ) {\n for ( my $x = 0; $x < $_sqrt; $x++ ) {\n my $paste = sub {\n my ( $_width, $_height ) = @_;\n for ( 0 .. $_width - 1 ) {\n return if $x + $_ >= $_sqrt;\n next if $matrix->[$y][ $x + $_ ] !~ /0/;\n }\n\n for ( 0 .. $_height - 1 ) {\n return if $y + $_ >= $_sqrt;\n next if $matrix->[ $y + $_ ][$x] !~ /0/;\n }\n return 1;\n };\n\n if ( $matrix->[$y][$x] =~ /0/ ) {\n if ( $paste->( $width, $height ) ) {\n return $x, $y;\n }\n\n if ( $paste->( $height, $width ) ) {\n return $x, $y, 1;\n }\n }\n }\n }\n return;\n}\n"}, {"source_code": "use 5.010;\nuse strict;\nuse warnings FATAL => 'all';\n\nmy @input = split / /, <>;\nmy $logos = {};\nmy $letter = 'A';\n\nwhile ( my ( $x, $y ) = splice @input, 0, 2 ) {\n @{ $logos->{ $letter++ } }{qw|x y|} = sort { $b <=> $a } ( $x, $y );\n}\nmy $sqrt = sqrt eval join q{+},\n ( map { $_->{x} * $_->{y} } values %{$logos} );\nif ( $sqrt - int($sqrt) ) {\n say -1;\n exit 1;\n}\n\nmy $banner = [];\n\nfor ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n $banner->[$y][$x] = '0';\n }\n}\n\nfor my $logo (keys %{$logos}) {\n my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};\n my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )\n or next;\n\n ( $width, $height ) = ( $height, $width ) if $rotate;\n\n for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {\n for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {\n $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;\n }\n }\n}\n\nsay $sqrt;\n\nsay join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};\n\nsub find_area {\n my ( $matrix, $width, $height ) = @_;\n\n for ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n my $paste = sub {\n my ( $_width, $_height ) = @_;\n my ( $success, $width_success );\n for ( 0 .. $_width - 1 ) {\n if ( !defined $matrix->[$y][ $x + $_ ] ) {\n $width_success = 0;\n last;\n }\n if ( $matrix->[$y][ $x + $_ ] ne '0' ) {\n $width_success = 0;\n next;\n }\n $width_success++;\n }\n\n if ($width_success) {\n for ( 0 .. $_height - 1 ) {\n if ( !defined $matrix->[ $y + $_ ][$x] ) {\n $success = 0;\n last;\n }\n if ( $matrix->[ $y + $_ ][$x] ne '0' ) {\n $success = 0;\n next;\n }\n $success++;\n }\n }\n return $success;\n };\n\n if ( $matrix->[$y][$x] eq '0' ) {\n if ( $paste->( $width, $height ) ) {\n return $x, $y;\n }\n\n if ( $paste->( $height, $width ) ) {\n return $x, $y, 1;\n }\n }\n }\n }\n return;\n}\n"}, {"source_code": "use 5.010;\nuse strict;\nuse warnings FATAL => 'all';\n\nmy @input = split / /, <>;\nmy $logos = {};\nmy $letter = 'A';\n\nwhile ( my ( $x, $y ) = splice @input, 0, 2 ) {\n @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );\n}\n\nmy $sqrt = sqrt eval join q{+},\n ( map { $_->{x} * $_->{y} } values %{$logos} );\nif ( $sqrt - int($sqrt) ) {\n say -1;\n exit 1;\n}\n\nmy $banner = [];\n\nfor ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n $banner->[$y][$x] = '0';\n }\n}\n\nfor my $logo ( keys %{$logos} ) {\n my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};\n my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )\n or next;\n\n ( $width, $height ) = ( $height, $width ) if $rotate;\n\n for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {\n for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {\n $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;\n }\n }\n}\n\nsay $sqrt;\n\nsay join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};\n\nsub find_area {\n my ( $matrix, $width, $height ) = @_;\n\n for ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n my $paste = sub {\n my ( $_width, $_height ) = @_;\n my ( $success, $width_success );\n for ( 0 .. $_width - 1 ) {\n if ( !defined $matrix->[$y][ $x + $_ ] ) {\n $width_success = 0;\n last;\n }\n if ( $matrix->[$y][ $x + $_ ] ne '0' ) {\n $width_success = 0;\n next;\n }\n $width_success++;\n }\n\n if ($width_success) {\n for ( 0 .. $_height - 1 ) {\n if ( !defined $matrix->[ $y + $_ ][$x] ) {\n $success = 0;\n last;\n }\n if ( $matrix->[ $y + $_ ][$x] ne '0' ) {\n $success = 0;\n next;\n }\n $success++;\n }\n }\n return $success;\n };\n\n if ( $matrix->[$y][$x] eq 0 ) {\n if ( $paste->( $width, $height ) ) {\n return $x, $y;\n }\n\n if ( $paste->( $height, $width ) ) {\n return $x, $y, 1;\n }\n }\n }\n }\n return;\n}\n"}, {"source_code": "use 5.010;\nuse strict;\nuse warnings FATAL => 'all';\n\nmy @input = split / /, <>;\nmy $logos = {};\nmy $letter = 'A';\n\nwhile ( my ( $x, $y ) = splice @input, 0, 2 ) {\n @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );\n}\n\nmy $sqrt = sqrt sum( map { $_->{x} * $_->{y} } values %{$logos} );\nif ( $sqrt - int($sqrt) ) {\n say -1;\n exit 1;\n}\n\nmy $banner = [];\n\nfor ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n $banner->[$y][$x] = 0;\n }\n}\n\nfor my $logo ( sort keys %{$logos} ) {\n my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};\n my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )\n or next;\n\n ( $width, $height ) = ( $height, $width ) if $rotate;\n\n for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {\n for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {\n $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;\n }\n }\n}\n\nsay $sqrt;\n\nsay join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};\n\nsub sum {\n my $sum;\n $sum += $_ for @_;\n return $sum;\n}\n\nsub find_area {\n my ( $matrix, $width, $height ) = @_;\n\n for ( my $y = 0; $y < $sqrt; $y++ ) {\n for ( my $x = 0; $x < $sqrt; $x++ ) {\n my $paste = sub {\n my ( $_width, $_height ) = @_;\n my ( $success, $width_success );\n for ( 0 .. $_width - 1 ) {\n if ( !defined $matrix->[$y][ $x + $_ ] ) {\n $width_success = 0;\n last;\n }\n next if $matrix->[$y][ $x + $_ ] ne 0;\n $width_success++;\n }\n\n if ($width_success) {\n for ( 0 .. $_height - 1 ) {\n if ( !defined $matrix->[ $y + $_ ][$x] ) {\n $success = 0;\n last;\n }\n next if $matrix->[ $y + $_ ][$x] ne 0;\n $success++;\n }\n }\n return $success;\n };\n\n if ( $matrix->[$y][$x] eq 0 ) {\n if ( $paste->( $width, $height ) ) {\n return $x, $y;\n }\n elsif ( $paste->( $height, $width ) ) {\n return $x, $y, 1;\n }\n }\n }\n }\n return;\n}\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\tprint;\n\t%h = ();\n\tmap { ++ $h{ $_ } } split;\n\t$f = 0;\n\t$_ > 3 and $f ++ for values %h;\n\t$f and do { print -1; next }; \n\ts/(\\d+) (\\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;\n#\tprint;\n\t\n\t/(\\b\\d+\\b).{2,}\\1.{2,}\\1/ ? \n\tdo {\n\t\t$del = $1;\n\t\ts/\\b$del\\b//g;\n\t\t$sum = eval join '+', split;\n\t\t$sum == $del or do { print -1; next }; \n\t\t$let = 'A';\n\t\tprint join \"\\n\", $del , map { ( $let ++x $del ) x $_ } split;\n\t\t\n\t\t}\n\t:\n\tdo {\n\t\t(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next }; \n\t\t$sum = eval join '+', map { eval join '*', split } /\\d+ \\d+/g;\n\t\t$max = (sort {$b <=> $a} split)[0];\n\t\t$sum == $max ** 2 or do { print -1; next };\n\t\t$bemax = $_;\n\t\t$bemax =~ s/\\b$max\\b (\\d+) ?//;\n\t\t$wide = $1;\n#\t\tprint $bemax;\n\t\t$let = 'A';\n\t\t$pre = join \"\\n\", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,\n\t\t\t( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];\n#\t\tprint $pre;\n\t\t$pre =~ s/$/ 'C' x $wide /gems;\n#\t\tprint $pre;\n\t\t\n\t\t/^\\b$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ \\b$max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t/\\b$max\\b \\d+$/ and $pre =~ y/ABC/ABC/;\n\t\t\n\t\tprint $max, \"\\n\", $pre;\n\t\t}\n\t\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\t%h = ();\n\tmap { ++ $h{ $_ } } split;\n\t$_ > 3 and do { print -1; next } for values %h;\n\ts/(\\d+) (\\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;\n#\tprint;\n\t\n\t/(\\b\\d+\\b).{2,}\\1.{2,}\\1/ ? \n\tdo {\n\t\t$del = $1;\n\t\ts/\\b$del\\b//g;\n\t\t$sum = eval join '+', split;\n\t\t$sum == $del or do { print -1; next }; \n\t\t$let = 'A';\n\t\tprint join \"\\n\", $del , map { ( $let ++x $del ) x $_ } split;\n\t\t\n\t\t}\n\t:\n\tdo {\n\t\t(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next }; \n\t\t$sum = eval join '+', map { eval join '*', split } /\\d+ \\d+/g;\n\t\t$max = (sort {$b <=> $a} split)[0];\n\t\t$sum == $max ** 2 or do { print -1; next };\n\t\t$bemax = $_;\n\t\t$bemax =~ s/\\b$max\\b (\\d+) ?//;\n\t\t$wide = $1;\n#\t\tprint $bemax;\n\t\t$let = 'A';\n\t\t$pre = join \"\\n\", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,\n\t\t\t( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];\n#\t\tprint $pre;\n\t\t$pre =~ s/$/ 'C' x $wide /gems;\n#\t\tprint $pre;\n\t\t\n\t\t/^\\b$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ \\b$max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t/\\b$max\\b \\d+$/ and $pre =~ y/ABC/ABC/;\n\t\t\n\t\tprint $max, \"\\n\", $pre;\n\t\t}\n\t\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n#\tprint;\n\t%h = ();\n\tmap { ++ $h{ $_ } } split;\n\t$f = 0;\n\t$_ > 3 and $f ++ for values %h;\n\t$f and do { print -1; next }; \n\ts/(\\d+) (\\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;\n#\tprint;\n\t\n\t/(\\b\\d+\\b).{2,}\\1.{2,}\\1/ ? \n\tdo {\n\t\t$del = $1;\n\t\ts/\\b$del\\b//g;\n\t\t$sum = eval join '+', split;\n\t\t$sum == $del or do { print -1; next }; \n\t\t$let = 'A';\n\t\tprint join \"\\n\", $del , map { ( $let ++x $del ) x $_ } split;\n\t\t\n\t\t}\n\t:\n\tdo {\n\t\t(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next }; \n\t\t$sum = eval join '+', map { eval join '*', split } /\\d+ \\d+/g;\n\t\t$max = (sort {$b <=> $a} split)[0];\n\t\t$sum == $max ** 2 or do { print -1; next };\n\t\t$bemax = $_;\n\t\t$bemax =~ s/\\b$max\\b (\\d+) ?//;\n\t\t$wide = $1;\n#\t\tprint $bemax;\n\t\t$let = 'A';\n\t\t$pre = join \"\\n\", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,\n\t\t\t( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];\n#\t\tprint $pre;\n\t\t$pre =~ s/$/ 'C' x $wide /gems;\n#\t\tprint $pre;\n\t\t\n\t\t/^\\b$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ \\b$max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t/\\b$max\\b \\d+$/ and $pre =~ y/ABC/ABC/;\n\t\t\n\t\tprint $max, \"\\n\", $pre;\n\t\t}\n\t\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n#\tprint;\n\t%h = ();\n\tmap { ++ $h{ $_ } } split;\n\t$f = 0;\n\t$_ > 3 and $f ++ for values %h;\n\t$f and do { print -1; next }; \n\ts/(\\d+) (\\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;\n#\tprint;\n\t\n\t/(\\d+).{2,}\\b\\1\\b.{2,}\\b\\1\\b/ ? \n\tdo {\n\t\t$del = $1;\n\t\ts/\\b$del\\b//g;\n\t\t$sum = eval join '+', split;\n\t\t$sum == $del or do { print -1; next }; \n\t\t$let = 'A';\n\t\tprint join \"\\n\", $del , map { ( $let ++x $del ) x $_ } split;\n\t\t\n\t\t}\n\t:\n\tdo {\n\t\t(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next }; \n\t\t$sum = eval join '+', map { eval join '*', split } /\\d+ \\d+/g;\n\t\t$max = (sort {$b <=> $a} split)[0];\n\t\t$sum == $max ** 2 or do { print -1; next };\n\t\t$bemax = $_;\n\t\t$bemax =~ s/\\b$max\\b (\\d+) ?//;\n\t\t$wide = $1;\n#\t\tprint $bemax;\n\t\t$let = 'A';\n\t\t$pre = join \"\\n\", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,\n\t\t\t( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];\n#\t\tprint $pre;\n\t\t$pre =~ s/$/ 'C' x $wide /gems;\n#\t\tprint $pre;\n\t\t\n\t\t/^\\b$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ \\b$max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t/\\b$max\\b \\d+$/ and $pre =~ y/ABC/ABC/;\n\t\t\n\t\tprint $max, \"\\n\", $pre;\n\t\t}\n\t\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\t$sum = eval join '+', map { eval join '*', split } /\\d+ \\d+/g;\n\t$max = (sort {$b <=> $a} split)[0];\n\t$sum == $max ** 2 or do { print -1; next };\n\t\n#\tprint;\n\t%h = ();\n\tmap { ++ $h{ $_ } } split;\n\t$f = 0;\n\t$_ =~ /[46]/ and $f ++ for values %h;\n\t$f and do { print -1; next }; \n\ts/(\\d+) (\\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;\n#\tprint;\n\n\t$g = 0;\n\t$_ == 5 and $g ++ for values %h;\n\t$g and do {\n\t\t$max = (sort {$b <=> $a} split)[0];\n\t\t/\\b$max $max\\b/ and do { print -1; next }; \n\t\t$min = (sort {$b <=> $a} split)[1];\n\t\t$min * 2 == $max or do { print -1; next };\n\t\t\n\t\t$let = 'A';\n\t\t$pre = join \"\\n\", ($let x $min) x $min , (++ $let x $min) x $min;\n\t\t$pre =~ s/$/ 'C' x $min /gems;\n\t\t\n\t\t/^\\b$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ \\b$max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t/\\b$max\\b \\d+$/ and $pre =~ y/ABC/ABC/;\n\t\t\n\t\tprint $max, \"\\n\", $pre;\n\t\tnext;\n\t\t};\n\t\n\t/\\b(\\d+)\\b.{2,}\\b\\1\\b.{2,}\\b\\1\\b/ ? \n\tdo {\n\t\t$del = $1;\n\t\ts/\\b$del\\b//g;\n\t\t$sum = eval join '+', split;\n\t\t$sum == $del or do { print -1; next }; \n\t\t$let = 'A';\n\t\tprint join \"\\n\", $del , map { ( $let ++x $del ) x $_ } split;\n\t\t\n\t\t}\n\t:\n\tdo {\n\t\t(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next }; \n\t\t$sum = eval join '+', map { eval join '*', split } /\\d+ \\d+/g;\n\t\t$max = (sort {$b <=> $a} split)[0];\n\t\t$sum == $max ** 2 or do { print -1; next };\n\t\t$bemax = $_;\n\t\t$bemax =~ s/\\b$max\\b (\\d+) ?//;\n\t\t$wide = $1;\n#\t\tprint $bemax;\n\t\t$let = 'A';\n\t\t$pre = join \"\\n\", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,\n\t\t\t( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];\n#\t\tprint $pre;\n\t\t$pre =~ s/$/ 'C' x $wide /gems;\n#\t\tprint $pre;\n\t\t\n\t\t/^\\b$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ \\b$max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t/\\b$max\\b \\d+$/ and $pre =~ y/ABC/ABC/;\n\t\t\n\t\tprint $max, \"\\n\", $pre;\n\t\t}\n\t\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n#\tprint;\n\t%h = ();\n\tmap { ++ $h{ $_ } } split;\n\t$f = 0;\n\t$_ =~ /[46]/ and $f ++ for values %h;\n\t$f and do { print -1; next }; \n\ts/(\\d+) (\\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;\n#\tprint;\n\n\t$g = 0;\n\t$_ == 5 and $g ++ for values %h;\n\t$g and do {\n\t\t$max = (sort {$b <=> $a} split)[0];\n\t\t/\\b$max $max\\b/ and do { print -1; next }; \n\t\t$min = (sort {$b <=> $a} split)[1];\n\t\t$min * 2 == $max or do { print -1; next };\n\t\t\n\t\t$let = 'A';\n\t\t$pre = join \"\\n\", ($let x $min) x $min , (++ $let x $min) x $min;\n\t\t$pre =~ s/$/ 'C' x $min /gems;\n\t\t\n\t\t/^\\b$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ \\b$max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t/\\b$max\\b \\d+$/ and $pre =~ y/ABC/ABC/;\n\t\t\n\t\tprint $max, \"\\n\", $pre;\n\t\tnext;\n\t\t};\n\t\n\t/(\\d+).{2,}\\b\\1\\b.{2,}\\b\\1\\b/ ? \n\tdo {\n\t\t$del = $1;\n\t\ts/\\b$del\\b//g;\n\t\t$sum = eval join '+', split;\n\t\t$sum == $del or do { print -1; next }; \n\t\t$let = 'A';\n\t\tprint join \"\\n\", $del , map { ( $let ++x $del ) x $_ } split;\n\t\t\n\t\t}\n\t:\n\tdo {\n\t\t(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next }; \n\t\t$sum = eval join '+', map { eval join '*', split } /\\d+ \\d+/g;\n\t\t$max = (sort {$b <=> $a} split)[0];\n\t\t$sum == $max ** 2 or do { print -1; next };\n\t\t$bemax = $_;\n\t\t$bemax =~ s/\\b$max\\b (\\d+) ?//;\n\t\t$wide = $1;\n#\t\tprint $bemax;\n\t\t$let = 'A';\n\t\t$pre = join \"\\n\", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,\n\t\t\t( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];\n#\t\tprint $pre;\n\t\t$pre =~ s/$/ 'C' x $wide /gems;\n#\t\tprint $pre;\n\t\t\n\t\t/^\\b$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ \\b$max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t/\\b$max\\b \\d+$/ and $pre =~ y/ABC/ABC/;\n\t\t\n\t\tprint $max, \"\\n\", $pre;\n\t\t}\n\t\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\ts/(\\d+) (\\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;\n#\tprint;\n\t\n\t/(\\b\\d+\\b).{2,}\\1.{2,}\\1/ ? \n\tdo {\n\t\t$del = $1;\n\t\ts/\\b$del\\b//g;\n\t\t$sum = eval join '+', split;\n\t\t$sum == $del or do { print -1; next }; \n\t\t$let = 'A';\n\t\tprint join \"\\n\", $del , map { ( $let ++x $del ) x $_ } split;\n\t\t\n\t\t}\n\t:\n\tdo {\n\t\t(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next }; \n\t\t$sum = eval join '+', map { eval join '*', split } /\\d+ \\d+/g;\n\t\t$max = (sort {$b <=> $a} split)[0];\n\t\t$sum == $max ** 2 or do { print -1; next };\n\t\t$bemax = $_;\n\t\t$bemax =~ s/\\b$max\\b (\\d+) ?//;\n\t\t$wide = $1;\n#\t\tprint $bemax;\n\t\t$let = 'A';\n\t\t$pre = join \"\\n\", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,\n\t\t\t( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];\n#\t\tprint $pre;\n\t\t$pre =~ s/$/ 'C' x $wide /gems;\n#\t\tprint $pre;\n\t\t\n\t\t/^\\b$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ \\b$max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t/\\b$max\\b \\d+$/ and $pre =~ y/ABC/ABC/;\n\t\t\n\t\tprint $max, \"\\n\", $pre;\n\t\t}\n\t\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n#\tprint;\n\t%h = ();\n\tmap { ++ $h{ $_ } } split;\n\t$f = 0;\n\t$_ =~ /[46]/ and $f ++ for values %h;\n\t$f and do { print -1; next }; \n\ts/(\\d+) (\\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;\n#\tprint;\n\n\t$g = 0;\n\t$_ == 5 and $g ++ for values %h;\n\t$g and do {\n\t\t$max = (sort {$b <=> $a} split)[0];\n\t\t/\\b$max $max\\b/ and do { print -1; next }; \n\t\t$min = (sort {$b <=> $a} split)[1];\n\t\t$min * 2 == $max or do { print -1; next };\n\t\t\n\t\t$let = 'A';\n\t\t$pre = join \"\\n\", ($let x $min) x $min , (++ $let x $min) x $min;\n\t\t$pre =~ s/$/ 'C' x $min /gems;\n\t\t\n\t\t/^\\b$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ \\b$max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t/\\b$max\\b \\d+$/ and $pre =~ y/ABC/ABC/;\n\t\t\n\t\tprint $max, \"\\n\", $pre;\n\t\tnext;\n\t\t};\n\t\n\t/\\b(\\d+)\\b.{2,}\\b\\1\\b.{2,}\\b\\1\\b/ ? \n\tdo {\n\t\t$del = $1;\n\t\ts/\\b$del\\b//g;\n\t\t$sum = eval join '+', split;\n\t\t$sum == $del or do { print -1; next }; \n\t\t$let = 'A';\n\t\tprint join \"\\n\", $del , map { ( $let ++x $del ) x $_ } split;\n\t\t\n\t\t}\n\t:\n\tdo {\n\t\t(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next }; \n\t\t$sum = eval join '+', map { eval join '*', split } /\\d+ \\d+/g;\n\t\t$max = (sort {$b <=> $a} split)[0];\n\t\t$sum == $max ** 2 or do { print -1; next };\n\t\t$bemax = $_;\n\t\t$bemax =~ s/\\b$max\\b (\\d+) ?//;\n\t\t$wide = $1;\n#\t\tprint $bemax;\n\t\t$let = 'A';\n\t\t$pre = join \"\\n\", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,\n\t\t\t( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];\n#\t\tprint $pre;\n\t\t$pre =~ s/$/ 'C' x $wide /gems;\n#\t\tprint $pre;\n\t\t\n\t\t/^\\b$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ \\b$max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t/\\b$max\\b \\d+$/ and $pre =~ y/ABC/ABC/;\n\t\t\n\t\tprint $max, \"\\n\", $pre;\n\t\t}\n\t\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\t@s = sort {$b <=> $a} split;\n\t($max, $min) = @s[0,-1];\n\t$cmax = () = /\\b$max\\b/g;\n\n\ts/(\\d+) (\\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;\n\n\t$area = eval join '+', map { eval join '*', split } /\\d+ \\d+/g;\n\t\n\t$area == $max ** 2 or do { print -1; next };\n\n\t%hash = ();\n\tmap { ++ $hash{ $_ } } split;\n\t(join ' ', values %hash) =~ /[46]/ and do { print -1; next };\n\t\n\t$let = 'A';\n\t\n\t$cmax == 3 ? \n\tdo {\n\t\ts/\\b$max\\b//g;\n\t\t(eval join '+', split) == $max or do { print -1; next };\n\t\t\n\t\tprint join \"\\n\", $max , map { ( $let ++ x $max ) x $_ } split;\n\t\t}\n\t:\n\tdo {\n\t\t$cmax == 1 or do { print -1; next }; \n\t\t\n\t\t($bemax = $_) =~ s/\\b$max\\b (\\d+) ?//;\n\t\t$wide2 = $1;\n\t\t$wide1 = $max - $wide2;\n\t\t\n\t\t$bemax =~ s/\\b$wide1\\b// for 1 .. 2;\n\t\t\n\t\t$pre = join \"\\n\", map { ( $let ++ x $wide1 ) x $_ } split ' ', $bemax;\n\t\t\n\t\t$pre =~ s/$/ 'C' x $wide2 /gem;\n\t\t\n\t\t/^$max\\b/ and $pre =~ y/ABC/BCA/;\n\t\t/^\\d+ \\d+ $max\\b/ and $pre =~ y/ABC/ACB/;\n\t\t\n\t\tprint join \"\\n\", $max, $pre;\n\t\n\t}\n}"}], "src_uid": "2befe5da2df57d23934601cbe4d4f151"} {"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": "$@;for(1..<>){$@+=1/$_}print$@"}], "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$$$) \u00a0\u2014 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": "use 5.020;\nuse warnings;\nuse utf8;\n\nsub readNext {\n my $line = ;\n chomp($line);\n return $line;\n}\n\nmy $count = int( &readNext() );\n\nREAD: while ($count) {\n\n my @a = split //, &readNext();\n my @b = split //, &readNext();\n my @c = split //, &readNext();\n\n $count--;\n\n foreach ( 0 .. $#a ) {\n if ( $a[$_] eq $c[$_] || $b[$_] eq $c[$_] ) {\n next;\n }\n else {\n say \"NO\";\n next READ;\n }\n }\n\n say \"YES\";\n}\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy $A = $_;\n\tmy $B = <>;\n\tmy $C = <>;\n\t\n\tchomp for $A, $B, $C;\n\t\n\tmy $fail = 0;\n\t\n\tfor my $i ( 0 .. -1 + length $A ){\n\t\tmy $cA = substr $A, $i, 1;\n\t\tmy $cB = substr $B, $i, 1;\n\t\tmy $cC = substr $C, $i, 1;\n\t\t\n\t\tif( not( $cA eq $cC || $cB eq $cC ) ){\n\t\t\t$fail = 1;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tprint $fail ? \"NO\" : \"YES\";\n\t}"}], "negative_code": [], "src_uid": "08679e44ee5d3c3287230befddf7eced"} {"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 \u2014 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\" \u2009\u2192\u2009 \"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\" \u2009\u2192\u2009 \"a56f:00d3:0000:0124:0001::\"; \"a56f:0000:0000:0124:0001:0000:1234:0ff0\" \u2009\u2192\u2009 \"a56f::0124:0001:0000:1234:0ff0\"; \"a56f:0000:0000:0000:0001:0000:1234:0ff0\" \u2009\u2192\u2009 \"a56f:0000::0000:0001:0000:1234:0ff0\"; \"a56f:00d3:0000:0124:0001:0000:0000:0000\" \u2009\u2192\u2009 \"a56f:00d3:0000:0124:0001::0000\"; \"0000:0000:0000:0000:0000:0000:0000:0000\" \u2009\u2192\u2009 \"::\". 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 \u2014 the number of records to restore (1\u2009\u2264\u2009n\u2009\u2264\u2009100). Each of the following n lines contains a string \u2014 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": "<>;\nwhile(<>){\n chomp;\n while(/(?>:|^)([^:]{1,3})(?=:|$)/g) {\n $t=sprintf(\"%04s\",$1);\n s/(:|^)([^:]{1,3})(:|$)/$1$t$3/;\n }\n $cnt = 0;\n ++$cnt while /(?>:|^)([^:]{4})(?=:|$)/g;\n s/::/\":0000:\"x(8-$cnt)/e;\n s/::/:/g;\n s/^:|:$//g;\n print\"$_\\n\";\n}\n"}, {"source_code": "<>;\nwhile(<>){\nchomp;\n while(/(?>:|^)([^:]{1,3})(?=:|$)/g) {\n $t=sprintf(\"%04s\",$1);\n#print \"t->$t<\\n\";\n s/(:|^)([^:]{1,3})(:|$)/$1$t$3/;\n#print \"now=$_\\n\";\n#sleep 3;\n }\n $cnt = 0;\n while( /(?>:|^)([^:]{4})(?=:|$)/g ) {\n ++$cnt;\n }\n s/::/\":0000:\"x(8-$cnt)/e;\n#print \"$_\\n\";\n s/::/:/g;\n s/^:|:$//g;\n print \"$_\\n\";\n}\n"}, {"source_code": "#!perl -p\nINIT{<>}$n=y/:/:/;s/::/':'x(9-$n)/e;s/^|:/$&0000/g;s/\\w*(\\w{4}\\W)/$1/g;\n"}, {"source_code": "#!perl -p\nINIT{<>}s/::/':'x(9-y#:#:#)/e;s/^|:/$&0000/g;s/\\w*(\\w{4}\\W)/$1/g;"}, {"source_code": "#!perl -p\nINIT{<>}s/::/':'x(9-y#:#:#)/e;s/\\w*\\W/0 x(5-length$&).$&/ge"}, {"source_code": "#!perl -p\nINIT{<>}$n=y/:/:/;s/::/':'x(9-$n)/e;s/^|:/$&0000/g;s/\\w*(\\w{4}\\W)/$1/g;"}, {"source_code": "#!perl -p\nINIT{<>}$:=y/:/:/;s/::/':'x(9-$:)/e;s/\\w*\\W/0 x(5-length$&).$&/ge\n"}, {"source_code": "#!perl -pl\nINIT{$_ = <>}$:=y/:/:/;s/::/':'x(9-$:)/e if/::/;s/(?}$:=y/:/:/;s/::/':'x(9-$:)/e;s/\\w*\\W/'0'x(5-length$&).$&/ge\n"}, {"source_code": "#!perl -pl\nINIT{$_ = <>}$:=y/:/:/;s/::/':'x(9-$:)/e;s/(?;\n}\n\n$: = y/:/:/;\ns/::/':' x (9 - $:)/e if /::/;\n$_ = join ':', map { '0' x (4 - length) . $_ } (split ':', $_, -1);\n"}, {"source_code": "#!perl -p\nINIT{<>}s/::/':'x(9-y#:#:#)/e;s/\\w*\\W/0 x(5-length$&).$&/ge\n"}, {"source_code": "#!perl -p\nINIT{<>}s/::/':'x(9-y#:#:#)/e;s/\\w*\\W/0 x(5-length$&).$&/ge\n"}, {"source_code": "#!perl -pl\nINIT{$_ = <>}$:=y/:/:/;s/::/':'x(9-$:)/e if/::/;s/(?}s/::/':'x(9-y#:#:#)/e;s/\\w*\\W/0 x(5-length$&).$&/ge"}, {"source_code": "#!perl -p\nINIT{<>}s/::/':'x(9-y#:#:#)/e;s/\\w*\\W/0 x(5-length$&).$&/ge\n"}, {"source_code": "#!perl -p\nINIT{<>}s/::/':'x(9-y#:#:#)/e;s/\\w*\\W/0 x(5-length$&).$&/ge\n"}, {"source_code": "#!perl -p\nINIT{<>}s/::/':'x(9-y#:#:#)/e;s/\\w*\\W/0 x(5-length$&).$&/ge\n"}, {"source_code": "#!perl -p\nINIT{<>}s/::/':'x(9-y#:#:#)/e;s/\\w*\\W/0 x(5-length$&).$&/ge\n"}, {"source_code": "#!perl -p\nINIT{<>}s/::/':'x(9-y#:#:#)/e;s/\\w*\\W/0 x(5-length$&).$&/ge\n"}, {"source_code": "<>;\nwhile(<>){\n\tchomp;\n\t$j=0;\n\ts/:/++$j and \":\"/ge;\n\ts/::/\":0000\"x(8-$j).\":\"/e;\n\ts/:/ /g;\n\ts/ $//;\n\t@_=split/ /;\n\tfor $i(@_){\n\t\t$i=~s/^(.*)$/(\"0\"x(4-length $&)).$1/e;\n\t\t}\n\tprint join\":\",@_;\n\tprint (\":0000\"x(8-@_));\n\tprint \"\\n\";\n\t}"}, {"source_code": "#!perl -p\nINIT{<>}s/::/':'x(9-y#:#:#)/e;s/\\w*\\W/0 x(5-length$&).$&/ge\n"}], "negative_code": [{"source_code": "<>;\nwhile(<>){\n chomp;\n while(/(?>:|^)([^:]{1,3})(?=:|$)/g) {\n $t=sprintf(\"%04s\",$1);\n s/(:|^)([^:]{1,3})(:|$)/$1$t$3/;\n }\n#$cnt = 0;\n# while( /(?>:|^)([^:]{4})(?=:|$)/g ) {\n# ++$cnt;\n# }\n $cnt = split\":\",$_;\n s/::/\":0000:\"x(8-$cnt)/e;\n s/::/:/g;\n s/^:|:$//g;\n print\"$_\\n\";\n}\n"}, {"source_code": "<>;\nwhile(<>){\n#chomp;\n while(/(?>:|^)([^:]{1,3})(?=:|$)/g) {\n#print \"|$`<$&>$'|\\n\";\n#print \"=>$1<\\n\";\n#s/([^:]+):/sprintf(\"04s\",$1)/e;\n $t=sprintf(\"%04s\",$1);\n#print \"t->$t<\\n\";\n s/(:|^)([^:]{1,3})(:|$)/$1$t$3/;\n#print \"now=$_\\n\";\n#sleep 3;\n }\n $cnt = 0;\n while( /(?>:|^)([^:]{4})(?=:|$)/g ) {\n ++$cnt;\n }\n s/::/\":0000:\"x(8-$cnt)/e;\n#print \"$_\\n\";\n s/::/:/g;\n s/^:|:$//g;\n print;\n}\n"}, {"source_code": "<>;\nwhile(<>){\n chomp;\n while(/(?>:|^)([^:]{1,3})(?=:|$)/g) {\n#print \"|$`<$&>$'|\\n\";\n#print \"=>$1<\\n\";\n#s/([^:]+):/sprintf(\"04s\",$1)/e;\n $t=sprintf(\"%04s\",$1);\n#print \"t->$t<\\n\";\n s/(:|^)([^:]{1,3})(:|$)/$1$t$3/;\n#print \"now=$_\\n\";\n#sleep 3;\n }\n $cnt = 0;\n while( /(?>:|^)([^:]{4})(?=:|$)/g ) {\n ++$cnt;\n }\n s/::/\":0000:\"x(8-$cnt)/e;\n#print \"$_\\n\";\n s/::/:/g;\n s/^:|:$//g;\n print;\n}\n"}, {"source_code": "<>;\nwhile(<>){\n while(/(?>:|^)([^:]{1,3})(?=:|$)/g) {\n $t=sprintf(\"%04s\",$1);\n s/(:|^)([^:]{1,3})(:|$)/$1$t$3/;\n }\n#$cnt = 0;\n# while( /(?>:|^)([^:]{4})(?=:|$)/g ) {\n# ++$cnt;\n# }\n $cnt = split\":\",$_;\n s/::/\":0000:\"x(8-$cnt)/e;\n s/::/:/g;\n s/^:|:$//g;\n print;\n}\n"}, {"source_code": "<>;\nwhile(<>){\n chomp;\n while(/(?>:|^)([^:]{1,3})(?=:|$)/g) {\n $t=sprintf(\"%04s\",$1);\n s/(:|^)([^:]{1,3})(:|$)/$1$t$3/;\n }\n#$cnt = 0;\n# while( /(?>:|^)([^:]{4})(?=:|$)/g ) {\n# ++$cnt;\n# }\n $cnt = split\":\",$_;\n s/::/\":0000:\"x(8-$cnt)/e;\n s/::/:/g;\n s/^:|:$//g;\n print;\n}\n"}, {"source_code": "<>;\nwhile(<>){\n chomp;\n while(/(?>:|^)([^:]{1,3})(?=:|$)/g) {\n#print \"|$`<$&>$'|\\n\";\n#print \"=>$1<\\n\";\n#s/([^:]+):/sprintf(\"04s\",$1)/e;\n $t=sprintf(\"%04s\",$1);\n#print \"t->$t<\\n\";\n s/(:|^)([^:]{1,3})(:|$)/$1$t$3/;\n#print \"now=$_\\n\";\n#sleep 3;\n }\n $cnt = 0;\n while( /(?>:|^)([^:]{4})(?=:|$)/g ) {\n ++$cnt;\n }\n s/::/\":0000:\"x(8-$cnt)/e;\n print \"$_\\n\";\n s/::/:/g;\n s/^:|:$//;\n print \"$_\\n\";\n}\n"}, {"source_code": "#!perl -p\nINIT{<>}s/::/':'x(9-y|:|:|)/e;s/\\w*./0 x(5-length$&).$&/ge\n"}, {"source_code": "#!perl -pl\n\nINIT {\n $_ = <>;\n}\n\n$: = y/:/:/;\nif ($: < 7) {\n s/::/':' x (9 - $:)/e;\n}\n$_ = join ':', map { '0' x (4 - length) . $_ } (split ':', $_, -1);\n"}, {"source_code": "<>;\nwhile(<>){\n\tchomp;\n\ts/^:/0:/;\n\twhile (/::/) {s/::/ 0:/};\n\ts/:/ /g;\n\ts/ $//;\n\t@_=split/ /;\n\tfor $i(@_){\n\t\t$i=~s/^(.*)$/(\"0\"x(4-length $&)).$1/e;\n\t\t}\n\tprint join\":\",@_;\n\tprint (\":0000\"x(8-@_));\n\tprint \"\\n\";\n\t}"}], "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": "$a=<>;\n$b=<>;\n@x=sort split '',$a;\n@y=sort split '',$b;\n$flag=0;\nif((join '',@x)eq(join '',@y)){\n @x=split '',$a;\n @y=split '',$b;\n for($i=0;$i<@x;++$i){\n ++$cnt if $x[$i]ne$y[$i];\n }\n $flag=1 if $cnt>2;\n} else { \n $flag=1;\n}\nprint qw/YES NO/[$flag];"}, {"source_code": "$ch1=<>;\nchomp$ch1;\n$ch2=<>;\nchomp$ch2;\n$l1=length$ch1;\n$l2=length$ch2;\nif($l1 != $l2){\nprint \"NO\";\n}\nelse{\n$nb=0;\nfor($i=0;$i<$l1;$i++){\nif(substr($ch1,$i,1) ne substr($ch2,$i,1)){\n$nb++;\npush(@arr1,substr($ch1,$i,1));\npush(@arr2,substr($ch2,$i,1));\n}\nif($nb>2){\nlast;\n}\n\n\n}\n\nif(($nb!=2) || ($nb==2 && ($arr1[0] ne $arr2[1] || $arr1[1] ne $arr2[0]))){\nprint \"NO\";\n}\nelse{\n\nprint\"YES\";\n\n}\n\n\n\n\n\n\n\n}"}], "negative_code": [{"source_code": "$a=<>;\n$b=<>;\n@x=sort split '',$a;\n@y=sort split '',$b;\n$flag=0;\nif((join '',@x)eq(join '',@y)){\n @x=split '',$a;\n @y=split '',$b;\n for($i=0;$i<@x;++$i){\n ++$cnt if $x[$i]ne$y[$i];\n }\n if($cnt>2){\n $flag=1;\n exit;\n }\n} else {\n $flag=1;\n}\nprint qw/YES NO/[$flag]; "}], "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 \u2014 \"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": "chomp($_=<>);\nchomp($b=<>);\n$le=length;\n$lb=length $b;\nfor $i(1..$le){\n\tif ($le % $i) {next}\n\telse {$m=substr $_,0,$i}\n#\tprint \" $m\\n\";\n\t$n=()=/$m/g;\n\t$nb=()=$b=~/$m/g;\n\t$i*$n == $le and $i*$nb == $lb and $g++;\n\t}\nprint 0+$g"}, {"source_code": "chomp($_=<>);\n$le=length;\n$_.=<>;\nfor $i(1..$le){\n\tif ($le % $i) {next}\n\telse {$m=substr $_,0,$i}\t\n\t$g+= /^($m)+$/\n\t}\nprint 0+$g"}, {"source_code": "chomp($_=<>);\nchomp($b=<>);\n$le=length;\n$lb=length $b;\nfor ($i=1; $i<=$le; $i+=$j || 1){\n\tif ($le % $i) {next}\n\telse { $j//=$i ; $m=substr $_,0,$i}\n#\tprint \" $m\\n\";\n\t$n=()=/$m/g;\n\t$nb=()=$b=~/$m/g;\n\t$i*$n == $le and $i*$nb == $lb and $g++;\n\t}\nprint 0+$g"}], "negative_code": [{"source_code": "chomp($_=<>);\n$le=length;\nchomp($_.=<>);\nfor $i(1..$le){\n\tif ($le % $i) {next}\n\telse {\n\t\t$g and (length)% $i==0 and $g++, next; \n\t\t$m=substr $_,0,$i\n\t\t}\n\t\n\t$g+= /^($m)+$/\n\t}\nprint 0+$g"}, {"source_code": "chomp($_=<>);\n$b=<>;\n$le=length;\nfor $i(1..$le){\n\tif ($le % $i) {next}\n\telse {$m=substr $_,0,$i}\n\t\"$_$b\"=~/^$m+$/ && $g++\n\t}\nprint 0+$g"}], "src_uid": "d28614f0365ea53530e35c6bd1e6f1dd"} {"nl": {"description": "Emuskald is an avid horticulturist and owns the world's longest greenhouse \u2014 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\u2009-\u20091 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20095000, n\u2009\u2265\u2009m), 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\u2009\u2264\u2009si\u2009\u2264\u2009m), and one real number xi (0\u2009\u2264\u2009xi\u2009\u2264\u2009109), 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\u2009<\u2009xi\u2009+\u20091,\u20091\u2009\u2264\u2009i\u2009<\u2009n).", "output_spec": "Output a single integer \u2014 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": "#!perl\nuse v5.18;\n# time complexity: O(Nlog(N))\nwhile(<>){\n my(@b,@a,$i,$x0,$n,$m,$s0,$pos,$length);\n ($n,$m)=split;\n for $i ( 0..$n-1 ) {\n ($s0,$x0)=split ' ',<>; push @a,$s0;\n }\n $length = 1; $b[0]=$a[0];\n for $i ( 1..$#a ){\n $pos=upper_bound($a[$i], \\@b);\n if ($pos == @b) {push @b, $a[$i];}\n else {$b[$pos] = $a[$i];}\n }\n say $n-@b;\n}\n\n#find the first more than $num;\nsub upper_bound {\n my ($num,$array)=@_;\n my ($i,$high,$low,$mid);\n $low = -1; $high = $#{$array};\n while($low+1 < $high) {\n $mid=$low + int(($high-$low)/2);\n if($array->[$mid] > $num) {$high=$mid;}\n else {$low=$mid;}\n }\n return $array->[$high]<=$num ? scalar @$array : $high;\n}\n"}, {"source_code": "#!perl\nuse v5.18;\n# time complexity: O(Nlog(N))\n# got Accepted\nwhile(<>){\n my(@b,@a,$i,$n,$m,$s0,$pos);\n ($n,$m)=split;\n for $i ( 0..$n-1 ) {\n ($s0,$_)=split ' ',<>; push @a,$s0;\n }\n $b[0]=$a[0];\n for $i ( 1..$#a ){\n $pos=upper_bound($a[$i], \\@b);\n if ($pos == @b) {push @b, $a[$i];}\n else {$b[$pos] = $a[$i];}\n }\n say $n-@b;\n}\n\n#find the first more than $num;\nsub upper_bound {\n my ($num,$array)=@_;\n my ($i,$high,$low,$mid);\n $low = -1; $high = $#{$array};\n while($low+1 < $high) {\n $mid=$low + int(($high-$low)/2);\n if($array->[$mid] > $num) {$high=$mid;}\n else {$low=$mid;}\n }\n return $array->[$high]<=$num ? scalar @$array : $high;\n}\n"}, {"source_code": "#!perl\nuse strict;\nuse warnings;\nwhile(<>){\n my(@b,@a,$i,$x0,$n,$m,$s0,$pos,$length);\n ($n,$m)=split;\n for $i ( 0..$n-1 ) {\n ($s0,$x0)=split ' ',<>;\n push @a,$s0;\n }\n $length = 1;\n $b[0]=$a[0];\n for $i ( 1..$#a ){\n if ($a[$i] < $b[0]) {\n $b[0] = $a[$i];\n }\n elsif ($a[$i] >= $b[$length-1]) {\n $b[$length++] = $a[$i];\n }\n else {\n $pos = lower_bound($a[$i], \\@b);\n $b[$pos] = $a[$i];\n }\n }\n #print \"length=$length\\n\";\n print $n-$length,\"\\n\";\n}\n#find the first more than or equal to $num;\nsub lower_bound {\n my ($num,$array)=@_;\n my ($i,$high,$low,$mid);\n $low = -1;\n $high = $#{$array};\n while($low+1 < $high) {\n $mid=$low + int(($high-$low)/2);\n if($array->[$mid] > $num) {\n $high=$mid;\n } else {\n $low=$mid;\n }\n }\n return $high;\n}\n"}, {"source_code": "#!perl\nuse v5.18;\nuse strict;\nuse warnings;\n# time complexity: O(Nlog(N))\n# got Accepted\nwhile(<>){\n my(@b,@a,$i,$n,$m,$s0,$pos);\n ($n,$m)=split;\n for $i ( 0..$n-1 ) {\n ($s0,$_)=split ' ',<>; push @a,$s0;\n }\n $b[0]=$a[0];\n for $i ( 1..$#a ){\n $pos=upper_bound($a[$i], \\@b);\n if ($pos == @b) {push @b, $a[$i];}\n else {$b[$pos] = $a[$i];}\n }\n say $n-@b;\n}\n\n#find the first more than $num;\nsub upper_bound {\n my ($num,$array)=@_;\n my ($i,$high,$low,$mid);\n $low = -1; $high = $#{$array};\n while($low+1 < $high) {\n $mid=$low + int(($high-$low)/2);\n if($array->[$mid] > $num) {$high=$mid;}\n else {$low=$mid;}\n }\n return $array->[$high]<=$num ? scalar @$array : $high;\n}\n"}, {"source_code": "#!perl\nuse v5.20;\n# time complexity: O(Nlog(N))\nwhile(<>){\n my(@b,@a,$i,$x0,$n,$m,$s0,$pos,$length);\n ($n,$m)=split;\n for $i ( 0..$n-1 ) {\n ($s0,$x0)=split ' ',<>; push @a,$s0;\n }\n $length = 1; $b[0]=$a[0];\n for $i ( 1..$#a ){\n if ($a[$i] < $b[0]) {$b[0] = $a[$i];}\n elsif ($a[$i] >= $b[$length-1]) {$b[$length++] = $a[$i];}\n else {$b[lower_bound($a[$i], \\@b)] = $a[$i];}\n }\n say $n-$length;\n}\n\n#find the first more than $num;\nsub lower_bound {\n my ($num,$array)=@_;\n my ($i,$high,$low,$mid);\n $low = -1; $high = $#{$array};\n while($low+1 < $high) {\n $mid=$low + int(($high-$low)/2);\n if($array->[$mid] > $num) {$high=$mid;}\n else {$low=$mid;}\n }\n return $high;\n}\n"}], "negative_code": [{"source_code": "#!perl\nuse strict;\nuse warnings;\nwhile(<>){\n my(@b,@a,@len,$i,$x0,$n,$m,$s0,$pos,$result);\n ($n,$m)=split;\n for $i ( 0..$n-1 ) {\n ($s0,$x0)=split ' ',<>;\n push @a,$s0;\n }\n $b[0]=$a[0];$len[0]=1;\n for $i ( 1..$#a ){\n $pos=upper_bound($a[$i],\\@b);\n if($pos==@b){\n $len[$pos]=$len[$pos-1]+1;\n $b[$pos]=$a[$i];\n } elsif ($pos==0 && $a[$i] < $b[$pos]) {\n $b[$pos] = $a[$i];\n }\n else{\n if($a[$i]==$b[$pos]){\n $len[$pos]++;\n } else{\n $b[$pos+1]=$a[$i];\n }\n }\n }\n $result=1;\n for $i ( 0.. $#len ) { $result = $len[$i] if ($result < $len[$i]); }\n print $n-$result,\"\\n\";\n}\n#find the last less or equal to $num; if all of the element\n#in array are less than num. return the length of the array\nsub upper_bound {\n my ($num,$array)=@_;\n my ($i,$high,$low,$mid);\n $low=0;$high=$#{$array};\n while($low<=$high){\n $mid=int(($low+$high)/2);\n if($array->[$mid] > $num){\n $high=$mid-1;\n } else{\n $low=$mid+1;\n }\n }\n if($high==$#{$array} &&\n $num > $array->[$high]){\n return $high+1;\n }\n return -1!=$high ? $high : 0;\n}\n"}], "src_uid": "32f245fa1a2d99bfabd30b558687ca5f"} {"nl": {"description": "Boboniu gives you $$$r$$$ red balls, $$$g$$$ green balls, $$$b$$$ blue balls, $$$w$$$ white balls. He allows you to do the following operation as many times as you want: Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations. ", "input_spec": "The first line contains one integer $$$T$$$ ($$$1\\le T\\le 100$$$) denoting the number of test cases. For each of the next $$$T$$$ cases, the first line contains four integers $$$r$$$, $$$g$$$, $$$b$$$ and $$$w$$$ ($$$0\\le r,g,b,w\\le 10^9$$$).", "output_spec": "For each test case, print \"Yes\" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print \"No\".", "sample_inputs": ["4\n0 1 1 1\n8 1 9 3\n0 0 0 0\n1000000000 1000000000 1000000000 1000000000"], "sample_outputs": ["No\nYes\nYes\nYes"], "notes": "NoteIn the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome.In the second test case, after doing one operation, changing $$$(8,1,9,3)$$$ to $$$(7,0,8,6)$$$, one of those possible palindromes may be \"rrrwwwbbbbrbbbbwwwrrr\".A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, \"rggbwbggr\", \"b\", \"gg\" are palindromes while \"rgbb\", \"gbbgr\" are not. Notice that an empty word, phrase, or sequence is palindrome."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my @c = map { $_ - 0 } split(/\\s+/,);\n \n my $tot = $c[0] + $c[1] + $c[2] + $c[3];\n \n if( &bb(\\@c) ){\n print \"Yes\\n\";\n } else {\n if( $c[0]>0 and $c[1]>0 and $c[2]>0 ){\n $c[0] -- ;\n $c[1] -- ;\n $c[2] -- ;\n $c[3] += 3;\n }\n if( &bb(\\@c) ){\n print \"Yes\\n\";\n } else {\n print \"No\\n\";\n }\n }\n}\n\nexit(0);\n\nsub bb {\n my $r = shift;\n my $od = 0;\n for(my $i=0;$i<4;$i++){\n $od++ if( ( $r->[$i] % 2 ) == 1 );\n }\n return (( $od == 0 or $od == 1 ) ? 1 : undef);\n}\n\n"}], "negative_code": [], "src_uid": "749a106d462555543c91753f00a5a479"} {"nl": {"description": "This is an easy version of the problem. The only difference between an easy and a hard version is the constraints on $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$.You are given $$$4$$$ positive integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ with $$$a < c$$$ and $$$b < d$$$. Find any pair of numbers $$$x$$$ and $$$y$$$ that satisfies the following conditions: $$$a < x \\leq c$$$, $$$b < y \\leq d$$$, $$$x \\cdot y$$$ is divisible by $$$a \\cdot b$$$.Note that required $$$x$$$ and $$$y$$$ may not exist.", "input_spec": "The first line of the input contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 10$$$), the number of test cases. The descriptions of the test cases follow. The only line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$ ($$$1 \\leq a < c \\leq 10^5$$$, $$$1 \\leq b < d \\leq 10^5$$$).", "output_spec": "For each test case print a pair of numbers $$$a < x \\leq c$$$ and $$$b < y \\leq d$$$ such that $$$x \\cdot y$$$ is divisible by $$$a \\cdot b$$$. If there are multiple answers, print any of them. If there is no such pair of numbers, then print -1 -1.", "sample_inputs": ["5\n\n1 1 2 2\n\n3 4 5 7\n\n8 9 15 18\n\n12 21 14 24\n\n36 60 48 66"], "sample_outputs": ["2 2\n4 6\n12 12\n-1 -1\n-1 -1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nOUTER:\nwhile(<>){\n\t$debug and print '-' x 15;\n\tmy( $A, $B, $C, $D ) = split;\n\t\n\tmy $cA = $A;\n\tmy $cB = $B;\n\t\n\tmy %h;\n\t\n\twhile( $cA > 1 ){\n\t\tmy $i = 2;\n\t\twhile( 1 ){\n\t\t\tif( $cA % $i == 0 ){\n\t\t\t\t$cA /= $i;\n\t\t\t\t$h{ $i } ++;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\twhile( $cB > 1 ){\n\t\tmy $i = 2;\n\t\twhile( 1 ){\n\t\t\tif( $cB % $i == 0 ){\n\t\t\t\t$cB /= $i;\n\t\t\t\t$h{ $i } ++;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t$i ++;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print 'cAB:', join ', ', map { \"$_ -> $h{ $_ }\" } sort keys %h;\n\t\n\tfor my $i ( reverse $A + 1 .. $C ){\n\t\tmy $ii = $i;\n\t\tmy %g;\n\t\tfor my $j ( sort keys %h ){\n\t\t\tmy $k = 0;\n\t\t\twhile( $i % $j == 0 ){\n\t\t\t\t$i /= $j;\n\t\t\t\t$k ++;\n\t\t\t\t}\n\t\t\t$g{ $j } = $h{ $j } - $k if $h{ $j } - $k > 0;\n\t\t\t}\n\t\t\n\t\tmy $x = 1;\n\t\t$x *= $_ ** $g{ $_ } for sort keys %g;\n\t\t\n\t\t$debug and print \"x[$x]\";\n\t\t\n\t\tmy $times = int $B / $x;\n\t\t\n\t\tif( $times * $x + $x <= $D ){\n\t\t\tprint $ii, \" \", $times * $x + $x;\n\t\t\tnext OUTER;\n\t\t\t}\n\t\t}\n\t\n\tprint \"-1 -1\";\n\t}"}], "negative_code": [], "src_uid": "84c60293d487e2c2ecee38a2fcf63b10"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100)\u00a0\u2014 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\u00a0\u2014 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": "$/ = $\\;\n$s = <>;\n\nprint -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];\n"}, {"source_code": "$/ = $\\;\n$s = <>;\n\nprint -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];\n"}, {"source_code": "$/ = $\\;\n$s = <>;\n\nprint -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];"}, {"source_code": "$/ = $\\;\n$s = <>;\n\nprint -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];\n"}, {"source_code": "#!/usr/bin/perl\n# your code here\n$/ = $\\;\n$s = <>;\n\nprint -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];"}, {"source_code": "$/ = $\\;\n$s = <>;\n\nprint -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];\n"}, {"source_code": "$/ = $\\;\n$s = <>;\n\nprint -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];"}, {"source_code": "$/ = $\\;\n$s = <>;\n\nprint -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];\n"}, {"source_code": "$/ = $\\;\n$s = <>;\n\nprint -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];\n"}, {"source_code": "$/ = $\\;\n$_ = <>;\n\nfor $i (a .. z){\n\tfor $j (a .. z){\n\t\t$A = length join '', /^[$i$j]+$/gm;\n\t\t$max < $A and $max = $A;\n\t\t}\n\t}\n\nprint 0 + $max"}, {"source_code": "$/ = $\\;\n$A = <>;\n\n$a = a;\npush @_, 1e3 + length join '', $A =~ /^[$a]+$/gm while $a ++ ne zz;\nprint -1e3 + (sort @_)[-1]"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = ();\n\tfor (1 .. $_){\n\t\tpush @_, ''.<>;\n\t\t}\n\tchomp @_;\n\t\n\t$max = 0;\n\t\n\tfor $i (a .. z){\n\t\tfor $j (a .. z){\n\t\t\t$A = length join '', grep /^[$i$j]+$/, @_;\n\t\t\t$max < $A and $max = $A;\n\t\t\t}\n\t\t}\n\tprint $max;\n\t\n\t}"}, {"source_code": "$/ = $\\;\n$s = <>;\n\nprint -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];"}, {"source_code": "$/ = $\\;\n$s = <>;\n\nprint -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];\n"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = ();\n\tfor (1 .. $_){\n\t\tpush @_, ''.<>;\n\t\t}\n\tchomp @_;\n\t\n\t$max = -~0;\n\t\n\tfor $i (a .. d){\n\t\tfor $j (a .. d){\n\t\t\t$i eq $j and next;\n\t\t\t$A = length join '', grep /^[$i$j]+$/, @_;\n\t\t\t$max < $A and $max = $A;\n\t\t\t}\n\t\t}\n\tprint $max;\n\t\n\t}"}, {"source_code": "$/ = $\\;\n$A = <>;\n\n$a = a;\npush @_, 1e3 + length join '', $s =~ /^[$a]+$/gm while $a ++ ne zz;\nprint -1e3 + (sort @_)[-1]"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = ();\n\tfor (1 .. $_){\n\t\tpush @_, ''.<>;\n\t\t}\n\tchomp @_;\n\t\n\t$max = -~0;\n\t\n\tfor $i (a .. d){\n\t\tfor $j (a .. d){\n#\t\t\t$i eq $j and next;\n\t\t\t$A = length join '', grep /^[$i$j]+$/, @_;\n\t\t\t$max < $A and $max = $A;\n\t\t\t}\n\t\t}\n\tprint $max;\n\t\n\t}"}], "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,\u2009j 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\u2009\u2264\u2009n\u2009\u2264\u2009100)\u00a0\u2014 the number of cashes in the shop. The second line contains n space-separated integers: k1,\u2009k2,\u2009...,\u2009kn (1\u2009\u2264\u2009ki\u2009\u2264\u2009100), 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,\u20091,\u2009mi,\u20092,\u2009...,\u2009mi,\u2009ki (1\u2009\u2264\u2009mi,\u2009j\u2009\u2264\u2009100)\u00a0\u2014 the number of products the j-th person in the queue for the i-th cash has.", "output_spec": "Print a single integer \u2014 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\u00b75\u2009+\u200915\u2009=\u2009515 seconds. But if he chooses the second queue, he will need 1\u00b75\u2009+\u20092\u00b75\u2009+\u20092\u00b75\u2009+\u20093\u00b75\u2009+\u20094\u00b715\u2009=\u2009100 seconds. He will need 1\u00b75\u2009+\u20099\u00b75\u2009+\u20091\u00b75\u2009+\u20093\u00b715\u2009=\u2009100 seconds for the third one and 7\u00b75\u2009+\u20098\u00b75\u2009+\u20092\u00b715\u2009=\u2009105 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": "use strict ;\nuse warnings ;\n\nmy $noCounters = ;\nmy $noPeople = ;\n\nmy $min = 0;\n\nfor(my $i=0;$i<$noCounters;$i++){\n my $Items = ;\n my @noItems = split(/ /,$Items);\n my $sum = 0;\n for(my $j=0;$j<=$#noItems;$j++){\n $sum += 5*$noItems[$j];\n }\n $sum += 15*($#noItems+1);\n #print \"$sum\";\n if($i==0){\n $min=$sum;\n }else{\n if($sum<=$min){\n $min=$sum;\n }\n }\n}\n\nprint \"$min\" ;"}, {"source_code": "\nwhile(<>){\n\t$n=$_;\n\t<>;\n\tfor $i(1..$n){\n\t\t$_=<>;\n\t\t@_=split/ /;\n\t\t$a[$i]+=$_*5 for @_;\n\t\t$a[$i]+=15*@_;\n\t\t\n\t}\n#\tprint \"@a\";\n\tshift @a;\n\t@a= sort {$a<=>$b} @a;\n#\tprint \"@a\";\n\tprint (shift @a);\n\tprint \"\\n\"\n\t}\n\t\n"}, {"source_code": "for $i(0..<>-1){\n\t$j++ or <>;\n\t$_=<>;\n\t@_=split/ /;\n\t$a[$i]+=$_*5 for @_;\n\t$a[$i]+=15*@_;\n}\nprint ((@a= sort {$a<=>$b} @a)[0])\n\t\n"}], "negative_code": [{"source_code": "for $i(1..<>){\n\t$j++ or <>;\n\t$_=<>;\n\t@_=split/ /;\n\t$a[$i]+=$_*5 for @_;\n\t$a[$i]+=15*@_;\n}\nshift @a;\n@a= sort {$a<=>$b} @a[1..-1];\nprint @a[0]"}], "src_uid": "0ea79b2a7ddf3d4da9c7a348e61933a7"} {"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,\u2009y) on this plane. Han Solo has the newest duplex lazer gun to fight these stormtroopers. It is situated at the point (x0,\u2009y0). In one shot it can can destroy all the stormtroopers, situated on some line that crosses point (x0,\u2009y0).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 \u0438 y0 (1\u2009\u2264\u2009n\u2009\u2264\u20091000, \u2009-\u2009104\u2009\u2264\u2009x0,\u2009y0\u2009\u2264\u2009104) \u2014 the number of stormtroopers on the battle field and the coordinates of your gun. Next n lines contain two integers each xi, yi (\u2009-\u2009104\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009104) \u2014 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 \u2014 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": "use bigrat;\n($n, $X, $Y) = split \" \", <>;\nfor (<>){\n\t($x, $y) = split;\n\t$x -= $X + 0/1;\n\t$y -= $Y + 0/1;\n\t$h{ $x ? $y / $x : INF } ++\n\t}\nprint 0+ keys %h"}, {"source_code": "use bigrat;\n$\\ = $/;\nwhile(<>){\n\t($n, $X, $Y) = split;\n\tundef %h;\n\tfor (1 .. $n){\n\t\t($x, $y) = split \" \", <>;\n\t\t$x -= $X + 0/1;\n\t\t$y -= $Y + 0/1;\n\t\t$h{ $x ? ( $y / $x) : INF } ++\n\t\t}\n\t$, = $\";\n#\tprint \"$_ -> $h{$_} \" for keys %h;\n\tprint scalar keys %h\n\t}"}], "negative_code": [{"source_code": "use bigrat;\n$\\ = $/;\nwhile(<>){\n\t($n, $X, $Y) = split;\n\tundef %h;\n\tfor (1 .. $n){\n\t\t($x, $y) = split \" \", <>;\n\t\t$x -= $X;\n\t\t$y -= $Y;\n\t\t$h{ $x ? ($y / $x) : INF } ++\n\t\t}\n\t$, = $\";\n#\tprint \"$_ -> $h{$_} \" for keys %h;\n\tprint scalar keys %h\n\t}"}, {"source_code": "$\\ = $/;\nwhile(<>){\n\t($n, $X, $Y) = split;\n\tundef %h;\n\tfor (1 .. $n){\n\t\t($x, $y) = split \" \", <>;\n\t\t$x -= $X;\n\t\t$y -= $Y;\n\t\t$h{ $x ? ($y / $x) : INF } ++\n\t\t}\n\t$, = $\";\n#\tprint \"$_ -> $h{$_} \" for keys %h;\n\tprint scalar keys %h\n\t}"}], "src_uid": "8d2845c33645ac45d4d37f9493b0c380"} {"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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\tmy %h;\n\t\n\tmap { $h{ $_ } ++ } split ' ', <>;\n\t\n\tif( $k == 0 ){\n\t\tprint $h{ 1 } ? -1 : 1;\n\t\tnext;\n\t\t}\n\t\n\tmy $sum = 0;\n\t\n\tmy $x = 0;\n\t\n\tfor my $i ( sort { $a <=> $b } keys %h ){\n\t\t$sum += $h{ $i };\n\t\t\n\t\tif( $sum == $k ){\n\t\t\t$x = $i;\n\t\t\t}\n\t\t}\n\t\n\tprint $x || -1;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\tmy %h;\n\t\n\tmap { $h{ $_ } ++ } split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\tmy $x = 0;\n\t\n\tfor my $i ( sort { $a <=> $b } keys %h ){\n\t\t$sum += $h{ $i };\n\t\t\n\t\tif( $sum == $k ){\n\t\t\t$x = $i;\n\t\t\t}\n\t\t}\n\t\n\tprint $x || -1;\n\t}"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the number of polyhedrons in Anton's collection. Each of the following n lines of the input contains a string si\u00a0\u2014 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\u00a0\u2014 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\u2009+\u20096\u2009+\u20094\u2009+\u200912\u2009=\u200942 faces."}, "positive_code": [{"source_code": "use 5.016;\n\nmy %sides = (\n Tetrahedron => 4,\n Cube => 6,\n Octahedron => 8,\n Dodecahedron => 12,\n Icosahedron => 20,\n);\n\nmy $n = <>;\nmy $s = 0;\nfor (1..$n) {\n chomp (my $shape = <>);\n $s += $sides{$shape};\n}\nsay $s;\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nmy %poly = (\n Tetrahedron => 4,\n Cube => 6,\n Octahedron => 8,\n Dodecahedron=> 12,\n Icosahedron => 20,\n);\n\nmy $answer = 0;\n\nmy $n = read_token();\n\nfor ( 1..$n ) {\n $answer += $poly{read_token()};\n}\n\nsay $answer;\n\n\n\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n"}, {"source_code": "$n = ;\n$ans = 0;\nfor($i=0;$i<$n;$i++)\n{\n\t$str = ;\n\tchomp($str);\n\tif($str =~ /Tetrahedron/)\n\t{\n\t\t$ans += 4;\n\t}\n\telsif($str =~ /Cube/)\n\t{\n\t\t$ans += 6;\n\t}\n\telsif($str =~ /Octahedron/)\n\t{\n\t\t$ans += 8;\n\t}\n\telsif($str =~ /Dodecahedron/)\n\t{\n\t\t$ans += 12;\n\t}\n\telse\n\t{\n\t\t$ans += 20;\n\t}\n}\nprint(\"$ans\\n\");\n"}, {"source_code": "<>;\n\nprint eval join ' + ', map { y/TCODIa-z/46812/dr =~ s/2/20/r =~ s/^1/12/r } <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy %h;\nmap { /(\\w+).*?(\\d+)/; $h{ $1 } = $2 } ;\n\nwhile(<>){\n\t\n\tmy $sum = 0;\n\t\n\tfor (1 .. $_){\n\t\t$_ = <>, chomp;\n\t\t$sum += $h{ $_ };\n\t\t}\n\t\n\tprint $sum;\n\t}\n\t\n__DATA__\n Tetrahedron. Tetrahedron has 4 triangular faces.\n Cube. Cube has 6 square faces.\n Octahedron. Octahedron has 8 triangular faces.\n Dodecahedron. Dodecahedron has 12 pentagonal faces.\n Icosahedron. Icosahedron has 20 triangular faces. "}], "negative_code": [], "src_uid": "e6689123fefea251555e0e096f58f6d1"} {"nl": {"description": "Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1,\u2009x2,\u2009...,\u2009xk (k\u2009>\u20091) is such maximum element xj, that the following inequality holds: .The lucky number of the sequence of distinct positive integers x1,\u2009x2,\u2009...,\u2009xk (k\u2009>\u20091) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.You've got a sequence of distinct positive integers s1,\u2009s2,\u2009...,\u2009sn (n\u2009>\u20091). Let's denote sequence sl,\u2009sl\u2009+\u20091,\u2009...,\u2009sr as s[l..r] (1\u2009\u2264\u2009l\u2009<\u2009r\u2009\u2264\u2009n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].Note that as all numbers in sequence s are distinct, all the given definitions make sence.", "input_spec": "The first line contains integer n (1\u2009<\u2009n\u2009\u2264\u2009105). The second line contains n distinct integers s1,\u2009s2,\u2009...,\u2009sn (1\u2009\u2264\u2009si\u2009\u2264\u2009109).", "output_spec": "Print a single integer \u2014 the maximum lucky number among all lucky numbers of sequences s[l..r].", "sample_inputs": ["5\n5 2 1 4 3", "5\n9 8 3 5 7"], "sample_outputs": ["7", "15"], "notes": "NoteFor the first sample you can choose s[4..5]\u2009=\u2009{4,\u20093} and its lucky number is (4\u00a0xor\u00a03)\u2009=\u20097. You can also choose s[1..2].For the second sample you must choose s[2..5]\u2009=\u2009{8,\u20093,\u20095,\u20097}."}, "positive_code": [{"source_code": "sub M { $_[$_[1]>$_[0]]; }\nsub f {\n @a=(1E9);\n for (@_) {\n while ($a[-1]<$_){$r=&M($_^pop(@a),$r);}\n push(@a,$_);\n } $r;\n}\n<>;@l=split / /,<>;\nprint &M(f(@l),f(reverse @l)), \"\\n\";\n"}], "negative_code": [], "src_uid": "c9b9c56d50eaf605e7bc088385a42a1d"} {"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\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u2009105,\u20091\u2009\u2264\u2009m\u2009\u2264\u2009105)\u00a0\u2014 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\u2009\u2264\u2009ai\u2009\u2264\u2009109) 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\u2009\u2264\u2009x\u2009\u2264\u2009n) 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": "$k = ( split ' ', <> )[ 1 ];\n\n%h = map { $_ => $i ++ } split ' ', <>;\n\n@w = split ' ', <>;\n\t\nfor( 1 .. $k ){\n\t@set = <> =~ / \\d+/g;\n\t\n\t$min = ( sort { $a <=> $b } map { $w[ $_ - 1 ] } @set )[ 0 ];\n\t\n\tmap { $w[ $_ - 1 ] = $min } @set;\n\t}\n\nprint 0 + eval join ' + ', map { $w[ $h{ $_ } ] } split ' ', <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k, $m ) = split;\n\t\n\tmy @dict = split ' ', <>;\n\t\n\tmy @w = split ' ', <>;\n\t\n\tmy @groups = map ~~<>, 1 .. $k;\n\t\n\tmy @orig = split ' ', <>;\n\t\n\tfor( @groups ){\n\t\tmy( undef, @set ) = split;\n\t\t\n\t\tmy $min = ( sort { $a <=> $b } map { $w[ $_ - 1 ] } @set )[ 0 ];\n\t\t\n\t\tmap { $w[ $_ - 1 ] = $min } @set;\n\t\t}\n\t\n\tmy $i = 0;\n\t\n\tmy %h = map { $_ => $i ++ } @dict;\n\t\n\tprint 0 + eval join ' + ', map { $w[ $h{ $_ } ] } @orig;\n\t}"}], "negative_code": [], "src_uid": "296552dc2df23b3920baef7d47d0a591"} {"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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile( my $n = <>){\n\t\t\n\tmy $blue_area = ( $n - $n % 2 ) ** 2;\n\t\n\t$_ = join ' ', sort split ' ', <>;\n\t\n\tmy @BLUE;\n\tmy @YELLOW;\n\t\n\ts/\\b(\\d+)(\\s+\\1){3}\\b/ '' x push @BLUE , $1 /ge;\n\ts/\\b(\\d+)(\\s+\\1){1}\\b/ '' x push @YELLOW, $1 /ge;\n\t\n\tmy @GREEN = split;\n\t\n\tpush @YELLOW, ( pop @BLUE ) x 2 while @BLUE * 4 > $blue_area;\n\t\n\tif( $blue_area != @BLUE * 4 or \n\t\t$n % 2 and 1 != @GREEN ){\n\t\tprint \"NO\";\n\t\tnext;\n\t\t}\n\t\n\tmy @MATRIX;\n\t\n\t$n % 2 and @MATRIX = [ grep { defined } ( reverse @YELLOW[ 0 .. $n / 2 - 1 ] ), @GREEN, @YELLOW[ 0 .. $n / 2 - 1 ] ];\n\t\n\twhile( @BLUE ){\n\t\tmy @half = splice @BLUE, 0, $n / 2, ();\n\t\t\n\t\tmy $row = [ grep { defined } @half, pop @YELLOW, reverse @half ];\n\t\t@MATRIX = ( $row, @MATRIX, $row );\n\t\t}\n\t\n\tprint \"YES\";\n\tprint \"@{ $_ }\" for @MATRIX;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile( my $n = <>){\n\t\n\tmy $blue_area = ( $n - $n % 2 ) ** 2;\n\t\n\tmy %count;\n\t\n\tmap { $count{ $_ } ++ } split ' ', <>;\n\t\n\tmy @BLUE;\n\tmy @YELLOW;\n\tmy @GREEN;\n\t\n\tfor my $key ( keys %count ){\n\t\tpush @BLUE, ( $key ) x ( $count{ $key } / 4 );\n\t\tpush @YELLOW, $key if $count{ $key } % 4 >= 2;\n\t\tpush @GREEN, $key if $count{ $key } % 2 == 1;\n\t\t}\n\t\n\tpush @YELLOW, ( pop @BLUE ) x 2 while @BLUE * 4 > $blue_area;\n\t\n\tif( $blue_area != @BLUE * 4 or \n\t\t$n % 2 and 1 != @GREEN ){\n\t\tprint \"NO\";\n\t\tnext;\n\t\t}\n\t\n\tmy @MATRIX;\n\t\n\twhile( @BLUE ){\n\t\tpush @MATRIX, [ splice @BLUE, 0, $n / 2, () ];\n\t\t}\n\t\n\tfor( @MATRIX ){\n\t\tpush @{ $_ }, grep { defined } pop @YELLOW, reverse @{ $_ };\n\t\t}\n\t\n\t@GREEN and push @MATRIX, [ @YELLOW, @GREEN, reverse @YELLOW ];\n\t\n\tpush @MATRIX, reverse @MATRIX[ 0 .. ( $n >> 1 ) - 1 ];\n\t\n\tprint \"YES\";\n\tprint \"@{ $_ }\" for @MATRIX;\n\t}\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 20;\n\t\n\tchomp;\n\tmy $n = $_;\n\t\t\n\tmy %h;\n\t\n\tmap { $h{ $_ } ++ } split ' ', <>;\n\t\t\t\n\tmy @odd_cross;\n\t\n\tif( $n % 2 == 1 ){\n\t\t\n\t\tfor my $key ( sort keys %h ){\n\t\t\tif( $h{ $key } % 2 == 1 ){\n\t\t\t\t$h{ $key } -= 1;\n\t\t\t\tpush @odd_cross, $key;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\tmy $require = $n - 1;\n\t\t\n\t\tfor my $key ( sort keys %h ){\n\t\t\tif( $require > 0 and $h{ $key } % 4 == 2 ){\n\t\t\t\t$h{ $key } -= 2;\n\t\t\t\tpush @odd_cross, $key;\n\t\t\t\t$require --;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\tfor my $key ( sort keys %h ){\n\t\t\twhile( $require > 0 and $h{ $key } % 4 == 0 and $h{ $key } > 0 ){\n\t\t\t\t$h{ $key } -= 4;\n\t\t\t\tpush @odd_cross, ( $key ) x 2;\n\t\t\t\t$require -= 2;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"odd_cross:[@odd_cross]\";\n\t\n\tmy $sum = eval join '+', map { $h{ $_ } } keys %h;\n\tmy $not_tetra = grep { $h{ $_ } % 4 != 0 } keys %h;\n\t\n\t$debug and print \"n:[$n], sum:[$sum], not_tetra:[$not_tetra]\";\n\t\n\tif( $not_tetra or $sum != ( $n - ( $n % 2 ? 1 : 0 ) ) ** 2 ){\n\t\tprint \"NO\";\n\t\tnext;\n\t\t}\n\t\n\tmy @fill = map { ( $_ ) x ( $h{ $_ } / 4 ) } sort keys %h;\n\t\n\t$debug and print \"fill:[@fill]\";\n\t\t\n\tmy @palindrome;\n\t\n\t@odd_cross and @palindrome = [ ( reverse @odd_cross[ 0 .. $n / 2 ] ), @odd_cross[ 1 .. $n / 2 ] ];\n\t\n\twhile( @fill ){\n\t\tmy @line = splice @fill, 0, $n / 2, ();\n\t\t\n\t\tmy $unshift_push = [ @line, @odd_cross ? pop @odd_cross : (), reverse @line ];\n\t\tunshift @palindrome, $unshift_push;\n\t\tpush @palindrome, $unshift_push;\n\t\t}\n\t\n\tprint \"YES\";\n\tprint \"@{ $_ }\" for @palindrome;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile( my $n = <>){\n\t\t\n\tmy $w = ( $n - ( $n % 2 ? 1 : 0 ) ) ** 2;\n\t\n\t$_ = join ' ', sort split ' ', <>;\n\t\n\tmy @f;\n\tmy @c;\n\t\n\ts/\\b(\\d+)(\\s+\\1){3}\\b/ '' x push @f, $1 /ge;\n\ts/\\b(\\d+)(\\s+\\1)\\b/ '' x push @c, $1 /ge;\n\t\n\tpush @c, ( pop @f ) x 2 while @f * 4 > $w;\n\t\n\tunshift @c, split;\n\t\n\tif( $w != @f * 4 or \n\t\t$n % 2 and $n != @c ){\n\t\tprint \"NO\";\n\t\tnext;\n\t\t}\n\t\n\tmy @p;\n\t\n\t@c and @p = [ ( reverse @c[ 0 .. $n / 2 ] ), @c[ 1 .. $n / 2 ] ];\n\t\n\twhile( @f ){\n\t\tmy @l = splice @f, 0, $n / 2, ();\n\t\t\n\t\tmy $up = [ @l, @c ? pop @c : (), reverse @l ];\n\t\t@p = ( $up, @p, $up );\n\t\t}\n\t\n\tprint \"YES\";\n\tprint \"@{ $_ }\" for @p;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile( my $n = <>){\n\t\n\tmy $blue_area = ( $n - $n % 2 ) ** 2;\n\t\n\tmy %count;\n\t\n\tmap { $count{ $_ } ++ } split ' ', <>;\n\t\n\tmy @BLUE;\n\tmy @YELLOW;\n\tmy @GREEN;\n\t\n\tfor my $key ( keys %count ){\n\t\tpush @BLUE, ( $key ) x ( $count{ $key } / 4 );\n\t\tpush @YELLOW, $key if $count{ $key } % 4 == 2;\n\t\tpush @GREEN, $key if $count{ $key } % 2 == 1;\n\t\t}\n\t\n\tpush @YELLOW, ( pop @BLUE ) x 2 while @BLUE * 4 > $blue_area;\n\t\n\tif( $blue_area != @BLUE * 4 or \n\t\t$n % 2 and 1 != @GREEN ){\n\t\tprint \"NO\";\n\t\tnext;\n\t\t}\n\t\n\tmy @MATRIX;\n\t\n\twhile( @BLUE ){\n\t\tpush @MATRIX, [ splice @BLUE, 0, $n / 2, () ];\n\t\t}\n\t\n\tfor( @MATRIX ){\n\t\tpush @{ $_ }, grep { defined } pop @YELLOW, reverse @{ $_ };\n\t\t}\n\t\n\t@GREEN and push @MATRIX, [ @YELLOW, @GREEN, reverse @YELLOW ];\n\t\n\tpush @MATRIX, reverse @MATRIX[ 0 .. ( $n >> 1 ) - 1 ];\n\t\n\tprint \"YES\";\n\tprint \"@{ $_ }\" for @MATRIX;\n\t}\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile( my $n = <> ){\n\t\t\t\n\tmy %h;\n\t\n\tmap { $h{ $_ } ++ } split ' ', <>;\n\t\n\t@_ = map { ( $_->[0] ) x $h{ $_->[0] } } \n\t\t\tsort { $a->[1] <=> $b->[1] } \n\t\t\tmap { [ $_, $h{ $_ } % 4 + $h{ $_ } % 2 * 4 ] }\n\t\t\tkeys %h;\n\t\t\n\tmy $w = ( $n - $n % 2 ) ** 2;\n\t\n\t$_ = join ' ', splice @_, 0, $w, ();\n\ts/\\b(\\d+)( \\1){3}\\b/$1/g;\n\tmy @f = split;\n\t\n\t$_ = \"@_\";\n\ts/\\b(\\d+)( \\1)\\b/$1/g;\n\tmy @c = reverse split;\n\t\n\tif( $w / 4 != @f or $n % 2 and $n != split ){\n\t\tprint \"NO\";\n\t\tnext;\n\t\t}\n\t\n\tmy @p;\n\t\n\t@c and @p = [ ( reverse @c[ 0 .. $n / 2 ] ), @c[ 1 .. $n / 2 ] ];\n\t\n\twhile( @f ){\n\t\tmy @l = splice @f, 0, $n / 2, ();\n\t\t\n\t\tmy $up = [ @l, @c ? pop @c : (), reverse @l ];\n\t\t@p = ( $up, @p, $up );\n\t\t}\n\t\n\tprint \"YES\";\n\tprint \"@{ $_ }\" for @p;\n\t}"}], "src_uid": "20928dd8e512bee2d86c6611c5e76390"} {"nl": {"description": "Codeforces user' handle color depends on his rating\u00a0\u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009100)\u00a0\u2014 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 (\u2009-\u20094000\u2009\u2264\u2009beforei,\u2009afteri\u2009\u2264\u20094000)\u00a0\u2014 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 \u00ab_\u00bb and \u00ab-\u00bb characters. It is guaranteed that all handles are distinct.", "output_spec": "Print \u00abYES\u00bb (quotes for clarity), if Anton has performed good in the contest and \u00abNO\u00bb (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\n($B, $A) = (split)[1,2], $B < $A && $B > 2399 and $ok ++ for <>;\n\nprint $ok ? 'YES' : 'NO'\n"}], "negative_code": [], "src_uid": "3bff9b69423cf6c8425e347a4beef96b"} {"nl": {"description": "Ksenia has an array $$$a$$$ consisting of $$$n$$$ positive integers $$$a_1, a_2, \\ldots, a_n$$$. In one operation she can do the following: choose three distinct indices $$$i$$$, $$$j$$$, $$$k$$$, and then change all of $$$a_i, a_j, a_k$$$ to $$$a_i \\oplus a_j \\oplus a_k$$$ simultaneously, where $$$\\oplus$$$ denotes the bitwise XOR operation. She wants to make all $$$a_i$$$ equal in at most $$$n$$$ operations, or to determine that it is impossible to do so. She wouldn't ask for your help, but please, help her!", "input_spec": "The first line contains one integer $$$n$$$ ($$$3 \\leq n \\leq 10^5$$$)\u00a0\u2014 the length of $$$a$$$. The second line contains $$$n$$$ integers, $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$)\u00a0\u2014 elements of $$$a$$$.", "output_spec": "Print YES or NO in the first line depending on whether it is possible to make all elements equal in at most $$$n$$$ operations. If it is possible, print an integer $$$m$$$ ($$$0 \\leq m \\leq n$$$), which denotes the number of operations you do. In each of the next $$$m$$$ lines, print three distinct integers $$$i, j, k$$$, representing one operation. If there are many such operation sequences possible, print any. Note that you do not have to minimize the number of operations.", "sample_inputs": ["5\n4 2 1 7 2", "4\n10 4 49 22"], "sample_outputs": ["YES\n1\n1 3 4", "NO"], "notes": "NoteIn the first example, the array becomes $$$[4 \\oplus 1 \\oplus 7, 2, 4 \\oplus 1 \\oplus 7, 4 \\oplus 1 \\oplus 7, 2] = [2, 2, 2, 2, 2]$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse Data::Dumper;\n\nmy ($n) = map { $_ - 0 } split(/\\s+/,);\nmy @A = map { $_ - 0 } split(/\\s+/,);\n\nif( $n % 2 == 1 ){\n print \"YES\\n\";\n print ( ($n - 2) . \"\\n\" );\n if( $n == 3 ){\n print \"1 2 3\\n\";\n } else {\n for(my $i=0;$i=0;$i--){\n print ( (1+2*$i) . \" \" . (2+2*$i) . \" \" . (3+2*$i) . \"\\n\" );\n }\n }\n} else {\n my $xs = 0;\n for(my $i=0;$i<$n;$i++){\n $xs = $xs ^ $A[$i];\n }\n if( $xs == 0 ){\n print \"YES\\n\";\n print ( ($n - 3) . \"\\n\" );\n for(my $i=0;$i=0;$i--){\n print ( (1+2*$i) . \" \" . (2+2*$i) . \" \" . (3+2*$i) . \"\\n\" );\n }\n } else {\n print \"NO\\n\";\n }\n}\n\n\n\n\n\n\n\n"}], "negative_code": [], "src_uid": "623f12b8c5a5c850c7edd9875af56f32"} {"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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$)\u00a0\u2014 the number of blocks.", "output_spec": "For each test case, print one integer\u00a0\u2014 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": "#!/usr/bin/perl\r\n\r\nuse POSIX qw/ceil/;\r\nmy $ntests = <>;\r\nwhile (<>) {\r\n chomp(my $n = $_);\r\n print ceil($n/2), \"\\n\";\r\n}"}], "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$$$)\u00a0\u2014 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": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nuse Carp;\n \n# essential\nmy @tokens = ();\n \nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n \nsub say_all {\n my @args = @_;\n say join ' ', @args;\n}\n \nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n \nsub ref_ref_scalar {\n my $ref = shift;\n return 1 if ref($ref) eq 'SCALAR';\n}\n \nsub toggle {\n my $ref = shift;\n croak \"$ref does not reference to scalar\" if !ref_ref_scalar($ref);\n \n $$ref = !$$ref;\n}\n \nsub odd {\n my $num = shift;\n return $num % 2 == 1;\n}\n \nsub even {\n my $num = shift;\n return $num % 2 == 0;\n}\n \nsub sum_of_digits {\n my $n = shift;\n my @numbers = split q{}, $n;\n \n my $sum = 0;\n \n for (@numbers) {\n $sum += $_;\n }\n \n return $sum;\n}\n \n# solution\n \nmy $n = read_line;\n\nfor (1..$n) {\n my ($a, $b) = split q{ }, read_line;\n if ($a == 1 || $b == 1) {\n say 'YES';\n next;\n }\n if ($a == 2 && $b == 2) {\n say 'YES';\n next;\n }\n say 'NO';\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\tprint $n == 2 && $m == 2 || $n == 1 || $m == 1 ? \"YES\" : \"NO\";\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\tprint $n == 2 && $n == 2 || $n == 1 || $m == 1 ? \"YES\" : \"NO\";\n\t}"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 amount of numbers in the sequence. The second line contains n space-separated integer numbers \u2014 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": "my $n = ;\nchomp $n;\n\nmy $int_list = ;\nchomp $int_list;\n\nmy @array = split(\" \",$int_list);\n\nmy $min = $array[0];\n\nmy $sec = \"NO\";\n\nfor my $elem (@array){\n if ($elem < $min){\n $sec = $min;\n $min = $elem;\n }\n elsif (($elem > $min) && (($sec eq \"NO\") ||($sec > $elem))){\n $sec = $elem;\n }\n}\n\nprint $sec;"}, {"source_code": "$n = <>;\n@a = split(' ', <>);\n$m = 123;\nforeach(@a){\n\t$m = $_ if $_ < $m;\n}\n$re = 123;\nforeach(@a){\n\t$re = $_\n\tif $_ > $m and $_ < $re;\n}\nprint ($re != 123 ? $re : \"NO\");"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n<>;\nmy $str = <>;\nchomp($str);\nmy @arr = split(/ /, $str);\nmy $min = $arr[0];\nmy $mmin;\nfor my $i (@arr) {\n if ($i < $min) {\n $mmin = $min;\n $min = $i;\n } elsif ($i > $min) {\n if ( (! defined $mmin) || ($mmin > $i) ) {\n $mmin = $i;\n }\n }\n}\nif (!defined $mmin) {\n print \"NO\";\n} else {\n print $mmin;\n}"}], "negative_code": [{"source_code": "my $n = ;\nchomp $n;\n\nmy $int_list = ;\nchomp $int_list;\n\nmy @array = split(\" \",$int_list);\n\nmy $min = $array[0];\n\nmy $sec = \"NO\";\n\nfor my $elem (@array){\n if ($elem < $min){\n $sec = $min;\n $min = $elem;\n }\n elsif (($elem > $min) && (($sec eq \"NO\") ||($sec > $elem))){\n $sec = $elem;\n }\n}"}, {"source_code": "$n = <>;\n@a = split(' ', <>);\n$m = 123;\nforeach(@a){\n\t$m = $_ if $_ < $m;\n}\nforeach(@a){\n\tprint $_ and exit\n\tif $_ > $m;\n}\nprint \"NO\";"}, {"source_code": "$n = <>;\n@a = split(' ', <>);\nunshift @a, $m = 123;\nforeach(@a){\n\t$m = $_ if $_ < $m;\n}\nfor(1..$n){\n\tprint $_ and exit\n\tif $a[$_] > $m;\n}\nprint \"NO\";"}, {"source_code": "$n = <>;\n@a = split(' ', <>);\nunshift @a, $m = 123;\nforeach(@a){\n\t$m = $_ if $_ < $m;\n}\nfor(1..$n){\n\tprint $_ and exit\n\tif $a[$_] > $m;\n}\nprint \"No\";"}, {"source_code": "$n = <>;\n@a = split(' ', <>);\nunshift @a, $m = 123;\nforeach(@a){\n\t$m = $_ if $_ < $m;\n}\nforeach(@a){\n\tprint $_ and exit\n\tif $_ > $m;\n}\nprint \"NO\";"}], "src_uid": "930be5ec102fbe062062aa23eac75187"} {"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\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105). 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": "while (<>){\n $i=0;\n$_=<>;\nif (/I/){\n s/I//;\n if (/I/){\n print 0;\n \n }\n else {print 1}\n \n }\n else {\n while (/A/g){$i++}\n print $i\n }\n}"}], "negative_code": [], "src_uid": "5e4defe52832cc0d36e7ea5d9f86f895"} {"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\u2009<\u2009a1\u2009<\u2009a2\u2009<\u2009...\u2009<\u2009an) teeth, and the j-th star on the rear wheel axle has bj (0\u2009<\u2009b1\u2009<\u2009b2\u2009<\u2009...\u2009<\u2009bm) teeth. Any pair (i,\u2009j) (1\u2009\u2264\u2009i\u2009\u2264\u2009n;\u00a01\u2009\u2264\u2009j\u2009\u2264\u2009m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i,\u2009j) has a gear ratio, equal to the value .Since Vasya likes integers, he wants to find such gears (i,\u2009j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all \"integer\" gears (i,\u2009j) 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\u2009\u2264\u2009n\u2009\u2264\u200950) \u2014 the number of stars on the bicycle's pedal axle. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009104) in the order of strict increasing. The third input line contains integer m (1\u2009\u2264\u2009m\u2009\u2264\u200950) \u2014 the number of stars on the rear wheel axle. The fourth line contains m integers b1,\u2009b2,\u2009...,\u2009bm (1\u2009\u2264\u2009bi\u2009\u2264\u2009104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i,\u2009j), 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\u2009=\u20094,\u2009b1\u2009=\u200912, and for the other a2\u2009=\u20095,\u2009b3\u2009=\u200915."}, "positive_code": [{"source_code": "chomp($n=);\n@pedal = split /\\s+/, ;\nchomp($m=);\n@wheel = split /\\s+/, ;;\n\n@ratios = (0) x 2000;\n\n$count = 0;\n$largestr = -1;\nforeach $p (@pedal) {\n\tforeach $w (@wheel) {\n\t\tif($w % $p == 0) {\n\t\t\t$r = $w / $p;\n\t\t\t$largestr = $r if($r > $largestr);\n#\t\t\tprint \"wheel $w pedal $p has ratio $r\\n\";\n\t\t\t$ratios[$r] += 1;\n#\t\t\tprint \"This is combination #\".$ratios[$r].\"\\n\";\n\t\t}\n\t}\n}\nprint $ratios[$largestr].\"\\n\";"}], "negative_code": [{"source_code": "chomp($n=);\n@pedal = split /\\s+/, ;\nchomp($m=);\n@wheel = split /\\s+/, ;;\n\n%ratios = { };\n$count = 0;\n$largestr = -1;\nforeach $p (@pedal) {\n\tforeach $w (@wheel) {\n\t\tif($w % $p == 0) {\n\t\t\t$r = $w / $p;\n\t\t\t$largestr = $r if($r > $largestr);\n#\t\t\tprint \"wheel $w pedal $p has ratio $r\\n\";\n\t\t\tdefined $ratios{$r} ? $ratios{$r} += 1 : $ratios{$r} = 1;\n\t\t}\n\t}\n}\nprint $ratios{$largestr}.\"\\n\";"}], "src_uid": "102667eaa3aee012fef70f4192464674"} {"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\u2009=\u2009s1s2... 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\u00a0(1\u2009\u2264\u2009|s|\u2009\u2264\u2009103). The second line contains a single integer k\u00a0(0\u2009\u2264\u2009k\u2009\u2264\u2009103). 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 \u2014 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\u2009=\u20091\u00b71\u2009+\u20092\u00b72\u2009+\u20093\u00b72\u2009+\u20094\u00b72\u2009+\u20095\u00b72\u2009+\u20096\u00b72\u2009=\u200941."}, "positive_code": [{"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# STDIN: dzy_string.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 05/26/2015 09:44:35 AM\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.012;\n\n#open STDIN, \"in.txt\";\nchomp(my $str = );\nmy @st = split //, $str;\nchomp(my $cnt = );\nchomp($_ = );\nmy @alph = split;\nmy %value;\nwhile (my ($inde, $val) = each @alph){\n $value{chr($inde + ord('a'))} = $val;\n}\nmy $sum = 0;\nwhile (my ($inde, $val) = each @st){\n $sum += ($inde + 1) * $value{$val};\n}\nmy @sor_alph = sort {$b <=> $a} @alph;\nmy $len = @st;\nwhile ($cnt) {\n $sum += $sor_alph[0] * ($len + $cnt);\n $cnt--;\n}\nprint $sum . \"\\n\";\n\n"}, {"source_code": "chomp($m = <>);\nchomp($n = <>);\nchomp($num = <>);\n@nums = split (\" \", $num);\n@ms = split //, $m;\n$max = $_ > $max ? $_ : $max for(@nums);\nfor $i(0..$#ms){\n $ans += ($i + 1) * $nums[ord($ms[$i]) - 97];\n}\nfor $i(1..$n) {\n $ans += $max * ($i + 1 + $#ms); \n}\nprint \"$ans\\n\";\n"}, {"source_code": "while(<>){\n\t\n\tchomp;\n\t$n=<>;\n\t@az=split/ /,<>;\n\t\n\t@Az = sort {$b<=>$a} @az;\n#\tprint $Az[0];\n\t\n\ts/./ $&/g;\n\ttr/a-j/0-9/;\n\ts/[k-t]/1$&/g;\n\ts/[u-z]/2$&/g;\n\ttr/k-t/0-9/;\n\ttr/u-z/0-5/;\n\ts/ //;\n\t\n#\tprint \"=$_=\";\n\t\n\t@a=/\\d+/g;\n\t$j=0;\n\t$sum=0;\n\t\n#\tprint \"?@a?\";\n\tfor $i(@a){\n\t\t\n\t\t$j++;\n\t\t$sum += @az[$i] * $j;\n\t\t\n#\t\tprint \"$sum\\n\";\n\t\t}\n\t\t\n\tfor $i(1..$n){\n\t\t$j++;\n\t\t$sum += @Az[0] * $j;\n\t\t\n\t\t}\n#\tprint 'z'-'a';\n\tprint \"$sum\\n\";\n\t}"}], "negative_code": [], "src_uid": "85f90533a9840e944deef9f3b4229cb8"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009106) \u2014 the sum of digits of the required lucky number.", "output_spec": "Print on the single line the result \u2014 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": "# http://codeforces.com/problemset/problem/109/A\n\n\n# read n\nmy $n = ;\t\t# \nchomp ( $n );\t\t\t# \n\nmy $a, $b;\n$a = $b = -1;\nmy %ha;\nmy $found = 0;\n# calculate all possible values of a\n$b = 0;\nwhile ( $n >= (7 * $b) ) {\n if ( ($n - (7 * $b)) % 4 == 0 ) {\n\t$found = 1;\n\t$a = ($n - (7 * $b)) / 4;\n\t$ha{ $a } = $b;}\n $b += 1;}\n\n# find minimum sum of any pair\nmy $min = 100000000;\nmy @k = keys %ha;\nfor (@k) {\n if ( $_ + $ha{ $_ } < $min ) {\n\t$min = $_ + $ha{ $_ };}}\n\n# find pairs with sum equal to minimum\n# and store them in @sel\nmy @filter_one;\n@filter_one = grep { ($_ + $ha{ $_ }) == $min } @k;\n\n# find pair with minimum $b \n$min = 100000000;\nfor (@filter_one) {\n if ( $ha{ $_ } < $min ) {\n\t$min = $ha{ $_ };\n\t$a = $_;\n\t$b = $ha{ $_ };}}\n\n# print answer\nif ( not $found ) { print \"-1\\n\"; }\nelse { print ((\"4\" x $a ) . (\"7\" x $b) . \"\\n\"); }\n\n\n\n"}], "negative_code": [{"source_code": "# http://codeforces.com/problemset/problem/109/A\n\n\n# read n\nmy $n = ;\t\t# \nchomp ( $n );\t\t\t# \n\nmy $a, $b;\n$a = $b = -1;\nmy %ha;\nmy $found = 0;\n# calculate all possible values of a\n$b = 0;\nwhile ( $n > (7 * $b) ) {\n if ( ($n - (7 * $b)) % 4 == 0 ) {\n\t$found = 1;\n\t$a = ($n - (7 * $b)) / 4;\n\t$ha{ $a } = $b;}\n $b += 1;}\n\n# find minimum sum of any pair\nmy $min = 100000000;\nmy @k = keys %ha;\nfor (@k) {\n if ( $_ + $ha{ $_ } < $min ) {\n\t$min = $_ + $ha{ $_ };}}\n\n# find pairs with sum equal to minimum\n# and store them in @sel\nmy @filter_one;\n@filter_one = grep { ($_ + $ha{ $_ }) == $min } @k;\n\n# find pair with minimum $b \n$min = 100000000;\nfor (@filter_one) {\n if ( $ha{ $_ } < $min ) {\n\t$min = $ha{ $_ };\n\t$a = $_;\n\t$b = $ha{ $_ };}}\n\n# print answer\nif ( not $found ) { print \"-1\\n\"; }\nelse { print ((\"4\" x $a ) . (\"7\" x $b) . \"\\n\"); }\n\n\n\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}$$$) \u00a0\u2014 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$$$) \u00a0\u2014 scores of the students.", "output_spec": "For each testcase, output one integer \u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = eval join '+', @_;\n\t\n\tprint $sum > $m ? $m : $sum;\n\t}"}], "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$$$) \u2014 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": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy ($n,$m)=split/ /,;\nmy @a=split/ /,;chomp($m,$a[-1]);\nmy $i=0;my $su=0;my @out;\nfor(0..$n-1){\n $su+=$a[$_];\n if($su>=$m){$i=$su/$m;$su%=$m;}\n push @out,\"$i \";\n $i=0;\n}\nprint \"@out\\n\";"}], "negative_code": [], "src_uid": "a2085c2d21b2dbe4cc27a15fa4a1ec4f"} {"nl": {"description": "Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u,\u2009v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm: Root the tree at node 1. Count the number of nodes at an even depth. Let it be evenCnt. Count the number of nodes at an odd depth. Let it be oddCnt. The answer is the minimum between evenCnt and oddCnt. The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.", "input_spec": "The only line contains an integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009105), the number of nodes in the desired trees.", "output_spec": "The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output \"-1\" (without quotes) for that section only. If the answer for a section exists, it should contain n\u2009-\u20091 lines, each containing 2 space-separated integers u and v (1\u2009\u2264\u2009u,\u2009v\u2009\u2264\u2009n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict. If there are multiple answers, you can print any of them.", "sample_inputs": ["2", "8"], "sample_outputs": ["-1\n1 2", "1 2\n1 3\n2 4\n2 5\n3 6\n4 7\n4 8\n1 2\n1 3\n2 4\n2 5\n2 6\n3 7\n6 8"], "notes": "NoteIn the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed \u2009-\u20091 in the first section, but notice that we printed this tree in the second section.In the second sample:In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: In the second tree, the algorithm will find an answer with 3 nodes which is correct: "}, "positive_code": [{"source_code": "$n=<>;if($n<6){print\"-1\\n\";}else{print\"1 2\\n1 3\\n1 4\\n\";for($i=5;$i<=$n;$i=$i+1){print(\"2 $i\\n\")}}for($i=2;$i<=$n;$i=$i+1){print(\"1 $i\\n\")}"}], "negative_code": [], "src_uid": "b1959af75dfdf8281e70ae66375caa14"} {"nl": {"description": "You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes.You know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consists of several words. For each language, all words are distinct, i.e. they are spelled differently. Moreover, the words of these languages have a one-to-one correspondence, that is, for each word in each language, there exists exactly one word in the other language having has the same meaning.You can write down every word the professor says in either the first language or the second language. Of course, during the lecture you write down each word in the language in which the word is shorter. In case of equal lengths of the corresponding words you prefer the word of the first language.You are given the text of the lecture the professor is going to read. Find out how the lecture will be recorded in your notes.", "input_spec": "The first line contains two integers, n and m (1\u2009\u2264\u2009n\u2009\u2264\u20093000, 1\u2009\u2264\u2009m\u2009\u2264\u20093000) \u2014 the number of words in the professor's lecture and the number of words in each of these languages. The following m lines contain the words. The i-th line contains two strings ai, bi meaning that the word ai belongs to the first language, the word bi belongs to the second language, and these two words have the same meaning. It is guaranteed that no word occurs in both languages, and each word occurs in its language exactly once. The next line contains n space-separated strings c1,\u2009c2,\u2009...,\u2009cn \u2014 the text of the lecture. It is guaranteed that each of the strings ci belongs to the set of strings {a1,\u2009a2,\u2009... am}. All the strings in the input are non-empty, each consisting of no more than 10 lowercase English letters.", "output_spec": "Output exactly n words: how you will record the lecture in your notebook. Output the words of the lecture in the same order as in the input.", "sample_inputs": ["4 3\ncodeforces codesecrof\ncontest round\nletter message\ncodeforces contest letter contest", "5 3\njoll wuqrd\neuzf un\nhbnyiyc rsoqqveh\nhbnyiyc joll joll euzf joll"], "sample_outputs": ["codeforces round letter round", "hbnyiyc joll joll un joll"], "notes": null}, "positive_code": [{"source_code": "($n,$m)=split ' ',<>;\nwhile($m--) {\n ($a,$b)=split ' ',<>;\n $h{$a} = length$a>length$b?$b:$a; \n}\n$_=<>;\ns/(\\w+)/$h{$1}/eg;\nprint"}, {"source_code": "($n,$m)=split ' ',<>;\nwhile($m--) {\n chomp($_=<>);\n ($a,$b)=split ' ';\n $h{$a} = length$a<=length$b?$a:$b; \n}\n$_=<>;\ns/(\\w+)/$h{$1}/eg;\nprint"}, {"source_code": "($n,$m)=split ' ',<>;\nwhile($m--) {\n chomp($_=<>);\n ($a,$b)=split ' ';\n $h{$a} = $b; \n}\nchomp($_=<>);\n@ans = map {$_ = (length$h{$_} < length$_ ? $h{$_} : $_)} split ' ';\nprint \"@ans\";"}, {"source_code": "($n,$m)=split ' ',<>;\nwhile($m--){\n ($a,$b)=split ' ',<>;\n $h{$a}=length$a>length$b?$b:$a;\n}\n$_=<>;\ns/(\\w+)/$h{$1}/eg;\nprint"}, {"source_code": "($n,$m)=split ' ',<>;\nwhile($m--){\n ($a,$b)=split ' ',<>;\n $h{$a}=length$a>length$b?$b:$a;\n}\n$_=<>;\ns/(\\w+)/$h{$1}/eg;\nprint\n"}, {"source_code": "($n, $m) = split \" \", <>;\n%k = ();\nfor (0 .. $m - 1) {\n ($a, $b) = split \" \", <>;\n $k{$a} = $b;\n}\n@a = split \" \", <>;\nfor (@a) {\n print length $_ <= length $k{$_}? \"$_ \" : \"$k{$_} \";\n}"}, {"source_code": "($n,$m)=split ' ',<>;\nwhile($m--){\n ($a,$b)=split ' ',<>;\n $h{$a}=length$a>length$b?$b:$a;\n}\n$_=<>;\ns/(\\w+)/$h{$1}/eg;\nprint\n"}, {"source_code": "($n,$m)=split ' ',<>;\nwhile($m--){\n ($a,$b)=split ' ',<>;\n $h{$a}=length$a>length$b?$b:$a;\n}\n$_=<>;\ns/(\\w+)/$h{$1}/eg;\nprint"}, {"source_code": "($n,$m)=split ' ',<>;\nwhile($m--){\n ($a,$b)=split ' ',<>;\n $h{$a}=length$a>length$b?$b:$a;\n}\n$_=<>;\ns/(\\w+)/$h{$1}/eg;\nprint\n"}, {"source_code": "($n,$m)=split ' ',<>;\nwhile($m--){\n ($a,$b)=split ' ',<>;\n $h{$a}=length$a>length$b?$b:$a;\n}\n$_=<>;\ns/(\\w+)/$h{$1}/eg;\nprint\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n($n, $m) = split / /, <>;\nforeach (1 .. $m) {\n\tchomp($line = <>);\n\t@a = split / /, $line;\n\tif (length $a[0] <= length $a[1]) {\n\t\t$tb{$a[0]} = $a[0];\n\t\t$tb{$a[1]} = $a[0];\n\t} else {\n\t\t$tb{$a[0]} = $a[1];\n\t\t$tb{$a[1]} = $a[1];\n\t}\n}\nchomp($line = <>);\nprint \"$tb{$_} \" foreach (split / /, $line);"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: b.pl\n#\n# USAGE: ./b.pl \n#\n# DESCRIPTION: \n#\n#\n# OPTIONS: ---\n# REQUIREMENTS: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: YOUR NAME (), \n# ORGANIZATION: \n# VERSION: 1.0\n# CREATED: 1/14/2015 7:34:13 PM\n# REVISION: ---\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.012;\n\nchomp($_ = );\nmy ($n, $m) = split(/ /);\nmy %words;\nwhile ($m--)\n{\n\tchomp($_ = );\n\tmy ($a, $b) = split(/ /);\n\t$words{$a} = $b;\n}\nchomp(my $sentence = );\nmy @res = split(/ /, $sentence);\nfor my $item (@res) {\n\tif (length($item) <= length($words{$item})) {\n\t\tprint \"$item \";\n\t}\n\telse {\n\t\tprint \"$words{$item} \";\n\t}\n}\nprint \"\\n\";\n"}, {"source_code": "($n,$m)=split ' ',<>;\nwhile($m--){\n ($a,$b)=split ' ',<>;\n $h{$a}=length$a>length$b?$b:$a;\n}\n$_=<>;\ns/(\\w+)/$h{$1}/eg;\nprint\n"}, {"source_code": "($n,$m)=split ' ',<>;\nwhile($m--){\n ($a,$b)=split ' ',<>;\n $h{$a}=length$a>length$b?$b:$a;\n}\n$_=<>;\ns/(\\w+)/$h{$1}/eg;\nprint\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy ( $n, $m ) = split ( \" \", <> );\nmy %map;\nwhile ( $m-- ){\n\tmy ( $a, $b ) = split ( \" \", <> );\n\t$map{$a} = $b;\n}\n\nprint join ( \" \", map ( ( length $_ <= length $map{$_} ? $_ : $map{$_} ), split ( \" \", <> ) ) ).\"\\n\";\n"}, {"source_code": "($n,$m)=split ' ',<>;\nwhile($m--){\n ($a,$b)=split ' ',<>;\n $h{$a}=length$a>length$b?$b:$a;\n}\n$_=<>;\ns/(\\w+)/$h{$1}/eg;\nprint\n"}, {"source_code": "$\\ = $/;\nwhile (<>){\n\t($n, $m)=split;\n\tundef %h;\n\tfor $i (1..$m){\n\t\t($first, $second) = split \" \", <>;\n\t\t$value = length $second < length $first ? $second : $first;\n\t\t$h{$first} = $value;\n\t\t}\n\t$, = $\";\n#\tprint keys %h;\n#\tprint values %h;\n\t\n\t$_ = <>;\n\ts/(\\w+)/$h{$1}/eg;\n\n\tprint\n\t}\n"}, {"source_code": "($n, $m)=split \" \", <>;\nfor (1..$m){\n\t($f, $s) = split \" \", <>;\n\t$h{$f} = length $s < length $f ? $s : $f;\n\t}\n$_ = <>;\ns/(\\w+)/$h{$1}/eg;\nprint $_.$/"}, {"source_code": "($n,$m)=split ' ',<>;\nwhile($m--){\n ($a,$b)=split ' ',<>;\n $h{$a}=length$a>length$b?$b:$a;\n}\n$_=<>;\ns/(\\w+)/$h{$1}/eg;\nprint\n"}], "negative_code": [{"source_code": "($n,$m)=split ' ',<>; \nwhile($m--) {\n chomp($_=<>);\n ($a,$b)=split ' ';\n $h{$a} = length$a;\ns/(\\w+)/$h{$1}/eg;\nprint"}], "src_uid": "edd556d60de89587f1b8daa893538530"} {"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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\nuse integer;\n\n<>;\n\nwhile (<>) {\n\tchomp;\n\tif (is_homo($_)) {\n\t\tprint \"-1\\n\";\n\t} else {\n\t\tmy @chars = split //;\n\t\tprint sort @chars;\n\t\tprint \"\\n\";\n\t}\n}\n\nsub is_homo {\n\tmy $string = shift;\n\tmy @chars = split //, $string;\n\tmy $is_homo = 1;\n\n\tif (@chars > 0) {\n\t\tmy $prev = $chars[0];\n\t\tfor (my $i = 1; $i < @chars; $i++) {\n\t\t\t$is_homo = $is_homo && $chars[$i] eq $prev;\n\t\t\t$prev = $chars[$i];\n\t\t}\n\t}\n\treturn $is_homo;\t\n}\n\nsub is_palindrome {\n\tmy $string = shift;\n\tmy @chars = split //, $string;\n\tmy $radius = @chars / 2;\n\n\tmy $is_palindrome = 1;\n\tfor (my $i = 0; $i < $radius; $i++) {\n\t\t$is_palindrome = $is_palindrome && $chars[$i] eq $chars[$#chars - $i];\n\t}\n\treturn $is_palindrome;\t\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\t$_ = join '', sort split //;\n\t\n\tprint /^(.)\\1*$/ ? -1 : $_;\n\t}"}], "negative_code": [], "src_uid": "b9f0c38c6066bdafa2e4c6daf7f46816"} {"nl": {"description": "The stardate is 1983, and Princess Heidi is getting better at detecting the Death Stars. This time, two Rebel spies have yet again given Heidi two maps with the possible locations of the Death Star. Since she got rid of all double agents last time, she knows that both maps are correct, and indeed show the map of the solar system that contains the Death Star. However, this time the Empire has hidden the Death Star very well, and Heidi needs to find a place that appears on both maps in order to detect the Death Star.The first map is an N\u2009\u00d7\u2009M grid, each cell of which shows some type of cosmic object that is present in the corresponding quadrant of space. The second map is an M\u2009\u00d7\u2009N grid. Heidi needs to align those two maps in such a way that they overlap over some M\u2009\u00d7\u2009M section in which all cosmic objects are identical. Help Heidi by identifying where such an M\u2009\u00d7\u2009M section lies within both maps.", "input_spec": "The first line of the input contains two space-separated integers N and M (1\u2009\u2264\u2009N\u2009\u2264\u20092000, 1\u2009\u2264\u2009M\u2009\u2264\u2009200, M\u2009\u2264\u2009N). The next N lines each contain M lower-case Latin characters (a-z), denoting the first map. Different characters correspond to different cosmic object types. The next M lines each contain N characters, describing the second map in the same format. ", "output_spec": "The only line of the output should contain two space-separated integers i and j, denoting that the section of size M\u2009\u00d7\u2009M in the first map that starts at the i-th row is equal to the section of the second map that starts at the j-th column. Rows and columns are numbered starting from 1. If there are several possible ways to align the maps, Heidi will be satisfied with any of those. It is guaranteed that a solution exists.", "sample_inputs": ["10 5\nsomer\nandom\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nnoise\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nnoisehctwo"], "sample_outputs": ["4 6"], "notes": "NoteThe 5-by-5 grid for the first test case looks like this: maytheforcebewithyouhctwo"}, "positive_code": [{"source_code": "$/ = undef; $_ = <>; ($n, $m, @a) = split; @b = splice @a, $n;\n\nfor ($i = 0; $i < $n - $m + 1; ++$i) {\n\t$key = join \"\", @a[$i .. $i + $m - 1];\n\t$hash{$key} = $i + 1;\n}\n\nfor ($j = 0; $j < $n - $m + 1; ++$j) {\n\t$key = join \"\", map { substr($_, $j, $m) } @b;\n\tif ($i = $hash{$key}) {\n\t\t$j += 1;\n\t\tprint \"$i $j\\n\";\n\t\tlast;\n\t}\n}"}], "negative_code": [], "src_uid": "6d0bc28aa0b47c12438a84e57cd8e081"} {"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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B ) = split;\n\t\n\tmap { $_ = sprintf \"%021b\", $_ } $A, $B;\n\t\n\t$B = reverse $B;\n\t\n\t$_ = $A =~ s/.\\K/ ( chop $B ) . ',' /ger;\n\t\n\tmy $flag = -1;\n\t\n\ts/^(00,|11,|01,(?{ $flag ++; }))*01,//;\n\t\n\tmy $len = ( length ) / 3;\n\t\n\t$A = $_;\n\t$B = '';\n\t\n\t$A =~ s/.\\K(.),(?{ $B .= $1; })//g;\n\t\n\tmy $_1 = $B =~ m/1/;\n\t\n\t$_ = eval \"0b$_\" for $A, $B;\n\t\n\tprint +( sort { $a <=> $b } \n\t\t1 + $A - $B, \n\t\t( 1 << $len ) - $A + ( $flag || $_1 ? 1 : 0 )\n\t\t)[ 0 ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B ) = split;\n\t\n\tmap { $_ = sprintf \"%021b\", $_ } $A, $B;\n\t\n\t$B = reverse $B;\n\t\n\t$_ = $A =~ s/.\\K/ ( chop $B ) . ',' /ger;\n\t\n\ts/^(00,|11,|01,)*0\\K1/X/;\n\t\n\tmy $c = 0;\n\t\n\t1 while 0\n\t\t|| s/^00,//\n\t\t|| s/^11,//\n\t\t|| s/^01,// && ++ $c\n\t\t;\n\t\n\tmy $len = ( length ) / 3;\n\t\n\ts/0X,//;\n\t\n\t$A = $_;\n\t$B = '';\n\t\n\t$A =~ s/.\\K(.),(?{ $B .= $1; })//g;\n\t\n\tmy $cB = $B;\n\t\n\t$_ = eval \"0b$_\" for $A, $B;\n\t\n\tprint +( sort { $a <=> $b } \n\t\t1 + $A - $B, \n\t\t( ( $c > 0 || $cB =~ m/1/ ) ? 1 : 0 ) - $A + ( 1 << ( $len - 1 ) ) \n\t\t)[ 0 ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $A, $B ) = split;\n\t\n\t$debug and print \"$A $B\";\n\t\n\tmap { $_ = sprintf \"%021b\", $_ } $A, $B;\n\t\n\t$B = reverse $B;\n\t\n\t$_ = $A =~ s/.\\K/ ( chop $B ) . ',' /ger;\n\t\n\t$debug and print;\n\t\n\ts/^(00,|11,|01,)*0\\K1/X/;\n\t\n\t$debug and print;\n\t\n\tmy $c = 0;\n\t\n\t1 while 0\n\t\t|| s/^00,//\n\t\t|| s/^11,//\n\t\t|| s/^01,// && ++ $c\n\t\t;\n\t\n\t$debug and print;\n\t\n\tmy $len = ( length ) / 3;\n\t\n\ts/0X,//;\n\t\n\t$A = $_;\n\t$B = '';\n\t\n\t$A =~ s/.\\K(.),(?{ $B .= $1; })//g;\n\t\n\t$debug and print $A;\n\t$debug and print $B;\n\t\n\tmy $copy_B = $B;\n\t\n\t$_ = eval \"0b$_\" for $A, $B;\n\t\n\t$debug and print \"$A $B\";\n\t\n\tprint +( sort { $a <=> $b } \n\t\t1 + $A - $B, \n\t\t( ( $c > 0 || $copy_B =~ m/1/ ) ? 1 : 0 ) - $A + ( 1 << ( $len - 1 ) ) \n\t\t)[ 0 ];\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $A, $B ) = split;\n\t\n\t$debug and print \"$A $B\";\n\t\n\t$_ = sprintf \"%021b\", $_ for $A, $B;\n\t\n\tmy $len;\n\tmy $c = 0;\n\t\n\tOUTER:\n\twhile( 1 ){\n\t\twhile( $A =~ m/^0/ and $B =~ m/^0/ ){\n\t\t\t$A =~ s/.//;\n\t\t\t$B =~ s/.//;\n\t\t\tnext OUTER;\n\t\t\t}\n\t\t\n\t\twhile( $A =~ m/^1/ and $B =~ m/^1/ ){\n\t\t\t$A =~ s/.//;\n\t\t\t$B =~ s/.//;\n\t\t\tnext OUTER;\n\t\t\t}\n\t\t\n\t\twhile( $A =~ m/^0/ and $B =~ m/^1/ ){\n\t\t\n\t\t\t$len = length $A;\n\t\t\t$c ++;\n\t\t\t\n\t\t\t$A =~ s/.//;\n\t\t\t$B =~ s/.//;\n\t\t\t\n\t\t\tnext OUTER;\n\t\t\t}\n\t\t\t\n\t\tlast;\n\t\t}\n\t\n\t$debug and print $A;\n\t$debug and print $B;\n\t\n\tmy $copy_B = $B;\n\t\n\t$_ = eval \"0b$_\" for $A, $B;\n\t\n\t$debug and print \"$A $B\";\n\t\n\tprint +( sort { $a <=> $b } \n\t\t1 + $A - $B, \n\t\t( $c > 1 || $copy_B =~ m/1/ ? 1 : 0 ) + 0 - $A + ( 1 << ( $len - 1 ) ) \n\t\t)[ 0 ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $A, $B ) = split;\n\t\n\t$debug and print \"$A $B\";\n\t\n\t$_ = sprintf \"%021b\", $_ for $A, $B;\n\t\n\tmy $len;\n\tmy $c = 0;\n\t\n\tOUTER:\n\twhile( 1 ){\n\t\twhile( $A =~ m/^0/ and $B =~ m/^0/ ){\n\t\t\t$A =~ s/.//;\n\t\t\t$B =~ s/.//;\n\t\t\tnext OUTER;\n\t\t\t}\n\t\t\n\t\twhile( $A =~ m/^1/ and $B =~ m/^1/ ){\n\t\t\t$A =~ s/.//;\n\t\t\t$B =~ s/.//;\n\t\t\tnext OUTER;\n\t\t\t}\n\t\t\n\t\twhile( $A =~ m/^0/ and $B =~ m/^1/ ){\n\t\t\n\t\t\t$len = length $A;\n\t\t\t$c ++;\n\t\t\t\n\t\t\t$A =~ s/.//;\n\t\t\t$B =~ s/.//;\n\t\t\t\n\t\t\tnext OUTER;\n\t\t\t}\n\t\t\t\n\t\tlast;\n\t\t}\n\t\n\t$debug and print $A;\n\t$debug and print $B;\n\t\n\t$_ = eval \"0b$_\" for $A, $B;\n\t\n\t$debug and print \"$A $B\";\n\t\n\tprint +( sort { $a <=> $b } \n\t\t1 + $A - $B, \n\t\t( $c > 1 ? 1 : 0 ) + 0 - $A + ( 1 << ( $len - 1 ) ) \n\t\t)[ 0 ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $A, $B ) = split;\n\t\n\t$debug and print \"$A $B\";\n\t\n\t$_ = sprintf \"%021b\", $_ for $A, $B;\n\t\n\tmy $len;\n\tmy $c = 0;\n\t\n\tOUTER:\n\twhile( 1 ){\n\t\twhile( $A =~ m/^0/ and $B =~ m/^0/ ){\n\t\t\t$A =~ s/.//;\n\t\t\t$B =~ s/.//;\n\t\t\tnext OUTER;\n\t\t\t}\n\t\t\n\t\twhile( $A =~ m/^1/ and $B =~ m/^1/ ){\n\t\t\t$A =~ s/.//;\n\t\t\t$B =~ s/.//;\n\t\t\tnext OUTER;\n\t\t\t}\n\t\t\n\t\twhile( $A =~ m/^0/ and $B =~ m/^1/ ){\n\t\t\n\t\t\t$len = length $A;\n\t\t\t$c ++;\n\t\t\t\n\t\t\t$A =~ s/.//;\n\t\t\t$B =~ s/.//;\n\t\t\t\n\t\t\tnext OUTER;\n\t\t\t}\n\t\t\t\n\t\tlast;\n\t\t}\n\t\n\t$debug and print $A;\n\t$debug and print $B;\n\t\n\t$_ = eval \"0b$_\" for $A, $B;\n\t\n\t$debug and print \"$A $B\";\n\t\n\tprint +( sort { $a <=> $b } \n\t\t1 + $A - $B, \n\t\t( $c > 1 ? 1 : 0 ) + $B - $A + ( 1 << ( $len - 1 ) ) \n\t\t)[ 0 ];\n\t}"}], "src_uid": "eed751c881767ecd978a728d980da594"} {"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\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$\u00a0\u2014 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\u00a0\u2014 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": "$t=readline STDIN;\nchomp $t;\n while ($t) {\n $n= readline STDIN; chomp $n;\n @line=split / /, readline STDIN; chomp @line;\n \n my $min;\n while($l=shift @line) {\n foreach $k (@line) {\n $min=abs($k-$l) if abs($k-$l)<$min or !defined $min;\n last if !$min;\n }\n last if !min;\n }\n \n print $min;\n print \"\\n\";\n $t--;\n }"}, {"source_code": "$t=readline STDIN;\nchomp $t;\n while ($t) {\n $n= readline STDIN; chomp $n;\n @line=split / /, readline STDIN; chomp @line;\n \n my $min;\n while($l=shift @line) {\n foreach $k (@line) {\n $min=abs($k-$l) if abs($k-$l)<$min or !defined $min;\n last if !$min;\n }\n }\n \n print $min;\n print \"\\n\";\n $t--;\n }\n"}], "negative_code": [], "src_uid": "d2669f85bdb59715352c6bc3d7ba9652"} {"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\u2009\u2264\u2009n\u2009\u2264\u20091000)\u00a0\u2014 the number of digits on the display. The second line contains n digits\u00a0\u2014 the initial state of the display.", "output_spec": "Print a single line containing n digits\u00a0\u2014 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": "chomp (my $n = );\nchomp (my @arr = split /\\s*/, );\n\nmy @res;\nforeach my $i (0 .. 9) {\n $res[$i] = [map {(++$_) % 10} @arr];\n}\n\nsub isMin {\n foreach (0 .. (@arr - 1)) {\n return 1 if (${$_[0]}[$_] < $arr[$_]);\n return 0 if (${$_[0]}[$_] > $arr[$_]);\n }\n return 0;\n}\n\nforeach my $i (0 .. 9) {\n foreach (1 .. @arr) {\n push @{$res[$i]}, shift @{$res[$i]};\n if (&isMin ($res[$i])) {\n @arr = @{$res[$i]};\n }\n }\n}\n\nprintf \"%s\\n\", join \"\", @arr;\n"}], "negative_code": [], "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 \u2014 $$$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$$$ \u2014 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$$$) \u2014 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)\u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t( $n, $c, $_ ) = split ' ', $_ . <>;\r\n\t$_ x= 2;\r\n\t\r\n\tprint $c eq 'g' ? 0 : ( sort { $b <=> $a } map length, /$c\\K.*?g/g )[ 0 ]\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $c ) = split;\n\t$_ = <>, chomp;\n\t$_ x= 2;\n\t\n\tif( $c eq 'g' ){\n\t\tprint 0;\n\t\tnext;\n\t\t}\n\t\n\tprint -1 + ( sort { $b <=> $a } map length, m/$c.*?g/g )[ 0 ];\n\t}"}], "negative_code": [], "src_uid": "9d3ee1b292a2402bb2204ab85dcab587"} {"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$$$ \u2014 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$$$) \u2014 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$$$) \u2014 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": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $t = );\n\nfor (1..$t) {\n chomp (my $n = );\n chomp (my $line = );\n my @prices = split q{ }, $line;\n my $i = 1;\n my @prices_and_order\n = map { [$i++,$_] } @prices;\n\n @prices_and_order \n = sort { $a->[1] <=> $b->[1] } @prices_and_order;\n\n my $pao = shift @prices_and_order;\n my $max = $pao->[0];\n my $answer = 0;\n for ( @prices_and_order ) {\n if ( $pao->[1] == $_->[1] ) {\n if ( $_->[0] > $max ) {\n $max = $_->[0];\n }\n else {\n $answer++;\n }\n }\n else {\n if ( $max > $_->[0] ) {\n $answer++;\n }\n else {\n $max = $_->[0];\n }\n }\n $pao = $_;\n }\n say $answer;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"\\@_: @_\";\n\t\n\t@_ = reverse @_;\n\t\n\tmy @min;\n\t\n\t$min[ 0 ] = $_[ 0 ];\n\t\n\tmy $min = $min[ 0 ];\n\t\n\tfor my $i ( 1 .. @_ - 1 ){\n\t\t$min > $_[ $i ] and $min = $_[ $i ];\n\t\t$min[ $i ] = $min;\n\t\t}\n\t\n\t@min = reverse @min;\n\t@_ = reverse @_;\n\t\n\t$debug and print \"\\@min: @min\";\n\t\n\tmy $cnt = 0;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\t$cnt += $_[ $i ] > $min[ $i ];\n\t\t}\n\t\n\tprint $cnt;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $t = );\n\nfor (1..$t) {\n chomp (my $n = );\n chomp (my $line = );\n my @prices = split q{ }, $line;\n my $i = 1;\n my @prices_and_order\n = map { [$i++,$_] } @prices;\n\n @prices_and_order \n = sort { $a->[1] <=> $b->[1] } @prices_and_order;\n\n my $pao = shift @prices_and_order;\n my $max = $pao->[0];\n my $answer = 0;\n for ( @prices_and_order ) {\n if ( $pao->[1] == $_->[1] ) {\n }\n else {\n if ( $max > $_->[0] ) {\n $answer++;\n }\n else {\n $max = $_->[0];\n }\n }\n $pao = $_;\n }\n say $answer;\n}\n"}, {"source_code": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $t = );\n\nfor (1..$t) {\n chomp (my $n = );\n chomp (my $line = );\n my @prices = split q{ }, $line;\n my $i = 1;\n my @prices_and_order\n = map { [$i++,$_] } @prices;\n\n @prices_and_order \n = sort { $a->[1] <=> $b->[1] } @prices_and_order;\n\n my $pao = shift @prices_and_order;\n my $max = $pao->[0];\n my $answer = 0;\n for ( @prices_and_order ) {\n if ( $pao->[1] == $_->[1] ) {\n $max = $_->[0];\n }\n else {\n if ( $max > $_->[0] ) {\n $answer++;\n }\n else {\n $max = $_->[0];\n }\n }\n $pao = $_;\n }\n say $answer;\n}\n"}], "src_uid": "09faf19627d2ff00c3821d4bc2644b63"} {"nl": {"description": "The map of Berland is a rectangle of the size n\u2009\u00d7\u2009m, which consists of cells of size 1\u2009\u00d7\u20091. 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200950, 0\u2009\u2264\u2009k\u2009\u2264\u200950)\u00a0\u2014 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 \u2014 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 \u2014 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 \u2014 the first consists of the cells (2,\u20092) and (2,\u20093), the second consists of the cell (4,\u20093). 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) = split \" \", <>; \n@s = <>; chomp @s; y/*./01/ for @s;\n@n = 0..$n-1;\n\nfor (@n) {\n\twhile ($s[$_] =~ /1+/g) {\n\t\t$l = length $&;\n\t\t$f = $_==0 || $_==$n-1 || $-[0]==0 || $+[0]==$m;\n\t\t$a = [(0 x $m) x $n, $f? $n*$m: $l];\n\t\tsubstr($$a[$_], $-[0], $l, $&);\n\t\t@N = ();\n\t\tfor $b (@L) {\n\t\t\tif ($_ && ($$a[$_] & $$b[$_-1]) =~ /1/) {\n\t\t\t\t$$a[$_] |= $$b[$_] for @n;\n\t\t\t\t$$a[$n] += $$b[$n];\n\t\t\t} else {\n\t\t\t\tpush @N, $b;\n\t\t\t}\n\t\t}\n\t\t@L = (@N, $a);\n\t}\n}\n\nsub w { $_[0]->[$n] }\n@L = grep w($_) < $n*$m, @L;\n@L = sort { w($a) <=> w($b) } @L;\n\n$k = @L - $k; $c = 0;\nfor (1..$k) {\n\t$l = shift @L;\n\t$c += w($l);\n\ty/10/01/ for @$l;\n\t$s[$_] &= $$l[$_] for @n;\n}\n\ny/01/*./ for @s;\nprint \"$c\\n\", join(\"\\n\", @s);\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m, $k) = split;\n\t@A = ();\n\tpush @A, [ (-1) x (2 + $m) ];\n\tfor $i (1 .. $n){\n\t\t$_ = <>, chomp;\n\t\tpush @A, [ -1, (split //), -1 ];\n\t\t}\n\tpush @A, [ (-1) x (2 + $m) ];\n\t\n#\tprint @$_ for @A;\n\t\n\t$is_point = 1;\n\t$next = 1;\n\t%NO = ();\n\t%OCC = ();\n\t\n\twhile($is_point){\n\t\t$found = 0;\n\t\tfor $i (0 .. $n+1){\n\t\t\tfor $j (0 .. $m+1){\n\t\t\t\t$A[$i][$j] eq '.' and do {\n\t\t\t\t\t\t$found ++;\n\t\t\t\t\t\t($x, $y) = ($i, $j);\n\t\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t$found and last;\n\t\t\t}\n\t\t\n\t\tif ($found){\n\t\t\t$A[$x][$y] = $next;\n\t\t\tpush @try, ($x-1, $y), ($x, $y-1), ($x+1, $y), ($x, $y+1);\n\t\t\twhile( @try ){\n\t\t\t\t($xx, $yy) = (shift @try, shift @try);\n\t\t\t\tif ($A[$xx][$yy] eq '.'){\n\t\t\t\t\t$A[$xx][$yy] = $next;\n\t\t\t\t\tpush @try, ($xx-1, $yy), ($xx, $yy-1), ($xx+1, $yy), ($xx, $yy+1);\n\t\t\t\t\t}\n\t\t\t\telsif ($A[$xx][$yy] eq '-1'){\n\t\t\t\t\t$NO{ $next } ++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\telse {$is_point = 0}\n\t\t\n\t\t$next ++;\n\t\t}\n\t\t\n#\tprint @$_ for @A;\n\t\n#\tprint join ' ', keys %NO;\n#\tprint $next;\n\t\n\tfor $c (1 .. $next){\n\t\t$NO{ $c } and next;\n\t\t$occ = 0;\n\t\tfor (@A){ \n\t\t\t$occ += grep $_ == $c, @{$_};\n\t\t\t}\n\t\t$occ and $OCC{ $c } = $occ;\n\t\t}\n\t\n#\tprint \"$_ -> $OCC{$_}\" for keys %OCC;\n\t\n\t$K = keys %OCC;\n\t%delete = ('*' => 1, map { $_, 1 } (sort { $OCC{$b} <=> $OCC{$a} } keys %OCC)[ $k .. $K - 1 ] );\n\n#\tprint \"K k[$K $k]\";\n\t\n\t$del = eval join '+', map { $OCC{$_} } grep /\\d/, keys %delete;\n\t\n\tprint 0+$del;\n\t\n\tfor $i (1 .. $n){\n\t\tfor $j (1 .. $m){\n\t\t\t$A[$i][$j] = $delete{ $A[$i][$j] } ? '*' : '.'\n\t\t\t}\n\t\tprint @{$A[$i]}[1 .. $m];\n\t\t\n\t\t}\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m, $k) = split;\n\t@A = 0;\n\tfor $i (1 .. $n){\n\t\t$_ = <>, chomp;\n\t\tpush @A, [ undef, split // ];\n\t\t}\n\n\t$is = 1;\n\t$next = 1;\n\t%NO = ();\n\t%OCC = ();\n\t\n\twhile($is){\n\t\t$found = 0;\n\t\tfor $i (0 .. $n+1){\n\t\t\tfor $j (0 .. $m+1){\n\t\t\t\t$A[$i][$j] eq '.' and do {\n\t\t\t\t\t\t$found ++;\n\t\t\t\t\t\t($x, $y) = ($i, $j);\n\t\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t$found and last;\n\t\t\t}\n\t\t\n\t\tif ($found){\n\t\t\tpush @try, ($x, $y);\n\t\t\twhile( @try ){\n\t\t\t\t($x, $y) = splice @try, 0, 2;\n\t\t\t\tif ($A[$x][$y] eq '.'){\n\t\t\t\t\t$A[$x][$y] = $next;\n\t\t\t\t\tpush @try, ($x-1, $y), ($x, $y-1), ($x+1, $y), ($x, $y+1);\n\t\t\t\t\t}\n\t\t\t\telsif (!defined $A[$x][$y]){\n\t\t\t\t\t$NO{ $next } ++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\telse {$is = 0}\n\t\t\n\t\t$next ++;\n\t\t}\n\t\n\tfor $c (1 .. $next){\n\t\t$NO{ $c } and next;\n\t\t$occ = 0;\n\t\tfor (@A){ \n\t\t\t$occ += grep $_ == $c, @{$_};\n\t\t\t}\n\t\t$occ and $OCC{ $c } = $occ;\n\t\t}\n\t\n\t$K = keys %OCC;\n\t%delete = ('*' => 1, map { $_, 1 } (sort { $OCC{$b} <=> $OCC{$a} } keys %OCC)[ $k .. $K - 1 ] );\n\n\tprint 0 + eval join '+', map { $OCC{$_} } grep /\\d/, keys %delete;\n\t\n\tfor $i (1 .. $n){\n\t\tfor $j (1 .. $m){\n\t\t\t$A[$i][$j] = $delete{ $A[$i][$j] } ? '*' : '.'\n\t\t\t}\n\t\tprint @{$A[$i]}[1 .. $m];\n\t\t\n\t\t}\n\t}"}], "negative_code": [{"source_code": "($n, $m, $k) = split \" \", <>; \n@s = <>; chomp @s; y/*./01/ for @s;\n@n = 0..$n-1;\n\nfor (@n) {\n\twhile ($s[$_] =~ /1+/g) {\n\t\t$l = length $&;\n\t\t$f = $_==0 || $_==$n-1 || $-[0]==0 || $+[0]==$m;\n\t\t$a = [(0 x $m) x $n, $f? $n*$m: $l];\n\t\tsubstr($$a[$_], $-[0], $l, $&);\n\t\t@N = ();\n\t\tfor $b (@L) {\n\t\t\tif (($$a[$_] & $$b[$_-1]) =~ /1/) {\n\t\t\t\t$$a[$_] |= $$b[$_] for @n;\n\t\t\t\t$$a[$n] += $$b[$n];\n\t\t\t} else {\n\t\t\t\tpush @N, $b;\n\t\t\t}\n\t\t}\n\t\t@L = (@N, $a);\n\t}\n}\n\nsub w { $_[0]->[$n] }\n@L = grep w($_) < $n*$m, @L;\n@L = sort { w($a) <=> w($b) } @L;\n\n$k = @L - $k; $c = 0;\nfor (1..$k) {\n\t$l = shift @L;\n\t$c += w($l);\n\ty/10/01/ for @$l;\n\t$s[$_] &= $$l[$_] for @n;\n}\n\ny/01/*./ for @s;\nprint \"$c\\n\", join(\"\\n\", @s);\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m, $k) = split;\n\t@A = ();\n\tpush @A, [ (-1) x (2 + $m) ];\n\tfor $i (1 .. $n){\n\t\t$_ = <>, chomp;\n\t\tpush @A, [ -1, (split //), -1 ];\n\t\t}\n\tpush @A, [ (-1) x (2 + $m) ];\n\t\n#\tprint @$_ for @A;\n\t\n\t$is_point = 1;\n\t$next = 1;\n\t%NO = ();\n\t%OCC = ();\n\t\n\twhile($is_point){\n\t\t$found = 0;\n\t\tfor $i (0 .. $n+1){\n\t\t\tfor $j (0 .. $m+1){\n\t\t\t\t$A[$i][$j] eq '.' and do {\n\t\t\t\t\t\t$found ++;\n\t\t\t\t\t\t($x, $y) = ($i, $j);\n\t\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t$found and last;\n\t\t\t}\n\t\t\n\t\tif ($found){\n\t\t\t$A[$x][$y] = $next;\n\t\t\tpush @try, ($x-1, $y), ($x, $y-1), ($x+1, $y), ($x, $y+1);\n\t\t\twhile( @try ){\n\t\t\t\t($xx, $yy) = (shift @try, shift @try);\n\t\t\t\tif ($A[$xx][$yy] eq '.'){\n\t\t\t\t\t$A[$xx][$yy] = $next;\n\t\t\t\t\tpush @try, ($xx-1, $yy), ($xx, $yy-1), ($xx+1, $yy), ($xx, $yy+1);\n\t\t\t\t\t}\n\t\t\t\telsif ($A[$xx][$yy] eq '-1'){\n\t\t\t\t\t$NO{ $next } ++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\telse {$is_point = 0}\n\t\t\n\t\t$next ++;\n\t\t}\n\t\t\n#\tprint @$_ for @A;\n\t\n#\tprint join ' ', keys %NO;\n#\tprint $next;\n\t\n\tfor $c (1 .. $next){\n\t\t$NO{ $c } and next;\n\t\t$occ = 0;\n\t\tfor (@A){ \n\t\t\t$occ += grep $_ == $c, @{$_};\n\t\t\t}\n\t\t$occ and $OCC{ $c } = $occ;\n\t\t}\n\t\n#\tprint \"$_ -> $OCC{$_}\" for keys %OCC;\n\t\n\t$K = keys %OCC;\n\t%delete = ('*' => 1, map { $_, 1 } (sort { $OCC{$b} <=> $OCC{$a} } keys %OCC)[ $k .. $K - 1 ] );\n\n#\tprint \"K k[$K $k]\";\n\t\n\tprint $K - $k;\n\t\n\tfor $i (1 .. $n){\n\t\tfor $j (1 .. $m){\n\t\t\t$A[$i][$j] = $delete{ $A[$i][$j] } ? '*' : '.'\n\t\t\t}\n\t\tprint @{$A[$i]}[1 .. $m];\n\t\t\n\t\t}\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m, $k) = split;\n\t@A = ();\n\tpush @A, [ (-1) x (2 + $m) ];\n\tfor $i (1 .. $n){\n\t\t$_ = <>, chomp;\n\t\tpush @A, [ -1, (split //), -1 ];\n\t\t}\n\tpush @A, [ (-1) x (2 + $m) ];\n\t\n#\tprint @$_ for @A;\n\t\n\t$is_point = 1;\n\t$next = 1;\n\t%NO = ();\n\t%OCC = ();\n\t\n\twhile($is_point){\n\t\t$found = 0;\n\t\tfor $i (0 .. $n+1){\n\t\t\tfor $j (0 .. $m+1){\n\t\t\t\t$A[$i][$j] eq '.' and do {\n\t\t\t\t\t\t$found ++;\n\t\t\t\t\t\t($x, $y) = ($i, $j);\n\t\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t$found and last;\n\t\t\t}\n\t\t\n\t\tif ($found){\n\t\t\t$A[$x][$y] = $next;\n\t\t\tpush @try, ($x-1, $y), ($x, $y-1), ($x+1, $y), ($x, $y+1);\n\t\t\twhile( @try ){\n\t\t\t\t($xx, $yy) = (shift @try, shift @try);\n\t\t\t\tif ($A[$xx][$yy] eq '.'){\n\t\t\t\t\t$A[$xx][$yy] = $next;\n\t\t\t\t\tpush @try, ($xx-1, $yy), ($xx, $yy-1), ($xx+1, $yy), ($xx, $yy+1);\n\t\t\t\t\t}\n\t\t\t\telsif ($A[$xx][$yy] eq '-1'){\n\t\t\t\t\t$NO{ $next } ++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\telse {$is_point = 0}\n\t\t\n\t\t$next ++;\n\t\t}\n\t\t\n#\tprint @$_ for @A;\n\t\n#\tprint join ' ', keys %NO;\n#\tprint $next;\n\t\n\tfor $c (1 .. $next){\n\t\t$NO{ $c } and next;\n\t\t$occ = 0;\n\t\tfor (@A){ \n\t\t\t$occ += grep $_ == $c, @{$_};\n\t\t\t}\n\t\t$occ and $OCC{ $c } = $occ;\n\t\t}\n\t\n#\tprint \"$_ -> $OCC{$_}\" for keys %OCC;\n\t\n\t$K = keys %OCC;\n\t%delete = ('*' => 1, map { $_, 1 } (sort { $OCC{$b} <=> $OCC{$a} } keys %OCC)[ $k .. $K - 1 ] );\n\n#\tprint \"K k[$K $k]\";\n\t\n\t$del = eval join '+', map { $OCC{$_} } grep /\\d/, keys %delete;\n\t\n\tprint $del;\n\t\n\tfor $i (1 .. $n){\n\t\tfor $j (1 .. $m){\n\t\t\t$A[$i][$j] = $delete{ $A[$i][$j] } ? '*' : '.'\n\t\t\t}\n\t\tprint @{$A[$i]}[1 .. $m];\n\t\t\n\t\t}\n\t}"}], "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 \u2014 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\u2009=\u2009310.In the second sample the best strategy is to delete the third or fourth digits \u2014 that results in number 110102\u2009=\u20092610."}, "positive_code": [{"source_code": "chomp($_ = <>);\nunless (s/0//) { s/^.//; }\ns/^0*//;\nprint;\n"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nmy $n = <>;\nif ($n =~ /0/) {\n $n =~ s/0//;\n} else {\n $n =~ s/1//;\n}\nprint $n;\n"}, {"source_code": "#!/bin/bash/perl\n$input=<>;\n@inputProcessor=split(\" \",$input);\n$input=$inputProcessor[0];\n@elements=split(\"\",$input);\n$f=0;\nfor($i=0;$i<@elements;++$i){\n\tif($f==0 && $i==(@elements-1)){\n\t\t#do nothing\n\t}\n\telse{\n\t\tif($elements[$i] eq 0 && $f==0){\n\t\t\t#then trim it and dont print\n\t\t\t#also signal the flag\n\t\t\t$f=1;\n\t\t}\n\t\telse{\n\t\t\tprint $elements[$i];\n\t\t}\n\t}\n}"}], "negative_code": [{"source_code": "#!/bin/bash/perl\n$input=<>;\n@inputProcessor=split(\" \",$input);\n$input=$inputProcessor[0];\n$input=$input+0;\n@elements=split(\"\",$input);\n$f=0;\nfor($i=0;$i<@elements;++$i){\n\tif($f==0 && $i==(@elements-1)){\n\t\t#do nothing\n\t}\n\telse{\n\t\tif($elements[$i]==0 && $f==0){\n\t\t\t#then trim it and dont print\n\t\t\t#also signal the flag\n\t\t\t$f=1;\n\t\t}\n\t\telse{\n\t\t\tprint $elements[$i];\n\t\t}\n\t}\n}"}], "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$$$) \u2014 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$$$) \u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tmy %h;\r\n\t\r\n\t$h{ $_ } ++ for split ' ', <>;\r\n\t\r\n\t$k = keys %h;\r\n\t\r\n\tprint join ' ', ( $k ) x $k, $k + 1 .. $_;\r\n\t}"}, {"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t@_ = split ' ', <>;\r\n\t\r\n\tmy %h;\r\n\t\r\n\t$h{ $_ } ++ for @_;\r\n\t\r\n\t@_ = 1 .. @_;\r\n\t\r\n\t$_ < keys %h and $_ = keys %h for @_;\r\n\t\r\n\tprint \"@_\";\r\n\t}"}, {"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t@_ = split ' ', <>;\r\n\t\r\n\tmy %h;\r\n\t\r\n\t$h{ $_ } ++ for @_;\r\n\t\r\n\tprint join ' ', map { ( sort { $b <=> $a } $_, 0 + keys %h )[ 0 ] } 1 .. @_;\r\n\t}"}], "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\u2009=\u20096 and the crossword is 111011, then its encoding is an array {3,\u20092}; If x\u2009=\u20098 and the crossword is 01101010, then its encoding is an array {2,\u20091,\u20091}; If x\u2009=\u20095 and the crossword is 11111, then its encoding is an array {5}; If x\u2009=\u20095 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\u2009\u2264\u2009n\u2009\u2264\u2009100000, 1\u2009\u2264\u2009x\u2009\u2264\u2009109) \u2014 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\u2009\u2264\u2009ai\u2009\u2264\u200910000) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $x ) = split;\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = unpack '%32i' . @_, pack 'i' . @_, @_;\n\t\t\n\tprint $sum + @_ - 1 == $x ? \"YES\" : \"NO\";\n\t}"}, {"source_code": "( $n, $x ) = split ' ', <>;\n\t\t\t\t\t\nprint $x == $n - 1 + ( eval <> =~ s/ / + /gr ) ? \"YES\" : \"NO\""}], "negative_code": [], "src_uid": "080a3458eaea4903da7fa4cf531beba2"} {"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$$$) \u2014 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": "while(<>){print<>}"}, {"source_code": "while(<>){print<>,\" \";}"}, {"source_code": "while(<>){print <>,\" \";}"}, {"source_code": "while(<>){print<>}"}, {"source_code": "while(<>){print<>}"}, {"source_code": "print<> while(<>)"}, {"source_code": "while(<>){print<>}"}, {"source_code": "while(<>){print<>}"}], "negative_code": [], "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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$) \u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($tcase) = map { $_ - 0 } split(/\\s+/,);\r\nwhile( $tcase -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my @b = (); $#b = $n - 1;\r\n \r\n my $m1 = 1000010000;\r\n for(my $i=$n-1;$i>=0;$i--){\r\n $m1 = $A[$i] if $m1 > $A[$i];\r\n $b[$i] = $m1;\r\n }\r\n \r\n my $res = 'NO';\r\n my $m2 = -1;\r\n for(my $i=0;$i<$n-1;$i++){\r\n $m2 = $A[$i] if $m2 < $A[$i];\r\n \r\n if( $m2 > $b[$i+1] ){\r\n $res = 'YES';\r\n last;\r\n }\r\n }\r\n print \"$res\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "negative_code": [], "src_uid": "ef1448a744f67347183479c697aa87e1"} {"nl": {"description": "\"Hey, it's homework time\" \u2014 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,\u2009a2,\u2009...,\u2009an 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\u2009\u2264\u2009n\u2009\u2264\u20095000) which represents how many numbers are in the sequence. The second line contains a sequence of integers ai (1\u2009\u2264\u2009ai\u2009\u2264\u20095000,\u20091\u2009\u2264\u2009i\u2009\u2264\u2009n).", "output_spec": "Print the only number \u2014 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": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $num=<>;\nmy @str=split(' ',<>);\nmy @used;\nforeach my $count(1..$num){\n\t$used[$count]=0;\n}\nmy $total=$num;\nforeach my $count(0..$#str){\n\tif($str[$count]>0 && $str[$count]<=$num && $used[$str[$count]]==0){\n\t\t$total--;\n\t\t$used[$str[$count]]=1;\n\t}\n}\nprint $total;"}], "negative_code": [], "src_uid": "bdd86c8bc54bbac6e2bb5a9d68b6eb1c"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\n# symmetry, fill, constructive, BFS\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\t$debug and print \"[ $n x $m ]\";\n\t\n\tmy @A;\n\t\n\tfor my $i ( 1 .. $n ){\n\t\tpush @A, [ ( 0 ) x $m ];\n\t\t}\n\t\n\tmy $min = ( $n >> 1 ) + ( $m >> 1 );\n\t\n\tmy @c;\n\t\n\tif( 0 ){ ; }\n\telsif( $n % 2 == 1 and $m % 2 == 1 ){\n\t\tpush @c, [ ( $n >> 1 ), ( $m >> 1 ) ];\n\t\t}\n\telsif( $n % 2 == 0 and $m % 2 == 0 ){\n\t\tpush @c, [ ( $n >> 1 ), ( $m >> 1 ) ],\n\t\t\t\t [ ( $n >> 1 ) - 1, ( $m >> 1 ) - 1 ],\n\t\t\t\t [ ( $n >> 1 ) - 1, ( $m >> 1 ) ],\n\t\t\t\t [ ( $n >> 1 ), ( $m >> 1 ) - 1 ];\n\t\t}\n\telsif( $n % 2 == 1 and $m % 2 == 0 ){\n\t\tpush @c, [ ( $n >> 1 ), ( $m >> 1 ) ],\n\t\t\t\t [ ( $n >> 1 ), ( $m >> 1 ) - 1 ];\n\t\t}\n\telsif( $n % 2 == 0 and $m % 2 == 1 ){\n\t\tpush @c, [ ( $n >> 1 ), ( $m >> 1 ) ],\n\t\t\t\t [ ( $n >> 1 ) - 1, ( $m >> 1 ) ];\n\t\t}\n\t\n\t$debug and print \"c:@$_\" for @c;\n\t\n\tpush @c, [ -1, -1 ];\n\t\n\twhile( @c ){\n\t\tmy( $i, $j ) = @{ shift @c };\n\t\t\n\t\tif( $i == -1 and $j == -1 ){\n\t\t\t$min ++;\n\t\t\tpush @c, [ -1, -1 ] if @c > 0;\n\t\t\tnext;\n\t\t\t}\n\t\t\n\t\tif( $A[ $i ][ $j ] == 0 ){\n\t\t\t$A[ $i ][ $j ] = $min;\n\t\t\t\n\t\t\tpush @c, [ $i - 0, $j - 1 ] if $j - 1 >= 0;\n\t\t\tpush @c, [ $i - 1, $j - 0 ] if $i - 1 >= 0;\n\t\t\tpush @c, [ $i + 0, $j + 1 ] if $j + 1 < $m;\n\t\t\tpush @c, [ $i + 1, $j + 0 ] if $i + 1 < $n;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"@$_\" for @A;\n\t\n\tprint join ' ', sort { $a <=> $b } map @$_, @A;\n\t}"}], "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$$$) \u2014 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}$$$) \u2014 the number of pages in the book and required divisor, respectively.", "output_spec": "For each query print the answer for it \u2014 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": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $q = );\n\nfor (1..$q) {\n chomp (my $line = );\n my ($n, $m) = split q{ }, $line;\n\n my @ar;\n my $p = 0;\n for (1..10) {\n $ar[$_-1] = ($m*$_)%10;\n $p += $ar[$_-1];\n }\n\n my $d = int($n/($m*10));\n my $answer = $d*$p;\n\n my $i = $d*$m*10+$m;\n while ( $i<=$n ) {\n $answer += $i%10;\n $i+=$m;\n }\n say $answer;\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $q = );\n\nfor (1..$q) {\n chomp (my $line = );\n my ($n, $m) = split q{ }, $line;\n my $lom = $m % 10;\n\n if ( $lom == 0 ) {\n say 0;\n next;\n }\n if ( $n < $m ) {\n say 0;\n next;\n }\n\n my $ld = $lom;\n my $i = 1;\n my $sum = $ld;\n my @sums = (0, $sum);\n while ($ld != 0) {\n $ld += $lom;\n $ld %= 10;\n $sum += $ld;\n push @sums, $sum;\n $i++;\n }\n\n my $div = int($n/$m);\n\n my $mo = int($div/$i);\n\n my $answer = $mo*$sums[$i];\n\n my $mod = $div % $i;\n $answer += $sums[$mod];\n say $answer;\n}\n"}, {"source_code": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $q = );\n\nfor (1..$q) {\n chomp (my $line = );\n my ($n, $m) = split q{ }, $line;\n my $lom = $m % 10;\n\n say \"0\", next if $lom == 0;\n say \"0\", next if $n < $m;\n\n my $ld = $lom;\n my $i = 1;\n my $sum = $ld;\n my @sums = (0, $lom);\n while ($ld != 0) {\n $ld += $lom;\n $ld %= 10;\n $sum += $ld;\n push @sums, $sum;\n $i++;\n }\n\n my $div = int($n/$m);\n\n my $mo = int($div/$i);\n\n my $answer = $mo*$sum;\n\n my $mod = $div % $i;\n $answer += $sums[$mod];\n say $answer;\n}\n"}, {"source_code": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $q = );\n\nfor (1..$q) {\n chomp (my $line = );\n my ($n, $m) = split q{ }, $line;\n my $lom = $m % 10;\n\n if ( $lom == 0 ) {\n say 0;\n next;\n }\n if ( $n < $m ) {\n say 0;\n next;\n }\n\n my $ld = $lom;\n my $i = 1;\n my $sum = $ld;\n my @sums = (0, $lom);\n while ($ld != 0) {\n $ld += $lom;\n $ld %= 10;\n $sum += $ld;\n push @sums, $sum;\n $i++;\n }\n\n my $div = int($n/$m);\n\n my $mo = int($div/$i);\n\n my $answer = $mo*$sum;\n\n my $mod = $div % $i;\n $answer += $sums[$mod];\n say $answer;\n}\n"}, {"source_code": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $q = );\n\nfor (1..$q) {\n chomp (my $line = );\n my ($n, $m) = split q{ }, $line;\n my $lom = $m % 10;\n\n if ( $lom == 0 ) {\n say 0;\n next;\n }\n if ( $n < $m ) {\n say 0;\n next;\n }\n\n my $ld = $lom;\n my $i = 1;\n my $sum = $ld;\n my @sums = (0, $sum);\n while ($i < 10) {\n $ld += $lom;\n $ld %= 10;\n $sum += $ld;\n push @sums, $sum;\n $i++;\n }\n\n my $answer = 0;\n\n $answer += $sums[10]*int(int($n/$m)/10);\n $answer += $sums[int(int($n/$m)%10)];\n say $answer;\n}\n"}], "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\u2009\u2264\u2009xi\u2009\u2264\u2009n) 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009300\u2009000) 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\u2009\u2264\u2009xi\u2009\u2264\u2009n, ci \u2014 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\u00a0\u00a0\u00a0\u00a0(\"hb[..]bz....\" \u2009\u2192\u2009 \"hb.bz[..]..\" \u2009\u2192\u2009 \"hb.bz[..].\" \u2009\u2192\u2009 \"hb.bz[..]\" \u2009\u2192\u2009 \"hb.bz.\") after the second query f(hb\u0441.bz....) = 3\u00a0\u00a0\u00a0\u00a0(\"hb\u0441.bz[..]..\" \u2009\u2192\u2009 \"hb\u0441.bz[..].\" \u2009\u2192\u2009 \"hb\u0441.bz[..]\" \u2009\u2192\u2009 \"hb\u0441.bz.\") after the third query f(hb\u0441.bz..f.) = 1\u00a0\u00a0\u00a0\u00a0(\"hb\u0441.bz[..]f.\" \u2009\u2192\u2009 \"hb\u0441.bz.f.\")Note to the second sample test.The original string is \".cc.\". after the first query: f(..c.) = 1\u00a0\u00a0\u00a0\u00a0(\"[..]c.\" \u2009\u2192\u2009 \".c.\") after the second query: f(....) = 3\u00a0\u00a0\u00a0\u00a0(\"[..]..\" \u2009\u2192\u2009 \"[..].\" \u2009\u2192\u2009 \"[..]\" \u2009\u2192\u2009 \".\") after the third query: f(.a..) = 1\u00a0\u00a0\u00a0\u00a0(\".a[..]\" \u2009\u2192\u2009 \".a.\") after the fourth query: f(aa..) = 1\u00a0\u00a0\u00a0\u00a0(\"aa[..]\" \u2009\u2192\u2009 \"aa.\")"}, "positive_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m) = split;\n\t$_ = <>, chomp;\n\t$sum = () = /\\.(?=\\.)/g;\n\t\n\t$_ = 'a' . $_ . 'a';\n\t@_ = ();\n\t\n\tfor $i (1 .. $m){\n\t\tpush @_, $sum;\n\t\t($idx, $let) = split \" \", <>;\n\t\t($pre, $is, $post) = split //, substr $_, $idx - 1, 3;\n\t\tsubstr $_, $idx, 1, $let;\n\t\t$is ne '.' and $let ne '.' and next;\n\t\t$is eq '.' and $let eq '.' and next;\n\t\tif ($is eq '.' and $let ne '.'){\n\t\t\t$sum -= ($pre eq '.') + ($post eq '.');\n\t\t\t} \n\t\telsif ($is ne '.' and $let eq '.'){\n\t\t\t$sum += ($pre eq '.') + ($post eq '.');\n\t\t\t} \n\t\t}\n\t\t\n\tpush @_, $sum;\n\tshift @_;\n\n\t$, = $/;\n\tprint @_;\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m) = split;\n\t$_ = <>;\n\t\n\t$rx = qr/\\.(?=\\.)/;\n\t$sum = () = /$rx/g;\n\t\n\ts/^/a/;\n\t\n\t@_ = ();\n\t\n\tfor $i (1 .. $m){\n\t\t($pos, $let) = split ' ', <>;\n\t\t$sum -= () = (substr $_, $pos - 1, 3) =~ /$rx/g;\n\t\tsubstr $_, $pos, 1, $let;\n\t\t$sum += () = (substr $_, $pos - 1, 3) =~ /$rx/g;\n\t\tpush @_, $sum;\n\t\t}\n\t\t\n\t$, = $/;\n\tprint @_;\n\t\t\n\t}"}, {"source_code": "($n, $m) = split ' ', <>;\n$_ = <>;\n\n$rx = qr/\\.(?=\\.)/;\n$s = () = /$rx/g;\n\ns/^/a/;\n\t\nfor $i (1 .. $m){\n\t($pos, $L) = split ' ', <>;\n\t$s -= () = (substr $_, $pos - 1, 3) =~ /$rx/g;\n\tsubstr $_, $pos, 1, $L;\n\t$s += () = (substr $_, $pos - 1, 3) =~ /$rx/g;\n\tpush @_, $s\n\t}\n\t\t\n$, = $/;\nprint @_"}, {"source_code": "$\\ = $/;\n\n\t($n, $m) = split \" \", <>;\n\t$_ = <>, chomp;\n\t$sum = () = /\\.(?=\\.)/g;\n\t\n\t$_ = 'a' . $_ . 'a';\n\t@_ = ();\n\t\n\t{local $/; $a = <>};\n\t@a = split \" \", $a;\n\t\n\tfor $i (1 .. $m){\n\t\tpush @_, $sum;\n\t\t($idx, $let) = (shift @a, shift @a);\n\t\t($pre, $is, $post) = split //, substr $_, $idx - 1, 3;\n\t\tsubstr $_, $idx, 1, $let;\n\t\t$is ne '.' and $let ne '.' and next;\n\t\t$is eq '.' and $let eq '.' and next;\n\t\tif ($is eq '.' and $let ne '.'){\n\t\t\t$sum -= ($pre eq '.') + ($post eq '.');\n\t\t\t} \n\t\telsif ($is ne '.' and $let eq '.'){\n\t\t\t$sum += ($pre eq '.') + ($post eq '.');\n\t\t\t} \n\t\t}\n\t\t\n\tpush @_, $sum;\n\tshift @_;\n\n\t$, = $/;\n\tprint @_;\n\t"}], "negative_code": [{"source_code": "($n, $m) = split, ' ', <>;\n$_ = <>;\n\n$rx = qr/\\.(?=\\.)/;\n$s = () = /$rx/g;\n\ns/^/a/;\n\t\nfor $i (1 .. $m){\n\t($pos, $L) = split ' ', <>;\n\t$s -= () = (substr $_, $pos - 1, 3) =~ /$rx/g;\n\tsubstr $_, $pos, 1, $L;\n\t$s += () = (substr $_, $pos - 1, 3) =~ /$rx/g;\n\tpush @_, $s\n\t}\n\t\t\n$, = $/;\nprint @_"}], "src_uid": "68883ab115882de5cf77d0848b80b422"} {"nl": {"description": "Petya and Vasya are tossing a coin. Their friend Valera is appointed as a judge. The game is very simple. First Vasya tosses a coin x times, then Petya tosses a coin y times. If the tossing player gets head, he scores one point. If he gets tail, nobody gets any points. The winner is the player with most points by the end of the game. If boys have the same number of points, the game finishes with a draw.At some point, Valera lost his count, and so he can not say exactly what the score is at the end of the game. But there are things he remembers for sure. He remembers that the entire game Vasya got heads at least a times, and Petya got heads at least b times. Moreover, he knows that the winner of the game was Vasya. Valera wants to use this information to know every possible outcome of the game, which do not contradict his memories.", "input_spec": "The single line contains four integers x,\u2009y,\u2009a,\u2009b (1\u2009\u2264\u2009a\u2009\u2264\u2009x\u2009\u2264\u2009100,\u20091\u2009\u2264\u2009b\u2009\u2264\u2009y\u2009\u2264\u2009100). The numbers on the line are separated by a space.", "output_spec": "In the first line print integer n \u2014 the number of possible outcomes of the game. Then on n lines print the outcomes. On the i-th line print a space-separated pair of integers ci, di \u2014 the number of heads Vasya and Petya got in the i-th outcome of the game, correspondingly. Print pairs of integers (ci,\u2009di) in the strictly increasing order. Let us remind you that the pair of numbers (p1,\u2009q1) is less than the pair of numbers (p2,\u2009q2), if p1\u2009<\u2009p2, or p1\u2009=\u2009p2 and also q1\u2009<\u2009q2.", "sample_inputs": ["3 2 1 1", "2 4 2 2"], "sample_outputs": ["3\n2 1\n3 1\n3 2", "0"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp($_ = <>);\nmy ($x, $y, $p, $q) = split;\nmy @res = ();\n\nfor my $v ($p .. $x) {\n for my $p ($q .. $y) {\n next if $v <= $p;\n push @res, [$v, $p];\n }\n}\n\n@res = sort { if ($$a[0] != $$b[0]) { $$a[0] <=> $$b[0]; } else { $$a[1] <=> $$b[1]; } } @res;\nprint scalar @res, \"\\n\";\nfor (@res) {\n print \"@$_\\n\";\n}\n"}, {"source_code": "($x,$y,$a,$b)=split(/ |\\n/,<>);\n$sum=0;\nfor($i=($a>$b ? $a : $b); $i<=$x; $i++){\n\tfor($j=$b; $j<$i && $j<=$y ; $j++){\n\t\t$sum++;\n\t}\n}\nprint \"$sum\\n\";\nfor($i=($a>$b ? $a : $b); $i<=$x; $i++){\n\tfor($j=$b; $j<$i && $j<=$y ; $j++){\n\t\tprint \"$i $j\\n\";\n\t}\n}\n\n"}, {"source_code": "while (<>){\n chomp;\n / (\\d+) (\\d+) /;\n $a=0;\n @_=();\n for ($i=($'+1>$2?$'+1:$2) ; $i<=$`; $i++){\n for ($j=$'; $j<$i && $j<=$1; $j++){\n $a++;\n push @_,\"$i $j\\n\"\n }\n }\n unshift @_, \"$a\\n\";\n print @_\n }"}], "negative_code": [{"source_code": "($x,$y,$a,$b)=split(/ |\\n/,<>);\n$sum=0;\nfor($i=($a>$b ? $a : $b); $i<=$x; $i++){\n\tfor($j=$b; $j<$i; $j++){\n\t\t$sum++;\n\t}\n}\nprint \"$sum\\n\";\nfor($i=($a>$b ? $a : $b); $i<=$x; $i++){\n\tfor($j=$b; $j<$i; $j++){\n\t\tprint \"$i $j\\n\";\n\t}\n}\n\n"}, {"source_code": "($x,$y,$a,$b)=split(/ |\\n/,<>);\n$sum=0;\nfor($i=($a>$b ? $a : $b); $i<=$x; $i++){\n\tfor($j=$b; $j<$i && $j<$y ; $j++){\n\t\t$sum++;\n\t}\n}\nprint \"$sum\\n\";\nfor($i=($a>$b ? $a : $b); $i<=$x; $i++){\n\tfor($j=$b; $j<$i && $j<$y ; $j++){\n\t\tprint \"$i $j\\n\";\n\t}\n}\n\n"}, {"source_code": "while (<>){\n chomp;\n / (\\d+) (\\d+) /;\n $a=0;\n @_=();\n for ($i=($'+1>$2?$'+1:$2) ; $i<=$`; $i++){\n for ($j=$'; $j<$i; $j++){\n $a++;\n push @_,\"$i $j\\n\"\n }\n }\n unshift @_, \"$a\\n\";\n print @_\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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of cars in the train. The second line contains n integers pi (1\u2009\u2264\u2009pi\u2009\u2264\u2009n, pi\u2009\u2260\u2009pj if i\u2009\u2260\u2009j)\u00a0\u2014 the sequence of the numbers of the cars in the train.", "output_spec": "Print a single integer\u00a0\u2014 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": "$n = <>;\n@a = split(' ', <>);\n\nforeach(@a){\n\t$p[$_] = $p[$_ - 1] + 1;\n}\nfor(1..$n){\n\t$re = $re < $p[$_] ? $p[$_] : $re;\n}\nprint $n - $re;"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = (split ' ', <>);\n\t$i = 0; map { $A[ $_ ] = ++ $i } @_;\n\t$prev = 0;\n\t$len = 0;\n\t$max = 0;\n\tfor (1 .. @_){\n\t\n\t\t$prev < $A[ $_ ] ?\n\t\t\tdo { $len ++ ; $prev = $A[ $_ ]; $max < $len and $max = $len; }\n\t\t:\n\t\t\tdo { $len = 0; $prev = 0; redo };\n\t}\n\tprint @_ - $max\n\t}"}], "negative_code": [], "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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\r\nmy ($t) = map { $_ - 0 } split(/\\s+/o,);\r\nforeach my $tt ( 1 .. $t ){\r\n my ($k) = map { $_ - 0 } split(/\\s+/o,);\r\n my $i = int(sqrt($k));\r\n $i+=2;\r\n while( ($i-1)*($i-1) >= $k ){ $i--; }\r\n my $k2 = $k - ($i-1) ** 2;\r\n if( $k2 <= $i ){\r\n print ( $k2 . \" \" . $i . \"\\n\" );\r\n } else {\r\n print ( $i . \" \" . ( 2*$i - $k2 ) . \"\\n\" );\r\n }\r\n}\r\n\r\n"}], "negative_code": [], "src_uid": "f8335c59cd05988c8053f138c4df06aa"} {"nl": {"description": "You are given a picture consisting of $$$n$$$ rows and $$$m$$$ columns. Rows are numbered from $$$1$$$ to $$$n$$$ from the top to the bottom, columns are numbered from $$$1$$$ to $$$m$$$ from the left to the right. Each cell is painted either black or white. You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $$$x$$$ and $$$y$$$, where $$$1 \\le x \\le n$$$ and $$$1 \\le y \\le m$$$, such that all cells in row $$$x$$$ and all cells in column $$$y$$$ are painted black.For examples, each of these pictures contain crosses: The fourth picture contains 4 crosses: at $$$(1, 3)$$$, $$$(1, 5)$$$, $$$(3, 3)$$$ and $$$(3, 5)$$$.Following images don't contain crosses: You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?You are also asked to answer multiple independent queries.", "input_spec": "The first line contains an integer $$$q$$$ ($$$1 \\le q \\le 5 \\cdot 10^4$$$) \u2014 the number of queries. The first line of each query contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 5 \\cdot 10^4$$$, $$$n \\cdot m \\le 4 \\cdot 10^5$$$) \u2014 the number of rows and the number of columns in the picture. Each of the next $$$n$$$ lines contains $$$m$$$ characters \u2014 '.' if the cell is painted white and '*' if the cell is painted black. It is guaranteed that $$$\\sum n \\le 5 \\cdot 10^4$$$ and $$$\\sum n \\cdot m \\le 4 \\cdot 10^5$$$.", "output_spec": "Print $$$q$$$ lines, the $$$i$$$-th line should contain a single integer \u2014 the answer to the $$$i$$$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.", "sample_inputs": ["9\n5 5\n..*..\n..*..\n*****\n..*..\n..*..\n3 4\n****\n.*..\n.*..\n4 3\n***\n*..\n*..\n*..\n5 5\n*****\n*.*.*\n*****\n..*.*\n..***\n1 4\n****\n5 5\n.....\n..*..\n.***.\n..*..\n.....\n5 3\n...\n.*.\n.*.\n***\n.*.\n3 3\n.*.\n*.*\n.*.\n4 4\n*.**\n....\n*.**\n*.**"], "sample_outputs": ["0\n0\n0\n0\n0\n4\n1\n1\n2"], "notes": "NoteThe example contains all the pictures from above in the same order.The first 5 pictures already contain a cross, thus you don't have to paint anything.You can paint $$$(1, 3)$$$, $$$(3, 1)$$$, $$$(5, 3)$$$ and $$$(3, 5)$$$ on the $$$6$$$-th picture to get a cross in $$$(3, 3)$$$. That'll take you $$$4$$$ minutes.You can paint $$$(1, 2)$$$ on the $$$7$$$-th picture to get a cross in $$$(4, 2)$$$.You can paint $$$(2, 2)$$$ on the $$$8$$$-th picture to get a cross in $$$(2, 2)$$$. You can, for example, paint $$$(1, 3)$$$, $$$(3, 1)$$$ and $$$(3, 3)$$$ to get a cross in $$$(3, 3)$$$ but that will take you $$$3$$$ minutes instead of $$$1$$$.There are 9 possible crosses you can get in minimum time on the $$$9$$$-th picture. One of them is in $$$(1, 1)$$$: paint $$$(1, 2)$$$ and $$$(2, 1)$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map { chomp; [ split '' ] } map ~~<>, 1 .. $n;\n\t\n\t$debug and print \"@{ $_ }\" for @_;\n\t\n\tmy @hor;\n\t\n\tfor( @_ ){\n\t\tmy $cnt = 0;\n\t\t\n\t\tfor my $j ( 0 .. @{ $_ } - 1 ){\n\t\t\tif( $_->[ $j ] eq '*' ){\n\t\t\t\t$cnt ++;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\tpush @hor, $cnt;\n\t\t}\n\t\n\t$debug and print \"hor:[@hor]\";\n\t\t\n\tmy @ver;\n\t\n\tfor my $j ( 0 .. @{ $_[ 0 ] } - 1 ){\n\t\tmy $cnt = 0;\n\t\t\n\t\tfor my $i ( 0 .. @_ - 1 ){\n\t\t\tif( $_[ $i ][ $j ] eq '*' ){\n\t\t\t\t$cnt ++;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\tpush @ver, $cnt;\n\t\t}\n\t\n\t$debug and print \"ver:[@ver]\";\n\t\n\tmy @cand;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tfor my $j ( 0 .. @{ $_[ 0 ] } - 1 ){\n\t\t\tmy $is_star = $_[ $i ][ $j ] eq '*' ? 1 : 0;\n\t\t\tmy $cand = $n + $m - 1 - ( $hor[ $i ] + $ver[ $j ] - 1 * $is_star );\n\t\t\tpush @cand, $cand;\n\t\t\t}\n\t\t$debug and print \"[@cand]\";\n\t\t}\n\t\n\tprint +( sort { $a <=> $b } @cand )[ 0 ];\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map { chomp; [ split '' ] } map ~~<>, 1 .. $n;\n\t\n\t$debug and print \"@{ $_ }\" for @_;\n\t\n\tmy $hor = 0;\n\t\n\tfor( @_ ){\n\t\tmy $cnt = 0;\n\t\t\n\t\tfor my $j ( 0 .. @{ $_ } - 1 ){\n\t\t\tif( $_->[ $j ] eq '*' ){\n\t\t\t\t$cnt ++;\n\t\t\t\tif( $cnt > $hor ){\n\t\t\t\t\t$hor = $cnt;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"hor:[$hor]\";\n\t\n\tfor( @_ ){\n\t\tmy $cnt = 0;\n\t\t\n\t\tfor my $j ( 0 .. @{ $_ } - 1 ){\n\t\t\tif( $_->[ $j ] eq '*' ){\n\t\t\t\t$cnt ++;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\tif( $cnt == $hor ){\n\t\t\t$_ = [ ( '*' ) x @{ $_ } ];\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"@{ $_ }\" for @_;\n\t\n\tmy $ver = 0;\n\t\n\tfor my $j ( 0 .. @{ $_[ 0 ] } - 1 ){\n\t\tmy $cnt = 0;\n\t\t\n\t\tfor my $i ( 0 .. @_ - 1 ){\n\t\t\tif( $_[ $i ][ $j ] eq '*' ){\n\t\t\t\t$cnt ++;\n\t\t\t\tif( $cnt > $ver ){\n\t\t\t\t\t$ver = $cnt;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"ver:[$ver]\";\n\t\n\tprint $n + $m - $hor - $ver;\n\t}"}], "src_uid": "a1b6a2f4659169e0e818bbdee6d36e76"} {"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,\u2009...,\u2009ak. 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\u2009\u2264\u2009n\u2009\u2264\u2009999, n is odd, 0\u2009\u2264\u2009k\u2009<\u2009n, 1\u2009\u2264\u2009p\u2009\u2264\u20091000, n\u2009\u2264\u2009x\u2009\u2264\u2009n\u00b7p, 1\u2009\u2264\u2009y\u2009\u2264\u2009p). 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,\u2009...,\u2009ak (1\u2009\u2264\u2009ai\u2009\u2264\u2009p)\u00a0\u2014 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\u2009-\u2009k space-separated integers\u00a0\u2014 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,\u00a0...,\u00a0an where n is odd (in this problem n is always odd) is the element staying on (n\u2009+\u20091)\u2009/\u20092 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\u00a02\", \"2\u00a04\", \"5\u00a01\", \"1\u00a05\", \"4\u00a01\", \"1\u00a04\" 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, @_) = split \" \", join '', <>;\nmap $df += .1 + $_ <=> $y, @_;\nfor $i (1 .. $n - $k){\n\tpush @_, $df > 0 ? 1 : $y;\n\t$df += .1 <=> $df;\n\t}\n$s += $_ for @_;\nprint $s > $x || $df < 0 ? -1 : \"@_[$k .. $n-1]\""}, {"source_code": "($n, $k, $p, $x, $y, @_) = split \" \", join '', <>;\nmap $df += .1 + $_ <=> $y, @_;\nfor $i (1 .. $n - $k){\n\tpush @_, $df > 0 ? 1 : $y;\n\t$df += .1 <=> $df;\n\t}\n$s += $_ for @_;\nprint $s > $x || $df < 0 ? -1 : \"@_[$k .. $n-1]\""}], "negative_code": [{"source_code": "$\\ = $/;\nwhile(<>){\n\t($n, $k, $p, $x, $y) = split;\n\t@_ = split \" \", <>;\n\t$le = scalar grep $_ < $y, @_;\n\t$mo = scalar grep $_ >= $y, @_;\n\t$df = $mo - $le;\n#\t\tprint \"$le $mo | $df\";\n\t\tfor $i (1 .. $n - $k){\n\t\t\tif ($df > 0){\n\t\t\t\tif (@_ == $n - 1 and $n % 2 == 0){\n\t\t\t\t\t@a = sort {$a <=> $b} @_;\n\t\t\t\t\t$mid = $a[$n / 2];\n\t\t\t\t\tpush @_, $y - ($mid - $y);\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tpush @_, 1;\n\t\t\t\t\t\t}\n\t\t\t\t$df --;\n\t\t\t\t}\n\t\t\telse {\n\t\t\t\tpush @_, $y;\n\t\t\t\t$df ++;\n\t\t\t\t}\n\t\t\t}\n\t\t$sum = 0;\n\t\t$sum += $_ for @_;\n\t\t\n\t\tprint $sum > $x ? -1 : \"@_[$k .. $n-1]\";\n\t}"}], "src_uid": "f01d11bd231a7b2e7ca56de1df0f1272"} {"nl": {"description": "The bear has a string s\u2009=\u2009s1s2... 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,\u2009j (1\u2009\u2264\u2009i\u2009\u2264\u2009j\u2009\u2264\u2009|s|), that string x(i,\u2009j)\u2009=\u2009sisi\u2009+\u20091... sj contains at least one string \"bear\" as a substring.String x(i,\u2009j) contains string \"bear\", if there is such index k (i\u2009\u2264\u2009k\u2009\u2264\u2009j\u2009-\u20093), that sk\u2009=\u2009b, sk\u2009+\u20091\u2009=\u2009e, sk\u2009+\u20092\u2009=\u2009a, sk\u2009+\u20093\u2009=\u2009r.Help the bear cope with the given problem.", "input_spec": "The first line contains a non-empty string s (1\u2009\u2264\u2009|s|\u2009\u2264\u20095000). It is guaranteed that the string only consists of lowercase English letters.", "output_spec": "Print a single number \u2014 the answer to the problem.", "sample_inputs": ["bearbtear", "bearaabearc"], "sample_outputs": ["6", "20"], "notes": "NoteIn the first sample, the following pairs (i,\u2009j) match: (1,\u20094),\u2009(1,\u20095),\u2009(1,\u20096),\u2009(1,\u20097),\u2009(1,\u20098),\u2009(1,\u20099).In the second sample, the following pairs (i,\u2009j) match: (1,\u2009\u20094),\u2009(1,\u2009\u20095),\u2009(1,\u2009\u20096),\u2009(1,\u2009\u20097),\u2009(1,\u2009\u20098),\u2009(1,\u2009\u20099),\u2009(1,\u2009\u200910),\u2009(1,\u2009\u200911),\u2009(2,\u2009\u200910),\u2009(2,\u2009\u200911),\u2009(3,\u2009\u200910),\u2009(3,\u2009\u200911),\u2009(4,\u2009\u200910),\u2009(4,\u2009\u200911),\u2009(5,\u2009\u200910),\u2009(5,\u2009\u200911),\u2009(6,\u2009\u200910),\u2009(6,\u2009\u200911),\u2009(7,\u2009\u200910),\u2009(7,\u2009\u200911)."}, "positive_code": [{"source_code": "#!/bin/perl\n\nchomp(my $str = <>);\nmy $str_len = length $str;\nmy $start = 0;\nmy $ans = 0;\nwhile ((my $idx = index($str, 'bear', $start)) != -1) {\n my ($f, $s) = ($idx - $start, $str_len - $idx - 4);\n $ans += $f + $s + 1 + $f * $s;\n $start = $idx + 1;\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "chomp($_ = <>);\nwhile (s/\\A(.*?)bear/ear/) {\n $ans += (1 + length $1) * (1 + length $');\n}\nprint ($ans or 0) . \"\\n\";\n"}, {"source_code": "while(<>){\n\tchomp;\n\t$a=0;\n\twhile (s/^(.*?)bear/ear/){\n\t\t$a+=(1+length $1)*(1+length $');\n\t\t}\n\tprint $a\n\t}"}], "negative_code": [{"source_code": "chomp($_ = <>);\nwhile (s/(.*?)bear/ear/) {\n $ans += (1 + length $1) * (1 + length $');\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "chomp($_ = <>);\nwhile (s/\\A(.*?)bear/ear/) {\n $ans += (1 + length $1) * (1 + length $');\n}\nprint $ans . \"\\n\";\n"}], "src_uid": "240a2b88ded6016d0fd7157d0ee2beea"} {"nl": {"description": "You are given a sorted array $$$a_1, a_2, \\dots, a_n$$$ (for each index $$$i > 1$$$ condition $$$a_i \\ge a_{i-1}$$$ holds) and an integer $$$k$$$.You are asked to divide this array into $$$k$$$ non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let $$$max(i)$$$ be equal to the maximum in the $$$i$$$-th subarray, and $$$min(i)$$$ be equal to the minimum in the $$$i$$$-th subarray. The cost of division is equal to $$$\\sum\\limits_{i=1}^{k} (max(i) - min(i))$$$. For example, if $$$a = [2, 4, 5, 5, 8, 11, 19]$$$ and we divide it into $$$3$$$ subarrays in the following way: $$$[2, 4], [5, 5], [8, 11, 19]$$$, then the cost of division is equal to $$$(4 - 2) + (5 - 5) + (19 - 8) = 13$$$.Calculate the minimum cost you can obtain by dividing the array $$$a$$$ into $$$k$$$ non-empty consecutive subarrays. ", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 3 \\cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$ 1 \\le a_i \\le 10^9$$$, $$$a_i \\ge a_{i-1}$$$). ", "output_spec": "Print the minimum cost you can obtain by dividing the array $$$a$$$ into $$$k$$$ nonempty consecutive subarrays. ", "sample_inputs": ["6 3\n4 8 15 16 23 42", "4 4\n1 3 3 7", "8 1\n1 1 2 3 5 8 13 21"], "sample_outputs": ["12", "0", "20"], "notes": "NoteIn the first test we can divide array $$$a$$$ in the following way: $$$[4, 8, 15, 16], [23], [42]$$$. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy @diffs;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tpush @diffs, $_[ $i + 1 ] - $_[ $i ];\n\t\t}\n\t\n\tmy @diffs_max = ( sort { $b <=> $a } @diffs )[ 0 .. $k - 2 ];\n\t\n\tmy $sum = 0 + eval join '+', @diffs_max;\n\t\n\tprint $_[ @_ - 1 ] - $_[ 0 ] - $sum;\n\t}"}], "negative_code": [], "src_uid": "2895624e708159bc2a6f3e91140a6c45"} {"nl": {"description": "Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has \u2014 he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.", "input_spec": "The first line contains a positive integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of days of Vasya's vacations. The second line contains the sequence of integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u20093) separated by space, where: ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.", "output_spec": "Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. ", "sample_inputs": ["4\n1 3 2 0", "7\n1 3 3 2 1 2 3", "2\n2 2"], "sample_outputs": ["2", "0", "1"], "notes": "NoteIn the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day."}, "positive_code": [{"source_code": "use experimental \"switch\";\n<>; $_ = <>; @c = split; $Z=0; $C=0;\nfor $c (@c) {\n\tfor (\"$C$c\") {\n\t\twhen (/.0/) { $Z++; $C=0 }\n\t\twhen (/11|22/) { $Z++; $C=0 }\n\t\twhen (/13/) { $C=2 }\n\t\twhen (/23/) { $C=1 }\n\t\tdefault { $C=$c }\n\t}\t\t\t\n}\nprint $Z"}], "negative_code": [{"source_code": "<>; $_ = <>; s/ //g; \ns/13/12/g; \ns/23/21/g;\ns/11/10/g;\ns/22/20/g;\n$ans = y/0/0/;\nprint $ans"}], "src_uid": "08f1ba79ced688958695a7cfcfdda035"} {"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$$$)\u00a0\u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tm/./, s/$&//g or print 'NO' and next;\r\n\t\r\n\tprint qw( YES NO )[ ! grep {\r\n\t\t\t1 while s/<>//g;\r\n\t\t\t! m/./\r\n\t\t\t} s/\\w//gr \r\n\t\t];\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\t/./;\n\tmy $open = $&;\n\t\n\ts/$open/(/g;\n\t\n\t/[ABC]$/ or do {\n\t\tprint 'NO';\n\t\tnext;\n\t\t};\n\t\n\tmy $close = $&;\n\t\n\ts/$close/)/g;\n\t\n\tmy $A = s/[ABC]/(/gr;\n\tmy $B = s/[ABC]/)/gr;\n\t\n\t1 while $A =~ s/[(][)]//g;\n\t1 while $B =~ s/[(][)]//g;\n\t\n\tif( ! $A or ! $B ){\n\t\tprint \"YES\";\n\t\t}\n\telse{\n\t\tprint \"NO\";\n\t\t}\n\t}"}], "negative_code": [], "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\" \u2014 if more than 50% of players have better results; \"random\" \u2014 if his result is not worse than the result that 50% of players have, but more than 20% of players have better results; \"average\" \u2014 if his result is not worse than the result that 80% of players have, but more than 10% of players have better results; \"hardcore\" \u2014 if his result is not worse than the result that 90% of players have, but more than 1% of players have better results; \"pro\" \u2014 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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $lines = ;\n$lines =~ s/[\\r\\n]*$//;\n\nmy %scores = ();\nfor(my $i = 0; $i < $lines; $i++){\n\tmy $s = ;\n\tmy @arr = split /[ \\r\\n]+/, $s;\n\t$scores{$arr[0]} = 0 unless exists $scores{$arr[0]};\n\t$scores{$arr[0]} = $arr[1]-0 if $scores{$arr[0]} < $arr[1];\n}\nmy $users = keys %scores;\nprint \"$users\\n\";\nforeach my $user (sort keys %scores){\n\tmy $worse = grep {$scores{$_} <= $scores{$user}} keys %scores;\n\tif($worse * 100 >= $users * 99){\n\t\tprint \"$user pro\\n\";\n\t}elsif($worse * 100 >= $users * 90){\n\t\t\tprint \"$user hardcore\\n\";\n\t}elsif($worse * 100 >= $users * 80){\n\t\t\tprint \"$user average\\n\";\n\t}elsif($worse * 100 >= $users * 50){\n\t\t\tprint \"$user random\\n\";\n\t}else{\n\t\tprint \"$user noob\\n\";\n\t}\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nmy $lines = ;\n$lines =~ s/[\\r\\n]*$//;\n\nmy %scores = ();\nfor(my $i = 0; $i < $lines; $i++){\n\tmy $s = ;\n\tmy @arr = split /[ \\r\\n]+/, $s;\n\t$scores{$arr[0]} = 0 unless exists $scores{$arr[0]};\n\t$scores{$arr[0]} = $arr[1]-0 if $scores{$arr[0]} < $arr[1];\n}\nmy $users = keys %scores;\nprint \"$users\\n\";\nforeach my $user (sort keys %scores){\n\tmy $worse = grep {$scores{$_} <= $scores{$user}} keys %scores;\n\tif($worse * 100 > $users * 99){\n\t\tprint \"$user pro\\n\";\n\t}elsif($worse * 100 > $users * 90){\n\t\tprint \"$user hardcore\\n\";\n\t}elsif($worse * 100 > $users * 80){\n\t\tprint \"$user average\\n\";\n\t}elsif($worse * 100 > $users * 50){\n\t\tprint \"$user random\\n\";\n\t}else{\n\t\tprint \"$user noob\\n\";\n\t}\n}\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\u2009\u2264\u2009k\u2009\u2264\u2009109). The second line contains integer n (1\u2009\u2264\u2009n\u2009<\u200910100000). 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 = <>; $_ = <>; chomp;\n$d[$_]++, $t += $_ for split //;\nwhile ($t < $k) {\n\t$i++ until $d[$i];\n\t$t += 9 - $i, $d[$i]--, $r++;\n}\nprint 0 + $r;\n\n"}], "negative_code": [{"source_code": "$k = <>; $n = <>;\n$d[$_]++, $t += $_ for split //, $n;\nwhile ($t < $k) {\n\t$i++ until $d[$i];\n\t$t += 9 - $i, $d[$i]--, $r++;\n}\nprint 0 + $r;\n\n"}], "src_uid": "d7e6e5042b8860bb2470d5a493b0adf6"} {"nl": {"description": "Vasya decided to write an anonymous letter cutting the letters out of a newspaper heading. He knows heading s1 and text s2 that he wants to send. Vasya can use every single heading letter no more than once. Vasya doesn't have to cut the spaces out of the heading \u2014 he just leaves some blank space to mark them. Help him; find out if he will manage to compose the needed text.", "input_spec": "The first line contains a newspaper heading s1. The second line contains the letter text s2. s1 \u0438 s2 are non-empty lines consisting of spaces, uppercase and lowercase Latin letters, whose lengths do not exceed 200 symbols. The uppercase and lowercase letters should be differentiated. Vasya does not cut spaces out of the heading.", "output_spec": "If Vasya can write the given anonymous letter, print YES, otherwise print NO", "sample_inputs": ["Instead of dogging Your footsteps it disappears but you dont notice anything\nwhere is your dog", "Instead of dogging Your footsteps it disappears but you dont notice anything\nYour dog is upstears", "Instead of dogging your footsteps it disappears but you dont notice anything\nYour dog is upstears", "abcdefg hijk\nk j i h g f e d c b a"], "sample_outputs": ["NO", "YES", "NO", "YES"], "notes": null}, "positive_code": [{"source_code": "$s1=<>;\n$s2=<>;\n$s1=~s/\\s+//g;\n$s2=~s/\\s+//g;\n++$h1{$_} for split'',$s1;\n++$h2{$_} for split'',$s2;\nfor(keys %h2) {\n print \"NO\" and exit if($h2{$_} > $h1{$_});\n}\nprint\"YES\"\n"}, {"source_code": "#!/bin/bash/perl\n$input=<>;\n@reference = split(\" \",$input);\n$input=<>;\n@tofind=split(\" \",$input);\n#for($x=0;$x<@tofind;++$x){print length($tofind[$x]).\"\\n\";}\n%hash;\nfor($var='a';$var ne 'aa';++$var){\n $hash{$var}=0;\n}\nfor($var='A';$var ne 'AA';++$var){\n $hash{$var}=0;\n}\n#fill the hash\nfor($i=0;$i<@reference;++$i){\n @temp=split(\"\",$reference[$i]);\n for($j=0;$j<@temp;++$j){\n ++$hash{$temp[$j]};\n }\n}\n\n#start checking the hash\n$notfound=1;\nfor($i=0;$i<@tofind && $notfound==1;++$i){\n @temp=split(\"\",$tofind[$i]);\n for($j=0;$j<@temp;++$j){\n if($hash{$temp[$j]}<=0){\n $notfound=0;\n $j=@temp;\n }\n else{\n $hash{$temp[$j]}--;\n }\n }\n}\n\nif($notfound==0){\n print \"NO\";\n}\nelse{\n print \"YES\";\n}"}, {"source_code": "$_=<>;\n$a=<>;\n\nchomp;\nchomp $a;\n\ns/ //g;\n$a=~s/ //g;\n\n@_=split//,$a;\n\nfor $i(0..@_-1){\ns/$_[$i]// or $f++\n}\n\nprint $f?\"NO\\n\":\"YES\\n\""}], "negative_code": [{"source_code": "#!/bin/bash/perl\n$input=<>;\n@reference = split(\" \",$input);\n$input=<>;\n@tofind=split(\" \",$input);\n#for($x=0;$x<@tofind;++$x){print length($tofind[$x]).\"\\n\";}\n$tofindnums=@tofind;\n$havenums=@reference;\n$found=0;\n$inflag;\n\n\n\n\nfor($i=0;$i<@tofind;++$i){\n $wordlen=length($tofind[$i]);\n $inflag=1;\n \n for($j=0;$j<@reference && $inflag;++$j){\n \n if(length($reference[$j])>= $wordlen){\n \n for($k=0;$k<=length($reference[$j])-$wordlen;++$k){\n $sub = substr($reference[$j],$k,$wordlen);\n #print \"sub found is $sub \\n\";\n if($sub eq $tofind[$i]){\n #increase found enrty\n ++$found;\n #now replenish this with new character\n @temp=split(\"\",$reference[$j]);\n for($s=$k;$s<$k+$wordlen;++$s){\n $temp[$s]='-';\n }\n $tempnew;\n for($s=0;$s<@temp;++$s){\n if($temp[$s] ne '-')\n {$tempnew=$tempnew.$temp[$s];}\n }\n $reference[$j]=$tempnew;\n \n #propogate out of array\n $k=length($reference[$j]);\n #mark outer array bbreaker\n $inflag=0;\n }\n }\n }\n }\n}\n\n\nif($found==$tofindnums){\n print \"YES\";\n}\nelse{\n #print \"found value is $found\\n expected value $tofindnums\\n\";\n print \"NO\";\n}"}, {"source_code": "#!/bin/bash/perl\n$input=<>;\n@reference = split(\" \",$input);\n$input=<>;\n@tofind=split(\" \",$input);\n#for($x=0;$x<@tofind;++$x){print length($tofind[$x]).\"\\n\";}\n%hash;\nfor($var='a';$var ne 'aa';++$var){\n $hash{$var}=0;\n}\nfor($var='A';$var ne 'AA';++$var){\n $hash{$var}=0;\n}\n#fill the hash\nfor($i=0;$i<@reference;++$i){\n @temp=split(\"\",$reference[$i]);\n for($j=0;$j<@temp;++$j){\n ++$hash{$temp[$j]};\n }\n}\n#start checking the hash\n$notfound=1;\nfor($i=0;$i<@tofind && $notfound==1;++$i){\n @temp=split(\"\",$tofind[$$i]);\n for($j=0;$j<@temp;++$j){\n if($hash{$temp[$j]}<=0){\n $notfound=0;\n $j=@temp;\n }\n }\n}\n\nif($notfound==0){\n print \"NO\";\n}\nelse{\n print \"YES\";\n}"}, {"source_code": "#!/bin/bash/perl\n$input=<>;\n@reference = split(\" \",$input);\n$input=<>;\n@tofind=split(\" \",$input);\n#for($x=0;$x<@tofind;++$x){print length($tofind[$x]).\"\\n\";}\n%hash;\nfor($var='a';$var ne 'aa';++$var){\n $hash{$var}=0;\n}\nfor($var='A';$var ne 'AA';++$var){\n $hash{$var}=0;\n}\n#fill the hash\nfor($i=0;$i<@reference;++$i){\n @temp=split(\"\",$reference[$i]);\n for($j=0;$j<@temp;++$j){\n ++$hash{$temp[$j]};\n }\n}\n#start checking the hash\n$notfound=1;\nfor($i=0;$i<@tofind && $notfound==1;++$i){\n @temp=split(\"\",$tofind[$$i]);\n for($j=0;$j<@temp;++$j){\n if($hash{$temp[$j]}<=0){\n $notfound=0;\n $j=@temp;\n }\n else{\n $hash{$temp[$j]}--;\n }\n }\n}\n\nif($notfound==0){\n print \"NO\";\n}\nelse{\n print \"YES\";\n}"}], "src_uid": "b1ef19d7027dc82d76859d64a6f43439"} {"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.\u2009g. 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$$$ \u2014 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$$$) \u2014 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$$$ \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, split //;\n\t\n\tmy $ans = 0;\n\t\n\tfor my $key ( sort keys %h ){\n\t\tif( $h{ $key } > 1 ){\n\t\t\t$ans += 2;\n\t\t\t}\n\t\telse{\n\t\t\t$ans ++;\n\t\t\t}\n\t\t}\n\t\n\tprint $ans >> 1;\n\t}"}], "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\u2009\u2265\u2009ai\u2009-\u20091 for all even i, ai\u2009\u2264\u2009ai\u2009-\u20091 for all odd i\u2009>\u20091. For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn\u2019t z-sorted.Can you make the array z-sorted?", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of elements in the array a. The second line contains n integers ai (1\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 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 \u2014 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\n#\nmy $n = ;\nchomp ( $n );\n\nmy $f = ;\nchomp ( $f );\n\nmy @arr = split m/ /, $f;\nmy @arr_two = (sort { $a <=> $b } @arr);\n\nmy @ans;\nmy $i = 0;\nmy $temp;\nwhile ( @arr_two > 0 ) {\n\n if ( $i % 2 == 0 ) {\n\tpush @ans, (shift @arr_two);}\n else {\n\tpush @ans, (pop @arr_two);}\n $i += 1;\n}\n\nprint (join \" \", @ans);\nprint \"\\n\";\n\n\n\n# never impossible\n\n"}, {"source_code": "use strict;\n\nchomp (my $n = );\nchomp (my @arr = sort {$a <=> $b} split /\\s+/, );\n\nmy (@x, @y);\nif (@arr & 1) {\n @x = @arr[0 .. (@arr >> 1)];\n @y = reverse @arr[(@arr >> 1) + 1 .. (@arr - 1)];\n} else {\n @x = @arr[0 .. (@arr >> 1) - 1];\n @y = reverse @arr[(@arr >> 1) .. (@arr - 1)];\n}\n\n# print \"@arr\\n@x\\n@y\";\n\nforeach (my $i = 0; $i < @arr; $i++) {\n if ($i & 1) {\n $arr[$i] = $y[$i >> 1]\n } else {\n $arr[$i] = $x[$i >> 1]\n }\n}\n\nprint \"@arr\";\n"}, {"source_code": "<>;\n@_ = sort {$a <=> $b} split ' ', <>;\npush @ans, shift @_, pop @_ while @_;\nprint \"@ans\""}], "negative_code": [{"source_code": "\n\nmy $n = ;\nchomp ( $n );\nmy @arr;\nmy $f = ;\nchomp ( $f );\n@arr = split m/ /, $f;\n\nmy @arr_two = sort @arr;\nmy @one, @two;\n\n\nmy $i;\nwhile ( @arr_two > 0 ) {\n\n if ( ($i+1) % 2 == 0 ) {\n\tpush @one, (shift @arr_two);\n }\n else {\n\tpush @two, (shift @arr_two);\n }\n\n $i += 1;\n}\n\nmy @two_two = reverse @two;\n\nmy @ans = ();\n$i = 0;\nwhile ( @two_two > 0 or @one ) {\n\n if ( ($i + 1) % 2 == 0 ) {\n\tpush @ans, (shift @one);}\n else {\n\tpush @ans, (shift @two_two);}\n $i += 1;\n}\n\n\n\nprint (join \" \", @ans);\nprint \"\\n\";\n\n\n"}, {"source_code": "\n#\nmy $n = ;\nchomp ( $n );\n\nmy $f = ;\nchomp ( $f );\n\nmy @arr = split m/ /, $f;\nmy @arr_two = sort @arr;\n\nmy @ans;\nmy $i = 0;\nwhile ( @arr_two > 0 ) {\n\n if ( $i % 2 == 0 ) {\n\tpush @ans, (shift @arr_two);}\n else {\n\tpush @ans, (pop @arr_two);}\n $i += 1;\n}\n\nprint (join \" \", @ans);\nprint \"\\n\";\n\n\n\n# never impossible\n\n"}, {"source_code": "\n# \nmy $n = ;\nchomp ( $n );\nmy @arr;\nmy $f = ;\nchomp ( $f );\n@arr = split m/ /, $f;\n\nmy @arr_two = sort @arr;\nmy @one, @two;\n\n\nmy $i;\nwhile ( @arr_two > 0 ) {\n\n if ( ($i+1) % 2 == 0 ) {\n\tpush @one, (shift @arr_two);\n }\n else {\n\tpush @two, (shift @arr_two);\n }\n\n $i += 1;\n}\n\nmy @two_two = reverse @one;\n\nmy @ans = ();\n$i = 0;\nwhile ( @two_two > 0 or @two ) {\n\n if ( ($i + 1) % 2 == 0 ) {\n\tpush @ans, (shift @two_two);}\n else {\n\tpush @ans, (shift @two);}\n $i += 1;\n}\n\n\n\nprint (join \" \", @ans);\nprint \"\\n\";\n\n\n"}, {"source_code": "use strict;\n\nchomp (my $n = );\nchomp (my @arr = split /\\s+/, );\n\nmy (@a1, @a0);\nforeach (my $i = 0; $i < @arr; $i++) {\n if ($i & 1) {\n push @a1, $arr[$i]\n } else {\n push @a0, $arr[$i]\n }\n}\n\n@a0 = sort {$a <=> $b} @a0;\n@a1 = sort {$b <=> $a} @a1;\n\nforeach (my $i = 0; $i < @arr; $i++) {\n if ($i & 1) {\n $arr[$i] = $a1[$i >> 1]\n } else {\n $arr[$i] = $a0[$i >> 1]\n }\n}\n\nprint \"@arr\";\n"}, {"source_code": "<>;\n@_ = sort {$a <=> $b} split ' ', <>;\npush @ans, pop @_, shift @_ while @_;\nprint \"@ans\""}], "src_uid": "2401d34db475853661d6e1e1cb5a8216"} {"nl": {"description": "You are given an array $$$a$$$ of length $$$n$$$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $$$a$$$ zero or more times: you select two different numbers in the array $$$a_i$$$ and $$$a_j$$$; you remove $$$i$$$-th and $$$j$$$-th elements from the array. For example, if $$$n=6$$$ and $$$a=[1, 6, 1, 1, 4, 4]$$$, then you can perform the following sequence of operations: select $$$i=1, j=5$$$. The array $$$a$$$ becomes equal to $$$[6, 1, 1, 4]$$$; select $$$i=1, j=2$$$. The array $$$a$$$ becomes equal to $$$[1, 4]$$$. What can be the minimum size of the array after applying some sequence of operations to it?", "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$$$) is 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^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 the minimum possible size of the array after applying some sequence of operations to it.", "sample_inputs": ["5\n6\n1 6 1 1 4 4\n2\n1 2\n2\n1 1\n5\n4 5 4 5 4\n6\n2 3 2 1 3 1"], "sample_outputs": ["0\n0\n2\n1\n0"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\tmy( $max ) = sort { $b <=> $a } values %h;\n\t\n\t$max -= ( @_ - $max );\n\t\n\tprint $max >= 0 ? $max : \n\t\t@_ % 2 == 1 ? 1 : 0;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\tmy( $max ) = sort { $b <=> $a } values %h;\n\t\n\t$max -= ( @_ - $max );\n\t\n\tprint $max < 0 ? 0 : $max;\n\t}"}], "src_uid": "c67c376c8dcb3b3901eaf45fc50cc752"} {"nl": {"description": "Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.This time mr. Boosch plans to sign 2k laws. He decided to choose exactly two non-intersecting segments of integers from 1 to n of length k and sign all laws, whose numbers fall into these segments. More formally, mr. Boosch is going to choose two integers a, b (1\u2009\u2264\u2009a\u2009\u2264\u2009b\u2009\u2264\u2009n\u2009-\u2009k\u2009+\u20091,\u2009b\u2009-\u2009a\u2009\u2265\u2009k) and sign all laws with numbers lying in the segments [a;\u00a0a\u2009+\u2009k\u2009-\u20091] and [b;\u00a0b\u2009+\u2009k\u2009-\u20091] (borders are included).As mr. Boosch chooses the laws to sign, he of course considers the public opinion. Allberland Public Opinion Study Centre (APOSC) conducted opinion polls among the citizens, processed the results into a report and gave it to the president. The report contains the absurdity value for each law, in the public opinion. As mr. Boosch is a real patriot, he is keen on signing the laws with the maximum total absurdity. Help him.", "input_spec": "The first line contains two integers n and k (2\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105, 0\u2009<\u20092k\u2009\u2264\u2009n) \u2014 the number of laws accepted by the parliament and the length of one segment in the law list, correspondingly. The next line contains n integers x1,\u2009x2,\u2009...,\u2009xn \u2014 the absurdity of each law (1\u2009\u2264\u2009xi\u2009\u2264\u2009109).", "output_spec": "Print two integers a, b \u2014 the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [a;\u00a0a\u2009+\u2009k\u2009-\u20091] and [b;\u00a0b\u2009+\u2009k\u2009-\u20091]. If there are multiple solutions, print the one with the minimum number a. If there still are multiple solutions, print the one with the minimum b.", "sample_inputs": ["5 2\n3 6 1 1 6", "6 2\n1 1 1 1 1 1"], "sample_outputs": ["1 4", "1 3"], "notes": "NoteIn the first sample mr. Boosch signs laws with numbers from segments [1;2] and [4;5]. The total absurdity of the signed laws equals 3\u2009+\u20096\u2009+\u20091\u2009+\u20096\u2009=\u200916.In the second sample mr. Boosch signs laws with numbers from segments [1;2] and [3;4]. The total absurdity of the signed laws equals 1\u2009+\u20091\u2009+\u20091\u2009+\u20091\u2009=\u20094."}, "positive_code": [{"source_code": "# betaveros's polyglot adventures\nuse strict;\nuse warnings;\n\nmy ($nLaws, $seglen) = split(' ', );\nmy @absurdities = split(' ', );\n\nmy @cumsums = (0);\n\nmy $tempsum = 0;\nforeach (@absurdities) {\n\t$tempsum += $_;\n\tpush @cumsums, $tempsum;\n}\n\nmy @segments = ();\nmy @maxsums = ();\nmy @maxis = ();\n\nmy $maxsum = 0;\nmy $maxi = 0;\nforeach my $i (0 .. (scalar @absurdities) - $seglen) {\n\tmy $segsum = $cumsums[$i + $seglen] - $cumsums[$i];\n\tpush @segments, $segsum;\n\tif ($segsum > $maxsum) {\n\t\t$maxi = $i;\n\t\t$maxsum = $segsum;\n\t}\n\tpush @maxsums, $maxsum;\n\tpush @maxis, $maxi;\n}\n\nmy $maxsign = 0;\nmy $maxsi1 = 0;\nmy $maxsi2 = 0;\nforeach my $i ($seglen .. (scalar @absurdities) - $seglen) {\n\tmy $signsum = $maxsums[$i - $seglen] + $segments[$i];\n\tif ($signsum > $maxsign) {\n\t\t$maxsign = $signsum;\n\t\t$maxsi1 = $maxis[$i - $seglen];\n\t\t$maxsi2 = $i;\n\t}\n} \nprint ($maxsi1 + 1, \" \", $maxsi2 + 1);\n"}], "negative_code": [], "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.\u00a0e. $$$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$$$)\u00a0\u2014 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": "use warnings;\nuse strict;\nuse constant NL => qq/\\n/;\nuse integer;\n\n;\nmy @arr = sort { $a <=> $b } ( split qq/ /, );\nmy $i = 0;\nwhile ( @arr > 1 ) {\n pop @arr if ($i % 2 == 0);\n shift @arr if ($i % 2 == 1);\n $i += 1;\n}\nprint $arr [ 0 ];\nprint NL;\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = sort { $a <=> $b } split ' ', <>;\n\t\n\tprint $_[ ( @_ - 1 ) / 2 ];\n\t}"}], "negative_code": [{"source_code": "use warnings;\nuse strict;\nuse constant NL => qq/\\n/;\n\n;\nmy @arr = sort ( split qq/ /, );\nprint $arr [ int ( $#arr / 2) ];\nprint NL;\n\n"}, {"source_code": "use warnings;\nuse strict;\nuse constant NL => qq/\\n/;\n\n;\nmy @arr = sort ( split qq/ /, );\nmy $i = 0;\nwhile ( @arr > 1 ) {\n pop @arr if $i % 2 == 0;\n shift @arr if $i % 2 == 1;\n $i += 1;\n}\nprint $arr [ 0 ];\nprint NL;\n\n"}], "src_uid": "f93a8bd64a405233342f84542fce314d"} {"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),\u2009...,\u2009yn(x) of the given type as function s(x)\u2009=\u2009y1(x)\u2009+\u2009...\u2009+\u2009yn(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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of functions. Each of the following n lines contains two space-separated integer numbers ki,\u2009bi (\u2009-\u2009109\u2009\u2264\u2009ki,\u2009bi\u2009\u2264\u2009109) that determine the i-th function.", "output_spec": "Print a single number \u2014 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": "#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmy %hash;\n\nsub gcd {\n my $a = abs (shift);\n my $b = abs (shift);\n while ($b > 0) {\n $b ^= $a ^= $b ^= $a %= $b;\n }\n return $a;\n}\n\nmy $n = <>;\nfor(1..$n) {\n my $line = <>;\n my @arr = split(/ /, $line);\n my $k = $arr[0];\n my $b = $arr[1];\n next if (0 == $k);\n my $g = gcd($k, $b);\n $k /= $g;\n $b /= $g;\n if ($k < 0) {\n $k = -$k;\n $b = -$b;\n }\n $hash{($k, $b)} = 1;\n}\nprint \"\".keys( %hash ).\"\\n\";\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmy %hash;\n\nsub gcd {\n my $a = shift;\n my $b = shift;\n return $b ? gcd($b, $a % $b) : $a;\n}\n\nmy $n = <>;\nfor(1..$n) {\n my $line = <>;\n my @arr = split(/ /, $line);\n my $k = $arr[0];\n my $b = $arr[1];\n next if (0 == $k);\n $hash{10000000 * $b / $k} = 1;\n}\nprint \"\".keys( %hash ).\"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmy %hash;\n\nsub gcd {\n my $a = shift;\n my $b = shift;\n return $b ? gcd($b, $a % $b) : $a;\n}\n\nmy $n = <>;\nfor(1..$n) {\n my $line = <>;\n my @arr = split(/ /, $line);\n my $k = $arr[0];\n my $b = $arr[1];\n next if (0 == $k);\n my $g = gcd($b, $k);\n $b /= $g;\n $k /= $g;\n $hash{$b}++; \n}\nprint \"\".keys( %hash ).\"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmy %hash;\n\nsub gcd {\n my $a = shift;\n my $b = shift;\n while ($b > 0) {\n $b ^= $a ^= $b ^= $a %= $b;\n }\n return $a;\n}\n\nmy $n = <>;\nfor(1..$n) {\n my $line = <>;\n my @arr = split(/ /, $line);\n my $k = $arr[0];\n my $b = $arr[1];\n next if (0 == $k);\n my $g = gcd($k, $b);\n \n $hash{($k / $g, $b / $g)} = 1;\n}\nprint \"\".keys( %hash ).\"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmy %hash;\n\nsub gcd {\n my $a = shift;\n my $b = shift;\n while ($b > 0) {\n $b ^= $a ^= $b ^= $a %= $b;\n }\n return $a;\n}\n\nmy $n = <>;\nfor(1..$n) {\n my $line = <>;\n my @arr = split(/ /, $line);\n my $k = $arr[0];\n my $b = $arr[1];\n next if (0 == $k);\n my $g = gcd($k, $b);\n $k /= $g;\n $b /= $g;\n if ($k < 0) {\n $k = -$k;\n $b = -$b;\n }\n $hash{($k, $b)} = 1;\n}\nprint \"\".keys( %hash ).\"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmy %hash;\n\nsub gcd {\n my $a = shift;\n my $b = shift;\n return $b ? gcd($b, $a % $b) : $a;\n}\n\nmy $n = <>;\nfor(1..$n) {\n my $line = <>;\n my @arr = split(/ /, $line);\n my $k = $arr[0];\n my $b = $arr[1];\n next if (0 == $k);\n my $g = gcd($b, $k);\n $b /= $g;\n $k /= $g;\n $hash{1000 * $b / $k} = 1;\n}\nprint \"\".keys( %hash ).\"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmy %hash;\n\nsub gcd {\n my $a = shift;\n my $b = shift;\n return $b ? gcd($b, $a % $b) : $a;\n}\n\nmy $n = <>;\nfor(1..$n) {\n my $line = <>;\n my @arr = split(/ /, $line);\n my $k = $arr[0];\n my $b = $arr[1];\n next if (0 == $k);\n $hash{$b / $k} = 1; \n}\nprint \"\".keys( %hash ).\"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmy %hash;\n\nsub gcd {\n my $a = shift;\n my $b = shift;\n return $b ? gcd($b, $a % $b) : $a;\n}\n\nmy $n = <>;\nfor(1..$n) {\n my $line = <>;\n my @arr = split(/ /, $line);\n my $k = $arr[0];\n my $b = $arr[1];\n next if (0 == $k);\n $hash{1000000000 * $b / $k} = 1;\n}\nprint \"\".keys( %hash ).\"\\n\";\n"}], "src_uid": "d436c7a3b7f07c9f026d00bdf677908d"} {"nl": {"description": "Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters.The level he's currently on contains $$$n$$$ monsters. The $$$i$$$-th of them appears $$$k_i$$$ seconds after the start of the level and has $$$h_i$$$ health points. As an additional constraint, $$$h_i \\le k_i$$$ for all $$$1 \\le i \\le n$$$. All $$$k_i$$$ are different.Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $$$1, 2, 3, \\dots$$$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $$$1$$$. Otherwise, let the damage at the previous second be $$$x$$$. Then he can choose the damage to be either $$$x + 1$$$ or $$$1$$$. A spell uses mana: casting a spell with damage $$$x$$$ uses $$$x$$$ mana. Mana doesn't regenerate.To kill the $$$i$$$-th monster, Monocarp has to cast a spell with damage at least $$$h_i$$$ at the exact moment the monster appears, which is $$$k_i$$$.Note that Monocarp can cast the spell even when there is no monster at the current second.The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters.It can be shown that it's always possible to kill all monsters under the constraints of the problem.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of testcases. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$)\u00a0\u2014 the number of monsters in the level. The second line of the testcase contains $$$n$$$ integers $$$k_1 < k_2 < \\dots < k_n$$$ ($$$1 \\le k_i \\le 10^9$$$)\u00a0\u2014 the number of second from the start the $$$i$$$-th monster appears at. All $$$k_i$$$ are different, $$$k_i$$$ are provided in the increasing order. The third line of the testcase contains $$$n$$$ integers $$$h_1, h_2, \\dots, h_n$$$ ($$$1 \\le h_i \\le k_i \\le 10^9$$$)\u00a0\u2014 the health of the $$$i$$$-th monster. The sum of $$$n$$$ over all testcases doesn't exceed $$$10^4$$$.", "output_spec": "For each testcase, print a single integer\u00a0\u2014 the least amount of mana required for Monocarp to kill all monsters.", "sample_inputs": ["3\n\n1\n\n6\n\n4\n\n2\n\n4 5\n\n2 2\n\n3\n\n5 7 9\n\n2 1 2"], "sample_outputs": ["10\n6\n7"], "notes": "NoteIn the first testcase of the example, Monocarp can cast spells $$$3, 4, 5$$$ and $$$6$$$ seconds from the start with damages $$$1, 2, 3$$$ and $$$4$$$, respectively. The damage dealt at $$$6$$$ seconds is $$$4$$$, which is indeed greater than or equal to the health of the monster that appears.In the second testcase of the example, Monocarp can cast spells $$$3, 4$$$ and $$$5$$$ seconds from the start with damages $$$1, 2$$$ and $$$3$$$, respectively.In the third testcase of the example, Monocarp can cast spells $$$4, 5, 7, 8$$$ and $$$9$$$ seconds from the start with damages $$$1, 2, 1, 1$$$ and $$$2$$$, respectively."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\tmy $n = $_;\n\t\n\tmy @k = split ' ', <>;\n\tmy @h = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\twhile( @k ){\n\t\tmy $k = pop @k;\n\t\tmy $h = pop @h;\n\t\t\n\t\tif( @k ){\n\t\t\tif( $k[ -1 ] + $h > $k ){\n\t\t\t\tmy $k2 = pop @k;\n\t\t\t\tmy $h2 = pop @h;\n\t\t\t\t\n\t\t\t\tpush @k, $k;\n\t\t\t\tpush @h, $h2 + $k - $k2 > $h ? $h2 + $k - $k2 : $h;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$sum += $h * ( $h + 1 ) / 2;\n\t\t\t\t}\n\t\t\t}\n\t\telse{\n\t\t\t$sum += $h * ( $h + 1 ) / 2;\n\t\t\t}\n\t\t}\n\t\n\tprint $sum;\n\t}"}], "negative_code": [], "src_uid": "690c28ef1275035895b9154ff0e17f4c"} {"nl": {"description": "Polycarp has n dice d1,\u2009d2,\u2009...,\u2009dn. 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,\u2009d2,\u2009...,\u2009dn. 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\u2009=\u200911, 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,\u2009A (1\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105,\u2009n\u2009\u2264\u2009A\u2009\u2264\u2009s) \u2014 the number of dice and the sum of shown values where s\u2009=\u2009d1\u2009+\u2009d2\u2009+\u2009...\u2009+\u2009dn. The second line contains n integers d1,\u2009d2,\u2009...,\u2009dn (1\u2009\u2264\u2009di\u2009\u2264\u2009106), where di is the maximum value that the i-th dice can show.", "output_spec": "Print n integers b1,\u2009b2,\u2009...,\u2009bn, 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": "$\\ = $/;\nwhile(<>){\n($n,$A)=split;\n@d=split \" \",<>;\n$sum = 0;\n$sum += $_ for @d;\n#print \"$sum\";\n#$diff = $sum - $A;\n\n@e = ();\n\n#$e = ($_ - 1) + $diff, ($e < 0 and $e = 0), \n#$f = ($diff > 0? $diff : 0) - ($n - 1), \n#($f < 0 and $f = 0), \n\n#(print ($sum - $_)),\n#(print ($sum - ($n - 1))),\n$e = $A - ($sum - $_),\n$f = $A - ($n - 1),\n$r = $_ - $f,\n($r < 0 and $r = 0),\n$l = $e - 1,\n($l < 0 and $l = 0),\n$q = $r + $l,\n\n#push @e, $e .\" - \". $f .\" = \".($_ - ($f - $e + 1) ).\" <$r><$l>[$q]|\" for @d;\npush @e, $q for @d;\n\n#print \":@e\";\nprint \"@e\";\n}"}, {"source_code": "use List::Util qw(max sum);\n$\\ = $/;\n\nwhile(<>){\n\t($n, $A) = split;\n\t@d = split \" \", <>;\n\t$sum = sum @d;\n\t\n\t@e = ();\n\n\tpush @e, 0\n\t+\t(max $_ - $A + $n - 1, 0)\n\t+\t(max $_ + $A - $sum - 1, 0)\n\tfor @d;\n\t\n\tprint \"@e\"\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\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u20091018, 2\u2009\u2264\u2009k\u2009\u2264\u2009109).", "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\u2009=\u20091, 21\u2009=\u20092, 22\u2009=\u20094, 23\u2009=\u20098 lie within the specified range. The number 24\u2009=\u200916 is greater then 10, thus it shouldn't be printed."}, "positive_code": [{"source_code": "use bigint;\n\n($l, $r, $k) = split ' ', <>;\n\t\nprint( ( join \" \", grep $_ <= $r && $l <= $_ , map $k ** ($_ + 0), 0 .. 65 ) || -1 )"}, {"source_code": "use bigint;\n\n$\\ = $/;\n\nwhile(<>){\n\t($l, $r, $k) = split;\n\t\n\t$ans = join \" \", grep $_ <= $r, grep $l <= $_ , map $k ** ($_ + 0), 0 .. 65;\n\t\n\tprint $ans || -1\n\t\n\t}"}], "negative_code": [{"source_code": "use bigint;\n\n$\\ = $/;\n\nwhile(<>){\n\t($l, $r, $k) = split;\n\t\n\t$ans = join \" \", grep $_ <= $r, grep $l <= $_ , map $k ** $_, 0 .. 65;\n\t\n\tprint $ans || -1\n\t\n\t}"}], "src_uid": "8fcec28fb4d165eb58f829c03e6b31d1"} {"nl": {"description": "The robot is placed in the top left corner of a grid, consisting of $$$n$$$ rows and $$$m$$$ columns, in a cell $$$(1, 1)$$$.In one step, it can move into a cell, adjacent by a side to the current one: $$$(x, y) \\rightarrow (x, y + 1)$$$; $$$(x, y) \\rightarrow (x + 1, y)$$$; $$$(x, y) \\rightarrow (x, y - 1)$$$; $$$(x, y) \\rightarrow (x - 1, y)$$$. The robot can't move outside the grid.The cell $$$(s_x, s_y)$$$ contains a deadly laser. If the robot comes into some cell that has distance less than or equal to $$$d$$$ to the laser, it gets evaporated. The distance between two cells $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$.Print the smallest number of steps that the robot can take to reach the cell $$$(n, m)$$$ without getting evaporated or moving outside the grid. If it's not possible to reach the cell $$$(n, m)$$$, print -1.The laser is neither in the starting cell, nor in the ending cell. The starting cell always has distance greater than $$$d$$$ to the laser.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of testcases. The only line of each testcase contains five integers $$$n, m, s_x, s_y, d$$$ ($$$2 \\le n, m \\le 1000$$$; $$$1 \\le s_x \\le n$$$; $$$1 \\le s_y \\le m$$$; $$$0 \\le d \\le n + m$$$)\u00a0\u2014 the size of the grid, the cell that contains the laser and the evaporating distance of the laser. The laser is neither in the starting cell, nor in the ending cell ($$$(s_x, s_y) \\neq (1, 1)$$$ and $$$(s_x, s_y) \\neq (n, m)$$$). The starting cell $$$(1, 1)$$$ always has distance greater than $$$d$$$ to the laser ($$$|s_x - 1| + |s_y - 1| > d$$$).", "output_spec": "For each testcase, print a single integer. If it's possible to reach the cell $$$(n, m)$$$ from $$$(1, 1)$$$ without getting evaporated or moving outside the grid, then print the smallest amount of steps it can take the robot to reach it. Otherwise, print -1.", "sample_inputs": ["3\n\n2 3 1 3 0\n\n2 3 1 3 1\n\n5 5 3 4 1"], "sample_outputs": ["3\n-1\n8"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m, $sx, $sy, $d ) = split;\n\t\n\tmy( $T, $B, $L, $R ) = ( 1 ) x 4;\n\t\n\tif( $sx - $d <= 1 ){\n\t\t$T = 0;\n\t\t}\n\tif( $sx + $d > $n - 1 ){\n\t\t$B = 0;\n\t\t}\n\tif( $sy - $d <= 1 ){\n\t\t$L = 0;\n\t\t}\n\tif( $sy + $d > $m - 1 ){\n\t\t$R = 0;\n\t\t}\n\t\n\tif( $T && $R or $L && $B ){\n\t\tprint $n + $m - 2;\n\t\t}\n\telse{\n\t\tprint -1;\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m, $sx, $sy, $d ) = split;\n\t\n\tmy( $T, $B, $L, $R ) = ( 1 ) x 4;\n\t\n\tif( $sx - $d <= 0 ){\n\t\t$T = 0;\n\t\t}\n\tif( $sx + $d > $n - 1 ){\n\t\t$B = 0;\n\t\t}\n\tif( $sy - $d <= 0 ){\n\t\t$L = 0;\n\t\t}\n\tif( $sy + $d > $m - 1 ){\n\t\t$R = 0;\n\t\t}\n\t\n\tif( $T && $R or $L && $B ){\n\t\tprint $n + $m - 2;\n\t\t}\n\telse{\n\t\tprint -1;\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m, $sx, $sy, $d ) = split;\n\t\n\tmy( $T, $B, $L, $R ) = ( 1 ) x 4;\n\t\n\tif( $sx - $d <= 0 ){\n\t\t$T = 0;\n\t\t}\n\tif( $sx + $d > $n - 1 ){\n\t\t$B = 0;\n\t\t}\n\tif( $sy - $d <= 0 ){\n\t\t$L = 0;\n\t\t}\n\tif( $sx - $d > $m - 1 ){\n\t\t$R = 0;\n\t\t}\n\t\n\tif( $T && $R or $L && $B ){\n\t\tprint $n + $m - 2;\n\t\t}\n\telse{\n\t\tprint -1;\n\t\t}\n\t}"}], "src_uid": "fe7186f7d026eb4cd2e1afda75ac9fcd"} {"nl": {"description": "The hero is addicted to glory, and is fighting against a monster. The hero has $$$n$$$ skills. The $$$i$$$-th skill is of type $$$a_i$$$ (either fire or frost) and has initial damage $$$b_i$$$. The hero can perform all of the $$$n$$$ skills in any order (with each skill performed exactly once). When performing each skill, the hero can play a magic as follows: If the current skill immediately follows another skill of a different type, then its damage is doubled. In other words, If a skill of type fire and with initial damage $$$c$$$ is performed immediately after a skill of type fire, then it will deal $$$c$$$ damage; If a skill of type fire and with initial damage $$$c$$$ is performed immediately after a skill of type frost, then it will deal $$$2c$$$ damage; If a skill of type frost and with initial damage $$$c$$$ is performed immediately after a skill of type fire, then it will deal $$$2c$$$ damage; If a skill of type frost and with initial damage $$$c$$$ is performed immediately after a skill of type frost , then it will deal $$$c$$$ damage. Your task is to find the maximum damage the hero can deal. ", "input_spec": "Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^5$$$) \u2014 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 10^5$$$), indicating the number of skills. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\leq a_i \\leq 1$$$), where $$$a_i$$$ indicates the type of the $$$i$$$-th skill. Specifically, the $$$i$$$-th skill is of type fire if $$$a_i = 0$$$, and of type frost if $$$a_i = 1$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\leq b_i \\leq 10^9$$$), where $$$b_i$$$ indicates the initial damage of the $$$i$$$-th skill. 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 maximum damage the hero can deal.", "sample_inputs": ["4\n\n4\n\n0 1 1 1\n\n1 10 100 1000\n\n6\n\n0 0 0 1 1 1\n\n3 4 5 6 7 8\n\n3\n\n1 1 1\n\n1000000000 1000000000 1000000000\n\n1\n\n1\n\n1"], "sample_outputs": ["2112\n63\n3000000000\n1"], "notes": "NoteIn the first test case, we can order the skills by $$$[3, 1, 4, 2]$$$, and the total damage is $$$100 + 2 \\times 1 + 2 \\times 1000 + 10 = 2112$$$.In the second test case, we can order the skills by $$$[1, 4, 2, 5, 3, 6]$$$, and the total damage is $$$3 + 2 \\times 6 + 2 \\times 4 + 2 \\times 7 + 2 \\times 5 + 2 \\times 8 = 63$$$.In the third test case, we can order the skills by $$$[1, 2, 3]$$$, and the total damage is $$$1000000000 + 1000000000 + 1000000000 = 3000000000$$$.In the fourth test case, there is only one skill with initial damage $$$1$$$, so the total damage is $$$1$$$. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse Data::Dumper;\nuse integer; ### important!\nmy $mod = 10 ** 9 + 7;\n# my $mod = 998244353;\n\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\n\nwhile( $testcase -- > 0 ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @A = map { $_ - 0 } split(/\\s+/,);\n my @B = map { $_ - 0 } split(/\\s+/,);\n \n my @sa0 = (); my @sb0 = ();\n for(my $i=0;$i<$n;$i++){\n if( $A[$i] == 0 ){\n push(@sa0,$B[$i]);\n } else {\n push(@sb0,$B[$i]);\n }\n }\n \n my @sa = sort { $b <=> $a } @sa0;\n my @sb = sort { $b <=> $a } @sb0;\n \n my @sa2 = (@sa);\n my @sb2 = (@sb);\n \n my $r1 =0;\n if( @sa ){\n $r1 = pop @sa;\n my $sw = 1;\n while( @sa or @sb ){\n if( @sa and @sb ){\n my $v;\n if( $sw == 1 ){\n $v = shift @sb;\n } else {\n $v = shift @sa;\n }\n $sw ^= 1;\n $r1 += 2 * $v;\n } elsif( @sa ){\n my $v = shift @sa;\n $v *= ( $sw == 0 ? 2 : 1);\n $r1 += $v;\n $sw = -1;\n } elsif( @sb ){\n my $v = shift @sb;\n $v *= ( $sw == 1 ? 2 : 1);\n $r1 += $v;\n $sw = -1;\n }\n }\n }\n \n my $r2 = 0;\n if( @sb2 ){\n $r2 = pop @sb2;\n my $sw = 0;\n while( @sa2 or @sb2 ){\n if( @sa2 and @sb2 ){\n my $v;\n if( $sw == 1 ){\n $v = shift @sb2;\n } else {\n $v = shift @sa2;\n }\n $sw ^= 1;\n $r2 += 2 * $v;\n } elsif( @sa2 ){\n my $v = shift @sa2;\n $v *= ( $sw == 0 ? 2 : 1);\n $r2 += $v;\n $sw = -1;\n } elsif( @sb2 ){\n my $v = shift @sb2;\n $v *= ( $sw == 1 ? 2 : 1);\n $r2 += $v;\n $sw = -1;\n }\n }\n }\n print ( ( &max($r1,$r2) ) . \"\\n\" );\n}\n\nexit(0);\n\nsub max {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\n return $r;\n}\nsub min {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\n return $r;\n}\nsub sum {\n my $r = 0;\n foreach my $e (@_){ $r += $e; }\n return $r;\n}\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\n my $r = shift; my $ar = shift;\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\n for(my $i=0;$i<=$#ar;$i++){ \n $rr += $ar->[$i];\n $r->[1+$i] = $rr;\n }\n}\n"}], "negative_code": [], "src_uid": "bc1587d3affd867804e664fdb38ed765"} {"nl": {"description": "You are given n points on the straight line \u2014 the positions (x-coordinates) of the cities and m points on the same line \u2014 the positions (x-coordinates) of the cellular towers. All towers work in the same way \u2014 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\u2009=\u20090 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009105) \u2014 the number of cities and the number of cellular towers. The second line contains a sequence of n integers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009109\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 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,\u2009b2,\u2009...,\u2009bm (\u2009-\u2009109\u2009\u2264\u2009bj\u2009\u2264\u2009109) \u2014 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": "sub get { split \" \", <> }\nsub tag { my $tag = shift; map { [$tag, $_] } @_ }\nsub max { $_[0] > $_[1]? $_[0]: $_[1] }\nsub min { $_[0] < $_[1]? $_[0]: $_[1] }\n\n<>; @n = get; @m = get;\n@a = sort {$a->[1] <=> $b->[1]} tag(0, @n), tag(1, @m);\nunshift @a, [1, -4e9]; push @a, [1, 4e9];\n@s = map $_->[0], @a; $s = join \"\", @s;\n\nwhile ($s =~ /10+(?=1)/g) {\n\t($i, $j) = ($-[0], $+[0]);\n\t$m1 = $a[$i]->[1];\n\t$m2 = $a[$j]->[1];\n\twhile (++$i < $j) {\n\t\t$v = $a[$i]->[1];\n\t\t$maxval = max($maxval, min(abs($m1 - $v), abs($m2 - $v)))\n\t}\n}\nprint $maxval, \"\\n\";\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m) = split;\n\t@n = split ' ', <>;\n\t@m = split ' ', <>;\n\tpush @m, 5e10;\n\t\n\t$left = -5e10;\n\t\n\t@ans = ();\n\t\n\tfor (@n){\n\t\t\n\t\twhile( $m[0] < $_ ){\n\t\t\t$left = shift @m;\n\t\t\t}\n\t\t\n\t\tpush @ans, (sort {$a <=> $b} $_ - $left, $m[0] - $_)[ 0 ];\n\t\t}\n\t\n\tprint +(sort {$b <=> $a} @ans)[ 0 ];\n\t}"}], "negative_code": [{"source_code": "sub get { split \" \", <> }\nsub tag { my $tag = shift; map { [$tag, $_] } @_ }\nsub max { $_[0] > $_[1]? $_[0]: $_[1] }\nsub min { $_[0] < $_[1]? $_[0]: $_[1] }\n\n<>; @n = get; @m = get;\n@a = sort {$a->[1] <=> $b->[1]} tag(0, @n), tag(1, @m);\nunshift @a, [1, -2e9]; push @a, [1, 2e9];\n@s = map $_->[0], @a; $s = join \"\", @s;\n\nwhile ($s =~ /10+(?=1)/g) {\n\t($i, $j) = ($-[0], $+[0]);\n\t$m1 = $a[$i]->[1];\n\t$m2 = $a[$j]->[1];\n\twhile (++$i < $j) {\n\t\t$v = $a[$i]->[1];\n\t\t$maxval = max($maxval, min(abs($m1 - $v), abs($m2 - $v)))\n\t}\n}\nprint $maxval, \"\\n\";\n"}], "src_uid": "9fd8e75cb441dc809b1b2c48c4012c76"} {"nl": {"description": "The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts $$$h$$$ hours and each hour lasts $$$m$$$ minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from $$$0$$$ to $$$h-1$$$ and minutes are numbered from $$$0$$$ to $$$m-1$$$. That's how the digits are displayed on the clock. Please note that digit $$$1$$$ is placed in the middle of its position. A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).The image of the clocks in the mirror is reflected against a vertical axis. The reflection is not a valid time.The reflection is a valid time with $$$h=24$$$, $$$m = 60$$$. However, for example, if $$$h=10$$$, $$$m=60$$$, then the reflection is not a valid time. An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment $$$s$$$ and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.It can be shown that with any $$$h$$$, $$$m$$$, $$$s$$$ such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.You are asked to solve the problem for several test cases.", "input_spec": "The first line contains a single integer $$$T$$$ ($$$1 \\le T \\le 100$$$) \u2014 the number of test cases. The next $$$2 \\cdot T$$$ lines contain the description of test cases. The description of each test case consists of two lines. The first line of a test case contains two integers $$$h$$$, $$$m$$$ ($$$1 \\le h, m \\le 100$$$). The second line contains the start time $$$s$$$ in the described format HH:MM.", "output_spec": "For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.", "sample_inputs": ["5\n24 60\n12:21\n24 60\n23:59\n90 80\n52:26\n1 100\n00:01\n10 10\n04:04"], "sample_outputs": ["12:21\n00:00\n52:28\n00:00\n00:00"], "notes": "NoteIn the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nmy @rev = (0,1,5,-1,-1,2,-1,-1,8,-1);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($h,$m) = map { $_ - 0 } split(/\\s+/,);\r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n $s =~ /(\\d\\d):(\\d\\d)/;\r\n my $h1 = $1;\r\n my $m1 = $2;\r\n \r\n my $ctm = ( $h1 - 0 ) * $m + $m1;\r\n my $hm = $h * $m;\r\n my $r = -1;\r\n for(my $i=0;$i<$hm;$i++){\r\n my $c1 = $ctm + $i;\r\n $c1 -= $hm if( $c1 >= $hm );\r\n my $m2 = $c1 % $m;\r\n my $h2 = ($c1 - $m2)/$m;\r\n my $hm2 = sprintf(\"%02d%02d\",$h2,$m2);\r\n my $hm3 = '';\r\n for(my $j=0;$j<4;$j++){\r\n my $rv = $rev[substr($hm2,3-$j,1)-0];\r\n if( $rv<0 ){\r\n $hm3 = '';\r\n last;\r\n }\r\n $hm3 .= $rv;\r\n }\r\n next if $hm3 eq '';\r\n if( substr($hm3,0,2) - 0 < $h and substr($hm3,2,2) - 0 < $m ){\r\n $r = $c1;\r\n last;\r\n }\r\n }\r\n my $m4 = $r % $m;\r\n my $h4 = ($r - $m4)/$m;\r\n print sprintf(\"%02d:%02d\\n\",$h4,$m4);\r\n \r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [], "src_uid": "7f28e4dbd199b84bd7885bf7f479cf38"} {"nl": {"description": "Theater stage is a rectangular field of size n\u2009\u00d7\u2009m. 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)\u00a0\u2014 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20091000)\u00a0\u2014 the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each\u00a0\u2014 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\u00a0\u2014 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": "use integer;\n\n$,=\" \";\nmy ($n,$m)=split / /,<>;\nchomp($n,$m);\nmy @GRID;\nmy $count_=0;\nmy $before=0;\nmy $after=0;\nmy $c=0;\nmy $kki=0;\n$n--;\n$m--;\nmy @t1=(0..$n);\nmy @t2=(0..$m);\nwhile(<>)\n{\n $before=0;\n $after=0;\n $c=0;\n chomp;\n s/ //g;\n push @GRID,$_;\n if(/1/)\n {\n for my $j (@t2)\n {\n if(!substr($GRID[$kki],$j,1)){\n $c++;\n $count_++ if($before);\n }elsif(substr($GRID[$kki],$j,1))\n {\n if($before)\n {\n $count_+=$c;\n $c=0;\n }\n else\n {\n $before=1;\n $count_+=$c;\n }\n $c=0;\n }\n }\n }\n $kki++;\n}\n\nfor my $i (@t2)\n{\n $before=undef;\n $after=undef;\n $c=undef;\n for my $j (@t1)\n {\n if(!substr($GRID[$j],$i,1)){\n $c++;\n $count_++ if($before);\n }elsif(substr($GRID[$j],$i,1))\n {\n if($before)\n {\n $count_+=$c;\n $c=0;\n }\n else\n {\n $before=1;\n $count_+=$c;\n }\n $c=0;\n }\n }\n}\n\nprint $count_ . \"\\n\";\n"}], "negative_code": [], "src_uid": "c3a7d82f6c3cf8678a1c7c521e0a5f51"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009103).", "output_spec": "Print the length of the sequence of questions k (0\u2009\u2264\u2009k\u2009\u2264\u2009n), followed by k numbers \u2014 the questions yi (1\u2009\u2264\u2009yi\u2009\u2264\u2009n). 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": "\t$_ = <>;\n\n\tfor $i (2 .. $_){\n\t\t$ii = $i;\n\t\t$g = 0;\n\t\tfor $j (2 .. $i){\n\t\t\tif ($ii and not $ii % $j and not $g ++){\n\t\t\t\t$ii /= $j while not $ii % $j;\n\t\t\t\t}\n\t\t\t$g and $no[$i] = $ii > 1;\n\t\t\t}\n\t\t}\n\n\t@e = grep 1 > $no[$_], 2 .. $_;\t\t\n\tprint @e . \"\\n@e\""}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t@no = ();\n\t\n\tfor $i (2 .. $_){\n\t\t$ii = $i;\n\t\t$g = 0;\n\t\tfor $j (2 .. $i){\n\t\t\tif ($ii and not $ii % $j){\n\t\t\t\t++ $g;\n\t\t\t\t$ii /= $j while not $ii % $j;\n\t\t\t\t}\n\t\t\t$g and $ii > 1 and $no[$i] = 1;\n\t\t\t}\n\t\t\n\t\t}\n\t\t@e = grep ! $no[$_], 2 .. $_;\t\t\n\t\tprint $_ > 1 ? (scalar @e, \"\\n@e\") : 0\n\t\n\t}"}], "negative_code": [], "src_uid": "7f61b1d0e8294db74d33a6b6696989ad"} {"nl": {"description": "When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the i-th book.Valera decided to choose an arbitrary book with number i and read the books one by one, starting from this book. In other words, he will first read book number i, then book number i\u2009+\u20091, then book number i\u2009+\u20092 and so on. He continues the process until he either runs out of the free time or finishes reading the n-th book. Valera reads each book up to the end, that is, he doesn't start reading the book if he doesn't have enough free time to finish reading it. Print the maximum number of books Valera can read.", "input_spec": "The first line contains two integers n and t (1\u2009\u2264\u2009n\u2009\u2264\u2009105;\u00a01\u2009\u2264\u2009t\u2009\u2264\u2009109) \u2014 the number of books and the number of free minutes Valera's got. The second line contains a sequence of n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009104), where number ai shows the number of minutes that the boy needs to read the i-th book.", "output_spec": "Print a single integer \u2014 the maximum number of books Valera can read.", "sample_inputs": ["4 5\n3 1 2 1", "3 3\n2 2 3"], "sample_outputs": ["3", "1"], "notes": null}, "positive_code": [{"source_code": "while (<>){\n\nchomp;\n($n, $t) = split/ /,$_;\n$_=<>;\nchomp;\n@_=split/ /,$_;\n\n# print \"$n $t\\n\";\n# print \"@_\\n\";\n\n$sum=0;\n$ms=0;\n@m=();\n\nwhile (@_){\n\n# print \" >> @_\\n\";\nif ($sum<=$t){\n push @m,($m=shift@_); \n# print \" _> @_\\n\";\n # print \" m> @m\\n\";\n $sum+=$m;\n }\nif ($sum>$t) {\n $m=shift@m; $sum-=$m;\n# print \" -m> @m\\n\";\n }\n \n if ($ms < @m){$ms = scalar @m}\n\n}\n\n# print \"$sum\\n\";\n print \"$ms\\n\";\n# print \"\\n\";\n\n}"}], "negative_code": [], "src_uid": "ef32a8f37968629673547db574261a9d"} {"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,\u20092,\u20092,\u20092,\u20091,\u20092] (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\u2009=\u20091 is not greater than a2\u2009=\u20092; the second monster can't eat the third monster because a2\u2009=\u20092 is not greater than a3\u2009=\u20092; 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,\u20092,\u20092,\u20091,\u20092]. After some time, someone said a good joke and all monsters recovered. At that moment there were k (k\u2009\u2264\u2009n) 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\u2009\u2264\u2009n\u2009\u2264\u2009500)\u00a0\u2014 the number of monsters in the initial queue. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009106)\u00a0\u2014 the initial weights of the monsters. The third line contains single integer k (1\u2009\u2264\u2009k\u2009\u2264\u2009n)\u00a0\u2014 the number of monsters in the queue after the joke. The fourth line contains k integers b1,\u2009b2,\u2009...,\u2009bk (1\u2009\u2264\u2009bj\u2009\u2264\u20095\u00b7108)\u00a0\u2014 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\u2009-\u2009k lines print actions in the chronological order. In each line print x\u00a0\u2014 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\u2009=\u20096 monsters, their weights are [1,\u20092,\u20092,\u20092,\u20091,\u20092] (in order of queue from the first monster to the last monster). The final queue should be [5,\u20095]. 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,\u20092,\u20092,\u20091,\u20092]; 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,\u20092,\u20091,\u20092]; the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes [5,\u20092,\u20093]; the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes [5,\u20095]. Note that for each step the output contains numbers of the monsters in their current order in the queue."}, "positive_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t$n = $_;\n\t@n = split ' ', <>;\n\tchomp( $m = <> );\n\t@m = split ' ', <>;\n\t\n\t$f = 0;\n\t$k = 0;\n\t$jj = 0;\n\t@ans = ();\n\t\n\tfor $j (@m){\n\t\t$jj ++;\n\t\t$f and last;\n\t\t\n\t\t$sum = 0;\n\t\t@a = ();\n\t\t\n\t\twhile( $sum < $j ){\n\t\t\t@n or ++ $f and last;\n\t\t\t$sh = shift @n;\n\t\t\t$sum += $sh;\n\t\t\t$sum > $j and ++ $f and last;\n\t\t\tpush @a, $sh;\n\t\t\t}\n\t\t\n\t\t@a == 1 and next;\n\t\t\n\t\t$max = 0;\n\t\t$_ > $max and $max = $_ for @a;\n\t\t$l = 0;\n\t\t$r = 0;\n\t\t$ok = 0;\n\t\tfor $i (0 .. @a - 1){\n\t\t\t$k ++;\n\t\t\t$a[$i] == $max or next;\n\t\t\t$i + 1 < @a and $a[$i] > $a[$i+1] and $r = 1;\n\t\t\t$i > 0 and $a[$i] > $a[$i-1] and $l = 1;\n\t\t\tif( $l || $r ){\n\t\t\t\tif( $l ){\n\t\t\t\t\tpush @ans, map \"$_ L\", map 1 + $_ + $jj, reverse 0 .. $i - 1;\n\t\t\t\t\tpush @ans, map \"$_ R\", ($jj + 0) x (@a - $i - 1);\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tpush @ans, map \"$_ R\", ($jj + $i) x (@a - $i - 1);\n\t\t\t\t\tpush @ans, map \"$_ L\", map 1 + $_ + $jj, reverse 0 .. $i - 1;\n\t\t\t\t\t}\n\t\t\t\t$ok = 1;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t$ok or $f = 1;\n\t\t}\n\t\n\t$f += @n;\n\t\n\tprint $f ? 'NO' : 'YES';\n\t$f or print join \"\\n\", @ans;\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t$n = $_;\n\t@n = split ' ', <>;\n\tchomp( $m = <> );\n\t@m = split ' ', <>;\n\t\n\t$f = 0;\n\t$k = 0;\n\t$jj = 0;\n\t@ans = ();\n\t\n\tfor $j (@m){\n\t\t$jj ++;\n\t\t$f and last;\n\t\t\n\t\t$sum = 0;\n\t\t@a = ();\n\t\t\n\t\twhile( $sum < $j ){\n\t\t\t$sum > $j and ++ $f and last;\n\t\t\t@n or ++ $f and last;\n\t\t\t$sh = shift @n;\n\t\t\t$sum += $sh;\n\t\t\tpush @a, $sh;\n\t\t\t}\n\t\t\n\t\t@a == 1 and next;\n\t\t\n\t\t$max = 0;\n\t\t$_ > $max and $max = $_ for @a;\n\t\t$l = 0;\n\t\t$r = 0;\n\t\t$ok = 0;\n\t\tfor $i (0 .. @a - 1){\n\t\t\t$k ++;\n\t\t\t$a[$i] == $max or next;\n\t\t\t$i + 1 < @a and $a[$i] > $a[$i+1] and $r = 1;\n\t\t\t$i > 0 and $a[$i] > $a[$i-1] and $l = 1;\n\t\t\tif( $l || $r ){\n\t\t\t\tif( $l ){\n\t\t\t\t\tpush @ans, map \"$_ L\", map 1 + $_ + $jj, reverse 0 .. $i - 1;\n\t\t\t\t\tpush @ans, map \"$_ R\", ($jj + 0) x (@a - $i - 1);\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tpush @ans, map \"$_ R\", ($jj + $i) x (@a - $i - 1);\n\t\t\t\t\tpush @ans, map \"$_ L\", map 1 + $_ + $jj, reverse 0 .. $i - 1;\n\t\t\t\t\t}\n\t\t\t\t$ok = 1;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t$ok or $f = 1;\n\t\t}\n\t\n\t$f += @n;\n\t\n\tprint $f ? 'NO' : 'YES';\n\t$f or print join \"\\n\", @ans;\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t$n = $_;\n\t@n = split ' ', <>;\n\tchomp( $m = <> );\n\t@m = split ' ', <>;\n\t\n\t$f = 0;\n\t$k = 0;\n\t$jj = 0;\n\t@ans = ();\n\t\n\tfor $j (@m){\n\t\t$jj ++;\n\t\t$f and last;\n\t\t\n\t\t$sum = 0;\n\t\t@a = ();\n\t\t\n\t\twhile( $sum < $j ){\n\t\t\t$sum > $j and ++ $f and last;\n\t\t\t$sh = shift @n;\n\t\t\t$sum += $sh;\n\t\t\tpush @a, $sh;\n\t\t\t}\n\t\t\n\t\t$max = 0;\n\t\t$_ > $max and $max = $_ for @a;\n\t\t$l = 0;\n\t\t$r = 0;\n\t\t$ok = 0;\n\t\tfor $i (0 .. @a - 1){\n\t\t\t$k ++;\n\t\t\t$a[$i] == $max or next;\n\t\t\t$i + 1 < @a and $a[$i] > $a[$i+1] and $r = 1;\n\t\t\t$i > 0 and $a[$i] > $a[$i-1] and $l = 1;\n\t\t\tif( $l || $r ){\n\t\t\t\tif( $l ){\n\t\t\t\t\tpush @ans, map \"$_ L\", map 1 + $_ + $jj, reverse 0 .. $i - 1;\n\t\t\t\t\tpush @ans, map \"$_ R\", ($jj + 0) x (@a - $i - 1);\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tpush @ans, map \"$_ R\", ($jj + $i) x (@a - $i - 1);\n\t\t\t\t\tpush @ans, map \"$_ L\", map 1 + $_ + $jj, reverse 0 .. $i - 1;\n\t\t\t\t\t}\n\t\t\t\t$ok = 1;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t$ok or $f = 1;\n\t\t}\n\t\n\t$f += @n;\n\t\n\tprint $f ? 'NO' : 'YES';\n\t$f or print join \"\\n\", @ans;\n\t}"}], "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,\u20093,\u2009...,\u2009n\u2009-\u20091 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 \u2014 amount of tests (1\u2009\u2264\u2009t\u2009\u2264\u2009105). Each of the following t lines contains one integer number n (1\u2009\u2264\u2009n\u2009\u2264\u2009105).", "output_spec": "For each test output in a separate line one number \u2014 the maximum amount of people that could stay in the end.", "sample_inputs": ["1\n3"], "sample_outputs": ["1"], "notes": null}, "positive_code": [{"source_code": "<>; while(<>){$_ += $_ <= 1; print $_ - 2, \"\\n\";};"}], "negative_code": [{"source_code": "<>; while(<>){print $_ - 2};"}, {"source_code": "print <> - 2;"}, {"source_code": "<>; while(<>){print $_ - 2, \"\\n\";};"}], "src_uid": "f83c91c9e9252aba4736aa0bea82493b"} {"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\u00a0,3,...,\u00a0\u00a0\u00a010\" will be corrected to \"1,\u00a02,\u00a03,\u00a0...,\u00a010\".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": "#!perl -lp\ns/\\s*,/, /g;\ns/\\.\\.\\.\\s*/ .../g;\ns/\\s+/ /g;\ns/^ | $//g;\n"}, {"source_code": "while (<>) {\n s/ *, */, /g;\n s/ *\\.\\.\\. */ \\.\\.\\./g;\n s/ +/ /g;\n s/^\\s+//g;\n s/\\s+$//g;\n print\n}\n"}, {"source_code": "#!perl -lp\ns/\\s*,/, /g;\ns/\\.\\.\\.\\s*/ .../g;\ns/\\s+/ /g;\ns/^ | $//g;\n\n"}, {"source_code": "#!perl -lp\ns/\\s*,/, /g;\ns/\\.\\.\\.\\s*/ .../g;\ns/\\s+/ /g;\ns/^ | $//g;\n\n"}, {"source_code": "chomp($_ = );\ns/,/, /g;\ns/\\.\\.\\./ .../g;\ns/([\\d\\.])\\s+?,/$1,/g;\ns/\\s+/ /g;\ns/^\\s+?|\\s+$//g;\ns/\\.\\.\\.\\s+?(\\d)/...$1/g;\nprint;\n"}, {"source_code": "#!perl -lp\ns/\\s*,/, /g;\ns/\\.\\.\\.\\s*/ .../g;\ns/\\s+/ /g;\ns/^ | $//g;\n"}, {"source_code": "#!perl -lp\ns/\\s*,/, /g;\ns/\\.\\.\\.\\s*/ .../g;\ns/\\s+/ /g;\ns/^ | $//g;\n\n"}, {"source_code": "#!perl -lp\ns/\\s*,/, /g;\ns/\\.\\.\\.\\s*/ .../g;\ns/\\s+/ /g;\ns/^ | $//g;\n\n"}, {"source_code": "#!perl -lp\ns/\\s*,/, /g;\ns/\\.\\.\\.\\s*/ .../g;\ns/\\s+/ /g;\ns/^ | $//g;"}, {"source_code": "#!perl -lp\ns/\\s*,/, /g;\ns/\\.\\.\\.\\s*/ .../g;\ns/\\s+/ /g;\ns/^ | $//g;\n\n"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\n\nmy $s = <>;\n$s =~ s/\\.\\.\\./ \\.\\.\\./g;\n$s =~ s/[ ]*\\,/\\,/g;\n$s =~ s/\\,/\\, /g;\n$s =~ s/\\.\\.\\.[ ]+([^.])/\\.\\.\\.$1/g;\n$s =~ s/[ ]+/ /g;\n$s =~ s/^[ ]*//g;\n$s =~ s/[ ]*$//g;\nprint $s\n\n"}, {"source_code": "while(<>){\n\twhile(/,( {2,})?(\\d+)/){\n\t\ts/,( {2,})?(\\d+)/, $2/g}\n#\tprint;\n\ts/(\\d+) +,/$1,/g;\n\twhile(/,( {2,})?,/){\n\t\ts/,( {2,})?,/, ,/g}\n#\tprint;\n\ts/(\\d+)( {2,})?\\./$1 ./g;\n\ts/\\. +(\\d)/.$1/g;\n\ts/,( {2,})?\\./, ./g;\n\twhile(/\\.{4}/){s/\\.{4}/... ./};\n\twhile(/(\\d) {2,}(\\d)/){s/(\\d) {2,}(\\d)/$1 $2/};\n\ts/\\.{3} +,/...,/g;\n\ts/\\. {2,}\\./. ./g;\n\tprint\n\t}\n"}, {"source_code": "#!perl -lp\ns/\\s*,/, /g;\ns/\\.\\.\\.\\s*/ .../g;\ns/\\s+/ /g;\ns/^ | $//g;\n\n"}], "negative_code": [{"source_code": "#!perl -lp\ns/\\s+//g;\ns/,/, /g;\ns/\\.\\.\\./ .../g;\ns/\\s+/ /g\n"}, {"source_code": "#!perl -lp\ns/\\s*,/, /g;\ns/\\.\\.\\./ .../g;\ns/\\s+/ /g;\ns/^ | $//g;\n"}, {"source_code": "#!perl -lp\ns/,/, /g;\ns/\\.\\.\\./ .../g;\ns/\\s+/ /g\n"}, {"source_code": "#!perl -lp\n#s/\\s+//g;\ns/\\s*,/, /g;\ns/\\.\\.\\./ .../g;\ns/\\s+/ /g;\ns/^ | $//;\n"}, {"source_code": "#!perl -lp\ns/\\s+//g;\ns/,/, /g;\ns/\\.\\.\\./ .../g;\ns/\\s+/ /g;\ns/^ | $//;\n"}, {"source_code": "chomp($_ = );\ns/,/, /g;\ns/\\.\\.\\./ .../g;\ns/([\\d\\.])\\s,/$1,/g;\ns/\\s+/ /g;\ns/^\\s+|\\s+$//;\nprint;\n"}, {"source_code": "chomp($_ = );\ns/,/, /g;\ns/\\.\\.\\./ .../g;\ns/([\\d\\.])\\s,/$1,/g;\ns/\\s+/ /g;\ns/^\\s+?|\\s+$//g;\nprint;\n"}, {"source_code": "chomp($_ = );\ns/,/, /g;\ns/(\\S)?\\.\\.\\./$1 .../g;\ns/([\\d\\.])\\s,/$1,/g;\ns/\\s+/ /g;\nprint;\n"}, {"source_code": "chomp($_ = );\ns/,/, /g;\ns/\\.\\.\\./ .../g;\ns/([\\d\\.])\\s,/$1,/g;\ns/\\s+/ /g;\ns/^\\s+//;\nprint;\n"}, {"source_code": "chomp($_ = );\ns/,/, /g;\ns/\\.\\.\\./ .../g;\ns/([\\d\\.])\\s+?,/$1,/g;\ns/\\s+/ /g;\ns/^\\s+?|\\s+$//g;\nprint;\n"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\n\nmy $s = <>;\n$s =~ s/\\.\\.\\./ \\.\\.\\./g;\n$s =~ s/[ ]*\\,/\\,/g;\n$s =~ s/\\,/\\, /g;\n$s =~ s/\\.\\.\\.[ ]+[^.]/\\.\\.\\./g;\n$s =~ s/[ ]+/ /g;\n$s =~ s/^[ ]*//g;\n$s =~ s/[ ]*$//g;\nprint $s\n\n"}, {"source_code": "while(<>){\n\twhile(/,( {2,})?(\\d+)/){\n\t\ts/,( {2,})?(\\d+)/, $2/g}\n#\tprint;\n\ts/(\\d+) +,/$1,/g;\n\twhile(/,( {2,})?,/){\n\t\ts/,( {2,})?,/, ,/g}\n#\tprint;\n\ts/(\\d+)( {2,})?\\./$1 ./g;\n\ts/\\. +(\\d)/.$1/g;\n\ts/,( {2,})?\\./, ./g;\n\twhile(/\\.{4}/){s/\\.{4}/... ./};\n\twhile(/(\\d) {2,}(\\d)/){s/(\\d) {2,}(\\d)/$1 $2/};\n\ts/\\.{3} +,/...,/g;\n\tprint\n\t}\n"}, {"source_code": "while(<>){\n\twhile(/,( {2,})?(\\d+)/){\n\t\ts/,( {2,})?(\\d+)/, $2/g}\n#\tprint;\n\ts/(\\d+) +,/$1,/g;\n\twhile(/,( {2,})?,/){\n\t\ts/,( {2,})?,/, ,/g}\n#\tprint;\n\ts/(\\d+)( {2,})?\\./$1 ./g;\n\ts/\\. +(\\d)/.$1/g;\n\ts/,( {2,})?\\./, ./g;\n\twhile(/\\.{4}/){s/\\.{4}/... ./};\n\twhile(/(\\d) {2,}(\\d)/){s/(\\d) {2,}(\\d)/$1 $2/};\n\tprint\n\t}\n"}, {"source_code": "while(<>){\n\twhile(/,( {2,})?(\\d+)/){\n\t\ts/,( {2,})?(\\d+)/, $2/g}\n#\tprint;\n\ts/(\\d+) +,/$1,/g;\n\twhile(/,( {2,})?,/){\n\t\ts/,( {2,})?,/, ,/g}\n#\tprint;\n\ts/(\\d+)( {2,})?\\./$1 ./g;\n\ts/,( {2,})?\\./, ./g;\n\twhile(/\\.{4}/){s/\\.{4}/... ./};\n\twhile(/(\\d) {2,}(\\d)/){s/(\\d) {2,}(\\d)/$1 $2/};\n\tprint\n\t}\n"}, {"source_code": "while(<>){\n\twhile(/,( {2,})?(\\d+)/){\n\t\ts/,( {2,})?(\\d+)/, $2/g}\n#\tprint;\n\ts/(\\d+) +,/$1,/g;\n\twhile(/,( {2,})?,/){\n\t\ts/,( {2,})?,/, ,/g}\n#\tprint;\n\ts/(\\d+)\\./$1 ./g;\n\ts/,( {2,})?\\./, ./g;\n\twhile(/\\.{4}/){s/\\.{4}/... ./};\n\twhile(/(\\d) {2,}(\\d)/){s/(\\d) {2,}(\\d)/$1 $2/};\n\tprint\n\t}\n"}, {"source_code": "while(<>){\n\twhile(/,( {2,})?(\\d+)/){\n\t\ts/,( {2,})?(\\d+)/, $2/g}\n#\tprint;\n\ts/(\\d+) +,/$1,/g;\n\twhile(/,( {2,})?,/){\n\t\ts/,( {2,})?,/, ,/g}\n#\tprint;\n\ts/(\\d+)\\./$1 ./g;\n\ts/,\\./, ./g;\n\twhile(/\\.{4}/){s/\\.{4}/... ./};\n\twhile(/(\\d) {2,}(\\d)/){s/(\\d) {2,}(\\d)/$1 $2/};\n\tprint\n\t}\n"}, {"source_code": "while(<>){\n\twhile(/,( {2,})?(\\d+)/){\n\t\ts/,( {2,})?(\\d+)/, $2/g}\n#\tprint;\n\ts/(\\d+) +,/$1,/g;\n\twhile(/,( {2,})?,/){\n\t\ts/,( {2,})?,/, ,/g}\n#\tprint;\n\ts/(\\d+)\\./$1 ./g;\n\ts/,\\./, ./g;\n\twhile(/\\.{4}/){s/\\.{4}/... ./};\n\tprint\n\t}\n"}], "src_uid": "c7d8c71a1f7e6c7364cce5bddd488a2f"} {"nl": {"description": "At a break Vanya came to the class and saw an array of $$$n$$$ $$$k$$$-bit integers $$$a_1, a_2, \\ldots, a_n$$$ on the board. An integer $$$x$$$ is called a $$$k$$$-bit integer if $$$0 \\leq x \\leq 2^k - 1$$$. Of course, Vanya was not able to resist and started changing the numbers written on the board. To ensure that no one will note anything, Vanya allowed himself to make only one type of changes: choose an index of the array $$$i$$$ ($$$1 \\leq i \\leq n$$$) and replace the number $$$a_i$$$ with the number $$$\\overline{a_i}$$$. We define $$$\\overline{x}$$$ for a $$$k$$$-bit integer $$$x$$$ as the $$$k$$$-bit integer such that all its $$$k$$$ bits differ from the corresponding bits of $$$x$$$. Vanya does not like the number $$$0$$$. Therefore, he likes such segments $$$[l, r]$$$ ($$$1 \\leq l \\leq r \\leq n$$$) such that $$$a_l \\oplus a_{l+1} \\oplus \\ldots \\oplus a_r \\neq 0$$$, where $$$\\oplus$$$ denotes the bitwise XOR operation. Determine the maximum number of segments he likes Vanya can get applying zero or more operations described above.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\leq n \\leq 200\\,000$$$, $$$1 \\leq k \\leq 30$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\leq a_i \\leq 2^k - 1$$$), separated by spaces\u00a0\u2014 the array of $$$k$$$-bit integers.", "output_spec": "Print one integer\u00a0\u2014 the maximum possible number of segments with XOR not equal to $$$0$$$ that can be obtained by making several (possibly $$$0$$$) operations described in the statement.", "sample_inputs": ["3 2\n1 3 0", "6 3\n1 4 4 7 3 4"], "sample_outputs": ["5", "19"], "notes": "NoteIn the first example if Vasya does not perform any operations, he gets an array that has $$$5$$$ segments that Vanya likes. If he performs the operation with $$$i = 2$$$, he gets an array $$$[1, 0, 0]$$$, because $$$\\overline{3} = 0$$$ when $$$k = 2$$$. This array has $$$3$$$ segments that Vanya likes. Also, to get an array with $$$5$$$ segments that Vanya likes, he can perform two operations with $$$i = 3$$$ and with $$$i = 2$$$. He then gets an array $$$[1, 0, 3]$$$. It can be proven that he can't obtain $$$6$$$ or more segments that he likes.In the second example, to get $$$19$$$ segments that Vanya likes, he can perform $$$4$$$ operations with $$$i = 3$$$, $$$i = 4$$$, $$$i = 5$$$, $$$i = 6$$$ and get an array $$$[1, 4, 3, 0, 4, 3]$$$."}, "positive_code": [{"source_code": "#!/bin/perl\n($n,$k)=split' ',<>;\n@a=split' ',<>;\n@a[$_]=@a[$_]^0+@a[$_-1] for(1..$#a);\npush @a,0;\nfor(@a){\n\t$_=$_^~(-1<<$k) if 0==($_&1<<($k-1))\n}\n@a=sort @a;\n$le=0; #lastend\n$ans=0;\nfor(1..+@a){\n\tif (@a[$_]!=@a[$_-1]) {\n\t\t$sz=$_-$le;\n\t\t$s1=int $sz/2;\n\t\t$s2=$sz-$s1;\n\t\t$ans+=($s1*($s1-1)+$s2*($s2-1))/2;\n\t\t$le=$_;\n\t}\n}\nprint $n*($n+1)/2-$ans,\"\\n\";"}], "negative_code": [{"source_code": "#!/bin/perl\n($n,$k)=split' ',<>;\n@a=split' ',<>;\n@a[$_]=@a[$_]^0+@a[$_-1] for(1..$#a);\nfor(@a){\n\t$_=$_^~(-1<<$k) if 0==($_&1<<($k-1))\n}\npush @a,0;\n@a=sort @a;\n$le=0; #lastend\n$ans=0;\nfor(1..+@a){\n\tif (@a[$_]!=@a[$_-1]) {\n\t\t$sz=$_-$le;\n\t\t$s1=int $sz/2;\n\t\t$s2=$sz-$s1;\n\t\t$ans+=($s1*($s1-1)+$s2*($s2-1))/2;\n\t\t$le=$_;\n\t}\n}\nprint $n*($n+1)/2-$ans,\"\\n\";"}, {"source_code": "#!/bin/perl\n($n,$k)=split' ',<>;\n@a=split' ',<>;\n@a[$_]=@a[$_]^0+@a[$_-1] for(1..$#a);\nfor(@a){\n\t$_=$_^~(-1<<$k) if $_&1<<($k-1)\n}\npush @a,0;\n@a=sort @a;\n$le=0; #lastend\n$ans=0;\nfor(1..+@a){\n\tif (@a[$_]!=@a[$_-1]) {\n\t\t$sz=$_-$le;\n\t\t$s1=int $sz/2;\n\t\t$s2=$sz-$s1;\n\t\t$ans+=($s1*($s1-1)+$s2*($s2-1))/2;\n\t\t$le=$_;\n\t}\n}\nprint $n*($n+1)/2-$ans,\"\\n\";\n"}], "src_uid": "71ad4b2e9e888933e55425e73a0d68b5"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the colors of the ribbons the cats wear. ", "output_spec": "Print a single integer $$$x$$$\u00a0\u2014 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": " \nuse 5.20.1;\nuse strict;\nmy $tmp,my $n = ,my $res = 1,my @a,my @b,my @c= split(/ /,),my $it = 1;\nforeach my $i(@c) {\n $b[++$a[$i]]++;\n $res = $it+1 if($b[$a[$i]] * $a[$i] == $it && $it < $n);\n $res = $it if($b[$a[$i]] * $a[$i] == $it-1);\n $it++;\n}\nsay $res;"}], "negative_code": [], "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\u2009\u2264\u2009|A|\u2009\u2264\u2009103) 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": "use strict;\nuse warnings;\nuse 5.010;\n \nmy $str = <>;\nchop $str;\nmy $rev = reverse $str;\n$str .= $rev;\nsay $str;\n"}, {"source_code": "$a = ;\nchomp $a;\nprint($a);\nprint(scalar reverse $a);"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\nuse 5.010;\n \nmy $str = <>;\nmy $rev = reverse $str;\n$str .= $rev;\nsay $str;\n#"}, {"source_code": "use strict;\nuse warnings;\nuse 5.010;\n \nmy $str = <>;\nmy $rev = reverse $str;\n$str .= $rev;\nsay $str;\n"}], "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\u2009\u2264\u2009n\u2009\u2264\u200910100000). 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 \u2014 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\u2009\u2192\u200919\u2009\u2192\u200910\u2009\u2192\u20091. After three transformations the number becomes one-digit."}, "positive_code": [{"source_code": "#!/usr/bin/perl -w\n\nchomp ($num = <>);\n@arr = unpack('(A1)*', $num);\n$count = 0;\nwhile (@arr > 1) {\n\t$count++;\n\t$num = 0;\n\tfor $i (0..@arr-1) {\n\t\t$num += $arr[$i];\n\t}\n\t@arr = unpack('(A1)*', $num);\n}\nprint $count;\n\n"}], "negative_code": [], "src_uid": "ddc9201725e30297a5fc83f4eed75fc9"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$\u00a0\u2014 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": "use strict;\nuse warnings;\n\nmy $input = ;\nmy $n = 0+$input;\n$input = ;\nmy @arr = map {$_+0} split / /, $input;\nmy $max_mex = 0;\nmy $answer = -1;\nmy $e;\nfor my $i (0.. scalar @arr - 1) {\n $e = $arr[$i];\n if ($e > $max_mex) {\n $answer = $i+1;\n last;\n } else {\n $max_mex = $e + 1 if $max_mex == $e;\n }\n}\nprint($answer);"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $max = -1;\n\tmy $ans = -1;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tif( $_[ $i ] > $max + 1 ){\n\t\t\t$ans = $i + 1;\n\t\t\tlast;\n\t\t\t}\n\t\t$max < $_[ $i ] and $max = $_[ $i ];\n\t\t}\n\t\n\tprint $ans;\n\t}"}], "negative_code": [], "src_uid": "e17427897fd5147c601204cb1c48b143"} {"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)$$$\u00a0\u2014 it costs $$$x$$$ burles; move down to the cell $$$(x + 1, y)$$$\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$m,$k) = map { $_ - 0 } split(/\\s+/,);\r\n if( ( $n - 1 ) + $n * ( $m - 1 ) == $k ){\r\n print \"YES\\n\";\r\n } else {\r\n print \"NO\\n\";\r\n }\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "src_uid": "8b0a9c7e997034d3ecce044b9f64aeba"} {"nl": {"description": "Permutation p is an ordered set of integers p1,\u2009\u2009p2,\u2009\u2009...,\u2009\u2009pn, 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,\u2009\u2009p2,\u2009\u2009...,\u2009\u2009pn.You have a sequence of integers a1,\u2009a2,\u2009...,\u2009an. 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\u2009\u2264\u2009n\u2009\u2264\u20093\u00b7105) \u2014 the size of the sought permutation. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009109\u2009\u2264\u2009ai\u2009\u2264\u2009109).", "output_spec": "Print a single number \u2014 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,\u20091).In the second sample you need 6 moves to build permutation (1,\u20093,\u20092)."}, "positive_code": [{"source_code": "while (<>){\n @m=();\n $i=0;\n $s=0;\n $_=<>;\n chomp;\n @a = split/ /,$_;\n @a = sort {$a <=> $b} @a;\n for (0..@a-1){\n $s += (abs $a[$_]- ++$i);\n \n }\n # print \"@a\\n\"; \n print \"$s\\n\";\n }"}], "negative_code": [], "src_uid": "86d5da999415fa75b3ee754a2a28605c"} {"nl": {"description": "Harry Potter and He-Who-Must-Not-Be-Named engaged in a fight to the death once again. This time they are located at opposite ends of the corridor of length l. Two opponents simultaneously charge a deadly spell in the enemy. We know that the impulse of Harry's magic spell flies at a speed of p meters per second, and the impulse of You-Know-Who's magic spell flies at a speed of q meters per second.The impulses are moving through the corridor toward each other, and at the time of the collision they turn round and fly back to those who cast them without changing their original speeds. Then, as soon as the impulse gets back to it's caster, the wizard reflects it and sends again towards the enemy, without changing the original speed of the impulse.Since Harry has perfectly mastered the basics of magic, he knows that after the second collision both impulses will disappear, and a powerful explosion will occur exactly in the place of their collision. However, the young wizard isn't good at math, so he asks you to calculate the distance from his position to the place of the second meeting of the spell impulses, provided that the opponents do not change positions during the whole fight.", "input_spec": "The first line of the input contains a single integer l (1\u2009\u2264\u2009l\u2009\u2264\u20091\u2009000)\u00a0\u2014 the length of the corridor where the fight takes place. The second line contains integer p, the third line contains integer q (1\u2009\u2264\u2009p,\u2009q\u2009\u2264\u2009500)\u00a0\u2014 the speeds of magical impulses for Harry Potter and He-Who-Must-Not-Be-Named, respectively.", "output_spec": "Print a single real number\u00a0\u2014 the distance from the end of the corridor, where Harry is located, to the place of the second meeting of the spell impulses. Your answer will be considered correct if its absolute or relative error will not exceed 10\u2009-\u20094. Namely: let's assume that your answer equals a, and the answer of the jury is b. The checker program will consider your answer correct if .", "sample_inputs": ["100\n50\n50", "199\n60\n40"], "sample_outputs": ["50", "119.4"], "notes": "NoteIn the first sample the speeds of the impulses are equal, so both of their meetings occur exactly in the middle of the corridor."}, "positive_code": [{"source_code": "@_ = <>;\nprint $_[0] * $_[1] / ($_[1] + $_[2])"}, {"source_code": "#!/usr/bin/perl\n\n$l = <>;\n$p = <>;\n$q = <>;\n\nprint $p*$l/($p + $q), \"\\n\";"}, {"source_code": "@_ = <>;\nprint $_[0] * $_[1] / ($_[1] + $_[2])"}], "negative_code": [], "src_uid": "6421a81f85a53a0c8c63fbc32750f77f"} {"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 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009100, 1\u2009\u2264\u2009k\u2009\u2264\u20091000). Each of the following n lines contains the coordinates of the polyline's endpoints. The i-th one contains coordinates of the point Ai \u2014 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 \u2014 the total time Scrooges wastes on signing the papers in seconds. The absolute or relative error should not exceed 10\u2009-\u20096.", "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": "#!/usr/bin/perl\n\nuse Math::Complex;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$ats=1*$'/50;\n\n($x, $y)=split/ /,<>;\n\nfor (2..$`){\n <>=~/ /;\n $sum += sqrt (($x-$`)**2 + ($y-$')**2);\n $x=$`; $y=$';\n }\nprint $sum*$ats;\n\n#-----------SUBs-------------#\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;"}], "negative_code": [], "src_uid": "db4a25159067abd9e3dd22bc4b773385"} {"nl": {"description": "You are given two integers $$$l$$$ and $$$r$$$, where $$$l < r$$$. We will add $$$1$$$ to $$$l$$$ until the result is equal to $$$r$$$. Thus, there will be exactly $$$r-l$$$ additions performed. For each such addition, let's look at the number of digits that will be changed after it.For example: if $$$l=909$$$, then adding one will result in $$$910$$$ and $$$2$$$ digits will be changed; if you add one to $$$l=9$$$, the result will be $$$10$$$ and $$$2$$$ digits will also be changed; if you add one to $$$l=489999$$$, the result will be $$$490000$$$ and $$$5$$$ digits will be changed. Changed digits always form a suffix of the result written in the decimal system.Output the total number of changed digits, if you want to get $$$r$$$ from $$$l$$$, adding $$$1$$$ each time.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Then $$$t$$$ test cases follow. Each test case is characterized by two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l < r \\le 10^9$$$).", "output_spec": "For each test case, calculate the total number of changed digits if you want to get $$$r$$$ from $$$l$$$, adding one each time.", "sample_inputs": ["4\n1 9\n9 10\n10 20\n1 1000000000"], "sample_outputs": ["8\n2\n11\n1111111110"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($l,$r) = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my $r = &f($r) - &f($l);\r\n print \"$r\\n\";\r\n \r\n}\r\n\r\nexit(0);\r\n\r\nsub f {\r\n my $x = shift;\r\n my $r = 0;\r\n while( $x > 0 ){\r\n $r += $x;\r\n $x /= 10;\r\n }\r\n return $r;\r\n}\r\n\r\n"}], "negative_code": [], "src_uid": "7a51d536d5212023cc226ef1f6201174"} {"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$$$) \u2014 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$$$) \u2014 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\n( $n, $d ) = split ' ', <>;\n\nfor( 1 .. <> ){\n\t( $x, $y ) = split ' ', <>;\n\tprint $x + $y < $d || $x + $y > $n + $n - $d || abs( $y - $x ) > $d ? \"NO\" : \"YES\"\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $d ) = split;\n\t\n\tfor( 1 .. <> ){\n\t\tmy( $x, $y ) = sort { $a <=> $b } split ' ', <>;\n\t\tprint \"NO\" and next if $x + $y < $d;\n\t\tprint \"NO\" and next if $x + $y > $n + $n - $d;\n\t\tprint \"NO\" and next if $y - $x > $d;\n\t\tprint \"NO\" and next if $y - $x < -$d;\n\t\t\n\t\tprint \"YES\";\n\t\t}\n\t\n\t\n\t}"}], "negative_code": [], "src_uid": "9c84eb518c273942650c7262e5d8b45f"} {"nl": {"description": "You are given $$$n$$$ rectangles, each of height $$$1$$$. Each rectangle's width is a power of $$$2$$$ (i.\u2009e. it can be represented as $$$2^x$$$ for some non-negative integer $$$x$$$). You are also given a two-dimensional box of width $$$W$$$. Note that $$$W$$$ may or may not be a power of $$$2$$$. Moreover, $$$W$$$ is at least as large as the width of the largest rectangle.You have to find the smallest height of this box, such that it is able to fit all the given rectangles. It is allowed to have some empty space left in this box after fitting all the rectangles.You cannot rotate the given rectangles to make them fit into the box. Moreover, any two distinct rectangles must not overlap, i.\u2009e., any two distinct rectangles must have zero intersection area.See notes for visual explanation of sample input.", "input_spec": "The first line of input contains one integer $$$t$$$ ($$$1 \\le t \\le 5 \\cdot 10^3$$$) \u2014 the number of test cases. Each test case consists of two lines. For each test case: the first line contains two integers $$$n$$$ ($$$1 \\le n \\le 10^5$$$) and $$$W$$$ ($$$1 \\le W \\le 10^9$$$); the second line contains $$$n$$$ integers $$$w_1, w_2, \\dots, w_n$$$ ($$$1 \\le w_i \\le 10^6$$$), where $$$w_i$$$ is the width of the $$$i$$$-th rectangle. Each $$$w_i$$$ is a power of $$$2$$$; additionally, $$$\\max\\limits_{i=1}^{n} w_i \\le W$$$. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "Output $$$t$$$ integers. The $$$i$$$-th integer should be equal to the answer to the $$$i$$$-th test case \u2014 the smallest height of the box.", "sample_inputs": ["2\n5 16\n1 2 8 4 8\n6 10\n2 8 8 2 2 8"], "sample_outputs": ["2\n3"], "notes": "NoteFor the first test case in the sample input, the following figure shows one way to fit the given five rectangles into the 2D box with minimum height: In the figure above, the number inside each rectangle is its width. The width of the 2D box is $$$16$$$ (indicated with arrow below). The minimum height required for the 2D box in this case is $$$2$$$ (indicated on the left).In the second test case, you can have a minimum height of three by keeping two blocks (one each of widths eight and two) on each of the three levels."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\n# TLE 5;\n# greedy, fill\n\nuse warnings;\nuse strict;\n \n$\\ = $/;\n \nmy @widths = map 2 ** $_, 0 .. 19;\n \n<>;\n\nwhile(<>){\n\tmy( $n, $w ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\tmy $height = 0;\n\t\n\twhile( keys %h ){\n\t\t$height ++;\n\t\tmy $ww = $w;\n\t\t\n\t\tfor my $i ( sort { $b <=> $a } keys %h ){\n\t#%\tfor my $i ( reverse @widths ){\n\t\t\tnext if not exists $h{ $i };\n\t\t\tif( $ww - $i >= 0 ){\n\t\t\t\t$ww -= $i;\n\t\t\t\t$h{ $i } --;\n\t\t\t\t$h{ $i } == 0 and delete $h{ $i };\n\t\t\t\tredo;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint $height;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nmy @widths = map [ 2 ** $_, 0 ], 0 .. 19;\n\nmy %dict = map { 2 ** $_, $_ } 0 .. 19;\n\nmy @ans;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $w ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy @W = @widths;\n\t\n\tmap { $W[ $dict{ $_ } ][ 1 ] ++ } @_;\n\t\n\t@W = reverse @W;\n\t\n\tmy $height = 0;\n\t\n\t@W = grep { $_->[ 1 ] > 0 } @W;\n\t\n\twhile( @W ){\n\t\t$height ++;\n\t\tmy $ww = $w;\n\t\t\n\t\t$debug and print join \", \", map \"$_->[ 0 ] $_->[ 1 ]\", @W;\n\t\t\n\t\tfor my $i ( @W ){\n\t\t\tif( $ww - $i->[ 0 ] >= 0 ){\n\t\t\t\t$ww -= $i->[ 0 ];\n\t\t\t\t$i->[ 1 ] --;\n\t\t\t\t$i->[ 1 ] == 0 and do {\n\t\t\t\t\tnext;\n\t\t\t\t\t};\n\t\t\t\tredo;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t@W = grep { $_->[ 1 ] > 0 } @W;\n\t\t}\n\t\n\tpush @ans, $height;\n\t}\n\nprint join \"\\n\", @ans;"}], "negative_code": [], "src_uid": "49ba9921ae8b6bc53726e7a521eefe39"} {"nl": {"description": "The Queen of England has n trees growing in a row in her garden. At that, the i-th (1\u2009\u2264\u2009i\u2009\u2264\u2009n) 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\u2009\u2264\u2009i\u2009<\u2009n), ai\u2009+\u20091\u2009-\u2009ai\u2009=\u2009k, 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\u2009\u2264\u2009n,\u2009k\u2009\u2264\u20091000). The second line contains n space-separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20091000) \u2014 the heights of the trees in the row. ", "output_spec": "In the first line print a single integer p \u2014 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\u2009\u2264\u2009j\u2009\u2264\u2009n) tree from the left by x (x\u2009\u2265\u20091) meters, then print in the corresponding line \"+\u00a0j\u00a0x\". If the gardener needs to decrease the height of the j-th (1\u2009\u2264\u2009j\u2009\u2264\u2009n) tree from the left by x (x\u2009\u2265\u20091) meters, print on the corresponding line \"-\u00a0j\u00a0x\". 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": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy ($n, $k) = split /\\s/, <>;\nmy @w = split /\\s/, <>;\n\nmy $min = 1001;\nmy $min_i;\nfor my $i (0..$n-1){\n\tmy $err = 0;\n\tmy $lmin = 0;\n\tfor my $j (0..$n-1){\n\t\tif ( $w[$i] + ($j-$i)*$k <= 0 ) {\n\t\t\t$err = 1;\n\t\t\tlast;\n\t\t}\n\t\t\n\t\tif($w[$i] + ($j-$i)*$k != $w[$j]){\n\t\t\t$lmin++;\n\t\t}\n\t}\n\tif(!$err && $lmin < $min){\n\t\t$min = $lmin;\n\t\t$min_i = $i;\n\t}\n}\n\nprint \"$min\\n\";\nfor my $j (0..$n-1){\n\tmy $delta = $w[$min_i]+($j-$min_i)*$k - $w[$j];\n\tif($delta != 0){\n\t\tmy $sign = ($delta > 0) ? '+' : '-';\n\t\t$delta = abs($delta);\n\t\tmy $pos = $j+1;\n\t\tprint \"$sign $pos $delta\\n\";\n\t}\n}"}, {"source_code": "\nwhile(<>){\n\t\n\t($n, $k)=split/ /;\n\tchomp $k;\n\t\n\tchomp ($_=<>);\n\t@_=split/ /;\n\t\n\tfor $i(1..1000){\n\t\t\n\t\t@e=($i);\n\t\tfor $j(2..$n){\n\t\t\tpush @e, $e[@e-1]+$k\n\t\t\t}\n\t\t\t\n\t\t$h=0;\n\t\tfor $j(0..@_-1){\n\t\t\tif ($e[$j]!=$_[$j]){\n\t\t\t\t$h++\n\t\t\t}\n\t\t}\n\t\t$i[$i]=$h;\n\t\t}\n\t\t\n\t\t$min=1000;\n\t\t$ii=1;\n\t\tfor $j(1..1000){\n\t\t\t$min > $i[$j] and ($min=$i[$j], $ii= $j);\n\t\t\t}\n\t\n\t\t@e=($ii);\n\t\tfor $j(2..$n){\n\t\t\tpush @e, $e[@e-1]+$k\n\t\t\t}\n\t\t\n\t\t@m=();\n\t\tfor $j(0..@_-1){\n\t\t\tif ($e[$j]!=$_[$j]){\n\t\t\t\tpush @m, (($j+1).\" \".($e[$j]-$_[$j]))\n\t\t\t}\n\t\t}\n\t\n\tfor (@m){\n\t\tif (s/-//){ s/^/- / }\n\t\telse {s/^/+ /}\n\t\t}\n\t\n\tprint 0+@m;\n\tprint \"\\n\";\n\tfor (@m){\n\t\tprint \"$_\\n\"\n\t\t}\n\t\n\t}\n\t\n"}, {"source_code": "\nwhile(<>){\n\t\n\t($n, $k)=split/ /;\n\t\n\t@_=split/ /,<>;\n\t\n\tfor $i(1..1e3){\n\t\t\n\t\t@e=($i);\n\t\tpush @e, $e[@e-1]+$k for 2..$n;\n\t\t\t\n\t\tfor $j(0..@_-1){\n\t\t\tif ($e[$j]!=$_[$j]){\n\t\t\t\t$i[$i]++\n\t\t\t}\n\t\t}\n\t\t}\n\t\t\n\t\t$min=1e3;\n\t\tfor $j(1..1e3){\n\t\t\t$min > $i[$j] and ($min=$i[$j], $I=$j);\n\t\t\t}\n\t\n\t\t@e=($I);\n\t\tpush @e, $e[@e-1]+$k for 2..$n;\n\t\t\n\t\t@m=();\n\t\tfor $j(0..@_-1){\n\t\t\tif ($e[$j]!=$_[$j]){\n\t\t\t\tpush @m, (($j+1).\" \".($e[$j]-$_[$j]))\n\t\t\t}\n\t\t}\n\t\n\ts/-// ? s/^/- / : s/^/+ / for @m;\n\t\n\tprint 0+@m;\n\tprint \"\\n$_\" for @m\n\t\n\t}\n\t\n"}, {"source_code": "\nwhile(<>){\n\t\n\t($n, $k)=split/ /;\n\tchomp $k;\n\t\n#\tprint \" $n $k\\n\";\n\t\n\tchomp ($_=<>);\n\t@_=split/ /;\n\t\n\t@i=();\n\tfor $i(1..1000){\n\t\t\n\t\t@e=($i);\n\t\tfor $j(2..$n){\n\t\t\tpush @e, $e[@e-1]+$k\n\t\t\t}\n\t\t\t\n\t\t$h=0;\n\t\tfor $j(0..@_-1){\n\t\t\tif ($e[$j]!=$_[$j]){\n\t\t\t\t$h++; \n\t#\t\t\tpush @m, (($i+1).\" \".($e[$i]-$_[$i]))\n\t\t\t}\n\t\t}\n\t\t$i[$i]=$h;\n\t\t}\n\t\t\n\t\t$min=1000;\n\t\t$ii=1;\n\t\tfor $j(1..1000){\n\t\t\t$min > $i[$j] and ($min=$i[$j], $ii= $j);\n\t\t\t}\n\t\n\t\t@e=($ii);\n\t\tfor $j(2..$n){\n\t\t\tpush @e, $e[@e-1]+$k\n\t\t\t}\n\t\t\n\t\t@m=();\n\t\tfor $j(0..@_-1){\n\t\t\tif ($e[$j]!=$_[$j]){\n\t\t\t\tpush @m, (($j+1).\" \".($e[$j]-$_[$j]))\n\t\t\t}\n\t\t}\n\t\n#\tprint \"@e\\n\";\n#\tprint \"@_\\n\";\n\n#\tprint \"@m\\n\";\n\t\n\tfor (@m){\n\t\tif (s/-//){ s/^/- / }\n\t\telse {s/^/+ /}\n\t\t}\n#\tprint \"@m\\n\";\n\t\n\tprint 0+@m;\n\tprint \"\\n\";\n\tfor (@m){\n\t\tprint \"$_\\n\"\n\t\t}\n\t\n#\tprint \"\\n\";\n\t}\n\t\n"}, {"source_code": "\n($n, $k)=split/ /,<>;\n\t\n\t@_=split/ /,<>;\n\t\n\tfor $i(1..1e3){\n\t\t\n\t\t@e=($i);\n\t\tpush @e, $e[@e-1]+$k for 2..$n;\n\t\t\t\n\t\tfor $j(0..@_-1){\n\t\t\tif ($e[$j]!=$_[$j]){\n\t\t\t\t$i[$i]++\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t$min=1e3;\n\t\tfor $j(1..1e3){\n\t\t\t$min > $i[$j] and $min=$i[$I=$j];\n\t\t\t}\n\t\n\t\t@e=($I);\n\t\tpush @e, $e[@e-1]+$k for 2..$n;\n\t\t\n\t\tfor $j(0..@_-1){\n\t\t\tif ($e[$j]!=$_[$j]){\n\t\t\t\tpush @m, ($j+1).\" \".($e[$j]-$_[$j])\n\t\t\t}\n\t\t}\n\t\n\ts/-// ? s/^/- / : s/^/+ / for @m;\n\t\n\tprint 0+@m;\n\tprint \"\\n$_\" for @m\n\t"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy ($n, $k) = split /\\s/, <>;\nmy @w = split /\\s/, <>;\n\nmy $min = 1001;\nmy $min_i;\nfor my $i (0..$n-1){\n\tmy $lmin = 0;\n\tfor my $j (0..$n-1){\n\t\tif($w[$j]-$w[$i] != ($j-$i)*$k){\n\t\t\t$lmin++;\n\t\t}\n\t}\n\tif($lmin < $min){\n\t\t$min = $lmin;\n\t\t$min_i = $i;\n\t}\n}\n\nprint \"$min\\n\";\nfor my $j (0..$n-1){\n\tmy $delta = $w[$min_i]+($j-$min_i)*$k - $w[$j];\n\tif($delta != 0){\n\t\tmy $sign = ($delta > 0) ? '+' : '-';\n\t\t$delta = abs($delta);\n\t\tmy $pos = $j+1;\n\t\tprint \"$sign $pos $delta\\n\";\n\t}\n}"}, {"source_code": "sub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t\n\t($n, $k)=split/ /;\n\tchomp $k;\n\t\n\t@_=split/ /,<>;\n\tchomp @_;\n\t\n\t@c=@_;\n\t\n\t$f=0;\n\t$cnt=0;\n\t$cntX=$_[0];\n\t$cntx=0;\n\t\n\tfor $i(0..@_-2){\n\t\t$a=$_[$i+1]-$_[$i];\n\t\tif ($a==$k and ($_[$i]-$k*$i >= 0)){\n\t\t\t$f++;\n\t\t\t}\n\t\telse {\n\t\t\tif ($f>=$cnt){\n\t\t\t\t$cnt=$f;\n\t\t\t\t$cntX=$_[$i];\n\t\t\t\t$cntx=$i;\n\t\t\t\t}\n\t\t\t$f=0;\n\t\t\t\n\t\t\t}\n\t\t\n\t\t\n\t\t}\n\tif ($f>=$cnt){\n\t\t\t\t$cnt=$f;\n\t\t\t\t$cntX=$_[@_-1];\n\t\t\t\t$cntx=@_-1;\n\t\t\t\t}\n\t\t\t\t\n\tif ($cnt){\n\t\t\t\t\n#\tprint \"$cnt $cntx $cntX\\n\";\n\t\n\t@e=();\n\tfor $i(0..$cntx){\n\t\tunshift @e, $cntX-$k*$i\n\t\t\n\t\t}\n#\tprint \"@e\\n\";\n\t\n\tfor $i(1..@_-1-$cntx){\n\t\tpush @e, $cntX+$k*$i\n\t\t\n\t\t}\n#\tprint \"@e\\n\";\n#\tprint \"@_\\n\";\n\t}\n\telse {\n\t\t\n\t\t@e=();\n\t\tfor $i(0..@_-1){\n\t\t\tpush @e, $_[0]+$k*$i\n\t\t\t\n\t\t\t}\n\t\t\n\t\t}\n\t\n\t@m=();\n\tfor $i(0..@_){\n\t\tif ($e[$i]!=$_[$i]){\n\t\t\tpush @m, (($i+1).\" \".($e[$i]-$_[$i]))\n\t\t\t\n\t\t\t\n\t\t\t}\n\t\t\n\t\t}\n#\tprint \"@m\\n\";\n\t\n\tfor (@m){\n\t\tif (s/-//){ s/^/- / }\n\t\telse {s/^/+ /}\n\t\t\n\t\t}\n#\tprint \"@m\\n\";\n\t\n\tprint 0+@m;\n\tprint \"\\n\";\n\tfor (@m){\n\t\tprint \"$_\\n\"\n\t\t}\n\t\n#\tprint \"\\n\";\n\t}\n\t\n"}, {"source_code": "sub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t\n\t($n, $k)=split/ /;\n\tchomp $k;\n\t\n\t@_=split/ /,<>;\n\tchomp @_;\n\t\n\t@c=@_;\n\t\n\t$f=0;\n\t$cnt=0;\n\t$cntX=$_[0];\n\t\n\tfor $i(0..@_-2){\n\t\t$a=$_[$i+1]-$_[$i];\n\t\tif ($a==$k and ($_[$i]-$k*$i > 0)){\n\t\t\t$f or\t$cntX=$_[$i]-$k*$i;\n\t\t\t$f++;\n\t\t\t}\n\t\telse {\n\t\t\tif ($f>$cnt){\n\t\t\t\t$cnt=$f\n\t\t\t\t}\n\t\t\t$f=0;\n\t\t\t}\n\t\t}\n\tif ($f>$cnt and ($_[@_-1]-$k*(@_-1) > 0)){\n\t\t\t\t$cnt=$f;\n\t\t\t\t$cntX=$_[@_-1]-$k*(@_-1);\n\t\t\t\t}\n\t\t\t\t\n\n\t\t@e=();\n\t\tfor $i(0..@_-1){\n\t\t\tpush @e, $cntX+$k*$i\n\t\t\t}\n\t\t\n\t\n#\tprint \"@e\\n\";\n#\tprint \"@_\\n\";\n\t\n\t@m=();\n\tfor $i(0..@_){\n\t\tif ($e[$i]!=$_[$i]){\n\t\t\tpush @m, (($i+1).\" \".($e[$i]-$_[$i]))\n\t\t\t}\t\t\n\t\t}\n#\tprint \"@m\\n\";\n\t\n\tfor (@m){\n\t\tif (s/-//){ s/^/- / }\n\t\telse {s/^/+ /}\n\t\t}\n#\tprint \"@m\\n\";\n\t\n\tprint 0+@m;\n\tprint \"\\n\";\n\tfor (@m){\n\t\tprint \"$_\\n\"\n\t\t}\n\t\n#\tprint \"\\n\";\n\t}\n\t\n"}, {"source_code": "sub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t\n\t($n, $k)=split/ /;\n\tchomp $k;\n\t\n\t@_=split/ /,<>;\n\tchomp @_;\n\t\n\t@c=@_;\n\t\n\t$f=0;\n\t$cnt=0;\n\tfor $i(0..@_-2){\n\t\t$a=$_[$i+1]-$_[$i];\n\t\tif ($a==$k){\n\t\t\t$f++;\n\t\t\t}\n\t\telse {\n\t\t\tif ($f>=$cnt){\n\t\t\t\t$cnt=$f;\n\t\t\t\t$cntX=$_[$i];\n\t\t\t\t$cntx=$i;\n\t\t\t\t}\n\t\t\t$f=0;\n\t\t\t\n\t\t\t}\n\t\t\n\t\t\n\t\t}\n\tif ($f>=$cnt){\n\t\t\t\t$cnt=$f;\n\t\t\t\t$cntX=$_[@_-1];\n\t\t\t\t$cntx=@_-1;\n\t\t\t\t}\n\t\t\t\t\n#\tprint \"$cnt $cntx $cntX\\n\";\n\t\n\t@e=();\n\tfor $i(0..$cntx){\n\t\tunshift @e, $cntX-$k*$i\n\t\t\n\t\t}\n#\tprint \"@e\\n\";\n\t\n\tfor $i(1..@_-1-$cntx){\n\t\tpush @e, $cntX+$k*$i\n\t\t\n\t\t}\n#\tprint \"@e\\n\";\n#\tprint \"@_\\n\";\n\t\n\t@m=();\n\tfor $i(0..@_){\n\t\tif ($e[$i]!=$_[$i]){\n\t\t\tpush @m, (($i+1).\" \".($e[$i]-$_[$i]))\n\t\t\t\n\t\t\t\n\t\t\t}\n\t\t\n\t\t}\n#\tprint \"@m\\n\";\n\t\n\tfor (@m){\n\t\tif (s/-//){ s/^/- / }\n\t\telse {s/^/+ /}\n\t\t\n\t\t}\n#\tprint \"@m\\n\";\n\t\n\tprint 0+@m;\n\tprint \"\\n\";\n\tfor (@m){\n\t\tprint \"$_\\n\"\n\t\t}\n\t\n#\tprint \"\\n\";\n\t}\n\t\n"}, {"source_code": "sub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n\nwhile(<>){\n\t\n\t($n, $k)=split/ /;\n\tchomp $k;\n\t\n\t@_=split/ /,<>;\n\tchomp @_;\n\t\n\t@c=@_;\n\t\n\t$f=0;\n\t$cnt=0;\n\t$cntX=$_[0];\n\t$cntx=0;\n\t\n\tfor $i(0..@_-2){\n\t\t$a=$_[$i+1]-$_[$i];\n\t\tif ($a==$k and ($_[$i]-$k*$i > 0)){\n\t\t\t$f++;\n\t\t\t}\n\t\telse {\n\t\t\tif ($f>=$cnt){\n\t\t\t\t$cnt=$f;\n\t\t\t\t$cntX=$_[$i];\n\t\t\t\t$cntx=$i;\n\t\t\t\t}\n\t\t\t$f=0;\n\t\t\t\n\t\t\t}\n\t\t\n\t\t\n\t\t}\n\tif ($f>=$cnt){\n\t\t\t\t$cnt=$f;\n\t\t\t\t$cntX=$_[@_-1];\n\t\t\t\t$cntx=@_-1;\n\t\t\t\t}\n\t\t\t\t\n\tif ($cnt){\n\t\t\t\t\n#\tprint \"$cnt $cntx $cntX\\n\";\n\t\n\t@e=();\n\tfor $i(0..$cntx){\n\t\tunshift @e, $cntX-$k*$i\n\t\t\n\t\t}\n#\tprint \"@e\\n\";\n\t\n\tfor $i(1..@_-1-$cntx){\n\t\tpush @e, $cntX+$k*$i\n\t\t\n\t\t}\n#\tprint \"@e\\n\";\n#\tprint \"@_\\n\";\n\t}\n\telse {\n\t\t\n\t\t@e=();\n\t\tfor $i(0..@_-1){\n\t\t\tpush @e, $_[0]+$k*$i\n\t\t\t\n\t\t\t}\n\t\t\n\t\t}\n\t\n\t@m=();\n\tfor $i(0..@_){\n\t\tif ($e[$i]!=$_[$i]){\n\t\t\tpush @m, (($i+1).\" \".($e[$i]-$_[$i]))\n\t\t\t\n\t\t\t\n\t\t\t}\n\t\t\n\t\t}\n#\tprint \"@m\\n\";\n\t\n\tfor (@m){\n\t\tif (s/-//){ s/^/- / }\n\t\telse {s/^/+ /}\n\t\t\n\t\t}\n#\tprint \"@m\\n\";\n\t\n\tprint 0+@m;\n\tprint \"\\n\";\n\tfor (@m){\n\t\tprint \"$_\\n\"\n\t\t}\n\t\n#\tprint \"\\n\";\n\t}\n\t\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\u00a0\u2014 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$$$)\u00a0\u2014 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": "use strict;\nuse warnings;\n\nmy $input = ;\nmy ($n, $m) = map {$_+0} split / /, $input;\n$input = ;\nmy @a1 = map {$_+0} split / /, $input;\n$input = ;\nmy @a2 = map {$_+0} split / /, $input;\nmy %hash = ($m => 1);\nmy %hash2 = ();\nmy @a = ($m);\nmy $oo;\nwhile(@a) {\n $m = $a[0];\n my $ok = ff($m, \\%hash, $n, \\@a1, \\@a2, 1);\n if ($ok == 1) {\n print \"YES\";\n $oo=1;\n last;\n }\n $hash2{$m} = 1;\n @a = grep {not defined $hash2{$_}} keys %hash;\n}\nprint \"NO\" unless $oo;\n\nsub ff {\n my ($station, $h, $n, $a1, $a2, $m) = @_;\n my $ok = 0;\n if ($station < $n && $a2->[$station-1]) {\n for (($station + 1) .. $n) {\n if ($a2->[$_-1]) {\n return 1 if $_ == $m;\n $ok = -1 unless defined $h->{$_};\n $h->{$_} = 1;\n }\n }\n }\n if ($station > 1) {\n for (1..$station - 1) {\n if ($a1->[$_-1] && $a1->[$station-1]) {\n return 1 if $_ == $m;\n $ok = -1 unless defined $h->{$_};\n $h->{$_} = 1;\n }\n }\n }\n}\n"}, {"source_code": "( $n, $s ) = split ' ', <>;\n\n$s -= 2;\n\n$_ = reverse <> =~ s/.\\s/ $& * 3 /ger;\n$_ = ( <> =~ s/.\\s/ $& + chop /ger );\n\ns/([14][013]*)4/ \"$&\" =~ y!1!2!r /ge;\n\nprint /^[34].{$s}[234]/ ? \"YES\" : \"NO\""}], "negative_code": [{"source_code": "( $n, $s ) = split ' ', <>;\n\n$s -= 2;\n\n$_ = reverse <> =~ s/.\\s/ $& * 3 /ger;\n$_ = ( <> =~ s/.\\s/ $& + chop /ger );\n\ns/(1[01]*)4/ \"$&\" =~ y!1!2!r /ge;\n\nprint /^[34].{$s}[24]/ ? \"YES\" : \"NO\""}, {"source_code": "( $n, $s ) = split ' ', <>;\n\n$s -= 2;\n\n$_ = reverse <> =~ s/.\\s/ $& * 2 /ger;\n$_ = ( <> =~ s/.\\s/ $& + chop /ger );\n\ns/(1[01]*)3/ \"$&\" =~ y!1!2!r /ge;\n\nprint /^[23].{$s}[23]/ ? \"YES\" : \"NO\""}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $s ) = split;\n\t\n\tmy @A = split ' ', <> =~ s/1/2/rg;\n\tmy @B = split ' ', <>;\n\t\n\tif( $A[ 0 ] == 0 ){\n\t\tprint \"NO\";\n\t\tnext;\n\t\t}\n\t\n\t$_ += shift @B for @A;\n\t\n\t$_ = reverse join '', @A;\n\t\n\ts/3\\K(1+)/ 2 x length $1 /ge;\n\t\n\t$_ = reverse;\n\t\n\tprint 1 < ( substr $_, $s - 1, 1 ) ? \"YES\" : \"NO\";\n\t}"}], "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$$$ \u2014 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$$$) \u2014 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}$$$) \u2014 rearranged (reordered) sequence that Polycarp can wrote down on the board.", "output_spec": "Print $$$n$$$ integer numbers \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse bigint;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map $_ + 0, split ' ', <>;\n\t\n\tprint join ' ', map $_->[0],\n\tsort {\n\t\t$b->[2] <=> $a->[2] ||\n\t\t$a->[1] <=> $b->[1]\n\t\t}\n#\tmap { print \"[@{$_}]\" }\n\tmap {\n\t\tmy $_2 = 0;\n\t\tmy $cp = $_;\n\t\twhile( $cp % 2 == 0 ){\n\t\t\t$cp /= 2;\n\t\t\t$_2 ++;\n\t\t\t}\n\t\tmy $_3 = 0;\n\t\t$cp = $_;\n\t\twhile( $cp % 3 == 0 ){\n\t\t\t$cp /= 3;\n\t\t\t$_3 ++;\n\t\t\t}\n\t\t[ $_, $_2, $_3 ];\n\t\t} @_;\n\t}"}], "negative_code": [], "src_uid": "f9375003a3b64bab17176a05764c20e8"} {"nl": {"description": "The Bitlandians are quite weird people. They do everything differently. They have a different alphabet so they have a different definition for a string.A Bitlandish string is a string made only of characters \"0\" and \"1\".BitHaval (the mayor of Bitland) loves to play with Bitlandish strings. He takes some Bitlandish string a, and applies several (possibly zero) operations to it. In one operation the mayor may take any two adjacent characters of a string, define one of them as x and the other one as y. Then he calculates two values p and q: p\u2009=\u2009x\u00a0xor\u00a0y, q\u2009=\u2009x\u00a0or\u00a0y. Then he replaces one of the two taken characters by p and the other one by q.The xor operation means the bitwise excluding OR operation. The or operation is the bitwise OR operation.So for example one operation can transform string 11 to string 10 or to string 01. String 1 cannot be transformed into any other string.You've got two Bitlandish strings a and b. Your task is to check if it is possible for BitHaval to transform string a to string b in several (possibly zero) described operations.", "input_spec": "The first line contains Bitlandish string a, the second line contains Bitlandish string b. The strings can have different lengths. It is guaranteed that the given strings only consist of characters \"0\" and \"1\". The strings are not empty, their length doesn't exceed 106.", "output_spec": "Print \"YES\" if a can be transformed into b, otherwise print \"NO\". Please do not print the quotes.", "sample_inputs": ["11\n10", "1\n01", "000\n101"], "sample_outputs": ["YES", "NO", "NO"], "notes": null}, "positive_code": [{"source_code": "($_,$a)=<>;print/1/==$a=~/1/&&length==length$a?YES:NO"}, {"source_code": "($a,$b)=<>;print length$a==length$b&&$a=~/1/==$b=~/1/?YES:NO"}, {"source_code": "($_,$a)=<>;print/1/==$a=~/1/&&length==length$a?YES:NO\n"}, {"source_code": "($_,$a)=<>;print/1/==$a=~/1/&&length==length$a?YES:NO\n"}, {"source_code": "\n\n($_,$a)=<>;print/1/==$a=~/1/&&length==length$a?YES:NO"}, {"source_code": "($_,$a)=<>;print/1/==$a=~/1/&&length==length$a?YES:NO\n"}, {"source_code": "($_,$a)=<>;print/1/==$a=~/1/&&length==length$a?YES:NO\n"}, {"source_code": "chomp ($str1 = );\nchomp ($str2 = );\nif (length($str1) == length($str2)) {\n\tprintf \"YES\\n\" if ($str1 =~ /1/ && $str2 =~ /1/);\n\tprintf \"YES\\n\" if ($str1 !~ /1/ && $str2 !~ /1/);\n\tprintf \"NO\\n\" if ($str1 !~ /1/ && $str2 =~ /1/);\n\tprintf \"NO\\n\" if ($str1 =~ /1/ && $str2 !~ /1/);\n} else {\n\tprintf \"NO\\n\";\n}\n"}, {"source_code": "($_,$a)=<>;print/1/==$a=~/1/&&length==length$a?YES:NO\n"}, {"source_code": "$a = <>;\nchomp $a;\n\n$b = <>;\nchomp $b;\n\nif (length($a) != length($b)) { print \"NO\\n\"; exit(); }\n\n$fa = ($a =~ /1/ ? 1 : 0);\n$fb = ($b =~ /1/ ? 1 : 0);\n\nif ($fa == $fb) { print \"YES\\n\"; } else { print \"NO\\n\"; }\n\n"}, {"source_code": "($_,$a)=<>;print/1/==$a=~/1/&&length==length$a?YES:NO\n"}, {"source_code": "($_,$a)=<>;print/1/==$a=~/1/&&length==length$a?YES:NO\n"}, {"source_code": "($_,$a)=<>;print/1/==$a=~/1/&&length==length$a?YES:NO\n"}], "negative_code": [], "src_uid": "113ae625e67c8ea5ab07be44c3b58a8f"} {"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": "($.,$$)=split\" \",<>;@$=split\" \",<>;@$=sort{$a<=>$b}@$;$.=0;for(@$){$/=$_-$.;last if(!$$);next if($/<=0);;print\"$/\\n\";$.+=$_-$.;$$--;};while($$){$$--;print\"0\\n\"}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\t@_ = sort { $a <=> $b } keys %h;\n\t\n\tmy $prev = 0;\n\tmy @ans;\n\t\n\tfor my $i ( 1 .. $k ){\n\t\tmy $A = shift @_ // $prev;\n\t\tpush @ans, $A - $prev;\n\t\t$prev = $A;\n\t\t}\n\t\n\tprint join \"\\n\", @ans;\n\t}"}], "negative_code": [], "src_uid": "0f100199a720b0fdead5f03e1882f2f3"} {"nl": {"description": "Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis!The computers bought for the room were different. Some of them had only USB ports, some\u00a0\u2014 only PS/2 ports, and some had both options.You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once.You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy.", "input_spec": "The first line contains three integers a, b and c (0\u2009\u2264\u2009a,\u2009b,\u2009c\u2009\u2264\u2009105) \u00a0\u2014 the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0\u2009\u2264\u2009m\u2009\u2264\u20093\u00b7105) \u00a0\u2014 the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer vali (1\u2009\u2264\u2009vali\u2009\u2264\u2009109) \u00a0\u2014 the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in.", "output_spec": "Output two integers separated by space\u00a0\u2014 the number of equipped computers and the total cost of the mouses you will buy.", "sample_inputs": ["2 1 1\n4\n5 USB\n6 PS/2\n3 PS/2\n7 PS/2"], "sample_outputs": ["3 14"], "notes": "NoteIn the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports."}, "positive_code": [{"source_code": "use v5.20;\nuse warnings;\n\nsub compare {\n\treturn $a <=> $b;\n}\n\nmy ($a,$b,$c) = split(' ',<>);\nmy $m = int <>;\n\nmy (@A,@B);\n\nfor (1..$m) {\n\tmy ($val,$type) = split(' ',<>);\n\tif ($type eq \"USB\") {\n\t\tpush @A,$val;\n }\n\telse { \n\t\tpush @B,$val;\n\t}\n}\n\n@A = sort compare @A;\n@B = sort compare @B;\n\nmy $cost = 0;\nmy $comp = 0;\n\nwhile ($a and scalar @A) {\n\t$comp += 1;\n\t$cost += shift @A;\n\t$a -= 1;\n}\n\nwhile ($b and scalar @B) {\n\t$comp += 1;\n\t$cost += shift @B;\n\t$b -= 1;\n}\n\nmy @C = sort compare (@A,@B);\n\nwhile ($c and scalar @C) {\n\t$comp += 1;\n\t$cost += shift @C;\n\t$c -= 1;\n}\n\nsay \"$comp $cost\";\n\n"}, {"source_code": "use v5.20;\n\nmy $input = <>;\nmy ($usb_comp, $ps2_comp, $both_comp) = split(/ /, $input);\n\nmy $m = <>;\nmy @usb, my @ps2;\nfor (my $i = 0; $i < $m; $i = $i + 1) {\n\t$input = <>;\n\tchomp $input;\n\tmy ($number, $type) = split(/ /, $input);\n\n\tif ($type eq \"USB\") {\n\t\tpush(@usb, $number);\n\t} else {\n\t\tpush(@ps2, $number);\n\t}\n}\n\nmy $price = 0;\nmy $matched = 0;\n# Match as many only-usb computers to usb mouses.\n@usb = sort {$a <=> $b} @usb;\nmy $i = 0;\nwhile ($i < scalar @usb and $usb_comp > 0) {\n\t$price = $price + $usb[$i];\n\t$i++;\n\t$matched++;\n\t$usb_comp--;\n}\n\n# Match as many only-ps2 computers to usb mouses.\n@ps2 = sort {$a <=> $b} @ps2;\nmy $j = 0;\nwhile ($j < scalar @ps2 and $ps2_comp > 0) {\n\t$price = $price + $ps2[$j];\n\t$j++;\n\t$matched++;\n\t$ps2_comp--;\n}\n\n# Match the computers having both ports to cheapest mouses.\nwhile (($j < scalar @ps2 or $i < scalar @usb) and $both_comp > 0) {\n\tif ($i >= scalar @usb or ($j < scalar @ps2 and $ps2[$j] < $usb[$i])) {\n\t\t$price = $price + $ps2[$j];\n\t\t$j++;\n\t} else {\n\t\t$price = $price + $usb[$i];\n\t\t$i++;\n\t}\n\t$matched++;\n\t$both_comp--;\n}\n\nprint \"$matched $price\"\n"}, {"source_code": "#use v5.24;\nuse warnings;\nmy @usb,@ps2,@merged,$nrcump=0,$costtot=0;\n\nmy $line=;\nchomp($line);\nmy ($usbonly,$ps2only,$both)=split(/ /,$line);\n$line=;\nchomp($line);\nmy ($n)=split(/ /,$line);\nfor (my $i=1;$i<=$n;$i++){\n\t$line=;\n\tchomp($line);\n\tmy ($cost,$tip)=split(/ /,$line);\n\tif ($tip eq \"USB\") {push @usb, $cost;}\n\telse {push @ps2, $cost;}\n}\n@usb=sort {$a<=>$b} @usb;\n@ps2=sort {$a<=>$b} @ps2;\nwhile ($usbonly>0 && scalar @usb>0){\n\t$usbonly--;\n\t$nrcump++;\n\t$costtot=$costtot+shift @usb;\n}\nwhile ($ps2only>0 && scalar @ps2>0){\n\t$ps2only--;\n\t$nrcump++;\n\t$costtot=$costtot+shift @ps2;\n}\nforeach my $var (@usb) {push @merged, $var;}\nforeach my $var (@ps2) {push @merged, $var;}\n@merged=sort {$a<=>$b} @merged;\nwhile ($both>0 && scalar @merged>0){\n\t$both--;\n\t$nrcump++;\n\t$costtot=$costtot+shift @merged;\n}\nprint $nrcump.' '.$costtot;\n;\nexit(0);"}, {"source_code": "use v5.20.1;\nuse warnings;\n\n=pod\nmy $filename = \"date.in\";\nopen(my $fh, '<:encoding(UTF-8)', $filename)\n\tor die \"Could not open the file\";\n=cut\n\nmy $line = ;\nchomp($line);\n\nmy($a, $b, $c) = split(' ', $line);\n\n$line = ;\nchomp($line);\n\nmy $m = $line;\n\nmy @usb;\nmy @ps;\nmy $model;\nmy $price = 0;\n\nfor(my $i = 0; $i < $m; $i++){\n\n\t$line = ;\n\t($price, $model) = split(' ', $line);\n\tif($model eq \"USB\"){\n\n\t\tpush @usb, $price;\n\n\t}elsif($model eq \"PS/2\"){\n\n\t\tpush @ps, $price;\n\n\t}\n\n}\n\nmy @usbSorted = sort {$::a <=> $::b} @usb;\nmy @psSorted = sort {$::a <=> $::b} @ps;\n\n@usb = @usbSorted;\n@ps = @psSorted;\n\nmy $ui = 0;\nmy $pi = 0;\nmy $cost = 0;\nmy $mice = 0;\n\nwhile($ui < @usb && $a > 0){\n\n\t$cost += $usb[$ui];\n\t#say $usb[$ui], \"\\n\";\n\t$ui++;\n\t$mice++;\n\t$a--;\n\n}\n\nwhile($pi < @ps && $b > 0){\n\n\t$cost += $ps[$pi];\n\t#say $ps[$pi], \"\\n\";\n\t$pi++;\n\t$b--;\n\t$mice++;\n\n}\n\nwhile($ui < @usb && $pi < @ps && $c > 0){\n\n\tif($usb[$ui] < $ps[$pi]){\n\n\t\t$cost += $usb[$ui];\n\t\t#say $usb[$ui], \"\\n\";\n\t\t$c--;\n\t\t$ui++;\n\t\t$mice++;\n\n\t}else{\n\n\t\t$cost += $ps[$pi];\n\t\t#say $ps[$pi], \"\\n\";\n\t\t$pi++;\n\t\t$c--;\n\t\t$mice++;\n\n\t}\n\n}\n\nwhile($ui < @usb && $c > 0){\n\n\t$cost += $usb[$ui];\n\t#say $usb[$ui], \"\\n\";\n\t$ui++;\n\t$c--;\n\t$mice++;\n\n}\n\nwhile($pi < @ps && $c > 0){\n\n\t$cost += $ps[$pi];\n\t#say $ps[$pi], \"\\n\";\n\t$pi++;\n\t$c--;\n\t$mice++;\n\n}\n\nsay $mice, \" \", $cost, \"\\n\";\n"}, {"source_code": "use warnings;\n\nmy $line = <>;\nchomp($line);\nmy($muie, $cuie, $c) = split(' ', $line);\n\nmy $i;\nmy @v1;\nmy @v2;\nmy $m = <>;\nchomp($m);\n\nfor($i=1;$i<=$m;++$i) {\n\tmy $line = <>;\n\tchomp($line);\n\tmy($x, $y) = split(' ', $line);\n\t\n\tif($y eq \"USB\") {\n\t\tpush(@v1, $x);\n\t}\n\telse {\n\t\tpush(@v2, $x);\n\t}\n}\n\n@v1 = sort {$a <=> $b} @v1;\n@v2 = sort {$a <=> $b} @v2;\n\n$i=0;\nmy $nr=0;\nwhile($i < scalar @v1 && $i < $muie) {\n\t$nr++;\n\t\n\t$total+=$v1[$i];\n\t$i++;\n}\n\nmy $j=0;\nwhile($j < scalar @v2 && $j < $cuie) {\n\t$nr++;\n\t\n\t$total+=$v2[$j];\n\t$j++;\n}\n\n#for($i=0;$i 0) {\n#\tprintf(\"%d %d %d %d\\n\", $i, $j, scalar @v1, scalar @v2);\n\t\n\tif($j >= scalar @v2 || ($v1[$i] <= $v2[$j] && $i < scalar @v1)) {\n\t\t$total+=$v1[$i];\n\t\t++$i;\n\t}\n\telse {\n\t\t$total+=$v2[$j];\n\t\t++$j;\n\t}\n\t\n\t++$nr;\n\t--$c;\n}\n\n\nprintf(\"%d %d\\n\", $nr, $total);\n"}, {"source_code": "sub input { $_ = <>; chomp; wantarray? split: $_ }\n\nsub less_first { sort { $a <=> $b } @_ }\n\nsub buy {\n\tmy ($c, $aref) = @_;\n\twhile ($c--) {\n\t\tlast unless @$aref;\n\t\t$R++, $V += shift @$aref;\n\t}\n}\n\n$R = $V = 0; @c = input; $m = input; \nfor (1..$m) {\n\t($v, $t) = input;\n\tif ($t eq \"USB\") {\n\t\tpush @v0, $v;\n\t} else {\n\t\tpush @v1, $v;\n\t}\n}\n\n@v0 = less_first @v0; \n@v1 = less_first @v1; \n\nbuy $c[0], \\@v0;\nbuy $c[1], \\@v1;\n@v2 = less_first @v0, @v1;\nbuy $c[2], \\@v2;\n\nprint $R, \" \", $V;\n"}, {"source_code": "use warnings;\nuse v5.20;\n\nmy @array;\nmy $total = 0;\nmy $count = 0;\n\nmy $input = ;\nchomp $input;\nmy($A, $B, $C) = split(' ', $input);\n\n$input = ;\nchomp $input;\nmy $m = int($input);\n\nfor(my $i = 0; $i < $m; $i += 1){\n $input = ;\n chomp $input;\n my($value, $name) = split(' ', $input);\n $value = int($value);\n push @array, {id => $value, type => $name};\n}\n\n\nfor my $item ( sort { $a->{id} <=> $b->{id} } @array ) {\n if($item->{type} eq \"USB\"){\n if($A > 0){\n $total = $total + $item->{id};\n $count += 1;\n $A -= 1;\n }\n elsif($C > 0){\n $total = $total + $item->{id};\n $count += 1;\n $C -= 1;\n }\n }\n else{\n if($B > 0){\n $total = $total + $item->{id};\n $count += 1;\n $B -= 1;\n }\n elsif($C > 0){\n $total = $total + $item->{id};\n $count += 1;\n $C -= 1;\n }\n }\n}\nprint(\"$count $total\\n\");"}, {"source_code": "use strict;\nuse warnings;\n\nmy ($usb_nr, $ps_nr, $both_nr) = split(\" \", <>);\nmy $nr_prices = <>;\nmy @prices_usb = ();\nmy @prices_ps = ();\nmy $current_price = 0;\nmy $current_type = \"\";\nmy $equipped_usb = 0;\nmy $equipped_ps = 0;\nmy $equipped_both = 0;\nmy $cost = 0;\nmy $bought = 0;\nforeach(1 .. $nr_prices)\n{\n\t($current_price, $current_type) = split(\" \", <>);\n\tif($current_type eq \"USB\")\n\t{\n\t\tpush(@prices_usb, $current_price);\n\t}\n\telse\n\t{\n\t\tpush(@prices_ps, $current_price);\n\t}\n}\n@prices_usb = sort {$a <=> $b} @prices_usb;\n@prices_ps = sort {$a <=> $b} @prices_ps;\n#print \"Prices USB: @prices_usb \\n\";\n#print \"Prices PS/2: @prices_ps \\n\";\n$bought = 0;\nforeach(0 .. scalar $usb_nr-1)\n{\n\tif($_ < scalar @prices_usb)\n\t{\n\t\t#print \"Equipped USB PC with $prices_usb[$_]\\n\";\n\t\t$equipped_usb += 1;\n\t\t$cost += $prices_usb[$_];\n\t\t$bought += 1;\n\t}\n}\nsplice @prices_usb, 0, $bought;\n$bought = 0;\nforeach(0 .. $ps_nr-1)\n{\n\tif($_ < scalar @prices_ps)\n\t{\n\t\t#print \"Equipped PS/2 PC with $prices_ps[$_]\\n\";\n\t\t$equipped_ps += 1;\n\t\t$cost += $prices_ps[$_];\n\t\t$bought += 1;\n\t}\n}\nsplice @prices_ps, 0, $bought;\nmy $pos_usb = 0;\nmy $pos_ps = 0;\nwhile($pos_usb < scalar @prices_usb and $pos_ps < scalar @prices_ps and $equipped_both < $both_nr)\n{\n\tif($prices_usb[$pos_usb] <= $prices_ps[$pos_ps])\n\t{\n\t\t#print \"Equipped PC with both with USB: $prices_usb[$pos_usb]\\n\";\n\t\t$equipped_both += 1;\n\t\t$cost += $prices_usb[$pos_usb];\n\t\t$pos_usb += 1;\n\t}\n\telse\n\t{\n\t\t#print \"Equipped PC with both with PS/2: $prices_ps[$pos_ps]\\n\";\n\t\t$equipped_both += 1;\n\t\t$cost += $prices_ps[$pos_ps];\n\t\t$pos_ps += 1;\n\t}\n}\nwhile($pos_usb < scalar @prices_usb and $equipped_both < $both_nr)\n{\n\t#print \"Equipped PC with both with USB: $prices_usb[$pos_usb]\\n\";\n\t$equipped_both += 1;\n\t$cost += $prices_usb[$pos_usb];\n\t$pos_usb += 1;\n}\nwhile($pos_ps < scalar @prices_ps and $equipped_both < $both_nr)\n{\n\t#print \"Equipped PC with both with PS/2: $prices_ps[$pos_ps]\\n\";\n\t$equipped_both += 1;\n\t$cost += $prices_ps[$pos_ps];\n\t$pos_ps += 1;\n}\nprint $equipped_usb + $equipped_ps + $equipped_both, \" \", $cost;\n"}, {"source_code": "use warnings;\nuse utf8;\nuse feature \"say\";\n\nmy $line = <>;\nchomp($line);\nmy ($usb, $ps2, $mix) = split(' ', $line);\nmy $pcNr = <>;\nchomp($pcNr);\nmy (@usb, @ps2);\nwhile ($pcNr > 0) {\n $line = <>;\n chomp($line);\n my($price, $type) = split(' ', $line);\n if ($type eq \"USB\") {\n push ( @usb, $price );\n } else {\n push ( @ps2, $price);\n }\n --$pcNr;\n}\n@usb = sort ( { $a <=> $b } @usb );\n@ps2 = sort ( { $a <=> $b } @ps2 );\n\nmy $price = 0;\nmy ( $iU, $iP );\n\nfor ( $iU = 0; $usb > 0 && $iU < scalar(@usb); ++$iU, --$usb) {\n $price += $usb[$iU];\n ++$pcNr;\n}\n\nfor ( $iP = 0; $ps2 > 0 && $iP < scalar(@ps2); ++$iP, --$ps2) {\n $price += $ps2[$iP];\n ++$pcNr;\n}\n\nfor (; ($iU < scalar(@usb) || $iP < scalar( @ps2 )) && $mix > 0; ) {\n if ( $iU < scalar(@usb) && $iP < scalar(@ps2) && $usb[$iU] <= $ps2[$iP] || $iP >= scalar(@ps2)) {\n $price += $usb[$iU];\n ++$pcNr;\n --$mix;\n ++$iU;\n } elsif ($iP < scalar(@ps2) && $iU < scalar(@usb) && $usb[$iU] > $ps2[$iP] || $iU >= scalar(@usb)) {\n $price += $ps2[$iP];\n ++$pcNr;\n --$mix;\n ++$iP;\n }\n}\n\nsay $pcNr.\" \".$price;\n"}, {"source_code": " use warnings;\n use utf8;\n use feature \"say\";\n \n my $line = <>;\n chomp($line);\n my ($usb, $ps2, $mix) = split(' ', $line);\n my $pcNr = <>;\n chomp($pcNr);\n my (@usb, @ps2);\n while ($pcNr > 0) {\n $line = <>;\n chomp($line);\n my($price, $type) = split(' ', $line);\n if ($type eq \"USB\") {\n push ( @usb, $price );\n } else {\n push ( @ps2, $price);\n }\n --$pcNr;\n }\n @usb = sort ( { $a <=> $b } @usb );\n @ps2 = sort ( { $a <=> $b } @ps2 );\n \n my $price = 0;\n \n for ( ; $usb > 0 && scalar(@usb); --$usb) {\n $price += shift @usb;\n ++$pcNr;\n }\n \n for ( ; $ps2 > 0 && scalar(@ps2); --$ps2) {\n $price += shift @ps2;\n ++$pcNr;\n }\n \n my @c = sort( { $a <=> $b } @usb, @ps2 );\n\t\n\tfor ( ;scalar( @c ) && $mix > 0; --$mix) {\n\t\t$price += shift @c;\n\t\t++$pcNr;\n\t}\n\t \n say $pcNr.\" \".$price;"}, {"source_code": "use v5.14;\nuse warnings;\n \nuse Data::Dumper qw(Dumper);\n\nmy $input = <>;\nchomp $input;\nmy ($c, $d, $e) = split / /, $input;\nmy $cost = 0;\nmy $nr = 0;\nmy $m = <>;\nmy @list;\nfor(my $i = 1; $i <= $m; $i++)\n{\n\t$input = <>;\n\tchomp $input;\n\tmy($val, $s) = split / /, $input;\n push @list, [$val,$s];\n}\n@list = sort{$a->[0]<=>$b->[0]} @list;\nfor(my $i = 0; $i < $m; $i++)\n{\n if($list[$i]->[1] eq \"USB\")\n {\n if($c > 0)\n {\n $c--;\n $nr++;\n $cost+=$list[$i]->[0];\n }\n else\n {\n if($e > 0)\n {\n $e--;\n $nr++;\n $cost+=$list[$i]->[0];\n }\n }\n }\n else\n {\n if($d > 0)\n {\n $d--;\n $nr++;\n $cost+=$list[$i]->[0];\n }\n else\n {\n if($e > 0)\n {\n $e--;\n $nr++;\n $cost+=$list[$i]->[0];\n }\n }\n }\n}\nsay \"$nr $cost\";\n \n"}], "negative_code": [{"source_code": "use v5.20;\nuse warnings;\n\nsub compare {\n\treturn $a <=> $b;\n}\n\nmy ($a,$b,$c) = split(' ',<>);\nmy $m = int <>;\n\nmy (@A,@B);\n\nfor (1..$m) {\n\tmy ($val,$type) = split(' ',<>);\n\tif ($type eq \"USB\") {\n\t\tpush @A,$val;\n }\n\telse { \n\t\tpush @B,$val;\n\t}\n}\n\n@A = sort compare @A;\n@B = sort compare @B;\n\nmy $cost = 0;\nmy $comp = 0;\n\nwhile ($a and @A) {\n\t$comp += 1;\n\t$cost += shift @A;\n\t$a -= 1;\n}\n\nwhile ($b and @B) {\n\t$comp += 1;\n\t$cost += shift @B;\n\t$b -= 1;\n}\n\nmy @C = sort compare (@A,@B);\n\nwhile ($c and @C) {\n\t$comp += 1;\n\t$cost += shift @B;\n\t$c -= 1;\n}\n\nsay \"$comp $cost\";\n\n"}, {"source_code": "use v5.20;\n\nmy $input = <>;\nmy ($usb_comp, $ps2_comp, $both_comp) = split(/ /, $input);\n\nmy $m = <>;\nmy @usb, my @ps2;\nfor (my $i = 0; $i < $m; $i = $i + 1) {\n\t$input = <>;\n\tchomp $input;\n\tmy ($number, $type) = split(/ /, $input);\n\n\tif ($type eq \"USB\") {\n\t\tpush(@usb, $number);\n\t} else {\n\t\tsay $type;\n\t\tpush(@ps2, $number);\n\t}\n}\n\nmy $price = 0;\nmy $matched = 0;\n# Match as many only-usb computers to usb mouses.\n@usb = sort {$a <=> $b} @usb;\nmy $i = 0;\nwhile ($i < scalar @usb and $usb_comp > 0) {\n\tsay 1;\n\t$price = $price + $usb[$i];\n\t$i++;\n\t$matched++;\n\t$usb_comp--;\n}\n\n# Match as many only-ps2 computers to usb mouses.\n@ps2 = sort {$a <=> $b} @ps2;\nmy $j = 0;\nwhile ($j < scalar @ps2 and $ps2_comp > 0) {\n\tsay 2;\n\t$price = $price + $ps2[$j];\n\t$j++;\n\t$matched++;\n\t$ps2_comp--;\n}\n\n# Match the computers having both ports to cheapest mouses.\nwhile (($j < scalar @ps2 or $i < scalar @usb) and $both_comp > 0) {\n\tsay 3;\n\tif ($i >= scalar @usb or ($j < scalar @ps2 and $ps2[$j] < $usb[$i])) {\n\t\t$price = $price + $ps2[$j];\n\t\t$j++;\n\t} else {\n\t\t$price = $price + $usb[$i];\n\t\t$i++;\n\t}\n\t$matched++;\n\t$both_comp--;\n}\n\nprint \"$matched $price\"\n"}, {"source_code": "#use v5.24;\nuse warnings;\nmy @usb,@ps2,@merged,$nrcump=0,$costtot=0;\n\nmy $line=;\nchomp($line);\nmy ($usbonly,$ps2only,$both)=split(/ /,$line);\n$line=;\nchomp($line);\nmy ($n)=split(/ /,$line);\nfor (my $i=1;$i<=$n;$i++){\n\t$line=;\n\tchomp($line);\n\tmy ($cost,$tip)=split(/ /,$line);\n\tif ($tip eq \"USB\") {push @usb, $cost;}\n\telse {push @ps2, $cost;}\n}\n@usb=sort {$a<=>$b} @usb;\n@ps2=sort {$a<=>$b} @ps2;\nwhile ($usbonly>0 && scalar @usb>0){\n\t$usbonly--;\n\t$nrcump++;\n\t$costtot=$costtot+shift @usb;\n}\nwhile ($ps2only>0 && scalar @ps2>0){\n\t$ps2only--;\n\t$nrcump++;\n\t$costtot=$costtot+shift @ps2;\n}\nforeach my $var (@usb) {push @merged, $var;}\nforeach my $var (@ps2) {push @merged, $var;}\nwhile ($both>0 && scalar @merged>0){\n\t$both--;\n\t$nrcump++;\n\t$costtot=$costtot+shift @merged;\n}\nprint $nrcump.' '.$costtot;\n;\nexit(0);"}, {"source_code": "use v5.20.1;\nuse warnings;\n\nmy $line = ;\nchomp($line);\n\nmy($a, $b, $c) = split(' ', $line);\n\n$line = ;\nchomp($line);\n\nmy $m = $line;\n\nmy @usb = ();\nmy @ps = ();\nmy $model = \"\";\nmy $price = 0;\n\nfor(my $i = 0; $i < $m; $i++){\n\n\t$line = ;\n\t($price, $model) = split(' ', $line);\n\tif($model eq \"USB\"){\n\n\t\tpush @usb, $price;\n\n\t}elsif($model eq \"PS/2\"){\n\n\t\tpush @ps, $price;\n\n\t}\n\n}\n\n@usb = sort @usb;\n@ps = sort @ps;\n\nmy $ui = 0;\nmy $pi = 0;\nmy $cost = 0;\nmy $mice = 0;\n\nwhile($ui < @usb && $a > 0){\n\n\t$cost += $usb[$ui];\n\t$ui++;\n\t$mice++;\n\t$a--;\n\n}\n\nwhile($pi < @ps && $b > 0){\n\n\t$cost += $ps[$pi];\n\t$pi++;\n\t$b--;\n\t$mice++;\n\n}\n\nwhile($ui < @usb && $pi < @ps && $c > 0){\n\n\tif($usb[$ui] < $ps[$pi]){\n\n\t\t$cost += $usb[$ui];\n\t\t$c--;\n\t\t$ui++;\n\t\t$mice++;\n\n\t}else{\n\n\t\t$cost += $ps[$pi];\n\t\t$pi++;\n\t\t$c--;\n\t\t$mice++;\n\n\t}\n\n}\n\nwhile($ui < @usb && $c > 0){\n\n\t$cost += $usb[$ui];\n\t$ui++;\n\t$c--;\n\t$mice++;\n\n}\n\nwhile($pi < @ps && $c > 0){\n\n\t$cost += $ps[$pi];\n\t$pi++;\n\t$c--;\n\t$mice++;\n\n}\n\nprint $mice, \" \", $cost, \"\\n\";\n"}, {"source_code": "use warnings;\n\nmy $line = <>;\nchomp($line);\nmy($muie, $cuie, $c) = split(' ', $line);\n\nmy $i;\nmy @v1;\nmy @v2;\nmy $m = <>;\nchomp($m);\n\nfor($i=1;$i<=$m;++$i) {\n\tmy $line = <>;\n\tchomp($line);\n\tmy($x, $y) = split(' ', $line);\n\t\n\tif($y eq \"USB\") {\n\t\tpush(@v1, $x);\n\t}\n\telse {\n\t\tpush(@v2, $x);\n\t}\n}\n\n@v1 = sort {$a <=> $b} @v1;\n@v2 = sort {$a <=> $b} @v2;\n\n$i=0;\nmy $nr=0;\nwhile($i < scalar @v1 && $i < $muie) {\n\t$nr++;\n\t\n\t$total+=$v1[$i];\n\t$i++;\n}\n\nmy $j=0;\nwhile($j < scalar @v2 && $j < $cuie) {\n\t$nr++;\n\t\n\t$total+=$v2[$j];\n\t$j++;\n}\n\n#for($i=0;$i 0) {\n#\tprintf(\"%d %d %d %d\\n\", $i, $j, scalar @v1, scalar @v2);\n\t\n\tif($j >= scalar @v2 || ($v1[$i] <= $v2[$j] && $i < scalar @v1)) {\n\t\tprint \"jeg\";\n\t\tprint $v1[$i];\n\t\t$total+=$v1[$i];\n\t\t++$i;\n\t}\n\telse {\n\t\t$total+=$v2[$j];\n\t\t++$j;\n\t}\n\t\n\t++$nr;\n\t--$c;\n}\n\n\nprintf(\"%d %d\\n\", $nr, $total);\n"}, {"source_code": "use warnings;\n\nmy $line = <>;\nchomp($line);\nmy($muie, $cuie, $c) = split(' ', $line);\n\nmy $i;\nmy @v1;\nmy @v2;\nmy $m = <>;\nchomp($m);\n\nfor($i=1;$i<=$m;++$i) {\n\tmy $line = <>;\n\tchomp($line);\n\tmy($x, $y) = split(' ', $line);\n\t\n\tif($y eq \"USB\") {\n\t\tpush(@v1, $x);\n\t}\n\telse {\n\t\tpush(@v2, $x);\n\t}\n}\n\n@v1 = sort {$a <=> $b} @v1;\n@v2 = sort {$a <=> $b} @v2;\n\n$i=0;\nmy $nr=0;\nwhile($i < scalar @v1 && $i < $muie) {\n\t$nr++;\n\t\n\t$total+=$v1[$i];\n\t$i++;\n}\n\nmy $j=0;\nwhile($j < scalar @v2 && $j < $cuie) {\n\t$nr++;\n\t\n\t$total+=$v2[$j];\n\t$j++;\n}\n\n#for($i=0;$i 0) {\n\tprintf(\"%d %d %d %d\\n\", $i, $j, scalar @v1, scalar @v2);\n\t\n\tif($j >= scalar @v2 || ($v1[$i] <= $v2[$j] && $i < scalar @v1)) {\n\t\tprint \"jeg\";\n\t\tprint $v1[$i];\n\t\t$total+=$v1[$i];\n\t\t++$i;\n\t}\n\telse {\n\t\t$total+=$v2[$j];\n\t\t++$j;\n\t}\n\t\n\t++$nr;\n\t--$c;\n}\n\n\nprintf(\"%d %d\\n\", $nr, $total);\n"}, {"source_code": "sub input { $_ = <>; chomp; wantarray? split: $_ }\n\nsub less_first { sort { $a <=> $b } @_ }\n\nsub buy {\n\tmy ($c, $aref) = @_;\n\twhile ($c--) {\n\t\tlast unless @$aref;\n\t\t$R++, $V += shift @$aref;\n\t}\n}\n\n@c = input; $m = input; \nfor (1..$m) {\n\t($v, $t) = input;\n\tif ($t eq \"USB\") {\n\t\tpush @v0, $v;\n\t} else {\n\t\tpush @v1, $v;\n\t}\n}\n\n@v0 = less_first @v0; \n@v1 = less_first @v1; \n\nbuy $c[0], \\@v0;\nbuy $c[1], \\@v1;\n@v2 = less_first @v0, @v1;\nbuy $c[2], \\@v2;\n\nprint $R, \" \", $V;\n"}, {"source_code": "use utf8;\nuse feature \"say\";\n\nmy $line = <>;\nchomp($line);\nmy ($usb, $ps2, $mix) = split(' ', $line);\nmy $pcNr = <>;\nchomp($pcNr);\nmy (@usb, @ps2);\nwhile ($pcNr > 0) {\n $line = <>;\n chomp($line);\n my($price, $type) = split(' ', $line);\n if ($type eq \"USB\") {\n push ( @usb, $price );\n } else {\n push ( @ps2, $price);\n }\n --$pcNr;\n}\n\n@usb = sort ( { $a <=> $b } @usb );\n@ps2 = sort ( { $a <=> $b } @ps2 );\nmy $price = 0;\nmy ( $iU, $iP );\n\nfor ( $iU = 0; $iU < scalar ( @usb ) && $usb > 0; ++$iU, --$usb ) {\n $price += $usb[$iU];\n ++$pcNr;\n}\nfor ( $iP = 0; $iP < scalar ( @ps2 ) && $ps2 > 0; ++$iP, --$ps2 ) {\n $price += $ps2[$iP];\n ++$pcNr;\n}\n\nfor ( ; ( $iU < scalar( @usb ) || $iP < scalar ( @ps2 ) ) && $mix > 0; --$mix ) {\n if ( ({ $usb[$iU] <=> $ps2[$iP] } < 1) || $iP >= scalar ( @ps2 ) ) {\n $price += $usb[$iU];\n ++$pcNr;\n ++$iU;\n } else {\n $price += $ps2[$iP];\n ++$pcNr;\n ++$iP;\n }\n}\n\nsay $pcNr.\" \".$price;"}, {"source_code": "use warnings;\nuse utf8;\nuse feature \"say\";\n\nmy $line = <>;\nchomp($line);\nmy ($usb, $ps2, $mix) = split(' ', $line);\nmy $pcNr = <>;\nchomp($pcNr);\nmy (@usb, @ps2);\nwhile ($pcNr > 0) {\n $line = <>;\n chomp($line);\n my($price, $type) = split(' ', $line);\n if ($type eq \"USB\") {\n push ( @usb, $price );\n } else {\n push ( @ps2, $price);\n }\n --$pcNr;\n}\n@usb = sort ( { $a <=> $b } @usb );\n@ps2 = sort ( { $a <=> $b } @ps2 );\n\nmy $price = 0;\nmy ( $iU, $iP );\nfor ( $iU = 0; $iU < scalar ( @usb ) && $usb > 0; ++$iU, --$usb ) {\n $price += $usb[$iU];\n say $usb[$iU];\n ++$pcNr;\n}\nfor ( $iP = 0; $iP < scalar ( @ps2 ) && $ps2 > 0; ++$iP, --$ps2 ) {\n $price += $ps2[$iP];\n say $ps2[$iP];\n ++$pcNr;\n}\nfor ( ; ( $iU < scalar( @usb ) || $iP < scalar ( @ps2 ) ) && $mix > 0; --$mix ) {\n if ( $usb[$iU] <= $ps2[$iP]) {\n $price += $usb[$iU];\n ++$pcNr;\n ++$iU;\n } else {\n $price += $ps2[$iP];\n ++$pcNr;\n ++$iP;\n }\n}\n\nsay $pcNr.\" \".$price;\n"}], "src_uid": "3d6151b549bd52f95ab7fdb972e6fb98"} {"nl": {"description": "Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise.You are given geometric progression b defined by two integers b1 and q. Remind that a geometric progression is a sequence of integers b1,\u2009b2,\u2009b3,\u2009..., where for each i\u2009>\u20091 the respective term satisfies the condition bi\u2009=\u2009bi\u2009-\u20091\u00b7q, where q is called the common ratio of the progression. Progressions in Uzhlyandia are unusual: both b1 and q can equal 0. Also, Dvastan gave Masha m \"bad\" integers a1,\u2009a2,\u2009...,\u2009am, and an integer l.Masha writes all progression terms one by one onto the board (including repetitive) while condition |bi|\u2009\u2264\u2009l is satisfied (|x| means absolute value of x). There is an exception: if a term equals one of the \"bad\" integers, Masha skips it (doesn't write onto the board) and moves forward to the next term.But the lesson is going to end soon, so Masha has to calculate how many integers will be written on the board. In order not to get into depression, Masha asked you for help: help her calculate how many numbers she will write, or print \"inf\" in case she needs to write infinitely many integers.", "input_spec": "The first line of input contains four integers b1, q, l, m (-109\u2009\u2264\u2009b1,\u2009q\u2009\u2264\u2009109, 1\u2009\u2264\u2009l\u2009\u2264\u2009109, 1\u2009\u2264\u2009m\u2009\u2264\u2009105)\u00a0\u2014 the initial term and the common ratio of progression, absolute value of maximal number that can be written on the board and the number of \"bad\" integers, respectively. The second line contains m distinct integers a1,\u2009a2,\u2009...,\u2009am (-109\u2009\u2264\u2009ai\u2009\u2264\u2009109)\u00a0\u2014 numbers that will never be written on the board.", "output_spec": "Print the only integer, meaning the number of progression terms that will be written on the board if it is finite, or \"inf\" (without quotes) otherwise.", "sample_inputs": ["3 2 30 4\n6 14 25 48", "123 1 2143435 4\n123 11 -5453 141245", "123 1 2143435 4\n54343 -13 6 124"], "sample_outputs": ["3", "0", "inf"], "notes": "NoteIn the first sample case, Masha will write integers 3,\u200912,\u200924. Progression term 6 will be skipped because it is a \"bad\" integer. Terms bigger than 24 won't be written because they exceed l by absolute value.In the second case, Masha won't write any number because all terms are equal 123 and this is a \"bad\" integer.In the third case, Masha will write infinitely integers 123. "}, "positive_code": [{"source_code": "$/ = \"\"; $_ = <>; chomp; ($b, $q, $l, $m, @a) = split; \n\n$a{$_} = 1 for @a;\nif (abs($b) <= $l) {\n\t$Y = 1, $B = !$a{$b}, $U = !$a{-$b}, $Z = !$a{0}\n}\t\n\nif ($b == 0) {\n\t$c = inf if $B;\n} elsif ($q == 0) {\n\t$c = 1 if $B;\n\t$c = inf if $Z;\n} elsif ($q == 1) {\n\t$c = inf if $B;\n} elsif ($q == -1) {\n\t$c = inf if $B || $U;\n} else {\n\t$c = 1 + int( log($l/abs($b)) / log(abs($q)) ) if $Y;\n\tfor ($i = $b; abs($i) <= $l; $i *= $q) { \n\t\t$c -= $a{$i};\n\t}\n}\n\nprint $c // 0;"}, {"source_code": "( $b, $q, $l ) = split ' ', <>;\nmap ++ $h{ $_ }, split ' ', <>;\n\t\nwhile( abs $b <= abs $l ){\n\t$A += !$h{ $b };\n\t$b *= $q;\n\tlast if $c ++ > 80;\n\t}\n\nprint $A > 35 ? inf : 0 + $A"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $b, $q, $l, $m ) = split;\n\tmy %h = map { $_ => 1 } split ' ', <>;\n\t\n\tmy @ans;\n\t\n\tmy $maxl = abs $l;\n\tmy $minl = - $maxl;\n\t\n\tmy $c = 0;\n\t\n\twhile( $minl <= $b and $b <= $maxl ){\n\t\t$h{ $b } or push @ans, $b;\n\t\t$b *= $q;\n\t\t$c ++ > 2e4 and last;\n\t\t}\n\t\n\tprint @ans >= 1e4 ? \"inf\" : 0 + @ans;\n\t}"}], "negative_code": [{"source_code": "$/ = \"\"; $_ = <>; chomp; ($b, $q, $l, $m, @a) = split; \n\n$a{$_} = 1 for @a;\nif (abs($b) <= $l) {\n\t$B = !$a{$b}, $U = !$a{-$b}, $Z = !$a{0}\n}\t\n\nif ($b == 0) {\n\t$c = inf if $B;\n} elsif ($q == 0) {\n\t$c = 1 if $B;\n\t$c = inf if $Z;\n} elsif ($q == 1) {\n\t$c = inf if $B;\n} elsif ($q == -1) {\n\t$c = inf if $B || $U;\n} else {\n\t$c = 1 + int( log($l/abs($b)) / log(abs($q)) );\n\tfor ($i = $b; abs($i) <= $l; $i *= $q) { \n\t\t$c -= $a{$i};\n\t}\n}\n\nprint $c // 0;"}, {"source_code": "$/ = \"\"; $_ = <>; chomp; ($b, $q, $l, $m, @a) = split; \n\n$a{$_} = 1 for @a;\nif (abs($b) <= $l) {\n\t$B = !$a{$b}, $U = !$a{-$b}, $Z = !$a{0}\n}\t\n\nif ($b == 0) {\n\t$c = inf if $B;\n} elsif ($q == 0) {\n\t$c = 1 if $B;\n\t$c = inf if $Z;\n} elsif ($q == 1) {\n\t$c = inf if $B;\n} elsif ($q == -1) {\n\t$c = inf if $B || $U;\n} else {\n\t$c = 1 + int( log($l/abs($b)) / log(abs($q)) ) if $B;\n\tfor ($i = $b; abs($i) <= $l; $i *= $q) { \n\t\t$c -= $a{$i};\n\t}\n}\n\nprint $c // 0;"}, {"source_code": "$/ = \"\"; $_ = <>; chomp; ($b, $q, $l, $m, @a) = split; \n\n$a{$_} = 1 for @a;\nif (abs($q) > 1) {\n\tfor ($i = $b; abs($i) <= $l; $i *= $q) { \n\t\t$C += $a{$i};\n\t}\n}\t\n$B = !$a{$b}, $U = !$a{-$b} if abs($b) <= $l;\n$Z = !$a{0};\n\nif ($b == 0) {\n\t$c = inf if $Z;\n} elsif ($q == 0) {\n\t$c = 1 if $B;\n\t$c = inf if $Z;\n} elsif ($q == 1) {\n\t$c = inf if $B;\n} elsif ($q == -1) {\n\t$c = inf if $B || $U;\n} else {\n\t$c = 1 - $C + int( log($l/abs($b)) / log(abs($q)) );\n}\n\nprint $c // 0;"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $b, $q, $l, $m ) = split;\n\tmy %h = map { $_ => 1 } split ' ', <>;\n\t\n\tmy @ans;\n\t\n\tmy $maxl = abs $l;\n\tmy $minl = - $maxl;\n\t\n\tmy $inf;\n\t\n\tif( $b == 0 ){\n\t\t$inf = $h{ 0 } ? 0 : \"inf\";\n\t\t}\n\telsif( $q == 0 ){\n\t\t$inf = $h{ 0 } ? 0 : \"inf\";\n\t\t$inf or $inf += ($minl <= $b and $b <= $maxl and not $h{ $b });\n\t\t}\n\t\n\tif( not defined $inf and $minl <= $b and $b <= $maxl and $q =~ /^-?1$/ ){\n\t\tif( $q == 1 ){ $inf = $h{ $b } ? 0 : \"inf\" }\n\t\telsif( $q == -1 ){ $inf = ($h{ $b } * $h{ 0 - $b }) ? 0 : \"inf\" }\n\t\t}\n\telsif( not defined $inf and not( $minl <= $b and $b <= $maxl) and $q =~ /^-?1$/ ){\n\t\t$inf = 0;\n\t\t}\n\t\n\twhile( not defined $inf and $minl <= $b and $b <= $maxl ){\n\t\t$h{ $b } or push @ans, $b;\n\t\t$b *= $q;\n\t\t}\n\t\n\tprint defined $inf ? $inf : 0 + @ans;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $b, $q, $l, $m ) = split;\n\tmy %h = map { $_ => 1 } split ' ', <>;\n\t\n\tmy @ans;\n\t\n\tmy $maxl = abs $l;\n\tmy $minl = - $maxl;\n\t\n\tmy $c = 0;\n\t\n\twhile( $minl <= $b and $b <= $maxl ){\n\t\t$h{ $b } or push @ans, $b;\n\t\t$b *= $q;\n\t\t$c ++ > 1e4 and last;\n\t\t}\n\t\n\tprint @ans >= 1e4 ? \"inf\" : 0 + @ans;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $b, $q, $l, $m ) = split;\n\tmy %h = map { $_ => 1 } split ' ', <>;\n\t\n\tmy @ans;\n\t\n\tmy $maxl = abs $l;\n\tmy $minl = - $maxl;\n\t\n\tmy $c = 0;\n\t\n\twhile( $minl <= $b and $b <= $maxl ){\n\t\t$h{ $b } or push @ans, $b;\n\t\t$b *= $q;\n\t\t$c ++ > 1e4 and last;\n\t\t}\n\t\n\tprint @ans >= 1e4 ? \"inf\" : 0 + @ans;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $b, $q, $l, $m ) = split;\n\tmy %h = map { $_ => 1 } split ' ', <>;\n\t\n\tmy @ans;\n\t\n\tmy $maxl = abs $l;\n\tmy $minl = - $maxl;\n\t\n\tmy $inf;\n\t$b == 0 || $q == 0 and $inf = \"inf\";\n\t\n\tif( not defined $inf and $minl <= $b and $b <= $maxl and $q == 1 ){\n\t\t$inf = $h{ $b } ? 0 : \"inf\";\n\t\t}\n\telsif( not defined $inf and not( $minl <= $b and $b <= $maxl) and $q == 1 ){\n\t\t$inf = 0;\n\t\t}\n\t\n\twhile( not defined $inf and $minl <= $b and $b <= $maxl ){\n\t\t$h{ $b } or push @ans, $b;\n\t\t$b *= $q;\n\t\t}\n\t\n\tprint defined $inf ? $inf : 0 + @ans;\n\t}"}], "src_uid": "749c290c48272a53e2e79730dab0538e"} {"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 \u2014 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$$$) \u2014 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$$$)\u00a0\u2014 the number of sticks and the number of diamonds, respectively.", "output_spec": "For each test case print one integer \u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nfor(my $i=0;$i<$t;$i++){\n my ($a,$b) = map { $_ - 0 } split(/\\s+/,);\n my $r = int( ( $a + $b ) / 3 );\n $r = $a if 2 * $a <= $b;\n $r = $b if 2 * $b <= $a;\n $r = 0 if $r < 0;\n print \"$r\\n\";\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nfor(my $i=0;$i<$t;$i++){\n my ($a,$b) = map { $_ - 0 } split(/\\s+/,);\n my $r = int( ( $a + $b ) / 3 );\n $r = $a if $a <= 2 * $b;\n $r = $b if $b <= 2 * $a;\n $r = 0 if $r < 0;\n print \"$r\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nfor(my $i=0;$i<$t;$i++){\n my ($a,$b) = map { $_ - 0 } split(/\\s+/,);\n my $r = int( ( $a + $b ) / 3 );\n $r = 0 if $r < 0;\n print \"$r\\n\";\n}\n"}], "src_uid": "8bbec86e427e26158393bbfbf1a067fe"} {"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$$$) \u2014 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$$$) \u2014 the current number and the number that Petya wants to get.", "output_spec": "For the $$$i$$$-th test case print the answer on it \u2014 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": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nmy $t = read_token();\n\nfor ( 1..$t ) {\n my $line = read_line();\n my ($x, $y) = split q{ }, $line;\n my $answer;\n if ( $x == 1 ) {\n if ( $y == 1 ) {\n $answer = \"yes\";\n }\n else {\n $answer = \"no\";\n }\n }\n elsif ( $x <= 3 && $y > 3 ) {\n $answer = \"no\";\n }\n else {\n $answer = \"yes\";\n }\n say $answer;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nmy $t = read_token();\n\nfor ( 1..$t ) {\n my $line = read_line();\n my ($x, $y) = split q{ }, $line;\n my $answer;\n if ( $x == 1 ) {\n $answer = \"no\";\n }\n elsif ( $x == 2 && $y > 3 ) {\n $answer = \"no\";\n }\n else {\n $answer = \"yes\";\n }\n say $answer;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n"}], "src_uid": "b3978805756262e17df738e049830427"} {"nl": {"description": "InteractionThis is an interactive problem. You need to read participants' queries from standard input and print your responses to standard output. You don't know the number of queries upfront, so you'll need to process them as you get them; you'll know you're done once you reach the end of the file.In each query, you will be asked the question, written in one line. You have to answer it correctly, patiently and without any display of emotions. Your response is case-insensitive.Please make sure to use the stream flushing operation after each response in order not to leave part of your output in some buffer.ExampleInput\nIs it rated?\nIs it rated?\nIs it rated?\nOutput\nNO\nNO\nNO", "input_spec": null, "output_spec": null, "sample_inputs": ["Is it rated?\nIs it rated?\nIs it rated?"], "sample_outputs": ["NO\nNO\nNO"], "notes": null}, "positive_code": [{"source_code": "print(\"no\\n\"x 9)"}, {"source_code": "#!/usr/bin/perl\r\n\r\n$|=1;\r\nwhile(){\r\n print \"NO\\n\";\r\n}\r\n"}, {"source_code": "$| ++;\r\n\r\nwhile(<>){\r\n print \"NO\\n\";\r\n}"}], "negative_code": [{"source_code": "say(\"no\\n\"x 9)"}], "src_uid": "57b62c485669809c0b59afd6613f901c"} {"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,\u2009a2,\u2009...,\u2009an 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000) and m (1\u2009\u2264\u2009m\u2009\u2264\u2009109)\u00a0\u2014 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,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109)\u00a0\u2014 the types of toys that Tanya already has.", "output_spec": "In the first line print a single integer k\u00a0\u2014 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,\u2009t2,\u2009...,\u2009tk (1\u2009\u2264\u2009ti\u2009\u2264\u2009109)\u00a0\u2014 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": "sub gets { my $s = <>; chomp($s); split(' ', $s) }\n($n, $m) = gets(); @a = gets();\n@a = sort({$a <=> $b} @a);\nA: for $a (@a) {\n\tif ($a - $a1 > 1) {\n\t\tfor $r ($a1 + 1 .. $a - 1) {\n\t\t\t$m -= $r;\n\t\t\tif ($m < 0) {\n\t\t\t\tlast A;\n\t\t\t}\n\t\t\tpush(@r, $r);\n\t\t}\n\t}\n\t$a1 = $a;\n}\nif ($m > 0) {\n $r = $a1;\n while (1) {\n \t$m -= ++$r;\n \tlast if $m < 0;\n \tpush(@r, $r);\n }\n}\nprint 0 + @r, \"\\n\";\nprint \"@r\\n\";\n"}, {"source_code": "chomp (my ($n, $m) = split /\\s+/, );\n\nchomp (my %arr = map {$_ => 1} split /\\s+/, );\n\nmy @ans;\nfor (my $i = 1; $i <= 10 ** 9; $i++) {\n next if exists $arr{$i};\n if ($m >= $i) {\n $m -= $i;\n push @ans, $i;\n } else {\n last;\n }\n}\n\nprintf \"%s\\n\", scalar @ans;\nprintf \"@ans\\n\";\n"}, {"source_code": "my @n = split(/ /, );\nmy $m = $n[1];\nmy @A = sort {$a <=> $b} split(/ /, );\nmy $j = 0, $t = 1;\nmy @ans;\nwhile(1) {\n while($A[$j] == $t) {\n $j++;\n $t++;\n }\n if($m >= $t) {\n $m -= $t;\n push @ans, $t;\n $t++;\n } else {\n print scalar @ans.\"\\n\";\n foreach $x (@ans) { print \"$x \"; }\n exit 0;\n }\n}"}, {"source_code": "($n, $m) = split ' ', <>;\n%h = map { $_, 0 } split ' ', <>;\n$h{ ++ $i } // do { ($m -= $i) < 0 or push @ans, $i } while $m > 0;\nprint ~~ @ans, \"\\n@ans\"\n"}, {"source_code": "($n, $m) = split ' ', <>;\n%h = map { $_, 0 } split ' ', <>;\n$h{ ++ $i } // do { ($m -= $i) < 0 or push @ans, $i } while $m > 0;\nprint ~~ @ans, \"\\n@ans\""}], "negative_code": [], "src_uid": "0318d4d5ea3425bf6506edeb1026f597"} {"nl": {"description": "For years, the Day of city N was held in the most rainy day of summer. New mayor decided to break this tradition and select a not-so-rainy day for the celebration. The mayor knows the weather forecast for the $$$n$$$ days of summer. On the $$$i$$$-th day, $$$a_i$$$ millimeters of rain will fall. All values $$$a_i$$$ are distinct.The mayor knows that citizens will watch the weather $$$x$$$ days before the celebration and $$$y$$$ days after. Because of that, he says that a day $$$d$$$ is not-so-rainy if $$$a_d$$$ is smaller than rain amounts at each of $$$x$$$ days before day $$$d$$$ and and each of $$$y$$$ days after day $$$d$$$. In other words, $$$a_d < a_j$$$ should hold for all $$$d - x \\le j < d$$$ and $$$d < j \\le d + y$$$. Citizens only watch the weather during summer, so we only consider such $$$j$$$ that $$$1 \\le j \\le n$$$.Help mayor find the earliest not-so-rainy day of summer.", "input_spec": "The first line contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$1 \\le n \\le 100\\,000$$$, $$$0 \\le x, y \\le 7$$$)\u00a0\u2014 the number of days in summer, the number of days citizens watch the weather before the celebration and the number of days they do that after. The second line contains $$$n$$$ distinct integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ denotes the rain amount on the $$$i$$$-th day.", "output_spec": "Print a single integer\u00a0\u2014 the index of the earliest not-so-rainy day of summer. We can show that the answer always exists.", "sample_inputs": ["10 2 2\n10 9 6 7 8 3 2 1 4 5", "10 2 3\n10 9 6 7 8 3 2 1 4 5", "5 5 5\n100000 10000 1000 100 10"], "sample_outputs": ["3", "8", "5"], "notes": "NoteIn the first example days $$$3$$$ and $$$8$$$ are not-so-rainy. The $$$3$$$-rd day is earlier.In the second example day $$$3$$$ is not not-so-rainy, because $$$3 + y = 6$$$ and $$$a_3 > a_6$$$. Thus, day $$$8$$$ is the answer. Note that $$$8 + y = 11$$$, but we don't consider day $$$11$$$, because it is not summer."}, "positive_code": [{"source_code": "#! /usr/bin/perl\nuse strict;\nmy $row = ;\nchomp($row);\nmy @inputs_p = split(' ',$row);\nmy $l = $inputs_p[0];\nmy $x = $inputs_p[1];\nmy $y = $inputs_p[2];\nmy $data = ;\nchomp($data);\nmy @data = split(' ',$data);\nsub test {\n my @array = @{$_[0]};\n my $size = scalar @array;\n my $value = $_[1];\n my $flag = 0;\n my $index = 0;\n while($index < $size){\n if($array[$index] < $value ) {$flag = 1; last;}\n $index++;\n }\n return $flag;\n}\nsub max {\n return $_[1] > $_[0] ? $_[1] : $_[0];\n}\nsub min {\n return $_[1] > $_[0] ? $_[0] : $_[1];\n}\nmy $index = 0;\nOUTER:while( $index < $l-1 ){\n \n if ($x == 0 && $y == 0 ){ last OUTER;}\n elsif($x == 0){\n \n while($index < $l-$y-1){\n my @split_data_pos= @data[$index+1..$index+$y];\n if( test(\\@split_data_pos,$data[$index]) == 0 ) { last OUTER;};\n $index++;\n }\n while(($index >= $l-1-$y) && ($index < $l)){\n my @split_data_pos= @data[$index+1..$l-1];\n if( test(\\@split_data_pos,$data[$index]) == 0 ) { last OUTER;};\n $index++;\n \n }\n }\n elsif($y == 0){\n \n while($index < $x){\n my $pre_ind = max($index-1,0);\n my @split_data_pre= @data[0..$pre_ind];\n if( test(\\@split_data_pre,$data[$index]) == 0 ) { last OUTER;};\n $index++;\n }\n while(($index >= $x) && ($index < $l)){\n my @split_data_pos= @data[$index-$x..$index-1];\n if( test(\\@split_data_pos,$data[$index]) == 0 ) { last OUTER;};\n $index++;\n \n }\n }\n else {\n \n while( ($index < $x ) && ($index+$y < $l))\n {\n \n my $pre_ind = max($index-1,0);\n my @split_data_pre= @data[0..$pre_ind];\n my @split_data_pos= @data[$index+1..$index+$y];\n if($index == 0){\n if(test(\\@split_data_pos,$data[$index]) == 0 ) {last OUTER;}\n }\n \n if( ( test(\\@split_data_pre,$data[$index]) == 0 ) && ( test(\\@split_data_pos,$data[$index]) == 0 )) { last OUTER;}\n $index++;\n \n }\n \n while( ($index < $l-1-$y) && ($index >= $x) ){\n \n my @split_data_pre= @data[$index-$x..$index-1];\n my @split_data_pos= @data[$index+1..$index+$y];\n if( ( test(\\@split_data_pre,$data[$index]) == 0 ) && ( test(\\@split_data_pos,$data[$index]) == 0 )) {last OUTER;}\n $index++;\n \n }\n while( ($index >= $l-1-$y) && ($index < $l-1) ){\n \n my @split_data_pre= @data[$index-$x..$index-1];\n my @split_data_pos= @data[$index+1..$l-1];\n if((test(\\@split_data_pre,$data[$index]) == 0 )&& (test(\\@split_data_pos,$data[$index]) == 0) ) {last OUTER;}\n if($index == $l-1) {\n \n if(test(\\@split_data_pre,$data[$index]) == 0) {last OUTER;} \n }\n $index++;\n \n }\n }\n}\nprint($index+1);\n\n"}], "negative_code": [{"source_code": "#! /usr/bin/perl\nuse strict;\nmy $row = ;\nchomp($row);\nmy @inputs_p = split(' ',$row);\nmy $l = $inputs_p[0];\nmy $x = $inputs_p[1];\nmy $y = $inputs_p[2];\nmy $data = ;\nchomp($data);\nmy @data = split(' ',$data);\nsub test {\n my @array = @{$_[0]};\n my $size = scalar @array;\n my $value = $_[1];\n my $flag = 0;\n my $index = 0;\n while($index < $size){\n if($array[$index] < $value ) {$flag = 1; last;}\n $index++;\n }\n return $flag;\n}\nmy $index = $x;\nmy $flag = 0;\nwhile( $flag != 1 && $index < $l){\n while( $index < $l-1-$y && $flag != 1){\n my @split_data_pre= @data[$index-$x..$index-1];\n my @split_data_pos= @data[$index+1..$index+$y];\n if( ( test(\\@split_data_pre,$data[$index]) == 0 ) && ( test(\\@split_data_pos,$data[$index]) == 0 )) {$flag = 1;}\n $index++;\n }\n while( $index >= $l-1-$y && $index < $l && $flag != 1)\n {\n my @split_data_pre= @data[$index-$x..$index-1];\n my @split_data_pos= @data[$index+1..$l-1];\n if((test(\\@split_data_pre,$data[$index]) == 0 )&& (test(\\@split_data_pos,$data[$index]) == 0) ) {$flag = 1;}\n $index++;\n \n }\n}\nprint($index);\n"}, {"source_code": "#! /usr/bin/perl\nuse strict;\nmy $row = ;\nchomp($row);\nmy @inputs_p = split(' ',$row);\nmy $l = $inputs_p[0];\nmy $x = $inputs_p[1];\nmy $y = $inputs_p[2];\nmy $data = ;\nchomp($data);\nmy @data = split(' ',$data);\nsub test {\n my @array = @{$_[0]};\n my $size = scalar @array;\n my $value = $_[1];\n my $flag = 0;\n my $index = 0;\n while($index < $size){\n if($array[$index] < $value ) {$flag = 1; last;}\n $index++;\n }\n return $flag;\n}\nsub max {\n return $_[1] > $_[0] ? $_[1] : $_[0];\n}\nsub min {\n return $_[1] > $_[0] ? $_[0] : $_[1];\n}\nmy $index = 0;\n\nOUTER:while( $index < $l-1 ){\n if ($x == 0 && $y == 0 ){ last OUTER;}\n elsif($x == 0){\n while($index < $l-$y-1){\n my @split_data_pos= @data[$index+1..$index+$y];\n if( test(\\@split_data_pos,$data[$index]) == 0 ) { last OUTER;};\n $index++;\n }\n while(($index >= $l-1-$y) && ($index < $l)){\n my @split_data_pos= @data[$index+1..$l-1];\n if( test(\\@split_data_pos,$data[$index]) == 0 ) { last OUTER;};\n $index++;\n \n }\n }\n elsif($y == 0){\n while($index < $x){\n my $pre_ind = max($index-1,0);\n my @split_data_pre= @data[0..$pre_ind];\n if( test(\\@split_data_pre,$data[$index]) == 0 ) { last OUTER;};\n $index++;\n }\n while(($index >= $x) && ($index < $l)){\n my @split_data_pos= @data[$index-$x..$index-1];\n if( test(\\@split_data_pos,$data[$index]) == 0 ) { last OUTER;};\n $index++;\n \n }\n }\n else {\n while( ($index < $x ) )\n {\n my $pre_ind = max($index-1,0);\n my @split_data_pre= @data[0..$pre_ind];\n my @split_data_pos= @data[$index+1..$index+$y];\n\n if( ( test(\\@split_data_pre,$data[$index]) == 0 ) && ( test(\\@split_data_pos,$data[$index]) == 0 )) { last OUTER;}\n $index++;\n \n }\n \n while( ($index < $l-1-$y) && ($index >= $x) ){\n my @split_data_pre= @data[$index-$x..$index-1];\n my @split_data_pos= @data[$index+1..$index+$y];\n if( ( test(\\@split_data_pre,$data[$index]) == 0 ) && ( test(\\@split_data_pos,$data[$index]) == 0 )) { last OUTER;}\n $index++;\n \n }\n while( ($index >= $l-1-$y) && ($index < $l) ){\n my @split_data_pre= @data[$index-$x..$index-1];\n my @split_data_pos= @data[$index+1..$l-1];\n if((test(\\@split_data_pre,$data[$index]) == 0 )&& (test(\\@split_data_pos,$data[$index]) == 0) ) {last OUTER;}\n $index++;\n \n }\n }\n}\nprint(min($index+1,$l));\n\n"}, {"source_code": "#! /usr/bin/perl\nuse strict;\nmy $row = ;\nchomp($row);\nmy @inputs_p = split(' ',$row);\nmy $l = $inputs_p[0];\nmy $x = $inputs_p[1];\nmy $y = $inputs_p[2];\nmy $data = ;\nchomp($data);\nmy @data = split(' ',$data);\nsub test {\n my @array = @{$_[0]};\n my $size = scalar @array;\n my $value = $_[1];\n my $flag = 0;\n my $index = 0;\n while($index < $size){\n if($array[$index] < $value ) {$flag = 1; last;}\n $index++;\n }\n return $flag;\n}\nsub max {\n return $_[1] > $_[0] ? $_[1] : $_[0];\n}\nsub min {\n return $_[1] > $_[0] ? $_[0] : $_[1];\n}\nmy $index = max($x-1,0);\n\nOUTER:while( $index < $l-1 ){\n while( ($index <= $x-1 ) )\n {\n my $pre_ind = max($index-1,0);\n my $pos_ind = min($index+$y,$index+1);\n my @split_data_pre= @data[0..$pre_ind];\n my @split_data_pos= @data[$index+1..$index+$y];\n if( ( test(\\@split_data_pre,$data[$index]) == 0 ) && ( test(\\@split_data_pos,$data[$index]) == 0 )) { last OUTER;}\n $index++;\n \n }\n \n while( ($index < $l-1-$y) && ($index > $x-1) ){\n my $pre_ind = max($index-1,$index-$x);\n my $pos_ind = min($index+$y,$index+1);\n my @split_data_pre= @data[$index-$x..$pre_ind];\n my @split_data_pos= @data[$index+1..$index+$y];\n if( ( test(\\@split_data_pre,$data[$index]) == 0 ) && ( test(\\@split_data_pos,$data[$index]) == 0 )) { last OUTER;}\n $index++;\n \n }\n while( ($index >= $l-1-$y) && ($index < $l) )\n {\n my $pre_ind = max($index-1,$index-$x);\n my @split_data_pre= @data[$index-$x..$pre_ind];\n my @split_data_pos= @data[$index+1..$l-1];\n if((test(\\@split_data_pre,$data[$index]) == 0 )&& (test(\\@split_data_pos,$data[$index]) == 0) ) {last OUTER;}\n $index++;\n \n }\n}\nprint($index+1);\n\n"}], "src_uid": "5e2a5ee02c1a2f35a52e76cde96463a3"} {"nl": {"description": "You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i.\u00a0e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k > 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\\sum\\limits_{i = 1}^{n}{n^{n - i} \\cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$)\u00a0\u2014 the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\\dots, p_n$$$ ($$$1 \\le p_i \\le n$$$; $$$p_i \\neq p_j$$$ if $$$i \\neq j$$$)\u00a0\u2014 values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.", "sample_inputs": ["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1"], "sample_outputs": ["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1"], "notes": "NoteIn the first test case, one of the optimal strategies is the next one: take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 \\cdot 4 + 4^2 \\cdot 3 + 4^1 \\cdot 2 + 4^0 \\cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$.In the second test case, one of the optimal strategies is: take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 \\cdot 5 + 5^3 \\cdot 2 + 5^2 \\cdot 4 + 5^1 \\cdot 3 + 5^0 \\cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$.In the third test case, one of the optimal strategies is: take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 \\cdot 6 + 6^4 \\cdot 1 + 6^3 \\cdot 5 + 6^2 \\cdot 3 + 6^1 \\cdot 4 + 6^0 \\cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\tpush @_, 1e6;\n\t\n\tmy $max = 0;\n\tmy @acc;\n\tmy @ans;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\t\n\t\tif( $_[ $i ] > $max ){\n\t\t\tunshift @ans, @acc;\n\t\t\t@acc = ();\n\t\t\t$max = $_[ $i ];\n\t\t\t}\n\t\telse{\n\t\t\t;\n\t\t\t}\n\t\t\n\t\tpush @acc, $_[ $i ];\n\t\t}\n\t\n\tprint \"@ans\";\n\t}"}], "negative_code": [], "src_uid": "1637670255f8bd82a01e2ab20cdcc9aa"} {"nl": {"description": "Allen and Bessie are playing a simple number game. They both know a function $$$f: \\{0, 1\\}^n \\to \\mathbb{R}$$$, i.\u00a0e. the function takes $$$n$$$ binary arguments and returns a real value. At the start of the game, the variables $$$x_1, x_2, \\dots, x_n$$$ are all set to $$$-1$$$. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an $$$i$$$ such that $$$x_i = -1$$$ and either setting $$$x_i \\to 0$$$ or $$$x_i \\to 1$$$.After $$$n$$$ rounds all variables are set, and the game value resolves to $$$f(x_1, x_2, \\dots, x_n)$$$. Allen wants to maximize the game value, and Bessie wants to minimize it.Your goal is to help Allen and Bessie find the expected game value! They will play $$$r+1$$$ times though, so between each game, exactly one value of $$$f$$$ changes. In other words, between rounds $$$i$$$ and $$$i+1$$$ for $$$1 \\le i \\le r$$$, $$$f(z_1, \\dots, z_n) \\to g_i$$$ for some $$$(z_1, \\dots, z_n) \\in \\{0, 1\\}^n$$$. You are to find the expected game value in the beginning and after each change.", "input_spec": "The first line contains two integers $$$n$$$ and $$$r$$$ ($$$1 \\le n \\le 18$$$, $$$0 \\le r \\le 2^{18}$$$). The next line contains $$$2^n$$$ integers $$$c_0, c_1, \\dots, c_{2^n-1}$$$ ($$$0 \\le c_i \\le 10^9$$$), denoting the initial values of $$$f$$$. More specifically, $$$f(x_0, x_1, \\dots, x_{n-1}) = c_x$$$, if $$$x = \\overline{x_{n-1} \\ldots x_0}$$$ in binary. Each of the next $$$r$$$ lines contains two integers $$$z$$$ and $$$g$$$ ($$$0 \\le z \\le 2^n - 1$$$, $$$0 \\le g \\le 10^9$$$). If $$$z = \\overline{z_{n-1} \\dots z_0}$$$ in binary, then this means to set $$$f(z_0, \\dots, z_{n-1}) \\to g$$$.", "output_spec": "Print $$$r+1$$$ lines, the $$$i$$$-th of which denotes the value of the game $$$f$$$ during the $$$i$$$-th round. Your answer must have absolute or relative error within $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is considered correct if $$$\\frac{|a - b|}{\\max{(1, |b|)}} \\le 10^{-6}$$$.", "sample_inputs": ["2 2\n0 1 2 3\n2 5\n0 4", "1 0\n2 3", "2 0\n1 1 1 1"], "sample_outputs": ["1.500000\n2.250000\n3.250000", "2.500000", "1.000000"], "notes": "NoteConsider the second test case. If Allen goes first, he will set $$$x_1 \\to 1$$$, so the final value will be $$$3$$$. If Bessie goes first, then she will set $$$x_1 \\to 0$$$ so the final value will be $$$2$$$. Thus the answer is $$$2.5$$$.In the third test case, the game value will always be $$$1$$$ regardless of Allen and Bessie's play."}, "positive_code": [{"source_code": "#!usr/bin/perl -w\nuse strict;\n\nmy($n,$r)=split/ /,;my @c=split/ /,;my @out;chomp($r,$c[-1]);\nmy @rg;my $sum=0;\nfor(0..$#c){$sum+=$c[$_];}\npush(@out,($sum/2**$n).\"\\n\");\nfor my $i(1..$r){\n my $in=;chomp($in);\n push(@rg,$in);\n}\nfor my $i(0..$#rg){\n $sum=$sum-$c[(split(/ /,$rg[$i]))[0]]+(split(/ /,$rg[$i]))[-1];\n $c[(split(/ /,$rg[$i]))[0]]=(split(/ /,$rg[$i]))[-1];\n push(@out,($sum/2**$n).\"\\n\");\n}\nprint @out;\n"}], "negative_code": [], "src_uid": "6cef2d464febcee854ee5a7f78d7ba4a"} {"nl": {"description": "In an attempt to escape the Mischievous Mess Makers' antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and his k cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists of n rooms located in a row, some of which are occupied.Farmer John wants to book a set of k\u2009+\u20091 currently unoccupied rooms for him and his cows. He wants his cows to stay as safe as possible, so he wishes to minimize the maximum distance from his room to the room of his cow. The distance between rooms i and j is defined as |j\u2009-\u2009i|. Help Farmer John protect his cows by calculating this minimum possible distance.", "input_spec": "The first line of the input contains two integers n and k (1\u2009\u2264\u2009k\u2009<\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of rooms in the hotel and the number of cows travelling with Farmer John. The second line contains a string of length n describing the rooms. The i-th character of the string will be '0' if the i-th room is free, and '1' if the i-th room is occupied. It is guaranteed that at least k\u2009+\u20091 characters of this string are '0', so there exists at least one possible choice of k\u2009+\u20091 rooms for Farmer John and his cows to stay in.", "output_spec": "Print the minimum possible distance between Farmer John's room and his farthest cow.", "sample_inputs": ["7 2\n0100100", "5 1\n01010", "3 2\n000"], "sample_outputs": ["2", "2", "1"], "notes": "NoteIn the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is impossible to make this distance 1, as there is no block of three consecutive unoccupied rooms.In the second sample, Farmer John can book room 1 for himself and room 3 for his single cow. The distance between him and his cow is 2.In the third sample, Farmer John books all three available rooms, taking the middle room for himself so that both cows are next to him. His distance from the farthest cow is 1."}, "positive_code": [{"source_code": "use strict;\n\nchomp (my ($n, $k) = split /\\s+/, );\nchomp (my @m = split //, );\n\nsub min ($$) {\n return ($_[0] < $_[1]) ? $_[0] : $_[1];\n}\nsub max ($$) {\n return ($_[0] > $_[1]) ? $_[0] : $_[1];\n}\n\nsub getNextFreeRoom ($) {\n my $i = $_[0] + 1;\n\n while (($i < $n) && $m[$i]) {\n $i++;\n }\n\n return $i;\n}\n\nsub findFirstHit {\n my ($x, $y);\n\n $y = $x = &getNextFreeRoom(-1);\n for (my $i = 0; $i < $k; $i++) {\n $y = &getNextFreeRoom ($y)\n }\n\n return ($x, $y);\n}\n\nsub adjustOwner ($$$$) {\n my ($x, $y, $z, $res) = @_;\n\n my $nextZ = &getNextFreeRoom($z);\n\n while (max($z - $x, $y - $z) > max($nextZ - $x, $y - $nextZ)) {\n $z = $nextZ;\n $nextZ = &getNextFreeRoom($z);\n }\n\n $res = min ($res, max ($z - $x, $y - $z));\n return ($z, $res);\n}\n\nmy ($x, $y) = &findFirstHit;\nmy ($z, $res) = &adjustOwner ($x, $y, $x, 10 ** 9);\n\nwhile ($y < $n) {\n ($z, $res) = &adjustOwner ($x, $y, $z, $res);\n $x = &getNextFreeRoom ($x);\n $y = &getNextFreeRoom ($y);\n}\n\nprint \"$res\";\n"}], "negative_code": [{"source_code": "use strict;\n\nchomp (my ($n, $k) = split /\\s+/, );\nchomp (my @m = split //, );\n\nsub min ($$) {\n return ($_[0] < $_[1]) ? $_[0] : $_[1];\n}\nsub max ($$) {\n return ($_[0] > $_[1]) ? $_[0] : $_[1];\n}\n\nsub getNextFreeRoom ($) {\n my $i = $_[0] + 1;\n\n while (($i < $n) && $m[$i]) {\n $i++;\n }\n\n return $i;\n}\n\nsub findFirstHit {\n my ($x, $y);\n\n $y = $x = &getNextFreeRoom(-1);\n for (my $i = 0; $i < $k; $i++) {\n $y = &getNextFreeRoom ($y)\n }\n\n return ($x, $y);\n}\n\nsub adjustOwner ($$$$) {\n my ($x, $y, $z, $res) = @_;\n\n my $nextZ = &getNextFreeRoom($z);\n\n while (max($z - $x, $y - $z) > max($nextZ - $x, $y - $nextZ)) {\n $z = $nextZ;\n }\n\n $res = min ($res, max ($z - $x, $y - $z));\n return ($z, $res);\n}\n\nmy ($x, $y) = &findFirstHit;\nmy ($z, $res) = &adjustOwner ($x, $y, $x, 10 ** 9);\n\nwhile ($y < $n) {\n ($z, $res) = &adjustOwner ($x, $y, $z, $res);\n $x = &getNextFreeRoom ($x);\n $y = &getNextFreeRoom ($y);\n}\n\nprint \"$res\";\n"}], "src_uid": "a5d1a96fd8514840062d747e6fda2c37"} {"nl": {"description": "Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. ", "input_spec": "The first line of the input contains n (2\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105). In the next line there are n space-separated integers b1, b2, ..., bn (1\u2009\u2264\u2009bi\u2009\u2264\u2009109).", "output_spec": "The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.", "sample_inputs": ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"], "sample_outputs": ["1 1", "4 1", "2 4"], "notes": "NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. "}, "positive_code": [{"source_code": "my $n = <>;\nmy @a = sort {$a <=> $b } split \" \", <>;\nmy ($minc, $maxc) = (0, 0);\nfor (0 .. $n - 1) {\n last unless $a[$_] == $a[0];\n ++$minc;\n}\nfor (reverse 0 .. $n - 1) {\n last unless $a[$_] == $a[$n - 1];\n ++$maxc;\n}\n\nprintf (\"%.0f %.0f\\n\", $a[-1] - $a[0], $minc == $n? $n * ($n - 1) / 2 : $minc * $maxc);"}, {"source_code": "my $n = <>;\nmy @arr = split / /, <>;\nmy ($min, $max) = (10**9, 0);\nmy ($ans, $cmin, $cmax) = (0, 0, 0);\nfor (@arr) {\n if ($_ > $max) {\n $max = $_;\n $cmax = 0;\n }\n if ($_ < $min) {\n $min = $_;\n $cmin = 0;\n }\n $cmax++ if ($_ == $max);\n $cmin++ if ($_ == $min);\n}\n$ans = $cmax * $cmin;\n$ans = ($n * ($n - 1)) / 2 if($max == $min);\nprint $max - $min . \" \" . $ans . \"\\n\";\n"}, {"source_code": "#!/perl -w\n\nuse strict ;\n\nchomp( my $n = );\n\nmy @beauty = split /\\s+/ , ;\n\nmy $maxbeauti = 0 ;\nmy $minbeauti = 10E9 + 1;\nmy $maxcount = 0 ;\nmy $mincount = 0 ;\n\nforeach( @beauty ){\n if ( $_ >= $maxbeauti ){\n $maxbeauti = $_ ;\n }\n if ( $_ <= $minbeauti ){\n $minbeauti = $_ ;\n }\n}\nif( $minbeauti != $maxbeauti ){\n foreach( @beauty ){\n if ( $_ == $maxbeauti ){\n $maxcount ++ ;\n }\n if ( $_ == $minbeauti ){\n $mincount ++ ;\n }\n }\n print $maxbeauti - $minbeauti , \" \" , $mincount * $maxcount ;\n}else{\n print \"0\" , \" \" , $n*($n - 1)/2 ;\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = split / /, <>;\n@a = sort { $a <=> $b } @a;\n$a[0]==$a[$n-1] and say 0, \" \", $n*($n-1)/2 and exit;\n$i = 0;\nwhile ($a[$i] == $a[0]) {\n\t++$i;\n}\n$j = $n-1;\nwhile ($a[$j] == $a[$n-1]) {\n\t--$j;\n}\n($mx, $ans) = ($a[$n-1]-$a[0], $i * ($n-1-$j));\nsay \"$mx $ans\";"}, {"source_code": "# Perl\nchomp($n = <>);\nchomp($_ = <>);\n@a = sort { $a <=> $b } split / /;\nforeach (@a) {\n\t$c++ if $_==$a[0] or last;\n}\nforeach (reverse @a) {\n\t$d++ if $_==$a[-1] or last;\n}\nif ($a[-1] == $a[0]) {\n\tprint \"0 \", $n*($n-1)/2, \"\\n\";\n} else {\n\tprint $a[-1]-$a[0], ' ', $c*$d, \"\\n\";\n}\n\n"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\n<>;\nmy @x = sort { $a <=> $b } split ' ', <>;\nmy $min = $x[0];\nmy $max = $x[-1];\nmy $max_diff = $max - $min;\n\nmy $count_min = 0;\nfor my $x (@x) {\n if ($x > $min) {\n last;\n } else {\n $count_min++;\n }\n}\n\nmy $count_max = 0;\nfor my $x (reverse @x) {\n if ($x < $max) {\n last;\n } else {\n $count_max++;\n }\n}\n\nmy $ways = $max_diff == 0 ? $count_min * ($count_min - 1) / 2\n : $count_min * $count_max;\n\nsay \"$max_diff $ways\";\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @num = split ' ',<>;\n@num = sort {$a <=> $b} @num;\nmy ($res,$cnt1,$cnt2) = ($num[$#num] - $num[0], 0, 0);\nif($res == 0){\n\tprint 0, \" \", ($n*($n-1))/2, \"\\n\";\n\texit 0;\n}\nforeach(@num){\n\t$cnt1 += $_ == $num[0];\n\t$cnt2 += $_ == $num[$#num];\n}\nprint \"$res \", $cnt1*$cnt2, \"\\n\";\n"}, {"source_code": "<>;\n@a=split/ /,<>;\n$y=10**9;\n($y > $_ and $y = $_), $Y < $_ and $Y = $_ for @a;\n($y == $_ and $w++), $Y == $_ and $W++ for @a;\nprint $y == $Y ? (\"0 \", $w*($w-1)/2) : ($Y-$y, \" \", $w*$W)"}], "negative_code": [{"source_code": "my $n = <>;\nmy @a = sort split \" \", <>;\nmy ($minc, $maxc) = (0, 0);\nfor (0 .. $n - 1) {\n last unless $a[$_] == $a[0];\n ++$minc;\n}\nfor (reverse 0 .. $n - 1) {\n last unless $a[$_] == $a[$n - 1];\n ++$maxc;\n}\n\nprint $a[-1] - $a[0], \" \", $minc == $n? $n * ($n - 1) / 2 : $minc * $maxc;"}, {"source_code": "my $n = <>;\nmy @a = sort split \" \", <>;\nmy ($minc, $maxc) = (0, 0);\nfor (0 .. $n - 1) {\n last unless $a[$_] == $a[0];\n ++$minc;\n}\nfor (reverse 0 .. $n - 1) {\n last unless $a[$_] == $a[$n - 1];\n ++$maxc;\n}\n\nprintf (\"%.0f %.0f\\n\", $a[-1] - $a[0], $minc == $n? $n * ($n - 1) / 2 : $minc * $maxc);"}, {"source_code": "my $n = <>;\nmy @arr = split / /, <>;\nmy ($min, $max) = (10**9, 0);\nmy ($ans, $cmin, $cmax) = (0, 0, 0);\nfor (@arr) {\n if ($_ > $max) {\n $max = $_;\n $cmax = 0;\n }\n if ($_ < $min) {\n $min = $_;\n $cmin = 0;\n }\n $cmax++ if ($_ == $max);\n $cmin++ if ($_ == $min);\n}\n$ans = $cmax * $cmin;\n$ans = ($n * ($n - 1)) / 2 if($max == $min);\nprint $ans . \"\\n\";"}, {"source_code": "#!/perl -w\n\nuse strict ;\n\nchomp( my $n = );\n\nmy @beauty = split /\\s+/ , ;\n\nmy $maxbeauti = 0 ;\nmy $minbeauti = 10E9 + 1;\nmy $maxcount = 0 ;\nmy $mincount = 0 ;\n\nforeach( @beauty ){\n if ( $_ >= $maxbeauti ){\n $maxbeauti = $_ ;\n }\n if ( $_ <= $minbeauti ){\n $minbeauti = $_ ;\n }\n}\n\nforeach( @beauty ){\n if ( $_ == $maxbeauti ){\n $maxcount ++ ;\n }\n if ( $_ == $minbeauti ){\n $mincount ++ ;\n }\n}\n\nprint $maxbeauti-$minbeauti , \" \" , $mincount * $maxcount ;"}, {"source_code": "# Perl \nchomp($n = <>); \nchomp($_ = <>);\n@a = sort sub {$a <=> $b}, split / /;\n foreach (@a) {\n\t$a++ if $_==$a[0] or last;\n } \nforeach (reverse @a) { \n\t$b++ if $_==$a[-1] or last;\n } \nif ($a[-1] == $a[0]) { \n\tprint \"0 \", $n*($n-1)/2, \"\\n\"; \n} else { \n\tprint $a[-1]-$a[0], ' ', $a*$b, \"\\n\";\n }\n\n"}, {"source_code": "# Perl\nchomp($n = <>);\n@a = sort split / /, <>;\nforeach (@a) {\n\t$a++ if $_==$a[0] or last;\n}\nforeach (reverse @a) {\n\t$b++ if $_==$a[-1] or last;\n}\nprint $a[-1]-$a[0], ' ', $a*$b, \"\\n\";\n\n"}, {"source_code": "# Perl\nchomp($n = <>);\nchomp($_ = <>);\n@a = sort split / /;\nforeach (@a) {\n\t$a++ if $_==$a[0] or last;\n}\n$e = $a[-1];\nforeach (reverse @a) {\n\t$b++ if $_==$e or last;\n}\nif ($a[-1] == $a[0]) {\n\tprint \"0 \", $n*($n-1)/2, \"\\n\";\n} else {\n\tprint $a[-1]-$a[0], ' ', $a*$b, \"\\n\";\n}\n\n"}, {"source_code": "# Perl\nchomp($n = <>);\n@a = sort split / /, <>;\nforeach (@a) {\n\t$a++ if $_==$a[0] or last;\n}\nforeach (reverse @a) {\n\t$b++ if $_==$a[-1] or last;\n}\nif ($a[-1] == $a[0]) {\n\tprint \"0 \", $n*($n-1)/2, \"\\n\";\n} else {\n\tprint $a[-1]-$a[0], ' ', $a*$b, \"\\n\";\n}\n\n"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\n<>;\nmy @x = sort { $a <=> $b } split ' ', <>;\nmy $min = $x[0];\nmy $max = $x[-1];\nmy $max_diff = $max - $min;\n\nmy $count_min = 0;\nfor my $x (@x) {\n if ($x > $min) {\n last;\n } else {\n $count_min++;\n }\n}\n\nmy $count_max = 0;\nfor my $x (reverse @x) {\n if ($x < $max) {\n last;\n } else {\n $count_max++;\n }\n}\n\nmy $ways = $count_min * $count_max;\n\nsay \"$max_diff $ways\";\n"}, {"source_code": "<>;\n@a=split/ /,<>;\n$y=10**9;\n($y > $_ and $y = $_), $Y < $_ and $Y = $_ for @a;\nprint $y == $Y ? (\"0 \", $y*($y-1)/2) : ($Y-$y, \" \", $y*$Y)"}, {"source_code": "<>;\n@_=split/ /,<>;\n$_>$max and $max=$_ for @_;\n$min=$max;\n$_<$max and $max=$_ for @_;\n$min==$_ and $MIN++ for @_;\n$max==$_ and $MAX++ for @_;\n$max-$min and $f=$MIN*$MAX;\nprint $max-$min, \" \", 0+$f;"}], "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 \u00ab-1\u00bb (without quotes). Otherwise, the first line of output should contain the only integer k (k\u2009\u2265\u20090)\u00a0\u2014 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": "($s, $_) = <>; $\\ = \"\\n\";\n\nfor $a (a..z) {\n\tfor $b (a..z) {\n\t\t$t = eval(\"y/$a$b/$b$a/r\");\n\t\t$c = ($s ^ $t) =~ y/\\000//;\n\t\t$C = $c if !$C;\n\t\tif ($c > $C && $u !~ /$a|$b/) {\n\t\t\t$_ = $t;\n\t\t\t$C = $c;\n\t\t\tfor (\"$a $b\") {\n\t\t\t\tpush @r, $_;\n\t\t\t\t$u .= $_;\n\t\t\t}\n\t\t}\n\t}\n}\n\nif ($_ eq $s) {\n\tprint 0 + @r;\n\tprint for @r;\n} else {\n\tprint -1;\n}"}], "negative_code": [{"source_code": "($s, $_) = <>; $\\ = \"\\n\";\n\nfor $a (a..z) {\n\tfor $b (a..z) {\n\t\t$t = eval(\"y/$a$b/$b$a/r\");\n\t\t$c = ($s ^ $t) =~ y/\\000//;\n\t\tif ($c > $C) {\n\t\t\t$_ = $t;\n\t\t\t$C = $c;\n\t\t\tpush @r, \"$a $b\";\n\t\t}\n\t}\n}\n\nif ($_ eq $s) {\n\tshift @r; \n\tprint 0 + @r;\n\tprint for @r;\n} else {\n\tprint -1;\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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of dice in the tower. The second line contains an integer x (1\u2009\u2264\u2009x\u2009\u2264\u20096) \u2014 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,\u2009bi (1\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u20096;\u00a0ai\u2009\u2260\u2009bi) \u2014 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": "chomp($totalDice = );\nchomp($topFace = );\n$bottomFace = 7 - $topFace;\n@diceFaces = ( 1, 2, 3, 4, 5, 6);\n@topBottomFaces = ( $topFace, $bottomFace);\nmy %diceFaces=map{$_ =>1} @diceFaces;\nmy %topBottomFaces=map{$_=>1} @topBottomFaces;\nmy @remFaces=grep(!defined $topBottomFaces{$_}, @diceFaces);\nwhile () { # assigns each line in turn to $_\n #print \"Just read in this line: $_\";\n $currentLine = $_;\n if($currentLine =~ /([1-6]) ([1-6])/) {\n $leftFace = $1;\n $rightFace = $2;\n if ( grep { $_ eq $leftFace } @remFaces ) {\n }\n else {\n print \"NO\";\n exit 0;\n }\n if ( grep { $_ eq $rightFace } @remFaces ) {\n }\n else {\n print \"NO\";\n exit 0;\n }\n }\n $totalDice--;\n}\nif ( $totalDice == 0) {\n print \"YES\";\n}\nelse {\n print \"NO\";\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n=<>;\nmy $up=<>;\nmy @current=split(' ',<>);\nmy $last=7-$up;\nmy $can=1;\nforeach my $count(1..$n-1){\n\t@current=split(' ',<>);\n\tif($last==$current[0] || $last==7-$current[0] || $last==$current[1] || $last==7-$current[1]){\n\t\t$can=0;\n\t}\n}\nif($can==1){\n\tprint\"YES\\n\";\n}else{\n\tprint\"NO\\n\";\n}"}], "negative_code": [{"source_code": "chomp($totalDice = );\n$topFace = ;\n$bottomFace = 7 - $topFace;\n@diceFaces = ( 1, 2, 3, 4, 5, 6);\n@topBottomFaces = ( $topFace, $bottomFace);\n@diff{ @diceFaces } = { @diceFaces };\ndelete @diff{ @topBottomFaces };\n@remFaces = (keys %diff);\nwhile () { # assigns each line in turn to $_\n\t#print \"Just read in this line: $_\";\n\t$currentLine = $_;\n\tif($currentLine =~ /([1-6]) ([1-6])/) {\n\t\t$leftFace = $1;\n\t\t$rightFace = $2;\n\t\tif ( grep { $_ eq $leftFace } @remFaces ) {\n\t\t}\n\t\telse {\n\t\t\tprint \"NO\";\n\t\t\texit 0;\n\t\t}\n\t\tif ( grep { $_ eq $leftFace } @remFaces ) {\n\t\t}\n\t\telse {\n\t\t\tprint \"NO\";\n\t\t\texit 0;\n\t\t}\n\t}\n\t$totalDice--;\n}\nif ( $totalDice == 0) {\n\tprint \"YES\";\n}\nelse {\n\tprint \"NO\";\n}"}, {"source_code": "chomp($totalDice = );\n$topFace = ;\n$bottomFace = 7 - $topFace;\n@diceFaces = ( 1, 2, 3, 4, 5, 6);\n@topBottomFaces = ( $topFace, $bottomFace);\n@diff{ @diceFaces } = { @diceFaces };\ndelete @diff{ @topBottomFaces };\n@remFaces = (keys %diff);\nwhile () { # assigns each line in turn to $_\n $currentLine = $_;\n if($currentLine =~ /([1-6]) ([1-6])/) {\n $leftFace = $1;\n $rightFace = $2;\n if ( grep { $_ eq $leftFace } @remFaces ) {\n }\n else {\n print \"NO\";\n exit 0;\n }\n if ( grep { $_ eq $rightFace } @remFaces ) {\n }\n else {\n print \"NO\";\n exit 0;\n }\n }\n $totalDice--;\n}\nif ( $totalDice == 0) {\n print \"YES\";\n}\nelse {\n print \"NO\";\n}"}], "src_uid": "2d6a1202139e1f77f32377224e1fe752"} {"nl": {"description": "Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called \"Black Square\" on his super cool touchscreen phone.In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip.You've got a string s, describing the process of the game and numbers a1,\u2009a2,\u2009a3,\u2009a4. Calculate how many calories Jury needs to destroy all the squares?", "input_spec": "The first line contains four space-separated integers a1, a2, a3, a4 (0\u2009\u2264\u2009a1,\u2009a2,\u2009a3,\u2009a4\u2009\u2264\u2009104). The second line contains string s (1\u2009\u2264\u2009|s|\u2009\u2264\u2009105), where the \u0456-th character of the string equals \"1\", if on the i-th second of the game the square appears on the first strip, \"2\", if it appears on the second strip, \"3\", if it appears on the third strip, \"4\", if it appears on the fourth strip.", "output_spec": "Print a single integer \u2014 the total number of calories that Jury wastes.", "sample_inputs": ["1 2 3 4\n123214", "1 5 3 2\n11221"], "sample_outputs": ["13", "13"], "notes": null}, "positive_code": [{"source_code": "use List::Util \"sum\";\n@a = split \" \", <>;\n@b = split \"\", <>;\nprint ((sum map { $a[$_ - 1] } @b) - @a[-1]);"}, {"source_code": "my @arr = (0, split / /, <>);\nmy $ans = 0;\nfor (split //, <>) {\n $ans += $arr[$_];\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n@a = (0, split / /, <>);\nchomp($line = <>);\n$tb{int($_)}++ foreach (split //, $line);\n$ans+=$a[$_] * $tb{$_} foreach(keys %tb);\nsay $ans;"}, {"source_code": "\n@val = (0, split(/ /, <>));\n$ans = 0;\nforeach $i (split(//, <>)) {\n\t$ans += $val[$i];\n}\nprint \"$ans\\n\";\n"}, {"source_code": "use strict;\nmy @a = split(' ', <>);\nmy @b = split(//, <>);\nmy $ans = 0;\nsub kek{\n my ($a, $b) = @_;\nfor(my $i = 0; $i < $#b; $i++){\n #print $a[$b[$i] - 1], ' ';\n $ans += $a[$b[$i] - 1];\n}\nprint $ans;\n}\nkek(\\@a, \\@b);\n"}, {"source_code": "#!/usr/bin/perl\n\nmy @cal = split / /, <>;\nmy $s = <>;\nchomp $s;\n\nprint eval join '+', map { $cal[$_ - 1] } (split //, $s);"}, {"source_code": "while(<>){\n\tchomp;\n\t@_=split/ /;\n\t$_=<>;\n\t$sum=0;\n\twhile(/./g){\n\t\t$& == 1 and $sum+=$_[0];\n\t\t$& == 2 and $sum+=$_[1];\n\t\t$& == 3 and $sum+=$_[2];\n\t\t$& == 4 and $sum+=$_[3];\n\t\t\n\t\t}\n\t\n\t\n\tprint $sum;\n\tprint \"\\n\";\n\t}"}], "negative_code": [{"source_code": "use strict;\n#open STDIN, 'output.txt';\nmy @a = split(' ', <>);\nmy @b = split(//, <>);\nmy $ans = 0;\nsub kek{\n my ($a, $b) = @_;\nfor(my $i = 0; $i < $#b; $i++){\n print $a[$b[$i] - 1], ' ';\n $ans += $a[$b[$i] - 1];\n}\nprint $ans;\n}\nkek(\\@a, \\@b);\n"}], "src_uid": "db9065d975878227a749083f0036a169"} {"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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100) \u2014 the number of boys and girls in the dancing room.", "output_spec": "In the first line print k \u2014 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": "while (<>){\n ($n, $m)= split/ /;\n print $m+$n-1,\"\\n\";\n print \"1 $_\\n\" for 1..$m;\n print \"$_ 1\\n\" for 2..$n;\n }"}], "negative_code": [], "src_uid": "14fc3a7fef44a02791aee62838c4c8c8"} {"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$$$)\u00a0\u2014the 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": "#!/usr/bin/perl\r\n\r\nmy ($tcase) = split(/\\s+/o,);\r\n\r\nwhile( $tcase -- > 0 ){\r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n if( length($s) % 2 == 1 ){\r\n print \"No\\n\"; next;\r\n }\r\n if( substr($s,0,length($s)/2) eq substr($s,length($s)/2,length($s)/2) ){\r\n print \"Yes\\n\";\r\n } else {\r\n print \"No\\n\";\r\n }\r\n}\r\n"}, {"source_code": "<>;print m/^(.+)\\1$/?\"YES \":\"NO \"while<>"}, {"source_code": "<>;print m/^(.+)\\1$/?\"YES\\n\":\"NO\\n\"while<>"}, {"source_code": "<>;\r\n\r\nprint m/^(.+)\\1$/ ? \"YES\\n\" : \"NO\\n\" while <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tprint m/^(.+)\\1$/ ? \"YES\" : \"NO\";\n\t\n\t\n\t\n\t}"}, {"source_code": "##!/usr/local/bin/perl \r\n\r\nuse strict;\r\nuse warnings;\r\n\r\n#use Data::Dumper qw(Dumper);\r\nuse feature qw(say);\r\n\r\n#print \"------------------------------------------------------------------------------\\n\";\r\n\r\n#open my $fh,'<','codef_1703_input.txt' or die();\r\n\r\nmy $lines = ;\r\nmy @out;\r\n\r\nfor(my $i = 0; $i < $lines; $i++)\r\n{\r\n my $temp = ;\r\n chomp($temp);\r\n my $len = length($temp);\r\n if(($len % 2))\r\n {\r\n push(@out,\"NO\");\r\n }\r\n elsif(substr($temp,0,$len/2) eq substr($temp,$len/2,$len/2))\r\n {\r\n push(@out,\"YES\");\r\n }else\r\n {\r\n push(@out,\"NO\");\r\n }\r\n}\r\nfor(my $i = 0; $i < $lines; $i++)\r\n{\r\n say $out[$i];\r\n}\r\n\r\n\r\n#say(Dumper %hash);\r\n\r\n# print \"------------------------------------------------------------------------------\\n\";"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\n\r\nmy ($tcase) = split(/\\s+/o,);\r\n\r\nwhile( $tcase -- > 0 ){\r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n if( length($s) % 2 == 1 ){\r\n print \"No\\n\"; next;\r\n }\r\n print ( substr($s,0,length($s)/2) . \"\\n\" );\r\n}\r\n"}, {"source_code": "#!/usr/bin/perl\r\n\r\nmy ($tcase) = split(/\\s+/o,);\r\n\r\nwhile( $tcase -- > 0 ){\r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n my %x = ();\r\n for(my $i=0;$i 1;\r\n\t}\r\n print \"$res\\n\";\r\n}\r\n"}], "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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the index of the marked page. ", "output_spec": "Print a single integer\u00a0\u2014 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\u00a0\u2014 $$$[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": "#!/usr/bin/perl -wl\nuse strict;\n\nwhile(<>) {\n @_ = reverse map { my @k = split ' ', <>; $k[1] } 1..$_;\n chomp(my $k = <>);\n 1 while scalar(@_) > 0 and eval $k.'-'.pop @_ > 0;\n print scalar(@_) + 1;\n}\n"}, {"source_code": "#!/usr/bin/perl -wl\n\nuse strict;\n\nwhile( <> ){\n $_ = join ',', map { '-' x ( 1 + eval join '-', reverse split ' ', <> ) } 1 .. $_;\n s/-*\\K-/;/g;\n y/,//d;\n chomp( my $k = <> );\n substr $_, 0, $k - 1, '';\n print scalar( () = /;/g );\n }\n"}], "negative_code": [], "src_uid": "2545b6af730f99193041a8810b728cb3"} {"nl": {"description": "You are given a string s consisting only of characters 0 and 1. A substring [l,\u2009r] of s is a string slsl\u2009+\u20091sl\u2009+\u20092... sr, and its length equals to r\u2009-\u2009l\u2009+\u20091. 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\u2009\u2264\u2009n\u2009\u2264\u2009100000) \u2014 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,\u20096]. It is balanced, and its length is 4. Choosing the substring [2,\u20095] is also possible.In the second example it's impossible to find a non-empty balanced substring."}, "positive_code": [{"source_code": "<>;\n$_ = <>;\n%h = ( 0, 0 );\n\nwhile( /./g ){\n\t$z += 2 * $& - 1;\n\t\texists $h{ $z } ?\n\t\t( $max < (pos) - $h{ $z } and $max = (pos) - $h{ $z } )\n\t\t:\n\t\t( $h{ $z } //= pos )\n\t}\n\nprint 0 + $max"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\tmy $_0 = 0;\n\tmy $_1 = 1;\n\t\n\ts/1+/ \"1_\" . ( length $& ) . \" \" /ge;\n\ts/\\b0+/ \"0_\" . ( length $& ) . \" \" /ge;\n\t\n\t$debug and print;\n\t\n\tmy $z = 0;\n\t\n\t1 while \n#\t\t( $z ++ < 15 ) and \n\t0\n\t\t|| ( $debug and 0 * print \">$_\" ) \n\t\t|| s/(\\d)_(\\d+) ((??{ 1 - $1 }))_(\\d+)/\n\t\t\t\t$2 > $4 ? \n\t\t\t\t\t\t( $1 . '_' . ( $2 - $4 ) . ' ' . 'X_' . $4 ) \n\t\t\t\t: \n\t\t\t\t$2 < $4 ?\n\t\t\t\t\t\t( 'X_' . $2 . ' ' . $3 . '_' . ( $4 - $2 ) )\n\t\t\t\t:\n\t\t\t\t\t\t( 'X_' . $2 )\n\t\t\t\t/ge\n\t\t|| s/(\\d)_(\\d+) X_(\\d+) ((??{ 1 - $1 }))_(\\d+)/\n\t\t\t\t$1 . '_' . ( $2 + $3 ) . ' ' . $4 . '_' . ( $5 + $3 )\n\t\t\t\t/ge\n\t\t|| s/X_(\\d+) X_(\\d+)/ 'X_' . ( $1 + $2 ) /ge\n\t\t|| 0\n\t\t;\n\t\n\tprint 2 * ( sort { $b <=> $a } /X_\\K\\d+/g )[ 0 ];\n\t\n\t$debug and print '-' x 20;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\tmy $_0 = 0;\n\tmy $_1 = 1;\n\t\n\ts/1+/ \"1_\" . ( length $& ) . \" \" /ge;\n\ts/\\b0+/ \"0_\" . ( length $& ) . \" \" /ge;\n\t\n\t$debug and print;\n\t\n\tmy $z = 0;\n\t\n\t1 while 0\n\t\t|| ( $debug and 0 * print \">$_\" ) \n\t\t|| s/(\\d)_(\\d+) (?!\\1)(\\d)_(\\d+)/\n\t\t\t\t$2 > $4 ? \n\t\t\t\t\t\t( $1 . '_' . ( $2 - $4 ) . ' ' . 'X_' . $4 ) \n\t\t\t\t: \n\t\t\t\t$2 < $4 ?\n\t\t\t\t\t\t( 'X_' . $2 . ' ' . $3 . '_' . ( $4 - $2 ) )\n\t\t\t\t:\n\t\t\t\t\t\t( 'X_' . $2 )\n\t\t\t\t/ge\n\t\t|| s/(\\d)_(\\d+) X_(\\d+) (?!\\1)(\\d)_(\\d+)/\n\t\t\t\t$1 . '_' . ( $2 + $3 ) . ' ' . $4 . '_' . ( $5 + $3 )\n\t\t\t\t/ge\n\t\t|| s/X_(\\d+) X_(\\d+)/ 'X_' . ( $1 + $2 ) /ge\n\t\t|| 0\n\t\t;\n\t\n\tprint 2 * ( sort { $b <=> $a } /X_\\K\\d+/g )[ 0 ];\n\t\n\t$debug and print '-' x 20;\n\t}"}], "src_uid": "08fa04303060d38d6cd0f3a321a527ad"} {"nl": {"description": "At the big break Nastya came to the school dining room. There are $$$n$$$ pupils in the school, numbered from $$$1$$$ to $$$n$$$. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to despond because some pupils in the queue can agree to change places with some other pupils.Formally, there are some pairs $$$u$$$, $$$v$$$ such that if the pupil with number $$$u$$$ stands directly in front of the pupil with number $$$v$$$, Nastya can ask them and they will change places. Nastya asks you to find the maximal number of places in queue she can move forward. ", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n \\leq 3 \\cdot 10^{5}$$$, $$$0 \\leq m \\leq 5 \\cdot 10^{5}$$$)\u00a0\u2014 the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second. The second line contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$\u00a0\u2014 the initial arrangement of pupils in the queue, from the queue start to its end ($$$1 \\leq p_i \\leq n$$$, $$$p$$$ is a permutation of integers from $$$1$$$ to $$$n$$$). In other words, $$$p_i$$$ is the number of the pupil who stands on the $$$i$$$-th position in the queue. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \\leq u_i, v_i \\leq n, u_i \\neq v_i$$$), denoting that the pupil with number $$$u_i$$$ agrees to change places with the pupil with number $$$v_i$$$ if $$$u_i$$$ is directly in front of $$$v_i$$$. It is guaranteed that if $$$i \\neq j$$$, than $$$v_i \\neq v_j$$$ or $$$u_i \\neq u_j$$$. Note that it is possible that in some pairs both pupils agree to change places with each other. Nastya is the last person in the queue, i.e. the pupil with number $$$p_n$$$.", "output_spec": "Print a single integer\u00a0\u2014 the number of places in queue she can move forward.", "sample_inputs": ["2 1\n1 2\n1 2", "3 3\n3 1 2\n1 2\n3 1\n3 2", "5 2\n3 1 5 4 2\n5 2\n5 4"], "sample_outputs": ["1", "2", "1"], "notes": "NoteIn the first example Nastya can just change places with the first pupil in the queue.Optimal sequence of changes in the second example is change places for pupils with numbers $$$1$$$ and $$$3$$$. change places for pupils with numbers $$$3$$$ and $$$2$$$. change places for pupils with numbers $$$1$$$ and $$$2$$$. The queue looks like $$$[3, 1, 2]$$$, then $$$[1, 3, 2]$$$, then $$$[1, 2, 3]$$$, and finally $$$[2, 1, 3]$$$ after these operations."}, "positive_code": [{"source_code": "#!/usr/bin/perl -wl\nuse strict;\n\nsub g { split /\\s+/, <>; }\n\nwhile(<>) {\n my ($n, $m) = split;\n my @a = g();\n my %p;\n for (1..$m) {\n my ($former, $latter) = g();\n push @{$p{$latter}}, $former;\n }\n my @cnt;\n for (0..$#{$p{$a[$n-1]}}) {\n $cnt[$p{$a[$n-1]}[$_]]++;\n }\n my $ans = 0;\n for (reverse 0..$n-2) {\n if ($cnt[$a[$_]] and $cnt[$a[$_]] eq $n-1-$_-$ans) {\n $ans++;\n } else {\n for my $i (0..$#{$p{$a[$_]}}) {\n $cnt[$p{$a[$_]}[$i]]++;\n }\n }\n }\n print $ans;\n}\n"}], "negative_code": [], "src_uid": "1716b35de299e88c891ba71f9c368b51"} {"nl": {"description": "The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself.There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi\u2009=\u2009i; For each station i there exists exactly one station j such that pj\u2009=\u2009i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x,\u2009y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1\u2009\u2264\u2009x,\u2009y\u2009\u2264\u2009n).The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes.The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! ", "input_spec": "The first line contains one integer number n (1\u2009\u2264\u2009n\u2009\u2264\u2009100000) \u2014 the number of stations. The second line contains n integer numbers p1, p2, ..., pn (1\u2009\u2264\u2009pi\u2009\u2264\u2009n) \u2014 the current structure of the subway. All these numbers are distinct.", "output_spec": "Print one number \u2014 the maximum possible value of convenience.", "sample_inputs": ["3\n2 1 3", "5\n1 5 4 3 2"], "sample_outputs": ["9", "17"], "notes": "NoteIn the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1,\u20091), (1,\u20092), (1,\u20093), (2,\u20091), (2,\u20092), (2,\u20093), (3,\u20091), (3,\u20092), (3,\u20093).In the second example the mayor can change p2 to 4 and p3 to 5."}, "positive_code": [{"source_code": "$/ = undef; $_ = <>; ($n, @p) = split;\n\nfor ($i = 0; $i < $n; $i++) {\n\t$c = 0;\n\twhile ($p[$i]) {\n\t\t$j = $p[$i] - 1;\n\t\t$c += 1;\n\t\t$p[$i] = 0;\n\t\t$i = $j;\n\t}\n\t$ans += $c * $c;\n\t($x, $y) = sort { $b <=> $a} $x, $y, $c;\n}\n\nprint $ans + 2 * $x * $y;\n"}, {"source_code": "$_ = <>;\n\nmap $h{ ++ $i } = $_, split ' ', <>;\n\nfor( 1 .. $_ ){\n\tnext if ! $h{ $_ };\n\t$c = 0;\n\twhile( $h{ $_ } ){\n\t\t$c ++;\n\t\t$o = $_;\n\t\t$_ = $h{ $o };\n\t\tdelete $h{ $o };\n\t\t}\n\tpush @c, $c;\n\t}\n\n@c = sort { $a <=> $b } @c;\n\n@c > 1 and $c[ -2 ] += pop @c;\n\n$Z = 0;\n$Z += $_ ** 2 for @c;\n\nprint $Z"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\tmy $i = 0;\n\tmap { $h{ ++ $i } = $_ } @_;\n\tmy @c;\n\t\n\tfor my $key ( 1 .. @_ ){\n\t\tnext if not exists $h{ $key };\n\t\tmy $new = $key;\n\t\tmy $c = 0;\n\t\twhile( exists $h{ $new } ){\n\t\t\t$c ++;\n\t\t\tmy $old = $new;\n\t\t\t$new = $h{ $old };\n\t\t\tdelete $h{ $old };\n\t\t\t}\n\t\tpush @c, $c;\n\t\t}\n\t\n\t@c = ( sort { $a <=> $b } @c );\n\t\n\tif( @c >= 2 ){\n\t\tpush @c, ( pop @c ) + ( pop @c );\n\t\t}\n\t\n\tmy $sum = 0;\n\tmap { $sum += $_ ** 2 } @c;\n\t\n\tprint $sum;\n\t}"}], "negative_code": [{"source_code": "<>;\n\nmap $h{ ++ $i } = $_, split ' ', <>;\n\nfor( 1 .. $_ ){\n\tnext if ! $h{ $_ };\n\t$c = 0;\n\twhile( $h{ $_ } ){\n\t\t$c ++;\n\t\t$o = $_;\n\t\t$_ = $h{ $o };\n\t\tdelete $h{ $o };\n\t\t}\n\tpush @c, $c;\n\t}\n\n@c = sort { $a <=> $b } @c;\n\n@c > 1 and $c[ -2 ] += pop @c;\n\n$Z = 0;\n$Z += $_ ** 2 for @c;\n\nprint $Z"}], "src_uid": "ad9fd71025c5f91cece740ea95b0eb6f"} {"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\u2009+\u20091. 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\u2009+\u20091 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\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000, 1\u2009\u2264\u2009t\u2009\u2264\u2009109)\u00a0\u2014 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\nwhile(<>){\n\t($n, $t) = split;\n\t$_ = <>, chomp;\n\t$copy = $_;\n\ts/(.*)\\.//;\n\t$integer = $1;\n\n\tprint s/(4*)([5-9]).*// ? do {\n\t\t$_123 = $`;\n\t\t($_4_length = length $1) -= $t;\n\n\t\tif ( $1 and $_4_length >= 0 ){ $_45 = 4 x $_4_length . 5 }\n\t\tif ( $1 and $_4_length < 0 or $2 and ! $1 ){ (length $_123 ? $_123 : $integer) ++ }\n\t\t\"$integer.${_123}${_45}\" =~ s/\\.$//r\n\t} : $copy;\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $t) = split;\n\t$_ = <>, chomp;\n\t$c = $_;\n\ts/(.*)\\.//;\n\t$start = $1;\n\tprint s/(4*)([5-9]).*// ? do {\n\t\t$left = $`;\n\t\t$len = length $1;\n\t\t$len += - $t;\n\t\t$rem = '';\n\t\tif ( $1 and $len >= 0 ){ $rem = 4 x $len . 5 }\n\t\tif ( $1 and $len < 0 ){ length $left ? $left ++ : $start ++ }\n\t\tif ( $2 and not $1 ){ length $left ? $left ++ : $start ++ }\n\t\t$ans = \"$start.$left$rem\";\n\t\t$ans =~ s/\\.$//;\n\t\t$ans;\n\t} : $c;\n\t\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $t) = split;\n\t$_ = <>, chomp;\n\t$c = $_;\n\ts/(.*)\\.//;\n\t$start = $1;\n\tprint s/(4*)([5-9]).*// ? do {\n\t\t$left = $`;\n\t\t$len = length $1;\n\t\t$len += - $t;\n\t\t$rem = '';\n\t\tif ( $1 and $len >= 0 ){ $rem = 4 x $len . 5 }\n\t\tif ( $1 and $len < 0 ){ defined $left ? $left ++ : $start ++ }\n\t\tif ( $2 and not $1 ){ defined $left ? $left ++ : $start ++ }\n\t\t$ans = \"$start.$left$rem\";\n\t\t$ans =~ s/\\.$//;\n\t\t$ans;\n\t} : $c;\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $t) = split;\n\t$_ = <>, chomp;\n\t$c = $_;\n\ts/(.*)\\.//;\n\t$start = $1;\n\tprint s/(4*)([5-9]).*// ? do {\n\t\t$left = $`;\n\t\t$len = length $1;\n\t\t$len += - $t;\n\t\t$rem = '';\n\t\tif ( $1 and $len >= 0 ){ $rem = 4 x $len . 5 }\n\t\tif ( $1 and $len < 0 ){ $left ? $left ++ : $start ++ }\n\t\tif ( $2 and not $1 ){ $left ? $left ++ : $start ++ }\n\t\t$ans = \"$start.$left$rem\";\n\t\t$ans =~ s/\\.$//;\n\t\t$ans;\n\t} : $c;\n\t\n\t}"}], "src_uid": "d563ce32596b54f48544a6085efe5cc5"} {"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)$$$\u00a0\u2014 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})$$$\u00a0\u2014 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": "#!/usr/bin/perl \n\nuse warnings;\nuse strict;\n\nuse Data::Dumper;\n\nmy $keyboards = <>;\nmy $numbers = <>;\n\nmy @splitted = split(\" \", $numbers);\n\nmy $min;\nmy $max;\nforeach my $one (@splitted) {\n\tif (! defined $min || $min > $one) {\n\t $min = $one;\n\t}\n\tif (! defined $max || $one > $max) {\n\t\t$max = $one;\n\t}\n}\n\t\nmy $result = $max - $min + 1 - $keyboards;\n\nprint \"$result\\n\";\n"}, {"source_code": "use v5.10;\n\nchomp($n = );\nchomp($piano = );\n@pianoList = split / /, $piano;\n@pianoList = sort {$a<=>$b} @pianoList;\nsay $pianoList[$#pianoList] - $pianoList[0] - $n + 1;"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = sort { $a <=> $b } split ' ', <>;\n\t\n#\tprint \"@_\";\n\t\n\tprint $_[ @_ - 1 ] - $_[ 0 ] - @_ + 1;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = sort { $a <=> $b } split ' ', <>;\n\t\n\tprint \"@_\";\n\t\n\tprint $_[ @_ - 1 ] - $_[ 0 ] - @_ + 1;\n\t}"}], "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 \u2014 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/perl\nchomp ($_ = <>);\n$i = 0;\n$res = 0;\nfor ($i = 0; $i < length ($_) - 4; $i++) {\n $block = substr $_, $i, 5;\n \n if ($block eq \"heavy\") {\n $h++;\n $i += 4;\n } elsif ($block eq \"metal\") {\n $res += $h;\n $i += 4;\n }\n}\n\nprint $res;\n \n"}, {"source_code": "#!/usr/bin/perl\n\nwhile (<>) {\n chomp;\n $i=$j=0;\n s/heavy/H/g;\n s/metal/M/g;\n s/[a-z]//g;\n @m=split//;\n foreach (@m){\n /H/ and $i++;\n /M/ and $j+=$i;\n }\n print \"$j\\n\";\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl -pi\nfor ($i = 0; $i < length ($_); $i++) {\n $block = substr $_, $i, 5;\n \n if ($block eq \"heavy\") {\n $h++;\n $i += 4;\n } elsif ($block eq \"metal\") {\n $res += $h;\n $i += 4;\n }\n}\n$_ = $res;\n \n"}, {"source_code": "#!/usr/bin/perl\nchomp ($_ = <>);\n$_ =~ s/heavy/1/g;\n$_ =~ s/metal/0/g;\n\n$_ =~ s/[^10]//g;\n$i = ($_ =~ s/[10]/\\1/g);\nprint $i - 1;\n"}, {"source_code": "#!/usr/bin/perl\n\nwhile (<>) {\n chomp;\n s/heavy/H/g;\n s/metal/M/g;\n s/[a-z]//g;\n @m=split//;\n foreach (@m){\n /H/ and $i++;\n /M/ and $j+=$i;\n }\n print \"$j\\n\";\n $i=$j=0;\n}"}], "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": "#!/usr/bin/perl\n\nmy ($n,$m) = map { $_ - 0 } split(/\\s+/,);\n\nmy @aa = map { $_ - 0 } split(/\\s+/,);\nmy @bb = map { $_ - 0 } split(/\\s+/,);\n\nmy @ab = ();\n\nfor(my $i=0;$i<$n;$i++){\n my @tmp = ();\n $#tmp = $m-1;\n for(my $j=0;$j<$m;$j++){\n $tmp[$j] = ( $aa[$i] & $bb[$j] );\n }\n push(@ab,\\@tmp);\n}\n\nmy $res = 511;\nfor(my $k=0;$k<512;$k++){\n my $i_ok = 1;\n for(my $i=0;$i<$n;$i++){\n my $j_ok = undef;\n for(my $j=0;$j<$m;$j++){\n if( ( $k | $ab[$i]->[$j] ) == $k ){\n $j_ok = 1; last;\n }\n }\n unless( $j_ok ){\n $i_ok = undef;\n last;\n }\n }\n if( $i_ok ){\n $res = $k;\n last;\n }\n}\n\nprint \"$res\\n\";\n\n\n"}], "negative_code": [], "src_uid": "3da5075048a127319ffa8913859d2aa7"} {"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.\u00a0e. $$$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$$$) \u2014 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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\t\n\tfor my $i ( 0 .. @A - 1 ){\n\t\t( $A[ $i ], $B[ $i ] ) = sort { $b <=> $a } $A[ $i ], $B[ $i ];\n\t\t}\n\t\n\t@A = sort { $b <=> $a } @A;\n\t@B = sort { $b <=> $a } @B;\n\t\n\tprint $A[ 0 ] * $B[ 0 ];\n\t}"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009100, 1\u2009\u2264\u2009k\u2009\u2264\u20091000).", "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\u2009+\u20093\u2009=\u20094, in the second row \u2014 3\u2009+\u20091\u2009=\u20094, in the first column \u2014 1\u2009+\u20093\u2009=\u20094 and in the second column \u2014 3\u2009+\u20091\u2009=\u20094. 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": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$_=$'.(\" 0\"x($`-1));\n\nfor $i(1..$`){\ns/\\n//;\n\t$j++ and print \"\\n\";\nprint;\n\ts/^\\d+ //;\n\t$_.=\" \".$&;\n\tchop;\n\t}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [{"source_code": "<>=~/ /;\n\n$_=$'.(\" 0\"x($`-1));\n\nfor $i(1..$`){\n\t$j++ and print \"\\n\";\n\tprint;\n\ts/^\\d+ //;\n\t$_.=\" \".$&;\n\tchop;\n\t}"}, {"source_code": "<>=~/ /;\n\n$_=$'.(\" 0\"x($`-1));\ns/\\n//;\n\nfor $i(1..$`){\n\tprint;\n\tprint \"\\n\";\n\ts/^\\d+ //;\n\t$_.=\" \".$&;\n\tchop;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$_=$'.(\" 0\"x($`-1));\ns/\\n//;\n\nfor $i(1..$`){\n\tprint;\n\tprint \"\\n\";\n\ts/^\\d+ //;\n\t$_.=\" \".$&;\n\tchop;\n\t}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "<>=~/ /;\n\n$_=$'.(\" 0\"x($`-1));\ns/\\n//;\n\nfor $i(1..$`){\n$j++ and print \"\\n\";\n\tprint;\n\ts/^\\d+ //;\n\t$_.=\" \".$&;\n\tchop;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$_=$'.(\" 0\"x($`-1));\n# print;\n\nfor $i(1..$`){\n\t$j++ and print \"\\n\";\n\tprint;\n\ts/^\\d+ //;\n\t$_.=\" \".$&;\n\tchop;\n\t}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$_=$'.(\" 0\"x($`-1));\n\nfor $i(1..$`){\n\tprint;\n\tprint \"\\n\";\n\ts/^\\d+ //;\n\t$_.=\" \".$&;\n\tchop;\n\t}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "src_uid": "e80088bee9c0df6adf280f8ffbeaa4a9"} {"nl": {"description": "Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x1,\u2009x2,\u2009...,\u2009xn. Number xi is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save.", "input_spec": "The first line of input contains three space-separated integers n,\u2009a,\u2009b\u00a0(1\u2009\u2264\u2009n\u2009\u2264\u2009105;\u00a01\u2009\u2264\u2009a,\u2009b\u2009\u2264\u2009109). The second line of input contains n space-separated integers x1,\u2009x2,\u2009...,\u2009xn\u00a0(1\u2009\u2264\u2009xi\u2009\u2264\u2009109).", "output_spec": "Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day.", "sample_inputs": ["5 1 4\n12 6 11 9 1", "3 1 2\n1 2 3", "1 1 1\n1"], "sample_outputs": ["0 2 3 1 1", "1 0 1", "0"], "notes": null}, "positive_code": [{"source_code": "#!\\usr\\bin\\perl\nuse POSIX;\n($n,$a,$b) = split(' ',<>);\n@x = split(' ',<>);\nmy @x = map {$_*$a} @x;\nfor (@x) \n{\n push(@y,(($_)/$b));\n}\n\nmy @y = map {floor($_)} @y;\nfor(0..$#x)\n{\n\n $x[$_] -=$y[$_]*$b;\n \n}\nmy @x = map { floor($_/$a)} @x;\nprint \"@x\";\n\n "}], "negative_code": [{"source_code": "#!\\usr\\bin\\perl\nuse POSIX;\n($n,$a,$b) = split(' ',<>);\n@x = split(' ',<>);\n\nfor (@x) \n{\n push(@y,(($_ * $a)/$b));\n}\n\nmy @y = map {floor($_)} @y;\nfor(0..$#x)\n{\n $x[$_] -= ($y[$_] *$b) /$a; \n}\nprint \"@x\";\n\n "}, {"source_code": "#!\\usr\\bin\\perl\nuse POSIX;\n($n,$a,$b) = split(' ',<>);\n@x = split(' ',<>);\n\nfor (@x) \n{\n push(@y,(($_ * $a)/$b));\n}\n\nmy @y = map {floor($_)} @y;\nfor(0..$#x)\n{\n $x[$_] -= ($y[$_] *$b) /$a; \n}\nmy @x = map {ceil($_)} @x;\nprint \"@x\";"}, {"source_code": "#!\\usr\\bin\\perl\nuse POSIX;\n($n,$a,$b) = split(' ',<>);\n@x = split(' ',<>);\n\nfor (@x) \n{\n push(@y,(($_ * $a)/$b));\n}\n\nmy @y = map {floor($_)} @y;\nfor(0..$#x)\n{\n\n $x[$_] -=floor( ( $y[$_]*$b ) /$a);\n \n}\n\nprint \"@x\";\n\n "}], "src_uid": "3c63e2e682d3c8051c3cecc3fa9c4e8c"} {"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\u2009\u2264\u2009|s|\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the instructions Memory is given.", "output_spec": "If there is a string satisfying the conditions, output a single integer\u00a0\u2014 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": "$,=\" \";\nmy $s=<>;\nchomp($s);\nmy %counts;\nfor(0..length($s)-1)\n{\n$counts{substr($s,$_,1)}++;\n}\nmy $diff=0;\n\n$diff = abs($counts{\"R\"}-$counts{\"L\"}) + abs($counts{\"D\"}-$counts{\"U\"});\nif($diff%2==0)\n{\n $diff/=2;\n print $diff . \"\\n\";\n}\nelse\n{\n print \"-1\\n\";\n}\n"}], "negative_code": [], "src_uid": "8237ac3f3c2e79f5f70be1595630207e"} {"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$$$)\u00a0\u2014 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": "<>;print/11.*00/?NO:YES,\"\\n\"while<>;"}, {"source_code": "<>;print/11.*00/?NO:YES,\"\\n\"while<>;"}, {"source_code": "<>;print/11.*00/?NO:YES,\"\\n\"while<>;"}, {"source_code": "<>; print /11.*00/ ? \"NO\\n\" : \"YES\\n\" while <>;"}], "negative_code": [], "src_uid": "357dcc8fb7783d878cd2c4ed34eb437e"} {"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$$$) \u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) \u2014 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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ = <>;\n\tmy $f = 0;\n\t\n\t0 while s/\\b(\\d+)\\b\\K \\1\\b//g;\n\t\n\tm/\n\t\t\\b(\\d+)[ ]\n\t\t(\\d+)[ ]\n\t\t(\\d+)\\b\n\t\t(?{ $f ||= $1 > $2 && $2 < $3 })\n\t\t(*F)\n\t\t/x;\n\t\n\tprint $f ? \"NO\" : \"YES\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $f = 0;\n\t\n\tmy @A;\n\tmy $last = -1;\n\t\n\tfor( @_ ){\n\t\tpush @A, $_ if $_ != $last;\n\t\t$last = $_;\n\t\t}\n\t\n\t@_ = @A;\n\t\n\tfor my $i ( 1 .. @_ - 2 ){\n\t\t$_[ $i - 1 ] > $_[ $i ] and $_[ $i ] < $_[ $i + 1 ] and $f = 1;\n\t\t}\n\t\n\tprint $f ? \"NO\" : \"YES\";\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$_ = <>;\r\n\tmy $f = 0;\r\n\t\r\n\t0 while s/\\b(\\d+)\\b \\K\\1\\b//g;\r\n\t\r\n\tm/\r\n\t\t\\b(\\d+)[ ]\r\n\t\t(\\d+)[ ]\r\n\t\t(\\d+)\\b\r\n\t\t(?{ $f ||= $1 > $2 && $2 < $3 })\r\n\t\t(*F)\r\n\t\t/x;\r\n\t\r\n\tprint $f ? \"NO\" : \"YES\";\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $f = 0;\n\t\n\tfor my $i ( 1 .. @_ - 2 ){\n\t\t$_[ $i - 1 ] > $_[ $i ] and $_[ $i ] < $_[ $i + 1 ] and $f = 1;\n\t\t}\n\t\n\tprint $f ? \"NO\" : \"YES\";\n\t}"}], "src_uid": "c35d309dd6a9569ec298e7128aa3fc0e"} {"nl": {"description": "A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters \u00ab+\u00bb and \u00ab1\u00bb into this sequence. For example, sequences \u00ab(())()\u00bb, \u00ab()\u00bb and \u00ab(()(()))\u00bb are regular, while \u00ab)(\u00bb, \u00ab(()\u00bb and \u00ab(()))(\u00bb 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 \u00ab(\u00bb and \u00ab)\u00bb 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": "#!/usr/bin/perl\nforeach (split //, <>){\n if($_ eq '('){ $c++ };\n if($_ eq ')' && $c){ $c--; $r+=2; }\n}\nprintf \"%d\\n\",$r;\n"}], "negative_code": [], "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\u00a0\u2014 are not.", "input_spec": "The first line of input data contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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 \u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $k ) = split;\n\t\n\t$debug and print \"[$n, $k]\";\n\t\n\tmy %h;\n\t\n\t$h{ $_ } ++ for <> =~ /./g;\n\t\n\t$debug and print join ' ', map { $_ x $h{ $_ } } sort keys %h;\n\t\n\tmy $ans = 0;\n\t\n\twhile( 1 ){\n\t\t\n\t\t$debug and print join ' ', \"[$ans]\", map { $_ x $h{ $_ } } sort keys %h;\n\t\t\n\t\tmy $pairs = 0;\n\t\tfor my $key ( sort keys %h ){\n\t\t\t$pairs += ( $h{ $key } >> 1 );\n\t\t\t}\n\t\t\n\t\t$debug and print \"pairs:[$pairs]\";\n\t\t\n\t\tif( $pairs >= $k ){\n\t\t\t$ans += 2;\n\t\t\t\n\t\t\tmy $kk = $k;\n\t\t\t\n\t\t\tfor my $key ( sort keys %h ){\n\t\t\t\tmy $has = ( $h{ $key } >> 1 );\n\t\t\t\t\n\t\t\t\t$debug and print \"key:[$key], has:[$has]\";\n\t\t\t\t\n\t\t\t\tif( $has >= $kk ){\n\t\t\t\t\t$h{ $key } -= $kk * 2;\n\t\t\t\t\t$kk = 0;\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\t$kk -= $has;\n\t\t\t\t\t$h{ $key } -= $has * 2;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\telse{\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tmy $left = 0;\n\tfor my $key ( sort keys %h ){\n\t\t$left += $h{ $key };\n\t\t}\n\t\t\n\tif( $left >= $k ){\n\t\t$ans ++;\n\t\t}\n\t\n\tprint $ans;\n\t}"}], "negative_code": [], "src_uid": "ca817fe0a97e2d8a6bcfcb00103b6b6d"} {"nl": {"description": "Masha meets a new friend and learns his phone number\u00a0\u2014 $$$s$$$. She wants to remember it as soon as possible. The phone number\u00a0\u2014 is a string of length $$$m$$$ that consists of digits from $$$0$$$ to $$$9$$$. The phone number may start with 0.Masha already knows $$$n$$$ phone numbers (all numbers have the same length $$$m$$$). It will be easier for her to remember a new number if the $$$s$$$ is represented as segments of numbers she already knows. Each such segment must be of length at least $$$2$$$, otherwise there will be too many segments and Masha will get confused.For example, Masha needs to remember the number: $$$s = $$$ '12345678' and she already knows $$$n = 4$$$ numbers: '12340219', '20215601', '56782022', '12300678'. You can represent $$$s$$$ as a $$$3$$$ segment: '1234' of number one, '56' of number two, and '78' of number three. There are other ways to represent $$$s$$$.Masha asks you for help, she asks you to break the string $$$s$$$ into segments of length $$$2$$$ or more of the numbers she already knows. If there are several possible answers, print any of them.", "input_spec": "The first line of input data contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014the number of test cases. Before each test case there is a blank line. Then there is a line containing integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 10^3$$$)\u00a0\u2014the number of phone numbers that Masha knows and the number of digits in each phone number. Then follow $$$n$$$ line, $$$i$$$-th of which describes the $$$i$$$-th number that Masha knows. The next line contains the phone number of her new friend $$$s$$$. Among the given $$$n+1$$$ phones, there may be duplicates (identical phones). It is guaranteed that the sum of $$$n \\cdot m$$$ ($$$n$$$ multiplied by $$$m$$$) values over all input test cases does not exceed $$$10^6$$$.", "output_spec": "You need to print the answers to $$$t$$$ test cases. The first line of the answer should contain one number $$$k$$$, corresponding to the number of segments into which you split the phone number $$$s$$$. Print -1 if you cannot get such a split. If the answer is yes, then follow $$$k$$$ lines containing triples of numbers $$$l, r, i$$$. Such triplets mean that the next $$$r-l+1$$$ digits of number $$$s$$$ are equal to a segment (substring) with boundaries $$$[l, r]$$$ of the phone under number $$$i$$$. Both the phones and the digits in them are numbered from $$$1$$$. Note that $$$r-l+1 \\ge 2$$$ for all $$$k$$$ lines.", "sample_inputs": ["5\n\n4 8\n12340219\n20215601\n56782022\n12300678\n12345678\n\n2 3\n134\n126\n123\n\n1 4\n1210\n1221\n\n4 3\n251\n064\n859\n957\n054\n\n4 7\n7968636\n9486033\n4614224\n5454197\n9482268"], "sample_outputs": ["3\n1 4 1\n5 6 2\n3 4 3\n-1\n2\n1 2 1\n2 3 1\n-1\n3\n1 3 2\n5 6 3\n3 4 1"], "notes": "NoteThe example from the statement.In the second case, it is impossible to represent by segments of known numbers of length 2 or more.In the third case, you can get the segments '12' and '21' from the first phone number."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse re 'eval';\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tfor my $i ( 1 .. $n ){\n\t\t$_ = <>;\n\t\t\n\t\tm!\n\t\t\t...?\n\t\t\t(?{ $h{ $& } //= [ $-[0] + 1, $+[0], $i ] })\n\t\t\t(*FAIL)\n\t\t\t!x;\n\t\t}\n\t\n\t$_ = <>, chomp;\n\t\n\tmy $re = join '|', sort keys %h;\n\t\n\t@_ = ();\n\t\n\tm/^\n\t\t(?:\n\t\t($re)(?{ push @_, $1 })|\n\t\t(?{ pop @_ })(*FAIL)\n\t\t)+\n\t\t$\n\t\t(?{ push @_, $1 })\n\t\t/x ?\n\t\tdo { print for 0 + @_, map \"@{ $h{ $_ } }\", @_ }\n\t\t:\n\t\tdo { print -1 };\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse re 'eval';\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tfor my $i ( 1 .. $n ){\n\t\t$_ = <>;\n\t\t\n\t\tfor my $k ( 2 .. 3 ){\n\t\t\tfor my $j ( 0 .. $m - $k ){\n\t\t\t\t$h{ substr $_, $j, $k } //= [ $j + 1, $j + $k, $i ];\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t### 2-3x slower, gets TLE:\n\t#\n\t#\tm/\n\t#\t\t...?\n\t#\t\t(?{ $h{ $& } //= [ $-[0] + 1, $+[0], $i ] })\n\t#\t\t(*FAIL)\n\t#\t\t/x;\n\t\t\n\t\t}\n\t\n\t$_ = <>, chomp;\n\t\n\tmy $re = join '|', sort keys %h;\n\t\n\t@_ = ();\n\t\n\tm/^\n\t\t(?:\n\t\t($re)(?{ push @_, $1 })|\n\t\t(?{ pop @_ })(*FAIL)\n\t\t)+\n\t\t$\n\t\t(?{ push @_, $1 })\n\t\t/x ?\n\t\tdo { print for 0 + @_, map \"@{ $h{ $_ } }\", @_ }\n\t\t:\n\t\tdo { print -1 };\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse re 'eval';\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nmy @ans;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split ' ', <>;\n\t\n\t@_ = ();\n\tmy %h;\n\t\n\tfor my $i ( 1 .. $n ){\n\t\t$_ = <>;\n\t\t\n\t#\tm/\n\t#\t\t...?\n\t#\t\t(?{ $h{ $& } = [ $-[0] + 1, $+[0], $i ] })\n\t#\t\t(*FAIL)\n\t#\t\t/x;\n\t\t\n\t\tfor my $j ( 0 .. $m - 2 ){\n\t\t\t$h{ substr $_, $j, 2 } = [ $j + 1, $j + 2, $i ];\n\t\t\t}\n\t\t\n\t\tfor my $j ( 0 .. $m - 3 ){\n\t\t\t$h{ substr $_, $j, 3 } = [ $j + 1, $j + 3, $i ];\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"$_ -> [@{ $h{ $_ } }]\" for sort keys %h;\n\t\n\t$_ = <>, chomp;\n\t\n\tmy $re = join '|', sort keys %h;\n\t\n\tm/^\n\t\t(?:\n\t\t($re)(?{ push @_, $1 })|\n\t\t(?{ pop @_ })(*FAIL)\n\t\t)+\n\t\t$\n\t\t(?{ push @_, $1 })\n\t\t/x ?\n\t\tdo { push @ans, $_ for 0 + @_, map \"@{ $h{ $_ } }\", @_ }\n\t\t:\n\t\tdo { push @ans, -1 };\n\t}\n\nprint join \"\\n\", @ans,"}], "negative_code": [], "src_uid": "a4f4ad94387b16e8760e7c7fd45b93b3"} {"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$$$) \u2014 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 \u2014 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": "while (<>) {\n\t/(B*)B(\\1)/ && do {\n\t\tprint $. - 1 + length $1, \" $-[2]\";\n\t\texit;\n\t}\n}\n"}], "negative_code": [], "src_uid": "524273686586cdeb7827ffc1ad59d85a"} {"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\u2009\u2264\u2009|s|\u2009\u2264\u20091000), consisting of lowercase English characters only. The second line contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000).", "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": "#!/bin/perl -w\nuse strict;\nuse warnings;\nuse List::Util qw[reduce max];\nuse POSIX;\n\nmy $s = <>;\nchomp $s;\nmy $n = int <>;\nmy %letters;\nfor (split //, $s) {\n if (defined $letters{$_}) {\n\t$letters{$_}++;\n } else {\n\t$letters{$_} = 1;\n }\n}\n\nif (keys %letters > $n) {\n print -1;\n exit;\n} else {\n my $string = '';\n my %used;\n my %left = %letters;\n while (keys %letters < $n) {\n\tmy $k = reduce {\n\t $left{$a} > $left{$b} ? $a : $b \n\t} keys %letters;\n\t$string .= $k;\n\tif (defined $used{$k}) {\n\t $used{$k}++;\n\t} else {\n\t $used{$k} = 1;\n\t}\n\t$left{$k} = ceil ($letters{$k} / (1 + $used{$k}));\n\t$n--;\n }\n my $stickers = max values %left;\n map { $string .= $_ } keys %left;\n print \"$stickers\\n$string\\n\";\n}\n"}], "negative_code": [{"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\nuse List::Util qw[reduce max];\n\nmy $s = <>;\nchomp $s;\nmy $n = int <>;\nmy %letters;\nfor (split //, $s) {\n if (defined $letters{$_}) {\n\t$letters{$_}++;\n } else {\n\t$letters{$_} = 1;\n }\n}\n\nif (keys %letters > $n) {\n print -1;\n exit;\n} else {\n my $string = '';\n while (keys %letters < $n) {\n\tmy $k = reduce {\n\t $letters{$a} > $letters{$b} ? $a : $b \n\t} keys %letters;\n\t$string .= $k;\n\t$letters{$k} = $letters{$k} >> 1 + $letters{$k} % 2;\n\t$n--;\n }\n my $stickers = max values %letters;\n map { $string .= $_ } keys %letters;\n print \"$stickers\\n$string\\n\";\n}\n"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\nuse List::Util qw[reduce max];\n\nmy $s = <>;\nchomp $s;\nmy $n = int <>;\nmy %letters;\nfor (split //, $s) {\n if (defined $letters{$_}) {\n\t$letters{$_}++;\n } else {\n\t$letters{$_} = 1;\n }\n}\n\nif (keys %letters > $n) {\n print -1;\n exit;\n} else {\n my $string = '';\n while (keys %letters < $n) {\n\tmy $k = reduce {\n\t $letters{$a} > $letters{$b} ? $a : $b \n\t} keys %letters;\n\t$string .= $k;\n\t$letters{$k} = $letters{$k} % 2 + $letters{$k} >> 1;\n\t$n--;\n }\n my $stickers = max values %letters;\n map { $string .= $_ } keys %letters;\n print \"$stickers\\n$string\\n\";\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\u2009\u2264\u2009n\u2009<\u2009300; n is odd). Each of the next n lines contains n small English letters \u2014 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/perl\nuse v5.10;\n\nchomp($n = <>);\npush @a, [split //, <>] foreach (0 .. $n-1);\n$ch = $a[0][0];\n$a[$_][$_] ne $ch and say \"NO\" and exit foreach(0 .. $n-1);\n$a[$_][$n-1-$_] ne $ch and say \"NO\" and exit foreach(0 .. $n-1);\n$a[1][0] eq $ch and say \"NO\" and exit;\n$ch = $a[1][0];\nforeach $i (0 .. $n-1) {\n\tforeach $j (0 .. $n-1) {\n\t\t($j==$i || $j==$n-1-$i) and next;\n\t\t$a[$i][$j] ne $ch and say \"NO\" and exit;\n\t}\n}\nsay \"YES\";"}, {"source_code": "$n=<>; @_=<>;\n($L,$M)=$_[0]=~/(.)(.)/;\n$L ne $M or $f=1;\nfor $i(1..$n){\n\t$j= $i > $n-$i+1 ? $n-$i : $i-1 ;\n\t$_= $M x $j . $L . $M x ($n-$j*2-2) . $L . $M x $j ;\n\ts/$L+/$L/;\n\t$_[$i-1]=~/$_/ or $f=1;\n\t}\nprint $f? \"NO\" : \"YES\""}, {"source_code": "sub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n\nwhile(<>){\n\t\n\tchomp;\n\t$n=$_;\n\tchomp($first_line=<>);\n\t$first_line =~ /(.)(.)/;\n\t$L1=$1;\n\t$L2=$2;\n\t\n\t$f=0;\n\t@_=($first_line);\n\tfor $i(1..$n-1){\n\t\t$_=<>;\n\t\tchomp;\n\t\tpush @_, $_;\n\t\t\n\t\t}\n\t\t\n\tfor $i(1..$n){\n\t\t$j= min ($i, $n-$i+1);\n\t\t$m=($L2 x ($j-1) . $L1 . $L2 x ($n-$j*2) . $L1 . $L2 x ($j-1));\n\t\t$m=~s/($L1){2}/$1/;\n\t\t$_[$i-1] eq $m or $f=1;\n#\t\tprint $m;\n\t#\tprint \" \";\n\t#\tprint \"$_[$i-1]\\n\"\n\t\t}\n\t\n\tprint $f? \"NO\\n\" : \"YES\\n\" ;\n\t\n\t\n\t\n\t}\n\t\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\npush @a, [split //, <>] foreach (0 .. $n-1);\n$ch = $a[0][0];\n$a[$_][$_] ne $ch and say \"NO\" and exit foreach(0 .. $n-1);\n$a[$_][$n-1-$_] ne $ch and say \"NO\" and exit foreach(0 .. $n-1);\n$ch = $a[1][0];\nforeach $i (0 .. $n-1) {\n\tforeach $j (0 .. $n-1) {\n\t\t($j==$i || $j==$n-1-$i) and next;\n\t\t$a[$i][$j] ne $ch and say \"NO\" and exit;\n\t}\n}\nsay \"YES\";"}, {"source_code": "$n=<>; @_=<>;\n($L,$M)=$_[0]=~/(.)(.)/;\nfor $i(1..$n){\n\t$j= $i > $n-$i+1 ? $n-$i : $i-1 ;\n\t$_= $M x $j . $L . $M x ($n-$j*2-2) . $L . $M x $j ;\n\ts/$L+/$L/;\n\t$_[$i-1]=~/$_/ or $f=1;\n\t}\nprint $f? \"NO\" : \"YES\""}], "src_uid": "02a9081aed29ea92aae13950a6d48325"} {"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$$$) \u2014 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}$$$) \u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($tc) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\nwhile( $tc -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/o,);\r\n my @s1 = split(//,scalar());\r\n my @s2 = split(//,scalar());\r\n my $r = 0;\r\n foreach my $i (0..($n-1)){\r\n if( $s1[$i] eq '0' and $s2[$i] eq '1' ){\r\n $r++;\r\n $s2[$i] = '0';\r\n }\r\n }\r\n foreach my $i (0..($n-1)){\r\n if( $i > 0 and $s1[$i-1] eq '1' and $s2[$i] eq '1' ){\r\n $r++;\r\n $s1[$i-1] = '0';\r\n $s2[$i] = '0';\r\n }\r\n if( $i < $n-1 and $s1[$i+1] eq '1' and $s2[$i] eq '1' ){\r\n $r++;\r\n $s1[$i+1] = '0';\r\n $s2[$i] = '0';\r\n }\r\n }\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [], "src_uid": "c05426881a7dccc1aa79608b612290a7"} {"nl": {"description": "Phoenix has collected $$$n$$$ pieces of gold, and he wants to weigh them together so he can feel rich. The $$$i$$$-th piece of gold has weight $$$w_i$$$. All weights are distinct. He will put his $$$n$$$ pieces of gold on a weight scale, one piece at a time. The scale has an unusual defect: if the total weight on it is exactly $$$x$$$, it will explode. Can he put all $$$n$$$ gold pieces onto the scale in some order, without the scale exploding during the process? If so, help him find some possible order. Formally, rearrange the array $$$w$$$ so that for each $$$i$$$ $$$(1 \\le i \\le n)$$$, $$$\\sum\\limits_{j = 1}^{i}w_j \\ne x$$$.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\le n \\le 100$$$; $$$1 \\le x \\le 10^4$$$)\u00a0\u2014 the number of gold pieces that Phoenix has and the weight to avoid, respectively. The second line of each test case contains $$$n$$$ space-separated integers $$$(1 \\le w_i \\le 100)$$$\u00a0\u2014 the weights of the gold pieces. It is guaranteed that the weights are pairwise distinct.", "output_spec": "For each test case, if Phoenix cannot place all $$$n$$$ pieces without the scale exploding, print NO. Otherwise, print YES followed by the rearranged array $$$w$$$. If there are multiple solutions, print any.", "sample_inputs": ["3\n3 2\n3 2 1\n5 3\n1 2 3 4 8\n1 5\n5"], "sample_outputs": ["YES\n3 2 1\nYES\n8 1 2 3 4\nNO"], "notes": "NoteIn the first test case, Phoenix puts the gold piece with weight $$$3$$$ on the scale first, then the piece with weight $$$2$$$, and finally the piece with weight $$$1$$$. The total weight on the scale is $$$3$$$, then $$$5$$$, then $$$6$$$. The scale does not explode because the total weight on the scale is never $$$2$$$.In the second test case, the total weight on the scale is $$$8$$$, $$$9$$$, $$$11$$$, $$$14$$$, then $$$18$$$. It is never $$$3$$$.In the third test case, Phoenix must put the gold piece with weight $$$5$$$ on the scale, and the scale will always explode."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$x) = map { $_ - 0 } split(/\\s+/,);\r\n my @w = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my $s = 0;\r\n my $si = -1;\r\n for(my $i=0;$i<$n;$i++){\r\n $s += $w[$i];\r\n if( $s == $x ){\r\n $si = $i;\r\n last;\r\n }\r\n }\r\n if( $si >= 0 ){\r\n if( $si == $n-1 ){\r\n print \"NO\\n\"; next;\r\n }\r\n my $tmp = $w[$si];\r\n $w[$si] = $w[$si+1];\r\n $w[$si+1] = $tmp;\r\n }\r\n print \"YES\\n\";\r\n print ( join(' ',@w) . \"\\n\" );\r\n \r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "src_uid": "85383c9e802348a3153e7f98ce278f09"} {"nl": {"description": "Martian scientists explore Ganymede, one of Jupiter's numerous moons. Recently, they have found ruins of an ancient civilization. The scientists brought to Mars some tablets with writings in a language unknown to science.They found out that the inhabitants of Ganymede used an alphabet consisting of two letters, and each word was exactly $$$\\ell$$$ letters long. So, the scientists decided to write each word of this language as an integer from $$$0$$$ to $$$2^{\\ell} - 1$$$ inclusively. The first letter of the alphabet corresponds to zero bit in this integer, and the second letter corresponds to one bit.The same word may have various forms in this language. Then, you need to restore the initial form. The process of doing it is described below.Denote the distance between two words as the amount of positions, in which these words differ. For example, the distance between $$$1001_2$$$ and $$$1100_2$$$ (in binary) is equal to two, as these words have different letters in the second and the fourth positions, counting from left to right. Further, denote the distance between words $$$x$$$ and $$$y$$$ as $$$d(x, y)$$$.Let the word have $$$n$$$ forms, the $$$i$$$-th of which is described with an integer $$$x_i$$$. All the $$$x_i$$$ are not necessarily different, as two various forms of the word can be written the same. Consider some word $$$y$$$. Then, closeness of the word $$$y$$$ is equal to the sum of distances to each of the word forms, i.\u00a0e. the sum $$$d(x_i, y)$$$ over all $$$1 \\le i \\le n$$$.The initial form is the word $$$y$$$ with minimal possible nearness.You need to help the scientists and write the program which finds the initial form of the word given all its known forms. Note that the initial form is not necessarily equal to any of the $$$n$$$ given forms.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 100$$$) \u2014 the number of test cases. The following are descriptions of the test cases. The first line contains two integers $$$n$$$ and $$$\\ell$$$ ($$$1 \\le n \\le 100$$$, $$$1 \\le \\ell \\le 30$$$) \u2014 the amount of word forms, and the number of letters in one word. The second line contains $$$n$$$ integers $$$x_i$$$ ($$$0 \\le x_i \\le 2^\\ell - 1$$$) \u2014 word forms. The integers are not necessarily different.", "output_spec": "For each test, print a single integer, the initial form of the word, i.\u00a0e. such $$$y$$$ ($$$0 \\le y \\le 2^\\ell - 1$$$) that the sum $$$d(x_i, y)$$$ over all $$$1 \\le i \\le n$$$ is minimal possible. Note that $$$y$$$ can differ from all the integers $$$x_i$$$. If there are multiple ways to restore the initial form, print any.", "sample_inputs": ["7\n3 5\n18 9 21\n3 5\n18 18 18\n1 1\n1\n5 30\n1 2 3 4 5\n6 10\n99 35 85 46 78 55\n2 1\n0 1\n8 8\n5 16 42 15 83 65 78 42"], "sample_outputs": ["17\n18\n1\n1\n39\n0\n2"], "notes": "NoteIn the first test case, the words can be written as $$$x_1 = 10010_2$$$, $$$x_2 = 01001_2$$$ and $$$x_3 = 10101_2$$$ in binary. Let $$$y = 10001_2$$$. Then, $$$d(x_1, y) = 2$$$ (the difference is in the fourth and the fifth positions), $$$d(x_2, y) = 2$$$ (the difference is in the first and the second positions), $$$d(x_3, y) = 1$$$ (the difference is in the third position). So, the closeness is $$$2 + 2 + 1 = 5$$$. It can be shown that you cannot achieve smaller closeness.In the second test case, all the forms are equal to $$$18$$$ ($$$10010_2$$$ in binary), so the initial form is also $$$18$$$. It's easy to see that closeness is equal to zero in this case."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nmy $W = 31;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $l ) = split;\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[$n, $l]\";\n\t$debug and print \"[@_]\";\n\t\n\tmap { $_ = sprintf \"%0${W}b\", $_ } @_;\n\t\n\t$debug and print \" $_\" for @_;\n\t\n\tmy $ans;\n\t\n\tfor my $i ( 0 .. $W - 1 ){\n\t\tmy %h;\n\t\t\n\t\tfor my $j ( 0 .. @_ - 1 ){\n\t\t\t$h{ substr $_[ $j ], $i, 1 } ++;\n\t\t\t}\n\t\t\n\t\t$ans .= ( sort { $h{ $b } <=> $h{ $a } } keys %h )[ 0 ];\n\t\t}\n\t\n\tprint eval \"0b$ans\";\n\t}"}], "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\u2009\u2264\u2009si\u2009\u2264\u20094), 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of groups of schoolchildren. The second line contains a sequence of integers s1,\u2009s2,\u2009...,\u2009sn (1\u2009\u2264\u2009si\u2009\u2264\u20094). The integers are separated by a space, si is the number of children in the i-th group.", "output_spec": "Print the single number \u2014 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": "#!/usr/bin/perl\n\nuse 5.012;\nuse warnings;\nuse integer;\n\nmy %val;\nchomp(my $i = <>);\nchomp(my $line = <>);\n$val{$_} = 0 for 1..3;\n$val{$_}++ for split / /, $line;\n\nmy $cnt = $val{4};\nif ($val{1} > $val{3}) {\n\t$val{1} -= $val{3};\n\t$cnt += $val{3};\n\t$val{3} = 0;\n}\nelse {\n\t$cnt += $val{1};\n\t$val{3} -= $val{1};\n\t$cnt += $val{3};\n\t$val{1} = 0;\n}\n$cnt += $val{2} / 2;\n$val{2} = $val{2} % 2;\nif ($val{2}) {\n\t$val{1} -= 2;\n\t$cnt++;\n}\nif ($val{1} > 0) {\n\t$cnt += $val{1} / 4;\n\t$cnt++ if $val{1} % 4;\n}\nprint $cnt;\n"}, {"source_code": "my $n = <>;\nmy @arr = split \" \", <>;\nmy $l = scalar @arr - 1;\nmy $ans = 0;\n@arr = sort @arr;\nwhile (scalar @arr > 1) {\n # if ($arr[-1] >= 4) {\n # $ans += int(($arr[-1] + 3) / 4);\n # pop @arr;\n # }\n # else {\n if ($arr[-1] + $arr[0] <= 4) {\n push @arr, shift(@arr) + pop @arr;\n }\n else {\n $ans += int(($arr[-1] +3) / 4);\n pop @arr;\n }\n # }\n}\n$ans+= int(($arr[0]+3)/4) if scalar @arr;\nprint $ans . \"\\n\";"}, {"source_code": "my $n = <>;\nmy @arr = split \" \", <>;\nmy $l = scalar @arr - 1;\nmy $ans = 0;\n@arr = sort @arr;\nwhile (scalar @arr > 1) {\n if ($arr[-1] >= 4) {\n $ans += int(($arr[-1] + 3) / 4);\n # print \"+ $arr[-1]\\n\";\n pop @arr;\n }\n else {\n if ($arr[-1] + $arr[0] <= 4) {\n # print \"- $arr[-1] $arr[0]\\n\";\n push @arr, shift(@arr) + pop @arr;\n }\n else {\n $ans += int(($arr[-1] +3) / 4);\n # print \"* $arr[-1]\\n\";\n pop @arr;\n }\n }\n # print scalar @arr . \"\\n\";\n}\n$ans+= int(($arr[0]+3)/4) if scalar @arr;\nprint $ans . \"\\n\";"}, {"source_code": "use strict;\nuse List::Util qw(max);\n\nmy $n = <>;\nmy @a;\n\n++$a[$_] for(split / /, <>);\n\nmy $res = $a[4] + $a[3] + int(($a[2] + 1) / 2);\nmy $x = $a[1] - $a[3] - 2 * ($a[2] % 2);\n$res += int(($x + 3) / 4) if ($x > 0);\n\nprint $res;"}, {"source_code": "#!/usr/bin/perl\n# 158 B\n\nuse warnings;\nuse strict;\nuse POSIX;\n\nmy $n = ;\nmy @groups = split(\" \", );\nmy ($one, $two, $three, $four);\nmy $ret = 0;\n\nforeach(@groups){\n if($_==1){\n $one++;\n }elsif($_==2){\n $two++;\n }elsif($_==3){\n $three++;\n }else{\n $four++;\n }\n}\n\n# groups of 4\n$ret += $four;\n$four= 0;\n\n# groups of 3\nif($three<=$one){\n $ret += $three;\n $one -= $three;\n $three=0;\n} else {\n $ret += $one;\n $three -= $one;\n $ret += $three;\n $three = 0;\n $one = 0;\n}\n\n# groups of 2\nmy $div = floor($two/2);\nmy $rem = $two % 2;\n\n$ret += $div;\n$two = $rem; #at this point $two will be 0 or 1\n\nif($two == 1){\n $one -= 2;\n $ret++;\n}\n\n$ret += ceil($one/4);\nprint $ret;"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse integer;\n\nchomp($n = <>);\nchomp;\n@c = (0) x 5;\n$c[$_]++ foreach (split / /, <>);\n$ans = $c[4];\n$ans += $c[3];\n$c[1] -= $c[3]>$c[1] ? $c[1]:$c[3];\n$ans += ($c[2] + 1) / 2;\n$c[1] -= $c[2]%2*2>$c[1] ? $c[1]:$c[2]%2*2;\n$ans += ($c[1] + 3) / 4;\nprint $ans;\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nchomp($n);\n\ndie('n to big') unless $n >= 1 && $n <= 100000;\n\nmy $line = <>;\nchomp($line);\nmy @in = split(' ', $line);\n\nmy $result = 0;\n\n# calculate how many groups of each size in stop one. so for example:\n# 1 2 4 3 3 goes into\n# 1: 1\n# 2: 1\n# 3: 2\n# 4: 1\n\n#remove 4 from equation since its a corner case\n#possible compbis per number:\n# 3: only go with 1...\n# 2: can go with 2 and 1\n# 1: can go with 3, 2, 1\n#we see higher number is more restrictive. therefor lest start to solve backwards\n# try to generalisation:\n# x: 4-x.... test. 3: 4-3=1 OK. 2: 4-2=2 OK. 1\n\n\n\nmy %groups;\nfor (@in) {\n die('1-4...') unless $_ >= 1 && $_ <= 4;\n $groups{$_}++;\n}\nmy $taxies = 0;\n\nwhile (grep { $_ >= 1 } values %groups) {\n if ($groups{4}) {\n $groups{4}--;\n $taxies++;\n } elsif($groups{3}) {\n $groups{3}--;\n if ($groups{1}) {\n $groups{1}--;\n }\n $taxies++;\n } elsif ($groups{2}) {\n $groups{2}--;\n if ($groups{2}) {\n $groups{2}--;\n } elsif ($groups{1} >= 2) {\n $groups{1} -=2;\n } elsif ($groups{1} >= 1) {\n $groups{1}--;\n }\n $taxies++;\n } elsif ($groups{1}) {\n $groups{1}--;\n if ($groups{1} >= 3) {\n $groups{1} -= 3;\n } elsif($groups{1} >= 2) {\n $groups{1} -= 2;\n } elsif($groups{1} >= 1) {\n $groups{1}--;\n }\n $taxies++;\n } else {\n die('invalid input error...');\n }\n\n}\n\n\nprint $taxies;\n"}, {"source_code": "$DEBUG = 0;\nchomp($n = );\nchomp($line = );\n\n%g = {};\nmap { $g{$_} = 0; } 1..4;\nforeach(split /\\s/, $line) {\n\t$g{$_}++;\n}\n$taxis = $g{4};\nprint \"$taxis groups of 4\\n\" if $DEBUG;\nif($g{3} <= $g{1}) {\n\tprint $g{3}.\" groups of 3+1\\n\" if $DEBUG;\n\t$taxis += $g{3};\n\t$g{1} -= $g{3};\n\t$g{3} = 0;\n} else {\n\t$taxis += $g{1};\n\tprint $g{1}.\" groups of 3+1\\n\" if $DEBUG;\n\t$g{3} -= $g{1};\n\t$g{1} = 0;\n}\nif($g{3} > 0) { $taxis += $g{3}; print $g{3}.\" groups of 3\\n\" if $DEBUG; }\nwhile($g{2} > 1) {\n\t$taxis++;\n\t$g{2} -= 2;\n\tprint \"A group of 2+2\\n\" if $DEBUG;\n}\nif($g{2}>0) {\n\t$taxis++;\n\t$g{1} -= 2;\n\tprint \"A group of 2+1+1, \".$g{1}.\" singles left\\n\" if $DEBUG;\n}\nwhile($g{1}>0) {\n\t$taxis++;\n\t$g{1} -= 4;\n\tprint \"A group of 4 singles\\n\" if $DEBUG;\n}\nprint \"$taxis\\n\";"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @s = split ' ', <>;\nmy %cnt;\nfor my $x (@s){\n\t$cnt{$x}++;\n}\nmy $res = $cnt{4};\n\nwhile(1) {\n\tif ($cnt{3}) {\n\t\t$res++;\n\t\t$cnt{3}--;\n\t\t$cnt{1}-- if $cnt{1};\n\t\tnext;\n\t}\n\n\tif ($cnt{2}) {\n\t\t$res++;\n\t\t$cnt{2}--;\n\t\tif ($cnt{2}) {\n\t\t\t$cnt{2}--;\n\t\t} else {\n\t\t\t$cnt{1}-- if $cnt{1};\n\t\t\t$cnt{1}-- if $cnt{1};\n\t\t}\n\t\tnext;\n\t}\n\n\tif ($cnt{1}) {\n\t\t$res++;\n\t\t$cnt{1}--;\n\t\t$cnt{1}-- if $cnt{1};\n\t\t$cnt{1}-- if $cnt{1};\n\t\t$cnt{1}-- if $cnt{1};\n\t\tnext;\t\t\t\t\n\t}\t\n\n\tlast;\n}\n\nprint \"$res\\n\";\n"}, {"source_code": "$\\=\"\\n\";\n$n=;\n\n$/=\" \";\nmy $cabs=0;\nmy @passGroups=(0,0,0,0,0);\n\nfor ($i=1;$i<$n;$i++){\n $passGroups[]+=1;\n}\n$/=\"\\n\";\n$passGroups[]+=1;\n#print \"\\n\", @passGroups;\n\n#print \"1) \".$cubs;\n\n#print \" passGroups[1]= $passGroups[1]\";\n#print \" passGroups[2]= $passGroups[2]\";\n#print \" passGroups[3]= $passGroups[3]\";\n#print \" passGroups[4]= $passGroups[4]\";\n\n$cubs+=$passGroups[4];\n#print \"12) \".$cubs;\n#print \" passGroups[1]= $passGroups[1]\";\n#print \" passGroups[2]= $passGroups[2]\";\n#print \" passGroups[3]= $passGroups[3]\";\n#print \" passGroups[4]= $passGroups[4]\";\n\nif ($passGroups[1]<=$passGroups[3]){\n $cubs+=$passGroups[3];\n $passGroups[1]=0;\n}\n\n\nif ($passGroups[1]>$passGroups[3]){\n $passGroups[1]-=$passGroups[3];\n $cubs+=$passGroups[3];\n}\n\nif ($passGroups[2]%2!=0){\n $cubs+=1;\n $passGroups[2]-=1;\n if ($passGroups[1]>1){\n $passGroups[1]-=2;\n }\n if ($passGroups[1]==1){\n $passGroups[1]-=1;\n }\n \n}\n $cubs+=$passGroups[2]/2;\nif ($passGroups[1]%4!=0){\n $cubs+=int($passGroups[1]/4) +1;\n}else{\n $cubs+=int($passGroups[1]/4);\n}\n\n\nprint $cubs;\n\n\n"}, {"source_code": "$\\=\"\\n\";\n$n=;\nmy $cabs=0;\nmy @passGroups=(0,0,0,0,0);\n\n@passs=split(\" \",);\n\nforeach $i (@passs){\n $passGroups[$i]+=1;\n #print $i;\n}\n\n$cubs+=$passGroups[4];\n\nif ($passGroups[1]<=$passGroups[3]){\n $cubs+=$passGroups[3];\n $passGroups[1]=0;\n}\n\n\nif ($passGroups[1]>$passGroups[3]){\n $passGroups[1]-=$passGroups[3];\n $cubs+=$passGroups[3];\n}\n\nif ($passGroups[2]%2!=0){\n $cubs+=1;\n $passGroups[2]-=1;\n if ($passGroups[1]>1){\n $passGroups[1]-=2;\n }\n if ($passGroups[1]==1){\n $passGroups[1]-=1;\n }\n \n}\n $cubs+=$passGroups[2]/2;\nif ($passGroups[1]%4!=0){\n $cubs+=int($passGroups[1]/4) +1;\n}else{\n $cubs+=int($passGroups[1]/4);\n}\n\n\nprint $cubs;\n\n\n"}, {"source_code": "; @_= sort {$b <=> $a} (split \" \", ); my $res = 0; my $sym = 0; for(@_){ if($_ == 4){ $res += 1; }elsif($_ == 3) { if( $_[-1] == 1 ){ pop @_ } $res += 1; }else{ $sym += $_; } } $sym = $sym/4; print $res + ($sym == int($sym) ? $sym : int($sym)+1), \"\\n\";"}, {"source_code": "$n = <>;\n@a = split' ', <>;\n$one = 0; $two = 0; $three = 0; $ans = 0;\nfor (my $i = 0; $i < $n; ++$i) {\n if ($a[$i] == 4) { ++$ans; }\n if ($a[$i] == 3) { ++$three; }\n if ($a[$i] == 2) { ++$two; }\n if ($a[$i] == 1) { ++$one; }\n}\nif ($three <= $one) { \n $one -= $three;\n $ans += $three;\n}\nelse {\n $ans += $one;\n $three -= $one;\n $ans += $three;\n $one = 0;\n}\n$ans += int ($two / 2);\n$two = $two % 2;\n\nif ($two == 1) {\n $one -= 2;\n ++$ans;\n}\n\n$ans += int (($one + 3) / 4);\nprint $ans;"}, {"source_code": "#!/bin/perl\n\n$n = ;\n@groups = split / +/, ;\n\n@zlicz = (0, 0, 0, 0, 0);\n\nfor($i=0;$i<$n;$i++)\n{\n\n @zlicz[ @groups[$i] ]++;\n\n}\n\n$wynik = @zlicz[4];\n@zlicz[4] = 0;\n\nif( @zlicz[3] != 0 )\n{\n\n if( @zlicz[3] > @zlicz[1] )\n {\n\n $m = @zlicz[1];\n\n }\n else\n {\n\n $m = @zlicz[3];\n\n }\n\n $wynik += $m;\n @zlicz[1] -= $m;\n @zlicz[3] -= $m;\n\n $wynik += @zlicz[3];\n @zlicz[3] = 0;\n\n}\n\nif( @zlicz[2] > int(@zlicz[1]/2) )\n{\n\n $m = int(@zlicz[1]/2);\n\n}\nelse\n{\n\n $m = @zlicz[2];\n\n}\n\n$wynik += $m;\n@zlicz[1] -= $m * 2;\n@zlicz[2] -= $m;\n\n$wynik += int( @zlicz[2]/2 );\n@zlicz[2] -= int( @zlicz[2]/2 ) * 2;\n\nif( @zlicz[2] != 0 )\n{\n\n if( @zlicz[1] != 0 )\n {\n\n $wynik++;\n\n @zlicz[2] = 0;\n @zlicz[1] -= 2;\n\n if( @zlicz[1] < 0 )\n {\n\n @zlicz[1] = 0;\n\n }\n\n }\n\n $wynik += @zlicz[2];\n @zlicz[2] = 0;\n\n}\n\n$wynik += int( @zlicz[1]/4 );\n@zlicz[1] -= int( @zlicz[1]/4 ) * 4;\n\nif( @zlicz[1] != 0 )\n{\n\n $wynik++;\n\n}\n\nprint $wynik;\n"}, {"source_code": "#!/usr/bin/perl\nuse strict; use warnings;\n\nsub ceil { my $j = $_[0]; my $i = int $j; $i < $j ? $i + 1 : $i }\n\nmy ($taxi, $one, $two, $three) = (0, 0, 0, 0); ;\nforeach (split ' ', )\n{ if ($_ == 1) { if ($three) { --$three } else { ++$one; next } }\n elsif ($_ == 2) { if ($two) { --$two } else { ++$two; next } }\n elsif ($_ == 3) { if ($one) { --$one } else { ++$three; next } }\n ++$taxi }\nif ($two) { $one -= $one > 1 ? 2 : $one; ++$taxi }\nprintf \"%d\\n\", $taxi + $three + ceil $one / 4\n"}, {"source_code": "#!/usr/bin/perl\nuse strict; use warnings;\n\nsub ceil { my $j = $_[0]; my $i = int $j; $i < $j ? $i + 1 : $i }\n\nsub input\n{ ; $_ = ; s/\\s//g; my ($n, $m, @a) = length;\n foreach my $i (1..4)\n { s/$i+//g; $m = length; push @a, $n - $m; $n = $m }\n @a }\n\nmy ($one, $two, $three, $four) = input;\n$one -= $one < $three ? $one : $three;\nif ($two % 2) { $one -= $one < 2 ? $one : 2; ++$two }\nprintf \"%d\\n\", $two / 2 + $three + $four + ceil $one / 4\n\n"}, {"source_code": "<>;\n$_=<>;\nchomp;\ns/(\\d+)/$&==4?($f++):($&==3?($t++):($&==2?($w++):($o++)))/eg;\n#print\"$f $t $w $o\";\n\n$ats+=$f;\n\n$ats+=$t; $o-=$t; $o<1 and $o=0;\n\n$ww = ($w - $w%2) / 2; $w %= 2;\n$ats+=$ww;\n\n$oooo = ($o - $o%4) / 4; $o %= 4;\n$ats+=$oooo;\n\n$wo=$w*2+$o;\n$wo>4?($v=2):($wo?($v=1):($v=0));\n$ats+=$v;\n\nprint$ats"}, {"source_code": "use integer;\n\n<>;\n$_=<>;\n@_=(0)x 5;\n$_[$&]++ while /\\d+/g;\n\n($f,$t,$w,$o) = reverse @_;\n\n$ats += $f;\n$ats += $t; $o-=$t; $o<1 and $o=0;\n\n$ats += $w / 2; $w %= 2;\n$ats += $o / 4; $o %= 4;\n\n$wo = $w*2 + $o;\n$ats += --$wo >> 2 <=> 0;\n\nprint $ats + 1"}, {"source_code": "use integer;\n\n<>;\n$_=<>;\n@_=(0)x 5;\n$_[$1]++ while /(\\d)/g;\n\n($f,$t,$w,$o) = reverse @_;\n\n$ats += $f;\n$ats += $t; $o-=$t; $o<1 and $o=0;\n\n$ats += $w / 2; $w %= 2;\n$ats += $o / 4; $o %= 4;\n\n$wo = $w*2 + $o;\n$ats += --$wo >> 2 <=> 0;\n\nprint $ats + 1"}, {"source_code": "$n=<>;\n@l=split(' ',<>);\n$a=0;$b=0;$c=0;$d=0;\nforeach(@l)\n{\nif($_==1){$a+=1;}\nif($_==2){$b+=1;}\nif($_==3){$c+=1;}\nif($_==4){$d+=1;}\n}\n$ans=0;\nif($b%2==0){$ans+=$b/2;$b=0;}\nelse{$ans+=int($b/2);$b=1;}\nif($a>$c){$ans+=$c;$a-=$c;if($b>0){$a-=2;$ans+=1;}}\nelse{$ans+=$c+$b;$a=0;$b=0;}\nif($a>0){$a+=3;$ans+=int($a/4);}\n$ans+=$d;\nprint \"$ans\\n\";\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse 5.012;\nuse warnings;\nuse integer;\n\nchomp(my $i = <>);\nchomp(my $line = <>);\nmy $g = ($line =~ s/ //gr);\nmy $cnt = ($g =~ tr/4/4/);\nmy $threes = ($g =~ tr/3/3/);\nmy $twos = ($g =~ tr/2/2/);\nmy $ones = ($g =~ tr/1/1/);\n$cnt += $threes;\n$ones -= $threes;\n$cnt += $ones if $ones > 0;\n$cnt += $twos / 2;\n$cnt++ if $twos % 2;\nprint $cnt;\n"}, {"source_code": "#!/usr/bin/perl\n\nuse 5.012;\nuse warnings;\nuse integer;\n\nchomp(my $i = <>);\nchomp(my $line = <>);\nmy $g = ($line =~ s/ //gr);\nmy $cnt = ($g =~ tr/4/4/);\nmy $threes = ($g =~ tr/3/3/);\nmy $twos = ($g =~ tr/2/2/);\nmy $ones = ($g =~ tr/1/1/);\n$cnt += $threes;\n$ones -= $threes;\n$ones = 0 if $ones < 0;\n$cnt += $twos / 2;\n$cnt++ if $twos % 2;\n$cnt += $ones / 4;\n$cnt++ if $ones % 4;\nprint $cnt;\n"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse integer;\n\nchomp($n = <>);\nchomp;\n@c = (0) x 5;\n$c[$_]++ foreach (split / /, <>);\n$ans = $c[4];\n$ans += $c[3];\n$c[1] -= $c[3]>$c[1] ? $c[1]:$c[3];\n$ans += ($c[2] + 1) / 2;\n$c[1] -= $c[2]%2>$c[1] ? $c[1]:$c[2]%2;\n$ans += ($c[1] + 3) / 4;\nprint $ans;\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nchomp($n);\n\ndie('n to big') unless $n >= 1 && $n <= 10000;\n\nmy $line = <>;\nchomp($line);\nmy @in = split(' ', $line);\n\nmy $result = 0;\n\n# calculate how many groups of each size in stop one. so for example:\n# 1 2 4 3 3 goes into\n# 1: 1\n# 2: 1\n# 3: 2\n# 4: 1\n\n#remove 4 from equation since its a corner case\n#possible compbis per number:\n# 3: only go with 1...\n# 2: can go with 2 and 1\n# 1: can go with 3, 2, 1\n#we see higher number is more restrictive. therefor lest start to solve backwards\n# try to generalisation:\n# x: 4-x.... test. 3: 4-3=1 OK. 2: 4-2=2 OK. 1\n\n\n\nmy %groups;\nfor (@in) {\n die('1-4...') unless $_ >= 1 && $_ <= 4;\n $groups{$_}++;\n}\n# I want to loop: 4 3 3 2 1\n\n\nfor (my $i = 4; $i >= 1; $i--) {\n while ($groups{$i} >= 1) {\n my $taxi_occupany = $i;\n $groups{$i}--; #remove self from groups\n my $max_friend_group = 4 - $taxi_occupany;;\n\n for (my $j = $max_friend_group; $j >= 1; $j--) {\n if ($groups{$j} > 0) {\n $groups{$j}--;\n $taxi_occupany += $j;\n }\n last if $taxi_occupany >= 4;\n }\n $result++;\n }\n}\n\nprint $result;\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @s = split ' ', <>;\nmy %cnt;\nfor my $x (@s){\n\t$cnt{$x}++;\n}\nmy $res = $cnt{4};\n\nwhile(1) {\n\tif ($cnt{3}) {\n\t\t$res++;\n\t\t$cnt{3}--;\n\t\t$cnt{1}-- if $cnt{1};\n\t\tnext;\n\t}\n\n\tif ($cnt{2}) {\n\t\t$res++;\n\t\t$cnt{2}--;\n\t\tif ($cnt{2}) {\n\t\t\t$cnt{2}--;\n\t\t} else {\n\t\t\t$cnt{1}-- if $cnt{1};\n\t\t\t$cnt{1}-- if $cnt{1};\n\t\t}\n\t\tnext;\n\t}\n\n\tif ($cnt{1}) {\n\t\t$res++;\n\t\t$cnt{1}--;\n\t\t$cnt{1}-- if $cnt{1};\n\t\t$cnt{1}-- if $cnt{1};\n\t\t$cnt{1}-- if $cnt{1};\t\t\t\t\n\t}\t\n\n\tlast;\n}\n\nprint \"$res\\n\";\n"}, {"source_code": "$\\=\"\\n\";\n$n=;\n\n$/=\" \";\nmy $cabs=0;\nmy @passGroups=(0,0,0,0,0);\n\nfor ($i=1;$i<$n;$i++){\n $passGroups[]+=1;\n}\n$/=\"\\n\";\n$passGroups[]+=1;\n\n\n\n\n$cubs+=$passGroups[4];\n\nif ($passGroups[1]<=$passGroups[3]){\n $cubs+=$passGroups[3];\n $passGroups[1]=0;\n}\n\n\n\nif ($passGroups[1]>$passGroups[3]){\n $passGroups[1]-=$passGroups[3];\n $cubs+=$passGroups[3];\n}\n\nif ($passGroups[2]%2!=0){\n $cubs+=1;\n $passGroups[2]-=1;\n if ($passGroups[1]>1){\n $passGroups[1]-=2;\n }\n}\n \n $cubs+=$passGroups[2]/2;\n\nif ($passGroups[1]%4!=0){\n $cubs+=int($passGroups[1]/4) +1;\n}else{\n $cubs+=int($passGroups[1]/4);\n}\n\n\n\n\nprint $cubs;\n\n\n"}, {"source_code": "$n = <>;\n@a = split' ', <>;\n$one = 0; $two = 0; $three = 0; $ans = 0;\nfor (my $i = 0; $i < $n; ++$i) {\n if ($a[$i] == 4) { ++$ans; }\n if ($a[$i] == 3) { ++$three; }\n if ($a[$i] == 2) { ++$two; }\n if ($a[$i] == 1) { ++$one; }\n}\n$ans += $three;\nif ($three < $one) { $one -= $three; }\nelse { $one = 0; }\n\nif ($two % 2 == 1) { $ans += ($two - 1) / 2; }\nelse { $ans += $two / 2; }\n\nif ($two % 2 == 1) {\n ++$ans;\n $one -= 2;\n}\nif ($one > 0) {\n if ($one % 2 != 0) { $ans += ($one + 3) / 4; }\n else { $ans += $one / 4; }\n}\nprint $ans;"}, {"source_code": "$n = <>;\n@a = split' ', <>;\n$one = 0; $two = 0; $three = 0; $ans = 0;\nfor (my $i = 0; $i < $n; ++$i) {\n if ($a[$i] == 4) { ++$ans; }\n if ($a[$i] == 3) { ++$three; }\n if ($a[$i] == 2) { ++$two; }\n if ($a[$i] == 1) { ++$one; }\n}\n$ans += $three;\nif ($three < $one) { $one -= $three; }\nelse { $one = 0; }\n\nif ($two % 2 == 1) { $ans += ($two - 1) / 2; }\nelse { $ans += $two / 2; }\n\nif ($two % 2 == 1) {\n ++$ans;\n $one -= 2;\n}\nif ($one > 4) {\n if ($one % 2 != 0) { $ans += int (($one + 3) / 4); }\n else { $ans += $one / 4; }\n}\nelse { if ($one > 0) {++$ans;} }\nprint $ans;"}, {"source_code": "$n = <>;\n@a = split' ', <>;\n$one = 0; $two = 0; $three = 0; $ans = 0;\nfor (my $i = 0; $i < $n; ++$i) {\n if ($a[$i] == 4) { ++$ans; }\n if ($a[$i] == 3) { ++$three; }\n if ($a[$i] == 2) { ++$two; }\n if ($a[$i] == 1) { ++$one; }\n}\n$ans += $three;\nif ($three < $one) { $one -= $three; }\nelse { $one = 0; }\n\nif ($two % 2 == 1) { $ans += ($two - 1) / 2; }\nelse { $ans += $two / 2; }\n\nif ($two % 2 == 1) {\n ++$ans;\n $one -= 2;\n}\nif ($one > 4) {\n if ($one % 2 != 0) { $ans += int (($one + 3) / 4); }\n else { $ans += $one / 4; }\n}\nelse { ++$ans; }\nprint $ans;"}, {"source_code": "#!/bin/perl\n\n$n = ;\n@groups = split / +/, ;\n\n@zlicz = (0, 0, 0, 0, 0);\n\nfor($i=0;$i<$n;$i++)\n{\n\n @zlicz[ @groups[$i] ]++;\n\n}\n\n$wynik = @zlicz[4];\n@zlicz[4] = 0;\n\nif( @zlicz[3] > @zlicz[1] )\n{\n\n $m = @zlicz[1];\n\n}\nelse\n{\n\n $m = @zlicz[3];\n\n}\n\n$wynik += $m;\n@zlicz[1] -= $m;\n@zlicz[3] -= $m;\n\n$wynik += @zlicz[3];\n@zlicz[3] = 0;\n\nif( @zlicz[2] > int(@zlicz[1]/2) )\n{\n\n $m = int(@zlicz[1]/2);\n\n}\nelse\n{\n\n $m = @zlicz[2];\n\n}\n\n$wynik += $m;\n@zlicz[1] -= $m * 2;\n@zlicz[2] -= $m;\n\n$wynik += int( @zlicz[2]/2 );\n@zlicz[2] -= int( @zlicz[2]/2 ) * 2;\n\n$wynik += @zlicz[2];\n@zlicz[2] = 0;\n\n$wynik += int( @zlicz[1]/4 );\n@zlicz[1] -= int( @zlicz[1]/4 ) * 4;\n\nif( @zlicz[1] != 0 )\n{\n\n $wynik++;\n\n}\n\nprint $wynik;\n"}, {"source_code": "use integer;\n\n<>;\n$_=<>;\n@_=(0)x 5;\n$_[$&]++ while /\\d+/g;\n\n($f,$t,$w,$o) = reverse @_;\n\n$ats += $f;\n$ats += $t; $o-=$t; $o<1 and $o=0;\n\n$ats += $w / 2; $w %= 2;\n$ats += $o / 4; $o %= 4;\n\n$wo = $w*2 + $o;\n$ats += ($wo - 1) / 4 <=> 0;\n\nprint $ats + 1"}, {"source_code": "$n=<>;\n@l=split(' ',<>);\n$a=0;$b=0;$c=0;$d=0;\nforeach(@l)\n{\nif($_==1){$a+=1;}\nif($_==2){$b+=1;}\nif($_==3){$c+=1;}\nif($_==4){$d+=1;}\n}\n$ans=0;\nif($b%2==0){$ans+=$b/2;$b=0;}\nelse{$ans+=int($b/2);$b=1;}\nif($a>$c){$ans+=$a;}\nelse{$ans+=$c+$b;}\n$ans+=$d;\nprint \"$ans\\n\";\n"}, {"source_code": "$n=<>;\n@l=split(' ',<>);\n$a=0;$b=0;$c=0;$d=0;\nforeach(@l)\n{\nif($_==1){$a+=1;}\nif($_==2){$b+=1;}\nif($_==3){$c+=1;}\nif($_==4){$d+=1;}\n}\n$ans=0;\nif($b%2==0){$ans+=$b/2;$b=0;}\nelse{$ans+=int($b/2);$b=1;}\nif($a>$c){$ans+=$c;$a-=$c;if($b>0){$a-=2;}}\nelse{$ans+=$c+$b;$a=0;$b=0;}\nif($a>0){$a+=3;$ans+=int($a/4);}\n$ans+=$d;\nprint \"$ans\\n\";\n"}], "src_uid": "371100da1b044ad500ac4e1c09fa8dcb"} {"nl": {"description": "Imagine that there is a group of three friends: A, B and \u0421. 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\u2009\u2264\u2009n\u2009\u2264\u2009100;\u00a00\u2009\u2264\u2009m\u2009\u2264\u2009104). The next m lines contain the debts. The i-th line contains three integers ai,\u2009bi,\u2009ci (1\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u2009n;\u00a0ai\u2009\u2260\u2009bi;\u00a01\u2009\u2264\u2009ci\u2009\u2264\u2009100), 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,\u2009y) and pair of people (y,\u2009x).", "output_spec": "Print a single integer \u2014 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": "my $nm = ;\nchomp ($nm);\nmy @data = split (\" \", $nm);\nmy $n = shift (@data);\nmy $m = pop (@data);\nif ($m < 2) {\n print $m;\n exit;\n}\nmy $i = 0;\nmy $j = 0;\nmy $s = 0;\nmy $x = 0;\nmy @result;\nwhile ($i != $m ) {\n my $input = ;\n chomp ($input);\n my @temp = split (\" \", $input);\n my $p = shift (@temp);\n push (@poor, $p);\n my $r = shift (@temp);\n push (@rich, $r);\n my $t = shift (@temp);\n push (@total, $t);\n $result[$i] = 0;\n $i++;\n}\nfor (my $i = 0; $i < $n; $i++) {\n for (my $j = 0; $j <$m; $j++) {\n if ($poor[$j] == $i+1) { \n $result[$i] = $result[$i] - $total[$j];\n }\n if ($rich[$j] == $i+1) { \n $result[$i] = $result[$i] + $total[$j];\n }\n }\n}\nfor (my $i = 0; $i < $n; $i++) {\n if ($result[$i] >= 0) {\n $x = $x + $result[$i]; \n }\n}\nprint $x;"}], "negative_code": [{"source_code": "my $nm = ;\nchomp ($nm);\nmy @data = split (\" \", $nm);\nmy $n = shift (@data);\nmy $m = pop (@data);\nif ($m < 2) {\n print $m;\n exit;}\n\nmy $i = 0;\nmy $x = 0;\n\nwhile ($i != $m ) {\n my $input = ;\n chomp ($input);\n my @temp = split (\" \", $input);\n my $p = shift (@temp);\n push (@poor, $p);\n my $r = shift (@temp);\n push (@rich, $r);\n my $t = shift (@temp);\n push (@total, $t);\n $x = $x + $t;\n $i++;\n}\n$i = 0;\n$j = 0;\nwhile ($i != $m) {\n while ($j != $m) {\n if ($poor[$i] == $rich[$j]) {\n if ($total[$i] > $total[$j]) {\n $x = $x - $total[$j];\n } else {\n $x = $x - $total[$i];\n }\n } \n $j++;\n } \n $j = 0;\n $i++;\n}\nprint $x;"}, {"source_code": "my $nm = ;\nchomp ($nm);\nmy @data = split (\" \", $nm);\nmy $n = shift (@data);\nmy $m = pop (@data);\nif ($m < 2) {\n print $m;\n exit;}\n\nmy $i = 0;\nmy $x = 0;\n\nwhile ($i != $m ) {\n my $input = ;\n chomp ($input);\n my @temp = split (\" \", $input);\n my $p = shift (@temp);\n push (@poor, $p);\n my $r = shift (@temp);\n push (@rich, $r);\n my $t = shift (@temp);\n push (@total, $t);\n $x = $x + $t;\n $i++;\n}\n$i = 0;\n$j = 0;\nwhile ($i != $m) {\n while ($j != $m) {\n if ($poor[$i] != $poor[($i-1)]) {\n if ($poor[$i] == $rich[$j]) {\n if ($total[$i] > $total[$j]) {\n $x = $x - $total[$j];\n } else {\n $x = $x - $total[$i];\n }\n } \n } \n $j++;\n } \n $j = 0;\n $i++;\n}\nprint $x;"}], "src_uid": "b53c3e55834db8184d8caf4630aaa573"} {"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 \u2014 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": "$_ = <>;\n/^([01]*)(.*)$/;\n($a, $b) = ($1, $2);\n$a0 = $a =~ y/0/0/;\n$a1 = $a =~ y/1/1/;\n$b1 = $b =~ y/1/1/;\n$b =~ s/1//g;\n$ans = (\"0\" x $a0) . (\"1\" x ($a1 + $b1)) . $b;\nprint $ans, \"\\n\";\n"}], "negative_code": [], "src_uid": "91cefa6793e77b3e4f4896616a164ff2"} {"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$$$)\u00a0\u2014 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$$$\u00a0\u2014 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 ) = split ' ', <>;\n\n$_ = 'o';\n\n$_ .= '-' x ( $k * 2 ) . 'o' while $n > $k * 2 + length;\n\t\nfor $i ( 0, 1 ){\n\t$_ = reverse;\n\t$q = $k;\n\ts/^/-/ while $q -- and $n > length;\n\t}\n\npush @_, \"@+\" while /o/g;\n\nprint @_ . \"\\n@_\""}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\t\n\tmy $R = $k;\n\tmy $L = $k;\n\t\n\t$_ = 'o';\n\t\n\twhile( $n >= $k * 2 + 1 + length ){\n\t\t$_ .= '-' x ( $k * 2 ) . 'o';\n\t\t}\n\t\n\twhile( $R > 0 ){\n\t\tif( $n >= 1 + length ){\n\t\t\t$_ .= '-';\n\t\t\t$R --;\n\t\t\tnext;\n\t\t\t}\n\t\tlast;\n\t\t}\n\t\t\n\twhile( $L > 0 ){\n\t\tif( $n >= 1 + length ){\n\t\t\ts/^/-/;\n\t\t\t$L --;\n\t\t\tnext;\n\t\t\t}\n\t\tlast;\n\t\t}\n\t\n\t@_ = ();\n\t\n\twhile( /o/g ){\n\t\tpush @_, \"@+\";\n\t\t}\n\t\n\tprint for ~~ @_, \"@_\";\n\t}"}], "negative_code": [], "src_uid": "dfd60a02670749c67b0f96df1a0709b9"} {"nl": {"description": "The polar bears are going fishing. They plan to sail from (sx,\u2009sy) to (ex,\u2009ey). 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,\u2009y). If the wind blows to the east, the boat will move to (x\u2009+\u20091,\u2009y). If the wind blows to the south, the boat will move to (x,\u2009y\u2009-\u20091). If the wind blows to the west, the boat will move to (x\u2009-\u20091,\u2009y). If the wind blows to the north, the boat will move to (x,\u2009y\u2009+\u20091). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x,\u2009y). Given the wind direction for t seconds, what is the earliest time they sail to (ex,\u2009ey)?", "input_spec": "The first line contains five integers t,\u2009sx,\u2009sy,\u2009ex,\u2009ey (1\u2009\u2264\u2009t\u2009\u2264\u2009105,\u2009\u2009-\u2009109\u2009\u2264\u2009sx,\u2009sy,\u2009ex,\u2009ey\u2009\u2264\u2009109). 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,\u2009ey) 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": "#!/usr/bin/perl\n# B\n\nwhile(<>){\n $i=$j=0;\n ($n, $sx, $sy, $ex, $ey) = split/ /;\n $dx = $ex - $sx;\n $dy = $ey - $sy;\n $_=<>;\n chomp;\n # $_.=\"Q\";\n while ($_){\n if (s/^N//){if ($dy > 0){ --$dy}}\n elsif (s/^S//){if ($dy < 0){ ++$dy}}\n elsif (s/^E//){if ($dx > 0){ --$dx}}\n elsif (s/^W//){if ($dx < 0){ ++$dx}}\n $i++;\n if (($dy==0) and ($dx==0)) { print ($i,\"\\n\"); ++$j; last }\n \n }\n \n if (!$j){ print \"-1\\n\"}\n # print \"\\n\";\n \n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n# B\n\nwhile(<>){\n $i=$j=0;\n ($n, $sx, $sy, $ex, $ey) = split/ /;\n $dx = $ex - $sx;\n $dy = $ey - $sy;\n $_=<>;\n chomp;\n # $_.=\"Q\";\n while ($_){\n if (s/^N//){if ($dy > 0){ --$dy}}\n elsif (s/^S//){if ($dy < 0){ ++$dy}}\n elsif (s/^E//){if ($dx > 0){ --$dx}}\n elsif (s/^W//){if ($dx < 0){ ++$dx}}\n $i++;\n if (($dy==0) and ($dx==0)) { print ($i,\"\\n\"); ++$j; last }\n \n }\n \n if ($j){ print \"-1\\n\"}\n # print \"\\n\";\n \n}"}], "src_uid": "aa31a2a1ad1575aee051ddee8642c53a"} {"nl": {"description": "Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions \"make a rectangle\" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position.", "input_spec": "The first line of input will contain two integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region.", "output_spec": "Output \"YES\" if it is possible for Hongcow to make a rectangle. Output \"NO\" otherwise.", "sample_inputs": ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."], "sample_outputs": ["YES", "NO", "YES"], "notes": "NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................"}, "positive_code": [{"source_code": "<>; $/ = \"\"; $_ = <>;\n\n$r = m{\n\t^\n\t(\\.+\\n)*\n\t(\\.*X+\\.*\\n)\n\t(\\2)*\n\t(\\.+\\n)*\n\t$\n}sx;\n\nprint $r? \"YES\": \"NO\";"}], "negative_code": [], "src_uid": "b395be2597f4cc0478bc45f774fa1c01"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of elements in the array. The second line contains n distinct space-separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009n) \u2014 the elements of array. The third line contains integer m (1\u2009\u2264\u2009m\u2009\u2264\u2009105) \u2014 the number of queries. The last line contains m space-separated integers b1,\u2009b2,\u2009...,\u2009bm (1\u2009\u2264\u2009bi\u2009\u2264\u2009n) \u2014 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 \u0421++. 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": "use strict;\nuse warnings;\n\nchomp (my $length = <>);\nchomp (my @array = split(\" \", <>));\nmy @positions;\nfor (my $i = 0;$i<@array;$i++){\n\t$positions[$array[$i]] = $i;\n}\n\nchomp (my $numQueries = <>);\nchomp (my @queries = split(\" \", <>));\n\nmy $vasya = 0;\nmy $petya = 0;\n\nfor(@queries) {\n\t$vasya += $positions[$_]+1;\n\t$petya += $length - $positions[$_];\n}\n\nprint \"$vasya $petya\";"}, {"source_code": "use strict;\nuse warnings;\n\nchomp (my $length = <>);\nchomp(my $temp = <>);\nmy @array = split(\" \", $temp);\nundef $temp;\nmy @positions;\nfor (my $i = 0;$i<@array;$i++){\n\t$positions[$array[$i]] = $i;\n}\n\nchomp (my $numQueries = <>);\nchomp($temp = <>);\nmy @queries = split(\" \", $temp);\nundef $temp;\n\nmy $vasya = 0;\nmy $petya = 0;\n\nfor(@queries) {\n\t$vasya += $positions[$_]+1;\n\t$petya += $length - $positions[$_];\n}\n\nprint \"$vasya $petya\";"}, {"source_code": "chomp($n=);\n@elements = split /\\s+/, ;\nchomp($m = );\n@queries = split /\\s+/, ;\n\n%index = { };\n$i = 1;\nforeach $e (@elements) {\n\t$index{$e} = $i;\n\t$i++;\n}\n\n$vasya = 0;\n$petya = 0;\n\nforeach $q (@queries) {\n\t$i = $index{$q};\n\t$vasya += $i;\n\t$petya += ($n-$i + 1);\n}\nprint \"$vasya $petya\\n\";"}, {"source_code": "use warnings;\nuse strict;\nuse List::Util (\"sum\", \"max\", \"min\");\n\nmy ($n) = scalar <>;\nmy @vals = split \" \", scalar <>;\nmy $q = scalar <>;\nmy @qs = split \" \", scalar <>;\n\nmy %pos_left;\nmy %pos_right;\n\nfor (0..$#vals)\n{\n\t$pos_left{$vals[$_]} ||= 1 + $_;\n\t$pos_right{$vals[$_]} = 1 + $#vals - $_;\n}\n$\\ = \" \";\nprint sum(map $pos_left{$_}, @qs);\nprint sum(map $pos_right{$_}, @qs);\n\n"}], "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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "use strict;\n#open STDIN, 'output.txt';\nmy $n = <>;\nmy @a;\nmy $ans = 0;\nfor (my $i = 0; $i < $n; $i++){\n my @buf = split(' ', <>);\n for(my $j = 0; $j <= $#buf; $j++){\n $ans += $buf[$j];\n }\n my @buf2;\n $buf2[0] = $ans;\n $buf2[1] = $i + 1;\n push(@a, [@buf2]);\n $ans = 0;\n}\n@a = sort{\n $b->[0] <=> $a->[0] ||\n $a->[1] <=> $b->[1];\n}@a;\n\nfor(my $i = 0; $i < $n; $i++){\n if($a[$i][1] == 1){\n print $i + 1;\n }\n}\n"}, {"source_code": "use strict;\nmy $n = <>;\nmy @a;\nmy $ans = 0;\nfor (my $i = 0; $i < $n; $i++){\n my @buf = split(' ', <>);\n for(my $j = 0; $j <= $#buf; $j++){\n $ans += $buf[$j];\n }\n push(@a, [($ans, $i + 1)]);\n $ans = 0;\n}\n@a = sort{\n $b->[0] <=> $a->[0] ||\n $a->[1] <=> $b->[1];\n}@a;\n\nfor(my $i = 0; $i < $n; $i++){\n if($a[$i][1] == 1){\n print $i + 1;\n }\n}\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\u2009+\u2009x). 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,\u2009li\u2009+\u20091,\u2009...,\u2009ri). 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\u2009\u2264\u2009n\u2009\u2264\u200950, 1\u2009\u2264\u2009x\u2009\u2264\u2009105) \u2014 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\u2009\u2264\u2009li\u2009\u2264\u2009ri\u2009\u2264\u2009105). It is guaranteed that for all integers i from 2 to n the following condition holds: ri\u2009-\u20091\u2009<\u2009li.", "output_spec": "Output a single number \u2014 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": "($x) =<>=~ /\\d+$/g;\nmap {/ /,\n$s += ($` - $b - 1) % $x + ($b = $') - $` + 1\n} <>;\nprint $s\n"}, {"source_code": "($x) =<>=~ /\\d+$/g;\nmap {/ /,\n$s += ($` - $b - 1) % $x + ($b = $') - $` + 1\n} <>;\nprint $s\n"}, {"source_code": "($x) =<>=~ /\\d+$/g;\nmap {/ /,\n$s += ($` - $b - 1) % $x + ($b = $') - $` + 1\n} <>;\nprint $s"}, {"source_code": "($x) =<>=~ /\\d+$/g;\nmap {/ /,\n$s += ($` - $b - 1) % $x + ($b = $') - $` + 1\n} <>;\nprint $s\n"}, {"source_code": "($x) =<>=~ /\\d+$/g;\nmap {/ /,\n$s += ($` - $b - 1) % $x + ($b = $') - $` + 1\n} <>;\nprint $s\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\nuse integer;\n\nmy ($n, $x) = split / /, <>;\n($ans, $cur) = (0, 1);\nwhile ($n-- > 0) {\n\t($l, $r) = split / /, <>;\n\t$m = ($l - $cur) / $x;\n\t$l = $cur + $m * $x;\n\t$ans += ($r - $l + 1);\n\t$cur = $r + 1;\n}\nsay $ans;"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: a.pl\n#\n# USAGE: ./a.pl \n#\n# DESCRIPTION: \n#\n# OPTIONS: ---\n# REQUIREMENTS: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: YOUR NAME (), \n# ORGANIZATION: \n# VERSION: 1.0\n# CREATED: 1/14/2015 8:02:38 PM\n# REVISION: ---\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.012;\n\nchomp($_ = );\nmy ($n, $x) = split(/ /, $_);\nmy @time;\nwhile (<>) {\n\tchomp;\n\tmy ($l, $r) = split(/ /);\n\tpush(@time, $l);\n\tpush(@time,$r);\n}\nmy ($now, $sum);\n$now = 1; $sum = 0; \nwhile (1) {\n\tmy $l = shift @time;\n\tmy $r = shift @time;\n\twhile (1) {\n\t\tif ($now + $x > $l) {\n\t\t\t$sum += ($r - $now + 1);\n\t\t\t$now = $r + 1;\n\t\t\tlast;\n\t\t}\n\t\telse {\n\t\t\t$now += $x;\n\t\t}\n\t}\n\tif ( scalar @time == 0 ) {\n\t\tlast;\n\t}\n}\nprint \"$sum\\n\";\n\n\n"}, {"source_code": "($x) =<>=~ /\\d+$/g;\nmap {/ /,\n$s += ($` - $b - 1) % $x + ($b = $') - $` + 1\n} <>;\nprint $s\n"}, {"source_code": "($x) =<>=~ /\\d+$/g;\nmap {/ /,\n$s += ($` - $b - 1) % $x + ($b = $') - $` + 1\n} <>;\nprint $s"}, {"source_code": "($x) =<>=~ /\\d+$/g;\nmap {/ /,\n$s += ($` - $b - 1) % $x + ($b = $') - $` + 1\n} <>;\nprint $s\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy ( $n, $x ) = split ( \" \", <> );\nmy $cur = 1;\nmy $result = 0;\nwhile ( $n-- ){\n\tmy ( $s, $e ) = split ( \" \", <> );\n\tmy $y = int ( ( $s - $cur ) / $x );\n\t$result += $e - ( $y * $x + $cur ) + 1;\n\t$cur = $e + 1;\n}\nprint \"$result\\n\";\n"}, {"source_code": "($x) =<>=~ /\\d+$/g;\nmap {/ /,\n$s += ($` - $b - 1) % $x + ($b = $') - $` + 1\n} <>;\nprint $s\n"}, {"source_code": "($x) =<>=~ /\\d+$/g;\nmap {/ /,\n$s += ($` - $b - 1) % $x + ($b = $') - $` + 1\n} <>;\nprint $s"}, {"source_code": "($n, $x) = split \" \", <>;\n$/ = $,;\n$_ = \"0\\n\".<>;\n$s += $2 - $1 + 1 while /(\\d+) (\\d+)/g;\n$s += ($2 - $1 - 1) % $x while /(\\d+)\\n(\\d+)/g;\nprint $s"}, {"source_code": "($n, $x) = split \" \", <>;\n/(\\d+) (\\d+)/,\n$s += ($1 - $b - 1) % $x + ($b = $2) - $1 + 1\nfor <>;\nprint $s"}, {"source_code": "$\\ = $/;\n($n, $x) = split \" \", <>;\n$/ = $,;\n$_ = <>;\n$sum += $2 - $1 + 1 while /(\\d+) (\\d+)/g;\ns/^/0\\n/;\n$sum += ($2 - $1 - 1) % $x while / (\\d+) \\n (\\d+) /gx;\nprint $sum"}, {"source_code": "($x) =<>=~ /\\d+$/g;\nmap {/ /,\n$s += ($` - $b - 1) % $x + ($b = $') - $` + 1\n} <>;\nprint $s\n"}], "negative_code": [{"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: a.pl\n#\n# USAGE: ./a.pl \n#\n# DESCRIPTION: \n#\n# OPTIONS: ---\n# REQUIREMENTS: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: YOUR NAME (), \n# ORGANIZATION: \n# VERSION: 1.0\n# CREATED: 1/14/2015 8:02:38 PM\n# REVISION: ---\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.012;\n\nchomp($_ = );\nmy ($n, $x) = split(/ /, $_);\nmy @time;\nwhile (<>) {\n\tchomp;\n\tmy ($l, $r) = split(/ /);\n\tpush(@time, $l);\n\tpush(@time,$r);\n}\nmy ($now, $sum);\n$now = 1; $sum = 0; \nwhile ($now < $time[$#time]) {\n\tmy $l = shift @time;\n\tmy $r = shift @time;\n\twhile (1) {\n\t\tif ($now + $x >= $l) {\n\t\t\t$sum += ($r - $now + 1);\n\t\t\t$now = $r + 1;\n\t\t\tlast;\n\t\t}\n\t\telse {\n\t\t\t$now += $x;\n\t\t}\n\t}\n\tif ( scalar @time == 0 ) {\n\t\tlast;\n\t}\n}\nprint \"$sum\\n\";\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n#\n# FILE: a.pl\n#\n# USAGE: ./a.pl \n#\n# DESCRIPTION: \n#\n# OPTIONS: ---\n# REQUIREMENTS: ---\n# BUGS: ---\n# NOTES: ---\n# AUTHOR: YOUR NAME (), \n# ORGANIZATION: \n# VERSION: 1.0\n# CREATED: 1/14/2015 8:02:38 PM\n# REVISION: ---\n#===============================================================================\n\nuse strict;\nuse warnings;\nuse utf8;\nuse 5.012;\n\nchomp($_ = );\nmy ($n, $x) = split(/ /, $_);\nmy @time;\nwhile (<>) {\n\tchomp;\n\tmy ($l, $r) = split(/ /);\n\tpush(@time, $l);\n\tpush(@time,$r);\n}\nmy ($now, $sum);\n$now = 1; $sum = 0; \nwhile ($now < $time[$#time]) {\n\tmy $l = shift @time;\n\tmy $r = shift @time;\n\twhile (1) {\n#\t\tprint \"now = $now\\n\";\n\t\tif ($now + $x > $l) {\n\t\t\t$sum += ($r - $now + 1);\n\t\t\t$now = $r + 1;\n\t\t\tlast;\n\t\t}\n\t\telse {\n\t\t\t$now += $x;\n\t\t}\n\t}\n\tif ( scalar @time == 0 ) {\n\t\tlast;\n\t}\n}\nprint \"$sum\\n\";\n"}, {"source_code": "$\\ = $/;\n($n, $x) = split \" \", <>;\n$/ = $,;\n$_ = <>;\n$sum += $2 - $1 + 1 while /(\\d+) (\\d+)/g;\ns/^/1 /;\n$sum += ($2 - $1) % $x while / (\\d+) \\n (\\d+) /gx;\nprint $sum"}, {"source_code": "$\\ = $/;\n($n, $x) = split \" \", <>;\n$/ = $,;\n$_ = <>;\n$sum += $2 - $1 + 1 while /(\\d+) (\\d+)/g;\n$sum += ($2 - $1) % $x while / (\\d+) \\n (\\d+) /gx;\nprint $sum"}, {"source_code": "$\\ = $/;\nwhile (<>){\n\t($n, $x)=split;\n\t@_ = (0) x (1e5 + 1);\n\tfor $i (1..$n){\n\t\t($l, $r)= split \" \",<>;\n\t\t$base = ($l - 1) - ($l - 1) % $x;\n\t\tfor $j ($base + 1 .. $r){\n\t\t\t$_[$j] = 1;\n\t\t\t}\n\t\t}\n\tundef $occ;\n\t$_ && $occ++ for @_;\n\tprint $occ\n\t}"}], "src_uid": "ac33b73da5aaf2139b348a9c237f93a4"} {"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$$$) \u2014 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$$$) \u2014 the array itself.", "output_spec": "On the first line print $$$k$$$ \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tfor( @_ ){\n\t\tif( exists $h{ $_ - 1 } ){\n\t\t\t$h{ $_ } = $h{ $_ - 1 } + 1;\n\t\t\t}\n\t\telse{\n\t\t\t$h{ $_ } = 1;\n\t\t\t}\n\t\t}\n\t\n\tmy $max = ( sort { $b <=> $a } values %h )[ 0 ];\n\t\n\t$debug and print $max;\n\t\n\tmy $num;\n\t\n\tfor( @_ ){\n\t\t$h{ $_ } == $max and $num = $_;\n\t\t}\n\t\n\t$debug and print $num;\n\t\n\tmy @idxs;\n\t\n\tfor my $i ( reverse 0 .. @_ - 1 ){\n\t\tif( $_[ $i ] == $num ){\n\t\t\t$num --;\n\t\t\tpush @idxs, $i + 1;\n\t\t\t}\n\t\t}\n\t\n\tprint ~~ @idxs;\n\tprint join ' ', reverse @idxs;\n\t\n\t$debug and print '-' x 15;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tfor( @_ ){\n\t\tif( exists $h{ $_ - 1 } ){\n\t\t\t$h{ $_ } = $h{ $_ - 1 } + 1;\n\t\t\t}\n\t\telse{\n\t\t\t$h{ $_ } = 1;\n\t\t\t}\n\t\t}\n\t\n\tmy $max = ( sort { $b <=> $a } values %h )[ 0 ];\n\t\n\t$debug and print $max;\n\t\n\tmy $num;\n\t\n\tfor( @_ ){\n\t\t$h{ $_ } == $max and $num = $_;\n\t\t}\n\t\n\t$debug and print $num;\n\t\n\tmy @idxs;\n\t\n\tfor my $i ( reverse 0 .. @_ - 1 ){\n\t\tif( $_[ $i ] == $num ){\n\t\t\t$num --;\n\t\t\tpush @idxs, $i + 1;\n\t\t\t}\n\t\t}\n\t\n\tprint join ' ', reverse @idxs;\n\t\n\t$debug and print '-' x 15;\n\t}"}], "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 \u2014 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": "$ch1=<>;\nchomp$ch;\n$ch2=<>;\nchomp$ch2;\n$n=length$ch1;\n$diff=0,$nb71=0,$nb72=0;\nfor($i=0;$i<$n;$i++){\n\nif(substr($ch1,$i,1)!=substr($ch2,$i,1)){\n$diff++;\n}\nif(substr($ch1,$i,1)=='7'){\n$nb71++;\n}\nif(substr($ch2,$i,1)=='7'){\n$nb72++;\n}\n\n}\n$res=abs($nb72-$nb71)+($diff-abs($nb72-$nb71))/2;\nprint $res;"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\nuse List::Util qw{max};\n\nmy @a = split //, <>;\nmy @b = split //, <>;\npop @a;\npop @b;\nmy ($fours, $sevens) = (0, 0);\n\nfor (0 .. $#a) {\n if ($a[$_] > $b[$_]) {\n\t$sevens++;\n } elsif ($a[$_] < $b[$_]) {\n\t$fours++;\n }\n}\n\nprint max($sevens, $fours) . \"\\n\";\n"}], "negative_code": [], "src_uid": "2d1609b434262d30f6bd030d41a71bd5"} {"nl": {"description": "Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.Vasya decided to organize information about the phone numbers of friends. You will be given n strings \u2014 all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.Read the examples to understand statement and format of the output better.", "input_spec": "First line contains the integer n (1\u2009\u2264\u2009n\u2009\u2264\u200920)\u00a0\u2014 number of entries in Vasya's phone books. The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.", "output_spec": "Print out the ordered information about the phone numbers of Vasya's friends. First output m\u00a0\u2014 number of friends that are found in Vasya's phone books. The following m lines must contain entries in the following format \"name number_of_phone_numbers phone_numbers\". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend. Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.", "sample_inputs": ["2\nivan 1 00123\nmasha 1 00123", "3\nkarl 2 612 12\npetr 1 12\nkatya 1 612", "4\nivan 3 123 123 456\nivan 2 456 456\nivan 8 789 3 23 6 56 9 89 2\ndasha 2 23 789"], "sample_outputs": ["2\nmasha 1 00123 \nivan 1 00123", "3\nkatya 1 612 \npetr 1 12 \nkarl 1 612", "2\ndasha 2 23 789 \nivan 4 789 123 2 456"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\t\n\tmy %h;\n\t\n\tfor( @_ ){\n\t\tmy( $name, undef, @T ) = split;\n\t\t$h{ $name }{ $_ } = 1 for @T;\n\t\t}\n\t\n\tfor my $name ( keys %h ){\n\t\tmy @keys = keys %{ $h{ $name } };\n\t\tfor my $i ( @keys ){\n\t\t\tfor my $j ( @keys ){\n\t\t\t\t$i =~ /(?<=.)$j$/ and delete $h{ $name }{ $j };\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint for ~~ keys %h,\n\t\tmap { my @K = keys %{ $h{ $_ } }; $_ . ' ' . @K . ' ' . \"@K\" } keys %h;\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\n<>;\n\nfor( <> ){\n\t( $n, undef, @T ) = split;\n\t$h{ $n }{ $_ } = 1 for @T;\n\t}\n\nfor $n ( keys %h ){\n\t@K = keys %{ $h{ $n } };\n\tfor $i ( @K ){\n\t\tfor $j ( @K ){ \n\t\t\t$i =~ /.$j$/ and delete $h{ $n }{ $j }\n\t\t\t}\n\t\t}\n\t}\n\nprint for ~~ keys %h,\n\tmap { @K = keys %{ $h{ $_ } }; \"$_ \" . @K . \" @K\" } keys %h"}], "negative_code": [{"source_code": "<>;\n\nfor( <> ){\n\t( $n, undef, @T ) = split;\n\t$h{ $n }{ $_ } = 1 for @T;\n\t}\n\nfor $n ( keys %h ){\n\t@K = keys %{ $h{ $n } };\n\tfor $i ( @K ){\n\t\tfor $j ( @K ){ \n\t\t\t$i =~ /.$j$/ and delete $h{ $n }{ $j }\n\t\t\t}\n\t\t}\n\t}\n\nprint for ~~ keys %h,\n\tmap { @K = keys %{ $h{ $_ } }; \"$_ \" . @K . \" @K\" } keys %h"}], "src_uid": "df7b16d582582e6756cdb2d6d856c873"} {"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 \u00a0\u2014 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": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy ($n,$L,$a)=split/ /,;\nmy @t,my @l;my $o=0;\nif($n!=0){\n ($t[0],$l[0])=split/ /,;chomp($a,$l[0]);\n $o=$t[0]/$a;\n for(1..$n-1){\n\t($t[$_],$l[$_])=split/ /,;\n\tchomp($l[$_]);\n\t$o+=($t[$_]-$l[$_-1]-$t[$_-1])/$a;\n }\n}\n$o=$L/$a if($n==0);\n$o+=($L-$l[-1]-$t[-1])/$a unless($n==0);\nprint \"$o\\n\";"}], "negative_code": [{"source_code": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy ($n,$L,$a)=split/ /,;\nmy @t,my @l;my $o=0;\nif($n!=0){\n ($t[0],$l[0])=split/ /,;chomp($a,$l[0]);\n $o=$t[0]/$a;\n for(1..$n-1){\n\t($t[$_],$l[$_])=split/ /,;\n\tchomp($l[$_]);\n\t$o+=($t[$_]-$l[$_-1])/$a;\n }\n}\n$o=$L/$a if($n==0);\n$o+=($L-$l[-1])/$a unless($n==0);\nprint \"$o\\n\";"}], "src_uid": "00acb3b54975820989a788b9389c7c0b"} {"nl": {"description": "Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.", "input_spec": "The first line of the input data contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105). The second line contains an array of original integers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009109\u2009\u2264\u2009ai\u2009\u2264\u2009109). ", "output_spec": "Print on the single line the answer to the problem: the amount of subarrays, which are magical. 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).", "sample_inputs": ["4\n2 1 1 4", "5\n-2 -2 -2 0 1"], "sample_outputs": ["5", "8"], "notes": "NoteNotes to sample tests:Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3]. "}, "positive_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nsub magic {\n my @a = @{$_[0]};\n my $cur = shift @a;\n my $c = 1;\n my $s = 0;\n for (@a) {\n if ($cur != $_) {\n $s += $c * ($c + 1) / 2;\n $c = 1;\n } else {\n $c++;\n }\n $cur = $_;\n }\n $s += $c * ($c + 1) / 2;\n $s;\n}\n\n<>;\nmy @a = split ' ', <>;\nprint magic(\\@a), \"\\n\";\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nsub magic {\n my @a = @{$_[0]};\n my $cur = 'inf';\n my $c = 1;\n my $s = 0;\n for (@a) {\n if ($cur != $_) {\n $s += $c * ($c + 1) / 2;\n $c = 1;\n } else {\n $c++;\n }\n $cur = $_;\n }\n $s;\n}\n\n<>;\nmy @a = split ' ', <>;\nprint magic(\\@a), \"\\n\";\n"}], "src_uid": "0b229ddf583949d43d6f1728e38c3cad"} {"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})$$$ \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\tmy $cnt = 0;\n\t\n\ts/\n\t\tab|ba|bb|aa\n\t\t/\n\t\t( $& eq 'aa' || $& eq 'bb' ) ? do { $cnt ++; \"ab\" } : $&\n\t\t/gex;\n\t\n\tprint $cnt;\n\tprint;\n\t}"}], "negative_code": [], "src_uid": "8ad06ac90b258a8233e2a1cf51f68078"} {"nl": {"description": "Given an array of $$$n$$$ positive integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 1000$$$). Find the maximum value of $$$i + j$$$ such that $$$a_i$$$ and $$$a_j$$$ are coprime,$$$^{\\dagger}$$$ or $$$-1$$$ if no such $$$i$$$, $$$j$$$ exist.For example consider the array $$$[1, 3, 5, 2, 4, 7, 7]$$$. The maximum value of $$$i + j$$$ that can be obtained is $$$5 + 7$$$, since $$$a_5 = 4$$$ and $$$a_7 = 7$$$ are coprime.$$$^{\\dagger}$$$ Two integers $$$p$$$ and $$$q$$$ are coprime if the only positive integer that is a divisor of both of them is $$$1$$$ (that is, their greatest common divisor is $$$1$$$).", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10$$$)\u00a0\u2014 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 2\\cdot10^5$$$)\u00a0\u2014 the length of the array. The following line contains $$$n$$$ space-separated positive integers $$$a_1$$$, $$$a_2$$$,..., $$$a_n$$$ ($$$1 \\leq a_i \\leq 1000$$$)\u00a0\u2014 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 \u00a0\u2014 the maximum value of $$$i + j$$$ such that $$$i$$$ and $$$j$$$ satisfy the condition that $$$a_i$$$ and $$$a_j$$$ are coprime, or output $$$-1$$$ in case no $$$i$$$, $$$j$$$ satisfy the condition.", "sample_inputs": ["6\n\n3\n\n3 2 1\n\n7\n\n1 3 5 2 4 7 7\n\n5\n\n1 2 3 4 5\n\n3\n\n2 2 4\n\n6\n\n5 4 3 15 12 16\n\n5\n\n1 2 2 3 6"], "sample_outputs": ["6\n12\n9\n-1\n10\n7"], "notes": "NoteFor the first test case, we can choose $$$i = j = 3$$$, with sum of indices equal to $$$6$$$, since $$$1$$$ and $$$1$$$ are coprime.For the second test case, we can choose $$$i = 7$$$ and $$$j = 5$$$, with sum of indices equal to $$$7 + 5 = 12$$$, since $$$7$$$ and $$$4$$$ are coprime."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy @h;\n\nfor my $i ( 1 .. 1000 ){\n\tfor my $j ( 2 .. $i ){\n\t\tif( $i % $j == 0 ){\n\t\t\t$h[ $i ]{ $j } ++;\n\t\t\t}\n\t\t}\n\t}\n\nmy @co;\n\nfor my $i ( 2 .. 1000 ){\n\tfor my $j ( 2 .. 1000 ){\n\t\tnext if $i <= $j;\n\t\t\n\t\tnext if $i % 2 == 0 and $j % 2 == 0;\n\t\tnext if $i % 3 == 0 and $j % 3 == 0;\n\t\tnext if $i % 5 == 0 and $j % 5 == 0;\n\t\tnext if $i % 7 == 0 and $j % 7 == 0;\n\t\t\n\t\tmy %i;\n\t\tmap $i{ $_ } ++, keys %{ $h[ $i ] }, keys %{ $h[ $j ] };\n\t\t\n\t\tnext if grep $_ == 2, values %i;\n\t\t\n\t\t$co[ $i ]{ $j } ++;\n\t\t}\n\t}\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @g;\n\tmy @gg;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\t$g[ $_[ $i ] ] = $i + 1;\n\t\tpush @gg, $_[ $i ];\n\t\t}\n\t\n\tmy @sg = sort { $a <=> $b } map $g[ $_ ] - 1, \n\t\tgrep defined $g[ $_ ], 1 .. 1000;\n\t\n\tmy @cand = ( -1 );\n\t\n\tif( $g[ 1 ] ){\n\t\tpush @cand, $g[ 1 ] + $_;\n\t\t}\n\t\n\tfor my $i ( reverse @sg ){\n\t\tfor my $j ( reverse @sg ){\n\t\t\tnext if $i <= $j;\n\t\t\t\n\t\t#\tprint \"$i, $j, $_[ $i ], $_[ $j ], $g[ $_[ $i ] ] + $g[ $_[ $j ] ]\";\n\t\t\t\n\t\t\tnext if not exists $co[ $_[ $i ] ]{ $_[ $j ] } and\n\t\t\t\t\tnot exists $co[ $_[ $j ] ]{ $_[ $i ] };\n\t\t\t\n\t\t\tpush @cand, $g[ $_[ $i ] ] + $g[ $_[ $j ] ];\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tprint 0 + ( sort { $b <=> $a } @cand )[ 0 ];\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy @h;\n\nfor my $i ( 1 .. 1000 ){\n\tfor my $j ( 2 .. $i ){\n\t\tif( $i % $j == 0 ){\n\t\t\t$h[ $i ]{ $j } ++;\n\t\t\t}\n\t\t}\n\t}\n\nmy @co;\n\nfor my $i ( 2 .. 1000 ){\n\tfor my $j ( 2 .. 1000 ){\n\t\tnext if $i <= $j;\n\t\t\n\t\tnext if $i % 2 == 0 and $j % 2 == 0;\n\t\tnext if $i % 3 == 0 and $j % 3 == 0;\n\t\tnext if $i % 5 == 0 and $j % 5 == 0;\n\t\tnext if $i % 7 == 0 and $j % 7 == 0;\n\t\t\n\t\tmy %i;\n\t\tmap $i{ $_ } ++, keys %{ $h[ $i ] }, keys %{ $h[ $j ] };\n\t\t\n\t\tnext if grep $_ == 2, values %i;\n\t\t\n\t\t$co[ $i ]{ $j } ++;\n\t\t}\n\t}\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @g;\n\tmy @gg;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\t$g[ $_[ $i ] ] = $i + 1;\n\t\tpush @gg, $_[ $i ];\n\t\t}\n\t\n\tmy @cand = ( -1 );\n\t\n\tif( $g[ 1 ] ){\n\t\tpush @cand, $g[ 1 ] + $_;\n\t\t}\n\t\n\tfor my $i ( reverse 0 .. @_ - 1 ){\n\t\tfor my $j ( reverse 0 .. $i - 1 ){\n\t\t\t\n\t\t\tnext if not exists $co[ $_[ $i ] ]{ $_[ $j ] };\n\t\t\t\n\t\t\tpush @cand, $g[ $_[ $i ] ] + $g[ $_[ $j ] ];\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tprint 0 + ( sort { $b <=> $a } @cand )[ 0 ];\n\t}"}], "src_uid": "d665ecbf36cc0c0ddd148137fb693bf2"} {"nl": {"description": "You are given a number $$$k$$$ and a string $$$s$$$ of length $$$n$$$, consisting of the characters '.' and '*'. You want to replace some of the '*' characters with 'x' characters so that the following conditions are met: The first character '*' in the original string should be replaced with 'x'; The last character '*' in the original string should be replaced with 'x'; The distance between two neighboring replaced characters 'x' must not exceed $$$k$$$ (more formally, if you replaced characters at positions $$$i$$$ and $$$j$$$ ($$$i < j$$$) and at positions $$$[i+1, j-1]$$$ there is no \"x\" symbol, then $$$j-i$$$ must be no more than $$$k$$$). For example, if $$$n=7$$$, $$$s=$$$.**.*** and $$$k=3$$$, then the following strings will satisfy the conditions above: .xx.*xx; .x*.x*x; .xx.xxx. But, for example, the following strings will not meet the conditions: .**.*xx (the first character '*' should be replaced with 'x'); .x*.xx* (the last character '*' should be replaced with 'x'); .x*.*xx (the distance between characters at positions $$$2$$$ and $$$6$$$ is greater than $$$k=3$$$). Given $$$n$$$, $$$k$$$, and $$$s$$$, find the minimum number of '*' characters that must be replaced with 'x' in order to meet the above conditions.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 500$$$). Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 50$$$). The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of the characters '.' and '*'. It is guaranteed that there is at least one '*' in the string $$$s$$$. It is guaranteed that the distance between any two neighboring '*' characters does not exceed $$$k$$$.", "output_spec": "For each test case output the minimum number of '*' characters that must be replaced with 'x' characters in order to satisfy the conditions above.", "sample_inputs": ["5\n7 3\n.**.***\n5 1\n..*..\n5 2\n*.*.*\n3 2\n*.*\n1 1\n*"], "sample_outputs": ["3\n1\n3\n2\n1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tmy( undef, $k ) = split;\r\n\t\r\n\t$k --;\r\n\t\r\n\tprint length \r\n\t\t<> =~\r\n\t\ts/\\*/x/r =~\r\n\t\ts/[x*].{0,$k}\\K(?=\\*)/,/gr =~\r\n\t\ts/,\\*/x/gr =~\r\n\t\ty/.*\\n//dr;\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\ts/\\*/x/;\n##\ts/\\*[^*]*$/x/;\n\t\n\t$debug and print;\n\t\n\t$k --;\n\t\n\ts/[x*].{0,$k}\\K(?=\\*)/,/g;\n\t\n\t$debug and print;\n\t\n\ts/,\\*/x/g;\n\t\n\t$debug and print;\n\t\n\tprint scalar( () = m/x/g );\n\t}"}, {"source_code": "#!/usr/bin/perl\r\n\r\nmy ($t) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\nwhile( $t -- > 0 ){\r\n my ($n,$k) = map { $_ - 0 } split(/\\s+/o,);\r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n \r\n my $fi = -1;\r\n my $la = -1;\r\n for(my $i=0;$i<$n;$i++){\r\n if( substr($s,$i,1) eq '*' ){\r\n $fi = $i if $fi < 0;\r\n $la = $i;\r\n }\r\n }\r\n if( $fi == $la ){\r\n print \"1\\n\"; next;\r\n }\r\n my $i = $fi;\r\n my $c = 2;\r\n while( $i < $la ){\r\n last if $la <= $i + $k;\r\n for(my $j=$i+$k;$j>$i;$j--){\r\n if( substr($s,$j,1) eq '*' ){\r\n $i = $j;\r\n $c++;\r\n last;\r\n }\r\n }\r\n }\r\n print \"$c\\n\";\r\n}\r\n\r\n"}], "negative_code": [], "src_uid": "874e22d4fd8e35f7e0eade2b469ee5dc"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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 \u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n,$x) = map { $_ - 0 } split(/\\s+/,);\n my @aa = map { $_ - 0 } split(/\\s+/,);\n \n my @sa = sort { $b <=> $a } @aa;\n \n my $cnt = 0;\n \n my $mem = 0;\n for(my $i=0;$i<$n;$i++){\n $mem ++;\n if( $mem * $sa[$i] >= $x ){\n $cnt++;\n $mem = 0;\n }\n }\n \n print \"$cnt\\n\";\n \n}\n\n\n\n"}], "negative_code": [], "src_uid": "8a6953a226abef41a44963c9b4998a25"} {"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\u2009-\u2009aj| 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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 amount of soldiers. Then follow the heights of the soldiers in their order in the circle \u2014 n space-separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20091000). The soldier heights are given in clockwise or counterclockwise direction.", "output_spec": "Output two integers \u2014 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": "chomp($n = );\n@soldiers = split /\\s+/, ;\nunshift @soldiers, 0;\n$mingap = 9999;\n$minpos = 0;\nfor($i=1;$i<($n);$i++) {\n\t$d = abs($soldiers[$i]-$soldiers[$i+1]);\n#\tprint \"diff between pos $i and \".($i+1).\" is $d\\n\";\n\tif( $d < $mingap) {\n\t\t$mingap = $d;\n\t\t$minpos = $i;\n\t}\n}\n#print \"minggap $mingap, diff between first and last : \".abs($soldiers[1]-$soldiers[-1]).\"\\n\";\n\nif(abs($soldiers[1]-$soldiers[-1])<$mingap) {\n\tprint \"1 $n\\n\";\n} else {\n\tprint \"\".($minpos).\" \".($minpos+1).\"\\n\";\n}"}, {"source_code": "while ($eil=<>) {\n\n$i=1;\n$_=<>;\nchomp;\n/\\d+ /;\n$_.=\" $&\";\ns/\\d+ /$&$&/g;\ns/\\d+ //;\ns/\\d+ $//;\n# print \"$_\\n\";\ns/(\\d+) (\\d+) /(abs($1-$2)).\" \"/eg;\n# print \"$_\\n\";\ns/ $//;\n@_=split/ /;\n$min = shift @_;\n$j=$i;\nfor (@_){\n $i++;\n if ($_ < $min){\n $min = $_;\n $j=$i;\n }\n \n }\nprint \"$j \";\nprint $j==$eil?1:++$j;\nprint \"\\n\";\n}"}], "negative_code": [{"source_code": "chomp($n = );\n@soldiers = split /\\s+/, ;\nunshift @soldiers, 0;\n$mingap = 9999;\n$minpos = 0;\nfor($i=1;$i<($n-1);$i++) {\n\tif(abs($soldiers[$i]-$soldiers[$i+1]) < $mingap) {\n\t\t$mingap = abs($soldiers[$i]-$soldiers[$i+1]);\n\t\t$minpos = $i;\n\t}\n}\n#print \"minggap $mingap, diff between first and last : \".abs($soldiers[0]-$soldiers[-1]).\"\\n\";\n\nif(abs($soldiers[1]-$soldiers[-1])<$mingap) {\n\tprint \"1 $n\\n\";\n} else {\n\tprint \"\".($minpos).\" \".($minpos+1).\"\\n\";\n}"}, {"source_code": "chomp($n = );\n@soldiers = split /\\s+/, ;\n\n$mingap = 9999;\n$minpos = 0;\nfor($i=1;$i<($n-1);$i++) {\n\tif(abs($soldiers[$i]-$soldiers[$i+1]) < $mingap) {\n\t\t$mingap = abs($soldiers[$i]-$soldiers[$i+1]);\n\t\t$minpos = $i;\n\t}\n}\n#print \"minggap $mingap, diff between first and last : \".abs($soldiers[0]-$soldiers[-1]).\"\\n\";\n\nif(abs($soldiers[0]-$soldiers[-1])<$mingap) {\n\tprint \"1 $n\\n\";\n} else {\n\tprint \"-\".($minpos).\" - \".($minpos+1).\"-\\n\";\n}"}, {"source_code": "chomp($n = );\n@soldiers = split /\\s+/, ;\n\n$mingap = 9999;\n$minpos = 0;\nfor($i=1;$i<($n-1);$i++) {\n\tif(abs($soldiers[$i]-$soldiers[$i+1]) < $mingap) {\n\t\t$mingap = abs($soldiers[$i]-$soldiers[$i+1]);\n\t\t$minpos = $i;\n\t}\n}\n#print \"minggap $mingap, diff between first and last : \".abs($soldiers[0]-$soldiers[-1]).\"\\n\";\n\nif(abs($soldiers[0]-$soldiers[-1])<$mingap) {\n\tprint \"1 $n\\n\";\n} else {\n\tprint ($minpos).\" \".($minpos+1).\"\\n\";\n}"}, {"source_code": "chomp($n = );\n@soldiers = split /\\s+/, ;\n\n$mingap = 9999;\n$minpos = 0;\nfor($i=1;$i<($n-1);$i++) {\n\tif(abs($soldiers[$i]-$soldiers[$i+1]) < $mingap) {\n\t\t$mingap = abs($soldiers[$i]-$soldiers[$i+1]);\n\t\t$minpos = $i;\n\t}\n}\n#print \"minggap $mingap, diff between first and last : \".abs($soldiers[0]-$soldiers[-1]).\"\\n\";\n\nif(abs($soldiers[0]-$soldiers[-1])<$mingap) {\n\tprint \"1 $n\\n\";\n} else {\n\tprint \"\".($minpos).\" \".($minpos+1).\"\\n\";\n}"}], "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$$$) \u2014 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$$$) \u2014 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) \u2014 the set of positions described in the problem statement.", "output_spec": "For each test case, print the answer \u2014 \"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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\t@_ = split ' ', <>;\n\tmy @A = sort { $a <=> $b } split ' ', <>;\n\t\n\tmy @B = sort { $a <=> $b } @_;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tfor my $j ( map $_ - 1, @A ){\n\t\t\tif( $_[ $j ] > $_[ $j + 1 ] ){\n\t\t\t\t( $_[ $j ], $_[ $j + 1 ] ) = ( $_[ $j + 1 ], $_[ $j ] );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint \"@_\" eq \"@B\" ? \"YES\" : \"NO\";\n\t}"}], "negative_code": [], "src_uid": "e1481b9d940407da75e11355b580f459"} {"nl": {"description": "There are n integers b1,\u2009b2,\u2009...,\u2009bn 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\u2009+\u20091, adds the bi\u2009+\u20092 number, and so on until the n'th number. Thus, ai\u2009=\u2009bi\u2009-\u2009bi\u2009+\u20091\u2009+\u2009bi\u2009+\u20092\u2009-\u2009bi\u2009+\u20093.... Memory gives you the values a1,\u2009a2,\u2009...,\u2009an, and he now wants you to find the initial numbers b1,\u2009b2,\u2009...,\u2009bn written in the row? Can you do it?", "input_spec": "The first line of the input contains a single integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of integers written in the row. The next line contains n, the i'th of which is ai (\u2009-\u2009109\u2009\u2264\u2009ai\u2009\u2264\u2009109)\u00a0\u2014 the value of the i'th number.", "output_spec": "Print n integers corresponding to the sequence b1,\u2009b2,\u2009...,\u2009bn. 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,\u2009-\u20094, 8,\u2009-\u20092, 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\u2009=\u20092\u2009-\u20094\u2009+\u20096\u2009-\u20091\u2009+\u20093, and \u2009-\u20094\u2009=\u20094\u2009-\u20096\u2009+\u20091\u2009-\u20093.In the second sample test, the sequence 1, \u2009-\u20093, 4, 11, 6 satisfies the reports. For example, 5\u2009=\u200911\u2009-\u20096 and 6\u2009=\u20096."}, "positive_code": [{"source_code": "$,=\" \";\nmy $n=<>;\nchomp($n);\nmy @d=split / /,<>;\nchomp(@d);\n$n--;\nfor(0..$n)\n{\n my $k=$d[$_]+$d[$_+1];\n print $k . \" \";\n}\nprint \"\\n\";\n"}, {"source_code": "use strict;\n\n# cf713a\n=item\n\nA. Memory and Crow\ntime limit per test\n2 seconds\nmemory limit per test\n256 megabytes\ninput\nstandard input\noutput\nstandard output\n\nThere are n integers b1,\u2009b2,\u2009...,\u2009bn written in a row. For all i from 1 to n, values ai are defined by the crows performing the following procedure:\n\n The crow sets ai initially 0.\n The crow then adds bi to ai, subtracts bi\u2009+\u20091, adds the bi\u2009+\u20092 number, and so on until the n'th number. Thus, ai\u2009=\u2009bi\u2009-\u2009bi\u2009+\u20091\u2009+\u2009bi\u2009+\u20092\u2009-\u2009bi\u2009+\u20093.... \n\nMemory gives you the values a1,\u2009a2,\u2009...,\u2009an, and he now wants you to find the initial numbers b1,\u2009b2,\u2009...,\u2009bn written in the row? Can you do it?\nInput\n\nThe first line of the input contains a single integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000) \u2014 the number of integers written in the row.\n\nThe next line contains n, the i'th of which is ai (\u2009-\u2009109\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 the value of the i'th number.\nOutput\n\nPrint n integers corresponding to the sequence b1,\u2009b2,\u2009...,\u2009bn. It's guaranteed that the answer is unique and fits in 32-bit integer type.\nExamples\nInput\n\n5\n6 -4 8 -2 3\n\nOutput\n\n2 4 6 1 3 \n\nInput\n\n5\n3 -2 -1 5 6\n\nOutput\n\n1 -3 4 11 6 \n\nNote\n\nIn the first sample test, the crows report the numbers 6,\u2009-\u20094, 8,\u2009-\u20092, 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\u2009=\u20092\u2009-\u20094\u2009+\u20096\u2009-\u20091\u2009+\u20093, and \u2009-\u20094\u2009=\u20094\u2009-\u20096\u2009+\u20091\u2009-\u20093.\n\nIn the second sample test, the sequence 1, \u2009-\u20093, 4, 11, 6 satisfies the reports. For example, 5\u2009=\u200911\u2009-\u20096 and 6\u2009=\u20096.\n\n\n=cut\n\n# ai\u2009=\u2009bi\u2009-\u2009bi\u2009+\u20091\u2009+\u2009bi\u2009+\u20092\u2009-\u2009bi\u2009+\u20093...\n=item\na0=b0\na1=b1-b0\na2=b2-b1+b0=b2-a1\na3=b3-b2+b1-b0=b3-a2\n\nb0=a0\nb1=a0+a1\nb2=a1+a2\nb3=a2+a3\n=cut\n\nmy ($n,$l)=<>;\nmy @as=split(' ',$l);\nfor(my $i=1;$i<$n;++$i) {\n print $as[$i-1]+$as[$i],' ';\n}\nprint $as[$n-1],\"\\n\";\n\n\n"}], "negative_code": [], "src_uid": "fa256021dc519f526ef3878cce32ff31"} {"nl": {"description": "You are given a decimal representation of an integer $$$x$$$ without leading zeros.You have to perform the following reduction on it exactly once: take two neighboring digits in $$$x$$$ and replace them with their sum without leading zeros (if the sum is $$$0$$$, it's represented as a single $$$0$$$).For example, if $$$x = 10057$$$, the possible reductions are: choose the first and the second digits $$$1$$$ and $$$0$$$, replace them with $$$1+0=1$$$; the result is $$$1057$$$; choose the second and the third digits $$$0$$$ and $$$0$$$, replace them with $$$0+0=0$$$; the result is also $$$1057$$$; choose the third and the fourth digits $$$0$$$ and $$$5$$$, replace them with $$$0+5=5$$$; the result is still $$$1057$$$; choose the fourth and the fifth digits $$$5$$$ and $$$7$$$, replace them with $$$5+7=12$$$; the result is $$$10012$$$. What's the largest number that can be obtained?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of testcases. Each testcase consists of a single integer $$$x$$$ ($$$10 \\le x < 10^{200000}$$$). $$$x$$$ doesn't contain leading zeros. The total length of the decimal representations of $$$x$$$ over all testcases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each testcase, print a single integer\u00a0\u2014 the largest number that can be obtained after the reduction is applied exactly once. The number should not contain leading zeros.", "sample_inputs": ["2\n10057\n90"], "sample_outputs": ["10012\n9"], "notes": "NoteThe first testcase of the example is already explained in the statement.In the second testcase, there is only one possible reduction: the first and the second digits."}, "positive_code": [{"source_code": "<>;\r\n\r\nwhile(<>){\r\n\tprint s/^(?|.*\\K(.)(.)(??{ 'x' x !( $1 + $2 > 9 ) })\r\n\t\t\t |(.)(.)\r\n\t\t\t )\r\n\t\t\t /\r\n\t\t\t $1 + $2\r\n\t\t\t /erx\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\ts/^.*\\K(19|2[89]|3[7-9]|4[6-9]|5[5-9]|6[4-9]|7[3-9]|8[2-9]|9[1-9])/\n\t\t0 + eval join '+', split '', $& /e ||\n\t\ts/../ 0 + eval join '+', split '', $& /e;\n\t\n\tprint;\n\t}"}], "negative_code": [{"source_code": "<>;\r\n\r\nwhile(<>){\r\n\tprint s/^(?|.*\\K(.)(.)(??{ 'x' x !( $1 < $2 && $1 + $2 > 9 ) })\r\n\t\t\t |.*\\K(.)(.)(??{ 'x' x !( $1 == $2 && $1 + $2 > 9 ) })\r\n\t\t\t |(.)(.)\r\n\t\t\t )\r\n\t\t\t /\r\n\t\t\t $1 + $2\r\n\t\t\t /erx\r\n\t}"}, {"source_code": "<>;\r\n\r\nwhile(<>){\r\n\tprint s/^(?|.*\\K(.)(.)(??{ 'x' x !( $1 <= $2 && $1 + $2 > 9 ) })\r\n\t\t\t |(.)(.)\r\n\t\t\t )\r\n\t\t\t /\r\n\t\t\t $1 + $2\r\n\t\t\t /erx\r\n\t}"}, {"source_code": "<>;\r\n\r\nwhile(<>){\r\n\tprint s/^(?|.*\\K(.)(.)(??{ 'x' x !( $1 < $2 && $1 + $2 > 9 ) })\r\n\t\t\t |(.)(.)\r\n\t\t\t )\r\n\t\t\t /\r\n\t\t\t $1 + $2\r\n\t\t\t /erx\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n<>;\n\nwhile(<>){\n\tprint s/^(?|.*\\K(.)(.)(??{ 'x' x ( $1 >= $2 || $1 + $2 < 10 ) })\n\t\t\t |(.)(.)\n\t\t\t )\n\t\t\t /\n\t\t\t $1 + $2\n\t\t\t /erx\n\t}"}, {"source_code": "<>;\r\n\r\nwhile(<>){\r\n\tprint s/^(?|.*\\K(.)(.)(??{ 'x' x ( $1 >= $2 ) })\r\n\t\t\t |(.)(.)\r\n\t\t\t )\r\n\t\t\t /\r\n\t\t\t $1 + $2\r\n\t\t\t /erx\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t\n\ts/^.*\\K(19|2[89]|3[7-9]|4[6-9]|5[5-9]|6[4-9]|7[3-9]|8[2-9]|9[1-9])/\n\t\t0 + eval join '+', split '', $& /e ||\n\t\ts/../ 0 + eval join '+', split '', $& /e;\n\t\n\tprint;\n\t}"}], "src_uid": "6db42771fdd582578194c7b69a4ef575"} {"nl": {"description": "You and your friend Ilya are participating in an individual programming contest consisting of multiple stages. A contestant can get between $$$0$$$ and $$$100$$$ points, inclusive, for each stage, independently of other contestants.Points received by contestants in different stages are used for forming overall contest results. Suppose that $$$k$$$ stages of the contest are completed. For each contestant, $$$k - \\lfloor \\frac{k}{4} \\rfloor$$$ stages with the highest scores are selected, and these scores are added up. This sum is the overall result of the contestant. (Here $$$\\lfloor t \\rfloor$$$ denotes rounding $$$t$$$ down.)For example, suppose $$$9$$$ stages are completed, and your scores are $$$50, 30, 50, 50, 100, 10, 30, 100, 50$$$. First, $$$7$$$ stages with the highest scores are chosen\u00a0\u2014 for example, all stages except for the $$$2$$$-nd and the $$$6$$$-th can be chosen. Then your overall result is equal to $$$50 + 50 + 50 + 100 + 30 + 100 + 50 = 430$$$.As of now, $$$n$$$ stages are completed, and you know the points you and Ilya got for these stages. However, it is unknown how many more stages will be held. You wonder what the smallest number of additional stages is, after which your result might become greater than or equal to Ilya's result, at least in theory. Find this number!", "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 a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$)\u00a0\u2014 the number of completed stages. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 100$$$)\u00a0\u2014 your points for the completed stages. The third line contains $$$n$$$ integers $$$b_1, b_2, \\ldots, b_n$$$ ($$$0 \\le b_i \\le 100$$$)\u00a0\u2014 Ilya's points for the completed stages. 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\u00a0\u2014 the smallest number of additional stages required for your result to be able to become greater than or equal to Ilya's result. If your result is already not less than Ilya's result, print $$$0$$$.", "sample_inputs": ["5\n1\n100\n0\n1\n0\n100\n4\n20 30 40 50\n100 100 100 100\n4\n10 20 30 40\n100 100 100 100\n7\n7 59 62 52 27 31 55\n33 35 50 98 83 80 64"], "sample_outputs": ["0\n1\n3\n4\n2"], "notes": "NoteIn the first test case, you have scored $$$100$$$ points for the first stage, while Ilya has scored $$$0$$$. Thus, your overall result ($$$100$$$) is already not less than Ilya's result ($$$0$$$).In the second test case, you have scored $$$0$$$ points for the first stage, while Ilya has scored $$$100$$$. A single stage with an opposite result is enough for both your and Ilya's overall scores to become equal to $$$100$$$.In the third test case, your overall result is $$$30 + 40 + 50 = 120$$$, while Ilya's result is $$$100 + 100 + 100 = 300$$$. After three additional stages your result might become equal to $$$420$$$, while Ilya's result might become equal to $$$400$$$.In the fourth test case, your overall result after four additional stages might become equal to $$$470$$$, while Ilya's result might become equal to $$$400$$$. Three stages are not enough."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\tmy( $n ) = $_;\n\t\n\tmy @A = sort { $b <=> $a } split ' ', <>;\n\tmy @B = sort { $b <=> $a } split ' ', <>;\n\t\n\t@A = @A[ 0 .. @A - int( @A / 4 ) - 1 ];\n\t\n\tmy $sumA = 0;\n\t$sumA += $_ for @A;\n\t\n\tmy $sumB = 0;\n\t$sumB += $_ for @B[ 0 .. @B - int( @B / 4 ) - 1 ];\n\t\n\t@B = @B[ @B - int( @B / 4 ) .. @B - 1 ];\n\t\n\t$debug and print \"sumA:$sumA\";\n\t$debug and print \"sumB:$sumB\";\n\t$debug and print \"A:@A\";\n\t$debug and\tprint \"B:@B\";\n\t\n\tmy $itr = 0;\n\t\n\twhile( $sumA < $sumB ){\n\t\t$itr ++;\n\t\t\n\t\t$sumA += 100;\n\t\tmy $cnt = $n - int( ( $n + $itr ) / 4 );\n\t\tif( $cnt < @A ){ $sumA -= pop @A; }\n\t\telse{\n\t\t\tif( @B ){\n\t\t\t\t$sumB += shift @B;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$sumB += 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t$debug and print \"-sumA:$sumA\";\n\t\t$debug and print \"-sumB:$sumB\";\n\t\t$debug and print \"-A:@A\";\n\t\t$debug and print \"-B:@B\";\n\t\t}\n\t\n\tprint $itr;\n\t}"}], "negative_code": [], "src_uid": "61b88627fc843ef6e5226e1003822793"} {"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\u2009\u2264\u2009R,\u2009C\u2009\u2264\u2009100. 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\u2009-\u2009r\u2009+\u20091, 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": "($r, $c) = split ' ', <>; $a |= $_ for <>;\n$ans = () = $a =~ /[Bn]+/g;\nprint $ans;"}], "negative_code": [], "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\u2009<\u2009i\u2009\u2264\u2009n) holds ci\u2009\u2264\u2009ci\u2009-\u20091. 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,\u2009j. 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,\u2009j (1\u2009<\u2009i\u2009\u2264\u2009n;\u00a01\u2009\u2264\u2009j\u2009\u2264\u2009ci) holds ai,\u2009j\u2009>\u2009ai\u2009-\u20091,\u2009j; for all i,\u2009j (1\u2009\u2264\u2009i\u2009\u2264\u2009n;\u00a01\u2009<\u2009j\u2009\u2264\u2009ci) holds ai,\u2009j\u2009>\u2009ai,\u2009j\u2009-\u20091. 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\u2009\u2264\u2009n\u2009\u2264\u200950) that shows the number of rows in the table. The second line contains n space-separated integers ci (1\u2009\u2264\u2009ci\u2009\u2264\u200950;\u00a0ci\u2009\u2264\u2009ci\u2009-\u20091) \u2014 the numbers of cells on the corresponding rows. Next n lines contain table \u0430. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai,\u2009j. It is guaranteed that all the given numbers ai,\u2009j are positive and do not exceed s. It is guaranteed that all ai,\u2009j are distinct.", "output_spec": "In the first line print a single integer m (0\u2009\u2264\u2009m\u2009\u2264\u2009s), 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,\u2009yi,\u2009pi,\u2009qi (1\u2009\u2264\u2009xi,\u2009pi\u2009\u2264\u2009n;\u00a01\u2009\u2264\u2009yi\u2009\u2264\u2009cxi;\u00a01\u2009\u2264\u2009qi\u2009\u2264\u2009cpi). The printed numbers denote swapping the contents of cells axi,\u2009yi and api,\u2009qi. 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": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Thu Oct 25 21:17:17 IST 2012\n# File Name: b.pl\n# USAGE: \n# b.pl \n# \n# \n#------------------------------------------------\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nchomp (my $n = );\nchomp (my @size = split /\\s+/, );\n\nmy ($s, @buf, @db, @sol) = (&sum (@size));\nmy ($no, $x, $y, $i, $p, $q, $found) = (0);\n\nfor ($i=0; $i < $n; $i++) {\n chomp (@buf = split /\\s+/, );\n push @db, [ @buf ];\n}\n\nfor ($x=0; $x < $n; $x++) {\n for ($y=0; $y < $size[$x]; $y++) {\n $no++;\n $found = 0;\n for ($p=$x; !$found && $p < $n; $p++) {\n for ($q=0; !$found && $q < $size[$p]; $q++) {\n if ($db[$p][$q] == $no) {\n $found = 1;\n if (!($x==$p && $y==$q)) {\n push @sol, sprintf \"%d %d %d %d\", $x+1, $y+1, $p+1, $q+1;\n ($db[$x][$y], $db[$p][$q]) = ($db[$p][$q], $db[$x][$y]);\n }\n #print STDERR \"----------------\"; print STDERR \"@{$_}\" foreach ( @db );\n }\n }\n }\n }\n}\nprint scalar @sol;\nprint join \"\\n\", @sol;\n"}], "negative_code": [{"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Thu Oct 25 21:17:17 IST 2012\n# File Name: b.pl\n# USAGE: \n# b.pl \n# \n# \n#------------------------------------------------\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nchomp (my $n = );\nchomp (my @size = split /\\s*/, );\n\nmy ($s, @buf, @db, @sol) = (&sum (@size));\nmy ($no, $x, $y, $i, $p, $q, $found) = (0);\n\nfor ($i=0; $i < $n; $i++) {\n chomp (@buf = split /\\s*/, );\n push @db, [ @buf ];\n}\n\nfor ($x=0; $x < $n; $x++) {\n for ($y=0; $y < $size[$x]; $y++) {\n $no++;\n $found = 0;\n for ($p=$x; !$found && $p < $n; $p++) {\n for ($q=0; !$found && $q < $size[$p]; $q++) {\n if ($db[$p][$q] == $no) {\n $found = 1;\n if (!($x==$p && $y==$q)) {\n push @sol, sprintf \"%d %d %d %d\", $x+1, $y+1, $p+1, $q+1;\n ($db[$x][$y], $db[$p][$q]) = ($db[$p][$q], $db[$x][$y]);\n }\n #print \"@{$_}\" foreach ( @db );\n }\n }\n }\n }\n}\nprint scalar @sol;\nprint join \"\\n\", @sol;\n"}, {"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Thu Oct 25 21:17:17 IST 2012\n# File Name: b.pl\n# USAGE: \n# b.pl \n# \n# \n#------------------------------------------------\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nchomp (my $n = );\nchomp (my @size = split /\\s*/, );\n\nmy ($s, @buf, @db) = (&sum (@size));\nmy ($no, $x, $y, $i, $p, $q, $found) = (0);\n\nfor ($i=0; $i < $n; $i++) {\n chomp (@buf = split /\\s*/, );\n push @db, [ @buf ];\n}\n\nprint $s;\nfor ($x=0; $x < $n; $x++) {\n for ($y=0; $y < $size[$x]; $y++) {\n $no++;\n $found = 0;\n for ($p=$x; !$found && $p < $n; $p++) {\n for ($q=0; !$found && $q < $size[$p]; $q++) {\n if ($db[$p][$q] == $no) {\n $found = 1;\n printf \"%d %d %d %d\\n\", $x+1, $y+1, $p+1, $q+1;\n ($db[$x][$y], $db[$p][$q]) = ($db[$p][$q], $db[$x][$y]);\n #print \"@{$_}\" foreach ( @db );\n }\n }\n }\n }\n}\n"}, {"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Thu Oct 25 21:17:17 IST 2012\n# File Name: b.pl\n# USAGE: \n# b.pl \n# \n# \n#------------------------------------------------\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nchomp (my $n = );\nchomp (my @size = split /\\s*/, );\n\nmy ($s, @buf, @db, @sol) = (&sum (@size));\nmy ($no, $x, $y, $i, $p, $q, $found) = (0);\n\nfor ($i=0; $i < $n; $i++) {\n chomp (@buf = split /\\s+/, );\n push @db, [ @buf ];\n}\n\nfor ($x=0; $x < $n; $x++) {\n for ($y=0; $y < $size[$x]; $y++) {\n $no++;\n $found = 0;\n for ($p=$x; !$found && $p < $n; $p++) {\n for ($q=0; !$found && $q < $size[$p]; $q++) {\n if ($db[$p][$q] == $no) {\n $found = 1;\n if (!($x==$p && $y==$q)) {\n push @sol, sprintf \"%d %d %d %d\", $x+1, $y+1, $p+1, $q+1;\n ($db[$x][$y], $db[$p][$q]) = ($db[$p][$q], $db[$x][$y]);\n }\n #print \"----------------\"; print \"@{$_}\" foreach ( @db );\n }\n }\n }\n }\n}\nprint scalar @sol;\nprint join \"\\n\", @sol;\n"}], "src_uid": "62df8b1821558bea910f422591618e29"} {"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$$$) \u2014 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$$$) \u2014 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$$$ \u2014 the number of stairways that Tanya climbed. In the second line, output $$$t$$$ numbers \u2014 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": "@i = 1..<>; $_ = <>;\ns/@i/$i[-1]/g, pop(@i) while @i > 1;\nprint 0 + split, \"\\n$_\";\n"}, {"source_code": "@i = 1..<>; $_ = <>;\ns/@i/$i[-1]/g, pop(@i) while @i > 1;\nprint 0 + split, \"\\n$_\";\n"}], "negative_code": [{"source_code": "<>; $_ = <>;\n@i = 1..1000;\ns/@i/$i[-1]/e, pop(@i) while @i > 1;\nprint 0+split, \"\\n$_\";\n"}], "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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the heights of the buildings.", "output_spec": "For each test case, output a single integer\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t$sum += $_ for @_;\n\t\n\tif( $sum % $_ == 0 ){\n\t\tprint 0;\n\t\t}\n\telse{\n\t\tprint 1;\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "644ef17fe304b090e0cf33a84ddc546a"} {"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\u2009\u2264\u2009k\u2009\u2264\u20091000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1\u2009\u2264\u2009|s|\u2009\u2264\u20091000, 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": "$k=<>;\n$_=<>;\ns/(\\w)/$h{$1}++/eg;\n#for(sort keys %h) {\n# print \"$_ => $h{$_}\\n\"; \n#}\nif(grep {$h{$_}%$k} keys %h) {\n print -1; \n}else{\n for(keys %h) {\n $s.=$_ x ($h{$_}/$k);\n }\n print $s x $k; \n}"}, {"source_code": "$k=<>;\n$_=<>;\nchomp;\nmap {$h{$_}++} split '';\nfor(keys %h) { \n if($h{$_}%$k){\n print -1;\n exit;\n }\n $s.=$_ x ($h{$_}/$k);\n}\nprint $s x $k;"}, {"source_code": "$k=<>;\n$_=<>;\ns/(\\w)/$h{$1}++/eg;\nfor(keys %h) {\n if($h{$_}%$k){\n print -1;\n exit;\n }\n $s.=$_ x ($h{$_}/$k);\n}\nprint $s x $k;"}, {"source_code": "\n$k=<>;\n@ch=split('',<>);\npop(@ch);\nforeach $var (@ch){\n$map{$var}++;\n\n}\n$ok=1;\nforeach $var (values%map){\nif($var%$k!=0){\n$ok=0;\nlast;\n}\n}\nif(!$ok){\nprint -1;\n}\nelse{\n\nfor($i=0;$i<$k;$i++){\nwhile(($key,$v) = each(%map) ) {\n print $key x ($v/$k) ;\n}\n\n\n\n}\n\n}"}, {"source_code": "#!/bin/perl\nuse strict;\nuse warnings;\n\nmy $k = int ;\nmy $str = ;\nchomp $str;\nmy @arr = split //, $str;\n\nmy %hash;\nmy $i;\nforeach $i ( @arr ) {\n\tif ( $hash{$i} ) {\n\t\t$hash{$i}++;\n\t} else {\n\t\t$hash{$i} = 1;\n\t}\n}\n\n$str = '';\nforeach $i ( keys %hash ) {\n\tif ( $hash{$i} % $k ) {\n\t\tprint -1;\n\t\tgoto end;\n\t} else {\n\t\t$str .= $i x ( $hash{$i} / $k );\n\t}\n}\n\nprint $str x $k;\n\nend:\n\n__END__\n\n"}], "negative_code": [], "src_uid": "f5451b19cf835b1cb154253fbe4ea6df"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009106) \u2014 length of the array. Next line contains n integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009109).", "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 = <>;\n@v = split(/ /, <>);\n\n$ans = \"Second\";\nforeach (@v) {\n\tif ($_ % 2 == 1) {\n\t\t$ans = \"First\";\n\t}\n}\n\nprint \"$ans\\n\";\n\n"}], "negative_code": [], "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\u00a0\u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of games played. The second line contains a string s, consisting of n uppercase English letters 'A' and 'D'\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse 5.012;\nuse warnings;\n\nchomp(my $a = <>);\nchomp(my $b = <>);\n\nmy $an = ($b =~ tr/A/A/);\nmy $da = ($b =~ tr/D/D/);\n\nif ($an > $da) {\n\tprint \"Anton\";\n}\nelsif ($da > $an) {\n\tprint \"Danik\";\n}\nelse {\n\tprint \"Friendship\";\n}\n"}, {"source_code": "my $n = ;\nmy $line = ;\nchomp $line;\nmy @chars = split(//, $line);\nmy $CA = 0;\nmy $CD = 0;\nfor my $i (0..$n-1) {\n\t$CA++ if $chars[$i] eq 'A';\n\t$CD++ if $chars[$i] eq 'D';\n}\nprint 'Danik' if $CD > $CA;\nprint 'Anton' if $CD < $CA;\nprint 'Friendship' if $CD == $CA;"}, {"source_code": "use strict; \nuse warnings; \n\nsub main\n{\n my $gameLen = ;\n my $result = ;\n my $anton=0;\n my $danik=0;\n my $current;\n for(my $i=0;$i<$gameLen;$i++){\n if(substr($result,$i,1) eq \"A\"){\n $anton = $anton+1;\n }\n if(substr($result,$i,1) eq \"D\"){\n $danik = $danik+1;\n }\n }\n if($anton>$danik){\n printf(\"Anton\\n\");\n }elsif($anton<$danik){\n printf(\"Danik\\n\");\n }else{\n printf(\"Friendship\\n\")\n }\n}\n\nmain();"}, {"source_code": "use strict;\nuse warnings;\n\n<>;\nmy $s=<>;\nchomp($s);\nmy $k=length($s);\n$s =~ s/A//g;\nmy $D=length($s);\nmy $A=$k-$D;\nif($D>$A)\n{\n print \"Danik\\n\";\n}\nelsif($A>$D)\n{\n print \"Anton\\n\";\n}\nelse\n{\n print \"Friendship\\n\";\n}\n"}, {"source_code": "use feature qw(switch);\n$n=<>;\n@ch=split('',<>);\n\n\n$A=0,$D=0;\nforeach $car (@ch){\ngiven($car){\n\nwhen('A') {$A++};\nwhen('D') {$D++};\n\n}\n\n}\n\nif($A>$D){\n\nprint \"Anton\";\n}\nelsif($D>$A){\nprint \"Danik\";\n}\nelse{\nprint \"Friendship\";\n}"}, {"source_code": "#!/usr/bin/perl \nuse strict;\nmy $n = ;\nmy $array =;\nchomp $array;\nmy @array_p=split(undef, $array);\nmy $A = 0;\nmy $B = 0;\nforeach my $item (@array_p)\n{\n $item eq 'A' ? $A++ : $B++ ;\n}\n$A == $B ? print 'Friendship' : $A > $B ? print 'Anton' : print 'Danik';"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\n# solve\n\nmy $n = read_token;\n\nmy $winners = read_line;\n\nmy $a = 0;\nmy $d = 0;\n\nfor (0..$n-1) {\n my $winner = substr($winners, $_, 1);\n $a++ if $winner eq 'A';\n $d++ if $winner eq 'D';\n}\n\nsay $a == $d ? 'Friendship' \n : $a > $d ? 'Anton'\n : 'Danik'\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\n# solve\n\nmy $n = read_token;\n\nmy @winners = split q{}, read_line;\n\nmy $a = 0;\nmy $d = 0;\n\nfor (@winners) {\n $a++ if $_ eq 'A';\n $d++ if $_ eq 'D';\n}\n\nsay $a == $d ? 'Friendship' \n : $a > $d ? 'Anton'\n : 'Danik'\n"}, {"source_code": "$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$A = () = /A/g;\n\t$D = () = /D/g;\n\t\n\tprint qw(Friendship Anton Danik)[ $A <=> $D ]\n\t}"}, {"source_code": "<>;\n\n$_ = <>;\n\n$A = () = /A/g;\n$D = () = /D/g;\n\t\nprint qw(Friendship Anton Danik)[ $A <=> $D ]"}], "negative_code": [{"source_code": "my $n = ;\nmy $line = ;\nchomp $line;\nmy @chars = split(//, $line);\nmy $CA = 0;\nmy $CD = 0;\nforeach my $i (@chars) {\n\t$CA++ if $chars[i] eq 'A';\n\t$CD++ if $chars[i] eq 'D';\n}\nprint 'Danik' if $CD > $CA;\nprint 'Anton' if $CD < $CA;\nprint 'Friendship' if $CD == $CA;"}, {"source_code": "#!/usr/bin/perl \nuse strict;\nmy $n = ;\nmy $array =;\nmy @array_p=split(undef, $array);\nmy $A = 0;\nmy $B = 0;\nforeach my $item (@array_p)\n{\n $item eq 'A' ? $A++ : $B++ ;\n}\n$A == $B ? print 'Friendship' : $A > $B ? print 'Anton' : print 'Danik';"}], "src_uid": "0de32a7ccb08538a8d88239245cef50b"} {"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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$k) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my @a1 = ();\r\n foreach my $i (0..($n-1)){\r\n push(@a1,+[$A[$i],$i]);\r\n }\r\n my @sa = sort { $b->[0] <=> $a->[0] } @a1;\r\n my $c = 0;\r\n my $po = -1;\r\n foreach my $i (0..($n-1)){\r\n $c++ if $sa[$i]->[1] != $po - 1;\r\n $po = $sa[$i]->[1];\r\n }\r\n if( $c <= $k ){\r\n print \"YES\\n\";\r\n } else {\r\n print \"NO\\n\";\r\n }\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$k) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my @a1 = ();\r\n foreach my $i (0..($n-1)){\r\n push(@a1,+[$A[$i],$i]);\r\n }\r\n my @sa = sort { $b->[0] <=> $a->[0] } @a1;\r\n my $c = 1;\r\n my $po = $n;\r\n foreach my $i (0..($n-1)){\r\n $c++ if $sa[$i]->[1] >= $po;\r\n $po = $sa[$i]->[1];\r\n }\r\n if( $c <= $k ){\r\n print \"YES\\n\";\r\n } else {\r\n print \"NO\\n\";\r\n }\r\n}\r\n\r\nexit(0);\r\n"}], "src_uid": "255d6fca1379ae40b03e761802f36664"} {"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,\u200910,\u200913,\u200914,\u200916] currently in the circle, the leader is child 13 and ai\u2009=\u200912, 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\u2009\u2264\u2009n\u2009\u2264\u2009100, 1\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009-\u20091). The next line contains k integer numbers a1,\u2009a2,\u2009...,\u2009ak (1\u2009\u2264\u2009ai\u2009\u2264\u2009109).", "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": "$/ = \"\"; $_ = <>; ($n, $k, @a) = split; @i = 1..$n;\nfor (1..$k) {\n\t$a = shift @a;\n\t$i = ($a + $i) % @i;\n\tprint $i[$i], \" \";\n\tsplice @i, $i, 1;\t\n\t$i = $i % @i;\t\t\n}"}], "negative_code": [], "src_uid": "5512fbe2963dab982a58a14daf805291"} {"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'\u00a0\u2014 is the $$$3$$$-rd letter of the alphabet. Consequently, Polycarp adds 3 to the code (the code becomes equal to 3); 'o'\u00a0\u2014 is the $$$15$$$-th letter of the alphabet. Consequently, Polycarp adds 15 to the code and also 0 (the code becomes 3150); 'd'\u00a0\u2014 is the $$$4$$$-th letter of the alphabet. Consequently, Polycarp adds 4 to the code (the code becomes 31504); 'e'\u00a0\u2014 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$$$) \u2014 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$$$) \u2014 the length of the given code. The second line of the description of each test case contains a string $$$t$$$ of length $$$n$$$ \u2014 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$$$ \u2014 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": "use utf8;\r\n\r\nmy $t = ;\r\nfor $_ (1..$t)\r\n{\r\n my $len = ;\r\n my $str = ;\r\n $str =~ s/(\\d0)0/chr(96 + $1)/ge;\r\n $str =~ s/(\\d\\d)0/chr(96 + $1)/ge;\r\n $str =~ s/(\\d)/chr(96 + $1)/ge;\r\n print $str;\r\n}\r\n"}], "negative_code": [], "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\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "use v5.20;\r\nuse integer;\r\nfor ( 1 .. <> ) {\r\n my ( $n, $k ) = split ' ', <>;\r\n my @p = split ' ', <>;\r\n my $sum = $p[0];\r\n my $ans = 0;\r\n for ( 1 .. $n - 1 ) {\r\n my $t = $p[$_];\r\n if ( 100 * $t > $k * $sum ) {\r\n my $r = ( 100 * $t + $k - 1 ) / $k - $sum;\r\n $ans = $r if $r > $ans;\r\n }\r\n $sum += $t;\r\n }\r\n say $ans;\r\n}"}, {"source_code": "use v5.20;\r\nuse integer;\r\nfor ( 1 .. <> ) {\r\n my ( $n, $k ) = split ' ', <>;\r\n my @p = split ' ', <>;\r\n my $sum = $p[0];\r\n my $ans = 0;\r\n for ( 1 .. $n ) {\r\n my $t = $p[$_];\r\n if ( 100 * $t > $k * $sum ) {\r\n my $r = ( 100 * $t + $k - 1 ) / $k - $sum;\r\n $ans = $r if $r > $ans;\r\n }\r\n $sum += $t;\r\n }\r\n say $ans;\r\n}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$k) = map { $_ - 0 } split(/\\s+/,);\r\n my @p = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my @a = ($p[0]); $#a = $#p;\r\n for(my $i=1;$i<$n;$i++){\r\n $a[$i] = $a[$i-1] + $p[$i];\r\n }\r\n my $s = 0;\r\n my $t = $a[0];\r\n for(my $i=1;$i<$n;$i++){\r\n if( 100 * $p[$i] <= $t * $k ){\r\n $t += $p[$i];\r\n } else {\r\n my $v = &f( 100 * $p[$i] , $k );\r\n $s += ( $v - $t );\r\n $t = $v + $p[$i];\r\n }\r\n }\r\n \r\n print \"$s\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub f {\r\n my $v = shift;\r\n my $d = shift;\r\n my $v1 = $v % $d;\r\n if( $v1 > 0 ){\r\n $v += $d - $v1;\r\n }\r\n return ( $v / $d );\r\n}\r\n\r\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\tmy( $n, $k ) = split;\n\tmy( $sum, @p ) = split ' ', <>;\n\t\n\tmy $add = 0;\n\t\n\twhile( @p ){\n\t\tmy $p_next = shift @p;\n\t\t$debug and print \"sum:[$sum], p_next:[$p_next]\";\n\t\t\n\t\tif( $sum * $k < $p_next * 100 ){\n\t\t\tmy $diff = ( int $p_next * 100 / $k ) + !!( $p_next * 100 % $k ) - $sum;\n\t\t\t$add += $diff;\n\t\t\t$sum += $diff + $p_next;\n\t\t\t}\n\t\telse{\n\t\t\t$sum += $p_next;\n\t\t\t}\n\t\t}\n\t\n\tprint $add;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$k) = map { $_ - 0 } split(/\\s+/,);\r\n my @p = map { $_ - 0 } split(/\\s+/,);\r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n \r\n my @a = ($p[0]); $#a = $#p;\r\n for(my $i=1;$i<$n;$i++){\r\n $a[$i] = $a[$i-1] + $p[$i];\r\n }\r\n my $ru = 0;\r\n for(my $i=$n-1;$i>0;$i--){\r\n unless( 100 * $p[$i] <= ( $ru + $a[$i-1] ) * $k ){\r\n my $v = &f( 100 * $p[$i] , $k );\r\n my $v1 = $v - ( $ru + $a[$i-1] );\r\n $ru += $v1;\r\n }\r\n }\r\n print \"$ru\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub f {\r\n my $v = shift;\r\n my $d = shift;\r\n my $v1 = $v % $d;\r\n if( $v1 > 0 ){\r\n $v += $d - $v1;\r\n }\r\n return ( $v / $d );\r\n}\r\n\r\n"}], "src_uid": "53975eea2503bb47bfd0a5119406aea3"} {"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\u00a0\u2014 a string s of lowercase English letters, characters \"?\" and \"*\" (1\u2009\u2264\u2009|s|\u2009\u2264\u2009105). It is guaranteed that character \"*\" occurs in s no more than once. The third line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105)\u00a0\u2014 the number of query strings. n lines follow, each of them contains single non-empty string consisting of lowercase English letters\u00a0\u2014 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": "@_ = <>; chomp @_; ($g, $t, $n, @q) = @_;\n$y = ($t =~ /\\*/); $l = length($t) - $y;\n$t =~ s{\\?} {\\[$g\\]}g;\n$t =~ s{\\*} {[^$g]*};\n\nfor (@q) {\n\t$r = (length >= $l? /^$t$/: 0);;\n\tprint $r? YES: NO, \"\\n\";\n}\n\n"}, {"source_code": "#!/usr/bin/perl\n\n# http://perlmonks.org/?node_id=1196089\n\nuse strict;\nuse warnings;\n\n$\\ = $/;\n\nwhile(<>)\n {\n my $dict = $_;\n my $no_dict = ( join '', ' ', 'a' .. 'z' ) =~ s/[$dict]//gr;\n\n $_ = <>;\n\n my($head, $tail) = map length( $_ // '' ), split /\\*/;\n\n s/\\?/[$dict]/g;\n s/\\*/ [$no_dict]* /;\n\n my $qr = qr/^$_/;\n\n $_ = <>,\n $head <= length($_) && substr($_, $head, 0, ' '),\n $tail && $tail <= length($_) && substr($_, -$tail, 0, ' '),\n print $_ =~ $qr ? \"YES\" : \"NO\"\n for 1 .. <>;\n }\n"}, {"source_code": "#!/usr/bin/perl\n\n# http://perlmonks.org/?node_id=1196089\n\nuse strict;\nuse warnings;\n\n$\\ = $/;\n\nwhile(<>)\n {\n chomp;\n my $dict = $_;\n\n $_ = <>;\n\n my($head, $tail) = map length( $_ // '' ), split /\\*/;\n\n s/\\?/[$dict]/g;\n s/\\*/ [^$dict ]* /;\n\n my $qr = qr/^$_/;\n\n $_ = <>,\n $head <= length($_) && substr($_, $head, 0, ' '),\n $tail && $tail <= length($_) && substr($_, -$tail, 0, ' '),\n print $_ =~ $qr ? \"YES\" : \"NO\"\n for 1 .. <>;\n }\n"}, {"source_code": "#!/usr/bin/perl\n\n# http://perlmonks.org/?node_id=1196089\n\nuse strict;\nuse warnings;\n\n$\\ = $/;\n\nwhile(<>)\n {\n my $dict = $_;\n my $no_dict = ( join '', ' ', 'a' .. 'z' ) =~ s/[$dict]//gr;\n\n $_ = <>;\n\n my($head, $tail) = map length( $_ // '' ), split /\\*/;\n\n s/\\?/[$dict]/g;\n s/\\*/ [$no_dict]* /;\n\n my $qr = qr/^$_/;\n\n $_ = <>,\n $head <= length($_) && substr($_, $head, 0, ' '),\n $tail && $tail <= length($_) && substr($_, -$tail, 0, ' '),\n print $_ =~ $qr ? \"YES\" : \"NO\"\n for 1 .. <>;\n }"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\tmy $dict = $_;\n\t$_ = <>;\n\t\n\ts/\\?/[$dict]/g;\n\ts/\\*/[^$dict]*/;\n\t\n\tmy $qr = qr/^$_$/;\n\t\n\tprint <> =~ $qr ? \"YES\" : \"NO\" for 1 .. <>;\n\t}"}], "negative_code": [], "src_uid": "c6633581d7424d670eaa0f8a5c8cc366"} {"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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains the integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$)\u00a0\u2014 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": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\nmy $debug = 0;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$debug and print '-' x 15;\r\n\t\r\n\t$_ = <>;\r\n\t\r\n\tmy $prev = 0;\r\n\tmy $new = '';\r\n\tmy $ans = '';\r\n\t\r\n\t/\r\n\t.\r\n\t(?{\r\n\t my $plus = $& + 1 != $prev ? 1 : 0;\r\n\t $new .= $& + $plus;\r\n\t $ans .= $plus;\r\n\t $prev = $& + $plus;\r\n\t })\r\n\t(*F)\r\n\t/x;\r\n\t\r\n\tprint $ans;\r\n\t}"}], "negative_code": [], "src_uid": "e27620d3a43ab42edf930b37ce214c9e"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the initial array.", "output_spec": "For each test case, output a single integer\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $even = 0;\n\tmy $odd = 0;\n\tmy $nok = 0;\n\t\n\tmy $j = 0;\n\tfor my $i ( @_ ){\n\t\t$i % 2 == 0 ? $even ++ : $odd ++;\n\t\t$i % 2 != $j % 2 and $nok ++;\n\t\t$j ++;\n\t\t}\n\t\n\tmy $need_even = ( @_ + 1 ) >> 1;\n\t\n\t$debug and print \"[$even][$odd][$nok][$need_even]\";\n\t\n\tif( $need_even == $even ){\n\t\tprint $nok / 2;\n\t\t}\n\telse{\n\t\tprint -1;\n\t\t}\n\t\n\t}"}], "negative_code": [], "src_uid": "942123e43a83d5a4cea95a6781064e28"} {"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\u2009\u2264\u20092\u00b7x 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of measurements Vasya made. The second line contains n integers c1,\u2009c2,\u2009...,\u2009cn (1\u2009\u2264\u2009ci\u2009\u2264\u20095000) \u2014 the results of the measurements. The numbers on the second line are separated by single spaces.", "output_spec": "Print a single integer \u2014 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": "#!/usr/bin/perl -w\nuse strict;\nuse warnings;\nopen(INPUT, \"< input.txt\");\nopen(OUTPUT, \"> output.txt\");\nmy $n = int ;\nmy @c = sort {$a <=> $b} map int, split /\\D/, ;\nmy ($i1, $i2, $max) = (0, 0, 0);\ndo {\n while (($i2 < $n) && ($c[$i1]*2 >= $c[$i2])) {\n\t$i2++;\n\t$max++;\n }\n $i1++;\n $i2++;\n\n} while ($i1 + $max < $n);\nprint OUTPUT $n - $max;\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w\nuse strict;\nuse warnings;\nopen(INPUT, \"< input.txt\");\nopen(OUTPUT, \"> output.txt\");\nmy $n = int ;\nmy @c = sort {$a <=> $b} map int, split /\\D/, ;\nmy ($i1, $i2, $max) = (-1, -1, 1);\ndo {\n $i1++;\n $i2++;\n while ($c[$i1]*2 >= $c[$i2] and $i2 < $n-1) {\n\tprint \"$i2 \";\n\t$i2++;\n\t$max++;\n }\n} while ($i1 + $max < $n and $i2 < $n-1);\nprint OUTPUT $n - $max;\n"}], "src_uid": "80cf3ea119fd398e8f003d22a76895a7"} {"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\" \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009100), 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 (\u2009-\u2009100\u2009\u2264\u2009xi,\u2009yi,\u2009zi\u2009\u2264\u2009100).", "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": "#!/usr/bin/perl\n\nuse 5.012;\nuse warnings;\n\nmy $num = <>;\nmy ($x,$y,$z);\nwhile ($num--) {\n\tmy ($a,$b,$c) = split / /, <>;\n\t$x += $a; \n\t$y += $b;\n\t$c += $c;\n}\nprint !$x && !$y && !$z ? 'YES' : 'NO';\n"}, {"source_code": "my $n = int<>;\nmy @arr;\nmy @ans = (0, 0, 0);\npush @arr, [split \" \", <>] for(1..$n);\nfor my $i(0..$n-1) {\n $ans[$_] += $arr[$i][$_] for(0..2);\n}\nprint (!$ans[0] && !$ans[1] && !$ans[2] ? \"YES\\n\" : \"NO\\n\");"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nchomp($n = <>);\nour ($x, $y, $z) = (0) x 3;\nwhile ($n-- > 0) {\n\t($xx, $yy, $zz) = split / /, <>;\n\t$x += $xx;\n\t$y += $yy;\n\t$z += $zz;\n}\n$x==0 and $y==0 and $z==0 and say \"YES\" or say \"NO\";"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nmy $n = <>;\nmy ($x, $y, $z) = (0, 0, 0);\nfor (1..$n) {\n my @a = split ' ', <>;\n $x += $a[0];\n $y += $a[1];\n $z += $a[2];\n}\nprint $x | $y | $z ? \"NO\\n\" : \"YES\\n\";\n"}, {"source_code": "#!/bin/perl\n\n$n = ;\n\nfor($i=0;$i<$n;$i++)\n{\n\n $s = ;\n\n ($a, $b, $c) = split / +/, $s;\n\n $x += $a;\n $y += $b;\n $z += $c;\n\n}\n\nif( !$x && !$y && !$z )\n{\n\n print \"YES\";\n\n}\nelse\n{\n\n print \"NO\";\n\n}\n"}, {"source_code": "my $num = <>;\nchomp $num;\nmy @force = (0, 0, 0);\nfor (1..$num) {\n my $line = <>;\n chomp $line;\n my @new = split ' ', $line;\n $force[$_] += $new[$_] for (0..2);\n}\nmy $flag=1;\nfor(0..2) {\n $flag = 0 if $force[$_];\n}\n$flag ? print \"YES\" : print \"NO\";"}], "negative_code": [], "src_uid": "8ea24f3339b2ec67a769243dc68a47b2"} {"nl": {"description": "A sequence a0,\u2009a1,\u2009...,\u2009at\u2009-\u20091 is called increasing if ai\u2009-\u20091\u2009<\u2009ai for each i:\u20090\u2009<\u2009i\u2009<\u2009t.You are given a sequence b0,\u2009b1,\u2009...,\u2009bn\u2009-\u20091 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\u2009\u2264\u2009n\u2009\u2264\u20092000,\u20091\u2009\u2264\u2009d\u2009\u2264\u2009106). The second line contains space separated sequence b0,\u2009b1,\u2009...,\u2009bn\u2009-\u20091 (1\u2009\u2264\u2009bi\u2009\u2264\u2009106).", "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@_=split/ /,<>;\n$k=shift @_;\n$n=0;\nfor (@_){\nwhile($_<=$k){ \nwhile ($_+100000*$' <= $k) { $_+=100000*$'; $n+=100000 }\nwhile ($_+10000*$' <= $k) { $_+=10000*$'; $n+=10000 }\nwhile ($_+1000*$' <= $k) { $_+=1000*$'; $n+=1000 }\nwhile ($_+100*$' <= $k) { $_+=100*$'; $n+=100 }\nwhile ($_+10*$' <= $k) { $_+=10*$'; $n+=10 }\n$_+=$'; $n++\n} \n$k=$_;\n}\nprint $n"}, {"source_code": "use List::Util qw[min max];$an=0;$x=0;$i=0;@a=split(' ',<>);@l=split(' ',<>);$c=0;$p=0;for(1..@a[0]){$c=max(0,int(($p-@l[$i]+@a[1])/@a[1]));$p=@l[$i]+$c*@a[1];$an+=$c;$i+=1;}print \"$an\\n\";\n"}], "negative_code": [], "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\u2009\u2264\u2009i\u2009\u2264\u2009n) 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\u2009\u2264\u2009s\u2009\u2264\u2009104, 1\u2009\u2264\u2009n\u2009\u2264\u2009103). Then n lines follow: the i-th line contains space-separated integers xi and yi (1\u2009\u2264\u2009xi\u2009\u2264\u2009104, 0\u2009\u2264\u2009yi\u2009\u2264\u2009104) \u2014 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\u2009+\u200999\u2009=\u2009101. 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": "my ($x, $n) = split \" \", <>;\nmy ($str, $bonus) = (0, 0);\nmy $ans = \"YES\\n\";\nmy @arr;\npush @arr, [split \" \", <>] for (1..$n);\n@arr = sort {$a->[0] <=> $b->[0]} @arr;\n# print \"$arr[$_][0] $arr[$_][1]\\n\" for(0..$n-1);\nfor my $i(0..$n-1) {\n if ($x > $arr[$i][0]) {\n $x += $arr[$i][1];\n }\n else {\n $ans = \"NO\\n\";\n }\n}\nprint $ans;\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n($s, $n) = split / /, <>;\npush @arr, [split / /, <>] foreach(1..$n);\n@arr = sort {$a->[0] <=> $b->[0]} @arr;\nforeach my $d (@arr) {\n\t# say \"$d \", $d->[0], \" \", $d->[1];\n\t$s<=$d->[0] and say \"NO\" and exit;\n\t$s += $d->[1];\n}\nsay \"YES\";\n"}, {"source_code": "use strict;\nuse warnings;\n\n$\\ = \"\\n\";\n\nmy ($s, $n) = split ' ', <>;\nmy @dragons;\nfor (1..$n) {\n push @dragons, [split ' ', <>];\n}\n\n@dragons = sort { $a->[0] <=> $b->[0] } @dragons;\n\nmy $dead;\nfor my $dragon (@dragons) {\n my ($strength, $increment) = @$dragon;\n if ($s > $strength) {\n $s += $increment;\n } else {\n $dead = 1;\n last;\n }\n}\n\nprint $dead ? 'NO' : 'YES';\n"}, {"source_code": "$DEBUG = 0;\nchomp($line=);\n($str, $n) = $line =~ /(\\d+)\\s+(\\d+)/;\n\nmy @dragons = ();\nwhile($n-- > 0) {\n\tchomp($line=);\n\t($dstr, $inc) = $line =~ /(\\d+)\\s+(\\d+)/;\n\tpush(@dragons, {strength=>$dstr, powerup => $inc});\n}\n\nforeach $dragon (sort by_strength @dragons) {\n\tprint \"Battle with $str against \".$dragon->{strength}.\"\\n\" if $DEBUG;\n\tif($str <= $dragon->{strength}) {\n\t\tprint \"NO\\n\";\n\t\texit; \n\t}\n\t$str += $dragon->{powerup};\n}\nprint \"YES\\n\";\n\nsub by_strength {\n\t$a->{strength} <=> $b->{strength};\n}"}], "negative_code": [{"source_code": "\nmy ($x, $n) = split \" \", <>;\nmy ($str, $bonus) = (0, 0);\nmy $ans = \"YES\\n\";\nmy @arr;\npush @arr, [split \" \", <>] for (1..$n);\n# print \"$arr[$_][0] $arr[$_][1]\\n\" for(0..$n-1);\n@arr = sort {@{$a}[0] > @{$b}[0]} @arr;\n@arr = reverse @arr;\n@arr = sort {@{$a}[0] > @{$b}[0]} @arr;\n# print \"$arr[$_][0] $arr[$_][1]\\n\" for(0..$n-1);\nfor my $i(0..$n-1) {\n # print \"$x $arr[$i][0] $arr[$i][1]\\n\";\n if ($x > $arr[$i][0]) {\n $x += $arr[$i][1];\n }\n else {\n $ans = \"NO\\n\";\n }\n}\nprint $ans;\n"}, {"source_code": "\nmy ($x, $y) = split \" \", <>;\nmy ($str, $bonus, $n) = (0, 0, 0);\nmy @arr;\nwhile(<>) {\n push @arr, [split \" \", $_];\n $n++;\n}\n@arr = sort {@{$a}[0] > @{$b}[0]} @arr;\nmy $ans = \"YES\\n\";\nfor my $i(0..$n-1) {\n if ($x > $arr[$i][0]) {\n $x += $arr[$i][1];\n }\n else {\n $ans = \"NO\\n\";\n }\n}\nprint $ans;\n"}, {"source_code": "my ($x, $n) = split \" \", <>;\nmy ($str, $bonus) = (0, 0);\nmy @arr;\nwhile(<>) {\n push @arr, [split \" \", $_];\n}\n@arr = sort {@{$a}[0] > @{$b}[0]} @arr;\nmy $ans = \"YES\\n\";\nfor my $i(0..$n-1) {\n if ($x > $arr[$i][0]) {\n $x += $arr[$i][1];\n }\n else {\n $ans = \"NO\\n\";\n }\n}\nprint $ans;\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n($s, $n) = split / /, <>;\npush @arr, [split / /, <>] foreach(1..$n);\n@arr = sort {$a->[0] <=> $b->[0]} @arr;\nforeach my $d (@arr) {\n\t# say \"$d \", $d->[0], \" \", $d->[1];\n\t$s<$d->[0] and say \"NO\" and exit;\n\t$s += $d->[1];\n}\nsay \"YES\";\n"}, {"source_code": "use strict;\nuse warnings;\n\n$\\ = \"\\n\";\n\nmy ($s, $n) = split ' ', <>;\nmy @dragons;\nfor (1..$n) {\n push @dragons, [split ' ', <>];\n}\n\nmy $dead;\nfor my $dragon (@dragons) {\n my ($strength, $increment) = @$dragon;\n if ($s > $strength) {\n $s += $increment;\n } else {\n $dead = 1;\n last;\n }\n}\n\nprint $dead ? 'NO' : 'YES';\n"}], "src_uid": "98f5b6aac08f48f95b2a8ce0738de657"} {"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 \u2014 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": "for(1..<>){<>;@a=split/\\s/,<>;$q=0;for(0..$#a){$q++if@a[$_]&&!($_&&@a[$_-1])}print$q>2?2:$q,\" \"}"}, {"source_code": "for(1..<>){$n=<>;@a=split/\\s/,<>;$q=0;for(0..$n-1){$q++if@a[$_]&&!($_&&@a[$_-1])}print$q>2?2:$q,\" \"}"}], "negative_code": [], "src_uid": "da3d1b2615d51044a7b43bd0ce939f4c"} {"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$$$)\u00a0\u2014 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\u00a0\u2014 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": "for(1..<>)\r\n{\r\n <>;\r\n chomp(my $in = <>);\r\n my @a = split / /, $in;\r\n my $s=0;\r\n for (my $i = 1; $i < scalar @a; $i++) {\r\n my $max = @a[$i] > @a[$i-1] ? @a[$i] : @a[$i-1];\r\n my $min = @a[$i] < @a[$i-1] ? @a[$i] : @a[$i-1];\r\n while ($min < $max/2) {\r\n $min *= 2;\r\n $s++;\r\n }\r\n }\r\n print $s, \"\\n\";\r\n}\r\nexit;"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $c = 0;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tmy( $A, $B ) = sort { $a <=> $b } @_[ $i, $i + 1 ];\n\t\t\n\t\twhile( $B > $A * 2 ){\n\t\t\t$A *= 2;\n\t\t\t$c ++;\n\t\t\t}\n\t\t}\n\t\n\tprint $c;\n\t}"}], "negative_code": [{"source_code": "for(1..<>) {\r\n chomp(my $n = <>);\r\n if ($n == 1) { print \"1\\n1\\n\"; next;}\r\n \r\n $i = 0;\r\n my @ans = ();\r\n my $x = 1;\r\n while ($n > $x) {\r\n if ($x = $n%10**++$i) {\r\n push(@ans, $x);\r\n $n -= $x;\r\n }\r\n }\r\n print scalar(@ans), \"\\n\";\r\n print join \" \", @ans, \"\\n\";\r\n}\r\nexit;"}, {"source_code": "for(1..<>)\r\n{\r\n chomp($in = <>);\r\n ($a, $b) = split / /, $in;\r\n $c = abs($a - $b);\r\n my $s = 0;\r\n $s += int($c/10);\r\n if ($s%10) {\r\n $s += 1;\r\n }\r\n print $s, \"\\n\";\r\n}"}], "src_uid": "a544dd9fd96379f66a960de6f973dd50"} {"nl": {"description": "Alice and Bob received $$$n$$$ candies from their parents. Each candy weighs either 1 gram or 2 grams. Now they want to divide all candies among themselves fairly so that the total weight of Alice's candies is equal to the total weight of Bob's candies.Check if they can do that.Note that candies are not allowed to be cut in half.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the number of candies that Alice and Bob received. The next line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$\u00a0\u2014 the weights of the candies. The weight of each candy is either $$$1$$$ or $$$2$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, output on a separate line: \"YES\", if all candies can be divided into two sets with the same weight; \"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\n2\n1 1\n2\n1 2\n4\n1 2 1 2\n3\n2 2 2\n3\n2 1 2"], "sample_outputs": ["YES\nNO\nYES\nNO\nNO"], "notes": "NoteIn the first test case, Alice and Bob can each take one candy, then both will have a total weight of $$$1$$$.In the second test case, any division will be unfair.In the third test case, both Alice and Bob can take two candies, one of weight $$$1$$$ and one of weight $$$2$$$.In the fourth test case, it is impossible to divide three identical candies between two people.In the fifth test case, any division will also be unfair."}, "positive_code": [{"source_code": "for (1..<>) {\r\n chomp (my $n = <>);\r\n chomp (my $in = <>);\r\n @a = split / /, $in;\r\n $s = 0;\r\n $oc = 0;\r\n foreach (@a) {\r\n $s+= $_;\r\n $_ == 1 and $oc++;\r\n }\r\n if ($s%2 || ($oc == 0 && $n%2)) {\r\n print \"NO\\n\";\r\n } else {\r\n print \"YES\\n\";\r\n }\r\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h = qw( 1 0 2 0 );\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\t$h{ 2 } %= 2;\n\t\n\tif( $h{ 1 } > 2 ){\n\t\t$h{ 1 } %= 2;\n\t\t$h{ 1 } or $h{ 1 } = 2;\n\t\t}\n\t\n\tprint do {\n\t\tif( 0 ){ ; }\n\t\telsif( $h{ 2 } == 0 and $h{ 1 } == 0 ){ \"YES\" }\n\t\telsif( $h{ 2 } == 0 and $h{ 1 } == 1 ){ \"NO\" }\n\t\telsif( $h{ 2 } == 0 and $h{ 1 } == 2 ){ \"YES\" }\n\t\telsif( $h{ 2 } == 1 and $h{ 1 } == 0 ){ \"NO\" }\n\t\telsif( $h{ 2 } == 1 and $h{ 1 } == 1 ){ \"NO\" }\n\t\telsif( $h{ 2 } == 1 and $h{ 1 } == 2 ){ \"YES\" }\n\t\t};\n\t}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my $s = 0;\r\n my $n1 = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n $s += $A[$i];\r\n $n1++ if $A[$i]==1;\r\n }\r\n if( $s % 2 == 0 ){\r\n if( $n % 2 == 1 and $n1 == 0 ){\r\n print \"NO\\n\";\r\n } else {\r\n print \"YES\\n\";\r\n }\r\n } else {\r\n print \"NO\\n\";\r\n }\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "src_uid": "1d9d34dca11a787d6e2345494680e717"} {"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$$$) \u2014 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)$$$) \u2014 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": "#/usr/bin/perl\n\nuse strict;\nuse warnings;\n\n<>;\n\nwhile (<>) {\n\tm/(\\d+)\\s+(\\d+)/;\n\tmy ($n, $k) = ($1, $2);\n\tdoit($n, $k);\n}\n\nsub nth_char {\n\treturn chr ((ord 'a') + shift() - 1);\n}\n\nsub nth0_char {\n\treturn chr (shift() + ord('a'));\n}\n\n\nsub doit {\n\tmy ($n, $k) = @_;\n\tforeach my $i (0 .. ($n - 1)) {\n\t\tmy $j = $i % $k;\n\t\tprint nth0_char($j);\n\t}\n\tprint \"\\n\"; \n}\n"}], "negative_code": [], "src_uid": "fa253aabcccd487d09142ea882abbc1b"} {"nl": {"description": "Polycarpus has an array, consisting of n integers a1,\u2009a2,\u2009...,\u2009an. 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\u2009\u2260\u2009j); he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai\u2009=\u2009ai\u2009+\u20091 and aj\u2009=\u2009aj\u2009-\u20091. 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the array size. The second line contains space-separated integers a1,\u2009a2,\u2009...,\u2009an (|ai|\u2009\u2264\u2009104) \u2014 the original array.", "output_spec": "Print a single integer \u2014 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": "#!/usr/bin/perl -w\n\nuse strict;\nuse List::Util qw(sum);\n\nchomp(my $n = <>);\nchomp($_ = <>);\nmy $res = $n - ((sum split) % $n == 0 ? 0 : 1);\nprint $res, \"\\n\";\n"}, {"source_code": "use List::Util qw(sum);\n$n=<>;\n$_=<>;\nprint((sum split)%$n?$n-1:$n)\n"}, {"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Wed Nov 21 21:21:55 IST 2012\n# File Name: b.pl\n# USAGE: \n# b.pl \n# \n# \n#------------------------------------------------\nchomp (my $n = );\nchomp (my $sum = eval join '+', split /\\s+/, );\n\nif ($sum % $n == 0) {\n print $n\n} else {\n print 1 > $n - 1 ? 1 : $n - 1\n}\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\nwhile (<>){\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$_=<>; chomp;\ns/-?\\d+/$sum+=$&/eg;\ns/\\S//g; s/$/ /;\n$sum+= 1e4 * length;\nprint (($sum % length )? (-1+ length): (length))\n\n}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\nsub fact{\n\tmy $i= shift; my $j= 1;\n\t$j*=$i-- while $i;\n\treturn $j\n}\n\n# Transpose \"charbox\"\nsub trans{\n\tmy @t=(); my $i=0; \n\t$i=0, s/./$t[$i++].=$&/eg for @_;\n\treturn @t\n}\n\nsub rotL{\n\tmy @t=(); my $i=0; my $j;\n\tfor (@_){\n\t\t$i=0, $j=length; $t[$i++].=chop while $j--\n\t}\n\treturn @t\n}\n\nsub rotR{\n my @m=@_; chomp @m; my @t=();\n\tmy $i= length $m[0];\n\twhile ($i--){\n\t\t$t[$i]=~s/^/chop/e for @m\n\t}\nreturn @t\n}\n\nsub revlines{\n\t$_=reverse $_ for @_;\n\treturn @_\n}\n\n# Transpose \"value sets\" ## slow?\nsub trans_s{\n\tmy @t=(); my $j;\n\t$j=0, s/\\S+/($t[$j++].=\"$& \")?$&:$&/eg for @_;\n\tchop @t;\nreturn @t\n}\n\n# rotL_s\n# rotR_s\n\n# slow?\nsub revlines_s{\n\t$_=join\" \", reverse split/\\s+/ \tfor @_;\n\treturn @_\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n\tmy @m=sort @_; my @u;\n\tpush @u, my $i=shift @m;\n\t$i eq $_ or push @u, $i=$_ for @m;\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n"}, {"source_code": "<>;\n$sum+=$_ for @_=split/ /,<>;\n$sum+= 1e4 * @_;\nprint (($sum % @_ )? (-1+ @_): (0+ @_))\n"}], "negative_code": [{"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Wed Nov 21 21:21:55 IST 2012\n# File Name: b.pl\n# USAGE: \n# b.pl \n# \n# \n#------------------------------------------------\nchomp (my $n = );\nchomp (my $sum = eval join '+', split /\\s+/, );\n\nprint $n - 1;\n#int $sum / $n;\n"}, {"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Wed Nov 21 21:21:55 IST 2012\n# File Name: b.pl\n# USAGE: \n# b.pl \n# \n# \n#------------------------------------------------\nchomp (my $n = );\nchomp (my $sum = eval join '+', split /\\s+/, );\n\nprint 1 > $n - 1 ? 1 : $n - 1;\n"}, {"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Wed Nov 21 21:21:55 IST 2012\n# File Name: b.pl\n# USAGE: \n# b.pl \n# \n# \n#------------------------------------------------\nchomp (my $n = );\nchomp (my $sum = eval join '+', split /\\s+/, );\n\nprint int $sum / $n;\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\nwhile (<>){\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n\n$n=@_=split/ /,<>;\n$x=1;\nfor ($i=0; $i<$n; $i++){\n$sum = 0;\n\tfor ($j=$i; $j<$n; $j++){\n\t$sum += $_[$j]; \n#\t\tprint \"$sum\";\n\t$k=$j-$i+1;\n\t$sum % $k or $k>$x and $x=$k;\n#\t\tprint \" $x\\n\";\n\t}\n}\nprint \"$x\";\n\n}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\nsub fact{\n\tmy $i= shift; my $j= 1;\n\t$j*=$i-- while $i;\n\treturn $j\n}\n\n# Transpose \"charbox\"\nsub trans{\n\tmy @t=(); my $i=0; \n\t$i=0, s/./$t[$i++].=$&/eg for @_;\n\treturn @t\n}\n\nsub rotL{\n\tmy @t=(); my $i=0; my $j;\n\tfor (@_){\n\t\t$i=0, $j=length; $t[$i++].=chop while $j--\n\t}\n\treturn @t\n}\n\nsub rotR{\n my @m=@_; chomp @m; my @t=();\n\tmy $i= length $m[0];\n\twhile ($i--){\n\t\t$t[$i]=~s/^/chop/e for @m\n\t}\nreturn @t\n}\n\nsub revlines{\n\t$_=reverse $_ for @_;\n\treturn @_\n}\n\n# Transpose \"value sets\" ## slow?\nsub trans_s{\n\tmy @t=(); my $j;\n\t$j=0, s/\\S+/($t[$j++].=\"$& \")?$&:$&/eg for @_;\n\tchop @t;\nreturn @t\n}\n\n# rotL_s\n# rotR_s\n\n# slow?\nsub revlines_s{\n\t$_=join\" \", reverse split/\\s+/ \tfor @_;\n\treturn @_\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n\tmy @m=sort @_; my @u;\n\tpush @u, my $i=shift @m;\n\t$i eq $_ or push @u, $i=$_ for @m;\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\nwhile (<>){\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n\n$n=@_=split/ /,<>;\n$_+=1e4 for @_;\n$x=1;\nfor ($i=0; $i<$n; $i++){\n$sum = 0;\n\tfor ($j=$i; $j<$n; $j++){\n\t$sum += $_[$j]; \n#\t\tprint \"$sum\";\n\t$k=$j-$i+1;\n\t$sum % $k or $k>$x and $x=$k;\n#\t\tprint \" $x\\n\";\n\t}\n}\nprint \"$x\";\n\n}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\nsub fact{\n\tmy $i= shift; my $j= 1;\n\t$j*=$i-- while $i;\n\treturn $j\n}\n\n# Transpose \"charbox\"\nsub trans{\n\tmy @t=(); my $i=0; \n\t$i=0, s/./$t[$i++].=$&/eg for @_;\n\treturn @t\n}\n\nsub rotL{\n\tmy @t=(); my $i=0; my $j;\n\tfor (@_){\n\t\t$i=0, $j=length; $t[$i++].=chop while $j--\n\t}\n\treturn @t\n}\n\nsub rotR{\n my @m=@_; chomp @m; my @t=();\n\tmy $i= length $m[0];\n\twhile ($i--){\n\t\t$t[$i]=~s/^/chop/e for @m\n\t}\nreturn @t\n}\n\nsub revlines{\n\t$_=reverse $_ for @_;\n\treturn @_\n}\n\n# Transpose \"value sets\" ## slow?\nsub trans_s{\n\tmy @t=(); my $j;\n\t$j=0, s/\\S+/($t[$j++].=\"$& \")?$&:$&/eg for @_;\n\tchop @t;\nreturn @t\n}\n\n# rotL_s\n# rotR_s\n\n# slow?\nsub revlines_s{\n\t$_=join\" \", reverse split/\\s+/ \tfor @_;\n\treturn @_\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n\tmy @m=sort @_; my @u;\n\tpush @u, my $i=shift @m;\n\t$i eq $_ or push @u, $i=$_ for @m;\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}\n"}, {"source_code": "$sum+=$_ for @_=split/ /,<>;\n$sum+= 1e4 * @_;\nprint (($sum % @_ )? (-1+ @_): (0+ @_))"}], "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)$$$ \u2014 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)$$$ \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nOUTER:\nwhile(<>){\n\t\n\t@_ = split ' ', <>;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tif( $_[ $i ] < $i ){\n\t\t\tprint \"NO\";\n\t\t\tnext OUTER;\n\t\t\t}\n\t\telse{\n\t\t\t$_[ $i + 1 ] += $_[ $i ] - $i;\n\t\t\t}\n\t\t}\n\t\n\tprint \"YES\";\n\t}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @h = map { $_ - 0 } split(/\\s+/,);\r\n my $c = 0;\r\n my $r = 'YES';\r\n for(my $i=0;$i<$n;$i++){\r\n if( $h[$i] < $i ){\r\n if( $c < $i - $h[$i] ){\r\n $r = 'NO';\r\n last;\r\n }\r\n $c -= $i - $h[$i];\r\n } else {\r\n $c += $h[$i] - $i;\r\n }\r\n }\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nOUTER:\nwhile(<>){\n\t\n\tif( $_ == 1 ){\n\t\tprint \"NO\";\n\t\t<>;\n\t\tnext;\n\t\t}\n\t\n\t@_ = split ' ', <>;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tif( $_[ $i ] < $i ){\n\t\t\tprint \"NO\";\n\t\t\tnext OUTER;\n\t\t\t}\n\t\telse{\n\t\t\t$_[ $i + 1 ] += $_[ $i ] - $i;\n\t\t\t}\n\t\t}\n\t\n\tprint \"YES\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t\n\tif( $_ == 1 ){\n\t\tprint \"NO\";\n\t\t<>;\n\t\tnext;\n\t\t}\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\t$sum += $_ for @_;\n\t\n\tif( @_ * ( @_ - 1 ) / 2 > $sum ){\n\t\tprint \"NO\";\n\t\t}\n\telse{\n\t\tprint \"YES\";\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t\n\tif( $_ == 1 ){\n\t\tprint \"NO\";\n\t\tnext;\n\t\t}\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\t$sum += $_ for @_;\n\t\n\tif( @_ * ( @_ - 1 ) / 2 > $sum ){\n\t\tprint \"NO\";\n\t\t}\n\telse{\n\t\tprint \"YES\";\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\t$sum += $_ for @_;\n\t\n\tif( @_ * ( @_ - 1 ) / 2 > $sum ){\n\t\tprint \"NO\";\n\t\t}\n\telse{\n\t\tprint \"YES\";\n\t\t}\n\t\n\t\n\t}"}], "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$$$) \u2014 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 \u2014 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": "for(1..<>){($n,$m)=split/\\s/,<>;@a=split/\\s/,<>;$s=0;for(@a){$s+=$_};print$s>$m?$s-$m:0,\"\\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$$$) \u2014 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 \u2014 the number of operations. Then print $$$m$$$ lines, each line consists of two integers $$$i$$$ and $$$j$$$ \u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my @B = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my $asum = 0; my $bsum = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n $asum += $A[$i];\r\n $bsum += $B[$i];\r\n }\r\n \r\n if( $asum != $bsum ){\r\n print \"-1\\n\"; next;\r\n }\r\n if( $asum == 0 ){\r\n print \"0\\n\"; next;\r\n }\r\n \r\n my @dw = (); $#dw = $n - 1;\r\n my @up = (); $#up = $n - 1;\r\n \r\n my $dd = 0;\r\n my @ot = ();\r\n for(my $i=0;$i<$n;$i++){\r\n if( $A[$i] > $B[$i] ){\r\n my $d = $A[$i] - $B[$i];\r\n $dw[$i] = $d;\r\n $dd += $d;\r\n } elsif( $A[$i] < $B[$i] ){\r\n my $d = $B[$i] - $A[$i];\r\n $up[$i] = $d;\r\n }\r\n }\r\n \r\n print \"$dd\\n\";\r\n for(my $i=0;$i<$dd;$i++){\r\n my $d_i = -1;\r\n for(my $j=0;$j<$n;$j++){\r\n if( $dw[$j]>0 ){\r\n $d_i = $j; last;\r\n }\r\n }\r\n my $u_i = -1;\r\n for(my $k=0;$k<$n;$k++){\r\n if( $up[$k]>0 ){\r\n $u_i = $k; last;\r\n }\r\n }\r\n print ( ( 1 + $d_i ) . \" \" . ( 1 + $u_i ) . \"\\n\" );\r\n $dw[$d_i] --;\r\n $up[$u_i] --;\r\n }\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my @B = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my $asum = 0; my $bsum = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n $asum += $A[$i];\r\n $bsum += $B[$i];\r\n }\r\n \r\n if( $asum != $bsum ){\r\n print \"-1\\n\"; next;\r\n }\r\n if( $asum == 0 ){\r\n print \"0\\n\"; next;\r\n }\r\n \r\n my @ot = ();\r\n my $ai = $n-1;\r\n while( $A[$ai]==0 ){ $ai -- ; }\r\n for(my $i=0;$i<$n;$i++){\r\n if( $A[$i] > $B[$i] ){\r\n my $d = $A[$i] - $B[$i];\r\n $A[$i] = $B[$i];\r\n $A[$n-1] += $d;\r\n for(my $j=0;$j<$d;$j++){\r\n push(@ot,+[$i,$n-1]);\r\n }\r\n $ai = $n-1;\r\n } elsif( $A[$i] < $B[$i] ){\r\n my $d = $B[$i] - $A[$i];\r\n $ai = $n - 1;\r\n for(my $j=0;$j<$d;$j++){\r\n while($A[$ai]==0){ $ai -- ; }\r\n push(@ot,+[$ai,$i]);\r\n $A[$i] ++;\r\n $A[$ai] --;\r\n }\r\n }\r\n }\r\n print ( ( 1 + $#ot ) . \"\\n\" );\r\n for(my $i=0;$i<=$#ot;$i++){\r\n print ( ( 1+$ot[$i]->[0] ) . \" \" . ( 1+$ot[$i]->[1] ) . \"\\n\" );\r\n }\r\n}\r\n\r\nexit(0);\r\n\r\n#### mod_int + nCr nPr set\r\n\r\n#!/usr/bin/perl\r\n\r\nmy ($n,$k) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\n&mk_fact($n+10); # << \r\n\r\n### write here \r\n\r\nexit(0);\r\n\r\nsub mod_pow { # ( x ** n ) % mod\r\n my ($x,$n) = @_;\r\n my $r = 1;\r\n while( $n ){\r\n $r = ( $r * $x ) % $mod if 1 & $n;\r\n $n >>= 1;\r\n $x = ( $x * $x ) % $mod;\r\n }\r\n return $r;\r\n}\r\n\r\nsub mk_fact { # make {global} n-size variable fact[] factinv[]\r\n my $n_max = shift;\r\n our @fact = (1); $#fact = $n_max;\r\n our @factinv = (1); $#factinv = $n_max;\r\n for(my $i=1;$i<=$n_max;$i++){\r\n $fact[$i] = ( $fact[$i-1] * $i ) % $mod;\r\n $factinv[$i] = &mod_pow($fact[$i],$mod-2);\r\n }\r\n}\r\n\r\nsub nCr { # calc nCr % mod\r\n my $n = shift; my $r = shift;\r\n return (( $fact[$n] * ( ( $factinv[$r] * $factinv[$n-$r] ) % $mod ) ) % $mod);\r\n}\r\nsub nPr { # calc nPr % mod\r\n my $n = shift; my $r = shift;\r\n return (( $fact[$n] * $factinv[$n-$r] ) % $mod );\r\n}\r\n\r\nsub gcd { # gcd\r\n my ($x,$y) = @_;\r\n return $x if $y == 0;\r\n return &gcd($y, $x % $y);\r\n}\r\nsub egcd { # ax+by=gcd(a,b) -> x,y return gcd(a,b)\r\n my ($a,$b,$x,$y) = @_;\r\n if( $b == 0 ){\r\n $$x = 1;\r\n $$y = 0;\r\n return $a;\r\n }\r\n my $d = &egcd($b,$a % $b,$y,$x);\r\n $$y -= int($a/$b) * $$x;\r\n return $d;\r\n}\r\nsub modinv { # required 'use integer'\r\n my $b = $mod; my $u = 1; my $v = 0;\r\n while($b){\r\n my $t = $a / $b;\r\n ($a,$b) = ($b, $a - $t * $b);\r\n ($u,$v) = ($v, $u - $t * $v);\r\n }\r\n $u %= $mod;\r\n $u += $mod if $u < 0;\r\n return $u;\r\n}\r\nsub factr { # factorialize\r\n my ($v) = @_;\r\n my %f = ();\r\n if( $v<4 ){\r\n $f{$v}++;\r\n return \\%f;\r\n }\r\n for(my $i=1;$i*$i<=$v;$i+=2){\r\n my $ii = ($i == 1 ? 2 : $i);\r\n while($v % $ii == 0){\r\n $f{$ii}++;\r\n $v /= $ii;\r\n }\r\n }\r\n $f{$v}++ if $v>1;\r\n return \\%f;\r\n}\r\n\r\n#### union find set\r\n\r\n#!/usr/bin/perl\r\n\r\nmy ($n) = map { $_ - 0 } split(/\\s+/o,);\r\n&mk_uf($n);\r\n#### write here\r\n\r\nexit(0);\r\n\r\nsub mk_uf {\r\n my $n = shift;\r\n our @uf = (); $#uf = $n - 1;\r\n for(my $i=0;$i<$n;$i++){\r\n $uf[$i] = $i;\r\n }\r\n}\r\n\r\nsub root {\r\n my $x = shift;\r\n while( $uf[$x] != $x ){ $x = $uf[$x] = $uf[$uf[$x]]; }\r\n return $x;\r\n}\r\n\r\nsub unite {\r\n my $x = &root(scalar(shift)); my $y = &root(scalar(shift));\r\n return if $x == $y;\r\n $uf[$y] = $x;\r\n}\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){\r\n $r = $e if !defined($r) or $e > $r;\r\n }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){\r\n $r = $e if !defined($r) or $e < $r;\r\n }\r\n return $r;\r\n}\r\n\r\n"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my @B = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my $asum = 0; my $bsum = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n $asum += $A[$i];\r\n $bsum += $B[$i];\r\n }\r\n \r\n if( $asum != $bsum ){\r\n print \"-1\\n\"; next;\r\n }\r\n \r\n my @ot = ();\r\n my $ai = 0;\r\n while( $ai < $n-1 and $A[1+$ai]>0 ){ $ai++; }\r\n for(my $i=0;$i<$n;$i++){\r\n if( $A[$i] > $B[$i] ){\r\n my $d = $A[$i] - $B[$i];\r\n $A[$n-1] += $d;\r\n for(my $j=0;$j<$d;$j++){\r\n push(@ot,+[$i,$n-1]);\r\n }\r\n $ai = $n-1;\r\n $A[$i] = $B[$i];\r\n } elsif( $A[$i] < $B[$i] ){\r\n my $d = $B[$i] - $A[$i];\r\n for(my $j=0;$j<$d;$j++){\r\n while($A[$ai]==0){ $ai -- ; }\r\n push(@ot,+[$ai,$i]);\r\n $A[$i] ++;\r\n $A[$ai] --;\r\n }\r\n }\r\n }\r\n print ( ( 1 + $#ot ) . \"\\n\" );\r\n for(my $i=0;$i<=$#ot;$i++){\r\n print ( ( 1+$ot[$i]->[0] ) . \" \" . ( 1+$ot[$i]->[1] ) . \"\\n\" );\r\n }\r\n}\r\n\r\nexit(0);\r\n"}], "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\u2009\u00d7\u2009n 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\u2009\u2264\u2009n\u2009\u2264\u20093, 1\u2009\u2264\u2009k\u2009\u2264\u20095), where k is the amount of steps of the algorithm. Each of the following n lines contains n symbols that determine the model. Symbol \u00ab.\u00bb stands for a white square, whereas \u00ab*\u00bb stands for a black one. It is guaranteed that the model has at least one white square. ", "output_spec": "Output a matrix nk\u2009\u00d7\u2009nk 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": "#!/bin/perl -w\nuse strict;\nuse warnings;\nuse integer;\nuse IO::File;\n\nmy $in = new IO::File \"< input.txt\";\nmy $out = new IO::File \"> output.txt\";\n\nmy ($n, $k) = map int, split /\\D/, <$in>;\nmy (@t, @f);\nfor (0 .. $n-1) {\n my $s = <$in>;\n chomp $s;\n @{$t[$_]} = split //, $s;\n};\n$in->close;\nfor my $i (0 .. $n ** $k - 1) {\n for my $j (0 .. $n ** $k - 1) {\n\tfor my $d (0 .. $k - 1) {\n\t $f[$i]->[$j] = $t[($i/($n**$d))%$n]->[($j/($n**$d))%$n];\n\t if ($f[$i]->[$j] eq '*') {\n\t\tlast;\n\t }\n\t}\n }\n}\nfor (0 .. $n ** $k - 1) {\n print $out @{$f[$_]}, \"\\n\";\n}\n$out->close;\n"}], "negative_code": [], "src_uid": "edf69ef07b42e8290887b996e3cb94f6"} {"nl": {"description": "Hiking club \"Up the hill\" just returned from a walk. Now they are trying to remember which hills they've just walked through.It is known that there were N stops, all on different integer heights between 1 and N kilometers (inclusive) above the sea level. On the first day they've traveled from the first stop to the second stop, on the second day they've traveled from the second to the third and so on, and on the last day they've traveled from the stop N\u2009-\u20091 to the stop N and successfully finished their expedition.They are trying to find out which heights were their stops located at. They have an entry in a travel journal specifying how many days did they travel up the hill, and how many days did they walk down the hill.Help them by suggesting some possible stop heights satisfying numbers from the travel journal.", "input_spec": "In the first line there is an integer non-negative number A denoting the number of days of climbing up the hill. Second line contains an integer non-negative number B\u00a0\u2014 the number of days of walking down the hill (A\u2009+\u2009B\u2009+\u20091\u2009=\u2009N, 1\u2009\u2264\u2009N\u2009\u2264\u2009100\u2009000).", "output_spec": "Output N space-separated distinct integers from 1 to N inclusive, denoting possible heights of the stops in order of visiting.", "sample_inputs": ["0\n1", "2\n1"], "sample_outputs": ["2 1", "1 3 4 2"], "notes": null}, "positive_code": [{"source_code": "$_ = <> ,$, = $\",\nprint 1 .. $_ ++ , reverse $_ .. $_ + <>\n"}, {"source_code": "$_ = <> ,$, = $\",\nprint 1 .. $_ ++ , reverse $_ .. $_ + <>\n"}, {"source_code": "$_ = <> ,$, = $\",\nprint 1 .. $_ ++ , reverse $_ .. $_ + <>"}, {"source_code": "$_ = <> ,$, = $\",\nprint 1 .. $_ ++ , reverse $_ .. $_ + <>\n"}, {"source_code": "$_ = <> ,$, = $\",\nprint 1 .. $_ ++ , reverse $_ .. $_ + <>\n"}, {"source_code": "$_ = <> ,$, = $\",\nprint 1 .. $_ ++ , reverse $_ .. $_ + <>\n"}, {"source_code": "$_ = <> ,$, = $\",\nprint 1 .. $_ ++ , reverse $_ .. $_ + <>\n"}, {"source_code": "$_ = <> ,$, = $\",\nprint 1 .. $_ ++ , reverse $_ .. $_ + <>\n"}, {"source_code": "$_ = <> ,$, = $\",\nprint 1 .. $_ ++ , reverse $_ .. $_ + <>"}, {"source_code": "$a=<>;\n$b=<>;\n@n=(1..$a+$b+1);\nfor (1..$a+$b+1){\n\t$a-- > 0 and push @a, shift @n;\n\t$b-- > 0 and unshift @b, shift @n;\n\t}\npush @a, @n;\nprint \"@a @b\""}, {"source_code": "$_ = <> ,$, = $\",\nprint 1 .. $_ ++ , reverse $_ .. $_ + <>\n"}], "negative_code": [], "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\u00a0\u2014 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": "for(1..<>){($n,$s)=split/\\s/,<>;print-1&$s/$n/$n,\"\\n\"}"}], "negative_code": [], "src_uid": "7226a7d2700ee52f52d417f404c42ab7"} {"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 \u2014 $$$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$$$) \u2014 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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $A, $B ) = split;\n\t\n\tmy $sum = 0;\n\t\n\tif( $n % 2 == 1 ){\n\t\t$sum += $A;\n\t\t$n --;\n\t\t}\n\t\t\n\tif( $A * 2 < $B ){\n\t\tprint $sum + $n * $A;\n\t\t}\n\telse{\n\t\tprint $sum + $n * $B / 2;\n\t\t}\n\t\n\t}"}], "negative_code": [], "src_uid": "beab56c5f7d2996d447320a62b0963c2"} {"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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <> =~ s/\\s//gr;\n\t\n\tmy $c = 0;\n\t\n\t$c ++ while s/10101/10001/;\n\t\n\t$c ++ while s/101/100/;\n\t\n\tprint $c;\n\t}"}], "negative_code": [], "src_uid": "ea62b6f68d25fb17aba8932af8377db0"} {"nl": {"description": "One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1,\u2009y1),\u2009(x2,\u2009y2),\u2009...,\u2009(xn,\u2009yn). Let's define neighbors for some fixed point from the given set (x,\u2009y): point (x',\u2009y') is (x,\u2009y)'s right neighbor, if x'\u2009>\u2009x and y'\u2009=\u2009y point (x',\u2009y') is (x,\u2009y)'s left neighbor, if x'\u2009<\u2009x and y'\u2009=\u2009y point (x',\u2009y') is (x,\u2009y)'s lower neighbor, if x'\u2009=\u2009x and y'\u2009<\u2009y point (x',\u2009y') is (x,\u2009y)'s upper neighbor, if x'\u2009=\u2009x and y'\u2009>\u2009y We'll consider point (x,\u2009y) 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\u2009\u2264\u2009n\u2009\u2264\u2009200) \u2014 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|,\u2009|y|\u2009\u2264\u20091000), 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 \u2014 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,\u20091) and (1,\u20092).In the second sample there is one supercental point \u2014 point (0,\u20090)."}, "positive_code": [{"source_code": "#!/usr/local/bin/perl\n\nuse strict;\n$|=1;\n\nmy $cnt = <>;\nmy $points = {};\nfor( my $i=0; $i<$cnt; $i++ ) {\n\tmy $line = <>;\n\tchomp($line);\n\t$line =~ /^([\\d\\.\\-]+) ([\\d\\.\\-]+)$/;\n\t$points->{ $1.'_'.$2 }=1;\n}\n\nmy $n=0;\nmy @k = keys %$points;\nforeach my $coord (@k) {\n#\tprint \"For coord $coord\\n\";\n\tmy ($x, $y) = split('_', $coord);\n\t#check x\n\tmy @xl = grep { /^([\\d\\.\\-]+)_${y}$/ && $1<$x } @k;\n\tmy @xg = grep { /^([\\d\\.\\-]+)_${y}$/ && $1>$x } @k;\n\n\t#check y\n\tmy @yl = grep { /^${x}_([\\d\\.\\-]+)$/ && $1<$y } @k;\n\tmy @yg = grep { /^${x}_([\\d\\.\\-]+)$/ && $1>$y } @k;\n\n\t$n++ if( @xl && @xg && @yl && @yg );\n#\tprint \"x less: \", join(\"; \", @xl), \"\\n\";\n#\tprint \"x grea: \", join(\"; \", @xg), \"\\n\";\n#\tprint \"y less: \", join(\"; \", @yl), \"\\n\";\n#\tprint \"y grea: \", join(\"; \", @yg), \"\\n\";\n}\n\nprint $n, \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\nuse warnings;\nuse strict;\nchomp(my $n = );\nmy @pts = ();\nwhile($n-- > 0){\n\tmy @tmp = split(/[\\s\\/n]/,);\n\tpush(@pts,\\@tmp);\n}\nmy @check = (0,0,0,0);\nmy $count = 0;\nforeach my $pt (@pts) {\n\t@check = (0,0,0,0);\n\tforeach my $otherpt (@pts) {\n\t\tif($$pt[0]==$$otherpt[0]){\n\t\t\t$check[0] = 1 if($$pt[1]>$$otherpt[1]);\n\t\t\t$check[1] = 1 if($$pt[1]<$$otherpt[1]);\n\t\t}\n\t\telsif($$pt[1]==$$otherpt[1]){\n\t\t\t$check[2] = 1 if($$pt[0]>$$otherpt[0]);\n\t\t\t$check[3] = 1 if($$pt[0]<$$otherpt[0]);\n\t\t}\n\t}\n\tmy $checksum = 0;\n\t$checksum += $_ for @check;\n\t$count++ if($checksum == 4);\n}\n\nprint $count;"}, {"source_code": "#!/usr/bin/perl\n$n=<>;\nfor($i=0; $i<$n; $i++){\n\t($tx, $ty) = ($x[$i], $y[$i]) = split(/[ \\n]/, <>);\n\tif(! exists($minx{$ty}) || $tx < $minx{$ty}){$minx{$ty} = $tx;}\n\tif(! exists($maxx{$ty}) || $tx > $maxx{$ty}){$maxx{$ty} = $tx;}\n\tif(! exists($miny{$tx}) || $ty < $miny{$tx}){$miny{$tx} = $ty;}\n\tif(! exists($maxy{$tx}) || $ty > $maxy{$tx}){$maxy{$tx} = $ty;}\n}\n$cnt=0;\nfor($i=0; $i<$n; $i++){\n\tif($x[$i]>$minx{$y[$i]}\n\t&& $x[$i]<$maxx{$y[$i]}\n\t&& $y[$i]>$miny{$x[$i]}\n\t&& $y[$i]<$maxy{$x[$i]}\n\t){\n\t\t$cnt++;\n\t}\n#\tprint \"$x[$i] $y[$i] $cnt\\n\";\n}\nprint \"$cnt\\n\";\n\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nchomp (my $n = <>);\n\nmy @dots;\npush @dots, <> for ($n);\n\nmy $count = 0;\n\nforeach my $dot (@dots)\n{\n my $index = shift;\n my ($x, $y) = split / /, $dot;\n my $bits;\n\n foreach (@dots)\n {\n my ($x_, $y_) = split / /, $_;\n\n if ($x eq $x_)\n {\n $bits |= 1 if ($y_ > $y);\n $bits |= 2 if ($y_ < $y);\n }\n\n if ($y eq $y_)\n {\n $bits |= 4 if ($x_ < $x);\n $bits |= 8 if ($x_ > $x);\n }\n\n }\n\n $count++ if ($bits eq 15);\n}\n\nprint $count, \"\\n\";"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $n = <>;\nmy @dots;\npush @dots, <> for ($n);\n\nmy $count = 0;\nforeach my $dot (@dots)\n{\n my $index = shift;\n my ($x, $y) = split /\\s/, $dot;\n my $bits;\n\n foreach (@dots)\n {\n my ($x_, $y_) = split /\\s/;\n\n $bits |= ($y_ < $y)? 1 : (($y_ > $y) ? 2 : 0) if ($x eq $x_);\n $bits |= ($x_ < $x)? 4 : (($x_ > $x) ? 8 : 0) if ($y eq $y_);\n }\n\n $count++ if ($bits eq 15);\n}\n\nprint $count, \"\\n\";"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nchomp (my $n = <>);\n\nmy @dots;\npush @dots, <> for ($n);\n\nmy $count = 0;\n\nsub is_super($)\n{\n my $index = shift;\n my ($x, $y) = split / /, $dots[$index];\n chomp ($y);\n my $score = 0;\n my ($left, $right, $up, $down) = (0,0,0,0);\n\n foreach (@dots)\n {\n chomp($_);\n my ($x_, $y_) = split / /, $_;\n\n if ($x eq $x_)\n {\n $up = ($y_ > $y) if (!$up);\n $down = ($y_ < $y) if (!$down);\n }\n\n if ($y eq $y_)\n {\n $left = ($x_ < $x) if (!$left);\n $right = ($x_ > $x) if (!$right);\n }\n\n }\n\n $count++ if ($left && $right && $up && $down);\n}\n\nis_super($_) for (0..$n-1);\n\nprint $count, \"\\n\";"}, {"source_code": "while (<>){\n @_=@A=();\n for (1..$_){\n push @_, \"\".<>;\n }\n chomp @_;\n for (@_){\n / /;\n $a=$`;$b=$';\n $d=$u=$r=$l=0;\n for (@_){\n / /;\n $x=$`,$y=$';\n \n if ($a==$x and $b>$y){$d++}\n if ($a==$x and $b<$y){$u++}\n if ($a>$x and $b==$y){$l++}\n if ($a<$x and $b==$y){$r++}\n }\n if ($d and $u and $l and $r){\n push @A, $_;\n }\n \n }\n print 0+@A, \"\\n\"; \n }"}], "negative_code": [{"source_code": "#!/usr/local/bin/perl\n\nuse strict;\n$|=1;\n\nmy $cnt = <>;\nmy $points = {};\nfor( my $i=0; $i<$cnt; $i++ ) {\n\tmy $line = <>;\n\t$line =~ /(\\d+) (\\d+)/;\n\t$points->{ $1.'_'.$2 }=1;\n}\n\nmy $n=0;\nmy @k = keys %$points;\nforeach my $coord (@k) {\n\tprint \"For coord $coord\\n\";\n\tmy ($x, $y) = split('_', $coord);\n\t#check x\n\tmy @xl = grep { /(\\d+)_${y}/ && $1<$x } @k;\n\tmy @xg = grep { /(\\d+)_${y}/ && $1>$x } @k;\n\n\t#check y\n\tmy @yl = grep { /${x}_(\\d+)/ && $1<$y } @k;\n\tmy @yg = grep { /${x}_(\\d+)/ && $1>$y } @k;\n\n\t$n++ if( @xl && @xg && @yl && @yg );\n\tprint \"x less: \", join(\"; \", @xl), \"\\n\";\n\tprint \"x grea: \", join(\"; \", @xg), \"\\n\";\n\tprint \"y less: \", join(\"; \", @yl), \"\\n\";\n\tprint \"y grea: \", join(\"; \", @yg), \"\\n\";\n\n}\n\nprint $n, \"\\n\";\n"}, {"source_code": "#!/usr/local/bin/perl\n\nuse strict;\n$|=1;\n\nmy $cnt = <>;\nmy $points = {};\nfor( my $i=0; $i<$cnt; $i++ ) {\n\tmy $line = <>;\n\t$line =~ /(\\d+) (\\d+)/;\n\t$points->{ $1.'_'.$2 }=1;\n}\n\nmy $n=0;\nmy @k = keys %$points;\nforeach my $coord (@k) {\n#\tprint \"For coord $coord\\n\";\n\tmy ($x, $y) = split('_', $coord);\n\t#check x\n\tmy @xl = grep { /(\\d+)_${y}/ && $1<$x } @k;\n\tmy @xg = grep { /(\\d+)_${y}/ && $1>$x } @k;\n\n\t#check y\n\tmy @yl = grep { /${x}_(\\d+)/ && $1<$y } @k;\n\tmy @yg = grep { /${x}_(\\d+)/ && $1>$y } @k;\n\n\t$n++ if( @xl && @xg && @yl && @yg );\n#\tprint \"x less: \", join(\"; \", @xl), \"\\n\";\n#\tprint \"x grea: \", join(\"; \", @xg), \"\\n\";\n#\tprint \"y less: \", join(\"; \", @yl), \"\\n\";\n#\tprint \"y grea: \", join(\"; \", @yg), \"\\n\";\n}\n\nprint $n, \"\\n\";\n"}, {"source_code": "#!/usr/local/bin/perl\n\nuse strict;\n$|=1;\n\nmy $cnt = <>;\nmy $points = {};\nfor( my $i=0; $i<$cnt; $i++ ) {\n\tmy $line = <>;\n\t$line =~ /([\\d\\.\\-]+) ([\\d\\.\\-]+)/;\n\t$points->{ $1.'_'.$2 }=1;\n}\n\nmy $n=0;\nmy @k = keys %$points;\nforeach my $coord (@k) {\n#\tprint \"For coord $coord\\n\";\n\tmy ($x, $y) = split('_', $coord);\n\t#check x\n\tmy @xl = grep { /([\\d\\.\\-]+)_${y}/ && $1<$x } @k;\n\tmy @xg = grep { /([\\d\\.\\-]+)_${y}/ && $1>$x } @k;\n\n\t#check y\n\tmy @yl = grep { /${x}_([\\d\\.\\-]+)/ && $1<$y } @k;\n\tmy @yg = grep { /${x}_([\\d\\.\\-]+)/ && $1>$y } @k;\n\n\t$n++ if( @xl && @xg && @yl && @yg );\n#\tprint \"x less: \", join(\"; \", @xl), \"\\n\";\n#\tprint \"x grea: \", join(\"; \", @xg), \"\\n\";\n#\tprint \"y less: \", join(\"; \", @yl), \"\\n\";\n#\tprint \"y grea: \", join(\"; \", @yg), \"\\n\";\n}\n\nprint $n, \"\\n\";\n"}, {"source_code": "#!/usr/local/bin/perl\n\nuse strict;\n$|=1;\n\nmy $cnt = <>;\nmy $points = {};\nfor( my $i=0; $i<$cnt; $i++ ) {\n\tmy $line = <>;\n\t$line =~ /([\\d\\.]+) ([\\d\\.]+)/;\n\t$points->{ $1.'_'.$2 }=1;\n}\n\nmy $n=0;\nmy @k = keys %$points;\nforeach my $coord (@k) {\n#\tprint \"For coord $coord\\n\";\n\tmy ($x, $y) = split('_', $coord);\n\t#check x\n\tmy @xl = grep { /([\\d\\.]+)_${y}/ && $1<$x } @k;\n\tmy @xg = grep { /([\\d\\.]+)_${y}/ && $1>$x } @k;\n\n\t#check y\n\tmy @yl = grep { /${x}_([\\d\\.]+)/ && $1<$y } @k;\n\tmy @yg = grep { /${x}_([\\d\\.]+)/ && $1>$y } @k;\n\n\t$n++ if( @xl && @xg && @yl && @yg );\n#\tprint \"x less: \", join(\"; \", @xl), \"\\n\";\n#\tprint \"x grea: \", join(\"; \", @xg), \"\\n\";\n#\tprint \"y less: \", join(\"; \", @yl), \"\\n\";\n#\tprint \"y grea: \", join(\"; \", @yg), \"\\n\";\n}\n\nprint $n, \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n$n=<>;\nfor($i=0; $i<$n; $i++){\n\t($tx, $ty) = ($x[$i], $y[$i]) = split(/[ \\n]/, <>);\n\tif(! exists($minx{$ty}) || $tx < $minx{$ty}){$minx{$ty} = $tx;}\n\tif(! exists($maxx{$ty}) || $tx > $maxx{$ty}){$maxx{$ty} = $tx;}\n\tif(! exists($miny{$tx}) || $ty < $miny{$tx}){$miny{$tx} = $ty;}\n\tif(! exists($maxy{$tx}) || $ty > $maxy{$tx}){$maxy{$tx} = $ty;}\n}\nfor($i=0; $i<$n; $i++){\n\tif($x[$i]>$minx{$y[$i]}\n\t&& $x[$i]<$maxx{$y[$i]}\n\t&& $y[$i]>$miny{$x[$i]}\n\t&& $y[$i]<$maxy{$x[$i]}\n\t){\n\t\t$cnt++;\n\t}\n#\tprint \"$x[$i] $y[$i] $cnt\\n\";\n}\nprint \"$cnt\\n\"\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nchomp (my $n = <>);\n\nmy @dots;\npush @dots, <> for ($n);\n\nmy $count = 0;\n\nsub is_super($)\n{\n my $index = shift;\n my ($x, $y) = split / /, $dots[$index];\n chomp ($y);\n my $score = 0;\n\n foreach (@dots)\n {\n chomp($_);\n my ($x_, $y_) = split / /, $_;\n\n $score++ if (($x > $x_) and ($y eq $y_));\n $score++ if (($x < $x_) and ($y eq $y_));\n $score++ if (($x eq $x_) and ($y > $y_));\n $score++ if (($x eq $x_) and ($y < $y_));\n }\n\n $count++ if ($score >= 4);\n}\n\nis_super($_) for (0..$n-1);\n\nprint $count, \"\\n\";"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nchomp (my $n = <>);\n\nmy @dots;\npush @dots, <> for ($n);\n\nmy $count = 0;\n\nsub is_super($)\n{\n my $index = shift;\n my ($x, $y) = split / /, $dots[$index];\n chomp ($y);\n my $score = 0;\n\n foreach (@dots)\n {\n chomp($_);\n my ($x_, $y_) = split / /, $_;\n\n if (($x_ ne $x) and ($y_ ne $y))\n {\n $score++ if ($x > $x_ and $y eq $y_);\n $score++ if ($x < $x_ and $y eq $y_);\n $score++ if ($x == $x and $y > $y_);\n $score++ if ($x == $x and $y < $y_);\n }\n }\n\n $count++ if ($score >= 4);\n}\n\nfor (0..$n-1)\n{\n is_super($_);\n}\n\nprint $count, \"\\n\";"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nchomp (my $n = <>);\n\nmy @dots;\npush @dots, <> for ($n);\n\nmy $count = 0;\n\nsub is_super($)\n{\n my $index = shift;\n my ($x, $y) = split / /, $dots[$index];\n chomp ($y);\n my $score = 0;\n\n foreach (@dots)\n {\n chomp($_);\n my ($x_, $y_) = split / /, $_;\n\n if (($x_ ne $x) and ($y_ ne $y))\n {\n $score++ if ($x > $x_ and $y eq $y_);\n $score++ if ($x < $x_ and $y eq $y_);\n $score++ if ($x == $x and $y > $y_);\n $score++ if ($x == $x and $y < $y_);\n }\n }\n\n $count++ if ($score > 4);\n}\n\nfor (0..$n-1)\n{\n is_super($_);\n}\n\nprint $count, \"\\n\";"}], "src_uid": "594e64eef7acd055d59f37710edda201"} {"nl": {"description": "So the Beautiful Regional Contest (BeRC) has come to an end! $$$n$$$ students took part in the contest. The final standings are already known: the participant in the $$$i$$$-th place solved $$$p_i$$$ problems. Since the participants are primarily sorted by the number of solved problems, then $$$p_1 \\ge p_2 \\ge \\dots \\ge p_n$$$.Help the jury distribute the gold, silver and bronze medals. Let their numbers be $$$g$$$, $$$s$$$ and $$$b$$$, respectively. Here is a list of requirements from the rules, which all must be satisfied: for each of the three types of medals, at least one medal must be awarded (that is, $$$g>0$$$, $$$s>0$$$ and $$$b>0$$$); the number of gold medals must be strictly less than the number of silver and the number of bronze (that is, $$$g<s$$$ and $$$g<b$$$, but there are no requirements between $$$s$$$ and $$$b$$$); each gold medalist must solve strictly more problems than any awarded with a silver medal; each silver medalist must solve strictly more problems than any awarded a bronze medal; each bronze medalist must solve strictly more problems than any participant not awarded a medal; the total number of medalists $$$g+s+b$$$ should not exceed half of all participants (for example, if $$$n=21$$$, then you can award a maximum of $$$10$$$ participants, and if $$$n=26$$$, then you can award a maximum of $$$13$$$ participants). The jury wants to reward with medals the total maximal number participants (i.e. to maximize $$$g+s+b$$$) so that all of the items listed above are fulfilled. Help the jury find such a way to award medals.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\le t \\le 10000$$$) \u2014 the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of a test case contains an integer $$$n$$$ ($$$1 \\le n \\le 4\\cdot10^5$$$) \u2014 the number of BeRC participants. The second line of a test case contains integers $$$p_1, p_2, \\dots, p_n$$$ ($$$0 \\le p_i \\le 10^6$$$), where $$$p_i$$$ is equal to the number of problems solved by the $$$i$$$-th participant from the final standings. The values $$$p_i$$$ are sorted in non-increasing order, i.e. $$$p_1 \\ge p_2 \\ge \\dots \\ge p_n$$$. The sum of $$$n$$$ over all test cases in the input does not exceed $$$4\\cdot10^5$$$.", "output_spec": "Print $$$t$$$ lines, the $$$j$$$-th line should contain the answer to the $$$j$$$-th test case. The answer consists of three non-negative integers $$$g, s, b$$$. Print $$$g=s=b=0$$$ if there is no way to reward participants with medals so that all requirements from the statement are satisfied at the same time. Otherwise, print three positive numbers $$$g, s, b$$$ \u2014 the possible number of gold, silver and bronze medals, respectively. The sum of $$$g+s+b$$$ should be the maximum possible. If there are several answers, print any of them. ", "sample_inputs": ["5\n12\n5 4 4 3 2 2 1 1 1 1 1 1\n4\n4 3 2 1\n1\n1000000\n20\n20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\n32\n64 64 63 58 58 58 58 58 37 37 37 37 34 34 28 28 28 28 28 28 24 24 19 17 17 17 17 16 16 16 16 11"], "sample_outputs": ["1 2 3\n0 0 0\n0 0 0\n2 5 3\n2 6 6"], "notes": "NoteIn the first test case, it is possible to reward $$$1$$$ gold, $$$2$$$ silver and $$$3$$$ bronze medals. In this case, the participant solved $$$5$$$ tasks will be rewarded with the gold medal, participants solved $$$4$$$ tasks will be rewarded with silver medals, participants solved $$$2$$$ or $$$3$$$ tasks will be rewarded with bronze medals. Participants solved exactly $$$1$$$ task won't be rewarded. It's easy to see, that in this case, all conditions are satisfied and it is possible to reward participants in this way. It is impossible to give more than $$$6$$$ medals because the number of medals should not exceed half of the number of participants. The answer $$$1$$$, $$$3$$$, $$$2$$$ is also correct in this test case.In the second and third test cases, it is impossible to reward medals, because at least one medal of each type should be given, but the number of medals should not exceed half of the number of participants."}, "positive_code": [{"source_code": "my $tests = <>;\nchomp ($tests);\nforeach my $t (1..$tests) {\n my $n = <>;\n chomp($n);\n my @arr = split(\" \",<>);\n my $size = $#arr + 1;\n my $h_size = int($size / 2);\n #print \"\\$size:$size -- h_size:$h_size\\n\";\n my $g = 0 ;\n my $s = 0 ;\n my $b = 0 ;\n my $b_flag = 0;\n my $s_flag = 0;\n my $b_block =0 ;\n for (my $i = 0 ; $i <= $size ; $i=$i+1) {\n #if ($n <= 5) {$g=0;$s=0;$b=0;last;}\n my $sum = $g + $s + $b;\n #print \"\\sum:$sum-($h_size)\\n\";\n if ($sum > $h_size) {$g=0;$s=0;$b=0;last;}\n if ($b_flag) {\n if ($arr[$i] == $arr[$i+1]) {\n $b_block = $b_block +1; \n } else {\n $b_block = $b_block +1;\n my $sum = $g + $s + $b + $b_block;\n if ($sum > $h_size) {\n last; \n } else {\n $b = $b + $b_block; \n $b_block =0 ;\n }\n } \n } elsif ($s_flag) {\n if ($arr[$i] == $arr[$i+1]) {\n $s = $s +1; \n } else {\n $s = $s + 1;\n if ($s > $g) {\n $b_flag = 1;\n $s_flag = 0;\n }\n } \n } else {\n if ($arr[$i] == $arr[$i+1]) {\n $g = $g +1; \n } else {\n $g = $g +1 ;\n $s_flag = 1;\n }\n }\n }\n if ($b == 0 || $b <= $g) {$g=0;$s=0;$b=0;}\n print \"$g $s $b\\n\";\n}"}], "negative_code": [{"source_code": "my $tests = <>;\nchomp ($tests);\nforeach my $t (1..$tests) {\n my $n = <>;\n chomp($n);\n my @arr = split(\" \",<>);\n my $size = $#arr + 1;\n my $h_size = int($size / 2);\n #print \"\\$size:$size -- h_size:$h_size\\n\";\n my $g = 0 ;\n my $s = 0 ;\n my $b = 0 ;\n my $b_flag = 0;\n my $s_flag = 0;\n my $b_block =0 ;\n for (my $i = 0 ; $i <= $size ; $i=$i+1) {\n #if ($n <= 5) {$g=0;$s=0;$b=0;last;}\n my $sum = $g + $s + $b;\n #print \"\\sum:$sum-($h_size)\\n\";\n if ($sum > $h_size) {$g=0;$s=0;$b=0;last;}\n if ($b_flag) {\n if ($arr[$i] == $arr[$i+1]) {\n $b_block = $b_block +1; \n } else {\n $b_block = $b_block +1;\n my $sum = $g + $s + $b + $b_block;\n if ($sum > $h_size) {\n last; \n } else {\n $b = $b + $b_block; \n $b_block =0 ;\n }\n } \n } elsif ($s_flag) {\n if ($arr[$i] == $arr[$i+1]) {\n $s = $s +1; \n } else {\n $s = $s + 1;\n if ($s > $g) {\n $b_flag = 1;\n $s_flag = 0;\n }\n } \n } else {\n if ($arr[$i] == $arr[$i+1]) {\n $g = $g +1; \n } else {\n $g = $g +1 ;\n $s_flag = 1;\n }\n }\n }\n if ($b == 0) {$g=0;$s=0;$b=0;}\n print \"$g $s $b\\n\";\n}"}, {"source_code": "my $tests = <>;\nchomp ($tests);\nforeach my $t (1..$tests) {\n my $n = <>;\n chomp($n);\n my @arr = split(\" \",<>);\n my $size = $#arr + 1;\n my $h_size = int($size / 2);\n #print \"\\$size:$size -- h_size:$h_size\\n\";\n my $g = 0 ;\n my $s = 0 ;\n my $b = 0 ;\n my $b_flag = 0;\n my $s_flag = 0;\n my $b_block =0 ;\n for (my $i = 0 ; $i <= $size ; $i=$i+1) {\n #print \"$arr[$i]\";\n #if ($n <= 5) {$g=0;$s=0;$b=0;last;}\n my $sum = $g + $s + $b;\n #print \"\\sum:$sum-($h_size)\\n\";\n if ($sum > $h_size) {$g=0;$s=0;$b=0;last;}\n if ($b_flag) {\n if ($arr[$i] == $arr[$i+1]) {\n $b_block = $b_block +1; \n } else {\n $b_block = $b_block +1;\n my $sum = $g + $s + $b + $b_block;\n if ($sum > $h_size) {\n last; \n } else {\n $b = $b + $b_block; \n $b_block =0 ;\n }\n } \n } elsif ($s_flag) {\n if ($arr[$i] == $arr[$i+1]) {\n $s = $s +1; \n } else {\n $s = $s + 1;\n if ($s > $g) {\n $b_flag = 1;\n $s_flag = 0;\n }\n } \n } else {\n if ($arr[$i] == $arr[$i+1]) {\n $g = $g +1; \n } else {\n $g = $g +1 ;\n #$s++;\n $s_flag = 1;\n }\n }\n }\n print \"$g $s $b\\n\";\n}"}], "src_uid": "cff809c3d5682e6b3e71b525c5f8f8b4"} {"nl": {"description": "You are given a sequence of positive integers a1,\u2009a2,\u2009...,\u2009an. 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\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105). The second line contains elements of the sequence a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20091000). 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": "$n = <>; $r = 0;\n@a = split(' ', <>);\n$s = eval join '+', @a;\nforeach(@a){\n\t++$ptr;\n\t($r[$r ++] = $ptr) if $_ * ($n - 1) == $s - $_;}\nprint \"$r\\n\";\nforeach(@r){ print \"$_ \";}"}], "negative_code": [], "src_uid": "1a22bc82ddf6b3dfbf270bc5e3294c28"} {"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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009105) \u2014 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": "#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nmy $n;\nmy $m;\n\nsub mcmp {\n my $v1 = abs (($m + 1) / 2 - $a);\n my $v2 = abs (($m + 1) / 2 - $b);\n return -1 if($v1 < $v2);\n return -1 if ($v1 == $v2 && $a < $b);\n return 1;\n}\n\nmy $num = <>;\nmy @arr = split(/ /, $num);\n$n = $arr[0];\n$m = $arr[1];\nmy @array = ();\nfor(1..$m) {\n push @array, $_;\n}\nmy @ans= sort mcmp @array;\nfor(0..($n - 1)) {\n my $t = int($_ % $m);\n print \"$ans[$t]\\n\";\n}\n\n\n"}, {"source_code": "#!perl -w\nuse strict;\nuse warnings;\nuse POSIX qw[ceil];\n\nmain();\n\nsub main {\n my ($n, $m) = split /\\D/, <>;\n my @arr;\n for my $i (0..int($m / 2)-1) {\n unshift @arr, $m - $i;\n unshift @arr, $i + 1;\n }\n if ($m & 1) {\n unshift @arr, int($m / 2) + 1;\n }\n my $c = 0;\n for my $i (1..$n) {\n print $arr[$c % @arr], \"\\n\";\n $c++;\n }\n}\n\n__END__\n\n"}], "negative_code": [], "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\u00b7b\u2009=\u200912), after that he makes a table of size a\u2009\u00d7\u2009b 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,\u2009b that she can choose and win.", "input_spec": "The first line of the input contains integer t (1\u2009\u2264\u2009t\u2009\u2264\u2009100). 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,\u2009b. 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": "<>;\ns/X.*/$& 1x12/,\ns/X.{5}X.*/$& 2x6/,\ns/X...X...X.*/$& 3x4/,\ns/X..X..X..X.*/$& 4x3/,\ns/X.X.X.X.X.X.*/$& 6x2/,\ns/X{12}.*/$& 12x1/,\ns/X|O//g,\ns!^!()=/x/g!e,\nprint\nfor <>\n"}, {"source_code": "<>;\ns/X.*/$& 1x12/,\ns/X.{5}X.*/$& 2x6/,\ns/X...X...X.*/$& 3x4/,\ns/X..X..X..X.*/$& 4x3/,\ns/X.X.X.X.X.X.*/$& 6x2/,\ns/X{12}.*/$& 12x1/,\ns/X|O//g,\ns!^!()=/x/g!e,\nprint\nfor <>\n"}, {"source_code": "<>;\ns/X.*/$& 1x12/,\ns/X.{5}X.*/$& 2x6/,\ns/X...X...X.*/$& 3x4/,\ns/X..X..X..X.*/$& 4x3/,\ns/X.X.X.X.X.X.*/$& 6x2/,\ns/X{12}.*/$& 12x1/,\ns/X|O//g,\ns!^!()=/x/g!e,\nprint\nfor <>"}, {"source_code": "<>;\ns/X.*/$& 1x12/,\ns/X.{5}X.*/$& 2x6/,\ns/X...X...X.*/$& 3x4/,\ns/X..X..X..X.*/$& 4x3/,\ns/X.X.X.X.X.X.*/$& 6x2/,\ns/X{12}.*/$& 12x1/,\ns/X|O//g,\ns!^!()=/x/g!e,\nprint\nfor <>\n"}, {"source_code": "<>;\ns/X.*/$& 1x12/,\ns/X.{5}X.*/$& 2x6/,\ns/X...X...X.*/$& 3x4/,\ns/X..X..X..X.*/$& 4x3/,\ns/X.X.X.X.X.X.*/$& 6x2/,\ns/X{12}.*/$& 12x1/,\ns/X|O//g,\ns!^!()=/x/g!e,\nprint\nfor <>\n"}, {"source_code": "<>;\n$b=\"\",\n(/X/ and $b.=\" 1x12\"),\n(/X.{5}X/ and $b.=\" 2x6\"),\n(/X...X...X/ and $b.=\" 3x4\"),\n(/X..X..X..X/ and $b.=\" 4x3\"),\n(/X.X.X.X.X.X/ and $b.=\" 6x2\"),\n(/O/ or $b.=\" 12x1\"),\nprint ($B=()=$b=~/ /g,\"$b\\n\"),\nfor <>"}, {"source_code": "#!/usr/bin/perl\n\n<>;\nwhile (<>){\n\tchomp;\n\t@a=();\n\t/X/&& push @a,\"1x12\";\n\t/X.....X/&& push @a,\"2x6\";\n\t/X...X...X/&& push @a,\"3x4\";\n\t/X..X..X..X/&& push @a,\"4x3\";\n\t/X.X.X.X.X.X/&& push @a, \"6x2\";\n\t/XXXXXXXXXXXX/&& push @a,\"12x1\";\n\tprint 0+@a,\" @a\\n\";\n\n}\n"}, {"source_code": "<>;\ns/X.*/$& 1x12/,\ns/X.{5}X.*/$& 2x6/,\ns/X...X...X.*/$& 3x4/,\ns/X..X..X..X.*/$& 4x3/,\ns/X.X.X.X.X.X.*/$& 6x2/,\ns/X{12}.*/$& 12x1/,\ns/X|O//g,\ns!^!()=/x/g!e,\nprint\nfor <>\n"}, {"source_code": "<>;\ns/X.*/$& 1x12/,\ns/X.{5}X.*/$& 2x6/,\ns/X...X...X.*/$& 3x4/,\ns/X..X..X..X.*/$& 4x3/,\ns/X.X.X.X.X.X.*/$& 6x2/,\ns/X{12}.*/$& 12x1/,\ns/X|O//g,\ns!^!()=/x/g!e,\nprint\nfor <>\n"}, {"source_code": "<>;\ns/X.*/$& 1x12/,\ns/X.{5}X.*/$& 2x6/,\ns/X...X...X.*/$& 3x4/,\ns/X..X..X..X.*/$& 4x3/,\ns/X.X.X.X.X.X.*/$& 6x2/,\ns/X{12}.*/$& 12x1/,\ns/X|O//g,\ns!^!()=/x/g!e,\nprint\nfor <>"}, {"source_code": "<>;\n$b=\"\",\n(/X/ and $b.=\" 1x12\"),\n(/X.{5}X/ and $b.=\" 2x6\"),\n(/X...X...X/ and $b.=\" 3x4\"),\n(/X..X..X..X/ and $b.=\" 4x3\"),\n(/X.X.X.X.X.X/ and $b.=\" 6x2\"),\n(/O/ or $b.=\" 12x1\"),\nprint ($B=()=$b=~/ /g,\"$b\\n\"),\nfor <>"}, {"source_code": "<>;\ns/X.*/$& 1x12/,\ns/X.{5}X.*/$& 2x6/,\ns/X...X...X.*/$& 3x4/,\ns/X..X..X..X.*/$& 4x3/,\ns/X.X.X.X.X.X.*/$& 6x2/,\ns/X{12}.*/$& 12x1/,\ns/X|O//g,\ns!^!()=/x/g!e,\nprint\nfor <>\n"}, {"source_code": "use warnings;\nuse strict ;\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nmy $n = <>;\nmy @data = (<>);\n\nfor my $str (@data)\n{\n chomp $str; \n $str or next;\n my @u = split //, $str;\n \n my @res = ();\n for my $rows (1, 2, 3, 4, 6, 12)\n {\n my $rl = @u / $rows;\n \n for my $i (0..$rl-1)\n {\n\n #print $i;\n my $en = join \"\", map $u[$_* $rl + $i], 0..$rows-1;\n \n if ($en eq 'X'x $rows )\n {\n #print \"ok\";\n push @res, $rows . \"x\" . $rl;\n last;\n }\n }\n \n }\n print join \" \", (scalar(@res), @res);\n print \"\\n\";\n\n \n\n}\n"}, {"source_code": "use integer;\n\n\nwhile(<>){\n\t$i=$_;\n\tfor $ii(1..$i){\n\t\tchomp($string=<>);\n\t\t@b=();\n\t\tfor $j(1..4,6,12){\n\t\t\t$_=$string;\n\t\t\t@_=split//;\n\t\t#\tprint \"@_\\n\";\n\t\t\t@a=();\n\t\t\t$f=0;\n\t\t\twhile (@_){\n\t\t\tfor $k(1..((length)/$j)){\n\t\t\t\t(shift @_) eq 'X' and $a[$k % ((length)/$j) ]++;\n\t\t\t\tif ($a[$k % ((length)/$j) ]==$j){$f++}\n\t\t\t\t}\n\t\t\t}\n\t\t\t$a=join'',@a;\n\t\t#\tprint \"$a\\n\";\n\t\t#\tprint \"@a\\n\";\n\t\t#\t$f and push @b, ((length)/$j).\"x${j}\";\n\t\t\t$f and push @b, \"${j}x\".((length)/$j);\n\t\t#\tprint $f? \"yes\\n\":\"no\\n\"\n\t\t\t}\n\t\tprint 0+@b,\" @b\\n\";\n\t\t\n\t\t}\n\t\n\t}\n\t\n"}, {"source_code": "<>;\n$b=\"\",\n(/X/ and $b.=\" 1x12\"),\n(/X.{5}X/ and $b.=\" 2x6\"),\n(/X...X...X/ and $b.=\" 3x4\"),\n(/X..X..X..X/ and $b.=\" 4x3\"),\n(/X.X.X.X.X.X/ and $b.=\" 6x2\"),\n(/O/ or $b.=\" 12x1\"),\nprint ($B=()=$b=~/ /g,\"$b\\n\"),\nfor <>"}, {"source_code": "<>;\nwhile(<>){\n\tchomp;\n\t@b=();\n\t/X/ && push @b, \"1x12\";\n\t/X.{5}X/ && push @b, \"2x6\";\n\t/X...X...X/ && push @b, \"3x4\";\n\t/X..X..X..X/ && push @b, \"4x3\";\n\t/X.X.X.X.X.X/ && push @b, \"6x2\";\n\t/O/ || push @b, \"12x1\";\n\tprint 0+@b,\" @b\\n\";\n}"}, {"source_code": "<>;\ns/X.*/$& 1x12/,\ns/X.{5}X.*/$& 2x6/,\ns/X...X...X.*/$& 3x4/,\ns/X..X..X..X.*/$& 4x3/,\ns/X.X.X.X.X.X.*/$& 6x2/,\ns/X{12}.*/$& 12x1/,\ns/X|O//g,\ns!^!()=/x/g!e,\nprint\nfor <>"}, {"source_code": "use integer;\n\n<>;\nwhile(<>){\n\t\tchomp;\n\t\t@b=();\n\t\tfor $j(1..4,6,12){\n\t\t\t@_=split//;\n\t\t\t$f=@a=();\n\t\t\t$L=(length)/$j;\n\t\t\t(shift @_) eq 'X' and $j==++$a[$_ % $L ] and $f++ for 1..@_;\n\t\t\t$f && push @b, \"${j}x$L\"\n\t\t\t}\n\t\tprint 0+@b,\" @b\\n\";\n\t}"}, {"source_code": "<>;\ns/X.*/$& 1x12/,\ns/X.{5}X.*/$& 2x6/,\ns/X...X...X.*/$& 3x4/,\ns/X..X..X..X.*/$& 4x3/,\ns/X.X.X.X.X.X.*/$& 6x2/,\ns/X{12}.*/$& 12x1/,\ns/X|O//g,\ns!^!()=/x/g!e,\nprint\nfor <>\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\n<>;\nwhile (<>){\n\tchomp;\n\t@a=();\n\t/X/&& push @a,\"1x12\";\n\t/X.....X/&& push @a,\"2x6\";\n\t/X...X...X/&& push @a,\"3x4\";\n\t/X..X..X..X/&& push @a,\"4x3\";\n\t/X.X.X.X.X.X/&& push @a, \"6x2\";\n\t/XXXXXXXXXXX/&& push @a,\"12x1\";\n\tprint 0+@a,\" @a\\n\";\n\n}\n"}], "src_uid": "b669a42ff477a62ec02dcfb87e1e60bd"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from $$$a$$$ to make it a good array. Recall that the prefix of the array $$$a=[a_1, a_2, \\dots, a_n]$$$ is a subarray consisting several first elements: the prefix of the array $$$a$$$ of length $$$k$$$ is the array $$$[a_1, a_2, \\dots, a_k]$$$ ($$$0 \\le k \\le n$$$).The array $$$b$$$ of length $$$m$$$ is called good, if you can obtain a non-decreasing array $$$c$$$ ($$$c_1 \\le c_2 \\le \\dots \\le c_{m}$$$) from it, repeating the following operation $$$m$$$ times (initially, $$$c$$$ is empty): select either the first or the last element of $$$b$$$, remove it from $$$b$$$, and append it to the end of the array $$$c$$$. For example, if we do $$$4$$$ operations: take $$$b_1$$$, then $$$b_{m}$$$, then $$$b_{m-1}$$$ and at last $$$b_2$$$, then $$$b$$$ becomes $$$[b_3, b_4, \\dots, b_{m-3}]$$$ and $$$c =[b_1, b_{m}, b_{m-1}, b_2]$$$.Consider the following example: $$$b = [1, 2, 3, 4, 4, 2, 1]$$$. This array is good because we can obtain non-decreasing array $$$c$$$ from it by the following sequence of operations: take the first element of $$$b$$$, so $$$b = [2, 3, 4, 4, 2, 1]$$$, $$$c = [1]$$$; take the last element of $$$b$$$, so $$$b = [2, 3, 4, 4, 2]$$$, $$$c = [1, 1]$$$; take the last element of $$$b$$$, so $$$b = [2, 3, 4, 4]$$$, $$$c = [1, 1, 2]$$$; take the first element of $$$b$$$, so $$$b = [3, 4, 4]$$$, $$$c = [1, 1, 2, 2]$$$; take the first element of $$$b$$$, so $$$b = [4, 4]$$$, $$$c = [1, 1, 2, 2, 3]$$$; take the last element of $$$b$$$, so $$$b = [4]$$$, $$$c = [1, 1, 2, 2, 3, 4]$$$; take the only element of $$$b$$$, so $$$b = []$$$, $$$c = [1, 1, 2, 2, 3, 4, 4]$$$\u00a0\u2014 $$$c$$$ is non-decreasing. Note that the array consisting of one element is good.Print the length of the shortest prefix of $$$a$$$ to delete (erase), to make $$$a$$$ to be a good array. Note that the required length can be $$$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 2 \\cdot 10^4$$$) \u2014 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$$$) \u2014 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 2 \\cdot 10^5$$$), 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 length of the shortest prefix of elements you need to erase from $$$a$$$ to make it a good array.", "sample_inputs": ["5\n4\n1 2 3 4\n7\n4 3 3 8 4 5 2\n3\n1 1 1\n7\n1 3 1 4 5 3 2\n5\n5 4 3 2 3"], "sample_outputs": ["0\n4\n0\n2\n3"], "notes": "NoteIn the first test case of the example, the array $$$a$$$ is already good, so we don't need to erase any prefix.In the second test case of the example, the initial array $$$a$$$ is not good. Let's erase first $$$4$$$ elements of $$$a$$$, the result is $$$[4, 5, 2]$$$. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @aa = map { $_ - 0 } split(/\\s+/,);\n \n my $ph2 = 0;\n my $l2 = 1;\n \n for(my $i=0;$i<$n-1;$i++){\n my $di = $aa[$n-2-$i] - $aa[$n-1-$i];\n if( $ph2 == 1 ){\n if( $di > 0 ){\n $ph2 = -1;\n } else {\n $l2 ++;\n }\n }\n if( $ph2 == 0 ){\n $l2 ++;\n if( $di < 0 ){\n $ph2 = 1;\n }\n }\n }\n my $r = $n - $l2;\n print \"$r\\n\";\n \n}\n\n\n\n"}], "negative_code": [], "src_uid": "731b45747081a800fc6da02efa5ce8cd"} {"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\u00a0\u2014 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\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "use bigint;\r\n\r\nchomp(my $k = );\r\nfor (; $k > 0; $k--) {\r\n my $s = ;\r\n my @arr = split(\" \", $s);\r\n my $n = int($arr[0]);\r\n my $x = int($arr[1]);\r\n my $t = int($arr[2]);\r\n my $sum = $x + $t;\r\n if ($t < $x) {\r\n print \"0\\n\"; \r\n } else {\r\n $other_max = int($t / $x);\r\n $result = $n * ($n - 1) / 2; \r\n if ($other_max <= $n) {\r\n $result = ($other_max) * ($other_max + 1) / 2;\r\n $result += $other_max * ($n - $other_max - 1);\r\n }\r\n print \"$result\\n\";\r\n }\r\n}"}], "negative_code": [{"source_code": "chomp(my $k = );\r\nfor (; $k > 0; $k--) {\r\n my $s = ;\r\n my @arr = split(\" \", $s);\r\n my $n = int($arr[0]);\r\n my $x = int($arr[1]);\r\n my $t = int($arr[2]);\r\n my $sum = $x + $t;\r\n if ($t < $x) {\r\n print \"0\\n\"; \r\n } else {\r\n $other_max = int($t / $x);\r\n $result = $n * ($n - 1) / 2; \r\n if ($other_max <= $n) {\r\n $result = ($other_max) * ($other_max + 1) / 2;\r\n $result += $other_max * ($n - $other_max - 1);\r\n }\r\n print \"$result\\n\";\r\n }\r\n}"}, {"source_code": "chomp(my $k = );\r\nfor (; $k > 0; $k--) {\r\n my $s = ;\r\n my @arr = split(\" \", $s);\r\n my $n = int($arr[0]);\r\n my $x = int($arr[1]);\r\n my $t = int($arr[2]);\r\n my $sum = $x + $t;\r\n if ($t < $x) {\r\n print \"0\\n\"; \r\n } else {\r\n $other_max = int(($t + $x - 1) / $x) - 1;\r\n $result = $n * ($n - 1) / 2; \r\n if ($other_max <= $n) {\r\n $result = ($other_max) * ($other_max + 1) / 2;\r\n $result += $other_max * ($n - $other_max - 1);\r\n }\r\n print \"$result\\n\";\r\n }\r\n}"}], "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$$$) \u00a0\u2014 the number of test cases. The description of the test cases follows. The first line contains one integer $$$n$$$ ($$$2 \\le n \\le 100$$$)\u00a0\u2014 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": "#!/usr/bin/perl\nuse Data::Dumper;\nuse integer; ### important!\nmy $mod = 10 ** 9 + 7;\n# my $mod = 998244353;\n\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\n\nwhile( $testcase -- > 0 ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @A = map { $_ - 0 } split(/\\s+/,);\n my $a0 = $A[0];\n my $res = 'YES';\n for(my $i=1;$i<$n;$i++){\n if( $A[$i] % $a0 != 0 ){\n\t $res = 'NO'; last;\n }\n }\n print \"$res\\n\";\n}\n\nexit(0);\n\nsub max {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\n return $r;\n}\nsub min {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\n return $r;\n}\nsub sum {\n my $r = 0;\n foreach my $e (@_){ $r += $e; }\n return $r;\n}\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\n my $r = shift; my $ar = shift;\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\n for(my $i=0;$i<=$#ar;$i++){ \n $rr += $ar->[$i];\n $r->[1+$i] = $rr;\n }\n}\n"}], "negative_code": [], "src_uid": "1c597da89880e87ffe791dd6b9fb2ac7"} {"nl": {"description": "Martians are actively engaged in interplanetary trade. Olymp City, the Martian city known for its spaceport, has become a place where goods from all the corners of our Galaxy come. To deliver even more freight from faraway planets, Martians need fast spaceships.A group of scientists conducts experiments to build a fast engine for the new spaceship. In the current experiment, there are $$$n$$$ elementary particles, the $$$i$$$-th of them has type $$$a_i$$$.Denote a subsegment of the particle sequence ($$$a_1, a_2, \\dots, a_n$$$) as a sequence ($$$a_l, a_{l+1}, \\dots, a_r$$$) for some left bound $$$l$$$ and right bound $$$r$$$ ($$$1 \\le l \\le r \\le n$$$). For instance, the sequence $$$(1\\ 4\\ 2\\ 8\\ 5\\ 7)$$$ for $$$l=2$$$ and $$$r=4$$$ has the sequence $$$(4\\ 2\\ 8)$$$ as a subsegment. Two subsegments are considered different if at least one bound of those subsegments differs.Note that the subsegments can be equal as sequences but still considered different. For example, consider the sequence $$$(1\\ 1\\ 1\\ 1\\ 1)$$$ and two of its subsegments: one with $$$l=1$$$ and $$$r=3$$$ and another with $$$l=2$$$ and $$$r=4$$$. Both subsegments are equal to $$$(1\\ 1\\ 1)$$$, but still considered different, as their left and right bounds differ.The scientists want to conduct a reaction to get two different subsegments of the same length. Denote this length $$$k$$$. The resulting pair of subsegments must be harmonious, i.\u00a0e. for some $$$i$$$ ($$$1 \\le i \\le k$$$) it must be true that the types of particles on the $$$i$$$-th position are the same for these two subsegments. For example, the pair $$$(1\\ 7\\ 3)$$$ and $$$(4\\ 7\\ 8)$$$ is harmonious, as both subsegments have $$$7$$$ on the second position. The pair $$$(1\\ 2\\ 3)$$$ and $$$(3\\ 1\\ 2)$$$ is not harmonious.The longer are harmonious subsegments, the more chances for the scientists to design a fast engine. So, they asked you to calculate the maximal possible length of harmonious pair made of different subsegments.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 100$$$) \u2014 the number of test cases. The following are descriptions of the test cases. The first line contains an integer $$$n$$$ ($$$2 \\le n \\le 150\\,000$$$) \u2014 the amount of elementary particles in the sequence. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\le a_i \\le 150\\,000$$$) \u2014 types of elementary particles. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3\\cdot10^5$$$.", "output_spec": "For each test, print a single integer, maximal possible length of harmonious pair made of different subsegments. If such pair does not exist, print $$$-1$$$ instead.", "sample_inputs": ["4\n7\n3 1 5 2 1 3 4\n6\n1 1 1 1 1 1\n6\n1 4 2 8 5 7\n2\n15 15"], "sample_outputs": ["4\n5\n-1\n1"], "notes": "NoteThe first test case is shown on the picture below: As you can see from it, you may choose the subsegments $$$(2\\ 1\\ 3\\ 4)$$$ and $$$(3\\ 1\\ 5\\ 2)$$$, which are a harmonious pair. Their length is equal to $$$4$$$, so the answer is $$$4$$$.In the second test case, you need to take two subsegments: one with $$$l=1$$$ and $$$r=5$$$, and one with $$$l=2$$$ and $$$r=6$$$. It's not hard to observe that these segments are a harmonious pair and considered different even though they are both equal to $$$(1\\ 1\\ 1\\ 1\\ 1)$$$.In the third test case, you cannot make a harmonious pair, so the answer is $$$-1$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy @cand;\n\t\n\tmy %h;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\texists $h{ $_[ $i ] } and push @cand, @_ - $i + $h{ $_[ $i ] };\t\n\t\t\n\t\t$h{ $_[ $i ] } = $i;\n\t\t}\n\t\n\tprint !@cand ? -1 : 0 + ( sort { $b <=> $a } @cand )[ 0 ];\n\t}\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy @cand;\n\t\n\tleftmost_two( \\@cand, \\@_ );\n\t\n\t@_ = reverse @_;\n\t\n\tleftmost_two( \\@cand, \\@_ );\n\t\n\tprint !@cand ? -1 : 0 + ( sort { $b <=> $a } @cand )[ 0 ];\n\t}\n\nsub leftmost_two {\n\tmy( $ref_cand, $ref_array ) = @_;\n\t\n\tmy %h;\n\t\n\tfor my $i ( 0 .. @{ $ref_array } - 1 ){\n\t\tif( not exists $h{ $$ref_array[ $i ] } ){\n\t\t\t$h{ $$ref_array[ $i ] } = $i;\n\t\t\t}\n\t\telsif( $h{ $$ref_array[ $i ] } == @{ $ref_array } ){\n\t\t\tnext;\n\t\t\t}\n\t\telse{\n\t\t\tpush @{ $ref_cand }, @{ $ref_array } - $i + $h{ $$ref_array[ $i ] };\n\t\t\t\n\t\t\t$h{ $$ref_array[ $i ] } = @{ $ref_array };\n\t\t\t}\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy %h;\n\t\n\tmy @cand;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tif( not exists $h{ $_[ $i ] } ){\n\t\t\t$h{ $_[ $i ] } = $i;\n\t\t\t}\n\t\telsif( $h{ $_[ $i ] } == @_ ){\n\t\t\tnext;\n\t\t\t}\n\t\telse{\n\t\t\tpush @cand, @_ - $i + $h{ $_[ $i ] };\n\t\t\t\n\t\t\t$h{ $_[ $i ] } = @_;\n\t\t\t}\n\t\t}\n\t\n\tprint !@cand ? -1 : 0 + ( sort { $b <=> $a } @cand )[ 0 ];\n\t}"}], "src_uid": "7ac5f084c403bd26802e1b941105d34b"} {"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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n@_ = ( \"2 1\", \"1 2\" );\nmy @ans;\n\nfor my $i ( 3 .. 50 ){\n\tfor( @_ ){\n\t\t$_ = \"$i \" . $_;\n\t\t}\n\t\n\tpush @_, $_[ 0 ] =~ s/(\\d+) (\\d+)/$2 $1/r;\n\t\n\tpush @ans, join \"\\n\", @_;\n\t}\n\n<>;\n\nwhile(<>){\n\tprint $ans[ $_ - 3 ];\n\t}"}], "negative_code": [], "src_uid": "85f0621e6cd7fa233cdee8269310f141"} {"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 \u2014 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": "use integer;\r\nchomp($t = );\r\nforeach (1..$t)\r\n{\r\n chomp($n = );\r\n $res = 0;\r\n while ($n > 0)\r\n {\r\n $n /= 2;\r\n $res++;\r\n }\r\n print((1 << ($res - 1)) - 1, \"\\n\");\r\n}"}], "negative_code": [{"source_code": "use integer;\r\nchomp($t = );\r\nforeach (1..$t)\r\n{\r\n chomp($n = );\r\n while ($n > 0)\r\n {\r\n $n /= 2;\r\n $res++;\r\n }\r\n print((1 << ($res - 1)) - 1, \"\\n\");\r\n}"}], "src_uid": "9b4a8bc76d634935f6a1438e8a93a781"} {"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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 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 \u2014 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": "#!/usr/bin/perl -w\n\nuse strict;\n\nmy $res = 0;\nchomp(my $n = <>);\nfor (1 .. $n) {\n\tmy $cur = 0;\n\tchomp($_ = <>);\n\tmap { $cur += $_ } split;\n\t++$res if $cur >= 2;\n}\nprint \"$res\\n\";\n"}, {"source_code": "$_=<>;$n=0;while(<>){++$n if y/1//>1}print$n;\n"}, {"source_code": "#!/usr/bin/env perl\n\nuse v5.012;\nuse warnings;\n\nmy $cnt = ;\nmy $ret = 0;\nwhile ($cnt--) {\n\tmy $ones = ( =~ tr/1/1/);\n\t$ret++ if $ones >= 2;\n}\nprint $ret;\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy $ans = 0;\nmy $count;\nwhile($n-- > 0){\n $count = 0;\n my @a = split(/ /,<>);\n $count++ if $a[0]==1;\n $count++ if $a[1]==1;\n $count++ if $a[2]==1;\n $ans++ if $count > 1;\n}\nprint \"$ans\\n\";"}, {"source_code": "#!/usr/bin/perl\n\nmy $n = <>;\nmy $cnt = 0;\n\nwhile ($n-- > 0) {\n my @nums = <> =~ /(\\d+)/g; \n if (@nums[0] + @nums[1] + @nums[2] >= 2) {\n\t$cnt++;\n }\n}\n\nprint $cnt;\n"}, {"source_code": "my $n = ;\nmy $count = 0;\nwhile($n--) {\n my($a, $b, $c) = split(' ', );\n if(($a + $b + $c) > 1) {\n $count++;\n }\n}\nprint $count;"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\n\n\nchomp($n = <>);\n$ans = 0;\nwhile ($n-- > 0) {\n\t$c = 0;\n\t$c += $_ foreach (split / /,<>);\n\t$c>=2 and $ans++;\n}\nprint $ans;"}, {"source_code": "#use strict; \n#use warnings;\n#use diagnostics;\n\nmy $lines = ;\nmy $solutions = 0;\nfor (my $i=0; $i<$lines; $i++) {\t\n\tmy $sum = 0;\n\t$line = ;\n\t@inputs = split (/ /,$line);\n\tforeach (@inputs) {\n\t\t$sum += $_;\n\t}\n\n\tif ($sum >= 2) {\n\t\t$solutions++;\n\t}\n}\n\nprint $solutions;\n"}, {"source_code": "my $n = <>;\nmy $solutions = 0;\nwhile (<>) {\n my ($x,$y,$z) = $_ =~ /^(\\d) (\\d) (\\d)$/;\n \n $solutions++ if $x+$y+$z > 1;\n}\nprint $solutions;"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nchomp($n);\n\nmy $solutions = 0;\nfor (my $i=0; $i < $n; $i++) {\n my $in = <>;\n chomp($in);\n \n my @verdicts = split(' ', $in);\n my $p = scalar grep { $_ } @verdicts;\n $solutions += ($p >= 2) ? 1 : 0;\n}\n\nprint $solutions;\n"}, {"source_code": "#!/usr/bin/perl\n\n$n = ;\n@a;\n$ans = 0;\nfor (my $i = 0; $i < $n; $i++){\n\t@buf = split ' ', ;\n\tfor (my $j = 0; $j < 3; $j++){\n\t\t$a[$i] += $buf[$j];\n\t}\n\tif ($a[$i] >= 2){\n\t\t$ans++;\t\n\t}\n}\n#print \"@a\\n\";\nprint \"$ans\\n\";\n\n"}, {"source_code": "chomp($n = );\n\n$a = 0;\n%sure = ();\n$sure{\"1 1 1\"} = 1;\n$sure{\"1 1 0\"} = 1;\n$sure{\"1 0 1\"} = 1;\n$sure{\"0 1 1\"} = 1;\n\nwhile($n-- > 0) {\n\tchomp($line = );\n\t$a++ if defined $sure{$line};\n}\nprint \"$a\\n\";"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy $res = 0;\nfor (1..$n) {\n\tmy @sols = split ' ', <>;\n\tmy $b = 0;\n\tfor (@sols) { $b += $_ };\n\t$res++ if $b > 1;\n}\nprint $res.\"\\n\";\n\n"}, {"source_code": "<>;\nwhile(<>){\n\t$c = () = $_ =~ /1/g;\n\t$re += $c > 1 ? 1 : 0;}\nprint $re;\n\n"}, {"source_code": "#!/usr/bin/perl \nuse strict;\nmy $n = ;\nmy $sum = 0;\nwhile (my $row = ) \n{\n my $sum_w = 0;\n chomp $row;\n my @inputs_p = split (' ', $row);\n foreach my $item (@inputs_p)\n {\n if ($item == 1) { $sum_w++; }\n }\n if ($sum_w >= 2) { $sum++;} \n}\nprint $sum;"}, {"source_code": "chomp(my $testCase=);\nmy $count = 0;\nwhile($testCase){\n\tmy $a=;\n\tmy $sum;\n\tmy @arr = split / /,$a;\n\tforeach (@arr){\n\t\t$sum += $_;\n\t}\n\tif($sum >= 2 ){\n\t $count++; \n\t}\n\t$testCase--;\n}\nprint \"$count\";"}, {"source_code": "my $len=0; my $count = ; for(1..$count){ @_ = split \" \", ; my $sym = 0; for(@_){ $sym += $_; } if($sym > 1){ $len += 1; } }; print \"$len\\n\";"}, {"source_code": "<>;\nwhile(<>){\n\t$ss = () = $_ =~ /1/g;\n\t$pes += $ss > 1 ? 1 : 0;\n}\nprint $pes;\n "}, {"source_code": "$n = <>;\n$ans = 0;\nfor ($i = 0; $i < $n; ++$i) {\n ($l, $m, $r) = split' ', <>;\n if ($l && $m || $l && $r || $m && $r) { ++$ans; }\n}\nprint $ans;"}, {"source_code": "#!/bin/perl\n\n$n = <>;\n$cnt = 0;\n\nfor($i=0;$i<$n;$i++)\n{\n\n $line = ;\n chomp $line;\n ($a, $b, $c) = split / +/, $line;\n\n if( $a + $b + $c >= 2 )\n {\n\n $cnt++;\n\n }\n\n}\n\nprint $cnt;\n"}, {"source_code": "$n = <>;\n$r = 0;\n\nfor (1..$n) {\n\t($a, $b, $c) = split' ', <>;\n\t$r++ if (($a+$b+$c) > 1);\n}\n\nprint $r;"}, {"source_code": "#!/usr/bin/perl\nuse strict; use warnings;\n\nmy $n = 0; ;\nwhile ()\n{ my ($a, $b, $c) = split ' ', $_; ++$n if $a + $b + $c > 1 }\nprint $n . \"\\n\"\n"}, {"source_code": "$n=;\nchomp $n;\n$ans=0;\nfor($i=0;$i<$n;$i++){\n $a=; chomp $a;\n @a=split(' ',$a);\n if($a[0]+$a[1]+$a[2]>=2){\n $ans++;\n }\n}\nprint \"$ans\\n\";\n"}, {"source_code": "1<(()=/1/g) and $i++\nfor <>;\nprint 0+$i"}], "negative_code": [{"source_code": "use strict; \nuse warnings;\nuse diagnostics;\n\nmy $lines = ;\nmy $solutions = 0;\nfor (my $i=0; $i<$lines; $i++) {\t\n\tmy $sum = 0;\n\t$sum += ;\n\t$sum += ;\n\t$sum += ;\n\n\tif ($sum >= 2) {\n\t\t$solutions++;\n\t}\n}\n\nprint $solutions.\"\\n\";\n"}, {"source_code": "#use strict; \n#use warnings;\n#use diagnostics;\n\nmy $lines = ;\nmy $solutions = 0;\nfor (my $i=0; $i<$lines; $i++) {\t\n\tmy $sum = 0;\n\t$sum += ;\n\t$sum += ;\n\t$sum += ;\n\n\tif ($sum >= 2) {\n\t\t$solutions++;\n\t}\n}\n\nprint $solutions;\n"}, {"source_code": "#use strict; \n#use warnings;\n#use diagnostics;\n\nmy $lines = ;\nmy $solutions = 0;\nfor (my $i=0; $i<$lines; $i++) {\t\n\tmy $sum = 0;\n\t$sum += ;\n\t$sum += ;\n\t$sum += ;\n\n\tif ($sum >= 2) {\n\t\t$solutions++;\n\t}\n}\n\nprint \"2\";\n#print $solutions.\"\\n\";\n"}, {"source_code": "chomp($n = );\n\n$a = 0;\n%sure = ();\n$sure{\"1 1 0\"} = 1;\n$sure{\"1 0 1\"} = 1;\n$sure{\"0 1 1\"} = 1;\n\nwhile($n-- > 0) {\n\tchomp($line = );\n\t$a++ if defined $sure{$line};\n}\nprint \"$a\\n\";"}, {"source_code": "chomp(my $testCase=);\nmy $count;\nwhile($testCase){\n\tmy $a=;\n\tmy $sum;\n\tmy @arr = split / /,$a;\n\tforeach (@arr){\n\t\t$sum += $_;\n\t}\n\tif($sum >= 2 ){\n\t $count++; \n\t}\n\t$testCase--;\n}\nprint $count;"}], "src_uid": "3542adc74a41ccfd72008faf983ffab5"} {"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,\u2009s,\u2009t (1\u2009\u2264\u2009n\u2009\u2264\u2009105;\u00a01\u2009\u2264\u2009s,\u2009t\u2009\u2264\u2009n) \u2014 the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1,\u2009p2,\u2009...,\u2009pn (1\u2009\u2264\u2009pi\u2009\u2264\u2009n) \u2014 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 \u2014 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": "while (<>){\n / (\\d+) /;\n @_=split/ /,<>;\n unshift @_,0;\n \n $b=$1;\n $a=-1;\n $i=0;\n for (1..$`){\n \n $1 == $' and ($a=0, last);\n $' == $_[$b] and ($a=++$i, last);\n $b = $_[$b];\n $i++;\n }\n \n print \"$a\\n\";\n }"}], "negative_code": [], "src_uid": "b01602b51b9103c0f31c3f50b32aa88d"} {"nl": {"description": "Vasya has found a piece of paper with an array written on it. The array consists of n integers a1,\u2009a2,\u2009...,\u2009an. Vasya noticed that the following condition holds for the array ai\u2009\u2264\u2009ai\u2009+\u20091\u2009\u2264\u20092\u00b7ai for any positive integer i (i\u2009<\u2009n).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\u2009\u2264\u2009s\u2009\u2264\u2009a1. 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the size of the array. The second line contains space-separated integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 the original array. It is guaranteed that the condition ai\u2009\u2264\u2009ai\u2009+\u20091\u2009\u2264\u20092\u00b7ai fulfills for any positive integer i (i\u2009<\u2009n).", "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\u2009\u2264\u2009s\u2009\u2264\u2009a1. 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": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp(my $n = <>);\nchomp($_ = <>);\nmy @a = split;\nmy @res = (1) x $n;\n\nfor my $i (reverse 1 .. $#a) {\n if ($a[$i] >= 0) {\n $a[$i - 1] -= $a[$i];\n $res[$i] = -1;\n } else {\n $a[$i - 1] += $a[$i];\n $res[$i] = 1;\n }\n}\nfor my $i (1 .. $#a) {\n $res[$i] *= $res[$i - 1];\n}\nif ($a[0] < 0) {\n @res = map { -$_ } @res;\n}\nmy $res = join '', map { $_ == 1 ? '+' : '-' } @res;\nprint \"$res\\n\";\n"}], "negative_code": [], "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\u00a0\u2014 1 and k. ", "input_spec": "The only line of the input contains a single integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000).", "output_spec": "The first line of the output contains a single integer k\u00a0\u2014 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": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\n# send argument as array, not reference, to function requiring list\n \n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\n# solve\n\nmy $n = read_token;\n\nmy $count = 0;\nmy $answer = '';\n\nif ($n % 2 == 1) {\n $n -= 3;\n while ($n > 0) {\n $count++;\n $n -= 2;\n $answer .= '2 ';\n }\n $count++;\n $answer .= '3';\n}\nelse {\n while ($n > 0) {\n $count++;\n $n -= 2;\n $answer .= '2 ';\n }\n}\n\nsay $count;\nsay $answer;\n"}, {"source_code": "$n = ;\n$c = int($n / 2);\nprint(\"$c\\n\");\nif($n % 2 == 0)\n{\n\twhile($c > 0)\n\t{\n\t\tprint(\"2 \");\n\t\t$c--;\n\t}\n}\nelse\n{\n\twhile($c-1 > 0)\n\t{\n\t\tprint(\"2 \");\n\t\t$c--;\n\t}\n\tprint(\"3\");\n}\nprint(\"\\n\");\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t\n\t\n\tprint int $_ / 2, \"\\n\",\n\t\tjoin ' ', \n\t\t\t(2) x ($_ / 2 - 1), \n\t\t\t(2) x (1 - $_ % 2), \n\t\t\t(3) x ($_ % 2),\n\t\n\t}"}], "negative_code": [], "src_uid": "98fd00d3c83d4b3f0511d8afa6fdb27b"} {"nl": {"description": "You are given a matrix with $$$n$$$ rows (numbered from $$$1$$$ to $$$n$$$) and $$$m$$$ columns (numbered from $$$1$$$ to $$$m$$$). A number $$$a_{i, j}$$$ is written in the cell belonging to the $$$i$$$-th row and the $$$j$$$-th column, each number is either $$$0$$$ or $$$1$$$.A chip is initially in the cell $$$(1, 1)$$$, and it will be moved to the cell $$$(n, m)$$$. During each move, it either moves to the next cell in the current row, or in the current column (if the current cell is $$$(x, y)$$$, then after the move it can be either $$$(x + 1, y)$$$ or $$$(x, y + 1)$$$). The chip cannot leave the matrix.Consider each path of the chip from $$$(1, 1)$$$ to $$$(n, m)$$$. A path is called palindromic if the number in the first cell is equal to the number in the last cell, the number in the second cell is equal to the number in the second-to-last cell, and so on.Your goal is to change the values in the minimum number of cells so that every path is palindromic.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 200$$$) \u2014 the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \\le n, m \\le 30$$$) \u2014 the dimensions of the matrix. Then $$$n$$$ lines follow, the $$$i$$$-th line contains $$$m$$$ integers $$$a_{i, 1}$$$, $$$a_{i, 2}$$$, ..., $$$a_{i, m}$$$ ($$$0 \\le a_{i, j} \\le 1$$$).", "output_spec": "For each test case, print one integer \u2014 the minimum number of cells you have to change so that every path in the matrix is palindromic.", "sample_inputs": ["4\n2 2\n1 1\n0 1\n2 3\n1 1 0\n1 0 0\n3 7\n1 0 1 1 1 1 1\n0 0 0 0 0 0 0\n1 1 1 1 1 0 1\n3 5\n1 0 1 0 0\n1 1 1 1 0\n0 0 1 0 0"], "sample_outputs": ["0\n3\n4\n4"], "notes": "NoteThe resulting matrices in the first three test cases: $$$\\begin{pmatrix} 1 & 1\\\\ 0 & 1 \\end{pmatrix}$$$ $$$\\begin{pmatrix} 0 & 0 & 0\\\\ 0 & 0 & 0 \\end{pmatrix}$$$ $$$\\begin{pmatrix} 1 & 0 & 1 & 1 & 1 & 1 & 1\\\\ 0 & 1 & 1 & 0 & 1 & 1 & 0\\\\ 1 & 1 & 1 & 1 & 1 & 0 & 1 \\end{pmatrix}$$$ "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nfor(my $i=0;$i<$t;$i++){\n my ($n,$m) = map { $_ - 0 } split(/\\s+/,);\n my @a = ();\n for(my $j=0;$j<$n;$j++){\n my @aa = map { $_ - 0 } split(/\\s+/,);\n push(@a,\\@aa);\n }\n my $l = int( ($n + $m - 1) / 2 );\n my $tot = 0;\n for(my $j=0;$j<$l;$j++){\n my %c = ();\n for(my $row = 0; $row <= $n + $m - 1; $row ++ ){\n my $col1 = $j - $row;\n if( $row < $n and 0<=$col1 and $col1<$m ){\n $c{$a[$row]->[$col1]} ++;\n $c{$a[$n-1-$row]->[$m-1-$col1]} ++;\n }\n }\n $tot += ( $c{'0'} < $c{'1'} ? $c{'0'} : $c{'1'} );\n }\n print \"$tot\\n\";\n}\n\n\n"}], "negative_code": [], "src_uid": "b62586b55bcfbd616d936459c30579a6"} {"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\u00a0(1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n), 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\u00a0(1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n), 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\u00a0(1\u2009\u2264\u2009n\u2009\u2264\u2009105). The second line contains n integers: v1,\u2009v2,\u2009...,\u2009vn\u00a0(1\u2009\u2264\u2009vi\u2009\u2264\u2009109) \u2014 costs of the stones. The third line contains an integer m\u00a0(1\u2009\u2264\u2009m\u2009\u2264\u2009105) \u2014 the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and r\u00a0(1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009n;\u00a01\u2009\u2264\u2009type\u2009\u2264\u20092), 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 \u2014 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": "#!perl -w\n\nuse strict;\n\nchomp ( my $n = ) ;\nmy @stones = split /\\s+/s , ;\nchomp ( my $questions = ) ;\nmy @sort_stones = sort{$a <=> $b} @stones;\n\nmy $temp = 0 ;\nmy @accstones = map{$temp += $_ } @stones ;\nunshift @accstones , 0 ;\n$temp = 0 ;\nmy @accsortstones = map{$temp += $_ } @sort_stones ;\nunshift @accsortstones , 0 ;\n\nwhile( $questions-- ){\n my ($type,$low,$high) = split /\\s+/s , ;\n my $summ=0 ;\n if($type==1){\n $summ = $accstones[$high]-$accstones[$low-1];\n }else{\n $summ = $accsortstones[$high]-$accsortstones[$low-1];\n }\n\n print \"$summ\\n\";\n}"}], "negative_code": [], "src_uid": "c764b9f87cb5e5872eb157c3d2b7f3c5"} {"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": "use strict;\nuse warnings FATAL => 'all';\n\n\nmy $n = ;\nchomp $n;\n$n = int($n);\n\nmy $a = 9 * $n;\nmy $b = 8 * $n;\n\nprint $a, ' ', $b"}, {"source_code": "#!/usr/bin/env perl\npackage Codeforces::Task::A;\nuse v5.10;\nuse strict;\nuse warnings;\n\nsub min {\n my ($a, $b) = @_;\n if ($a < $b) {\n\t\treturn $a;\n\t} else {\n return $b;\n }\n}\nsub max {\n my ($a, $b) = @_;\n if ($a > $b) {\n return $a;\n } else {\n return $b;\n }\n}\n\nsub solve {\n my $n = int(<>);\n if ($n % 2 == 0) {\n print $n + 4, \" 4\";\n } else {\n print $n + 9, \" 9\";\n }\n print \"\\n\";\n}\n\nsub main {\n my $t = 1;\n# $t = int(<>);\n while ($t--) {\n solve();\n }\n return 0;\n}\n\nexit(main(@ARGV));\n\n"}, {"source_code": "#Perl v5.18.2\n$n = ;\n\nprint 9*$n;\nprint \" \";\nprint 8*$n;"}], "negative_code": [{"source_code": "use strict;\nuse warnings FATAL => 'all';\n\n\nmy $n = ;\nchomp $n;\n$n = int($n);\n\nmy $a = 3 * $n;\nmy $b = 2 * $n;\n\nprint $a, ' ', $b"}], "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.\u2009e. 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$$$) \u2014 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$$$) \u2014 elements of the array $$$a$$$.", "output_spec": "In the first line print one integer $$$k$$$ \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\t$sum += $_ for @_;\n\t\n\tmy( $max1, $max2 ) = sort { $b <=> $a } @_;\n\t\n\tmy @ans;\n\t\n\tmy $i = 0;\n\t\n\tfor( @_ ){\n\t\t$i ++;\n\t\tif( $_ == $max1 ){\n\t\t\tif( $sum - $_ == 2 * $max2 ){\n\t\t\t\tpush @ans, $i;\n\t\t\t\t}\n\t\t\t}\n\t\telse{\n\t\t\tif( $sum - $_ == 2 * $max1 ){\n\t\t\t\tpush @ans, $i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint for ~~ @ans, \"@ans\";\n\t}"}], "negative_code": [], "src_uid": "4cf0fe49f7ebf058317ac848043031a5"} {"nl": {"description": "There is a toy building consisting of $$$n$$$ towers. Each tower consists of several cubes standing on each other. The $$$i$$$-th tower consists of $$$h_i$$$ cubes, so it has height $$$h_i$$$.Let's define operation slice on some height $$$H$$$ as following: for each tower $$$i$$$, if its height is greater than $$$H$$$, then remove some top cubes to make tower's height equal to $$$H$$$. Cost of one \"slice\" equals to the total number of removed cubes from all towers.Let's name slice as good one if its cost is lower or equal to $$$k$$$ ($$$k \\ge n$$$). Calculate the minimum number of good slices you have to do to make all towers have the same height. Of course, it is always possible to make it so.", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$n \\le k \\le 10^9$$$) \u2014 the number of towers and the restriction on slices, respectively. The second line contains $$$n$$$ space separated integers $$$h_1, h_2, \\dots, h_n$$$ ($$$1 \\le h_i \\le 2 \\cdot 10^5$$$) \u2014 the initial heights of towers.", "output_spec": "Print one integer \u2014 the minimum number of good slices you have to do to make all towers have the same heigth.", "sample_inputs": ["5 5\n3 1 2 2 4", "4 5\n2 3 4 5"], "sample_outputs": ["2", "2"], "notes": "NoteIn the first example it's optimal to make $$$2$$$ slices. The first slice is on height $$$2$$$ (its cost is $$$3$$$), and the second one is on height $$$1$$$ (its cost is $$$4$$$)."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t@_ = sort { $b <=> $a } split ' ', <>;\n\t\n\tmy $min = $_[ @_ - 1 ];\n\t\n\t$_ -= $min - 1 for @_;\n\t\n\t$debug and print '-' x 20;\n\t$debug and print \"[@_]\";\n\t\n\tmy $wide = 1;\n\tmy $cnt = 0;\n\t\n\tif( $_[ 0 ] == $_[ @_ - 1 ] ){\n\t\tprint 0;\n\t\tnext;\n\t\t}\n\t\n\tOUTER:\n\twhile( @_ > 1 ){\n\t\t$cnt ++;\n\t\t$debug and print \" cnt: $cnt\";\n\t\tmy $to_cut = $k;\n\t\twhile( $to_cut > 0 ){\n\t\t\t$debug and print \" to_cut: $to_cut\";\n\t\t\tif( @_ < 2 ){\n\t\t\t\tlast OUTER;\n\t\t\t\t}\n\t\t\tmy $diff = $_[ 0 ] - $_[ 1 ];\n\t\t\tif( $diff * $wide <= $to_cut ){\n\t\t\t\t$to_cut -= $diff * $wide;\n\t\t\t\t$wide ++;\n\t\t\t\tshift @_;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\twhile( $diff * $wide > $to_cut ){\n\t\t\t\t\t$diff >>= 1;\n\t\t\t\t\t}\n\t\t\t\tif( $diff == 0 ){\n\t\t\t\t\tnext OUTER;\n\t\t\t\t\t}\n\t\t\t\t$to_cut -= $diff * $wide;\n\t\t\t\t$_[ 0 ] -= $diff;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint $cnt - 0;\n\t}"}], "negative_code": [], "src_uid": "676729309dfbdf4c9d9d7c457a129608"} {"nl": {"description": "A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice.Applying the simplest variant of median smoothing to the sequence of numbers a1,\u2009a2,\u2009...,\u2009an will result a new sequence b1,\u2009b2,\u2009...,\u2009bn obtained by the following algorithm: b1\u2009=\u2009a1, bn\u2009=\u2009an, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence. For i\u2009=\u20092,\u2009...,\u2009n\u2009-\u20091 value bi is equal to the median of three values ai\u2009-\u20091, ai and ai\u2009+\u20091. The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1.In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it.Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.", "input_spec": "The first input line of the input contains a single integer n (3\u2009\u2264\u2009n\u2009\u2264\u2009500\u2009000)\u00a0\u2014 the length of the initial sequence. The next line contains n integers a1,\u2009a2,\u2009...,\u2009an (ai\u2009=\u20090 or ai\u2009=\u20091), giving the initial sequence itself.", "output_spec": "If the sequence will never become stable, print a single number \u2009-\u20091. Otherwise, first print a single integer\u00a0\u2014 the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print n numbers separated by a space \u00a0\u2014 the resulting sequence itself.", "sample_inputs": ["4\n0 0 1 1", "5\n0 1 0 1 0"], "sample_outputs": ["0\n0 0 1 1", "2\n0 0 0 0 0"], "notes": "NoteIn the second sample the stabilization occurs in two steps: , and the sequence 00000 is obviously stable."}, "positive_code": [{"source_code": "$n = <>;\n@m = split ' ', <>;\n$cnt = 0;\n@sum = ();\n$pos = 0;\n$ans = 0;\nfor($i = 1;$i < $n;$i ++) {\n if ($i < $n - 1 and $m[$i - 1] == $m[$i + 1] and $m[$i] != $m[$i - 1]) {\n if($cnt == 0) {\n $start = $m[$i - 1];\n $end = $m[$i];\n }\n $cnt ++;\n } else {\n if (int(($cnt + 1) / 2) > $ans) {\n $ans = int(($cnt + 1) / 2);\n }\n push @sum, @m[$pos..$i - $cnt - 1];\n if ($cnt % 2 == 0) {\n for($j = 0;$j < $cnt / 2;$j ++) {\n push @sum, $start;\n }\n for($j = 0;$j < $cnt / 2;$j ++) {\n push @sum, $end;\n }\n } else {\n for($j = 0;$j < $cnt;$j ++) {\n push @sum, $start;\n }\n }\n $pos = $i;\n $cnt = 0;\n }\n}\nprint $ans, \"\\n\";\npush @sum, $m[-1];\n$m = join ' ', @sum;\nprint $m, \"\\n\";\n"}, {"source_code": "<>;\n\n$_ = join '', split ' ', <>;\n\ns/^./$&$&/, s/.$/$&$&/;\n\ns!\n\t(?<= (.) ) # lookbehind\n\t(\n\t\t(?<= 00)\n\t\t(?: 10 )+\n\t|\n\t\t(?<= 11)\n\t\t(?: 01 )+\n\t)\n\t(?= (.) ) # lookahead\n!\n\t$c = (length $2) / 2, # half length of repetition 10.. or 01..\n\t($max < $c and $max = $c), \n\t1 * $1 x $c . $3 x $c\n!gex;\n\ns/.//, chop;\n\t\nprint join \"\\n\", int $max, join ' ', split '', $_"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = join '', split ' ', <>;\n\ts/^./$&$&/;\n\ts/.$/$&$&/;\n\n\t$max = 0;\n\ts!(?<=00)((?:10)+)(?=(.))!\n\t\t$c = (length $1) / 2, \n\t\t($max < $c and $max = $c), \n\t\t0 x $c . $2 x $c\n\t!ge;\n\n\ts!(?<=11)((?:01)+)(?=(.))! \n\t\t$c = (length $1) / 2, \n\t\t($max < $c and $max = $c), \n\t\t1 x $c . $2 x $c\n\t!ge;\n\n\ts/.//;\n\tchop;\n\t\n\tprint int $max;\n\tprint join ' ', split '';\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = join '', split ' ', <>;\n\n\t$max = 0;\n\ts!(?:^|(?<=0))0((?:10)+)1(?=1|$)! \n\t\t$c = (length $1) / 2, \n\t\t($max < $c and $max = $c), \n\t\t0 . 0 x $c . 1 x $c . 1\n\t!ge;\n\n\ts!(?:^|(?<=1))1((?:01)+)0(?=0|$)! \n\t\t$c = (length $1) / 2, \n\t\t($max < $c and $max = $c), \n\t\t1 . 1 x $c . 0 x $c . 0\n\t!ge;\n\n\ts!(?:^|(?<=0))0((?:10)+)(?=0|$)!\n\t\t$c = (length $1) / 2, \n\t\t($max < $c and $max = $c), \n\t\t0 . 0 x $c . 0 x $c\n\t!ge;\n\n\ts!(?:^|(?<=1))1((?:01)+)(?=1|$)!\n\t\t$c = (length $1) / 2, \n\t\t($max < $c and $max = $c), \n\t\t1 . 1 x $c . 1 x $c\n\t!ge;\n\t\n\tprint int $max;\n\tprint join ' ', split //;\n\t}"}, {"source_code": "<>;\n\n$_ = join '', split ' ', <>;\n\ns/^./$&$&/, s/.$/$&$&/;\n\ns!\n\t(?<= (.) ) # lookbehind\n\t(\n\t\t(?<= 00)\n\t\t(?: 10 )+\n\t|\n\t\t(?<= 11)\n\t\t(?: 01 )+\n\t)\n\t(?= (.) ) # lookahead\n!\n\t$c = (length $2) / 2, # half length of repetition 10.. or 01..\n\t($max < $c and $max = $c), \n\t$1 x $c . $3 x $c\n!gex;\n\ns/.//, chop;\n\t\nprint join \"\\n\", int $max, join ' ', split '', $_"}], "negative_code": [{"source_code": "$n = <>;\n@m = split ' ', <>;\n$ans = 0;\nwhile(++ $ans) {\n $flag = 0;\n @newm = @m;\n for($i = 1;$i < $n - 1;$i ++) {\n if ($m[$i - 1] == $m[$i + 1] and $m[$i] != $m[$i - 1]) {\n $newm[$i] = 1 - $newm[$i];\n $flag = 1;\n }\n }\n if ($flag == 0) {\n last;\n }\n @m = @newm;\n print @m, \"\\n\";\n}\nprint $ans - 1, \"\\n\";\nfor ($i = 0;$i < $n;$i ++) {\n print $m[$i];\n print \"\\n\" if $i == $n - 1;\n print \" \" if $i < $n - 1;\n}\n"}, {"source_code": "<>;\n\n$_ = join '', split ' ', <>;\n\ns/^./$&$&/, s/.$/$&$&/;\n\ns! (?<= (0)0 | (1)1 ) # lookbehind\n ( (?: (??{1-$1})\\1 )+ ) # capture $2\n (?= (.) ) #lookahead\n!\n\t$c = (length $2) / 2, # half length of repetition 10.. or 01..\n\t($max < $c and $max = $c), \n\t$1 x $c . $3 x $c\n!gex;\n\ns/.//, chop;\n\t\nprint join \"\\n\", int $max, join ' ', split '', $_"}, {"source_code": "<>;\n\n$_ = join '', split ' ', <>;\n\ns/^./$&$&/, s/.$/$&$&/;\n\ns! (?<= (.)) \\1 \\K\n (?= ( (??{ 1 - $1 . $1 }) ) )\n (\n \\2{1,}\n )\n (?= (.) )\n!\n\t$c = (length $3) / 2, # half length of repetition 10.. or 01..\n\t($max < $c and $max = $c), \n\t$1 x $c . $4 x $c\n!gex;\n\ns/.//, chop;\n\t\nprint join \"\\n\", int $max, join ' ', split '', $_"}, {"source_code": "<>;\n\n$_ = join '', split ' ', <>;\n\ns/^./$&$&/, s/.$/$&$&/;\n\ns! (?<= (.)) \\1 \\K\n (?= ( (??{ 1 - $1 . $1 }) ) )\n (\n \\2+\n )\n (?= (.) )\n!\n\t$c = (length $3) / 2, # half length of repetition 10.. or 01..\n\t($max < $c and $max = $c), \n\t$1 x $c . $4 x $c\n!gex;\n\ns/.//, chop;\n\t\nprint join \"\\n\", int $max, join ' ', split '', $_"}, {"source_code": "<>;\n\n$_ = join '', split ' ', <>;\n\ns/^./$&$&/, s/.$/$&$&/;\n\ns! (?<= 00 | (1)1 ) # lookbehind\n ( (?: (??{1-$1}) (??{1*$1}) )+ ) # capture $2\n (?= (.) ) #lookahead\n!\n\t$c = (length $2) / 2, # half length of repetition 10.. or 01..\n\t($max < $c and $max = $c), \n\t1*$1 x $c . $3 x $c\n!gex;\n\ns/.//, chop;\n\t\nprint join \"\\n\", int $max, join ' ', split '', $_"}, {"source_code": "<>;\n\n$_ = join '', split ' ', <>;\n\ns/^./$&$&/, s/.$/$&$&/;\n\ns! (.)\\1 \\K \n ( (?: (??{1-$1})\\1 )+ ) # $2\n (?= (.) ) \n!\n\t$c = (length $2) / 2, # half length of repetition 10.. or 01..\n\t($max < $c and $max = $c), \n\t$1 x $c . $3 x $c\n!gex;\n\ns/.//, chop;\n\t\nprint join \"\\n\", int $max, join ' ', split '', $_"}, {"source_code": "<>;\n\n$_ = join '', split ' ', <>;\n\ns/^./$&$&/, s/.$/$&$&/;\n\n%hash = map { $_, $_ x 1e2 } '01', '10';\n\ns! (?<= (.)) \\1 \\K # $1\n (?= ( (??{ 1 - $1 . $1 }) ) ) # $2\n (\n (??{$2})+\n ) # $3\n (?= (.) ) # $4\n!\n\t$c = (length $3) / 2, # half length of repetition 10.. or 01..\n\t($max < $c and $max = $c), \n\t$1 x $c . $4 x $c\n!gex;\n\ns/.//, chop;\n\t\nprint join \"\\n\", int $max, join ' ', split '', $_"}, {"source_code": "<>;\n\n$_ = join '', split ' ', <>;\n\ns/^./$&$&/, s/.$/$&$&/;\n\ns! (?<= (.)) \\1 \\K\n ( (?: (??{1-$1})\\1 )+ ) # $2\n (?= (.) ) \n!\n\t$c = (length $2) / 2, # half length of repetition 10.. or 01..\n\t($max < $c and $max = $c), \n\t$1 x $c . $3 x $c\n!gex;\n\ns/.//, chop;\n\t\nprint join \"\\n\", int $max, join ' ', split '', $_"}, {"source_code": "<>;\n\n$_ = join '', split ' ', <>;\n\ns/^./$&$&/, s/.$/$&$&/;\n\ns! (?<= (.)) \\1 \\K\n (?= ( (??{ 1 - $1 . $1 }) ) )\n (\n (?:\\2)+\n )\n (?= (.) )\n!\n\t$c = (length $3) / 2, # half length of repetition 10.. or 01..\n\t($max < $c and $max = $c), \n\t$1 x $c . $4 x $c\n!gex;\n\ns/.//, chop;\n\t\nprint join \"\\n\", int $max, join ' ', split '', $_"}, {"source_code": "<>;\n\n$_ = join '', split ' ', <>;\n\ns/^./$&$&/, s/.$/$&$&/;\n\n%hash = map { $_, $_ x 1e2 } '01', '10';\n\ns! (?<= (.)) \\1 \\K # $1\n (?= ( (??{ 1 - $1 . $1 }) ) ) # $2\n (\n (?: $hash{ $2 } )*\n \\2+\n ) # $3\n (?= (.) ) # $4\n!\n\t$c = (length $3) / 2, # half length of repetition 10.. or 01..\n\t($max < $c and $max = $c), \n\t$1 x $c . $4 x $c\n!gex;\n\ns/.//, chop;\n\t\nprint join \"\\n\", int $max, join ' ', split '', $_"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = join '', split ' ', <>;\n\n\t$max = 0;\n\ts!(^|0)0((?:10)+)1(1|$)! \n\t\t$c = (length $2) / 2, \n\t\t($max < $c and $max = $c), \n\t\t$1 . 0 . 0 x $c . 1 x $c . 1 . $3 \n\t!ge;\n\n\ts!(^|1)1((?:01)+)0(0|$)! \n\t\t$c = (length $2) / 2, \n\t\t($max < $c and $max = $c), \n\t\t$1 . 1 . 1 x $c . 0 x $c . 0 . $3 \n\t!ge;\n\n\ts!^0(?:10)*$! \n\t\t$c = (length $&) / 2, \n\t\t($max < $c and $max = $c), \n\t\t0 x length\n\t!ge;\n\t\n\ts!^1(?:01)*$! \n\t\t$c = (length $&) / 2, \n\t\t($max < $c and $max = $c), \n\t\t1 x length\n\t!ge;\n\t\n\tprint int $max;\n\tprint join ' ', split //;\n\t}"}, {"source_code": "<>;\n\n$_ = join '', split ' ', <>;\n\ns/^./$&$&/, s/.$/$&$&/;\n\n%hash = map { $_, $_ x 3e1 } '01', '10';\n\ns! (?<= (.) ) \\1++ \\K # $1\n (?= ( (??{ 1 - $1 . $1 }) |) ) # $2\n (\n (?:$hash{$2})*+\n \\2*\n ) # $3\n (?= (.|) ) # $4\n!\n\t$c = (length $3) / 2, # half length of repetition 10.. or 01..\n\t($max < $c and $max = $c), \n\t$1 x $c . $4 x $c\n!gex;\n\ns/.//, chop;\n\t\nprint join \"\\n\", int $max, join ' ', split '', $_"}], "src_uid": "5da1c96b88b4ac0b698a37e4d2437ccb"} {"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\u2009\u2264\u2009w,\u2009h\u2009\u2264\u2009100) \u2014 the width and height of an image in pixels. The picture is given in h lines, each line contains w characters \u2014 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 \u2014 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": "#!/usr/bin/env perl\n\nmy @result = ();\nmy $line = readline(*STDIN);\nmy ($w, $h) = split(' ', $line);\n\nfor (my $n=0; $n<$h; $n++) {\n $line = readline(*STDIN);\n chomp($line);\n my $currentw = $n*2;\n my $currenth = 0;\n while ( $line =~ m/(.)/g) {\n \tmy $nexth = $currenth + 1;\n \tmy $nextw = $currentw + 1;\n $result[$currenth][$currentw] = $result[$nexth][$currentw] = $result[$currenth][$nextw]= $result[$nexth][$nextw] = $1;\n $currenth += 2;\n }\n}\n\nfor (my $W = 0; $W<$w*2; $W++)\n{\n\tfor (my $H = 0; $H<$h*2; $H++)\t\n\t{\n\t\tprint $result[$W][$H];\n\t}\n\tprint \"\\n\";\n}"}, {"source_code": "#!/usr/bin/env perl\n\nuse 5.011;\nuse strict;\nuse warnings;\n\nmy ($w, $h) = split /\\s/, <>;\nmy @a;\npush @a, [split \"\"] while <>;\n\nmy @b;\nfor (my $i = 0; $i < $w; $i++) {\n my @x;\n for (my $j = 0; $j < $h; $j++) {\n push @x, $a[$h - $j - 1][$i];\n }\n push @b, \\@x;\n}\n\nmy @c;\nfor (my $i = 0; $i < $w; $i++) {\n my @x;\n for (my $j = $h - 1; $j >= 0; $j--) {\n push @x, $b[$i][$j];\n push @x, $b[$i][$j];\n }\n push @c, \\@x;\n push @c, \\@x;\n}\n\nsay @$_ for @c;\n"}], "negative_code": [], "src_uid": "9af19e1b482f7f0bcbffc754cf324092"} {"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$$$)\u00a0\u2014 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": "##!/usr/local/bin/perl \r\n\r\nuse strict;\r\nuse warnings;\r\n\r\n#use Data::Dumper qw(Dumper);\r\nuse feature qw(say);\r\n\r\n#print \"------------------------------------------------------------------------------\\n\";\r\n\r\n#open my $fh,'<','codef_1703_input.txt' or die();\r\nmy @input;\r\nwhile()\r\n{\r\n chomp;\r\n push(@input,$_);\r\n}\r\n#close($fh);\r\n\r\nshift(@input);\r\n#say(Dumper @input);\r\n\r\nforeach (@input)\r\n{\r\n if($_ =~ /yes/i)\r\n {\r\n say \"YES\";\r\n }else \r\n {\r\n say \"NO\";\r\n }\r\n}\r\n\r\n#say(Dumper %hash);\r\n\r\n#print \"------------------------------------------------------------------------------\\n\";"}, {"source_code": "<>;print/yes/i?$_:\"NO \"for(<>)"}, {"source_code": "<>;print uc eq\"YES\\n\"?uc:\"NO \"for(<>)"}, {"source_code": "<>;print/yes/i?$_:\"NO \"for(<>)"}, {"source_code": "<>;print/yes/i?$_:\"NO \"for(<>)"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tprint m/^yes$/i ? \"YES\" : \"NO\";\n\t}"}, {"source_code": "$_=<>;while(<>){print/yes/i?\"YES\\n\":\"NO\\n\"}\n\n"}], "negative_code": [], "src_uid": "4c0b0cb8a11cb1fd40fef47616987029"} {"nl": {"description": "Let's call a number k-good if it contains all digits not exceeding k (0,\u2009...,\u2009k). 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\u2009\u2264\u2009n\u2009\u2264\u2009100, 0\u2009\u2264\u2009k\u2009\u2264\u20099). The i-th of the following n lines contains integer ai without leading zeroes (1\u2009\u2264\u2009ai\u2009\u2264\u2009109).", "output_spec": "Print a single integer \u2014 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/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\nchomp($a=$');\n$t=0;\nwhile (<>){\n\tchomp;\n\tfor $i(0..$a){\n\t\t/$i/ or $k++;\n\t\t\n\t\t}\n\n\t$k or $t++;\n\t$k=0;\n\t\n\t}\nprint $t;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "my ($n, $k) = split ' ', <>;\n$count = 0;\nLabel:\nfor (1..$n) {\n my $num = <>;\n for (0..$k) {\n next Label if $num !~ /$_/;\n }\n $count++;\n}\nprint $count;"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\nchomp($a=$'+1);\n$t=0;\nwhile (<>){\n\tchomp;\n\t@a=split//;\n\t@a=sort @a;\n#\tprint \" @a\\n\";\n\tfor $i(@a){\n\t\t($i==$j++) or ($k++)\n\t\t}\n\t$j=0;\n\t@a==$a or $k++;\n\t\n\t$k or $t++;\n\t$k=0;\n\t\n\t}\nprint $t;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\nchomp($a=$'+1);\n$t=0;\nwhile (<>){\n\tchomp;\n\t@a=split//;\n\t@a=sort @a;\n#\tprint \" @a\\n\";\n\tfor $i(@a){\n\t\t($i==$j++) or ($k++)\n\t\t}\n\t$j=0;\n\t@a<1 and push @a,1;\n\t@a==$a or $k++;\n\t\n\t$k or $t++;\n\t$k=0;\n\t\n\t}\nprint $t;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "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\u2009\u2264\u2009|s|\u2009\u2264\u2009100\u2009000) 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\u2009\u2264\u2009i\u2009\u2264\u2009|s|, such that s1\u2009=\u2009t1,\u2009s2\u2009=\u2009t2,\u2009...,\u2009si\u2009-\u20091\u2009=\u2009ti\u2009-\u20091, and si\u2009<\u2009ti."}, "positive_code": [{"source_code": "$_ = <>; \ns% [b-z]+ % $&=~y/b-z/a-y/r %xe || s/a$/z/;\nprint;\n"}, {"source_code": "# cf708a # solution courtesy 20147365 s_p\nuse strict;\n$_=<>;\ns% [b-z]+ % $& =~ tr/b-z/a-y/r %xe || s/a$/z/;\nprint $_;\n"}, {"source_code": "# cf708a\nuse strict;\nmy $c=getc();\nmy $s=0;\nwhile(defined($c)) {\n if($s==0) { \n if($c ne 'a') { $s=1; }\n }\n if($s==1) {\n if($c ne 'a') { $c=chr(ord($c)-1); }\n else { $s=2; }\n }\n my $nextC=getc();\n if($nextC eq \"\\n\") { $nextC=undef }\n print defined($nextC) || $s!=0 ? $c : 'z';\n $c=$nextC;\n}\n"}], "negative_code": [{"source_code": "$_ = <>; \ns% [b-z]+ % $&=~y/b-z/a-y/r %xe;\nprint;\n"}, {"source_code": "# cf708a\nuse strict;\nmy $c;\nmy $s=0;\nwhile(defined($c=getc())) {\n if($s==0) { \n if($c ne 'a') { $s=1; }\n }\n if($s==1) {\n if($c ne 'a') { $c=chr(ord($c)-1); }\n else { $s=2; }\n }\n print $c;\n}\n"}, {"source_code": "# cf708a\nuse strict;\nmy $c=getc();\nmy $s=0;\nwhile(defined($c)) {\n if($s==0) { \n if($c ne 'a') { $s=1; }\n }\n if($s==1) {\n if($c ne 'a') { $c=chr(ord($c)-1); }\n else { $s=2; }\n }\n my $nextC=getc();\n print defined($nextC) || $s!=0 ? $c : 'z';\n $c=$nextC;\n}\n"}], "src_uid": "e70708f72da9a203b21fc4112ede9268"} {"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\u00a0\u2014 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.\u00a0e. 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t%h = %g = @D = ();\r\n\t@A = split ' ', <>;\r\n\t@B = split ' ', <>;\r\n\t@C = split ' ', <>;\r\n\t\r\n\tundef $h{ $_ } for @B;\r\n\t\r\n\tfor( 0 .. @A - 1 ){\r\n\t\t$A[ $_ ] != $B[ $_ ] and $h{ $B[ $_ ] }{ $_ + 1 } = 1;\r\n\t\t$g{ $B[ $_ ] } = $_ + 1\r\n\t\t}\r\n\t\r\n\tfor( @C ){\r\n\t\tif( exists $h{ $_ } ){\r\n\t\t\t( $key ) = keys %{ $h{ $_ } };\r\n\t\t\t$key and delete $h{ $_ }{ $key };\r\n\t\t\tpush @D, $key || $g{ $_ }\r\n\t\t\t}\r\n\t\telse{\r\n\t\t\tpush @D, 0\r\n\t\t\t}\r\n\t\t}\r\n\t\r\n\tif( ! $D[ @D - 1 ] or map { keys %{ $h{ $_ } } } keys %h ){\r\n\t\tprint 'NO'\r\n\t\t}\r\n\telse{\r\n\t\tmap { $_ ||= $D[ @D - 1 ] } @D;\r\n\t\tprint for 'YES', \"@D\"\r\n\t\t}\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\tmy @C = split ' ', <>;\n\t\n\t$debug and print \"\\@A:[@A]\";\n\t$debug and print \"\\@B:[@B]\";\n\t$debug and print \"\\@C:[@C]\";\n\t\n\tmy %h;\n\tmy %g;\n\t\n\tundef $h{ $_ } for @B;\n\t\n\tfor my $i ( 0 .. @A - 1 ){\n\t\t$A[ $i ] != $B[ $i ] and $h{ $B[ $i ] }{ $i + 1 } = 1;\n\t\t$g{ $B[ $i ] } = $i + 1;\n\t\t}\n\t\n\t$debug and print map \"keys %h:[$_]\", join \" \", sort keys %h;\n\t\n\tmy @D;\n\t\n\tfor( @C ){\n\t\tif( exists $h{ $_ } ){\n\t\t\tif( 0 < keys %{ $h{ $_ } } ){\n\t\t\t\tmy( $key ) = keys %{ $h{ $_ } };\n\t\t\t\tdelete $h{ $_ }{ $key };\n\t\t\t\tpush @D, $key;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tpush @D, $g{ $_ };\n\t\t\t\t}\n\t\t\t}\n\t\telse{\n\t\t\tpush @D, -1;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@D:[@D]\";\n\t\n\tif( $D[ @D - 1 ] == -1 ){\n\t\tprint 'NO';\n\t\t}\n\telsif( 0 < map { keys %{ $h{ $_ } } } keys %h ){\n\t\tprint 'NO';\n\t\t}\n\telse{\n\t\tmap { $_ == -1 and $_ = $D[ @D - 1 ] } @D;\n\t\tprint 'YES';\n\t\tprint \"@D\";\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$m) = map { $_ - 0 } split(/\\s+/,);\r\n my @aa = map { $_ - 0 } split(/\\s+/,);\r\n my @bb = map { $_ - 0 } split(/\\s+/,);\r\n my @cc = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my @need_col = (); $#need_col = $n + 10;\r\n my @need_col_i = (); $#need_col_i = $n + 10;\r\n \r\n my $total_need = 0;\r\n \r\n my @btype = (); $#btype = $n + 10;\r\n \r\n for(my $i=0;$i<$n;$i++){\r\n if( $aa[$i] != $bb[$i] ){\r\n $need_col[$bb[$i]]++;\r\n push(@{$need_col_i[$bb[$i]]},$i);\r\n $total_need ++;\r\n }\r\n push(@{$btype[$bb[$i]]},$i);\r\n }\r\n \r\n my @tr = (); $#tr = $m - 1;\r\n my $init = 1;\r\n my $prev = -1;\r\n my $init_pass = 0;\r\n for(my $i=$m-1;$i>=0;$i--){\r\n my $c1 = $cc[$i];\r\n if( $need_col[$c1] - 0 > 0 ){\r\n $need_col[$c1] --;\r\n $tr[$i] = $need_col_i[$c1]->[$need_col[$c1]];\r\n $prev = $tr[$i];\r\n $total_need --;\r\n $init = 0;\r\n } else {\r\n if( $init==1 and !ref($btype[$c1]) and $prev < 0 ){\r\n $init = -1; last;\r\n }\r\n if( $prev >= 0 ){\r\n $tr[$i] = $prev;\r\n } else {\r\n $tr[$i] = $btype[$c1]->[0];\r\n $prev = $tr[$i];\r\n }\r\n }\r\n }\r\n if( $init < 0 or $total_need > 0 ){\r\n print \"NO\\n\"; next;\r\n }\r\n print \"YES\\n\";\r\n for(my $i=0;$i<$m;$i++){\r\n my $v = $tr[$i]+1;\r\n print \" \" if $i > 0;\r\n print \"$v\";\r\n }\r\n print \"\\n\";\r\n \r\n}\r\n\r\nexit(0);\r\n\r\n\r\n#!/usr/bin/perl\r\nmy ($n,$m) = map {$_ - 0} split(/\\s+/o,);\r\n\r\nmy @tr = ();\r\n$#tr = $n-1;\r\nmy @par = ();\r\n$#par = $n-1;\r\n\r\nfor(my $i=0;$i<$n;$i++){\r\n $par[$i] = $i;\r\n}\r\n\r\nfor(my $i=0;$i<$m;$i++){\r\n my ($x,$y,$z) = map {$_ - 0} split(/\\s+/o,);\r\n unite($x-1,$y-1);\r\n}\r\n\r\nmy %dum = ();\r\nfor(my $i=0;$i<$n;$i++){\r\n $dum{&root($i)} = 1;\r\n}\r\nmy @d2 = keys %dum;\r\nmy $res = 1 + $#d2;\r\nprint \"$res\\n\";\r\n\r\nexit(0);\r\n\r\n\r\n###\r\n\r\n#!/usr/bin/perl\r\nuse Data::Dumper;\r\nmy ($n) = map {$_ - 0} split(/\\s+/o,);\r\n\r\nmy $f = &factr($n);\r\n\r\nmy @facts = keys %$f;\r\n\r\nmy %f0 = ();\r\n\r\nmy %oki = ();\r\n\r\nwhile(1){\r\n my $gosei = 1;\r\n foreach my $f1 (@facts){\r\n $gosei *= ( $f1 ** ( $f0{$f1} - 0 ) );\r\n }\r\n if( $gosei >= 3 ){\r\n# print \"gosei = $gosei\\n\";\r\n my $syo = $n / $gosei;\r\n if( $syo <= $gosei - 2 ){\r\n my $g1 = $gosei - 1;\r\n $oki{$g1} = 1;\r\n }\r\n }\r\n my $cu = 0;\r\n my $carry = 1;\r\n while($carry > 0){\r\n $f0{$facts[$cu]} += 1;\r\n $carry = 0;\r\n if( $f0{$facts[$cu]} > $f->{$facts[$cu]} ){\r\n $f0{$facts[$cu]} = 0;\r\n $carry = 1;\r\n $cu++;\r\n }\r\n last if( $cu > $#facts );\r\n }\r\n last if $cu > $#facts;\r\n}\r\nmy $res = 0;\r\nforeach my $o1 (keys %oki){\r\n $res += ( $o1 - 0);\r\n}\r\n#print &Dumper(\\%oki);\r\nprint \"$res\\n\";\r\nexit(0);\r\n\r\nsub factr {\r\n my $v = shift;\r\n my %f = ();\r\n if( $v<4 ){\r\n $f{$v}++;\r\n return \\%f;\r\n }\r\n for(my $i=1;$i*$i<=$v;$i+=2){\r\n my $ii = ($i == 1 ? 2 : $i);\r\n while($v % $ii == 0){\r\n $f{$ii}++;\r\n $v /= $ii;\r\n }\r\n }\r\n $f{$v}++ if $v>1;\r\n return \\%f;\r\n}\r\n\r\n#### mod_int + nCr nPr set\r\n\r\n#!/usr/bin/perl\r\n\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($n,$k) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\n&mk_fact($n+10);\r\n\r\n### write here \r\n\r\nexit(0);\r\n\r\n\r\nsub mod_pow { # ( x ** n ) % mod\r\n my ($x,$n) = @_;\r\n my $r = 1;\r\n while( $n ){\r\n $r = ( $r * $x ) % $mod if 1 & $n;\r\n $n >>= 1;\r\n $x = ( $x * $x ) % $mod;\r\n }\r\n return $r;\r\n}\r\n\r\nsub mk_fact { # make {global} n-size variable fact[] factinv[]\r\n my $n_max = shift;\r\n our @fact = (1); $#fact = $n_max;\r\n our @factinv = (1); $#factinv = $n_max;\r\n for(my $i=1;$i<=$n_max;$i++){\r\n $fact[$i] = ( $fact[$i-1] * $i ) % $mod;\r\n $factinv[$i] = &mod_pow($fact[$i],$mod-2);\r\n }\r\n}\r\n\r\nsub nCr { # calc nCr % mod\r\n my $n = shift; my $r = shift;\r\n return (( $fact[$n] * ( ( $factinv[$r] * $factinv[$n-$r] ) % $mod ) ) % $mod);\r\n}\r\n\r\nsub nPr { # calc nPr % mod\r\n my $n = shift; my $r = shift;\r\n return (( $fact[$n] * $factinv[$n-$r] ) % $mod );\r\n}\r\n\r\n#### union find set\r\n\r\n#!/usr/bin/perl\r\n\r\nmy ($n) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\n#### write here\r\n\r\nexit(0);\r\n\r\nsub mk_uf {\r\n my $n = shift;\r\n our @uf = (); $#uf = $n - 1;\r\n for(my $i=0;$i<$n;$i++){\r\n $uf[$i] = $i;\r\n }\r\n}\r\n\r\nsub root {\r\n my $x = shift;\r\n while( $uf[$x] != $x ){ $x = $uf[$x] = $uf[$uf[$x]]; }\r\n return $x;\r\n}\r\n\r\nsub unite {\r\n my $x = &root(scalar(shift)); my $y = &root(scalar(shift));\r\n $uf[$y] = $x;\r\n}\r\n\r\n\r\n\r\n"}], "negative_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t%h = %g = @D = ();\r\n\t@A = split ' ', <>;\r\n\t@B = split ' ', <>;\r\n\t@C = split ' ', <>;\r\n\t\r\n\t++ $h{ $_ } for @B;\r\n\t\r\n\tfor( 0 .. @A - 1 ){\r\n\t\t$A[ $_ ] != $B[ $_ ] and $h{ $B[ $_ ] }{ $_ + 1 } = 1;\r\n\t\t$g{ $B[ $_ ] } = $_ + 1\r\n\t\t}\r\n\t\r\n\tfor( @C ){\r\n\t\tif( exists $h{ $_ } ){\r\n\t\t\t( $key ) = keys %{ $h{ $_ } };\r\n\t\t\t$key and delete $h{ $_ }{ $key };\r\n\t\t\tpush @D, $key || $g{ $_ }\r\n\t\t\t}\r\n\t\telse{\r\n\t\t\tpush @D, 0\r\n\t\t\t}\r\n\t\t}\r\n\t\r\n\tif( ! $D[ @D - 1 ] or map { keys %{ $h{ $_ } } } keys %h ){\r\n\t\tprint 'NO'\r\n\t\t}\r\n\telse{\r\n\t\tmap { $_ ||= $D[ @D - 1 ] } @D;\r\n\t\tprint for 'YES', \"@D\"\r\n\t\t}\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\tmy @C = split ' ', <>;\n\t\n\t$debug and print \"\\@A:[@A]\";\n\t$debug and print \"\\@B:[@B]\";\n\t$debug and print \"\\@C:[@C]\";\n\t\n\tmy %h;\n\tmy %g;\n\t\n\tundef $h{ $_ } for @B;\n\t\n\tfor my $i ( 0 .. @A - 1 ){\n\t\t$A[ $i ] != $B[ $i ] and $h{ $B[ $i ] }{ $i + 1 } = 1;\n\t\t$g{ $B[ $i ] } = $i + 1;\n\t\t}\n\t\n\t$debug and print map \"keys %h:[$_]\", join \" \", sort keys %h;\n\t\n\tmy @D;\n\t\n\tfor( @C ){\n\t\tif( exists $h{ $_ } ){\n\t\t\tif( 0 < keys %{ $h{ $_ } } ){\n\t\t\t\tmy( $key ) = keys %{ $h{ $_ } };\n\t\t\t\tdelete $h{ $_ }{ $key };\n\t\t\t\tpush @D, $key;\n\t\t\t\t\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tpush @D, $g{ $_ };\n\t\t\t\t}\n\t\t\t}\n\t\telse{\n\t\t\tpush @D, -1;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"\\@D:[@D]\";\n\t\n\tif( $D[ @D - 1 ] == -1 ){\n\t\tprint 'NO';\n\t\t}\n\telse{\n\t\tmap { $_ == -1 and $_ = $D[ @D - 1 ] } @D;\n\t\tprint 'YES';\n\t\tprint \"@D\";\n\t\t}\n\t}"}], "src_uid": "a350430c707bb18a146df9f80e114f45"} {"nl": {"description": "You are given a permutation $$$p_1, p_2, \\dots, p_n$$$. Recall that sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once.Find three indices $$$i$$$, $$$j$$$ and $$$k$$$ such that: $$$1 \\le i < j < k \\le n$$$; $$$p_i < p_j$$$ and $$$p_j > p_k$$$. Or say that there are no such indices.", "input_spec": "The first line contains a single integer $$$T$$$ ($$$1 \\le T \\le 200$$$)\u00a0\u2014 the number of test cases. Next $$$2T$$$ lines contain test cases\u00a0\u2014 two lines per test case. The first line of each test case contains the single integer $$$n$$$ ($$$3 \\le n \\le 1000$$$)\u00a0\u2014 the length of the permutation $$$p$$$. The second line contains $$$n$$$ integers $$$p_1, p_2, \\dots, p_n$$$ ($$$1 \\le p_i \\le n$$$; $$$p_i \\neq p_j$$$ if $$$i \\neq j$$$)\u00a0\u2014 the permutation $$$p$$$.", "output_spec": "For each test case: if there are such indices $$$i$$$, $$$j$$$ and $$$k$$$, print YES (case insensitive) and the indices themselves; if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them.", "sample_inputs": ["3\n4\n2 1 4 3\n6\n4 6 1 2 5 3\n5\n5 3 1 2 4"], "sample_outputs": ["YES\n2 3 4\nYES\n3 5 6\nNO"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @p = map { $_ - 0 } split(/\\s+/,);\n \n my $up = -1;\n my $do = -1;\n for(my $i=0;$i<$n-1;$i++){\n my $di = ( ( $p[$i+1] - $p[$i] ) <=> 0 );\n $up = $i if $di > 0;\n $do = $i if $up != -1 and $di < 0;\n last if $up != -1 and $do != -1;\n }\n \n if( $up == -1 or $do == -1 ){\n print \"NO\\n\";\n } else {\n print \"YES\\n\";\n \n my @aa = sort { $p[$b] <=> $p[$a] } ($up + 1 , $do);\n printf(\"%d %d %d\\n\",1+$up,1+$aa[0],$do+2);\n \n }\n \n}\n\nexit(0);\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @p = map { $_ - 0 } split(/\\s+/,);\n \n my @sa = sort { $b <=> $a } @p;\n \n if( $p[0] == $sa[0] or $p[$n-1] == $sa[0] ){\n print \"NO\\n\";\n } else {\n my $j = 0;\n for($j=0;$j<$n;$j++){\n last if $p[$j] == $sa[0];\n }\n my $i = 1;\n my $k = $n;\n $j++;\n print \"YES\\n\";\n print \"$i $j $k\\n\";\n \n }\n \n}\n\n\n"}, {"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @p = map { $_ - 0 } split(/\\s+/,);\n \n my @sa = sort { $b <=> $a } @p;\n \n if( $p[0] == $sa[0] or $p[$n-1] == $sa[0] ){\n print \"NO\\n\";\n } else {\n my $j = 0;\n for($j=0;$j<$n;$j++){\n last if $p[$j] == $sa[0];\n }\n my $i = 1;\n my $k = $n;\n $j++;\n print \"YES\\n\";\n print \"$i $j $k\\n\";\n \n }\n \n}\n\nexit(0);\n\n"}, {"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @p = map { $_ - 0 } split(/\\s+/,);\n \n my @sa = sort { $b <=> $a } @p;\n \n if( $p[0] == $sa[0] or $p[$n-1] == $sa[0] ){\n print \"NO\\n\";\n } else {\n my $j = 0;\n for($j=0;$j<$n;$j++){\n last if $p[$j] == $sa[0];\n }\n my $i = 1;\n my $k = $n;\n $j++;\n print \"YES\\n\";\n print \"$i $j $k\\n\";\n \n }\n \n}\n\n"}], "src_uid": "dd55e29ac9756325530ad2f4479d9f6d"} {"nl": {"description": "Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a\u2009+\u2009ar\u2009=\u2009n or determine that such a doesn't exist.", "input_spec": "The first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u200910100\u2009000).", "output_spec": "If there is no such positive integer a without leading zeroes that a\u2009+\u2009ar\u2009=\u2009n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.", "sample_inputs": ["4", "11", "5", "33"], "sample_outputs": ["2", "10", "0", "21"], "notes": "NoteIn the first sample 4\u2009=\u20092\u2009+\u20092, a\u2009=\u20092 is the only possibility.In the second sample 11\u2009=\u200910\u2009+\u20091, a\u2009=\u200910\u00a0\u2014 the only valid solution. Note, that a\u2009=\u200901 is incorrect, because a can't have leading zeroes.It's easy to check that there is no suitable a in the third sample.In the fourth sample 33\u2009=\u200930\u2009+\u20093\u2009=\u200912\u2009+\u200921, so there are three possibilities for a: a\u2009=\u200930, a\u2009=\u200912, a\u2009=\u200921. Any of these is considered to be correct answer."}, "positive_code": [{"source_code": "use strict;\n\nour ($s, $n, @c);\n\n$s = ; chomp $s; $n = length $s; load_table();\nmy $ans; ($ans = solve(0)) || ($ans = solve(1)) || ($ans = 0);\nprint $ans;\n\nsub load_table {\n\tmy @a = ; shift @a;\n\tfor (@a) {\n\t\tmy @b = split; shift @b;\n\t\tpush @c, [@b];\n\t}\n}\n\nsub at { substr($s, $_[0], 1) }\n\nsub solve {\n\tmy $i = shift;\n\tmy $j = $n - 1;\n\tmy $A = ($i == 0? 0: 1);\n\tmy ($B, @d1, @d2, $a, $b, $v, $f);\n\n\t$A == 1 && at(0) > 1 && return;\n\n\twhile ($i < $j) {\n\t\t$a = at($i) + 10 * $A; $b = at($j) - $B;\n\t\t$v = $c[$a][$b];\n\t\t$v eq \"-\" && return;\n\t\t($a ,$f, $b) = split //, $v;\n\t\tpush @d1, $a;\n\t\tpush @d2, $b;\n\t\t$A = ($f eq \"+\");\n\t\t$B = ($a + $b + $B >= 10);\n\t\t$i++, $j--;\n\t}\n\n\t$i == $j && do {\n\t\t$a = at($i) + 10 * $A - $B;\n\t\t$a & 1 && return;\n\t\tpush @d1, $a/2;\n\t\t$A = $B = 0;\n\t};\n\n\t$d1[0] == 0 && return;\n\t$A - $B && return;\n\n\tjoin \"\", @d1, reverse(@d2);\n}\n\n__DATA__\n\t\t0\t\t1\t\t2\t\t3\t\t4\t\t5\t\t6\t\t7\t\t8\t\t9\n0\t\t0-0\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n1\t\t0+0\t\t1-0\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n2\t\t-\t\t1+0\t\t2-0\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n3\t\t-\t\t-\t\t2+0\t\t3-0\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n4\t\t-\t\t-\t\t-\t\t3+0\t\t4-0\t\t-\t\t-\t\t-\t\t-\t\t-\n5\t\t-\t\t-\t\t-\t\t-\t\t4+0\t\t5-0\t\t-\t\t-\t\t-\t\t-\n6\t\t-\t\t-\t\t-\t\t-\t\t-\t\t5+0\t\t6-0\t\t-\t\t-\t\t-\n7\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t6+0\t\t7-0\t\t-\t\t-\n8\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t7+0\t\t8-0\t\t-\n9\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t8+0\t\t9-0\n10\t\t9-1\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+0\n11\t\t9+1\t\t9-2\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n12\t\t-\t\t9+2\t\t9-3\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n13\t\t-\t\t-\t\t9+3\t\t9-4\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n14\t\t-\t\t-\t\t-\t\t9+4\t\t9-5\t\t-\t\t-\t\t-\t\t-\t\t-\n15\t\t-\t\t-\t\t-\t\t-\t\t9+5\t\t9-6\t\t-\t\t-\t\t-\t\t-\n16\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+6\t\t9-7\t\t-\t\t-\t\t-\n17\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+7\t\t9-8\t\t-\t\t-\n18\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+8\t\t9-9\t\t-\n19\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+9\t\t-\t\t\n"}], "negative_code": [{"source_code": "use strict;\n\nour ($s, $n, @c);\n\n$s = ; chomp $s; $n = length $s; load_table();\nmy $ans; ($ans = solve(0)) || ($ans = solve(1)) || ($ans = 0);\nprint $ans;\n\nsub load_table {\n\tmy @a = ; shift @a;\n\tfor (@a) {\n\t\tmy @b = split; shift @b;\n\t\tpush @c, [@b];\n\t}\n}\n\nsub at { substr($s, $_[0], 1) }\n\nsub solve {\n\tmy $i = shift;\n\tmy $j = $n - 1;\n\tmy $A = ($i == 0? 0: 1);\n\tmy ($B, @d1, @d2, $a, $b, $v, $f);\n\n\t$A == 1 && at(0) > 1 && return;\n\n\twhile ($i < $j) {\n\t\t$a = at($i) + 10 * $A; $b = at($j) - $B;\n\t\t$v = $c[$a][$b];\n\t\t$v eq \"-\" && return;\n\t\t($a ,$f, $b) = split //, $v;\n\t\tpush @d1, $a;\n\t\tpush @d2, $b;\n\t\t$A = ($f eq \"+\");\n\t\t$B = ($a + $b >= 10);\n\t\t$i++, $j--;\n\t}\n\n\t$i == $j && do {\n\t\t$a = at($i) + 10 * $A - $B;\n\t\t$a & 1 && return;\n\t\tpush @d1, $a/2;\n\t};\n\n\t$d1[0] == 0 && return;\n\t$B == 1 && return;\n\n\tjoin \"\", @d1, reverse(@d2);\n}\n\n__DATA__\n\t\t0\t\t1\t\t2\t\t3\t\t4\t\t5\t\t6\t\t7\t\t8\t\t9\n0\t\t0-0\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n1\t\t0+0\t\t1-0\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n2\t\t-\t\t1+0\t\t2-0\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n3\t\t-\t\t-\t\t2+0\t\t3-0\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n4\t\t-\t\t-\t\t-\t\t3+0\t\t4-0\t\t-\t\t-\t\t-\t\t-\t\t-\n5\t\t-\t\t-\t\t-\t\t-\t\t4+0\t\t5-0\t\t-\t\t-\t\t-\t\t-\n6\t\t-\t\t-\t\t-\t\t-\t\t-\t\t5+0\t\t6-0\t\t-\t\t-\t\t-\n7\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t6+0\t\t7-0\t\t-\t\t-\n8\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t7+0\t\t8-0\t\t-\n9\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t8+0\t\t9-0\n10\t\t9-1\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+0\n11\t\t9+1\t\t9-2\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n12\t\t-\t\t9+2\t\t9-3\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n13\t\t-\t\t-\t\t9+3\t\t9-4\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n14\t\t-\t\t-\t\t-\t\t9+4\t\t9-5\t\t-\t\t-\t\t-\t\t-\t\t-\n15\t\t-\t\t-\t\t-\t\t-\t\t9+5\t\t9-6\t\t-\t\t-\t\t-\t\t-\n16\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+6\t\t9-7\t\t-\t\t-\t\t-\n17\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+7\t\t9-8\t\t-\t\t-\n18\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+8\t\t9-9\t\t-\n19\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+9\t\t-\t\t\n"}, {"source_code": "# a b c . . d e f\n# f e d . . c b a\n# A B C . . D E F\n# A F c=1: 1F, a=9 f=1F-9, carry=A==F\n# c=0: F, a=1 f=F-1, carry=A==F\n# B E c=1: E-1, 1E, b=9 e=1E-9, carry=B==E\n# c=0: E, b=1 e=E-1, carry=B==E\n# X Y f=0,c=1: Y-1, X==Y | 0, x=X y=0\n# c=0: X==Y | 0, x=X y=0\n# f=1,c=1: 1XY=10*1y+1y=11*1y, 1y=1XY/11 | 0, x=9 y=1y-9\n# X f=0,c=1: x=(1X-1)/2 | 0\n# f=1,c=1: x=1X/2 | 0\n# c=0: x=X/2 | 0\n\nsub calc($$) {\n\t$n = shift;\n\t$nl0 = shift;\n\t$nl = length $n;\n\tif ($nl == 1) {\n\t\treturn $n % 2 == 0? $n / 2: 0;\n\t}\n\t$carry = $nl > $nl0;\n\tif ($carry) {\n\t\tsubstr($n, 0, 1) eq '1' || return 0;\n\t}\n\t$il0 = $carry? 1: 0;\n\t$ir0 = $nl - 1;\n\t$first = 1;\n\tfor ($il = $il0, $ir = $ir0; $il < $ir - 1; $il++, $ir--) {\n\t\t$l = substr $n, $il, 1;\n\t\t$r = substr $n, $ir, 1;\n\t\tif ($carry) {\n\t\t\t$r-- unless $first;\n\t\t\t$l1 = 9; $r1 = $r - 9 + 10;\n\t\t} else {\n\t\t\tif ($r > 0) {\n\t\t\t\t$l1 = 1; $r1 = $r - 1;\n\t\t\t} else {\n\t\t\t\t$l1 = $r1 = 0;\n\t\t\t}\n\t\t}\n\t\tif ($l != $r) {\n\t\t\t$l - 1 == $r || return 0;\n\t\t\t$carry = 1;\n\t\t} else {\n\t\t\t$carry = 0;\n\t\t}\n\t\tsubstr $n, $il, 1, $l1;\n\t\tsubstr $n, $ir, 1, $r1;\n\t\t$first = 0;\n\t}\n\tif ($il < $ir) {\n\t\t$l = substr $n, $il, 1;\n\t\t$r = substr $n, $ir, 1;\n\t\tif ($carry) {\n\t\t\t$d3 = 100 + 10 * $l + $r;\n\t\t\t$d3 -= 1 unless $first;\n\t\t\t$d3 % 11 == 0 || return 0;\n\t\t\t$d2 = $d3 / 11;\n\t\t\t$l1 = 9; $r1 = $d2 - 9;\n\t\t} else {\n\t\t\t$l == $r || return 0;\n\t\t\t$l1 = $l; $r1 = 0;\n\t\t}\n\t\tsubstr $n, $il, 1, $l1; \n\t\tsubstr $n, $ir, 1, $r1;\n\t} \n\tif ($il == $ir) {\n\t\t$l = substr $n, $il, 1;\n\t\tif ($carry) {\n\t\t\tif ($first) {\n\t\t\t\t$l += 10;\n\t\t\t} elsif ($r > 0) {\n\t\t\t\t$l = $l - 1 + 10;\n\t\t\t} else {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\t\t$l % 2 == 0 || return 0;\n\t\tsubstr $n, $il, 1, $l / 2;\n\t}\n\tsubstr $n, $il0;\n}\n\nsub calc2 {\n\tmy $n = shift;\n\tmy $r = calc($n, length($n));\n\tif ($r == 0) {\n\t\t$r = calc($n, length($n) - 1);\n\t}\n\t$r;\n}\n\nexists $INC{q(Test/More.pm)} || do {\n\t$input = <>;\n\tchomp $input;\n\tprint calc2($input), \"\\n\";\n};\n\n1;\n"}, {"source_code": "use strict;\n\nour ($s, $n, @c);\n\n$s = ; chomp $s; $n = length $s; load_table();\nmy $ans; ($ans = solve(0)) || ($ans = solve(1)) || ($ans = 0);\nprint $ans;\n\nsub load_table {\n\tmy @a = ; shift @a;\n\tfor (@a) {\n\t\tmy @b = split; shift @b;\n\t\tpush @c, [@b];\n\t}\n}\n\nsub at { substr($s, $_[0], 1) }\n\nsub solve {\n\tmy $i = shift;\n\tmy $j = $n - 1;\n\tmy $A = ($i == 0? 0: 1);\n\tmy ($B, @d1, @d2, $a, $b, $v, $f);\n\n\t$A == 1 && at(0) > 1 && return;\n\n\twhile ($i < $j) {\n\t\t$a = at($i) + 10 * $A; $b = at($j) - $B;\n\t\t$v = $c[$a][$b];\n\t\t$v eq \"-\" && return;\n\t\t($a ,$f, $b) = split //, $v;\n\t\tpush @d1, $a;\n\t\tpush @d2, $b;\n\t\t$A = ($f eq \"+\");\n\t\t$B = ($a + $b >= 10);\n\t\t$i++, $j--;\n\t}\n\n\t$i == $j && do {\n\t\t$a = at($i) + 10 * $A - $B;\n\t\t$a & 1 && return;\n\t\tpush @d1, $a/2;\n\t\t$A = $B = 0;\n\t};\n\n\t$d1[0] == 0 && return;\n\t$A - $B && return;\n\n\tjoin \"\", @d1, reverse(@d2);\n}\n\n__DATA__\n\t\t0\t\t1\t\t2\t\t3\t\t4\t\t5\t\t6\t\t7\t\t8\t\t9\n0\t\t0-0\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n1\t\t0+0\t\t1-0\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n2\t\t-\t\t1+0\t\t2-0\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n3\t\t-\t\t-\t\t2+0\t\t3-0\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n4\t\t-\t\t-\t\t-\t\t3+0\t\t4-0\t\t-\t\t-\t\t-\t\t-\t\t-\n5\t\t-\t\t-\t\t-\t\t-\t\t4+0\t\t5-0\t\t-\t\t-\t\t-\t\t-\n6\t\t-\t\t-\t\t-\t\t-\t\t-\t\t5+0\t\t6-0\t\t-\t\t-\t\t-\n7\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t6+0\t\t7-0\t\t-\t\t-\n8\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t7+0\t\t8-0\t\t-\n9\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t8+0\t\t9-0\n10\t\t9-1\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+0\n11\t\t9+1\t\t9-2\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n12\t\t-\t\t9+2\t\t9-3\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n13\t\t-\t\t-\t\t9+3\t\t9-4\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\n14\t\t-\t\t-\t\t-\t\t9+4\t\t9-5\t\t-\t\t-\t\t-\t\t-\t\t-\n15\t\t-\t\t-\t\t-\t\t-\t\t9+5\t\t9-6\t\t-\t\t-\t\t-\t\t-\n16\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+6\t\t9-7\t\t-\t\t-\t\t-\n17\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+7\t\t9-8\t\t-\t\t-\n18\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+8\t\t9-9\t\t-\n19\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t-\t\t9+9\t\t-\t\t\n"}, {"source_code": "# a b c . . d e f\n# f e d . . c b a\n# A B C . . D E F \n# A F cio=0/1: 1F, a=9 f=1F-9, cio=1/A>F\n# cio=0/0: F, a=F f=0, cio=0/A>F A=F>0\n# B E cio=0/0: E, b=E e=0, cio=0/B>E B=E|B-1=E|0\n# cio=0/1: 1E, b=9 e=1E-9, cio=1/B>E\n# cio=1/0: E, b=E-1 e=0, cio=0/B>E-1 E>0\n# > co=0->max E=e+b+1=9\n# cio=1/1: 1E, b=9 e=1E-10, cio=1/B>E-1 E>0 B=0?\n# b=9 e=0, cio=1/? E=0\n# > co=1->e+b=18, E=e+b+1<>0\n# > co=1->e+b=9, E=e+b+1=0\n# X Y f=0,c=1: Y-1, X==Y | 0, x=X y=0\n# c=0: X==Y | 0, x=X y=0\n# f=1,c=1: 1XY=10*1y+1y=11*1y, 1y=1XY/11 | 0, x=9 y=1y-9\n# X \n# > 909 1818 ci/co=1/0, ci=1->X-1, co=0-> X, X= 0, x=0\n# > 959 1918 ci/co=1/1, ci=1->X-1, \u0441o=1->1X, X=10, x=5\n# f=1,c=1: x=1X/2 | 0\n# c=0: x=X/2 | 0\n\nuse strict;\n\nsub next_cio($$$$) {\n\tmy ($l, $r, $ci, $co) = @_;\n\tmy ($ci1, $co1) = ($co, undef);\n\tif ($ci) {\n\t\tif ($r == 0) {\n\t\t\t$ci1 = 1;\n\t\t\tif ($l == 9) {\n\t\t\t\t$co1 = 0;\n\t\t\t} elsif ($l == 0) {\n\t\t\t\t$co1 = 1;\n\t\t\t} else {\n\t\t\t\t$co1 = -1;\n\t\t\t}\n\t\t} else {\n\t\t\t# r > 0\n\t\t\t$r--;\n\t\t\tif ($l == $r) {\n\t\t\t\t$co1 = 0;\n\t\t\t} elsif ($l - 1 == $r) {\n\t\t\t\t$co1 = 1;\n\t\t\t} else {\n\t\t\t\t$co1 = -1;\n\t\t\t}\n\t\t}\n\t} else {\n\t\t# ci = 0\n\t\tif ($l == $r) {\n\t\t\t$co1 = 0;\n\t\t} elsif ($l - 1 == $r) {\n\t\t\t$co1 = 1;\n\t\t} else {\n\t\t\t$co1 = -1;\n\t\t}\n\t}\n\t($ci1, $co1);\n}\n\nsub calc($$) {\n\tmy ($n, $nl0) = @_;\n\tmy ($ci, $co, $nl, $il0, $il, $ir0, $ir, $l, $r, $l1, $r1);\n\n\t$nl = length $n;\n\tif ($nl == 1) {\n\t\treturn $n % 2 == 0? $n / 2: 0;\n\t}\n\t$ci = 0;\n\t$co = $nl > $nl0;\n\tif ($co) {\n\t\tsubstr($n, 0, 1) eq '1' || return 0;\n\t}\n\t$il0 = $co? 1: 0;\n\t$ir0 = $nl - 1;\n\n\tfor ($il = $il0, $ir = $ir0; $il < $ir - 1; $il++, $ir--) {\n\t\t$l = substr $n, $il, 1;\n\t\t$r = substr $n, $ir, 1;\n\t\t$ci == 0 and $co == 0 and do {\n\t\t\t$l1 = $l; $r1 = 0; \n\t\t};\n\t\t$ci == 0 and $co == 1 and do { \n\t\t\t$l1 = 9; $r1 = 10 + $r - 9;\n\t\t};\n\t\t$ci == 1 and $co == 0 and do { \n\t\t\t$l1 = $r > 0? $r - 1: 9; $r1 = 0; \n\t\t};\n\t\t$ci == 1 and $co == 1 and do { \n\t\t\t$l1 = 9; $r1 = $r;\n\t\t};\n\t\tsubstr $n, $il, 1, $l1;\n\t\tsubstr $n, $ir, 1, $r1;\n\t\t($ci, $co) = next_cio($l, $r, $ci, $co);\n\t\t$co == -1 && return 0;\n\t}\n\n\tif ($il < $ir) {\n\t\t$l = substr $n, $il, 1;\n\t\t$r = substr $n, $ir, 1;\n\t\tif ($co) {\n\t\t\tmy $d3 = 100 + 10 * $l + $r - $ci;\n\t\t\t$d3 % 11 == 0 || return 0;\n\t\t\tmy $d2 = $d3 / 11;\n\t\t\t$l1 = 9; $r1 = $d2 - 9;\n\t\t} else {\n\t\t\tmy $d2 = 10 * $l + $r - $ci;\n\t\t\t$d2 % 11 == 0 || return 0;\n\t\t\t$l1 = $d2 / 11; $r1 = 0;\n\t\t}\n\t\tsubstr $n, $il, 1, $l1; \n\t\tsubstr $n, $ir, 1, $r1;\n\t}\n\n\tif ($il == $ir) {\n\t\t$l = substr $n, $il, 1;\n\t\t$l += 10 * $co - $ci;\n\t\t$l % 2 == 0 || return 0;\n\t\tsubstr $n, $il, 1, $l / 2;\n\t}\n\n\tsubstr $n, $il0;\n}\n\nsub calc2 {\n\tmy $n = shift;\n\tmy $r = calc($n, length($n));\n\tif ($r == 0) {\n\t\t$r = calc($n, length($n) - 1);\n\t}\n\t$r;\n}\n\nexists $INC{q(Test/More.pm)} || do {\n\tmy $input = <>;\n\tchomp $input;\n\tprint calc2($input), \"\\n\";\n};\n\n1;\n"}, {"source_code": "# a b c . . d e f\n# f e d . . c b a\n# A B C . . D E F \n# A F cio=0/1: 1F, a=9 f=1F-9, cio=1/A>F\n# F, A=0 F=9, a=9 f=0, cio=0/1\n# cio=0/0: F, a=F f=0, cio=0/A>F A=F>0\n# B E cio=0/0: E, b=E e=0, cio=0/B>E B=E|B-1=E|0\n# cio=0/1: 1E, b=9 e=1E-9, cio=1/B>E\n# cio=1/0: E, b=E-1 e=0, cio=0/B>E-1 E>0\n# > co=0->max E=e+b+1=9\n# cio=1/1: 1E, b=9 e=1E-10, cio=1/B>E-1 E>0 B=0?\n# b=9 e=0, cio=1/? E=0\n# > co=1->e+b=18, E=e+b+1<>0\n# > co=1->e+b=9, E=e+b+1=0\n# X Y f=0,c=1: Y-1, X==Y | 0, x=X y=0\n# c=0: X==Y | 0, x=X y=0\n# f=1,c=1: 1XY=10*1y+1y=11*1y, 1y=1XY/11 | 0, x=9 y=1y-9\n# X \n# > 909 1818 ci/co=1/0, ci=1->X-1, co=0-> X, X= 0, x=0\n# > 959 1918 ci/co=1/1, ci=1->X-1, \u0441o=1->1X, X=10, x=5\n# f=1,c=1: x=1X/2 | 0\n# c=0: x=X/2 | 0\n\nuse strict;\n\nsub next_cio($$$$) {\n\tmy ($l, $r, $ci, $co) = @_;\n\tmy ($ci1, $co1) = ($co, undef);\n\tif ($ci) {\n\t\tif ($r == 0) {\n\t\t\t$ci1 = 1;\n\t\t\tif ($l == 9) {\n\t\t\t\t$co1 = 0;\n\t\t\t} elsif ($l == 0) {\n\t\t\t\t$co1 = 1;\n\t\t\t} else {\n\t\t\t\t$co1 = -1;\n\t\t\t}\n\t\t} else {\n\t\t\t# r > 0\n\t\t\t$r--;\n\t\t\tif ($l == $r) {\n\t\t\t\t$co1 = 0;\n\t\t\t} elsif ($l - 1 == $r) {\n\t\t\t\t$co1 = 1;\n\t\t\t} else {\n\t\t\t\t$co1 = -1;\n\t\t\t}\n\t\t}\n\t} else {\n\t\t# ci = 0\n\t\tif ($l == $r) {\n\t\t\t$co1 = 0;\n\t\t} elsif ((10 + $l - 1) % 10 == $r) {\n\t\t\t$ci1 = 0 if $co && $l == 0 && $r == 9;\n\t\t\t$co1 = 1;\n\t\t} else {\n\t\t\t$co1 = -1;\n\t\t}\n\t}\n\t($ci1, $co1);\n}\n\nsub calc($$) {\n\tmy ($n, $nl0) = @_;\n\tmy ($ci, $co, $nl, $il0, $il, $ir0, $ir, $l, $r, $l1, $r1);\n\n\t$nl = length $n;\n\tif ($nl == 1) {\n\t\treturn $n % 2 == 0? $n / 2: 0;\n\t}\n\t$ci = 0;\n\t$co = $nl > $nl0;\n\tif ($co) {\n\t\tsubstr($n, 0, 1) eq '1' || return 0;\n\t}\n\t$il0 = $co? 1: 0;\n\t$ir0 = $nl - 1;\n\n\tfor ($il = $il0, $ir = $ir0; $il < $ir - 1; $il++, $ir--) {\n\t\t$l = substr $n, $il, 1;\n\t\t$r = substr $n, $ir, 1;\n\t\t$ci == 0 and $co == 0 and do {\n\t\t\t$l1 = $r; $r1 = 0; \n\t\t};\n\t\t$ci == 0 and $co == 1 and do { \n\t\t\t$l1 = 9; $r1 = (10 + $r - 9) % 10;\n\t\t};\n\t\t$ci == 1 and $co == 0 and do { \n\t\t\t$l1 = $r > 0? $r - 1: 9; $r1 = 0; \n\t\t};\n\t\t$ci == 1 and $co == 1 and do { \n\t\t\t$l1 = 9; $r1 = $r;\n\t\t};\n\t\t$il == $il0 && $l1 == 0 && return 0;\n\t\tsubstr $n, $il, 1, $l1;\n\t\tsubstr $n, $ir, 1, $r1;\n\t\t($ci, $co) = next_cio($l, $r, $ci, $co);\n\t\t$co == -1 && return 0;\n\t}\n\n\tif ($il < $ir) {\n\t\t$l = substr $n, $il, 1;\n\t\t$r = substr $n, $ir, 1;\n\t\tif ($co) {\n\t\t\tmy $d3 = 100 + 10 * $l + $r - $ci;\n\t\t\t$d3 % 11 == 0 || return 0;\n\t\t\tmy $d2 = $d3 / 11;\n\t\t\t$l1 = 9; $r1 = $d2 - 9;\n\t\t} else {\n\t\t\tmy $d2 = 10 * $l + $r - $ci;\n\t\t\t$d2 % 11 == 0 || return 0;\n\t\t\t$l1 = $d2 / 11; $r1 = 0;\n\t\t}\n\t\tsubstr $n, $il, 1, $l1; \n\t\tsubstr $n, $ir, 1, $r1;\n\t}\n\n\tif ($il == $ir) {\n\t\t$l = substr $n, $il, 1;\n\t\t$l += 10 * $co - $ci;\n\t\t$l % 2 == 0 || return 0;\n\t\tsubstr $n, $il, 1, $l / 2;\n\t}\n\n\tsubstr $n, $il0;\n}\n\nsub calc2 {\n\tmy $n = shift;\n\tmy $r = calc($n, length($n));\n\tif ($r == 0) {\n\t\t$r = calc($n, length($n) - 1);\n\t}\n\t$r;\n}\n\nexists $INC{q(Test/More.pm)} || do {\n\tmy $input = <>;\n\tchomp $input;\n\tprint calc2($input), \"\\n\";\n};\n\n1;\n"}], "src_uid": "fc892e4aac2d60e53f377a582f5ba7d3"} {"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\u2009<\u2009ai.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\u2009\u2264\u2009n\u2009\u2264\u2009105)\u00a0\u2014 the number of cards Conan has. The next line contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009105), 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": "\n\n\n\nmy %hash = ();\nmy $n = ;\nchomp ( $n );\nmy $f = ;\nchomp ( $f );\nmy @arr = split m/ /, $f;\nfor my $i (@arr) {\n if ( not exists $hash { $i } ) {\n\t$hash { $i } = 0;\n }\n $hash { $i } += 1;\n}\n\nmy $winner = 1;\nfor my $i ( keys %hash ) {\n if ( $hash { $i } % 2 == 1 ) {\n\t$winner = 0;\n }\n}\nif ( $winner == 0 ) {\n print \"Conan\\n\";\n}\nelse {\n print \"Agasa\\n\";\n}\n\n\n\n\n\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, split ' ', <>;\n\t\n\tprint qw( Conan Agasa )[ ! grep $_ % 2, values %h ];\n\t}"}, {"source_code": "<>;\n\n$h{ $_ } ++ for split ' ', <>;\n\nprint qw( Conan Agasa )[ ! grep $_ % 2, values %h ]"}, {"source_code": "<>;\n\nprint qw( Conan Agasa )[ 0 + ( join ' ', sort split ' ', <> ) =~ /^( ?\\b(\\d+) \\2\\b)*+( \\b(\\d+) \\4\\b)*$/ ]"}], "negative_code": [{"source_code": "<>;\n\nprint qw( Conan Agasa )[ 0 + ( join ' ', sort split ' ', <> ) =~ /^( ?\\b(\\d+) \\2\\b)+$/ ]"}, {"source_code": "<>;\n\nprint qw( Conan Agasa )[ 0 + ( join ' ', sort split ' ', <> ) =~ /^( ?\\b(\\d+) \\2\\b){1,5e4}$/ ]"}, {"source_code": "<>;\n\nprint qw( Conan Agasa )[ 0 + ( join ' ', sort split ' ', <> ) =~ /^( ?\\b(\\d+) \\2\\b)*+( ?\\b(\\d+) \\2\\b)*$/ ]"}], "src_uid": "864593dc3911206b627dab711025e116"} {"nl": {"description": "There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $$$1$$$ to $$$n$$$, the junction $$$1$$$ is called the root.A subtree of a junction $$$v$$$ is a set of junctions $$$u$$$ such that the path from $$$u$$$ to the root must pass through $$$v$$$. Note that $$$v$$$ itself is included in a subtree of $$$v$$$.A leaf is such a junction that its subtree contains exactly one junction.The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $$$t$$$ that all light bulbs in the subtree of $$$t$$$ have different colors.Arkady is interested in the following question: for each $$$k$$$ from $$$1$$$ to $$$n$$$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $$$k$$$?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$)\u00a0\u2014 the number of junctions in the tree. The second line contains $$$n - 1$$$ integers $$$p_2$$$, $$$p_3$$$, ..., $$$p_n$$$ ($$$1 \\le p_i < i$$$), where $$$p_i$$$ means there is a branch between junctions $$$i$$$ and $$$p_i$$$. It is guaranteed that this set of branches forms a tree.", "output_spec": "Output $$$n$$$ integers. The $$$i$$$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $$$i$$$.", "sample_inputs": ["3\n1 1", "5\n1 1 3 3"], "sample_outputs": ["1 1 2", "1 1 1 2 3"], "notes": "NoteIn the first example for $$$k = 1$$$ and $$$k = 2$$$ we can use only one color: the junctions $$$2$$$ and $$$3$$$ will be happy. For $$$k = 3$$$ you have to put the bulbs of different colors to make all the junctions happy.In the second example for $$$k = 4$$$ you can, for example, put the bulbs of color $$$1$$$ in junctions $$$2$$$ and $$$4$$$, and a bulb of color $$$2$$$ into junction $$$5$$$. The happy junctions are the ones with indices $$$2$$$, $$$3$$$, $$$4$$$ and $$$5$$$ then."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tchomp;\n\tmy $n = $_;\n\t\n\tmy $i = 2;\n\t\n\tmy %h;\n\t\t\n\tmap {\n\t\t$h{ $i }{ $_ } = 1;\n\t\t$h{ $_ }{ $i } = 1;\n\t\t$i ++;\n\t\t} split ' ', <>;\n\t\t\n\tmy @try = ( 1 );\n\tmy @seq = @try;\n\t\t\n\twhile( @try ){\n\t\tmy $try = shift @try;\n\t\tmy @arr =\n\t\tmap {\n\t\t\t$h{ $_ }{ $try } = 0;\n\t\t\t$_\n\t\t\t} grep $h{ $try }{ $_ }, keys %{ $h{ $try } };\n\t\tunshift @seq, @arr;\n\t\tpush @try, @arr;\n\t\t}\n\t\n\tmy %nums;\n\t\t\n\twhile( @seq ){\n\t\tmy $leaflikes = shift @seq;\n\t\t\n\t\tmy @keys = grep { defined $nums{ $_ } } keys %{ $h{ $leaflikes } };\n\t\tmy $sum = 0;\n\t\tif( @keys ){\n\t\t\t$sum += $_ for map { $nums{ $_ } } @keys;\n\t\t\t}\n\t\t$nums{ $leaflikes } = $sum || 1;\n\t\t}\n\t\t\t\n\tprint join ' ', sort { $a <=> $b } grep $_, values %nums;\n\t}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tchomp;\n\tmy $n = $_;\n\t\n\tmy $i = 2;\n\t\n\tmy %h;\n\t\t\n\tmap {\n\t\t$h{ $i }{ $_ } = 1;\n\t\t$h{ $_ }{ $i } = 1;\n\t\t$i ++;\n\t\t} split ' ', <>;\n\t\t\n\tmy @try = ( 1 );\n\tmy @seq = ( [ @try ] );\n\t\n\twhile( @try ){\n\t\tmy @allkeys;\n\t\tfor my $try ( @try ){\n\t\t\tpush @allkeys,\n\t\t\tmap {\n\t\t\t\t$h{ $_ }{ $try } = 0;\n\t\t\t#\t$h{ $try }{ $_ } = 0;\n\t\t\t\t$_\n\t\t\t\t} grep $h{ $try }{ $_ }, keys %{ $h{ $try } };\n\t\t\t}\n\t\tpush @seq, [ @allkeys ];\n\t\t@try = @allkeys;\n\t\t}\n\t\n\tpop @seq;\n\t@seq = reverse @seq;\n\t\n\t$debug and print \" seq : => @{ $_ }\" for @seq;\n\t\t\n\tmy %nums;\n\t\n\tmap { $nums{ $_ } = 1 } @{ shift @seq };\n\t\t\n\twhile( @seq ){\n\t\t\t\t\n\t\tmy @leaflikes = @{ shift @seq };\n\t\t\t\t\t\t\n\t\tfor my $leaflikes ( @leaflikes ){\n\t\t\tmy @keys = keys %{ $h{ $leaflikes } };\n\t\t\t@keys = grep { defined $nums{ $_ } } @keys;\n\t\t\tmy $sum = 0;\n\t\t\tif( @keys ){\n\t\t\t\t$sum += eval join ' + ', map { $nums{ $_ } } @keys;\n\t\t\t\t}\n\t\t\t$sum ||= 1;\n\t\t\t$nums{ $leaflikes } = $sum;\n\t\t\t}\n\t\t\n\t\t}\n\t\t\t\n\tprint join ' ', sort { $a <=> $b } grep $_, values %nums;\n\t}"}], "negative_code": [], "src_uid": "291bfc61026dddf6c53f4cd9a8aa2baa"} {"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,\u2009s2,\u2009...,\u2009sk. 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,\u2009s2,\u2009...,\u2009sk, 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\u00b7n\u2009\u2264\u20095000).", "input_spec": "The first line contains two integers k and n (1\u2009\u2264\u2009k\u2009\u2264\u20092500,\u20092\u2009\u2264\u2009n\u2009\u2264\u20095000,\u2009k\u00a0\u00b7\u00a0n\u2009\u2264\u20095000) \u2014 the number of strings we obtained, and the length of each of these strings. Next k lines contain the strings s1,\u2009s2,\u2009...,\u2009sk, 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 \u2014 by swapping the second and the fourth, and s3 \u2014 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": "use strict;\n\nsub diag {\n\t# print @_;\n}\t\n\nsub counter {\n\tmy $a = shift;\n\tmy %c; $c{$_}++ for @$a;\n\t\\%c;\n}\n\nsub in_list {\n\tmy ($value, @list) = @_;\n\tfor (@list) {\n\t\treturn 1 if $_ == $value;\n\t}\n\treturn 0;\n}\n\nsub unique {\n\tmy %u; $u{$_} = 1 for @_;\n\tmy @rv; \n\tfor (@_) {\n\t\tif ($u{$_} > 0) {\n\t\t\tpush(@rv, $_);\n\t\t\t$u{$_} = 0;\n\t\t}\n\t}\n\t@rv;\n}\n\nsub swap {\n\tmy ($a, $i, $j) = @_;\n\tmy @b = @{ $a };\n\t@b[$i,$j] = @b[$j,$i];\n\t\\@b;\n}\n\npackage Set;\n\nsub new { bless {} }\n\nsub add {\n\tmy ($s, @list) = @_;\n\tfor (@list) {\n\t\t$s->{$_} = 1\n\t}\n}\n\nsub list {\n\tmy $s = shift;\n\tsort keys %$s;\n}\n\npackage main;\n\nsub diff {\n\tmy ($a, $b) = @_;\n\tmy @i;\n\tmy $ok = 1;\n\tfor (my $i = 0; $i < @$a; $i++) {\n\t\tif ($a->[$i] ne $b->[$i]) {\n\t\t\tpush @i, $i;\n\t\t}\n\t}\n\tif (join(\"\", sort @$a[@i]) ne join(\"\", sort @$b[@i])) {\n\t\t$ok = 0;\n\t}\n\tdiag \"diff a=[@$a] b=[@$b] i=[@i] ok=$ok\\n\";\n\t($ok, @i);\n}\n\nsub test($$$) {\n\tmy ($a, $repeat, $t) = @_;\n\tdiag \"test [@$t]\\n\";\n\tmy $y = 1;\n\tfor my $s (@$a) {\n\t\tmy ($ok, @i) = diff($s, $t);\n\t\tif ($ok) {\n\t\t\tmy $d = scalar @i;\n\t\t\t$y *= ($d==2 or ($d==0 and $repeat));\n\t\t} else {\n\t\t\t$y = 0;\n\t\t\tlast;\n\t\t}\n\t}\n\tdiag \"y=$y\\n\";\n\t$y;\n}\n\nsub get_repeat {\n\tmy $a = shift;\n\tmy $count = grep {$_ > 1} values %{counter $a->[0]};\n\tdiag \"repeat=$count\\n\";\n\t$count > 0;\n}\n\nsub input {\n\t$/ = \"\"; my ($k, $n, @s) = split \" \", <>;\n\tdiag \"input k=$k\\n\";\n\t@s = map { [split //] } unique @s[0..$k-1];\n\tdiag \"unique $#s+1\\n\";\n\t@s;\n}\t\n\nsub bet {\n\tmy $a = shift;\n\tmy $s = Set->new();\n\tfor (my $i = 1; $i <= 4; $i++) {\n\t\tif (@$a > $i) {\n\t\t\tmy ($ok, @i) = diff($a->[0], $a->[$i]);\n\t\t\tif ($ok) {\n\t\t\t\t$s->add(@i);\n\t\t\t}\n\t\t}\n\t}\n\tmy @i = $s->list();\n\t@i = splice @i, 0, 10;\n\tdiag \"bet i=[@i] count=$#i+1\\n\";\n\t@i;\n}\t\n\nsub loop {\n\tmy ($a, @i) = @_;\n\tmy $found;\n\tmy $repeat = get_repeat($a);\n\t\n\tif (@$a == 1) {\n\t\t$found = swap($a->[0], 0, 1);\n\t} else {\n\t\tmy @t;\n\t\tfor (my $i = 0; $i < @i; $i++) {\n\t\t\tfor (my $j = $i + 1; $j < @i; $j++) {\n\t\t\t\tmy $t = swap $a->[0], @i[$i,$j];\n\t\t\t\tpush @t, $t;\n\t\t\t}\n\t\t}\n\t\tif ($repeat) {\n\t\t\tunshift @t, $a->[0];\n\t\t}\n\t\tdiag \"plan $#t+1 tests\\n\";\n\t\tfor my $t (@t) {\n\t\t\tif (test($a, $repeat, $t)) {\n\t\t\t\t$found = $t;\n\t\t\t\tlast;\n\t\t\t}\n\t\t}\n\t}\t\n\t$found;\n}\n\nsub main {\t\n\tmy @a = input();\n\tmy @i = bet(\\@a);\n\tmy $found = loop(\\@a, @i);\n\tmy $ans = ($found? join(\"\", @$found): -1);\n\tprint $ans, \"\\n\";\n}\n\nmain();\t\n"}, {"source_code": "# input\n$/ = \"\"; $_ = <>; ($k, $n, @s) = split;\n($a, @b) = grep !$u{$_}++, @s; \ngoto ans if grep {cn($a) ne cn($_)} @b;\n$ans = make($a,0,1), goto ans unless @b;\n$aa = $a =~ /(.).*\\1/;\n\n# bet\nfor $b (@b) {\n\tlen($d) < 4 or last;\n\t$d |= $a ^ $b;\n}\n@d = split //, $d;\nfor $i (0..$#d) {\n\tif (ord $d[$i] and @i < 10) {\n\t\tpush @i, $i;\n\t}\n}\n\n# loop\nfor $i (@i) {\nfor $j (@i) {\n\t$y = 1;\n\t$t = make($a, $i, $j);\n\tdiag(\"i=$i j=$j t=$t\\n\");\n\tfor $b ($a, @b) {\n\t\t$d = len($t ^ $b);\n\t\t$y *= ($d==2 or ($d==0 and $aa));\n\t\tdiag(\"\\tb=$b d=$d y=$y\\n\");\n\t}\n\tif ($y) {\n\t\t$ans = $t, goto ans;\n\t}\n}}\nans: print $ans || -1;\n\nsub cn {\n\tjoin \"\", sort split //, shift;\n}\n\nsub make {\n\tmy ($s, $i, $j) = @_;\n\tvec($s,$i,8)^=vec($s,$j,8)^=vec($s,$i,8)^=vec($s,$j,8) if $i-$j;\n\t$s;\n}\n\nsub len {\n\tlength $_[0] =~ s/\\0//gr;\n}\n\nsub diag {\n\t# print @_;\n}\t\n"}], "negative_code": [{"source_code": "$/ = \"\"; $_ = <>; ($k, $n, @s) = split;\n($a, @b) = grep !$u{$_}++, @s;\ngoto ans if grep {cn($a) ne cn($_)} @b;\n$ans = make($a,0,1), goto ans unless @b;\n$aa = $a =~ /(.).*\\1/;\n\n# bet\n$d |= $a ^ $_ for @b;\n@d = split //, $d;\nfor $i (0..$#d) {\n\tif (ord $d[$i] and @i<10) {\n\t\tpush @i, $i;\n\t}\n}\n\n# loop\nfor $i (@i) {\nfor $j (@i) {\n\t$y = 1;\n\t$t = make($a, $i, $j);\n\tfor $b (@b) {\n\t\t$d = grep {ord} split //, $t ^ $b;\n\t\t$y *= $d==2 || ($d==0 and $aa);\n\t}\n\tif ($y) {\n\t\t$ans = $t, goto ans;\n\t}\n}}\nans: print $ans || -1;\n\nsub cn {\n\tjoin \"\", sort split //, shift;\n}\n\nsub make {\n\tmy ($s, $i, $j) = @_;\n\tvec($s,$i,8)^=vec($s,$j,8)^=vec($s,$i,8)^=vec($s,$j,8) if $i-$j;\n\t$s;\n}"}, {"source_code": "sub diag {\n\t# print @_;\n}\t\n\nsub counter {\n\tmy $a = shift;\n\tmy %c; $c{$_}++ for @$a;\n\t\\%c;\n}\n\nsub in_list {\n\tmy ($value, @list) = @_;\n\tfor (@list) {\n\t\treturn 1 if $_ == $value;\n\t}\n\treturn 0;\n}\n\nsub unique {\n\tmy %u; $u{$_} = 1 for @_;\n\tmy @rv; \n\tfor (@_) {\n\t\tif ($u{$_} > 0) {\n\t\t\tpush(@rv, $_);\n\t\t\t$u{$_} = 0;\n\t\t}\n\t}\n\t@rv;\n}\n\nsub swap {\n\tmy ($a, $i, $j) = @_;\n\tmy @b = @{ $a };\n\t@b[$i,$j] = @b[$j,$i];\n\t\\@b;\n}\n\nsub diff {\n\tmy ($a, $b) = @_;\n\tmy @i;\n\tmy $ok = 1;\n\tfor $i (0..$n-1) {\n\t\tif ($a->[$i] ne $b->[$i]) {\n\t\t\tpush @i, $i;\n\t\t}\n\t}\n\tif (join(\"\", sort @$a[@i]) ne join(\"\", sort @$b[@i])) {\n\t\t$ok = 0;\n\t}\n\tdiag \"diff a=[@$a] b=[@$b] i=[@i] ok=$ok\\n\";\n\t($ok, @i);\n}\n\nsub test {\n\tmy $t = shift;\n\tdiag \"test [@$t]\\n\";\n\tmy $y = 1;\n\tfor $s (@s) {\n\t\tmy ($ok, @i) = diff($s, $t);\n\t\tif ($ok) {\n\t\t\tmy $d = scalar @i;\n\t\t\t$y *= ($d==2 or ($d==0 and $repeat));\n\t\t} else {\n\t\t\t$y = 0;\n\t\t\tlast;\n\t\t}\n\t}\n\tdiag \"y=$y\\n\";\n\t$y;\n}\n\nsub set_new {\n\tmy %s; \\%s;\n}\n\nsub set_add {\n\tmy ($s, @list) = @_;\n\tfor (@list) {\n\t\t$s->{$_} = 1\n\t}\n}\n\nsub set_size {\n\tmy $s = shift;\n\tscalar keys %$s;\n}\n\nsub set_list {\n\tmy $s = shift;\n\tsort keys %$s;\n}\n\n$/ = \"\"; ($k, $n, @s) = split \" \", <>;\ndiag \"input k=$k\\n\";\n@s = map { [split //] } unique @s[0..$k-1];\ndiag \"unique $#s+1\\n\";\n$repeat += $_ - 1 for values %{counter $s[0]};\ndiag \"repeat=$repeat\\n\";\n\n$s = set_new();\nfor ($i = 1; $i <= 4; $i++) {\n\tif (@s > $i) {\n\t\tmy ($ok, @i) = diff($s[0], $s[$i]);\n\t\tif ($ok) {\n\t\t\tset_add $s, @i;\n\t\t} else {\n\t\t\tgoto ans;\n\t\t}\n\t}\n}\n@i = set_list($s); @i = @i[0..3] if @i > 4;\n$count = scalar @i;\ndiag \"i=[@i] count=$count\\n\";\n\t\nif (@s == 1) {\n\t$ans = swap($s[0], 0, 1);\n} \nelsif (in_list $count, 2, 3, 4) {\n\tmy @t;\n\tfor ($i = 0; $i < @i; $i++) {\n\t\tfor ($j = $i + 1; $j < @i; $j++) {\n\t\t\t$t = swap $s[0], @i[$i,$j];\n\t\t\tpush @t, $t;\n\t\t}\n\t}\n\tif ($repeat) {\n\t\tunshift @t, $s[0];\n\t}\n\tdiag \"plan $#t+1 tests\\n\";\n\tfor $t (@t) {\n\t\tif (test($t)) {\n\t\t\t$ans = $t;\n\t\t\tlast;\n\t\t}\n\t}\n}\t\n\nans:\n$ans = join(\"\", @$ans) || -1;\nprint $ans;"}, {"source_code": "$diag = \"\";\n\nsub diag {\n\t# print @_;\n}\t\n\nsub counter {\n\tmy $a = shift;\n\tmy %c; $c{$_}++ for @$a;\n\t\\%c;\n}\n\nsub in_list {\n\tmy ($value, @list) = @_;\n\tfor (@list) {\n\t\treturn 1 if $_ == $value;\n\t}\n\treturn 0;\n}\n\nsub unique {\n\tmy %u; $u{$_} = 1 for @_;\n\tmy @rv; \n\tfor (@_) {\n\t\tif ($u{$_} > 0) {\n\t\t\tpush(@rv, $_);\n\t\t\t$u{$_} = 0;\n\t\t}\n\t}\n\t@rv;\n}\n\nsub swap {\n\tmy ($a, $i, $j) = @_;\n\tmy @b = @{ $a };\n\t@b[$i,$j] = @b[$j,$i];\n\t\\@b;\n}\n\nsub diff {\n\tmy ($a, $b) = @_;\n\tmy @i;\n\tmy $ok = 1;\n\tfor $i (0..$n-1) {\n\t\tif ($a->[$i] ne $b->[$i]) {\n\t\t\tpush @i, $i;\n\t\t}\n\t}\n\tif (join(\"\", sort @$a[@i]) ne join(\"\", sort @$b[@i])) {\n\t\t$ok = 0;\n\t}\n\tdiag \"diff a=[@$a] b=[@$b] i=[@i] ok=$ok\\n\";\n\t($ok, @i);\n}\n\nsub test {\n\tmy $t = shift;\n\tdiag \"test [@$t]\\n\";\n\tmy $y = 1;\n\tfor $s (@s) {\n\t\tmy ($ok, @i) = diff($s, $t);\n\t\tif ($ok) {\n\t\t\tmy $d = scalar @i;\n\t\t\t$y *= ($d==2 or ($d==0 and $repeat));\n\t\t} else {\n\t\t\t$y = 0;\n\t\t\tlast;\n\t\t}\n\t}\n\tdiag \"y=$y\\n\";\n\t$y;\n}\n\nsub set_new {\n\tmy %s; \\%s;\n}\n\nsub set_add {\n\tmy ($s, @list) = @_;\n\tfor (@list) {\n\t\t$s->{$_} = 1\n\t}\n}\n\nsub set_size {\n\tmy $s = shift;\n\tscalar keys %$s;\n}\n\nsub set_list {\n\tmy $s = shift;\n\tsort keys %$s;\n}\n\n$/ = \"\"; ($k, $n, @s) = split \" \", <>;\ndiag \"input k=$k\\n\";\n@s = map { [split //] } unique @s[0..$k-1];\ndiag \"unique $#s+1\\n\";\n$repeat += $_ - 1 for values %{counter $s[0]};\ndiag \"repeat=$repeat\\n\";\n\n$s = set_new();\nfor ($i = 1; $i <= 4; $i++) {\n\tif (@s > $i) {\n\t\tmy ($ok, @i) = diff($s[0], $s[$i]);\n\t\t$diag .= \"$i-$ok-$#i+1;\";\n\t\tif ($ok) {\n\t\t\tset_add $s, @i;\n\t\t} else {\n\t\t\tgoto ans;\n\t\t}\n\t}\n}\n@i = set_list($s); @i = @i[0..3] if @i > 4;\n$count = scalar @i;\ndiag \"i=[@i] count=$count\\n\";\n$diag .= join(\"+\", @i) . \";\";\n\t\nif (@s == 1) {\n\t$ans = swap($s[0], 0, 1);\n} \nelsif (in_list $count, 2, 3, 4) {\n\tmy @t;\n\tfor ($i = 0; $i < @i; $i++) {\n\t\tfor ($j = $i + 1; $j < @i; $j++) {\n\t\t\t$t = swap $s[0], @i[$i,$j];\n\t\t\tpush @t, $t;\n\t\t}\n\t}\n\tif ($repeat) {\n\t\tunshift @t, $s[0];\n\t}\n\tdiag \"plan $#t+1 tests\\n\";\n\t$diag .= \"$#t+1;\";\n\tfor $t (@t) {\n\t\tif (test($t)) {\n\t\t\t$ans = $t;\n\t\t\tlast;\n\t\t}\n\t}\n}\t\n\nans:\nif ($n == 1666) {\n\tprint $diag;\n} else {\n\t$ans = join(\"\", @$ans) || -1;\n\tprint $ans;\n}"}, {"source_code": "$/ = \"\"; $_ = <>; ($k, $n, @s) = split;\n($a, @b) = grep !$u{$_}++, @s;\ngoto ans if grep {cn($a) ne cn($_)} @b;\n$ans = make($a,0,1), goto ans unless @b;\n$aa = $a =~ /(.).*\\1/;\n\n# bet\n$d |= $a ^ $_ for @b;\n@d = split //, $d;\nfor $i (0..$#d) {\n\tpush @i, $i if $d[$i] and @i<10;\n}\n\n# loop\nfor $i (@i) {\nfor $j (@i) {\n\t$y = 1;\n\t$t = make($a, $i, $j);\n\tfor $b (@b) {\n\t\t$d = grep {ord} split //, $t ^ $b;\n\t\t$y *= $d==2 || ($d==0 and $aa);\n\t}\n\tif ($y) {\n\t\t$ans = $t, goto ans;\n\t}\n}}\nans: print $ans || -1;\n\nsub cn {\n\tjoin \"\", sort split //, shift;\n}\n\nsub make {\n\tmy ($s, $i, $j) = @_;\n\tvec($s,$i,8)^=vec($s,$j,8)^=vec($s,$i,8)^=vec($s,$j,8) if $i-$j;\n\t$s;\n}"}, {"source_code": "sub in_list {\n\tmy ($value, @list) = @_;\n\tfor (@list) {\n\t\treturn 1 if $_ == $value;\n\t}\n\treturn 0;\n}\n\nsub unique {\n\tmy %u; $u{$_} = 1 for @_;\n\tmy @rv; \n\tfor (@_) {\n\t\tif ($u{$_} > 0) {\n\t\t\tpush(@rv, $_);\n\t\t\t$u{$_} = 0;\n\t\t}\n\t}\n\t@rv;\n}\n\nsub swap {\n\tmy ($a, $i, $j) = @_;\n\tmy @b = @{ $a };\n\t@b[$i,$j] = @b[$j,$i];\n\t\\@b;\n}\n\nsub diff {\n\tmy ($a, $b) = @_;\n\tmy $count = 0;\n\tmy @i;\n\tfor $i (0..$n-1) {\n\t\tif ($a->[$i] ne $b->[$i]) {\n\t\t\t$count++;\n\t\t\tpush @i, $i;\n\t\t}\n\t}\n\tif (join(\"\", sort @$a[@i]) ne join(\"\", sort @$b[@i])) {\n\t\t$count = -1;\n\t}\n\t($count, @i);\n}\n\n$/ = \"\"; ($k, $n, @s) = split \" \", <>;\n@s = map { [split //] } unique splice @s, 0, $k;\n($count, @i) = diff($s[0], $s[1]);\n\nif (@s == 1) {\n\t$ans = swap($s[0], 0, 1);\n} \nelsif (in_list $count, 2, 3, 4) {\n\tfor ($i = 0; $i < @i; $i++) {\n\t\tfor ($j = $i + 1; $j < @i; $j++) {\n\t\t\t$t = swap $s[0], @i[$i,$j];\n\t\t\t$y = 1;\n\t\t\tfor $s (@s) {\n\t\t\t\t$d = diff($s, $t);\n\t\t\t\t$y *= ($d==0 or $d==2);\n\t\t\t}\n\t\t\tif ($y) {\n\t\t\t\t$ans = $t;\n\t\t\t}\n\t\t}\n\t}\n}\t\n\n$ans = join(\"\", @$ans) || -1;\nprint $ans;"}, {"source_code": "sub diag {\n\t# print @_;\n}\t\n\nsub counter {\n\tmy $a = shift;\n\tmy %c; $c{$_}++ for @$a;\n\t\\%c;\n}\n\nsub in_list {\n\tmy ($value, @list) = @_;\n\tfor (@list) {\n\t\treturn 1 if $_ == $value;\n\t}\n\treturn 0;\n}\n\nsub unique {\n\tmy %u; $u{$_} = 1 for @_;\n\tmy @rv; \n\tfor (@_) {\n\t\tif ($u{$_} > 0) {\n\t\t\tpush(@rv, $_);\n\t\t\t$u{$_} = 0;\n\t\t}\n\t}\n\t@rv;\n}\n\nsub swap {\n\tmy ($a, $i, $j) = @_;\n\tmy @b = @{ $a };\n\t@b[$i,$j] = @b[$j,$i];\n\t\\@b;\n}\n\nsub diff {\n\tmy ($a, $b) = @_;\n\tmy $count = 0;\n\tmy @i;\n\tfor $i (0..$n-1) {\n\t\tif ($a->[$i] ne $b->[$i]) {\n\t\t\t$count++;\n\t\t\tpush @i, $i;\n\t\t}\n\t}\n\tif (join(\"\", sort @$a[@i]) ne join(\"\", sort @$b[@i])) {\n\t\t$count = -1;\n\t}\n\tdiag \"diff a=[@$a] b=[@$b] count=$count i=[@i]\\n\";\n\t($count, @i);\n}\n\nsub test {\n\tmy $t = shift;\n\tdiag \"test [@$t]\\n\";\n\tmy $y = 1;\n\tfor $s (@s) {\n\t\t$d = diff($s, $t);\n\t\t$y *= ($d==2 or ($d==0 and $repeat));\n\t}\n\t$y;\n}\n\n$/ = \"\"; ($k, $n, @s) = split \" \", <>;\n@s = map { [split //] } unique splice @s, 0, $k;\n$repeat += $_ - 1 for values %{counter $s[0]};\ndiag \"repeat=$repeat\\n\";\n($count, @i) = diff($s[0], $s[1]);\n\nif (@s == 1) {\n\t$ans = swap($s[0], 0, 1);\n} \nelsif (in_list $count, 2, 3, 4) {\n\tmy @t;\n\tfor ($i = 0; $i < @i; $i++) {\n\t\tfor ($j = $i + 1; $j < @i; $j++) {\n\t\t\t$t = swap $s[0], @i[$i,$j];\n\t\t\tpush @t, $t;\n\t\t}\n\t}\n\tif ($repeat) {\n\t\tpush @t, $s[0];\n\t}\n\tdiag \"plan $#t+1 tests\\n\";\n\tfor $t (@t) {\n\t\tif (test($t)) {\n\t\t\t$ans = $t;\n\t\t\tlast;\n\t\t}\n\t}\n}\t\n\n$ans = join(\"\", @$ans) || -1;\nprint $ans;"}, {"source_code": "sub diag {\n\t# print @_;\n}\t\n\nsub counter {\n\tmy $a = shift;\n\tmy %c; $c{$_}++ for @$a;\n\t\\%c;\n}\n\nsub in_list {\n\tmy ($value, @list) = @_;\n\tfor (@list) {\n\t\treturn 1 if $_ == $value;\n\t}\n\treturn 0;\n}\n\nsub unique {\n\tmy %u; $u{$_} = 1 for @_;\n\tmy @rv; \n\tfor (@_) {\n\t\tif ($u{$_} > 0) {\n\t\t\tpush(@rv, $_);\n\t\t\t$u{$_} = 0;\n\t\t}\n\t}\n\t@rv;\n}\n\nsub swap {\n\tmy ($a, $i, $j) = @_;\n\tmy @b = @{ $a };\n\t@b[$i,$j] = @b[$j,$i];\n\t\\@b;\n}\n\nsub diff {\n\tmy ($a, $b) = @_;\n\tmy @i;\n\tfor $i (0..$n-1) {\n\t\tif ($a->[$i] ne $b->[$i]) {\n\t\t\tpush @i, $i;\n\t\t}\n\t}\n\tif (join(\"\", sort @$a[@i]) ne join(\"\", sort @$b[@i])) {\n\t\t@i = ();\n\t}\n\tdiag \"diff a=[@$a] b=[@$b] i=[@i]\\n\";\n\t@i;\n}\n\nsub test {\n\tmy $t = shift;\n\tdiag \"test [@$t]\\n\";\n\tmy $y = 1;\n\tfor $s (@s) {\n\t\t$d = diff($s, $t);\n\t\t$y *= ($d==2 or ($d==0 and $repeat));\n\t}\n\t$y;\n}\n\nsub set_new {\n\tmy %s; \\%s;\n}\n\nsub set_add {\n\tmy ($s, @list) = @_;\n\tfor (@list) {\n\t\t$s->{$_} = 1\n\t}\n}\n\nsub set_size {\n\tmy $s = shift;\n\tscalar keys %$s;\n}\n\nsub set_list {\n\tmy $s = shift;\n\tsort keys %$s;\n}\n\n$/ = \"\"; ($k, $n, @s) = split \" \", <>;\ndiag \"input k=$k\\n\";\n@s = map { [split //] } unique splice @s, 0, $k;\ndiag \"unique $#s+1\\n\";\n$repeat += $_ - 1 for values %{counter $s[0]};\ndiag \"repeat=$repeat\\n\";\n\n$s = set_new();\nfor ($i = 1; $i <= 4; $i++) {\n\tif (@s > $i) {\n\t\tset_add $s, diff($s[0], $s[$i]);\n\t}\n}\n@i = set_list($s);\n$count = scalar @i;\ndiag \"i=[@i] count=$count\\n\";\n\t\nif (@s == 1) {\n\t$ans = swap($s[0], 0, 1);\n} \nelsif (in_list $count, 2, 3, 4) {\n\tmy @t;\n\tfor ($i = 0; $i < @i; $i++) {\n\t\tfor ($j = $i + 1; $j < @i; $j++) {\n\t\t\t$t = swap $s[0], @i[$i,$j];\n\t\t\tpush @t, $t;\n\t\t}\n\t}\n\tif ($repeat) {\n\t\tpush @t, $s[0];\n\t}\n\tdiag \"plan $#t+1 tests\\n\";\n\tfor $t (@t) {\n\t\tif (test($t)) {\n\t\t\t$ans = $t;\n\t\t\tlast;\n\t\t}\n\t}\n}\t\n\n$ans = join(\"\", @$ans) || -1;\nprint $ans;"}, {"source_code": "# input\n$/ = \"\"; $_ = <>; ($k, $n, @s) = split;\n($a, @b) = grep !$u{$_}++, @s; \ngoto ans if grep {cn($a) ne cn($_)} @b;\n$ans = make($a,0,1), goto ans unless @b;\n$aa = $a =~ /(.).*\\1/;\n\n# bet\nfor $b (@b) {\n\tlen($d) < 4 or last;\n\t$d |= $a ^ $b;\n}\n@d = split //, $d;\nfor $i (0..$#d) {\n\tif (ord $d[$i] and @i < 10) {\n\t\tpush @i, $i;\n\t}\n}\n\n# loop\nfor $i (@i) {\nfor $j (@i) {\n\t$y = 1;\n\t$t = make($a, $i, $j);\n\tfor $b (@b) {\n\t\t$d = len($t ^ $b);\n\t\t$y *= ($d==2 or ($d==0 and $aa));\n\t}\n\tif ($y) {\n\t\t$ans = $t, goto ans;\n\t}\n}}\nans: print $ans || -1;\n\nsub cn {\n\tjoin \"\", sort split //, shift;\n}\n\nsub make {\n\tmy ($s, $i, $j) = @_;\n\tvec($s,$i,8)^=vec($s,$j,8)^=vec($s,$i,8)^=vec($s,$j,8) if $i-$j;\n\t$s;\n}\n\nsub len {\n\tlength $_[0] =~ s/\\0//gr;\n}"}], "src_uid": "0550004c6c7a386b8e7d214e71990572"} {"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$$$) \u2014 the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le 998244353$$$) \u2014 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 \u2014 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": "$\\ = $/;\n\n<>;\n\nfor( <> ){\n ( $_ ) = split;\n print join ' ', $_, $_ * 2;\n }"}], "negative_code": [], "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 \u2014 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 \u2014 n and k (1\u2009\u2264\u2009n,\u2009k\u2009\u2264\u20091000).", "output_spec": "In the first line print an integer m \u2014 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\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u2009n; ai\u2009\u2260\u2009bi). 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": "<>=~/ /;\nif ($`<=$'*2){print -1; exit}\nfor $j(1..$'){\n\tfor $i(1..$`){\n\t\tpush @_,(\"\\n$i \".($i+$j>$`?($i+$j-$`):($i+$j)));\n\t\t\t}\n\t\t}\nprint 0+@_,@_"}, {"source_code": "<>=~/ /;\nif ($`<=$'*2){print -1; exit}\nfor $j(1..$'){\n\tfor $i(1..($m=$`+0)){\n\t\t$_.=\"\\n$i \".($i+$j>$m?($i+$j-$m):($i+$j));\n\t\t\t}\n\t\t}\nprint $`*$',$_"}, {"source_code": "<>=~/ /;\nif ($`<=$'*2){print -1; exit}\nfor ($j=1; $j<=$'; $j++){\n\tfor ($i=1; $i<=$`; $i++){\n\t\tpush @_,(\"\\n$i \".($i+$j>$`?($i+$j-$`):($i+$j)));\n\t\t\t}\n\t\t}\nprint 0+@_,@_"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t\n\t$_=\"\";\n\tif ($n<=$k*2){print -1; next}\n\tfor ($j=1; $j<=$k; $j++){\n\t\tfor ($i=1; $i<=$n; $i++){\n\t\t\t\n\t\t\t$_.= (\"\\n$i \".($i+$j>$n?($i+$j-$n):($i+$j)));\n\t\t\t\n\t\t\t\n\t\t\t}\n\t\t\t\n\t\t}\n\tprint $n*$k,$_\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t\n\t@_=();\n\tif ($n<=$k*2){print -1; next}\n\tfor $j(1..$k){\n\t\tfor $i(1..$n){\n\t\t\t\n\t\t\tpush @_, (\"$i \".($i+$j>$n?($i+$j-$n):($i+$j)).\"\\n\");\n\t\t\t\n\t\t\t\n\t\t\t}\n\t\t\t\n\t\t}\n\t\tprint 0+@_,\"\\n\";\n\tprint @_\n\t}"}, {"source_code": "$_=<>;\nchomp;\n($n,$k)=split/ /;\n$_=\"\";\nif ($n<=$k*2){print -1; exit}\nfor $j(1..$k){\n\tfor $i(1..$n){\n\t\t\t\n\t\t$_.= \"\\n$i \".($i+$j>$n?($i+$j-$n):($i+$j));\n\t\t\t\t\n\t\t}\n\t\t\n\t}\nprint $n*$k,$_"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t\n\t@_=();\n\tif ($n<=$k*2){print -1; next}\n\tfor ($j=1; $j<=$k; $j++){\n\t\tfor ($i=1, $m=1+$j; $i<=$n; $i++, $m++){\n\t\t\t\n\t\t\tpush @_, (\"$i \".($m>$n?($m-$n):$m).\"\\n\");\n\t\t\t\n\t\t\t\n\t\t\t}\n\t\t\t\n\t\t}\n\t\tprint 0+@_,\"\\n\";\n\tprint @_\n\t}"}, {"source_code": "($n,$k)=split/ /,<>;\nchomp $k;\n$_=\"\";\nif ($n<=$k*2){print -1; exit}\nfor $j(1..$k){\n\tfor $i(1..$n){\n\t\t\t\n\t\t$_.= \"\\n$i \".($i+$j>$n?($i+$j-$n):($i+$j));\n\t\t\t\t\n\t\t}\n\t\t\n\t}\nprint $n*$k,$_"}, {"source_code": "<>=~/ /;\nif ($`<=$'*2){print -1; exit}\nfor $j(1..$'){\n\tfor $i(1..$`){\n\t\tpush @_,(\"\\n$i \".($i+$j>$`?($i+$j-$`):($i+$j)));\n\t\t\t}\n\t\t}\nprint 0+@_,@_"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t\n\t@_=();\n\tif ($n<=$k*2){print -1; next}\n\tfor ($j=1; $j<=$k; $j++){\n\t\tfor ($i=1; $i<=$n; $i++){\n\t\t\t\n\t\t\tpush @_, (\"$i \".($i+$j>$n?($i+$j-$n):($i+$j)).\"\\n\");\n\t\t\t\n\t\t\t\n\t\t\t}\n\t\t\t\n\t\t}\n\t\tprint 0+@_,\"\\n\";\n\tprint @_\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t\n\t@_=();\n\tif ($n<=$k*2){print -1; next}\n\tfor $j(1..$k){\n\t\tfor $i(1..$n){\n\t\t\t\n\t\t\tpush @_, (\"\\n$i \".($i+$j>$n?($i+$j-$n):($i+$j)));\n\t\t\t\n\t\t\t\n\t\t\t}\n\t\t\t\n\t\t}\n\tprint 0+@_,@_\n\t}"}], "negative_code": [{"source_code": "$_=<>;chop;/ /;\nif ($`<=$'*2){print -1; exit}\nfor $j(1..$'){\n\tfor $i(1..$`){\n\t\t$_.=\"\\n$i \".($i+$j>$`?($i+$j-$`):($i+$j));\n\t\t\t}\n\t\t}\nprint $`*$',$_"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t\n\t@_=();\n\tif ($n<=$k){print -1; next}\n\tfor ($j=1; $j<=$k; $j++){\n\t\tfor ($i=1; $i<=$n; $i++){\n\t\t\t\n\t\t\tpush @_, (\"$i \".($i+$j>$n?($i+$j-$n):($i+$j)).\"\\n\");\n\t\t\t\n\t\t\t\n\t\t\t}\n\t\t\t\n\t\t}\nprint 0+@_,\"\\n\";\n\tprint @_\n\t}"}, {"source_code": "($n,$k)=split/ /,<>;\nchomp $k;\n$_=\"\";\nif ($n<=$k*2){print -1; next}\nfor $j(1..$k){\n\tfor $i(1..$n){\n\t\t\t\n\t\t$_.= \"\\n$i \".($i+$j>$n?($i+$j-$n):($i+$j));\n\t\t\t\t\n\t\t}\n\t\t\n\t}\nprint $n*$m,$_"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t\n\t@_=();\n\tif ($n<=$k){print -1; next}\n\tfor ($j=1; $j<=$k; $j++){\n\t\tfor ($i=1; $i<=$n; $i++){\n\t\t\t\n\t\t\tpush @_, (\"$i \".($i+$j>$n?($i+$j-$n):($i+$j)).\"\\n\");\n\t\t\t\n\t\t\t\n\t\t\t}\n\t\t\t\n\t\t}\n\tprint @_\n\t}"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t\n\t@_=();\n\tif ($n<=$k*2){print -1; next}\n\tfor $j(1..$k){\n\t\tfor $i(1..$n){\n\t\t\t\n\t\t\tpush @_, (\"\\n$i \".($i+$j));\n\t\t\t\n\t\t\t\n\t\t\t}\n\t\t\t\n\t\t}\n\tprint 0+@_,@_\n\t}"}], "src_uid": "14570079152bbf6c439bfceef9816f7e"} {"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\u2009\u2264\u2009n\u2009\u2264\u200950; 1\u2009\u2264\u2009k\u2009\u2264\u2009 ) \u2014 the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009107) \u2014 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\u2009\u2264\u2009ci\u2009\u2264\u2009n) \u2014 the number of soldiers in the detachment on the i-th day of the pageant and ci distinct integers p1,\u2009i,\u2009p2,\u2009i,\u2009...,\u2009pci,\u2009i \u2014 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": "#!/usr/bin/perl -w\n\nuse strict;\nuse bigint;\nuse List::Util qw(sum);\n\nchomp($_ = <>);\nmy ($n, $k) = split;\nchomp($_ = <>);\nmy @a = split;\nmy %h = ();\n\nfor my $i (@a) {\n my %tmp = (%h, $i => [$i]);\n for my $j (keys %h) {\n my $sum = $j + $i;\n next if exists $tmp{$sum};\n $tmp{$sum} = [@{$h{$j}}, $i];\n }\n %h = %tmp;\n last if keys %h >= $k;\n}\nwhile (my ($key, $val) = each %h) {\n print scalar @$val, \" @$val\\n\";\n last if --$k == 0;\n}\n"}, {"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Wed Nov 21 22:10:07 IST 2012\n# File Name: c.pl\n# USAGE: \n# c.pl \n# \n# \n#------------------------------------------------\nchomp (my ($n, $k) = split /\\s+/, );\nchomp (my @lst = sort {$a <=> $b} split /\\s+/, );\n\nmy $prefix = '';\nmy $i = 1;\nwhile ($k > 0) {\n foreach ( @lst ) {\n print \"$i $prefix$_\";\n $k--;\n last if not ($k > 0);\n }\n $prefix .= pop @lst;\n $prefix .= ' ';\n $i++;\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\nuse bigint;\nuse List::Util qw(sum);\n\nchomp($_ = <>);\nmy ($n, $k) = split;\nchomp($_ = <>);\nmy @a = split;\nmy %h = ();\nfor my $i (@a) {\n my %tmp = (%h, $i => [$i]);\n for my $j (keys %h) {\n my $sum = $j + $i;\n next if exists $tmp{$sum};\n $tmp{$sum} = [@{$h{$j}}, $i];\n }\n %h = %tmp;\n last if keys %h >= $k;\n}\nwhile (my ($key, $val) = each %h) {\n print scalar @$val, \" @$val\\n\";\n}\n"}, {"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Wed Nov 21 22:10:07 IST 2012\n# File Name: c.pl\n# USAGE: \n# c.pl \n# \n# \n#------------------------------------------------\nchomp (my ($n, $k) = split /\\s+/, );\nchomp (my @lst = split /\\s+/, );\n\nmy $prefix = '';\nmy $i = 1;\nwhile ($k > 0) {\n foreach ( @lst ) {\n print \"$i $prefix$_\";\n $k--;\n last if not ($k > 0);\n }\n $prefix .= pop @lst;\n $prefix .= ' ';\n $i++;\n}\n"}, {"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Wed Nov 21 22:10:07 IST 2012\n# File Name: c.pl\n# USAGE: \n# c.pl \n# \n# \n#------------------------------------------------\nchomp (my ($n, $k) = split /\\s+/, );\nchomp (my @lst = split /\\s+/, );\n\nmy $prefix = '';\nmy $i = 1;\nwhile ($k > 0) {\n foreach ( @lst ) {\n print \"$i $prefix$_\";\n $k--;\n }\n $prefix .= pop @lst;\n $prefix .= ' ';\n $i++;\n}\n"}], "src_uid": "2b77aa85a192086316a7f87ce140112d"} {"nl": {"description": "Polycarp likes arithmetic progressions. A sequence $$$[a_1, a_2, \\dots, a_n]$$$ is called an arithmetic progression if for each $$$i$$$ ($$$1 \\le i < n$$$) the value $$$a_{i+1} - a_i$$$ is the same. For example, the sequences $$$[42]$$$, $$$[5, 5, 5]$$$, $$$[2, 11, 20, 29]$$$ and $$$[3, 2, 1, 0]$$$ are arithmetic progressions, but $$$[1, 0, 1]$$$, $$$[1, 3, 9]$$$ and $$$[2, 3, 1]$$$ are not.It follows from the definition that any sequence of length one or two is an arithmetic progression.Polycarp found some sequence of positive integers $$$[b_1, b_2, \\dots, b_n]$$$. He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by $$$1$$$, an element can be increased by $$$1$$$, an element can be left unchanged.Determine a minimum possible number of elements in $$$b$$$ which can be changed (by exactly one), so that the sequence $$$b$$$ becomes an arithmetic progression, or report that it is impossible.It is possible that the resulting sequence contains element equals $$$0$$$.", "input_spec": "The first line contains a single integer $$$n$$$ $$$(1 \\le n \\le 100\\,000)$$$ \u2014 the number of elements in $$$b$$$. The second line contains a sequence $$$b_1, b_2, \\dots, b_n$$$ $$$(1 \\le b_i \\le 10^{9})$$$.", "output_spec": "If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer \u2014 the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't use operation twice to the same position).", "sample_inputs": ["4\n24 21 14 10", "2\n500 500", "3\n14 5 1", "5\n1 3 6 9 12"], "sample_outputs": ["3", "0", "-1", "1"], "notes": "NoteIn the first example Polycarp should increase the first number on $$$1$$$, decrease the second number on $$$1$$$, increase the third number on $$$1$$$, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to $$$[25, 20, 15, 10]$$$, which is an arithmetic progression.In the second example Polycarp should not change anything, because his sequence is an arithmetic progression.In the third example it is impossible to make an arithmetic progression.In the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like $$$[0, 3, 6, 9, 12]$$$, which is an arithmetic progression."}, "positive_code": [{"source_code": "# author : Gabriel Hofer\nuse warnings;\nuse strict;\nuse constant NL => qq/\\n/;\n\nmy $n = ;\nmy @seq = split qq/ /, ;\nmy ($one, $two) = ( $seq [ 0 ], $seq [ 1 ] );\nmy $min = 10e9 + 3;\nAB: for my $three ( ($one-1) .. ($one+1) ) {\n AC: for my $four ( ($two-1) .. ($two+1) ) {\n my $sum = 0; \n my $six = $three; \n my $difference = $four - $three;\n AD: for my $five ( @seq ) {\n if ( abs ( $six - $five ) > 1 ) { next AC }\n else { $sum += abs ( $six - $five );\n $six += $difference }}\n $min = $sum if $sum < $min; \n }\n}\n\nprint qq/-1\\n/ if $min == (10e9 + 3);\nprint qq/$min\\n/ if $min != (10e9 + 3);\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tif( @_ == 1 ){\n\t\tprint 0;\n\t\tnext;\n\t\t}\n\t\n\tmy @ok;\n\t\n\tfor my $i ( map { $_[ 0 ] + $_ } -1 .. 1 ){\n\t\tfor my $j ( map { $_[ @_ - 1 ] + $_ } -1 .. 1 ){\n\t\t\tmy $d = $i - $j;\n\t\t\tnext if $d % ( @_ - 1 );\n\t\t\tmy $step = $d / ( @_ - 1 );\n\t\t\tmy $st = 0;\n\t\t\tmy @diffs;\n\t\t\tfor( @_ ){\n\t\t\t\tmy $diff = $st + ( $_ - $i );\n\t\t\t\tpush @diffs, $diff;\n\t\t\t\t$st += $step;\n\t\t\t\t}\n\t\t\t$debug and print \"$step| $i [@diffs] $j\";\n\t\t\t\n\t\t\tmy $bad = grep { $_ < -1 || $_ > 1 } @diffs;\n\t\t\t\n\t\t\t$bad or push @ok, \"@diffs\";\n\t\t\t}\n\t\t}\n\t\n\tif( !@ok ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\tprint +( sort { $a <=> $b } map { ~~ ( () = /1/g ) } @ok )[ 0 ];\n\t\n\t$debug and print \"-\" x 15;\n\t}"}, {"source_code": "$\\ = $/;\n\n<>;\n\n@_ = split ' ', <>;\n\nif( @_ < 2 ){\n\tprint 0;\n\texit;\n\t}\n\nmy @ok;\n\nfor $i ( map { $_[ 0 ] + $_ } -1 .. 1 ){\n\tfor $j ( map { $_[ @_ - 1 ] + $_ } -1 .. 1 ){\n\t\t$s = ( $i - $j ) / ( @_ - 1 );\n\t\tnext if $s =~ /\\./;\n\t\t$z = 0;\n\t\tmy @d;\n\t\tfor( @_ ){\n\t\t\tpush @d, $z + ( $_ - $i );\n\t\t\t$z += $s;\n\t\t\t}\n\t\t\t\t\t\n\t\tpush @ok, ~~ ( () = \"@d\" =~ /1/g ) if !grep { $_ < -1 || $_ > 1 } @d;\n\t\t}\n\t}\n\nprint !@ok ? -1 : ( sort { $a <=> $b } @ok )[ 0 ]"}], "negative_code": [], "src_uid": "79b0794f81acc1c882649d96b1c7f8da"} {"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\u2009-\u20091 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\u2009\u2264\u2009n\u2009\u2264\u2009105). The second line contains n space-separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 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 \u2014 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 \u0421++. 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": "#!/usr/bin/perl -w\n\nmy $n = ;\nmy @a = split(/ /, );\nmy $s = 0;\nforeach my $x (@a) { $s += $x; }\n$s = int(($s + $n - 2) / ($n-1));\nforeach my $x (@a) { $s = $x if $s < $x; }\nprintf(\"%d\\n\", $s);"}, {"source_code": "#!/usr/bin/perl -w\n\nmy $n = ;\nmy @a = split(/ /, );\nmy $s = 0;\nforeach my $x (@a) { $s += $x; }\npush @a, int(($s + $n - 2) / ($n-1));\n$s = 0;\nforeach my $x (@a) { $s = $x if $s < $x; }\nprintf(\"%d\\n\", $s);"}, {"source_code": "use POSIX qw/ceil/;\n$n=<>;$m=0;@l=split(' ',<>);foreach(@l){if($_>$m){$m=$_;}}\n$s=0;map{$s+=$_}@l;$s=ceil($s/($n-1));\nif($m>$s){$s=$m;}\nprint \"$s\\n\";\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w\n\nmy $n = ;\nmy @a = split(/ /, );\nforeach my $x (@a) { $s += $x; }\n$s = int(($s + $n - 2) / ($n-1));\nprint \"$s\";"}, {"source_code": "use POSIX qw/ceil/;\n$n=<>;@l=split(' ',<>);$s=0;map{$s+=$_}@l;$s=ceil($s/($n-1));print \"$s\\n\";\n"}], "src_uid": "09f5623c3717c9d360334500b198d8e0"} {"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$$$)\u00a0\u2014 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 = ;\r\nfor (; $t > 0; $t--) {\r\n my $n = ;\r\n my %h = ();\r\n my @arr = split(\" \", );\r\n for ($i = 0; $i < $n; ++$i) {\r\n $ind = $arr[$i] - $i;\r\n if (exists($h{$ind})) {\r\n $h{$ind} = $h{$ind} + 1; \r\n } else {\r\n $h{$ind} = 1;\r\n }\r\n }\r\n \r\n my $ans = 0;\r\n for my $key ( keys %h ) { \r\n my $val = $h{$key};\r\n $ans += $val * ($val - 1) / 2;\r\n }\r\n print(\"$ans\\n\");\r\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\u2009\u2264\u2009b,\u2009d,\u2009s\u2009\u2264\u20091018,\u2009\u2009b\u2009+\u2009d\u2009+\u2009s\u2009\u2265\u20091)\u00a0\u2014 the number of breakfasts, dinners and suppers which Vasiliy had during his vacation in the sanatorium. ", "output_spec": "Print single integer\u00a0\u2014 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": "print 0 + eval join '+', grep !/-/, map { $_[0] - 1 - $_ } @_ = sort {$b <=> $a} split ' ', <>"}, {"source_code": "print 0 + eval join '+', grep !/-/, map { $_[0] - 1 - $_ } @_ = sort {$b <=> $a} split ' ', <>"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\t@_ = sort {$b <=> $a} split;\n\tprint 0 + eval join '+', grep !/-/, map { $_[0] - 1 - $_ } @_;\n\t\n\t}"}], "negative_code": [{"source_code": "print eval join '+', grep !/-/, map { $_[0] - 1 - $_ } @_ = sort {$b <=> $a} split ' ', <>"}], "src_uid": "b34846d90dc011f2ef37a8256202528a"} {"nl": {"description": "In the official contest this problem has a different statement, for which jury's solution was working incorrectly, and for this reason it was excluded from the contest. This mistake have been fixed and the current given problem statement and model solution corresponds to what jury wanted it to be during the contest.Vova and Lesha are friends. They often meet at Vova's place and compete against each other in a computer game named The Ancient Papyri: Swordsink. Vova always chooses a warrior as his fighter and Leshac chooses an archer. After that they should choose initial positions for their characters and start the fight. A warrior is good at melee combat, so Vova will try to make the distance between fighters as small as possible. An archer prefers to keep the enemy at a distance, so Lesha will try to make the initial distance as large as possible.There are n (n is always even) possible starting positions for characters marked along the Ox axis. The positions are given by their distinct coordinates x1,\u2009x2,\u2009...,\u2009xn, two characters cannot end up at the same position.Vova and Lesha take turns banning available positions, Vova moves first. During each turn one of the guys bans exactly one of the remaining positions. Banned positions cannot be used by both Vova and Lesha. They continue to make moves until there are only two possible positions remaining (thus, the total number of moves will be n\u2009-\u20092). After that Vova's character takes the position with the lesser coordinate and Lesha's character takes the position with the bigger coordinate and the guys start fighting.Vova and Lesha are already tired by the game of choosing positions, as they need to play it before every fight, so they asked you (the developer of the The Ancient Papyri: Swordsink) to write a module that would automatically determine the distance at which the warrior and the archer will start fighting if both Vova and Lesha play optimally.", "input_spec": "The first line on the input contains a single integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000, n is even)\u00a0\u2014 the number of positions available initially. The second line contains n distinct integers x1,\u2009x2,\u2009...,\u2009xn (0\u2009\u2264\u2009xi\u2009\u2264\u2009109), giving the coordinates of the corresponding positions.", "output_spec": "Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally.", "sample_inputs": ["6\n0 1 3 7 15 31", "2\n73 37"], "sample_outputs": ["7", "36"], "notes": "NoteIn the first sample one of the optimum behavior of the players looks like that: Vova bans the position at coordinate 15; Lesha bans the position at coordinate 3; Vova bans the position at coordinate 31; Lesha bans the position at coordinate 1. After these actions only positions 0 and 7 will remain, and the distance between them is equal to 7.In the second sample there are only two possible positions, so there will be no bans."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\n$n = ;\n@x = split \" \", ;\n@x = sort {$a <=> $b} @x;\n$len = @x;\n$step = ($n - 2) / 2 + 1;\n$ans = -1;\nfor ($i = 0;$i + $step < $len;$i ++) {\n if ($ans == -1 || $x[$i + $step] - $x[$i] < $ans) {\n $ans = $x[$i + $step] - $x[$i];\n }\n}\nprint $ans, \"\\n\";\n\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @array = split(' ', <>);\n\n@array = sort {$a <=> $b} @array;\n\n$n /= 2;\nmy $ans = 1 << 30;\n\nfor($_ = 0 ; $_ < $n ; $_ ++){\n\t$ans = $array[$_ + $n] - $array[$_]\n\t\tif $ans > $array[$_ + $n] - $array[$_];\n}\nprint \"$ans\\n\";\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @array = split(' ', <>);\n\n@array = sort {$a <=> $b} @array;\n\n$n /= 2;\nmy $ans = 1 << 30;\n\nfor($_ = 0 ; $_ < $n ; $_ ++){\n $ans = $array[$_ + $n] - $array[$_]\n if $ans > $array[$_ + $n] - $array[$_];\n}\nprint \"$ans\\n\";"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @array = split(' ', <>);\n\n@array = sort {$a <=> $b} @array;\n\n$n /= 2;\nmy $ans = 1 << 30;\n\nfor(0..$n){\n $ans = $array[$_ + $n] - $array[$_]\n if $ans > $array[$_ + $n] - $array[$_];\n}\nprint \"$ans\\n\";"}], "src_uid": "8aaabe089d2512b76e5de938bac5c3bf"} {"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\u00e9j\u00e0 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$$$) \u2014 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": "#!/usr/bin/perl\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n my $l = length($s);\r\n if( $l == 1 ){ # \u9577\u30551\u306e\u30b1\u30fc\u30b9\u3092\u5148\u306b\r\n if( $s eq 'a' ){\r\n print \"NO\\n\";\r\n } else {\r\n print \"YES\\n\";\r\n print \"a$s\\n\";\r\n }\r\n next;\r\n }\r\n my $l2 = int($l/2);\r\n my $par = 1;\r\n for(my $i=0;$i<$l2;$i++){\r\n if( substr($s,$i,1) ne substr($s,$l-1-$i,1) ){\r\n $par = undef; last;\r\n }\r\n }\r\n if( $l % 2 == 1 ){ # \u5947\u6570\u9577\r\n if( substr($s,$l2,1) ne 'a' ){\r\n print \"YES\\n\";\r\n my $r = substr($s,0,$l2) . 'a' . substr($s,$l2,1) . substr($s,1+$l2,$l2);\r\n print \"$r\\n\";\r\n next;\r\n } else {\r\n my $s1 = substr($s,0,$l2);\r\n if( $par and $s1 =~ /^[a]*$/o ){\r\n print \"NO\\n\";\r\n next;\r\n }\r\n }\r\n } else {\r\n my $s1 = substr($s,0,$l2);\r\n if( $par and $s1 =~ /^[a]*$/o ){\r\n print \"NO\\n\"; next;\r\n }\r\n }\r\n my $i;\r\n for($i=0;$i<$l;$i++){\r\n last if( substr($s,$i,1) ne 'a' );\r\n }\r\n if( $i >= $l ){\r\n print \"NO\\n\"; next;\r\n }\r\n if( $i < $l2 ){\r\n my $s1 = substr($s,0,$l-$i) . 'a';\r\n $s1 .= substr($s,$l-$i,$i) if $i>0;\r\n print \"YES\\n$s1\\n\";\r\n } else {\r\n my $s1 = substr($s,0,($n-1)-$i) . 'a';\r\n $s1 .= substr($s,($n-1)-$i,$l-(($n-1)-$i)) if $l-(($n-1)-$i)>0;\r\n print \"YES\\n$s1\\n\";\r\n }\r\n \r\n}\r\n\r\nexit(0);\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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { [ $_, ++ $i ] } @_;\n\t\n\t@_ = sort { $a->[ 0 ] <=> $b->[ 0 ] } @_;\n\t\n\tmy $acc = $_[ 0 ][ 0 ];\n\tmy $min = 0;\n\t\n\tfor my $i ( 1 .. @_ - 1 ){\n\t\tif( $_[ $i ][ 0 ] > $acc ){\n\t\t\t$min = $i;\n\t\t\t}\n\t\t$acc += $_[ $i ][ 0 ];\n\t\t}\n\t\n\t@_ = sort { $a <=> $b } map $_->[ 1 ], @_[ $min .. @_ - 1 ];\n\t\n\tprint for ~~ @_, \"@_\";\n\t}"}], "negative_code": [], "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\u2009\u00d7\u20091. 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\u2009+\u20091. In case of x\u2009=\u2009n, 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\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the number of jars with colors Vika has. The second line of the input contains a sequence of integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109), 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\u00a0\u2014 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": "use feature ':all';\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t$min = ~0;\n\t$min > $_ and $min = $_ for @_;\n\t@_ = (@_) x 2;\n\t$max = 0;\n\tfor (0 .. @_-1){\n\t\t$_[$_] == $min ?\n\t\t\t\t( $cnt = 0 )\n\t\t\t: \n\t\t\t\tdo { $cnt ++; $max < $cnt and $max = $cnt }\n\t\t}\n\t\t\n\tsay $min * @_ / 2 + $max\n\t}"}, {"source_code": "$m = ~0;\n$m > $_ and $m = $_ for @_ = (@_ = split ' ', (<>, <>)) x 2;\n\t\nprint $m * @_ / 2 + ( sort {$b <=> $a} map length, split ' ', join '', map { $_ = $_ == $m ? ' ':1 } @_ )[ 0 ]"}], "negative_code": [], "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": "#!/usr/bin/env perl\n\nuse 5.012;\nno warnings 'experimental';\n\nchomp(my $number = );\n\nmy @last = ();\nmy $res = 0;\nfor (split '', $number) {\n my $residue = $_ % 3;\n given ($residue) {\n when (0) {\n $res++;\n @last = ();\n }\n default {\n push @last, $residue;\n my $count = 0;\n for my $num (reverse @last) {\n $count = ($count + $num) % 3;\n if ($count == 0) {\n ++$res;\n @last = ();\n last;\n }\n }\n }\n }\n}\n\nsay $res;\n## 1101121113\n## 221\n"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\n\nuse 5.012;\nno warnings 'experimental';\n\nchomp(my $number = );\n\nmy $last = 0;\nmy $res = 0;\nfor (split '', $number) {\n my $residue = $_ % 3;\n given ($residue) {\n when (0) {\n $res++;\n $last = 0;\n }\n default {\n $last = ($last + $residue) % 3;\n if ($last == 0) {\n $res++;\n }\n }\n }\n}\n\nsay $res;\n"}], "src_uid": "3b2d0d396649a200a73faf1b930ef611"} {"nl": {"description": "You are given $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$, where $$$n$$$ is odd. You are allowed to flip the sign of some (possibly all or none) of them. You wish to perform these flips in such a way that the following conditions hold: At least $$$\\frac{n - 1}{2}$$$ of the adjacent differences $$$a_{i + 1} - a_i$$$ for $$$i = 1, 2, \\dots, n - 1$$$ are greater than or equal to $$$0$$$. At least $$$\\frac{n - 1}{2}$$$ of the adjacent differences $$$a_{i + 1} - a_i$$$ for $$$i = 1, 2, \\dots, n - 1$$$ are less than or equal to $$$0$$$. Find any valid way to flip the signs. It can be shown that under the given constraints, there always exists at least one choice of signs to flip that satisfies the required condition. If there are several solutions, you can find any of them.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 500$$$) \u00a0\u2014 the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3 \\le n \\le 99$$$, $$$n$$$ is odd) \u00a0\u2014 the number of integers given to you. 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$$$) \u00a0\u2014 the numbers themselves. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10000$$$.", "output_spec": "For each test case, print $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$, corresponding to the integers after flipping signs. $$$b_i$$$ has to be equal to either $$$a_i$$$ or $$$-a_i$$$, and of the adjacent differences $$$b_{i + 1} - b_i$$$ for $$$i = 1, \\dots, n - 1$$$, at least $$$\\frac{n - 1}{2}$$$ should be non-negative and at least $$$\\frac{n - 1}{2}$$$ should be non-positive. It can be shown that under the given constraints, there always exists at least one choice of signs to flip that satisfies the required condition. If there are several solutions, you can find any of them.", "sample_inputs": ["5\n3\n-2 4 3\n5\n1 1 1 1 1\n5\n-2 4 7 -6 4\n9\n9 7 -4 -2 1 -3 9 -4 -5\n9\n-4 1 9 4 8 9 5 1 -9"], "sample_outputs": ["-2 -4 3\n1 1 1 1 1\n-2 -4 7 -6 4\n-9 -7 -4 2 1 -3 -9 -4 -5\n4 -1 -9 -4 -8 -9 -5 -1 9"], "notes": "NoteIn the first test case, the difference $$$(-4) - (-2) = -2$$$ is non-positive, while the difference $$$3 - (-4) = 7$$$ is non-negative.In the second test case, we don't have to flip any signs. All $$$4$$$ differences are equal to $$$0$$$, which is both non-positive and non-negative.In the third test case, $$$7 - (-4)$$$ and $$$4 - (-6)$$$ are non-negative, while $$$(-4) - (-2)$$$ and $$$(-6) - 7$$$ are non-positive."}, "positive_code": [{"source_code": "$t = <>;\nwhile ($t--) {\n $n = <>;\n @a = split(\" \", <>);\n for (my $i = 0; $i < $n; $i++) {\n $a[$i] *= -1 if ($a[$i] < 0 xor $i % 2);\n }\n print join(\" \", @a), \"\\n\";\n}\n"}], "negative_code": [], "src_uid": "d07ae42b7902ba3a49cf4463248710ea"} {"nl": {"description": "Monocarp wrote down two numbers on a whiteboard. Both numbers follow a specific format: a positive integer $$$x$$$ with $$$p$$$ zeros appended to its end.Now Monocarp asks you to compare these two numbers. Can you help him?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of testcases. The first line of each testcase contains two integers $$$x_1$$$ and $$$p_1$$$ ($$$1 \\le x_1 \\le 10^6; 0 \\le p_1 \\le 10^6$$$)\u00a0\u2014 the description of the first number. The second line of each testcase contains two integers $$$x_2$$$ and $$$p_2$$$ ($$$1 \\le x_2 \\le 10^6; 0 \\le p_2 \\le 10^6$$$)\u00a0\u2014 the description of the second number.", "output_spec": "For each testcase print the result of the comparison of the given two numbers. If the first number is smaller than the second one, print '<'. If the first number is greater than the second one, print '>'. If they are equal, print '='.", "sample_inputs": ["5\n2 1\n19 0\n10 2\n100 1\n1999 0\n2 3\n1 0\n1 0\n99 0\n1 2"], "sample_outputs": [">\n=\n<\n=\n<"], "notes": "NoteThe comparisons in the example are: $$$20 > 19$$$, $$$1000 = 1000$$$, $$$1999 < 2000$$$, $$$1 = 1$$$, $$$99 < 100$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $x1, $p1, $x2, $p2 ) = split ' ', $_ . <>;\n\t\n\tmy $min = $p1;\n\t$min > $p2 and $min = $p2;\n\t\n\t$_ -= $min for $p1, $p2;\n\t\n\t$_ > 7 and $_ = 7 for $p1, $p2;\n\t\n\t$x1 *= 10 ** $p1;\n\t$x2 *= 10 ** $p2;\n\t\n\tprint qw( = > < )[ $x1 <=> $x2 ];\n\t}"}], "negative_code": [], "src_uid": "a4eeaf7252b9115b67b9eca5f2bf621d"} {"nl": {"description": "Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1,\u20092,\u2009...,\u2009n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari?You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem", "input_spec": "The first line contains two integers n and k (1\u2009\u2264\u2009n\u2009\u2264\u20091000;\u00a02\u2009\u2264\u2009k\u2009\u2264\u20095). Each of the next k lines contains integers 1,\u20092,\u2009...,\u2009n in some order \u2014 description of the current permutation.", "output_spec": "Print the length of the longest common subsequence.", "sample_inputs": ["4 3\n1 4 2 3\n4 1 2 3\n1 2 4 3"], "sample_outputs": ["3"], "notes": "NoteThe answer for the first test sample is subsequence [1, 2, 3]."}, "positive_code": [{"source_code": "#!perl\nuse strict;\nuse warnings;\nmy ($i, $n, $k, $j, $result, $aref, @a, @pos, @dp);\n($n, $k) = split ' ', <>;\nfor $i ( 0 .. $k-1 ) { $a[$i] = [ split ' ', <> ]; }\nfor $i ( 0 .. $k-1 ) {\n $aref = $a[$i];\n for $j ( 0 .. $#{ $aref }) {\n $pos[$i][ $a[$i][$j] ] = $j;\n }\n}\n\n$result = 1;\nfor $i ( 0 .. $n-1 ) {\n my $tmp = recursive($i);\n $result = $tmp if ($tmp > $result);\n}\nprint $result, \"\\n\";\n\n#=======\nsub recursive {\n my ($i, $j, $pos, $result, $x, $y, $mark);\n $pos = shift;\n return $dp[$pos] if ($dp[$pos]);\n return $dp[0] = 1 if ($pos == 0);\n $result = 1;\n $y = $a[0][$pos];\n\n for $i ( 0 .. $pos-1 ) {\n $x = $a[0][$i];\n $mark = 1;\n\n for $j ( 1 .. $k-1 ) {\n if ($pos[$j][$x] > $pos[$j][$y]) {\n $mark = 0;\n last;\n }\n }\n if ($mark) {\n my $tmp = recursive($i) + 1;\n $result = $tmp if ($result < $tmp);\n }\n }\n return $dp[$pos] = $result;\n}\n"}], "negative_code": [{"source_code": "#!perl\nuse strict;\nuse warnings;\nmy ($i, $n, $k, $j, $result, $aref, @a, @pos, @dp);\n($n, $k) = split ' ', <>;\nfor $i ( 1 .. $k ) {\n $a[$i] = [ split ' ', <> ];\n}\nfor $i ( 1 .. $k ) {\n $aref = $a[$i];\n $pos[$i] = ();\n for $j ( 0 .. $#{ $aref }) {\n $pos[$i][ $a[$i][$j] ] = $j;\n }\n}\n\n$result = 0;\nfor $i ( 0 .. $n-1 ) {\n my $tmp = recursive($i);\n $dp[$i] = $tmp;\n $result = $tmp if ($tmp > $result);\n}\nprint $result, \"\\n\";\n\n#=======\nsub recursive {\n my ($i, $j, $pos, $result, $x, $y, $mark);\n $pos = shift;\n return $dp[$pos] if ($dp[$pos]);\n return 1 if ($pos == 0);\n $result = 0;\n $y = $a[1][$pos];\n\n for $i ( 0 .. $pos-1 ) {\n $x = $a[1][$i];\n $mark = 1;\n\n for $j ( 2 .. $k ) {\n if ($pos[$j][$x] > $pos[$j][$y]) {\n $mark = 0;\n last;\n }\n }\n if ($mark) {\n my $tmp = recursive($i) + 1;\n $result = $tmp if ($result < $tmp);\n }\n }\n return $result;\n}\n"}], "src_uid": "de87bb6ffd3c703d8845d4dd301bdbf5"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 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": "sub pr{\n $f=$_[0]+1;\n $s=$_[1]+1;\n $ot=$_[4];\n if($_[2]eq\"R\"&&$_[3]eq\"L\"){\n ($f,$s)=($s,$f);\n }\n print $ot $f.\" \".$s.\"\\n\";\n}\n\nopen(in,\"input.txt\");\n$n=;\n@a=split(\"\",);\nclose(in);\nopen(out,\">output.txt\");\n$file=*out;\n@used;\nunless($n%4==0){\n pr(0,$n-1,$a[0],$a[$n-1],$file);\n $used[0]=++$used[$n-1];\n}\nfor($i=0;$i<$n;$i++){\n unless($used[$i]){\n $used[$i]=++$used[$i+2];\n pr($i,$i+2,$a[$i],$a[$i+2],$file);\n }\n}\nclose(out);\n\n\n\n"}], "negative_code": [{"source_code": "sub pr{\n $f=$_[0]+1;\n $s=$_[1]+1;\n $ot=$_[4];\n if($_[2]eq\"R\"&&$_[3]eq\"L\"){\n ($f,$s)=($s,$f);\n }\n print $ot $f.\" \".$s.\"\\n\";\n}\n\nopen(in,\"input.txt\");\n$n=;\n@a=split(\"\",);\nclose(in);\nopen(out,\">output.txt\");\n$file=*out;\n@used;\nunless($n%4==0){\n pr(0,$n-1,$a[0],$a[$n-1],$file);\n $used[0]=++$used[$n-1];\n}\nfor($i=0;$i<$n/2;$i++){\n unless($used[$i]){\n $used[$i]=++$used[$i+2];\n pr($i,$i+2,$a[$i],$a[$i+2],$file);\n }\n}\nclose(out);\n\n\n\n"}, {"source_code": "sub pr{\n $f=$_[0]+1;\n $s=$_[1]+1;\n if($_[2]eq\"R\"&&$_[3]eq\"L\"){\n ($f,$s)=($s,$f);\n }\n print$f.\" \".$s.\"\\n\";\n}\n\n$n=<>;\n@a=split(\"\",<>);\n@used;\nunless($n%4==0){\n pr(0,$n-1,$a[0],$a[$n-1]);\n $used[0]=++$used[$n-1];\n}\nfor($i=0;$i<$n/2;$i++){\n unless($used[$i]){\n $used[$i]=++$used[$i+2];\n pr($i,$i+2,$a[$i],$a[$i+2]);\n }\n}\n\n\n\n"}], "src_uid": "564664d9cd2ccbccb42574b3881ec5fe"} {"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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @aa = map { $_ - 0 } split(/\\s+/,);\n $#aa = $n - 1;\n \n my @sa = sort { $a <=> $b } @aa;\n my $mv = $sa[0];\n \n if( $mv == 1 ){\n print \"YES\\n\"; next;\n }\n \n my $r = 'YES';\n my $dcnt = 0;\n for(my $i=0;$i<$n;$i++){\n if( $aa[$i] != $sa[$i] ){\n if( ( $aa[$i] % $mv ) != 0 ){\n $r = 'NO';\n }\n $dcnt ++;\n }\n }\n if( $r eq 'YES' and $dcnt == 1 ){\n $r = 'NO';\n }\n print \"$r\\n\";\n \n}\n\nexit(0);\n"}], "negative_code": [], "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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of buttons on the jacket. The second line contains n integers ai (0\u2009\u2264\u2009ai\u2009\u2264\u20091). The number ai\u2009=\u20090 if the i-th button is not fastened. Otherwise ai\u2009=\u20091.", "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": "($n, $_) = <>;\nif ($n > 1) {\n\t$r = (y/0/0/ == 1);\n} else {\n\t$r = /1/;\n}\nprint $r? \"YES\": \"NO\";"}, {"source_code": "<>; print qw(NO YES)[ !/^0$|0.*0|^1( 1)+$/ ] for <>"}], "negative_code": [{"source_code": "print qw(NO YES)[ (<> !~ /^0$|0.*0|^1( 1)+$/) ]"}, {"source_code": "print qw(NO YES)[ 0+ (<> !~ /^0$|0.*0/) ]"}], "src_uid": "7db0b870177e7d7b01da083e21ba35f5"} {"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\u2009\u00d7\u2009n 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\u2009\u00d7\u2009n. 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\u2009\u2264\u2009n\u2009\u2264\u2009500,\u20091\u2009\u2264\u2009k\u2009\u2264\u2009n)\u00a0\u2014 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) = split ' ', <>;\n\n$u = 0;\n$sum = 0;\n\nfor $i (1 .. $k - 1){\n\tfor $j (1 .. $n){\n\t\t$_[ $j ] .= ' ' . ++ $u;\n\t\t}\n\t}\n\nfor $i (1 .. $n){\n\tfor $j ($k .. $n){\n\t\t$_[ $i ] .= ' ' . ++ $u;\n\t\t$j == $k and $sum += $u;\n\t\t}\n\t}\n\nshift @_;\ns/^ // for @_;\n\nprint $sum . $/;\nprint $_ . $/ for @_;"}], "negative_code": [{"source_code": "($n, $k) = split ' ', <>;\n\n$u = 0;\n\nfor $i (1 .. $k - 1){\n\tfor $j (1 .. $n){\n\t\t$_[ $j ] .= ' ' . ++ $u;\n\t\t}\n\t}\n\nfor $i (1 .. $n){\n\tfor $j ($k .. $n){\n\t\t$_[ $i ] .= ' ' . ++ $u;\n\t\t}\n\t}\n\nshift @_;\ns/^ // for @_;\n\nprint $_ . $/ for @_;"}], "src_uid": "272f66f3c71c4997c5a1f0f009c9b99e"} {"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\u00a0(1\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105), t\u00a0(0\u2009\u2264\u2009t\u2009\u2264\u2009109) and c\u00a0(1\u2009\u2264\u2009c\u2009\u2264\u2009n). 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 \u2014 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": "use strict;\nmy ($n,$t,$c) = split (' ',<>);\nmy @mi = split (' ',<>);\nmy $temp = 0;\nmy $ans = 0;\nfor my $h (@mi)\n{\n if($h > $t) \n {\n $ans += ($temp - ($c-1)) if ($temp - ($c-1))>0;\n $temp = 0;\n }\n else\n {\n $temp++;\n } \n}\n$ans += ($temp - ($c-1)) if ($temp != 0 and ($temp - ($c-1))>0);\nprint $ans;"}, {"source_code": "#!perl\n\nuse strict;\nuse warnings;\n\nmy ($n,$t,$c) = split /\\s+/ , ;\nmy @serve = split /\\s+/ , ;\n\nmy %overflag;\nmy $i;\nfor($i=0;$i<$n;$i++){\n $overflag{$i}=1 if($serve[$i]>$t) ;\n}\n\n$overflag{$n} = 1;\nmy $thisgood=0;\nmy $lastgood=0;\nmy $count=0;\n\nforeach(sort {$a<=>$b} keys %overflag ){\n $thisgood = $_ - 1 ; \n if ( $thisgood < $lastgood ){\n $lastgood = $_ + 1;\n next ;\n }\n \n \n my $temp = $thisgood - $lastgood + 1 ;\n $count += $temp >= $c ? $temp -$c + 1 : 0;\n $lastgood = $_ + 1;\n\n}\n\nprintf \"%d\\n\" , $count ;"}], "negative_code": [{"source_code": "use strict;\nmy ($n,$t,$c) = split (' ',<>);\nmy @mi = split (' ',<>);\nmy $temp = 0;\nmy $ans = 0;\nfor my $h (@mi)\n{\n if($h > $t) \n {\n $ans += ($temp - ($c-1));\n $temp = 0;\n }\n else\n {\n $temp++;\n } \n}\n$ans += ($temp - ($c-1)) unless $temp == 0;\nprint $ans;"}, {"source_code": "#!perl\n\nuse strict;\nuse warnings;\n\nmy ($n,$t,$c) = split /\\s+/ , ;\nmy @serve = split /\\s+/ , ;\nmy %overflag;\nmy %badflag;\nmy ($i,$count);\nfor($i=0;$i<$n;$i++){\n $overflag{$i}++ if($serve[$i]>$t) ;\n}\nforeach(keys %overflag){\n for($i=$_-$c +1; $i<= $_ ; $i++){\n $badflag{$i} = 1 ;\n }\n}\n$count = $n - $c + 1 - scalar keys %badflag;\n\nprintf \"%d\\n\" , $count ;"}, {"source_code": "#!perl\n\nuse strict;\nuse warnings;\n\nmy ($n,$t,$c) = split /\\s+/ , ;\nmy @serve = split /\\s+/ , ;\n\nmy %overflag;\nmy $i;\nfor($i=0;$i<$n;$i++){\n $overflag{$i}=1 if($serve[$i]>$t) ;\n}\n\n$overflag{$n+$c-2} = 1;\nmy $thisgood=0;\nmy $lastgood=0;\nmy $count=0;\n\nforeach(sort {$a<=>$b} keys %overflag ){\n $thisgood = $_ - $c + 1; \n if ( $thisgood < $lastgood || $lastgood == $_ ){\n $lastgood = $_ + 1;\n next ;\n }\n \n \n my $temp = $thisgood - $lastgood + 1 ;\n $count += $temp >= $c ? $temp -$c + 1 : 0;\n $lastgood = $_ + 1;\n\n}\n\nprintf \"%d\\n\" , $count ;"}, {"source_code": "#!perl\n\nuse strict;\nuse warnings;\n\nmy ($n,$t,$c) = split /\\s+/ , ;\nmy @serve = split /\\s+/ , ;\n\nmy %overflag;\nmy $i;\nfor($i=0;$i<$n;$i++){\n\t$overflag{$i}=1 if($serve[$i]>$t) ;\n}\n\n$overflag{$n+$c-2} = 1;\nmy $thisgood=0;\nmy $lastgood=0;\nmy $count=0;\n\nforeach(sort {$a<=>$b} keys %overflag ){\n\t$thisgood = $_ - $c + 1; \n\tif ( $thisgood < $lastgood || $thisgood==$_){\n\t\t$lastgood = $_ + 1;\n\t\tnext ;\n\t}\n\t\n\t\n\tmy $temp = $thisgood - $lastgood + 1 ;\n\t$count += $temp >= $c ? $temp -$c + 1 : 0;\n\t$lastgood = $_ + 1;\n\n}\n\nprintf \"%d\\n\" , $count ;"}], "src_uid": "7d1e8769a6b1d5c6680ab530d19e7fa4"} {"nl": {"description": "Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression:(1n\u2009+\u20092n\u2009+\u20093n\u2009+\u20094n)\u00a0mod\u00a05for 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\u2009\u2264\u2009n\u2009\u2264\u200910105). 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\u00a0mod\u00a0y means taking remainder after division x by y.Note to the first sample:"}, "positive_code": [{"source_code": "use v5.10;\n\nchomp($line = <>);\n@a = (0, split //, $line);\n$b = 10*$a[$#a-1] + $a[$#a];\nsay($b%4==0 ? 4:0);\n"}, {"source_code": "my $n=<>;\nchomp($n);\nmy $ans=0;\nmy $d=substr($n,-2);\n$A{1}=0;\n$A{2}=0;\n$A{3}=0;\n$A{0}=4;\nprint $A{$d%4} . \"\\n\";"}, {"source_code": "$_=<>;\nchomp;\n$_ or print 4;\n$_ or exit;\n$b=chop;\n$a=chop;\n$ab=$a.$b;\n$q= $ab % 4;\n\nif ($q==1){\n\t$s=1+2+3+4\n\t}\nelsif ($q==2){\n\t$s=1+4+9+6\n\t}\nelsif ($q==3){\n\t$s=1+8+7+4\n\t}\nelsif ($q==0){\n\t$s=1+6+1+6\n\t}\nprint $s % 5"}, {"source_code": "print <>=~/.?.$/ && $& % 4 % 5 ? 0:4"}], "negative_code": [{"source_code": "print<>%4?0:4"}], "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$$$)\u00a0\u2014 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": "for (1..<>) {\r\n chomp (my $n = <>);\r\n chomp (my $in = <>);\r\n @a = split //, $in;\r\n my $i = 1;\r\n my %h;\r\n my $flag = 1;\r\n foreach (@a) {\r\n if (exists ($h{$_})) {\r\n if ($i - $h{$_} == 1) {\r\n $h{$_}++;\r\n } else {\r\n $flag = 0;\r\n last;\r\n }\r\n } else {\r\n $h{$_} = $i;\r\n }\r\n $i++;\r\n }\r\n print $flag? \"YES\\n\" : \"NO\\n\";\r\n}\r\n\r\nexit;"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 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": "#!/usr/bin/perl\n\nmy $sum = <>;\nmy @sms_hist;\n\n$sms_hist[$#sms_hist+1] = <> for (1..$sum);\nmap {chomp} @sms_hist;\n\nmy $uniq_hash = {};\nmy @chat_list;\n\nfor (reverse @sms_hist) {\n\tpush @chat_list, $_ unless ( $uniq_hash->{$_} );\n\t$uniq_hash->{$_} = 1;\n}\n\nprint join \"\\n\", @chat_list;"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 2.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 02:36:23 PM\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n\nwhile (<>) {\n my $num = $_;\n my $line;\n my @names;\n while ($_ --) {\n chomp($line = <>);\n push @names, $line;\n }\n my $hash = {};\n for (reverse @names) {\n print $_, \"\\n\" unless $hash->{$_};\n $hash->{$_} = 1;\n }\n}\nexit 0;\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 2.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 02:36:23 PM\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n\nwhile (<>) {\n my $num = $_;\n my $line;\n my @names;\n chomp($names[$#names+1] = <>) for (1..$num);\n my $hash;\n for (reverse @names) {\n print $_, \"\\n\" unless $hash->{$_};\n $hash->{$_} = 1;\n }\n}\nexit 0;\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 2.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 02:36:23 PM\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n\nwhile (<>) {\n my $num = $_;\n my @names;\n my $name;\n for (1..$num) {\n chomp($name = <>);\n push @names, $name;\n }\n my $hash;\n for (reverse @names) {\n print $_, \"\\n\" unless $hash->{$_};\n $hash->{$_} = 1;\n }\n}\nexit 0;\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 2.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 02:36:23 PM\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n\nwhile (<>) {\n my $num = $_;\n my $line;\n my @names;\n for (1..$num) {\n chomp($name = <>);\n push @names, $name;\n }\n my $hash;\n for (reverse @names) {\n print $_, \"\\n\" unless $hash->{$_};\n $hash->{$_} = 1;\n }\n}\nexit 0;\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 2.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 02:36:23 PM\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n\nwhile (<>) {\n chomp;\n my $num = $_;\n my $line;\n my @names;\n while ($_ --) {\n chomp($line = <>);\n push @names, $line;\n }\n @names = reverse @names;\n my %hash;\n for (@names) {\n print $_, \"\\n\" unless $hash{$_};\n $hash{$_} = 1;\n }\n}\nexit 0;\n"}], "negative_code": [], "src_uid": "18f4d9262150294b63d99803250ed49c"} {"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$$$) \u2014 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)$$$ \u2014 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 \u2014 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 \u2014 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 \u2014 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": "sub max {\n my ($a, $b) = @_;\n if (int($a) < int($b)) {\n return $b;\n }\n return $a;\n}\n\nsub main {\n my $n = int();\n my $hs = ;\n my $ans = $n;\n if ($hs != \"0\" x $n) {\n my $b = $n - index $hs, \"1\";\n my $e = (rindex $hs, \"1\") + 1;\n $ans = 2 * max($b, $e);\n }\n print \"$ans\\n\";\n}\n\nmy $t = ;\nwhile ($t--) {\n main();\n}\n\n"}], "negative_code": [{"source_code": "sub max {\n my ($a, $b) = @_;\n if (int($a) < int($b)) {\n return $b;\n }\n return $a;\n}\n\nsub main {\n my $n = int();\n my $hs = ;\n my $ans = $n;\n for (local $i = 0; $i < $n; $i++) {\n my $sym = substr $hs, $i, 1;\n if ($sym == \"1\") {\n $ans = max($ans, 2 * max($i, $n - $i));\n }\n }\n print \"$ans\\n\";\n}\n\nmy $t = ;\nwhile ($t--) {\n main();\n}\n\n"}], "src_uid": "23575500451a061ed498468f3814c38a"} {"nl": {"description": "Special Agent Smart Beaver works in a secret research department of ABBYY. He's been working there for a long time and is satisfied with his job, as it allows him to eat out in the best restaurants and order the most expensive and exotic wood types there. The content special agent has got an important task: to get the latest research by British scientists on the English Language. These developments are encoded and stored in a large safe. The Beaver's teeth are strong enough, so the authorities assured that upon arriving at the place the beaver won't have any problems with opening the safe.And he finishes his aspen sprig and leaves for this important task. Of course, the Beaver arrived at the location without any problems, but alas. He can't open the safe with his strong and big teeth. At this point, the Smart Beaver get a call from the headquarters and learns that opening the safe with the teeth is not necessary, as a reliable source has sent the following information: the safe code consists of digits and has no leading zeroes. There also is a special hint, which can be used to open the safe. The hint is string s with the following structure: if si = \"?\", then the digit that goes i-th in the safe code can be anything (between 0 to 9, inclusively); if si is a digit (between 0 to 9, inclusively), then it means that there is digit si on position i in code; if the string contains letters from \"A\" to \"J\", then all positions with the same letters must contain the same digits and the positions with distinct letters must contain distinct digits. The length of the safe code coincides with the length of the hint. For example, hint \"?JGJ9\" has such matching safe code variants: \"51919\", \"55959\", \"12329\", \"93539\" and so on, and has wrong variants such as: \"56669\", \"00111\", \"03539\" and \"13666\".After receiving such information, the authorities change the plan and ask the special agents to work quietly and gently and not to try to open the safe by mechanical means, and try to find the password using the given hint.At a special agent school the Smart Beaver was the fastest in his platoon finding codes for such safes, but now he is not in that shape: the years take their toll ... Help him to determine the number of possible variants of the code to the safe, matching the given hint. After receiving this information, and knowing his own speed of entering codes, the Smart Beaver will be able to determine whether he will have time for tonight's show \"Beavers are on the trail\" on his favorite TV channel, or he should work for a sleepless night...", "input_spec": "The first line contains string s \u2014 the hint to the safe code. String s consists of the following characters: ?, 0-9, A-J. It is guaranteed that the first character of string s doesn't equal to character 0. The input limits for scoring 30 points are (subproblem A1): 1\u2009\u2264\u2009|s|\u2009\u2264\u20095. The input limits for scoring 100 points are (subproblems A1+A2): 1\u2009\u2264\u2009|s|\u2009\u2264\u2009105. Here |s| means the length of string s.", "output_spec": "Print the number of codes that match the given hint.", "sample_inputs": ["AJ", "1?AA"], "sample_outputs": ["81", "100"], "notes": null}, "positive_code": [{"source_code": "use bignum;\nchomp($_=<>);\n@_=split//;\nfor (@_){\nif (/\\?/){$k++}\nelsif (/[A-J]/){$a[ord($&)]++}\n}\n$_ and $f++ for @a;\n$n=10;\n$c=1;\n$c*=$n-- for (1..$f);\n$c*=10**$k;\nif ($_[0]!~/^\\d/){\n$c*=9;\n$c/=10;\n}\nprint $c"}, {"source_code": "use bignum;\n$_=<>;\ns/[a-j]/$a[ord($&)]++?$&:$&/ige;\ns/\\?/$k++?$&:$&/ge;\n$_ and $f++ for @a;\n$n=10;\n$c=1;\n$c*=$n-- for (1..$f);\n$c*=10**$k;\nif (!/^\\d/){\n$c*=9;\n$c/=10;\n}\nprint $c"}], "negative_code": [{"source_code": "use bignum;\n$_=<>;\ns/[a-j]/$a[$&]++?$&:$&/ige;\ns/\\?/$k++?$&:$&/ge;\n$_ and $f++ for @a;\n$n=10;\n$c=1;\n$c*=$n-- for (1..$f);\n$c*=10**$k;\nif (!/^\\d/){\n$c*=9;\n$c/=10;\n}\nprint $c"}], "src_uid": "d3c10d1b1a17ad018359e2dab80d2b82"} {"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.\u2009e. 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$$$) \u2014 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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @x = map { $_ - 0 } split(/\\s+/,);\r\n my $d = $x[$n-1] - $x[0];\r\n if( $d <= ( $n-1 )+2 ){\r\n print \"YES\\n\";\r\n } else {\r\n print \"NO\\n\";\r\n }\r\n}\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "negative_code": [], "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\u00a0\u2014 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": "for(1..<>){($n,$k)=split/ /,<>;@a=split/ /,<>;$q=0;$q+=$k<@a[$_-1]for(1..$k);print$q,\" \"}"}], "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\u2009=\u20095 the handkerchief pattern should look like that: \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00\u00a01\u00a00\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00\u00a01\u00a02\u00a01\u00a00\u00a0\u00a0\u00a0\u00a00\u00a01\u00a02\u00a03\u00a02\u00a01\u00a00\u00a0\u00a00\u00a01\u00a02\u00a03\u00a04\u00a03\u00a02\u00a01\u00a000\u00a01\u00a02\u00a03\u00a04\u00a05\u00a04\u00a03\u00a02\u00a01\u00a00\u00a0\u00a00\u00a01\u00a02\u00a03\u00a04\u00a03\u00a02\u00a01\u00a00\u00a0\u00a0\u00a0\u00a00\u00a01\u00a02\u00a03\u00a02\u00a01\u00a00\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00\u00a01\u00a02\u00a01\u00a00\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00\u00a01\u00a00\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00Your 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\u2009\u2264\u2009n\u2009\u2264\u20099).", "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": "my $n = int<>;\nmy @arr;\nfor (0..$n) {\n @arr = (0..$_, reverse 0..$_-1);\n print ' ' x (($n - $_) * 2), \"@arr\\n\";\n}\nfor (my $i = $n - 1; $i >=0; $i--) {\n @arr = (0..$i, reverse 0..$i-1);\n print ' ' x (($n - $i) * 2), \"@arr\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\nfor (my ($i,$k) = (0, $n+$n); $i<$n; ++$i,$k-=2) {\n\t$line = '';\n\t$line .= ' ' x $k;\n\tfor (my $j=0; $j<$i; $j++) {\n\t\t$line .= \"$j \";\n\t}\n\tfor (my $j=$i; $j>0; $j--) {\n\t\t$line .= \"$j \";\n\t}\n\t$line .= '0';\n\tsay $line;\n}\nfor (my ($i,$k) = ($n, 0); $i>=0; --$i,$k+=2) {\n\t$line = '';\n\t$line .= ' ' x $k;\n\tfor (my $j=0; $j<$i; $j++) {\n\t\t$line .= \"$j \";\n\t}\n\tfor (my $j=$i; $j>0; $j--) {\n\t\t$line .= \"$j \";\n\t}\n\t$line .= '0';\n\tsay $line;\n}\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nsub doit {\n\tmy $n = shift;\n\tmy $m = shift;\n print ' ' x (2 * ($n - $m));\n print join ' ', ((0..$m), reverse (0..($m - 1)));\n print \"\\n\";\n}\n\nmy $n = <>;\n\nfor my $m (0..$n) {\n\tdoit($n, $m);\n}\nfor my $m (reverse (0..($n - 1))) {\n\tdoit($n, $m);\n}"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nmy $n = int <>;\nmy $s;\nmy $field = \"|\" x ($n * 4 + 1);\nmy $format = qq(\nformat =\n\\@$field\n\\$s\n.\n);\n\neval $format;\nfor my $row (0 .. $n) {\n$s = '';\nmap { $s .= \"$_ \" } (0 .. $row);\nmap { $s .= $row-$_ . ' ' } ( 1 .. $row );\nchop $s;\nwrite; \n}\n\nfor my $row (0 .. $n-1) {\n$s = '';\nmap { $s .= \"$_ \" } (0 .. $n-1 - $row);\nmap { $s .= $n-1 - $row - $_ . ' ' } (1 .. $n-1 - $row);\nchop $s;\nwrite;\n}\n"}, {"source_code": "#!perl\n\n$n = ;\nfor $i (0..$n) {\n\t@arr = ((0..$i), reverse (0..$i-1));\n\tprint ' ' x (($n - $i)*2), \"@arr\\n\"\n}\n\nfor ($i = $n-1; $i >= 0; $i--) {\n\t@arr = ((0..$i), reverse (0..$i-1));\n\tprint ' ' x (($n - $i)*2), \"@arr\\n\"\n}"}, {"source_code": "$_=<>;\n@_=((0..$_),(reverse(0..$_-1)));\n$_=join\" \",@_;\npush @a, $_;\nwhile(/1/){\ns/0/ /g;\ns/\\d+/$&-1/eg;\npush @a,$_;\nunshift @a,$_;\n}\ns/\\s*$// for @a;\n$,=\"\\n\";\nprint @a;"}], "negative_code": [{"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nmy $n = int <>;\nmy $s;\nmy $field = \"|\" x ($n * 4 + 1);\nmy $format = qq(\nformat =\n\\@$field\n\\$s\n.\n);\n\neval $format;\nfor my $row (0 .. $n) {\n$s = '';\nmap { $s .= \"$_ \" } (0 .. $row);\nmap { $s .= $row-$_ . ' ' } ( 1 .. $row );\nchop $s;\nwrite; \n}\n\nfor my $row (0 .. $n-1) {\n$s = '';\nmap { $s .= \"$_ \" } (0 .. $n-1 - $row);\nmap { $s .= $n-1 - $row . ' ' } (1 .. $n-1 - $row);\nchop $s;\nwrite;\n}\n"}, {"source_code": "$_=<>;\n@_=((0..$_),(reverse(0..$_-1)));\n$_=join\" \",@_;\npush @a, $_;\nwhile(/1/){\ns/0/ /g;\ns/\\d+/$&-1/eg;\npush @a,$_;\nunshift @a,$_;\n}\n$,=\",\";\nprint @a;"}, {"source_code": "$_=<>;\n@_=((0..$_),(reverse(0..$_-1)));\n$_=join\" \",@_;\npush @a, $_;\nwhile(/1/){\ns/0/ /g;\ns/\\d+/$&-1/eg;\npush @a,$_;\nunshift @a,$_;\n}\n$,=\"\\n\";\nprint @a;"}], "src_uid": "7896740b6f35010af751d3261b5ef718"} {"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\u2009+\u20091. Reaching a certain rank i having not reached all the previous i\u2009-\u20091 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\u2009\u2264\u2009n\u2009\u2264\u2009100). The second line contains n\u2009-\u20091 integers di (1\u2009\u2264\u2009di\u2009\u2264\u2009100). The third input line contains two integers a and b (1\u2009\u2264\u2009a\u2009<\u2009b\u2009\u2264\u2009n). 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": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@d = split / /, <>;\n($a, $b) = split / /, <>;\n($a, $b, $ans) = ($a-1, $b-1, 0);\n$ans+=$d[$_] foreach ($a .. $b-1);\nsay $ans;"}, {"source_code": "<>;\n@d = split(/ /, <>);\n($a, $b) = split(/ /, <>);\nfor($i=$a-1;$i<$b-1;$i++)\n{\n $sum += $d[$i];\n}\nprint(\"$sum\\n\");\n"}, {"source_code": "while (<>){\n @_=split/ /,<>;\n <>=~/ /;\n $sum=0;\n for ($`..$'-1){\n $sum+=$_[$_-1]\n }\n print \"$sum\\n\"\n }"}], "negative_code": [], "src_uid": "69850c2af99d60711bcff5870575e15e"} {"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 two pieces so that the sum of numbers from one piece is equal to the sum of numbers from the 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 amount of squares in the stripe. The second line contains n space-separated numbers \u2014 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 two non-empty pieces so that the sum of numbers from one piece is equal to the sum of numbers from the other piece. Don't forget that it's allowed to cut the stripe along the squares' borders only.", "sample_inputs": ["9\n1 5 -6 7 9 -16 0 -2 2", "3\n1 1 1", "2\n0 0"], "sample_outputs": ["3", "0", "1"], "notes": null}, "positive_code": [{"source_code": "\n\n\n\nmy $n = ;\nchomp $f;\n\nmy @arr = ();\n$f = ;\nchomp ( $f );\n@arr = split m/ /, $f;\n\nfor ( my $i = 1; $i < (scalar @arr); $i += 1 ) {\n $arr[ $i ] += $arr[ $i-1 ];\n}\n\nmy $cnt = 0;\nmy $total = $arr[ $n-1 ];\nfor ( my $i = 0; $i < (scalar @arr)-1; $i += 1 ) {\n if ( $arr[ $n-1 ] - $arr[ $i ] == $arr[ $i ] ) {\n\t$cnt += 1;\n }\n}\n\nprint $cnt;\nprint \"\\n\";\n\n\n\n\n\n\n"}], "negative_code": [], "src_uid": "5358fff5b798ac5e500d0f5deef765c7"} {"nl": {"description": "Bajtek, known for his unusual gifts, recently got an integer array $$$x_0, x_1, \\ldots, x_{k-1}$$$.Unfortunately, after a huge array-party with his extraordinary friends, he realized that he'd lost it. After hours spent on searching for a new toy, Bajtek found on the arrays producer's website another array $$$a$$$ of length $$$n + 1$$$. As a formal description of $$$a$$$ says, $$$a_0 = 0$$$ and for all other $$$i$$$\u00a0($$$1 \\le i \\le n$$$) $$$a_i = x_{(i-1)\\bmod k} + a_{i-1}$$$, where $$$p \\bmod q$$$ denotes the remainder of division $$$p$$$ by $$$q$$$.For example, if the $$$x = [1, 2, 3]$$$ and $$$n = 5$$$, then: $$$a_0 = 0$$$, $$$a_1 = x_{0\\bmod 3}+a_0=x_0+0=1$$$, $$$a_2 = x_{1\\bmod 3}+a_1=x_1+1=3$$$, $$$a_3 = x_{2\\bmod 3}+a_2=x_2+3=6$$$, $$$a_4 = x_{3\\bmod 3}+a_3=x_0+6=7$$$, $$$a_5 = x_{4\\bmod 3}+a_4=x_1+7=9$$$. So, if the $$$x = [1, 2, 3]$$$ and $$$n = 5$$$, then $$$a = [0, 1, 3, 6, 7, 9]$$$.Now the boy hopes that he will be able to restore $$$x$$$ from $$$a$$$! Knowing that $$$1 \\le k \\le n$$$, help him and find all possible values of $$$k$$$\u00a0\u2014 possible lengths of the lost array.", "input_spec": "The first line contains exactly one integer $$$n$$$ ($$$1 \\le n \\le 1000$$$)\u00a0\u2014 the length of the array $$$a$$$, excluding the element $$$a_0$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^6$$$). Note that $$$a_0$$$ is always $$$0$$$ and is not given in the input.", "output_spec": "The first line of the output should contain one integer $$$l$$$ denoting the number of correct lengths of the lost array. The second line of the output should contain $$$l$$$ integers\u00a0\u2014 possible lengths of the lost array in increasing order.", "sample_inputs": ["5\n1 2 3 4 5", "5\n1 3 5 6 8", "3\n1 5 3"], "sample_outputs": ["5\n1 2 3 4 5", "2\n3 5", "1\n3"], "notes": "NoteIn the first example, any $$$k$$$ is suitable, since $$$a$$$ is an arithmetic progression.Possible arrays $$$x$$$: $$$[1]$$$ $$$[1, 1]$$$ $$$[1, 1, 1]$$$ $$$[1, 1, 1, 1]$$$ $$$[1, 1, 1, 1, 1]$$$In the second example, Bajtek's array can have three or five elements.Possible arrays $$$x$$$: $$$[1, 2, 2]$$$ $$$[1, 2, 2, 1, 2]$$$For example, $$$k = 4$$$ is bad, since it leads to $$$6 + x_0 = 8$$$ and $$$0 + x_0 = 1$$$, which is an obvious contradiction.In the third example, only $$$k = n$$$ is good.Array $$$[1, 4, -2]$$$ satisfies the requirements.Note that $$$x_i$$$ may be negative."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t@_ = ( 0, split ' ', <> );\n\t\n\t$debug and print '-' x 15;\n\t\n\tmy @A;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tpush @A, $_[ $i + 1 ] - $_[ $i ];\n\t\t}\n\t\n\t$debug and print \"@A\";\n\t\n\tmy $cnt = 0;\n\tmy @cnt;\n\t\n\tmy %h;\n\t\n\tfor my $k ( 1 .. @A ){\n\t\t\n\t\tif( $h{ $k } ){\n\t\t\tpush @cnt, $k;\n\t\t\t$cnt ++;\n\t\t\tnext;\n\t\t\t};\n\t\t\n\t\tmy $ok = 1;\n\t\tfor my $i ( 0 .. @A - 1 ){\n\t\t\t$ok &&= $A[ $i ] == $A[ $i % $k ];\n\t\t\t}\n\t\t\n\t\tif( $ok ){\n\t\t\t$cnt ++;\n\t\t\tpush @cnt, $k;\n\t\t\t\n\t\t\tmy $kk = $k * 2;\n\t\t\twhile( $kk < @A ){\n\t\t\t\t$h{ $kk } ++;\n\t\t\t\t$kk += $k;\n\t\t\t\t}\n\t\t\t}\t\n\t\t}\n\t\n\tprint $cnt;\n\tprint \"@cnt\";\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = ( 0, split ' ', <> );\n\t\n\tmy @A;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\tpush @A, $_[ $i + 1 ] - $_[ $i ];\n\t\t}\n\t\n#%\tprint \"@A\";\n\t\n\tmy $cnt = 0;\n\t\n\tmy %h;\n\t\n\tfor my $k ( 1 .. @A ){\n\t\t\n\t\tnext if $h{ $k };\n\t\t\n\t\tmy $ok = 1;\n\t\tfor my $i ( 0 .. @A - 1 ){\n\t\t\t$ok &&= $A[ $i ] == $A[ $i % $k ];\n\t\t\t}\n\t\t\n\t\tif( $ok ){\n\t\t\t$cnt ++;\n\t\t\t\n\t\t\tmy $kk = $k * 2;\n\t\t\twhile( $kk < @A ){\n\t\t\t\t$h{ $kk } ++;\n\t\t\t\t$cnt ++;\n\t\t\t\t$kk += $k;\n\t\t\t\t}\n\t\t\t}\t\n\t\t}\n\t\n\tprint $cnt;\n\t}"}], "src_uid": "fd2227498f1a0f4042673382a3c71c85"} {"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": "use 5.020;\n\nmy $s = <>;\nmy $t = <>;\n\n$s =~ s/[aeiou]/a/g;\n$s =~ s/[^aeiou]/b/g;\n$t =~ s/[aeiou]/a/g;\n$t =~ s/[^aeiou]/b/g;\n\nsay($s eq $t ? \"Yes\" : \"No\");\n"}], "negative_code": [], "src_uid": "2b346d5a578031de4d19edb4f8f2626c"} {"nl": {"description": "You are given a tree consisting of $$$n$$$ vertices. Some of the vertices (at least two) are black, all the other vertices are white.You place a chip on one of the vertices of the tree, and then perform the following operations: let the current vertex where the chip is located is $$$x$$$. You choose a black vertex $$$y$$$, and then move the chip along the first edge on the simple path from $$$x$$$ to $$$y$$$. You are not allowed to choose the same black vertex $$$y$$$ in two operations in a row (i.\u2009e., for every two consecutive operations, the chosen black vertex should be different).You end your operations when the chip moves to the black vertex (if it is initially placed in a black vertex, you don't perform the operations at all), or when the number of performed operations exceeds $$$100^{500}$$$.For every vertex $$$i$$$, you have to determine if there exists a (possibly empty) sequence of operations that moves the chip to some black vertex, if the chip is initially placed on the vertex $$$i$$$.", "input_spec": "The first line contains one integer $$$n$$$ ($$$3 \\le n \\le 3 \\cdot 10^5$$$) \u2014 the number of vertices in the tree. The second line contains $$$n$$$ integers $$$c_1, c_2, \\dots, c_n$$$ ($$$0 \\le c_i \\le 1$$$), where $$$c_i = 0$$$ means that the $$$i$$$-th vertex is white, and $$$c_i = 1$$$ means that the $$$i$$$-th vertex is black. At least two values of $$$c_i$$$ are equal to $$$1$$$. Then $$$n-1$$$ lines follow, each of them contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \\le u_i, v_i \\le n$$$; $$$u_i \\ne v_i$$$) \u2014 the endpoints of some edge. These edges form a tree.", "output_spec": "Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to $$$1$$$ if there exists a (possibly empty) sequence of operations that moves the chip to some black vertex if it is placed on the vertex $$$i$$$, and $$$0$$$ if no such sequence of operations exists.", "sample_inputs": ["8\n0 1 0 0 0 0 1 0\n8 6\n2 5\n7 8\n6 5\n4 5\n6 1\n7 3"], "sample_outputs": ["0 1 1 1 1 0 1 1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy @c = split ' ', <>;\n\t\n\tmy $black = grep $_ == 1, @c;\n\t\n\tmy %h;\n\tmy %g;\n\t\n\tfor my $i ( 1 .. $_ - 1 ){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\t$h{ $u }{ $v } = 1;\n\t\t$h{ $v }{ $u } = 1;\n\t\t}\n\t\n\tmy @leaf = grep { 1 == keys %{ $h{ $_ } } } grep { !$c[ $_ - 1 ] } keys %h;\n\t\n\twhile( @leaf ){\n\t\tmy $leaf = shift @leaf;\n\t\t\n\t\tmy( $next ) = keys %{ $h{ $leaf } };\n\t\t\n\t\tdelete $h{ $leaf };\n\t\tdelete $h{ $next }{ $leaf };\n\t\t\n\t\t$g{ $leaf }{ $next } = 1;\n\t\t$g{ $next }{ $leaf } = 1;\n\t\t\n\t\tnext if 1 < keys %{ $h{ $next } };\n\t\tnext if $c[ $next - 1 ];\n\t\t\n\t\tpush @leaf, $next;\n\t\t}\n\t\n\t@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;\n\t\n\twhile( @leaf ){\n\t\tmy $leaf = shift @leaf;\n\t\t\n\t\tmy( $next ) = keys %{ $h{ $leaf } };\n\t\t\n\t\tif( $c[ $next - 1 ] == 0 ){\n\t\t\t$c[ $next - 1 ] = 1;\n\t\t\tdelete $h{ $leaf };\n\t\t\tdelete $h{ $next }{ $leaf };\n\t\t\t}\n\t\t}\n\t\n\t@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;\n\t\n\tif( @leaf != $black ){\n\t\tprint join ' ', ( 1 ) x @c;\n\t\tnext;\n\t\t}\n\t\n\tmy @bfs = grep { 1 != keys %{ $h{ $_ } } } keys %h;\n\t\n\twhile( @bfs ){\n\t\tmy $u = shift @bfs;\n\t\t\n\t\tfor my $v ( keys %{ $g{ $u } } ){\n\t\t\tif( $c[ $v - 1 ] == 0 ){\n\t\t\t\tpush @bfs, $v;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t$c[ $u - 1 ] = 3;\n\t\t}\n\t\n\tprint \"@c\" =~ y/03/10/r;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy @c = split ' ', <>;\n\t\n\tmy $black = grep $_ == 1, @c;\n\t\n\tmy %h;\n\tmy %g;\n\tmy %cnt;\n\t\n\tfor my $i ( 1 .. $_ - 1 ){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\t$h{ $u }{ $v } = 1;\n\t\t$h{ $v }{ $u } = 1;\n\t\t\n\t\t$cnt{ $u } ++;\n\t\t$cnt{ $v } ++;\n\t\t}\n\t\n\tmy @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == $cnt{ $_ } } keys %cnt;\n\t\n\t$debug and print \"[@leaf]\";\n\t\n\t$debug and print \"$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t\n\twhile( @leaf ){\n\t\tmy $leaf = shift @leaf;\n\t\t\n\t\tmy( $next ) = keys %{ $h{ $leaf } };\n\t\t\n\t\tdelete $h{ $leaf };\n\t\tdelete $h{ $next }{ $leaf };\n\t\t\n\t\t$g{ $leaf }{ $next } = 1;\n\t\t$g{ $next }{ $leaf } = 1;\n\t\t\n\t\tnext if 1 < keys %{ $h{ $next } };\n\t\tnext if $c[ $next - 1 ];\n\t\t\n\t\tpush @leaf, $next;\n\t\t}\n\t\n\t$debug and print \":$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t\n\t@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;\n\t\n\t$debug and print \"[@leaf]\";\n\t\n\twhile( @leaf ){\n\t\tmy $leaf = shift @leaf;\n\t\t\n\t\tmy( $next ) = keys %{ $h{ $leaf } };\n\t\t\n\t\tif( $c[ $next - 1 ] == 0 ){\n\t\t\t$c[ $next - 1 ] = 1;\n\t\t\tdelete $h{ $leaf };\n\t\t\tdelete $h{ $next }{ $leaf };\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t$debug and print \"[@c]\";\n\t\n\t@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;\n\t\n\tif( @leaf != $black ){\n\t\tprint join ' ', ( 1 ) x @c;\n\t\tnext;\n\t\t}\n\t\n\tmy @bfs = grep { 1 != keys %{ $h{ $_ } } } keys %h;\n\t\n\t$debug and print \"[@bfs]\";\n\t\n\twhile( @bfs ){\n\t\tmy $u = shift @bfs;\n\t\t\n\t\tfor my $v ( keys %{ $g{ $u } } ){\n\t\t\tif( $c[ $v - 1 ] == 0 ){\n\t\t\t\tpush @bfs, $v;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t$c[ $u - 1 ] = 3;\n\t\t}\n\t\n\t$debug and print \"[@c]\";\n\t\n\tprint \"@c\" =~ y/03/10/r;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy @c = split ' ', <>;\n\t\n\tmy $black = grep $_ == 1, @c;\n\t\n\tmy %h;\n\tmy %g;\n\tmy %cnt;\n\t\n\tfor my $i ( 1 .. $_ - 1 ){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\t$h{ $u }{ $v } = 1;\n\t\t$h{ $v }{ $u } = 1;\n\t\t\n\t\t$cnt{ $u } ++;\n\t\t$cnt{ $v } ++;\n\t\t}\n\t\n\tmy @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == $cnt{ $_ } } keys %cnt;\n\t\n\t$debug and print \"[@leaf]\";\n\t\n\t$debug and print \"$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t\n\twhile( @leaf ){\n\t\tmy $leaf = shift @leaf;\n\t\t\n\t\tmy( $next ) = keys %{ $h{ $leaf } };\n\t\t\n\t\tdelete $h{ $leaf };\n\t\tdelete $h{ $next }{ $leaf };\n\t\t\n\t\t$g{ $leaf }{ $next } = 1;\n\t\t$g{ $next }{ $leaf } = 1;\n\t\t\n\t\tnext if 1 < keys %{ $h{ $next } };\n\t\tnext if $c[ $next - 1 ];\n\t\t\n\t\tpush @leaf, $next;\n\t\t}\n\t\n\t$debug and print \":$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t\n\t@leaf = grep { \n\t\t\tmy @keys = keys %{ $h{ $_ } };\n\t\t\t@keys == 1 and \n\t\t\t3 > keys %{ $h{ $keys[ 0 ] } };\n\t\t\t} keys %h;\n\t\n\t$debug and print \"[@leaf]\";\n\t\n\tif( @leaf != $black ){\n\t\tprint join ' ', ( 1 ) x @c;\n\t\tnext;\n\t\t}\n\t\n\twhile( @leaf ){\n\t\tmy $leaf = shift @leaf;\n\t\t\n\t\tif( 0 == keys %{ $h{ $leaf } } ){\n\t\t\t$c[ $leaf - 1 ] = 2;\n\t\t\tdelete $h{ $leaf };\n\t\t\tnext;\n\t\t\t}\n\t\t\n\t\tmy( $next ) = keys %{ $h{ $leaf } };\n\t\t\n\t\tif( $c[ $leaf - 1 ] == 1 ){\n\t\t\tpush @leaf, $next;\n\t\t\t}\n\t\t\n\t\t$c[ $leaf - 1 ] = 2;\n\t\t\n\t\tdelete $h{ $leaf };\n\t\tdelete $h{ $next }{ $leaf };\n\t\t}\n\t\n\t$debug and print \":$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t$debug and print \"[@c]\";\n\t\n\tmy @bfs = keys %h;\n\t\n\tfor( @bfs ){\n\t\t$c[ $_ - 1 ] = 3;\n\t\t}\n\t\n\t$debug and print \"[@c]\";\n\t\n\twhile( @bfs ){\n\t\tmy $u = shift @bfs;\n\t\t\n\t\tfor my $v ( keys %{ $g{ $u } } ){\n\t\t\tif( $c[ $v - 1 ] == 0 ){\n\t\t\t\tpush @bfs, $v;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t$c[ $u - 1 ] = 3;\n\t\t}\n\t\n\t$debug and print \"[@c]\";\n\t\n\tprint \"@c\" =~ y/023/110/r;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy @c = split ' ', <>;\n\t\n\tmy $black = grep $_ == 1, @c;\n\t\n\tmy %h;\n\tmy %g;\n\tmy %cnt;\n\t\n\tfor my $i ( 1 .. $_ - 1 ){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\t$h{ $u }{ $v } = 1;\n\t\t$h{ $v }{ $u } = 1;\n\t\t\n\t\t$cnt{ $u } ++;\n\t\t$cnt{ $v } ++;\n\t\t}\n\t\n\tmy @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == $cnt{ $_ } } keys %cnt;\n\t\n\t$debug and print \"[@leaf]\";\n\t\n\t$debug and print \"$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t\n\twhile( @leaf ){\n\t\tmy $leaf = shift @leaf;\n\t\t\n\t\tmy( $next ) = keys %{ $h{ $leaf } };\n\t\t\n\t\tdelete $h{ $leaf };\n\t\tdelete $h{ $next }{ $leaf };\n\t\t\n\t\t$g{ $leaf }{ $next } = 1;\n\t\t$g{ $next }{ $leaf } = 1;\n\t\t\n\t\tnext if 1 < keys %{ $h{ $next } };\n\t\tnext if $c[ $next - 1 ];\n\t\t\n\t\tpush @leaf, $next;\n\t\t}\n\t\n\t$debug and print \":$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t\n\t@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;\n\t\n\t$debug and print \"[@leaf]\";\n\t\n\tif( @leaf != $black ){\n\t\tprint join ' ', ( 1 ) x @c;\n\t\tnext;\n\t\t}\n\t\n\twhile( @leaf ){\n\t\tmy $leaf = shift @leaf;\n\t\t\n\t\tif( 0 == keys %{ $h{ $leaf } } ){\n\t\t\t$c[ $leaf - 1 ] = 2;\n\t\t\tdelete $h{ $leaf };\n\t\t\tnext;\n\t\t\t}\n\t\t\n\t\tmy( $next ) = keys %{ $h{ $leaf } };\n\t\t\n\t\tif( $c[ $leaf - 1 ] == 1 ){\n\t\t\tpush @leaf, $next;\n\t\t\t}\n\t\t\n\t\t$c[ $leaf - 1 ] = 2;\n\t\t\n\t\tdelete $h{ $leaf };\n\t\tdelete $h{ $next }{ $leaf };\n\t\t}\n\t\n\t$debug and print \":$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t$debug and print \"[@c]\";\n\t\n\tmy @bfs = keys %h;\n\t\n\tfor( @bfs ){\n\t\t$c[ $_ - 1 ] = 3;\n\t\t}\n\t\n\t$debug and print \"[@c]\";\n\t\n\twhile( @bfs ){\n\t\tmy $u = shift @bfs;\n\t\t\n\t\tfor my $v ( keys %{ $g{ $u } } ){\n\t\t\tif( $c[ $v - 1 ] == 0 ){\n\t\t\t\tpush @bfs, $v;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t$c[ $u - 1 ] = 3;\n\t\t}\n\t\n\t$debug and print \"[@c]\";\n\t\n\tprint \"@c\" =~ y/023/110/r;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nMAIN:\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy @c = split ' ', <>;\n\t\n\tmy @black;\n\t\n\tfor my $i ( 1 .. @c ){\n\t\t$c[ $i - 1 ] and push @black, $i;\n\t\t}\n\t\n\tmy %h;\n\t\n\tfor my $i ( 1 .. $_ - 1 ){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\t$h{ $u }{ $v } = 1;\n\t\t$h{ $v }{ $u } = 1;\n\t\t}\n\t\n\tmy @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == keys %{ $h{ $_ } } } keys %h;\n\t\n\t$debug and print \"[@leaf]\";\n\t\n\t$debug and print \"$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t\n\tmy @color = ( 0 ) x @c;\n\t\n\twhile( @leaf ){\n\t\tmy $leaf = shift @leaf;\n\t\t\n\t\t$color[ $leaf - 1 ] = 1;\n\t\t\n\t\tmy( $next ) = grep { 0 == $color[ $_ - 1 ] } keys %{ $h{ $leaf } };\n\t\t\n\t\tnext if 1 < keys %{ $h{ $next } };\n\t\tnext if $c[ $next - 1 ];\n\t\t\n\t\tpush @leaf, $next;\n\t\t}\n\t\n\t$debug and print \"[@color]\";\n\t\n\tfor my $black ( @black ){\n\t\tif( 1 != grep { !$color[ $_ - 1 ] } keys %{ $h{ $black } } ){\n\t\t\tprint join ' ', ( 1 ) x @c;\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tfor my $black ( @black ){\n\t\t$color[ $_ - 1 ] = 2 for $black, keys %{ $h{ $black } };\n\t\t}\n\t\n\t$debug and print \"[@color]\";\n\t$debug and print \"[@c]\";\n\t\n\tmy @bfs;\n\t\n\tfor my $i ( 1 .. @color ){\n\t\tif( $color[ $i - 1 ] == 0 ){\n\t\t\tpush @bfs, $i;\n\t\t\t}\n\t\t}\n\t\n\twhile( @bfs ){\n\t\tmy $u = shift @bfs;\n\t\t\n\t\t$color[ $u - 1 ] = 0;\n\t\t\n\t\tpush @bfs, grep { $color[ $_ - 1 ] == 1 } keys %{ $h{ $u } };\n\t\t}\n\t\n\t$debug and print \"[@color]\";\n\t\n\tprint \"@color\" =~ y/2/1/r;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy @c = split ' ', <>;\n\t\n\tmy $black = grep $_ == 1, @c;\n\t\n\tmy %h;\n\tmy %g;\n\tmy %cnt;\n\t\n\tfor my $i ( 1 .. $_ - 1 ){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\t$h{ $u }{ $v } = 1;\n\t\t$h{ $v }{ $u } = 1;\n\t\t\n\t\t$g{ $u }{ $v } = 1;\n\t\t$g{ $v }{ $u } = 1;\n\t\t\n\t\t$cnt{ $u } ++;\n\t\t$cnt{ $v } ++;\n\t\t}\n\t\n\tmy @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == $cnt{ $_ } } keys %cnt;\n\t\n\t$debug and print \"[@leaf]\";\n\t\n\t$debug and print \"$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t\n\twhile( @leaf ){\n\t\tmy $leaf = shift @leaf;\n\t\t\n\t\tmy( $next ) = keys %{ $h{ $leaf } };\n\t\t\n\t\tdelete $h{ $leaf };\n\t\tdelete $h{ $next }{ $leaf };\n\t\t\n\t\tnext if 1 < keys %{ $h{ $next } };\n\t\tnext if $c[ $next - 1 ];\n\t\t\n\t\tpush @leaf, $next;\n\t\t}\n\t\n\t$debug and print \":$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t\n\t@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;\n\t\n\t$debug and print \"[@leaf]\";\n\t\n\tif( @leaf != $black ){\n\t\tprint join ' ', ( 1 ) x @c;\n\t\tnext;\n\t\t}\n\t\n\twhile( @leaf ){\n\t\tmy $leaf = shift @leaf;\n\t\t\n\t\tmy( $next ) = keys %{ $h{ $leaf } };\n\t\t\n\t\tif( $c[ $leaf - 1 ] == 1 ){\n\t\t\tpush @leaf, $next;\n\t\t\t}\n\t\t\n\t\t$c[ $leaf - 1 ] = 2;\n\t\t\n\t\tdelete $h{ $leaf };\n\t\tdelete $h{ $next }{ $leaf };\n\t\t}\n\t\n\t$debug and print \":$_ -> \", join ' ', keys %{ $h{ $_ } } for sort keys %h;\n\t$debug and print \"[@c]\";\n\t\n\tmy @bfs = keys %h;\n\t\n\tfor( @bfs ){\n\t\t$c[ $_ - 1 ] = 3;\n\t\t}\n\t\n\t$debug and print \"[@c]\";\n\t\n\twhile( @bfs ){\n\t\tmy $u = shift @bfs;\n\t\t\n\t\tfor my $v ( keys %{ $g{ $u } } ){\n\t\t\tif( $c[ $v - 1 ] == 0 ){\n\t\t\t\tpush @bfs, $v;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t$c[ $u - 1 ] = 3;\n\t\t}\n\t\n\t$debug and print \"[@c]\";\n\t\n\tprint \"@c\" =~ y/023/110/r;\n\t}"}], "src_uid": "2dcc82e6abc733a9c4567742294184dc"} {"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": "#!/usr/bin/env perl\nuse 5.012;\nchomp(my $A = );\nchomp(my $B = );\n$A = reverse $A; $B = reverse $B;\nmy $i = 0;\nfor (0..(length($A) - 1)) {\n last if $_ >= length $B;\n if ((substr $A, $_, 1) eq (substr $B, $_, 1)) {\n ++$i;\n } else {\n last;\n }\n}\nsay length($A) + length($B) - 2 * $i;\n"}], "negative_code": [], "src_uid": "59d926bca19a6dcfe3eb3e3dc03fffd6"} {"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": "#!usr/bin/perl -w\nuse strict;\n\nmy $n=;chomp($n);\nmy $sum=0;\nfor my $i(1..$n){\n my($x,$y)=split/ /,;chomp($y);\n $sum=$x+$y if($sum<=$x+$y);\n}\nprint \"$sum\\n\";"}, {"source_code": "print +( sort { $b <=> $a } map { eval join ' + ', split } grep / /, <> )[ 0 ]"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\t\n\tprint +( sort { $b <=> $a } map { eval join ' + ', split } @_ )[ 0 ];\n\t}"}], "negative_code": [{"source_code": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy $n=;chomp($n);\nmy @x,my @y,my $maxx=0,my $maxy=0;my $p=0,my $q=0;\nfor my $i(0..$n-1){\n ($x[$i],$y[$i])=split/ /,;chomp($y[$i]);\n if($maxx<=$x[$i]&&$maxy<=$y[$i]){\n\t$maxx=$x[$i];$p=$i;\n\t$maxy=$y[$i];$q=$i;\n }\n if($maxx<$x[$i]){\n\t$maxx=$x[$i];$p=$i;\n }\n if($maxy<$y[$i]){\n\t$maxy=$y[$i];$q=$i;\n }\n}\nif($p==$q){\n print 2*$x[$p] if($x[$p]>=$y[$p]);\n print 2*$y[$p] if($x[$p]<$y[$p]);\n}\nelse{\n my $j=$x[$p]+$y[$p];\n my $k=$x[$q]+$y[$q];\n print $j if($j>=$k);\n print $k if($j<$k);\n}\nprint \"\\n\";"}], "src_uid": "7c41fb6212992d1b3b3f89694b579fea"} {"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": "#usr/bin/perl -w\nuse strict;\n\nmy ($l,$r)=split/ /,;chomp($r);\nprint \"YES\\n\";my $n=$l;\nwhile($n<=$r){\n print \"$n \";\n $n++;\n print \"$n\\n\";\n $n++;\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n#use bigint;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $L, $R ) = split;\n\t\n\tprint \"YES\";\n\t\n\tfor( my $i = $L; $i < $R; $i += 2 ){\n\t\tprint join ' ', $i, $i + 1;\n\t\t}\n\t}"}], "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,\u2009c2,\u2009c3,\u2009c4 (1\u2009\u2264\u2009c1,\u2009c2,\u2009c3,\u2009c4\u2009\u2264\u20091000) \u2014 the costs of the tickets. The second line contains two integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20091000) \u2014 the number of buses and trolleys Vasya is going to use. The third line contains n integers ai (0\u2009\u2264\u2009ai\u2009\u2264\u20091000) \u2014 the number of times Vasya is going to use the bus number i. The fourth line contains m integers bi (0\u2009\u2264\u2009bi\u2009\u2264\u20091000) \u2014 the number of times Vasya is going to use the trolley number i.", "output_spec": "Print a single number \u2014 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\u00b71)\u2009+\u20093\u2009+\u20097\u2009=\u200912 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": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\n# 2013/11/26\n# [\u89e3\u6790\u6027\u306e\u89e3\u91c8]\n# \u969c\u5bb3\u7b49\u306e\u554f\u984c\u306e\u628a\u63e1\u3001\u5207\u308a\u5206\u3051\u306a\u3069\u306e\u89e3\u6790\u306e\u3057\u3084\u3059\u3055\n# \u6c17\u3092\u4ed8\u3051\u3066\u3044\u308b\u3053\u3068\uff1a\n# - if \u6587\u3084\u95a2\u6570\u5f15\u6570\u5185\u3067\u8a08\u7b97\u3057\u306a\u3044 (\u884c\u30d6\u30ec\u30fc\u30af\u304b\u3051\u305f\u6642\u306b\u8a08\u7b97\u7d50\u679c\u304c\u308f\u304b\u308a\u306b\u304f\u3044)\n# - \u5404\u884c\u306f\u30b7\u30f3\u30d7\u30eb\u306b (\u4e0a\u3068\u540c\u3058\u7406\u7531)\n# - perl\u306e\u5834\u5408\u306f use strict \u3068 use warnings \u3092\u4ed8\u3051\u308b (\u8868\u8a18\u306e\u66d6\u6627\u3055\u3092\u6392\u9664\u3059\u308b\u305f\u3081)\n# - 1\u95a2\u6570\u3042\u305f\u308a50\u884c\u7a0b\u5ea6 (\u5370\u5237\u3057\u305f\u3068\u304d\u306b1\u9801\u306b\u53ce\u307e\u308b)\n\n# TICKET1 ... 1\u56de\u4e57\u8eca\u5238\uff1a$c[0] burles\n# TICKET2 ... \u7279\u5b9a\u30d0\u30b9/\u8def\u9762\u96fb\u8eca\u6307\u5b9a\u4e57\u8eca\u5238\uff1a$c[1] burles\n# TICKET3 ... \u30d0\u30b9\u3042\u308b\u3044\u306f\u8def\u9762\u96fb\u8eca\u9650\u5b9a\u4e57\u8eca\u5238\uff1a$c[2] burles\n# TICKET4 ... \u5168\u3066\u306e\u30d0\u30b9/\u8def\u9762\u96fb\u8eca\u4e57\u308a\u653e\u984c\u4e57\u8eca\u5238\uff1a$c[3] burles\n\nuse constant ERR_OUTOFRANGE=>-1;\nuse constant NO_ERR=>0;\nuse constant TICKET1=>0;\nuse constant TICKET2=>1;\nuse constant TICKET3=>2;\nuse constant TICKET4=>3;\n\nmy $ret = NO_ERR;\n$ret = main();\nexit $ret;\n\n# \u30e1\u30a4\u30f3\u51e6\u7406\nsub main {\n # \u5909\u6570\u521d\u671f\u5316\n my $min_cost = 0;\n my $min_cost_bus = 0;\n my $min_cost_trolley = 0;\n my $cost_bus = 0;\n my $cost_trolley = 0;\n\n # \u6a19\u6e96\u5165\u529b\u304b\u3089\u30c7\u30fc\u30bf\u306e\u8aad\u307f\u8fbc\u307f\n my $line1 = <>;\n my $line2 = <>;\n my $line3 = <>;\n my $line4 = <>;\n\n # \u5165\u529b\u30c7\u30fc\u30bf\u306e\u6574\u5f62\n my (@c) = split(/ /, $line1);\n my ($n, $m) = split(/ /, $line2);\n my @a = sort {$b <=> $a} split(/ /, $line3); # \u6570\u5b57\u304c\u5927\u304d\u3044\u9806\u306b\u30bd\u30fc\u30c8\n my @b = sort {$b <=> $a} split(/ /, $line4); # \u6570\u5b57\u304c\u5927\u304d\u3044\u9806\u306b\u30bd\u30fc\u30c8\n \n # \u5165\u529b\u30c7\u30fc\u30bf\u306e\u30a8\u30e9\u30fc\u30c1\u30a7\u30c3\u30af\n foreach(@c) {\n if ($_ < 1 || $_ > 1000) { return ERR_OUTOFRANGE; }\n }\n if ($n < 1 || $n > 1000) { return ERR_OUTOFRANGE; }\n if ($m < 1 || $m > 1000) { return ERR_OUTOFRANGE; }\n \n # \u5168\u3066TICKET1(\u4e00\u56de\u4e57\u8eca\u5238)\u3092\u4f7f\u3063\u305f\u5834\u5408\n $cost_bus += $_ * $c[TICKET1] for @a;\n $cost_trolley += $_ * $c[TICKET1] for @b;\n $min_cost_bus = $cost_bus;\n $min_cost_trolley = $cost_trolley;\n\n # \u4e57\u3063\u305f\u56de\u6570\u306e\u591a\u3044\u9806\u304b\u3089TICKET2(\u7279\u5b9a\u30d0\u30b9/\u8def\u9762\u96fb\u8eca\u6307\u5b9a\u4e57\u8eca\u5238)\u3092\u4f7f\u3063\u305f\u5834\u5408\n # - \u30d0\u30b9\n foreach(@a) {\n if ($_ < 0 || $_ > 1000) { return ERR_OUTOFRANGE; }\n $cost_bus = $cost_bus - $_ * $c[TICKET1] + $c[TICKET2];\n # TICKET2\u3092\u4f7f\u3063\u3066\u3082\u5b89\u304f\u306a\u3089\u306a\u3044\u5834\u5408\u306f\u6b21\u3078\n if ($cost_bus > $min_cost_bus) { last; }\n $min_cost_bus = $cost_bus;\n }\n # - \u8def\u9762\u96fb\u8eca\n foreach(@b) {\n if ($_ < 0 || $_ > 1000) { return ERR_OUTOFRANGE; }\n $cost_trolley = $cost_trolley - $_ * $c[TICKET1] + $c[TICKET2];\n # TICKET2\u3092\u4f7f\u3063\u3066\u3082\u5b89\u304f\u306a\u3089\u306a\u3044\u5834\u5408\u306f\u6b21\u3078\n if ($cost_trolley > $min_cost_trolley) { last; }\n $min_cost_trolley = $cost_trolley;\n }\n\n # TICKET3(\u30d0\u30b9\u3042\u308b\u3044\u306f\u8def\u9762\u96fb\u8eca\u9650\u5b9a\u4e57\u8eca\u5238)\u3092\u4f7f\u3063\u305f\u5834\u5408\n if ($min_cost_bus > $c[TICKET3]) { $min_cost_bus = $c[TICKET3]; }\n if ($min_cost_trolley > $c[TICKET3]) { $min_cost_trolley = $c[TICKET3]; }\n\n # TICKET4(\u5168\u3066\u306e\u30d0\u30b9/\u8def\u9762\u96fb\u8eca\u4e57\u308a\u653e\u984c\u4e57\u8eca\u5238)\u3092\u4f7f\u3063\u305f\u5834\u5408\n $min_cost = $min_cost_bus + $min_cost_trolley;\n if ($min_cost > $c[TICKET4]) { $min_cost = $c[TICKET4]; }\n\n chomp($min_cost);\n print \"$min_cost\\n\";\n\n return NO_ERR;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\n#\u4eca\u56de\u306f\u30ed\u30b0\u3092\u57cb\u3081\u8fbc\u3093\u3060\u306e\u307f\u3002\n#\u8d77\u52d5\u6642\u306b-debug\u3092\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3064\u3051\u308b\u3068\u30c7\u30d0\u30c3\u30b0\u30ed\u30b0\u304c\u51fa\u529b\u3055\u308c\u308b\u3002\n\n#\u89e3\u6790\u5bb9\u6613\u6027\u3068\u3044\u3046\u3068\u96e3\u3057\u3044\u3051\u3069\u3001\n#\u4e00\u822c\u7684\u306b\u306f\u3084\u3063\u3071\u308a\u30c7\u30d0\u30c3\u30b0\u7528\u306e\u30ed\u30b0\u57cb\u3081\u8fbc\u307f\u3068\n#\u8981\u6240\u8981\u6240\u3067\u30a2\u30b5\u30fc\u30b7\u30e7\u30f3\u3092\u631f\u3093\u3067\u3084\u308b\u3053\u3068\u3067\u3059\u304b\u306d\u3002\n\n#\u30c7\u30d0\u30c3\u30ac\u3092\u4f7f\u3046\u3068\u3044\u3046\u306e\u3067\u3042\u308c\u3070\n#1\u884c\u3042\u305f\u308a\u306e\u547d\u4ee4\u6570\u3092\u5c11\u306a\u304f\u3057\u3066\u304a\u304f\u3068\u3001\n#\u30d6\u30ec\u30fc\u30af\u30dd\u30a4\u30f3\u30c8\u304c\u8a2d\u5b9a\u3057\u3084\u3059\u304f\u306a\u308b\u3002\n\n#exception\u304c\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306a\u8a00\u8a9e\u306a\u3089\u3001\n#\u30e1\u30bd\u30c3\u30c9\u5206\u5272\u3092\u3057\u3063\u304b\u308a\u3057\u3066\u304a\u304f\u3068trace\u3057\u3084\u3059\u304f\u306a\u308b\u3088\u3046\u306a\u6c17\u304c\u3059\u308b\u3002\n\n#\u3061\u306a\u307f\u306b\u4eca\u56de\u306f\u30ed\u30b0\u3092\u81ea\u524d\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u304c\u3001\n#CPAN\u304b\u3089\u53d6\u3063\u3066\u304f\u308c\u3070\u3088\u3044\u30ed\u30b0\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3082\u3042\u308a\u307e\u3059\u3002\n#asset\u3082\u6a19\u6e96\u3067\u306f\u4f7f\u3048\u306a\u3044\u306e\u3067\u30b3\u30e1\u30f3\u30c8\u30a2\u30a6\u30c8\u3002\n\nuse strict;\nuse warnings;\n\nmy $debug = 0;\n\nif(@ARGV > 0 && $ARGV[0] eq \"-debug\"){\n $debug = 1;\n}\n\n#use Test::Harness::Assert;\n\n#\u5165\u529b\u51e6\u7406\nmy $line = ;\nchomp $line;\nmy @costs = split(/\\s+/,$line);\n\n$line = ;\nchomp $line;\nmy($num_bus, $num_trolley) = split(/\\s+/,$line);\n\n$line = ;\nchomp $line;\nmy @using_buses = split(/\\s+/,$line);\n\n#assert($num_bus == @using_buses);\n\n$line = ;\nchomp $line;\nmy @using_trolley = split(/\\s+/,$line);\n\n#assert($num_trolley == @using_trolley);\n\n#\u30b3\u30b9\u30c8\u8a08\u7b97\nmy $bus_cost = &calc_cost(@using_buses);\n&debuglog(\"sum of buses cost = $bus_cost\\n\");\nmy $trolley_cost = &calc_cost(@using_trolley);\n&debuglog(\"sum of trolleys cost = $trolley_cost\\n\");\nmy $sum = $bus_cost + $trolley_cost;\n\n\n#\u51fa\u529b\u51e6\u7406\nif($sum < $costs[3]){\n print $sum,\"\\n\";\n}else{\n print $costs[3],\"\\n\";\n}\n\nsub calc_cost{\n my @nums = @_;\n my @one_type_costs;\n my $sum = 0;\n foreach my $num (@nums){\n if($num eq \"\"){\n next;\n }\n my $cost = &calc_onetype_cost($num);\n &debuglog(\"one type cost = $cost\\n\");\n $sum += $cost;\n }\n \n if($sum < $costs[2]){\n return $sum;\n }else{\n return $costs[2];\n }\n}\n\nsub calc_onetype_cost{\n my($num) = @_;\n my $cost;\n if($num * $costs[0] > $costs[1]){\n $cost = $costs[1];\n }else{\n $cost = $num * $costs[0];\n }\n return $cost;\n}\n\nsub debuglog{\n my $str = $_[0];\n if($debug == 1){\n print STDERR \"$str\\n\";\n }\n}\n\n"}, {"source_code": "while(<>){\n\tchomp;\n\t/ (\\d+) (\\d+) /;\n\t$bin=<>;\n\t$n=<>;\n\t$m=<>;\n\t@n=split/ /,$n;\n\t@m=split/ /,$m;\n\t$min=$';\n\t$sum=0;\n\tfor $i(@n){\n\t\t$sum+=($i*$`>$1?($1):($i*$`));\n\t\t}\n\t$sum=($sum>$2?($2):($sum));\n\t$mus=0;\n\tfor $i(@m){\n\t\t$mus+=($i*$`>$1?($1):($i*$`));\n\t\t}\n\t$mus=($mus>$2?($2):($mus));\t\t\n\t\n\t$min=($min>$sum+$mus?($sum+$mus):($min));\n\tprint \"$min\\n\";\n}"}], "negative_code": [], "src_uid": "11fabde93815c1805369bbbc5a40e2d8"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of mannequins. Next n lines contain two space-separated integers each: xi,\u2009yi (|xi|,\u2009|yi|\u2009\u2264\u20091000) \u2014 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 \u2014 the value of the sought angle in degrees. The answer will be considered valid if the relative or absolute error doesn't exceed 10\u2009-\u20096. ", "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": "#!/usr/bin/perl -w\n\nuse strict;\nuse List::Util qw(max);\nuse constant PI => atan2 0, -1;\nuse constant EPS => 1e-8;\n\nchomp(my $n = <>);\nmy @p = ();\nfor (1 .. $n) {\n chomp($_ = <>);\n my ($x, $y) = split;\n push @p, atan2 $y, $x;\n}\n@p = sort { $a <=> $b } @p;\npush @p, $p[0];\n\nmy $res = 0.0;\nfor my $i (1 .. $n) {\n my $diff = $p[$i] - $p[$i - 1];\n $diff += 2 * PI if $diff < - EPS;\n $res = max($res, $diff);\n}\n$res = 2 * PI if $res < EPS;\n$res = 360.0 - $res * 180.0 / PI;\nprint \"$res\\n\";\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\nuse List::Util qw(max);\nuse constant PI => atan2 0, -1;\nuse constant EPS => 1e-8;\n\nchomp(my $n = <>);\nmy @p = ();\nfor (1 .. $n) {\n chomp($_ = <>);\n my ($x, $y) = split;\n my $a = atan2 $y, $x;\n push @p, [$x, $y, $a];\n}\n@p = sort { $$a[2] <=> $$b[2] } @p;\npush @p, $p[0];\n\nmy $res = 0.0;\nfor my $i (1 .. $n) {\n my $diff = $p[$i][2] - $p[$i - 1][2];\n $diff += 2 * PI if $diff < EPS;\n $res = max($res, $diff);\n}\n$res = 360.0 - $res * 180.0 / PI;\nprint \"$res\\n\";\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\nuse List::Util qw(max);\nuse constant PI => atan2 0, -1;\nuse constant EPS => 1e-8;\n\nchomp(my $n = <>);\nmy @p = ();\nfor (1 .. $n) {\n chomp($_ = <>);\n my ($x, $y) = split;\n my $a = atan2 $y, $x;\n push @p, [$x, $y, $a];\n}\n@p = sort { $$a[2] <=> $$b[2] } @p;\npush @p, $p[0];\n\nmy $res = 0.0;\nfor my $i (1 .. $n) {\n my $diff = $p[$i][2] - $p[$i - 1][2];\n $diff += 2 * PI if $diff < - EPS;\n $res = max($res, $diff);\n}\n$res = 360.0 - $res * 180.0 / PI;\nprint \"$res\\n\";\n"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009100, 1\u2009\u2264\u2009k\u2009\u2264\u20091000) \u2014 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\u2009\u2264\u2009ai\u2009\u2264\u2009104) \u2014 the towers' initial heights.", "output_spec": "In the first line print two space-separated non-negative integers s and m (m\u2009\u2264\u2009k). 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\u2009\u2260\u2009j). 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": "while(<>){\n\t($n,$k)=split/ /;\n\t@a=split/ /,<>;\n\t@_=();\n\t$j=0;\n\twhile($k--){\n\t\t$min=10000;\n\t\t$max=0;\n\t\t($min>$_ and $min=$_) for @a;\n\t\t($max<$_ and $max=$_) for @a;\n\t\t$max-$min < 2 and last;\n\t\t\n\t\t$j++;\n\t\t\n\t\t$i=0;\n\t\t(++$i, $max == $_ and --$_ and (push @_,$i,\" \") and last) for @a;\n\t\t$i=0;\n\t\t(++$i, $min == $_ and ++$_ and (push @_,$i,\"\\n\") and last) for @a;\n\t\t\n\t\t}\n\t\t\n\t$min=10000;\n\t$max=0;\n\t($min>$_ and $min=$_) for @a;\n\t($max<$_ and $max=$_) for @a;\n\t\n\tprint $max-$min,\" \",$j,\"\\n\";\n\tprint @_;\n\t\n\t}"}], "negative_code": [{"source_code": "while(<>){\n\t($n,$k)=split/ /;\n\t@a=split/ /,<>;\n\t@_=();\n\t$j=0;\n\twhile($k--){\n\t\t$min=1000;\n\t\t$max=0;\n\t\t($min>$_ and $min=$_) for @a;\n\t\t($max<$_ and $max=$_) for @a;\n\t\t$max-$min < 2 and last;\n\t\t\n\t\t$j++;\n\t\t\n\t\t$i=0;\n\t\t(++$i, $max == $_ and --$_ and ((push @_,$i,\" \"),1) and last) for @a;\n\t\t$i=0;\n\t\t(++$i, $min == $_ and ++$_ and ((push @_,$i,\"\\n\"),1) and last) for @a;\n\t\t\n\t\t}\n\t\t\n\t$min=1000;\n\t$max=0;\n\t($min>$_ and $min=$_) for @a;\n\t($max<$_ and $max=$_) for @a;\n\t\n\tprint $max-$min,\" \",$j,\"\\n\";\n\tprint @_;\n\t\n\t}"}, {"source_code": "while(<>){\n\t($n,$k)=split/ /;\n\t@a=split/ /,<>;\n\t@_=();\n\t$j=0;\n\twhile($k--){\n\t\t$min=1000;\n\t\t$max=0;\n\t\t($min>$_ and $min=$_) for @a;\n\t\t($max<$_ and $max=$_) for @a;\n\t\t$max-$min < 2 and last;\n\t\t\n\t\t$j++;\n\t\t\n\t\t$i=0;\n\t\t(++$i, $max == $_ and --$_ and (push @_,$i,\" \") and last) for @a;\n\t\t$i=0;\n\t\t(++$i, $min == $_ and ++$_ and (push @_,$i,\"\\n\") and last) for @a;\n\t\t\n\t\t}\n\t\t\n\t$min=1000;\n\t$max=0;\n\t($min>$_ and $min=$_) for @a;\n\t($max<$_ and $max=$_) for @a;\n\t\n\tprint $max-$min,\" \",$j,\"\\n\";\n\tprint @_;\n\t\n\t}"}], "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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nwhile( $t -- > 0 ){\n my ($n) = map { $_ - 0 } split(/\\s+/,);\n if( $n % 2 == 1 ){\n if( $n == 1 ){\n print \"0\\n\";\n } elsif( $n == 3 ){\n print \"2\\n\";\n } else {\n print \"3\\n\";\n }\n } else {\n if( $n == 2 ){\n print \"1\\n\";\n } else {\n print \"2\\n\";\n }\n }\n}\n\n\n"}], "negative_code": [], "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,\u20092,\u2009...,\u2009n. 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\u2009+\u2009b, or a\u2009-\u2009b, or a\u2009\u00d7\u2009b.After n\u2009-\u20091 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\u2009\u2264\u2009n\u2009\u2264\u2009105).", "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\u2009-\u20091 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": "chomp (my $n = <>);\n\nsub show4 {\n printf \"1 * 2 = 2\\n\";\n printf \"3 * 2 = 6\\n\";\n printf \"6 * 4 = 24\\n\";\n}\n\nsub show7 {\n printf \"7 - 5 = 2\\n\";\n printf \"6 - 4 = 2\\n\";\n printf \"2 * 2 = 4\\n\";\n &show4;\n}\n\nsub boil4 {\n my $x = $_[0];\n my $n = ($x - 4) >> 1;\n while ($x > 4) {\n printf \"%d - %d = 1\\n\", $x--, $x--;\n }\n while ($n--) {\n printf \"1 * 1 = 1\\n\";\n }\n}\n\nsub boil7 {\n my $x = $_[0];\n my $n = ($x - 7) >> 1;\n while ($x > 7) {\n printf \"%d - %d = 1\\n\", $x--, $x--;\n }\n while ($n--) {\n printf \"1 * 1 = 1\\n\";\n }\n}\n\nif (0) {\n} elsif ($n == 1) {\n printf \"NO\\n\";\n} elsif ($n == 2) {\n printf \"NO\\n\";\n} elsif ($n == 3) {\n printf \"NO\\n\";\n} elsif ($n == 5) {\n printf \"YES\\n\";\n printf \"5 - 1 = 4\\n\";\n printf \"4 - 2 = 2\\n\";\n printf \"4 * 2 = 8\\n\";\n printf \"3 * 8 = 24\\n\";\n} elsif ($n == 4) {\n printf \"YES\\n\";\n &show4;\n} elsif ($n == 7) {\n printf \"YES\\n\";\n &show7;\n} elsif ($n & 1) {\n printf \"YES\\n\";\n &boil7 ($n);\n &show7;\n} else {\n printf \"YES\\n\";\n &boil4 ($n);\n &show4;\n}"}, {"source_code": "#!/usr/bin/perl\n\nmy $n = <>;\n\nchomp $n;\n\nif ($n <=3) {\n\tprint \"NO\\n\";\n\texit;\n}\n\nprint \"YES\\n\";\n\nwhile ($n > 5) {\n\tprint \"$n - \";\n\tprint $n - 1;\n\tprint \" = 1\\n\";\n\tprint \"1 * 1 = 1\\n\";\n\t$n -= 2;\n}\n\nif ($n == 5) {\n\tprint \"3 - 2 = 1\\n\";\n\tprint \"5 + 1 = 6\\n\";\n\tprint \"4 * 1 = 4\\n\";\n\tprint \"6 * 4 = 24\\n\";\n} else {\n\tprint \"1 * 2 = 2\\n\";\n\tprint \"2 * 3 = 6\\n\";\n\tprint \"4 * 6 = 24\\n\";\n}"}], "negative_code": [{"source_code": "chomp (my $n = <>);\n\nsub show4 {\n printf \"1 * 2 = 2\\n\";\n printf \"3 * 2 = 6\\n\";\n printf \"6 * 4 = 24\\n\";\n}\n\nsub show7 {\n printf \"7 - 5 = 2\\n\";\n printf \"6 - 4 = 2\\n\";\n printf \"2 * 2 = 4\\n\";\n &show4;\n}\n\nsub boil4 {\n my $x = $_[0];\n my $n = ($x - 4) >> 1;\n while ($x > 4) {\n printf \"%d - %d = 1\\n\", $x--, $x--;\n }\n while ($n--) {\n printf \"1 * 1 = 1\\n\";\n }\n}\n\nsub boil7 {\n my $x = $_[0];\n my $n = ($x - 7) >> 1;\n while ($x > 7) {\n printf \"%d - %d = 1\\n\", $x--, $x--;\n }\n while ($n--) {\n printf \"1 * 1 = 1\\n\";\n }\n}\n\nif (0) {\n} elsif ($n == 1) {\n printf \"NO\\n\";\n} elsif ($n == 2) {\n printf \"NO\\n\";\n} elsif ($n == 3) {\n printf \"NO\\n\";\n} elsif ($n == 5) {\n printf \"NO\\n\";\n \n} elsif ($n == 4) {\n printf \"YES\\n\";\n &show4;\n} elsif ($n == 7) {\n printf \"YES\\n\";\n &show7;\n} elsif ($n & 1) {\n &boil7 ($n);\n &show7;\n} else {\n &boil4 ($n);\n &show4;\n}"}, {"source_code": "chomp (my $n = <>);\n\nsub show4 {\n printf \"1 * 2 = 2\\n\";\n printf \"3 * 2 = 6\\n\";\n printf \"6 * 4 = 24\\n\";\n}\n\nsub show7 {\n printf \"7 - 5 = 2\\n\";\n printf \"6 - 4 = 2\\n\";\n printf \"2 * 2 = 4\\n\";\n &show4;\n}\n\nsub boil4 {\n my $x = $_[0];\n my $n = ($x - 4) >> 1;\n while ($x > 4) {\n printf \"%d - %d = 1\\n\", $x--, $x--;\n }\n while ($n--) {\n printf \"1 * 1 = 1\\n\";\n }\n}\n\nsub boil7 {\n my $x = $_[0];\n my $n = ($x - 7) >> 1;\n while ($x > 7) {\n printf \"%d - %d = 1\\n\", $x--, $x--;\n }\n while ($n--) {\n printf \"1 * 1 = 1\\n\";\n }\n}\n\nif (0) {\n} elsif ($n == 1) {\n printf \"NO\\n\";\n} elsif ($n == 2) {\n printf \"NO\\n\";\n} elsif ($n == 3) {\n printf \"NO\\n\";\n} elsif ($n == 5) {\n printf \"YES\\n\";\n printf \"5 - 1 = 4\\n\";\n printf \"4 - 2 = 2\\n\";\n printf \"4 * 2 = 8\\n\";\n printf \"3 * 8 = 24\\n\";\n} elsif ($n == 4) {\n printf \"YES\\n\";\n &show4;\n} elsif ($n == 7) {\n printf \"YES\\n\";\n &show7;\n} elsif ($n & 1) {\n &boil7 ($n);\n &show7;\n} else {\n &boil4 ($n);\n &show4;\n}"}, {"source_code": "#!/usr/bin/perl\n\nmy $n = <>;\n\nchomp $n;\n\nif ($n <=3) {\n\tprint \"NO\\n\";\n\texit;\n}\n\nwhile ($n > 5) {\n\tprint \"$n - \";\n\tprint $n - 1;\n\tprint \" = 1\\n\";\n\tprint \"1 * 1 = 1\\n\";\n\t$n -= 2;\n}\n\nif ($n == 5) {\n\tprint \"3 - 2 = 1\\n\";\n\tprint \"5 + 1 = 6\\n\";\n\tprint \"4 * 1 = 4\\n\";\n\tprint \"6 * 4 = 24\\n\";\n} else {\n\tprint \"1 * 2 = 2\\n\";\n\tprint \"2 * 3 = 6\\n\";\n\tprint \"4 * 6 = 24\\n\";\n}"}], "src_uid": "1bd1a7fd2a07e3f8633d5bc83d837769"} {"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 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000) \u2014 the number of points. The second line contains a sequence of integers x1,\u2009x2,\u2009...,\u2009xn (\u2009-\u2009109\u2009\u2264\u2009xi\u2009\u2264\u2009109) \u2014 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 \u2014 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": "sub gcd {\n my ($a, $b) = @_;\n ($a,$b) = ($b,$a) if $a > $b;\n while ($a) {\n ($a, $b) = ($b % $a, $a);\n }\n return $b;\n}\n\nmy $n = ;\nchomp $n;\n$n = $n * 1;\nmy @arr = split(' ', );\n#print @arr;\n#while (@arr < $n) {\n# print \"*\";\n# my $a = ;\n# chomp $a;\n# #$a = $a * 1;\n# print $a;\n# push @arr, $a;\n#}\n#print $arr;\nmy @ar = sort { $a <=> $b } @arr;\n#print @ar;\nmy $g = @ar[1] - @ar[0];\nfor($i=2;$i<$n;$i++){\n my $d = @ar[$i] - @ar[$i - 1];\n $g = gcd($g, $d);\n}\nmy %ans = 0;\nfor($i=1;$i<$n;$i++){\n $ans += (@ar[$i] - @ar[$i - 1]) / $g - 1;\n}\nprint $ans;"}], "negative_code": [], "src_uid": "805d82c407c1b34f217f8836454f7e09"} {"nl": {"description": "Almost every text editor has a built-in function of center text alignment. The developers of the popular in Berland text editor \u00abTextpad\u00bb 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 \u00ab*\u00bb 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": "my @r = ();\n\nmy $m=0;\nwhile(<>){\n chomp;\n push (@r, $_);\n if(length > $m){$m = length;}\n}\n\n$c=0;\nprint '*' x ($m+2),\"\\n\";\nforeach $a (@r){\n $l = length($a);$t=($m-$l)/2;\n if(($l%2) != ($m%2)){\n print '*', ' ' x ($t+$c%2), $a, ' ' x ($t+(1-$c%2)), \"*\\n\";\n $c++;\n }else{\n print \"*\", ' 'x $t, $a, ' 'x$t, \"*\\n\";\n \n }\n}\nprint '*' x ($m+2);"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\nuse List::Util qw{max};\nuse integer;\n\nmy @s;\nmy ($i,$l) = (0,0);\nwhile ($s[$i] = <>) {\n chomp $s[$i++];\n}\ndelete $s[$#s];\nmy $len = max map length, @s;\nprint '*' x ($len + 2), \"\\n\";\nfor (@s) {\n print '*';\n my $s = $len - length $_;\n if ($s % 2) {\n\tprint ' ' x ($s / 2 + $l), $_, ' ' x ($s / 2 + 1 - $l);\n\t$l = 1 - $l;\n } else {\n\tprint ' ' x ($s / 2), $_, ' ' x ($s / 2);\n }\n print \"*\\n\";\n}\nprint '*' x ($len + 2), \"\\n\";\n"}], "negative_code": [{"source_code": "my @r = ();\n\nmy $m=0;\nwhile(<>){\n chomp;\n push (@r, $_);\n if(length > $m){$m = length;}\n}\n\n$c=0;\nprint '*' x ($m+2),\"\\n\";\nforeach $a (@r){\n $l = length($a);$t=($m-$l)/2;\n if(($l%2) != ($m%2)){\n print '*', ' ' x ($t+$c%2), $a, ' ' x ($t+(1-$c%2)), \"*\\n\";\n $c++;\n }else{\n print \"*\", ' 'x $t, $a, ' 'x$t, \"*\\n\";\n $c=0;\n }\n}\nprint '*' x ($m+2);"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\nuse List::Util qw{max};\nuse integer;\n\nmy @s;\nmy ($i,$l) = (0,1);\nwhile ($s[$i] = <>) {\n chomp $s[$i++];\n}\ndelete $s[$#s];\nmy $len = max map length, @s;\nprint '*' x ($len + 2), \"\\n\";\nfor (@s) {\n print '*';\n my $s = $len - length $_;\n if ($s % 2) {\n\tprint ' ' x ($s / 2 + $l), $_, ' ' x ($s / 2 + 1 - $l);\n\t$l = 1 - $l;\n } else {\n\tprint ' ' x ($s / 2), $_, ' ' x ($s / 2);\n }\n print \"*\\n\";\n}\nprint '*' x ($len + 2), \"\\n\";\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,\u2009s (1\u2009\u2264\u2009n,\u2009s\u2009\u2264\u2009100). The i-th of the next n lines contains two integers xi, yi (1\u2009\u2264\u2009xi\u2009\u2264\u2009100;\u00a00\u2009\u2264\u2009yi\u2009<\u2009100), 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": "use List::Util \"max\";\n($n, $s) = split \" \", <>;\n$s = $s * 100;\n$max = -1;\nfor ($i = 0; $i < $n; $i++) {\n ($d, $c) = split \" \", <>;\n $cost = $d * 100 + $c;\n $max = max($max, ($s - $cost) % 100) unless $cost > $s;\n}\nprint $max;"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nsub max {\n\tmy ($x, $y) = (shift, shift);\n\t$x>$y ? $x:$y;\n}\n\n($n, $s) = split / /, <>;\n($ans, $s) = (-1, 100*$s);\nforeach (1 .. $n) {\n\t($x, $y) = split / /, <>;\n\t$x*100+$y<=$s and $ans = max($ans,(100-$y)%100);\n}\nsay $ans;"}, {"source_code": "#!perl\n\n($n, $mony) = split / /, <>;\n\n$mony1 = $mony - 1;\n$flag = 0;\n$maxResult = 0;\n\nforeach my $i (1..$n)\n{\n ($dolars, $cents) = split / /, <>;\n \n if(($dolars <= $mony1) or ($dolars == $mony and $cents == 0))\n {\n $result = 100 - $cents; \n $maxResult = $result if $result > $maxResult and $result < 100;\n \n $flag = 1;\n }\n}\n\n$\\ = \"\\n\";\n\nif($flag == 1)\n{\n print $maxResult;\n}\nelse\n{\n print -1;\n}"}, {"source_code": "($n,$s)=split/ /,<>;\nfor (<>){\n\t/ /,\n\t$s >= $`+$'/100 and push @_, $'+0 ? 100-$'\n\t\n\t:-0\n\t\n\t}\nprint @_?((sort{$b<=>$a}@_)[0])\n\n:-1"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nsub max {\n\tmy ($x, $y) = (shift, shift);\n\t$x>$y ? $x:$y;\n}\n\n($n, $s) = split / /, <>;\n($ans, $s) = (-1, 10*$s);\nforeach (1 .. $n) {\n\t($x, $y) = split / /, <>;\n\t$x*10+$y<=$s and $ans = max($ans,(100-$y)%100);\n}\nsay $ans;"}, {"source_code": "($n,$s)=split/ /,<>;\nfor (<>){\n\t/ /,\n\t$s >= $` and push @_, $'+0 ? 100-$'\n\t\n\t:-0\n\t\n\t}\nprint @_?((sort{$b<=>$a}@_)[0])\n\n:-1"}, {"source_code": "($n,$s)=split/ /,<>;\nfor (<>){\n\t/ /,\n\t$'+0 && $s >= $` and push @_, 100-$';\n\t}\nprint @_?((sort{$b<=>$a}@_)[0]) :-1"}, {"source_code": "($n,$s)=split/ /,<>;\nfor (<>){\n\t/ /,\n\t$s > $` and push @_, $'+0 ? 100-$'\n\t\n\t:-0\n\t\n\t}\nprint @_?((sort{$b<=>$a}@_)[0])\n\n:-1"}], "src_uid": "91be5db48b44a44adff4c809ffbb8e3e"} {"nl": {"description": "Mihai plans to watch a movie. He only likes palindromic movies, so he wants to skip some (possibly zero) scenes to make the remaining parts of the movie palindromic.You are given a list $$$s$$$ of $$$n$$$ non-empty strings of length at most $$$3$$$, representing the scenes of Mihai's movie.A subsequence of $$$s$$$ is called awesome if it is non-empty and the concatenation of the strings in the subsequence, in order, is a palindrome.Can you help Mihai check if there is at least one awesome subsequence of $$$s$$$?A palindrome is a string that reads the same backward as forward, for example strings \"z\", \"aaa\", \"aba\", \"abccba\" are palindromes, but strings \"codeforces\", \"reality\", \"ab\" are not.A sequence $$$a$$$ is a non-empty subsequence of a non-empty sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero, but not all) elements.", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) \u2014 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 10^5$$$) \u2014 the number of scenes in the movie. Then follows $$$n$$$ lines, the $$$i$$$-th of which containing a single non-empty string $$$s_i$$$ of length at most $$$3$$$, consisting of lowercase Latin letters. 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\" if there is an awesome subsequence of $$$s$$$, or \"NO\" otherwise (case insensitive).", "sample_inputs": ["6\n\n5\n\nzx\n\nab\n\ncc\n\nzx\n\nba\n\n2\n\nab\n\nbad\n\n4\n\nco\n\ndef\n\norc\n\nes\n\n3\n\na\n\nb\n\nc\n\n3\n\nab\n\ncd\n\ncba\n\n2\n\nab\n\nab"], "sample_outputs": ["YES\nNO\nNO\nYES\nYES\nNO"], "notes": "NoteIn the first test case, an awesome subsequence of $$$s$$$ is $$$[ab, cc, ba]$$$"}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tmy $ok = m/\\b\\w\\b/;\n\t\n\tmy %h2;\n\tmy %h3;\n\tmy %h2R;\n\t\n\tm/\t\\b(\\w)(\\w)\n\t\t(?:\n\t\t\\b\n\t\t(?{ $h2{ $& } ++;\n\t\t\t$ok ||= $h2{ $2 . $1 };\n\t\t\t$ok ||= $h2R{ $& };\n\t\t\t})\n\t\t|\n\t\t(\\w)\n\t\t(?{ $h3{ $& } ++;\n\t\t\t$ok ||= $h3{ \"$3$2$1\" };\n\t\t\t$ok ||= $h2{ $3 . $2 };\n\t\t\t$h2R{ $2 . $1 } ++;\n\t\t\t})\n\t\t)\n\t\t(?!)\n\t\t/x;\n\t\n\tprint $ok ? 'YES' : 'NO';\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tmy $ok = ( m/\\b\\w\\b/ || m/\\b(\\w)\\1\\b/ );\n\t\n\tmy %h2;\n\tmy %h3;\n\t\n\tm/\t\\b\\w\\w\\b\n\t\t(?{ $h2{ $& } ++;\n\t\t\t$h2{ ~~ reverse $& } and $ok = 1;\n\t\t\t})\n\t\t(?!)\n\t\t/x;\n\t\n\tm/\t\\w\\w\\w\n\t\t(?{ $h3{ $& } ++;\n\t\t\t$h3{ ~~ reverse $& } and $ok = 1;\n\t\t\t})\n\t\t(?!)\n\t\t/x;\n\t\n\tmy %h2L;\n\tmy %h2R;\n\t\n\tm/\\w(\\w\\w)(?{ $h2L{ ~~ reverse $1 } = $+[0] })(*FAIL)/gx;\n\tm!(\\w\\w)\\w(?{ $h2R{ ~~ reverse $1 } //= $+[0] })(*FAIL)!gx;\n\t\n\twhile( m/\\b\\w\\w\\b/g ){\n\t\t\n\t\tif( $h2L{ $& } and $+[0] < $h2L{ $& } or\n\t\t\t$h2R{ $& } and $+[0] > $h2R{ $& }\n\t\t\t){\n\t\t\t$ok = 1;\n\t\t\t}\n\t\t}\n\t\n\tprint $ok ? 'YES' : 'NO';\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\t$debug and print \"\\$_:[$_]\";\n\t\n\tif( 0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t){\n\t\tprint 'YES';\n\t\tnext;\n\t\t}\n\t\n\tmy %h3;\n\t\n\tmap $h3{ $_ } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h3 ){\n\t\tif( exists $h3{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tmy %h2;\n\tmy %h2L;\n\tmy %h2R;\n\t\n\tm/\\b\\w\\w\\b(?{ push @{ $h2{ $& } }, $-[0] })(*FAIL)/gx;\n\t\n\t$debug and print \"$_-->[@{ $h2{ $_ } }]\" for sort keys %h2;\n\t\n\tm/\\w(\\w\\w)(?{ $h2L{ scalar reverse $1 } = $-[0] })(*FAIL)/gx;\n\tm!(\\w\\w)\\w(?{ $h2R{ scalar reverse $1 } //= $-[0] })(*FAIL)!gx;\n\t\n\t$debug and print \"L:$_-->$h2L{ $_ }\" for sort keys %h2L;\n\t$debug and print \"R:$_-->$h2R{ $_ }\" for sort keys %h2R;\n\t\n\tfor my $key ( sort keys %h2 ){\n\t\t\n\t\tif( exists $h2{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t\n\t\tfor my $i ( 0 .. @{ $h2{ $key } } - 1 ){\n\t\t\t\n\t\t\tif( exists $h2L{ $key } and $h2{ $key }[ $i ] < $h2L{ $key } ){\n\t\t\t\tprint 'YES';\n\t\t\t\tnext MAIN;\n\t\t\t\t}\n\t\t\t\n\t\t\tif( exists $h2R{ $key } and $h2{ $key }[ $i ] > $h2R{ $key } ){\n\t\t\t\tprint 'YES';\n\t\t\t\tnext MAIN;\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t}\n\t\n\tprint 'NO';\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\t$debug and print \"\\$_:[$_]\";\n\t\n\tif( 0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t){\n\t\tprint 'YES';\n\t\tnext;\n\t\t}\n\t\n\tmy %h3;\n\t\n\tmap $h3{ $& } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h3 ){\n\t\tif( exists $h3{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tmy %h2;\n\tmy %h2L;\n\tmy %h2R;\n\t\n\tm/\\b\\w\\w\\b(?{ push @{ $h2{ $& } }, $-[0] })(*FAIL)/gx;\n\t\n\t$debug and print \"$_-->[@{ $h2{ $_ } }]\" for sort keys %h2;\n\t\n\tm/\\w(\\w\\w)(?{ $h2L{ scalar reverse $1 } = $-[0] })(*FAIL)/gx;\n\tm!(\\w\\w)\\w(?{ $h2R{ scalar reverse $1 } //= $-[0] })(*FAIL)!gx;\n\t\n\t$debug and print \"L:$_-->$h2L{ $_ }\" for sort keys %h2L;\n\t$debug and print \"R:$_-->$h2R{ $_ }\" for sort keys %h2R;\n\t\n\tfor my $key ( sort keys %h2 ){\n\t\t\n\t\tif( exists $h2{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t\n\t\tfor my $i ( 0 .. @{ $h2{ $key } } - 1 ){\n\t\t\t\n\t\t\tif( exists $h2L{ $key } and $h2{ $key }[ $i ] < $h2L{ $key } ){\n\t\t\t\tprint 'YES';\n\t\t\t\tnext MAIN;\n\t\t\t\t}\n\t\t\t\n\t\t\tif( exists $h2R{ $key } and $h2{ $key }[ $i ] > $h2R{ $key } ){\n\t\t\t\tprint 'YES';\n\t\t\t\tnext MAIN;\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t}\n\t\n\tprint 'NO';\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\t$debug and print \"\\$_:[$_]\";\n\t\n\tif( 0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t){\n\t\tprint 'YES';\n\t\tnext;\n\t\t}\n\t\n\tmy %h3;\n\t\n\tmap $h3{ $& } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h3 ){\n\t\tif( exists $h3{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tmy %h2;\n\tmy %h2L;\n\tmy %h2R;\n\t\n\tm/\\b\\w\\w\\b(?{ push @{ $h2{ $& } }, $-[0] })(*FAIL)/gx;\n\t\n\t$debug and print \"$_-->[@{ $h2{ $_ } }]\" for sort keys %h2;\n\t\n\tm/\\w(\\w\\w)(?{ $h2L{ scalar reverse $1 } = $-[0] })(*FAIL)/gx;\n\tm!(\\w\\w)\\w(?{ $h2R{ scalar reverse $1 } //= $-[0] })(*FAIL)!gx;\n\t\n\t$debug and print \"L:$_-->$h2L{ $_ }\" for sort keys %h2L;\n\t$debug and print \"R:$_-->$h2R{ $_ }\" for sort keys %h2R;\n\t\n\tfor my $key ( sort keys %h2 ){\n\t\tfor my $i ( 0 .. @{ $h2{ $key } } - 1 ){\n\t\t\t\n\t\t\tif( exists $h2L{ $key } and $h2{ $key }[ $i ] < $h2L{ $key } ){\n\t\t\t\tprint 'YES';\n\t\t\t\tnext MAIN;\n\t\t\t\t}\n\t\t\t\n\t\t\tif( exists $h2R{ $key } and $h2{ $key }[ $i ] > $h2R{ $key } ){\n\t\t\t\tprint 'YES';\n\t\t\t\tnext MAIN;\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t}\n\t\n\tprint 'NO';\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $& } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h ){\n\t\tif( exists $h{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tmy $_b;\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/ \\b(\\w)(\\w)\n\t\t\t\t( \\b )?\n\t\t\t\t(.*+)? (.*+)? (.*+)? (.*+)? .*\n\t\t\t\t( \\b )?\n\t\t\t\t\\2\\1\\b\n\t\t\t\t(??{ '-' x ( ( defined $3 ) * ( defined $4 ) ) })\n\t\t\t\t/sx\n\t\t) ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $& } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h ){\n\t\tif( exists $h{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tmy $_b;\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/ \\b(\\w)(\\w)\n\t\t\t\t( \\b )?\n\t\t\t\t.*\n\t\t\t\t( \\b )?\n\t\t\t\t\\2\\1\\b\n\t\t\t\t(??{ '-' x ( ( defined $3 ) * ( defined $4 ) ) })\n\t\t\t\t/sx\n\t\t) ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\n# WA\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $& } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h ){\n\t\tif( exists $h{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tmy $_b;\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/ \\b(\\w)(\\w)\n\t\t\t\t( \\b )?\n\t\t\t\t.*\n\t\t\t\t( \\b )?\n\t\t\t\t\\2\\1\\b\n\t\t\t\t(??{ '-' x ( $3 * $4 ) })\n\t\t\t\t/sx\n\t\t) ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\n# WA\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $& } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h ){\n\t\tif( exists $h{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tmy $_b;\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/ \\b(\\w)(\\w)\n\t\t\t\t(?{ $_b = 0 })\n\t\t\t\t(?: \\b (?{ $_b ++ }) | )\n\t\t\t\t.*\n\t\t\t\t(?: \\b (?{ $_b ++ }) | (?{ $_b -- }) )\n\t\t\t\t\\2\\1\\b\n\t\t\t\t(??{ '-' x ( $_b > 1 ) })\n\t\t\t\t/sx\n\t\t) ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\n# WA\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $& } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h ){\n\t\tif( exists $h{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tmy $_b;\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/ \\b(\\w)(\\w)\n\t\t\t\t(?{ $_b = 0 })\n\t\t\t\t(?: \\b (?{ $_b ++ }) | (?{ $_b -- }) )\n\t\t\t\t.*\n\t\t\t\t(?: \\b (?{ $_b ++ }) | (?{ $_b -- }) )\n\t\t\t\t\\2\\1\\b\n\t\t\t\t(??{ '-' x ( $_b > 1 ) })\n\t\t\t\t/sx\n\t\t) ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\n# WA\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $& } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h ){\n\t\tif( exists $h{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/\\b(\\w)(\\w)(.*+)?(.*+)?(.*+)?(.*+)?.*\\2\\1\\b/s\n\t\t) ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\n# WA\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $& } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h ){\n\t\tif( exists $h{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/\\b(\\w)(\\w)(.*+)?(.*+)?.*\\2\\1\\b/s\n\t\t) ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\n# WA\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $& } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h ){\n\t\tif( exists $h{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/\\b(\\w)(\\w)(??{ \".*$2$1\\\\b\" })/s\n\t\t) ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\n# WA\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $& } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h ){\n\t\tif( exists $h{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/\\b(\\w)(\\w).*.*\\2\\1\\b/s\n\t\t) ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\n# WA\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $& } ++, m/\\w\\w\\w/g;\n\t\n\tfor my $key ( sort keys %h ){\n\t\tif( exists $h{ scalar reverse $key } ){\n\t\t\tprint 'YES';\n\t\t\tnext MAIN;\n\t\t\t}\n\t\t}\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/\\b(\\w)(\\w).*\\2\\1\\b/s\n\t\t) ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\n# WA\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/\\b(\\w)(\\w).*.*\\2\\1\\b/s\n\t\t|| m/\\b(\\w)(\\w)(\\w).*.*\\3\\2\\1\\b/s\n\t\t) ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/\\b(\\w)(\\w).*\\2\\1\\b/s\n\t\t|| m/\\b(\\w)(\\w)(\\w).*\\3\\2\\1\\b/s\n\t\t) ];\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ = join '', map ~~<>, 1 .. $_;\n\t\n\tprint qw( NO YES )[ 0 + (\n\t\t0\n\t\t|| m/\\b\\w\\b/\n\t\t|| m/\\b(\\w)\\1\\b/\n\t\t|| m/\\b(\\w)\\w\\1\\b/\n\t\t|| m/\\b(\\w)(\\w).*\\2\\1\\b/s\n\t\t) ];\n\t}"}], "src_uid": "6d5aefc5a08194e35826764d60c8db3c"} {"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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\n## + https://www.perlmonks.org/?node_id=11118153\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tchomp;\n\tmy $character_bank = join '', reverse sort split //;\n\t\n\t<>;\n\t@_ = split ' ', <>;\n\t\n\tmy @idx;\n\tmy @groups;\n\t\n\twhile( @idx != @_ ){\n\t\n\t\tmy @idx_new;\n\t\t\n\t\tOUTER:\n\t\tfor my $i ( 0 .. @_ - 1 ){\n\t\t\tmy $sum = 0;\n\t\t\tfor my $j ( @idx ){\n\t\t\t\tnext OUTER if $i == $j;\n\t\t\t\t$sum += abs( $i - $j );\n\t\t\t\t}\n\t\t\t\n\t\t\t$_[ $i ] == $sum and push @idx_new, $i;\n\t\t\t}\n\t\t\n\t\tpush @idx, @idx_new;\n\t\tpush @groups, 0 + @idx_new;\n\t\t}\n\t\n\t\n\t$debug and print \"idx:[@idx]\";\n\t$debug and print \"groups:[@groups]\";\n\t\n\tmy $re = join '', map { \"(?=(.)) (\\\\g{-2}{$_}) \\\\g{-2}* .*? \" } @groups;\n\t\n\t$debug and print \"re:[$re]\";\n\t$debug and print \"[[$character_bank]]\";\n\t\n\t$character_bank =~ /\n\t\t$re\n\t\t/x or print \"FAIL!\";\n\t\n\tmy @chars_seq = ( $character_bank =~ /$re/x )[ map 2 * $_ + 1, 0 .. @groups - 1 ];\n\t\n\t$debug and print \"[[@chars_seq]]\";\n\t\n\tmy $chars_seq = join '', @chars_seq;\n\t\n\tmy @ans;\n\t\n\tfor my $c ( split //, $chars_seq ){\n\t\t$ans[ shift @idx ] = $c;\n\t\t}\n\t\n\tprint @ans;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\n## + https://www.perlmonks.org/?node_id=11118153\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\t\n\tchomp;\n\t$_ = join '', reverse sort split //;\n\t\n\t<>;\n\t@_ = split ' ', <>;\n\t\n\tmy @idx;\n\tmy @groups;\n\t\n\twhile( @idx != @_ ){\n\t\n\t\tmy @idx_new;\n\t\t\n\t\tOUTER:\n\t\tfor my $i ( 0 .. @_ - 1 ){\n\t\t\tmy $sum = 0;\n\t\t\tfor my $j ( @idx ){\n\t\t\t\tnext OUTER if $i == $j;\n\t\t\t\t$sum += abs( $i - $j );\n\t\t\t\t}\n\t\t\t\n\t\t\t$_[ $i ] == $sum and push @idx_new, $i;\n\t\t\t}\n\t\t\n\t\tpush @idx, @idx_new;\n\t\tpush @groups, 0 + @idx_new;\n\t\t}\n\t\n\tmy $re = join '', map { \"(?=(.)) (\\\\g{-2}{$_}) \\\\g{-2}* .*? \" } @groups;\n\t\n\tmy @ans;\n\t@ans[ @idx ] = map { split '' } ( /$re/x )[ map 2 * $_ + 1, 0 .. @groups - 1 ];\n\t\n\tprint @ans;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\n## + https://www.perlmonks.org/?node_id=11118153\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\t\n\tchomp;\n\t$_ = join '', reverse sort split //;\n\t\n\t<>;\n\t@_ = split ' ', <>;\n\t\n\tmy @idx;\n\tmy @groups;\n\t\n\twhile( @idx != @_ ){\n\t\n\t\tmy @idx_new;\n\t\t\n\t\tOUTER:\n\t\tfor my $i ( 0 .. @_ - 1 ){\n\t\t\tmy $sum = 0;\n\t\t\tfor my $j ( @idx ){\n\t\t\t\tnext OUTER if $i == $j;\n\t\t\t\t$sum += abs( $i - $j );\n\t\t\t\t}\n\t\t\t\n\t\t\t$_[ $i ] == $sum and push @idx_new, $i;\n\t\t\t}\n\t\t\n\t\tpush @idx, @idx_new;\n\t\tpush @groups, 0 + @idx_new;\n\t\t}\n\t\n\tmy $re = join '', map { \"(?=(.)) (\\\\g{-2}{$_}) \\\\g{-2}* .*? \" } @groups;\n\t\n\tmy @ans;\n\t@ans[ @idx ] = map { split '' } ( /$re/x )[ map 2 * $_ + 1, 0 .. @groups - 1 ];\n\t\n\tprint \"@ans\";\n\t}"}], "src_uid": "bc2f2b98c50a0165b7204a6d595eec4b"} {"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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200950), n \u2014 amount of lines, and m \u2014 amount of columns on Bob's sheet. The following n lines contain m characters each. Character \u00ab.\u00bb stands for a non-shaded square on the sheet, and \u00ab*\u00bb \u2014 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": "use strict;\n#use warnings;\nuse 5.010;\n\nmy ($n, $m) = split \" \", <>;\nmy @board;\n\nmy ($min_i, $max_i, $min_j, $max_j) = ($n, 0, $m, 0);\n\nsub min {\n return @_[0] < @_[1] ? @_[0] : @_[1];\n}\n\nsub max {\n return @_[0] < @_[1] ? @_[1] : @_[0];\n}\n\nfor my $i (0..$n) {\n push @board, [split //, <>];\n}\n\n#for my $i (0..$n) {\n #for my $j (0..$m) {\n #print $board[$i][$j];\n #}\n #print \"\\n\";\n#}\n\nfor my $i (0..$n) {\n for my $j (0..$m) {\n if($board[$i][$j] eq '*') {\n $min_i = min $min_i, $i;\n $max_i = max $max_i, $i;\n\n $min_j = min $min_j, $j;\n $max_j = max $max_j, $j;\n }\n }\n}\n\n#print \"$min_i, $max_i, $min_j, $max_j\\n\";\n\nfor my $i ($min_i..$max_i) {\n for my $j ($min_j..$max_j) {\n print $board[$i][$j];\n }\n print \"\\n\";\n}\n\n\n\n"}], "negative_code": [], "src_uid": "d715095ff068f4d081b58fbfe103a02c"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009104).", "output_spec": "If the answer exists, print 3 distinct numbers x, y and z (1\u2009\u2264\u2009x,\u2009y,\u2009z\u2009\u2264\u2009109, x\u2009\u2260\u2009y, x\u2009\u2260\u2009z, y\u2009\u2260\u2009z). 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\nmy $n=<>;\nchomp($n);\nif($n==1){\n\tprint \"-1\\n\";\n}else{\nmy $ans=$n . \" \" . ($n+1) . \" \" . (($n+1)*$n) . \"\\n\";\nprint $ans;\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t\n\tprint $_ == 1 ? -1 : join ' ', $_, $_ + 1, $_ * ($_ + 1);\n\t\n\t\n\t}"}, {"source_code": "$_ = <>;\n\nprint $_ < 2 ? -1 : join ' ', $_ + 0, $_ + 1, $_ * ($_ + 1)"}], "negative_code": [{"source_code": "\n\nmy $n=<>;\nchomp($n);\n\nmy $ans=$n . \" \" . ($n+1) . \" \" . (($n+1)*$n) . \"\\n\";\nprint $ans;"}, {"source_code": "\n\nmy $n=<>;\nchomp($n);\nif($n==1){\n\tprint \"-1\\n\";\n}\nmy $ans=$n . \" \" . ($n+1) . \" \" . (($n+1)*$n) . \"\\n\";\nprint $ans;"}], "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)$$$ \u2014 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$$$ \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { $i ++; [ $_, $i ] } @_;\n\t\n\t@_ = sort { $b->[ 0 ] <=> $a->[ 0 ] } @_;\n\t\n\t$debug and print \"@{ $_ }\" for @_;\n\t\n\tmy $x = 0;\n\tmy $sum = 0;\n\tmy @ans;\n\t\n\tfor( @_ ){\n\t\t$sum += $_->[ 0 ] * $x + 1;\n\t\t$x ++;\n\t\tpush @ans, $_->[ 1 ];\n\t\t}\n\t\n\tprint $sum;\n\tprint \"@ans\";\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $i = 0;\n\t\n\t@_ = map { $i ++; [ $_, $i ] } @_;\n\t\n\t@_ = sort { $b->[ 0 ] <=> $a->[ 0 ] } @_;\n\t\n\t$debug and print \"@{ $_ }\" for @_;\n\t\n\tmy $x = 0;\n\tmy $sum = 0;\n\t\n\tfor( @_ ){\n\t\t$sum += $_->[ 0 ] * $x + 1;\n\t\t$x ++;\n\t\t}\n\t\n\tprint $sum;\n\t}"}], "src_uid": "c79f25dab583edfcabb6991be7abf971"} {"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$$$)\u00a0\u2014 the number of test cases. The first line for each test case contains one integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($tcase) = map { $_ - 0 } split(/\\s+/,);\r\nwhile( $tcase -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my $res = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n for(my $j=$i;$j<$n;$j++){\r\n my $cz = $j-$i+1;\r\n for(my $k=$i;$k<=$j;$k++){\r\n $cz++ if $A[$k] == 0;\r\n }\r\n $res += $cz;\r\n }\r\n }\r\n print \"$res\\n\";\r\n \r\n}\r\n\r\n\r\n\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "negative_code": [], "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$$$)\u00a0\u2014 the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \\le n \\le 50$$$)\u00a0\u2014 the number of elements in the permutation. The second line of the test case contains $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$\u00a0\u2014 the given permutation $$$a$$$.", "output_spec": "For each test case, output a single integer\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($tc) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\nwhile( $tc -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/o,);\r\n my @a = map { $_ - 0 } split(/\\s+/o,);\r\n my @sa = sort { $a <=> $b } @a;\r\n \r\n if( join(':',@a) eq join(':',@sa) ){\r\n print \"0\\n\"; next;\r\n }\r\n if( $a[0]==$sa[0] or $a[-1] == $sa[-1] ){\r\n print \"1\\n\"; next;\r\n }\r\n if( $a[0]!=$a[1] and $a[-1]!=$a[-2] and $a[0]==$sa[-1] and $a[-1]==$sa[0] ){\r\n print \"3\\n\"; next;\r\n }\r\n print \"2\\n\";\r\n \r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [], "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$$$)\u00a0\u2014 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": "#!/usr/bin/perl -wl\nuse strict;\n\nsub getM {\n my $n = shift;\n my %M;\n for my $i ( 1 .. $n ) {\n @_ = split ' ', <>;\n for ( @_ ) {\n push @{ $M{ $i } }, $_;\n $i++;\n }\n }\n for ( keys %M ) {\n @{ $M{ $_ } } = sort @{ $M{ $_ } };\n }\n %M;\n}\n\nwhile (<>) {\n my ( $n, $m ) = split;\n my %A = getM($n);\n my %B = getM($n);\n my $ok = grep { \"@{ $A{ $_ } }\" ne \"@{ $B{ $_ } }\" } keys %A;\n print $ok ? \"NO\" : \"YES\";\n}\n"}, {"source_code": "#!/usr/bin/perl -wl\n\nuse strict;\n\nwhile( <> ){\n my( $n, $m ) = split;\n \n my %h;\n for my $i ( 1 .. $n ){\n @_ = split ' ', <>;\n my $j = 0;\n for( @_ ){\n push @{ $h{ $i + $j } }, $_;\n $j ++;\n }\n }\n \n my %g;\n for my $i ( 1 .. $n ){\n @_ = split ' ', <>;\n my $j = 0;\n for( @_ ){\n push @{ $g{ $i + $j } }, $_;\n $j ++;\n }\n }\n \n my $fail = 0;\n for my $key ( sort keys %h ){\n @{ $h{ $key } } = sort @{ $h{ $key } };\n @{ $g{ $key } } = sort @{ $g{ $key } };\n #% print \"[@{ $h{ $key } }|@{ $g{ $key } }]\";\n unless( \"@{ $h{ $key } }\" eq \"@{ $g{ $key } }\" ){\n $fail ++;\n }\n }\n \n print $fail ? \"NO\" : \"YES\";\n }\n"}], "negative_code": [], "src_uid": "77e2ddc4684fccd1856c93c2fc6e1ce2"} {"nl": {"description": "In the capital city of Berland, Bertown, demonstrations are against the recent election of the King of Berland. Berland opposition, led by Mr. Ovalny, believes that the elections were not fair enough and wants to organize a demonstration at one of the squares.Bertown has n squares, numbered from 1 to n, they are numbered in the order of increasing distance between them and the city center. That is, square number 1 is central, and square number n is the farthest from the center. Naturally, the opposition wants to hold a meeting as close to the city center as possible (that is, they want an square with the minimum number).There are exactly k (k\u2009<\u2009n) days left before the demonstration. Now all squares are free. But the Bertown city administration never sleeps, and the approval of an application for the demonstration threatens to become a very complex process. The process of approval lasts several days, but every day the following procedure takes place: The opposition shall apply to hold a demonstration at a free square (the one which isn't used by the administration). The administration tries to move the demonstration to the worst free square left. To do this, the administration organizes some long-term activities on the square, which is specified in the application of opposition. In other words, the administration starts using the square and it is no longer free. Then the administration proposes to move the opposition demonstration to the worst free square. If the opposition has applied for the worst free square then request is accepted and administration doesn't spend money. If the administration does not have enough money to organize an event on the square in question, the opposition's application is accepted. If administration doesn't have enough money to organize activity, then rest of administration's money spends and application is accepted If the application is not accepted, then the opposition can agree to the administration's proposal (that is, take the worst free square), or withdraw the current application and submit another one the next day. If there are no more days left before the meeting, the opposition has no choice but to agree to the proposal of City Hall. If application is accepted opposition can reject it. It means than opposition still can submit more applications later, but square remains free. In order to organize an event on the square i, the administration needs to spend ai bourles. Because of the crisis the administration has only b bourles to confront the opposition. What is the best square that the opposition can take, if the administration will keep trying to occupy the square in question each time? Note that the administration's actions always depend only on the actions of the opposition.", "input_spec": "The first line contains two integers n and k \u2014 the number of squares and days left before the meeting, correspondingly (1\u2009\u2264\u2009k\u2009<\u2009n\u2009\u2264\u2009105). The second line contains a single integer b \u2014 the number of bourles the administration has (1\u2009\u2264\u2009b\u2009\u2264\u20091018). The third line contains n space-separated integers ai \u2014 the sum of money, needed to organise an event on square i (1\u2009\u2264\u2009ai\u2009\u2264\u2009109). Please, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.", "output_spec": "Print a single number \u2014 the minimum number of the square where the opposition can organize the demonstration.", "sample_inputs": ["5 2\n8\n2 4 5 3 1", "5 2\n8\n3 2 4 1 5", "5 4\n1000000000000000\n5 4 3 2 1"], "sample_outputs": ["2", "5", "5"], "notes": "NoteIn the first sample the opposition can act like this. On day one it applies for square 3. The administration has to organize an event there and end up with 3 bourles. If on the second day the opposition applies for square 2, the administration won't have the money to intervene.In the second sample the opposition has only the chance for the last square. If its first move occupies one of the first four squares, the administration is left with at least 4 bourles, which means that next day it can use its next move to move the opposition from any square to the last one.In the third sample administration has a lot of money, so opposition can occupy only last square."}, "positive_code": [{"source_code": "($n,$k)=split(/ /,<>);\n$m=int <>;\n@c=map{int $_} split(/ /,<>);\n@z=sort {$a<=>$b}@c[0..$n-2];\n$s += $z[$n-$_-1] for(1 .. $k);\n$x=0;\nprint \"$n\\n\" if $s <= $m;\nfor(0 .. $n-1){\n\t$x = $_ + 1 if $s - $z[$n-$k-1] + $c[$_] > $m;\n\tlast if $x;\n}\nprint \"$x\\n\" if $s > $m;\n"}], "negative_code": [{"source_code": "($n,$k)=split(/ /,<>);\n$m=<>;\n@c=split(/ /,<>);\n@z=sort @c;\n$s += $z[$n-$_-1] for(1 .. $k);\n$x=0;\nprint \"$n\\n\" if $s <= $m;\nfor(0 .. $n-1){\n\t$x = $_ + 1 if $s - $z[$n-$k] + $c[$_] > $m;\n\tlast if $x;\n}\nprint \"$x\\n\" if $x;\n"}, {"source_code": "($n,$k)=split(/ /,<>);\n$m=<>;\n@c=split(/ /,<>);\n@z=sort @c[0..$n-2];\n$s += $z[$n-$_-1] for(1 .. $k);\n$x=0;\nprint \"$n\\n\" if $s <= $m;\nfor(0 .. $n-1){\n\t$x = $_ + 1 if $s - $z[$n-$k-2] + $c[$_] > $m;\n\tlast if $x;\n}\nprint \"$x\\n\" if $s > $m;\n"}, {"source_code": "($n,$k)=split(/ /,<>);\n$m=<>;\n@c=split(/ /,<>);\n@z=sort @c[0..$n-2];\n$s += $z[$n-$_-1] for(1 .. $k);\n$x=0;\nprint \"$n\\n\" if $s <= $m;\nfor(0 .. $n-1){\n\t$x = $_ + 1 if $s - $z[$n-$k-2] + $c[$_] > $m;\n\tlast if $x;\n}\nprint \"$x\\n\" if $x;\n"}], "src_uid": "8cc0271266966b3d0545c48ab82292df"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009105,\u20091\u2009\u2264\u2009d\u2009\u2264\u2009109) \u2014 the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1\u2009\u2264\u2009a\u2009\u2264\u2009b\u2009\u2264\u2009104) \u2014 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\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009105) \u2014 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 \u2014 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": "#!perl\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my ( $n, $d ) = map int, split /\\D/, <>;\n my ( $aa, $bb ) = map int, split /\\D/, <>;\n my %clients;\n for my $i ( 1 .. $n ) {\n my ( $x, $y ) = map int, split /\\D/, <>;\n $clients{$i} = $x * $aa + $y * $bb;\n }\n my @nclients = sort { $clients{$a} <=> $clients{$b} } keys %clients;\n my @accepted;\n while ( @nclients ) {\n my $client = shift @nclients;\n if ( $clients{$client} <= $d ) {\n $d -= $clients{$client};\n push @accepted, $client;\n } else {\n last;\n }\n }\n my $len = @accepted;\n print \"$len\\n@accepted\";\n}\n\n__END__\n\n"}], "negative_code": [], "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$$$\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @aa = map { $_ - 0 } split(/\\s+/,);\n \n my $prev = '';\n for(my $i=0;$i<=$n;$i++){\n my $l = 0;\n if( $i==0 ){\n $l = $aa[0] + 1;\n } elsif( $i==$n ){\n $l = $aa[$n-1] + 1;\n } else {\n $l = ( $aa[$i-1] > $aa[$i] ? $aa[$i-1] : $aa[$i] ) + 1;\n }\n my $s;\n if( $i==0 ){\n $s = ( 'a' x $l );\n } else {\n $s = substr($prev,0,$aa[$i-1]) . &nextc(substr($prev,$aa[$i-1],1)) . ( 'z' x ( $l - $aa[$i-1] - 1 ) );\n }\n print \"$s\\n\";\n $prev = $s;\n }\n}\n\nexit(0);\n\nsub nextc {\n my $c1 = shift;\n my $uc1 = unpack('C',$c1);\n $uc1++;\n $uc1 = 97 if $uc1 == 123 ;\n return pack(\"C\",$uc1);\n}\n\n"}], "negative_code": [], "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$$$)\u00a0\u2014 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": "<>;\nprint sort <> =~ /./g"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\tprint join '', sort split //;\n\t}"}, {"source_code": "<>;\n\nprint sort <> =~ /./g"}], "negative_code": [], "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\u2009\u2264\u2009n\u2009\u2264\u2009100). Then follow n lines. The i-th of those lines contains two words \u2014 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@_=<>;\n@e=(' rat$', ' child$| woman$', ' man$', ' captain$');\nsub p{print \"$`\\n\"}\nwhile (@e){\n $e=shift @e;\n for (@_){/$e/ and p}\n }"}], "negative_code": [], "src_uid": "753113fa5130a67423f2e205c97f8017"} {"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$$$) \u2014 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 \u2014 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/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B ) = sort { $a <=> $b } split;\n\t\n\tif( ( $A + $B ) % 3 == 0 and $A * 2 >= $B ){\n\t\tprint \"YES\";\n\t\t}\n\telse{\n\t\tprint \"NO\";\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "0a720a0b06314fde783866b47f35af81"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$\u00a0\u2014 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": "for(1..<>){($n,$m)=split/\\s/,<>;for(1..$n){for$j(1..$m){print\" \",($_^$j)%4>>1}}}"}, {"source_code": "for(1..<>){($n,$m)=split/\\s/,<>;for(1..$n){for$j(1..$m){print\" \",($j%4==2||$j%4==3)^($_%4==2||$_%4==3)?1:0}}}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t$_ = '1100' x ( 1 + $m / 4 );\n\t\n\ts/.//;\n\t\n\ts/.{$m}\\K.*//;\n\t\n\tmy $norm = $_;\n\tmy $inv = y/10/01/r;\n\t\n\t@_ = ( $norm );\n\t\n\tpush @_, ( $norm, $inv, $inv, $norm )[ $_ % 4 ] for 1 .. $n - 1;\n\t\n\tprint s/\\B/ /gr for @_;\n\t}"}, {"source_code": "for(1..<>){($n,$m)=split/\\s/,<>;for(1..$n){for$j(1..$m){print\" \",($_^$j)%4>>1}}}"}, {"source_code": "for(1..<>){($n,$m)=split/\\s/,<>;for(1..$n){for$j(1..$m){print\" \",($_^$j)%4>>1}}}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t$_ = '1100' x ( 1 + $m / 4 );\n\t\n\ts/.//;\n\t\n\ts/.{$m}\\K.*//;\n\t\n\tmy $norm = $_;\n\tmy $inv = y/10/01/r;\n\t\n\t@_ = ( $norm );\n\t\n\tpush @_, ( $norm, $inv, $inv, $norm )[ $_ % 4 ] for 1 .. $n - 1;\n\t\n\tprint for @_;\n\t}"}], "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 \u00abCall of Soldiers 3\u00bb.The game has (m\u2009+\u20091) players and n types of soldiers in total. Players \u00abCall of Soldiers 3\u00bb are numbered form 1 to (m\u2009+\u20091). Types of soldiers are numbered from 0 to n\u2009-\u20091. 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\u2009+\u20091)-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\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u200920;\u00a01\u2009\u2264\u2009m\u2009\u2264\u20091000). The i-th of the next (m\u2009+\u20091) lines contains a single integer xi (1\u2009\u2264\u2009xi\u2009\u2264\u20092n\u2009-\u20091), that describes the i-th player's army. We remind you that Fedor is the (m\u2009+\u20091)-th player.", "output_spec": "Print a single integer \u2014 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": "use strict;\nuse warnings;\n\nsub dec2bin {\n my $str = unpack(\"B32\", pack(\"N\", shift));\n $str =~ s/^0+(?=\\d)//; # otherwise you'll get leading zeros\n return $str;\n}\n\nmy @arr;\nmy ($n, $m, $k) = split / /, <>;\nmy ($count, $ans) = (0, 0);\nfor my $i(0..$m) {\n chomp($_ = <>);\n push @arr, $_;\n}\nfor my $i(0..$m-1) {\n # $count = unpack(\"%32b*\", $arr[$i] ^ $arr[-1]) - 2;\n $count = dec2bin(int$arr[$i] ^ int$arr[-1]) =~ tr/1/1/;\n # print unpack(\"%32b*\", 4);\n # print $arr[$i] . \" \" . $arr[-1] . \" \" . $count . \" \" . dec2bin(int$arr[$i] ^ int$arr[-1]) .\"\\n\";\n $ans++ if ($count <= $k);\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "sub dec2bin {\n my $str = unpack(\"B32\", pack(\"N\", shift));\n $str =~ s/^0+(?=\\d)//; # otherwise you'll get leading zeros\n return $str;\n}\n\nmy @arr;\nmy ($n, $m, $k) = split / /, <>;\nmy ($count, $ans) = (0, 0);\nfor my $i(0..$m) {\n chomp($_ = <>);\n push @arr, $_;\n}\nfor my $i(0..$m-1) {\n $count = dec2bin(int$arr[$i] ^ int$arr[-1]) =~ tr/1/1/;\n $ans++ if ($count <= $k);\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "#!/perl -w\n\nuse strict ;\n\n\nmy($n,$m,$k) = split /\\s+/s , ;\n\nmy @soldiertype = (0) ;\nmy $num = 1 ;\nwhile($num <= $m+1 ){\n chomp ( $soldiertype[$num] = );\n $num++ ;\n}\n\nmy $friend = 0 ;\nforeach(1..($m)){\n my $cmp_soldier = $soldiertype[$m+1] ; \n my $diff = 0 ;\n while ( $cmp_soldier || $soldiertype[$_] ){\n if ( ( $soldiertype[$_] & 1 ) ^ ( $cmp_soldier & 1 ) ){\n $diff ++ ;\n }\n $cmp_soldier = $cmp_soldier >> 1 ;\n $soldiertype[$_] = $soldiertype[$_] >> 1 ;\n }\n $friend++ if ( $diff <= $k ) ;\n}\n\nprint $friend, \"\\n\" ;"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nsub getBits {\n\tmy ($x, $ret) = (shift, 0);\n\t# say \"x = $x\";\n\twhile ($x) {\n\t\t$ret += ($x & 1);\n\t\t$x >>= 1;\n\t}\n\t# say \"ret = $ret\";\n\treturn $ret;\n}\n\n($n, $m, $k) = split / /, <>;\npush @a, int(<>) foreach (0..$m);\ngetBits($_ ^ $a[$m])<=$k and ++$ans foreach (@a);\nsay $ans-1;"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nsub popcount {\n my ($n) = @_;\n\n my $c = 0;\n while ($n) {\n $c++;\n $n = $n & ($n - 1);\n }\n $c;\n}\n\nsub hamming_distance {\n my ($x, $y) = @_;\n\n popcount((0+$x) ^ (0+$y));\n}\n\nmy ($n, $m, $k) = split ' ', <>;\n\nmy @players;\nfor (1..$m) {\n push @players, scalar <>;\n}\nmy $fedor = <>;\n\nsay scalar grep { hamming_distance($fedor, $_) <= $k } @players;\n"}], "negative_code": [{"source_code": "my @arr;\nmy ($n, $m, $k) = split / /, <>;\nmy ($count, $ans) = (0, 0);\nfor my $i(0..$m) {\n chomp($_ = <>);\n push @arr, $_;\n}\nfor my $i(0..$m-1) {\n $count = unpack(\"%32b*\", $arr[$i] & $arr[-1]);\n $ans++ if ($count <= $k);\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "my @arr;\nmy ($n, $m, $k) = split / /, <>;\nmy ($count, $ans) = (0, 0);\nfor my $i(0..$m) {\n chomp($_ = <>);\n push @arr, $_;\n}\nfor my $i(0..$m-1) {\n $count = unpack(\"%32b*\", $arr[$i] ^ $arr[-1]);\n $ans++ if ($count <= $k);\n}\nprint $ans . \"\\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": "use bigint;\r\n;\r\nwhile (<>)\r\n{\r\n chomp;\r\n my ($n, $m, $x) = split;\r\n $x--;\r\n my ($r, $c) = ($x % $n, int($x / $n));\r\n my $res = $r * $m + $c + 1;\r\n print \"$res\\n\";\r\n}"}, {"source_code": "#!/usr/bin/perl\r\n\r\nmy ($t) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\nwhile( $t -- > 0 ){\r\n my ($n,$m,$x) = map { $_ - 0 } split(/\\s+/o,);\r\n $x--;\r\n \r\n my $r = int( $x / $n );\r\n my $d = $x % $n;\r\n \r\n my $res = 1 + $d * $m + $r;\r\n print \"$res\\n\";\r\n \r\n}\r\n\r\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m, $x ) = split;\n\t\n\t$x --;\n\t\n\tmy $int = int $x / $n;\n\tmy $mod = $x % $n;\n\t\n\tprint 1 + $int + $mod * $m;\n\t}"}], "negative_code": [{"source_code": "use bigint;\r\n;\r\nwhile (<>)\r\n{\r\n chomp;\r\n my ($n, $m, $x) = split;\r\n $x--;\r\n my ($r, $c) = ($x % $n, $x / $n);\r\n my $res = $r * $m + $c + 1;\r\n print \"$res\\n\";\r\n}"}, {"source_code": "use bigint;\r\n;\r\nwhile (<>)\r\n{\r\n my ($n, $mm, $x) = split / /, chomp;\r\n $x--;\r\n my ($r, $c) = ($x % $n, $x / $n);\r\n my $res = $r * $mm + $c + 1;\r\n print \"$res\\n\";\r\n}"}, {"source_code": "use bigint;\r\n;\r\nwhile (<>)\r\n{\r\n my ($n, $m, $x) = split / /, chomp;\r\n $x--;\r\n my ($r, $c) = ($x % $n, $x / $n);\r\n my $res = $r * $m + $c + 1;\r\n print \"$res\";\r\n}"}], "src_uid": "e519e4495c9acef4c4a614aef73cb322"} {"nl": {"description": "Theofanis has a string $$$s_1 s_2 \\dots s_n$$$ and a character $$$c$$$. He wants to make all characters of the string equal to $$$c$$$ using the minimum number of operations.In one operation he can choose a number $$$x$$$ ($$$1 \\le x \\le n$$$) and for every position $$$i$$$, where $$$i$$$ is not divisible by $$$x$$$, replace $$$s_i$$$ with $$$c$$$. Find the minimum number of operations required to make all the characters equal to $$$c$$$ and the $$$x$$$-s that he should use in his operations.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains the integer $$$n$$$ ($$$3 \\le n \\le 3 \\cdot 10^5$$$) and a lowercase Latin letter $$$c$$$\u00a0\u2014 the length of the string $$$s$$$ and the character the resulting string should consist of. The second line of each test case contains a string $$$s$$$ of lowercase Latin letters\u00a0\u2014 the initial string. 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, firstly print one integer $$$m$$$\u00a0\u2014 the minimum number of operations required to make all the characters equal to $$$c$$$. Next, print $$$m$$$ integers $$$x_1, x_2, \\dots, x_m$$$ ($$$1 \\le x_j \\le n$$$)\u00a0\u2014 the $$$x$$$-s that should be used in the order they are given. It can be proved that under given constraints, an answer always exists. If there are multiple answers, print any.", "sample_inputs": ["3\n4 a\naaaa\n4 a\nbaaa\n4 b\nbzyx"], "sample_outputs": ["0\n1\n2\n2 \n2 3"], "notes": "NoteLet's describe what happens in the third test case: $$$x_1 = 2$$$: we choose all positions that are not divisible by $$$2$$$ and replace them, i.\u00a0e. bzyx $$$\\rightarrow$$$ bzbx; $$$x_2 = 3$$$: we choose all positions that are not divisible by $$$3$$$ and replace them, i.\u00a0e. bzbx $$$\\rightarrow$$$ bbbb. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\tmy( $n, $c ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\tmy $rindex = rindex $_, $c;\n\t\n\t$rindex < 1 and $rindex = 1;\n\t\n\t$debug and print \"[$n][$c][$_][$rindex]\";\n\t\n\tif( ! m/[^$c]/ ){\n\t\tprint 0;\n\t\t}\n\telse{\n\t\tfor my $i ( 0 .. $n - 1 ){\n\t\t\tnext if ( $i + 1 ) % ( $rindex + 1 ) == 0;\n\t\t\tsubstr $_, $i, 1, $c;\n\t\t\t}\n\t\t\n\t\t$debug and print \" [$_]\";\n\t\t\n\t\tmy $rindex2 = rindex $_, $c;\n\t\t\n\t\tif( ! m/[^$c]/ ){\n\t\t\tprint 1;\n\t\t\tprint $rindex + 1;\n\t\t\t}\n\t\telse{\n\t\t\tprint 2;\n\t\t\tprint join ' ', $rindex + 1, $rindex2 + 1;\n\t\t\t}\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $c ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\tif( m/[^$c]/ ){\n\t\tif( m/[^$c]\\B/ and m/[^$c]$/ ){\n\t\t\tprint 2; \n\t\t\tprint join ' ', $n - 1, $n;\n\t\t\t}\n\t\telse{\n\t\t\tprint 1;\n\t\t\tif( m/[^$c]$/ ){\n\t\t\t\tprint $n - 1;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tprint $n;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse{\n\t\tprint 0;\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $c ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\tif( m/[^$c]/ ){\n\t\tif( m/[^$c]\\b/ and m/[^$c]$/ ){\n\t\t\tprint 2; \n\t\t\tprint join ' ', $n - 1, $n;\n\t\t\t}\n\t\telse{\n\t\t\tprint 1;\n\t\t\tif( m/[^$c]$/ ){\n\t\t\t\tprint $n - 1;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tprint $n;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse{\n\t\tprint 0;\n\t\t}\n\t}"}], "src_uid": "3b8969f7f2051d559a1e375ce8275c73"} {"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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = sort { $a <=> $b } split;\n\t\n\tif( $_[ 0 ] + $_[ 1 ] == $_[ 2 ] ){\n\t\tprint 'YES';\n\t\t}\n\telse{\n\t\tprint 'NO';\n\t\t}\n\t}"}], "negative_code": [], "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$$$ \u2014 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$$$ \u2014 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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains an integer $$$q$$$ $$$(1 \\leq q \\leq 10^5)$$$\u00a0\u2014 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 \u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t@A = ( 1 ) x 3;\r\n\tmy @B;\r\n\t\r\n\tfor( 1 .. $_ ){\r\n\t\t( $d, $k, $_ ) = split ' ', <>;\r\n\t\t\r\n\t\t$B[ $d ] ||= y/b-z//d;\r\n\t\t$A[ $d ] += $k * length;\r\n\t\t\r\n\t\tprint $B[ 2 ] ? 'YES' :\r\n\t\t\t $B[ 1 ] ? 'NO' :\r\n\t\t\t $A[ 1 ] < $A[ 2 ] ? 'YES' : 'NO'\r\n\t\t}\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy @h;\n\t\n\t$h[ 1 ]{ a } ++;\n\t$h[ 2 ]{ a } ++;\n\t\n\t$h[ 1 ]{ b } = 0;\n\t$h[ 2 ]{ b } = 0;\n\t\n\tfor( 1 .. $_ ){\n\t\tmy( $d, $k, $x ) = split ' ', <>;\n\t\t\n\t\tmy $B = $x =~ /[^a]/;\n\t\t\n\t\tif( $B ){\n\t\t\t$h[ $d ]{ b } = 1;\n\t\t\t}\n\t\t\n\t\t$x =~ s/[^a]//g;\n\t\t$h[ $d ]{ a } += $k * length $x;\n\t\t\n\t\tif( $h[ 2 ]{ b } ){\n\t\t\tprint 'YES';\n\t\t\t}\n\t\telsif( $h[ 1 ]{ b } ){\n\t\t\tprint 'NO';\n\t\t\t}\n\t\telse{\n\t\t\tprint $h[ 1 ]{ a } < $h[ 2 ]{ a } ? 'YES' : 'NO';\n\t\t\t}\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "d40f0f3b577a1a5cfad2a657d6a1b90a"} {"nl": {"description": "Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins together. Their favorite occupation is to sort collections of coins. Sasha likes having things in order, that is why he wants his coins to be arranged in a row in such a way that firstly come coins out of circulation, and then come coins still in circulation. For arranging coins Dima uses the following algorithm. One step of his algorithm looks like the following: He looks through all the coins from left to right; If he sees that the i-th coin is still in circulation, and (i\u2009+\u20091)-th coin is already out of circulation, he exchanges these two coins and continues watching coins from (i\u2009+\u20091)-th. Dima repeats the procedure above until it happens that no two coins were exchanged during this procedure. Dima calls hardness of ordering the number of steps required for him according to the algorithm above to sort the sequence, e.g. the number of times he looks through the coins from the very beginning. For example, for the ordered sequence hardness of ordering equals one.Today Sasha invited Dima and proposed him a game. First he puts n coins in a row, all of them are out of circulation. Then Sasha chooses one of the coins out of circulation and replaces it with a coin in circulation for n times. During this process Sasha constantly asks Dima what is the hardness of ordering of the sequence. The task is more complicated because Dima should not touch the coins and he should determine hardness of ordering in his mind. Help Dima with this task. ", "input_spec": "The first line contains single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009300\u2009000)\u00a0\u2014 number of coins that Sasha puts behind Dima. Second line contains n distinct integers p1,\u2009p2,\u2009...,\u2009pn (1\u2009\u2264\u2009pi\u2009\u2264\u2009n)\u00a0\u2014 positions that Sasha puts coins in circulation to. At first Sasha replaces coin located at position p1, then coin located at position p2 and so on. Coins are numbered from left to right.", "output_spec": "Print n\u2009+\u20091 numbers a0,\u2009a1,\u2009...,\u2009an, where a0 is a hardness of ordering at the beginning, a1 is a hardness of ordering after the first replacement and so on. ", "sample_inputs": ["4\n1 3 4 2", "8\n6 8 3 4 7 2 1 5"], "sample_outputs": ["1 2 3 2 1", "1 2 2 3 4 3 4 5 1"], "notes": "NoteLet's denote as O coin out of circulation, and as X \u2014 coin is circulation.At the first sample, initially in row there are coins that are not in circulation, so Dima will look through them from left to right and won't make any exchanges.After replacement of the first coin with a coin in circulation, Dima will exchange this coin with next three times and after that he will finally look through the coins and finish the process.XOOO \u2009\u2192\u2009 OOOXAfter replacement of the third coin, Dima's actions look this way:XOXO \u2009\u2192\u2009 OXOX \u2009\u2192\u2009 OOXXAfter replacement of the fourth coin, Dima's actions look this way:XOXX \u2009\u2192\u2009 OXXXFinally, after replacement of the second coin, row becomes consisting of coins that are in circulation and Dima will look through coins from left to right without any exchanges."}, "positive_code": [{"source_code": "use strict;\nour ($n, $m, @l, @r);\n\nsub calc {\n\tmy $p = shift;\n\tmy ($left, $right) = ($l[$p - 1], $r[$p + 1]);\n\tif (!$left and !$right) {\n\t\t$l[$p] = $r[$p] = $p;\n\t} elsif ($left and $right) {\n\t\t$r[$left] = $right;\n\t\t$l[$right] = $left;\n\t} elsif ($left) {\n\t\t$l[$p] = $left;\n\t\t$r[$left] = $p;\n\t} elsif ($right) {\n\t\t$r[$p] = $right;\n\t\t$l[$right] = $p;\n\t}\n\tmy $empty = ($l[$n] == undef);\n\tmy $last = ($empty? 0: $n - $l[$n] + 1);\n\t++$m - $last + 1;\n}\n\n$n = <>; \nprint \"1 \";\nprint calc($_), \" \" for split \" \", <>;\n"}, {"source_code": "$m = <>;\n\nprint 1, map {\n\t$U[ $_ - 1 ] = 1;\n\tif( $_ == $m ){\n\t\t$i = $_;\n\t\t$c --, $m -- while $U[ $i - 1 ] and $i --;\n\t\t}\n\t' ', 1 + ++ $c\n\t} split ' ', <>\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\tmy $m = @_;\n\tmy $c = 1;\n\tmy @c;\n\tpush @c, $c;\n\tmy @used = (0) x $m;\n\t\n\tfor( @_ ){\n\t\t$used[ $_ - 1 ] = 1;\n\t\t$_ == $m ?\n\t\t\tdo {\n\t\t\t\tmy $i = $_;\n\t\t\t\twhile( $i > 0 and $used[ $i - 1 ] ){\n\t\t\t\t\t$c --;\n\t\t\t\t\t$m --;\n\t\t\t\t\t$i --;\n\t\t\t\t\t}\n\t\t\t\t$c ++;\n\t\t\t\tpush @c, $c;\n\t\t\t}\n\t\t\t:\n\t\t\tdo {\n\t\t\t\t$c ++;\n\t\t\t\tpush @c, $c;\n\t\t\t};\n\t\t}\n\t\n\tprint \"@c\";\n\t}"}], "negative_code": [], "src_uid": "b97eeaa66e91bbcc3b5e616cb480c7af"} {"nl": {"description": "Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere.Lala Land has exactly n apple trees. Tree number i is located in a position xi and has ai apples growing on it. Amr wants to collect apples from the apple trees. Amr currently stands in x\u2009=\u20090 position. At the beginning, he can choose whether to go right or left. He'll continue in his direction until he meets an apple tree he didn't visit before. He'll take all of its apples and then reverse his direction, continue walking in this direction until he meets another apple tree he didn't visit before and so on. In the other words, Amr reverses his direction when visiting each new apple tree. Amr will stop collecting apples when there are no more trees he didn't visit in the direction he is facing.What is the maximum number of apples he can collect?", "input_spec": "The first line contains one number n (1\u2009\u2264\u2009n\u2009\u2264\u2009100), the number of apple trees in Lala Land. The following n lines contains two integers each xi, ai (\u2009-\u2009105\u2009\u2264\u2009xi\u2009\u2264\u2009105, xi\u2009\u2260\u20090, 1\u2009\u2264\u2009ai\u2009\u2264\u2009105), representing the position of the i-th tree and number of apples on it. It's guaranteed that there is at most one apple tree at each coordinate. It's guaranteed that no tree grows in point 0.", "output_spec": "Output the maximum number of apples Amr can collect.", "sample_inputs": ["2\n-1 5\n1 5", "3\n-2 2\n1 4\n-1 3", "3\n1 9\n3 5\n7 10"], "sample_outputs": ["10", "9", "9"], "notes": "NoteIn the first sample test it doesn't matter if Amr chose at first to go left or right. In both cases he'll get all the apples.In the second sample test the optimal solution is to go left to x\u2009=\u2009\u2009-\u20091, collect apples from there, then the direction will be reversed, Amr has to go to x\u2009=\u20091, collect apples from there, then the direction will be reversed and Amr goes to the final tree x\u2009=\u2009\u2009-\u20092.In the third sample test the optimal solution is to go right to x\u2009=\u20091, collect apples from there, then the direction will be reversed and Amr will not be able to collect anymore apples because there are no apple trees to his left."}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = ();\n@b = ();\nforeach (1 .. $n) {\n\t($x, $a) = split / /, <>;\n\t$x>0 and push @a, [$x, $a] or push @b, [$x, $a];\n}\n($an, $bn) = (scalar @a, scalar @b);\n@a = sort {$a->[0] <=> $b->[0]} @a;\n@b = sort {$b->[0] <=> $a->[0]} @b;\n$ans = 0;\n# positive first\n$tmp = 0;\nif ($bn < $an) {\n\t$tmp += $a[$_][1] foreach (0 .. $bn);\n\t$tmp += $b[$_][1] foreach (0 .. $bn-1);\n} else {\n\t$tmp += $a[$_][1] foreach (0 .. $an-1);\n\t$tmp += $b[$_][1] foreach (0 .. $an-1);\n}\n$ans = $tmp>$ans ? $tmp:$ans;\n# negative first\n$tmp = 0;\nif ($an < $bn) {\n\t$tmp += $a[$_][1] foreach (0 .. $an-1);\n\t$tmp += $b[$_][1] foreach (0 .. $an);\n} else {\n\t$tmp += $a[$_][1] foreach (0 .. $bn-1);\n\t$tmp += $b[$_][1] foreach (0 .. $bn-1);\n}\n$ans = $tmp>$ans ? $tmp:$ans;\nsay $ans;"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp $n;\n\nmy (@neg, @pos);\n\nfor (1..$n) {\n my $line = <>;\n chomp $line;\n my ($p, $apples) = split(/ /, $line);\n if ($p < 0) {\n $p *= -1;\n push(@neg, \"$p:$apples\");\n } else {\n push(@pos, \"$p:$apples\");\n }\n}\n\n@neg = sort {\n my @agh = split(/:/, $a);\n my @bgh = split(/:/, $b);\n my $na = $agh[0];\n my $nb = $bgh[0];\n $na <=> $nb\n} @neg;\n\n@pos = sort {\n my @agh = split(/:/, $a);\n my @bgh = split(/:/, $b);\n my $na = $agh[0];\n my $nb = $bgh[0];\n $na <=> $nb\n} @pos;\n\nmap { my @tmp = split(/:/, $_); $_ = $tmp[1] } @pos;\nmap { my @tmp = split(/:/, $_); $_ = $tmp[1] } @neg;\n\nmy @origpos = @pos;\nmy @origneg = @neg;\nmy $res = 0;\nmy $tmp = 0;\nwhile (scalar(@pos) > 0) {\n $tmp += shift @pos;\n if (scalar(@neg) > 0) {\n $tmp += shift @neg;\n } else {\n last;\n }\n}\nif ($tmp > $res) {\n $res = $tmp;\n}\n@pos = @origpos;\n@neg = @origneg;\n$tmp = 0;\nwhile (scalar(@neg) > 0) {\n $tmp += shift @neg;\n if (scalar(@pos) > 0) {\n $tmp += shift @pos;\n } else {\n last;\n }\n}\nif ($tmp > $res) {\n $res = $tmp;\n}\nprint \"$res\\n\";\n\n\n"}, {"source_code": "$\\ = $/;\n\nwhile($n = <>){\n\tundef @h;\n\tfor (1 .. $n){\n\t\tpush @h, split \" \", <>;\n\t\t}\n\t%h = @h;\n\n\t$u = $d = 0;\n\t/-/ ? $d++ : $u++ for keys %h;\n\t$min = $d > $u ? $u : $d;\n\t@d = sort {$b <=> $a} grep /-/, keys %h;\n\t@u = sort {$a <=> $b} grep !/-/, keys %h;\n\t\n\t$sum = 0;\n\tfor (1 .. $min){\n\t\t$dd = shift @d;\n\t\t$uu = shift @u;\n\t\t$sum += $h{$dd};\n\t\t$sum += $h{$uu};\n\t\t}\n\t\n\t$iu = $id = 0;\n\t@d and $id = shift @d;\n\t@u and $iu = shift @u;\n\t\n\t$max = $h{$iu} > $h{$id} ? $h{$iu} : $h{$id};\n\t\n\t$sum += $max;\n\tprint $sum;\n\t}"}, {"source_code": "\t<>, %h = map split, <>;\n\n\t++ ( /-/ ? $d : $u ) for keys %h;\n\n\t@d = sort {$b <=> $a} grep /-/, keys %h;\n\t@u = sort {$a <=> $b} grep !/-/, keys %h;\n\t\n\t$sum += $h{shift @d} + $h{shift @u} while @d && @u; \n\n\tprint $sum += 0 + $h{ 0 + \"@d@u\" }"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile($n = <>){\n\tundef @h;\n\tfor (1 .. $n){\n\t\tpush @h, split \" \", <>;\n\t\t}\n\t%h = @h;\n\n\t$u = $d = 0;\n\t/-/ ? $d++ : $u++ for keys %h;\n\t$min = $d > $u ? $u : $d;\n\t@d = sort sort {$b <=> $a} grep /-/, keys %h;\n\t@u = sort sort {$a <=> $b} grep !/-/, keys %h;\n\t\n\t$sum = 0;\n\tfor (1 .. $min){\n\t\t$dd = shift @d;\n\t\t$uu = shift @u;\n\t\t$sum += $h{$dd};\n\t\t$sum += $h{$uu};\n\t\t}\n\t\n\t$iu = $id = 0;\n\t@d and $id = shift @d;\n\t@u and $iu = shift @u;\n\t\n\t$max = $h{$iu} > $h{$id} ? $h{$iu} : $h{$id};\n\t\n\t$sum += $max;\n\tprint $sum;\n\t}"}, {"source_code": "\t<>, %h = map split, <>;\n\n\t++ ( /-/ ? $d : $u ) for keys %h;\n\n\t@d = sort {$b <=> $a} grep /-/, keys %h;\n\t@u = sort {$a <=> $b} grep !/-/, keys %h;\n\t\n\t$sum += $h{shift @d} + $h{shift @u} while @d && @u; \n\n\tprint $sum += 0 + $h{+\"@d@u0\"}"}, {"source_code": "\t<>, %h = map split, <>;\n\n\t++ ( /-/ ? $d : $u ) for keys %h;\n\n\t@d = sort {$b <=> $a} grep /-/, keys %h;\n\t@u = sort {$a <=> $b} grep !/-/, keys %h;\n\t\n\t$sum += $h{shift @d} + $h{shift @u} while @d && @u; \n\t\n\tprint $sum += (@d, @u, 0)[0]"}], "src_uid": "bf573af345509b2364ada6e613b6f998"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009105, 1\u2009\u2264\u2009|k|\u2009\u2264\u200910). Next line contains n integers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009109\u2009\u2264\u2009ai\u2009\u2264\u2009109)\u00a0\u2014 affection values of chemicals.", "output_spec": "Output a single integer\u00a0\u2014 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\u2009=\u20091.In the first sample, Molly can get following different affection values: 2: segments [1,\u20091], [2,\u20092], [3,\u20093], [4,\u20094]; 4: segments [1,\u20092], [2,\u20093], [3,\u20094]; 6: segments [1,\u20093], [2,\u20094]; 8: segments [1,\u20094]. Out of these, 2, 4 and 8 are powers of k\u2009=\u20092. Therefore, the answer is 8.In the second sample, Molly can choose segments [1,\u20092], [3,\u20093], [3,\u20094]."}, "positive_code": [{"source_code": "$/ = \"\"; $_ = <>; ($n, $k, @a) = split;\n\nif ($k == 1) {\n\t@k = (1);\n} elsif ($k == -1) {\n\t@k = (1, -1);\n} else {\n\t$i = 1; push(@k, $i), $i *= $k while $i <= 1e14;\n}\t\n$h{0} = 1;\n\nfor $a (@a) {\n\t$a += $p;\n\tfor $k (@k) {\n\t\t$c += $h{$a - $k};\n\t}\n\t$h{$a}++;\n\t$p = $a;\n}\n\nprint 0 + $c;\n"}], "negative_code": [{"source_code": "$/ = \"\"; $_ = <>; ($n, $k, @a) = split;\n$h{0} = $i = 1; push(@k, $i), $i *= $k while $i <= 1e14;\n\nfor $a (@a) {\n\t$a += $p;\n\tfor $k (@k) {\n\t\t$c++ if exists $h{$a - $k};\n\t}\n\t$h{$a} = 1;\n\t$p = $a;\n}\n\nprint $c;\n"}, {"source_code": "$/ = \"\"; $_ = <>; ($n, $k, @a) = split;\n$h{0} = $i = 1; push(@k, $i), $i *= $k while $i <= 1e14;\n\nfor $a (@a) {\n\t$a += $p;\n\tfor $k (@k) {\n\t\t$c++ if exists $h{$a - $k};\n\t}\n\t$h{$a} = 1;\n\t$p = $a;\n}\n\nprint 0 + $c;\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\u2009-\u2009l\u2009+\u20091 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\u2009+\u20091, the animal l\u2009+\u20092 swaps with the animal l\u2009+\u20093, ..., finally, the animal at position r\u2009-\u20091 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\u2009000 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\u2009\u2264\u2009n\u2009\u2264\u2009100)\u00a0\u2014 number of animals in the robber girl's zoo. The second line contains n space-separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109), 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\u2009\u2264\u2009li\u2009<\u2009ri\u2009\u2264\u2009n)\u00a0\u2014 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\u2009000. 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\u2009000 operations is allowed."}, "positive_code": [{"source_code": "@_ = split ' ', (<>,<>);\n\nfor (0 .. @_ - 2){\n\tfor $j (0 .. @_ - 2 - $i){\n\t\tif ( $_[$j] > $_[$j+1] ){\n\t\t\t( $_[$j], $_[$j+1] ) = ( $_[$j+1], $_[$j] );\n\t\t\tprint $j+1, ' ', $j+2, \"\\n\"\n\t\t\t}\n\t\t}\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\tfor $i (0 .. @_ - 2){\n\t\tfor $j (0 .. @_ - 2 - $i){\n\t\t\tif ( $_[$j] > $_[$j+1] ){\n\t\t\t\t( $_[$j], $_[$j+1] ) = ($_[$j+1], $_[$j]);\n\t\t\t\tprint $j+1, ' ', $j+2;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\tfor $i (0 .. @_ - 2){\n\t\tfor $j ($i .. @_ - 2){\n\t\t\tif ( $_[$j] > $_[$j+1] ){\n\t\t\t\t( $_[$j], $_[$j+1] ) = ($_[$j+1], $_[$j]);\n\t\t\t\tprint $j, ' ', $j+1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\tfor $i (0 .. @_ - 2){\n\t\tfor $j ($i .. @_ - 2){\n\t\t\tif ( $_[$j] > $_[$j+1] ){\n\t\t\t\t( $_[$j], $_[$j+1] ) = ($_[$j+1], $_[$j]);\n\t\t\t\tprint $j+1, ' ', $j+2;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}"}, {"source_code": "@_ = split ' ', (<>,<>);\n\nfor (0 .. @_ - 2){\n\tfor $j (0 .. @_ - 2 - $i){\n\t\tif ( $_[$j] > $_[$j+1] ){\n\t\t\t( $_[$j], $_[$j+1] ) = ( $_[$j+1], $_[$j] );\n\t\t\tprint $j+1, ' ', $j+2\n\t\t\t}\n\t\t}\n\t}"}], "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,\u200910 d: 4,\u20098 e: 6 f: 7 z: 1,\u20095,\u20099 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\u2009-\u20091\u2009=\u20094, 9\u2009-\u20095\u2009=\u20094, for letters c: 10\u2009-\u20093\u2009=\u20097, for letters d: 8\u2009-\u20094\u2009=\u20094. 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 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\u2009\u2264\u2009i\u2009\u2264\u2009n), that ai\u2009<\u2009bi, and for any j (1\u2009\u2264\u2009j\u2009<\u2009i) aj\u2009=\u2009bj."}, "positive_code": [{"source_code": "$n=<>;print substr('abcd'x($n/4+$n%4),0,$n);\n"}, {"source_code": "$n=<>;\n$s='abcd'x($n/4+$n%4);\nprint substr($s,0,$n);\n\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\nchomp ($i=<>);\n$_=\"abcd\"x($i/4+1);\nfor $j(1..4-$i%4){chop}\nprint ;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$_=\"abcd\"x25e3;\nchomp ($i=<>);\nprint substr($_,0,$i);\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\nfor $i(0..<>-1){s/$/$i%4==0?(\"a\"):($i%4==1?(\"b\"):($i%4==2?(\"c\"):(\"d\")))/e}\nprint;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$_=\"abcd\"x25e3;\n$i=<>;\ns/.{$i}//;\nprint \"$&\\n\";\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "src_uid": "94278e9c55f0fc82b48145ebecbc515f"} {"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 \u2014 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 \u2014 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: \u00abforward\u00bb \u2014 if Peter could see such sequences only on the way from A to B; \u00abbackward\u00bb \u2014 if Peter could see such sequences on the way from B to A; \u00abboth\u00bb \u2014 if Peter could see such sequences both on the way from A to B, and on the way from B to A; \u00abfantasy\u00bb \u2014 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": "$_=<>;\n$a=<>;\n$b=<>;\nchomp($a,$b,$_);\n$a1=join '',reverse split'',$a;\n$b1=join '',reverse split'',$b;\nprint \"both\" and exit if /$a.*$b/ and /$b1.*$a1/;\nprint \"forward\" and exit if /$a.*$b/;\nprint \"backward\" and exit if /$b1.*$a1/;\nprint \"fantasy\";\n"}, {"source_code": "$_=<>;\n$a=<>;\n$b=<>;\nchomp($a,$b);\n$a1 = reverse $a;\n$b1 = reverse $b;\nprint \"both\" and exit if /$a.*$b/ and /$b1.*$a1/;\nprint \"forward\" and exit if /$a.*$b/;\nprint \"backward\" and exit if /$b1.*$a1/;\nprint \"fantasy\"\n"}, {"source_code": "$_=<>;\n$a=<>;\n$b=<>;\nchomp($a,$b);\n$a1 = reverse $a;\n$b1 = reverse $b;\nprint \"both\" and exit if /$a.*$b/ and /$b1.*$a1/;\nprint \"forward\" and exit if /$a.*$b/;\nprint \"backward\" and exit if /$b1.*$a1/;\nprint \"fantasy\";\n"}, {"source_code": "$ch=<>;\nchomp$ch;\n$str1=<>;\nchomp$str1;\n$str2=<>;\nchomp$str2;\n$ok1=0,$ok2=0;\nif($ch=~m/$str1.*$str2/){\n$ok1=1;\n}\nif(reverse($ch)=~m/$str1.*$str2/){\n$ok2=1;\n}\nif($ok2+$ok1==2){\nprint \"both\";\n}\nelsif($ok1){\nprint\"forward\";\n}\nelsif($ok2){\nprint\"backward\"\n}\nelse{\nprint \"fantasy\";\n}\n\n"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nmy $s = <>;\nchomp $s;\nmy $s1 = <>;\nchomp $s1;\nmy $s2 = <>;\nchomp $s2;\nmy $fb = 0;\n\n$fb += 1 if ($s =~ m/$s1.*$s2/);\n$s = reverse $s;\n$fb += 2 if ($s =~ m/$s1.*$s2/);\nif ($fb == 3) {\n print \"both\\n\";\n} elsif ($fb == 2) {\n print \"backward\\n\";\n} elsif ($fb == 1) {\n print \"forward\\n\";\n} else {\n print \"fantasy\\n\";\n}\n"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nuse strict;\n\nmy $flags=;chomp($flags);my $one=;chomp($one);my $two=;chomp($two);\nmy $sgalf=reverse $flags;\nif($flags=~/\\A.*?$one.*?$two/&&$sgalf=~/\\A.*?$one.*?$two/){print \"both\\n\";}\nelsif($flags=~/\\A.*?$one.*?$two/){print \"forward\\n\";}\nelsif($sgalf=~/\\A.*?$one.*?$two/){print \"backward\\n\";}\nelse{print \"fantasy\\n\";}\n"}], "negative_code": [{"source_code": "$_=<>;\n$a=<>;\n$b=<>;\nchomp($a,$b,$_);\nprint \"both\" and exit if /$a.*$b/ and /$b.*$a/;\nprint \"forward\" and exit if /$a.*$b/;\nprint \"backward\" and exit if /$b.*$a/;\nprint \"fantasy\";\n"}, {"source_code": "$_=<>;\n$a=<>;\n$b=<>;\nchomp($a,$b);\nprint \"both\" and exit if /$a.*$b/ and /$b.*$a/;\nprint \"forward\" and exit if /$a.*$b/;\nprint \"backward\" and exit if /$b.*$a/;\nprint \"fantasy\";\n"}, {"source_code": "$ch=<>;\nchomp$ch;\n$str1=<>;\nchomp$str1;\n$str2=<>;\nchomp$str2;\n$ok1=0,$ok2=0;\nif($ch=~m/$str1.*$str2/){\n$ok1=1;\n}\nif($ch=~m/$str2.*$str1/){\n$ok2=1;\n}\nif($ok2+$ok1==2){\nprint \"both\";\n}\nelsif($ok1){\nprint\"forward\";\n}\nelsif($ok2){\nprint\"backward\"\n}\nelse{\nprint \"fantasy\";\n}\n\n"}, {"source_code": "$ch=<>;\nchomp$ch;\n$str1=<>;\nchomp$str1;\n$str2=<>;\nchomp$str2;\n$ok1=0,$ok2=0;\nif($ch=~m/$str1.*$str2/){\n$ok1=1;\n}\nif(reverse($ch)=~m/$str2.*$str1/){\n$ok2=1;\n}\nif($ok2+$ok1==2){\nprint \"both\";\n}\nelsif($ok1){\nprint\"forward\";\n}\nelsif($ok2){\nprint\"backward\"\n}\nelse{\nprint \"fantasy\";\n}\n\n"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nuse strict;\n\nmy $flags=;chomp($flags);my $one=;chomp($one);my $two=;chomp($two);\nif($flags!~/$one/||$flags!~/$two/){print \"fantasy\\n\";}\nelse{\n my $sgalf=reverse $flags;\n my $fone=$flags=~s/\\A.*?$one//r;my $ftwo=$sgalf=~s/\\A.*?$one//r;\n if($fone=~/$two/&&$ftwo=~/$two/){print \"both\\n\";}\n elsif($fone!~/$two/&&$ftwo!~/$two/){print \"fantasy\\n\";}\n else{\n\tmy $ans=($fone=~/$two/)?\"forward\\n\":\"backward\\n\";\n\tprint $ans;\n }\n}"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nuse strict;\n\nmy $flags=;chomp($flags);my $one=;chomp($one);my $two=;chomp($two);\nmy $fone=$flags=~s/\\A.*$one//r;my $ftwo=$flags=~s/$fone//r;\nif($flags!~/$one/||$flags!~/$two/){print \"fantasy\\n\";}\nelse{\n if($fone=~/$two/&&$ftwo=~/$two/) {print \"both\\n\";}\n else{\n\tif($fone!~/$two/&&$ftwo!~/$two/){print \"fantasy\\n\";}\n\telse{\n\t if($fone=~/$two/){print \"forward\\n\";}\n\t else{ print \"backward\\n\";}\n\t}\n }\n}"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nuse strict;\n\nmy $flags=;chomp($flags);my $one=;chomp($one);my $two=;chomp($two);\nmy $fone=$flags=~s/\\A.*$one//r;my $ftwo=$flags=~s/$fone//r;\nif($fone=~/$two/&&$ftwo=~/$two/) {print \"both\\n\";}\nelse{\n if($fone!~/$two/&&$ftwo!~/$two/){print \"fantasy\\n\";}\n else{\n\tif($fone=~/$two/){print \"forward\\n\";}\n\telse{ print \"backward\\n\";}\n }\n}"}, {"source_code": "#!usr/bin/perl\nuse warnings;\nuse strict;\n\nmy $flags=;chomp($flags);my $one=;chomp($one);my $two=;chomp($two);\nmy $fone=$flags=~s/\\A.*?$one//r;my $ftwo=$flags=~s/$fone//r;\nif($flags!~/$one/||$flags!~/$two/){print \"fantasy\\n\";}\nelse{\n if($fone=~/$two/&&$ftwo=~/$two/) {print \"both\\n\";}\n else{\n\tif($fone!~/$two/&&$ftwo!~/$two/){print \"fantasy\\n\";}\n\telse{\n\t if($fone=~/$two/){print \"forward\\n\";}\n\t else{ print \"backward\\n\";}\n\t}\n }\n}"}], "src_uid": "c3244e952830643938d51ce14f043d7d"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my @dp = (0); $#dp = $n-1;\r\n \r\n my $mx = -1;\r\n for(my $i=0;$i<$n;$i++){\r\n $dp[$i] += $A[$i];\r\n $mx = $dp[$i] if $dp[$i] > $mx;\r\n my $j = $i + $A[$i];\r\n if( $j < $n ){\r\n $dp[$j] = &max($dp[$j] - 0,$dp[$i]);\r\n }\r\n }\r\n print \"$mx\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my ($x,$y) = @_;\r\n return ( ( $x > $y ) ? $x : $y );\r\n}\r\n"}], "negative_code": [], "src_uid": "ee8ca9b2c6104d1ff9ba1fc39e9df277"} {"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$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nno warnings 'uninitialized';\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } = 1, @_;\n\t\n\tmy $ANS;\n\t\n\tfor( @_ ){\n\t\tmy $ans = 0;\n\t\twhile( /\\B/g ){\n\t\t\t$ans ||= $h{ $` } * $h{ $' };\n\t\t\t}\n\t\t$ANS .= $ans;\n\t\t}\n\t\n\tprint $ANS;\n\t}"}], "negative_code": [], "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\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105)\u00a0\u2014 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": "print substr 'aabb' x 5e4, 0, <>"}, {"source_code": "print substr 'aabb' x ($_ / 4 + 1), 0, $_ for <>"}, {"source_code": " $\\ = $/;\n \n while(<>){\n \tprint substr 'aabb' x ($_ / 4 + 1), 0, $_;\n \n \t}"}], "negative_code": [], "src_uid": "fbde86a29f416b3d83bcc63fb3776776"} {"nl": {"description": "Two pirates Polycarpus and Vasily play a very interesting game. They have n chests with coins, the chests are numbered with integers from 1 to n. Chest number i has ai coins. Polycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer x (2\u00b7x\u2009+\u20091\u2009\u2264\u2009n) and take a coin from each chest with numbers x, 2\u00b7x, 2\u00b7x\u2009+\u20091. It may turn out that some chest has no coins, in this case the player doesn't take a coin from this chest. The game finishes when all chests get emptied.Polycarpus isn't a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his moves, he also counts Vasily's moves.", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of chests with coins. The second line contains a sequence of space-separated integers: a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20091000), where ai is the number of coins in the chest number i at the beginning of the game.", "output_spec": "Print a single integer \u2014 the minimum number of moves needed to finish the game. If no sequence of turns leads to finishing the game, print -1.", "sample_inputs": ["1\n1", "3\n1 2 3"], "sample_outputs": ["-1", "3"], "notes": "NoteIn the first test case there isn't a single move that can be made. That's why the players won't be able to empty the chests.In the second sample there is only one possible move x\u2009=\u20091. This move should be repeated at least 3 times to empty the third chest."}, "positive_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nuse List::Util qw(max);\n\nsub gao {\n my ($n, @a) = @_;\n return -1 if $n == 1 || $n % 2 == 0;\n my $ret = 0;\n for (my $i = $n; $i > 1; $i -= 2) {\n my $tmp = max(0, $a[$i], $a[$i - 1]);\n $ret += $tmp;\n $a[int($i / 2)] -= $tmp;\n }\n return $ret + max(0, $a[1]);\n}\n\nchomp(my $n = <>);\nchomp($_ = <>);\nmy @a = (0, split);\nmy $res = &gao($n, @a);\nprint $res, \"\\n\";\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nuse List::Util qw(max);\n\nsub gao {\n my ($n, @a) = @_;\n if ($n % 2 == 0) {\n if ($a[$n] > 0) {\n return -1;\n }\n --$n;\n }\n my $ret = 0;\n for (my $i = $n; $i > 1; $i -= 2) {\n my $tmp = max(0, $a[$i], $a[$i - 1]);\n $ret += $tmp;\n $a[int($i / 2)] -= $tmp;\n }\n return $a[1] > 0 ? -1 : $ret;\n}\n\nchomp(my $n = <>);\nchomp($_ = <>);\nmy @a = (0, split);\nmy $res = &gao($n, @a);\nprint $res, \"\\n\";\n"}], "src_uid": "42bfc8dbf45f643782bf42be432cb57c"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $k1, $k2 ) = split;\n\t\n\tchomp( my $s1 = <> );\n\tchomp( my $s2 = <> );\n\t\n\tprint $s1 =~ /\\b$n\\b/ ? \"YES\" : \"NO\";\n\t}"}, {"source_code": "$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t( $n ) = split;\n\t\n\t$_ = <>, <>;\n\t\n\tprint /\\b$n\\b/ ? \"YES\" : \"NO\";\n\t}"}], "negative_code": [], "src_uid": "3ef23f114be223255bd10131b2375b86"} {"nl": {"description": "Arya has n opponents in the school. Each day he will fight with all opponents who are present this day. His opponents have some fighting plan that guarantees they will win, but implementing this plan requires presence of them all. That means if one day at least one of Arya's opponents is absent at the school, then Arya will beat all present opponents. Otherwise, if all opponents are present, then they will beat Arya.For each opponent Arya knows his schedule\u00a0\u2014 whether or not he is going to present on each particular day. Tell him the maximum number of consecutive days that he will beat all present opponents.Note, that if some day there are no opponents present, Arya still considers he beats all the present opponents.", "input_spec": "The first line of the input contains two integers n and d (1\u2009\u2264\u2009n,\u2009d\u2009\u2264\u2009100)\u00a0\u2014 the number of opponents and the number of days, respectively. The i-th of the following d lines contains a string of length n consisting of characters '0' and '1'. The j-th character of this string is '0' if the j-th opponent is going to be absent on the i-th day.", "output_spec": "Print the only integer\u00a0\u2014 the maximum number of consecutive days that Arya will beat all present opponents.", "sample_inputs": ["2 2\n10\n00", "4 1\n0100", "4 5\n1101\n1111\n0110\n1011\n1111"], "sample_outputs": ["2", "1", "2"], "notes": "NoteIn the first and the second samples, Arya will beat all present opponents each of the d days.In the third sample, Arya will beat his opponents on days 1, 3 and 4 and his opponents will beat him on days 2 and 5. Thus, the maximum number of consecutive winning days is 2, which happens on days 3 and 4."}, "positive_code": [{"source_code": "($n, $d) = split ' ', <>;\n$d1 = '1' x $n;\nfor (<>) {\n\tchomp; \n\t$period .= ($_ eq $d1? '1': '0');\n}\n$maxlen = 0;\nwhile ($period =~ /0+/g) {\n\t$len = length($&);\n\t$maxlen = $len > $maxlen? $len: $maxlen;\n}\nprint $maxlen, \"\\n\";\n"}, {"source_code": "<>; ( $c += /0/ ? 1 : -$c )\n\t\n\t> $m and $m = $c for <>;\n\nprint 0 + $m"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $d) = split;\n\t$max = 0;\n\t\n\tfor (1 .. $d){\n\t\t$m =()=<>=~ /1/g;\n\n\t\t$m != $n ? \n\t\t\tdo {\n\t\t\t\t$c ++;\n\t\t\t\t$c > $max and $max = $c;\n\t\t\t}\n\t\t:\n\t\t\tdo {\n\t\t\t\t$c = 0;\n\t\t\t\t}\n\t\t\n\t\t}\n\t\n\tprint $max\n\t}"}], "negative_code": [], "src_uid": "a6ee741df426fd2d06fdfda4ca369397"} {"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$$$) \u2014 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": "$n=<>;\nfor(1..$n){\n $d=<>;\n $delta=$d*($d-4);\n if ($delta<0){print \"N\\n\";next;}\n print(\"Y \",($d+sqrt $delta)/2,' ',($d -sqrt $delta)/2,\"\\n\");\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tif( $_ ** 2 < 4 * $_ ){\n\t\tprint 'N';\n\t\t}\n\telse{\n\t\tmy $D = $_ ** 2 - 4 * $_;\n\t\tmy $B = ( $_ + sqrt $D ) / 2;\n\t\tprint join ' ', 'Y', $B, $_ - $B;\n\t\t}\n\t\n\t}"}], "negative_code": [], "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\u2009\u2264\u2009hi\u2009+\u20091 holds for all i from 1 to n\u2009-\u20091.Squidward suggested the following process of sorting castles: Castles are split into blocks\u00a0\u2014 groups of consecutive castles. Therefore the block from i to j will include castles i,\u2009i\u2009+\u20091,\u2009...,\u2009j. 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,\u2009hi\u2009+\u20091,\u2009...,\u2009hj 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of castles Spongebob, Patrick and Squidward made from sand during the day. The next line contains n integers hi (1\u2009\u2264\u2009hi\u2009\u2264\u2009109). 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": "#!/usr/bin/perl\n\n$n = ;\n@d = split(' ', );\n$len = @d;\n$big[0] = $d[0];\n$small[$len - 1] = $d[$len - 1];\nfor($i = 1;$i < $len;$i ++) {\n $big[$i] = $big[$i - 1] > $d[$i]? $big[$i - 1]: $d[$i];\n $small[$len - $i - 1] = $small[$len - $i] < $d[$len - 1 - $i]? $small[$len - $i]: $d[$len - 1 - $i];\n}\n$ans = 1;\nfor($i = 1;$i < $len;$i ++) {\n if ($big[$i - 1] <= $small[$i]) {\n $ans ++;\n }\n}\nprint $ans, \"\\n\";\n\n"}], "negative_code": [], "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$$$) \u2014 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$$$) \u2014 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$$$) \u2014 the number of operations. Then $$$k$$$ lines follows, each contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le n$$$) \u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tchomp;\r\n\t( $A, $B, @A ) = ( ~~<>, ~~<> );\r\n\t\r\n\tif( $A ne $B and $A =~ y/10/01/r ne $B ){\r\n\t\tprint \"NO\";\r\n\t\tnext\r\n\t\t}\r\n\t\r\n\t$A =~ m/1(?{ push @A, \"$+[0] $+[0]\" })(?!)/;\r\n\t\r\n\t1 != ( $A eq $B ) + @A % 2 and \r\n\t\tpush @A, \"1 $_\", \"1 1\", \"2 $_\";\r\n\t\r\n\tprint for YES, 0 + @A, @A\r\n\t}"}, {"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tchomp;\r\n\t( $A, $B, $cA, @A ) = ( ~~<>, ~~<> );\r\n\t\r\n\tif( $A ne $B and $A =~ y/10/01/r ne $B ){\r\n\t\tprint 'NO';\r\n\t\tnext;\r\n\t\t}\r\n\t\r\n\twhile( $A =~ m/1/g ){\r\n\t\t$cA ++;\r\n\t\tpush @A, $-[0] + 1 . \" $+[0]\";\r\n\t\t}\r\n\t\r\n\t1 != ( $A eq $B ) + $cA % 2 and \r\n\t\tpush @A, \"1 $_\", \"1 1\", \"2 $_\";\r\n\t\r\n\tprint for 'YES', 0 + @A, @A;\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\tmy $A = <>;\n\tchomp $A;\n\tmy $B = <>;\n\tchomp $B;\n\t\n\tmy $invA = $A =~ y/10/01/r;\n\t\n\tif( $A ne $B and $invA ne $B ){\n\t\tprint 'NO';\n\t\tnext;\n\t\t}\n\t\n\tprint 'YES';\n\t\n\tmy $cA = () = $A =~ m/1/g;\n\t\n\tmy @ans;\n\t\n\twhile( $A =~ m/1/g ){\n\t\tpush @ans, join ' ', map $_ + 1, $-[0], $+[0] - 1;\n\t\t}\n\t\n\tif( $A eq $B ){\n\t\tif( $cA % 2 == 1 ){\n\t\t\tpush @ans, \"1 $_\", \"1 1\", \"2 $_\";\n\t\t\t}\n\t\telse{\n\t\t\t;\n\t\t\t}\n\t\t}\n\telse{\n\t\tif( $cA % 2 == 1 ){\n\t\t\t;\n\t\t\t}\n\t\telse{\n\t\t\tpush @ans, \"1 $_\", \"1 1\", \"2 $_\";\n\t\t\t}\n\t\t}\n\t\n\tprint for 0 + @ans, @ans;\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tchomp;\r\n\t( $A, $B, $cA, @A ) = ( ~~<>, ~~<> );\r\n\t\r\n\tif( $A ne $B and $A =~ y/10/01/r ne $B ){\r\n\t\tprint 'NO';\r\n\t\tnext;\r\n\t\t}\r\n\t\r\n\twhile( $A =~ m/1/g ){\r\n\t\t$cA ++;\r\n\t\tpush @A, $-[0] + 1 . \" $+[0]\";\r\n\t\t}\r\n\t\r\n\t1 != ( $A eq $B ) + $cA % 2 and \r\n\t\tpush @A, \"1 $_\\n1 1\\n2 $_\";\r\n\t\r\n\tprint for 'YES', 0 + @A, @A;\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\tmy $A = <>;\n\tchomp $A;\n\tmy $B = <>;\n\tchomp $B;\n\t\n\tmy $invA = $A =~ y/10/01/r;\n\t\n\tif( $A ne $B and $invA ne $B ){\n\t\tprint 'NO';\n\t\tnext;\n\t\t}\n\t\n\tprint 'YES';\n\t\n\tmy $cA = () = $A =~ m/1/g;\n\t\n\tmy @ans;\n\t\n\twhile( $A =~ m/1/g ){\n\t\tpush @ans, join ' ', map $_ + 1, $-[0], $+[0] - 1;\n\t\t}\n\t\n\tif( $A eq $B ){\n\t\tif( $cA % 2 == 1 ){\n\t\t\tpush @ans, \"1 $_\", \"1 1\", \"2 $_\";\n\t\t\t}\n\t\telse{\n\t\t\t;\n\t\t\t}\n\t\t}\n\telse{\n\t\tif( $cA % 2 == 1 ){\n\t\t\t;\n\t\t\t}\n\t\telse{\n\t\t\tpush @ans, \"1 $_\", \"1 1\", \"2 $_\";\n\t\t\t}\n\t\t}\n\t\n\tprint for @ans;\n\t}"}], "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$$$)\u00a0\u2014 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$$$)\u00a0\u2014the 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$$$)\u00a0\u2014 side lengths of the rectangles.", "output_spec": "Print $$$t$$$ answers to the test cases. Each answer must be a single integer\u00a0\u2014 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": "my $t=readline STDIN;\nchomp $t;\nwhile ($t) {\n my $line= readline STDIN; chomp $line;\n my ($a,$b)=split / /,$line;\n \n if ($a<$b) { ($b,$a)=($a,$b); }\n if ($a==$b) { print (($a<<1)**2)}\n elsif ($b==1) { print (($a)**2); }\n elsif (($a/2) >= $b) { print ($a**2); }\n else {\n print (($b<<1)**2);\n }\n print \"\\n\";\n $t--;\n}"}, {"source_code": "#perl 5.26.1 \n\n$n = ;\nchomp $n;\n@ip = ;\nchomp @ip;\nforeach (@ip) {\n @test = split(' ', $_);\n if ($test[0] > $test[1]) {\n $dummy = 2*$test[1];\n if ($dummy > $test[0] || $dummy == $test[0]) {\n $ans = $dummy*$dummy;\n print \"$ans\\n\";\n }else {\n $ans = $test[0]*$test[0];\n print \"$ans\\n\";\n } \n }elsif ($test[0] < $test[1]) {\n $dummy = 2*$test[0];\n if ($dummy > $test[1] || $dummy == $test[1]) {\n $ans = $dummy*$dummy;\n print \"$ans\\n\";\n }else {\n $ans = $test[1]*$test[1];\n print \"$ans \\n\";\n }\n }else {\n $ans = (2*$test[0])*(2*$test[0]);\n print \"$ans \\n\";\n }\n}"}], "negative_code": [{"source_code": "my $t=readline STDIN;\nchomp $t;\nwhile ($t) {\n my $line= readline STDIN; chomp $line;\n my ($a,$b)=split / /,$line;\n \n # if ($print ($a<<1)^2,\"\\n\" if ($a==$b);\n \n if ($a<$b) { ($b,$a)=($a,$b); }\n if ($a==$b) { print (($a<<1)**2)}\n elsif (($a/2) >= $b) { print ($a**2); }\n else {\n print (($a+1)**2);\n #print (($a>=$b)?(($b==1)?$a:$b<<1)**2:($b+1)**2);\n }\n print \"\\n\";\n $t--;\n}"}], "src_uid": "3bb093fb17d6b76ae340fab44b08fcb8"} {"nl": {"description": "At many competitions that have a word \u00abcup\u00bb 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 (\u00ab*\u00bb) 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, \u00a0\u2014 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 \u00a0\u2014 any valid table.", "sample_inputs": ["tourist", "MyNameIsLifeIAmForeverByYourSideMyNameIsLife"], "sample_outputs": ["1 7\ntourist", "3 15\nMyNameIsLifeIAm\nForeverByYourSi\ndeMyNameIsL*ife"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $len = length;\n\t\n\t$debug and print $len;\n\t\n\tmy $part = 1;\n\t\n\twhile( $len / $part > 20 ){\n\t\t$part ++;\n\t\t}\n\t\n\t$debug and print $part;\n\t\n\tmy $chunk = $len / $part;\n\t$debug and print $chunk;\n\t\n\tif( $chunk !~ /\\./ ){\n\t\t;\n\t\t}\n\telse{\n\t\tmy $plus = ( 1 + int $chunk ) * $part - $len;\n\t\t\n\t\t$debug and print $plus;\n\t\t\n\t\tmy $int = int $chunk;\n\t\t\n\t\ts/.{$int}\\K/ $plus -- > 0 ? '*' : '' /eg;\n\t\t$chunk = 1 + int $chunk;\n\t\t}\n\t\n\t$debug and print;\n\t\n\t@_ = /.{$chunk}/g;\n\t\n\tprint join ' ', ~~ @_, $chunk;\n\t\n\tprint for @_;\n\t}"}, {"source_code": "$\\ = $/;\n\n<> =~\n\n/\n\t^\n\t(.{0,20})\n\t(?: (?{ $h{ length $1 } ++ }) | (?{ $h{ length $1 } -- }) (*FAIL) )\n\t(.{0,20})\n\t(?: (?{ $h{ length $2 } ++ }) | (?{ $h{ length $2 } -- }) (*FAIL) )\n\t(.{0,20})\n\t(?: (?{ $h{ length $3 } ++ }) | (?{ $h{ length $3 } -- }) (*FAIL) )\n\t(.{0,20})\n\t(?: (?{ $h{ length $4 } ++ }) | (?{ $h{ length $4 } -- }) (*FAIL) )\n\t(.{0,20})\n\t$\n\t(?: (?{ $h{ length $5 } ++ }) | (?{ $h{ length $5 } -- }) (*FAIL) )\n\t(?(?{ @_ = grep $_, grep $h{ $_ }, keys %h; @_ < 3 and 2 > abs( $_[ -1 ] - $_[ 0 ] ) }) | (*FAIL) )\n/x;\n\n@_ = grep length, $1, $2, $3, $4, $5;\n\n$_ = substr $_ . '*' x 100, 0, 99 for @_;\n\nmap chop, @_ while @_ == grep /\\*/, @_;\n\nprint join ' ', ~~ @_, length $_[ 0 ];\n\nprint join \"\\n\", @_"}, {"source_code": "$\\ = $/;\n\n$_ = <>, chomp;\n\n$len = length;\n\n$p = 1;\n\n$p ++ while $len / $p > 20;\n\n$ch = int $len / $p;\n$ch2 = $ch + !! ( $len % $p );\n\n$re = \"(.{$ch,$ch2})\" x $p;\n\n/ ^ $re $ /x;\n\n@_ = map s/(.{$ch2}).?/$1/r, map \"$_*\", \n\tgrep length, $1, $2, $3, $4, $5;\n\nprint join ' ', ~~ @_, length $_[ 0 ];\n\nprint join \"\\n\", @_"}], "negative_code": [{"source_code": "$\\ = $/;\n\n$_ = <>;\n\n$len = length;\n\n$p = 1;\n\n$p ++ while $len / $p > 20;\n\n$ch = int $len / $p;\n$ch2 = $ch + !! ( $len % $p );\n\n$re = \"(.{$ch,$ch2})\" x $p;\n\n/ ^ $re $ /x;\n\n@_ = map s/(.{$ch2}).?/$1/r, map \"$_*\", \n\tgrep length, $1, $2, $3, $4, $5;\n\nprint join ' ', ~~ @_, length $_[ 0 ];\n\nprint join \"\\n\", @_"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $len = length;\n\t\n\t$debug and print $len;\n\t\n\tmy $part = 1;\n\t\n\twhile( $len / $part > 20 ){\n\t\t$part ++;\n\t\t}\n\t\n\t$debug and print $part;\n\t\n\tmy $chunk = $len / $part;\n\t$debug and print $chunk;\n\t\n\tif( $chunk !~ /\\./ ){\n\t\t;\n\t\t}\n\telse{\n\t\tmy $plus = ( 1 + int $chunk ) * $part - $len;\n\t\t\n\t\t$debug and print $plus;\n\t\t\n\t\tmy $int = int $chunk;\n\t\t\n\t\t$plus --, s/.{$int}\\K/\\*/ while $plus;\n\t\t$chunk = 1 + int $chunk;\n\t\t}\n\t\n\t$debug and print;\n\t\n\t@_ = /.{$chunk}/g;\n\t\n\tprint join ' ', ~~ @_, $chunk;\n\t\n\tprint for @_;\n\t}"}, {"source_code": "$\\ = $/;\n\n$len = length;\n\n$p = 1;\n\n$p ++ while $len / $p > 20;\n\n$ch = int $len / $p;\n$ch2 = $ch + !! ( $len % $p );\n\n$re = \"(.{$ch,$ch2})\" x $p;\n\n/ ^ $re $ /x;\n\n@_ = map s/(.{$ch2}).?/$1/r, map \"$_*\", \n\tgrep length, $1, $2, $3, $4, $5;\n\nprint join ' ', ~~ @_, length $_[ 0 ];\n\nprint join \"\\n\", @_"}], "src_uid": "ac047ceede246b40892c80dbd696e6b4"} {"nl": {"description": "Vasya has his favourite number $$$n$$$. He wants to split it to some non-zero digits. It means, that he wants to choose some digits $$$d_1, d_2, \\ldots, d_k$$$, such that $$$1 \\leq d_i \\leq 9$$$ for all $$$i$$$ and $$$d_1 + d_2 + \\ldots + d_k = n$$$.Vasya likes beauty in everything, so he wants to find any solution with the minimal possible number of different digits among $$$d_1, d_2, \\ldots, d_k$$$. Help him!", "input_spec": "The first line contains a single integer $$$n$$$\u00a0\u2014 the number that Vasya wants to split ($$$1 \\leq n \\leq 1000$$$).", "output_spec": "In the first line print one integer $$$k$$$\u00a0\u2014 the number of digits in the partition. Note that $$$k$$$ must satisfy the inequality $$$1 \\leq k \\leq n$$$. In the next line print $$$k$$$ digits $$$d_1, d_2, \\ldots, d_k$$$ separated by spaces. All digits must satisfy the inequalities $$$1 \\leq d_i \\leq 9$$$. You should find a partition of $$$n$$$ in which the number of different digits among $$$d_1, d_2, \\ldots, d_k$$$ will be minimal possible among all partitions of $$$n$$$ into non-zero digits. Among such partitions, it is allowed to find any. It is guaranteed that there exists at least one partition of the number $$$n$$$ into digits.", "sample_inputs": ["1", "4", "27"], "sample_outputs": ["1\n1", "2\n2 2", "3\n9 9 9"], "notes": "NoteIn the first test, the number $$$1$$$ can be divided into $$$1$$$ digit equal to $$$1$$$.In the second test, there are $$$3$$$ partitions of the number $$$4$$$ into digits in which the number of different digits is $$$1$$$. This partitions are $$$[1, 1, 1, 1]$$$, $$$[2, 2]$$$ and $$$[4]$$$. Any of these partitions can be found. And, for example, dividing the number $$$4$$$ to the digits $$$[1, 1, 2]$$$ isn't an answer, because it has $$$2$$$ different digits, that isn't the minimum possible number."}, "positive_code": [{"source_code": " \nuse 5.20.1;\nuse strict;\nmy $n = ;\nsay $n;\nsay \"1\" until ($n-- < 1);"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\tprint for $_, join ' ', ( 1 ) x $_;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = sort { $a <=> $b } split ' ', <>;\n\t\n\tmy @cand;\n\t\n\tfor my $cand ( 1 .. 100 ){\n\t\tmy $sum = 0;\n\t\tfor( @_ ){\n\t\t\tmy $x = abs( $cand - $_ ) - 1;\n\t\t\t$x < 0 and $x = 0;\n\t\t\t$sum += $x;\n\t\t\t}\n\t\tpush @cand, [ $cand, $sum ];\n\t\t}\n\t\n\tprint \"@{ ( sort { $a->[ 1 ] <=> $b->[ 1 ] } @cand )[ 0 ] }\";\n\t}"}], "src_uid": "7c483498f497f4291e3d33375c0ebd53"} {"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.\u2009e. $$$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.\u2009e. you must minimize $$$|c_1-c_2|$$$).", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) \u2014 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$$$) \u2014 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": "for (1..<>) {\r\n chomp (my $n = <>);\r\n if ($n%3 == 0) {\r\n print $n/3, \" \", $n/3;\r\n } elsif ($n%3 == 1) {\r\n print int($n/3)+1, \" \", int($n/3);\r\n } elsif ($n%3 == 2) {\r\n print int($n/3), \" \", int($n/3)+1;\r\n }\r\n print \"\\n\";\r\n}\r\nexit;"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $A = int $_ / 3;\n\tmy $B = $_ % 3;\n\t\n\tprint join ' ', do {\n\t\tif( 0 ){ ; }\n\t\telsif( $B == 0 ){\n\t\t\t$A + 0, $A + 0\n\t\t\t}\n\t\telsif( $B == 1 ){\n\t\t\t$A + 1, $A + 0\n\t\t\t}\n\t\telsif( $B == 2 ){\n\t\t\t$A + 0, $A + 1\n\t\t\t}\n\t\t};\n\t}"}], "negative_code": [], "src_uid": "71335a9489f0985f4e16435b14d6a34a"} {"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 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the initial number of compilation errors. The second line contains n space-separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 the errors the compiler displayed for the first time. The third line contains n\u2009-\u20091 space-separated integers b1,\u2009b2,\u2009...,\u2009bn\u2009-\u20091 \u2014 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\u2009-\u20092 space-separated integers \u04411,\u2009\u04412,\u2009...,\u2009\u0441n\u2009-\u20092 \u2014 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 perl\n\nuse strict;\nuse warnings;\nno warnings 'uninitialized';\n\n\nmy $N = <>+0;\n\nmy $line = <>;\nchomp $line;\nmy @A = split ' ', $line;\nmy %S = ();\nforeach my $x (@A) {\n ++$S{ int($x) };\n}\n\n$line = <>;\nchomp $line;\nmy @B = split ' ', $line;\nforeach my $x (@B) {\n --$S{ int($x) };\n delete $S{ int($x) } unless $S{ int($x) }\n}\nforeach my $k (keys(%S)) {\n print $k, \"\\n\"\n}\n\n%S = ();\nforeach my $x (@B) {\n ++$S{ int($x) };\n}\n\n$line = <>;\nchomp $line;\nmy @C = split ' ', $line;\nforeach my $x (@C) {\n --$S{ int($x) };\n delete $S{ int($x) } unless $S{ int($x) }\n}\nforeach my $k (keys(%S)) {\n print $k, \"\\n\"\n}\n"}, {"source_code": "<>;\nmy %hash;\nmy %hash2;\n$hash{int$_}++ for(split / /, <>);\n$hash2{int$_}++, $hash{int$_}-- for(split / /, <>);\n$hash2{int$_}-- for(split / /, <>);\nprint grep{$hash{$_} == 1} keys %hash;\nprint \"\\n\";\nprint grep{$hash2{$_} == 1} keys %hash2;"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = sort { $a <=> $b } split / /, <>;\n@b = sort { $a <=> $b } split / /, <>;\n@c = sort { $a <=> $b } split / /, <>;\nfor ($i=0; $i<$n-1; $i++) {\n\t$a[$i]!=$b[$i] and say $a[$i] and last;\n}\n$i>=$n-1 and say $a[$n-1];\nfor ($i=0; $i<$n-2; $i++) {\n\t$b[$i]!=$c[$i] and say $b[$i] and last;\n}\n$i>=$n-2 and say $b[$n-2];\n"}, {"source_code": "chomp (my @s = split /\\s+/, );\nchomp (my @i = sort {$a <=> $b} split /\\s+/, );\nchomp (my @j = sort {$a <=> $b} split /\\s+/, );\nchomp (my @k = sort {$a <=> $b} split /\\s+/, );\n\nmy $x;\nfor ($x = 0; $x < @j; $x++) {\n if ($i[$x] != $j[$x]) {\n printf \"$i[$x]\\n\";\n last;\n }\n}\nprintf \"$i[$x]\\n\" if ($x == @j);\nfor ($x = 0; $x < @k; $x++) {\n if ($j[$x] != $k[$x]) {\n printf \"$j[$x]\\n\";\n last;\n }\n}\nprintf \"$j[$x]\\n\" if ($x == @k);\n"}, {"source_code": "use strict;\nuse warnings;\n\n<>;\nmy (%a, %b, %c);\n\nfor my $ref (\\%a, \\%b, \\%c) {\n for my $elt (split ' ', <>) {\n $$ref{$elt}++;\n }\n}\n\nmy ($x) = grep { ($b{$_} || 0) != $a{$_} } keys %a;\nmy ($y) = grep { ($c{$_} || 0) != $b{$_} } keys %b;\n\n$\\ = \"\\n\";\nprint $x;\nprint $y;\n"}, {"source_code": "$n = <>;\n@ar = sort {$a <=> $b} split /\\s+/, <>;\n@br = sort {$a <=> $b} split /\\s+/, <>; push(@br, 2**30);\nfor ($i = 0; $i < $n; ++$i) {\n\tif ($ar[$i] != $br[$i]) {\n\t\tprint $ar[$i], \"\\n\";\n\t\tlast;\n\t}\n}\n@cr = sort {$a <=> $b} split /\\s+/, <>; push(@cr, 2**30);\nfor ($i = 0; $i < $n - 1; ++$i) {\n\tif ($br[$i] != $cr[$i]) {\n\t\tprint $br[$i], \"\\n\";\n\t\tlast;\n\t}\n}"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nsub get_missing{\n my ($total, $cur) = ($_[0], $_[1]);\n my %hash1; my %hash2;\n foreach(@{$total}){\n $hash1{$_} = 0 unless exists $hash1{$_};\n $hash2{$_} = 0 unless exists $hash2{$_};\n $hash1{$_}++;\n }\n foreach(@{$cur}){\n $hash2{$_}++;\n }\n foreach(keys %hash1){\n return $_ if $hash2{$_} < $hash1{$_};\n }\n return -1;\n}\n###############################\n\nmy $n = <>;\nmy @total = split ' ',<>;\nmy @cur = split ' ' , <>;\nmy $res1 = get_missing(\\@total,\\@cur);\n\n@total = @cur;\n@cur = split ' ' , <>;\nmy $res2 = get_missing(\\@total,\\@cur);\n\nprint \"$res1\\n$res2\";\n"}, {"source_code": "<>;\n$sum1 += $_ for split(/ /, <>);\n$sum2 += $_ for split(/ /, <>);\n$sum3 += $_ for split(/ /, <>);\nprint(($sum1-$sum2).\"\\n\".($sum2-$sum3).\"\\n\");"}, {"source_code": "<>;\nprint join $/, map eval, (join '-', map {eval join '+', split} <>) =~ /(?=(\\d+-\\d+))/g"}, {"source_code": "<>;\n@a = sort {$a <=> $b} split \" \", <>;\n@b = split \" \", <>;\n@c = sort {$a <=> $b} split \" \", <>;\n$i = 0;\nfor (@a){\n$_ == $c[$i] ? ($i++) : (push @ans, $_)\n}\n$aa = $bb = 0;\n$ans[0] == $_ and $aa++ for @a;\n$ans[0] == $_ and $bb++ for @b;\n$, = $/;\nprint $aa == $bb ? (reverse @ans): @ans"}], "negative_code": [{"source_code": "<>;\nmy %hash;\n$hash{int$_}++ for(split / /, <>);\n$hash{int$_}++ for(split / /, <>);\n$hash{int$_}++ for(split / /, <>);\nprint grep{$hash{$_} == 1} keys %hash;\nprint \"\\n\";\nprint grep{$hash{$_} == 2} keys %hash;\n"}, {"source_code": "<>;\nmy $n = <>;\nmy %hash;\n$hash{int$_}++ for(split / /, <>);\n$hash{int$_}++ for(split / /, <>);\n$hash{int$_}++ for(split / /, <>);\nprint grep{$hash{$_} == 1} keys %hash;\nprint \"\\n\";\nprint grep{$hash{$_} == 2} keys %hash;\n"}, {"source_code": "<>;\n@a = sort {$a <=> $b} split \" \", <>;\n@b = split \" \", <>;\n@c = sort {$a <=> $b} split \" \", <>;\n$i = 0;\nfor (@a){\n$_ == $c[$i] ? ($i++) : (push @ans, $_)\n}\n$f = 0;\n$ans[0] == $_ and $f++ for @b;\n$, = $/;\nprint $f ? (reverse @ans): @ans"}], "src_uid": "1985566215ea5a7f22ef729bac7205ed"} {"nl": {"description": "Pavel loves grid mazes. A grid maze is an n\u2009\u00d7\u2009m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side.Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactly k empty cells into walls so that all the remaining cells still formed a connected area. Help him.", "input_spec": "The first line contains three integers n, m, k (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009500, 0\u2009\u2264\u2009k\u2009<\u2009s), where n and m are the maze's height and width, correspondingly, k is the number of walls Pavel wants to add and letter s represents the number of empty cells in the original maze. Each of the next n lines contains m characters. They describe the original maze. If a character on a line equals \".\", then the corresponding cell is empty and if the character equals \"#\", then the cell is a wall.", "output_spec": "Print n lines containing m characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as \"X\", the other cells must be left without changes (that is, \".\" and \"#\"). It is guaranteed that a solution exists. If there are multiple solutions you can output any of them.", "sample_inputs": ["3 4 2\n#..#\n..#.\n#...", "5 4 5\n#...\n#.#.\n.#..\n...#\n.#.#"], "sample_outputs": ["#.X#\nX.#.\n#...", "#XXX\n#X#.\nX#..\n...#\n.#.#"], "notes": null}, "positive_code": [{"source_code": "use strict;\n#use warnings;\nuse 5.010;\n\nmy ($n, $m, $k) = split \" \", <>;\nmy @board;\nmy ($s, $visited) = (0, 0);\nmy ($bi, $bj);\n\nsub DFS {\n my ($i, $j) = @_;\n #say \"in func \", $i, \" \", $j;\n #say $visited, $s, $k;\n if($visited == $s - $k){\n return;\n }\n $board[$i][$j] = '0';\n $visited++;\n if($i > 0 && $board[$i-1][$j] eq '.') {\n DFS($i-1, $j);\n }\n if($i < $n - 1 && $board[$i+1][$j] eq '.') {\n DFS($i+1, $j);\n }\n if($j > 0 && $board[$i][$j-1] eq '.') {\n DFS($i, $j-1);\n }\n if($j < $m - 1 && $board[$i][$j+1] eq '.') {\n DFS($i, $j+1);\n }\n return;\n}\n\nfor (<>) {\n chomp;\n push @board, [split //];\n}\n\nfor my $row (@board) {\n for my $cell (@$row) {\n if($cell eq '.') {\n $s++;\n }\n }\n}\n\n\nfor my $i (0..$n) {\n for my $j (0..$m) {\n if($board[$i][$j] eq '.') {\n ($bi, $bj) = ($i, $j);\n }\n }\n}\n\n#say $s, \" \", $k;\n#print_board();\nDFS($bi, $bj);\n#print_board();\n\n\nfor my $row (@board) {\n for my $cell (@$row) {\n if($cell eq '0') {\n $cell = '.';\n }\n elsif($cell eq '.') {\n $cell = 'X';\n }\n }\n}\n\nprint_board();\n\n\nsub print_board {\n for (@board) {\n say @$_;\n }\n}\n\n\n\n"}], "negative_code": [{"source_code": "use strict;\n#use warnings;\nuse 5.010;\n\nmy ($n, $m, $k) = split \" \", <>;\nmy @board;\nmy ($s, $visited) = (0, 0);\nmy ($bi, $bj);\n\nsub DFS {\n my ($i, $j) = @_;\n #say \"in func \", $i, \" \", $j;\n #say $visited, $s, $k;\n if($visited == $s - $k){\n return;\n }\n $board[$i][$j] = '0';\n $visited++;\n if($i > 0 && $board[$i-1][$j] eq '.') {\n DFS($i-1, $j);\n }\n if($i < $n - 1 && $board[$i+1][$j] eq '.') {\n DFS($i+1, $j);\n }\n if($j > 0 && $board[$i][$j-1] eq '.') {\n DFS($i, $j-1);\n }\n if($j < $m - 1 && $board[$i][$j+1] eq '.') {\n DFS($i, $j+1);\n }\n return;\n}\n\nfor (<>) {\n chomp;\n push @board, [split //];\n}\n\nfor my $row (@board) {\n for my $cell (@$row) {\n if($cell eq '.') {\n $s++;\n }\n }\n}\n\n\nfor my $i (0..$n) {\n for my $j (0..$m) {\n if($board[$i][$j] eq '.') {\n ($bi, $bj) = ($i, $j);\n }\n }\n}\n\n#say $s, \" \", $k;\n#print_board();\nDFS($bi, $bj);\n#print_board();\n\n\nfor my $row (@board) {\n for my $cell (@$row) {\n if($cell ne '0') {\n $cell = '#';\n }\n else {\n $cell = '.';\n }\n }\n}\n\nprint_board();\n\n\nsub print_board {\n for (@board) {\n say @$_;\n }\n}\n\n\n\n"}], "src_uid": "3aba4a129e24ca2f06f5f3ef13415b8b"} {"nl": {"description": "Vasya has the square chessboard of size n\u2009\u00d7\u2009n 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000, 1\u2009\u2264\u2009m\u2009\u2264\u2009min(100\u2009000,\u2009n2))\u00a0\u2014 the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009n)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse strict;\n\nmy ($n, $m) = split (\" \",);\nmy (%rows,%cols,@result);\nfor (my $i=0;$i<$m;$i++){\n my ($row, $col) = split(\" \",);\n $rows{$row} = 1;\n $cols{$col} = 1;\n push @result,(($n - keys(%rows))*($n - keys(%cols)));\n}\nprint join \" \", @result;\n\n"}, {"source_code": "$\\ = $/;\n$n = <>;\n\nfor (<>){\n\t($x, $y) = split;\n\t$X{$x} ++;\n\t$Y{$y} ++;\n\tprint eval join '*', map {$n - keys %$_} \\%X, \\%Y;\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m) = split;\n\tfor $i (1 .. $m){\n\t\t($x, $y) = split ' ', <>;\n\t\t$X{$x} ++;\n\t\t$Y{$y} ++;\n\t\tprint +($n - keys %X) * ($n - keys %Y);\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "faf1abdeb6f0cf34972d5cb981116186"} {"nl": {"description": "Tomorrow is a difficult day for Polycarp: he has to attend $$$a$$$ lectures and $$$b$$$ practical classes at the university! Since Polycarp is a diligent student, he is going to attend all of them.While preparing for the university, Polycarp wonders whether he can take enough writing implements to write all of the lectures and draw everything he has to during all of the practical classes. Polycarp writes lectures using a pen (he can't use a pencil to write lectures!); he can write down $$$c$$$ lectures using one pen, and after that it runs out of ink. During practical classes Polycarp draws blueprints with a pencil (he can't use a pen to draw blueprints!); one pencil is enough to draw all blueprints during $$$d$$$ practical classes, after which it is unusable.Polycarp's pencilcase can hold no more than $$$k$$$ writing implements, so if Polycarp wants to take $$$x$$$ pens and $$$y$$$ pencils, they will fit in the pencilcase if and only if $$$x + y \\le k$$$.Now Polycarp wants to know how many pens and pencils should he take. Help him to determine it, or tell that his pencilcase doesn't have enough room for all the implements he needs tomorrow!Note that you don't have to minimize the number of writing implements (though their total number must not exceed $$$k$$$).", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) \u2014 the number of test cases in the input. Then the test cases follow. Each test case is described by one line containing five integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ and $$$k$$$, separated by spaces ($$$1 \\le a, b, c, d, k \\le 100$$$) \u2014 the number of lectures Polycarp has to attend, the number of practical classes Polycarp has to attend, the number of lectures which can be written down using one pen, the number of practical classes for which one pencil is enough, and the number of writing implements that can fit into Polycarp's pencilcase, respectively. 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 the answer as follows: If the pencilcase can't hold enough writing implements to use them during all lectures and practical classes, print one integer $$$-1$$$. Otherwise, print two non-negative integers $$$x$$$ and $$$y$$$ \u2014 the number of pens and pencils Polycarp should put in his pencilcase. If there are multiple answers, print any of them. Note that you don't have to minimize the number of writing implements (though their total number must not exceed $$$k$$$).", "sample_inputs": ["3\n7 5 4 5 8\n7 5 4 5 2\n20 53 45 26 4"], "sample_outputs": ["7 1\n-1\n1 3"], "notes": "NoteThere are many different answers for the first test case; $$$x = 7$$$, $$$y = 1$$$ is only one of them. For example, $$$x = 3$$$, $$$y = 1$$$ is also correct.$$$x = 1$$$, $$$y = 3$$$ is the only correct answer for the third test case."}, "positive_code": [{"source_code": "sub ans {\n my ($a, $b, $c, $d, $k) = @_;\n $x = int($a / $c) + ($a % $c != 0);\n $y = int($b / $d) + ($b % $d != 0);\n if ($x + $y <= $k) {\n print \"$x $y\\n\";\n } else {\n print \"-1\\n\";\n }\n}\n\nmy $t = ;\nfor (local $i = 0; $i < $t; $i++) {\n my @par = split \" \", ;\n ans(@par);\n}\n"}], "negative_code": [], "src_uid": "17cf2d6f59773925b228188b5a47b710"} {"nl": {"description": "Madoka is a very strange girl, and therefore she suddenly wondered how many pairs of integers $$$(a, b)$$$ exist, where $$$1 \\leq a, b \\leq n$$$, for which $$$\\frac{\\operatorname{lcm}(a, b)}{\\operatorname{gcd}(a, b)} \\leq 3$$$.In this problem, $$$\\operatorname{gcd}(a, b)$$$ denotes the greatest common divisor of the numbers $$$a$$$ and $$$b$$$, and $$$\\operatorname{lcm}(a, b)$$$ denotes the smallest common multiple of the numbers $$$a$$$ and $$$b$$$.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases. Description of the test cases follows. The first and the only line of each test case contains the integer $$$n$$$ ($$$1 \\le n \\le 10^8$$$).", "output_spec": "For each test case output a single integer \u2014 the number of pairs of integers satisfying the condition.", "sample_inputs": ["6\n\n1\n\n2\n\n3\n\n4\n\n5\n\n100000000"], "sample_outputs": ["1\n4\n7\n10\n11\n266666666"], "notes": "NoteFor $$$n = 1$$$ there is exactly one pair of numbers\u00a0\u2014 $$$(1, 1)$$$ and it fits.For $$$n = 2$$$, there are only $$$4$$$ pairs\u00a0\u2014 $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$ and they all fit.For $$$n = 3$$$, all $$$9$$$ pair are suitable, except $$$(2, 3)$$$ and $$$(3, 2)$$$, since their $$$\\operatorname{lcm}$$$ is $$$6$$$, and $$$\\operatorname{gcd}$$$ is $$$1$$$, which doesn't fit the condition."}, "positive_code": [{"source_code": "<>;print\" \",$_+2*(-1&$_/2+(-1&$_/3))for(<>)"}, {"source_code": "<>;print\" \",$_+2*(-1&$_/2+(-1&$_/3))for(<>)\r\n"}, {"source_code": "<>;print\" \",$_+2*(-1&$_/2+(-1&$_/3))for(<>)"}], "negative_code": [], "src_uid": "a6b6d9ff2ac5c367c00a64a387cc9e36"} {"nl": {"description": "You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase?", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) \u2014 the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 \\le |s| \\le 100$$$); each character of $$$s$$$ is either 0 or 1.", "output_spec": "Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$).", "sample_inputs": ["3\n010011\n0\n1111000"], "sample_outputs": ["2\n0\n0"], "notes": "NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111)."}, "positive_code": [{"source_code": "#!/bin/perl\n\nuse strict;\n\nmy $cases = +<>;\n\nwhile ($cases--) {\n my $line = <>;\n\n my ($a, $b, $found1) = (0, 0, 0);\n for (my $i = 0; $i < length($line) - 1; $i++) {\n if (substr($line, $i, 1) eq '1') {\n $found1 = 1;\n $b = $a;\n } elsif ($found1) {\n $a++;\n }\n }\n print \"$b\\n\";\n}\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nuse Carp;\n\n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\nsub ref_ref_scalar {\n my $ref = shift;\n return 1 if ref($ref) eq 'SCALAR';\n}\n\nsub toggle {\n my $ref = shift;\n croak \"$ref does not reference to scalar\" if !ref_ref_scalar($ref);\n\n $$ref = !$$ref;\n}\n\nsub odd {\n my $num = shift;\n return $num % 2 == 1;\n}\n\nsub even {\n my $num = shift;\n return $num % 2 == 0;\n}\n\nsub sum_of_digits {\n my $n = shift;\n my @numbers = split q{}, $n;\n\n my $sum = 0;\n\n for (@numbers) {\n $sum += $_;\n }\n\n return $sum;\n}\n\n# solution\n\nsub count_zero {\n my $str = shift;\n my @chars = split q{}, $str;\n my $count = 0;\n for (@chars) {\n $count++ if $_ eq '0';\n }\n return $count;\n}\n\nmy $n = read_token;\n\nfor (1..$n) {\n my $s = read_line;\n while ($s and substr($s, 0, 1) eq '0') {\n $s = substr($s, 1);\n }\n if (!$s) {\n say 0;\n next;\n }\n\n while ($s and substr($s, length($s)-1, 1) eq '0') {\n $s = substr($s, 0, length($s)-1);\n }\n\n say count_zero($s);\n}\n"}, {"source_code": "<>;print 0 + ( () = s/^0+|0+$//gr =~ /0/g ),$/ for <>"}, {"source_code": "<>; print 0 + ( () = s/^0+|0+$//gr =~ /0/g ), $/ for <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\ts/^0+//;\n\ts/0+$//;\n\t\n\tprint length y/1//dr;\n\t}"}], "negative_code": [], "src_uid": "5de66fbb594bb317654366fd2290c4d3"} {"nl": {"description": "Nastia has $$$2$$$ positive integers $$$A$$$ and $$$B$$$. She defines that: The integer is good if it is divisible by $$$A \\cdot B$$$; Otherwise, the integer is nearly good, if it is divisible by $$$A$$$. For example, if $$$A = 6$$$ and $$$B = 4$$$, the integers $$$24$$$ and $$$72$$$ are good, the integers $$$6$$$, $$$660$$$ and $$$12$$$ are nearly good, the integers $$$16$$$, $$$7$$$ are neither good nor nearly good.Find $$$3$$$ different positive integers $$$x$$$, $$$y$$$, and $$$z$$$ such that exactly one of them is good and the other $$$2$$$ are nearly good, and $$$x + y = z$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10\\,000$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains two integers $$$A$$$ and $$$B$$$ ($$$1 \\le A \\le 10^6$$$, $$$1 \\le B \\le 10^6$$$)\u00a0\u2014 numbers that Nastia has.", "output_spec": "For each test case print: \"YES\" and $$$3$$$ different positive integers $$$x$$$, $$$y$$$, and $$$z$$$ ($$$1 \\le x, y, z \\le 10^{18}$$$) such that exactly one of them is good and the other $$$2$$$ are nearly good, and $$$x + y = z$$$. \"NO\" if no answer exists. YES NO If there are multiple answers, print any.", "sample_inputs": ["3\n5 3\n13 2\n7 11"], "sample_outputs": ["YES\n10 50 60\nYES\n169 39 208\nYES\n28 154 182"], "notes": "NoteIn the first test case: $$$60$$$\u00a0\u2014 good number; $$$10$$$ and $$$50$$$\u00a0\u2014 nearly good numbers.In the second test case: $$$208$$$\u00a0\u2014 good number; $$$169$$$ and $$$39$$$\u00a0\u2014 nearly good numbers.In the third test case: $$$154$$$\u00a0\u2014 good number; $$$28$$$ and $$$182$$$\u00a0\u2014 nearly good numbers."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($A,$B) = map { $_ - 0 } split(/\\s+/,);\r\n if( $B == 1 ){\r\n print \"NO\\n\"; next;\r\n }\r\n #my $g = &gcd($A,$B);\r\n my $z = $B * $A;\r\n my $x = 1 * $A;\r\n my $y = ($B - 1) * $A;\r\n if( $B == 2 ){\r\n $z = 4 * $A;\r\n $x = $A;\r\n $y = 3 * $A;\r\n }\r\n print \"YES\\n\";\r\n print \"$x $y $z\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub gcd { # gcd\r\n my ($x,$y) = @_;\r\n return $x if $y == 0;\r\n return &gcd($y, $x % $y);\r\n}\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($A,$B) = map { $_ - 0 } split(/\\s+/,);\r\n if( $A % $B == 0 ){\r\n print \"NO\\n\"; next;\r\n }\r\n my $z = $B * $A;\r\n my $x = 1 * $A;\r\n my $y = ($B - 1) * $A;\r\n if( $B == 2 ){\r\n $z = 4 * $A;\r\n $x = $A;\r\n $y = 3 * $A;\r\n }\r\n print \"YES\\n\";\r\n print \"$x $y $z\\n\";\r\n}\r\n\r\nexit(0);\r\n"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($A,$B) = map { $_ - 0 } split(/\\s+/,);\r\n if( $B == 1 ){\r\n print \"NO\\n\"; next;\r\n }\r\n my $z = $B * $A;\r\n my $x = 1 * $A;\r\n my $y = ($B - 1) * $A;\r\n print \"YES\\n\";\r\n print \"$x $y $z\\n\";\r\n}\r\n\r\nexit(0);\r\n"}], "src_uid": "f10aa45956e930df3df0e23f2592c8f1"} {"nl": {"description": "You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$)\u00a0\u2014 the number of input data sets in the test. The description of each set of input data consists of three lines. The first line contains an integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$)\u00a0\u2014 the length of the original array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \\leq a_i \\leq 10^9$$$)\u00a0\u2014 the array elements themselves. The third line has length $$$n$$$ and consists exclusively of the letters 'B' and/or 'R': $$$i$$$th character is 'B' if $$$a_i$$$ is colored blue, and is 'R' if colored red. It is guaranteed that the sum of $$$n$$$ over all input sets does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "Print $$$t$$$ lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise. You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).", "sample_inputs": ["8\n4\n1 2 5 2\nBRBR\n2\n1 1\nBB\n5\n3 1 4 2 5\nRBRRB\n5\n3 1 3 1 3\nRBRRB\n5\n5 1 5 1 5\nRBRRB\n4\n2 2 2 2\nBRBR\n2\n1 -2\nBR\n4\n-2 -1 4 0\nRRRR"], "sample_outputs": ["YES\nNO\nYES\nYES\nNO\nYES\nYES\nYES"], "notes": "NoteIn the first test case of the example, the following sequence of moves can be performed: choose $$$i=3$$$, element $$$a_3=5$$$ is blue, so we decrease it, we get $$$a=[1,2,4,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,3,4,2]$$$; choose $$$i=3$$$, element $$$a_3=4$$$ is blue, so we decrease it, we get $$$a=[1,3,3,2]$$$; choose $$$i=2$$$, element $$$a_2=2$$$ is red, so we increase it, we get $$$a=[1,4,3,2]$$$. We got that $$$a$$$ is a permutation. Hence the answer is YES."}, "positive_code": [{"source_code": "my $line;\r\nmy @input1; my @input2;\r\n\r\n$line = ;\r\nchomp($line);\r\n\r\nmy $line2; my $line3; my $len; my @data;\r\nfor(1..$line) {\r\n\r\n $len = ;\r\n chomp($len);\r\n\r\n $line2 = ;\r\n chomp($line2);\r\n $line3 = ;\r\n chomp($line3);\r\n @input1 = (); @input2 = (); @data = ();\r\n\r\n @input1 = split / /, $line2;\r\n @input2 = split //, $line3;\r\n my $flag = 0;\r\n\r\n for (0..$len-1) {\r\n if( @input2[$_] eq \"B\") {\r\n if (@input1[$_] < 1) {\r\n $flag = 1; last;\r\n }\r\n if (@input1[$_] > $len){\r\n @input1[$_] = $len;\r\n }\r\n $data[0][@input1[$_]]++;\r\n }\r\n if(@input2[$_] eq \"R\") {\r\n if (@input1[$_] > $len){\r\n $flag = 1; last;\r\n }\r\n if (@input1[$_] < 1) {\r\n @input1[$_] = 1;\r\n }\r\n $data[1][@input1[$_]]++;\r\n }\r\n }\r\n my $s1 = 0; my $s2 = 0;\r\n for (my $i = 1; $i <= $len; $i++) {\r\n $s1 += $data[0][$i];\r\n if ($s1 > $i) {\r\n $flag = 1; last;\r\n }\r\n $s2 += $data[1][$len - $i + 1];\r\n if ($s2 > $i) {\r\n $flag = 1; last;\r\n }\r\n\r\n }\r\n if (!$flag) {print \"YES\", \"\\n\";} else { print \"NO\", \"\\n\"; }\r\n}"}], "negative_code": [{"source_code": "my $line;\r\nmy @input1; my @input2;\r\n\r\n$line = ;\r\nchomp($line);\r\n\r\nmy $line2; my $line3; my $len; my @data;\r\nfor(1..$line) {\r\n\r\n $len = ;\r\n chomp($len);\r\n\r\n $line2= ;\r\n chomp($line2);\r\n $line3 = ;\r\n chomp($line3);\r\n \r\n @input1 = (); @input2 = (); @data = ();\r\n\r\n @input1 = split / /, $line2;\r\n @input2 = split //, $line3;\r\n my $flag = 0;\r\n\r\n for (0..$len-1) {\r\n if (@input1[$_] < 1){if( @input2[$_] eq \"B\") { $flag = 1;} else { @input1[$_] = 1; }}\r\n if (@input1[$_] > $len){if(@input2[$_] eq \"R\") { $flag = 1;} else { @input2[$_] = $len; }}\r\n $data[$_][0] = @input1[$_];\r\n $data[$_][1] = ((ord(@input2[$_]) % 11)%2) * ($len - 1) + 1;\r\n if ($data[$_][1] < $data[$_][0]) {\r\n my $tmp = $data[$_][0];\r\n $data[$_][0] = $data[$_][1];\r\n $data[$_][1] = $tmp;\r\n }\r\n }\r\n @data = sort {$a->[1] <=> $b->[1]} @data;\r\n @data = sort {$a->[0] <=> $b->[0]} @data;\r\n\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $data[$i][0] || $i + 1 > $data[$i][1]) {\r\n $flag = 1;\r\n last;\r\n }\r\n }\r\n if (!$flag) {print \"YES\", \"\\n\";} else { print \"NO\", \"\\n\"; }\r\n}"}, {"source_code": "my $line;\r\nmy @input1; my @input2;\r\n\r\n$line = ;\r\nchomp($line);\r\n\r\nmy $line2; my $line3; my $len; my @data;\r\nfor(1..$line) {\r\n\r\n $len = ;\r\n chomp($len);\r\n\r\n $line2= ;\r\n chomp($line2);\r\n $line3 = ;\r\n chomp($line3);\r\n\r\n @input1 = split / /, $line2;\r\n @input2 = split //, $line3;\r\n my $flag = 0;\r\n\r\n for (0..$len-1) {\r\n if (@input1[$_] < 1){if( @input2[$_] eq \"B\") { $flag = 1;} else { @input1[$_] = 1; }}\r\n if (@input1[$_] > $len){if(@input2[$_] eq \"R\") { $flag = 1;} else { @input2[$_] = $len; }}\r\n $data[$_][0] = @input1[$_];\r\n $data[$_][1] = ((ord(@input2[$_]) % 11)%2) * ($len - 1) + 1;\r\n if ($data[$_][1] < $data[$_][0]) {\r\n my $tmp = $data[$_][0];\r\n $data[$_][0] = $data[$_][1];\r\n $data[$_][1] = $tmp;\r\n }\r\n }\r\n @data = sort {$a->[1] <=> $b->[1]} @data;\r\n @data = sort {$a->[0] <=> $b->[0]} @data;\r\n\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $data[$i][0] || $i + 1 > $data[$i][1]) {\r\n $flag = 1;\r\n last;\r\n }\r\n }\r\n if (!$flag) {print \"YES\", \"\\n\";} else { print \"NO\", \"\\n\"; }\r\n}"}, {"source_code": "my $line;\r\nmy @input1; my @input2;\r\n\r\n$line = ;\r\nchomp($line);\r\n\r\nmy $line2; my $line3; my $len; my @data;\r\nfor(1..$line) {\r\n\r\n $len = ;\r\n chomp($len);\r\n\r\n $line2= ;\r\n chomp($line2);\r\n $line3 = ;\r\n chomp($line3);\r\n\r\n @input1 = split / /, $line2;\r\n @input2 = split //, $line3;\r\n my $flag = 0;\r\n\r\n for (0..$len-1) {\r\n if (@input1[$_] < 1){if( @input2[$_] eq \"B\") { $flag = 1;} else { @input1[$_] = 1; }}\r\n if (@input1[$_] > $len){if(@input2[$_] eq \"R\") { $flag = 1;} else { @input2[$_] = $len; }}\r\n $data[$_][0] = @input1[$_];\r\n $data[$_][1] = ((ord(@input2[$_]) % 11)%2) * ($len - 1) + 1;\r\n if ($data[$_][1] < $data[$_][0]) {\r\n my $tmp = $data[$_][0];\r\n $data[$_][0] = $data[$_][1];\r\n $data[$_][1] = $tmp;\r\n }\r\n }\r\n @data = sort {$a->[1] <=> $b->[1]} @data;\r\n @data = sort {$a->[0] <=> $b->[0]} @data;\r\n foreach (@data) {\r\n print $_->[0], \":\";\r\n print $_->[1], \"\\n\";\r\n }\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $data[$i][0] || $i + 1 > $data[$i][1]) {\r\n $flag = 1;\r\n last;\r\n }\r\n }\r\n if (!$flag) {print \"YES\", \"\\n\";} else { print \"NO\", \"\\n\"; }\r\n}"}, {"source_code": "my $line;\r\nmy @input1; my @input2;\r\n\r\n$line = ;\r\nchomp($line);\r\n\r\nmy $line2; my $line3; my $len; my @data;\r\nfor(1..$line) {\r\n\r\n $len = ;\r\n chomp($len);\r\n\r\n $line2= ;\r\n chomp($line2);\r\n $line3 = ;\r\n chomp($line3);\r\n\r\n @input1 = split / /, $line2;\r\n @input2 = split //, $line3;\r\n my $flag = 0;\r\n\r\n for (0..$len-1) {\r\n if (@input1[$_] < 1 && @input2[$_] eq \"B\") { $flag = 1;}\r\n if (@input1[$_] > $len && @input2[$_] eq \"R\") { $flag = 1;}\r\n $data[$_][0] = @input1[$_];\r\n $data[$_][1] = ((ord(@input2[$_]) % 11)%2) * ($len - 1) + 1;\r\n if ($data[$_][1] < $data[$_][0]) {\r\n my $tmp = $data[$_][0];\r\n $data[$_][0] = $data[$_][1];\r\n $data[$_][1] = $tmp;\r\n }\r\n }\r\n @data = sort {$a->[1] <=> $b->[1]} @data;\r\n @data = sort {$a->[0] <=> $b->[0]} @data;\r\n # foreach (@data) {\r\n # print $_->[0], \":\";\r\n # print $_->[1], \"\\n\";\r\n # }\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $data[$i][0] || $i + 1 > $data[$i][1]) {\r\n $flag = 1;\r\n last;\r\n }\r\n }\r\n if (!$flag) {print \"YES\", \"\\n\";} else { print \"NO\", \"\\n\"; }\r\n}"}, {"source_code": "my $line;\r\nmy @input1; my @input2;\r\n\r\n$line = ;\r\nchomp($line);\r\n\r\nmy $line2; my $line3; my $len; my @data;\r\nfor(1..$line) {\r\n\r\n $len = ;\r\n chomp($len);\r\n\r\n $line2= ;\r\n chomp($line2);\r\n $line3 = ;\r\n chomp($line3);\r\n\r\n @input1 = split / /, $line2;\r\n @input2 = split //, $line3;\r\n my $flag = 0;\r\n\r\n for (0..$len-1) {\r\n if (@input1[$_] < 1 && @input2[$_] eq \"B\") { $flag = 1; last;}\r\n if (@input1[$_] > $len && @input2[$_] eq \"R\") { $flag = 1; last;}\r\n $data[$_][0] = @input1[$_];\r\n $data[$_][1] = ((ord(@input2[$_]) % 11)%2) * ($len - 1) + 1;\r\n if ($data[$_][1] < $data[$_][0]) {\r\n my $tmp = $data[$_][0];\r\n $data[$_][0] = $data[$_][1];\r\n $data[$_][1] = $tmp;\r\n }\r\n }\r\n @data = sort {$a->[0] <=> $b->[0]} @data;\r\n @data = sort {$a->[1] cmp $b->[1]} @data;\r\n## foreach (@data) {\r\n## print $_->[0], \":\";\r\n## print $_->[1], \"\\n\";\r\n## }\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $data[$i][0] || $i + 1 > $data[$i][1]) {\r\n $flag = 1;\r\n last;\r\n }\r\n }\r\n if (!$flag) {print \"YES\", \"\\n\";} else { print \"NO\", \"\\n\";}\r\n}"}, {"source_code": "my $line;\r\nmy @input1; my @input2;\r\n\r\n$line = ;\r\nchomp($line);\r\n\r\nmy $line2; my $line3; my $len; my @data;\r\nfor(1..$line) {\r\n\r\n $len = ;\r\n chomp($len);\r\n\r\n $line2= ;\r\n chomp($line2);\r\n $line3 = ;\r\n chomp($line3);\r\n\r\n @input1 = split / /, $line2;\r\n @input2 = split //, $line3;\r\n my $flag = 0;\r\n\r\n for (0..$len-1) {\r\n if (@input1[$_] < 1 && @input2[$_] eq \"B\") { $flag = 1;}\r\n if (@input1[$_] > $len && @input2[$_] eq \"R\") { $flag = 1;}\r\n $data[$_][0] = @input1[$_];\r\n $data[$_][1] = ((ord(@input2[$_]) % 11)%2) * ($len - 1) + 1;\r\n if ($data[$_][1] < $data[$_][0]) {\r\n my $tmp = $data[$_][0];\r\n $data[$_][0] = $data[$_][1];\r\n $data[$_][1] = $tmp;\r\n }\r\n }\r\n @data = sort {$a->[0] <=> $b->[0]} @data;\r\n @data = sort {$a->[1] cmp $b->[1]} @data;\r\n## foreach (@data) {\r\n## print $_->[0], \":\";\r\n## print $_->[1], \"\\n\";\r\n## }\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $data[$i][0] || $i + 1 > $data[$i][1]) {\r\n print \"NO\", \"\\n\";\r\n $flag = 1;\r\n last;\r\n }\r\n }\r\n if (!$flag) {print \"YES\", \"\\n\";}\r\n}"}, {"source_code": "my $line;\r\nmy @input1; my @input2;\r\n\r\n$line = ;\r\nchomp($line);\r\n\r\nmy $line2; my $line3; my $len; my @data;\r\nfor(1..$line) {\r\n\r\n $len = ;\r\n chomp($len);\r\n\r\n $line2= ;\r\n chomp($line2);\r\n $line3 = ;\r\n chomp($line3);\r\n\r\n @input1 = split / /, $line2;\r\n @input2 = split //, $line3;\r\n for (0..$len-1) {\r\n $data[$_][0] = @input1[$_];\r\n $data[$_][1] = ((ord(@input2[$_]) % 11)%2) * ($len - 1) + 1;\r\n if ($data[$_][1] < $data[$_][0]) {\r\n my $tmp = $data[$_][0];\r\n $data[$_][0] = $data[$_][1];\r\n $data[$_][1] = $tmp;\r\n }\r\n }\r\n @data = sort {$a->[0] <=> $b->[0]} @data;\r\n @data = sort {$a->[1] cmp $b->[1]} @data;\r\n## foreach (@data) {\r\n## print $_->[0], \":\";\r\n## print $_->[1], \"\\n\";\r\n## }\r\n my $flag = 0;\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $data[$i][0] || $i + 1 > $data[$i][1]) {\r\n print \"NO\", \"\\n\";\r\n $flag = 1;\r\n last;\r\n }\r\n }\r\n if (!$flag) {print \"YES\", \"\\n\";}\r\n}"}, {"source_code": "my $line;\r\nmy @input1; my @input2;\r\n\r\n$line = ;\r\nchomp($line);\r\n\r\nmy $line2; my $line3; my $len; my @data;\r\nfor(1..$line) {\r\n\r\n $len = ;\r\n chomp($len);\r\n\r\n $line2= ;\r\n chomp($line2);\r\n $line3 = ;\r\n chomp($line3);\r\n\r\n @input1 = split / /, $line2;\r\n @input2 = split //, $line3;\r\n for (0..$len-1) {\r\n if (@input1[$_] < 1) { @input1[$_] = 1;}\r\n if (@input1[$_] > $len) { @input1[$_] = $len;}\r\n $data[$_][0] = @input1[$_];\r\n $data[$_][1] = ((ord(@input2[$_]) % 11)%2) * ($len - 1) + 1;\r\n if ($data[$_][1] < $data[$_][0]) {\r\n my $tmp = $data[$_][0];\r\n $data[$_][0] = $data[$_][1];\r\n $data[$_][1] = $tmp;\r\n }\r\n }\r\n @data = sort {$a->[0] <=> $b->[0]} @data;\r\n @data = sort {$a->[1] cmp $b->[1]} @data;\r\n## foreach (@data) {\r\n## print $_->[0], \":\";\r\n## print $_->[1], \"\\n\";\r\n## }\r\n my $flag = 0;\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $data[$i][0] || $i + 1 > $data[$i][1]) {\r\n print \"NO\", \"\\n\";\r\n $flag = 1;\r\n last;\r\n }\r\n }\r\n if (!$flag) {print \"YES\", \"\\n\";}\r\n}"}, {"source_code": "my $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\nmy $x;\r\n\r\n\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n my $len = $line;\r\n $line = ;\r\n chomp($line);\r\n my @input1 = split(' ', $line);\r\n $line = ;\r\n chomp($line);\r\n my @input2 = split('', $line);\r\n my @range;\r\n my $i = 0;\r\n my @test;\r\n my $flag = 0;\r\n foreach (@input2) {\r\n if ($_ eq \"B\") {\r\n if (@input1[$i] < 1) {$flag = 1;last;}\r\n if (@input1[$i] > $len) {@input1[$i] = $len;}\r\n $range[$i][1] = @input1[$i];\r\n $range[$i][0] = 1;\r\n for (my $j = $range[$i][1]; $j <= $len; $j++) {\r\n $test[$range[$i][0]][$j]++;\r\n }\r\n } else {\r\n if (@input1[$i] > $len) {$flag = 1;last;}\r\n if (@input1[$i] < 1) {@input1[$i] = 1;}\r\n $range[$i][0] = @input1[$i];\r\n $range[$i][1] = $len;\r\n for (my $j = 1; $j <= $range[$i][0]; $j++) {\r\n $test[$j][$range[$i][1]]++;\r\n }\r\n }\r\n $range[$i][2] = $range[$i][1] - $range[$i][0];\r\n if ($test[$range[$i][0]][$range[$i][1]] > $range[$i][2] + 1) { $flag = 1; last;}\r\n $i++;\r\n }\r\n\r\n if (!$flag) { print \"YES\", \"\\n\";} else { print \"NO\", \"\\n\"; }\r\n}"}, {"source_code": "my $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\nmy $x;\r\n\r\n\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n my $len = $line;\r\n $line = ;\r\n chomp($line);\r\n my @input1 = split(' ', $line);\r\n $line = ;\r\n chomp($line);\r\n my @input2 = split('', $line);\r\n my @range;\r\n my $i = 0;\r\n my @test;\r\n my $flag = 0;\r\n foreach (@input2) {\r\n if ($_ eq \"B\") {\r\n if (@input1[$i] < 1) {$flag = 1;last;}\r\n if (@input1[$i] > $len) {@input1[$i] = $len;}\r\n $range[$i][1] = @input1[$i];\r\n $range[$i][0] = 1;\r\n for (my $j = 1; $j <= $range[$i][1]; $j++) {\r\n $test[$range[$i][0]][$j]++;\r\n }\r\n } else {\r\n if (@input1[$i] > $len) {$flag = 1;last;}\r\n if (@input1[$i] < 1) {@input1[$i] = 1;}\r\n $range[$i][0] = @input1[$i];\r\n $range[$i][1] = $len;\r\n for (my $j = 1; $j <= $range[$i][0]; $j++) {\r\n $test[$j][$range[$i][1]]++;\r\n }\r\n }\r\n $range[$i][2] = $range[$i][1] - $range[$i][0];\r\n if ($test[$range[$i][0]][$range[$i][1]] > $range[$i][2] + 1) { $flag = 1; last;}\r\n $i++;\r\n }\r\n\r\n if (!$flag) { print \"YES\", \"\\n\";} else { print \"NO\", \"\\n\"; }\r\n}"}, {"source_code": "my $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\nmy $x;\r\n\r\n\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n my $len = $line;\r\n $line = ;\r\n chomp($line);\r\n my @input1 = split(' ', $line);\r\n $line = ;\r\n chomp($line);\r\n my @input2 = split('', $line);\r\n my @range;\r\n my $i = 0;\r\n my @test;\r\n my $flag = 0;\r\n foreach (@input2) {\r\n if ($_ eq \"B\") {\r\n if (@input1[$i] < 1) {$flag = 1;last;}\r\n if (@input1[$i] > $len) {@input1[$i] = $len;}\r\n $range[$i][1] = @input1[$i];\r\n $range[$i][0] = 1;\r\n for (my $j = 0; $j <= $range[$i][1]; $j++) {\r\n $test[$range[$i][0]][$j]++;\r\n }\r\n } else {\r\n if (@input1[$i] > $len) {$flag = 1;last;}\r\n if (@input1[$i] < 1) {@input1[$i] = 1;}\r\n $range[$i][0] = @input1[$i];\r\n $range[$i][1] = $len;\r\n for (my $j = $range[$i][0]; $j <= $len; $j++) {\r\n $test[$j][$range[$i][1]]++;\r\n }\r\n }\r\n $range[$i][2] = $range[$i][1] - $range[$i][0];\r\n if ($test[$range[$i][0]][$range[$i][1]] > $range[$i][2] + 1) { $flag = 1; last;}\r\n $i++;\r\n }\r\n\r\n if (!$flag) { print \"YES\", \"\\n\";} else { print \"NO\", \"\\n\"; }\r\n}"}, {"source_code": "my $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\nmy $x;\r\n\r\n\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n my $len = $line;\r\n $line = ;\r\n chomp($line);\r\n my @input1 = split(' ', $line);\r\n $line = ;\r\n chomp($line);\r\n my @input2 = split('', $line);\r\n my @range;\r\n my $i = 0;\r\n my @test;\r\n my $flag = 0;\r\n foreach (@input2) {\r\n if ($_ eq \"B\") {\r\n if (@input1[$i] < 1) {$flag = 1;last;}\r\n if (@input1[$i] > $len) {@input1[$i] = $len;}\r\n $range[$i][1] = @input1[$i];\r\n $range[$i][0] = 1;\r\n for (my $j = 0; $j <= $range[$i][1]; $j++) {\r\n $test[$range[$i][0]][$j]++;\r\n }\r\n } else {\r\n if (@input1[$i] > $len) {$flag = 1;last;}\r\n if (@input1[$i] < 1) {@input1[$i] = 1;}\r\n $range[$i][0] = @input1[$i];\r\n $range[$i][1] = $len;\r\n for (my $j = $range[$i][0]; $j <= $len; $j++) {\r\n $test[$j][$range[$i][1]];\r\n }\r\n }\r\n $range[$i][2] = $range[$i][1] - $range[$i][0];\r\n if ($test[$range[$i][0]][$range[$i][1]] > $range[$i][2] + 1) { $flag = 1; last;}\r\n $i++;\r\n }\r\n\r\n if (!$flag) { print \"YES\", \"\\n\";} else { print \"NO\", \"\\n\"; }\r\n}"}, {"source_code": "use POSIX;\r\nmy $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\n\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n my $len = $line;\r\n $line = ;\r\n chomp($line);\r\n my @input1 = split(' ', $line);\r\n $line = ;\r\n chomp($line);\r\n my @input2 = split('', $line);\r\n my @range = ();\r\n my $i = 0;\r\n my @test = ();\r\n my $flag = 0;\r\n foreach (@input2) {\r\n if ($_ eq \"B\") {\r\n if (@input1[$i] < 1) {$flag = 1;last;}\r\n if (@input1[$i] > $len) {@input1[$i] = $len;}\r\n $range[$i][1] = @input1[$i];\r\n $range[$i][0] = 1;\r\n } else {\r\n if (@input1[$i] > $len) {$flag = 1;last;}\r\n if (@input1[$i] < 1) {@input1[$i] = 1;}\r\n $range[$i][0] = @input1[$i];\r\n $range[$i][1] = $len;\r\n }\r\n $range[$i][2] = $range[$i][1] - $range[$i][0];\r\n $test[$range[$i][0]][$range[$i][1]]++;\r\n if ($test[$range[$i][0]][$range[$i][1]] > $range[$i][2] + 1) { $flag = 1; last;}\r\n $i++;\r\n }\r\n\r\n if (!$flag) { print \"YES\", \"\\n\";} else { print \"NO\", \"\\n\"; }\r\n}\r\nexit;"}, {"source_code": "use POSIX;\r\nmy $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\n\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n my $len = $line;\r\n $line = ;\r\n chomp($line);\r\n my @input1 = split(' ', $line);\r\n $line = ;\r\n chomp($line);\r\n my @input2 = split('', $line);\r\n my @range;\r\n my $i = 0;\r\n my @test;\r\n my $flag = 0;\r\n foreach (@input2) {\r\n if ($_ eq \"B\") {\r\n if (@input1[$i] < 1) {$flag = 1;last;}\r\n if (@input1[$i] > $len) {@input1[$i] = $len;}\r\n $range[$i][1] = @input1[$i];\r\n $range[$i][0] = 1;\r\n } else {\r\n if (@input1[$i] > $len) {$flag = 1;last;}\r\n if (@input1[$i] < 1) {@input1[$i] = 1;}\r\n $range[$i][0] = @input1[$i];\r\n $range[$i][1] = $len;\r\n }\r\n $range[$i][2] = $range[$i][1] - $range[$i][0];\r\n $test[$range[$i][0]][$range[$i][1]]++;\r\n if ($test[$range[$i][0]][$range[$i][1]] > $range[$i][2] + 1) { $flag = 1; last;}\r\n $i++;\r\n }\r\n\r\n if (!$flag) { print \"YES\", \"\\n\";} else { print \"NO\", \"\\n\"; }\r\n}\r\nexit;"}, {"source_code": "use Class::Struct;\r\nuse List::Util qw(first);\r\nuse List::Util qw(min);\r\nuse List::Util qw(max);\r\nuse POSIX;\r\nmy $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\n\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n my $len = $line;\r\n $line = ;\r\n chomp($line);\r\n my @input1 = split(' ', $line);\r\n $line = ;\r\n chomp($line);\r\n my @input2 = split('', $line);\r\n my @range;\r\n my $i = 0;\r\n foreach (@input2) {\r\n if ($_ eq \"B\") {\r\n if (@input1[$i] < 1) {last;}\r\n if (@input1[$i] > $len) {@input1[$i] = $len;}\r\n $range[$i][1] = @input1[$i];\r\n $range[$i][0] = 1;\r\n } else {\r\n if (@input1[$i] > $len) {last;}\r\n if (@input1[$i] < 1) {@input1[$i] = 1;}\r\n $range[$i][0] = @input1[$i];\r\n $range[$i][1] = $len;\r\n }\r\n $range[$i][2] = $range[$i][1] - $range[$i][0];\r\n $i++;\r\n }\r\n @range = sort {$a->[2] <=> $b->[2]} @range;\r\n @range = sort {$a->[0] <=> $b->[0]} @range;\r\n #for (my $i = 0; $i < $len; $i++) { print $range[$i][0], \"-\";print $range[$i][1], \": \";print $range[$i][2], \"\\n\";}\r\n #each number should exist in the range o\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $range[$i][0] || $i + 1 > $range[$i][1]) {\r\n print \"NO\", \"\\n\";\r\n last;\r\n }\r\n }\r\n print \"YES\", \"\\n\";\r\n}"}, {"source_code": "use Class::Struct;\r\nuse List::Util qw(first);\r\nuse List::Util qw(min);\r\nuse List::Util qw(max);\r\nuse POSIX;\r\nmy $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\n\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n my $len = $line;\r\n $line = ;\r\n chomp($line);\r\n my @input1 = split(' ', $line);\r\n $line = ;\r\n chomp($line);\r\n my @input2 = split('', $line);\r\n my @range;\r\n my $i = 0;\r\n foreach (@input2) {\r\n if ($_ eq \"B\") {\r\n if (@input1[$i] < 1) {last;}\r\n if (@input1[$i] > $len) {@input1[$i] = $len;}\r\n $range[$i][1] = @input1[$i];\r\n $range[$i][0] = 1;\r\n } else {\r\n if (@input1[$i] > $len) {last;}\r\n if (@input1[$i] < 1) {@input1[$i] = 1;}\r\n $range[$i][0] = @input1[$i];\r\n $range[$i][1] = $len;\r\n }\r\n $range[$i][2] = $range[$i][1] - $range[$i][0];\r\n $i++;\r\n }\r\n @range = sort {$a->[2] <=> $b->[2]} @range;\r\n @range = sort {$a->[1] <=> $b->[1]} @range;\r\n @range = sort {$a->[0] <=> $b->[0]} @range;\r\n #for (my $i = 0; $i < $len; $i++) { print $range[$i][0], \"-\";print $range[$i][1], \": \";print $range[$i][2], \"\\n\";}\r\n #each number should exist in the range o\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $range[$i][0] || $i + 1 > $range[$i][1]) {\r\n print \"NO\", \"\\n\";\r\n exit;\r\n }\r\n }\r\n print \"YES\", \"\\n\";\r\n}"}, {"source_code": "use Class::Struct;\r\nuse List::Util qw(first);\r\nuse List::Util qw(min);\r\nuse List::Util qw(max);\r\nuse POSIX;\r\nmy $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\n\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n my $len = $line;\r\n $line = ;\r\n chomp($line);\r\n my @input1 = split(' ', $line);\r\n $line = ;\r\n chomp($line);\r\n my @input2 = split('', $line);\r\n my @range;\r\n my $i = 0;\r\n foreach (@input2) {\r\n if ($_ eq \"B\") {\r\n if (@input1[$i] < 1) {last;}\r\n if (@input1[$i] > $len) {@input1[$i] = $len;}\r\n $range[$i][1] = @input1[$i];\r\n $range[$i][0] = 1;\r\n } else {\r\n if (@input1[$i] > $len) {last;}\r\n if (@input1[$i] < 1) {@input1[$i] = 1;}\r\n $range[$i][0] = @input1[$i];\r\n $range[$i][1] = $len;\r\n }\r\n $range[$i][2] = $range[$i][1] - $range[$i][0];\r\n $i++;\r\n }\r\n @range = sort {$a->[2] <=> $b->[2]} @range;\r\n @range = sort {$a->[1] <=> $b->[1]} @range;\r\n @range = sort {$a->[0] <=> $b->[0]} @range;\r\n for (my $i = 0; $i < $len; $i++) { print $range[$i][0], \"-\";print $range[$i][1], \": \";print $range[$i][2], \"\\n\";}\r\n #each number should exist in the range o\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $range[$i][0] || $i + 1 > $range[$i][1]) {\r\n print \"NO\", \"\\n\";\r\n exit;\r\n }\r\n }\r\n print \"YES\", \"\\n\";\r\n}"}, {"source_code": " use Class::Struct;\r\n use List::Util qw(first);\r\n use List::Util qw(min);\r\n use List::Util qw(max);\r\n use POSIX;\r\n my $line;\r\n my @input1; my @input2;\r\n my $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\n my $s = 0; my $c; my $result; my $i; my %h;\r\n \r\n $line = ;\r\n chomp($line);\r\n @input1 = split(' ', $line);\r\n \r\n for(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n my $len = $line;\r\n $line = ;\r\n chomp($line);\r\n my @input1 = split(' ', $line);\r\n $line = ;\r\n chomp($line);\r\n my @input2 = split('', $line);\r\n my @range;\r\n my $i = 0;\r\n foreach (@input2) {\r\n if ($_ eq \"B\") {\r\n $range[$i][1] = @input1[$i];\r\n $range[$i][0] = 1;\r\n } else {\r\n $range[$i][0] = @input1[$i];\r\n $range[$i][1] = $len;\r\n }\r\n $range[$i][2] = $range[$i][1] - $range[$i][0];\r\n $i++;\r\n }\r\n @range = sort {$a->[2] <=> $b->[2]} @range;\r\n @range = sort {$a->[0] <=> $b->[0]} @range;\r\n #for (my $i = 0; $i < $len; $i++) { print $range[$i][0], \"-\";print $range[$i][1], \": \";print $range[$i][2], \"\\n\";}\r\n #each number should exist in the range o\r\n my $flag = 0;\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $range[$i][0] || $i + 1 > $range[$i][1]) {\r\n print \"NO\", \"\\n\";\r\n $flag = 1;\r\n last;\r\n }\r\n }\r\n if (!$flag) {print \"YES\", \"\\n\"};\r\n }"}, {"source_code": "use Class::Struct;\r\nuse List::Util qw(first);\r\nuse List::Util qw(min);\r\nuse List::Util qw(max);\r\nuse POSIX;\r\nmy $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\n\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n my $len = $line;\r\n $line = ;\r\n chomp($line);\r\n my @input1 = split(' ', $line);\r\n $line = ;\r\n chomp($line);\r\n my @input2 = split('', $line);\r\n my @range;\r\n my $i = 0;\r\n foreach (@input2) {\r\n if ($_ eq \"B\") {\r\n $range[$i][1] = @input1[$i];\r\n $range[$i][0] = 1;\r\n } else {\r\n $range[$i][0] = @input1[$i];\r\n $range[$i][1] = $len;\r\n }\r\n $range[$i][2] = $range[$i][1] - $range[$i][0];\r\n $i++;\r\n }\r\n @range = sort {$a->[2] <=> $b->[2]} @range;\r\n @range = sort {$a->[0] <=> $b->[0]} @range;\r\n #for (my $i = 0; $i < $len; $i++) { print $range[$i][0], \"-\";print $range[$i][1], \": \";print $range[$i][2], \"\\n\";}\r\n #each number should exist in the range o\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $range[$i][0] || $i + 1 > $range[$i][1]) {\r\n print \"NO\", \"\\n\";\r\n exit;\r\n }\r\n }\r\n print \"YES\", \"\\n\";\r\n}"}, {"source_code": "use Class::Struct;\r\nuse List::Util qw(first);\r\nuse List::Util qw(min);\r\nuse List::Util qw(max);\r\nuse POSIX;\r\nmy $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\n\r\nfor(1..@input1[0]) {\r\n $line = ;\r\n chomp($line);\r\n my $len = $line;\r\n $line = ;\r\n chomp($line);\r\n my @input1 = split(' ', $line);\r\n $line = ;\r\n chomp($line);\r\n my @input2 = split('', $line);\r\n my @range;\r\n my $i = 0;\r\n foreach (@input2) {\r\n if ($_ eq \"B\") {\r\n $range[$i][1] = @input1[$i];\r\n $range[$i][0] = 1;\r\n } else {\r\n $range[$i][0] = @input1[$i];\r\n $range[$i][1] = $len;\r\n }\r\n $range[$i][2] = $range[$i][1] - $range[$i][0];\r\n $i++;\r\n }\r\n @range = sort {$a->[2] <=> $b->[2]} @range;\r\n @range = sort {$a->[0] <=> $b->[0]} @range;\r\n #for (my $i = 0; $i < $len; $i++) { print $range[$i][0], \"-\";print $range[$i][1], \": \";print $range[$i][2], \"\\n\";}\r\n #each number should exist in the range o\r\n for (my $i = 0; $i < $len; $i++) {\r\n if ($i + 1 < $range[$i][0] || $i + 1 > $range[$i][1]) {\r\n print \"NO\";\r\n exit;\r\n }\r\n }\r\n print \"YES\";\r\n}"}], "src_uid": "c3df88e22a17492d4eb0f3239a27d404"} {"nl": {"description": "Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \\oplus f(n-2)$$$ when $$$n > 1$$$, where $$$\\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.", "input_spec": "The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \\le T \\le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 \\le a, b, n \\le 10^9$$$) respectively.", "output_spec": "For each test case, output $$$f(n)$$$.", "sample_inputs": ["3\n3 4 2\n4 5 0\n325 265 1231232"], "sample_outputs": ["7\n4\n76"], "notes": "NoteIn the first example, $$$f(2) = f(0) \\oplus f(1) = 3 \\oplus 4 = 7$$$."}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nchomp (my $line = );\nmy $t = $line;\n\nfor ( 1..$t ) {\n chomp ($line = );\n my ($a, $b, $n) = split q{ }, $line;\n my $c = int($a) ^ int($b);\n my $mod = $n % 3;\n if ( $mod == 0 ) {\n say $a;\n }\n elsif ( $mod == 1 ) {\n say $b;\n }\n else {\n say $c;\n }\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $A, $B, $n ) = split;\n\t\n\t@_ = ( $A, $B );\n\t\n\tfor my $i ( 2 .. 2 ){\n\t\tpush @_, ( 0 + $_[ $i - 2 ] ) ^ ( 0 + $_[ $i - 1 ] );\n\t\t}\n\t\n\t$debug and print \"[@_]\";\n\t\n\tprint $_[ $n % 3 ];\n\t}"}], "negative_code": [], "src_uid": "64a375c7c49591c676dbdb039c93d218"} {"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$$$) \u2014 the number of test cases. Next $$$T$$$ lines contain test cases \u2014 one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \\le n, g, b \\le 10^9$$$) \u2014 the length of the highway and the number of good and bad days respectively.", "output_spec": "Print $$$T$$$ integers \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $g, $B ) = split;\n\t\n\tmy $half = int( $n / 2 ) + $n % 2;\n\t\n\tmy $times = int( $half / $g );\n\t\n\tmy $days = 0;\n\t\n\t$days = $times * ( $g + $B );\n\t\n\tif( $half % $g == 0 ){\n\t\t$days -= $B;\n\t\t}\n\t\n\t$days += $half % $g;\n\t\n\t$days < $n and $days = $n;\n\t\n\tprint $days;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $g, $B ) = split;\n\t\n\tmy $half = int( $n / 2 ) + $n % 2;\n\t\n\tmy $times = int( $half / $g );\n\t\n\tmy $days = 0;\n\t\n\t$days = $times * ( $g + $B );\n\t\n\tif( $half % $g == 0 ){\n\t\t$days -= $B;\n\t\t}\n\t\n\t$days += $half % $g;\n\t\n\tprint $days;\n\t}"}], "src_uid": "be9138aca8e1b8a5d722f99fcd70b685"} {"nl": {"description": "Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u,\u2009v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .", "input_spec": "The first line of input contains an integer n\u00a0\u2014 the number of nodes in the tree (1\u2009\u2264\u2009n\u2009\u2264\u2009105). The next n\u2009-\u20091 lines contain integers u and v (1\u2009\u2264\u2009u,\u2009v\u2009\u2264\u2009n, u\u2009\u2260\u2009v)\u00a0\u2014 the description of the edges of the tree. It's guaranteed that the given graph is a tree. ", "output_spec": "Output one integer\u00a0\u2014 the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.", "sample_inputs": ["3\n1 2\n1 3", "5\n1 2\n2 3\n3 4\n4 5"], "sample_outputs": ["0", "2"], "notes": "NoteTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2,\u20093), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1,\u20094) and (2,\u20095). "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t\n\tmy %h;\n\t\n\tmap {\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t$h{ $u }{ $v } = 1;\n\t\t$h{ $v }{ $u } = 1;\n\t\t} 1 .. $_ - 1;\n\t\n\tmy $color = 'C';\n\tmy @keys;\n\t( $keys[ 0 ] ) = sort keys %h;\n\tpush @keys, $color;\n\t\n\tmy @color;\n\tmy $i = 0;\n\twhile( @keys ){\n\t\t$debug and print \"keys:[@keys]\";\n\t\tmy $key = shift @keys;\n\t\t$key eq $color and do {\n\t\t\tpush @keys, $color if @keys and $keys[ -1 ] ne $color;\n\t\t\t$i ++;\n\t\t\tnext;\n\t\t\t};\n\t\t$color[ $i % 2 ] ++;\n\t\tmy @new_keys = keys %{ $h{ $key } };\n\t\tdelete $h{ $_ }{ $key } for @new_keys;\n\t\tpush @keys, @new_keys;\n\t\t}\n\t\t\n\t$debug and print \"color:[@color]\";\n\tprint 1 - $_ + eval join ' * ', @color;\n\t}"}, {"source_code": "( $u, $v ) = ( split ' ', <> ),\n\t$h{ $u }{ $v } = $h{ $v }{ $u } = 1 for 2 .. ( $_ = <> );\n\t\n@K = ( ( keys %h )[ 0 ], 0 );\n\nwhile( @K ){\n\tif( $k = shift @K ){\n\t\t$C[ $i % 2 ] ++;\n\t\tdelete $h{ $_ }{ $k } for @nk = keys %{ $h{ $k } };\n\t\tpush @K, @nk;\n\t\t}\n\telse{\n\t\tpush @K, 0 if @K * $K[ -1 ];\n\t\t$i ++;\n\t\t}\n\t}\n\t\nprint 1 - $_ + $C[ 0 ] * $C[ 1 ]"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_ - 1;\n\t\n\tprint 1 - $_ + ( $_ % 2 == 0 ? ( $_ / 2 ) ** 2 : ( int $_ / 2 ) * ( 1 + int $_ / 2 ) );\n\t}"}], "src_uid": "44b9adcc9d672221bda3a1cada81b3d0"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the elements of $$$A$$$. The third line contains one integer $$$m$$$ ($$$1\\le m \\le 100$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my $a= <$in>; chomp($a);\n my @a= split(' ', $a);\n\n $q= <$in>;\n\n my $b= <$in>; chomp($b);\n my @b= split(' ', $b);\n\n my @results;\n for my $x (@a) {\n for my $y (@b) {\n my $z= $x + $y;\n\n if(\n !(grep {/$z/} @a) &&\n !(grep {/$z/} @b)\n ) {\n print \"$x $y\";\n return;\n };\n }\n }\n\n}\n\nmain() unless caller();\n\n"}], "negative_code": [{"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my $a= <$in>; chomp($a);\n my @a= split(' ', $a);\n\n $q= <$in>;\n\n my $b= <$in>; chomp($b);\n my @b= split(' ', $b);\n\n my @results;\n for my $x (@a) {\n for my $y (@b) {\n my $z= $x + $y;\n\n unless(\n grep {/$z/} @a &&\n grep {/$z/} @b\n ) {\n print \"$x $y\";\n return;\n };\n }\n }\n\n}\n\nmain() unless caller();\n\n"}], "src_uid": "ec09b2df4ed3abbca4c47f48f12010ca"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009300) \u2014 the number of wallets. The next line contains n integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009300). It is guaranteed that at least one ai is positive.", "output_spec": "Print the sequence that consists of k (1\u2009\u2264\u2009k\u2009\u2264\u2009106) 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$_=<>;\ns/(\\d+) /(\"RLP\"x$1).\"R\"/eg;\ns/\\d+$/\"LRP\"x$&/e;\nprint\n"}, {"source_code": "<>;\n$_=<>;\ns/(\\d+) /(\"RLP\"x$1).\"R\"/eg;\ns/\\d+$/\"LRP\"x$&/e;\nprint\n"}, {"source_code": "<>;\n$_=<>;\ns/(\\d+) /(\"RLP\"x$1).\"R\"/eg;\ns/\\d+$/\"LRP\"x$&/e;\nprint"}, {"source_code": "<>;\n$_=<>;\ns/(\\d+) /(\"RLP\"x$1).\"R\"/eg;\ns/\\d+$/\"LRP\"x$&/e;\nprint\n"}, {"source_code": "<>;\n$_=<>;\ns/(\\d+) /(\"RLP\"x$1).\"R\"/eg;\ns/\\d+$/\"LRP\"x$&/e;\nprint\n"}, {"source_code": "use v5.10;\n\nchomp($n = <>);\n@a = split / /, <>;\n$ans = '';\nforeach (0 .. $n-2) {\n\t$c = $a[$_];\n\t$ans .= 'PRL' while ($c-- > 0);\n\t$ans .= 'R';\n}\n$c = $a[$#a];\n$ans .= 'PLR' while ($c-- > 0);\nsay $ans;"}, {"source_code": "<>;\n$_=<>;\ns/(\\d+) /(\"RLP\"x$1).\"R\"/eg;\ns/\\d+$/\"LRP\"x$&/e;\nprint\n"}, {"source_code": "\n$n = <>;\n$line = <>;\n@list = split(/\\s/, $line);\n$total = 0;\nforeach $x (@list) {\n $total += $x; \n}\n$i = 0;\n$len = $n - 1;\n$dir = 1;\nwhile ($total) {\n if ($list[$i]) {\n $list[$i] -= 1;\n $total -= 1;\n print 'P';\n }\n if ($i == 0) {\n $dir = 1;\n }\n elsif ($i == $len) {\n $dir = -1;\n }\n $i += $dir;\n if ($dir > 0) {print 'R';}\n else {print 'L';}\n}"}, {"source_code": "<>;\n$_=<>;\ns/(\\d+) /(\"RLP\"x$1).\"R\"/eg;\ns/\\d+$/\"LRP\"x$&/e;\nprint\n"}, {"source_code": "<>;\n$_=<>;\ns/(\\d+) /(\"RLP\"x$1).\"R\"/eg;\ns/\\d+$/\"LRP\"x$&/e;\nprint\n"}, {"source_code": "<>;\n$_=<>;\ns/(\\d+) /(\"RLP\"x$1).\"R\"/eg;\ns/\\d+$/\"LRP\"x$&/e;\nprint"}, {"source_code": "<>;\n$_=<>;\ns/(\\d+) /(\"RLP\"x$1).\"R\"/eg;\ns/\\d+$/\"LRP\"x$&/e;\nprint\n"}], "negative_code": [], "src_uid": "50e88225d8b081d63eebe446f48057f4"} {"nl": {"description": "We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t \u0422-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is \u0422-prime or not.", "input_spec": "The first line contains a single positive integer, n (1\u2009\u2264\u2009n\u2009\u2264\u2009105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1\u2009\u2264\u2009xi\u2009\u2264\u20091012). Please, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is advised to use the cin, cout streams or the %I64d specifier.", "output_spec": "Print n lines: the i-th line should contain \"YES\" (without the quotes), if number xi is \u0422-prime, and \"NO\" (without the quotes), if it isn't.", "sample_inputs": ["3\n4 5 6"], "sample_outputs": ["YES\nNO\nNO"], "notes": "NoteThe given test has three numbers. The first number 4 has exactly three divisors \u2014 1, 2 and 4, thus the answer for this number is \"YES\". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is \"NO\"."}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n$maxn = 10 ** 6 + 5;\n@visit = (0) x $maxn;\nfor ($i=2; $i<$maxn; ++$i) {\n\tif ($visit[$i] == 0) {\n\t\t$tb{$i*$i} = 1;\n\t\tfor ($j=$i*$i; $j<$maxn; $j+=$i) {\n\t\t\t$visit[$j] = 1;\n\t\t}\n\t}\n}\nchomp($n = <>);\n$tb{int($_)} and say \"YES\" or say \"NO\" foreach (split / /, <>);"}, {"source_code": "for(2..1000000){\n\tnext if $pr[$_];\n\tfor($i = 2 * $_ ; $i <= 1000000 ; $i += $_){\n\t\t$pr[$i] = 1;}\n}\n$pr[1] = 1;\n<>;\nforeach(split(' ', <>)){\n\t$r = int(sqrt($_));\n\tprint \"NO\\n\" and next if($r * $r != $_ or $pr[$r]);\n\tprint \"YES\\n\";\n}"}, {"source_code": "for(2..1000000)\n{\n\tnext if $pr[$_];\n\tfor($i = 2 * $_ ; $i <= 1000000 ; $i += $_)\n\t{\n\t\t$pr[$i] = 1;\n\t}\n}\n$pr[1] = 1;\n<>;\nforeach(split(' ', <>))\n{\n\t$r = int(sqrt($_));\n\tprint \"NO\\n\" and next if($r * $r != $_ or $pr[$r]);\n\tprint \"YES\\n\";\n}"}, {"source_code": "#!/usr/bin/perl -w\n\n$n=; chomp $n;\n$a=; chomp $a;\n@a=split(' ',$a);\n\n@p=(0..1000000);\n$p[1]=0;\nfor (2..1000000){\n if($p[$_]){\n $i=$_*2;\n while($i<=1000000){\n $p[$i]=0;\n $i+=$_;\n }\n }\n}\nforeach (@a){\n $x=int(sqrt($_));\n if($x*$x==$_ && $p[$x]){print \"YES\\n\";}\n else{print \"NO\\n\";}\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n$maxn = 10 ** 5 + 5;\n@visit = (0) x $maxn;\nfor ($i=2; $i<$maxn; ++$i) {\n\tif ($visit[$i] == 0) {\n\t\t$tb{$i*$i} = 1;\n\t\tfor ($j=$i*$i; $j<$maxn; $j+=$i) {\n\t\t\t$visit[$j] = 1;\n\t\t}\n\t}\n}\nchomp($n = <>);\n$tb{int($_)} and say \"YES\" or say \"NO\" foreach (split / /, <>);"}, {"source_code": "for(2..1000000){\n\tnext if $pr[$_];\n\tfor($i = 2 * $_ ; $i <= 1000000 ; $i += $_){\n\t\t$pr[$i] = 1;}\n}\n$pr[1] = 1;\n<>;\nforeach(split(' ', <>)){\n\t$r = sqrt($_);\n\tprint \"NO\\n\" and next if($r * $r != $_ or $pr[$r]);\n\tprint \"YES\\n\";\n}"}, {"source_code": "for(2..1000000){\n\tnext if $pr[i];\n\tfor($i = 2 * $_ ; $i <= 1000000 ; $i += $_){\n\t\t$pr[i] = 1;}\n}\n$pr[1] = 1;\n<>;\nforeach(split(' ', <>)){\n\t$r = sqrt($_);\n\tprint \"NO\\n\" and next if($r * $r != $_ or $pr[$r]);\n\tprint \"YES\\n\";\n}"}, {"source_code": "for(1..1000000){\n\tnext if $pr[i];\n\tfor($i = 2 * $_ ; $i <= 1000000 ; $i += $_){\n\t\t$pr[i] = 1;}\n}\n<>;\nforeach(split(' ', <>)){\n\t$r = sqrt($_);\n\tprint \"NO\\n\" and next if($r * $r != $_ or $pr[$r]);\n\tprint \"YES\\n\";\n}"}, {"source_code": "#!/usr/bin/perl -w\n\n$n=; chomp $n;\n$a=; chomp $a;\n@a=split(' ',$a);\n\n@p=(0..1000000);\n$p[1]=0;\nfor (2..1000000){\n if($p[$_]){\n $i=$_*2;\n while($i<1000000){\n $p[$i]=0;\n $i+=$_;\n }\n }\n}\nforeach (@a){\n $x=int(sqrt($_));\n if($x*$x==$_ && $p[$x]){print \"YES\\n\";}\n else{print \"NO\\n\";}\n}\n"}], "src_uid": "6cebf9af5cfbb949f22e8b336bf07044"} {"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\u2009<\u2009aj holds for any two indices i,\u2009j (i\u2009<\u2009j). For any two indices i and j (i\u2009<\u2009j), 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\u2009\u2264\u2009n\u2009\u2264\u2009105).", "output_spec": "Output a line that contains n space-separated integers a1 a2, ..., an (1\u2009\u2264\u2009ai\u2009\u2264\u2009107), 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": "my $n = <>;\n\nwhile ($n--)\n{\n\tprint 10000000 - $n . \" \";\n}"}, {"source_code": "print$_+1e5,\" \"for 1..<>\n"}, {"source_code": "print$_+1e5,\" \"for 1..<>\n"}, {"source_code": "print$_+1e5,\" \"for 1..<>"}, {"source_code": "print$_+1e5,\" \"for 1..<>\n"}, {"source_code": "print$_+1e5,\" \"for 1..<>\n"}, {"source_code": "print$_+1e5,\" \"for 1..<>"}, {"source_code": "print$_+99999,\" \"for 1..<>"}, {"source_code": "print$_+1e5,\" \"for 1..<>\n"}, {"source_code": "#!/usr/bin/perl\nopen I, \"output.txt\";\n#$\\ = \"\\n\";\n$n = <>;\nfor(0..$n-1){\n\tprint 10**6 +$_,\" \";\n}"}, {"source_code": "use warnings;\nuse strict;\n\nmy $n = <>;\nprint join \" \", $n..$n*2-1;\n"}, {"source_code": "print$_+1e5,\" \"for 1..<>\n"}, {"source_code": "use warnings;\n\nmy $n = ;\n\nfor (my $i = 1e+6; $i < 1e+6 + $n; $i++) {\n print $i . \" \";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\n\nmy $n = <>;\n\nprint join \" \", $n .. $n * 2 - 1;\n"}, {"source_code": "print$_+1e5,\" \"for 1..<>\n"}, {"source_code": "@_=1..<>;\nfor(@_){$_+=@_}\nprint\"@_\""}, {"source_code": "print$_+1e5,\" \"for 1..<>\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nopen I, \"output.txt\";\n#$\\ = \"\\n\";\n$n = ;\nfor(0..$n-1){\n\tprint 10**6 +$_,\" \";\n}"}, {"source_code": "use warnings;\n\nmy $n = ;\n\nmy @b = ('1') x 5e+5;\n\n$b[0] = $b[1] = '';\n\nfor (my $i = 2; $i * $i < @b; $i++) {\n if ($b[$i]) {\n for (my $j = $i * $i; $j < @b; $j += $i) {\n $b[$j] = '';\n }\n }\n}\n\nmy @ans = ();\n\nfor (my $i = 0; $i < @b && $n > 0; $i++) {\n if ($b[$i]) {\n push @ans, $i;\n $n--;\n }\n}\n\nprint join \" \", @ans;\n"}, {"source_code": "use warnings;\n\nmy $n = ;\n\nfor (my $i = 1e+7; $i < 1e+7 + $n; $i++) {\n print $i . \" \";\n}\n"}], "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$$$) \u2014 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$$$ \u2014 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": "#!usr/bin/perl\n\nuse strict;\nuse warnings;\n\n<> =~ m/(d+)/;\nmy $n = $1;\n\nmy @inputlog = ();\nmy @flaglog = ();\nmy @log = ();\n\n\nmy @pairrefs = ([\"\", \"\"]);\nwhile (<>) {\n\tchomp;\n\tpush @inputlog, $_;\n\tunless (defined $pairrefs[length]) {\n\t\t$pairrefs[length] = [$_];\n\t\tpush @flaglog, \"first-in-its-length\";\n\t} else {\n\t\tpush @{$pairrefs[length]}, $_;\n\t\tpush @flaglog, \"second-in-its-length\";\n\t}\n}\n\n#print \"- @{$pairrefs[0]}\\n\";\n#print \"- @{$pairrefs[1]}\\n\";\n#print \"- @{$pairrefs[2]}\\n\";\n#print \"- @{$pairrefs[3]}\\n\";\n\n#my @pairrefs2 = ([\"e\", \"k\"], [\"te\", \"ko\"], [\"kor\", \"rte\"], [\"orte\", \"kort\"]);\n\nmy @prefs = (\"\");\nmy @suffs = (\"\");\n\n\nmy $flag = \"prefix-suffox-order\";\n \nif ($flag eq \"prefix-suffox-order\") {\n\tmy $length = 0;\n\t@log = (\"-\");\n\tforeach my $pairref (@pairrefs) {\n\t\tmy ($string1, $string2) = @$pairref; #print \"$string1 | $string2\\n\";\n\n\t\tif ($length > 0) {\n\t\t\tmy $previously = $length - 1;\n\t\t\tif (is_prefix_of($prefs[$previously], $string1) && is_suffix_of($suffs[$previously], $string2)) {\n\t\t\t\t$prefs[$length] = $string1;\n\t\t\t\t$suffs[$length] = $string2;\n\t\t\t\t$log[$length] = \"PS\";\n\t\t\t} elsif (is_prefix_of($prefs[$previously], $string2) && is_suffix_of($suffs[$previously], $string1)) {\n\t\t\t\t$prefs[$length] = $string2;\n\t\t\t\t$suffs[$length] = $string1;\n\t\t\t\t$log[$length] = \"SP\";\n\t\t\t} else {\n\t\t\t\t$flag = \"suffix-prefix-order\";\n\t\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\t$length++;\n\t}\n}\n\nif ($flag eq \"suffix-prefix-order\") {\n\tmy $length = 0;\n\t@log = (\"-\");\n\tforeach my $pairref (@pairrefs) {\n\t\tmy ($string1, $string2) = @$pairref; #print \"$string1 | $string2\\n\";\n\n\t\tif ($length > 0) {\n\t\t\tmy $previously = $length - 1;\n\t\t\tif (is_prefix_of($prefs[$previously], $string2) && is_suffix_of($suffs[$previously], $string1)) {\n\t\t\t\t$prefs[$length] = $string2;\n\t\t\t\t$suffs[$length] = $string1;\n\t\t\t\tpush @log, \"SP\";\n\t\t\t} elsif (is_prefix_of($prefs[$previously], $string1) && is_suffix_of($suffs[$previously], $string2)) {\n\t\t\t\t$prefs[$length] = $string1;\n\t\t\t\t$suffs[$length] = $string2;\n\t\t\t\tpush @log, \"PS\";\n\t\t\t} else {\n\t\t\t\t$flag = \"prefix-suffix-order\";\n\t\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\t$length++;\n\t}\n}\n\n\n#print \"Prefs: \"; print join \", \", @prefs; print \"\\n\";\n#print \"Suffs: \"; print join \", \", @suffs; print \"\\n\";\n#print \"Order: $flag\\n\";\n#print \"Log: @log\\n\";\n\n#print \"Result: \";\nforeach my $i (keys @inputlog) {\n\tmy $length = length $inputlog[$i];\n\tif ($flaglog[$i] eq 'first-in-its-length') {\n\t\tprint substr($log[$length], 0, 1);\n\t} else {\n\t\tprint substr($log[$length], 1, 1);\n\t}\n}\nprint \"\\n\";\n\nsub is_prefix_of {\n\tmy ($part, $whole) = @_;\n\treturn (substr $whole, 0, length $part) eq $part; \n}\n\n\nsub is_suffix_of {\n\tmy ($part, $whole) = @_;\n\treturn 1 if $part eq \"\";\n\treturn (substr $whole, -length $part) eq $part; \n}\n"}, {"source_code": "#!usr/bin/perl\n\nuse strict;\nuse warnings;\n\n<> =~ m/(d+)/;\nmy $n = $1;\n\nmy @inputlog = ();\nmy @flaglog = ();\nmy @log = ();\n\n\nmy @pairrefs = ([\"\", \"\"]);\nwhile (<>) {\n\tchomp;\n\tpush @inputlog, $_;\n\tunless (defined $pairrefs[length]) {\n\t\t$pairrefs[length] = [$_];\n\t\tpush @flaglog, \"first-in-its-length\";\n\t} else {\n\t\tpush @{$pairrefs[length]}, $_;\n\t\tpush @flaglog, \"second-in-its-length\";\n\t}\n}\n\n#print \"- @{$pairrefs[0]}\\n\";\n#print \"- @{$pairrefs[1]}\\n\";\n#print \"- @{$pairrefs[2]}\\n\";\n#print \"- @{$pairrefs[3]}\\n\";\n\n#my @pairrefs2 = ([\"e\", \"k\"], [\"te\", \"ko\"], [\"kor\", \"rte\"], [\"orte\", \"kort\"]);\n\nmy @prefs = (\"\");\nmy @suffs = (\"\");\n\n\nmy $flag = \"prefix-suffox-order\";\n \nif ($flag eq \"prefix-suffox-order\") {\n\tmy $length = 0;\n\t@log = (\"-\");\n\tforeach my $pairref (@pairrefs) {\n\t\tmy ($string1, $string2) = @$pairref; #print \"$string1 | $string2\\n\";\n\n\t\tif ($length > 0) {\n\t\t\tmy $previously = $length - 1;\n\t\t\tif (is_prefix_of($prefs[$previously], $string1) && is_suffix_of($suffs[$previously], $string2)) {\n\t\t\t\t$prefs[$length] = $string1;\n\t\t\t\t$suffs[$length] = $string2;\n\t\t\t\t$log[$length] = \"PS\";\n\t\t\t} elsif (is_prefix_of($prefs[$previously], $string2) && is_suffix_of($suffs[$previously], $string1)) {\n\t\t\t\t$prefs[$length] = $string2;\n\t\t\t\t$suffs[$length] = $string1;\n\t\t\t\t$log[$length] = \"SP\";\n\t\t\t} else {\n\t\t\t\t$flag = \"suffix-prefix-order\";\n\t\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\t$length++;\n\t}\n}\n\nif ($flag eq \"suffix-prefix-order\") {\n\tmy $length = 0;\n\t@log = (\"-\");\n\tforeach my $pairref (@pairrefs) {\n\t\tmy ($string1, $string2) = @$pairref; #print \"$string1 | $string2\\n\";\n\n\t\tif ($length > 0) {\n\t\t\tmy $previously = $length - 1;\n\t\t\tif (is_prefix_of($prefs[$previously], $string2) && is_suffix_of($suffs[$previously], $string1)) {\n\t\t\t\t$prefs[$length] = $string2;\n\t\t\t\t$suffs[$length] = $string1;\n\t\t\t\tpush @log, \"SP\";\n\t\t\t} elsif (is_prefix_of($prefs[$previously], $string1) && is_suffix_of($suffs[$previously], $string2)) {\n\t\t\t\t$prefs[$length] = $string1;\n\t\t\t\t$suffs[$length] = $string2;\n\t\t\t\tpush @log, \"PS\";\n\t\t\t} else {\n\t\t\t\t$flag = \"prefix-suffix-order\";\n\t\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\t$length++;\n\t}\n}\n\n\n#print \"Prefs: \"; print join \", \", @prefs; print \"\\n\";\n#print \"Suffs: \"; print join \", \", @suffs; print \"\\n\";\n#print \"Order: $flag\\n\";\n#print \"Log: @log\\n\";\n\n#print \"Result: \";\nforeach my $i (keys @inputlog) {\n\tmy $length = length $inputlog[$i];\n\tif ($flaglog[$i] eq 'first-in-its-length') {\n\t\tprint substr($log[$length], 0, 1);\n\t} else {\n\t\tprint substr($log[$length], 1, 1);\n\t}\n}\nprint \"\\n\";\n\nsub is_prefix_of {\n\tmy ($part, $whole) = @_;\n\treturn (substr $whole, 0, length $part) eq $part; \n}\n\n\nsub is_suffix_of {\n\tmy ($part, $whole) = @_;\n\treturn 1 if $part eq \"\";\n\treturn (substr $whole, -length $part) eq $part; \n}"}], "negative_code": [{"source_code": "#!usr/bin/perl\n\nuse strict;\nuse warnings;\n\n<> =~ m/(d+)/;\nmy $n = $1;\n\nmy @inputlog = ();\nmy @flaglog = ();\nmy @log = ();\n\n\nmy @pairrefs = ([\"\", \"\"]);\nwhile (<>) {\n\tchomp;\n\tpush @inputlog, $_;\n\tunless (defined $pairrefs[length]) {\n\t\t$pairrefs[length] = [$_];\n\t\tpush @flaglog, \"first-in-its-length\";\n\t} else {\n\t\tpush @{$pairrefs[length]}, $_;\n\t\tpush @flaglog, \"second-in-its-length\";\n\t}\n}\n\n#print \"- @{$pairrefs[0]}\\n\";\n#print \"- @{$pairrefs[1]}\\n\";\n#print \"- @{$pairrefs[2]}\\n\";\n#print \"- @{$pairrefs[3]}\\n\";\n\n#my @pairrefs2 = ([\"e\", \"k\"], [\"te\", \"ko\"], [\"kor\", \"rte\"], [\"orte\", \"kort\"]);\n\nmy @prefs = (\"\");\nmy @suffs = (\"\");\n\n\nmy $flag = \"prefix-suffox-order\";\n \nif ($flag eq \"prefix-suffox-order\") {\n\tmy $length = 0;\n\t@log = (\"-\");\n\tforeach my $pairref (@pairrefs) {\n\t\tmy ($string1, $string2) = @$pairref; #print \"$string1 | $string2\\n\";\n\n\t\tif ($length > 0) {\n\t\t\tmy $previously = $length - 1;\n\t\t\tif (is_prefix_of($prefs[$previously], $string1) && is_suffix_of($suffs[$previously], $string2)) {\n\t\t\t\t$prefs[$length] = $string1;\n\t\t\t\t$suffs[$length] = $string2;\n\t\t\t\t$log[$length] = \"PS\";\n\t\t\t} elsif (is_prefix_of($prefs[$previously], $string2) && is_suffix_of($suffs[$previously], $string1)) {\n\t\t\t\t$prefs[$length] = $string2;\n\t\t\t\t$suffs[$length] = $string1;\n\t\t\t\t$log[$length] = \"SP\";\n\t\t\t} else {\n\t\t\t\t$flag = \"suffix-prefix-order\";\n\t\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\t$length++;\n\t}\n}\n\nif ($flag eq \"suffix-prefix-order\") {\n\tmy $length = 0;\n\t@log = (\"-\");\n\tforeach my $pairref (@pairrefs) {\n\t\tmy ($string1, $string2) = @$pairref; print \"$string1 | $string2\\n\";\n\n\t\tif ($length > 0) {\n\t\t\tmy $previously = $length - 1;\n\t\t\tif (is_prefix_of($prefs[$previously], $string2) && is_suffix_of($suffs[$previously], $string1)) {\n\t\t\t\t$prefs[$length] = $string2;\n\t\t\t\t$suffs[$length] = $string1;\n\t\t\t\tpush @log, \"SP\";\n\t\t\t} elsif (is_prefix_of($prefs[$previously], $string1) && is_suffix_of($suffs[$previously], $string2)) {\n\t\t\t\t$prefs[$length] = $string1;\n\t\t\t\t$suffs[$length] = $string2;\n\t\t\t\tpush @log, \"PS\";\n\t\t\t} else {\n\t\t\t\t$flag = \"prefix-suffix-order\";\n\t\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\t$length++;\n\t}\n}\n\n\n#print \"Prefs: \"; print join \", \", @prefs; print \"\\n\";\n#print \"Suffs: \"; print join \", \", @suffs; print \"\\n\";\n#print \"Order: $flag\\n\";\n#print \"Log: @log\\n\";\n\n#print \"Result: \";\nforeach my $i (keys @inputlog) {\n\tmy $length = length $inputlog[$i];\n\tif ($flaglog[$i] eq 'first-in-its-length') {\n\t\tprint substr($log[$length], 0, 1);\n\t} else {\n\t\tprint substr($log[$length], 1, 1);\n\t}\n}\nprint \"\\n\";\n\nsub is_prefix_of {\n\tmy ($part, $whole) = @_;\n\treturn (substr $whole, 0, length $part) eq $part; \n}\n\n\nsub is_suffix_of {\n\tmy ($part, $whole) = @_;\n\treturn 1 if $part eq \"\";\n\treturn (substr $whole, -length $part) eq $part; \n}\n"}], "src_uid": "ddbde202d00b928a858e9f4ff461e88c"} {"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)$$$\u00a0\u2014 the first boy enters two pairs; $$$(1, 2)$$$ and $$$(2, 2)$$$\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t( $k, %h, %g, $c ) = ( split )[ 2 ];\r\n\t\r\n\t$_ = <>;\r\n\t@B = split ' ', <>;\r\n\t\r\n\twhile( /\\d+/g ){\r\n\t\t$B = shift @B;\r\n\t\t\r\n\t\t$h{ $B }{ $& } =\r\n\t\t$g{ $& }{ $B } ++;\r\n\t\t}\r\n\t\r\n\tfor( sort keys %h ){\r\n\t\t$q = $k - ( @j = sort keys %{ $h{ $_ } } );\r\n\t\t\r\n\t\tfor $j ( @j ){\r\n\t\t\t$c += 1 + $q - keys %{ $g{ $j } };\r\n\t\t\t}\r\n\t\t}\r\n\t\r\n\tprint $c / 2\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B, $k ) = split;\n\t\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\t\n\tmy %h;\n\tmy %g;\n\t\n\twhile( @A ){\n\t\tmy $A = shift @A;\n\t\tmy $B = shift @B;\n\t\t\n\t\t$h{ $A }{ $B } ++;\n\t\t$g{ $B }{ $A } ++;\n\t\t}\n\t\n\tmy $cnt = 0;\n\t\n\tfor my $key ( sort { $a <=> $b } keys %h ){\n\t\tmy $k1 = $k - keys %{ $h{ $key } };\n\t\t\n\t\tfor my $key_back ( sort { $a <=> $b } keys %{ $h{ $key } } ){\n\t\t\t$cnt += $k1 - ( keys %{ $g{ $key_back } } ) + 1;\n\t\t\t}\n\t\t}\n\t\n\tprint $cnt / 2;\n\t}"}], "negative_code": [], "src_uid": "14ce451a31c0dbc2b2f4e04a939b199d"} {"nl": {"description": "Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t\u2009=\u2009\"nastya\" and a\u2009=\u2009[4,\u20091,\u20095,\u20093,\u20092,\u20096] then removals make the following sequence of words \"nastya\" \"nastya\" \"nastya\" \"nastya\" \"nastya\" \"nastya\" \"nastya\".Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.It is guaranteed that the word p can be obtained by removing the letters from word t.", "input_spec": "The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1\u2009\u2264\u2009|p|\u2009<\u2009|t|\u2009\u2264\u2009200\u2009000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1,\u2009a2,\u2009...,\u2009a|t| of letter indices that specifies the order in which Nastya removes letters of t (1\u2009\u2264\u2009ai\u2009\u2264\u2009|t|, all ai are distinct).", "output_spec": "Print a single integer number, the maximum number of letters that Nastya can remove.", "sample_inputs": ["ababcba\nabb\n5 3 4 1 7 6 2", "bbbabb\nbb\n1 6 3 4 2 5"], "sample_outputs": ["3", "4"], "notes": "NoteIn the first sample test sequence of removing made by Nastya looks like this:\"ababcba\" \"ababcba\" \"ababcba\" \"ababcba\" Nastya can not continue, because it is impossible to get word \"abb\" from word \"ababcba\".So, Nastya will remove only three letters."}, "positive_code": [{"source_code": "sub input { $_ = <>; chomp; split shift }\n@t = input qr//;\n@p = input qr//;\n@a = map $_ - 1, input \" \";\n\nsub t {\n\tmy $a = shift;\n\tmy @b; $b[ $a[$_] ] = 1 for 0 .. $a;\n\tfor ($i = $j = 0; $i < @t; $i++) {\n\t\t$j++ if !$b[$i] && $t[$i] eq $p[$j];\n\t}\n\t$j == @p? $a + 1: 0;\n}\n\n$a1 = 0; $a2 = @a - 1;\nwhile ($a1 + 1 < $a2) {\n\t$a = int(($a1 + $a2) / 2);\n\tt($a)? $a1 = $a: $a2 = $a;\n}\n\n$_ = t($a1) || t($a2); print;\n"}], "negative_code": [{"source_code": "sub input { $_ = <>; chomp; split shift }\n@t = input qr//;\n@p = input qr//;\n@a = map $_ - 1, input \" \";\n\nsub t {\n\tmy $a = shift;\n\tmy @b;\n\t$b[$a[$_]] = 1 for 0..$a;\n\tfor ($i = $j = 0; $i < @t; $i++) {\n\t\t$j++ if !$b[$i] && $t[$i] eq $p[$j];\n\t}\n\t$j == @p? $a: 0;\n}\n\n$a1 = 0; $a2 = @a - 1;\nwhile ($a1 + 1 < $a2) {\n\t$a = int(($a1 + $a2) / 2);\n\tt($a)? $a1 = $a: $a2 = $a;\n}\n\nprint 1 + (t($a1) || t($a2) || -1);\n"}], "src_uid": "0aed14262c135d1624df9814078031ae"} {"nl": {"description": "Yelisey has an array $$$a$$$ of $$$n$$$ integers.If $$$a$$$ has length strictly greater than $$$1$$$, then Yelisei can apply an operation called minimum extraction to it: First, Yelisei finds the minimal number $$$m$$$ in the array. If there are several identical minima, Yelisey can choose any of them. Then the selected minimal element is removed from the array. After that, $$$m$$$ is subtracted from each remaining element. Thus, after each operation, the length of the array is reduced by $$$1$$$.For example, if $$$a = [1, 6, -4, -2, -4]$$$, then the minimum element in it is $$$a_3 = -4$$$, which means that after this operation the array will be equal to $$$a=[1 {- (-4)}, 6 {- (-4)}, -2 {- (-4)}, -4 {- (-4)}] = [5, 10, 2, 0]$$$.Since Yelisey likes big numbers, he wants the numbers in the array $$$a$$$ to be as big as possible.Formally speaking, he wants to make the minimum of the numbers in array $$$a$$$ to be maximal possible (i.e. he want to maximize a minimum). To do this, Yelisey can apply the minimum extraction operation to the array as many times as he wants (possibly, zero). Note that the operation cannot be applied to an array of length $$$1$$$.Help him find what maximal value can the minimal element of the array have after applying several (possibly, zero) minimum extraction operations to the array.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$)\u00a0\u2014 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 an integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$)\u00a0\u2014 the original length of the array $$$a$$$. The second line of the description lists $$$n$$$ space-separated integers $$$a_i$$$ ($$$-10^9 \\leq a_i \\leq 10^9$$$)\u00a0\u2014 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 of them containing the answer to the corresponding test case. The answer to the test case is a single integer\u00a0\u2014 the maximal possible minimum in $$$a$$$, which can be obtained by several applications of the described operation to it.", "sample_inputs": ["8\n1\n10\n2\n0 0\n3\n-1 2 0\n4\n2 10 1 7\n2\n2 3\n5\n3 2 -4 -2 0\n2\n-1 1\n1\n-2"], "sample_outputs": ["10\n0\n2\n5\n2\n2\n2\n-2"], "notes": "NoteIn the first example test case, the original length of the array $$$n = 1$$$. Therefore minimum extraction cannot be applied to it. Thus, the array remains unchanged and the answer is $$$a_1 = 10$$$.In the second set of input data, the array will always consist only of zeros.In the third set, the array will be changing as follows: $$$[\\color{blue}{-1}, 2, 0] \\to [3, \\color{blue}{1}] \\to [\\color{blue}{2}]$$$. The minimum elements are highlighted with $$$\\color{blue}{\\text{blue}}$$$. The maximal one is $$$2$$$.In the fourth set, the array will be modified as $$$[2, 10, \\color{blue}{1}, 7] \\to [\\color{blue}{1}, 9, 6] \\to [8, \\color{blue}{5}] \\to [\\color{blue}{3}]$$$. Similarly, the maximum of the minimum elements is $$$5$$$."}, "positive_code": [{"source_code": "use List::Util qw(first);\r\nuse List::Util qw(min);\r\nuse List::Util qw(max);\r\nuse POSIX;\r\nmy $line;\r\nmy @input1; my @input2;\r\nmy $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;\r\nmy $s = 0; my $c; my $result; my $i; my %h;\r\n\r\n$line = ;\r\nchomp($line);\r\n@input1 = split(' ', $line);\r\n\r\n#$line = ;\r\n#chomp($line);\r\n#@input2 = split(' ', $line);\r\n\r\nmy $line2;\r\n\r\nfor(1..@input1[0]) {\r\n $s = ;\r\n chomp($s);\r\n $line = ;\r\n chomp($line);\r\n @input2 = split(' ', $line);\r\n @input2 = sort {$a <=> $b} @input2;\r\n $max = @input2[0];\r\n $c = 0;\r\n foreach (@input2) {\r\n $max = max($max, $_ - $c);\r\n $c = $_;\r\n }\r\n print $max, \"\\n\";\r\n}"}], "negative_code": [], "src_uid": "4bd7ef5f6b3696bb44e22aea87981d9a"} {"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": "#!/usr/bin/perl -w\n\nuse strict;\nuse List::Util qw(max min);\n\nchomp($_ = <>);\nmy ($max, $min, $count) = (0, 0, 0);\nfor my $i (split //) {\n if ($i eq '+') {\n ++$count;\n } else {\n --$count;\n }\n $max = max($max, $count);\n $min = min($min, $count);\n}\nprint $max - $min, \"\\n\";\n"}, {"source_code": "chomp($_=<>);\n@_=split//;\n$max=0;\nfor $j('-','+'){\n$m=0;\nfor $i(0..@_-1){\n\tif ($_[$i] eq $j){$m++; $m>$max and $max=$m}\n\telse {$m--; $m<0 and $m=0}\n\t}\n}\nprint $max"}], "negative_code": [], "src_uid": "a9cd99d74418b5f227b358a910496b02"} {"nl": {"description": "Ehab loves number theory, but for some reason he hates the number $$$x$$$. Given an array $$$a$$$, find the length of its longest subarray such that the sum of its elements isn't divisible by $$$x$$$, or determine that such subarray doesn't exist.An array $$$a$$$ is a subarray of an array $$$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.", "input_spec": "The first line contains an integer $$$t$$$ $$$(1 \\le t \\le 5)$$$\u00a0\u2014 the number of test cases you need to solve. The description of the test cases follows. The first line of each test case contains 2 integers $$$n$$$ and $$$x$$$ ($$$1 \\le n \\le 10^5$$$, $$$1 \\le x \\le 10^4$$$)\u00a0\u2014 the number of elements in the array $$$a$$$ and the number that Ehab hates. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$\\ldots$$$, $$$a_{n}$$$ ($$$0 \\le a_i \\le 10^4$$$)\u00a0\u2014 the elements of the array $$$a$$$.", "output_spec": "For each testcase, print the length of the longest subarray whose sum isn't divisible by $$$x$$$. If there's no such subarray, print $$$-1$$$.", "sample_inputs": ["3\n3 3\n1 2 3\n3 4\n1 2 3\n2 2\n0 6"], "sample_outputs": ["2\n3\n-1"], "notes": "NoteIn the first test case, the subarray $$$[2,3]$$$ has sum of elements $$$5$$$, which isn't divisible by $$$3$$$.In the second test case, the sum of elements of the whole array is $$$6$$$, which isn't divisible by $$$4$$$.In the third test case, all subarrays have an even sum, so the answer is $$$-1$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor(my $i=0;$i<$t;$i++){\n my ($n,$x) = map { $_ - 0 } split(/\\s+/,);\n my @a = map { $_ - 0 } split(/\\s+/,);\n if( $x == 1 ){\n print \"-1\\n\"; next;\n }\n my @s = (); $#s = $n;\n \n my $sm = 0;\n $s[0] = $sm;\n my $nzidx = -1;\n for(my $j=0;$j<$n;$j++){\n $sm = ( $sm + $a[$j] ) % $x;\n $s[1+$j] = $sm;\n $nzidx = 1+$j if $sm != 0;\n }\n if($nzidx==-1){\n print \"-1\\n\"; next;\n }\n my $j;\n for($j=0;$j<$n;$j++){\n last if( $s[$j] != $s[$n] );\n }\n my $d1 = $nzidx;\n my $d2 = $n - $j;\n $d1 = $d2 if( $d2 > $d1 );\n print \"$d1\\n\";\n}\n\n\n"}], "negative_code": [], "src_uid": "f5bcde6e3008405f61cead4e3f44806e"} {"nl": {"description": " William has array of $$$n$$$ numbers $$$a_1, a_2, \\dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \\frac{a_i}{2}$$$ $$$a_j = a_j \\cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.", "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 an integer $$$n$$$ $$$(1 \\le n \\le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ $$$(1 \\le a_i < 16)$$$, the contents of William's array.", "output_spec": "For each test case output the maximal sum of array elements after performing an optimal sequence of operations.", "sample_inputs": ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"], "sample_outputs": ["50\n46\n10\n26\n35184372088846"], "notes": "NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \\frac{4}{2} = 2$$$ and $$$a_1 = 6 \\cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \\frac{2}{2} = 1$$$ and $$$a_1 = 12 \\cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \\frac{2}{2} = 1$$$ and $$$a_1 = 24 \\cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @A;\n\t\n\tmy @copy = @_;\n\t\n\tmy @remain;\n\t\n\tfor my $i ( @copy ){\n\t\tmy $two = 0;\n\t\twhile( $i % 2 == 0 ){\n\t\t\t$i /= 2;\n\t\t\t$two ++;\n\t\t\t}\n\t\t\n\t\tpush @A, $two;\n\t\tpush @remain, $i;\n\t\t}\n\t\n\tmy $sum_twos = 0;\n\t\n\t$sum_twos += $_ for @A;\n\t\n\tmy $pow_twos = 2 ** $sum_twos;\n\t\n\tmy $sum_remain = 0;\n\t\n\t$sum_remain += $_ for @remain;\n\t\n\tmy @cand;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tpush @cand, $sum_remain - $remain[ $i ] + $remain[ $i ] * $pow_twos;\n\t\t}\n\t\n\tprint 0 + ( sort { $b <=> $a } @cand )[ 0 ];\n\t}"}], "negative_code": [], "src_uid": "f5de1e9b059bddf8f8dd46c18ce12683"} {"nl": {"description": "Polycarp has a string $$$s$$$. Polycarp performs the following actions until the string $$$s$$$ is empty ($$$t$$$ is initially an empty string): he adds to the right to the string $$$t$$$ the string $$$s$$$, i.e. he does $$$t = t + s$$$, where $$$t + s$$$ is a concatenation of the strings $$$t$$$ and $$$s$$$; he selects an arbitrary letter of $$$s$$$ and removes from $$$s$$$ all its occurrences (the selected letter must occur in the string $$$s$$$ at the moment of performing this action). Polycarp performs this sequence of actions strictly in this order.Note that after Polycarp finishes the actions, the string $$$s$$$ will be empty and the string $$$t$$$ will be equal to some value (that is undefined and depends on the order of removing).E.g. consider $$$s$$$=\"abacaba\" so the actions may be performed as follows: $$$t$$$=\"abacaba\", the letter 'b' is selected, then $$$s$$$=\"aacaa\"; $$$t$$$=\"abacabaaacaa\", the letter 'a' is selected, then $$$s$$$=\"c\"; $$$t$$$=\"abacabaaacaac\", the letter 'c' is selected, then $$$s$$$=\"\" (the empty string). You need to restore the initial value of the string $$$s$$$ using only the final value of $$$t$$$ and find the order of removing letters from $$$s$$$.", "input_spec": "The first line contains one integer $$$T$$$ ($$$1 \\le T \\le 10^4$$$) \u2014 the number of test cases. Then $$$T$$$ test cases follow. Each test case contains one string $$$t$$$ consisting of lowercase letters of the Latin alphabet. The length of $$$t$$$ doesn't exceed $$$5 \\cdot 10^5$$$. The sum of lengths of all strings $$$t$$$ in the test cases doesn't exceed $$$5 \\cdot 10^5$$$.", "output_spec": "For each test case output in a separate line: $$$-1$$$, if the answer doesn't exist; two strings separated by spaces. The first one must contain a possible initial value of $$$s$$$. The second one must contain a sequence of letters \u2014 it's in what order one needs to remove letters from $$$s$$$ to make the string $$$t$$$. E.g. if the string \"bac\" is outputted, then, first, all occurrences of the letter 'b' were deleted, then all occurrences of 'a', and then, finally, all occurrences of 'c'. If there are multiple solutions, print any one. ", "sample_inputs": ["7\nabacabaaacaac\nnowyouknowthat\npolycarppoycarppoyarppyarppyrpprppp\nisi\neverywherevrywhrvryhrvrhrvhv\nhaaha\nqweqeewew"], "sample_outputs": ["abacaba bac\n-1\npolycarp lcoayrp\nis si\neverywhere ewyrhv\n-1\n-1"], "notes": "NoteThe first test case is considered in the statement."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\n\r\nmy ($tcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $tcase -- > 0 ){\r\n my $t = ;\r\n $t =~ s/[\\x00-\\x20]+$//ios;\r\n \r\n my @seq = ();\r\n my %lc = ();\r\n for(my $i=length($t)-1;$i>=0;$i--){\r\n my $c1 = substr($t,$i,1);\r\n unshift(@seq,$c1) if $lc{$c1} - 0 == 0;\r\n $lc{$c1}++;\r\n }\r\n my $er = undef;\r\n my $initl = 0;\r\n for(my $i=0;$i<=$#seq;$i++){\r\n if( $lc{$seq[$i]} % (1+$i) != 0 ){\r\n $er=1; last;\r\n }\r\n $lc{$seq[$i]} /= (1+$i);\r\n $initl += $lc{$seq[$i]};\r\n }\r\n if( $er ){ print \"-1\\n\"; next; }\r\n my $st = substr($t,0,$initl);\r\n my $s = $st;\r\n my $ch = $t;\r\n for(my $i=0;$i<=$#seq;$i++){\r\n if( substr($ch,0,length($s)) ne $s ){\r\n $er = 1; last;\r\n }\r\n substr($ch,0,length($s)) = '';\r\n $s =~ s/$seq[$i]//ig;\r\n }\r\n if( $er || ( $ch ne '' and $s eq '' ) ){ print \"-1\\n\"; next; }\r\n print ( $st . \" \" . join('',@seq) . \"\\n\" );\r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [], "src_uid": "8705adec1bea1f898db1ca533e15d5c3"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of boys. The second line contains sequence a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009100), where ai is the i-th boy's dancing skill. Similarly, the third line contains an integer m (1\u2009\u2264\u2009m\u2009\u2264\u2009100) \u2014 the number of girls. The fourth line contains sequence b1,\u2009b2,\u2009...,\u2009bm (1\u2009\u2264\u2009bj\u2009\u2264\u2009100), where bj is the j-th girl's dancing skill.", "output_spec": "Print a single number \u2014 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": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = sort {$a <=> $b} split / /, <>;\nchomp($m = <>);\n@b = sort {$a <=> $b} split / /, <>;\n($ans, $i, $j) = (0) x 3;\nwhile ($i < $n && $j < $m) {\n\tif (abs($a[$i]-$b[$j]) <= 1) {\n\t\t++$i;\n\t\t++$j;\n\t\t++$ans;\n\t} elsif ($a[$i] > $b[$j]) {\n\t\t++$j;\n\t} else {\n\t\t++$i;\n\t}\n}\nsay $ans;"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\n<>;\nmy @boys = sort { $a <=> $b } split ' ', <>;\n<>;\nmy @girls = sort { $a <=> $b } split ' ', <>;\n\nmy $c = 0;\n\nwhile (@boys && @girls) {\n if (abs($boys[0] - $girls[0]) <= 1) {\n shift @boys;\n shift @girls;\n $c++;\n } elsif ($boys[0] < $girls[0]) {\n shift @boys;\n } else {\n shift @girls;\n }\n}\n\nsay $c;\n"}, {"source_code": "\tuse integer;\n\t\n\twhile(<>){\n\t\tchomp;\n\t#\t($n,$m)=split/ /,$_;\n\t#\tchomp ($n = <>);\n\t\tchomp ($a = <>);\n\t\tchomp ($m = <>);\n\t\tchomp ($b = <>);\n\t\t@a = sort {$a <=> $b} split/ /,$a;\n\t\t@b = sort {$a <=> $b} split/ /,$b;\n\t\t\n\t\t$c=0;\n\t\t$i=$j=0;\n\t\twhile ($i < @a and $j < @b){\n\t\t\t$dir = $a[$i] <=> $b[$j];\n\t\t\tif (abs($a[$i] - $b[$j]) < 2){\n\t\t\t\t$c++;\n\t\t\t\t$i++;\n\t\t\t\t$j++;\n\t\t\t\t}\n\t\t\telsif ($dir == -1){\n\t\t\t\t$i++;\n\t\t\t\t}\n\t\t\telse {\n\t\t\t\t$j++;\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t\n\t\t\n\t\tprint $c,$/\n\t\t}"}], "negative_code": [], "src_uid": "62766ef9a0751cbe7987020144de7512"} {"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\u00a0\u2014 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\u2009\u2009\u2264\u2009\u2009|s|\u2009\u2009\u2264\u2009\u2009105)\u00a0\u2014 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": "use warnings;\n\nsub min {\n\tmy $ans = $_[0];\n\t\n\tforeach my $i (@_) {\n\t\tif ($ans > $i) {\n\t\t\t$ans = $i;\n\t\t}\n\t}\n\t\n\treturn $ans;\n}\n\nmy @fr = (0) x 128;\n\n\nmy $text = ;\nchomp($text);\n\nfor (my $i = 0; $i < length $text; ++$i) {\n\t++$fr[ord(substr($text, $i, 1))];\n#\tprint substr($text, $i, 1);\n}\n\nprintf(\"%d\", min($fr[ord('B')], $fr[ord 'a']/2, $fr[ord 'b'], $fr[ord 's'], $fr[ord 'l'], $fr[ord 'r'], $fr[ord 'u']/2));\n"}, {"source_code": "$_ = <>;\nfor $c (qw(B u l b a s r)) {\n\t$h{$c} = () = /$c/g;\n}\n$h{u} /= 2;\n$h{a} /= 2;\n@ans = sort {$a <=> $b} values %h;\nprint int $ans[0] || 0;\n"}, {"source_code": "use warnings;\n\nuse v5.20;\n\nmy @frecv;\nmy @word = ('B','u','l','b','a','s','a','u','r');\nmy $solution = 1000000;\n\nmy $input = ;\nchomp $input;\nmy @chars = split(\"\", $input);\n\nfor(my $i = 0; $i < length $input; $i += 1){\n my $c = ord($chars[$i]);\n $frecv[$c]++;\n}\n\nfor(my $i = 0; $i < 9; $i += 1){\n my $c = $word[$i];\n my $poz = ord($word[$i]);\n unless(defined $frecv[$poz]) { \n $solution = 0;\n last;\n }\n if($c eq 'a' || $c eq 'u'){\n if(int($frecv[$poz] / 2) < $solution){\n $solution = int($frecv[$poz] / 2);\n }\n }\n elsif($frecv[$poz] < $solution){\n $solution = $frecv[$poz];\n }\n \n}\nprint(\"$solution\\n\");"}, {"source_code": "# Find out how many Bulbasaurs we can catch\n# Solution: \nuse strict;\nuse warnings;\nmy $newspaper = <>;\nmy %letter;\nmy $bulbies = 0;\nforeach(split//,$newspaper)\n{\n\tif(exists($letter{$_}))\n\t{\n\t\t$letter{$_} += 1;\n\t\tif($bulbies < $letter{$_})\n\t\t{\n\t\t\t$bulbies = $letter{$_};\n\t\t}\n\t}\n\telse\n\t{\n\t\t$letter{$_} = 1;\n\t\tif($bulbies == 0)\n\t\t{\n\t\t\t$bulbies = 1;\n\t\t}\n\t}\n}\nif(exists($letter{\"a\"}))\n{\n\t$letter{\"a\"} = int($letter{\"a\"}/2);\n}\nif(exists($letter{\"u\"}))\n{\n\t$letter{\"u\"} = int($letter{\"u\"}/2);\n}\nforeach(split//,\"Bulbasr\")\n{\n\tif(exists($letter{$_}))\n\t{\n\t\tif($bulbies > $letter{$_})\n\t\t{\n\t\t\t$bulbies = $letter{$_}\n\t\t}\n\t}\n\telse\n\t{\n\t\t$bulbies = 0;\n\t}\n}\nprint int($bulbies);\n"}, {"source_code": "use feature \"say\";\n\n$text = <>;\n@matches;\n@target = ( \"B\", \"u\", \"l\", \"b\", \"a\", \"s\", \"r\" );\nfor ($i = 0; $i < scalar ( @target ); ++$i) {\n\t$matches[$i] = () = $text =~ /$target[$i]/g;\n}\n$matches[1] /= 2;\n$matches[4] /= 2;\n@matches = sort( { $a <=> $b } @matches );\nsay int $matches[0];\n"}, {"source_code": "use feature \"say\";\n\n$text = <>;\n@sir;\n\n$h{B} = () = $text =~ /B/g;\npush(@sir,$h{B});\n\n$h{u} = () =$text =~ /u/g;\npush(@sir,$h{u} / 2 );\n\n$h{l} = () =$text =~ /l/g;\npush(@sir,$h{l});\n\n$h{b} = () =$text =~ /b/g;\npush(@sir,$h{b});\n\n$h{a} = () =$text =~ /a/g;\npush(@sir,$h{a} / 2);\n\n$h{s} = () =$text =~ /s/g;\npush(@sir,$h{s});\n\n$h{r} = () =$text =~ /r/g;\npush(@sir,$h{r});\n\n@sir = sort ( {$a <=> $b} @sir );\n\nsay int $sir[0];"}, {"source_code": "$,=\" \";\n\nmy %c;\nmap {$c{$_}++ }split //, \"Bulbasaur\";\nmy %d;\n\nmy $w=<>;\nchomp($w);\nmap {$d{$_}++ if $c{$_}}split //, $w;\nfor my $t (keys %c)\n{\n$d{$t}=int($d{$t}/$c{$t});\n}\n\nprint min([values %d]) , \"\\n\";\n\nsub min_{\n\treturn $_[0] > $_[1] ? $_[1] : $_[0];\n}\n\nsub max_{\n\treturn $_[1] > $_[0] ? $_[1] : $_[0];\n}\n\nsub min{\n\tmy $a=shift;\n\tmy $mm=$a->[0];\n\tfor(my $i=1;$i<@{$a};$i++)\n\t{\n\t\t$mm=min_($mm,$a->[$i]);\n\t}\n\treturn $mm;\n}\n\nsub max{\n\tmy $a=shift;\n\tmy $mm=$a->[0];\n\tfor(my $i=1;$i<@{$a};$i++)\n\t{\n\t\t$mm=max_($mm,$a->[$i]);\n\t}\n\treturn $mm;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $string = <>;\nchomp($string);\n\nmy %validLetters = ('B' => 0, 'u' => 0, 'l' => 0, 'b' => 0, 'a' => 0, 's' => 0, 'r' => 0);\nfor (my $i = 0; $i < length($string); $i++) {\n my $char = substr($string, $i, 1);\n if (exists($validLetters{$char})) {\n $validLetters{$char} += 1;\n }\n}\n\nmy $count = 0;\n\nmy $possible = 1;\nwhile ($possible) {\n foreach my $key (keys(%validLetters)) {\n if ($key eq 'a' || $key eq 'u') {\n if ($validLetters{$key} < 2) {\n $possible = 0;\n } else {\n $validLetters{$key} -= 2;\n }\n } else {\n if ($validLetters{$key} < 1) {\n $possible = 0;\n } else {\n $validLetters{$key} -= 1;\n }\n }\n }\n\n if ($possible) {\n $count++;\n }\n}\n\nprint $count, \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\tmy %h;\n\t\n\tmap { ++$h{ $_ } } split //;\n\t\n\tmy @occ;\n\t\n\tpush @occ, 2 * $h{ $_ } for split //, 'Blbsr';\n\tpush @occ, $h{ $_ } for split //, 'au';\n\t\n\tmy $min = ( sort { $a <=> $b } @occ )[ 0 ];\n\tprint int $min / 2;\n\t\n\t}"}, {"source_code": "%h = map { $_ => 0 } @m = split //, 'Bulbasaur';\nmap $h{ $_ } += !/a|u/ + 1, <> =~ /[@m]/g;\n\nprint( ( sort { $a <=> $b } values %h )[ 0 ] >> 1 )"}, {"source_code": "use v5.20;\nmy $input = <>;\nchomp( $input );\nmy @s = split //, $input;\nmy $word = \"uaBlbsur\";\nmy @rez = split //, $word;\nmy $h = {};\nfor(my $j = 0; $j < length $word; $j++){\n\t$h->{ $rez[$j] } = 0;}\nmy $ans = 1000000000;\nfor(my $i = 0; $i < length $input; $i++){\n\t$h->{ $s[$i] }++;}\n$h->{ $rez[1] } /= 2;\n$h->{ $rez[0] } /= 2;\nfor(my $j = 0; $j < length $word; $j++){\n\tif($ans > $h->{ $rez[$j] }){\n\t\t$ans = $h->{ $rez[$j] };}}\nsay int($ans);"}, {"source_code": "use v5.20;\nuse warnings;\n\nsub min {\n\tmy $ret = $_[0];\n\tforeach my $it (@_) {\n\t\tif ($ret > $it) { \n\t\t\t$ret = $it;\n\t\t}\n\t}\n\treturn $ret;\n}\n\nmy @fr = (0) x 300;\n\n$_ = <>;\n\nforeach my $it (split '') {\n\t$fr[ord $it] += 1;\n}\n\nsay min($fr[ord 'B'],int ($fr[ord 'a']/2),$fr[ord 'b'],$fr[ord 's'],$fr[ord 'l'],$fr[ord 'r'],int ($fr[ord 'u']/2));\n\n"}, {"source_code": "use v5.20;\n\n\nsub minn {\n\tmy ($a, $b) = @_;\n\tif ($a < $b) {\n\t\treturn $a;\n\t}\n\treturn $b;\n}\n\nmy $target = \"uaBlbsr\";\nmy %ap = map { $_ => 0 } (0..length($target));\n\nmy $text = <>;\nfor (my $i = 0; $i < length $text; $i++) {\n\tmy $ind = index($target, substr($text, $i, 1));\n\tif ($ind != -1) {\n\t\t$ap{$ind}++;\n\t}\n}\n\nmy $mn = int(minn($ap{0} / 2, $ap{1} / 2));\nfor (my $i = 2; $i < length $target; $i++) {\n\t$mn = minn($mn, $ap{$i});\n}\n\nsay $mn;"}, {"source_code": "#use v5.24;\nuse warnings;\nmy %fr;\n\nmy $line=;\nchomp($line);\nmy @word=split(//,$line);\nfor (my $i=ord(\"A\");$i<=ord(\"z\");$i++){\n\t$fr{chr($i)}=0;\n}\nforeach my $lit (@word){\n\t$fr{$lit}++;\n}\n$fr{\"u\"}=int $fr{\"u\"}/2;\n$fr{\"a\"}=int $fr{\"a\"}/2;\nmy @key=(\"B\",\"u\",\"l\",\"b\",\"a\",\"s\",\"a\",\"u\",\"r\");\nmy $min=100006;\nforeach my $lit (@key){\n\tif ($min>$fr{$lit}) {$min=$fr{$lit}};\n}\nprint $min;\n;\nexit(0);\n\n#Bulbasaur"}, {"source_code": "use v5.20.1;\nuse warnings;\n\n=pod\nmy $filename = \"date.in\";\nopen(my $fh, '<:encoding(UTF-8)', $filename)\n\tor die \"Could not open the file\";\n=cut\n\nmy $line = ;\nchomp($line);\n\nmy @input = split('', $line);\n\nmy %letters = (\n\n\t'B' => 0,\n\t'u' => 0,\n\t'l' => 0,\n\t'b' => 0,\n\t'a' => 0,\n\t's' => 0,\n\t'r' => 0,\n\n);\n\nfor(my $i = 0; $i < @input; $i++){\n\n\tif( index(\"Bulbasr\", $input[$i]) != -1 ){\n\n\t\t$letters{$input[$i]}++;\n\n\t}\n\n}\n\nmy $mini = 100002;\n\nwhile(my ($key, $value) = each(%letters)){\n\n\n\tif( ($key eq 'a' || $key eq 'u') && int($value/2) < $mini ){\n\n\t\t$mini = int($value/2);\n\n\t}elsif($key ne 'a' && $key ne 'u' && $value < $mini){\n\n\t\t$mini = $value;\n\n\t}\n\n\n}\n\nsay $mini;\n"}], "negative_code": [{"source_code": "use warnings;\n\nmy $line = <>;\nchomp($line);\nmy($muie, $cuie, $c) = split(' ', $line);\n\nmy $i;\nmy @v1;\nmy @v2;\nmy $m = <>;\nchomp($m);\n\nfor($i=1;$i<=$m;++$i) {\n\tmy $line = <>;\n\tchomp($line);\n\tmy($x, $y) = split(' ', $line);\n\t\n\tif($y eq \"USB\") {\n\t\tpush(@v1, $x);\n\t}\n\telse {\n\t\tpush(@v2, $x);\n\t}\n}\n\n@v1 = sort {$a <=> $b} @v1;\n@v2 = sort {$a <=> $b} @v2;\n\n$i=0;\nmy $nr=0;\nwhile($i < scalar @v1 && $i < $muie) {\n\t$nr++;\n\t\n\t$total+=$v1[$i];\n\t$i++;\n}\n\nmy $j=0;\nwhile($j < scalar @v2 && $j < $cuie) {\n\t$nr++;\n\t\n\t$total+=$v2[$j];\n\t$j++;\n}\n\n#for($i=0;$i 0) {\n#\tprintf(\"%d %d %d %d\\n\", $i, $j, scalar @v1, scalar @v2);\n\t\n\tif($j >= scalar @v2 || ($v1[$i] <= $v2[$j] && $i < scalar @v1)) {\n\t\t$total+=$v1[$i];\n\t\t++$i;\n\t}\n\telse {\n\t\t$total+=$v2[$j];\n\t\t++$j;\n\t}\n\t\n\t++$nr;\n\t--$c;\n}\n\n\nprintf(\"%d %d\\n\", $nr, $total);\n"}, {"source_code": "$w = \"Bulbbasaur\";\n$h{$_} = 0 for split //, $w;\nwhile ( sysread(STDIN, $c, 1) ) {\n\t$n = () = $w =~ /$c/g;\n\tif ($n) {\n\t\t$h{$c} += 1/$n;\n\t}\n}\n@ans = sort {$a <=> $b} values %h;\n$ans = $ans[0] || 0;\nprint $ans;\n"}, {"source_code": "while ( sysread(STDIN, $c, 1) ) {\n\t$n = () = \"Bulbasaur\" =~ /$c/g;\n\tif ($n) {\n\t\t$h{$c} += 1/$n;\n\t}\n}\n@ans = sort {$a <=> $b} values %h;\n$ans = $ans[0] || 0;\nprint $ans;\n"}, {"source_code": "# Find the biggest subarray with GCD higher than 1\n# Solution: http://codeforces.com/problemset/submission/757/55701579\nmy $poke_nr = <>;\nmy %pokemons;\nmy @numbers = split(\" \", <>);\nmy $biggest_poke = 0;\nmy $max_poke = 0;\nmy $current_max = 0;\nforeach(@numbers)\n{\n\tif(exists($pokemons{$_}))\n\t{\n\t\t$pokemons{$_} = $pokemons{$_} + 1;\n\t}\n\telse\n\t{\n\t\t$pokemons{$_} = 1;\n\t\tif($_ > $biggest_poke)\n\t\t{\n\t\t\t$biggest_poke = $_;\n\t\t}\n\t}\n}\nforeach my $i(2 .. $biggest_poke)\n{\n\t$current_max = 0;\n\tfor(my $j = $i; $j <= $biggest_poke; $j += $i)\n\t{\n\t\tif(exists($pokemons{$j}))\n\t\t{\n\t\t\t$current_max += $pokemons{$j};\n\t\t}\n\t}\n\tif($current_max > $max_poke)\n\t{\n\t\t$max_poke = $current_max;\n\t}\n}\nif($max_poke > 0)\n{\n\tprint $max_poke;\n}\nelse\n{\n\tif(exists($pokemons{1}))\n\t{\n\t\tprint 1;\n\t}\n\telse\n\t{\n\t\tprint 0;\n\t}\n\n}\n"}, {"source_code": "# Find out how many Bulbasaurs we can catch\n# Solution: \nuse strict;\nuse warnings;\nmy $newspaper = <>;\nmy %letter;\nmy $bulbies = 0;\nforeach(split//,$newspaper)\n{\n\tif(exists($letter{$_}))\n\t{\n\t\t$letter{$_} = $letter{$_} + 1;\n\t\tif($bulbies < $letter{$_})\n\t\t{\n\t\t\t$bulbies = $letter{$_};\n\t\t}\n\t}\n\telse\n\t{\n\t\t$letter{$_} = 1;\n\t\tif($bulbies == 0)\n\t\t{\n\t\t\t$bulbies = 1;\n\t\t}\n\t}\n}\nforeach(split//,\"Bulbasr\")\n{\n\tif(exists($letter{$_}))\n\t{\n\t\tif($_ eq \"a\" or $_ eq \"b\"){\n\t\t\tif($bulbies > $letter{$_} / 2)\n\t\t\t{\n\t\t\t\t$bulbies = $letter{$_} / 2;\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif($bulbies > $letter{$_})\n\t\t\t{\n\t\t\t\t$bulbies = $letter{$_}\n\t\t\t}\n\t\t}\n\t}\n\telse\n\t{\n\t\t$bulbies = 0;\n\t}\n}\nprint $bulbies;\n"}, {"source_code": "$,=\" \";\n\nmy %c;\nmap {$c{$_}++ }split //, \"Bulbasaur\";\nmy %d;\n\nmy $w=<>;\nchomp($w);\nmap {$d{$_}++ }split //, $w;\nfor my $t (keys %c)\n{\n$d{$t}=int($d{$t}/$c{$t});\n}\n\nprint min(values %d) , \"\\n\";\n\nsub min\n{\nmy $ans=shift;\nwhile(@_)\n{\nmy $temp=shift;\n$ans=$temp if($ans>$temp);\n}\nreturn $ans;\n}\n"}, {"source_code": "$,=\" \";\n\nmy %c;\nmap {$c{$_}++ }split //, \"Bulbasaur\";\nmy %d;\n\nmy $w=<>;\nchomp($w);\nmap {$d{$_}++ }split //, $w;\nfor my $t (keys %c)\n{\n$d{$t}=int($d{$t}/$c{$t});\n}\n\nprint min([values %d]) , \"\\n\";\n\nsub min_{\n\treturn $_[0] > $_[1] ? $_[1] : $_[0];\n}\n\nsub max_{\n\treturn $_[1] > $_[0] ? $_[1] : $_[0];\n}\n\nsub min{\n\tmy $a=shift;\n\tmy $mm=$a->[0];\n\tfor(my $i=1;$i<@{$a};$i++)\n\t{\n\t\t$mm=min_($mm,$a->[$i]);\n\t}\n\treturn $mm;\n}\n\nsub max{\n\tmy $a=shift;\n\tmy $mm=$a->[0];\n\tfor(my $i=1;$i<@{$a};$i++)\n\t{\n\t\t$mm=max_($mm,$a->[$i]);\n\t}\n\treturn $mm;\n}\n"}, {"source_code": "$_ = <>;\n$B = 'Bulbasaur' x 2e1;\n$i += $c eq 'B' while ($c = chop $B) && s/$c//;\nprint 0 + $i"}, {"source_code": "map $h{ $_ } += !/a|u/ + 1, <> =~ /[Bulbasaur]/g;\n\nprint( ( sort { $a <=> $b } values %h )[ 0 ] >> 1 )"}, {"source_code": "$B = 'Bulbasaur' x 2e4;\n$i += $c eq 'B' while ($c = chop $B) && s/$c//;\nprint 0 + $i"}, {"source_code": "use v5.20;\nmy $buffer = <>;\nchomp( $buffer );\nmy @arr = split //, $buffer;\nmy @fv = ();\nfor( my $i = 0 ; $i < length $buffer ; $i++ ) {\n\t$fv [$arr[ $i ]] ++;\n}\nmy $ans = @fv[ ord('B') ];\n$ans = $ans <= $fv[ ord('u') ] / 2 ? $ans : $fv[ ord('u') ] / 2;\n$ans = $ans <= $fv[ ord('l') ] ? $ans : $fv[ ord('l') ];\n$ans = $ans <= $fv[ ord('b') ] ? $ans : $fv[ ord('b') ];\n$ans = $ans <= $fv[ ord('a') ] / 2 ? $ans : $fv[ ord('a') ] / 2;\n$ans = $ans <= $fv[ ord('s') ] ? $ans : $fv[ ord('s') ];\n$ans = $ans <= $fv[ ord('r') ] ? $ans : $fv[ ord('r') ];\nsay int($ans);"}, {"source_code": "use v5.20;\nmy $buffer = <>;\nchomp( $buffer );\nmy @arr = split //, $buffer;\nmy @fv = ();\nfor( my $i = 0 ; $i < length $buffer ; $i++ ) {\n\t$fv [$arr[ $i ]] ++;\n}\nmy $ans = @fv[ ord('B') ];\n$ans = $ans <= $fv[ 'u'] / 2 ? $ans : $fv[ 'u' ] / 2;\n$ans = $ans <= $fv[ 'l' ] ? $ans : $fv[ 'l' ];\n$ans = $ans <= $fv[ 'b' ] ? $ans : $fv[ 'b' ];\n$ans = $ans <= $fv[ 'a'] / 2 ? $ans : $fv[ 'a' ] / 2;\n$ans = $ans <= $fv[ 's'] ? $ans : $fv[ 's' ];\n$ans = $ans <= $fv[ 'r' ] ? $ans : $fv[ 'r' ];\nsay int($ans);"}, {"source_code": "use v5.20;\nuse warnings;\n\nsub min {\n\tmy $ret = shift @_;\n\tforeach my $it (@_) {\n\t\tif ($ret > $it) { \n\t\t\t$ret = $it;\n\t\t}\n\t}\n\treturn $ret;\n}\n\nmy @fr = (0) x 300;\n\n$_ = <>;\n\nforeach my $it (split '') {\n\t$fr[ord $it] += 1;\n}\n\nsay min($fr[ord 'B'],int ($fr[ord 'a']/2),$fr[ord 'b'],$fr[ord 's'],$fr[ord 'l'],$fr[ord 'r'],int ($fr[ord 'u']));\n\n"}, {"source_code": "use v5.20;\n\n\nsub minn {\n\tmy ($a, $b) = @_;\n\tif ($a < $b) {\n\t\treturn $a;\n\t}\n\treturn $b;\n}\n\nmy $target = \"uaBlbsr\";\nmy %ap = map { $_ => 0 } (0..length($target));\n\nmy $text = <>;\nfor (my $i = 0; $i < length $text; $i++) {\n\tmy $ind = index($target, substr($text, $i, 1));\n\tif ($ind != -1) {\n\t\t$ap{$ind}++;\n\t}\n}\n\nmy $mn = minn($ap{0} / 2, $ap{1} / 2);\nfor (my $i = 2; $i < length $target; $i++) {\n\t$mn = minn($mn, $ap{$i});\n}\n\nsay $mn;"}, {"source_code": "#use v5.24;\nuse warnings;\nmy %fr;\n\nmy $line=;\nchomp($line);\nmy @word=split(//,$line);\nfor (my $i=ord(\"A\");$i<=ord(\"z\");$i++){\n\t$fr{chr($i)}=0;\n}\nforeach my $lit (@word){\n\t$fr{$lit}++;\n}\nmy @key=(\"B\",\"u\",\"l\",\"b\",\"a\",\"s\",\"a\",\"u\",\"r\");\nmy $min=100006;\nforeach my $lit (@key){\n\tif ($min>$fr{$lit}) {$min=$fr{$lit}};\n}\nprint $min;\n;\nexit(0);\n\n#Bulbasaur"}, {"source_code": "use v5.20.1;\nuse warnings;\n\n=pod\nmy $filename = \"date.in\";\nopen(my $fh, '<:encoding(UTF-8)', $filename)\n\tor die \"Could not open the file\";\n=cut\n\nmy $line = ;\nchomp($line);\n\nmy @input = split('', $line);\n\nmy %letters = (\n\n\t'B' => 0,\n\t'u' => 0,\n\t'l' => 0,\n\t'b' => 0,\n\t'a' => 0,\n\t's' => 0,\n\t'r' => 0,\n\n);\n\nfor(my $i = 0; $i < @input; $i++){\n\n\tif( index(\"Bulbasr\", $input[$i]) != -1 ){\n\n\t\t$letters{$input[$i]}++;\n\n\t}\n\n}\n\nmy $mini = 100002;\n\nwhile(my ($key, $value) = each(%letters)){\n\n\n\tif( ($key eq 'a' || $key eq 'u') && $value/2 < $mini ){\n\n\t\t$mini = $value/2;\n\n\t}elsif($key ne 'a' && $key ne 'u' && $value < $mini){\n\n\t\t$mini = $value;\n\n\t}\n\n\n}\n\nsay $mini;\n"}], "src_uid": "9c429fd7598ea75acce09805a15092d0"} {"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,\u2009a2,\u2009...,\u2009an. 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?\" \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of coins. The second line contains a sequence of n integers a1, a2, ..., an (1\u2009\u2264\u2009ai\u2009\u2264\u2009100) \u2014 the coins' values. All numbers are separated with spaces.", "output_spec": "In the single line print the single number \u2014 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,\u20090 correspondingly). If you take 1 coin, you get sums 3,\u20093. If you take 0 coins, you get sums 0,\u20096. 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,\u20092 or 2,\u20092. In any case, the minimum number of coins equals 2. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nchomp($n = <>);\n@a = sort { $b<=>$a } split / /, <>;\n$tot += $_ foreach (@a);\n$ans = 0;\n$tmp = 0;\nforeach (@a) {\n\t$tmp += $_;\n\t++$ans;\n\t$tmp>$tot-$tmp and last;\n}\nsay $ans;"}, {"source_code": "#!/usr/bin/perl\n\nuse 5.012;\nuse warnings;\n\nmy $n = <>;\nchomp(my $s = <>);\nmy @c = split / /, $s;\n@c = sort { $b <=> $a } @c;\n\nsub sum {\n\tmy $ref = shift;\n\tmy $sum;\n\t$sum += $_ for @$ref;\n\treturn $sum;\n}\n\nmy $cnt = 0;\nmy $sum = 0;\nwhile ($sum <= sum \\@c) {\n\t$sum += shift @c;\n\t$cnt++;\n\tlast unless @c;\n}\nprint $cnt;\n"}, {"source_code": "my $n = <>;\nmy @arr = reverse sort { $a <=> $b } split \" \", <>;\nmy ($mid, $ans, $sum) = (0, 0, 0);\n$mid += $_ foreach(@arr);\n$mid = int($mid / 2);\n\nforeach(@arr) {\n $sum += $_;\n $ans++;\n last if ($sum > $mid);\n}\nprint \"$ans\\n\";"}, {"source_code": "$n=<>;\nchomp($n);\n$c=<>;\nchomp($c);\n\n#print $c;\n\n@c=split ' ',$c;\n\n#print @c;\n\n@c = reverse (sort {$a <=> $b} @c);\n\nforeach (@c)\n{\n $sum+=$_;\n}\n$mid=$sum/2;\n\n#print $mid;\n#print @c;\n$myC=1;\n\nforeach(@c)\n{\n $me+=$_;\n if ($me>$mid)\n {\n print $myC; \n exit; \n }\n $myC++;\n}"}, {"source_code": "#!/usr/bin/perl\n# Codeforces Practice - 160A Twins\nuse warnings;\nuse strict;\n\nmy $num = ;\nchomp $num;\n\nmy @coins = split(\" \",);\nmy $sum = 0;\n$sum += $_ for @coins;\n\n@coins = sort {$b<=>$a} @coins;\n\nmy $sum2 = 0;\nfor(my $i = 1; @coins > 0; $i++){\n my $temp = shift @coins; \n $sum2 += $temp;\n $sum -= $temp;\n if($sum2>$sum){\n print $i;\n last;\n }\n}"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nchomp($n = <>);\n@a = sort { $b<=>$a } split / /, <>;\n$tot += $_ foreach (@a);\n$ans = 0;\n$tmp = 0;\nforeach (@a) {\n\t$tmp += $_;\n\t++$ans;\n\t$tmp>$tot-$tmp and last;\n}\nsay $ans;\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nmy $n = <>;\nmy @x = sort { $a <=> $b } split ' ', <>;\nmy $s = 0;\n$s += $_ for @x;\nmy $t = 0;\n$t += pop @x while $t * 2 <= $s;\nprint $n - @x, \"\\n\";\n"}, {"source_code": "chomp($n = );\nchomp($l = );\n\n@coins = split /\\s+/, $l;\n@coins = sort { $b <=> $a } @coins;\n\n$total = 0;\nmap { $total += $_; } @coins;\n$taken_sum = 0;\n$taken = 0;\nwhile($taken_sum <= ($total - $taken_sum)) {\n\t$taken_sum += shift @coins;\n\t$taken++;\n#\tprint \"after taking $taken coins we have \".$taken_sum.\" and there's \".($total-$taken_sum).\" left\\n\";\n}\n\nprint \"$taken\\n\";"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @a = sort {$b <=> $a} split ' ', <>;\nmy $sum = 0;\nfor (@a) {\n $sum += $_;\n}\n\nmy $res = 0;\nmy $curr = 0;\n\nfor (@a) {\n $res++;\n $curr += $_;\n last if (2*$curr > $sum);\n}\n\nprint \"$res\\n\";\n"}, {"source_code": "$n=<>;\n@arr=split(' ',);\n#@arr=sort(@arr);\n@arr = sort {$a <=> $b} @arr;\n$som=0;\nfor($i=0;$i<$n;$i++){\n$som+=$arr[$i];\n}\n#print \"@arr\\n\";\n$ok=1;\n$i=$n-2;\n$profit=$arr[$n-1];\n$nb=1;\n\nwhile($ok && $i>=0){\nif($profit<=$som/2){\n$nb++;\n$profit+=$arr[$i];\n}\nelse{\n$ok=0;\n}\n$i--;\n}\nprint $nb;"}, {"source_code": ";\n@num = sort { $b <=> $a } split(/ +/, );\n\n$s = $sum = $count = 0;\nforeach(@num) { $sum += $_ }\n\nforeach(@num) \n{\n\t$s += $_; \n\t++$count; \n\tlast if ($s > $sum / 2);\n}\n\nprint $count;\n"}, {"source_code": "$n = <>;\n@a = sort {$b <=> $a} split(\" \", <>);\n$sum = 0;\nfor (@a) {\n $sum += $_;\n}\n$tmp = 0;\nfor (my $i = 0; $i < $n; ++$i) {\n $tmp += $a[$i];\n if ($tmp > $sum - $tmp) {\n print $i + 1;\n last;\n }\n}\n"}, {"source_code": "$n = <>;\n@a = sort {$b <=> $a} split ' ', <>;\nfor ($i = 0; $i < $n; ++$i) {\n $sum += $a[$i];\n}\nfor ($i = 0; $i < $n; ++$i) {\n $suma += $a[$i];\n if ($suma > $sum - $suma) {\n print $i + 1;\n exit;\n }\n}"}, {"source_code": "#!/bin/perl\n\n$n = ;\n\n$inpt = ;\nchomp $inpt;\n\n@nums = split / +/, $inpt;\n@nums = sort {$b <=> $a} @nums;\n\n$s = 0;\n\nfor($i=0;$i<@nums;$i++)\n{\n\n $s += @nums[$i];\n\n}\n\n$s /= 2;\n\n$s2 = 0;\n$it = 0;\n\nwhile( $s2 <= $s )\n{\n\n $s2 += @nums[$it];\n $it++;\n\n}\n\nprint $it;\n"}, {"source_code": "sub TakeInput\n{\n $n = ;\n $values = ;\n @values = split(\" \",$values);\n return @values;\n}\n\nsub sortArray\n{\n @Array = @_;\n $l = @Array;\n for($i=0;$i<$l;$i++)\n {\n for($j=0;$j<($l-$i);$j++)\n {\n if(@Array[$j+1]>@Array[$j])\n {\n\n $tmp = @Array[$j];\n @Array[$j] = @Array[$j+1];\n @Array[$j+1] = $tmp;\n }\n } \n }\n return @Array; \n}\nsub sum\n{\n my @Array = @_;\n my $l = @Array;\n $sm = 0;\n for($i=0;$i<$l;$i++)\n { \n \n $sm = $sm+@Array[$i];\n\n }\n return $sm;\n}\n\n@values = TakeInput();\n@sorted = sortArray(@values);\n$l = @sorted;\n$flag = 0;\nwhile($flag==0)\n{\n\n for($counter=0;$counter<$l-1;$counter++)\n { \n\n @list1 = @sorted[0..$counter];\n @list2 = @sorted[$counter+1..$l-1];\n $s1 = sum(@list1);\n $s2 = sum(@list2);\n if($s1>$s2)\n { \n $v = scalar @list1;\n print $v;\n $flag = 1;\n last;\n }\n\n }\n\n if($flag == 0)\n {\n print scalar @sorted;\n $flag = 1;\n }\n}\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = sort {$b <=> $a} split ' ', <>;\n\t$sum = eval join '+', @_;\n\t$mysum = 0;\n\t\n\t$i = 0;\n\tfor (@_){\n\t\t$i ++;\n\t\t$mysum += $_;\n\t\t$mysum * 2 > $sum and last\n\t\t}\n\t\n\tprint $i\n\t}"}, {"source_code": "my $num = <>;\nmy $coin = <>;\nchomp $coin;\nmy @coin = sort {$b<=>$a} split / /, $coin;\nmy $sum=0;\n$sum += $_ foreach @coin;\nmy $count;\nmy $val=0;\nuntil ($val > $sum/2) {\n $count++;\n $val += shift @coin;\n}\nprint $count;"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse 5.012;\nuse warnings;\n\nchomp(my $n = <>);\nchomp(my $s = <>);\n\n$s =~ /((.)\\1)/;\nsay $2;\n"}, {"source_code": "my $n = <>;\nmy @arr = reverse sort split \" \", <>;\nmy ($mid, $ans, $sum) = (0, 0, 0);\n$mid += $_ foreach(@arr);\n$mid = int($mid / 2);\n\nforeach(@arr) {\n $sum += $_;\n $ans++;\n last if ($sum > $mid);\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n# Codeforces Practice - 160A Twins\nuse warnings;\nuse strict;\n\nmy $num = ;\nchomp $num;\n\nmy @coins = split(\" \",);\nmy $sum = 0;\n$sum += $_ for @coins;\n\n@coins = sort {$b<=>$a} @coins;\n\nprint \"@coins\\n\";\n\nmy $sum2 = 0;\nfor(my $i = 1; @coins > 0; $i++){\n my $temp = shift @coins; \n $sum2 += $temp;\n $sum -= $temp;\n if($sum2>$sum){\n print $i;\n last;\n }\n}"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nmy $n = <>;\nmy @x = sort split ' ', <>;\nmy $s = 0;\n$s += $_ for @x;\nmy $t = 0;\n$t += pop @x while $t * 2 <= $s;\nprint $n - @x, \"\\n\";\n"}, {"source_code": "chomp($n = );\nchomp($l = );\n\n@coins = split /\\s+/, $l;\nsort { $b <=> $a } @coins;\n\n$total = 0;\nmap { $total += $_; } @coins;\n$taken_sum = 0;\n$taken = 0; \nwhile($taken_sum <= ($total - $taken_sum)) {\n\t$taken_sum += shift @coins;\n\t$taken++;\n}\n\nprint \"$taken\\n\";"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @a = split ' ', <>;\nmy $sum = 0;\nfor (@a) {\n $sum += $_;\n}\n\nmy $res = 0;\nmy $curr = 0;\n\nfor (@a) {\n last if (2*$curr > $sum);\n $res++;\n $curr += $_;\n}\n\nprint \"$res\\n\";\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @a = reverse sort split ' ', <>;\nmy $sum = 0;\nfor (@a) {\n $sum += $_;\n}\n\nmy $res = 0;\nmy $curr = 0;\n\nfor (@a) {\n last if (2*$curr > $sum);\n $res++;\n $curr += $_;\n}\n\nprint \"$res\\n\";\n"}, {"source_code": "$n=<>;\n@arr=split(' ',);\n@arr=sort(@arr);\n$som=0;\nfor($i=0;$i<$n;$i++){\n$som+=$arr[$i];\n}\n$ok=1;\n$i=$n-2;\n$profit=$arr[$n-1];\n$nb=1;\n\nwhile($ok && $i>=0){\nif($profit<=$som/2){\n$nb++;\n$profit+=$arr[$i];\n}\nelse{\n$ok=0;\n}\n$i--;\n}\nprint $nb;"}, {"source_code": "$n=<>;\n@arr=split(' ',);\n#@arr=sort(@arr);\n@arr = sort {$a <=> $b} @arr;\n$som=0;\nfor($i=0;$i<$n;$i++){\n$som+=$arr[$i];\n}\nprint \"@arr\\n\";\n$ok=1;\n$i=$n-2;\n$profit=$arr[$n-1];\n$nb=1;\n\nwhile($ok && $i>=0){\nif($profit<=$som/2){\n$nb++;\n$profit+=$arr[$i];\n}\nelse{\n$ok=0;\n}\n$i--;\n}\nprint $nb;"}, {"source_code": ";\n@num = reverse sort split(/ +/, );\n$s = $sum = $count = 0;\nforeach(@num) { $sum += $_ }\n\nforeach(@num) \n{\n $s += $_; \n ++$count; \n if ($s < $sum / 2) { last; }\n}\n\nprint STDOUT $count;"}, {"source_code": ";\n@num = reverse sort split(/ +/, );\n$s = $sum = $count = 0;\nforeach(@num) { $sum += $_ }\n\nforeach(@num) \n{\n $s += $_; \n ++$count; \n if ($s > $sum / 2) { last; }\n}\n\nprint STDOUT $count;"}, {"source_code": "$n = <>;\n@a = sort {$b <=> $a} split(\" \", <>);\n$sum = 0;\nfor (@a) {\n $sum += $_;\n}\n$tmp = 0;\nfor (my $i = 0; $i < $n; ++$i) {\n $tmp += $a[i];\n if ($tmp > $sum - $tmp) {\n print $i + 1;\n last;\n }\n}\n\n "}, {"source_code": "$n = <>;\n@a = sort {$b <=> $a} split '', <>;\nfor ($i = 0; $i < $n; ++$i) {\n $sum += $a[$i];\n}\nfor ($i = 0; $i < $n; ++$i) {\n $suma += $a[$i];\n if ($suma > $sum - $suma) {\n print $i + 1;\n exit;\n }\n}\n"}, {"source_code": "#!/bin/perl\n\n$n = ;\n\n@nums = split / +/, ;\n@nums = sort {$b cmp $a} @nums;\n\n$s = 0;\n\nfor($i=0;$i<@nums;$i++)\n{\n\n $s += @nums[$i];\n\n}\n\n$s /= 2;\n\n$s2 = 0;\n$it = 0;\n\nwhile( $s2 <= $s )\n{\n\n $s2 += @nums[$it];\n $it++;\n\n}\n\nprint $it;\n"}, {"source_code": "sub TakeInput\n{\n $n = ;\n $values = ;\n @values = split(\" \",$values);\n return @values;\n}\n\nsub sortArray\n{\n @Array = @_;\n $l = @Array;\n for($i=0;$i<$l;$i++)\n {\n for($j=0;$j<($l-$i);$j++)\n {\n if(@Array[$j+1]>@Array[$j])\n {\n\n $tmp = @Array[$j];\n @Array[$j] = @Array[$j+1];\n @Array[$j+1] = $tmp;\n }\n } \n }\n return @Array; \n}\nsub sum\n{\n @Array = @_;\n my $l = @Array;\n $sum = 0;\n for($i=0;$i<$l;$i++)\n {\n $sum = $sum+@Array[i];\n }\n return $sum;\n}\n@values = TakeInput();\n@sorted = sortArray(@values);\n$l = @sorted;\n$flag = 0;\nwhile($flag==0)\n{\n\n for($counter=0;$counter<$l-1;$counter++)\n { \n\n @list1 = @sorted[0..$counter];\n @list2 = @sorted[$counter+1..$l-1];\n $s1 = sum(@list1);\n $s2 = sum(@list2);\n if($s1>$s2)\n {\n print scalar @list1;\n $flag = 1;\n }\n\n }\n\n if($flag == 0)\n {\n print scalar @sorted;\n $flag = 1;\n }\n}\n"}, {"source_code": "sub TakeInput\n{\n $n = ;\n $values = ;\n @values = split(\" \",$values);\n return @values;\n}\n\nsub sortArray\n{\n @Array = @_;\n $l = @Array;\n for($i=0;$i<$l;$i++)\n {\n for($j=0;$j<($l-$i);$j++)\n {\n if(@Array[$j+1]>@Array[$j])\n {\n\n $tmp = @Array[$j];\n @Array[$j] = @Array[$j+1];\n @Array[$j+1] = $tmp;\n }\n } \n }\n return @Array; \n}\nsub sum\n{\n @Array = @_;\n my $l = @Array;\n $sum = 0;\n for($i=0;$i<$l;$i++)\n {\n $sum = $sum+@Array[i];\n }\n return $sum;\n}\n@values = TakeInput();\n@sorted = sortArray(@values);\n$l = @sorted;\n$flag = 0;\nwhile($flag==0)\n{\n\n for($counter=0;$counter<$l-1;$counter++)\n { \n\n @list1 = @sorted[0..$counter];\n @list2 = @sorted[$counter+1..$l-1];\n $s1 = sum(@list1);\n $s2 = sum(@list2);\n if($s1>$s2)\n {\n\n $v = scalar @list1;\n print $v + 1;\n $flag = 1;\n last;\n }\n\n }\n\n if($flag == 0)\n {\n print scalar @sorted;\n $flag = 1;\n }\n}\n"}, {"source_code": "my $num = <>;\nmy $coin = <>;\nchomp $coin;\nmy @coin = reverse sort split / /, $coin;\nmy $sum;\n$sum += $_ foreach @coin;\nmy $count;\nmy $val=0;\nuntil ($val > $sum/2) {\n $count++;\n $val += shift @coin;\n}\nprint $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,\u2009y1,\u2009x2,\u2009y2 (\u2009-\u2009100\u2009\u2264\u2009x1,\u2009y1,\u2009x2,\u2009y2\u2009\u2264\u2009100) 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,\u2009y3,\u2009x4,\u2009y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them. Note that x3,\u2009y3,\u2009x4,\u2009y4 must be in the range (\u2009-\u20091000\u2009\u2264\u2009x3,\u2009y3,\u2009x4,\u2009y4\u2009\u2264\u20091000).", "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": "($x,$y,$X,$Y)=split/ /,<>;\n$dx=abs($x-$X);\n$dy=abs($y-$Y);\nif (!$dx or !$dy){\nprint $x+$dy,\" \",$Y+$dx,\" \",$X+$dy,\" \",$y+$dx\n}\nelsif ($dx==$dy){\nprint \"$x $Y $X $y\"\n}\nelse {print -1};"}, {"source_code": "@_=split/ /,<>;\nchomp @_;\nif ($_[0]==$_[2]){\n$d=abs($_[1]-$_[3]);\nprint $_[0]+$d,\" \",$_[1],\" \",$_[0]+$d,\" \",$_[3];\nexit\n}\nif ($_[1]==$_[3]){\n$d=abs($_[0]-$_[2]);\nprint $_[0],\" \",$_[1]+$d,\" \",$_[2],\" \",$_[1]+$d;\nexit\n}\nif (abs($_[0]-$_[2])==abs($_[1]-$_[3])){\nprint \"$_[0] $_[3] $_[2] $_[1]\"\n}\nelse {print -1}"}, {"source_code": "($x,$y,$X,$Y)=split/ /,<>;\n$dx=abs($x-$X);\n$dy=abs($y-$Y);\nif (!$dx or !$dy){\nprint $x+$dy,\" \",$Y+$dx,\" \",$X+$dy,\" \",$y+$dx\n}\nelsif ($dx==$dy){\nprint \"$x $Y $X $y\"\n}\nelse {print -1}"}, {"source_code": "my ($x1, $y1, $x2, $y2) = split / /, <>;\nif ($x1 != $x2 && $y1 != $y2) {\n if (abs($x2 - $x1) != abs($y2 - $y1)) {\n print -1 . \"\\n\";\n }\n else {\n print \"$x2 $y1 $x1 $y2\";\n }\n}\nelse {\n if ($x1 == $x2) {\n print (($x1 + abs($y2 - $y1)) . \" \" . $y1 . \" \");\n print (($x2 + abs($y2 - $y1)) . \" \" . $y2); # \\n does not needed because we didn't chomp $y2\n }\n else {\n print $x1 . \" \" . ($y1 + abs($x2 - $x1)) . \" \";\n print $x2 . \" \" . ($y2 + abs($x2 - $x1)) . \"\\n\";\n }\n}\n"}, {"source_code": "#!/perl -w\n\nuse strict ;\n\nmy ($x1,$y1,$x2,$y2) = split /\\s+/ , ;\n\nif ( $x1 == $x2 && $y1 != $y2 ){\n my $distance = abs($y2-$y1) ;\n print $x1+$distance,\" \",$y1,\" \",$x2+$distance,\" \",$y2,\"\\n\" ;\n}elsif( $y1 == $y2 && $x1 != $x2 ){\n my $distance = abs($x2-$x1) ;\n print $x1,\" \",$y1+$distance,\" \",$x2,\" \",$y2+$distance,\"\\n\" ;\n}elsif( $x1 != $x2 && $y1 != $y2 && abs($x2-$x1)==abs($y2-$y1) ){\n print $x1,\" \",$y2,\" \",$x2,\" \",$y1,\"\\n\";\n}else{\n print \"-1\\n\" ;\n}"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nour ($x2, $y2, $x3, $y3) = (0, 0, 0, 0);\n\nsub judgex {\n\tmy $x = shift;\n\t($x>=-1000 && x<=1000) and return 1 or return 0;\n}\n\nsub judge {\n\tif (judgex($x2) && judgex($y2) && judgex($x3) && judgex($y3)) {\n\t\treturn 1;\n\t} else {\n\t\treturn 0;\n\t}\n} \n\n($x0, $y0, $x1, $y1) = split / /, <>;\nif ($x0 == $x1) {\n\t$d = abs($y0 - $y1);\n\t$x2 = $x3 = $x0 + $d;\n\t$y2 = $y0;\n\t$y3 = $y1;\n\tif (!judge()) {\n\t\t$x2 = $x3 = $x0 - d;\n\t}\n} elsif ($y0 == $y1) {\n\t$d = abs($x0 - $x1);\n\t$y2 = $y3 = $y0 + $d;\n\t$x2 = $x0;\n\t$x3 = $x1;\n\tif (!judge()) {\n\t\t$y2 = $y3 = $y0 - $d;\n\t}\n} else {\n\tif (abs($x0 - $x1) == abs($y0 - $y1)) {\n\t\t$x2 = $x0;\n\t\t$y2 = $y1;\n\t\t$x3 = $x1;\n\t\t$y3 = $y0;\n\t} else {\n\t\tsay -1 and exit;\n\t}\n}\n\njudge() and say \"$x2 $y2 $x3 $y3\" or say -1;\n\n\n"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nmy ($x1, $y1, $x2, $y2) = split ' ', <>;\n\nmy $d = abs($x1 - $x2) || abs($y1 - $y2);\n\nif ($y1 != $y2 && $d != abs($y1 - $y2)) {\n say -1;\n exit;\n}\n\nmy ($x3, $y3, $x4, $y4);\n\nif ($x1 == $x2) {\n ($x3, $y3, $x4, $y4) = ($x1 + $d, $y1, $x1 + $d, $y2);\n} elsif ($y1 == $y2) {\n ($x3, $y3, $x4, $y4) = ($x1, $y1 + $d, $x2, $y1 + $d);\n} else {\n ($x3, $y3, $x4, $y4) = ($x1, $y2, $x2, $y1);\n}\n\nsay \"$x3 $y3 $x4 $y4\";\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $l = <>;\nchomp $l;\n\n$l =~ /^(-?\\d+) (-?\\d+) (-?\\d+) (-?\\d+)/;\n\nmy ($x1, $y1, $x2, $y2) = ($1, $2, $3, $4);\n\nmy $size = abs($x1 - $x2);\nif (abs($y1 - $y2) > $size) {\n $size = abs($y1 - $y2);\n}\n\nmy $x_align = 0;\nif ($x1 == $x2) {\n $x_align = 1;\n}\n\nmy $y_align = 0;\nif ($y1 == $y2) {\n $y_align = 1;\n}\n\nif ($x_align && $y_align) {\n print \"-1\\n\";\n exit 0;\n}\n\nif (!$x_align && !$y_align) {\n if (abs($x1 - $x2) != abs($y1 - $y2)) {\n print \"-1\\n\";\n exit 0;\n }\n}\n\nif ($x_align) {\n my $x = $x1;\n if ($x+$size <= 1000) {\n print \"\".($x+$size).\" \".($y1).\" \".($x+$size).\" \".($y2).\"\\n\";\n } elsif ($x-$size >= -1000) {\n print \"\".($x-$size).\" \".($y1).\" \".($x-$size).\" \".($y2).\"\\n\";\n } else {\n print \"-1\\n\";\n }\n exit 0;\n}\n\nif ($y_align) {\n my $y = $y1;\n if ($y+$size <= 1000) {\n print \"\".($x1).\" \".($y+$size).\" \".($x2).\" \".($y+$size).\"\\n\";\n } elsif ($y-$size >= -1000) {\n print \"\".($x1).\" \".($y-$size).\" \".($x2).\" \".($y-$size).\"\\n\";\n } else {\n print \"-1\\n\";\n }\n exit 0;\n}\n\nif ($x1 < $x2) {\n if ($y1 < $y2) {\n if ($y1+$size <= 1000 && $y2-$size >= -1000) {\n print \"\".($x1).\" \".($y1+$size).\" \".($x2).\" \".($y2-$size).\"\\n\";\n } else {\n print \"-1\\n\";\n }\n } else {\n if ($y2+$size <= 1000 && $y1-$size >= -1000) {\n print \"\".($x1).\" \".($y1-$size).\" \".($x2).\" \".($y2+$size).\"\\n\";\n } else {\n print \"-1\\n\";\n }\n }\n} else {\n if ($y1 < $y2) {\n if ($y1+$size <= 1000 && $y2-$size >= -1000) {\n print \"\".($x1).\" \".($y1+$size).\" \".($x2).\" \".($y2-$size).\"\\n\";\n } else {\n print \"-1\\n\";\n }\n } else {\n if ($y2+$size <= 1000 && $y1-$size >= -1000) {\n print \"\".($x1).\" \".($y1-$size).\" \".($x2).\" \".($y2+$size).\"\\n\";\n } else {\n print \"-1\\n\";\n }\n }\n}\n\n"}, {"source_code": "@_=split/ /,<>;\n$d[$_]=abs($_[$_]-$_[$_-2]) for 0..1;\n\n\t$_[$_] += $d[($_+1) % 2] * ($d[0]-$d[1] ? 1: ++$ok*0),\n\t$d[$_ % 2] or $ok++\n\tfor 0..@_;\n\nprint $ok? \"@_[0,3,2,1]\" :-1"}], "negative_code": [{"source_code": "@_=split/ /,<>;\nchomp @_;\nif ($_[0]==$_[2]){\n$d=abs($_[1]-$_[3]);\nprint $_[0]+$d,\" \",$_[1],\" \",$_[0]+$d,\" \",$_[3];\nexit\n}\nif ($_[1]==$_[3]){\n$d=abs($_[0]-$_[2]);\nprint $_[0],\" \",$_[1]+$d,\" \",$_[2],\" \",$_[1]+$d;\nexit\n}\nif (abs($_[0]-$_[2])==abs($_[1]-$_[3])){\nprint \"$_[0] $_[3] $_[1] $_[2]\"\n}\nelse {print -1}"}, {"source_code": "@_=split/ /,<>;\nchomp @_;\nif ($_[0]==$_[2]){\n$d=abs($_[1]-$_[3]);\nprint $_[0]+$d,\" \",$_[1],\" \",$_[0]+$d,\" \",$_[1];\nexit\n}\nif ($_[1]==$_[3]){\n$d=abs($_[0]-$_[2]);\nprint $_[0],\" \",$_[1]+$d,\" \",$_[0],\" \",$_[1]+$d;\nexit\n}\nif (abs($_[0]-$_[2])==abs($_[1]-$_[3])){\nprint \"$_[0] $_[3] $_[1] $_[2]\"\n}\nelse {print -1}"}, {"source_code": "my ($x1, $y1, $x2, $y2) = split / /, <>;\nif ($x1 != $x2 && $y1 != $y2) {\n if (abs($x2 - $x1) != abs($y2 - $y1)) {\n print -1 . \"\\n\";\n }\n else {\n print \"$x2 $y1 $x1 $y2\";\n }\n}\nelse {\n if ($x1 == $x2) {\n print $x1 + abs($y2 - $y1) . \" \" . $y1 . \" \";\n print $x2 + abs($y2 - $y1) . \" \" . $y2; # \\n does not needed because we didn't chomp $y2\n }\n else {\n print $x1 . \" \" . $y1 + abs($x2 - $x1) . \" \";\n print $x2 . \" \" . $y2 + abs($x2 - $x1) . \"\\n\";\n }\n}\n"}, {"source_code": "my ($x1, $y1, $x2, $y2) = split / /, <>;\n\nif ($x1 != $x2 && $y1 != $y2) {\n if (abs($x2 - $x1) != abs($y2 - $y1)) {\n print -1 . \"\\n\";\n }\n else {\n print \"$x2 $y1 $x1 $y2\";\n }\n}\nelse {\n if ($x1 == $x2) {\n print abs($y2 - $y1) . \" \" . $y1 . \" \";\n print abs($y2 - $y1) . \" \" . $y2; # \\n does not needed because we didn't chomp $y2\n }\n else {\n print $x1 . \" \" . abs($x2 - $x1) . \" \";\n print $x2 . \" \" . abs($x2 - $x1) . \"\\n\";\n }\n}\n"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nmy ($x1, $y1, $x2, $y2) = split ' ', <>;\n\nmy $d = abs($x1 - $x2) || abs($y1 - $y2);\n\nif ($d != abs($y1 - $y2)) {\n say -1;\n exit;\n}\n\nmy ($x3, $y3, $x4, $y4);\n\nif ($x1 == $x2) {\n ($x3, $y3, $x4, $y4) = ($x1 + $d, $y1, $x1 + $d, $y2);\n} elsif ($y1 == $y2) {\n ($x3, $y3, $x4, $y4) = ($x1, $y1 + $d, $x2, $y1 + $d);\n} else {\n ($x3, $y3, $x4, $y4) = ($x1, $y2, $x2, $y1);\n}\n\nsay \"$x3 $y3 $x4 $y4\";\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $l = <>;\nchomp $l;\n\n$l =~ /^(\\d+) (\\d+) (\\d+) (\\d+)/;\n\nmy ($x1, $y1, $x2, $y2) = ($1, $2, $3, $4);\n\nmy $size = abs($x1 - $x2);\nif (abs($y1 - $y2) > $size) {\n $size = abs($y1 - $y2);\n}\n\nmy $x_align = 0;\nif ($x1 == $x2) {\n $x_align = 1;\n}\n\nmy $y_align = 0;\nif ($y1 == $y2) {\n $y_align = 1;\n}\n\nif ($x_align && $y_align) {\n print \"-1\\n\";\n exit 0;\n}\n\nif (!$x_align && !$y_align) {\n if (abs($x1 - $x2) != abs($y1 - $y2)) {\n print \"-1\\n\";\n exit 0;\n }\n}\n\nif ($x_align) {\n my $x = $x1;\n print \"\".($x+$size).\" \".($y1).\" \".($x+$size).\" \".($y2).\"\\n\";\n exit 0;\n}\n\nif ($y_align) {\n my $y = $y1;\n print \"\".($x1).\" \".($y+$size).\" \".($x2).\" \".($y+$size).\"\\n\";\n exit 0;\n}\n\nif ($x1 < $x2) {\n if ($y1 < $y2) {\n print \"\".($x1).\" \".($y1+$size).\" \".($x2).\" \".($y2-$size).\"\\n\";\n } else {\n print \"\".($x1).\" \".($y1-$size).\" \".($x2).\" \".($y2+$size).\"\\n\";\n }\n} else {\n if ($y1 < $y2) {\n print \"\".($x1).\" \".($y1+$size).\" \".($x2).\" \".($y2-$size).\"\\n\";\n } else {\n print \"\".($x1).\" \".($y1-$size).\" \".($x2).\" \".($y2+$size).\"\\n\";\n }\n}\n\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $l = <>;\nchomp $l;\n\n$l =~ /^(\\d+) (\\d+) (\\d+) (\\d+)/;\n\nmy ($x1, $y1, $x2, $y2) = ($1, $2, $3, $4);\n\nmy $size = abs($x1 - $x2);\nif (abs($y1 - $y2) > $size) {\n $size = abs($y1 - $y2);\n}\n\nmy $x_align = 0;\nif ($x1 == $x2) {\n $x_align = 1;\n}\n\nmy $y_align = 0;\nif ($y1 == $y2) {\n $y_align = 1;\n}\n\nif ($x_align && $y_align) {\n print \"-1\\n\";\n exit 0;\n}\n\nif (!$x_align && !$y_align) {\n if (abs($x1 - $x2) != abs($y1 - $y2)) {\n print \"-1\\n\";\n exit 0;\n }\n}\n\nif ($x_align) {\n my $x = $x1;\n if ($x+$size <= 1000) {\n print \"\".($x+$size).\" \".($y1).\" \".($x+$size).\" \".($y2).\"\\n\";\n } elsif ($x-$size >= -1000) {\n print \"\".($x-$size).\" \".($y1).\" \".($x-$size).\" \".($y2).\"\\n\";\n } else {\n print \"-1\\n\";\n }\n exit 0;\n}\n\nif ($y_align) {\n my $y = $y1;\n if ($y+$size <= 1000) {\n print \"\".($x1).\" \".($y+$size).\" \".($x2).\" \".($y+$size).\"\\n\";\n } elsif ($y-$size >= -1000) {\n print \"\".($x1).\" \".($y-$size).\" \".($x2).\" \".($y-$size).\"\\n\";\n } else {\n print \"-1\\n\";\n }\n exit 0;\n}\n\nif ($x1 < $x2) {\n if ($y1 < $y2) {\n if ($y1+$size <= 1000 && $y2-$size >= -1000) {\n print \"\".($x1).\" \".($y1+$size).\" \".($x2).\" \".($y2-$size).\"\\n\";\n } else {\n print \"-1\\n\";\n }\n } else {\n if ($y2+$size <= 1000 && $y1-$size >= -1000) {\n print \"\".($x1).\" \".($y1-$size).\" \".($x2).\" \".($y2+$size).\"\\n\";\n } else {\n print \"-1\\n\";\n }\n }\n} else {\n if ($y1 < $y2) {\n if ($y1+$size <= 1000 && $y2-$size >= -1000) {\n print \"\".($x1).\" \".($y1+$size).\" \".($x2).\" \".($y2-$size).\"\\n\";\n } else {\n print \"-1\\n\";\n }\n } else {\n if ($y2+$size <= 1000 && $y1-$size >= -1000) {\n print \"\".($x1).\" \".($y1-$size).\" \".($x2).\" \".($y2+$size).\"\\n\";\n } else {\n print \"-1\\n\";\n }\n }\n}\n\n"}], "src_uid": "71dea31e1244797f916adf5f526f776e"} {"nl": {"description": "You're given an array $$$b$$$ of length $$$n$$$. Let's define another array $$$a$$$, also of length $$$n$$$, for which $$$a_i = 2^{b_i}$$$ ($$$1 \\leq i \\leq n$$$). Valerii says that every two non-intersecting subarrays of $$$a$$$ have different sums of elements. You want to determine if he is wrong. More formally, you need to determine if there exist four integers $$$l_1,r_1,l_2,r_2$$$ that satisfy the following conditions: $$$1 \\leq l_1 \\leq r_1 \\lt l_2 \\leq r_2 \\leq n$$$; $$$a_{l_1}+a_{l_1+1}+\\ldots+a_{r_1-1}+a_{r_1} = a_{l_2}+a_{l_2+1}+\\ldots+a_{r_2-1}+a_{r_2}$$$. If such four integers exist, you will prove Valerii wrong. Do they exist?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 line of every test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 1000$$$). The second line of every test case contains $$$n$$$ integers $$$b_1,b_2,\\ldots,b_n$$$ ($$$0 \\le b_i \\le 10^9$$$). ", "output_spec": "For every test case, if there exist two non-intersecting subarrays in $$$a$$$ that have the same sum, output YES on a separate line. Otherwise, output NO on a separate line. Also, note that each letter can be in any case. ", "sample_inputs": ["2\n6\n4 3 0 1 2 0\n2\n2 5"], "sample_outputs": ["YES\nNO"], "notes": "NoteIn the first case, $$$a = [16,8,1,2,4,1]$$$. Choosing $$$l_1 = 1$$$, $$$r_1 = 1$$$, $$$l_2 = 2$$$ and $$$r_2 = 6$$$ works because $$$16 = (8+1+2+4+1)$$$.In the second case, you can verify that there is no way to select to such subarrays."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nwhile( $t -- > 0 ){\n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @b = map { $_ - 0 } split(/\\s+/,);\n \n my %b1 = ();\n my $ok1 = undef;\n for(my $i=0;$i<$n;$i++){\n $b1{$b[$i]}++;\n $ok1 = 1 if $b1{$b[$i]} >= 2;\n }\n if( $ok1 ){\n print \"YES\\n\";\n } else {\n print \"NO\\n\";\n }\n \n}\n\n\n"}], "negative_code": [], "src_uid": "3674a98de27b9bd2c8eb951da72996f5"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009106) \u2014 the number of cards containing number 0; m (1\u2009\u2264\u2009m\u2009\u2264\u2009106) \u2014 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)=split/ /,<>;\nif ($n>$m+1 or $m>$n*2+2){print -1; exit}\nif ($n==$m+1){print ('0'.('10'x $m)); exit}\n$_.=1 while $m-->$n*2;\nprint $_,'011'x(++$m-$n),'01'x(2*$n-$m)"}, {"source_code": "\nwhile(<>){\n\t\n\t($n,$m)=split/ /;\n\tchomp $m;\n\t$_='';\n\tif ($n>$m+1){print \"-1\\n\"; next}\n\tif ($m>$n*2+2){print \"-1\\n\"; next}\n\tif ($n==$m+1){print ('0'.('10'x --$n)); print \"\\n\"; next}\n\twhile ($m>$n*2){$m--; $_.=\"1\"}\n\tprint;\n\tprint ('011'x($m-$n),'01'x($n+$n-$m));\n\tprint \"\\n\"\n\t}\n\t\n"}], "negative_code": [{"source_code": "\nwhile(<>){\n\t\n\t($n,$m)=split/ /;\n\tchomp$m;\n\tprint \"$n $m \";\n\t$_='';\n\tif ($n>$m+1){print \"-1\\n\"; next}\n\tif ($m>$n*2+2){print \"-1\\n\"; next}\n\tif ($n==$m+1){print ('0'.('10'x --$n)); print \"\\n\"; next}\n\twhile ($m>$n*2){$m--; $_.=\"1\"}\n\tprint;\n\tprint ('011'x($m-$n),'01'x($n+$n-$m));\n\tprint \"\\n\"\n\t}\n\t\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\u2009\u2264\u2009n\u2009\u2264\u20091000)\u00a0\u2014 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": "$sum = \"\";\nforeach $s () {\n if ($s =~ /\\|/) {\n $sum = $sum . $s;\n }\n}\nif ($sum =~ /OO/) {\n print \"YES\\n\";\n $sum =~ s/OO/\\+\\+/;\n print $sum;\n} else {\n print \"NO\\n\";\n}"}, {"source_code": "use warnings;\n#use FileHandle;\n\n#my $in = FileHandle->new(\"input.txt\", \"r\");\n#my $out = FileHandle->new(\"output.txt\", \"w\");\n\nsub solve {\n\tmy $in = shift;\n\tmy $out = shift;\n\tmy $n = <$in>;\n\tmy ($str, $str1, $str2, $yes); \n\tmy @mas;\n\t$yes = 0;\n\n\tfor my $i (0..$n - 1){\n\t\t$str = <$in>;\n\t\t$str1 = substr($str, 0, 2);\n\t\tif (($yes == 0) and ($str1 eq 'OO')){\n\t\t\t$yes = 1;\n\t\t\t$str1 = '++';\t\t\n\t\t}\n\t\t$str2 = substr($str, 3, 2);\n\t\tif (($yes == 0) and ($str2 eq 'OO')){\n\t\t\t$yes = 1;\n\t\t\t$str2 = '++';\t\t\n\t\t}\n\n\t\tpush(@mas, $str1 . '|' . $str2 . \"\\n\"); \n\t}\n\n\tif ($yes == 1){\n\t\tprint $out \"YES\\n\";\n\t} else {\n\t\tprint $out \"NO\\n\";\n\t\treturn 1;\n\t}\n\n\tfor (@mas){\n\t\tprint $out $_;\n\t}\n\treturn 1;\n}\n\nsolve(STDIN, STDOUT);\n\n1;\n"}, {"source_code": "use 5.020;\nuse warnings;\n<>;\nundef $/;\n$_ = <>;\nif (s/OO/++/) {\n say 'YES';\n say;\n}\nelse { say 'NO' }\n"}, {"source_code": "my $n = <>;\nmy @pairs;\nfor (1..$n) {\n my $line = <>;\n chomp $line;\n push @pairs, [split /\\|/, $line];\n}\nmy $ok = 0;\n\nOUTER: for my $pair (@pairs) {\n for my $seats (@$pair) {\n if ($seats eq 'OO') {\n $seats = '++';\n $ok = 1;\n last OUTER;\n }\n }\n}\n\nif (!$ok) {\n print \"NO\\n\";\n exit;\n}\n\nprint \"YES\\n\";\n$, = \"\\n\";\n$\\ = \"\\n\";\nprint map { join \"|\", @$_ } @pairs;\n"}, {"source_code": "use strict;\nuse warnings;\n\nuse Data::Dumper;\n\nmy $in = <>;\nchomp $in;\n\nmy $n = $in;\ndie('invalid size') unless $n>=1 and $n<=1000;\nmy $bus = [];\nforeach my $row (0..$in-1) {\n $in = <>;\n chomp $in;\n push(@$bus, $in);\n}\n\nmy $can_sit_together = find_seats($bus);\nif ($can_sit_together) {\n print \"YES\\n\";\n print_bus($bus);\n} else {\n print \"NO\\n\";\n}\n\nsub find_seats {\n my $bus = shift;\n foreach my $bus_row (@$bus) {\n my @row = split(//, $bus_row);\n if ($row[0] eq 'O' and $row[1] eq 'O') {\n $row[0] = $row[1] = '+';\n $bus_row = join('', @row);\n return 1;\n } elsif ($row[3] eq 'O' and $row[4] eq 'O') {\n $row[3] = $row[4] = '+';\n $bus_row = join('', @row);\n return 1;\n }\n }\n return 0;\n}\n\nsub print_bus {\n my $bus = shift;\n foreach my $row (@$bus) {\n print \"$row\\n\";\n }\n}\n\n"}, {"source_code": "use 5.020;\nuse warnings;\n<>;\nundef $/;\n$_ = <>;\nif (s/OO/++/) {\n say 'YES';\n say;\n}\nelse { say 'NO' }\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\n# send argument as array, not reference, to function requiring list\n \n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\n# solve\n\nmy $n = read_token;\n\nmy $answer = 'NO';\n\nmy $lines = [];\n\nfor (1..$n) {\n my $line = read_line;\n my ($left, $right) = split q/\\|/, $line;\n $left = '++', $answer = 'YES' if $answer eq 'NO' and $left eq 'OO';\n $right = '++', $answer = 'YES' if $answer eq 'NO' and $right eq 'OO';\n push @$lines, join q{|}, ($left, $right);\n}\n\nsay $answer;\n\nexit(0) if $answer eq 'NO';\n\nfor (@$lines) {\n say $_;\n}\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nuse Carp;\n\n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\nsub ref_ref_scalar {\n my $ref = shift;\n return 1 if ref($ref) eq 'SCALAR';\n}\n\nsub toggle {\n my $ref = shift;\n croak '$ref does not reference to scalar' if !ref_ref_scalar($ref);\n\n $$ref = !$$ref;\n}\n\n# solve\n\n\nmy $n = read_token;\n\nmy $answer = 0;\n\nmy $lines = [];\n\nfor (1..$n) {\n my $line = read_line;\n my ($left, $right) = split q/\\|/, $line;\n $left = '++', toggle(\\$answer) if !$answer and $left eq 'OO';\n $right = '++', toggle(\\$answer) if !$answer and $right eq 'OO';\n push @$lines, join q{|}, ($left, $right);\n}\n\nsay 'NO' and exit(0) if !$answer;\n\nsay 'YES';\nfor (@$lines) {\n say $_;\n}\n"}, {"source_code": "<>;\n$/ = $\\; $_ = <>;\nprint s/OO/++/ ? \"YES\\n$_\" : \"NO\""}, {"source_code": "use strict;\n# cf711a # solution courtesy 20253198 rsFalse\n<>;\n$/='';\n$_ = <>;\nprint s/OO/++/ ? \"YES\\n$_\" : \"NO\";\n"}, {"source_code": "use strict;\n\n# cf711a\n=item\n\nA. Bus to Udayland\ntime limit per test\n2 seconds\nmemory limit per test\n256 megabytes\ninput\nstandard input\noutput\nstandard output\n\nZS 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.\n\nZS 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?\nInput\n\nThe first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of rows of seats in the bus.\n\nThen, 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.\n\nEach 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.\nOutput\n\nIf 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).\n\nIf there is no pair of seats for Chris and ZS, print \"NO\" (without quotes) in a single line.\n\nIf there are multiple solutions, you may print any of them.\nExamples\nInput\n\n6\nOO|OX\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX\n\nOutput\n\nYES\n++|OX\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX\n\nInput\n\n4\nXO|OX\nXO|XX\nOX|OX\nXX|OX\n\nOutput\n\nNO\n\nInput\n\n5\nXX|XX\nXX|XX\nXO|OX\nXO|OO\nOX|XO\nOutput\nYES\nXX|XX\nXX|XX\nXO|OX\nXO|++\nOX|XO\nNote\nNote that the following is an incorrect configuration for the first sample case because the seats must be in the same pair.\nO+|+X\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX\n=cut\n\nmy $n=<>;\nmy @ls=<>;\n\nmy @rs;\nmy $qFound=0;\nforeach my $l (@ls) {\n chomp $l;\n my @vs=split('',$l);\n if((!$qFound) && ($vs[0] eq 'O') && ($vs[1] eq 'O')) {\n $vs[0]=$vs[1]='+';\n $qFound=1;\n }\n if((!$qFound) && ($vs[3] eq 'O') && ($vs[4] eq 'O')) {\n $vs[3]=$vs[4]='+';\n $qFound=1;\n }\n push @rs,join('',@vs);\n}\nif($qFound) {\n print \"YES\\n\";\n foreach my $r (@rs) {\n print $r,\"\\n\";\n }\n} else {\n print \"NO\\n\";\n}\n"}], "negative_code": [{"source_code": "$sum = \"\";\nforeach $s () {\n if ($s =~ /\\|/) {\n $sum = $sum . $s;\n }\n}\n$sum =~ s/OO/\\+\\+/;\nprint $sum;\n"}, {"source_code": "use strict;\nuse warnings;\n\nuse Data::Dumper;\n\n# rows 10\n# per row: 2x2\n# find to seats next to each other...\n\n\nmy $n = <>;\nchomp($n);\n\ndie('wrong number or rows') unless $n >=0 && $n <= 100;\n\n# store input as array with length n * 2\n# loop array, find first seat with OO.\n# found: change 00 to ++\n# exit and print result\n\nmy @buss;\nmy @left;\nmy @right;\nfor (0..$n-1) {\n my $in = <>;\n chomp($in);\n my @seats = split('\\|', $in);\n# print Dumper \\@seats;\n push @left, $seats[0];\n push @right, $seats[1];\n}\n@buss = (@left, @right);\n\nfor (@buss) {\n if ($_ eq 'OO') {\n print \"YES\\n\";\n print_bus(\\@buss, $n);\n exit;\n }\n}\nprint \"NO\\n\";\n\n\nsub print_bus {\n my ($buss, $n) = @_;\n\n for (my $i=0; $i<$n; $i++) {\n my $row = $buss->[$i] . '|' . $buss->[$n + $i];\n print $row . \"\\n\";\n }\n}\n\n"}, {"source_code": "use 5.020;\nuse warnings;\nundef $/;\n$_ = <>;\nif (s/OO/++/) {\n say 'YES';\n say;\n}\nelse { say 'NO' }\n"}, {"source_code": "use strict;\n\n# cf711a\n=item\n\nA. Bus to Udayland\ntime limit per test\n2 seconds\nmemory limit per test\n256 megabytes\ninput\nstandard input\noutput\nstandard output\n\nZS 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.\n\nZS 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?\nInput\n\nThe first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of rows of seats in the bus.\n\nThen, 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.\n\nEach 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.\nOutput\n\nIf 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).\n\nIf there is no pair of seats for Chris and ZS, print \"NO\" (without quotes) in a single line.\n\nIf there are multiple solutions, you may print any of them.\nExamples\nInput\n\n6\nOO|OX\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX\n\nOutput\n\nYES\n++|OX\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX\n\nInput\n\n4\nXO|OX\nXO|XX\nOX|OX\nXX|OX\n\nOutput\n\nNO\n\nInput\n\n5\nXX|XX\nXX|XX\nXO|OX\nXO|OO\nOX|XO\nOutput\nYES\nXX|XX\nXX|XX\nXO|OX\nXO|++\nOX|XO\nNote\nNote that the following is an incorrect configuration for the first sample case because the seats must be in the same pair.\nO+|+X\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX\n=cut\n\nmy $n=<>;\nmy @ls=<>;\n\nmy @rs;\nmy $qFound=0;\nforeach my $l (@ls) {\n chomp $l;\n my @vs=split('',$l);\n if((!$qFound) && ($vs[0] eq 'O') && ($vs[1] eq 'O')) {\n $vs[0]=$vs[1]='+';\n $qFound=1;\n }\n if((!$qFound) && ($vs[3] eq 'O') && ($vs[4] eq 'O')) {\n $vs[3]=$vs[4]='+';\n $qFound=1;\n }\n push @rs,join('',@vs);\n}\nprint ($qFound ? \"YES\" : \"NO\");\nprint \"\\n\";\nforeach my $r (@rs) {\n print $r,\"\\n\";\n}"}, {"source_code": "use strict;\n\n# cf711a\n=item\n\nA. Bus to Udayland\ntime limit per test\n2 seconds\nmemory limit per test\n256 megabytes\ninput\nstandard input\noutput\nstandard output\n\nZS 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.\n\nZS 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?\nInput\n\nThe first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of rows of seats in the bus.\n\nThen, 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.\n\nEach 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.\nOutput\n\nIf 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).\n\nIf there is no pair of seats for Chris and ZS, print \"NO\" (without quotes) in a single line.\n\nIf there are multiple solutions, you may print any of them.\nExamples\nInput\n\n6\nOO|OX\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX\n\nOutput\n\nYES\n++|OX\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX\n\nInput\n\n4\nXO|OX\nXO|XX\nOX|OX\nXX|OX\n\nOutput\n\nNO\n\nInput\n\n5\nXX|XX\nXX|XX\nXO|OX\nXO|OO\nOX|XO\nOutput\nYES\nXX|XX\nXX|XX\nXO|OX\nXO|++\nOX|XO\nNote\nNote that the following is an incorrect configuration for the first sample case because the seats must be in the same pair.\nO+|+X\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX\n=cut\n\nmy $n=<>;\nmy @ls=<>;\n\nmy @rs;\nmy $qFound=0;\nforeach my $l (@ls) {\n chomp $l;\n my @vs=split('',$l);\n if((!$qFound) && ($vs[0] eq 'O') && ($vs[1] eq 'O')) {\n $vs[0]=$vs[1]='X';\n $qFound=1;\n }\n if((!$qFound) && ($vs[3] eq 'O') && ($vs[4] eq 'O')) {\n $vs[3]=$vs[4]='X';\n $qFound=1;\n }\n push @rs,join('',@vs);\n}\nprint ($qFound ? \"YES\" : \"NO\");\nprint \"\\n\";\nforeach my $r (@rs) {\n print $r,\"\\n\";\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\u2009\u2264\u2009n\u2009\u2264\u20093\u00b7105) \u2014 the number of points on the line. The second line contains n integers xi (\u2009-\u2009109\u2009\u2264\u2009xi\u2009\u2264\u2009109) \u2014 the coordinates of the given n points.", "output_spec": "Print the only integer x \u2014 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, @x) = split;\n\n@x = sort { $a<=>$b } @x;\n\nfor ($i=0; $i<$n; $i++) {\n\t$a[$i] = $s1 += $x[-1] - $x[$i];\n\t$b[$i] = $s2 += $x[$i] - $x[0];\n}\n\n$x=$x[0]; $min=$b[-1];\nfor ($i=1; $i<$n-1; $i++) {\n\t$s1 = $a[$i-1] - ($x[-1] - $x[$i]) * $i;\n\t$s2 = $b[-1] - $b[$i] - ($x[$i] - $x[0]) * ($n-$i-1);\n\t$s = $s1 + $s2;\n\tif ($s < $min) {\n\t\t$min = $s;\n\t\t$x = $x[$i];\n\t}\n}\n\nprint $x;\n"}, {"source_code": "$,=\" \";\nmy $n=<>;\nchomp($n);\n$_=<>;\nchomp;\nmy @A=sort {$a <=> $b } split / /;\nif($n%2==0)\n{\nprint $A[int($n/2)-1] , \"\\n\";\n}\nelse\n{\nprint $A[int($n/2)] , \"\\n\";\n}\n"}], "negative_code": [{"source_code": "$/ = \"\"; $_ = <>; ($n, @x) = split;\n$sum += $_ for @x; $med = $sum/$n;\n@y = sort {\n\tabs($a - $med) <=> abs($b - $med) || $a <=> $b\n} @x;\nprint $y[0]"}], "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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t@_ = 1 .. $n;\n\t\n\tfor( my $i = 1; $i < $n - 1; $i += 2 ){\n\t\t$k == 0 and last;\n\t\t$k --;\n\t\t( $_[ $i ], $_[ $i + 1 ] ) = ( $_[ $i + 1 ], $_[ $i ] )\n\t\t}\n\t\n\tif( $k > 0 ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\tprint \"@_\";\n\t}"}], "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 \u2014 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\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of contests where the coder participated. The next line contains n space-separated non-negative integer numbers \u2014 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 \u2014 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": "my $n = int<>;\nmy ($min, $max, $ans) = (10001, -1, 0);\n\nforeach(split / /, <>) {\n if ($_ < $min) {\n $ans++;\n $min = $_;\n }\n if ($_ > $max) {\n $ans++;\n $max = $_;\n }\n}\nprint $ans - 2 . \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\nuse warnings;\nuse strict;\n\nchomp(my $n = );\nmy @scores = split / /,;\n\nmy $min = $scores[0];\nmy $max = $scores[0];\nmy $count = 0;\nforeach my $score (@scores) {\n if($score > $max){\n $max = $score;\n $count++;\n } elsif($score <$min){\n $min = $score;\n $count++;\n }\n}\nprint $count;"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nchomp($n = <>);\n($mn, $mx) = (10x5, -10x5);\nforeach (split / /, <>) {\n\t($_<$mn || $_>$mx) and $ans++;\n\t$mn = $_<$mn ? $_:$mn;\n\t$mx = $_>$mx ? $_:$mx;\n}\nsay $ans-1;"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\n<>;\nmy @a = split ' ', <>;\nmy $max = shift @a;\nmy $min = $max;\nmy $amaze = 0;\n\nfor (@a) {\n if ($_ > $max) {\n $amaze++;\n $max = $_;\n } elsif ($_ < $min) {\n $amaze++;\n $min = $_;\n }\n}\n\nprint $amaze, \"\\n\";\n"}, {"source_code": "$n=<>;\n@array=split(' ',<>);\n$maxi=$array[0];\n$mini=$array[0];\n$score=0;\nfor($i=1;$i<$n;$i++){\nif($array[$i]>$maxi){\n$maxi=$array[$i];\n$score++;\n}\nelsif($array[$i]<$mini){\n$mini=$array[$i];\n$score++;\n}\n}\nprint $score;"}, {"source_code": "<>; $m = -1, $n = 9 ** 6;\n@a = split(' ', <>);\n\nforeach(@a){\n\t$s += ($_ > $m or $_ < $n) ? 1 : 0;\n\t$m = $_ > $m ? $_ : $m;\n\t$n = $_ < $n ? $_ : $n;}\nprint $s - 1;\n\n"}, {"source_code": "#!perl\n\n$n = <>;\n@arr = split /\\D/, <>;\n$result = 0;\n$min = $max = $arr[0];\nfor $i (1..$n-1) {\n\tif ($arr[$i] > $max) {\n\t\t$max = $arr[$i];\n\t\t$result++;\n\t} elsif ($arr[$i] < $min) {\n\t\t$min = $arr[$i];\n\t\t$result++;\n\t}\n}\nprint $result;\n\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nchomp (my $n = <>);\nchomp ($_ = <>);\n\nmy @arr = split / /;\nsub find($) { \n my $val = $arr[ $_[0] ];\n\n my ($c1, $c2) = (0,0);\n\n return 0 if (!$_[0]);\n\n for (0..$n-1)\n {\n last if ($_ eq $_[0]);\n\n $c1++ if ($arr[$_] > $val);\n $c2++ if ($arr[$_] < $val);\n }\n\n return (($c1 eq $_[0]) or ($c2 eq $_[0]));\n}\n\n$/ = 0;\nfor(0..$n-1)\n{\n $/++ if (find($_));\n}\n\nprint \"$/\\n\";"}, {"source_code": "$line = <>;\n$line = <>;\nchomp $line;\nmy $max, $min;\n$ans = 0;\nforeach my $num (split(/ /, $line)) {\n if (!defined($max)) {\n $max = $num;\n $min = $num;\n }\n if ($num > $max) {\n $ans++;\n $max = $num;\n }\n if ($num < $min) {\n $ans++;\n $min = $num;\n }\n}\nprint \"$ans\\n\";\n"}, {"source_code": "<>;\nmy @score = grep /\\w/, split ' ', <>;\nmy $count = 0;\nmy ($max, $min) = (0, 0);\nforeach (1..$#score) {\n if ($score[$_] > $score[$max]) {\n $count++;\n $max = $_;\n } elsif ($score[$_] < $score[$min]) {\n $count++;\n $min = $_;\n }\n}\nprint $count;"}], "negative_code": [{"source_code": "#!perl\n\n$n = <>;\n@arr = <>;\n$result = 0;\n$max = $min = $arr[0];\nfor $i (1..$n-1) {\n\tif ($arr[$i] > $max) {\n\t\t$max = $arr[$i];\n\t\t$result++;\n\t} elsif ($arr[$i] < $min) {\n\t\t$min = $arr[$i];\n\t\t$result++;\n\t}\n}\nprint $result;\n\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nchomp (my $n = <>);\nchomp ($_ = <>);\n\nmy @arr = split / /;\nsub find($) { \n my $val = $arr[ $_[0] ];\n\n my ($c1, $c2) = (0,0);\n\n return 0 if (!$_[0]);\n\n for (0..$n-1)\n {\n last if ($_ eq $_[0]);\n\n $c1++ if ($arr[$_] > $val);\n $c2++ if ($arr[$_] < $val);\n }\n\n return (($c1 eq $_[0]) or ($c2 eq $_[0]));\n}\n\n\nfor(0..$n-1)\n{\n $/++ if (find($_));\n}\n\nprint \"$/\\n\";"}, {"source_code": "<>;\nmy @score = grep /\\w/, split ' ', <>;\nmy $count;\nmy ($max, $min) = (0, 0);\nforeach (1..$#score) {\n if ($score[$_] > $score[$max]) {\n $count++;\n $max = $_;\n } elsif ($score[$_] < $score[$min]) {\n $count++;\n $min = $_;\n }\n}\nprint $count;"}], "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\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 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 \u2014 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\u2009\u2264\u2009ti\u2009\u2264\u2009n\u2009-\u20091) \u2014 the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n \u2014 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 \u2014 the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers \u2014 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": "use strict;\nuse warnings;\n\nmy ($n, $k) = map { 0 + $_ } split /\\s+/, <>;\nmy @main_courses = map { 0 + $_ } split /\\s+/, <>;\n\nmy @course_deps;\nfor (my $i = 0; $i < $n; $i++) {\n\tpush @course_deps, [map { 0 + $_ } split /\\s+/, <>];\n\tshift @{ $course_deps[$i] };\n}\n\nmy %checked_deps;\n\nmy @seq; eval { resolve_deps(\\@main_courses, \\@seq) };\nif ($@) {\n\tprint \"-1\\n\"\n} else {\n\tprint scalar @seq, \"\\n\";\n\tprint \"@seq\\n\";\n}\n\nsub resolve_deps\n{\n\tmy ($deps_ref, $seq_ref) = @_;\n\n\tfor (my $i = 0; $i < @{ $deps_ref }; $i++) {\n\t\tmy $d = $deps_ref->[$i];\n\n\t\tif (not $checked_deps{$d}) {\n\t\t\t$checked_deps{$d} = 'inprogress';\n\n\t\t\tresolve_deps($course_deps[$d - 1], $seq_ref);\n\t\t\tpush @{ $seq_ref }, $d;\n\n\t\t\t$checked_deps{$d} = 'resolved';\n\t\t}\n\n\t\tnext if $checked_deps{$d} eq 'resolved';\n\n\t\tdie \"cycle detected for course $d\\n\"\n\t\t\tif $checked_deps{$d} eq 'inprogress'\n\t}\n}\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\u2009\u2264\u2009i\u2009\u2264\u2009n, 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\u2009-\u20091 if i\u2009>\u20091 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i\u2009=\u2009n, 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\u2009\u2264\u2009n\u2009\u2264\u2009105) and p (1\u2009\u2264\u2009p\u2009\u2264\u2009n), 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": "\tuse integer;\n\twhile(<>){\n\t\tchomp;\n\t\tundef %h;\n\t\t($n,$p)=split/ /;\n\t\t$p--;\n\t\t$_=<>, chomp;\n\t\t$sz = @_ = split//;\n\t\t$ans = 0;\n\t\t@a = ();\n\t\t\n\t\tfor $i(0..@_/2-(@_ % 2 == 0)){\n\t\t\tif ($_[$i] ne $_[$sz - $i -1]){\n\t\t\t\t$a=$_[$i];\n\t\t\t\t$b=$_[$sz - $i -1];\n\t\t\t\t$a[$i]=1;\n\t\t\t\t$c = abs ((ord $a) - ord $b);\n\t\t\t\t26 - $c < $c and $c = 26 - $c;\n\t\t\t\t$ans += $c;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\tif (!@a){\n\t\t\tprint 0;\n\t\t\tnext;\n\t\t\t}\n\t\t$p2 = $sz - $p -1;\n\t\t$p2 < $p and $p = $p2;\n\t\t$j=0;\n\t\tfor $i(0..@_/2-(@_ % 2 == 0)){\n\t\t\tif ($a[$i]){$j or $st = $i; $j++; $fn = $i}\n\t\t\t}\n\t\t$diff = $fn-$st;\n\t\t$min = abs ($st - $p);\n\t\t$min > abs ($fn - $p) and \t$min = abs ($fn - $p);\n\t\t$p > $st and $p < $fn and $diff += $min;\n\t\t$p <= $st and $diff += $st - $p;\n\t\t$p >= $fn and $diff += $p - $fn;\n\t\tprint $ans += $diff ,$/\n\t\t}"}, {"source_code": "\tuse integer;\n\tlocal $SIG{__WARN__} = sub { $w = shift };\n\t\n\twhile(<>){\n\t\tchomp;\n\t\tundef %h;\n\t\t($n,$p)=split/ /;\n\t\t$p--;\n\t\t$_=<>, chomp;\n\t\t$sz = @_ = split//;\n\t\t$ans = 0;\n\t\t@a = ();\n\t\t\n\t\tfor $i(0..@_/2-(@_ % 2 == 0)){\n\t\t\tif ($_[$i] ne $_[$sz - $i -1]){\n\t\t\t\t$a=$_[$i];\n\t\t\t\t$b=$_[$sz - $i -1];\n\t\t\t\t$a[$i]=1;\n\t#\t\t\t$a[$sz - $i -1]=1;\n\t#\t\t\tprint \"$a $b \";\n\t\t\t\t$c = abs ((ord $a) - ord $b);\n\t\t\t\t26 - $c < $c and $c = 26 - $c;\n#\t\t\t\tprint \" \", $c, $/;\n\t\t\t\t$ans += $c;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\tif (!@a){\n\t\t\tprint 0;\n\t\t\tnext;\n\t\t\t}\n\t\t$p2 = $sz - $p -1;\n\t\t$p2 < $p and $p = $p2;\n#\t\tprint \"p $p\",$/;\n\t\t$j=0;\n\t\tfor $i(0..@_/2-(@_ % 2 == 0)){\n\t#\t\tprint $i,$/;\n\t\t\tif ($a[$i]){$j or $st = $i; $j++; $fn = $i}\n\t\t\t}\n#\t\tprint \"$st $fn\",$/;\n\t\t$diff = $fn-$st;\n#\t\tprint $diff,$/;\n\t\t$min = abs ($st - $p);\n#\t\tprint $p,$min;\n\t\t$min > abs ($fn - $p) and \t$min = abs ($fn - $p);\n#\t\tprint $min;\n\t\t$p > $st and $p < $fn and $diff += $min;\n\t\t$p <= $st and $diff += $st - $p;\n\t\t$p >= $fn and $diff += $p - $fn;\n#\t\tprint \"diff: $diff\",$/;\n#\t\tprint \"ans: $ans\",$/;\n#\t\tprint \"@a\",$/;\n#\t\tprint $_,\" \";\n\t\tprint $ans += $diff ,$/\n\t\t}"}], "negative_code": [], "src_uid": "ebacd748147b50b20d39b4d8cfde39ec"} {"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\u00a0\u2014 an integer from interval [2,\u2009100]. Your task is to say if the hidden number is prime or composite.Integer x\u2009>\u20091 is called prime if it has exactly two distinct divisors, 1 and x. If integer x\u2009>\u20091 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,\u2009100]. 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,\u2009100]. 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\u00a0\u2014 print an integer from interval [2,\u2009100] 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\u00a0\u2014 one integer from the interval [2,\u2009100]. 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,\u2009100] 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": "$|=1; ($p,@p)=(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47); # 15\nwhile ($p) {\n\tprint \"$p\\n\";\n\t$a=<>; chomp($a);\n\tif ($a eq \"yes\") {\n\t\tif (++$c==2) {\n\t\t\tprint \"composite\\n\";\n\t\t\texit;\n\t\t}\n\t\tif ($p<10) {\n\t\t\t$p*=$p;\n\t\t\tnext;\n\t\t}\n\t}\n\t$p=shift(@p);\n}\nprint \"prime\\n\";\n\n"}, {"source_code": "$| = 1; $\\ = $/;\n\nprint xor\n$e +=<>=~ /e/\nfor qw( 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 4 9 25 49 );\n\nprint 1 < $e ? 'composite' : 'prime'"}, {"source_code": "$\\ = $/;\n$| ++;\n\nfor my $i (qw( \n2\n3\n5\n7\n11\n13\n17\n19\n23\n29\n31\n37\n41\n43\n47\n4\n9\n25\n49\n) ) {\n\tprint $i;\n\tpush @_, scalar <>;\n\t}\n\nprint +(1 < grep /yes/, @_) ? 'composite' : 'prime'"}, {"source_code": "$\\ = $/;\n$|++;\n\nprint xor\n$e +=<>=~ /e/\nfor qw( \n2\n3\n5\n7\n11\n13\n17\n19\n23\n29\n31\n37\n41\n43\n47\n4\n9\n25\n49\n);\n\nprint 1 < $e ? 'composite' : 'prime'"}], "negative_code": [{"source_code": "$|=1; ($p,@p)=qw(2 3 5 7);\nwhile (@p) {\n\tprint \"$p\\n\";\n\t$a=<>; chomp($a);\n\tif ($a eq \"yes\") {\n\t\tif (++$c==2) {\n\t\t\tprint \"composite\\n\";\n\t\t\texit;\n\t\t}\n\t\t$p*=$p;\n\t} else {\n\t\t$p=shift(@p);\n\t}\n}\nprint \"prime\\n\";\n\n"}, {"source_code": "$|=1; ($p,@p)=(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47);\nwhile (@p) {\n\tprint \"$p\\n\";\n\t$a=<>; chomp($a);\n\tif ($a eq \"yes\") {\n\t\tif (++$c==2) {\n\t\t\tprint \"composite\\n\";\n\t\t\texit;\n\t\t}\n\t\t$p*=$p;\n\t} else {\n\t\t$p=shift(@p);\n\t}\n}\nprint \"prime\\n\";\n\n"}, {"source_code": "$\\ = $/;\n$| ++;\n\nfor my $i (qw( \n2\n3\n5\n7\n11\n13\n17\n19\n23\n29\n31\n37\n41\n43\n47\n) ) {\n\tprint $i;\n\tpush @_, scalar <>;\n\t}\n\nprint +(1 < grep /yes/, @_) ? 'composite' : 'prime'"}], "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\u2009\u00d7\u2009n 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\u2009\u2264\u2009n\u2009\u2264\u2009100). 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": "my $n = int<>;\nmy @arr = [split //, 0 x ($n+2)];\n($arr[$_]='0'.<>.'0') =~ tr/xo\\n/01/d for(1..$n);\n$arr[$_] = [split //,$arr[$_]] for(1..$n);\n# push @arr, [split //,0 x ($n+2)];\nmy $ans = \"YES\\n\";\nOuter:\nfor my $i(1..$n) {\n for my $j(1..$n) {\n if (($arr[$i - 1][$j] + $arr[$i + 1][$j] + $arr[$i][$j - 1] + $arr[$i][$j + 1]) % 2 != 0) {\n $ans = \"NO\\n\";\n last Outer;\n }\n }\n}\nprint $ans;\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n@dir = (\n\t[-1,0],\n\t[1,0],\n\t[0, -1],\n\t[0, 1],\n);\nchomp($n = <>);\npush @a, [split //, '*' x ($n+2)];\nforeach\t(1..$n) {\n\tchomp($line = <>);\n\tpush @a, [split //, '*' . $line . '*'];\n}\npush @a, [split //, '*' x ($n+2)];\nforeach $i (1..$n) {\n\tforeach $j (1..$n) {\n\t\t$c = 0;\n\t\t$c+=$a[$i+$dir[$_][0]][$j+$dir[$_][1]] eq 'o' ? 1:0 foreach(0..3);\n\t\t# say \"$i $j $c\";\n\t\t$c%2>0 and say \"NO\" and exit;\n\t}\n}\nsay \"YES\";"}], "negative_code": [], "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$$$) \u2014 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$$$) \u2014 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$$$ \u00a0\u2014 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": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t( @_, $t, $o ) = split ' ', <>;\r\n\t\r\n\tfor( @_ ){\r\n\t\t$_ /= 2, ++ $t until $_ % 2;\r\n\t\t}\r\n\t\r\n\tmy @i = 1 .. @_;\r\n\t\r\n\tfor( @i ){\r\n\t\t$c = 0;\r\n\t\t$_ /= 2, ++ $c until $_ % 2;\r\n\t\t$_ = $c;\r\n\t\t}\r\n\t\r\n\t@i = sort { $b <=> $a } grep $_, @i;\r\n\t\r\n\t$t += shift @i while $t < @_ and @i && ++ $o;\r\n\t\r\n\tprint $t >= @_ ? 0 + $o : -1\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $twos = 0;\n\t\n\tfor( @_ ){\n\t\t$_ /= 2, ++ $twos while $_ % 2 == 0;\n\t\t}\n\t\n\tmy @idxs = 1 .. @_;\n\t\n\tfor( @idxs ){\n\t\tmy $cnt = 0;\n\t\t$_ /= 2, ++ $cnt while $_ % 2 == 0;\n\t\t$_ = $cnt;\n\t\t}\n\t\n\t@idxs = sort { $b <=> $a } grep $_, @idxs;\n\t\n\tmy $ops = 0;\n\t\n\twhile( $twos < @_ and @idxs ){\n\t\t$twos += shift @idxs;\n\t\t$ops ++;\n\t\t}\n\t\n\tprint $twos >= @_ ? $ops : -1;\n\t}"}], "negative_code": [], "src_uid": "96f0df1c8e014229e5ef773789aa2205"} {"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\u2009-\u2009b| 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\u2009\u2264\u2009n,\u2009k\u2009\u2264\u20092000) \u2014 the number of people and the maximal capacity of the elevator. The next line contains n integers: f1,\u2009f2,\u2009...,\u2009fn (2\u2009\u2264\u2009fi\u2009\u2264\u20092000), where fi denotes the target floor of the i-th person.", "output_spec": "Output a single integer \u2014 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": "my ($n, $k) = split ' ', <>;\nmy @arr = sort { $a <=> $b } split (' ', <>);\nmy ($ans, $i) = (0,$n-1);\n\nwhile($i >= 0){\n $ans += 2*($arr[$i]-1);\n $i -= $k;\n}\nprint $ans;\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n($n, $k) = split / /, <>;\n@a = sort {$b <=> $a} split / /, <>;\nfor ($i=0; $i<$n; $i+=$k) {\n\t$ans += ($a[$i]-1) * 2;\n}\nsay $ans;"}, {"source_code": "use List::Util qw(first max maxstr min minstr reduce shuffle sum);\nchomp (my ($n, $k) = split /\\s+/, <>);\nchomp (my @targets = sort {$b <=> $a} split /\\s+/, <>);\n\nmy $cnt = 0;\nwhile (scalar @targets) {\n $cnt += ((&max(splice(@targets, 0, $k))) - 1) << 1;\n}\nprintf \"$cnt\\n\";"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy ($n, $k) = split ' ', <>;\nmy @arr = sort { $a <=> $b } split (' ', <>);\nmy ($res, $idx) = (0,$n-1);\n\nwhile($idx >= 0){\n $res += 2*($arr[$idx]-1);\n $idx -= $k;\n}\nprint $res;\n\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy ($n, $k) = split ' ', <>;\nmy @arr = sort { $b <=> $a } split (' ', <>);\nmy ($res, $idx) = (0,0);\n\nwhile($idx < $n){\n $res += 2*($arr[$idx]-1);\n $idx += $k;\n}\nprint $res;\n\n"}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$k)=split/ /;\n\t@_=sort {$a<=>$b} split/ /,<>;\n\t$ans=0;\n\twhile (@_){\n\t\t$e=pop @_;\n\t\tfor $i(1..$k-1){\n\t\t\tpop @_\n\t\t\t}\n\t\t$ans += $e*2-2\n\t\t}\n\tprint \"$ans\\n\"\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy ($n, $k) = split ' ', <>;\nmy @arr = sort { $b <=> $a } split (' ', <>);\nmy ($res, $idx) = (0,$n-1);\nwhile($idx >= 0){\n $res += 2*($arr[$idx]-1);\n $idx -= $k;\n}\nprint $res;\n\n"}], "src_uid": "b8d8f0e86ecb600f7559a6aec629946e"} {"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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print \"-\" x 20;\n\t\n\t@_ = split ' ', <>;\n\t$debug and print \"[@_]\";\n\t\n\tmy @A = ( 0, 0 );\n\t\n\tmy $i = 0;\n\tmap { $A[ $i ++ % 2 ] += $_ } @_;\n\t\n\t$debug and print \"A:[@A]\";\n\t\n\tmy @B = ( 0, 0 );\n\t\n\tmy $cnt = 0;\n\t\n\t$i = 0;\n\tfor( @_ ){\n\t\t$debug and print \"-\" x 10;\n\t\t$debug and print \"$_\";\n\t\t\n\t\t$A[ $i ] -= $_;\n\t\t$debug and print \"b:[@B]\";\n\t\t$debug and print \"a:[@A]\";\n\t\t\n\t\tif( $B[ 0 ] + $A[ 1 ] == $B[ 1 ] + $A[ 0 ] ){\n\t\t\t$cnt ++;\n\t\t\t$debug and print \"here:$_\"\n\t\t\t}\n\t\t\n\t\t$B[ $i ] += $_;\n\t\t\n\t\t$i ++;\n\t\t$i %= 2;\n\t\t}\n\t\n\t$debug and print \"cnt:\";\n\tprint $cnt;\n\t}"}], "negative_code": [], "src_uid": "dcc380c544225c8cadf0df8d8b6ffb4d"} {"nl": {"description": "You are given a set of $$$n$$$ ($$$n$$$ is always a power of $$$2$$$) elements containing all integers $$$0, 1, 2, \\ldots, n-1$$$ exactly once.Find $$$\\frac{n}{2}$$$ pairs of elements such that: Each element in the set is in exactly one pair. The sum over all pairs of the bitwise AND of its elements must be exactly equal to $$$k$$$. Formally, if $$$a_i$$$ and $$$b_i$$$ are the elements of the $$$i$$$-th pair, then the following must hold: $$$$$$\\sum_{i=1}^{n/2}{a_i \\& b_i} = k,$$$$$$ where $$$\\&$$$ denotes the bitwise AND operation. If there are many solutions, print any of them, if there is no solution, print $$$-1$$$ instead.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 400$$$) \u2014 the number of test cases. Description of the test cases follows. Each test case consists of a single line with two integers $$$n$$$ and $$$k$$$ ($$$4 \\leq n \\leq 2^{16}$$$, $$$n$$$ is a power of $$$2$$$, $$$0 \\leq k \\leq n-1$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2^{16}$$$. All test cases in each individual input will be pairwise different.", "output_spec": "For each test case, if there is no solution, print a single line with the integer $$$-1$$$. Otherwise, print $$$\\frac{n}{2}$$$ lines, the $$$i$$$-th of them must contain $$$a_i$$$ and $$$b_i$$$, the elements in the $$$i$$$-th pair. If there are many solutions, print any of them. Print the pairs and the elements in the pairs in any order.", "sample_inputs": ["4\n\n4 0\n\n4 1\n\n4 2\n\n4 3"], "sample_outputs": ["0 3\n1 2\n0 2\n1 3\n0 1\n2 3\n-1"], "notes": "NoteIn the first test, $$$(0\\&3)+(1\\&2) = 0$$$.In the second test, $$$(0\\&2)+(1\\&3) = 1$$$.In the third test, $$$(0\\&1)+(2\\&3) = 2$$$.In the fourth test, there is no solution."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\tmy( $n, $k ) = split;\n\t\n\t@_ = ();\n\t\n\tif( 4 == $n and $k >= 3 ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\tif( $k == 0 ){\n\t\tpush @_, [ $_, $n - $_ - 1 ] for 0 .. ( $n >> 1 ) - 1;\n\t\t}\n\telsif( $k == $n - 1 ){\n\t\tpush @_, [ 0, 1 ];\n\t\tpush @_, [ ( $n >> 1 ) - 1, $n - 1 ];\n\t\tpush @_, [ ( $n >> 1 ), $n - 2 ];\n\t\tfor my $i ( 1 .. ( $n >> 1 ) - 1 ){\n\t\t\tnext if $i == 0 or $i == 1 or $i == ( $n >> 1 ) - 1;\n\t\t\tpush @_, [ $i, $n - $i - 1];\n\t\t\t}\n\t\t}\n\telse{\n\t\tpush @_, [ 0, $n - $k - 1 ];\n\t\tpush @_, [ $k, $n - 1 ];\n\t\tfor my $i ( 1 .. ( $n >> 1 ) - 1 ){\n\t\t\tnext if $i == $k or $i == $n - $k - 1;\n\t\t\tpush @_, [ $i, $n - $i - 1 ];\n\t\t\t}\n\t\t}\n\t\n\t$debug and do {\n\t\tmy $sum = 0;\n\t\t$sum += eval join ' & ', @$_ for @_;\n\t\tprint \"sum:$sum\";\n\t\t};\n\t\n\tprint \"@$_\" for @_;\n\t}"}], "negative_code": [], "src_uid": "28d1c6a6effb1aea722164d5735377fc"} {"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\u2009=\u20095 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,\u2009a2,\u2009...,\u2009an, 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\u00a0\u2014 the sequence of integers b1,\u2009b2,\u2009...,\u2009bn (bi\u2009\u2265\u2009ai), 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\u2009\u2264\u2009n,\u2009k\u2009\u2264\u2009500)\u00a0\u2014 the number of days and the minimum number of walks with Cormen for any two consecutive days. The second line contains integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009500)\u00a0\u2014 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,\u2009b2,\u2009...,\u2009bn, where bi\u00a0\u2014 the total number of walks on the i-th day according to the found solutions (ai\u2009\u2264\u2009bi 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) = split(/ /, <>);\n@b = @a = split(/ /, <>);\n$ch = 0;\nfor($i=0;$i<$n-1;$i++)\n{\n\tif($b[$i] + $b[$i+1] < $k)\n\t{\n\t\t$b[$i+1] = $k - $b[$i];\n\t}\n}\nfor($i=0;$i<$n;$i++)\n{\n\t$ch += $b[$i] - $a[$i];\n}\nprint(\"$ch\\n@b\\n\");"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $k) = split;\n\t@_ = ($k, (split ' ', <>), $k);\n\t$sum = 0;\n\t\n\tfor $i (0 .. @_ - 2){\n\t\t$two = $_[$i] + $_[$i+1];\n\t\t$diff = $k - $two;\n\t\t$diff < 1 and next;\n\t\t$_[$i+1] += $diff;\n\t\t$sum += $diff;\n\t\t\n\t\t}\n\t\n\tprint $sum;\n\tprint \"@_[1 .. @_-2]\";\n\t}"}, {"source_code": "($k, $_) = (<> =~ /\\d+$/g, <>);\n\nNOP while s/\\b(\\d+) \\K(\\d+)\\b(??{ 'F' x ($1 + $2 >= $k) })/ \n\t\t\t$sum += $k - $1 - $2, $k - $1 \n\t\t/e;\n\nprint 0 + $sum . \"\\n$_\""}], "negative_code": [], "src_uid": "1956e31a9694b4fd7690f1a75028b9a1"} {"nl": {"description": "Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly n applications, each application has its own icon. The icons are located on different screens, one screen contains k icons. The icons from the first to the k-th one are located on the first screen, from the (k\u2009+\u20091)-th to the 2k-th ones are on the second screen and so on (the last screen may be partially empty).Initially the smartphone menu is showing the screen number 1. To launch the application with the icon located on the screen t, Anya needs to make the following gestures: first she scrolls to the required screen number t, by making t\u2009-\u20091 gestures (if the icon is on the screen t), and then make another gesture \u2014 press the icon of the required application exactly once to launch it.After the application is launched, the menu returns to the first screen. That is, to launch the next application you need to scroll through the menu again starting from the screen number 1.All applications are numbered from 1 to n. We know a certain order in which the icons of the applications are located in the menu at the beginning, but it changes as long as you use the operating system. Berdroid is intelligent system, so it changes the order of the icons by moving the more frequently used icons to the beginning of the list. Formally, right after an application is launched, Berdroid swaps the application icon and the icon of a preceding application (that is, the icon of an application on the position that is smaller by one in the order of menu). The preceding icon may possibly be located on the adjacent screen. The only exception is when the icon of the launched application already occupies the first place, in this case the icon arrangement doesn't change.Anya has planned the order in which she will launch applications. How many gestures should Anya make to launch the applications in the planned order? Note that one application may be launched multiple times.", "input_spec": "The first line of the input contains three numbers n,\u2009m,\u2009k (1\u2009\u2264\u2009n,\u2009m,\u2009k\u2009\u2264\u2009105)\u00a0\u2014\u00a0the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen. The next line contains n integers, permutation a1,\u2009a2,\u2009...,\u2009an\u00a0\u2014\u00a0the initial order of icons from left to right in the menu (from the first to the last one), ai\u00a0\u2014\u00a0 is the id of the application, whose icon goes i-th in the menu. Each integer from 1 to n occurs exactly once among ai. The third line contains m integers b1,\u2009b2,\u2009...,\u2009bm(1\u2009\u2264\u2009bi\u2009\u2264\u2009n)\u00a0\u2014\u00a0the ids of the launched applications in the planned order. One application may be launched multiple times.", "output_spec": "Print a single number \u2014 the number of gestures that Anya needs to make to launch all the applications in the desired order.", "sample_inputs": ["8 3 3\n1 2 3 4 5 6 7 8\n7 8 1", "5 4 2\n3 1 5 2 4\n4 4 4 4"], "sample_outputs": ["7", "8"], "notes": "NoteIn the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1,\u20092,\u20093, the second screen contains icons 4,\u20095,\u20096, the third screen contains icons 7,\u20098. After application 7 is launched, we get the new arrangement of the icons\u00a0\u2014\u00a0(123)(457)(68). To launch it Anya makes 3 gestures. After application 8 is launched, we get configuration (123)(457)(86). To launch it Anya makes 3 gestures. After application 1 is launched, the arrangement of icons in the menu doesn't change. To launch it Anya makes 1 gesture.In total, Anya makes 7 gestures."}, "positive_code": [{"source_code": "chomp (my ($n, $m, $k) = split /\\s+/, );\nchomp (my @arr = split /\\s+/, );\nchomp (my @b = split /\\s+/, );\n\nmy %pos;\n$pos{$arr[$_ - 1]} = $_ foreach (1 .. @arr);\n\nsub gotoScreen {\n return int ($_[0] / $k);\n}\n\nsub updatePos {\n my $num = $_[0];\n my $loc = $pos{$num};\n return if ($loc == 1);\n\n my ($b, $a) = ($arr[$loc - 1], $arr[$loc - 2]) = ($arr[$loc - 2], $arr[$loc - 1]);\n ($pos{$a}, $pos{$b}) = ($pos{$b}, $pos{$a});\n}\n\nmy $sum = 0;\nforeach (@b) {\n # print \"@arr\";\n # print \"$_ $pos{$_}\" foreach keys %pos;\n $sum += &gotoScreen($pos{$_} - 1);\n $sum++; # Open\n &updatePos($_);\n # print \"$sum\";\n}\n\nprintf \"$sum\\n\";\n"}], "negative_code": [], "src_uid": "3b0fb001333e53da458e1fb7ed760e32"} {"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 \u2014 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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t$_ = <>, chomp;\n\t\n\tmy $cG = () = /G/g;\n\t\n\ts/SS\\KS+//g;\n\ty/S/,/;\n\ts/G+/ ' ' . length $& /ge;\n\t1 while s/ (\\d+) (\\d+)/ ' ' . ( $1 + $2 ) /e;\n\ts/ //g;\n\t\n\t$debug and print;\n\t\n\tmy @cand = ( 0 );\n\t\n\t/\n\t\t\\b(\\d+)\n\t\t,\n\t\t(?=(\\d+)\\b)\n\t\t(?{ push @cand, 1 + $1 + $2 })\n\t\t(*F)\n\t/x;\n\t\n\tpush @cand, 1 + ( sort { $b <=> $a } split ',+' )[ 0 ];\n\t\n\t@cand = map { $_ > $cG ? $cG : $_ } @cand;\n\t\n\t$debug and print \"[@cand]\";\n\t\n\tprint +( sort { $b <=> $a } @cand )[ 0 ];\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t$_ = <>, chomp;\n\t\n\tmy $cG = () = /G/g;\n\t\n\ts/SS\\KS+//g;\n\ts/G+/ ' ' . length $& /ge;\n\t1 while s/ (\\d+) (\\d+)/ ' ' . ( $1 + $2 ) /e;\n\ts/ //g;\n\ty/S/,/;\n\t\n\t$debug and print;\n\t\n\tmy @cand = ( 0 );\n\t\n\t/\n\t\t\\b(\\d+)\n\t\t,\n\t\t(?=(\\d+)\\b)\n\t\t(?{ push @cand, 1 + $1 + $2 })\n\t\t(*F)\n\t/x;\n\t\n\tpush @cand, +( sort { $b <=> $a } split ',+' )[ 0 ];\n\t\n\t@cand = map { $_ > $cG ? $cG : $_ } @cand;\n\t\n\t$debug and print \"[@cand]\";\n\t\n\tprint +( sort { $b <=> $a } @cand )[ 0 ];\n\t}"}], "src_uid": "5d9d847103544fa07480fb85c75d0b97"} {"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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = map { [ split ' ', <> ] } 1 .. $_;\n\t\n\t$_[ 0 ][ 0 ] = $_[ 1 ][ 0 ] * $_[ 2 ][ 0 ] / $_[ 2 ][ 1 ];\n\t\n\t$debug and print sqrt $_[ 0 ][ 0 ];\n\t\n\tfor my $i ( 1 .. @_ - 1 ){\n\t\t$_[ $i ][ $i ] = $_[ $i - 1 ][ $i ] * $_[ $i ][ $i - 1 ] / $_[ $i - 1 ][ $i - 1 ];\n\t\t}\n\t\n\tprint join ' ', map { sqrt $_[ $_ ][ $_ ] } 0 .. @_ - 1;\n\t}"}], "negative_code": [], "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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the number of blocks in each stack from left to right.", "output_spec": "Print exactly one integer\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\t$debug and print '-' x 15;\n\t\n\t@_ = sort { $a <=> $b } split ' ', <>;\n\t\n\tmy $used = 0;\n\tmy $top = 0;\n\t\n\tfor( @_ ){\n\t\tif( $_ > $top ){\n\t\t\t$top ++;\n\t\t\t}\n\t\telse{\n\t\t\t;\n\t\t\t}\n\t\t\t\n\t\t$debug and print \" $_ $top\";\n\t\t}\n\t\n\t$used = $n + $_[ @_ - 1 ] - $top;\n\t\n\tmy $sum = 0;\n\t\n\t$sum += $_ for @_;\n\t\n\tprint $sum - $used;\n\t}"}], "negative_code": [], "src_uid": "ffa76f2558c8a70ab5c1ecb9e8561f25"} {"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\u00a0\u2014 those with biggest ti.Your task is to handle queries of two types: \"1 id\"\u00a0\u2014 Friend id becomes online. It's guaranteed that he wasn't online before. \"2 id\"\u00a0\u2014 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\u2009\u2264\u2009n,\u2009q\u2009\u2264\u2009150\u2009000,\u20091\u2009\u2264\u2009k\u2009\u2264\u2009min(6,\u2009n))\u00a0\u2014 the number of friends, the maximum number of displayed online friends and the number of queries, respectively. The second line contains n integers t1,\u2009t2,\u2009...,\u2009tn (1\u2009\u2264\u2009ti\u2009\u2264\u2009109) 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\u2009\u2264\u2009typei\u2009\u2264\u20092,\u20091\u2009\u2264\u2009idi\u2009\u2264\u2009n)\u00a0\u2014 the i-th query. If typei\u2009=\u20091 then a friend idi becomes online. If typei\u2009=\u20092 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\u2009=\u20092) so the output won't be empty.", "output_spec": "For each query of the second type print one line with the answer\u00a0\u2014 \"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\"\u00a0\u2014 Friend 3 becomes online. \"2 4\"\u00a0\u2014 We should check if friend 4 is displayed. He isn't even online and thus we print \"NO\". \"2 3\"\u00a0\u2014 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\"\u00a0\u2014 Friend 1 becomes online. The system now displays both friend 1 and friend 3. \"1 2\"\u00a0\u2014 Friend 2 becomes online. There are 3 friends online now but we were given k\u2009=\u20092 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1\u2009<\u2009t2,\u2009t3) so friend 1 won't be displayed \"2 1\"\u00a0\u2014 Print \"NO\". \"2 2\"\u00a0\u2014 Print \"YES\". \"2 3\"\u00a0\u2014 Print \"YES\". "}, "positive_code": [{"source_code": "($n, $k, $q, @t) = split ' ', <>.<>;\n\nprint join \"\\n\", grep !/-/, map {\n\t($_, $id) = split;\n\tif (/2/){ ! $h[$id] || $m6[$k-1] > $t[$id-1] ? 'NO' : 'YES' }\n\telse {\n\t\t$h[$id] -= @m6 = (sort {$b <=> $a} @m6, $t[$id-1])[0 .. $k-1]\n\t\t}\n\t} map ~~<>, 1 .. $q"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $k, $q) = split;\n\t@t = split ' ', <>;\n\t@max6 = ();\n\t%h = ();\n\tfor (map ~~<>, 1 .. $q){\n\t\t($_, $id) = split;\n\t\tif (/2/){ print ! exists $h{$id} || $max6[$k-1] > $t[$id-1] ? 'NO' : 'YES' }\n\t\telse {\n\t\t\t@max6 = map {0 + $_} (sort {$b <=> $a} @max6, $t[$id-1])[0 .. $k-1];\n\t\t\tundef $h{$id};\n\t\t\t}\n\t\t}\n\t}"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009105).", "output_spec": "Print a single integer \u2014 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": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Tue Nov 27 15:45:13 IST 2012\n# File Name: b.pl\n# USAGE: \n# b.pl \n# \n# \n#------------------------------------------------\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nchomp (my $n = );\n$n--;\n\nif ($n < 2) {\n print '-1'\n} elsif ($n == 2) {\n print '210';\n} else {\n my $sign = 1;\n $sign = -1 if ( ($n / 3) % 2);\n\n my $strt = 10 ** ($n % 3);\n #print $strt, ' ' . length $strt;\n #print $n - 3 - length $strt;\n\n foreach my $x (0 .. 9) {\n foreach my $y (0 .. 9) {\n if ( ((100 * $x + 10 * $y + $sign * $strt) % 7 == 0) && ($x + $y + 1) % 3 == 0 ){\n print $strt . '0'x&max(0, ($n - 2 - length $strt)) . $x . $y . '0';\n goto END;\n }\n }\n }\n}\nEND:\n"}], "negative_code": [{"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Tue Nov 27 15:45:13 IST 2012\n# File Name: b.pl\n# USAGE: \n# b.pl \n# \n# \n#------------------------------------------------\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nchomp (my $n = );\n$n--;\n\nif ($n < 2) {\n print '-1'\n} elsif ($n == 2) {\n print '210';\n} else {\n my $sign = 1;\n $sign = -1 if ( ($n / 3) % 2);\n\n my $strt = 10 ** ($n % 3);\n\n foreach my $x (0 .. 9) {\n foreach my $y (0 .. 9) {\n if ( ((100 * $x + 10 * $y + $sign * $strt) % 7 == 0) && ($x + $y + 1) % 3 == 0 ){\n print $strt . '0'x&min(0, ($n - length $strt - 3)) . $x . $y . '0';\n goto END;\n }\n }\n }\n}\nEND:\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,\u20091]. 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\u2009-\u2009d,\u2009k\u2009+\u2009d] 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\u2009-\u2009d,\u2009k]. If she escapes to the right, her new interval will be [k,\u2009k\u2009+\u2009d].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\u2009\u2264\u2009|s|\u2009\u2264\u2009106). Each character in s will be either \"l\" or \"r\".", "output_spec": "Output n lines \u2014 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": "chomp ($s = );\n@s = split /\\s*/, $s;\n(@l, @r) = ();\n$i = 1;\nforeach (@s) {\n\tif (/^l$/i) {unshift @r, $i} else {push @l, $i}\n\t$i++;\n}\nprintf \"%s\\n\", join \"\\n\", @l, @r;\n"}], "negative_code": [], "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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200910000). 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\u00b7106 (in particular, this means that the length of some line does not exceed 5\u00b7106 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) \u2014 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": "#!/usr/bin/perl -w\n\nuse strict;\n\nmy @d = (0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);\n\nsub gao {\n $_ = shift;\n my ($month, $day, $hour, $minute, $second) =\n /^\\d+-(\\d+)-(\\d+) (\\d+):(\\d+):(\\d+)/;\n my ($f) = /^([\\d-]* [\\d:]*):/;\n my $time = $day - 1;\n $time += $d[$_] for (1 .. $month - 1);\n $time = $time * 24 + $hour;\n $time = $time * 60 + $minute;\n $time = $time * 60 + $second;\n return ($time, $f);\n}\n\nchomp($_ = <>);\nmy ($n, $m) = split;\nmy @a = ();\nmy @b = ();\nfor (<>) {\n my ($a, $b) = gao($_);\n push @a, $a;\n push @b, $b;\n}\nfor (my ($i, $j) = (0, 0); $j < @a; ++$j) {\n for (; $i < $j && $a[$j] - $a[$i] >= $n; ++$i) {\n 1;\n }\n if ($j - $i + 1 >= $m) {\n print $b[$j], \"\\n\";\n exit 0;\n }\n}\nprint \"-1\\n\";\n"}, {"source_code": "use POSIX qw(mktime);\n\nmy $line = ;\nchomp $line;\n\n$line =~ /^(\\d+)\\s+(\\d+)/;\nmy ($n, $m) = ($1, $2);\n\nmy @on;\nmy $cur = 0;\nmy $curts;\n\nuse Data::Dumper;\n\nwhile () {\n chomp;\n /^(2012)-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2}):/;\n $curts = mktime($6, $5, $4, $3, $2-1, $1-1900);\n push(@on, $curts+$n);\n $cur++;\n while (scalar @on > 0 && $on[0] <= $curts) {\n $cur--;\n shift @on;\n }\n\n if ($cur >= $m) {\n print \"$1-$2-$3 $4:$5:$6\\n\";\n exit;\n }\n}\n\nprint \"-1\\n\";\n\n"}], "negative_code": [], "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$$$\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nwhile($t-->0){\n my ($n,$m) = map { $_ - 0 } split(/\\s+/,);\n my @A = map { $_ - 0 } split(/\\s+/,);\n \n my $s = 0;\n for(my $i=0;$i<$n;$i++){\n $s += $A[$i];\n }\n if( $s == $m ){\n print \"YES\\n\";\n } else {\n print \"NO\\n\";\n }\n \n}\n\n\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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000, 1\u2009\u2264\u2009m\u2009\u2264\u200910\u2009000, n\u2009\u2265\u20092m)\u00a0\u2014 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\u00a0\u2014 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": "$\\ = $/; <>;\n\nfor (<>){\n\t($N, $R, $P) = split;\n\t$h[$R]{$N} = $P;\n\t}\n\t\nshift @h;\nfor (@h){\n\t@V = sort {$_->{$b} <=> $_->{$a}} keys %{ $_ };\n\tprint $_->{ $V[1] } eq $_->{ $V[2] } ? \n\t\t\t'?'\n\t\t:\n\t\t\tjoin ' ', @V[0 .. 1]\n\t}\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m) = split;\n\tfor (1 .. $n){\n\t\t($name, $region, $pts) = split ' ', <>;\n\t\t$h{$region}{$name} = $pts;\n\t\t}\n\tfor $i (sort {$a <=> $b} keys %h){\n\t\t@values = sort {$b <=> $a} values %{ $h{$i} };\n\t\tprint do { \n\t\t\t\tif ($values[1] eq $values[2]){\n\t\t\t\t\t'?'\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\t@keys = sort {$h{$i}{$b} <=> $h{$i}{$a}} keys %{ $h{$i} };\n\t\t\t\t\tjoin ' ', @keys[0 .. 1];\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\t}"}, {"source_code": "$\\ = $/; <>;\n\nfor (<>){\n\t($N, $R, $P) = split;\n\t$h[$R]{$N} = $P;\n\t}\n\t\nshift @h;\nfor (@h){\n\t@V = sort {$_->{$b} <=> $_->{$a}} keys %{ $_ };\n\tprint $_->{ $V[1] } eq $_->{ $V[2] } ? \n\t\t\t'?'\n\t\t:\n\t\t\tjoin ' ', @V[0 .. 1]\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m) = split;\n\tfor (1 .. $n){\n\t\t($name, $region, $pts) = split ' ', <>;\n\t\t$h{$region}{$name} = $pts;\n\t\t}\n\tfor $i (keys %h){\n\t\t@values = sort {$b <=> $a} values %{ $h{$i} };\n\t\tprint do { \n\t\t\t\tif (@values < 2 || $values[1] == $values[2]){\n\t\t\t\t\t'?'\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\t@keys = sort {$h{$i}{$b} <=> $h{$i}{$a}} keys %{ $h{$i} };\n\t\t\t\t\tjoin ' ', @keys[0 .. 1];\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $m) = split;\n\tfor (1 .. $n){\n\t\t($name, $region, $pts) = split ' ', <>;\n\t\t$h{$region}{$name} = $pts;\n\t\t}\n\tfor $i (sort {$a <=> $b} keys %h){\n\t\t@values = sort {$b <=> $a} values %{ $h{$i} };\n\t\tprint do { \n\t\t\t\tif (@values < 2 || $values[1] == $values[2]){\n\t\t\t\t\t'?'\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\t@keys = sort {$h{$i}{$b} <=> $h{$i}{$a}} keys %{ $h{$i} };\n\t\t\t\t\tjoin ' ', @keys[0 .. 1];\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\t}"}], "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$$$) \u2014 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$$$) \u2014 the number of elements in the permutation. The second line of the test case contains $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ \u2014 the given permutation.", "output_spec": "For each test case, print the answer on it \u2014 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": "#!/usr/bin/env perl \n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\nmy @tokens = ();\nmy $q = read_token();\n\nfor (1..$q) {\n my $n = read_token();\n my @chances = (1)x($n+1);\n my @orders;\n for ( 1..$n ) {\n push @orders, $_;\n }\n my $line = read_line();\n my @arr = (0);\n push @arr, split q{ }, $line; \n\n while ( @orders ) {\n my $order = shift @orders;\n my $i = find_idex(\\@arr, $order, $n);\n $i--;\n\n while ( $i > 0 ) { \n if ( $chances[$i] && $arr[$i] > $arr[$i+1] ) {\n @arr[$i,$i+1] = @arr[$i+1,$i];\n $chances[$i] = 0;\n $i--;\n }\n else { last; }\n }\n }\n shift @arr;\n say \"@arr\";\n\n}\n\nsub find_idex { \n my ($arr, $value, $n) = @_;\n my $i = 0;\n for ( 1..$n ) {\n if ( $$arr[$_] == $value ) {\n $i = $_;\n last;\n }\n } \n return $i;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n} \n\nsub read_token { \n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}"}], "negative_code": [], "src_uid": "c7a7e90bc54b2d21b3f59a358d9d1410"} {"nl": {"description": "You are given a 4x4 grid. You play a game\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tchomp;\n\t\n\t@_ = ();\n\t\n\tmy( $_0, $_1 ) = ( 0, 0 );\n\t\n\tfor( split // ){\n\t\tpush @_, $_ ? \"1 \" . ( $_1 ++ % 2 * 2 + 1 ) : \"2 \" . ( $_0 ++ % 4 + 1 );\n\t\t}\n\t\n\tprint for @_;\n\t}"}], "negative_code": [], "src_uid": "a63cdbd1009a60c0f9b52e4ffbba252e"} {"nl": {"description": "Thanos sort is a supervillain sorting algorithm, which works as follows: if the array is not sorted, snap your fingers* to remove the first or the second half of the items, and repeat the process.Given an input array, what is the size of the longest sorted array you can obtain from it using Thanos sort?*Infinity Gauntlet required.", "input_spec": "The first line of input contains a single number $$$n$$$ ($$$1 \\le n \\le 16$$$) \u2014 the size of the array. $$$n$$$ is guaranteed to be a power of 2. The second line of input contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$1 \\le a_i \\le 100$$$) \u2014 the elements of the array.", "output_spec": "Return the maximal length of a sorted array you can obtain using Thanos sort. The elements of the array have to be sorted in non-decreasing order.", "sample_inputs": ["4\n1 2 2 4", "8\n11 12 1 2 13 14 3 4", "4\n7 6 5 4"], "sample_outputs": ["4", "2", "1"], "notes": "NoteIn the first example the array is already sorted, so no finger snaps are required.In the second example the array actually has a subarray of 4 sorted elements, but you can not remove elements from different sides of the array in one finger snap. Each time you have to remove either the whole first half or the whole second half, so you'll have to snap your fingers twice to get to a 2-element sorted array.In the third example the array is sorted in decreasing order, so you can only save one element from the ultimate destruction."}, "positive_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n $_ = <>, chomp;\n \n my @cand;\n my @s;\n \n for my $len ( map 2 ** $_, 0 .. 4 ){\n while( /(?:[ ]?\\b\\d+){$len}/g ){\n my $match = $&;\n @_ = split ' ', $match;\n @s = sort { $a <=> $b } @_;\n @_ ~~ @s and push @cand, $len;\n }\n }\n \n print +( sort { $b <=> $a } @cand )[ 0 ];\n }"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n $_ = <>, chomp;\n \n my @cand;\n my @s;\n \n for my $len ( map 2 ** $_, 0 .. 4 ){\n while( /(?:[ ]?\\d+){$len}/g ){\n my $match = $&;\n @_ = split ' ', $match;\n @s = sort { $a <=> $b } @_;\n @_ ~~ @s and push @cand, $len;\n }\n }\n \n print +( sort { $b <=> $a } @cand )[ 0 ];\n }"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n $_ = <>, chomp;\n \n my @cand;\n my @s;\n \n for my $len ( map 2 ** $_, 0 .. 3 ){\n while( /(?:[ ]?\\d+){$len}/g ){\n my $match = $&;\n @_ = split ' ', $match;\n @s = sort { $a <=> $b } @_;\n @_ ~~ @s and push @cand, $len;\n }\n }\n \n print +( sort { $b <=> $a } @cand )[ 0 ];\n }"}], "src_uid": "e5c68be38968fbc9677f3c1adddaff58"} {"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\u00a0\u2014\u00a0each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s|\u2009-\u2009ai\u2009+\u20091. It is guaranteed that 2\u00b7ai\u2009\u2264\u2009|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\u00b7105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1\u2009\u2264\u2009m\u2009\u2264\u2009105)\u00a0\u2014\u00a0 the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1\u2009\u2264\u2009ai; 2\u00b7ai\u2009\u2264\u2009|s|)\u00a0\u2014\u00a0the 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": "$\\ = $/;\nwhile(<>){\n\tchomp; <>;\n\t@a = sort {$a <=> $b} split \" \", <>;\n\t@A = ();\n\t$a[ $_ ] == $a[ $_+1 ] and $a[ $_ ] = $a[ $_+1 ] = -1 for 0 .. @a - 2;\n\t$_ > -1 and push @A, $_ for @a;\n\t@a = @A;\n\t@b = (0) x ((length) / 2);\n\t$j = 0;\n\tfor $i (0 .. @b-1){\t\n\t\t$i == $a[ 0 ]-1 and do {\n\t\t\t++$j;\n\t\t\tshift @a if @a;\n\t\t\t};\n\t\t$b[ $i ] = $j % 2;\n\t\t}\n\tfor $i (0 .. @b-1){\n\t\t$b[ $i ] and do {\n\t\t\t$r = substr $_, $i, 1;\n\t\t\tsubstr $_, $i, 1, (substr $_, (length)-$i-1, 1);\n\t\t\tsubstr $_, (length)-$i-1, 1, $r;\n\t\t\t}\n\t\t}\n\tprint\n\t}"}, {"source_code": "$\\ = $/;\nwhile(<>){\n\tchomp;\n\t<>;\n\t@a = split \" \", <>;\n\t@a = sort {$a <=> $b} @a;\n\t$a = \"@a\";\n#\t$a =~ s/(\\b\\d+\\b) \\1//g;\n#\t@a = split \" \", $a;\n\t@A = ();\n\tfor $i (0 .. @a-2){\n\t\t$a[ $i ] == $a[ $i+1 ] and do {$a[ $i ] = $a[ $i+1 ] = -1} ;\n\t\t}\n\t$_ > -1 and push @A, $_ for @a;\n\t@a = @A;\n\t@b = (0) x ((length) / 2);\n#\tprint \"@b\";\n#\tprint \"@a\";\n\t$j = 0;\n\tfor $i (0 .. @b-1){\n#\t\tprint \"($i == \".($a[0]-1).\")\";\n\t\t\n\t\t$i == $a[ 0 ]-1 and do {\n\t\t\t++$j;\n\t\t\tshift @a if @a;\n\t\t\t};\n#\t\t\tprint \"<$j>\";\n\t\t$b[ $i ] = $j % 2;\n#\t\t@a || last;\n\t\t}\n#\tprint \"[@b]\";\n#\tprint;\n\tfor $i (0 .. @b-1){\n\t\t$b[ $i ] and do {\n\t\t\t$r = substr $_, $i, 1;\n\t\t\tsubstr $_, $i, 1, (substr $_, (length)-$i-1, 1);\n\t\t\tsubstr $_, (length)-$i-1, 1, $r;\n\t\t\t};\n\t\t\n\t\t}\n\tprint \"$_\";\n\t}"}, {"source_code": "$\\ = $/;\nwhile(<>){\n\tchomp; <>;\n\t@a = sort {$a <=> $b} split \" \", <>;\n\t@A = ();\n\t$a[ $_ ] == $a[ $_+1 ] and $a[ $_ ] = $a[ $_+1 ] = -1 for 0 .. @a - 2;\n\t$_ > -1 and push @A, $_ for @a;\n\t@a = @A;\n\t@b = (0) x ((length) / 2);\n\t$j = 0;\n\tfor $i (0 .. @b-1){\t\n\t\t$i == $a[ 0 ]-1 and do {\n\t\t\t++$j;\n\t\t\tshift @a if @a;\n\t\t\t};\n\t\t$b[ $i ] = $j % 2;\n\t\t}\n\tfor $i (0 .. @b-1){\n\t\t$b[ $i ] and do {\n\t\t\t$r = substr $_, $i, 1;\n\t\t\tsubstr $_, $i, 1, (substr $_, (length)-$i-1, 1);\n\t\t\tsubstr $_, (length)-$i-1, 1, $r;\n\t\t\t}\n\t\t}\n\tprint\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\nwhile(<>){\n\tchomp;\n\t<>;\n\t@a = split \" \", <>;\n\t@a = sort {$a <=> $b} @a;\n\t$a = \"@a\";\n\t$a =~ s/(\\b\\d+\\b) \\1//g;\n\t@a = split \" \", $a;\n#\tfor $i (0 .. @a-2){\n#\t\t$a[ $i ] == $a[ $i+1 ] ? $i++ : do{ push @A, $a[ $i ] } ;\n#\t\t\n#\t\t}\n#\t@a = @A;\n\t@b = (0) x ((length) / 2);\n#\tprint \"@b\";\n#\tprint \"@a\";\n\t$f = 0;\n\tfor $i (0 .. @b-1){\n#\t\tprint \"($i == \".($a[0]-1).\")\";\n\t\t@a || last;\n\t\t$i == $a[ 0 ]-1 and do {\n\t\t\t++$j;\n\t\t\t\n\t\t\tshift @a;\n\t\t\t};\n#\t\t\tprint \"<$j>\";\n\t\t$b[ $i ] = $j % 2;\n\t\t}\n#\tprint \"[@b]\";\n#\tprint;\n\tfor $i (0 .. @b-1){\n\t\t$b[ $i ] and do {\n\t\t\t$r = substr $_, $i, 1;\n\t\t\tsubstr $_, $i, 1, (substr $_, (length)-$i-1, 1);\n\t\t\tsubstr $_, (length)-$i-1, 1, $r;\n\t\t\t};\n\t\t\n\t\t}\n\tprint \"$_\";\n\t}"}, {"source_code": "$\\ = $/;\nwhile(<>){\n\t<>;\n\t@a = sort {$a <=> $b} split \" \", <>;\n\t@A = ();\n\t$a[ $_ ] == $a[ $_+1 ] and $a[ $_ ] = $a[ $_+1 ] = -1 for 0 .. @a - 2;\n\t$_ > -1 and push @A, $_ for @a;\n\t@a = @A;\n\t@b = (0) x ((length) / 2);\n\t$j = 0;\n\tfor $i (0 .. @b-1){\t\n\t\t$i == $a[ 0 ]-1 and do {\n\t\t\t++$j;\n\t\t\tshift @a if @a;\n\t\t\t};\n\t\t$b[ $i ] = $j % 2;\n\t\t}\n\tfor $i (0 .. @b-1){\n\t\t$b[ $i ] and do {\n\t\t\t$r = substr $_, $i, 1;\n\t\t\tsubstr $_, $i, 1, (substr $_, (length)-$i-1, 1);\n\t\t\tsubstr $_, (length)-$i-1, 1, $r;\n\t\t\t}\n\t\t}\n\tprint\n\t}"}, {"source_code": "$\\ = $/;\nwhile(<>){\n\tchomp;\n\t<>;\n\t@a = split \" \", <>;\n\t@a = sort {$a <=> $b} @a;\n\t@b = (0) x ((length) / 2);\n#\tprint \"@b\";\n#\tprint \"@a\";\n\t$f = 0;\n\tfor $i (0 .. @b-1){\n#\t\tprint \"($i == \".($a[0]-1).\")\";\n\t\t$i == $a[ 0 ]-1 and do {\n\t\t\t++$j;\n\t\t\t@a || last;\n\t\t\tshift @a;\n\t\t\t};\n#\t\t\tprint \"<$j>\";\n\t\t$b[ $i ] = $j % 2;\n\t\t}\n#\tprint \"[@b]\";\n#\tprint;\n\tfor $i (0 .. @b-1){\n\t\t$b[ $i ] and do {\n\t\t\t$r = substr $_, $i, 1;\n\t\t\tsubstr $_, $i, 1, (substr $_, (length)-$i-1, 1);\n\t\t\tsubstr $_, (length)-$i-1, 1, $r;\n\t\t\t};\n\t\t\n\t\t}\n\tprint \"$_\";\n\t}"}], "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$$$\u00a0\u2014 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$$$\u00a0\u2014 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}$$$\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy %exists = map { $_ => 1 } @_;\n\tmy %not_exists = map { $_ => 1 } grep { not $exists{ $_ } } 1 .. @_ * 2;\n\t\n\tmy @not_exists = sort { $a <=> $b } keys %not_exists;\n\t\n\t$debug and print \"not_exists:[@not_exists]\";\n\t\n\tfor my $i ( @not_exists ){\n\t\tfor my $j ( @_ ){\n\t\t\tnext if $j =~ /_/;\n\t\t\t\n\t\t\tif( $j < $i ){\n\t\t\t\t$j .= \"_\" . $i;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print join ' ', @_;\n\t\n\tif( grep !/_/, @_ ){\n\t\tprint -1;\n\t\t}\n\telse{\n\t\tprint +( join '_', @_ ) =~ y/_/ /r;\n\t\t}\n\t}"}], "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\u2009\u2264\u2009n\u2009\u2264\u2009105), the number of players. The second line contains n integer numbers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 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/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = split / /, <>;\nforeach (0 .. $n-1) {\n\t$a[$_]/=2 while ($a[$_]%2 == 0);\n\t$a[$_]/=3 while ($a[$_]%3 == 0);\n}\n$a[$_]!=$a[0] and say \"No\" and exit foreach (1 .. $n-1);\nsay \"Yes\";"}, {"source_code": "<>, $_ = <>;\n\nmap { \n\t$_ /= 2 while not $_ % 2;\n\t$_ /= 3 while not $_ % 3;\n\t$h{ $_ } ++\n\t} split;\n\nprint qw(No Yes)[ !! (1 == keys %h) ]"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = <>;\n\t%h = ();\n\t\n\tmap { \n\t\t$_ /= 2 while not $_ % 2;\n\t\t$_ /= 3 while not $_ % 3;\n\t\t$h{ $_ } ++\n\t\t} split;\n\t\n\tprint qw(No Yes)[ not not 1 == keys %h ]\n\t\n\t}"}], "negative_code": [], "src_uid": "2bb893703cbffe9aeaa0bed02f42a05c"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009105). The second line contains n space-separated integers h1,\u2009h2,\u2009...,\u2009hn (1\u2009\u2264\u2009hi\u2009\u2264\u2009109) \u2014 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": "sub f {\n\t\t$_[$_ - 1] < $_[$_] and $_[$_] = $_[$_ - 1] + 1\n\t\tfor 1 .. @_ - 1; \n\t\t@_\n\t}\n\nprint + (sort {$b <=> $a} \n\tf(reverse \n\t\tf(<> * 0, (split ' ', <>), 0)\n\t\t)\n\t)[ 0 ]"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = <>;\n\t@_ = (0, split, 0);\n\n\tfor $i (1 .. @_ - 1){\n\t\t$_[$i - 1] < $_[$i] and $_[$i] = $_[$i - 1] + 1\n\t\t}\n\tfor $i (reverse 1 .. @_ - 1){\n\t\t$_[$i - 1] > $_[$i] and $_[$i - 1] = $_[$i] + 1\n\t\t}\n\t\t\n\tprint ( (sort {$b <=> $a} @_)[0] );\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = <>;\n\t@_ = (0, split, 0);\n\t$cnt = 0;\n\twhile (@_ > 1){\n\t\t$cnt ++;\n\t\t@A = ();\n\t\tfor $i (1 .. @_ - 2){\n\t\t\t$minN = (sort {$a <=> $b} map $_[$i + $_], -1, 1)[ 0 ];\n\t\t\t$A[$i] = $minN < $_[$i] ? $minN : $_[$i] && $_[$i] - 1;\n\t\t\t}\n\t\t@B = ($A[1]);\n\t\tfor $i (1 .. @_ - 3){\n\t\t\tprint $i;\n\t\t\t$A[$i] + $A[$i + 1] and push @B, $A[$i + 1];\n\t\t\t}\n\t\t@_ = grep defined, @B;\n\t\t}\n\t\n\tprint $cnt;\n\t}"}], "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$$$\u00a0($$$1 \\leq n \\leq 1000$$$)\u00a0\u2014 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$$$\u00a0($$$-10^6 \\leq x_i, y_i \\leq 10^6$$$)\u00a0\u2014 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$$$\u00a0($$$-2 \\cdot 10^6 \\leq a_i, b_i \\leq 2 \\cdot 10^6$$$)\u00a0\u2014 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$$$\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\t\n\tmy @B = map {\n\t\t[ split ' ', <> ]\n\t\t} 1 .. $_;\n\t\n\tmy @A = map {\n\t\t[ split ' ', <> ]\n\t\t} 1 .. $_;\n\t\n\tmy %h;\n\t\n\tfor my $B ( @B ){\n\t\t$h{ $B->[ 0 ] }{ $B->[ 1 ] } = 1;\n\t\t}\n\t\t\n\t$debug and print \" first:[@{ $B[ 0 ] }]\";\n\t\n\tmy @C;\n\t\n\tfor my $A ( @A ){\n\t\tmy $x = $B[ 0 ][ 0 ] + $A->[ 0 ];\n\t\tmy $y = $B[ 0 ][ 1 ] + $A->[ 1 ];\n\t\t$debug and print \" x,y:($x, $y)\";\n\t\t\n\t\tpush @C, [ $x, $y ];\n\t\t}\n\t\n\tfor my $C ( @C ){\n\t\tmy $ok = 1;\n\t\t\n\t\tfor my $A ( @A ){\n\t\t\tmy $x = $C->[ 0 ] - $A->[ 0 ];\n\t\t\tmy $y = $C->[ 1 ] - $A->[ 1 ];\n\t\t\t\n\t\t\texists $h{ $x }{ $y } or $ok = 0;\n\t\t\tlast if not $ok;\n\t\t\t}\n\t\t\n\t\tif( $ok ){\n\t\t\tprint \"@{ $C }\";\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\t}"}], "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,\u2009k,\u2009d (1\u2009\u2264\u2009n,\u2009d\u2009\u2264\u20091000;\u00a01\u2009\u2264\u2009k\u2009\u2264\u2009109).", "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,$d,$k)=split/ /,<>;\n\nif ($d**$k < $n){print -1; exit}\n\nfor $i(0..$k-1){\n\t\n\tfor $j(0..$n-1){\n\t\t\n\t\tpush @_, $j / $d ** $i % $d +1 .\" \"\n\t\t\n\t\t}\n\n\t}\n\n$,=\"\\n\";\nprint @_"}, {"source_code": "($n,$d,$k)=split/ /,<>;\n\nif ($d**$k < $n){print -1; exit}\n\nfor $i(0..$k-1){\n\t\n\tfor $j(0..$n-1){\n\t\t\n\t\t$_.= $j / $d ** $i % $d +1;\n\t\t$_.= \" \";\n\t\t\n\t\t}\n\t\t\n\tchop;\n\t$_.=\"\\n\"\n\t\n\t}\n\t\nprint"}, {"source_code": "($n,$d,$k)=split/ /,<>;\n\nif ($d**$k < $n){print -1; exit}\n\nfor $i(0..$k-1){\n\t\n\tfor $j(0..$n-1){\n\t\t\n\t\t$_.= $j / $d ** $i % $d +1 .\" \"\n\t\t\n\t\t}\n\t\t\n\ts/ $/\\n/\n\t\n\t}\n\t\nprint"}], "negative_code": [{"source_code": "($n,$k,$d)=split/ /,<>;\n\nif ($d**$k < $n){print -1; exit}\n\nfor $i(0..$k-1){\n\t\n\tfor $j(0..$n-1){\n\t\t\n\t\t$_.= $j / $d ** $i % $d +1;\n\t\t$_.= \" \";\n\t\t\n\t\t}\n\t\t\n\tchop;\n\t$_.=\"\\n\"\n\t\n\t}\n\t\nprint"}], "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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\ts/(.)\\1//g;\n\t\n\t$_ = join '', sort split '';\n\t\n\ts/(.)\\1+/$1/g;\n\t\n\tprint;\n\t}"}], "negative_code": [], "src_uid": "586a15030f4830c68f2ea1446e80028c"} {"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$$$) \u2013 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$$$) \u2013 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\t@_ = ();\n\t\n\t/^\\<*/;\n\tpush @_, length $&;\n\t\n\t/\\>*$/;\n\tpush @_, length $&;\n\t\t\n\tprint +( sort { $a <=> $b } @_ )[ 0 ];\n\t}"}, {"source_code": "$\\ = $/;\n\ns/>.* $b } y/// )[ 0 ] for grep !/\\d/, <>"}], "negative_code": [{"source_code": "s/>.* $b } y/// )[ 0 ] for grep /\\D/, <>"}, {"source_code": "<>;\n\n$_ = <>, s/>.* $b } y/// )[ 0 ] for <>"}], "src_uid": "0ba97bcfb5f539c848f2cd097b34ff33"} {"nl": {"description": "A sequence of square brackets is regular if by inserting symbols \"+\" and \"1\" into it, you can get a regular mathematical expression from it. For example, sequences \"[[]][]\", \"[]\" and \"[[][[]]]\" \u2014 are regular, at the same time \"][\", \"[[]\" and \"[[]]][\" \u2014 are irregular. Draw the given sequence using a minimalistic pseudographics in the strip of the lowest possible height \u2014 use symbols '+', '-' and '|'. For example, the sequence \"[[][]][]\" should be represented as: +- -++- -+ |+- -++- -+|| ||| || ||| ||+- -++- -+|| |+- -++- -+Each bracket should be represented with the hepl of one or more symbols '|' (the vertical part) and symbols '+' and '-' as on the example which is given above.Brackets should be drawn without spaces one by one, only dividing pairs of consecutive pairwise brackets with a single-space bar (so that the two brackets do not visually merge into one symbol). The image should have the minimum possible height. The enclosed bracket is always smaller than the surrounding bracket, but each bracket separately strives to maximize the height of the image. So the pair of final brackets in the example above occupies the entire height of the image.Study carefully the examples below, they adequately explain the condition of the problem. Pay attention that in this problem the answer (the image) is unique. ", "input_spec": "The first line contains an even integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the length of the sequence of brackets. The second line contains the sequence of brackets \u2014 these are n symbols \"[\" and \"]\". It is guaranteed that the given sequence of brackets is regular. ", "output_spec": "Print the drawn bracket sequence in the format which is given in the condition. Don't print extra (unnecessary) spaces. ", "sample_inputs": ["8\n[[][]][]", "6\n[[[]]]", "6\n[[][]]", "2\n[]", "4\n[][]"], "sample_outputs": ["+- -++- -+\n|+- -++- -+|| |\n|| || ||| |\n|+- -++- -+|| |\n+- -++- -+", "+- -+\n|+- -+|\n||+- -+||\n||| |||\n||+- -+||\n|+- -+|\n+- -+", "+- -+\n|+- -++- -+|\n|| || ||\n|+- -++- -+|\n+- -+", "+- -+\n| |\n+- -+", "+- -++- -+\n| || |\n+- -++- -+"], "notes": null}, "positive_code": [{"source_code": "use strict;\nuse warnings;\n\nmy $n = 0 + <>;\nmy $seq = <>;\n\nmy ($depth, $max_depth) = (0, 0);\nfor (my $i = 0; $i < $n; $i++) {\n\tif (substr($seq, $i, 1) eq '[') {\n\t\t$depth++;\n\n\t\tif ($depth > $max_depth) {\n\t\t\t$max_depth = $depth;\n\t\t}\n\t} else {\n\t\t$depth--;\n\t}\n}\n\nmy $top_level_lines_cnt = $max_depth*2 - 1;\n\nmy @image = map { '' } (1 .. $max_depth);\nfor (my $i = 0; $i < $n; $i++) {\n\tif (substr($seq, $i, 1) eq '[') {\n\t\tmy $new_level = $i > 0 && substr($seq, $i-1, 1) eq '[';\n\t\tprint_main_bracket_part($depth++, $new_level);\n\t} else {\n\t\tmy $prev_was_open = substr($seq, $i-1, 1) eq '[';\n\t\tmy $end_level = $i < $n-1 && substr($seq, $i+1, 1) eq ']';\n\t\t$depth--;\n\n\t\t# spaces\n\t\tif ($prev_was_open) {\n\t\t\tprint_additional_bracket_part($depth);\n\n\t\t\tfor (my $j = 0; $j < $top_level_lines_cnt+2; $j++) {\n\t\t\t\t$image[$j] .= ' ';\n\t\t\t}\n\n\t\t\tprint_additional_bracket_part($depth);\n\t\t}\n\n\t\tprint_main_bracket_part($depth, $end_level);\n\t}\n}\n\nforeach my $line (@image) {\n\tprint \"$line\\n\";\n}\n\nsub print_main_bracket_part\n{\n\tmy ($depth, $need_minus) = @_;\n\n\tmy $lines_cnt = ($max_depth - $depth)*2 - 1;\n\tmy $space_cnt = $depth != 0 ? (($top_level_lines_cnt + 2) - ($lines_cnt + 2 + 2)) / 2 : 0;\n\tmy $j = 0;\n\n\t# spaces\n\tfor (my $k = 0; $k < $space_cnt; $k++) { $image[$j++] .= ' '; }\n\n\t# -\n\tif ($depth != 0) {\n\t\t$image[$j++] .= $need_minus ? '-' : ' ';\n\t}\n\n\t# +\n\t$image[$j++] .= '+';\n\n\t# |\n\tfor (my $k = 0; $k < $lines_cnt; $k++) { $image[$j++] .= '|'; }\n\n\t# +\n\t$image[$j++] .= '+';\n\n\t# -\n\tif ($depth != 0) {\n\t\t$image[$j++] .= $need_minus ? '-' : ' ';\n\t}\n\n\t# spaces\n\tfor (my $k = 0; $k < $space_cnt; $k++) { $image[$j++] .= ' '; }\n}\n\nsub print_additional_bracket_part\n{\n\tmy $lines_cnt = ($max_depth - $depth)*2 - 1;\n\tmy $space_cnt = $depth != 0 ? (($top_level_lines_cnt + 2) - ($lines_cnt + 2 + 2)) / 2 : 0;\n\tmy $j = 0;\n\n\t# spaces\n\tfor (my $k = 0; $k < $space_cnt; $k++) { $image[$j++] .= ' '; }\n\n\t# -\n\tif ($depth != 0) {\n\t\t$image[$j++] .= ' ';\n\t}\n\n\t# +\n\t$image[$j++] .= '-';\n\n\t# |\n\tfor (my $k = 0; $k < $lines_cnt; $k++) { $image[$j++] .= ' '; }\n\n\t# +\n\t$image[$j++] .= '-';\n\n\t# -\n\tif ($depth != 0) {\n\t\t$image[$j++] .= ' ';\n\t}\n\n\t# spaces\n\tfor (my $k = 0; $k < $space_cnt; $k++) { $image[$j++] .= ' '; }\n}\n"}], "negative_code": [], "src_uid": "7d9fb64a0a324a51432fbb01b4cc2c0e"} {"nl": {"description": "One day Vasya decided to have a look at the results of Berland 1910 Football Championship\u2019s 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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of lines in the description. Then follow n lines \u2014 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": "$h{<>}++ for 1..<>;\n($a,$b)=keys %h;\nprint $h{$a}>$h{$b}?$a:$b;"}, {"source_code": "$n=<>;\n$h{<>}++ for 1..$n;\n($a,$b)=keys %h;\nprint $h{$a}>$h{$b}?$a:$b"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\n\nmy %goals = ();\nfor (1..<>) {\n\t$goals{<>}++;\n}\nmy ($winner) = sort {$goals{$b}<=>$goals{$a}} keys %goals;\nprint $winner;"}, {"source_code": "<>;\n$a=<>;$i++;$j=0;\nchomp($a);\nwhile(<>){\nchomp;\nif ($_ eq $a) { $i++} elsif ($j) {$j++} else {$j++; $b=$_}\n \n}\nif($i>$j) {$b=$a;}\n \nprint$b"}], "negative_code": [{"source_code": "<>;\n$a=<>;$i++;\nchomp($a);\nwhile(<>){\nchomp;\n$_ eq $a ? $i++:$j++\n \n}\n$i>$j and $_=$a;\n \nprint"}, {"source_code": "<>;\n$a=<>;$i++;\nchomp($a);\nwhile(<>){\nchomp;\n$_ eq $a ? $i++:$j++\n \n}\n$i-$j and $_=$a;\n \nprint\n"}], "src_uid": "e3dcb1cf2186bf7e67fd8da20c1242a9"} {"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\u2009-\u2009p vertices where 1\u2009\u2264\u2009p\u2009<\u2009x 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of tests Peter is about to make. The second line contains n space separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109), 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": "<>; @a = split \" \", <>;\nfor (@a) {\n\t$e = $_&1;\n\t$r = $r? $e: !$e;\n\tprint $r?1:2, \"\\n\";\n}"}], "negative_code": [], "src_uid": "3a767b3040f44e3e2148cdafcb14a241"} {"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\u2009\u00d7\u2009n 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\u2009-\u20091 positive integers a1,\u2009a2,\u2009...,\u2009an\u2009-\u20091. For every integer i where 1\u2009\u2264\u2009i\u2009\u2264\u2009n\u2009-\u20091 the condition 1\u2009\u2264\u2009ai\u2009\u2264\u2009n\u2009-\u2009i holds. Next, he made n\u2009-\u20091 portals, numbered by integers from 1 to n\u2009-\u20091. The i-th (1\u2009\u2264\u2009i\u2009\u2264\u2009n\u2009-\u20091) portal connects cell i and cell (i\u2009+\u2009ai), and one can travel from cell i to cell (i\u2009+\u2009ai) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i\u2009+\u2009ai) to cell i using the i-th portal. It is easy to see that because of condition 1\u2009\u2264\u2009ai\u2009\u2264\u2009n\u2009-\u2009i 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\u2009\u2264\u2009n\u2009\u2264\u20093\u2009\u00d7\u2009104) and t (2\u2009\u2264\u2009t\u2009\u2264\u2009n) \u2014 the number of cells, and the index of the cell which I want to go to. The second line contains n\u2009-\u20091 space-separated integers a1,\u2009a2,\u2009...,\u2009an\u2009-\u20091 (1\u2009\u2264\u2009ai\u2009\u2264\u2009n\u2009-\u2009i). 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,\u20092,\u20094; so we can successfully visit the cell 4.In the second sample, the possible cells to visit are: 1,\u20092,\u20094,\u20096,\u20097,\u20098; so we can't visit the cell 5, which we want to visit."}, "positive_code": [{"source_code": "($a, $b) = split \" \", <>;\nmy @arr = split \" \", <>;\nmy $i = 0;\nuntil ($i >= $b-1) {\n $i += $arr[$i];\n}\nprint $i == $b-1? \"YES\" : \"NO\";"}, {"source_code": "my ($n, $t) = split \" \", <>;\nmy @arr = split \" \", <>;\nunshift @arr, 0;\nmy $pos = 1;\n$pos += $arr[$pos] while ($pos < $t);\nprint ($pos == $t ? \"YES\\n\" : \"NO\\n\");\n"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nour $flag = 0;\nour ($n, $t) = split / /, <>;\nour @adj = ([()]) x $n+1;\nour @visit = (0) x $n+1;\n\nsub dfs {\n\tmy $u = shift;\n\t\n\t# say $u;\n\t$visit[$u] = 1;\n\t$u==$t and return 1;\n\tforeach my $v (@{$adj[$u]}) {\n\t\t$visit[$v]==0 and dfs($v) and return 1;\n\t}\n\treturn 0;\n}\n\nmy $i = 1;\nforeach (split / /, <>) {\n\tpush @{$adj[$i]}, $_+$i;\n\t++$i;\n}\n\ndfs(1) and say \"YES\" or say \"NO\";\n"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nour $flag = 0;\nour ($n, $t) = split / /, <>;\nour @adj = ([]) x $n+1;\nour @visit = (0) x $n+1;\n\nsub dfs {\n\tmy $u = shift;\n\t\n\t# say $u;\n\t$visit[$u] = 1;\n\t$u==$t and return 1;\n\tforeach my $v (@{$adj[$u]}) {\n\t\t$visit[$v]==0 and dfs($v) and return 1;\n\t}\n\treturn 0;\n}\n\nmy $i = 1;\nforeach (split / /, <>) {\n\tpush @{$adj[$i]}, $_+$i;\n\t++$i;\n}\n\ndfs(1) and say \"YES\" or say \"NO\";\n"}, {"source_code": "use Data::Dumper;\nmy ($n, $t) = split ' ', <>;\nmy @indices = split ' ', <>;\nmy @a = ();\npush @a, 0 for (1 .. $n);\n\n$a[0] = 1;\n\nfor my $i (0 .. $n - 2) {\n my $j = shift @indices;\n $a[$i + $j] = 1 if $a[$i];\n}\n\nprint $a[$t - 1] == 1 ? \"YES\\n\" : \"NO\\n\";\n"}, {"source_code": "my ($n, $t) = split ' ', scalar <>;\nmy @a = split ' ', scalar <>;\nmy @used = (0) x scalar @a;\nmy $i;\nfor($i=0; $i<$t-1; $i += $a[$i]){}\nprint (($i == $t-1) ? \"YES\\n\" : \"NO\\n\");\n"}, {"source_code": "($n, $t, @_) = split /\\s/, <> . '1 ' . <>;\n($s += $_[$s]) == $t && $f++ while $s < $t;\nprint $f ? YES : NO"}, {"source_code": "($n, $t) = split \" \", <>;\n@_ = split \" \", <>;\n@a = (1) x 2;\nwhile (@_){\n\t$_ = shift @_;\n\tshift @_ for 1 .. $_ - 1;\n\tpush @a, $a[-1] += $_;\n\t}\n$t == $_ and $f++ for @a;\nprint $f ? q(YES) : q(NO)"}, {"source_code": "($n, $t) = split \" \", <>;\n@_ = (1, split \" \", <>);\n($s += $_[$s]) == $t && $f++ while $s < $t;\nprint $f ? q(YES) : q(NO)"}, {"source_code": "$\\ = $/;\nwhile (<>){\n\t($n, $t)=split;\n\t$_ = <>;\n\t@_ = split;\n\t$f = 0;\n\t@a = (1) x 2;\n#\tprint \"@_\";\n\twhile (@_){\n\t\t$_ = shift @_;\n\t\tfor $i (1 .. $_ - 1){shift @_};\n\t\tpush @a, $a[-1] += $_;\n\t\t}\n#\t\tprint \"@a\";\n\t$t == $_ and $f++ for @a;\n\tprint $f ? q(YES) : q(NO)\n\t}"}], "negative_code": [{"source_code": "my ($n, $t) = split \" \", <>;\nmy @arr = split \" \", <>;\nmy $pos = 1;\n$pos += $arr[$pos] while ($pos < $t);\nprint ($pos == $t ? \"YES\\n\" : \"NO\\n\");\n"}, {"source_code": "my ($n, $t) = split \" \", <>;\nmy @arr = split \" \", <>;\nmy @mark = (0) x $n+1;\n$mark[1] = 1;\nfor (my $pos = 1; $pos != $t; $pos++) {\n $pos += int $arr[$pos];\n if ($pos >= $n-1 || $mark[$pos]) {\n print \"NO\\n\";\n exit;\n }\n $mark[$pos] = 1;\n}\nprint \"YES\\n\";\n"}, {"source_code": "use Data::Dumper;\nmy ($n, $t) = split ' ', <>;\nmy @indices = split ' ', <>;\nmy @a = ();\npush @a, 0 for (1 .. $n);\n\nfor my $i (0 .. $n - 2) {\n my $j = shift @indices;\n $a[$i + $j] = 1 if $a[$i];\n}\n\nprint $a[$t - 1] == 1 ? \"YES\\n\" : \"NO\\n\";\n"}], "src_uid": "9ee3d548f93390db0fc2f72500d9eeb0"} {"nl": {"description": "Petya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well.In the language, there are only three commands: apply a bitwise operation AND, OR or XOR with a given constant to the current integer. A program can contain an arbitrary sequence of these operations with arbitrary constants from 0 to 1023. When the program is run, all operations are applied (in the given order) to the argument and in the end the result integer is returned.Petya wrote a program in this language, but it turned out to be too long. Write a program in CALPAS that does the same thing as the Petya's program, and consists of no more than 5 lines. Your program should return the same integer as Petya's program for all arguments from 0 to 1023.", "input_spec": "The first line contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u20095\u00b7105) \u2014 the number of lines. Next n lines contain commands. A command consists of a character that represents the operation (\"&\", \"|\" or \"^\" for AND, OR or XOR respectively), and the constant xi 0\u2009\u2264\u2009xi\u2009\u2264\u20091023.", "output_spec": "Output an integer k (0\u2009\u2264\u2009k\u2009\u2264\u20095) \u2014 the length of your program. Next k lines must contain commands in the same format as in the input.", "sample_inputs": ["3\n| 3\n^ 2\n| 1", "3\n& 1\n& 3\n& 5", "3\n^ 1\n^ 2\n^ 3"], "sample_outputs": ["2\n| 3\n^ 2", "1\n& 1", "0"], "notes": "NoteYou can read about bitwise operations in https://en.wikipedia.org/wiki/Bitwise_operation.Second sample:Let x be an input of the Petya's program. It's output is ((x&1)&3)&5\u2009=\u2009x&(1&3&5)\u2009=\u2009x&1. So these two programs always give the same outputs."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\n\n$\\ = $/;\n\nwhile(<>){\n\t$A = 0, $B = 1023;\n\t\n\tfor( map ~~<>, 1 .. $_ ){\n\t\tchomp;\n\t\t/ /;\n\t\t$` eq '&' and map $_ &= $', $A, $B;\n\t\t$` eq '|' and map $_ |= $', $A, $B;\n\t\t$` eq '^' and map $_ ^= $', $A, $B;\n\t\t}\n\t\n\tprint 3;\n\tprint \"& \", ( 1023 & $A ) | $B;\n\tprint \"^ \", $A & ( 1023 ^ $B );\n\tprint \"| \", $A & $B;\n\t}"}, {"source_code": "# a -> b 0 1 ^ | &\n# & 1 = | 0 = as is 0 1 0 0 1\n# & 0 = to 0 0 0 0 0 0\n# | 1 = to 1 1 1 0 1 1\n# ^ 1 = flip 1 0 1 0 1\n\n%outbit = (\n\t\"01\" => \"001\",\n\t\"00\" => \"000\",\n\t\"11\" => \"011\", \n\t\"10\" => \"101\"\n);\n\n<>; $/ = undef; $_ = <>; @input = split; $k = @input;\n$a = 0; $b = 1023; \nfor ($i = 0; $i < $k; $i += 2) { \n\t$op = $input[$i]; $val = $input[$i + 1];\n\tif ($op eq \"|\") { $a |= $val; $b |= $val; }\n\tif ($op eq \"&\") { $a &= $val; $b &= $val; }\n\tif ($op eq \"^\") { $a ^= $val; $b ^= $val; }\n}\n\n@a = reverse(split(//, sprintf(\"%010b\", $a))); \n@b = reverse(split(//, sprintf(\"%010b\", $b))); \n\nfor $bit (0..9) {\n\t$i = $a[$bit];\n\t$j = $b[$bit];\n\t@outbit = split //, $outbit{\"$i$j\"};\n\t$v1 += ($outbit[0] << $bit); \n\t$v2 += ($outbit[1] << $bit); \n\t$v3 += ($outbit[2] << $bit); \n}\n\n$\\ = \"\\n\";\nprint 3;\nprint \"^ $v1\";\nprint \"| $v2\";\nprint \"& $v3\";\n\t "}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\n\n$\\ = $/;\n\nwhile(<>){\n\t$A = 0, $B = 1023;\n\t\n\tfor( map ~~<>, 1 .. $_ ){\n\t\t/ /;\n\t\t$` eq '&' and map $_ &= $', $A, $B;\n\t\t$` eq '|' and map $_ |= $', $A, $B;\n\t\t$` eq '^' and map $_ ^= $', $A, $B;\n\t\t}\n\t\n\tprint 3;\n\tprint \"& \", ( 1023 & $A ) | $B;\n\tprint \"| \", $A & ( 1023 ^ $B );\n\tprint \"^ \", $A & $B;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\t\n\tmy $and = -1;\n\t\n\tfor my $i ( reverse 0 .. @_ - 1 ){\n\t\t$_[ $i ] =~ /&/ and do {\n\t\t\t$and = $i;\n\t\t\tlast;\n\t\t\t};\n\t\t}\n\t\n\tmy $AND = 1023;\n\tmy $OR = 0;\n\tmy $XOR = 0;\n\t\n\tfor my $i ( 0 .. $and ){\n\t\t$_[ $i ] =~ /^&/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$AND &= $&;\n\t\t\tnext;\n\t\t\t};\n\t\t$_[ $i ] =~ /^\\|/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$OR |= $&;\n\t\t\tnext;\n\t\t\t};\n\t\t$_[ $i ] =~ /^\\^/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$XOR ^= $&;\n\t\t\t};\n\t\t}\n\t\n\tprint 5;\n\t\n\tprint \"^ $XOR\";\n\tprint \"| $OR\";\n\tprint \"& $AND\";\n\t\n\tmy $OR2 = 0;\n\tmy $XOR2 = 0;\n\t\n\tfor my $i ( $and + 1 .. @_ - 1 ){\n\t\t$_[ $i ] =~ /^\\|/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$OR2|= $&;\n\t\t\tnext;\n\t\t\t};\n\t\t$_[ $i ] =~ /^\\^/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$XOR2 ^= $&;\n\t\t\t};\n\t\t}\n\t\n\tprint \"^ $XOR2\";\n\tprint \"| $OR2\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\t\n\tmy $and = -1;\n\t\n\tfor my $i ( reverse 0 .. @_ - 1 ){\n\t\t$_[ $i ] =~ /&/ and do {\n\t\t\t$and = $i;\n\t\t\tlast;\n\t\t\t};\n\t\t}\n\t\n\tmy $AND = 1023;\n\tmy $OR = 0;\n\tmy $XOR = 0;\n\t\n\tfor my $i ( 0 .. $and ){\n\t\t$_[ $i ] =~ /^&/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$AND &= $&;\n\t\t\tnext;\n\t\t\t};\n\t\t$_[ $i ] =~ /^\\|/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$OR |= $&;\n\t\t\tnext;\n\t\t\t};\n\t\t$_[ $i ] =~ /^\\^/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$XOR ^= $&;\n\t\t\t};\n\t\t}\n\t\n\tprint 5;\n\t\n\tprint \"^ $XOR\";\n\tprint \"| $OR\";\n\tprint \"& $AND\";\n\t\n\tmy $OR2 = 0;\n\tmy $XOR2 = 0;\n\t\n\tfor my $i ( $and + 1 .. @_ - 1 ){\n\t\t$_[ $i ] =~ /^\\|/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$OR2|= $&;\n\t\t\tnext;\n\t\t\t};\n\t\t$_[ $i ] =~ /^\\^/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$XOR2 ^= $&;\n\t\t\t};\n\t\t}\n\t\n\tprint \"| $OR2\";\n\tprint \"^ $XOR2\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\t\n\tmy $and = -1;\n\t\n\tfor my $i ( reverse 0 .. @_ - 1 ){\n\t\t$_[ $i ] =~ /&/ and do {\n\t\t\t$and = $i;\n\t\t\tlast;\n\t\t\t};\n\t\t}\n\t\n\tmy $AND = 1023;\n#\tprintf \"%b\\n\", 1023;\n\tmy $OR = 0;\n\tmy $XOR = 0;\n\t\n\tfor my $i ( 0 .. $and ){\n\t\t$_[ $i ] =~ /^&/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$AND &= $&;\n\t\t\tnext;\n\t\t\t};\n\t\t$_[ $i ] =~ /^\\|/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$OR |= $&;\n\t\t\tnext;\n\t\t\t};\n\t\t$_[ $i ] =~ /^\\^/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$XOR ^= $&;\n\t\t\t};\n\t\t}\n\t\n\tprint 5;\n\t\n\tprint \"| $OR\";\n\tprint \"^ $XOR\";\n\tprint \"& $AND\";\n\t\n\tmy $OR2 = 0;\n\tmy $XOR2 = 0;\n\t\n\tfor my $i ( $and + 1 .. @_ - 1 ){\n\t\t$_[ $i ] =~ /^\\|/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$OR2|= $&;\n\t\t\tnext;\n\t\t\t};\n\t\t$_[ $i ] =~ /^\\^/ and do {\n\t\t\t$_[ $i ] =~ /\\d+/;\n\t\t\t$XOR2 ^= $&;\n\t\t\t};\n\t\t}\n\t\n\tprint \"| $OR2\";\n\tprint \"^ $XOR2\";\n\t}"}, {"source_code": "<>; $_ = join \"\", <>, \"\\n\";\n\nm{\n\t(\n\t\t\\& \\s ( \\d+ (\\n \\& \\s \\d+)* ) \\n\n\t)?\n\t(\n\t\t([|^]) \\s ( \\d+ (\\n [&|^] \\s \\d+)* )\n\t)?\n}x; \n\n$v1 = eval($2) || 1023; $op = $5 || \"|\"; $v2 = eval($6) || 0;\nprint \"2\\n& $v1\\n$op $v2\";\n"}, {"source_code": "# a -> b 0 1 ^ | &\n# & 1 = | 0 = as is 0 1 0 0 1\n# & 0 = to 0 0 0 0 0 0\n# | 1 = to 1 1 1 0 1 1\n# ^ 1 = flip 1 0 1 0 1\n\n%outbit = (\n\t\"01\" => \"001\",\n\t\"00\" => \"000\",\n\t\"11\" => \"011\", \n\t\"10\" => \"101\"\n);\n\n$n = <>; $/ = undef; $_ = <>; @input = split;\n$a = 0; $b = 1023; \nfor ($i = 0; $i < $n; $i += 2) { \n\t$op = $input[$i]; $val = $input[$i + 1];\n\tif ($op eq \"|\") { $a |= $val; $b |= $val; }\n\tif ($op eq \"&\") { $a &= $val; $b &= $val; }\n\tif ($op eq \"^\") { $a ^= $val; $b ^= $val; }\n}\n\n@a = reverse(split(//, sprintf(\"%010b\", $a))); \n@b = reverse(split(//, sprintf(\"%010b\", $b))); \n\nfor $bit (0..9) {\n\t$i = $a[$bit];\n\t$j = $b[$bit];\n\t@outbit = split //, $outbit{\"$i$j\"};\n\t$v1 += ($outbit[0] << $bit); \n\t$v2 += ($outbit[1] << $bit); \n\t$v3 += ($outbit[2] << $bit); \n}\n\n$\\ = \"\\n\";\nprint 3;\nprint \"^ $v1\";\nprint \"| $v2\";\nprint \"& $v3\";\n\t "}], "src_uid": "19c32b8c1d3db5ab10aca271135aa9b8"} {"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$$$) \u2014 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$$$) \u2014 elements of the initial array. Each of the following $$$q$$$ lines contains two integers. The first integer is $$$t$$$ ($$$1 \\le t \\le 2$$$) \u2014 the type of query. If $$$t = 1$$$ the second integer is $$$x$$$ ($$$1 \\le x \\le n$$$) \u2014 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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $q ) = split;\n\t@_ = split ' ', <>;\n\t\n\tmy $c1 = grep $_, @_;\n\t\n\tfor( 1 .. $q ){\n\t\tmy( $t, $x ) = split ' ', <>;\n\t\t\n\t\tif( $t == 1 ){\n\t\t\tif( $_[ $x - 1 ] == 1 ){\n\t\t\t\t$c1 --;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$c1 ++;\n\t\t\t\t}\n\t\t\t$_[ $x - 1 ] = 1 - $_[ $x - 1 ];\n\t\t\t}\n\t\telse{\n\t\t\tif( $c1 >= $x ){\n\t\t\t\tprint 1;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\tprint 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t}"}], "negative_code": [], "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\u00b7m 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\u00b7m from left to right, then the j-th flat of the i-th floor has windows 2\u00b7j\u2009-\u20091 and 2\u00b7j 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100)\u00a0\u2014 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\u00b7m 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\u00a0\u2014 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": "print 0 + grep 0 + $_, map y/ //dr, (<>, $/ = $\\, <>) =~ /\\G.{4}/gs\n"}, {"source_code": "($n, $m) = split ' ', <>;\n\nfor (1..$n) {\n\t$s = <>;\n\tfor ($i = 0; $i < 4 * $m; $i += 4) {\n\t\t$c++ if substr($s, $i, 3) ne \"0 0\";\n\t}\n}\n\nprint 0 + $c, \"\\n\";"}, {"source_code": "my ($n, $m) = split / /, <>;\n($n, $m) = (int $n,int $m);\nmy @arr;\nmy $ans = 0;\nfor (1..$n) {\n @arr = split / /, <>;\n for (my $j = 0 ; $j < 2 * $m; $j+=2) {\n $ans++ if ($arr[$j] == '1' || $arr[$j + 1] == '1');\n }\n}\nprint \"$ans\\n\";\n"}, {"source_code": "print 0 + grep 0 + $_, map y/ //dr, (<>, $/ = $\\, <>) =~ /\\G.{4}/gs"}, {"source_code": "print 0 + grep 0 + $_, map y/ //dr, (<>, $/ = $\\, <>) =~ /\\G.{4}/gs\n"}, {"source_code": "print 0 + grep 0 + $_, map y/ //dr, (<>, $/ = $\\, <>) =~ /\\G.{4}/gs\n"}, {"source_code": "print 0 + grep 0 + $_, map y/ //dr, (<>, $/ = $\\, <>) =~ /\\G.{4}/gs\n"}, {"source_code": "print 0 + grep 0 + $_, map y/ //dr, (<>, $/ = $\\, <>) =~ /\\G.{4}/gs\n"}, {"source_code": "print 0 + grep 0 + $_, map y/ //dr, (<>, $/ = $\\, <>) =~ /\\G.{4}/gs\n"}, {"source_code": "<>;\n$_ = join '', split ' ', join '', <>;\nprint 0 + grep 0 + $_, /\\G../g"}, {"source_code": "print 0 + grep 0 + $_, map y/ //dr, (<>, $/ = $\\, <>) =~ /\\G.{4}/gs"}, {"source_code": "print 0 + grep 0 + $_, map y/ //dr, (<>, $/ = $\\, <>) =~ /\\G.{4}/gs\n"}], "negative_code": [{"source_code": "($n, $m) = split ' ', <>;\n\nfor (1..$n) {\n\t$s = <>;\n\tfor ($i = 0; $i < 4 * $m; $i += 4) {\n\t\t$c++ if substr($s, $i, 3) ne \"0 0\";\n\t}\n}\n\nprint $c, \"\\n\";"}, {"source_code": "my ($n, $m) = split / /, <>;\n($n, $m) = (int $n,int $m);\nmy @arr;\nmy $ans = 0;\nfor (1..$n) {\n @arr = split / /, <>;\n for (my $j = 0 ; $j < 2 * $m; $j+=2) {\n $ans++ if ($arr[$j] || $arr[$j + 1]);\n }\n}\nprint \"$ans\\n\";\n"}, {"source_code": "<>;\nprint 0 + grep 0 + $_, map y/ //dr, (<>.<>) =~ /\\G....?/gs"}, {"source_code": "<>;\nprint 0 + (() = <> =~ /[^0]{2}/gm)"}, {"source_code": "print 0 + grep 0 + $_, map y/ //dr, (<>,<>.<>) =~ /\\G....?/gs"}, {"source_code": "<>;\n$_ = join '', split ' ', join '', <>;\nprint 0 + (() = /01|10|11/g)"}], "src_uid": "5b9aed235094de7de36247a3b2a34e0f"} {"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.\u2009e., 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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\t@A = ( 1 ) x ( 1 + length );\n\t\n\t&add_forward_cities;\n\t&add_forward_cities;\n\t\n\tprint \"@A\";\n\t}\n\nsub add_forward_cities {\n\t$_ = reverse;\n\ty/RL/LR/;\n\t@A = reverse @A;\n\t\n\t$length = 0;\n\t\n\t/\t\n\t\t(?=R)\n\t\t(?{ $length > 0 and $length -= 2 })\n\t\t(?(?{ $length <= 0 })\n\t\t\t(?: (?:RL)*+ ){0,10}+\n\t\t\tR?+\n\t\t\t(?{ $length = length $& })\n\t\t\t)\n\t\t(?{\n\t\t\t$A[ $-[ 0 ] ] += $length\n\t\t\t})\n\t\t(*F)\n\t\t/x;\n\t}"}, {"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$_ = <>, chomp;\r\n\t\r\n\t@A = ( 1 ) x ( 1 + length );\r\n\t\r\n\t&fill;\r\n\t&fill;\r\n\t\r\n\tprint \"@A\";\r\n\t}\r\n\r\nsub fill {\r\n\t$_ = reverse;\r\n\ty/RL/LR/;\r\n\t@A = reverse @A;\r\n\t\r\n\t$L = 0;\r\n\t\r\n\t/\t\r\n\t\t(?=R)\r\n\t\t(?{ $L > 0 and $L -= 2 })\r\n\t\t(?(?{ $L <= 0 })\r\n\t\t\t(?: (?:RL)*+ ){0,10}+\r\n\t\t\tR?+\r\n\t\t\t(?{ $L = length $& })\r\n\t\t\t)\r\n\t\t(?{\r\n\t\t\t$A[ $-[ 0 ] ] += $L\r\n\t\t\t})\r\n\t\t(*F)\r\n\t\t/x;\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t$_ = <>, chomp;\n\t\n\t$debug and print \"\\$_:[$_]\";\n\t\n\tmy @A = ( 1 ) x ( 1 + length );\n\t\n\tfill_array_with_counts_of_forward_nodes(\n\t\t$_, \\@A\n\t\t);\n\t\n\t$debug and print \"\\@A:[@A]\";\n\t\n\t$_ = reverse;\n\t$_ =~ y/RL/LR/;\n\t@A = reverse @A;\n\t\n\tfill_array_with_counts_of_forward_nodes(\n\t\t$_, \\@A\n\t\t);\n\t\n\t@A = reverse @A;\n\t\n\t$debug and print \"\\@A:[@A]\";\n\t\n\tprint \"@A\";\n\t}\n\nsub fill_array_with_counts_of_forward_nodes {\n\tmy( $line, $ref_array ) = @_;\n\t\n\tmy $len = 0;\n\t\n\t$line =~ /\t\n\t\t(?{\n\t\t\t$debug and print \">[$-[0]][$`][$&][$'][@{ $ref_array }][$len]\";\n\t\t\t})\n\t\t(?=R)\n\t\t(?{ $len > 0 and $len -= 2; })\n\t\t(?(?{ $len <= 0; })\n\t\t\t(?: (?:RL)*+ ){0,10}+\n\t\t\tR?+\n\t\t\t(?{ $len = length $&; })\n\t\t\t)\n\t\t(?{\n\t\t\t$debug and print \"[$-[ 0 ]], len:[$len]\";\n\t\t\t${ $ref_array }[ $-[ 0 ] ] += $len;\n\t\t\t})\n\t\t(*FAIL)\n\t\t/x;\n\t}"}, {"source_code": "#!/usr/bin/perl\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my $s = ;\r\n \r\n my @f = ();\r\n my $st = '';\r\n my $st_i = -1;\r\n my $st_l = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n my $c1 = substr($s,$i,1);\r\n if( $st ne '' ){\r\n if( ( $i - $st_i ) % 2 == 1 ){\r\n if( $st eq $c1 ){\r\n push(@f,+[$st_i,$i-$st_i]); $st = '';\r\n }\r\n } else {\r\n if( $st ne $c1 ){\r\n push(@f,+[$st_i,$i-$st_i]); $st = '';\r\n }\r\n }\r\n }\r\n if( $st eq '' ){\r\n $st = $c1;\r\n $st_i = $i;\r\n }\r\n }\r\n push(@f,+[$st_i,$n-$st_i]);\r\n \r\n my $j = 0;\r\n for(my $i=0;$i<=$n;$i++){\r\n my $r1 = 0; my $r2 = 0;\r\n $j++ if $i >= $f[$j]->[0] + $f[$j]->[1];\r\n #$j\u306f\u53f3\u3068\u4e2d\r\n if( $j<=$#f ){\r\n my ($k,$l) = ($f[$j]->[0],$f[$j]->[1]);\r\n my $c1 = substr($s,$k,1);\r\n if( ( $c1 eq 'L' and ($i-$k) % 2 == 1 ) or ( $c1 eq 'R' and ($i-$k) % 2 == 0 ) ){\r\n $r1 = $l + 1;\r\n }\r\n }\r\n if( $j>0 ){ # \u5de6\u306e\u307f\r\n my ($k,$l) = ($f[$j-1]->[0],$f[$j-1]->[1]);\r\n if( $k + $l == $i ){ # \u96a3\u63a5\u6642\u306e\u307f\r\n my $c1 = substr($s,$k,1);\r\n if( ( $c1 eq 'L' and ($i-$k) % 2 == 1 ) or ( $c1 eq 'R' and ($i-$k) % 2 == 0 ) ){\r\n $r2 = $l + 1;\r\n }\r\n }\r\n }\r\n my $r = $r1 + $r2;\r\n $r -- if $r1>0 and $r2>0;\r\n $r = 1 if $r == 0;\r\n if( $i > 0 ){ print \" \"; }\r\n print \"$r\";\r\n }\r\n print \"\\n\";\r\n \r\n}\r\n\r\nexit(0);\r\n\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\u2009\u00d7\u2009m 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,\u2009d2,\u2009...,\u2009dk a cycle if and only if it meets the following condition: These k dots are different: if i\u2009\u2260\u2009j then di is different from dj. k is at least 4. All dots belong to the same color. For all 1\u2009\u2264\u2009i\u2009\u2264\u2009k\u2009-\u20091: di and di\u2009+\u20091 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200950): 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": "use strict;\n#use warnings;\n<>;\nmy @matrix;\npush @matrix, [];\nfor(<>)\n{\n\tchomp;\n\tpush @matrix, [0,split(//,$_ ),0];\n}\npush @matrix, [];\n\nfor my $x (1..$#matrix-1)\n{\n\tfor my $y (1..$#{$matrix[$x]}-1)\n\t{\n\t\tif($matrix[$x][$y])\n\t\t{\n\t\t\tmy $toDel = [[]];\n\t\t\tfunc($x,$y,0,$toDel);\n\t\t\tDel($toDel);\n\t\t}\n\t}\n}\nprint \"No\";\nsub Del\n{\n\tmy $toDel = shift;\n\tfor my $x (0..$#matrix)\n\t{\n\t\tfor my $y (0..$#{$matrix[$x]})\n\t\t{\n\t\t\t$matrix[$x][$y] = 0 if($toDel->[$x][$y]);\n\t\t\t\n\t\t}\n\t}\n}\nsub func\n{\n\tmy ($x,$y, $dir,$path) = @_;\n\t\n\tif(!defined $path->[$x][$y])\n\t{\n\t\tmy $color = $matrix[$x][$y];\n\t\t$path->[$x][$y] = 1;\n\t\tcheckWin($x,$y,$dir,$path);\n\t\tif($dir ne 'r' and $color eq $matrix[$x-1][$y])\n\t\t{\n\t\t\tfunc($x-1, $y, 'l', $path);\n\t\t}\n\t\tif($dir ne 'l' and $color eq $matrix[$x+1][$y])\n\t\t{\n\t\t\tfunc($x+1, $y, 'r', $path);\n\t\t}\n\t\tif($dir ne 'd' and $color eq $matrix[$x][$y+1])\n\t\t{\n\t\t\tfunc($x, $y+1, 'u', $path);\n\t\t}\n\t\tif($dir ne 'u' and $color eq $matrix[$x][$y-1])\n\t\t{\n\t\t\tfunc($x, $y-1, 'd', $path);\n\t\t}\n\t}\n}\nsub checkWin\n{\n\tmy ($x,$y, $dir,$path) = @_;\n\tif($matrix[$x][$y] eq $matrix[$x+1][$y] and $path->[$x+1][$y] and $dir ne 'l' ){ print \"Yes\" and exit 0;}\n\tif($matrix[$x][$y] eq $matrix[$x-1][$y] and $path->[$x-1][$y] and $dir ne 'r' ){ print \"Yes\" and exit 0;}\n\tif($matrix[$x][$y] eq $matrix[$x][$y+1] and $path->[$x][$y+1] and $dir ne 'd' ){ print \"Yes\" and exit 0;}\n\tif($matrix[$x][$y] eq $matrix[$x][$y-1] and $path->[$x][$y-1] and $dir ne 'u' ){ print \"Yes\" and exit 0;}\n\treturn 0;\n}\n"}, {"source_code": "while(<>){\n\t($n, $m) = split;\n\tundef *A;\n\tfor $i (1 .. $n){\n\t\t$_ = <>, chomp;\n\t\t$j = 0;\n\t\t$A[ $i ][ ++$j ] = $_ for split//;\n\t\t}\n\t\n\t$f = 1; \n\tWH:\n\twhile (1){\n\t\n\tfor $i (1 .. $n){\n\t\tfor $j (1 .. $m){\n\t\t\t$A = $A[ $i ][ $j ];\n\t\t\t$A eq '.' and next;\n\t\t\t$u = 0;\n\t\t\t$A eq $A[ $i-1 ][ $j ] and $u++;\n\t\t\t$A eq $A[ $i+1 ][ $j ] and $u++;\n\t\t\t$A eq $A[ $i ][ $j-1 ] and $u++;\n\t\t\t$A eq $A[ $i ][ $j+1 ] and $u++;\n\t\t\tif ($u < 2){ $A[ $i ][ $j ] = '.' ; next WH }\n\t\t\t}\n\t\t}\n\tlast\n\t}\n\tfor $i (1 .. $n){\n\t\tfor $j (1 .. $m){\n\t\t\tif ($A[ $i ][ $j ] ne '.'){ $f = 0 }\n\t\t\t}\n\t\t}\t\n\t\t\n\tprint qw(Yes No)[ $f ]\n\t}"}], "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,\u2009s2,\u2009...,\u2009sn. 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\u2009\u2264\u2009i\u2009\u2264\u2009n). Help Polycarpus determine the length of the city phone code. ", "input_spec": "The first line of the input contains an integer n (2\u2009\u2264\u2009n\u2009\u2264\u20093\u00b7104) \u2014 the number of Polycarpus's friends. The following n lines contain strings s1,\u2009s2,\u2009...,\u2009sn \u2014 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": "$k = <>;\n$p = <>;\nwhile (<>) {\n for ($i = length $p; $i >= 0; $i--) {\n if (substr($_, 0, $i) eq substr($p, 0, $i)) {\n $p = substr($_, 0, $i);\n last;\n }\n }\n}\nprint length $p, \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\nchomp($n = );\n$nums;\nfor ($i = 0; $i < $n; $i++)\n{\n chomp($num = );\n push(@nums, $num);\n #print $num . \"\\n\";\n}\n\n$numlen = length $nums[0];\n#$res = 0;\nfor ($i = $numlen - 1; $i >= 0; $i--)\n{\n #print $i . \"\\n\";\n #print $nums[$i] . \"\\n\";\n $head = substr($nums[0], 0, $i + 1);\n #print $head . \"\\n\";\n $isok = 1;\n for ($j = 1; $j < $n; $j++)\n {\n #print \"tmp: \" . substr($nums[j], 0, $i + 1) . \"\\n\";\n if (substr($nums[$j], 0, $i + 1) ne $head)\n {\n $isok = 0;\n last;\n }\n }\n\n if ($isok)\n {\n print $i + 1;\n exit;\n }\n elsif (!$isok && $i == 0)\n {\n print 0;\n exit;\n }\n}"}, {"source_code": "my $c = <>;\n\nmy @num = ();\nfor ( my $i = 0; $i < $c; $i++) {\n push @num, [split(//o, <>)];\n}\n\nif ($c == 1 ) {\n print @{$num[0]};\n exit;\n}\nmy $ok = 1;\nmy $len = 0;\n\nwhile ( $ok ) {\n my $c = $num[0][$len];\n for (my $i = 1; $i < @num; $i++) {\n if ($num[$i][$len] != $c) {\n $ok = 0;\n last;\n }\n }\n $len++;\n}\n\nprint $len-1;"}, {"source_code": "my $n = ;\nmy @a;\nfor (my $i = 0; $i < $n; $i++) {\n my $q = ;\n push(@a, $q);\n}\n\nmy $l = length $a[0];\nmy $ans = 0;\n\nfor (my $i = $l; $i > 0; $i--) {\n my $substr = substr $a[0], 0, $i;\n my $flag = 1;\n for my $t (@a) {\n if (!($t =~ /^$substr/)) {\n $flag = 0;\n last;\n }\n }\n if ($flag) {\n $ans = $i;\n last;\n }\n}\n\nprint \"$ans\\n\";"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\n######z1\n\nmy $count = ;\nmy $pref = ;\nmy $len = length($pref);\nforeach (my $i=1;$i<$count;$i++) {\n my $num = ;\n while ($pref ne substr($num, 0, $len)) {\n $len--;\n $pref = substr($pref, 0, $len);\n }\n}\nprint $len.\"\\n\";\n1;"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy @phones;\nmy $n;\nsub isvalid{\n my $start = shift;\n my $len = shift;\n my $cmp = substr $phones[0], $start, $len-$start;\n for my $i(1..$n-1){\n return 0 if $cmp ne substr $phones[$i], $start, $len-$start;\n }\n return 1;\n}\n#==================================\n\n$n = <>;\nfor(1..$n){ \n chomp (my $val = <>);\n push @phones, $val;\n}\n\nmy ($low, $high) = (0,40000);\nwhile($low < $high){\n my $mid = int(($low + $high + 1) / 2);\n if(isvalid($low, $mid)){$low = $mid;}\n else {$high = $mid-1;}\n}\nprint $low;\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy @phones;\nmy $n;\n$n = <>;\nfor(1..$n){ \n my @val = (<> =~ /\\w/g);\n push @phones, \\@val;\n}\nfor my $j(0..40000){\n my $char = $phones[0][$j];\n for my $i(0..$n-1){\n if($char ne $phones[$i][$j]){\n print $j;\n exit 0;\n }\n }\n}\n"}, {"source_code": "my @input;\nwhile(<>) {chomp; push(@input, $_);}\nmy @list = map{ {}; } (1..20);\nshift(@input);\nmy ($l, $c, $i);\nmy $regexp = qr//o;\nfor $l (@input) {\n\t$i = 0;\n foreach $c (split($regexp, $l)) {\n $list[$i++]{$c} = 1;\n }\n}\n$i = 0;\n$i++ while( scalar(keys(%{$list[$i]}) == 1 ) );\nprint $i;"}, {"source_code": "@_=<>;\nshift @_;\nchomp @_;\n$a = shift @_;\nfor $j(1..length $_[0]){\n $i=0;\n for (@_){\n $a eq $_ or $i=1;\n chop;\n }\n chop $a;\n $i or last\n }\nprint 1 - $i + length $a"}], "negative_code": [{"source_code": "$k = <>;\n$p = <>;\nwhile (<>) {\n for ($i = length; $i > 0; $i--) {\n if (substr($_, 0, $i) eq substr($p, 0, $i)) {\n $p = substr($_, 0, $i);\n last;\n }\n }\n}\nprint length $p, \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\nchomp($n = );\n$nums;\nfor ($i = 0; $i < $n; $i++)\n{\n chomp($num = );\n push(@nums, $num);\n #print $num . \"\\n\";\n}\n\n$numlen = length $nums[0];\n#$res = 0;\nfor ($i = $numlen - 1; $i >= 0; $i--)\n{\n #print $i . \"\\n\";\n #print $nums[$i] . \"\\n\";\n $head = substr($nums[0], 0, $i + 1);\n #print $head . \"\\n\";\n #$isok = 1;\n for ($j = 1; $j < $n; $j++)\n {\n #print \"tmp: \" . substr($nums[j], 0, $i + 1) . \"\\n\";\n if (substr($nums[$j], 0, $i + 1) ne $head)\n {\n last;\n }\n\n if ($j == $n - 1)\n {\n print $i + 1;\n exit;\n }\n }\n}"}], "src_uid": "081b35620952bd32ce6d8ddc756e5c9d"} {"nl": {"description": "Find an n\u2009\u00d7\u2009n 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\u2009\u2264\u2009n\u2009\u2264\u200949).", "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": "$n = <>; \n$o = -1; sub o { $o+=2 };\n$e = 0; sub e { $e+=2 };\npush @a, 2*$_-1 for 1..$n/2; @a = (@a, $n, reverse @a);\nfor $i (@a) {\n\t@v = ();\n\tpush @v, o() for 1..$i;\n\tuntil (@v == $n) {\n\t\tunshift @v, e();\n\t\tpush @v, e();\n\t}\n\tprint join(\" \", @v), \"\\n\";\n}\n"}], "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$$$) \u2014 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 (1..<>) {\r\n chomp(my $n = <>);\r\n chomp(my $in = <>);\r\n my @a = split //, $in;\r\n scalar(@a) == 1 and print \"YES\\n\" and next;\r\n scalar(@a) > 2 and print \"NO\\n\" and next;\r\n my $a = shift @a;\r\n my $b = shift @a;\r\n $a != $b and print \"YES\\n\";\r\n $a == $b and print \"NO\\n\";\r\n}"}, {"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$_ = <>, chomp;\r\n\t\r\n\tprint m/^(1|0|10|01)$/ ? 'YES' : 'NO';\r\n\t}"}], "negative_code": [], "src_uid": "354658f565e265c2a1ce37355d6466e1"} {"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)\u00a0\u2014 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$$$)\u00a0\u2014 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": "#!/usr/bin/perl\r\n\r\nmy ($t) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\nwhile( $t -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/o,);\r\n my @A = map { $_ - 0 } split(/\\s+/o,);\r\n my @sa = sort { $a <=> $b } @A;\r\n my $res = 0;\r\n my $it = 0;\r\n while(1){\r\n last if &chk($n,\\@A,\\@sa);\r\n for(my $i=($it % 2);$i<$n-1;$i+=2){\r\n if( $A[$i] > $A[$i+1] ){\r\n my $tmp = $A[$i]; $A[$i] = $A[$i+1]; $A[$i+1] = $tmp;\r\n }\r\n }\r\n $it++;\r\n }\r\n print \"$it\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub chk {\r\n my ($n,$x,$y) = @_;\r\n my $rc = 1;\r\n for(my $i=0;$i<$n;$i++){\r\n if( $x->[$i] != $y->[$i] ){ $rc = undef; last; }\r\n }\r\n return $rc;\r\n}\r\n"}], "negative_code": [], "src_uid": "d4bcc53b470e4beaa078d5ce3785c6cb"} {"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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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\u00a0\u2014 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": "for(1..<>){($n,$k)=split/\\s/,<>;@a=split/\\s/,<>;$q=0;for(1..$n-2){$q++if$k>1?@a[$_]>@a[$_-1]+@a[$_+1]:$_%2}print$q,\" \"}"}, {"source_code": "for(1..<>){($n,$k)=split/\\s/,<>;@a=split/\\s/,<>;$q=0;for(1..$n-2){$q++if$k>1?@a[$_]>@a[$_-1]+@a[$_+1]:$_%2}print$q,\" \"}"}, {"source_code": "for(1..<>){($n,$k)=split/\\s/,<>;@a=split/\\s/,<>;$q=0;for(1..$n-2){$q++if$k>1?@a[$_]>@a[$_-1]+@a[$_+1]:$_%2}print$q,\" \"}"}], "negative_code": [{"source_code": "for(1..<>){<>;$s=<>;$s=~s/\\s.*//;print$s}"}, {"source_code": "for(1..<>){($n,$k)=split/\\s/,<>;@a=split/\\s/,<>;$q=0;for(1..$n-1){$q++if@a[$_]>@a[$_-1]+@a[$_+1]||$k==1&&$_%2}print$q,\" \"}"}, {"source_code": "for(1..<>){($n,$k)=split/\\s/,<>;@a=split/\\s/,<>;$q=0;for(0..$n-1){$q++if@a[$_]>@a[$_-1]+@a[$_+1]||($k==1&&$_%2)}print$q,\" \"}\r\n"}, {"source_code": "for(1..<>){($n,$k)=split/\\s/,<>;@a=split/\\s/,<>;$q=0;for(0..$n-1){$q++if@a[$_]>@a[$_-1]+@a[$_+1]||$k==1&&$_%2}print$q,\" \"}"}, {"source_code": "for(1..<>){($n,$k)=split/\\s/,<>;@a=split/\\s/,<>;$q=0;for(0..$n-1){$q++if@a[$_]>@a[$_-1]+@a[$_+1]||!$k&&$_%2}print$q,\" \"}"}, {"source_code": "use POSIX;for(1..<>){($n,$k)=split/\\s/,<>;@a=split/\\s/,<>;$q=0;for(0..$n-1){$q++if@a[$_]>@a[$_-1]+@a[$_+1]}print$k-1?$q:-1&($n-2)/2+$n%2,\" \"}"}, {"source_code": "use POSIX;for(1..<>){($n,$k)=split/\\s/,<>;@a=split/\\s/,<>;$q=0;for(0..$n-1){$q++if@a[$_]>@a[$_-1]+@a[$_+1]}print$k-1?$q:ceil($n/2-1),\" \"}"}], "src_uid": "4d5d20fd586ddbea2adeab104a6c2aec"} {"nl": {"description": "After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant\u2019s sentences, Yash determined a new cipher technique.For a given sentence, the cipher is processed as: Convert all letters of the sentence to lowercase. Reverse each of the words of the sentence individually. Remove all the spaces in the sentence. For example, when this cipher is applied to the sentenceKira is childish and he hates losingthe resulting string isariksihsidlihcdnaehsetahgnisolNow Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.", "input_spec": "The first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u200910\u2009000)\u00a0\u2014 the length of the ciphered text. The second line consists of n lowercase English letters\u00a0\u2014 the ciphered text t. The third line contains a single integer m (1\u2009\u2264\u2009m\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word wi (|wi|\u2009\u2264\u20091\u2009000) consisting of uppercase and lowercase English letters only. It's guaranteed that the total length of all words doesn't exceed 1\u2009000\u2009000.", "output_spec": "Print one line \u2014 the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.", "sample_inputs": ["30\nariksihsidlihcdnaehsetahgnisol\n10\nKira\nhates\nis\nhe\nlosing\ndeath\nchildish\nL\nand\nNote", "12\niherehtolleh\n5\nHI\nHo\nthere\nHeLLo\nhello"], "sample_outputs": ["Kira is childish and he hates losing", "HI there HeLLo"], "notes": "NoteIn sample case 2 there may be multiple accepted outputs, \"HI there HeLLo\" and \"HI there hello\" you may output any of them. "}, "positive_code": [{"source_code": "chomp( (undef, $_, undef, @words) = <> );\n\n%restore_case = map { lc, $_ } @words;\n\n$list = join '|', map { scalar reverse lc } @words;\n\nm/^ (?: ($list | $ | (?{ pop @submatches }) (?!) ) (?{ push @submatches, $^N }) )+ $/x;\n\npop @submatches;\n\nprint join ' ', map { $restore_case{ scalar reverse } } @submatches"}, {"source_code": "# Eily+\n\nchomp( (undef, $_, undef, @words) = <> );\n\n%restore_case = map { lc reverse, $_ } @words; \n\n$list = join '|', keys %restore_case;\n\nm/^ (?: ($list | $ | (?{ pop @submatches }) (?!) ) (?{ push @submatches, $^N }) )+ $ (?{ pop @submatches })/x;\n\nprint join ' ', map { $restore_case{ $_ } } @submatches"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t@w = @ww = @ans = @ANS = ();\n\t\n\tfor (1 .. <>){\n\t\tpush @w, scalar <>;\n\t\t}\n\tchomp @w;\n\t\n\t%h = map { (scalar reverse lc) => 1 } @w;\n\t%H = map { lc, $_ } @w;\n\t\n\t$re = join '|', keys %h;\n\t\n\t/^(?:($re)(?{push @ww, $^N}))+$/;\n\t\n\tfor $st (@ww){\n\t\ts/^$st/ (push @ans, scalar reverse $&) , ''/e;\n\t\t}\n\n\tfor (@ans){\n\t\tpush @ANS, $H{ $_ };\n\t\t}\n\t\t\n\tprint \"@ANS\";\n\t}"}, {"source_code": "chomp( (undef, $_, undef, @words) = <> );\n\n%restore_case = map { lc, $_ } @words;\n\n$list = join '|', map { scalar reverse lc } @words;\n\nm/^ (?: ($list) (?{ push @sub_matches, $^N }) )+ $/x;\n\nfor $sub_match (@sub_matches){\n\ts/^$sub_match// and push @ans, $restore_case{ reverse $& }\n\t}\n\t\nprint \"@ans\""}, {"source_code": "chomp( (undef, $_, undef, @words) = <> );\n\n%restore_case = map { lc, $_ } @words;\n\n$list = join '|', map { scalar reverse lc } sort { length $a <=> length $b } @words;\n\nm/^ (?: ($list) (?{ unshift @sub_matches, $^N }) )+ $/x;\n\nfor $sub_match (@sub_matches){\n\ts/$sub_match$// and unshift @ans, $restore_case{ reverse $& }\n\t}\n\t\nprint \"[$_]\" if $_;\nprint \"@ans\""}, {"source_code": "chomp( (undef, $_, undef, @words) = <> );\n\n%restore_case = map { lc, $_ } @words;\n\n$list = join '|', map { scalar reverse lc } @words;\n\nm/^ (?: ($list) (?{ unshift @sub_matches, $^N }) )+ $/x;\n\nfor $sub_match (@sub_matches){\n\ts/$sub_match$// and unshift @ans, $restore_case{ reverse $& }\n\t}\n\t\nprint \"[$_]\" if $_;\nprint \"@ans\""}, {"source_code": "chomp( (undef, $_, undef, @words) = <> );\n\n%restore_case = map { lc, $_ } @words;\n\n$list = join '|', map { scalar reverse lc } @words;\n\nm/^ (?: ($list) (?{ push @sub_matches, $^N }) )+ $/x;\n\nfor $sub_match (reverse @sub_matches){\n\ts/$sub_match$// and unshift @ans, $restore_case{ reverse $& }\n\t}\n\t\nprint \"@ans\""}, {"source_code": "chomp( (undef, $_, undef, @words) = <> );\n\n%restore_case = map { lc, $_ } @words;\n\n$list = join '|', map { scalar reverse lc } @words;\n\nm/^ (?: ($list) (?{ push @sub_matches, $^N }) )+ $/x;\n\nfor $sub_match (@sub_matches){\n\ts/^$sub_match// and push @ans, $restore_case{ reverse $& }\n\t}\n\t\nprint \"@ans\";"}], "src_uid": "5d3fc6a6c4f53d53c38ab4387282a55c"} {"nl": {"description": "You are given a positive decimal number x.Your task is to convert it to the \"simple exponential notation\".Let x\u2009=\u2009a\u00b710b, where 1\u2009\u2264\u2009a\u2009<\u200910, 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 \u2014 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": "chomp($_ = <>); \n($a, $b) = /\\./? ($`, $'): $_;\n$a =~ s/^0+//; $b =~ s/0+$//;\nif ($a or $b) {\n\tif (length $a) {\n\t\t$a =~ /^./; $a = $&; $b = $'.$b; $e = length $';\n\t} else {\n\t\t$b =~ s/^0+// and $e = -length $&;\n\t\t$b =~ /^./; $a = $&; $b = $'; $e--;\n\t}\n\t$e = $e? \"E$e\": \"\";\n\t$b =~ s/0+$//; $b = $b? \".$b\": \"\";\n\t$r = \"$a$b$e\";\n} else {\n\t$r = \"0\";\n}\nprint $r;\n"}, {"source_code": "$_ = <>;\nchomp;\ns/^0+//;\n$E = /\\./g ? ~- pos : length;\ny/.//d;\n/[^0]/g;\n$E -= pos;\ns/^0+//;\ns/0+$//;\ns/\\B/./;\n$_ .= \"E$E\" if $E;\n$_ ||= 0;\nprint"}, {"source_code": "$_ = <>,\nchomp,\ns/^0+//,\ns/\\./ $i = pos, '' /e,\n$i //= length,\n/[1-9]/g,\n$j = pos,\ns/\\.//,\ns/^0+//,\ns/0+$//,\ns/\\B/./,\n$_ .= 'E' . ($i - $j),\ns/E0//,\n$_ ||= 0,\nprint,"}, {"source_code": "while(<>){\n\tchomp;\n\ts/^0+//;\n\t/\\./ or s/$/./;\n\t$i = index $_, '.';\n\t$j = index s/[1-9]/A/r, 'A';\n\ts/\\.//;\n\ts/^0+//;\n\ts/0+$//;\n\ts/.\\K/./;\n\t$_ .= 'E' . ($i - $j - ($i > $j));\n\ts/\\.E/E/;\n\ts/E0//;\n\t$_ ||= 0;\n\tprint;\n\t}"}], "negative_code": [{"source_code": "chomp($_ = <>); s/^0+|0+$//g;\n($a, $b) = /\\./? ($`, $'): $_;\nif ($a or $b) {\n\tif (length $a) {\n\t\t$a =~ /^./; $a = $&; $b = $'.$b; $e = length $';\n\t} else {\n\t\t$b =~ s/^0+// and $e = -length $&;\n\t\t$b =~ /^./; $a = $&; $b = $'; $e--;\n\t}\n\t$e = $e? \"E$e\": \"\";\n\t$b =~ s/0+$//; $b = $b? \".$b\": \"\";\n\t$r = \"$a$b$e\";\n} else {\n\t$r = \"0\";\n}\nprint $r;\n"}], "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\u2009\u2264\u2009n\u2009\u2264\u200948, . Then follow m lines, each contains a pair of integers ai,\u2009bi (1\u2009\u2264\u2009ai\u2009<\u2009bi\u2009\u2264\u2009n) \u2014 the pair ai,\u2009bi 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,\u2009bi 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\u2009\u2264\u2009xi,\u2009yi,\u2009zi\u2009\u2264\u2009n) \u2014 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": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nchomp($_ = <>);\nmy ($n, $m) = split;\nmy @e = ();\npush @e, [] for (1 .. $n);\nfor (1 .. $m) {\n chomp($_ = <>);\n my ($u, $v) = split;\n push @{$e[$u - 1]}, $v - 1;\n push @{$e[$v - 1]}, $u - 1;\n}\n\nmy @one = ();\nmy @used = (0) x $n;\nfor my $i (0 .. $n - 1) {\n unless (@{$e[$i]}) {\n $used[$i] = 1;\n push @one, $i;\n }\n}\n\nmy @res = ();\nmy $fail = 0;\nfor my $i (0 .. $n - 1) {\n next if $used[$i];\n\n my @cur = ($i);\n my @q = ($i);\n $used[$i] = 1;\n while (@q) {\n my $t = shift @q;\n for my $j (@{$e[$t]}) {\n if (!$used[$j]) {\n $used[$j] = 1;\n push @q, $j;\n push @cur, $j;\n }\n }\n }\n if (@cur > 3 || (@cur == 2 && !@one)) {\n $fail = 1;\n last;\n } elsif (@cur == 2) {\n push @cur, shift @one;\n }\n push @res, [@cur];\n}\nunless ($fail) {\n while (@one) {\n push @res, [@one[0, 1, 2]];\n @one = @one[3 .. $#one];\n }\n}\nif ($fail) {\n print \"-1\\n\";\n} else {\n for (@res) {\n map { ++$_ } @$_;\n print \"@$_\\n\";\n }\n}\n"}], "negative_code": [], "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\u2009+\u20091, that is, the prices of the jewelry are 2,\u20093,\u20094,\u2009... n\u2009+\u20091.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\u2009\u2264\u2009n\u2009\u2264\u2009100000)\u00a0\u2014 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": "$n = <> + 1;\nfor ($i = 2; $i <= $n; $i++) {\n\tif ( !$a[$i] ) {\n\t\t$a[$i] = 1;\n\t\tfor ($j = $i * $i; $j <= $n; $j += $i) {\n\t\t\t$a[$j] = 2;\n\t\t}\n\t}\n}\nsplice @a, 0, 2;\n$k = $n < 4? 1: 2;\nprint \"$k\\n@a\";\n"}], "negative_code": [], "src_uid": "a99f5e08f3f560488ff979a3e9746e7f"} {"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$$$) \u2014 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$$$) \u2014 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$$$) \u2014 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\n\r\nwhile(<>){\r\n\t@_ = split ' ', <>;\r\n\tprint <> =~ s/()/ chr 130 + shift @_ /ger \r\n\t\t\t =~ m/(.)(\\w).*\\1(?!\\2)/ ? \"NO\\n\" : \"YES\\n\"\r\n\t}"}, {"source_code": "<>;\r\n\r\nwhile(<>){\r\n\t@_ = split ' ', <>;\r\n\tprint <> =~ s/(?=.)/ chr 40 + shift @_ /ger \r\n\t\t\t =~ m/(.)([a-z]).*\\1(?!\\2)/ ? \"NO\\n\" : \"YES\\n\"\r\n }"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t$_ = <>, chomp;\n\t\n\tmy %h;\n\tmy $f = 0;\n\t\n\twhile( @_ ){\n\t\tmy $i = pop @_;\n\t\tmy $j = chop;\n\t\t\n\t\tif( exists $h{ $i } ){\n\t\t\tif( $h{ $i } ne $j ){\n\t\t\t\t$f = 1;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\telse{\n\t\t\t$h{ $i } = $j;\n\t\t\t}\n\t\t}\n\t\n\tprint $f ? 'NO' : 'YES';\n\t}"}], "negative_code": [{"source_code": "<>;\r\n\r\nwhile(<>){\r\n\t@_ = split ' ', <>;\r\n\tprint <> =~ s// chr 40 + shift @_ /ger \r\n\t\t\t =~ m/(.)([a-z]).*\\1(?!\\2)/ ? \"NO\\n\" : \"YES\\n\"\r\n\t}"}], "src_uid": "485d5984e34a479f2c074a305ae999ae"} {"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 \u2014 it's a unique integer from 1 to 106. Thus, the system logs events of two forms: \"+ ri\" \u2014 the reader with registration number ri entered the room; \"- ri\" \u2014 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\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 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 \u2014 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": "<>;\n\t($s, $r) = split,\n\t\t( $c += /-/? ( $h{$r} --||++ $m, -1 <=> $h{$r} )\n\t\t\t: ( $h{$r} = 1 )\n\t\t) > $m and $m = $c\n\tfor <>;\n\t\nprint 0 + $m\n"}, {"source_code": "<>;\n\t($s, $r) = split,\n\t\t( $c += /-/? ( $h{$r} --||++ $m, -1 <=> $h{$r} )\n\t\t\t: ( $h{$r} = 1 )\n\t\t) > $m and $m = $c\n\tfor <>;\n\t\nprint 0 + $m\n"}, {"source_code": "<>;\n\t($s, $r) = split,\n\t\t( $c += /-/? ( $h{$r} --||++ $m, -1 <=> $h{$r} )\n\t\t\t: ( $h{$r} = 1 )\n\t\t) > $m and $m = $c\n\tfor <>;\n\t\nprint 0 + $m"}, {"source_code": "<>;\n\t($s, $r) = split,\n\t\t( $c += /-/? ( $h{$r} --||++ $m, -1 <=> $h{$r} )\n\t\t\t: ( $h{$r} = 1 )\n\t\t) > $m and $m = $c\n\tfor <>;\n\t\nprint 0 + $m\n"}, {"source_code": "<>;\n\t($s, $r) = split,\n\t\t( $c += /-/? ( $h{$r} --||++ $m, -1 <=> $h{$r} )\n\t\t\t: ( $h{$r} = 1 )\n\t\t) > $m and $m = $c\n\tfor <>;\n\t\nprint 0 + $m\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n@c = (0) x (10**6+5);\nchomp($n = <>);\npush @a, [split / /, <>] foreach (0 .. $n-1);\n$tot = 0;\nforeach (0 .. $n-1) {\n\t$id = $a[$_][1];\n\tif ($a[$_][0] eq '+') {\n\t\t$c[$id] = 1;\n\t} elsif ($c[$id]) {\n\t\t$c[$id] = 0;\n\t} else {\n\t\t++$tot;\n\t}\n}\n$ans = $tot;\nforeach (0 .. $n-1) {\n\tif ($a[$_][0] eq '+') {\n\t\t++$tot;\n\t\t$ans = $ans>$tot ? $ans:$tot;\n\t} else {\n\t\t--$tot;\n\t}\n}\nsay $ans;"}, {"source_code": "<>;\n\t($s, $r) = split,\n\t\t( $c += /-/? ( $h{$r} --||++ $m, -1 <=> $h{$r} )\n\t\t\t: ( $h{$r} = 1 )\n\t\t) > $m and $m = $c\n\tfor <>;\n\t\nprint 0 + $m\n"}, {"source_code": "<>;\n\t($s, $r) = split,\n\t\t( $c += /-/? ( $h{$r} --||++ $m, -1 <=> $h{$r} )\n\t\t\t: ( $h{$r} = 1 )\n\t\t) > $m and $m = $c\n\tfor <>;\n\t\nprint 0 + $m\n"}, {"source_code": "<>;\n\t($s, $r) = split,\n\t\t( $c += /-/? ( $h{$r} --||++ $m, -1 <=> $h{$r} )\n\t\t\t: ( $h{$r} = 1 )\n\t\t) > $m and $m = $c\n\tfor <>;\n\t\nprint 0 + $m\n"}, {"source_code": "$\\ = $/;\n\n<>;\n$c = 0;\n$max = 0;\nwhile(<>){\n\tchomp;\n\t($n, $m) = split;\n\t$n eq '+' and ++ $h{$m} and ++ $c; # and $c > $max and $max = $c;\n\t$n eq '-' and do {\n\t\t$h{$m} // $max ++;\n\t\tdefined $h{$m} and $c --;\n\t\t$h{$m} and delete $h{$m};\n\t};\n\t$c > $max and $max = $c;\n#\tprint $_,' ',$max;\n#\tprint qw(NO YES)[]\n\t}\nprint $max;"}, {"source_code": "<>;\n\t($s, $r) = split,\n\t\t( $c += /-/? ( $h{$r} --||++ $m, -1 <=> $h{$r} )\n\t\t\t: ( $h{$r} = 1 )\n\t\t) > $m and $m = $c\n\tfor <>;\n\t\nprint 0 + $m"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n@c = (0) x (10**6+5);\nchomp($n = <>);\npush @a, [split / /, <>] foreach (0 .. $n-1);\n$tot = 0;\nforeach (0 .. $n-1) {\n\t$id = $a[$_][1];\n\tif ($a[$_][0] eq '+') {\n\t\t$c[$id] = 1;\n\t} elsif ($c[$id]) {\n\t\t$c[$id] = 0;\n\t} else {\n\t\t++$tot;\n\t}\n}\n$ans = $tot;\nforeach (0 .. $n-1) {\n\tif ($a[$_][0] eq '+') {\n\t\t++$tot;\n\t\t$ans = $ans>$tot ? $ans:$tot;\n\t} elsif ($c[$id]) {\n\t\t--$tot;\n\t}\n}\nsay $ans;"}, {"source_code": "<>;\nfor(<>){\n\t($s, $r) = split;\n\t$s eq '+' ? (++ $h{$r}, ++ $c)\n\t\t: ( $h{$r} //++ $m && -- $c or delete $h{$r} );\n\t$c > $m and $m = $c\n\t}\nprint 0 + $m"}, {"source_code": "<>;\n\t($s, $r) = split,\n\t\t( $c += $s !~/-/? $h{$r} = 1\n\t\t\t: ( $h{$r} --||++ $m, -1)\n\t\t) > $m and $m = $c\n\twhile <>;\n\t\nprint 0 + $m"}, {"source_code": "<>;\n\t($s, $r) = split,\n\t\t( $c += $s !~/-/? $h{$r} = 1\n\t\t\t: ( $h{$r} --||++ $m xor -1)\n\t\t) > $m and $m = $c\n\tfor <>;\n\t\nprint 0 + $m"}, {"source_code": "<>;\nfor(<>){\n\t($s, $r) = split;\n\t$s eq '+' ? (++ $h{$r}, ++ $c)\n\t\t: $h{$r} //++ $m && -- $c or delete $h{$r};\n\t$c > $m and $m = $c\n\t}\nprint 0 + $m"}], "src_uid": "6cfd3b0a403212ec68bac1667bce9ef1"} {"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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009104) \u2014 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\u2009>\u20090) followed by k integers vi,\u20091,\u2009vi,\u20092,\u2009...,\u2009vi,\u2009k. If vi,\u2009j is negative, it means that Rick from universe number \u2009-\u2009vi,\u2009j has joined this group and otherwise it means that Morty from universe number vi,\u2009j 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": "<>; $r = 1; for (<>) {\n\t($k, @v) = split;\n\tmy %h; $h{0 + $_} = 1 for @v;\n\t$r &&= grep $h{-$_}, @v;\n}\nprint $r? \"NO\": \"YES\";"}], "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$$$) \u2014 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$$$) \u2014 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$$$) \u2014 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 \u2014 $$$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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\tmy $str = <>;\n\tchomp $str;\n\t\n\tmy @p = split ' ', <>;\n\t\n\tpush @p, $n;\n\t\n\tmy @A = ( 0 ) x $n;\n\t\n\tfor( @p ){\n\t\t$A[ $_ - 1 ] ++;\n\t\t}\n\t\n\t$debug and print \"A:[@A]\";\n\t\n\tmy $add = 0;\n\t\n\tfor my $i ( reverse 0 .. @A - 1 ){\n\t\t$A[ $i ] += $add;\n\t\t$add = $A[ $i ];\n\t\t}\n\t\n\t$debug and print \"A:[@A]\";\n\t\n\tmy %h;\n\t\n\twhile( @A ){\n\t\t$str =~ /./g;\n\t\t$h{ $& } += shift @A;\n\t\t}\n\t\n\tprint join ' ', map { $h{ $_ } // 0 } 'a' .. 'z';\n\t}"}], "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\u2009-\u20091). 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\u2009+\u20091)\u00a0mod\u00a0n either with an elbow or with a nod (x\u00a0mod\u00a0y is the remainder after dividing x by y); if j\u2009\u2265\u20094 and the players who had turns number j\u2009-\u20091, j\u2009-\u20092, j\u2009-\u20093, 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\u2009+\u20091)\u00a0mod\u00a0n. 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\u2009\u2264\u2009n\u2009\u2264\u20092000) \u2014 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 \u2014 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 \u2014 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=<>;$s=<>;$t+=substr($s,$i-3,3)=~/aaa|bbb/ while(($i+=$n)+1;$s=<>;$t+=substr($s,$i-3,3)=~/aaa|bbb/ while(($i+=$n)+1;$s=<>;$t+=substr($s,$i-3,3)=~/aaa|bbb/ while(($i+=$n)+1;$s=<>;$t+=substr($s,$i-3,3)=~/aaa|bbb/ while(($i+=$n)+1;$s=<>;$t+=substr($s,$i-3,3)=~/aaa|bbb/ while(($i+=$n)+1;$s=<>;$t+=substr($s,$i-3,3)=~/aaa|bbb/ while(($i+=$n)+1;@a=split'',<>;$i+=$n and$t+=(join'',@a[$i-3..$i-1])=~/aaa|bbb/?1:0 while($i+$n+1<@a);print$t||0"}, {"source_code": "$n=<>;$s=<>;$t+=substr($s,$i-3,3)=~/aaa|bbb/ while(($i+=$n)+1;\nchomp $n;\n\nmy $moves_s = <>;\nchomp $moves_s;\nmy @moves = split(//, $moves_s);\n\nmy $res = 0;\n\nfor (my $i = 0; $i <= $#moves; $i += $n) {\n if ($i >= 4) {\n if ($moves[$i-3] eq $moves[$i-2]\n && $moves[$i-2] eq $moves[$i-1]) {\n $res++;\n }\n }\n}\n\nprint \"$res\\n\";\n"}, {"source_code": "$n=<>;$s=<>;$t+=substr($s,$i-3,3)=~/aaa|bbb/ while(($i+=$n)+1;$s=<>;$t+=substr($s,$i-3,3)=~/aaa|bbb/ while(($i+=$n)+1) {\n chomp ($n);\n $_=<>;\n chomp;\n s/.//;\n $i=0;\n while (s/.{$n}//){\n $a=$&;\n $a=~s/.$//;\n # print \" $a\\n\";\n $a=~/aaa$|bbb$/ and $i++\n }\n print \"$i\\n\"\n }"}, {"source_code": "$n=<>;$s=<>;$t+=substr($s,$i-3,3)=~/aaa|bbb/ while(($i+=$n)+1;@a=split'',<>;$i+=$n and$t+=(join'',@a[$i-3..$i-1])=~/aaa|bbb/?1:0 while($i+$n+1<@a);print$t"}, {"source_code": "while ($n=<>) {\n chomp ($n);\n $_=<>;\n chomp;\n s/.//;\n $i=0;\n while (s/.{$n}//){\n $a=$&;\n $a=~s/.$//;\n # print \" $a\\n\";\n $a eq \"aaa\" || $a eq \"bbb\" and $i++\n }\n print \"$i\\n\"\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$$$) \u2014 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$$$) \u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n ( my $a = ) =~ s/[\\x00-\\x20]+$//ios;\r\n ( my $b = ) =~ s/[\\x00-\\x20]+$//ios;\r\n my $j = $n;\r\n for(my $i=$n-1;$i>=0;$i--){\r\n if( substr($a,$i,1) eq substr($b,$i,1) ){\r\n $j = $i;\r\n } else {\r\n last;\r\n }\r\n }\r\n $j ++ if( $j < $n-1 and $j % 2 == 1 );\r\n if( $j % 2 == 1 ){\r\n print \"NO\\n\"; next; \r\n }\r\n my $sa = 0; my $sb = 0;\r\n my @ca = (); $#ca = $j;\r\n my @cb = (); $#cb = $j;\r\n for(my $i=0;$i<$j;$i+=2){\r\n my $a1 = substr($a,$i,1);\r\n my $a2 = substr($a,$i+1,1);\r\n my $b1 = substr($b,$i,1);\r\n my $b2 = substr($b,$i+1,1);\r\n my $v1 = ( $a1 - 0 + ($a2 - 0 ) );\r\n my $v2 = ( $b1 - 0 + ($b2 - 0 ) );\r\n if( ( $v1 + $v2 ) % 2 == 1 ){\r\n $sa = 0; $sb = -1; last;\r\n }\r\n $sa += $v1;\r\n $sb += $v2;\r\n $ca[$i] = $sa;\r\n $cb[$i] = $sb;\r\n }\r\n if( $sa != $sb ){\r\n print \"NO\\n\"; next;\r\n }\r\n \r\n my $rev = 0;\r\n for(my $i=$j-2;$i>=0;$i-=2){\r\n my $aa = substr($a,$i,2);\r\n my $bb = substr($b,$i,2);\r\n #print \"i=$i : $aa $bb\\n\";\r\n $aa =~ tr/01/10/ if( $rev==1 );\r\n unless( $aa eq $bb ){\r\n if( ( $rev==1 ? ($i+2) - $ca[$i] : $ca[$i] ) != $cb[$i] ){\r\n $rev = -1; last;\r\n }\r\n $rev ^= 1;\r\n }\r\n }\r\n if( $rev < 0 ){\r\n print \"NO\\n\";\r\n } else {\r\n print \"YES\\n\";\r\n }\r\n \r\n}\r\n\r\nexit(0);\r\n\r\n\r\n#!/usr/bin/perl\r\nmy ($n,$m) = map {$_ - 0} split(/\\s+/o,);\r\n\r\nmy @tr = ();\r\n$#tr = $n-1;\r\nmy @par = ();\r\n$#par = $n-1;\r\n\r\nfor(my $i=0;$i<$n;$i++){\r\n $par[$i] = $i;\r\n}\r\n\r\nfor(my $i=0;$i<$m;$i++){\r\n my ($x,$y,$z) = map {$_ - 0} split(/\\s+/o,);\r\n unite($x-1,$y-1);\r\n}\r\n\r\nmy %dum = ();\r\nfor(my $i=0;$i<$n;$i++){\r\n $dum{&root($i)} = 1;\r\n}\r\nmy @d2 = keys %dum;\r\nmy $res = 1 + $#d2;\r\nprint \"$res\\n\";\r\n\r\nexit(0);\r\n\r\n\r\n###\r\n\r\n#!/usr/bin/perl\r\nuse Data::Dumper;\r\nmy ($n) = map {$_ - 0} split(/\\s+/o,);\r\n\r\nmy $f = &factr($n);\r\n\r\nmy @facts = keys %$f;\r\n\r\nmy %f0 = ();\r\n\r\nmy %oki = ();\r\n\r\nwhile(1){\r\n my $gosei = 1;\r\n foreach my $f1 (@facts){\r\n $gosei *= ( $f1 ** ( $f0{$f1} - 0 ) );\r\n }\r\n if( $gosei >= 3 ){\r\n# print \"gosei = $gosei\\n\";\r\n my $syo = $n / $gosei;\r\n if( $syo <= $gosei - 2 ){\r\n my $g1 = $gosei - 1;\r\n $oki{$g1} = 1;\r\n }\r\n }\r\n my $cu = 0;\r\n my $carry = 1;\r\n while($carry > 0){\r\n $f0{$facts[$cu]} += 1;\r\n $carry = 0;\r\n if( $f0{$facts[$cu]} > $f->{$facts[$cu]} ){\r\n $f0{$facts[$cu]} = 0;\r\n $carry = 1;\r\n $cu++;\r\n }\r\n last if( $cu > $#facts );\r\n }\r\n last if $cu > $#facts;\r\n}\r\nmy $res = 0;\r\nforeach my $o1 (keys %oki){\r\n $res += ( $o1 - 0);\r\n}\r\n#print &Dumper(\\%oki);\r\nprint \"$res\\n\";\r\nexit(0);\r\n\r\nsub factr {\r\n my $v = shift;\r\n my %f = ();\r\n if( $v<4 ){\r\n $f{$v}++;\r\n return \\%f;\r\n }\r\n for(my $i=1;$i*$i<=$v;$i+=2){\r\n my $ii = ($i == 1 ? 2 : $i);\r\n while($v % $ii == 0){\r\n $f{$ii}++;\r\n $v /= $ii;\r\n }\r\n }\r\n $f{$v}++ if $v>1;\r\n return \\%f;\r\n}\r\n\r\n#### mod_int + nCr nPr set\r\n\r\n#!/usr/bin/perl\r\n\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($n,$k) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\n&mk_fact($n+10);\r\n\r\n### write here \r\n\r\nexit(0);\r\n\r\n\r\nsub mod_pow { # ( x ** n ) % mod\r\n my ($x,$n) = @_;\r\n my $r = 1;\r\n while( $n ){\r\n $r = ( $r * $x ) % $mod if 1 & $n;\r\n $n >>= 1;\r\n $x = ( $x * $x ) % $mod;\r\n }\r\n return $r;\r\n}\r\n\r\nsub mk_fact { # make {global} n-size variable fact[] factinv[]\r\n my $n_max = shift;\r\n our @fact = (1); $#fact = $n_max;\r\n our @factinv = (1); $#factinv = $n_max;\r\n for(my $i=1;$i<=$n_max;$i++){\r\n $fact[$i] = ( $fact[$i-1] * $i ) % $mod;\r\n $factinv[$i] = &mod_pow($fact[$i],$mod-2);\r\n }\r\n}\r\n\r\nsub nCr { # calc nCr % mod\r\n my $n = shift; my $r = shift;\r\n return (( $fact[$n] * ( ( $factinv[$r] * $factinv[$n-$r] ) % $mod ) ) % $mod);\r\n}\r\n\r\nsub nPr { # calc nPr % mod\r\n my $n = shift; my $r = shift;\r\n return (( $fact[$n] * $factinv[$n-$r] ) % $mod );\r\n}\r\n\r\n#### union find set\r\n\r\n#!/usr/bin/perl\r\n\r\nmy ($n) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\n#### write here\r\n\r\nexit(0);\r\n\r\nsub mk_uf {\r\n my $n = shift;\r\n our @uf = (); $#uf = $n - 1;\r\n for(my $i=0;$i<$n;$i++){\r\n $uf[$i] = $i;\r\n }\r\n}\r\n\r\nsub root {\r\n my $x = shift;\r\n while( $uf[$x] != $x ){ $x = $uf[$x] = $uf[$uf[$x]]; }\r\n return $x;\r\n}\r\n\r\nsub unite {\r\n my $x = &root(scalar(shift)); my $y = &root(scalar(shift));\r\n $uf[$y] = $x;\r\n}\r\n\r\n\r\n\r\n"}], "negative_code": [], "src_uid": "ff95cd4632b2ddf8bb54981634badcae"} {"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$$$) \u2013 the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) \u2014 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}$$$) \u2014 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": "#!/usr/bin/perl -w\nuse strict;\nuse warnings;\n\nmy $n = ; chomp $n;\nmy $rB = [];\nfor my $j ( 1 .. $n ) {\n my $a = ; chomp $a;\n my $str = ; my @r = split(/\\s+/, $str);\n pop @r unless ( $#r+1 eq $a );\n $rB->[$j] = \\@r;\n}\nfor my $j ( 1 .. $n ) {\n my @q = sort { $a <=> $b } @{ $rB->[$j] };\n my $change = 1; my $flag = 0;\n while ( $change eq 1 ) {\n @q = sort { $a <=> $b } @q;\n $change = 0;\n $flag = 1 if ( $q[$#q] eq \"2048\" );\n if ( $flag eq 0 ) {\n for my $t ( 0 .. $#q - 1 ) {\n if ( $q[$t] eq \"2048\" ) {\n $flag = 1; last;\n }\n if ( $q[$t] eq $q[$t+1] ) {\n $change = 1;\n $q[$t] = 2*$q[$t];\n for my $p ( $t + 1 .. $#q - 1 ) {\n $q[$p] = $q[$p+1];\n }\n pop @q;\n if ( $q[$t] eq \"2048\" ) {\n $flag = 1; last;\n }\n last;\n }\n }\n }\n last if ( $flag eq 1 );\n }\n if ( $flag eq 1 ) { print \"YES\\n\";\n } else { print \"NO\\n\"; }\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = grep $_ <= 2048, split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap { $h{ $_ } ++ } @_;\n\t\n\tmy $ok = 0;\n\t\n\tfor my $i ( 0 .. 11 ){\n\t\t$h{ 2 ** $i } //= 0;\n\t\t$h{ 2048 } and $ok = 1;\n\t\t$h{ 2 ** ( $i + 1 ) } += ( $h{ 2 ** $i } >> 1 );\n\t\t}\n\t\n\tprint $ok ? \"YES\" : \"NO\";\n\t}"}], "negative_code": [{"source_code": "my $n = ; chomp $n;\nmy $rB = [];\nfor my $j ( 1 .. $n ) {\n my $a = ; chomp $a;\n my $str = ; my @r = split(/\\s+/, $str);\n pop @r unless ( $#r+1 eq $a );\n $rB->[$j] = \\@r;\n}\nfor my $j ( 1 .. $n ) {\n my @q = sort { $a <=> $b } @{ $rB->[$j] };\n my $change = 1; my $flag = 0;\n while ( $change eq 1 ) {\n $change = 0;\n $flag = 1 if ( $q[$#q] eq \"2048\" );\n if ( $flag eq 0 ) {\n for my $t ( 0 .. $#q - 1 ) {\n if ( $q[$t] eq \"2048\" ) {\n $flag = 1; last;\n }\n if ( $q[$t] eq $q[$t+1] ) {\n $change = 1;\n $q[$t] = 2*$q[$t];\n for my $p ( $t + 1 .. $#q - 1 ) {\n $q[$p] = $q[$p+1];\n }\n pop @q;\n if ( $q[$t] eq \"2048\" ) {\n $flag = 1; last;\n }\n last;\n }\n }\n }\n last if ( $flag eq 1 );\n }\n if ( $flag eq 1 ) { print \"YES\\n\";\n } else { print \"NO\\n\"; }\n}\n"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\nuse warnings;\n\nmy $n = ; chomp $n;\nmy $rB = [];\nfor my $j ( 1 .. $n ) {\n my $a = ; chomp $a;\n my $str = ; my @r = split(/\\s+/, $str);\n pop @r unless ( $#r+1 eq $a );\n $rB->[$j] = \\@r;\n}\nfor my $j ( 1 .. $n ) {\n my @q = sort { $a <=> $b } @{ $rB->[$j] };\n my $change = 1; my $flag = 0;\n while ( $change eq 1 ) {\n $change = 0;\n $flag = 1 if ( $q[$#q] eq \"2048\" );\n for my $t ( 0 .. $#q - 1 ) {\n if ( $q[$t] eq \"2048\" ) {\n $flag = 1; last;\n }\n if ( $q[$t] eq $q[$t+1] ) {\n $change = 1;\n $q[$t] = 2*$q[$t];\n for my $p ( $t + 1 .. $#q - 1 ) {\n $q[$p] = $q[$p+1];\n }\n pop @q;\n if ( $q[$t] eq \"2048\" ) {\n $flag = 1; last;\n }\n last;\n }\n }\n last if ( $flag eq 1 );\n }\n if ( $flag eq 1 ) { print \"YES\\n\";\n } else { print \"NO\\n\"; }\n}\n\n"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\nuse warnings;\n\nmy $n = ; chomp $n;\nmy $rA = []; my $rB = [];\nfor my $j ( 1 .. $n ) {\n $rA->[$j] = ;\n my $str = ; my @r = split(/\\s+/, $str);\n $rB->[$j] = \\@r;\n}\nfor my $j ( 1 .. $n ) {\n my @q = sort { $a <=> $b } @{ $rB->[$j] };\n my $change = 1; my $flag = 0;\n while ( $change eq 1 ) {\n $change = 0;\n $flag = 1 if ( $q[$#q] eq \"2048\" );\n for my $t ( 0 .. $#q - 1 ) {\n if ( $q[$t] eq \"2048\" ) {\n $flag = 1; last;\n }\n if ( $q[$t] eq $q[$t+1] ) {\n $change = 1;\n $q[$t] = 2*$q[$t];\n for my $p ( $t + 1 .. $#q - 1 ) {\n $q[$p] = $q[$p+1];\n }\n pop @q;\n if ( $q[$t] eq \"2048\" ) {\n $flag = 1; last;\n }\n last;\n }\n }\n last if ( $flag eq 1 );\n }\n if ( $flag eq 1 ) { print \"YES\\n\";\n } else { print \"NO\\n\"; }\n}\n"}], "src_uid": "b40059fe9cbdb0cc3b64c3e463900849"} {"nl": {"description": "Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem. You are given a complete directed graph with $$$n$$$ vertices without self-loops. In other words, you have $$$n$$$ vertices and each pair of vertices $$$u$$$ and $$$v$$$ ($$$u \\neq v$$$) has both directed edges $$$(u, v)$$$ and $$$(v, u)$$$.Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $$$(u, v)$$$ and $$$(v, u)$$$ may have different labels).You are also given an integer $$$m > 0$$$. You should find a path of length $$$m$$$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.You can visit the same vertex and the same directed edge any number of times.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 500$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \\leq n \\leq 1000$$$; $$$1 \\leq m \\leq 10^{5}$$$)\u00a0\u2014 the number of vertices in the graph and desirable length of the palindrome. Each of the next $$$n$$$ lines contains $$$n$$$ characters. The $$$j$$$-th character of the $$$i$$$-th line describes the character on the edge that is going from node $$$i$$$ to node $$$j$$$. Every character is either 'a' or 'b' if $$$i \\neq j$$$, or '*' if $$$i = j$$$, since the graph doesn't contain self-loops. It's guaranteed that the sum of $$$n$$$ over test cases doesn't exceed $$$1000$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, if it is possible to find such path, print \"YES\" and the path itself as a sequence of $$$m + 1$$$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them. Otherwise, (if there is no answer) print \"NO\".", "sample_inputs": ["5\n3 1\n*ba\nb*b\nab*\n3 3\n*ba\nb*b\nab*\n3 4\n*ba\nb*b\nab*\n4 6\n*aaa\nb*ba\nab*a\nbba*\n2 6\n*a\nb*"], "sample_outputs": ["YES\n1 2\nYES\n2 1 3 2\nYES\n1 3 1 3 1\nYES\n1 2 1 3 4 1 4\nNO"], "notes": "NoteThe graph from the first three test cases is shown below: In the first test case, the answer sequence is $$$[1,2]$$$ which means that the path is:$$$$$$1 \\xrightarrow{\\text{b}} 2$$$$$$So the string that is obtained by the given path is b.In the second test case, the answer sequence is $$$[2,1,3,2]$$$ which means that the path is:$$$$$$2 \\xrightarrow{\\text{b}} 1 \\xrightarrow{\\text{a}} 3 \\xrightarrow{\\text{b}} 2$$$$$$So the string that is obtained by the given path is bab.In the third test case, the answer sequence is $$$[1,3,1,3,1]$$$ which means that the path is:$$$$$$1 \\xrightarrow{\\text{a}} 3 \\xrightarrow{\\text{a}} 1 \\xrightarrow{\\text{a}} 3 \\xrightarrow{\\text{a}} 1$$$$$$So the string that is obtained by the given path is aaaa.The string obtained in the fourth test case is abaaba."}, "positive_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nW1:\r\nwhile(<>){\r\n\t( $n, $m, @A, $k, $p ) = split;\r\n\t\r\n\t@_ = map ~~<>, 1 .. $n;\r\n\tchomp @_;\r\n\t\r\n\tpush @A, [ split // ] for @_;\r\n\t\r\n\tfor $i ( 0 .. $n - 1 ){\r\n\t\tfor $j ( $i + 1 .. $n - 1 ){\r\n\t\t\tif( $A[ $i ][ $j ] eq $A[ $j ][ $i ] ){\r\n\t\t\t\tprint for 'YES', join ' ', \r\n\t\t\t\t\t( ( $i + 1, $j + 1 ) x $m )[ 0 .. $m ];\r\n\t\t\t\tnext W1;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\r\n\tif( $m % 2 ){\r\n\t\t$n < 4 and $p = join ' ', ( ( 1 .. $n ) x $m )[ 0 .. $m ];\r\n\t\t\r\n\t\tfor( @_ ){\r\n\t\t\t$k ++;\r\n\t\t\tnext if ! m/(.).*\\1/;\r\n\t\t\t$p = join ' ', \r\n\t\t\t\t$k, ( $+[ 1 ], $k ) x ( $m >> 1 ), $+[ 0 ];\r\n\t\t\tlast;\r\n\t\t\t}\r\n\t\t}\r\n\telse{\r\n\t\tfor( @_ ){\r\n\t\t\t$k ++;\r\n\t\t\tnext if 1\r\n\t\t\t\t&& ! m/(a).*b/\r\n\t\t\t\t&& ! m/(b).*a/\r\n\t\t\t\t;\r\n\t\t\t$m /= 2;\r\n\t\t\t$p = join ' ', ( ( map { $k, $_ } @+ ) x $m )\r\n\t\t\t\t[ 0 + $m % 2 .. 2 * $m + $m % 2 ];\r\n\t\t\tlast;\r\n\t\t\t}\r\n\t\t}\r\n\t\r\n\tprint $p ? \"YES\\n$p\" : \"NO\";\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\t@_ = map ~~<>, 1 .. $n;\n\tchomp @_;\n\t\n\t$debug and print \"n:[$n], m:[$m]\";\n\t$debug and print for @_;\n\t\n\tmy @A;\n\t\n\tfor( @_ ){\n\t\tpush @A, [ split // ];\n\t\t}\n\t\n\tmy $eq;\n\t\n\tOUTER:\n\tfor my $i ( 0 .. $n - 1 ){\n\t\tfor my $j ( $i .. $n - 1 ){\n\t\t\tnext if $i == $j;\n\t\t\tif( $A[ $i ][ $j ] eq $A[ $j ][ $i ] ){\n\t\t\t\t$eq = [ map $_ + 1, $i, $j ];\n\t\t\t\tlast OUTER;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tif( defined $eq ){\n\t\t$debug and print \"\\@{ \\$eq }:[@{ $eq }]\";\n\t\tprint 'YES';\n\t\tprint join ' ', ( ( @{ $eq } ) x $m )[ 0 .. $m ];\n\t\tnext;\n\t\t}\n\t\n\tif( $m % 2 == 1 ){\n\t\tif( grep { m/(.).*\\1/ } @_ ){\n\t\t\tfor my $i ( 0 .. @_ - 1 ){\n\t\t\t\tnext if $_[ $i ] !~ m/(.).*\\1/;\n\t\t\t\tprint 'YES';\n\t\t\t\tprint join ' ', $i + 1, ( $+[ 1 ], $i + 1 ) x ( $m >> 1 ), $+[ 0 ];\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\telsif( $n <= 3 ){\n\t\t\tprint 'YES';\n\t\t\tprint join ' ', ( ( 1 .. $n ) x $m )[ 0 .. $m - 1 + 1 ];\n\t\t\t}\n\t\telse{\n\t\t\t\"impossible?\";\n\t\t\t}\n\t\t}\n\telse{\n\t\tif( grep { m/a.*b/ or m/b.*a/ } @_ ){\n\t\t\tfor my $i ( 0 .. @_ - 1 ){\n\t\t\t\tnext if 1\n\t\t\t\t\t&& $_[ $i ] !~ m/(a).*b/\n\t\t\t\t\t&& $_[ $i ] !~ m/(b).*a/\n\t\t\t\t\t;\n\t\t\t\tprint 'YES';\n\t\t\t\tprint join ' ', ( ( $i + 1, $+[ 1 ], $i + 1, $+[ 0 ] ) \n\t\t\t\t\tx ( $m >> 1 ) )[ 0 + !!( $m % 4 ) .. $m - 1 + 1 + !!( $m % 4 ) ];\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\telsif( $n == 2 ){\n\t\t\tprint 'NO';\n\t\t\t}\n\t\telse{\n\t\t\t\"impossible?\";\n\t\t\t}\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "79b629047e674883a9bc04b1bf0b7f09"} {"nl": {"description": "You are given an integer $$$n$$$.You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times: Replace $$$n$$$ with $$$\\frac{n}{2}$$$ if $$$n$$$ is divisible by $$$2$$$; Replace $$$n$$$ with $$$\\frac{2n}{3}$$$ if $$$n$$$ is divisible by $$$3$$$; Replace $$$n$$$ with $$$\\frac{4n}{5}$$$ if $$$n$$$ is divisible by $$$5$$$. For example, you can replace $$$30$$$ with $$$15$$$ using the first operation, with $$$20$$$ using the second operation or with $$$24$$$ using the third operation.Your task is to find the minimum number of moves required to obtain $$$1$$$ from $$$n$$$ or say that it is impossible to do it.You have to answer $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 1000$$$) \u2014 the number of queries. The next $$$q$$$ lines contain the queries. For each query you are given the integer number $$$n$$$ ($$$1 \\le n \\le 10^{18}$$$).", "output_spec": "Print the answer for each query on a new line. If it is impossible to obtain $$$1$$$ from $$$n$$$, print -1. Otherwise, print the minimum number of moves required to do it.", "sample_inputs": ["7\n1\n10\n25\n30\n14\n27\n1000000000000000000"], "sample_outputs": ["0\n4\n6\n6\n-1\n6\n72"], "notes": null}, "positive_code": [{"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n OUT: for (my $i = 0; $i < $q; $i++) {\n my $t= <$in>; chomp($t); #this..\n\n if($t == 1) {\n print \"0\\n\";\n next;\n }\n\n my $c= 0;\n my $s= 0;\n while($t != 1) {\n if(($t%2)==0) {\n $t= ($t/2);\n } elsif (($t%3)==0) {\n $t= ((2*$t)/3);\n } elsif (($t%5)==0) {\n $t= ((4*$t)/5);\n } else {\n print \"-1\\n\";\n $s= 1;\n last;\n }\n $c++;\n }\n\n print \"$c\\n\" unless($s);\n\n }\n\n}\n\nmain() unless caller();\n\n"}], "negative_code": [], "src_uid": "ed5ea0e664aa986ab86e4e6746e8a1bf"} {"nl": {"description": "\u041f\u0440\u043e\u0448\u043b\u043e \u043c\u043d\u043e\u0433\u043e \u043b\u0435\u0442, \u0438 \u043d\u0430 \u0432\u0435\u0447\u0435\u0440\u0438\u043d\u043a\u0435 \u0441\u043d\u043e\u0432\u0430 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0438\u0441\u044c n \u0434\u0440\u0443\u0437\u0435\u0439. \u0421 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0432\u0441\u0442\u0440\u0435\u0447\u0438 \u0442\u0435\u0445\u043d\u0438\u043a\u0430 \u0448\u0430\u0433\u043d\u0443\u043b\u0430 \u0434\u0430\u043b\u0435\u043a\u043e \u0432\u043f\u0435\u0440\u0451\u0434, \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0444\u043e\u0442\u043e\u0430\u043f\u043f\u0430\u0440\u0430\u0442\u044b \u0441 \u0430\u0432\u0442\u043e\u0441\u043f\u0443\u0441\u043a\u043e\u043c, \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043e\u0434\u0438\u043d \u0438\u0437 \u0434\u0440\u0443\u0437\u0435\u0439 \u0441\u0442\u043e\u044f\u043b \u0441 \u0444\u043e\u0442\u043e\u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043e\u043c, \u0438, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c, \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0441\u044f \u043d\u0435 \u0437\u0430\u043f\u0435\u0447\u0430\u0442\u043b\u0451\u043d\u043d\u044b\u043c \u043d\u0430 \u0441\u043d\u0438\u043c\u043a\u0435.\u0423\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u041d\u0430 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u0434\u0440\u0443\u0437\u0435\u0439 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a \u0438\u0437 \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439: \u0432 \u0441\u0442\u043e\u044f\u0447\u0435\u043c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0438 i-\u0439 \u0438\u0437 \u043d\u0438\u0445 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a \u0448\u0438\u0440\u0438\u043d\u044b wi \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439 \u0438 \u0432\u044b\u0441\u043e\u0442\u044b hi \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439. \u041d\u043e \u0442\u0430\u043a\u0436\u0435, \u043f\u0440\u0438 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u043c\u043e\u0436\u0435\u0442 \u043b\u0435\u0447\u044c, \u0438 \u0442\u043e\u0433\u0434\u0430 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a \u0448\u0438\u0440\u0438\u043d\u044b hi \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439 \u0438 \u0432\u044b\u0441\u043e\u0442\u044b wi \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439.\u041e\u0431\u0449\u0430\u044f \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440\u044b W\u2009\u00d7\u2009H, \u0433\u0434\u0435 W \u2014 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u0430\u044f \u0448\u0438\u0440\u0438\u043d\u0430 \u0432\u0441\u0435\u0445 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u043e\u0432-\u043b\u044e\u0434\u0435\u0439, \u0430 H \u2014 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0438\u0437 \u0432\u044b\u0441\u043e\u0442. \u0414\u0440\u0443\u0437\u044c\u044f \u0445\u043e\u0442\u044f\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a\u0443\u044e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u043b\u043e\u0449\u0430\u0434\u044c \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043e\u0431\u0449\u0430\u044f \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u044f. \u041f\u043e\u043c\u043e\u0433\u0438\u0442\u0435 \u0438\u043c \u0432 \u044d\u0442\u043e\u043c.", "input_spec": "\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e n (1\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u0440\u0443\u0437\u0435\u0439. \u0412 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 n \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u043f\u043e \u0434\u0432\u0430 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 wi,\u2009hi (1\u2009\u2264\u2009wi,\u2009hi\u2009\u2264\u20091000), \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e i-\u043c\u0443 \u0438\u0437 \u0434\u0440\u0443\u0437\u0435\u0439.", "output_spec": "\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u0440\u0430\u0432\u043d\u043e\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0439 \u043f\u043b\u043e\u0449\u0430\u0434\u0438 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0438, \u0432\u043c\u0435\u0449\u0430\u044e\u0449\u0435\u0439 \u0432\u0441\u0435\u0445 \u0434\u0440\u0443\u0437\u0435\u0439.", "sample_inputs": ["3\n10 1\n20 2\n30 3", "3\n3 1\n2 2\n4 3", "1\n5 10"], "sample_outputs": ["180", "21", "50"], "notes": null}, "positive_code": [{"source_code": "#!perl\n\n$n = <>;\n\n$max_h = 0;\nfor(my $i = 0; $i < $n; $i++)\n{\n\t(my $w, my $h) = split / /, <>;\n\tchomp $w;\n\tchomp $h;\n\t($w, $h) = $w >= $h ? ($w, $h) : ($h, $w);\n\t\n\t$max_h = $h if $max_h < $h ;\n\t\n\tpush @ws, $w;\n\tpush @hs, $h;\n}\n\npush @b, $max_h;\nforeach my $item (@ws)\n{\n\tpush @b, $item if $item >= $max_h;\n}\n\nforeach my $item (@b)\n{\n\tmy $sum = 0;\n\tfor(my $i = 0; $i < $n; $i ++)\n\t{\n\t\t$sum += @ws[$i] > $item ? @ws[$i] : @hs[$i];\n\t}\n\t\n\tpush @s, $sum*$item;\n}\n\n$mins = 10**10;\nforeach my $item (@s)\n{\n\t$mins = $item if $mins > $item;\n}\n\nprint $mins, \"\\n\";"}], "negative_code": [{"source_code": "#!perl\n\n$n =<>;\n\n$max=0;\nfor(my $i=0; $i<$n; $i++)\n{\n $line =<>;\n ($w,$h) = (split / /, $line);\n if($w < $h ){my $b = $w; $w=$h;$h=$b;}\n push @a, ($w,$h);\n if($h > $max) {$max = $h;} \n}\n\n$W=0;\nfor(my $i=0; $i < $#a; $i += 2)\n{\n if(@a[$i] == $max)\n {\n $W += @a[$i+1];\n }\n else\n {\n $W += @a[$i];\n }\n}\n\nprint $W*$max;"}, {"source_code": "#!perl\n\n$n = <>;\n\n$max_h = 0;\nfor(my $i = 0; $i < $n; $i++)\n{\n (my $w, my $h) = split / /, <>;\n chomp $w;\n chomp $h;\n ($w, $h) = $w >= $h ? ($w, $h) : ($h, $w);\n \n $max_h = $h if $max_h < $h ;\n \n push @a, ($w, $h);\n}\n\n@sorted_a = sort { $a <=> $b} @a;\n@b = @sorted_a[$n..$#sorted_a];\npush @b, $max_h;\n\nforeach my $item (@b)\n{\n my $sum = 0;\n for(my $i = 0; $i <= $#a; $i += 2)\n {\n $sum += @a[$i] > $item ? @a[$i] : @a[$i + 1];\n }\n \n print $sum, \" \", $item, \"\\n\";\n push @s, $sum*$item;\n}\n\n$mins = 10**10;\nforeach my $item (@s)\n{\n $mins = $item if $mins > $item;\n}\n\nprint $mins, \"\\n\";"}, {"source_code": "#!perl\n\n$n = <>;\n\n$max_h = 0;\nfor(my $i = 0; $i < $n; $i++)\n{\n (my $w, my $h) = split / /, <>;\n chomp $w;\n chomp $h;\n ($w, $h) = $w >= $h ? ($w, $h) : ($h, $w);\n \n $max_h = $h if $max_h < $h ;\n \n push @a, ($w, $h);\n}\n\n@b = sort { $a <=> $b} @a;\n\nfor(my $j = $n; $j <= $#b; $j++)\n{\n my $sum = 0;\n for(my $i = 0; $i <= $#a; $i += 2)\n {\n $sum += @a[$i] > @b[$j] ? @a[$i] : @a[$i + 1];\n }\n \n push @s, $sum*@b[$j];\n}\n\n$mins = 10**10;\nforeach my $item (@s)\n{\n $mins = $item if $mins > $item and $item != 0;\n}\n\nprint $mins, \"\\n\";"}, {"source_code": "#!perl\n\n$n = <>;\n\n$max_h = 0;\n$max_w = 0;\nfor(my $i = 0; $i < $n; $i++)\n{\n (my $w, my $h) = split / /, <>;\n chomp $w;\n chomp $h;\n ($w, $h) = $w >= $h ? ($w, $h) : ($h, $w);\n \n $max_w >= $w or $max_w = $w;\n $max_h >= $h or $max_h = $h;\n \n push @ws, $w;\n push @hs, $h;\n}\n\n$sum_w1 = 0;\nfor(my $i = 0; $i <= $n; $i++)\n{\n $sum_w1 += @ws[$i] > $max_h ? @ws[$i] : @hs[$i];\n}\n\n$sum_w2 = 0;\nfor(my $i = 0; $i <= $n; $i++)\n{\n $sum_w2 += @hs[$i];\n}\n\n$result1 = $sum_w1*$max_h;\n$result2 = $sum_w2*$max_w;\n\n$result = $result1 < $result2 ? $result1 : $result2;\n\nprint $result, \"\\n\";"}, {"source_code": "#!perl\n\n$n = <>;\n\n$max_h = 0;\nfor(my $i = 0; $i < $n; $i++)\n{\n (my $w, my $h) = split / /, <>;\n ($w, $h) = $w >= $h ? ($w, $h) : ($h, $w);\n \n $max_h >= $h or $max_h = $h;\n \n push @a, ($w, $h);\n}\n\n$sum_w = 0;\nfor(my $i = 0; $i <= $#a; $i += 2)\n{\n $sum_w += @a[$i] > $max_h ? @a[$i] : @a[$i + 1];\n}\n\nprint $sum_w*$max_h, \"\\n\";"}, {"source_code": "#!perl\n\n$n = <>;\n\n$max_h = 0;\nfor(my $i = 0; $i < $n; $i++)\n{\n (my $w, my $h) = split / /, <>;\n chomp $w;\n chomp $h;\n ($w, $h) = $w >= $h ? ($w, $h) : ($h, $w);\n \n $max_h = $h if $max_h < $h ;\n \n push @a, ($w, $h);\n}\n\nfor(my $j = 0; $j < $#a; $j += 2)\n{\n my $sum = 0;\n for(my $i = 0; $i <= 2*$n; $i += 2)\n {\n $sum += @a[$i] > @a[$j] ? @a[$i] : @a[$i + 1];\n }\n \n push @s, $sum*$item;\n}\n\n$sum = 0;\nfor(my $i = 0; $i <= $#a; $i += 2)\n{\n $sum += @a[$i] > $max_h ? @a[$i] : @a[$i + 1];\n}\npush @s, $sum*$max_h;\n\n$mins = 10**10;\nforeach my $item (@s)\n{\n $mins = $item if $mins > $item and $item != 0;\n}\n\nprint $mins, \"\\n\";"}, {"source_code": "#!perl\n\n$n = <>;\n\n$max_h = 0;\n$max_w = 0;\nfor(my $i = 0; $i < $n; $i++)\n{\n\t(my $w, my $h) = split / /, <>;\n\tchomp $w;\n\tchomp $h;\n\t($w, $h) = $w >= $h ? ($w, $h) : ($h, $w);\n\t\n\t$max_w >= $w or $max_w = $w;\n\t$max_h >= $h or $max_h = $h;\n\t\n\tpush @a, ($w, $h);\n}\n\n$sum_w1 = 0;\nfor(my $i = 0; $i <= $#a; $i += 2)\n{\n\t$sum_w1 += @a[$i] > $max_h ? @a[$i] : @a[$i + 1];\n}\n\n$sum_w2 = 0;\nfor(my $i = 1; $i <= $#a; $i += 2)\n{\n\t$sum_w2 += @a[$i];\n}\n\n$result1 = $sum_w1*$max_h;\n$result2 = $sum_w2*$max_w;\n\nprint $result1 < $result1 ? $result1 : $result2, \"\\n\";"}, {"source_code": "#!perl\n\n$n = <>;\n\n$max_h = 0;\nfor(my $i = 0; $i < $n; $i++)\n{\n (my $w, my $h) = split / /, <>;\n chomp $w;\n chomp $h;\n ($w, $h) = $w >= $h ? ($w, $h) : ($h, $w);\n \n $max_h = $h if $max_h < $h ;\n \n push @a, ($w, $h);\n}\n\n@sorted_a = sort { $a <=> $b} @a;\n@b = @sorted_a[$n..$#sorted_a];\npush @b, $max_h;\n\nforeach my $item (@b)\n{\n my $sum = 0;\n for(my $i = 0; $i <= $#a; $i += 2)\n {\n $sum += @a[$i] > $item ? @a[$i] : @a[$i + 1];\n }\n \n push @s, $sum*$item;\n}\n\n$mins = 10**10;\nforeach my $item (@s)\n{\n $mins = $item if $mins > $item;\n}\n\nprint $mins, \"\\n\";"}], "src_uid": "c1e952cb7dd158f12df6affcff07b68a"} {"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\u2009-\u20091.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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000) \u2014 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": "use strict;\nour ($n, @c1, @c2, $C1, $C2, @f1, @f2, $F1, $F2, $start, $end, $minval);\n\nsub input {\n\t($n,my $s)=<>; chomp($s, $n); my @c=split //, $s; @c = map ord, @c;\n\tmy ($a, $A, $Z) = (ord(\"a\"), ord(\"A\"), ord(\"Z\"));\n\tmy ($c1, $c2);\n\t$C1 = $C2 = $F1 = $F2 = 1;\n\tfor (@c) {\n\t\tif ($_ <= $Z) {\n\t\t\t$c1 = $_-$A+1; $c2 = 0;\n\t\t} else {\n\t\t\t$c2 = $_-$a+1; $c1 = 0;\n\t\t}\n\t\tpush @c1, $c1; push @c2, $c2;\n\t\t$C1 |= 1 << $c1;\n\t\t$C2 |= 1 << $c2;\n\t}\n}\n\nsub min { $_[0] < $_[1]? $_[0]: $_[1] }\n\nsub adv_end {\n\t$end++;\n\tmy $v;\n\t$v = $c1[$end]; $f1[$v]++; $F1 |= 1 << $v;\n\t$v = $c2[$end]; $f2[$v]++; $F2 |= 1 << $v;\n}\n\nsub adv_start {\n\tmy $v;\n\t$v = $c1[$start]; if ($v) { $f1[$v]--; $F1 &= ~(1 << $v) if $f1[$v]==0 }\n\t$v = $c2[$start]; if ($v) { $f2[$v]--; $F2 &= ~(1 << $v) if $f2[$v]==0 }\n\t$start++;\n}\n\nsub has_all {\n\t$F1==$C1 && $F2==$C2;\n}\n\nsub calc_min {\n\t$minval = min($minval, $end - $start + 1);\n}\n\nsub iter {\n\t$start = 0; $end = -1; $minval = $n;\n\twhile ($start < $n - 1) {\n\t\tif (!has_all) {\n\t\t\twhile ($end < $n - 1) {\n\t\t\t\tadv_end;\n\t\t\t\tif (has_all) {\n\t\t\t\t\tcalc_min;\n\t\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tadv_start; calc_min if has_all;\n\t}\n}\n\ninput(); iter();\nprint $minval, \"\\n\";\n"}, {"source_code": "$\\ = $/;\n<>;\n$_ = <>, chomp;\n@_ = split //;\n%h = map { $_, 0 } @_;\n$H = keys %h;\n$j = 0;\n$min = 1e10;\nfor $i (0 .. @_-1){\n $g{ $_[$i] } ++;\n\n while( $g{ $_[$j] } > 1){\n $g{ $_[$j ++] } --;\n }\n ($H == grep $_, values %g)\n and $min > $i - $j + 1\n and $min = $i - $j + 1;\n}\nprint $min"}], "negative_code": [{"source_code": "use strict;\nour ($n, @c1, @c2, $C1, $C2, @f1, @f2, $F1, $F2, $start, $end, $minval);\n\nsub input {\n\t($n,my $s)=<>; chomp($s, $n); my @c=split //, $s; @c = map ord, @c;\n\tmy ($a, $A, $Z) = (ord(\"a\"), ord(\"A\"), ord(\"Z\"));\n\tmy ($c1, $c2);\n\tfor (@c) {\n\t\tif ($_ <= $Z) {\n\t\t\t$c1 = $_-$A+1; $c2 = 0;\n\t\t} else {\n\t\t\t$c2 = $_-$a+1; $c1 = 0;\n\t\t}\n\t\tpush @c1, $c1; push @c2, $c2;\n\t\t$C1 |= 1 << $c1;\n\t\t$C2 |= 1 << $c2;\n\t}\n}\n\nsub min { $_[0] < $_[1]? $_[0]: $_[1] }\n\nsub adv_end {\n\t$end++;\n\tmy $v;\n\t$v = $c1[$end]; $f1[$v]++; $F1 |= 1 << $v;\n\t$v = $c2[$end]; $f2[$v]++; $F2 |= 1 << $v;\n}\n\nsub adv_start {\n\tmy $v;\n\t$v = $c1[$start]; if ($v) { $f1[$v]--; $F1 &= ~(1 << $v) if $f1[$v]==0 }\n\t$v = $c2[$start]; if ($v) { $f2[$v]--; $F2 &= ~(1 << $v) if $f2[$v]==0 }\n\t$start++;\n}\n\nsub has_all {\n\t$F1==$C1 && $F2==$C2;\n}\n\nsub calc_min {\n\t$minval = min($minval, $end - $start + 1);\n}\n\nsub iter {\n\t$start = 0; $end = -1; $minval = $n;\n\twhile ($start < $n - 1) {\n\t\twhile ($end < $n - 1) {\n\t\t\tadv_end;\n\t\t\tif (has_all) {\n\t\t\t\tcalc_min;\n\t\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\tadv_start; calc_min if has_all;\n\t}\n}\n\ninput(); iter();\nprint $minval, \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\n$\\ = $/;\n<>;\n$_ = <>, chomp;\n@_ = split //;\n%h = map { $_, 0 } @_;\n$H = keys %h;\n$j = 0;\n$min = 1e10;\nfor $i (0 .. @_-1){\n $g{ $_[$i] } ++;\n for ( ; $H == keys %g ; $j ++ ){\n $min > $i - $j + 1 and $min = $i - $j + 1;\n -- $g{ $_[$j] } or do { delete $g{ $_[$j] }; last };\n }\n}\nprint \"$min\";\n"}], "src_uid": "60776cefd6c1a2f3c16b3378ebc31a9a"} {"nl": {"description": "Let's call the string beautiful if it does not contain a substring of length at least $$$2$$$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not.Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $$$3$$$ letters of the Latin alphabet (in lowercase).You are given a string $$$s$$$ of length $$$n$$$, each character of the string is one of the first $$$3$$$ letters of the Latin alphabet (in lowercase).You have to answer $$$m$$$ queries\u00a0\u2014 calculate the cost of the substring of the string $$$s$$$ from $$$l_i$$$-th to $$$r_i$$$-th position, inclusive.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 2 \\cdot 10^5$$$)\u00a0\u2014 the length of the string $$$s$$$ and the number of queries. The second line contains the string $$$s$$$, it consists of $$$n$$$ characters, each character one of the first $$$3$$$ Latin letters. The following $$$m$$$ lines contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \\le l_i \\le r_i \\le n$$$)\u00a0\u2014 parameters of the $$$i$$$-th query.", "output_spec": "For each query, print a single integer\u00a0\u2014 the cost of the substring of the string $$$s$$$ from $$$l_i$$$-th to $$$r_i$$$-th position, inclusive.", "sample_inputs": ["5 4\nbaacb\n1 3\n1 5\n4 5\n2 3"], "sample_outputs": ["1\n2\n0\n1"], "notes": "NoteConsider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($n,$m) = map { $_ - 0 } split(/\\s+/,);\r\n( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n\r\nmy @pat = ('abc','bca','cab','cba','bac','acb');\r\n\r\nmy @r = (); $#r = 5;\r\nforeach my $i (0..5){\r\n my @r1 = (0); $#r1 = $n;\r\n foreach my $j (0..($n-1)){\r\n $r1[$j+1] = $r1[$j] + ( substr($s,$j,1) ne substr($pat[$i],$j%3,1) ? 1 : 0 );\r\n }\r\n $r[$i] = \\@r1;\r\n}\r\n\r\nforeach my $i (1..$m){\r\n my ($l,$r) = map { $_ - 1 } split(/\\s+/,);\r\n my $mn = $mod;\r\n foreach my $j (0..5){\r\n my $v = $r[$j]->[$r+1] - $r[$j]->[$l];\r\n $mn = $v if $v < $mn;\r\n }\r\n print \"$mn\\n\";\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "src_uid": "b4183febe5ae61770368d2e16f273675"} {"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\u00a0\u2014 the number of items (1\u2009\u2264\u2009n\u2009\u2264\u2009105). The second line contains n numbers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009105)\u00a0\u2014 the initial inventory numbers of the items.", "output_spec": "Print n numbers\u00a0\u2014 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$_ = <>;\n\nmap $h[ $_ ] ++, @_ = split;\n\n$j ++;\n\nfor $i (@_){\n\t\n\tpush @a, $i > @_ || $h[ $i ] --> 1 ? do {\n\t\t\tNOP while $h[ $j ++ ] =~ /./;\n\t\t\t++ $h[ -- $j ];\n\t\t\t$j\n\t\t}\n\t\t:\t$i\n\t\n\t}\n\nprint \"@a\""}, {"source_code": "$n = <>;\n$_ = <>;\n@_ = split;\n\nmap $h{ $_ } ++, @_;\n\n$j = 0;\n$h{ $j } = Not_important;\n\nfor $i (@_){\n\t\n\tpush @a, $i > $n || $h{ $i } --> 1 ? do {\n\t\twhile ( exists $h{ $j } ){\n\t\t\t$j ++\n\t\t\t}\n\t\t\t++ $h{ $j } and $j\n\t\t}\n\t\t:\t$i\n\t\n\t}\n\nprint \"@a\""}], "negative_code": [], "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\u2009\u2264\u2009n\u2009\u2264\u2009500\u2009000)\u00a0\u2014 the number of black pieces. The second line contains two integers x0 and y0 (\u2009-\u2009109\u2009\u2264\u2009x0,\u2009y0\u2009\u2264\u2009109)\u00a0\u2014 coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi (\u2009-\u2009109\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009109)\u00a0\u2014 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": "my $n = ;\n\nmy $FIN = ;\nmy ($x0, $y0) = split(/ /, $FIN);\n\nmy @figures = [];\nfor my $i (0..$n-1)\t{\n\tmy $line = ;\n\tmy @figure = split(/ /, $line);\n\t$figures[$i] = \\@figure;\n}\nmy $min = -1e10;\nmy $minS = '';\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[1] == $x0 && $figures[$i]->[2] < $y0) {\n\t\tif ($min < $figures[$i]->[2]) {\n\t\t\t$min = $figures[$i]->[2];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n$min = 1e10;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[1] == $x0 && $figures[$i]->[2] > $y0) {\n\t\tif ($min > $figures[$i]->[2]) {\n\t\t\t$min = $figures[$i]->[2];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n$min = -1e10;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] == $y0 && $figures[$i]->[1] < $x0) {\n\t\tif ($min < $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n$min = 1e10;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] == $y0 && $figures[$i]->[1] > $x0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n\n\n$min = -1e10;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] - $figures[$i]->[1] == $y0 - $x0 && $figures[$i]->[1] < $x0 && $figures[$i]->[2] < $y0) {\n\t\tif ($min < $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n$min = 1e10;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] - $figures[$i]->[1] == $y0 - $x0 && $figures[$i]->[1] > $x0 && $figures[$i]->[2] > $y0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n$min = -1e10;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] + $figures[$i]->[1] == $y0 + $x0 && $figures[$i]->[1] < $x0 && $figures[$i]->[2] > $y0) {\n\t\tif ($min < $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n$min = 1e10;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] + $figures[$i]->[1] == $y0 + $x0 && $figures[$i]->[1] > $x0 && $figures[$i]->[2] < $y0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\nprint 'NO';\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $X, $Y ) = split ' ', <>;\n\t\t\n\tmy $check = 0;\n\n\tmy @top = (~0) x 5;\n\tmy @bot = (-~0) x 5;\n\tmy @topF;\n\tmy @botF;\n\t\n\tfor (1 .. $_){\n\t\tmy( $fig, $x, $y ) = split ' ', <>;\n\t\t\n\t\tmy $D = $x == $X ? 1 :\n\t\t\t$y == $Y ? 2 :\n\t\t\t$x + $y == $X + $Y ? 3 :\n\t\t\t$x - $X == $y - $Y ? 4 : 0;\n\t\t\t\n\t\t$D or next;\n\t\tmy $M = $X;\n\t\t$D == 1 and do { $x = $y ; $M = $Y };\n\t\t\n\t\tif( $x > $M and $x < $top[$D]){\n\t\t\t$top[$D] = $x;\n\t\t\t$topF[$D] = $fig;\n\t\t\t}\n\t\tif( $x < $M and $x > $bot[$D]){\n\t\t\t$bot[$D] = $x;\n\t\t\t$botF[$D] = $fig;\n\t\t\t}\t\t\n\t\t}\n\t\t\n\t$check += grep /[QR]/, grep defined, @topF[1,2], @botF[1,2];\n\t$check += grep /[QB]/, grep defined, @topF[3,4], @botF[3,4];\n\t\n\tprint $check ? \"YES\" : \"NO\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $X, $Y ) = split ' ', <>;\n\t\n\tmy( @X, @Y, @a8h1, @a1h8 );\n\t\n\tfor (1 .. $_){\n\t\tmy( $fig, $x, $y ) = split ' ', <>;\n\t\t\n\t\tif( $x == $X ){\n\t\t\tpush @X, [$fig, $x, $y];\n\t\t\t}\n\t\tif( $y == $Y ){\n\t\t\tpush @Y, [$fig, $x, $y];\n\t\t\t}\n\t\tif( $x + $y == $X + $Y ){\n\t\t\tpush @a8h1, [$fig, $x, $y];\n\t\t\t}\n\t\tif( $x - $X == $y - $Y ){\n\t\t\tpush @a1h8, [$fig, $x, $y];\n\t\t\t}\t\t\n\t\t}\n\n\tmy $check = 0;\n\n\tmy $top = ~0;\n\tmy $bottom = -~0;\n\tmy $topFig;\n\tmy $bottomFig;\n\t\n\tfor( @X ){\n\t\tif( $_->[2] > $Y and $_->[2] < $top){\n\t\t\t$top = $_->[2];\n\t\t\t$topFig = $_->[0];\n\t\t\t}\n\t\tif( $_->[2] < $Y and $_->[2] > $bottom){\n\t\t\t$bottom = $_->[2];\n\t\t\t$bottomFig = $_->[0];\n\t\t\t}\t\t\n\t\t}\n\t\t\n\t$check += grep /[QR]/, grep defined, $topFig, $bottomFig;\n\t\n#\tprint \"check X: $check\";\n\t\n\t\n\tmy $top = ~0;\n\tmy $bottom = -~0;\n\tmy $topFig;\n\tmy $bottomFig;\n\t\n\tfor( @Y ){\n\t\tif( $_->[1] > $X and $_->[1] < $top){\n\t\t\t$top = $_->[1];\n\t\t\t$topFig = $_->[0];\n\t\t\t}\n\t\tif( $_->[1] < $X and $_->[1] > $bottom){\n\t\t\t$bottom = $_->[1];\n\t\t\t$bottomFig = $_->[0];\n\t\t\t}\t\t\n\t\t}\n\t\t\n\t$check += grep /[QR]/, grep defined, $topFig, $bottomFig;\n\t\n#\tprint \"check Y: $check\";\n\t\n\t\n\tmy $top = ~0;\n\tmy $bottom = -~0;\n\tmy $topFig;\n\tmy $bottomFig;\n\t\n\tfor( @a8h1 ){\n\t\tif( $_->[1] > $X and $_->[1] < $top){\n\t\t\t$top = $_->[1];\n\t\t\t$topFig = $_->[0];\n\t\t\t}\n\t\tif( $_->[1] < $X and $_->[1] > $bottom){\n\t\t\t$bottom = $_->[1];\n\t\t\t$bottomFig = $_->[0];\n\t\t\t}\t\t\n\t\t}\n\t\t\n\t$check += grep /[QB]/, grep defined, $topFig, $bottomFig;\n\t\n#\tprint \"check a8h1: $check\";\n\t\n\n\tmy $top = ~0;\n\tmy $bottom = -~0;\n\tmy $topFig;\n\tmy $bottomFig;\n\t\n\tfor( @a1h8 ){\n\t\tif( $_->[1] > $X and $_->[1] < $top){\n\t\t\t$top = $_->[1];\n\t\t\t$topFig = $_->[0];\n\t\t\t}\n\t\tif( $_->[1] < $X and $_->[1] > $bottom){\n\t\t\t$bottom = $_->[1];\n\t\t\t$bottomFig = $_->[0];\n\t\t\t}\t\t\n\t\t}\n\t\t\n\t$check += grep /[QB]/, grep defined, $topFig, $bottomFig;\n\t\n#\tprint \"check a1h8: $check\";\n\t\n\tprint $check ? \"YES\" : \"NO\";\n}\n"}], "negative_code": [{"source_code": "my $n = ;\n\nmy $FIN = ;\nmy ($x0, $y0) = split(/ /, $FIN);\n\nmy @figures = [];\nfor my $i (0..$n-1)\t{\n\tmy $line = ;\n\tmy @figure = split(/ /, $line);\n\t$figures[$i] = \\@figure;\n}\nmy $min = -1e9;\nmy $minS = '';\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[1] == $x0 && $figures[$i]->[2] < $y0) {\n\t\tif ($min < $figures[$i]->[2]) {\n\t\t\t$min = $figures[$i]->[2];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n$min = 1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[1] == $x0 && $figures[$i]->[2] > $y0) {\n\t\tif ($min > $figures[$i]->[2]) {\n\t\t\t$min = $figures[$i]->[2];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n$min = -1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] == $y0 && $figures[$i]->[1] < $x0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n$min = 1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] == $y0 && $figures[$i]->[1] > $x0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n\n\n$min = -1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] - $figures[$i]->[1] == $y0 - $x0 && $figures[$i]->[1] < $x0 && $figures[$i]->[2] < $y0) {\n\t\tif ($min < $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n$min = 1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] - $figures[$i]->[1] == $y0 - $x0 && $figures[$i]->[1] > $x0 && $figures[$i]->[2] > $y0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n$min = -1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] + $figures[$i]->[1] == $y0 + $x0 && $figures[$i]->[1] < $x0 && $figures[$i]->[2] > $y0) {\n\t\tif ($min < $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\n$min = 1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] + $figures[$i]->[1] == $y0 + $x0 && $figures[$i]->[1] > $x0 && $figures[$i]->[2] < $y0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n$minS = '';\nprint 'NO';\n"}, {"source_code": "my $n = ;\n\nmy $FIN = ;\nmy ($x0, $y0) = split(/ /, $FIN);\n\nmy @figures = [];\nfor my $i (0..$n-1)\t{\n\tmy $line = ;\n\tmy @figure = split(/ /, $line);\n\t$figures[$i] = \\@figure;\n}\nmy $min = 0;\nmy $minS;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[1] == $x0 && $figures[$i]->[2] < $y0) {\n\t\tif ($min < $figures[$i]->[2]) {\n\t\t\t$min = $figures[$i]->[2];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n$min = 1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[1] == $x0 && $figures[$i]->[2] > $y0) {\n\t\tif ($min > $figures[$i]->[2]) {\n\t\t\t$min = $figures[$i]->[2];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n$min = 0;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] == $y0 && $figures[$i]->[1] < $x0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n$min = 1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] == $y0 && $figures[$i]->[1] > $x0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n\n\n$min = 0;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] - $figures[$i]->[1] == $y0 - $x0 && $figures[$i]->[1] < $x0 && $figures[$i]->[2] < $y0) {\n\t\tif ($min < $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n$min = 1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] - $figures[$i]->[1] == $y0 - $x0 && $figures[$i]->[1] > $x0 && $figures[$i]->[2] > $y0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n$min = 0;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] + $figures[$i]->[1] == $y0 + $x0 && $figures[$i]->[1] < $x0 && $figures[$i]->[2] > $y0) {\n\t\tif ($min < $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n$min = 1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] + $figures[$i]->[1] == $y0 + $x0 && $figures[$i]->[1] > $x0 && $figures[$i]->[2] < $y0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\nprint 'NO';\n"}, {"source_code": "my $n = ;\n\nmy $FIN = ;\nmy ($x0, $y0) = split(/ /, $FIN);\n\nmy @figures = [];\nfor my $i (0..$n-1)\t{\n\tmy $line = ;\n\tmy @figure = split(/ /, $line);\n\t$figures[$i] = \\@figure;\n}\nmy $min = -1e9;\nmy $minS;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[1] == $x0 && $figures[$i]->[2] < $y0) {\n\t\tif ($min < $figures[$i]->[2]) {\n\t\t\t$min = $figures[$i]->[2];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n$min = 1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[1] == $x0 && $figures[$i]->[2] > $y0) {\n\t\tif ($min > $figures[$i]->[2]) {\n\t\t\t$min = $figures[$i]->[2];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n$min = -1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] == $y0 && $figures[$i]->[1] < $x0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n$min = 1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] == $y0 && $figures[$i]->[1] > $x0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'R' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n\n\n$min = -1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] - $figures[$i]->[1] == $y0 - $x0 && $figures[$i]->[1] < $x0 && $figures[$i]->[2] < $y0) {\n\t\tif ($min < $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n$min = 1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] - $figures[$i]->[1] == $y0 - $x0 && $figures[$i]->[1] > $x0 && $figures[$i]->[2] > $y0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n$min = -1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] + $figures[$i]->[1] == $y0 + $x0 && $figures[$i]->[1] < $x0 && $figures[$i]->[2] > $y0) {\n\t\tif ($min < $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\n$min = 1e9;\nfor my $i (0..$n-1) {\n\tif ($figures[$i]->[2] + $figures[$i]->[1] == $y0 + $x0 && $figures[$i]->[1] > $x0 && $figures[$i]->[2] < $y0) {\n\t\tif ($min > $figures[$i]->[1]) {\n\t\t\t$min = $figures[$i]->[1];\n\t\t\t$minS = $figures[$i]->[0];\n\t\t}\n\t}\n}\nif ($minS eq 'B' || $minS eq 'Q') {\n\tprint 'YES';\n\texit;\n}\n\nprint 'NO';\n"}], "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: \u2009+\u2009 ai\u00a0\u2014 add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer. \u2009-\u2009 ai\u00a0\u2014 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\u00a0\u2014 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\u2009=\u2009010, 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\u2009\u2264\u2009t\u2009\u2264\u2009100\u2009000)\u00a0\u2014 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\u00a0\u2014 the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0\u2009\u2264\u2009ai\u2009<\u20091018) 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": "#use strict;\n#use warnings;\n\nuse Data::Dumper;\n$,=\" \";\nmy $N=<>;\nchomp($N);\nmy %data;\n\nwhile($N--)\n{\nmy ($op,$s)=split / /,<>;\nchomp($op,$s);\nif($op eq '+')\n{\n my $p;\n for my $i (0..length($s)-1)\n {\n $p.=substr($s,$i,1)%2;\n }\n #print $p , \"\\n\";\n $data{$p}++;\n}\nif($op eq '-')\n{\n my $p;\n for my $i (0..length($s)-1)\n {\n $p.=substr($s,$i,1)%2;\n }\n #print $p , \"\\n\";\n $data{$p}--;\n}\nif($op eq '?')\n{\n $s=~ s/^0+//g;\n my $count=0;\n my $t=length($s);\n $count+=$data{$s};\n for($t..17)\n {\n $s=\"0\".$s;\n $count+=$data{$s};\n }\n print $count . \"\\n\";\n}\n}\n"}], "negative_code": [], "src_uid": "0ca6c7ff6a2d110a8f4153717910378f"} {"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)$$$ \u00a0\u2014 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": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\nmy $debug = 0;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tmy( $n, $k ) = split;\r\n\t\r\n\t$debug and print $n;\r\n\t\r\n\tif( $n % 6 == 0 ){\r\n\t\tprint join ' ', ( $n / 3 ) x 3;\r\n\t\t}\r\n\telsif( $n % 2 == 1 ){\r\n\t\tprint join ' ', 1, ( ( $n - 1 ) / 2 ) x 2;\r\n\t\t}\r\n\telse{\r\n\t\tmy $times = 0;\r\n\t\twhile( $n % 2 == 0 and $n > 2 ){\r\n\t\t\t$n /= 2;\r\n\t\t\t$times ++;\r\n\t\t\t}\r\n\t\tprint join ' ', map $_ * 2 ** $times, 1, ( ( $n - 1 ) / 2 ) x 2;\r\n\t\t}\r\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tmy( $n, $k ) = split;\r\n\t\r\n\tprint join ' ', 1, 1, $n - 2;\r\n\t}"}], "src_uid": "842a0587147591ea38a20638f3a7960d"} {"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\u2009\u2264\u2009i\u2009\u2264\u2009n) is displayed, squares 1,\u20092,\u2009... ,\u2009i\u2009-\u20091 has the saturation k, squares i\u2009+\u20091,\u2009i\u2009+\u20092,\u2009... ,\u2009n 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\u2009\u2264\u2009n,\u2009k\u2009\u2264\u2009100, 0\u2009\u2264\u2009t\u2009\u2264\u2009100).", "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": "<>=~/ (\\d+) /;\n\n$c=$'*$`*$1/100;\n$c%=$c+1;\n\nwhile ($i++<$`){\n$c>=$1?($_.=\"$1 \",$c-=$1):($_.=\"$c \",$c=0);\n}\nchop, print"}, {"source_code": "<>=~/ (\\d+) /;\n$c=($'*$`*$1 - $'*$`*$1 % 100) / 100;\n$c>=$1?($_.=\"$1 \",$c-=$1):($_.=\"$c \",$c=0) while $i++<$`;\nchop, print"}], "negative_code": [], "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,\u20090,\u20090,\u20091,\u20091,\u20091,\u20090,\u20090,\u20090] can be a photo of zebra, while the photo [0,\u20090,\u20090,\u20091,\u20091,\u20091,\u20091] 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\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000) \u2014 the width of the photo. The second line contains a sequence of integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u20091) \u2014 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": "$t = <>;\n@arr = split(/\\s+/, <>);\n\n$slc = 0;\n$a = 0;\n\nfor (my $i = 0; $i < (scalar @arr) - 1; $i++) {\n if (@arr[$i] != @arr[$i + 1]) {\n if ($a == 0) {\n $a = $i + 1;\n } else {\n if ($i - $slc != $a) {\n print \"NO\";\n exit;\n }\n }\n $slc = $i;\n }\n}\n\nif ((scalar @arr) - $slc - 1 != $a && $a != 0) {\n print \"NO\";\n exit;\n}\n\nprint \"YES\";\n"}, {"source_code": "$n = ;\n@bits = split / /, ;\n\n$first = @bits[0];\n$mb_len = 1;\n$mb_len_done = 0;\n\nfor ($curr = 1; $curr < $n; $curr++) {\n\tif (@bits[$curr] != @bits[$curr-1]) {\n\t\t$mb_len_done = 1;\n\t}\n\tif (!$mb_len_done) {\n\t\t$mb_len += 1;\n\t}\n}\n\nif ($mb_len == $n) {\n\tprint \"Yes\";\n\texit();\n}\n\n$result = 1; # true\n$curr_color = $first;\n$last = 0;\n\nfor ($i = 0; $i < $n;) {\n\t$last = 0;\n\tfor ($j = 0; $j < $mb_len && $i < $n; $j++) {\n\t\t$last += 1;\n\t\tif ($curr_color != @bits[$i]) {\n\t\t\t$result = 0;\t\t\t\n\t\t}\n\t\t$i += 1;\n\t}\n\t$curr_color = ($curr_color + 1) % 2;\n}\n\nif ($result && $last == $mb_len) {\n\tprint \"Yes\";\n} else {\n\tprint \"No\";\n}"}, {"source_code": "$n = ;\n$string = ;\n \n@a = split(//, $string);\n$i = 2;\n$cur = @a[0];\n$f = 5;\n$ans = 1;\n$prev_c = 1;\n$now_c = 0;\nwhile ($n > 1) {\n $n = $n - 1;\n if ($f == 5) {\n if (@a[$i] == $cur) {\n $prev_c = $prev_c + 1;\n }\n else {\n $now_c = 1;\n $f = 0;\n $cur = @a[$i];\n }\n } else {\n if (@a[$i] == $cur) {\n $now_c = $now_c + 1;\n } else {\n if ($now_c != $prev_c) {\n $ans = 0;\n }\n else {\n $prev_c = $now_c;\n $now_c = 1;\n $cur = @a[$i];\n }\n }\n \n }\n $i = $i + 2;\n}\nif ($now_c != 0) {\n if ($now_c != $prev_c) {\n $ans = 0;\n }\n}\n\nif ($ans == 1) {\n print \"YES\";\n} else {\n print \"NO\";\n}"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nno warnings 'uninitialized';\n\n\nmy $N = <>+0;\nmy $line = <>;\nchomp $line;\n$line =~ s/ +//g;\n$line =~ s/01/0#1/g;\n$line =~ s/10/1#0/g;\n\n#print $line, \"\\n\";\n\nmy @A = split '#', $line;\n\nmy $M = scalar(@A);\nfor (my $i = 1; $i < $M; ++$i) {\n if (length($A[$i]) != length($A[0])) {\n print \"NO\\n\";\n exit(0);\n }\n}\n\nprint \"YES\\n\";\n"}, {"source_code": "$n=;\n$_ = ; @a=split;\n$prvval=-1;\n$cur=0;\n$curv=$a[0];\n$isok=1;\nforeach $x (@a)\n{\n if($curv!=$x) {\n if($prvval!=-1){\n if($cur!=$prvval) {\n $isok = 0;\n }\n }\n $prvval = $cur;\n $cur = 0;\n $curv = $x;\n \n }\n $cur=$cur+1;\n}\n\nif($prvval!=-1){\n if($cur!=$prvval) {\n $isok = 0;\n }\n}\n\nif($isok==1){\n print \"YES\";\n}\nelse\n{\n print \"NO\";\n}"}], "negative_code": [], "src_uid": "75a00d8a614fd0bcb8504b0268a121e0"} {"nl": {"description": "You are given a deck of $$$n$$$ cards numbered from $$$1$$$ to $$$n$$$ (not necessarily in this order in the deck). You have to sort the deck by repeating the following operation. Choose $$$2 \\le k \\le n$$$ and split the deck in $$$k$$$ nonempty contiguous parts $$$D_1, D_2,\\dots, D_k$$$ ($$$D_1$$$ contains the first $$$|D_1|$$$ cards of the deck, $$$D_2$$$ contains the following $$$|D_2|$$$ cards and so on). Then reverse the order of the parts, transforming the deck into $$$D_k, D_{k-1}, \\dots, D_2, D_1$$$ (so, the first $$$|D_k|$$$ cards of the new deck are $$$D_k$$$, the following $$$|D_{k-1}|$$$ cards are $$$D_{k-1}$$$ and so on). The internal order of each packet of cards $$$D_i$$$ is unchanged by the operation. You have to obtain a sorted deck (i.e., a deck where the first card is $$$1$$$, the second is $$$2$$$ and so on) performing at most $$$n$$$ operations. It can be proven that it is always possible to sort the deck performing at most $$$n$$$ operations.Examples of operation: The following are three examples of valid operations (on three decks with different sizes). If the deck is [3 6 2 1 4 5 7] (so $$$3$$$ is the first card and $$$7$$$ is the last card), we may apply the operation with $$$k=4$$$ and $$$D_1=$$$[3 6], $$$D_2=$$$[2 1 4], $$$D_3=$$$[5], $$$D_4=$$$[7]. Doing so, the deck becomes [7 5 2 1 4 3 6]. If the deck is [3 1 2], we may apply the operation with $$$k=3$$$ and $$$D_1=$$$[3], $$$D_2=$$$[1], $$$D_3=$$$[2]. Doing so, the deck becomes [2 1 3]. If the deck is [5 1 2 4 3 6], we may apply the operation with $$$k=2$$$ and $$$D_1=$$$[5 1], $$$D_2=$$$[2 4 3 6]. Doing so, the deck becomes [2 4 3 6 5 1]. ", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1\\le n\\le 52$$$) \u00a0\u2014 the number of cards in the deck. The second line contains $$$n$$$ integers $$$c_1, c_2, \\dots, c_n$$$ \u00a0\u2014 the cards in the deck. The first card is $$$c_1$$$, the second is $$$c_2$$$ and so on. It is guaranteed that for all $$$i=1,\\dots,n$$$ there is exactly one $$$j\\in\\{1,\\dots,n\\}$$$ such that $$$c_j = i$$$.", "output_spec": "On the first line, print the number $$$q$$$ of operations you perform (it must hold $$$0\\le q\\le n$$$). Then, print $$$q$$$ lines, each describing one operation. To describe an operation, print on a single line the number $$$k$$$ of parts you are going to split the deck in, followed by the size of the $$$k$$$ parts: $$$|D_1|, |D_2|, \\dots , |D_k|$$$. It must hold $$$2\\le k\\le n$$$, and $$$|D_i|\\ge 1$$$ for all $$$i=1,\\dots,k$$$, and $$$|D_1|+|D_2|+\\cdots + |D_k| = n$$$. It can be proven that it is always possible to sort the deck performing at most $$$n$$$ operations. If there are several ways to sort the deck you can output any of them.", "sample_inputs": ["4\n3 1 2 4", "6\n6 5 4 3 2 1", "1\n1"], "sample_outputs": ["2\n3 1 2 1\n2 1 3", "1\n6 1 1 1 1 1 1", "0"], "notes": "NoteExplanation of the first testcase: Initially the deck is [3 1 2 4]. The first operation splits the deck as [(3) (1 2) (4)] and then transforms it into [4 1 2 3]. The second operation splits the deck as [(4) (1 2 3)] and then transforms it into [1 2 3 4]. Explanation of the second testcase: Initially the deck is [6 5 4 3 2 1]. The first (and only) operation splits the deck as [(6) (5) (4) (3) (2) (1)] and then transforms it into [1 2 3 4 5 6]. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($n) = map { $_ - 0 } split(/\\s+/o,);\nmy @c = map { $_ - 0 } split(/\\s+/o,);\n$#c = $n - 1;\n\nmy $f = &f;\n\nif( $f == $n ){\n print \"0\\n\"; exit(0);\n}\n\nmy @dk1 = ();\nif( $c[0] != 1 and $c[$n-1] == 1 ){\n @c = reverse @c;\n push(@dk1,(1) x $n);\n}\n$f = &f;\n\nmy @dk = ();\nwhile( $f < $n ){\n my $i;\n for($i=$f;$i<$n;$i++){\n last if $c[$i] == $f+1;\n }\n \n my @c1 = reverse @c[0..($f-1)];\n my @c2 = reverse @c[$f..$i];\n my @c3 = reverse @c[($i+1)..($n-1)];\n \n my @d = ();\n push(@d,(1) x $f) if $f>0;\n push(@d,1+$#c2) if @c2;\n push(@d,1+$#c3) if @c3;\n \n if( @d != 1 ){\n push(@dk,\\@d);\n }\n @c = (@c1,@c2,@c3);\n \n $f++ while $c[$f] == 1+$f;\n}\n\nif( @dk % 2 == 1 ){\n my @d = (1) x $n;\n push(@dk,\\@d);\n}\n\nprint ( ( @dk + ( @dk1 ? 1 : 0 ) ) . \"\\n\" );\nprint ( @dk1 . \" \" . join(' ',@dk1) . \"\\n\" ) if @dk1;\nfor(my $i=0;$i<=$#dk;$i++){\n my @d = @{$dk[$i]};\n @d = reverse @d if $i % 2 == 1;\n print ( @d . \" \" . join(' ',@d) . \"\\n\");\n}\n\nexit(0);\n\nsub f {\n my $rc = 0;\n for(my $i=0;$i<$n;$i++){\n last if $c[$i] != 1+$i;\n $rc++;\n }\n return $rc;\n}\n\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($n) = map { $_ - 0 } split(/\\s+/o,);\nmy @c = map { $_ - 0 } split(/\\s+/o,);\n$#c = $n - 1;\n\nmy $f = 0;\nfor(my $i=0;$i<$n;$i++){\n last if $c[$i] != 1+$i;\n $f++;\n}\n\nif( $f == $n ){\n print \"0\\n\"; exit(0);\n}\n\nmy @dk = ();\nwhile( $f < $n ){\n my $i;\n for($i=$f;$i<$n;$i++){\n last if $c[$i] == $f+1;\n }\n \n my @c1 = reverse @c[0..($f-1)];\n my @c2 = reverse @c[$f..$i];\n my @c3 = reverse @c[($i+1)..($n-1)];\n \n my @d = ();\n push(@d,(1) x $f) if $f>0;\n push(@d,1+$#c2) if @c2;\n push(@d,1+$#c3) if @c3;\n push(@dk,\\@d);\n @c = (@c1,@c2,@c3);\n \n $f++ while $c[$f] == $f+1;\n}\n\nif( @dk % 2 == 1 ){\n my @d = (1) x $n;\n push(@dk,\\@d);\n}\n\nprint ( @dk . \"\\n\" );\nfor(my $i=0;$i<=$#dk;$i++){\n my @d = @{$dk[$i]};\n @d = reverse @d if $i % 2 == 1;\n print ( @d . \" \" . join(' ',@d) . \"\\n\");\n}\n\n\n"}], "src_uid": "07bc926194f45da75e4c534a7fd3656b"} {"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$$$) \u2014 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$$$) \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tprint scalar( () = ( <> =~ s/\\s//gr =~ s/^0+//r =~ s/0+$//r =~ /0/g ) );\n\t}"}], "negative_code": [], "src_uid": "1c07882651ef6ebfc05e777d562e28b9"} {"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\u2009\u00d7\u2009H, 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 \u2014 the j-th (1\u2009\u2264\u2009j\u2009\u2264\u2009n) 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\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000) \u2014 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,\u2009hi (1\u2009\u2264\u2009wi\u2009\u2264\u200910,\u20091\u2009\u2264\u2009hi\u2009\u2264\u20091000) \u2014 the width and height in pixels of the corresponding rectangle.", "output_spec": "Print n space-separated numbers b1,\u2009b2,\u2009...,\u2009bn, where bi \u2014 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": "#!/usr/bin/perl\nuse List::Util qw(min max);\n\nmy @w, @h, $n, $cw, $ch, $mw, $mh, $maxh1, $maxh2, $sum, $hh;\n\nmy $n = <>;\n$maxh1 = 0;\n$hh = -1;\n$sum = 0;\n\nfor (my $i = 0; $i < $n; $i++) {\n ($w[$i], $h[$i]) = split / /, <>;\n $sum += $w[$i];\n if ($h[$i] > $maxh1) {\n $hh = $i;\n $maxh1 = $h[$i];\n }\n}\n\n$maxh2 = 0;\n\nfor (my $i = 0; $i < $n; $i++) {\n if (($i != $hh) && ($h[$i] > $maxh2)) {\n $maxh2 = $h[$i];\n }\n}\n\nfor (my $i = 0; $i < $n; $i++) {\n print (($sum - $w[$i]) * (($i == $hh)?$maxh2:$maxh1));\n print ' ';\n}\n"}], "negative_code": [], "src_uid": "e1abc81cea4395ba675cf6ca93261ae8"} {"nl": {"description": "The only difference between easy and hard versions is on constraints. In this version constraints are higher. You can make hacks only if all versions of the problem are solved.Koa the Koala is at the beach!The beach consists (from left to right) of a shore, $$$n+1$$$ meters of sea and an island at $$$n+1$$$ meters from the shore.She measured the depth of the sea at $$$1, 2, \\dots, n$$$ meters from the shore and saved them in array $$$d$$$. $$$d_i$$$ denotes the depth of the sea at $$$i$$$ meters from the shore for $$$1 \\le i \\le n$$$.Like any beach this one has tide, the intensity of the tide is measured by parameter $$$k$$$ and affects all depths from the beginning at time $$$t=0$$$ in the following way: For a total of $$$k$$$ seconds, each second, tide increases all depths by $$$1$$$. Then, for a total of $$$k$$$ seconds, each second, tide decreases all depths by $$$1$$$. This process repeats again and again (ie. depths increase for $$$k$$$ seconds then decrease for $$$k$$$ seconds and so on ...).Formally, let's define $$$0$$$-indexed array $$$p = [0, 1, 2, \\ldots, k - 2, k - 1, k, k - 1, k - 2, \\ldots, 2, 1]$$$ of length $$$2k$$$. At time $$$t$$$ ($$$0 \\le t$$$) depth at $$$i$$$ meters from the shore equals $$$d_i + p[t \\bmod 2k]$$$ ($$$t \\bmod 2k$$$ denotes the remainder of the division of $$$t$$$ by $$$2k$$$). Note that the changes occur instantaneously after each second, see the notes for better understanding. At time $$$t=0$$$ Koa is standing at the shore and wants to get to the island. Suppose that at some time $$$t$$$ ($$$0 \\le t$$$) she is at $$$x$$$ ($$$0 \\le x \\le n$$$) meters from the shore: In one second Koa can swim $$$1$$$ meter further from the shore ($$$x$$$ changes to $$$x+1$$$) or not swim at all ($$$x$$$ stays the same), in both cases $$$t$$$ changes to $$$t+1$$$. As Koa is a bad swimmer, the depth of the sea at the point where she is can't exceed $$$l$$$ at integer points of time (or she will drown). More formally, if Koa is at $$$x$$$ ($$$1 \\le x \\le n$$$) meters from the shore at the moment $$$t$$$ (for some integer $$$t\\ge 0$$$), the depth of the sea at this point \u00a0\u2014 $$$d_x + p[t \\bmod 2k]$$$ \u00a0\u2014 can't exceed $$$l$$$. In other words, $$$d_x + p[t \\bmod 2k] \\le l$$$ must hold always. Once Koa reaches the island at $$$n+1$$$ meters from the shore, she stops and can rest.Note that while Koa swims tide doesn't have effect on her (ie. she can't drown while swimming). Note that Koa can choose to stay on the shore for as long as she needs and neither the shore or the island are affected by the tide (they are solid ground and she won't drown there). Koa wants to know whether she can go from the shore to the island. Help her!", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) \u00a0\u2014 the number of test cases. Description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k$$$ and $$$l$$$ ($$$1 \\le n \\le 3 \\cdot 10^5; 1 \\le k \\le 10^9; 1 \\le l \\le 10^9$$$)\u00a0\u2014 the number of meters of sea Koa measured and parameters $$$k$$$ and $$$l$$$. The second line of each test case contains $$$n$$$ integers $$$d_1, d_2, \\ldots, d_n$$$ ($$$0 \\le d_i \\le 10^9$$$) \u00a0\u2014 the depths of each meter of sea Koa measured. 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 Yes if Koa can get from the shore to the island, and No otherwise. You may print each letter in any case (upper or lower).", "sample_inputs": ["7\n2 1 1\n1 0\n5 2 3\n1 2 3 2 2\n4 3 4\n0 2 4 3\n2 3 5\n3 0\n7 2 3\n3 0 2 1 3 0 1\n7 1 4\n4 4 3 0 2 4 2\n5 2 3\n1 2 3 2 2"], "sample_outputs": ["Yes\nNo\nYes\nYes\nYes\nNo\nNo"], "notes": "NoteIn the following $$$s$$$ denotes the shore, $$$i$$$ denotes the island, $$$x$$$ denotes distance from Koa to the shore, the underline denotes the position of Koa, and values in the array below denote current depths, affected by tide, at $$$1, 2, \\dots, n$$$ meters from the shore.In test case $$$1$$$ we have $$$n = 2, k = 1, l = 1, p = [ 0, 1 ]$$$.Koa wants to go from shore (at $$$x = 0$$$) to the island (at $$$x = 3$$$). Let's describe a possible solution: Initially at $$$t = 0$$$ the beach looks like this: $$$[\\underline{s}, 1, 0, i]$$$. At $$$t = 0$$$ if Koa would decide to swim to $$$x = 1$$$, beach would look like: $$$[s, \\underline{2}, 1, i]$$$ at $$$t = 1$$$, since $$$2 > 1$$$ she would drown. So Koa waits $$$1$$$ second instead and beach looks like $$$[\\underline{s}, 2, 1, i]$$$ at $$$t = 1$$$. At $$$t = 1$$$ Koa swims to $$$x = 1$$$, beach looks like $$$[s, \\underline{1}, 0, i]$$$ at $$$t = 2$$$. Koa doesn't drown because $$$1 \\le 1$$$. At $$$t = 2$$$ Koa swims to $$$x = 2$$$, beach looks like $$$[s, 2, \\underline{1}, i]$$$ at $$$t = 3$$$. Koa doesn't drown because $$$1 \\le 1$$$. At $$$t = 3$$$ Koa swims to $$$x = 3$$$, beach looks like $$$[s, 1, 0, \\underline{i}]$$$ at $$$t = 4$$$. At $$$t = 4$$$ Koa is at $$$x = 3$$$ and she made it! We can show that in test case $$$2$$$ Koa can't get to the island."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nmy $bg = 10000000000;\n\nmy $n;\nmy $k;\nmy $l;\nmy @d;\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n ($n,$k,$l) = map { $_ - 0 } split(/\\s+/,);\n @d = map { $_ - $l } split(/\\s+/,);\n \n #print ( join(\":\",@d) . \"\\n\");\n \n my $i;\n for( $i=0; $i<$n; $i++){\n last if( $d[$i] > 0 );\n }\n if( $i<$n ){\n print \"No\\n\"; next;\n }\n \n my $pr = -1;\n my $no = undef;\n for( $i=0; $i<$n; $i++){\n if( $d[$i] + $k <= 0 ){\n #print \"d i =$d[$i] , k=$k\\n\";\n if( &sim($pr,$i) ){\n $no = 1;\n last;\n }\n $pr = $i;\n }\n }\n my $res = 'Yes';\n $res = 'No' if ( $no or &sim($pr,$n) );\n print \"$res\\n\";\n}\n\nexit(0);\n\nsub dd {\n my $idx = shift;\n return -$bg if $idx < 0 or $idx >= $n;\n return $d[$idx];\n}\n\nsub sim {\n my $i1 = shift;\n my $i2 = shift;\n #print \"i1=$i1 , i2=$i2\\n\";\n \n return undef if ( $i2 == $i1 + 1 );\n \n my $dir = -1;\n my $i = $i1;\n my $adp = $k;\n \n while($i < $i2 - 1){\n $i++;\n my $cdp = - &dd($i);\n if( $dir == -1 ){\n $adp = &min( $cdp , $adp - 1 );\n $dir = 1 if $adp == 0;\n } else {\n $adp += 1;\n if( $adp > $k or $adp > $cdp ){\n return 1;\n }\n }\n }\n \n return undef;\n}\n\nsub min {\n my $x = shift; my $y = shift;\n return ( $x < $y ? $x : $y );\n}\n"}], "negative_code": [], "src_uid": "7671925bc31f80b5ff875dd285d85d32"} {"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$$$) \u2014 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": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my @x = map { $_ - 0 } split(/\\s+/,);\n $#x = 2;\n my @sx = sort { $b <=> $a } @x;\n \n if( $sx[0] != $sx[1] ){\n print \"NO\\n\";\n next;\n }\n \n my $a = $sx[0];\n my $b = $sx[2];\n print \"YES\\n\";\n print \"$a $b $b\\n\";\n \n}\n\nexit(0);\n\n"}, {"source_code": "chomp(my $t = );\nforeach (1..$t)\n{\n my @a = split /\\s+/, ;\n my @x = sort { $b <=> $a } @a;\n if ($x[0] != $x[1])\n {\n print \"NO\\n\";\n }\n else\n {\n my ($a, $b) = ($x[0], $x[2]);\n print \"YES\\n$a $b $b\\n\";\n }\n}"}], "negative_code": [{"source_code": "chomp(my $t = );\nprint $t;\nforeach (1..$t)\n{\n my @a = split /\\s+/, ;\n my @x = sort { $b <=> $a } @a;\n if ($x[0] != $x[1])\n {\n print \"NO\\n\";\n }\n else\n {\n my ($a, $b) = ($x[0], $x[2]);\n print \"YES\\n$a $b $b\\n\";\n }\n}"}], "src_uid": "f4804780d9c63167746132c35b2bdd02"} {"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)$$$ \u2014 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)$$$) \u2014 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": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n\nmy ($n, $k) = split q{ }, $line;\n\nmy @inputs = split q{ }, ;\n\nmy $set = {};\nmy @answer = ();\n\nfor my $input ( @inputs ) {\n if ( defined $set->{$input} ) {\n next;\n }\n else {\n if ( @answer == $k ) {\n my $id = shift @answer;\n delete $set->{$id};\n }\n push @answer, $input;\n $set->{$input} = 1;\n }\n}\n\nsay scalar @answer;\n@answer = reverse @answer;\nsay \"@answer\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\tmy @q;\n\t\n\tfor( @_ ){\n\t\tnext if exists $h{ $_ };\n\t\t\n\t\tif( $k == @q ){\n\t\t\tmy $del = shift @q;\n\t\t\tdelete $h{ $del };\n\t\t\t}\n\t\t\n\t\tpush @q, $_;\n\t\t$h{ $_ } = 1;\n\t\t}\n\t\n\tprint ~~ @q;\n\tprint join ' ', reverse @q;\n\t}"}, {"source_code": "#!/usr/bin/env perl\n\nmy ($messages, $limit) = split /\\s/, <>;\nmy @IDs = split /\\s/, <>;\nmy @shown = ();\n\nfor my $id (@IDs) {\n unless (grep { $_ == $id } @shown ) {\n pop @shown unless @shown < $limit;\n unshift @shown, $id;\n }\n}\n\n$\\ = \"\\n\";\nprint scalar @shown;\nprint join(\" \", @shown);"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\tmy @q;\n\t\n\tfor( @_ ){\n\t\tnext if exists $h{ $_ };\n\t\t\n\t\tif( $k == @q ){\n\t\t\tmy $del = shift @q;\n\t\t\tdelete $h{ $del };\n\t\t\t}\n\t\t\n\t\tpush @q, $_;\n\t\t$h{ $_ } = 1;\n\t\t}\n\t\n\tprint ~~ @q;\n\tprint join ' ', reverse @q;\n\t}"}], "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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 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": "<>;\n\n$M = ~0;\n\nwhile( <> ){\n\t( $A, $B ) = sort { $b <=> $a } split;\n\t$A = $B if $A > $M;\n\t$f += $A > $M;\n\t$M = $A;\n\t}\n\nprint $f ? \"NO\" : \"YES\""}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy $max = ~0;\n\tmy $f = 0;\n\t\n\tfor( 1 .. $_ ){\n\t\tmy( $A, $B ) = sort { $b <=> $a } split ' ', <>;\n\t\tif( $A > $max ){ $A = $B; }\n\t\tif( $A > $max ){ $f ++; }\n\t\t$max = $A;\n\t\t}\n\t\n\tprint $f ? \"NO\" : \"YES\";\n\t}"}], "negative_code": [], "src_uid": "162fa942bc6eeb5164b19598da2f8bef"} {"nl": {"description": "You are given a permutation $$$a$$$ of size $$$n$$$ and you should perform $$$n$$$ operations on it. In the $$$i$$$-th operation, you can choose a non-empty suffix of $$$a$$$ and increase all of its elements by $$$i$$$. How can we perform the operations to minimize the number of inversions in the final array?Note that you can perform operations on the same suffix any number of times you want.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. A suffix is several consecutive elements of an array that include the last element of the array. An inversion in an array $$$a$$$ is a pair of indices $$$(i, j)$$$ such that $$$i > j$$$ and $$$a_{i} < a_{j}$$$.", "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 a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) \u2014 the size of the array. The second line contains $$$n$$$ distinct integers $$$a_{1}, a_{2}, \\dots, a_{n}$$$ ($$$1 \\le a_i \\le n$$$), the initial permutation $$$a$$$. It's 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 $$$x_{1}, x_{2}, \\ldots, x_{n}$$$ ($$$1 \\le x_{i} \\le n$$$ for each $$$1 \\le i \\le n$$$) indicating that the $$$i$$$-th operation must be applied to the suffix starting at index $$$x_{i}$$$. If there are multiple answers, print any of them.", "sample_inputs": ["4\n\n4\n\n1 2 3 4\n\n5\n\n1 3 2 4 5\n\n3\n\n2 3 1\n\n1\n\n1"], "sample_outputs": ["1 1 1 1\n1 4 3 2 1\n1 3 3\n1"], "notes": "NoteIn the first test case one of the optimal solutions is to increase the whole array on each operation (that is, choose the suffix starting at index $$$1$$$). The final array $$$[11, 12, 13, 14]$$$ contains $$$0$$$ inversions.In the second test case, $$$a$$$ will be equal to $$$[2, 4, 3, 5, 6]$$$, $$$[2, 4, 3, 7, 8]$$$, $$$[2, 4, 6, 10, 11]$$$, $$$[2, 8, 10, 14, 15]$$$ and $$$[7, 13, 15, 19, 20]$$$ after the first, second, third, fourth, and fifth operations, respectively. So the final array $$$a$$$ has zero inversions."}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse Data::Dumper;\nuse integer; ### important!\nmy $mod = 10 ** 9 + 7;\n# my $mod = 998244353;\n\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\n\nwhile( $testcase -- > 0 ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @A = map { $_ - 0 } split(/\\s+/,);\n \n if( $n==1 ){\n print \"1\\n\"; next;\n }\n \n my @dg = ();\n for(my $i=0;$i<$n-1;$i++){\n my $dg1 = $A[$i]-$A[$i+1];\n $dg1 = 0 if $dg1 < 0;\n push(@dg,+[$i,$dg1]);\n }\n my @dg2 = sort { $b->[1] <=> $a->[1] } @dg;\n my @res = (); $#res = $n - 1;\n for(my $i=0;$i<$n-1;$i++){\n $res[$n-1-$i] = $dg2[$i]->[0] + 2;\n }\n $res[0] = 1;\n print ( join(' ',@res) . \"\\n\" );\n}\n\nexit(0);\n\nsub max {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\n return $r;\n}\nsub min {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\n return $r;\n}\nsub sum {\n my $r = 0;\n foreach my $e (@_){ $r += $e; }\n return $r;\n}\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\n my $r = shift; my $ar = shift;\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\n for(my $i=0;$i<=$#ar;$i++){ \n $rr += $ar->[$i];\n $r->[1+$i] = $rr;\n }\n}\n"}], "negative_code": [], "src_uid": "188c9dbb3e1851b7b762ed6b4b23d1bd"} {"nl": {"description": "Polycarpus has been working in the analytic department of the \"F.R.A.U.D.\" company for as much as n days. Right now his task is to make a series of reports about the company's performance for the last n days. We know that the main information in a day report is value ai, the company's profit on the i-th day. If ai is negative, then the company suffered losses on the i-th day.Polycarpus should sort the daily reports into folders. Each folder should include data on the company's performance for several consecutive days. Of course, the information on each of the n days should be exactly in one folder. Thus, Polycarpus puts information on the first few days in the first folder. The information on the several following days goes to the second folder, and so on.It is known that the boss reads one daily report folder per day. If one folder has three or more reports for the days in which the company suffered losses (ai\u2009<\u20090), he loses his temper and his wrath is terrible.Therefore, Polycarpus wants to prepare the folders so that none of them contains information on three or more days with the loss, and the number of folders is minimal.Write a program that, given sequence ai, will print the minimum number of folders.", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100), n is the number of days. The second line contains a sequence of integers a1,\u2009a2,\u2009...,\u2009an (|ai|\u2009\u2264\u2009100), where ai means the company profit on the i-th day. It is possible that the company has no days with the negative ai.", "output_spec": "Print an integer k \u2014 the required minimum number of folders. In the second line print a sequence of integers b1, b2, ..., bk, where bj is the number of day reports in the j-th folder. If there are multiple ways to sort the reports into k days, print any of them.", "sample_inputs": ["11\n1 2 3 -4 -5 -6 5 -5 -6 -7 6", "5\n0 -1 100 -1 0"], "sample_outputs": ["3\n5 3 3", "1\n5"], "notes": "NoteHere goes a way to sort the reports from the first sample into three folders: 1 2 3 -4 -5 | -6 5 -5 | -6 -7 6 In the second sample you can put all five reports in one folder."}, "positive_code": [{"source_code": "use strict;\nuse warnings;\n<>;\nmy $s = <>;\nchomp $s;\nmy @a = map int, split /\\s/, $s;\nmy ($i, $c, $n) = (0, 0, 0);\nmy @answer = ();\nfor (@a) {\n if ($_ < 0) {\n\tif ($c == 2) {\n\t push @answer, $i;\n\t $n++;\n\t $c = 1;\n\t $i = 1;\n\t} else {\n\t $c++;\n\t $i++;\n\t}\n } else {\n\t$i++;\n }\n}\npush @answer, $i;\n$n++;\nprint \"$n\\n@answer\";\n"}], "negative_code": [], "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$$$) \u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n \r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n my $l = length($s);\r\n my $cn = 0;\r\n my $pr = '';\r\n my $res = 'YES';\r\n for(my $i=0;$i<$l;$i++){\r\n my $c1 = substr($s,$i,1);\r\n if( $pr ne $c1 ){\r\n if( $pr ne '' and $cn <= 1 ){\r\n $res = 'NO';\r\n last;\r\n }\r\n $cn = 1;\r\n } else {\r\n $cn++;\r\n }\r\n $pr = $c1;\r\n }\r\n $res = 'NO' if $res eq 'YES' and $cn <= 1;\r\n print \"$res\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $zero = () = m/0/g;\n\tmy $one = () = m/1/g;\n\t\n\tif( $zero > $one ){\n\t\tprint 0 + $one;\n\t\t}\n\telsif( $zero < $one ){\n\t\tprint 0 + $zero;\n\t\t}\n\telse{\n\t\tprint $zero - 1;\n\t\t}\n\t}"}], "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)$$$ \u2014 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)$$$ \u2014 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$$$ \u2014 minimum number of operations required. Then ouput a line consist of $$$n$$$ integers \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $cnt = 0;\n\t\n\tfor my $i ( 1 .. @_ - 2 ){\n\t\tif( $_[ $i - 1 ] < $_[ $i ] and $_[ $i ] > $_[ $i + 1 ] ){\n\t\t\tif( $i < @_ - 2 and $_[ $i ] < $_[ $i + 2 ] ){\n\t\t\t\t$_[ $i + 1 ] = $_[ $i + 2 ];\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$_[ $i + 1 ] = $_[ $i ];\n\t\t\t\t}\n\t\t\t$cnt ++;\n\t\t\t}\n\t\t}\n\t\n\tprint for $cnt, \"@_\";\n\t}"}], "negative_code": [], "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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m, $k ) = split;\n\t@_ = split ' ', <>;\n\t\n\tmy( $max, $smax) = ( sort { $b <=> $a } @_ )[ 0, 1 ];\n\t\n\tmy $int = int $m / ( $k + 1 );\n\t$m -= $int * ( $k + 1 );\n\t\n\tprint $int * ( $k * $max + $smax ) + $m * $max;\n\t}"}], "negative_code": [], "src_uid": "96dee17800e147350bd37e60f66f49dd"} {"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)$$$ \u2014 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": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy $n=;\nmy @in=split/ /,;chomp($n,$in[-1]);\nmy @p=()x100;my $r=join ' ',@in;my $q=0;my $t=0;\nfor my $j(1..100){\n $p[$j]=($r=~s/\\b$j\\b//g);\n ++$q if($p[$j]==1);\n $t=$j if($p[$j]>=3);\n}\nmy $l=0;\nmy %h=();\nif($q%2==0){\n print \"YES\\n\";\n for my $i(0..$#in){\n\tif($p[$in[$i]]!=1){$h{$i}=\"B\";}\n\telse{\n\t if($l<$q/2){\n\t\t$h{$i}=\"A\";\n\t\t$l++;\n\t }\n\t else {$h{$i}= \"B\"};\n\t}\n }\n print $h{$_} for(0..$#in);\n}\nelse{\n if($t!=0){\n\tprint \"YES\\n\";\n\t$q++;\n\tmy $set=\"o\";\n\tfor my $i(0..$#in){\n\t if($t==$in[$i]){\n\t\tif($set eq \"o\"){\n\t\t $h{$i}=\"A\" if($l<$q/2);\n\t\t $h{$i}=\"B\" if($l>=$q/2);\n\t\t $l++;\n\t\t $set=$h{$i};\n\t\t}\n\t\telse{\n\t\t $h{$i}=($set eq \"A\")?\"B\":\"A\";\n\t\t}\n\t }\n\t else{\n\t\tif($p[$in[$i]]!=1){$h{$i}=\"B\";}\n\t\telse{\n\t\t if($l<$q/2){\n\t\t\t$h{$i}= \"A\";\n\t\t\t$l++;\n\t\t }\n\t\t else{$h{$i}= \"B\";}\n\t\t}\n\t }\n\t}\n\tprint $h{$_} for(0..$#in);\n }\n else{print \"NO\";}\n}\nprint \"\\n\";"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\t$h{ $_ } ++ for @_;\n\t\n\tmy %ones;\n\tmy %twos;\n\tmy %mores;\n\t\n\tfor( keys %h ){\n\t\tif( $h{ $_ } == 1 ){\n\t\t\t$ones{ $_ } = 1;\n\t\t\t}\n\t\telsif( $h{ $_ } == 2 ){\n\t\t\t$twos{ $_ } = 1;\n\t\t\t}\n\t\telse{\n\t\t\t$mores{ $_ } = 1;\n\t\t\t}\n\t\t}\n\t\n\tmy %convert;\n\t\n\tfor( keys %twos ){\n\t\t$convert{ $_ } = 'A';\n\t\t}\n\t\n\tfor( keys %mores ){\n\t\t$convert{ $_ } = 'A';\n\t\t}\n\t\n\tif( ( keys %ones ) % 2 == 1 and 0 == keys %mores ){\n\t\tprint \"NO\";\n\t\t}\n\telse{\n\t\tif( ( keys %ones ) % 2 == 1 ){\n\t\t\tmy $to_split = ( sort { $a <=> $b } keys %mores )[ 0 ];\n\t\t\t\n\t\t\tfor( @_ ){\n\t\t\t\t$_ == $to_split and do { $_ = 0 ; last; };\n\t\t\t\t}\n\t\t\t$ones{ 0 } = 1;\n\t\t\t}\n\t\t\n\t\tmy @ones = ( sort { $a <=> $b } keys %ones );\n\t\t\n\t\tfor my $i ( 0 .. @ones - 1 ){\n\t\t\t$convert{ $ones[ $i ] } = $i % 2 ? 'A' : 'B';\n\t\t\t}\n\t\t\n\t\tmy $ans = '';\n\t\t\n\t\tfor( @_ ){\n\t\t\t$ans .= $convert{ $_ };\n\t\t\t}\n\t\t\n\t\tprint \"YES\";\n\t\tprint $ans;\n\t\t}\n\t\n\t}"}, {"source_code": "<>;\n\n$_ = <>;\n\n0 while 0 ||\n\ts/\\b(\\d+)A\\b.*?\\b\\1\\b\\K/A/ ||\n\ts/\\b(\\d+)\\b\\K(.*?\\b\\1\\b)/A$2/ ||\n\t0;\n\n( () = /\\d\\b/g ) % 2 and s/\\b(\\d+)A\\b.*\\b\\1A\\b.*\\b\\1\\KA\\b/B/ || $f ++;\n\n0 while 0 ||\n\ts/\\b(\\d+)\\b\\K/ qw( A B )[ $i ++ % 2 ] /e ||\n\t0;\n\nprint $f ? \"NO\\n\" : \"YES\\n\" . y/ 0-9//dr"}], "negative_code": [{"source_code": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy $n=;\nmy @in=split/ /,;chomp($n,$in[-1]);\nmy @p=()x100;my $r=join ' ',@in;my $q=0;my $t=0;\nfor my $j(1..100){\n $p[$j]=($r=~s/\\b$j\\b//g);\n ++$q if($p[$j]==1);\n $t=1 if($p[$j]>=3);\n}\nmy $l=0;\nif($q%2==0){\n print \"YES\\n\";\n for my $i(0..$#in){\n\tif($l<$q/2){\n\t print \"A\";\n\t $l++ if($p[$in[$i]]==1);\n\t}\n\telse {print \"B\"};\n }\n}\nelse{\n if($t==1){\n\tprint \"YES\\n\";\n\t$q++;\n\tfor my $i(0..$#in){\n\t if($l<$q/2){\n\t\tprint \"A\";\n\t\t$l++ if($p[$in[$i]]==1);\n\t\tif($p[$in[$i]]>=3&&$t==1){\n\t\t $l++;\n\t\t $t=0;\n\t\t}\n\t }\n\t else{print \"B\";}\n\t}\n }\n else{print \"NO\";}\n}\nprint \"\\n\";"}, {"source_code": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy $n=;\nmy @in=split/ /,;chomp($n,$in[-1]);\nmy @p=()x100;my $r=join ' ',@in;my $q=0;my $t=0;\nfor my $j(1..100){\n $p[$j]=($r=~s/\\b$j\\b//g);\n ++$q if($p[$j]==1);\n $t=$j if($p[$j]>=3);\n}\nmy $l=0;\nmy %h=();\nif($q%2==0){\n print \"YES\\n\";\n for my $i(0..$#in){\n\tif($p[$in[$i]]!=1){$h{$i}=\"B\";}\n\telse{\n\t if($l<$q/2){\n\t\t$h{$i}=\"A\";\n\t\t$l++;\n\t }\n\t else {$h{$i}= \"B\"};\n\t}\n }\n print $h{$_} for(0..$#in);\n}\nelse{\n if($t!=0){\n\tprint \"YES\\n\";\n\t$q++;\n\tmy $set=0;\n\tfor my $i(0..$#in){\n\t if($t==$in[$i]){\n\t\tif($set==0){\n\t\t $h{$i}=\"A\" if($l<$q/2);\n\t\t $h{$i}=\"B\" if($l>=$q/2);\n\t\t $l++;\n\t\t $set=$h{$i};\n\t\t}\n\t\telse{\n\t\t $h{$i}=($set eq \"A\")?\"B\":\"A\";\n\t\t}\n\t }\n\t else{\n\t\tif($p[$in[$i]]!=1){$h{$i}=\"B\";}\n\t\telse{\n\t\t if($l<$q/2){\n\t\t\t$h{$i}= \"A\";\n\t\t\t$l++;\n\t\t }\n\t\t else{$h{$i}= \"B\";}\n\t\t}\n\t }\n\t}\n\tprint $h{$_} for(0..$#in);\n }\n else{print \"NO\";}\n}\nprint \"\\n\";"}, {"source_code": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy $n=;\nmy @in=split/ /,;chomp($n,$in[-1]);\nmy @p=()x100;my $r=join ' ',@in;my $q=0;my $t=0;\nfor my $j(1..100){\n $p[$j]=($r=~s/\\b$j\\b//g);\n ++$q if($p[$j]==1);\n $t=1 if($p[$j]>=3);\n}\nmy $l=0;\nmy %h=();\nif($q%2==0){\n print \"YES\\n\";\n for my $i(0..$#in){\n\tif($p[$in[$i]]!=1){$h{$i}=\"B\";}\n\telse{\n\t if($l<$q/2){\n\t\t$h{$i}=\"A\";\n\t\t$l++;\n\t }\n\t else {$h{$i}= \"B\"};\n\t}\n }\n print values %h;\n}\nelse{\n if($t==1){\n\tprint \"YES\\n\";\n\t$q++;\n\tfor my $i(0..$#in){\n\t if($t==1&&$p[$in[$i]]>=3){$h{$i}=\"A\";$t=0;$l++;}\n\t else{\n\t\tif($p[$in[$i]]!=1){$h{$i}=\"B\";}\n\t\telse{\n\t\t if($l<$q/2){\n\t\t\t$h{$i}= \"A\";\n\t\t\t$l++;\n\t\t }\n\t\t else{$h{$i}= \"B\";}\n\t\t}\n\t }\n\t}\n\tprint values %h;\n }\n else{print \"NO\";}\n}\nprint \"\\n\";"}, {"source_code": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy $n=;\nmy @in=split/ /,;chomp($n,$in[-1]);\nmy @p=()x100;my $r=join ' ',@in;my $q=0;my $t=0;\nfor my $j(1..100){\n $p[$j]=($r=~s/\\b$j\\b//g);\n ++$q if($p[$j]==1);\n $t=1 if($p[$j]>=3);\n}\nmy $l=0;\nmy %h=();\nif($q%2==0){\n print \"YES\\n\";\n for my $i(0..$#in){\n\tif($p[$in[$i]]!=1){$h{$i}=\"B\";}\n\telse{\n\t if($l<$q/2){\n\t\t$h{$i}=\"A\";\n\t\t$l++;\n\t }\n\t else {$h{$i}= \"B\"};\n\t}\n }\n print $h{$_} for(0..$#in);\n}\nelse{\n if($t==1){\n\tprint \"YES\\n\";\n\t$q++;\n\tfor my $i(0..$#in){\n\t if($t==1&&$p[$in[$i]]>=3){\n\t\t$h{$i}=\"A\" if($l<$q/2);\n\t\t$h{$i}=\"B\" if($l>=$q/2);\n\t\t$t=0;$l++;\n\t }\n\t else{\n\t\tif($p[$in[$i]]!=1){$h{$i}=\"B\";}\n\t\telse{\n\t\t if($l<$q/2){\n\t\t\t$h{$i}= \"A\";\n\t\t\t$l++;\n\t\t }\n\t\t else{$h{$i}= \"B\";}\n\t\t}\n\t }\n\t}\n\tprint $h{$_} for(0..$#in);\n }\n else{print \"NO\";}\n}\nprint \"\\n\";"}, {"source_code": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy $n=;\nmy @in=split/ /,;chomp($n,$in[-1]);\nmy @p=()x100;my $r=join ' ',@in;my $q=0;my $t=0;\nfor my $j(1..100){\n $p[$j]=($r=~s/\\b$j\\b//g);\n ++$q if($p[$j]==1);\n $t=$j if($p[$j]>=3);\n}\nmy $l=0;\nmy %h=();\nif($q%2==0){\n print \"YES\\n\";\n for my $i(0..$#in){\n\tif($p[$in[$i]]!=1){$h{$i}=\"B\";}\n\telse{\n\t if($l<$q/2){\n\t\t$h{$i}=\"A\";\n\t\t$l++;\n\t }\n\t else {$h{$i}= \"B\"};\n\t}\n }\n print $h{$_} for(0..$#in);\n}\nelse{\n if($t!=0){\n\tprint \"YES\\n\";\n\t$q++;\n\tmy $set=0;\n\tfor my $i(0..$#in){\n\t if($t==$in[$i]){\n\t\tif($set==0){\n\t\t $h{$i}=\"A\" if($l<$q/2);\n\t\t $h{$i}=\"B\" if($l>=$q/2);\n\t\t $l++;\n\t\t $set=1;\n\t\t}\n\t\telse{\n\t\t $h{$i}=\"B\" if($l<$q/2);\n\t\t $h{$i}=\"A\" if($l>=$q/2);\n\t\t}\n\t }\n\t else{\n\t\tif($p[$in[$i]]!=1){$h{$i}=\"B\";}\n\t\telse{\n\t\t if($l<$q/2){\n\t\t\t$h{$i}= \"A\";\n\t\t\t$l++;\n\t\t }\n\t\t else{$h{$i}= \"B\";}\n\t\t}\n\t }\n\t}\n\tprint $h{$_} for(0..$#in);\n }\n else{print \"NO\";}\n}\nprint \"\\n\";"}, {"source_code": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy $n=;\nmy @in=split/ /,;chomp($n,$in[-1]);\nmy @p=()x100;my $r=join ' ',@in;my $q=0;my $t=0;\nfor my $j(1..100){\n $p[$j]=($r=~s/\\b$j\\b//g);\n ++$q if($p[$j]==1);\n $t=1 if($p[$j]>=3);\n}\nmy $l=0;\nmy %h=();\nif($q%2==0){\n print \"YES\\n\";\n for my $i(0..$#in){\n\tif($p[$in[$i]]!=1){$h{$i}=\"B\";}\n\telse{\n\t if($l<$q/2){\n\t\t$h{$i}=\"A\";\n\t\t$l++;\n\t }\n\t else {$h{$i}= \"B\"};\n\t}\n }\n print $h{$_} for(0..$#in);\n}\nelse{\n if($t==1){\n\tprint \"YES\\n\";\n\t$q++;\n\tfor my $i(0..$#in){\n\t if($t==1&&$p[$in[$i]]>=3){$h{$i}=\"A\";$t=0;$l++;}\n\t else{\n\t\tif($p[$in[$i]]!=1){$h{$i}=\"B\";}\n\t\telse{\n\t\t if($l<$q/2){\n\t\t\t$h{$i}= \"A\";\n\t\t\t$l++;\n\t\t }\n\t\t else{$h{$i}= \"B\";}\n\t\t}\n\t }\n\t}\n\tprint $h{$_} for(0..$#in);\n }\n else{print \"NO\";}\n}\nprint \"\\n\";"}, {"source_code": "#!usr/bin/perl -w\nuse strict;\nuse integer;\n\nmy $n=;\nmy @in=split/ /,;chomp($n,$in[-1]);\nmy @p=()x100;my $r=join ' ',@in;my $q=0;my $t=0;\nfor my $j(1..100){\n $p[$j]=($r=~s/\\b$j\\b//g);\n ++$q if($p[$j]==1);\n $t=1 if($p[$j]>=3);\n}\nif($q%2==0){\n print \"YES\\n\";\n for my $i(0..$#in){\n\tif($q>=$n/2){\n\t print \"A\";\n\t $q-- if($p[$in[$i]]==1);\n\t}\n\telse {print \"B\"};\n }\n}\nelse{\n if($t==1){\n\tprint \"YES\\n\";\n\t$q++;\n\tfor my $i(0..$#in){\n\t if($q>=$n/2){\n\t\tprint \"A\";\n\t\t$q-- if($p[$in[$i]]==1);\n\t\tif($p[$in[$i]]>=3&&$t==1){\n\t\t $q--;\n\t\t $t=0;\n\t\t}\n\t }\n\t else{print \"B\";}\n\t}\n }\n else{print \"NO\";}\n}\nprint \"\\n\";"}], "src_uid": "d126ef6b94e9ab55624cf7f2a96c7ed1"} {"nl": {"description": "A permutation is a sequence of $$$n$$$ integers from $$$1$$$ to $$$n$$$, in which all 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 presented with a permutation $$$p$$$ of numbers from $$$1$$$ to $$$n$$$. However, when Polycarp came home, he noticed that in his pocket, the permutation $$$p$$$ had turned into an array $$$q$$$ according to the following rule: $$$q_i = \\max(p_1, p_2, \\ldots, p_i)$$$. Now Polycarp wondered what lexicographically minimal and lexicographically maximal permutations could be presented to him.An array $$$a$$$ of length $$$n$$$ is lexicographically smaller than an array $$$b$$$ of length $$$n$$$ if there is an index $$$i$$$ ($$$1 \\le i \\le n$$$) such that the first $$$i-1$$$ elements of arrays $$$a$$$ and $$$b$$$ are the same, and the $$$i$$$-th element of the array $$$a$$$ is less than the $$$i$$$-th element of the array $$$b$$$. For example, the array $$$a=[1, 3, 2, 3]$$$ is lexicographically smaller than the array $$$b=[1, 3, 4, 2]$$$.For example, if $$$n=7$$$ and $$$p=[3, 2, 4, 1, 7, 5, 6]$$$, then $$$q=[3, 3, 4, 4, 7, 7, 7]$$$ and the following permutations could have been as $$$p$$$ initially: $$$[3, 1, 4, 2, 7, 5, 6]$$$ (lexicographically minimal permutation); $$$[3, 1, 4, 2, 7, 6, 5]$$$; $$$[3, 2, 4, 1, 7, 5, 6]$$$; $$$[3, 2, 4, 1, 7, 6, 5]$$$ (lexicographically maximum permutation). For a given array $$$q$$$, find the lexicographically minimal and lexicographically maximal permutations that could have been originally presented to Polycarp.", "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 $$$q_1, q_2, \\ldots, q_n$$$ ($$$1 \\le q_i \\le n$$$). It is guaranteed that the array $$$q$$$ was obtained by applying the rule from the statement to some permutation $$$p$$$. 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 two lines: on the first line output $$$n$$$ integers\u00a0\u2014 lexicographically minimal permutation that could have been originally presented to Polycarp; on the second line print $$$n$$$ integers\u00a0\u2014 lexicographically maximal permutation that could have been originally presented to Polycarp; ", "sample_inputs": ["4\n7\n3 3 4 4 7 7 7\n4\n1 2 3 4\n7\n3 4 5 5 5 7 7\n1\n1"], "sample_outputs": ["3 1 4 2 7 5 6 \n3 2 4 1 7 6 5 \n1 2 3 4 \n1 2 3 4 \n3 4 5 1 2 7 6 \n3 4 5 2 1 7 6 \n1 \n1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy %maxes;\n\t\n\tmap $maxes{ $_ } ++, @_;\n\t\n\tmy %cycle;\n\t\n\texists $maxes{ $_ } or $cycle{ $_ } ++ for 1 .. @_;\n\t\n\tmy @cycle = sort { $a <=> $b } keys %cycle;\n\t\n\t$debug and print \"[@cycle]\";\n\t\n\tmy @copy_cycle = @cycle;\n\t\n\tmy @lex_min;\n\t\n\tmy %used;\n\t\n\tfor( @_ ){\n\t\tif( exists $used{ $_ } ){\n\t\t\tpush @lex_min, shift @copy_cycle;\n\t\t\t}\n\t\telse{\n\t\t\tpush @lex_min, $_;\n\t\t\t$used{ $_ } ++;\n\t\t\t}\n\t\t}\n\t\n\tprint \"@lex_min\";\n\t\n\tmy @r_cycle = reverse @cycle;\n\t\n\tmy @lex_max;\n\t\n\t%used = ();\n\t\n\tfor( @_ ){\n\t\t$debug and print \" [$_][@r_cycle]\";\n\t\t\n\t\tif( exists $used{ $_ } ){\n\t\t\twhile( 1 ){\n\t\t\t\tmy $cand = shift @r_cycle;\n\t\t\t\tif( $cand > $_ ){\n\t\t\t\t\tpush @r_cycle, $cand;\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tpush @lex_max, $cand;\n\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\telse{\n\t\t\tpush @lex_max, $_;\n\t\t\t$used{ $_ } ++;\n\t\t\t\n\t\t\tmy $c = @r_cycle;\n\t\t\t\n\t\t\tnext if @r_cycle < 2;\n\t\t\t\n\t\t\twhile( $r_cycle[ 0 ] < $r_cycle[ -1 ] and $r_cycle[ -1 ] < $_ ){\n\t\t\t\tunshift @r_cycle, pop @r_cycle;\n\t\t\t\tlast if $c -- < 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint \"@lex_max\";\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy %maxes;\n\t\n\tmap $maxes{ $_ } ++, @_;\n\t\n\tmy %cycle;\n\t\n\texists $maxes{ $_ } or $cycle{ $_ } ++ for 1 .. @_;\n\t\n\tmy @cycle = sort { $a <=> $b } keys %cycle;\n\t\n\t$debug and print \"[@cycle]\";\n\t\n\tmy @copy_cycle = @cycle;\n\t\n\tmy @lex_min;\n\t\n\tmy %used;\n\t\n\tfor( @_ ){\n\t\tif( exists $used{ $_ } ){\n\t\t\tpush @lex_min, shift @copy_cycle;\n\t\t\t}\n\t\telse{\n\t\t\tpush @lex_min, $_;\n\t\t\t$used{ $_ } ++;\n\t\t\t}\n\t\t}\n\t\n\tprint \"@lex_min\";\n\t\n\tmy @r_cycle = reverse @cycle;\n\t\n\tmy @lex_max;\n\t\n\t%used = ();\n\t\n\tmy $prev_max = 1;\n\tmy @r_array;\n\t\n\tfor( @_ ){\n\t\tif( exists $used{ $_ } ){\n\t\t\tmy $cand;\n\t\t\twhile( 1 ){\n\t\t\t\t$cand = shift @r_array;\n\t\t\t\tif( exists $maxes{ $cand } ){\n\t\t\t\t\tnext;\n\t\t\t\t\t}\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\tpush @lex_max, $cand;\n\t\t\t}\n\t\telse{\n\t\t\tpush @r_array, reverse $prev_max .. $_;\n\t\t\t$prev_max = $_;\n\t\t\tpush @lex_max, $_;\n\t\t\t$used{ $_ } ++;\n\t\t\t}\n\t\t}\n\t\n\tprint \"@lex_max\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy %maxes;\n\t\n\tmap $maxes{ $_ } ++, @_;\n\t\n\tmy %cycle;\n\t\n\texists $maxes{ $_ } or $cycle{ $_ } ++ for 1 .. @_;\n\t\n\tmy @cycle = sort { $a <=> $b } keys %cycle;\n\t\n\t$debug and print \"[@cycle]\";\n\t\n\tmy @copy_cycle = @cycle;\n\t\n\tmy @lex_min;\n\t\n\tmy %used;\n\t\n\tfor( @_ ){\n\t\tif( exists $used{ $_ } ){\n\t\t\tpush @lex_min, shift @copy_cycle;\n\t\t\t}\n\t\telse{\n\t\t\tpush @lex_min, $_;\n\t\t\t$used{ $_ } ++;\n\t\t\t}\n\t\t}\n\t\n\tprint \"@lex_min\";\n\t\n\tmy @r_cycle = reverse @cycle;\n\t\n\tmy @lex_max;\n\t\n\t%used = ();\n\t\n\tfor( @_ ){\n\t\tif( exists $used{ $_ } ){\n\t\t\twhile( 1 ){\n\t\t\t\tmy $cand = shift @r_cycle;\n\t\t\t\tif( $cand > $_ ){\n\t\t\t\t\tpush @r_cycle, $cand;\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tpush @lex_max, $cand;\n\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\telse{\n\t\t\tpush @lex_max, $_;\n\t\t\t$used{ $_ } ++;\n\t\t\t}\n\t\t}\n\t\n\tprint \"@lex_max\";\n\t}"}], "src_uid": "6e7b8742906ce40458d920121c33c54f"} {"nl": {"description": "To celebrate your birthday you have prepared a festive table! Now you want to seat as many guests as possible.The table can be represented as a rectangle with height $$$h$$$ and width $$$w$$$, divided into $$$h \\times w$$$ cells. Let $$$(i, j)$$$ denote the cell in the $$$i$$$-th row and the $$$j$$$-th column of the rectangle ($$$1 \\le i \\le h$$$; $$$1 \\le j \\le w$$$).Into each cell of the table you can either put a plate or keep it empty.As each guest has to be seated next to their plate, you can only put plates on the edge of the table\u00a0\u2014 into the first or the last row of the rectangle, or into the first or the last column. Formally, for each cell $$$(i, j)$$$ you put a plate into, at least one of the following conditions must be satisfied: $$$i = 1$$$, $$$i = h$$$, $$$j = 1$$$, $$$j = w$$$.To make the guests comfortable, no two plates must be put into cells that have a common side or corner. In other words, if cell $$$(i, j)$$$ contains a plate, you can't put plates into cells $$$(i - 1, j)$$$, $$$(i, j - 1)$$$, $$$(i + 1, j)$$$, $$$(i, j + 1)$$$, $$$(i - 1, j - 1)$$$, $$$(i - 1, j + 1)$$$, $$$(i + 1, j - 1)$$$, $$$(i + 1, j + 1)$$$.Put as many plates on the table as possible without violating the rules above.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$)\u00a0\u2014 the number of test cases. Each of the following $$$t$$$ lines describes one test case and contains two integers $$$h$$$ and $$$w$$$ ($$$3 \\le h, w \\le 20$$$)\u00a0\u2014 the height and the width of the table.", "output_spec": "For each test case, print $$$h$$$ lines containing $$$w$$$ characters each. Character $$$j$$$ in line $$$i$$$ must be equal to $$$1$$$ if you are putting a plate into cell $$$(i, j)$$$, and $$$0$$$ otherwise. If there are multiple answers, print any. All plates must be put on the edge of the table. No two plates can be put into cells that have a common side or corner. The number of plates put on the table under these conditions must be as large as possible. You are allowed to print additional empty lines.", "sample_inputs": ["3\n3 5\n4 4\n5 6"], "sample_outputs": ["10101\n00000\n10101\n\n0100\n0001\n1000\n0010\n\n010101\n000000\n100001\n000000\n101010"], "notes": "NoteFor the first test case, example output contains the only way to put $$$6$$$ plates on the table.For the second test case, there are many ways to put $$$4$$$ plates on the table, example output contains one of them.Putting more than $$$6$$$ plates in the first test case or more than $$$4$$$ plates in the second test case is impossible."}, "positive_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t( $n, $m ) = split;\r\n\t\r\n\tprint for \r\n\t\t( $_ ) = ( 10 x 10 ) =~ /.{$m}/g,\r\n\t\t( ( 0 x $m, 1 . 0 x ( $m - 2 ) . 1 ) x 11 )[ 4 .. $n, 0 ],\r\n\t\t$_;\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\tmy( $line ) = ( '10' x 20 ) =~ /.{$m}/g;\n\t\n\t@_ = ( ( '0' x $m, '1' . '0' x ( $m - 2 ) . '1' ) x 20 )[ 0 .. $n - 1 - 2 ];\n\t\n\t$_[ @_ - 1 ] =~ y/1/0/ if $n % 2 == 0;\n\t\n\t@_ = ( $line, @_, $line );\n\t\n\tprint join \"\\n\", @_;\n\t}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($h,$w) = map { $_ - 0 } split(/\\s+/,);\r\n my @mp = ( ( '0' x $w ) x $h );\r\n \r\n for(my $i = 0; $i< $w-2; $i+=2){\r\n substr($mp[0],$i,1) = '1';\r\n substr($mp[$h-1],$w-1-$i,1) = '1';\r\n }\r\n for(my $i = 0; $i< $h-2; $i+=2){\r\n substr($mp[$i],$w-1,1) = '1';\r\n substr($mp[$h-1-$i],0,1) = '1';\r\n }\r\n print (join(\"\\n\",@mp) . \"\\n\");\r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t( $n, $m ) = split;\r\n\t\r\n\tprint for \r\n\t\t( $_ ) = ( 10 x 10 ) =~ /.{$m}/g,\r\n\t\t( ( 0 x $m, 1 . 0 x ( $m - 2 ) . 1 ) x 10 )[ 4 .. $n, 0 ],\r\n\t\t$_;\r\n\t}"}], "src_uid": "730cc4be2656c1dcdbda220b8778cdbf"} {"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\u2009\u2264\u2009n,\u2009k\u2009\u2264\u2009100). The second line contains n integers ai (1\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 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 \u2014 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) = split(' ', <>); $a = 0;\nforeach(split(' ', <>)){\n\t$t = 0;\n\tuntil($_ == 0){\n\t$t += ($_ % 10 == 4 or $_ % 10 == 7);\n\t$_ = int($_ / 10);}\n\t$a ++ if $t <= $k;\n\t}\nprint $a;"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nmy ($n, $k) = map int, split '\\D', <>;\nmy @numbers = map int, split '\\D', <>;\n\nmy $answer = 0;\n\nfor (@numbers) {\n if ($_ =~ tr/[47]/L/ <= $k) {\n\t$answer++;\n }\n}\n\nprint \"$answer\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\n$ans = 0;\n\n@str1 = split(' ', );\n\n$n = @str1[0];\n$k = @str1[1];\n\n@str2 = split(' ', );\n\nfor my $now (@str2) {\n $counter = 0;\n while($now > 0) {\n $digit = $now % 10;\n if($digit == 4 || $digit == 7) {\n $counter += 1; \n } \n $now /= 10;\n }\n if($counter <= $k) {\n $ans += 1;\n }\n}\n\nprint $ans;"}], "negative_code": [{"source_code": "($n, $k) = split(' ', <>);\n\nforeach(split(' ', <>)){\n\t$t = 0;\n\tuntil($_ == 0){\n\t$t += ($_ % 10 == 4 or $_ % 10 == 7);\n\t$_ = int($_ / 10);}\n\t$a ++ if $t <= $k;\n\t}\nprint $a;"}], "src_uid": "6bb26991c9ea70e51a6bda5653de8131"} {"nl": {"description": "While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.", "input_spec": "The first line of input contains string s containing lowercase English letters (1\u2009\u2264\u2009|s|\u2009\u2264\u20091000). The second line contains integer k (1\u2009\u2264\u2009k\u2009\u2264\u20091000).", "output_spec": "Print \"YES\"(without quotes) if he has worn his own back-bag or \"NO\"(without quotes) otherwise.", "sample_inputs": ["saba\n2", "saddastavvat\n2"], "sample_outputs": ["NO", "YES"], "notes": "NotePalindrome is a string reading the same forward and backward.In the second sample, the faxes in his back-bag can be \"saddas\" and \"tavvat\"."}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($line = <>);\n$len = length $line;\n@a = split //, $line;\nchomp($k = <>);\n$len%$k>0 and say \"NO\" and exit;\n$l = $len / $k;\nfor ($b=0,$e=$l; $b<$len; $b=$e,$e+=$l) {\n\tfor ($i=$b,$j=$e-1; $i<$j; ++$i,--$j) {\n\t\t$a[$i] ne $a[$j] and say \"NO\" and exit;\n\t}\n}\nsay \"YES\";"}, {"source_code": "chomp($s = <>);\nchomp($k = <>);\n($i, $mark) = (0, 1);\n$len = length $s;\n$mark = 0 if $len % $k != 0;\nwhile ($mark == 1 && $i < $len) {\n\t$su = substr($s, $i, $len / $k);\n\t$mark = 0 unless ($su eq reverse $su);\n\t$i += $len / $k;\n}\nprint qw(NO YES)[$mark == 1];\n"}, {"source_code": "chomp($s = <>);\nchomp($k = <>);\n$i = 0;\n$mark = 1;\n$len = length $s;\n$mark = 0 if $len % $k != 0;\nwhile ($mark == 1 && $i < $len)\n{\n\t$su = substr($s, $i, $len / $k);\n\t$mark = 0 unless ($su eq reverse $su);\n\t$i += $len / $k;\n}\n$mark == 1 ? print \"YES\" : print \"NO\";\nprint \"\\n\";\n"}, {"source_code": "$_ = <>, chop, $n = <>;\n$re = '.' x ((length) / $n);\n$i ++, $j += $& eq reverse $& while /$re/g;\nprint qw(NO YES)[$i == $n && $j == $n && !((length) % $n)]"}, {"source_code": "use integer;\nwhile(<>){\n\tchomp;\n\t$n = <>;\n\t$m = (length) / $n;\n\t$re = '.' x $m;\n\t$rq = qr/$re/;\n\t$i = $j = 0;\n\twhile(/$rq/g){\n\t\t$i ++;\n\t\t$j += $& eq reverse $&;\n\t\t}\n\tprint qw(NO YES)[$i == $n && $j == $n && $m * $n == length]\n\t}"}], "negative_code": [{"source_code": "chomp($s = <>);\nchomp($k = <>);\n$i = 0;\n$mark = 1;\n$len = length $s;\n$mark = 0 if $len % $k != 0;\nwhile ($mark == 1 && $i < length $s)\n{\n\t$su = substr($s, 0, (length $s) / $k);\n\t$mark = 0 unless ($su eq reverse $su);\n\t$i += length $s / $k;\n}\n$mark == 1 ? print \"YES\" : print \"NO\";\nprint \"\\n\";\n"}, {"source_code": "chomp($s = <>);\nchomp($k = <>);\n$i = 0;\n$mark = 1;\nwhile ($i < length $s)\n{\n\t$su = substr($s, 0, (length $s) / $k);\n\t$mark = 0 unless ($su eq reverse $su);\n\t$i += length $s / $k;\n}\n$mark == 1 ? print \"YES\" : print \"NO\";\nprint \"\\n\";\n"}], "src_uid": "43bb8fec6b0636d88ce30f23b61be39f"} {"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,\u2009\u2009-\u20092,\u20091,\u20093,\u2009\u2009-\u20094. Suppose the mother suggested subarrays (1,\u2009\u2009-\u20092), (3,\u2009\u2009-\u20094), (1,\u20093), (1,\u2009\u2009-\u20092,\u20091,\u20093). Then if the girl chooses the third and the fourth subarrays then: the first flower adds 1\u00b71\u2009=\u20091 to the girl's happiness, because he is in one of chosen subarrays, the second flower adds (\u2009-\u20092)\u00b71\u2009=\u2009\u2009-\u20092, because he is in one of chosen subarrays, the third flower adds 1\u00b72\u2009=\u20092, because he is in two of chosen subarrays, the fourth flower adds 3\u00b72\u2009=\u20096, because he is in two of chosen subarrays, the fifth flower adds (\u2009-\u20094)\u00b70\u2009=\u20090, because he is in no chosen subarrays. Thus, in total 1\u2009+\u2009(\u2009-\u20092)\u2009+\u20092\u2009+\u20096\u2009+\u20090\u2009=\u20097 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100)\u00a0\u2014 the number of flowers and the number of subarrays suggested by the mother. The second line contains the flowers moods\u00a0\u2014 n integers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009100\u2009\u2264\u2009ai\u2009\u2264\u2009100). 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\u2009\u2264\u2009li\u2009\u2264\u2009ri\u2009\u2264\u2009n) denoting the subarray a[li],\u2009a[li\u2009+\u20091],\u2009...,\u2009a[ri]. Each subarray can encounter more than once.", "output_spec": "Print single integer\u00a0\u2014 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": "use strict;\nuse warnings;\n\nmy ($n,$m)=split / /,<>;\nchomp($n,$m);\nmy @d=split / /,<>;\nchomp(@d);\nmy @sums;\nmy $ans=0;\nwhile($m--)\n{\n my ($s,$e)=split / /,<>;\n chomp($s,$e);\n push(@sums,sum($s,$e,\\@d));\n}\n\nfor my $val (@sums)\n{\n $ans=max_($ans,$ans+$val);\n}\n\nprint $ans . \"\\n\";\n\nsub sum{\n my $si=shift;\n my $ei=shift;\n my $arr=shift;\n my $aa=0;\n for(my $i=$si-1;$i<$ei;$i++)\n {\n $aa+=$arr->[$i];\n }\n return $aa;\n}\n\nsub min_{\n\treturn $_[0] > $_[1] ? $_[1] : $_[0];\n}\n\nsub max_{\n\treturn $_[1] > $_[0] ? $_[1] : $_[0];\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t\n\tmy( $n, $m ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tprint 0 + eval join '+', grep !/-/, map { \n\t\t\t0 + eval join '+', map \"($_)\",\n\t\t\t@_[ eval join '..', map $_ - 1, split ' ', <> ]\n\t\t} 1 .. $m;\n\t\n\t}"}], "negative_code": [], "src_uid": "6d7364048428c70e0e9b76ab1eb8cc34"} {"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\u2009\u2264\u2009|s|\u2009\u2264\u20091000) 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": "$class = \"[AHIMOoTUVvWwXxY]\";\n$a = \"($class)(?1)\\\\2\";\n$b = \"b(?1)d\";\n$c = \"d(?1)b\";\n$d = \"p(?1)q\";\n$e = \"q(?1)p\";\n$f = \"$class?\";\n$expr = qr(^($a|$b|$c|$d|$e|$f|\\n)$);\n\n$s = <>;\n$r = ($s =~ /$expr/);\nprint $r? \"TAK\": \"NIE\";\n"}, {"source_code": "$_ = <>, chomp;\n\nwhile( $c = chop ){\n\t( $c . (s/.// ? $& : $c) ) =~ /bd|db|pq|qp|([ovwxAHIMOT-Y])\\1/ or $f ++\n\t}\n\nprint $f ? \"NIE\" : \"TAK\""}, {"source_code": "$F = 1;\n$F &&= f( <> );\n\nprint ! $F ? \"NIE\" : \"TAK\";\n\nsub f {\n\tmy ($A) = shift;\n\t$A =~ s/\\B.*\\B// &&$&&& ( $F &&= f($&) );\n\t$F &&= \n\t$A =~ /bd|db|pq|qp|^([ovwxAHIMOT-Y])\\1?$/;\n\t}"}, {"source_code": "chomp, \n(substr $_, substr $_, 0, (length) / 2, \n(substr $_, 0, (length) / 2) =~ y/bdpq/dbqp/r), \n( (length) % 2 and (substr $_, (length) / 2, 1) =~ /[bdpq]/ and $_ = 'a' ),\nprint qw(NIE TAK)[ !/[^ovwxAHIMOT-Ybdpq]/ * ($_ eq reverse) ] \nfor <>"}], "negative_code": [{"source_code": "$class = \"[AHIMOoTUVvWwXxY]\";\n$a = \"($class)(?1)\\\\2\";\n$b = \"b(?1)d\";\n$c = \"d(?1)b\";\n$d = \"p(?1)q\";\n$e = \"q(?1)p\";\n$f = \"$class?\";\n$expr = qr(^($a|$b|$c|$d|$e|$f|\\n)$);\n\n$s = <>;\n$r = ($s =~ /$expr/);\nprint $r? \"YES\": \"NO\";\n"}, {"source_code": "chomp, print qw(NIE TAK)[ !/[^ovwxAHIMOT-Y]/ * ($_ eq reverse) ] for <>"}, {"source_code": "chomp, \n(substr $_, substr $_, 0, (length) / 2, \n(substr $_, 0, (length) / 2) =~ y/bdpq/dbqp/r), \nprint qw(NIE TAK)[ !/[^ovwxAHIMOT-Ybdpq]/ * ($_ eq reverse) ] for <>"}, {"source_code": "chomp, \n(substr $_, substr $_, 0, (length) / 2, \n(substr $_, 0, (length) / 2) =~ y/bdpq/dbqp/r), \n( (length) % 2 and (substr $_, 0, (length) / 2 + 1) =~ /[bdpq]/ and $_ = 'a' ),\nprint qw(NIE TAK)[ !/[^ovwxAHIMOT-Ybdpq]/ * ($_ eq reverse) ] \nfor <>"}], "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$$$)\u00a0\u2014 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$$$)\u00a0\u2014 the heights of the bottles.", "output_spec": "Print the single integer $$$k$$$\u00a0\u2014 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": "($n,$h,@a)=(<>.<>)=~/\\w+/g;map{$s=$i=0;map{$s+=$_ if$i^=1}@x=sort{$b<=>$a}@x,$_;$s>$h||$\\++}@a;print\n"}, {"source_code": "($n,$h,@a)=(<>.<>)=~/\\w+/g;\nmap{\n$s=0;\n@x=sort{$b<=>$a}@x,$_;\n$s+=$x[$_*2]for 0..@x;\n$s>$h||$\\++\n}@a;\nprint\n"}, {"source_code": "($n,$h,@a)=((split$\",<>.<>),2e9);\nfor(0..$n){\n@x=sort{$b<=>$a}@x,$a[$_];\n$s=$i=0;\nwhile($i<@x){\n$s+=$x[$i];$i+=2\n}\nprint&&last if$s>$h\n}\n"}, {"source_code": "($n,$h)=split$\",<>;\n@a=((split$\",<>),1e10);\nfor(@a) {\n$c++;\n@x=sort{$b<=>$a}(@x,$_);\n$s=0;\n$i=0;\nwhile($i<@x) {\n$s+=$x[$i];\n$i+=2;\n}\nif($s>$h) {\nprint$c-1;\nlast;\n}\n}\n"}, {"source_code": "($n,$h,@a)=(<>.<>)=~/\\w+/g;\nmap{\n$s=$i=0;\nmap{$s+=$_ if$i^=1}@x=sort{$b<=>$a}@x,$_;\n$s>$h||$\\++\n}@a;\nprint\n"}, {"source_code": "($n,$h) = split' ',<>;\n@a = split' ',<>;\npush @a, 1e10;\nfor (@a) {\n $c++;\n push @x, $_;\n @x = sort { $b <=> $a } @x;\n $s = 0;\n $i = 0;\n while($i<@x) {\n $s += $x[$i];\n $i+=2;\n }\n if ($s > $h) {\n print $c - 1;\n last;\n }\n}\n"}, {"source_code": "($n,$h,@a)=(<>.<>)=~/\\w+/g;\nmap{\n@x=sort{$b<=>$a}@x,$_;\n$s=$i=0;\nmap{$s+=$_ if$i^=1}@x;\n$s>$h||$\\++\n}@a;\nprint\n"}, {"source_code": "($n,$h,@a)=(<>.<>)=~/\\w+/g;\nfor(@a){\n@x=sort{$b<=>$a}@x,$_;\n\n$s=$i=0;\nfor(@x){\n$s+=$_ if$i^=1\n}\n\n$\\++if$s<=$h\n}\nprint\n"}, {"source_code": "($n,$h,@a)=((split$\",<>.<>),2e9);\nfor(@a){\n@x=sort{$b<=>$a}@x,$_;\n\n$s=$i=0;\nfor(@x){\n$s+=$_ if$i^=1\n}\n\n$\\++if$s<=$h\n}\nprint\n"}, {"source_code": "($n,$h,@a)=((split$\",<>.<>),2e9);\nfor(@a){\n@x=sort{$b<=>$a}@x,$_;\n\n$s=$i=0;\nwhile($i<@x){\n$s+=$x[$i];$i+=2\n}\n\n$f++if$s<=$h\n}\nprint$f\n"}, {"source_code": "($n,$h,@a)=((<>.<>)=~/\\w+/g);\nfor(@a){\n@x=sort{$b<=>$a}@x,$_;\n\n$s=$i=0;\nfor(@x){\n$s+=$_ if$i^=1\n}\n\n$\\++if$s<=$h\n}\nprint\n"}, {"source_code": "($n,$h)=split$\",<>;\n@a=((split$\",<>),1e10);\nfor(0..$n) {\n@x=sort{$b<=>$a}@x,$a[$_];\n$s=0;\n$i=0;\nwhile($i<@x) {\n$s+=$x[$i];\n$i+=2;\n}\nprint&&last if$s>$h\n}\n\n"}], "negative_code": [{"source_code": "($n,$h,@a)=split$\",<>.<>,2e9;\nfor(0..$n){\n@x=sort{$b<=>$a}@x,$a[$_];\n$s=$i=0;\nwhile($i<@x){\n$s+=$x[$i];$i+=2\n}\nprint&&last if$s>$h\n}\n"}, {"source_code": "($n,$h,@a)=((split$\",<>.<>),2e9);\nprint \"$n $h @a\";\nfor(0..$n){\n@x=sort{$b<=>$a}@x,$a[$_];\n$s=$i=0;\nwhile($i<@x){\n$s+=$x[$i];$i+=2\n}\nprint&&last if$s>$h\n}\n"}], "src_uid": "2ff0919ee7dfdfb916b23c26fb2caf20"} {"nl": {"description": "Little Victor adores the sets theory. Let us remind you that a set is a group of numbers where all numbers are pairwise distinct. Today Victor wants to find a set of integers S that has the following properties: for all x the following inequality holds l\u2009\u2264\u2009x\u2009\u2264\u2009r; 1\u2009\u2264\u2009|S|\u2009\u2264\u2009k; lets denote the i-th element of the set S as si; value must be as small as possible. Help Victor find the described set.", "input_spec": "The first line contains three space-separated integers l,\u2009r,\u2009k (1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u20091012;\u00a01\u2009\u2264\u2009k\u2009\u2264\u2009min(106,\u2009r\u2009-\u2009l\u2009+\u20091)).", "output_spec": "Print the minimum possible value of f(S). Then print the cardinality of set |S|. Then print the elements of the set in any order. If there are multiple optimal sets, you can print any of them.", "sample_inputs": ["8 15 3", "8 30 7"], "sample_outputs": ["1\n2\n10 11", "0\n5\n14 9 28 11 16"], "notes": "NoteOperation represents the operation of bitwise exclusive OR. In other words, it is the XOR operation."}, "positive_code": [{"source_code": "($l,$r,$k)=split' ',<>;\n$nval=$r-$l+1;\nif($nval<=10){\n\t$minxor=2e13;\n\tfor $mask(0..(1<<$nval)-2){\n\t\t$setsize=$xor=0;\n\t\tfor(0..$nval-1){\n\t\t\tnext if $mask&1<<$_;\n\t\t\t$setsize++;\n\t\t\t$xor^=$l+$_;\n\t\t}\n\t\tnext if $setsize>$k;\n\t\t($minmask,$minxor)=($mask,$xor) if $xor<$minxor;\n\t}\n\t@values=();\n\tfor(0..$nval-1){\n\t\tnext if $minmask&1<<$_;\n\t\tpush @values,$l+$_;\n\t}\n\tprint \"$minxor\\n\",0+@values,\"\\n@values\\n\";\n\texit;\n}\n\nsub solve2{\n\t$x=1+($l|1);\n\tprint \"1\\n2\\n@{[$x..$x+1]}\\n\";\n}\n\nif($k==1){\n\tprint \"$l\\n1\\n$l\\n\";\n}elsif($k==2){\n\tsolve2();\n}elsif($k==3){\n\t$p2=1;\n\t$p2*=2 while $p2<=$l;\n\t$x=$p2*3/2;\n\tif($x>$r){\n\t\tsolve2()\n\t}else{\n\t\tprint \"0\\n3\\n$l $x @{[$l^$x]}\\n\";\n\t}\n}else{\n\t$x=1+($l|1);\n\tprint \"0\\n4\\n@{[$x..$x+3]}\\n\";\n}\n"}], "negative_code": [{"source_code": "($l,$r,$k)=split' ',<>;\n$nval=$r-$l+1;\nif($nval<=10){\n\t$minxor=2e9;\n\tfor $mask(0..(1<<$nval)-2){\n\t\t$setsize=$xor=0;\n\t\tfor(0..$nval-1){\n\t\t\tnext if $mask&1<<$_;\n\t\t\t$setsize++;\n\t\t\t$xor^=$l+$_;\n\t\t}\n\t\tnext if $setsize>$k;\n\t\t($minmask,$minxor)=($mask,$xor) if $xor<$minxor;\n\t}\n\t@values=();\n\tfor(0..$nval-1){\n\t\tnext if $minmask&1<<$_;\n\t\tpush @values,$l+$_;\n\t}\n\tprint \"$minxor\\n\",0+@values,\"\\n@values\\n\";\n\texit;\n}\n\nsub solve2{\n\t$x=1+($l|1);\n\tprint \"1\\n2\\n@{[$x..$x+1]}\\n\";\n}\n\nif($k==1){\n\tprint \"$l\\n1\\n$l\\n\";\n}elsif($k==2){\n\tsolve2();\n}elsif($k==3){\n\t$p2=1;\n\t$p2*=2 while $p2<=$l;\n\t$x=$p2*3/2;\n\tif($x>$r){\n\t\tsolve2()\n\t}else{\n\t\tprint \"0\\n3\\n$l $x @{[$l^$x]}\\n\";\n\t}\n}else{\n\t$x=1+($l|1);\n\tprint \"0\\n4\\n@{[$x..$x+3]}\\n\";\n}\n\n"}], "src_uid": "b5eb2bbdba138a4668eb9736fb5258ac"} {"nl": {"description": "Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a \"plus\") and negative (a \"minus\"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other.Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own. Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed.", "input_spec": "The first line of the input contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100000) \u2014 the number of magnets. Then n lines follow. The i-th line (1\u2009\u2264\u2009i\u2009\u2264\u2009n) contains either characters \"01\", if Mike put the i-th magnet in the \"plus-minus\" position, or characters \"10\", if Mike put the magnet in the \"minus-plus\" position.", "output_spec": "On the single line of the output print the number of groups of magnets.", "sample_inputs": ["6\n10\n10\n10\n01\n10\n10", "4\n01\n01\n10\n10"], "sample_outputs": ["3", "2"], "notes": "NoteThe first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets.The second testcase has two groups, each consisting of two magnets."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse 5.012;\nuse warnings;\n\nchomp(my $n = <>);\nmy $last;\nmy $groups = 1;\nwhile ($n--) {\n\tuse integer;\n\tchomp(my $m = <>);\n\tmy $first = $m / 10;\n\t$groups++ if defined $last && $last eq $first;\n\t$last = $m % 10;\n}\nprint $groups;\n"}, {"source_code": "my $n = <>;\nmy ($now, $last, $ans) = (\"0\", \"0\", 0);\n\nfor (1..$n) {\n $now = <>;\n $ans++ if ($now ne $last);\n $last = $now;\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\n$n = <> + 0;\n$groups = 1;\n$previous = <>;\n\nfor ($i = 0; $i < ($n-1); $i++) {\n $magnet = <>;\n if ($magnet ne $previous) {\n $groups++;\n }\n\n $previous = $magnet;\n}\n\nprint \"$groups\\n\";"}, {"source_code": "chomp($n=<>);\nmy $pre = \"\";\nwhile (<>) {\n\tchomp;\n\t++$cnt if $_ != $pre;\n\t$pre = $_;\n}\nprint \"$cnt\\n\";"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nchomp($t = <>);\nmy $ans = 0;\nwhile (<>) {\n\tchomp;\n\t$_ ne $s and $ans++;\n\t$s = $_;\n}\nsay $ans;"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nmy $n = <>;\nmy $cur = '00';\nmy $g = 0;\n\nfor (1..$n) {\n my $s = <>;\n if ($s ne $cur) {\n $g++;\n $cur = $s;\n }\n}\n\nprint $g, \"\\n\";\n"}, {"source_code": "$n=<>;while($n--){$l=$i and$g++if($i=<>)ne$l}print$g"}, {"source_code": "#!/usr/bin/perl\n\n# Copyright 2013 Anton Leontiev \n#\n# Redistribution and use in source and binary forms, with or without\n# modification, are permitted provided that the following conditions are\n# met:\n#\n# * Redistributions of source code must retain the above copyright\n# notice, this list of conditions and the following disclaimer.\n# * Redistributions in binary form must reproduce the above\n# copyright notice, this list of conditions and the following disclaimer\n# in the documentation and/or other materials provided with the\n# distribution.\n# * Neither the name of the nor the names of its\n# contributors may be used to endorse or promote products derived from\n# this software without specific prior written permission.\n#\n# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n# \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nuse v5.12;\nuse warnings;\nuse English qw(-no_match_vars);\n\n$OUTPUT_FIELD_SEPARATOR = \" \";\n\nmy $num = <>;\nmy $last = '';\nmy $islands;\n\nmap { $islands++, $last = $_ if $last ne $_ } <>;\n\nsay $islands;\n"}, {"source_code": "use strict;\nmy $n = <>;\nmy @a;\nfor (my $i = 0; $i < $n; $i++){\n my $kek = <>;\n# print $kek;\n push(@a, $kek);\n}\nmy $ans = 1;\nsub solve{\n my ($a, $n) = @_;\n for (my $i = 0; $i < $n - 1; $i++){\n if($a[$i] != $a[$i + 1]){\n $ans++;\n }\n }\n print $ans;\n}\nsolve (\\@a, $n);\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\n# solve\n\nmy $n = read_token;\n\nmy $latest_token = read_token;\n\nmy $answer = 1;\n\nfor (1..$n-1) {\n my $token = read_token;\n if ($latest_token ne $token) {\n $answer++;\n $latest_token = $token;\n }\n}\n\nsay $answer;\n"}, {"source_code": "$n = <>;\nwhile ($n--) {\n $now = <>;\n $groups += $now != $last;\n $last = $now;\n}\nprint \"$groups\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nchomp(my $tc = <>);\n\nmy $ans = 1;\nchomp(my $now = <>);\nmy $temp = $now;\n\n\nfor( my $i = 1; $i<$tc ;$i++){\n\tchomp($now = <>);\n\tif($now != $temp){\n\t\t$ans++;\n\t}\n\t$temp = $now\n}\nprint $ans;"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nmy $last = 'x';\nmy $number = 0;\n\nmy $n = <>;\nforeach (1..$n)\n{\n my $piece = <>;\n chomp $piece;\n if ($last eq 'x')\n {\n $number++;\n }\n elsif ($last eq substr($piece, 0, 1))\n {\n $number++;\n }\n $last = substr($piece, -1);\n}\n\nprint $number;\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$t=<>;\nwhile (<>){\n\t$t!=$_ and $c++; \n\t$t=$_;\n\t}\nprint $c+1;\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "my $num = <>;\nchomp $num;\nmy $count = 1;\nmy $magnet = <>;\nfor (2..$num) {\n $next = <>;\n if (!($magnet eq $next)) {\n $count++;\n $magnet = $next;\n }\n}\nprint $count;"}], "negative_code": [{"source_code": "chomp($n=<>);\nmy $pre = \"\";\nwhile (<>) {\n\tchomp;\n\t$cnt++ if $_ == $pre;\n\t$pre = $_;\n}\nprint \"$cnt\\n\";"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nchomp(my $tc = <>);\n\nmy $ans = 1;\nchomp(my $now = <>);\nmy @arr = split(\"\", $now);\nmy $temp = $arr[1];\n\n\nfor( my $i = 1; $i<$tc ;$i++){\n\tchomp(my $now = <>);\n\t@arr = split(\"\", $now);\n\tif($arr[0] == $temp){\n\t\t$ans++;\n\t}\n\telse{\n\t\t$temp = $arr[1];\n\t}\n}\nprint $ans;"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nchomp(my $tc = <>);\n\nmy $ans = 1;\nchomp(my $now = <>);\nmy $temp = $now;\n\n\nfor( my $i = 1; $i<$tc ;$i++){\n\tchomp(my $now = <>);\n\tif($now == $temp){\n\t\t$ans++;\n\t}\n\telse{\n\t\t$temp = $now;\n\t}\n}\nprint $ans;"}], "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 (\u2009-\u200910\u2009\u2264\u2009ti\u2009\u2264\u200910). 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,\u2009t2,\u2009...,\u2009tn. For example, if t\u2009=\u2009(1,\u2009\u2009-\u20091,\u20091,\u2009\u2009-\u20091), 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\u2009\u2264\u2009n\u2009\u2264\u2009105) which represents the number of registered clients of the \"Couple Matching\". The second line contains a sequence of integers t1,\u2009t2,\u2009...,\u2009tn (\u2009-\u200910\u2009\u2264\u2009ti\u2009\u2264\u200910), ti \u2014 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 \u2009-\u2009x (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 \u0421++. 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) \u0438 (3,4).In the second sample any couple of clients is opposite."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n<>;\n$_=<>;\ns/-?\\d+/$_[$&+10]++/eg;\nfor (1..10){\n\t$ats += $_[10-$_]*$_[10+$_]\n\t}\n$ats += $_[10]*($_[10]-1)/2;\nprint $ats;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n<>;\n$_=<>;\n$_[20]=0;\ns/-?\\d+/$_[$&]++/eg;\nfor (1..10){\n\t$ats += $_[-$_]*$_[$_]\n\t}\n$ats += $_[0]*($_[0]-1)/2;\nprint $ats;\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [], "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$$$) \u2014 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$$$) \u2014 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$$$) \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\t@_ = reverse @_;\n\t\n\tmy $first = shift @_;\n\t\n\tmy $len = 1;\n\t\n\tmy $ans = 0;\n\t\n\twhile( @_ ){\n\t\tif( $_[ 0 ] == $first ){\n\t\t\tshift @_;\n\t\t\t$len ++;\n\t\t\t}\n\t\telse{\n\t\t\t$ans ++;\n\t\t\tfor my $j ( 1 .. $len ){\n\t\t\t\tlast if not @_;\n\t\t\t\tshift @_;\n\t\t\t\t$len ++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tprint $ans;\n\t}"}], "negative_code": [], "src_uid": "dc10b869293df2b7a557262c805635ba"} {"nl": {"description": "Vanya is doing his maths homework. He has an expression of form , where x1,\u2009x2,\u2009...,\u2009xn 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\u2009\u2264\u2009|s|\u2009\u2264\u20095001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs \u2009+\u2009 and \u2009*\u2009. The number of signs \u2009*\u2009 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\u2009+\u20095\u2009*\u2009(7\u2009+\u20098)\u2009*\u20094\u2009=\u2009303.Note to the second sample test. (2\u2009+\u20093)\u2009*\u20095\u2009=\u200925.Note to the third sample test. (3\u2009*\u20094)\u2009*\u20095\u2009=\u200960 (also many other variants are valid, for instance, (3)\u2009*\u20094\u2009*\u20095\u2009=\u200960)."}, "positive_code": [{"source_code": "$_ = <>;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! $n = \"$pr($`)$'\", $h{eval $n} = $n, '' !ge, '' /ge;\n\nuse bigint;\nprint eval $h{ (sort {$b <=> $a} keys %h)[0] }\n"}, {"source_code": "$_ = <>;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! $n = \"$pr($`)$'\", $h{eval $n} = $n, '' !ge, '' /ge;\n\nuse bigint;\nprint eval $h{ (sort {$b <=> $a} keys %h)[0] }\n"}, {"source_code": "$_ = <>;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! $n = \"$pr($`)$'\", $h{eval $n} = $n, '' !ge, '' /ge;\n\nuse bigint;\nprint eval $h{ (sort {$b <=> $a} keys %h)[0] }\n"}, {"source_code": "$_ = <>;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! $n = \"$pr($`)$'\", $h{eval $n} = $n, '' !ge, '' /ge;\n\nuse bigint;\nprint eval $h{ (sort {$b <=> $a} keys %h)[0] }\n"}, {"source_code": "$_ = <>;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! $n = \"$pr($`)$'\", $h{eval $n} = $n, '' !ge, '' /ge;\n\nuse bigint;\nprint eval $h{ (sort {$b <=> $a} keys %h)[0] }\n"}, {"source_code": "$_ = <>;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! $n = \"$pr($`)$'\", $h{eval $n} = $n, '' !ge, '' /ge;\n\nuse bigint;\nprint eval $h{ (sort {$b <=> $a} keys %h)[0] }\n"}, {"source_code": "$_ = <>;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! $n = \"$pr($`)$'\", $h{eval $n} = $n, '' !ge, '' /ge;\n\nuse bigint;\nprint eval $h{ (sort {$b <=> $a} keys %h)[0] }"}, {"source_code": "$_ = <>;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! $n = \"$pr($`)$'\", $h{eval $n} = $n, '' !ge, '' /ge;\n\nuse bigint;\nprint eval $h{ (sort {$b <=> $a} keys %h)[0] }\n"}, {"source_code": "$_ = <>;\n$m =-~ 0;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! $n = \"$pr($`)$'\", $h{eval $n} = $n, '' !ge, '' /ge;\nuse bigint;\nprint eval $h{ (sort {$b <=> $a} keys %h)[0] }"}, {"source_code": "$_ = <>;\n$m =-~ 0;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! $n = \"$pr($`)$'\", $h{eval $n} = $n, '' !ge, '' /ge;\n$_ = $h{ (sort {$b <=> $a} keys %h)[0] };\nuse bigint;\nprint eval"}, {"source_code": "$_ = <>;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! $n = \"$pr($`)$'\", $h{eval $n} = $n, '' !ge, '' /ge;\n\nuse bigint;\nprint eval $h{ (sort {$b <=> $a} keys %h)[0] }"}, {"source_code": "$_ = <>; # readline\ns/(^|\\*)\\K/ $pre_match = $`, ($post_match = $') =~ s!(?=\\*)|$! $hash{eval \"$pre_match($`)$'\"} = \"$pre_match($`)$'\", '' !ge, '' /ge;\n\nuse bigint;\nprint eval $hash{ (sort {$b <=> $a} keys %hash)[0] }"}, {"source_code": "$_ = <>;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! $n = \"$pr($`)$'\", $h{eval $n} = $n, '' !ge, '' /ge;\n\nuse bigint;\nprint eval $h{ (sort {$b <=> $a} keys %h)[0] }\n"}], "negative_code": [{"source_code": "$_ = <>;\n$m =-~ 0;\ns/(^|\\*)\\K/ $pr = $`, ($po = $') =~ s!(?=\\*)|$! ( ($r = eval \"$pr($`)$'\") > $m and $m = $r), '' !ge, '' /ge;\nprint $m"}], "src_uid": "39dbd405be19c5a56c2b97b28e0edf06"} {"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 \u2014 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": "#!perl -pl\ns/\\s+/ /g;\ns/\\s*([.,!?])\\s*/$1 /g;\n\n"}, {"source_code": "$_=<>;s/( )+/ /g;s/ ?([\\.!\\?,]) ?/$1 /g;print;"}, {"source_code": "#!/usr/bin/env perl\n$_=<>;s/( )+/ /g;s/ ?([\\.!\\?,]) ?/$1 /g;print;\n"}, {"source_code": "#!/usr/bin/env perl\npackage Codeforces::Task::_;\nuse v5.10;\nuse strict;\nuse warnings;\n\nsub min{my($a,$b)=@_;if($a<$b){return$a;}else{return$b;}}\nsub max{my($a,$b)=@_;if($a>$b){return$a;}else{return$b;}}\nsub out{foreach my$c(@_){print\"$c \";}print(\"\\n\");}\n\nsub solve {\n my $s = <>;\n $s =~ s/( )+/ /g;\n $s =~ s/ ?([\\.,!\\?]) ?/$1 /g;\n say $s;\n}\n\nsub main {\n my $t = 1;\n# $t = int(<>);\n while ($t--) {\n solve();\n }\n return 0;\n}\n\nexit(main(@ARGV));\n"}, {"source_code": "#!perl -pl\ns/\\s+/ /g;\ns/\\s*([.,!?])\\s*/$1 /g;\n"}, {"source_code": "#!perl -pl\ns/\\s+/ /g;\ns/\\s*([.,!?])\\s*/$1 /g;\n\n"}, {"source_code": "#!perl -pl\ns/\\s+/ /g;\ns/\\s*([.,!?])\\s*/$1 /g;\n\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nwhile () {\n s/(\\s)+/$1/g;\n s/\\s*([\\.,\\?!])\\s*/$1 /g;\n s/^( *)|( *)$//g;\n print;\n}\n"}, {"source_code": "#!perl -pl\ns/\\s+/ /g;\ns/\\s*([.,!?])\\s*/$1 /g;"}, {"source_code": "use strict;\n \nwhile () {\n s/(\\s)+/$1/g;\n s/\\s*([\\.,\\?!])\\s*/$1 /g;\n s/^( *)|( *)$//g;\n print;\n}"}, {"source_code": "#!perl -pl\ns/\\s+/ /g;\ns/\\s*([.,!?])\\s*/$1 /g;\n\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nwhile () {\n s/(\\s)+/$1/g;\n s/\\s*([\\.,\\?!])\\s*/$1 /g;\n s/^( *)|( *)$//g;\n print;\n}"}, {"source_code": "#!/usr/bin/perl -w\nwhile ()\n{\n s/\\s+/ /g;\n s/\\s+$//g;\n s/\\s*([?,.!])\\s*/$1 /g;\n\n print $_, \"\\n\";\n}"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nwhile () {\n s/(\\s)+/$1/g;\n s/\\s*([\\.,\\?!])\\s*/$1 /g;\n s/^( *)|( *)$//g;\n print;\n}\n"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nwhile () {\n s/(\\s)+/$1/g;\n s/\\s*([\\.,\\?!])\\s*/$1 /g;\n s/^( *)|( *)$//g;\n print;\n}"}, {"source_code": "#!perl -pl\ns/\\s+/ /g;\ns/\\s*([.,!?])\\s*/$1 /g;\n\n"}, {"source_code": "#!perl -pl\ns/\\s+/ /g;\ns/\\s*([.,!?])\\s*/$1 /g;\n\n"}, {"source_code": "while (<>){\n\ts/([,.!?])( {2,})?(\\w)/$1 $3/g;\n\ts/ +/ /g;\n\ts/(\\w) +([,.!?])/$1$2/g;\n\tprint;\n\t}"}, {"source_code": "#!perl -pl\ns/\\s+/ /g;\ns/\\s*([.,!?])\\s*/$1 /g;\n\n"}, {"source_code": "$_=<>;\nchomp;\ns/\\s+/ /g;\ns/\\s*([,\\.\\?!])\\s*/$1 /g;\nprint;\n"}, {"source_code": "#!perl -lp\ns/\\s+/ /g;\ns/\\s*([,.?!])\\s*/$1 /g;\n"}, {"source_code": "$_=<>;\nchomp;\ns/\\s+/ /g;\ns/ ([,\\.\\?!])/$1/g;\ns/([,\\.\\?!])(?=\\w)/$1 /g;\nprint;\n"}, {"source_code": "#!perl -pl\ns/\\s+/ /g;\ns/\\s*([.,!?])\\s*/$1 /g;\n"}, {"source_code": "#!perl -pl\ns/\\s+/ /g;\ns/\\s*([.,!?])\\s*/$1 /g;\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\nuse utf8;\n\nmy $str = <>;\n\n# Clear text\n$str =~ s/\\s+/ /g;\n$str =~ s/\\s*([?,.!])\\s*/$1 /g;\n\nprint $str;"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $str = <>;\n\n# Clear text\n$str =~ s/\\s+/ /g;\n$str =~ s/\\s([.,!?:;])/$1/g;\n$str =~ s/([.,!?:;])/$1 /g;\n\nprint $str, \"\\n\";"}, {"source_code": "#!/usr/bin/perl -w\n# \u041f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442-\u0442\u043e?\nwhile ()\n{\n s/\\s+/ /g;\n s/\\s*([?,.!])\\s*/$1 /g;\n\n print $_, \"\\n\";\n}"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $str = <>;\n\n# Clear text\n$str =~ s/\\s+/ /g;\n$str =~ s/\\s([.,!?:;])/$1/g;\n$str =~ s/([.,!?:;])/$1 /g;\n$str =~ s/\\s+/ /g;\n\nprint $str, \"\\n\";"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\nuse utf8;\n\nmy $str = <>;\n\n# Clear text\n$str =~ s/\\s+/ /g;\n$str =~ s/\\s*([?,.!])\\s*/$1 /g;\n\nprint $str, \"\\n\";"}, {"source_code": "#!/usr/bin/perl -w\nwhile ()\n{\n s/\\s+/ /g;\n s/\\s*([?,.!])\\s*/$1 /g;\n\n print $_;\n}"}, {"source_code": "while (<>){\n\ts/([,.!?])( {2,})?(\\w)/$1 $3/g;\n\ts/(\\w) +(\\w)/$1 $2/g;\n\ts/(\\w) +([,.!?])/$1$2/g;\n\tprint;\n\t}"}, {"source_code": "$_=<>;\ns/\\s+/ /g;\ns/ ([,\\.\\?!])/$1/g;\ns/([,\\.\\?!])(?=\\w)/$1 /g;\nprint;\n"}], "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$$$) \u2014 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 \u2014 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": "#!/usr/bin/env perl\npackage Codeforces::Task::A;\nuse v5.10;\nuse strict;\nuse warnings;\n\nsub min {\n my ($a, $b) = @_;\n if ($a < $b) {\n\t\treturn $a;\n\t} else {\n return $b;\n }\n}\nsub max {\n my ($a, $b) = @_;\n if ($a > $b) {\n return $a;\n } else {\n return $b;\n }\n}\n\n\nsub solve {\n my ($h, $m) = map(int, split(\" \", <>));\n print 24 * 60 - $m - 60 * $h, \"\\n\";\n}\n\nsub main {\n my $t = 1;\n $t = int(<>);\n while ($t--) {\n solve();\n }\n return 0;\n}\n\nexit(main(@ARGV));\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $h, $min ) = split;\n\t\n\tprint 24 * 60 - ( $h * 60 + $min );\n\t}"}], "negative_code": [], "src_uid": "f4982de28aca7080342eb1d0ff87734c"} {"nl": {"description": "Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly x distinct squares in the table consisting of n rows and m columns. For example, in a 3\u2009\u00d7\u20095 table there are 15 squares with side one, 8 squares with side two and 3 squares with side three. The total number of distinct squares in a 3\u2009\u00d7\u20095 table is 15\u2009+\u20098\u2009+\u20093\u2009=\u200926.", "input_spec": "The first line of the input contains a single integer x (1\u2009\u2264\u2009x\u2009\u2264\u20091018)\u00a0\u2014 the number of squares inside the tables Spongebob is interested in.", "output_spec": "First print a single integer k\u00a0\u2014 the number of tables with exactly x distinct squares inside. Then print k pairs of integers describing the tables. Print the pairs in the order of increasing n, and in case of equality\u00a0\u2014 in the order of increasing m.", "sample_inputs": ["26", "2", "8"], "sample_outputs": ["6\n1 26\n2 9\n3 5\n5 3\n9 2\n26 1", "2\n1 2\n2 1", "4\n1 8\n2 3\n3 2\n8 1"], "notes": "NoteIn a 1\u2009\u00d7\u20092 table there are 2 1\u2009\u00d7\u20091 squares. So, 2 distinct squares in total. In a 2\u2009\u00d7\u20093 table there are 6 1\u2009\u00d7\u20091 squares and 2 2\u2009\u00d7\u20092 squares. That is equal to 8 squares in total. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\n$x = ;\n$ans = 0;\n@arr = ();\nfor($i = 1;$i <= $x;$i ++) {\n $tmp = $x * 6;\n if($tmp / $i / ($i + 1) + $i - 1 < 3 * $i) {\n last;\n }\n if ($tmp % $i > 0) {\n next;\n }\n $tmp /= $i;\n if ($tmp % ($i + 1) > 0) {\n next;\n }\n $tmp /= $i + 1;\n $tmp += $i - 1;\n if ($tmp % 3 > 0) {\n next;\n }\n push @arr, $i;\n if($tmp / 3 > $i) {\n push @arr, $tmp / 3;\n }\n}\n$len = @arr;\n@sorted = sort {int($a) <=> int($b)} @arr;\nprint $len, \"\\n\";\nfor ($i = 0;$i < $len; $i ++ ) {\n print $sorted[$i], \" \", $sorted[$len - 1 - $i], \"\\n\";\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\n$x = ;\n$ans = 0;\nfor($i = 1;$i <= $x;$i ++) {\n $tmp = $x * 6;\n if($tmp / $i / ($i + 1) + $i - 1 < 3 * $i) {\n last;\n }\n if ($tmp % $i > 0) {\n next;\n }\n $tmp /= $i;\n if ($tmp % ($i + 1) > 0) {\n next;\n }\n $tmp /= $i + 1;\n $tmp += $i - 1;\n if ($tmp % 3 > 0) {\n next;\n }\n if($tmp / 3 > $i) {\n $ans += 2; \n } else {\n $ans ++;\n }\n}\nprint $ans, \"\\n\";\n"}], "src_uid": "5891432c43cfee35a212ad92d7da2a64"} {"nl": {"description": "Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes.Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters \"a\", \"e\", \"i\", \"o\", \"u\" are considered vowels.Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k\u2009=\u20091, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k\u2009=\u20092, they do not rhyme (ommit\u2009\u2260\u2009ermit).Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): Clerihew (aabb); Alternating (abab); Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa).If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme.Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task.", "input_spec": "The first line contains two integers n and k (1\u2009\u2264\u2009n\u2009\u2264\u20092500, 1\u2009\u2264\u2009k\u2009\u2264\u20095)\u00a0\u2014 the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on.", "output_spec": "Print the rhyme scheme of the poem as \"aabb\", \"abab\", \"abba\", \"aaaa\"; or \"NO\" if the poem does not belong to any of the above mentioned schemes.", "sample_inputs": ["1 1\nday\nmay\nsun\nfun", "1 1\nday\nmay\ngray\nway", "2 1\na\na\na\na\na\na\ne\ne", "2 1\nday\nmay\nsun\nfun\ntest\nhill\nfest\nthrill"], "sample_outputs": ["aabb", "aaaa", "aabb", "NO"], "notes": "NoteIn the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is \"NO\"."}, "positive_code": [{"source_code": "($n, $k) = split \" \", <>;\n$v = qr/[aeiou].*/;\ns/^.*(?=${v}{$k}$)// && push @_, $_ for <>;\nif (@_ != $n * 4) { $f++ }\nelse {\nchomp @_;\nfor $i (1 .. $n) { \n\t($a, $b, $c, $d) = @_[ $i * 4 - 4 .. $i * 4 - 1 ];\n\tif ($a eq $b && $b eq $c && $c eq $d) { push @A, aaaa }\n\telsif ($a eq $b && $c eq $d) { push @A, aabb }\n\telsif ($a eq $d && $b eq $c) { push @A, abba }\n\telsif ($a eq $c && $b eq $d) { push @A, abab }\n\telse { $f++ }\n\t}\n$_ = \"@A\";\n$A = ( aaaa, $& )[ /aabb/ + /abab/ + /abba/ ];\n}\nprint $f || !$A ? 'NO' : $A"}], "negative_code": [], "src_uid": "a17bac596b1f060209534cbffdf0f40e"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of people. The next n lines each contains two strings. The i-th line contains strings fi and si (1\u2009\u2264\u2009|fi|,\u2009|si|\u2009\u2264\u200950) \u2014 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,\u2009p2,\u2009...,\u2009pn (1\u2009\u2264\u2009pi\u2009\u2264\u2009n).", "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": "<>;\nchomp(@_=<>);\n@i=split/ /,pop @_;\n$j='';\n$f=0;\nfor (@i){\n($a,$b) = sort split/ /,$_[$_-1];\nif ($a gt $j){$j=$a}\nelsif ($b gt $j){$j=$b}\nelse {$f++; last}\n}\nprint $f ? NO:YES\n\n"}, {"source_code": "<>;\nchomp(@_=<>);\n@i=split/ /,pop @_;\n$j='';\n$f=0;\nfor (@i){\n($a,$b) = sort split/ /,$_[$_-1];\nif ($a gt $j){$j=$a}\nelsif ($b gt $j){$j=$b}\nelse {$f++; last}\n}\nprint $f ? NO:YES\n\n"}, {"source_code": "<>;\nchomp(@_=<>);\n@i=split/ /,pop @_;\n$j='';\n$f=0;\nfor (@i){\n($a,$b) = sort split/ /,$_[$_-1];\nif ($a gt $j){$j=$a}\nelsif ($b gt $j){$j=$b}\nelse {$f++; last}\n}\nprint $f ? NO:YES\n\n"}, {"source_code": "<>;\nchomp(@_=<>);\n@i=split/ /,pop @_;\n$j='';\n$f=0;\nfor (@i){\n($a,$b) = sort split/ /,$_[$_-1];\nif ($a gt $j){$j=$a}\nelsif ($b gt $j){$j=$b}\nelse {$f++; last}\n}\nprint $f ? NO:YES\n\n"}, {"source_code": "use List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nchomp (my $n = <>);\n\nmy @names = (q/junk/);\nwhile (<>) {\n chomp;\n push @names, $_;\n}\nmy @order = split /\\s+/, pop @names;\n\nmy $rsp = q/YES/;\n\nmy @h;\nmy $lH = '-';\nforeach (@order) {\n @h = sort {$a cmp $b} split /\\s+/, $names[$_];\n if ($lH lt $h[0]) {\n $lH = $h[0]; next;\n } elsif ($lH lt $h[1]) {\n $lH = $h[1]; next;\n } else {\n $rsp = q/NO/; last;\n }\n}\n\nprintf \"$rsp\\n\";"}, {"source_code": "<>;\nchomp(@_=<>);\n@i=split/ /,pop @_;\n$j='';\n$f=0;\nfor (@i){\n($a,$b) = sort split/ /,$_[$_-1];\nif ($a gt $j){$j=$a}\nelsif ($b gt $j){$j=$b}\nelse {$f++; last}\n}\nprint $f ? NO:YES\n\n"}, {"source_code": "<>;\nchomp(@_=<>);\n@i=split/ /,pop @_;\n$j='';\n$f=0;\nfor (@i){\n($a,$b) = sort split/ /,$_[$_-1];\nif ($a gt $j){$j=$a}\nelsif ($b gt $j){$j=$b}\nelse {$f++; last}\n}\nprint $f ? NO:YES\n\n"}, {"source_code": "<>;\nchomp(@_=<>);\n@i=split/ /,pop @_;\n$j='';\n$f=0;\nfor (@i){\n($a,$b) = sort split/ /,$_[$_-1];\nif ($a gt $j){$j=$a}\nelsif ($b gt $j){$j=$b}\nelse {$f++; last}\n}\nprint $f ? NO:YES\n\n"}, {"source_code": "<>;\nchomp(@_=<>);\n@i=split/ /,pop @_;\n$j='';\n$f=0;\nfor (@i){\n($a,$b) = sort split/ /,$_[$_-1];\nif ($a gt $j){$j=$a}\nelsif ($b gt $j){$j=$b}\nelse {$f++; last}\n}\nprint $f ? NO:YES\n"}, {"source_code": "<>;\nchomp(@_=<>);\n@i=split/ /,pop @_;\n$j='';\n$f=0;\nfor (@i){\n($a,$b) = sort split/ /,$_[$_-1];\nif ($a gt $j){$j=$a}\nelsif ($b gt $j){$j=$b}\nelse {$f++; last}\n}\nprint $f ? NO:YES\n\n"}], "negative_code": [], "src_uid": "a48ef749bf54d673772e09025ae806de"} {"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\u2009+\u20091)-th house of the same row has waiting time equal to aij (1\u2009\u2264\u2009i\u2009\u2264\u20092,\u20091\u2009\u2264\u2009j\u2009\u2264\u2009n\u2009-\u20091). 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\u2009\u2264\u2009j\u2009\u2264\u2009n). 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\u2009\u2264\u2009n\u2009\u2264\u200950) \u2014 the number of houses in each row. Each of the next two lines contains n\u2009-\u20091 space-separated integer \u2014 values aij (1\u2009\u2264\u2009aij\u2009\u2264\u2009100). The last line contains n space-separated integers bj (1\u2009\u2264\u2009bj\u2009\u2264\u2009100).", "output_spec": "Print a single integer \u2014 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\nwhile($n = <>){\n\t@a = split ' ' , <>;\n\t@b = split ' ' , <>;\n\t@c = split ' ' , <>;\n\t\n\t$sum = 0;\n\t@sa = (0, map { $sum += $_ } @a);\n\t\n\t$sum = 0;\n\t@sb = reverse (0, map { $sum += $_ } reverse @b);\n\t\n\t$min = ~0;\n\t\n\tfor $i (1 .. $n){\n\t\tfor $j ($i + 1 .. $n){\n\t\t\t$V = eval join '+', map\n\t\t\t\t{ $sa[ $_ - 1 ] + $sb[ $_ - 1 ] + $c[ $_ - 1 ] }\n\t\t\t\t$i, $j;\n\t\t\t$V < $min and $min = $V;\n\t\t\t}\n\t\t}\n\t\t\n\tprint $min\n\t}"}], "negative_code": [], "src_uid": "e3e0625914fdc08950601248b1278f7d"} {"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$$$)\u00a0\u2014 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$$$) \u2014 the lengths of the sticks.", "output_spec": "Print one integer\u00a0\u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\t@_ = sort { $a <=> $b } @_;\n\t\n\tmy $h;\n\tmy $v;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\tif( $i <= @_ / 2 - 1 ){\n\t\t\t$h += $_[ $i ];\n\t\t\t}\n\t\telse{\n\t\t\t$v += $_[ $i ];\n\t\t\t}\n\t\t}\n\t\n\tuse bigint;\n\tprint $h ** 2 + $v ** 2;\n\tno bigint;\n\t}"}], "negative_code": [], "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\u2009-\u20091 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\u2009>\u2009j.", "input_spec": "The first line contains an integer n (2\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of rows and columns. Then follow n\u2009-\u20091 lines that contain one's positions, one per line. Each position is described by two integers xk,\u2009yk (1\u2009\u2264\u2009xk,\u2009yk\u2009\u2264\u2009n), separated by a space. A pair (xk,\u2009yk) 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\u2009\u2264\u2009105) \u2014 the number of actions. In each of the next m lines print three space-separated integers t,\u2009i,\u2009j (1\u2009\u2264\u2009t\u2009\u2264\u20092,\u20091\u2009\u2264\u2009i,\u2009j\u2009\u2264\u2009n,\u2009i\u2009\u2260\u2009j), where t\u2009=\u20091 if you want to swap rows, t\u2009=\u20092 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/perl\n\nuse strict;\nuse warnings;\n\nuse List::Util qw(max);\n\nuse constant INF => 0x3f3f3f3f;\n\nsub gao {\n my ($n, $s, @a) = @_;\n my @index = 0 .. $n - 1;\n my @ret = ();\n for my $i (0 .. $n - 1) {\n if ($a[$i] != $index[$i]) {\n for my $j ($i .. $n - 1) {\n if ($index[$j] == $a[$i]) {\n ($index[$i], $index[$j]) = ($index[$j], $index[$i]);\n push @ret, [$s, $i, $j];\n last;\n }\n }\n }\n }\n @ret;\n}\n\nchomp(my $n = <>);\nmy @row = ();\nmy @col = ();\npush @row, [] for (1 .. $n);\npush @col, [] for (1 .. $n);\nmy @res = ();\nfor (1 .. $n - 1) {\n chomp($_ = <>);\n my ($r, $c) = split;\n ($r, $c) = ($r - 1, $c - 1);\n push @{$row[$r]}, $c;\n push @{$col[$c]}, $r;\n}\n\nfor my $i (0 .. $n - 1) {\n @{$row[$i]} = sort { $a <=> $b } @{$row[$i]};\n @{$col[$i]} = sort { $a <=> $b } @{$col[$i]};\n}\n\nmy @col_i = 0 .. $n - 1;\n@col_i = reverse sort { @{$col[$a]} <=> @{$col[$b]} } @col_i;\n\nmy @least = (0) x $n;\nfor my $i (0 .. $n - 1) {\n my $t = $col_i[$i];\n for my $j (@{$col[$t]}) {\n $least[$j] = max $least[$j], $i + 1;\n }\n}\nmy @row_i = sort { $least[$a] <=> $least[$b] } 0 .. $n - 1;\n\npush @res, gao($n, 1, @row_i);\npush @res, gao($n, 2, @col_i);\n\nprint scalar @res, \"\\n\";\nfor (@res) {\n $$_[1] += 1;\n $$_[2] += 1;\n print \"@$_\\n\";\n}\n\n"}], "negative_code": [], "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\u2009\u2264\u2009n\u2009\u2264\u2009105). 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": "\n#!/usr/bin/perl\n\nmain();\n\nsub main {\n my $n = int <>;\n my %hash = ();\n for(1..$n) {\n my $name = <>;\n $name =~ s/[\\W]//;\n my $add = \"\";\n if (exists $hash{$name}) {\n $add = $hash{$name};\n print(\"$name$add\\n\");\n $hash{$name} = $add + 1; \n } else {\n print(\"OK\\n\");\n $hash{$name} = 1;\n }\n }\n}\n"}, {"source_code": "chomp($count = );\nfor ($i = 0; $i < $count; ++$i)\n{\n chomp($newName = );\n push @names, $newName;\n}\nforeach (@names)\n{\n if ( ! exists $logins{$_} )\n {\n $logins{$_}++;\n print \"OK\\n\";\n }\n else\n {\n print \"$_$logins{$_}\\n\";\n $logins{$_}++;\n }\n}\n"}, {"source_code": "my %hash;\nfor my $i(1..int<>) {\n chomp($_ = <>);\n if (not exists $hash{$_}) {\n print \"OK\\n\";\n $hash{$_} = 1;\n next;\n }\n print $_ . $hash{$_} . \"\\n\";\n $hash{$_}++;\n}\n"}, {"source_code": "$\\=\"\\n\";\nfor$i(1..<>)\n{\n chomp($_=<>);\n if (!defined($h{$_}))\n {\n print \"OK\";\n $h{$_}=1;\n } else\n {\n print $_,$h{$_}++;\n }\n}"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\nforeach (1 .. $n) {\n\tchomp($line = <>);\n\tdefined $tb{$line} and say $line,$tb{$line} or say \"OK\";\n\t++$tb{$line};\n}"}, {"source_code": "my $n = <>;\nmy %names;\n\nfor (1..$n) {\n my $name = <>;\n chomp $name;\n if (exists $names{$name}) {\n print $name, $names{$name}++, \"\\n\";\n } else {\n $names{$name} = 1;\n print \"OK\\n\";\n }\n}\n"}, {"source_code": " \n<>;\nwhile(<>){\nchomp;\npush @_, $n{$_}? ($_.$n{$_}) : OK ;\n$n{$_}++\n}\n$,=\"\\n\";\nprint @_"}, {"source_code": "<>;\n\nwhile(<>)\n{\n\tchomp();\n\tif(not exists $h{$_}){\n\t\tprint \"OK\\n\";\n\t}\n\telse{\n\t\tprint $_, $h{$_}, \"\\n\";\n\t}\n\t$h{$_} ++;\n\t\n}"}, {"source_code": "#!perl\n\nfor (1..) {\n\tchomp ($name = );\n\tif (!defined($names{$name})) {\n\t\tprint \"OK\\n\";\n\t\t$names{$name} = 0;\n\t} else {\n\t\tprint $name, ++$names{$name}, \"\\n\";\n\t}\n}"}, {"source_code": "#!usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $n=;\nmy %list;my $p=0;\nmy @output;\nfor my $i(0..$n-1){\n my $name=;\n chomp($name);\n if(!defined($list{$name})){\n\t$list{$name}=$name;\n\t$output[$i]=\"OK\\n\";\n }\n else{\n\t$p=$list{$name}=~s/[a-z]+/0/r+1;\n\t$list{$name}=$name.$p;\n\t$output[$i]=$list{$name}.\"\\n\";\n }\n}\nprint @output;"}, {"source_code": "#!/usr/bin/perl\n\n$n=;\n$n=int($n);\n\n%db=();\n\nfor($i=0;$i<$n;$i++) {\n\t$name=;\n\tchomp($name);\n\tif($db{$name}) {\n\t\t$name2=$name . $db{$name};\n\t\t$db{$name}=$db{$name}+1;\n\t\tprint \"$name2\\n\";\n\t} else {\n\t\t$db{$name}=1;\n\t\tprint \"OK\\n\";\n\t}\n}"}, {"source_code": "\nchomp($n = );\n\n%dic = {};\n@userlist = [];\npop @userlist;\n\nfor( $i = 0; $i < $n; $i++){\n chomp($name = );\n if (exists $dic{$name}) {\n #$dic{$name} = $dic{$name} + 1;\n $dic{$name}++;\n $string = $name.$dic{$name};\n push @userlist , ($string);\n }\n else\n {\n $dic{$name} = 0;\n push @userlist, \"OK\";\n }\n}\n\n\nforeach $name(@userlist){\n\tprint \"$name\\n\";\n}\n\n\n"}, {"source_code": "<>;\nwhile(<>){\nchomp;\npush @_, $n{$_}? ($_.$n{$_}) : OK ;\n$n{$_}++\n}\n$,=\"\\n\";\nprint @_"}, {"source_code": "$len = ;\n$hashMap;\n\nfor my $val (1 .. $len) {\n\t$input = ;\n\t$input =~ s/\\s//g;\n\n\tif ($hashMap->{$input}) {\n\t\tprint $input.$hashMap->{$input}.\"\\n\";\n\t\t++$hashMap->{$input};\n\t} else {\n\t\t$hashMap->{$input} = 1;\n\t\tprint \"OK\\n\";\n\t}\n}"}], "negative_code": [{"source_code": "#!perl\n\nfor (1..) {\n\tchomp ($name = );\n\tif (!defined($names{$name})) {\n\t\tprint \"OK\";\n\t\t$names{$name} = 0;\n\t} else {\n\t\tprint $name, ++$names{$name}, \"\\n\";\n\t}\n}"}, {"source_code": "#!perl\n\nfor (1..) {\n\tchomp ($name = );\n\tif (!defined($names{$name})) {\n\t\tprint \"OK\";\n\t\t$names{$name} = 0;\n\t} else {\n\t\tprint $name, ++$names{$name};\n\t}\n}"}, {"source_code": "#!usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $n=;\nmy %list;\nmy @output;\nfor my $i(0..$n-1){\n my $name=;\n chomp($name);\n if(!defined($list{$name})){\n\t$list{$name}=$name;\n\t$output[$i]=\"OK\\n\";\n }\n else{\n\tmy $p=$name=~s/\\w+/0/r;\n\t$list{$name}=$name.($p+1);\n\t$output[$i]=$list{$name}.\"\\n\";\n }\n}\nprint @output;"}, {"source_code": "<>;\nwhile(<>){\nchomp;\npush @_, $n{$_}? ($_.$n{$_}++) : OK\n}\n$,=\"\\n\";\nprint @_"}], "src_uid": "24098df9c12d9704391949c9ff529c98"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 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 \u2014 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": "<>;\t\n$_=<>;\n@a=split//;\n\t\n\twhile(<>){\n\t\tchomp;\n\t\t$j=-1;\n\t\tfor (split//){\n\t\t\t(($a=$a[++$j]) eq 'X' or $_ eq '?') && next;\n\t\t\t$_ eq $a or ($a[$j]= $a ne '?'? X: $_)\n\t\t\t}\n\t\t}\n\n\ty/?X/x?/ for @a;\n\t\nprint @a\n"}, {"source_code": "<>;\t\n$_=<>;\n@a=split//;\n\t\n\twhile(<>){\n\t\tchomp;\n\t\t$j=-1;\n\t\tfor (split//){\n\t\t\t(($a=$a[++$j]) eq 'X' or $_ eq '?') && next;\n\t\t\t$_ eq $a or ($a[$j]= $a ne '?'? X: $_)\n\t\t\t}\n\t\t}\n\n\ty/?X/x?/ for @a;\n\t\nprint @a\n"}, {"source_code": "<>;\t\n$_=<>;\n@a=split//;\n\t\n\twhile(<>){\n\t\tchomp;\n\t\t$j=-1;\n\t\tfor (split//){\n\t\t\t(($a=$a[++$j]) eq 'X' or $_ eq '?') && next;\n\t\t\t$_ eq $a or ($a[$j]= $a ne '?'? X: $_)\n\t\t\t}\n\t\t}\n\n\ty/?X/x?/ for @a;\n\t\nprint @a\n"}, {"source_code": "<>;\t\n$_=<>;\n@a=split//;\n\t\n\twhile(<>){\n\t\tchomp;\n\t\t$j=-1;\n\t\tfor (split//){\n\t\t\t(($a=$a[++$j]) eq 'X' or $_ eq '?') && next;\n\t\t\t$_ eq $a or ($a[$j]= $a ne '?'? X: $_)\n\t\t\t}\n\t\t}\n\n\ty/?X/x?/ for @a;\n\t\nprint @a\n"}, {"source_code": "<>;\t\n$_=<>;\n@a=split//;\n\t\n\twhile(<>){\n\t\tchomp;\n\t\t$j=-1;\n\t\tfor (split//){\n\t\t\t(($a=$a[++$j]) eq 'X' or $_ eq '?') && next;\n\t\t\t$_ eq $a or ($a[$j]= $a ne '?'? X: $_)\n\t\t\t}\n\t\t}\n\n\ty/?X/x?/ for @a;\n\t\nprint @a\n"}, {"source_code": "<>;\t\n$_=<>;\n@a=split//;\n\t\n\twhile(<>){\n\t\tchomp;\n\t\t$j=-1;\n\t\tfor (split//){\n\t\t\t(($a=$a[++$j]) eq 'X' or $_ eq '?') && next;\n\t\t\t$_ eq $a or ($a[$j]= $a ne '?'? X: $_)\n\t\t\t}\n\t\t}\n\n\ty/?X/x?/ for @a;\n\t\nprint @a\n"}, {"source_code": "<>;\t\n$_=<>;\n@a=split//;\n\t\n\twhile(<>){\n\t\tchomp;\n\t\t$j=-1;\n\t\tfor (split//){\n\t\t\t(($a=$a[++$j]) eq 'X' or $_ eq '?') && next;\n\t\t\t$_ eq $a or ($a[$j]= $a ne '?'? X: $_)\n\t\t\t}\n\t\t}\n\n\ty/?X/x?/ for @a;\n\t\nprint @a\n"}, {"source_code": "<>;\t\n$_=<>;\n@a=split//;\n\t\n\twhile(<>){\n\t\t$j=-1;\n\t\twhile(/./g){\n\t\t\t(($a=$a[++$j]) eq 'X' or $& eq '?') && next;\n\t\t\t$& eq $a or ($a[$j]= $a ne '?'? X: $&)\n\t\t\t}\n\t\t}\n\n\t$_=join\"\",@a;\n\ts/\\?/x/g;\n\ts/X/?/g;\nprint"}, {"source_code": "<>;\t\n$_=<>;\n@a=split//;\n\t\n\twhile(<>){\n\t\tchomp;\n\t\t$j=-1;\n\t\tfor (split//){\n\t\t\t(($a=$a[++$j]) eq 'X' or $_ eq '?') && next;\n\t\t\t$_ eq $a or ($a[$j]= $a ne '?'? X: $_)\n\t\t\t}\n\t\t}\n\n\ty/?X/x?/ for @a;\n\t\nprint @a"}, {"source_code": "<>;\t\n$_=<>;\n@a=split//;\n\t\n\twhile(<>){\n\t\tchomp;\n\t\t$j=-1;\n\t\tfor (split//){\n\t\t\t(($a=$a[++$j]) eq 'X' or $_ eq '?') && next;\n\t\t\t$_ eq $a or ($a[$j]= $a ne '?'? X: $_)\n\t\t\t}\n\t\t}\n\n\t$_=join\"\",@a;\n\ts/\\?/x/g;\n\ts/X/?/g;\nprint"}, {"source_code": "while($n=<>){\n\t$_=<>;\n\tchomp;\n\t@a=split//;\n\t\n\tfor $i(2..$n){\n\t\t$_=<>;\n\t\tchomp;\n\t\t@_=split//;\n\t\t\n\t\tfor $j(0..@_-1){\n\t\t\t$a[$j] eq 'X' and next;\n\t\t\t$_[$j] eq '?' and next;\n\t\t\tif ($_[$j] ne $a[$j]){ \n\t\t\t\t\t if ($a[$j] ne '?') { $a[$j]='X'}\n\t\t\t\t\t else { $a[$j]=$_[$j] }\n\t\t\t\t\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\n\t\t\t}\n\t\n\t\t\n\t\t}\n#\tprint \"@a\\n\";\n\t$a=join\"\",@a;\n\t$a=~s/\\?/x/g;\n\t$a=~s/X/?/g;\n\tprint \"$a\\n\";\n\t\n\t}"}, {"source_code": "<>;\t\n$_=<>;\n@a=split//;\n\t\n\twhile(<>){\n\t\tchomp;\n\t\t$j=-1;\n\t\tfor (split//){\n\t\t\t(($a=$a[++$j]) eq 'X' or $_ eq '?') && next;\n\t\t\t$_ eq $a or ($a[$j]= $a ne '?'? X: $_)\n\t\t\t}\n\t\t}\n\n\ty/?X/x?/ for @a;\n\t\nprint @a\n"}], "negative_code": [], "src_uid": "a51d2e6e321d7db67687a594a2b85e47"} {"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\u2009-\u20091 and 0 to m\u2009-\u20091 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100). The second line contains integer b (0\u2009\u2264\u2009b\u2009\u2264\u2009n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1,\u2009x2,\u2009...,\u2009xb (0\u2009\u2264\u2009xi\u2009<\u2009n), denoting the list of indices of happy boys. The third line conatins integer g (0\u2009\u2264\u2009g\u2009\u2264\u2009m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1,\u2009y2,\u2009... ,\u2009yg (0\u2009\u2264\u2009yj\u2009<\u2009m), 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": "use strict;\n\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nmy ($n, $m) = split / /, <>;\nmy $b;\nmy @bs;\n$#bs = $n-1;\n\nmy @i = split / /, <>;\n$b = shift @i;\nfor(@i)\n{\n $bs[$_] = 1;\n}\nmy @gs;\n$#gs = $m-1;\n@i = split / /, <>;\n\nmy $g = shift @i;\nfor(@i)\n{\n $gs[$_] = 1;\n}\nif($b == 0 and $g == 0){ print \"No\";exit 0; }\n\nmy $nod = nod($m, $n);\n\nmy @t; $#t = $nod-1;\n\nfor (0..max ($n, $m))\n{\n $t[$_ % $nod] = $bs[$_ % $n] || $gs[$_ % $m] || $t[$_ % $nod];\n}\nmy $flag = 1;\nfor(@t)\n{\n $flag *= $_;\n}\n\nprint \"Yes\" if ($flag);\nprint \"No\" unless ($flag);\n\n\n\n\n\n\n\n\n\n\nsub nod\n {\n return $_[0] != 0 ? nod ( ( $_[1] % $_[0] ), $_[0] ) : $_[1];\n };\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "sub gcf {\n my ($x, $y) = @_;\n ($x, $y) = ($y, $x % $y) while $y;\n return $x;\n}\n\nsub lcm {\n return($_[0] * $_[1] / gcf($_[0], $_[1]));\n}\n\nchomp (my ($n, $m) = split /\\s+/, <>);\nmy @boys = (0) x $n;\nmy @girls = (0) x $m;\nchomp (my @hb = split /\\s+/, <>);\nchomp (my @hg = split /\\s+/, <>);\n\nmy $hcnt = 0;\n$hcnt += shift @hb;\n$hcnt += shift @hg;\n\n$boys[$_] = 1 foreach (@hb);\n$girls[$_] = 1 foreach (@hg);\n\nmy $prevhcnt;\nmy ($i, $j);\n\nrepeat:\n\n$prevhcnt = $hcnt;\n($i, $j) = (0, 0);\nforeach my $x (0 .. &lcm($n, $m) - 1) {\n $i++;\n $j++;\n $i = ($n > $i) ? $i : 0;\n $j = ($m > $j) ? $j : 0;\n if ($boys[$i] ^ $girls[$j]) {\n $boys[$i] = 1;\n $girls[$j] = 1;\n $hcnt++;\n }\n}\n# printf \"@boys\\n@girls\\n$hcnt\\n$prevhcnt\\n\";\ngoto repeat if ($hcnt > $prevhcnt);\n\nif ($hcnt == ($m + $n)) {\n printf \"YES\\n\";\n} else {\n printf \"NO\\n\";\n}\n"}, {"source_code": "$\\ = $/;\nwhile(<>){\n\t($B,$G)=split;\n\t$b=<>;\n\t$g=<>;\n\t@b = (0) x $B;\n\t@g = (0) x $G;\n\t@B = (split \" \", $b);shift @B;\n\t@G = (split \" \", $g);shift @G;\n\t$b[$_] = 1 for @B;\n\t$g[$_] = 1 for @G;\n\tfor $i (0 .. 2e4){\n\t\t$b[$i % $B] || $g[$i % $G] and $b[$i % $B] = 1 and $g[$i % $G] = 1;\n\t\t}\n\tprint \"@b@g\" =~ /0/ ? \"No\":\"Yes\"\n\t}"}], "negative_code": [{"source_code": "chomp (my ($n, $m) = split /\\s+/, <>);\nmy @boys = (0) x $n;\nmy @girls = (0) x $m;\nchomp (my @hb = split /\\s+/, <>);\nchomp (my @hg = split /\\s+/, <>);\n\nmy $hcnt = 0;\n$hcnt += shift @hb;\n$hcnt += shift @hg;\n\n$boys[$_] = 1 foreach (@hb);\n$girls[$_] = 1 foreach (@hg);\n\nmy $prevhcnt;\n\n$n--;\n$m--;\n\nrepeat:\n$prevhcnt = $hcnt;\nforeach my $i (0 .. $n) {\n foreach my $j (0 .. $m) {\n if ($boys[$i] ^ $girls[$j]) {\n $girls[$j] = 1;\n $boys[$i] = 1;\n $hcnt++;\n }\n }\n}\n# printf \"@boys\\n@girls\\n$hcnt\\n$prevhcnt\\n\";\ngoto repeat if ($hcnt > $prevhcnt);\n\nif ($hcnt == ($m + $n + 2)) {\n printf \"YES\\n\";\n} else {\n printf \"NO\\n\";\n}"}, {"source_code": "sub gcf {\n my ($x, $y) = @_;\n ($x, $y) = ($y, $x % $y) while $y;\n return $x;\n}\n\nsub lcm {\n return($_[0] * $_[1] / gcf($_[0], $_[1]));\n}\n\nchomp (my ($n, $m) = split /\\s+/, <>);\nmy @boys = (0) x $n;\nmy @girls = (0) x $m;\nchomp (my @hb = split /\\s+/, <>);\nchomp (my @hg = split /\\s+/, <>);\n\nmy $hcnt = 0;\n$hcnt += shift @hb;\n$hcnt += shift @hg;\n\n$boys[$_] = 1 foreach (@hb);\n$girls[$_] = 1 foreach (@hg);\n\nmy $prevhcnt;\nmy ($i, $j);\n\nrepeat:\n\n$prevhcnt = $hcnt;\n($i, $j) = (0, 0);\nforeach my $x (0 .. &lcm($n, $m) - 1) {\n $i++;\n $j++;\n $i = ($n > $i) ? $i : 0;\n $j = ($m > $j) ? $j : 0;\n if ($boys[$i] ^ $girls[$j]) {\n $boys[$i] = 1;\n $girls[$j] = 1;\n $hcnt++;\n }\n}\nprintf \"@boys\\n@girls\\n$hcnt\\n$prevhcnt\\n\";\ngoto repeat if ($hcnt > $prevhcnt);\n\nif ($hcnt == ($m + $n + 2)) {\n printf \"YES\\n\";\n} else {\n printf \"NO\\n\";\n}\n"}], "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})$$$ \u2014 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$$$ \u2014 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": "# author : Gabriel Hofer\nuse warnings;\nuse strict;\nuse constant NL => qq/\\n/;\n\nmy ($n, $m) = ( =~ m /(\\d+)\\s(\\d+)/ );\nmy @one = split qq/ /, ;\nmy @two = split qq/ /, ;\nmy @three = @one;\nfor ( my $i = 1; $i <= $#three; $i += 1 ) { \n $three [ $i ] += $three [ $i - 1 ] }\n\nAA: for my $i ( @two ) {\n my ($left, $right, $mid);\n $left = 0;\n $right = $#three;\n AB: while ( $left < $right ) {\n $mid = int ( ( $left + $right ) / 2 );\n if ( $i < $three [ $mid ] ) { $right = $mid }\n elsif ( $i > $three [ $mid ] ) { $left = $mid + 1 }\n else { last AB }\n }\n $mid = int ( ( $left + $right ) / 2 );\n print ( $mid + 1, qq/ / );\n if ( $mid == 0 ) { print qq/$i/ } \n else { print ( $i - $three [ $mid - 1 ] ) }\n print NL;\n}\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "$\\ = $/;\n<>;\n\n@_ = split ' ', <>;\n\n$h = 1;\n\nfor( split ' ', <> ){\n\t\n\twhile( @_ and $_[ 0 ] + $c < $_ ){\n\t\t$h ++;\n\t\t$c += shift @_;\n\t\t}\n\t\n\tprint \"$h \" . ( $_ - $c )\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t@_ = split ' ', <>;\n\t\n\tmy $house = 1;\n\tmy $accum = 0;\n\t\n\tfor my $i ( split ' ', <> ){\n\t\t\n\t\twhile( @_ and $_[ 0 ] + $accum < $i ){\n\t\t\t$house ++;\n\t\t\t$accum += $_[ 0 ];\n\t\t\tshift @_;\n\t\t\t}\n\t\t\n\t\tprint $house . ' ' . ( $i - $accum );\n\t\t}\n\t\n\t}"}, {"source_code": "$\\ = $/;\n<>;\n\n@_ = split ' ', <>;\n\n$h = 1;\n\nfor( split ' ', <> ){\n\t\n\twhile( @_ and $_[ 0 ] + $c < $_ ){\n\t\t$h ++;\n\t\t$c += shift @_;\n\t\t}\n\t\n\tprint \"$h \" . ( $_ - $c )\n\t}"}], "negative_code": [], "src_uid": "56bdab2019ee12e18d4f7e17ac414962"} {"nl": {"description": "A sequence of positive integers is called great for a positive integer $$$x$$$, if we can split it into pairs in such a way that in each pair the first number multiplied by $$$x$$$ is equal to the second number. More formally, a sequence $$$a$$$ of size $$$n$$$ is great for a positive integer $$$x$$$, if $$$n$$$ is even and there exists a permutation $$$p$$$ of size $$$n$$$, such that for each $$$i$$$ ($$$1 \\le i \\le \\frac{n}{2}$$$) $$$a_{p_{2i-1}} \\cdot x = a_{p_{2i}}$$$. Sam has a sequence $$$a$$$ and a positive integer $$$x$$$. Help him to make the sequence great: find the minimum possible number of positive integers that should be added to the sequence $$$a$$$ to make it great for the number $$$x$$$.", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 20\\,000$$$) \u2014 the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$x$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$2 \\le x \\le 10^6$$$). The next 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$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case print a single integer\u00a0\u2014 the minimum number of integers that can be added to the end of $$$a$$$ to make it a great sequence for the number $$$x$$$.", "sample_inputs": ["4\n4 4\n1 16 4 4\n6 2\n1 2 2 2 4 7\n5 3\n5 2 3 5 15\n9 10\n10 10 10 20 1 100 200 2000 3"], "sample_outputs": ["0\n2\n3\n3"], "notes": "NoteIn the first test case, Sam got lucky and the sequence is already great for the number $$$4$$$ because you can divide it into such pairs: $$$(1, 4)$$$, $$$(4, 16)$$$. Thus we can add $$$0$$$ numbers.In the second test case, you can add numbers $$$1$$$ and $$$14$$$ to the sequence, then you can divide all $$$8$$$ integers into such pairs: $$$(1, 2)$$$, $$$(1, 2)$$$, $$$(2, 4)$$$, $$$(7, 14)$$$. It is impossible to add less than $$$2$$$ integers to fix the sequence."}, "positive_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t( $n, $x, %h ) = split;\r\n\t\r\n\tmap $h{ $_ } ++, @_ = split ' ', <>;\r\n\t\r\n\tfor $key ( sort { $a <=> $b } keys %h ){\r\n\t\tmap $h{ $key * $_ } --, 1, $x while $h{ $key * $x } && $h{ $key }\r\n\t\t}\r\n\t\r\n\tprint 0 + eval join '+', values %h;\r\n\t}"}, {"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tmy( $n, $x ) = split;\r\n\t\r\n\t@_ = split ' ', <>;\r\n\t\r\n\tmy %h;\r\n\t\r\n\tmap $h{ $_ } ++, @_;\r\n\t\r\n\tfor my $key ( sort { $b <=> $a } keys %h ){\r\n\t\tif( $key % $x == 0 and exists $h{ $key / $x } ){\r\n\t\t\twhile( $h{ $key } > 0 and $h{ $key / $x } > 0 ){\r\n\t\t\t\t$h{ $key } --;\r\n\t\t\t\t$h{ $key / $x } --;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\r\n\tprint 0 + eval join '+', values %h;\r\n\t}"}], "negative_code": [], "src_uid": "c19500d867fd0108fdeed2cbd00bc970"} {"nl": {"description": " William has an array of $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$. In one move he can swap two neighboring items. Two items $$$a_i$$$ and $$$a_j$$$ are considered neighboring if the condition $$$|i - j| = 1$$$ is satisfied.William wants you to calculate the minimal number of swaps he would need to perform to make it so that the array does not contain two neighboring items with the same parity.", "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 an integer $$$n$$$ $$$(1 \\le n \\le 10^5)$$$ which is the total number of items in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ $$$(1 \\le a_i \\le 10^9)$$$ which are William's array. 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 minimal number of operations needed or $$$-1$$$ if it is impossible to get the array to a state when no neighboring numbers have the same parity.", "sample_inputs": ["5\n3\n6 6 1\n1\n9\n6\n1 1 1 2 2 2\n2\n8 6\n6\n6 2 3 4 5 1"], "sample_outputs": ["1\n0\n3\n-1\n2"], "notes": "NoteIn the first test case the following sequence of operations would satisfy the requirements: swap(2, 3). Array after performing the operation: $$$[6, 1, 6]$$$ In the second test case the array initially does not contain two neighboring items of the same parity.In the third test case the following sequence of operations would satisfy the requirements: swap(3, 4). Array after performing the operation: $$$[1, 1, 2, 1, 2, 2]$$$ swap(2, 3). Array after performing the operation: $$$[1, 2, 1, 1, 2, 2]$$$ swap(4, 5). Array after performing the operation: $$$[1, 2, 1, 2, 1, 2]$$$ In the fourth test case it is impossible to satisfy the requirements.In the fifth test case the following sequence of operations would satisfy the requirements: swap(2, 3). Array after performing the operation: $$$[6, 3, 2, 4, 5, 1]$$$ swap(4, 5). Array after performing the operation: $$$[6, 3, 2, 5, 4, 1]$$$ "}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($tcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $tcase -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @c = (0,0);\r\n my @A = map { ( $_ - 0 ) % 2 } split(/\\s+/,);\r\n foreach my $i (0..($n-1)){\r\n $c[ $A[$i] ] ++;\r\n }\r\n unless( $c[0] == ($n/2) or $c[1] == ($n/2) ){\r\n print \"-1\\n\"; next;\r\n }\r\n my @c2 = ( 0,1 );\r\n if( $c[1] > $c[0] ){\r\n @c2 = (1,0);\r\n }\r\n \r\n my $mv = 0;\r\n my $i0 = 0;\r\n my $i1 = 1;\r\n foreach my $i (0..($n-1)){\r\n if( $A[$i] == $c2[0] ){\r\n $mv += abs( $i0 - $i );\r\n $i0 += 2;\r\n } else {\r\n $mv += abs( $i1 - $i );\r\n $i1 += 2;\r\n }\r\n }\r\n \r\n if( $mv % 2 == 1 ){\r\n $mv = -1;\r\n } else {\r\n $mv /= 2;\r\n }\r\n my $mv2 = -1;\r\n if( $c[1] == $c[0] ){\r\n $mv2 = 0;\r\n my @c3 = reverse @c2;\r\n my $i0 = 0;\r\n my $i1 = 1;\r\n foreach my $i (0..($n-1)){\r\n if( $A[$i] == $c3[0] ){\r\n $mv2 += abs( $i0 - $i );\r\n $i0 += 2;\r\n } else {\r\n $mv2 += abs( $i1 - $i );\r\n $i1 += 2;\r\n }\r\n }\r\n if( $mv2 % 2 == 1 ){\r\n $mv2 = -1;\r\n } else {\r\n $mv2 /= 2;\r\n }\r\n }\r\n \r\n if( $mv < 0 and $mv2 < 0 ){\r\n $mv = -1;\r\n } elsif( $mv < 0 ){\r\n $mv = $mv2;\r\n } elsif( $mv2 < 0 ){\r\n ;\r\n } else {\r\n $mv = &min($mv,$mv2);\r\n }\r\n \r\n print \"$mv\\n\";\r\n \r\n}\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "negative_code": [], "src_uid": "be51a3e4038bd9a3f6d4993b75a20d1c"} {"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 \u2014 their owners are ready to pay Bob if he buys their useless apparatus. Bob can \u00abbuy\u00bb 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\u2009\u2264\u2009m\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 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 (\u2009-\u20091000\u2009\u2264\u2009ai\u2009\u2264\u20091000) \u2014 prices of the TV sets. ", "output_spec": "Output the only number \u2014 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": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nsub min{\n return $_[0] < $_[1]? $_[0]:$_[1];\n}\nmy $n;my $m;\n($n, $m) = split(' ', <>);\n\nmy @arr = split(' ', <>);\n@arr = sort {$a <=> $b}@arr;\n\nmy $res = 0;\nfor(my $i = 0; $i < min($n,$m); $i++){\n if($arr[$i] < 0){$res += -$arr[$i];}\n else {last;}\n}\nprint $res;\n\n"}, {"source_code": "<>=~/ /;\n@_=sort {$a<=>$b} (split/ /,<>);\n$s=0;\nfor (1..$'){\n$a=shift @_;\n$s+=$a<0?(-$a):(0);\n}\nprint $s"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nsub min{\n return $_[0] < $_[1]? $_[0]:$_[1];\n}\nmy $n;my $m;\n($n, $m) = split(' ', <>);\n\nmy @arr = split(' ', <>);\n@arr = sort @arr;\n\nmy $res = 0;\nfor(my $i = 0; $i < min($n,$m); $i++){\n if($arr[$i] < 0){$res += -$arr[$i];}\n else {last;}\n}\nprint $res;\n"}], "src_uid": "9a56288d8bd4e4e7ef3329e102f745a5"} {"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$$$) \u2014 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 \u2014 the result of your changes (i.\u2009e. 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 \u2014 $$$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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tif( $_ % 7 == 0 ){\n\t\tprint;\n\t\tnext;\n\t\t}\n\t\n\ts/.$/0/;\n\t\n\twhile( $_ % 7 != 0 ){\n\t\ts/.$/ $& + 1 /e;\n\t\t}\n\t\n\tprint;\n\t}"}], "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$$$)\u00a0\u2014 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\u00a0\u2014 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": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n,$l,$r) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n if( $n == 1 ){\r\n print \"0\\n\"; next;\r\n }\r\n my %c = ();\r\n foreach my $i (0..($n-1)){\r\n $c{$A[$i]} ++;\r\n }\r\n my @ks = sort { $a <=> $b } map { $_ - 0 } keys %c;\r\n my $mn = $#ks;\r\n my $mx = $#ks;\r\n my @ksr = (0); $#ksr = 1+$#ks;\r\n for(my $i=0;$i<=$#ks;$i++){\r\n $ksr[$i+1] = $ksr[$i] + $c{$ks[$i]};\r\n }\r\n my $res = 0;\r\n for(my $i=0;$i<=$#ks;$i++){\r\n my $v1 = $ks[$i];\r\n while($mn > 0 and $v1+$ks[$mn-1]>=$l){ $mn --; } # ok \u306e\u4e00\u756a\u2190\r\n while($mx >= $mn and $v1+$ks[$mx]>$r){ $mx --; } # ok\u306e\u4e00\u756a\u53f3 \r\n my $v2 = 0;\r\n if( $mx >= $mn and $mx >= 0 and $v1+$ks[$mn]>=$l and $v1+$ks[$mx]<=$r ){\r\n $v2 = $ksr[1+$mx] - $ksr[$mn];\r\n if( $ks[$mn] <= $v1 and $v1 <= $ks[$mx] ){\r\n $res += $c{$v1} * ($v2 - 1);\r\n } else {\r\n $res += $c{$v1} * $v2;\r\n }\r\n }\r\n }\r\n $res /= 2;\r\n print \"$res\\n\";\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "src_uid": "2f12b2bb23497119bb70ca0839991d68"} {"nl": {"description": "Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of three letters.Each club's full name consist of two words: the team's name and the hometown's name, for example, \"DINAMO BYTECITY\". Innokenty doesn't want to assign strange short names, so he wants to choose such short names for each club that: the short name is the same as three first letters of the team's name, for example, for the mentioned club it is \"DIN\", or, the first two letters of the short name should be the same as the first two letters of the team's name, while the third letter is the same as the first letter in the hometown's name. For the mentioned club it is \"DIB\". Apart from this, there is a rule that if for some club x the second option of short name is chosen, then there should be no club, for which the first option is chosen which is the same as the first option for the club x. For example, if the above mentioned club has short name \"DIB\", then no club for which the first option is chosen can have short name equal to \"DIN\". However, it is possible that some club have short name \"DIN\", where \"DI\" are the first two letters of the team's name, and \"N\" is the first letter of hometown's name. Of course, no two teams can have the same short name.Help Innokenty to choose a short name for each of the teams. If this is impossible, report that. If there are multiple answer, any of them will suit Innokenty. If for some team the two options of short name are equal, then Innokenty will formally think that only one of these options is chosen. ", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000)\u00a0\u2014 the number of clubs in the league. Each of the next n lines contains two words\u00a0\u2014 the team's name and the hometown's name for some club. Both team's name and hometown's name consist of uppercase English letters and have length at least 3 and at most 20.", "output_spec": "It it is not possible to choose short names and satisfy all constraints, print a single line \"NO\". Otherwise, in the first line print \"YES\". Then print n lines, in each line print the chosen short name for the corresponding club. Print the clubs in the same order as they appeared in input. If there are multiple answers, print any of them.", "sample_inputs": ["2\nDINAMO BYTECITY\nFOOTBALL MOSCOW", "2\nDINAMO BYTECITY\nDINAMO BITECITY", "3\nPLAYFOOTBALL MOSCOW\nPLAYVOLLEYBALL SPB\nGOGO TECHNOCUP", "3\nABC DEF\nABC EFG\nABD OOO"], "sample_outputs": ["YES\nDIN\nFOO", "NO", "YES\nPLM\nPLS\nGOG", "YES\nABD\nABE\nABO"], "notes": "NoteIn the first sample Innokenty can choose first option for both clubs.In the second example it is not possible to choose short names, because it is not possible that one club has first option, and the other has second option if the first options are equal for both clubs.In the third example Innokenty can choose the second options for the first two clubs, and the first option for the third club.In the fourth example note that it is possible that the chosen short name for some club x is the same as the first option of another club y if the first options of x and y are different."}, "positive_code": [{"source_code": "$n = <>; @n = 0..$n-1; \nfor (<>) {\n\t/^(..)(.).* (.)/;\n\t@v = (\"$1$2\", \"$1$3\");\n\tpush @a, [@v];\n\tpush @{ $h0{$v[0]} }, $#a;\t\n\tpush @{ $h1{$v[$_]} }, $#a for 0, 1;\n}\n\nsub ans {\n\tmy ($i, $j) = @_;\n\tmy $a = $a[$i];\n\tif (!$a->[2]) {\n\t\t$v = $a->[2] = $a->[$j];\n\t\t$dup ||= $u{$v}; \n\t\t$u{$v} = 1;\n\t\tpush(@i, $i), $i{$i} = 1 unless exists $i{$i};\n\t}\n}\n\nfor $i (@n) {\n\tif ( @{$h0{$a[$i]->[0]}} > 1 ) {\n\t\tans $i, 1;\n\t}\n}\n\nfor $i (@i) {\n\tfor $j ( @{$h1{$a[$i]->[2]}} ) {\n\t\tif (!$a[$j]->[2]) {\n\t\t\t$k = $a[$j]->[0] ne $a[$i]->[2]? 0: 1;\n\t\t\tans $j, $k;\n\t\t}\n\t}\n}\t\n\nfor $i (@n) {\n\tans $i, 0;\n}\n\nif (!$dup) {\n\tprint \"YES\\n\";\n\tprint \"$$_[2]\\n\" for @a;\n} else {\n\tprint \"NO\\n\";\n}\n\t"}], "negative_code": [{"source_code": "<>; for (<>) {\n\t/^(..)(.).* (.)/;\n\t@v = (\"$1$2\", \"$1$3\");\n\tpush @a, [@v];\n\tpush @{ $h0{$v[0]} }, $a[-1];\t\n\tpush @{ $h1{$v[$_]} }, $a[-1] for 0, 1;\n}\n\nsub ans {\n\tmy ($a, $i) = @_;\n\tif (!$a->[2]) {\n\t\t$v = $a->[2] = $a->[$i];\n\t\t$dup ||= $u{$v}; \n\t\t$u{$v} = 1;\n\t}\n}\n\nfor $a (@a) {\n\tif ( @{$h0{$a->[0]}} > 1 ) {\n\t\tans $a, 1;\n\t}\n}\n\nfor $a (@a) {\n\tfor $b ( @{$h1{$a->[2]}} ) {\n\t\tans $b, $b->[0] eq $a->[2];\n\t}\n}\t\n\nfor $a (@a) {\n\tans $a, $u{$a->[0]};\n}\n\nif (!$dup) {\n\tprint \"YES\\n\";\n\tprint \"$$_[2]\\n\" for @a;\n} else {\n\tprint \"NO\\n\";\n}\n\t"}, {"source_code": "<>; for (<>) {\n\t/^(..)(.).* (.)/;\n\t@v = (\"$1$2\", \"$1$3\");\n\tpush @a, [@v];\n\tpush @{ $h0{$v[0]} }, $a[-1];\t\n\tpush @{ $h1{$v[$_]} }, $a[-1] for 0, 1;\n}\n\nfor $a (@a) {\n\tif ( @{$h0{$a->[0]}} > 1 ) {\n\t\t$a->[2] = $a->[1];\n\t}\n}\n\nfor $a (@a) {\n\tfor $b ( @{$h1{$a->[2]}} ) {\n\t\t$b->[2] ||= $b->[ $b->[0] eq $a->[2] ];\n\t}\n}\t\n\nfor $a (@a) {\n\t$a->[2] ||= $a->[0];\n\t$u{ $a->[2] } = 1;\t\n}\n\nif (@a == keys %u) {\n\tprint \"YES\\n\";\n\tprint \"$$_[2]\\n\" for @a;\n} else {\n\tprint \"NO\\n\";\n}\n\t"}, {"source_code": "<>; for (<>) {\n\t/^(..)(.)\\w* (.)/;\n\tpush @a, [$v = \"$1$2\", \"$1$3\"];\n\t$h{ $v }++;\n}\n# use Data::Dumper;\n# print Dumper(\\@a), \"\\n\";\n\nsub r { $r{ $$a[2] = $$a[shift] }++ }\n\nfor $a (@a) {\n\tif ( $h{$$a[0]} > 1 ) {\n\t\tr 1;\n\t}\n}\n\nfor $a (@a) {\n\tif ( !$$a[2] ) {\n\t\t!$r{$$a[0]}? r 0: r 1;\n\t}\n}\n\nif ($a[0]->[0] eq 'KQC') { # 16\n\tprint \"YES\\n\";\n\tfor (@a) {\n\t\tif ($_->[0] eq 'KQC' || $_->[0] eq 'KQP' || $_->[1] eq 'KQC' || $_->[1] eq 'KQP') {\n\t\t\tprint $_->[0], \" \", $_->[1], \"\\n\";\n\t\t}\n\t}\n} else {\n\tif (@a == keys %r) {\n\t\tprint \"YES\\n\";\n\t\tprint \"$$_[2]\\n\" for @a;\n\t} else {\n\t\tprint \"NO\\n\";\n\t}\n}\t"}, {"source_code": "$n = <>; for (<>) {\n\t/^(..)(.)\\w* (.)/;\n\tpush @a, [$v = \"$1$2\", \"$1$3\", undef, $_];\n\t$h{ $v }++;\n}\n# use Data::Dumper;\n# print Dumper(\\@a), \"\\n\";\n\nsub r { $r{ $$a[2] = $$a[shift] }++ }\n\nfor $a (@a) {\n\tif ( $h{$$a[0]} > 1 ) {\n\t\tr 1;\n\t}\n}\n\nfor $a (@a) {\n\tif ( !$$a[2] ) {\n\t\t!$r{$$a[0]}? r 0: r 1;\n\t}\n}\n\nif ($n == 1000 && $a[0]->[0] eq 'KQC') { # 16\n\tprint \"YES\\n\";\n\t$i0 = 100;\n\tfor ($i = $i0; $i < $i0 + 50; $i++) {\n\t\tprint $a[$i]->[3];\n\t}\n} else {\n\tif (@a == keys %r) {\n\t\tprint \"YES\\n\";\n\t\tprint \"$$_[2]\\n\" for @a;\n\t} else {\n\t\tprint \"NO\\n\";\n\t}\n}\t"}, {"source_code": "<>; for (<>) {\n\t/^(..)(.)\\w* (.)/;\n\tpush @a, [$v = \"$1$2\", \"$1$3\"];\n\t$h{ $v }++;\n}\n# use Data::Dumper;\n# print Dumper(\\@a), \"\\n\";\n\nsub r { $r{ $$a[2] = $$a[shift] }++ }\n\nfor $a (@a) {\n\tif ( $h{$$a[0]} > 1 ) {\n\t\tr 1;\n\t}\n}\n\nfor $a (@a) {\n\tif ( !$$a[2] ) {\n\t\t!$r{$$a[0]}? r 0: r 1;\n\t}\n}\n\nif ($a[0]->[0] eq 'KQC') { # 16\n\tprint \"YES\\n\";\n\tfor (@a) {\n\t\tif ($_->[0] eq 'KQI' || $_->[0] eq 'KQX' || $_->[1] eq 'KQI' || $_->[1] eq 'KQX') {\n\t\t\tprint $_->[0], \" \", $_->[1], \"\\n\";\n\t\t}\n\t}\n} else {\n\tif (@a == keys %r) {\n\t\tprint \"YES\\n\";\n\t\tprint \"$$_[2]\\n\" for @a;\n\t} else {\n\t\tprint \"NO\\n\";\n\t}\n}\t"}, {"source_code": "<>; for (<>) {\n\t/^(..)(.).* (.)/;\n\t$a = [\"$1$2\", \"$1$3\"];\n\tpush @a, $a;\n\t$h{ $$a[0] }++;\n}\n\nsub r { $r{ $$a[2] = $$a[shift] }++ }\n\nfor $a (@a) {\n\tif ( $h{$$a[0]} > 1 ) {\n\t\tr 1;\n\t}\n}\n\nfor $a (@a) {\n\tif ( !$$a[2] ) {\n\t\t!$r{$$a[0]}? r 0: r 1;\n\t}\n}\n\nif (@a == keys %r) {\n\tprint \"YES\\n\";\n\tprint \"$$_[2]\\n\" for @a;\n} else {\n\tprint \"NO\\n\";\n}"}, {"source_code": "<>; for (<>) {\n\t/^(..)(.)\\w* (.)/;\n\tpush @a, [$v = \"$1$2\", \"$1$3\", undef, $_];\n\t$h{ $v }++;\n}\n# use Data::Dumper;\n# print Dumper(\\@a), \"\\n\";\n\nsub r { $r{ $$a[2] = $$a[shift] }++ }\n\nfor $a (@a) {\n\tif ( $h{$$a[0]} > 1 ) {\n\t\tr 1;\n\t}\n}\n\nfor $a (@a) {\n\tif ( !$$a[2] ) {\n\t\t!$r{$$a[0]}? r 0: r 1;\n\t}\n}\n\nif ($a[0]->[0] eq 'KQC') { # 16\n\tprint \"YES\\n\";\n\t$i0 = 50;\n\tfor ($i = $i0; $i < $i0 + 50; $i++) {\n\t\tprint $a[$i]->[3];\n\t}\n} else {\n\tif (@a == keys %r) {\n\t\tprint \"YES\\n\";\n\t\tprint \"$$_[2]\\n\" for @a;\n\t} else {\n\t\tprint \"NO\\n\";\n\t}\n}\t"}, {"source_code": "$n = <>; for (<>) {\n\t/^(..)(.)\\w* (.)/;\n\tpush @a, [$v = \"$1$2\", \"$1$3\", undef, $_];\n\t$h{ $v }++;\n}\n# use Data::Dumper;\n# print Dumper(\\@a), \"\\n\";\n\nsub r { $r{ $$a[2] = $$a[shift] }++ }\n\nfor $a (@a) {\n\tif ( $h{$$a[0]} > 1 ) {\n\t\tr 1;\n\t}\n}\n\nfor $a (@a) {\n\tif ( !$$a[2] ) {\n\t\t!$r{$$a[0]}? r 0: r 1;\n\t}\n}\n\nif ($n == 1000 && $a[0]->[0] eq 'KQC') { # 16\n\tprint \"YES\\n\";\n\t$i0 = 250;\n\tfor ($i = $i0; $i < $i0 + 50; $i++) {\n\t\tprint $a[$i]->[3];\n\t}\n} else {\n\tif (@a == keys %r) {\n\t\tprint \"YES\\n\";\n\t\tprint \"$$_[2]\\n\" for @a;\n\t} else {\n\t\tprint \"NO\\n\";\n\t}\n}\t"}, {"source_code": "$n = <>; for (<>) {\n\t/^(..)(.)\\w* (.)/;\n\tpush @a, [$v = \"$1$2\", \"$1$3\", undef, $_];\n\t$h{ $v }++;\n}\n# use Data::Dumper;\n# print Dumper(\\@a), \"\\n\";\n\nsub r { $r{ $$a[2] = $$a[shift] }++ }\n\nfor $a (@a) {\n\tif ( $h{$$a[0]} > 1 ) {\n\t\tr 1;\n\t}\n}\n\nfor $a (@a) {\n\tif ( !$$a[2] ) {\n\t\t!$r{$$a[0]}? r 0: r 1;\n\t}\n}\n\nif ($n == 1000 && $a[0]->[0] eq 'KQC') { # 16\n\tprint \"YES\\n\";\n\t$i0 = 150;\n\tfor ($i = $i0; $i < $i0 + 50; $i++) {\n\t\tprint $a[$i]->[3];\n\t}\n} else {\n\tif (@a == keys %r) {\n\t\tprint \"YES\\n\";\n\t\tprint \"$$_[2]\\n\" for @a;\n\t} else {\n\t\tprint \"NO\\n\";\n\t}\n}\t"}, {"source_code": "$n = <>; for (<>) {\n\t/^(..)(.)\\w* (.)/;\n\tpush @a, [$v = \"$1$2\", \"$1$3\", undef, $_];\n\t$h{ $v }++;\n}\n# use Data::Dumper;\n# print Dumper(\\@a), \"\\n\";\n\nsub r { $r{ $$a[2] = $$a[shift] }++ }\n\nfor $a (@a) {\n\tif ( $h{$$a[0]} > 1 ) {\n\t\tr 1;\n\t}\n}\n\nfor $a (@a) {\n\tif ( !$$a[2] ) {\n\t\t!$r{$$a[0]}? r 0: r 1;\n\t}\n}\n\nif ($n == 1000 && $a[0]->[0] eq 'KQC') { # 16\n\tprint \"YES\\n\";\n\t$i0 = 200;\n\tfor ($i = $i0; $i < $i0 + 50; $i++) {\n\t\tprint $a[$i]->[3];\n\t}\n} else {\n\tif (@a == keys %r) {\n\t\tprint \"YES\\n\";\n\t\tprint \"$$_[2]\\n\" for @a;\n\t} else {\n\t\tprint \"NO\\n\";\n\t}\n}\t"}, {"source_code": "<>; for (<>) {\n\t/^(..)(.)\\w* (.)/;\n\tpush @a, [$v = \"$1$2\", \"$1$3\"];\n\t$h{ $v }++;\n}\n# use Data::Dumper;\n# print Dumper(\\@a), \"\\n\";\n\nsub r { $r{ $$a[2] = $$a[shift] }++ }\n\nfor $a (@a) {\n\tif ( $h{$$a[0]} > 1 ) {\n\t\tr 1;\n\t}\n}\n\nfor $a (@a) {\n\tif ( !$$a[2] ) {\n\t\t!$r{$$a[0]}? r 0: r 1;\n\t}\n}\n\nif ($a[0]->[0] eq 'KQC') { # 16\n\tprint \"YES\\n\";\n\tprint 0 + @a, \"\\n\";\n\tprint 0 + keys(%r), \"\\n\";\n\tprint 0 + keys(%h), \"\\n\";\n\tprint \"$$_[2]\\n\" for @a;\t\n} else {\n\tif (@a == keys %r) {\n\t\tprint \"YES\\n\";\n\t\tprint \"$$_[2]\\n\" for @a;\n\t} else {\n\t\tprint \"NO\\n\";\n\t}\n}\t"}, {"source_code": "$n = <>; for (<>) {\n\t/^(..)(.)\\w* (.)/;\n\tpush @a, [$v = \"$1$2\", \"$1$3\", undef, $_];\n\t$h{ $v }++;\n}\n# use Data::Dumper;\n# print Dumper(\\@a), \"\\n\";\n\nsub r { $r{ $$a[2] = $$a[shift] }++ }\n\nfor $a (@a) {\n\tif ( $h{$$a[0]} > 1 ) {\n\t\tr 1;\n\t}\n}\n\nfor $a (@a) {\n\tif ( !$$a[2] ) {\n\t\t!$r{$$a[0]}? r 0: r 1;\n\t}\n}\n\nif ($n == 1000 && $a[0]->[0] eq 'KQC') { # 16\n\tprint \"YES\\n\";\n\t$i0 = 300;\n\tfor ($i = $i0; $i < $i0 + 50; $i++) {\n\t\tprint $a[$i]->[3];\n\t}\n} else {\n\tif (@a == keys %r) {\n\t\tprint \"YES\\n\";\n\t\tprint \"$$_[2]\\n\" for @a;\n\t} else {\n\t\tprint \"NO\\n\";\n\t}\n}\t"}], "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$$$)\u00a0\u2014 the number of test cases. Next $$$t$$$ lines contain test cases\u00a0\u2014 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)$$$\u00a0\u2014 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": "use bigint;\nchomp(my $t = );\nforeach (1..$t)\n{\n chomp (my $n = );\n if ($n <= 30)\n {\n print \"NO\\n\";\n }\n elsif ($n == 36 || $n == 40 || $n == 44)\n {\n print \"YES\\n6 10 15 \", $n - 31, \"\\n\";\n }\n else\n {\n print \"YES\\n6 10 14 \", $n - 30, \"\\n\";\n }\n}"}, {"source_code": "use strict;\n\nmy $try = <>;\nwhile($try-- > 0)\n{\n my $val = <>;\n if($val < 31){\n print \"NO\\n\";\n } elsif($val == 36) {\n print \"YES\\n\";\n printf(\"15 10 6 %d\\n\", $val-31);\n } elsif($val == 40 || $val == 44) {\n print \"YES\\n\";\n printf(\"15 10 14 %d\\n\", $val-39);\n } else {\n print \"YES\\n\";\n printf(\"6 10 14 %d\\n\", $val-30);\n }\n}"}], "negative_code": [{"source_code": "use bigint;\nchomp(my $t = );\nforeach (1..$t)\n{\n chomp (my $n = );\n if ($n <= 30)\n {\n print \"NO\\n\";\n }\n elsif ($n == 36 || $n == 40 || $n == 44)\n {\n print \"6 10 15 \", $n - 31, \"\\n\";\n }\n else\n {\n print \"6 10 14 \", $n - 30, \"\\n\";\n }\n}"}, {"source_code": "use strict;\n\nmy $try = <>;\nwhile($try-- > 0)\n{\n my $val = <>;\n\n if($val < 31 || $val == 36){\n print \"NO\\n\";\n } elsif($val == 40 || $val == 44) {\n print \"YES\\n\";\n printf(\"15 10 14 %d\\n\", $val-39);\n } else {\n print \"YES\\n\";\n printf(\"6 10 14 %d\\n\", $val-30);\n }\n}"}], "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\u2019s laziness level is equal to their task\u2019s 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\u2019s 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\u2009007.", "input_spec": "The first line of input contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of tasks. The next n lines contain exactly one integer number ai (1\u2009\u2264\u2009ai\u2009\u2264\u2009100\u2009000)\u00a0\u2014 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\u2009007.", "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\u2009+\u20093\u2009=\u20096 time units."}, "positive_code": [{"source_code": "my $n=;\nchomp($n);\nmy @NUM;\nwhile($n--){\nmy $k=;\nchomp($k);\npush(@NUM,$k);\n}\nmy @L1=sort {$a <=> $b } @NUM;\nmy @L2=sort {$b <=> $a } @NUM;\nmy $ans=0;\nfor(my $i=0;$i;\nchomp($n);\nmy @NUM;\nwhile($n--){\nmy $k=;\nchomp($k);\npush(@NUM,$k);\n}\nmy @L1=sort {$a <=> $b } @NUM;\nmy @L2=sort {$b <=> $n } @NUM;\nmy $ans=0;\nfor(my $i=0;$i;\nchomp($n);\nmy @NUM;\nwhile($n--){\nmy $k=;\nchomp($k);\npush(@NUM,$k);\n}\nmy @L1=sort {$a <=> $b } @NUM;\nmy @L2=sort {$b <=> $n } @NUM;\nmy $ans=0;\nfor(my $i=0;$i;\nchomp($n);\nmy @NUM;\nwhile($n--){\nmy $k=;\nchomp($k);\npush(@NUM,$k);\n}\nmy @L1=sort {$a <=> $b } @NUM;\nmy @L2=sort {$b <=> $n } @NUM;\nmy $ans=0;\nfor(my $i=0;$i;\n$i ++, ($x, $y) = split, !$h{$x} && !$g{$y} and $h{$x}++, $g{$y}++, print \"$i \" for <>\n"}, {"source_code": "<>;\n$i ++, ($x, $y) = split, !$h{$x} && !$g{$y} and $h{$x}++, $g{$y}++, print \"$i \" for <>\n"}, {"source_code": "\nmy $n = int<>;\nmy ($x, $y);\nmy @arr = (0 x 200);\nfor(1..$n*$n) {\n ($x, $y) = split / /, <>;\n if (!$arr[$x] && !$arr[60 + $y]) {\n print $_ . \" \";\n $arr[$x] = 1;\n $arr[60 + $y] = 1;\n }\n}\n"}, {"source_code": "<>;\n$i ++, ($x, $y) = split, !$h{$x} && !$g{$y} and $h{$x}++, $g{$y}++, print \"$i \" for <>"}, {"source_code": "<>;\n$i ++, ($x, $y) = split, !$h{$x} && !$g{$y} and $h{$x}++, $g{$y}++, print \"$i \" for <>\n"}, {"source_code": "<>;\n$i ++, ($x, $y) = split, !$h{$x} && !$g{$y} and $h{$x}++, $g{$y}++, print \"$i \" for <>\n"}, {"source_code": "<>;\n$i ++, ($x, $y) = split, !$h{$x} && !$g{$y} and $h{$x}++, $g{$y}++, print \"$i \" for <>\n"}, {"source_code": "<>;\n$i ++, ($x, $y) = split, !$h{$x} && !$g{$y} and $h{$x}++, $g{$y}++, print \"$i \" for <>\n"}, {"source_code": "$n=<>;\n$n2=$n*$n;\n$m=0;\nfor ($i=0; $i<$n2; $i++)\n{\n\t($a,$b)=split \" \",<>;\n\tif ($v[$a]==0 and $h[$b]==0)\n\t{\n\t\t$v[$a]=1;\n\t\t$h[$b]=1;\n\t\t$rez[$m]=$i+1;\n\t\t$m++;\n\t}\n}\nprint \"@rez\";"}, {"source_code": "<>;\n$i ++, ($x, $y) = split, !$h{$x} && !$g{$y} and $h{$x}++, $g{$y}++, print \"$i \" for <>\n"}, {"source_code": "<>;\n$i ++, ($x, $y) = split, !$h{$x} && !$g{$y} and $h{$x}++, $g{$y}++, print \"$i \" for <>"}, {"source_code": "<>;\n$i ++, ($x, $y) = split, !$h{$x} && !$g{$y} and $h{$x}++, $g{$y}++, print \"$i \" for <>\n"}], "negative_code": [{"source_code": "<>;\n$i ++, ($x, $y) = split, !$h{$x} && !$h{$y} and $h{$x}++, $h{$y}++, print \"$i \" for <>"}], "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,\u2009si\u2009+\u2009di,\u2009si\u2009+\u20092di,\u2009....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 \u2014 number of doctors (1\u2009\u2264\u2009n\u2009\u2264\u20091000). Next n lines contain two numbers si and di (1\u2009\u2264\u2009si,\u2009di\u2009\u2264\u20091000).", "output_spec": "Output a single integer \u2014 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": "use integer;\n<>; while (<>) {\n\t($s, $d) = split;\n\t# a < s + dx, (a - s)/d < x \n\tif ($a >= $s) {\n\t\t$x = ($a - $s) / $d + 1;\n\t\t$a = $s + $d * $x;\n\t} else {\n\t\t$a = $s;\n\t}\n}\nprint $a;\n"}, {"source_code": "<>;\n\n$Z = 0;\n\nfor( <> ){\n\t( $s, $d ) = split;\n\t$Z ++;\n\t\n\t$Z = $s + $d * ( ( $Z - $s ) / $d ) =~ s/-.*/0/r =~ s/(\\d+)\\..*/ $1 + 1 /er;\n\t}\n\nprint $Z"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tmy $start = 0;\n\t\n\tfor( @_ ){\n\t\tmy( $s, $d ) = split;\n\t\t$start ++;\n\t\t\n\t\tmy $n = ( $start - $s ) / $d;\n\t\t$debug and print ' ', $n;\n\t\t$start = $s + $d * ( $n =~ s/-.*/0/r =~ s/(\\d+)\\..*/ $1 + 1 /er );\n\t\t$debug and print ' ', $start;\n\t\t}\n\t\n\tprint $start;\n\t}"}, {"source_code": "<>;\n\nfor( <> ){\n\t( $s, $d ) = split;\n\t\n\t$s += 1e3 * $d while $Z >= $s + 1e3 * $d;\n\t$s += $d while $Z >= $s;\n\t$Z = $s;\n\t}\n\nprint $Z"}], "negative_code": [], "src_uid": "d324617c86423d86cdcb839894e00e1a"} {"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\u2009-\u2009R|.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\u2009\u2264\u2009n\u2009\u2264\u2009105)\u00a0\u2014 the number of columns. The next n lines contain the pairs of integers li and ri (1\u2009\u2264\u2009li,\u2009ri\u2009\u2264\u2009500)\u00a0\u2014 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\u00a0\u2014 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\u2009+\u20098\u2009+\u200910\u2009=\u200923, and from the right leg\u00a0\u2014 6\u2009+\u20099\u2009+\u20093\u2009=\u200918. In this case the beauty of the parade will equal |23\u2009-\u200918|\u2009=\u20095.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\u2009+\u20098\u2009+\u20093\u2009=\u200916, and who march from the right leg\u00a0\u2014 6\u2009+\u20099\u2009+\u200910\u2009=\u200925. In this case the beauty equals |16\u2009-\u200925|\u2009=\u20099.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\n<>;\n\nmap { ($l, $r) = split; push @L, $l; push @R, $r; } <>;\n\n($L, $R) = map { unpack '%32i' . ~~ @$_, pack 'i' . ~~ @$_, @$_ } \\@L, \\@R;\n\n$beauty = abs($L - $R);\n\nfor $i (1 .. @L) {\n\t$beauty_2 = abs(\n\t\t($L + $R[$i-1] - $L[$i-1]) -\n\t\t($R + $L[$i-1] - $R[$i-1])\n\t);\n\t$beauty_2 > $beauty and do {\n\t\t$beauty = $beauty_2;\n\t\t$index = $i;\n\t};\n}\n\nprint $index // 0"}, {"source_code": "<>;\n\nmap { ($l, $r) = split; push @L, $l; push @R, $r; } <>;\n\n($L, $R) = map { unpack '%123d*', pack 'd*', @$_ } \\@L, \\@R;\n\n$B = abs($L - $R);\n\nfor (1 .. @L) {\n\t$b2 = abs($L - $R + 2 * ($R[$_-1] - $L[$_-1]) );\n\t$b2 > $B and $B = $b2 and $I = $_;\n}\n\nprint $I // 0"}, {"source_code": "<>;\n\n@_ = map [split, eval join '-', split], <>;\n\n$L += $_->[0] for @_;\n$R += $_->[1] for @_;\n$B = abs($L - $R);\n\nfor (1 .. @_) {\n\t$b2 = abs($R - $L + 2 * $_[$_-1][2] );\n\t$b2 > $B and $B = $b2 and $I = $_;\n}\n\nprint $I // 0"}], "negative_code": [], "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$$$) \u2014 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": "#!/usr/bin/perl\r\nmy ($q) = map { $_ - 0 } split(/\\s+/o,);\r\nwhile( $q -- > 0 ){\r\n ( my $s = ) =~ s/\\s+$//ios;\r\n ( my $t = ) =~ s/\\s+$//ios;\r\n my $sc = length($s);\r\n my $tc = length($t);\r\n my $c = ($sc * $tc)/&gcd($sc,$tc);\r\n \r\n my $s1 = $s x ($c/$sc);\r\n my $t1 = $t x ($c/$tc);\r\n \r\n $s1 = -1 if $s1 ne $t1;\r\n print \"$s1\\n\";\r\n}\r\nexit(0);\r\nsub gcd { # gcd(a,b)\r\n my ($a,$b) = @_;\r\n ($a,$b) = ($b,$a) if $a<$b;\r\n my $r = $a % $b;\r\n while($r!=0){\r\n $a = $b;\r\n $b = $r;\r\n $r = $a % $b;\r\n }\r\n return $b;\r\n}\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nmy ($q) = map { $_ - 0 } split(/\\s+/o,);\r\nwhile( $q -- > 0 ){\r\n ( my $s = ) =~ s/\\s+$//ios;\r\n ( my $t = ) =~ s/\\s+$//ios;\r\n my $r = -1;\r\n for(my $i=1;$i<=20;$i++){\r\n last if substr($s,$i-1,1) ne substr($t,$i-1,1);\r\n last unless (length($s) % $i == 0) and (length($t) % $i == 0);\r\n my $sc = (length($s)/$i);\r\n my $tc = (length($t)/$i);\r\n if( $s eq ( substr($s,0,$i) x $sc ) and $t eq ( substr($t,0,$i) x $tc ) ){\r\n my $c = $sc * $tc / gcd($sc,$tc);\r\n $r = ( substr($s,0,$i) x $c ); last;\r\n }\r\n }\r\n print \"$r\\n\";\r\n}\r\nexit(0);\r\nsub gcd { # gcd(a,b)\r\n my ($a,$b) = @_;\r\n ($a,$b) = ($b,$a) if $a<$b;\r\n my $r = $a % $b;\r\n while($r!=0){\r\n $a = $b;\r\n $b = $r;\r\n $r = $a % $b;\r\n }\r\n return $b;\r\n}\r\n"}, {"source_code": "#!/usr/bin/perl\r\nmy ($q) = map { $_ - 0 } split(/\\s+/o,);\r\nwhile( $q -- > 0 ){\r\n ( my $s = ) =~ s/\\s+$//ios;\r\n ( my $t = ) =~ s/\\s+$//ios;\r\n my $r = -1;\r\n for(my $i=1;$i<=20;$i++){\r\n last if substr($s,$i-1,1) ne substr($t,$i-1,1);\r\n last unless (length($s) % $i == 0) and (length($t) % $i == 0);\r\n if( $s eq ( substr($s,0,$i) x (length($s)/$i) ) and $t eq ( substr($t,0,$i) x (length($t)/$i) ) ){\r\n $r = substr($s,0,$i); last;\r\n }\r\n }\r\n print \"$r\\n\";\r\n}\r\n"}, {"source_code": "#!/usr/bin/perl\r\nmy ($q) = map { $_ - 0 } split(/\\s+/o,);\r\nwhile( $q -- > 0 ){\r\n ( my $s = ) =~ s/\\s+$//ios;\r\n ( my $t = ) =~ s/\\s+$//ios;\r\n my $r = -1;\r\n for(my $i=1;$i<=20;$i++){\r\n last if substr($s,$i-1,1) ne substr($t,$i-1,1);\r\n last unless length($s) % $i == 0 and length($t) % $i == 0;\r\n if( $s eq ( substr($s,0,$i) x (length($s)/$i) ) and $t eq ( substr($t,0,$i) x (length($t)/$i) ) ){\r\n $r = substr($s,0,$i);\r\n }\r\n }\r\n print \"$r\\n\";\r\n}\r\n"}], "src_uid": "710c7211d23cf8c01fae0b476a889276"} {"nl": {"description": "Gildong recently learned how to find the longest increasing subsequence (LIS) in $$$O(n\\log{n})$$$ time for a sequence of length $$$n$$$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $$$n$$$ distinct integers between $$$1$$$ and $$$n$$$, inclusive, to test his code with your output.The quiz is as follows.Gildong provides a string of length $$$n-1$$$, consisting of characters '<' and '>' only. The $$$i$$$-th (1-indexed) character is the comparison result between the $$$i$$$-th element and the $$$i+1$$$-st element of the sequence. If the $$$i$$$-th character of the string is '<', then the $$$i$$$-th element of the sequence is less than the $$$i+1$$$-st element. If the $$$i$$$-th character of the string is '>', then the $$$i$$$-th element of the sequence is greater than the $$$i+1$$$-st element.He wants you to find two possible sequences (not necessarily distinct) consisting of $$$n$$$ distinct integers between $$$1$$$ and $$$n$$$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible.", "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$$$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $$$n-1$$$. It is guaranteed that the sum of all $$$n$$$ in all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print two lines with $$$n$$$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $$$1$$$ and $$$n$$$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists.", "sample_inputs": ["3\n3 <<\n7 >><>><\n5 >>><"], "sample_outputs": ["1 2 3\n1 2 3\n5 4 3 7 2 1 6\n4 3 1 7 5 2 6\n4 3 2 1 5\n5 4 2 1 3"], "notes": "NoteIn the first case, $$$1$$$ $$$2$$$ $$$3$$$ is the only possible answer.In the second case, the shortest length of the LIS is $$$2$$$, and the longest length of the LIS is $$$3$$$. In the example of the maximum LIS sequence, $$$4$$$ '$$$3$$$' $$$1$$$ $$$7$$$ '$$$5$$$' $$$2$$$ '$$$6$$$' can be one of the possible LIS."}, "positive_code": [{"source_code": "# open STDIN, \");\nsub long {\n\tmy ($n, $s)=@_;\n\tmy @arr=();\n\tmy ($hi, $lo)=($n,1);\n\tfor (my $i=0; $i';\n\tmy @arr=();\n\tmy $hi=$n;\n\tmy ($idx,$i)=(index($s, '>'), 1);\n\tmy @arr=();\n\t$arr[0]=$hi-$idx;\n\tmy $newhi = $arr[0]-1;\n\twhile ($i<$n) {\n\t\twhile ($i<=$idx and $i<$n) {\n\t\t\t$arr[$i]=$hi-($idx-$i);\n\t\t\t++$i;\n\t\t}\n\t\twhile ($i<$n and substr($s,$i,1) eq '>') {\n\t\t\t$arr[$i]=$newhi--;\n\t\t\t++$i;\n\t\t}\n\t\t$hi=$newhi;\n\t\t$idx=index($s, '>', $i);\n\t\t$newhi=$hi-($idx-$i)-1;\n\t}\n\treturn @arr;\n}\nwhile ($t--) {\n\t($n, $s)=split(' ',<>); chomp $n, $s;\n\t# print \"$n $s\\n\";\n\t@longest = &long($n,$s);\n\t@shortest = &short($n,$s);\n\tprint \"@shortest\\n@longest\\n\";\n}"}], "negative_code": [], "src_uid": "fd0e9b90f36611c28fa8aca5b4e59ae9"} {"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\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of initial bouquets. The second line contains a sequence of integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009106) \u2014 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\u2009+\u20094\u2009+\u200910\u2009=\u200925."}, "positive_code": [{"source_code": "#!/usr/local/bin/perl\n \nmy @ttt = split(/\\s+/, <>);\nmy @arr = split(/\\s+/, <>);\n\n$odd = 0;\n$even = 0;\n\nforeach $a (@arr) {\n\n $c = int($a) % 2;\n\n \n if ($c == 1) {\n $odd = $odd + 1;\n } else {\n $even = $even + 1;\n }\n\n}\n\nif ($odd <= $even)\n{\n print($odd)\n}\nelse\n{\n print($even + int(($odd - $even) / 3 + 1e-12))\n}"}, {"source_code": "$_ = ; my ($n) = split;\n$_ = ; my @A = split;\n\n$odd = 0;\n$even = 0;\nfor($i=0; $i < $n ; $i=$i+1){\n \n\tif( $A[$i] %2 == 0){\n\t $even = $even+1;\n\t}else{\n\t $odd = $odd+1;\n\t}\n}\n\n$mn = $odd;\nif ($even < $mn) {\n $mn = $even;\n}\n\n$df = $odd - $mn;\n$kk = $df / 3;\n$qq = int($kk);\n$ans = $mn + $qq;\n\nprint \"$ans\\n\"\n\n"}, {"source_code": "my $n = <>;\n\n$inp = [split / /, <>];\n\n@a = (0, 0, 0);\n\n$ans = 0;\n\nforeach (@$inp) {\n if ($_ % 2 == 0) {\n $a[1]++;\n }\n else {\n $a[2]++; \n }\n}\n\nmy $d;\nif ($a[1] < $a[2]) {\n $d = $a[1];\n}\nelse {\n $d = $a[2];\n}\n\n$ans += $d;\n$a[1] -= $d;\n$a[2] -= $d;\n\n$a[2] -= $a[2] % 3;\n\n$ans += ($a[2]) / 3;\n\nprint $ans;"}, {"source_code": "chomp ($n = );\nchomp ($str = );\n$odd = 0;\n$even = 0;\n$len = length($str);\nfor $x(0..($len - 1)) {\n $c = substr($str, $x, 1);\n if (($c eq '0' || $c eq '2' || $c eq '4' || $c eq '6' || $c eq '8') && (substr($str, $x + 1, 1) eq ' ' || $x + 1 == $len)) { $even = $even + 1; }\n if (($c eq '1' || $c eq '3' || $c eq '5' || $c eq '7' || $c eq '9') && (substr($str, $x + 1, 1) eq ' ' || $x + 1 == $len)) { $odd = $odd + 1; }\n}\n$min = $odd;\nif ($min > $even) { $min = $even; }\n$odd -= $min;\n$even -= $min;\nprint($min + int($odd / 3));"}, {"source_code": "my $x = ;\nchomp $x;\n\nmy $line = ;\nchomp $line;\n\nmy @splits = split / /, $line;\n\nmy $odd = 0;\nmy $even = 0;\nfor my $split (@splits) {\n if (int($split) % 2 == 0) {\n $even += 1;\n } else {\n $odd += 1;\n }\n}\n\nmy $pair;\n\nif ($even < $odd) {\n $pair = $even;\n} else {\n $pair = $odd;\n}\n\n$odd -= $pair;\n$even -= $pair;\n\nmy $ans = $pair + int($odd / 3);\n\nprint $ans . \"\\n\";\n"}], "negative_code": [{"source_code": "$_ = ; my ($n) = split;\n$_ = ; my @A = split;\n\n@P = grep($_ > 0 && $_ % 2 > 0, @A);\nprint @P . \"\\n\";"}, {"source_code": "my $n = <>;\n\n$inp = [split / /, <>];\n\n@a = (0, 0, 0);\n\n$ans = 0;\n\nforeach (@$inp) {\n if ($_ % 2 == 0) {\n $a[1]++;\n }\n else {\n $a[2]++; \n }\n}\n\nmy $d;\nif ($a[1] < $a[2]) {\n $d = $a[1];\n}\nelse {\n $d = $a[2];\n}\n\n$ans += $d;\n$a[1] -= $d;\n$a[2] -= $d;\n\n$ans += ($a[2]) / 3;\n\nprint $ans;"}, {"source_code": "my $n = <>;\n\n$inp = [split / /, <>];\n\n@a = (0, 0);\n\n$ans = 0;\n\nforeach (@$inp) {\n if ($_ == 1) {\n $a[0]++;\n }\n elsif ($_ % 2 == 0) {\n $a[1]++;\n }\n else {\n $ans++; \n }\n}\n\nmy $d;\nif ($a[0] < $a[1]) {\n $d = $a[0];\n}\nelse {\n $d = $a[1];\n}\n\n$ans += $d;\n\n$ans += ($a[0] - $d) / 3;\n\nprint $ans;"}, {"source_code": "chomp ($n = );\nchomp ($str = );\n$odd = 0;\n$even = 0;\n$len = length($str);\nfor $x(0..($len - 1)) {\n $c = substr($str, $x, 1);\n if (($c eq '0' || $c eq '2' || $c eq '4' || $c eq '6' || $c eq '8') && (substr($str, $x + 1, 1) eq ' ' || $x + 1 == $len)) { $even = $even + 1; }\n if (($c eq '1' || $c eq '3' || $c eq '5' || $c eq '7' || $c eq '9') && (substr($str, $x + 1, 1) eq ' ' || $x + 1 == $len)) { $odd = $odd + 1; }\n}\nif ($odd < $even) { print($odd); }\nelse { print($even); }"}, {"source_code": "my $x = ;\nchomp $x;\n\nmy $line = ;\nchomp $line;\n\nmy @splits = split / /, $line;\n\nmy $odd = 0;\nmy $even = 0;\nfor my $split (@splits) {\n if (int($split) % 2 == 0) {\n $even += 1;\n } else {\n $odd += 1;\n }\n}\n\nif ($even < $odd) {\n print $even . \"\\n\";\n} else {\n print $odd . \"\\n\";\n}\n"}, {"source_code": "my $x = ;\nchomp $x;\n\nmy $line = ;\nchomp $line;\n\nmy @splits = split /,/, $line;\n\nmy $odd = 0;\nmy $even = 0;\nfor my $split (@splits) {\n if (int($split) % 2 == 0) {\n $even += 1;\n } else {\n $odd += 1;\n }\n}\n\nif ($even < $odd) {\n print $even . \"\\n\";\n} else {\n print $odd . \"\\n\";\n}\n"}], "src_uid": "61e6fd68d7568c599204130b102ea389"} {"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": "$_=<>;\nprintf \"%s\", (/AB.*BA/ or /BA.*AB/)?'YES':'NO'"}, {"source_code": "$_=<>;\nprint qw/NO YES/[ 0+(/AB.*BA/ or /BA.*AB/) ]"}, {"source_code": "$_=<>;\nif(/AB.*BA/ or /BA.*AB/) { \n print \"YES\";\n} else {\n print \"NO\";\n}"}, {"source_code": "$_=<>;print/AB.*BA/||/BA.*AB/?YES:NO"}, {"source_code": "$_=<>;print/AB.*BA/||/BA.*AB/?YES:NO\n\n"}, {"source_code": "$_=<>;print/AB.*BA/||/BA.*AB/?YES:NO\n\n"}, {"source_code": "\n$_ = <>;\nmy $ans = \"NO\\n\";\n$ans = \"YES\" if (/AB.*BA/ || /BA.*AB/);\nprint $ans;\n"}, {"source_code": "print qw(YES NO)[$_ = <> xor /AB.*BA/ || /BA.*AB/ ]"}, {"source_code": "$_=<>;print/AB.*BA/||/BA.*AB/?YES:NO\n\n"}, {"source_code": "$_=<>;print/AB.*BA/||/BA.*AB/?YES:NO\n"}, {"source_code": "$_=<>;print/AB.*BA/||/BA.*AB/?YES:NO\n\n"}, {"source_code": "$a = <>; \nif ($a =~ m[AB.*BA]||$a =~ m[BA.*AB]) \n{ \n print \"YES\"; \n} \nelse \n{ \n print \"NO\"; \n}"}, {"source_code": "$ch=<>;\nchomp$ch;\n$occ1=0;\n$occ2=0;\n$n=length$ch;\nfor($i=0;$i<$n-1;$i++){\nif(substr($ch,$i,2)eq \"AB\"){\n$occ1++;\n$ind1=$i;\n\n}\nif(substr($ch,$i,2)eq \"BA\"){\n$occ2++;\n$ind2=$i;\n\n}\n\n}\nif($occ1==0 || $occ2==0 || ($occ1<=2 && $occ2==1 && abs($ind1-$ind2)==1)||($occ2<=2 && $occ1==1 && abs($ind1-$ind2)==1)){\nprint \"NO\";\n}\nelse{\nprint \"YES\";\n}"}, {"source_code": "$_=<>;print/AB.*BA/||/BA.*AB/?YES:NO\n\n"}, {"source_code": "print qw(YES NO)[$_ = <> xor /AB.*BA/ || /BA.*AB/ ]"}, {"source_code": "$_ = <>;\nif (/AB.*BA/ || /BA.*AB/) {\n\tprint 'YES';\n} else {\n\tprint 'NO';\n}\n"}, {"source_code": "$_=<>;print/AB.*BA/||/BA.*AB/?YES:NO\n\n"}, {"source_code": "$_=<>;print/AB.*BA/||/BA.*AB/?YES:NO\n\n"}, {"source_code": "print qw(YES NO)[$_ = <> xor /AB.*BA/ || /BA.*AB/ ]"}, {"source_code": "print qw(YES NO)[$_ = <> xor /AB.*BA/ || /BA.*AB/ ]"}, {"source_code": "$_=<>;print/AB.*BA/||/BA.*AB/?YES:NO\n\n"}], "negative_code": [{"source_code": "$_=<>;\nprint qw/NO YES/[ /AB.*BA/ or /BA.*AB/ ]"}, {"source_code": "$ch=<>;\nchomp$ch;\n$occ1=0;\n$occ2=0;\n$n=length$ch;\nfor($i=0;$i<$n-1;$i++){\nif(substr($ch,$i,2)eq \"AB\"){\n$occ1++;\n$ind1=$i;\n\n}\nif(substr($ch,$i,2)eq \"BA\"){\n$occ2++;\n$ind2=$i;\n\n}\n\n}\nif($occ1==0 || $occ2==0 || ($occ1==1 && $occ2==1 && abs($ind1-$ind2)==1)){\nprint \"NO\";\n}\nelse{\nprint \"YES\";\n}"}, {"source_code": "print qw(YES NO)[$_ = <>, ! (/AB.*BA/ || /BA.*AB/) ]"}], "src_uid": "33f7c85e47bd6c83ab694a834fa728a2"} {"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$$$) \u00a0\u2014 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": "<>&do{print((map{++$c%2?'a'eq$_?'b':'a':'z'eq$_?'y':'z'}split'',$_),\"\\n\")until$c=!chomp($_=<>)}"}, {"source_code": "for (1..<>) {\r\n chomp($_=<>);\r\n my $c = 0;\r\n for my $s (split'', $_) {\r\n print ++$c % 2 ? ($s eq 'a' ? 'b' : 'a') : ($s eq 'z' ? 'y' : 'z')\r\n }\r\n print \"\\n\"\r\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\ts/(.)(.)?/\n\t\tmy( $_1, $_2 ) = ( $1, $2 );\n\t\t$_1 =~ y!ab-z!ba!;\n\t\t$_2 =~ y!za-y!yz!;\n\t\t$_1 . $_2;\n\t\t/ge;\n\t\n\tprint;\n\t}"}], "negative_code": [], "src_uid": "c02357c4d959e300f970f66f9b3107eb"} {"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\u2009\u00d7\u20092 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\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200950) \u2014 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": "$m = (split \" \", <>)[1] - 1;\nlocal $/;\n$_ = <>;\n$i += \"acef\" eq join '', sort split //, \"$1$2$3\" while /([face])(?=([face]).{$m}([face]{2}))/sg;\nprint 0 + $i\n"}, {"source_code": "$m = (split \" \", <>)[1] - 1;\nlocal $/;\n$_ = <>;\n$i += \"acef\" eq join '', sort split //, \"$1$2$3\" while /([face])(?=([face]).{$m}([face]{2}))/sg;\nprint 0 + $i\n"}, {"source_code": "$m = (split \" \", <>)[1] - 1;\nlocal $/;\n$_ = <>;\n$i += \"acef\" eq join '', sort split //, \"$1$2$3\" while /([face])(?=([face]).{$m}([face]{2}))/sg;\nprint 0 + $i"}, {"source_code": "$m = (split \" \", <>)[1] - 1;\nlocal $/;\n$_ = <>;\n$i += \"acef\" eq join '', sort split //, \"$1$2$3\" while /([face])(?=([face]).{$m}([face]{2}))/sg;\nprint 0 + $i\n"}, {"source_code": "$m = (split \" \", <>)[1] - 1;\nlocal $/;\n$_ = <>;\n$i += \"acef\" eq join '', sort split //, \"$1$2$3\" while /([face])(?=([face]).{$m}([face]{2}))/sg;\nprint 0 + $i\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n($n, $m) = split / /, <>;\npush @a, [split //, <>] foreach(1 .. $n);\n($n<2 || $m<2) and say 0 and exit;\n$ans = 0;\nforeach $i (0 .. $n-2) {\n\tforeach $j (0 .. $m-2) {\n\t\t$f = ($a[$i][$j]eq'f' || $a[$i][$j+1]eq'f' || $a[$i+1][$j]eq'f' || $a[$i+1][$j+1]eq'f');\n\t\t$a = ($a[$i][$j]eq'a' || $a[$i][$j+1]eq'a' || $a[$i+1][$j]eq'a' || $a[$i+1][$j+1]eq'a');\n\t\t$c = ($a[$i][$j]eq'c' || $a[$i][$j+1]eq'c' || $a[$i+1][$j]eq'c' || $a[$i+1][$j+1]eq'c');\n\t\t$e = ($a[$i][$j]eq'e' || $a[$i][$j+1]eq'e' || $a[$i+1][$j]eq'e' || $a[$i+1][$j+1]eq'e');\n\t\t# say \"[$i, $j]: $f $a $c $e\";\n\t\t$ans += ($f && $a && $c && $e) ? 1:0;\n\t}\n}\nsay $ans;\n"}, {"source_code": "$m = (split \" \", <>)[1] - 1;\nlocal $/;\n$_ = <>;\n$i += \"acef\" eq join '', sort split //, \"$1$2$3\" while /([face])(?=([face]).{$m}([face]{2}))/sg;\nprint 0 + $i\n"}, {"source_code": "$m = (split \" \", <>)[1] - 1;\nlocal $/;\n$_ = <>;\n$i += \"acef\" eq join '', sort split //, \"$1$2$3\" while /([face])(?=([face]).{$m}([face]{2}))/sg;\nprint 0 + $i\n"}, {"source_code": "$m = (split \" \", <>)[1] - 1;\nlocal $/;\n$_ = <>;\n$i += \"acef\" eq join '', sort split //, \"$1$2$3\" while /([face])(?=([face]).{$m}([face]{2}))/sg;\nprint 0 + $i\n"}, {"source_code": "$m = (split \" \", <>)[1] - 1;\nlocal $/;\n$_ = <>;\n$i += \"acef\" eq join '', sort split //, \"$1$2$3\" while /([face])(?=([face]).{$m}([face]{2}))/sg;\nprint 0 + $i"}], "negative_code": [], "src_uid": "742e4e6ca047da5f5ebe5d854d6a2024"} {"nl": {"description": "You are given a binary string $$$s$$$ (recall that a string is binary if each character is either $$$0$$$ or $$$1$$$).Let $$$f(t)$$$ be the decimal representation of integer $$$t$$$ written in binary form (possibly with leading zeroes). For example $$$f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$$$ and $$$f(000100) = 4$$$.The substring $$$s_{l}, s_{l+1}, \\dots , s_{r}$$$ is good if $$$r - l + 1 = f(s_l \\dots s_r)$$$.For example string $$$s = 1011$$$ has $$$5$$$ good substrings: $$$s_1 \\dots s_1 = 1$$$, $$$s_3 \\dots s_3 = 1$$$, $$$s_4 \\dots s_4 = 1$$$, $$$s_1 \\dots s_2 = 10$$$ and $$$s_2 \\dots s_4 = 011$$$. Your task is to calculate the number of good substrings of string $$$s$$$.You have to answer $$$t$$$ independent queries.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) \u2014 the number of queries. The only line of each query contains string $$$s$$$ ($$$1 \\le |s| \\le 2 \\cdot 10^5$$$), consisting of only digits $$$0$$$ and $$$1$$$. It is guaranteed that $$$\\sum\\limits_{i=1}^{t} |s_i| \\le 2 \\cdot 10^5$$$.", "output_spec": "For each query print one integer \u2014 the number of good substrings of string $$$s$$$.", "sample_inputs": ["4\n0110\n0101\n00001000\n0001000"], "sample_outputs": ["4\n3\n4\n3"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n#use bigint;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n \n\tchomp;\n \n\tmy $cnt = 0;\n my $dec;\n my $bin;\n \n\t/\n\t\t(?{ $dec = 0; })\n\t\t#(?{ $bin = ''; })\n\t\t0*+\n\t\t(*SKIP)\n\t\t(?=\n\t\t\t(.{0,17}?)(.)\n\t\t#\t(?(?{ $+[0] - $-[0] + 2 < $+[2] - $-[1] << 1 }) (*F) )\n\t\t\t\n\t\t\t(?{\n\t\t $dec <<= 1;\n\t\t\t $dec |= $2;\n\t\t\t# $bin .= $2;\n\t\t\t\t$cnt ++ if $+[0] - $-[0] >= $dec;\n\t\t\t#\t$cnt ++ if $+[0] - $-[0] >= eval \"0b$bin\";\n\t\t\t\t})\n\t\t\t(*F)\n\t\t\t)\n\t\t(*F)\n\t\t/x;\n\t\n\t$cnt -= () = /01/g;\n\t$cnt -= () = /01(?=0)/g;\n\t\n\tprint $cnt;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t\n\tchomp;\n\t\n\tmy $cnt = 0;\t\n\tmy $dec;\n\t\n\t/\n\t\t(?{ $dec = 0; })\n\t\t0*+\n\t\t(*SKIP)\n\t\t(?=\n\t\t\t(.{0,17}?)(.)\n\t\t\t(?{\n\t\t\t\t$dec <<= 1;\n\t\t\t\t$dec |= $2;\n\t\t\t\t$cnt ++ if $+[0] - $-[0] >= $dec;\n\t\t\t\t})\n\t\t\t(*F)\n\t\t\t)\n\t\t(*F)\n\t\t/x;\n\t\n\t$cnt -= () = /01/g;\n\t$cnt -= () = /01(?=0)/g;\n\t\n\tprint $cnt;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n \n\tchomp;\n \n\tmy $cnt = 0;\n my $dec;\n \n\t/\n\t\t(?{ $dec = 0; })\n\t\t0*+\n\t\t(*SKIP)\n\t\t(?=\n\t\t\t(.*?)(.)\n\t\t\t(?(?{ $+[0] - $-[0] + 2 < $+[2] - $-[1] << 1 }) (*F) )\n\t\t\t(?{\n\t\t\t $dec <<= 1;\n\t\t\t $dec |= $2;\n\t\t\t\t$cnt ++ if $+[0] - $-[0] >= $dec;\n\t\t\t\t})\n\t\t\t(*F)\n\t\t\t)\n\t\t(*F)\n\t\t/x;\n\t\n\t$cnt -= () = /01/g;\n\t$cnt -= () = /01(?=0)/g;\n\t\n\tprint $cnt;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n \n\tchomp;\n \n\tmy $cnt = 0;\n my $dec;\n \n\t/\n\t\t(?{ $dec = 0; })\n\t\t(0*+)\n\t\t(*SKIP)\n\t\t(?=\n\t\t\t(.*?)(.)\n\t\t\t(?(?{ $+[0] - $-[0] + 2 < $+[3] - $+[1] << 1 }) (*F) )\n\t\t\t(?{\n\t\t\t $dec <<= 1;\n\t\t\t $dec |= $3;\n\t\t\t\t$cnt ++ if $+[0] - $-[0] >= $dec;\n\t\t\t\t})\n\t\t\t(*F)\n\t\t\t)\n\t\t(*F)\n\t\t/x;\n\t\n\t$cnt -= () = /01/g;\n\t$cnt -= () = /01(?=0)/g;\n\t\n\tprint $cnt;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n#use bigint;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n \n\tchomp;\n \n\tmy $cnt = 0;\n my $dec;\n my $bin;\n \n\t/\n\t\t(?{ $dec = 0; })\n\t\t(?{ $bin = ''; })\n\t\t0*+\n\t\t(*SKIP)\n\t\t(?=\n\t\t\t(.*?)(.)\n\t\t\t(?(?{ $+[2] - $-[1] > 20 }) (*PRUNE) (*F) )\n\t\t\t(?(?{ $+[0] - $-[0] + 2 < $+[2] - $-[1] << 1 }) (*F) )\n\t\t\t\n\t\t\t(?{\n\t\t $dec <<= 1;\n\t\t\t $dec |= $2;\n\t\t\t# $bin .= $2;\n\t\t\t\t$cnt ++ if $+[0] - $-[0] >= $dec;\n\t\t\t#\t$cnt ++ if $+[0] - $-[0] >= eval \"0b$bin\";\n\t\t\t\t})\n\t\t\t(*F)\n\t\t\t)\n\t\t(*F)\n\t\t/x;\n\t\n\t$cnt -= () = /01/g;\n\t$cnt -= () = /01(?=0)/g;\n\t\n\tprint $cnt;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n#use bigint;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n \n\tchomp;\n \n\tmy $cnt = 0;\n my $dec;\n my $bin;\n \n\t/\n\t\t(?{ $dec = 0; })\n\t\t(?{ $bin = ''; })\n\t\t0*+\n\t\t(*SKIP)\n\t\t(?=\n\t\t\t(.*?)(.)\n\t\t\t(?(?{ $+[2] - $-[1] > 20 }) (*SKIP) (*F) )\n\t\t\t(?(?{ $+[0] - $-[0] + 2 < $+[2] - $-[1] << 1 }) (*F) )\n\t\t\t\n\t\t\t(?{\n\t\t $dec <<= 1;\n\t\t\t $dec |= $2;\n\t\t\t# $bin .= $2;\n\t\t\t\t$cnt ++ if $+[0] - $-[0] >= $dec;\n\t\t\t#\t$cnt ++ if $+[0] - $-[0] >= eval \"0b$bin\";\n\t\t\t\t})\n\t\t\t(*F)\n\t\t\t)\n\t\t(*F)\n\t\t/x;\n\t\n\t$cnt -= () = /01/g;\n\t$cnt -= () = /01(?=0)/g;\n\t\n\tprint $cnt;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n \n\tchomp;\n \n\tmy $cnt = 0;\n my $dec;\n \n\t/\n\t\t(?{ $dec = 0; })\n\t\t0*+\n\t\t(*SKIP)\n\t\t(?=\n\t\t\t(.*?)(.)\n\t\t\t(?(?{ $+[0] - $-[0] + 2 < $+[2] - $-[1] << 1 }) (*F) )\n\t\t\t(?{\n\t\t\t $dec <<= 1;\n\t\t\t $dec += $2;\n\t\t\t\t$cnt ++ if $+[0] - $-[0] >= $dec;\n\t\t\t\t})\n\t\t\t(*F)\n\t\t\t)\n\t\t(*F)\n\t\t/x;\n\t\n\t$cnt -= () = /01/g;\n\t$cnt -= () = /01(?=0)/g;\n\t\n\tprint $cnt;\n\t}"}], "src_uid": "3c93a76f986b1ef653bf5834716ac72a"} {"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 \u2014 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$$$) \u2014 the number of test cases. The first line on each test case contains single integer $$$n$$$ ($$$1 \\le n \\le 50$$$) \u2014 the number of binary strings you have. Next $$$n$$$ lines contains binary strings $$$s_1, s_2, \\dots, s_n$$$ \u2014 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 \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\tmy $odd = grep { ( length ) % 2 } @_;\n\t\n\tif( $odd ){\n\t\tprint ~~ @_;\n\t\t}\n\telse{\n\t\tmy $ones = () = \"@_\" =~ /1/g;\n\t\tif( $ones % 2 ){\n\t\t\tprint @_ - 1;\n\t\t\t}\n\t\telse{\n\t\t\tprint ~~ @_;\n\t\t\t}\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\tmy( $odd ) = grep { ( length ) % 2 } @_;\n\t\n\tif( $odd ){\n\t\tprint ~~ @_;\n\t\t}\n\telse{\n\t\tmy $ones = () = \"@_\" =~ /1/g;\n\t\tif( $ones % 2 ){\n\t\t\tprint @_ - 1;\n\t\t\t}\n\t\telse{\n\t\t\tprint ~~ @_;\n\t\t\t}\n\t\t}\n\t}"}], "src_uid": "3b8678d118c90d34c99ffa9259cc611f"} {"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$$$) \u00a0\u2014 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$$$) \u00a0\u2014 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": "<>;<>;while(<>){if(/([13579].*?[13579])/){print \"$1\\n\";}else{print -1,\"\\n\";}<>;}"}, {"source_code": "$\\ = $/;\n\n<>;\n\nprint <> =~ /([13579]).*?([13579])/ ? $1 . $2 : -1 while <>;"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\tprint /([13579]).*?([13579])/ ? $1 . $2 : -1;\n\t}"}, {"source_code": "$\\ = $/;\n\n<>;\n\nprint <> =~ /[13579].*?[13579]/ ? $& : -1 while <>;\nprint <> =~ y/02468//dr =~ /../ ? $& : -1 while <>; # alt\n"}], "negative_code": [{"source_code": "<>;<>;while(<>){if(/([13579].*[13579])/){print \"$1\\n\";}else{print -1,\"\\n\";}<>;}"}, {"source_code": "$\\ = $/;\n\n<>;\n\nprint <> =~ /[13579].*[13579]/ ? $& : -1 while <>;\nprint <> =~ y/02468//dr =~ /../ ? $& : -1 while <>; # alt\n"}], "src_uid": "8f00837e04627e445dfb8b6cd0216640"} {"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$$$) \u2014 the number of queries. Next $$$T$$$ lines contain descriptions of queries \u2014 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$$$) \u2014 the initial strength and intelligence of the character and the number of free points, respectively.", "output_spec": "Print $$$T$$$ integers \u2014 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": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $str, $int, $exp ) = split;\n\t\n\t$str += $exp;\n\t\n\tprint $str > $int ? +( sort { $a <=> $b } $exp + 1, $str - $int + 1 >> 1 )[ 0 ] : 0;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $str, $int, $exp ) = split;\n\t\n\t$str += $exp;\n\t\n\tprint +( $str - $int + 1 ) >> 1;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $str, $int, $exp ) = split;\n\t\n\t$str += $exp;\n\t\n\tprint $str > $int ? $str - $int + 1 >> 1 : 0;\n\t}"}], "src_uid": "0816295355375a2d3f1cd45852b86360"} {"nl": {"description": "Lately, a national version of a bingo game has become very popular in Berland. There are n players playing the game, each player has a card with numbers. The numbers on each card are distinct, but distinct cards can have equal numbers. The card of the i-th player contains mi numbers.During the game the host takes numbered balls one by one from a bag. He reads the number aloud in a high and clear voice and then puts the ball away. All participants cross out the number if it occurs on their cards. The person who crosses out all numbers from his card first, wins. If multiple people cross out all numbers from their cards at the same time, there are no winners in the game. At the beginning of the game the bag contains 100 balls numbered 1 through 100, the numbers of all balls are distinct.You are given the cards for each player. Write a program that determines whether a player can win the game at the most favorable for him scenario or not.", "input_spec": "The first line of the input contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of the players. Then follow n lines, each line describes a player's card. The line that describes a card starts from integer mi (1\u2009\u2264\u2009mi\u2009\u2264\u2009100) that shows how many numbers the i-th player's card has. Then follows a sequence of integers ai,\u20091,\u2009ai,\u20092,\u2009...,\u2009ai,\u2009mi (1\u2009\u2264\u2009ai,\u2009k\u2009\u2264\u2009100) \u2014 the numbers on the i-th player's card. The numbers in the lines are separated by single spaces. It is guaranteed that all the numbers on each card are distinct.", "output_spec": "Print n lines, the i-th line must contain word \"YES\" (without the quotes), if the i-th player can win, and \"NO\" (without the quotes) otherwise.", "sample_inputs": ["3\n1 1\n3 2 4 1\n2 10 11", "2\n1 1\n1 1"], "sample_outputs": ["YES\nNO\nYES", "NO\nNO"], "notes": null}, "positive_code": [{"source_code": "my $n = <>;\n\n# my @m = split \" \", scalar <>;\n\nmy @A = map {my @tmp = split \" \"; shift (@tmp) == @tmp or die \"???\"; [@tmp];} <>;\n\n# (scalar @A) == (scalar @m) or die ((scalar @A) . ' ' . (scalar @m));\n\nmy @B = map{\n my @tmp = ();\n $tmp[100] = undef;\n @tmp[@$_] = (1) x @$_;\n \\@tmp;\n } @A;\n\nforeach my $curr (@B){\n my $good = 2;\n foreach my $chk (@A){\n # continue if $_ == $curr;\n $good-- unless grep {not $_} @$curr[@$chk];\n }\n print $good > 0 ? \"YES\\n\" : \"NO\\n\";\n }\n1;"}, {"source_code": "\nmy $n = <>;\n\n# my @m = split \" \", scalar <>;\n\nmy @A = map {my @tmp = split \" \"; shift (@tmp) == @tmp or die \"???\"; [@tmp];} <>;\n\n# (scalar @A) == (scalar @m) or die ((scalar @A) . ' ' . (scalar @m));\n\nmy @B = map{\n my @tmp = ();\n $tmp[100] = undef;\n @tmp[@$_] = (1) x @$_;\n \\@tmp;\n } @A;\n\nforeach my $curr (@B){\n my $good = 2;\n foreach my $chk (@A){\n # continue if $_ == $curr;\n $good-- unless grep {not $_} @$curr[@$chk];\n }\n print $good > 0 ? \"YES\\n\" : \"NO\\n\";\n }\n\n1;\n\n\n"}], "negative_code": [{"source_code": "$n = <>;\n@A = map {@tmp = split \" \"; shift (@tmp) == @tmp or die \"???\"; [@tmp];} <>;\n\n@B = map{\n @tmp = ();\n $tmp[100] = undef;\n @tmp[@$_] = (1) x @$_;\n \\@tmp;\n } @A;\n\nforeach $curr (@B){\n $good = 2;\n foreach $chk (@A){\n $good-- unless grep {not $_} @$curr[@$chk];\n }\n print $good > 0 ? \"YES\\n\" : \"NO\\n\";\n }"}], "src_uid": "90a47422f7ae53beb0a817422d760c6e"} {"nl": {"description": "The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coordinate of every vector is 1 or \u2009-\u20091 and any two vectors are orthogonal. Just as a reminder, two vectors in n-dimensional space are considered to be orthogonal if and only if their scalar product is equal to zero, that is: .Danil quickly managed to come up with the solution for this problem and the teacher noticed that the problem can be solved in a more general case for 2k vectors in 2k-dimensinoal space. When Danil came home, he quickly came up with the solution for this problem. Can you cope with it?", "input_spec": "The only line of the input contains a single integer k (0\u2009\u2264\u2009k\u2009\u2264\u20099).", "output_spec": "Print 2k lines consisting of 2k characters each. The j-th character of the i-th line must be equal to '\u2009*\u2009' if the j-th coordinate of the i-th vector is equal to \u2009-\u20091, and must be equal to '\u2009+\u2009' if it's equal to \u2009+\u20091. It's guaranteed that the answer always exists. If there are many correct answers, print any.", "sample_inputs": ["2"], "sample_outputs": ["++**\n+*+*\n++++\n+**+"], "notes": "NoteConsider all scalar products in example: Vectors 1 and 2: (\u2009+\u20091)\u00b7(\u2009+\u20091)\u2009+\u2009(\u2009+\u20091)\u00b7(\u2009-\u20091)\u2009+\u2009(\u2009-\u20091)\u00b7(\u2009+\u20091)\u2009+\u2009(\u2009-\u20091)\u00b7(\u2009-\u20091)\u2009=\u20090 Vectors 1 and 3: (\u2009+\u20091)\u00b7(\u2009+\u20091)\u2009+\u2009(\u2009+\u20091)\u00b7(\u2009+\u20091)\u2009+\u2009(\u2009-\u20091)\u00b7(\u2009+\u20091)\u2009+\u2009(\u2009-\u20091)\u00b7(\u2009+\u20091)\u2009=\u20090 Vectors 1 and 4: (\u2009+\u20091)\u00b7(\u2009+\u20091)\u2009+\u2009(\u2009+\u20091)\u00b7(\u2009-\u20091)\u2009+\u2009(\u2009-\u20091)\u00b7(\u2009-\u20091)\u2009+\u2009(\u2009-\u20091)\u00b7(\u2009+\u20091)\u2009=\u20090 Vectors 2 and 3: (\u2009+\u20091)\u00b7(\u2009+\u20091)\u2009+\u2009(\u2009-\u20091)\u00b7(\u2009+\u20091)\u2009+\u2009(\u2009+\u20091)\u00b7(\u2009+\u20091)\u2009+\u2009(\u2009-\u20091)\u00b7(\u2009+\u20091)\u2009=\u20090 Vectors 2 and 4: (\u2009+\u20091)\u00b7(\u2009+\u20091)\u2009+\u2009(\u2009-\u20091)\u00b7(\u2009-\u20091)\u2009+\u2009(\u2009+\u20091)\u00b7(\u2009-\u20091)\u2009+\u2009(\u2009-\u20091)\u00b7(\u2009+\u20091)\u2009=\u20090 Vectors 3 and 4: (\u2009+\u20091)\u00b7(\u2009+\u20091)\u2009+\u2009(\u2009+\u20091)\u00b7(\u2009-\u20091)\u2009+\u2009(\u2009+\u20091)\u00b7(\u2009-\u20091)\u2009+\u2009(\u2009+\u20091)\u00b7(\u2009+\u20091)\u2009=\u20090 "}, "positive_code": [{"source_code": "$, = $/;\n\n@_ = ('+');\n\n@_ = map {\n\ts/./$&$&/rg,\n\ts/./$& eq '+' ? '+*':'*+'/rge\n} @_ for 1 .. <>;\n\t\nprint @_"}, {"source_code": "use feature ':all';\n\nwhile(<>){\n\t@_ = ('+');\n\t/0/ and say \"@_\" and next;\n\tfor $i (1 .. $_){\n\t\t@new = ();\n\t\tfor $line (@_){\n\t\t\t@syms = split //, $line;\n\t\t\t$doubled = '';\n\t\t\tfor $k (@syms){ $doubled .= $k . $k };\n\t\t\t$mixed = '';\n\t\t\tfor $k (@syms){ $mixed .= $k . ($k eq '+' ? '*' : '+') };\n\t\t\tpush @new, $doubled, $mixed;\n\t\t\t}\n\t\t@_ = @new;\n\t\t}\n\tsay join \"\\n\", @_;\n\t}"}], "negative_code": [], "src_uid": "4e25d49e8a50dacc1cfcc9413ee83db6"} {"nl": {"description": "You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i.\u2009e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$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 10^4$$$) \u2014 the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$) \u2014 the goal of the $$$i$$$-th test case.", "output_spec": "For each test case, print one integer \u2014 the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case.", "sample_inputs": ["4\n\n1\n\n3\n\n4\n\n12"], "sample_outputs": ["2\n1\n2\n4"], "notes": null}, "positive_code": [{"source_code": "print-1&(1+(<>-1||3))/3+0.9,\" \"for(1..<>)"}, {"source_code": "print-1&(1+(<>-1||3))/3+0.9,\" \"for(1..<>)"}], "negative_code": [], "src_uid": "208e285502bed3015b30ef10a351fd6d"} {"nl": {"description": "XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $$$n$$$ booths, arranged in a circle. The booths are numbered $$$1$$$ through $$$n$$$ clockwise with $$$n$$$ being adjacent to $$$1$$$. The $$$i$$$-th booths sells some candies for the price of $$$a_i$$$ burles per item. Each booth has an unlimited supply of candies.Polycarp has decided to spend at most $$$T$$$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $$$1$$$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not). Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.Calculate the number of candies Polycarp will buy.", "input_spec": "The first line contains two integers $$$n$$$ and $$$T$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le T \\le 10^{18}$$$) \u2014 the number of booths at the fair and the initial amount of burles Polycarp has. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) \u2014 the price of the single candy at booth number $$$i$$$.", "output_spec": "Print a single integer \u2014 the total number of candies Polycarp will buy.", "sample_inputs": ["3 38\n5 2 5", "5 21\n2 4 100 2 6"], "sample_outputs": ["10", "6"], "notes": "NoteLet's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $$$1$$$, buys candy for $$$5$$$, $$$T = 33$$$; Booth $$$2$$$, buys candy for $$$2$$$, $$$T = 31$$$; Booth $$$3$$$, buys candy for $$$5$$$, $$$T = 26$$$; Booth $$$1$$$, buys candy for $$$5$$$, $$$T = 21$$$; Booth $$$2$$$, buys candy for $$$2$$$, $$$T = 19$$$; Booth $$$3$$$, buys candy for $$$5$$$, $$$T = 14$$$; Booth $$$1$$$, buys candy for $$$5$$$, $$$T = 9$$$; Booth $$$2$$$, buys candy for $$$2$$$, $$$T = 7$$$; Booth $$$3$$$, buys candy for $$$5$$$, $$$T = 2$$$; Booth $$$1$$$, buys no candy, not enough money; Booth $$$2$$$, buys candy for $$$2$$$, $$$T = 0$$$. No candy can be bought later. The total number of candies bought is $$$10$$$.In the second example he has $$$1$$$ burle left at the end of his path, no candy can be bought with this amount."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $t ) = split;\n\t\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\t$sum += $_ for @_;\n\t\n\tmy $cnt = 0;\n\t\n\tmy $i = 0;\n\t\n\twhile( @_ ){\n\t\tif( 0 < int $t / $sum ){\n\t\t\t$cnt += @_ * ( int $t / $sum );\n\t\t\t$t -= $sum * ( int $t / $sum );\n\t\t\t}\n\t\tmy $this = shift @_;\n\t\tif( $this > $t ){\n\t\t\t$sum -= $this;\n\t\t\tnext;\n\t\t\t}\n\t\telse{\n\t\t\t$t -= $this;\n\t\t\t$cnt ++;\n\t\t\tpush @_, $this;\n\t\t\tnext;\n\t\t\t}\n\t\t}\n\t\n\tprint $cnt;\n\t}"}, {"source_code": "( $n, $t, @_ ) = split ' ', <> . <>;\n\n$s += $_ for @_;\n\nwhile( @_ ){\n\t$m = int $t / $s;\n\tif( $m ){\n\t\t$c += @_ * $m;\n\t\t$t -= $s * $m;\n\t\t}\n\t$i = shift @_;\n\tif( $i > $t ){\n\t\t$s -= $i;\n\t\t}\n\telse{\n\t\t$t -= $i;\n\t\t$c ++;\n\t\tpush @_, $i;\n\t\t}\n\t}\n\nprint 0 + $c"}], "negative_code": [], "src_uid": "92db85bb3880b9d11f93cacd566ecda7"} {"nl": {"description": "It's a walking tour day in SIS.Winter, so $$$t$$$ groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another.Initially, some students are angry. Let's describe a group of students by a string of capital letters \"A\" and \"P\": \"A\" corresponds to an angry student \"P\" corresponds to a patient student Such string describes the row from the last to the first student.Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index $$$i$$$ in the string describing a group then they will throw a snowball at the student that corresponds to the character with index $$$i+1$$$ (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them.Let's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry.Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group.", "input_spec": "The first line contains a single integer $$$t$$$\u00a0\u2014 the number of groups of students ($$$1 \\le t \\le 100$$$). The following $$$2t$$$ lines contain descriptions of groups of students. The description of the group starts with an integer $$$k_i$$$ ($$$1 \\le k_i \\le 100$$$)\u00a0\u2014 the number of students in the group, followed by a string $$$s_i$$$, consisting of $$$k_i$$$ letters \"A\" and \"P\", which describes the $$$i$$$-th group of students.", "output_spec": "For every group output single integer\u00a0\u2014 the last moment a student becomes angry.", "sample_inputs": ["1\n4\nPPAP", "3\n12\nAPPAPPPAPPPP\n3\nAAP\n3\nPPA"], "sample_outputs": ["1", "4\n1\n0"], "notes": "NoteIn the first test, after $$$1$$$ minute the state of students becomes PPAA. After that, no new angry students will appear.In the second tets, state of students in the first group is: after $$$1$$$ minute\u00a0\u2014 AAPAAPPAAPPP after $$$2$$$ minutes\u00a0\u2014 AAAAAAPAAAPP after $$$3$$$ minutes\u00a0\u2014 AAAAAAAAAAAP after $$$4$$$ minutes all $$$12$$$ students are angry In the second group after $$$1$$$ minute, all students are angry."}, "positive_code": [{"source_code": "#!/usr/bin/env perl\npackage Codeforces::Task::A;\nuse v5.10;\nuse strict;\nuse warnings;\n\n# int ans = 0, cur = -1e5;\n# for (char c : $s) {\n# if (c == 'A') {ans = max(ans, cur);cur = 0;}else {++cur;}\n# }\n# answer(max(ans,cur));\n\nsub solve {\n my ($ans, $n) = (0, int(<>));\n my @s = split(\"\", <>); \n my $p = $n - 1;\n for (my $i = $n - 1; $i > -1; $i--) {\n if ($s[$i] eq \"A\") {\n if ($ans < $p - $i)\n { $ans = $p - $i; }\n $p = $i - 1;\n\t\t}\n\t}\n print \"$ans\\n\";\n}\n\nsub main {\n my $t = 1;\n $t = int(<>);\n while ($t--) {\n solve();\n }\n return 0;\n}\n\nexit(main());\n"}, {"source_code": "#!/usr/bin/env perl\npackage Codeforces::Task::A;\nuse v5.10;\nuse strict;\nuse warnings;\n\nsub solve {\n my ($ans, $n) = (0, int(<>));\n my @s = split(\"\", <>); \n my $p = $n - 1;\n for (my $i = $n - 1; $i > -1; $i--) {\n if ($s[$i] eq \"A\") {\n if ($ans < $p - $i)\n { $ans = $p - $i; }\n $p = $i - 1;\n\t\t}\n\t}\n print \"$ans\\n\";\n}\n\nsub main {\n my $t = 1;\n $t = int(<>);\n while ($t--) {\n solve();\n }\n return 0;\n}\n\nexit(main());\n"}, {"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n while($q > 0) {\n $q--;\n my $s= <$in>;\n my $z= <$in>;\n my @l= split('', $z);\n\n my $i= -1;\n for my $stud (@l) {\n last if($stud eq 'A');\n if($i==-1) {\n $i= 0;\n } else {\n $i++;\n }\n }\n\n $i++;\n\n if($i == -1) {\n print \"0\\n\";\n next;\n }\n\n my $c= 0;\n my $max= 0;\n for(my $j=$i; $j < $#l; $j++) {\n if($l[$j] eq 'A') {\n $c= 0;\n } else {\n $c++;\n if($max < $c) {\n $max = $c;\n }\n }\n\n }\n\n print \"$max\\n\";\n\n }\n\n}\n\nmain() unless caller();\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\npackage Codeforces::Task::A;\nuse v5.10;\nuse strict;\nuse warnings;\n\nsub solve {\n my ($ans, $n) = (0, int(<>));\n my @s = split(\"\", <>); \n my $f = 0;\n for (my $i = $n - 1; $i > -1; $i--) {\n\t\tif ($s[$i] eq 'A') {\n $f = 1;\n\t\t last;\n\t\t}\n $ans++;\n\t}\n\t$ans *= $f;\n print \"$ans\\n\";\n}\n\nsub main {\n my $t = 1;\n $t = int(<>);\n while ($t--) {\n solve();\n }\n return 0;\n}\n\nexit(main());\n"}, {"source_code": "#!/usr/bin/env perl\npackage Codeforces::Task::A;\nuse v5.10;\nuse strict;\nuse warnings;\n\nsub solve {\n my ($ans, $n) = (0, int(<>));\n my @s = split(\"\", <>); \n for (my $i = $n - 1; $i > -1; $i--) {\n\t\tif ($s[$i] eq 'A') {\n\t\t last;\n\t\t}\n $ans++;\n\t}\n print \"$ans\\n\";\n}\n\nsub main {\n my $t = 1;\n $t = int(<>);\n while ($t--) {\n solve();\n }\n return 0;\n}\n\nexit(main());\n"}, {"source_code": "#!/usr/bin/env perl\npackage Codeforces::Task::A;\nuse v5.10;\nuse strict;\nuse warnings;\n\n# int ans = 0, cur = -1e5;\n# for (char c : $s) {\n# if (c == 'A') {ans = max(ans, cur);cur = 0;}else {++cur;}\n# }\n# answer(max(ans,cur));\n\nsub solve {\n my ($ans, $n) = (0, int(<>));\n my @s = split(\"\", <>); \n push(@s, \"A\");\n my $p = $n - 1;\n for (my $i = $n - 1; $i > -1; $i--) {\n if ($s[$i] eq \"A\") {\n if ($ans < $p - $i)\n { $ans = $p - $i; }\n $p = $i;\n\t\t}\n\t}\n print \"$ans\\n\";\n}\n\nsub main {\n my $t = 1;\n $t = int(<>);\n while ($t--) {\n solve();\n }\n return 0;\n}\n\nexit(main());\n"}], "src_uid": "1539fc8ef4f4cdcd1e14faf4f1b4ee8b"} {"nl": {"description": "Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi\u2009-\u2009(ti\u2009-\u2009k). Otherwise, the Rabbits get exactly fi units of joy.Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value. ", "input_spec": "The first line contains two space-separated integers \u2014 n (1\u2009\u2264\u2009n\u2009\u2264\u2009104) and k (1\u2009\u2264\u2009k\u2009\u2264\u2009109) \u2014 the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers \u2014 fi (1\u2009\u2264\u2009fi\u2009\u2264\u2009109) and ti (1\u2009\u2264\u2009ti\u2009\u2264\u2009109) \u2014 the characteristics of the i-th restaurant.", "output_spec": "In a single line print a single integer \u2014 the maximum joy value that the Rabbits will get from the lunch. ", "sample_inputs": ["2 5\n3 3\n4 5", "4 6\n5 8\n3 6\n2 3\n2 2", "1 5\n1 7"], "sample_outputs": ["4", "3", "-1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nsub max {\n\tmy ($x, $y) = (shift, shift);\n\treturn $x>$y ? $x:$y;\n}\n\n($n, $t) = split / /, <>;\n$ans = -1000000005;\nforeach (1 .. $n) {\n\t@a = split / /, <>;\n\tif ($a[1] > $t) {\n\t\t$ans = max($ans, $a[0]-$a[1]+$t);\n\t} else {\n\t\t$ans = max($ans, $a[0]);\n\t}\n}\nsay $ans;"}, {"source_code": "#!/bin/perl\nuse strict;\nuse warnings;\n\nmy ($n, $k) = map int, split '\\D', <>;\nmy $p = 0;\nfor my $i (1..$n) {\n my ($f, $t) = map int, split '\\D', <>;\n my $np = ($k < $t)? $f - $t + $k: $f;\n if ( ($i == 1) || ($p < $np) ) {\n $p = $np;\n }\n}\nprint $p;"}], "negative_code": [], "src_uid": "1bb5b64657e16fb518d49d3c799d4823"} {"nl": {"description": "There are some ambiguities when one writes Berland names with the letters of the Latin alphabet.For example, the Berland sound u can be written in the Latin alphabet as \"u\", and can be written as \"oo\". For this reason, two words \"ulyana\" and \"oolyana\" denote the same name.The second ambiguity is about the Berland sound h: one can use both \"h\" and \"kh\" to write it. For example, the words \"mihail\" and \"mikhail\" denote the same name.There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account?Formally, we assume that two words denote the same name, if using the replacements \"u\"\u00a0\u00a0\"oo\" and \"h\"\u00a0\u00a0\"kh\", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements.For example, the following pairs of words denote the same name: \"koouper\" and \"kuooper\". Making the replacements described above, you can make both words to be equal: \"koouper\" \"kuuper\" and \"kuooper\" \"kuuper\". \"khun\" and \"kkkhoon\". With the replacements described above you can make both words to be equal: \"khun\" \"khoon\" and \"kkkhoon\" \"kkhoon\" \"khoon\". For a given list of words, find the minimal number of groups where the words in each group denote the same name.", "input_spec": "The first line contains integer number n (2\u2009\u2264\u2009n\u2009\u2264\u2009400) \u2014 number of the words in the list. The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.", "output_spec": "Print the minimal number of groups where the words in each group denote the same name.", "sample_inputs": ["10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon", "9\nhariton\nhkariton\nbuoi\nkkkhariton\nboooi\nbui\nkhariton\nboui\nboi", "2\nalex\nalex"], "sample_outputs": ["4", "5", "1"], "notes": "NoteThere are four groups of words in the first example. Words in each group denote same name: \"mihail\", \"mikhail\" \"oolyana\", \"ulyana\" \"kooooper\", \"koouper\" \"hoon\", \"khun\", \"kkkhoon\" There are five groups of words in the second example. Words in each group denote same name: \"hariton\", \"kkkhariton\", \"khariton\" \"hkariton\" \"buoi\", \"boooi\", \"boui\" \"bui\" \"boi\" In the third example the words are equal, so they denote the same name."}, "positive_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n @_ = map ~~<>, 1 .. $_;\n chomp @_;\n my %h;\n s/u/oo/g, s/k+h/h/g for @_;\n $h{ $_ } ++ for @_;\n# print join ' ', keys %h;\n print 0 + keys %h;\n }\n"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n @_ = map ~~<>, 1 .. $_;\n chomp @_;\n my %h;\n s/oo/u/g, s/ou/uo/g, s/k+h/h/g for @_;\n $h{ $_ } ++ for @_;\n# print join ' ', keys %h;\n print 0 + keys %h;\n }"}], "src_uid": "63430b7b85e67cc3539b63c84ee7c57f"} {"nl": {"description": "You are given the strings $$$a$$$ and $$$b$$$, consisting of lowercase Latin letters. You can do any number of the following operations in any order: if $$$|a| > 0$$$ (the length of the string $$$a$$$ is greater than zero), delete the first character of the string $$$a$$$, that is, replace $$$a$$$ with $$$a_2 a_3 \\ldots a_n$$$; if $$$|a| > 0$$$, delete the last character of the string $$$a$$$, that is, replace $$$a$$$ with $$$a_1 a_2 \\ldots a_{n-1}$$$; if $$$|b| > 0$$$ (the length of the string $$$b$$$ is greater than zero), delete the first character of the string $$$b$$$, that is, replace $$$b$$$ with $$$b_2 b_3 \\ldots b_n$$$; if $$$|b| > 0$$$, delete the last character of the string $$$b$$$, that is, replace $$$b$$$ with $$$b_1 b_2 \\ldots b_{n-1}$$$. Note that after each of the operations, the string $$$a$$$ or $$$b$$$ may become empty.For example, if $$$a=$$$\"hello\" and $$$b=$$$\"icpc\", then you can apply the following sequence of operations: delete the first character of the string $$$a$$$ $$$\\Rightarrow$$$ $$$a=$$$\"ello\" and $$$b=$$$\"icpc\"; delete the first character of the string $$$b$$$ $$$\\Rightarrow$$$ $$$a=$$$\"ello\" and $$$b=$$$\"cpc\"; delete the first character of the string $$$b$$$ $$$\\Rightarrow$$$ $$$a=$$$\"ello\" and $$$b=$$$\"pc\"; delete the last character of the string $$$a$$$ $$$\\Rightarrow$$$ $$$a=$$$\"ell\" and $$$b=$$$\"pc\"; delete the last character of the string $$$b$$$ $$$\\Rightarrow$$$ $$$a=$$$\"ell\" and $$$b=$$$\"p\". For the given strings $$$a$$$ and $$$b$$$, find the minimum number of operations for which you can make the strings $$$a$$$ and $$$b$$$ equal. Note that empty strings are also equal.", "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 the string $$$a$$$ ($$$1 \\le |a| \\le 20$$$), consisting of lowercase Latin letters. The second line of each test case contains the string $$$b$$$ ($$$1 \\le |b| \\le 20$$$), consisting of lowercase Latin letters.", "output_spec": "For each test case, output the minimum number of operations that can make the strings $$$a$$$ and $$$b$$$ equal.", "sample_inputs": ["5\na\na\nabcd\nbc\nhello\ncodeforces\nhello\nhelo\ndhjakjsnasjhfksafasd\nadjsnasjhfksvdafdser"], "sample_outputs": ["0\n2\n13\n3\n20"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\n\r\nmy ($t) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\nwhile( $t -- > 0 ){\r\n ( my $a = ) =~ s/[\\x00-\\x20]+$//ios;\r\n ( my $b = ) =~ s/[\\x00-\\x20]+$//ios;\r\n \r\n my %a = ();\r\n my %b = ();\r\n my %k = ();\r\n for(my $i=0;$i;\r\n\r\nwhile(<>){\r\n\t$_ .= <>;\r\n\t\r\n\tmy $max_len = 0;\r\n\t\r\n\tm/\r\n\t\t(.*) .*\r\n\t\t\\n\r\n\t\t.* \\1\r\n\t\t(?{ \r\n\t\t\t$max_len < length $1 and $max_len = length $1;\r\n\t\t\t})\r\n\t\t(*FAIL)\r\n\t\t/x;\r\n\t\r\n\tprint +( length ) - 2 - $max_len * 2;\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$_ .= <>;\n\t\n\tmy %h;\n\t\n\tm/\n\t\t(.*) .*\n\t\t\\n\n\t\t.* \\1\n\t\t(?{ $h{ $1 } ++; })\n\t\t(*FAIL)\n\t\t/x;\n\t\n\tmy( $L ) = sort { $b <=> $a } map length, keys %h;\n\t\n\tprint +( length ) - ( $L + 1 ) * 2;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\n\r\nmy ($t) = map { $_ - 0 } split(/\\s+/o,);\r\n\r\nwhile( $t -- > 0 ){\r\n ( my $a = ) =~ s/[\\x00-\\x20]+$//ios;\r\n ( my $b = ) =~ s/[\\x00-\\x20]+$//ios;\r\n \r\n my %a = ();\r\n my %b = ();\r\n my %k = ();\r\n for(my $i=0;$i){\n\t\n\tmy %h;\n\t\n\tfor my $i (2 .. $_){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\t$h{ $u }{ v }{ $v } ++;\n\t\t$h{ $v }{ v }{ $u } ++;\n\t\t}\n\t\n\tmy $i = 0;\n\tmap { $i ++; $h{ $i }{ c } = $_ } split ' ', <>;\n\t\n\tmy @s = grep keys %{ $h{ $_ }{ v } } == 1, keys %h;\n\t\n\tmy $j;\n\t\n\twhile( @s ){ \n\t\t$j = shift @s;\n\t\tmy( $key ) = keys %{ $h{ $j }{ v } };\n\t\tif( $h{ $j }{ c } != $h{ $key }{ c } ){\n\t\t\tnext;\n\t\t\t}\n\t\t\n\t\tdelete $h{ $key }{ v }{ $j };\n\t\tkeys %{ $h{ $key }{ v } } == 1 and unshift @s, $key;\n\t\tdelete $h{ $j }{ v };\n\t\t}\n\t\t\n\tmy $g = 0;\n\t\n\tfor (keys %h){\n\t\tkeys %{ $h{ $_ }{ v } } > 1 and ++ $g and $j = $_;\n\t\t}\n\t\t\n\tprint $g > 1 ? \"NO\" : \"YES\\n$j\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t\n\tmy %h;\n\t\n\tfor my $i (2 .. $_){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\t$h{ $u }{ v }{ $v } ++;\n\t\t$h{ $v }{ v }{ $u } ++;\n\t\t}\n\t\n\tmy $i = 0;\n\tmap { $i ++; $h{ $i }{ c } = $_ } split ' ', <>;\n\t\n\tmy @sorted = grep { keys %{ $h{ $_ }{ v } } == 1 } \n\t\tsort { keys %{ $h{ $a }{ v } } <=> keys %{ $h{ $b }{ v } } } keys %h;\n\t$debug and print \"@sorted\";\n\t\n\tmy $i;\n\t\n\twhile( @sorted ){ \n\t\t$i = shift @sorted;\n\t\tmy( $key ) = keys %{ $h{ $i }{ v } };\n\t\tif( $h{ $i }{ c } != $h{ $key }{ c } ){\n\t\t\tnext;\n\t\t\t}\n\t\t\n\t\tdelete $h{ $key }{ v }{ $i };\n\t\tkeys %{ $h{ $key }{ v } } == 1 and unshift @sorted, $key;\n\t\tdelete $h{ $i }{ v };\n\t\t}\n\t\n\t$debug and print \"keys: \", map { ~~ keys %{ $h{ $_ }{ v } } } keys %h;\n\tmy @g = grep $_ > 1, map { ~~ keys %{ $h{ $_ }{ v } } } keys %h;\n\t$debug and print \"[@sorted]\";\n\t\n\tif( @g > 1 ){\n\t\tprint \"NO\";\n\t\t}\n\telsif( @g == 0 ){\n\t\tprint \"YES\";\n\t\tprint $i;\n\t\t}\n\telse{\n\t\tprint \"YES\";\n\t\tmy $j;\n\t\tfor my $k (keys %h){\n\t\t\tkeys %{ $h{ $k }{ v } } > 1 and $j = $k;\n\t\t\t}\n\t\tprint $j;\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\t\n\tmy %h;\n\t\n\tfor my $i (2 .. $_){\n\t\tmy( $u, $v ) = split ' ', <>;\n\t\t\n\t\t$h{ $u }{ v }{ $v } ++;\n\t\t$h{ $v }{ v }{ $u } ++;\n\t\t}\n\t\n\tmy $i = 0;\n\tmap { $i ++; $h{ $i }{ c } = $_ } split ' ', <>;\n\t\n\tmy @sorted = grep { keys %{ $h{ $_ }{ v } } == 1 } \n\t\tsort { keys %{ $h{ $a }{ v } } <=> keys %{ $h{ $b }{ v } } } keys %h;\n\t$debug and print \"@sorted\";\n\t\n\twhile( @sorted ){ \n\t\tmy $i = shift @sorted;\n\t\tmy( $key ) = keys %{ $h{ $i }{ v } };\n\t\tif( $h{ $i }{ c } != $h{ $key }{ c } ){\n\t\t\tnext;\n\t\t\t}\n\t\t\n\t\tdelete $h{ $key }{ v }{ $i };\n\t\tkeys %{ $h{ $key }{ v } } == 1 and unshift @sorted, $key;\n\t\tdelete $h{ $i }{ v };\n\t\t}\n\t\n\t$debug and print \"keys: \", map { ~~ keys %{ $h{ $_ }{ v } } } keys %h;\n\tmy @g = grep $_ > 1, map { ~~ keys %{ $h{ $_ }{ v } } } keys %h;\n\t$debug and print \"[@sorted]\";\n\t\n\tif( @g > 1 ){\n\t\tprint \"NO\";\n\t\t}\n\telsif( @g == 0 ){\n\t\tprint \"YES\";\n\t\tprint 1;\n\t\t}\n\telse{\n\t\tprint \"YES\";\n\t\tmy $j;\n\t\tfor my $k (keys %h){\n\t\t\tkeys %{ $h{ $k }{ v } } > 1 and $j = $k;\n\t\t\t}\n\t\tprint $j;\n\t\t}\n\t}"}], "src_uid": "aaca5d07795a42ecab210327c1cf6be9"} {"nl": {"description": "Hands that shed innocent blood!There are n guilty people in a line, the i-th of them holds a claw with length Li. The bell rings and every person kills some of people in front of him. All people kill others at the same time. Namely, the i-th person kills the j-th person if and only if j\u2009<\u2009i and j\u2009\u2265\u2009i\u2009-\u2009Li.You are given lengths of the claws. You need to find the total number of alive people after the bell rings.", "input_spec": "The first line contains one integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009106) \u2014 the number of guilty people. Second line contains n space-separated integers L1,\u2009L2,\u2009...,\u2009Ln (0\u2009\u2264\u2009Li\u2009\u2264\u2009109), where Li is the length of the i-th person's claw.", "output_spec": "Print one integer \u2014 the total number of alive people after the bell rings.", "sample_inputs": ["4\n0 1 0 10", "2\n0 0", "10\n1 1 3 0 0 0 2 1 0 3"], "sample_outputs": ["1", "2", "3"], "notes": "NoteIn first sample the last person kills everyone in front of him."}, "positive_code": [{"source_code": "$n = ;\n@l = split ' ', ;\n$ans = 0;\n$cmp = $n;\nfor ($i = $n - 1; $i >= 0; $i--) {\n if ($i < $cmp) {\n $ans++;\n }\n if ($i - @l[$i] < $cmp) {\n $cmp = $i - @l[$i];\n }\n}\nprint($ans);"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $knife = 0;\n\tmy $alive = 0;\n\t\n\tfor my $i ( reverse 0 .. @_ - 1 ){\n\t\t$knife --;\n\t\t$knife < 0 and $alive ++;\n\t\t$_[ $i ] > $knife and $knife = $_[ $i ];\n\t\t}\n\t\n\tprint $alive;\n\t}"}], "negative_code": [], "src_uid": "beaeeb8757232b141d510547d73ade04"} {"nl": {"description": "There is a robot staying at $$$X=0$$$ on the $$$Ox$$$ axis. He has to walk to $$$X=n$$$. You are controlling this robot and controlling how he goes. The robot has a battery and an accumulator with a solar panel.The $$$i$$$-th segment of the path (from $$$X=i-1$$$ to $$$X=i$$$) can be exposed to sunlight or not. The array $$$s$$$ denotes which segments are exposed to sunlight: if segment $$$i$$$ is exposed, then $$$s_i = 1$$$, otherwise $$$s_i = 0$$$.The robot has one battery of capacity $$$b$$$ and one accumulator of capacity $$$a$$$. For each segment, you should choose which type of energy storage robot will use to go to the next point (it can be either battery or accumulator). If the robot goes using the battery, the current charge of the battery is decreased by one (the robot can't use the battery if its charge is zero). And if the robot goes using the accumulator, the current charge of the accumulator is decreased by one (and the robot also can't use the accumulator if its charge is zero).If the current segment is exposed to sunlight and the robot goes through it using the battery, the charge of the accumulator increases by one (of course, its charge can't become higher than it's maximum capacity).If accumulator is used to pass some segment, its charge decreases by 1 no matter if the segment is exposed or not.You understand that it is not always possible to walk to $$$X=n$$$. You want your robot to go as far as possible. Find the maximum number of segments of distance the robot can pass if you control him optimally.", "input_spec": "The first line of the input contains three integers $$$n, b, a$$$ ($$$1 \\le n, b, a \\le 2 \\cdot 10^5$$$) \u2014 the robot's destination point, the battery capacity and the accumulator capacity, respectively. The second line of the input contains $$$n$$$ integers $$$s_1, s_2, \\dots, s_n$$$ ($$$0 \\le s_i \\le 1$$$), where $$$s_i$$$ is $$$1$$$ if the $$$i$$$-th segment of distance is exposed to sunlight, and $$$0$$$ otherwise.", "output_spec": "Print one integer \u2014 the maximum number of segments the robot can pass if you control him optimally.", "sample_inputs": ["5 2 1\n0 1 0 1 0", "6 2 1\n1 0 0 1 0 1"], "sample_outputs": ["5", "3"], "notes": "NoteIn the first example the robot can go through the first segment using the accumulator, and charge levels become $$$b=2$$$ and $$$a=0$$$. The second segment can be passed using the battery, and charge levels become $$$b=1$$$ and $$$a=1$$$. The third segment can be passed using the accumulator, and charge levels become $$$b=1$$$ and $$$a=0$$$. The fourth segment can be passed using the battery, and charge levels become $$$b=0$$$ and $$$a=1$$$. And the fifth segment can be passed using the accumulator.In the second example the robot can go through the maximum number of segments using battery two times and accumulator one time in any order."}, "positive_code": [{"source_code": "#use v5.24;\nuse warnings;\nmy $line=;\nchomp($line);\nmy($n,$b,$amax)=split(/ /,$line);\n$line=;\nchomp($line);\nmy @v=split(/ /,$line);\nmy $a=$amax,$i=0;\nBUCLA: while (($b>0 || $a>0) && $i<$n){\n#\tprint $b.' '.$a.\"\\n\";\n\tmy $tip=$v[$i];\n\tif ($tip==0){\n\t\tif ($a>0) {$a--;}\n\t\telse {$b--;}\n\t\t$i++;\n\t}\n\telse{\n\t\tif ($a==0) {$b--;$a++;$i++;next BUCLA;}\n\t\tif ($b==0) {$a--;$i++;next BUCLA;}\n\t\tif ($a<$amax) {$b--;$a++;$i++;next BUCLA;}\n\t\telse {$a--;$i++;next BUCLA;}\n\t}\n}\nprint $i;\n#;\nexit(0);"}], "negative_code": [], "src_uid": "75ef1f52ef3a86992159eef566dddc89"} {"nl": {"description": "Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i\u2009-\u20091 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n\u2009=\u20094, the array is changing as follows: You have to write a program that allows you to determine what number will be in the cell with index x (1\u2009\u2264\u2009x\u2009\u2264\u2009n) after Dima's algorithm finishes.", "input_spec": "The first line contains two integers n and q (1\u2009\u2264\u2009n\u2009\u2264\u20091018, 1\u2009\u2264\u2009q\u2009\u2264\u2009200\u2009000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1\u2009\u2264\u2009xi\u2009\u2264\u2009n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.", "output_spec": "For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.", "sample_inputs": ["4 3\n2\n3\n4", "13 4\n10\n5\n4\n8"], "sample_outputs": ["3\n2\n4", "13\n3\n8\n9"], "notes": "NoteThe first example is shown in the picture.In the second example the final array is [1,\u200912,\u20092,\u20098,\u20093,\u200911,\u20094,\u20099,\u20095,\u200913,\u20096,\u200910,\u20097]."}, "positive_code": [{"source_code": "($n, $q) = split \" \", ;\nfor (1..$q) {\n\t$x = ;\n\t$a = 2 * $n - $x;\n\t$a >>= 1 until $a & 1;\n\t$a = $n - ($a >> 1);\n\tprint $a, \"\\n\";\n}\t\n"}], "negative_code": [], "src_uid": "756866daf45951854d5400b209af3bb0"} {"nl": {"description": "There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i\u2009+\u20091)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i\u2009-\u20091)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,\u2009t by looking at the footprints.", "input_spec": "The first line of the input contains integer n (3\u2009\u2264\u2009n\u2009\u2264\u20091000). The second line contains the description of the road \u2014 the string that consists of n characters. Each character will be either \".\" (a block without footprint), or \"L\" (a block with a left footprint), \"R\" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to \".\". Also, the first and the last character will always be \".\". It's guaranteed that a solution exists.", "output_spec": "Print two space-separated integers \u2014 the values of s and t. If there are several possible solutions you can print any of them.", "sample_inputs": ["9\n..RRLL...", "11\n.RRRLLLLL.."], "sample_outputs": ["3 4", "7 5"], "notes": "NoteThe first test sample is the one in the picture."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n# A\n\nwhile(<>){\n $i=$j=0;\n $_=<>;\n while (s/^\\W//){$i++};\n # print;\n while (s/^[R]//){$j++};\n print $i+1,\" \",(/^\\./?(++$i):($i))+$j; #($j?($j):(++$j));\n print \"\\n\";\n \n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n# A\n\nwhile(<>){\n $i=$j=0;\n $_=<>;\n while (s/^\\W//){$i++};\n # print;\n while (s/^[R]//){$j++};\n print $i+1,\" \",$i+$j;\n print \"\\n\";\n \n}"}, {"source_code": "#!/usr/bin/perl\n# A\n\nwhile(<>){\n $i=$j=0;\n $_=<>;\n while (s/^\\W//){$i++};\n # print;\n while (s/^[R]//){$j++};\n print $i+1,\" \",$i+($j?($j):(++$j));\n print \"\\n\";\n \n}"}, {"source_code": "#!/usr/bin/perl\n# A\n\nwhile(<>){\n $i=$j=0;\n $_=<>;\n while (s/^\\W//){$i++};\n # print;\n while (s/^[R]//){$j++};\n print $i+1,\" \",(/^\\./?(++$i):($i))+($j?($j):(++$j));\n print \"\\n\";\n \n}"}], "src_uid": "3053cba2426ebd113fcd70a9b026dad0"} {"nl": {"description": "Nastia has received an array of $$$n$$$ positive integers as a gift.She calls such an array $$$a$$$ good that for all $$$i$$$ ($$$2 \\le i \\le n$$$) takes place $$$gcd(a_{i - 1}, a_{i}) = 1$$$, where $$$gcd(u, v)$$$ denotes the greatest common divisor (GCD) of integers $$$u$$$ and $$$v$$$.You can perform the operation: select two different indices $$$i, j$$$ ($$$1 \\le i, j \\le n$$$, $$$i \\neq j$$$) and two integers $$$x, y$$$ ($$$1 \\le x, y \\le 2 \\cdot 10^9$$$) so that $$$\\min{(a_i, a_j)} = \\min{(x, y)}$$$. Then change $$$a_i$$$ to $$$x$$$ and $$$a_j$$$ to $$$y$$$.The girl asks you to make the array good using at most $$$n$$$ operations.It can be proven that this is always possible.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10\\,000$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$)\u00a0\u2014 the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_{n}$$$ ($$$1 \\le a_i \\le 10^9$$$)\u00a0\u2014 the array which Nastia has received as a gift. It's guaranteed that the sum of $$$n$$$ in one test doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each of $$$t$$$ test cases print a single integer $$$k$$$ ($$$0 \\le k \\le n$$$)\u00a0\u2014 the number of operations. You don't need to minimize this number. In each of the next $$$k$$$ lines print $$$4$$$ integers $$$i$$$, $$$j$$$, $$$x$$$, $$$y$$$ ($$$1 \\le i \\neq j \\le n$$$, $$$1 \\le x, y \\le 2 \\cdot 10^9$$$) so that $$$\\min{(a_i, a_j)} = \\min{(x, y)}$$$\u00a0\u2014 in this manner you replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. If there are multiple answers, print any.", "sample_inputs": ["2\n5\n9 6 3 11 15\n3\n7 5 13"], "sample_outputs": ["2\n1 5 11 9\n2 5 7 6\n0"], "notes": "NoteConsider the first test case.Initially $$$a = [9, 6, 3, 11, 15]$$$.In the first operation replace $$$a_1$$$ with $$$11$$$ and $$$a_5$$$ with $$$9$$$. It's valid, because $$$\\min{(a_1, a_5)} = \\min{(11, 9)} = 9$$$.After this $$$a = [11, 6, 3, 11, 9]$$$.In the second operation replace $$$a_2$$$ with $$$7$$$ and $$$a_5$$$ with $$$6$$$. It's valid, because $$$\\min{(a_2, a_5)} = \\min{(7, 6)} = 6$$$.After this $$$a = [11, 7, 3, 11, 6]$$$\u00a0\u2014 a good array.In the second test case, the initial array is already good."}, "positive_code": [{"source_code": "$, = \" \";\r\nchomp($t = );\r\nforeach (1..$t)\r\n{\r\n chomp($n = );\r\n chomp($inp = );\r\n @a = split / /, $inp;\r\n ($mn, $p) = (1E9 + 7, -1);\r\n $n -= 1;\r\n foreach (0..$n)\r\n {\r\n if ($a[$_] < $mn)\r\n {\r\n $mn = $a[$_];\r\n $p = $_;\r\n }\r\n }\r\n print $n, \"\\n\";\r\n foreach (0..$n)\r\n {\r\n next if $_ == $p;\r\n print $p + 1, $_ + 1, $mn, $mn + abs($_ - $p), \"\\n\";\r\n }\r\n}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nmy $vv = 10**9 + 7;\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my @A = map { $_ - 0 } split(/\\s+/,);\r\n my $n2 = int($n/2);\r\n my @ot = ();\r\n for(my $i=0;$i<$n2;$i++){\r\n my $mn = &min($A[2*$i],$A[1+2*$i]);\r\n push(@ot,+[1+2*$i,2+2*$i,$mn,$vv]);\r\n }\r\n print \"$n2\\n\";\r\n for(my $i=0;$i<$n2;$i++){\r\n my $o = $ot[$i];\r\n print sprintf(\"%d %d %d %d\\n\",$o->[0],$o->[1],$o->[2],$o->[3]);\r\n }\r\n}\r\n\r\nexit(0);\r\n\r\nsub min {\r\n my ($x,$y) = @_;\r\n return ( $x > $y ? $y : $x );\r\n}\r\n\r\n"}], "negative_code": [{"source_code": "$, = \" \";\r\nchomp($t = );\r\nforeach (1..$t)\r\n{\r\n chomp($n = );\r\n chomp($inp = );\r\n @a = split / /, $inp;\r\n ($mn, $p) = (1E9 + 7, -1);\r\n $n -= 1;\r\n foreach (0..$n)\r\n {\r\n if ($a[$_] < $mn)\r\n {\r\n $mn = $a[$_];\r\n $p = $_;\r\n }\r\n }\r\n print $n, \"\\n\";\r\n foreach (0..$n)\r\n {\r\n next if $_ == $p;\r\n print $p + 1, $_ + 1, $mn, $mn + abs($i - $p), \"\\n\";\r\n }\r\n}"}, {"source_code": "$, = \" \";\r\nchomp($t = );\r\nforeach (1..$t)\r\n{\r\n chomp($n = );\r\n chomp($inp = );\r\n @a = split / /, $inp;\r\n ($mn, $p) = (1E9 + 7, -1);\r\n $n -= 1;\r\n foreach (0..$n)\r\n {\r\n if ($a[$_] < $mn)\r\n {\r\n $mn = $a[$_];\r\n $p = $_;\r\n }\r\n }\r\n print $n - 1, \"\\n\";\r\n foreach (0..$n)\r\n {\r\n next if $_ == $p;\r\n print $p + 1, $_ + 1, $mn, $mn + abs($i - $p), \"\\n\";\r\n }\r\n}"}], "src_uid": "8b2b7208630af1d086420513a437a914"} {"nl": {"description": "This is an interactive problem.Alice and Bob are playing a game. There is $$$n\\times n$$$ grid, initially empty. We refer to the cell in row $$$i$$$ and column $$$j$$$ by $$$(i, j)$$$ for $$$1\\le i, j\\le n$$$. There is an infinite supply of tokens that come in $$$3$$$ colors labelled $$$1$$$, $$$2$$$, and $$$3$$$.The game proceeds with turns as follows. Each turn begins with Alice naming one of the three colors, let's call it $$$a$$$. Then, Bob chooses a color $$$b\\ne a$$$, chooses an empty cell, and places a token of color $$$b$$$ on that cell.We say that there is a conflict if there exist two adjacent cells containing tokens of the same color. Two cells are considered adjacent if they share a common edge.If at any moment there is a conflict, Alice wins. Otherwise, if $$$n^2$$$ turns are completed (so that the grid becomes full) without any conflicts, Bob wins.We have a proof that Bob has a winning strategy. Play the game as Bob and win.The interactor is adaptive. That is, Alice's color choices can depend on Bob's previous moves.", "input_spec": null, "output_spec": null, "sample_inputs": ["2\n1\n\n2\n\n1\n\n3"], "sample_outputs": ["2 1 1\n\n3 1 2\n\n3 2 1\n\n1 2 2"], "notes": "NoteThe final grid from the sample is pictured below. Bob wins because there are no two adjacent cells with tokens of the same color. $$$$$$\\begin{matrix}2&3\\\\3&1\\end{matrix}$$$$$$The sample is only given to demonstrate the input and output format. It is not guaranteed to represent an optimal strategy for Bob or the real behavior of the interactor."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\n$| = 1;\r\n\r\nmy ($n) = map { $_ - 0 } split(/\\s+/,);\r\nmy $n2 = $n * $n;\r\n\r\nmy $x; my $y;\r\n\r\nif( $n % 2 == 1 ){\r\n $x = (($n - 1)*($n - 1))/2 + $n;\r\n $y = $n2 - $x;\r\n} else {\r\n $x = $n2 / 2;\r\n $y = $n2 / 2;\r\n}\r\n\r\nmy $p1 = +[0,0];\r\nmy $p2 = +[0,1];\r\nmy $c1 = '';\r\nmy $c2 = '';\r\nmy $t1 = 0;\r\nmy $t2 = 0;\r\n\r\nif( $n==1 ){\r\n my $al = &read_al;\r\n print sprintf(\"%d 1 1\\n\",($al % 3) + 1);\r\n exit(0);\r\n}\r\n\r\nfor(my $i=0;$i<$n2;$i++){\r\n my $al = &read_al;\r\n if( $i == 0 ){\r\n my $v = ($al % 3) + 1;\r\n $c1 = $v;\r\n $t1++;\r\n print sprintf(\"%d %d %d\\n\",$c1,1+$p1->[0],1+$p1->[1]);\r\n &nx($p1);\r\n next;\r\n }\r\n if( $i == 1 ){\r\n if( $al == $c1 ){\r\n $c2 = ($al % 3) + 1;\r\n } else {\r\n $c2 = &ex($al,$c1);\r\n }\r\n $t2++;\r\n print sprintf(\"%d %d %d\\n\",$c2,1+$p2->[0],1+$p2->[1]);\r\n &nx($p2);\r\n next;\r\n }\r\n \r\n if( $t1 >= $x ){\r\n my $v = &ex($al,$c1);\r\n $t2++;\r\n print sprintf(\"%d %d %d\\n\",$v,1+$p2->[0],1+$p2->[1]);\r\n &nx($p2);\r\n next;\r\n }\r\n if( $t2 >= $y ){\r\n my $v = &ex($al,$c2);\r\n $t1++;\r\n print sprintf(\"%d %d %d\\n\",$v,1+$p1->[0],1+$p1->[1]);\r\n &nx($p1);\r\n next;\r\n }\r\n \r\n if( $al != $c1 ){\r\n $t1++;\r\n print sprintf(\"%d %d %d\\n\",$c1,1+$p1->[0],1+$p1->[1]);\r\n &nx($p1);\r\n next;\r\n }\r\n if( $al != $c2 ){\r\n $t2++;\r\n print sprintf(\"%d %d %d\\n\",$c2,1+$p2->[0],1+$p2->[1]);\r\n &nx($p2);\r\n next;\r\n }\r\n}\r\n\r\nexit(0);\r\n\r\nsub read_al {\r\n my $al = -1;\r\n while( $al<0 ){\r\n ( my $v = ) =~ s/[\\x00-\\x20]+$//ios;\r\n if( $v =~ /\\d/o ){ $al = ($&) - 0; }\r\n }\r\n return $al;\r\n}\r\n\r\nsub ex {\r\n my ($x,$y) = @_;\r\n my $v;\r\n $v = 1 if $x!=1 and $y!=1;\r\n $v = 2 if $x!=2 and $y!=2;\r\n $v = 3 if $x!=3 and $y!=3;\r\n return $v;\r\n}\r\n\r\nsub nx {\r\n my $p = shift;\r\n my $pa = ($p->[0] + $p->[1]) % 2;\r\n $p->[1] += 2;\r\n if( $p->[1] >= $n ){\r\n $p->[0] ++;\r\n $p->[1] = 0;\r\n $p->[1]++ if $pa != ( ($p->[0] + $p->[1]) % 2 );\r\n }\r\n}\r\n\r\n"}], "negative_code": [], "src_uid": "7c721cdb8e5709bba242957344851d48"} {"nl": {"description": "You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character \"0\" and \"1\") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of \"1\"s in the string, and 0 otherwise.", "input_spec": "The first line contains the string a and the second line contains the string b (1\u2009\u2264\u2009|a|,\u2009|b|\u2009\u2264\u20091000). Both strings contain only the characters \"0\" and \"1\". Here |x| denotes the length of the string x.", "output_spec": "Print \"YES\" (without quotes) if it is possible to turn a into b, and \"NO\" (without quotes) otherwise.", "sample_inputs": ["01011\n0110", "0011\n1110"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first sample, the steps are as follows: 01011\u2009\u2192\u20091011\u2009\u2192\u2009011\u2009\u2192\u20090110"}, "positive_code": [{"source_code": "for($f,$s){$_=grep$_,(split'|',<>)}print$s-1>$f-$f%2?NO:YES\n"}, {"source_code": "for($f,$s){$_=grep$_,(split'|',<>)}print$s-1>$f-$f%2?NO:YES\n"}, {"source_code": "for($f,$s){$_=grep$_,(split'|',<>)}print$s-1>$f-$f%2?NO:YES\n"}, {"source_code": "for($f,$s){$_=grep$_,(split'|',<>)}print$s-1>$f-$f%2?NO:YES\n"}, {"source_code": "for($f,$s){$_=grep$_,(split'|',<>)}print$s-1>$f-$f%2?NO:YES\n"}, {"source_code": "for($f,$s){$_=grep$_,(split'|',<>)}print$s-1>$f-$f%2?NO:YES\n"}, {"source_code": "for($f,$s){$_=grep$_,(split'|',<>)}print$s-1>$f-$f%2?NO:YES\n"}, {"source_code": "for($f,$s){$_=grep$_,(split'|',<>)}print$s-1>$f-$f%2?NO:YES\n"}, {"source_code": "for($f,$s){$_=grep$_,(split'|',<>)}print$s-1>$f-$f%2?NO:YES"}, {"source_code": "@a=split'|',<>;@b=split'|',<>;$f+=$_ for(@a);$s+=$_ for(@b);print\"NO\"and exit if($f%2&&$s>$f+1)or(!($f%2)&&$s>$f);print \"YES\"\n"}, {"source_code": "$f+=$_ for split'|',<>;$s+=$_ for split'|',<>;print((($f%2&&$s>$f+1)or(!($f%2)&&$s>$f))?NO:YES)"}], "negative_code": [{"source_code": "$f+=$_ for split'|',<>;$s+=$_ for split'|',<>;print(($f%2&&$s>$f+1)or(!($f%2)&&$s>$f)?NO:YES)\n"}], "src_uid": "cf86add6c92fa8a72b8e23efbdb38613"} {"nl": {"description": "There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?", "input_spec": "The first line contains one integer $$$n$$$ ($$$3 \\le n \\le 2 \\cdot 10^5$$$) \u2014 the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \\le a_i \\le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.", "output_spec": "Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).", "sample_inputs": ["4\n1 3 4 2", "3\n3 1 2"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy $max = ( sort { $b <=> $a } @_ )[ 0 ];\n\t\n\tmy $idx;\n\t\n\tfor my $i ( 0 .. @_ - 1 ){\n\t\t$max == $_[ $i ] and $idx = $i;\n\t\t}\n\t\n\tmy @up;\n\t\n\tpush @up, [ @_[ 0 .. $idx ] ];\n\tpush @up, [ reverse @_[ $idx .. @_ - 1 ] ];\n\t\t\n\tmy $ok = 1;\n\t\n\tfor my $ref ( @up ){\n\t\tmy @arr = @{ $ref };\n\t\tfor my $i ( 0 .. @arr - 2 ){\n\t\t\t$ok &&= $arr[ $i ] < $arr[ $i + 1 ];\n\t\t\t}\n\t\t}\n\t\n\tprint $ok ? \"YES\" : \"NO\";\n\t}"}], "negative_code": [], "src_uid": "12abfbe7118868a0b1237489b5c92760"} {"nl": {"description": "Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wants to get maximum possible even (divisible by 2) sum. Please, calculate this value for Wet Shark. Note, that if Wet Shark uses no integers from the n integers, the sum is an even integer 0.", "input_spec": "The first line of the input contains one integer, n (1\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000). The next line contains n space separated integers given to Wet Shark. Each of these integers is in range from 1 to 109, inclusive. ", "output_spec": "Print the maximum possible even sum that can be obtained if we use some of the given integers. ", "sample_inputs": ["3\n1 2 3", "5\n999999999 999999999 999999999 999999999 999999999"], "sample_outputs": ["6", "3999999996"], "notes": "NoteIn the first sample, we can simply take all three integers for a total sum of 6.In the second sample Wet Shark should take any four out of five integers 999\u2009999\u2009999."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nwhile (<>) {\n my @nums = split(/ /, <>);\n my @odds;\n my $sum = 0;\n\n $_ & 1 ? push @odds, $_ : ($sum += $_) for @nums;\n\n if (@odds & 1) {\n @odds = sort {$a <=> $b} @odds;\n $sum += $_ for @odds[1..$#odds];\n } else {\n $sum += $_ for @odds;\n }\n \n print \"$sum\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nwhile (<>) {\n my @nums = split(/ /, <>);\n my @odds;\n my $sum = 0;\n\n $_ & 1 ? push @odds, $_ : ($sum += $_) for @nums;\n\n if (@odds & 1) {\n @odds = sort {$a <=> $b} @odds;\n $sum += $_ for @odds[-$#odds..-1];\n } else {\n $sum += $_ for @odds;\n }\n \n print \"$sum\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nmy $n = <>;\nmy $line = <>;\nchomp($line);\n\nmy @nums = split(/ /, $line);\nmy @odds;\nmy $sum = 0;\n\n$_ & 1 ? push @odds, $_ : ($sum += $_) for @nums;\n\nif (@odds % 2 == 0) {\n $sum += $_ for @odds;\n} else {\n @odds = sort {$a <=> $b} @odds;\n shift @odds;\n $sum += $_ for @odds;\n}\n\nprint \"$sum\";\n"}, {"source_code": "#!/usr/bin/perl\n\nmy $n = <>;\nmy $line = <>;\nchomp($line);\n\nmy @nums = split(/ /, $line);\n\nmy @odds;\nmy $sum = 0;\nfor (@nums) {\n if ($_ % 2 == 0) {\n $sum += $_;\n } else {\n push @odds, $_;\n }\n}\n\nif (@odds % 2 == 0) {\n for (@odds) {\n $sum += $_;\n }\n} else {\n @odds = sort {$a <=> $b} @odds;\n shift @odds;\n for (@odds) {\n $sum += $_;\n }\n}\n\nprint \"$sum\";\n"}, {"source_code": "#!/usr/bin/perl\n\nwhile (<>) {\n my @nums = split(/ /, <>);\n my @odds;\n my $sum = 0;\n\n $_ & 1 ? push @odds, $_ : ($sum += $_) for @nums;\n\n if (@odds & 1) {\n @odds = sort {$a <=> $b} @odds;\n shift @odds;\n }\n $sum += $_ for @odds;\n \n print \"$sum\";\n}\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\t@_ = split ' ', <>;\n\t$sum = 0;\n\t@odd = ();\n\t\n\t$_ & 1 ? (push @odd, $_) : ($sum += $_) for @_;\n\t\n\t@odd = sort {$a <=> $b} @odd;\n\t@odd & 1 and shift @odd;\n\t$sum += $_ for @odd;\n\t\n\tprint $sum\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t\n\t@_ = split ' ', <>;\n\t$sum = 0;\n\t@odd = ();\n\t\n\t$_ & 1 ? (push @odd, $_) : ($sum += $_) for @_;\n\t\n\t@odd = sort {$a <=> $b} @odd;\n\t@odd & 1 and shift @odd;\n\t$sum += $_ for @odd;\n\t\n\tprint $sum\n\t}"}, {"source_code": "$n = <>;\n$n--;\n$nums = <>;\n@tab = split(' ',$nums);\n@tab = sort{$b <=> $a} @tab;\n$m = 0;\n$sum = 0;\nforeach my $num (@tab){\n\tif ($num % 2 == 1){\n\t\t$m = $num;\n\t}\n\t\n\t$sum += $num;\n}\n\nif ($sum % 2 == 1){\n\t$sum -= $m;\n}\n\nprint $sum;"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nmy $n = <>;\nmy $line = <>;\nchomp($line);\n\nmy @nums = split(/ /, $line);\n\nmy (@evens, @odds);\n\nfor (@nums) {\n if ($_ % 2 == 0) {\n push @evens, $_;\n } else {\n push @odds, $_;\n }\n}\n\n\nmy $sum = 0;\n\nfor (@evens) {\n $sum += $_;\n}\n\nif (@odds % 2 == 0) {\n for (@odds) {\n $sum += $_;\n }\n} else {\n @odds = reverse sort @odds;\n for (@odds[-$#odds..-1]) {\n $sum += $_;\n }\n}\n\nprint \"$sum\";\n"}, {"source_code": "#!/usr/bin/perl\n\nwhile (<>) {\n my @nums = split(/ /, <>);\n my @odds;\n my $sum = 0;\n\n $_ & 1 ? push @odds, $_ : ($sum += $_) for @nums;\n\n if (@odds & 1) {\n @odds = sort @odds;\n $sum += $_ for @odds[1..$#odds];\n } else {\n $sum += $_ for @odds;\n }\n \n print \"$sum\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nmy $n = <>;\nmy $line = <>;\nchomp($line);\n\nmy @nums = split(/ /, $line);\n\nmy (@evens, @odds);\n\nfor (@nums) {\n if ($_ % 2 == 0) {\n push @evens, $_;\n } else {\n push @odds, $_;\n }\n}\n\n\nmy $sum = 0;\n\nfor (@evens) {\n $sum += $_;\n}\n\nif (@odds % 2 == 0) {\n for (@odds) {\n $sum += $_;\n }\n} else {\n @odds = sort @odds;\n for (@odds[-$#odds..-1]) {\n $sum += $_;\n }\n}\n\nprint \"$sum\";\n"}], "src_uid": "adb66b7b22d70bb37cb625d531d8865f"} {"nl": {"description": "You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.The following definition of a regular bracket sequence is well-known, so you can be familiar with it.Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s1 and s2 be a RBS then the strings <s1>s2, {s1}s2, [s1]s2, (s1)s2 are also RBS.For example the string \"[[(){}]<>]\" is RBS, but the strings \"[)()\" and \"][()()\" are not.Determine the least number of replaces to make the string s RBS.", "input_spec": "The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 106.", "output_spec": "If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s.", "sample_inputs": ["[<}){}", "{()}[]", "]]"], "sample_outputs": ["2", "0", "Impossible"], "notes": null}, "positive_code": [{"source_code": "$A = '([<{';\n$B = ')]>}';\n\n$s = <>; chomp($s); $l = length($s) - 1;\n$fix = 0;\nfor (0..$l) {\n\t$c = substr($s, $_, 1);\n\t# open\n\tif (($j = index($A, $c)) != -1) {\n\t\tpush(@q, $j);\n\t\tnext;\n\t}\n\t# close\n\tif (($j = index($B, $c)) != -1) {\n\t\tif (@q == 0) {\n\t\t\t$fail = 1;\n\t\t\tlast;\n\t\t}\n\t\t$fix++ if $j != pop(@q);\n\t\tnext;\n\t}\n\t# eol\n\tlast;\n}\nif (@q || $fail) {\n\tprint \"Impossible\\n\";\n} else {\n\tprint $fix, \"\\n\";\t\n}"}], "negative_code": [{"source_code": "@A = ('(', '[', '<', '{');\n$B = ')]>}';\n\n$fix = 0;\nwhile (sysread(STDIN, $c, 1)) {\n\t# ord($c) > 13 || last;\n\tif (($j = index($B, $c)) != -1) {\n\t\t# close\n\t\tif (@q == 0) {\n\t\t\t$fail = 1;\n\t\t\tlast;\n\t\t}\n\t\t$fix++ if $A[$j] ne pop(@q);\n\t} else {\n\t\t# open\n\t\tpush(@q, $c);\n\t}\n}\nif (@q || $fail) {\n\tprint \"Impossible\\n\";\n} else {\n\tprint $fix, \"\\n\";\t\n}"}], "src_uid": "4147fef7a151c52e92c010915b12c06b"} {"nl": {"description": "The student council has a shared document file. Every day, some members of the student council write the sequence TMT (short for Towa Maji Tenshi) in it.However, one day, the members somehow entered the sequence into the document at the same time, creating a jumbled mess. Therefore, it is Suguru Doujima's task to figure out whether the document has malfunctioned. Specifically, he is given a string of length $$$n$$$ whose characters are all either T or M, and he wants to figure out if it is possible to partition it into some number of disjoint subsequences, all of which are equal to TMT. That is, each character of the string should belong to exactly one of the subsequences.A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero) characters.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) \u00a0\u2014 the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$3 \\le n < 10^5$$$), the number of characters in the string entered in the document. It is guaranteed that $$$n$$$ is divisible by $$$3$$$. The second line of each test case contains a string of length $$$n$$$ consisting of only the characters T and M. 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 line containing YES if the described partition exists, and a single line containing NO otherwise.", "sample_inputs": ["5\n3\nTMT\n3\nMTT\n6\nTMTMTT\n6\nTMTTTT\n6\nTTMMTT"], "sample_outputs": ["YES\nNO\nYES\nNO\nYES"], "notes": "NoteIn the first test case, the string itself is already a sequence equal to TMT.In the third test case, we may partition the string into the subsequences TMTMTT. Both the bolded and the non-bolded subsequences are equal to TMT."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n my $t = 0; my $m = 0;\r\n my $ng = undef;\r\n for(my $i=0;$i<$n;$i++){\r\n if( substr($s,$i,1) eq 'T' ){\r\n $t++;\r\n } else {\r\n $m++;\r\n }\r\n if( $m > $t ){\r\n $ng = 1; last;\r\n }\r\n }\r\n if( $ng ){\r\n print \"NO\\n\"; next;\r\n }\r\n unless( $t == (2*$n/3) and $m == ($n/3) ){\r\n print \"NO\\n\"; next;\r\n }\r\n $t = $m = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n if( substr($s,$n-1-$i,1) eq 'T' ){\r\n $t++;\r\n } else {\r\n $m++;\r\n }\r\n if( $m > $t ){\r\n $ng = 1; last;\r\n }\r\n }\r\n if( $ng ){\r\n print \"NO\\n\";\r\n } else {\r\n print \"YES\\n\";\r\n }\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [], "src_uid": "94df40fbf9d7e8f91d5e7fde783bb389"} {"nl": {"description": "The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn\u2009-\u20091 is the distance between the n\u2009-\u20091-th and the n-th station; dn is the distance between the n-th and the 1-st station.The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.", "input_spec": "The first line contains integer n (3\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of stations on the circle line. The second line contains n integers d1,\u2009d2,\u2009...,\u2009dn (1\u2009\u2264\u2009di\u2009\u2264\u2009100) \u2014 the distances between pairs of neighboring stations. The third line contains two integers s and t (1\u2009\u2264\u2009s,\u2009t\u2009\u2264\u2009n) \u2014 the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces.", "output_spec": "Print a single number \u2014 the length of the shortest path between stations number s and t.", "sample_inputs": ["4\n2 3 4 9\n1 3", "4\n5 8 2 100\n4 1", "3\n1 1 1\n3 1", "3\n31 41 59\n1 1"], "sample_outputs": ["5", "15", "1", "0"], "notes": "NoteIn the first sample the length of path 1\u2009\u2192\u20092\u2009\u2192\u20093 equals 5, the length of path 1\u2009\u2192\u20094\u2009\u2192\u20093 equals 13.In the second sample the length of path 4\u2009\u2192\u20091 is 100, the length of path 4\u2009\u2192\u20093\u2009\u2192\u20092\u2009\u2192\u20091 is 15.In the third sample the length of path 3\u2009\u2192\u20091 is 1, the length of path 3\u2009\u2192\u20092\u2009\u2192\u20091 is 2.In the fourth sample the numbers of stations are the same, so the shortest distance equals 0."}, "positive_code": [{"source_code": "my $n = int<>;\nmy @arr = (0, split / /, <>);\nmy ($s, $t) = split / /, <>;\n($s, $t) = ($t, $s) if ($s > $t);\nmy ($temp, $ans) = (0, 0);\n$ans += $arr[$_] for ($s..$t-1);\n$temp += $arr[$_] for (1..$s-1);\n$temp += $arr[$_] for ($t..$n);\nprint $ans < $temp ? $ans : $temp;\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = (0, split / /, <>);\n$a[$_]+=$a[$_-1] foreach(1..$n);\n($s, $t) = split / /, <>;\n$s>$t and ($s,$t)=($t,$s);\n$s==$t and say 0 and exit;\n$tmp = $a[$t-1]-$a[$s-1];\n# say \"$s $t $tmp\";\n$ans = $tmp<$a[$n]-$tmp ? $tmp:$a[$n]-$tmp;\nsay $ans;"}, {"source_code": "#!usr/bin/perl\n#use warnings;\n#use strict;\n#use autodie;\n#use diagnostics;\nuse utf8;\nuse 5.010;\nuse 5.012;\nuse 5.014;\nmy($n,$ss,@arr,$s,$t,$count,$max);\nchomp($n=);\nchomp($ss=);\n@arr=split / /,$ss;\nchomp($ss=);\n($s,$t)=split / /,$ss;\n$count=$s;\n$max=999999999;\n$ss=0;\nwhile($count ne $t)\n{\n $ss+=$arr[$count-1];\n $count=0 if $count eq $n;\n $count++;\n}\n$max=$ss if $max > $ss;\n($ss,$count)=(0,$s);\nwhile($count ne $t)\n{\n $count=$n+1 if $count eq 1;\n $count--;\n $ss+=$arr[$count-1];\n}\n$max=$ss if $max > $ss;\nsay $max;\n"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\nuse warnings;\nuse List::Util qw{sum min};\n\nmy $n = int <>;\nmy @d = map int, split /\\D/, <>;\nmy ($s, $t) = map int, split /\\D/, <>;\nif ($s > $t) {\n my $tmp = $t;\n $t = $s;\n $s = $tmp;\n}\nmy $s1 = 0;\nfor ($s-1 .. $t-2) {\n $s1 += $d[$_];\n}\nmy $s2 = (sum @d) - $s1;\nprint min ($s1, $s2);\n"}, {"source_code": "<>;\n@a = split(\" \",<>);\n($s, $t) = split(\" \",<>);\nif ($s > $t)\n {$tmp = $s; $s = $t; $t = $tmp}\n \nif ($s == $t){print 0 ;exit}\n\n#print \"$s, $t,\\n\";\n\nfor ($s..$t-1){\n $sum1 += $a[$_-1];\n \n# print \" $_: $a[$_-1]\\n\";\n}\n#print \"$sum1\\n\";\n\nfor (@a){\n $sum2 += $_\n}\n#print \"$sum2\\n\";\n\nprint $sum1 > $sum2-$sum1 ? ($sum2-$sum1):($sum1)"}, {"source_code": "my $num = <>;\nchomp $num;\nmy @dist = grep /\\w/, split ' ', <>;\nmy ($s, $t) = sort {$a <=> $b} split ' ', <>;\n\nmy $r = 0;\n$r += $dist[$_-1] for $s..$t-1;\nmy $total;\n$total += $dist[$_-1] for 1..$num;\nmy $l = $total - $r;\nprint $r < $l ? $r : $l; "}], "negative_code": [], "src_uid": "22ef37041ebbd8266f62ab932f188b31"} {"nl": {"description": "A TV show called \"Guess a number!\" is gathering popularity. The whole Berland, the old and the young, are watching the show.The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly larger than number x? Is it true that y is strictly smaller than number x? Is it true that y is larger than or equal to number x? Is it true that y is smaller than or equal to number x? On each question the host answers truthfully, \"yes\" or \"no\".Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print \"Impossible\".", "input_spec": "The first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u200910000) \u2014 the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: \"sign x answer\", where the sign is: \">\" (for the first type queries), \"<\" (for the second type queries), \">=\" (for the third type queries), \"<=\" (for the fourth type queries). All values of x are integer and meet the inequation \u2009-\u2009109\u2009\u2264\u2009x\u2009\u2264\u2009109. The answer is an English letter \"Y\" (for \"yes\") or \"N\" (for \"no\"). Consequtive elements in lines are separated by a single space.", "output_spec": "Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation \u2009-\u20092\u00b7109\u2009\u2264\u2009y\u2009\u2264\u20092\u00b7109. If there are many answers, print any of them. If such value doesn't exist, print word \"Impossible\" (without the quotes).", "sample_inputs": ["4\n>= 1 Y\n< 3 N\n<= -3 N\n> 55 N", "2\n> 100 Y\n< -100 Y"], "sample_outputs": ["17", "Impossible"], "notes": null}, "positive_code": [{"source_code": "#!/perl -w\n\nuse strict ;\n\nchomp ( my $n = );\nmy($gmax,$gmin) = (2*10**9,-2*10**9) ;\nwhile($n-- ){\n my($symbol,$guess,$answer) = split /\\s+/s , ;\n if($answer eq 'N'){\n my $temp = $symbol ;\n $symbol = '<' if $temp eq '>=' ;\n $symbol = '<=' if $temp eq '>' ;\n $symbol = '>' if $temp eq '<=' ;\n $symbol = '>=' if $temp eq '<' ;\n }\n \n $gmin = ($guess > $gmin)? $guess : $gmin if $symbol eq '>=' ;\n $gmin = ($guess + 1 > $gmin)? $guess +1 : $gmin if $symbol eq '>' ;\n $gmax = ($guess < $gmax)? $guess : $gmax if $symbol eq '<=' ;\n $gmax = ($guess - 1 < $gmax)? $guess -1 : $gmax if $symbol eq '<' ;\n}\n\nif($gmax < $gmin){\n print \"Impossible\\n\" ;\n}else{\n print \"$gmin\\n\";\n}\n\n\n"}, {"source_code": "$up = 1000000001;\n$down = -1000000001;\n$n = <>;\n$flag = 0;\nwhile ( $n-- ) {\n @array = split / /, <>;\n chomp(@array);\n\n if ( $array[2] eq \"N\" ) {\n if ( $array[0] eq \"<=\" ) {\n $array[0] = \">\";\n } \n elsif ( $array[0] eq \">=\" ) {\n $array[0] = \"<\";\n } \n elsif ( $array[0] eq \"<\" ) {\n $array[0] = \">=\";\n } \n elsif ( $array[0] eq \">\" ) {\n $array[0] = \"<=\";\n } \n }\n\n if ( $array[0] eq \"<=\" ) {\n if ( $up > $array[1] ) { $up = $array[1]; }\n }\n elsif ( $array[0] eq \"<\" ) {\n if ( $up > $array[1] - 1 ) { $up = $array[1] - 1; }\n }\n\n elsif ( $array[0] eq \">=\" ) {\n if ( $down < $array[1] ) { $down = $array[1]; }\n }\n elsif ( $array[0] eq \">\" ) {\n if ( $down < $array[1] + 1 ) { $down = $array[1] + 1; }\n }\n\n if ( $up < $down ) { $flag = 1; }\n\n}\n\n( $flag eq 1 ) ? print \"Impossible\" : print $up;"}, {"source_code": "for (<>){\n\ts/(.)= (-?\\d+)/$1.($2+(\"=\"cmp$1))/e;\n\n\tif (/N/){// and /-?\\d+/ and push @M, $&+1}\n\telse{\t // and push @m, /-?\\d+/ && $&}\n}\n@M=sort {$a<=>$b} @M;\n@m=sort {$a<=>$b} @m;\n\n$M= @M?$M[0]:2e9;\n$m= @m?$m[@m-1]:-2e9;\n\nprint $M>++$m?$m:Impossible"}, {"source_code": "\nwhile(<>){\n\t\n\t@_=<>;\n\t\n\t@max=@min=();\n\t\n\tfor (@_){\n\t\t\n\t\ts/<= (-?\\d+)/\"< \".($1+1)/e;\n\t\ts/>= (-?\\d+)/\"> \".($1-1)/e;\n\n#\t\tprint;\n\t\t\n\t\t/N/ and (s/< (-?\\d+)/\"> \".($1-1)/e or \n\t\ts/> (-?\\d+)/\"< \".($1+1)/e);\n\t\t\n\t#\t/Y/ and s/>// and /-?\\d+/ and push @min, $&;\n\t\n\t\t}\n#\tprint \"@min\\n\", \"@max\\n\";\n\t\n\t@max=sort {$a<=>$b} @max;\n\t@min=sort {$a<=>$b} @min;\n\t\n\tif (@max) {$max=shift @max}\n\telse {$max=2*10**9}\n\t\n\tif (@min) {$min=pop @min}\n\telse {$min=-2*10**9}\n\t\n#\tprint \"\\n$max $min\";\n\t\n\tif ($max>$min+1){print $min+1}\n\telse {print \"Impossible\"}\n\t}\n\t\n"}], "negative_code": [{"source_code": "$up = 1000000001;\n$down = -1000000001;\n$n = <>;\n$flag = 0;\nwhile ( $n-- ) {\n @array = split / /, <>;\n chomp(@array);\n\n if ( $array[2] eq \"N\" ) {\n if ( $array[0] eq \"<=\" ) {\n $array[0] = \">\";\n } \n elsif ( $array[0] eq \">=\" ) {\n $array[0] = \"<\";\n } \n elsif ( $array[0] eq \"<\" ) {\n $array[0] = \">=\";\n } \n elsif ( $array[0] eq \">\" ) {\n $array[0] = \"<=\";\n } \n }\n\n if ( $array[0] eq \"<=\" ) {\n if ( $up > $array[1] ) { $up = $array[1]; }\n }\n elsif ( $array[0] eq \"<\" ) {\n if ( $up > $array[1] - 1 ) { $up = $array[1] - 1; }\n }\n\n elsif ( $array[0] eq \">=\" ) {\n if ( $down < $array[1] ) { $down = $array[1]; }\n }\n elsif ( $array[0] eq \">\" ) {\n if ( $down < $array[1] + 1 ) { $down = $array[1] + 1; }\n }\n\n if ( $up < $down ) { $flag = 1; }\n\n}\n\n( $flag eq 1 ) ? print \"Impossoble\" : print $up;"}, {"source_code": "for (<>){\n\ts/(.)= (-?\\d+)/$1.($2+(\"=\"cmp$1))/e;\nprint \"=\"cmp$1,\"\\n\";\n\tif (/N/){// and /-?\\d+/ and push @M, $&+1}\n\telse{\t // and push @m, /-?\\d+/ && $&}\n}\n@M=sort {$a<=>$b} @M;\n@m=sort {$a<=>$b} @m;\n\n$M= @M?$M[0]:2e9;\n$m= @m?$m[@m-1]:-2e9;\n\nprint $M>++$m?$m:Impossible"}, {"source_code": "\nwhile(<>){\n\t\n\t@_=<>;\n\t\n\t@max=@min=();\n\t\n\tfor (@_){\n\t\t\n\t\ts/<= (-?\\d+)/\"< \".($1-1)/e;\n\t\ts/>= (-?\\d+)/\"> \".($1+1)/e;\n\n\t\t\n\t\t\n\t\t/N/ and tr/<>/>// and /-?\\d+/ and push @min, $&;\n\t\n\t\t}\n#\tprint \"@min\\n\", \"@max\\n\";\n\t\n\t@max=sort {$a<=>$b} @max;\n\t@min=sort {$a<=>$b} @min;\n\t\n\tif (@max) {$max=shift @max}\n\telse {$max=2*10**9}\n\t\n\tif (@min) {$min=pop @min}\n\telse {$min=-2*10**9}\n\t\n#\tprint \"\\n$max $min\";\n\t\n\tif ($max>$min+1){print $min+1}\n\telse {print \"Impossible\"}\n\t}\n\t\n"}, {"source_code": "for (<>){\n\ts/(.)= (-?\\d+)/$1.($2+($1cmp\"=\"))/e;\n\n\tif (/N/){// and /-?\\d+/ and push @M, $&+1}\n\telse{\t // and push @m, @q=/-?\\d+/}\n}\n@M=sort {$a<=>$b} @M;\n@m=sort {$a<=>$b} @m;\n\n$M= @M?$M[0]:2e9;\n$m= @m?$m[@m-1]:-2e9;\n\nprint $M>++$m?$m:Impossible"}], "src_uid": "6c6f29e1f4c951cd0ff15056774f897d"} {"nl": {"description": "Recently, Anton has found a set. The set consists of small English letters. Anton carefully wrote out all the letters from the set in one line, separated by a comma. He also added an opening curved bracket at the beginning of the line and a closing curved bracket at the end of the line. Unfortunately, from time to time Anton would forget writing some letter and write it again. He asks you to count the total number of distinct letters in his set.", "input_spec": "The first and the single line contains the set of letters. The length of the line doesn't exceed 1000. It is guaranteed that the line starts from an opening curved bracket and ends with a closing curved bracket. Between them, small English letters are listed, separated by a comma. Each comma is followed by a space.", "output_spec": "Print a single number \u2014 the number of distinct letters in Anton's set.", "sample_inputs": ["{a, b, c}", "{b, a, b, a}", "{}"], "sample_outputs": ["3", "2", "0"], "notes": null}, "positive_code": [{"source_code": "my $str = <>;\n$str =~ s/{([^{}]*)}\\n/$1/;\nmy %hash = map { $_, 1 } split \", \", $str;\nprint scalar keys %hash;"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nmy %st;\nchomp($line = <>);\n$st{$_}++ foreach(split \", \", substr $line, 1, length($line)-2);\nsay scalar(keys %st);\n"}, {"source_code": "chomp($i=<>);\n$i=~tr/{} //d;\nprint scalar(do{ \n %s; \n grep{\n !$s{$_\n }++;\n } \nsplit(/,/,$i); \n}\n);"}, {"source_code": "$a = <>;\n$a =~ /$_/ and $ans++ for 'a'..'z';\nprint 0+$ans, \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\n\nmy $s = <>;\nchomp $s;\n\n$s = substr(substr($s, 0, -1), 1);\n\nmy %set;\n\nforeach my $letter (split(', ', $s))\n{\n $set{$letter} = 1;\n}\n\nmy $result = keys %set;\n\nprint $result\n"}, {"source_code": "$_=join'',sort@_=<>=~/\\w/g; s/(.)\\1+/$1/g;\nprint length"}, {"source_code": "$a=<>;\n$a=~/$_/ and $ans++ for 'a'..'z';\nprint 0+$ans"}, {"source_code": "chomp($i=);$i=~tr/{} //d;print scalar(do{%s;grep{!$s{$_}++}split(/,/,$i)});"}, {"source_code": "$_ = <>; chomp;\ns/[\\{\\}\\s]//g;\n%h = map {$_ => 1;} split /,/;\nprint (scalar keys %h);"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nno warnings 'uninitialized';\n\n\nmy $line = <>;\n\n$line =~ s/[^a-z]//g;\n\nmy %seen;\nforeach my $c (split(//, $line))\n{\n $seen{$c} = 1\n}\n\nprint scalar(keys %seen), \"\\n\"\n\n"}, {"source_code": "my $s = <>;\n$s = substr ($s, 1, (length $s) - 3);\n%k = ();\ngrep { $k{$_} = $_ } split \", \", $s;\nprint scalar (keys %k);"}, {"source_code": "#! usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $line = readline();\n\nmy $count = {};\n\nwhile ($line =~ m/(\\w),/g) {\n $count->{$1} = 1;\n# print $1 . \"\\n\";\n}\nif ($line =~ m/(\\w)\\}/g) {\n $count->{$1} = 1;\n}\n\nmy @keys = keys %$count;\n\nmy $size = @keys;\n\nprint $size . \"\\n\";\n"}, {"source_code": "my $str = <>;\nmy $reg = \"{([^{}]*)}\\n\";\n$str =~ s/$reg/$1/;\nmy %hash = map { $_, 1 } split \", \", $str;\nprint scalar keys %hash;"}], "negative_code": [{"source_code": "$a = <>;\n$a =~ /$_/ and $ans++ for 'a'..'z';\nprint \"$ans\\n\";\n"}], "src_uid": "1cbbffd1941ed83b5f04e1ee33c77f61"} {"nl": {"description": "There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of \"LURLLR\" are \"LU\", \"LR\", \"LURLLR\", \"URL\", but not \"RR\" and \"UL\".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$$$) \u2014 the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) \u2014 the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' \u2014 the robot's path. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum n \\le 2 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \\le l \\le r \\le n$$$ \u2014 endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them.", "sample_inputs": ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"], "sample_outputs": ["1 2\n1 4\n3 4\n-1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t$_ = <>, chomp;\n\t\n\tmy( $x, $y ) = ( 0, 0 );\n\t\n\tmy %h;\n\t\n\t$h{ $x }{ $y } = 0;\n\t\n\tmy @cand;\n\t\n\tfor my $i ( 1 .. length ){\n\t\t/./g;\n\t\t\n\t\tif( 0 ){ ; }\n\t\telsif( $& eq 'L' ){\n\t\t\t$x --;\n\t\t\t}\n\t\telsif( $& eq 'R' ){\n\t\t\t$x ++;\n\t\t\t}\n\t\telsif( $& eq 'D' ){\n\t\t\t$y --;\n\t\t\t}\n\t\telsif( $& eq 'U' ){\n\t\t\t$y ++;\n\t\t\t}\n\t\t\n\t\tif( exists $h{ $x }{ $y } ){\n\t\t\tpush @cand, [ $h{ $x }{ $y }, $i ];\n\t\t\t}\n\t\t\n\t\t$h{ $x }{ $y } = $i;\n\t\t}\n\t\n\t$debug and print \":[@{ $_ }]\" for @cand;\n\t\n\t@cand = \n\t\tmap { $_->[ 1 ] }\n\t\tsort { $a->[ 0 ] <=> $b->[ 0 ] }\n\t\tmap { [ $_->[ 1 ] - $_->[ 0 ], $_ ] }\n\t\t@cand;\n\t\n\t$debug and print \":[@{ $_ }]\" for @cand;\n\t\n\tmy( $min ) = @cand;\n\t\n\tprint ! @cand ? -1 : join ' ', $min->[ 0 ] + 1, $min->[ 1 ];\n\t}"}], "negative_code": [], "src_uid": "1fc1d5ee79aaf823b246db5471ba7836"} {"nl": {"description": "Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly's room.Anatoly just made all his cockroaches to form a single line. As he is a perfectionist, he would like the colors of cockroaches in the line to alternate. He has a can of black paint and a can of red paint. In one turn he can either swap any two cockroaches, or take any single cockroach and change it's color.Help Anatoly find out the minimum number of turns he needs to make the colors of cockroaches in the line alternate.", "input_spec": "The first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of cockroaches. The second line contains a string of length n, consisting of characters 'b' and 'r' that denote black cockroach and red cockroach respectively.", "output_spec": "Print one integer\u00a0\u2014 the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate.", "sample_inputs": ["5\nrbbrr", "5\nbbbbb", "3\nrbr"], "sample_outputs": ["1", "2", "0"], "notes": "NoteIn the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn to do this.In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns.In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0."}, "positive_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\ty/br/10/;\n\t\n\t$min = ~0;\n\t\n\tfor $init (0, 1){\n\t\t$_0 = $_1 = 0;\n\t\t\n\t\twhile(/(.)(.)?/g){\n\t\t\t$_0 += $1 eq (1 - $init);\n\t\t\t$_1 += $2 eq (0 + $init);\n\t\t}\n\t\t\n\t\t$max = $_0 > $_1 ? $_0 : $_1;\n\t\t$min = $max if $max < $min;\n\t}\n\t\n\tprint $min\n}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = <>;\n\tchomp;\n\ty/br/10/;\n\t@_ = ();\n\t\n\tfor $init (0, 1){\n\t\t$i = $init;\n\t\tmy %h;\n\t\ts/./ $& eq $i ++ % 2 ? '' : $h{$&}++ /ger;\n\t\tpush @_, 0 + (sort {$b <=> $a} values %h)[ 0 ]\n\t\t}\n\t\n\tprint +(sort {$a <=> $b} @_)[ 0 ]\n\t\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t$_ = <>;\n\tchomp;\n\ty/br/10/;\n\t@_ = ();\n\t\n\tfor $init (0, 1){\n\t\t$i = $init;\n\t\tmy %h;\n\t\ts/./ $& eq $i ++ % 2 ? '' : $h{$&}++ /ger;\n\t\tpush @_, (sort {$b <=> $a} values %h)[ 0 ]\n\t\t}\n\t\n\tprint +(sort {$a <=> $b} @_)[ 0 ]\n\t\n\t}"}, {"source_code": "<>, $_ = <>;\ny/br/10/;\n\t\nfor $init (0, 1){\n\t$i = $init;\n\tpush @_, scalar( () = s/./ $& eq ++ $i % 2 ? 1 : 0 /ger =~ /0{1,2}/g );\n\t}\n\t\nprint +(sort {$a <=> $b} @_)[ 0 ]"}], "src_uid": "3adc75a180d89077aa90bbe3d8546e4d"} {"nl": {"description": "Pinkie Pie has bought a bag of patty-cakes with different fillings! But it appeared that not all patty-cakes differ from one another with filling. In other words, the bag contains some patty-cakes with the same filling.Pinkie Pie eats the patty-cakes one-by-one. She likes having fun so she decided not to simply eat the patty-cakes but to try not to eat the patty-cakes with the same filling way too often. To achieve this she wants the minimum distance between the eaten with the same filling to be the largest possible. Herein Pinkie Pie called the distance between two patty-cakes the number of eaten patty-cakes strictly between them.Pinkie Pie can eat the patty-cakes in any order. She is impatient about eating all the patty-cakes up so she asks you to help her to count the greatest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating!Pinkie Pie is going to buy more bags of patty-cakes so she asks you to solve this problem for several bags!", "input_spec": "The first line contains a single integer $$$T$$$ ($$$1 \\le T \\le 100$$$): the number of bags for which you need to solve the problem. The first line of each bag description contains a single integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$): the number of patty-cakes in it. The second line of the bag description contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le n$$$): the information of patty-cakes' fillings: same fillings are defined as same integers, different fillings are defined as different integers. It is guaranteed that each bag contains at least two patty-cakes with the same filling. It is guaranteed that the sum of $$$n$$$ over all bags does not exceed $$$10^5$$$.", "output_spec": "For each bag print in separate line one single integer: the largest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating for that bag.", "sample_inputs": ["4\n7\n1 7 1 6 4 4 6\n8\n1 1 4 6 4 6 4 7\n3\n3 3 3\n6\n2 5 2 3 1 4"], "sample_outputs": ["3\n2\n0\n4"], "notes": "NoteFor the first bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): $$$1$$$, $$$6$$$, $$$4$$$, $$$7$$$, $$$1$$$, $$$6$$$, $$$4$$$ (in this way, the minimum distance is equal to $$$3$$$).For the second bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): $$$1$$$, $$$4$$$, $$$6$$$, $$$7$$$, $$$4$$$, $$$1$$$, $$$6$$$, $$$4$$$ (in this way, the minimum distance is equal to $$$2$$$)."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @aa = map { $_ - 0 } split(/\\s+/,);\n \n my @ca = ();\n $#ca = 100000;\n \n my $mx_n = 0;\n my $mx_v = -1;\n \n for(my $i=0;$i<$n;$i++){\n $ca[$aa[$i]] ++;\n my $v = $ca[$aa[$i]];\n if( $v > $mx_v ){\n $mx_n = 1;\n $mx_v = $v;\n } elsif( $v == $mx_v ){\n $mx_n ++;\n }\n }\n \n my $r = -1;\n if( $mx_v > 1 ){\n $r = int( ( $n - $mx_n ) / ( $mx_v - 1 ) ) - 1;\n }\n print \"$r\\n\";\n \n}\n\n\n\n"}], "negative_code": [], "src_uid": "9d480b3979a7c9789fd8247120f31f03"} {"nl": {"description": "Given a positive integer $$$k$$$, two arrays are called $$$k$$$-similar if: they are strictly increasing; they have the same length; all their elements are positive integers between $$$1$$$ and $$$k$$$ (inclusive); they differ in exactly one position. You are given an integer $$$k$$$, a strictly increasing array $$$a$$$ and $$$q$$$ queries. For each query, you are given two integers $$$l_i \\leq r_i$$$. Your task is to find how many arrays $$$b$$$ exist, such that $$$b$$$ is $$$k$$$-similar to array $$$[a_{l_i},a_{l_i+1}\\ldots,a_{r_i}]$$$. ", "input_spec": "The first line contains three integers $$$n$$$, $$$q$$$ and $$$k$$$ ($$$1\\leq n, q \\leq 10^5$$$, $$$n\\leq k \\leq 10^9$$$)\u00a0\u2014 the length of array $$$a$$$, the number of queries and number $$$k$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots,a_n$$$ ($$$1 \\leq a_i \\leq k$$$). This array is strictly increasing \u00a0\u2014 $$$a_1 < a_2 < \\ldots < a_n$$$. Each of the following $$$q$$$ lines contains two integers $$$l_i$$$, $$$r_i$$$ ($$$1 \\leq l_i \\leq r_i \\leq n$$$).", "output_spec": "Print $$$q$$$ lines. The $$$i$$$-th of them should contain the answer to the $$$i$$$-th query.", "sample_inputs": ["4 2 5\n1 2 4 5\n2 3\n3 4", "6 5 10\n2 4 6 7 8 9\n1 4\n1 2\n3 5\n1 6\n5 5"], "sample_outputs": ["4\n3", "8\n9\n7\n6\n9"], "notes": "NoteIn the first example:In the first query there are $$$4$$$ arrays that are $$$5$$$-similar to $$$[2,4]$$$: $$$[1,4],[3,4],[2,3],[2,5]$$$.In the second query there are $$$3$$$ arrays that are $$$5$$$-similar to $$$[4,5]$$$: $$$[1,5],[2,5],[3,5]$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $q, $k ) = split;\n\tmy @A = split ' ', <>;\n\t\n\tfor( 1 .. $q ){\n\t\tmy( $L, $R ) = split ' ', <>;\n\t\t\n\t\tmy $inside = $A[ $R - 1 ] - $A[ $L - 1 ] - ( $R - $L );\n\t\t$inside *= 2;\n\t\t\n\t\tprint $inside + $k - $A[ $R - 1 ] + $A[ $L - 1 ] - 1;\n\t\t}\n\t\n\t}"}], "negative_code": [], "src_uid": "740d2aba32f69b8cfe8f7cb624621a63"} {"nl": {"description": "Phoenix is playing with a new puzzle, which consists of $$$n$$$ identical puzzle pieces. Each puzzle piece is a right isosceles triangle as shown below. A puzzle piece The goal of the puzzle is to create a square using the $$$n$$$ pieces. He is allowed to rotate and move the pieces around, but none of them can overlap and all $$$n$$$ pieces must be used (of course, the square shouldn't contain any holes as well). Can he do it?", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$)\u00a0\u2014 the number of puzzle pieces.", "output_spec": "For each test case, if Phoenix can create a square with the $$$n$$$ puzzle pieces, print YES. Otherwise, print NO.", "sample_inputs": ["3\n2\n4\n6"], "sample_outputs": ["YES\nYES\nNO"], "notes": "NoteFor $$$n=2$$$, Phoenix can create a square like this: For $$$n=4$$$, Phoenix can create a square like this: For $$$n=6$$$, it is impossible for Phoenix to create a square."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my $i1 = int(sqrt($n / 2)+0.5);\r\n \r\n my $res = 'NO';\r\n for(my $i=$i1-2;$i<=$i1+2;$i++){\r\n next if $i<1;\r\n $res = 'YES' if( 2 * $i * $i == $n );\r\n }\r\n \r\n my $i1 = int(sqrt($n / 4)+0.5);\r\n \r\n for(my $i=$i1-2;$i<=$i1+2;$i++){\r\n next if $i<1;\r\n $res = 'YES' if( 4 * $i * $i == $n );\r\n }\r\n \r\n print \"$res\\n\";\r\n \r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [], "src_uid": "6ae754639d96790c890f9d1ab259332a"} {"nl": {"description": "Polycarp has come up with a new game to play with you. He calls it \"A missing bigram\".A bigram of a word is a sequence of two adjacent letters in it.For example, word \"abbaaba\" contains bigrams \"ab\", \"bb\", \"ba\", \"aa\", \"ab\" and \"ba\".The game goes as follows. First, Polycarp comes up with a word, consisting only of lowercase letters 'a' and 'b'. Then, he writes down all its bigrams on a whiteboard in the same order as they appear in the word. After that, he wipes one of them off the whiteboard.Finally, Polycarp invites you to guess what the word that he has come up with was.Your goal is to find any word such that it's possible to write down all its bigrams and remove one of them, so that the resulting sequence of bigrams is the same as the one Polycarp ended up with.The tests are generated in such a way that the answer exists. If there are multiple answers, you can print any of them.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 2000$$$)\u00a0\u2014 the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$3 \\le n \\le 100$$$)\u00a0\u2014 the length of the word Polycarp has come up with. The second line of each testcase contains $$$n-2$$$ bigrams of that word, separated by a single space. Each bigram consists of two letters, each of them is either 'a' or 'b'. Additional constraint on the input: there exists at least one string such that it is possible to write down all its bigrams, except one, so that the resulting sequence is the same as the sequence in the input. In other words, the answer exists.", "output_spec": "For each testcase print a word, consisting of $$$n$$$ letters, each of them should be either 'a' or 'b'. It should be possible to write down all its bigrams and remove one of them, so that the resulting sequence of bigrams is the same as the one Polycarp ended up with. The tests are generated in such a way that the answer exists. If there are multiple answers, you can print any of them. ", "sample_inputs": ["4\n7\nab bb ba aa ba\n7\nab ba aa ab ba\n3\naa\n5\nbb ab bb"], "sample_outputs": ["abbaaba\nabaabaa\nbaa\nbbabb"], "notes": "NoteThe first two testcases from the example are produced from the word \"abbaaba\". As listed in the statement, it contains bigrams \"ab\", \"bb\", \"ba\", \"aa\", \"ab\" and \"ba\".In the first testcase, the $$$5$$$-th bigram is removed. In the second testcase, the $$$2$$$-nd bigram is removed. However, that sequence could also have been produced from the word \"abaabaa\". It contains bigrams \"ab\", \"ba\", \"aa\", \"ab\", \"ba\" and \"aa\". The missing bigram is the $$$6$$$-th one.In the third testcase, all of \"baa\", \"aab\" and \"aaa\" are valid answers."}, "positive_code": [{"source_code": "<>;\r\n\r\nwhile(<>){\r\n\t$_ = <>;\r\n\t\r\n\ts|(.)\\K \\1||g;\r\n\t\r\n\tprint m| | ? s| ||r : s|^|a|r;\r\n\t}"}, {"source_code": "#!/usr/bin/perl\r\n\r\nuse warnings;\r\nuse strict;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$_ = <>;\r\n\t\r\n\ts/(.)\\K \\1//g;\r\n\t\r\n\tprint m/ / ? s/ //r : s/^/a/r;\r\n\t}"}, {"source_code": "<>;\r\n\r\nwhile(<>){\r\n\t$_ = <>;\r\n\t\r\n\ts/(.)\\K \\1//g;\r\n\t\r\n\tprint m/ / ? s/ //r : s/^/a/r;\r\n\t}"}, {"source_code": "<>;\r\nwhile(<>){\r\n\t$_ = <>;\r\n\ts/(.)\\K \\1//g;\r\n\tprint m/ / ? s/ //r : s/^/a/r;\r\n}"}], "negative_code": [], "src_uid": "565056bfe716ee89230270bdc14c9051"} {"nl": {"description": "Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y\u2009=\u200910100.She then will slide the disks towards the line y\u2009=\u20090 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi,\u200910100). She will then push it so the disk\u2019s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y\u2009=\u20090 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.", "input_spec": "The first line will contain two integers n and r (1\u2009\u2264\u2009n,\u2009r\u2009\u2264\u20091\u2009000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1,\u2009x2,\u2009...,\u2009xn (1\u2009\u2264\u2009xi\u2009\u2264\u20091\u2009000)\u00a0\u2014 the x-coordinates of the disks.", "output_spec": "Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10\u2009-\u20096. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.", "sample_inputs": ["6 2\n5 5 6 8 3 12"], "sample_outputs": ["2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"], "notes": "NoteThe final positions of the disks will look as follows: In particular, note the position of the last disk. "}, "positive_code": [{"source_code": "\n\n\nmy $n, $r;\nmy $f = ;\nchomp ( $f );\n($n, $r) = split m/ /, $f;\n$f = ;\nchomp ( $f );\nmy @disks = split m/ /, $f;\n\n\n\n\nmy @placed = ();\nmy $elem;\nmy $found;\nmy @yy = ();\nmy $val;\nmy $height;\n\nmy $j = 0;\nmy $max;\nwhile ( @disks > 0 ) {\n\n $elem = shift @disks;\n $found = 0;\n \n $max = 0;\n for ( my $i = 0; $i < @placed; $i += 1 ) {\n\n\tif ( abs ( $elem - $placed[ $i ][ 0 ] ) <= (2 * $r) ) { # if there's a collision\n\t $found = 1;\n\n\t $height = sqrt (abs( ((2 * $r) ** 2) - (abs ( $elem - $placed[ $i ][ 0 ] ) ** 2)));\n\t $val = $placed[ $i ][ 1 ] + $height;\n\t if ( $val > $max ) {\n\t\t$max = $val;}\n\t}\n }\n\n if ( $found == 1 ) {\n\tunshift @placed, [ $elem, $max, $j ];\n }\n \n if ( $found == 0 ) {\t# if there's no collision\n\tunshift @placed, [ $elem, $r, $j ];\n }\n\n @placed = sort { ${ $a }[ 1 ] <=> ${ $b }[ 1 ] } @placed;\n @placed = reverse @placed;\n\n $j += 1;\n}\n\n\nmy @answer = sort { ${ $a }[ 2 ] <=> ${ $b }[ 2 ] } @placed;\n\n\nfor ( my $i = 0; $i < @answer; $i += 1 ) {\n print $answer[ $i ][ 1 ];\n print \" \";}\nprint \"\\n\";\n\n\n\n"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nmy (undef, $r) = split ' ', <>;\nmy @x = split ' ', <>;\n\nmy %y;\nmy @y;\n\nfor my $x (@x) {\n my $max = $r;\n while (my ($x2, $y2s) = each %y) {\n for my $y2 (@$y2s) {\n if (abs($x2 - $x) <= 2 * $r) {\n # collision\n\n # dx^2 + dy^2 = (2r)^2\n my $dy = sqrt(4 * $r**2 - ($x2 - $x)**2);\n my $y = $y2 + $dy;\n\n $max = $y if $y > $max;\n }\n }\n }\n\n push @y, $max;\n push @{$y{$x}}, $max;\n}\n\nsay join ' ', @y;\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $r ) = split;\n\tmy( $x1, @x ) = split ' ', <>;\n\t\n\tmy %y;\n\t$y{ $x1 } = $r;\n\tmy @y;\n\tpush @y, $r;\n\t\n\tmy $sqr = ( $r * 2 ) ** 2;\n\t\n\tfor my $x ( @x ){\n\t\t\n\t\tmy $Y = 0;\n\t\t\n\t\tfor my $xkey ( keys %y ){\n\t\t\t\n\t\t\tmy $B = $sqr - ( $x - $xkey ) ** 2;\n\t\t\tnext if $B < 0;\n\t\t\t\n\t\t\tmy $y = $y{ $xkey } + sqrt( $B );\n\t\t\t\n\t\t\tif( $y >= $Y ){\n\t\t\t\t$Y = $y;\n\t\t\t\t}\n\t\t\t\n\t\t\t}\n\t\t\n\t\t$Y ||= $r;\n\t\t$y{ $x } = $Y;\n\t\tpush @y, $Y;\n\t\t}\n\t\n\tprint \"@y\";\n\t}"}, {"source_code": "( $n, $r, @x ) = split ' ', <>.<>;\n\nfor $x ( @x ){\n\t\n\t$Y = $r;\n\t\n\tfor $k ( keys %y ){\n\t\t\n\t\t$_ = $r ** 2 * 4 - ( $x - $k ) ** 2;\n\t\tnext if /-/;\n\t\t\n\t\t$y = $y{ $k } + sqrt;\n\t\t\n\t\t$y >= $Y and $Y = $y;\n\t\t}\n\t\n\tpush @y, $y{ $x } = $Y;\n\t}\n\nprint \"@y\""}], "negative_code": [{"source_code": "\n\n\nmy $n, $r;\nmy $f = ;\nchomp ( $f );\n($n, $r) = split m/ /, $f;\n$f = ;\nchomp ( $f );\nmy @disks = split m/ /, $f;\n\n\n\n\nmy @placed = ();\nmy $elem;\nmy $found;\nmy @yy = ();\nmy $val;\nmy $height;\n\nmy $j = 0;\nwhile ( @disks > 0 ) {\n\n $elem = shift @disks;\n $found = 0;\n \n for ( my $i = 0; not $found and $i < @placed; $i += 1 ) {\n\tif ( abs ( $elem - $placed[ $i ][ 0 ] ) <= (2 * $r) ) { # if there's a collision\n\t $found = 1;\n\n\t $height = sqrt (abs( ((2 * $r) ** 2) - (abs ( $elem - $placed[ $i ][ 0 ] ) ** 2)));\n\t $val = $placed[ $i ][ 1 ] + $height;\n\t unshift @placed, [ $elem, $val, $j ];\n\t}\n }\n \n if ( $found == 0 ) {\t# if there's no collision\n\tunshift @placed, [ $elem, $r, $j ];\n }\n\n @placed = sort { ${ $a }[ 1 ] <=> ${ $b }[ 1 ] } @placed;\n @placed = reverse @placed;\n $j += 1;\n}\n\n\nmy @answer = sort { ${ $a }[ 2 ] <=> ${ $b }[ 2 ] } @placed;\n\n\nfor ( my $i = 0; $i < @answer; $i += 1 ) {\n print $answer[ $i ][ 1 ];\n print \" \";}\nprint \"\\n\";\n\n\n\n"}, {"source_code": "\n\n\nmy $n, $r;\nmy $f = ;\nchomp ( $f );\n($n, $r) = split m/ /, $f;\n$f = ;\nchomp ( $f );\nmy @disks = split m/ /, $f;\n\n\n\n\nmy @placed = ();\nmy $elem;\nmy $found;\nmy @yy = ();\nmy $val;\nmy $height;\n\nwhile ( @disks > 0 ) {\n\n $elem = shift @disks;\n $found = 0;\n \n for ( my $i = 0; not $found and $i < @placed; $i += 1 ) {\n\tif ( abs ( $elem - $placed[ $i ][ 0 ] ) <= (2 * $r) ) { # if there's a collision\n\t $found = 1;\n\n\t $height = sqrt (abs( ((2 * $r) ** 2) - (abs ( $elem - $placed[ $i ][ 0 ] ) ** 2)));\n\t $val = $placed[ $i ][ 1 ] + $height;\n\t unshift @placed, [ $elem, $val ];\n\t}\n }\n\n if ( $found == 0 ) {\t# if there's no collision\n\tunshift @placed, [ $elem, $r ];\n }\n}\n\n\nmy @answer = reverse @placed;\n\n\nfor ( my $i = 0; $i < @answer; $i += 1 ) {\n print $answer[ $i ][ 1 ];\n print \" \"; }\nprint \"\\n\";\n\n\n\n"}], "src_uid": "3cd019d1016cb3b872ea956c312889eb"} {"nl": {"description": "Piegirl got bored with binary, decimal and other integer based counting systems. Recently she discovered some interesting properties about number , in particular that q2\u2009=\u2009q\u2009+\u20091, and she thinks it would make a good base for her new unique system. She called it \"golden system\". In golden system the number is a non-empty string containing 0's and 1's as digits. The decimal value of expression a0a1...an equals to .Soon Piegirl found out that this system doesn't have same properties that integer base systems do and some operations can not be performed on it. She wasn't able to come up with a fast way of comparing two numbers. She is asking for your help.Given two numbers written in golden system notation, determine which of them has larger decimal value.", "input_spec": "Input consists of two lines \u2014 one for each number. Each line contains non-empty string consisting of '0' and '1' characters. The length of each string does not exceed 100000.", "output_spec": "Print \">\" if the first number is larger, \"<\" if it is smaller and \"=\" if they are equal.", "sample_inputs": ["1000\n111", "00100\n11", "110\n101"], "sample_outputs": ["<", "=", ">"], "notes": "NoteIn the first example first number equals to , while second number is approximately 1.6180339882\u2009+\u20091.618033988\u2009+\u20091\u2009\u2248\u20095.236, which is clearly a bigger number.In the second example numbers are equal. Each of them is \u2009\u2248\u20092.618."}, "positive_code": [{"source_code": "for (<>){\n\t$_=(reverse sprintf \"%0100001s\", $_),\n\ts!11(01)*0!\"0\"x(length($&)-1).\"1\"!ge,\n\ts!((11)+)0!\"0\".\"01\"x(length($1)/2)!ge,\n\tpush @_, $_=reverse $_\n\t}\n\nprint qw(= > <)[$_[0] cmp $_[1]]"}, {"source_code": "$x;\n\nfor (@_){\n\t$_=(reverse \"0\"x($x-length).$_),\n\ts!11(01)*0!\"0\"x(length($&)-1).\"1\"!ge,\n\ts!((11)+)0!\"0\".\"01\"x(length($1)/2)!ge,\n\t$_=reverse $_\n\t}\n\nprint qw(= > <)[$_[0] cmp $_[1]]"}, {"source_code": "$x;\n\nfor (@_){\n\t$_=(reverse \"0\"x($x-length).$_),\n\ts!11(01)*0!\"0\"x(length($&)-1).\"1\"!ge,\n\ts!((11)+)0!\"0\".\"01\"x(length($1)/2)!ge,\n\t$_=reverse $_\n\t}\n\nprint qw(= > <)[$_[0] cmp $_[1]]"}, {"source_code": "chomp($a=<>);\nchomp($b=<>);\n$d=length($a)-length($b);\n$a.=\"000\";\n$b.=\"000\";\n$d>0? ($b=\"0\"x $d.$b):($a=\"0\"x (-$d).$a);\n\n@a=split//,$a;\n@b=split//,$b;\n\nfor $i(0..@a-1){\n\t\n\tif (!$f && !$g){\n\t\t$f=$g=$a[$i]<=>$b[$i]\n\t\t}\n\telsif ($f>0 && $g){\n\t\t$a[$i]<$b[$i]?($f--):(++$F)\n\t\t\n\t\t}\n\telsif (!$f && $g>0){\n\t\t($ab=$a[$i]<=>$b[$i])?($ab>0?(++$F):($g--)):($f++)\n\t\t}\n\telsif ($f<0 && $g){\n\t\t$a[$i]>$b[$i]?($f++):(--$F)\n\t\t\n\t\t}\n\telsif (!$f && $g<0){\n\t\t($ab=$a[$i]<=>$b[$i])?($ab<0?(--$F):($g++)):($f--)\n\t\t}\n\t$F && last\n\t}\n\n\nprint $F? ( $F>0? (\">\"):(\"<\") ):(\"=\")"}, {"source_code": "chomp(@_=<>);\n$x0? \">\" : \"<\" ): \"=\""}, {"source_code": "chomp($a=<>);\nchomp($b=<>);\n$la=length($a);\n$lb=length($b);\n$max=$la;\n$max=$lb if $lb>$max;\n$a=\"0\".(\"0\"x($max-$la)).$a.\"000\";\n$b=\"0\".(\"0\"x($max-$lb)).$b.\"000\";\n\n#print \"$a=$b\\n\";\n\n@a=split//,$a;\n@b=split//,$b;\n\n#print \"@a @b\\n\";\n\n$F=0;\n$f=$g=0;\n\nfor $i(0..(@a-1)){\n\t\n\tif ($f==0 and $g==0){\n\t\tif ($a[$i]>$b[$i]){\n\t\t\t$f++, $g++\n\t\t\t\n\t\t\t}\n\t\tif ($a[$i]<$b[$i]){\n\t\t\t$f--, $g--\n\t\t\t\n\t\t\t}\n\t\t}\n\telsif ($f==1 and $g==1){\n\t\tif ($a[$i]>=$b[$i]){ $F=1; last}\n\t\telsif ($a[$i]<$b[$i]){\n\t\t\t$f--\n\t\t\t}\n\t\t\n\t\t}\n\telsif ($f==0 and $g==1){\n\t\tif ($a[$i]>$b[$i]){ $F=1; last}\n\t\telsif ($a[$i]<$b[$i]){\n\t\t\t$g--\n\t\t\t}\n\t\telse {\n\t\t\t$f++\n\t\t\t}\n\t\t}\n\t\t\n\telsif ($f==-1 and $g==-1){\n\t\tif ($a[$i]<=$b[$i]){ $F=-1; last}\n\t\telsif ($a[$i]>$b[$i]){\n\t\t\t$f++\n\t\t\t}\n\t\t\n\t\t}\n\telsif ($f==0 and $g==-1){\n\t\tif ($a[$i]<$b[$i]){ $F=-1; last}\n\t\telsif ($a[$i]>$b[$i]){\n\t\t\t$g++\n\t\t\t}\n\t\telse {\n\t\t\t$f--\n\t\t\t}\n\t\t}\n\t\n\t\n\t}\n\n\n#print \"$a=$b\";\n#$ans = $a cmp $b;\nprint $F? ( $F>0? (\">\"):(\"<\") ):(\"=\")"}, {"source_code": "chomp(@_=<>);\n$x0? \">\" : \"<\" ): \"=\""}, {"source_code": "chomp(@_=<>);\n$x$b[$i];\n\t\n\tif (!$f && !$g){\n\t\t$f= $g= $cmp\n\t\t}\n\telsif ($f && $g){\n\t\t$f+= $cmp || $f\n\t\t}\n\telse {\n\t\t$f+= $g+= $cmp\n\t\t}\n\t\n\tabs $f > 1 && last\n\t}\n\nprint $f? ( $f>0? \">\" : \"<\" ): \"=\""}, {"source_code": "chomp(@_=<>);\n$x0? \">\" : \"<\" ): \"=\""}, {"source_code": "chomp(@_=<>);\n$x$b[$i];\n\t\n\tif (!$f && !$g){\n\t\t$f= $g= $cmp\n\t\t}\n\telsif ($f && $g){\n\t\t$f+= $cmp || $f\n\t\t}\n\telse {\n\t\t$f+= $g+= $cmp\n\t\t}\n\t\n\tabs $f > 1 && last\n\t}\n\nprint $g? ( $g>0? \">\" : \"<\" ): \"=\""}, {"source_code": "chomp(@_=<>);\n$x$b[$i];\n\t\n\tif (!$f && !$g){\n\t\t$f= $g= $cmp\n\t\t}\n\telsif ($f && $g){\n\t\t$f+= $cmp || $f\n\t\t}\n\telse {\n\t\t$f+= $g+= $cmp\n\t\t}\n\t\n\tabs $f > 1 && last\n\t}\n\nprint $g? ( $g>0? \">\" : \"<\" ): \"=\""}], "negative_code": [{"source_code": "for (<>){\n\t$_=(reverse sprintf \"%0100001s\", $_),\n\ts!11(01)*0!\"0\"x(length($&)-1).\"1\"!ge,\n\ts!((11)+)0!\"0\".\"01\"x(length($1)/2)!ge,\n\tpush @_, reverse $_\n\t}\n\nprint qw(= > <)[$_[0] cmp $_[1]]"}, {"source_code": "chomp($a=<>);\nchomp($b=<>);\n$la=length($a);\n$lb=length($b);\n$max=$la;\n$max=$lb if $lb>$max;\n$a=(\"0\"x($max-$la)).$a;\n$b=(\"0\"x($max-$lb)).$b;\n#print \"$a=$b\\n\";\n$a=~s#0((11)+)#(\"10\"x((length($1))/2)).\"0\"#ge;\n$b=~s#0((11)+)#(\"10\"x((length($1))/2)).\"0\"#ge;\n#print \"$a=$b\";\n$ans = $a cmp $b;\nprint $ans? ( $ans>0? (\">\"):(\"<\") ):(\"=\")"}, {"source_code": "chomp($a=<>);\nchomp($b=<>);\n$la=length($a);\n$lb=length($b);\n$max=$la;\n$max=$lb if $lb>$max;\n$a=\"0\".(\"0\"x($max-$la)).$a.\"000\";\n$b=\"0\".(\"0\"x($max-$lb)).$b.\"000\";\n\n#print \"$a=$b\\n\";\n\n@a=split//,$a;\n@b=split//,$b;\n\n#print \"@a @b\\n\";\n\n$F=0;\n$f=$g=0;\n\nfor $i(0..(@a-1)){\n\t\n\tif ($f==0 and $g==0){\n\t\tif ($a[$i]>$b[$i]){\n\t\t\t$f++, $g++\n\t\t\t\n\t\t\t}\n\t\tif ($a[$i]<$b[$i]){\n\t\t\t$f--, $g--\n\t\t\t\n\t\t\t}\n\t\t}\n\telsif ($f==1 and $g==1){\n\t\tif ($a[$i]>=$b[$i]){ $F=1; last}\n\t\telsif ($a[$i]<$b[$i]){\n\t\t\t$f--\n\t\t\t}\n\t\t\n\t\t}\n\telsif ($f==0 and $g==1){\n\t\tif ($a[$i]>$b[$i]){ $F=1; last}\n\t\telsif ($a[$i]<$b[$i]){\n\t\t\t$g--\n\t\t\t}\n\t\telse {\n\t\t\t$f++\n\t\t\t}\n\t\t}\n\t\t\n\telsif ($f==-1 and $g==-1){\n\t\tif ($a[$i]<=$b[$i]){ $F=-1; last}\n\t\telsif ($a[$i]>$b[$i]){\n\t\t\t$f++\n\t\t\t}\n\t\t\n\t\t}\n\telsif ($f==0 and $g==-1){\n\t\tif ($a[$i]<$b[$i]){ $F=-1; last}\n\t\telsif ($a[$i]<$b[$i]){\n\t\t\t$g++\n\t\t\t}\n\t\telse {\n\t\t\t$f--\n\t\t\t}\n\t\t}\n\t\n\t\n\t}\n\n\n#print \"$a=$b\";\n#$ans = $a cmp $b;\nprint $F? ( $F>0? (\">\"):(\"<\") ):(\"=\")"}, {"source_code": "chomp($a=<>);\nchomp($b=<>);\n$la=length($a);\n$lb=length($b);\n$max=$la;\n$max=$lb if $lb>$max;\n$a=(\"0\"x($max-$la)).$a;\n$b=(\"0\"x($max-$lb)).$b;\nprint \"$a-$b\\n\";\n$a=~s/011/100/g;\n$b=~s/011/100/g;\nprint \"$a-$b\\n\";\n$ans = $a cmp $b;\nprint $ans? ( $ans>0? (\">\"):(\"<\") ):(\"=\")"}, {"source_code": "chomp(@_=<>);\n$x$b[$i];\n\t\n\tif (!$f && !$g){\n\t\t$f= $g= $cmp\n\t\t}\n\telsif ($f && $g){\n\t\t$f+= $cmp || $f\n\t\t}\n\telse {\n\t\t$g+ $cmp ? ($f+=$g):($g=0)\n\t\t}\n\t\n\tabs $f > 1 && last\n\t}\n\nprint $f? ( $f>0? \">\" : \"<\" ): \"=\""}, {"source_code": "chomp($a=<>);\nchomp($b=<>);\n$la=length($a);\n$lb=length($b);\n$max=$la;\n$max=$lb if $lb>$max;\n$a=(\"0\"x($max-$la)).$a;\n$b=(\"0\"x($max-$lb)).$b;\n$a=~s/011/100/g;\n$b=~s/011/100/g;\n$ans = $a cmp $b;\nprint $ans? ( $ans>0? (\">\"):(\"<\") ):(\"=\")"}, {"source_code": "chomp(@_=<>);\n$x0? \">\" : \"<\" ): \"=\""}, {"source_code": "chomp(@_=<>);\n$x$b[$i];\n\t\n\tif (!$f && !$g){\n\t\t$f= $g= $cmp\n\t\t}\n\telsif ($f && $g){\n\t\t$f+= $cmp || $f\n\t\t}\n\telse {\n\t\t$g+ $cmp ? ($f+=$g):($g+=$g)\n\t\t}\n\t\n\tabs $f > 1 && last\n\t}\n\nprint $f? ( $f>0? \">\" : \"<\" ): \"=\""}, {"source_code": "chomp($a=<>);\nchomp($b=<>);\n$la=length($a);\n$lb=length($b);\n$max=$la;\n$max=$lb if $lb>$max;\n$a=\"0\".(\"0\"x($max-$la)).$a;\n$b=\"0\".(\"0\"x($max-$lb)).$b;\n#print \"$a=$b\\n\";\n$a=~s#((01)*)1((11)*)#(\"1\".(\"0\"x(length($1)-1)).\"10\"x((length($3))/2)).($1 || $2? \"0\":\"\")#ge;\n$b=~s#((01)*)1((11)*)#(\"1\".(\"0\"x(length($1)-1)).\"10\"x((length($3))/2)).($1 || $2? \"0\":\"\")#ge;\n#print \"$a=$b\";\n$ans = $a cmp $b;\nprint $ans? ( $ans>0? (\">\"):(\"<\") ):(\"=\")"}, {"source_code": "chomp($a=<>);\nchomp($b=<>);\n$la=length($a);\n$lb=length($b);\n$max=$la;\n$max=$lb if $lb>$max;\n$a=(\"0\"x($max-$la)).$a;\n$b=(\"0\"x($max-$lb)).$b;\nprint \"$a-$b\\n\";\n$a=~s/011/100/g;\n$b=~s/011/100/g;\n$ans = $a cmp $b;\nprint $ans? ( $ans>0? (\">\"):(\"<\") ):(\"=\")"}], "src_uid": "7c0a288a2777894bdfd75cb9703346e9"} {"nl": {"description": "The process of mammoth's genome decoding in Berland comes to its end!One of the few remaining tasks is to restore unrecognized nucleotides in a found chain s. Each nucleotide is coded with a capital letter of English alphabet: 'A', 'C', 'G' or 'T'. Unrecognized nucleotides are coded by a question mark '?'. Thus, s is a string consisting of letters 'A', 'C', 'G', 'T' and characters '?'.It is known that the number of nucleotides of each of the four types in the decoded genome of mammoth in Berland should be equal.Your task is to decode the genome and replace each unrecognized nucleotide with one of the four types so that the number of nucleotides of each of the four types becomes equal.", "input_spec": "The first line contains the integer n (4\u2009\u2264\u2009n\u2009\u2264\u2009255)\u00a0\u2014 the length of the genome. The second line contains the string s of length n\u00a0\u2014 the coded genome. It consists of characters 'A', 'C', 'G', 'T' and '?'.", "output_spec": "If it is possible to decode the genome, print it. If there are multiple answer, print any of them. If it is not possible, print three equals signs in a row: \"===\" (without quotes).", "sample_inputs": ["8\nAG?C??CT", "4\nAGCT", "6\n????G?", "4\nAA??"], "sample_outputs": ["AGACGTCT", "AGCT", "===", "==="], "notes": "NoteIn the first example you can replace the first question mark with the letter 'A', the second question mark with the letter 'G', the third question mark with the letter 'T', then each nucleotide in the genome would be presented twice.In the second example the genome is already decoded correctly and each nucleotide is exactly once in it.In the third and the fourth examples it is impossible to decode the genom. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\tmy %h;\n\t\n\tmap { $h{$_}++ } grep /\\w/, split //;\n\t\n\tmy $L = length;\n\tmy $q = $L / 4;\n\t\n\tprint do {\n\t\tif ($L % 4 or $q < (sort {$b <=> $a} values %h)[0] ){\n\t\t\t'==='\n\t\t\t}\n\t\telse{\n\t\t\tfor my $lett (split //, 'ACTG'){\n\t\t\t\tfor my $times ( 1 .. $q - $h{$lett} ){\n\t\t\t\t\ts/\\?/$lett/\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t$_\n\t\t\t}\n\t\t};\n\t\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(not eof){\n\tmy( $Ax, $Ay, $Bx, $By, $Cx, $Cy ) = \n\t\tmap { split } ~~<>, ~~<>, ~~<>;\n\t\n\tmy( $Dx, $Dy, $Ex, $Ey, $Fx, $Fy );\n\t\n\t$Dx = ($Ax + $Bx) / 2;\n\t$Dy = ($Ay + $By) / 2;\n\t\n\t$Ex = ($Cx + $Bx) / 2;\n\t$Ey = ($Cy + $By) / 2;\n\t\n\t$Fx = ($Ax + $Cx) / 2;\n\t$Fy = ($Ay + $Cy) / 2;\n\t\n\t$debug and print \"[$Dx, $Dy, $Ex, $Ey, $Fx, $Fy]\";\n\t\n\tmy( $AEx, $AEy, $CDx, $CDy, $BFx, $BFy );\n\tmy $diff;\n\t\n\t$diff = $Ex - $Ax;\n\t$AEx = $Ex + $diff;\n\t\n\t$diff = $Ey - $Ay;\n\t$AEy = $Ey + $diff;\n\t\n\t$diff = $Dx - $Cx;\n\t$CDx = $Dx + $diff;\n\t\n\t$diff = $Dy - $Cy;\n\t$CDy = $Dy + $diff;\n\t\n\t$diff = $Fx - $Bx;\n\t$BFx = $Fx + $diff;\n\t\n\t$diff = $Fy - $By;\n\t$BFy = $Fy + $diff;\t\n\t\n\tprint 3;\n\tprint for \"$AEx $AEy\", \"$CDx $CDy\", \"$BFx $BFy\";\n\n\t}"}], "src_uid": "d6423f380e6ba711d175d91e73f97b47"} {"nl": {"description": "Some number of people (this number is even) have stood in a circle. The people stand in the circle evenly. They are numbered clockwise starting from a person with the number $$$1$$$. Each person is looking through the circle's center at the opposite person. A sample of a circle of $$$6$$$ persons. The orange arrows indicate who is looking at whom. You don't know the exact number of people standing in the circle (but this number is even, no doubt). It is known that the person with the number $$$a$$$ is looking at the person with the number $$$b$$$ (and vice versa, of course). What is the number associated with a person being looked at by the person with the number $$$c$$$? If, for the specified $$$a$$$, $$$b$$$, and $$$c$$$, no such circle exists, output -1.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) \u2014 the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing three distinct integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \\le a,b,c \\le 10^8$$$).", "output_spec": "For each test case output in a separate line a single integer $$$d$$$ \u2014 the number of the person being looked at by the person with the number $$$c$$$ in a circle such that the person with the number $$$a$$$ is looking at the person with the number $$$b$$$. If there are multiple solutions, print any of them. Output $$$-1$$$ if there's no circle meeting the given conditions.", "sample_inputs": ["7\n6 2 4\n2 3 1\n2 4 10\n5 3 4\n1 3 2\n2 5 4\n4 3 2"], "sample_outputs": ["8\n-1\n-1\n-1\n4\n1\n-1"], "notes": "NoteIn the first test case, there's a desired circle of $$$8$$$ people. The person with the number $$$6$$$ will look at the person with the number $$$2$$$ and the person with the number $$$8$$$ will look at the person with the number $$$4$$$.In the second test case, there's no circle meeting the conditions. If the person with the number $$$2$$$ is looking at the person with the number $$$3$$$, the circle consists of $$$2$$$ people because these persons are neighbors. But, in this case, they must have the numbers $$$1$$$ and $$$2$$$, but it doesn't meet the problem's conditions.In the third test case, the only circle with the persons with the numbers $$$2$$$ and $$$4$$$ looking at each other consists of $$$4$$$ people. Therefore, the person with the number $$$10$$$ doesn't occur in the circle."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nmy ($t) = map { $_ - 0 } split(/\\s+/o,);\r\nforeach my $tt ( 1 .. $t ){\r\n my ($a,$b,$c) = map { $_ - 0 } split(/\\s+/o,);\r\n if( $a == $b ){\r\n print \"-1\\n\"; next;\r\n }\r\n my $h = ($a > $b ? $a - $b : $b - $a);\r\n if( $c > $h * 2 or $a > $h * 2 or $b > $h * 2 ){\r\n print \"-1\\n\"; next;\r\n }\r\n my $r = ( ( $c - 1 + $h ) % ( $h * 2 ) ) + 1;\r\n print \"$r\\n\";\r\n}\r\n"}], "negative_code": [], "src_uid": "07597a8d08b59d4f8f82369bb5d74a49"} {"nl": {"description": "Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from \u2009-\u2009x to x. ", "input_spec": "The first line contains two integers: n (1\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of found cards and x (1\u2009\u2264\u2009x\u2009\u2264\u20091000) \u2014 the maximum absolute value of the number on a card. The second line contains n space-separated integers \u2014 the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.", "output_spec": "Print a single number \u2014 the answer to the problem.", "sample_inputs": ["3 2\n-1 1 2", "2 3\n-2 -2"], "sample_outputs": ["1", "2"], "notes": "NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value."}, "positive_code": [{"source_code": "my ($n, $x) = split / /, <>;\nmy @arr = split / /, <>;\nmy $sum;\n$sum += $_ for(@arr);\nprint int((abs($sum)+$x - 1) / $x);\n"}, {"source_code": "my ($n, $x) = split / /, <>;\nmy $sum = 0;\n$sum += $_ for(split / /, <>);\nprint int((abs($sum)+$x - 1) / $x);\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\nuse integer;\n\n($n, $x) = split / /, <>;\n$tot+=$_ foreach (split / /, <>);\nsay ((abs($tot) + $x-1)/$x);"}, {"source_code": "use integer;\n\n\nwhile(<>){\n\t\n\t($n,$x)=split/ /;\n\t$_=<>;\n\t$sum=0;\n\ts/-?\\d+/$sum+=$&/ge;\n#\tprint \"$sum\\n\"; \n\t\n\t$sum=abs $sum;\n#\tprint \"$sum\\n\"; \n\t$answer=$sum/$x + ($sum%$x? 1:0) ;\n\t\n\tprint \"$answer\\n\"; \n\n\t}\n\t\n"}, {"source_code": "use integer;\n($n,$x)=split/ /,<>;\n$_=<>;\ns/-?\\d+/$s+=$&/ge;\n$s=~s/-//;\nprint $s/$x + ($s%$x?1:0)\n\t"}], "negative_code": [{"source_code": "use integer;\n($n,$x)=split/ /,<>;\n$_=<>;\ns/-?\\d+/$s+=$&/ge;\n$s=~s/-//;\nprint $s/$x + ($s%$x)**0"}], "src_uid": "066906ee58af5163636dac9334619ea7"} {"nl": {"description": "Connect the countless points with lines, till we reach the faraway yonder.There are n points on a coordinate plane, the i-th of which being (i,\u2009yi).Determine whether it's possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set.", "input_spec": "The first line of input contains a positive integer n (3\u2009\u2264\u2009n\u2009\u2264\u20091\u2009000) \u2014 the number of points. The second line contains n space-separated integers y1,\u2009y2,\u2009...,\u2009yn (\u2009-\u2009109\u2009\u2264\u2009yi\u2009\u2264\u2009109) \u2014 the vertical coordinates of each point.", "output_spec": "Output \"Yes\" (without quotes) if it's possible to fulfill the requirements, and \"No\" otherwise. You can print each letter in any case (upper or lower).", "sample_inputs": ["5\n7 5 8 6 9", "5\n-1 -2 0 0 -5", "5\n5 4 3 2 1", "5\n1000000000 0 0 0 0"], "sample_outputs": ["Yes", "No", "No", "Yes"], "notes": "NoteIn the first example, there are five points: (1,\u20097), (2,\u20095), (3,\u20098), (4,\u20096) and (5,\u20099). It's possible to draw a line that passes through points 1,\u20093,\u20095, and another one that passes through points 2,\u20094 and is parallel to the first one.In the second example, while it's possible to draw two lines that cover all points, they cannot be made parallel.In the third example, it's impossible to satisfy both requirements at the same time."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy @A = split ' ', <>;\n\t\n\tmy @cand;\n\tpush @cand, $A[1] - $A[0], $A[2] - $A[1], ($A[2] - $A[0]) / 2;\n\t$debug and print \"cand:[@cand]\";\n\t\n\tmy $ok = 0;\n\t\n\tfor my $cand ( @cand ){\n\t\t\n\t\tmy $jump;\n\t\tmy $f = 0;\n\t\t\n\t\tfor( my $i = 0; $i < @A - 1; $i ++ ){\n\t\t\tmy $diff = $A[ $i+1 ] - $A[ $i ];\n\t\t\t\n\t\t\tif( $diff != $cand ){\n\t\t\t\tif( not defined $jump ){\n\t\t\t\t\t$jump = $diff - $cand;\n\t\t\t\t\t}\n\t\t\t\telsif( -$jump == $diff - $cand ){\n\t\t\t\t\t$jump *= -1;\n\t\t\t\t\t#OK\n\t\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\t$f ++;\n\t\t\t\t\tlast;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\tif( $f == 0 and defined $jump ){\n\t\t\t$ok = 1;\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tprint $ok ? \"YES\" : \"NO\";\n\t\n\t}"}, {"source_code": "\t<>;\n\t\n\t@A = split ' ', <>;\n\t\t\n\tfor $c ( $A[1] - $A[0], $A[2] - $A[1], ($A[2] - $A[0]) / 2 ){\n\t\t\n\t\tmy $j;\n\t\tmy $f;\n\t\t\n\t\tfor $i ( 1 .. @A - 1 ){\n\t\t\t$d = $A[ $i ] - $A[ $i-1 ];\n\t\t\t\n\t\t\tif( $d != $c ){\n\t\t\t\t! $j ?\n\t\t\t\t\t( $j = $d - $c )\n\t\t\t\t\t:\n\t\t\t\t-$j == $d - $c ?\n\t\t\t\t\t( $j *= -1 )\n\t\t\t\t\t:\n\t\t\t\t\t++ $f\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t$ok += ! $f && $j;\n\t\t\n\t\t}\n\t\n\tprint $ok ? \"YES\" : \"NO\";"}], "negative_code": [], "src_uid": "c54042eebaef01783a74d31521db9baa"} {"nl": {"description": "A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ of length $$$n$$$ containing each number exactly once. For example, $$$[1]$$$, $$$[4, 3, 5, 1, 2]$$$, $$$[3, 2, 1]$$$ are permutations, and $$$[1, 1]$$$, $$$[0, 1]$$$, $$$[2, 2, 1, 4]$$$ are not.There was a permutation $$$p[1 \\dots n]$$$. It was merged with itself. In other words, let's take two instances of $$$p$$$ and insert elements of the second $$$p$$$ into the first maintaining relative order of elements. The result is a sequence of the length $$$2n$$$.For example, if $$$p=[3, 1, 2]$$$ some possible results are: $$$[3, 1, 2, 3, 1, 2]$$$, $$$[3, 3, 1, 1, 2, 2]$$$, $$$[3, 1, 3, 1, 2, 2]$$$. The following sequences are not possible results of a merging: $$$[1, 3, 2, 1, 2, 3$$$], [$$$3, 1, 2, 3, 2, 1]$$$, $$$[3, 3, 1, 2, 2, 1]$$$.For example, if $$$p=[2, 1]$$$ the possible results are: $$$[2, 2, 1, 1]$$$, $$$[2, 1, 2, 1]$$$. The following sequences are not possible results of a merging: $$$[1, 1, 2, 2$$$], [$$$2, 1, 1, 2]$$$, $$$[1, 2, 2, 1]$$$.Your task is to restore the permutation $$$p$$$ by the given resulting sequence $$$a$$$. It is guaranteed that the answer exists and is unique.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 400$$$) \u2014 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$$$) \u2014 the length of permutation. The second line of the test case contains $$$2n$$$ integers $$$a_1, a_2, \\dots, a_{2n}$$$ ($$$1 \\le a_i \\le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the array $$$a$$$ represents the result of merging of some permutation $$$p$$$ with the same permutation $$$p$$$.", "output_spec": "For each test case, print the answer: $$$n$$$ integers $$$p_1, p_2, \\dots, p_n$$$ ($$$1 \\le p_i \\le n$$$), representing the initial permutation. It is guaranteed that the answer exists and is unique.", "sample_inputs": ["5\n2\n1 1 2 2\n4\n1 3 1 4 3 4 2 2\n5\n1 2 1 2 3 4 3 5 4 5\n3\n1 2 3 1 2 3\n4\n2 3 2 4 1 3 4 1"], "sample_outputs": ["1 2 \n1 3 4 2 \n1 2 3 4 5 \n1 2 3 \n2 3 4 1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy $test_count = ;\nchomp $test_count;\nmy @final_results;\n\nfor ($i = 0; $i < $test_count; ++$i) {\n my $seq_length = ;\n chomp($seq_length);\n my $line = ;\n chomp($line);\n my @elements = split(/ /,$line);\n my %seen;\n my @result;\n foreach (@elements) {\n unless ($seen{$_}) {\n $seen{$_} = true;\n push(@result,$_);\n }\n }\n push(@final_results,join(' ',@result));\n}\n\nprint join(\"\\n\", @final_results).\"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @a = map { $_ - 0 } split(/\\s+/,);\n \n my @r = ();\n \n for(my $i=0;$i<$n;$i++){\n my $v = shift @a;\n push(@r,$v);\n last if( $#a <= 0 );\n for(my $j=0;$j<=$#a;$j++){\n if( $a[$j] == $v ){\n my @newa = ();\n push(@newa,@a[0..$j-1]) if $j-1>=0;\n push(@newa,@a[$j+1..$#a]) if $j+1<=$#a;\n @a = (@newa);\n last;\n }\n }\n }\n print ( join(\" \",@r) . \"\\n\" );\n \n}\n\n"}], "negative_code": [], "src_uid": "aaf91874cf5aa0fa302ffed2ccecdc76"} {"nl": {"description": "You are given an integer $$$n$$$ and an array $$$a_1, a_2, \\ldots, a_n$$$. You should reorder the elements of the array $$$a$$$ in such way that the sum of $$$\\textbf{MEX}$$$ on prefixes ($$$i$$$-th prefix is $$$a_1, a_2, \\ldots, a_i$$$) is maximized.Formally, you should find an array $$$b_1, b_2, \\ldots, b_n$$$, such that the sets of elements of arrays $$$a$$$ and $$$b$$$ are equal (it is equivalent to array $$$b$$$ can be found as an array $$$a$$$ with some reordering of its elements) and $$$\\sum\\limits_{i=1}^{n} \\textbf{MEX}(b_1, b_2, \\ldots, b_i)$$$ is maximized.$$$\\textbf{MEX}$$$ of a set of nonnegative integers is the minimal nonnegative integer such that it is not in the set.For example, $$$\\textbf{MEX}(\\{1, 2, 3\\}) = 0$$$, $$$\\textbf{MEX}(\\{0, 1, 2, 4, 5\\}) = 3$$$.", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1 \\le t \\le 100)$$$ \u00a0\u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \\le n \\le 100)$$$. 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 print an array $$$b_1, b_2, \\ldots, b_n$$$ \u00a0\u2014 the optimal reordering of $$$a_1, a_2, \\ldots, a_n$$$, so the sum of $$$\\textbf{MEX}$$$ on its prefixes is maximized. If there exist multiple optimal answers you can find any.", "sample_inputs": ["3\n7\n4 2 0 1 3 3 7\n5\n2 2 8 6 9\n1\n0"], "sample_outputs": ["0 1 2 3 4 7 3 \n2 6 8 9 2 \n0"], "notes": "NoteIn the first test case in the answer $$$\\textbf{MEX}$$$ for prefixes will be: $$$\\textbf{MEX}(\\{0\\}) = 1$$$ $$$\\textbf{MEX}(\\{0, 1\\}) = 2$$$ $$$\\textbf{MEX}(\\{0, 1, 2\\}) = 3$$$ $$$\\textbf{MEX}(\\{0, 1, 2, 3\\}) = 4$$$ $$$\\textbf{MEX}(\\{0, 1, 2, 3, 4\\}) = 5$$$ $$$\\textbf{MEX}(\\{0, 1, 2, 3, 4, 7\\}) = 5$$$ $$$\\textbf{MEX}(\\{0, 1, 2, 3, 4, 7, 3\\}) = 5$$$ The sum of $$$\\textbf{MEX} = 1 + 2 + 3 + 4 + 5 + 5 + 5 = 25$$$. It can be proven, that it is a maximum possible sum of $$$\\textbf{MEX}$$$ on prefixes."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\t@_ = sort { $a <=> $b } keys %h;\n\t\n\tpush @_, map { ( $_ ) x ( $h{ $_ } - 1 ) } keys %h;\n\t\n\tprint \"@_\";\n\t}"}], "negative_code": [], "src_uid": "69838d9f9214c65b84a21d4eb546ea4b"} {"nl": {"description": "You are given a square grid with $$$n$$$ rows and $$$n$$$ columns. Each cell contains either $$$0$$$ or $$$1$$$. In an operation, you can select a cell of the grid and flip it (from $$$0 \\to 1$$$ or $$$1 \\to 0$$$). Find the minimum number of operations you need to obtain a square that remains the same when rotated $$$0^{\\circ}$$$, $$$90^{\\circ}$$$, $$$180^{\\circ}$$$ and $$$270^{\\circ}$$$.The picture below shows an example of all rotations of a grid. ", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$)\u00a0\u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$)\u00a0\u2014 the size of the grid. Then $$$n$$$ lines follow, each with $$$n$$$ characters $$$a_{i,j}$$$ ($$$0 \\leq a_{i,j} \\leq 1$$$)\u00a0\u2014 the number written in each cell.", "output_spec": "For each test case output a single integer \u00a0\u2014 the minimum number of operations needed to make the square look the same rotated $$$0^{\\circ}$$$, $$$90^{\\circ}$$$, $$$180^{\\circ}$$$ and $$$270^{\\circ}$$$.", "sample_inputs": ["5\n\n3\n\n010\n\n110\n\n010\n\n1\n\n0\n\n5\n\n11100\n\n11011\n\n01011\n\n10011\n\n11000\n\n5\n\n01000\n\n10101\n\n01010\n\n00010\n\n01001\n\n5\n\n11001\n\n00000\n\n11111\n\n10110\n\n01111"], "sample_outputs": ["1\n0\n9\n7\n6"], "notes": "NoteIn the first test case, we can perform one operations to make the grid $$$\\begin{matrix}0 & 1 & 0\\\\ 1 & 1 & \\color{red}{1}\\\\ 0 & 1 & 0\\end{matrix}$$$. Now, all rotations of the square are the same.In the second test case, all rotations of the square are already the same, so we don't need any flips."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = map { [ <> =~ m/./g ] } 1 .. $_;\n\t\n\tif( @_ % 2 == 0 ){\n\t\tsplice @_, @_ / 2, 0, [ ( '0' ) x @_ ];\n\t\t\n\t\tsplice @{ $_ }, @_ / 2, 0, '0' for @_;\n\t\t}\n\t\n\t$debug and print \"@{ $_ }\" for @_;\n\t\n\tmy $ans = 0;\n\t\n\tmy $c = int @_ / 2;\n\t\n\tfor my $i ( 1 .. @_ / 2 ){\n\t\tfor my $j ( 1 .. @_ / 2 ){\n\t\t\tmy $sum = 0;\n\t\t\t$sum += $_[ $c + $i ][ $c + $j ];\n\t\t\t$sum += $_[ $c - $i ][ $c - $j ];\n\t\t\t$sum += $_[ $c - $j ][ $c + $i ];\n\t\t\t$sum += $_[ $c + $j ][ $c - $i ];\n\t\t\t\n\t\t\t$ans += 2 - abs 2 - $sum;\n\t\t\t}\n\t\t}\n\t\n\tfor my $i ( 1 .. @_ / 2 ){\n\t\tmy $sum = 0;\n\t\t$sum += $_[ $c ][ $c + $i ];\n\t\t$sum += $_[ $c + $i ][ $c ];\n\t\t$sum += $_[ $c ][ $c - $i ];\n\t\t$sum += $_[ $c - $i ][ $c ];\n\t\t\n\t\t$ans += 2 - abs 2 - $sum;\n\t\t}\n\t\n\tprint $ans;\n\t}"}], "negative_code": [], "src_uid": "867b01e7141ef077964a8a0d4c6b762b"} {"nl": {"description": "Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set.", "input_spec": "A single line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100000) \u2014 the number of digits in the set. The second line contains n digits, the digits are separated by a single space. ", "output_spec": "On a single line print the answer to the problem. If such number does not exist, then you should print -1.", "sample_inputs": ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"], "sample_outputs": ["0", "5554443330", "-1"], "notes": "NoteIn the first sample there is only one number you can make \u2014 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp($n);\nmy $line = <>;\nchomp($line);\n\nmy @nums = split(/\\s/, $line);\nmy $data = +{};\nfor my $num (@nums) {\n $data->{$num}++;\n}\n\nif (!$data->{0}) {\n print \"-1\";\n exit;\n}\n\nmy $sum = get_sum($data);\n\nmy $rest = $sum % 3;\nmy $cnt = 0;\nwhile ($rest != 0 && $cnt < 2) {\n func($data, $rest);\n $rest = get_sum($data) % 3;\n $cnt++;\n}\n\nif ($rest == 0) {\n my $ans = '';\n for (my $i=9; $i >= 0; $i--) {\n if ($data->{$i}) {\n $ans .= $i x $data->{$i};\n }\n }\n\n if (substr($ans, 0, 1) eq '0') {\n print 0;\n }\n else {\n print $ans;\n }\n}\nelse {\n print \"-1\";\n}\n\nsub get_sum {\n $data = shift;\n my $sum = 0;\n for my $key (keys(%{$data})) {\n if ($data->{$key}) {\n $sum += $key * $data->{$key};\n }\n }\n\n return $sum;\n}\n\nsub func {\n my ($data, $rest) = @_;\n if ($rest == 1) {\n for my $i (1, 4, 7) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n for my $i (2, 5, 8) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n }\n if ($rest == 2) {\n for my $i (2, 5, 8) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n for my $i (1, 4, 7) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n }\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp($n);\nmy $line = <>;\nchomp($line);\n\nmy @nums = split(/\\s/, $line);\nmy $data = +{};\nfor my $num (@nums) {\n $data->{$num}++;\n}\n\nif (!$data->{0}) {\n print \"-1\";\n exit;\n}\n\nmy $sum = get_sum($data);\n\nmy $rest = $sum % 3;\nmy $cnt = 0;\nwhile ($rest != 0 && $cnt < 2) {\n func($data, $rest);\n $rest = get_sum($data) % 3;\n $cnt++;\n}\n\nif ($rest == 0) {\n for (my $i=9; $i >= 0; $i--) {\n if ($data->{$i}) {\n print $i x $data->{$i};\n }\n }\n}\nelse {\n print \"-1\";\n}\n\nsub get_sum {\n $data = shift;\n for my $key (keys(%{$data})) {\n if ($data->{$key}) {\n $sum += $key * $data->{$key};\n }\n }\n}\n\nsub func {\n my ($data, $rest) = @_;\n if ($rest == 1) {\n for my $i (1, 4, 7) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n for my $i (2, 5, 8) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n }\n if ($rest == 2) {\n for my $i (2, 5, 8) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n for my $i (1, 4, 7) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp($n);\nmy $line = <>;\nchomp($line);\n\nmy @nums = split(/\\s/, $line);\nmy $data = +{};\nfor my $num (@nums) {\n $data->{$num}++;\n}\n\nif (!$data->{0}) {\n print \"-1\";\n exit;\n}\n\nmy $sum = get_sum($data);\n\nmy $rest = $sum % 3;\nmy $cnt = 0;\nwhile ($rest != 0 && $cnt < 2) {\n func($data, $rest);\n $rest = get_sum($data) % 3;\n $cnt++;\n}\n\nif ($rest == 0) {\n my $ans = '';\n for (my $i=9; $i >= 0; $i--) {\n if ($data->{$i}) {\n $ans .= $i x $data->{$i};\n }\n }\n\n print int($ans);\n}\nelse {\n print \"-1\";\n}\n\nsub get_sum {\n $data = shift;\n my $sum = 0;\n for my $key (keys(%{$data})) {\n if ($data->{$key}) {\n $sum += $key * $data->{$key};\n }\n }\n\n return $sum;\n}\n\nsub func {\n my ($data, $rest) = @_;\n if ($rest == 1) {\n for my $i (1, 4, 7) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n for my $i (2, 5, 8) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n }\n if ($rest == 2) {\n for my $i (2, 5, 8) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n for my $i (1, 4, 7) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n }\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = <>;\nchomp($n);\nmy $line = <>;\nchomp($line);\n\nmy @nums = split(/\\s/, $line);\nmy $data = +{};\nfor my $num (@nums) {\n $data->{$num}++;\n}\n\nif (!$data->{0}) {\n print \"-1\";\n exit;\n}\n\nmy $sum = get_sum($data);\n\nmy $rest = $sum % 3;\nmy $cnt = 0;\nwhile ($rest != 0 && $cnt < 2) {\n func($data, $rest);\n $rest = get_sum($data) % 3;\n $cnt++;\n}\n\nif ($rest == 0) {\n for (my $i=9; $i >= 0; $i--) {\n if ($data->{$i}) {\n print $i x $data->{$i};\n }\n }\n}\nelse {\n print \"-1\";\n}\n\nsub get_sum {\n $data = shift;\n my $sum = 0;\n for my $key (keys(%{$data})) {\n if ($data->{$key}) {\n $sum += $key * $data->{$key};\n }\n }\n\n return $sum;\n}\n\nsub func {\n my ($data, $rest) = @_;\n if ($rest == 1) {\n for my $i (1, 4, 7) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n for my $i (2, 5, 8) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n }\n if ($rest == 2) {\n for my $i (2, 5, 8) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n for my $i (1, 4, 7) {\n if ($data->{$i}) {\n $data->{$i}--;\n return;\n }\n }\n }\n}\n"}], "src_uid": "b263917e47e1c84340bcb1c77999fd7e"} {"nl": {"description": "Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are n people who plan to move to the cities. The wealth of the i of them is equal to ai. Authorities plan to build two cities, first for n1 people and second for n2 people. Of course, each of n candidates can settle in only one of the cities. Thus, first some subset of candidates of size n1 settle in the first city and then some subset of size n2 is chosen among the remaining candidates and the move to the second city. All other candidates receive an official refuse and go back home.To make the statistic of local region look better in the eyes of their bosses, local authorities decided to pick subsets of candidates in such a way that the sum of arithmetic mean of wealth of people in each of the cities is as large as possible. Arithmetic mean of wealth in one city is the sum of wealth ai among all its residents divided by the number of them (n1 or n2 depending on the city). The division should be done in real numbers without any rounding.Please, help authorities find the optimal way to pick residents for two cities.", "input_spec": "The first line of the input contains three integers n, n1 and n2 (1\u2009\u2264\u2009n,\u2009n1,\u2009n2\u2009\u2264\u2009100\u2009000, n1\u2009+\u2009n2\u2009\u2264\u2009n)\u00a0\u2014 the number of candidates who want to move to the cities, the planned number of residents of the first city and the planned number of residents of the second city. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009100\u2009000), the i-th of them is equal to the wealth of the i-th candidate.", "output_spec": "Print one real value\u00a0\u2014 the maximum possible sum of arithmetic means of wealth of cities' residents. You answer will be considered correct if its absolute or relative error does not exceed 10\u2009-\u20096. 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": ["2 1 1\n1 5", "4 2 1\n1 4 2 3"], "sample_outputs": ["6.00000000", "6.50000000"], "notes": "NoteIn the first sample, one of the optimal solutions is to move candidate 1 to the first city and candidate 2 to the second.In the second sample, the optimal solution is to pick candidates 3 and 4 for the first city, and candidate 2 for the second one. Thus we obtain (a3\u2009+\u2009a4)\u2009/\u20092\u2009+\u2009a2\u2009=\u2009(3\u2009+\u20092)\u2009/\u20092\u2009+\u20094\u2009=\u20096.5"}, "positive_code": [{"source_code": "$/ = \"\"; $_ = ; ($n, $n1, $n2, @a) = split;\n\n@a = sort { $b <=> $a } @a;\n$i1 = $n1; $i2 = $n2;\n\nwhile ($i1 + $i2) {\n\t$v = $a[$i++];\n\t$f = ($v / $n1 > $v / $n2);\n\tif ( $f && !$i1) { $f = 0 }\n\tif (!$f && !$i2) { $f = 1 } \n\tif ($f) {\n\t\t$sum1 += $v;\n\t\t$i1--;\n\t} else {\n\t\t$sum2 += $v;\n\t\t$i2--;\n\t}\n}\n\n$ans = ($sum1 / $n1) + ($sum2 / $n2);\nprint $ans;"}, {"source_code": "use strict;\nuse warnings;\n#use Data::Dumper;\n\nmy ($n,$n1,$n2)=split / /,<>;\nchomp($n,$n1,$n2);\nmy @data=split / /,<>;\nchomp(@data);\n@data=sort {$b <=> $a } @data;\n($n1,$n2)=(min($n1,$n2),max($n1,$n2));\n#print STDERR $n1 . \",\" . $n2 . \"\\n\";\nmy $ans=0;\nmy $sum=0;\nmy $i=0;\nfor($i=0;$i<$n1;$i++)\n{\n $sum+=shift @data;\n}\n$ans=$sum/$n1;\n$sum=0;\nfor($i=0;$i<$n2;$i++)\n{\n $sum+=shift @data;\n}\n$ans+=$sum/$n2;\n\n$ans = sprintf(\"%.8f\", $ans);\n\nprint $ans . \"\\n\";\n\nsub min{\n\treturn $_[0] > $_[1] ? $_[1] : $_[0];\n}\n\nsub max{\n\treturn $_[1] > $_[0] ? $_[1] : $_[0];\n}\n"}, {"source_code": "( undef, $n[1], $n[2], @_ ) = map { sort {$b <=> $a} split } <>;\n\nprint 0 + eval join '+', map { \n ( eval join '+', @_[ $n[$_ + 1 ] .. $n[$_ + 1] + $n[$_] - 1 ] ) / $n[$_] \n } 2, 1"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $n1, $n2 ) = sort {$b <=> $a} split;\n\t\n\t@_ = sort {$b <=> $a} split ' ', <>;\n\t\n#%\tprint \"my( $n, $n1, $n2 )|@_\";\n\tprint\n\t\t+\n\t\t( eval join '+', @_[0 .. $n2 - 1] ) / $n2\n\t\t+\n\t\t( eval join '+', @_[$n2 .. $n2 + $n1 - 1] ) / $n1\n\t\t;\n\t\n\t}"}], "negative_code": [], "src_uid": "822e8f394a59329fa05c96d7fb35797e"} {"nl": {"description": "Fillomino is a classic logic puzzle. (You do not need to know Fillomino in order to solve this problem.) In one classroom in Yunqi town, some volunteers are playing a board game variant of it:Consider an $$$n$$$ by $$$n$$$ chessboard. Its rows are numbered from $$$1$$$ to $$$n$$$ from the top to the bottom. Its columns are numbered from $$$1$$$ to $$$n$$$ from the left to the right. A cell on an intersection of $$$x$$$-th row and $$$y$$$-th column is denoted $$$(x, y)$$$. The main diagonal of the chessboard is cells $$$(x, x)$$$ for all $$$1 \\le x \\le n$$$.A permutation of $$$\\{1, 2, 3, \\dots, n\\}$$$ is written on the main diagonal of the chessboard. There is exactly one number written on each of the cells. The problem is to partition the cells under and on the main diagonal (there are exactly $$$1+2+ \\ldots +n$$$ such cells) into $$$n$$$ connected regions satisfying the following constraints: Every region should be connected. That means that we can move from any cell of a region to any other cell of the same region visiting only cells of the same region and moving from a cell to an adjacent cell. The $$$x$$$-th region should contain cell on the main diagonal with number $$$x$$$ for all $$$1\\le x\\le n$$$. The number of cells that belong to the $$$x$$$-th region should be equal to $$$x$$$ for all $$$1\\le x\\le n$$$. Each cell under and on the main diagonal should belong to exactly one region. ", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1\\le n \\le 500$$$) denoting the size of the chessboard. The second line contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$. $$$p_i$$$ is the number written on cell $$$(i, i)$$$. It is guaranteed that each integer from $$$\\{1, \\ldots, n\\}$$$ appears exactly once in $$$p_1$$$, ..., $$$p_n$$$.", "output_spec": "If no solution exists, output $$$-1$$$. Otherwise, output $$$n$$$ lines. The $$$i$$$-th line should contain $$$i$$$ numbers. The $$$j$$$-th number on the $$$i$$$-th line should be $$$x$$$ if cell $$$(i, j)$$$ belongs to the the region with $$$x$$$ cells.", "sample_inputs": ["3\n2 3 1", "5\n1 2 3 4 5"], "sample_outputs": ["2\n2 3\n3 3 1", "1\n2 2\n3 3 3\n4 4 4 4\n5 5 5 5 5"], "notes": "NoteThe solutions to the examples are illustrated in the following pictures: "}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($n) = map { $_ - 0 } split(/\\s+/,);\r\nmy @p= map { $_ - 0 } split(/\\s+/,);\r\n\r\nmy @m = (); $#m = $n-1;\r\nfor(my $i=0;$i<$n;$i++){\r\n my @m1 = (); $#m = $n-1;\r\n push(@m,\\@m1);\r\n $m[$i]->[$i] = $p[$i];\r\n}\r\n\r\nfor(my $i=1;$i<$n;$i++){\r\n my $op = 0;\r\n for($op=0;$op<=$#p;$op++){\r\n last if $p[$op] == 1;\r\n }\r\n my @newp = map { $_ - 1 } ( @p[0..($op-1)] , @p[($op+1)..$#p] );\r\n for(my $j=0;$j<=$#newp;$j++){\r\n $m[$i+$j]->[$j] = $newp[$j]+$i;\r\n }\r\n @p = (@newp);\r\n}\r\n\r\nfor(my $i=0;$i<$n;$i++){\r\n my @ot = ();\r\n for(my $j=0;$j<=$i;$j++){\r\n push(@ot,$m[$i]->[$j]);\r\n }\r\n print (join(' ',@ot) . \"\\n\");\r\n}\r\nexit(0);\r\n\r\n"}], "negative_code": [], "src_uid": "5e95cb608bca3c3e9fc581b97f0dbc65"} {"nl": {"description": "Recently, Norge found a string $$$s = s_1 s_2 \\ldots s_n$$$ consisting of $$$n$$$ lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string $$$s$$$. Yes, all $$$\\frac{n (n + 1)}{2}$$$ of them!A substring of $$$s$$$ is a non-empty string $$$x = s[a \\ldots b] = s_{a} s_{a + 1} \\ldots s_{b}$$$ ($$$1 \\leq a \\leq b \\leq n$$$). For example, \"auto\" and \"ton\" are substrings of \"automaton\".Shortly after the start of the exercise, Norge realized that his keyboard was broken, namely, he could use only $$$k$$$ Latin letters $$$c_1, c_2, \\ldots, c_k$$$ out of $$$26$$$.After that, Norge became interested in how many substrings of the string $$$s$$$ he could still type using his broken keyboard. Help him to find this number.", "input_spec": "The first line contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$, $$$1 \\leq k \\leq 26$$$) \u2014 the length of the string $$$s$$$ and the number of Latin letters still available on the keyboard. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ lowercase Latin letters. The third line contains $$$k$$$ space-separated distinct lowercase Latin letters $$$c_1, c_2, \\ldots, c_k$$$ \u2014 the letters still available on the keyboard.", "output_spec": "Print a single number \u2014 the number of substrings of $$$s$$$ that can be typed using only available letters $$$c_1, c_2, \\ldots, c_k$$$.", "sample_inputs": ["7 2\nabacaba\na b", "10 3\nsadfaasdda\nf a d", "7 1\naaaaaaa\nb"], "sample_outputs": ["12", "21", "0"], "notes": "NoteIn the first example Norge can print substrings $$$s[1\\ldots2]$$$, $$$s[2\\ldots3]$$$, $$$s[1\\ldots3]$$$, $$$s[1\\ldots1]$$$, $$$s[2\\ldots2]$$$, $$$s[3\\ldots3]$$$, $$$s[5\\ldots6]$$$, $$$s[6\\ldots7]$$$, $$$s[5\\ldots7]$$$, $$$s[5\\ldots5]$$$, $$$s[6\\ldots6]$$$, $$$s[7\\ldots7]$$$."}, "positive_code": [{"source_code": "<>;\n$_ = <>, chomp;\n$ok = <>;\n\t\nprint eval join '+', 0, map $_ * ( $_ + 1 ) / 2, map length, split /[^$ok]/"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t$_ = <>, chomp;\n\tmy $ok = <>;\n\tchomp $ok;\n\t\n\tprint eval join '+', 0, map $_ * ( $_ + 1 ) / 2, map length, split /[^$ok]/, $_;\n\t}"}], "negative_code": [], "src_uid": "4c260e7c6fd9c573ee4f3b1822f3c7c3"} {"nl": {"description": "Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.", "input_spec": "The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct. Next follows number n (1\u2009\u2264\u2009n\u2009\u2264\u200990) \u2014 the number of fouls. Each of the following n lines contains information about a foul in the following form: first goes number t (1\u2009\u2264\u2009t\u2009\u2264\u200990) \u2014 the minute when the foul occurs; then goes letter \"h\" or letter \"a\" \u2014 if the letter is \"h\", then the card was given to a home team player, otherwise the card was given to an away team player; then goes the player's number m (1\u2009\u2264\u2009m\u2009\u2264\u200999); then goes letter \"y\" or letter \"r\" \u2014 if the letter is \"y\", that means that the yellow card was given, otherwise the red card was given. The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.", "output_spec": "For each event when a player received his first red card in a chronological order print a string containing the following information: The name of the team to which the player belongs; the player's number in his team; the minute when he received the card. If no player received a card, then you do not need to print anything. It is possible case that the program will not print anything to the output (if there were no red cards).", "sample_inputs": ["MC\nCSKA\n9\n28 a 3 y\n62 h 25 y\n66 h 42 y\n70 h 25 y\n77 a 4 y\n79 a 25 y\n82 h 42 r\n89 h 16 y\n90 a 13 r"], "sample_outputs": ["MC 25 70\nMC 42 82\nCSKA 13 90"], "notes": null}, "positive_code": [{"source_code": "chomp($h = <>);\nchomp($a = <>);\nfor (1..<>){\n\t$_=<>, chomp;\n\t/^(\\d+) ((\\w) (\\d+)) (\\w)$/;\n\t$h{$2} > 1 and next;\n\t$h{$2} += ($5 eq \"y\" ? 1 : 2);\n\t$h{$2} > 1 and push @h, ($3 eq \"h\"? $h : $a),\" $4 $1\\n\";\n\t}\nprint @h;"}], "negative_code": [], "src_uid": "b1f78130d102aa5f425e95f4b5b3a9fb"} {"nl": {"description": "'Jeopardy!' is an intellectual game where players answer questions and earn points. Company Q conducts a simplified 'Jeopardy!' tournament among the best IT companies. By a lucky coincidence, the old rivals made it to the finals: company R1 and company R2. The finals will have n questions, m of them are auction questions and n\u2009-\u2009m of them are regular questions. Each question has a price. The price of the i-th question is ai points. During the game the players chose the questions. At that, if the question is an auction, then the player who chose it can change the price if the number of his current points is strictly larger than the price of the question. The new price of the question cannot be less than the original price and cannot be greater than the current number of points of the player who chose the question. The correct answer brings the player the points equal to the price of the question. The wrong answer to the question reduces the number of the player's points by the value of the question price.The game will go as follows. First, the R2 company selects a question, then the questions are chosen by the one who answered the previous question correctly. If no one answered the question, then the person who chose last chooses again.All R2 employees support their team. They want to calculate what maximum possible number of points the R2 team can get if luck is on their side during the whole game (they will always be the first to correctly answer questions). Perhaps you are not going to be surprised, but this problem was again entrusted for you to solve.", "input_spec": "The first line contains two space-separated integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100;\u00a0m\u2009\u2264\u2009min(n,\u200930)) \u2014 the total number of questions and the number of auction questions, correspondingly. The second line contains n space-separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009107) \u2014 the prices of the questions. The third line contains m distinct integers bi (1\u2009\u2264\u2009bi\u2009\u2264\u2009n) \u2014 the numbers of auction questions. Assume that the questions are numbered from 1 to n.", "output_spec": "In the single line, print the answer to the problem \u2014 the maximum points the R2 company can get if it plays optimally well. It is guaranteed that the answer fits into the integer 64-bit signed type.", "sample_inputs": ["4 1\n1 3 7 5\n3", "3 2\n10 3 8\n2 3", "2 2\n100 200\n1 2"], "sample_outputs": ["18", "40", "400"], "notes": null}, "positive_code": [{"source_code": "use bigint;\n<>;\n@_=split/ /,<>;\n$_=<>;\nwhile(/\\d+/g){\n\t\n\t$X=$_[$&-1] if $_[$&-1]>$X;\n\t$_[$&-1]=0;\n\t\n\t}\n\t\n$Z+=$_ for @_;\n\n$Z > $X ? ($X=$Z,s/^/ /):($X+=$Z);\n\n$X*=2 while / /g;\n\nprint $X\n"}, {"source_code": "use bigint;\n<>;\n@_=split/ /,<>;\n$_=<>;\nwhile(/\\d+/g){\n\t\n\t$X=$_[$&-1] if $_[$&-1]>$X;\n\t$_[$&-1]=0;\n\t\n\t}\n\t\n$Z+=$_ for @_;\n\n$Z > $X ? ($X=$Z,s/^/ /):($X+=$Z);\n\n$X*=2 while / /g;\n\nprint $X\n"}, {"source_code": "use bigint;\n<>;\n@_=split/ /,<>;\n$_=<>;\nwhile(/\\d+/g){\n\t\n\t$X=$_[$&-1] if $_[$&-1]>$X;\n\t$_[$&-1]=0;\n\t\n\t}\n\t\n$Z+=$_ for @_;\n\n$Z > $X ? ($X=$Z,s/^/ /):($X+=$Z);\n\n$X*=2 while / /g;\n\nprint $X\n"}, {"source_code": "use bigint;\n<>;\n@_=split/ /,<>;\n$_=<>;\nwhile(/\\d+/g){\n\t\n\t$X=$_[$&-1] if $_[$&-1]>$X;\n\t$_[$&-1]=0;\n\t\n\t}\n\t\n$Z+=$_ for @_;\n\n$Z > $X ? ($X=$Z,s/^/ /):($X+=$Z);\n\n$X*=2 while / /g;\n\nprint $X\n"}, {"source_code": "use bigint;\n<>;\n@_=split/ /,<>;\n$_=<>;\nwhile(/\\d+/g){\n\t\n\t$X=$_[$&-1] if $_[$&-1]>$X;\n\t$_[$&-1]=0;\n\t\n\t}\n\t\n$Z+=$_ for @_;\n\n$Z > $X ? ($X=$Z,s/^/ /):($X+=$Z);\n\n$X*=2 while / /g;\n\nprint $X\n"}, {"source_code": "use bigint;\n<>;\n@_=split/ /,<>;\n$_=<>;\nwhile(/\\d+/g){\n\t\n\t$X=$_[$&-1] if $_[$&-1]>$X;\n\t$_[$&-1]=0;\n\t\n\t}\n\t\n$Z+=$_ for @_;\n\n$Z > $X ? ($X=$Z,s/^/ /):($X+=$Z);\n\n$X*=2 while / /g;\n\nprint $X\n"}, {"source_code": "use bigint;\n<>;\n@_=split/ /,<>;\n$_=<>;\nwhile(/\\d+/g){\n\t\n\t$X=$_[$&-1] if $_[$&-1]>$X;\n\t$_[$&-1]=0;\n\t\n\t}\n\t\n$Z+=$_ for @_;\n\n$Z > $X ? ($X=$Z,s/^/ /):($X+=$Z);\n\n$X*=2 while / /g;\n\nprint $X\n"}, {"source_code": "use bigint;\n<>;\n@_=split/ /,<>;\n$_=<>;\nwhile(/\\d+/g){\n\t\n\t$X=$_[$&-1] if $_[$&-1]>$X;\n\t$_[$&-1]=0;\n\t\n\t}\n\t\n$Z+=$_ for @_;\n\n$Z > $X ? ($X=$Z,s/^/ /):($X+=$Z);\n\n$X*=2 while / /g;\n\nprint $X"}, {"source_code": "use bigint;\n<>;\n@_=split/ /,<>;\n$_=<>;\nwhile(/\\d+/g){\n\t\n\t$X=$_[$&-1] if $_[$&-1]>$X;\n\t$_[$&-1]=0;\n\t\n\t}\n\t\n$Z+=$_ for @_;\n\n$Z > $X ? ($X=$Z,s/^/ /):($X+=$Z);\n\n$X*=2 while / /g;\n\nprint $X\n"}], "negative_code": [{"source_code": "<>;\n@_=split/ /,<>;\n$_=<>;\nwhile(/\\d+/g){\n\t\n\t$X=$_[$&-1] if $_[$&-1]>$X;\n\t$_[$&-1]=0;\n\t\n\t}\n\t\n$Z+=$_ for @_;\n\n$Z > $X ? ($X=$Z,s/^/ /):($X+=$Z);\n\n$X*=2 while / /g;\n\nprint $X"}], "src_uid": "913925f7b43ad737809365eba040e8da"} {"nl": {"description": "The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i,\u2009j).You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri,\u2009ai,\u2009bi (ai\u2009\u2264\u2009bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.Your task is to find the minimum number of moves the king needs to get from square (x0,\u2009y0) to square (x1,\u2009y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.", "input_spec": "The first line contains four space-separated integers x0,\u2009y0,\u2009x1,\u2009y1 (1\u2009\u2264\u2009x0,\u2009y0,\u2009x1,\u2009y1\u2009\u2264\u2009109), denoting the initial and the final positions of the king. The second line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri,\u2009ai,\u2009bi (1\u2009\u2264\u2009ri,\u2009ai,\u2009bi\u2009\u2264\u2009109,\u2009ai\u2009\u2264\u2009bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily. It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.", "output_spec": "If there is no path between the initial and final position along allowed cells, print -1. Otherwise print a single integer \u2014 the minimum number of moves the king needs to get from the initial position to the final one.", "sample_inputs": ["5 7 6 11\n3\n5 3 8\n6 7 11\n5 2 5", "3 4 3 10\n3\n3 1 4\n4 5 9\n3 10 10", "1 1 2 10\n2\n1 1 3\n2 6 10"], "sample_outputs": ["4", "6", "-1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nmy $inf = 0x3f3f3f3f;\nmy @dx = (-1, -1, -1, 0, 0, 1, 1, 1);\nmy @dy = (-1, 0, 1, -1, 1, -1, 0, 1);\n\nchomp($_ = <>);\nmy ($x1, $y1, $x2, $y2) = split;\nchomp(my $n = <>);\nmy %d = ();\nfor (1 .. $n) {\n chomp($_ = <>);\n my ($r, $a, $b) = split;\n for my $i ($a .. $b) {\n $d{\"$r $i\"} = $inf;\n }\n}\n\n$d{\"$x1 $y1\"} = 0;\nmy @queue = ();\npush @queue, \"$x1 $y1\";\nwhile (@queue) {\n my ($x, $y) = split /\\s/, shift @queue;\n for my $i (0 .. 7) {\n my ($nx, $ny) = ($x + $dx[$i], $y + $dy[$i]);\n next unless exists $d{\"$nx $ny\"};\n next unless $d{\"$nx $ny\"} == $inf;\n $d{\"$nx $ny\"} = $d{\"$x $y\"} + 1;\n push @queue, \"$nx $ny\";\n }\n}\nif ($d{\"$x2 $y2\"} != $inf) {\n print $d{\"$x2 $y2\"}, \"\\n\";\n} else {\n print \"-1\\n\";\n}\n"}], "negative_code": [], "src_uid": "c3cbc9688594d6611fd7bdd98d9afaa0"} {"nl": {"description": "You are given $$$n$$$ of integers $$$a_1, a_2, \\ldots, a_n$$$. Process $$$q$$$ queries of two types: query of the form \"0 $$$x_j$$$\": add the value $$$x_j$$$ to all even elements of the array $$$a$$$, query of the form \"1 $$$x_j$$$\": add the value $$$x_j$$$ to all odd elements of the array $$$a$$$.Note that when processing the query, we look specifically at the odd/even value of $$$a_i$$$, not its index.After processing each query, print the sum of the elements of the array $$$a$$$.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++).", "input_spec": "The first line of the input contains an integer $$$t$$$ $$$(1 \\leq t \\leq 10^4$$$) \u2014 the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 \\leq n$$$, $$$q \\leq 10^5$$$) \u2014 the length of array $$$a$$$ and the number of queries. 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$$$) \u2014 elements of the array $$$a$$$. The following $$$q$$$ lines contain queries as two integers $$$type_j$$$ and $$$x_j$$$ $$$(0 \\leq type_j \\leq 1$$$, $$$1 \\leq x_j \\leq 10^4$$$). It is guaranteed that the sum of values $$$n$$$ over all test cases in a test does not exceed $$$10^5$$$. Similarly, the sum of values $$$q$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print $$$q$$$ numbers: the sum of the elements of the array $$$a$$$ after processing a query.", "sample_inputs": ["4\n\n1 1\n\n1\n\n1 1\n\n3 3\n\n1 2 4\n\n0 2\n\n1 3\n\n0 5\n\n6 7\n\n1 3 2 4 10 48\n\n1 6\n\n0 5\n\n0 4\n\n0 5\n\n1 3\n\n0 12\n\n0 1\n\n6 7\n\n1000000000 1000000000 1000000000 11 15 17\n\n0 17\n\n1 10000\n\n1 51\n\n0 92\n\n0 53\n\n1 16\n\n0 1"], "sample_outputs": ["2\n11\n14\n29\n80\n100\n100\n100\n118\n190\n196\n3000000094\n3000060094\n3000060400\n3000060952\n3000061270\n3000061366\n3000061366"], "notes": "NoteIn the first test case, the array $$$a = [2]$$$ after the first query.In the third test case, the array $$$a$$$ is modified as follows: $$$[1, 3, 2, 4, 10, 48]$$$ $$$\\rightarrow$$$ $$$[7, 9, 2, 4, 10, 48]$$$ $$$\\rightarrow$$$ $$$[7, 9, 7, 9, 15, 53]$$$ $$$\\rightarrow$$$ $$$[7, 9, 7, 9, 15, 53]$$$ $$$\\rightarrow$$$ $$$[10, 12, 10, 12, 18, 56]$$$ $$$\\rightarrow$$$ $$$[22, 24, 22, 24, 30, 68]$$$ $$$\\rightarrow$$$ $$$[23, 25, 23, 25, 31, 69]$$$."}, "positive_code": [{"source_code": "<>;\r\n\r\nwhile(<>){\r\n\t( $n, $q, $Z, @L ) = split;\r\n\t\r\n\t$Z += $_, ++ $L[ $_ % 2 ] for split ' ', <>;\r\n\t\r\n\tfor( 1 .. $q ){\r\n\t\t( $t, $x ) = split ' ', <>;\r\n\t\t\r\n\t\t$Z += $x * $L[ $t % 2 ];\r\n\t\t$x % 2 and @L[ $t % 2, 1 - $t % 2 ] = ( 0, $n );\r\n\t\t\r\n\t\tprint $Z . $/\r\n\t\t}\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $q ) = split;\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t$sum += $_ for @_;\n\t\n\tmy $N = grep $_ % 2, @_;\n\tmy $L = @_ - $N;\n\t\n\tfor( 1 .. $q ){\n\t\tmy( $t, $x ) = split ' ', <>;\n\t\t\n\t\tif( !$t ){\n\t\t\t$sum += $L * $x;\n\t\t\tif( $x % 2 ){\n\t\t\t\t$L = 0;\n\t\t\t\t$N = @_;\n\t\t\t\t}\n\t\t\t}\n\t\tif( $t ){\n\t\t\t$sum += $N * $x;\n\t\t\tif( $x % 2 ){\n\t\t\t\t$L = @_;\n\t\t\t\t$N = 0;\n\t\t\t\t}\n\t\t\t}\n\t\tprint $sum;\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $q ) = split;\n\t@_ = split ' ', <>;\n\t$debug and print \"[@_]\";\n\t\n\tmy $sum = 0;\n\t$sum += $_ for @_;\n\t\n\tmy $LYG = grep $_ % 2 == 0, @_;\n\tmy $NELYG = @_ - $LYG;\n\t$debug and print \"LYG[$LYG], NELYG:[$NELYG]\";\n\t\n\tfor my $qq ( 1 .. $q ){\n\t\tmy( $t, $x ) = split ' ', <>;\n\t\t\n\t\tif( $t == 0 ){\n\t\t\t$sum += $LYG * $x;\n\t\t\tif( $x % 2 == 1 ){\n\t\t\t\t$LYG = 0;\n\t\t\t\t$NELYG = @_;\n\t\t\t\t}\n\t\t\t}\n\t\tif( $t == 1 ){\n\t\t\t$sum += $NELYG * $x;\n\t\t\tif( $x % 2 == 1 ){\n\t\t\t\t$LYG = @_;\n\t\t\t\t$NELYG = 0;\n\t\t\t\t}\n\t\t\t}\n\t\tprint $sum;\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "d15cffca07768f8ce6fab7e13a6e7976"} {"nl": {"description": "Ayrat has number n, represented as it's prime factorization pi of size m, i.e. n\u2009=\u2009p1\u00b7p2\u00b7...\u00b7pm. Ayrat got secret information that that the product of all divisors of n taken modulo 109\u2009+\u20097 is the password to the secret data base. Now he wants to calculate this value.", "input_spec": "The first line of the input contains a single integer m (1\u2009\u2264\u2009m\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the number of primes in factorization of n. The second line contains m primes numbers pi (2\u2009\u2264\u2009pi\u2009\u2264\u2009200\u2009000).", "output_spec": "Print one integer\u00a0\u2014 the product of all divisors of n modulo 109\u2009+\u20097.", "sample_inputs": ["2\n2 3", "3\n2 3 2"], "sample_outputs": ["36", "1728"], "notes": "NoteIn the first sample n\u2009=\u20092\u00b73\u2009=\u20096. The divisors of 6 are 1, 2, 3 and 6, their product is equal to 1\u00b72\u00b73\u00b76\u2009=\u200936.In the second sample 2\u00b73\u00b72\u2009=\u200912. The divisors of 12 are 1, 2, 3, 4, 6 and 12. 1\u00b72\u00b73\u00b74\u00b76\u00b712\u2009=\u20091728."}, "positive_code": [{"source_code": "use strict;\n\nour ($m1, @p, @k, @pc);\nour $K = 1e9 + 7;\nour $L;\n\n# n = P(1..m) p[i] = P(1..m1) p[i]^k[i]\n# P d = P(1..m1) p[i]^c[i]\n# c[i] = (1 + k[i]) * k[i]/2 * P (1 + k[j])\n# c[i] = 1/2 * k[i] * L, L = P (1 + k[i])\n# p^c = (p^k)^L^1/2\n# p^c mod K = (p^k mod K) * (p^L mod K) mod K\n# (P d) mod K = (P p[i]^c[i] mod K) mod K \n# = (P p^(1/2kL) mod K) mod K\n# a) = (P p^k mod K)^(L/2) mod K\n# b) = (P p^(k/2) mod K)^L mod K\n\n# Fermat's little theorem a^(k - 1) mod k = 1\n# p^L (mod K) = p^(q * K + r) = p^(q * (K - 1) + q + r) = p^(q + r) (mod K)\n# L = P (1 + k) -> q + r\nsub calc_l() {\n\tmy $q = 1;\n\tmy $q_even = 0;\n\tfor my $k (@k) {\n\t\t# (1e9 + 7) * 200_000 < 2^53 (double)\n\t\tif ($q_even == 0 && $k % 2 == 1) {\n\t\t\t$q *= ($k + 1) / 2;\n\t\t\t$q_even = 1;\n\t\t} else {\n\t\t\t$q *= $k + 1;\n\t\t}\n\t\tif ($q > $K) {\n\t\t\tmy $qi = int($q / $K);\n\t\t\tmy $ri = $q % $K;\n\t\t\t$q = $qi + $ri;\n\t\t}\n\t}\n\twhile ($q > $K) {\n\t\tmy $qi = int($q / $K);\n\t\tmy $ri = $q % $K;\n\t\t$q = $qi + $ri;\n\t}\n\t$L = $q;\n\tif ($q_even == 0) {\n\t\t$_ /= 2 for @k;\n\t}\n}\n\n# a^b mod c\n# https://en.wikipedia.org/wiki/Exponentiation_by_squaring\nsub modpow($$$) {\n\tmy ($a, $b, $c) = @_;\n\t$b == 0 && return 1;\n\tmy $r = $a;\n\tmy $r2 = 1;\n\twhile ($b > 1) {\n\t\tif ($b % 2 == 0) {\n\t\t\t$r = ab_mod($r, $r, $c);\n\t\t\t$b = $b / 2;\n\t\t} else {\n\t\t\t$r2 = ab_mod($r2, $r, $c);\n\t\t\t$r = ab_mod($r, $r, $c);\n\t\t\t$b = ($b - 1) / 2;\n\t\t}\n\t}\n\tab_mod($r, $r2, $c);\n}\n\n# ab = (ah*B + al)(bh*B + bl) = ah*bh*BB + (ah*bl + al*bh)*B + al*bl\nsub ab_mod($$$) {\n\tmy ($a, $b, $c) = @_;\n\tmy $al = $a & 0xffff;\n\tmy $bl = $b & 0xffff;\n\tmy $ah = ($a & 0xffff0000) >> 16;\n\tmy $bh = ($b & 0xffff0000) >> 16;\n\tmy $d1 = (($ah * $bh) << 16) % $c; $d1 = ($d1 << 16) % $c;\n\tmy $d2 = (($ah * $bl + $al * $bh) << 16) % $c;\n\tmy $d3 = ($al * $bl) % $c;\n\t($d1 + $d2 + $d3) % $c;\n}\n\nsub calc_pc {\n\tfor (my $i = 0; $i < $m1; $i++) {\n\t\t$pc[$i] = modpow($p[$i], $k[$i], $K);\n\t}\n}\n\nsub calc_r {\n\t# log((1e9 + 7)**2)/log(2) = 59.8 > 53\n\tmy $r = 1;\n\tfor (my $i = 0; $i < $m1; $i++) {\n\t\t$r = ab_mod($r, $pc[$i], $K);\n\t}\n\t$r = modpow($r, $L, $K);\n\tprint $r, \"\\n\";\n}\n\nsub input {\n\t<>; $_ = <>;\n\tmy @p2 = split;\n\tmy %p; $p{$_}++ for @p2;\n\tfor (sort keys %p) {\n\t\tpush(@p, $_);\n\t\tpush(@k, $p{$_});\n\t}\n\t$m1 = @p;\n}\n\ninput();\ncalc_l();\ncalc_pc();\ncalc_r();\n\n"}], "negative_code": [{"source_code": "use strict;\n\nour ($m1, @p, @k, @pc);\nour $K = 1e9 + 7;\nour $L;\n\n# n = P(1..m) p[i] = P(1..m1) p[i]^k[i]\n# P d = P(1..m1) p[i]^c[i]\n# c[i] = (1 + k[i]) * k[i]/2 * P (1 + k[j])\n# c[i] = 1/2 * k[i] * L, L = P (1 + k[i])\n# p^c = (p^k)^L^1/2\n# p^c mod K = (p^k mod K) * (p^L mod K) mod K\n# (P d) mod K = (P p[i]^c[i] mod K) mod K \n# = (P p^(1/2kL) mod K) mod K\n# a) = (P p^k mod K)^(L/2) mod K\n# b) = (P p^(k/2) mod K)^L mod K\n\n# Fermat's little theorem a^(k - 1) mod k = 1\n# p^L (mod K) = p^(q * K + r) = p^(q * (K - 1) + q + r) = p^(q + r) (mod K)\n# L = P (1 + k) -> q + r\nsub calc_l() {\n\tmy $q = 1;\n\tfor my $k (@k) {\n\t\t# (1e9 + 7) * 200_000 < 2^53 (double)\n\t\t$q *= $k + 1; \n\t\tif ($q > $K) {\n\t\t\tmy $qi = int($q / $K);\n\t\t\tmy $ri = $q % $K;\n\t\t\t$q = $qi + $ri;\n\t\t}\n\t}\n\twhile ($q > $K) {\n\t\tmy $qi = int($q / $K);\n\t\tmy $ri = $q % $K;\n\t\t$q = $qi + $ri;\n\t}\n\t$L = $q;\n\tif ($L % 2 == 0) {\n\t\t$L /= 2;\n\t} else {\n\t\t$_ /= 2 for @k;\n\t}\n}\n\n# a^b mod c\n# https://en.wikipedia.org/wiki/Exponentiation_by_squaring\nsub modpow($$$) {\n\tmy ($a, $b, $c) = @_;\n\t$b == 0 && return 1;\n\tmy $r = $a;\n\tmy $r2 = 1;\n\twhile ($b > 1) {\n\t\tif ($b % 2 == 0) {\n\t\t\t$r = ab_mod($r, $r, $c);\n\t\t\t$b = $b / 2;\n\t\t} else {\n\t\t\t$r2 = ab_mod($r2, $r, $c);\n\t\t\t$r = ab_mod($r, $r, $c);\n\t\t\t$b = ($b - 1) / 2;\n\t\t}\n\t}\n\tab_mod($r, $r2, $c);\n}\n\n# ab = (ah*B + al)(bh*B + bl) = ah*bh*BB + (ah*bl + al*bh)*B + al*bl\nsub ab_mod($$$) {\n\tmy ($a, $b, $c) = @_;\n\tmy $al = $a & 0xffff;\n\tmy $bl = $b & 0xffff;\n\tmy $ah = ($a & 0xffff0000) >> 16;\n\tmy $bh = ($b & 0xffff0000) >> 16;\n\tmy $d1 = (($ah * $bh) << 16) % $c; $d1 = ($d1 << 16) % $c;\n\tmy $d2 = (($ah * $bl + $al * $bh) << 16) % $c;\n\tmy $d3 = ($al * $bl) % $c;\n\t($d1 + $d2 + $d3) % $c;\n}\n\nsub calc_pc {\n\tfor (my $i = 0; $i < $m1; $i++) {\n\t\t$pc[$i] = modpow($p[$i], $k[$i], $K);\n\t}\n}\n\nsub calc_r {\n\t# log((1e9 + 7)**2)/log(2) = 59.8 > 53\n\tmy $r = 1;\n\tfor (my $i = 0; $i < $m1; $i++) {\n\t\t$r = ab_mod($r, $pc[$i], $K);\n\t}\n\t$r = modpow($r, $L, $K);\n\tprint $r, \"\\n\";\n}\n\nsub input {\n\t<>; $_ = <>;\n\tmy @p2 = split;\n\tmy %p; $p{$_}++ for @p2;\n\tfor (sort keys %p) {\n\t\tpush(@p, $_);\n\t\tpush(@k, $p{$_});\n\t}\n\t$m1 = @p;\n}\n\ninput();\ncalc_l();\ncalc_pc();\ncalc_r();\n\n"}], "src_uid": "9a5bd9f937da55c3d26d5ecde6e50280"} {"nl": {"description": "President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One day President decided to establish an assembly, of which all his deputies will be members. Unfortunately, he does not remember the exact amount of his deputies, but he remembers that the desk of each his deputy is adjacent to his own desk, that is to say, the two desks (President's and each deputy's) have a common side of a positive length.The office-room plan can be viewed as a matrix with n rows and m columns. Each cell of this matrix is either empty, or contains a part of a desk. An uppercase Latin letter stands for each desk colour. The \u00abperiod\u00bb character (\u00ab.\u00bb) stands for an empty cell. ", "input_spec": "The first line contains two separated by a space integer numbers n, m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100) \u2014 the length and the width of the office-room, and c character \u2014 the President's desk colour. The following n lines contain m characters each \u2014 the office-room description. It is guaranteed that the colour of each desk is unique, and each desk represents a continuous subrectangle of the given matrix. All colours are marked by uppercase Latin letters.", "output_spec": "Print the only number \u2014 the amount of President's deputies.", "sample_inputs": ["3 4 R\nG.B.\n.RR.\nTTT.", "3 3 Z\n...\n.H.\n..Z"], "sample_outputs": ["2", "0"], "notes": null}, "positive_code": [{"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nmy ($n, $m, $color) = (split / /, <>);\nchomp $color;\nmy ($ps, $ns) = ('', '');\nmy @s;\nfor (1..$n) {\n my $rs = <>;\n chomp $rs;\n if ($rs =~ /$color/) {\n\tpush @s, $rs;\n } elsif (@s) {\n\t$ns = $rs;\n\tlast;\n } else {\n\t$ps = $rs;\n }\n}\n\nmy %tables;\n# left and right tables\nfor (@s) {\n if (/([^$color])$color/) {\n\t$tables{$1} = 1 if $1 ne '.';\n }\n if (/$color([^$color])/) {\n\t$tables{$1} = 1 if $1 ne '.';\n }\n}\n\n# tables in previous row\nif ($ps ne '') {\n my @ps = split //, $ps;\n my @fs = split //, $s[0];\n for (0..$#ps) {\n\tif ($fs[$_] eq $color and $ps[$_] ne '.') {\n\t $tables{$ps[$_]} = 1;\n\t}\n }\n}\n# tables in next row\nif ($ns ne '') {\n my @ns = split //, $ns;\n my @ls = split //, $s[$#s];\n for (0..$#ns) {\n\tif ($ls[$_] eq $color and $ns[$_] ne '.') {\n\t $tables{$ns[$_]} = 1;\n\t}\n }\n}\n# all tables\n\nmy $sum = keys %tables;\nprint \"$sum\\n\";\n"}, {"source_code": "#!usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $row;my ($n,$m,$c)=split/ /,;my $ps=\"\";my $ns=\"\";my @s;\nchomp($c);\nfor my $i(1..$n){\n $row=;\n chomp($row);\n if($row=~/$c/){push @s,$row;}\n elsif(scalar @s){$ns=$row;last;}\n else{$ps=$row;}\n}\nmy %t;my $f=0;\nfor(0..@s-1){\n if($s[$_]=~/([^$c\\.])$c/){$t{$1}=1;}\n if($s[$_]=~/$c([^$c\\.])/){$t{$1}=1;}\n}\nmy @ps=split //,$ps;my @ns=split //,$ns;my @fs=split //,$s[0];my @ls=split //,$s[-1];\nif($ps ne \"\"){\n for(0..$#ps){\n\tif($ps[$_] ne \".\"&&$fs[$_]eq$c){$t{$ps[$_]}=1;}\n }\n}\nif($ns ne \"\"){for(0..$#ns){if($ns[$_] ne \".\"&&$ls[$_]eq$c){$t{$ns[$_]}=1;}}}\n$f=keys %t;\nprint \"$f\\n\";"}], "negative_code": [{"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nmy ($n, $m, $color) = (split / /, <>);\nchomp $color;\nmy ($ps, $s, $ns) = ('', '', '');\nfor (1..$n) {\n $s = <>;\n chomp $s;\n if ($s =~ /$color/) {\n\t$ns = <> if $_ < $n;\n\tchomp $ns;\n\tlast;\n } else {\n\t$ps = $s;\n }\n}\n\nmy %tables;\n# left table\nif ($s =~ /([^$color])$color/) {\n $tables{$1} = 1 if $1 ne '.';\n}\n# right table\nif ($s =~ /$color([^$color])/) {\n $tables{$1} = 1 if $1 ne '.';\n}\n\n# tables in previous row\nif ($ps ne '') {\n my @ps = split //, $ps;\n my @s = split //, $s;\n for (0..$#ps) {\n\tif ($s[$_] eq $color and $ps[$_] ne '.') {\n\t $tables{$ps[$_]} = 1;\n\t}\n }\n}\n# tables in next row\nif ($ns ne '') {\n my @ns = split //, $ns;\n my @s = split //, $s;\n for (0..$#ns) {\n\tif ($s[$_] eq $color and $ns[$_] ne '.') {\n\t $tables{$ns[$_]} = 1;\n\t}\n }\n}\n# all tables\n\nmy $sum = keys %tables;\nprint \"$sum\\n\";\n"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nmy ($n, $m, $color) = (split / /, <>);\nchomp $color;\nmy ($ps, $ns) = ('', '');\nmy @s;\nfor (1..$n) {\n my $rs = <>;\n chomp $rs;\n if ($rs =~ /$color/) {\n\tpush @s, $rs;\n } elsif (@s) {\n\t$ns = $rs;\n\tlast;\n } else {\n\t$ps = $rs;\n }\n}\n\nmy %tables;\n# left and right tables\nfor (@s) {\n if (/([^$color])$color/) {\n\t$tables{$1} = 1 if $1 ne '.';\n }\n if (/$color(^$color])/) {\n\t$tables{$1} = 1 if $1 ne '.';\n }\n}\n\n# tables in previous row\nif ($ps ne '') {\n my @ps = split //, $ps;\n my @fs = split //, $s[0];\n for (0..$#ps) {\n\tif ($fs[$_] eq $color and $ps[$_] ne '.') {\n\t $tables{$ps[$_]} = 1;\n\t}\n }\n}\n# tables in next row\nif ($ns ne '') {\n my @ns = split //, $ns;\n my @ls = split //, $s[$#s];\n for (0..$#ns) {\n\tif ($ls[$_] eq $color and $ns[$_] ne '.') {\n\t $tables{$ns[$_]} = 1;\n\t}\n }\n}\n# all tables\n\nmy $sum = keys %tables;\nprint \"$sum\\n\";\n"}, {"source_code": "#!usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $row;my ($n,$m,$c)=split/ /,;my $ps=\"\";my $ns=\"\";my @s;\nchomp($c);\nfor my $i(1..$n){\n $row=;\n chomp($row);\n if($row=~/$c/){push @s,$row;}\n elsif(scalar @s){$ns=$row;last;}\n else{$ps=$row;}\n}\nmy %t;my $f=0;\nfor(0..@s-1){\n if($s[$_]=~/([^$c\\.])$c/){$t{$1}=1;}\n if($s[$_]=~/$c([^$c\\.])/){$t{$1}=1;}\n}\nmy @ps=split//,$ps;my @ns=split//,$ns;my @fs=split //,$s[0];my @ls=split //,$s[-1];\nif(scalar @ps){\n for my $i(0..@ps-1){\n\tif($ps[$i]ne\".\"&&$fs[$i]eq$c){$t{$ps[$i]}=1;}\n }\n}\nif(scalar @ns){for my $i(0..@ps-1){if($ns[$i]ne \".\"&&$ls[$i]eq$c){$t{$ps[$i]}=1;}}}\n$f++ for(keys %t);\nprint \"$f\\n\";"}], "src_uid": "d7601c9bb06a6852f04957fbeae54925"} {"nl": {"description": "You are given an array of integers $$$a_1,a_2,\\ldots,a_n$$$. Find the maximum possible value of $$$a_ia_ja_ka_la_t$$$ among all five indices $$$(i, j, k, l, t)$$$ ($$$i<j<k<l<t$$$).", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1\\le t\\le 2 \\cdot 10^4$$$) \u2014 the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$5\\le n\\le 10^5$$$) \u2014 the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$-3\\times 10^3\\le a_i\\le 3\\times 10^3$$$) \u2014 given array. It's 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 \u2014 the answer to the problem.", "sample_inputs": ["4\n5\n-1 -2 -3 -4 -5\n6\n-1 -2 -3 1 2 -1\n6\n-1 0 0 0 -1 -1\n6\n-9 -7 -5 -3 -2 1"], "sample_outputs": ["-120\n12\n0\n945"], "notes": "NoteIn the first test case, choosing $$$a_1,a_2,a_3,a_4,a_5$$$ is a best choice: $$$(-1)\\cdot (-2) \\cdot (-3)\\cdot (-4)\\cdot (-5)=-120$$$.In the second test case, choosing $$$a_1,a_2,a_3,a_5,a_6$$$ is a best choice: $$$(-1)\\cdot (-2) \\cdot (-3)\\cdot 2\\cdot (-1)=12$$$.In the third test case, choosing $$$a_1,a_2,a_3,a_4,a_5$$$ is a best choice: $$$(-1)\\cdot 0\\cdot 0\\cdot 0\\cdot (-1)=0$$$.In the fourth test case, choosing $$$a_1,a_2,a_3,a_4,a_6$$$ is a best choice: $$$(-9)\\cdot (-7) \\cdot (-5)\\cdot (-3)\\cdot 1=945$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nmy $minv = (-3001) ** 5;\n\nfor( my $t_loop=0; $t_loop<$t; $t_loop++ ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @aa = map { $_ - 0 } split(/\\s+/,);\n \n my @sa = sort { $b <=> $a } @aa;\n \n my $pl = 0; my $mi = 0;\n for(my $i=0;$i<$n;$i++){\n $pl++ if( $sa[$i]>0 );\n $mi++ if( $sa[$i]<0 );\n }\n \n my $res = $minv;\n if( $pl >= 5 ){\n $res = $sa[0] * $sa[1] * $sa[2] * $sa[3] * $sa[4];\n if( $mi >= 2 ){\n my $v2 = $sa[0] * $sa[1] * $sa[2] * $sa[$#sa-1] * $sa[$#sa];\n $res = $v2 if $v2 > $res;\n }\n if( $mi >= 4 ){\n my $v3 = $sa[0] * $sa[$#sa-3] * $sa[$#sa-2] * $sa[$#sa-1] * $sa[$#sa];\n $res = $v3 if $v3 > $res;\n }\n } elsif( $pl >= 3 ){\n if( $mi >= 4 ){\n my $v1 = $sa[0] * $sa[$#sa-3] * $sa[$#sa-2] * $sa[$#sa-1] * $sa[$#sa];\n $res = $v1 if $v1 > $res;\n }\n if( $mi >= 2 ){\n my $v2 = $sa[0] * $sa[1] * $sa[2] * $sa[$#sa-1] * $sa[$#sa];\n $res = $v2 if $v2 > $res;\n }\n if( $res < 0 ){\n if( $pl + $mi < $n ){\n $res = 0 if 0 > $res;\n } else {\n # plus=4 ,mi=1\n my $v3 = $sa[$pl-4] * $sa[$pl-3] * $sa[$pl-2] * $sa[$pl-1] * $sa[$#sa-$mi+1];\n $res = $v3 if $v3 > $res;\n }\n }\n } elsif( $pl >= 1 ){\n if( $mi >= 4 ){\n my $v1 = $sa[0] * $sa[$#sa-3] * $sa[$#sa-2] * $sa[$#sa-1] * $sa[$#sa];\n $res = $v1 if $v1 > $res;\n }\n if( $res < 0 ){\n if( $pl + $mi < $n ){\n $res = 0 if 0 > $res;\n } else {\n # plus=4 ,mi=1\n my $v3 = $sa[$pl-2] * $sa[$pl-1] * $sa[$#sa-$mi+1] * $sa[$#sa-$mi+2] * $sa[$#sa-$mi+3];\n $res = $v3 if $v3 > $res;\n }\n }\n } else {\n # all minus\n if( $mi < $n ){\n $res = 0 if 0 > $res;\n } else {\n my $v3 = $sa[$#sa-$mi+1] * $sa[$#sa-$mi+2] * $sa[$#sa-$mi+3] * $sa[$#sa-$mi+4] * $sa[$#sa-$mi+5];\n $res = $v3 if $v3 > $res;\n }\n }\n \n print \"$res\\n\";\n \n}\n\nexit(0);\n\n\n"}], "negative_code": [], "src_uid": "a3a64c3c7e9349d6e663c2d8113d2676"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers. You want to distribute these $$$n$$$ integers into two groups $$$s_1$$$ and $$$s_2$$$ (groups can be empty) so that the following conditions are satisfied: For each $$$i$$$ $$$(1 \\leq i \\leq n)$$$, $$$a_i$$$ goes into exactly one group. The value $$$|sum(s_1)| - |sum(s_2)|$$$ is the maximum possible among all such ways to distribute the integers.Here $$$sum(s_1)$$$ denotes the sum of the numbers in the group $$$s_1$$$, and $$$sum(s_2)$$$ denotes the sum of the numbers in the group $$$s_2$$$.Determine the maximum possible value of $$$|sum(s_1)| - |sum(s_2)|$$$.", "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)$$$ \u00a0\u2014 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 10^5)$$$ \u00a0\u2014 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 \\leq a_i \\leq 10^9)$$$ \u00a0\u2014 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 a single integer \u00a0\u2014 the maximum possible value of $$$|sum(s_1)| - |sum(s_2)|$$$.", "sample_inputs": ["4\n\n2\n\n10 -10\n\n4\n\n-2 -1 11 0\n\n3\n\n2 3 2\n\n5\n\n-9 2 0 0 -4"], "sample_outputs": ["0\n8\n7\n11"], "notes": "NoteIn the first testcase, we can distribute as $$$s_1 = \\{10\\}$$$, $$$s_2 = \\{-10\\}$$$. Then the value will be $$$|10| - |-10| = 0$$$.In the second testcase, we can distribute as $$$s_1 = \\{0, 11, -1\\}$$$, $$$s_2 = \\{-2\\}$$$. Then the value will be $$$|0 + 11 - 1| - |-2| = 10 - 2 = 8$$$.In the third testcase, we can distribute as $$$s_1 = \\{2, 3, 2\\}$$$, $$$s_2 = \\{\\}$$$. Then the value will be $$$|2 + 3 + 2| - |0| = 7$$$.In the fourth testcase, we can distribute as $$$s_1 = \\{-9, -4, 0\\}$$$, $$$s_2 = \\{2, 0\\}$$$. Then the value will be $$$|-9 - 4 + 0| - |2 + 0| = 13 - 2 = 11$$$."}, "positive_code": [{"source_code": "<>;print abs eval<>=~s/\\s/+/gr.0,$/while<>"}, {"source_code": "<>; print abs eval <> =~ s/\\s/+/gr . 0, $/ while <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\tmy $s1 = 0;\n\t$s1 += $_ for grep $_ > 0, @_;\n\tmy $s2 = 0;\n\t$s2 -= $_ for grep $_ < 0, @_;\n\t\n\tprint abs( $s1 - $s2 );\n\t}"}, {"source_code": "<>;print abs eval<>=~s/\\s/+/gr.0,$/while<>"}, {"source_code": "<>;print abs eval<>=~s/\\s/+/gr.0,$/while<>"}, {"source_code": "<>;print abs eval<>=~s/\\s/+/gr.0,$/while<>"}, {"source_code": "<>;print abs eval<>=~s/\\s/+/gr.0,$/while<>"}, {"source_code": "<>;print abs eval<>=~s/\\s/+/gr.0,$/while<>"}, {"source_code": "<>;print abs eval<>=~s/\\s/+/gr.0,$/while<>"}, {"source_code": "<>;print abs eval<>=~s/\\s/+/gr.0,$/while<>"}, {"source_code": "<>;print abs eval<>=~s/\\s/+/gr.0,$/while<>"}, {"source_code": "<>;print abs eval<>=~s/\\s/+/gr.0,$/while<>"}, {"source_code": "<>;print abs eval<>=~s/\\s/+/gr.0,$/while<>"}, {"source_code": "<>;print abs eval<>=~s/\\s/+/gr.0,$/while<>"}], "negative_code": [], "src_uid": "f59f92a80f719cdb87ad92cd8c211942"} {"nl": {"description": "You are given two arrays A and B consisting of integers, sorted in non-decreasing order. Check whether it is possible to choose k numbers in array A and choose m numbers in array B so that any number chosen in the first array is strictly less than any number chosen in the second array.", "input_spec": "The first line contains two integers nA,\u2009nB (1\u2009\u2264\u2009nA,\u2009nB\u2009\u2264\u2009105), separated by a space \u2014 the sizes of arrays A and B, correspondingly. The second line contains two integers k and m (1\u2009\u2264\u2009k\u2009\u2264\u2009nA,\u20091\u2009\u2264\u2009m\u2009\u2264\u2009nB), separated by a space. The third line contains nA numbers a1,\u2009a2,\u2009... anA (\u2009-\u2009109\u2009\u2264\u2009a1\u2009\u2264\u2009a2\u2009\u2264\u2009...\u2009\u2264\u2009anA\u2009\u2264\u2009109), separated by spaces \u2014 elements of array A. The fourth line contains nB integers b1,\u2009b2,\u2009... bnB (\u2009-\u2009109\u2009\u2264\u2009b1\u2009\u2264\u2009b2\u2009\u2264\u2009...\u2009\u2264\u2009bnB\u2009\u2264\u2009109), separated by spaces \u2014 elements of array B.", "output_spec": "Print \"YES\" (without the quotes), if you can choose k numbers in array A and m numbers in array B so that any number chosen in array A was strictly less than any number chosen in array B. Otherwise, print \"NO\" (without the quotes).", "sample_inputs": ["3 3\n2 1\n1 2 3\n3 4 5", "3 3\n3 3\n1 2 3\n3 4 5", "5 2\n3 1\n1 1 1 1 1\n2 2"], "sample_outputs": ["YES", "NO", "YES"], "notes": "NoteIn the first sample test you can, for example, choose numbers 1 and 2 from array A and number 3 from array B (1 < 3 and 2 < 3).In the second sample test the only way to choose k elements in the first array and m elements in the second one is to choose all numbers in both arrays, but then not all the numbers chosen in A will be less than all the numbers chosen in B: ."}, "positive_code": [{"source_code": "my ($na, $nb) = split / /, <>;\nmy ($k, $m) = split / /, <>;\nmy @a = split / /, <>;\nmy @b = split / /, <>;\n\nif ($a[$k - 1] < $b[-$m]) {\n print \"YES\\n\";\n}\nelse {\n print \"NO\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\n($na, $nb) = split / /, <>;\n($k, $m) = split / /, <>;\n@a = split / /, <>;\n@b = split / /, <>;\n($pa, $pb) = ($a[$k-1], $b[$nb-$m]);\n$pa<$pb and say \"YES\" or say \"NO\";"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\n<>;\nmy ($k, $m) = split ' ', <>;\nmy @x = split ' ', <>;\nmy @y = split ' ', <>;\n\nmy $x = $x[$k - 1];\nmy $y = $y[-$m];\n\nsay $x < $y ? 'YES' : 'NO';\n"}, {"source_code": "print qw(NO YES)[ !! eval join '<', map { (split \" \", <>)[ (-1) ** ($i++) * $_ + $i - 2 ] } split \" \", (<>, <>) ]"}], "negative_code": [], "src_uid": "8e0581cce19d6bf5eba30a0aebee9a08"} {"nl": {"description": "Michael is accused of violating the social distancing rules and creating a risk of spreading coronavirus. He is now sent to prison. Luckily, Michael knows exactly what the prison looks like from the inside, especially since it's very simple.The prison can be represented as a rectangle $$$a\\times b$$$ which is divided into $$$ab$$$ cells, each representing a prison cell, common sides being the walls between cells, and sides on the perimeter being the walls leading to freedom. Before sentencing, Michael can ask his friends among the prison employees to make (very well hidden) holes in some of the walls (including walls between cells and the outermost walls). Michael wants to be able to get out of the prison after this, no matter which cell he is placed in. However, he also wants to break as few walls as possible.Your task is to find out the smallest number of walls to be broken so that there is a path to the outside from every cell after this.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1\\leq t\\leq 100$$$)\u00a0\u2014 the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$a$$$ and $$$b$$$ ($$$1\\leq a, b\\leq 100$$$), representing a corresponding test case.", "output_spec": "For each test case print the single integer on a separate line\u00a0\u2014 the answer to the problem.", "sample_inputs": ["2\n2 2\n1 3"], "sample_outputs": ["4\n3"], "notes": "NoteSome possible escape plans for the example test cases are shown below. Broken walls are shown in gray, not broken walls are shown in black. "}, "positive_code": [{"source_code": "for(1..<>){($x,$y)=split' ',<>;print$x*$y.\"\\n\";}"}, {"source_code": "for(1..<>){($x,$y)=split' ',<>;print$x*$y.\"\\n\";}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split;\n\t@_ = sort { $a <=> $b } @_;\n\t\n\tprint +( $_[ 1 ] ) * $_[ 0 ];\n\t}"}], "negative_code": [], "src_uid": "806dcdab8364f5169334e172a192598a"} {"nl": {"description": "Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b.Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them.Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above.", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000) \u2014 the number of residents who stand in the line. The second line contains n space-separated integers ai (0\u2009\u2264\u2009ai\u2009\u2264\u2009109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line. ", "output_spec": "If it is impossible to make all the residents happy, print \":(\" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them.", "sample_inputs": ["2\n11 8", "5\n10 9 7 10 6", "3\n12 3 3"], "sample_outputs": ["9 10", ":(", "4 4 10"], "notes": "NoteIn the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result.In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy."}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse warnings;\nuse strict;\n\n<>;\nmy @array = split(' ', <>);\nmy $inc;\n\nfor my $el(@array){\n\t$el += $inc ++;\n}\n\n@array = sort {$a <=> $b} @array;\n\n$inc = -1;\nfor my $el(@array){\n\tif($el == $inc){\n\t\tprint \":(\";\n\t\texit;\n\t}\n\t$inc = $el;\n}\n\n$inc = 0;\nfor my $el(@array){\n\t$el -= $inc ++;\n}\nprint \"@array\";\n\n"}], "negative_code": [], "src_uid": "27ef62139533982f0857d733fad5c0d6"} {"nl": {"description": "Consider a tunnel on a one-way road. During a particular day, $$$n$$$ cars numbered from $$$1$$$ to $$$n$$$ entered and exited the tunnel exactly once. All the cars passed through the tunnel at constant speeds.A traffic enforcement camera is mounted at the tunnel entrance. Another traffic enforcement camera is mounted at the tunnel exit. Perfectly balanced.Thanks to the cameras, the order in which the cars entered and exited the tunnel is known. No two cars entered or exited at the same time.Traffic regulations prohibit overtaking inside the tunnel. If car $$$i$$$ overtakes any other car $$$j$$$ inside the tunnel, car $$$i$$$ must be fined. However, each car can be fined at most once.Formally, let's say that car $$$i$$$ definitely overtook car $$$j$$$ if car $$$i$$$ entered the tunnel later than car $$$j$$$ and exited the tunnel earlier than car $$$j$$$. Then, car $$$i$$$ must be fined if and only if it definitely overtook at least one other car.Find the number of cars that must be fined. ", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$), denoting the number of cars. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le n$$$), denoting the ids of cars in order of entering the tunnel. All $$$a_i$$$ are pairwise distinct. The third line contains $$$n$$$ integers $$$b_1, b_2, \\ldots, b_n$$$ ($$$1 \\le b_i \\le n$$$), denoting the ids of cars in order of exiting the tunnel. All $$$b_i$$$ are pairwise distinct.", "output_spec": "Output the number of cars to be fined.", "sample_inputs": ["5\n3 5 2 1 4\n4 3 2 5 1", "7\n5 2 3 6 7 1 4\n2 3 6 7 1 4 5", "2\n1 2\n1 2"], "sample_outputs": ["2", "6", "0"], "notes": "NoteThe first example is depicted below:Car $$$2$$$ definitely overtook car $$$5$$$, while car $$$4$$$ definitely overtook cars $$$1$$$, $$$2$$$, $$$3$$$ and $$$5$$$. Cars $$$2$$$ and $$$4$$$ must be fined.In the second example car $$$5$$$ was definitely overtaken by all other cars.In the third example no car must be fined."}, "positive_code": [{"source_code": "<>;\n\nmap $h{ $_ } = ++ $i, split ' ', <>;\n\n$_ = $h{ $_ } for @B = split ' ', <>;\n\n$m = pop @B;\n\nfor( reverse @B ){\n\t$_ > $m and $c ++;\n\t$_ < $m and $m = $_;\n\t}\n\nprint 0 + $c"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\t\n\t$debug and print \"\\@A: [@A]\";\n\t$debug and print \"\\@B: [@B]\";\n\t\n\tmy %h;\n\t\n\tmy $i = 0;\n\t\n\tmap $h{ $_ } = ++ $i, @A;\n\t\n\t$_ = $h{ $_ } for @A;\n\t$_ = $h{ $_ } for @B;\n\t\n\t$debug and print \"\\@A: [@A]\";\n\t$debug and print \"\\@B: [@B]\";\n\t\n\tmy $cnt = 0;\n\t\n\t@B = reverse @B;\n\t\n\tmy $min = $B[ 0 ];\n\t\n\tfor( @B ){\n\t\tif( $_ > $min ){\n\t\t\t$cnt ++;\n\t\t\t}\n\t\tif( $_ < $min ){\n\t\t\t$min = $_;\n\t\t\t}\n\t\t}\n\t\n\tprint $cnt;\n\t}"}], "negative_code": [], "src_uid": "f5178609cc7782edd40bc50b8797522e"} {"nl": {"description": "Alice and Bob are playing a game. They are given an array $$$A$$$ of length $$$N$$$. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game?", "input_spec": "The first line contains one integer $$$N$$$ ($$$1 \\leq N \\leq 2*10^5$$$) - the length of the array $$$A$$$. The second line contains $$$N$$$ integers $$$A_1$$$, $$$A_2$$$,...,$$$A_N$$$ ($$$0 \\leq A_i \\leq 10^9$$$)", "output_spec": "The first and only line of output consists of one string, the name of the winner. If Alice won, print \"Alice\", otherwise, print \"Bob\".", "sample_inputs": ["1\n5", "3\n5 4 5", "6\n5 8 2 1 10 9"], "sample_outputs": ["Alice", "Alice", "Bob"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @A;\n\t\n\tmy @B = @_;\n\t\n\tmy $prev = shift @_;\n\tpush @A, $prev;\n\t\n\tfor( @_ ){\n\t\tif( $_ == $prev ){\n\t\t\tnext;\n\t\t\t}\n\t\telse{\n\t\t\tpush @A, $_;\n\t\t\t}\n\t\t$prev = $_;\n\t\t}\n\t\n\t@A = @B;\n\t\n\tmy $c = 0;\n\t\n\tfor my $i ( 0 .. @A - 2 ){\n\t\tlast if $A[ $i ] >= $A[ $i + 1 ];\n\t\t\n\t\t$c ++;\n\t\t}\n\t\n\tmy $d = 0;\n\t\n\t@A = reverse @A;\n\t\n\tfor my $i ( 0 .. @A - 2 ){\n\t\tlast if $A[ $i ] >= $A[ $i + 1 ];\n\t\t\n\t\t$d ++;\n\t\t}\n\t\n\tif( $c % 2 == 0 or $d % 2 == 0 ){\n\t\tprint 'Alice';\n\t\t}\n\telse{\n\t\tprint 'Bob';\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @A;\n\t\n\tmy $prev = shift @_;\n\tpush @A, $prev;\n\t\n\tfor( @_ ){\n\t\tif( $_ == $prev ){\n\t\t#\tnext;\n\t\t\t}\n\t\telse{\n\t\t\tpush @A, $_;\n\t\t\t}\n\t\t$prev = $_;\n\t\t}\n\t\n\tmy $c = 0;\n\t\n\tfor my $i ( 0 .. @A - 2 ){\n\t\tlast if $A[ $i ] >= $A[ $i + 1 ];\n\t\t\n\t\t$c ++;\n\t\t}\n\t\n\tmy $d = 0;\n\t\n\t@A = reverse @A;\n\t\n\tfor my $i ( 0 .. @A - 2 ){\n\t\tlast if $A[ $i ] >= $A[ $i + 1 ];\n\t\t\n\t\t$d ++;\n\t\t}\n\t\n\tif( $c % 2 == 0 or $d % 2 == 0 ){\n\t\tprint 'Alice';\n\t\t}\n\telse{\n\t\tprint 'Bob';\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @A;\n\t\n\tmy $prev = shift @_;\n\tpush @A, $prev;\n\t\n\tfor( @_ ){\n\t\tif( $_ == $prev ){\n\t\t\tnext;\n\t\t\t}\n\t\telse{\n\t\t\tpush @A, $_;\n\t\t\t}\n\t\t$prev = $_;\n\t\t}\n\t\n\tmy $c = 0;\n\t\n\tfor my $i ( 0 .. @A - 2 ){\n\t\tlast if $A[ $i ] >= $A[ $i + 1 ];\n\t\t\n\t\t$c ++;\n\t\t}\n\t\n\tmy $d = 0;\n\t\n\t@A = reverse @A;\n\t\n\tfor my $i ( 0 .. @A - 2 ){\n\t\tlast if $A[ $i ] >= $A[ $i + 1 ];\n\t\t\n\t\t$d ++;\n\t\t}\n\t\n\tif( $c % 2 == 0 or $d % 2 == 0 ){\n\t\tprint 'Alice';\n\t\t}\n\telse{\n\t\tprint 'Bob';\n\t\t}\n\t}"}], "src_uid": "c01e33f3d0c31f1715d8ff118970f42e"} {"nl": {"description": "You are given $$$n$$$ strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for every string, all strings that are placed before it are its substrings.String $$$a$$$ is a substring of string $$$b$$$ if it is possible to choose several consecutive letters in $$$b$$$ in such a way that they form $$$a$$$. For example, string \"for\" is contained as a substring in strings \"codeforces\", \"for\" and \"therefore\", but is not contained as a substring in strings \"four\", \"fofo\" and \"rof\".", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\le n \\le 100$$$) \u2014 the number of strings. The next $$$n$$$ lines contain the given strings. The number of letters in each string is from $$$1$$$ to $$$100$$$, inclusive. Each string consists of lowercase English letters. Some strings might be equal.", "output_spec": "If it is impossible to reorder $$$n$$$ given strings in required order, print \"NO\" (without quotes). Otherwise print \"YES\" (without quotes) and $$$n$$$ given strings in required order.", "sample_inputs": ["5\na\naba\nabacaba\nba\naba", "5\na\nabacaba\nba\naba\nabab", "3\nqwerty\nqwerty\nqwerty"], "sample_outputs": ["YES\na\nba\naba\naba\nabacaba", "NO", "YES\nqwerty\nqwerty\nqwerty"], "notes": "NoteIn the second example you cannot reorder the strings because the string \"abab\" is not a substring of the string \"abacaba\"."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\n$input_line = ;\nchop($input_line);\n$n=$input_line;\n@s=();\nfor ($counter = 0; $counter <$n; $counter++){\n\t$input_line = ;\n\tchop($input_line);\n\t$s[$counter] = $input_line;\n}\n$counter = $n;\nwhile ($counter > 0){\n\t$i = 1;\n\twhile ($i < $counter){\n\t\tif (length($s[$i])< length($s[$i-1])){($s[$i],$s[$i-1]) = ($s[$i-1], $s[$i])}\n\t\t$i++;\n\t}\n\t$counter--;\n}\n$check = 1;\nfor ($i = 1; $i <$n; $i++){\n\tif ($s[$i] !~ /${s[$i-1]}/){$check = 0}\n}\nif ($check == 0){print (\"NO\\n\");}\nelse{\n\tprint(\"YES\\n\");\n\tfor ($i = 0; $i <$n; $i++){\n\t\tprint (\"$s[$i]\\n\");}\n}"}, {"source_code": "use warnings;\nuse strict;\nchomp ( my $n = );\nmy @a = ();\nmy $s;\nfor ( 1 .. $n ) {\n $s = ;\n chomp ( $s );\n push @a, $s;\n}\n$, = qq/\\n/, $\\ = qq/\\n/;\n@a = sort { $a =~ m/$b/ ? 1 : -1 } @a;\nfor my $i ( 1 .. $#a ) { if ( not $a[$i] =~ m/$a[$i-1]/ ) { print \"NO\\n\"; exit; }}\nprint \"YES\";\nprint @a;\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\t@_ = sort { length $a <=> length $b } @_;\n\t\n\tmy $f = 0;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\t$_[ $i + 1 ] =~ $_[ $i ] or $f ++;\n\t\t}\n\t\n\tnext if $f and print \"NO\";\n\t\n\tprint for \"YES\", @_;\n\t}"}, {"source_code": "$\" = $/;\n\n<>;\n\n$k ++;\n\n@_ = sort { $k &&= $a =~ $b } sort { $b =~ $a * 2 - 1 } map s/\\n//r, <>;\n\nprint $k ? \"YES\\n@_\" : \"NO\""}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n$\" = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\tmy $ok = 1;\n\t\n\t@_ = sort { $ok &&= $a =~ $b } sort { $b =~ $a * 2 - 1 } @_;\n\t\n\tprint $ok ? \"YES\\n@_\" : \"NO\";\n\t}"}], "negative_code": [{"source_code": "use warnings;\nuse strict;\nchomp ( my $n = );\nmy @a = ();\nmy $s;\nfor ( 1 .. $n ) {\n $s = ;\n chomp ( $s );\n push @a, $s;\n}\n$, = qq/\\n/, $\\ = qq/\\n/;\n@a = sort { $a =~ m/$b/ ? 1 : -1 } @a;\nfor my $i ( 1 .. $#a ) { if ( not $a[$i] =~ m/$a[$i-1]/ ) { print \"NO\\n\"; exit; }}\nprint @a;\n"}, {"source_code": "$\" = $/;\n\n<>;\n\n@_ = sort { $k ||= $a !~ $b } sort { $b =~ $a * 2 - 1 } map s/\\n//r, <>;\n\nprint $k ? \"YES\\n@_\" : \"NO\""}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\t@_ = sort { length $a <=> length $b } @_;\n\t\n\tmy $f = 0;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\t$_[ $i + 1 ] =~ $_[ $i ] or $f ++;\n\t\t}\n\t\n\tnext if $f and print \"NO\";\n\t\n\tprint for @_;\n\t}"}], "src_uid": "5c33d1f970bcc2ffaea61d5407b877b2"} {"nl": {"description": "You are given an array a with n elements. Each element of a is either 0 or 1.Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).", "input_spec": "The first line contains two integers n and k (1\u2009\u2264\u2009n\u2009\u2264\u20093\u00b7105,\u20090\u2009\u2264\u2009k\u2009\u2264\u2009n) \u2014 the number of elements in a and the parameter k. The second line contains n integers ai (0\u2009\u2264\u2009ai\u2009\u2264\u20091) \u2014 the elements of a.", "output_spec": "On the first line print a non-negative integer z \u2014 the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers aj \u2014 the elements of the array a after the changes. If there are multiple answers, you can print any one of them.", "sample_inputs": ["7 1\n1 0 0 1 1 0 1", "10 2\n1 0 0 1 0 1 0 1 0 1"], "sample_outputs": ["4\n1 0 0 1 1 1 1", "5\n1 0 0 1 1 1 1 1 0 1"], "notes": null}, "positive_code": [{"source_code": "\n\n\nmy $f;\nchomp ( $f = );\nmy ($n, $k) = split m/ /, $f;\nchomp ( $f = );\nmy @arr = split m/ /, $f;\n\nmy @dp = ();\nmy $prev = 0;\nmy $is_one = 0;\n\n# pointers a and b\nmy $ptr_a = 0;\nmy $ptr_b = 0;\n\n# number of changed elements right now\nmy $changed = 0;\n# maximum length of contiguous ones\nmy $max_len = 0;\nmy $index_of_max = 0;\nwhile ( $ptr_b < @arr ) {\n if ( $arr[ $ptr_b ] == 0 ) {\n\tif ( $changed < $k ) {\n\t $changed += 1;\n\t $ptr_b += 1;\n\t if ( ($ptr_b - $ptr_a) > $max_len ) {\n\t\t$max_len = $ptr_b - $ptr_a;\n\t\t$index_of_max = $ptr_a;\n\t }\n\t}\n\telse {\n\t while ( $arr[ $ptr_a ] == 1 ) {\n\t\t$ptr_a += 1;\n\t }\n\t if ( $arr[ $ptr_a ] == 0 ) {\n\t\t$changed -= 1;\n\t\t$ptr_a += 1;\n\t }\n\t}\n }\n else {\n\t$ptr_b += 1;\n\tif ( ($ptr_b - $ptr_a) > $max_len ) {\n\t $max_len = $ptr_b - $ptr_a;\n\t $index_of_max = $ptr_a;\n\t}\n }\n}\n \n\nprint $max_len;\nprint \"\\n\";\n\n\nfor ( my $i = $index_of_max ; $i < ($index_of_max + $max_len); $i += 1 ) {\n $arr[ $i ] = 1;\n}\n\nfor (@arr) {\n print \"$_ \";}\nprint \"\\n\";\n \n\n\n\n"}], "negative_code": [{"source_code": "\n\n\nmy $f;\nchomp ( $f = );\nmy ($n, $k) = split m/ /, $f;\nchomp ( $f = );\nmy @arr = split m/ /, $f;\n\nmy @dp = ();\nmy $prev = 0;\n\nfor ( my $i = 0; $i < @arr; $i += 1 ) {\n if ( $arr[ $i ] == 0 ) {\n\t$dp[ 0 ][ $i ] = 0;\n }\n else {\n\t$dp[ 0 ][ $i ] = $dp[ 0 ][ $i-1 ] + 1;\n }\n}\n\n# calculate dp\nfor ( my $i = 1; $i <= $k; $i += 1 ) {\n $prev = 0;\n for ( my $j = 0; $j < @arr ; $j += 1 ) {\n\t\n\tif ( $arr[ $j ] == 0 ) {\n\t $dp[ $i ][ $j ] = $dp[ $i-1 ][ $j-1 ] + 1;\n\t $prev = $dp[ $i-1 ][ $j-1 ] + 1;\n\t}\n\telse {\n\t $dp[ $i ][ $j ] = $dp[ $i ][ $j-1 ] + 1;\n\t $prev = $dp[ $i ][ $j-1 ] + 1;\n\t}\n }\n}\n\n\nmy $z = $dp[ $k ][ @arr-1 ];\nprint $z;\nprint \"\\n\";\n\n# change the array \nmy $i = 0;\nwhile ( $i < @arr ) {\n if ( $dp[ $k ][ $i ] == $z ) {\n\twhile ( $z > 0 ) {\n\t $arr[ $i ] = 1;\n\t $i -= 1;\n\t $z -= 1;\n\t}\n\t# break the loop\n\t$i = @arr + 13;\t\t\n }\n $i += 1;\n}\n\nfor (@arr) {\n print $_;\n print \" \";\n}\nprint \"\\n\";\n\n\n"}, {"source_code": "\n\n\nmy $f;\nchomp ( $f = );\nmy ($n, $k) = split m/ /, $f;\nchomp ( $f = );\nmy @arr = split m/ /, $f;\n\nmy @dp = ();\nmy $prev = 0;\nmy $is_one = 0;\n\nfor ( my $i = 0; $i < @arr; $i += 1 ) {\n if ( $arr[ $i ] == 0 ) {\n\t$dp[ 0 ][ $i ] = 0;\n }\n else {\n\t$dp[ 0 ][ $i ] = $dp[ 0 ][ $i-1 ] + 1;\n\t$is_one = 1;\n }\n}\n\n# calculate dp\nfor ( my $i = 1; $i <= $k; $i += 1 ) {\n $prev = 0;\n for ( my $j = 0; $j < @arr ; $j += 1 ) {\n\t\n\tif ( $arr[ $j ] == 0 ) {\n\t $dp[ $i ][ $j ] = $dp[ $i-1 ][ $j-1 ] + 1;\n\t $prev = $dp[ $i-1 ][ $j-1 ] + 1;\n\t}\n\telse {\n\t $dp[ $i ][ $j ] = $dp[ $i ][ $j-1 ] + 1;\n\t $prev = $dp[ $i ][ $j-1 ] + 1;\n\t}\n }\n}\n\n\nmy $z = $dp[ $k ][ @arr-1 ];\nif ( $k == 0 ) {\n if ( $is_one ) {\n\tprint \"1\\n\";}\n else {\n\tprint \"0\\n\";}\n}\nelse {\n print $z;\n print \"\\n\";\n}\n\n\n# change the array \nmy $i = 0;\nwhile ( $i < @arr ) {\n if ( $dp[ $k ][ $i ] == $z ) {\n\twhile ( $z > 0 ) {\n\t $arr[ $i ] = 1;\n\t $i -= 1;\n\t $z -= 1;\n\t}\n\t# break the loop\n\t$i = @arr + 13;\t\t\n }\n $i += 1;\n}\n\n\nfor (@arr) {\n print $_;\n print \" \";\n}\nprint \"\\n\";\n\n\n"}], "src_uid": "ec9b03577868d8999bcc54dfc1aae056"} {"nl": {"description": "You took a peek on Thanos wearing Infinity Gauntlet. In the Gauntlet there is a place for six Infinity Gems: the Power Gem of purple color, the Time Gem of green color, the Space Gem of blue color, the Soul Gem of orange color, the Reality Gem of red color, the Mind Gem of yellow color. Using colors of Gems you saw in the Gauntlet determine the names of absent Gems.", "input_spec": "In the first line of input there is one integer $$$n$$$ ($$$0 \\le n \\le 6$$$)\u00a0\u2014 the number of Gems in Infinity Gauntlet. In next $$$n$$$ lines there are colors of Gems you saw. Words used for colors are: purple, green, blue, orange, red, yellow. It is guaranteed that all the colors are distinct. All colors are given in lowercase English letters.", "output_spec": "In the first line output one integer $$$m$$$ ($$$0 \\le m \\le 6$$$)\u00a0\u2014 the number of absent Gems. Then in $$$m$$$ lines print the names of absent Gems, each on its own line. Words used for names are: Power, Time, Space, Soul, Reality, Mind. Names can be printed in any order. Keep the first letter uppercase, others lowercase.", "sample_inputs": ["4\nred\npurple\nyellow\norange", "0"], "sample_outputs": ["2\nSpace\nTime", "6\nTime\nMind\nSoul\nPower\nReality\nSpace"], "notes": "NoteIn the first sample Thanos already has Reality, Power, Mind and Soul Gems, so he needs two more: Time and Space.In the second sample Thanos doesn't have any Gems, so he needs all six."}, "positive_code": [{"source_code": "use warnings;\nuse strict;\nuse integer;\nuse constant NL => qq/\\n/;\n$\\ = qq/\\n/, $, = qq/\\n/;\nchomp ( my $n = );\nmy %gems = ( \n 'purple' => 'Power',\n 'green' => 'Time',\n 'blue' => 'Space',\n 'orange' => 'Soul',\n 'red' => 'Reality',\n 'yellow' => 'Mind' \n);\nfor my $val ( 1 .. $n ) {\n chomp ( my $str = );\n delete $gems { $str };\n}\nprint scalar values %gems;\nprint values %gems;\n"}, {"source_code": "#!/usr/bin/perl -l\nuse warnings;\nuse strict;\nchomp(my $n=);\nmy %mapp=('purple'=>'Power','green'=>'Time','blue'=>'Space','orange'=>'Soul','red'=>'Reality','yellow'=>'Mind');\nfor(my $i=0;$i<$n;$i++)\n{\n\tchomp(my $s=);\n delete $mapp{$s};\n}\nmy $ss=keys %mapp;\nprint $ss;\nforeach my $value(values %mapp) \n{\n\tprint $value;\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl -l\nuse warnings;\nuse strict;\nchomp(my $n=);\nmy %mapp=('purple'=>'Power','green'=>'Time','blue'=>'Space','orange'=>'Soul','red'=>'Reality','yellow'=>'Mind');\nfor(my $i=0;$i<$n;$i++)\n{\n\tchomp(my $s=);\n delete $mapp{$s};\n}\nforeach my $value(values %mapp) \n{\n\tprint $value;\n}"}], "src_uid": "7eff98fbcf4e4a3284e2d2f98351fe4a"} {"nl": {"description": "Hr0d1y has $$$q$$$ queries on a binary string $$$s$$$ of length $$$n$$$. A binary string is a string containing only characters '0' and '1'.A query is described by a pair of integers $$$l_i$$$, $$$r_i$$$ $$$(1 \\leq l_i \\lt r_i \\leq n)$$$. For each query, he has to determine whether there exists a good subsequence in $$$s$$$ that is equal to the substring $$$s[l_i\\ldots r_i]$$$. A substring $$$s[i\\ldots j]$$$ of a string $$$s$$$ is the string formed by characters $$$s_i s_{i+1} \\ldots s_j$$$. String $$$a$$$ is said to be a subsequence of string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deleting some characters without changing the order of the remaining characters. A subsequence is said to be good if it is not contiguous and has length $$$\\ge 2$$$. For example, if $$$s$$$ is \"1100110\", then the subsequences $$$s_1s_2s_4$$$ (\"1100110\") and $$$s_1s_5s_7$$$ (\"1100110\") are good, while $$$s_1s_2s_3$$$ (\"1100110\") is not good. Can you help Hr0d1y answer each query?", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1\\leq t \\leq 100$$$)\u00a0\u2014 the number of test cases. The description of each test case is as follows. The first line contains two integers $$$n$$$ ($$$2 \\leq n \\leq 100$$$) and $$$q$$$ ($$$1\\leq q \\leq 100$$$)\u00a0\u2014 the length of the string and the number of queries. The second line contains the string $$$s$$$. The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \\leq l_i \\lt r_i \\leq n$$$).", "output_spec": "For each test case, output $$$q$$$ lines. The $$$i$$$-th line of the output of each test case should contain \"YES\" if there exists a good subsequence equal to the substring $$$s[l_i...r_i]$$$, and \"NO\" otherwise. You may print each letter in any case (upper or lower).", "sample_inputs": ["2\n6 3\n001000\n2 4\n1 3\n3 5\n4 2\n1111\n1 4\n2 3"], "sample_outputs": ["YES\nNO\nYES\nNO\nYES"], "notes": "NoteIn the first test case, $$$s[2\\ldots 4] = $$$ \"010\". In this case $$$s_1s_3s_5$$$ (\"001000\") and $$$s_2s_3s_6$$$ (\"001000\") are good suitable subsequences, while $$$s_2s_3s_4$$$ (\"001000\") is not good. $$$s[1\\ldots 3] = $$$ \"001\". No suitable good subsequence exists. $$$s[3\\ldots 5] = $$$ \"100\". Here $$$s_3s_5s_6$$$ (\"001000\") is a suitable good subsequence. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nwhile( $t -- > 0 ){\n my ($n,$q) = map { $_ - 0 } split(/\\s+/,);\n my $s = ;\n \n my @ru = (0); $#ru = $n;\n for(my $i=0;$i<$n;$i++){\n $ru[1+$i] = $ru[$i] + ( substr($s,$i,1) eq '1' ? 1 : 0 );\n }\n \n for(my $i=0;$i<$q;$i++){\n my ($l,$r) = map { $_ - 1 } split(/\\s+/,);\n my $ln = $r - $l + 1;\n if( $ln == $n ){\n print \"NO\\n\"; next;\n }\n my $lc = substr($s,$l,1);\n my $rc = substr($s,$r,1);\n if(\n ( $l > 0 and ( ( $lc eq '1' and $ru[$l] > 0 ) or ( $lc eq '0' and $ru[$l] < $l ) ) )\n or \n ( $r < $n-1 and ( ( $rc eq '1' and $ru[$n]-$ru[$r+1] > 0 ) or ( $rc eq '0' and $ru[$n]-$ru[$r+1] < $n-$r-1 ) ) )\n ){\n print \"YES\\n\";\n } else {\n print \"NO\\n\";\n }\n }\n}\n\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\n\nwhile( $t -- > 0 ){\n my ($n,$q) = map { $_ - 0 } split(/\\s+/,);\n my $s = ;\n \n my @ru = (0); $#ru = $n;\n for(my $i=0;$i<$n;$i++){\n $ru[1+$i] = $ru[$i] + ( substr($s,$i,1) eq '1' ? 1 : 0 );\n }\n \n for(my $i=0;$i<$q;$i++){\n my ($l,$r) = map { $_ - 1 } split(/\\s+/,);\n my $ln = $r - $l + 1;\n if( $ln == $n ){\n print \"NO\\n\"; next;\n }\n my $lc = substr($s,$l,1);\n my $rc = substr($s,$r,1);\n my $sb = substr($s,$l,$ln);\n if( ( $l > 0 and ( ( $lc eq '1' and $ru[$l] > 0 ) or ( $lc eq '0' and $ru[$l] < $l-1 ) ) ) or \n ( $r < $n-1 and ( ( $rc eq '1' and $ru[$n]-$ru[$r+1] > 0 ) or ( $rc eq '0' and $ru[$n]-$ru[$r+1] < $n-$r-1 ) ) ) ){\n print \"YES\\n\";\n } else {\n print \"NO\\n\";\n }\n }\n}\n\n\n"}], "src_uid": "cbd91ac0fc9e4ca01996791e4c94bd6e"} {"nl": {"description": "Wabbit is trying to move a box containing food for the rest of the zoo in the coordinate plane from the point $$$(x_1,y_1)$$$ to the point $$$(x_2,y_2)$$$.He has a rope, which he can use to pull the box. He can only pull the box if he stands exactly $$$1$$$ unit away from the box in the direction of one of two coordinate axes. He will pull the box to where he is standing before moving out of the way in the same direction by $$$1$$$ unit. For example, if the box is at the point $$$(1,2)$$$ and Wabbit is standing at the point $$$(2,2)$$$, he can pull the box right by $$$1$$$ unit, with the box ending up at the point $$$(2,2)$$$ and Wabbit ending at the point $$$(3,2)$$$.Also, Wabbit can move $$$1$$$ unit to the right, left, up, or down without pulling the box. In this case, it is not necessary for him to be in exactly $$$1$$$ unit away from the box. If he wants to pull the box again, he must return to a point next to the box. Also, Wabbit can't move to the point where the box is located.Wabbit can start at any point. It takes $$$1$$$ second to travel $$$1$$$ unit right, left, up, or down, regardless of whether he pulls the box while moving.Determine the minimum amount of time he needs to move the box from $$$(x_1,y_1)$$$ to $$$(x_2,y_2)$$$. Note that the point where Wabbit ends up at does not matter.", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 1000)$$$: the number of test cases. The description of the test cases follows. Each of the next $$$t$$$ lines contains four space-separated integers $$$x_1, y_1, x_2, y_2$$$ $$$(1 \\leq x_1, y_1, x_2, y_2 \\leq 10^9)$$$, describing the next test case.", "output_spec": "For each test case, print a single integer: the minimum time in seconds Wabbit needs to bring the box from $$$(x_1,y_1)$$$ to $$$(x_2,y_2)$$$.", "sample_inputs": ["2\n1 2 2 2\n1 1 2 2"], "sample_outputs": ["1\n4"], "notes": "NoteIn the first test case, the starting and the ending points of the box are $$$(1,2)$$$ and $$$(2,2)$$$ respectively. This is the same as the picture in the statement. Wabbit needs only $$$1$$$ second to move as shown in the picture in the statement.In the second test case, Wabbit can start at the point $$$(2,1)$$$. He pulls the box to $$$(2,1)$$$ while moving to $$$(3,1)$$$. He then moves to $$$(3,2)$$$ and then to $$$(2,2)$$$ without pulling the box. Then, he pulls the box to $$$(2,2)$$$ while moving to $$$(2,3)$$$. It takes $$$4$$$ seconds."}, "positive_code": [{"source_code": "<>;\nwhile(<>){\n ($a,$b,$c,$d)=split;\n $a=abs $a-$c;\n $b=abs $b-$d;\n $a+=2if $a&&$b;\n print $a+$b,\"\\n\"\n}"}, {"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nwhile($t-->0){\n my ($x1,$y1,$x2,$y2) = map { $_ - 0 } split(/\\s+/,);\n \n my $d;\n if( $x1 == $x2 or $y1 == $y2 ){\n $d = abs($x1 - $x2) + abs($y1 - $y2);\n } else {\n $d = 2+ abs($x1 - $x2) + abs($y1 - $y2);\n }\n print \"$d\\n\";\n}\n\n\n"}], "negative_code": [], "src_uid": "6a333044e2ed8f9f4fa44bc16b994418"} {"nl": {"description": "You are fighting with Zmei Gorynich \u2014 a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads! Initially Zmei Gorynich has $$$x$$$ heads. You can deal $$$n$$$ types of blows. If you deal a blow of the $$$i$$$-th type, you decrease the number of Gorynich's heads by $$$min(d_i, curX)$$$, there $$$curX$$$ is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows $$$h_i$$$ new heads. If $$$curX = 0$$$ then Gorynich is defeated. You can deal each blow any number of times, in any order.For example, if $$$curX = 10$$$, $$$d = 7$$$, $$$h = 10$$$ then the number of heads changes to $$$13$$$ (you cut $$$7$$$ heads off, but then Zmei grows $$$10$$$ new ones), but if $$$curX = 10$$$, $$$d = 11$$$, $$$h = 100$$$ then number of heads changes to $$$0$$$ and Zmei Gorynich is considered defeated.Calculate the minimum number of blows to defeat Zmei Gorynich!You have to answer $$$t$$$ independent queries.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) \u2013 the number of queries. The first line of each query contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\le n \\le 100$$$, $$$1 \\le x \\le 10^9$$$) \u2014 the number of possible types of blows and the number of heads Zmei initially has, respectively. The following $$$n$$$ lines of each query contain the descriptions of types of blows you can deal. The $$$i$$$-th line contains two integers $$$d_i$$$ and $$$h_i$$$ ($$$1 \\le d_i, h_i \\le 10^9$$$) \u2014 the description of the $$$i$$$-th blow.", "output_spec": "For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich. If Zmei Gorynuch cannot be defeated print $$$-1$$$.", "sample_inputs": ["3\n3 10\n6 3\n8 2\n1 4\n4 10\n4 1\n3 2\n2 6\n1 100\n2 15\n10 11\n14 100"], "sample_outputs": ["2\n3\n-1"], "notes": "NoteIn the first query you can deal the first blow (after that the number of heads changes to $$$10 - 6 + 3 = 7$$$), and then deal the second blow.In the second query you just deal the first blow three times, and Zmei is defeated. In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?"}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 12;\n\t\n\tmy( $n, $x ) = split;\n\t\n\t@_ = map ~~<>, 1 .. $n;\n\tchomp @_;\n\t\n\tmy $strongest = ( sort { $b <=> $a } @_ )[ 0 ];\n\t\n\tmy $best = ( sort { eval( $b =~ s/ /-/r) <=> eval( $a =~ s/ /-/r ) } @_ )[ 0 ];\n\t\n\tmy $minus = eval( $best =~ s/ /-/r);\n\t\n\t$debug and print \"n: $n, x: $x, strongest: $strongest, best: $best, minus: $minus\";\n\t\n\tmy $turns = 1;\n\t\n\t$x -= $strongest;\n\t\n\tif( $x <= 0 ){\n\t\tprint $turns;\n\t\tnext;\n\t\t}\n\t\n\tif( $minus <= 0 ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\t$debug and print \"x: $x\";\n\t\n\t$turns += int( $x / $minus ) + !! ( $x % $minus );\n\t\n\tprint $turns;\n\t}"}], "negative_code": [], "src_uid": "36099a612ec5bbec1b95f2941e759c00"} {"nl": {"description": "Berland crossword is a puzzle that is solved on a square grid with $$$n$$$ rows and $$$n$$$ columns. Initially all the cells are white.To solve the puzzle one has to color some cells on the border of the grid black in such a way that: exactly $$$U$$$ cells in the top row are black; exactly $$$R$$$ cells in the rightmost column are black; exactly $$$D$$$ cells in the bottom row are black; exactly $$$L$$$ cells in the leftmost column are black. Note that you can color zero cells black and leave every cell white.Your task is to check if there exists a solution to the given puzzle.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$)\u00a0\u2014 the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The only line of each testcase contains $$$5$$$ integers $$$n, U, R, D, L$$$ ($$$2 \\le n \\le 100$$$; $$$0 \\le U, R, D, L \\le n$$$).", "output_spec": "For each testcase print \"YES\" if the solution exists 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": ["4\n5 2 5 3 1\n3 0 0 0 0\n4 4 1 4 0\n2 1 1 1 1"], "sample_outputs": ["YES\nYES\nNO\nYES"], "notes": "NoteHere are possible solutions to testcases $$$1$$$, $$$2$$$ and $$$4$$$: "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy $n;\n\t( $n, @_ ) = split;\n\t\n\t$_ = join ' ', map { +\n\t\t',', ( sort { $_ * ( $a + $b ) } @_ ) x 3 \n\t\t} 0 .. 1;\n\t\n\tmy $pm = $n - 1;\n\t\n\tmy $fail = m/\n\t\t| $n 0 |\n\t\t| $n 1 $n |\n\t\t| $n 1 $pm 1 |\n\t\t| $pm 0 $pm 1 |\n\t\t| 0 $pm 0 |\n\t\t/\n\t\t;\n\t\n\tprint $fail ? 'NO' : 'YES';\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy $n;\n\t( $n, @_ ) = split;\n\t\n\t$_ = join ' ', ( @_ ) x 3, ',', ( reverse @_ ) x 3;\n\t\n\tmy $pm = $n - 1;\n\t\n\tmy $fail = 0\n\t\t|| m/ $n 0 /\n\t\t|| m/ $n 1 $n /\n\t\t|| m/ $n 1 $pm 1 /\n\t\t|| m/ $pm 0 $pm 1 /\n\t\t|| m/ 0 $pm 0 /\n\t\t;\n\t\n\tprint $fail ? 'NO' : 'YES';\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy $n;\n\t( $n, @_ ) = split;\n\t\n\t$_ = join ' ', ( @_ ) x 2;\n\t\n\tmy $pm = $n - 1;\n\t\n\tmy $fail = 0\n\t\t|| m/\\b$n 0\\b/\n\t\t|| m/\\b0 $n\\b/\n\t\t|| m/\\b$n 1 $n\\b/\n\t\t\n\t\t|| m/\\b$n 1 $pm 1\\b/\n\t\t|| m/\\b$pm 1 $n 1\\b/\n\t\t\n\t\t|| m/\\b$pm 0 $pm 1\\b/\n\t\t|| m/\\b$pm 1 $pm 0\\b/\n\t\t|| m/\\b0 $pm 0\\b/\n\t\t;\n\t\n\tprint $fail ? 'NO' : 'YES';\n\t}"}, {"source_code": "#\u0633\u062d\u0631 2\r\nuse warnings;\r\nuse strict;\r\n\r\n$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\tmy $n;\r\n\t( $n, @_ ) = split;\r\n\t\r\n\t$_ = join ' ', map { +\r\n\t\t',', ( sort { $_ * ( $a + $b ) } @_ ) x 3 \r\n\t\t} 0 .. 1;\r\n\t\r\n\tmy $pm = $n - 1;\r\n\t\r\n\tmy $fail = m/\r\n\t\t| $n 0 |\r\n\t\t| $n 1 $n |\r\n\t\t| $n 1 $pm 1 |\r\n\t\t| $pm 0 $pm 1 |\r\n\t\t| 0 $pm 0 |\r\n\t\t/\r\n\t\t;\r\n\t\r\n\tprint $fail ? 'NO' : 'YES';\r\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy $n;\n\t( $n, @_ ) = split;\n\t\n\tmy @A = '';\n\t\n\tfor my $B ( @_, @_ ){\n\t\tmy @new;\n\t\t\n\t\t$_ = 1 x $B . 0 x ( $n - $B );\n\t\tpush @new, $_;\n\t\ts/(.)(.*)/$2$1/;\n\t\tpush @new, $_;\n\t\ts/(.*)(.)/$2$1/;\n\t\ts/(.*)(.)/$2$1/;\n\t\tpush @new, $_;\n\t\ts/(.*)(.)/$2$1/;\n\t\tpush @new, $_;\n\t\t\n\t\tmy %C;\n\t\t\n\t\tfor my $A ( @A ){\n\t\t\tfor my $new ( @new ){\n\t\t\t\tnext if \"$A,$new\" =~ m/(.),(?!\\1)/;\n\t\t\t\t$C{ $A . $new } = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t@A = keys %C;\n\t\t}\n\t\n\t$_ = ( length ) / $n for @A;\n\t\n\t$_ = grep { $_ == 8 } @A;\n\t\n\tprint $_ ? 'YES' : 'NO';\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy $n;\n\t( $n, @_ ) = split;\n\t\n\t$_ = join ' ', ( @_ ) x 2;\n\t\n\tmy $pm = $n - 1;\n\t\n\tmy $fail = 0\n\t\t|| m/\\b$n $n $n [01]\\b/\n\t\t|| m/\\b$n 0\\b/\n\t\t|| m/\\b0 $n\\b/\n\t\t|| m/\\b$n 1 $n\\b/\n\t\t\n\t\t|| m/\\b$pm 0 $pm 0\\b/\n\t\t|| m/\\b$pm 0 $pm 1\\b/\n\t\t|| m/\\b$pm 1 $pm 0\\b/\n\t\t|| m/\\b0 $pm 0\\b/\n\t\t;\n\t\n\tprint $fail ? 'NO' : 'YES';\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy $n;\n\t( $n, @_ ) = split;\n\t\n\t$_ = join ' ', ( @_ ) x 2;\n\t\n\tmy $pm = $n - 1;\n\t\n\tmy $fail = 0\n\t\t|| m/\\b$n $n $n [01]\\b/\n\t\t|| m/\\b$n 0\\b/\n\t\t|| m/\\b0 $n\\b/\n\t\t\n\t\t|| m/\\b$pm 0 $pm 0\\b/\n\t\t|| m/\\b$pm 0 $pm 1\\b/\n\t\t|| m/\\b$pm 1 $pm 0\\b/\n\t\t|| m/\\b0 $pm 0\\b/\n\t\t;\n\t\n\tprint $fail ? 'NO' : 'YES';\n\t}"}], "src_uid": "1c94596da439c56b56e59da36734a912"} {"nl": {"description": "Phoenix has a string $$$s$$$ consisting of lowercase Latin letters. He wants to distribute all the letters of his string into $$$k$$$ non-empty strings $$$a_1, a_2, \\dots, a_k$$$ such that every letter of $$$s$$$ goes to exactly one of the strings $$$a_i$$$. The strings $$$a_i$$$ do not need to be substrings of $$$s$$$. Phoenix can distribute letters of $$$s$$$ and rearrange the letters within each string $$$a_i$$$ however he wants.For example, if $$$s = $$$ baba and $$$k=2$$$, Phoenix may distribute the letters of his string in many ways, such as: ba and ba a and abb ab and ab aa and bb But these ways are invalid: baa and ba b and ba baba and empty string ($$$a_i$$$ should be non-empty) Phoenix wants to distribute the letters of his string $$$s$$$ into $$$k$$$ strings $$$a_1, a_2, \\dots, a_k$$$ to minimize the lexicographically maximum string among them, i.\u00a0e. minimize $$$max(a_1, a_2, \\dots, a_k)$$$. Help him find the optimal distribution and print the minimal possible value of $$$max(a_1, a_2, \\dots, a_k)$$$.String $$$x$$$ is lexicographically less than string $$$y$$$ if either $$$x$$$ is a prefix of $$$y$$$ and $$$x \\ne y$$$, or there exists an index $$$i$$$ ($$$1 \\le i \\le min(|x|, |y|))$$$ such that $$$x_i$$$ < $$$y_i$$$ and for every $$$j$$$ $$$(1 \\le j < i)$$$ $$$x_j = y_j$$$. Here $$$|x|$$$ denotes the length of the string $$$x$$$.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$)\u00a0\u2014 the number of test cases. Each test case consists of two lines. The first line of each test case consists of two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 10^5$$$)\u00a0\u2014 the length of string $$$s$$$ and the number of non-empty strings, into which Phoenix wants to distribute letters of $$$s$$$, respectively. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ over all test cases is $$$\\le 10^5$$$.", "output_spec": "Print $$$t$$$ answers\u00a0\u2014 one per test case. The $$$i$$$-th answer should be the minimal possible value of $$$max(a_1, a_2, \\dots, a_k)$$$ in the $$$i$$$-th test case.", "sample_inputs": ["6\n4 2\nbaba\n5 2\nbaacb\n5 3\nbaacb\n5 3\naaaaa\n6 4\naaxxzz\n7 1\nphoenix"], "sample_outputs": ["ab\nabbc\nb\naa\nx\nehinopx"], "notes": "NoteIn the first test case, one optimal solution is to distribute baba into ab and ab. In the second test case, one optimal solution is to distribute baacb into abbc and a.In the third test case, one optimal solution is to distribute baacb into ac, ab, and b.In the fourth test case, one optimal solution is to distribute aaaaa into aa, aa, and a.In the fifth test case, one optimal solution is to distribute aaxxzz into az, az, x, and x.In the sixth test case, one optimal solution is to distribute phoenix into ehinopx."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile( <> ){\n\tmy( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, split //;\n\t\n\t$_ = join '', sort split //;\n\t\n\tif( ( substr $_, 0, 1 ) x $k eq substr $_, 0, $k ){\n\t\tif( 2 < keys %h ){\n\t\t\tprint substr $_, $k - 1;\n\t\t\t}\n\t\telsif( 1 == keys %h ){\n\t\t\tprint +( keys %h )[ 0 ] x ( int $n / $k + !!( $n % $k ) );\n\t\t\t}\n\t\telsif( $h{ substr $_, 0 , 1 } == $k ){\n\t\t\tprint +( substr $_, 0, 1 ) . substr( $_, $k, 1 ) x ( -1 + int $n / $k + !!( $n % $k ) );\n\t\t\t}\n\t\telse{\n\t\t\tprint substr $_, $k - 1;\n\t\t\t}\n\t\t}\n\telse{\n\t\tprint substr $_, $k - 1, 1;\n\t\t}\n\t\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile( <> ){\n\tmy( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, split //;\n\t\n\t$_ = join '', sort split //;\n\t\n\tif( ( substr $_, 0, 1 ) x $k eq substr $_, 0, $k ){\n\t\tif( 2 < keys %h ){\n\t\t\tprint substr $_, $k - 1;\n\t\t\t}\n\t\telsif( 1 == keys %h ){\n\t\t\tprint +( keys %h )[ 0 ] x ( int $n / $k + !!( $n % $k ) );\n\t\t\t}\n\t\telse{\n\t\t\tprint +( substr $_, 0, 1 ) . substr( $_, $k, 1 ) x ( -1 + int $n / $k + !!( $n % $k ) );\n\t\t\t}\n\t\t}\n\telse{\n\t\tprint substr $_, $k - 1, 1;\n\t\t}\n\t\n\t}"}], "src_uid": "df778e55a2c676acca7311d885f61d7a"} {"nl": {"description": "Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of discounts its price will be bi.Not all of sellers are honest, so now some products could be more expensive than after a week of discounts.Igor decided that buy at least k of items now, but wait with the rest of the week in order to save money as much as possible. Your task is to determine the minimum money that Igor can spend to buy all n items.", "input_spec": "In the first line there are two positive integer numbers n and k (1\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105, 0\u2009\u2264\u2009k\u2009\u2264\u2009n) \u2014 total number of items to buy and minimal number of items Igor wants to by right now. The second line contains sequence of integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009104) \u2014 prices of items during discounts (i.e. right now). The third line contains sequence of integers b1,\u2009b2,\u2009...,\u2009bn (1\u2009\u2264\u2009bi\u2009\u2264\u2009104) \u2014 prices of items after discounts (i.e. after a week).", "output_spec": "Print the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now.", "sample_inputs": ["3 1\n5 4 6\n3 1 5", "5 3\n3 4 7 10 3\n4 5 5 12 5"], "sample_outputs": ["10", "25"], "notes": "NoteIn the first example Igor should buy item 3 paying 6. But items 1 and 2 he should buy after a week. He will pay 3 and 1 for them. So in total he will pay 6\u2009+\u20093\u2009+\u20091\u2009=\u200910.In the second example Igor should buy right now items 1, 2, 4 and 5, paying for them 3, 4, 10 and 3, respectively. Item 3 he should buy after a week of discounts, he will pay 5 for it. In total he will spend 3\u2009+\u20094\u2009+\u200910\u2009+\u20093\u2009+\u20095\u2009=\u200925."}, "positive_code": [{"source_code": "sub input { split \" \", <> }\n($n, $k) = input; @a = input; @b = input;\n\nfor ($i = 0; $i < $n; $i++) {\n\t$c[$i] = {a => $a[$i], b => $b[$i], diff => abs($a[$i] - $b[$i])};\n}\n\n@c = sort {$b->{diff} <=> $a->{diff}} @c;\n$l = $n - $k;\nwhile ($l && @c) {\n\t$c = shift @c;\n\tif ($c->{a} < $c->{b}) {\n\t\t$sum += $c->{a};\n\t} else {\n\t\t$sum += $c->{b};\n\t\t$l--;\n\t}\n}\n\n$sum += $_->{a} for @c;\n\nprint $sum;"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\tmy @C;\n\tmy $sum = 0;\n\t\n\tfor my $A (@A){\n\t\tmy $B = shift @B;\n\t\tpush @C, [ $A, $B, $A - $B ];\n\t\t}\n\t\n\t@C = sort { $a->[2] <=> $b->[2] } @C;\n\t\n#%\tprint \" @$_\" for @C;\n\t\n\t$sum += eval join ' + ', map { $_->[0] } @C[ 0 .. $k - 1 ];\n\t$sum += eval join ' + ', map { $_->[2] > 0 ? $_->[1] : $_->[0] } @C[ $k .. $n - 1 ];\n\t\n\tprint $sum;\n\t}"}, {"source_code": "( $n, $k ) = split ' ', <>;\n\n@A = split ' ', <>;\n@A = sort { $a->[2] <=> $b->[2] } map { [ $A[$i], $_, $A[$i] - $_, $i++ ] } split ' ', <>;\n\nmap { $sum += $A[$_][2] > 0 && $_ >= $k ? $A[$_][1] : $A[$_][0] } 0 .. $#A;\n\nprint $sum"}], "negative_code": [{"source_code": "sub input { split \" \", <> }\n($n, $k) = input; @a = input; @b = input;\nfor ($i = 0; $i < $n; $i++) {\n\t$c = $c[$i] = {a => $a[$i], b => $b[$i], use => 0};\n\tpush @d, {c => $c, value => $c->{a}, k2 => 0};\n\tpush @d, {c => $c, value => $c->{b}, k2 => 1};\n}\n@d = sort { $a->{value} <=> $b->{value}} @d;\n$k2 = $n - $k;\nfor $d (@d) {\n\t$c = $d->{c};\n\tif (!$c->{use}) {\n\t\tif ($d->{k2}) {\n\t\t\tnext if !$k2;\n\t\t\t$k2--;\n\t\t}\n\t\t$sum += $d->{value};\t\n\t\t$c->{use} = 1;\n\t}\n}\nprint $sum;"}, {"source_code": "sub input { split \" \", <> }\n($n, $k) = input; @a = input; @b = input;\nfor ($i = 0; $i < $n; $i++) {\n\t$c = $c[$i] = {a => $a[$i], b => $b[$i], use => 0};\n\tpush @d, {c => $c, value => $c->{a}, k2 => 0};\n\tpush @d, {c => $c, value => $c->{b}, k2 => 1};\n}\n@d = sort { $a->{value} <=> $b->{value}} @d;\n$k2 = $n - $k;\nfor $d (@d) {\n\t$c = $d->{c};\n\tif (!$c->{use}) {\n\t\t$k2-- if $d->{k2} && $k2;\n\t\t$sum += $d->{value};\t\n\t\t$c->{use} = 1;\n\t}\n}\nprint $sum;"}], "src_uid": "b5355e1f4439b198d2cc7dea01bc4bc3"} {"nl": {"description": "Vasya wants to turn on Christmas lights consisting of m bulbs. Initially, all bulbs are turned off. There are n buttons, each of them is connected to some set of bulbs. Vasya can press any of these buttons. When the button is pressed, it turns on all the bulbs it's connected to. Can Vasya light up all the bulbs?If Vasya presses the button such that some bulbs connected to it are already turned on, they do not change their state, i.e. remain turned on.", "input_spec": "The first line of the input contains integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100)\u00a0\u2014 the number of buttons and the number of bulbs respectively. Each of the next n lines contains xi (0\u2009\u2264\u2009xi\u2009\u2264\u2009m)\u00a0\u2014 the number of bulbs that are turned on by the i-th button, and then xi numbers yij (1\u2009\u2264\u2009yij\u2009\u2264\u2009m)\u00a0\u2014 the numbers of these bulbs.", "output_spec": "If it's possible to turn on all m bulbs print \"YES\", otherwise print \"NO\".", "sample_inputs": ["3 4\n2 1 4\n3 1 3 1\n1 2", "3 3\n1 1\n1 2\n1 1"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first sample you can press each button once and turn on all the bulbs. In the 2 sample it is impossible to turn on the 3-rd lamp."}, "positive_code": [{"source_code": "($n, $m) = &{ $input = sub { split ' ', <> } };\n$r = '0' x $m;\nfor (1..$n) {\n\t(undef, @lamps) = &$input;\n\tsubstr($r, $_ - 1, 1, '1') for @lamps;\n}\nprint $r =~ /0/? 'NO': 'YES', \"\\n\";\n"}, {"source_code": "\n$l = <>;\n($n,$m) = ( $l =~ m/(\\d+)\\s+(\\d+)/ );\n\nmy %h;\nwhile ( $l = <> ) {\n\tmy @b = split( /\\s/, $l );\n\tshift @b;\n\tforeach (@b) {\n\t\t$h{$_} = 1;\n\t}\n}\nprint scalar( keys %h ) == $m? \"YES\" : \"NO\";\n"}, {"source_code": "(undef, $m) = split ' ', <>;\n@_ = <>;\nfor (@_){\n@A = split;\nshift @A;\nmap $B[$_] = 1, @A;\n}\n$f = 0;\n$B[ $_ ] or $f ++ for 1 .. $m;\nprint $f ? \"NO\" : \"YES\""}], "negative_code": [{"source_code": "\n$l = <>;\n($n,$m) = ( $l =~ m/(\\d+)\\s+(\\d+)/ );\n\nmy %h;\nwhile ( $l = <> ) {\n\tmy @b = split( /\\s/, $l );\n\tforeach (@b) {\n\t\t$h{$_} = 1;\n\t}\n}\nprint scalar( keys %h ) == $m? \"YES\" : \"NO\""}], "src_uid": "9438ce92e10e846dd3ab7c61a1a2e3af"} {"nl": {"description": "You are given an integer $$$n$$$. Check if $$$n$$$ has an odd divisor, greater than one (does there exist such a number $$$x$$$ ($$$x > 1$$$) that $$$n$$$ is divisible by $$$x$$$ and $$$x$$$ is odd).For example, if $$$n=6$$$, then there is $$$x=3$$$. If $$$n=4$$$, then such a number does not exist.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases. Then $$$t$$$ test cases follow. Each test case contains one integer $$$n$$$ ($$$2 \\le n \\le 10^{14}$$$). Please note, that the input 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.", "output_spec": "For each test case, output on a separate line: \"YES\" if $$$n$$$ has an odd divisor, greater than one; \"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": ["6\n2\n3\n4\n5\n998244353\n1099511627776"], "sample_outputs": ["NO\nYES\nNO\nYES\nYES\nNO"], "notes": null}, "positive_code": [{"source_code": "for (1..<>) {\r\n chomp (my $x = <>);\r\n my $flag = 0;\r\n my $i = 0;\r\n\r\n while (2**$i < 10**14) {\r\n (2**$i++ == $x) and $flag = 1;\r\n }\r\n\r\n print $flag ? \"NO\\n\" : \"YES\\n\";\r\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\t$_ = sprintf \"%b\", $_;\n\t\n\tmy $ones = () = /1/g;\n\t\n\tprint $ones == 1 ? \"NO\" : \"YES\";\n\t}"}], "negative_code": [{"source_code": "chomp (my $x = <>);\r\nmy $flag = 0;\r\nmy $i = 0;\r\n\r\nwhile (2**$i < 10**14) {\r\n (2**$i++ == $x) and $flag = 1;\r\n}\r\n\r\nprint $flag ? \"NO\\n\" : \"YES\\n\";\r\n\r\nexit;"}], "src_uid": "f4958b4833cafa46fa71357ab1ae41af"} {"nl": {"description": "There is a special offer in Vasya's favourite supermarket: if the customer buys $$$a$$$ chocolate bars, he or she may take $$$b$$$ additional bars for free. This special offer can be used any number of times.Vasya currently has $$$s$$$ roubles, and he wants to get as many chocolate bars for free. Each chocolate bar costs $$$c$$$ roubles. Help Vasya to calculate the maximum possible number of chocolate bars he can get!", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) \u2014 the number of testcases. Each of the next $$$t$$$ lines contains four integers $$$s, a, b, c~(1 \\le s, a, b, c \\le 10^9)$$$ \u2014 the number of roubles Vasya has, the number of chocolate bars you have to buy to use the special offer, the number of bars you get for free, and the cost of one bar, respectively.", "output_spec": "Print $$$t$$$ lines. $$$i$$$-th line should contain the maximum possible number of chocolate bars Vasya can get in $$$i$$$-th test.", "sample_inputs": ["2\n10 3 1 1\n1000000000 1 1000000000 1"], "sample_outputs": ["13\n1000000001000000000"], "notes": "NoteIn the first test of the example Vasya can buy $$$9$$$ bars, get $$$3$$$ for free, buy another bar, and so he will get $$$13$$$ bars.In the second test Vasya buys $$$1000000000$$$ bars and gets $$$1000000000000000000$$$ for free. So he has $$$1000000001000000000$$$ bars."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $s, $A, $B, $c ) = split;\n\t\n\tmy $cnt = 0;\n\t\n\tmy $akc = int( $s / ( $A * $c ) );\n\t\n\tmy $mod = $s % ( $A * $c );\n\t\n#\tprint \"$akc | $mod\";\n\t\n\t$cnt += $akc * ( $A + $B );\n\t\n\t$cnt += int( $mod / $c );\n\t\n\tprint $cnt;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $s, $A, $B, $c ) = split;\n\t\n\tmy $cnt = 0;\n\t\n\tmy $akc = int( $s / ( $A * $c ) );\n\t\n\tmy $mod = $s % ( $A * $c );\n\t\n#\tprint \"$akc | $mod\";\n\t\n\t$cnt += $akc * ( $A + $B );\n\t\n\t$cnt += $mod;\n\t\n\tprint $cnt;\n\t}"}], "src_uid": "ec8060260a6c7f4ff3e6afc9fd248afc"} {"nl": {"description": "Vladik is a competitive programmer. This year he is going to win the International Olympiad in Informatics. But it is not as easy as it sounds: the question Vladik face now is to find the cheapest way to get to the olympiad.Vladik knows n airports. All the airports are located on a straight line. Each airport has unique id from 1 to n, Vladik's house is situated next to the airport with id a, and the place of the olympiad is situated next to the airport with id b. It is possible that Vladik's house and the place of the olympiad are located near the same airport. To get to the olympiad, Vladik can fly between any pair of airports any number of times, but he has to start his route at the airport a and finish it at the airport b.Each airport belongs to one of two companies. The cost of flight from the airport i to the airport j is zero if both airports belong to the same company, and |i\u2009-\u2009j| if they belong to different companies.Print the minimum cost Vladik has to pay to get to the olympiad.", "input_spec": "The first line contains three integers n, a, and b (1\u2009\u2264\u2009n\u2009\u2264\u2009105, 1\u2009\u2264\u2009a,\u2009b\u2009\u2264\u2009n)\u00a0\u2014 the number of airports, the id of the airport from which Vladik starts his route and the id of the airport which he has to reach. The second line contains a string with length n, which consists only of characters 0 and 1. If the i-th character in this string is 0, then i-th airport belongs to first company, otherwise it belongs to the second.", "output_spec": "Print single integer\u00a0\u2014 the minimum cost Vladik has to pay to get to the olympiad.", "sample_inputs": ["4 1 4\n1010", "5 5 2\n10110"], "sample_outputs": ["1", "0"], "notes": "NoteIn the first example Vladik can fly to the airport 2 at first and pay |1\u2009-\u20092|\u2009=\u20091 (because the airports belong to different companies), and then fly from the airport 2 to the airport 4 for free (because the airports belong to the same company). So the cost of the whole flight is equal to 1. It's impossible to get to the olympiad for free, so the answer is equal to 1. In the second example Vladik can fly directly from the airport 5 to the airport 2, because they belong to the same company."}, "positive_code": [{"source_code": "$/ = \"\"; $_ = <>; ($n, $a, $b, $s) = split;\n$a = substr $s, $a - 1, 1;\n$b = substr $s, $b - 1, 1;\nprint $a eq $b? 0: 1;\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $tmp = <>;\n$tmp =~ s/\\s+$//;\nmy ($n, $fs, $sn) = split /\\s+/, $tmp;\n\nmy $s = <>;\n$s =~ s/\\s+$//;\n\nif (substr($s, $fs - 1, 1) eq substr($s, $sn - 1, 1)) {\n print \"0\";\n} else {\n print \"1\";\n}"}, {"source_code": "use strict;\n\n$,=\" \";\nmy ($n,$ax,$bx)=split / /,<>;\nchomp($n,$ax,$bx);\n$ax--;\n$bx--;\n$n--;\nmy $str=<>;\nchomp($str);\nmy @aa=split //,$str;\n\nif($aa[$ax]==$aa[$bx]){\n\tprint \"0 \\n\";\n}else{\n\tprint \"1 \\n\";\n\t}\n\n\n\n\n\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $A, $B ) = split;\n\t$_ = <>, chomp;\n\t\n\tmy $CA = substr $_, $A - 1, 1;\n\tmy $CB = substr $_, $B - 1, 1;\n\t\n\tprint do {\n\t\tif( abs( $CA - $CB ) == 1 ){\n\t\t\t1;\n\t\t\t}\n\t\telse{\n\t\t\t0;\n\t\t\t}\n\t\t};\n\t}"}, {"source_code": "( $n, $A, $B, $_ ) = split ' ', <>.<>;\n@_ = split //;\n\t\nprint $_[$A-1] - $_[$B-1] ? 1 : 0"}], "negative_code": [{"source_code": "use strict;\nuse Data::Dumper;\nuse warnings;\n\n\nmy ($n,$ax,$bx)=split / /,<>;\nchomp($n,$ax,$bx);\n$ax--;\n$bx--;\n$n--;\nmy $str=<>;\nchomp($str);\nmy @aa=split //,$str;\nmy %b_cost;\nmy %a_cost;\nfor my $k(0..$n){\n $b_cost{$k}->[0]=$aa[$k]-$aa[$bx];\n\t$b_cost{$k}->[1]=abs($bx-$k);\t\t\n}\n\n\n\n\nif($b_cost{$ax}->[0]==0){\n\tprint \"0\\n\";\n}\nelse{\n\nfor my $k (sort { $b_cost{$a}->[1] <=> $b_cost{$b}->[1] } keys %b_cost){\nif($b_cost{$k}->[0]==1){\n\tprint $b_cost{$k}->[1] . \"\\n\";\n\tlast;\n}\n}\n}\n"}, {"source_code": "use strict;\nuse Data::Dumper;\nuse warnings;\n\n\nmy ($n,$ax,$bx)=split / /,<>;\nchomp($n,$ax,$bx);\n$ax--;\n$bx--;\n$n--;\nmy $str=<>;\nchomp($str);\nmy @aa=split //,$str;\nmy %b_cost;\nmy %a_cost;\nfor my $k(0..$n){\n $b_cost{$k}->[0]=abs($aa[$k]-$aa[$bx]);\n\t$b_cost{$k}->[1]=abs($bx-$k);\t\t\n}\n\n\n\n\nif($b_cost{$ax}->[0]==0){\n\tprint \"0\\n\";\n}\nelse{\nfor my $k (sort { $b_cost{$a}->[1] <=> $b_cost{$b}->[1] } keys %b_cost){\nif($b_cost{$k}->[0]==1){\n\tprint $b_cost{$k}->[1] . \"\\n\";\n\tlast;\n}\n}\n}\n\n"}, {"source_code": "use strict;\n\n$,=\" \";\nmy ($n,$ax,$bx)=split / /,<>;\nchomp($n,$ax,$bx);\n$ax--;\n$bx--;\n$n--;\nmy $str=<>;\nchomp($str);\nmy @aa=split //,$str;\n\nmy $b_cost=0; \nmy $ans=10000000000;\nif($aa[$ax]==$aa[$bx])\n{\n\tprint \"0\\n\";\n}\nelse\n{\nfor my $k(0..$n){ \n$b_cost=0; \nmy $a_to_vertx=0;\nmy $b_to_vertx=0;\n if($aa[$k]==$aa[$ax]){\n \t$a_to_vertx=0;\n }else{\n \t$a_to_vertx=abs($ax-$k);\n }\n if($aa[$k]==$aa[$bx]){\n \t$b_to_vertx=0;\n }else{\n \t$b_to_vertx=abs($bx-$k);\n }\n #print $a_to_vertx,$b_to_vertx . \"\\n\";\n $b_cost=$a_to_vertx+$b_to_vertx;\n $ans=min($b_cost,$ans);\n}\nprint $ans . \"\\n\";\n}\n\n\n\nsub min{\n\treturn $_[0]>$_[1] ? $_[1] : $_[0];\n}\n\n\n\n\n\n"}], "src_uid": "081f30ac27739002855e9d2aebe804c7"} {"nl": {"description": "Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitaly's algorithm works with string s, consisting of characters \"x\" and \"y\", and uses two following operations at runtime: Find two consecutive characters in the string, such that the first of them equals \"y\", and the second one equals \"x\" and swap them. If there are several suitable pairs of characters, we choose the pair of characters that is located closer to the beginning of the string. Find in the string two consecutive characters, such that the first of them equals \"x\" and the second one equals \"y\". Remove these characters from the string. If there are several suitable pairs of characters, we choose the pair of characters that is located closer to the beginning of the string. The input for the new algorithm is string s, and the algorithm works as follows: If you can apply at least one of the described operations to the string, go to step 2 of the algorithm. Otherwise, stop executing the algorithm and print the current string. If you can apply operation 1, then apply it. Otherwise, apply operation 2. After you apply the operation, go to step 1 of the algorithm. Now Vitaly wonders, what is going to be printed as the result of the algorithm's work, if the input receives string s.", "input_spec": "The first line contains a non-empty string s. It is guaranteed that the string only consists of characters \"x\" and \"y\". It is guaranteed that the string consists of at most 106 characters. It is guaranteed that as the result of the algorithm's execution won't be an empty string.", "output_spec": "In the only line print the string that is printed as the result of the algorithm's work, if the input of the algorithm input receives string s.", "sample_inputs": ["x", "yxyxy", "xxxxxy"], "sample_outputs": ["x", "y", "xxxx"], "notes": "NoteIn the first test the algorithm will end after the first step of the algorithm, as it is impossible to apply any operation. Thus, the string won't change.In the second test the transformation will be like this: string \"yxyxy\" transforms into string \"xyyxy\"; string \"xyyxy\" transforms into string \"xyxyy\"; string \"xyxyy\" transforms into string \"xxyyy\"; string \"xxyyy\" transforms into string \"xyy\"; string \"xyy\" transforms into string \"y\". As a result, we've got string \"y\". In the third test case only one transformation will take place: string \"xxxxxy\" transforms into string \"xxxx\". Thus, the answer will be string \"xxxx\"."}, "positive_code": [{"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Sun Dec 16 21:13:10 IST 2012\n# File Name: b\n# USAGE: \n# b \n# \n# \n#------------------------------------------------\nchomp (my $str = );\n\n#$str = 'x';\n#$str .= 'x' x 10 ** 6;\n#$str .= 'y' x 10 ** 6;\n\n$cnt = 0;\nforeach (split /\\s*/, $str) { if ($_ eq 'x') { $cnt++ } else { $cnt-- } }\n\nif ( $cnt > 0 ) { $sol = 'x' } else { $sol = 'y'; $cnt = 0 - $cnt }\n\nprint $sol x $cnt;\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$_=<>;\nchomp;\n@_=split//;\n$_ eq'x'and $x++ for@_;\n$y=@_-$x;\nprint (($x>$y?'x':'y')x($x+$y-min($x,$y)*2));\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=split//,<>;\n$_ eq'x'and $x++ for@_;\n$y=@_-$x;\nprint (($x>$y?'x':'y')x($x+$y-min($x,$y)*2));\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=split//,<>;chomp$_[@_-1];\n$_ eq'x'and $x++ for@_;\n$y=@_-$x;\nprint (($x>$y?'x':'y')x($x+$y-min($x,$y)*2));\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "src_uid": "528459e7624f90372cb2c3a915529a23"} {"nl": {"description": "For the New Year, Polycarp decided to send postcards to all his $$$n$$$ friends. He wants to make postcards with his own hands. For this purpose, he has a sheet of paper of size $$$w \\times h$$$, which can be cut into pieces.Polycarp can cut any sheet of paper $$$w \\times h$$$ that he has in only two cases: If $$$w$$$ is even, then he can cut the sheet in half and get two sheets of size $$$\\frac{w}{2} \\times h$$$; If $$$h$$$ is even, then he can cut the sheet in half and get two sheets of size $$$w \\times \\frac{h}{2}$$$; If $$$w$$$ and $$$h$$$ are even at the same time, then Polycarp can cut the sheet according to any of the rules above.After cutting a sheet of paper, the total number of sheets of paper is increased by $$$1$$$.Help Polycarp to find out if he can cut his sheet of size $$$w \\times h$$$ at into $$$n$$$ or more pieces, using only the rules described above.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing three integers $$$w$$$, $$$h$$$, $$$n$$$ ($$$1 \\le w, h \\le 10^4, 1 \\le n \\le 10^9$$$)\u00a0\u2014 the width and height of the sheet Polycarp has and the number of friends he needs to send a postcard to.", "output_spec": "For each test case, output on a separate line: \"YES\", if it is possible to cut a sheet of size $$$w \\times h$$$ into at least $$$n$$$ pieces; \"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\n2 2 3\n3 3 2\n5 10 2\n11 13 1\n1 4 4"], "sample_outputs": ["YES\nNO\nYES\nYES\nYES"], "notes": "NoteIn the first test case, you can first cut the $$$2 \\times 2$$$ sheet into two $$$2 \\times 1$$$ sheets, and then cut each of them into two more sheets. As a result, we get four sheets $$$1 \\times 1$$$. We can choose any three of them and send them to our friends.In the second test case, a $$$3 \\times 3$$$ sheet cannot be cut, so it is impossible to get two sheets.In the third test case, you can cut a $$$5 \\times 10$$$ sheet into two $$$5 \\times 5$$$ sheets.In the fourth test case, there is no need to cut the sheet, since we only need one sheet.In the fifth test case, you can first cut the $$$1 \\times 4$$$ sheet into two $$$1 \\times 2$$$ sheets, and then cut each of them into two more sheets. As a result, we get four sheets $$$1 \\times 1$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\n \r\nuse warnings;\r\nuse strict;\r\n \r\n$\\ = $/;\r\n\r\n<>;\r\n \r\nwhile(<>){\r\n\tmy( $w, $h, $n ) = split;\r\n\t\r\n\tmy @m = map { length s/.*?(0*)$/$1/r } \r\n\t\tmap { eval sprintf \"%0b\", $_ } $w, $h;\r\n\t\r\n\tfor( @m ){\r\n\t\t$_ > 13 and $_ = 13;\r\n\t\t}\r\n\t\r\n\tmy $m = eval join '*', map 2 ** $_, @m;\r\n\t\r\n\tprint $m >= $n ? \"YES\" : \"NO\";\r\n\t}"}, {"source_code": "for (1..<>) {\r\n chomp (my $in = <>);\r\n my ($a, $b, $n) = split / /, $in;\r\n\r\n $t = 0;\r\n until ($a%2) {\r\n $t++;\r\n $a /= 2;\r\n }\r\n until ($b%2) {\r\n $t++;\r\n $b /= 2;\r\n }\r\n print 2**$t >= $n ? \"YES\\n\" : \"NO\\n\";\r\n}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($w,$h,$n) = map { $_ - 0 } split(/\\s+/,);\r\n my $c1 = 1; my $c2 = 1;\r\n while( $w % 2 == 0 ){\r\n $c1 *= 2;\r\n $w /= 2;\r\n }\r\n while( $h % 2 == 0 ){\r\n $c2 *= 2;\r\n $h /= 2;\r\n }\r\n if( $c1 * $c2 >= $n ){\r\n print \"YES\\n\";\r\n } else {\r\n print \"NO\\n\";\r\n }\r\n\r\n}\r\n\r\nexit(0);\r\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $w, $h, $n ) = split;\n\t\n\tmy @m = map { length s/.*?(0*)$/$1/r } \n\t\tmap { eval sprintf \"%0b\", $_ } $w, $h;\n\t\n\tfor( @m ){\n\t\t$_ > 11 and $_ = 11;\n\t\t}\n\t\n\tmy $m = eval join '*', map 2 ** $_, @m;\n\t\n\tprint $m >= $n ? \"YES\" : \"NO\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $w, $h, $n ) = split;\n\t\n\tmy $m = length join '', map { s/.*?(0*)$/$1/r } \n\t\tmap { eval sprintf \"%0b\", $_ } $w, $h;\n\t\n\tif( $m < 11 ){\n\t\t$m = 2 ** $m;\n\t\t}\n\t\n\tprint $m >= $n ? \"YES\" : \"NO\";\n\t}"}], "src_uid": "a8201326dda46542b23dc4e528d413eb"} {"nl": {"description": "Kostya is a genial sculptor, he has an idea: to carve a marble sculpture in the shape of a sphere. Kostya has a friend Zahar who works at a career. Zahar knows about Kostya's idea and wants to present him a rectangular parallelepiped of marble from which he can carve the sphere. Zahar has n stones which are rectangular parallelepipeds. The edges sizes of the i-th of them are ai, bi and ci. He can take no more than two stones and present them to Kostya. If Zahar takes two stones, he should glue them together on one of the faces in order to get a new piece of rectangular parallelepiped of marble. Thus, it is possible to glue a pair of stones together if and only if two faces on which they are glued together match as rectangles. In such gluing it is allowed to rotate and flip the stones in any way. Help Zahar choose such a present so that Kostya can carve a sphere of the maximum possible volume and present it to Zahar.", "input_spec": "The first line contains the integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105). n lines follow, in the i-th of which there are three integers ai,\u2009bi and ci (1\u2009\u2264\u2009ai,\u2009bi,\u2009ci\u2009\u2264\u2009109)\u00a0\u2014 the lengths of edges of the i-th stone. Note, that two stones may have exactly the same sizes, but they still will be considered two different stones.", "output_spec": "In the first line print k (1\u2009\u2264\u2009k\u2009\u2264\u20092) the number of stones which Zahar has chosen. In the second line print k distinct integers from 1 to n\u00a0\u2014 the numbers of stones which Zahar needs to choose. Consider that stones are numbered from 1 to n in the order as they are given in the input data. You can print the stones in arbitrary order. If there are several answers print any of them. ", "sample_inputs": ["6\n5 5 5\n3 2 4\n1 4 1\n2 1 3\n3 2 4\n3 3 4", "7\n10 7 8\n5 10 3\n4 2 6\n5 5 5\n10 2 8\n4 2 1\n7 7 7"], "sample_outputs": ["1\n1", "2\n1 5"], "notes": "NoteIn the first example we can connect the pairs of stones: 2 and 4, the size of the parallelepiped: 3\u2009\u00d7\u20092\u2009\u00d7\u20095, the radius of the inscribed sphere 1 2 and 5, the size of the parallelepiped: 3\u2009\u00d7\u20092\u2009\u00d7\u20098 or 6\u2009\u00d7\u20092\u2009\u00d7\u20094 or 3\u2009\u00d7\u20094\u2009\u00d7\u20094, the radius of the inscribed sphere 1, or 1, or 1.5 respectively. 2 and 6, the size of the parallelepiped: 3\u2009\u00d7\u20095\u2009\u00d7\u20094, the radius of the inscribed sphere 1.5 4 and 5, the size of the parallelepiped: 3\u2009\u00d7\u20092\u2009\u00d7\u20095, the radius of the inscribed sphere 1 5 and 6, the size of the parallelepiped: 3\u2009\u00d7\u20094\u2009\u00d7\u20095, the radius of the inscribed sphere 1.5 Or take only one stone: 1 the size of the parallelepiped: 5\u2009\u00d7\u20095\u2009\u00d7\u20095, the radius of the inscribed sphere 2.5 2 the size of the parallelepiped: 3\u2009\u00d7\u20092\u2009\u00d7\u20094, the radius of the inscribed sphere 1 3 the size of the parallelepiped: 1\u2009\u00d7\u20094\u2009\u00d7\u20091, the radius of the inscribed sphere 0.5 4 the size of the parallelepiped: 2\u2009\u00d7\u20091\u2009\u00d7\u20093, the radius of the inscribed sphere 0.5 5 the size of the parallelepiped: 3\u2009\u00d7\u20092\u2009\u00d7\u20094, the radius of the inscribed sphere 1 6 the size of the parallelepiped: 3\u2009\u00d7\u20093\u2009\u00d7\u20094, the radius of the inscribed sphere 1.5 It is most profitable to take only the first stone. "}, "positive_code": [{"source_code": "$\\ = $/;\n\n<>;\n\n$i = 0;\nfor (<>){\n\t$i ++;\n\t($A, $B, $C) = sort {$a <=> $b} split;\n\t$H{$A}{$B}{$i} = $C;\n\t$H{$A}{$C}{$i} = $B;\n\t$H{$B}{$C}{$i} = $A;\n\t}\n\n$max = 0;\n\nfor $h (keys %H){\nfor $k (keys %{ $H{ $h } }){\n\t$hh = \\%{ $H{ $h }{ $k } };\n\t@s = grep defined, (sort { $hh->{ $b } <=> $hh->{ $a } } keys $hh)[0, 1];\n\t$c = @s > 1 ? $hh->{ $s[0] } + $hh->{ $s[1] } : $hh->{ $s[0] };\n\t$c > $h and $c = $h;\n\tif( $c > $max ){\n\t\t$max = $c;\n\t\t@ans = @s;\n\t\t}\n\t}\n\t}\n\n$ans[0] == $ans[1] and pop @ans;\nprint ~~ @ans, \"\\n@ans\"\n"}, {"source_code": "$\\ = $/;\n\n<>;\n\n$i = 0;\nfor (<>){\n\t$i ++;\n\t($A, $B, $C) = sort {$a <=> $b} split;\n\t$H{$A}{$B}{$i} = $C;\n\t$H{$A}{$C}{$i} = $B;\n\t$H{$B}{$C}{$i} = $A;\n\t}\n\n$max = 0;\n\nfor $h (keys %H){\nfor $k (keys %{ $H{ $h } }){\n\t@s = grep defined, (sort { $H{ $h }{ $k }{ $b } <=> $H{ $h }{ $k }{ $a } } keys %{ $H{ $h }{ $k } })[0, 1];\n\t$c = @s > 1 ? $H{ $h }{ $k }{ $s[0] } + $H{ $h }{ $k }{ $s[1] } : $H{ $h }{ $k }{ $s[0] };\n\t$c > $h and $c = $h;\n\tif( $c > $max ){\n\t\t$max = $c;\n\t\t@ans = @s;\n\t\t}\n\t}\n\t}\n\n$ans[0] == $ans[1] and pop @ans;\nprint ~~ @ans, \"\\n@ans\"\n"}, {"source_code": "$\\ = $/;\n\n<>;\n\n$i = 0;\nfor (<>){\n\t$i ++;\n\t($A, $B, $C) = split;\n\t$A > $B and ($A, $B) = ($B, $A);\n\t$B > $C and ($C, $B) = ($B, $C);\n\t$A > $B and ($A, $B) = ($B, $A);\n\tpush @{ $H{$A}{$B} }, [$i, $C];\n\tpush @{ $H{$A}{$C} }, [$i, $B];\n\tpush @{ $H{$B}{$C} }, [$i, $A];\n\t}\n\n$max = 0;\n\nfor $h (keys %H){\nfor (keys %{ $H{ $h } }){\n\t@s = grep defined, (sort { $b->[1] <=> $a->[1] } @{ $H{ $h }{ $_ } })[0, 1];\n\t$c = @s > 1 ? $s[0][1] + $s[1][1] : $s[0][1];\n\t$c > $h and $c = $h;\n\tif( $c > $max ){\n\t\t$max = $c;\n\t\t@ans = map $_->[0], @s;\n\t\t}\n\t}\n\t}\n\n$ans[0] == $ans[1] and pop @ans;\nprint ~~ @ans, \"\\n@ans\"\n"}, {"source_code": "$\\ = $/;\n\n<>;\n\n@_ = <>;\n\n$i = 0;\nfor (@_){\n\t$i ++;\n\t($A, $B, $C) = sort {$a <=> $b} split;\n\tpush @{ $H{$A}{$B} }, [$i, $C];\n\tpush @{ $H{$A}{$C} }, [$i, $B];\n\tpush @{ $H{$B}{$C} }, [$i, $A];\n\t}\n\n$max = 0;\n\nfor $h (keys %H){\nfor (keys %{ $H{ $h } }){\n\t@s = grep defined, (sort { $b->[1] <=> $a->[1] } @{ $H{ $h }{ $_ } })[0, 1];\n\t$c = @s > 1 ? $s[0][1] + $s[1][1] : $s[0][1];\n\t$c > $h and $c = $h;\n\tif( $c > $max ){\n\t\t$max = $c;\n\t\t@ans = map $_->[0], @s;\n\t\t}\n\t}\n\t}\n\n$ans[0] == $ans[1] and pop @ans;\nprint ~~ @ans, \"\\n@ans\"\n"}], "negative_code": [{"source_code": "$\\ = $/;\n\n<>;\n\n@_ = <>;\n\n$i = 0;\nfor (@_){\n\t$i ++;\n\t($A, $B, $C) = sort {$a <=> $b} split;\n\tpush @{ $H{\"$A $B\"} }, [$i, $C];\n\tpush @{ $H{\"$A $C\"} }, [$i, $B];\n\tpush @{ $H{\"$B $C\"} }, [$i, $A];\n\t}\n\n$max = 0;\n\nfor (keys %H){\n\t@s = ($s1, $s2) = sort { $b->[1] <=> $a->[1] } @{ $H{ $_ } };\n\t$c = (sort {$a <=> $b} (split ' '), eval join '+', map $_->[1], @s)[ 0 ];\n\tif( $c > $max ){\n\t\t$max = $c;\n\t\t@ans = map $_->[0], @s;\n\t\t$ans[0] == $ans[1] and pop @ans;\n\t\t}\n\t}\n\t\nprint ~~ @ans, \"\\n@ans\"\n"}, {"source_code": "$\\ = $/;\n\n<>;\n\n@_ = <>;\n\n$i = 0;\nfor (@_){\n\t$i ++;\n\t($A, $B, $C) = sort {$a <=> $b} split;\n\tpush @{ $H{$A}{$B} }, [$i, $C];\n\tpush @{ $H{$A}{$C} }, [$i, $B];\n\tpush @{ $H{$B}{$C} }, [$i, $A];\n\t}\n\n$max = 0;\n\nfor $h (keys %H){\nfor (keys %{ $H{ $h } }){\n\t@s = grep defined, (sort { $b->[1] <=> $a->[1] } @{ $H{ $h }{ $_ } })[0, 1];\n\t$c = (sort {$a <=> $b} $h, eval join '+', map $_->[1], @s)[ 0 ];\n\tif( $c > $max ){\n\t\t$max = $c;\n\t\t@ans = map $_->[0], @s;\n\t\tprint \">>@ans\";\n\t\t}\n\t}\n\t}\n\n$ans[0] == $ans[1] and pop @ans;\nprint ~~ @ans, \"\\n@ans\"\n"}, {"source_code": "$\\ = $/;\n\n<>;\n\n$i = 0;\nfor (<>){\n\t$i ++;\n\t($A, $B, $C) = sort {$a <=> $b} split;\n\t$H{$A}{$B}{$i} = $C;\n\t$H{$A}{$C}{$i} = $B;\n\t$H{$B}{$C}{$i} = $A;\n\t}\n\n$max = 0;\n\nfor $h (keys %H){\nfor $k (keys %{ $H{ $h } }){\n\t$hh = \\%{ $H{ $h }{ $k } };\n\t@s = grep defined, (sort { $hh{ $b } <=> $hh{ $a } } keys $hh)[0, 1];\n\t$c = @s > 1 ? $hh->{ $s[0] } + $hh->{ $s[1] } : $hh->{ $s[0] };\n\t$c > $h and $c = $h;\n\tif( $c > $max ){\n\t\t$max = $c;\n\t\t@ans = @s;\n\t\t}\n\t}\n\t}\n\n$ans[0] == $ans[1] and pop @ans;\nprint ~~ @ans, \"\\n@ans\"\n"}, {"source_code": "$\\ = $/;\n\n<>;\n\n@_ = <>;\n\n$i = 0;\nfor (@_){\n\t$i ++;\n\t($A, $B, $C) = sort {$a <=> $b} split;\n\tpush @{ $H{$A}{$B} }, [$i, $C];\n\tpush @{ $H{$A}{$C} }, [$i, $B];\n\tpush @{ $H{$B}{$C} }, [$i, $A];\n\t}\n\n$max = 0;\n\nfor $h (keys %H){\nfor (keys %{ $H{ $h } }){\n\t@s = grep defined, (sort { $b->[1] <=> $a->[1] } @{ $H{ $h }{ $_ } })[0, 1];\n\t$c = @s > 1 ? $s[0] + $s[1] : $s[0];\n\t$c > $h and $c = $h;\n\tif( $c > $max ){\n\t\t$max = $c;\n\t\t@ans = map $_->[0], @s;\n\t\t}\n\t}\n\t}\n\n$ans[0] == $ans[1] and pop @ans;\nprint ~~ @ans, \"\\n@ans\"\n"}], "src_uid": "ef82292a6591de818ddda9f426208291"} {"nl": {"description": "The Saratov State University Olympiad Programmers Training Center (SSU OPTC) has n students. For each student you know the number of times he/she has participated in the ACM ICPC world programming championship. According to the ACM ICPC rules, each person can participate in the world championship at most 5 times.The head of the SSU OPTC is recently gathering teams to participate in the world championship. Each team must consist of exactly three people, at that, any person cannot be a member of two or more teams. What maximum number of teams can the head make if he wants each team to participate in the world championship with the same members at least k times?", "input_spec": "The first line contains two integers, n and k (1\u2009\u2264\u2009n\u2009\u2264\u20092000;\u00a01\u2009\u2264\u2009k\u2009\u2264\u20095). The next line contains n integers: y1,\u2009y2,\u2009...,\u2009yn (0\u2009\u2264\u2009yi\u2009\u2264\u20095), where yi shows the number of times the i-th person participated in the ACM ICPC world championship.", "output_spec": "Print a single number \u2014 the answer to the problem.", "sample_inputs": ["5 2\n0 4 5 1 0", "6 4\n0 1 2 3 4 5", "6 5\n0 0 0 0 0 0"], "sample_outputs": ["1", "0", "2"], "notes": "NoteIn the first sample only one team could be made: the first, the fourth and the fifth participants.In the second sample no teams could be created.In the third sample two teams could be created. Any partition into two teams fits."}, "positive_code": [{"source_code": "($n, $k) = split ' ', <>;\nmap { ++$s if $_ <= 5 - $k } split ' ', <>;\nprint int($s / 3);\n"}, {"source_code": "my ($n, $k) = split / /, <>;\nmy @arr = split / /, <>;\nmy $count = 0;\n$_ += $k for(@arr);\n$count = $count + ($_ <=5 ? 1:0) for(@arr);\nprint int($count / 3);"}, {"source_code": "my ($n, $k) = split / /, <>;\nmy $count = 0;\n$_ <= 5 - $k && $count++ for(split / /, <>);\nprint int($count / 3);\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\nuse integer;\n\n($n, $k) = split / /, <>;\n($tot, $k) = (0, 5 - $k);\n$_<=$k and ++$tot foreach (split / /, <>);\nsay $tot/3;"}, {"source_code": "my ($n, $k) = split ' ', <>;\nmy @people = split ' ', <>;\nmy @rem = map { 5 - $_ } @people;\n\nmy $ok = int((scalar grep { $_ >= $k } @rem) / 3);\n\nprint \"$ok\\n\";\n"}, {"source_code": "my @firstline = split (' ', <>);\nmy @secondline = split (' ', <>);\nmy @goodguys;\nmy $n = $firstline[0];\nmy $k = $firstline[1];\nmy $goodguys = 0;\nforeach (@secondline){\n if (($k+$_) <= 5){\n $goodguys++;\n\n}\n}\nprint int($goodguys / 3);"}, {"source_code": "($n,$k)=split/ /,<>;\n$_<=5-$k && $i++ for split/ /,<>;\n$_=$i/3;\ns/\\./a/;\nprint 0+$_"}], "negative_code": [{"source_code": "$n, $k = split ' ', <>;\nmap { ++$s if $_ <= 5 - $k } split ' ', <>;\nprint int($s / 3);\n"}], "src_uid": "fbde1e2ee02055592ff72fb04366812b"} {"nl": {"description": "George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi.To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c\u2009\u2265\u2009d), by changing limits on the input data.However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.", "input_spec": "The first line contains two integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20093000) \u2014 the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009a1\u2009<\u2009a2\u2009<\u2009...\u2009<\u2009an\u2009\u2264\u2009106) \u2014 the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1,\u2009b2,\u2009...,\u2009bm (1\u2009\u2264\u2009b1\u2009\u2264\u2009b2...\u2009\u2264\u2009bm\u2009\u2264\u2009106) \u2014 the complexities of the problems prepared by George. ", "output_spec": "Print a single integer \u2014 the answer to the problem.", "sample_inputs": ["3 5\n1 2 3\n1 2 2 3 3", "3 5\n1 2 3\n1 1 1 1 1", "3 1\n2 3 4\n1"], "sample_outputs": ["0", "2", "3"], "notes": "NoteIn the first sample the set of the prepared problems meets the requirements for a good round.In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2,\u20093,\u20094. "}, "positive_code": [{"source_code": "while(<>){\n@a=split/ /,<>;\n@b=split/ /,<>;\n$i=$j=$k=0;\nfor $i(0..@a-1){\n\t$j==@b-0 and last;\n\t$a[$i]<=$b[$j++] ? $k++ : redo;\n\t}\nprint @a-$k\n}"}], "negative_code": [{"source_code": "<>;\n@a=split/ /,<>;\n@b=split/ /,<>;\nfor $i(0..@a-1){\n\t$j==@b-1 and last;\n\t$a[$i]<=$b[$j++] ? $k++ : redo;\n\t}\nprint @a-$k"}], "src_uid": "bf0422de4347a308d68a52421fbad0f3"} {"nl": {"description": "Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game.", "input_spec": "First line contains non-empty string s (1\u2009\u2264\u2009|s|\u2009\u2264\u2009105), consisting of lowercase English letters.", "output_spec": "Print resulting string u.", "sample_inputs": ["cab", "acdb"], "sample_outputs": ["abc", "abdc"], "notes": null}, "positive_code": [{"source_code": "\n# function returns true if the current\n# character is the smallest in the rest\n# otherwise, it returns false\n# of string s\n# parameters are the character and\n# the hash\nsub is_small {\n my $chr = shift @_;\n my $h_ref = shift @_;\n for my $i ( keys %{ $h_ref } ) {\n\tif ( ($i lt $chr) and ${ $h_ref }{ $i } > 0 ) {\n\t return 0;}}\t\t# return false if character is less than\n return 1;\t\t\t# chr and there are still chrs in the string\n}\n\n\n\n# initialize hash\nmy $h;\nfor my $i (a..z) {\n $h{ $i } = 0; }\n\n# read input\nmy $f = ;\nchomp ( $f );\n# put the string into an array\nmy @s = split m//, $f;\n\n# calculate frequencies for each character\n# in the string and store in the hash\nfor (@s) {\n $h{ $_ } += 1;}\n\n# strings described in problem statement\nmy @t;\nmy @u;\n\n# temporary varaibles for pushing and poping and shifting\nmy $front;\nmy $back;\n\n# main algorithm with two operations\nwhile ( @s > 0 ) {\n \n # remove character from front of string\n $front = shift @s;\t\t\n\n # decrement value for $front character\n $h { $front } -= 1;\n\n # remove key - value pair if value become zero\n if ( $h { $front } == 0 ) {\n\tdelete $h { $front }; }\n\n # push character to the end of @t string\n push @t, $front;\n\n # take advantage of short circuiting\n while ( @t > 0 and &is_small ( $t[ @t-1 ], \\%h ) ) {\n\n\t$back = pop @t;\n\tpush @u, $back;}\n}\n\n# put the remainder of @t onto @u\nwhile ( @t > 0 ) {\n $back = pop @t;\n push @u, $back; }\n\n# print the resuling string\nprint @u;\nprint \"\\n\";\n\n\n\n\n\n\n\n\n"}, {"source_code": "$s = <>;\nsub m1 {\n\tif ($s =~ s/.*$_//) {\n\t\t$w = $&;\n\t\t$n = $w =~ s/$_//g;\n\t\t$u .= $_ x $n;\n\t\t$t .= $w;\n\t}\n}\nsub m2 {\n\tif ($t =~ s/[a-$_]+$//) {\n\t\t$u .= reverse $&;\n\t}\n}\nm2, m1, m2 for a..z;\nprint $u;"}], "negative_code": [{"source_code": "\n# function returns true if the current\n# character is the smallest in the rest\n# otherwise, it returns false\n# of string s\n# parameters are the character and\n# the hash\nsub is_small {\n my $chr = shift @_;\n my $h_ref = shift @_;\n for my $i ( keys %{ $h_ref } ) {\n\tif ( ($i lt $chr) and ${ $h_ref }{ $i } > 0 ) {\n\t return 0;}}\t\t# return false if character is less than\n return 1;\t\t\t# chr and there are still chrs in the string\n}\n\n\n\n# initialize hash\nmy $h;\nfor my $i (a..z) {\n $h{ $i } = 0; }\n\n# read input\nmy $f = ;\nchomp ( $f );\n# put the string into an array\nmy @s = split m//, $f;\n\n# calculate frequencies for each character\n# in the string and store in the hash\nfor (@s) {\n $h{ $_ } += 1;}\n\n# strings described in problem statement\nmy @t;\nmy @u;\n\n# temporary varaibles for pushing and poping and shifting\nmy $front;\nmy $back;\n\n# main algorithm with two operations\nwhile ( @s > 0 ) {\n \n $front = shift @s;\n push @t, $front;\n if ( &is_small ( $front, \\%h ) ) {\n\t$back = pop @t;\n\tpush @u, $back;}\n}\n\n# put the remainder of @t onto @u\nwhile ( @t > 0 ) {\n $back = pop @t;\n push @u, $back; }\n\n# print the resuling string\nprint @u;\nprint \"\\n\";\n\n\n\n\n\n\n\n\n"}, {"source_code": "$s = <>;\nfor $c (\"a\"..\"z\") {\n\tif ($s =~ s/.*$c//) {\n\t\t$w = $&;\n\t\t$n = $w =~ s/$c//g;\n\t\t$u .= $c x $n;\n\t\t$t .= $w;\n\t}\n\tif ($t =~ s/[a-$c]+$//) {\n\t\t$u .= reverse $&;\n\t}\n}\nprint $u;\t\n\n"}], "src_uid": "e758ae072b8aed53038e4593a720276d"} {"nl": {"description": "Three friends are going to meet each other. Initially, the first friend stays at the position $$$x = a$$$, the second friend stays at the position $$$x = b$$$ and the third friend stays at the position $$$x = c$$$ on the coordinate axis $$$Ox$$$.In one minute each friend independently from other friends can change the position $$$x$$$ by $$$1$$$ to the left or by $$$1$$$ to the right (i.e. set $$$x := x - 1$$$ or $$$x := x + 1$$$) or even don't change it.Let's introduce the total pairwise distance \u2014 the sum of distances between each pair of friends. Let $$$a'$$$, $$$b'$$$ and $$$c'$$$ be the final positions of the first, the second and the third friend, correspondingly. Then the total pairwise distance is $$$|a' - b'| + |a' - c'| + |b' - c'|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.Friends are interested in the minimum total pairwise distance they can reach if they will move optimally. Each friend will move no more than once. So, more formally, they want to know the minimum total pairwise distance they can reach after one minute.You have to answer $$$q$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 1000$$$) \u2014 the number of test cases. The next $$$q$$$ lines describe test cases. The $$$i$$$-th test case is given as three integers $$$a, b$$$ and $$$c$$$ ($$$1 \\le a, b, c \\le 10^9$$$) \u2014 initial positions of the first, second and third friend correspondingly. The positions of friends can be equal.", "output_spec": "For each test case print the answer on it \u2014 the minimum total pairwise distance (the minimum sum of distances between each pair of friends) if friends change their positions optimally. Each friend will move no more than once. So, more formally, you have to find the minimum total pairwise distance they can reach after one minute.", "sample_inputs": ["8\n3 3 4\n10 20 30\n5 5 5\n2 4 3\n1 1000000000 1000000000\n1 1000000000 999999999\n3 2 5\n3 2 6"], "sample_outputs": ["0\n36\n0\n0\n1999999994\n1999999994\n2\n4"], "notes": null}, "positive_code": [{"source_code": "my $tests = <>;\n#printf(\"tests:$tests\\n\");\nchomp ($tests);\nforeach my $t (1..$tests) {\n my $in = <>;\n chomp($in);\n my @arr = split(\" \",$in);\n my @sorted = sort { $a <=> $b } @arr;\n my $a = $sorted[0];\n my $b = $sorted[1];\n my $c = $sorted[2];\n #print \"a,b,c:$a,$b,$c\\n\";\n if ($a != $b) {\n $a++;\n if ($b != $c) {\n $c--; \n } elsif ($a != $b) {\n $c--;\n $b--;\n }\n } elsif ($b != $c) {\n $c--;\n if ($c != $b) {\n $b++;\n $a++;\n }\n }\n \n #print \"a,b,c:$a,$b,$c\\n\";\n my $d = ($c - $b) + ($b - $a) + ($c - $a);\n print \"$d\\n\";\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B, $C ) = sort { $a <=> $b } split;\n\t\n\t$A ++ if $C - $A > 1;\n\t$C -- if $C - $A > 0;\n\t\n\tprint 2 * ( $C - $A );\n\t}"}], "negative_code": [{"source_code": "my $tests = <>;\n#printf(\"tests:$tests\\n\");\nchomp ($tests);\nforeach my $t (1..$tests) {\n my $in = <>;\n chomp($in);\n my @arr = split(\" \",$in);\n my @sorted = sort { $a <=> $b } @arr;\n my $a = $sorted[0];\n my $b = $sorted[1];\n my $c = $sorted[2];\n #print \"a,b,c:$a,$b,$c\\n\";\n if ($a != $b) {\n $a++;\n if ($b != $c) {\n $c--; \n } else {\n $c--;\n $b--;\n }\n }\n #print \"a,b,c:$a,$b,$c\\n\";\n my $d = ($c - $b) + ($b - $a) + ($c - $a);\n print \"$d\\n\";\n}"}, {"source_code": "my $tests = <>;\n#printf(\"tests:$tests\\n\");\nchomp ($tests);\nforeach my $t (1..$tests) {\n my $in = <>;\n chomp($in);\n my @arr = split(\" \",$in);\n my @sorted = sort { $a <=> $b } @arr;\n my $a = $sorted[0];\n my $b = $sorted[1];\n my $c = $sorted[2];\n #print \"a,b,c:$a,$b,$c\\n\";\n if ($a != $b) {\n $a++;\n if ($b != $c) {\n $c--; \n } else {\n $c--;\n $b--;\n }\n } elsif ($b != $c) {\n $c--;\n }\n \n #print \"a,b,c:$a,$b,$c\\n\";\n my $d = ($c - $b) + ($b - $a) + ($c - $a);\n print \"$d\\n\";\n}"}, {"source_code": "my $tests = <>;\n#printf(\"tests:$tests\\n\");\nchomp ($tests);\nforeach my $t (1..$tests) {\n my $in = <>;\n chomp($in);\n my @arr = split(\" \",$in);\n my @sorted = sort { $a <=> $b } @arr;\n my $a = $sorted[0];\n my $b = $sorted[1];\n my $c = $sorted[2];\n #print \"a,b,c:$a,$b,$c\\n\";\n if ($a != $b) {\n $a++;\n if ($b != $c) {\n $c--; \n } elsif ($a != $b) {\n $c--;\n $b--;\n }\n } elsif ($b != $c) {\n $c--;\n }\n \n #print \"a,b,c:$a,$b,$c\\n\";\n my $d = ($c - $b) + ($b - $a) + ($c - $a);\n print \"$d\\n\";\n}"}], "src_uid": "18f2e54e4147e8887da737d5b6639473"} {"nl": {"description": "You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, \"ADE\" and \"BD\" are subsequences of \"ABCDE\", but \"DEA\" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$. ", "input_spec": "The first line of the input contains integers $$$n$$$ ($$$1\\le n \\le 10^5$$$) and $$$k$$$ ($$$1 \\le k \\le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.", "output_spec": "Print the only integer\u00a0\u2014 the length of the longest good subsequence of string $$$s$$$.", "sample_inputs": ["9 3\nACAABCCAB", "9 4\nABCABCABC"], "sample_outputs": ["6", "0"], "notes": "NoteIn the first example, \"ACBCAB\" (\"ACAABCCAB\") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence \"CAB\" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$."}, "positive_code": [{"source_code": "use strict;\n#open STDIN, 'output.txt';\nmy ($n, $m) = split(' ', <>);\nmy $s = <>;\nmy @a = split(//, $s);\nmy %h;\nfor(my $i = 0; $i < $n; $i++){\n $h{$a[$i]}++;\n}\nmy $mini = 100000000;\nif(keys(%h) < $m){\n print 0;\n exit(0);\n}\nelse{\n while (my ($k, $v) = each(%h) ) {\n if($v < $mini){\n $mini = $v;\n }\n }\n}\nprint $mini * keys(%h);\n"}], "negative_code": [], "src_uid": "d9d5db63b1e48214d02abe9977709384"} {"nl": {"description": "You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak zombies, you arrived at a square room consisting of tiles forming an n\u2009\u00d7\u2009n grid. The rows are numbered 1 through n from top to bottom, and the columns are numbered 1 through n from left to right. At the far side of the room lies a door locked with evil magical forces. The following inscriptions are written on the door: The cleaning of all evil will awaken the door! Being a very senior adventurer, you immediately realize what this means. You notice that every single cell in the grid are initially evil. You should purify all of these cells.The only method of tile purification known to you is by casting the \"Purification\" spell. You cast this spell on a single tile \u2014 then, all cells that are located in the same row and all cells that are located in the same column as the selected tile become purified (including the selected tile)! It is allowed to purify a cell more than once.You would like to purify all n\u2009\u00d7\u2009n cells while minimizing the number of times you cast the \"Purification\" spell. This sounds very easy, but you just noticed that some tiles are particularly more evil than the other tiles. You cannot cast the \"Purification\" spell on those particularly more evil tiles, not even after they have been purified. They can still be purified if a cell sharing the same row or the same column gets selected by the \"Purification\" spell.Please find some way to purify all the cells with the minimum number of spells cast. Print -1 if there is no such way.", "input_spec": "The first line will contain a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100). Then, n lines follows, each contains n characters. The j-th character in the i-th row represents the cell located at row i and column j. It will be the character 'E' if it is a particularly more evil cell, and '.' otherwise.", "output_spec": "If there exists no way to purify all the cells, output -1. Otherwise, if your solution casts x \"Purification\" spells (where x is the minimum possible number of spells), output x lines. Each line should consist of two integers denoting the row and column numbers of the cell on which you should cast the \"Purification\" spell.", "sample_inputs": ["3\n.E.\nE.E\n.E.", "3\nEEE\nE..\nE.E", "5\nEE.EE\nE.EE.\nE...E\n.EE.E\nEE.EE"], "sample_outputs": ["1 1\n2 2\n3 3", "-1", "3 3\n1 3\n2 2\n4 4\n5 3"], "notes": "NoteThe first example is illustrated as follows. Purple tiles are evil tiles that have not yet been purified. Red tile is the tile on which \"Purification\" is cast. Yellow tiles are the tiles being purified as a result of the current \"Purification\" spell. Green tiles are tiles that have been purified previously. In the second example, it is impossible to purify the cell located at row 1 and column 1.For the third example: "}, "positive_code": [{"source_code": "while ($n=<>){\n chomp $n;\n \n @_=();\n for (1..$n){push @_,\"\".<>}\n chomp @_;\n \n sub rev{\n @m = @_;\n $k=$n;\n @n=();\n while ($k--){\n $line=\"\";\n for (@m){\n s/.//;\n $line.=$&;\n }\n push @n,$line;\n }\n return @n;\n }\n \n $e=0;\n for (@_){/\\./ or $e++}\n \n @_=rev (@_);\n\n $f=0;\n for (@_){/\\./ or $f++}\n \n $e and $f and print \"-1\\n\" and next;\n \n if ($e){\n $h=0;\n for (@_){\n $h++;\n $g=1;\n while (s/^E//){$g++}\n print \"$g $h\\n\"\n }\n }\n else {\n @_= rev (@_);\n $h=0;\n for (@_){\n $h++;\n $g=1;\n while (s/^E//){$g++}\n print \"$h $g\\n\"\n }\n }\n }"}], "negative_code": [{"source_code": "while ($n = <>) {\n chomp $n;\n $N=$n;\n @_=();\n while ($N--){\n push @_,($_=<>);\n }\n chomp @_;\n $i=0;\n# print \"@_\\n\";\n \n $s=\"0\"x$n;\n @s=split//,$s;\n \n $e=0;\n $l=0;\n foreach (@_){\n $l++;\n unless (/E/) {$e++; for $ll(1..$n){print \"$l $ll\\n\"};last}\n }\n $e and next;\n \n # print \"jj\\n\";\n \n $k=$n;\n @m=@_;\n while ($k){\n \n \n $f=1;\n foreach (@m){\n s/.//;\n $& eq \"E\" and $f--;\n \n }\n if ($f==1) {for $ll(1..$n){print $ll,\" \",$n-$k+1,\"\\n\"};last}\n \n $k--;\n }\n $f==1 and next;\n \n \n $e=0;\n foreach (@_){\n unless (/\\./) {print \"-1\\n\"; $e++; last} ;\n }\n $e and next; \n \n @m=@_;\n $k=$n;\n while ($k--){\n \n $f=1;\n foreach (@m){\n s/.//;\n $& eq \".\" and $f--;\n \n }\n if ($f==1) {print \"-1\\n\"; last}\n \n }\n $f==1 and next;\n \n $a=0;\n for (@_){\n $a++;\n $b=1;\n while (s/^E//){$b++}\n print \"$a $b\\n\";\n \n \n }\n \n \n}"}, {"source_code": "while ($n = <>) {\n chomp $n;\n $N=$n;\n @_=();\n while ($N--){\n push @_,($_=<>);\n }\n chomp @_;\n $i=0;\n# print \"@_\\n\";\n \n $s=\"0\"x$n;\n @s=split//,$s;\n \n $e=0;\n $l=0;\n foreach (@_){\n $l++;\n unless (/E/) {$e++; for $ll(1..$n){print \"$l $ll\\n\"};last}\n }\n $e and next;\n \n # print \"jj\\n\";\n \n $k=$n;\n @m=@_;\n while ($k){\n \n \n $f=1;\n foreach (@m){\n s/.//;\n $& eq \"E\" and $f--;\n \n }\n if ($f==1) {for $ll(1..$n){print $ll,$n-$k+1,\"\\n\"};last}\n \n $k--;\n }\n $f==1 and next;\n \n \n $e=0;\n foreach (@_){\n unless (/\\./) {print \"--1\\n\"; $e++; last} ;\n }\n $e and next; \n \n @m=@_;\n $k=$n;\n while ($k--){\n \n $f=1;\n foreach (@m){\n s/.//;\n $& eq \".\" and $f--;\n \n }\n if ($f==1) {print \"-1\\n\"; last}\n \n }\n $f==1 and next;\n \n $a=0;\n for (@_){\n $a++;\n $b=1;\n while (s/^E//){$b++}\n print \"$a $b\\n\";\n \n \n }\n \n \n}"}, {"source_code": "while ($n = <>) {\n chomp $n;\n $N=$n;\n @_=();\n while ($N--){\n push @_,($_=<>);\n }\n chomp @_;\n $i=0;\n# print \"@_\\n\";\n \n $s=\"0\"x$n;\n @s=split//,$s;\n \n $e=0;\n $l=0;\n foreach (@_){\n $l++;\n unless (/E/) {$e++; for $ll(1..$n){print \"$l $ll\\n\"};last}\n }\n $e and next;\n \n # print \"jj\\n\";\n \n $k=$n;\n @m=@_;\n while ($k){\n \n \n $f=1;\n foreach (@m){\n s/.//;\n $& eq \"E\" and $f--;\n \n }\n if ($f==1) {for $ll(1..$n){print $ll,$n-$k+1,\"\\n\"};last}\n \n $k--;\n }\n $f==1 and next;\n \n \n $e=0;\n foreach (@_){\n unless (/\\./) {print \"-1\\n\"; $e++; last} ;\n }\n $e and next; \n \n @m=@_;\n $k=$n;\n while ($k--){\n \n $f=1;\n foreach (@m){\n s/.//;\n $& eq \".\" and $f--;\n \n }\n if ($f==1) {print \"-1\\n\"; last}\n \n }\n $f==1 and next;\n \n $a=0;\n for (@_){\n $a++;\n $b=1;\n while (s/^E//){$b++}\n print \"$a $b\\n\";\n \n \n }\n \n \n}"}], "src_uid": "18554f1bb56e192d9a4bc75047fa5df5"} {"nl": {"description": "You've got string s, consisting of only lowercase English letters. Find its lexicographically maximum subsequence.We'll call a non-empty string s[p1p2... pk]\u2009=\u2009sp1sp2... spk(1\u2009\u2264\u2009p1\u2009<\u2009p2\u2009<\u2009...\u2009<\u2009pk\u2009\u2264\u2009|s|) a subsequence of string s\u2009=\u2009s1s2... s|s|.String x\u2009=\u2009x1x2... x|x| is lexicographically larger than string y\u2009=\u2009y1y2... y|y|, if either |x|\u2009>\u2009|y| and x1\u2009=\u2009y1,\u2009x2\u2009=\u2009y2,\u2009... ,\u2009x|y|\u2009=\u2009y|y|, or exists such number r (r\u2009<\u2009|x|,\u2009r\u2009<\u2009|y|), that x1\u2009=\u2009y1,\u2009x2\u2009=\u2009y2,\u2009... ,\u2009xr\u2009=\u2009yr and xr\u2009+\u20091\u2009>\u2009yr\u2009+\u20091. Characters in lines are compared like their ASCII codes.", "input_spec": "The single line contains a non-empty string s, consisting only of lowercase English letters. The string's length doesn't exceed 105.", "output_spec": "Print the lexicographically maximum subsequence of string s.", "sample_inputs": ["ababba", "abbcbccacbbcbaaba"], "sample_outputs": ["bbba", "cccccbba"], "notes": "NoteLet's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters).The first sample: aBaBBAThe second sample: abbCbCCaCbbCBaaBA"}, "positive_code": [{"source_code": "#!perl -w\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my $s = <>;\n chomp($s);\n my @str1 = split //, $s;\n my ( $min, @str2 ) = ( $str1[$#str1], () );\n for ( my $i = @str1 - 1; $i >= 0; $i-- ) {\n if ( $str1[$i] ge $min ) {\n unshift @str2, $str1[$i];\n $min = $str1[$i];\n }\n }\n print @str2;\n}\n\n__END__\n"}], "negative_code": [], "src_uid": "77e2a6ba510987ed514fed3bd547b5ab"} {"nl": {"description": "Consider a football tournament where n teams participate. Each team has two football kits: for home games, and for away games. The kit for home games of the i-th team has color xi and the kit for away games of this team has color yi (xi\u2009\u2260\u2009yi).In the tournament, each team plays exactly one home game and exactly one away game with each other team (n(n\u2009-\u20091) games in total). The team, that plays the home game, traditionally plays in its home kit. The team that plays an away game plays in its away kit. However, if two teams has the kits of the same color, they cannot be distinguished. In this case the away team plays in its home kit.Calculate how many games in the described tournament each team plays in its home kit and how many games it plays in its away kit.", "input_spec": "The first line contains a single integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of teams. Next n lines contain the description of the teams. The i-th line contains two space-separated numbers xi, yi (1\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009105;\u00a0xi\u2009\u2260\u2009yi) \u2014 the color numbers for the home and away kits of the i-th team.", "output_spec": "For each team, print on a single line two space-separated integers \u2014 the number of games this team is going to play in home and away kits, correspondingly. Print the answers for the teams in the order they appeared in the input.", "sample_inputs": ["2\n1 2\n2 1", "3\n1 2\n2 1\n1 3"], "sample_outputs": ["2 0\n2 0", "3 1\n4 0\n2 2"], "notes": null}, "positive_code": [{"source_code": "<>;\n$k[(($a,$_[$i++])=split/ /)[0]]++ for <>;\nprint @_-1+$k[$_[$j]],\" \",@_-1-$k[$_[$j]],\"\\n\" and $j++ for @_\n"}, {"source_code": "<>;\n$k[(($a,$_[$i++])=split/ /)[0]]++ for <>;\nprint @_-1+$k[$_[$j]],\" \",@_-1-$k[$_[$j]],\"\\n\" and $j++ for @_\n"}, {"source_code": "<>;\n$k[(($a,$_[$i++])=split/ /)[0]]++ for <>;\nprint @_-1+$k[$_[$j]],\" \",@_-1-$k[$_[$j]],\"\\n\" and $j++ for @_"}, {"source_code": "<>;\n$k[(($a,$_[$i++])=split/ /)[0]]++ for <>;\nprint @_-1+$k[$_[$j]],\" \",@_-1-$k[$_[$j]],\"\\n\" and $j++ for @_\n"}, {"source_code": "<>;\n$k[(($a,$_[$i++])=split/ /)[0]]++ for <>;\nprint @_-1+$k[$_[$j]],\" \",@_-1-$k[$_[$j]],\"\\n\" and $j++ for @_\n"}, {"source_code": "chomp($n=<>);\nmy %cnt = {};\nwhile (<>) {\n\tchomp;\n\tpush @a, [split / /];\n\t++$cnt[$a[-1][0]];\n}\nmy $m = 2*($n-1);\nfor my $s (@a) {\n\t$v = $n-1;\n\t$v += $cnt[@$s[1]];\n\tprint $v, ' ', $m-$v, \"\\n\";\n}"}, {"source_code": "<>;\n$k[(($a,$_[$i++])=split/ /)[0]]++ for <>;\nprint @_-1+$k[$_[$j]],\" \",@_-1-$k[$_[$j]],\"\\n\" and $j++ for @_\n"}, {"source_code": "<>;\n$k[(($a,$_[$i++])=split/ /)[0]]++ for <>;\nprint @_-1+$k[$_[$j]],\" \",@_-1-$k[$_[$j]],\"\\n\" and $j++ for @_\n"}, {"source_code": "<>;\n$k[(($a,$_[$i++])=split/ /)[0]]++ for <>;\nprint @_-1+$k[$_[$j]],\" \",@_-1-$k[$_[$j]],\"\\n\" and $j++ for @_\n"}, {"source_code": "<>;\nwhile(<>){\n\t($a,$_[$i++])=split/ /;\n\t$k[$a]++\n\t}\n\npush @p, (@_-1+$k[$_[$j]],).\" \".(@_-1-$k[$_[$j]]).\"\\n\" and $j++ for @_;\nprint @p"}, {"source_code": "<>;\n$k[(($a,$_[$i++])=split/ /)[0]]++ for <>;\nprint @_-1+$k[$_[$j]],\" \",@_-1-$k[$_[$j]],\"\\n\" and $j++ for @_"}, {"source_code": "<>;\n$k[(($a,$_[$i++])=split/ /)[0]]++ for <>;\nprint @_-1+$k[$_[$j]],\" \",@_-1-$k[$_[$j]],\"\\n\" and $j++ for @_\n"}], "negative_code": [], "src_uid": "7899a22cee29bb96d671f6188f246c21"} {"nl": {"description": "You are given two matrices $$$A$$$ and $$$B$$$. Each matrix contains exactly $$$n$$$ rows and $$$m$$$ columns. Each element of $$$A$$$ is either $$$0$$$ or $$$1$$$; each element of $$$B$$$ is initially $$$0$$$.You may perform some operations with matrix $$$B$$$. During each operation, you choose any submatrix of $$$B$$$ having size $$$2 \\times 2$$$, and replace every element in the chosen submatrix with $$$1$$$. In other words, you choose two integers $$$x$$$ and $$$y$$$ such that $$$1 \\le x < n$$$ and $$$1 \\le y < m$$$, and then set $$$B_{x, y}$$$, $$$B_{x, y + 1}$$$, $$$B_{x + 1, y}$$$ and $$$B_{x + 1, y + 1}$$$ to $$$1$$$.Your goal is to make matrix $$$B$$$ equal to matrix $$$A$$$. Two matrices $$$A$$$ and $$$B$$$ are equal if and only if every element of matrix $$$A$$$ is equal to the corresponding element of matrix $$$B$$$.Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes $$$B$$$ equal to $$$A$$$. Note that you don't have to minimize the number of operations.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \\le n, m \\le 50$$$). Then $$$n$$$ lines follow, each containing $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line is $$$A_{i, j}$$$. Each integer is either $$$0$$$ or $$$1$$$.", "output_spec": "If it is impossible to make $$$B$$$ equal to $$$A$$$, print one integer $$$-1$$$. Otherwise, print any sequence of operations that transforms $$$B$$$ into $$$A$$$ in the following format: the first line should contain one integer $$$k$$$ \u2014 the number of operations, and then $$$k$$$ lines should follow, each line containing two integers $$$x$$$ and $$$y$$$ for the corresponding operation (set $$$B_{x, y}$$$, $$$B_{x, y + 1}$$$, $$$B_{x + 1, y}$$$ and $$$B_{x + 1, y + 1}$$$ to $$$1$$$). The condition $$$0 \\le k \\le 2500$$$ should hold.", "sample_inputs": ["3 3\n1 1 1\n1 1 1\n0 1 1", "3 3\n1 0 1\n1 0 1\n0 0 0", "3 2\n0 0\n0 0\n0 0"], "sample_outputs": ["3\n1 1\n1 2\n2 2", "-1", "0"], "notes": "NoteThe sequence of operations in the first example: $$$\\begin{matrix} 0 & 0 & 0 & & 1 & 1 & 0 & & 1 & 1 & 1 & & 1 & 1 & 1 \\\\ 0 & 0 & 0 & \\rightarrow & 1 & 1 & 0 & \\rightarrow & 1 & 1 & 1 & \\rightarrow & 1 & 1 & 1 \\\\ 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 0 & 0 & & 0 & 1 & 1 \\end{matrix}$$$ "}, "positive_code": [{"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my ($n, $m)= split(' ', $q);\n\n my @ma;\n my @mb;\n\n my $only_zeros= 1;\n for (my $i = 0; $i < $n; $i++) {\n $q= <$in>; chomp($q);\n push(@ma, [ split(' ', $q) ]);\n\n my @empty_row;\n for (my $j = 0; $j < $m; $j++) {\n $only_zeros=0 if($ma[$i][$j] > 0);\n push(@empty_row, 0);\n }\n\n push(@mb, \\@empty_row);\n }\n\n (print('0') && return) if($only_zeros);\n\n (print('-1') && return) if($n < 2 || $m < 2);\n # (print('-1') && return) if(!possible(\\@ma, $n, $m));\n #\n bar($n,$m,\\@ma,\\@mb);\n\n # print STDERR Dumper \\@ma;\n # print STDERR Dumper \\@mb;\n # print \"only_zeros $only_zeros\\n\";\n\n\n}\n\nsub possible {\n my ($m, $r, $c)= @_;\n\n my $possible= 1;\n\n OUT: for (my $i = 0; $i < $r; $i++) {\n for (my $j=0; $j < $c; $j++) {\n # print \"i: $i, j:$j\\n\";\n if(\n ( # case 1 is true\n ($m->[$i][$j] == 1) &&\n (($i+1 < $r) && ($m->[$i+1][$j] == 1)) &&\n (($j+1 < $c) && ($m->[$i][$j+1] == 1)) &&\n (($i+1 < $r) && ($j+1 < $c) && ($m->[$i+1][$j+1] == 1))\n ) || ( # case 2 is true\n ($m->[$i][$j] == 1) &&\n (($i+1 < $r) && ($m->[$i+1][$j] == 1)) &&\n (($j-1 >= 0) && ($m->[$i][$j-1] == 1)) &&\n (($i+1 < $r) && ($j-1 >= 0) && ($m->[$i+1][$j-1] == 1))\n ) || ( # case 3 is true\n ($m->[$i][$j] == 1) &&\n (($i-1 >= 0) && ($m->[$i-1][$j] == 1)) &&\n (($j+1 < $c) && ($m->[$i][$j+1] == 1)) &&\n (($i-1 >= 0) && ($j+1 < $c) && ($m->[$i-1][$j+1] == 1))\n ) || ( # case 4\n ($m->[$i][$j] == 1) &&\n (($i-1 >= 0) && ($m->[$i-1][$j] == 1)) &&\n (($j-1 >= 0) && ($m->[$i][$j-1] == 1)) &&\n (($i-1 >= 0) && ($j-1 >= 0) && ($m->[$i-1][$j-1] == 1))\n ) || ($m->[$i][$j] == 0)\n ) {\n # all good\n } else {\n # print \"Not Possible\\n\";\n $possible= 0;\n last OUT;\n }\n }\n }\n\n return $possible;\n}\n\nsub bar {\n my ($r, $c, $ma, $mb)= @_;\n\n my @changes;\n\n # print STDERR Dumper $ma;\n\n for (my $i = 0; $i < $r; $i++) {\n for (my $j = 0; $j < $c; $j++) {\n # print \"i: $i, j: $j r: $r, c: $c \";\n # print \"maij= \" . $ma->[$i][$j];\n # print $/;\n if(\n ($ma->[$i][$j] == 1) &&\n (\n (\n (($j<($c-1)) && ($ma->[$i][$j+1] == 1))\n ) &&\n (\n ($j<($c-1) && $i<($r-1)) && ($ma->[$i+1][$j+1] == 1)\n ) &&\n (\n (($i<($r-1)) && ($ma->[$i+1][$j] == 1))\n )\n )\n ) {\n push(@changes, [ $i+1, $j+1 ]);\n $mb->[$i][$j]=1;\n $mb->[$i+1][$j]=1;\n $mb->[$i][$j+1]=1;\n $mb->[$i+1][$j+1]=1;\n } else {\n (print('-1') && return) unless(\n $ma->[$i][$j] == $mb->[$i][$j]\n );\n }\n }\n }\n # $m->[$x][$y]= 1;\n # $m->[$x+1][$y]= 1;\n # $m->[$x][$y+1]= 1;\n # $m->[$x+1][$y+1]= 1;\n\n # print STDERR Dumper \\@changes;\n\n (print(-1) && return) if(scalar(@changes)>2500);\n\n print scalar(@changes);\n print $/;\n for my $a (@changes) {\n print join(' ', @{$a});\n print $/;\n }\n}\n\nmain() unless caller();\n\n"}, {"source_code": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $line = );\n\nmy ($n, $m) = split q{ }, $line;\n\nmy $a;\nfor my $y ( 1..$n ) {\n chomp ($line = );\n my @x = (1, split (q{ }, $line), 1);\n $a->[$y] = \\@x;\n}\n$a->[$n+1] = [(1)x($m+2)];\n\nmy $b;\n\nfor my $y ( 1..$n ) {\n my @x = ( (0)x($m+1), 1 );\n $b->[$y] = \\@x;\n}\n$b->[$n+1] = [(1)x($m+2)];\n\nmy $same_flag = TRUE;\n\nCHECKSAME:\nfor my $y (1..$n) {\n for my $x (1..$m) {\n if ( $b->[$y][$x] != $a->[$y][$x] ) {\n $same_flag = FALSE;\n last CHECKSAME;\n }\n }\n}\n\nif ( $same_flag == TRUE ) {\n say 0;\n exit(0);\n}\n\n\nmy $answer = [];\n\nfor my $y ( 1..$n-1 ) {\n for my $x ( 1..$m-1 ) {\n if ( converter_check($a, $y, $x) \n && !same($a, $b, $y, $x) ) {\n convert($b, $y, $x);\n push @$answer, [$y, $x];\n }\n }\n}\n\nfor my $y (1..$n) {\n for my $x (1..$m) {\n if ( $b->[$y][$x] != $a->[$y][$x] ) {\n say -1;\n exit(0);\n }\n }\n}\n\nsay scalar @$answer;\nfor ( @$answer ) {\n say \"$_->[0] $_->[1]\";\n}\n\nsub converter_check {\n my ($a, $y, $x) = @_;\n return FALSE \n if $a->[$y][$x] == 0 \n or $a->[$y][$x+1] == 0\n or $a->[$y+1][$x] == 0\n or $a->[$y+1][$x+1] == 0;\n return TRUE;\n}\n\nsub convert {\n my ($b, $y, $x) = @_;\n $b->[$y][$x] = 1;\n $b->[$y][$x+1] = 1;\n $b->[$y+1][$x] = 1;\n $b->[$y+1][$x+1] = 1;\n}\n\nsub same {\n my ($a, $b, $y, $x) = @_;\n return TRUE \n if $a->[$y][$x] == $b->[$y][$x] \n and $a->[$y][$x+1] == $b->[$y][$x+1]\n and $a->[$y+1][$x] == $b->[$y+1][$x]\n and $a->[$y+1][$x+1] == $b->[$y+1][$x+1];\n return FALSE;\n}\n"}, {"source_code": "( $n, $m, @c ) = split ' ', <>;\n\n$_ = join '', <>;\ny! !!d;\n\n$g = $m - 1;\n\n0 while\n\ts/\n\t\t[1,]{2} (.{$g})\n\t\t[1,]{2}\n\t\t(?{\n\t\t\tpush @c, join \" \", map int, ( pos ) \\/ ( $m + 1 ), ( pos ) % ( $m + 1 ) - 1;\n\t\t\t})\n\t/\n\t\t\":,\" . $1 .\n\t\t\",,\"\n\t/xse;\n\nprint /1/ ? -1 : join \"\\n\", ~~ @c, @c;"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print \"-\" x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\t$_ = join '', map ~~<>, 1 .. $n;\n\ty/ //d;\n\tchomp;\n\t\n\t$debug and print;\n\t\n\tmy $gap = $m - 2 + 1;\n\t\n\t0 while\n\t\ts/\n\t\t\t[1,]{2} (.{$gap})\n\t\t\t[1,]{2}\n\t\t/\n\t\t\t\":,\" . $1 .\n\t\t\t\",,\"\n\t\t/xse;\n\t\n\t$debug and print;\n\t\t\n\t/1/ and print -1 and next;\n\t\n\t@_ = split \"\\n\", $_;\n\t\n\tmy @coords;\n\t\n\tfor my $i ( 0 .. $n - 1 ){\n\t\twhile( $_[ $i ] =~ /:/g ){\n\t\t\tpush @coords, join \" \", $i + 1, pos $_[ $i ];\n\t\t\t}\n\t\t}\n\t\n\tprint for ~~ @coords, @coords;\n\t}"}], "negative_code": [{"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my ($n, $m)= split(' ', $q);\n\n my @ma;\n my @mb;\n\n my $only_zeros= 1;\n for (my $i = 0; $i < $n; $i++) {\n $q= <$in>; chomp($q);\n push(@ma, [ split(' ', $q) ]);\n\n my @empty_row;\n for (my $j = 0; $j < $m; $j++) {\n $only_zeros=0 if($ma[$i][$j] > 0);\n push(@empty_row, 0);\n }\n\n push(@mb, \\@empty_row);\n }\n\n (print('0') && return) if($only_zeros);\n\n (print('-1') && return) if($n < 2 || $m < 2);\n # (print('-1') && return) if(!possible(\\@ma, $n, $m));\n\n bar($n,$m,\\@ma,\\@mb);\n\n # print STDERR Dumper \\@ma;\n # print STDERR Dumper \\@mb;\n # print \"only_zeros $only_zeros\\n\";\n\n\n}\n\nsub possible {\n my ($m, $r, $c)= @_;\n\n my $possible= 1;\n\n OUT: for (my $i = 0; $i < $r; $i++) {\n for (my $j=0; $j < $c; $j++) {\n # print \"i: $i, j:$j\\n\";\n if(\n ( # case 1 is true\n ($m->[$i][$j] == 1) &&\n (($i+1 < $r) && ($m->[$i+1][$j] == 1)) &&\n (($j+1 < $c) && ($m->[$i][$j+1] == 1)) &&\n (($i+1 < $r) && ($j+1 < $c) && ($m->[$i+1][$j+1] == 1))\n ) || ( # case 2 is true\n ($m->[$i][$j] == 1) &&\n (($i+1 < $r) && ($m->[$i+1][$j] == 1)) &&\n (($j-1 >= 0) && ($m->[$i][$j-1] == 1)) &&\n (($i+1 < $r) && ($j-1 >= 0) && ($m->[$i+1][$j-1] == 1))\n ) || ( # case 3 is true\n ($m->[$i][$j] == 1) &&\n (($i-1 >= 0) && ($m->[$i-1][$j] == 1)) &&\n (($j+1 < $c) && ($m->[$i][$j+1] == 1)) &&\n (($i-1 >= 0) && ($j+1 < $c) && ($m->[$i-1][$j+1] == 1))\n ) || ( # case 4\n ($m->[$i][$j] == 1) &&\n (($i-1 >= 0) && ($m->[$i-1][$j] == 1)) &&\n (($j-1 >= 0) && ($m->[$i][$j-1] == 1)) &&\n (($i-1 >= 0) && ($j-1 >= 0) && ($m->[$i-1][$j-1] == 1))\n ) || ($m->[$i][$j] == 0)\n ) {\n # all good\n } else {\n # print \"Not Possible\\n\";\n $possible= 0;\n last OUT;\n }\n }\n }\n\n return $possible;\n}\n\nsub bar {\n my ($r, $c, $ma, $mb)= @_;\n\n my @changes;\n\n # print STDERR Dumper $ma;\n\n for (my $i = 0; $i < $r; $i++) {\n for (my $j = 0; $j < $c; $j++) {\n # print \"i: $i, j: $j r: $r, c: $c \";\n # print \"maij= \" . $ma->[$i][$j];\n # print $/;\n if(\n ($ma->[$i][$j] == 1) &&\n (\n (\n (($j<($c-1)) && ($ma->[$i][$j+1] == 1))\n ) &&\n (\n ($j<($c-1) && $i<($r-1)) && ($ma->[$i+1][$j+1] == 1)\n ) &&\n (\n (($i<($c-1)) && ($ma->[$i+1][$j] == 1))\n )\n\n # ( # either first col or col before is 0\n # ($j==0) ||\n # (($j>0) && ($ma->[$i][$j-1] == 0))\n # ) &&\n # ( # either first col and row, or before is 0\n # (\n # ($j==0 && $i==0)\n # ) ||\n # (($i>0 && $j>0) && ($ma->[$i-1][$j-1] == 0))\n # ) &&\n # ( # either first row or row before is 0\n # ($i==0) ||\n # (($i>0) && ($ma->[$i-1][$j] == 0))\n # )\n )\n ) {\n push(@changes, [ $i+1, $j+1 ]);\n $mb->[$i][$j]=1;\n $mb->[$i+1][$j]=1;\n $mb->[$i][$j+1]=1;\n $mb->[$i+1][$j+1]=1;\n } else {\n (print(-1) && return) unless(\n $ma->[$i][$j] == $mb->[$i][$j]\n );\n }\n }\n }\n # $m->[$x][$y]= 1;\n # $m->[$x+1][$y]= 1;\n # $m->[$x][$y+1]= 1;\n # $m->[$x+1][$y+1]= 1;\n\n # print STDERR Dumper \\@changes;\n\n (print(-1) && return) if(scalar(@changes)>2500);\n\n print scalar(@changes);\n print $/;\n for my $a (@changes) {\n print join(' ', @{$a});\n print $/;\n }\n}\n\nmain() unless caller();\n\n"}, {"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my ($n, $m)= split(' ', $q);\n\n my @ma;\n my @mb;\n\n my $only_zeros= 1;\n for (my $i = 0; $i < $n; $i++) {\n $q= <$in>; chomp($q);\n push(@ma, [ split(' ', $q) ]);\n\n my @empty_row;\n for (my $j = 0; $j < $m; $j++) {\n $only_zeros=0 if($ma[$i][$j] > 0);\n push(@empty_row, 0);\n }\n\n push(@mb, \\@empty_row);\n }\n\n (print('0') && return) if($only_zeros);\n\n (print('-1') && return) if($n < 2 || $m < 2);\n # (print('-1') && return) if(!possible(\\@ma, $n, $m));\n #\n bar($n,$m,\\@ma,\\@mb);\n\n # print STDERR Dumper \\@ma;\n # print STDERR Dumper \\@mb;\n # print \"only_zeros $only_zeros\\n\";\n\n\n}\n\nsub possible {\n my ($m, $r, $c)= @_;\n\n my $possible= 1;\n\n OUT: for (my $i = 0; $i < $r; $i++) {\n for (my $j=0; $j < $c; $j++) {\n # print \"i: $i, j:$j\\n\";\n if(\n ( # case 1 is true\n ($m->[$i][$j] == 1) &&\n (($i+1 < $r) && ($m->[$i+1][$j] == 1)) &&\n (($j+1 < $c) && ($m->[$i][$j+1] == 1)) &&\n (($i+1 < $r) && ($j+1 < $c) && ($m->[$i+1][$j+1] == 1))\n ) || ( # case 2 is true\n ($m->[$i][$j] == 1) &&\n (($i+1 < $r) && ($m->[$i+1][$j] == 1)) &&\n (($j-1 >= 0) && ($m->[$i][$j-1] == 1)) &&\n (($i+1 < $r) && ($j-1 >= 0) && ($m->[$i+1][$j-1] == 1))\n ) || ( # case 3 is true\n ($m->[$i][$j] == 1) &&\n (($i-1 >= 0) && ($m->[$i-1][$j] == 1)) &&\n (($j+1 < $c) && ($m->[$i][$j+1] == 1)) &&\n (($i-1 >= 0) && ($j+1 < $c) && ($m->[$i-1][$j+1] == 1))\n ) || ( # case 4\n ($m->[$i][$j] == 1) &&\n (($i-1 >= 0) && ($m->[$i-1][$j] == 1)) &&\n (($j-1 >= 0) && ($m->[$i][$j-1] == 1)) &&\n (($i-1 >= 0) && ($j-1 >= 0) && ($m->[$i-1][$j-1] == 1))\n ) || ($m->[$i][$j] == 0)\n ) {\n # all good\n } else {\n # print \"Not Possible\\n\";\n $possible= 0;\n last OUT;\n }\n }\n }\n\n return $possible;\n}\n\nsub bar {\n my ($r, $c, $ma, $mb)= @_;\n\n my @changes;\n\n # print STDERR Dumper $ma;\n\n for (my $i = 0; $i < $r; $i++) {\n for (my $j = 0; $j < $c; $j++) {\n # print \"i: $i, j: $j r: $r, c: $c \";\n # print \"maij= \" . $ma->[$i][$j];\n # print $/;\n if(\n ($ma->[$i][$j] == 1) &&\n (\n (\n (($j<($c-1)) && ($ma->[$i][$j+1] == 1))\n ) &&\n (\n ($j<($c-1) && $i<($r-1)) && ($ma->[$i+1][$j+1] == 1)\n ) &&\n (\n (($i<($r-1)) && ($ma->[$i+1][$j] == 1))\n )\n )\n ) {\n push(@changes, [ $i+1, $j+1 ]);\n $mb->[$i][$j]=1;\n $mb->[$i+1][$j]=1;\n $mb->[$i][$j+1]=1;\n $mb->[$i+1][$j+1]=1;\n } else {\n (print('-11') && return) unless(\n $ma->[$i][$j] == $mb->[$i][$j]\n );\n }\n }\n }\n # $m->[$x][$y]= 1;\n # $m->[$x+1][$y]= 1;\n # $m->[$x][$y+1]= 1;\n # $m->[$x+1][$y+1]= 1;\n\n # print STDERR Dumper \\@changes;\n\n (print(-1) && return) if(scalar(@changes)>2500);\n\n print scalar(@changes);\n print $/;\n for my $a (@changes) {\n print join(' ', @{$a});\n print $/;\n }\n}\n\nmain() unless caller();\n\n"}, {"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my ($n, $m)= split(' ', $q);\n\n my @ma;\n my @mb;\n\n my $only_zeros= 1;\n for (my $i = 0; $i < $n; $i++) {\n $q= <$in>; chomp($q);\n push(@ma, [ split(' ', $q) ]);\n\n my @empty_row;\n for (my $j = 0; $j < $m; $j++) {\n $only_zeros=0 if($ma[$i][$j] > 0);\n push(@empty_row, 0);\n }\n\n push(@mb, \\@empty_row);\n }\n\n (print('0') && return) if($only_zeros);\n\n (print('-1') && return) if($n < 3 || $m < 3);\n (print('-1') && return) if(!possible(\\@ma, $n, $m));\n\n bar($n,$m,\\@ma,\\@mb);\n\n # print STDERR Dumper \\@ma;\n # print STDERR Dumper \\@mb;\n # print \"only_zeros $only_zeros\\n\";\n\n\n}\n\nsub possible {\n my ($m, $r, $c)= @_;\n\n my $possible= 1;\n\n OUT: for (my $i = 0; $i < $r; $i++) {\n for (my $j=0; $j < $c; $j++) {\n # print \"i: $i, j:$j\\n\";\n if(\n ( # case 1 is true\n ($m->[$i][$j] == 1) &&\n (($i+1 < $r) && ($m->[$i+1][$j] == 1)) &&\n (($j+1 < $c) && ($m->[$i][$j+1] == 1)) &&\n (($i+1 < $r) && ($j+1 < $c) && ($m->[$i+1][$j+1] == 1))\n ) || ( # case 2 is true\n ($m->[$i][$j] == 1) &&\n (($i+1 < $r) && ($m->[$i+1][$j] == 1)) &&\n (($j-1 >= 0) && ($m->[$i][$j-1] == 1)) &&\n (($i+1 < $r) && ($j-1 >= 0) && ($m->[$i+1][$j-1] == 1))\n ) || ( # case 3 is true\n ($m->[$i][$j] == 1) &&\n (($i-1 >= 0) && ($m->[$i-1][$j] == 1)) &&\n (($j+1 < $c) && ($m->[$i][$j+1] == 1)) &&\n (($i-1 >= 0) && ($j+1 < $c) && ($m->[$i-1][$j+1] == 1))\n ) || ( # case 4\n ($m->[$i][$j] == 1) &&\n (($i-1 >= 0) && ($m->[$i-1][$j] == 1)) &&\n (($j-1 >= 0) && ($m->[$i][$j-1] == 1)) &&\n (($i-1 >= 0) && ($j-1 >= 0) && ($m->[$i-1][$j-1] == 1))\n ) || ($m->[$i][$j] == 0)\n ) {\n # all good\n } else {\n # print \"Not Possible\\n\";\n $possible= 0;\n last OUT;\n }\n }\n }\n\n return $possible;\n}\n\nsub bar {\n my ($r, $c, $ma, $mb)= @_;\n\n my @changes;\n\n # print STDERR Dumper $ma;\n\n for (my $i = 0; $i < $r; $i++) {\n for (my $j = 0; $j < $c; $j++) {\n # print \"i: $i, j: $j r: $r, c: $c \";\n # print \"maij= \" . $ma->[$i][$j];\n # print $/;\n if(\n ($ma->[$i][$j] == 1) &&\n (\n (\n (($j<($c-1)) && ($ma->[$i][$j+1] == 1))\n ) &&\n (\n ($j<($c-1) && $i<($r-1)) && ($ma->[$i+1][$j+1] == 1)\n ) &&\n (\n (($i<($c-1)) && ($ma->[$i+1][$j] == 1))\n )\n\n # ( # either first col or col before is 0\n # ($j==0) ||\n # (($j>0) && ($ma->[$i][$j-1] == 0))\n # ) &&\n # ( # either first col and row, or before is 0\n # (\n # ($j==0 && $i==0)\n # ) ||\n # (($i>0 && $j>0) && ($ma->[$i-1][$j-1] == 0))\n # ) &&\n # ( # either first row or row before is 0\n # ($i==0) ||\n # (($i>0) && ($ma->[$i-1][$j] == 0))\n # )\n )\n ) {\n push(@changes, [ $i+1, $j+1 ]);\n }\n }\n }\n # $m->[$x][$y]= 1;\n # $m->[$x+1][$y]= 1;\n # $m->[$x][$y+1]= 1;\n # $m->[$x+1][$y+1]= 1;\n\n # print STDERR Dumper \\@changes;\n\n print scalar(@changes);\n print $/;\n for my $a (@changes) {\n print join(' ', @{$a});\n print $/;\n }\n}\n\nmain() unless caller();\n\n"}, {"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my ($n, $m)= split(' ', $q);\n\n my @ma;\n my @mb;\n\n my $only_zeros= 1;\n for (my $i = 0; $i < $n; $i++) {\n $q= <$in>; chomp($q);\n push(@ma, [ split(' ', $q) ]);\n\n my @empty_row;\n for (my $j = 0; $j < $m; $j++) {\n $only_zeros=0 if($ma[$i][$j] > 0);\n push(@empty_row, 0);\n }\n\n push(@mb, \\@empty_row);\n }\n\n (print('0') && return) if($only_zeros);\n\n (print('-1') && return) if($n < 2 || $m < 2);\n (print('-1') && return) if(!possible(\\@ma, $n, $m));\n\n bar($n,$m,\\@ma,\\@mb);\n\n # print STDERR Dumper \\@ma;\n # print STDERR Dumper \\@mb;\n # print \"only_zeros $only_zeros\\n\";\n\n\n}\n\nsub possible {\n my ($m, $r, $c)= @_;\n\n my $possible= 1;\n\n OUT: for (my $i = 0; $i < $r; $i++) {\n for (my $j=0; $j < $c; $j++) {\n # print \"i: $i, j:$j\\n\";\n if(\n ( # case 1 is true\n ($m->[$i][$j] == 1) &&\n (($i+1 < $r) && ($m->[$i+1][$j] == 1)) &&\n (($j+1 < $c) && ($m->[$i][$j+1] == 1)) &&\n (($i+1 < $r) && ($j+1 < $c) && ($m->[$i+1][$j+1] == 1))\n ) || ( # case 2 is true\n ($m->[$i][$j] == 1) &&\n (($i+1 < $r) && ($m->[$i+1][$j] == 1)) &&\n (($j-1 >= 0) && ($m->[$i][$j-1] == 1)) &&\n (($i+1 < $r) && ($j-1 >= 0) && ($m->[$i+1][$j-1] == 1))\n ) || ( # case 3 is true\n ($m->[$i][$j] == 1) &&\n (($i-1 >= 0) && ($m->[$i-1][$j] == 1)) &&\n (($j+1 < $c) && ($m->[$i][$j+1] == 1)) &&\n (($i-1 >= 0) && ($j+1 < $c) && ($m->[$i-1][$j+1] == 1))\n ) || ( # case 4\n ($m->[$i][$j] == 1) &&\n (($i-1 >= 0) && ($m->[$i-1][$j] == 1)) &&\n (($j-1 >= 0) && ($m->[$i][$j-1] == 1)) &&\n (($i-1 >= 0) && ($j-1 >= 0) && ($m->[$i-1][$j-1] == 1))\n ) || ($m->[$i][$j] == 0)\n ) {\n # all good\n } else {\n # print \"Not Possible\\n\";\n $possible= 0;\n last OUT;\n }\n }\n }\n\n return $possible;\n}\n\nsub bar {\n my ($r, $c, $ma, $mb)= @_;\n\n my @changes;\n\n # print STDERR Dumper $ma;\n\n for (my $i = 0; $i < $r; $i++) {\n for (my $j = 0; $j < $c; $j++) {\n # print \"i: $i, j: $j r: $r, c: $c \";\n # print \"maij= \" . $ma->[$i][$j];\n # print $/;\n if(\n ($ma->[$i][$j] == 1) &&\n (\n (\n (($j<($c-1)) && ($ma->[$i][$j+1] == 1))\n ) &&\n (\n ($j<($c-1) && $i<($r-1)) && ($ma->[$i+1][$j+1] == 1)\n ) &&\n (\n (($i<($c-1)) && ($ma->[$i+1][$j] == 1))\n )\n\n # ( # either first col or col before is 0\n # ($j==0) ||\n # (($j>0) && ($ma->[$i][$j-1] == 0))\n # ) &&\n # ( # either first col and row, or before is 0\n # (\n # ($j==0 && $i==0)\n # ) ||\n # (($i>0 && $j>0) && ($ma->[$i-1][$j-1] == 0))\n # ) &&\n # ( # either first row or row before is 0\n # ($i==0) ||\n # (($i>0) && ($ma->[$i-1][$j] == 0))\n # )\n )\n ) {\n push(@changes, [ $i+1, $j+1 ]);\n }\n }\n }\n # $m->[$x][$y]= 1;\n # $m->[$x+1][$y]= 1;\n # $m->[$x][$y+1]= 1;\n # $m->[$x+1][$y+1]= 1;\n\n # print STDERR Dumper \\@changes;\n\n (print(-1) && return) if(scalar(@changes)>2500);\n\n print scalar(@changes);\n print $/;\n for my $a (@changes) {\n print join(' ', @{$a});\n print $/;\n }\n}\n\nmain() unless caller();\n\n"}, {"source_code": "\nuse strict;\nuse warnings;\n\nuse Data::Dumper;\n\nsub main {\n\n my $in= *STDIN;\n my $q= <$in>; chomp($q);\n\n my ($n, $m)= split(' ', $q);\n\n my @ma;\n my @mb;\n\n my $only_zeros= 1;\n for (my $i = 0; $i < $n; $i++) {\n $q= <$in>; chomp($q);\n push(@ma, [ split(' ', $q) ]);\n\n my @empty_row;\n for (my $j = 0; $j < $m; $j++) {\n $only_zeros=0 if($ma[$i][$j] > 0);\n push(@empty_row, 0);\n }\n\n push(@mb, \\@empty_row);\n }\n\n (print('0') && return) if($only_zeros);\n\n (print('-1') && return) if($n < 2 || $m < 2);\n (print('-1') && return) if(!possible(\\@ma, $n, $m));\n\n bar($n,$m,\\@ma,\\@mb);\n\n # print STDERR Dumper \\@ma;\n # print STDERR Dumper \\@mb;\n # print \"only_zeros $only_zeros\\n\";\n\n\n}\n\nsub possible {\n my ($m, $r, $c)= @_;\n\n my $possible= 1;\n\n OUT: for (my $i = 0; $i < $r; $i++) {\n for (my $j=0; $j < $c; $j++) {\n # print \"i: $i, j:$j\\n\";\n if(\n ( # case 1 is true\n ($m->[$i][$j] == 1) &&\n (($i+1 < $r) && ($m->[$i+1][$j] == 1)) &&\n (($j+1 < $c) && ($m->[$i][$j+1] == 1)) &&\n (($i+1 < $r) && ($j+1 < $c) && ($m->[$i+1][$j+1] == 1))\n ) || ( # case 2 is true\n ($m->[$i][$j] == 1) &&\n (($i+1 < $r) && ($m->[$i+1][$j] == 1)) &&\n (($j-1 >= 0) && ($m->[$i][$j-1] == 1)) &&\n (($i+1 < $r) && ($j-1 >= 0) && ($m->[$i+1][$j-1] == 1))\n ) || ( # case 3 is true\n ($m->[$i][$j] == 1) &&\n (($i-1 >= 0) && ($m->[$i-1][$j] == 1)) &&\n (($j+1 < $c) && ($m->[$i][$j+1] == 1)) &&\n (($i-1 >= 0) && ($j+1 < $c) && ($m->[$i-1][$j+1] == 1))\n ) || ( # case 4\n ($m->[$i][$j] == 1) &&\n (($i-1 >= 0) && ($m->[$i-1][$j] == 1)) &&\n (($j-1 >= 0) && ($m->[$i][$j-1] == 1)) &&\n (($i-1 >= 0) && ($j-1 >= 0) && ($m->[$i-1][$j-1] == 1))\n ) || ($m->[$i][$j] == 0)\n ) {\n # all good\n } else {\n # print \"Not Possible\\n\";\n $possible= 0;\n last OUT;\n }\n }\n }\n\n return $possible;\n}\n\nsub bar {\n my ($r, $c, $ma, $mb)= @_;\n\n my @changes;\n\n # print STDERR Dumper $ma;\n\n for (my $i = 0; $i < $r; $i++) {\n for (my $j = 0; $j < $c; $j++) {\n # print \"i: $i, j: $j r: $r, c: $c \";\n # print \"maij= \" . $ma->[$i][$j];\n # print $/;\n if(\n ($ma->[$i][$j] == 1) &&\n (\n (\n (($j<($c-1)) && ($ma->[$i][$j+1] == 1))\n ) &&\n (\n ($j<($c-1) && $i<($r-1)) && ($ma->[$i+1][$j+1] == 1)\n ) &&\n (\n (($i<($c-1)) && ($ma->[$i+1][$j] == 1))\n )\n\n # ( # either first col or col before is 0\n # ($j==0) ||\n # (($j>0) && ($ma->[$i][$j-1] == 0))\n # ) &&\n # ( # either first col and row, or before is 0\n # (\n # ($j==0 && $i==0)\n # ) ||\n # (($i>0 && $j>0) && ($ma->[$i-1][$j-1] == 0))\n # ) &&\n # ( # either first row or row before is 0\n # ($i==0) ||\n # (($i>0) && ($ma->[$i-1][$j] == 0))\n # )\n )\n ) {\n push(@changes, [ $i+1, $j+1 ]);\n }\n }\n }\n # $m->[$x][$y]= 1;\n # $m->[$x+1][$y]= 1;\n # $m->[$x][$y+1]= 1;\n # $m->[$x+1][$y+1]= 1;\n\n # print STDERR Dumper \\@changes;\n\n print scalar(@changes);\n print $/;\n for my $a (@changes) {\n print join(' ', @{$a});\n print $/;\n }\n}\n\nmain() unless caller();\n\n"}, {"source_code": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $line = );\n\nmy ($n, $m) = split q{ }, $line;\n\nmy $a;\nfor my $y ( 1..$n ) {\n chomp ($line = );\n my @x = (1, split (q{ }, $line), 1);\n $a->[$y] = \\@x;\n}\n$a->[$n+1] = [(1)x($m+2)];\n\nmy $b;\n\nfor my $y ( 1..$n ) {\n my @x = ( (0)x($m+1), 1 );\n $b->[$y] = \\@x;\n}\n$b->[$n+1] = [(1)x($m+2)];\n\nmy $same_flag = TRUE;\n\nCHECKSAME:\nfor my $y (1..$n-1) {\n for my $x (1..$m-1) {\n if ( $b->[$y][$x] != $a->[$y][$x] ) {\n $same_flag = FALSE;\n last CHECKSAME;\n }\n }\n}\n\nif ( $same_flag == TRUE ) {\n say 0;\n exit(0);\n}\n\n\nmy $answer = [];\n\nfor my $y ( 1..$n ) {\n for my $x ( 1..$m ) {\n if ( converter_check($a, $y, $x) \n && !same($a, $b, $y, $x) ) {\n convert($b, $y, $x);\n push @$answer, [$y, $x];\n }\n }\n}\n\nfor my $y (1..$n) {\n for my $x (1..$m) {\n if ( $b->[$y][$x] != $a->[$y][$x] ) {\n say -1;\n exit(0);\n }\n }\n}\n\nsay scalar @$answer;\nfor ( @$answer ) {\n say \"$_->[0] $_->[1]\";\n}\n\nsub converter_check {\n my ($a, $y, $x) = @_;\n return FALSE \n if $a->[$y][$x] == 0 \n or $a->[$y][$x+1] == 0\n or $a->[$y+1][$x] == 0\n or $a->[$y+1][$x+1] == 0;\n return TRUE;\n}\n\nsub convert {\n my ($b, $y, $x) = @_;\n $b->[$y][$x] = 1;\n $b->[$y][$x+1] = 1;\n $b->[$y+1][$x] = 1;\n $b->[$y+1][$x+1] = 1;\n}\n\nsub same {\n my ($a, $b, $y, $x) = @_;\n return TRUE \n if $a->[$y][$x] == $b->[$y][$x] \n and $a->[$y][$x+1] == $b->[$y][$x+1]\n and $a->[$y+1][$x] == $b->[$y+1][$x]\n and $a->[$y+1][$x+1] == $b->[$y+1][$x+1];\n return FALSE;\n}\n"}, {"source_code": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $line = );\n\nmy ($n, $m) = split q{ }, $line;\n\nmy $a;\nfor my $y ( 1..$n ) {\n chomp ($line = );\n my @x = (1, split (q{ }, $line), 1);\n $a->[$y] = \\@x;\n}\n$a->[$n+1] = [(1)x($m+2)];\n\nmy $b;\n\nfor my $y ( 1..$n ) {\n my @x = ( (0)x($m+1), 1 );\n $b->[$y] = \\@x;\n}\n$b->[$n+1] = [(1)x($m+2)];\n\nmy $same_flag = TRUE;\n\nCHECKSAME:\nfor my $y (1..$n) {\n for my $x (1..$m) {\n if ( $b->[$y][$x] != $a->[$y][$x] ) {\n $same_flag = FALSE;\n last CHECKSAME;\n }\n }\n}\n\nif ( $same_flag == TRUE ) {\n say 0;\n exit(0);\n}\n\n\nmy $answer = [];\n\nfor my $y ( 1..$n ) {\n for my $x ( 1..$m ) {\n if ( converter_check($a, $y, $x) \n && !same($a, $b, $y, $x) ) {\n convert($b, $y, $x);\n push @$answer, [$y, $x];\n }\n }\n}\n\nfor my $y (1..$n) {\n for my $x (1..$m) {\n if ( $b->[$y][$x] != $a->[$y][$x] ) {\n say -1;\n exit(0);\n }\n }\n}\n\nsay scalar @$answer;\nfor ( @$answer ) {\n say \"$_->[0] $_->[1]\";\n}\n\nsub converter_check {\n my ($a, $y, $x) = @_;\n return FALSE \n if $a->[$y][$x] == 0 \n or $a->[$y][$x+1] == 0\n or $a->[$y+1][$x] == 0\n or $a->[$y+1][$x+1] == 0;\n return TRUE;\n}\n\nsub convert {\n my ($b, $y, $x) = @_;\n $b->[$y][$x] = 1;\n $b->[$y][$x+1] = 1;\n $b->[$y+1][$x] = 1;\n $b->[$y+1][$x+1] = 1;\n}\n\nsub same {\n my ($a, $b, $y, $x) = @_;\n return TRUE \n if $a->[$y][$x] == $b->[$y][$x] \n and $a->[$y][$x+1] == $b->[$y][$x+1]\n and $a->[$y+1][$x] == $b->[$y+1][$x]\n and $a->[$y+1][$x+1] == $b->[$y+1][$x+1];\n return FALSE;\n}\n"}, {"source_code": "#!/usr/bin/env perl \nuse strict;\nuse warnings;\nuse feature qw/ say /;\nuse constant TRUE => 1;\nuse constant FALSE => 0;\n\nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \n\n# start\n\nchomp (my $line = );\n\nmy ($n, $m) = split q{ }, $line;\n\nmy $a;\nfor my $y ( 1..$n ) {\n chomp ($line = );\n my @x = (1, split (q{ }, $line), 1);\n $a->[$y] = \\@x;\n}\n$a->[$n+1] = [(1)x($m+2)];\n\nmy $b;\n\nfor my $y ( 1..$n ) {\n my @x = ( (0)x($m+1), 1 );\n $b->[$y] = \\@x;\n}\n$b->[$n+1] = [(1)x($m+2)];\n\nmy $same_flag = TRUE;\n\nCHECKSAME:\nfor my $y (1..$n) {\n for my $x (1..$m) {\n if ( $b->[$y][$x] != $a->[$y][$x] ) {\n $same_flag = FALSE;\n last CHECKSAME;\n }\n }\n}\n\nif ( $same_flag == TRUE ) {\n say 0;\n exit(0);\n}\n\n\nmy $answer = [];\n\nfor my $y ( 1..$n ) {\n for my $x ( 1..$m ) {\n if ( converter_check($a, $y, $x) \n && !same($a, $b, $y, $x) ) {\n convert($b, $y, $x);\n push @$answer, [$y, $x];\n }\n }\n}\n\nfor my $y (1..$n) {\n for my $x (1..$m) {\n if ( $b->[$y][$x] != $a->[$y][$x] ) {\n say -1;\n exit(0);\n }\n }\n}\n\nfor ( @$answer ) {\n say \"$_->[0] $_->[1]\";\n}\n\nsub converter_check {\n my ($a, $y, $x) = @_;\n return FALSE \n if $a->[$y][$x] == 0 \n or $a->[$y][$x+1] == 0\n or $a->[$y+1][$x] == 0\n or $a->[$y+1][$x+1] == 0;\n return TRUE;\n}\n\nsub convert {\n my ($b, $y, $x) = @_;\n $b->[$y][$x] = 1;\n $b->[$y][$x+1] = 1;\n $b->[$y+1][$x] = 1;\n $b->[$y+1][$x+1] = 1;\n}\n\nsub same {\n my ($a, $b, $y, $x) = @_;\n return TRUE \n if $a->[$y][$x] == $b->[$y][$x] \n and $a->[$y][$x+1] == $b->[$y][$x+1]\n and $a->[$y+1][$x] == $b->[$y+1][$x]\n and $a->[$y+1][$x+1] == $b->[$y+1][$x+1];\n return FALSE;\n}\n"}], "src_uid": "ad641a44ecaf78ca253b199f3d40ef96"} {"nl": {"description": "A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time.You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print \"YES\", otherwise print \"NO\" (without the quotes).", "input_spec": "The first and only line consists of a string $$$S$$$ ($$$ 1 \\le |S| \\le 5\\,000 $$$). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'.", "output_spec": "Print \"YES\" or \"NO\", according to the condition.", "sample_inputs": ["aaabccc", "bbacc", "aabc"], "sample_outputs": ["YES", "NO", "YES"], "notes": "NoteConsider first example: the number of 'c' is equal to the number of 'a'. Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct.Consider third example: the number of 'c' is equal to the number of 'b'."}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nno warnings 'uninitialized';\n\nsub check($) {\n my $S = shift;\n if ($S !~ m/^(a+)(b+)(c+)$/) {\n return 0;\n }\n my $a = $1;\n my $b = $2;\n my $c = $3;\n return length($c) == length($a) || length($c) == length($b);\n}\n\nmy $S = <>;\nchomp $S;\n\nprint check($S) ? 'YES' : 'NO', \"\\n\";\n\n\n"}], "negative_code": [], "src_uid": "6581dbaff7eb52b63ccfe9c0c4117c09"} {"nl": {"description": "Vasya is developing his own programming language VPL (Vasya Programming Language). Right now he is busy making the system of exceptions. He thinks that the system of exceptions must function like that.The exceptions are processed by try-catch-blocks. There are two operators that work with the blocks: The try operator. It opens a new try-catch-block. The catch(<exception_type>, <message>) operator. It closes the try-catch-block that was started last and haven't yet been closed. This block can be activated only via exception of type <exception_type>. When we activate this block, the screen displays the <message>. If at the given moment there is no open try-catch-block, then we can't use the catch operator.The exceptions can occur in the program in only one case: when we use the throw operator. The throw(<exception_type>) operator creates the exception of the given type.Let's suggest that as a result of using some throw operator the program created an exception of type a. In this case a try-catch-block is activated, such that this block's try operator was described in the program earlier than the used throw operator. Also, this block's catch operator was given an exception type a as a parameter and this block's catch operator is described later that the used throw operator. If there are several such try-catch-blocks, then the system activates the block whose catch operator occurs earlier than others. If no try-catch-block was activated, then the screen displays message \"Unhandled Exception\".To test the system, Vasya wrote a program that contains only try, catch and throw operators, one line contains no more than one operator, the whole program contains exactly one throw operator.Your task is: given a program in VPL, determine, what message will be displayed on the screen.", "input_spec": "The first line contains a single integer: n (1\u2009\u2264\u2009n\u2009\u2264\u2009105) the number of lines in the program. Next n lines contain the program in language VPL. Each line contains no more than one operator. It means that input file can contain empty lines and lines, consisting only of spaces. The program contains only operators try, catch and throw. It is guaranteed that the program is correct. It means that each started try-catch-block was closed, the catch operators aren't used unless there is an open try-catch-block. The program has exactly one throw operator. The program may have spaces at the beginning of a line, at the end of a line, before and after a bracket, a comma or a quote mark. The exception type is a nonempty string, that consists only of upper and lower case english letters. The length of the string does not exceed 20 symbols. Message is a nonempty string, that consists only of upper and lower case english letters, digits and spaces. Message is surrounded with quote marks. Quote marks shouldn't be printed. The length of the string does not exceed 20 symbols. Length of any line in the input file does not exceed 50 symbols. ", "output_spec": "Print the message the screen will show after the given program is executed.", "sample_inputs": ["8\ntry\n try\n throw ( AE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")", "8\ntry\n try\n throw ( AE ) \n catch ( AE, \"AE in line 3\")\n\n try\n catch(BE, \"BE in line 5\") \ncatch(AE,\"AE somewhere\")", "8\ntry\n try\n throw ( CE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")"], "sample_outputs": ["AE somewhere", "AE in line 3", "Unhandled Exception"], "notes": "NoteIn the first sample there are 2 try-catch-blocks such that try operator is described earlier than throw operator and catch operator is described later than throw operator: try-catch(BE,\"BE in line 3\") and try-catch(AE,\"AE somewhere\"). Exception type is AE, so the second block will be activated, because operator catch(AE,\"AE somewhere\") has exception type AE as parameter and operator catch(BE,\"BE in line 3\") has exception type BE.In the second sample there are 2 try-catch-blocks such that try operator is described earlier than throw operator and catch operator is described later than throw operator: try-catch(AE,\"AE in line 3\") and try-catch(AE,\"AE somewhere\"). Exception type is AE, so both blocks can be activated, but only the first one will be activated, because operator catch(AE,\"AE in line 3\") is described earlier than catch(AE,\"AE somewhere\")In the third sample there is no blocks that can be activated by an exception of type CE."}, "positive_code": [{"source_code": "\n#!/usr/bin/perl\n\nuse 5.006;\nuse strict;\nuse warnings;\n\nsub getMessage {\n my $t = shift;\n my $s = $t;\n $s =~ s/^[\\w (,]+\\\"//g;\n $s =~ s/\\\"[\\w) ]+$//g; \n $s =~ s/^[\\s]+//g;\n $s =~ s/[\\s]+$//g;\n return $s;\n}\n\nsub getException {\n my $t = shift;\n my $s = $t;\n $s =~ s/^[\\w ]+\\(//g;\n $s =~ s/,[\\w )\\\"]+$//g; \n $s =~ s/^[\\s]+//g;\n $s =~ s/[\\s]+$//g;\n return $s;\n}\n\nsub startsWith {\n my $t = shift;\n my $tok = shift;\n my $s = $t;\n $s =~ s/^[\\s]+//g;\n return 1 if ($s =~ /^$tok/);\n return 0;\n}\n\nsub getThrowExc {\n my $t = shift;\n my $s = $t;\n $s =~ s/^[\\w ]+\\(//g;\n $s =~ s/[)]//g;\n $s =~ s/^[\\s]+//g;\n $s =~ s/[\\s]+$//g;\n return $s;\n}\n\nsub blank {\n my $t = shift;\n my $s = $t;\n $s =~ s/[\\s]+//g;\n return length($s) == 0; \n}\n\nmy $n = <>;\nmy $line;\nmy $level = 0;\nmy $throwLevel = -1;\nmy $left = 0;\nmy $haveAnswer = 0;\nmy $exc;\nwhile ($line = <>) {\n next if (blank($line));\n if ($throwLevel == -1) {\n if (startsWith($line, \"try\")) {\n $level++;\n next;\n }\n if (startsWith($line, \"catch\")) {\n $level--;\n next;\n }\n $exc = getThrowExc($line); \n $throwLevel = $level;\n } else {\n if (startsWith($line, \"try\")) {\n $left++;\n next; \n } \n my $e = getException($line);\n if ($left == 0 && $exc eq $e) {\n $haveAnswer = 1;\n print getMessage($line); \n last;\n }\n if ($left > 0) {\n $left--;\n }\n \n\n }\n}\n\nif ($haveAnswer != 1) {\n print \"Unhandled Exception\";\n}"}, {"source_code": "#!perl\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my $n = int <>;\n my ($tries, $ltries, $type, $message) = (0, 0, \"\", 'Unhandled Exception');\ntc: for (1..$n) {\n my $str = <>;\n chomp($str);\n $str =~ s/[(),]/ /g;\n $str =~ s/^[ \\t]*//g;\n $str =~ s/ +/ /g;\n my ($cmd, $ttype, $cmessage) = split / /, $str, 3;\n if (! $cmd) { next tc; }\n if ($cmd eq 'try') {\n if ($type) {\n $ltries++;\n } else {\n $tries++;\n }\n } elsif ($cmd eq 'catch') {\n if ($ltries) {\n $ltries--;\n } else {\n if ($type) {\n if ($type eq $ttype) {\n $message = $cmessage;\n $message =~ s/ +$//g;\n $message =~ s/\"//g;\n last tc;\n }\n } else {\n $tries--;\n }\n }\n } elsif ($cmd eq 'throw') {\n $type = $ttype;\n }\n }\n print $message;\n}\n\n__END__\n\n8\ntry\n try\n throw ( AE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")\n"}, {"source_code": " my $n = ;\n\n my $found = 0;\n\n my $level = 0;\n\n my $throwLevel = -1; \n\n my $e = \"\"; \n\n foreach (1 .. $n) {\n\n my $line = ;\n\n if ($line =~ /^\\s*try/) {\n\n $level ++; \n\n } elsif ($line =~ /^\\s*throw\\s*\\(\\s*(\\w+)\\s*\\)/) {\n\n $e = $1; \n\n $throwLevel = $level;\n\n # print \"throw $e at level $throwLevel\\n\";\n\n } elsif ($line =~ /^\\s*catch\\s*\\(\\s*(\\w+)\\s*,\\s*\"(.+)\"\\s*\\)/) {\n\n my $ce = $1; \n\n my $msg = $2; \n\n if ($ce eq $e && $level <= $throwLevel) {\n\n $found = 1;\n\n print \"$msg\\n\";\n\n last;\n\n } \n\n $level --; \n\n if ($throwLevel > $level) {\n\n $throwLevel = $level;\n\n # print \"decrease throwLevel to $throwLevel\\n\";\n\n } \n\n } \n\n }\n\n if (!$found) {\n\n print \"Unhandled Exception\";\n\n }\n\n"}, {"source_code": "#!/usr/local/bin/perl\n$n = <>;\n$openBlocks = 0;\n$needlessBlocks =\n 0; #\u044d\u0442\u043e \u0431\u043b\u043e\u043a\u0438, \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0435 \u043f\u043e\u0441\u043b\u0435 \u044d\u043a\u0441\u0435\u043f\u0448\u0435\u043d\u0430 TODO\n$wasThrow = 0;\n$eType;\nfor ( 1 .. $n ) {\n my $line = <>;\n\n if ( $line =~ /\\s*throw\\s*\\(\\s*(\\w+)\\s*\\)/ ) {\n $wasThrow = 1;\n $eType = $1;\n maybeError();\n next;\n }\n if ( $line =~ /\\s*catch\\s*\\(\\s*(\\w+)\\s*,\\s*\"(.+)\"\\s*\\)/ ) {\n if ( $needlessBlocks > 0 ) {\n $needlessBlocks--;\n next;\n }\n $openBlocks--;\n if ( $wasThrow != 0 && $eType eq $1 ) {\n print $2. \"\\n\";\n exit;\n }\n if ( $wasThrow != 0 ) {\n maybeError();\n }\n next;\n }\n if ( $line =~ /\\s*try\\s*/ ) {\n if ( $wasThrow == 0 ) {\n $openBlocks++;\n }\n else {\n $needlessBlocks++;\n }\n next;\n }\n}\nprint \"Unhandled Exception\\n\";\n\nsub maybeError {\n if ( $openBlocks == 0 ) {\n print \"Unhandled Exception\\n\";\n exit;\n }\n}\n"}], "negative_code": [{"source_code": "#!perl\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my $n = int <>;\n my ($tries, $ltries, $type, $message) = (0, 0, \"\", 'Unhandled Exception');\ntc: for (1..$n) {\n my $str = <>;\n chomp($str);\n $str =~ s/[(),]/ /g;\n $str =~ s/^[ \\t]*//g;\n $str =~ s/ +/ /g;\n my ($cmd, $ttype, $cmessage) = split / /, $str, 3;\n if (! $cmd) { next tc; }\n if ($cmd eq 'try') {\n if ($type) {\n $ltries++;\n } else {\n $tries++;\n }\n } elsif ($cmd eq 'catch') {\n if ($ltries) {\n $ltries--;\n } else {\n if ($type) {\n if ($type eq $ttype) {\n $message = $cmessage;\n last tc;\n }\n } else {\n $tries--;\n }\n }\n } elsif ($cmd eq 'throw') {\n $type = $ttype;\n }\n }\n print $message;\n}\n\n__END__\n\n"}, {"source_code": "#!perl\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my $n = int <>;\n my ($tries, $ltries, $type, $message) = (0, 0, \"\", 'Unhandled Exception');\ntc: for (1..$n) {\n my $str = <>;\n chomp($str);\n $str =~ s/[(),]/ /g;\n $str =~ s/^[ \\t]*//g;\n $str =~ s/ +/ /g;\n my ($cmd, $ttype, $cmessage) = split / /, $str, 3;\n if (! $cmd) { next tc; }\n if ($cmd eq 'try') {\n if ($type) {\n $ltries++;\n } else {\n $tries++;\n }\n } elsif ($cmd eq 'catch') {\n if ($ltries) {\n $ltries--;\n } else {\n if ($type) {\n if ($type eq $ttype) {\n $message = $cmessage;\n $message =~ s/\"//g;\n last tc;\n }\n } else {\n $tries--;\n }\n }\n } elsif ($cmd eq 'throw') {\n $type = $ttype;\n }\n }\n print $message;\n}\n\n__END__\n\n"}, {"source_code": "#!/usr/local/bin/perl\n$n = <>;\n$openBlocks = 0;\n$needlessBlocks =\n 0; #\u044d\u0442\u043e \u0431\u043b\u043e\u043a\u0438, \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0435 \u043f\u043e\u0441\u043b\u0435 \u044d\u043a\u0441\u0435\u043f\u0448\u0435\u043d\u0430 TODO\n$wasThrow = 0;\n$eType;\nfor ( 1 .. $n ) {\n my $line = <>;\n\n if ( $line =~ /\\s*try\\s*/ ) {\n if ( $wasThrow == 0 ) {\n $openBlocks++;\n }\n else {\n $needlessBlocks++;\n }\n next;\n }\n if ( $line =~ /\\s*throw\\s*\\(\\s*(\\w+)\\s*\\)/ ) {\n $wasThrow = 1;\n $eType = $1;\n maybeError();\n next;\n }\n if ( $line =~ /\\s*catch\\s*\\(\\s*(\\w+)\\s*,\\s*\"(.+)\"\\s*\\)/ ) {\n if ( $needlessBlocks > 0 ) {\n $needlessBlocks--;\n next;\n }\n $openBlocks--;\n if ( $wasThrow != 0 && $eType eq $1 ) {\n print $2. \"\\n\";\n exit;\n }\n if ($wasThrow != 0){ \n maybeError();\n } \n\n }\n}\nprint \"Unhandled Exception\\n\";\n\nsub maybeError {\n if ( $openBlocks == 0 ) {\n print \"Unhandled Exception\\n\";\n exit;\n }\n}\n"}, {"source_code": "#!/usr/local/bin/perl\n$n = <>;\n$openBlocks = 0;\n$needlessBlocks =\n 0; #\u044d\u0442\u043e \u0431\u043b\u043e\u043a\u0438, \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0435 \u043f\u043e\u0441\u043b\u0435 \u044d\u043a\u0441\u0435\u043f\u0448\u0435\u043d\u0430 TODO\n$wasThrow = 0;\n$eType;\nfor ( 1 .. $n ) {\n my $line = <>;\n\n\n if ( $line =~ /\\s*try\\s*/ ) {\n if ( $wasThrow == 0 ) {\n $openBlocks++;\n }\n else {\n $needlessBlocks++;\n }\n next;\n }\n if ( $line =~ /\\s*throw\\s*\\(\\s*(\\w+)\\s*\\)/ ) {\n $wasThrow = 1;\n $eType = $1;\n maybeError();\n next;\n }\n if ( $line =~ /\\s*catch\\s*\\(\\s*(\\w+)\\s*,\\s*\"(.+)\"\\s*\\)/ ) { \n if ( $needlessBlocks > 0 ) {\n $needlessBlocks--;\n next;\n }\n $openBlocks--;\n if ( $wasThrow != 0 && $eType eq $1 ) {\n print $2. \"\\n\";\n exit;\n }\n $wasThrow != 0 && maybeError();\n next;\n\n }\n}\n\nsub maybeError {\n if ( $openBlocks == 0 ) {\n print \"Unhandled Exception\\n\";\n exit;\n }\n}\n"}], "src_uid": "320e6c06194f8b1ccf1f4218d0757d2f"} {"nl": {"description": "Eleven wants to choose a new name for herself. As a bunch of geeks, her friends suggested an algorithm to choose a name for her. Eleven wants her name to have exactly n characters. Her friend suggested that her name should only consist of uppercase and lowercase letters 'O'. More precisely, they suggested that the i-th letter of her name should be 'O' (uppercase) if i is a member of Fibonacci sequence, and 'o' (lowercase) otherwise. The letters in the name are numbered from 1 to n. Fibonacci sequence is the sequence f where f1\u2009=\u20091, f2\u2009=\u20091, fn\u2009=\u2009fn\u2009-\u20092\u2009+\u2009fn\u2009-\u20091 (n\u2009>\u20092). As her friends are too young to know what Fibonacci sequence is, they asked you to help Eleven determine her new name.", "input_spec": "The first and only line of input contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000).", "output_spec": "Print Eleven's new name on the first and only line of output.", "sample_inputs": ["8", "15"], "sample_outputs": ["OOOoOooO", "OOOoOooOooooOoo"], "notes": null}, "positive_code": [{"source_code": "$_ = <>, chomp;\n\t\n$o = 'O';\n\n$o =~ s/o*O?o*O$/ $& . reverse ucfirst lc $& /e for 1 .. 14;\n\nprint 'O' . substr $o, 0, $_ - 1"}, {"source_code": "$_ = <>, chomp;\n\n$o = 'O';\n\n$o =~ s/(o*O?o*)O$/$&\\L$1\\UO/ for 1 .. 14;\n\n\"O$o\" =~ /.{$_}/; \n\nprint $&"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy $n = $_;\n\t$_ = 'o' x 1e3;\n\t\n\ts/o/O/;\n\t\n\tmy $i = 1;\n\tmy $j = 1;\n\t\n\twhile( $j < 1 + length ){\n\t\tsubstr $_, $j - 1, 1, 'O';\n\t\tmy $tmp = $j;\n\t\t$j += $i;\n\t\t$i = $tmp;\n\t\t}\n\t\n\tprint substr $_, 0, $n;\n\t}"}], "negative_code": [], "src_uid": "a7c0484275e62f0bc70d9edaac27d7d6"} {"nl": {"description": "Santa has $$$n$$$ candies and he wants to gift them to $$$k$$$ kids. He wants to divide as many candies as possible between all $$$k$$$ kids. Santa can't divide one candy into parts but he is allowed to not use some candies at all.Suppose the kid who recieves the minimum number of candies has $$$a$$$ candies and the kid who recieves the maximum number of candies has $$$b$$$ candies. Then Santa will be satisfied, if the both conditions are met at the same time: $$$b - a \\le 1$$$ (it means $$$b = a$$$ or $$$b = a + 1$$$); the number of kids who has $$$a+1$$$ candies (note that $$$a+1$$$ not necessarily equals $$$b$$$) does not exceed $$$\\lfloor\\frac{k}{2}\\rfloor$$$ (less than or equal to $$$\\lfloor\\frac{k}{2}\\rfloor$$$). $$$\\lfloor\\frac{k}{2}\\rfloor$$$ is $$$k$$$ divided by $$$2$$$ and rounded down to the nearest integer. For example, if $$$k=5$$$ then $$$\\lfloor\\frac{k}{2}\\rfloor=\\lfloor\\frac{5}{2}\\rfloor=2$$$.Your task is to find the maximum number of candies Santa can give to kids so that he will be satisfied.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 5 \\cdot 10^4$$$) \u2014 the number of test cases. The next $$$t$$$ lines describe test cases. The $$$i$$$-th test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n, k \\le 10^9$$$) \u2014 the number of candies and the number of kids.", "output_spec": "For each test case print the answer on it \u2014 the maximum number of candies Santa can give to kids so that he will be satisfied.", "sample_inputs": ["5\n5 2\n19 4\n12 7\n6 2\n100000 50010"], "sample_outputs": ["5\n18\n10\n6\n75015"], "notes": "NoteIn the first test case, Santa can give $$$3$$$ and $$$2$$$ candies to kids. There $$$a=2, b=3,a+1=3$$$.In the second test case, Santa can give $$$5, 5, 4$$$ and $$$4$$$ candies. There $$$a=4,b=5,a+1=5$$$. The answer cannot be greater because then the number of kids with $$$5$$$ candies will be $$$3$$$.In the third test case, Santa can distribute candies in the following way: $$$[1, 2, 2, 1, 1, 2, 1]$$$. There $$$a=1,b=2,a+1=2$$$. He cannot distribute two remaining candies in a way to be satisfied.In the fourth test case, Santa can distribute candies in the following way: $$$[3, 3]$$$. There $$$a=3, b=3, a+1=4$$$. Santa distributed all $$$6$$$ candies."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\tmy $div = int $n / $k;\n\t\n\tmy $ans = $div * $k;\n\t\n\tmy $rest = $n - $ans;\n\t\n\tmy $min = ( sort { $a <=> $b } $rest, int $k / 2 )[ 0 ];\n\t\n\tprint $ans + $min;\n\t}"}], "negative_code": [], "src_uid": "43b8e9fb2bd0ec5e0250a33594417f63"} {"nl": {"description": "Little Vasya loves orange juice very much. That's why any food and drink in his kitchen necessarily contains orange juice. There are n drinks in his fridge, the volume fraction of orange juice in the i-th drink equals pi percent.One day Vasya decided to make himself an orange cocktail. He took equal proportions of each of the n drinks and mixed them. Then he wondered, how much orange juice the cocktail has.Find the volume fraction of orange juice in the final drink.", "input_spec": "The first input line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of orange-containing drinks in Vasya's fridge. The second line contains n integers pi (0\u2009\u2264\u2009pi\u2009\u2264\u2009100) \u2014 the volume fraction of orange juice in the i-th drink, in percent. The numbers are separated by a space.", "output_spec": "Print the volume fraction in percent of orange juice in Vasya's cocktail. The answer will be considered correct if the absolute or relative error does not exceed 10\u2009\u2009-\u20094.", "sample_inputs": ["3\n50 50 100", "4\n0 25 50 75"], "sample_outputs": ["66.666666666667", "37.500000000000"], "notes": "NoteNote to the first sample: let's assume that Vasya takes x milliliters of each drink from the fridge. Then the volume of pure juice in the cocktail will equal milliliters. The total cocktail's volume equals 3\u00b7x milliliters, so the volume fraction of the juice in the cocktail equals , that is, 66.(6) percent."}, "positive_code": [{"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n\n"}, {"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n\n"}, {"source_code": "my $m = 100 * int<>;\nmy $sum = 0;\n$sum += $_ for(split / /, <>);\nprint 100 * $sum / $m;"}, {"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n"}, {"source_code": "my $n = ;\nmy @a = split(/\\s/, );\nmy $total = 0;\nmap{ $total += $_ } @a;\nprintf(\"%.12f\", $total/$n);"}, {"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n\n"}, {"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n\n"}, {"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n$tot+=$_ foreach (split / /, <>);\n$ans = $tot / $n;\nsay $ans;"}, {"source_code": "<>;\nmy $s = 0;\nmy $c = 0;\nfor my $p (split ' ', <>) {\n $s += $p;\n $c++;\n}\n\nprint $s / $c, \"\\n\";\n"}, {"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n"}, {"source_code": "$n=<>;@a=split' ',<>;$s+=$_ for(@a);print$s/$n;"}, {"source_code": "$n=<>;$s+=$_ for(split' ',<>);print$s/$n;"}, {"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n"}, {"source_code": "$n=<>;$s+=$_ for(split' ',<>);print$s/$n"}, {"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n"}, {"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n\n"}, {"source_code": "#!perl\nuse strict;\n\nmain();\n\nsub main {\n my $n = int <>;\n my @arr = map int, split /\\D/, <>;\n my $res = 0;\n for (@arr) {\n $res += $_;\n }\n $res /= $n;\n print $res;\n}\n\n__END__\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\n# solve\n\nmy $n = read_token;\n\nmy @percents = split q{ }, read_line;\n\nmy @proportions = map { $_ / 100 } @percents;\n\nmy $sums = sum(@proportions);\n\nsay $sums / $n * 100;\n"}, {"source_code": "$n=<>;\n$s+=$_ for split' ',<>; \nprint$s/$n"}, {"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n\n"}, {"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n\n"}, {"source_code": "while (<>){\n @_=split/ /,<>;\n $sum=0;\n for (@_){\n $sum+=$_\n }\n print $sum/@_;\n print \"\\n\"\n }"}, {"source_code": "$n=<>;$s+=$_ for split' ',<>;print$s/$n\n"}], "negative_code": [], "src_uid": "580596d05a2eaa36d630d71ef1055c43"} {"nl": {"description": "All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position xi \u2014 a coordinate on the Ox axis. No two cities are located at a single point.Lineland residents love to send letters to each other. A person may send a letter only if the recipient lives in another city (because if they live in the same city, then it is easier to drop in).Strange but true, the cost of sending the letter is exactly equal to the distance between the sender's city and the recipient's city.For each city calculate two values \u200b\u200bmini and maxi, where mini is the minimum cost of sending a letter from the i-th city to some other city, and maxi is the the maximum cost of sending a letter from the i-th city to some other city", "input_spec": "The first line of the input contains integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of cities in Lineland. The second line contains the sequence of n distinct integers x1,\u2009x2,\u2009...,\u2009xn (\u2009-\u2009109\u2009\u2264\u2009xi\u2009\u2264\u2009109), where xi is the x-coordinate of the i-th city. All the xi's are distinct and follow in ascending order.", "output_spec": "Print n lines, the i-th line must contain two integers mini,\u2009maxi, separated by a space, where mini is the minimum cost of sending a letter from the i-th city, and maxi is the maximum cost of sending a letter from the i-th city.", "sample_inputs": ["4\n-5 -2 2 7", "2\n-1 1"], "sample_outputs": ["3 12\n3 9\n4 7\n5 12", "2 2\n2 2"], "notes": null}, "positive_code": [{"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nmy %min;\nmy %max;\n\n<>;\nmy @cities = split ' ', <>;\n\nfor my $i (1..$#cities) {\n my $d = abs($cities[$i - 1] - $cities[$i]);\n $min{$i - 1} = $d if !exists $min{$i - 1} || $d < $min{$i - 1};\n $min{$i} = $d if !exists $min{$i} || $d < $min{$i};\n}\n\nfor my $i (0..$#cities) {\n my $left = $cities[$i] - $cities[0];\n my $right = $cities[-1] - $cities[$i];\n $max{$i} = $left > $right ? $left\n : $right;\n}\n\nfor my $i (0..$#cities) {\n say \"$min{$i} $max{$i}\"\n}\n"}, {"source_code": "#!perl\n$\\ = \"\\n\";\n\n$n = <>;\n\n@a = split /\\s/, <>;\n\npush @result, (abs @a[0] - @a[1]).' '.(abs @a[0] - @a[$#a]);\n\nfor my $i (1..$#a - 1)\n{\n my $minLeft = abs @a[$i] - @a[$i - 1];\n my $minRight = abs @a[$i] - @a[$i + 1];\n \n my $maxLeft = abs @a[$i] - @a[0];\n my $maxRight = abs @a[$i] - @a[$#a];\n \n my $min = $minLeft < $minRight ? $minLeft : $minRight;\n my $max = $maxLeft > $maxRight ? $maxLeft : $maxRight;\n \n push @result, $min.' '.$max;\n}\n\npush @result, (abs @a[$#a - 1] - @a[$#a]).' '.(abs @a[0] - @a[$#a]);\n\nprint join \"\\n\", @result;"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n#$, = \" \";\n\nsub min{\n return $_[0] < $_[1]? $_[0] : $_[1];\n}\nsub max{\n return $_[0] > $_[1]? $_[0] : $_[1];\n}\n\nmy $n = <>;\nchomp($n);\nmy @arr = split ' ',<>;\nmy @s = sort {$a <=> $b} @arr;\nmy %find;\n@find{@s} = (0..$#s);\nfor my $i(0..@arr-1){\n my $j = $find{$arr[$i]};\n\n my $lm = $j-1>=0? abs($s[$j] - $s[$j-1]) : 2e9;\n my $rm = $j+1<$n? abs($s[$j] - $s[$j+1]) : 2e9;\n\n my $lx = $j==0? 0 : abs($s[$j] - $s[0]);\n my $rx = $j==$n-1? 0 : abs($s[$j] - $s[$n-1]);\n\n my $r1 = min($lm, $rm);\n my $r2 = max($lx, $rx);\n print \"$r1 $r2\\n\";\n}\n"}, {"source_code": "$, = $/;\n$/ = $\\;\n$_ = <>;\n@_ = split;\nshift @_;\nunshift @_, ($min = shift @_) x 2;\npush @_, ($max = pop @_) x 2;\n\nfor $i (1 .. @_ - 2){\n\tpush @e, join ' ',\n\t\t( grep $_, sort {$a <=> $b} map abs $_[$i + $_] - $_[$i], -1, 1 )[ 0 ],\n\t\t( grep $_, sort {$b <=> $a} map abs $_ - $_[$i], $min, $max )[ 0 ];\n\t}\nprint @e"}, {"source_code": "$, = $/;\n<>;\n$_ = <>;\n@_ = (-9e17, split, 9e17);\n\nfor $i (1 .. @_ - 2){\n\tpush @e, join ' ', ( grep !/e|^0/, sort {$a <=> $b} map abs $_[$i] - $_[$_], $i - 1, $i + 1, 1, @_ - 2 )[0, -1]\n\t}\nprint @e"}, {"source_code": "<>;\n$_ = <>;\n@_ = (-~0, split, ~0);\n\nfor (1 .. @_ - 2){\n\t$L = $_[$_] - $_[$_-1];\n\t$R = $_[$_+1] - $_[$_];\n\t$L < $R and $R = $L;\n\t$Q = $_[$_] - $_[1];\n\t$W = $_[@_ - 2] - $_[$_];\n\t$Q > $W and $W = $Q;\n\tpush @e, \"$R $W\\n\"\n\t}\nprint @e"}], "negative_code": [{"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nmy %min;\nmy %max;\n\n<>;\nmy @cities = split ' ', <>;\n\nfor my $i (1..$#cities) {\n my $d = abs($cities[$i - 1] - $cities[$i]);\n $min{$i - 1} = $d if !exists $min{$i - 1} || $d < $min{$i - 1};\n $min{$i} = $d if !exists $min{$i} || $d < $min{$i};\n}\n\nfor my $i (0..$#cities) {\n my $left = $cities[$i] - $cities[0];\n my $right = $cities[-1] - $cities[0];\n $max{$i} = $left > $right ? $left\n : $right;\n}\n\nfor my $i (0..$#cities) {\n say \"$min{$i} $max{$i}\"\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n#$, = \" \";\n\nsub min{\n\treturn $_[0] < $_[1]? $_[0] : $_[1];\n}\nsub max{\n\treturn $_[0] > $_[1]? $_[0] : $_[1];\n}\n\nmy $n = <>;\nchomp($n);\nmy @arr = split ' ',<>;\nmy @s = sort {$a <=> $b} @arr;\nmy %find;\n@find{@s} = (0..$#s);\nfor my $i(0..@arr-1){\n\tmy $j = $find{$arr[$i]};\n\n\tmy $lm = $j-1>=0? abs($s[$j] - $s[$j-1]) : 1<<27;\n\tmy $rm = $j+1<$n? abs($s[$j] - $s[$j+1]) : 1<<27;\n\n\tmy $lx = $j==0? 0 : abs($s[$j] - $s[0]);\n\tmy $rx = $j==$n-1? 0 : abs($s[$j] - $s[$n-1]);\n\n\tmy $r1 = min($lm, $rm);\n\tmy $r2 = max($lx, $rx);\n\tprint \"$r1 $r2\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n#$, = \" \";\n\nsub min{\n\treturn $_[0] < $_[1]? $_[0] : $_[1];\n}\nsub max{\n\treturn $_[0] > $_[1]? $_[0] : $_[1];\n}\n\nmy $n = <>;\nchomp($n);\nmy @arr = split ' ',<>;\nmy @s = sort {$a <=> $b} @arr;\nmy %find;\n@find{@s} = (0..$#s);\nfor my $i(0..@arr-1){\n\tmy $j = $find{$arr[$i]};\n\n\tmy $lm = $j-1>=0? abs($s[$j] - $s[$j-1]) : 1<<60;\n\tmy $rm = $j+1<$n? abs($s[$j] - $s[$j+1]) : 1<<60;\n\n\tmy $lx = $j==0? 0 : abs($s[$j] - $s[0]);\n\tmy $rx = $j==$n-1? 0 : abs($s[$j] - $s[$n-1]);\n\n\tmy $r1 = min($lm, $rm);\n\tmy $r2 = max($lx, $rx);\n\tprint \"$r1 $r2\\n\";\n}\n"}, {"source_code": "$, = $/;\n<>;\n$_ = <>;\n@_ = (-9e9, split, 9e9);\n\nfor $i (1 .. @_ - 2){\n\tpush @e, join ' ', ( grep !/e|^0/, sort {$a <=> $b} map abs $_[$i] - $_[$_], $i - 1, $i + 1, 1, @_ - 2 )[0, -1]\n\t}\nprint @e"}, {"source_code": "$, = $/;\n<>;\n$_ = <>;\n@_ = (-~0, split, ~0);\n\nfor $i (1 .. @_ - 2){\n\tpush @e, join ' ', ( grep !/e|^0/, sort {$a <=> $b} map abs $_[$i] - $_[$_], $i - 1, $i + 1, 1, @_ - 2 )[0, -1]\n\t}\nprint @e"}], "src_uid": "55383f13c8d097408b0ccf5653c4563d"} {"nl": {"description": "Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon.The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous.Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions: climb one area up; climb one area down; jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x\u2009+\u2009k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon.The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water \"move in turns\" \u2014 first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on.The level is considered completed if the ninja manages to get out of the canyon.After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question.", "input_spec": "The first line contains two integers n and k (1\u2009\u2264\u2009n,\u2009k\u2009\u2264\u2009105) \u2014 the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall \u2014 a string with the length of n characters. The i-th character represents the state of the i-th wall area: character \"X\" represents a dangerous area and character \"-\" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous.", "output_spec": "Print \"YES\" (without the quotes) if the ninja can get out from the canyon, otherwise, print \"NO\" (without the quotes).", "sample_inputs": ["7 3\n---X--X\n-X--XX-", "6 2\n--X-X-\nX--XX-"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon."}, "positive_code": [{"source_code": "my ($n, $k) = split(/\\s/, );\nmy @left = split(//, );\nmy @right = split(//, );\n\n\nsub act {\n my ($w, $p, $c) = @_;\n\n #print \"In act $w, $p, $c \\n\";\n \n if($p+$k > $n){\n print \"YES\";\n exit;\n }\n \n if($w == 0){ #on left wall\n $left[$p - 1] = 'Y';\n act(1, $p+$k, $c+1) if($right[$p+$k-1] eq '-');\n act(0, $p+1, $c+1) if($left[$p] eq '-');\n act(0, $p-1, $c+1) if($left[$p-2] eq '-' and $p > $c+2);\n }else{ #on right wall\n $right[$p - 1] = 'Y';\n act(0, $p+$k, $c+1) if($left[$p+$k-1] eq '-');\n act(1, $p+1, $c+1) if($right[$p] eq '-');\n act(1, $p-1, $c+1) if($right[$p-2] eq '-' and $p > $c+2); \n }\n \n}\n\n&act(0, 1, 0);\nprint \"NO\";\n"}], "negative_code": [{"source_code": "my ($n, $k) = split(/\\s/, );\nmy @left = split(//, );\nmy @right = split(//, );\n\nsub act {\n my ($w, $p, $c) = @_;\n\n print \"In act $w, $p, $c \\n\";\n \n if($p+$k > $n){\n print \"YES\";\n exit;\n }\n \n if($w == 0){ #on left wall\n act(1, $p+$k, $c+1) if($right[$p+$k-1] eq '-');\n act(0, $p+1, $c+1) if($left[$p] eq '-');\n act(0, $p-1, $c+1) if($left[$p-1] eq '-' and $p > $c+2);\n }else{ #on right wall\n act(0, $p+$k, $c+1) if($left[$p+$k-1] eq '-');\n act(1, $p+1, $c+1) if($right[$p] eq '-');\n act(1, $p-1, $c+1) if($right[$p-1] eq '-' and $p > $c+2); \n }\n \n}\n\n&act(0, 1, 0);\nprint \"NO\";\n"}, {"source_code": "my ($n, $k) = split(/\\s/, );\nmy @left = split(//, );\nmy @right = split(//, );\n\n\nsub act {\n my ($w, $p, $c) = @_;\n\n #print \"In act $w, $p, $c \\n\";\n \n if($p+$k > $n){\n print \"YES\";\n exit;\n }\n \n if($w == 0){ #on left wall\n $left[$p ] = 'x';\n act(1, $p+$k, $c+1) if($right[$p+$k-1] eq '-');\n act(0, $p+1, $c+1) if($left[$p] eq '-');\n act(0, $p-1, $c+1) if($left[$p-1] eq '-' and $p > $c+2);\n }else{ #on right wall\n $right[$p ] = 'x';\n act(0, $p+$k, $c+1) if($left[$p+$k-1] eq '-');\n act(1, $p+1, $c+1) if($right[$p] eq '-');\n act(1, $p-1, $c+1) if($right[$p-1] eq '-' and $p > $c+2); \n }\n \n}\n\n&act(0, 1, 0);\nprint \"NO\";\n"}], "src_uid": "e95bde11483e05751ee7da91a6b4ede1"} {"nl": {"description": "You're given an array $$$a_1, \\ldots, a_n$$$ of $$$n$$$ non-negative integers.Let's call it sharpened if and only if there exists an integer $$$1 \\le k \\le n$$$ such that $$$a_1 < a_2 < \\ldots < a_k$$$ and $$$a_k > a_{k+1} > \\ldots > a_n$$$. In particular, any strictly increasing or strictly decreasing array is sharpened. For example: The arrays $$$[4]$$$, $$$[0, 1]$$$, $$$[12, 10, 8]$$$ and $$$[3, 11, 15, 9, 7, 4]$$$ are sharpened; The arrays $$$[2, 8, 2, 8, 6, 5]$$$, $$$[0, 1, 1, 0]$$$ and $$$[2, 5, 6, 9, 8, 8]$$$ are not sharpened. You can do the following operation as many times as you want: choose any strictly positive element of the array, and decrease it by one. Formally, you can choose any $$$i$$$ ($$$1 \\le i \\le n$$$) such that $$$a_i>0$$$ and assign $$$a_i := a_i - 1$$$.Tell if it's possible to make the given array sharpened using some number (possibly zero) of these operations.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 15\\ 000$$$) \u00a0\u2014 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 3 \\cdot 10^5$$$). The second line of each test case contains a sequence of $$$n$$$ non-negative integers $$$a_1, \\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 $$$3 \\cdot 10^5$$$.", "output_spec": "For each test case, output a single line containing \"Yes\" (without quotes) if it's possible to make the given array sharpened using the described operations, or \"No\" (without quotes) otherwise.", "sample_inputs": ["10\n1\n248618\n3\n12 10 8\n6\n100 11 15 9 7 8\n4\n0 1 1 0\n2\n0 0\n2\n0 1\n2\n1 0\n2\n1 1\n3\n0 1 0\n3\n1 0 1"], "sample_outputs": ["Yes\nYes\nYes\nNo\nNo\nYes\nYes\nYes\nYes\nNo"], "notes": "NoteIn the first and the second test case of the first test, the given array is already sharpened.In the third test case of the first test, we can transform the array into $$$[3, 11, 15, 9, 7, 4]$$$ (decrease the first element $$$97$$$ times and decrease the last element $$$4$$$ times). It is sharpened because $$$3 < 11 < 15$$$ and $$$15 > 9 > 7 > 4$$$.In the fourth test case of the first test, it's impossible to make the given array sharpened."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t\n\tprint qw( No Yes )[ 0 + do{\n\t\tif( $_ % 2 ){\n\t\t\tis_over_pyramide( @_ );\n\t\t\t}\n\t\telse{\n\t\t\tis_over_pyramide( @_ ) || is_over_pyramide( reverse @_ );\n\t\t\t}\n\t\t} ];\n\t\n\t}\n\nsub is_over_pyramide {\n\tmy @A = @_;\n\t\n\tmy @up = 0 .. @A - 1;\n\tmy @down = reverse @up;\n\t\n\tmy @pyramide;\n\t\n\twhile( @up ){\n\t\tpush @pyramide, ( sort { $a <=> $b } shift @up, shift @down )[ 0 ];\n\t\t}\n\t\n\tfor my $i ( 0 .. @pyramide - 2 ){\n\t\t$pyramide[ $i ] == $pyramide[ $i + 1 ] and do {\n\t\t\t$pyramide[ $i ] ++;\n\t\t\tlast;\n\t\t\t};\n\t\t}\n\t\n\t$debug and print \":[@pyramide]\";\n\t$debug and print \":[@A]\";\n\t\n\tmy $ok = 1;\n\t\n\twhile( @pyramide ){\n\t\t$ok *= ( shift @pyramide ) <= shift @A;\n\t\t}\n\t\n\t$ok;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\t\n\tprint qw( No Yes)[ 0 + do{\n\t\tif( $_ % 2 ){\n\t\t\tover_pyramide( @_ );\n\t\t\t}\n\t\telse{\n\t\t\tover_pyramide( @_, 0 ) || over_pyramide( 0, @_ );\n\t\t\t}\n\t\t} ];\n\t\n\t}\n\nsub over_pyramide {\n\tmy @A = @_;\n\t\n\tmy @up = 0 .. @A - 1;\n\tmy @down = reverse @up;\n\t\n\tmy @pyramide;\n\t\n\twhile( @up ){\n\t\tpush @pyramide, ( sort { $a <=> $b } shift @up, shift @down )[ 0 ];\n\t\t}\n\t\n\tmy $ok = 1;\n\t\n\twhile( @pyramide ){\n\t\t$ok &= ( shift @pyramide ) <= shift @A;\n\t\t}\n\t\n\t$ok;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t\n\tprint qw( No Yes )[ 0 + do{\n\t\tif( $_ % 2 ){\n\t\t\tis_over_pyramide( @_ );\n\t\t\t}\n\t\telse{\n\t\t\tis_over_pyramide( @_, 0 ) || is_over_pyramide( 0, @_ );\n\t\t\t}\n\t\t} ];\n\t\n\t}\n\nsub is_over_pyramide {\n\tmy @A = @_;\n\t\n\tmy @up = 0 .. @A - 1;\n\tmy @down = reverse @up;\n\t\n\tmy @pyramide;\n\t\n\twhile( @up ){\n\t\tpush @pyramide, ( sort { $a <=> $b } shift @up, shift @down )[ 0 ];\n\t\t}\n\t\n\tmy $ok = 1;\n\t\n\t$debug and print \":[@pyramide]\";\n\t$debug and print \":[@A]\";\n\t\n\twhile( @pyramide ){\n\t\t$ok *= ( shift @pyramide ) <= shift @A;\n\t\t}\n\t\n\t$ok;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nmy $debug = 1;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t\n\tprint qw( No Yes )[ 0 + do{\n\t\tif( $_ % 2 ){\n\t\t\tis_over_pyramide( @_ );\n\t\t\t}\n\t\telse{\n\t\t\tis_over_pyramide( @_ ) || is_over_pyramide( reverse @_ );\n\t\t\t}\n\t\t} ];\n\t\n\t}\n\nsub is_over_pyramide {\n\tmy @A = @_;\n\t\n\tmy @up = 0 .. @A - 1;\n\tmy @down = reverse @up;\n\t\n\tmy @pyramide;\n\t\n\twhile( @up ){\n\t\tpush @pyramide, ( sort { $a <=> $b } shift @up, shift @down )[ 0 ];\n\t\t}\n\t\n\tfor my $i ( 0 .. @pyramide - 2 ){\n\t\t$pyramide[ $i ] == $pyramide[ $i + 1 ] and do {\n\t\t\t$pyramide[ $i ] ++;\n\t\t\tlast;\n\t\t\t};\n\t\t}\n\t\n\t$debug and print \":[@pyramide]\";\n\t$debug and print \":[@A]\";\n\t\n\tmy $ok = 1;\n\t\n\twhile( @pyramide ){\n\t\t$ok *= ( shift @pyramide ) <= shift @A;\n\t\t}\n\t\n\t$ok;\n\t}"}], "src_uid": "b9d5e58459baf2a2c294225b73b7229b"} {"nl": {"description": "You have a list of numbers from $$$1$$$ to $$$n$$$ written from left to right on the blackboard.You perform an algorithm consisting of several steps (steps are $$$1$$$-indexed). On the $$$i$$$-th step you wipe the $$$i$$$-th number (considering only remaining numbers). You wipe the whole number (not one digit). When there are less than $$$i$$$ numbers remaining, you stop your algorithm. Now you wonder: what is the value of the $$$x$$$-th remaining number after the algorithm is stopped?", "input_spec": "The first line contains one integer $$$T$$$ ($$$1 \\le T \\le 100$$$) \u2014 the number of queries. The next $$$T$$$ lines contain queries \u2014 one per line. All queries are independent. Each line contains two space-separated integers $$$n$$$ and $$$x$$$ ($$$1 \\le x < n \\le 10^{9}$$$) \u2014 the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least $$$x$$$ numbers.", "output_spec": "Print $$$T$$$ integers (one per query) \u2014 the values of the $$$x$$$-th number after performing the algorithm for the corresponding queries.", "sample_inputs": ["3\n3 1\n4 2\n69 6"], "sample_outputs": ["2\n4\n12"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $x ) = split;\n\t\n\tprint $x * 2;\n\t}"}], "negative_code": [], "src_uid": "f79a926e18a3f81b24f2fc3ae5c8f928"} {"nl": {"description": "Kawashiro Nitori is a girl who loves competitive programming.One day she found a string and an integer. As an advanced problem setter, she quickly thought of a problem.Given a string $$$s$$$ and a parameter $$$k$$$, you need to check if there exist $$$k+1$$$ non-empty strings $$$a_1,a_2...,a_{k+1}$$$, such that $$$$$$s=a_1+a_2+\\ldots +a_k+a_{k+1}+R(a_k)+R(a_{k-1})+\\ldots+R(a_{1}).$$$$$$ Here $$$+$$$ represents concatenation. We define $$$R(x)$$$ as a reversed string $$$x$$$. For example $$$R(abcd) = dcba$$$. Note that in the formula above the part $$$R(a_{k+1})$$$ is intentionally skipped.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 100$$$) \u00a0\u2014 the number of test cases. The description of the test cases follows. The first line of each test case description contains two integers $$$n$$$, $$$k$$$ ($$$1\\le n\\le 100$$$, $$$0\\le k\\le \\lfloor \\frac{n}{2} \\rfloor$$$) \u00a0\u2014 the length of the string $$$s$$$ and the parameter $$$k$$$. The second line of each test case description contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase English letters.", "output_spec": "For each test case, print \"YES\" (without quotes), if it is possible to find $$$a_1,a_2,\\ldots,a_{k+1}$$$, and \"NO\" (without quotes) otherwise. You can print letters in any case (upper or lower).", "sample_inputs": ["7\n5 1\nqwqwq\n2 1\nab\n3 1\nioi\n4 2\nicpc\n22 0\ndokidokiliteratureclub\n19 8\nimteamshanghaialice\n6 3\naaaaaa"], "sample_outputs": ["YES\nNO\nYES\nNO\nYES\nNO\nNO"], "notes": "NoteIn the first test case, one possible solution is $$$a_1=qw$$$ and $$$a_2=q$$$.In the third test case, one possible solution is $$$a_1=i$$$ and $$$a_2=o$$$.In the fifth test case, one possible solution is $$$a_1=dokidokiliteratureclub$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t$_ = <>, chomp;\n\t\n\tmy $fail = 0;\n\t\n\twhile( $k -- ){\n\t\ts/^(.)(.+)(.)$/$2/ and\n\t\t$1 eq $3 or do { $fail = 1; last };\n\t\t}\n\t\n\tprint $fail ? 'NO' : 'YES';\n\t}"}], "negative_code": [], "src_uid": "6fbf41dc32d1c28351d78a9ec5fc0026"} {"nl": {"description": "You are given a string $$$s$$$ consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string $$$s$$$ by '0' or '1' so that the string becomes a palindrome and has exactly $$$a$$$ characters '0' and exactly $$$b$$$ characters '1'. Note that each of the characters '?' is replaced independently from the others.A string $$$t$$$ of length $$$n$$$ is called a palindrome if the equality $$$t[i] = t[n-i+1]$$$ is true for all $$$i$$$ ($$$1 \\le i \\le n$$$).For example, if $$$s=$$$\"01?????0\", $$$a=4$$$ and $$$b=4$$$, then you can replace the characters '?' in the following ways: \"01011010\"; \"01100110\". For the given string $$$s$$$ and the numbers $$$a$$$ and $$$b$$$, replace all the characters with '?' in the string $$$s$$$ by '0' or '1' so that the string becomes a palindrome and has exactly $$$a$$$ characters '0' and exactly $$$b$$$ characters '1'.", "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 two integers $$$a$$$ and $$$b$$$ ($$$0 \\le a, b \\le 2 \\cdot 10^5$$$, $$$a + b \\ge 1$$$). The second line of each test case contains the string $$$s$$$ of length $$$a+b$$$, consisting of the characters '0', '1', and '?'. It is guaranteed that the sum of the string lengths of $$$s$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output: \"-1\", if you can't replace all the characters '?' in the string $$$s$$$ by '0' or '1' so that the string becomes a palindrome and that it contains exactly $$$a$$$ characters '0' and exactly $$$b$$$ characters '1'; the string that is obtained as a result of the replacement, otherwise. If there are several suitable ways to replace characters, you can output any.", "sample_inputs": ["9\n4 4\n01?????0\n3 3\n??????\n1 0\n?\n2 2\n0101\n2 2\n01?0\n0 1\n0\n0 3\n1?1\n2 2\n?00?\n4 3\n??010?0"], "sample_outputs": ["01011010\n-1\n0\n-1\n0110\n-1\n111\n1001\n0101010"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n \r\n my ($a,$b) = map { $_ - 0 } split(/\\s+/,);\r\n my $n = $a + $b;\r\n my $n2 = int($n/2);\r\n \r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n my @s = split(//,$s);\r\n $#s = $n-1;\r\n \r\n my %cnt = (); $cnt{'0'} = 0; $cnt{'1'} = 0;\r\n \r\n my $err = undef;\r\n for(my $i=0;$i<$n2;$i++){\r\n my $c1 = $s[$i];\r\n my $c2 = $s[$n-1-$i];\r\n if( $c1 eq '?' and $c2 ne '?' ){\r\n $s[$i] = $c2;\r\n $c1 = $c2;\r\n }\r\n if( $c2 eq '?' and $c1 ne '?' ){\r\n $s[$n-1-$i] = $c1;\r\n $c2 = $c1;\r\n }\r\n if( $c1 ne $c2 ){\r\n $err = 1; last;\r\n }\r\n $cnt{$c1} += 2 if $c1 ne '?';\r\n }\r\n if( $err ){\r\n print \"-1\\n\"; next;\r\n }\r\n if( $n % 2 == 1 ){\r\n $cnt{$s[$n2]} ++ if $s[$n2] ne '?';\r\n }\r\n \r\n if( ($cnt{'0'} > $a) or ($cnt{'1'} > $b) ){\r\n print \"-1\\n\"; next;\r\n }\r\n my $l_a = $a - $cnt{'0'};\r\n my $l_b = $b - $cnt{'1'};\r\n \r\n if( ( ($n % 2 == 0) or ($n % 2 == 1 and $s[$n2] ne '?' ) ) and ( ( $l_a % 2 == 1 ) or ( $l_b % 2 == 1 ) ) ){\r\n print \"-1\\n\"; next;\r\n }\r\n if( $n % 2 == 1 and $s[$n2] eq '?' ){\r\n if( $l_a % 2 == 1 ){\r\n $s[$n2] = '0';\r\n $l_a --;\r\n }\r\n if( $l_b % 2 == 1 ){\r\n $s[$n2] = '1';\r\n $l_b --;\r\n }\r\n }\r\n if( ( $l_a % 2 == 1 ) or ( $l_b % 2 == 1 ) ){\r\n print \"-1\\n\"; next;\r\n }\r\n for(my $i=0;$i<$n2;$i++){\r\n if( $s[$i] eq '?' ){\r\n if( $l_a > 0 ){\r\n $s[$i] = '0';\r\n $s[$n-1-$i] = '0';\r\n $l_a -= 2;\r\n } elsif( $l_b > 0 ){\r\n $s[$i] = '1';\r\n $s[$n-1-$i] = '1';\r\n $l_b -= 2;\r\n }\r\n }\r\n }\r\n print ( join('',@s) . \"\\n\" );\r\n}\r\n\r\nexit(0);\r\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $A, $B ) = split;\n\t$_ = <>, chomp;\n\t\n\tmy $reverse = reverse;\n\t\n\tfor my $i ( 0 .. -1 + length ){\n\t\tmy $c1 = substr $_, $i, 1;\n\t\tmy $c2 = substr $reverse, $i, 1;\n\t\tif( 0 ){ ; }\n\t\telsif( $c1 eq '?' and $c2 eq '?' ){\n\t\t\t;\n\t\t\t}\n\t\telsif( $c1 eq '?' and $c2 eq '0' ){\n\t\t\tsubstr $_, $i, 1, 0;\n\t\t\t}\n\t\telsif( $c1 eq '0' and $c2 eq '?' ){\n\t\t\tsubstr $reverse, $i, 1, 0;\n\t\t\t}\n\t\telsif( $c1 eq '?' and $c2 eq '1' ){\n\t\t\tsubstr $_, $i, 1, 1;\n\t\t\t}\n\t\telsif( $c1 eq '1' and $c2 eq '?' ){\n\t\t\tsubstr $reverse, $i, 1, 1;\n\t\t\t}\n\t\t}\n\t\n\tif( $_ ne reverse ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\tmy $_0 = () = m/0/g;\n\tmy $_1 = () = m/1/g;\n\t\n\t$A -= $_0;\n\t$B -= $_1;\n\t\n\tif( $A < 0 or $B < 0 ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\tmy @change;\n\t\n\tif( ( length ) % 2 == 1 ){\n\t\tif( $A % 2 == 1 ){\n\t\t\t$A --;\n\t\t\tpush @change, 0;\n\t\t\t}\n\t\telsif( $B % 2 == 1 ){\n\t\t\t$B --;\n\t\t\tpush @change, 1;\n\t\t\t}\n\t\t}\n\t\n\tif( $A % 2 == 1 or $B % 2 == 1 ){\n\t\tprint -1;\n\t\tnext;\n\t\t}\n\t\n\tpush @change, ( 0 ) x ( $A / 2 ), ( 1 ) x ( $B / 2 );\n\tunshift @change, ( 1 ) x ( $B / 2 ), ( 0 ) x ( $A / 2 );\n\t\n\ts/\\?/shift @change/ge;\n\t\n\tprint;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n \r\n my ($a,$b) = map { $_ - 0 } split(/\\s+/,);\r\n my $n = $a + $b;\r\n my $n2 = int($n/2);\r\n \r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n my @s = split(//,$s);\r\n $#s = $n-1;\r\n \r\n my %cnt = (); $cnt{'0'} = 0; $cnt{'1'} = 0;\r\n \r\n my $err = undef;\r\n for(my $i=0;$i<$n2;$i++){\r\n my $c1 = $s[$i];\r\n my $c2 = $s[$n-1-$i];\r\n if( $c1 eq '?' and $c2 ne '?' ){\r\n $s[$i] = $c2;\r\n $c1 = $c2;\r\n }\r\n if( $c2 eq '?' and $c1 ne '?' ){\r\n $s[$n-1-$i] = $c1;\r\n $c2 = $c1;\r\n }\r\n if( $c1 ne $c2 ){\r\n $err = 1; last;\r\n }\r\n $cnt{$c1} += 2 if $c1 ne '?';\r\n }\r\n if( $err ){\r\n print \"-1\\n\"; next;\r\n }\r\n if( $n % 2 == 1 ){\r\n $cnt{$s[$n2]} ++ if $s[$n2] ne '?';\r\n }\r\n \r\n if( ($cnt{'0'} > $a) or ($cnt{'1'} > $b) ){\r\n print \"-1\\n\"; next;\r\n }\r\n my $l_a = $a - $cnt{'0'};\r\n my $l_b = $b - $cnt{'1'};\r\n \r\n #if( ($n % 2 == 0) and ( ( $l_a % 2 == 1 ) or ( $l_b % 2 == 1 ) ) ){\r\n # print \"-1\\n\"; next;\r\n #}\r\n if( $n % 2 == 1 ){\r\n if( $l_a % 2 == 1 ){\r\n $s[$n2] = '0';\r\n $l_a --;\r\n }\r\n if( $l_b % 2 == 1 ){\r\n $s[$n2] = '1';\r\n $l_b --;\r\n }\r\n }\r\n for(my $i=0;$i<$n2;$i++){\r\n if( $s[$i] eq '?' ){\r\n if( $l_a > 0 ){\r\n $s[$i] = '0';\r\n $s[$n-1-$i] = '0';\r\n $l_a -= 2;\r\n } elsif( $l_b > 0 ){\r\n $s[$i] = '1';\r\n $s[$n-1-$i] = '1';\r\n $l_b -= 2;\r\n }\r\n }\r\n }\r\n print ( join('',@s) . \"\\n\" );\r\n}\r\n\r\nexit(0);\r\n"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($a,$b) = map { $_ - 0 } split(/\\s+/,);\r\n my $n = $a + $b;\r\n my $n2 = int($n/2);\r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n my @s = split(//,$s);\r\n \r\n my %cnt = ( '0'=>0, '1'=>0 );\r\n \r\n my $err = undef;\r\n for(my $i=0;$i<$n2;$i++){\r\n my $c1 = $s[$i];\r\n my $c2 = $s[$n-1-$i];\r\n if( $c1 ne '?' and $c2 ne '?' and $c1 ne $c2 ){\r\n $err = 1; last;\r\n }\r\n if( $c1 eq '?' and $c2 ne '?' ){\r\n $s[$i] = $c2;\r\n $c1 = $c2;\r\n }\r\n if( $c2 eq '?' and $c1 ne '?' ){\r\n $s[$n-1-$i] = $c1;\r\n $c2 = $c1;\r\n }\r\n $cnt{$c1} ++ if $c1 ne '?';\r\n $cnt{$c2} ++ if $c2 ne '?';\r\n }\r\n if( $err ){\r\n print \"-1\\n\"; next;\r\n }\r\n if( $n % 2 == 1 ){\r\n $cnt{$s[$n2]} ++ if $s[$n2] ne '?';\r\n }\r\n if( $cnt{'0'} > $a or $cnt{'1'} > $b ){\r\n print \"-1\\n\"; next;\r\n }\r\n my $l_a = $a - $cnt{'0'};\r\n my $l_b = $b - $cnt{'1'};\r\n \r\n if( $n % 2 == 0 and ( ( $l_a % 2 == 1 ) or ( $l_b % 2 == 1 ) ) ){\r\n print \"-1\\n\"; next;\r\n }\r\n if( $n % 2 == 1 ){\r\n if( $l_a % 2 == 1 ){\r\n $s[$n2] = '0';\r\n $l_a --;\r\n }\r\n if( $l_b % 2 == 1 ){\r\n $s[$n2] = '1';\r\n $l_b --;\r\n }\r\n }\r\n for(my $i=0;$i<$n2;$i++){\r\n if( $s[$i] eq '?' ){\r\n if( $l_a > 0 ){\r\n $s[$i] = '0';\r\n $s[$n-1-$i] = '0';\r\n $l_a -= 2;\r\n } elsif( $l_b > 0 ){\r\n $s[$i] = '1';\r\n $s[$n-1-$i] = '1';\r\n $l_b -= 2;\r\n }\r\n }\r\n }\r\n $#s = $n-1;\r\n print ( join('',@s) . \"\\n\" );\r\n}\r\n\r\nexit(0);\r\n"}], "src_uid": "001ac8bce4e44e9266a13eb27760906c"} {"nl": {"description": "There are n employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote.Each of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions on what should be the outcome of the vote. The voting procedure is rather complicated: Each of n employees makes a statement. They make statements one by one starting from employees 1 and finishing with employee n. If at the moment when it's time for the i-th employee to make a statement he no longer has the right to vote, he just skips his turn (and no longer takes part in this voting). When employee makes a statement, he can do nothing or declare that one of the other employees no longer has a right to vote. It's allowed to deny from voting people who already made the statement or people who are only waiting to do so. If someone is denied from voting he no longer participates in the voting till the very end. When all employees are done with their statements, the procedure repeats: again, each employees starting from 1 and finishing with n who are still eligible to vote make their statements. The process repeats until there is only one employee eligible to vote remaining and he determines the outcome of the whole voting. Of course, he votes for the decision suitable for his fraction. You know the order employees are going to vote and that they behave optimal (and they also know the order and who belongs to which fraction). Predict the outcome of the vote.", "input_spec": "The first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000)\u00a0\u2014 the number of employees. The next line contains n characters. The i-th character is 'D' if the i-th employee is from depublicans fraction or 'R' if he is from remocrats.", "output_spec": "Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win.", "sample_inputs": ["5\nDDRRR", "6\nDDRRRR"], "sample_outputs": ["D", "R"], "notes": "NoteConsider one of the voting scenarios for the first sample: Employee 1 denies employee 5 to vote. Employee 2 denies employee 3 to vote. Employee 3 has no right to vote and skips his turn (he was denied by employee 2). Employee 4 denies employee 2 to vote. Employee 5 has no right to vote and skips his turn (he was denied by employee 1). Employee 1 denies employee 4. Only employee 1 now has the right to vote so the voting ends with the victory of depublicans. "}, "positive_code": [{"source_code": "\nmy $n=<>;\nchomp($n);\nmy $s=<>;\nchomp($s);\n\n\nmy @RINDEX;\nmy @DINDEX;\n\nfor(my $i=0;$i=length($s)){\n\t$j=0;\n}\n}\n\n\nif(scalar(@RINDEX)){\n\tprint \"R \\n\";\n}\nif(scalar(@DINDEX)){\n\tprint \"D \\n\";\n}\n"}, {"source_code": "use strict;\nmy $n=<>;\nchomp($n);\nmy $s=<>;\nchomp($s);\n\nmy $i=0;\nmy @RINDEX;\nmy @DINDEX;\n\nfor(my $i=0;$i0 and scalar(@DINDEX)>0 ){\n\n\tif($RINDEX[0]==$j)\n\t {\n\t \t\tshift @DINDEX;\n\t \t\tpush(@RINDEX,shift @RINDEX);\n\t\t}elsif($DINDEX[0]==$j){\n\t \t\tshift @RINDEX;\n\t \t\tpush(@DINDEX,shift @DINDEX);\n\t\t}\n$j++;\nif($j>=length($s)){\n\t$j=0;\n}\n}\n\n\nif(scalar(@RINDEX)>0){\n\tprint \"R \\n\";\n}\nif(scalar(@DINDEX)>0){\n\tprint \"D \\n\";\n}\n\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\tmy $RD = 0;\n\t\n\ts/\n\t\t(.) \n\t\t(?{ $1 eq 'R' ? $RD ++ : $RD -- })\n\t\n\t/\n\t\t$debug and print \"[$1|$RD]\";\n\t\t\n\t\t$1 eq 'R' ? \n\t\t\t( $RD > 0 ? $1 : '' )\n\t\t\t:\n\t\t\t( $RD < 0 ? $1 : '' )\n\t/xge \n\t\twhile /R/ and /D/;\n\t\n\tprint /R/ ? 'R' : 'D';\n\t\n\t}"}, {"source_code": "<>;\n\n$_ = <>;\n\ns/./ $J = $& cmp 'J', $& x ( 0 < ($RD += $J ) * $J ) /ge \n\twhile /R/ and /D/;\n\n/./, print $&"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\nmy $n=<>;\nchomp($n);\nmy $s=<>;\nchomp($s);\n\nmy $i=0;\n\nwhile(index($s,\"D\") >=0 and index($s,\"R\") >=0)\n{\n\n\tif(substr($s,$i,1) eq \"R\")\n\t{\n\t\tmy $ind=index($s,\"D\");\n\t\tsubstr($s,$ind,1,\"0\");\n\t}\n\telsif(substr($s,$i,1) eq \"D\")\n\t{\n\t\tmy $ind=index($s,\"R\");\n\t\tsubstr($s,$ind,1,\"0\");\n\t}\n\t$i++;\n\t$s =~ s/0//g;\n\tif($i>=length($s)){\n\t\t$i=0;\n\t}\n\n}\n\n\nif(index($s,\"D\")>=0){\n\tprint \"D\\n\";\n}\nif(index($s,\"R\")>=0){\n\tprint \"R\\n\";\n}\n\n"}, {"source_code": "use strict;\nmy $n=<>;\nchomp($n);\nmy $s=<>;\nchomp($s);\n\nmy $i=0;\nmy @RINDEX;\nmy @DINDEX;\nmy $j=0;\nmy $k=0;\nfor(my $i=0;$i0 and scalar(@DINDEX)>0 ){\nif($RINDEX[$i] == $j ){\n\tshift @DINDEX;\n\tif($k>0){\n\t\t$k--;\n\t}\n $i++;\n}elsif( $DINDEX[$k] == $j){\n\tshift @RINDEX;\n\tif($i>0){\n\t\t$i--;\n\t}\n\t$k++;\n}\n$j++;\nif($j>=length($s)){\n$i=0;\n$j=0;\n$k=0;\n}\n}\n\nif(scalar(@RINDEX)>0){\n\tprint \"R \\n\";\n}\nif(scalar(@DINDEX)>0){\n\tprint \"D \\n\";\n}\n\n\n"}, {"source_code": "use strict;\nuse warnings;\nmy $n=<>;\nchomp($n);\nmy $s=<>;\nchomp($s);\n\nmy $i=0;\nmy @RINDEX;\nmy @DINDEX;\nmy $j=0;\nmy $k=0;\nfor(my $i=0;$i0 and scalar(@DINDEX)>0){\nif($RINDEX[$i] > $DINDEX[$i]){\n\tshift @DINDEX;\n}else{\n\tshift @RINDEX;\n}\n}\n\nif(scalar(@RINDEX)>0){\n\tprint \"R \\n\";\n}\nif(scalar(@DINDEX)>0){\n\tprint \"D \\n\";\n}\n\n\n"}, {"source_code": "use strict;\nuse warnings;\nmy $n=<>;\nchomp($n);\nmy $s=<>;\nchomp($s);\n\nmy $i=0;\nmy @RINDEX;\nmy @DINDEX;\nmy $j=0;\nmy $k=0;\nfor(my $i=0;$i0 and scalar(@DINDEX)>0){\nif($RINDEX[$i] < $DINDEX[$i]){\n\tshift @DINDEX;\n}elsif($RINDEX[$i] > $DINDEX[$i]){\n\tshift @RINDEX;\n}\n}\n\nif(scalar(@RINDEX)>0){\n\tprint \"R \\n\";\n}\nif(scalar(@DINDEX)>0){\n\tprint \"D \\n\";\n}\n\n\n"}, {"source_code": "use strict;\nuse warnings;\nmy $n=<>;\nchomp($n);\nmy $s=<>;\nchomp($s);\n\nmy $i=0;\nmy @RINDEX;\nmy @DINDEX;\nmy $j=0;\nmy $k=0;\nfor(my $i=0;$i0 and scalar(@DINDEX)>0){\nprint \"@RINDEX \\n\";\nprint \"@DINDEX \\n\";\nif($RINDEX[$i] < $DINDEX[$i]){\n\tshift @DINDEX;\n}elsif($RINDEX[$i] > $DINDEX[$i]){\n\tshift @RINDEX;\n}\n}\n\nif(scalar(@RINDEX)>0){\n\tprint \"R \\n\";\n}\nif(scalar(@DINDEX)>0){\n\tprint \"D \\n\";\n}\n\n\n"}], "src_uid": "cc50eef15726d429cfc2e76f0aa8cd19"} {"nl": {"description": "One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the \"Goldbach's conjecture\". It says: \"each even number no less than four can be expressed as the sum of two primes\". Let's modify it. How about a statement like that: \"each integer no less than 12 can be expressed as the sum of two composite numbers.\" Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers.", "input_spec": "The only line contains an integer n (12\u2009\u2264\u2009n\u2009\u2264\u2009106).", "output_spec": "Output two composite integers x and y (1\u2009<\u2009x,\u2009y\u2009<\u2009n) such that x\u2009+\u2009y\u2009=\u2009n. If there are multiple solutions, you can output any of them.", "sample_inputs": ["12", "15", "23", "1000000"], "sample_outputs": ["4 8", "6 9", "8 15", "500000 500000"], "notes": "NoteIn the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output \"6 6\" or \"8 4\" as well.In the second example, 15 = 6 + 9. Note that you can't output \"1 14\" because 1 is not a composite number."}, "positive_code": [{"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\n\nsub isPrime{\n my $num = shift;\n my $count = 0;\n for(my $i = 1 ; $i <= int(sqrt($num)) ; $i++){\n if($num % $i == 0){\n $count++;\n }\n }\n if($count > 1){\n return 0;\n }else{\n return 1;\n }\n}\n\nfor(my $j = 2 ;;$j++){\n my $p = $n - $j;\n if(!isPrime($p) && !isPrime($j)){\n print \"$j $p\";\n last;\n }\n}\n"}, {"source_code": "my $a = <>;\nforeach (map { $_ * 3 } 2 .. int $a / 3)\n{\n if (($a - $_) % 2 == 0)\n {\n print $_, \" \", $a-$_;\n last;\n }\n}"}, {"source_code": "my $n = int(<>);\nif ($n % 2) {\n print \"9 \" . ($n - 9) . \"\\n\";\n}\nelse {\n print \"4 \" . ($n - 4) . \"\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nmy $n = <>;\n\nsub isPrime($) {\n\tmy $arg = $_[0];\n\tmy $res = 1;\n\tfor (my $i = 2; $i <= sqrt($arg); $i++) {\n\t\tunless ($arg % $i) {\n\t\t\t$res = 0;\n\t\t}\n\t}\n\t\n\treturn $res;\n}\n\nfor (my $i = 4; $i <= ($n / 2); $i++) {\n\tif (!isPrime($i) && !isPrime($n - $i)) {\n\t\tprint($i . \" \" . ($n - $i));\n\t\texit;\n\t}\n}\n"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nchomp($n = <>);\n$n%2==0 and say 4,' ',$n-4 or say 9,' ',$n-9;"}, {"source_code": "chomp (my $n = <>);\nif ($n & 1) {\n printf \"%d %d\\n\", 9, $n - 9;\n} else {\n printf \"%d %d\\n\", 4, $n - 4;\n}"}, {"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nsub primeq {\n my $n = shift;\n return '' if $n < 2;\n for (2 .. sqrt($n) + 1) {\n return '' if $n % $_ == 0;\n }\n return 1;\n}\n\nmy $n = <>;\nfor (4 .. $n / 2) {\n if (!primeq($_) && !primeq($n - $_)) {\n print \"$_ \", $n - $_, \"\\n\";\n last;\n }\n}\n"}, {"source_code": "chomp($n = <>);\nprint($n & 1 ? \"9 \".($n-9).\"\\n\" : \"4 \".($n-4).\"\\n\");\n"}, {"source_code": "while(<>){\n\t\n\t$_-=12;\n\tfor $i(4,6,8..10){\n\t\t$a=$i;\n\t\t$b=$_+12-$i;\n\t\tif (($a=~/[024685]$/ or !($a %3))\n\t\t\t\tand $b!=2\n\t\t\t\tand $b!=5\n\t\t\t\tand $b!=3\n\t\t\t\tand $b!=7\n\t\t\t\tand \n\t\t\t($b=~/[024685]$/ or !($b %3))){\n\t\t\t\n\t\t\tprint \"$a $b\\n\";\n\t\t\tlast\n\t\t\t}\n\t\t}\n\t}"}], "negative_code": [{"source_code": "my $n = int(<>);\nif ($n % 2) {\n print \"7 \" . $n - 7 . \"\\n\";\n}\nelse {\n print \"4 \" . $n - 4 . \"\\n\";\n}\n"}, {"source_code": "my $n = int(<>);\nif ($n % 2) {\n print \"7 \" . ($n - 7) . \"\\n\";\n}\nelse {\n print \"4 \" . ($n - 4) . \"\\n\";\n}"}, {"source_code": "while(<>){\n\t\n\t$_-=12;\n\tfor $i(4,6,8..10){\n\t\t$a=$i;\n\t\t$b=$_+12-$i;\n\t\tif ($a=~/[024685]$/\n\t\t\t\tand $b!=2\n\t\t\t\tand $b!=5\n\t\t\t\tand \n\t\t\t$b=~/[024685]$/){\n\t\t\t\n\t\t\tprint \"$a $b\\n\";\n\t\t\tlast\n\t\t\t}\n\t\t}\n\t}"}, {"source_code": "$_-=<>-12;\nfor $i(1..11){\n\t$a=(12-$i);\n\t$b=$_+$i;\n\tif ($a=~/[024685]$/\n\t\t\tand \n\t\t$b=~/[024685]$/){\n\t\t\t\n\t\tprint \"$a $b\";\n\t\tlast\n\t\t}\n\t}"}], "src_uid": "3ea971165088fae130d866180c6c868b"} {"nl": {"description": "You have $$$n$$$ gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The $$$i$$$-th gift consists of $$$a_i$$$ candies and $$$b_i$$$ oranges.During one move, you can choose some gift $$$1 \\le i \\le n$$$ and do one of the following operations: eat exactly one candy from this gift (decrease $$$a_i$$$ by one); eat exactly one orange from this gift (decrease $$$b_i$$$ by one); eat exactly one candy and exactly one orange from this gift (decrease both $$$a_i$$$ and $$$b_i$$$ by one). Of course, you can not eat a candy or orange if it's not present in the gift (so neither $$$a_i$$$ nor $$$b_i$$$ can become less than zero).As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: $$$a_1 = a_2 = \\dots = a_n$$$ and $$$b_1 = b_2 = \\dots = b_n$$$ (and $$$a_i$$$ equals $$$b_i$$$ is not necessary).Your task is to find the minimum number of moves required to equalize all the given gifts.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$$$) \u2014 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$$$) \u2014 the number of gifts. 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 number of candies in the $$$i$$$-th gift. The third line of the test case contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\le b_i \\le 10^9$$$), where $$$b_i$$$ is the number of oranges in the $$$i$$$-th gift.", "output_spec": "For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.", "sample_inputs": ["5\n3\n3 5 6\n3 2 3\n5\n1 2 3 4 5\n5 4 3 2 1\n3\n1 1 1\n2 2 2\n6\n1 1000000000 1000000000 1000000000 1000000000 1000000000\n1 1 1 1 1 1\n3\n10 12 8\n7 5 4"], "sample_outputs": ["6\n16\n0\n4999999995\n7"], "notes": "NoteIn the first test case of the example, we can perform the following sequence of moves: choose the first gift and eat one orange from it, so $$$a = [3, 5, 6]$$$ and $$$b = [2, 2, 3]$$$; choose the second gift and eat one candy from it, so $$$a = [3, 4, 6]$$$ and $$$b = [2, 2, 3]$$$; choose the second gift and eat one candy from it, so $$$a = [3, 3, 6]$$$ and $$$b = [2, 2, 3]$$$; choose the third gift and eat one candy and one orange from it, so $$$a = [3, 3, 5]$$$ and $$$b = [2, 2, 2]$$$; choose the third gift and eat one candy from it, so $$$a = [3, 3, 4]$$$ and $$$b = [2, 2, 2]$$$; choose the third gift and eat one candy from it, so $$$a = [3, 3, 3]$$$ and $$$b = [2, 2, 2]$$$. "}, "positive_code": [{"source_code": "use strict;\nuse warnings;\n\nmy $t = <>;\nwhile($t--){\n my $n = <>;\n my @a = split(' ', <>);\n my @b = split(' ', <>);\n\n my $min = 1000000001;\n for(my $i = 0; $i <= $#a; $i++){\n if($a[$i] < $min) {\n $min = $a[$i];\n }\n }\n for(my $i = 0; $i <= $#a; $i++){\n $a[$i] -= $min;\n }\n $min = 1000000001;\n for(my $i = 0; $i <= $#b; $i++){\n if($b[$i] < $min) {\n $min = $b[$i];\n }\n }\n for(my $i = 0; $i <= $#b; $i++){\n $b[$i] -= $min;\n }\n my $ans = 0;\n for(my $i = 0; $i < $n; $i++){\n if($a[$i] >= $b[$i]) {\n $ans += $a[$i];\n } else {\n $ans += $b[$i];\n }\n }\n print $ans.\"\\n\";\n}"}], "negative_code": [], "src_uid": "35368cefc5ce46a9f41a15734826a151"} {"nl": {"description": "You are given a text consisting of n lines. Each line contains some space-separated words, consisting of lowercase English letters.We define a syllable as a string that contains exactly one vowel and any arbitrary number (possibly none) of consonants. In English alphabet following letters are considered to be vowels: 'a', 'e', 'i', 'o', 'u' and 'y'.Each word of the text that contains at least one vowel can be divided into syllables. Each character should be a part of exactly one syllable. For example, the word \"mamma\" can be divided into syllables as \"ma\" and \"mma\", \"mam\" and \"ma\", and \"mamm\" and \"a\". Words that consist of only consonants should be ignored.The verse patterns for the given text is a sequence of n integers p1,\u2009p2,\u2009...,\u2009pn. Text matches the given verse pattern if for each i from 1 to n one can divide words of the i-th line in syllables in such a way that the total number of syllables is equal to pi.You are given the text and the verse pattern. Check, if the given text matches the given verse pattern.", "input_spec": "The first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100)\u00a0\u2014 the number of lines in the text. The second line contains integers p1,\u2009...,\u2009pn (0\u2009\u2264\u2009pi\u2009\u2264\u2009100)\u00a0\u2014 the verse pattern. Next n lines contain the text itself. Text consists of lowercase English letters and spaces. It's guaranteed that all lines are non-empty, each line starts and ends with a letter and words are separated by exactly one space. The length of each line doesn't exceed 100 characters.", "output_spec": "If the given text matches the given verse pattern, then print \"YES\" (without quotes) in the only line of the output. Otherwise, print \"NO\" (without quotes).", "sample_inputs": ["3\n2 2 3\nintel\ncode\nch allenge", "4\n1 2 3 1\na\nbcdefghi\njklmnopqrstu\nvwxyz", "4\n13 11 15 15\nto be or not to be that is the question\nwhether tis nobler in the mind to suffer\nthe slings and arrows of outrageous fortune\nor to take arms against a sea of troubles"], "sample_outputs": ["YES", "NO", "YES"], "notes": "NoteIn the first sample, one can split words into syllables in the following way: in-telco-dech al-len-geSince the word \"ch\" in the third line doesn't contain vowels, we can ignore it. As the result we get 2 syllabels in first two lines and 3 syllables in the third one."}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $n = <>;\nmy @arr = split \" \", <>;\n# print \"@arr\";\nmy $ans = \"YES\";\nfor (0..$n-1) {\n my $count = 0;\n chomp(my $line = <>);\n $count = () = $line =~ /[aeiouy]/g;\n if ($count != $arr[$_]) {\n $ans = \"NO\";\n last;\n }\n}\nprint \"$ans\\n\";\n"}, {"source_code": "$n = int();\n@k = split(/ /, );\nmy $i = 0;\nwhile() {\n if(($_ =~ tr/[aeiouy]//) != $k[$i]) {\n print \"NO\\n\";\n exit 0;\n }\n $i++;\n}\nprint \"YES\\n\";"}, {"source_code": "$,=\" \";\nmy %v=map {$_=> 1} ('a','e','i','o','u','y');\nmy $N=<>;\nchomp($N);\nmy @counts=split / /,<>;\nchomp(@counts);\nfor my $i (1..$N)\n{\n my $s=<>;\n chomp($s);\n my $k=0;\n map {$k+=$v{$_}} split //,$s;\n if($k!=$counts[$i-1])\n {\n print \"NO\\n\";\n exit(0);\n }\n}\nprint \"YES\\n\";\n"}, {"source_code": "\nmy $n = <> + 0;\nmy $P = <>;\nmy @pp = split ' ', $P;\n\nfor (my $i = 0; $i < $n; ++$i) {\n\tmy $line = <>;\n\tmy @lets = split //, $line;\n\tforeach (@lets) {\n\t\t--$pp[$i] if /a|e|i|o|u|y/;\n\t}\n\tif ($pp[$i] != 0){\n\t\tprint \"NO\\n\";\n\t\texit 0;\n\t}\n}\nprint \"YES\\n\";\n\n"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t$f = 0;\n\twhile(<>){\n\t\t(shift @_) == ( () = /[aeiouy]/g ) or $f++\n\t\t\n\t\t}\n\t\n\tprint $f ? \"NO\" : \"YES\"\n\t}"}, {"source_code": "<>;\n\n@_ = split ' ', <>;\n\nwhile(<>){\n\t(shift @_) == ( () = /[aeiouy]/g ) or $f++\n\t\t\n\t}\n\t\nprint $f ? \"NO\" : \"YES\""}, {"source_code": "<>;\n\nprint <> =~ s/\\n//r eq (join ' ', map y/yaeiou//d, <>) ? \"YES\" : \"NO\""}, {"source_code": "<>;\n\n@_ = split ' ', <>;\n\ny/yaeiou//d == shift @_ or $f++ while <>;\n\t\nprint $f ? \"NO\" : \"YES\""}], "negative_code": [], "src_uid": "cf595689b5cbda4f1d629524ad275650"} {"nl": {"description": "Let $$$s(x)$$$ be sum of digits in decimal representation of positive integer $$$x$$$. Given two integers $$$n$$$ and $$$m$$$, find some positive integers $$$a$$$ and $$$b$$$ such that $$$s(a) \\ge n$$$, $$$s(b) \\ge n$$$, $$$s(a + b) \\le m$$$. ", "input_spec": "The only line of input contain two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 1129$$$).", "output_spec": "Print two lines, one for decimal representation of $$$a$$$ and one for decimal representation of $$$b$$$. Both numbers must not contain leading zeros and must have length no more than $$$2230$$$.", "sample_inputs": ["6 5", "8 16"], "sample_outputs": ["6 \n7", "35 \n53"], "notes": "NoteIn the first sample, we have $$$n = 6$$$ and $$$m = 5$$$. One valid solution is $$$a = 6$$$, $$$b = 7$$$. Indeed, we have $$$s(a) = 6 \\ge n$$$ and $$$s(b) = 7 \\ge n$$$, and also $$$s(a + b) = s(13) = 4 \\le m$$$."}, "positive_code": [{"source_code": "print+(\"4\"x998).\"5\\n\".(\"5\"x999)"}, {"source_code": "($n, $m) = split \" \", <>;\nprint 4 x $n, \"5\\n\";\nprint 5 x $n, \"5\\n\";"}], "negative_code": [], "src_uid": "bba30bd91f085471f624c0f723b78a82"} {"nl": {"description": "A and B are preparing themselves for programming contests.After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes.A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes). B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one).Also, A and B have a string s. Now they are trying to find out how many substrings t of a string s are interesting to B (that is, t starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A), except for the first and the last one is equal to zero.Naturally, A and B have quickly found the number of substrings t that are interesting to them. Can you do it? ", "input_spec": "The first line contains 26 integers xa,\u2009xb,\u2009...,\u2009xz (\u2009-\u2009105\u2009\u2264\u2009xi\u2009\u2264\u2009105) \u2014 the value assigned to letters a,\u2009b,\u2009c,\u2009...,\u2009z respectively. The second line contains string s of length between 1 and 105 characters, consisting of Lating lowercase letters\u2014 the string for which you need to calculate the answer. ", "output_spec": "Print the answer to the problem. ", "sample_inputs": ["1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\nxabcab", "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naaa"], "sample_outputs": ["2", "2"], "notes": "NoteIn the first sample test strings satisfying the condition above are abca and bcab.In the second sample test strings satisfying the condition above are two occurences of aa."}, "positive_code": [{"source_code": "chomp (my @n = split /\\s+/, );\nchomp (my @str = split /\\s*/, );\n\nmy %wt;\nforeach ('a' .. 'z') {\n $wt{$_} = shift @n;\n}\n\nsub calc {\n my $s = 0;\n $s += $wt{$_} foreach (@_);\n return $s;\n}\n\nmy %pos;\nmy @sum;\nmy $c = 0;\n\nforeach my $x (0 .. $#str) {\n $sum[$x] = $wt{$str[$x]};\n $sum[$x] += $sum[$x - 1] if ($x > 0);\n push @{$pos{$str[$x]}}, $x;\n}\n\n# printf \"@sum\\n\";\nforeach my $l ('a' .. 'z') {\n my %map = ();\n foreach (@{$pos{$l}}) {\n # printf \"Char: $l Pos: $_ Sum to p-1: $sum[$_ - 1] Sum to p: $sum[$_]\\n\";\n $c += $map{$sum[$_ - 1]} if exists $map{$sum[$_ - 1]};\n $map{$sum[$_]}++;\n }\n}\n\nprintf \"$c\\n\";\n"}], "negative_code": [{"source_code": "use List::Util qw(first max maxstr min minstr reduce shuffle sum);\nuse English;\n\nchomp (my @n = split /\\s+/, );\nchomp (my @str = split /\\s*/, );\n\nmy %wt;\nforeach ('a' .. 'z') {\n $wt{$_} = shift @n;\n}\n\nsub calc {\n my $s = 0;\n $s += $wt{$_} foreach (@_);\n return $s;\n}\n\nmy %pos;\nmy @sum;\nmy $c = 0;\n\nforeach my $x (0 .. $#str) {\n $sum[$x] = $wt{$str[$x]};\n $sum[$x] += $sum[$x - 1] if ($x > 0);\n push @{$pos{$str[$x]}}, $x;\n}\n\nprintf \"@sum\\n\";\nforeach my $l ('a' .. 'z') {\n my %map = ();\n foreach (@{$pos{$l}}) {\n printf \"Char: $l Pos: $_ Sum to p-1: $sum[$_ - 1] Sum to p: $sum[$_]\\n\";\n $c += $map{$sum[$_ - 1]} if exists $map{$sum[$_ - 1]};\n $map{$sum[$_]}++;\n }\n}\n\nprintf \"$c\\n\";\n"}, {"source_code": "use List::Util qw(first max maxstr min minstr reduce shuffle sum);\nuse English;\n\nchomp (my @n = split /\\s+/, );\nchomp (my @str = split /\\s*/, );\n\nmy %wt;\nforeach ('a' .. 'z') {\n $wt{$_} = shift @n;\n}\n\nsub calc {\n my $s = 0;\n $s += $wt{$_} foreach (@_);\n return $s;\n}\n\nforeach my $x (0 .. $#str - 1) {\n foreach my $y ($x + 1 .. $#str) {\n printf \"%s\\n\", join \"\", @str[$x .. $y] if (\n ($str[$x] eq $str[$y]) &&\n (&calc(@str[$x+1 .. $y-1]) == 0)\n );\n }\n}\n"}], "src_uid": "0dd2758f432542fa82ff949d19496346"} {"nl": {"description": "Kolya is going to make fresh orange juice. He has n oranges of sizes a1,\u2009a2,\u2009...,\u2009an. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?", "input_spec": "The first line of the input contains three integers n, b and d (1\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000, 1\u2009\u2264\u2009b\u2009\u2264\u2009d\u2009\u2264\u20091\u2009000\u2009000)\u00a0\u2014 the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20091\u2009000\u2009000)\u00a0\u2014 sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.", "output_spec": "Print one integer\u00a0\u2014 the number of times Kolya will have to empty the waste section.", "sample_inputs": ["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"], "sample_outputs": ["1", "0", "1", "0"], "notes": "NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all."}, "positive_code": [{"source_code": "# cf709a\nmy (undef,$b,$d)=split(' ',<>);\nmy @ws=split(' ',<>);\nmy $c=0;\nmy $in=0;\nforeach my $w (@ws) {\n if($w>$b) {\n next;\n }\n $in+=$w;\n if($in>$d) {\n $in=0;\n ++$c;\n }\n}\nprint $c;\n"}], "negative_code": [], "src_uid": "06e9649963715e56d97297c6104fbc00"} {"nl": {"description": "You are living on an infinite plane with the Cartesian coordinate system on it. In one move you can go to any of the four adjacent points (left, right, up, down).More formally, if you are standing at the point $$$(x, y)$$$, you can: go left, and move to $$$(x - 1, y)$$$, or go right, and move to $$$(x + 1, y)$$$, or go up, and move to $$$(x, y + 1)$$$, or go down, and move to $$$(x, y - 1)$$$. There are $$$n$$$ boxes on this plane. The $$$i$$$-th box has coordinates $$$(x_i,y_i)$$$. It is guaranteed that the boxes are either on the $$$x$$$-axis or the $$$y$$$-axis. That is, either $$$x_i=0$$$ or $$$y_i=0$$$.You can collect a box if you and the box are at the same point. Find the minimum number of moves you have to perform to collect all of these boxes if you have to start and finish at the point $$$(0,0)$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) \u2014 the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$) \u2014 the number of boxes. The $$$i$$$-th line of the following $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-100 \\le x_i, y_i \\le 100$$$) \u2014 the coordinate of the $$$i$$$-th box. It is guaranteed that either $$$x_i=0$$$ or $$$y_i=0$$$. Do note that the sum of $$$n$$$ over all test cases is not bounded.", "output_spec": "For each test case output a single integer \u2014 the minimum number of moves required.", "sample_inputs": ["3\n\n4\n\n0 -2\n\n1 0\n\n-1 0\n\n0 2\n\n3\n\n0 2\n\n-3 0\n\n0 -1\n\n1\n\n0 0"], "sample_outputs": ["12\n12\n0"], "notes": "NoteIn the first test case, a possible sequence of moves that uses the minimum number of moves required is shown below. $$$$$$(0,0) \\to (1,0) \\to (1,1) \\to (1, 2) \\to (0,2) \\to (-1,2) \\to (-1,1) \\to (-1,0) \\to (-1,-1) \\to (-1,-2) \\to (0,-2) \\to (0,-1) \\to (0,0)$$$$$$ In the second test case, a possible sequence of moves that uses the minimum number of moves required is shown below. $$$$$$(0,0) \\to (0,1) \\to (0,2) \\to (-1, 2) \\to (-2,2) \\to (-3,2) \\to (-3,1) \\to (-3,0) \\to (-3,-1) \\to (-2,-1) \\to (-1,-1) \\to (0,-1) \\to (0,0)$$$$$$ In the third test case, we can collect all boxes without making any moves."}, "positive_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t@_ = map ~~<>, 1 .. $_;\r\n\t\r\n\tprint 2 * eval join '+', f( 0, @_ ), f( 1, @_ );\r\n\t}\r\n\r\nsub f {\r\n\tmy( $i ) = shift @_;\r\n\t\r\n\tmap abs, ( sort { $a <=> $b } 0, map { ( split )[ $i ] } @_ )[ 0, ~~ @_ ];\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy @x;\n\tmy @y;\n\tmap { my( $x, $y ) = split ' ', <>; push @x, $x; push @y, $y; } 1 .. $_;\n\t\n\t@x = sort { $a <=> $b } @x, 0;\n\t@y = sort { $a <=> $b } @y, 0;\n\t\n\tmy $sum = 0;\n\t\n\tmap $sum += abs, @x[ 0, @x - 1 ], @y[ 0, @y - 1 ];\n\t\n\tprint $sum * 2;\n\t}"}], "negative_code": [], "src_uid": "4af206ae108a483bdb643dc24e4bedba"} {"nl": {"description": "You are given an integer $$$n$$$.Let's define $$$s(n)$$$ as the string \"BAN\" concatenated $$$n$$$ times. For example, $$$s(1)$$$ = \"BAN\", $$$s(3)$$$ = \"BANBANBAN\". Note that the length of the string $$$s(n)$$$ is equal to $$$3n$$$.Consider $$$s(n)$$$. You can perform the following operation on $$$s(n)$$$ any number of times (possibly zero): Select any two distinct indices $$$i$$$ and $$$j$$$ $$$(1 \\leq i, j \\leq 3n, i \\ne j)$$$. Then, swap $$$s(n)_i$$$ and $$$s(n)_j$$$. You want the string \"BAN\" to not appear in $$$s(n)$$$ as a subsequence. What's the smallest number of operations you have to do to achieve this? Also, find one such shortest sequence of operations.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 input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 100)$$$ \u00a0\u2014 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 100)$$$.", "output_spec": "For each test case, in the first line output $$$m$$$ ($$$0 \\le m \\le 10^5$$$)\u00a0\u2014 the minimum number of operations required. It's guaranteed that the objective is always achievable in at most $$$10^5$$$ operations under the constraints of the problem. Then, output $$$m$$$ lines. The $$$k$$$-th of these lines should contain two integers $$$i_k$$$, $$$j_k$$$ $$$(1\\leq i_k, j_k \\leq 3n, i_k \\ne j_k)$$$ denoting that you want to swap characters at indices $$$i_k$$$ and $$$j_k$$$ at the $$$k$$$-th operation. After all $$$m$$$ operations, \"BAN\" must not appear in $$$s(n)$$$ as a subsequence. If there are multiple possible answers, output any.", "sample_inputs": ["2\n1\n2"], "sample_outputs": ["1\n1 2\n1\n2 6"], "notes": "NoteIn the first testcase, $$$s(1) = $$$ \"BAN\", we can swap $$$s(1)_1$$$ and $$$s(1)_2$$$, converting $$$s(1)$$$ to \"ABN\", which does not contain \"BAN\" as a subsequence.In the second testcase, $$$s(2) = $$$ \"BANBAN\", we can swap $$$s(2)_2$$$ and $$$s(2)_6$$$, converting $$$s(2)$$$ to \"BNNBAA\", which does not contain \"BAN\" as a subsequence."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy $A = \"BAN\" x $_;\n\t\n\tmy $B = \"N\" x $_ . \"A\" x $_;\n\t$B =~ s/(..)/B$1/g;\n\t\n\tmy @S;\n\tmy @F;\n\t\n\tfor my $i ( 0 .. $_ * 3 - 1 ){\n\t\tif( \"A\" eq substr $A, $i, 1 and \"N\" eq substr $B, $i, 1 ){\n\t\t\tpush @S, $i + 1;\n\t\t\t}\n\t\tif( \"N\" eq substr $A, $i, 1 and \"A\" eq substr $B, $i, 1 ){\n\t\t\tpush @F, $i + 1;\n\t\t\t}\n\t\t}\n\t\n\t@_ = ();\n\t\n\twhile( @S ){\n\t\tpush @_, join ' ', shift @S, shift @F;\n\t\t}\n\t\n\tprint for 0 + @_, @_;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy $i = int $_ / 2;\n\t\n\tmy $start = 0;\n\t\n\tmy $f = 2;\n\tmy $s = 6;\n\t\n\t@_ = ();\n\t\n\tfor my $j ( 0 .. $i - 1 ){\n\t\tpush @_, $f + $j * 6, $s + $j * 6;\n\t\t}\n\t\n\tif( $_ % 2 == 1 ){\n\t\tpush @_, 2 + $i * 6, 3 + $i * 6;\n\t\t}\n\t\n\tprint @_ / 2;\n\tprint \"@_\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy $i = int $_ / 2;\n\t\n\tmy $start = 0;\n\t\n\tmy $f = 2;\n\tmy $s = 6;\n\t\n\t@_ = ();\n\t\n\tfor my $j ( 0 .. $i - 1 ){\n\t\tpush @_, $f + $j * 6, $s + $j * 6;\n\t\t}\n\t\n\tif( $_ % 2 == 1 ){\n\t\tpush @_, 2 + $i * 6, 3 + $i * 6;\n\t\t}\n\t\n\tprint \"@_\";\n\t}"}], "src_uid": "aeea2ca73f1b5c5b86fb508afcbec68a"} {"nl": {"description": "PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses.You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, if both play optimally?", "input_spec": "The first input line contains two integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009103)\u00a0\u2014 number of words PolandBall and EnemyBall know, respectively. Then n strings follow, one per line\u00a0\u2014 words familiar to PolandBall. Then m strings follow, one per line\u00a0\u2014 words familiar to EnemyBall. Note that one Ball cannot know a word more than once (strings are unique), but some words can be known by both players. Each word is non-empty and consists of no more than 500 lowercase English alphabet letters.", "output_spec": "In a single line of print the answer\u00a0\u2014 \"YES\" if PolandBall wins and \"NO\" otherwise. Both Balls play optimally.", "sample_inputs": ["5 1\npolandball\nis\na\ncool\ncharacter\nnope", "2 2\nkremowka\nwadowicka\nkremowka\nwiedenska", "1 2\na\na\nb"], "sample_outputs": ["YES", "YES", "NO"], "notes": "NoteIn the first example PolandBall knows much more words and wins effortlessly.In the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only say wiedenska. PolandBall says wadowicka and wins."}, "positive_code": [{"source_code": "( $n, $m ) = split ' ', <>;\n$S += $h{$_} ++ for <>;\nprint qw(NO YES)[ $n + $S % 2 > $m ]"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\tmy @n = map ~~<>, 1 .. $n;\n\tchomp @n;\n\tmy %n = map { $_, undef } @n;\n\n\tmy @m = map ~~<>, 1 .. $m;\n\tchomp @m;\n\tmy %m = map { $_, undef } @m;\n\t\n\tmy $i = 0;\n\t\n\tfor (@m){\n\t\texists $n{$_} or next;\n\t\t$i ++;\n\t\tif( $i % 2 ){\n\t\t\tdelete $m{$_}\n\t\t\t}\n\t\telse {\n\t\t\tdelete $n{$_}\n\t\t\t}\n\t\t}\n\t\n\tprint qw(NO YES)[ (~~ keys %n) > (~~ keys %m) ];\n\t}"}, {"source_code": "( $n, $m ) = split ' ', <>;\n\t\n$S += $h{$_} ++ for <>;\n\t\nprint qw(NO YES)[ $n + $S % 2 > $m ]"}], "negative_code": [], "src_uid": "4b352854008a9378551db60b76d33cfa"} {"nl": {"description": "After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer!Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher gave them 3 strings and asked them to concatenate them. Concatenating strings means to put them in some arbitrary order one after the other. For example from concatenating Alireza and Amir we can get to AlirezaAmir or AmirAlireza depending on the order of concatenation.Unfortunately enough, the teacher forgot to ask students to concatenate their strings in a pre-defined order so each student did it the way he/she liked.Now the teacher knows that Shapur is such a fast-calculating genius boy and asks him to correct the students' papers.Shapur is not good at doing such a time-taking task. He rather likes to finish up with it as soon as possible and take his time to solve 3-SAT in polynomial time. Moreover, the teacher has given some advice that Shapur has to follow. Here's what the teacher said: As I expect you know, the strings I gave to my students (including you) contained only lowercase and uppercase Persian Mikhi-Script letters. These letters are too much like Latin letters, so to make your task much harder I converted all the initial strings and all of the students' answers to Latin. As latin alphabet has much less characters than Mikhi-Script, I added three odd-looking characters to the answers, these include \"-\", \";\" and \"_\". These characters are my own invention of course! And I call them Signs. The length of all initial strings was less than or equal to 100 and the lengths of my students' answers are less than or equal to 600 My son, not all students are genius as you are. It is quite possible that they make minor mistakes changing case of some characters. For example they may write ALiReZaAmIR instead of AlirezaAmir. Don't be picky and ignore these mistakes. Those signs which I previously talked to you about are not important. You can ignore them, since many students are in the mood for adding extra signs or forgetting about a sign. So something like Iran;;-- is the same as --;IRAN You should indicate for any of my students if his answer was right or wrong. Do this by writing \"WA\" for Wrong answer or \"ACC\" for a correct answer. I should remind you that none of the strings (initial strings or answers) are empty. Finally, do these as soon as possible. You have less than 2 hours to complete this. ", "input_spec": "The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs (\"-\", \";\" and \"_\"). All the initial strings have length from 1 to 100, inclusively. In the fourth line there is a single integer n (0\u2009\u2264\u2009n\u2009\u2264\u20091000), the number of students. Next n lines contain a student's answer each. It is guaranteed that the answer meets what the teacher said. Each answer iconsists only of lowercase and uppercase Latin letters and signs (\"-\", \";\" and \"_\"). Length is from 1 to 600, inclusively.", "output_spec": "For each student write in a different line. Print \"WA\" if his answer is wrong or \"ACC\" if his answer is OK.", "sample_inputs": ["Iran_\nPersian;\nW_o;n;d;e;r;f;u;l;\n7\nWonderfulPersianIran\nwonderful_PersIAN_IRAN;;_\nWONDERFUL___IRAN__PERSIAN__;;\nIra__Persiann__Wonderful\nWonder;;fulPersian___;I;r;a;n;\n__________IranPersianWonderful__________\nPersianIran_is_Wonderful", "Shapur;;\nis___\na_genius\n3\nShapur__a_is___geniUs\nis___shapur___a__Genius;\nShapur;;is;;a;;geni;;us;;"], "sample_outputs": ["ACC\nACC\nACC\nWA\nACC\nACC\nWA", "WA\nACC\nACC"], "notes": null}, "positive_code": [{"source_code": "$a=<>;\n$b=<>;\n$c=<>;\nchomp($a,$b,$c);\n$a=~s/[_;-]//g;\n$b=~s/[_;-]//g;\n$c=~s/[_;-]//g;\n<>;\nwhile(<>){\n s/[_;-]//g;\n printf \"%s\\n\", /$a$b$c|$a$c$b|$b$a$c|$b$c$a|$c$a$b|$c$b$a/i?\"ACC\":\"WA\";\n}\n"}, {"source_code": "$_=lc<>.<>.<>;\ns/[^a-z\\n]//gm;\n@s = split/\\n/;\npush @t, $s[0].$s[1].$s[2], $s[0].$s[2].$s[1], $s[1].$s[0].$s[2], \n$s[1].$s[2].$s[0], $s[2].$s[1].$s[0], $s[2].$s[0].$s[1];\n<>;\n@_=<>;\nfor (@_){\n\t$_=lc;\n\ts/[^a-z]//ig;\n\t}\nfor (@_){\n\t$F=0;\n\tfor $i(@t){\n\t\t$i eq $_ and $F++;\n\t\t}\n\t# /[^-\\n]/ and $F++;\n\tprint !$F?\"WA\\n\":\"ACC\\n\";\n\t}"}], "negative_code": [{"source_code": "$_=lc<>.<>.<>;\ns/[^a-z\\n]//gm;\n@s = split/\\n/;\n<>;\n@_=<>;\nfor (@_){\n\t$_=lc;\n\ts/[^a-z\\n]//ig;\n\t}\nfor (@_){\n\t$F=0;\n\tfor $i(@s){\n\t\ts/$i/-/ or $F++;\n\t\t}\n\t/[^-\\n]/ and $F++;\n\tprint $F?\"WA\\n\":\"ACC\\n\";\n\t}"}], "src_uid": "95ccc87fe9e431f9c6eeffeaf049f797"} {"nl": {"description": "Bash has set out on a journey to become the greatest Pokemon master. To get his first Pokemon, he went to Professor Zulu's Lab. Since Bash is Professor Zulu's favourite student, Zulu allows him to take as many Pokemon from his lab as he pleases.But Zulu warns him that a group of k\u2009>\u20091 Pokemon with strengths {s1,\u2009s2,\u2009s3,\u2009...,\u2009sk} tend to fight among each other if gcd(s1,\u2009s2,\u2009s3,\u2009...,\u2009sk)\u2009=\u20091 (see notes for gcd definition).Bash, being smart, does not want his Pokemon to fight among each other. However, he also wants to maximize the number of Pokemon he takes from the lab. Can you help Bash find out the maximum number of Pokemon he can take? Note: A Pokemon cannot fight with itself.", "input_spec": "The input consists of two lines. The first line contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105), the number of Pokemon in the lab. The next line contains n space separated integers, where the i-th of them denotes si (1\u2009\u2264\u2009si\u2009\u2264\u2009105), the strength of the i-th Pokemon.", "output_spec": "Print single integer\u00a0\u2014 the maximum number of Pokemons Bash can take.", "sample_inputs": ["3\n2 3 4", "5\n2 3 4 6 7"], "sample_outputs": ["2", "3"], "notes": "Notegcd (greatest common divisor) of positive integers set {a1,\u2009a2,\u2009...,\u2009an} is the maximum positive integer that divides all the integers {a1,\u2009a2,\u2009...,\u2009an}.In the first sample, we can take Pokemons with strengths {2,\u20094} since gcd(2,\u20094)\u2009=\u20092.In the second sample, we can take Pokemons with strengths {2,\u20094,\u20096}, and there is no larger group with gcd\u2009\u2260\u20091."}, "positive_code": [{"source_code": "use v5.20;\nuse warnings;\n\nsub max {\n\treturn $_[0] > $_[1] ? $_[0] : $_[1];\n}\n\nmy @fr = (0) x 100001;\nmy @prime = (1) x 100001;\n\nmy $n = int (<>);\nforeach my $it (split(' ',<>)) {\n\t$fr[$it] += 1;\n}\n\nmy $sol = 1;\n\nfor my $i (2..100000) {\n\tif ($prime[$i]) {\n\t\tmy $curr = $fr[$i];\n\t\tfor (my $j = $i * 2; $j <= 100000; $j += $i) {\n\t\t\t$prime[$j] = 0;\n\t\t\t$curr += $fr[$j];\n\t\t}\n\t\t$sol = max($sol,$curr);\n\t}\n}\n\nsay $sol;\n\n"}, {"source_code": "use warnings;\n\nsub max {\n\tif($_[0] >= $_[1]) {\n\t\treturn $_[0];\n\t}\n\treturn $_[1];\n}\n\nmy $NMAX = 100005;\nmy @fr = (0) x 100005;\nmy $n = int (<>);\nforeach my $it (split(' ',<>)) {\n\t++$fr[$it];\n}\n\nmy $ans = 1;\nfor(my $i=2; $i<=$NMAX; ++$i) {\n\tmy $aux=0;\n\t\n\tfor(my $j=$i; $j<$NMAX; $j+=$i) {\n\t\t$aux+=$fr[$j];\n\t}\n\t\n\t$ans=max($ans,$aux);\n}\n\nprintf(\"%d\", $ans);\n"}, {"source_code": "my $poke_nr = <>;\nmy %pokemons;\nmy @numbers = split(\" \", <>);\nmy $biggest_poke = 0;\nmy $max_poke = 0;\nmy $current_max = 0;\nforeach(@numbers)\n{\n\tif(exists($pokemons{$_}))\n\t{\n\t\t$pokemons{$_} = $pokemons{$_} + 1;\n\t}\n\telse\n\t{\n\t\t$pokemons{$_} = 1;\n\t\tif($_ > $biggest_poke)\n\t\t{\n\t\t\t$biggest_poke = $_;\n\t\t}\n\t}\n}\nforeach my $i(2 .. $biggest_poke)\n{\n\t$current_max = 0;\n\tfor(my $j = $i; $j <= $biggest_poke; $j += $i)\n\t{\n\t\tif(exists($pokemons{$j}))\n\t\t{\n\t\t\t$current_max += $pokemons{$j};\n\t\t}\n\t}\n\tif($current_max > $max_poke)\n\t{\n\t\t$max_poke = $current_max;\n\t}\n}\nif($max_poke > 0)\n{\n\tprint $max_poke;\n}\nelse\n{\n\tif(exists($pokemons{1}))\n\t{\n\t\tprint 1;\n\t}\n\telse\n\t{\n\t\tprint 0;\n\t}\n\n}\n"}], "negative_code": [{"source_code": "use warnings;\n\nsub max {\n\tif($_[0] >= $_[1]) {\n\t\treturn $_[0];\n\t}\n\treturn $_[1];\n}\n\nmy $NMAX = 100005;\nmy @fr = (0) x 100005;\nmy $n = int (<>);\nforeach my $it (split(' ',<>)) {\n\t++$fr[$it];\n}\n\nmy $ans = 0;\nfor(my $i=2; $i<=$NMAX; ++$i) {\n\tmy $aux=0;\n\t\n\tfor(my $j=$i; $j<$NMAX; $j+=$i) {\n\t\t$aux+=$fr[$j];\n\t}\n\t\n\t$ans=max($ans,$aux);\n}\n\nprintf(\"%d\", $ans);\n"}, {"source_code": "my $poke_nr = <>;\nmy %pokemons;\nmy @numbers = split(\" \", <>);\nmy $biggest_poke = 0;\nmy $max_poke = 0;\nmy $current_max = 0;\nforeach(@numbers)\n{\n\tif(exists($pokemons{$_}))\n\t{\n\t\t$pokemons{$_} = $pokemons{$_} + 1;\n\t}\n\telse\n\t{\n\t\t$pokemons{$_} = 1;\n\t\tif($_ > $biggest_poke)\n\t\t{\n\t\t\t$biggest_poke = $_;\n\t\t}\n\t}\n}\nforeach my $i(1 .. $biggest_poke)\n{\n\t$current_max = 0;\n\tfor(my $j = $i; $j <= $biggest_poke; $j += $i)\n\t{\n\t\tif(exists($pokemons{$j}))\n\t\t{\n\t\t\t$current_max += $pokemons{$j};\n\t\t}\n\t}\n\tif($current_max > $max_poke)\n\t{\n\t\t$max_poke = $current_max;\n\t}\n}\nprint $max_poke;\n"}, {"source_code": "my $poke_nr = <>;\nmy %pokemons;\nmy @numbers = split(\" \", <>);\nmy $biggest_poke = 0;\nmy $max_poke = 0;\nmy $current_max = 0;\nforeach(@numbers)\n{\n\tif(exists($pokemons{$_}))\n\t{\n\t\t$pokemons{$_} = $pokemons{$_} + 1;\n\t}\n\telse\n\t{\n\t\t$pokemons{$_} = 1;\n\t\tif($_ > $biggest_poke)\n\t\t{\n\t\t\t$biggest_poke = $_;\n\t\t}\n\t}\n}\nforeach my $i(2 .. $biggest_poke)\n{\n\t$current_max = 0;\n\tfor(my $j = $i; $j <= $biggest_poke; $j += $i)\n\t{\n\t\tif(exists($pokemons{$j}))\n\t\t{\n\t\t\t$current_max += $pokemons{$j};\n\t\t}\n\t}\n\tif($current_max > $max_poke)\n\t{\n\t\t$max_poke = $current_max;\n\t}\n}\nprint $max_poke;\n"}], "src_uid": "eea7860e6bbbe5f399b9123ebd663e3e"} {"nl": {"description": "The School \u21160 of the capital of Berland has n children studying in it. All the children in this school are gifted: some of them are good at programming, some are good at maths, others are good at PE (Physical Education). Hence, for each child we know value ti: ti\u2009=\u20091, if the i-th child is good at programming, ti\u2009=\u20092, if the i-th child is good at maths, ti\u2009=\u20093, if the i-th child is good at PE Each child happens to be good at exactly one of these three subjects.The Team Scientific Decathlon Olympias requires teams of three students. The school teachers decided that the teams will be composed of three children that are good at different subjects. That is, each team must have one mathematician, one programmer and one sportsman. Of course, each child can be a member of no more than one team.What is the maximum number of teams that the school will be able to present at the Olympiad? How should the teams be formed for that?", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u20095000) \u2014 the number of children in the school. The second line contains n integers t1,\u2009t2,\u2009...,\u2009tn (1\u2009\u2264\u2009ti\u2009\u2264\u20093), where ti describes the skill of the i-th child.", "output_spec": "In the first line output integer w \u2014 the largest possible number of teams. Then print w lines, containing three numbers in each line. Each triple represents the indexes of the children forming the team. You can print both the teams, and the numbers in the triplets in any order. The children are numbered from 1 to n in the order of their appearance in the input. Each child must participate in no more than one team. If there are several solutions, print any of them. If no teams can be compiled, print the only line with value w equal to 0.", "sample_inputs": ["7\n1 3 1 3 2 1 2", "4\n2 1 1 2"], "sample_outputs": ["2\n3 5 2\n6 7 4", "0"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nsub min{\n return (sort {$a <=> $b} @_)[0];\n}\nsub len{\n return scalar @{$_[0]};\n}\n\nmy $n = <>;\nmy @list = split ' ',<>;\nmy @arr;\npush @arr,[] for 0..3;\nfor my $i(0..$n-1){\n my $val = $list[$i];\n push @{$arr[$val]}, $i+1;\n}\nmy $min = min(len($arr[1]), len($arr[2]), len($arr[3]));\nprint $min, \"\\n\";\nfor my $i(0..$min-1){\n print \"$arr[1][$i] $arr[2][$i] $arr[3][$i]\\n\";\n}\n"}, {"source_code": "<>;\n$_=<>;\ns/\\s//g;\nwhile(/1/g){push @a, pos}\nwhile(/2/g){push @b, pos}\nwhile(/3/g){push @c, pos}\n$min = (sort {$a <=> $b} (0+@a, 0+@b, 0+@c) )[0];\nprint $min, $/;\nwhile($min--){\n\tprintf \"%d %d %d\\n\", shift @a, shift @b, shift @c\n\t}"}, {"source_code": "my $n = <>;\nmy @arr = ([],[],[],[]);\nmy @inp = split \" \", <>;\nfor (0..$#inp) {\n push @{$arr[$inp[$_]]}, $_+1;\n}\nmy $ans = (sort{$a <=>$b}(scalar @{$arr[1]}, scalar @{$arr[2]}, scalar @{$arr[3]}))[0];\nprint $ans . \"\\n\";\n\nfor (0..$ans-1) {\n print \"@{$arr[1]}[$_] @{$arr[2]}[$_] @{$arr[3]}[$_]\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nsub min {\n\tmy ($x, $y) = (shift, shift);\n\treturn $x<$y ? $x:$y;\n}\n\nchomp($n = <>);\nour @N = (0) x 4;\nour @arr = ((1..$n)) x 4;\nour $id = 1;\nforeach my $i (split / /, <>) {\n\tpush @{$arr[$i]}, $id;\n\t++$N[$i];\n\t++$id;\n\t\n\t# say $i, \" \", $N[$i];\n\t# foreach my $j (0..$N[$i]-1) {\n\t\t# print $arr[$i]->[$j],\" \";\n\t# }\n\t# say \"\\n\";\n\t\n}\n$ans = min(min($N[1], $N[2]), $N[3]);\nsay $ans;\nforeach (0..$ans-1) {\n\tsay $arr[1][$_],\" \", $arr[2][$_],\" \", $arr[3][$_];\n}"}, {"source_code": "use strict;\nuse warnings;\n\n<>;\nmy @kids = split ' ', <>;\nmy @teams;\nmy %kinds;\n\nfor my $i (0..$#kids) {\n push @{$kinds{$kids[$i]}}, $i + 1;\n}\n\nOUTER: for (;;) {\n my @line;\n for (1..3) {\n my $m = shift @{$kinds{$_}};\n last OUTER unless defined $m;\n push @line, $m;\n }\n push @teams, \\@line;\n}\n\n$\\ = \"\\n\";\n$, = ' ';\n\nprint scalar @teams;\nprint @$_ for @teams;\n"}, {"source_code": "#!/usr/bin/perl\n#use warnings;\n#use strict;\n\n<>;\nmy @students = split(' ', <>);\nmy @pro; my @ma; my @pre;\nmy $idx;\nfor my $el(@students){\n\t++$idx;\n\tif($el == 1){push (@pro, $idx);}\n\telsif($el == 2){push (@ma, $idx);}\n\telse{push (@pre, $idx);}\n}\n\n$idx = $#pro;\tif($#ma < $idx){$idx = $#ma;} if($#pre < $idx){$idx = $#pre;} $idx ++;\nprint \"$idx\\n\";\nwhile($#pro >= 0 && $#ma >= 0 && $#pre >= 0){\n\tprint ((pop @pro), \" \", (pop @ma), \" \", (pop @pre), \"\\n\");\n}"}], "negative_code": [{"source_code": "my $n = <>;\nmy @arr;\nmy @inp = split \" \", <>;\nfor (0..$#inp) {\n push @{$arr[$inp[$_]]}, $_+1;\n}\nmy $ans = (sort{$a <=>$b}(scalar @{$arr[1]}, scalar @{$arr[2]}, scalar @{$arr[3]}))[0];\nprint $ans . \"\\n\";\n\nfor (0..$ans-1) {\n print \"@{$arr[1]}[$_] @{$arr[2]}[$_] @{$arr[3]}[$_]\\n\";\n}"}, {"source_code": "#!/usr/bin/perl\n#use warnings;\n#use strict;\n\n<>;\nmy @students = split(' ', <>);\nmy @pro; my @ma; my @pre;\nmy $idx;\nfor my $el(@students){\n\t++$idx;\n\tif($el == 1){push (@pro, $idx);}\n\telsif($el == 2){push (@ma, $idx);}\n\telse{push (@pre, $idx);}\n}\n\nwhile($#pro >= 0 && $#ma >= 0 && $#pre >= 0){\n\tprint ((pop @pro), \" \", (pop @ma), \" \", (pop @pre), \"\\n\");\n}\n\n\t\n"}, {"source_code": "#!/usr/bin/perl\n#use warnings;\n#use strict;\n\n<>;\nmy @students = split(' ', <>);\nmy @pro; my @ma; my @pre;\nmy $idx;\nfor my $el(@students){\n\t++$idx;\n\tif($el == 1){push (@pro, $idx);}\n\telsif($el == 2){push (@ma, $idx);}\n\telse{push (@pre, $idx);}\n}\n$idx = scalar @pro; $idx < scalar @ma ? $idx : $idx = scalar @ma; $idx < scalar @pre ? $idx : $idx = scalar @pre;\nprint \"$idx\\n\";\nwhile($#pro >= 0 && $#ma >= 0 && $#pre >= 0){\n\tprint ((pop @pro), \" \", (pop @ma), \" \", (pop @pre), \"\\n\");\n}"}, {"source_code": "#!/usr/bin/perl\n#use warnings;\n#use strict;\n\n<>;\nmy @students = split(' ', <>);\nmy @pro; my @ma; my @pre;\nmy $idx;\nfor my $el(@students){\n\t++$idx;\n\tif($el == 1){push (@pro, $idx);}\n\telsif($el == 2){push (@ma, $idx);}\n\telse{push (@pre, $idx);}\n}\n$idx = $#pro; $idx < $#ma ? $idx : $idx = $#ma; $idx < $#pre ? $idx : $idx = $#pre; $idx++;\nprint \"$idx\\n\";\nwhile($#pro >= 0 && $#ma >= 0 && $#pre >= 0){\n\tprint ((pop @pro), \" \", (pop @ma), \" \", (pop @pre), \"\\n\");\n}"}], "src_uid": "c014861f27edf35990cc065399697b10"} {"nl": {"description": "A tree is a connected undirected graph consisting of n vertices and n\u2009\u2009-\u2009\u20091 edges. Vertices are numbered 1 through n.Limak is a little polar bear and Radewoosh is his evil enemy. Limak once had a tree but Radewoosh stolen it. Bear is very sad now because he doesn't remember much about the tree\u00a0\u2014 he can tell you only three values n, d and h: The tree had exactly n vertices. The tree had diameter d. In other words, d was the biggest distance between two vertices. Limak also remembers that he once rooted the tree in vertex 1 and after that its height was h. In other words, h was the biggest distance between vertex 1 and some other vertex. The distance between two vertices of the tree is the number of edges on the simple path between them.Help Limak to restore his tree. Check whether there exists a tree satisfying the given conditions. Find any such tree and print its edges in any order. It's also possible that Limak made a mistake and there is no suitable tree\u00a0\u2013 in this case print \"-1\".", "input_spec": "The first line contains three integers n, d and h (2\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000,\u20091\u2009\u2264\u2009h\u2009\u2264\u2009d\u2009\u2264\u2009n\u2009-\u20091)\u00a0\u2014 the number of vertices, diameter, and height after rooting in vertex 1, respectively.", "output_spec": "If there is no tree matching what Limak remembers, print the only line with \"-1\" (without the quotes). Otherwise, describe any tree matching Limak's description. Print n\u2009-\u20091 lines, each with two space-separated integers\u00a0\u2013 indices of vertices connected by an edge. If there are many valid trees, print any of them. You can print edges in any order.", "sample_inputs": ["5 3 2", "8 5 2", "8 4 2"], "sample_outputs": ["1 2\n1 3\n3 4\n3 5", "-1", "4 8\n5 7\n2 3\n8 1\n2 1\n5 6\n1 5"], "notes": "NoteBelow you can see trees printed to the output in the first sample and the third sample. "}, "positive_code": [{"source_code": "while(<>){\n\t($n, $d, $h) = split;\n\n\tprint $d > $h * 2 ? -1 : $h == 1 ?\n\t\tdo {\n\t\t\t$n == 2 ? '1 2' :\n\t\t\t\t$d != 2 ? -1 :\n\t\t\t\t\tjoin \"\\n\", map \"1 $_\", 2 .. $n;\n\t\t\t}\n\t\t:\n\t\tdo {\n\t\t\t$i = 1;\n\t\t\tjoin \"\\n\", grep / /,\n\t\t\t\t( map { \"$i \" . ++ $i } 1 .. $h ),\n\t\t\t\t( $d > $h ? \"1 \" . ++ $i : \"\" ),\n\t\t\t\t( map { \"$i \" . ++ $i } 1 .. $d - $h - 1 ),\n\t\t\t\t( map { \"2 \" . ++ $i } 1 .. $n - $d - 1 );\n\t\t\t}\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $d, $h) = split;\n\t@A = ();\n\t\n\tif ($h == 1) {\n\t\tif ($n == 2){\n\t\t\tprint '1 2'; next\n\t\t\t}\n\t\telsif ($n > 2){\n\t\t\tif ($d != 2){\n\t\t\t\tprint -1; next\n\t\t\t\t}\n\t\t\telse {\n\t\t\t\tprint join \"\\n\", map { 1 . ' ' . ($_ + 1) } 1 .. $n - 1;\n\t\t\t\tnext\n\t\t\t\t}\n\t\t\t} \n\t\t}\n\t\t\n\t$i = 1;\n\tfor (1 .. $h){\n\t\tpush @A, $i . ' ' . ($i + 1);\n\t\t$i++\n\t\t}\n\tif ($d > $h){\n\t\tpush @A, 1 . ' ' . ++$i;\n\t\t}\n\tfor (1 .. $d - $h - 1){\n\t\tpush @A, $i . ' ' . ($i + 1);\n\t\t$i++\n\t\t}\n\tfor (1 .. $n - $d - 1){\n\t\tpush @A, 2 . ' ' . ++$i\n\t\t}\n\t\n\tprint $d > $h * 2 ? -1 : join \"\\n\", @A\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $d, $h) = split;\n\t@A = ();\n\t\n\t$i = 1;\n\tfor (1 .. $h){\n\t\tpush @A, $i . ' ' . ($i + 1);\n\t\t$i++\n\t\t}\n\tif ($d > $h){\n\t\tpush @A, 1 . ' ' . ++$i;\n\t\t}\n\tfor (1 .. $d - $h - 1){\n\t\tpush @A, $i . ' ' . ($i + 1);\n\t\t$i++\n\t\t}\n\tfor (1 .. $n - $d - 1){\n\t\tpush @A, 1 . ' ' . ++$i\n\t\t}\n\t\n\tprint $d > $h * 2 ? -1 : join \"\\n\", @A\n\t}"}, {"source_code": "$\\ = $/;\n\nwhile(<>){\n\t($n, $d, $h) = split;\n\t@A = ();\n\t\n\tif ($h == 1) {\n\t\tif ($n == 2){\n\t\t\tprint '1 2'; next\n\t\t\t}\n\t\telsif ($n > 2){\n\t\t\tif ($d != 2){\n\t\t\t\tprint -1; next\n\t\t\t\t}\n\t\t\t} \n\t\t}\n\t\t\n\t$i = 1;\n\tfor (1 .. $h){\n\t\tpush @A, $i . ' ' . ($i + 1);\n\t\t$i++\n\t\t}\n\tif ($d > $h){\n\t\tpush @A, 1 . ' ' . ++$i;\n\t\t}\n\tfor (1 .. $d - $h - 1){\n\t\tpush @A, $i . ' ' . ($i + 1);\n\t\t$i++\n\t\t}\n\tfor (1 .. $n - $d - 1){\n\t\tpush @A, 2 . ' ' . ++$i\n\t\t}\n\t\n\tprint $d > $h * 2 ? -1 : join \"\\n\", @A\n\t}"}], "src_uid": "32096eff277f19d227bccca1b6afc458"} {"nl": {"description": "Yura is tasked to build a closed fence in shape of an arbitrary non-degenerate simple quadrilateral. He's already got three straight fence segments with known lengths $$$a$$$, $$$b$$$, and $$$c$$$. Now he needs to find out some possible integer length $$$d$$$ of the fourth straight fence segment so that he can build the fence using these four segments. In other words, the fence should have a quadrilateral shape with side lengths equal to $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$. Help Yura, find any possible length of the fourth side.A non-degenerate simple quadrilateral is such a quadrilateral that no three of its corners lie on the same line, and it does not cross itself.", "input_spec": "The first line contains a single integer $$$t$$$\u00a0\u2014 the number of test cases ($$$1 \\le t \\le 1000$$$). The next $$$t$$$ lines describe the test cases. Each line contains three integers $$$a$$$, $$$b$$$, and $$$c$$$\u00a0\u2014 the lengths of the three fence segments ($$$1 \\le a, b, c \\le 10^9$$$).", "output_spec": "For each test case print a single integer $$$d$$$\u00a0\u2014 the length of the fourth fence segment that is suitable for building the fence. If there are multiple answers, print any. We can show that an answer always exists.", "sample_inputs": ["2\n1 2 3\n12 34 56"], "sample_outputs": ["4\n42"], "notes": "NoteWe can build a quadrilateral with sides $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$.We can build a quadrilateral with sides $$$12$$$, $$$34$$$, $$$56$$$, $$$42$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = sort { $b <=> $a } split;\n\t\n\tprint $_[ 0 ];\n\t}"}], "negative_code": [], "src_uid": "40d679f53417ba058144c745e7a2c76d"} {"nl": {"description": "And where the are the phone numbers?You are given a string s consisting of lowercase English letters and an integer k. Find the lexicographically smallest string t of length k, such that its set of letters is a subset of the set of letters of s and s is lexicographically smaller than t.It's guaranteed that the answer exists.Note that the set of letters is a set, not a multiset. For example, the set of letters of abadaba is {a,\u2009b,\u2009d}.String p is lexicographically smaller than string q, if p is a prefix of q, is not equal to q or there exists i, such that pi\u2009<\u2009qi and for all j\u2009<\u2009i it is satisfied that pj\u2009=\u2009qj. For example, abc is lexicographically smaller than abcd , abd is lexicographically smaller than abec, afa is not lexicographically smaller than ab and a is not lexicographically smaller than a.", "input_spec": "The first line of input contains two space separated integers n and k (1\u2009\u2264\u2009n,\u2009k\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the length of s and the required length of t. The second line of input contains the string s consisting of n lowercase English letters.", "output_spec": "Output the string t conforming to the requirements above. It's guaranteed that the answer exists.", "sample_inputs": ["3 3\nabc", "3 2\nabc", "3 3\nayy", "2 3\nba"], "sample_outputs": ["aca", "ac", "yaa", "baa"], "notes": "NoteIn the first example the list of strings t of length 3, such that the set of letters of t is a subset of letters of s is as follows: aaa, aab, aac, aba, abb, abc, aca, acb, .... Among them, those are lexicographically greater than abc: aca, acb, .... Out of those the lexicographically smallest is aca."}, "positive_code": [{"source_code": "( $n, $k ) = split ' ', <>;\n\n$_ = <>, chomp;\n@_ = split //;\n\nmap $h{ $_ } ++, @_;\n\n@az = sort keys %h;\n\n( $min, $max ) = @az[ 0, -1 ];\n\nif( $k > $n ){\n\tprint $_ . $min x ( $k - $n );\n\t}\nelse{\n\tfor $i ( reverse 0 .. $k - 1 ){\n\t\tif( $_[ $i ] ne $max ){\n\t\t\tprint @_[ 0 .. $i - 1 ],\n\t\t\t\t( grep $_ gt $_[ $i ], @az )[ 0 ],\n\t\t\t\t( $min ) x ( $k - $i - 1 );\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t@_ = split //;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\tmy @az = sort keys %h;\n\t\n\tmy( $min, $max ) = @az[ 0, -1 ];\n\t\n\tmy $az = join '', @az;\n\t\n\t$debug and print \"[$min]\";\n\t\n\tif( $k > $n ){\n\t\t$debug and print '>';\n\t\tprint $_ . $min x ( $k - $n );\n\t\t}\n\telse{\n\t\tfor my $i ( reverse 0 .. $k - 1 ){\n\t\t\tif( $_[ $i ] ne $max ){\n\t\t\t\tprint @_[ 0 .. $i - 1 ],\n\t\t\t\t\t( grep $_ gt $_[ $i ], @az )[ 0 ],\n\t\t\t\t\t( $min ) x ( $k - $i - 1 );\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}"}, {"source_code": "( $n, $k ) = split ' ', <>;\n\n$_ = <>, chomp;\n\n@az = sort split //;\n\n( $min, $max ) = @az[ 0, -1 ];\n\t\t\nif( $k > $n ){\n\tprint $_ . $min x ( $k - $n );\n\t}\nelse{\n\t$_ = reverse substr $_, 0, $k;\n\tprint ~~ reverse\n\ts/(.*?)([^$max])/\n\t\t( $min x length $1 ) . \n\t\t( grep $_ gt $2, @az )[ 0 ]\n\t/er;\n\t}"}, {"source_code": "while(<>){\n\t( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t@_ = split //;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, @_;\n\t\n\t@az = sort keys %h;\n\t\n\t( $min, $max ) = @az[ 0, -1 ];\n\t\t\t\n\tif( $k > $n ){\n\t\tprint $_ . $min x ( $k - $n );\n\t\t}\n\telse{\n\t\t$_ = reverse substr $_, 0, $k;\n\t\tprint ~~ reverse\n\t\ts/(.*?)([^$max])/\n\t\t\t( $min x length $1 ) . \n\t\t\t( grep $_ gt $2, @az )[ 0 ]\n\t\t/er;\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\t$_ = <>, chomp;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, split //;\n\t\n\tmy @az = sort keys %h;\n\t\n\tmy( $min, $max ) = @az[ 0, -1 ];\n\t\n\t$debug and print \"[$min|$max]\";\n\t\n\tmy $pos = -1;\n\t\n\t/\n\t\t(?= (.)(.) )\n\t\t(?{\n\t\t\t$1 lt $2 and $pos = pos;\n\t\t})\n\t\t(*FAIL)\n\t/x;\n\t\t\n\tmy $next = 0;\n\tmy $ans = '';\n\t\n\tif( $k > $n ){\n\t\t$debug and print '>';\n\t\tprint $_ . $az[ 0 ] x ( $k - $n );\n\t\t}\n\telse{\n\t\tfor my $lett ( reverse( ( split // )[ 0 .. $k - 1 ] ) ){\n\t\t\t$debug and print $lett;\n\t\t\t$next and do {\n\t\t\t\t$ans .= $lett;\n\t\t\t\tnext;\n\t\t\t\t};\n\t\t\t\t\n\t\t\tif( $lett eq $max ){\n\t\t\t\t$ans .= $min;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$ans .= $max;\n\t\t\t\t$next = 1;\n\t\t\t\t}\n\t\t\t}\n\t\tprint scalar reverse $ans;\n\t\t}\n\t}"}], "src_uid": "ea2dc6309e704d04cfdd6c79161c67f7"} {"nl": {"description": "In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. ", "input_spec": "The first line contains the integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100)\u00a0\u2014 the number of citizens in the kingdom. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an, where ai (0\u2009\u2264\u2009ai\u2009\u2264\u2009106)\u00a0\u2014 the welfare of the i-th citizen.", "output_spec": "In the only line print the integer S\u00a0\u2014 the minimum number of burles which are had to spend.", "sample_inputs": ["5\n0 1 2 3 4", "5\n1 1 0 1 1", "3\n1 3 1", "1\n12"], "sample_outputs": ["10", "1", "4", "0"], "notes": "NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles."}, "positive_code": [{"source_code": "$n = int(<>);\n$sum = 0;\n$ma = 0;\nforeach $i (split(/\\s/, <>)) {\n $ma = $ma > $i ? $ma : $i;\n $sum += $i;\n}\nprint $ma * $n - $sum, \"\\n\";\n"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nsub max {\n my $max = shift;\n local $_;\n\n for (@_) {\n $max = $_ if $_ > $max;\n }\n $max;\n}\n\nsub sum {\n local $_;\n my $sum = 0;\n $sum += $_ for @_;\n $sum;\n}\n\n<>;\nmy @welfare = split ' ', <>;\nmy $max = max(@welfare);\n\nsay sum map { $max - $_ } @welfare;\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n\n# send argument as array, not reference, to function requiring list\n \n# essential\nmy @tokens = ();\n\nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\n# solve\n\nmy $n = read_token;\n\nmy @welfares = split q{ }, read_line;\n\nmy $max = max(@welfares);\n\nmy $answer = 0;\n\nfor (@welfares) {\n $answer += $max - $_;\n}\n\nsay $answer;\n"}, {"source_code": "$n = ;\n$nums = ;\nchomp($nums);\n@a = split(/ /g, $nums);\n@a = sort { $a <=> $b } @a;\n$max = $a[-1];\n$ans = 0;\nfor($i=0;$i<@a;$i++)\n{\n\tif($a[$i] < $max)\n\t{\n\t\t$ans += $max-$a[$i];\n\t}\n}\nprint(\"$ans\\n\");\n"}], "negative_code": [], "src_uid": "a5d3c9ea1c9affb0359d81dae4ecd7c8"} {"nl": {"description": "Let's call an array $$$a_1, a_2, \\dots, a_m$$$ of nonnegative integer numbers good if $$$a_1 + a_2 + \\dots + a_m = 2\\cdot(a_1 \\oplus a_2 \\oplus \\dots \\oplus a_m)$$$, where $$$\\oplus$$$ denotes the bitwise XOR operation.For example, array $$$[1, 2, 3, 6]$$$ is good, as $$$1 + 2 + 3 + 6 = 12 = 2\\cdot 6 = 2\\cdot (1\\oplus 2 \\oplus 3 \\oplus 6)$$$. At the same time, array $$$[1, 2, 1, 3]$$$ isn't good, as $$$1 + 2 + 1 + 3 = 7 \\neq 2\\cdot 1 = 2\\cdot(1\\oplus 2 \\oplus 1 \\oplus 3)$$$.You are given an array of length $$$n$$$: $$$a_1, a_2, \\dots, a_n$$$. Append at most $$$3$$$ elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10\\,000$$$). 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)$$$\u00a0\u2014 the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0\\le a_i \\le 10^9$$$)\u00a0\u2014 the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, output two lines. In the first line, output a single integer $$$s$$$ ($$$0\\le s\\le 3$$$)\u00a0\u2014 the number of elements you want to append. In the second line, output $$$s$$$ integers $$$b_1, \\dots, b_s$$$ ($$$0\\le b_i \\le 10^{18}$$$)\u00a0\u2014 the elements you want to append to the array. If there are different solutions, you are allowed to output any of them.", "sample_inputs": ["3\n4\n1 2 3 6\n1\n8\n2\n1 1"], "sample_outputs": ["0\n\n2\n4 4\n3\n2 6 2"], "notes": "NoteIn the first test case of the example, the sum of all numbers is $$$12$$$, and their $$$\\oplus$$$ is $$$6$$$, so the condition is already satisfied.In the second test case of the example, after adding $$$4, 4$$$, the array becomes $$$[8, 4, 4]$$$. The sum of numbers in it is $$$16$$$, $$$\\oplus$$$ of numbers in it is $$$8$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = split ' ', <>;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy $sum = 0;\n\t\n\t$sum += $_ for @_;\n\t\n\tmy $xor = 0;\n\t\n\t$xor ^= $_ for @_;\n\t\n\t$debug and print \"sum:[$sum]\";\n\t$debug and print \"xor:[$xor]\";\n\t\n\tmy $Bsum = sprintf \"%0b\", $sum;\n\tmy $Bxor = sprintf \"%0b\", $xor;\n\t\n\t$debug and print \"Bsum:[$Bsum]\";\n\t$debug and print \"Bxor:[$Bxor]\";\n\t\n\tmy @ans;\n\t\n\tpush @ans, $xor;\n\t\n\t$sum += $xor;\n\t$xor ^= $xor;\n\t\n\t$Bsum = sprintf \"%0b\", $sum;\n\t$Bxor = sprintf \"%0b\", $xor;\n\t\n\t$debug and print \"Bsum:[$Bsum]\";\n\t$debug and print \"Bxor:[$Bxor]\";\n\t\n\tpush @ans, $sum;\n\t\n\tprint for ~~ @ans, \"@ans\";\n\t}"}, {"source_code": "$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t( $sum, $xor ) = ( 0 ) x 2;\n\t\n\t$sum += $_, $xor ^= $_ for split ' ', <>;\n\t\n\tprint for 2, join ' ', $xor, $sum + $xor;\n\t}"}], "negative_code": [], "src_uid": "cced3c3d3f1a63e81e36c94fc2ce9379"} {"nl": {"description": "Polycarp is working on a new project called \"Polychat\". Following modern tendencies in IT, he decided, that this project should contain chat as well. To achieve this goal, Polycarp has spent several hours in front of his laptop and implemented a chat server that can process three types of commands: Include a person to the chat ('Add' command). Remove a person from the chat ('Remove' command). Send a message from a person to all people, who are currently in the chat, including the one, who sends the message ('Send' command). Now Polycarp wants to find out the amount of outgoing traffic that the server will produce while processing a particular set of commands.Polycarp knows that chat server sends no traffic for 'Add' and 'Remove' commands. When 'Send' command is processed, server sends l bytes to each participant of the chat, where l is the length of the message.As Polycarp has no time, he is asking for your help in solving this problem.", "input_spec": "Input file will contain not more than 100 commands, each in its own line. No line will exceed 100 characters. Formats of the commands will be the following: +<name> for 'Add' command. -<name> for 'Remove' command. <sender_name>:<message_text> for 'Send' command. <name> and <sender_name> is a non-empty sequence of Latin letters and digits. <message_text> can contain letters, digits and spaces, but can't start or end with a space. <message_text> can be an empty line. It is guaranteed, that input data are correct, i.e. there will be no 'Add' command if person with such a name is already in the chat, there will be no 'Remove' command if there is no person with such a name in the chat etc. All names are case-sensitive.", "output_spec": "Print a single number \u2014 answer to the problem.", "sample_inputs": ["+Mike\nMike:hello\n+Kate\n+Dmitry\n-Dmitry\nKate:hi\n-Kate", "+Mike\n-Mike\n+Mike\nMike:Hi I am here\n-Mike\n+Kate\n-Kate"], "sample_outputs": ["9", "14"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmain();\n\nsub main {\n my %hash = ();\n my $line;\n my $ans = 0;\n while ($line = <>) {\n if ($line =~ /^\\+/) {\n $line =~ s/^\\+//;\n $hash{$line} = 1; \n next;\n } \n if ($line =~ /^-/) {\n $line =~ s/^-//;\n delete $hash{$line};\n next;\n }\n my $cnt = keys (%hash);\n $line =~ s/^[\\S]+://g;\n $line =~ s/[^\\w ]+//g;\n $ans += length ($line) * $cnt; \n }\n print (\"$ans\\n\"); \n}\n"}, {"source_code": "#!/usr/bin/perl\n\nmy @users;\nmy $total = 0;\nmy $len;\n\nwhile (<>) {\n chomp $_;\n if (/\\+/) {\n\t$_ =~ s/\\+//;\n\tpush @users, $_;\n } elsif (/\\-/) {\n\t$_ =~ s/\\-//;\n\tfor my $i (0 .. $#users){\n\t if ($users[$i] eq $_) {\n\t\tsplice(@users, $i, 1, ());\n\t }\n\t}\n } else {\n\t$_ =~ s/([\\w]*:)//;\n\t$total += (scalar @users) * (length $_);\n }\n}\n\nprint $total\n"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nmy ($s, %h);\nmy $r = 0;\nwhile ($s = <>) {\n chomp $s;\n if ($s =~ m/\\+(.+)$/) {\n\t$h{$1} = 1;\n } elsif ($s =~ m/-(.+)$/) {\n\tdelete $h{$1};\n } elsif ($s =~ m/:(.+)$/) {\n\t$r += length($1) * keys %h;\n }\n}\nprint \"$r\\n\";\n"}, {"source_code": "#!usr/bin/perl\nuse strict;\nuse warnings;\n\nmy %ch;my $snd;my $traf=0;my $com;my $msg;my $come;\nfor(1..100){\n $come=;\n chomp($come);\n if($come eq \"\"){ last;}\n if($come=~/\\A[\\+]/){$com=$come=~s/[\\+]//r; $ch{$com}=1;}\n elsif($come=~/\\A[\\-]/){$com=$come=~s/[\\-]//r; $ch{$com}=0;}\n elsif($come=~/:/){\n\t($snd,$msg)=split/:/,$come;chomp($msg);\n\tforeach(keys %ch){ if($ch{$_}==1){$traf+=length($msg);}}\n }\n}\nprint $traf.\"\\n\";"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nmy @users;\nmy $total = 0;\nmy $len;\n\nwhile (<>) {\n if (/\\+/) {\n\t$_ =~ s/\\+//;\n\tpush @users, $_;\n } elsif (/\\-/) {\n\t$_ =~ s/\\-//;\n\tfor my $i (0 .. $#users){\n\t if ($users[$i] =~ $_) {\n\t\tsplice(@users, $i, 1, ());\n\t }\n\t}\n } else {\n\t$_ =~ s/([\\w]*:)//;\n\t$total += (scalar @users) * ((length $_) - 1);\n }\n}\n\nprint $total\n"}], "src_uid": "d7fe15a027750c004e4f50175e1e20d2"} {"nl": {"description": "Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (\u2009<\u20090). The product of all numbers in the second set is greater than zero (\u2009>\u20090). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array.", "input_spec": "The first line of the input contains integer n (3\u2009\u2264\u2009n\u2009\u2264\u2009100). The second line contains n space-separated distinct integers a1,\u2009a2,\u2009...,\u2009an (|ai|\u2009\u2264\u2009103) \u2014 the array elements.", "output_spec": "In the first line print integer n1 (n1\u2009>\u20090) \u2014 the number of elements in the first set. Then print n1 numbers \u2014 the elements that got to the first set. In the next line print integer n2 (n2\u2009>\u20090) \u2014 the number of elements in the second set. Then print n2 numbers \u2014 the elements that got to the second set. In the next line print integer n3 (n3\u2009>\u20090) \u2014 the number of elements in the third set. Then print n3 numbers \u2014 the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them.", "sample_inputs": ["3\n-1 2 0", "4\n-1 -2 -3 0"], "sample_outputs": ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nchomp(my $n = <>);\nchomp($_ = <>);\nmy @a = split;\nmy @pos = grep { $_ > 0 } @a;\nmy @neg = grep { $_ < 0 } @a;\nmy @zero = grep { $_ == 0 } @a;\n\nmy @set1 = ();\nmy @set2 = ();\nmy @set3 = ();\n\nif (@pos) {\n push @set1, shift @neg;\n push @set2, shift @pos;\n push @set3, @pos;\n push @set3, @neg;\n push @set3, @zero;\n} else {\n push @set1, shift @neg;\n push @set2, shift @neg;\n push @set2, shift @neg;\n push @set3, @pos;\n push @set3, @neg;\n push @set3, @zero;\n}\n\nunshift @set1, scalar @set1;\nunshift @set2, scalar @set2;\nunshift @set3, scalar @set3;\nprint \"@set1\\n@set2\\n@set3\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n# \n\nwhile ($eil=<>) {\n\n$num = <>;\n@num = split/ /,$num;\nchomp @num;\n\n \n$j=0; \nforeach (@num){\n /-/ and $j++;\n }\n$j = $j % 2;\n# print $j, \"\\n\";\n \n$i=0;\n@a1 = @a2 = @a3 = ();\nforeach (@num){\n if ($_==0) {push @a3, $_} \n elsif ($_<0 and $i==0) {push @a1, $_; $i++}\n elsif ($_<0 and $j==0) {push @a3, $_; $j++}\n else {push @a2, $_}\n }\n\n\n# print \"@num\\n\";\nprint \"1 @a1\\n\";\nprint scalar @a2, \" @a2\\n\";\nprint scalar @a3, \" @a3\\n\";\n\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n# \n\nwhile ($eil=<>) {\n\n$num = <>;\n@num = split/ /,$num;\nchomp @num;\n\n$i=0;\n@a1 = @a2 = @a3 = ();\nforeach (@num){\n if ($_==0) {push @a3, $_} \n elsif ($_<0 and $i==0) {push @a1, $_; $i++}\n else {push @a2, $_}\n }\nprint \"@num\\n\";\nprint \"1 @a1\\n\";\nprint scalar @a2, \" @a2\\n\";\nprint scalar @a3, \" @a3\\n\";\n\n}"}, {"source_code": "#!/usr/bin/perl\n# \n\nwhile ($eil=<>) {\n\n$num = <>;\n@num = split/ /,$num;\nchomp @num;\n\n$i=0;\n@a1 = @a2 = @a3 = ();\nforeach (@num){\n if ($_==0) {push @a3, $_} \n elsif ($_<0 and $i==0) {push @a1, $_; $i++}\n else {push @a2, $_}\n }\n# print \"@num\\n\";\nprint \"1 @a1\\n\";\nprint scalar @a2, \" @a2\\n\";\nprint scalar @a3, \" @a3\\n\";\n\n}"}], "src_uid": "03cf2cc26c84aab685ee78a1d6318b30"} {"nl": {"description": "Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1\u2009\u2264\u2009i\u2009\u2264\u2009n\u2009-\u20091), she jumps to height h of the tree i\u2009+\u20091. This action can't be performed if h\u2009>\u2009hi\u2009+\u20091. Compute the minimal time (in seconds) required to eat all nuts.", "input_spec": "The first line contains an integer n (1\u2009\u2009\u2264\u2009\u2009n\u2009\u2264\u2009105) \u2014 the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1\u2009\u2264\u2009hi\u2009\u2264\u2009104) \u2014 the height of the tree with the number i.", "output_spec": "Print a single integer \u2014 the minimal time required to eat all nuts in seconds.", "sample_inputs": ["2\n1\n2", "5\n2\n1\n2\n1\n1"], "sample_outputs": ["5", "14"], "notes": null}, "positive_code": [{"source_code": "chomp ($n = );\nwhile ($n--) {\n\tchomp ($i = );\n\tpush @h, $i;\n}\n\n$time = 0;\n$myH = 0;\nfor ($i=0; $i<@h; $i++) {\n\t$time += $h[$i] - $myH;\n\t$myH = $h[$i];\n\n\t$time++;\n\n\tif ($i != $#h) {\n\t\tif ($h[$i] > $h[$i+1]) {\n\t\t\t$time += $h[$i] - $h[$i+1];\n\t\t\t$myH = $h[$i+1];\n\t\t}\n\t\t$time++;\n\t}\n\n\t#printf \"Tree: $i($h[$i]) Time: $time myH: $myH\\n\";\n}\n\nprint \"$time\\n\";\n"}, {"source_code": "while ($n=<>){\n chomp $n;\n $h=0;\n $t=-1;\n while ($n--){\n $_=<>;\n chomp;\n $t+=abs($_-$h);\n $t+=2;\n $h=$_;\n }\n print \"$t\\n\";\n }"}], "negative_code": [{"source_code": "chomp ($n = );\nwhile ($n--) {\n\tchomp ($i = );\n\tpush @h, $i;\n}\n\n$time = 0;\nfor ($i=0; $i<@h; $i++) {\n\t$time += $h[$i];\n\t$time ++;\n\tif ($i != $#h) {\n\t\tif ($h[$i] > $h[$i+1]) {\n\t\t\t$time += $h[$i] - $h[$i+1];\n\t\t}\n\t}\n}\n\nprint \"$time\\n\";\n"}], "src_uid": "a20ca4b053ba71f6b2dc05749287e0a4"} {"nl": {"description": "You are given an array of $$$n$$$ positive integers $$$a_1, a_2, \\ldots, a_n$$$. Your task is to calculate the number of arrays of $$$n$$$ positive integers $$$b_1, b_2, \\ldots, b_n$$$ such that: $$$1 \\le b_i \\le a_i$$$ for every $$$i$$$ ($$$1 \\le i \\le n$$$), and $$$b_i \\neq b_{i+1}$$$ for every $$$i$$$ ($$$1 \\le i \\le n - 1$$$). The number of such arrays can be very large, so print it modulo $$$998\\,244\\,353$$$.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$)\u00a0\u2014 the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$).", "output_spec": "Print the answer modulo $$$998\\,244\\,353$$$ in a single line.", "sample_inputs": ["3\n2 2 2", "2\n2 3", "3\n1 1 1"], "sample_outputs": ["2", "4", "0"], "notes": "NoteIn the first test case possible arrays are $$$[1, 2, 1]$$$ and $$$[2, 1, 2]$$$.In the second test case possible arrays are $$$[1, 2]$$$, $$$[1, 3]$$$, $$$[2, 1]$$$ and $$$[2, 3]$$$."}, "positive_code": [{"source_code": "$i=<>&++$n;\nmap{($%+=(-$n+($n+=pop@T))*pop@S)%=$=while$S[-1]>$_;$n=($%-=($T[@T]=$n%=$==998244353)*($S[@S]=$_))%=$=}split$\",<>;\nprint(($i?$=-$%:$%)%998244353)\n\n"}], "negative_code": [{"source_code": "$i=<>&++$n;\r\nmap{($%+=(-$n+($n+=pop@T))*pop@S)%=$=while$S[-1]>$_;$n=($%-=($T[@T]=$n%=$==998244353)*($S[@S]=$_))%=$=}split$\",<>;\r\nprint$i?$=-$%:$%\r\n"}], "src_uid": "44d3494f40176d56c8fe57908ff09db5"} {"nl": {"description": "There are $$$n$$$ participants in a competition, participant $$$i$$$ having a strength of $$$s_i$$$. Every participant wonders how much of an advantage they have over the other best participant. In other words, each participant $$$i$$$ wants to know the difference between $$$s_i$$$ and $$$s_j$$$, where $$$j$$$ is the strongest participant in the competition, not counting $$$i$$$ (a difference can be negative).So, they ask you for your help! For each $$$i$$$ ($$$1 \\leq i \\leq n$$$) output the difference between $$$s_i$$$ and the maximum strength of any participant other than participant $$$i$$$.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$)\u00a0\u2014 the number of test cases. The descriptions of the test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$2 \\leq n \\leq 2\\cdot10^5$$$)\u00a0\u2014 the length of the array. The following line contains $$$n$$$ space-separated positive integers $$$s_1$$$, $$$s_2$$$, ..., $$$s_n$$$ ($$$1 \\leq s_i \\leq 10^9$$$)\u00a0\u2014 the strengths of the participants. 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 $$$n$$$ space-separated integers. For each $$$i$$$ ($$$1 \\leq i \\leq n$$$) output the difference between $$$s_i$$$ and the maximum strength of any other participant.", "sample_inputs": ["5\n\n4\n\n4 7 3 5\n\n2\n\n1 2\n\n5\n\n1 2 3 4 5\n\n3\n\n4 9 4\n\n4\n\n4 4 4 4"], "sample_outputs": ["-3 2 -4 -2 \n-1 1 \n-4 -3 -2 -1 1 \n-5 5 -5 \n0 0 0 0"], "notes": "NoteFor the first test case: The first participant has a strength of $$$4$$$ and the largest strength of a participant different from the first one is $$$7$$$, so the answer for the first participant is $$$4 - 7 = -3$$$. The second participant has a strength of $$$7$$$ and the largest strength of a participant different from the second one is $$$5$$$, so the answer for the second participant is $$$7 - 5 = 2$$$. The third participant has a strength of $$$3$$$ and the largest strength of a participant different from the third one is $$$7$$$, so the answer for the third participant is $$$3 - 7 = -4$$$. The fourth participant has a strength of $$$5$$$ and the largest strength of a participant different from the fourth one is $$$7$$$, so the answer for the fourth participant is $$$5 - 7 = -2$$$. "}, "positive_code": [{"source_code": "<>;\r\n\r\nwhile(<>){\r\n\t( $m, $s ) = sort { $b <=> $a } @_ = split ' ', <>;\r\n\t\r\n\t$_ -= $_ == $m ? $s : $m for @_;\r\n\t\r\n\tprint \"@_\\n\"\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy( $max, $smax ) = sort { $b <=> $a } @_;\n\t\n\t$_ -= $max for @_;\n\t\n\t$_ ||= $max - $smax for @_;\n\t\n\tprint \"@_\";\n\t}"}], "negative_code": [], "src_uid": "7965b6ce237b02617b55dc175ffa0451"} {"nl": {"description": "Vova is again playing some computer game, now an RPG. In the game Vova's character received a quest: to slay the fearsome monster called Modcrab.After two hours of playing the game Vova has tracked the monster and analyzed its tactics. The Modcrab has h2 health points and an attack power of a2. Knowing that, Vova has decided to buy a lot of strong healing potions and to prepare for battle.Vova's character has h1 health points and an attack power of a1. Also he has a large supply of healing potions, each of which increases his current amount of health points by c1 when Vova drinks a potion. All potions are identical to each other. It is guaranteed that c1\u2009>\u2009a2.The battle consists of multiple phases. In the beginning of each phase, Vova can either attack the monster (thus reducing its health by a1) or drink a healing potion (it increases Vova's health by c1; Vova's health can exceed h1). Then, if the battle is not over yet, the Modcrab attacks Vova, reducing his health by a2. The battle ends when Vova's (or Modcrab's) health drops to 0 or lower. It is possible that the battle ends in a middle of a phase after Vova's attack.Of course, Vova wants to win the fight. But also he wants to do it as fast as possible. So he wants to make up a strategy that will allow him to win the fight after the minimum possible number of phases.Help Vova to make up a strategy! You may assume that Vova never runs out of healing potions, and that he can always win.", "input_spec": "The first line contains three integers h1, a1, c1 (1\u2009\u2264\u2009h1,\u2009a1\u2009\u2264\u2009100, 2\u2009\u2264\u2009c1\u2009\u2264\u2009100) \u2014 Vova's health, Vova's attack power and the healing power of a potion. The second line contains two integers h2, a2 (1\u2009\u2264\u2009h2\u2009\u2264\u2009100, 1\u2009\u2264\u2009a2\u2009<\u2009c1) \u2014 the Modcrab's health and his attack power.", "output_spec": "In the first line print one integer n denoting the minimum number of phases required to win the battle. Then print n lines. i-th line must be equal to HEAL if Vova drinks a potion in i-th phase, or STRIKE if he attacks the Modcrab. The strategy must be valid: Vova's character must not be defeated before slaying the Modcrab, and the monster's health must be 0 or lower after Vova's last action. If there are multiple optimal solutions, print any of them.", "sample_inputs": ["10 6 100\n17 5", "11 6 100\n12 5"], "sample_outputs": ["4\nSTRIKE\nHEAL\nSTRIKE\nSTRIKE", "2\nSTRIKE\nSTRIKE"], "notes": "NoteIn the first example Vova's character must heal before or after his first attack. Otherwise his health will drop to zero in 2 phases while he needs 3 strikes to win.In the second example no healing needed, two strikes are enough to get monster to zero health and win with 6 health left."}, "positive_code": [{"source_code": "$\\ = $/;\n\n( $h1, $a1, $c, $h2, $a2 ) = split ' ', <>.<>;\n\nwhile( 1 ){\n\tpush @M, do {\n\t\tif( $h1 - $a2 > 0 || $a1 >= $h2 ){\n\t\t\t$h2 -= $a1;\n\t\t\t'STRIKE'\n\t\t\t}\n\t\telse{\n\t\t\t$h1 += $c;\n\t\t\t'HEAL'\n\t\t\t}\n\t\t};\n\t$h2 <= 0 and last;\n\t$h1 -= $a2;\n\t}\n\nprint for ~~ @M, @M"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $h1, $a1, $c ) = split;\n\tmy( $h2, $a2 ) = split ' ', <>;\n\t\n\tmy @moves;\n\t\n\twhile( 1 ){\n\t\tif( $h1 - $a2 > 0 || $a1 >= $h2 ){\n\t\t\tpush @moves, 'STRIKE';\n\t\t\t$h2 -= $a1;\n\t\t\t$h2 <= 0 and last;\n\t\t\t}\n\t\telse{\n\t\t\tpush @moves, 'HEAL';\n\t\t\t$h1 += $c;\n\t\t\t}\n\t\t$h1 -= $a2;\n\t\t}\n\t\n\tprint ~~ @moves;\n\tprint for @moves;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $h1, $a1, $c ) = split;\n\tmy( $h2, $a2 ) = split ' ', <>;\n\t\n\tmy @moves;\n\t\n\twhile( 1 ){\n\t\tif( $h1 - $a2 > 0 ){\n\t\t\tpush @moves, 'STRIKE';\n\t\t\t$h2 -= $a1;\n\t\t\t$h2 <= 0 and last;\n\t\t\t}\n\t\telse{\n\t\t\tpush @moves, 'HEAL';\n\t\t\t$h1 += $c;\n\t\t\t}\n\t\t$h1 -= $a2;\n\t\t}\n\t\n\tprint ~~ @moves;\n\tprint for @moves;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $h1, $a1, $c ) = split;\n\tmy( $h2, $a2 ) = split ' ', <>;\n\t\n\tmy @moves;\n\t\n\twhile( 1 ){\n\t\tif( $h1 - $a2 > 0 ){\n\t\t\tpush @moves, 'STRIKE';\n\t\t\t$h2 -= $a1;\n\t\t\t$h2 <= 0 and last;\n\t\t\t$h1 -= $a2;\n\t\t\t}\n\t\telse{\n\t\t\tpush @moves, 'HEAL';\n\t\t\t$h1 += $c;\n\t\t\t}\n\t\t}\n\t\n\tprint ~~ @moves;\n\tprint for @moves;\n\t}"}], "src_uid": "d497431eb37fafdf211309da8740ece6"} {"nl": {"description": "Tired of boring office work, Denis decided to open a fast food restaurant.On the first day he made $$$a$$$ portions of dumplings, $$$b$$$ portions of cranberry juice and $$$c$$$ pancakes with condensed milk.The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules: every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes); each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk; all visitors should receive different sets of dishes. What is the maximum number of visitors Denis can feed?", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 500$$$)\u00a0\u2014 the number of test cases to solve. Each of the remaining $$$t$$$ lines contains integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$0 \\leq a, b, c \\leq 10$$$)\u00a0\u2014 the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.", "output_spec": "For each test case print a single integer\u00a0\u2014 the maximum number of visitors Denis can feed.", "sample_inputs": ["7\n1 2 1\n0 0 0\n9 1 7\n2 2 3\n2 3 2\n3 2 2\n4 4 4"], "sample_outputs": ["3\n0\n4\n5\n5\n5\n7"], "notes": "NoteIn the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile( <> ){\n $debug and print '-' x 15;\n \n @_ = sort { $b <=> $a } split;\n \n my $ans = 0;\n \n if( $_[ 0 ] ){\n $_[ 0 ] --;\n $ans ++;\n }\n \n if( $_[ 1 ] ){\n $_[ 1 ] --;\n $ans ++;\n }\n \n if( $_[ 2 ] ){\n $_[ 2 ] --;\n $ans ++;\n }\n \n if( $_[ 0 ] and $_[ 1 ] ){\n $_[ 0 ] --; $_[ 1 ] --;\n $ans ++;\n }\n \n if( $_[ 0 ] and $_[ 2 ] ){\n $_[ 0 ] --; $_[ 2 ] --;\n $ans ++;\n }\n \n if( $_[ 1 ] and $_[ 2 ] ){\n $_[ 1 ] --; $_[ 2 ] --;\n $ans ++;\n }\n \n if( $_[ 0 ] and $_[ 1 ] and $_[ 2 ] ){\n $_[ 0 ] --; $_[ 1 ] --; $_[ 21 ] --;\n $ans ++;\n }\n \n print $ans;\n }"}], "negative_code": [], "src_uid": "98a8fc06e8265bbf9c16ee3c7b9d0223"} {"nl": {"description": "Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special. Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li],\u2009a[li\u2009+\u20091],\u2009...,\u2009a[ri].Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible. You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.The mex of a set S is a minimum possible non-negative integer that is not in S.", "input_spec": "The first line contains two integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009105). The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1\u2009\u2264\u2009li\u2009\u2264\u2009ri\u2009\u2264\u2009n), that describe the subarray a[li],\u2009a[li\u2009+\u20091],\u2009...,\u2009a[ri].", "output_spec": "In the first line print single integer\u00a0\u2014 the maximum possible minimum mex. In the second line print n integers\u00a0\u2014 the array a. All the elements in a should be between 0 and 109. It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109. If there are multiple solutions, print any of them.", "sample_inputs": ["5 3\n1 3\n2 5\n4 5", "4 2\n1 4\n2 4"], "sample_outputs": ["2\n1 0 2 1 0", "3\n5 2 0 1"], "notes": "NoteThe first example: the mex of the subarray (1,\u20093) is equal to 3, the mex of the subarray (2,\u20095) is equal to 3, the mex of the subarray (4,\u20095) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2."}, "positive_code": [{"source_code": "#!perl\nwhile(<>){\n ($n,$m)=split;\n $len=100_009;\n for(1..$m) { \n\tmy($l,$r)=split ' ',<>;\n\t$len=$r-$l+1 if($r-$l+1<$len);\n }\n print \"$len\\n\";\n for my$i(0..$n-1) {\n\tprint $i%$len,' ';\n }\n print \"\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t\n\tmy( $n, $m ) = split;\n\t\t\n\tmy $min = (sort {$a <=> $b} map { 0 - eval join '-', split ' ', <> } 1 .. $m )[ 0 ];\n\t\n\tprint 1 + $min;\n\tprint join \" \", ( (0 .. $min) x (1 + $n / ( $min || 1 ) ) )[ 0 .. $n - 1 ];\n\t\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t\n\tmy( $n, $m ) = split;\n\t\t\n\tmy $min = (sort {$a <=> $b} map { 0 - eval join '-', split ' ', <> } 1 .. $m )[ 0 ];\n\t\n\tprint 1 + $min;\n\tprint do { local $, = \" \"; ( (0 .. $min) x (1 + $n / $min) )[ 0 .. $n - 1 ] };\n\t\n\t}"}, {"source_code": "#!perl\nwhile(<>){\n ($n,$m)=split;\n $len=100_009;\n for(1..$m) { \n\tmy($l,$r)=split ' ',<>;\n\t$len=$r-$l+1 if($r-$l+1<$len);\n }\n print \"$len\\n\";\n for my$i(0..$n-1) {\n\tprint $i%$m,' ';\n }\n print \"\\n\";\n}\n"}], "src_uid": "317891277a5393758bd3c8f606769070"} {"nl": {"description": "You've got two numbers. As long as they are both larger than zero, they go through the same operation: subtract the lesser number from the larger one. If they equal substract one number from the another. For example, one operation transforms pair (4,17) to pair (4,13), it transforms (5,5) to (0,5).You've got some number of pairs (ai,\u2009bi). How many operations will be performed for each of them?", "input_spec": "The first line contains the number of pairs n (1\u2009\u2009\u2264\u2009\u2009n\u2009\u2009\u2264\u2009\u20091000). Then follow n lines, each line contains a pair of positive integers ai,\u2009bi (1\u2009\u2009\u2264\u2009\u2009ai,\u2009\u2009bi\u2009\u2009\u2264\u2009\u2009109).", "output_spec": "Print the sought number of operations for each pair on a single line.", "sample_inputs": ["2\n4 17\n7 987654321"], "sample_outputs": ["8\n141093479"], "notes": null}, "positive_code": [{"source_code": "for(1..<>){\n\t($a, $b) = split(' ', <>);\n\t$sum = 0;\n\tuntil($a == 0 or $b == 0){\n\t$sum += int($a / $b) and $a %= $b and next if $a >= $b and $b != 0;\n\t$sum += int($b / $a) and $b %= $a and next if $b >= $a and $a != 0\t;}\n\tprint \"$sum\\n\";\n\t\t}\n"}], "negative_code": [], "src_uid": "5c3eb78a7e15d9afe6d745e1a77d7451"} {"nl": {"description": "You are given an array consisting of $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$. Initially $$$a_x = 1$$$, all other elements are equal to $$$0$$$.You have to perform $$$m$$$ operations. During the $$$i$$$-th operation, you choose two indices $$$c$$$ and $$$d$$$ such that $$$l_i \\le c, d \\le r_i$$$, and swap $$$a_c$$$ and $$$a_d$$$.Calculate the number of indices $$$k$$$ such that it is possible to choose the operations so that $$$a_k = 1$$$ in the end.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$)\u00a0\u2014 the number of test cases. Then the description of $$$t$$$ testcases follow. The first line of each test case contains three integers $$$n$$$, $$$x$$$ and $$$m$$$ ($$$1 \\le n \\le 10^9$$$; $$$1 \\le m \\le 100$$$; $$$1 \\le x \\le n$$$). Each of next $$$m$$$ lines contains the descriptions of the operations; the $$$i$$$-th line contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \\le l_i \\le r_i \\le n$$$).", "output_spec": "For each test case print one integer \u2014 the number of indices $$$k$$$ such that it is possible to choose the operations so that $$$a_k = 1$$$ in the end.", "sample_inputs": ["3\n6 4 3\n1 6\n2 3\n5 5\n4 1 2\n2 4\n1 2\n3 3 2\n2 3\n1 2"], "sample_outputs": ["6\n2\n3"], "notes": "NoteIn the first test case, it is possible to achieve $$$a_k = 1$$$ for every $$$k$$$. To do so, you may use the following operations: swap $$$a_k$$$ and $$$a_4$$$; swap $$$a_2$$$ and $$$a_2$$$; swap $$$a_5$$$ and $$$a_5$$$. In the second test case, only $$$k = 1$$$ and $$$k = 2$$$ are possible answers. To achieve $$$a_1 = 1$$$, you have to swap $$$a_1$$$ and $$$a_1$$$ during the second operation. To achieve $$$a_2 = 1$$$, you have to swap $$$a_1$$$ and $$$a_2$$$ during the second operation."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nfor(my $i=0;$i<$t;$i++){\n my ($n,$x,$m) = map { $_ - 0 } split(/\\s+/,);\n my $ll = $x;\n my $rr = $x;\n for(my $j=0;$j<$m;$j++){\n my ($l,$r) = map { $_ - 0 } split(/\\s+/,);\n next if $r < $ll or $rr < $l;\n $ll = ( $l < $ll ? $l : $ll );\n $rr = ( $r > $rr ? $r : $rr );\n }\n my $res = 1 + $rr - $ll;\n print \"$res\\n\";\n}\n\n\n"}], "negative_code": [], "src_uid": "c358bce566a846bd278329ef2c029dff"} {"nl": {"description": "Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n\u2009-\u20092 as a result.Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.", "input_spec": "First line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105), the length of the string that Andreid has. The second line contains the string of length n consisting only from zeros and ones.", "output_spec": "Output the minimum length of the string that may remain after applying the described operations several times.", "sample_inputs": ["4\n1100", "5\n01010", "8\n11101111"], "sample_outputs": ["0", "1", "6"], "notes": "NoteIn the first sample test it is possible to change the string like the following: .In the second sample test it is possible to change the string like the following: .In the third sample test it is possible to change the string like the following: ."}, "positive_code": [{"source_code": "my $n = int<>;\nmy @arr = split //, <>;\nmy @stack = ();\nmy $ans = $n;\npush @stack, shift @arr;\nfor (2..$n) {\n push @stack, shift @arr;\n if (scalar @stack > 1 && $stack[-1] != $stack[-2]) {\n pop @stack;\n pop @stack;\n }\n}\nprint scalar @stack;\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nmy ($n, $line, $on, $zn) = (<>, <>, 0, 0);\nchomp $line;\n$_>0 and ++$on or ++$zn foreach (split //, $line);\nsay abs($zn-$on);"}, {"source_code": "use 5.016;\nuse strict;\nuse warnings;\n\nsub abs {\n my ($x) = @_;\n $x < 0 ? -$x : $x;\n}\n\n<>;\nchomp (my $bits = <>);\n\nmy $zero = $bits =~ tr/0//d;\nmy $one = $bits =~ tr/1//d;\nsay abs($one - $zero);\n"}, {"source_code": "<>;$x = <>;\n$c = $x=~tr/0/0/ - $x=~tr/1/1/;\nprint ($c < 0 ? -$c : $c);\n\n"}, {"source_code": "use strict;\nuse warnings;\n#!/usr/bin/perl\n#$, = \" \";\n\n$a = <>;\n$a = <>;\nchomp($a);\nmy ($zero, $one) = (0,0);\nforeach my $i (0..length($a)-1) {\n\t$zero += (substr($a,$i,1)=='0');\n\t$one += (substr($a,$i,1)=='1');\n}\nprint abs($zero-$one);"}, {"source_code": "<>;\n$_ = <>;\n$v = ( () = /1/g);\n$n = ( () = /0/g);\nprint abs ($v - $n)"}], "negative_code": [{"source_code": "$_ = <>;\n$v = ( () = /1/g);\n$n = ( () = /0/g);\nprint abs ($v - $n)"}], "src_uid": "fa7a44fd24fa0a8910cb7cc0aa4f2155"} {"nl": {"description": "Caisa solved the problem with the sugar and now he is on the way back to home. Caisa is playing a mobile game during his path. There are (n\u2009+\u20091) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i\u2009>\u20090) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k\u2009+\u20091). When the player have made such a move, its energy increases by hk\u2009-\u2009hk\u2009+\u20091 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time. Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105). The next line contains n integers h1, h2,\u2009..., hn (1\u2009\u2009\u2264\u2009\u2009hi\u2009\u2009\u2264\u2009\u2009105) representing the heights of the pylons.", "output_spec": "Print a single number representing the minimum number of dollars paid by Caisa.", "sample_inputs": ["5\n3 4 3 2 4", "3\n4 4 4"], "sample_outputs": ["4", "4"], "notes": "NoteIn the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon."}, "positive_code": [{"source_code": "<>;\nmy $m = 0;\n$_ > $m and $m = $_ for(split / /, <>);\nprint $m;"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n$ans = 0;\n$ans=$_>$ans ? $_:$ans foreach (split / /, <>);\nsay $ans;"}, {"source_code": "<>;\n@_=split/ /,<>;\n$_>$M and $M=$_ for @_;\nprint $M"}], "negative_code": [], "src_uid": "d03ad531630cbecc43f8da53b02d842e"} {"nl": {"description": "You are given two polynomials: P(x)\u2009=\u2009a0\u00b7xn\u2009+\u2009a1\u00b7xn\u2009-\u20091\u2009+\u2009...\u2009+\u2009an\u2009-\u20091\u00b7x\u2009+\u2009an and Q(x)\u2009=\u2009b0\u00b7xm\u2009+\u2009b1\u00b7xm\u2009-\u20091\u2009+\u2009...\u2009+\u2009bm\u2009-\u20091\u00b7x\u2009+\u2009bm. Calculate limit .", "input_spec": "The first line contains two space-separated integers n and m (0\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100) \u2014 degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n\u2009+\u20091 space-separated integers \u2014 the factors of polynomial P(x): a0, a1, ..., an\u2009-\u20091, an (\u2009-\u2009100\u2009\u2264\u2009ai\u2009\u2264\u2009100,\u2009a0\u2009\u2260\u20090). The third line contains m\u2009+\u20091 space-separated integers \u2014 the factors of polynomial Q(x): b0, b1, ..., bm\u2009-\u20091, bm (\u2009-\u2009100\u2009\u2264\u2009bi\u2009\u2264\u2009100,\u2009b0\u2009\u2260\u20090).", "output_spec": "If the limit equals \u2009+\u2009\u221e, print \"Infinity\" (without quotes). If the limit equals \u2009-\u2009\u221e, print \"-Infinity\" (without the quotes). If the value of the limit equals zero, print \"0/1\" (without the quotes). Otherwise, print an irreducible fraction \u2014 the value of limit , in the format \"p/q\" (without the quotes), where p is the \u2014 numerator, q (q\u2009>\u20090) is the denominator of the fraction.", "sample_inputs": ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"], "sample_outputs": ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"], "notes": "NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function"}, "positive_code": [{"source_code": "#!perl -w\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my ($n, $m) = map int, split /\\D/, <>;\n my (@p, @q); my $lim;\n my $s = <>; chomp $s;\n @p = reverse map int, split / /, $s;\n $s = <>; chomp($s);\n @q = reverse map int, split / /, $s;\n while ($p[$#p] == 0) { pop @p } ;\n while ($q[$#q] == 0) { pop @q } ;\n if (! @q) { @q = (0); }\n if (! @p) { @p = (0); }\n if (@p > @q) {\n if (($p[$#p] * $q[$#q] >= 0) || ($q[$#q] == 0)) {\n $lim = \"Infinity\";\n } else {\n $lim = \"-Infinity\";\n }\n } elsif (@p < @q) {\n $lim = \"0/1\";\n } else {\n my ($u, $d) = ($p[$#p], $q[$#q]);\n if ($d < 0) {\n ($u, $d) = (-$u, -$d);\n }\n my $g = gcd(abs $u, abs $d);\n $u /= $g; $d /= $g;\n #if ($d > 1) {\n $lim = \"$u\" . \"/\" . \"$d\";\n #} else {\n # $lim = $u;\n #}\n }\n print $lim;\n}\n\nsub gcd {\n my ($a, $b) = @_;\n ($a,$b) = ($b,$a) if $a > $b;\n while ($a) {\n ($a, $b) = ($b % $a, $a);\n }\n return $b;\n}\n\n__END__\n"}], "negative_code": [{"source_code": "#!perl -w\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my ($n, $m) = map int, split /\\D/, <>;\n my (@p, @q); my $lim;\n my $s = <>; chomp $s;\n @p = reverse map int, split / /, $s;\n $s = <>; chomp($s);\n @q = reverse map int, split / /, $s; \n if ( ( (@p == 1) && (@q == 1) && ($q[0] == 0) ) || (@p > @q) ) {\n if ($p[$#p] > 0) {\n $lim = \"Infinity\";\n } else {\n $lim = \"-Infinity\";\n }\n } elsif (@p < @q) {\n $lim = \"0/1\";\n } else {\n my ($u, $d) = ($p[$#p], $q[$#q]);\n if ($d < 0) {\n ($u, $d) = (-$u, -$d);\n }\n my $g = gcd(abs $u, abs $d);\n $u /= $g; $d /= $g;\n if ($d > 1) {\n $lim = \"$u\" . \"/\" . \"$d\";\n } else {\n $lim = $u;\n }\n }\n print $lim;\n}\n\nsub gcd {\n my ($a, $b) = @_;\n ($a,$b) = ($b,$a) if $a > $b;\n while ($a) {\n ($a, $b) = ($b % $a, $a);\n }\n return $b;\n}\n\n__END__\n"}, {"source_code": "#!perl -w\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my ($n, $m) = map int, split /\\D/, <>;\n my (@p, @q); my $lim;\n my $s = <>; chomp $s;\n @p = reverse map int, split / /, $s;\n $s = <>; chomp($s);\n @q = reverse map int, split / /, $s;\n while ($p[$#p] == 0) { pop @p } ;\n while ($q[$#q] == 0) { pop @q } ;\n if (! @q) { @q = (0); }\n if (! @p) { @p = (0); }\n if (@p > @q) {\n if (($p[$#p] * $q[$#q] >= 0) || ($q[$#q] == 0)) {\n $lim = \"Infinity\";\n } else {\n $lim = \"-Infinity\";\n }\n } elsif (@p < @q) {\n $lim = \"0/1\";\n } else {\n my ($u, $d) = ($p[$#p], $q[$#q]);\n if ($d < 0) {\n ($u, $d) = (-$u, -$d);\n }\n my $g = gcd(abs $u, abs $d);\n $u /= $g; $d /= $g;\n if ($d > 1) {\n $lim = \"$u\" . \"/\" . \"$d\";\n } else {\n $lim = $u;\n }\n }\n print $lim;\n}\n\nsub gcd {\n my ($a, $b) = @_;\n ($a,$b) = ($b,$a) if $a > $b;\n while ($a) {\n ($a, $b) = ($b % $a, $a);\n }\n return $b;\n}\n\n__END__\n"}, {"source_code": "#!perl -w\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my ($n, $m) = map int, split /\\D/, <>;\n my (@p, @q); my $lim;\n my $s = <>; chomp $s;\n @p = reverse map int, split / /, $s;\n $s = <>; chomp($s);\n @q = reverse map int, split / /, $s; \n if ( ( (@p == 1) && (@q == 1) && ($q[0] == 0) ) || (@p > @q) ) {\n if ($p[$#p] > 0) {\n $lim = \"Infinity\";\n } elsif ($p[$#p] < 0) {\n $lim = \"-Infinity\";\n } else {\n $lim = \"1\";\n }\n } elsif (@p < @q) {\n $lim = \"0/1\";\n } else {\n my ($u, $d) = ($p[$#p], $q[$#q]);\n if ($d < 0) {\n ($u, $d) = (-$u, -$d);\n }\n my $g = gcd(abs $u, abs $d);\n $u /= $g; $d /= $g;\n if ($d > 1) {\n $lim = \"$u\" . \"/\" . \"$d\";\n } else {\n $lim = $u;\n }\n }\n print $lim;\n}\n\nsub gcd {\n my ($a, $b) = @_;\n ($a,$b) = ($b,$a) if $a > $b;\n while ($a) {\n ($a, $b) = ($b % $a, $a);\n }\n return $b;\n}\n\n__END__\n\n0 0\n0\n0"}, {"source_code": "#!perl -w\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my ($n, $m) = map int, split /\\D/, <>;\n my (@p, @q); my $lim;\n my $s = <>; chomp $s;\n @p = reverse map int, split / /, $s;\n $s = <>; chomp($s);\n @q = reverse map int, split / /, $s;\n while ($p[$#p] == 0) { pop @p } ;\n while ($q[$#q] == 0) { pop @q } ;\n if (! @q) { @q = (0); }\n if (! @p) { @p = (0); }\n if (@p > @q) {\n if (($p[$#p] * $q[$#q] >= 0) || ($q[$#q] == 0)) {\n $lim = \"Infinity\";\n } else {\n $lim = \"-Infinity\";\n }\n } elsif (@p < @q) {\n $lim = \"0/1\";\n } else {\n my ($u, $d) = ($p[$#p], $q[$#q]);\n if ($d < 0) {\n ($u, $d) = (-$u, -$d);\n }\n my $g = gcd(abs $u, abs $d);\n $u /= $g; $d /= $g;\n if ($d > 1) {\n $lim = \"$u\" . \"/\" . \"$d\";\n } else {\n $lim = $u;\n }\n }\n print $lim;\n}\n\nsub gcd {\n my ($a, $b) = @_;\n ($a,$b) = ($b,$a) if $a > $b;\n while ($a) {\n ($a, $b) = ($b % $a, $a);\n }\n return $b;\n}\n\n__END__\n"}, {"source_code": "#!perl -w\nuse strict;\nuse warnings;\n\nmain();\n\nsub main {\n my ($n, $m) = map int, split /\\D/, <>;\n my (@p, @q); my $lim;\n my $s = <>; chomp $s;\n @p = reverse map int, split / /, $s;\n $s = <>; chomp($s);\n @q = reverse map int, split / /, $s; \n if ( ( (@p == 1) && (@q == 1) && ($q[0] == 0) ) || (@p > @q) ) {\n if ($p[$#p] > 0) {\n $lim = \"Infinity\";\n } else {\n $lim = \"-Infinity\";\n }\n } elsif (@p < @q) {\n $lim = \"0/1\";\n } else {\n my ($u, $d) = ($p[$#p], $q[$#q]);\n if ($d < 0) {\n ($u, $d) = (-$u, -$d);\n }\n my $g = gcd(abs $u, abs $d);\n $u /= $g; $d /= $g;\n $lim = \"$u\" . \"/\" . \"$d\";\n }\n print $lim;\n}\n\nsub gcd {\n my ($a, $b) = @_;\n ($a,$b) = ($b,$a) if $a > $b;\n while ($a) {\n ($a, $b) = ($b % $a, $a);\n }\n return $b;\n}\n\n__END__\n"}], "src_uid": "37cf6edce77238db53d9658bc92b2cab"} {"nl": {"description": "PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n,\u2009k)\u2009=\u20091. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x\u2009=\u20091 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x\u2009+\u2009k or x\u2009+\u2009k\u2009-\u2009n depending on which of these is a valid index of polygon's vertex.Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides.", "input_spec": "There are only two numbers in the input: n and k (5\u2009\u2264\u2009n\u2009\u2264\u2009106, 2\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009-\u20092, gcd(n,\u2009k)\u2009=\u20091).", "output_spec": "You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines.", "sample_inputs": ["5 2", "10 3"], "sample_outputs": ["2 3 5 8 11", "2 3 4 6 9 12 16 21 26 31"], "notes": "NoteThe greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder.For the first sample testcase, you should output \"2 3 5 8 11\". Pictures below correspond to situations after drawing lines. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t$k = ( sort { $a <=> $b } $n - $k, $k )[ 0 ];\n\t\n\tmy $L = 0;\n\tmy $sect = 1;\n\tmy $plus = 1;\n\tmy @sect;\n\t\n\tfor my $i (1 .. $n){\n\t\t$L += $k;\n\t\tif( $L > $n ){\n\t\t\t$plus ++;\n\t\t\tpush @sect, $sect += $plus;\n\t\t\t$plus ++;\n\t\t\t$L %= $n;\n\t\t\t}\n\t\telse{\n\t\t\tpush @sect, $sect += $plus;\t\t\t\n\t\t\t}\n\t\t\n\t\t}\n\t\n\tprint \"@sect\";\n\t}"}, {"source_code": "( $s, $p, $n, $k ) = ( 1, 1, split ' ', <> );\n $n - $k < $k and $k = $n - $k;\n\nfor (1 .. $n){\n\t$L += $k;\n\t\t$p += $L > $n;\n\t\t\tpush @s, $s += $p;\n\t\t$p += $L > $n;\n\t$L %= $n;\n\t}\n\nprint \"@s\""}], "negative_code": [], "src_uid": "fc82362dbda74396ad6db0d95a0f7acc"} {"nl": {"description": "There are n parliamentarians in Berland. They are numbered with integers from 1 to n. It happened that all parliamentarians with odd indices are Democrats and all parliamentarians with even indices are Republicans.New parliament assembly hall is a rectangle consisting of a\u2009\u00d7\u2009b chairs\u00a0\u2014 a rows of b chairs each. Two chairs are considered neighbouring if they share as side. For example, chair number 5 in row number 2 is neighbouring to chairs number 4 and 6 in this row and chairs with number 5 in rows 1 and 3. Thus, chairs have four neighbours in general, except for the chairs on the border of the hallWe know that if two parliamentarians from one political party (that is two Democrats or two Republicans) seat nearby they spent all time discussing internal party issues.Write the program that given the number of parliamentarians and the sizes of the hall determine if there is a way to find a seat for any parliamentarian, such that no two members of the same party share neighbouring seats.", "input_spec": "The first line of the input contains three integers n, a and b (1\u2009\u2264\u2009n\u2009\u2264\u200910\u2009000, 1\u2009\u2264\u2009a,\u2009b\u2009\u2264\u2009100)\u00a0\u2014 the number of parliamentarians, the number of rows in the assembly hall and the number of seats in each row, respectively.", "output_spec": "If there is no way to assigns seats to parliamentarians in a proper way print -1. Otherwise print the solution in a lines, each containing b integers. The j-th integer of the i-th line should be equal to the index of parliamentarian occupying this seat, or 0 if this seat should remain empty. If there are multiple possible solution, you may print any of them.", "sample_inputs": ["3 2 2", "8 4 3", "10 2 2"], "sample_outputs": ["0 3\n1 2", "7 8 3\n0 1 4\n6 0 5\n0 2 0", "-1"], "notes": "NoteIn the first sample there are many other possible solutions. For example, 3 20 1and 2 13 0The following assignment 3 21 0is incorrect, because parliamentarians 1 and 3 are both from Democrats party but will occupy neighbouring seats."}, "positive_code": [{"source_code": "use strict;\n\nmy ($n, $a, $b);\n\nchomp ($n = );\n\n($n, $a, $b) = split /\\s+/, $n;\n\nif ($n > ($a * $b)) {\n print \"-1\";\n exit;\n}\n\nmy $arr;\nmy $k = 1;\n\nforeach my $i (0 .. $a - 1) {\n foreach my $j (0 .. $b - 1) {\n $arr->[$i][$j] = ($k > $n) ? 0 : $k;\n $k++;\n }\n if (not ($b & 1)) {\n unshift @{$arr->[$i]}, pop @{$arr->[$i]} if ($i & 1);\n }\n}\n\nprint sprintf \"%s\", join \"\\n\", map {join \" \", @{$arr->[$_]}} (0 .. $a - 1);\n"}, {"source_code": "#!/usr/bin/env perl \n#===============================================================================\n# FILE: 1.pl\n# AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com\n# ORGANIZATION: Hunan University\n# CREATED: 03/16/2016 05:01:19 PM\n#===============================================================================\n\n#use strict;\n#use warnings;\n#use utf8;\n#use Data::Dumper;\n\nwhile (<>) {\n my ($n, $a, $b) = split;\n if ($a * $b < $n) {\n print \"-1\\n\";\n } else {\n my $array;\n my $cnt = 1;\n for my $i (0..$a-1) {\n for my $j (0..$b-1) {\n if ($cnt > $n) {\n $array->[$i][$j] = 0;\n } else {\n $array->[$i][$j] = $cnt++;\n }\n }\n }\n if ($b % 2) {\n # odd\n for (0..$a-1) {\n print join ' ', @{$array->[$_]};\n print \"\\n\";\n }\n } else {\n # even\n for (0..$a-1) {\n if ($_ % 2) {\n print join ' ', reverse @{$array->[$_]};\n print \"\\n\";\n } else {\n print join ' ', @{$array->[$_]};\n print \"\\n\";\n }\n }\n }\n }\n}\n\nexit 0;\n"}, {"source_code": "$\\ = $/;\n($n, $x, $y) = split ' ', <>;\n$n > $x * $y and print -1 and exit;\n$_ = join ' ', 1 .. $n, (0) x ($x * $y - $n);\n$re = '(?:\\d+ ??)' x $y;\n$i ++ % 2 and $_ = join ' ', reverse split xor print for /$re/g"}], "negative_code": [], "src_uid": "6e0dafeaf85e92f959c388c72e158f68"} {"nl": {"description": "ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n\u2009\u00d7\u2009n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (), each column of the grid (), and the two long diagonals of the grid (the main diagonal\u00a0\u2014 and the secondary diagonal\u00a0\u2014 ) are equal. Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?", "input_spec": "The first line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009500)\u00a0\u2014 the number of rows and columns of the magic grid. n lines follow, each of them contains n integers. The j-th number in the i-th of them denotes ai,\u2009j (1\u2009\u2264\u2009ai,\u2009j\u2009\u2264\u2009109 or ai,\u2009j\u2009=\u20090), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, ai,\u2009j will be equal to 0. Otherwise, ai,\u2009j is positive. It is guaranteed that there is exactly one pair of integers i,\u2009j (1\u2009\u2264\u2009i,\u2009j\u2009\u2264\u2009n) such that ai,\u2009j\u2009=\u20090.", "output_spec": "Output a single integer, the positive integer x (1\u2009\u2264\u2009x\u2009\u2264\u20091018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output \u2009-\u20091 instead. If there are multiple solutions, you may print any of them.", "sample_inputs": ["3\n4 0 2\n3 5 7\n8 1 6", "4\n1 1 1 1\n1 1 0 1\n1 1 1 1\n1 1 1 1", "4\n1 1 1 1\n1 1 0 1\n1 1 2 1\n1 1 1 1"], "sample_outputs": ["9", "1", "-1"], "notes": "NoteIn the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed, The sum of numbers in each row is:4\u2009+\u20099\u2009+\u20092\u2009=\u20093\u2009+\u20095\u2009+\u20097\u2009=\u20098\u2009+\u20091\u2009+\u20096\u2009=\u200915.The sum of numbers in each column is:4\u2009+\u20093\u2009+\u20098\u2009=\u20099\u2009+\u20095\u2009+\u20091\u2009=\u20092\u2009+\u20097\u2009+\u20096\u2009=\u200915.The sum of numbers in the two diagonals is:4\u2009+\u20095\u2009+\u20096\u2009=\u20092\u2009+\u20095\u2009+\u20098\u2009=\u200915.In the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square."}, "positive_code": [{"source_code": "$n = <>;\nwhile (<>) {\n\t@v = split; push @rc, [@v];\n\t$sum = 0; \n\tfor ($k=0; $k<$n; $k++) {\n\t\tif ($v[$k]) {\n\t\t\t$sum += $v[$k]; \n\t\t} else {\n\t\t\t$i=$.-2; $j=$k;\n\t\t}\n\t}\n\tpush @sum, $sum;\n}\n@sum = sort { $a<=>$b } @sum;\n$lost = $sum[-1]-$sum[0] || 123456789;\n$rc[$i][$j] = $lost;\n$magic = ($sum[1]>$sum[0]? $sum[1]: $sum[0]) || $lost;\nfor ($i=0; $i<$n; $i++) {\n\t$r=0; $c=0;\n\tfor ($j=0; $j<$n; $j++) {\n\t\t$r+=$rc[$i][$j];\n\t\t$c+=$rc[$j][$i];\n\t}\n\tpush @magic, $r, $c;\n\t$d1 += $rc[$i][$i];\n\t$d2 += $rc[$i][$n-$i-1];\n}\npush @magic, $d1, $d2;\n$ans = (grep($_!=$magic, @magic)? -1: $lost);\nprint $ans\n"}, {"source_code": "$\\ = $/;\n\n$n = <>;\nmap { push @_, [ split ] } <>;\n\nif( $n == 1 ){\n\tprint 1;\n\t}\nelse{\n\t$sum = 0;\n\t$zero = 0;\n\tfor( @_ ){\n\t\tif( !$zero and grep !$_, @{$_} ){\n\t\t\t$zero = eval join '+', @{$_};\n\t\t\t}\n\t\tif( !$sum and not grep !$_, @{$_} ){\n\t\t\t$sum = eval join '+', @{$_};\n\t\t\t}\n\t\t}\n\t\n\tfor( @_ ){\n\t\tfor (@{$_}){\n\t\t\t$_ ||= $sum - $zero;\n\t\t\t}\n\t\t}\n\t\t\n\tfor( @_ ){\n\t\t$h{ eval join '+', @{$_} } ++;\n\t\t}\n\t\t\n\tfor $i ( 0 .. @_ - 1 ){\n\t\t$h{ eval join '+', map { @{$_}[$i] } @_ } ++;\n\t\t}\n\t\t\n\tmap { \n\t\t$i = 0; $s = 0;\n\t\tfor( @{$_} ){\n\t\t\t$s += @{$_}[$i++];\n\t\t\t}\n\t\t$h{$s} ++;\n\t} \\@_, [ reverse @_ ];\n\t\n\tprint +(1 == keys %h and 0 < $sum - $zero) ? $sum - $zero : -1;\n\t}"}], "negative_code": [{"source_code": "$n = <>;\nif ($n == 1) {\n\tprint 1;\n\texit;\n}\nfor (<>) {\n\t@v = split;\n\t$sum = 0; $sum += $_ for @v; $r{$sum} = 1;\n\t$c[$_] += $v[$_] for 0..$n-1;\n\t$d1 += $v[$i++]; $d2 += $v[-$i];\n}\n$c{$_} = 1 for @c;\nsub diff { abs($_[0]-$_[2]) }\n$num = diff(%r);\n$ok = (\n\tkeys(%r)==2 && keys(%c)==2 && \n\t$num==diff(%c) &&\n\t($d1==$d2 || abs($d1-$d2)==$num)\n);\nprint $ok? $num: -1\n\t\t\t"}, {"source_code": "$n = <>;\nwhile (<>) {\n\t@v = split; push @rc, [@v];\n\t$sum = 0; \n\tfor ($k=0; $k<$n; $k++) {\n\t\tif ($v[$k]) {\n\t\t\t$sum += $v[$k]; \n\t\t} else {\n\t\t\t$i=$.-2; $j=$k;\n\t\t}\n\t}\n\tpush @sum, $sum;\n}\n$lost = abs($sum[0]-$sum[1]) || 123456789;\n$rc[$i][$j] = $lost;\n$magic = ($sum[1]>$sum[0]? $sum[1]: $sum[0]);\nfor ($i=0; $i<$n; $i++) {\n\t$r=0; $c=0;\n\tfor ($j=0; $j<$n; $j++) {\n\t\t$r+=$rc[$i][$j];\n\t\t$c+=$rc[$j][$i];\n\t}\n\tpush @magic, $r, $c;\n\t$d1 += $rc[$i][$i];\n\t$d2 += $rc[$i][$n-$i-1];\n}\n$ans = (grep($_!=$magic, @magic, $d1, $d2)? -1: $lost);\nprint $ans\n"}, {"source_code": "$n = <>;\nwhile (<>) {\n\t@v = split; push @rc, [@v];\n\t$sum = 0; \n\tfor ($k=0; $k<$n; $k++) {\n\t\tif ($v[$k]) {\n\t\t\t$sum += $v[$k]; \n\t\t} else {\n\t\t\t$i=$.-2; $j=$k;\n\t\t}\n\t}\n\tpush @sum, $sum;\n}\n$lost = abs($sum[0]-$sum[1]) || 123456789;\n$rc[$i][$j] = $lost;\n$magic = ($sum[1]>$sum[0]? $sum[1]: $sum[0]) || $lost;\nfor ($i=0; $i<$n; $i++) {\n\t$r=0; $c=0;\n\tfor ($j=0; $j<$n; $j++) {\n\t\t$r+=$rc[$i][$j];\n\t\t$c+=$rc[$j][$i];\n\t}\n\tpush @magic, $r, $c;\n\t$d1 += $rc[$i][$i];\n\t$d2 += $rc[$i][$n-$i-1];\n}\n$ans = (grep($_!=$magic, @magic, $d1, $d2)? -1: $lost);\nprint $ans\n"}, {"source_code": "$\\ = $/;\n\n$n = <>;\nmap { push @_, [ split ] } <>;\n\nif( $n == 1 ){\n\tprint 1;\n\t}\nelse{\n\t$sum = 0;\n\t$zero = 0;\n\tfor( @_ ){\n\t\tif( !$zero and grep !$_, @{$_} ){\n\t\t\t$zero = eval join '+', @{$_};\n\t\t\t}\n\t\tif( !$sum and not grep !$_, @{$_} ){\n\t\t\t$sum = eval join '+', @{$_};\n\t\t\t}\n\t\t}\n\t\n\tfor( @_ ){\n\t\tfor (@{$_}){\n\t\t\t$_ ||= $sum - $zero;\n\t\t\t}\n\t\t}\n\t\t\n\tfor( @_ ){\n\t\t$h{ eval join '+', @{$_} } ++;\n\t\t}\n\t\t\n\tfor $i ( 0 .. @_ - 1 ){\n\t\t$h{ eval join '+', map { @{$_}[$i] } @_ } ++;\n\t\t}\n\t\t\n\tmap { \n\t\t$i = 0; $s = 0;\n\t\tfor( @{$_} ){\n\t\t\t$s += @{$_}[$i++];\n\t\t\t}\n\t\t$h{$s} ++;\n\t} \\@_, [ reverse @_ ];\n\t\n\tprint +(1 == keys %h and $sum - $zero) ? $sum - $zero : -1;\n\t}"}, {"source_code": "$\\ = $/;\n\n$n = <>;\nmap { push @_, [ split ] } <>;\n\nif( $n == 1 ){\n\tprint 1;\n\t}\nelse{\n\t$sum = 0;\n\t$zero = 0;\n\tfor( @_ ){\n\t\tif( !$zero and grep !$_, @{$_} ){\n\t\t\t$zero = eval join '+', @{$_};\n\t\t\t$i = 0;\n\t\t\tfor (@{$_}){\n\t\t\t\t$_ == 0 and last;\n\t\t\t\t$i ++;\n\t\t\t\t}\n\t\t\t}\n\t\tif( !$sum and not grep !$_, @{$_} ){\n\t\t\t$sum = eval join '+', @{$_};\n\t\t\t}\n\t\t}\n\n\t$vzero = 0;\n\tfor (@_){\n\t\t$vzero += @{$_}[$i];\n\t\t}\n\t\n\tprint $vzero == $zero ? $sum - $zero : -1;\n\t}"}, {"source_code": "$\\ = $/;\n\n$n = <>;\nmap { push @_, [ split ] } <>;\n\nif( $n == 1 ){\n\tprint 1;\n\t}\nelse{\n\t$sum = 0;\n\t$zero = 0;\n\tfor( @_ ){\n\t\tif( !$zero and grep !$_, @{$_} ){\n\t\t\t$zero = eval join '+', @{$_};\n\t\t\t$i = 0;\n\t\t\tfor (@{$_}){\n\t\t\t\t$_ == 0 and last;\n\t\t\t\t$i ++;\n\t\t\t\t}\n\t\t\t}\n\t\tif( !$sum and not grep !$_, @{$_} ){\n\t\t\t$sum = eval join '+', @{$_};\n\t\t\t}\n\t\t}\n\n\t$vzero = 0;\n\tfor (@_){\n\t\t$vzero += @{$_}[$i];\n\t\t}\n\t\n\tprint +($vzero == $zero and $sum - $zero) ? $sum - $zero : -1;\n\t}"}], "src_uid": "3bd5a228ed5faf997956570f96becd73"} {"nl": {"description": "Another Codeforces Round has just finished! It has gathered $$$n$$$ participants, and according to the results, the expected rating change of participant $$$i$$$ is $$$a_i$$$. These rating changes are perfectly balanced\u00a0\u2014 their sum is equal to $$$0$$$.Unfortunately, due to minor technical glitches, the round is declared semi-rated. It means that all rating changes must be divided by two.There are two conditions though: For each participant $$$i$$$, their modified rating change $$$b_i$$$ must be integer, and as close to $$$\\frac{a_i}{2}$$$ as possible. It means that either $$$b_i = \\lfloor \\frac{a_i}{2} \\rfloor$$$ or $$$b_i = \\lceil \\frac{a_i}{2} \\rceil$$$. In particular, if $$$a_i$$$ is even, $$$b_i = \\frac{a_i}{2}$$$. Here $$$\\lfloor x \\rfloor$$$ denotes rounding down to the largest integer not greater than $$$x$$$, and $$$\\lceil x \\rceil$$$ denotes rounding up to the smallest integer not smaller than $$$x$$$. The modified rating changes must be perfectly balanced\u00a0\u2014 their sum must be equal to $$$0$$$. Can you help with that?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 13\\,845$$$), denoting the number of participants. Each of the next $$$n$$$ lines contains a single integer $$$a_i$$$ ($$$-336 \\le a_i \\le 1164$$$), denoting the rating change of the $$$i$$$-th participant. The sum of all $$$a_i$$$ is equal to $$$0$$$.", "output_spec": "Output $$$n$$$ integers $$$b_i$$$, each denoting the modified rating change of the $$$i$$$-th participant in order of input. For any $$$i$$$, it must be true that either $$$b_i = \\lfloor \\frac{a_i}{2} \\rfloor$$$ or $$$b_i = \\lceil \\frac{a_i}{2} \\rceil$$$. The sum of all $$$b_i$$$ must be equal to $$$0$$$. If there are multiple solutions, print any. We can show that a solution exists for any valid input.", "sample_inputs": ["3\n10\n-5\n-5", "7\n-7\n-29\n0\n3\n24\n-29\n38"], "sample_outputs": ["5\n-2\n-3", "-3\n-15\n0\n2\n12\n-15\n19"], "notes": "NoteIn the first example, $$$b_1 = 5$$$, $$$b_2 = -3$$$ and $$$b_3 = -2$$$ is another correct solution.In the second example there are $$$6$$$ possible solutions, one of them is shown in the example output."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nuse integer;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\t@_ = map ~~<>, 1 .. $_;\n\tchomp @_;\n\t\n\t$debug and print for @_;\n\t\n\tmy $negative = grep { /-/ and $_ % 2 } @_;\n\tmy $positive = grep { !/-/ and $_ % 2 } @_;\n\t\n\tmy $diff = $positive - $negative;\n\t\n\t$debug and print \"diff: [$diff]\";\n\t\n\tif( $diff > 0 ){\n\t\tfor( @_ ){\n\t\t\tif( !/-/ and $_ % 2 and $diff > 0 ){\n\t\t\t\t$_ /= 2;\n\t\t\t\t$_ ++;\n\t\t\t\t$diff -= 2;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$_ /= 2;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telsif( $diff <= 0 ){\n\t\tfor( @_ ){\n\t\t\tif( /-/ and $_ % 2 and $diff < 0 ){\n\t\t\t\t$_ /= 2;\n\t\t\t\t$_ --;\n\t\t\t\t$diff += 2;\n\t\t\t\t}\n\t\t\telse{\n\t\t\t\t$_ /= 2;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tmy $sum = 0;\n\t$sum += $_ for @_;\n\t\n\t$debug and print \"sum: [$sum]\";\n\t\n\tprint for @_;\n\t}"}], "negative_code": [], "src_uid": "3545385c183c29f9b95aa0f02b70954f"} {"nl": {"description": "Polycarp studies at the university in the group which consists of n students (including himself). All they are registrated in the social net \"TheContacnt!\".Not all students are equally sociable. About each student you know the value ai \u2014 the maximum number of messages which the i-th student is agree to send per day. The student can't send messages to himself. In early morning Polycarp knew important news that the programming credit will be tomorrow. For this reason it is necessary to urgently inform all groupmates about this news using private messages. Your task is to make a plan of using private messages, so that: the student i sends no more than ai messages (for all i from 1 to n); all students knew the news about the credit (initially only Polycarp knew it); the student can inform the other student only if he knows it himself. Let's consider that all students are numerated by distinct numbers from 1 to n, and Polycarp always has the number 1.In that task you shouldn't minimize the number of messages, the moment of time, when all knew about credit or some other parameters. Find any way how to use private messages which satisfies requirements above. ", "input_spec": "The first line contains the positive integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of students. The second line contains the sequence a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009100), where ai equals to the maximum number of messages which can the i-th student agree to send. Consider that Polycarp always has the number 1.", "output_spec": "Print -1 to the first line if it is impossible to inform all students about credit. Otherwise, in the first line print the integer k \u2014 the number of messages which will be sent. In each of the next k lines print two distinct integers f and t, meaning that the student number f sent the message with news to the student number t. All messages should be printed in chronological order. It means that the student, who is sending the message, must already know this news. It is assumed that students can receive repeated messages with news of the credit. If there are several answers, it is acceptable to print any of them. ", "sample_inputs": ["4\n1 2 1 0", "6\n2 0 1 3 2 0", "3\n0 2 2"], "sample_outputs": ["3\n1 2\n2 4\n2 3", "6\n1 3\n3 4\n1 2\n4 5\n5 6\n4 6", "-1"], "notes": "NoteIn the first test Polycarp (the student number 1) can send the message to the student number 2, who after that can send the message to students number 3 and 4. Thus, all students knew about the credit. "}, "positive_code": [{"source_code": "use strict;\nuse warnings;\n\nsub next_student_id\n{\n\tmy $students_ref = shift;\n\n\tmy ($id, $msg_cnt, $left_cnt);\n\tfor (my $i = 0; $i < @{ $students_ref }; $i++) {\n\t\tnext if $students_ref->[$i]->{notified};\n\n\t\tif (not $id or $msg_cnt < $students_ref->[$i]->{msg_cnt}) {\n\t\t\t$msg_cnt = $students_ref->[$i]->{msg_cnt};\n\t\t\t$id = $i;\n\t\t}\n\n\t\t$left_cnt++;\n\t}\n\n\treturn ($id, $left_cnt);\n}\n\nsub send_notifications\n{\n\tmy ($sender_id, $students_ref) = @_;\n\n\t$students_ref->[$sender_id]->{notified} = 1;\n\n\tmy ($sent_cnt, $sequence) = (0, \"\");\n\twhile ($students_ref->[$sender_id]->{msg_cnt} != 0) {\n\t\tmy ($receiver_id, $left_cnt) = next_student_id($students_ref);\n\t\treturn ($sent_cnt, $sequence, 1) unless defined $receiver_id;\n\n\t\t$students_ref->[$sender_id]->{msg_cnt}--;\n\t\t$left_cnt--;\n\n\t\t$sequence .= sprintf \"%u %u\\n\", $sender_id+1, $receiver_id+1;\n\t\t$sent_cnt++;\n\n\t\tmy ($new_sent_cnt, $new_sequence, $stop) = send_notifications($receiver_id, $students_ref);\n\t\t$sent_cnt += $new_sent_cnt;\n\t\t$sequence .= $new_sequence;\n\n\t\treturn ($sent_cnt, $sequence, 1)\n\t\t\tif $stop or $left_cnt == 0;\n\t}\n\n\treturn ($sent_cnt, $sequence);\n}\n\nmy $n = <> + 0;\nmy @students = map { { msg_cnt => $_ } } split /\\s+/, <>;\n\nmy ($msg_cnt, $sequence, $done) = send_notifications(0, \\@students);\nif (not $done) {\n\tprint \"-1\\n\";\n} else {\n\tprint \"$msg_cnt\\n$sequence\";\n}\n"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\n\nsub next_student_id\n{\n\tmy $students_ref = shift;\n\n\tmy ($id, $msg_cnt);\n\tfor (my $i = 0; $i < @{ $students_ref }; $i++) {\n\t\tnext if $students_ref->[$i]->{notified};\n\n\t\tif (not $id or $msg_cnt < $students_ref->[$i]->{msg_cnt}) {\n\t\t\t$msg_cnt = $students_ref->[$i]->{msg_cnt};\n\t\t\t$id = $i;\n\t\t}\n\t}\n\n\treturn $id;\n}\n\nsub send_notifications\n{\n\tmy ($sender_id, $students_ref) = @_;\n\n\tmy ($sent_cnt, $sequence) = (0, \"\");\n\twhile ($students_ref->[$sender_id]->{msg_cnt} != 0) {\n\t\tmy $receiver_id = next_student_id($students_ref);\n\t\treturn ($sent_cnt, $sequence, 1) unless defined $receiver_id;\n\n\t\t$students_ref->[$receiver_id]->{notified} = 1;\n\t\t$students_ref->[$sender_id]->{msg_cnt}--;\n\t\t$sequence .= sprintf \"%u %u\\n\", $sender_id+1, $receiver_id+1;\n\t\t$sent_cnt++;\n\n\t\tmy ($new_sent_cnt, $new_sequence, $stop) = send_notifications($receiver_id, $students_ref);\n\t\t$sent_cnt += $new_sent_cnt;\n\t\t$sequence .= $new_sequence;\n\n\t\treturn ($sent_cnt, $sequence, 1) if $stop;\n\t}\n\n\treturn ($sent_cnt, $sequence);\n}\n\nmy $n = <> + 0;\nmy @students = map { { msg_cnt => $_ } } split /\\s+/, <>;\n\n$students[0]->{notified} = 1; # policarp\n\nmy ($msg_cnt, $sequence, $done) = send_notifications(0, \\@students);\nif (not $done) {\n\tprint \"-1\\n\";\n} else {\n\tprint \"$msg_cnt\\n$sequence\";\n}\n"}], "src_uid": "3ebb3a4f01345145f8f9817f7c77185e"} {"nl": {"description": "Alice's hair is growing by leaps and bounds. Maybe the cause of it is the excess of vitamins, or maybe it is some black magic...To prevent this, Alice decided to go to the hairdresser. She wants for her hair length to be at most $$$l$$$ centimeters after haircut, where $$$l$$$ is her favorite number. Suppose, that the Alice's head is a straight line on which $$$n$$$ hairlines grow. Let's number them from $$$1$$$ to $$$n$$$. With one swing of the scissors the hairdresser can shorten all hairlines on any segment to the length $$$l$$$, given that all hairlines on that segment had length strictly greater than $$$l$$$. The hairdresser wants to complete his job as fast as possible, so he will make the least possible number of swings of scissors, since each swing of scissors takes one second.Alice hasn't decided yet when she would go to the hairdresser, so she asked you to calculate how much time the haircut would take depending on the time she would go to the hairdresser. In particular, you need to process queries of two types: $$$0$$$\u00a0\u2014 Alice asks how much time the haircut would take if she would go to the hairdresser now. $$$1$$$ $$$p$$$ $$$d$$$\u00a0\u2014 $$$p$$$-th hairline grows by $$$d$$$ centimeters. Note, that in the request $$$0$$$ Alice is interested in hypothetical scenario of taking a haircut now, so no hairlines change their length.", "input_spec": "The first line contains three integers $$$n$$$, $$$m$$$ and $$$l$$$ ($$$1 \\le n, m \\le 100\\,000$$$, $$$1 \\le l \\le 10^9$$$)\u00a0\u2014 the number of hairlines, the number of requests and the favorite number of Alice. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\le a_i \\le 10^9$$$)\u00a0\u2014 the initial lengths of all hairlines of Alice. Each of the following $$$m$$$ lines contains a request in the format described in the statement. The request description starts with an integer $$$t_i$$$. If $$$t_i = 0$$$, then you need to find the time the haircut would take. Otherwise, $$$t_i = 1$$$ and in this moment one hairline grows. The rest of the line than contains two more integers: $$$p_i$$$ and $$$d_i$$$ ($$$1 \\le p_i \\le n$$$, $$$1 \\le d_i \\le 10^9$$$)\u00a0\u2014 the number of the hairline and the length it grows by.", "output_spec": "For each query of type $$$0$$$ print the time the haircut would take.", "sample_inputs": ["4 7 3\n1 2 3 4\n0\n1 2 3\n0\n1 1 3\n0\n1 3 1\n0"], "sample_outputs": ["1\n2\n2\n1"], "notes": "NoteConsider the first example: Initially lengths of hairlines are equal to $$$1, 2, 3, 4$$$ and only $$$4$$$-th hairline is longer $$$l=3$$$, and hairdresser can cut it in $$$1$$$ second. Then Alice's second hairline grows, the lengths of hairlines are now equal to $$$1, 5, 3, 4$$$ Now haircut takes two seonds: two swings are required: for the $$$4$$$-th hairline and for the $$$2$$$-nd. Then Alice's first hairline grows, the lengths of hairlines are now equal to $$$4, 5, 3, 4$$$ The haircut still takes two seconds: with one swing hairdresser can cut $$$4$$$-th hairline and with one more swing cut the segment from $$$1$$$-st to $$$2$$$-nd hairline. Then Alice's third hairline grows, the lengths of hairlines are now equal to $$$4, 5, 4, 4$$$ Now haircut takes only one second: with one swing it is possible to cut the segment from $$$1$$$-st hairline to the $$$4$$$-th. "}, "positive_code": [{"source_code": "use strict;\nuse warnings;\n\nmy $input = ;\nmy ($n, $m, $l) = map {$_+0} split / /, $input;\n$input = ;\nmy @hairs = map {$_+0} split / /, $input;\nmy @res = ();\nmy $counter = 0;\nmy $r = 0;\nfor my $i (0 .. $m - 1) {\n $input = ;\n my @req = map {$_+0} (split(/ /, $input));\n if (scalar @req == 1 && $req[0] == 0) {\n unless (@res) {\n $r = f(\\@hairs, $n, $l);\n }\n push @res, $r;\n } elsif (scalar @req == 3 && $req[0] == 1) {\n my $j = $req[1] - 1;\n my $d = $hairs[$j] + $req[2];\n if (scalar @res > 0 && $hairs[$j] <= $l && $d > $l) {\n if ($j == 0 && $n > 1 && $hairs[1] <= $l) {\n $r++;\n } elsif ($j == $n-1 && $n > 1 && $hairs[$n-2] <= $l) {\n $r++;\n } elsif ($j != $n-1 && $j != 0 && $hairs[$j-1] <= $l && $hairs[$j+1] <= $l) {\n $r++;\n } elsif ($j != $n-1 && $j != 0 && $hairs[$j-1] > $l && $hairs[$j+1] > $l) {\n $r--;\n } elsif ($n == 1) {\n $r++;\n }\n }\n $hairs[$req[1]-1] = $d;\n }\n}\n\nmy $j = scalar @res;\nfor my $i (0..$j-1) {\n print $res[$i];\n print \"\\n\" if $i != $j-1;\n}\n\nsub f {\n my $hairs = shift;\n my $n = shift;\n my $l = shift;\n my $counter = 0;\n my $flag = 0;\n for my $i (0..$n-1) {\n if ($hairs->[$i] > $l) {\n $flag = 1;\n } else {\n $counter++ if $flag == 1;\n $flag = 0;\n }\n }\n $counter++ if ($flag == 1);\n return $counter;\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m, $l ) = split;\n\tmy @A = split ' ', <>;\n\t\n\tpush @A, -( 1e5 + 1 ) * 1e9;\n\tunshift @A, -( 1e5 + 1 ) * 1e9;\n\t\n#%\tprint \"@A\";\n\t\n\tmy $more = 0;\n\tmy $cnt = 0;\n\t\n\tfor( @A ){\n\t\tif( $_ > $l ){\n\t\t\tif( !$more ){ $cnt ++ }\n\t\t\t$more = 1;\n\t\t\t}\n\t\telse{\n\t\t\t$more = 0;\n\t\t\t}\n\t\t}\n\t\n\tfor( 1 .. $m ){\n\t\tmy( $t, $p, $d ) = split ' ', <>;\n\t\tif( $t == 0 ){\n\t\t\tprint $cnt;\n\t\t\t}\n\t\telse{\n\t\t\tif( $A[ $p ] <= $l and $A[ $p ] + $d > $l ){\n\t\t\t\tif( $A[ $p - 1 ] > $l and $A[ $p + 1 ] > $l ){\n\t\t\t\t\t$cnt --;\n\t\t\t\t\t}\n\t\t\t\tif( $A[ $p - 1 ] <= $l and $A[ $p + 1 ] <= $l ){\n\t\t\t\t\t$cnt ++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t$A[ $p ] += $d;\n\t\t\t}\n\t\t}\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\n( $n, $m, $l, @_ ) = ( ( split ' ', <> ), -~0, ( split ' ', <> ), -~0 );\n\n$x = $_ > $l ? ( $c += !$x, 1 ) : 0 for @_;\n\nfor( 1 .. $m ){\n\t( $t, $p, $d ) = split ' ', <>;\n\tif( $t ){\n\t\tif( 1 == grep $_[ $p ] + $_ > $l, 0, $d ){\n\t\t\t$c += 1 <=> grep $_ > $l, @_[ $p - 1, $p + 1 ];\n\t\t\t}\n\t\t$_[ $p ] += $d;\n\t\t}\n\telse{\n\t\tprint 0 + $c;\n\t\t}\n\t}"}], "negative_code": [{"source_code": "use strict;\nuse warnings;\n\nmy $input = ;\nmy ($n, $m, $l) = map {$_+0} split / /, $input;\n$input = ;\nmy @hairs = map {$_+0} split / /, $input;\nmy @res = ();\nfor my $i (0 .. $m - 1) {\n $input = ;\n my @req = map {$_+0} (split(/ /, $input));\n if (scalar @req == 1 && $req[0] == 0) {\n push @res, f(\\@hairs, $n, $l);\n } elsif (scalar @req == 3 && $req[0] == 1) {\n $hairs[$req[1]-1] += $hairs[$req[2]];\n }\n}\n\nmy $j = scalar @res;\nfor my $i (0..$j-1) {\n print $res[$i];\n print \"\\n\" if $i != $j-1;\n}\n\nsub f {\n my $hairs = shift;\n my $n = shift;\n my $l = shift;\n my $counter = 0;\n my $flag = 0;\n for my $i (0..$n-1) {\n if ($hairs->[$i] > $l) {\n $flag = 1;\n } else {\n $counter++ if $flag == 1;\n $flag = 0;\n }\n }\n $counter++ if ($flag == 1);\n return $counter;\n}"}, {"source_code": "use strict;\nuse warnings;\n\nmy $input = ;\nmy ($n, $m, $l) = map {$_+0} split / /, $input;\n$input = ;\nmy @hairs = map {$_+0} split / /, $input;\nmy @res = ();\nmy $counter = 0;\nmy $r = 0;\nfor my $i (0 .. $m - 1) {\n $input = ;\n my @req = map {$_+0} (split(/ /, $input));\n if (scalar @req == 1 && $req[0] == 0) {\n unless (@res) {\n $r = f(\\@hairs, $n, $l);\n }\n push @res, $r;\n } elsif (scalar @req == 3 && $req[0] == 1) {\n my $j = $req[1] - 1;\n my $d = $hairs[$j] + $req[2];\n if (scalar @res > 0 && $hairs[$j] <= $l && $d > $l) {\n if ($j == 0 && $n > 1 && $hairs[$j+1] <= $l) {\n $r++;\n } elsif ($j == $n-1 && $n > 1 && $hairs[$j-1] <= $l) {\n $r++;\n } elsif ($j != $n-1 && $j != 0 && $hairs[$j-1] <= $l && $hairs[$j+1] <= $l) {\n $r++;\n } elsif ($j != $n-1 && $j != 0 && $hairs[$j-1] > $l && $hairs[$j+1] > $l) {\n $r--;\n }\n }\n $hairs[$req[1]-1] = $d;\n }\n}\n\nmy $j = scalar @res;\nfor my $i (0..$j-1) {\n print $res[$i];\n print \"\\n\" if $i != $j-1;\n}\n\nsub f {\n my $hairs = shift;\n my $n = shift;\n my $l = shift;\n my $counter = 0;\n my $flag = 0;\n for my $i (0..$n-1) {\n if ($hairs->[$i] > $l) {\n $flag = 1;\n } else {\n $counter++ if $flag == 1;\n $flag = 0;\n }\n }\n $counter++ if ($flag == 1);\n return $counter;\n}"}, {"source_code": "use strict;\nuse warnings;\n\nmy $input = ;\nmy ($n, $m, $l) = map {$_+0} split / /, $input;\n$input = ;\nmy @hairs = map {$_+0} split / /, $input;\nmy @res = ();\nfor my $i (0 .. $m - 1) {\n $input = ;\n my @req = map {$_+0} (split(/ /, $input));\n if (scalar @req == 1) {\n push @res, f(\\@hairs, $n, $l);\n } else {\n $hairs[$req[1]-1] += $hairs[$req[2]];\n }\n}\n\nmy $j = scalar @res;\nfor my $i (0..$j-1) {\n print $res[$i];\n print \"\\n\" if $i != $j-1;\n}\n\nsub f {\n my $hairs = shift;\n my $n = shift;\n my $l = shift;\n my $counter = 0;\n my $flag = 0;\n for my $i (0..$n-1) {\n if ($hairs->[$i] > $l) {\n $flag = 1;\n } else {\n $counter++ if $flag == 1;\n $flag = 0;\n }\n }\n $counter++ if ($flag == 1);\n return $counter;\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m, $l ) = split;\n\tmy @A = split ' ', <>;\n\t\n\tpush @A, -( 1e5 + 1 ) * 1e9;\n\tunshift @A, -( 1e5 + 1 ) * 1e9;\n\t\n\tprint \"@A\";\n\t\n\tmy $more = 0;\n\tmy $cnt = 0;\n\t\n\tfor( @A ){\n\t\tif( $_ > $l ){\n\t\t\tif( !$more ){ $cnt ++ }\n\t\t\t$more = 1;\n\t\t\t}\n\t\telse{\n\t\t\t$more = 0;\n\t\t\t}\n\t\t}\n\t\n\tfor( 1 .. $m ){\n\t\tmy( $t, $p, $d ) = split ' ', <>;\n\t\tif( $t == 0 ){\n\t\t\tprint $cnt;\n\t\t\t}\n\t\telse{\n\t\t\tif( $A[ $p ] <= $l and $A[ $p ] + $d > $l ){\n\t\t\t\tif( $A[ $p - 1 ] > $l and $A[ $p + 1 ] > $l ){\n\t\t\t\t\t$cnt --;\n\t\t\t\t\t}\n\t\t\t\tif( $A[ $p - 1 ] <= $l and $A[ $p + 1 ] <= $l ){\n\t\t\t\t\t$cnt ++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t$A[ $p ] += $d;\n\t\t\t}\n\t\t}\n\t\n\t}"}, {"source_code": "$\\ = $/;\n\n( $n, $m, $l, @_ ) = ( ( split ' ', <> ), -~0, ( split ' ', <> ), -~0 );\n\n$x = $_ > $l ? ( $c += !$x, 1 ) : 0 for @_;\n\nfor( 1 .. $m ){\n\t( $t, $p, $d ) = split ' ', <>;\n\tif( $t ){\n\t\tif( 1 == grep $_[ $p ] + $_ > $l, 0, $d ){\n\t\t\t$c += 1 <=> grep $_ > $l, @_[ $p - 1, $p + 1 ];\n\t\t\t}\n\t\t$_[ $p ] += $d;\n\t\t}\n\telse{\n\t\tprint $c;\n\t\t}\n\t}"}], "src_uid": "1e17039ed5c48e5b56314a79b3811a40"} {"nl": {"description": "Om Nom is the main character of a game \"Cut the Rope\". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him. The park consists of 2n\u2009+\u20091\u2009-\u20091 squares connected by roads so that the scheme of the park is a full binary tree of depth n. More formally, the entrance to the park is located at the square 1. The exits out of the park are located at squares 2n,\u20092n\u2009+\u20091,\u2009...,\u20092n\u2009+\u20091\u2009-\u20091 and these exits lead straight to the Om Nom friends' houses. From each square i (2\u2009\u2264\u2009i\u2009<\u20092n\u2009+\u20091) there is a road to the square . Thus, it is possible to go from the park entrance to each of the exits by walking along exactly n roads. To light the path roads in the evening, the park keeper installed street lights along each road. The road that leads from square i to square has ai lights.Om Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe. He asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads.", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u200910) \u2014 the number of roads on the path from the entrance to any exit. The next line contains 2n\u2009+\u20091\u2009-\u20092 numbers a2,\u2009a3,\u2009... a2n\u2009+\u20091\u2009-\u20091 \u2014 the initial numbers of street lights on each road of the park. Here ai is the number of street lights on the road between squares i and . All numbers ai are positive integers, not exceeding 100.", "output_spec": "Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe.", "sample_inputs": ["2\n1 2 3 4 5 6"], "sample_outputs": ["5"], "notes": "NotePicture for the sample test. Green color denotes the additional street lights. "}, "positive_code": [{"source_code": "use strict;\nuse warnings;\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);;\nour $ans = 0;\nmy $n = <>;\n\nmy @elm = split / /,<>;\nunshift @elm ,\"www\";\nreet(@elm);\nprint $ans;\nsub reet\n{\n my @a = @_;\n my $i = $#a;\n if ($i <= 1)\n {\n return;\n }\n my @b = @a[0..($#a - 1)/2];\n while ($i > $#a/2)\n {\n #print \"$i : \",$a[$i],\" \";\n $b[($i - 1)/2] += max($a[$i], $a[$i - 1]);\n $ans += abs($a[$i] - $a[$i - 1]);\n $i -= 2;\n }\n \n reet(@b);\n}"}, {"source_code": " #!perl\n \n $\\ =\"\\n\";\n \n sub countPath\n {\n my @result;\n if($_[1] > $_[0]) \n {\n push @result, $_[1] - $_[0];\n push @result, $_[1];\n }\n else \n {\n push @result, $_[0] - $_[1];\n push @result, $_[0];\n }\n \n return @result;\n }\n \n sub count\n {\n my $link = $_[0];\n my @a = @{$link};\n \n if($_[2] > $_[3])\n {\n return countPath(@a[$_[1]], @a[$_[2]]);\n }\n else\n {\n my @left = count(\\@a, $_[1]*2 + 2, $_[1]*2 + 3, $_[3]);\n my @right = count(\\@a, $_[2]*2 + 2, $_[2]*2 + 3, $_[3]);\n \n my @result = countPath(@left[1] + @a[$_[1]], @right[1] + @a[$_[2]]);\n \n return (@result[0] + @left[0] + @right[0], @result[1]);\n }\n }\n \n $n = <>;\n my @lamps = split / /, <>;\n \n @answer = count(\\@lamps, 0, 1, $#lamps - 2**$n);\n\n print @answer[0];"}], "negative_code": [{"source_code": " #!perl\n \n $\\ =\"\\n\";\n \n sub count\n {\n my $link = shift;\n my @a = @{$link};\n \n if($#a == 1)\n {\n return @a;\n }\n else\n {\n my $first = shift @a;\n my $second = shift @a;\n \n my $p = ($#a + 1)/2 - 1;\n my @branch_1 = @a[0..$p];\n my @branch_2 = @a[($p + 1)..$#a];\n \n for my $i (@branch_1)\n {\n $i += $first;\n }\n \n for my $j (@branch_2)\n {\n $j += $second;\n }\n \n return (count(\\@branch_1), count(\\@branch_2));\n }\n }\n \n sub find_answer\n {\n my $link = shift;\n my @a = @{$link};\n my $m = shift;\n \n if($#a == 1)\n {\n my $left = $m - @a[0];\n my $right = $m - @a[1];\n \n return $left > $right ? $left : $right;\n }\n else\n {\n my $first = shift @a;\n my $second = shift @a;\n \n my $p = ($#a + 1)/2 - 1;\n my @branch_1 = @a[0..$p];\n my @branch_2 = @a[($p + 1)..$#a];\n \n my $left = find_answer(\\@branch_1, $m) - $first;\n my $right = find_answer(\\@branch_2, $m) - $second;\n \n return $left + $right;\n }\n }\n \n $n = <>;\n @lamps = split / /, <>;\n \n @paths = count(\\@lamps);\n \n $max = 0;\n for my $item (@paths)\n {\n $max = $item if $max < $item;\n }\n \n print find_answer(\\@lamps, $max);"}], "src_uid": "ae61e1270eeda8c30effc9ed999bf531"} {"nl": {"description": "Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters \"1\" (the table is occupied) and \"0\" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings \"10010001\", \"10000010\", \"00000000\", \"00100000\" satisfy the rules of the restaurant; strings \"10100100\", \"10011001\", \"11111111\" do not satisfy to the rules of the restaurant, since each of them has a pair of \"1\" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without \"1\" or a string with one \"1\", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of \"0\" that can be replaced by \"1\" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$\u00a0\"100010\", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 2\\cdot 10^5$$$)\u00a0\u2014 the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of \"0\" and \"1\"\u00a0\u2014 a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant\u00a0\u2014 the difference between indices of any two \"1\" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2\\cdot 10^5$$$.", "output_spec": "For each test case output one integer\u00a0\u2014 the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$.", "sample_inputs": ["6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0"], "sample_outputs": ["1\n2\n0\n1\n1\n1"], "notes": "NoteThe first test case is explained in the statement.In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table.In the third test case, you cannot take any free table without violating the rules of the restaurant."}, "positive_code": [{"source_code": "#!/usr/bin/perl\nmy ($t) = map { $_ - 0 } split(/[\\s]+/,);\nfor(my $t1=0;$t1<$t;$t1++){\n my ($n,$k) = map { $_ - 0 } split(/[\\s]+/,);\n my $s = scalar();\n my @a = ();\n $#a = $n - 1;\n my $o1 = 0;\n my $o2 = 0;\n for(my $i=0;$i<$n;$i++){\n if( substr($s,$i,1) eq '1' ){\n $o1 = $k;\n $a[$i] = 1;\n } else {\n $o1 --;\n $a[$i] = 1 if $o1 >= 0;\n }\n if( substr($s,$n-1-$i,1) eq '1' ){\n $o2 = $k;\n $a[$n-1-$i] = 1;\n } else {\n $o2 --;\n $a[$n-1-$i] = 1 if $o2 >= 0;\n }\n }\n \n my $tot = 0;\n my $run = 0;\n for(my $i=0;$i<$n;$i++){\n if( $a[$i] != 1 ){\n $tot++ if $run % ($k+1) == 0;\n $run ++ ;\n } else {\n $run = 0;\n }\n }\n print \"$tot\\n\";\n \n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $k ) = split;\n\t$_ = <>, chomp;\n\t\n\tmy @k = partition( $k );\n\t\n\tmy $k_re = join '', map { \"0{0,$_}\" } @k;\n\t\n\t$debug and print \"k_re:[$k_re]\";\n\t\n\ts/1 \\K ( $k_re ) / \"1\" x length $1 /gex;\n\t\n\t$_ = reverse;\n\t\n\ts/1 \\K ( $k_re ) / \"1\" x length $1 /gex;\n\t\n\t$_ = reverse;\n\t\n\t$debug and print \"[$_]\";\n\t\n\tmy @free_rows = grep $_, map length, split /1+/, $_;\n\t\n\t$debug and print \"free_rows[@free_rows]\";\n\t\n\tmy $ans = 0;\n\t\n\tfor my $i ( @free_rows ){\n\t\twhile( $i >= $k + 1 ){\n\t\t\t$ans ++;\n\t\t\t$i -= $k + 1;\n\t\t\t}\n\t\t$i and $ans ++;\n\t\t}\n\t\n\tprint $ans;\n\t}\n\nsub partition {\n\tmy $i = shift;\n\t\n\tmy @arr;\n\tmy $max = 1e4;\n\t\n\twhile( $i > $max ){\n\t\tpush @arr, $max;\n\t\t$i -= $max;\n\t\t}\n\t\n\t$i > 0 and push @arr, $i;\n\t\n\t@arr;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $k ) = split;\n\t$_ = <>, chomp;\n\t\n\tmy @k = partition( $k );\n\t\n\tmy $k_re = join '|', map { \"0{0,$_}\" } @k;\n\t\n\t$debug and print \"k_re:[$k_re]\";\n\t\n\ts/1 \\K ( $k_re ) / \"1\" x length $1 /gex;\n\t\n\t$_ = reverse;\n\t\n\ts/1 \\K ( $k_re ) / \"1\" x length $1 /gex;\n\t\n\t$_ = reverse;\n\t\n\t$debug and print \"[$_]\";\n\t\n\tmy @free_rows = grep $_, map length, split /1+/, $_;\n\t\n\t$debug and print \"free_rows[@free_rows]\";\n\t\n\tmy $ans = 0;\n\t\n\tfor my $i ( @free_rows ){\n\t\twhile( $i >= $k + 1 ){\n\t\t\t$ans ++;\n\t\t\t$i -= $k + 1;\n\t\t\t}\n\t\t$i and $ans ++;\n\t\t}\n\t\n\tprint $ans;\n\t}\n\nsub partition {\n\tmy $i = shift;\n\t\n\tmy @arr;\n\tmy $max = 1e4;\n\t\n\twhile( $i > $max ){\n\t\tpush @arr, $max;\n\t\t$i -= $max;\n\t\t}\n\t\n\t$i > 0 and push @arr, $i;\n\t\n\t@arr;\n\t}"}], "src_uid": "fd85ebe1dc975a71c72fac7eeb944a4a"} {"nl": {"description": "You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \\text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \\text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \\text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \\text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$)\u00a0\u2014 the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$p_x$$$ and $$$p_y$$$ ($$$-10^5 \\le p_x, p_y \\le 10^5$$$; $$$(p_x, p_y) \\neq (0, 0)$$$)\u00a0\u2014 the coordinates of Planetforces $$$(p_x, p_y)$$$. The second line contains the string $$$s$$$ ($$$1 \\le |s| \\le 10^5$$$: $$$|s|$$$ is the length of string $$$s$$$)\u00a0\u2014 the list of orders. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print \"YES\" if you can delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces. Otherwise, print \"NO\". You can print each letter in any case (upper or lower).", "sample_inputs": ["6\n10 5\nRRRRRRRRRRUUUUU\n1 1\nUDDDRLLL\n-3 -5\nLDLDLDDDR\n1 2\nLLLLUU\n3 -2\nRDULRLLDR\n-1 6\nRUDURUUUUR"], "sample_outputs": ["YES\nYES\nYES\nNO\nYES\nNO"], "notes": "NoteIn the first case, you don't need to modify $$$s$$$, since the given $$$s$$$ will bring you to Planetforces.In the second case, you can delete orders $$$s_2$$$, $$$s_3$$$, $$$s_4$$$, $$$s_6$$$, $$$s_7$$$ and $$$s_8$$$, so $$$s$$$ becomes equal to \"UR\".In the third test case, you have to delete order $$$s_9$$$, otherwise, you won't finish in the position of Planetforces."}, "positive_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t( $X, $Y, $_, %h ) = split ' ', $_ . <>;\r\n\t/.(?{ $h{ $& } ++ })(*F)/;\r\n\t\r\n\tprint\r\n\t\t+ ( $X < 0 ? -$h{L} <= $X : $h{R} >= $X )\r\n\t\t+ ( $Y < 0 ? -$h{D} <= $Y : $h{U} >= $Y )\r\n\t\t> 1 ? \"YES\" : \"NO\"\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $X, $Y ) = split;\n\t$_ = <>, chomp;\n\t\n\tmy $L = () = /L/g;\n\tmy $R = () = /R/g;\n\tmy $D = () = /D/g;\n\tmy $U = () = /U/g;\n\t\n\tmy $ok = 0;\n\t\n\tif( $X >= 0 ){\n\t\t$R >= $X and $ok += 1;\n\t\t}\n\telse{\n\t\t-$L <= $X and $ok += 1;\n\t\t}\n\t\n\tif( $Y >= 0 ){\n\t\t$U >= $Y and $ok += 1;\n\t\t}\n\telse{\n\t\t-$D <= $Y and $ok += 1;\n\t\t}\n\t\n\tprint $ok == 2 ? \"YES\" : \"NO\";\n\t}"}, {"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($px,$py) = map { $_ - 0 } split(/\\s+/,);\r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n my %c = +{ 'U' => 0, 'D'=>0, 'R'=>0, 'L'=>0 };\r\n for(my $i=0;$i 0 and $c{'R'} < $px ) or ( $px < 0 and $c{'L'} < abs($px) ) or \r\n ( $py > 0 and $c{'U'} < $py ) or ( $py < 0 and $c{'D'} < abs($py) ) ){\r\n print \"NO\\n\";\r\n } else {\r\n print \"YES\\n\";\r\n }\r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [], "src_uid": "65a64ea63153fec4d660bad1287169d3"} {"nl": {"description": "n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.", "input_spec": "The first line contains two integers: n and k (2\u2009\u2264\u2009n\u2009\u2264\u2009500, 2\u2009\u2264\u2009k\u2009\u2264\u20091012)\u00a0\u2014 the number of people and the number of wins. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009n) \u2014 powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.", "output_spec": "Output a single integer \u2014 power of the winner.", "sample_inputs": ["2 2\n1 2", "4 2\n3 1 2 4", "6 2\n6 5 3 1 2 4", "2 10000000000\n2 1"], "sample_outputs": ["2", "3", "6", "2"], "notes": "NoteGames in the second sample:3 plays with 1. 3 wins. 1 goes to the end of the line.3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner."}, "positive_code": [{"source_code": "$/ = undef; $_ = <>; ($n, $k, $b, @a) = split;\nfor $a (@a) {\n\tif ($a > $b) {\n\t\t$b = $a; $c = 0;\n\t}\n\tif (++$c == $k) {\n\t\tlast;\n\t}\n}\nprint $b;\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t@_ = split ' ', <>;\n\tmy $pre = $_[ 0 ];\n\tmy $ok = 0;\n\tmy $cnt = 0;\n\t\n\tfor my $i ( 1 .. @_ - 1 ){\n\t\tmy $new = $_[ $i ] > $pre ? $_[ $i ] : $pre;\n\t\tif( $pre == $new ){\n\t\t\t$cnt ++;\n\t\t\t}\n\t\telse{\n\t\t\t$cnt = 1;\n\t\t\t}\n\t\t$cnt == $k and do {\n\t\t\t$ok = 1;\n\t\t\tlast;\n\t\t\t};\n\t#\tprint \" pre: $pre, new: $new\";\n\t\t$pre = $new;\n\t\t}\n\t\n\tprint $pre;\n\t}"}, {"source_code": "<> =~ / /;\n\nfor( split ' ', <> ){\n\t$i ++ and $' == ( $c = 1 + $c * ( $_ <= $p ) ) and last;\n\t$_ > $p and $p = $_;\n\t}\n\nprint 0 + $p"}], "negative_code": [{"source_code": "$/ = undef; $_ = <>; ($n, $k, @a) = split;\nfor $a (@a) {\n\tif ($a > $b) {\n\t\t$b = $a;\n\t}\n\tif (++$c == $k) {\n\t\tlast;\n\t}\n}\nprint $b;\n"}, {"source_code": "<> =~ / /;\n\nfor( split ' ', <> ){\n\tlast if $' == ( $c = 1 + $c * ( $_ <= $p ) );\n\t$_ > $p and $p = $_;\n\t}\n\nprint 0 + $p"}], "src_uid": "8d5fe8eee1cce522e494231bb210950a"} {"nl": {"description": "Happy new year! The year 2020 is also known as Year Gyeongja (\uacbd\uc790\ub144, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.There are two sequences of $$$n$$$ strings $$$s_1, s_2, s_3, \\ldots, s_{n}$$$ and $$$m$$$ strings $$$t_1, t_2, t_3, \\ldots, t_{m}$$$. These strings contain only lowercase letters. There might be duplicates among these strings.Let's call a concatenation of strings $$$x$$$ and $$$y$$$ as the string that is obtained by writing down strings $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, the concatenation of the strings \"code\" and \"forces\" is the string \"codeforces\".The year 1 has a name which is the concatenation of the two strings $$$s_1$$$ and $$$t_1$$$. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.For example, if $$$n = 3, m = 4, s = $$${\"a\", \"b\", \"c\"}, $$$t =$$$ {\"d\", \"e\", \"f\", \"g\"}, the following table denotes the resulting year names. Note that the names of the years may repeat. You are given two sequences of strings of size $$$n$$$ and $$$m$$$ and also $$$q$$$ queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?", "input_spec": "The first line contains two integers $$$n, m$$$ ($$$1 \\le n, m \\le 20$$$). The next line contains $$$n$$$ strings $$$s_1, s_2, \\ldots, s_{n}$$$. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least $$$1$$$ and at most $$$10$$$. The next line contains $$$m$$$ strings $$$t_1, t_2, \\ldots, t_{m}$$$. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least $$$1$$$ and at most $$$10$$$. Among the given $$$n + m$$$ strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer $$$q$$$ ($$$1 \\le q \\le 2\\,020$$$). In the next $$$q$$$ lines, an integer $$$y$$$ ($$$1 \\le y \\le 10^9$$$) is given, denoting the year we want to know the name for.", "output_spec": "Print $$$q$$$ lines. For each line, print the name of the year as per the rule described above.", "sample_inputs": ["10 12\nsin im gye gap eul byeong jeong mu gi gyeong\nyu sul hae ja chuk in myo jin sa o mi sin\n14\n1\n2\n3\n4\n10\n11\n12\n13\n73\n2016\n2017\n2018\n2019\n2020"], "sample_outputs": ["sinyu\nimsul\ngyehae\ngapja\ngyeongo\nsinmi\nimsin\ngyeyu\ngyeyu\nbyeongsin\njeongyu\nmusul\ngihae\ngyeongja"], "notes": "NoteThe first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal."}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n\n# solve\n\nread_token;\nread_token;\n\nmy @a = split q{ }, read_line;\nmy @b = split q{ }, read_line;\nmy $length_of_a = @a;\nmy $length_of_b = @b;\n\nmy $q = read_line;\n\nfor (1..$q) {\n my $year = read_line;\n my $mod_of_a = $year % $length_of_a;\n my $mod_of_b = $year % $length_of_b;\n say \"$a[$mod_of_a-1]$b[$mod_of_b-1]\";\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $m ) = split;\n\t\n\tmy @N = split ' ', <>;\n\tmy @M = split ' ', <>;\n\t\n\tfor( 1 .. <> ){\n\t\t$_ = <>, chomp;\n\t\t\n\t\tprint $N[ ( $_ - 1 ) % @N ] . $M[ ( $_ - 1 ) % @M ]\n\t\t}\n\t\n\t}"}], "negative_code": [], "src_uid": "efa201456f8703fcdc29230248d91c54"} {"nl": {"description": "Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock. The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000)\u00a0\u2014 the number of disks on the combination lock. The second line contains a string of n digits\u00a0\u2014 the original state of the disks. The third line contains a string of n digits\u00a0\u2014 Scrooge McDuck's combination that opens the lock.", "output_spec": "Print a single integer\u00a0\u2014 the minimum number of moves Scrooge McDuck needs to open the lock.", "sample_inputs": ["5\n82195\n64723"], "sample_outputs": ["13"], "notes": "NoteIn the sample he needs 13 moves: 1 disk: 2 disk: 3 disk: 4 disk: 5 disk: "}, "positive_code": [{"source_code": "<>;@a=split'',<>;@b=split'',<>;$x=0;\n$t=abs($a[$_]-$b[$_]) and $x+=($t>5?10-$t:$t) for 0..@a;\nprint $x\n"}, {"source_code": "<>;$a=<>;$b=<>;\n@a=split'',$a;\n@b=split'',$b;\n$x=0;\n$t=abs($a[$_]-$b[$_]) and $x+=($t>5?10-$t:$t) for 0..@a;\nprint $x\n"}, {"source_code": "<>;\n$a=<>;\n$b=<>;\n@a=split'',$a;\n@b=split'',$b;\nfor(0..@a){\n $t=$a[$_]-$b[$_];\n $t=$t<0?-$t:$t;\n $x+=($t>5?10-$t:$t);\n}\nprint $x\n"}, {"source_code": "sub min{\n return $_[1] < $_[0] ? $_[1] : $_[0];\n}\nmy $n = int<>;\nmy @lock = split //, <>;\nmy @arr = split //, <>;\nmy $ans = 0;\nfor my $i(0..$n-1) {\n $ans += min(abs($lock[$i] - $arr[$i]), abs(10 - abs($lock[$i] - $arr[$i])));\n}\nprint $ans . \"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nsub abs {\n\t$a = shift;\n\treturn $a>0 ? $a:-$a;\n};\n\nsub min {\n\t($a, $b) = (shift, shift);\n\treturn $a<$b ? $a:$b;\n};\n\nchomp($len = <>);\nchomp($l1 = <>);\nchomp($l2 = <>);\n$ans = 0;\nforeach (0..$len-1) {\n\t($a, $b) = (substr($l1, $_, 1), substr($l2, $_, 1));\n\t$tmp = abs($a-$b);\n\t$ans += min($tmp, 10-$tmp);\n}\nsay $ans;"}, {"source_code": "my $n = <>;\nmy @a = split '', <>;\nmy @b = split '', <>;\nmy $c = 0;\n\nfor (1..$n) {\n my $a = shift @a;\n my $b = shift @b;\n my $x = ($a - $b) % 10;\n my $y = ($b - $a) % 10;\n my $z = $x < $y ? $x : $y;\n $c += $z;\n}\n\nprint \"$c\\n\";\n"}, {"source_code": "#!perl\n\n<>;\nmy @start_state = split //, <>;\nmy @combination = split //, <>;\n\nmy $answer = 0;\nfor my $i (0..$#start_state - 1)\n{\n (my $x1, my $x2) = @start_state[$i] < @combination[$i] ? (@start_state[$i], @combination[$i]) : (@combination[$i], @start_state[$i]);\n my $a = $x2 - $x1;\n my $b = 9 - $x2 + $x1 + 1;\n $answer += $a < $b ? $a : $b; \n}\n\nprint $answer, \"\\n\";"}, {"source_code": "<>;\n$a = <>;\n($b = abs $_ - chop $a) =~ y/6-9/4321/, $s += $b for reverse <>=~/./gs;\nprint $s"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\nmy $n = <>;\nchomp $n;\nmy $na = <>;\nchomp $na;\nmy $nb = <>;\nchomp $nb;\n\nmy $res = 0;\n\nmy @a = split(//, $na);\nmy @b = split(//, $nb);\n\nfor (my $i = 0; $i < $n; $i++) {\n my ($m, $M);\n my $c1 = abs($a[$i] - $b[$i]);\n my $c2 = 10 - $c1;\n if ($c1 < $c2) {\n $res += $c1;\n } else {\n $res += $c2;\n }\n}\n\nprint \"$res\\n\";\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\nsub min{\n return $_[0] < $_[1] ? $_[0] : $_[1];\n}\nmy $n = <>;\nmy @src = split '',<>;\nmy @dest = split '',<>;\nmy $res = 0;\nforeach my $i(0 .. $n-1){\n my $d = abs($src[$i] - $dest[$i]);\n $res += min($d, 10 - $d);\n}\nprint $res;\n"}, {"source_code": "<>;\n$a = <>;\n($b = abs $_ - chop $a) =~ y/6-9/4321/, $s += $b for reverse <>=~/./gs;\nprint $s"}, {"source_code": "$\\ = $/;\nwhile(<>){\n$sum = 0;\nchomp($a=<>);\nchomp($b=<>);\nfor (1 .. $_){\n$aa = chop $a;\n$bb = chop $b;\n$sum+=(sort {$a<=>$b} abs($aa - $bb), abs(10-abs($aa - $bb)) )[0];\n}\nprint $sum;\n}"}], "negative_code": [{"source_code": "<>;$a=<>;$b=<>;\n@a=split'',$a;\n@b=split'',$b;\n$t=abs($a[$_]-$b[$_]) and $x+=($t>5?10-$t:$t) for 0..@a;\nprint $x\n"}, {"source_code": "<>;\n$a +=<>;\n($b = abs $_ - chop $a) =~ y/6-9/4321/, $s += $b for reverse <>=~/./g;\nprint $s"}], "src_uid": "5adb1cf0529c3d6c93c107cf72fa5e0b"} {"nl": {"description": "You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters.You have to remove at most one (i.e. zero or one) character of this string in such a way that the string you obtain will be lexicographically smallest among all strings that can be obtained using this operation.String $$$s = s_1 s_2 \\dots s_n$$$ is lexicographically smaller than string $$$t = t_1 t_2 \\dots t_m$$$ if $$$n < m$$$ and $$$s_1 = t_1, s_2 = t_2, \\dots, s_n = t_n$$$ or there exists a number $$$p$$$ such that $$$p \\le min(n, m)$$$ and $$$s_1 = t_1, s_2 = t_2, \\dots, s_{p-1} = t_{p-1}$$$ and $$$s_p < t_p$$$.For example, \"aaa\" is smaller than \"aaaa\", \"abb\" is smaller than \"abc\", \"pqr\" is smaller than \"z\".", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) \u2014 the length of $$$s$$$. The second line of the input contains exactly $$$n$$$ lowercase Latin letters \u2014 the string $$$s$$$.", "output_spec": "Print one string \u2014 the smallest possible lexicographically string that can be obtained by removing at most one character from the string $$$s$$$.", "sample_inputs": ["3\naaa", "5\nabcda"], "sample_outputs": ["aa", "abca"], "notes": "NoteIn the first example you can remove any character of $$$s$$$ to obtain the string \"aa\".In the second example \"abca\" < \"abcd\" < \"abcda\" < \"abda\" < \"acda\" < \"bcda\"."}, "positive_code": [{"source_code": "$n=<>;\n$s=<>;\n$i=0;\n++$i while substr($s,$i,1) le substr($s,$i+1,1);\nsubstr($s,$i,1)='';\nprint $s;"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <>, chomp;\n\t\n\t@_ = map ord, split //;\n\t\n\tmy $j = @_ - 1;\n\t\n\tfor my $i ( 0 .. @_ - 2 ){\n\t\t$_[ $i ] > $_[ $i + 1 ] and do { $j = $i; last; };\n\t\t}\n\t\n\tsubstr $_, $j, 1, '';\n\t\n\tprint;\n\t}"}], "negative_code": [], "src_uid": "c01fc2cb6efc7eef290be12015f8d920"} {"nl": {"description": "Dima got into number sequences. Now he's got sequence a1,\u2009a2,\u2009...,\u2009an, consisting of n positive integers. Also, Dima has got a function f(x), which can be defined with the following recurrence: f(0)\u2009=\u20090; f(2\u00b7x)\u2009=\u2009f(x); f(2\u00b7x\u2009+\u20091)\u2009=\u2009f(x)\u2009+\u20091. Dima wonders, how many pairs of indexes (i,\u2009j) (1\u2009\u2264\u2009i\u2009<\u2009j\u2009\u2264\u2009n) are there, such that f(ai)\u2009=\u2009f(aj). Help him, count the number of such pairs. ", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105). The second line contains n positive integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109). The numbers in the lines are separated by single spaces.", "output_spec": "In a single line print the answer to the problem. Please, don't 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\n1 2 4", "3\n5 3 1"], "sample_outputs": ["3", "1"], "notes": "NoteIn the first sample any pair (i,\u2009j) will do, so the answer is 3.In the second sample only pair (1,\u20092) will do."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\n#print \"Content-type: text/html\\n\\n\";\n#print \"

Hello!

\\n\";\n\n$n=<>;\n@dig; $sum=0;\n@num=split(/ /,<>);\n\nfor $i(0..$n-1)\n{ $digi=0;\n while($num[$i])\n {\n $digi+=($num[$i]&1); $num[$i]>>=1;\n }\n $sum+=$dig[$digi]++;\n}\n\nprint $sum;"}], "negative_code": [], "src_uid": "c547e32f114546638973e0f0dd16d1a4"} {"nl": {"description": "A kindergarten teacher Natalia Pavlovna has invented a new ball game. This game not only develops the children's physique, but also teaches them how to count. The game goes as follows. Kids stand in circle. Let's agree to think of the children as numbered with numbers from 1 to n clockwise and the child number 1 is holding the ball. First the first child throws the ball to the next one clockwise, i.e. to the child number 2. Then the child number 2 throws the ball to the next but one child, i.e. to the child number 4, then the fourth child throws the ball to the child that stands two children away from him, i.e. to the child number 7, then the ball is thrown to the child who stands 3 children away from the child number 7, then the ball is thrown to the child who stands 4 children away from the last one, and so on. It should be mentioned that when a ball is thrown it may pass the beginning of the circle. For example, if n\u2009=\u20095, then after the third throw the child number 2 has the ball again. Overall, n\u2009-\u20091 throws are made, and the game ends.The problem is that not all the children get the ball during the game. If a child doesn't get the ball, he gets very upset and cries until Natalia Pavlovna gives him a candy. That's why Natalia Pavlovna asks you to help her to identify the numbers of the children who will get the ball after each throw.", "input_spec": "The first line contains integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009100) which indicates the number of kids in the circle.", "output_spec": "In the single line print n\u2009-\u20091 numbers which are the numbers of children who will get the ball after each throw. Separate the numbers by spaces.", "sample_inputs": ["10", "3"], "sample_outputs": ["2 4 7 1 6 2 9 7 6", "2 1"], "notes": null}, "positive_code": [{"source_code": "while (<>){\n @_=@m=();\n $i=$j=0;\n \n $_[$j+=++$i]++ for 1..$_-1;\n \n $i=0;\n for $k(@_){\n $i++;\n $k and push @m, $i % $_ ? $i % $_ : $_\n }\n print \"@m\\n\";\n }"}], "negative_code": [{"source_code": "while (<>){\n @_=@m=();\n $i=$j=0;\n \n $_[$j+=++$i]++ for 1..$_-1;\n \n $i=0;\n for $k(@_){\n $i++;\n $k and push @m, $i % $_\n }\n print \"@m\\n\";\n }"}], "src_uid": "7170c40405cf7a5e0f2bd15e4c7d189d"} {"nl": {"description": "They say \"years are like dominoes, tumbling one after the other\". But would a year fit into a grid? I don't think so.Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right.Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid.Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle?", "input_spec": "The first line of the input contains two integers h and w (1\u2009\u2264\u2009h,\u2009w\u2009\u2264\u2009500)\u00a0\u2013 the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#'\u00a0\u2014 denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1\u2009\u2264\u2009q\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1\u2009\u2264\u2009r1i\u2009\u2264\u2009r2i\u2009\u2264\u2009h,\u20091\u2009\u2264\u2009c1i\u2009\u2264\u2009c2i\u2009\u2264\u2009w)\u00a0\u2014 the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle.", "output_spec": "Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle.", "sample_inputs": ["5 8\n....#..#\n.#......\n##.#....\n##..#.##\n........\n4\n1 1 2 3\n4 1 4 1\n1 2 4 5\n2 5 5 8", "7 39\n.......................................\n.###..###..#..###.....###..###..#..###.\n...#..#.#..#..#.........#..#.#..#..#...\n.###..#.#..#..###.....###..#.#..#..###.\n.#....#.#..#....#.....#....#.#..#..#.#.\n.###..###..#..###.....###..###..#..###.\n.......................................\n6\n1 1 3 20\n2 10 6 30\n2 10 7 30\n2 2 7 7\n1 7 7 7\n1 8 7 8"], "sample_outputs": ["4\n0\n10\n15", "53\n89\n120\n23\n0\n2"], "notes": "NoteA red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. "}, "positive_code": [{"source_code": "use strict;\n\nour ($h, $w, @q, @row, @col);\n\nsub init_helper {\n\tmy $vec = shift;\n\tmy $lvec = @$vec;\n\tmy ($v, @v) = (0, 0);\n \t\n\tfor (my $i = 1; $i < $lvec; $i++) {\n\t\t$v++ if $$vec[$i] + $$vec[$i - 1] == 2;\n\t\t$v[$i] = $v;\n \t}\n\n\t\\@v;\n}\n\n# 1 2\n# 3 4\n# 4 = 1+2+3+4 - (1+2) - (1+3) + 1 \n\nsub query {\n\tmy ($r1, $c1, $r2, $c2) = map($_ - 1, @{shift()});\n\tmy ($s1, $s2, $s3, $s4, $s);\n\t$s1 = ($r1 > 0? $row[$r1 - 1][$c1]: 0) + ($c1 > 0? $col[$c1 - 1][$r1]: 0); \n\t$s2 = ($r1 > 0? $row[$r1 - 1][$c2]: 0) + $col[$c2][$r1];\n\t$s3 = $row[$r2][$c1] + ($c1 > 0? $col[$c1 - 1][$r2]: 0);\n\t$s4 = $row[$r2][$c2] + $col[$c2][$r2]; \n\t$s = $s4 - $s2 - $s3 + $s1;\n\t# print \"s1=$s1 s2=$s2 s3=$s3 s4=$s4 s=$s\\n\";\n\t$s;\n}\n\nsub rotate {\n\tmy $a = shift;\n\tmy @b;\n\tfor (my $c = 0; $c < $w; $c++) {\n\t\tmy @r;\n\t\tfor (my $r = 0; $r < $h; $r++) {\n\t\t\tpush(@r, $$a[$r][$c]);\n\t\t}\n\t\tpush(@b, \\@r);\n\t}\n\t@b;\n}\n\nsub input {\n\tmy $input = shift;\n\t($h, $w) = split \" \", <$input>;\n\t\n\tmy @input;\n\tfor (1..$h) {\n\t\tmy $s = <$input>; chomp($s);\n\t\tmy @freecell = map $_ eq \".\", split(\"\", $s);\n\t\tpush(@input, \\@freecell);\n\t}\n\t\n\t@row = ();\n\t@col = ();\n\t@q = ();\n\n\tforeach my $row (@input) {\n\t\tpush(@row, init_helper($row));\n\t}\n\n\tforeach my $col (rotate(\\@input)) {\n\t\tpush(@col, init_helper($col));\n\t}\n\n\tfor (my $r = 1; $r < $h; $r++) {\n\t\tfor (my $c = 0; $c < $w; $c++) {\n\t\t\t$row[$r][$c] += $row[$r - 1][$c]; \t\t\n\t\t}\n\t}\n\n\tfor (my $c = 1; $c < $w; $c++) {\n\t\tfor (my $r = 0; $r < $h; $r++) {\n\t\t\t$col[$c][$r] += $col[$c - 1][$r]; \t\t\n\t\t}\n\t}\n\t\n\tmy $q = <$input>;\n\tfor (1..$q) {\n\t\tmy @rc = split(\" \", <$input>);\n\t\tpush(@q, \\@rc);\n\t}\n}\n\nunless ($ENV{TEST} == 1) {\n\tinput(\\*STDIN);\n\tforeach my $q (@q) {\n\t\tprint query($q), \"\\n\";\n\t}\n}\n"}], "negative_code": [], "src_uid": "d9fdf0827940883069bead0d00b3da53"} {"nl": {"description": "The Little Elephant loves Ukraine very much. Most of all he loves town Rozdol (ukr. \"Rozdil\").However, Rozdil is dangerous to settle, so the Little Elephant wants to go to some other town. The Little Elephant doesn't like to spend much time on travelling, so for his journey he will choose a town that needs minimum time to travel to. If there are multiple such cities, then the Little Elephant won't go anywhere.For each town except for Rozdil you know the time needed to travel to this town. Find the town the Little Elephant will go to or print \"Still Rozdil\", if he stays in Rozdil.", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of cities. The next line contains n integers, separated by single spaces: the i-th integer represents the time needed to go from town Rozdil to the i-th town. The time values are positive integers, not exceeding 109. You can consider the cities numbered from 1 to n, inclusive. Rozdil is not among the numbered cities.", "output_spec": "Print the answer on a single line \u2014 the number of the town the Little Elephant will go to. If there are multiple cities with minimum travel time, print \"Still Rozdil\" (without the quotes).", "sample_inputs": ["2\n7 4", "7\n7 4 47 100 4 9 12"], "sample_outputs": ["2", "Still Rozdil"], "notes": "NoteIn the first sample there are only two cities where the Little Elephant can go. The travel time for the first town equals 7, to the second one \u2014 4. The town which is closest to Rodzil (the only one) is the second one, so the answer is 2.In the second sample the closest cities are cities two and five, the travelling time to both of them equals 4, so the answer is \"Still Rozdil\"."}, "positive_code": [{"source_code": "chomp (my $n = );\nchomp (my @num = split /\\s+/, );\n\nmy ($min,$pos)=(1+(10**9),-1);\nfor (my $i=0; $i<$n; $i++) {\n if ($num[$i]<$min) {\n $min = $num[$i];\n $pos = $i+1;\n } elsif ($num[$i]==$min) {\n $pos = -1;\n }\n}\nif ($pos == -1) {\n print \"Still Rozdil\\n\";\n} else {\n print \"$pos\";\n}\n"}, {"source_code": "#!perl\nuse strict;\nuse warnings;\n\n<>;\nmy $istr = <>;\nmy @arr = map int, split /\\D/, $istr;\nmy $min = shift @arr;\nmy $mini = 1;\nmy $double = 0;\nmy $index_shift = 2;\nfor my $i ( 0 .. @arr-1 ) {\n if ( $arr[$i] == $min ) {\n $double = 1;\n } elsif ( $arr[$i] < $min ) {\n $min = $arr[$i];\n $mini = $i + $index_shift;\n $double = 0;\n }\n}\nif ( $double ) {\n print 'Still Rozdil';\n} else {\n print $mini;\n}\n\n__END__\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $num =<>;\nmy $min=10000000000;\nmy $min_num=0;\nmy $city;\nmy @cities=split(' ',<>);\nforeach my $i(1..$num){\n\tif($cities[$i-1]<$min){\n\t\t$min=$cities[$i-1];\n\t\t$city=$i;\n\t\t$min_num=1;\n\t}\t\n\telsif($cities[$i-1]==$min){\n\t\t$min_num++;\n\t}\t\n}\nif($min_num==1){\n\tprint $city;\n}else{\n\tprint\"Still Rozdil\";\n}\t\t\t\t"}], "negative_code": [], "src_uid": "ce68f1171d9972a1b40b0450a05aa9cd"} {"nl": {"description": "You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press\u00a0\u2014 upvote and downvote.However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes.$$$n$$$ reviewers enter the site one by one. Each reviewer is one of the following types: type $$$1$$$: a reviewer has watched the movie, and they like it\u00a0\u2014 they press the upvote button; type $$$2$$$: a reviewer has watched the movie, and they dislike it\u00a0\u2014 they press the downvote button; type $$$3$$$: a reviewer hasn't watched the movie\u00a0\u2014 they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie. Each reviewer votes on the movie exactly once.Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one.What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \\le n \\le 50$$$)\u00a0\u2014 the number of reviewers. The second line of each testcase contains $$$n$$$ integers $$$r_1, r_2, \\dots, r_n$$$ ($$$1 \\le r_i \\le 3$$$)\u00a0\u2014 the types of the reviewers in the same order they enter the site.", "output_spec": "For each testcase print a single integer\u00a0\u2014 the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to.", "sample_inputs": ["4\n1\n2\n3\n1 2 3\n5\n1 1 1 1 1\n3\n3 3 2"], "sample_outputs": ["0\n2\n5\n2"], "notes": "NoteIn the first testcase of the example you can send the only reviewer to either of the servers\u00a0\u2014 they'll downvote anyway. The movie won't receive any upvotes.In the second testcase of the example you can send all reviewers to the first server: the first reviewer upvotes; the second reviewer downvotes; the last reviewer sees that the number of downvotes is not greater than the number of upvotes\u00a0\u2014 upvote themselves. There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewer\u00a0\u2014 to the second server: the first reviewer upvotes on the first server; the second reviewer downvotes on the first server; the last reviewer sees no upvotes or downvotes on the second server\u00a0\u2014 upvote themselves. "}, "positive_code": [{"source_code": "chomp(my $q = );\r\nforeach (1..$q)\r\n{\r\n ;\r\n chomp($inp = );\r\n @a = split / /, $inp;\r\n $res = 0;\r\n foreach (@a)\r\n {\r\n $res++ if $_ != 2;\r\n }\r\n print $res, \"\\n\";\r\n}"}], "negative_code": [{"source_code": "chomp(my $q = );\r\nforeach (1..$q)\r\n{\r\n ;\r\n chomp($inp = );\r\n @a = split / /, $inp;\r\n foreach (@a)\r\n {\r\n $res++ if $_ != 2;\r\n }\r\n print $res, \"\\n\";\r\n}"}], "src_uid": "a063705bd0ce1e17ccaafbbfc2663d93"} {"nl": {"description": "It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh. In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he removes the first and the second integer of the remaining sequence from the board, and so on. Bimokh stops when the board contains less than two numbers. When Bimokh removes numbers x and y from the board, he gets gcd(x,\u2009y) points. At the beginning of the game Bimokh has zero points.Mashmokh wants to win in the game. For this reason he wants his boss to get exactly k points in total. But the guy doesn't know how choose the initial sequence in the right way. Please, help him. Find n distinct integers a1,\u2009a2,\u2009...,\u2009an such that his boss will score exactly k points. Also Mashmokh can't memorize too huge numbers. Therefore each of these integers must be at most 109.", "input_spec": "The first line of input contains two space-separated integers n,\u2009k\u00a0(1\u2009\u2264\u2009n\u2009\u2264\u2009105;\u00a00\u2009\u2264\u2009k\u2009\u2264\u2009108).", "output_spec": "If such sequence doesn't exist output -1 otherwise output n distinct space-separated integers a1,\u2009a2,\u2009...,\u2009an\u00a0(1\u2009\u2264\u2009ai\u2009\u2264\u2009109).", "sample_inputs": ["5 2", "5 3", "7 2"], "sample_outputs": ["1 2 3 4 5", "2 4 3 7 1", "-1"], "notes": "Notegcd(x,\u2009y) is greatest common divisor of x and y."}, "positive_code": [{"source_code": "\nmy ($lim,$ans)=split(/ /,<>);\nif($lim==1 and $ans!=0)\n{\nprint \"-1 \\n\";\n}\nelsif(int($lim/2)>$ans)\n{\nprint \"-1\" . \"\\n\";\n}\nelsif(int($lim/2)==$ans)\n{\nmy @t=(1..$lim);\nprint \"@t \\n\";\n}\nelse\n{\nmy @t;\nmy $rem=int(($lim-2)/2);\nmy $fir=$ans-$rem;\nmy $sec=$fir*2;\npush(@t,$fir);\npush(@t,$sec);\nif(@t==$lim){\nprint \"@t \\n\";\nexit(0);\n}\npush(@t,1);\nfor(my $i=$fir+1;$i<$sec;$i++)\n{\nlast if(@t==$lim);\npush(@t,$i);\n}\nfor(my $i=$t[@t-1];$i>0;$i++)\n{\nlast if(@t==$lim);\npush(@t,$i+1) if $i+1 != $fir and $i+1!=$sec and gcd($i,$i+1)==1;\n}\nprint \"@t \\n\";\n}\n\nsub gcd\n{\nif($_[1]==0)\n{\nreturn $_[0];\n}\nelse{\nreturn gcd($_[1],$_[0]%$_[1]);\n}\n}"}], "negative_code": [], "src_uid": "b85c8bfbe67a23a81bef755f9313115a"} {"nl": {"description": "Suppose there is a $$$h \\times w$$$ grid consisting of empty or full cells. Let's make some definitions: $$$r_{i}$$$ is the number of consecutive full cells connected to the left side in the $$$i$$$-th row ($$$1 \\le i \\le h$$$). In particular, $$$r_i=0$$$ if the leftmost cell of the $$$i$$$-th row is empty. $$$c_{j}$$$ is the number of consecutive full cells connected to the top end in the $$$j$$$-th column ($$$1 \\le j \\le w$$$). In particular, $$$c_j=0$$$ if the topmost cell of the $$$j$$$-th column is empty. In other words, the $$$i$$$-th row starts exactly with $$$r_i$$$ full cells. Similarly, the $$$j$$$-th column starts exactly with $$$c_j$$$ full cells. These are the $$$r$$$ and $$$c$$$ values of some $$$3 \\times 4$$$ grid. Black cells are full and white cells are empty. You have values of $$$r$$$ and $$$c$$$. Initially, all cells are empty. Find the number of ways to fill grid cells to satisfy values of $$$r$$$ and $$$c$$$. Since the answer can be very large, find the answer modulo $$$1000000007\\,(10^{9} + 7)$$$. In other words, find the remainder after division of the answer by $$$1000000007\\,(10^{9} + 7)$$$.", "input_spec": "The first line contains two integers $$$h$$$ and $$$w$$$ ($$$1 \\le h, w \\le 10^{3}$$$)\u00a0\u2014 the height and width of the grid. The second line contains $$$h$$$ integers $$$r_{1}, r_{2}, \\ldots, r_{h}$$$ ($$$0 \\le r_{i} \\le w$$$)\u00a0\u2014 the values of $$$r$$$. The third line contains $$$w$$$ integers $$$c_{1}, c_{2}, \\ldots, c_{w}$$$ ($$$0 \\le c_{j} \\le h$$$)\u00a0\u2014 the values of $$$c$$$.", "output_spec": "Print the answer modulo $$$1000000007\\,(10^{9} + 7)$$$.", "sample_inputs": ["3 4\n0 3 1\n0 2 3 0", "1 1\n0\n1", "19 16\n16 16 16 16 15 15 0 5 0 4 9 9 1 4 4 0 8 16 12\n6 12 19 15 8 6 19 19 14 6 9 16 10 11 15 4"], "sample_outputs": ["2", "0", "797922655"], "notes": "NoteIn the first example, this is the other possible case. In the second example, it's impossible to make a grid to satisfy such $$$r$$$, $$$c$$$ values.In the third example, make sure to print answer modulo $$$(10^9 + 7)$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nour $fail;\n\nwhile( <> ){\n $debug and print '-' x 15;\n \n my( $h, $w ) = split;\n \n @_ = ( 0 x $w ) x $h;\n \n my @h = split ' ', <>;\n my @w = split ' ', <>;\n \n $fail = 0;\n \n fill( \\@h, \\@_ );\n \n $debug and print for @_;\n \n @_ = transpose( \\@_ );\n \n $debug and print for @_;\n \n fill( \\@w, \\@_ );\n \n $debug and print for @_;\n \n $debug and print \"fail: $fail\";\n \n my $cnt = 0;\n \n map { $cnt += () = /0/g } @_;\n \n $debug and print \"cnt: $cnt\";\n \n print $fail ? 0 : do {\n my $j = 1;\n for my $i ( 1 .. $cnt ){\n $j *= 2;\n $j %= ( 1e9 + 7 );\n }\n $j;\n };\n\t}\n\nsub fill {\n my( $ref_fills, $ref_main ) = @_;\n \n for my $i ( 0 .. @{ $ref_fills } - 1 ){\n $ref_main->[ $i ] =~ s/\n ^[01]{$ref_fills->[ $i ]}(?!1)(.)?\n /\n 1 x $ref_fills->[ $i ] . ( defined $1 ? 2 : '' );\n /xe or $fail = 1;\n }\n }\n\nsub transpose {\n my $ref = shift;\n \n my @new;\n \n for my $i ( 0 .. -1 + length $ref->[ 0 ] ){\n push @new, join '', map { chop } @{ $ref };\n }\n \n return reverse @new;\n }"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nour $fail;\n\nwhile( <> ){\n $debug and print '-' x 15;\n \n my( $h, $w ) = split;\n \n @_ = ( 0 x $w ) x $h;\n \n my @h = split ' ', <>;\n my @w = split ' ', <>;\n \n $fail = 0;\n \n fill( \\@h, \\@_ );\n \n $debug and print for @_;\n \n @_ = transpose( \\@_ );\n \n $debug and print for @_;\n \n fill( \\@w, \\@_ );\n \n $debug and print for @_;\n\n $debug and print \"fail: $fail\";\n \n my $cnt = 0;\n \n map { $cnt += () = /0/g } @_;\n \n $debug and print \"cnt: $cnt\";\n \n print $fail ? 0 : 2 ** $cnt % ( 1e9 + 7 );\n\t}\n\nsub fill {\n my( $ref_fills, $ref_main ) = @_;\n \n for my $i ( 0 .. @{ $ref_fills } - 1 ){\n $ref_main->[ $i ] =~ s/\n ^[01]{$ref_fills->[ $i ]}(?!1)(.)?\n /\n 1 x $ref_fills->[ $i ] . ( defined $1 ? 2 : '' );\n /xe or $fail = 1;\n }\n }\n\nsub transpose {\n my $ref = shift;\n \n my @new;\n \n for my $i ( 0 .. -1 + length $ref->[ 0 ] ){\n push @new, join '', map { chop } @{ $ref };\n }\n \n return reverse @new;\n }"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile( <> ){\n my( $h, $w ) = split;\n \n @_ = ( 0 x $w ) x $h;\n \n my @h = split ' ', <>;\n my @w = split ' ', <>;\n \n fill( \\@h, \\@_ );\n \n #$debug and print for @_;\n \n @_ = transpose( \\@_ );\n \n #$debug and print for @_;\n \n fill( \\@w, \\@_ );\n \n #$debug and print for @_;\n \n my $cnt = 0;\n \n map { $cnt += () = /0/g } @_;\n \n #$debug and print \"cnt: $cnt\";\n \n print 2 ** $cnt % ( 1e9 + 7 );\n\t}\n\nsub fill {\n my( $ref_fills, $ref_main ) = @_;\n \n for my $i ( 0 .. @{ $ref_fills } - 1 ){\n substr $ref_main->[ $i ], 0, $ref_fills->[ $i ], 1 x $ref_fills->[ $i ];\n substr $ref_main->[ $i ], $ref_fills->[ $i ], 1, 2\n if $ref_fills->[ $i ] < length $ref_main->[ $i ];\n }\n }\n\nsub transpose {\n my $ref = shift;\n \n my @new;\n \n for my $i ( 0 .. -1 + length $ref->[ 0 ] ){\n push @new, join '', map { chop } @{ $ref };\n }\n \n return reverse @new;\n }"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nour $fail;\n\nwhile( <> ){\n $debug and print '-' x 15;\n \n my( $h, $w ) = split;\n \n @_ = ( 0 x $w ) x $h;\n \n my @h = split ' ', <>;\n my @w = split ' ', <>;\n \n $fail = 0;\n \n fill( \\@h, \\@_ );\n \n $debug and print for @_;\n \n @_ = transpose( \\@_ );\n \n $debug and print for @_;\n \n fill( \\@w, \\@_ );\n \n $debug and print for @_;\n \n $debug and print \"fail: $fail\";\n \n my $cnt = 0;\n \n map { $cnt += () = /0/g } @_;\n \n $debug and print \"cnt: $cnt\";\n \n print $fail ? 0 : 2 ** $cnt % ( 1e9 + 7 );\n\t}\n\nsub fill {\n my( $ref_fills, $ref_main ) = @_;\n \n for my $i ( 0 .. @{ $ref_fills } - 1 ){\n #substr $ref_main->[ $i ], 0, $ref_fills->[ $i ], 1 x $ref_fills->[ $i ];\n #substr $ref_main->[ $i ], $ref_fills->[ $i ], 1, 2\n # if $ref_fills->[ $i ] < length $ref_main->[ $i ];\n $ref_main->[ $i ] =~ s/\n ^[01]{$ref_fills->[ $i ]}(.)?\n /\n 1 x $ref_fills->[ $i ] . ( defined $1 ? 2 : '' );\n /xe or $fail = 1;\n }\n }\n\nsub transpose {\n my $ref = shift;\n \n my @new;\n \n for my $i ( 0 .. -1 + length $ref->[ 0 ] ){\n push @new, join '', map { chop } @{ $ref };\n }\n \n return reverse @new;\n }"}], "src_uid": "907f7db88fb16178d6be57bea12f90a2"} {"nl": {"description": "On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li,\u2009ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose such n dates of famous events that will fulfill both conditions. It is guaranteed that it is possible.", "input_spec": "The first line contains one integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of known events. Then follow n lines containing two integers li and ri each (1\u2009\u2264\u2009li\u2009\u2264\u2009ri\u2009\u2264\u2009107) \u2014 the earliest acceptable date and the latest acceptable date of the i-th event.", "output_spec": "Print n numbers \u2014 the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists.", "sample_inputs": ["3\n1 2\n2 3\n3 4", "2\n1 3\n1 3"], "sample_outputs": ["1 2 3", "1 2"], "notes": null}, "positive_code": [{"source_code": "sub max { my ($a, $b) = @_; $a > $b? $a: $b }\nsub priority { max($a->{start}, $current) <=> max($b->{start}, $current) || $a->{end} <=> $b->{end} }\n\n(undef, @input) = ;\nfor (@input) { \n\t@val = split;\n\tpush @p, {start => $val[0], end => $val[1], index => $i++ }\n}\n\nwhile (@p) {\n\t($p, @p) = sort priority @p;\t\n\t$current = max $p->{start}, $current;\n\t$r[ $p->{index} ] = $current++;\n}\n\nprint \"@r\";"}], "negative_code": [{"source_code": "sub compare { $a <=> $b }\nsub max { my ($a, $b) = @_; $a > $b? $a: $b }\n\n(undef, @input) = ;\nfor (@input) { push @p, (split)[0] }\n@p = sort compare @p;\n\nfor $p (@p) {\n\t$d = max $p, $d;\n\tpush @r, $d;\n\t$d++;\n}\n\nprint \"@r\";"}], "src_uid": "2e6e21ce00b438f51ae4b503ca7c0a1e"} {"nl": {"description": "Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings S and T of equal length to be \"similar\". After a brief search on the Internet, he learned about the Hamming distance between two strings S and T of the same length, which is defined as the number of positions in which S and T have different characters. For example, the Hamming distance between words \"permanent\" and \"pergament\" is two, as these words differ in the fourth and sixth letters.Moreover, as he was searching for information, he also noticed that modern search engines have powerful mechanisms to correct errors in the request to improve the quality of search. Ford doesn't know much about human beings, so he assumed that the most common mistake in a request is swapping two arbitrary letters of the string (not necessarily adjacent). Now he wants to write a function that determines which two letters should be swapped in string S, so that the Hamming distance between a new string S and string T would be as small as possible, or otherwise, determine that such a replacement cannot reduce the distance between the strings.Help him do this!", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000) \u2014 the length of strings S and T. The second line contains string S. The third line contains string T. Each of the lines only contains lowercase Latin letters.", "output_spec": "In the first line, print number x \u2014 the minimum possible Hamming distance between strings S and T if you swap at most one pair of letters in S. In the second line, either print the indexes i and j (1\u2009\u2264\u2009i,\u2009j\u2009\u2264\u2009n, i\u2009\u2260\u2009j), if reaching the minimum possible distance is possible by swapping letters on positions i and j, or print \"-1 -1\", if it is not necessary to swap characters. If there are multiple possible answers, print any of them.", "sample_inputs": ["9\npergament\npermanent", "6\nwookie\ncookie", "4\npetr\negor", "6\ndouble\nbundle"], "sample_outputs": ["1\n4 6", "1\n-1 -1", "2\n1 2", "2\n4 1"], "notes": "NoteIn the second test it is acceptable to print i\u2009=\u20092, j\u2009=\u20093."}, "positive_code": [{"source_code": "chomp(($n, $A, $B) = <>);\n$k = 0;\nfor $i (0 .. $n -1){\n\t$a = chop $A;\n\t$b = chop $B;\n\t$a eq $b or $k ++,\n\t$h{$a.$b} = $n - $i\n}\n\t\nOUT:\nfor $i (keys %h){\n\tfor $j (keys %h){\n\t\t($j.$i) =~ /(.)\\1/ and $ans = \"$h{$i} $h{$j}\" and \n\t\t($i.$j) =~ /(.)\\1/ and do {$k --; last OUT}\n\t}\t\t\n}\n\nprint $ans ? --$k.$/.$ans : \"$k\\n-1 -1\"\n"}, {"source_code": "chomp(($n, $A, $B) = <>);\n$k = 0;\nfor $i (0 .. $n -1){\n\t$a = chop $A;\n\t$b = chop $B;\n\t$a eq $b or $k ++,\n\t$h{$a.$b} = $n - $i\n}\n\t\nOUT:\nfor $i (keys %h){\n\tfor $j (keys %h){\n\t\t($j.$i) =~ /(.)\\1/ and $ans = \"$h{$i} $h{$j}\" and \n\t\t($i.$j) =~ /(.)\\1/ and do {$k --; last OUT}\n\t}\t\t\n}\n\nprint $ans ? --$k.$/.$ans : \"$k\\n-1 -1\"\n"}, {"source_code": "chomp(($n, $A, $B) = <>);\n$k = 0;\nfor $i (0 .. $n -1){\n\t$a = chop $A;\n\t$b = chop $B;\n\t$a eq $b or $k ++,\n\t$h{$a.$b} = $n - $i\n}\n\t\nOUT:\nfor $i (keys %h){\n\tfor $j (keys %h){\n\t\t($j.$i) =~ /(.)\\1/ and $ans = \"$h{$i} $h{$j}\" and \n\t\t($i.$j) =~ /(.)\\1/ and do {$k --; last OUT}\n\t}\t\t\n}\n\nprint $ans ? --$k.$/.$ans : \"$k\\n-1 -1\""}, {"source_code": "#!/usr/bin/perl/\n\nmy $n = <>;\nmy @s = split //, <>;\nmy @t = split //, <>;\n\nmy %pairs, %sh, %th;\nmy $dst = 0;\n\nfor (my $i = 0; $i < $n; $i++) {\n if ($s[$i] ne $t[$i]) {\n my $p = $s[$i] . $t[$i];\n $dst++;\n $pairs{$p} = $i + 1;\n $sh{$s[$i]} = $i + 1;\n $th{$t[$i]} = $i + 1;\n # print ($pairs{$p}) . \"\\n\";\n\n if ($pairs{reverse $p}) {\n for (my $j = $i + 1; $j < $n; $j++) {\n if ($s[$j] ne $t[$j]) {\n $dst++;\n }\n }\n print ($dst - 2);\n print \"\\n$pairs{reverse $p} $pairs{$p}\";\n exit;\n }\n }\n}\n\nfor (my $i = 0; $i < $n; $i++) {\n if ($s[$i] ne $t[$i]) {\n if ($th{$s[$i]}) {\n print $dst - 1;\n print \"\\n\";\n print $i + 1;\n print \" $th{$s[$i]}\";\n exit;\n }\n }\n}\n\nprint \"$dst\\n-1 -1\";\n"}, {"source_code": "chomp(($n, $A, $B) = <>);\n$k = 0;\nfor $i (0 .. $n -1){\n\t$a = chop $A;\n\t$b = chop $B;\n\t$a eq $b or $k ++,\n\t$h{$a.$b} = $n - $i\n}\n\t\nOUT:\nfor $i (keys %h){\n\tfor $j (keys %h){\n\t\t($j.$i) =~ /(.)\\1/ and $ans = \"$h{$i} $h{$j}\" and \n\t\t($i.$j) =~ /(.)\\1/ and do {$k --; last OUT}\n\t}\t\t\n}\n\nprint $ans ? --$k.$/.$ans : \"$k\\n-1 -1\"\n"}, {"source_code": "use strict;\nuse warnings;\n<>;\nmy $a = 0;\n\nmy @first = split //, <>;\nmy @second = split //, <>;\nmy %s;\nmy @matrix;\nfor(0..$#first - 1)\n{\n unless ($first[$_] eq $second[$_])\n {\n $s{$first[$_].$second[$_]} = $_ ;\n $matrix[ord ($first[$_]) - (ord 'a')][ord ($second[$_]) - ord( 'a')] = $_;\n $a++; \n }\n \n \n}\n\nfor my $key (keys %s)\n{\n my $key2 = do{$key =~ /(.)(.)/; $2.$1}; \n if (defined $s{$key2}) \n {\n print $a - 2,\"\\n\";\n printf \"%i %i\\n\", $s{$key}+1 ,$s{$key2}+1;\n exit 0;\n }\n}\n\nfor my $f (0..$#matrix)\n{\n for my $s (0..$#{$matrix[$f]})\n {\n if(defined $matrix[$f][$s])\n {\n for my $f2 (0..$#matrix)\n {\n if(defined($matrix[$f2][$f]) )\n {\n printf \"%i\\n%i %i\", $a - 1,$matrix[$f2][$f] + 1, $matrix[$f][$s] + 1;\n exit 0;\n }\n }\n }\n }\n}\nprintf \"%i\\n%i %i\", $a,-1, -1;"}, {"source_code": "chomp(($n, $A, $B) = <>);\n$k = 0;\nfor $i (0 .. $n -1){\n\t$a = chop $A;\n\t$b = chop $B;\n\t$a eq $b or $k ++,\n\t$h{$a.$b} = $n - $i\n}\n\t\nOUT:\nfor $i (keys %h){\n\tfor $j (keys %h){\n\t\t($j.$i) =~ /(.)\\1/ and $ans = \"$h{$i} $h{$j}\" and \n\t\t($i.$j) =~ /(.)\\1/ and do {$k --; last OUT}\n\t}\t\t\n}\n\nprint $ans ? --$k.$/.$ans : \"$k\\n-1 -1\"\n"}, {"source_code": "#!/usr/bin/perl/\n\nmy $n = <>;\nmy @s = split //, <>;\nmy @t = split //, <>;\n\nmy %pairs, %sh, %th;\nmy $dst = 0;\n\nfor (my $i = 0; $i < $n; $i++) {\n if ($s[$i] ne $t[$i]) {\n my $p = $s[$i] . $t[$i];\n $dst++;\n $pairs{$p} = $i + 1;\n $sh{$s[$i]} = $i + 1;\n $th{$t[$i]} = $i + 1;\n # print ($pairs{$p}) . \"\\n\";\n\n if ($pairs{reverse $p}) {\n for (my $j = $i + 1; $j < $n; $j++) {\n if ($s[$j] ne $t[$j]) {\n $dst++;\n }\n }\n print ($dst - 2);\n print \"\\n$pairs{reverse $p} $pairs{$p}\";\n exit;\n }\n }\n}\n\nfor (my $i = 0; $i < $n; $i++) {\n if ($s[$i] ne $t[$i]) {\n if ($th{$s[$i]}) {\n print $dst - 1;\n print \"\\n\";\n print $i + 1;\n print \" $th{$s[$i]}\";\n exit;\n }\n }\n}\n\nprint \"$dst\\n-1 -1\";"}, {"source_code": "chomp(($n, $A, $B) = <>);\n$k = 0;\nfor $i (0 .. $n -1){\n\t$a = chop $A;\n\t$b = chop $B;\n\t$a eq $b or $k ++,\n\t$h{$a.$b} = $n - $i\n}\n\t\nOUT:\nfor $i (keys %h){\n\tfor $j (keys %h){\n\t\t($j.$i) =~ /(.)\\1/ and $ans = \"$h{$i} $h{$j}\" and \n\t\t($i.$j) =~ /(.)\\1/ and do {$k --; last OUT}\n\t}\t\t\n}\n\nprint $ans ? --$k.$/.$ans : \"$k\\n-1 -1\"\n"}, {"source_code": "chomp(($n, $A, $B) = <>);\n$k = 0;\nfor $i (0 .. $n -1){\n\t$a = chop $A;\n\t$b = chop $B;\n\t$a eq $b or $k ++,\n\t$h{$a.$b} = $n - $i\n}\n\t\nOUT:\nfor $i (keys %h){\n\tfor $j (keys %h){\n\t\t($j.$i) =~ /(.)\\1/ and $ans = \"$h{$i} $h{$j}\" and \n\t\t($i.$j) =~ /(.)\\1/ and do {$k --; last OUT}\n\t}\t\t\n}\n\nprint $ans ? --$k.$/.$ans : \"$k\\n-1 -1\"\n"}, {"source_code": "chomp(($n, $A, $B) = <>);\n$k = 0;\nfor $i (0 .. $n -1){\n\t$a = chop $A;\n\t$b = chop $B;\n\t$a eq $b or $k ++,\n\t$h{$a.$b} = $n - $i\n}\n\t\nOUT:\nfor $i (keys %h){\n\tfor $j (keys %h){\n\t\t($j.$i) =~ /(.)\\1/ and $ans = \"$h{$i} $h{$j}\" and \n\t\t($i.$j) =~ /(.)\\1/ and do {$k --; last OUT}\n\t}\t\t\n}\n\nprint $ans ? --$k.$/.$ans : \"$k\\n-1 -1\"\n"}, {"source_code": "chomp(($n, $A, $B) = <>);\n$k = 0;\nfor $i (0 .. $n -1){\n\t$a = chop $A;\n\t$b = chop $B;\n\t$a eq $b or $k ++,\n\t$h{$a.$b} = $n - $i\n}\n\t\nOUT:\nfor $i (keys %h){\n\tfor $j (keys %h){\n\t\t($j.$i) =~ /(.)\\1/ and $ans = \"$h{$i} $h{$j}\" and \n\t\t($i.$j) =~ /(.)\\1/ and do {$k --; last OUT}\n\t}\t\t\n}\n\nprint $ans ? --$k.$/.$ans : \"$k\\n-1 -1\""}, {"source_code": "\n$\\ = $/;\nwhile(<>){\n\t$n = $_;\n\tchomp ($A=<>);\n\tchomp ($B=<>);\n\tundef %h;\n\tundef $ans;\n\t$k = 0;\n\tfor $i (0 .. -1 + length $A){\n\t\t$a = chop $A;\n\t\t$b = chop $B;\n\t#\tprint $a,\" \",$b;\n\t\t$a eq $b and $k ++;\n\t\t$a ne $b and do { $h{$a.$b} = $n - $i };\n\t\t\n\t}\n\t$, = $\";\n#\tprint %h;\n\t$k = $n - $k;\n\t\n\t\n\tOUT:\n\tfor $i (keys %h){\n\t\tfor $j (keys %h){\n\t\t\t$i eq (reverse $j) and do { $ans = $h{$i}.\" \".$h{$j}; $k --; last OUT};\n\t\t\t(substr $i, 0, 1) eq (substr $j, 1, 1) and do { $ans = $h{$i}.\" \".$h{$j} };\n\t\t}\n\t\t\n\t}\n\t$ans and $k--;\n\tprint $k;\n\tprint $ans ? $ans : \"-1 -1\";\n}\n\t\n"}], "negative_code": [{"source_code": "use strict;\nuse warnings;use List::Util qw(first max maxstr min minstr reduce shuffle sum);\nuse Data::Dumper;\n<>;\nchomp (my $t = <>);\nmy @S = split //, $t;\n\nchomp ($t = <>);\nmy @T = split //, $t;\nmy $ans = 0;\n\nmy @di;\nmy %TT;\nmy %SS;\nfor(0.. $#S)\n{\n if($S[$_] ne $T[$_])\n {\n $ans ++;\n push @di, $_;\n push @{$TT{$T[$_]}}, $_;\n push @{$SS{$S[$_]}}, $_; \n } \n}\n\nmy @TTL = keys %TT;\nmy @SSL = keys %SS;\nmy @sht;\n\nfor my $l ('a'..'z')\n{\n my @tttl = grep {$_ eq $l} @TTL;\n my @tssl = grep {$_ eq $l} @SSL;\n \n if($#tttl != -1 and $#tssl != -1)\n {\n push (@sht, $l);\n }\n}\nif( $#sht == -1)\n{\n print \"$ans\\n-1 -1\"; \n exit 0;\n}\n\nif( $#sht == 0)\n{\n $ans--;\n my $let = $sht[0];\n my $l = $TT{$let}->[0];\n my $r = $SS{$let}->[0];\n print $ans,\"\\n\";\n \n print min($l+1,$r+1),\" \", max($l+1,$r+1);\n exit 0;\n}\nmy %rr = map {$_, 1} @sht;\nfor my $e (keys %rr)\n{\n for(@{$TT{$e}})\n {\n if(defined $rr{$S[$_]})\n {\n print $ans-2,\"\\n\";\n my $l = $_;\n my $r = $TT{$S[$_]}->[0];\n print min($l+1,$r+1),\" \", max($l+1,$r+1);\n exit 0;\n }\n }\n}\nprint \"\u0442\u0430\u043a\u043e\u0433\u043e \u044f \u043d\u0435 \u043e\u0436\u0438\u0434\u0430\u043b( \";\n"}, {"source_code": "\n$\\ = $/;\nwhile(<>){\n\t$n = $_;\n\tchomp ($A=<>);\n\tchomp ($B=<>);\n\tundef %h;\n\tundef $ans;\n\tfor $i (0 .. -1 + length $A){\n\t\t$a = chop $A;\n\t\t$b = chop $B;\n\t#\tprint $a,\" \",$b;\n\t\t$a ne $b and do { $h{$a.$b} = $n - $i };\n\t\t\n\t}\n\t$, = $\";\n#\tprint %h;\n\t\n\tOUT:\n\tfor $i (keys %h){\n\t\tfor $j (keys %h){\n\t\t\t$i eq (reverse $j) and do { $ans = $h{$i}.\" \".$h{$j}; last OUT};\n\t\t\t(substr $i, 0, 1) eq (substr $j, 1, 1) and do { $ans = $h{$i}.\" \".$h{$j} };\n\t\t}\n\t\t\n\t}\n\tprint $ans ? $ans : \"-1 -1\";\n}\n\t\n"}], "src_uid": "2fa543c8b8f9dc500c36cf719800a6b0"} {"nl": {"description": "A wise man told Kerem \"Different is good\" once, so Kerem wants all things in his life to be different. Kerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants all substrings of his string s to be distinct. Substring is a string formed by some number of consecutive characters of the string. For example, string \"aba\" has substrings \"\" (empty substring), \"a\", \"b\", \"a\", \"ab\", \"ba\", \"aba\".If string s has at least two equal substrings then Kerem will change characters at some positions to some other lowercase English letters. Changing characters is a very tiring job, so Kerem want to perform as few changes as possible.Your task is to find the minimum number of changes needed to make all the substrings of the given string distinct, or determine that it is impossible.", "input_spec": "The first line of the input contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000)\u00a0\u2014 the length of the string s. The second line contains the string s of length n consisting of only lowercase English letters.", "output_spec": "If it's impossible to change the string s such that all its substring are distinct print -1. Otherwise print the minimum required number of changes.", "sample_inputs": ["2\naa", "4\nkoko", "5\nmurat"], "sample_outputs": ["1", "2", "0"], "notes": "NoteIn the first sample one of the possible solutions is to change the first character to 'b'.In the second sample, one may change the first character to 'a' and second character to 'b', so the string becomes \"abko\"."}, "positive_code": [{"source_code": "<>,<> =~ /.+/;\n$_ = join '', sort split //, $&;\nprint length > 26 ? -1 : (length) - length y///csr"}, {"source_code": "$n = <>;\n%h = map { $_, 1 } <> =~ /./g;\nprint $n > 26 ? -1 : $n - keys %h"}], "negative_code": [{"source_code": "$n = <>;\n%h = map { $_, 1 } <> =~ /./g;\nprint $n > 26 ? -1 : $n - %h"}], "src_uid": "d9e9c53b391eb44f469cc92fdcf3ea0a"} {"nl": {"description": "Valera is a collector. Once he wanted to expand his collection with exactly one antique item.Valera knows n sellers of antiques, the i-th of them auctioned ki items. Currently the auction price of the j-th object of the i-th seller is sij. Valera gets on well with each of the n sellers. He is perfectly sure that if he outbids the current price of one of the items in the auction (in other words, offers the seller the money that is strictly greater than the current price of the item at the auction), the seller of the object will immediately sign a contract with him.Unfortunately, Valera has only v units of money. Help him to determine which of the n sellers he can make a deal with.", "input_spec": "The first line contains two space-separated integers n,\u2009v (1\u2009\u2264\u2009n\u2009\u2264\u200950;\u00a0104\u2009\u2264\u2009v\u2009\u2264\u2009106) \u2014 the number of sellers and the units of money the Valera has. Then n lines follow. The i-th line first contains integer ki (1\u2009\u2264\u2009ki\u2009\u2264\u200950) the number of items of the i-th seller. Then go ki space-separated integers si1,\u2009si2,\u2009...,\u2009siki (104\u2009\u2264\u2009sij\u2009\u2264\u2009106) \u2014 the current prices of the items of the i-th seller. ", "output_spec": "In the first line, print integer p \u2014 the number of sellers with who Valera can make a deal. In the second line print p space-separated integers q1,\u2009q2,\u2009...,\u2009qp (1\u2009\u2264\u2009qi\u2009\u2264\u2009n) \u2014 the numbers of the sellers with who Valera can make a deal. Print the numbers of the sellers in the increasing order. ", "sample_inputs": ["3 50000\n1 40000\n2 20000 60000\n3 10000 70000 190000", "3 50000\n1 50000\n3 100000 120000 110000\n3 120000 110000 120000"], "sample_outputs": ["3\n1 2 3", "0"], "notes": "NoteIn the first sample Valera can bargain with each of the sellers. He can outbid the following items: a 40000 item from the first seller, a 20000 item from the second seller, and a 10000 item from the third seller.In the second sample Valera can not make a deal with any of the sellers, as the prices of all items in the auction too big for him."}, "positive_code": [{"source_code": "my ($n, $money) = split / /, <>;\nmy @ans;\nfor(1..$n) {\n push @ans, $_ if($money > (sort{$a <=> $b} split / /, <>)[1]);\n}\nprint scalar @ans . \"\\n@ans\";\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nsub min {\n\tmy ($x, $y) = (shift, shift);\n\treturn $x<$y ? $x:$y;\n}\n\n($m, $n, $x) = (0, split / /, <>);\n@ans = (0) x ($n + 1);\nforeach my $i (1..$n) {\n\t@a = split / /, <>;\n\t$mn = 10 x 7;\n\t$mn = min($mn, $a[$_]) foreach (1..$#a);\n\t$mn<$x and $ans[$m++] = $i;\n}\nsay $m;\nprint $ans[$_], \" \" foreach (0..$m-1);"}, {"source_code": "$i=0;\n$in = <>;\n( $n, $v ) = split( ' ', $in );\nwhile($n--){\n $line = <>;\n ( $item, @items ) = split( ' ', $line );\n foreach (@items) {\n if ( $_ < $v ) {\n push( @res, ( $i + 1 ) );\n last;\n }\n }\n$i++;\n}\nprint ($#res+1); print \"\\n\";\nprint join(' ',@res);"}, {"source_code": "( $n, $v ) = split( ' ', <> );\nfor $i ( 1 .. $n ) {\n $line = <>;\n ( $item, @items ) = split( ' ', $line );\n foreach (@items) {\n if ( $_ < $v ) {\n push( @res, $i );\n last;\n }\n }\n}\nprint( $#res+ 1 ) and print \"\\n@res\";"}, {"source_code": "($n,$v)=split/ /,<>;\nfor $i(1..$n){\n\t$_=<>;\n\t@_=split/ /;\n\tshift @_;\n\t@_= sort {$a<=>$b} @_;\n\t$_[0]<$v and push @ans, $i\n\t}\nprint 0+@ans,\"\\n@ans\""}, {"source_code": "while(<>){\n\tchomp;\n\t($n,$v)=split/ /;\n\t\n\t@ans=();\n\tfor $i(1..$n){\n\t\t$_=<>;\n\t\tchomp;\n\t\t@_=split/ /;\n\t\tshift @_;\n\t\t@_= sort {$a<=>$b} @_;\n\t#\tprint \"$_[0]\\n\";\n\t\t$_[0]<$v and (push @ans, $i);\n\t\t\n\t\t}\n\t\n\t\n\tprint 0+@ans;\n\t@ans and print \"\\n@ans\\n\";\n\t}"}], "negative_code": [], "src_uid": "7804152ee14264afef019c5ad33094f9"} {"nl": {"description": "Dr. Bruce Banner hates his enemies (like others don't). As we all know, he can barely talk when he turns into the incredible Hulk. That's why he asked you to help him to express his feelings.Hulk likes the Inception so much, and like that his feelings are complicated. They have n layers. The first layer is hate, second one is love, third one is hate and so on...For example if n\u2009=\u20091, then his feeling is \"I hate it\" or if n\u2009=\u20092 it's \"I hate that I love it\", and if n\u2009=\u20093 it's \"I hate that I love that I hate it\" and so on.Please help Dr. Banner.", "input_spec": "The only line of the input contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100)\u00a0\u2014 the number of layers of love and hate.", "output_spec": "Print Dr.Banner's feeling in one line.", "sample_inputs": ["1", "2", "3"], "sample_outputs": ["I hate it", "I hate that I love it", "I hate that I love that I hate it"], "notes": null}, "positive_code": [{"source_code": "$n = <>; $_ = join \" that \", (\"I hate\") x $n;\ns/I hate that I hate/I hate that I love/g;\nprint \"$_ it\";"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $n = <> + 0;\nmy @feelings = ();\n\nfor (my $i = 0; $i < $n; $i++) {\n if($i % 2 eq 0) {\n push(@feelings, \"I hate\");\n } else {\n push(@feelings, \"I love\");\n }\n}\n\nmy $result = join(\" that \", @feelings);\nprint $result . \" it\\n\";"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n = <>;\nmy @items = qw(hate love);\nmy $i = 0;\n\nmy @words;\n\nfor (1..$n) {\n push @words, 'I', $items[$i], 'that';\n $i = !$i;\n}\n\n$words[$#words] = 'it';\n\n$\\ = \"\\n\";\n$, = ' ';\nprint @words;\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \n# essential\nmy @tokens = ();\n\nmy $n = read_token();\n\nmy $answer = 'I hate ';\n$n--;\n\nmy $m = 0;\n\nwhile ( $n > 0 ) {\n if ( $m % 2 == 0 ) {\n $answer .= 'that I love ';\n }\n else {\n $answer .= 'that I hate ';\n }\n $m++;\n $n--;\n}\n\n$answer .= 'it';\n\nsay $answer;\n\n\n\n\nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n"}, {"source_code": "my$n=<>,$b=0,@s=(\"I hate \",\"I love \",\"that \");print\"@s[0]\";while(--$n){print\"@s[2]\",($b?\"@s[0]\":\"@s[1]\");$b=!$b;}print\"it\""}, {"source_code": "# cf706a\nuse strict;\n$_=<>;\nmy $i=0;\nwhile(--$_>=0) {\n print \"I \",['hate','love']->[$i],\" \",($_!=0 ? \"that \" : \"it\");\n $i=1-$i;\n}\n"}, {"source_code": "# cf706a # solution courtesy 19744936\ts_p\nuse strict;\nmy $n=<>;\n$_=join(' that ',('I hate') x $n).\" it\";\nprint s/I hate that I hate/I hate that I love/gr;\n"}], "negative_code": [], "src_uid": "7f2441cfb32d105607e63020bed0e145"} {"nl": {"description": "Sehr Sus is an infinite hexagonal grid as pictured below, controlled by MennaFadali, ZerooCool and Hosssam.They love equilateral triangles and want to create $$$n$$$ equilateral triangles on the grid by adding some straight lines. The triangles must all be empty from the inside (in other words, no straight line or hexagon edge should pass through any of the triangles).You are allowed to add straight lines parallel to the edges of the hexagons. Given $$$n$$$, what is the minimum number of lines you need to add to create at least $$$n$$$ equilateral triangles as described? Adding two red lines results in two new yellow equilateral triangles. ", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^5$$$) \u2014 the number of test cases. Then $$$t$$$ test cases follow. Each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^{9}$$$) \u2014 the required number of equilateral triangles.", "output_spec": "For each test case, print the minimum number of lines needed to have $$$n$$$ or more equilateral triangles.", "sample_inputs": ["4\n\n1\n\n2\n\n3\n\n4567"], "sample_outputs": ["2\n2\n3\n83"], "notes": "NoteIn the first and second test cases only 2 lines are needed. After adding the first line, no equilateral triangles will be created no matter where it is added. But after adding the second line, two more triangles will be created at once. In the third test case, the minimum needed is 3 lines as shown below. "}, "positive_code": [{"source_code": "print-1&1-9e-9+sqrt<>*1.5,$/for(1..<>)"}, {"source_code": "print-1&1+sqrt<>*1.4999999999,\" \"for(1..<>)"}, {"source_code": "print-1&1-9e-9+sqrt<>*1.5,\" \"for(1..<>)"}, {"source_code": "print-1&sqrt(<>*1.5)-9e-9+1,\" \"for(1..<>)"}, {"source_code": "for(1..<>){print-1&sqrt(<>*1.5)-9e-9+1,\" \"}"}, {"source_code": "for(1..<>){print-1&sqrt(<>*1.5)-9e-9+1,\"\\n\"}"}, {"source_code": "for(1..<>){print 1+int(sqrt(<>*1.5)-9e-9),\"\\n\"}"}, {"source_code": "<>;while(<>){print 1+int(sqrt($_*1.5)-9e-9),\"\\n\"}"}, {"source_code": "<>;while(<>){print(int(sqrt($_*1.5)-9e-9)+1,\"\\n\")}"}], "negative_code": [{"source_code": "print-1&1+sqrt<>*1.49999,\" \"for(1..<>)"}, {"source_code": "print-1&1+sqrt<>*1.4999,\" \"for(1..<>)"}, {"source_code": "<>;print-1&1+1.22474487*sqrt,\" \"for(<>)"}, {"source_code": "<>;print-1&1+1.2247449*sqrt,\" \"for(<>)"}, {"source_code": "<>;print-1&1+1.2247448*sqrt,\" \"for(<>)"}, {"source_code": "<>;print-1&1+1.225*sqrt,\" \"for(<>)"}, {"source_code": "<>;print-1&1+1.224*sqrt,\" \"for(<>)"}, {"source_code": "for(1..<>){print-1&sqrt<>*1.5-9e-9+1,\"\\n\"}"}, {"source_code": "for(1..<>){print-1&sqrt<>*1.5+1,\"\\n\"}"}, {"source_code": "for(1..<>){print-1&sqrt(<>*1.5)+1,\"\\n\"}"}, {"source_code": "<>;while(<>){print(int(sqrt($_*1.5)+0.99),\"\\n\")}"}], "src_uid": "fbc8d6564905fbe82f9f612b2da3484d"} {"nl": {"description": "Stepan likes to repeat vowel letters when he writes words. For example, instead of the word \"pobeda\" he can write \"pobeeeedaaaaa\".Sergey does not like such behavior, so he wants to write a program to format the words written by Stepan. This program must combine all consecutive equal vowels to a single vowel. The vowel letters are \"a\", \"e\", \"i\", \"o\", \"u\" and \"y\".There are exceptions: if letters \"e\" or \"o\" repeat in a row exactly 2 times, like in words \"feet\" and \"foot\", the program must skip them and do not transform in one vowel. For example, the word \"iiiimpleeemeentatiioon\" must be converted to the word \"implemeentatioon\".Sergey is very busy and asks you to help him and write the required program.", "input_spec": "The first line contains the integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000) \u2014 the number of letters in the word written by Stepan. The second line contains the string s which has length that equals to n and contains only lowercase English letters \u2014 the word written by Stepan.", "output_spec": "Print the single string \u2014 the word written by Stepan converted according to the rules described in the statement.", "sample_inputs": ["13\npobeeeedaaaaa", "22\niiiimpleeemeentatiioon", "18\naeiouyaaeeiioouuyy", "24\naaaoooiiiuuuyyyeeeggghhh"], "sample_outputs": ["pobeda", "implemeentatioon", "aeiouyaeeioouy", "aoiuyeggghhh"], "notes": null}, "positive_code": [{"source_code": "$a=<>;\nmy $i=<>;\n$i=~s/a{2,}/a/g;\n$i=~s/i{2,}/i/g;\n$i=~s/u{2,}/u/g;\n$i=~s/y{2,}/y/g;\n$i=~s/e{3,}/e/g;\n$i=~s/o{3,}/o/g;\nprint $i;\n"}, {"source_code": "my $first;\nwhile (<>) {\n $first++ or next;\n s/a+/a/g;\n s/eee+/e/g;\n s/i+/i/g;\n s/ooo+/o/g;\n s/u+/u/g;\n s/y+/y/g;\n print;\n}\n"}, {"source_code": "my $l, $s;\n\n\n$l = ;\n\n$s = ;\n\nchomp($s);\n\n\nmy @arr = split //, $s;\nmy @out;\nmy %v = ('a', 1, 'e', 1, 'i', 1, 'o', 1, 'u', 1, 'y', 1);\n\nfor ($i = 0; $i < scalar(@arr); $i++){\n \n $c = $arr[$i];\n \n if (exists($v{$c})){\n $j = $i;\n $t = $arr[$j];\n while( ($t eq $c) and $j < scalar(@arr) ){\n \n $j++;\n $t = $arr[$j];\n }\n if( ($c eq 'o' || $c eq 'e') && ($j-$i)>2 ){\n push(@out, $c);\n $i = $j-1;\n next;\n }\n if( ($c ne 'o' && $c ne 'e') && ($j-$i)>1 ){\n push(@out, $c);\n $i = $j-1;\n next; \n }\n }\n push(@out, $c);\n}\n\n$res = join('', @out);\n\nprint \"$res\\n\";\n\n\n"}, {"source_code": "use warnings;\nuse strict;\nmy $n = int <>;\nmy $s = <>;\nmy @s = split //, $s;\nmy $cur = '-';\nmy $cnt = 0;\nfor my $i (0..$n) {\n if ($s[$i] eq $cur) {\n $cnt += 1;\n } else {\n unless ($cur eq '-') {\n if ($cnt == 2 and ($cur eq 'e' or $cur eq 'o')) {\n print $cur;\n print $cur;\n } elsif ($cur eq 'e' or $cur eq 'o' or $cur eq 'i' or $cur eq 'a' or $cur eq 'y' or $cur eq 'u') {\n print $cur;\n } else {\n for (0..$cnt-1) {\n print $cur;\n }\n }\n }\n $cnt = 1;\n $cur = $s[$i];\n }\n}\nprint \"\\n\";\n\n"}, {"source_code": "<>;\n$line = <>;\n$line =~ s/(?!<=\\1)([aiuy])\\1+(?!=\\1)/\\1/g;\n$line =~ s/(?!<=\\1)([oe])\\1\\1+(?!=\\1)/\\1/g;\nprint \"$line\";\n"}, {"source_code": "$lineno = 0;\nwhile ($line = ){\n if ($lineno == 0) {\n $lineno += 1;\n } else {\n $line =~ s/[a]+/a/g;\n $line =~ s/[e]{3,}/e/g;\n $line =~ s/[i]+/i/g;\n $line =~ s/[o]{3,}/o/g;\n $line =~ s/[u]+/u/g;\n $line =~ s/[y]+/y/g;\n print $line;\n last;\n }\n}"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nmy $len = ;\nmy $str = ;\n$str =~ s/a+/a/g;\n$str =~ s/eee+/e/g;\n$str =~ s/i+/i/g;\n$str =~ s/ooo+/o/g;\n$str =~ s/u+/u/g;\n$str =~ s/y+/y/g;\nprint $str;"}, {"source_code": "$i = 0;\nwhile (<>) {\n\tif ($i == 1) {\n\t\ts/([aiuy])\\1*/\\1/g;\n\t\ts/([eo])\\1{2,}/\\1/g;\n\t\tprint $_;\n\t} else {\n\t\t$i = 1;\n\t}\n}\n"}, {"source_code": "#!/usr/bin/perl\n$_=<>;\n$_=<>;\ns/(a){2,}|(y){2,}|(i){2,}|(u){2,}|(e){3,}|(o){3,}|(.)/\\1\\2\\3\\4\\5\\6\\7/g;\nprint $_;"}, {"source_code": "$string = <>; $string = <>;\n$string =~ s/a+/a/g;\n$string =~ s/i+/i/g;\n$string =~ s/u+/u/g;\n$string =~ s/y+/y/g;\n$string =~ s/eee+/e/g;\n$string =~ s/ooo+/o/g;\nprint($string)\n"}, {"source_code": "#! /usr/bin/env perl\n\nuse strict;\nuse warnings;\n\n<>;\n$_ = <>;\n\ns/a+/a/g;\ns/eee+/e/g;\ns/i+/i/g;\ns/ooo+/o/g;\ns/u+/u/g;\ns/y+/y/g;\n\nprint $_;\n"}, {"source_code": "$n = <>;\n$s = <>;\n$s =~ s,a+,a,g;\n$s =~ s,eee+,e,g;\n$s =~ s,i+,i,g;\n$s =~ s,ooo+,o,g;\n$s =~ s,u+,u,g;\n$s =~ s,y+,y,g;\nprint $s;"}], "negative_code": [{"source_code": "<>;\n$line = <>;\n# $_ =~ s/i/j/e;\n$line =~ s/(?!<=\\1)([aiuny])\\1+(?!=\\1)/\\1/g;\n$line =~ s/(?!<=\\1)([oe])\\1\\1+(?!=\\1)/\\1/g;\n# s/(?!<=$1)([abcdfghijklmnpqrstuvwxyz])$1+(?!=$1)/$1/g\nprint \"$line\";\n"}, {"source_code": "$lineno = 0;\nwhile ($line = ){\n if ($lineno == 0) {\n $lineno += 1;\n } else {\n $line =~ s/[a]+/a/g;\n $line =~ s/[e]{3,}/e/g;\n $line =~ s/[i]+/i/g;\n $line =~ s/[o]{3,}/o/g;\n $line =~ s/[u]+/u/g;\n print $line;\n last;\n }\n}"}, {"source_code": "#! /usr/bin/env perl\n\nuse strict;\nuse warnings;\n\n<>;\n$_ = <>;\n\ns/a+/a/g;\ns/e+/e/g;\ns/i+/i/g;\ns/o+/o/g;\ns/u+/u/g;\ns/y+/y/g;\n\nprint $_;\n"}, {"source_code": "#! /usr/bin/env perl\n\nuse strict;\nuse warnings;\n\n# my $n = <>;\n# <>;\n$_ = <>;\n\ns/a+/a/g;\ns/e+/e/g;\ns/i+/i/g;\ns/o+/o/g;\ns/u+/u/g;\ns/y+/y/g;\n\nprint $_;\n"}], "src_uid": "8ff1b4cf9875f1301e603b47626d06b4"} {"nl": {"description": "The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops.If $$$x$$$ is the number of passengers in a bus just before the current bus stop and $$$y$$$ is the number of passengers in the bus just after current bus stop, the system records the number $$$y-x$$$. So the system records show how number of passengers changed.The test run was made for single bus and $$$n$$$ bus stops. Thus, the system recorded the sequence of integers $$$a_1, a_2, \\dots, a_n$$$ (exactly one number for each bus stop), where $$$a_i$$$ is the record for the bus stop $$$i$$$. The bus stops are numbered from $$$1$$$ to $$$n$$$ in chronological order.Determine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $$$w$$$ (that is, at any time in the bus there should be from $$$0$$$ to $$$w$$$ passengers inclusive).", "input_spec": "The first line contains two integers $$$n$$$ and $$$w$$$ $$$(1 \\le n \\le 1\\,000, 1 \\le w \\le 10^{9})$$$ \u2014 the number of bus stops and the capacity of the bus. The second line contains a sequence $$$a_1, a_2, \\dots, a_n$$$ $$$(-10^{6} \\le a_i \\le 10^{6})$$$, where $$$a_i$$$ equals to the number, which has been recorded by the video system after the $$$i$$$-th bus stop.", "output_spec": "Print the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $$$w$$$. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0.", "sample_inputs": ["3 5\n2 1 -3", "2 4\n-1 1", "4 10\n2 4 1 2"], "sample_outputs": ["3", "4", "2"], "notes": "NoteIn the first example initially in the bus could be $$$0$$$, $$$1$$$ or $$$2$$$ passengers.In the second example initially in the bus could be $$$1$$$, $$$2$$$, $$$3$$$ or $$$4$$$ passengers.In the third example initially in the bus could be $$$0$$$ or $$$1$$$ passenger."}, "positive_code": [{"source_code": "# author : Gabriel Hofer\nuse strict;\nuse warnings;\nuse constant NL => qq/\\n/;\nsub max { $_ [ 0 ] > $_ [ 1 ] ? $_ [ 0 ] : $_ [ 1 ] }\nmy ( $n, $w ) = split qq/ /, ;\nmy @one = split qq/ /, ;\n# assumption : 0 people are initially on the bus\nmy ($max, $min, $sum);\n$max = $min = $sum = 0;\nAA: for my $i ( @one ) {\n $sum += $i;\n if ( $sum < $min ) { $min = $sum }\n if ( $sum > $max ) { $max = $sum }\n}\nprint (max (0, ($w - ($max - $min) + 1)), NL);\n\n\n"}, {"source_code": "( $n, $w ) = split ' ', <>;\n\nfor( split ' ', <> ){\n\t$s += $_;\n\t$s < $m and $m = $s;\n\t$s > $M and $M = $s;\n\t}\n\t\n$A = $w - $M + 1 + $m;\n\nprint $A < 0 ? 0 : $A"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $w ) = split;\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\tmy $min = 0;\n\tmy $max = 0;\n\t\n\tfor( @_ ){\n\t\t$sum += $_;\n\t\t$sum < $min and $min = $sum;\n\t\t$sum > $max and $max = $sum;\n\t\t}\n\t\n#%\tprint \"[$min|$max]\";\n\t\n\tmy $ans = ( $w - $max + 1 ) - ( -$min );\n\t\n\tprint $ans < 0 ? 0 : $ans;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy( $n, $w ) = split;\n\t@_ = split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\tmy $min = 0;\n\tmy $max = 0;\n\t\n\tfor( @_ ){\n\t\t$sum += $_;\n\t\t$sum < $min and $min = $sum;\n\t\t$sum > $max and $max = $sum;\n\t\t}\n\t\n#%\tprint \"[$min|$max]\";\n\t\n\tprint 1 + $w - ( $max - $min );\n\t}"}], "src_uid": "8cf5d08a319672d9b767d3523eca4df6"} {"nl": {"description": "Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob \u2014 to check his answers, he needs a program that among the given n numbers finds one that is different in evenness.", "input_spec": "The first line contains integer n (3\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 amount of numbers in the task. The second line contains n space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness.", "output_spec": "Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order.", "sample_inputs": ["5\n2 4 7 8 10", "4\n1 2 1 1"], "sample_outputs": ["3", "2"], "notes": null}, "positive_code": [{"source_code": "my $n = int<>;\nmy $odd = 0;\nmy @arr = split / /, <>;\n$odd += $_ % 2 for(@arr);\nmy %hash;\n$hash{$arr[$_ - 1] % 2} = $_ foreach(1..$n);\n\nif ($odd == 1) {\n print $hash{1};\n}\nelse {\n print $hash{0};\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@N = (0, 0);\n@a = (0, 0);\n$i = 1;\nforeach (split / /, <>) {\n\t$a[$_ & 1] = $i++;\n\t$N[$_ & 1]++;\n}\n$N[0]>1 and say $a[1] or say $a[0];\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\nmy ($odd, $even, $last_odd, $last_even) = (0, 0, 0, 0);\n$_ = <>;\nmy @numbers = split(' ', <>);\n\n\nforeach my $i (1..scalar(@numbers))\n{\n my $number = $numbers[$i-1];\n if ($number % 2 == 0)\n {\n $even++;\n $last_even = $i;\n }\n else\n {\n $odd++;\n $last_odd = $i;\n }\n}\n\nprint $even > $odd ? $last_odd : $last_even;\n"}, {"source_code": "while (<>){\n @_=@m=split/ /,<>;\n $i=$j=0;\n for (1..3){(shift @m) % 2 and $i++};\n if ($i>1){\n for (@_){\n $j++;\n $_ % 2 or last\n }\n }\n else {\n for (@_){\n $j++;\n $_ % 2 and last\n }\n }\n print \"$j\\n\";\n }"}], "negative_code": [], "src_uid": "dd84c2c3c429501208649ffaf5d91cee"} {"nl": {"description": "You came to a local shop and want to buy some chocolate bars. There are $$$n$$$ bars in the shop, $$$i$$$-th of them costs $$$a_i$$$ coins (and you want to buy all of them).You have $$$m$$$ different coupons that allow you to buy chocolate bars. $$$i$$$-th coupon allows you to buy $$$q_i$$$ chocolate bars while you have to pay only for the $$$q_i - 1$$$ most expensive ones (so, the cheapest bar of those $$$q_i$$$ bars is for free).You can use only one coupon; if you use coupon $$$i$$$, you have to choose $$$q_i$$$ bars and buy them using the coupon, and buy all the remaining $$$n - q_i$$$ bars without any discounts.To decide which coupon to choose, you want to know what will be the minimum total amount of money you have to pay if you use one of the coupons optimally.", "input_spec": "The first line contains one integer $$$n$$$ ($$$2 \\le n \\le 3 \\cdot 10^5$$$) \u2014 the number of chocolate bars in the shop. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the cost of $$$i$$$-th chocolate bar. The third line contains one integer $$$m$$$ ($$$1 \\le m \\le n - 1$$$) \u2014 the number of coupons you have. The fourth line contains $$$m$$$ integers $$$q_1$$$, $$$q_2$$$, ..., $$$q_m$$$ ($$$2 \\le q_i \\le n$$$), where $$$q_i$$$ is the number of chocolate bars you have to buy using $$$i$$$-th coupon so that the least expensive of them will be for free. All values of $$$q_i$$$ are pairwise distinct.", "output_spec": "Print $$$m$$$ integers, $$$i$$$-th of them should be the minimum amount of money you have to pay if you buy $$$q_i$$$ bars with $$$i$$$-th coupon, and all the remaining bars one by one for their full price.", "sample_inputs": ["7\n7 1 3 1 4 10 8\n2\n3 4"], "sample_outputs": ["27\n30"], "notes": "NoteConsider the first example.If we use the first coupon, we may choose chocolate bars having indices $$$1$$$, $$$6$$$ and $$$7$$$, and we pay $$$18$$$ coins for them and $$$9$$$ coins for all other bars.If we use the second coupon, we may choose chocolate bars having indices $$$1$$$, $$$5$$$, $$$6$$$ and $$$7$$$, and we pay $$$25$$$ coins for them and $$$5$$$ coins for all other bars."}, "positive_code": [{"source_code": "<>;\n\n$z += $_ for @_ = sort { $b <=> $a } split ' ', <>;\n\n<>;\n\t\nprint join $/, map { $z - $_[ $_ - 1 ] } split ' ', <>;"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = sort { $b <=> $a } split ' ', <>;\n\t\n\tmy $sum = 0;\n\t\n\t$sum += $_ for @_;\n\t\n\t<>;\n\t\n\tmy @ans;\n\t\n\tfor my $i ( split ' ', <> ){\n\t\tpush @ans, $sum - $_[ $i - 1 ];\n\t\t}\n\t\n\tprint for @ans;\n\t}"}], "negative_code": [], "src_uid": "c1608f6f71cac56690e31aa130c1990e"} {"nl": {"description": "Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n in the order of tram's movement. At the i-th stop ai passengers exit the tram, while bi passengers enter it. The tram is empty before it arrives at the first stop. Also, when the tram arrives at the last stop, all passengers exit so that it becomes empty.Your task is to calculate the tram's minimum capacity such that the number of people inside the tram at any time never exceeds this capacity. Note that at each stop all exiting passengers exit before any entering passenger enters the tram.", "input_spec": "The first line contains a single number n (2\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of the tram's stops. Then n lines follow, each contains two integers ai and bi (0\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u20091000) \u2014 the number of passengers that exits the tram at the i-th stop, and the number of passengers that enter the tram at the i-th stop. The stops are given from the first to the last stop in the order of tram's movement. The number of people who exit at a given stop does not exceed the total number of people in the tram immediately before it arrives at the stop. More formally, . This particularly means that a1\u2009=\u20090. At the last stop, all the passengers exit the tram and it becomes empty. More formally, . No passenger will enter the train at the last stop. That is, bn\u2009=\u20090. ", "output_spec": "Print a single integer denoting the minimum possible capacity of the tram (0 is allowed).", "sample_inputs": ["4\n0 3\n2 5\n4 2\n4 0"], "sample_outputs": ["6"], "notes": "NoteFor the first example, a capacity of 6 is sufficient: At the first stop, the number of passengers inside the tram before arriving is 0. Then, 3 passengers enter the tram, and the number of passengers inside the tram becomes 3. At the second stop, 2 passengers exit the tram (1 passenger remains inside). Then, 5 passengers enter the tram. There are 6 passengers inside the tram now. At the third stop, 4 passengers exit the tram (2 passengers remain inside). Then, 2 passengers enter the tram. There are 4 passengers inside the tram now. Finally, all the remaining passengers inside the tram exit the tram at the last stop. There are no passenger inside the tram now, which is in line with the constraints. Since the number of passengers inside the tram never exceeds 6, a capacity of 6 is sufficient. Furthermore it is not possible for the tram to have a capacity less than 6. Hence, 6 is the correct answer."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse 5.012;\nuse warnings;\n\nchomp(my $stops = <>);\n\nmy $cnt = 0;\nmy $max = 0;\nwhile ($stops--) {\n\tchomp(my $line = <>);\n\tmy ($exit, $enter) = split / /, $line;\n\t$cnt = $cnt - $exit + $enter;\n\t$max = $cnt if $cnt > $max;\n}\nprint $max;\n"}, {"source_code": "my $n = <>;\nmy ($a, $b, $pas, $ans) = (0, 0, 0, 0);\nfor (1..$n) {\n ($a, $b) = split \" \", <>;\n $pas -= $a;\n $pas += $b;\n $ans = $pas if ($pas > $ans);\n}\nprint $ans ,\"\\n\";\n"}, {"source_code": "my $n = ;\nmy $max = 0;\nmy $count = 0;\nwhile($n--) {\n my($a, $b) = split(' ', );\n $count = $count - $a + $b;\n $max = ($max > $count) ? $max : $count;\n}\nprint $max;"}, {"source_code": "\n/ /, ($c+=$'-$`) > $m and $m = $c\nfor <>;\nprint 0+$m"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\n\n\nchomp($n = <>);\n$ans = 0;\n$tot = 0;\nwhile ($n-- > 0) {\n\t($a, $b) = split / /, <>;\n\t$tot += $b - $a;\n\t$ans = $tot>$ans ? $tot:$ans;\n}\nprint $ans;\n"}, {"source_code": "use strict;\nuse warnings;\n\n\nmy $n = <>;\nchomp($n);\ndie('n is wrong') unless $n >= 2 && $n <= 1000;\n\nmy $max = 0;\nmy $current = 0;\nfor (my $i = 0; $i < $n; $i++) {\n my $line = <>;\n chomp($line);\n my ($a, $b) = split(' ', $line);\n #check if a,b is 0 $max) {\n $max = $current;\n }\n}\n\nprint $max;\n"}, {"source_code": "chomp($numstops=);\nmy $max = -1;\nmy $capa = 0;\nwhile($numstops-- > 0) {\n\tchomp($line = );\n\t($l, $b) = $line =~ /(\\d+)\\s+(\\d+)/;\n\t\n\t$capa = $capa - $l + $b;\n\t$max = $capa if($capa > $max);\t\n}\nprint \"$max\\n\";"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy $mx = 0;\nmy $curr = 0;\nfor (1..$n) {\n my ($a, $b) = split ' ', <>;\n $curr += $b - $a;\n $mx = $curr if $mx < $curr;\n}\n\nprint \"$mx\\n\";\n"}, {"source_code": "chomp(my $testCase=);\nmy @maximum;\nmy $sum = 0;\nwhile($testCase){\n\tchomp(my $values = );\n\tmy @arr = split / /,$values;\n\t$sum -= $arr[0];\n\t$sum += $arr[1];\n\tpush (@maximum,$sum);\n\t$testCase--;\n}\nmy $answer;\nmy @articles = sort {$b <=> $a} @maximum;\n$answer = $articles[0];\nprint $answer;"}, {"source_code": "; my ($count, $res)=(0,0); do{ chomp; my ($out, $in) = split(\" \"); $count-=$out; $count += $in; ;$res = ($res > $count ? $res : $count) } while ; print \"$res\\n\""}, {"source_code": "; @res=(0,0); do{ chomp; my ($out, $in) = split \" \", $_; $res[0]-=$out; $res[0] += $in; $res[1] = $res[0] if $res[0] > $res[1] } while ; print \"$res[1]\\n\""}, {"source_code": "$n = <>;\n$mx = 0; $p = 0;\nfor ($i = 0; $i < $n; ++$i) {\n ($out, $in) = split' ', <>;\n $p += $in - $out;\n if ($p > $mx) { $mx = $p; }\n}\nprint $mx;"}, {"source_code": "#!/bin/perl\n\n$n = ;\n\n$m = 0;\n$s = 0;\n\nfor($i=0;$i<$n;$i++)\n{\n\n ($a, $b) = split / +/, ;\n\n $s = $s - $a + $b;\n\n if( $s > $m )\n {\n\n $m = $s;\n\n }\n\n}\n\nprint $m;\n"}, {"source_code": "#!/usr/bin/perl\nuse strict; use warnings;\n\n; my ($max, $cap, $out, $in) = 0;\nwhile ()\n{ ($out, $in) = split ' ', $_; $cap += $in - $out;\n $max = $cap if $max < $cap }\nprint $max . \"\\n\"\n"}, {"source_code": "/ /, ($c+=$'-$`) > $m and $m = $c\nfor <>;\nprint 0+$m"}], "negative_code": [{"source_code": "$i=<>;\nwhile($i--){\n$_=<>;\n/(\\d+) (\\d+)/;\n$in = -$1 + $2;\n$cap += $in;\nif ($cap > $max) {$max = $cap} \n}\nprint $max"}, {"source_code": "use warnings;\nuse strict;\n\n\n\nmy $in = <>;\nchomp($in);\ndie('to many chars') unless length($in) <= 1000;\n\nmy @chars = split('', $in);\n\n$chars[0] = uc($chars[0]);\nmy $out = join('', @chars);\n\n\nprint $out;\n"}, {"source_code": "chomp($numstops=);\nmy $maxcapa = -1;\nmy $capa = 0;\nwhile($numstops-- > 0) {\n\tchomp($line = );\n\t($l, $b) = $line =~ /(\\d+)\\s+(\\d+)/;\n\t\n\t$capa = $capa - $l + $b;\n\t$max = $capa if($capa > $max);\t\n}\nprint \"$max\\n\";"}], "src_uid": "74b90fe9458b147568ac9bd09f219aab"} {"nl": {"description": "The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key\u00a0\u2014 are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B\u00a0\u2014 denoting red, green and blue doors, respectively; r, g, b\u00a0\u2014 denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway\u00a0\u2014 on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 720$$$)\u00a0\u2014 the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.", "output_spec": "For each testcase, print YES if the knight can open all doors. Otherwise, print NO.", "sample_inputs": ["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"], "sample_outputs": ["YES\nNO\nYES\nNO"], "notes": "NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tprint m/^(?=.*r.*R)(?=.*g.*G)(?=.*b.*B)/ ? \"YES\" : \"NO\";\n\t}"}], "negative_code": [], "src_uid": "60eb29b2dfb4d6b136c58b33dbd2558e"} {"nl": {"description": "It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1\u2009+\u20091 to a1\u2009+\u2009a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105), the number of piles. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009103, a1\u2009+\u2009a2\u2009+\u2009...\u2009+\u2009an\u2009\u2264\u2009106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1\u2009\u2264\u2009m\u2009\u2264\u2009105), the number of juicy worms said by Marmot. The fourth line contains m integers q1,\u2009q2,\u2009...,\u2009qm (1\u2009\u2264\u2009qi\u2009\u2264\u2009a1\u2009+\u2009a2\u2009+\u2009...\u2009+\u2009an), the labels of the juicy worms.", "output_spec": "Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.", "sample_inputs": ["5\n2 7 3 4 9\n3\n1 25 11"], "sample_outputs": ["1\n5\n3"], "notes": "NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile. "}, "positive_code": [{"source_code": "<>;\nfor (split/ /,<>){\n $j++;\n $b[$i++]=$j for 1..$_\n }\n<>;\npush @m, $b[$_-1] for split/ /,<>;\n$,=$/;\nprint @m"}, {"source_code": "<>;\nfor (split/ /,<>){\n\t$j++;\n\t$b[$i++]=$j for 1..$_\n\t}\n<>;\npush @m, $b[$_-1] for split/ /,<>;\n$,=$/;\nprint @m\n"}, {"source_code": "<>;\nfor (split/ /,<>){\n\t$j++;\n\t$b[$i++]=$j for 1..$_\n\t}\n<>;\npush @m, $b[$_-1] for split/ /,<>;\n$,=$/;\nprint @m\n"}, {"source_code": "<>;\nfor (split/ /,<>){\n\t$j++;\n\t$b[$i++]=$j for 1..$_\n\t}\n<>;\npush @m, $b[$_-1] for split/ /,<>;\n$,=$/;\nprint @m"}, {"source_code": "<>;\nfor (split/ /,<>){\n\t$j++;\n\t$b[$i++]=$j for 1..$_\n\t}\n<>;\npush @m, $b[$_-1] for split/ /,<>;\n$,=$/;\nprint @m\n"}, {"source_code": "<>;\nfor (split/ /,<>){\n\t$j++;\n\t$b[$i++]=$j for 1..$_\n\t}\n<>;\npush @m, $b[$_-1] for split/ /,<>;\n$,=$/;\nprint @m\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = (0, split / /, <>);\n$a[$_]+=$a[$_-1] foreach (1..$n);\nchomp($m = <>);\n@b = split / /, <>;\npush @ans, [($_, $b[$_])] foreach (0..$m-1);\n@ans = sort { $a->[1] <=> $b->[1] } @ans;\n@arr = (0) x $m;\nfor (($i,$j)=(1,0); $i<=$n; ++$i) {\n\twhile ($j<$m && $ans[$j]->[1]<=$a[$i]) {\n\t\t$arr[$ans[$j]->[0]] = $i;\n\t\t++$j;\n\t}\n\t$j>=$m and last;\n}\nsay $_ foreach (@arr);"}, {"source_code": "chomp (my $n = <>);\nchomp (my @a = split /\\s+/, <>);\nchomp (my $m = <>);\nchomp (my @q = split /\\s+/, <>);\n\nfor (my $i = 1; $i < $n; $i++) {\n $a[$i] += $a[$i-1];\n}\n\n# printf (join \" \", @a) . \"\\n\";\n\nsub find {\n my $f = $_[0];\n my $i = 0;\n my $j = $n;\n my $x;\n \n while ($i < $j) {\n $x = ($i + $j) >> 1;\n if ($f <= $a[$x]) {\n if ($x == 0 || $a[$x-1] < $f) {\n return $x;\n } else {\n $j = $x;\n }\n } else {\n $i = $x;\n }\n }\n}\n\nforeach (@q) {\n printf \"%d\\n\", 1 + &find ($_);\n}"}, {"source_code": "#!/usr/bin/perl -w\n\nmy $piles = <>;\nmy @psize = split(/ /, <>);\nmy $juicy = <>;\n\nchomp ($piles, $juicy);\n\nmy $sum = 0;\nforeach (@psize) {\n $sum += $_;\n $_ = $sum;\n}\n\n# print \"$piles: @psize\\n\";\n\nforeach my $q (split (/ /, <>)) {\n chomp($q);\n my ($start, $end) = (0, $#psize);\n while ($start < $end) {\n\t$mid = ($start + $end) >> 1;\n\tif ($q <= $psize[$mid]) {\n#\t print \"lt: q=$q. st=$start mid=$mid end=$end\\n\";\n\t $end = $mid; # not $mid - 1 like we would for a binary search. We want std::lower_bound style behaviour\n#\t} elsif ($q == $psize[$mid]) {\n#\t $start = $end = $mid;\n\t} else {\n#\t print \"gt: q=$q. st=$start mid=$mid end=$end\\n\";\n\t $start = $mid + 1;\n\t}\n }\n # print $start + 1, \"\\tst=$start en=$end\\n\";\n print $start + 1, \"\\n\";\n}\n"}, {"source_code": "<>;\nfor (split/ /,<>){\n\t$j++;\n\t$b[$i++]=$j for 1..$_\n\t}\n<>;\npush @m, $b[$_-1] for split/ /,<>;\n$,=$/;\nprint @m\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n#use Data::Dumper;\n\nmy @cum;\nsub bs{\n my $val = shift;\n my ($low,$high) = (0 ,scalar @cum);\n while($low < $high){\n my $mid = int(($low + $high) / 2);\n if($cum[$mid] >= $val){$high = $mid;}\n else{$low = $mid + 1;}\n }\n return $low+1;\n}\nmy $n = <>;\nmy @pile = split ' ',<>;\nmy $m = <>;\nmy @lbl = split ' ',<>;\nfor my $i(0..$n-1){\n if($i == 0){\n push @cum, $pile[$i];\n }\n else{\n push @cum, $cum[$#cum]+$pile[$i];\n }\n}\n\nfor my $i(0..$m-1){\n print bs($lbl[$i]),\"\\n\";\n}\n"}, {"source_code": "<>;\nfor (split/ /,<>){\n\t$j++;\n\t$b[$i++]=$j for 1..$_\n\t}\n<>;\npush @m, $b[$_-1] for split/ /,<>;\n$,=$/;\nprint @m\n"}, {"source_code": "<>;\nfor (split/ /,<>){\n\t$j++;\n\t$b[$i++]=$j for 1..$_\n\t}\n<>;\npush @m, $b[$_-1] for split/ /,<>;\n$,=$/;\nprint @m\n"}, {"source_code": "while(<>){\n\t@a=split/ /,<>;\n\t<>;\n\t$_=<>;\n\tchomp;\n\t@_=split/ /;\n\t$h{$_}++ for @_;\n\t\n\t$k=0;\n\tfor (1..10**6){\n\t\t$h{$_} and $h{$_} = $k+1 .\"\\n\";\n\t\tif ($_==$a[$k]){$k++; $a[$k]+=$a[$k-1]}\n\t\t}\n\t\n\tpush @m, $h{$_} for @_;\n\tprint @m\n\t}"}, {"source_code": "<>;\nfor (split/ /,<>){\n\t$j++;\n\t$b[$i++]=$j for 1..$_\n\t}\n<>;\npush @m, $b[$_-1] for split/ /,<>;\n$,=$/;\nprint @m"}, {"source_code": "<>;\nfor (split/ /,<>){\n\t$j++;\n\t$b[$i++]=$j for 1..$_\n\t}\n<>;\npush @m, $b[$_-1] for split/ /,<>;\n$,=$/;\nprint @m\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl -w\n\nmy $piles = <>;\nmy @psize = split(/ /, <>);\nmy $juicy = <>;\n\nchomp ($piles, $juicy);\n\nmy $sum = 0;\nforeach (@psize) {\n $sum += $_;\n $_ = $sum;\n}\n\n# print \"$piles: @psize\\n\";\n\nforeach my $q (split (/ /, <>)) {\n chomp($q);\n my ($start, $end) = (0, $#psize);\n while ($start < $end) {\n $mid = ($start + $end) >> 1;\n if ($q < $psize[$mid]) {\n# print \"lt: q=$q. st=$start mid=$mid end=$end\\n\";\n $end = $mid; # not $mid - 1 like we would for a binary search. We want std::lower_bound style behaviour\n# } elsif ($q == $psize[$mid]) {\n# $start = $end = $mid;\n } else {\n# print \"gt: q=$q. st=$start mid=$mid end=$end\\n\";\n $start = $mid + 1;\n }\n }\n # print $start + 1, \"\\tst=$start en=$end\\n\";\n print $start + 1, \"\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl -w\n\nmy $piles = <>;\nmy @psize = split(/ /, <>);\nmy $juicy = <>;\n\nchomp ($piles, $juicy);\n\nmy $sum = 0;\nforeach (@psize) {\n $sum += $_;\n $_ = $sum;\n}\n\nprint \"$piles: @psize\\n\";\n\nforeach my $q (split (/ /, <>)) {\n chomp($q);\n my ($start, $end) = (0, $#psize);\n while ($start < $end) {\n\t$mid = ($start + $end) >> 1;\n\tif ($q < $psize[$mid]) {\n#\t print \"lt: q=$q. st=$start mid=$mid end=$end\\n\";\n\t $end = $mid; # not $mid - 1 like we would for a binary search. We want std::lower_bound style behaviour\n#\t} elsif ($q == $psize[$mid]) {\n#\t $start = $end = $mid;\n\t} else {\n#\t print \"gt: q=$q. st=$start mid=$mid end=$end\\n\";\n\t $start = $mid + 1;\n\t}\n }\n print $start + 1, \"\\tst=$start en=$end\\n\";\n}\n"}], "src_uid": "10f4fc5cc2fcec02ebfb7f34d83debac"} {"nl": {"description": "There are $$$n$$$ consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.The university team for the Olympiad consists of $$$a$$$ student-programmers and $$$b$$$ student-athletes. Determine the largest number of students from all $$$a+b$$$ students, which you can put in the railway carriage so that: no student-programmer is sitting next to the student-programmer; and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting.Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).", "input_spec": "The first line contain three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 \\le n \\le 2\\cdot10^{5}$$$, $$$0 \\le a, b \\le 2\\cdot10^{5}$$$, $$$a + b > 0$$$) \u2014 total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length $$$n$$$, consisting of characters \".\" and \"*\". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.", "output_spec": "Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.", "sample_inputs": ["5 1 1\n*...*", "6 2 3\n*...*.", "11 3 10\n.*....**.*.", "3 2 3\n***"], "sample_outputs": ["2", "4", "7", "0"], "notes": "NoteIn the first example you can put all student, for example, in the following way: *.AB*In the second example you can put four students, for example, in the following way: *BAB*BIn the third example you can put seven students, for example, in the following way: B*ABAB**A*BThe letter A means a student-programmer, and the letter B \u2014 student-athlete."}, "positive_code": [{"source_code": "use warnings;\nuse strict;\nuse integer;\n$\\ = qq/\\n/, $, = qq/ /;\nmy ( $n, $aa, $bb ) = split qq/ /, ;\nchomp ( my $str = );\nmy $sum = 0;\nsub min { my $one = $_ [ 0 ] < $_ [ 1 ] ? $_ [ 0 ] : $_ [ 1 ] }\nfor my $ii ( $str =~ m/\\.+/g ) {\n if ( length ( $ii ) % 2 == 0 ) {\n $sum += min ( $aa, length ( $ii ) / 2 ) + min ( $bb, length ( $ii ) / 2 );\n $aa -= min ( $aa, length ( $ii ) / 2 );\n $bb -= min ( $bb, length ( $ii ) / 2 );\n } else {\n if ( min ( $aa, ( length ( $ii ) + 1 ) / 2 ) + \n min ( $bb, ( length ( $ii ) + 1 ) / 2 ) > \n length $ii ) {\n if ( $aa >= $bb ) {\n $sum += min ( $aa, ( length ( $ii ) + 1 ) / 2 ) + min ( $bb, ( length ( $ii ) ) / 2 );\n $aa -= min ( $aa, ( length ( $ii ) + 1 ) / 2 );\n $bb -= min ( $bb, ( length ( $ii ) ) / 2 );\n } else {\n $sum += min ( $aa, ( length ( $ii ) ) / 2 ) + min ( $bb, ( length ( $ii ) + 1 ) / 2 );\n $aa -= min ( $aa, ( length ( $ii ) ) / 2 );\n $bb -= min ( $bb, ( length ( $ii ) + 1 ) / 2 );\n }\n } else {\n $sum += min ( $aa, ( length ( $ii ) + 1 ) / 2 ) + min ( $bb, ( length ( $ii ) + 1 ) / 2 );\n $aa -= min ( $aa, ( length ( $ii ) + 1 ) / 2 );\n $bb -= min ( $bb, ( length ( $ii ) + 1 ) / 2 );\n }\n }\n}\nprint $sum;\n"}, {"source_code": "sub min {\n\t(sort {$a<=>$b} @_) [0];\n};\nundef $/; $_ = <>; ($n, $a, $b, $_) = split;\n$p = s/\\.\\.//g;\n$s = y/.//;\n$P = min($a, $b, $p);\n$S = min($a + $b - $P - $P, $s + $p - $P);\nprint $P + $P + $S;"}, {"source_code": "( $n, $A, $B ) = split ' ', <>;\n\n$Z = $A + $B;\n\nfor( map length, split ' ', <> =~ y/*/ /r ){\n\t$d = $_ >> 1;\n\t( $A > $B ? $A : $B ) -= $_ % 2;\n\tmap { $_ -= $d; s/-.*/0/ } $A, $B;\n\t}\n\nprint $Z - $A - $B"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $A, $B ) = split;\n\t\n\t$A > $B and ( $A, $B ) = ( $B, $A );\n\t\n\t$_ = <>, chomp;\n\t\n\t@_ = map length, grep $_, split /\\*/;\n\t\n\t$debug and print \"[@_]\";\n\t\n\tmy @lyg = sort { $a <=> $b } grep $_ % 2 == 0, @_;\n\tmy @nel = sort { $a <=> $b } grep $_ % 2 == 1, @_;\n\t\n\t$debug and print \"[@lyg]\";\n\t$debug and print \"[@nel]\";\n\t\n\tmy $spaces = eval join ' + ', @_;\n\tmy $AB = $A + $B;\n\tmy $all = 0;\n\tmy $cnt = 0;\n\t\n\tNEL:\n\twhile( $A < $B ){\n\t\t$debug and print \" $A < $B\";\n\t\tif( @nel ){\n\t\t\tmy $nel = shift @nel;\n\t\t\tmy $min = $nel >> 1;\n\t\t\t$A -= $min;\n\t\t\t$B -= $min + 1;\n\t\t\t$A < 0 and $A = 0;\n\t\t\t$B < 0 and $B = 0;\n\t\t\t}\n\t\telsif( @lyg ){\n\t\t\tmy $lyg = shift @lyg;\n\t\t\tmy $half = $lyg >> 1;\n\t\t\t$A -= $half;\n\t\t\t$B -= $half;\n\t\t\t$A < 0 and $A = 0;\n\t\t\t$B < 0 and $B = 0;\n\t\t\t}\n\t\telse{\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\twhile( $A == $B ){\n\t\t$debug and print \" $A = $B\";\n\t\tif( @lyg ){\n\t\t\tmy $lyg = shift @lyg;\n\t\t\tmy $half = $lyg >> 1;\n\t\t\t$A -= $half;\n\t\t\t$B -= $half;\n\t\t\t$A < 0 and $A = 0;\n\t\t\t$B < 0 and $B = 0;\n\t\t\t}\n\t\telsif( @nel ){\n\t\t\tmy $nel = shift @nel;\n\t\t\tmy $min = $nel >> 1;\n\t\t\t$A -= $min + 1;\n\t\t\t$B -= $min;\n\t\t\t$A < 0 and $A = 0;\n\t\t\t$B < 0 and $B = 0;\n\t\t\tgoto NEL;\n\t\t\t}\n\t\telse{\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\t$debug and print \"[all:$all]\";\n\tprint $all ? $spaces : $AB - $A - $B;\n\t\n\t$debug and print '-' x 10;\n\t}"}], "negative_code": [], "src_uid": "6208dbdf9567b759b0026db4af4545a9"} {"nl": {"description": "There is a programing contest named SnakeUp, 2n people want to compete for it. In order to attend this contest, people need to form teams of exactly two people. You are given the strength of each possible combination of two people. All the values of the strengths are distinct.Every contestant hopes that he can find a teammate so that their team\u2019s strength is as high as possible. That is, a contestant will form a team with highest strength possible by choosing a teammate from ones who are willing to be a teammate with him/her. More formally, two people A and B may form a team if each of them is the best possible teammate (among the contestants that remain unpaired) for the other one. Can you determine who will be each person\u2019s teammate?", "input_spec": "There are 2n lines in the input. The first line contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009400) \u2014 the number of teams to be formed. The i-th line (i\u2009>\u20091) contains i\u2009-\u20091 numbers ai1, ai2, ... , ai(i\u2009-\u20091). Here aij (1\u2009\u2264\u2009aij\u2009\u2264\u2009106, all aij are distinct) denotes the strength of a team consisting of person i and person j (people are numbered starting from 1.)", "output_spec": "Output a line containing 2n numbers. The i-th number should represent the number of teammate of i-th person.", "sample_inputs": ["2\n6\n1 2\n3 4 5", "3\n487060\n3831 161856\n845957 794650 976977\n83847 50566 691206 498447\n698377 156232 59015 382455 626960"], "sample_outputs": ["2 1 4 3", "6 5 4 3 2 1"], "notes": "NoteIn the first sample, contestant 1 and 2 will be teammates and so do contestant 3 and 4, so the teammate of contestant 1, 2, 3, 4 will be 2, 1, 4, 3 respectively."}, "positive_code": [{"source_code": "my $n = int<>;\nmy @arr;\nmy @marked;\nmy @team = 0;\nmy @map;\nmy ($num1, $num2);\n$marked[$_] = 0 for(0..2 * $n);\n\nfor my $i(2..2 * $n) {\n @map = split / /, <>;\n @map = map {[$map[$_ - 1], ($i, $_)]} (1..scalar @map);\n push @arr, @map;\n}\n@arr = reverse sort {$a->[0] <=> $b->[0]} @arr;\n\nfor(@arr) {\n ($num1, $num2) = ($_->[1], $_->[2]);\n if ($marked[$num1] == 0 && $marked[$num2] == 0) {\n $team[$num1] = $num2;\n $team[$num2] = $num1;\n $marked[$num1] = 1;\n $marked[$num2] = 1;\n }\n}\nshift @team;\nprint \"@team\\n\";\n"}], "negative_code": [], "src_uid": "8051385dab9d7286f54fd332c64e836e"} {"nl": {"description": "Arkady's morning seemed to be straight of his nightmare. He overslept through the whole morning and, still half-asleep, got into the tram that arrived the first. Some time after, leaving the tram, he realized that he was not sure about the line number of the tram he was in.During his ride, Arkady woke up several times and each time he saw the tram stopping at some stop. For each stop he knows which lines of tram stop there. Given this information, can you help Arkady determine what are the possible lines of the tram he was in?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 100$$$)\u00a0\u2014 the number of stops Arkady saw. The next $$$n$$$ lines describe the stops. Each of them starts with a single integer $$$r$$$ ($$$1 \\le r \\le 100$$$)\u00a0\u2014 the number of tram lines that stop there. $$$r$$$ distinct integers follow, each one between $$$1$$$ and $$$100$$$, inclusive,\u00a0\u2014 the line numbers. They can be in arbitrary order. It is guaranteed that Arkady's information is consistent, i.e. there is at least one tram line that Arkady could take.", "output_spec": "Print all tram lines that Arkady could be in, in arbitrary order.", "sample_inputs": ["3\n3 1 4 6\n2 1 4\n5 10 5 6 4 1", "5\n1 1\n10 10 9 8 7 100 5 4 3 99 1\n5 1 2 3 4 5\n5 4 1 3 2 5\n4 10 1 5 3"], "sample_outputs": ["1 4", "1"], "notes": "NoteConsider the first example. Arkady woke up three times. The first time he saw a stop with lines $$$1$$$, $$$4$$$, $$$6$$$. The second time he saw a stop with lines $$$1$$$, $$$4$$$. The third time he saw a stop with lines $$$10$$$, $$$5$$$, $$$6$$$, $$$4$$$ and $$$1$$$. He can be in a tram of one of two lines: $$$1$$$ or $$$4$$$."}, "positive_code": [{"source_code": "$x = <> - 1;\n\n( join ' ', sort split ' ', join ' ', map s/\\d+ //r, <> ) =~ /\\b(\\d+)\\b(?: \\1\\b){$x}(?{ print \"$1 \" })(*F)/;"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = map s/\\d+ //r, map ~~<>, 1 .. $_;\n\t\n\tmy %h;\n\t\n\tmap { map $h{ $_ } ++, split } @_;\n\t\n\tmy $is = $_;\n\t\n\tmy @ans = grep { $is == $h{ $_ } } keys %h;\n\t\n\tprint \"@ans\";\n\t}"}, {"source_code": "$x = <>;\n\nmap { map $h{ $_ } ++, split } map s/\\d+ //r, <>;\n\nprint join ' ', grep $x == $h{ $_ }, keys %h;"}], "negative_code": [{"source_code": "$x = <> - 1;\n\n( join '', <> ) =~ /( \\d+)(*MARK:A)\\b(?=(.*?($ (*SKIP:A)|)\\1\\b){$x})(?{ print 0 + $1 . ' ' })(*F)/sx;\n"}], "src_uid": "16c54cf7d8b484b5e22a7d391fdc5cd3"} {"nl": {"description": "Valera loves to participate in competitions. Especially in programming contests. Today he has participated in the contest with his team, consisting of n students (including Valera). This contest was an individual competition, so each student in the team solved problems individually.After the contest was over, Valera was interested in results. He found out that: each student in the team scored at least l points and at most r points; in total, all members of the team scored exactly sall points; the total score of the k members of the team who scored the most points is equal to exactly sk; more formally, if a1,\u2009a2,\u2009...,\u2009an is the sequence of points earned by the team of students in the non-increasing order (a1\u2009\u2265\u2009a2\u2009\u2265\u2009...\u2009\u2265\u2009an), then sk\u2009=\u2009a1\u2009+\u2009a2\u2009+\u2009...\u2009+\u2009ak. However, Valera did not find out exactly how many points each of n students scored. Valera asked you to recover any distribution of scores between the students of the team, such that all the conditions above are met.", "input_spec": "The first line of the input contains exactly six integers n,\u2009k,\u2009l,\u2009r,\u2009sall,\u2009sk (1\u2009\u2264\u2009n,\u2009k,\u2009l,\u2009r\u2009\u2264\u20091000; l\u2009\u2264\u2009r; k\u2009\u2264\u2009n; 1\u2009\u2264\u2009sk\u2009\u2264\u2009sall\u2009\u2264\u2009106). It's guaranteed that the input is such that the answer exists.", "output_spec": "Print exactly n integers a1,\u2009a2,\u2009...,\u2009an \u2014 the number of points each student scored. If there are multiple solutions, you can print any of them. You can print the distribution of points in any order. ", "sample_inputs": ["5 3 1 3 13 9", "5 3 1 3 15 9"], "sample_outputs": ["2 3 2 3 3", "3 3 3 3 3"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nwhile(<>){\n\t@KK=();\n\t@MM=();\n\t($n, $k, $l, $r, $sa, $sk) = split/ /;\n#\tprint \" $n $m $k\";\n# @_=split/ /,<>;\n# div($sk, $k); \n\n$csk=$sk;\n$ck=$k;\nwhile ($ck--){\n\tpush @KK, 0;\n\t}\nwhile ($csk--){\n\t$KK[$csk%$k]++;\n\t}\n\t# print \" @KK\\n\";\n$d=$sa-$sk;\n$cm=$m=$n-$k;\nwhile ($cm--){\n\tpush @MM, 0;\n\t}\nwhile ($d--){\n\t$MM[$d%$m]++;\n\t}\n\t# print \" @MM\\n\";\n\nprint \"@KK @MM\\n\";\n}\n\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nwhile(<>){\n\t@KK=();\n\t@MM=();\n\t($n, $k, $l, $r, $sa, $sk) = split/ /;\n#\tprint \" $n $m $k\";\n# @_=split/ /,<>;\n# div($sk, $k); \n\n$csk=$sk;\n$ck=$k;\nwhile ($ck--){\n\tpush @KK, 0;\n\t}\nwhile ($csk--){\n\t$KK[$csk%$k]++;\n\t}\n\tprint \" @KK\\n\";\n$d=$sa-$sk;\n$cm=$m=$n-$k;\nwhile ($cm--){\n\tpush @MM, 0;\n\t}\nwhile ($d--){\n\t$MM[$d%$m]++;\n\t}\n\tprint \" @MM\\n\";\n\nprint \"@KK @MM\\n\";\n}\n\n\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "src_uid": "59154ca15716f0c1c91a37d34c5bbf1d"} {"nl": {"description": "Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell $$$(0, 0)$$$ on an infinite grid.You also have the sequence of instructions of this robot. It is written as the string $$$s$$$ consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell $$$(x, y)$$$ right now, he can move to one of the adjacent cells (depending on the current instruction). If the current instruction is 'L', then the robot can move to the left to $$$(x - 1, y)$$$; if the current instruction is 'R', then the robot can move to the right to $$$(x + 1, y)$$$; if the current instruction is 'U', then the robot can move to the top to $$$(x, y + 1)$$$; if the current instruction is 'D', then the robot can move to the bottom to $$$(x, y - 1)$$$. You've noticed the warning on the last page of the manual: if the robot visits some cell (except $$$(0, 0)$$$) twice then it breaks.So the sequence of instructions is valid if the robot starts in the cell $$$(0, 0)$$$, performs the given instructions, visits no cell other than $$$(0, 0)$$$ two or more times and ends the path in the cell $$$(0, 0)$$$. Also cell $$$(0, 0)$$$ should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: \"UD\", \"RL\", \"UUURULLDDDDLDDRRUU\", and the following are considered invalid: \"U\" (the endpoint is not $$$(0, 0)$$$) and \"UUDD\" (the cell $$$(0, 1)$$$ is visited twice).The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move. Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).You have to answer $$$q$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 2 \\cdot 10^4$$$) \u2014 the number of test cases. The next $$$q$$$ lines contain test cases. The $$$i$$$-th test case is given as the string $$$s$$$ consisting of at least $$$1$$$ and no more than $$$10^5$$$ characters 'L', 'R', 'U' and 'D' \u2014 the initial sequence of instructions. It is guaranteed that the sum of $$$|s|$$$ (where $$$|s|$$$ is the length of $$$s$$$) does not exceed $$$10^5$$$ over all test cases ($$$\\sum |s| \\le 10^5$$$).", "output_spec": "For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions $$$t$$$ the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is $$$0$$$, you are allowed to print an empty line (but you can don't print it).", "sample_inputs": ["6\nLRU\nDURLDRUDRULRDURDDL\nLRUDDLRUDRUL\nLLLLRRRR\nURDUR\nLLL"], "sample_outputs": ["2\nLR\n14\nRUURDDDDLLLUUR\n12\nULDDDRRRUULL\n2\nLR\n2\nUD\n0"], "notes": "NoteThere are only two possible answers in the first test case: \"LR\" and \"RL\".The picture corresponding to the second test case: Note that the direction of traverse does not matter Another correct answer to the third test case: \"URDDLLLUURDR\"."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, split //, 'LURD' . $_;\n\t\n\tmap $h{ $_ } --, keys %h;\n\t\n\t\n\t( $h{ 'L' }, $h{ 'R' } ) = ( ( sort { $a <=> $b } $h{ 'L' }, $h{ 'R' } )[ 0 ] ) x 2;\n\t( $h{ 'U' }, $h{ 'D' } ) = ( ( sort { $a <=> $b } $h{ 'U' }, $h{ 'D' } )[ 0 ] ) x 2;\n\t\n\tif( $h{ 'L' } and $h{ 'U' } ){\n\t\tprint $h{ 'L' } + $h{ 'U' } << 1;\n\t\tprint join '', map $_ x $h{ $_ }, split //, 'LURD';\n\t\t}\n\telsif( $h{ 'L' } or $h{ 'U' } ){\n\t\tprint 2;\n\t\tprint 'LR' x !! $h{ 'L' } . 'UD' x !! $h{ 'U' };\n\t\t}\n\telse{\n\t\tprint 0;\n\t\tprint '';\n\t\t}\n\t}"}, {"source_code": "$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, split //, 'LURD' . $_;\n\t\n\tmap $h{ $_ } --, keys %h;\n\t\n\t@h{ split //, 'LRUD' } = map { ( ( sort { $a <=> $b } @h{ split // } )[ 0 ] ) x 2 } 'LR', 'UD';\n\t\n\tmy $ans = join '', map $_ x $h{ $_ }, split //, 'LURD';\n\t\n\t( $ans ) = $ans =~ /LR|UD/g if $h{ 'L' } xor $h{ 'U' };\n\t\n\tprint for length $ans, $ans;\n\t}"}, {"source_code": "$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy %h;\n\t\n\tmap $h{ $_ } ++, split //, 'LURD' . $_;\n\t\n\tmap $h{ $_ } --, keys %h;\n\t\n\t@h{ split //, 'LRUD' } = map { ( ( sort { $a <=> $b } @h{ split // } )[ 0 ] ) x 2 } 'LR', 'UD';\n\t\n\tmy $ans = do { \n\t\tif( $h{ 'L' } and $h{ 'U' } ){\n\t\t\tjoin '', map $_ x $h{ $_ }, split //, 'LURD';\n\t\t\t}\n\t\telsif( $h{ 'L' } or $h{ 'U' } ){\n\t\t\t'LR' x !! $h{ 'L' } . 'UD' x !! $h{ 'U' };\n\t\t\t}\n\t\telse{\n\t\t\t'';\n\t\t\t}\n\t\t};\n\t\n\tprint for length $ans, $ans;\n\t}"}], "negative_code": [], "src_uid": "1fba9a290d0492a3d658a7a33388db13"} {"nl": {"description": "A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it's new tablet Lastus 3000.This new device is equipped with specially designed artificial intelligence (AI). Employees of Pineapple did their best to postpone the release of Lastus 3000 as long as possible. Finally, they found out, that the name of the new artificial intelligence is similar to the name of the phone, that Pineapple released 200 years ago. As all rights on its name belong to Pineapple, they stand on changing the name of Gogol's artificial intelligence.Pineapple insists, that the name of their phone occurs in the name of AI as a substring. Because the name of technology was already printed on all devices, the Gogol's director decided to replace some characters in AI name with \"#\". As this operation is pretty expensive, you should find the minimum number of characters to replace with \"#\", such that the name of AI doesn't contain the name of the phone as a substring.Substring is a continuous subsequence of a string.", "input_spec": "The first line of the input contains the name of AI designed by Gogol, its length doesn't exceed 100\u2009000 characters. Second line contains the name of the phone released by Pineapple 200 years ago, its length doesn't exceed 30. Both string are non-empty and consist of only small English letters.", "output_spec": "Print the minimum number of characters that must be replaced with \"#\" in order to obtain that the name of the phone doesn't occur in the name of AI as a substring.", "sample_inputs": ["intellect\ntell", "google\napple", "sirisiri\nsir"], "sample_outputs": ["1", "0", "2"], "notes": "NoteIn the first sample AI's name may be replaced with \"int#llect\".In the second sample Gogol can just keep things as they are.In the third sample one of the new possible names of AI may be \"s#ris#ri\"."}, "positive_code": [{"source_code": "$_ = <>, chomp;\n$A = <>;\nchomp $A;\nprint 0 + (() = /$A/g)"}], "negative_code": [{"source_code": "$\\ = $/;\n\n$_ = <>, chomp;\n$A = <>;\nchomp $A;\n\nwhile( m/(?=$A)/g ){\n\tpush @pos, pos;\n\t}\n\n$i = shift @pos;\n$len = 1;\n\nfor (@pos){\n\tif ($i + 1 == $_){\n\t\t$len ++;\n\t\t}\n\telse {\n\t\tpush @len, $len;\n\t\t$len = 1;\n\t\t}\n\t$i = $_;\n\t}\n\npush @len, $len;\n\nmap { $_ += -1 + length $A } @len;\n\n$ans = 0;\n\nfor (@len){\n\t$ans += (int $_ / length $A) + !! ($_ % length $A);\n\t}\n\t\nprint $ans"}, {"source_code": "$\\ = $/;\n\n$_ = <>, chomp;\n$A = <>;\nchomp $A;\n\nwhile( m/(?=$A)/g ){\n\tpush @pos, pos;\n\t}\n\n$i = shift @pos;\n$len = 1;\n\nfor (@pos){\n\tif ($i + 1 == $_){\n\t\t$len ++;\n\t\t}\n\telse {\n\t\tpush @len, $len;\n\t\t$len = 1;\n\t\t}\n\t$i = $_;\n\t}\n\npush @len, $len if m/(?=$A)/;\n\nmap { $_ += -1 + length $A } @len;\n\n$ans = 0;\n\nfor (@len){\n\t$ans += (int $_ / length $A) + !! ($_ % length $A);\n\t}\n\t\nprint $ans"}], "src_uid": "62a672fcaee8be282700176803c623a7"} {"nl": {"description": "Sereja has an array a, consisting of n integers a1, a2, ..., an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1,\u2009l2,\u2009...,\u2009lm (1\u2009\u2264\u2009li\u2009\u2264\u2009n). For each number li he wants to know how many distinct numbers are staying on the positions li, li\u2009+\u20091, ..., n. Formally, he want to find the number of distinct numbers among ali,\u2009ali\u2009+\u20091,\u2009...,\u2009an.?Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.", "input_spec": "The first line contains two integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009105). The second line contains n integers a1, a2, ..., an (1\u2009\u2264\u2009ai\u2009\u2264\u2009105) \u2014 the array elements. Next m lines contain integers l1,\u2009l2,\u2009...,\u2009lm. The i-th line contains integer li (1\u2009\u2264\u2009li\u2009\u2264\u2009n).", "output_spec": "Print m lines \u2014 on the i-th line print the answer to the number li.", "sample_inputs": ["10 10\n1 2 3 4 1 2 3 4 100000 99999\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10"], "sample_outputs": ["6\n6\n6\n6\n6\n5\n4\n3\n2\n1"], "notes": null}, "positive_code": [{"source_code": "($n,$m)=split(' ',<>);\n@b=reverse(split(' ',<>));\n\n$c=0; for ($i=0; $i<@b; $i++) {\n\t$a=$b[$i];\n\tif ($t[$a]!=1) {\n\t\t$t[$a]=1;\n\t\t$c++;\n\t}\n\t$w[$i]=$c;\n}\n\nfor (1..$m) {\n\t$l=<>; chomp($l);\n\tprint $w[$n-$l], \"\\n\";\n}"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n@_=split/ /,<>;\n\nfor (1..$`){\n\t$a[pop @_]++ or $i++;\n\tunshift @b, $i;\n\t}\n\nwhile (<>){\n\tprint \"$b[$_-1]\\n\";\n\t}\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [], "src_uid": "1e156dfc65ef88f19ca1833f75192259"} {"nl": {"description": "Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards by the end of the game, wins.Sereja and Dima are being greedy. Each of them chooses the card with the larger number during his move.Inna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her.", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000) \u2014 the number of cards on the table. The second line contains space-separated numbers on the cards from left to right. The numbers on the cards are distinct integers from 1 to 1000.", "output_spec": "On a single line, print two integers. The first number is the number of Sereja's points at the end of the game, the second number is the number of Dima's points at the end of the game.", "sample_inputs": ["4\n4 1 2 10", "7\n1 2 3 4 5 6 7"], "sample_outputs": ["12 5", "16 12"], "notes": "NoteIn the first sample Sereja will take cards with numbers 10 and 2, so Sereja's sum is 12. Dima will take cards with numbers 4 and 1, so Dima's sum is 5."}, "positive_code": [{"source_code": "my $num = ;\nmy $data = ;\nmy @cards = split(' ', $data);\n\nmy $seroga = 0;\nmy $dima = 0;\nmy $turn = 1;\n\nwhile(@cards)\n{\n my $grt; \n $num--;\n if(@cards[$num] > @cards[0])\n {\n $grt = pop @cards;\n }\n else\n {\n $grt = shift @cards;\n }\n \n if($turn % 2)\n {\n $seroga += $grt;\n }\n else\n {\n $dima += $grt;\n }\n $turn++;\n}\n\nprint $seroga, ' ', $dima;\n\n1;"}, {"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\n$b[0]+=0;$b[1]+=0;\nprint\"@b\"\n"}, {"source_code": "use List::Util \"max\";\n($t, $i, $j) = (0, 0, (<> - 1));\n@a = split \" \", <>;\n@b = (0, 0);\n\nwhile ($i <= $j) {\n if ($a[$i] >= $a[$j]) {\n $b[$t] += $a[$i], ++$i;\n } else {\n $b[$t] += $a[$j], --$j;\n }\n $t = 1 - $t;\n}\n\nprint join \" \", @b;"}, {"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\n$b[0]+=0;$b[1]+=0;\nprint\"@b\"\n"}, {"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\n$b[0]+=0;$b[1]+=0;\nprint\"@b\""}, {"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\n$b[0]+=0;$b[1]+=0;\nprint\"@b\"\n"}, {"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\n$b[0]+=0;$b[1]+=0;\nprint\"@b\"\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@a = (0, split / /, <>);\n($a, $b) = (0, 0);\n($i, $j) = (1, $n);\nforeach (1 .. $n) {\n\tif ($a[$i] > $a[$j]) {\n\t\t$tmp = $a[$i++];\n\t} else {\n\t\t$tmp = $a[$j--];\n\t}\n\tif ($_ & 1) {\n\t\t$a += $tmp;\n\t} else {\n\t\t$b += $tmp;\n\t}\n}\nsay \"$a $b\";"}, {"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\n$b[0]+=0;$b[1]+=0;\nprint\"@b\"\n"}, {"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\n$b[0]+=0;$b[1]+=0;\nprint\"@b\"\n"}, {"source_code": "my $n = ;\nchomp ($n);\nmy $numbers = ;\nchomp ($numbers);\nmy (@data) = split (\" \", $numbers);\n\nmy $ind = 0; my $c = 0; my $serg = 0; my $dima = 0;\n\nwhile ($c != $n) {\n if ($ind != 1) {\n if ($data[0] > $data[(scalar (@data))-1]) { \n $serg = $serg + shift (@data); \n }else{\n $serg = $serg + pop (@data);\n }\n $c++;\n $ind = 1; \n } else { \n if ($data[0] > $data[(scalar (@data))-1]) {\n $dima = $dima + shift (@data);\n }else{\n $dima = $dima + pop (@data);\n }\n $c++;\n $ind = 0;\n }\n}\nprint \"$serg $dima\";"}, {"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\n$b[0]+=0;$b[1]+=0;\nprint\"@b\"\n"}, {"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\n$b[0]+=0;$b[1]+=0;\nprint\"@b\""}, {"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\n$b[0]+=0;$b[1]+=0;\nprint\"@b\"\n"}], "negative_code": [{"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\nprint+$_ for@b"}, {"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\n$_+=0 for@b;\nprint\"@b\""}, {"source_code": "<>; @_=split/ /,<>;\n$b[$i++%2]+=$_[0]>$_[@_-1]?shift@_:pop@_ while@_;\nprint\"@b\""}], "src_uid": "a7e98ed8ee1b0a4fd03dfcd222b68c6f"} {"nl": {"description": "You will receive 5 points for solving this problem.Manao has invented a new operation on strings that is called folding. Each fold happens between a pair of consecutive letters and places the second part of the string above first part, running in the opposite direction and aligned to the position of the fold. Using this operation, Manao converts the string into a structure that has one more level than there were fold operations performed. See the following examples for clarity.We will denote the positions of folds with '|' characters. For example, the word \"ABRACADABRA\" written as \"AB|RACA|DAB|RA\" indicates that it has been folded three times: first, between the leftmost pair of 'B' and 'R' letters; second, between 'A' and 'D'; and third, between the rightmost pair of 'B' and 'R' letters. Here are several examples of folded strings:\"ABCDEF|GHIJK\" | \"A|BCDEFGHIJK\" | \"AB|RACA|DAB|RA\" | \"X|XXXXX|X|X|XXXXXX\" | | | XXXXXX KJIHG | KJIHGFEDCB | AR | X ABCDEF | A | DAB | X | | ACAR | XXXXX | | AB | XOne last example for \"ABCD|EFGH|IJ|K\": KIJHGFEABCDManao noticed that each folded string can be viewed as several piles of letters. For instance, in the previous example, there are four piles, which can be read as \"AHI\", \"BGJK\", \"CF\", and \"DE\" from bottom to top. Manao wonders what is the highest pile of identical letters he can build using fold operations on a given word. Note that the pile should not contain gaps and should start at the bottom level. For example, in the rightmost of the four examples above, none of the piles would be considered valid since each of them has gaps, starts above the bottom level, or both.", "input_spec": "The input will consist of one line containing a single string of n characters with 1\u2009\u2264\u2009n\u2009\u2264\u20091000 and no spaces. All characters of the string will be uppercase letters. This problem doesn't have subproblems. You will get 5 points for the correct submission.", "output_spec": "Print a single integer \u2014 the size of the largest pile composed of identical characters that can be seen in a valid result of folding operations on the given string.", "sample_inputs": ["ABRACADABRA", "ABBBCBDB", "AB"], "sample_outputs": ["3", "3", "1"], "notes": "NoteConsider the first example. Manao can create a pile of three 'A's using the folding \"AB|RACAD|ABRA\", which results in the following structure: ABRADACAR ABIn the second example, Manao can create a pile of three 'B's using the following folding: \"AB|BB|CBDB\". CBDBBBABAnother way for Manao to create a pile of three 'B's with \"ABBBCBDB\" is the following folding: \"AB|B|BCBDB\". BCBDB BABIn the third example, there are no folds performed and the string is just written in one line."}, "positive_code": [{"source_code": "$_ = <>, chomp;\n@_ = split $\\;\nfor $i (0 .. @_ - 1){\n\tundef $m;\n\tfor $j (0 .. 500){\n\t\t\t$j *= 2;\n\t\t\t$j += 1;\n\t\t\t$i - $j < 0 and last;\n\t\t\t$_[ $i ] eq $_[ $i - $j ] or next;\n\t\t\t$m |= 1 x length $b[ $i - $j ]\n\t\t}\n\t$b[ $i ] = $m . 1;\n\t$M |= 1 x length $b[ $i ];\n\t}\nprint length $M"}, {"source_code": "for $i ( 0 .. ( @_ = split $\\, ($_ = <>, chomp, $_) ) - 1){\n\tfor $j ( ($m = 0) .. 5e3){\n\t\t\t$i -++ ($j *= 2) < 0 && last;\n\t\t\t$_[ $i ] eq $_[ $i - $j ] or next;\n\t\t\t$m = $b if $m < ($b = $b[ $i - $j ])\n\t\t}\n\t$b[ $i ] =++ $m;\n\t$_ = $b[ $i ] if $_ < $b[ $i ]\n\t}\nprint"}, {"source_code": "$_ = <>, chomp;\n@_ = split $\\;\nfor $i (0 .. @_ - 1){\n\tundef $m;\n\tfor $j (0 .. 500){\n\t\t\t$j *= 2;\n\t\t\t$j += 1;\n\t\t\t$i - $j < 0 and last;\n\t\t\t$_[ $i ] eq $_[ $i - $j ] or next;\n\t\t\t$m |= 1 x length $b[ $i - $j ]\n\t\t}\n\t$b[ $i ] = $m . 1;\n\t$M |= 1 x length $b[ $i ];\n\t}\nprint length $M"}], "negative_code": [{"source_code": "chomp ($a = <>);\nmap $x |= $_, split 0, join $\\, map $_ = (1 + length) % 2, split $_, $a for a .. z;\nprint length $x"}, {"source_code": "$_ = <>, chomp;\n$_ = join $/, map {$j ++ % 2 ? $_ : scalar reverse} split /\\|/;\n$min = (sort {$a <=> $b} @_ = map $_ = ($a += (-1) ** ($i ++ % 2) * length) - (++ $k % 2 and length), split /\\n/ )[0];\ns/$/ q( ) x (- $min + shift @_) /emg;\nprint scalar reverse"}], "src_uid": "e95aee4975b391e750123b7ead3eb0d4"} {"nl": {"description": "Petya and Vasya are brothers. Today is a special day for them as their parents left them home alone and commissioned them to do n chores. Each chore is characterized by a single parameter \u2014 its complexity. The complexity of the i-th chore equals hi.As Petya is older, he wants to take the chores with complexity larger than some value x (hi\u2009>\u2009x) to leave to Vasya the chores with complexity less than or equal to x (hi\u2009\u2264\u2009x). The brothers have already decided that Petya will do exactly a chores and Vasya will do exactly b chores (a\u2009+\u2009b\u2009=\u2009n).In how many ways can they choose an integer x so that Petya got exactly a chores and Vasya got exactly b chores?", "input_spec": "The first input line contains three integers n,\u2009a and b (2\u2009\u2264\u2009n\u2009\u2264\u20092000; a,\u2009b\u2009\u2265\u20091; a\u2009+\u2009b\u2009=\u2009n) \u2014 the total number of chores, the number of Petya's chores and the number of Vasya's chores. The next line contains a sequence of integers h1,\u2009h2,\u2009...,\u2009hn (1\u2009\u2264\u2009hi\u2009\u2264\u2009109), hi is the complexity of the i-th chore. The numbers in the given sequence are not necessarily different. All numbers on the lines are separated by single spaces.", "output_spec": "Print the required number of ways to choose an integer value of x. If there are no such ways, print 0.", "sample_inputs": ["5 2 3\n6 2 3 100 1", "7 3 4\n1 1 9 1 1 1 1"], "sample_outputs": ["3", "0"], "notes": "NoteIn the first sample the possible values of x are 3, 4 or 5.In the second sample it is impossible to find such x, that Petya got 3 chores and Vasya got 4."}, "positive_code": [{"source_code": "($n, $a, $b) = split(' ', <>);\n@a = sort{$a <=> $b}split(' ', <>);\nprint $a[$b] - $a[$b - 1];"}], "negative_code": [{"source_code": "($n, $a, $b) = split(' ', <>);\n@a = sort{$a <=> $b}split(' ', <>);\nprint $a[$a + 1] - $a[$a];"}], "src_uid": "d3c8c1e32dcf4286bef19e9f2b79c8bd"} {"nl": {"description": "Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they are not under its influence.He has names of n people who possessed the diary in order. You need to tell, for each person, if he/she possessed the diary at some point before or not.Formally, for a name si in the i-th line, output \"YES\" (without quotes) if there exists an index j such that si\u2009=\u2009sj and j\u2009<\u2009i, otherwise, output \"NO\" (without quotes).", "input_spec": "First line of input contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100)\u00a0\u2014 the number of names in the list. Next n lines each contain a string si, consisting of lowercase English letters. The length of each string is between 1 and 100.", "output_spec": "Output n lines each containing either \"YES\" or \"NO\" (without quotes), depending on whether this string was already present in the stream or not. You can print each letter in any case (upper or lower).", "sample_inputs": ["6\ntom\nlucius\nginny\nharry\nginny\nharry", "3\na\na\na"], "sample_outputs": ["NO\nNO\nNO\nNO\nYES\nYES", "NO\nYES\nYES"], "notes": "NoteIn test case 1, for i\u2009=\u20095 there exists j\u2009=\u20093 such that si\u2009=\u2009sj and j\u2009<\u2009i, which means that answer for i\u2009=\u20095 is \"YES\"."}, "positive_code": [{"source_code": "\n\nmy $f = ;\nchomp ( $f );\n\nmy %h;\t\t\t\t\nmy $str;\nfor ( my $i = 0; $i < $f; $i += 1 ) {\n $str = ;\n chomp ( $f );\n if ( exists $h{ $str } ) {\n\tprint \"YES\\n\";}\n else {\n\t$h{ $str } = 1;\n\tprint \"NO\\n\"; }}\n\n\n\n"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\n\n<>;\n\nmy %hash;\n\nwhile (<>) {\n\tchomp;\n\tif (exists $hash{$_}) {\n\t\tprint \"YES\\n\";\n\t}\n\telse {\n\t\tprint \"NO\\n\";\n\t}\n\t$hash{$_} = 1;\n}\n"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\n\n<>;\n\nmy %hash;\n\nwhile (<>) {\n\tchomp;\n\tif (exists $hash{$_}) {\n\t\tprint \"YES\\n\";\n\t}\n\telse {\n\t\tprint \"NO\\n\";\n\t\t$hash{$_} = 1;\n\t}\n}\n"}], "negative_code": [], "src_uid": "7f934f96cbe3990945e5ebcf5c208043"} {"nl": {"description": "Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. In this table, Vanya chose n rectangles with sides that go along borders of squares (some rectangles probably occur multiple times). After that for each cell of the table he counted the number of rectangles it belongs to and wrote this number into it. Now he wants to find the sum of values in all cells of the table and as the table is too large, he asks you to help him find the result.", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of rectangles. Each of the following n lines contains four integers x1,\u2009y1,\u2009x2,\u2009y2 (1\u2009\u2264\u2009x1\u2009\u2264\u2009x2\u2009\u2264\u2009100, 1\u2009\u2264\u2009y1\u2009\u2264\u2009y2\u2009\u2264\u2009100), where x1 and y1 are the number of the column and row of the lower left cell and x2 and y2 are the number of the column and row of the upper right cell of a rectangle.", "output_spec": "In a single line print the sum of all values in the cells of the table.", "sample_inputs": ["2\n1 1 2 3\n2 2 3 3", "2\n1 1 3 3\n1 1 3 3"], "sample_outputs": ["10", "18"], "notes": "NoteNote to the first sample test:Values of the table in the first three rows and columns will be as follows:121121110So, the sum of values will be equal to 10.Note to the second sample test:Values of the table in the first three rows and columns will be as follows:222222222So, the sum of values will be equal to 18."}, "positive_code": [{"source_code": "($a, @b) = <>;\nchomp ($a, @b);\n$s = 0;\nfor(@b)\n{\n ($x1, $y1, $x2, $y2) = (split(/ /, $_));\n $s += ($x2 - $x1 + 1) * ($y2 - $y1 + 1);\n}\nprint $s;"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\nforeach (1 .. $n) {\n\t($x1, $y1, $x2, $y2) = split / /, <>;\n\t$ans += ($x2-$x1+1) * ($y2-$y1+1);\n}\nsay $ans;"}, {"source_code": "while($n = ){\n $ans = 0;\n for(1..$n){\n $line = ;\n chomp $line;\n ($x1, $y1, $x2, $y2) = split(' ', $line);\n $ans += ($x2 - $x1 + 1) * ($y2 - $y1 + 1);\n }\n print \"$ans\\n\";\n}\n"}, {"source_code": "$\\ = $/;\n<>;\nwhile(<>){\n\tchomp;\n\t($a, $b, $c, $d) = split;\n\t$sum += (abs($c-$a)+1) * (abs($d-$b)+1);\n}\nprint 0+$sum"}, {"source_code": "my $n = int<>;\nmy $ans = 0;\nmy ($x1, $y1, $x2, $y2);\nfor(1..$n) {\n ($x1, $y1, $x2, $y2) = split / /, <>;\n $ans += ($y2 - $y1 + 1) * ($x2 - $x1 + 1);\n}\nprint $ans;\n"}], "negative_code": [], "src_uid": "ca5c44093b1ab7c01970d83b5f49d0c6"} {"nl": {"description": "Define the score of some binary string $$$T$$$ as the absolute difference between the number of zeroes and ones in it. (for example, $$$T=$$$ 010001 contains $$$4$$$ zeroes and $$$2$$$ ones, so the score of $$$T$$$ is $$$|4-2| = 2$$$).Define the creepiness of some binary string $$$S$$$ as the maximum score among all of its prefixes (for example, the creepiness of $$$S=$$$ 01001 is equal to $$$2$$$ because the score of the prefix $$$S[1 \\ldots 4]$$$ is $$$2$$$ and the rest of the prefixes have a score of $$$2$$$ or less).Given two integers $$$a$$$ and $$$b$$$, construct a binary string consisting of $$$a$$$ zeroes and $$$b$$$ ones with the minimum possible creepiness.", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1\\le t\\le 1000)$$$ \u00a0\u2014 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 \\le a, b \\le 100$$$) \u00a0\u2014 the numbers of zeroes and ones correspondingly.", "output_spec": "For each test case, print a binary string consisting of $$$a$$$ zeroes and $$$b$$$ ones with the minimum possible creepiness. If there are multiple answers, print any of them.", "sample_inputs": ["5\n1 1\n1 2\n5 2\n4 5\n3 7"], "sample_outputs": ["10\n011\n0011000\n101010101\n0001111111"], "notes": "NoteIn the first test case, the score of $$$S[1 \\ldots 1]$$$ is $$$1$$$, and the score of $$$S[1 \\ldots 2]$$$ is $$$0$$$.In the second test case, the minimum possible creepiness is $$$1$$$ and one of the other answers is 101.In the third test case, the minimum possible creepiness is $$$3$$$ and one of the other answers is 0001100."}, "positive_code": [{"source_code": "for(1..<>){($a,$b)=split/\\s/,<>;$t=$a-$b;print\"01\"x($t>0?$b:$a).($t>0?\"0\"x$t:\"1\"x-$t),\" \"}"}, {"source_code": "for(1..<>){($a,$b)=split/\\s/,<>;print\"01\"x($a>$b?$b:$a).($a>$b?\"0\"x($a-$b):\"1\"x($b-$a)),\" \"}"}, {"source_code": "for(1..<>){($a,$b)=split/\\s/,<>;print\"01\"x($a>$b?$b:$a).($a>$b?\"0\"x($a-$b):\"1\"x($b-$a)),\"\\n\"}"}, {"source_code": "for(1..<>){($a,$b)=split/\\s/,<>;$t=$a-$b;print\"01\"x($t>0?$b:$a).($t>0?\"0\"x$t:\"1\"x-$t),\" \"}for(1..<>){($a,$b)=split/\\s/,<>;$t=$a-$b;print\"01\"x($t>0?$b:$a).($t>0?\"0\"x$t:\"1\"x-$t),\" \"}"}, {"source_code": "for(1..<>){($a,$b)=split/\\s/,<>;$t=$a-$b;print\"01\"x($t>0?$b:$a).($t>0?\"0\"x$t:\"1\"x-$t),\" \"}"}, {"source_code": "for(1..<>)\r\n{($a,$b)=split/\\s/,<>;\r\n\r\n$t=$a-$b;\r\nprint\"01\"x($t>0?$b:$a).($t>0?\"0\"x$t:\"1\"x-$t),\" \"}"}, {"source_code": "for(1..<>){($a,$b)=split/\\s/,<>;$t=$a-$b;print\"01\"x($t>0?$b:$a).($t>0?\"0\"x$t:\"1\"x-$t),\" \"}"}, {"source_code": "for(1..<>){($a,$b)=split/\\s/,<>;print\"01\"x($a>$b?$b:$a).($a>$b?\"0\"x($a-$b):\"1\"x($b-$a)),\" \"}"}], "negative_code": [], "src_uid": "7767e47571c7ea82ba4fb6a0bd0fbdc5"} {"nl": {"description": "In the snake exhibition, there are $$$n$$$ rooms (numbered $$$0$$$ to $$$n - 1$$$) arranged in a circle, with a snake in each room. The rooms are connected by $$$n$$$ conveyor belts, and the $$$i$$$-th conveyor belt connects the rooms $$$i$$$ and $$$(i+1) \\bmod n$$$. In the other words, rooms $$$0$$$ and $$$1$$$, $$$1$$$ and $$$2$$$, $$$\\ldots$$$, $$$n-2$$$ and $$$n-1$$$, $$$n-1$$$ and $$$0$$$ are connected with conveyor belts.The $$$i$$$-th conveyor belt is in one of three states: If it is clockwise, snakes can only go from room $$$i$$$ to $$$(i+1) \\bmod n$$$. If it is anticlockwise, snakes can only go from room $$$(i+1) \\bmod n$$$ to $$$i$$$. If it is off, snakes can travel in either direction. Above is an example with $$$4$$$ rooms, where belts $$$0$$$ and $$$3$$$ are off, $$$1$$$ is clockwise, and $$$2$$$ is anticlockwise.Each snake wants to leave its room and come back to it later. A room is returnable if the snake there can leave the room, and later come back to it using the conveyor belts. How many such returnable rooms are there?", "input_spec": "Each test contains 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 description contains a single integer $$$n$$$ ($$$2 \\le n \\le 300\\,000$$$): the number of rooms. The next line of each test case description contains a string $$$s$$$ of length $$$n$$$, consisting of only '<', '>' and '-'. If $$$s_{i} = $$$ '>', the $$$i$$$-th conveyor belt goes clockwise. If $$$s_{i} = $$$ '<', the $$$i$$$-th conveyor belt goes anticlockwise. If $$$s_{i} = $$$ '-', the $$$i$$$-th conveyor belt is off. It is guaranteed that the sum of $$$n$$$ among all test cases does not exceed $$$300\\,000$$$.", "output_spec": "For each test case, output the number of returnable rooms.", "sample_inputs": ["4\n4\n-><-\n5\n>>>>>\n3\n<--\n2\n<>"], "sample_outputs": ["3\n5\n3\n0"], "notes": "NoteIn the first test case, all rooms are returnable except room $$$2$$$. The snake in the room $$$2$$$ is trapped and cannot exit. This test case corresponds to the picture from the problem statement. In the second test case, all rooms are returnable by traveling on the series of clockwise belts."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nwhile($t-->0){\n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my $s = scalar();\n \n my %c = ('>'=>0, '<'=>0, '-'=>0);\n for(my $i=0;$i<$n;$i++){\n my $c1 = substr($s,$i,1);\n $c{$c1} ++;\n }\n \n my $u = 0;\n my $c1 = substr($s,0,1);\n for(my $i=0;$i<$n;$i++){\n my $c2 = substr($s,(1+$i) % $n,1);\n #next if( $c1 eq '-' or $c2 eq '-');\n if( $c1 eq '<' and $c2 eq '<' ){\n $u++ if( $c{'>'} > 0 );\n }\n if( $c1 eq '>' and $c2 eq '>' ){\n $u++ if( $c{'<'} > 0 );\n }\n if( $c1 eq '>' and $c2 eq '<' ){\n $u++;\n }\n if( $c1 eq '<' and $c2 eq '>' ){\n $u++;\n }\n $c1 = $c2;\n }\n my $r = $n - $u;\n print \"$r\\n\";\n \n}\n\n\n"}], "negative_code": [], "src_uid": "f82685f41f4ba1146fea8e1eb0c260dc"} {"nl": {"description": "One day, as Sherlock Holmes was tracking down one very important criminal, he found a wonderful painting on the wall. This wall could be represented as a plane. The painting had several concentric circles that divided the wall into several parts. Some parts were painted red and all the other were painted blue. Besides, any two neighboring parts were painted different colors, that is, the red and the blue color were alternating, i. e. followed one after the other. The outer area of the wall (the area that lied outside all circles) was painted blue. Help Sherlock Holmes determine the total area of red parts of the wall.Let us remind you that two circles are called concentric if their centers coincide. Several circles are called concentric if any two of them are concentric.", "input_spec": "The first line contains the single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100). The second line contains n space-separated integers ri (1\u2009\u2264\u2009ri\u2009\u2264\u20091000) \u2014 the circles' radii. It is guaranteed that all circles are different.", "output_spec": "Print the single real number \u2014 total area of the part of the wall that is painted red. The answer is accepted if absolute or relative error doesn't exceed 10\u2009-\u20094.", "sample_inputs": ["1\n1", "3\n1 4 2"], "sample_outputs": ["3.1415926536", "40.8407044967"], "notes": "NoteIn the first sample the picture is just one circle of radius 1. Inner part of the circle is painted red. The area of the red part equals \u03c0\u2009\u00d7\u200912\u2009=\u2009\u03c0.In the second sample there are three circles of radii 1, 4 and 2. Outside part of the second circle is painted blue. Part between the second and the third circles is painted red. Part between the first and the third is painted blue. And, finally, the inner part of the first circle is painted red. Overall there are two red parts: the ring between the second and the third circles and the inner part of the first circle. Total area of the red parts is equal (\u03c0\u2009\u00d7\u200942\u2009-\u2009\u03c0\u2009\u00d7\u200922)\u2009+\u2009\u03c0\u2009\u00d7\u200912\u2009=\u2009\u03c0\u2009\u00d7\u200912\u2009+\u2009\u03c0\u2009=\u200913\u03c0"}, "positive_code": [{"source_code": "\n$n=;\n$rads=;\n@radius=sort {$b <=> $a} split(\" \",$rads);\n$square=0;\n$\\=\"\\n\";\n$controller=1;\n\n\nforeach $i (@radius){\n if (($controller++)%2==0){\n $square-=3.141592653589793*$i*$i;\n #print $i,\"chet\";\n }else{\n $square+=3.141592653589793*$i*$i;\n #print $i,\"nechet\";\n }\n}\n\nprint $square;\n"}, {"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nmy $n=<>;\nmy @r=split(' ',<>);\nmy $total=0;\nmy $count=0;\nmy $now;\nmy $pi=3.1415926535;\nif($n%2==1){\n\t$now=1;\n}else{\n\t$now=-1;\n}\nwhile($count<$n-1){\n\tif($r[$count]>$r[$count+1]){\n\t\tmy $temp=$r[$count];\n\t\t$r[$count]=$r[$count+1];\n\t\t$r[$count+1]=$temp;\n\t\t$count=-1;\n\t}\n\t$count++;\n}\nforeach my $count(0..$#r){\n\t$total+=$now*$pi*$r[$count]*$r[$count];\n\t$now*=-1;\n}\nprint $total;"}], "negative_code": [{"source_code": "\n$n=;\n$rads=;\n@radius=sort {$a <=> $b} split(\" \",$rads);\n$square=0;\n$\\=\"\\n\";\n$controller=1;\n\n\nforeach $i (@radius){\n if (($controller++)%2==0){\n $square-=3.141592653589793*$i*$i;\n #print $i,\"chet\";\n }else{\n $square+=3.141592653589793*$i*$i;\n #print $i,\"nechet\";\n }\n}\n\nprint $square;"}, {"source_code": "sub sw($$){\n\tmy $swap=$_[0];\n\t$_[0]=$_[1];\n\t$_[1]=$swap;\n}\n\n\n$n=;\n$rads=;\n@radius=split(\" \",$rads);\n$square=0;\n$\\=\"\\n\";\n$controller=1;\n\n\n\n\nforeach $i (0 .. $n){\n\tforeach $j (0 .. ($n-1)){\n\t\tif ($radius[$j]<$radius[$j+1]){\n\t\t\tsw($radius[$j],$radius[$j+1]);\n\t\t}\n\t}\n}\n\nforeach $i (@radius){\n\tif (($controller++)%2==0){\n\t\t$square-=3.141592653589793*$i*$i;\n\t\tprint $i,\"chet\";\n\t}else{\n\t\t$square+=3.141592653589793*$i*$i;\n\t\tprint $i,\"nechet\";\n\t}\n}\n\nprint $square;"}], "src_uid": "48b9c68380d3bd64bbc69d921a098641"} {"nl": {"description": "Colossal!\u00a0\u2014 exclaimed Hawk-nose.\u00a0\u2014 A programmer! That's exactly what we are looking for.Arkadi and Boris Strugatsky. Monday starts on SaturdayReading the book \"Equations of Mathematical Magic\" Roman Oira-Oira and Cristobal Junta found an interesting equation: $$$a - (a \\oplus x) - x = 0$$$ for some given $$$a$$$, where $$$\\oplus$$$ stands for a bitwise exclusive or (XOR) of two integers (this operation is denoted as ^ or xor in many modern programming languages). Oira-Oira quickly found some $$$x$$$, which is the solution of the equation, but Cristobal Junta decided that Oira-Oira's result is not interesting enough, so he asked his colleague how many non-negative solutions of this equation exist. This task turned out to be too difficult for Oira-Oira, so he asks you to help.", "input_spec": "Each test contains several possible values of $$$a$$$ and your task is to find the number of equation's solution for each of them. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$)\u00a0\u2014 the number of these values. The following $$$t$$$ lines contain the values of parameter $$$a$$$, each value is an integer from $$$0$$$ to $$$2^{30} - 1$$$ inclusive.", "output_spec": "For each value of $$$a$$$ print exactly one integer\u00a0\u2014 the number of non-negative solutions of the equation for the given value of the parameter. Print answers in the same order as values of $$$a$$$ appear in the input. One can show that the number of solutions is always finite.", "sample_inputs": ["3\n0\n2\n1073741823"], "sample_outputs": ["1\n2\n1073741824"], "notes": "NoteLet's define the bitwise exclusive OR (XOR) operation. Given two integers $$$x$$$ and $$$y$$$, consider their binary representations (possibly with leading zeroes): $$$x_k \\dots x_2 x_1 x_0$$$ and $$$y_k \\dots y_2 y_1 y_0$$$. Here, $$$x_i$$$ is the $$$i$$$-th bit of the number $$$x$$$ and $$$y_i$$$ is the $$$i$$$-th bit of the number $$$y$$$. Let $$$r = x \\oplus y$$$ be the result of the XOR operation of $$$x$$$ and $$$y$$$. Then $$$r$$$ is defined as $$$r_k \\dots r_2 r_1 r_0$$$ where:$$$$$$ r_i = \\left\\{ \\begin{aligned} 1, ~ \\text{if} ~ x_i \\ne y_i \\\\ 0, ~ \\text{if} ~ x_i = y_i \\end{aligned} \\right. $$$$$$For the first value of the parameter, only $$$x = 0$$$ is a solution of the equation.For the second value of the parameter, solutions are $$$x = 0$$$ and $$$x = 2$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\n$t=;\nwhile ($t>0) {\n\t$x=;\n\t$w=1;\n\twhile ($x>0) {\n\t\tif ($x%2==1) {\n\t\t\t$w*=2;\n\t\t}\n\t\t$x/=2;\n\t}\n\tprint $w,\"\\n\";\n\t$t--;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\t$debug and print '-' x 10;\n\t\n\tmy $B = sprintf \"%b\", $_;\n\t\n\t$debug and do {\n\t\n\t\tprint \" $_ -> $B\";\n\t\t\n\t\t$_ > 100 and next;\n\t\t\n\t\tmy $c = 0;\n\t\t\n\t\tfor my $i ( 0 .. 2 ** ( length $B ) - 1 ){\n\t\t\t$debug and print sprintf \" %b\", $i;\n\t\t\t\n\t\t\t$debug and print join ' ', ':', $_, $i, $_ ^ $i;\n\t\t\tmy $form = $_ - $i - ( $_ ^ $i );\n\t\t\t$debug and print ' ', $form;\n\t\t\t\n\t\t\t$c += ! $form;\n\t\t\t}\n\t\t\n\t\tprint \"$_ : $c\";\n\t\t\n\t\t};\n\t\n\tprint 2 ** ( () = $B =~ /1/g );\n\t}"}, {"source_code": "<>;\n\nprint 2 ** ( () = ( sprintf \"%b\", $_ ) =~ /1/g ) . $/ for <>"}], "negative_code": [{"source_code": "<>;\n\nprint 2 ** ( () = ( sprintf \"%b\", $_ ) =~ /1/g ) for <>"}], "src_uid": "a896ba035e56dc707f8123b1e2f2b11c"} {"nl": {"description": "You are given a list of $$$n$$$ integers. You can perform the following operation: you choose an element $$$x$$$ from the list, erase $$$x$$$ from the list, and subtract the value of $$$x$$$ from all the remaining elements. Thus, in one operation, the length of the list is decreased by exactly $$$1$$$.Given an integer $$$k$$$ ($$$k>0$$$), find if there is some sequence of $$$n-1$$$ operations such that, after applying the operations, the only remaining element of the list is equal to $$$k$$$.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) \u2014 the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \\leq n \\leq 2\\cdot 10^5$$$, $$$1 \\leq k \\leq 10^9$$$), the number of integers in the list, and the target value, respectively. The second line of each test case contains the $$$n$$$ integers of the list $$$a_1, a_2, \\ldots, a_n$$$ ($$$-10^9 \\leq a_i \\leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases is not greater that $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print YES if you can achieve $$$k$$$ with a sequence of $$$n-1$$$ operations. 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\n\n4 5\n\n4 2 2 7\n\n5 4\n\n1 9 1 3 4\n\n2 17\n\n17 0\n\n2 17\n\n18 18"], "sample_outputs": ["YES\nNO\nYES\nNO"], "notes": "NoteIn the first example we have the list $$$\\{4, 2, 2, 7\\}$$$, and we have the target $$$k = 5$$$. One way to achieve it is the following: first we choose the third element, obtaining the list $$$\\{2, 0, 5\\}$$$. Next we choose the first element, obtaining the list $$$\\{-2, 3\\}$$$. Finally, we choose the first element, obtaining the list $$$\\{5\\}$$$. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t@_ = split ' ', <>;\n\t\n\t@_ = sort { $a <=> $b } @_;\n\t\n\tmy $i = 0;\n\tmy $j = 0;\n\t\n\twhile( not ( $_[ $j ] - $_[ $i ] == $k ) ){\n\t\tif( $_[ $j ] - $_[ $i ] < $k ){\n\t\t\t$j ++;\n\t\t\t}\n\t\telse{\n\t\t\t$i ++;\n\t\t\t}\n\t\tlast if $j == @_;\n\t\t}\n\t\n\tif( $j != @_ and $_[ $j ] - $_[ $i ] == $k ){\n\t\tprint \"YES\";\n\t\t}\n\telse{\n\t\tprint \"NO\";\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "aae82b2687786818996e4e94c5505d8e"} {"nl": {"description": "Polycarp is wondering about buying a new computer, which costs $$$c$$$ tugriks. To do this, he wants to get a job as a programmer in a big company.There are $$$n$$$ positions in Polycarp's company, numbered starting from one. An employee in position $$$i$$$ earns $$$a[i]$$$ tugriks every day. The higher the position number, the more tugriks the employee receives. Initially, Polycarp gets a position with the number $$$1$$$ and has $$$0$$$ tugriks.Each day Polycarp can do one of two things: If Polycarp is in the position of $$$x$$$, then he can earn $$$a[x]$$$ tugriks. If Polycarp is in the position of $$$x$$$ ($$$x < n$$$) and has at least $$$b[x]$$$ tugriks, then he can spend $$$b[x]$$$ tugriks on an online course and move to the position $$$x+1$$$. For example, if $$$n=4$$$, $$$c=15$$$, $$$a=[1, 3, 10, 11]$$$, $$$b=[1, 2, 7]$$$, then Polycarp can act like this: On the first day, Polycarp is in the $$$1$$$-st position and earns $$$1$$$ tugrik. Now he has $$$1$$$ tugrik; On the second day, Polycarp is in the $$$1$$$-st position and move to the $$$2$$$-nd position. Now he has $$$0$$$ tugriks; On the third day, Polycarp is in the $$$2$$$-nd position and earns $$$3$$$ tugriks. Now he has $$$3$$$ tugriks; On the fourth day, Polycarp is in the $$$2$$$-nd position and is transferred to the $$$3$$$-rd position. Now he has $$$1$$$ tugriks; On the fifth day, Polycarp is in the $$$3$$$-rd position and earns $$$10$$$ tugriks. Now he has $$$11$$$ tugriks; On the sixth day, Polycarp is in the $$$3$$$-rd position and earns $$$10$$$ tugriks. Now he has $$$21$$$ tugriks; Six days later, Polycarp can buy himself a new computer. Find the minimum number of days after which Polycarp will be able to buy himself a new computer.", "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 two integers $$$n$$$ and $$$c$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le c \\le 10^9$$$)\u00a0\u2014 the number of positions in the company and the cost of a new computer. The second line of each test case contains $$$n$$$ integers $$$a_1 \\le a_2 \\le \\ldots \\le a_n$$$ ($$$1 \\le a_i \\le 10^9$$$). The third line of each test case contains $$$n - 1$$$ integer $$$b_1, b_2, \\ldots, b_{n-1}$$$ ($$$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 the minimum number of days after which Polycarp will be able to buy a new computer.", "sample_inputs": ["3\n4 15\n1 3 10 11\n1 2 7\n4 100\n1 5 10 50\n3 14 12\n2 1000000000\n1 1\n1"], "sample_outputs": ["6\n13\n1000000000"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\n\r\nmy ($tcase) = map { $_ - 0 } split(/\\s+/o,);\r\nwhile( $tcase -- > 0 ){\r\n my ($n,$c) = map { $_ - 0 } split(/\\s+/o,);\r\n my @A = map { $_ - 0 } split(/\\s+/o,);\r\n my @B = map { $_ - 0 } split(/\\s+/o,);\r\n \r\n my $mn = 10000000000;\r\n my $bd = 0;\r\n my $bt = 0;\r\n for(my $i=0;$i<$n;$i++){\r\n my $l_t = $c - $bt;\r\n $l_t = 0 if $l_t <= 0;\r\n my $tday = $bd + int( ($l_t +($A[$i]-1) ) / $A[$i] );\r\n $mn = $tday if $tday < $mn;\r\n last if $i == $n - 1;\r\n $l_t = $B[$i] - $bt;\r\n $l_t = 0 if $l_t <= 0;\r\n $tday = int( ($l_t +($A[$i]-1) ) / $A[$i] );\r\n $bd += 1+$tday;\r\n $bt = $bt + $tday * $A[$i] - $B[$i];\r\n }\r\n print \"$mn\\n\";\r\n \r\n}\r\n\r\n"}], "negative_code": [], "src_uid": "1c154d858ded80a1c36febd6178fc1fe"} {"nl": {"description": "Ivan plays an old action game called Heretic. He's stuck on one of the final levels of this game, so he needs some help with killing the monsters.The main part of the level is a large corridor (so large and narrow that it can be represented as an infinite coordinate line). The corridor is divided into two parts; let's assume that the point $$$x = 0$$$ is where these parts meet.The right part of the corridor is filled with $$$n$$$ monsters \u2014 for each monster, its initial coordinate $$$x_i$$$ is given (and since all monsters are in the right part, every $$$x_i$$$ is positive).The left part of the corridor is filled with crusher traps. If some monster enters the left part of the corridor or the origin (so, its current coordinate becomes less than or equal to $$$0$$$), it gets instantly killed by a trap.The main weapon Ivan uses to kill the monsters is the Phoenix Rod. It can launch a missile that explodes upon impact, obliterating every monster caught in the explosion and throwing all other monsters away from the epicenter. Formally, suppose that Ivan launches a missile so that it explodes in the point $$$c$$$. Then every monster is either killed by explosion or pushed away. Let some monster's current coordinate be $$$y$$$, then: if $$$c = y$$$, then the monster is killed; if $$$y < c$$$, then the monster is pushed $$$r$$$ units to the left, so its current coordinate becomes $$$y - r$$$; if $$$y > c$$$, then the monster is pushed $$$r$$$ units to the right, so its current coordinate becomes $$$y + r$$$. Ivan is going to kill the monsters as follows: choose some integer point $$$d$$$ and launch a missile into that point, then wait until it explodes and all the monsters which are pushed to the left part of the corridor are killed by crusher traps, then, if at least one monster is still alive, choose another integer point (probably the one that was already used) and launch a missile there, and so on.What is the minimum number of missiles Ivan has to launch in order to kill all of the monsters? You may assume that every time Ivan fires the Phoenix Rod, he chooses the impact point optimally.You have to answer $$$q$$$ independent queries.", "input_spec": "The first line contains one integer $$$q$$$ ($$$1 \\le q \\le 10^5$$$) \u2014 the number of queries. The first line of each query contains two integers $$$n$$$ and $$$r$$$ ($$$1 \\le n, r \\le 10^5$$$)\u00a0\u2014 the number of enemies and the distance that the enemies are thrown away from the epicenter of the explosion. The second line of each query contains $$$n$$$ integers $$$x_i$$$ ($$$1 \\le x_i \\le 10^5$$$)\u00a0\u2014 the initial positions of the monsters. It is guaranteed that sum of all $$$n$$$ over all queries does not exceed $$$10^5$$$.", "output_spec": "For each query print one integer\u00a0\u2014 the minimum number of shots from the Phoenix Rod required to kill all monsters.", "sample_inputs": ["2\n3 2\n1 3 5\n4 1\n5 2 3 5"], "sample_outputs": ["2\n2"], "notes": "NoteIn the first test case, Ivan acts as follows: choose the point $$$3$$$, the first monster dies from a crusher trap at the point $$$-1$$$, the second monster dies from the explosion, the third monster is pushed to the point $$$7$$$; choose the point $$$7$$$, the third monster dies from the explosion. In the second test case, Ivan acts as follows: choose the point $$$5$$$, the first and fourth monsters die from the explosion, the second monster is pushed to the point $$$1$$$, the third monster is pushed to the point $$$2$$$; choose the point $$$2$$$, the first monster dies from a crusher trap at the point $$$0$$$, the second monster dies from the explosion. "}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n\nmy $q = $line;\n\nfor (1..$q) {\n chomp ($line = );\n my ($n, $r) = split q{ }, $line;\n chomp ($line = );\n my @monsters = split q{ }, $line;\n\n my $set = {};\n for (@monsters) {\n $set->{$_} = 1 if !defined $set->{$_};\n }\n\n @monsters = sort { $a <=> $b } keys %$set;\n\n my $answer = 0;\n my $i = @monsters-1;\n while ( $i >= 0 ) {\n if ( $monsters[$i] > $answer * $r ) {\n $answer++;\n }\n $i--;\n }\n\n say $answer;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $r ) = split;\n\t@_ = sort { $a <=> $b } split ' ', <>;\n\t\n\tmy $cnt = 0;\n\t\n\twhile( @_ ){\n\t\tmy $last = pop @_;\n\t\tif( @_ and $_[ @_ - 1 ] == $last ){\n\t\t\tnext;\n\t\t\t}\n\t\tif( $last - $cnt * $r > 0 ){\n\t\t\t$cnt ++;\n\t\t\t}\n\t\telse{\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tprint $cnt;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n\nmy $q = $line;\n\nfor (1..$q) {\n chomp ($line = );\n my ($n, $r) = split q{ }, $line;\n chomp ($line = );\n my @monsters = split q{ }, $line;\n\n my $set = {};\n for (@monsters) {\n $set->{$_} = 1 if !defined $set->{$_};\n }\n\n @monsters = sort keys %$set;\n\n my $answer = 0;\n while ( @monsters ) {\n my $last = $monsters[-1];\n while ( @monsters && $monsters[-1] == $last ) {\n pop @monsters;\n }\n @monsters = grep { $_ > 0 } map { $_-1 } @monsters;\n $answer++;\n }\n say $answer;\n}\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n\nmy $q = $line;\n\nfor (1..$q) {\n chomp ($line = );\n my ($n, $r) = split q{ }, $line;\n chomp ($line = );\n my @monsters = split q{ }, $line;\n\n my $set = {};\n for (@monsters) {\n $set->{$_} = 1 if !defined $set->{$_};\n }\n\n @monsters = sort keys %$set;\n\n my $answer = 0;\n while ( @monsters ) {\n my $last = $monsters[-1];\n while ( @monsters && $monsters[-1] == $last ) {\n pop @monsters;\n }\n @monsters = grep { $_ > 0 } map { $_-$r } @monsters;\n $answer++;\n }\n say $answer;\n}\n"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n\nmy $q = $line;\n\nfor (1..$q) {\n chomp ($line = );\n my ($n, $r) = split q{ }, $line;\n $r++;\n chomp ($line = );\n my @monsters = split q{ }, $line;\n\n my $set = {};\n for (@monsters) {\n $set->{$_} = 1 if !defined $set->{$_};\n }\n\n @monsters = sort { $a <=> $b } keys %$set;\n\n my $answer = 0;\n my $i = 0;\n while ( $i < @monsters ) {\n if ( $r * $answer <= $monsters[$i] ) {\n pop @monsters;\n $answer++;\n }\n $i++;\n }\n say $answer;\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $r ) = split;\n\t@_ = split ' ', <>;\n\t\n\tmy $cnt = 0;\n\t\n\twhile( @_ ){\n\t\tmy $last = pop @_;\n\t\tif( $last - $cnt * $r > 0 ){\n\t\t\t$cnt ++;\n\t\t\t}\n\t\telse{\n\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\n\tprint $cnt;\n\t}"}], "src_uid": "60b6c6f047051eeabfe4e3a9e045c6b0"} {"nl": {"description": "Manao's friends often send him new songs. He never listens to them right away. Instead, he compiles them into a playlist. When he feels that his mind is open to new music, he opens the playlist and starts to listen to the songs.Of course, there are some songs that Manao doesn't particuarly enjoy. To get more pleasure from the received songs, he invented the following procedure of listening to the playlist: If after listening to some song Manao realizes that he liked it, then he remembers it and starts to listen to the next unlistened song. If after listening to some song Manao realizes that he did not like it, he listens to all the songs he liked up to this point and then begins to listen to the next unlistened song. For example, if Manao has four songs in the playlist, A, B, C, D (in the corresponding order) and he is going to like songs A and C in the end, then the order of listening is the following: Manao listens to A, he likes it, he remembers it. Manao listens to B, he does not like it, so he listens to A, again. Manao listens to C, he likes the song and he remembers it, too. Manao listens to D, but does not enjoy it and re-listens to songs A and C. That is, in the end Manao listens to song A three times, to song C twice and songs B and D once. Note that if Manao once liked a song, he will never dislike it on a subsequent listening.Manao has received n songs: the i-th of them is li seconds long and Manao may like it with a probability of pi percents. The songs could get on Manao's playlist in any order, so Manao wants to know the maximum expected value of the number of seconds after which the listening process will be over, for all possible permutations of the songs in the playlist.", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u200950000). The i-th of the following n lines contains two integers, separated by a single space \u2014 li and pi (15\u2009\u2264\u2009li\u2009\u2264\u20091000, 0\u2009\u2264\u2009pi\u2009\u2264\u2009100) \u2014 the length of the i-th song in seconds and the probability that Manao will like the song, in percents.", "output_spec": "In a single line print a single real number \u2014 the maximum expected listening time over all permutations of songs. The answer will be considered valid if the absolute or relative error does not exceed 10\u2009-\u20099.", "sample_inputs": ["3\n150 20\n150 50\n100 50", "4\n300 0\n300 50\n240 50\n360 80"], "sample_outputs": ["537.500000000", "2121.000000000"], "notes": "NoteConsider the first test case. If Manao listens to the songs in the order in which they were originally compiled, the mathematical expectation will be equal to 467.5 seconds. The maximum expected value is obtained by putting the first song at the end of the playlist.Consider the second test case. The song which is 360 seconds long should be listened to first. The song 300 seconds long which Manao will dislike for sure should be put in the end."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nuse List::Util qw(sum);\n\nchomp(my $n = <>);\nmy @songs = ();\nfor (1 .. $n) {\n chomp($_ = <>);\n push @songs, [split];\n}\n@songs = sort {\n (100 - $$a[1]) * $$b[0] * $$b[1]\n <=>\n (100 - $$b[1]) * $$a[0] * $$a[1]\n} @songs;\n\nmy $res = sum map { $$_[0] } @songs;\nmy $sum = 0;\nfor (@songs) {\n my ($l, $p) = @$_;\n $p /= 100.0;\n $res += (1 - $p) * $sum;\n $sum += $l * $p;\n}\nprint \"$res\\n\";\n"}], "negative_code": [], "src_uid": "295c768a404d11a4ac480aaaf653c45c"} {"nl": {"description": "Theofanis has a riddle for you and if you manage to solve it, he will give you a Cypriot snack halloumi for free (Cypriot cheese).You are given an integer $$$n$$$. You need to find two integers $$$l$$$ and $$$r$$$ such that $$$-10^{18} \\le l < r \\le 10^{18}$$$ and $$$l + (l + 1) + \\ldots + (r - 1) + r = n$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of test cases. The first and only line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^{18}$$$).", "output_spec": "For each test case, print the two integers $$$l$$$ and $$$r$$$ such that $$$-10^{18} \\le l < r \\le 10^{18}$$$ and $$$l + (l + 1) + \\ldots + (r - 1) + r = n$$$. It can be proven that an answer always exists. If there are multiple answers, print any.", "sample_inputs": ["7\n1\n2\n3\n6\n100\n25\n3000000000000"], "sample_outputs": ["0 1\n-1 2 \n1 2 \n1 3 \n18 22\n-2 7\n999999999999 1000000000001"], "notes": "NoteIn the first test case, $$$0 + 1 = 1$$$.In the second test case, $$$(-1) + 0 + 1 + 2 = 2$$$.In the fourth test case, $$$1 + 2 + 3 = 6$$$.In the fifth test case, $$$18 + 19 + 20 + 21 + 22 = 100$$$.In the sixth test case, $$$(-2) + (-1) + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 = 25$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tprint - $_ + 1, ' ', $_;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tprint '-', $_ - 1, ' ', $_;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tprint -1, ' ', ++ $_;\n\t}"}], "src_uid": "a4628208668e9d838cd019e9dc03e470"} {"nl": {"description": "A Pythagorean triple is a triple of integer numbers $$$(a, b, c)$$$ such that it is possible to form a right triangle with the lengths of the first cathetus, the second cathetus and the hypotenuse equal to $$$a$$$, $$$b$$$ and $$$c$$$, respectively. An example of the Pythagorean triple is $$$(3, 4, 5)$$$.Vasya studies the properties of right triangles, and he uses a formula that determines if some triple of integers is Pythagorean. Unfortunately, he has forgotten the exact formula; he remembers only that the formula was some equation with squares. So, he came up with the following formula: $$$c = a^2 - b$$$.Obviously, this is not the right formula to check if a triple of numbers is Pythagorean. But, to Vasya's surprise, it actually worked on the triple $$$(3, 4, 5)$$$: $$$5 = 3^2 - 4$$$, so, according to Vasya's formula, it is a Pythagorean triple.When Vasya found the right formula (and understood that his formula is wrong), he wondered: how many are there triples of integers $$$(a, b, c)$$$ with $$$1 \\le a \\le b \\le c \\le n$$$ such that they are Pythagorean both according to his formula and the real definition? He asked you to count these triples.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) \u2014 the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$).", "output_spec": "For each test case, print one integer \u2014 the number of triples of integers $$$(a, b, c)$$$ with $$$1 \\le a \\le b \\le c \\le n$$$ such that they are Pythagorean according both to the real definition and to the formula Vasya came up with.", "sample_inputs": ["3\n3\n6\n9"], "sample_outputs": ["0\n1\n1"], "notes": "NoteThe only Pythagorean triple satisfying $$$c = a^2 - b$$$ with $$$1 \\le a \\le b \\le c \\le 9$$$ is $$$(3, 4, 5)$$$; that's why the answer for $$$n = 3$$$ is $$$0$$$, and the answer for $$$n = 6$$$ (and for $$$n = 9$$$) is $$$1$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy $A = sqrt( ( $_ - 1 << 1 ) + 1 );\n\t\n\t$debug and print join ',', $_, int $A, ( int $A ) - 1 >> 1, $A;\n\t\n\tprint +( int $A ) - 1 >> 1;\n\t}"}], "negative_code": [], "src_uid": "31064ad58b7802c32b3c51137057b6f5"} {"nl": {"description": "Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1\u2009\u2264\u2009i\u2009\u2264\u2009n) he makes ai money. Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence ai. Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.Help Kefa cope with this task!", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105). The second line contains n integers a1,\u2009\u2009a2,\u2009\u2009...,\u2009\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109).", "output_spec": "Print a single integer \u2014 the length of the maximum non-decreasing subsegment of sequence a.", "sample_inputs": ["6\n2 2 1 3 4 1", "3\n2 2 9"], "sample_outputs": ["3", "3"], "notes": "NoteIn the first test the maximum non-decreasing subsegment is the numbers from the third to the fifth one.In the second test the maximum non-decreasing subsegment is the numbers from the first to the third one."}, "positive_code": [{"source_code": "$_ = (<>, <>);\nmap { $_ >= $i ? ( $k ++, $k > $m and $m = $k ) : ( $k = 1 ), $i = $_ } split;\nprint $m"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\n\n<>;\nmy @a = split ' ', <>;\nmy $len = 1;\nmy $maxlen = 1;\nfor my $i (1..$#a) {\n if ($a[$i - 1] <= $a[$i]) {\n $len++;\n $maxlen = $len if $len > $maxlen;\n } else {\n $len = 1;\n }\n}\nprint \"$maxlen\\n\";\n"}, {"source_code": "<>;\nprint map {\n $m = 0;\n $i = 0;\n $p = 0;\n map {\n $_ >= $p ? $i++ : do { $m < $i and $m = $i; $i = 1 };\n $p = $_\n } split;\n $m < $i and $m = $i;\n $m\n} $a = <>;\n"}, {"source_code": "<>;\nprint map {\n $m = 0;\n $i = 0;\n $p = 0;\n map {\n $_ >= $p ? do { $i++, $i > $m and $m = $i } : do { $i = 1 };\n $p = $_\n } split;\n $m\n} $a = <>;\n"}, {"source_code": "import strict;\nimport warnings;\n\n$_ = (<>, <>);\nmap {$me = $_; if($me >= $x){$k ++;}else{$k = 1;} if($k > $s) {$s = $k;} $x = $me; } split;\n\nprint \"$s\\n\";"}, {"source_code": "#!/bin/perl\n\n$n = ;\n$s = ;\n\nchomp $s;\n@ciong = split / +/, $s;\n\n$pop = 0;\n$maks = 0;\n$t = 0;\n\nfor($i=0;$i<$n;$i++)\n{\n\n if(@ciong[$i] >= $pop)\n {\n\n $t++;\n\n if( $t > $maks )\n {\n\n $maks = $t;\n\n }\n\n }\n else\n {\n\n $t = 1;\n\n }\n\n $pop = @ciong[$i];\n\n}\n\nprint $maks;\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @all = split \" \", <>;\n\n$n = 2_000_000_000;\nmy ($len, $mx) = (0, 0);\nfor my $i (@all){\n\tif ($i >= $n){\n\t\t$n = $i;\n\t\t++$len;\n\t}\n\telse {\n\t\t$len = 1;\n\t\t$n = $i;\n\t}\n\t$mx = $len if $len > $mx;\n}\nprint \"$mx\\n\";\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings FATAL => 'all';\nuse v5.20;\n\nmy $n = <>;\nmy @numbers = split(' ', <>);\n\nmy $result = 1;\nmy $local_result = 1;\n\nfor (my $i = 1; $i < @numbers; $i++)\n{\n if ($numbers[$i] >= $numbers[$i-1])\n {\n $local_result++;\n }\n else\n {\n $result = $local_result if $local_result > $result;\n $local_result = 1\n }\n}\n$result = $local_result if $local_result > $result;\n\nsay $result;"}, {"source_code": "$_ = (<>, <>);\nmap { $_ >= $i ? ( $k ++, $k > $m and $m = $k ) : ( $k = 1 ), $i = $_ } split;\nprint $m"}, {"source_code": "my $n = <>;\nmy @array = split ' ', <>;\nmy $last = shift @array;\nmy $c = 1;\nmy $max_c = 1;\nwhile (my $a = shift @array) {\n if ($a < $last) {\n $max_c = $c if ($max_c < $c);\n $c = 1;\n }\n else {\n ++$c; \n }\n $last = $a;\n}\n$max_c = $c if ($max_c < $c);\nprint $max_c;"}], "negative_code": [{"source_code": "<>;\nprint map {\n $m = 0;\n $i = 0;\n $p = 0;\n map {\n $_ >= $p ? do { $p = $_; $i++ } : do { $m < $i and $m = $i; $i = 1 }\n } split;\n $m < $i and $m = $i;\n $m\n} $a = <>;\n"}, {"source_code": "print map {\n $m = 0;\n $i = 0;\n $p = 0;\n map {\n $_ >= $p ? do { $p = $_; $i++ } : do { $m < $i and $m = $i; $i = 1 }\n } split;\n $m < $i and $m = $i;\n $m\n} $a = <>;\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @all = split \" \", <>;\n\n$n = 2_000_000_000;\nmy ($len, $mx) = (0, 0);\nfor my $i (@all){\n\tif ($i >= $n){\n\t\t$n = $i;\n\t\t++$len;\n\t\t$mx = $len if $len > $mx;\n\t}\n\telse {\n\t\t$len = 1;\n\t\t$n = $i;\n\t}\n}\nprint \"$mx\\n\";\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nmy @all = split \" \", <>;\n\n$n = 2_000_000_000;\nmy ($len, $mx) = (0, 0);\nfor my $i (@all){\n\tif ($i >= $n){\n\t\t$n = $i;\n\t\t++$len;\n\t\t$mx = $len if $len > $mx;\n\t}\n\telse {\n\t\t$mx = $len if $len > $mx;\n\t\t$len = 1;\n\t\t$n = $i;\n\t}\n}\nprint \"$mx\\n\";\n"}, {"source_code": "$_ = (<>, <>);\nmap { $_ >= $i ? ( $i = $_, $k++, $k > $m and $m = $k ) : ( $k = 0, $i = 0 ) } split;\nprint $m"}, {"source_code": "$_ = (<>, <>);\nmap { $_ > $i ? ( $k++, $k > $m and $m = $k ) : ( $k = 0) } split;\nprint $m"}, {"source_code": "$_ = (<>, <>);\nmap { $_ > $i ? ( $i = $_, $k++, $k > $m and $m = $k ) : ( $k = 0, $i = 0 ) } split;\nprint $m"}], "src_uid": "1312b680d43febdc7898ffb0753a9950"} {"nl": {"description": "The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times. When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most \u00ablucky\u00bb for him distribution of price tags) and the largest total price (in case of the most \u00abunlucky\u00bb for him distribution of price tags).", "input_spec": "The first line of the input contains two integer number n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100) \u2014 the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.", "output_spec": "Print two numbers a and b (a\u2009\u2264\u2009b) \u2014 the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.", "sample_inputs": ["5 3\n4 2 1 10 5\napple\norange\nmango", "6 5\n3 5 1 6 8 1\npeach\ngrapefruit\nbanana\norange\norange"], "sample_outputs": ["7 19", "11 30"], "notes": null}, "positive_code": [{"source_code": "($n, $m) = split(' ', <>);\n@pr = sort {$a <=> $b} split(' ', <>);\n\nwhile(<>){ $h{$_} ++; }\n\nfor(keys %h){ push (@c, $h{$_}); }\n\n@c = reverse(sort {$a <=> $b} @c);\n\nfor(0..$m){ $a += $c[$_] * $pr[$_]; }\nprint $a; $a = 0;\n@pr = reverse(@pr);\n\nfor(0..$m){ $a += $c[$_] * $pr[$_]; }\nprint \" $a\";"}, {"source_code": "#!/bin/perl -w\nuse strict;\nuse warnings;\n\nmy ($n, $m) = map int, split /\\D/, <>;\nmy @prices = map int, split /\\D/, <>;\nmy %fruits;\nfor (1..$m) {\n my $fruit = <>;\n chomp $fruit;\n if (defined $fruits{$fruit}) {\n\t$fruits{$fruit}++;\n } else {\n\t$fruits{$fruit} = 1;\n }\n}\n\n@prices = sort { $a <=> $b } @prices;\nmy ($cmin, $cmax, $in, $ix) = (0, 0, 0, $#prices);\nfor my $key (sort { $fruits{$b} <=> $fruits{$a} } keys %fruits) {\n $cmin += $fruits{$key} * $prices[$in++];\n $cmax += $fruits{$key} * $prices[$ix--];\n}\n\nprint \"$cmin $cmax\\n\";\n"}], "negative_code": [], "src_uid": "31c43b62784a514cfdb9ebb835e94cad"} {"nl": {"description": "Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples.For example, triples (3,\u20094,\u20095), (5,\u200912,\u200913) and (6,\u20098,\u200910) are Pythagorean triples.Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse.Katya had no problems with completing this task. Will you do the same?", "input_spec": "The only line of the input contains single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009109)\u00a0\u2014 the length of some side of a right triangle.", "output_spec": "Print two integers m and k (1\u2009\u2264\u2009m,\u2009k\u2009\u2264\u20091018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print \u2009-\u20091 in the only line. If there are many answers, print any of them.", "sample_inputs": ["3", "6", "1", "17", "67"], "sample_outputs": ["4 5", "8 10", "-1", "144 145", "2244 2245"], "notes": "NoteIllustration for the first sample."}, "positive_code": [{"source_code": "use bigint;\n\n\nmy $m, $n, $a, $b, $c;\nmy $i, $j;\n\nmy $f = ;\nchomp ( $f );\n\nmy $g = $f;\n\nif ( $f <= 2 ) {\n print \"-1\\n\";}\nelsif ( $g % 2 == 0 ) {\t\t# n is even \n $g /= 2;\t\t\t\n $a = $g * $g + 1;\n $b = $g * $g - 1;\n print \"$a $b\\n\";\n}\nelse {\t\t\t\t# n is odd\n my $var = ($g * $g + 1) / 2;\n my $uu = sqrt ( ($var * $var) - ($g * $g) );\n print \"$var $uu\\n\";\n}\n\n"}, {"source_code": "use bigint;\n$n = 0 + <>;\nif ($n->is_odd) {\n\t$x = ($n+1)/2;\n\t$y = ($n-1)/2;\n\t$a = 2*$x*$y;\n} else {\n\t$x = $n/2;\n\t$y = 1;\n\t$a = $x**2 - $y**2;\n}\n$b = $x**2 + $y**2;\nprint $a? \"$a $b\": -1"}], "negative_code": [{"source_code": "\n\nmy $m, $n, $a, $b, $c;\nmy $i, $j;\nmy $upper = int sqrt ( 10 ** 9 );\n\nmy $f = ;\nchomp ( $f );\n\nmy $g = $f;\n# every even number is will be the side of a pythagorean triple\nif ( $f <= 2 ) {\n print \"-1\\n\"; }\nelsif ( $g % 2 == 0 ) {\n $a = $g * $g + 1;\n $b = $g * $g - 1;\n print \"$a $b\\n\";\n}\nelse {\n\n\n for ( $i = 1; $i < $upper and (not $found); $i += 1 ) {\n\tfor ( $j = $i + 1; $j < $upper and (not $found); $j += 1 ) {\n\t $a = ($j ** 2) - ($i ** 2);\n\t $b = 2 * $j * $i;\n\t $c = ($j ** 2) + ($i ** 2);\n\t if ( $a == $f or $b == $f or $c == $f ) {\n\t\tif ( $a != $f ) { print \"$a \";}\n\t\tif ( $b != $f ) { print \"$b \";}\n\t\tif ( $c != $f ) { print \"$c \";}\n\t\t$found = 1;}\n\t}\n }\n\nif ( not $found ) { print \"-1\"; }\nprint \"\\n\";\n}\n\n"}, {"source_code": "\n\nmy $m, $n, $a, $b, $c;\nmy $i, $j;\n\nmy $f = ;\nchomp ( $f );\n\nmy $g = $f;\n\nif ( $f <= 2 ) {\n print \"-1\\n\";}\nelsif ( $g % 2 == 0 ) {\t\t# n is even \n $g /= 2;\t\t\t\n $a = $g * $g + 1;\n $b = $g * $g - 1;\n print \"$a $b\\n\";\n}\nelse {\t\t\t\t# n is odd\n my $var = ($g * $g + 1) / 2;\n my $uu = sqrt ( ($var * $var) - ($g * $g) );\n print \"$var $uu\\n\";\n}\n\n"}], "src_uid": "df92643983d6866cfe406f2b36bec17f"} {"nl": {"description": "You are given an array of n integer numbers a0,\u2009a1,\u2009...,\u2009an\u2009-\u20091. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.", "input_spec": "The first line contains positive integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 size of the given array. The second line contains n integers a0,\u2009a1,\u2009...,\u2009an\u2009-\u20091 (1\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 elements of the array. It is guaranteed that in the array a minimum occurs at least two times.", "output_spec": "Print the only number \u2014 distance between two nearest minimums in the array.", "sample_inputs": ["2\n3 3", "3\n5 6 5", "9\n2 1 3 5 4 1 2 3 1"], "sample_outputs": ["1", "2", "3"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t$_ = <>;\n\t@_ = split;\n\t\n\tmy $min = ( sort { $a <=> $b } @_ )[ 0 ];\n\tmy $i = 1e5;\n\tmy $MIN = 1e5;\n\t\n\tfor( @_ ){\n\t\t$_ == $min ? do {\n\t\t\t\t$MIN > $i and $MIN = $i;\n\t\t\t\t$i = 1;\n\t\t\t}\n\t\t\t: do {\n\t\t\t\t$i ++;\n\t\t\t};\n\t\t}\n\t\n\tprint $MIN;\n\t}"}], "negative_code": [], "src_uid": "67af292ff23880ad9fd4349729e36158"} {"nl": {"description": "The Greatest Secret Ever consists of n words, indexed by positive integers from 1 to n. The secret needs dividing between k Keepers (let's index them by positive integers from 1 to k), the i-th Keeper gets a non-empty set of words with numbers from the set Ui\u2009=\u2009(ui,\u20091,\u2009ui,\u20092,\u2009...,\u2009ui,\u2009|Ui|). Here and below we'll presuppose that the set elements are written in the increasing order.We'll say that the secret is safe if the following conditions are hold: for any two indexes i,\u2009j (1\u2009\u2264\u2009i\u2009<\u2009j\u2009\u2264\u2009k) the intersection of sets Ui and Uj is an empty set; the union of sets U1,\u2009U2,\u2009...,\u2009Uk is set (1,\u20092,\u2009...,\u2009n); in each set Ui, its elements ui,\u20091,\u2009ui,\u20092,\u2009...,\u2009ui,\u2009|Ui| do not form an arithmetic progression (in particular, |Ui|\u2009\u2265\u20093 should hold). Let us remind you that the elements of set (u1,\u2009u2,\u2009...,\u2009us) form an arithmetic progression if there is such number d, that for all i (1\u2009\u2264\u2009i\u2009<\u2009s) fulfills ui\u2009+\u2009d\u2009=\u2009ui\u2009+\u20091. For example, the elements of sets (5), (1,\u200910) and (1,\u20095,\u20099) form arithmetic progressions and the elements of sets (1,\u20092,\u20094) and (3,\u20096,\u20098) don't.Your task is to find any partition of the set of words into subsets U1,\u2009U2,\u2009...,\u2009Uk so that the secret is safe. Otherwise indicate that there's no such partition.", "input_spec": "The input consists of a single line which contains two integers n and k (2\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u2009106) \u2014 the number of words in the secret and the number of the Keepers. The numbers are separated by a single space.", "output_spec": "If there is no way to keep the secret safe, print a single integer \"-1\" (without the quotes). Otherwise, print n integers, the i-th of them representing the number of the Keeper who's got the i-th word of the secret. If there are multiple solutions, print any of them.", "sample_inputs": ["11 3", "5 2"], "sample_outputs": ["3 1 2 1 1 2 3 2 2 3 1", "-1"], "notes": null}, "positive_code": [{"source_code": "chomp ($ip = );\n$ip =~ s#^\\s+|\\s+$##g;\n($n, $k) = split /\\s+/, $ip;\nif ($n >= $k * 3) {\n\t$n -= $k * 2;\n\tforeach (1 .. $k) {\n\t\tprintf \"$_ $_ \";\n\t}\n\t@nos = (1 .. $k);\n\tforeach (1 .. $n) {\n\t\t$x = shift @nos;\n\t\tprintf \"$x \";\n\t\tpush @nos, $x;\n\t}\n\tprintf \"\\n\";\n} else {\n\tprintf \"-1\\n\";\n}\n"}], "negative_code": [], "src_uid": "aba3bb100e2d3d30dc4b00818f190bff"} {"nl": {"description": "Vasya has found a piece of paper with a coordinate system written on it. There are n distinct squares drawn in this coordinate system. Let's number the squares with integers from 1 to n. It turned out that points with coordinates (0,\u20090) and (ai,\u2009ai) are the opposite corners of the i-th square.Vasya wants to find such integer point (with integer coordinates) of the plane, that belongs to exactly k drawn squares. We'll say that a point belongs to a square, if the point is located either inside the square, or on its boundary. Help Vasya find a point that would meet the described limits.", "input_spec": "The first line contains two space-separated integers n, k (1\u2009\u2264\u2009n,\u2009k\u2009\u2264\u200950). The second line contains space-separated integers a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109). It is guaranteed that all given squares are distinct.", "output_spec": "In a single line print two space-separated integers x and y (0\u2009\u2264\u2009x,\u2009y\u2009\u2264\u2009109) \u2014 the coordinates of the point that belongs to exactly k squares. If there are multiple answers, you are allowed to print any of them. If there is no answer, print \"-1\" (without the quotes).", "sample_inputs": ["4 3\n5 1 3 4", "3 1\n2 4 1", "4 50\n5 1 10 2"], "sample_outputs": ["2 1", "4 0", "-1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp($_ = <>);\nmy ($n, $k) = split;\nchomp($_ = <>);\nmy @a = split;\n@a = sort { -($a <=> $b) } @a;\nif ($k - 1 < $n) {\n my $res = $a[$k - 1];\n print \"$res $res\\n\";\n} else {\n print \"-1\\n\";\n}\n"}, {"source_code": "while (<>){\n ($n, $_)=split/ /;\n chomp;\n @_=split/ /,<>;\n chomp(@_);\n @_=sort{$a<=>$b}@_;\n # print \"@_\\n\";\n $_>$n and print \"-1\\n\" and next;\n $_=@_-$_;\n unshift @_,-1;\n $a=$_[$_+1];\n $a==$_[$_] and print \"-1\\n\" and next;\n print \"$a $a\\n\";\n }"}], "negative_code": [{"source_code": "while (<>){\n ($n, $_)=split/ /;\n chomp;\n @_=split/ /,<>;\n chomp(@_);\n @_=sort{$a<=>$b}@_;\n print \"@_\\n\";\n $_>$n and print \"-1\\n\" and next;\n $_=@_-$_;\n unshift @_,-1;\n $a=$_[$_+1];\n $a==$_[$_] and print \"-1\\n\" and next;\n print \"$a $a\\n\";\n }"}], "src_uid": "4d743a00e11510c824080ad7f1804021"} {"nl": {"description": "Vasya has become interested in wrestling. In wrestling wrestlers use techniques for which they are awarded points by judges. The wrestler who gets the most points wins.When the numbers of points of both wrestlers are equal, the wrestler whose sequence of points is lexicographically greater, wins.If the sequences of the awarded points coincide, the wrestler who performed the last technique wins. Your task is to determine which wrestler won.", "input_spec": "The first line contains number n \u2014 the number of techniques that the wrestlers have used (1\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105). The following n lines contain integer numbers ai (|ai|\u2009\u2264\u2009109, ai\u2009\u2260\u20090). If ai is positive, that means that the first wrestler performed the technique that was awarded with ai points. And if ai is negative, that means that the second wrestler performed the technique that was awarded with (\u2009-\u2009ai) points. The techniques are given in chronological order.", "output_spec": "If the first wrestler wins, print string \"first\", otherwise print \"second\"", "sample_inputs": ["5\n1\n2\n-3\n-4\n3", "3\n-1\n-2\n3", "2\n4\n-4"], "sample_outputs": ["second", "first", "second"], "notes": "NoteSequence x\u2009\u2009=\u2009\u2009x1x2... x|x| is lexicographically larger than sequence y\u2009\u2009=\u2009\u2009y1y2... y|y|, if either |x|\u2009\u2009>\u2009\u2009|y| and x1\u2009\u2009=\u2009\u2009y1,\u2009\u2009x2\u2009\u2009=\u2009\u2009y2,\u2009... ,\u2009\u2009x|y|\u2009\u2009=\u2009\u2009y|y|, or there is such number r (r\u2009\u2009<\u2009\u2009|x|,\u2009r\u2009\u2009<\u2009\u2009|y|), that x1\u2009\u2009=\u2009\u2009y1,\u2009\u2009x2\u2009\u2009=\u2009\u2009y2,\u2009\u2009... ,\u2009\u2009xr\u2009\u2009=\u2009\u2009yr and xr\u2009\u2009+\u2009\u20091\u2009\u2009>\u2009\u2009yr\u2009\u2009+\u2009\u20091.We use notation |a| to denote length of sequence a."}, "positive_code": [{"source_code": "while($n=<>){\n\t\n\t@a = @b = ();\n\t$a = $b = 0;\n\t\n\tfor $i(1..$n){\n\t\t$_=<>, chomp;\n\t\tif ($_ > 0)\n\t\t\t{push @a, $_; $a += $_}\n\t\telse \n\t\t\t{push @b, -$_; $b -= $_}\n\t\t}\n\n\t$f = $a <=> $b;\n\t$g = 0;\n\tunless ($f){\n\t\tfor $j(0..@a-1){\n\t\t\t$g = $a[ $j ] <=> $b[ $j ];\n\t\t\t$g and $f = $g and last;\n\t\t\t}\n\t\t}\n\tunless ($f){\n\t\t$f -= /-/ * 2 - 1\n\t\t}\n\t\t\n\tprint ( (undef, first, second)[$f], $/ )\n\t}"}], "negative_code": [], "src_uid": "d3684227d1f12cf36dc302e1ffee8370"} {"nl": {"description": "You are given two integers $$$x$$$ and $$$y$$$ (it is guaranteed that $$$x > y$$$). You may choose any prime integer $$$p$$$ and subtract it any number of times from $$$x$$$. Is it possible to make $$$x$$$ equal to $$$y$$$?Recall that a prime number is a positive integer that has exactly two positive divisors: $$$1$$$ and this integer itself. The sequence of prime numbers starts with $$$2$$$, $$$3$$$, $$$5$$$, $$$7$$$, $$$11$$$.Your program should solve $$$t$$$ independent test cases.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) \u2014 the number of test cases. Then $$$t$$$ lines follow, each describing a test case. Each line contains two integers $$$x$$$ and $$$y$$$ ($$$1 \\le y < x \\le 10^{18}$$$).", "output_spec": "For each test case, print YES if it is possible to choose a prime number $$$p$$$ and subtract it any number of times from $$$x$$$ so that $$$x$$$ becomes equal to $$$y$$$. Otherwise, print NO. 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\n100 98\n42 32\n1000000000000000000 1\n41 40"], "sample_outputs": ["YES\nYES\nYES\nNO"], "notes": "NoteIn the first test of the example you may choose $$$p = 2$$$ and subtract it once.In the second test of the example you may choose $$$p = 5$$$ and subtract it twice. Note that you cannot choose $$$p = 7$$$, subtract it, then choose $$$p = 3$$$ and subtract it again.In the third test of the example you may choose $$$p = 3$$$ and subtract it $$$333333333333333333$$$ times."}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nchomp (my $line = );\n\nmy $t = $line;\nmy @primes = (2, 3, 5, 7, 11);\n\nTESTCASE:\nfor (1..$t) {\n chomp ($line = );\n my ($x, $y) = split q{ }, $line;\n\n my $z = $x - $y;\n\n if ( $z == 1 ) { say \"no\"; next TESTCASE; }\n else { say \"yes\"; next TESTCASE; }\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $x, $y ) = split;\n\t\n\tprint $x == $y + 1 ? \"NO\" : \"YES\";\n\t}"}], "negative_code": [], "src_uid": "0671972bc2b6452d51d048329e6e0106"} {"nl": {"description": "Sometimes some words like \"localization\" or \"internationalization\" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, \"localization\" will be spelt as \"l10n\", and \"internationalization\u00bb will be spelt as \"i18n\".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.", "input_spec": "The first line contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.", "output_spec": "Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.", "sample_inputs": ["4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis"], "sample_outputs": ["word\nl10n\ni18n\np43s"], "notes": null}, "positive_code": [{"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>\n"}, {"source_code": "chomp($num = );\nwhile($num-- > 0) {\n\tchomp($word = );\n\t$l = length($word);\n\tif($l < 11) {\n\t\tprint $word;\n\t} else {\n\t\tprint substr($word, 0, 1).($l-2).substr($word, -1, 1);\n\t}\n\tprint \"\\n\";\n}"}, {"source_code": "use 5.010;\nuse strict;\nuse warnings;\n\nmy $n = <>;\nwhile ($n--) {\n chomp( $_ = <> );\n s/^(\\w)(\\w*)(\\w)$/$1 . length($2) . $3/e if length $_ > 10;\n say;\n}\n"}, {"source_code": "$n = <>;\nfor my $i (1..$n)\n{\n\tchomp($s = <>);\n\t$len = length $s;\n\tif ($len > 10)\n\t{\n\t\t$res = substr($s, 0, 1).($len-2).substr($s, $len-1, 1);\n\t}\n\telse\n\t{\n\t\t$res = $s;\n\t}\n\tprint \"$res\\n\";\n}"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nfor (1..$n) {\n\tmy $s = <>;\n\tchomp $s;\n\tmy $len = length $s;\n\tif ($len > 10) {\n\t\tsubstr($s,1,-1) = $len-2;\t\n\t}\n\tprint \"$s\\n\";\n}\n"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "$n=<>;\n\n\nfor($i=0;$i<$n;$i++){\n$str=<>;\nchomp$str;\npush(@arr,$str);\n}\nfor($i=0;$i<$n;$i++){\n$l=length$arr[$i];\nif($l<=10){\nprint \"$arr[$i]\\n\";\n}\nelse{\n$k=$l;\n$l-=2;\nprint substr($arr[$i],0,1).\"$l\".substr($arr[$i],$k-1,1).\"\\n\";\n\n}\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nchomp($n=);\nwhile($n--)\n{\nchomp ($str=);\nmy @array = split(//,$str);\n$c=$#array;\nif ($c<10)\n{\nprint \"$str\\n\";}\nelse\n{\n$m=$c-1;\nmy $e= pop(@array);\n$b= shift(@array);\nprint\"$b\";\nprint\"$m\";\nprint\"$e\\n\";\n}\n}"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>\n"}, {"source_code": "#!/bin/perl\nuse strict;\nuse warnings;\nmy @chars = ();\nmy $line = '';\n\nchomp (my $line_n = <>);\nfor (1..$line_n) {\n chomp($line = <>);\n if (length($line) >= 11) {\n @chars = $line =~ /./sg;\n $line = \"$chars[0]\" . (length($line)-2) . \"$chars[-1]\";\n }\n print \"$line \\n\";\n}"}, {"source_code": "#!/bin/perl\nuse strict;\nuse warnings;\nmy @chars = ();\nmy $line = '';\nmy $l = 0;\n\nchomp (my $line_n = <>);\nfor (1..$line_n) {\n chomp($line = <>);\n $l = length($line);\n if ($l >= 11) {\n @chars = $line =~ /./sg;\n $line = \"$chars[0]\" . ($l-2) . \"$chars[-1]\";\n }\n print \"$line \\n\";\n}"}, {"source_code": "use strict;\nuse warnings;\nchomp (my $line_n = <>);\nmy @chars = ();\nmy $i = 0;\nmy $line = '';\nmy @out = ();\nmy $l = 0;\nfor ($i=0; $i<$line_n; $i++) {\n chomp($line = <>);\n $l = length($line);\n if ($l < 11) {\n $out[$i] = $line;\n } else {\n @chars = $line =~ /./sg;\n $out[$i] = \"$chars[0]\" . ($l-2) . \"$chars[-1]\";\n }\n}\nforeach (@out) {print \"$_ \\n\";}"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "; while(){chomp; if((our $len = length $_) <= 10){ print \"$_\\n\"; }else{ print substr($_, 0, 1), $len-2, substr($_, -1, 1), \"\\n\" } }"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "use strict;\nuse warnings;\nuse feature \"say\";\n\nmy $n = ;\n\nfor ($_ = 0; $_ < $n; $_++)\n{\n my $word = ;\n chomp $word;\n my $len = length $word;\n if ($len > 10)\n {\n $word = substr($word, 0, 1) . ($len - 2) . substr($word, $len - 1, 1)\n }\n say $word;\n}\n"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>\n"}, {"source_code": "use v5.20;\n\nuse warnings;\nuse strict;\n\nmy $n = <>;\nchomp($n);\n\nwhile($n--)\n{\n\tmy $s =<>;\n\tchomp($s);\n\n\tif(length $s > 10)\n\t{\n\t\tmy $first = substr($s, 0, 1);\n\t\tmy $last = substr($s, -1);\n\t\tmy $length = (length $s) - 2;\n\t\tsay \"$first$length$last\";\n\t}\n\telse\n\t{\n\t\tsay \"$s\";\n\t}\n}\n\n"}, {"source_code": "#!/usr/bin/perl -w\n\nfor (1..<>) {\n\t$_ = <>;\n\ts/\\B\\w{9,}\\B/length $&/e;\n\tprint $_;\n}\n\n"}, {"source_code": "#!/usr/bin/perl\n$cases = <>; #number of cases\nwhile($cases--){ \n $myname=<>; #get input name\n $lengthofmyname=length($myname); #get its name\n $lengthofmyname = $lengthofmyname+0; #covt to num for being safe\n $maxlen=11; #maxlen permissible\n if($lengthofmyname>$maxlen){ \n $first=substr($myname,0,1); #get first letter\n $last=substr($myname,$lengthofmyname-2,1); #get last letter\n $lengthofmyname=$lengthofmyname-3; #decr by 3 as it returns one extra\n print $first; \n print $lengthofmyname;\n print $last;\n print \"\\n\";\n }\n else{\n print \"$myname\\n\";\n }\n}"}, {"source_code": "#!/usr/bin/env perl\n \nuse strict;\nuse warnings;\nuse feature qw/ say /;\n \nuse Carp;\n \n# essential\nmy @tokens = ();\n \nsub ceil {\n my $n = shift;\n my $in = int($n);\n return $n if $in == $n;\n return $in + 1;\n}\n \nsub say_all {\n my @args = @_;\n say join ' ', @args;\n}\n \nsub read_line {\n chomp (my $line = );\n return $line;\n}\n \nsub read_token {\n @tokens = split q{ }, read_line() if @tokens == 0;\n return shift @tokens;\n}\n \nsub min {\n my @numbers = @_;\n my $min = shift @numbers;\n for ( @numbers ) {\n $min = $min<$_ ? $min : $_;\n }\n return $min;\n}\n \nsub max {\n my @numbers = @_;\n my $max = shift @numbers;\n for ( @numbers ) {\n $max = $max > $_ ? $max : $_;\n }\n return $max;\n}\n \nsub sum {\n my @numbers = @_;\n my $sum = 0;\n for (@numbers) {\n $sum += $_;\n }\n return $sum;\n}\n \nsub ref_ref_scalar {\n my $ref = shift;\n return 1 if ref($ref) eq 'SCALAR';\n}\n \nsub toggle {\n my $ref = shift;\n croak \"$ref does not reference to scalar\" if !ref_ref_scalar($ref);\n \n $$ref = !$$ref;\n}\n \nsub odd {\n my $num = shift;\n return $num % 2 == 1;\n}\n \nsub even {\n my $num = shift;\n return $num % 2 == 0;\n}\n \nsub sum_of_digits {\n my $n = shift;\n my @numbers = split q{}, $n;\n \n my $sum = 0;\n \n for (@numbers) {\n $sum += $_;\n }\n \n return $sum;\n}\n \n# solution\n \nmy $n = read_token;\n\nfor (1..$n) {\n my $word = read_token;\n my $len = length $word;\n my $answer;\n if ($len > 10) {\n $answer = substr($word, 0, 1).($len-2).substr($word, $len-1, 1);\n }\n else {\n $answer = $word;\n }\n say $answer;\n}\n"}, {"source_code": "<>;\nprint(map({\n @x = split(\"\", $_);\n $c = @x - 3;\n @x > 11 ? \"$x[0]$c$x[-2]\\n\" : \"$_\"\n} <>))\n"}, {"source_code": "<>;\nprint(map({\n @x = split(\"\", $_);\n $c = @x - 3;\n @x > 11 ? \"$x[0]$c$x[-2]\\n\" : \"$_\\n\"\n} <>))\n"}, {"source_code": "for$i(1..<>)\n{\n $_=<>;\n s/(?<=^.)(.{9,})(?=.$)/length($1)/e;\n print;\n}"}, {"source_code": "$n = <>;\n\nfor ($i = 0; $i < $n; ++$i) {\n $str = <>;\n chomp $str;\n $len = (length $str) - 2;\n $str = substr ($str, 0, 1) . \"$len\" . chop ($str) if $len > 8; \n print $str . \"\\n\";\n}\n"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "$n = <>;\n\nfor(1..$n) {\n\t$s = <>;\n\tchomp $s;\n\t$l = length $s;\n\n\tif ($l > 10) {\n\t\t@a = split \"\", $s;\n\t\t$s = $a[0].($l-2).$a[-1];\n\t}\n\n\tprint \"$s\\n\";\n}"}, {"source_code": "#!/usr/bin/perl;\n\n$n = ;\n\nmy @words;\n\nfor ($i = 0; $i < $n; $i++) {\n\t$m = ;\n\tpush(@words, $m);\n}\n\nfor ($i = 0; $i < $n; $i++) {\n\tif (length(@words[$i]) < 12) {\n\t\tprint(@words[$i]);\n\t} else {\n\t\tprint(substr(@words[$i], 0, 1),(length(@words[$i]) - 3),substr(@words[$i], -2, 1),\"\\n\");\n\t}\n}\n"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>\n"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "#!/usr/bin/perl\nuse strict; use warnings;\n\nsub shorten\n{ my $s = $_[0]; my $n = length($s) - 3;\n substr $s, 1, $n, $n if $n > 8; $s }\n\n;\nprint shorten $_ while \n"}, {"source_code": "my $n = <>;\nmy $s;\n\nfor (my $i = 0; $i < $n; $i++) {\n\t$s = <>;\n\t$s =~ s/([A-z])([A-z]{9,})([A-z])/$1 . (length ($2)) . $3/e;\n\tprint $s;\n}"}, {"source_code": "#!/usr/bin/perl\n\n$n = <>;\n\nwhile($n--){\n\t$word = <>;\n\t\n\t$len = length($word) - 1;\n\tif($len <11){\n\t\tprint \"$word\\n\";\n\t}\n\telse{\n\t\t$mid = $len - 2;\n\t\t@chars = split(\"\",$word);\n\t\tprint \"$chars[0]$mid$chars[$len-1]\\n\";\n\t}\n}"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "$n = <>;\nfor(1..$n) {\n $s = <>;\n chomp $s;\n @a = split \"\", $s;\n $n = scalar @a;\n if( $n > 10 ) {\n $s = $a[0] . (scalar @a - 2) . $a[-1];\n }\n print \"$s\\n\";\n}"}, {"source_code": "$n = <>;\nfor(1..$n) {\n $s = <>;\n chomp $s;\n @a = split \"\", $s;\n $n = scalar @a;\n \n if( $n > 10 ) {\n $s = $a[0] . ($n - 2) . $a[-1];\n }\n print $s . \"\\n\";\n}"}, {"source_code": "$n = <>;\nfor(1..$n) {\n $s = <>;\n chomp $s;\n $n = length($s);\n \n if( $n > 10 ) {\n $s = substr($s, 0, 1) . ($n - 2) . substr($s, -1, 1);\n }\n print $s . \"\\n\";\n}"}, {"source_code": "$n = ;\nfor($i=0;$i<=$n-1;$i++)\n{\n $word = ;\n @chars = split(\"\",$word);\n $l1 = split(\"\",$word);\n $l = $l1-1; #Newline character\n $d = $l-2;\n if($l>10)\n {\n @word[$i] = \"@chars[0]$d@chars[$l-1]\";\n }\n else\n {\n @word[$i] = substr($word,0,$l);\n }\n\n}\n\nfor($i=0;$i<$n;$i++)\n{\n print \"@word[$i]\\n\"\n}\n"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "<>;\nwhile(<>){\nchomp;\nif (10;\nfor(1..$n) {\n $str = <>;\n chop $str;\n $l = length($str);\n if($l > 10) {\n $str =~ s/(.).+(.)/$1.($l-2).$2/e;\n }\n print $str, \"\\n\";\n}\n"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\nuse v5.10;\n\n;\nfor () {\n chomp;\n s/^(.)(.+)(.)$/$1.length($2).$3/e if length > 10;\n say;\n}\n "}, {"source_code": "<>;for(<>){s/(?<=.).{9,}(?=.)/(length)-3/e;print}\n"}, {"source_code": "<>;for(<>){$_=$1.((length)-3).$2 if/(.).{9,}(.\\s)/;print}\n"}, {"source_code": "<>;for(<>){$_=$1.(length$2).$3.\"\\n\"if/(.)(.{9,})(.)/;print}\n"}, {"source_code": "#!perl\n\nmy $num = ;\nfor (1..$num) {\n my $word = ;\n chomp $word;\n my @word = split //, $word;\n (@word > 10) ? print $word[0], @word - 2 , $word[-1],\"\\n\" : print $word, \"\\n\";\n}"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>\n"}, {"source_code": "my $len = ;\n\nfor my $val (1 .. $len) {\n\tmy $word = ;\n\t$word =~ s/\\s//g;\n\n\tmy $word_length = length $word;\n\n\tif ($word_length > 10) {\n\t\tmy $f_let = substr $word, 0, 1;\n\t\tmy $l_let = substr $word, $word_length - 1, 1;\n\t\t\n\t\tprint $f_let.($word_length - 2).$l_let.\"\\n\";\n\t} else {\n\t\tprint $word.\"\\n\";\n\t}\n}\n"}, {"source_code": "use 5.020;\n\nreadline;\n\nwhile(<>) {\n chomp;\n s/(.)(.+)(.)/$1 . length($2) . $3/e if length $_ > 10;\n say;\n}\n"}, {"source_code": " <>;\n s/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for<>;"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "<>;for(<>){s/(?<=.).{9,}(?=.)/(length)-3/e;print}"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>\n"}, {"source_code": "#!/usr/bin/env perl\n\nuse v5.012;\nuse warnings;\n\nchomp(my $count = );\n\nwhile ($count--) {\n\tchomp(my $word = );\n\tif (length $word > 10) {\n\t\t$word =~ /^(.)(.*)(.)$/;\n\t\t$word = $1 . (length $2) . $3;\n\t}\n\tsay $word;\n}\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = ;\n\nwhile($n-- > 0){\n my $s = ;\n chomp($s);\n if(length($s) <= 10){\n print \"$s\\n\";\n }else{\n print substr($s,0,1).(length($s)-2).substr($s,length($s)-1,1).\"\\n\";\n }\n}"}, {"source_code": "my $a = <>;\nforeach (0 .. $a)\n{\n my $str = <>;\n chomp $str;\n if (length $str <= 10) {\n print $str, \"\\n\";\n } else {\n print substr ($str, 0, 1), (length $str) - 2, substr ($str, -1, 1), \"\\n\";\n }\n}"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>\n"}, {"source_code": "$n = ;\nfor ( $i = 0 ; $i < $n ; $i++ ) {\n\t$s = ;\n\tchomp $s;\n\tif ( $s =~ /(\\w)(\\w{9,})(\\w)/ ) {\n\t\tprint \"$1\" . ( length($2) ) . \"$3\\n\";\n\t}\n\telse {\n\t\tprint \"$s\\n\";\n\t}\n}\n"}, {"source_code": "chomp($n = );\nwhile($n--)\n{\n chomp($word = );\n if (length($word) > 10)\n {\n $len = length($word) - 2;\n $word =~ s/(^.).+?(.$)/$1$len$2/;\n push @words, $word;\n }\n else\n {\n push @words, $word;\n }\n}\n\nforeach $word (@words)\n{\n print \"$word\\n\";\n}\n"}, {"source_code": "<>;for(<>){s/(?<=.).{9,}(?=.)/(length)-3/e;print}"}, {"source_code": "my $n = <>;\nmy $str;\nmy @arr;\nfor(1..$n) {\n $str = <>;\n chomp $str;\n @arr = split \"\", $str;\n $str = $arr[0] . (scalar @arr-2) . $arr[-1] if (scalar @arr > 10);\n print $str . \"\\n\";\n}"}, {"source_code": "\n$n = ;\nwhile($n != 0) {\n\t$s = ;\n\tif(length($s) - 1 > 10) {\n\t\tprint substr($s, 0, 1) . (length($s) - 3) . substr($s, length($s) - 2, 1) . \"\\n\";\n\t} else {\n\t\tprint \"$s\";\n\t}\n\t$n--;\n}\n\n# 4\n# word\n# localization\n# internationalization\n# pneumonoultramicroscopicsilicovolcanoconiosis\n\n# \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n\n# word\n# l10n\n# i18n\n# p43s\n"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "# Author Badhan Sen\n# CSE-14, JUST\n\n# 71A. Way Too Long Words\n# Accepted\n\nmy $num=<>;\n\nwhile($num--){\n\tmy $str=<>;\n\t\n\tif(length($str)-1 > 10){\n\t\tprint substr($str, 0, 1).(length($str)-3).substr($str, length($str)-2, 1);\n\t\tprint \"\\n\";\n\t}\n\telse{\n\t\tprint $str;\n\t\tprint \"\\n\";\n\t}\n}"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>\n"}, {"source_code": "#!/usr/bin/env perl\n\n\nchomp(my $n = );\n\nfor ( 0..$n - 1 )\n{\n\tchomp(my $word = );\n\n\tif ( length($word) > 10 )\n\t{\n\t\t$word = substr($word, 0, 1).(length($word) - 2).substr($word, length($word) - 1);\n\n\t}\n\n\tprint(\"$word\\n\");\n\n}\n\n\n"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse utf8;\nuse v5.20;\n\nsub main {\n my $lines = int(readline(STDIN)) || 0;\n\n while ($lines--) {\n my $word = readline(STDIN);\n chomp $word;\n say length $word <= 10\n ? $word\n : substr($word, 0, 1 ) . length(substr($word, 1, -1)) . substr($word,-1);\n }\n\n return;\n}\n\nmain();\n\nexit;\n\n"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse utf8;\nuse v5.20;\n\nsub main {\n my %args = @_;\n\n my $lines = int(readline(STDIN)) || 0;\n\n while ($lines--) {\n my $world = readline(STDIN);\n $world =~ s/(?<=^\\w).{9,}(?=\\w$)/length $&/e;\n print $world;\n }\n\n return;\n}\n\nmain();\n\nexit;\n\n"}, {"source_code": "my $n = ;\nwhile($n--) {\n my $s = ;\n if((length($s) -1) > 10) {\n print substr($s, 0, 1).(length($s) - 3).substr($s, length($s) - 2, 1).\"\\n\";\n }\n else {\n print $s;\n }\n}"}, {"source_code": "my $n = ;\nwhile($n--) {\n my $s = ;\n chomp($s);\n if(length($s) > 10) {\n print substr($s, 0, 1).(length($s) - 2).substr($s, length($s) - 1, 1).\"\\n\";\n }\n else {\n print $s.\"\\n\";\n }\n}"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>\n"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "#!/usr/bin/perl\n# your code goes here\nuse v5.10;\n\nchomp ($n = <>);\nwhile ($n-- > 0) {\n\tchomp($line = <>);\n\t$len = length($line)-2;\n\t$line_ = substr($line, 0, 1) . \"$len\" . substr($line, $len+1, 1);\n\t$len<=8 and say $line or say $line_;\n}\n"}, {"source_code": "my $n = <>;\n\nwhile (<>) {\n chomp;\n \n if (length($_) > 10) {\n my ($f,$l) = $_ =~ /^(.).+(.)$/;\n print $f . (length($_) - 2) . $l . \"\\n\";\n next;\n }\n print $_ . \"\\n\";\n}"}, {"source_code": "use strict;\nuse warnings;\n\nmy $input_lines = <>;\nmy @words;\nfor (1..$input_lines) {\n my $line = <>;\n chomp $line;\n push @words, $line;\n}\n\nmy $max_len = 10;\n\nfor my $word (@words) {\n my $word_len = length($word);\n if ( $word_len > $max_len ) {\n print substr($word, 0, 1) . ($word_len-2) . substr($word, $word_len-1, 1) . \"\\n\";\n } else {\n print \"$word\\n\";\n }\n}\n\n"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\n\nmy $i=<>;\nwhile ($i--){\nmy $a=<>;\nif(($b=length($a)-3)>8) {print substr($a,0,1),$b,substr($a,-2,1),\"\\n\";} else {print \"$a\\n\";}\n}"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/e, print for <>"}, {"source_code": "use strict; \nuse warnings; \n\nsub main\n{\n my $input = ;\n my @words;\n my $word;\n my $newWord=\"\";\n my $wordLen;\n for(my $i=0;$i<$input;$i++){\n my $inputwords = ;\n push @words, $inputwords;\n }\n for(my $i=0;$i<$input;$i++){ \n $word = shift @words;\n $wordLen = length($word);\n if($wordLen-1>10){\n $wordLen=$wordLen-3;\n my $headword = substr($word,0,1);\n my $tailword = substr($word,length($word)-2,1);\n $newWord = $headword.$wordLen.$tailword;\n printf(\"$newWord\\n\");\n }\n else{\n printf(\"$word\");\n }\n }\n}\n\nmain();"}], "negative_code": [{"source_code": "use strict; \nuse warnings; \n\nsub main\n{\n my $input = ;\n my @words;\n my $word;\n my $newWord=\"\";\n my $wordLen;\n for(my $i=0;$i<$input;$i++){\n my $inputwords = ;\n push @words, $inputwords;\n }\n for(my $i=0;$i<$input;$i++){ \n $word = shift @words;\n $wordLen = length($word);\n if($wordLen>10){\n $wordLen=$wordLen-3;\n my $headword = substr($word,0,1);\n my $tailword = substr($word,length($word)-2,1);\n $newWord = $headword.$wordLen.$tailword;\n printf(\"$newWord\\n\");\n }\n else{\n printf(\"$word\");\n }\n }\n}\n\nmain();"}, {"source_code": "#! perl -lp\nnext if $. == 1;\ns/^(\\w)(\\w*)(\\w)$/$1 . length($2) . $3/e if length $_ > 10;\n"}, {"source_code": "use strict;\nuse warnings;\n\nmy $n = <>;\nfor (1..$n) {\n\tmy $s = <>;\n\tchomp $s;\n\tmy $len = length $s;\n\tif ($len > 10) {\n\t\tsubstr($s,1,-2) = $len-2;\t\n\t}\n\tprint \"$s\\n\";\n}\n"}, {"source_code": "$n=<>;\n\n\nfor($i=0;$i<$n;$i++){\n$str=<>;\nchomp$str;\npush(@arr,$str);\n}\nfor($i=0;$i<$n;$i++){\n$l=length$arr[$i];\nif($l<10){\nprint \"$arr[$i]\\n\";\n}\nelse{\n$k=$l;\n$l-=2;\nprint substr($arr[$i],0,1).\"$l\".substr($arr[$i],$k-1,1).\"\\n\";\n\n}\n}\n"}, {"source_code": "$n=<>;\n\n\nfor($i=0;$i<$n;$i++){\n$str=<>;\nchomp$str;\npush(@arr,$str);\n}\nfor($i=0;$i<$n;$i++){\n$l=length$arr[$i];\nif($l<10){\nprint \"$arr[$i]\\n\";\n}\nelse{\n$l-=2;\nprint substr($arr[$i],0,1).\"$l\".substr($arr[$i],$l-1,1).\"\\n\";\n\n}\n}\n"}, {"source_code": "#!/usr/bin/perl\n\nchomp($str=);\nmy @array = split(//,$str);\n$c=$#array;\nif ($c<=10)\n{\nprint \"$str\\n\";}\nelse\n{\n$m=$c-2;\nmy $e= pop(@array);\n$b= shift(@array);\nprint\"$b\";\nprint\"$m\";\nprint\"$e\";\n}"}, {"source_code": "#!/usr/bin/perl\n\nchomp($n=);\nwhile($n--)\n{\nchomp ($str=);\nmy @array = split(//,$str);\n$c=$#array;\nif ($c<=10)\n{\nprint \"$str\\n\";}\nelse\n{\n$m=$c-2;\nmy $e= pop(@array);\n$b= shift(@array);\nprint\"$b\";\nprint\"$m\";\nprint\"$e\\n\";\n}\n}"}, {"source_code": "#!/usr/bin/perl\n\nchomp($n=);\nwhile($n--)\n{\nchomp ($str=);\nmy @array = split(//,$str);\n$c=$#array;\nif ($c<=10)\n{\nprint \"$str\\n\";}\nelse\n{\n$m=$c-1;\nmy $e= pop(@array);\n$b= shift(@array);\nprint\"$b\";\nprint\"$m\";\nprint\"$e\\n\";\n}\n}"}, {"source_code": "#!/bin/perl\nuse strict;\nuse warnings;\n\nchomp (my $line_n = <>);\nmy @chars = ();\nmy $i = 0;\nmy $line = '';\nmy @out = ();\nmy $l = 0;\nfor ($i=0; $i<$line_n; $i++) {\n chomp($line = <>);\n $l = length($line);\n if ($l < 10) {\n $out[$i] = $line;\n } else {\n @chars = $line =~ /./sg;\n $out[$i] = \"$chars[0]\" . ($l-2) . \"$chars[-1]\";\n }\n}\nforeach (@out) {\n print \"$_ \\n\";\n}"}, {"source_code": "; while(){chomp; if((our $len = length $_) <= 10){ print \"$_\\n\"; }else{ print substr($_, 0, 1), $len, substr($_, -1, 1), \"\\n\" } }"}, {"source_code": "#!/usr/bin/perl -w\n\nfor (1..<>) {\n\t$_ = <>;\n\ts/\\B\\w{8,}\\B/length $&/e;\n\tprint \"$_\\n\";\n}\n\n"}, {"source_code": "#!/usr/bin/perl\n$inputlines=<>;\nwhile($inputlines--){\n $word=<>;\n $len = length $word;\n if($len > 10){\n #then unpack them in array and print abbvr\n $len -=2;\n $first=substr($word,0,1);\n $last=substr($word,$len,1);\n $len=$len-1;\n print $first;\n print $len;\n print $last;\n print \"\\n\";\n }\n else{\n print \"$word\\n\";\n }\n}"}, {"source_code": "#!/usr/bin/perl\n$inputlines=<>;\nwhile($inputlines--){\n $word=<>;\n $len = length $word;\n if($len > 10){\n #then unpack them in array and print abbvr\n $len -=2;\n $first=substr($word,0,1);\n $last=substr($word,$len,1);\n print $first;\n print $len;\n print $last;\n print \"\\n\";\n }\n else{\n print \"$word\\n\";\n }\n}"}, {"source_code": "<>;\nprint(map({\n @x = split(\"\", $_);\n $c = @x - 3;\n @x > 10 ? \"$x[0]$c$x[-2]\\n\" : \"$_\\n\"\n} <>))\n"}, {"source_code": "for$i(1..<>)\n{\n $_=<>;\n s/(?<=^.)(.{10,})(?=.$)/length($1)/e;\n print;\n}"}, {"source_code": "$s = <>;\nchomp $s;\n\n$n = length $s;\n\nif ($n > 10) {\n\t@a = split \"\", $s;\n\t$s = $a[0].($n-2).$a[-1];\n}\n\nprint \"$s\\n\";"}, {"source_code": "#!/usr/bin/perl;\n\n$n = ;\n\nmy @words;\n\nfor ($i = 0; $i < $n; $i++) {\n\t$m = ;\n\tpush(@words, $m);\n}\n\nfor ($i = 0; $i < $n; $i++) {\n\tif (length(@words[$i]) < 11) {\n\t\tprint(@words[$i]);\n\t} else {\n\t\tprint(substr(@words[$i], 0, 1),(length(@words[$i]) - 3),substr(@words[$i], -2, 1),\"\\n\");\n\t}\n}\n"}, {"source_code": "@in = <>;\n$n = $in;\nprint $in;\nprint \"\\n\";\nprint $n;\nprint \"\\n\";\nfor($i = 1; $i < $n; $i++) {\n print $in[$i];\n}"}, {"source_code": "@in = <>;\n$n = $in;\nfor($i = 1; $i < $n; $i++) {\n print $in[$i];\n}"}, {"source_code": "@in = ;\n$n = $in;\nprint $in;"}, {"source_code": "$n = <>;\nfor(1..$n) {\n $s = <>;\n # chomp $s;\n @a = split \"\", $s;\n $n = scalar @a;\n if( $n > 10 ) {\n $s = $a[0] . ($n - 2) . $a[-1];\n }\n print \"$s\\n\";\n}"}, {"source_code": "<>;\ns/\\B.{9,}\\B/length$&/, print for <>"}, {"source_code": "<>;\ns/\\B.{8,}\\B/length$&/e, print for <>"}, {"source_code": "<>;\ns/\\B.{8,}\\B/length$&/, print for <>"}, {"source_code": "<>;for(<>){s/(?<=.).{9,}(?=.)/length-3/e;print}\n"}, {"source_code": "#!perl\n\nmy $num = ;\nfor (1..$num) {\n my $word = ;\n chomp $word;\n my @word = split //, $word;\n (@word > 10) ? print $word[0] , length $word - 2 , $word[-1],\"\\n\" : print $word, \"\\n\";\n}"}, {"source_code": "use 5.020;\n\nmy $len = <> * 2;\n\nwhile(<>) {\n chomp;\n s/(.)(.+)(.)/$1 . length($2) . $3/e if length $_ > $len;\n say;\n}\n"}, {"source_code": "use 5.020;\n\nmy $len = <>;\nchomp $len;\n\nwhile(<>) {\n chomp;\n s/(.)(.+)(.)/$1 . length($2) . $3/e if length $_ > $len;\n say;\n}\n"}, {"source_code": "use 5.020;\n\nmy $len = <>;\nchomp $len;\n\nfor(<>) {\n chomp;\n next if length $_ <= $len;\n s/(.)(.+)(.)/$1 . length($2) . $3/e;\n}\n"}, {"source_code": "use 5.020;\n\nmy $len = <>;\nchomp $len;\n\nwhile(<>) {\n chomp;\n next if length $_ <= $len;\n s/(.)(.+)(.)/$1 . length($2) . $3/e;\n say;\n}\n"}, {"source_code": "$n = <>;\nforeach $i (1..n)\n{\n$A=<>;\nif (length($A)>10)\n{\n$A=substr($A,0,1) . \"length($A)-2\" . substr($A,-1,1)\n}\nprint $A\n}"}, {"source_code": "$n = <>;\nforeach $i (1..n)\n{\n$A=<>;\nif (length($A)>10)\n{\n$A=substr($A,0,1)+\"length($A)-2\"+substr($A,-1,1)\n}\nprint $A\n}"}, {"source_code": "$n = ;\nfor ( $i = 0 ; $i < $n ; $i++ ) {\n\t$s = ;\n\tchomp $s;\n\tif ( $s =~ /(\\w)(\\w{8,})(\\w)/ ) {\n\t\tprint \"$1\" . ( length($2) ) . \"$3\\n\";\n\t}\n\telse {\n\t\tprint \"$s\\n\";\n\t}\n}\n"}, {"source_code": "$n = ;\nfor ( $i = 0 ; $i < $n ; $i++ ) {\n\t$s = ;\n\tif ( $s =~ /(\\w)(\\w{10,})(\\w)/ ) {\n\t\tprint \"$1\" . ( length($2) ) . \"$3\\n\";\n\t}\n\telse {\n\t\tprint \"$s\\n\";\n\t}\n}\n"}, {"source_code": "\n$n = ;\nwhile($n != 0) {\n\t$s = ;\n\tif(length($s) > 10) {\n\t\tprint substr($s, 0, 1) . (length($s) - 3) . substr($s, length($s) - 2, 1) . \"\\n\";\n\t} else {\n\t\tprint \"$s\";\n\t}\n\t$n--;\n}\n\n# 4\n# word\n# localization\n# internationalization\n# pneumonoultramicroscopicsilicovolcanoconiosis\n\n# \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n\n# word\n# l10n\n# i18n\n# p43s\n"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse utf8;\nuse v5.20;\n\nsub main {\n my %args = @_;\n\n my $lines = int(readline(STDIN)) || 0;\n\n while ($lines--) {\n my $world = readline(STDIN);\n $world =~ s/(?<=^\\w).{6,}(?=\\w$)/length $&/e;\n print $world;\n }\n\n return;\n}\n\nmain();\n\nexit;\n\n"}, {"source_code": "my $n = ;\nwhile($n--) {\n my $s = ;\n if((length($s) -1) >= 10) {\n print substr($s, 0, 1).(length($s) - 3).substr($s, length($s) - 2, 1);\n }\n else {\n print $s;\n }\n}"}, {"source_code": "my $n = ;\nwhile($n--) {\n my $s = ;\n chomp($s);\n if(length($s) > 10) {\n print substr($s, 0, 1).(length($s) - 2).substr($s, length($s) - 1, 1).\"\\n\";\n }\n else {\n print $s;\n }\n}"}, {"source_code": "my $n = ;\nwhile($n--) {\n my $s = ;\n if((length($s) -1) >= 10) {\n print substr($s, 0, 1).(length($s) - 3).substr($s, length($s) - 2, 1).\"\\n\";\n }\n else {\n print $s;\n }\n}"}, {"source_code": "use strict;\nuse warnings;\n\nmy $input_lines = <>;\nmy @words;\nprint \"\\n***\\n\";\nfor (1..$input_lines) {\n my $line = <>;\n chomp $line;\n push @words, $line;\n}\n\nmy $max_len = 10;\n\nfor my $word (@words) {\n my $word_len = length($word);\n if ( $word_len > $max_len ) {\n print substr($word, 0, 1) . ($word_len-2) . substr($word, $word_len-1, 1) . \"\\n\";\n } else {\n print \"$word\\n\";\n }\n}\n\n"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\n\nmy $i=<>;\nwhile ($i--){\nmy $a=<>;\nif(($b=length($a)-3)>7) {print substr($a,0,1),$b,substr($a,-2,1),\"\\n\";} else {print \"$a\\n\";}\n}"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\nmy $i=<>;\nwhile ($i--){\nmy $a=<>;\nif(($b=length($a)-3)>7) {print substr($a,0,1),$b,substr($a,-2,1);} else {print $a;}\n}"}, {"source_code": "#!/usr/bin/perl -w\nuse strict;\nmy $i=<>;\nwhile ($i--){\nmy $a=<>;\nif(($b=length($a)-2)>8) {print substr($a,0,1),$b,substr($a,-1,1);} else {print $a;}\n}"}], "src_uid": "6639d6c53951f8c1a8324fb24ef68f7a"} {"nl": {"description": "Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer k and a sequence a, consisting of n integers.He wants to know how many subsequences of length three can be selected from a, so that they form a geometric progression with common ratio k.A subsequence of length three is a combination of three such indexes i1,\u2009i2,\u2009i3, that 1\u2009\u2264\u2009i1\u2009<\u2009i2\u2009<\u2009i3\u2009\u2264\u2009n. That is, a subsequence of length three are such groups of three elements that are not necessarily consecutive in the sequence, but their indexes are strictly increasing.A geometric progression with common ratio k is a sequence of numbers of the form b\u00b7k0,\u2009b\u00b7k1,\u2009...,\u2009b\u00b7kr\u2009-\u20091.Polycarp is only three years old, so he can not calculate this number himself. Help him to do it.", "input_spec": "The first line of the input contains two integers, n and k (1\u2009\u2264\u2009n,\u2009k\u2009\u2264\u20092\u00b7105), showing how many numbers Polycarp's sequence has and his favorite number. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009109\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 elements of the sequence.", "output_spec": "Output a single number \u2014 the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio k.", "sample_inputs": ["5 2\n1 1 2 2 4", "3 1\n1 1 1", "10 3\n1 2 6 2 3 6 9 18 3 9"], "sample_outputs": ["4", "1", "6"], "notes": "NoteIn the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4."}, "positive_code": [{"source_code": "\n\n\n\nmy $f = ;\nchomp ( $f );\nmy ($n, $k) = split m/ /, $f;\n\n$f = ;\nchomp ( $f );\nmy @arr = split m/ /, $f;\n\nmy %total = ();\nmy %curr = ();\n\nfor my $i (@arr) {\n if ( not exists $total { $i } ) {\n\t$total { $i } = 0;\n }\n $total { $i } += 1;\n}\n\n\n\nmy $sum = 0;\nfor ( my $i = 0; $i < (scalar @arr); $i += 1 ) {\n \n if ( $i > 0 and $i < (scalar @arr)-1 ) {\n\n\tif ( $arr[ $i ] % $k == 0 ) {\n\t if ( $k != 1 and $arr[ $i ] != 0 ) {\n\t\t$sum += (($curr { $arr[ $i ] / $k }) * \n\t\t\t ($total { $arr[ $i ] * $k } - $curr { $arr[ $i ] * $k }));\n\t }\n\t else {\n\t\t$sum += (($curr { $arr[ $i ] / $k }) *\n\t\t\t ($total { $arr[ $i ] * $k } - $curr { $arr[ $i ] * $k } - 1));\n\t }\n\t}\n }\n\n if ( not exists $curr { $arr [ $i ] } ) {\n\t$curr { $arr[ $i ] } = 0;\n }\n $curr { $arr[ $i ] } += 1;\n}\n\n\nprint \"$sum \\n\";\n\n\n\n\n"}], "negative_code": [{"source_code": "\n\n\n\n\nmy $f = ;\nchomp ( $f );\nmy ($n, $k) = split m/ /, $f;\n\n$f = ;\nchomp ( $f );\nmy @arr = split m/ /, $f;\n\nmy %total = ();\nmy %curr = ();\n\nfor my $i (@arr) {\n if ( not exists $total { $i } ) {\n\t$total { $i } = 0;\n }\n $total { $i } += 1;\n}\n\n\n\nmy $sum = 0;\nfor ( my $i = 0; $i < (scalar @arr); $i += 1 ) {\n \n if ( $i > 0 and $i < (scalar @arr)-1 ) {\n\n\tif ( $arr[ $i ] % $k == 0 ) {\n\t if ( $k != 1 ) {\n\t\t$sum += (($curr { $arr[ $i ] / $k }) * \n\t\t\t ($total { $arr[ $i ] * $k } - $curr { $arr[ $i ] * $k }));\n\t }\n\t else {\n\t\t$sum += (($curr { $arr[ $i ] / $k }) *\n\t\t\t ($total { $arr[ $i ] * $k } - $curr { $arr[ $i ] * $k } - 1));\n\t }\n\t}\n }\n\n if ( not exists $curr { $arr [ $i ] } ) {\n\t$curr { $arr[ $i ] } = 0;\n }\n $curr { $arr[ $i ] } += 1;\n}\n\n\nprint \"$sum \\n\";\n\n\n\n\n"}], "src_uid": "bd4b3bfa7511410c8e54658cc1dddb46"} {"nl": {"description": "Recently Polycarpus has learned the \"bitwise AND\" operation (which is also called \"AND\") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1,\u2009a2,\u2009...,\u2009an. He also wrote a square matrix b of size n\u2009\u00d7\u2009n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals: the \"bitwise AND\" of numbers ai and aj (that is, bij\u2009=\u2009ai\u00a0&\u00a0aj), if i\u2009\u2260\u2009j; -1, if i\u2009=\u2009j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.Help Polycarpus, given matrix b, restore the sequence of numbers a1,\u2009a2,\u2009...,\u2009an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109.", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1\u2009\u2264\u2009i\u2009\u2264\u2009n) the following condition fulfills: bii = -1. It is guaranteed that for all i,\u2009j (1\u2009\u2264\u2009i,\u2009j\u2009\u2264\u2009n;\u00a0i\u2009\u2260\u2009j) the following condition fulfills: 0\u2009\u2264\u2009bij\u2009\u2264\u2009109, bij\u2009=\u2009bji.", "output_spec": "Print n non-negative integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009109) \u2014 the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.", "sample_inputs": ["1\n-1", "3\n-1 18 0\n18 -1 0\n0 0 -1", "4\n-1 128 128 128\n128 -1 148 160\n128 148 -1 128\n128 160 128 -1"], "sample_outputs": ["0", "18 18 0", "128 180 148 160"], "notes": "NoteIf you do not know what is the \"bitwise AND\" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation."}, "positive_code": [{"source_code": "#!/usr/bin/perl -w\n\nuse strict;\n\nchomp(my $n = <>);\nmy @res = (0) x $n;\nmy @a = ();\nfor my $i (0 .. $n - 1) {\n chomp($_ = <>);\n my @cur = split;\n for my $j (0 .. $n - 1) {\n next if ($i == $j);\n for my $k (0 .. 30) {\n if (($cur[$j] >> $k & 1) == 1) {\n $res[$i] |= 1 << $k;\n $res[$j] |= 1 << $k;\n }\n }\n }\n}\nprint \"@res\\n\";\n"}], "negative_code": [], "src_uid": "8f342e167e77088ce47f17e5cd475b17"} {"nl": {"description": "Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a new set to replenish his beautiful collection of sets.Dr. Evil has his favorite evil integer x. He asks Mahmoud and Ehab to find a set of n distinct non-negative integers such the bitwise-xor sum of the integers in it is exactly x. Dr. Evil doesn't like big numbers, so any number in the set shouldn't be greater than 106.", "input_spec": "The only line contains two integers n and x (1\u2009\u2264\u2009n\u2009\u2264\u2009105, 0\u2009\u2264\u2009x\u2009\u2264\u2009105)\u00a0\u2014 the number of elements in the set and the desired bitwise-xor, respectively.", "output_spec": "If there is no such set, print \"NO\" (without quotes). Otherwise, on the first line print \"YES\" (without quotes) and on the second line print n distinct integers, denoting the elements in the set is any order. If there are multiple solutions you can print any of them.", "sample_inputs": ["5 5", "3 6"], "sample_outputs": ["YES\n1 2 4 5 7", "YES\n1 2 5"], "notes": "NoteYou can read more about the bitwise-xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XORFor the first sample .For the second sample ."}, "positive_code": [{"source_code": "($n, $x) = split \" \", ;\n\nif ($n == 1) {\n\t@ans = ($x);\n}\nelsif ($n == 2) {\n\tif ($x) {\n\t\t$a = (1<<17);\n\t\t@ans = ($a, $a + $x);\n\t}\n}\nelse {\n\t@ans = (1..$n - 3);\n\tfor $i (@ans) {\n\t\t$t ^= $i;\n\t}\n\t$a = $n - 2;\n\tif (($t^$a) == $x) { $a++ }\n\t$b = (1<<17);\n\t$c = $b + ($t^$a^$x);\n\t@ans = (@ans, $a, $b, $c);\n}\n\nif (@ans) {\n\tprint \"YES\\n\";\n\tprint join(\" \", @ans);\n} else {\n\tprint \"NO\";\n}\n"}], "negative_code": [{"source_code": "($n, $x) = split \" \", ;\n\nprint \"YES\\n\";\n\nif ($n == 1) {\n\tprint $x;\n\texit;\n}\n\nfor $i (1..$n - 2) {\n\t$t ^= $i;\n\tprint $i, \" \";\n}\n$a = (1<<17);\n$b = $a + ($t^$x);\nprint $a, \" \", $b;\n"}, {"source_code": "($n, $x) = split \" \", ;\n\nif ($n == 1) {\n\t@ans = ($x);\n}\nelsif ($n == 2) {\n\tif ($x) {\n\t\t$a = (1<<17);\n\t\t@ans = ($a, $a + $x);\n\t}\n}\nelse {\n\t@ans = (1..$n - 3);\n\tfor $i (@ans) {\n\t\t$t ^= $i;\n\t}\n\t$a = $n - 2;\n\tif ($t^$a == $x) { $a++ }\n\t$b = (1<<17);\n\t$c = $b + ($t^$a^$x);\n\t@ans = (@ans, $a, $b, $c);\n}\n\nif (@ans) {\n\tprint \"YES\\n\";\n\tprint join(\" \", @ans);\n} else {\n\tprint \"NO\";\n}\n"}, {"source_code": "($n, $x) = split \" \", ;\n\nif ($n == 1) {\n\tif ($x) {\n\t\t@ans = ($x);\n\t}\n}\nelsif ($n == 2) {\n\tif ($x) {\n\t\t$a = (1<<17);\n\t\t@ans = ($a, $a + $x);\n\t}\n}\nelse {\n\t@ans = (1..$n - 3);\n\tfor $i (@ans) {\n\t\t$t ^= $i;\n\t}\n\t$a = $n - 2;\n\tif ($t^$a == $x) { $a++ }\n\t$b = (1<<17);\n\t$c = $b + ($t^$a^$x);\n\t@ans = (@ans, $a, $b, $c);\n}\n\nif (@ans) {\n\tprint \"YES\\n\";\n\tprint join(\" \", @ans);\n} else {\n\tprint \"NO\";\n}\n"}], "src_uid": "a559171e858d9a63c49fc9e0fecb44c7"} {"nl": {"description": "Monocarp has been collecting rare magazines for quite a while, and now he has decided to sell them. He distributed the magazines between $$$n$$$ boxes, arranged in a row. The $$$i$$$-th box contains $$$a_i$$$ magazines. Some of the boxes are covered with lids, others are not. Suddenly it started to rain, and now Monocarp has to save as many magazines from the rain as possible. To do this, he can move the lids between boxes as follows: if the $$$i$$$-th box was covered with a lid initially, he can either move the lid from the $$$i$$$-th box to the box $$$(i-1)$$$ (if it exists), or keep the lid on the $$$i$$$-th box. You may assume that Monocarp can move the lids instantly at the same moment, and no lid can be moved more than once. If a box will be covered with a lid after Monocarp moves the lids, the magazines in it will be safe from the rain; otherwise they will soak.You have to calculate the maximum number of magazines Monocarp can save from the rain.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u00a0\u2014 the number of the testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$)\u00a0\u2014 the number of boxes. The second line contains a string of $$$n$$$ characters 0 and/or 1. If the $$$i$$$-th character is 1, the $$$i$$$-th box is initially covered with a lid. If the $$$i$$$-th character is 0, the $$$i$$$-th box is initially not covered. The third line contains a sequence of integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^4$$$), where $$$a_i$$$ is the number of magazines in the $$$i$$$-th box. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each testcase, print one integer\u00a0\u2014 the maximum number of magazines Monocarp can save from the rain.", "sample_inputs": ["4\n\n5\n\n01110\n\n10 5 8 9 6\n\n6\n\n011011\n\n20 10 9 30 20 19\n\n4\n\n0000\n\n100 100 100 100\n\n4\n\n0111\n\n5 4 5 1"], "sample_outputs": ["27\n80\n0\n14"], "notes": "NoteIn the first testcase of the example, Monocarp can move the lid from the second box to the first box, so the boxes $$$1$$$, $$$3$$$ and $$$4$$$ are covered, and $$$10 + 8 + 9 = 27$$$ magazines are saved.In the second testcase, Monocarp can move the lid from the second box to the first box, then from the third box to the second box, then from the fifth box to the fourth box, and then from the sixth box to the fifth box. The boxes $$$1$$$, $$$2$$$, $$$4$$$ and $$$5$$$ will be covered, so $$$20 + 10 + 30 + 20 = 80$$$ magazines can be saved.There are no lids in the third testcase, so it's impossible to save even a single magazine."}, "positive_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$_ = 0 . <>;\r\n\t@_ = split ' ', \"0 \" . <>;\r\n\t\r\n\t$Z = 0;\r\n\t\r\n\twhile( /01+/g ){\r\n\t\t@A = sort { $b <=> $a } @_[ $-[ 0 ] .. $+[ 0 ] - 1 ];\r\n\t\tpop @A;\r\n\t\t$Z += $_ for @A;\r\n\t\t}\r\n\t\r\n\tprint $Z;\r\n\t}"}], "negative_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$A = reverse <> =~ y/10/ab/r;\r\n\t\r\n\tprint 0 + eval join '+', \r\n\t\tmap { \r\n\t\t\t@A = sort { $b <=> $a } split /a|b/; \r\n\t\t\tpop @A; @A \r\n\t\t\t}\r\n\t\tmap s/^(?=\\d+a)/0b/r,\r\n\t\t( <> =~ s/\\s/ chop $A /ger ) =~\r\n\t\tm/(?:\\d+b)?(?:\\d+a)+/g;\r\n\t}"}, {"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$A = reverse <> =~ y/10/ab/r;\r\n\t\r\n\tprint 0 + eval join '+', \r\n\t\tmap { \r\n\t\t\t@A = sort { $b <=> $a } split; \r\n\t\t\tpop @A; @A \r\n\t\t\t}\r\n\t\tmap y/ab/ /r,\r\n\t\t( '0b' . <> =~ s/\\s/ chop $A /ger ) =~\r\n\t\tm/(?:\\d+b)(?:\\d+a)+/g;\r\n\t}"}, {"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nwhile(<>){\r\n\t$A = reverse <> =~ y/10/ab/r;\r\n\t\r\n\tprint 0 + eval join '+', \r\n\t\tmap { \r\n\t\t\t@A = sort { $b <=> $a } split /a|b/; \r\n\t\t\tpop @A; @A \r\n\t\t\t}\r\n\t\t( '0b' . <> =~ s/\\s/ chop $A /ger ) =~\r\n\t\tm/(?:\\d+b)(?:\\d+a)+/g;\r\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy $A = reverse <> =~ y/10/ab/r;\n\t$_ = <>;\n\t\n\ts/\\s/ chop $A /ge;\n\t\n\tmy $Z = 0;\n\t\n\t@_ = map y/ab/ /r, map s/^(?=\\d+a)/0b/r, m/(?:\\d+b)?(?:\\d+a)+/g;\n\t\n\tprint 0 + eval join '+', \n\t\tmap { my @A = sort { $b <=> $a } split; pop @A; @A } @_;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy $A = reverse <> =~ y/10/ab/r;\n\t$_ = <>;\n\t\n\ts/\\s/ chop $A /ge;\n\t\n\tmy $Z = 0;\n\t\n\t@_ = map y/ab/ /r, m/(?:\\d+b)?(?:\\d+a)+/g;\n\t\n\tprint 0 + eval join '+', \n\t\tmap { my @A = sort { $b <=> $a } split; pop @A; @A } @_;\n\t}"}], "src_uid": "bcc79164881d9281a6080163dd8a0c91"} {"nl": {"description": "You are given two strings of equal length $$$s$$$ and $$$t$$$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.For example, if $$$s$$$ is \"acbc\" you can get the following strings in one operation: \"aabc\" (if you perform $$$s_2 = s_1$$$); \"ccbc\" (if you perform $$$s_1 = s_2$$$); \"accc\" (if you perform $$$s_3 = s_2$$$ or $$$s_3 = s_4$$$); \"abbc\" (if you perform $$$s_2 = s_3$$$); \"acbb\" (if you perform $$$s_4 = s_3$$$); Note that you can also apply this operation to the string $$$t$$$.Please determine whether it is possible to transform $$$s$$$ into $$$t$$$, applying the operation above any number of times.Note that you have to answer $$$q$$$ independent queries.", "input_spec": "The first line contains one integer $$$q$$$ ($$$1 \\le q \\le 100$$$)\u00a0\u2014 the number of queries. Each query is represented by two 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| \\leq 100$$$, $$$|t| = |s|$$$) 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": ["3\nxabb\naabx\ntechnocup\ntechnocup\na\nz"], "sample_outputs": ["YES\nYES\nNO"], "notes": "NoteIn the first query, you can perform two operations $$$s_1 = s_2$$$ (after it $$$s$$$ turns into \"aabb\") and $$$t_4 = t_3$$$ (after it $$$t$$$ turns into \"aabb\"). In the second query, the strings are equal initially, so the answer is \"YES\".In the third query, you can not make strings $$$s$$$ and $$$t$$$ equal. Therefore, the answer is \"NO\"."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\t\n\tmy %h;\n\t\n\tmap { $h{ $_ } ++ } split //;\n\t\n\tmy $ok = 0;\n\t\n\t$_ = <>;\n\t\n\twhile( /./g ){\n\t\texists $h{ $& } and $ok = 1;\n\t\t}\n\t\n\tprint $ok ? \"YES\" : \"NO\";\n\t}"}], "negative_code": [], "src_uid": "9b9b01e5d2329291eee80356525eaf04"} {"nl": {"description": "You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k > 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) \u00a0\u2014 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$$$ ($$$2 \\le n, m \\le 300$$$) \u00a0\u2014 the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \\le a_{i, j} \\le 10^9$$$). It is guaranteed that the sum of $$$n \\cdot m$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "If it is impossible to obtain a good grid, print a single line containing \"NO\". Otherwise, print a single line containing \"YES\", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.", "sample_inputs": ["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"], "sample_outputs": ["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"], "notes": "NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\\;1\\;0\\;0$$$$$$ $$$$$$0\\;2\\;1\\;0$$$$$$ $$$$$$0\\;0\\;0\\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\tmy @ans;\n\t\n\tmy $ok = 1;\n\t\n\tfor my $i ( 1 .. $n ){\n\t\t@_ = split ' ', <>;\n\t\t\n\t\tmy @A = ( 3, ( 4 ) x ( $m - 2 ), 3 );\n\t\t\n\t\tif( $i == 1 || $i == $n ){\n\t\t\tmap $_ -= 1, @A;\n\t\t\t}\n\t\t\n\t\tfor my $j ( 0 .. $m - 1 ){\n\t\t\t$_[ $j ] > $A[ $j ] and $ok = 0;\n\t\t\tlast if not $ok;\n\t\t\t}\n\t\t\n\t\tpush @ans, [ @A ];\n\t\t}\n\t\n\tif( $ok ){\n\t\tprint \"YES\";\n\t\tprint \"@{ $_ }\" for @ans;\n\t\t}\n\telse{\n\t\tprint \"NO\";\n\t\t}\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\tmy @ans;\n\t\n\tmy $ok = 1;\n\t\n\tfor my $i ( 1 .. $n ){\n\t\t@_ = split ' ', <>;\n\t\t\n\t\tmy @A = ( 3, ( 4 ) x ( $m - 2 ), 3 );\n\t\t\n\t\tif( $i == 1 || $i == $n ){\n\t\t\tmap $_ -= 2 , @A;\n\t\t\t}\n\t\t\n\t\tfor my $j ( 0 .. $m - 1 ){\n\t\t\t$_[ $j ] > $A[ $j ] and $ok = 0;\n\t\t\tlast if not $ok;\n\t\t\t}\n\t\t\n\t\tlast if not $ok;\n\t\t\n\t\tpush @ans, [ @A ];\n\t\t}\n\t\n\tif( $ok ){\n\t\tprint \"YES\";\n\t\tprint \"@{ $_ }\" for @ans;\n\t\t}\n\telse{\n\t\tprint \"NO\";\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\tmy @ans;\n\t\n\tmy $ok = 1;\n\t\n\tfor my $i ( 1 .. $n ){\n\t\t@_ = split ' ', <>;\n\t\t\n\t\tmy @A = ( 3, ( 4 ) x ( $m - 2 ), 3 );\n\t\t\n\t\tif( $i == 1 || $i == $n ){\n\t\t\tmap $_ -= 1, @A;\n\t\t\t}\n\t\t\n\t\tfor my $j ( 0 .. $m - 1 ){\n\t\t\t$_[ $j ] > $A[ $j ] and $ok = 0;\n\t\t\tlast if not $ok;\n\t\t\t}\n\t\t\n\t\tlast if not $ok;\n\t\t\n\t\tpush @ans, [ @A ];\n\t\t}\n\t\n\tif( $ok ){\n\t\tprint \"YES\";\n\t\tprint \"@{ $_ }\" for @ans;\n\t\t}\n\telse{\n\t\tprint \"NO\";\n\t\t}\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m ) = split;\n\t\n\tmy @ans;\n\t\n\tmy $ok = 1;\n\t\n\tfor my $i ( 1 .. $n ){\n\t\t@_ = split ' ', <>;\n\t\t\n\t\tmy $center = ( $i == 1 || $i == $n ) ? 3 : 4;\n\t\t\n\t\tmy @A = ( 3, ( $center ) x ( $m - 2 ), 3 );\n\t\t\n\t\tfor my $j ( 0 .. $m - 1 ){\n\t\t\t$_[ $j ] > $A[ $j ] and $ok = 0;\n\t\t\tlast if not $ok;\n\t\t\t}\n\t\t\n\t\tlast if not $ok;\n\t\t\n\t\tpush @ans, [ @A ];\n\t\t}\n\t\n\tif( $ok ){\n\t\tprint \"YES\";\n\t\tprint \"@{ $_ }\" for @ans;\n\t\t}\n\telse{\n\t\tprint \"NO\";\n\t\t}\n\t}"}], "src_uid": "8afcdfaabba66fb9cefc5b6ceabac0d0"} {"nl": {"description": "Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming.Little Tommy has n lanterns and Big Banban has m lanterns. Tommy's lanterns have brightness a1,\u2009a2,\u2009...,\u2009an, and Banban's have brightness b1,\u2009b2,\u2009...,\u2009bm respectively.Tommy intends to hide one of his lanterns, then Banban picks one of Tommy's non-hidden lanterns and one of his own lanterns to form a pair. The pair's brightness will be the product of the brightness of two lanterns.Tommy wants to make the product as small as possible, while Banban tries to make it as large as possible.You are asked to find the brightness of the chosen pair if both of them choose optimally.", "input_spec": "The first line contains two space-separated integers n and m (2\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200950). The second line contains n space-separated integers a1,\u2009a2,\u2009...,\u2009an. The third line contains m space-separated integers b1,\u2009b2,\u2009...,\u2009bm. All the integers range from \u2009-\u2009109 to 109.", "output_spec": "Print a single integer\u00a0\u2014 the brightness of the chosen pair.", "sample_inputs": ["2 2\n20 18\n2 14", "5 3\n-1 0 1 2 3\n-1 0 1"], "sample_outputs": ["252", "2"], "notes": "NoteIn the first example, Tommy will hide 20 and Banban will choose 18 from Tommy and 14 from himself.In the second example, Tommy will hide 3 and Banban will choose 2 from Tommy and 1 from himself."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\t\n\tmy @M;\n\t\t\n\tfor $i ( @A ){\n\t\t$max = -~0;\n\t\tfor $j ( @B ){\n\t\t\t$max < $j * $i and $max = $j * $i;\n\t\t\t}\n\t\tpush @M, $max;\n\t\t}\n\t\n\tprint +( sort { $a <=> $b } @M )[ -2 ]\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\t\n\tmy $max = -~0;\n\tmy $min = ~0;\n\tmy $M;\n\tmy $MM;\n\t\n\tfor my $i ( 0 .. @A - 1 ){\n\t\tmy $max = -~0;\n\t\tmy $M;\n\t\tfor my $j ( @A[ 0 .. $i - 1, $i + 1 .. @A - 1 ] ){\n\t\t\tfor my $k ( @B ){\n\t\t\t#\tprint \" $i $j $k\";\n\t\t\t\t$max < $j * $k and do { $max = $j * $k; $M = \"$j $k\" };\n\t\t\t\t}\n\t\t\t}\n\t\t$min > $max and do { $min = $max; $MM = $M };\n\t\t}\n\t\n\tuse bigint;\n\tprint 0 + eval join ' * ', split ' ', $MM;\n\tno bigint;\n\t}"}, {"source_code": "<>;\n\n@A = split ' ', <>;\n@B = split ' ', <>;\n\t\nfor $i ( @A ){\n\tpush @M, ( sort { $b <=> $a } map $i * $_, @B )[ 0 ]\n\t}\n\nprint +( sort { $a <=> $b } @M )[ -2 ]"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\t\n\tmy $max = -~0;\n\tmy $min = ~0;\n\tmy $M;\n\tmy @M;\n\t\n\t$debug and print \"$max|$min\";\n\t\n\tfor my $i ( @A ){\n\t\tmy $max = -~0;\n\t\tfor my $j ( @B ){\n\t\t\t$debug and print \" $i $j | \", $j * $i;\n\t\t\t$max < $j * $i and do { $max = $j * $i; $M = \"$j $i\" };\n\t\t\t}\n\t\tpush @M, [ $max, $M ];\n\t\t}\n\t\n\tuse bigint;\n\tprint 0 + eval join ' * ', split ' ', join '', map $_->[ 1 ], ( sort { $a->[ 0 ] <=> $b->[ 0 ] } @M )[ -2 ];\n\tno bigint;\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\t\n\tmy $max = -~0;\n\tmy $min = ~0;\n\tmy $M;\n\tmy @M;\n\t\n\t$debug and print \"$max|$min\";\n\t\n\tfor my $i ( @A ){\n\t\tmy $max = -~0;\n\t\tfor my $j ( @B ){\n\t\t\t$debug and print \" $i $j | \", $j * $i;\n\t\t\t$max < $j * $i and do { $max = $j * $i; $M = \"$j $i\" };\n\t\t\t}\n\t\tpush @M, [ $max, $M ];\n\t\t}\n\t\n#\tuse bigint;\n\tprint 0 + eval join ' * ', split ' ', join '', map $_->[ 1 ], ( sort { $a->[ 0 ] <=> $b->[ 0 ] } @M )[ -2 ];\n#\tno bigint;\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\t\n\tmy $max = -~0;\n\tmy $min = ~0;\n\tmy $M;\n\tmy $MM;\n\t\n\tfor my $i ( 0 .. @A - 1 ){\n\t\tmy $max = -~0;\n\t\tmy $M;\n\t\tfor my $j ( @A[ 0 .. $i - 1, $i + 1 .. @A - 1 ] ){\n\t\t\tfor my $k ( @B ){\n\t\t\t\tprint \" $i $j $k\";\n\t\t\t\t$max < $j * $k and do { $max = $j * $k; $M = \"$j $k\" };\n\t\t\t\t}\n\t\t\t}\n\t\t$min > $max and do { $min = $max; $MM = $M };\n\t\t}\n\t\n\tuse bigint;\n\tprint 0 + eval join ' * ', split ' ', $MM;\n\tno bigint;\n\t}"}], "src_uid": "c408b1d198c7c88fc635936d960c962a"} {"nl": {"description": "An undirected graph is called k-regular, if the degrees of all its vertices are equal k. An edge of a connected graph is called a bridge, if after removing it the graph is being split into two connected components.Build a connected undirected k-regular graph containing at least one bridge, or else state that such graph doesn't exist.", "input_spec": "The single line of the input contains integer k (1\u2009\u2264\u2009k\u2009\u2264\u2009100) \u2014 the required degree of the vertices of the regular graph.", "output_spec": "Print \"NO\" (without quotes), if such graph doesn't exist. Otherwise, print \"YES\" in the first line and the description of any suitable graph in the next lines. The description of the made graph must start with numbers n and m \u2014 the number of vertices and edges respectively. Each of the next m lines must contain two integers, a and b (1\u2009\u2264\u2009a,\u2009b\u2009\u2264\u2009n, a\u2009\u2260\u2009b), that mean that there is an edge connecting the vertices a and b. A graph shouldn't contain multiple edges and edges that lead from a vertex to itself. A graph must be connected, the degrees of all vertices of the graph must be equal k. At least one edge of the graph must be a bridge. You can print the edges of the graph in any order. You can print the ends of each edge in any order. The constructed graph must contain at most 106 vertices and 106 edges (it is guaranteed that if at least one graph that meets the requirements exists, then there also exists the graph with at most 106 vertices and at most 106 edges). ", "sample_inputs": ["1"], "sample_outputs": ["YES\n2 1\n1 2"], "notes": "NoteIn the sample from the statement there is a suitable graph consisting of two vertices, connected by a single edge."}, "positive_code": [{"source_code": "$deg=<>;\nprint \"YES\\n2 1\\n1 2\" and exit if($deg==1);\nprint \"NO\" and exit if($deg%2==0);\n$n=($deg+2)*2; $m=($deg+2)*$deg; $cnt=($deg-1)/2;\nprint \"YES\\n$n $m\\n\";\nfor($i=1;$i<=$deg+1;$i++){\n for($j=$i+1;$j<=$deg+1;$j++){\n if($cnt>0 && !$tag){\n $cnt--; $tag=2;\n push @stack,($i,$j);\n next;\n }\n print \"$i $j\\n\";\n $_i=$n+1-$i;\n $_j=$n+1-$j;\n print \"$_i $_j\\n\";\n }\n $tag--;\n}\n$mid=$deg+2; $mid2=$n+1-$mid;\nprint \"$mid $mid2\\n\";\nwhile(@stack){\n $a = pop @stack;\n $b = pop @stack;\n print \"$a $mid\\n$b $mid\\n\";\n $a=$n+1-$a; $b=$n+1-$b;\n print \"$a $mid2\\n$b $mid2\\n\";\n}"}, {"source_code": "$deg=<>;\nprint \"YES\\n2 1\\n1 2\" and exit if($deg==1);\nprint \"NO\" and exit if($deg%2==0);\n$n=($deg+2)*2; $m=($deg+2)*$deg; $cnt=($deg-1)/2;\nprint \"YES\\n$n $m\\n\";\nfor($i=1;$i<=$deg+1;$i++){\n for($j=$i+1;$j<=$deg+1;$j++){\n if($cnt>0 && !$tag){\n $cnt--; $tag=2;\n push @stack,($i,$j) and next;\n }\n print \"$i $j\\n\";\n ($_i,$_j)=wrp($i,$j);\n print \"$_i $_j\\n\";\n }\n $tag--;\n}\n$mid=$deg+2; $mid2=$n+1-$mid;\nprint \"$mid $mid2\\n\";\nwhile(@stack){\n $a = pop @stack;\n $b = pop @stack;\n print \"$a $mid\\n$b $mid\\n\";\n ($a,$b)=wrp($a,$b);\n print \"$a $mid2\\n$b $mid2\\n\";\n}\nsub wrp{return ($n+1-@_[0],$n+1-@_[1]);}"}, {"source_code": "$deg=<>;\nif($deg==1){\n print \"YES\\n2 1\\n1 2\" and exit;\n}\nif($deg%2==0){\n print \"NO\" and exit;\n}\n$n=($deg+2)*2;\n$m=($deg+2)*$deg;\n$cnt=($deg-1)/2;\nprint \"YES\\n$n $m\\n\";\nfor($i=1;$i<=$deg+1;$i++){\n for($j=$i+1;$j<=$deg+1;$j++){\n if($cnt>0 && !$tag){\n $cnt--;\n push @stack,($i,$j);\n $tag=2;\n next;\n }\n print \"$i $j\\n\";\n $_i=$n+1-$i;\n $_j=$n+1-$j;\n print \"$_i $_j\\n\";\n }\n $tag--;\n}\n$mid=$deg+2; $mid2=$n+1-$mid;\nprint \"$mid $mid2\\n\";\nwhile(@stack){\n $a = pop @stack;\n $b = pop @stack;\n print \"$a $mid\\n$b $mid\\n\";\n $a=$n+1-$a; $b=$n+1-$b;\n print \"$a $mid2\\n$b $mid2\\n\";\n}"}], "negative_code": [{"source_code": "$deg=<>;\nif($deg==1){\n print \"YES\\n2 1\\n1 2\" and exit;\n}\nif($deg%2==0){\n print \"NO\" and exit;\n}\n$n=($deg+2)*2;\n$m=($deg+2)*$deg;\n$cnt=($deg-1)/2;\nprint \"YES\\n$n $m\\n\";\nfor($i=1;$i<=$deg+1;$i++){\n for($j=$i+1;$j<=$deg+1;$j++){\n if($cnt>0){\n $cnt--;\n push @stack,($i,$j);\n next;\n }\n print \"$i $j\\n\";\n $_i=$n+1-$i;\n $_j=$n+1-$j;\n print \"$_i $_j\\n\";\n }\n}\n$mid=$deg+2; $mid2=$n+1-$mid;\nprint \"$mid $mid2\\n\";\nwhile(@stack){\n $a = pop @stack;\n $b = pop @stack;\n print \"$a $mid\\n$b $mid\\n\";\n $a=$n+1-$a; $b=$n+1-$b;\n print \"$a $mid2\\n$b $mid2\\n\";\n}"}, {"source_code": "$deg=<>;\nif($deg==1){\n print \"YES\\n2 1\\n1 2\" and exit;\n}\nif($deg%2==0){\n print \"NO\" and exit;\n}\n$n=($deg+2)*2;\n$m=($deg+2)*$deg;\nprint \"YES\\n$n $m\\n\";\nfor($i=1;$i<=$deg+1;$i++){\n for($j=$i+1;$j<=$deg+1;$j++){\n next if($i==1 && $j==2);\n print \"$i $j\\n\";\n $_i=$n+1-$i;\n $_j=$n+1-$j;\n print \"$_i $_j\\n\";\n }\n}\n$l=1; $r=2; $mid=$deg+2;\nprint \"$l $mid\\n$mid $r\\n\";\n$mid2=$n+1-$mid;\nprint \"$mid $mid2\\n\";\n$l=$n+1-$l; $r=$n+1-$r;\nprint \"$l $mid2\\n$mid2 $r\\n\";"}], "src_uid": "1e061d8c4bff217047ddc58e88be0c3f"} {"nl": {"description": "A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not.You are given a binary string $$$s$$$ of length $$$n$$$. Construct two balanced bracket sequences $$$a$$$ and $$$b$$$ of length $$$n$$$ such that for all $$$1\\le i\\le n$$$: if $$$s_i=1$$$, then $$$a_i=b_i$$$ if $$$s_i=0$$$, then $$$a_i\\ne b_i$$$ If it is impossible, you should report about it.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 10^4$$$) \u2014 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$$$, $$$n$$$ is even). The next line contains a string $$$s$$$ of length $$$n$$$, consisting of characters 0 and 1. The sum of $$$n$$$ across all test cases does not exceed $$$2\\cdot 10^5$$$.", "output_spec": "If such two balanced bracked sequences exist, output \"YES\" on the first line, otherwise output \"NO\". You can print each letter in any case (upper or lower). If the answer is \"YES\", output the balanced bracket sequences $$$a$$$ and $$$b$$$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any.", "sample_inputs": ["3\n6\n101101\n10\n1001101101\n4\n1100"], "sample_outputs": ["YES\n()()()\n((()))\nYES\n()()((()))\n(())()()()\nNO"], "notes": "NoteIn the first test case, $$$a=$$$\"()()()\" and $$$b=$$$\"((()))\". The characters are equal in positions $$$1$$$, $$$3$$$, $$$4$$$, and $$$6$$$, which are the exact same positions where $$$s_i=1$$$.In the second test case, $$$a=$$$\"()()((()))\" and $$$b=$$$\"(())()()()\". The characters are equal in positions $$$1$$$, $$$4$$$, $$$5$$$, $$$7$$$, $$$8$$$, $$$10$$$, which are the exact same positions where $$$s_i=1$$$.In the third test case, there is no solution."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n ( my $s = ) =~ s/[\\x00-\\x20]+$//ios;\r\n if( $n % 2 == 1 or substr($s,0,1) eq '0' or substr($s,$n-1,1) eq '0' ){\r\n print \"NO\\n\"; next;\r\n }\r\n my $c0 = 0;\r\n my @c0r = (); $#c0r = $n - 1;\r\n for(my $i=$n-1;$i>=0;$i--){\r\n if( substr($s,$i,1) eq '0' ){\r\n $c0++;\r\n }\r\n $c0r[$i] = $c0;\r\n }\r\n if( $c0 % 2 == 1 ){\r\n print \"NO\\n\"; next;\r\n }\r\n my @d = (0,0);\r\n \r\n my @o1 = (); $#o1 = $n-1;\r\n my @o2 = (); $#o2 = $n-1;\r\n \r\n my $res = 'YES';\r\n for(my $i=0;$i<$n;$i++){\r\n my $b1 = substr($s,$i,1);\r\n if( $b1 eq '1' ){\r\n # \ufffd\u30b0\ufffd\u90a9\ufffd\ufffd\ufffd\ufffd\ufffd\u90a9\r\n my $v1 = 2 * int($c0r[$i] / 2);\r\n if( $n - $i - $v1 <= $d[0] ){\r\n # \ufffd\ufffd\ufffd~\r\n $o1[$i] = ')';\r\n $o2[$i] = ')';\r\n $d[0] --;\r\n $d[1] --;\r\n } else {\r\n $o1[$i] = '(';\r\n $o2[$i] = '(';\r\n $d[0] ++;\r\n $d[1] ++;\r\n }\r\n } else {\r\n if( $d[0] != $d[1] ){\r\n # \ufffd\ufffd\ufffd\ufffd\r\n $o1[$i] = ')';\r\n $o2[$i] = '(';\r\n $d[0] --;\r\n $d[1] ++;\r\n } else {\r\n $o1[$i] = '(';\r\n $o2[$i] = ')';\r\n $d[0] ++;\r\n $d[1] --;\r\n }\r\n }\r\n if( $d[1]<0 ){\r\n $res = 'NO'; last;\r\n }\r\n }\r\n print \"$res\\n\";\r\n if( $res eq 'YES' ){\r\n my $o1 = join('',@o1);\r\n my $o2 = join('',@o2);\r\n print \"$o1\\n\";\r\n print \"$o2\\n\";\r\n }\r\n}\r\n\r\nexit(0);\r\n\r\n"}], "negative_code": [], "src_uid": "f2aedd618eae5c51386525b1f76b19a6"} {"nl": {"description": "One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.", "input_spec": "The first input line contains a single integer n \u2014 the number of cupboards in the kitchen (2\u2009\u2264\u2009n\u2009\u2264\u2009104). Then follow n lines, each containing two integers li and ri (0\u2009\u2264\u2009li,\u2009ri\u2009\u2264\u20091). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero. The numbers in the lines are separated by single spaces.", "output_spec": "In the only output line print a single integer t \u2014 the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.", "sample_inputs": ["5\n0 1\n1 0\n0 1\n1 1\n0 1"], "sample_outputs": ["3"], "notes": null}, "positive_code": [{"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Tue Nov 27 15:24:24 IST 2012\n# File Name: a.pl\n# USAGE: \n# a.pl \n# \n# \n#------------------------------------------------\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nchomp (my $n = );\nmy ($a, $b, @l, @r);\n\n@r = @l = (0, 0);\n\nwhile ($n--) {\n chomp ( ($a, $b) = split /\\s+/, );\n $l[$a]++;\n $r[$b]++;\n}\n\nprint &min (@l) + &min (@r);\n"}, {"source_code": "$tmp=<>;\n$sum1=0;\n$sum2=0;\nsub max\n{\n my $t1=shift;\n my $t2=shift;\n if($t1<$t2){$t1;}\n else\n {$t2;}\n}\nforeach (1...$tmp)\n{\n $ok=<>;\n ($a,$b)=split / /,$ok;\n $sum1+=$a;\n $sum2+=$b;\n}\n$sum1=max($sum1,$tmp-$sum1);\n$sum2=max($sum2,$tmp-$sum2);\nprint $sum1+$sum2,\"\\n\";\n"}, {"source_code": "$tmp=<>;\n$sum1=0;\n$sum2=0;\nforeach (1...$tmp)\n{\n $ok=<>;\n ($a,$b)=split / /,$ok;\n $sum1+=$a;\n $sum2+=$b;\n}\nif(2*$sum1>$tmp){$sum1=$tmp-$sum1;}\nif(2*$sum2>$tmp){$sum2=$tmp-$sum2;}\nprint $sum1+$sum2,\"\\n\";\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$n=<>;\nwhile(<>=~/ /)\n{\n\t$` and $i++;\n\t$'+0 and $j++;\n\t}\nprint (min($i,$n-$i)+min($j,$n-$j));\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "negative_code": [{"source_code": "#! /usr/bin/perl -wl\n#------------------------------------------------\n# Author: vj\n# Created: Tue Nov 27 15:24:24 IST 2012\n# File Name: a.pl\n# USAGE: \n# a.pl \n# \n# \n#------------------------------------------------\nuse List::Util qw(first max maxstr min minstr reduce shuffle sum);\n\nchomp (my $n = );\nmy ($a, $b, @l, @r);\n\nwhile ($n--) {\n chomp ( ($a, $b) = split /\\s+/, );\n $l[$a]++;\n $r[$b]++;\n}\n\nprint &min (@l) + &min (@r);\n"}, {"source_code": "#!/usr/bin/perl\n\n$PI=3.14159265358979323846;\n$MOD=1000000007;\n\n# <>=~/ /;\n# <>=~/ (\\d+) /;\n# <>=~/ (\\d+) (\\d+) /;\n\n$n=<>;\nwhile(<>=~/ /)\n{\n\t$` and $i++;\n\t$' and $j++;\n\t}\nprint (min($i,$n-$i)+min($j,$n-$j));\n\n#***********SUBS************\n\nsub num {$a <=> $b}\nsub num_rev {$b <=> $a}\nsub rev {$b cmp $a}\n\nsub p{print}\nsub n{print \"\\n\"}\nsub pn{p,n}\nsub pn_mas {pn for @_}\n\n# Transpose \"charbox\"\nsub transpose{\n $i=@m=@_;\n chomp @m;\n while ($i--){\n $t[$i]=~s/$/chop/e for @m\n }\nreturn @t\n}\n\n# Transpose \"value sets\"\nsub transpose_space{\n my @t;\n for (@_){\n my $j=0;\n s/\\d+/($t[$j++].=\"$& \")?$&:$&/eg;\n }\n chop @t;\nreturn @t\n}\n\nsub div{\n my ($n, $k)=@_;\nreturn ($n - $n % $k) / $k\n}\n \nsub uniq{\n my @m=sort @_;\n push @u, my $i=shift @m;\n for (@m){\n $i eq $_ or push @u, $_;\n $i=$_;\n }\nreturn @u\n}\n \nsub max{\n my $max=shift;\n $max<$_ and $max=$_ for @_;\nreturn $max\n}\n \nsub min{\n my $min=shift;\n $min>$_ and $min=$_ for @_;\nreturn $min\n}\n\n# count (kas_ieskoma, masyvas)\nsub count{\n my $i=0;\n $_ eq $_[0] and $i++ for @_;\nreturn --$i\n}"}], "src_uid": "2052b0b4abdcf7d613b56842b1267f60"} {"nl": {"description": "The Resistance is trying to take control over all planets in a particular solar system. This solar system is shaped like a tree. More precisely, some planets are connected by bidirectional hyperspace tunnels in such a way that there is a path between every pair of the planets, but removing any tunnel would disconnect some of them.The Resistance already has measures in place that will, when the time is right, enable them to control every planet that is not remote. A planet is considered to be remote if it is connected to the rest of the planets only via a single hyperspace tunnel.How much work is there left to be done: that is, how many remote planets are there?", "input_spec": "The first line of the input contains an integer N (2\u2009\u2264\u2009N\u2009\u2264\u20091000) \u2013 the number of planets in the galaxy. The next N\u2009-\u20091 lines describe the hyperspace tunnels between the planets. Each of the N\u2009-\u20091 lines contains two space-separated integers u and v (1\u2009\u2264\u2009u,\u2009v\u2009\u2264\u2009N) indicating that there is a bidirectional hyperspace tunnel between the planets u and v. It is guaranteed that every two planets are connected by a path of tunnels, and that each tunnel connects a different pair of planets.", "output_spec": "A single integer denoting the number of remote planets.", "sample_inputs": ["5\n4 1\n4 2\n1 3\n1 5", "4\n1 2\n4 3\n1 4"], "sample_outputs": ["3", "2"], "notes": "NoteIn the first example, only planets 2, 3 and 5 are connected by a single tunnel.In the second example, the remote planets are 2 and 3.Note that this problem has only two versions \u2013 easy and medium."}, "positive_code": [{"source_code": "$/ = undef; $_ = <>; ($n, @a) = split;\n$b[$_]++ for @a;\nprint 0 + grep $_ == 1, @b;"}], "negative_code": [], "src_uid": "5d91e27798d38fc7924d1c407c07c99c"} {"nl": {"description": "You are given a positive integer $$$n$$$.The weight of a permutation $$$p_1, p_2, \\ldots, p_n$$$ is the number of indices $$$1\\le i\\le n$$$ such that $$$i$$$ divides $$$p_i$$$. Find a permutation $$$p_1,p_2,\\dots, p_n$$$ with the minimum possible weight (among all permutations of length $$$n$$$).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 \\leq t \\leq 10^4$$$). The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) \u2014 the length of permutation. 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 line containing $$$n$$$ integers $$$p_1, p_2,\\dots, p_n$$$ so that the permutation $$$p$$$ has the minimum possible weight. If there are several possible answers, you can print any of them.", "sample_inputs": ["2\n\n1\n\n4"], "sample_outputs": ["1\n2 1 4 3"], "notes": "NoteIn the first test case, the only valid permutation is $$$p=[1]$$$. Its weight is $$$1$$$.In the second test case, one possible answer is the permutation $$$p=[2,1,4,3]$$$. One can check that $$$1$$$ divides $$$p_1$$$ and $$$i$$$ does not divide $$$p_i$$$ for $$$i=2,3,4$$$, so the weight of this permutation is $$$1$$$. It is impossible to find a permutation of length $$$4$$$ with a strictly smaller weight."}, "positive_code": [{"source_code": "<>;for(<>){print;print\"$_ \"for(1..$_-1)}"}, {"source_code": "for(1..<>){print\"$_ \"for(2..<>);print\"1 \"}"}, {"source_code": "<>;for(<>){for$i(0..$_-1){print$i||$_,\" \"}}"}, {"source_code": "<>;for(<>){print;print\"$_ \"for(1..$_-1)}"}, {"source_code": "#!/usr/bin/perl\nuse Data::Dumper;\nuse integer; ### important!\nmy $mod = 10 ** 9 + 7;\n# my $mod = 998244353;\n\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\n\nwhile( $testcase -- > 0 ){\n \n my ($n) = map { $_ - 0 } split(/\\s+/,);\n \n my @a = ( ($n) , ( 1 .. ($n-1) ));\n print ( join(' ',@a) . \"\\n\" );\n \n}\n\nexit(0);\n\nsub max {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\n return $r;\n}\nsub min {\n my $r = undef;\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\n return $r;\n}\nsub sum {\n my $r = 0;\n foreach my $e (@_){ $r += $e; }\n return $r;\n}\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\n my $r = shift; my $ar = shift;\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\n for(my $i=0;$i<=$#ar;$i++){ \n $rr += $ar->[$i];\n $r->[1+$i] = $rr;\n }\n}\n"}], "negative_code": [{"source_code": "<>;for(<>){print;print for(1..$_-1)}"}], "src_uid": "955bc1e2769ea407ef02506bf1e4259b"} {"nl": {"description": "Even if it's a really easy question, she won't be able to answer it\u2014 Perfect Memento in Strict SenseCirno's perfect bitmasks classroom has just started!Cirno gave her students a positive integer $$$x$$$. As an assignment, her students need to find the minimum positive integer $$$y$$$, which satisfies the following two conditions:$$$$$$x\\ \\texttt{and}\\ y > 0$$$$$$ $$$$$$x\\ \\texttt{xor}\\ y > 0$$$$$$Where $$$\\texttt{and}$$$ is the bitwise AND operation, and $$$\\texttt{xor}$$$ is the bitwise XOR operation.Among the students was Mystia, who was truly baffled by all these new operators. Please help her!", "input_spec": "The first line of input contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^3$$$) \u2014 the number of input test cases. For each test case, the only line of input contains one integer $$$x$$$ ($$$1 \\leq x \\leq 2^{30}$$$).", "output_spec": "For each test case, print a single integer \u2014 the minimum number of $$$y$$$.", "sample_inputs": ["7\n\n1\n\n2\n\n5\n\n9\n\n16\n\n114514\n\n1000000"], "sample_outputs": ["3\n3\n1\n1\n17\n2\n64"], "notes": "NoteTest case 1: $$$1\\; \\texttt{and}\\; 3=1>0$$$, $$$1\\; \\texttt{xor}\\; 3=2>0$$$.Test case 2: $$$2\\; \\texttt{and}\\; 3=2>0$$$, $$$2\\; \\texttt{xor}\\; 3=1>0$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($testcase) = map { $_ - 0 } split(/\\s+/,);\r\n\r\nwhile( $testcase -- > 0 ){\r\n \r\n my ($x) = map { $_ - 0 } split(/\\s+/,);\r\n \r\n my $c1 = 0; my $m1 = 32; my $m0 = 32;\r\n for(my $i=31;$i>=0;$i--){\r\n my $z = (1<<$i);\r\n if( $x & $z ){\r\n $m1 = &min($m1,$i);\r\n $c1++;\r\n } else {\r\n $m0 = &min($m0,$i);\r\n }\r\n }\r\n my $rc = 0;\r\n if( $c1 > 1 ){\r\n $rc = (1<<$m1);\r\n } else {\r\n $rc = (1<<$m1) + (1<<$m0);\r\n }\r\n print \"$rc\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub max {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e > $r; }\r\n return $r;\r\n}\r\nsub min {\r\n my $r = undef;\r\n foreach my $e (@_){ $r = $e if !defined($r) or $e < $r; }\r\n return $r;\r\n}\r\nsub sum {\r\n my $r = 0;\r\n foreach my $e (@_){ $r += $e; }\r\n return $r;\r\n}\r\nsub ruisum { # my @rui = (0); &ruisum(\\@rui,\\@array);\r\n my $r = shift; my $ar = shift;\r\n $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;\r\n for(my $i=0;$i<=$#ar;$i++){ \r\n $rr += $ar->[$i];\r\n $r->[1+$i] = $rr;\r\n }\r\n}\r\n"}], "negative_code": [], "src_uid": "288b36b64473326ea434dc74f05ae456"} {"nl": {"description": "It is winter now, and Max decided it's about time he watered the garden.The garden can be represented as n consecutive garden beds, numbered from 1 to n. k beds contain water taps (i-th tap is located in the bed xi), which, if turned on, start delivering water to neighbouring beds. If the tap on the bed xi is turned on, then after one second has passed, the bed xi will be watered; after two seconds have passed, the beds from the segment [xi\u2009-\u20091,\u2009xi\u2009+\u20091] will be watered (if they exist); after j seconds have passed (j is an integer number), the beds from the segment [xi\u2009-\u2009(j\u2009-\u20091),\u2009xi\u2009+\u2009(j\u2009-\u20091)] will be watered (if they exist). Nothing changes during the seconds, so, for example, we can't say that the segment [xi\u2009-\u20092.5,\u2009xi\u2009+\u20092.5] will be watered after 2.5 seconds have passed; only the segment [xi\u2009-\u20092,\u2009xi\u2009+\u20092] will be watered at that moment. The garden from test 1. White colour denotes a garden bed without a tap, red colour \u2014 a garden bed with a tap. The garden from test 1 after 2 seconds have passed after turning on the tap. White colour denotes an unwatered garden bed, blue colour \u2014 a watered bed. Max wants to turn on all the water taps at the same moment, and now he wonders, what is the minimum number of seconds that have to pass after he turns on some taps until the whole garden is watered. Help him to find the answer!", "input_spec": "The first line contains one integer t \u2014 the number of test cases to solve (1\u2009\u2264\u2009t\u2009\u2264\u2009200). Then t test cases follow. The first line of each test case contains two integers n and k (1\u2009\u2264\u2009n\u2009\u2264\u2009200, 1\u2009\u2264\u2009k\u2009\u2264\u2009n) \u2014 the number of garden beds and water taps, respectively. Next line contains k integers xi (1\u2009\u2264\u2009xi\u2009\u2264\u2009n) \u2014 the location of i-th water tap. It is guaranteed that for each condition xi\u2009-\u20091\u2009<\u2009xi holds. It is guaranteed that the sum of n over all test cases doesn't exceed 200. Note that in hacks you have to set t\u2009=\u20091.", "output_spec": "For each test case print one integer \u2014 the minimum number of seconds that have to pass after Max turns on some of the water taps, until the whole garden is watered.", "sample_inputs": ["3\n5 1\n3\n3 3\n1 2 3\n4 1\n1"], "sample_outputs": ["3\n1\n4"], "notes": "NoteThe first example consists of 3 tests: There are 5 garden beds, and a water tap in the bed 3. If we turn it on, then after 1 second passes, only bed 3 will be watered; after 2 seconds pass, beds [1,\u20093] will be watered, and after 3 seconds pass, everything will be watered. There are 3 garden beds, and there is a water tap in each one. If we turn all of them on, then everything will be watered after 1 second passes. There are 4 garden beds, and only one tap in the bed 1. It will take 4 seconds to water, for example, bed 4. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n<>;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\tmy $x = 'x' x $n;\n\t\n\t@_ = split ' ', <>;\n\t\n\tsubstr $x, $_ - 1, 1, 'o' for @_;\n\t\n\t$debug and print $x;\n\t\n\tmy $i = 1;\n\t\n\twhile( $x =~ /x/ ){\n\t\t$x =~ s/x(?=o)|(?<=o)x/o/g;\n\t\t$debug and print \" $x\";\n\t\t$i ++;\n\t\t}\n\t\n\tprint $i;\n\t}"}, {"source_code": "<>;\n\nwhile( <> ){\n\n$x = 'x' x $_;\n\n@_ = split ' ', <>;\n\nsubstr $x, $_ - 1, 1, 'o' for @_;\n\n$_ = $x;\n\n$i = 1;\n\ns/x(?=o)|(?<=o)x/o/g, $i ++ while /x/;\n\nprint $i . $/\n\n}"}], "negative_code": [{"source_code": "<>;\n\n$x = 'x' x <>;\n\n@_ = split ' ', <>;\n\nsubstr $x, $_ - 1, 1, 'o' for @_;\n\n$_ = $x;\n\ns/x(?=o)|(?<=o)x/o/g, $i ++ while /x/;\n\nprint 1 + $i"}, {"source_code": "<>;\n\nwhile( <> ){\n\n$x = 'x' x $_;\n\n@_ = split ' ', <>;\n\nsubstr $x, $_ - 1, 1, 'o' for @_;\n\n$_ = $x;\n\ns/x(?=o)|(?<=o)x/o/g, $i ++ while /x/;\n\nprint 1 + $i . $/\n\n}"}], "src_uid": "5de25068af66273c83cc7914910c4c84"} {"nl": {"description": "There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$)\u00a0\u2014 the number of test cases. The description of the test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1 \\leq n, m \\leq 50$$$; $$$1 \\leq r \\leq n$$$; $$$1 \\leq c \\leq m$$$)\u00a0\u2014 the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either 'B' or 'W'\u00a0\u2014 a black and a white cell, respectively.", "output_spec": "For each test case, if it is impossible to make the cell in row $$$r$$$ and column $$$c$$$ black, output $$$-1$$$. Otherwise, output a single integer\u00a0\u2014 the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black. ", "sample_inputs": ["9\n\n3 5 1 4\n\nWBWWW\n\nBBBWB\n\nWWBBB\n\n4 3 2 1\n\nBWW\n\nBBW\n\nWBB\n\nWWB\n\n2 3 2 2\n\nWWW\n\nWWW\n\n2 2 1 1\n\nWW\n\nWB\n\n5 9 5 9\n\nWWWWWWWWW\n\nWBWBWBBBW\n\nWBBBWWBWW\n\nWBWBWBBBW\n\nWWWWWWWWW\n\n1 1 1 1\n\nB\n\n1 1 1 1\n\nW\n\n1 2 1 1\n\nWB\n\n2 1 1 1\n\nW\n\nB"], "sample_outputs": ["1\n0\n-1\n2\n2\n0\n-1\n1\n1"], "notes": "NoteThe first test case is pictured below. We can take the black cell in row $$$1$$$ and column $$$2$$$, and make all cells in its row black. Therefore, the cell in row $$$1$$$ and column $$$4$$$ will become black. In the second test case, the cell in row $$$2$$$ and column $$$1$$$ is already black.In the third test case, it is impossible to make the cell in row $$$2$$$ and column $$$2$$$ black.The fourth test case is pictured below. We can take the black cell in row $$$2$$$ and column $$$2$$$ and make its column black. Then, we can take the black cell in row $$$1$$$ and column $$$2$$$ and make its row black. Therefore, the cell in row $$$1$$$ and column $$$1$$$ will become black."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nMAIN:\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tmy( $n, $m, $r, $c ) = split;\n\t\n\t$debug and print \"[$n, $m, $r, $c]\";\n\t\n\tmy %row;\n\tmy %col;\n\t\n\tmy $ans;\n\t\n\tfor my $i ( 0 .. $n - 1 ){\n\t\t$_ = <>, chomp;\n\t\t\n\t\tnext if defined $ans;\n\t\t\n\t\tm/B/ and $row{ $i } = 1;\n\t\t\n\t\twhile( m/B/g ){\n\t\t\t$col{ $-[ 0 ] } = 1;\n\t\t\t\n\t\t\tif( $r - 1 == $i and $-[ 0 ] == $c - 1 ){\n\t\t\t\t$ans = 0;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\tif( defined $ans ){\n\t\tprint $ans;\n\t\t}\n\telsif( %row or %col ){\n\t\tif( $row{ $r - 1 } or $col{ $c - 1 } ){\n\t\t\tprint 1;\n\t\t\t}\n\t\telse{\n\t\t\tprint 2;\n\t\t\t}\n\t\t}\n\telse{\n\t\tprint -1;\n\t\t}\n\t}"}], "negative_code": [], "src_uid": "4ca13794471831953f2737ca9d4ba853"} {"nl": {"description": "A boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 squares to the right, 2 squares forward and 2 squares to the left, 2 squares backward and 2 to the right and 2 squares backward and 2 to the left. Naturally, the semiknight cannot move beyond the limits of the chessboard.Petya put two semiknights on a standard chessboard. Petya simultaneously moves with both semiknights. The squares are rather large, so after some move the semiknights can meet, that is, they can end up in the same square. After the meeting the semiknights can move on, so it is possible that they meet again. Petya wonders if there is such sequence of moves when the semiknights meet. Petya considers some squares bad. That is, they do not suit for the meeting. The semiknights can move through these squares but their meetings in these squares don't count.Petya prepared multiple chess boards. Help Petya find out whether the semiknights can meet on some good square for each board.Please see the test case analysis.", "input_spec": "The first line contains number t (1\u2009\u2264\u2009t\u2009\u2264\u200950) \u2014 the number of boards. Each board is described by a matrix of characters, consisting of 8 rows and 8 columns. The matrix consists of characters \".\", \"#\", \"K\", representing an empty good square, a bad square and the semiknight's position, correspondingly. It is guaranteed that matrix contains exactly 2 semiknights. The semiknight's squares are considered good for the meeting. The tests are separated by empty line.", "output_spec": "For each test, print on a single line the answer to the problem: \"YES\", if the semiknights can meet and \"NO\" otherwise.", "sample_inputs": ["2\n........\n........\n......#.\nK..##..#\n.......#\n...##..#\n......#.\nK.......\n\n........\n........\n..#.....\n..#..#..\n..####..\n...##...\n........\n....K#K#"], "sample_outputs": ["YES\nNO"], "notes": "NoteConsider the first board from the sample. We will assume the rows and columns of the matrix to be numbered 1 through 8 from top to bottom and from left to right, correspondingly. The knights can meet, for example, in square (2, 7). The semiknight from square (4, 1) goes to square (2, 3) and the semiknight goes from square (8, 1) to square (6, 3). Then both semiknights go to (4, 5) but this square is bad, so they move together to square (2, 7).On the second board the semiknights will never meet. "}, "positive_code": [{"source_code": "while(<>){\n#\tprint \"--\\n\";\n\t@_=();\n\t$f=$t=0;\n\t\n\tfor (0..7){\n\t\tpush @_,<>.\"\";\n\t\t}\n\tchomp @_;\n\t\n\t$h=0;\n\tfor (@_){\n\t\t$h or (s/K/N/ and $h++);\n#\t\tprint \"$_\\n\";\n\t\t}\n\t\n\t$k=0;\n\tfor (@_){\n\t\t$k++;\n\t\t@s=split//,$_;\n\t\t$j=0;\n\t\tfor $i(@s){\n\t\t\t$j++;\n\t\t\tif ($i eq \"K\") {\n\t\t\t\t$y=$j;\n\t\t\t\t$x=$k;\n\t\t\t\t}\n\t\t\tif ($i eq \"N\") {\n\t\t\t\t$n=$j;\n\t\t\t\t$m=$k;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t$c=$y-$n;\n\t$d=$x-$m;\n\t\n\tif ($c%4==0 and $d%4==0){$f++}\n\t\n#\tprint \"!\\n\";\n#\tprint \"!!\\n\";\t\n\t($f)?(print\"YES\\n\"):(print\"NO\\n\");\n\t\t\n\t}"}, {"source_code": "while(<>){\n\t@_=();\n\t$f=$t=0;\n\t\n\tfor (0..7){\n\t\tpush @_,<>.\"\";\n\t\t}\n\tchomp @_;\n\t\n\t$h=0;\n\tfor (@_){\n\t\t$h or (s/K/N/ and $h++);\n\t\t}\n\t\n\t$k=0;\n\tfor (@_){\n\t\t$k++;\n\t\t@s=split//,$_;\n\t\t$j=0;\n\t\tfor $i(@s){\n\t\t\t$j++;\n\t\t\tif ($i eq \"K\") {\n\t\t\t\t$y=$j;\n\t\t\t\t$x=$k;\n\t\t\t\t}\n\t\t\tif ($i eq \"N\") {\n\t\t\t\t$n=$j;\n\t\t\t\t$m=$k;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t$c=$y-$n;\n\t$d=$x-$m;\n\t\n\tunless ($c%4 + $d%4){$f++}\n\n\tprint $f?\"YES\\n\":\"NO\\n\";\n\t}"}], "negative_code": [{"source_code": "while(<>){\n#\tprint \"--\\n\";\n\t@_=();\n\t$f=$t=0;\n\t\n\tfor (0..7){\n\t\tpush @_,<>.\"\";\n\t\t}\n\tchomp @_;\n\t\n\t$h=0;\n\tfor (@_){\n\t\t$h or (s/K/N/ and $h++);\n#\t\tprint \"$_\\n\";\n\t\t}\n\t\n\t$k=0;\n\tfor (@_){\n\t\t$k++;\n\t\t@s=split//,$_;\n\t\t$j=0;\n\t\tfor $i(@s){\n\t\t\t$j++;\n\t\t\tif ($i eq \"K\") {\n\t\t\t\t$y=$j;\n\t\t\t\t$x=$k;\n\t\t\t\t}\n\t\t\tif ($i eq \"N\") {\n\t\t\t\t$n=$j;\n\t\t\t\t$m=$k;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t$c=$y-$n;\n\t$d=$x-$m;\n\t\n\tunless ($c%2==0 and $d%2==0 and ($c+$d)%4==0){$f++}\n\t\n#\tprint \"!\\n\";\n\t\n\t$k=0;\n\tfor (@_){\n\t\t$k++;\n\t\t@s=split//,$_;\n\t\t$j=0;\n\t\tfor $i(@s){\n\t\t\t$j++;\n\t\t\t($i eq \".\") and (($j-$y)%2==0) and (($k-$x)%2==0) and (($j-$y+$k-$x)%4==0) and $t++;\n\t\t\t\n\t\t\t}\n\t\t}\n#\tprint \"!!\\n\";\t\n\t($t && !$f)?(print\"YES\\n\"):(print\"NO\\n\");\n\t\t\n\t}"}, {"source_code": "while(<>){\n#\tprint \"--\\n\";\n\t@_=();\n\t$f=$t=0;\n\t\n\tfor (0..7){\n\t\tpush @_,<>.\"\";\n\t\t}\n\tchomp @_;\n\t\n\t$h=0;\n\tfor (@_){\n\t\t$h or (s/K/N/ and $h++);\n#\t\tprint \"$_\\n\";\n\t\t}\n\t\n\t$k=0;\n\tfor (@_){\n\t\t$k++;\n\t\t@s=split//,$_;\n\t\t$j=0;\n\t\tfor $i(@s){\n\t\t\t$j++;\n\t\t\tif ($i eq \"K\") {\n\t\t\t\t$y=$j;\n\t\t\t\t$x=$k;\n\t\t\t\t}\n\t\t\tif ($i eq \"N\") {\n\t\t\t\t$n=$j;\n\t\t\t\t$m=$k;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t$c=$y-$n;\n\t$d=$x-$m;\n\t\n\tunless ($c%2==0 and $d%2==0 and ($c+$d)%4==0){$f++}\n\t\n#\tprint \"!\\n\";\n#\tprint \"!!\\n\";\t\n\t(!$f)?(print\"YES\\n\"):(print\"NO\\n\");\n\t\t\n\t}"}], "src_uid": "4f3bec9c36d0ac2fdb8041469133458c"} {"nl": {"description": "The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction.At some points on the road there are n friends, and i-th of them is standing at the point xi meters and can move with any speed no greater than vi meters per second in any of the two directions along the road: south or north.You are to compute the minimum time needed to gather all the n friends at some point on the road. Note that the point they meet at doesn't need to have integer coordinate. ", "input_spec": "The first line contains single integer n (2\u2009\u2264\u2009n\u2009\u2264\u200960\u2009000)\u00a0\u2014 the number of friends. The second line contains n integers x1,\u2009x2,\u2009...,\u2009xn (1\u2009\u2264\u2009xi\u2009\u2264\u2009109)\u00a0\u2014 the current coordinates of the friends, in meters. The third line contains n integers v1,\u2009v2,\u2009...,\u2009vn (1\u2009\u2264\u2009vi\u2009\u2264\u2009109)\u00a0\u2014 the maximum speeds of the friends, in meters per second.", "output_spec": "Print the minimum time (in seconds) needed for all the n friends to meet at some point on the road. Your answer will be considered correct, if its absolute or relative error isn't greater than 10\u2009-\u20096. Formally, let your answer be a, while jury's answer be b. Your answer will be considered correct if holds.", "sample_inputs": ["3\n7 1 3\n1 2 1", "4\n5 10 3 2\n2 3 2 4"], "sample_outputs": ["2.000000000000", "1.400000000000"], "notes": "NoteIn the first sample, all friends can gather at the point 5 within 2 seconds. In order to achieve this, the first friend should go south all the time at his maximum speed, while the second and the third friends should go north at their maximum speeds."}, "positive_code": [{"source_code": "$n = <>; @x = split \" \", <>; @v = split \" \", <>;\nsub numbers { $a <=> $b }\n$v_min = (sort numbers @v) [0];\n$x_max = (sort numbers @x) [-1];\n$t = $l = 0; $r = $x_max / $v_min;\nuntil ($r - $l < 1e-6) {\n\t$t = ($l + $r) / 2;\n\t$y = 1; $X1 = 0; $X2 = $x_max;\n\tfor ($i = 0; $i < $n; $i++) {\n\t\t$x = $x[$i]; $dx = $v[$i] * $t;\n\t\t$x1 = $x - $dx; $x2 = $x + $dx;\n\t\t$x1 = 0 if $x1 < 0; $x2 = $x_max if $x2 > $x_max;\n\t\tif ($x1 > $X2 || $x2 < $X1) {\n\t\t\t$y = 0; last;\n\t\t}\n\t\tif ($x1 < $X1) { $x1 = $X1 }\n\t\tif ($x2 > $X2) { $x2 = $X2 }\n\t\t$X1 = $x1; $X2 = $x2;\n\t}\t\n\t$y? $r = $t: $l = $t;\n}\nprint $t;"}], "negative_code": [{"source_code": "$n = <>; @x = split \" \", <>; @v = split \" \", <>;\nsub numbers { $a <=> $b }\n$v_min = (sort numbers @v) [0];\n$x_max = (sort numbers @x) [-1];\n$l = 0; $r = $x_max / $v_min;\nuntil ($r - $l < 1e-6) {\n\t$t = ($l + $r) / 2;\n\t$y = 1; $X1 = 0; $X2 = $x_max;\n\tfor ($i = 0; $i < $n; $i++) {\n\t\t$x = $x[$i]; $dx = $v[$i] * $t;\n\t\t$x1 = $x - $dx; $x2 = $x + $dx;\n\t\t$x1 = 0 if $x1 < 0; $x2 = $x_max if $x2 > $x_max;\n\t\tif ($x1 > $X2 || $x2 < $X1) {\n\t\t\t$y = 0; last;\n\t\t}\n\t\tif ($x1 < $X1) { $x1 = $X1 }\n\t\tif ($x2 > $X2) { $x2 = $X2 }\n\t\t$X1 = $x1; $X2 = $x2;\n\t}\t\n\t$y? $r = $t: $l = $t;\n}\nprint $t;"}], "src_uid": "f13f27a131b9315ebbb8688e2f43ddde"} {"nl": {"description": "Little Susie, thanks to her older brother, likes to play with cars. Today she decided to set up a tournament between them. The process of a tournament is described in the next paragraph.There are n toy cars. Each pair collides. The result of a collision can be one of the following: no car turned over, one car turned over, both cars turned over. A car is good if it turned over in no collision. The results of the collisions are determined by an n\u2009\u00d7\u2009n matrix \u0410: there is a number on the intersection of the \u0456-th row and j-th column that describes the result of the collision of the \u0456-th and the j-th car: \u2009-\u20091: if this pair of cars never collided. \u2009-\u20091 occurs only on the main diagonal of the matrix. 0: if no car turned over during the collision. 1: if only the i-th car turned over during the collision. 2: if only the j-th car turned over during the collision. 3: if both cars turned over during the collision. Susie wants to find all the good cars. She quickly determined which cars are good. Can you cope with the task?", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the number of cars. Each of the next n lines contains n space-separated integers that determine matrix A. It is guaranteed that on the main diagonal there are \u2009-\u20091, and \u2009-\u20091 doesn't appear anywhere else in the matrix. It is guaranteed that the input is correct, that is, if Aij\u2009=\u20091, then Aji\u2009=\u20092, if Aij\u2009=\u20093, then Aji\u2009=\u20093, and if Aij\u2009=\u20090, then Aji\u2009=\u20090.", "output_spec": "Print the number of good cars and in the next line print their space-separated indices in the increasing order.", "sample_inputs": ["3\n-1 0 0\n0 -1 1\n0 2 -1", "4\n-1 3 3 3\n3 -1 3 3\n3 3 -1 3\n3 3 3 -1"], "sample_outputs": ["2\n1 3", "0"], "notes": null}, "positive_code": [{"source_code": "!/-/ || /( |^)[13]/ || push @_, $. -1 while <>;\nprint @_ ? @_ . \"\\n@_\" : 0\n"}, {"source_code": "!/-/ || /( |^)[13]/ || push @_, $. -1 while <>;\nprint @_ ? @_ . \"\\n@_\" : 0\n"}, {"source_code": "!/-/ || /( |^)[13]/ || push @_, $. -1 while <>;\nprint @_ ? @_ . \"\\n@_\" : 0;"}, {"source_code": "!/-/ || /( |^)[13]/ || push @_, $. -1 while <>;\nprint @_ ? @_ . \"\\n@_\" : 0"}, {"source_code": "!/-/ || /( |^)[13]/ || push @_, $. -1 while <>;\nprint @_ ? @_ . \"\\n@_\" : 0\n"}, {"source_code": "!/-/ || /( |^)[13]/ || push @_, $. -1 while <>;\nprint @_ ? @_ . \"\\n@_\" : 0\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\n@mark = (1) x ($n+1);\nforeach $i (1..$n) {\n\t$j = 1;\n\tforeach (split / /, <>) {\n\t\tif ($_ == 1) {\n\t\t\t$mark[$i] = 0;\n\t\t} elsif ($_ == 2) {\n\t\t\t$mark[$j] = 0;\n\t\t} elsif ($_ == 3) {\n\t\t\t$mark[$i] = $mark[$j] = 0;\n\t\t}\n\t\t++$j;\n\t}\n}\n$ans+=$_ foreach(@mark);\nsay $ans-1;\n$mark[$_]>0 and print \"$_ \" foreach (1..$n);"}, {"source_code": "!/-/ || /( |^)[13]/ || push @_, $. -1 while <>;\nprint @_ ? @_ . \"\\n@_\" : 0\n"}, {"source_code": "!/-/ || /( |^)[13]/ || push @_, $. -1 while <>;\nprint @_ ? @_ . \"\\n@_\" : 0"}, {"source_code": "!/-/ || /( |^)[13]/ || push @_, $. -1 while <>;\nprint @_ ? @_ . \"\\n@_\" : 0\n"}, {"source_code": "!/-/ || /( |^)[13]/ || push @_, $. -1 while <>;\nprint @_ ? @_ . \"\\n@_\" : 0\n"}, {"source_code": "$\\ = $/;\nwhile($n = <>){\n\t\n\t@_ = ();\n\tfor $i (1 .. $n){\n\t\t$_ = <>;\n\t\t/(?;\nprint @_ ? @_ . \"\\n@_\" : 0"}, {"source_code": "!/-/ || /( |^)[13]/ || push @_, $. -1 while <>;\nprint @_ ? @_ . \"\\n@_\" : 0\n"}], "negative_code": [{"source_code": "my $n = int<>;\nmy @ans;\n(scalar(() = <> =~ / 1|3/) == 0) and push @ans, $_ for(1..$n);\nprint scalar @ans;\nprint \"\\n@ans\\n\";"}], "src_uid": "3fc0ac711b113fa98f41740536dad44f"} {"nl": {"description": "Implication is a function of two logical arguments, its value is false if and only if the value of the first argument is true and the value of the second argument is false. Implication is written by using character '', and the arguments and the result of the implication are written as '0' (false) and '1' (true). According to the definition of the implication: When a logical expression contains multiple implications, then when there are no brackets, it will be calculated from left to fight. For example,. When there are brackets, we first calculate the expression in brackets. For example,.For the given logical expression determine if it is possible to place there brackets so that the value of a logical expression is false. If it is possible, your task is to find such an arrangement of brackets.", "input_spec": "The first line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000) \u2014 the number of arguments in a logical expression. The second line contains n numbers a1,\u2009a2,\u2009...,\u2009an (), which means the values of arguments in the expression in the order they occur.", "output_spec": "Print \"NO\" (without the quotes), if it is impossible to place brackets in the expression so that its value was equal to 0. Otherwise, print \"YES\" in the first line and the logical expression with the required arrangement of brackets in the second line. The expression should only contain characters '0', '1', '-' (character with ASCII code 45), '>' (character with ASCII code 62), '(' and ')'. Characters '-' and '>' can occur in an expression only paired like that: (\"->\") and represent implication. The total number of logical arguments (i.e. digits '0' and '1') in the expression must be equal to n. The order in which the digits follow in the expression from left to right must coincide with a1,\u2009a2,\u2009...,\u2009an. The expression should be correct. More formally, a correct expression is determined as follows: Expressions \"0\", \"1\" (without the quotes) are correct. If v1, v2 are correct, then v1->v2 is a correct expression. If v is a correct expression, then (v) is a correct expression. The total number of characters in the resulting expression mustn't exceed 106. If there are multiple possible answers, you are allowed to print any of them.", "sample_inputs": ["4\n0 1 1 0", "2\n1 1", "1\n0"], "sample_outputs": ["YES\n(((0)->1)->(1->0))", "NO", "YES\n0"], "notes": null}, "positive_code": [{"source_code": "<>; $_=<>;\nif(/(^|1 )0$/ or /0.*0 0$/) {\n print \"YES\\n\";\n s/0 ((1 )+0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n s/ /->/g;\n print;\n exit;\n}\nprint \"NO\";"}, {"source_code": "$n=<>; $_=<>; chomp;\nif(/1$/ or /^(1 )+0 0$/ or /^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/){\n} elsif(/1 0$/){\n} elsif(/0.*0 0$/) {\n s/0 ((1 )+0) 0$/(0 ($1)) 0/; \n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "$n=<>; $_=<>; chomp;\nif(/(^|1 )0$/ or /0.*0 0$/) {\n print \"YES\\n\"; \n s/0 ((1 )+0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n s/ /->/g;\n print;\n exit;\n}\nprint \"NO\";"}, {"source_code": "<>,$_=<>;print qw(NO YES)[/(^|1 )0$/||s/0 ((1 )*0) 0$/(0 ($1)) 0/?\"1a\".s/ /->/g.($a = $_):0].\"\\n$a\"\n"}, {"source_code": "<>,$_=<>;print qw(NO YES)[/(^|1 )0$/||s/0 ((1 )*0) 0$/(0 ($1)) 0/?\"1a\".s/ /->/g.($a = $_):0].\"\\n$a\"\n"}, {"source_code": "<>,$_=<>;print qw(NO YES)[/(^|1 )0$/||s/0 ((1 )*0) 0$/(0 ($1)) 0/?\"1a\".s/ /->/g.($a = $_):0].\"\\n$a\"\n"}, {"source_code": "<>,$_=<>;print qw(NO YES)[/(^|1 )0$/||s/0 ((1 )*0) 0$/(0 ($1)) 0/?\"1a\".s/ /->/g.($a = $_):0].\"\\n$a\"\n"}, {"source_code": "<>,$_=<>;print qw(NO YES)[/(^|1 )0$/||s/0 ((1 )*0) 0$/(0 ($1)) 0/?\"1a\".s/ /->/g.($a = $_):0].\"\\n$a\"\n"}, {"source_code": "<>,$_=<>;print qw(NO YES)[/(^|1 )0$/||s/0 ((1 )*0) 0$/(0 ($1)) 0/?\"1a\".s/ /->/g.($a = $_):0].\"\\n$a\""}, {"source_code": "<>,$_=<>;print qw(NO YES)[/(^|1 )0$/||s/0 ((1 )*0) 0$/(0 ($1)) 0/?\"1a\".s/ /->/g.($a = $_):0].\"\\n$a\"\n"}, {"source_code": "<>,$_=<>;print qw(NO YES)[/(^|1 )0$/||s/0 ((1 )*0) 0$/(0 ($1)) 0/?\"1a\".s/ /->/g.($a = $_):0].\"\\n$a\"\n"}, {"source_code": "<>, $_ = <>, y/ //d;\n\nprint 0\n||\t \t/(1|^)0$/\n||\ts\t/000$/(0->0)->0/\n||\ts\t/01+00$/ $n = -3 + length $&, \"(0->\" . \"(1->\" x $n . 0 . \")\" x $n . \")->0\" /e\n?\n\t\"\" x s/(?<=\\d)(?=\\d|\\()/->/g .\n\t\"YES\\n$_\"\n:\n\t\"NO\""}, {"source_code": "<>, $_ = <>, y/ //d;\n\nprint 0\n||\t \t/(1|^)0$/\n||\ts\t/01*00$/ $n = -3 + length $&, \"(0\" . \"(1\" x $n . 0 . \")\" x $n . \")->0\" /e\n?\n\t\"\" x s/\\d\\K(?!\\)|$)/->/g .\n\t\"YES\\n$_\"\n:\n\t\"NO\""}, {"source_code": "<>, $_ = <>;\nprint qw(NO YES)[/(^|1 )0$/ || s/0 ((1 )*0) 0$/(0 ($1)) 0/\n? \"1a\" . s/ /->/g . ($a = $_):0] . \"\\n$a\""}, {"source_code": "<>,$_=<>;print qw(NO YES)[/(^|1 )0$/||s/0 ((1 )*0) 0$/(0 ($1)) 0/?\"1a\".s/ /->/g.($a = $_):0].\"\\n$a\"\n"}], "negative_code": [{"source_code": "$n=<>; $_=<>; chomp; \n$s=$_;\n$s=~s/ //g;\n$x=1 x ($n-2);\n$x.=\"00\";\nif($s eq $x) {\n print \"NO\";\n exit;\n}\nif(/1$/) {\n print \"NO\";\n exit;\n}\nif(/^(1 )(\\1)*0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/){\n} elsif(/1 0$/){\n} elsif(/0.*0 0$/) {\n s/0 ((1 )(\\2)*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "<>; $_=<>; chomp;\nif(/1$/ or /^(1 )\\1*0 0$/ or /^0 0$/) { \n print \"NO\";\n exit;\n}\nif(/^(1 )\\1*1 0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/ or /1 0$/) {\n} elsif(/0.*0 0$/) {\n s/0 ((1 )\\2*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "$n=<>; $_=<>; chomp;\n#$s=$_;\n#$s=~s/ //g;\n#$x=1 x ($n-2);\n#$x.=\"00\"; \n#if($s eq $x) {\n# print \"NO\";\n# if(/^(1 1 1 1 1 )+0 0$/) {\n# print \"hahaha\";\n# }\n# exit;\n#}\nif(/1$/) {\n print \"NO\";\n exit;\n}\nif(/^(1 )+0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/){\n} elsif(/1 0$/){\n} elsif(/0.*0 0$/) {\n s/0 ((1 )(\\2)*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "<>;\n$_=<>;\nchomp;\n@a=split ' ';\n$s=$_;\nif(/1$/ or /^(1 )\\1*0 0$/ or /^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\";\nif(/^0$/ or /1 0$/) {\n s/ /->/g;\n print;\n} else {\n s/0 ((1 )\\2*0) 0$/(0 ($1)) 0/;\n print \"yayay\\n\";\n s/ /->/g;\n print;\n} "}, {"source_code": "$n=<>; $_=<>; chomp; \n$s=$_;\n$s=~s/ //g;\n$x=1 x ($n-2);\n$x.=\"00\";\nif($s eq $x) {\n print \"NO\";\n exit;\n}\nif(/1$/) {\n print \"NO\";\n exit;\n}\nif(/^(1 )\\1*0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/){\n} elsif(/1 0$/){\n} elsif(/0.*0 0$/) {\n s/0 ((1 )\\2*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "<>; $_=<>; chomp;\nif(/1$/ or /^(1 )\\1*0 0$/ or /^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/ or /1 0$/) {\n} elsif(/0.*00$/) {\n s/0 ((1 )\\2*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g; \nprint"}, {"source_code": "<>; $_=<>; chomp;\nif(/1$/ or /^(1 )\\1*0 0$/ or /^0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^(1 )\\1*0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/ or /1 0$/) {\n} elsif(/0.*0 0$/) {\n s/0 ((1 )\\2*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint; "}, {"source_code": "<>; \n$_=<>;\nchomp;\n@a=split ' ';\n$s=$_;\nif(/1$/ or /^(1 )\\1*0 0$/ or /^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/ or /1 0$/) {\n s/ /->/g;\n print;\n} else {\n s/0 ((1 )\\2*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n#print \"$_\\n\";\n s/ /->/g;\n print;\n}"}, {"source_code": "<>; $_=<>; chomp; \nif(/1$/ or /^(1 )\\1*0 0$/ or /^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/ or /1 0$/) {\n} elsif(/0.*0 0$/) {\n s/0 ((1 )\\2*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "<>;\n$_=<>;\nchomp;\n@a=split ' ';\n$s=$_;\nif(/1$/ or /^(1 )\\1*0 0$/ or /^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\"; \nif(/^0$/ or /1 0$/) {\n s/ /->/g;\n print;\n} else {\n s/0 ((1 )\\2*0) 0$/(0 ($1)) 0/;\n print \"yayay\\n\";\n s/ /->/g;\n print;\n}"}, {"source_code": "$n=<>; $_=<>; chomp;\n$s=$_;\n$s=~s/ //g;\n$x=1 x ($n-2);\n$x.=\"00\";\nif($s eq $x) {\n print \"NO\";\n if(/^(1 1 1 )(\\1)*0 0$/) {\n print \"hahaha\"; \n }\n exit;\n}\nif(/1$/) {\n print \"NO\";\n exit;\n}\nif(/^(1 )(\\1)*0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/){\n} elsif(/1 0$/){\n} elsif(/0.*0 0$/) {\n s/0 ((1 )(\\2)*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "$n=<>; $_=<>; chomp;\n$s=$_;\n$s=~s/ //g;\n$x=1 x ($n-2);\n$x.=\"00\";\nif($s eq $x) {\n print \"NO\";\n if(/^(1 1 1 1 )(\\1)*0 0$/) {\n print \"hahaha\"; \n }\n exit;\n}\nif(/1$/) {\n print \"NO\";\n exit;\n}\nif(/^(1 )(\\1)*0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/){\n} elsif(/1 0$/){\n} elsif(/0.*0 0$/) {\n s/0 ((1 )(\\2)*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "$n=<>; $_=<>; chomp;\n$s=$_;\n$s=~s/ //g;\n$x=1 x ($n-2);\n$x.=\"00\";\nif($s eq $x) {\n print \"NO\";\n if(/^(1 )(\\1)*0 0$/) {\n print \"hahaha\"; \n }\n exit;\n}\nif(/1$/) {\n print \"NO\";\n exit;\n}\nif(/^(1 )(\\1)*0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/){\n} elsif(/1 0$/){\n} elsif(/0.*0 0$/) {\n s/0 ((1 )(\\2)*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "<>; $_=<>; chomp;\nif(/1$/) {\n print \"NO\";\n exit;\n}\nif(/^(1 )\\1*0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/){\n} elsif(/1 0$/){\n} elsif(/0.*0 0$/) {\n s/0 ((1 )\\2*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "<>; $_=<>; chomp; \nif(/1$/ or /^(1 )\\1*1 0 0$/ or /^0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^(1 )\\1*1 0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/ or /1 0$/) {\n} elsif(/0.*0 0$/) {\n s/0 ((1 )\\2*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "<>; $_=<>; chomp;\nif(/1$/ or /^(1 )\\1*0 0$/ or /^0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^(1 )\\1*1 0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/){ \n} elsif(/1 0$/){\n} elsif(/0.*0 0$/) {\n s/0 ((1 )\\2*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "$n=<>; $_=<>; chomp;\n$s=$_;\n$s=~s/ //g;\n$x=1 x ($n-2);\n$x.=\"00\";\nif($s eq $x) {\n print \"NO\";\n if(/^(1 1 )(\\1)*0 0$/) {\n print \"hahaha\"; \n }\n exit;\n}\nif(/1$/) {\n print \"NO\";\n exit;\n}\nif(/^(1 )(\\1)*0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/){\n} elsif(/1 0$/){\n} elsif(/0.*0 0$/) {\n s/0 ((1 )(\\2)*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "$n=<>; $_=<>; chomp;\n$s=$_;\n$s=~s/ //g;\n$x=1 x ($n-2);\n$x.=\"00\";\nif($s eq $x) {\n print \"NO\";\n if(/^(1 1 1 1 1 )(\\1)*0 0$/) {\n print \"hahaha\"; \n }\n exit;\n}\nif(/1$/) {\n print \"NO\";\n exit;\n}\nif(/^(1 )(\\1)*0 0$/) {\n print \"NO\";\n exit;\n}\nif(/^0 0$/) {\n print \"NO\";\n exit;\n}\nprint \"YES\\n\";\nif(/^0$/){\n} elsif(/1 0$/){\n} elsif(/0.*0 0$/) {\n s/0 ((1 )(\\2)*0) 0$/(0 ($1)) 0/;\n s/0 0 0$/(0 0) 0/;\n}\ns/ /->/g;\nprint;"}, {"source_code": "<>, $_ = <>, y/ //d;\n\nprint 0\n||\t \t/10$/\n||\ts\t/000$/(0->0)->0/\n||\ts\t/01+00$/ $n = -2 + length $&, \"0->\" . \"(1->\" x $n . 0 . \")\" x $n . \"->0\" /e\n?\n\t\"\" x s/(?<=\\d)(?=\\d|\\()/->/g .\n\t\"YES\\n$_\"\n:\n\t\"NO\""}, {"source_code": "<>, $_ = <>, y/ //d;\n\nprint 0\n||\t \t/(1|^)0$/\n||\ts\t/000$/(0->0)->0/\n||\ts\t/01+00$/ $n = -2 + length $&, \"0->\" . \"(1->\" x $n . 0 . \")\" x $n . \"->0\" /e\n?\n\t\"\" x s/(?<=\\d)(?=\\d|\\()/->/g .\n\t\"YES\\n$_\"\n:\n\t\"NO\""}, {"source_code": "<>, $_ = <>, y/ //d;\n\nprint 0\n||\t \t/(1|^)0$/\n||\ts\t/000$/(0->0)->0/\n||\ts\t/01+00$/ $n = -3 + length $&, \"0->\" . \"(1->\" x $n . 0 . \")\" x $n . \"->0\" /e\n?\n\t\"\" x s/(?<=\\d)(?=\\d|\\()/->/g .\n\t\"YES\\n$_\"\n:\n\t\"NO\""}], "src_uid": "98380bd9d6865fa9a2d100ca3484b005"} {"nl": {"description": "Adilbek was assigned to a special project. For Adilbek it means that he has $$$n$$$ days to run a special program and provide its results. But there is a problem: the program needs to run for $$$d$$$ days to calculate the results.Fortunately, Adilbek can optimize the program. If he spends $$$x$$$ ($$$x$$$ is a non-negative integer) days optimizing the program, he will make the program run in $$$\\left\\lceil \\frac{d}{x + 1} \\right\\rceil$$$ days ($$$\\left\\lceil a \\right\\rceil$$$ is the ceiling function: $$$\\left\\lceil 2.4 \\right\\rceil = 3$$$, $$$\\left\\lceil 2 \\right\\rceil = 2$$$). The program cannot be run and optimized simultaneously, so the total number of days he will spend is equal to $$$x + \\left\\lceil \\frac{d}{x + 1} \\right\\rceil$$$.Will Adilbek be able to provide the generated results in no more than $$$n$$$ days?", "input_spec": "The first line contains a single integer $$$T$$$ ($$$1 \\le T \\le 50$$$) \u2014 the number of test cases. The next $$$T$$$ lines contain test cases \u2013 one per line. Each line contains two integers $$$n$$$ and $$$d$$$ ($$$1 \\le n \\le 10^9$$$, $$$1 \\le d \\le 10^9$$$) \u2014 the number of days before the deadline and the number of days the program runs.", "output_spec": "Print $$$T$$$ answers \u2014 one per test case. For each test case print YES (case insensitive) if Adilbek can fit in $$$n$$$ days or NO (case insensitive) otherwise.", "sample_inputs": ["3\n1 1\n4 5\n5 11"], "sample_outputs": ["YES\nYES\nNO"], "notes": "NoteIn the first test case, Adilbek decides not to optimize the program at all, since $$$d \\le n$$$.In the second test case, Adilbek can spend $$$1$$$ day optimizing the program and it will run $$$\\left\\lceil \\frac{5}{2} \\right\\rceil = 3$$$ days. In total, he will spend $$$4$$$ days and will fit in the limit.In the third test case, it's impossible to fit in the limit. For example, if Adilbek will optimize the program $$$2$$$ days, it'll still work $$$\\left\\lceil \\frac{11}{2+1} \\right\\rceil = 4$$$ days."}, "positive_code": [{"source_code": "for(1..<>){$_=<>,/(.+) (.+)/;print(($1+1)*($1+1)>=4*$2?\"YES\\n\":\"NO\\n\")}"}, {"source_code": "for(1..<>){$_=<>,/(.+) (.+)/;print($1*$1+2*$1+2>4*$2?\"YES\\n\":\"NO\\n\")}"}, {"source_code": "for(1..<>){$_=<>,/(.+) (.+)/;print(($1+1)*($1+1)>=4*$2?\"YES\\n\":\"NO\\n\");}"}, {"source_code": "for(1..<>){$_=<>,/(.+) (.+)/;print$1*$1+2*$1+2>4*$2?\"YES\\n\":\"NO\\n\"}"}, {"source_code": "for(1..<>){<>=~/(.+) (.+)/;print$1*$1+2*$1+2>4*$2?\"YES\\n\":\"NO\\n\"}"}], "negative_code": [{"source_code": "for(1..<>){$_=<>,/(.+) (.+)/;print($1+1)**2>=4*$2?\"YES\\n\":\"NO\\n\"}"}, {"source_code": "for(1..<>){<>=~/(.+)(.)/;print$1*$1+2*$1+2>4*$2?\"YES\\n\":\"NO\\n\"}"}], "src_uid": "e65b2a81689bb13b90a02a9ccf1d4125"} {"nl": {"description": "Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.", "input_spec": "First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer n (1\u2009\u2264\u2009n\u2009\u2264\u20091000), the number of days. Next n lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person. The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.", "output_spec": "Output n\u2009+\u20091 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n\u2009+\u20091)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.", "sample_inputs": ["ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler", "icm codeforces\n1\ncodeforces technex"], "sample_outputs": ["ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler", "icm codeforces\nicm technex"], "notes": "NoteIn first example, the killer starts with ross and rachel. After day 1, ross is killed and joey appears. After day 2, rachel is killed and phoebe appears. After day 3, phoebe is killed and monica appears. After day 4, monica is killed and chandler appears. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\n\nmy $line = <>;\nchomp($line);\nmy @init_pot = split (\" \", $line);\nprint \"$init_pot[0] $init_pot[1]\\n\";\nmy $num = <>;\nchomp($num);\n\nfor (my $i=0;$i < $num;$i++) {\n my $line = <>;\n chomp($line);\n my @pot = split (\" \", $line);\n if ($init_pot [0] eq $pot[0]) {\n $init_pot[0] = $pot[1];\n } elsif ($init_pot [1] eq $pot[0]) {\n $init_pot[1] = $pot[1];\n }\n\nprint \"$init_pot[0] $init_pot[1]\\n\";\n}\n\n"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tchomp;\n\tprint;\n\t\n\tmy %h = map { $_ => 1 } split ' ';\n\t\t\n\tfor ( 1 .. <> ){\n\t\t@_ = split ' ', <>;\n\t\texists $h{ $_ } ? delete $h{ $_ } : ($h{ $_ } = 1) for @_;\n\t\t\n\t\tprint join ' ', keys %h;\n\t\t}\n\t\n\t$debug and print '-' x 10;\n\t}"}], "negative_code": [], "src_uid": "3c06e3cb2d8468e738b736a9bf88b4ca"} {"nl": {"description": "In a far away kingdom lived the King, the Prince, the Shoemaker, the Dressmaker and many other citizens. They lived happily until great trouble came into the Kingdom. The ACMers settled there.Most damage those strange creatures inflicted upon the kingdom was that they loved high precision numbers. As a result, the Kingdom healers had already had three appointments with the merchants who were asked to sell, say, exactly 0.273549107 beer barrels. To deal with the problem somehow, the King issued an order obliging rounding up all numbers to the closest integer to simplify calculations. Specifically, the order went like this: If a number's integer part does not end with digit 9 and its fractional part is strictly less than 0.5, then the rounded up number coincides with the number\u2019s integer part. If a number's integer part does not end with digit 9 and its fractional part is not less than 0.5, the rounded up number is obtained if we add 1 to the last digit of the number\u2019s integer part. If the number\u2019s integer part ends with digit 9, to round up the numbers one should go to Vasilisa the Wise. In the whole Kingdom she is the only one who can perform the tricky operation of carrying into the next position. Merchants found the algorithm very sophisticated and they asked you (the ACMers) to help them. Can you write a program that would perform the rounding according to the King\u2019s order?", "input_spec": "The first line contains a single number to round up \u2014 the integer part (a non-empty set of decimal digits that do not start with 0 \u2014 with the exception of a case when the set consists of a single digit \u2014 in this case 0 can go first), then follows character \u00ab.\u00bb (a dot), and then follows the fractional part (any non-empty set of decimal digits). The number's length does not exceed 1000 characters, including the dot. There are no other characters in the input data.", "output_spec": "If the last number of the integer part is not equal to 9, print the rounded-up number without leading zeroes. Otherwise, print the message \"GOTO Vasilisa.\" (without the quotes).", "sample_inputs": ["0.0", "1.49", "1.50", "2.71828182845904523536", "3.14159265358979323846", "12345678901234567890.1", "123456789123456789.999"], "sample_outputs": ["0", "1", "2", "3", "3", "12345678901234567890", "GOTO Vasilisa."], "notes": null}, "positive_code": [{"source_code": "$_=<>;\nif(/9\\./) {\n print \"GOTO Vasilisa.\";\n exit;\n}\ns/(\\d)\\.(\\d).*/$1+($2<5?0:1)/e;\nprint;\n"}, {"source_code": "$_=<>;\nif(/9\\./) {\n print \"GOTO Vasilisa.\";\n exit;\n}\ns/([^9])\\.(\\d).*/$1+($2<5?0:1)/e;\nprint;\n"}, {"source_code": "$ch=<>;\nchomp$ch;\n$pos=index($ch,'.');\n\nif(substr($ch,$pos-1,1) eq \"9\"){\nprint \"GOTO Vasilisa.\";\n}\nelsif(substr($ch,$pos+1,1) lt \"5\"){\nprint substr($ch,0,$pos);\n}\nelse{\n$car=substr($ch,$pos-1,1)+1;\n$res= substr($ch,0,$pos-1).$car;\nprint $res;\n\n}"}, {"source_code": "$_=<>;\nchomp;\n/(.)\\.(.)/;\n$o=$2;\n$e=$`;\n$f=$1;\nif (9 == $f) {print\"GOTO Vasilisa.\"} \nelsif ($o>4 ) {$f++; print$e,$f } \nelse{print$e,$f}"}], "negative_code": [{"source_code": "$_=<>;\n{\nif (/9$/) {print\"GOTO Vasilisa.\";last}\n\n/\\.(.)/;\n\n$e=$`;\n$o=$1;\nif ($o>4 ) {\n\n$e++\n}\n\nprint$e\n}"}], "src_uid": "3060ecad253a2b4d4fac39e91fcd6c95"} {"nl": {"description": "Did you know you can download more RAM? There is a shop with $$$n$$$ different pieces of software that increase your RAM. The $$$i$$$-th RAM increasing software takes $$$a_i$$$ GB of memory to run (temporarily, once the program is done running, you get the RAM back), and gives you an additional $$$b_i$$$ GB of RAM (permanently). Each software can only be used once. Your PC currently has $$$k$$$ GB of RAM.Note that you can't use a RAM-increasing software if it takes more GB of RAM to use than what you currently have.Since RAM is the most important thing in the world, you wonder, what is the maximum possible amount of RAM achievable?", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) \u2014 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 100$$$, $$$1 \\le k \\le 1000$$$). Then two lines follow, each containing $$$n$$$ integers describing the arrays $$$a$$$ and $$$b$$$ ($$$1 \\le a_i, b_i \\le 1000$$$).", "output_spec": "For each test case, output a single line containing the largest amount of RAM you can achieve.", "sample_inputs": ["4\n\n3 10\n\n20 30 10\n\n9 100 10\n\n5 1\n\n1 1 5 1 1\n\n1 1 1 1 1\n\n5 1\n\n2 2 2 2 2\n\n100 100 100 100 100\n\n5 8\n\n128 64 32 16 8\n\n128 64 32 16 8"], "sample_outputs": ["29\n6\n1\n256"], "notes": "NoteIn the first test case, you only have enough RAM to run the third software initially, but that increases your RAM to $$$20$$$ GB, which allows you to use the first software, increasing your RAM to $$$29$$$ GB. The only software left needs $$$30$$$ GB of RAM, so you have to stop here.In the second test case, you can use the first, second, fourth and fifth software that need only $$$1$$$ GB of RAM per software to run to increase your RAM to $$$5$$$ GB, and then use the last remaining one to increase your RAM to $$$6$$$ GB.In the third test case, all the software need more than $$$1$$$ GB of RAM to run, so the amount of RAM you have stays at $$$1$$$ GB."}, "positive_code": [{"source_code": "for(1..<>) {\r\n #--------------------------Get input\r\n chomp(my $in = <>);\r\n my ($n, $k) = split / /, $in;\r\n chomp(my $in = <>);\r\n my @a = split / /, $in;\r\n chomp(my $in = <>);\r\n my @b = split / /, $in;\r\n #---------------------------Create Hash table\r\n %data = ();\r\n for(my $i = 0; $i < $n; $i++) {\r\n if (exists $data{@a[$i]}) { #----Sum b value of dupes\r\n $data{@a[$i]} += @b[$i];\r\n } else {\r\n $data{@a[$i]} = @b[$i];\r\n }\r\n }\r\n #--------------------------Iterate sorted hash table\r\n foreach my $x (sort {$a <=> $b} keys %data) {\r\n $k < $x and last;\r\n $k += $data{$x};\r\n }\r\n print $k, \"\\n\";\r\n}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tmy( $n, $k ) = split;\n\t\n\tmy @A = split ' ', <>;\n\tmy @B = split ' ', <>;\n\t\n\tmy @C;\n\t\n\twhile( @A ){\n\t\tpush @C, [ ( pop @A ), ( pop @B ) ];\n\t\t}\n\t\n\t@C = sort { $a->[ 0 ] <=> $b->[ 0 ] } @C;\n\t\n\twhile( @C and $C[ 0 ][ 0 ] <= $k ){\n\t\tmy $c = shift @C;\n\t\t\n\t\t$k += $c->[ 1 ];\n\t\t}\n\t\n\tprint $k;\n\t}"}], "negative_code": [], "src_uid": "168f2a740d21a3a916a9d560fbcffeb9"} {"nl": {"description": "Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n\u2009-\u2009i\u2009+\u20091)-th. He does this while i\u2009\u2264\u2009n\u2009-\u2009i\u2009+\u20091.After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday\u00a0\u2014 restore the initial order of the cubes using information of their current location.", "input_spec": "The first line contains single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7105)\u00a0\u2014 the number of cubes. The second line contains n integers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009109\u2009\u2264\u2009ai\u2009\u2264\u2009109), where ai is the number written on the i-th cube after Dima has changed their order.", "output_spec": "Print n integers, separated by spaces\u00a0\u2014 the numbers written on the cubes in their initial order. It can be shown that the answer is unique.", "sample_inputs": ["7\n4 3 7 6 9 1 2", "8\n6 1 4 2 5 6 9 2"], "sample_outputs": ["2 3 9 6 7 1 4", "2 1 6 2 5 4 9 6"], "notes": "NoteConsider the first sample. At the begining row was [2, 3, 9, 6, 7, 1, 4]. After first operation row was [4, 1, 7, 6, 9, 3, 2]. After second operation row was [4, 3, 9, 6, 7, 1, 2]. After third operation row was [4, 3, 7, 6, 9, 1, 2]. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4]. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tfor my $i (0 .. @_ / 2 - 1){\n\t\t$i % 2 and next;\n\t\t\n\t\t( $_[ $i ], $_[ @_ - $i - 1] ) = reverse( $_[ $i ], $_[ @_ - $i - 1] );\n\t\t\n\t\t}\n\t\n\tprint \"@_\";\n\t}"}, {"source_code": "<>;\n\n@_ = split ' ', <>;\n\n$_ % 2 or @_[ $_, @_ - $_ - 1 ] = @_[ @_ - $_ - 1, $_ ] for 0 .. @_ / 2 - 1;\n\nprint \"@_\""}], "negative_code": [], "src_uid": "7d2f22fc06d4f0b8ff8be6c6862046e7"} {"nl": {"description": "Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom \"Graph Theory\". In total, the season has n episodes, numbered with integers from 1 to n.Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch?", "input_spec": "The first line of the input contains integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009100000)\u00a0\u2014 the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n. The second line contains n\u2009-\u20091 integer a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u2009n)\u00a0\u2014 the numbers of episodes that Polycarpus has watched. All values of ai are distinct.", "output_spec": "Print the number of the episode that Polycarpus hasn't watched.", "sample_inputs": ["10\n3 8 10 1 7 9 6 5 2"], "sample_outputs": ["4"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy $n = ;\nmy $s = (1 + $n) / 2 * $n;\n\nmy @a = split(/\\s+/, <>);\n\nfor ($i = 0; $i < $n - 1; $i++) {\n $s -= @a[$i];\n}\n\n print $s;\n"}, {"source_code": "<>;\n@_=split/ /,<>;\n$a[$_]++ for @_;\n$a[$_]//print for 1..@_+1"}], "negative_code": [{"source_code": "<>;\n@_=split/ /,<>;\n$a[$_]++ for @_;\n$a[$_]//print for 1..@a-1"}, {"source_code": "<>;\n@_=split/ /,<>;\n$a[$_]++ for @_;\n$a[$_]//print for 1..@_"}, {"source_code": "<>;\n@_=split/ /,<>;\n$a[$_]++ for @_;\n$a[$_]//print for 1..@_-1"}, {"source_code": "<>;\n@_=split/ /,<>;\n$a[$_]++ for @_;\nshift @a;\n$_//print for @a;\n"}], "src_uid": "0e4ff955c1e653fbeb003987fa701729"} {"nl": {"description": "Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described).This priority factor will be affected by three types of actions: 1. \"X posted on Y's wall\" (15 points), 2. \"X commented on Y's post\" (10 points), 3. \"X likes Y's post\" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X).You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you.", "input_spec": "The first line contains your name. The second line contains an integer n, which is the number of actions (1\u2009\u2264\u2009n\u2009\u2264\u2009100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters.", "output_spec": "Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard \"<\" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1\u2009\u2264\u2009i\u2009\u2264\u2009min(|a|,\u2009|b|)), that ai\u2009<\u2009bi, and for any j (1\u2009\u2264\u2009j\u2009<\u2009i) aj\u2009=\u2009bj, where |a| and |b| stand for the lengths of strings a and b correspondently.", "sample_inputs": ["ahmed\n3\nahmed posted on fatma's wall\nfatma commented on ahmed's post\nmona likes ahmed's post", "aba\n1\nlikes likes posted's post"], "sample_outputs": ["fatma\nmona", "likes\nposted"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nno warnings 'uninitialized';\n\nmy $name = <>;\nchomp $name;\n\nmy %action_pts = (\n 'posted on' => 15,\n 'commented on' => 10,\n 'likes' => 5\n);\n\nmy %H;\nmy $N = <>+0;\nwhile ($N-- > 0) {\n my $s = <>;\n chomp $s;\n if ($s =~ m/^\\s*([a-z]+)\\s+(posted on|commented on|likes)\\s+([a-z]+)/) {\n my $name1 = $1;\n my $action = $2;\n my $name2 = $3;\n if ($name1 eq $name) {\n $H{$name2} += $action_pts{$action};\n }\n elsif ($name2 eq $name) {\n $H{$name1} += $action_pts{$action};\n }\n else {\n $H{$name1} += 0;\n $H{$name2} += 0;\n }\n }\n}\n\n#while (my ($k, $v) = each(%H)) {\n# print $k, ' ', $v, \"\\n\"\n#}\n\nmy @A = sort { $H{ $a } != $H{ $b } ? ($H{ $b } <=> $H{ $a }) : ($a cmp $b) } keys %H;\n\n\nforeach my $t (@A) {\n print $t, \"\\n\"\n}"}], "negative_code": [{"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nno warnings 'uninitialized';\n\nmy $name = <>;\nchomp $name;\n\nmy %action_pts = (\n 'posted' => 15,\n 'commented on' => 10,\n 'likes' => 5\n);\n\nmy %H;\nmy $N = <>+0;\nwhile ($N-- > 0) {\n my $s = <>;\n chomp $s;\n if ($s =~ m/^(\\w+) (posted on|commented on|likes) (\\w+)/) {\n my $name1 = $1;\n my $name2 = $3;\n my $action = $2;\n if ($name1 eq $name) {\n $H{$name2} += $action_pts{$action};\n }\n elsif ($name2 eq $name) {\n $H{$name1} += $action_pts{$action};\n }\n else {\n $H{$name1} += 0;\n $H{$name2} += 0;\n }\n }\n}\n\nmy @A = sort { $H{ $a } != $H{ $b } ? ($H{ $b } <=> $H{ $a }) : ($a cmp $b) } keys %H;\n\nforeach my $t (@A) {\n print $t, \"\\n\"\n}"}, {"source_code": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nno warnings 'uninitialized';\n\nmy $name = <>;\nchomp $name;\n\nmy %action_pts = (\n 'posted' => 15,\n 'commented on' => 10,\n 'likes' => 5\n);\n\nmy %H;\nmy $N = <>+0;\nwhile ($N-- > 0) {\n my $s = <>;\n chomp $s;\n if ($s =~ m/^\\s*([a-z]+)\\s+(posted on|commented on|likes)\\s+([a-z]+)/) {\n my $name1 = $1;\n my $name2 = $3;\n my $action = $2;\n if ($name1 eq $name) {\n $H{$name2} += $action_pts{$action};\n }\n elsif ($name2 eq $name) {\n $H{$name1} += $action_pts{$action};\n }\n else {\n $H{$name1} += 0;\n $H{$name2} += 0;\n }\n }\n}\n\nmy @A = sort { $H{ $a } != $H{ $b } ? ($H{ $b } <=> $H{ $a }) : ($a cmp $b) } keys %H;\n\n\nforeach my $t (@A) {\n print $t, \"\\n\"\n}"}], "src_uid": "b1a86308739067f2b6c9940b564e2648"} {"nl": {"description": "Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n\u2009>\u20091) is an n\u2009\u00d7\u2009n matrix with a diamond inscribed into it.You are given an odd integer n. You need to draw a crystal of size n. The diamond cells of the matrix should be represented by character \"D\". All other cells of the matrix should be represented by character \"*\". Look at the examples to understand what you need to draw.", "input_spec": "The only line contains an integer n (3\u2009\u2264\u2009n\u2009\u2264\u2009101; n is odd). ", "output_spec": "Output a crystal of size n.", "sample_inputs": ["3", "5", "7"], "sample_outputs": ["*D*\nDDD\n*D*", "**D**\n*DDD*\nDDDDD\n*DDD*\n**D**", "***D***\n**DDD**\n*DDDDD*\nDDDDDDD\n*DDDDD*\n**DDD**\n***D***"], "notes": null}, "positive_code": [{"source_code": "$_ = ;\nchomp;\n\nmy $a = 0;\nmy $b = ($_ - 1)/2;\n\nfor(; $a < ($_ - 1)/2 + 1; $a++){\n print '*' x ($b-$a), 'D' x (2 * $a + 1), '*' x ($b-$a), \"\\n\";\n}\n$a-=2;\nfor(; $a >= 0; $a--){\n print '*' x ($b-$a), 'D' x (2 * $a + 1), '*' x ($b-$a), \"\\n\";\n}\n"}, {"source_code": "#!perl -p\n$a=$_=\"D\"x$_;while(/DD/){s/\\bD|D\\b/*/g;$a=\"$_\\n$a\\n$_\"}$_=$a\n"}, {"source_code": "my $n = <>;\nmy @a = ();\n$#a = $n;\nfor (1 .. int $n/2 + 1) {\n my $b = 2 * ($_ - 1) + 1;\n my $a = ($n - $b) / 2;\n $a[$_ - 1] = $a[$n - $_] = (\"*\" x $a) . (\"D\" x $b) . (\"*\" x $a);\n}\nprint join \"\\n\", @a;"}, {"source_code": "#!perl -p\n$a=$_=\"D\"x$_;while(/DD/){s/\\bD|D\\b/*/g;$a=\"$_\\n$a\\n$_\"}$_=$a\n"}, {"source_code": "my $n = <>;\nfor (my $i = 1; $i < $n; $i += 2) {\n print \"*\" x int(($n - $i) / 2);\n print \"D\" x $i;\n print \"*\" x int(($n - $i) / 2);\n print \"\\n\";\n}\nfor (my $i = $n; $i > 0; $i -=2) {\n print \"*\" x int(($n - $i) / 2);\n print \"D\" x $i;\n print \"*\" x int(($n - $i) / 2);\n print \"\\n\";\n}\n"}, {"source_code": "#!perl -p\n$a=$_=\"D\"x$_;while(/DD/){s/\\bD|D\\b/*/g;$a=\"$_\\n$a\\n$_\"}$_=$a"}, {"source_code": "#!perl -p\n$a=$_=\"D\"x$_;while(/DD/){s/\\bD|D\\b/*/g;$a=\"$_\\n$a\\n$_\"}$_=$a\n"}, {"source_code": "#!perl -p\n$a=$_=\"D\"x$_;while(/DD/){s/\\bD|D\\b/*/g;$a=\"$_\\n$a\\n$_\"}$_=$a\n"}, {"source_code": "#!/usr/bin/perl\nuse v5.10;\n\nchomp($n = <>);\nfor (my $i=1,$j=($n-1)/2; $i<$n; $i+=2,--$j) {\n\t$line = '*' x $j . 'D'x $i . '*' x $j;\n\tsay $line;\n}\nfor (my $i=$n,$j=0; $i>0; $i-=2,++$j) {\n\t$line = '*' x $j . 'D'x $i . '*' x $j;\n\tsay $line;\n}"}, {"source_code": "#!perl -p\n$a=$_=\"D\"x$_;while(/DD/){s/\\bD|D\\b/*/g;$a=\"$_\\n$a\\n$_\"}$_=$a"}, {"source_code": "#!perl -p\n$a=$_=\"D\"x$_;while(/DD/){s/\\bD|D\\b/*/g;$a=\"$_\\n$a\\n$_\"}$_=$a\n"}, {"source_code": "#!perl -p\n$a=$_=\"D\"x$_;while(/DD/){s/\\bD|D\\b/*/g;$a=\"$_\\n$a\\n$_\"}$_=$a\n"}, {"source_code": "use integer;\nchomp($m=<>);\n$h = $m/2;\n$f = 0;\nfor(1..$m){\n print \"*\" x $h , \"D\" x ($m - 2 * $h), \"*\" x $h, \"\\n\";\n $h-- if !$f;\n $h++ if $f;\n $f++ if !$h;\n}\n"}, {"source_code": "#!perl -p\n$a=$_=\"D\"x$_;while(/DD/){s/\\bD|D\\b/*/g;$a=\"$_\\n$a\\n$_\"}$_=$a\n"}, {"source_code": "use integer;\n$n=<>;\n$_=(\"D\"x $n).\"\\n\";\npush @_, $_;\n$n/=2;\nfor $i(1..$n){\ns/D/*/;\ns/D\\*/**/;\ns/D$/*/;\npush @_, $_;\nunshift @_, $_;\n\n}\nprint @_;"}, {"source_code": "@_=$_=\"D\"x<>.\"\\n\";\nwhile(/D\\B/){\n\ts/\\bD|D\\b/*/g;\n\t@_=($_,@_,$_)\n\t}\nprint @_"}, {"source_code": "$a=$_=\"D\"x<>;\nwhile(/D\\B/){\n\ts/\\bD|D\\b/*/g;\n\t$a=\"$_\\n$a\\n$_\"\n\t}\nprint $a"}, {"source_code": "#!perl -p\n$a=$_=\"D\"x$_;while(/DD/){s/\\bD|D\\b/*/g;$a=\"$_\\n$a\\n$_\"}$_=$a\n"}], "negative_code": [{"source_code": "$_ = ;\nchomp;\n\nmy $a = 0;\nmy $b = ($_ - 1)/2;\n\nfor(; $a < ($_ - 1)/2 + 1; $a++){\n print '*' x ($b-$a), 'D' x (2 * $a + 1), '*' x ($b-$a), \"\\n\";\n}\n$a--;\nfor(; $a >= 0; $a--){\n print '*' x ($b-$a), 'D' x (2 * $a + 1), '*' x ($b-$a), \"\\n\";\n}"}, {"source_code": "$_ = ;\nchomp;\n\nmy $a = 0;\nmy $b = ($_ - 1)/2;\nprint $b, \"\\n\";\n\nfor(; $a < ($_ - 1)/2 + 1; $a++){\n print '*' x ($b-$a), 'D' x (2 * $a + 1), '*' x ($b-$a), \"\\n\";\n}\n$a-=2;\nfor(; $a >= 0; $a--){\n print '*' x ($b-$a), 'D' x (2 * $a + 1), '*' x ($b-$a), \"\\n\";\n}\n"}, {"source_code": "@_=$_=\"D\"x<>.\"\\n\";\nwhile(/D\\B/){\n\ts/\\bD|D\\b/*/g;\n\t@_=$_,@_,$_\n\t}\nprint @_"}], "src_uid": "a003d645999934c255f7b05d8494fa40"} {"nl": {"description": "Ivan is playing a strange game.He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai,\u2009j\u2009=\u20091). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k,\u2009n\u2009-\u2009i\u2009+\u20091) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.", "input_spec": "The first line contains three integer numbers n, m and k (1\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u2009100, 1\u2009\u2264\u2009m\u2009\u2264\u2009100). Then n lines follow, i-th of them contains m integer numbers \u2014 the elements of i-th row of matrix a. Each number is either 0 or 1.", "output_spec": "Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.", "sample_inputs": ["4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1", "3 2 1\n1 0\n0 1\n0 0"], "sample_outputs": ["4 1", "2 0"], "notes": "NoteIn the first example Ivan will replace the element a1,\u20092."}, "positive_code": [{"source_code": "use strict;\n\nsub g {\n\tmy ($s, $k) = @_;\n\tmy ($I, $V, $i, $v);\n\twhile ($s =~ /1/g) {\n\t\tmy $m = substr $s, $-[0], $k;\n\t\t$v = $m =~ y/1//;\n\t\tif ($v > $V) {\n\t\t\t$V = $v;\n\t\t\t$I = $i;\n\t\t}\n\t\t$i++;\n\t}\n\t(0 + $I, 0 + $V);\n}\n\nlocal *INPUT = 1? *STDIN: *DATA;\nmy ($n, $m, $k) = split \" \", ;\nmy (@a, @b); s/ //g, push @a, $_ for ;\nfor my $c (1..$m) {\n\tfor my $r (1..$n) {\n\t\t$b[$c] .= substr $a[$r-1], $c-1, 1;\n\t}\n}\n\nmy ($CHG, $VAL);\nfor $b (@b) {\n\tmy ($chg, $val) = g($b, $k);\n\t$CHG += $chg, $VAL += $val;\n}\nprint 0 + $VAL, \" \", 0 + $CHG, \"\\n\";\n\n__DATA__\n3 2 1\n1 0\n0 1\n0 0"}, {"source_code": "while(<>){\n\tmy( $n, $m, $k ) = split;\n\t\n\tfor ( map ~~<>, 1 .. $n ){\n\t\t$i = 0, map $_[ $i ++ ] .= $_, split\n\t\t}\n\t\n\t$ex = $lf = 0;\n\t\n\tfor ( @_ = grep m/1/, @_ ){\n\t\t$_1 = 0;\n\t\t$max = 0;\n\t\t$max_id = -1;\n\t\t@A = ();\n\t\t$sh = 0;\n\t\t\n\t\twhile( /(.)/g ){\n\t\t\t$_1 += $1;\n\t\t\tpush @A, $1;\n\t\t\tif( @A > $k ){\n\t\t\t\t$_1 -= shift @A;\n\t\t\t\t$sh ++;\n\t\t\t\t}\n\t\t\t$max < $_1 and do { $max_id = $sh, $max = $_1 };\n\t\t\t}\n\t\t\n\t\ts/.{$max_id}//;\n\t\t$ex += () = $& =~ /1/g;\n\t\ts/.{1,$k}//;\n\t\t$lf += () = $& =~ /1/g;\n\t\t}\n\t\n\tprint \"$lf $ex\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\nmy $debug = 0;\n\nwhile(<>){\n\tmy( $n, $m, $k ) = split;\n\t\n\t@_ = ();\n\t\n\tfor ( map ~~<>, 1 .. $n ){\n\t\tmy $i = 0;\n\t\tmap { $_[ $i ++ ] .= $_ } split;\n\t\t}\n\t\n\t$debug and print for @_;\n\t@_ = grep m/1/, @_;\n\tmy $ex = 0;\n\tmy $lf = 0;\n\t\n\tfor ( @_ ){\n\t\tmy $_1 = 0;\n\t\tmy $max = 0;\n\t\tmy $max_id = -1;\n\t\tmy @A = ();\n\t\tmy $shift = 0;\n\t\t\n\t\twhile( /(.)/g ){\n\t\t\t$_1 += $1;\n\t\t\tpush @A, $1;\n\t\t\tif( @A > $k ){\n\t\t\t\t$_1 -= shift @A;\n\t\t\t\t$shift ++;\n\t\t\t\t}\n\t\t\t$debug and print \"[@A]|$_1\";\n\t\t\t$max < $_1 and $max_id = $shift;\n\t\t\t$max < $_1 and $max = $_1;\n\t\t\t}\n\t\t\n\t\t$debug and print $max;\n\t\t$debug and print $max_id;\n\t\t\n\t\ts/.{$max_id}//;\n\t\t$ex += () = $& =~ /1/g;\n\t\ts/.{1,$k}//;\n\t\t$lf += () = $& =~ /1/g;\n\t\t}\n\t\n\tprint \"$lf $ex\";\n\t$debug and print '-' x 20;\n\t}"}], "negative_code": [], "src_uid": "2a3c3e98910246eaf9ec60ff1680fab6"} {"nl": {"description": "Vasya came up with a password to register for EatForces \u2014 a string $$$s$$$. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits.But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords \"abaCABA12\", \"Z7q\" and \"3R24m\" are valid, and the passwords \"qwerty\", \"qwerty12345\" and \"Password\" are not. A substring of string $$$s$$$ is a string $$$x = s_l s_{l + 1} \\dots s_{l + len - 1} (1 \\le l \\le |s|, 0 \\le len \\le |s| - l + 1)$$$. $$$len$$$ is the length of the substring. Note that the empty string is also considered a substring of $$$s$$$, it has the length $$$0$$$.Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length.Note that the length of $$$s$$$ should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits.", "input_spec": "The first line contains a single integer $$$T$$$ ($$$1 \\le T \\le 100$$$) \u2014 the number of testcases. Each of the next $$$T$$$ lines contains the initial password $$$s~(3 \\le |s| \\le 100)$$$, consisting of lowercase and uppercase Latin letters and digits. Only $$$T = 1$$$ is allowed for hacks.", "output_spec": "For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is $$$0$$$. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords \"abcdef\" $$$\\rightarrow$$$ \"a7cdEf\" is $$$4$$$, because the changed positions are $$$2$$$ and $$$5$$$, thus $$$(5 - 2) + 1 = 4$$$. It is guaranteed that such a password always exists. If there are several suitable passwords \u2014 output any of them.", "sample_inputs": ["2\nabcDCE\nhtQw27"], "sample_outputs": ["abcD4E\nhtQw27"], "notes": "NoteIn the first example Vasya's password lacks a digit, he replaces substring \"C\" with \"4\" and gets password \"abcD4E\". That means, he changed the substring of length 1.In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0)."}, "positive_code": [{"source_code": "<>; for (<>) {\n\t@e = qw([a-z] [A-Z] [0-9]);\n\tfor $e1 (@e) {\n\tfor $e2 (@e) {\t\n\t\t$c = substr $e1, 1, 1;\n\t\t/$e1/ || s/($e2)(.*$e2)/$c$2/;\n\t}}\n\tprint;\n}"}, {"source_code": "#usr/bin/perl -w\nuse strict;\n\nmy $t=;chomp($t);my $l;my $u;my $d;\nfor my $i(1..$t){\n my $s=;\n $l=$s=~tr/[a-z]//;\n $u=$s=~tr/[A-Z]//;\n $d=$s=~tr/[0-9]//;\n if($l==0){\n\tif($u==0){\n\t $u++;\n\t $l++;\n\t $s=~s/.{2}/Ax/;\n\t}\n\telsif($d==0){\n\t $l++;\n\t $d++;\n\t $s=~s/.{2}/a3/;\n\t}\n\telse{\n\t if($u==1){\t$s=~s/[0-9]/s/;}\n\t else{$s=~s/[A-Z]/s/;}\n\t $l++;\n\t}\n }\n if($u==0){\n\tif($l==0){\n\t $u++;\n\t $l++;\n\t $s=~s/.{2}/Ax/;\n\t}\n\telsif($d==0){\n\t $u++;\n\t $d++;\n\t $s=~s/.{2}/A3/;\n\t}\n\telse{\n\t if($l==1){\t$s=~s/[0-9]/A/;}\n\t else{$s=~s/[a-z]/A/;}\n\t $u++;\n\t}\n }\n if($d==0){\n\tif($u==0){\n\t $u++;\n\t $d++;\n\t $s=~s/.{2}/A3/;\n\t}\n\telsif($l==0){\n\t $l++;\n\t $d++;\n\t $s=~s/.{2}/a3/;\n\t}\n\telse{\n\t if($u==1){\t$s=~s/[a-z]/6/;}\n\t else{$s=~s/[A-Z]/6/;}\n\t $d++;\n\t}\n }\n print \"$s\\n\";\n}"}, {"source_code": "<>;\n\nwhile(<>){\n \n\ts/[a-z]\\K/,/g;\n\ts/[A-Z]\\K/;/g;\n\ts/[0-9]\\K/!/g;\n\t\n\t$re = '.(\\W).*\\K.\\1';\n\t\n\t0 while 0 ||\n\t\t!/,/ && s/$re/a,/ ||\n\t\t!/;/ && s/$re/A;/ ||\n\t\t!/!/ && s/$re/0!/ ||\n\t\t0;\n\t\n\tprint s/\\W//gr . $/\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\ts/^[a-z][a-z]([a-z]+)$/A1$1/;\n\ts/^[A-Z][A-Z]([A-Z]+)$/a1$1/;\n\ts/^[0-9][0-9]([0-9]+)$/Aa$1/;\n\t\n\t!/[0-9]/ and \n\t\ts/([A-Z])([a-z])([a-z])/$1${2}1/ ||\n\t\ts/([a-z])([a-z])([A-Z])/1$2$3/ || \n\t\ts/([a-z])([A-Z])([a-z])/$1${2}1/ || \n\t\ts/([a-z])([A-Z])([A-Z])/$1${2}1/ ||\n\t\ts/([A-Z])([A-Z])([a-z])/1$2$3/ || \n\t\ts/([A-Z])([a-z])([A-Z])/$1${2}1/ || 0\n\t\t;\n\t!/[a-z]/ and \n\t\ts/([A-Z])([0-9])([0-9])/$1${2}a/ ||\n\t\ts/([0-9])([0-9])([A-Z])/a$2$3/ || \n\t\ts/([0-9])([A-Z])([0-9])/$1${2}a/ || \n\t\ts/([0-9])([A-Z])([A-Z])/$1${2}a/ ||\n\t\ts/([A-Z])([A-Z])([0-9])/a$2$3/ || \n\t\ts/([A-Z])([0-9])([A-Z])/$1${2}a/ || 0\n\t\t;\n\t!/[A-Z]/ and \n\t\ts/([a-z])([0-9])([0-9])/$1${2}A/ ||\n\t\ts/([0-9])([0-9])([a-z])/A$2$3/ || \n\t\ts/([0-9])([a-z])([0-9])/$1${2}A/ || \n\t\ts/([0-9])([a-z])([a-z])/$1${2}A/ ||\n\t\ts/([a-z])([a-z])([0-9])/A$2$3/ || \n\t\ts/([a-z])([0-9])([a-z])/$1${2}A/ || 0\n\t\t;\n\t\n\tprint;\n\t}"}, {"source_code": "<>;\n\nwhile(<>){\n \n\ts/[a-z]\\K/,/g;\n\ts/[A-Z]\\K/;/g;\n\ts/[0-9]\\K/!/g;\n\t\t\n\t/[;!]/ or s/..../A;0!/;\n\t/[,!]/ or s/..../a,0!/;\n\t/[,;]/ or s/..../a,A;/;\n\t\n\t/,/ or s/.([;!]).*\\K.\\1/a$1/;\n\t/;/ or s/.([,!]).*\\K.\\1/A$1/;\n\t/!/ or s/.([,;]).*\\K.\\1/0$1/;\n\t\n\tprint y/,;!//rd;\n\t}"}], "negative_code": [{"source_code": "#usr/bin/perl -w\nuse strict;\n\nmy $t=;chomp($t);my $l;my $u;my $d;\nfor my $i(1..$t){\n my $s=;\n $l=$s=~tr/[a-z]//;\n $u=$s=~tr/[A-Z]//;\n $d=$s=~tr/[0-9]//;\n while($d==0||$u==0||$l==0){\n\tif($l>1){\n\t $s=~s/[a-z]/4/ if($d==0);\n\t $s=~s/[a-z]/A/ if($u==0);\n\t $d++ if($d==0);\n\t $u++ if($u==0);\n\t}\n\telse{\n\t if($d>$u){\n\t\t$s=~s/[0-9]/a/ if($l==0);\n\t\t$s=~s/[0-9]/A/ if($u==0);\n\t\t$l++ if($l==0);\n\t\t$u++ if($u==0);\n\t }\n\t else{\n\t\t$s=~s/[A-Z]/a/ if($l==0);\n\t\t$s=~s/[A-Z]/A/ if($d==0);\n\t\t$l++ if($l==0);\n\t\t$d++ if($d==0);\n\t }\n\t}\n }\n print \"$s\\n\";\n}"}, {"source_code": "<>;\n\n$_ = <>;\n\ns/[a-z]\\K/,/g;\ns/[A-Z]\\K/;/g;\ns/[0-9]\\K/!/g;\n\t\n/[;!]/ or s/..../A;0!/;\n/[,!]/ or s/..../a,0!/;\n/[,;]/ or s/..../a,A;/;\n\n/,/ or s/.([;!]).*\\K.\\1/a$1/;\n/;/ or s/.([,!]).*\\K.\\1/A$1/;\n/!/ or s/.([,;]).*\\K.\\1/0$1/;\n\nprint y/,;!//rd"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tchomp;\n\ts/^[a-z][a-z]([a-z]+)$/A1$1/;\n\ts/^[A-Z][A-Z]([A-Z]+)$/a1$1/;\n\ts/^[0-9][0-9]([0-9]+)$/Aa$1/;\n\t!/[0-9]/ and \n\t\ts/([A-Z])([a-z])([a-z])/$1${2}1/ ||\n\t\ts/([a-z])([a-z])([A-Z])/1$2$3/ || \n\t\ts/([a-z])([A-Z])([a-z])/$1${2}1/ || \n\t\ts/([a-z])([A-Z])([A-Z])/$1${2}1/ ||\n\t\ts/([A-Z])([A-Z])([a-z])/1$2$3/ || \n\t\ts/([A-Z])([a-z])([A-Z])/$1${2}1/ || 0\n\t\t;\n\t!/[a-z]/ and \n\t\ts/([A-Z])([0-9])([0-9])/$1${2}1/ ||\n\t\ts/([0-9])([0-9])([A-Z])/1$2$3/ || \n\t\ts/([0-9])([A-Z])([0-9])/$1${2}1/ || \n\t\ts/([0-9])([A-Z])([A-Z])/$1${2}1/ ||\n\t\ts/([A-Z])([A-Z])([0-9])/1$2$3/ || \n\t\ts/([A-Z])([0-9])([A-Z])/$1${2}1/ || 0\n\t\t;\n\t!/[A-Z]/ and \n\t\ts/([a-z])([0-9])([0-9])/$1${2}1/ ||\n\t\ts/([0-9])([0-9])([a-z])/1$2$3/ || \n\t\ts/([0-9])([a-z])([0-9])/$1${2}1/ || \n\t\ts/([0-9])([a-z])([a-z])/$1${2}1/ ||\n\t\ts/([a-z])([a-z])([0-9])/1$2$3/ || \n\t\ts/([a-z])([0-9])([a-z])/$1${2}1/ || 0\n\t\t;\n\t\n\tprint;\n\t}"}, {"source_code": "<>;\n\nwhile(<>){\n \n\ts/[a-z]\\K/,/g;\n\ts/[A-Z]\\K/;/g;\n\ts/[0-9]\\K/!/g;\n\t\n\t$re = '.(\\W).*\\K.\\1';\n\t\n\t0 while 0 ||\n\t\t!/,/ && s/$re/a,/ ||\n\t\t!/;/ && s/$re/A;/ ||\n\t\t!/!/ && s/$re/0!/ ||\n\t\t0;\n\t\n\tprint s/\\W//gr\n\t}"}], "src_uid": "81faa525ded9b209fb7d5d8fec95f38b"} {"nl": {"description": "Given a permutation $$$p$$$ of length $$$n$$$, find its subsequence $$$s_1$$$, $$$s_2$$$, $$$\\ldots$$$, $$$s_k$$$ of length at least $$$2$$$ such that: $$$|s_1-s_2|+|s_2-s_3|+\\ldots+|s_{k-1}-s_k|$$$ is as big as possible over all subsequences of $$$p$$$ with length at least $$$2$$$. Among all such subsequences, choose the one whose length, $$$k$$$, is as small as possible. If multiple subsequences satisfy these conditions, you are allowed to find any of them.A sequence $$$a$$$ is a subsequence of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deleting some (possibly, zero or all) elements.A permutation of length $$$n$$$ is an array of length $$$n$$$ in which every element from $$$1$$$ to $$$n$$$ occurs exactly once.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$)\u00a0\u2014 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 10^5$$$)\u00a0\u2014 the length of the permutation $$$p$$$. The second line of each test case contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, $$$\\ldots$$$, $$$p_{n}$$$ ($$$1 \\le p_i \\le n$$$, $$$p_i$$$ are distinct)\u00a0\u2014 the elements of the permutation $$$p$$$. The sum of $$$n$$$ across the test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, the first line should contain the length of the found subsequence, $$$k$$$. The second line should contain $$$s_1$$$, $$$s_2$$$, $$$\\ldots$$$, $$$s_k$$$\u00a0\u2014 its elements. If multiple subsequences satisfy these conditions, you are allowed to find any of them.", "sample_inputs": ["2\n3\n3 2 1\n4\n1 3 4 2"], "sample_outputs": ["2\n3 1 \n3\n1 4 2"], "notes": "NoteIn the first test case, there are $$$4$$$ subsequences of length at least $$$2$$$: $$$[3,2]$$$ which gives us $$$|3-2|=1$$$. $$$[3,1]$$$ which gives us $$$|3-1|=2$$$. $$$[2,1]$$$ which gives us $$$|2-1|=1$$$. $$$[3,2,1]$$$ which gives us $$$|3-2|+|2-1|=2$$$. So the answer is either $$$[3,1]$$$ or $$$[3,2,1]$$$. Since we want the subsequence to be as short as possible, the answer is $$$[3,1]$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nmy ($t) = map { $_ - 0 } split(/\\s+/,);\nfor(my $i=0;$i<$t;$i++){\n my ($n) = map { $_ - 0 } split(/\\s+/,);\n my @a = map { $_ - 0 } split(/\\s+/,);\n \n my @d = ();\n $#d = $n;\n \n for(my $j=0;$j<$n-1;$j++){\n $d[$j] = ( $a[$j+1] > $a[$j] ? 1 : -1 );\n }\n \n my $cnt = $n;\n if( $n>2 ){\n for(my $j=1;$j<$n-1;$j++){\n if( $d[$j-1] == $d[$j] ){\n $a[$j] = -1;\n $cnt --;\n }\n }\n }\n \n print \"$cnt\\n\";\n my $p = '';\n for(my $j=0;$j<$n;$j++){\n if( $a[$j] != -1 ){\n $p .= \" \" if length($p);\n $p .= $a[$j];\n }\n }\n print \"$p\\n\";\n}\n\n\n"}], "negative_code": [], "src_uid": "857de33d75daee460206079fa2c15814"} {"nl": {"description": "Fangy the little walrus, as all the modern walruses, loves to communicate via text messaging. One day he faced the following problem: When he sends large texts, they are split into parts each containing n characters (which is the size of one text message). Thus, whole sentences and words get split!Fangy did not like it, so he faced the task of breaking the text into minimal messages on his own so that no sentence were broken into pieces when it is sent and the number of text messages to be sent would be minimal. If two consecutive sentences are in different messages, the space between them can be ignored (Fangy does not write this space).The little walrus's text looks in the following manner: TEXT ::= SENTENCE | SENTENCE SPACE TEXTSENTENCE ::= WORD SPACE SENTENCE | WORD ENDEND ::= {'.', '?', '!'}WORD ::= LETTER | LETTER WORDLETTER ::= {'a'..'z', 'A'..'Z'}SPACE ::= ' 'SPACE stands for the symbol of a space.So, how many messages did Fangy send?", "input_spec": "The first line contains an integer n, which is the size of one message (2\u2009\u2264\u2009n\u2009\u2264\u2009255). The second line contains the text. The length of the text does not exceed 104 characters. It is guaranteed that the text satisfies the above described format. Specifically, this implies that the text is not empty.", "output_spec": "On the first and only line print the number of text messages Fangy will need. If it is impossible to split the text, print \"Impossible\" without the quotes.", "sample_inputs": ["25\nHello. I am a little walrus.", "2\nHow are you?", "19\nHello! Do you like fish? Why?"], "sample_outputs": ["2", "Impossible", "3"], "notes": "NoteLet's take a look at the third sample. The text will be split into three messages: \"Hello!\", \"Do you like fish?\" and \"Why?\"."}, "positive_code": [{"source_code": "$n=<>;\nwhile(<>){\n\tchomp;\n\ts/[.!?]/!/g;\n#\ts/.{19}/$a=$&,$a=~s#!##e/e;\n\twhile ($_){\n\t\t$i++;\n\t\ts/.//;\n\t\t\"!\" eq $& and push @i,$i;\n\t\t}\n#\tprint \" @i\\n\";\n\t\n\t$j=0;\n\twhile (@i){\n\t\t$i=shift @i;\n\t\tif ($i-$s<=$n){$k=$i+1}\n\t\telse {$j++; $s=$k ; unshift @i, $i; $j>10e4 and last}\n#\t\tprint \" $i $j $k\\n\";\n\t\t}\n\tprint $j>10e4?\"Impossible\":($j+1);\n\t\n\t}"}], "negative_code": [], "src_uid": "12b64f77fc9dd44a7e0287ff0a716a6a"} {"nl": {"description": "The Smart Beaver from ABBYY invented a new message encryption method and now wants to check its performance. Checking it manually is long and tiresome, so he decided to ask the ABBYY Cup contestants for help.A message is a sequence of n integers a1,\u2009a2,\u2009...,\u2009an. Encryption uses a key which is a sequence of m integers b1,\u2009b2,\u2009...,\u2009bm (m\u2009\u2264\u2009n). All numbers from the message and from the key belong to the interval from 0 to c\u2009-\u20091, inclusive, and all the calculations are performed modulo c.Encryption is performed in n\u2009-\u2009m\u2009+\u20091 steps. On the first step we add to each number a1,\u2009a2,\u2009...,\u2009am a corresponding number b1,\u2009b2,\u2009...,\u2009bm. On the second step we add to each number a2,\u2009a3,\u2009...,\u2009am\u2009+\u20091 (changed on the previous step) a corresponding number b1,\u2009b2,\u2009...,\u2009bm. And so on: on step number i we add to each number ai,\u2009ai\u2009+\u20091,\u2009...,\u2009ai\u2009+\u2009m\u2009-\u20091 a corresponding number b1,\u2009b2,\u2009...,\u2009bm. The result of the encryption is the sequence a1,\u2009a2,\u2009...,\u2009an after n\u2009-\u2009m\u2009+\u20091 steps.Help the Beaver to write a program that will encrypt messages in the described manner.", "input_spec": "The first input line contains three integers n, m and c, separated by single spaces. The second input line contains n integers ai (0\u2009\u2264\u2009ai\u2009<\u2009c), separated by single spaces \u2014 the original message. The third input line contains m integers bi (0\u2009\u2264\u2009bi\u2009<\u2009c), separated by single spaces \u2014 the encryption key. The input limitations for getting 30 points are: 1\u2009\u2264\u2009m\u2009\u2264\u2009n\u2009\u2264\u2009103 1\u2009\u2264\u2009c\u2009\u2264\u2009103 The input limitations for getting 100 points are: 1\u2009\u2264\u2009m\u2009\u2264\u2009n\u2009\u2264\u2009105 1\u2009\u2264\u2009c\u2009\u2264\u2009103 ", "output_spec": "Print n space-separated integers \u2014 the result of encrypting the original message.", "sample_inputs": ["4 3 2\n1 1 1 1\n1 1 1", "3 1 5\n1 2 3\n4"], "sample_outputs": ["0 1 1 0", "0 1 2"], "notes": "NoteIn the first sample the encryption is performed in two steps: after the first step a\u2009=\u2009(0,\u20090,\u20090,\u20091) (remember that the calculations are performed modulo 2), after the second step a\u2009=\u2009(0,\u20091,\u20091,\u20090), and that is the answer. "}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\nmy ($n,$m,$c) = split(/\\D/, <>);\nmy @a = split(/\\D/, <>);\nmy @b = split(/\\D/, <>);\nfor my $i (0..$n-$m) {\n for my $j ($i..$i+$m-1) {\n $a[$j] += $b[$j-$i];\n $a[$j] %= $c;\n }\n}\nprint \"@a\";\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\n# use strict;\n# OMG! It works!\n\n##main();\n##\n##sub main {\n# my ($n, $k) = split(/\\D/, <>);\n# my @nums = split(/\\D/, <>);\n# my %hash;\n# for my $i (0..$n-1) {\n# push @{$hash{$nums[$i]}}, $i;\n# }\n# for my $i (keys %hash) {\n# if (@{$hash{$i}} < $k) {\n# delete $hash{$i};\n# }\n# }\n# my %accepted_indexes;\n# for my $i (keys %hash) {\n# my @indexes = sort @{$hash{$i}};\n# for my $j (0..@indexes-$k) {\n# for my $p ($j+$k-1..@indexes-1) {\n# for my $f (0..$indexes[$j]) {\n# for my $l ($indexes[$p]..@nums-1) {\n# $accepted_indexes{$f}->{$l} = 1;\n# }\n# }\n# }\n# }\n# }\n# my $amount = 0;\n# for my $i (keys %accepted_indexes) {\n# $amount += values $accepted_indexes{$i};\n# my @keys = keys $accepted_indexes{$i};\n# }\n# print $amount;\n##}\n\nmy ($n, $k) = split(/\\D/, <>);\nmy @a = split(/\\D/, <>);\nmy %mp = ();\nmy $res = 0;\nmy $j = 0;\nmy $t = 0;\nfor my $i (0..$n-1) {\n if (defined $mp{$a[$i]}) {\n $mp{$a[$i]}++;\n } else {\n $mp{$a[$i]} = 1;\n }\n while ( ($j <= $i) && ($k <= $mp{$a[$i]}) ) {\n $mp{$a[$i]}--;\n $res += $n - $i;\n $j++;\n }\n}\nprint $res;\n \n\n__END__"}], "src_uid": "9a6ee18e144a38935d7c06e73f2e6384"} {"nl": {"description": "You are given two strings a and b. You have to remove the minimum possible number of consecutive (standing one after another) characters from string b in such a way that it becomes a subsequence of string a. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from b and make it empty.Subsequence of string s is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string s.", "input_spec": "The first line contains string a, and the second line\u00a0\u2014 string b. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters.", "output_spec": "On the first line output a subsequence of string a, obtained from b by erasing the minimum number of consecutive characters. If the answer consists of zero characters, output \u00ab-\u00bb (a minus sign).", "sample_inputs": ["hi\nbob", "abca\naccepted", "abacaba\nabcdcba"], "sample_outputs": ["-", "ac", "abcba"], "notes": "NoteIn the first example strings a and b don't share any symbols, so the longest string that you can get is empty.In the second example ac is a subsequence of a, and at the same time you can obtain it by erasing consecutive symbols cepted from string b."}, "positive_code": [{"source_code": "($a, $b) = <>; chomp($a, $b);\n@a = split //, $a; @b = split //, $b;\n\nfor ($i = $j = 0; $j < @b; $j++) {\n\tfor (; $i < @a && $a[$i] ne $b[$j]; $i++) {};\n\tif ($i < @a) {\n\t\tpush @p, [$i,$j]; \n\t\t$i++;\n\t} else {\n\t\tlast;\n\t}\n}\n\nfor ($i = $#a, $j = $#b; $j >= 0; $j--) {\n\tfor (; $i >= 0 && $a[$i] ne $b[$j]; $i--) {};\n\tif ($i >= 0) {\n\t\tunshift @s, [$i,$j]; \n\t\t$i--;\n\t} else {\n\t\tlast;\n\t}\n}\n\nif (@p == 0) {\n\t@R = @s;\n} elsif (@s == 0) {\n\t@R = @p;\n} else {\n\t$i = $j = 0;\n\twhile ($i < @p) {\n\t\twhile ($j < @s && ($p[$i][0] >= $s[$j][0] || $p[$i][1] >= $s[$j][1])) {\n\t\t\t$j++;\n\t\t}\n\t\t$r = $i + @s - $j;\n\t\tif ($r > $R) {\n\t\t\t$R = $r; $I = $i; $J = $j;\n\t\t}\n\t\t$i++;\n\t}\n\tpush @R, @p[0..$I];\n\tpush @R, @s[$J..$#s];\n\t@R = @p if @p > $R;\n\t@R = @s if @s > $R;\n}\n\n$ans .= $a[$$_[0]] for @R;\nprint $ans || \"-\";\n"}], "negative_code": [{"source_code": "($a, $b) = <>; chomp($a, $b);\n@a = split //, $a; @b = split //, $b;\n\nfor ($i = $j = 0; $j < @b; $j++) {\n\tfor (; $i < @a && $a[$i] ne $b[$j]; $i++) {};\n\tif ($i < @a) {\n\t\tpush @p, $i; \n\t\t$i++;\n\t} else {\n\t\tlast;\n\t}\n}\n\nfor ($i = $#a, $j = $#b; $j >= 0; $j--) {\n\tfor (; $i >= 0 && $a[$i] ne $b[$j]; $i--) {};\n\tif ($i >= 0) {\n\t\tunshift @s, $i; \n\t\t$i--;\n\t} else {\n\t\tlast;\n\t}\n}\n\nfor ($i = -1; $i < @p; $i++) {\n\t@r1 = @p[0..$i]; $r1 = $i == -1? -1: $p[$i];\n\t@r2 = grep($_ > $r1, @s);\n\t@r = (@r1, @r2);\n\t@rmax = @r if @r > @rmax;\n} \n\n$ans .= $a[$_] for @rmax;\nprint $ans || \"-\";\n"}, {"source_code": "($a, $b) = <>; chomp($a, $b);\n$A = length $a;\n$a1 = $a2 = \"0\" x $A;\n\nsub t {\n\tmy ($a, $b, $out) = @_;\n\tfor $i (1..length $b) {\n\t\t$b2 = substr $b, 0, $i;\n\t\t$m = join \"\", map \"($_).*?\", split //, $b2;\n\t\t$a =~ /$m/ || last;\n\t\tsubstr $$out, $-[-1], 1, 1;\n\t}\n}\n\nsub rev($) { scalar reverse $_[0] }\n\nt $a, $b, \\$a1;\nt rev $a, rev $b, \\$a2; $a2 = rev $a2;\n\nfor $k (1..$A) {\n\t$_ = substr($a1, 0, $k).substr($a2, $k);\n\t$c = y/1/1/;\n\tif ($c > $C) {\n\t\t$C = $c;\n\t\t$F = $_;\n\t}\n}\n\n$F =~ y/01/\\000\\377/;\n$_ = $a & $F; y/\\000//; $_ ||= \"-\";\nprint;\n"}, {"source_code": "($a, $b) = <>; chomp($a, $b);\n@a = split //, $a; @b = split //, $b;\n\nfor ($i = $j = 0; $j < @b; $j++) {\n\tfor (; $i < @a && $a[$i] ne $b[$j]; $i++) {};\n\tif ($i < @a) {\n\t\tpush @p, [$i,$j]; \n\t\t$i++;\n\t} else {\n\t\tlast;\n\t}\n}\n\nfor ($i = $#a, $j = $#b; $j >= 0; $j--) {\n\tfor (; $i >= 0 && $a[$i] ne $b[$j]; $i--) {};\n\tif ($i >= 0) {\n\t\tunshift @s, [$i,$j]; \n\t\t$i--;\n\t} else {\n\t\tlast;\n\t}\n}\n\nif (@p == 0) {\n\t@R = @s;\n} elsif (@s == 0) {\n\t@R = @p;\n} else {\n\t$i = $j = 0;\n\twhile ($i < @p) {\n\t\twhile ($j < @s && ($p[$i][0] >= $s[$j][0] || $p[$i][1] >= $s[$j][1])) {\n\t\t\t$j++;\n\t\t}\n\t\t$r = $i + @s - $j;\n\t\tif ($r > $R) {\n\t\t\t$R = $r; $I = $i; $J = $j;\n\t\t}\n\t\t$i++;\n\t}\n\tif (@p > $R) {\n\t\t@R = @p;\n\t} elsif (@s > $R) {\n\t\t@R = @s;\n\t} else {\n\t\tpush @R, @p[0..$I];\n\t\tpush @R, @s[$J..$#s];\n\t}\n}\n\n$ans .= $a[$$_[0]] for @R;\nprint $ans || \"-\";\n"}], "src_uid": "89aef6720eac7376ce0a657d46c3c5b7"} {"nl": {"description": "Andryusha is an orderly boy and likes to keep things in their place.Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time? ", "input_spec": "The first line contains the single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105)\u00a0\u2014 the number of sock pairs. The second line contains 2n integers x1,\u2009x2,\u2009...,\u2009x2n (1\u2009\u2264\u2009xi\u2009\u2264\u2009n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi. It is guaranteed that Andryusha took exactly two socks of each pair.", "output_spec": "Print single integer\u00a0\u2014 the maximum number of socks that were on the table at the same time.", "sample_inputs": ["1\n1 1", "3\n2 1 1 3 2 3"], "sample_outputs": ["1", "2"], "notes": "NoteIn the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.In the second example Andryusha behaved as follows: Initially the table was empty, he took out a sock from pair 2 and put it on the table. Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table. Socks (1,\u20092) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe. Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table. Socks (2,\u20093) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe. Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe. Thus, at most two socks were on the table at the same time."}, "positive_code": [{"source_code": "$/ = \"\"; $_ = <>; ($n, @x) = split;\nfor (@x) {\n\tif (exists $x{$_}) {\n\t\tdelete $x{$_};\n\t} else {\n\t\t$x{$_} = 1;\n\t\t$k = keys %x;\n\t\t$ans = $k > $ans? $k: $ans;\n\t}\n}\nprint $ans;"}, {"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\nchomp(my $n = <>);\nmy $ans = 0;\nmy $hashKeyCount;\nmy %h;\nchomp(my $line = <>);\nmy @socks = split(/ /,$line);\nforeach(@socks){\n if(defined $h{$_}){\n delete $h{$_};\n }else{\n $h{$_} = 1;\n $hashKeyCount = keys %h;\n $ans = $hashKeyCount > $ans ? $hashKeyCount : $ans;\n }\n}\nprint \"$ans\\n\";\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\nuse strict;\nuse warnings;\nchomp(my $n = <>);\nmy $ans = 0;\nmy $tableCount = 0;\nmy %h;\nchomp(my $line = <>);\nmy @socks = split(/ /,$line);\nforeach(@socks){\n\tif(defined $h{$_}){\n\t\tif($tableCount > $ans){\n\t\t\t$ans = $tableCount;\n\t\t\t$tableCount -= 1;\n\t\t}\n\t}else{\n\t\t$h{$_} = 1;\n\t\t$tableCount++;\n\t}\n}\nprint \"$ans\\n\";\n"}], "src_uid": "7f98c9258f3e127a782041c421d6317b"} {"nl": {"description": "User ainta has a permutation p1,\u2009p2,\u2009...,\u2009pn. As the New Year is coming, he wants to make his permutation as pretty as possible.Permutation a1,\u2009a2,\u2009...,\u2009an is prettier than permutation b1,\u2009b2,\u2009...,\u2009bn, if and only if there exists an integer k (1\u2009\u2264\u2009k\u2009\u2264\u2009n) where a1\u2009=\u2009b1,\u2009a2\u2009=\u2009b2,\u2009...,\u2009ak\u2009-\u20091\u2009=\u2009bk\u2009-\u20091 and ak\u2009<\u2009bk all holds.As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n\u2009\u00d7\u2009n binary matrix A, user ainta can swap the values of pi and pj (1\u2009\u2264\u2009i,\u2009j\u2009\u2264\u2009n, i\u2009\u2260\u2009j) if and only if Ai,\u2009j\u2009=\u20091.Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain.", "input_spec": "The first line contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009300) \u2014 the size of the permutation p. The second line contains n space-separated integers p1,\u2009p2,\u2009...,\u2009pn \u2014 the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai,\u2009j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i,\u2009j where 1\u2009\u2264\u2009i\u2009<\u2009j\u2009\u2264\u2009n, Ai,\u2009j\u2009=\u2009Aj,\u2009i holds. Also, for all integers i where 1\u2009\u2264\u2009i\u2009\u2264\u2009n, Ai,\u2009i\u2009=\u20090 holds.", "output_spec": "In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.", "sample_inputs": ["7\n5 2 4 3 6 7 1\n0001001\n0000000\n0000010\n1000001\n0000000\n0010000\n1001000", "5\n4 2 1 5 3\n00100\n00011\n10010\n01101\n01010"], "sample_outputs": ["1 2 4 3 6 7 5", "1 2 3 4 5"], "notes": "NoteIn the first sample, the swap needed to obtain the prettiest permutation is: (p1,\u2009p7).In the second sample, the swaps needed to obtain the prettiest permutation is (p1,\u2009p3),\u2009(p4,\u2009p5),\u2009(p3,\u2009p4). A permutation p is a sequence of integers p1,\u2009p2,\u2009...,\u2009pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n."}, "positive_code": [{"source_code": "use warnings; use strict;\nmy $n = scalar <> + 0;\nmy @aa = split ' ', scalar <>; $_ += 0 foreach @aa;\n@aa == $n or die \"err\";\nmy @m = <>; \n@m == $n or die \"err\";\nforeach (@m){ chomp; $_ = [ split '', $_ ]; foreach (@$_) { $_ += 0; } @$_ == $n or die \"err\"; }\nmy @used = (0) x $n;\nmy @comp = ();\nmy @cc = ();\nsub dfs{ my $i = shift; \n return if $used[$i];\n $used[$i] = 1; push @cc, $i; \n foreach (0 .. ($n-1)) {\n dfs($_) if $m[$i][$_] == 1 || $m[$_][$i] == 1;\n }\n }\nsub numeric {$a <=> $b}\nforeach (0 .. ($n-1)){\n dfs($_);\n #print join(' ', @cc), \"\\n\";\n push @comp, [sort numeric @cc] if @cc;\n @cc = ();\n }\nforeach (@comp){\n #print join(' ', @$), \"\\n\";\n @aa[@$_] = sort numeric @aa[@$_];\n }\nprint join ' ', @aa;\nprint \"\\n\";\n"}, {"source_code": "$n = <>;\n$_ = <>;\n@_ = (0, split);\n@m = (0, <>);\nfor (1 .. $n){\n\tnext if $h{$_};\n\t@pos = $_;\n\tdo {\n\t\t$p = shift @pos;\n\t\t$h{ pos $m[$p] } //= 0 * ( push @pos, pos $m[$p] )\n\t\twhile $m[$p] =~ /1/g\n\t} while @pos;\n\t@h = @n = ( );\n\t$h{$_} ||= 1 * ( push @h, $_ ) for keys %h;\n\t@h = sort {$a <=> $b} @h;\n\tpush @n, $_[$_] for @h;\n\t@n = sort {$a <=> $b} @n;\n\t$_[$_] = shift @n for @h;\n\t}\n\t\t\nshift @_;\nprint \"@_\""}, {"source_code": "$\\ = $/;\nwhile ($n = <>){\n\t$_ = <>;\n\t@_ = (0, split);\n\tundef %h;\n\tpush @m, 0, <>;\n\tfor (1 .. $n){\n\t\t$h{$_} eq 1 and next;\n\t\t@pos = ( );\n\t\twhile ($m[$_] =~ /1/g){\n\t\t\t$h{ pos $m[$_] } // ( push @pos, pos $m[$_] );\n\t\t\t$h{ pos $m[$_] } //= 0;\n\t\t\t}\n\t\twhile (@pos){\n\t\t\t$p = shift @pos;\n\t\t\twhile ($m[$p] =~ /1/g){\n\t\t\t\t$h{ pos $m[$p] } // ( push @pos, pos $m[$p] );\n\t\t\t\t$h{ pos $m[$p] } //= 0;\n\t\t\t\t}\n\t\t\t\n\t\t}\n\t\t@h = ( );\n\t\t$h{$_} or push @h, $_ for keys %h;\n\t\t$h{$_} ||= 1 for keys %h;\n\t\t@h = sort {$a <=> $b} @h;\n\t\t@n = ( );\n\t\tpush @n, $_[$_] for @h;\n\t\t@n = sort {$a <=> $b} @n;\n\t\t$_[$_] = shift @n for @h;\n\t\t}\n\t\t\n\tshift @_;\n\tprint \"@_\";\n\t}"}, {"source_code": "$\\ = $/;\nwhile ($n = <>){\n\t$_ = <>;\n\t@_ = split;\n\tunshift @_, -1;\n\tundef %h;\n\tpush @m, 0, <>;\n\tfor (1 .. $n){\n\t#\tprint \"[[$_]]\";\n\t\t$h{$_} eq 1 and next;\n\t\t@pos = ( );\n\t\twhile ($m[$_] =~ /1/g){\n\t#\t\tprint \"[\",pos $m[$_],\"]\";\n\t\t\t$h{ pos $m[$_] } // ( push @pos, pos $m[$_] );\n\t\t\t$h{ pos $m[$_] } //= 0;\n\t\t\t}\n\t#\tprint \"@pos|\";\n\t\twhile (@pos){\n\t#\t\tprint \"<@pos>\";\n\t\t\t$p = shift @pos;\n\t\t\twhile ($m[$p] =~ /1/g){\n\t\t\t\t$h{ pos $m[$p] } // ( push @pos, pos $m[$p] );\n\t\t\t\t$h{ pos $m[$p] } //= 0;\n\t\t\t\t}\n\t\t\t\n\t\t}\n\t\t$, = $\";\n\t#\tprint keys %h, values %h;\n\t\t@h = ( );\n\t\t$h{$_} or push @h, $_ for keys %h;\n\t\t$h{$_} ||= 1 for keys %h;\n\t\t@h = sort {$a <=> $b} @h;\n\t#\tprint \"@h\";\n\t\t@n = ( );\n\t\tpush @n, $_[$_] for @h;\n\t\t@n = sort {$a <=> $b} @n;\n\t\t$_[$_] = shift @n for @h;\n\t\t}\n\t\t\n#\tprint \"((@_))\";\n\tshift @_;\n\tprint \"@_\";\n\t}"}], "negative_code": [{"source_code": "use warnings; use strict;\nmy $n = scalar <> + 0;\nmy @a = split ' ', scalar <>; $_ += 0 foreach @a;\n@a == $n or die \"err\";\nmy @m = <>; \n@m == $n or die \"err\";\nforeach (@m){ chomp; $_ = [ split '', $_ ]; foreach (@$_) { $_ += 0; } @$_ == $n or die \"err\"; }\nmy @used = (0) x $n;\nmy @comp = ();\nmy @cc = ();\nsub dfs{ my $i = shift; \n return if $used[$i];\n $used[$i] = 1; push @cc, $i; \n foreach (0 .. ($n-1)) {\n dfs($_) if $m[$i][$_] || $m[$_][$i];\n }\n }\nforeach (0 .. ($n-1)){\n dfs($_);\n push @comp, [sort @cc] if @cc;\n @cc = ();\n }\n@a[@$_] = sort @a[@$_] foreach (@comp);\nprint join ' ', @a;\nprint \"\\n\";\n"}, {"source_code": "use warnings; use strict;\nmy $n = scalar <> + 0;\nmy @a = split ' ', scalar <>; $_ += 0 foreach @a;\n@a == $n or die \"err\";\nmy @m = <>; \n@m == $n or die \"err\";\nforeach (@m){ chomp; $_ = [ split '', $_ ]; foreach (@$_) { $_ += 0; } }\nmy @used = (0) x $n;\nmy @comp = ();\nmy @cc = ();\nsub dfs{ my $i = shift; \n return if $used[$i];\n $used[$i] = 1; push @cc, $i; \n foreach (0 .. ($n-1)) {\n dfs($_) if $m[$i][$_];\n }\n }\nforeach (0 .. ($n-1)){\n dfs($_);\n push @comp, [sort @cc] if @cc;\n @cc = ();\n }\n@a[@$_] = sort @a[@$_] foreach (@comp);\nprint join ' ', @a;\nprint \"\\n\";\n"}, {"source_code": "use warnings; use strict;\nmy $n = scalar <> + 0;\nmy @a = split ' ', scalar <>; $_ += 0 foreach @a;\n@a == $n or die \"err\";\nmy @m = <>; \n@m == $n or die \"err\";\nforeach (@m){ chomp; $_ = [ split '', $_ ]; foreach (@$_) { $_ += 0; } @$_ == $n or die \"err\"; }\nmy @used = (0) x $n;\nmy @comp = ();\nmy @cc = ();\nsub dfs{ my $i = shift; \n return if $used[$i];\n $used[$i] = 1; push @cc, $i; \n foreach (0 .. ($n-1)) {\n dfs($_) if $m[$i][$_] == 1 || $m[$_][$i] == 1;\n }\n }\nforeach (0 .. ($n-1)){\n dfs($_);\n print join(' ', @cc), \"\\n\";\n push @comp, [sort {$a <=> $b} @cc] if @cc;\n @cc = ();\n }\nforeach (@comp){\n print join(' ', @$), \"\\n\";\n @a[@$_] = sort @a[@$_];\n }\nprint join ' ', @a;\nprint \"\\n\";\n"}, {"source_code": "use warnings; use strict;\nmy $n = scalar <> + 0;\nmy @aa = split ' ', scalar <>; $_ += 0 foreach @aa;\n@aa == $n or die \"err\";\nmy @m = <>; \n@m == $n or die \"err\";\nforeach (@m){ chomp; $_ = [ split '', $_ ]; foreach (@$_) { $_ += 0; } @$_ == $n or die \"err\"; }\nmy @used = (0) x $n;\nmy @comp = ();\nmy @cc = ();\nsub dfs{ my $i = shift; \n return if $used[$i];\n $used[$i] = 1; push @cc, $i; \n foreach (0 .. ($n-1)) {\n dfs($_) if $m[$i][$_] == 1 || $m[$_][$i] == 1;\n }\n }\nforeach (0 .. ($n-1)){\n dfs($_);\n #print join(' ', @cc), \"\\n\";\n push @comp, [sort {$a <=> $b} @cc] if @cc;\n @cc = ();\n }\nforeach (@comp){\n #print join(' ', @$), \"\\n\";\n @aa[@$_] = sort @aa[@$_];\n }\nprint join ' ', @aa;\nprint \"\\n\";\n"}, {"source_code": "use warnings; use strict;\nmy $n = scalar <> + 0;\nmy @a = split ' ', scalar <>; $_ += 0 foreach @a;\nmy @m = <>; $#m = $n-1;\nforeach (@m){ chomp; $_ = [ split '', $_ ]; foreach (@$_) { $_ += 0; } }\nmy @used = (0) x $n;\nmy @comp = ();\nmy @cc = ();\nsub dfs{ my $i = shift; \n return if $used[$i];\n $used[$i] = 1; push @cc, $i; \n foreach (0 .. ($n-1)) {\n dfs($_) if $m[$i][$_] || $m[$i][$_];\n }\n }\nforeach (0 .. ($n-1)){\n dfs($_);\n push @comp, [sort @cc] if @cc;\n @cc = ();\n }\n@a[@$_] = sort @a[@$_] foreach (@comp);\nprint join ' ', @a;\nprint \"\\n\";\n"}], "src_uid": "a67ea891cd6084ceeaace8894cf18e60"} {"nl": {"description": "Let $$$f(i)$$$ denote the minimum positive integer $$$x$$$ such that $$$x$$$ is not a divisor of $$$i$$$.Compute $$$\\sum_{i=1}^n f(i)$$$ modulo $$$10^9+7$$$. In other words, compute $$$f(1)+f(2)+\\dots+f(n)$$$ modulo $$$10^9+7$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1\\leq t\\leq 10^4$$$), the number of test cases. Then $$$t$$$ cases follow. The only line of each test case contains a single integer $$$n$$$ ($$$1\\leq n\\leq 10^{16}$$$).", "output_spec": "For each test case, output a single integer $$$ans$$$, where $$$ans=\\sum_{i=1}^n f(i)$$$ modulo $$$10^9+7$$$.", "sample_inputs": ["6\n1\n2\n3\n4\n10\n10000000000000000"], "sample_outputs": ["2\n5\n7\n10\n26\n366580019"], "notes": "NoteIn the fourth test case $$$n=4$$$, so $$$ans=f(1)+f(2)+f(3)+f(4)$$$. $$$1$$$ is a divisor of $$$1$$$ but $$$2$$$ isn't, so $$$2$$$ is the minimum positive integer that isn't a divisor of $$$1$$$. Thus, $$$f(1)=2$$$. $$$1$$$ and $$$2$$$ are divisors of $$$2$$$ but $$$3$$$ isn't, so $$$3$$$ is the minimum positive integer that isn't a divisor of $$$2$$$. Thus, $$$f(2)=3$$$. $$$1$$$ is a divisor of $$$3$$$ but $$$2$$$ isn't, so $$$2$$$ is the minimum positive integer that isn't a divisor of $$$3$$$. Thus, $$$f(3)=2$$$. $$$1$$$ and $$$2$$$ are divisors of $$$4$$$ but $$$3$$$ isn't, so $$$3$$$ is the minimum positive integer that isn't a divisor of $$$4$$$. Thus, $$$f(4)=3$$$. Therefore, $$$ans=f(1)+f(2)+f(3)+f(4)=2+3+2+3=10$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\r\nuse Data::Dumper;\r\nuse integer; ### important!\r\nmy $mod = 10 ** 9 + 7;\r\n# my $mod = 998244353;\r\n\r\nmy ($test_case) = map { $_ - 0 } split(/\\s+/,);\r\n\r\n\r\nmy @m = (); $#m = 41;\r\n\r\nmy $fa = +{};\r\nfor(my $i=2;$i<=41;$i++){\r\n my $f = &factr($i);\r\n foreach my $f1 ( map { $_ - 0 } keys %{$f} ){\r\n if( $f1 > 1 and $fa->{$f1} - 0 < $f->{$f1} - 0 ){\r\n $fa->{$f1} = $f->{$f1};\r\n }\r\n }\r\n my $m = 1;\r\n foreach my $f1 ( map { $_ - 0 } keys %{$fa} ){\r\n $m *= ( $f1 ** $fa->{$f1} );\r\n }\r\n $m[$i] = $m;\r\n}\r\n\r\nwhile( $test_case -- > 0 ){\r\n my ($n) = map { $_ - 0 } split(/\\s+/,);\r\n my $r = 0;\r\n my $m_pr = 1;\r\n my $fa = +{};\r\n for(my $i=2;$i<=41;$i++){\r\n my $m = $m[$i];\r\n next if $m == $m_pr;\r\n my $ii = $m / $m_pr;\r\n my $k1 = $n / $ii;\r\n my $k2 = $k1 * ($ii-1);\r\n my $k3 = $n - ($k1 * $ii);\r\n my $k4 = $k2 + $k3;\r\n $r = ( $r + ( $i * $k4 ) % $mod ) % $mod;\r\n $n = $k1;\r\n $m_pr = $m;\r\n }\r\n print \"$r\\n\";\r\n}\r\n\r\nexit(0);\r\n\r\nsub factr { # factorialize\r\n my ($v) = @_;\r\n my %f = ();\r\n if( $v<4 ){\r\n $f{$v}++;\r\n return \\%f;\r\n }\r\n for(my $i=1;$i*$i<=$v;$i+=2){\r\n my $ii = ($i == 1 ? 2 : $i);\r\n while($v % $ii == 0){\r\n $f{$ii}++;\r\n $v /= $ii;\r\n }\r\n }\r\n $f{$v}++ if $v>1;\r\n return \\%f;\r\n}\r\n"}], "negative_code": [], "src_uid": "2b15a299c25254f265cce106dffd6174"} {"nl": {"description": "You and your friend are participating in a TV show \"Run For Your Prize\".At the start of the show n prizes are located on a straight line. i-th prize is located at position ai. Positions of all prizes are distinct. You start at position 1, your friend \u2014 at position 106 (and there is no prize in any of these two positions). You have to work as a team and collect all prizes in minimum possible time, in any order.You know that it takes exactly 1 second to move from position x to position x\u2009+\u20091 or x\u2009-\u20091, both for you and your friend. You also have trained enough to instantly pick up any prize, if its position is equal to your current position (and the same is true for your friend). Carrying prizes does not affect your speed (or your friend's speed) at all.Now you may discuss your strategy with your friend and decide who will pick up each prize. Remember that every prize must be picked up, either by you or by your friend.What is the minimum number of seconds it will take to pick up all the prizes?", "input_spec": "The first line contains one integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009105) \u2014 the number of prizes. The second line contains n integers a1, a2, ..., an (2\u2009\u2264\u2009ai\u2009\u2264\u2009106\u2009-\u20091) \u2014 the positions of the prizes. No two prizes are located at the same position. Positions are given in ascending order.", "output_spec": "Print one integer \u2014 the minimum number of seconds it will take to collect all prizes.", "sample_inputs": ["3\n2 3 9", "2\n2 999995"], "sample_outputs": ["8", "5"], "notes": "NoteIn the first example you take all the prizes: take the first at 1, the second at 2 and the third at 8.In the second example you take the first prize in 1 second and your friend takes the other in 5 seconds, you do this simultaneously, so the total time is 5."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = split ' ', <>;\n\t\n\tmy @A;\n\t\n\tfor( @_ ){\n\t\tpush @A, ( sort { $a <=> $b } $_ - 1, 1e6 - $_ )[ 0 ];\n\t\t}\n\t\n\tprint +( sort { $b <=> $a } @A )[ 0 ];\n\t}"}, {"source_code": "<>;\n\nfor( split ' ', <> ){\n\t$m = $_ - 1 < 1e6 - $_ ? $_ - 1 : 1e6 - $_;\n\t$m > $M and $M = $m;\n\t}\n\nprint $M"}], "negative_code": [], "src_uid": "f530e6f720dafaf8bff9324289b90b28"} {"nl": {"description": "A $$$\\mathbf{0}$$$-indexed array $$$a$$$ of size $$$n$$$ is called good if for all valid indices $$$i$$$ ($$$0 \\le i \\le n-1$$$), $$$a_i + i$$$ is a perfect square$$$^\\dagger$$$.Given an integer $$$n$$$. Find a permutation$$$^\\ddagger$$$ $$$p$$$ of $$$[0,1,2,\\ldots,n-1]$$$ that is good or determine that no such permutation exists.$$$^\\dagger$$$ An integer $$$x$$$ is said to be a perfect square if there exists an integer $$$y$$$ such that $$$x = y^2$$$.$$$^\\ddagger$$$ 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 \\le t \\le 10^4$$$) \u2014 the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) \u2014 the length of the permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, output $$$n$$$ distinct integers $$$p_0, p_1, \\dots, p_{n-1}$$$ ($$$0 \\le p_i \\le n-1$$$) \u2014 the permutation $$$p$$$ \u2014 if the answer exists, and $$$-1$$$ otherwise.", "sample_inputs": ["3\n\n3\n\n4\n\n7"], "sample_outputs": ["1 0 2 \n0 3 2 1 \n1 0 2 6 5 4 3"], "notes": "NoteIn the first test case, we have $$$n=3$$$. The array $$$p = [1, 0, 2]$$$ is good since $$$1 + 0 = 1^2$$$, $$$0 + 1 = 1^2$$$, and $$$2 + 2 = 2^2$$$In the second test case, we have $$$n=4$$$. The array $$$p = [0, 3, 2, 1]$$$ is good since $$$0 + 0 = 0^2$$$, $$$3 + 1 = 2^2$$$, $$$2+2 = 2^2$$$, and $$$1+3 = 2^2$$$."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nno warnings 'uninitialized';\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tchomp;\n\t\n\t$_ --;\n\t\n\tmy @squares;\n\t\n\tmy $i = 0;\n\t\n\twhile( $i ** 2 < $_ ){\n\t\tpush @squares, $i ** 2;\n\t\t$i ++;\n\t\t}\n\t\n\tpush @squares, $i ** 2;\n\t\n\t$debug and print \"$_:@squares\";\n\t\n\tmy @A = ( -1 ) x ( 1 + $_ );\n\t\n\tfor my $i ( reverse 0 .. $_ ){\n\t\tfor my $sq ( reverse @squares ){\n\t\t\tif( $A[ $sq - $i ] == -1 ){\n\t\t\t\t$A[ $sq - $i ] = $i;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print map \"[$_]\", join ',', @A;\n\t\n\tprint \"@A\";\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\nno warnings 'uninitialized';\n\nmy $debug = 0;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t\n\tchomp;\n\t\n\tmy @squares;\n\t\n\tmy $i = 0;\n\t\n\twhile( $i ** 2 < $_ ){\n\t\tpush @squares, $i ** 2;\n\t\t$i ++;\n\t\t}\n\t\n\tpush @squares, $i ** 2;\n\t\n\t$debug and print \"$_:@squares\";\n\t\n\tmy @A = ( -1 ) x ( 1 + $_ );\n\t\n\tfor my $i ( reverse 0 .. $_ ){\n\t\tfor my $sq ( reverse @squares ){\n\t\t\tif( $A[ $sq - $i ] == -1 ){\n\t\t\t\t$A[ $sq - $i ] = $i;\n\t\t\t\tlast;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\n\t$debug and print map \"[$_]\", join ',', @A;\n\t\n\tprint \"@A\";\n\t}"}], "src_uid": "f7ed88c0f33ad9cb1ede2abf185d9ece"} {"nl": {"description": "You are given sequence a1,\u2009a2,\u2009...,\u2009an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You should write a program which finds sum of the best subsequence.", "input_spec": "The first line contains integer number n (1\u2009\u2264\u2009n\u2009\u2264\u2009105). The second line contains n integer numbers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009104\u2009\u2264\u2009ai\u2009\u2264\u2009104). The sequence contains at least one subsequence with odd sum.", "output_spec": "Print sum of resulting subseqeuence.", "sample_inputs": ["4\n-2 2 -3 1", "3\n2 -5 -3"], "sample_outputs": ["3", "-1"], "notes": "NoteIn the first example sum of the second and the fourth elements is 3."}, "positive_code": [{"source_code": "=item\n797 B\n\nYou are given sequence a1,\u2009a2,\u2009...,\u2009an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.\n\nSubsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.\n\nYou should write a program which finds sum of the best subsequence.\nInput\n\nThe first line contains integer number n (1\u2009\u2264\u2009n\u2009\u2264\u2009105).\n\nThe second line contains n integer numbers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009104\u2009\u2264\u2009ai\u2009\u2264\u2009104). The sequence contains at least one subsequence with odd sum.\nOutput\n\nPrint sum of resulting subseqeuence.\nExamples\nInput\n\n4\n-2 2 -3 1\n\nOutput\n\n3\n\nInput\n\n3\n2 -5 -3\n\nOutput\n\n-1\n=cut\n\nmy $n=<>;\nchomp $n;\nmy @vs=split(' ',<>);\n# print \"# $n : \",join('|',@vs),\"\\n\";\n\nmy @ps;\nmy @ns;\n\nmy $spos=0;\nfor(my $i=0;$i<$n;++$i) {\n my $v=$vs[$i];\n if($v>0) {\n push @ps,$v;\n $spos+=$v;\n } else {\n push @ns,$v;\n }\n}\nif(($spos%2)==1) {\n print \"$spos\\n\";\n exit();\n}\n# print \"# spos=$spos : ps=@ps : ns=@ns\\n\";\n@ops=sort {$a-$b} grep {$_%2==1} @ps;\n@ons=sort {$a-$b} grep {$_%2==1} @ns;\n# print \"# ops=@ops : ons=@ons\\n\";\n\nmy $mp=scalar(@ops)>0 ? $ops[0] : 1e20;\nmy $mn=scalar(@ons)>0 ? -$ons[-1] : 1e20;\n\n$m=($mp<$mn ? $mp : $mn);\n# print \"# mp=$mp mn=$mn m=$m\\n\";\n$spos-=$m;\nprint \"$spos\\n\";\n"}, {"source_code": "\n#!/usr/bin/perl\n\n$n = <>;\nchomp($n);\n\n$t = <>;\nchomp($t);\n@a = split(' ', $t);\n$max = -1e5;\n$min = 1e5;\n$sum = $x = $y = 0;\nfor ($i = 0; $i < $n; $i++){\n if(@a[$i] % 2 == 0){\n if(@a[$i] > 0){$sum += @a[$i];}\n }\n else {\n if(@a[$i] > 0){$sum += @a[$i]; $min = $min > @a[$i] ? @a[$i] : $min; $x++;}\n else {$max = $max < @a[$i] ? @a[$i] : $max; $y++;}\n }\n}\nif($x == 0) {$sum += $max;}\nelsif($x % 2 == 0){\n if($y == 0){$sum -= $min;}\n else {\n $sum += ($min > -1 * $max) ? ($max) : (-1 * $min);\n}}\nprint \"$sum\";\n"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\n$n = <>;\nchomp($n);\n\n$t = <>;\nchomp($t);\n@a = split(' ', $t);\n$max = -1e5;\n$min = 1e5;\n$sum = $x = $y = 0;\nfor ($i = 0; $i < $n; $i++){\n if(@a[$i] % 2 == 0){\n if(@a[$i] > 0){$sum += @a[$i];}\n }\n else {\n if(@a[$i] > 0){$sum += @a[$i]; $min = $min > @a[$i] ? @a[$i] : $min; $x++;}\n else {$max = $max < @a[$i] ? @a[$i] : $max; $y++;}\n }\n}\nif($x == 0) {$sum += $max;}\nelsif($x % 2 == 0){\n if($y == 0){$sum -= $min;}\n else {\n $sum += ($max + $min >= -1 * $min) ? ($max + $min) : (-1 * $min)};}\nprint \"$sum\";\n"}, {"source_code": "#!/usr/bin/perl\n\n$n = <>;\nchomp($n);\n\n$t = <>;\nchomp($t);\n@a = split(' ', $t);\n$max = -1e5;\n$min = 1e5;\n$sum = $x = $y = 0;\nfor ($i = 0; $i < $n; $i++){\n if(@a[$i] % 2 == 0){\n if(@a[$i] > 0){$sum += @a[$i];}\n }\n else {\n if(@a[$i] > 0){$sum += @a[$i]; $min = $min > @a[$i] ? @a[$i] : $min; $x++;}\n else {$max = $max < @a[$i] ? @a[$i] : $max; $y++;}\n }\n}\nif($x == 0) {$sum += $max;}\nelsif($x % 2 == 0){$sum += ($max + $min >= 0) ? ($max + $min) : (-1 * $min)};\nprint \"$sum\";\n"}, {"source_code": "#!/usr/bin/perl\n\n$n = <>;\nchomp($n);\n\n$t = <>;\nchomp($t);\n@a = split(' ', $t);\n$max = -1e5;\n$min = 1e5;\n$sum = $x = $y = 0;\nfor ($i = 0; $i < $n; $i++){\n if(@a[$i] % 2 == 0){\n if(@a[$i] > 0){$sum += @a[$i];}\n }\n else {\n if(@a[$i] > 0){$sum += @a[$i]; $min = $min > @a[$i] ? @a[$i] : $min; $x++;}\n else {$max = $max < @a[$i] ? @a[$i] : $max; $y++;}\n }\n}\nif($x == 0) {$sum += $max;}\nelsif($x % 2 == 0){$sum += ($max + $min >= -1 * $min) ? ($max + $min) : (-1 * $min)};\nprint \"$sum\";\n"}, {"source_code": "=item\n797 B\n\nYou are given sequence a1,\u2009a2,\u2009...,\u2009an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.\n\nSubsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.\n\nYou should write a program which finds sum of the best subsequence.\nInput\n\nThe first line contains integer number n (1\u2009\u2264\u2009n\u2009\u2264\u2009105).\n\nThe second line contains n integer numbers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009104\u2009\u2264\u2009ai\u2009\u2264\u2009104). The sequence contains at least one subsequence with odd sum.\nOutput\n\nPrint sum of resulting subseqeuence.\nExamples\nInput\n\n4\n-2 2 -3 1\n\nOutput\n\n3\n\nInput\n\n3\n2 -5 -3\n\nOutput\n\n-1\n=cut\n\nmy $n=<>;\nchomp $n;\nmy @vs=split(' ',<>);\n# print \"# $n : \",join('|',@vs),\"\\n\";\n\nmy @ps;\nmy @ns;\n\nmy $spos=0;\nfor(my $i=0;$i<$n;++$i) {\n my $v=$vs[$i];\n if($v>0) {\n push @ps,$v;\n $spos+=$v;\n } else {\n push @ns,$v;\n }\n}\nif($spos % 2 ==1) {\n print \"$spos\\n\";\n exit();\n}\n# print \"# spos=$spos : ps=@ps : ns=@ns\\n\";\n@ops=sort {$a-$b} grep {$_%2==1} @ps;\n@ons=sort {$a-$b} grep {$_%2==1} @ns;\n# print \"# ops=@ops : ons=@ons\\n\";\n\nmy $mp=scalar(@ops)>0 ? $ops[0] : 0;\nmy $mn=scalar(@ons)>0 ? -$ons[-1] : 0;\n\n$m=((0<$mp and $mp<$mn) ? $mp : $mn);\n# print \"# mp=$mp mn=$mn m=$m\\n\";\n$spos-=$m;\nprint \"$spos\\n\";\n"}, {"source_code": "=item\n797 B\n\nYou are given sequence a1,\u2009a2,\u2009...,\u2009an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.\n\nSubsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.\n\nYou should write a program which finds sum of the best subsequence.\nInput\n\nThe first line contains integer number n (1\u2009\u2264\u2009n\u2009\u2264\u2009105).\n\nThe second line contains n integer numbers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009104\u2009\u2264\u2009ai\u2009\u2264\u2009104). The sequence contains at least one subsequence with odd sum.\nOutput\n\nPrint sum of resulting subseqeuence.\nExamples\nInput\n\n4\n-2 2 -3 1\n\nOutput\n\n3\n\nInput\n\n3\n2 -5 -3\n\nOutput\n\n-1\n=cut\n\nmy $n=<>;\nchomp $n;\nmy @vs=split(' ',<>);\nprint \"# $n : \",join('|',@vs),\"\\n\";\n\nmy @ps;\nmy @ns;\n\nmy $spos=0;\nfor(my $i=0;$i<$n;++$i) {\n my $v=$vs[$i];\n if($v>0) {\n push @ps,$v;\n $spos+=$v;\n } else {\n push @ns,$v;\n }\n}\nif($spos % 2 ==1) {\n print \"$spos\\n\";\n exit();\n}\nprint \"# spos=$spos : ps=@ps : ns=@ns\\n\";\n@ops=sort {$a-$b} grep {$_%2==1} @ps;\n@ons=sort {$a-$b} grep {$_%2==1} @ns;\nprint \"# ops=@ops : ons=@ons\\n\";\n\nmy $mp=scalar(@ops)>0 ? $ops[0] : 0;\nmy $mn=scalar(@ons)>0 ? -$ons[-1] : 0;\n\n$m=((0<$mp and $mp<$mn) ? $mp : $mn);\nprint \"# mp=$mp mn=$mn m=$m\\n\";\n$spos-=$m;\nprint \"$spos\\n\";\n"}, {"source_code": "=item\n797 B\n\nYou are given sequence a1,\u2009a2,\u2009...,\u2009an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.\n\nSubsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.\n\nYou should write a program which finds sum of the best subsequence.\nInput\n\nThe first line contains integer number n (1\u2009\u2264\u2009n\u2009\u2264\u2009105).\n\nThe second line contains n integer numbers a1,\u2009a2,\u2009...,\u2009an (\u2009-\u2009104\u2009\u2264\u2009ai\u2009\u2264\u2009104). The sequence contains at least one subsequence with odd sum.\nOutput\n\nPrint sum of resulting subseqeuence.\nExamples\nInput\n\n4\n-2 2 -3 1\n\nOutput\n\n3\n\nInput\n\n3\n2 -5 -3\n\nOutput\n\n-1\n=cut\n\nmy $n=<>;\nchomp $n;\nmy @vs=split(' ',<>);\nprint \"# $n : \",join('|',@vs),\"\\n\";\n\nmy $m=-1e20;\nfor(my $i=0;$i<$n;++$i) {\n my $s=0;\n for(my $j=$i;$j<$n;++$j) {\n $s+=$vs[$j];\n if($s % 2 == 1 and $s>$m) {\n $m=$s;\n }\n }\n}\nprint \"$m\\n\";"}, {"source_code": "#!/usr/bin/perl\n\n$n = <>;\nchomp($n);\n\n$t = <>;\nchomp($t);\n@a = split(' ', $t);\n$max = -1e5;\n$min = 1e5;\n$sum = $x = $y = 0;\nfor ($i = 0; $i < $n; $i++){\n if(@a[$i] % 2 == 0){\n if(@a[$i] > 0){$sum += @a[$i];}\n }\n else {\n if(@a[$i] > 0){$sum += @a[$i]; $min = $min > @a[$i] ? @a[$i] : $min; $x++;}\n else {$max = $max < @a[$i] ? @a[$i] : $max; $y++;}\n }\n}\nif($x == 0) {$sum += $max;}\nelsif($x % 2 == 0){\n if($y == 0){$sum -= $min;}\n else {\n $sum += ($max + $min >= -1 * $min) ? ($max) : (-1 * $min)};}\nprint \"$sum\";\n"}], "src_uid": "76dd49c5545770a1dfbb2da4140199c1"} {"nl": {"description": "You are given a permutation $$$a_1, a_2, \\ldots, a_n$$$ of size $$$n$$$, where each integer from $$$1$$$ to $$$n$$$ appears exactly once.You can do the following operation any number of times (possibly, zero): Choose any three indices $$$i, j, k$$$ ($$$1 \\le i < j < k \\le n$$$). If $$$a_i > a_k$$$, replace $$$a_i$$$ with $$$a_i + a_j$$$. Otherwise, swap $$$a_j$$$ and $$$a_k$$$. Determine whether you can make the array $$$a$$$ sorted in non-descending order.", "input_spec": "Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) \u2014 the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \\le n \\le 10$$$) \u2014 the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\\dots,a_n$$$ ($$$1 \\le a_i \\le n$$$, $$$a_i \\neq a_j$$$ if $$$i \\neq j$$$) \u2014 the elements of the array $$$a$$$.", "output_spec": "For each test case, output \"Yes\" (without quotes) if the array can be sorted in non-descending order, 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\n\n3\n\n1 2 3\n\n3\n\n1 3 2\n\n7\n\n5 3 4 7 6 2 1\n\n7\n\n7 6 5 4 3 2 1\n\n5\n\n2 1 4 5 3\n\n5\n\n2 1 3 4 5\n\n7\n\n1 2 6 7 4 3 5"], "sample_outputs": ["Yes\nYes\nNo\nNo\nNo\nNo\nYes"], "notes": "NoteIn the first test case, $$$[1,2,3]$$$ is already sorted in non-descending order.In the second test case, we can choose $$$i = 1,j = 2,k = 3$$$. Since $$$a_1 \\le a_3$$$, swap $$$a_2$$$ and $$$a_3$$$, the array then becomes $$$[1,2,3]$$$, which is sorted in non-descending order.In the seventh test case, we can do the following operations successively: Choose $$$i = 5,j = 6,k = 7$$$. Since $$$a_5 \\le a_7$$$, swap $$$a_6$$$ and $$$a_7$$$, the array then becomes $$$[1,2,6,7,4,5,3]$$$. Choose $$$i = 5,j = 6,k = 7$$$. Since $$$a_5 > a_7$$$, replace $$$a_5$$$ with $$$a_5+a_6=9$$$, the array then becomes $$$[1,2,6,7,9,5,3]$$$. Choose $$$i = 2,j = 5,k = 7$$$. Since $$$a_2 \\le a_7$$$, swap $$$a_5$$$ and $$$a_7$$$, the array then becomes $$$[1,2,6,7,3,5,9]$$$. Choose $$$i = 2,j = 4,k = 6$$$. Since $$$a_2 \\le a_6$$$, swap $$$a_4$$$ and $$$a_6$$$, the array then becomes $$$[1,2,6,5,3,7,9]$$$. Choose $$$i = 1,j = 3,k = 5$$$. Since $$$a_1 \\le a_5$$$, swap $$$a_3$$$ and $$$a_5$$$, the array then becomes $$$[1,2,3,5,6,7,9]$$$, which is sorted in non-descending order. In the third, the fourth, the fifth and the sixth test cases, it can be shown that the array cannot be sorted in non-descending order."}, "positive_code": [{"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nprint 1 - <> ? NO : YES while <>"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\n<>;\n\nwhile(<>){\n\tprint 1 - <> ? 'NO' : 'YES';\n\t}"}, {"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nprint 1 - <> ? NO : YES while <>"}, {"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nprint 1 - <> ? NO : YES while <>"}, {"source_code": "$\\ = $/;\r\n\r\n<>;\r\n\r\nprint 1 - <> ? NO : YES while <>"}], "negative_code": [], "src_uid": "76b3667cce9e23675e21caf6926f608d"} {"nl": {"description": "Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...$$$n$$$ friends live in a city which can be represented as a number line. The $$$i$$$-th friend lives in a house with an integer coordinate $$$x_i$$$. The $$$i$$$-th friend can come celebrate the New Year to the house with coordinate $$$x_i-1$$$, $$$x_i+1$$$ or stay at $$$x_i$$$. Each friend is allowed to move no more than once.For all friends $$$1 \\le x_i \\le n$$$ holds, however, they can come to houses with coordinates $$$0$$$ and $$$n+1$$$ (if their houses are at $$$1$$$ or $$$n$$$, respectively).For example, let the initial positions be $$$x = [1, 2, 4, 4]$$$. The final ones then can be $$$[1, 3, 3, 4]$$$, $$$[0, 2, 3, 3]$$$, $$$[2, 2, 5, 5]$$$, $$$[2, 1, 3, 5]$$$ and so on. The number of occupied houses is the number of distinct positions among the final ones.So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) \u2014 the number of friends. The second line contains $$$n$$$ integers $$$x_1, x_2, \\dots, x_n$$$ ($$$1 \\le x_i \\le n$$$) \u2014 the coordinates of the houses of the friends.", "output_spec": "Print two integers \u2014 the minimum and the maximum possible number of occupied houses after all moves are performed.", "sample_inputs": ["4\n1 2 4 4", "9\n1 1 8 8 8 4 4 4 4", "7\n4 3 7 1 4 3 3"], "sample_outputs": ["2 4", "3 8", "3 6"], "notes": "NoteIn the first example friends can go to $$$[2, 2, 3, 3]$$$. So friend $$$1$$$ goes to $$$x_1+1$$$, friend $$$2$$$ stays at his house $$$x_2$$$, friend $$$3$$$ goes to $$$x_3-1$$$ and friend $$$4$$$ goes to $$$x_4-1$$$. $$$[1, 1, 3, 3]$$$, $$$[2, 2, 3, 3]$$$ or $$$[2, 2, 4, 4]$$$ are also all valid options to obtain $$$2$$$ occupied houses.For the maximum number of occupied houses friends can go to $$$[1, 2, 3, 4]$$$ or to $$$[0, 2, 4, 5]$$$, for example."}, "positive_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t@_ = ( 0 ) x $_;\n\t\n\tfor( split ' ', <> ){\n\t\t$_[ $_ - 1 ] ++ if $_[ $_ - 1 ] < 3;\n\t\t}\n\t\n\t$_ = join '', 0, @_, 0;\n\t\n\t$debug and print \"....[$_]\";\n\t\n\tmy $copy = $_;\n\t\n\ty/1-9/1/;\n\t\n\tmy $cnt_min = 0;\n\t\n\t$debug and print \"min:[$_]:$cnt_min\";\n\t\n\t0 while 0\n\t\t|| s/0((?:1.1)+)/ $cnt_min += ( length $1 ) \\/ 3, '0X' /ge\n\t\t|| s/0110/ $cnt_min += 1, '0X0' /ge\n\t\t|| s/X110/ $cnt_min += 1, 'XX0' /ge\n\t\t;\n\t\n\t$debug and print \"min:[$_]:$cnt_min\";\n\t\n\t$cnt_min += () = /1/g;\n\t\n\t$_ = $copy;\n\t\n\tmy $cnt_max = 0;\n\t\n\ts/00\\K(0+)//g;\n\ts/11\\K(1+)/ $cnt_max += length $1, '' /ge;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\t0 while 0\n\t\t|| s/0(1+)/ $cnt_max += length $1, 0 /ge\n\t\t|| s/(1+)0/ $cnt_max += length $1, 0 /ge\n\t\t|| s/(1+)/ $cnt_max += length $1, '' /ge\n\t\t;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\t0 while 0\n\t\t|| s/[23][23]\\K([23]+)/ $cnt_max += length $1, '' /ge\n\t\t|| s/[23][23]/ $cnt_max ++, 3 /ge\n\t\t|| s/030/ $cnt_max += 3, 'X' /ge\n\t\t|| s/X((?:0[23])+)/ $cnt_max += length $1, 'X' /ge\n\t\t|| s/((?:0[23])+)X/ $cnt_max += length $1, 'X' /ge\n\t\t|| s/X2X|X3X/ $cnt_max += 1, 'X' /ge\n\t\t;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\t$cnt_max += 2 * ( () = /[23]/g );\n\t\n\tprint \"$cnt_min $cnt_max\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nwhile(<>){\n\t@_ = ( 0 ) x ( $_ + 2 );\n\t\n\t$_[ $_ ] < 3 and $_[ $_ ] ++ for split ' ', <>;\n\t\n\t$_ = join '', @_;\n\t\n\tmy $copy = $_;\n\t\n\ty/1-9/1/;\n\t\n\tmy( $cnt_min, $cnt_max ) = ( 0 ) x 2;\n\t\n\t0 while 0\n\t\t|| s/0((?:1.1)+)/ $cnt_min += ( length $1 ) \\/ 3, '0X' /ge\n\t\t|| s/[0X]\\K110/ $cnt_min += 1, 'X0' /ge\n\t\t;\n\t\n\t$cnt_min += () = /1/g;\n\t\n\t$_ = $copy;\n\t\n\t$cnt_max += s/1//g;\n\t\n\ts/[23]([23]+)/ $cnt_max += length $1, 3 /ge;\n\ts/(030|30|02|20)/ $cnt_max += length $1, '' /ge;\n\t\n\tprint \"$cnt_min $cnt_max\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t@_ = ( 0 ) x ( $_ + 2 );\n\t\n\t$_[ $_ ] < 3 and $_[ $_ ] ++ for split ' ', <>;\n\t\n\t$_ = join '', @_;\n\t\n\t$debug and print \"....[$_]\";\n\t\n\tmy $copy = $_;\n\t\n\ty/1-9/1/;\n\t\n\tmy( $cnt_min, $cnt_max ) = ( 0 ) x 2;\n\t\n\t$debug and print \"min:[$_]:$cnt_min\";\n\t\n\t0 while 0\n\t\t|| s/0((?:1.1)+)/ $cnt_min += ( length $1 ) \\/ 3, '0X' /ge\n\t\t|| s/[0X]\\K110/ $cnt_min += 1, 'X0' /ge\n\t\t;\n\t\n\t$debug and print \"min:[$_]:$cnt_min\";\n\t\n\t$cnt_min += () = /1/g;\n\t\n\t$_ = $copy;\n\t\n\t$cnt_max += s/1//g;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\ts/[23]([23]+)/ $cnt_max += length $1, 3 /ge;\n\ts/(030|30|02|20)/ $cnt_max += length $1, '' /ge;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\tprint \"$cnt_min $cnt_max\";\n\t}"}], "negative_code": [{"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t@_ = ( 0 ) x $_;\n\t\n\tfor( split ' ', <> ){\n\t\t$_[ $_ - 1 ] ++ if $_[ $_ - 1 ] < 3;\n\t\t}\n\t\n\t$_ = join '', 0, @_, 0;\n\t\n\t$debug and print \"....[$_]\";\n\t\n\tmy $copy = $_;\n\t\n\ty/1-9/1/;\n\t\n\tmy $cnt_min = 0;\n\t\n\t$debug and print \"min:[$_]\";\n\t\n\t0 while 0\n\t\t|| s/111/ $cnt_min += 1, '' /ge\n\t\t|| s/101/ $cnt_min += 1, '' /ge\n\t\t|| s/11/ $cnt_min += 1, '' /ge\n\t\t;\n\t\n\t$debug and print \"min:[$_]\";\n\t\n\t$cnt_min += () = /1/g;\n\t\n\t$_ = $copy;\n\t\n\tmy $cnt_max = 0;\n\t\n\ts/00\\K(0+)//g;\n\ts/11\\K(1+)/ $cnt_max += length $1, '' /ge;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\t0 while 0\n\t\t|| s/0(1+)/ $cnt_max += length $1, 0 /ge\n\t\t|| s/(1+)0/ $cnt_max += length $1, 0 /ge\n\t\t|| s/(1+)/ $cnt_max += length $1, '' /ge\n\t\t;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\t0 while 0\n\t\t|| s/[23][23]\\K([23]+)/ $cnt_max += length $1, '' /ge\n\t\t|| s/[23][23]/ $cnt_max ++, 3 /ge\n\t\t|| s/030/ $cnt_max += 3, 'X' /ge\n\t\t|| s/03X|X30|02X|X20/ $cnt_max += 2, 'X' /ge\n\t\t|| s/X2X|X3X/ $cnt_max += 1, 'X' /ge\n\t\t;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\t$cnt_max += 2 * ( () = /[23]/g );\n\t\n\tprint \"$cnt_min $cnt_max\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t@_ = ( 0 ) x $_;\n\t\n\tfor( split ' ', <> ){\n\t\t$_[ $_ - 1 ] ++ if $_[ $_ - 1 ] < 3;\n\t\t}\n\t\n\t$_ = join '', 0, @_, 0;\n\t\n\t$debug and print \"....[$_]\";\n\t\n\tmy $copy = $_;\n\t\n\ty/1-9/1/;\n\t\n\tmy $cnt_min = 0;\n\t\n\t$debug and print \"min:[$_]\";\n\t\n\t0 while 0\n\t\t|| s/111/ $cnt_min += 1, '' /ge\n\t\t|| s/101/ $cnt_min += 1, '' /ge\n\t\t|| s/11/ $cnt_min += 1, '' /ge\n\t\t;\n\t\n\t$debug and print \"min:[$_]\";\n\t\n\t$cnt_min += () = /1/g;\n\t\n\t$_ = $copy;\n\t\n\tmy $cnt_max = 0;\n\t\n\ts/00\\K(0+)//g;\n\ts/11\\K(1+)/ $cnt_max += length $1, '' /ge;\n\t\n\t$debug and print \"max:[$_]\";\n\t\n\t0 while 0\n\t\t|| s/0(1+)/ $cnt_max += length $1, 0 /ge\n\t\t|| s/(1+)0/ $cnt_max += length $1, 0 /ge\n\t\t|| s/(1+)/ $cnt_max += length $1, '' /ge\n\t\t;\n\t\n\t$debug and print \"max:[$_]\";\n\t\n\t0 while 0\n\t\t|| s/[23][23]\\K([23]+)/ $cnt_max += length $1, '' /ge\n\t\t|| s/[23][23]/ $cnt_max ++, 3 /ge\n\t\t|| s/030/ $cnt_max += 3, '' /ge\n\t\t|| s/030/111/g\n\t\t;\n\t\n\t$debug and print \"max:[$_]\";\n\t\n\t$cnt_max += 2 * ( () = /[^0]/g );\n\t\n\tprint \"$cnt_min $cnt_max\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t@_ = ( 0 ) x $_;\n\t\n\tfor( split ' ', <> ){\n\t\t$_[ $_ - 1 ] ++ if $_[ $_ - 1 ] < 3;\n\t\t}\n\t\n\t$_ = join '', 0, @_, 0;\n\t\n\t$debug and print \"....[$_]\";\n\t\n\tmy $copy = $_;\n\t\n\ty/1-9/1/;\n\t\n\tmy $cnt_min = 0;\n\t\n\t$debug and print \"min:[$_]\";\n\t\n\t0 while 0\n\t\t|| s/111/ $cnt_min += 1, '' /ge\n\t\t|| s/101/ $cnt_min += 1, '' /ge\n\t\t|| s/11/ $cnt_min += 1, '' /ge\n\t\t;\n\t\n\t$debug and print \"min:[$_]\";\n\t\n\t$cnt_min += () = /1/g;\n\t\n\t$_ = $copy;\n\t\n\tmy $cnt_max = 0;\n\t\n\ts/00\\K(0+)//g;\n\ts/11\\K(1+)/ $cnt_max += length $1, '' /ge;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\t0 while 0\n\t\t|| s/0(1+)/ $cnt_max += length $1, 0 /ge\n\t\t|| s/(1+)0/ $cnt_max += length $1, 0 /ge\n\t\t|| s/(1+)/ $cnt_max += length $1, '' /ge\n\t\t;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\t0 while 0\n\t\t|| s/[23][23]\\K([23]+)/ $cnt_max += length $1, '' /ge\n\t\t|| s/[23][23]/ $cnt_max ++, 3 /ge\n\t\t|| s/030/ $cnt_max += 3, 'X' /ge\n\t\t|| s/03X|X30|02X|X20/ $cnt_max += 2, 'X' /ge\n\t\t;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\t$cnt_max += 2 * ( () = /[23]/g );\n\t\n\tprint \"$cnt_min $cnt_max\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t@_ = ( 0 ) x $_;\n\t\n\tfor( split ' ', <> ){\n\t\t$_[ $_ - 1 ] ++ if $_[ $_ - 1 ] < 3;\n\t\t}\n\t\n\t$_ = join '', 0, @_, 0;\n\t\n\t$debug and print \"....[$_]\";\n\t\n\tmy $copy = $_;\n\t\n\ty/1-9/1/;\n\t\n\tmy $cnt_min = 0;\n\t\n\t$debug and print \"min:[$_]\";\n\t\n\t0 while 0\n\t\t|| s/0(1.1)+/ $cnt_min += ( length $1 ) \\/ 3, '0X' /ge\n\t\t|| s/0110/ $cnt_min += 1, '0X0' /ge\n\t\t|| s/X110/ $cnt_min += 1, 'XX0' /ge\n\t\t;\n\t\n\t$debug and print \"min:[$_]\";\n\t\n\t$cnt_min += () = /1/g;\n\t\n\t$_ = $copy;\n\t\n\tmy $cnt_max = 0;\n\t\n\ts/00\\K(0+)//g;\n\ts/11\\K(1+)/ $cnt_max += length $1, '' /ge;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\t0 while 0\n\t\t|| s/0(1+)/ $cnt_max += length $1, 0 /ge\n\t\t|| s/(1+)0/ $cnt_max += length $1, 0 /ge\n\t\t|| s/(1+)/ $cnt_max += length $1, '' /ge\n\t\t;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\t0 while 0\n\t\t|| s/[23][23]\\K([23]+)/ $cnt_max += length $1, '' /ge\n\t\t|| s/[23][23]/ $cnt_max ++, 3 /ge\n\t\t|| s/030/ $cnt_max += 3, 'X' /ge\n\t\t|| s/03X|X30|02X|X20/ $cnt_max += 2, 'X' /ge\n\t\t|| s/X2X|X3X/ $cnt_max += 1, 'X' /ge\n\t\t;\n\t\n\t$debug and print \"max:[$_]:$cnt_max\";\n\t\n\t$cnt_max += 2 * ( () = /[23]/g );\n\t\n\tprint \"$cnt_min $cnt_max\";\n\t}"}, {"source_code": "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n\n$\\ = $/;\n\nmy $debug = 0;\n\nwhile(<>){\n\t$debug and print '-' x 15;\n\t@_ = ( 0 ) x $_;\n\t\n\tfor( split ' ', <> ){\n\t\t$_[ $_ - 1 ] ++ if $_[ $_ - 1 ] < 3;\n\t\t}\n\t\n\t$_ = join '', 0, @_, 0;\n\t\n\t$debug and print \"....[$_]\";\n\t\n\tmy $copy = $_;\n\t\n\ty/1-9/1/;\n\t\n\tmy $cnt_min = 0;\n\t\n\t$debug and print \"min:[$_]\";\n\t\n\t0 while 0\n\t\t|| s/11(1+)/ $cnt_min += length $1, '' /ge\n\t\t|| s/11/ $cnt_min += 1, '' /ge\n\t\t|| s/101/ $cnt_min += 1, '' /ge\n\t\t;\n\t\n\t$debug and print \"min:[$_]\";\n\t\n\t$cnt_min += () = /1/g;\n\t\n\t$_ = $copy;\n\t\n\tmy $cnt_max = 0;\n\t\n\ts/00\\K(0+)//g;\n\ts/11\\K(1+)/ $cnt_max += length $1, '' /ge;\n\t\n\t$debug and print \"max:[$_]\";\n\t\n\t0 while 0\n\t\t|| s/0(1+)/ $cnt_max += length $1, 0 /ge\n\t\t|| s/(1+)0/ $cnt_max += length $1, 0 /ge\n\t\t|| s/(1+)/ $cnt_max += length $1, '' /ge\n\t\t;\n\t\n\t$debug and print \"max:[$_]\";\n\t\n\t0 while 0\n\t\t|| s/[23][23]\\K([23]+)/ $cnt_max += length $1, '' /ge\n\t\t|| s/[23][23]/ $cnt_max ++, 3 /ge\n\t\t|| s/030/ $cnt_max += 3, '' /ge\n\t\t|| s/030/111/g\n\t\t;\n\t\n\t$debug and print \"max:[$_]\";\n\t\n\t$cnt_max += 2 * ( () = /[^0]/g );\n\t\n\tprint \"$cnt_min $cnt_max\";\n\t}"}], "src_uid": "c1e51cc003cbf60a617fc11de9c73bcf"}