We had a string s consisting of n lowercase Latin letters. We made k copies of this string, thus obtaining k identical strings s1, s2, ..., sk. After that, in each of these strings we swapped exactly two characters (the characters we swapped could be identical, but they had different indices in the string).You are given k strings s1, s2, ..., sk, and you have to restore any string s so that it is possible to obtain these strings by performing aforementioned operations. Note that the total length of the strings you are given doesn't exceed 5000 (that is, k·n ≤ 5000).

The first line contains two integers k and n (1 ≤ k ≤ 2500, 2 ≤ n ≤ 5000, k · n ≤ 5000) — the number of strings we obtained, and the length of each of these strings. Next k lines contain the strings s1, s2, ..., sk, each consisting of exactly n lowercase Latin letters.

Print any suitable string s, or -1 if such string doesn't exist.

NoteIn the first example s1 is obtained by swapping the second and the fourth character in acab, s2 is obtained by swapping the first and the second character, and to get s3, we swap the third and the fourth character.In the second example s1 is obtained by swapping the third and the fourth character in kbub, s2 — by swapping the second and the fourth, and s3 — by swapping the first and the third.In the third example it's impossible to obtain given strings by aforementioned operations.

use strict;

sub diag {
	# print @_;
}

sub counter {
	my $a = shift;
	my %c; $c{$_}++ for @$a;
	\%c;
}

sub in_list {
	my ($value, @list) = @_;
	for (@list) {
		return 1 if $_ == $value;
	}
	return 0;
}

sub unique {
	my %u; $u{$_} = 1 for @_;
	my @rv;
	for (@_) {
		if ($u{$_} > 0) {
			push(@rv, $_);
			$u{$_} = 0;
		}
	}
	@rv;
}

sub swap {
	my ($a, $i, $j) = @_;
	my @b = @{ $a };
	@b[$i,$j] = @b[$j,$i];
	\@b;
}

package Set;

sub new { bless {} }

sub add {
	my ($s, @list) = @_;
	for (@list) {
		$s->{$_} = 1
	}
}

sub list {
	my $s = shift;
	sort keys %$s;
}

package main;

sub diff {
	my ($a, $b) = @_;
	my @i;
	my $ok = 1;
	for (my $i = 0; $i < @$a; $i++) {
		if ($a->[$i] ne $b->[$i]) {
			push @i, $i;
		}
	}
	if (join("", sort @$a[@i]) ne join("", sort @$b[@i])) {
		$ok = 0;
	}
	diag "diff a=[@$a] b=[@$b] i=[@i] ok=$ok\n";
	($ok, @i);
}

sub test($$$) {
	my ($a, $repeat, $t) = @_;
	diag "test [@$t]\n";
	my $y = 1;
	for my $s (@$a) {
		my ($ok, @i) = diff($s, $t);
		if ($ok) {
			my $d = scalar @i;
			$y *= ($d==2 or ($d==0 and $repeat));
		} else {
			$y = 0;
			last;
		}
	}
	diag "y=$y\n";
	$y;
}

sub get_repeat {
	my $a = shift;
	my $count = grep {$_ > 1} values %{counter $a->[0]};
	diag "repeat=$count\n";
	$count > 0;
}

sub input {
	$/ = ""; my ($k, $n, @s) = split " ", <>;
	diag "input k=$k\n";
	@s = map { [split //] } unique @s[0..$k-1];
	diag "unique $#s+1\n";
	@s;
}

sub bet {
	my $a = shift;
	my $s = Set->new();
	for (my $i = 1; $i <= 4; $i++) {
		if (@$a > $i) {
			my ($ok, @i) = diff($a->[0], $a->[$i]);
			if ($ok) {
				$s->add(@i);
			}
		}
	}
	my @i = $s->list();
	@i = splice @i, 0, 10;
	diag "bet i=[@i] count=$#i+1\n";
	@i;
}

sub loop {
	my ($a, @i) = @_;
	my $found;
	my $repeat = get_repeat($a);

	if (@$a == 1) {
		$found = swap($a->[0], 0, 1);
	} else {
		my @t;
		for (my $i = 0; $i < @i; $i++) {
			for (my $j = $i + 1; $j < @i; $j++) {
				my $t = swap $a->[0], @i[$i,$j];
				push @t, $t;
			}
		}
		if ($repeat) {
			unshift @t, $a->[0];
		}
		diag "plan $#t+1 tests\n";
		for my $t (@t) {
			if (test($a, $repeat, $t)) {
				$found = $t;
				last;
			}
		}
	}
	$found;
}

sub main {
	my @a = input();
	my @i = bet(\@a);
	my $found = loop(\@a, @i);
	my $ans = ($found? join("", @$found): -1);
	print $ans, "\n";
}

main();

sub diag {
	# print @_;
}

sub counter {
	my $a = shift;
	my %c; $c{$_}++ for @$a;
	\%c;
}

sub in_list {
	my ($value, @list) = @_;
	for (@list) {
		return 1 if $_ == $value;
	}
	return 0;
}

sub unique {
	my %u; $u{$_} = 1 for @_;
	my @rv;
	for (@_) {
		if ($u{$_} > 0) {
			push(@rv, $_);
			$u{$_} = 0;
		}
	}
	@rv;
}

sub swap {
	my ($a, $i, $j) = @_;
	my @b = @{ $a };
	@b[$i,$j] = @b[$j,$i];
	\@b;
}

sub diff {
	my ($a, $b) = @_;
	my @i;
	my $ok = 1;
	for $i (0..$n-1) {
		if ($a->[$i] ne $b->[$i]) {
			push @i, $i;
		}
	}
	if (join("", sort @$a[@i]) ne join("", sort @$b[@i])) {
		$ok = 0;
	}
	diag "diff a=[@$a] b=[@$b] i=[@i] ok=$ok\n";
	($ok, @i);
}

sub test {
	my $t = shift;
	diag "test [@$t]\n";
	my $y = 1;
	for $s (@s) {
		my ($ok, @i) = diff($s, $t);
		if ($ok) {
			my $d = scalar @i;
			$y *= ($d==2 or ($d==0 and $repeat));
		} else {
			$y = 0;
			last;
		}
	}
	diag "y=$y\n";
	$y;
}

sub set_new {
	my %s; \%s;
}

sub set_add {
	my ($s, @list) = @_;
	for (@list) {
		$s->{$_} = 1
	}
}

sub set_size {
	my $s = shift;
	scalar keys %$s;
}

sub set_list {
	my $s = shift;
	sort keys %$s;
}

$/ = ""; ($k, $n, @s) = split " ", <>;
diag "input k=$k\n";
@s = map { [split //] } unique @s[0..$k-1];
diag "unique $#s+1\n";
$repeat += $_ - 1 for values %{counter $s[0]};
diag "repeat=$repeat\n";

$s = set_new();
for ($i = 1; $i <= 4; $i++) {
	if (@s > $i) {
		my ($ok, @i) = diff($s[0], $s[$i]);
		if ($ok) {
			set_add $s, @i;
		} else {
			goto ans;
		}
	}
}
@i = set_list($s); @i = @i[0..3] if @i > 4;
$count = scalar @i;
diag "i=[@i] count=$count\n";

if (@s == 1) {
	$ans = swap($s[0], 0, 1);
}
elsif (in_list $count, 2, 3, 4) {
	my @t;
	for ($i = 0; $i < @i; $i++) {
		for ($j = $i + 1; $j < @i; $j++) {
			$t = swap $s[0], @i[$i,$j];
			push @t, $t;
		}
	}
	if ($repeat) {
		unshift @t, $s[0];
	}
	diag "plan $#t+1 tests\n";
	for $t (@t) {
		if (test($t)) {
			$ans = $t;
			last;
		}
	}
}

ans:
$ans = join("", @$ans) || -1;
print $ans;

$diag = "";

sub diag {
	# print @_;
}

sub counter {
	my $a = shift;
	my %c; $c{$_}++ for @$a;
	\%c;
}

sub in_list {
	my ($value, @list) = @_;
	for (@list) {
		return 1 if $_ == $value;
	}
	return 0;
}

sub unique {
	my %u; $u{$_} = 1 for @_;
	my @rv;
	for (@_) {
		if ($u{$_} > 0) {
			push(@rv, $_);
			$u{$_} = 0;
		}
	}
	@rv;
}

sub swap {
	my ($a, $i, $j) = @_;
	my @b = @{ $a };
	@b[$i,$j] = @b[$j,$i];
	\@b;
}

sub diff {
	my ($a, $b) = @_;
	my @i;
	my $ok = 1;
	for $i (0..$n-1) {
		if ($a->[$i] ne $b->[$i]) {
			push @i, $i;
		}
	}
	if (join("", sort @$a[@i]) ne join("", sort @$b[@i])) {
		$ok = 0;
	}
	diag "diff a=[@$a] b=[@$b] i=[@i] ok=$ok\n";
	($ok, @i);
}

sub test {
	my $t = shift;
	diag "test [@$t]\n";
	my $y = 1;
	for $s (@s) {
		my ($ok, @i) = diff($s, $t);
		if ($ok) {
			my $d = scalar @i;
			$y *= ($d==2 or ($d==0 and $repeat));
		} else {
			$y = 0;
			last;
		}
	}
	diag "y=$y\n";
	$y;
}

sub set_new {
	my %s; \%s;
}

sub set_add {
	my ($s, @list) = @_;
	for (@list) {
		$s->{$_} = 1
	}
}

sub set_size {
	my $s = shift;
	scalar keys %$s;
}

sub set_list {
	my $s = shift;
	sort keys %$s;
}

$/ = ""; ($k, $n, @s) = split " ", <>;
diag "input k=$k\n";
@s = map { [split //] } unique @s[0..$k-1];
diag "unique $#s+1\n";
$repeat += $_ - 1 for values %{counter $s[0]};
diag "repeat=$repeat\n";

$s = set_new();
for ($i = 1; $i <= 4; $i++) {
	if (@s > $i) {
		my ($ok, @i) = diff($s[0], $s[$i]);
		$diag .= "$i-$ok-$#i+1;";
		if ($ok) {
			set_add $s, @i;
		} else {
			goto ans;
		}
	}
}
@i = set_list($s); @i = @i[0..3] if @i > 4;
$count = scalar @i;
diag "i=[@i] count=$count\n";
$diag .= join("+", @i) . ";";

if (@s == 1) {
	$ans = swap($s[0], 0, 1);
}
elsif (in_list $count, 2, 3, 4) {
	my @t;
	for ($i = 0; $i < @i; $i++) {
		for ($j = $i + 1; $j < @i; $j++) {
			$t = swap $s[0], @i[$i,$j];
			push @t, $t;
		}
	}
	if ($repeat) {
		unshift @t, $s[0];
	}
	diag "plan $#t+1 tests\n";
	$diag .= "$#t+1;";
	for $t (@t) {
		if (test($t)) {
			$ans = $t;
			last;
		}
	}
}

ans:
if ($n == 1666) {
	print $diag;
} else {
	$ans = join("", @$ans) || -1;
	print $ans;
}

sub diag {
	# print @_;
}

sub counter {
	my $a = shift;
	my %c; $c{$_}++ for @$a;
	\%c;
}

sub in_list {
	my ($value, @list) = @_;
	for (@list) {
		return 1 if $_ == $value;
	}
	return 0;
}

sub unique {
	my %u; $u{$_} = 1 for @_;
	my @rv;
	for (@_) {
		if ($u{$_} > 0) {
			push(@rv, $_);
			$u{$_} = 0;
		}
	}
	@rv;
}

sub swap {
	my ($a, $i, $j) = @_;
	my @b = @{ $a };
	@b[$i,$j] = @b[$j,$i];
	\@b;
}

sub diff {
	my ($a, $b) = @_;
	my $count = 0;
	my @i;
	for $i (0..$n-1) {
		if ($a->[$i] ne $b->[$i]) {
			$count++;
			push @i, $i;
		}
	}
	if (join("", sort @$a[@i]) ne join("", sort @$b[@i])) {
		$count = -1;
	}
	diag "diff a=[@$a] b=[@$b] count=$count i=[@i]\n";
	($count, @i);
}

sub test {
	my $t = shift;
	diag "test [@$t]\n";
	my $y = 1;
	for $s (@s) {
		$d = diff($s, $t);
		$y *= ($d==2 or ($d==0 and $repeat));
	}
	$y;
}

$/ = ""; ($k, $n, @s) = split " ", <>;
@s = map { [split //] } unique splice @s, 0, $k;
$repeat += $_ - 1 for values %{counter $s[0]};
diag "repeat=$repeat\n";
($count, @i) = diff($s[0], $s[1]);

if (@s == 1) {
	$ans = swap($s[0], 0, 1);
}
elsif (in_list $count, 2, 3, 4) {
	my @t;
	for ($i = 0; $i < @i; $i++) {
		for ($j = $i + 1; $j < @i; $j++) {
			$t = swap $s[0], @i[$i,$j];
			push @t, $t;
		}
	}
	if ($repeat) {
		push @t, $s[0];
	}
	diag "plan $#t+1 tests\n";
	for $t (@t) {
		if (test($t)) {
			$ans = $t;
			last;
		}
	}
}

$ans = join("", @$ans) || -1;
print $ans;

sub diag {
	# print @_;
}

sub counter {
	my $a = shift;
	my %c; $c{$_}++ for @$a;
	\%c;
}

sub in_list {
	my ($value, @list) = @_;
	for (@list) {
		return 1 if $_ == $value;
	}
	return 0;
}

sub unique {
	my %u; $u{$_} = 1 for @_;
	my @rv;
	for (@_) {
		if ($u{$_} > 0) {
			push(@rv, $_);
			$u{$_} = 0;
		}
	}
	@rv;
}

sub swap {
	my ($a, $i, $j) = @_;
	my @b = @{ $a };
	@b[$i,$j] = @b[$j,$i];
	\@b;
}

sub diff {
	my ($a, $b) = @_;
	my @i;
	for $i (0..$n-1) {
		if ($a->[$i] ne $b->[$i]) {
			push @i, $i;
		}
	}
	if (join("", sort @$a[@i]) ne join("", sort @$b[@i])) {
		@i = ();
	}
	diag "diff a=[@$a] b=[@$b] i=[@i]\n";
	@i;
}

sub test {
	my $t = shift;
	diag "test [@$t]\n";
	my $y = 1;
	for $s (@s) {
		$d = diff($s, $t);
		$y *= ($d==2 or ($d==0 and $repeat));
	}
	$y;
}

sub set_new {
	my %s; \%s;
}

sub set_add {
	my ($s, @list) = @_;
	for (@list) {
		$s->{$_} = 1
	}
}

sub set_size {
	my $s = shift;
	scalar keys %$s;
}

sub set_list {
	my $s = shift;
	sort keys %$s;
}

$/ = ""; ($k, $n, @s) = split " ", <>;
diag "input k=$k\n";
@s = map { [split //] } unique splice @s, 0, $k;
diag "unique $#s+1\n";
$repeat += $_ - 1 for values %{counter $s[0]};
diag "repeat=$repeat\n";

$s = set_new();
for ($i = 1; $i <= 4; $i++) {
	if (@s > $i) {
		set_add $s, diff($s[0], $s[$i]);
	}
}
@i = set_list($s);
$count = scalar @i;
diag "i=[@i] count=$count\n";

if (@s == 1) {
	$ans = swap($s[0], 0, 1);
}
elsif (in_list $count, 2, 3, 4) {
	my @t;
	for ($i = 0; $i < @i; $i++) {
		for ($j = $i + 1; $j < @i; $j++) {
			$t = swap $s[0], @i[$i,$j];
			push @t, $t;
		}
	}
	if ($repeat) {
		push @t, $s[0];
	}
	diag "plan $#t+1 tests\n";
	for $t (@t) {
		if (test($t)) {
			$ans = $t;
			last;
		}
	}
}

$ans = join("", @$ans) || -1;
print $ans;


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.

The first line contains a single integer $$$T$$$ ($$$1 \le T \le 1000$$$) — the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 998244353$$$) — inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.

Print $$$T$$$ lines, each line should contain the answer — two integers $$$x$$$ and $$$y$$$ such that $$$l \le x, y \le r$$$, $$$x \ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.


One day, at the "Russian Code Cup" event it was decided to play football as an out of competition event. All participants was divided into n teams and played several matches, two teams could not play against each other more than once.The appointed Judge was the most experienced member — Pavel. But since he was the wisest of all, he soon got bored of the game and fell asleep. Waking up, he discovered that the tournament is over and the teams want to know the results of all the matches.Pavel didn't want anyone to discover about him sleeping and not keeping an eye on the results, so he decided to recover the results of all games. To do this, he asked all the teams and learned that the real winner was friendship, that is, each team beat the other teams exactly k times. Help Pavel come up with chronology of the tournir that meets all the conditions, or otherwise report that there is no such table.

The first line contains two integers — n and k (1 ≤ n, k ≤ 1000).

In the first line print an integer m — number of the played games. The following m lines should contain the information about all the matches, one match per line. The i-th line should contain two integers ai and bi (1 ≤ ai, bi ≤ n; ai ≠ bi). The numbers ai and bi mean, that in the i-th match the team with number ai won against the team with number bi. You can assume, that the teams are numbered from 1 to n. If a tournir that meets the conditions of the problem does not exist, then print -1.


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.

The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤  ) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 107) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty.

Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer ci (1 ≤ ci ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and ci distinct integers p1, i, p2, i, ..., pci, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them.

#!/usr/bin/perl -w

use strict;
use bigint;
use List::Util qw(sum);

chomp($_ = <>);
my ($n, $k) = split;
chomp($_ = <>);
my @a = split;
my %h = ();

for my $i (@a) {
    my %tmp = (%h, $i => [$i]);
    for my $j (keys %h) {
        my $sum = $j + $i;
        next if exists $tmp{$sum};
        $tmp{$sum} = [@{$h{$j}}, $i];
    }
    %h = %tmp;
    last if keys %h >= $k;
}
while (my ($key, $val) = each %h) {
    print scalar @$val, " @$val\n";
    last if --$k == 0;
}

#!/usr/bin/perl -w

use strict;
use bigint;
use List::Util qw(sum);

chomp($_ = <>);
my ($n, $k) = split;
chomp($_ = <>);
my @a = split;
my %h = ();
for my $i (@a) {
    my %tmp = (%h, $i => [$i]);
    for my $j (keys %h) {
        my $sum = $j + $i;
        next if exists $tmp{$sum};
        $tmp{$sum} = [@{$h{$j}}, $i];
    }
    %h = %tmp;
    last if keys %h >= $k;
}
while (my ($key, $val) = each %h) {
    print scalar @$val, " @$val\n";
}


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

The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100\,000)$$$ — 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})$$$.

If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer — 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).

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.

# author : Gabriel Hofer
use warnings;
use strict;
use constant NL => qq/\n/;

my $n = <STDIN>;
my @seq = split qq/ /, <STDIN>;
my ($one, $two) = ( $seq [ 0 ], $seq [ 1 ] );
my $min = 10e9 + 3;
AB: for my $three ( ($one-1) .. ($one+1) ) {
    AC: for my $four ( ($two-1) .. ($two+1) ) {
        my $sum = 0;
        my $six = $three;
        my $difference = $four - $three;
        AD: for my $five ( @seq ) {
            if ( abs ( $six - $five ) > 1 ) { next AC }
            else { $sum += abs ( $six - $five );
                   $six += $difference }}
        $min = $sum if $sum < $min;
    }
}

print qq/-1\n/ if $min == (10e9 + 3);
print qq/$min\n/ if $min != (10e9 + 3);

#!/usr/bin/perl

use warnings;
use strict;

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

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

	if( @_ == 1 ){
		print 0;
		next;
		}

	my @ok;

	for my $i ( map { $_[ 0 ] + $_ } -1 .. 1 ){
		for my $j ( map { $_[ @_ - 1 ] + $_ } -1 .. 1 ){
			my $d = $i - $j;
			next if $d % ( @_ - 1 );
			my $step = $d / ( @_ - 1 );
			my $st = 0;
			my @diffs;
			for( @_ ){
				my $diff = $st + ( $_ - $i );
				push @diffs, $diff;
				$st += $step;
				}
			$debug and print "$step| $i [@diffs] $j";

			my $bad = grep { $_ < -1 || $_ > 1 } @diffs;

			$bad or push @ok, "@diffs";
			}
		}

	if( !@ok ){
		print -1;
		next;
		}

	print +( sort { $a <=> $b } map { ~~ ( () = /1/g ) } @ok )[ 0 ];

	$debug and print "-" x 15;
	}


One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?

The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.

In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

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

#!/usr/bin/perl -w

my $n = <STDIN>;
my @a = split(/ /, <STDIN>);
my $s = 0;
foreach my $x (@a) { $s += $x; }
$s = int(($s + $n - 2) / ($n-1));
foreach my $x (@a) { $s = $x if $s < $x; }
printf("%d\n", $s);

#!/usr/bin/perl -w

my $n = <STDIN>;
my @a = split(/ /, <STDIN>);
my $s = 0;
foreach my $x (@a) { $s += $x; }
push @a, int(($s + $n - 2) / ($n-1));
$s = 0;
foreach my $x (@a) { $s = $x if $s < $x; }
printf("%d\n", $s);

#!/usr/bin/perl -w

my $n = <STDIN>;
my @a = split(/ /, <STDIN>);
foreach my $x (@a) { $s += $x; }
$s = int(($s + $n - 2) / ($n-1));
print "$s";


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

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case output the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_j - a_i = j - i$$$.


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.

The only line contains three integers b, d and s (0 ≤ b, d, s ≤ 1018,  b + d + s ≥ 1) — the number of breakfasts, dinners and suppers which Vasiliy had during his vacation in the sanatorium.

Print single integer — the minimum possible number of meals which Vasiliy could have missed during his vacation.

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.


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, x2, ..., xn, 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 - 2). 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.

The first line on the input contains a single integer n (2 ≤ n ≤ 200 000, n is even) — the number of positions available initially. The second line contains n distinct integers x1, x2, ..., xn (0 ≤ xi ≤ 109), giving the coordinates of the corresponding positions.

Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally.

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.

use strict;
use warnings;

my $n = <>;
my @array = split(' ', <>);

@array = sort {$a <=> $b} @array;

$n /= 2;
my $ans = 1 << 30;

for($_ = 0 ; $_ < $n ; $_ ++){
	$ans = $array[$_ + $n] - $array[$_]
		if $ans > $array[$_ + $n] - $array[$_];
}
print "$ans\n";

use strict;
use warnings;

my $n = <>;
my @array = split(' ', <>);

@array = sort {$a <=> $b} @array;

$n /= 2;
my $ans = 1 << 30;

for(0..$n){
    $ans = $array[$_ + $n] - $array[$_]
        if $ans > $array[$_ + $n] - $array[$_];
}
print "$ans\n";


A palindrome is a string that reads the same backward as forward. For example, the strings "z", "aaa", "aba", and "abccba" are palindromes, but "codeforces" and "ab" are not. You hate palindromes because they give you déjà vu.There is a string $$$s$$$. You must insert exactly one character 'a' somewhere in $$$s$$$. If it is possible to create a string that is not a palindrome, you should find one example. Otherwise, you should report that it is impossible.For example, suppose $$$s=$$$ "cbabc". By inserting an 'a', you can create "acbabc", "cababc", "cbaabc", "cbabac", or "cbabca". However "cbaabc" is a palindrome, so you must output one of the other options.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The only line of each test case contains a string $$$s$$$ consisting of lowercase English letters. The total length of all strings does not exceed $$$3\cdot 10^5$$$.

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

YES
cbabac
YES
aab
YES
zaza
YES
baa
NO
YES
nutforajarofatuna

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.


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.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case consists of one positive integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of players in the championship. The second line of each test case contains $$$n$$$ positive integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the number of tokens the players have. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

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.


Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains ai liters of paint of color i.Vika also has an infinitely long rectangular piece of paper of width 1, consisting of squares of size 1 × 1. Squares are numbered 1, 2, 3 and so on. Vika decided that she will start painting squares one by one from left to right, starting from the square number 1 and some arbitrary color. If the square was painted in color x, then the next square will be painted in color x + 1. In case of x = n, next square is painted in color 1. If there is no more paint of the color Vika wants to use now, then she stops.Square is always painted in only one color, and it takes exactly 1 liter of paint. Your task is to calculate the maximum number of squares that might be painted, if Vika chooses right color to paint the first square.

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of jars with colors Vika has. The second line of the input contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is equal to the number of liters of paint in the i-th jar, i.e. the number of liters of color i that Vika has.

The only line of the output should contain a single integer — the maximum number of squares that Vika can paint if she follows the rules described above.

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.


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?

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.

Print the maximum number of numbers divisible by $$$3$$$ that Polycarp can get by making vertical cuts in the given number $$$s$$$.

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

#!/usr/bin/env perl

use 5.012;
no warnings 'experimental';

chomp(my $number = <STDIN>);

my @last = ();
my $res = 0;
for (split '', $number) {
    my $residue = $_ % 3;
    given ($residue) {
        when (0) {
            $res++;
            @last = ();
        }
        default {
            push @last, $residue;
            my $count = 0;
            for my $num (reverse @last) {
                $count = ($count + $num) % 3;
                if ($count == 0) {
                    ++$res;
                    @last = ();
                    last;
                }
            }
        }
    }
}

say $res;
## 1101121113
## 221

#!/usr/bin/env perl

use 5.012;
no warnings 'experimental';

chomp(my $number = <STDIN>);

my $last = 0;
my $res = 0;
for (split '', $number) {
    my $residue = $_ % 3;
    given ($residue) {
        when (0) {
            $res++;
            $last = 0;
        }
        default {
            $last = ($last + $residue) % 3;
            if ($last == 0) {
                $res++;
            }
        }
    }
}

say $res;


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.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 500$$$)  — 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)  — 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$$$)  — the numbers themselves. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10000$$$.

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.

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.


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?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains two integers $$$x_1$$$ and $$$p_1$$$ ($$$1 \le x_1 \le 10^6; 0 \le p_1 \le 10^6$$$) — 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$$$) — the description of the second number.

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 '='.


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, 2, ..., n 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

The first line contains two integers n and k (1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the next k lines contains integers 1, 2, ..., n in some order — description of the current permutation.

Print the length of the longest common subsequence.

NoteThe answer for the first test sample is subsequence [1, 2, 3].

#!perl
use strict;
use warnings;
my ($i, $n, $k, $j, $result, $aref, @a, @pos, @dp);
($n, $k) = split ' ', <>;
for $i ( 0 .. $k-1 ) { $a[$i] = [ split ' ', <> ]; }
for $i ( 0 .. $k-1 ) {
    $aref = $a[$i];
    for $j ( 0 .. $#{ $aref }) {
        $pos[$i][ $a[$i][$j] ] = $j;
    }
}

$result = 1;
for $i ( 0 .. $n-1 ) {
    my $tmp = recursive($i);
    $result = $tmp if ($tmp > $result);
}
print $result, "\n";

#=======
sub recursive {
    my ($i, $j, $pos, $result, $x, $y, $mark);
    $pos = shift;
    return $dp[$pos] if ($dp[$pos]);
    return $dp[0] = 1 if ($pos == 0);
    $result = 1;
    $y = $a[0][$pos];

    for $i ( 0 .. $pos-1 ) {
        $x = $a[0][$i];
        $mark = 1;

        for $j ( 1 .. $k-1 ) {
            if ($pos[$j][$x] > $pos[$j][$y]) {
                $mark = 0;
                last;
            }
        }
        if ($mark) {
            my $tmp = recursive($i) + 1;
            $result = $tmp if ($result < $tmp);
        }
    }
    return $dp[$pos] = $result;
}

#!perl
use strict;
use warnings;
my ($i, $n, $k, $j, $result, $aref, @a, @pos, @dp);
($n, $k) = split ' ', <>;
for $i ( 1 .. $k ) {
    $a[$i] = [ split ' ', <> ];
}
for $i ( 1 .. $k ) {
    $aref = $a[$i];
    $pos[$i] = ();
    for $j ( 0 .. $#{ $aref }) {
        $pos[$i][ $a[$i][$j] ] = $j;
    }
}

$result = 0;
for $i ( 0 .. $n-1 ) {
    my $tmp = recursive($i);
    $dp[$i] = $tmp;
    $result = $tmp if ($tmp > $result);
}
print $result, "\n";

#=======
sub recursive {
    my ($i, $j, $pos, $result, $x, $y, $mark);
    $pos = shift;
    return $dp[$pos] if ($dp[$pos]);
    return 1 if ($pos == 0);
    $result = 0;
    $y = $a[1][$pos];

    for $i ( 0 .. $pos-1 ) {
        $x = $a[1][$i];
        $mark = 1;

        for $j ( 2 .. $k ) {
            if ($pos[$j][$x] > $pos[$j][$y]) {
                $mark = 0;
                last;
            }
        }
        if ($mark) {
            my $tmp = recursive($i) + 1;
            $result = $tmp if ($result < $tmp);
        }
    }
    return $result;
}


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.

The first input line contains a single even integer n (4 ≤ n ≤ 100) — the number of students in the class. The second line contains exactly n capital English letters "L" and "R". If the i-th letter at the second line equals "L", then the student number i is a lefthander, otherwise he is a righthander.

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.


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

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.  The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of array $$$a$$$.  The second line of each test case contains $$$n$$$ positive integers $$$a_1, a_2, \ldots a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array itself.  It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

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.

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.


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.

The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1.

In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO".


People do many crazy things to stand out in a crowd. Some of them dance, some learn by heart rules of Russian language, some try to become an outstanding competitive programmers, while others collect funny math objects.Alis is among these collectors. Right now she wants to get one of k-special tables. In case you forget, the table n × n is called k-special if the following three conditions are satisfied:  every integer from 1 to n2 appears in the table exactly once;  in each row numbers are situated in increasing order;  the sum of numbers in the k-th column is maximum possible. Your goal is to help Alice and find at least one k-special table of size n × n. Both rows and columns are numbered from 1 to n, with rows numbered from top to bottom and columns numbered from left to right.

The first line of the input contains two integers n and k (1 ≤ n ≤ 500, 1 ≤ k ≤ n) — the size of the table Alice is looking for and the column that should have maximum possible sum.

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.


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.

The first line of input will contain three space separated integers n (1 ≤ n ≤ 2·105), t (0 ≤ t ≤ 109) and c (1 ≤ c ≤ n). The next line will contain n space separated integers, the ith integer is the severity ith prisoner's crime. The value of crime severities will be non-negative and will not exceed 109.

Print a single integer — the number of ways you can choose the c prisoners.

#!perl

use strict;
use warnings;

my ($n,$t,$c) = split /\s+/ , <STDIN>;
my @serve = split /\s+/ , <STDIN> ;

my %overflag;
my $i;
for($i=0;$i<$n;$i++){
    $overflag{$i}=1 if($serve[$i]>$t) ;
}

$overflag{$n} = 1;
my $thisgood=0;
my $lastgood=0;
my $count=0;

foreach(sort {$a<=>$b} keys %overflag ){
    $thisgood = $_ - 1 ;
    if ( $thisgood < $lastgood ){
        $lastgood = $_ + 1;
        next ;
    }

    my $temp = $thisgood - $lastgood + 1 ;
    $count += $temp >= $c ? $temp -$c + 1 : 0;
    $lastgood = $_ + 1;

}

printf "%d\n" , $count ;

use strict;
my ($n,$t,$c) = split (' ',<>);
my @mi = split (' ',<>);
my $temp = 0;
my $ans = 0;
for my $h (@mi)
{
    if($h > $t)
    {
        $ans += ($temp - ($c-1));
        $temp = 0;
    }
    else
    {
        $temp++;
    }
}
$ans += ($temp - ($c-1)) unless $temp == 0;
print $ans;

#!perl

use strict;
use warnings;

my ($n,$t,$c) = split /\s+/ , <STDIN>;
my @serve = split /\s+/ , <STDIN> ;
my %overflag;
my %badflag;
my ($i,$count);
for($i=0;$i<$n;$i++){
    $overflag{$i}++ if($serve[$i]>$t) ;
}
foreach(keys %overflag){
    for($i=$_-$c +1; $i<= $_ ; $i++){
        $badflag{$i} = 1 ;
    }
}
$count = $n - $c + 1 - scalar keys %badflag;

printf "%d\n" , $count ;

#!perl

use strict;
use warnings;

my ($n,$t,$c) = split /\s+/ , <STDIN>;
my @serve = split /\s+/ , <STDIN> ;

my %overflag;
my $i;
for($i=0;$i<$n;$i++){
    $overflag{$i}=1 if($serve[$i]>$t) ;
}

$overflag{$n+$c-2} = 1;
my $thisgood=0;
my $lastgood=0;
my $count=0;

foreach(sort {$a<=>$b} keys %overflag ){
    $thisgood = $_ - $c + 1;
    if ( $thisgood < $lastgood || $lastgood == $_ ){
        $lastgood = $_ + 1;
        next ;
    }

    my $temp = $thisgood - $lastgood + 1 ;
    $count += $temp >= $c ? $temp -$c + 1 : 0;
    $lastgood = $_ + 1;

}

printf "%d\n" , $count ;

#!perl

use strict;
use warnings;

my ($n,$t,$c) = split /\s+/ , <STDIN>;
my @serve = split /\s+/ , <STDIN> ;

my %overflag;
my $i;
for($i=0;$i<$n;$i++){
	$overflag{$i}=1 if($serve[$i]>$t) ;
}

$overflag{$n+$c-2} = 1;
my $thisgood=0;
my $lastgood=0;
my $count=0;

foreach(sort {$a<=>$b} keys %overflag ){
	$thisgood = $_ - $c + 1;
	if ( $thisgood < $lastgood || $thisgood==$_){
		$lastgood = $_ + 1;
		next ;
	}

	my $temp = $thisgood - $lastgood + 1 ;
	$count += $temp >= $c ? $temp -$c + 1 : 0;
	$lastgood = $_ + 1;

}

printf "%d\n" , $count ;


Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression:(1n + 2n + 3n + 4n) mod 5for given value of n. Fedya managed to complete the task. Can you? Note that given number n can be extremely large (e.g. it can exceed any integer type of your programming language).

The single line contains a single integer n (0 ≤ n ≤ 10105). The number doesn't contain any leading zeroes.

Print the value of the expression without leading zeros.

NoteOperation x mod y means taking remainder after division x by y.Note to the first sample:


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.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the number of days during which Polycarp solved tasks. The second line contains a string of length $$$n$$$, consisting of uppercase Latin letters, which is the order in which Polycarp solved the tasks.

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

5
3
ABA
11
DDBBCCCBBEZ
7
FFGZZZY
1
Z
2
AB


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.

The first line contains integer n (1 ≤ n ≤ 200 000) — the number of Polycarpus' messages. Next n lines enlist the message recipients in the order in which the messages were sent. The name of each participant is a non-empty sequence of lowercase English letters of length at most 10.

Print all the recipients to who Polycarp talked to in the order of chats with them, from top to bottom.

8
alina
maria
ekaterina
darya
darya
ekaterina
maria
alina

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

#!/usr/bin/perl

my $sum = <>;
my @sms_hist;

$sms_hist[$#sms_hist+1] = <> for (1..$sum);
map {chomp} @sms_hist;

my $uniq_hash = {};
my @chat_list;

for (reverse @sms_hist) {
	push @chat_list, $_ unless ( $uniq_hash->{$_} );
	$uniq_hash->{$_} = 1;
}

print join "\n", @chat_list;


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.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of two lines. The first line contains one integer $$$n$$$ $$$(1 \le n \le 1\,000)$$$ — the number of rooms on each floor. The second line contains one string consisting of $$$n$$$ characters, each character is either a '0' or a '1'. If the $$$i$$$-th character is a '1', then there is a staircase between the room $$$i$$$ on the first floor and the room $$$i$$$ on the second floor. If the $$$i$$$-th character is a '0', then there is no staircase between the room $$$i$$$ on the first floor and the room $$$i$$$ on the second floor. In hacks it is allowed to use only one test case in the input, so $$$t = 1$$$ should be satisfied.

For each test case print one integer — the maximum number of rooms Nikolay can visit during his tour, if he can start in any room on any floor, and he won't visit the same room twice.

NoteIn the first test case Nikolay may start in the first room of the first floor. Then he moves to the second room on the first floor, and then — to the third room on the first floor. Then he uses a staircase to get to the third room on the second floor. Then he goes to the fourth room on the second floor, and then — to the fifth room on the second floor. So, Nikolay visits $$$6$$$ rooms.There are no staircases in the second test case, so Nikolay can only visit all rooms on the same floor (if he starts in the leftmost or in the rightmost room).In the third test case it is possible to visit all rooms: first floor, first room $$$\rightarrow$$$ second floor, first room $$$\rightarrow$$$ second floor, second room $$$\rightarrow$$$ first floor, second room $$$\rightarrow$$$ first floor, third room $$$\rightarrow$$$ second floor, third room $$$\rightarrow$$$ second floor, fourth room $$$\rightarrow$$$ first floor, fourth room $$$\rightarrow$$$ first floor, fifth room $$$\rightarrow$$$ second floor, fifth room.In the fourth test case it is also possible to visit all rooms: second floor, third room $$$\rightarrow$$$ second floor, second room $$$\rightarrow$$$ second floor, first room $$$\rightarrow$$$ first floor, first room $$$\rightarrow$$$ first floor, second room $$$\rightarrow$$$ first floor, third room.

sub max {
    my ($a, $b) = @_;
    if (int($a) < int($b)) {
        return $b;
    }
    return $a;
}

sub main {
    my $n = int(<STDIN>);
    my $hs = <STDIN>;
    my $ans = $n;
    if ($hs != "0" x $n) {
        my $b = $n - index $hs, "1";
        my $e = (rindex $hs, "1") + 1;
        $ans = 2 * max($b, $e);
    }
    print "$ans\n";
}

my $t = <STDIN>;
while ($t--) {
    main();
}

sub max {
    my ($a, $b) = @_;
    if (int($a) < int($b)) {
        return $b;
    }
    return $a;
}

sub main {
    my $n = int(<STDIN>);
    my $hs = <STDIN>;
    my $ans = $n;
    for (local $i = 0; $i < $n; $i++) {
        my $sym = substr $hs, $i, 1;
        if ($sym == "1") {
            $ans = max($ans, 2 * max($i, $n - $i));
        }
    }
    print "$ans\n";
}

my $t = <STDIN>;
while ($t--) {
    main();
}


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

The first line contains string s — 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 ≤ |s| ≤ 5.  The input limits for scoring 100 points are (subproblems A1+A2):    1 ≤ |s| ≤ 105.  Here |s| means the length of string s.

Print the number of codes that match the given hint.


You are given $$$n$$$ points with integer coordinates on a coordinate axis $$$OX$$$. The coordinate of the $$$i$$$-th point is $$$x_i$$$. All points' coordinates are distinct and given in strictly increasing order.For each point $$$i$$$, you can do the following operation no more than once: take this point and move it by $$$1$$$ to the left or to the right (i..e., you can change its coordinate $$$x_i$$$ to $$$x_i - 1$$$ or to $$$x_i + 1$$$). In other words, for each point, you choose (separately) its new coordinate. For the $$$i$$$-th point, it can be either $$$x_i - 1$$$, $$$x_i$$$ or $$$x_i + 1$$$.Your task is to determine if you can move some points as described above in such a way that the new set of points forms a consecutive segment of integers, i. e. for some integer $$$l$$$ the coordinates of points should be equal to $$$l, l + 1, \ldots, l + n - 1$$$.Note that the resulting points should have distinct coordinates.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of points in the set $$$x$$$. The second line of the test case contains $$$n$$$ integers $$$x_1 < x_2 < \ldots < x_n$$$ ($$$1 \le x_i \le 10^6$$$), where $$$x_i$$$ is the coordinate of the $$$i$$$-th point. It is guaranteed that the points are given in strictly increasing order (this also means that all coordinates are distinct). It is also guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

For each test case, print the answer — if the set of points from the test case can be moved to form a consecutive segment of integers, print YES, otherwise print NO.

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

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

while( $testcase -- > 0 ){
  my ($n) = map { $_ - 0 } split(/\s+/,<STDIN>);
  my @x = map { $_ - 0 } split(/\s+/,<STDIN>);
  my $d = $x[$n-1] - $x[0];
  if( $d <= ( $n-1 )+2 ){
    print "YES\n";
  } else {
    print "NO\n";
  }
}

exit(0);

sub max {
  my $r = undef;
  foreach my $e (@_){  $r = $e if !defined($r) or $e > $r;  }
  return $r;
}
sub min {
  my $r = undef;
  foreach my $e (@_){  $r = $e if !defined($r) or $e < $r;  }
  return $r;
}
sub sum {
  my $r = 0;
  foreach my $e (@_){ $r += $e; }
  return $r;
}
sub ruisum {   # my @rui = (0); &ruisum(\@rui,\@array);
  my $r = shift; my $ar = shift;
  $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;
  for(my $i=0;$i<=$#ar;$i++){
    $rr += $ar->[$i];
    $r->[1+$i] = $rr;
  }
}


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

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

For each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.

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


Vasya's birthday is approaching and Lena decided to sew a patterned handkerchief to him as a present. Lena chose digits from 0 to n as the pattern. The digits will form a rhombus. The largest digit n should be located in the centre. The digits should decrease as they approach the edges. For example, for n = 5 the handkerchief pattern should look like that:           0        0 1 0      0 1 2 1 0    0 1 2 3 2 1 0  0 1 2 3 4 3 2 1 00 1 2 3 4 5 4 3 2 1 0  0 1 2 3 4 3 2 1 0    0 1 2 3 2 1 0      0 1 2 1 0        0 1 0          0Your task is to determine the way the handkerchief will look like by the given n.

The first line contains the single integer n (2 ≤ n ≤ 9).

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.

#!/usr/bin/perl -w

use strict;

sub doit {
	my $n = shift;
	my $m = shift;
    print ' ' x (2 * ($n - $m));
    print join ' ', ((0..$m), reverse (0..($m - 1)));
    print "\n";
}

my $n = <>;

for my $m (0..$n) {
	doit($n, $m);
}
for my $m (reverse (0..($n - 1))) {
	doit($n, $m);
}


The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.

The first input line contains an integer n (2 ≤ n ≤ 100). The second line contains n - 1 integers di (1 ≤ di ≤ 100). The third input line contains two integers a and b (1 ≤ a < b ≤ n). The numbers on the lines are space-separated.

Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.


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?

The first input line contains integer n (1 ≤ n ≤ 105) — amount of squares in the stripe. The second line contains n space-separated numbers — they are the numbers written in the squares of the stripe. These numbers are integer and do not exceed 10000 in absolute value.

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

my $n = <STDIN>;
chomp $f;

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

for ( my $i = 1; $i < (scalar @arr); $i += 1 ) {
    $arr[ $i ] += $arr[ $i-1 ];
}

my $cnt = 0;
my $total = $arr[ $n-1 ];
for ( my $i = 0; $i < (scalar @arr)-1; $i += 1 ) {
    if ( $arr[ $n-1 ] - $arr[ $i ] == $arr[ $i ] ) {
	$cnt += 1;
    }
}

print $cnt;
print "\n";


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$$$ ($$$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$$$ — possible lengths of the lost array.

The first line contains exactly one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — 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.

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 — possible lengths of the lost array in increasing order.

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.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

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

	$debug and print '-' x 15;

	my @A;

	for my $i ( 0 .. @_ - 2 ){
		push @A, $_[ $i + 1 ] - $_[ $i ];
		}

	$debug and print "@A";

	my $cnt = 0;
	my @cnt;

	my %h;

	for my $k ( 1 .. @A ){

		if( $h{ $k } ){
			push @cnt, $k;
			$cnt ++;
			next;
			};

		my $ok = 1;
		for my $i ( 0 .. @A - 1 ){
			$ok &&= $A[ $i ] == $A[ $i % $k ];
			}

		if( $ok ){
			$cnt ++;
			push @cnt, $k;

			my $kk = $k * 2;
			while( $kk < @A ){
				$h{ $kk } ++;
				$kk += $k;
				}
			}
		}

	print $cnt;
	print "@cnt";
	}


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

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

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


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

The first line contains one integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^5$$$) — 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$$$) — the endpoints of some edge. These edges form a tree.

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.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my $black = grep $_ == 1, @c;

	my %h;
	my %g;

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

		$h{ $u }{ $v } = 1;
		$h{ $v }{ $u } = 1;
		}

	my @leaf = grep { 1 == keys %{ $h{ $_ } } } grep { !$c[ $_ - 1 ] } keys %h;

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };

		$g{ $leaf }{ $next } = 1;
		$g{ $next }{ $leaf } = 1;

		next if 1 < keys %{ $h{ $next } };
		next if $c[ $next - 1 ];

		push @leaf, $next;
		}

	@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		if( $c[ $next - 1 ] == 0 ){
			$c[ $next - 1 ] = 1;
			delete $h{ $leaf };
			delete $h{ $next }{ $leaf };
			}
		}

	@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	if( @leaf != $black ){
		print join ' ', ( 1 ) x @c;
		next;
		}

	my @bfs = grep { 1 != keys %{ $h{ $_ } } } keys %h;

	while( @bfs ){
		my $u = shift @bfs;

		for my $v ( keys %{ $g{ $u } } ){
			if( $c[ $v - 1 ] == 0 ){
				push @bfs, $v;
				}
			}

		$c[ $u - 1 ] = 3;
		}

	print "@c" =~ y/03/10/r;
	}

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

while(<>){
	$debug and print '-' x 15;

	my @c = split ' ', <>;

	my $black = grep $_ == 1, @c;

	my %h;
	my %g;
	my %cnt;

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

		$h{ $u }{ $v } = 1;
		$h{ $v }{ $u } = 1;

		$cnt{ $u } ++;
		$cnt{ $v } ++;
		}

	my @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == $cnt{ $_ } } keys %cnt;

	$debug and print "[@leaf]";

	$debug and print "$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };

		$g{ $leaf }{ $next } = 1;
		$g{ $next }{ $leaf } = 1;

		next if 1 < keys %{ $h{ $next } };
		next if $c[ $next - 1 ];

		push @leaf, $next;
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	$debug and print "[@leaf]";

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		if( $c[ $next - 1 ] == 0 ){
			$c[ $next - 1 ] = 1;
			delete $h{ $leaf };
			delete $h{ $next }{ $leaf };
			}
		}

	$debug and print "$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;
	$debug and print "[@c]";

	@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	if( @leaf != $black ){
		print join ' ', ( 1 ) x @c;
		next;
		}

	my @bfs = grep { 1 != keys %{ $h{ $_ } } } keys %h;

	$debug and print "[@bfs]";

	while( @bfs ){
		my $u = shift @bfs;

		for my $v ( keys %{ $g{ $u } } ){
			if( $c[ $v - 1 ] == 0 ){
				push @bfs, $v;
				}
			}

		$c[ $u - 1 ] = 3;
		}

	$debug and print "[@c]";

	print "@c" =~ y/03/10/r;
	}

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

while(<>){
	$debug and print '-' x 15;

	my @c = split ' ', <>;

	my $black = grep $_ == 1, @c;

	my %h;
	my %g;
	my %cnt;

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

		$h{ $u }{ $v } = 1;
		$h{ $v }{ $u } = 1;

		$cnt{ $u } ++;
		$cnt{ $v } ++;
		}

	my @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == $cnt{ $_ } } keys %cnt;

	$debug and print "[@leaf]";

	$debug and print "$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };

		$g{ $leaf }{ $next } = 1;
		$g{ $next }{ $leaf } = 1;

		next if 1 < keys %{ $h{ $next } };
		next if $c[ $next - 1 ];

		push @leaf, $next;
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	@leaf = grep {
			my @keys = keys %{ $h{ $_ } };
			@keys == 1 and
			3 > keys %{ $h{ $keys[ 0 ] } };
			} keys %h;

	$debug and print "[@leaf]";

	if( @leaf != $black ){
		print join ' ', ( 1 ) x @c;
		next;
		}

	while( @leaf ){
		my $leaf = shift @leaf;

		if( 0 == keys %{ $h{ $leaf } } ){
			$c[ $leaf - 1 ] = 2;
			delete $h{ $leaf };
			next;
			}

		my( $next ) = keys %{ $h{ $leaf } };

		if( $c[ $leaf - 1 ] == 1 ){
			push @leaf, $next;
			}

		$c[ $leaf - 1 ] = 2;

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;
	$debug and print "[@c]";

	my @bfs = keys %h;

	for( @bfs ){
		$c[ $_ - 1 ] = 3;
		}

	$debug and print "[@c]";

	while( @bfs ){
		my $u = shift @bfs;

		for my $v ( keys %{ $g{ $u } } ){
			if( $c[ $v - 1 ] == 0 ){
				push @bfs, $v;
				}
			}

		$c[ $u - 1 ] = 3;
		}

	$debug and print "[@c]";

	print "@c" =~ y/023/110/r;
	}

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

while(<>){
	$debug and print '-' x 15;

	my @c = split ' ', <>;

	my $black = grep $_ == 1, @c;

	my %h;
	my %g;
	my %cnt;

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

		$h{ $u }{ $v } = 1;
		$h{ $v }{ $u } = 1;

		$cnt{ $u } ++;
		$cnt{ $v } ++;
		}

	my @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == $cnt{ $_ } } keys %cnt;

	$debug and print "[@leaf]";

	$debug and print "$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };

		$g{ $leaf }{ $next } = 1;
		$g{ $next }{ $leaf } = 1;

		next if 1 < keys %{ $h{ $next } };
		next if $c[ $next - 1 ];

		push @leaf, $next;
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	$debug and print "[@leaf]";

	if( @leaf != $black ){
		print join ' ', ( 1 ) x @c;
		next;
		}

	while( @leaf ){
		my $leaf = shift @leaf;

		if( 0 == keys %{ $h{ $leaf } } ){
			$c[ $leaf - 1 ] = 2;
			delete $h{ $leaf };
			next;
			}

		my( $next ) = keys %{ $h{ $leaf } };

		if( $c[ $leaf - 1 ] == 1 ){
			push @leaf, $next;
			}

		$c[ $leaf - 1 ] = 2;

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;
	$debug and print "[@c]";

	my @bfs = keys %h;

	for( @bfs ){
		$c[ $_ - 1 ] = 3;
		}

	$debug and print "[@c]";

	while( @bfs ){
		my $u = shift @bfs;

		for my $v ( keys %{ $g{ $u } } ){
			if( $c[ $v - 1 ] == 0 ){
				push @bfs, $v;
				}
			}

		$c[ $u - 1 ] = 3;
		}

	$debug and print "[@c]";

	print "@c" =~ y/023/110/r;
	}

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

MAIN:
while(<>){
	$debug and print '-' x 15;

	my @c = split ' ', <>;

	my @black;

	for my $i ( 1 .. @c ){
		$c[ $i - 1 ] and push @black, $i;
		}

	my %h;

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

		$h{ $u }{ $v } = 1;
		$h{ $v }{ $u } = 1;
		}

	my @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == keys %{ $h{ $_ } } } keys %h;

	$debug and print "[@leaf]";

	$debug and print "$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	my @color = ( 0 ) x @c;

	while( @leaf ){
		my $leaf = shift @leaf;

		$color[ $leaf - 1 ] = 1;

		my( $next ) = grep { 0 == $color[ $_ - 1 ] } keys %{ $h{ $leaf } };

		next if 1 < keys %{ $h{ $next } };
		next if $c[ $next - 1 ];

		push @leaf, $next;
		}

	$debug and print "[@color]";

	for my $black ( @black ){
		if( 1 != grep { !$color[ $_ - 1 ] } keys %{ $h{ $black } } ){
			print join ' ', ( 1 ) x @c;
			next MAIN;
			}
		}

	for my $black ( @black ){
		$color[ $_ - 1 ] = 2 for $black, keys %{ $h{ $black } };
		}

	$debug and print "[@color]";
	$debug and print "[@c]";

	my @bfs;

	for my $i ( 1 .. @color ){
		if( $color[ $i - 1 ] == 0 ){
			push @bfs, $i;
			}
		}

	while( @bfs ){
		my $u = shift @bfs;

		$color[ $u - 1 ] = 0;

		push @bfs, grep { $color[ $_ - 1 ] == 1 } keys %{ $h{ $u } };
		}

	$debug and print "[@color]";

	print "@color" =~ y/2/1/r;
	}

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

while(<>){
	$debug and print '-' x 15;

	my @c = split ' ', <>;

	my $black = grep $_ == 1, @c;

	my %h;
	my %g;
	my %cnt;

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

		$h{ $u }{ $v } = 1;
		$h{ $v }{ $u } = 1;

		$g{ $u }{ $v } = 1;
		$g{ $v }{ $u } = 1;

		$cnt{ $u } ++;
		$cnt{ $v } ++;
		}

	my @leaf = grep { !$c[ $_ - 1 ] } grep { 1 == $cnt{ $_ } } keys %cnt;

	$debug and print "[@leaf]";

	$debug and print "$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };

		next if 1 < keys %{ $h{ $next } };
		next if $c[ $next - 1 ];

		push @leaf, $next;
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;

	@leaf = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	$debug and print "[@leaf]";

	if( @leaf != $black ){
		print join ' ', ( 1 ) x @c;
		next;
		}

	while( @leaf ){
		my $leaf = shift @leaf;

		my( $next ) = keys %{ $h{ $leaf } };

		if( $c[ $leaf - 1 ] == 1 ){
			push @leaf, $next;
			}

		$c[ $leaf - 1 ] = 2;

		delete $h{ $leaf };
		delete $h{ $next }{ $leaf };
		}

	$debug and print ":$_ -> ", join ' ', keys %{ $h{ $_ } } for sort keys %h;
	$debug and print "[@c]";

	my @bfs = keys %h;

	for( @bfs ){
		$c[ $_ - 1 ] = 3;
		}

	$debug and print "[@c]";

	while( @bfs ){
		my $u = shift @bfs;

		for my $v ( keys %{ $g{ $u } } ){
			if( $c[ $v - 1 ] == 0 ){
				push @bfs, $v;
				}
			}

		$c[ $u - 1 ] = 3;
		}

	$debug and print "[@c]";

	print "@c" =~ y/023/110/r;
	}


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.

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

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.

#!/usr/bin/env perl
use 5.012;
chomp(my $A = <STDIN>);
chomp(my $B = <STDIN>);
$A = reverse $A; $B = reverse $B;
my $i = 0;
for (0..(length($A) - 1)) {
    last if $_ >= length $B;
    if ((substr $A, $_, 1) eq (substr $B, $_, 1)) {
        ++$i;
    } else {
        last;
    }
}
say length($A) + length($B) - 2 * $i;


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.

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

Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer.

NoteIllustration for the first example: Illustration for the second example:

#!usr/bin/perl -w
use strict;

my $n=<STDIN>;chomp($n);
my $sum=0;
for my $i(1..$n){
    my($x,$y)=split/ /,<STDIN>;chomp($y);
    $sum=$x+$y if($sum<=$x+$y);
}
print "$sum\n";

print +( sort { $b <=> $a } map { eval join ' + ', split } grep / /, <> )[ 0 ]

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	@_ = map ~~<>, 1 .. $_;

	print +( sort { $b <=> $a } map { eval join ' + ', split } @_ )[ 0 ];
	}


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.

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

#usr/bin/perl -w
use strict;

my ($l,$r)=split/ /,<STDIN>;chomp($r);
print "YES\n";my $n=$l;
while($n<=$r){
    print "$n ";
    $n++;
    print "$n\n";
    $n++;
}

#!/usr/bin/perl

use warnings;
use strict;
#use bigint;

$\ = $/;

while(<>){
	my( $L, $R ) = split;

	print "YES";

	for( my $i = $L; $i < $R; $i += 2 ){
		print join ' ', $i, $i + 1;
		}
	}


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.

The first line contains four integers c1, c2, c3, c4 (1 ≤ c1, c2, c3, c4 ≤ 1000) — the costs of the tickets. The second line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of buses and trolleys Vasya is going to use. The third line contains n integers ai (0 ≤ ai ≤ 1000) — the number of times Vasya is going to use the bus number i. The fourth line contains m integers bi (0 ≤ bi ≤ 1000) — the number of times Vasya is going to use the trolley number i.

Print a single number — the minimum sum of burles Vasya will have to spend on the tickets.

NoteIn the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2·1) + 3 + 7 = 12 burles.In the second sample the profitable strategy is to buy one ticket of the fourth type.In the third sample the profitable strategy is to buy two tickets of the third type: for all buses and for all trolleys.

#!/usr/bin/perl
use strict;
use warnings;

# 2013/11/26
# [解析性の解釈]
#   障害等の問題の把握、切り分けなどの解析のしやすさ
# 気を付けていること：
# - if 文や関数引数内で計算しない (行ブレークかけた時に計算結果がわかりにくい)
# - 各行はシンプルに (上と同じ理由)
# - perlの場合は use strict と use warnings を付ける (表記の曖昧さを排除するため)
# - 1関数あたり50行程度 (印刷したときに1頁に収まる)

# TICKET1 ... 1回乗車券：$c[0] burles
# TICKET2 ... 特定バス/路面電車指定乗車券：$c[1] burles
# TICKET3 ... バスあるいは路面電車限定乗車券：$c[2] burles
# TICKET4 ... 全てのバス/路面電車乗り放題乗車券：$c[3] burles

use constant ERR_OUTOFRANGE=>-1;
use constant NO_ERR=>0;
use constant TICKET1=>0;
use constant TICKET2=>1;
use constant TICKET3=>2;
use constant TICKET4=>3;

my $ret = NO_ERR;
$ret = main();
exit $ret;

# メイン処理
sub main {
    # 変数初期化
    my $min_cost = 0;
    my $min_cost_bus = 0;
    my $min_cost_trolley = 0;
    my $cost_bus = 0;
    my $cost_trolley = 0;

    # 標準入力からデータの読み込み
    my $line1 = <>;
    my $line2 = <>;
    my $line3 = <>;
    my $line4 = <>;

    # 入力データの整形
    my (@c) = split(/ /, $line1);
    my ($n, $m) = split(/ /, $line2);
    my @a = sort {$b <=> $a} split(/ /, $line3); # 数字が大きい順にソート
    my @b = sort {$b <=> $a} split(/ /, $line4); # 数字が大きい順にソート

    # 入力データのエラーチェック
    foreach(@c) {
        if ($_ < 1 || $_ > 1000) { return ERR_OUTOFRANGE; }
    }
    if ($n < 1 || $n > 1000) { return ERR_OUTOFRANGE; }
    if ($m < 1 || $m > 1000) { return ERR_OUTOFRANGE; }

    # 全てTICKET1(一回乗車券)を使った場合
    $cost_bus += $_ * $c[TICKET1] for @a;
    $cost_trolley += $_ * $c[TICKET1] for @b;
    $min_cost_bus = $cost_bus;
    $min_cost_trolley = $cost_trolley;

    # 乗った回数の多い順からTICKET2(特定バス/路面電車指定乗車券)を使った場合
    # - バス
    foreach(@a) {
        if ($_ < 0 || $_ > 1000) { return ERR_OUTOFRANGE; }
        $cost_bus = $cost_bus - $_ * $c[TICKET1] + $c[TICKET2];
        # TICKET2を使っても安くならない場合は次へ
        if ($cost_bus > $min_cost_bus) { last; }
        $min_cost_bus = $cost_bus;
    }
    # - 路面電車
    foreach(@b) {
        if ($_ < 0 || $_ > 1000) { return ERR_OUTOFRANGE; }
        $cost_trolley = $cost_trolley - $_ * $c[TICKET1] + $c[TICKET2];
        # TICKET2を使っても安くならない場合は次へ
        if ($cost_trolley > $min_cost_trolley) { last; }
        $min_cost_trolley = $cost_trolley;
    }

    # TICKET3(バスあるいは路面電車限定乗車券)を使った場合
    if ($min_cost_bus > $c[TICKET3]) { $min_cost_bus = $c[TICKET3]; }
    if ($min_cost_trolley > $c[TICKET3]) { $min_cost_trolley = $c[TICKET3]; }

    # TICKET4(全てのバス/路面電車乗り放題乗車券)を使った場合
    $min_cost = $min_cost_bus + $min_cost_trolley;
    if ($min_cost > $c[TICKET4]) { $min_cost = $c[TICKET4]; }

    chomp($min_cost);
    print "$min_cost\n";

    return NO_ERR;
}

#!/usr/bin/perl

#今回はログを埋め込んだのみ。
#起動時に-debugをオプションでつけるとデバッグログが出力される。

#解析容易性というと難しいけど、
#一般的にはやっぱりデバッグ用のログ埋め込みと
#要所要所でアサーションを挟んでやることですかね。

#デバッガを使うというのであれば
#1行あたりの命令数を少なくしておくと、
#ブレークポイントが設定しやすくなる。

#exceptionが実装されているような言語なら、
#メソッド分割をしっかりしておくとtraceしやすくなるような気がする。

#ちなみに今回はログを自前実装していますが、
#CPANから取ってくればよいログのライブラリもあります。
#assetも標準では使えないのでコメントアウト。

use strict;
use warnings;

my $debug = 0;

if(@ARGV > 0 && $ARGV[0] eq "-debug"){
    $debug = 1;
}

#use Test::Harness::Assert;

#入力処理
my $line = <STDIN>;
chomp $line;
my @costs = split(/\s+/,$line);

$line = <STDIN>;
chomp $line;
my($num_bus, $num_trolley) = split(/\s+/,$line);

$line = <STDIN>;
chomp $line;
my @using_buses = split(/\s+/,$line);

#assert($num_bus == @using_buses);

$line = <STDIN>;
chomp $line;
my @using_trolley = split(/\s+/,$line);

#assert($num_trolley == @using_trolley);

#コスト計算
my $bus_cost = &calc_cost(@using_buses);
&debuglog("sum of buses cost = $bus_cost\n");
my $trolley_cost = &calc_cost(@using_trolley);
&debuglog("sum of trolleys cost = $trolley_cost\n");
my $sum = $bus_cost + $trolley_cost;

#出力処理
if($sum < $costs[3]){
    print $sum,"\n";
}else{
    print $costs[3],"\n";
}

sub calc_cost{
    my @nums = @_;
    my @one_type_costs;
    my $sum = 0;
    foreach my $num (@nums){
        if($num eq ""){
            next;
        }
        my $cost = &calc_onetype_cost($num);
        &debuglog("one type cost = $cost\n");
        $sum += $cost;
    }

    if($sum < $costs[2]){
        return $sum;
    }else{
        return $costs[2];
    }
}

sub calc_onetype_cost{
    my($num) = @_;
    my $cost;
    if($num * $costs[0] > $costs[1]){
        $cost = $costs[1];
    }else{
        $cost = $num * $costs[0];
    }
    return $cost;
}

sub debuglog{
    my $str = $_[0];
    if($debug == 1){
        print STDERR  "$str\n";
    }
}