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.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — 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$$$.

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.

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.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;
	my $n = $_;

	my %h;
	my %g;

	for my $i ( 1 .. $n - 1 ){
		my( $u, $v ) = split ' ', <>;

		$g{ $u } ++;
		$g{ $v } ++;
		$h{ $u }{ $v } = $i;
		$h{ $v }{ $u } = $i;
		}

	$_ = join '', sort { $a <=> $b } values %g;

	if( not m/^112*$/ ){
		print -1;
		next;
		}

	my $first;

	for my $i ( 1 .. $n ){
		if( $g{ $i } == 1 ){
			$first = $i;
			last;
			}
		}

	my $second;

	my @seq;

	for my $i ( 1 .. $n - 1 ){
		( $second ) = sort keys %{ $h{ $first } };
		push @seq, [ $h{ $first }{ $second }, $i % 2 ];
		exists $h{ $second }{ $first } and delete $h{ $second }{ $first };
		$first = $second;
		}

	@seq = map $_->[ 1 ], sort { $a->[ 0 ] <=> $b->[ 0 ] } @seq;

	print "@seq" =~ y/10/52/r;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;
	my $n = $_;

	my %h;
	my %g;

	for my $i ( 1 .. $n - 1 ){
		my( $u, $v ) = split ' ', <>;

		$g{ $u } ++;
		$g{ $v } ++;
		$h{ $u }{ $v } = $i;
		$h{ $v }{ $u } = $i;
		}

	$_ = join '', sort { $a <=> $b } values %g;

	if( not m/^112*$/ ){
		print -1;
		next;
		}

	my $first;

	for my $i ( 1 .. $n ){
		if( $g{ $i } == 1 ){
			$first = $i;
			last;
			}
		}

	my $second;

	my @seq;

	for my $i ( 1 .. $n - 1 ){
		( $second ) = keys %{ $h{ $first } };
		push @seq, [ $h{ $first }{ $second }, $i % 2 ];
		exists $h{ $second }{ $first } and delete $h{ $second }{ $first };
		$first = $second;
		}

	@seq = map $_->[ 1 ], sort { $a->[ 0 ] <=> $b->[ 1 ] } @seq;

	print "@seq" =~ y/10/52/r;
	}


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$$$.

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case contains one integer $$$n$$$ ($$$1 \leq n \leq 10^6$$$) — the number that Polycarp wants to represent as the sum of the numbers $$$2020$$$ and $$$2021$$$.

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).

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;

	my $div = int $_ / 2020;

	my $div_plus = 2021 * $div;

	if( $div_plus >= $_ ){
		print "YES";
		}
	else{
		print "NO";
		}
	}


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.

The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). 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; 0).

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 — 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 - 6.


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$$$)?

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$)  — the number of test cases in the test. The following are descriptions of the input test cases. The first line of the description of each test case contains one integer $$$n$$$ ($$$1 \le n \le 50$$$)  — the array $$$a$$$. The second line of the description of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$)  — elements of the array $$$a$$$.

For each test case, print one integer  — the minimum number of operations to make all elements of the array $$$a$$$ equal.

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]$$$.

my $max;
my $min;

open(FH, '<', 'input.txt');

for(1..<FH>) {
        $min = 9999999;
        $max = 0;
        <FH>;
        chomp (my $in = <FH>);
        @a = split / /, $in;
        foreach (@a){
                if ($_ > $max) {
                        $max = $_;
                }
                if ($_ < $min) {
                        $min = $_;
                }
        }
        print $max - $min;
        print "\n";
}


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.

The first line contains two space-separated integers n and m . Each of the next m lines contains two space-separated integers xi and yi (1 ≤ xi < yi ≤ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input. Consider all the chemicals numbered from 1 to n in some order.

Print a single integer — the maximum possible danger.

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).

# perform dfs to make the connected
# components
sub make_disjoint_sets {

    my ($lst_ref, $visit_ref, $ds_ref, $curr, $id ) = @_;

    if ( exists $visit_ref -> { $curr } ) {
	return;
    }

    $visit_ref -> { $curr } = 1;
    $ds_ref -> { $curr } = $id;

    for my $adjacent ( keys %{ $lst_ref -> { $curr } } ) {
	&make_disjoint_sets ( $lst_ref, $visit_ref,
			     $ds_ref, $adjacent, $id );
    }

    return;
}

# use a hash as a set to count the number
# of unique sets
sub count_disjoint_sets {

    my $ds_ref = $_[0];
    my %count_set = ();

    for my $i (values %{ $ds_ref }) {
	$count_set { $ds_ref -> { $i } } = 1;
    }

    my $val = (scalar (keys %count_set));
    return $val;
}

my $f = <STDIN>;
chomp ( $f );
my ($n, $m) = split m/ /, $f;

my %disjoint_sets = ();
my %lst = ();
my %visit = ();

my $a, $b;
for my $i (1..$m) {
    $f = <STDIN>;
    chomp ( $f );
    ($a, $b) = split m/ /, $f;
    $lst { $a }{ $b } = 1;
    $lst { $b }{ $a } = 1;
}

for my $i (1..$n) {
    &make_disjoint_sets ( \%lst, \%visit, \%disjoint_sets, $i, $i );
}

my $cc = &count_disjoint_sets ( \%disjoint_sets );

my $answer = (2 ** ($n - $cc));

print "$answer\n";


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.

The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.

Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.

#!/usr/bin/perl -w

use strict;

<>;
my @a = split ' ', <>;

my $imax = 0;
my $imin = 0;
$imax = $a[$_] > $a[$imax] ? $_ : $imax for 1..$#a;
$imin = $a[$_] <= $a[$imin] ? $_ : $imin for 1..$#a;

print $imax > $imin ? ($imax + ($#a - $imin) - 1) : ($imax + ($#a - $imin)), "\n";

chomp($n = <STDIN>);
@soldiers = split /\s+/, <STDIN>;

%pos = { };
$tall = -1;
$small = 99999;

for($s=0;$s < $n; $s++) {
	if($soldiers[$s] > $tall) {
		$tall = $soldiers[$s];
		$pos{'tall'} = $s;
	}
	if($soldiers[$s] <= $small) {
		$small = $soldiers[$s];
		$pos{'small'} = $s;
	}
}

#print "Tallest in pos ".$pos{'tall'}.", smallest in ".$pos{'small'}."\n";

$swaps = $pos{'tall'} + ($n - $pos{'small'} - 1);
if($pos{'tall'} > $pos{'small'}) { $swaps--; }
print "$swaps\n";

sub maxInd {
    (my $max, my $index) = (0, 0);
    for($i = 0; $i <= $#_; $i++)
    {
        if($max < $_[$i]) {
            $max = $_[$i];
            $index = $i;
        }
    }
    $index;
}

sub minInd {
    (my $min, my $index) = (100, 0);
    for($i = $#_; $i >= 0; $i--)
    {
        if($min > $_[$i]) {
            $min = $_[$i];
            $index = $i;
        }
    }
    $index;
}

<STDIN>;
chomp($s = <STDIN>);
@arr = split(/ /, $s);
$max = &maxInd(@arr);
$min = &minInd(@arr);
$answer = $max + abs($#arr - $min);
print $max - $min > 0 ? --$answer : $answer;

#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';

my $n = <STDIN>;
my @soldiers = split(' ', <STDIN>);
my $min = 100000;
my $min_position = 0;
my $max = -1;
my $max_position = 0;

for (my $i = 0; $i < $n; $i++)
{
    my $soldier = $soldiers[$i];
    if ($soldier > $max)
    {
        $max = $soldier;
        $max_position = $i;
    }
    if ($soldier <= $min)
    {
        $min = $soldier;
        $min_position = $i;
    }
}

my $result = $max_position + ($n - 1 - $min_position);
if ($max_position > $ min_position)
{
    $result--;
}

print $result;

sub maxInd {
    (my $max, my $index) = (0, 0);
    for($i = 0; $i <= $#_; $i++)
    {
        if($max < $_[$i]) {
            $max = $_[$i];
            $index = $i;
        }
    }
    $index;
}

sub minInd {
    (my $min, my $index) = (100, 0);
    for($i = $#_; $i >= 0; $i--)
    {
        if($min > $_[$i]) {
            $min = $_[$i];
            $index = $i;
        }
    }
    $index;
}

chomp($s = <STDIN>);
@arr = split(/ /, $s);
$max = &maxInd(@arr);
$min = &minInd(@arr);
$answer = $max + abs($#arr - $min);
print $max - $min > 0 ? --$answer : $answer;


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.

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of groups.  Each of the following n lines contains a sequence consisting of 7 zeroes and ones — the schedule of classes on Monday for a group. If the symbol in a position equals to 1 then the group has class in the corresponding time slot. In the other case, the group has no class in the corresponding time slot.

Print minimum number of rooms needed to hold all groups classes on Monday.

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.


One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k. There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.Now the children wonder, how to divide the orange so as to meet these conditions:  each child gets exactly n orange segments;  the i-th child gets the segment with number ai for sure;  no segment goes to two children simultaneously. Help the children, divide the orange and fulfill the requirements, described above.

The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get. It is guaranteed that all numbers ai are distinct.

Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces. You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.

use warnings;
use strict;

my $l = <>;
my ($n, $k) = split(" ", $l);
my $l2 = <>;
my @ch = split(" ", $l2);

my %seg;
@seg{1..$k} = @ch;
my %rseg = reverse %seg;

$/ = " ";
$\ = " ";

my $i = 0;

my $cur = 0;

for my $child (1..$k) {
    print $seg{$child};
    for my $part (1..$n-1) {
        while ( exists $rseg{++$cur} ) { }
        print $cur;
    }
}

use warnings;
use strict;

my $l = <>;
my ($n, $k) = split(" ", $l);
my $l2 = <>;
my @ch = split(" ", $l2);

my %seg;
@seg{1..$k} = @ch;
my %rseg = reverse %seg;

$/ = " ";
$\ = " ";

my $i = 0;

for (1..$n*$k)
{
  print $seg{++$i} if $_ % $k == 0;
  print $_   unless exists $rseg{$_};

}

use warnings;
use strict;

my $l = <>;
my ($n, $k) = split(" ", $l);
my $l2 = <>;
my @ch = split(" ", $l2);

my %seg;
@seg{1..$k} = @ch;
my %rseg = reverse %seg;

$/ = " ";
$\ = " ";

my $i = 0;

for (1..$n*$k)
{
  print $seg{++$i} if $_ % $n == 0;
  print $_   unless exists $rseg{$_};

}

use warnings;
use strict;

my $l = <>;
my ($n, $k) = split(" ", $l);
my $l2 = <>;
my @ch = split(" ", $l2);

my %seg;
@seg{1..$k} = @ch;
my %rseg = reverse %seg;

$/ = " ";
$\ = " ";

my $i = 0;

for (1..$n*$k)
{
  print $seg{++$i} if $_ % $k == 1;
  print $_   unless exists $rseg{$_};

}


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?

The first line of input contains an integer t (0 < t < 180) — the number of tests. Each of the following t lines contains a single integer a (0 < a < 180) — the angle the robot can make corners at measured in degrees.

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.

NoteIn the first test case, it is impossible to build the fence, since there is no regular polygon with angle .In the second test case, the fence is a regular triangle, and in the last test case — a square.

#!/usr/bin/perl
use v5.10;

chomp($n = <>);
360%(180-<>)==0 and say "YES" or say "NO" foreach (1..$n);

use 5.016;
use strict;
use warnings;

sub is_regular_angle {
  my ($theta) = @_;

  my $outside = 180 - $theta;
  360 % $outside == 0;
}

my $n = <>;
for (1..$n) {
  my $theta = <>;
  say is_regular_angle($theta) ? 'YES' : 'NO';
}

#!usr/bin/perl
#use warnings;
#use strict;
#use autodie;
#use diagnostics;
use utf8;
use 5.010;
use 5.012;
use 5.014;
my($count,$t,$n);
chomp($t=<STDIN>);
$count=0;
while($count != $t)
{
	$count++;
	chomp($n=<STDIN>);
	$n=180-$n;
	$n=360/$n;
	if($n=~/\./)
	{
		say "NO";
	}
	else
	{
		say "YES";
	}
}

my $num = <>;
chomp $num;
for (1..$num) {
    my $angle = <>;
    chomp $angle;
    (360%(180-$angle)) ? print "NO\n" : print "YES\n";
}


While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of length n and for each number ai got number bi = fai. To finish the prank he erased the initial sequence ai.It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible.

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the lengths of sequences fi and bi respectively. The second line contains n integers, determining sequence f1, f2, ..., fn (1 ≤ fi ≤ n). The last line contains m integers, determining sequence b1, b2, ..., bm (1 ≤ bi ≤ n).

Print "Possible" if there is exactly one sequence ai, such that bi = fai for all i from 1 to m. Then print m integers a1, a2, ..., am. If there are multiple suitable sequences ai, print "Ambiguity". If Spongebob has made a mistake in his calculations and no suitable sequence ai exists, print "Impossible".

NoteIn the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique.In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence.In the third sample fi ≠ 3 for all i, so no sequence ai transforms into such bi and we can say for sure that Spongebob has made a mistake.

#!perl
$\ = "\n";

$AmbiguityFlag = 0;
$ImpossibleFlag = 0;

<>;
@f = split /\s/, <>;
@b = split /\s/, <>;

for (my $i = 0; $i <= $#f; $i++)
{
    $repetition{@f[$i]} = 0 if !exists $repetition{@f[$i]};
    $repetition{@f[$i]}++;

    $numbers{@f[$i]} = $i + 1;
}

for my $i (@b)
{
    if ($repetition{$i} > 1)
    {
        $AmbiguityFlag = 1;
    }

    if (exists $numbers{$i})
    {
        push @a, $numbers{$i};
    }
    else
    {
        $ImpossibleFlag = 1;
        last;
    }
}

if($ImpossibleFlag == 1)
{
    print "Impossible";
}
elsif($AmbiguityFlag == 1)
{
    print "Ambiguity";
}
else
{
    print "Possible";
    print "@a";
}

#!perl
$\ = "\n";

$AmbiguityFlag = 0;
$ImpossibleFlag = 0;

<>;
@f = split /\s/, <>;
@b = split /\s/, <>;

for (my $i = 0; $i <= $#f; $i++)
{
    if (exists $repetition{@f[$i]})
    {
        $AmbiguityFlag = 1;
        #last;
    }
    else
    {
        $repetition{@f[$i]} = 1;
    }

    $numbers{@f[$i]} = $i + 1;
}

for my $i (@b)
{
    if (exists $numbers{$i})
    {
        push @a, $numbers{$i};
    }
    else
    {
        $ImpossibleFlag = 1;
        last;
    }
}

if($ImpossibleFlag == 1)
{
    print "Impossible";
}
elsif($AmbiguityFlag == 1)
{
    print "Ambiguity";
}
else
{
    print "Possible";
    print "@a";
}

#!perl
$\ = "\n";

$AmbiguityFlag = 0;
$ImpossibleFlag = 0;

<>;
@f = split /\s/, <>;
@b = split /\s/, <>;

for (my $i = 0; $i <= $#f; $i++)
{
    $repetition{@f[$i]} = 0 if !exists $repetition{@f[$i]};
    $repetition{@f[$i]}++;

    $numbers{@f[$i]} = $i + 1;
}

for my $i (@b)
{
    if ($repetition{$i} > 1)
    {
        $AmbiguityFlag = 1;
        last;
    }

    if (exists $numbers{$i})
    {
        push @a, $numbers{$i};
    }
    else
    {
        $ImpossibleFlag = 1;
        last;
    }
}

if($ImpossibleFlag == 1)
{
    print "Impossible";
}
elsif($AmbiguityFlag == 1)
{
    print "Ambiguity";
}
else
{
    print "Possible";
    print "@a";
}


One day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world from bombs planted by terrorists, but he rarely won.Alex has grown up since then, so he easily wins the most difficult levels. This quickly bored him, and he thought: what if the computer gave him invalid fields in the childhood and Alex could not win because of it?He needs your help to check it.A Minesweeper field is a rectangle $$$n \times m$$$, where each cell is either empty, or contains a digit from $$$1$$$ to $$$8$$$, or a bomb. The field is valid if for each cell:   if there is a digit $$$k$$$ in the cell, then exactly $$$k$$$ neighboring cells have bombs.  if the cell is empty, then all neighboring cells have no bombs. Two cells are neighbors if they have a common side or a corner (i. e. a cell has at most $$$8$$$ neighboring cells).

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the sizes of the field. The next $$$n$$$ lines contain the description of the field. Each line contains $$$m$$$ characters, each of them is "." (if this cell is empty), "*" (if there is bomb in this cell), or a digit from $$$1$$$ to $$$8$$$, inclusive.

Print "YES", if the field is valid and "NO" otherwise. You can choose the case (lower or upper) for each letter arbitrarily.

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.

use warnings;
use strict;
use constant NL => qq/\n/;

my ( $n, $m ) = (<STDIN> =~ m/(\d+)\s(\d+)/);
my @arr = ();
for ( 1 .. $n ) {
    my $line = <STDIN>;
    push @arr, [ split qq//, $line ] }
my $is_valid = 1;

AA: for my $i ( 0 .. ($n-1) ) {
    AB: for my $j ( 0 .. ($m-1) ) {
        if ( $arr [ $i ][ $j ] eq '.' ) {
            AC: for my $one ( ($i-1) .. ($i+1) ) {
                AD: for my $two ( ($j-1) .. ($j+1) ) {
                    if ( $one eq $i and $two eq $j ) { next AD }
                    if ( $one >= 0 and $two >= 0 and $one < $n and $two < $m and
                         ($arr [ $one ][ $two ] eq '*') ) {
                        $is_valid = 0;
                        last AA }}}}
        elsif ( $arr [ $i ][ $j ] ne '*' ) {
            my $sum = 0;
            AE: for my $one ( ($i-1) .. ($i+1) ) {
                AF: for my $two ( ($j-1) .. ($j+1) ) {
                    if ( $one eq $i and $two eq $j ) { next AF }
                    if ( $one >= 0 and $two >= 0 and
                         $one < $n and $two < $m and
                         ( $arr [ $one ][ $two ] eq '*') ) { $sum += 1 } }}
            if ( $sum != $arr [ $i ][ $j ] ) {
                $is_valid = 0;
                last AA }}
    }
}

print qq/YES\n/ if $is_valid;
print qq/NO\n/ if not $is_valid;

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 0;

while(<>){
	my( $n, $m ) = split;

	my @A;

	( $_ = <>, chomp ), push @A, [ split // ] for 1 .. $n;

	my $fail = 0;

	for my $i ( 0 .. $n - 1 ){
		for my $j ( 0 .. $m - 1 ){
			next if $A[$i][$j] eq '*';

			my $bombs = 0;

			$i > 0 and $A[$i-1][$j+0] eq '*' and $bombs ++;
			$i > 0 and $j > 0 and $A[$i-1][$j-1] eq '*' and $bombs ++;
			$i > 0 and $j < $m - 1 and $A[$i-1][$j+1] eq '*' and $bombs ++;
			$i < $n - 1 and $A[$i+1][$j+0] eq '*' and $bombs ++;
			$i < $n - 1 and $j > 0 and $A[$i+1][$j-1] eq '*' and $bombs ++;
			$i < $n - 1 and $j < $m - 1 and $A[$i+1][$j+1] eq '*' and $bombs ++;
			$j > 0 and $A[$i+0][$j-1] eq '*' and $bombs ++;
			$j < $m - 1 and $A[$i+0][$j+1] eq '*' and $bombs ++;

			$debug and print $bombs;

			next if $A[$i][$j] eq '.' and not $bombs;

			if( $bombs ne $A[$i][$j] ){ $fail = 1; }
			}
		}

	print $fail ? "NO" : "YES";

	$debug and print '-' x 15;
	}


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.

Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(-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$$$.

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)$$$ — 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)$$$— 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.

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.


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.

The first line contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of T-shirts. The $$$i$$$-th of the next $$$n$$$ lines contains $$$a_i$$$ — the size of the $$$i$$$-th T-shirt of the list for the previous year. The $$$i$$$-th of the next $$$n$$$ lines contains $$$b_i$$$ — the size of the $$$i$$$-th T-shirt of the list for the current year. It is guaranteed that all the sizes in the input are valid. It is also guaranteed that Ksenia can produce list $$$b$$$ from the list $$$a$$$.

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.

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.

%prev;
%now;
$num=<>;
for(1..$num){
    $shirt=<>;
    $shirt=~s/\n//;
    $prev{$shirt}=$prev{$shirt}+1;
}
for(1..$num){
    $shirt=<>;
    $shirt=~s/\n//;
    $cur{$shirt}=$cur{$shirt}+1;
}

$sum = 0;
foreach(keys%prev){
    $diff=$prev{$_}-$cur{$_};
    $sum+=$diff if $diff>0;
}
print $sum;


Eugeny has array a = a1, a2, ..., an, consisting of n integers. Each integer ai equals to -1, or to 1. Also, he has m queries:  Query number i is given as a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).  The response to the query will be integer 1, if the elements of array a can be rearranged so as the sum ali + ali + 1 + ... + ari = 0, otherwise the response to the query will be integer 0. Help Eugeny, answer all his queries.

The first line contains integers n and m (1 ≤ n, m ≤ 2·105). The second line contains n integers a1, a2, ..., an (ai = -1, 1). Next m lines contain Eugene's queries. The i-th line contains integers li, ri (1 ≤ li ≤ ri ≤ n).

Print m integers — the responses to Eugene's queries in the order they occur in the input.


ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen.For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen.You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything.

The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word.

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.

NoteThe first sample is already explained in the problem statement.For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1.

use 5.016;
use strict;
use warnings;

my ($n, $c) = split ' ', <>;
my @times = split ' ', <>;
my $last = 0;
my $count = 0;

for my $time (@times) {
  if ($time - $last > $c) {
    $count = 1;
  } else {
    $count++;
  }

  $last = $time;
}

say $count;

use strict;
use warnings;

use Data::Dumper;
$,=" ";
my ($n,$c)=split / /,<>;
chomp($n,$c);
my $sum=1;
my @t=split  / /,<>;
chomp(@t);
my $a=shift @t;
for(@t)
{
if($_-$a<=$c)
{
$sum++ ;
}
else
{
  $sum=1;
}
$a=$_;
}

print $sum . "\n";


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.

The first line contains a single integer $$$t$$$ $$$(1 \le t \le 1000)$$$  — the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$m$$$ $$$(1 \le n \le 10^5, 1 \le m \le 10^5)$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$10^5$$$.

For each test case print the answer to the problem.

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.


You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal.The input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Don't use the function input() in Python2 instead of it use the function raw_input().

The first line contains a non-negative integer a. The second line contains a non-negative integer b. The numbers a, b may contain leading zeroes. Each of them contains no more than 106 digits.

Print the symbol "<" if a < b and the symbol ">" if a > b. If the numbers are equal print the symbol "=".

# open STDIN,"<","a.in" or die $!;
my $a1 = ReadLine();
my $a2 = ReadLine();
$a1 =~ s/^0+//;
$a2 =~ s/^0+//;

# print "$a1 $a2";
my ($len1,$len2) = (length($a1),length($a2));
if($len1 eq $len2) {
    if($a1 eq $a2) {print "=";}
    elsif($a1 ge $a2) {print ">";}
    else {print "<"};
}
elsif($len1 ge $len2) {print ">";}
else {print "<";}

print "\n";

sub ReadLine {
    my $line = <STDIN>;
    chop($line);
    my $lll = length($line);
    while(not $lll) {
        $line = <STDIN>;
        chop($line);
        $lll = length($line);
    }
    return $line;
}

#open STDIN,"<","a.in" or die $!;
my $a1 = ReadLine();
my $a2 = ReadLine();
$a1 =~ s/^0+//; # 去前导零
$a2 =~ s/^0+//;

# print "$a1 $a2";
my ($len1,$len2) = (length($a1),length($a2));
if($len1 eq $len2) {
    if($a1 eq $a2) {print "=";}
    elsif($a1 ge $a2) {print ">";}
    else {print "<"};
}
elsif($len1 ge $len2) {print ">";}
else {print "<";}

print "\n";

sub ReadLine { # 读一行非空的字符串
    chop(my $line = <STDIN>);
    while(not length($line)) {
        chop($line = <STDIN>);
    }
    return $line;
}

#open STDIN,"<","a.in" or die $!;
my $a1 = ReadLine();
my $a2 = ReadLine();
$a1 =~ s/^0+//;
$a2 =~ s/^0+//;

# print "$a1 $a2";
my ($len1,$len2) = (length($a1),length($a2));
if($len1 eq $len2) {
    if($a1 eq $a2) {print "=";}
    elsif($a1 ge $a2) {print ">";}
    else {print "<"};
}
elsif($len1 ge $len2) {print ">";}
else {print "<";}

print "\n";

sub ReadLine {
    my $line = <STDIN>;
    chop($line);
    while(not length($line)) {
        chop($line = <STDIN>);
    }
    return $line;
}

$ch1=<>;
chomp $ch1;
$ch2=<>;
chomp$ch2;

$ch1=~m/^(0*)./;

$ll1=length$1;
$ch2=~m/^(0*)./;

$ll2=length$1;
$l1=length$ch1;
$l2=length$ch2;

if($l1-$ll1>$l2-$ll2){
print ">";
}
elsif($l1-$ll1<$l2-$ll2){
print "<";
}

elsif(substr($ch1,$ll1) gt substr($ch2,$ll2)){
print ">";
}
elsif(substr($ch1,$ll1) lt substr($ch2,$ll2)){
print "<";
}
else{
print "=";
}

($A, $B) = map { chomp; s/^0+//r } <>;

$W = 0;

print qw(= > <)[ length $A <=> length $B || do {
			for (1 .. length $A) { $W = chop $A <=> chop $B || $W }
			$W
		}
	]

my $a1 = <STDIN>;
my $a2 = <STDIN>;
$a1 =~ s/0+//;
$a2 =~ s/0+//;
my ($len1,$len2) = (length($a1),length($a2));
if($len1 eq $len2) {
    if($a1 eq $a2) {print "=";}
    elsif($a1 ge $a2) {print ">";}
    else {print "<"};
}
elsif($len1 ge $len2) {print ">";}
else {print "<";}

#open STDIN,"<","a.in" or die $!;
my $a1 = <STDIN>;
my $a2 = <STDIN>;
$a1 =~ s/^0+//;
$a2 =~ s/^0+//;

my ($len1,$len2) = (length($a1),length($a2));
if($len1 eq $len2) {
    if($a1 eq $a2) {print "=";}
    elsif($a1 ge $a2) {print ">";}
    else {print "<"};
}
elsif($len1 ge $len2) {print ">";}
else {print "<";}

my $a1 = <STDIN>;
my $a2 = <STDIN>;
if($a1 == $a2) {
    print "=";
}
elsif($a1 > $a2) {
    print ">";
}
else {
    print "<";
}

#open STDIN,"<","a.in" or die $!;
my $a1 = <STDIN>;
my $a2 = <STDIN>;
$a1 =~ s/^0+//;
$a2 =~ s/^0+//;

my ($len1,$len2) = (length($a1),length($a2));
print "$len1 $len2" if ($a2 eq "8");
if($len1 eq $len2) {
    if($a1 eq $a2) {print "=";}
    elsif($a1 ge $a2) {print ">";}
    else {print "<"};
}
elsif($len1 ge $len2) {print ">";}
else {print "<";}

$ch1=<>;
chomp $ch1;
$ch2=<>;
chomp$ch2;
$num1=int($ch1);
$num2=int($ch2);
if($num2==$num1){
print "=";
}
elsif ($num1>$num2){
print">";
}
else{
print "<";
}


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.

The first line of the input contains the number of countries n (1 ≤ n ≤ 100 000). The second line contains n non-negative integers ai without leading zeroes — the number of tanks of the i-th country. It is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number's representations doesn't exceed 100 000.

Print a single number without leading zeroes — the product of the number of tanks presented by each country.

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.


DZY loves Physics, and he enjoys calculating density.Almost everything has density, even a graph. We define the density of a non-directed graph (nodes and edges of the graph have some values) as follows:  where v is the sum of the values of the nodes, e is the sum of the values of the edges.Once DZY got a graph G, now he wants to find a connected induced subgraph G' of the graph, such that the density of G' is as large as possible.An induced subgraph G'(V', E') of a graph G(V, E) is a graph that satisfies:  ;  edge  if and only if , and edge ;  the value of an edge in G' is the same as the value of the corresponding edge in G, so as the value of a node. Help DZY to find the induced subgraph with maximum density. Note that the induced subgraph you choose must be connected.

The first line contains two space-separated integers n (1 ≤ n ≤ 500), . Integer n represents the number of nodes of the graph G, m represents the number of edges. The second line contains n space-separated integers xi (1 ≤ xi ≤ 106), where xi represents the value of the i-th node. Consider the graph nodes are numbered from 1 to n. Each of the next m lines contains three space-separated integers ai, bi, ci (1 ≤ ai < bi ≤ n; 1 ≤ ci ≤ 103), denoting an edge between node ai and bi with value ci. The graph won't contain multiple edges.

Output a real number denoting the answer, with an absolute or relative error of at most 10 - 9.

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.


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.

The first line contains two space-separated integers n and x (1 ≤ n ≤ 1000, 0 ≤ x ≤ 109). Each of the next n lines contains a character '+' or '-', and an integer di, separated by a space (1 ≤ di ≤ 109). 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.

Print two space-separated integers — number of ice cream packs left after all operations, and number of kids that left the house in distress.

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.


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.

Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — 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.

For each test case, output "Alice" (without quotes) if Alice wins and "Bob" (without quotes) otherwise.

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.


Let's say you are standing on the $$$XY$$$-plane at point $$$(0, 0)$$$ and you want to reach point $$$(n, n)$$$.You can move only in two directions:   to the right, i. e. horizontally and in the direction that increase your $$$x$$$ coordinate,  or up, i. e. vertically and in the direction that increase your $$$y$$$ coordinate. In other words, your path will have the following structure:   initially, you choose to go to the right or up;  then you go some positive integer distance in the chosen direction (distances can be chosen independently);  after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $$$n - 1$$$ direction changes.As a result, your path will be a polygonal chain from $$$(0, 0)$$$ to $$$(n, n)$$$, consisting of at most $$$n$$$ line segments where each segment has positive integer length and vertical and horizontal segments alternate.Not all paths are equal. You have $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ where $$$c_i$$$ is the cost of the $$$i$$$-th segment.Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $$$k$$$ segments ($$$k \le n$$$), then the cost of the path is equal to $$$\sum\limits_{i=1}^{k}{c_i \cdot length_i}$$$ (segments are numbered from $$$1$$$ to $$$k$$$ in the order they are in the path).Find the path of the minimum cost and print its cost.

The first line contains the single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 10^9$$$) — the costs of each segment. It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$10^5$$$.

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.

NoteIn the first test case, to reach $$$(2, 2)$$$ you need to make at least one turn, so your path will consist of exactly $$$2$$$ segments: one horizontal of length $$$2$$$ and one vertical of length $$$2$$$. The cost of the path will be equal to $$$2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$$$.In the second test case, one of the optimal paths consists of $$$3$$$ segments: the first segment of length $$$1$$$, the second segment of length $$$3$$$ and the third segment of length $$$2$$$.The cost of the path is $$$1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$$$.In the third test case, one of the optimal paths consists of $$$4$$$ segments: the first segment of length $$$1$$$, the second one — $$$1$$$, the third one — $$$4$$$, the fourth one — $$$4$$$. The cost of the path is $$$1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$$$.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;
	my $n = $_;
	@_ = split ' ', <>;

	my $min_h = shift @_;
	my $min_v = shift @_;

	my $sum_h = $min_h;
	my $sum_v = $min_v;

	my $cnt_h = 1;
	my $cnt_v = 1;

	my $sum = $min_h * $n + $min_v * $n;
	my $min_sum = $sum;

	for my $i ( 1 .. $n - 2 ){
		my $j = shift @_;

		if( $i % 2 == 1 ){
			if( $j < $min_h ){
				$min_h = $j;
				}
			$cnt_h ++;
			$sum_h += $j;
			}
		else{
			if( $j < $min_v ){
				$min_v = $j;
				}
			$cnt_v ++;
			$sum_v += $j;
			}

		my $sum = $sum_h - $min_h + $sum_v - $min_v
			+ ( $n - $cnt_h + 1 ) * $min_h
			+ ( $n - $cnt_v + 1 ) * $min_v
			;

		$sum < $min_sum and $min_sum = $sum;
		}

	print $min_sum;
	}


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.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 100)$$$ — the number of goods. The second line of the query contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^7$$$), where $$$a_i$$$ is the price of the $$$i$$$-th good.

For each query, print the answer for it — the minimum possible equal price of all $$$n$$$ goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	@_ = split ' ', <>;

	my $sum = 0;

	$sum += $_ for @_;

	print +( int $sum / @_ ) + !! ( $sum % @_ );
	}


Polycarp is flying in the airplane. Finally, it is his favorite time — 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, a2, ..., ak, where ai is the number of portions of the i-th dish.The stewardess has already given food to m - 1 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 - 1 passengers ahead of him, he noticed what dishes they were given. Besides, he's heard some strange mumbling from some of the m - 1 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.

Each test in this problem consists of one or more input sets. First goes a string that contains a single integer t (1 ≤ t ≤ 100 000) — 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 ≤ m ≤ 100 000, 1 ≤ k ≤ 100 000) — the number of Polycarp's seat and the number of dishes, respectively. The second line contains a sequence of k integers a1, a2, ..., ak (1 ≤ ai ≤ 100 000), where ai is the initial number of portions of the i-th dish. Then m - 1 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, rj (0 ≤ tj ≤ k, 0 ≤ rj ≤ 1), 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 — 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 000. Sum k for all input sets doesn't exceed 100 000.

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.

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".

use strict;
use warnings;
my $tests = <>;
my @ANS;

for my $curTest (1 .. $tests)
{
    <>;
    my @ans;
    my $any = 0;
    my $first = 0;
    my ($n,$m ) = split /\s/, <>;

    my @dish = split /\s/, <>;
    for(0..$#dish)
    {
        $ans[$_] = 'N';
    };
    my %beforeFirst;
    for(1 .. $n - 1)
    {
        my ($d, $w) = split /\s/, <>;
        if($w == 1 and $first == 0)
        {
            $first++;
            for (0..$#dish)
            {
                if($dish[$_] <= $any)
                {
                    $beforeFirst{$_} = 1;
                }
            }
        }
        if($d != 0)
        {
            delete ($beforeFirst{$d-1});
            $dish[$d-1]--;
        }
        else
        {
            $any++;
        }
    }
    my $min = 10**7;
    for (keys %beforeFirst)
    {
        if($min > $dish[$_])
        {
            $min = $dish[$_];
        }
        $ans[$_] = 'Y';
    }
    $any -= $min  unless $min == 10**7;

    if ($any < 0){$any = 0;}
    for(0.. $#dish)
    {
        if ($dish[$_] <= $any) { $ans[$_] = 'Y'; }
    }
    push @ANS, [@ans];
}
for (@ANS)
{
print @{$_};
print "\n";
}


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 = s1s2... sn, then the following inequality holds, si ≠ si + 1(1 ≤ i < n).  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 = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.

A single line contains two positive integers n and k (1 ≤ n ≤ 106, 1 ≤ k ≤ 26) — the string's length and the number of distinct letters.

In a single line print the required string. If there isn't such string, print "-1" (without the quotes).


Let's define the cost of a string $$$s$$$ as the number of index pairs $$$i$$$ and $$$j$$$ ($$$1 \le i < j < |s|$$$) such that $$$s_i = s_j$$$ and $$$s_{i+1} = s_{j+1}$$$.You are given two positive integers $$$n$$$ and $$$k$$$. Among all strings with length $$$n$$$ that contain only the first $$$k$$$ characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost — find any of them.

The only line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 26$$$).

Print the string $$$s$$$ such that it consists of $$$n$$$ characters, each its character is one of the $$$k$$$ first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings — print any of them.

#!/usr/bin/perl
use Data::Dumper;

my ($n,$p) = map { $_ - 0 } split(/\s+/,<STDIN>);

if( $p == 1 ){
  print ("a" x $n);
} elsif( $p == 2 ){
  while($n>0){
    print substr("aabb",0,$n<4 ? $n : 4);
    $n -= 4;
  }
} else {
  my $al = "abcdefghijklmnopqrstuvwxyz";
  my $b = 'a';
  for(my $i=0;$i<$p-1;$i++){
    $b .= substr($al,$i,1);
    for(my $j=0;$j<2*$p-3-2*$i;$j++){
      #print "j-$j\n";
      if( $j % 2 == 0 ){
        $b .= substr($al,1+$i+$j/2,1);
      } else {
        $b .= substr($al,$i,1);
      }
    }
    $b .= substr($al,$i+1,1);
    #print "i=$i, b=$b\n";
  }
  while($n>0){
    print substr($b,0,$n<length($b) ? $n : length($b));
    $n -= length($b);
  }
}
print "\n";
exit(0);


Ezzat has an array of $$$n$$$ integers (maybe negative). He wants to split it into two non-empty subsequences $$$a$$$ and $$$b$$$, such that every element from the array belongs to exactly one subsequence, and the value of $$$f(a) + f(b)$$$ is the maximum possible value, where $$$f(x)$$$ is the average of the subsequence $$$x$$$. A sequence $$$x$$$ is a subsequence of a sequence $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) elements.The average of a subsequence is the sum of the numbers of this subsequence divided by the size of the subsequence.For example, the average of $$$[1,5,6]$$$ is $$$(1+5+6)/3 = 12/3 = 4$$$, so $$$f([1,5,6]) = 4$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$)— the number of test cases. Each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3\cdot10^5$$$.

For each test case, print a single value — the maximum value that Ezzat can achieve. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$.

#!/usr/bin/perl
use Data::Dumper;
#use integer;   ### important!
my $mod = 10 ** 9 + 7;
# my $mod = 998244353;

my ($test_case) = map { $_ - 0 } split(/\s+/,<STDIN>);

while( $test_case -- > 0 ){
  my ($n) = map { $_ - 0 } split(/\s+/,<STDIN>);
  my @A = map { $_ - 0 } split(/\s+/,<STDIN>);
  my $mxv = - $mod;
  my $s = 0;
  foreach my $i (0..($n-1)){
    $s += $A[$i];
    $mxv = $A[$i] if $A[$i] > $mxv;
  }
  $s -= $mxv;
  my $res = $mxv + ( $s / ($n-1) );
  print "$res\n";
}

exit(0);


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$$$?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$; $$$1 \le k \le 10^9$$$) — the number of spots and hour $$$k$$$.

For each test case, print one integer — the index of the spot where cat B will sleep at hour $$$k$$$.

NoteIn the first and second test cases $$$n = 2$$$, so:   at the $$$1$$$-st hour, A is on spot $$$2$$$ and B is on $$$1$$$;  at the $$$2$$$-nd hour, A moves to spot $$$1$$$ and B — to $$$2$$$.  If $$$n = 3$$$ then:   at the $$$1$$$-st hour, A is on spot $$$3$$$ and B is on $$$1$$$;  at the $$$2$$$-nd hour, A moves to spot $$$2$$$; B'd like to move from $$$1$$$ to $$$2$$$, but this spot is occupied, so it moves to $$$3$$$;  at the $$$3$$$-rd hour, A moves to spot $$$1$$$; B also would like to move from $$$3$$$ to $$$1$$$, but this spot is occupied, so it moves to $$$2$$$. In the sixth test case:   A's spots at each hour are $$$[5, 4, 3, 2, 1]$$$;  B's spots at each hour are $$$[1, 2, 4, 5, 2]$$$.


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.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 131~072$$$) — the length of $$$s$$$. It is guaranteed that $$$n = 2^k$$$ for some integer $$$k \ge 0$$$. The second line of the test case contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

For each test case, print the answer — the minimum number of moves required to obtain an 'a'-good string from $$$s$$$ (i.e. $$$c$$$-good string with $$$c =$$$ 'a'). It is guaranteed that the answer exists.

6
8
bbdcaaaa
8
asdfghjk
8
ceaaaabb
8
bbaaddcc
1
z
2
ac

#!/usr/bin/perl

my ($t) = map { $_ - 0 } split(/\s+/,<STDIN>);

my $chr = 'abcdefghijklmnopqrstuvwxyz';

my %cnt = ();
my $k;
for( my $t_loop=0; $t_loop<$t; $t_loop++ ){

  my ($n) = map { $_ - 0 } split(/\s+/,<STDIN>);
  my $s = scalar(<STDIN>);

  $k = 0;
  for($k=0;$k<26;$k++){
    last if $n == 2**$k;
  }

  if( $k == 0 ){
    if( substr($s,0,1) eq 'a'){
      print "0\n";
    } else {
      print "1\n";
    }
    next;
  }

  %cnt = ();
  for(my $i=0;$i<$n;$i++){
    my $bin = sprintf("%0${k}b",$i);
    my $c = substr($s,$i,1);
    for(my $j=1;$j<=$k;$j++){
      $cnt{substr($bin,0,$j)}->{$c} ++;
    }
  }

  my $res = &f(0,'');
  print "$res\n";

}

exit(0);

sub f {
  my $ci = shift;
  my $ch = substr($chr,$ci,1);

  my $keystr = shift;

  if( length($keystr) == $k ){
    return (1 - $cnt{$keystr}->{$ch});
  }

  my $n2 = 2 ** ($k - length($keystr)-1);

  return
    min(
      ($n2 - $cnt{$keystr . '0'}->{$ch}) + &f($ci+1,$keystr . '1') ,
      ($n2 - $cnt{$keystr . '1'}->{$ch}) + &f($ci+1,$keystr . '0') )
      ;
}

sub max {
  my $x = shift;
  my $y = shift;
  return ( $x > $y ? $x : $y );
}
sub min {
  my $x = shift;
  my $y = shift;
  return ( $x > $y ? $y : $x );
}


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.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \le n \le 5 \cdot 10^4$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$; $$$a_{i - 1} \le a_i$$$) — the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

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.

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.

use strict;
use v5.020;

my $try = <>;
while($try-- > 0){
    my $n = <>;
    my @arr = split(' ', <>);
    my $found = 0;
    while(@arr >= 3){
        my $last = pop @arr;

        if($arr[1] + $arr[0] <= $last){
            $found = 1;
            last;
        }
    }
    if($found > 0){
        printf("1 2 %d\n", @arr+1);
    } else {
        say "-1";
    }
}

use strict;
use v5.020;

my $try = <>;
while($try-- > 0){
    my $n = <>;
    my @arr = split(' ', <>);
    my $found = 0;
    while(@arr >= 3){
        my $last = pop @arr;

        if($arr[1] + $arr[0] < $last){
            $found = 1;
            last;
        }
    }
    if($found > 0){
        printf("1 2 %d\n", @arr+1);
    } else {
        say "-1";
    }
}


Are you going to Scarborough Fair?Parsley, sage, rosemary and thyme.Remember me to one who lives there.He once was the true love of mine.Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there.Willem asks his friend, Grick for directions, Grick helped them, and gave them a task.Although the girl wants to help, Willem insists on doing it by himself.Grick gave Willem a string of length n.Willem needs to do m operations, each operation has four parameters l, r, c1, c2, which means that all symbols c1 in range [l, r] (from l-th to r-th, including l and r) are changed into c2. String is 1-indexed.Grick wants to know the final string after all the m operations.

The first line contains two integers n and m (1 ≤ n, m ≤ 100). The second line contains a string s of length n, consisting of lowercase English letters. Each of the next m lines contains four parameters l, r, c1, c2 (1 ≤ l ≤ r ≤ n, c1, c2 are lowercase English letters), separated by space.

Output string s after performing m operations described above.

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.


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. e., $$$\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.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 1000$$$) — 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$$$) — $$$a_i$$$ is the weight of the $$$i$$$-th piece of cake.

For each test case, print a single integer: the maximum tastiness of the cake after doing the operation at most once.

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.

#!/usr/bin/env perl

$tests = <STDIN>;

for ($_=0;$_<$tests;$_++)
{
    $n= <STDIN>;
    @list = split (" ",<STDIN>);
    @list = sort {$a <=> $b} @list;
    print $list[-1]+$list[-2];
    print "\n"
}


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.

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.

In a single line print the date of the Apocalypse. It is guaranteed that such date exists and is unique.