sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2 != 0 && $K > 1){
            return int($len * $K /2);
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-1) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($i < $len-1 && $ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len > $i+1) {
            $idx++;
        }
    }
    warn(join ":",@ans);
    if($fst eq $end){
        $ans[-1] += $ans[0];
        $ans[0] = 1;
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    return $anser * $K;
}


sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2 != 0 && $K > 1){
            return int($len * $K /2);
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-1) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($i < $len-1 && $ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len != $i+1) {
            $idx++;
        }
    }
    warn(join ":",@ans);
    if($fst eq $end){
        $ans[-1] += $ans[0];
        $ans[0] = 1;
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    return $anser * $K;
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2 != 0 && $K > 1){
            return int($len * $K /2);
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-1) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len != $i+1) {
            $idx++;
        }
    }
    warn(join ":",@ans);
    if($fst eq $end){
        $ans[-1] += $ans[0];
        $ans[0] = 1;
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    return $anser * $K;
}


ub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2 != 0 && $K > 1){
            return int($len * $K /2);
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-1) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len != $i+1) {
            $idx++;
        }
    }
    warn(join ":",@ans);
    if($fst eq $end){
        $ans[-1] += $ans[0];
        $ans[0] = 1;
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    return $anser * $K;
}


sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2 != 0 && $K > 1){
            return int($len * $K /2);
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-2) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len != $i+2) {
            $idx++;
        }
    }
    if($fst eq $end){
        $ans[-1] += $ans[0];
        $ans[0] = 1;
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    return $anser * $K;
}


sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2 != 0 && $K > 1){
            $len++;
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-2) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len != $i+2) {
            $idx++;
        }
    }
    if($fst eq $end){
        $ans[-1] += $ans[0];
        $ans[0] = 1;
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    return $anser * $K;
}


ub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2!=0 && $K > 1){
            $len++;
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-2) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len != $i+2) {
            $idx++;
        }
    }
    if($fst eq $end){
        $ans[-1] += $ans[0];
        $ans[0] = 1;
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    return $anser * $K;
}


my @A;
my %h;
foreach my $w (split(//o, <STDIN>)){
    chomp($w);
    next if ($w eq '');
    push(@A, $w);
    $h{$w} = 1;
}
my $N = <STDIN>;
chomp($N);

my $numA = $#A;
my $numK = (keys %h);
if ($numK == 1 and $numA > 0) {
  my $numA2 = $numA+1;
  if ( ($numA2*$N) % 2 ){
    # odd
    my $tmp = $numA2*$N - 1;
    print $tmp/2;
  } else {
    # even
    my $tmp = $numA2*$N;
    print $tmp/2;
  }
  exit 0;
}
my $i = 0;
my $s = '';
my $b = '';
my $num = 0;
my $lastflag = 0;
foreach my $w (@A){
    if ( $i == 0 ) {
      $s = $w;
      $b = $w;
    } else {
      if ( $b eq $w ) {
        $num++;
        $b = '';
      } elsif ( $i == $numA  and $s eq $w ) {
        $num++;
        $lastflag = 1;
      } else {
        $b = $w;
      }
    }
    $i++
}
if ( $i == 1 ) {
  print $N-1;
} else {
  my $v = $num*$N;
  $v-- if ($lastflag);
  print $v;
}


sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-2) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len != $i+2) {
            $idx++;
        }
    }
    if($fst eq $end){
        $ans[-1] += $ans[0];
        $ans[0] = 1;
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    return $anser * $K;
}


sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($S =~ /^(.)\1*$/){
        $anser = int($len/2);
        return $anser * $K;
    }
    if($len == 1){
        return int($K/2);
    }
    for my $i(0 .. $len-2) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len != $i+2) {
            $idx++;
        }
    }
    if($fst eq $end){
        $ans[-1] += $ans[0];
        $ans[0] = 1;
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    return $anser * $K;
}


my @A;
foreach my $w (split(//o, <STDIN>)){
    chomp($w);
    next if ($w eq '');
    push(@A, $w);
}
my $numA = $#A;
my $i = 0;
my $s = '';
my $b = '';
my $num = 0;
my $lastflag = 0;
foreach my $w (@A){
    if ( $i == 0 ) {
      $s = $w;
      $b = $w;
    } else {
      if ( $b eq $w ) {
        $num++;
        $b = '';
      } elsif ( $i == $numA  and $s eq $w ) {
        $num++;
        $lastflag = 1;
      } else {
        $b = $w;
      }
    }
    $i++
}
my $N = <STDIN>;
chomp($N);

if ( $i == 1 ) {
  print $N-1;
} else {
  my $v = $num*$N;
  $v-- if ($lastflag);
  print $v;
}


use strict;
use warnings;

sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    for my $i(0 .. $len-2) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len != $i+2) {
            $idx++;
        }
    }
    if($fst eq $end){
        $ans[-1] += $ans[0];
        $ans[0] = 1;
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    return $anser * $K;
}


Score : 400 points

Problem Statement

Takahashi is going to buy N items one by one.

The price of the i-th item he buys is A_i yen (the currency of Japan).

He has M discount tickets, and he can use any number of them when buying an item.

If Y tickets are used when buying an item priced X yen, he can get the item for \frac{X}{2^Y} (rounded down to the nearest integer) yen.

What is the minimum amount of money required to buy all the items?

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the minimum amount of money required to buy all the items.

Sample Input 1

3 3
2 13 8

Sample Output 1

9

We can buy all the items for 9 yen, as follows:

Buy the 1-st item for 2 yen without tickets.

Buy the 2-nd item for 3 yen with 2 tickets.

Buy the 3-rd item for 4 yen with 1 ticket.

Sample Input 2

4 4
1 9 3 5

Sample Output 2

6

Sample Input 3

1 100000
1000000000

Sample Output 3

0

We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.

Sample Input 4

10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 4

9500000000


use List::Util qw(max first);

my ($N, $M) = (split(/ /o, <STDIN>));
chomp($N);
chomp($M);
my @A;
my $tot = 0;
foreach (split(/ /o, <STDIN>)) {
  chomp($_);
  push(@A, $_);
  $tot += $_;
}
foreach my $i (1..$M) {
  my $t = max(@A);
  my $idx = first { $A[$_] == $t } 0..$#A;
  my $t2 = int($t/2);
  $A[$idx] = $t2;
  $tot = $tot - $t + $t2;
}
print $tot;


use List::Util qw(max first);

my ($N, $M) = (split(/ /o, <STDIN>));
chomp($N);
chomp($M);
my @A;
my $tot = 0;
foreach (split(/ /o, <STDIN>)) {
  chomp($_);
  push(@A, $_);
  $tot += $_;
}
foreach my $i (1..$M) {
  my $t = max(@A)
  my $idx = first { $A[$_] == $t } 0..$#A;
  my $t2 = int($t/2);
  $A[$idx] = $t2;
  $tot = $tot - $t + $t2;
}
print $tot;


my $s = <STDIN>;
my @dat = split(/ /,$s);
my $N = $dat[0];
my $M = $dat[1];
#print "$N $M\n";
$s = <STDIN>;
@dat = split(/ /,$s);
@dat = sort {$b <=> $a} @dat;

#$M = $M -1;
 for($i=0;$i<$M;$i++){
   @dat = sort {$b <=> $a} @dat;
   $cut = shift @dat;
   $cut = int ($cut /2);
   push @dat , $cut;
   while(){
     if($dat[0] > $cut){
       if( ($i +1) >= $M){last;}
       $cut = shift @dat;
       $cut = int ($cut /2);
       push @dat , $cut;
       $i++;
     }else{
       last;
     }
   }
   #print "@dat\n";
 }

 my $sum =0;
 for($i=0;$i<=$#dat;$i++){
   #print "$dat[$i] ";
   $sum = $sum + $dat[$i];
 }
print "$sum";


Score : 500 points

Problem Statement

Given is a string S of length N.

Find the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping.

More formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \leq l_1, l_2 \leq N - len + 1 ) that satisfy the following:

l_1 + len \leq l_2

S[l_1+i] = S[l_2+i] (i = 0, 1, ..., len - 1)

If there is no such integer len, print 0.

Constraints

2 \leq N \leq 5 \times 10^3

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. If there is no such non-empty string, print 0 instead.

Sample Input 1

5
ababa

Sample Output 1

2

The strings satisfying the conditions are: a, b, ab, and ba. The maximum length among them is 2, which is the answer.
Note that aba occurs twice in S as contiguous substrings, but there is no pair of integers l_1 and l_2 mentioned in the statement such that l_1 + len \leq l_2.

Sample Input 2

2
xy

Sample Output 2

0

No non-empty string satisfies the conditions.

Sample Input 3

13
strangeorange

Sample Output 3

5


#!/usr/bin/perl
my ($n) = map { $_ - 0 } split(/\s+/,<STDIN>);
my $s = <STDIN>;

my @tails = ();
for(my $i=0;$i<$n;$i++){
  $tails[$i] = substr($s,$i,length($s)-$i);
}
push(@tails,"");

my @s = sort { $a cmp $b } @tails;
my $res = 0;

for(my $i=0;$i<=$#s-1;$i++){
  my $l = length($s[$i]);
  $l = length($s[$i+1]) if length($s[$i+1])<$l;
  my $cnt = 0;
  my $j = 0;
  my $maxcnt = abs(length($s[$i]) - length($s[$i+1]));
  while( $j<$l and substr($s[$i],$j,1) eq substr($s[$i+1],$j,1) and $cnt < $maxcnt ){
    $cnt++;
    $j++;
  }
  $res = $cnt if $cnt > $res;
}

print "$res\n";


#!/usr/bin/perl
my ($n) = map { $_ - 0 } split(/\s+/,<STDIN>);
my $s = <STDIN>;

my @tails = ('');
for(my $i=0;$i<$n;$i++){
  $tails[$i] = substr($s,$i,length($s)-$i);
}

my @s = sort { $a cmp $b } @tails;
my $res = 0;

for(my $i=0;$i<=$#s-1;$i++){
  my $l = length($s[$i]);
  $l = length($s[$i+1]) if length($s[$i+1])<$l;
  my $cnt = 0;
  my $j = 0;
  my $maxcnt = abs(length($s[$i]) - length($s[$i+1]));
  while( $j<$l and substr($s[$i],$j,1) eq substr($s[$i+1],$j,1) and $cnt < $maxcnt ){
    $cnt++;
    $j++;
  }
  $res = $cnt if $cnt > $res;
}

print "$res\n";


#!/usr/bin/perl
my ($n) = map { $_ - 0 } split(/\s+/,<STDIN>);
my $s = <STDIN>;

my @tails = ();
for(my $i=0;$i<$n;$i++){
  $tails[$i] = substr($s,$i,length($s)-$i);
}

my @s = sort { $a cmp $b } @tails;
my $res = 0;

for(my $i=0;$i<=$#s-1;$i++){
  my $l = length($s[$i]);
  $l = length($s[$i+1]) if length($s[$i+1])<$l;
  my $cnt = 0;
  my $j = 0;
  my $maxcnt = abs(length($s[$i]) - length($s[$i+1]));
  while( $j<$l and substr($s[$i],$j,1) eq substr($s[$i+1],$j,1) and $cnt < $maxcnt ){
    $cnt++;
    $j++;
  }
  $res = $cnt if $cnt > $res;
}

print "$res\n";


#!/usr/bin/perl
my ($n) = map { $_ - 0 } split(/\s+/,<STDIN>);
my $s = <STDIN>;

my @tails = ();

for(my $i=0;$i<$n;$i++){
  $tails[$i] = substr($s,$i,length($s)-$i);
}

my @s = sort { $a cmp $b } @tails;

my @ss = ();
$#ss = $#s;

for(my $i=0;$i<=$#s-1;$i++){
  my $l = length($s[$i]);
  $l = length($s[$i+1]) if length($s[$i+1])<$l;
  my $cnt = 0;
  my $j = 0;
  my $maxcnt = abs(length($s[$i]) - length($s[$i+1]));
  while( $j<$l and substr($s[$i],$j,1) eq substr($s[$i+1],$j,1) and $cnt < $maxcnt ){
    $cnt++;
    $j++;
  }
  $ss[$j] = $cnt;
}

my $res = 0;
foreach(@ss){
  $res = $_ if $_ > $res;
}
print "$res\n";


#!/usr/bin/perl
my ($n) = map { $_ - 0 } split(/\s+/,<STDIN>);
my $s = <STDIN>;

my @tails = ();

for(my $i=0;$i<$n;$i++){
  $tails[$i] = substr($s,$i,length($s)-$i);
}

my @s = sort { $a cmp $b } @tails;

my @ss = ();
$#ss = $#s;

for(my $i=0;$i<=$#s-1;$i++){
  my $l = length($s[$i]);
  $l = length($s[$i+1]) if length($s[$i+1])<$l;
  my $cnt = 0;
  my $j = 0;
  while( $j<$l and substr($s[$i],$j,1) eq substr($s[$i+1],$j,1) ){
    $cnt++;
    $j++;
  }
  my $st1 = $n - length($s[$i]);
  my $st2 = $n - length($s[$i+1]);
  if( $cnt > abs($st1 - $st2) ){
    $cnt = abs($st1 - $st2);
  }
  $ss[$j] = $cnt;
}

my @sss = sort { $b <=> $a } @ss;
print "$sss[0]\n";


<>;
chomp($_=<>);
$orglen = length;
s/^.*?(.+).*?\1.*?$/\1/;
if (length == $orglen) {
    print 0;
} else {
    print length;
}


Score : 100 points

Problem Statement

You will be given an integer a and a string s consisting of lowercase English letters as input.

Write a program that prints s if a is not less than 3200 and prints red if a is less than 3200.

Constraints

2800 \leq a < 5000

s is a string of length between 1 and 10 (inclusive).

Each character of s is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

a
s

Output

If a is not less than 3200, print s; if a is less than 3200, print red.

Sample Input 1

3200
pink

Sample Output 1

pink

a = 3200 is not less than 3200, so we print s = pink.

Sample Input 2

3199
pink

Sample Output 2

red

a = 3199 is less than 3200, so we print red.

Sample Input 3

4049
red

Sample Output 3

red

a = 4049 is not less than 3200, so we print s = red.


$a = <STDIN>;
$s = <STDIN>;

chomp($a,$s);

if($a >= 3200){
  print "$s\n";
}
else{
  print "red\n";
}


$a = <STDIN>;
$str = <STDIN>;

if ($a < 3200) {
    print "red";
} else {
    print $str;
}


my $a =  <STDIN>;
chomp ($a);
$a = $a + 0;
my $s =  <STDIN>;
chomp ($s);
if ( $a >= 3200 ) {
  print $s;
} else {
  print 'red';
}


use v5.18; # strict say state
use warnings;
use List::Util qw(reduce first max min sum0);

chomp (my ($n, $q) = split / /, <STDIN>);
my (@a, @b);
for my $i (1..$n-1) {
  chomp (($a[$i], $b[$i]) = split / /, <STDIN>);
}
my (@p, @x);
for my $i (1..$q) {
  chomp (($p[$i], $x[$i]) = split / /, <STDIN>);
}
my @sum;
for my $i (1..$q) {
  $sum[$p[$i]] += $x[$i];
}
my (@s, @que);
push @que, 1;
while (@que) {
  my $i = pop @que;
  for my $j (1..$n) {
    if ($a[$j] == $i) {
      $sum[$b[$j]] += $sum[$i];
      push @que, $b[$j];
    }
  }
}
say join(" ", @sum[1..$n]);

Score : 400 points

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.
The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

Now, the following Q operations will be performed:

Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.

Find the value of the counter on each vertex after all operations.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq Q \leq 2 \times 10^5

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
:
a_{N-1} b_{N-1}
p_1 x_1
:
p_Q x_Q

Output

Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.

Sample Input 1

4 3
1 2
2 3
2 4
2 10
1 100
3 1

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.

Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.

Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.

Sample Input 2

6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10

Sample Output 2

20 20 20 20 20 20


use v5.18; # strict say state
use warnings;
use List::Util qw(reduce first max min sum0);

chomp (my ($n, $q) = split / /, <STDIN>);
my (@a, @b);
for my $i (1..$n-1) {
  chomp (($a[$i], $b[$i]) = split / /, <STDIN>);
}
my (@p, @x);
for my $i (1..$q) {
  chomp (($p[$i], $x[$i]) = split / /, <STDIN>);
}
my @sum;
for my $i (1..$q) {
  $sum[$p[$i]] += $x[$i];
}
my (@s, @que);
push @que, 1;
while (@que) {
  my $i = pop @que;
  for my $j (1..$n) {
    if ($a[$j] == $i) {
      $sum[$b[$j]] += $sum[$i];
      push @que, $b[$j];
    }
  }
}
print "$sum[$_] " for (1..$n);


Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right. The height of the i-th square from the left is H_i.

For each square, you will perform either of the following operations once:

Decrease the height of the square by 1.

Do nothing.

Determine if it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

If it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right, print Yes; otherwise, print No.

Sample Input 1

5
1 2 1 1 3

Sample Output 1

Yes

You can achieve the objective by decreasing the height of only the second square from the left by 1.

Sample Input 2

4
1 3 2 1

Sample Output 2

No

Sample Input 3

5
1 2 3 4 5

Sample Output 3

Yes

Sample Input 4

1
1000000000

Sample Output 4

Yes


my $n = get;
my $hs = get.words;
my $a = $hs.reduce(-> $cur, $x { if $cur == -1 { -1 } elsif $cur > $x { -1 } elsif $cur <= $x - 1 { $x - 1 } else { $x } }, with => 0);
(if $a != -1 { 'Yes' } else { 'No' }) . say;


use strict;
use warnings;

my ($max, $prev) = (1, 0);
my @num = ();
my $r = "Yes";

while(<>){
	chomp;
	@num = split(/ /) if($. == 2);
}

foreach my $now (@num){
	$max = $now if($now > $max);
	if($max - $now > 1){
		$r = "No";
		last;
	}
	if($now >= $prev){
		$prev = $now;
	}
	elsif($now - 1 >= $prev){
		$prev = $now - 1;
	}
	elsif($prev - 1 >= $now){
		$prev--;
	}
	else{
		$r = "No";
		last;
	}
}

print $r;


use strict;
use warnings;
use feature 'say';
use Data::Dumper;

my $N = <>; chomp($N);
my $H = <>; chomp($H);
$H = [split(" ", $H)];
$H = [map {+{number => $_, edited => 0}} @$H];

while (1) {
    my $edited = 0;
    for (0..$N-2) {
        if ($H->[$_]{number} > $H->[$_+1]{number}) {
            unless ($H->[$_]{edited}) {
                $H->[$_]{number}--;
                $H->[$_]{edited} = 1;
                $edited = 1;
            }

        }
    }
    last unless $edited;
}

for (0..$N-2) {
    if ($H->[$_]{number} > $H->[$_+1]{number}) {say 'No';exit}
}

say 'Yes';


my $N = <STDIN>;
my $s = <STDIN>;
@s = split(/ /,$s);

my $down_flag = 0;

for(my $i = 1; $i < $N;$i++ ){
  if( $s[$N-$i-1] <$s[$N-$i] ){
    if( $down_flag == 0 ) {
     next;
    }else{
       $down_flag = 0;
    }
  }elsif($s[$N-$i-1] == $s[$N-$i] ){
    next;
  }else{
   if( ($s[$N-$i-1] - 1 )== $s[$N-$i] ){
     if(  $down_flag == 0 ){
       $down_flag = 1;
     }else{
        print "No\n";
        exit;
     }
   }else{
     print "No\n";
     exit;
   }
  }
}
print "Yes\n";


my $N = <STDIN>;
my $s = <STDIN>;
@s = split(/ /,$s);

my $down_flag = 0;

for(my $i = 1; $i < $N;$i++ ){
print"$i $s[$N-$i-1],$s[$N-$i]\n";
  if( $s[$N-$i-1] <$s[$N-$i] ){
    if( $down_flag == 0 ) {
     next;
    }else{
       $down_flag = 0;
    }
  }elsif($s[$N-$i-1] == $s[$N-$i] ){
    next;
  }else{
   if( ($s[$N-$i-1] - 1 )== $s[$N-$i] ){
     if(  $down_flag == 0 ){
       $down_flag = 1;
     }else{
        print "No\n";
        exit;
     }
   }else{
     print "No\n";
     exit;
   }
  }
}
print "Yes\n";


my $N = <STDIN>;
my $s = <STDIN>;
@s = split(/ /,$s);

my $down_flag = 0;

for(my $i = 1; $i < $N;$i++ ){
  if( $s[$N-$i-1] <$s[$N-$i] ){
    if( $down_flag == 0 ) {
     next;
    }else{
       $down_flag = 0;
    }
  }elsif($s[$i-1] == $s[$i] ){
    next;
  }else{
   if( ($s[$i-1] - 1 )== $s[$i] ){
     if(  $down_flag == 0 ){
       $down_flag = 1;
     }else{
        print "No\n";
        exit;
     }
   }else{
     print "No\n";
     exit;
   }
  }
}
print "Yes\n";


Score : 300 points

Problem Statement

You are given two non-negative integers L and R.
We will choose two integers i and j such that L \leq i < j \leq R.
Find the minimum possible value of (i \times j) \mbox{ mod } 2019.

Constraints

All values in input are integers.

0 \leq L < R \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

L R

Output

Print the minimum possible value of (i \times j) \mbox{ mod } 2019 when i and j are chosen under the given condition.

Sample Input 1

2020 2040

Sample Output 1

2

When (i, j) = (2020, 2021), (i \times j) \mbox{ mod } 2019 = 2.

Sample Input 2

4 5

Sample Output 2

20

We have only one choice: (i, j) = (4, 5).


use v5.18; # strict say state
use warnings;
use List::Util qw(reduce first max min sum0);

chomp (my $s = <STDIN>);
my ($p, $ans) = (0, 0);
while (1) {
  my $p = index($s, "ABC");
  last if $p == -1;
  $ans++;
  substr($s, $p, 3) = "BCA";
}
say $ans;

Score : 600 points

Problem Statement

You are given a string s consisting of A, B and C.

Snuke wants to perform the following operation on s as many times as possible:

Choose a contiguous substring of s that reads ABC and replace it with BCA.

Find the maximum possible number of operations.

Constraints

1 \leq |s| \leq 200000

Each character of s is A, B and C.

Input

Input is given from Standard Input in the following format:

s

Output

Find the maximum possible number of operations.

Sample Input 1

ABCABC

Sample Output 1

3

You can perform the operations three times as follows: ABCABC → BCAABC → BCABCA → BCBCAA. This is the maximum result.

Sample Input 2

C

Sample Output 2

0

Sample Input 3

ABCACCBABCBCAABCB

Sample Output 3

6


use strict;
use warnings;

chomp(my $s = <>);

my $cnt = 0;

$s =~ s/BC/D/g;

my @list = split /[BC]/, $s;
foreach my $item (@list) {

	next if $item eq "";
	#print $item, "\n";

	my $str = $item;
	while ($str =~ m/AD/) {
		$str =~ s/(.*?)A/A/;
		$str =~ s/(A+)(D+)/$1/;
		my $add = (length $1) * (length $2);
		$cnt += $add;
	}
}

print $cnt, "\n";


use strict;
use warnings;

chomp(my $s = <>);

my $cnt = 0;

while ($s =~ m/ABC/) {
	$s =~ s/(.*)A((?:BC)+)/$1$2A/;
	my $add = (length $2) / 2;
	$cnt += $add;
}

print $cnt, "\n";


use strict;
use warnings;

chomp(my $s = <>);

my $cnt = 0;

while ($s =~ m/ABC/) {
	$s =~ s/(.*)ABC/$1BCA/;
	$cnt++;
}

print $cnt, "\n";


Score : 300 points

Problem Statement

You are given a grid of squares with H horizontal rows and W vertical columns, where each square is painted white or black.
HW characters from A_{11} to A_{HW} represent the colors of the squares.
A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

We will repeatedly perform the following operation until all the squares are black:

Every white square that shares a side with a black square, becomes black.

Find the number of operations that will be performed.
The initial grid has at least one black square.

Constraints

1 \leq H,W \leq 1000

A_{ij} is # or ..

The given grid has at least one black square.

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the number of operations that will be performed.

Sample Input 1

3 3
...
.#.
...

Sample Output 1

2

After one operation, all but the corners of the grid will be black. After one more operation, all the squares will be black.

Sample Input 2

6 6
..#..#
......
#..#..
......
.#....
....#.

Sample Output 2

3


use v5.18; # strict say state
use warnings;
use List::Util qw(reduce first max min sum0);

chomp (my ($h, $w) = split / /, <STDIN>);
my ($x, $y);
for my $i (0..$h-1) {
  chomp (my $in = <STDIN>);
  $x .= $in;
  $y .= "1"x$w;
}
$x =~ s/\./0/g;
$x =~ s/#/1/g;
$x = oct('b0'.$x);
$y = oct('b0'.$y);

my $ans = 0;
while ($x != $y) {
  $x = bitcalc($x, $y, $w+2);
  $ans++;
}
say $ans;
sub bitcalc {
  my ($s, $v, $w) = @_;
  my $t1 = $s >> 1;
  my $t2 = $s << 1;
  my $t3 = $s >> $w;
  my $t4 = $s << $w;
  my $u = $s | $t1 | $t2 | $t3 | $t4;
  $u &= $v;
  return $u;
}


use v5.18; # strict say state
use warnings;
use List::Util qw(reduce first max min sum0);

chomp (my ($h, $w) = split / /, <STDIN>);
my (@s, @que);
for my $i (1..$h) {
  chomp (my $in = <STDIN>);
  my @b = split //, $in;
  for my $j (1..$w) {
    if ($b[$j-1] eq '.') {
      $s[$i][$j] = -1;
    } else {
      $s[$i][$j] = 0;
      push @que, $i;
      push @que, $j;
    }
  }
}
my $ans = 0;
while (@que) {
  my ($i, $j);
  $i = shift @que;
  $j = shift @que;
  $ans = $s[$i][$j];
  if ($s[$i+1][$j] == -1) {
    $s[$i+1][$j] = $ans + 1;
    push @que, $i+1;
    push @que, $j;
  }
  if ($s[$i-1][$j] == -1) {
    $s[$i-1][$j] = $ans + 1;
    push @que, $i-1;
    push @que, $j;
  }
  if ($s[$i][$j+1] == -1) {
    $s[$i][$j+1] = $ans + 1;
    push @que, $i;
    push @que, $j+1;
  }
  if ($s[$i][$j-1] == -1) {
    $s[$i][$j-1] = $ans + 1;
    push @que, $i;
    push @que, $j-1;
  }
}
say $ans;


use v5.18; # strict say state
use warnings;
use List::Util qw(reduce first max min sum0);

chomp (my ($h, $w) = split / /, <STDIN>);
my (@s, @que);
my $c;
for my $i (1..$h) {
  chomp (my $in = <STDIN>);
  my @b = split //, $in;
  for my $j (1..$w) {
    if ($b[$j-1] eq '.') {
      $s[$i][$j] = -1;
      $c++;
    } else {
      push @que, $i;
      push @que, $j;
    }
  }
}
push @que, 0;
push @que, 0;
my $ans = 0;
while ($c) {
  my ($i, $j);
  $i = shift @que;
  $j = shift @que;
  if ($i==0) {
    push @que, 0;
    push @que, 0;
    $ans++;
    next;
  }
  if ($s[$i+1][$j] == -1) {
    $s[$i+1][$j] = 1;
    $c--;
    push @que, $i+1;
    push @que, $j;
  }
  if ($s[$i-1][$j] == -1) {
    $s[$i-1][$j] = 1;
    $c--;
    push @que, $i-1;
    push @que, $j;
  }
  if ($s[$i][$j+1] == -1) {
    $s[$i][$j+1] = 1;
    $c--;
    push @que, $i;
    push @que, $j+1;
  }
  if ($s[$i][$j-1] == -1) {
    $s[$i][$j-1] = 1;
    $c--;
    push @que, $i;
    push @que, $j-1;
  }
}
say ++$ans;


use 5.18.2;

my $H;
my $W;
my $loop=1;
my $count=0;
my @given;
my @pattern;

($H,$W)=split(/ /,<>);
push(@given,split(//,<>)) for(1..$H);
@given=grep $_ ne "\n", @given;

while($loop){
	$loop=0;
	@pattern=@given;

	for(0..$W*$H-1){
		if($given[$_]eq"#"){
			$pattern[$_+1]="#" if $_%$W!=$W-1;
			$pattern[$_-1]="#" if $_%$W!=0;
			$pattern[$_+$W]="#" if $_/$W!=0;
			$pattern[$_-$W]="#" if $_/$W!=$H-1;
		}

		for my $i(0..$W*$H-1){
			if ($pattern[$i] ne "#"){
				$loop=1;
				last;
			}
		}
	}

	@given=@pattern;
	$count++ if $loop;
}

say $count;


use 5.18.2;

my $H,$W=<>;
my @given;

for (1..$H){
	push(@given,<>);
}

say $H,$W;
say @given;


my $H,$W=<>;
my @given;

for (1..$H){
	push(@given,<>);
}

say $H,$W;
say @given;


Score : 300 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on the blackboard.

You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.

Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Output

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Sample Input 1

3
7 6 8

Sample Output 1

2

If we replace 7 with 4, the greatest common divisor of the three integers on the blackboard will be 2, which is the maximum possible value.

Sample Input 2

3
12 15 18

Sample Output 2

6

Sample Input 3

2
1000000000 1000000000

Sample Output 3

1000000000

We can replace an integer with itself.


use v5.18;
use warnings;
use List::Util qw( reduce max );

<>;
my @nums = glob <>;
say max map { gcb(right($_ - 1), left($_ + 1)) } 0 .. $#nums;

sub right {
  my $n = shift;
  return 0 if $n < 0;
  state @dp;
  return $dp[$n] if defined $dp[$n];
  if ($n > 0) {
    $dp[$n] = gcb($nums[$n], right($n - 1));
  } else {
    $nums[$n];
  }
}

sub left {
  my $n = shift;
  return 0 if $n > $#nums;
  state @dp;
  return $dp[$n] if defined $dp[$n];
  if ($n < $#nums) {
    $dp[$n] = gcb($nums[$n], left($n + 1));
  } else {
    $nums[$n];
  }
}

sub gcb {
  my ($x, $y) = @_;
  return $x if $y == 0;
  my $mod = $x % $y;
  $mod == 0 ? $y : gcb($y, $mod);
}


use v5.18;
use warnings;
use List::Util qw( reduce max );

<>;
my @nums = glob <>;

say max map {
  my $r = reduce { gcb($a, $b) } @nums[0 .. $_ - 1];
  my $l = reduce { gcb($a, $b) } @nums[$_ + 1 .. $#nums];
  my @defined = grep { defined $_ } ($r, $l);
  if (@defined == 2) {
    gcb($r, $l);
  } elsif (@defined == 1) {
    $defined[0];
  } else {
    ();
  }
} 0 .. $#nums;

sub gcb {
  my ($x, $y) = @_;
  ($x, $y) = ($y, $x) if ($x > $y);
  until ($x == 0){
      ($x, $y) = ($y % $x, $x)
  }
  $y;
}


use v5.18;
use warnings;
use List::Util qw( reduce max );

<>;
my @nums = glob <>;

say max map {
  my $r = right($_ - 1);
  my $l = reduce { gcd($a, $b) } @nums[$_ + 1 .. $#nums];
  my @defined = grep { defined $_ } ($r, $l);
  if (@defined == 2) {
    gcd($r, $l);
  } elsif (@defined == 1) {
    $defined[0];
  } else {
    ();
  }
} 0 .. $#nums;

sub right {
  my $j = shift;
  return !!0 if $j == @nums;
  state %r;
  return $r{$j} if exists $r{$j};
  my $ret = gcd(right($j + 1), $nums[$j]);
  $r{$j} = $ret;
  return $ret;
}

sub gcd {
  my ($x, $y) = @_;
  return $x if $y == 0;
  my $mod = $x % $y;
  $mod == 0 ? $y : gcd($y, $mod);
}


use v5.18;
use warnings;
use List::Util qw( reduce max );

<>;
my @nums = glob <>;

say max map {
  my $r = reduce { gcb($a, $b) } @nums[0 .. $_ - 1];
  my $l = reduce { gcb($a, $b) } @nums[$_ + 1 .. $#nums];
  my @defined = grep { defined $_ } ($r, $l);
  if (@defined == 2) {
    gcb($r, $l);
  } elsif (@defined == 1) {
    $defined[0];
  } else {
    ();
  }
} 0 .. $#nums;

sub gcb {
  my ($x, $y) = @_;
  return $x if $y == 0;
  state $dp = +{};
  return $dp->{$x}{$y} if defined $dp->{$x}{$y};
  my $mod = $x % $y;
  $dp->{$x}{$y} = $mod == 0 ? $y : gcb($y, $mod);
}


use v5.18;
use warnings;
use List::Util qw( reduce max );

<>;
my @nums = glob <>;

say max map {
  my $r = reduce { gcb($a, $b) } @nums[0 .. $_ - 1];
  my $l = reduce { gcb($a, $b) } @nums[$_ + 1 .. $#nums];
  my @defined = grep { defined $_ } ($r, $l);
  if (@defined == 2) {
    gcb($r, $l);
  } elsif (@defined == 1) {
    $defined[0];
  } else {
    ();
  }
} 0 .. $#nums;

sub gcb {
  my ($x, $y) = @_;
  return $x if $y == 0;
  my $mod = $x % $y;
  $mod == 0 ? $y : gcb($y, $mod);
}


use v5.18;
use warnings;
use List::Util qw( reduce max );

<>;
my @nums = glob <>;

my $max = 0;
for (0 .. $#nums) {
  my $r = reduce { gcb($a, $b) } @nums[0 .. $_ - 1];
  my $l = reduce { gcb($a, $b) } @nums[$_ + 1 .. $#nums];
  my @defined = grep { defined $_ } ($r, $l);
  if (@defined == 2) {
    my $gcb = gcb($r, $l);
    $max = $max < $gcb ? $gcb : $max;
  } elsif (@defined == 1) {
    my $gcb = $defined[0];
    $max = $max < $gcb ? $gcb : $max;
  } else {
    ();
  }
}
say $max;

sub gcb {
  my ($x, $y) = @_;
  return $x if $y == 0;
  state $dp = +{};
  return $dp->{$x}{$y} if defined $dp->{$x}{$y};
  my $mod = $x % $y;
  $dp->{$x}{$y} = $mod;
  $mod == 0 ? $y : gcb($y, $mod);
}


use v5.18;
use warnings;
use List::Util qw( reduce max );

<>;
my @nums = glob <>;

say max map {
  my $r = reduce_gcb(\@nums, 0, $_ - 1);
  my $l = reduce_gcb(\@nums, $_ + 1, $#nums);
  my @defined = grep { defined $_ } ($r, $l);
  if (@defined == 2) {
    gcb($r, $l);
  } elsif (@defined == 1) {
    $defined[0];
  } else {
    ();
  }
} 0 .. $#nums;

sub reduce_gcb {
  my ($nums, $begin, $end) = @_;
  if ($begin > $end) {
    undef;
  }
  elsif ($begin == $end) {
    $nums->[$begin];
  } else {
    my $gcb = gcb($nums->[$begin], $nums->[$begin + 1]);
    for (my $i = $begin + 2; $i <= $end; $i++) {
      $gcb = gcb($gcb, $nums->[$i]);
    }
    $gcb;
  }
}

sub gcb {
  my ($x, $y) = @_;
  return $x if $y == 0;
  state $dp = +{};
  return $dp->{$x}{$y} if defined $dp->{$x}{$y};
  my $mod = $x % $y;
  $dp->{$x}{$y} = $mod;
  $mod == 0 ? $y : gcb($y, $mod);
}


use v5.18;
use warnings;
use List::Util qw( reduce max );

<>;
my @nums = glob <>;

say max map {
  my $r = reduce { gcb($a, $b) } @nums[0 .. $_ - 1];
  my $l = reduce { gcb($a, $b) } @nums[$_ + 1 .. $#nums];
  my @defined = grep { defined $_ } ($r, $l);
  if (@defined == 2) {
    gcb($r, $l);
  } elsif (@defined == 1) {
    $defined[0];
  } else {
    ();
  }
} 0 .. $#nums;

sub gcb {
  my ($x, $y) = @_;
  return $x if $y == 0;
  state $dp = +{};
  return $dp->{$x}{$y} if defined $dp->{$x}{$y};
  my $mod = $x % $y;
  $dp->{$x}{$y} = $mod;
  $mod == 0 ? $y : gcb($y, $mod);
}

use v5.18;
use warnings;
use List::Util qw( reduce max );

<>;
my @nums = glob <>;

say max map {
  my $r = reduce { gcb($a, $b) } @nums[0 .. $_ - 1];
  my $l = reduce { gcb($a, $b) } @nums[$_ + 1 .. $#nums];
  my @defined = grep { defined $_ } ($r, $l);
  if (@defined == 2) {
    gcb($r, $l);
  } elsif (@defined == 1) {
    $defined[0];
  } else {
    ();
  }
} 0 .. $#nums;

sub gcb {
  my ($x, $y) = @_;
  return $x if $y == 0;
  state $dp = +{};
  return $dp->{$x}{$y} if defined $dp->{$x}{$y};
  my $mod = $x % $y;
  $dp->{$x}{$y} = $mod;
  $mod == 0 ? $y : gcb($y, $mod);
}


use v5.18;
use warnings;
use List::Util qw( reduce max );

<>;
my @nums = glob <>;

say max map {
  my $r = reduce { gcb($a, $b) } @nums[0 .. $_ - 1];
  my $l = reduce { gcb($a, $b) } @nums[$_ + 1 .. $#nums];
  my @defined = grep { defined $_ } ($r, $l);
  if (@defined == 2) {
    gcb( sort { $a <=> $b } $r, $l );
  } elsif (@defined == 1) {
    $defined[0];
  } else {
    ();
  }
} 0 .. $#nums;

sub gcb {
  my ($x, $y) = @_;
  state $dp = +{};
  return $dp->{$x}{$y} if defined $dp->{$x}{$y};
  my $mod = $x % $y;
  $dp->{$x}{$y} = $mod;
  $mod == 0 ? $y : gcb($y, $mod);
}


use v5.18;
use warnings;
use List::Util qw( reduce all max );

<>;
my @nums = glob <>;

say max map {
  my $r = reduce { gcb($a, $b) } @nums[0 .. $_ - 1];
  my $l = reduce { gcb($a, $b) } @nums[$_ + 1 .. $#nums];
  my @defined = grep { defined $_ } ($r, $l);
  if (@defined == 2) {
    gcb($r, $l);
  } elsif (@defined == 1) {
    $defined[0];
  } else {
    ();
  }
} 0 .. $#nums;

sub gcb {
  my ($x, $y) = @_;
  state $dp = +{};
  return $dp->{$x}{$y} if defined $dp->{$x}{$y};
  my $mod = $x % $y;
  $dp->{$x}{$y} = $mod;
  $mod == 0 ? $y : gcb($y, $mod);
}


Score : 100 points

Problem Statement

Takahashi likes the sound when he buys a drink from a vending machine.

That sound can be heard by spending A yen (the currency of Japan) each time.

Takahashi has B yen. He will hear the sound as many times as he can with that money, but at most C times, as he would be satisfied at that time.

How many times will he hear the sound?

Constraints

All values in input are integers.

1 \leq A, B, C \leq 100

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the number of times Takahashi will hear his favorite sound.

Sample Input 1

2 11 4

Sample Output 1

4

Since he has not less than 8 yen, he will hear the sound four times and be satisfied.

Sample Input 2

3 9 5

Sample Output 2

3

He may not be able to be satisfied.

Sample Input 3

100 1 10

Sample Output 3

0


#!/usr/bin/perl -w
$hoge=<>;
($a,$b,$c)=split(/ /,$hoge);
if($b/$a<$c){
    print int($b/$a);
}else{
    print $c;
}


if($b/$a<$c){
    print int($b/$a);
}else{
    print $c;
}


#!/usr/bin/perl

my($A,$B,$C)=split(/ /, <>);

if($A*$C<$B){
  print $C;
} else {
  print int($B/$A);
}


my $input = <>;
my ($A, $B, $C) =  split(' ', $input);

my $max =  int( $B / $A );
print $max > $C ? $C : $max;


Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to Stone i + 1 or Stone i + 2. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq h_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

4
10 30 40 20

Sample Output 1

30

If we follow the path 1 → 2 → 4, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

Sample Input 2

2
10 10

Sample Output 2

0

If we follow the path 1 → 2, the total cost incurred would be |10 - 10| = 0.

Sample Input 3

6
30 10 60 10 60 50

Sample Output 3

40

If we follow the path 1 → 3 → 5 → 6, the total cost incurred would be |30 - 60| + |60 - 60| + |60 - 50| = 40.


Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, i + K. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 100

1 \leq h_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N K
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

5 3
10 30 40 50 20

Sample Output 1

30

If we follow the path 1 → 2 → 5, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

Sample Input 2

3 1
10 20 10

Sample Output 2

20

If we follow the path 1 → 2 → 3, the total cost incurred would be |10 - 20| + |20 - 10| = 20.

Sample Input 3

2 100
10 10

Sample Output 3

0

If we follow the path 1 → 2, the total cost incurred would be |10 - 10| = 0.

Sample Input 4

10 4
40 10 20 70 80 10 20 70 80 60

Sample Output 4

40

If we follow the path 1 → 4 → 8 → 10, the total cost incurred would be |40 - 70| + |70 - 70| + |70 - 60| = 40.


Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^5

1 \leq w_i \leq W

1 \leq v_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 2

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.


use List::Util qw(max);

my ( $n, $w ) = glob <>;

my @dp;
for ( my $i = 0 ; $i < $n ; $i++ ) {
    my ( $wi, $vi ) = glob <>;
    my @dpp = @dp;
    for ( my $j = $wi ; $j < $w + 1 ; $j++ ) {
        $dp[$j] = max( $dpp[$j], $dpp[ $j - $wi ] + $vi );
    }
}

print( $dp[$w] );


use List::Util qw(max);
my ($N, $W) = glob<>;
@dp;
for($i=0;$i<$N;$i++){
    my($weight,$val)=glob<>;
    for($j=$W;$j>=0;$j--){
        if($weight<=$j){
            $tmp = $dp[$j-$weight] ? $dp[$j-$weight] : 0;
            $vd = $dp[$j-$weight] + $val;
            if($vd > $dp[$j]){
                $dp[$j] = $vd;
            }
        }
    }
}
print $dp[$W];


use strict;
use warnings;

my $d = <STDIN>;
chomp($d);
if ( $d == 25 ){
        print "Christmas";
}
elsif( $d == 24 ){
        print "Christmas Eve";
}
elsif( $d == 23 ){
        print "Christmas Eve Eve";
}
else{
        print "Christmas Eve Eve Eve";
}

Score : 100 points

Problem Statement

In some other world, today is December D-th.

Write a program that prints Christmas if D = 25, Christmas Eve if D = 24, Christmas Eve Eve if D = 23 and Christmas Eve Eve Eve if D = 22.

Constraints

22 \leq D \leq 25

D is an integer.

Input

Input is given from Standard Input in the following format:

D

Output

Print the specified string (case-sensitive).

Sample Input 1

25

Sample Output 1

Christmas

Sample Input 2

22

Sample Output 2

Christmas Eve Eve Eve

Be sure to print spaces between the words.


use strict;
use warnings;

my $d = STDIN;
chomp($d);
if ( $d == 25 ){
        print "Christmas";
}
elsif( $d == 24 ){
        print "Christmas Eve";
}
elsif( $d == 23 ){
        print "Christmas Eve Eve";
}
else{
        print "Christmas Eve Eve Eve";
}


$_ = <>;
if ($_ == 25) {
    print "Christmas";
}
if ($_ == 24) {
    print "Christmas Eve";
}
if ($_ == 23) {
    print "Christmas Eve Eve";
}
if ($_ == 22) {
    print "Christmas Eve Eve Eve";
}


my $n = +get;
say 'Christmas' ~ (' Eve' x (25 - $n));


#!/usr/bin/perl
my $d = scalar(<STDIN>) - 0;
print "Christmas Eve Eve Eve\n" if $d == 22;
print "Christmas Eve Eve\n" if $d == 23;
print "Christmas Eve\n" if $d == 24;
print "Christmas\n" if $d == 25;


#!/usr/bin/perl -w

chomp($line=<>);
print "Christmas ";
for(;$line<25;$line++){
    print "Eve ";
}


#!/usr/bin/perl -w

chomp($line=<>);
print "Chrismas ";
for(;$line<25;$line++){
    print "Eve ";
}


my $d = <STDIN>;

if($d==25){
        print "Christmas";
}
elsif($d==24){
        print "Christmas Eve";
}
elsif($d==23){
        print "Christmas Eve Eve";
}
elsif($d==22){
        print "Christmas Eve Eve Eve";
}
else{
}


Score: 300 points

Problem Statement

In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc.

The pyramid had center coordinates (C_X, C_Y) and height H. The altitude of coordinates (X, Y) is max(H - |X - C_X| - |Y - C_Y|, 0).

Aoki, an explorer, conducted a survey to identify the center coordinates and height of this pyramid. As a result, he obtained the following information:

C_X, C_Y was integers between 0 and 100 (inclusive), and H was an integer not less than 1.

Additionally, he obtained N pieces of information. The i-th of them is: "the altitude of point (x_i, y_i) is h_i."

This was enough to identify the center coordinates and the height of the pyramid. Find these values with the clues above.

Constraints

N is an integer between 1 and 100 (inclusive).

x_i and y_i are integers between 0 and 100 (inclusive).

h_i is an integer between 0 and 10^9 (inclusive).

The N coordinates (x_1, y_1), (x_2, y_2), (x_3, y_3), ..., (x_N, y_N) are all different.

The center coordinates and the height of the pyramid can be uniquely identified.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 h_1
x_2 y_2 h_2
x_3 y_3 h_3
:
x_N y_N h_N

Output

Print values C_X, C_Y and H representing the center coordinates and the height of the pyramid in one line, with spaces in between.

Sample Input 1

4
2 3 5
2 1 5
1 2 5
3 2 5

Sample Output 1

2 2 6

In this case, the center coordinates and the height can be identified as (2, 2) and 6.

Sample Input 2

2
0 0 100
1 1 98

Sample Output 2

0 0 100

In this case, the center coordinates and the height can be identified as (0, 0) and 100.

Note that C_X and C_Y are known to be integers between 0 and 100.

Sample Input 3

3
99 1 191
100 1 192
99 0 192

Sample Output 3

100 0 193

In this case, the center coordinates and the height can be identified as (100, 0) and 193.


Score : 100 points

Problem Statement

You have decided to give an allowance to your child depending on the outcome of the game that he will play now.

The game is played as follows:

There are three "integer panels", each with a digit between 1 and 9 (inclusive) printed on it, and one "operator panel" with a + printed on it.

The player should construct a formula of the form X + Y, by arranging the four panels from left to right. (The operator panel should not be placed at either end of the formula.)

Then, the amount of the allowance will be equal to the resulting value of the formula.

Given the values A, B and C printed on the integer panels used in the game, find the maximum possible amount of the allowance.

Constraints

All values in input are integers.

1 \leq A, B, C \leq 9

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the maximum possible amount of the allowance.

Sample Input 1

1 5 2

Sample Output 1

53

The amount of the allowance will be 53 when the panels are arranged as 52+1, and this is the maximum possible amount.

Sample Input 2

9 9 9

Sample Output 2

108

Sample Input 3

6 6 7

Sample Output 3

82


#!/usr/bin/env perl

chomp($s = <STDIN>);
($a, $b, $c)=sort(split(/ /, $s));

print "$c$b" + $a;


use List::Util qw(max);
use List::Util qw(sum);
my $input = <STDIN>;
chomp $input;
my ($A, $B, $C) = split / /, $input;
my $max = max($A,$B,$C);
my $sum = sum($A,$B,$C);
my $res = 9*$max+$sum;
print "$res\n";


#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use 5.010;
my @args;
for (<>) {
    chomp $_;
    push @args, $_;
}

my ($A, $B, $C) = sort split " ", shift @args;
say $C * 10 + $B + $A;

__END__

# 6
3 2
1 1 2


Score : 200 points

Problem Statement

Our world is one-dimensional, and ruled by two empires called Empire A and Empire B.

The capital of Empire A is located at coordinate X, and that of Empire B is located at coordinate Y.

One day, Empire A becomes inclined to put the cities at coordinates x_1, x_2, ..., x_N under its control, and Empire B becomes inclined to put the cities at coordinates y_1, y_2, ..., y_M under its control.

If there exists an integer Z that satisfies all of the following three conditions, they will come to an agreement, but otherwise war will break out.

X < Z \leq Y

x_1, x_2, ..., x_N < Z

y_1, y_2, ..., y_M \geq Z

Determine if war will break out.

Constraints

All values in input are integers.

1 \leq N, M \leq 100

-100 \leq X < Y \leq 100

-100 \leq x_i, y_i \leq 100

x_1, x_2, ..., x_N \neq X

x_i are all different.

y_1, y_2, ..., y_M \neq Y

y_i are all different.

Input

Input is given from Standard Input in the following format:

N M X Y
x_1 x_2 ... x_N
y_1 y_2 ... y_M

Output

If war will break out, print War; otherwise, print No War.

Sample Input 1

3 2 10 20
8 15 13
16 22

Sample Output 1

No War

The choice Z = 16 satisfies all of the three conditions as follows, thus they will come to an agreement.

X = 10 < 16 \leq 20 = Y

8, 15, 13 < 16

16, 22 \geq 16

Sample Input 2

4 2 -48 -1
-20 -35 -91 -23
-22 66

Sample Output 2

War

Sample Input 3

5 3 6 8
-10 3 1 5 -100
100 6 14

Sample Output 3

War


use v6;

my ($n, $m, $x, $y) = +<<get.words;
my @xs = +<<get.words;
my @ys = +<<get.words;
my ($mx, $my) = @xs.max, @ys.min;
if $mx < $my && $x < $mx && $my < $y {
  say 'No War'
}
else {
  say 'War';
}


#!/usr/bin/env perl

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

chomp($s=<STDIN>);
@x=split(/ /,$s);

chomp($s=<STDIN>);
@y=split(/ /,$s);

$z=$y;
foreach $y (@y) {
  $z > $y and $z = $y;
}

$ok = 1;

foreach $x (@x, $x) {
  $x < $z or $ok = 0;
}

print (($ok ? "No " : "") . "War\n");


#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use 5.010;
my @args;
for (<>) {
    chomp $_;
    push @args, $_;
}

my ($N, $M, $X, $Y) = split " ", shift @args;
my @x = split " ", shift @args;
my @y = split " ", shift @args;
my $xmax = (sort {$b <=> $a} @x)[0];
my $ymin = (sort {$a <=> $b} @y)[0];

if ($xmax < $ymin and $xmax < $Y and $X < $ymin) {
    say 'No War';
} else {
    say 'War';
}

__END__


#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use 5.010;
my @args;
for (<>) {
    chomp $_;
    push @args, $_;
}

my ($N, $M, $X, $Y) = split " ", shift @args;
my @x = split " ", shift @args;
my @y = split " ", shift @args;
print Dumper \@x;
my $xmax = (sort {$b <=> $a} @x)[0];
my $ymin = (sort {$a <=> $b} @y)[0];

say "xmax: $xmax, ymin: $ymin";
if ($xmax < $ymin and $xmax < $Y and $X < $ymin) {
    say 'No War';
} else {
    say 'War';
}

__END__


Score : 100 points

Problem Statement

You are given integers A and B, each between 1 and 3 (inclusive).

Determine if there is an integer C between 1 and 3 (inclusive) such that A \times B \times C is an odd number.

Constraints

All values in input are integers.

1 \leq A, B \leq 3

Input

Input is given from Standard Input in the following format:

A B

Output

If there is an integer C between 1 and 3 that satisfies the condition, print Yes; otherwise, print No.

Sample Input 1

3 1

Sample Output 1

Yes

Let C = 3. Then, A \times B \times C = 3 \times 1 \times 3 = 9, which is an odd number.

Sample Input 2

1 2

Sample Output 2

No

Sample Input 3

2 2

Sample Output 3

No


Score : 200 points

Problem Statement

Takahashi is practicing shiritori alone again today.

Shiritori is a game as follows:

In the first turn, a player announces any one word.

In the subsequent turns, a player announces a word that satisfies the following conditions:

That word is not announced before.

The first character of that word is the same as the last character of the last word announced.

In this game, he is practicing to announce as many words as possible in ten seconds.

You are given the number of words Takahashi announced, N, and the i-th word he announced, W_i, for each i. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions.

Constraints

N is an integer satisfying 2 \leq N \leq 100.

W_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
W_1
W_2
:
W_N

Output

If every word announced by Takahashi satisfied the conditions, print Yes; otherwise, print No.

Sample Input 1

4
hoge
english
hoge
enigma

Sample Output 1

No

As hoge is announced multiple times, the rules of shiritori was not observed.

Sample Input 2

9
basic
c
cpp
php
python
nadesico
ocaml
lua
assembly

Sample Output 2

Yes

Sample Input 3

8
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaa
aaaaaaa

Sample Output 3

No

Sample Input 4

3
abc
arc
agc

Sample Output 4

No


<>;
undef $/;
my $s = <>;
my @w = split /\s+/, $s;
my %w = map { $_ => 1 } @w;
@w != keys %w and print 'No' and exit;
$s =~ /^(?:\w*(\w)\s+(?=\g{-1}))+\w+\s+$/ and print 'Yes' and exit;
print 'No';


my $count = <STDIN>;

my $hash = {};
my $before_w = '';
for (1..$count) {
    my $w = <STDIN>;
    chomp($w);
    if ($hash->{$w} || ($_ != 1 && (substr($before_w, -1, 1) ne substr($w, 0, 1)))) {
        print('No');
        last;
    } elsif ($_ == $count) {
        print('Yes');
    } else {
        $before_w = $w;
        $hash->{$w} = 100;
    }

}


my $n = <STDIN>;
my @w;
for ($i=0; $i<$n; $i++) {
  my $input = <STDIN>;
  chomp $input;
  push(@w, $input);
}
my %uniq = map {$_ => 1} @w;
my $num = keys %uniq;
if ($num != $n) {
  $ret = "No\n";
} else {
  $ret = "Yes\n";
  for ($i=0; $i<$n-1; $i++) {
    if(substr($w[$i],-1,1) ne substr($w[$i+1],0,1)) {
    	$ret = "No\n";
        last;
    }
  }
}
print $ret;


#!/usr/bin/perl
#B - Shiritori

use strict;
use warnings;

my ($ret) = 'Yes';

# データ取得
my ($data_length);
    $data_length = <STDIN>;

#
my ($i);
my ($word);
my (@word_list);
my ($last_word) = '';
my ($head);

for ($i = 0 ; $i < $data_length ; $i++) {
    $word = <STDIN>;
    chomp($word);

    $head = substr($word , 0 , 1);

    if (   (   ($last_word eq $head)
            && (!(grep {$_ eq $word} @word_list)))
        || ($last_word eq '')) {

        # None

    }else{
        $ret = 'No';
        last;
    }

    # 覚える
    $word_list[$i] = $word;

    # 最後の文字をとる
    $last_word = substr($word , length($word)-1 , 1);
}

print("$ret\n");

exit(0);


Score : 300 points

Problem Statement

There are N cities on a number line. The i-th city is located at coordinate x_i.

Your objective is to visit all these cities at least once.

In order to do so, you will first set a positive integer D.

Then, you will depart from coordinate X and perform Move 1 and Move 2 below, as many times as you like:

Move 1: travel from coordinate y to coordinate y + D.

Move 2: travel from coordinate y to coordinate y - D.

Find the maximum value of D that enables you to visit all the cities.

Here, to visit a city is to travel to the coordinate where that city is located.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq X \leq 10^9

1 \leq x_i \leq 10^9

x_i are all different.

x_1, x_2, ..., x_N \neq X

Input

Input is given from Standard Input in the following format:

N X
x_1 x_2 ... x_N

Output

Print the maximum value of D that enables you to visit all the cities.

Sample Input 1

3 3
1 7 11

Sample Output 1

2

Setting D = 2 enables you to visit all the cities as follows, and this is the maximum value of such D.

Perform Move 2 to travel to coordinate 1.

Perform Move 1 to travel to coordinate 3.

Perform Move 1 to travel to coordinate 5.

Perform Move 1 to travel to coordinate 7.

Perform Move 1 to travel to coordinate 9.

Perform Move 1 to travel to coordinate 11.

Sample Input 2

3 81
33 105 57

Sample Output 2

24

Sample Input 3

1 1
1000000000

Sample Output 3

999999999


use v5.18; # strict say state
use warnings;
use List::Util qw(reduce first max min sum0);

chomp (my ($n, $x) = split / /, <STDIN>);
chomp (my @y = split / /, <STDIN>);
my $d = max(@y, $x) - min(@y, $x);
my $gcd = sub {
  my ($x, $y) = @_;
  while ($y) {
    my $r = $x % $y;
    $r += $y if $r < 0;
    $x = $y;
    $y = $r;
  }
  return $x;
};
my $g = $x;
for my $i (0..$n-1) {
  $g = $gcd->($g, $y[$i]);
}
for my $i (1..$d) {
  next if $d%$i>0;
  my $cf = $x % ($d/$i);
  my $sf = 1;
  for my $j (@y) {
    $sf = 0 if $j % ($d/$i) != $cf;
  }
  if ($sf == 1) {
    say $d/$i if $sf == 1;
    exit;
  }
}


#!/usr/bin/perl

my ($N,$X) = map {$_ - 0} split(/\s+/o,<STDIN>);
my @x = map { abs(($_ - 0) - $X) } split(/\s+/o,<STDIN>);
my $D = 10 ** 10;
for(my $i=0;$i<$N;$i++){
  $D = $x[$i] if $x[$i] != 0 and $x[$i] < $D;
}
foreach my $x (@x){
  next if $x == 0;
  my $nD = &gcd($x,$D);
}
print "$D\n";
exit(0);

sub gcd {
  my $mm = shift;
  my $nn = shift;
  while($nn != 0){
    my $rr = $mm % $nn;
    $mm = $nn;
    $nn = $rr;
  }
  return $mm;
}


use Math::BigInt;

my ($N, $X) =  do {
    my $input = <STDIN>;
    chomp $input;
    split(/\s/, $input);
};

my $x = do {
    my $x = <STDIN>;
    chomp $x;
    [ split(/\s/, $x) ];
};

my $gcc = $X - $x->[0];

for (@$x) {
    my $x1 = $X - $_;
    $gcc = Math::BigInt::bgcd($gcc, $x1);
}

print ($gcc);


say [gcd] map &abs,get.words[1..1] <<-<< get.words


say [gcd] get.words[1..1] <<-<< get.words


use Math::BigInt;
my $input = <STDIN>;
chomp $input;
my ($N, $X) = split / /, $input;
my $input = <STDIN>;
chomp $input;
my @x = split / /, $input;
for ($i=0; $i<$N; $i++) {
  $x[$i] -= $X;
}
my $gcd = Math::BigInt::bgcd(@x);
print $gcd;


Score: 200 points

Problem Statement

Today, the memorable AtCoder Beginner Contest 100 takes place. On this occasion, Takahashi would like to give an integer to Ringo.

As the name of the contest is AtCoder Beginner Contest 100, Ringo would be happy if he is given a positive integer that can be divided by 100 exactly D times.

Find the N-th smallest integer that would make Ringo happy.

Constraints

D is 0, 1 or 2.

N is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

D N

Output

Print the N-th smallest integer that can be divided by 100 exactly D times.

Sample Input 1

0 5

Sample Output 1

5

The integers that can be divided by 100 exactly 0 times (that is, not divisible by 100) are as follows: 1, 2, 3, 4, 5, 6, 7, ...

Thus, the 5-th smallest integer that would make Ringo happy is 5.

Sample Input 2

1 11

Sample Output 2

1100

The integers that can be divided by 100 exactly once are as follows: 100, 200, 300, 400, 500, 600, 700, 800, 900, 1 \ 000, 1 \ 100, ...

Thus, the integer we are seeking is 1 \ 100.

Sample Input 3

2 85

Sample Output 3

850000

The integers that can be divided by 100 exactly twice are as follows: 10 \ 000, 20 \ 000, 30 \ 000, ...

Thus, the integer we are seeking is 850 \ 000.


#! usr/bin/perl

use strict;
use warnings;

chomp(my $line = <STDIN>);
my ($D,$N) = split ' ',$line;

my $result = $N * 100**$D;
print "$result\n";


use v5.18;
use warnings;
sub read_line { chomp(my $str = <STDIN>); $str }
sub splited_read_line { split / /, read_line }

my ($d, $n) = splited_read_line;
$n += 1 if $n == 100;
say 100 ** $d * $n;


use warnings;
use strict;

my $input = <STDIN>;
chomp($input);

my ($D, $N) = split ' ', $input;
print 100 ** $D * $N;


use v5.18;
use warnings;
sub read_line { chomp(my $str = <STDIN>); $str }
sub splited_read_line { split / /, read_line }

my ($d, $n) = splited_read_line;
say 100 ** $d * $n;


Score : 200 points

Problem Statement

You have A 500-yen coins, B 100-yen coins and C 50-yen coins (yen is the currency of Japan).
In how many ways can we select some of these coins so that they are X yen in total?

Coins of the same kind cannot be distinguished. Two ways to select coins are distinguished when, for some kind of coin, the numbers of that coin are different.

Constraints

0 \leq A, B, C \leq 50

A + B + C \geq 1

50 \leq X \leq 20 000

A, B and C are integers.

X is a multiple of 50.

Input

Input is given from Standard Input in the following format:

A
B
C
X

Output

Print the number of ways to select coins.

Sample Input 1

2
2
2
100

Sample Output 1

2

There are two ways to satisfy the condition:

Select zero 500-yen coins, one 100-yen coin and zero 50-yen coins.

Select zero 500-yen coins, zero 100-yen coins and two 50-yen coins.

Sample Input 2

5
1
0
150

Sample Output 2

0

Note that the total must be exactly X yen.

Sample Input 3

30
40
50
6000

Sample Output 3

213


use strict;
use warnings;

my $a = <STDIN>;
chomp $a;
my $b = <STDIN>;
chomp $b;
my $c = <STDIN>;
chomp $c;
my $x = <STDIN>;
chomp $x;

my $ans = 0;
for (my $i=0; $i<=$a; $i++)
{
    for (my $j=0; $j<=$b; $j++)
    {
        for (my $k=0; $k<=$c; $k++)
        {
            $ans++ if $i*500+$j*100+$k*50==$x;
        }
    }
}

print "$ans\n";


my $a = glob<>;
my $b = glob<>;
my $c = glob<>;
my $x = glob<>;

my $cnt = 0;
for my $i ( 0 .. $a ) {
    for my $j ( 0 .. $b ) {
	my $y = $x - 500*$i - 100*$j;
	$y < 0 and last;
	$y / 50 <= $c and $cnt++;
    }
}
print $cnt;


use strict;
use warnings;
use utf8;

my ($A) = split /\s/, <STDIN>;
my ($B) = split /\s/, <STDIN>;
my ($C) = split /\s/, <STDIN>;
my ($X) = split /\s/, <STDIN>;

my @patterns;
for (my $cc = 0; $cc <= $C; $cc++) {
    for (my $bb = 0; $bb <= $B; $bb++) {
        for (my $aa = 0; $aa <= $A; $aa++) {
            my $sum = $aa * 500 + $bb * 100 + $cc * 50;
            if ($sum == $X) {
                push @patterns, $sum;
            }
        }
    }
}

printf "%s\n", scalar @patterns;


#!/usr/bin/env perl

use strict;
use warnings;
use 5.18.2;

use constant FIVE_HUNDRED_COIN => 500;
use constant HUNDRED_COIN => 100;
use constant FIFTY_COIN => 50;

my $input = do { local $/; <> };

chomp $input;

my ($fivecoin, $hundredcoin, $fiftycoin, $goal) = split /\n/, $input;

my @coins = ((FIVE_HUNDRED_COIN) x $fivecoin, (HUNDRED_COIN) x $hundredcoin, (FIFTY_COIN) x $fiftycoin);

my $total = $fivecoin * FIVE_HUNDRED_COIN + $hundredcoin * HUNDRED_COIN + $fiftycoin * FIFTY_COIN;

say 0 if $total < $goal;

my $way_count = 0;

for my $fiveh (0..$fivecoin) {
    for my $hund (0..$hundredcoin) {
        for my $five (0..$fiftycoin) {
            my $total = $fiveh * FIVE_HUNDRED_COIN + $hund * HUNDRED_COIN + $five * FIFTY_COIN;
            if ($total == $goal) {
                $way_count++;
            }
        }
    }
}

say $way_count;


#!/usr/bin/env perl

use strict;
use warnings;
use 5.18.2;

use constant FIVE_HUNDRED_COIN => 500;
use constant HUNDRED_COIN => 100;
use constant FIFTY_COIN => 50;

my $input = do { local $/; <> };

chomp $input;

my ($fivecoin, $hundredcoin, $fiftycoin, $goal) = split /\n/, $input;

my @coins = ((FIVE_HUNDRED_COIN) x $fivecoin, (HUNDRED_COIN) x $hundredcoin, (FIFTY_COIN) x $fiftycoin);

my $total = $fivecoin * FIVE_HUNDRED_COIN + $hundredcoin * HUNDRED_COIN + $fiftycoin * FIFTY_COIN;

say 0 if $total < $goal;

my $way_count = 0;

for my $fiveh (0..$fivecoin) {
    for my $hund (0..$hundredcoin) {
        for my $five (0..$fiftycoin) {
            my $total = $fiveh * FIVE_HUNDRED_COIN + $hund * HUNDRED_COIN + $five * FIFTY_COIN;
            next if $total != $goal;
            $way_count++;
        }
    }
}

say $way_count;


use strict;
use warnings;

main();

sub main{
	my $A = <STDIN>;
	my $B = <STDIN>;
	my $C = <STDIN>;
	my $X = <STDIN>;

	my $cnt = 0;

	for(my $a=0; $a<=$A; $a++){
		for(my $b=0; $b<=$B; $b++){
			for(my $c=0; $c<=$C; $c++){
				my $total = 500*$a + 100*$b + 50*$c;
				if($total == $X){ $cnt++;}
			}
		}
	}

	print "$cnt\n" ;
	1;
}


use strict;
use warnings;

main();

sub main{
	my $A = <STDIN>;
	my $B = <STDIN>;
	my $C = <STDIN>;
	my $X = <STDIN>;

	my $total = 500*$A + 100*$B + 50*$C;

	if($total < $X){ print "0\n"; last; }

	my $cnt = 0;

	for(my $a=0; $a<=$A; $a++){
		for(my $b=0; $b<=$B; $b++){
			for(my $c=0; $c<=$C; $c++){
				$total = 500*$a + 100*$b + 50*$c;
				if($total == $X){ $cnt++;}
			}
		}
	}

	print "$cnt\n" ;
	1;
}


# use strict;
my ($a,$b) = glob<>;
print $a*$b%2==0?Even:Odd

Score : 100 points

Problem Statement

AtCoDeer the deer found two positive integers, a and b.
Determine whether the product of a and b is even or odd.

Constraints

1 ≤ a,b ≤ 10000

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the product is odd, print Odd; if it is even, print Even.

Sample Input 1

3 4

Sample Output 1

Even

As 3 × 4 = 12 is even, print Even.

Sample Input 2

1 21

Sample Output 2

Odd

As 1 × 21 = 21 is odd, print Odd.


use strict;
my ($a,$b) = glob<>;
if ($a*$b%2==0){
    print "Even"
}
else{
    print "Odd"
}


($a, $b) = glob<>;
print $a*$b%2>0?Odd:Even


#!/usr/bin/env perl

use strict;
use warnings;
use utf8;

my $num = (<> =~ tr/ //dr);
my $res = (sqrt($num) == int(sqrt($num))) ? "Yes" : "No";
print $res, "\n";


#!/usr/bin/env perl

use strict;
use warnings;
use utf8;

my ($a, $b) = split(/ /, <>);
my $ret = (($a * $b) % 2) ? "Odd" : "Even";
print $ret . "\n";


#!/usr/bin/env perl

use strict;
use warnings;
use utf8;

my ($a, $b) = split(/ /, <>);
my $ret = (($a + $b) % 2) ? "Odd" : "Even";
print $ret . "\n";


use strict;
use warnings;

my ($a, $b);
{
    my $in = <STDIN>;
    ($a, $b) = split(" ", $in);
}

print "Even\n" if $a*$b%2==0;
print "Odd\n" unless $a*$b%2==0;


use strict;
use warnings;

chomp(my $text = <>);
my($a, $b) = split / /, $text;

if($a%2 ==0 || $b%2 == 0){
	print "Even\n";
} else {
	print "Odd\n";
}


use strict;
use warnings;

chomp(my $text = <>);
my($a, $b) = split / /, $text;

if($a*$b%2 == 0){
	print "Even\n";
} else {
	print "Odd\n";
}


use strict;
use warnings;

my ($a, $b) = split(" ", <STDIN>);
print ($a * $b % 2 == 0 ? "Even" : "Odd");


use strict;
use warnings;

my $input = <>;
chomp $input;
my ($a, $b) = split / /, $input;
if($a%2 == 0 || $b%2 == 0){
    print "Even\n";
}else{
    print "Odd\n";
}


chomp($n=<>);
($a,$b)=split/ /,$n;
if($a*$b%2==0){
    print "Even";
}else{
    print "Odd";
}


my ($a, $b) = split " ", <>;
my $result = $a*$b % 2 ? 'Odd' : 'Even';
print $result;


#!/usr/bin/env perl

use strict;
use warnings;

my $input = do { local $/; <> };

my ($left, $right) = split / /, $input;

if ($left % 2 == 0 || $right % 2 == 0) {
	print 'Even';
} else {
	print 'Odd';
}


#!/usr/bin/perl

$num = <STDIN>;
$num =~ s/\s+//g;

#print $num;

for ($i = 1; $i < $num; $i++) {
    if ($i * $i == $num) {
        print "Yes\n";
        last;
    } elsif ($i * $i > $num) {
        print "No\n";
        last;
    }
}


#!/usr/bin/perl

$num = <STDIN>;
$num =~ s/\s+//g;

for ($i = 1; $i < $num; $i++) {
    if ($i * $i == $num) {
        print "Yes\n";
        last;
    } elsif ($i * $i > $num) {
        print "No\n";
        last;
    }
}


#!/usr/bin/perl

$line = <STDIN>;
($a, $b) = split(/\s+/, $line);

$c = $a * $b;
if ($c % 2 == 0) {
	print "Even\n";
} else {
	print "Odd\n";
}


use strict;
use warnings;

main();

sub main{
	my $in = <STDIN>;

	(my $A, my $B) = split(/ /, $in);
	my $mul = $A * $B;
	if($mul%2){ print "Odd\n"}
	else{ print "Even\n"}

	# printf "> %d\n", $mul;
	# printf ">> %d\n", $mul%2;
	1;
}


#! /usr/bin/perl

use strict;
use warnings;

my $buf = <>;
my($a, $b) = split(/\s+/, $buf);
if($a % 2 == 0 || $b % 2 == 0){
    print "Even\n";
}else{
    print "Odd\n";
}


my $a, $b = split " ", <>;
my $prod = $a * $b;
($prod % 2 == 0) ? print "Odd" : print "Even";


my $a, $b = split " ", <>;
my $prod = $a * $b;
($prod % 2 == 0) ? print "Even" : print "Odd";


my $a, $b = split " ", <>;
my $prod = $a * $b;
($prod % 2) ? print "Even" : print "Odd";


chomp (my ($n, $m) = split / /, readline ARGV);
print $n * $m % 2 ? 'Odd' : 'Even';


#!/usr/bin/perl

use strict;

my $ret = "Odd";
my ($a, $b) = <STDIN> =~ /(\d+)\s(\d+)/;

$ret = "Even" if  ($a * $b) % 2 eq 0;

print "$ret";


#!/usr/bin/perl

use strict;

print "input: ";
my $n = <STDIN>;
my ($a, $b) = $n =~ /(\d+)\s(\d+)/;

if ( ($a * $b) % 2 eq 0 ) {
    print "Even";
} else {
    print "Odd";
}


#!/usr/bin/perl
#abc086_a.pl
#Sun Jan 21 21:00:30 2018
#

use warnings;
use strict;

my $temp = <STDIN>;
chomp $temp;

my @temps = split(" ",$temp);

my $a = $temps[0];
my $b = $temps[1];

my $total = $a * $b;

if ($total%2==1){
	print "Odd\n";
}else{
	print "Even\n";
}


Score : 300 points

Problem Statement

The commonly used bills in Japan are 10000-yen, 5000-yen and 1000-yen bills. Below, the word "bill" refers to only these.

According to Aohashi, he received an otoshidama (New Year money gift) envelope from his grandfather that contained N bills for a total of Y yen, but he may be lying. Determine whether such a situation is possible, and if it is, find a possible set of bills contained in the envelope. Assume that his grandfather is rich enough, and the envelope was large enough.

Constraints

1 ≤ N ≤ 2000

1000 ≤ Y ≤ 2 × 10^7

N is an integer.

Y is a multiple of 1000.

Input

Input is given from Standard Input in the following format:

N Y

Output

If the total value of N bills cannot be Y yen, print -1 -1 -1.

If the total value of N bills can be Y yen, let one such set of bills be "x 10000-yen bills, y 5000-yen bills and z 1000-yen bills", and print x, y, z with spaces in between. If there are multiple possibilities, any of them may be printed.

Sample Input 1

9 45000

Sample Output 1

4 0 5

If the envelope contained 4 10000-yen bills and 5 1000-yen bills, he had 9 bills and 45000 yen in total. It is also possible that the envelope contained 9 5000-yen bills, so the output 0 9 0 is also correct.

Sample Input 2

20 196000

Sample Output 2

-1 -1 -1

When the envelope contained 20 bills in total, the total value would be 200000 yen if all the bills were 10000-yen bills, and would be at most 195000 yen otherwise, so it would never be 196000 yen.

Sample Input 3

1000 1234000

Sample Output 3

14 27 959

There are also many other possibilities.

Sample Input 4

2000 20000000

Sample Output 4

2000 0 0


use strict;
use warnings;

main();

sub main{
	(my $nOB, my $total) =split" ", ,<stdin>;

	for(my $i = 0; $i <= $nOB; $i++){
		for(my $j = 0; $i+$j <= $nOB; $j++){
			my $k = $nOB - $i -$j;
			my $tmpVal = $i*10000 + $j*5000 + $k*1000;
			if($k >= 0 && $tmpVal == $total){ print "$i $j $k\n"; return;}
		}
	}
	print "-1 -1 -1\n"

}
1;


use strict;
use warnings;

main();

sub main{
	(my $nOB, my $total) =split" ", ,<stdin>;
	my $tmpVal = 0;

	for(my $i = 0; $i <= $nOB; $i++){
		for(my $j = 0; $i+$j <= $nOB; $j++){
			$tmpVal = $total - $i*10000;
			$tmpVal = $tmpVal - $j*5000;
			my $k = int($tmpVal/1000);
			if($k < 0){ next; }
			elsif(($i+$j+$k) > $nOB){ next; }
			else{print "$i $j $k\n"; return;}
		}
	}
	print "-1 -1 -1\n"

}
1;


use strict;
use warnings;

main();

sub main{
	(my $nOB, my $total) =split" ", ,<stdin>;

	for(my $i = 0; $i <= $nOB; $i++){
		for(my $j = 0; $i+$j <= $nOB; $j++){
			my $k = int( ($total - (10000*$i + 5000*$j) )/1000);
			if($k < 0){ next; }
			elsif(($i+$j+$k) > $nOB){ next; }
			else{print "$i $j $k\n"; return;}
		}
	}
	print "-1 -1 -1\n"

}
1;


use strict;
use warnings;

main();

sub main{
	(my $nOB, my $total) =split" ", ,<stdin>;

	my $t3 = int($total/10000);
	$total = $total - $t3*10000;
	my $t2 = int($total/5000);
	$total = $total - $t2*5000;
	my $t1 = int($total/1000);

	if ($t3+$t2+$t1 <= $nOB){ print "$t3 $t2 $t1\n" }
	else { print "-1 -1 -1\n" }
}
1;


#! /usr/bin/perl

use strict;
use warnings;

chomp(my $buf = <>);
my($count, $total) = split(/\s+/, $buf);

xxx($total - $count * 1000, $count, 0, 0);
print "-1 -1 -1\n";

sub xxx {
    my($remain, $c1k, $c5k, $c10k) = @_;

    if($remain == 0) {
        print "$c10k $c5k $c1k\n";
        exit;
    }
    if($remain < 0) {
        print "-1 -1 -1\n";
        exit;
    }
    if(4000 <= $remain && 0 < $c1k) {
        xxx($remain - 4000, $c1k-1, $c5k+1, $c10k);
    }
    if(9000 <= $remain && 0 < $c1k) {
        my($tmpRemain, $tmpC1k, $tmpC10k) = ($remain, $c1k, $c10k);
        while(9000 <= $tmpRemain && 0 < $tmpC1k){
            $tmpRemain -= 9000;
            $tmpC1k --;
            $tmpC10k ++;
        }
        xxx($tmpRemain, $tmpC1k, $c5k, $tmpC10k);
    }
    if(5000 <= $remain && 0 < $c5k) {
        my($tmpRemain, $tmpC5k, $tmpC10k) = ($remain, $c5k, $c10k);
        while(5000 <= $tmpRemain && 0 < $tmpC5k){
            $tmpRemain -= 5000;
            $tmpC5k --;
            $tmpC10k ++;
        }
        xxx($tmpRemain, $c1k, $tmpC5k, $tmpC10k);
    }
}


#!/usr/bin/perl
#abc85_c.pl
#Sun Jan  7 20:56:35 2018
#

use warnings;
use strict;

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

my @param = split(" ",$n);

my $bill = $param[0];
my $amount = $param[1]/1000;

my $x = $bill;
my $y = 0;
my $z = 0;

my $tamount;

my $flag = 0;

if ($amount > $bill*10){
	print "-1 -1 -1\n";
	$flag = 1;
}else{
while ($x >= 0){
	if ($flag == 1){
		last;
	}
	$tamount = 10*$x + 5*$y + 1*$z;

#	print $x," ",$y," ",$z," ",$tamount,"testtest\n";

	if ($tamount == $amount){
		print $x," ",$y," ",$z,"\n";
		$flag = 1;
		last;
	}
	$y = $bill-$x;
	while ($y >= 0){
		$z = $bill-$x-$y;
		$tamount = 10*$x + 5*$y + 1*$z;

#		print $x," ",$y," ",$z," ",$tamount,"testtest\n";

		if ($tamount == $amount){
			print $x," ",$y," ",$z,"\n";
			$flag = 1;
			last;
		}
		$y--
	}
	$y = 0;
	$z = 0;
	$x--;
}
}

if ($flag==0){
	print "-1 -1 -1\n"
}


Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, …, Katana N, and can perform the following two kinds of attacks in any order:

Wield one of the katana you have. When you wield Katana i (1 ≤ i ≤ N), the monster receives a_i points of damage. The same katana can be wielded any number of times.

Throw one of the katana you have. When you throw Katana i (1 ≤ i ≤ N) at the monster, it receives b_i points of damage, and you lose the katana. That is, you can no longer wield or throw that katana.

The monster will vanish when the total damage it has received is H points or more. At least how many attacks do you need in order to vanish it in total?

Constraints

1 ≤ N ≤ 10^5

1 ≤ H ≤ 10^9

1 ≤ a_i ≤ b_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N H
a_1 b_1
:
a_N b_N

Output

Print the minimum total number of attacks required to vanish the monster.

Sample Input 1

1 10
3 5

Sample Output 1

3

You have one katana. Wielding it deals 3 points of damage, and throwing it deals 5 points of damage. By wielding it twice and then throwing it, you will deal 3 + 3 + 5 = 11 points of damage in a total of three attacks, vanishing the monster.

Sample Input 2

2 10
3 5
2 6

Sample Output 2

2

In addition to the katana above, you also have another katana. Wielding it deals 2 points of damage, and throwing it deals 6 points of damage. By throwing both katana, you will deal 5 + 6 = 11 points of damage in two attacks, vanishing the monster.

Sample Input 3

4 1000000000
1 1
1 10000000
1 30000000
1 99999999

Sample Output 3

860000004

Sample Input 4

5 500
35 44
28 83
46 62
31 79
40 43

Sample Output 4

9


chomp (my ($n, $h) = split / /, readline ARGV);
my @s;
my $max_r = 0;
for (1..$n) {
  chomp (my ($r, $c) = split / /, readline ARGV);
  push @s, [$r, $c];
  $max_r = $r if $r > $max_r;
}
my @sorted = reverse sort { $a->[1] <=> $b->[1] } @s;

my $hp = $h;
my $cnt = 0;
for my $i (@sorted) {
  last if $i->[1] <= $max_r;
  $hp -= $i->[1];
  $cnt++;
  print "$cnt\n" and exit if $hp <= 0;
}

$cnt += int($hp / $max_r);
$cnt++ unless int($hp / $max_r) == $hp / $max_r;

print "$cnt\n" and exit;


#!/usr/bin/perl
#abc85_d.pl
#Sun Jan  7 20:56:39 2018
#

use warnings;
use strict;

my $fir = <STDIN>;
chomp $fir;

my @temp = split(" ",$fir);

my $n = $temp[0];
my $h = $temp[1];

my $loopnum = $n;

my @cut;
my @throw;

while ($loopnum > 0){
	my $stdin = <STDIN>;
	chomp $stdin;
	my @temp = split(" ",$stdin);
	push (@cut, $temp[0]);
	push (@throw, $temp[1]);
	$loopnum--;
}

my $maxcut = 0;

foreach (@cut){
	if ($maxcut < $_){
		$maxcut = $_;
	}
}

my $thrownum;
my $throwdamage = 0;

my @throwkatana;

foreach (@throw){
	if ($maxcut <= $_){
		$thrownum++;
		push (@throwkatana,$_);
		$throwdamage = $throwdamage+$_;
	}
}

if ($throwdamage >= $h){
	my @throwsorted = sort {$b <=> $a} @throwkatana;
	my $throwcount = 1;
	foreach (@throwsorted){
		$h = $h - $_;
#		print "HP: ${h}\n";
		if ($h <= 0){
			print $throwcount,"\n";
			last;
		}
		$throwcount++;
	}
#	print $throwcount,"\n";
}else{
	my $cutnum = int(($h - $throwdamage)/$maxcut);
	if (($h - $throwdamage)%$maxcut>0){
		$cutnum++;
	}
	print $cutnum+$thrownum,"\n";
}


#!/usr/bin/perl
#abc85_d.pl
#Sun Jan  7 20:56:39 2018
#

use warnings;
use strict;

my $fir = <STDIN>;
chomp $fir;

my @temp = split(" ",$fir);

my $n = $temp[0];
my $h = $temp[1];

my $loopnum = $n;

my @cut;
my @throw;

while ($loopnum > 0){
	my $stdin = <STDIN>;
	chomp $stdin;
	my @temp = split(" ",$stdin);
	push (@cut, $temp[0]);
	push (@throw, $temp[1]);
	$loopnum--;
}

my $maxcut = 0;

foreach (@cut){
	if ($maxcut < $_){
		$maxcut = $_;
	}
}

my $thrownum;
my $throwdamage = 0;

my @throwkatana;

foreach (@throw){
	if ($maxcut <= $_){
		$thrownum++;
		push (@throwkatana,$_);
		$throwdamage = $throwdamage+$_;
	}
}

if ($throwdamage > $h){
	my @throwsorted = sort {$b <=> $a} @throwkatana;
	my $throwcount = 1;
	foreach (@throwsorted){
		$h = $h - $_;
#		print "HP: ${h}\n";
		if ($h <= 0){
			print $throwcount,"\n";
			last;
		}
		$throwcount++;
	}
	print $throwcount,"\n";
}else{
	my $cutnum = int(($h - $throwdamage)/$maxcut);
	if (($h - $throwdamage)%$maxcut>0){
		$cutnum++;
	}
	print $cutnum+$thrownum,"\n";
}


Score : 200 points

Problem Statement

Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Constraints

1 \leq N \leq 10^4

1 \leq A \leq B \leq 36

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Sample Input 1

20 2 5

Sample Output 1

84

Among the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84.

Sample Input 2

10 1 2

Sample Output 2

13

Sample Input 3

100 4 16

Sample Output 3

4554


$/=words;say sum grep $2>=*.comb.sum>=$1,1..$0


use strict;
use warnings;

my ($n, $a, $b);
{
    my $in = <STDIN>;
    ($n, $a, $b) = split(" ", $in);
}
my $ans = 0;
for (my $i=0; $i<=$n; $i++)
{
    my $x = 0;
    my @y = split("", $i);
    $x += $_ for @y;
    $ans += $i if $a<=$x && $x<=$b;
}

print "$ans\n";


use strict;
use warnings;

chomp(my $nab = <>);
my($n, $a, $b) = split / /, $nab;

my $ans = 0;
my $sum = 0;

for (my $i=1; $i <= $n; $i++){
	my @num = split //, $i;;
    foreach (@num) {$sum += $_;}
    if ($sum >= $a && $sum <= $b) {
    $ans += $i;
    }
}

print $ans . "\n";


use strict;
use warnings;

chomp(my $nab = <>);
my($n $a $b) = split / /, $nab;

my $ans = 0;
my $sum = 0;

for (my $i=1; $i <= $n; $i++){
	my @num = split //, $i;;
    foreach (@num) {$sum += $_;}
    if ($sum >= $a && $sum <= $b) {
    $ans += $i;
    }
}

print $ans . "\n";


my ($N, $A, $B) = split " ", <>;
my $result = 0;
for my $n (1..$N) {
    my @num = split "", $n;
    my $sum;
    for my $num (@num) {
        $sum += $num;
    }
    $result += $n if $A <= $sum && $sum <= $B;
}
print $result;


use v6;

my ($n, $a, $b) = get().chomp.split(' ');
my $ans = 0;

loop (my $i = 1; $i <= $n; $i++) {
    my $num = $i;
    my $sum = 0;

    while ($num != 0) {
        $_ = $num % 10;
        $sum += $_;
        last if $b < $sum;
        $num div= 10;
    }

    $ans += $i if (($a <= $sum)and($sum <= $b));
}
say $ans;

exit;


use strict;
use warnings;

main();

sub main{

	(my $N, my $A, my $B) = split" ",<STDIN>;
	my $sum = 0;
	my $total = 0;

	foreach my $in (1..$N){
		$total = ($in/1000)%10 + ($in/100)%10 + ($in/10)%10 + ($in)%10;
		if($A <= $total && $total <= $B){ $sum += $in }
	}

	print $sum."\n";
	1;
}


#! /usr/bin/perl

use strict;
use warnings;

chomp(my $buf = <>);
my($max, $a, $b) = split(/\s+/, $buf);

my $total = 0;
for my $n (1 .. $max) {
    my $sum = 0;
    foreach my $c (split(//, $n)) {
        $sum += $c;
    }
    if($a <= $sum && $sum <= $b) {
        $total += $n;
    }
}
print $total, "\n";


#!/usr/bin/perl
#abc083_b.pl
#Wed Jan 10 20:26:19 2018
#

use warnings;
use strict;

my $temp = <STDIN>;
chomp $temp;

my @temps = split(" ",$temp);

my $n = $temps[0];
my $a = $temps[1];
my $b = $temps[2];

my $num = 1;

my $answer = 0;

while ($num <= $n){
	my @digit = split("",$num);
	my $total = 0;
	foreach (@digit){
		$total = $total + $_;
	}
	if ($total >= $a && $total <=$b){
		$answer = $answer + $num;
	}
	$num++;
}

print $answer,"\n";


use v5.18;
use warnings;
use utf8;
use List::Util qw( sum );
sub line { chomp(my $str = <>); $str; }
sub split_line { (split / /, line) }

my ($limit, $min, $max) = split_line;

my $sum = 0;
for my $num (1 .. $limit) {
  my @digits = split //, $num;
  my $dsum = sum @digits;
  if ( $dsum >= $min && $dsum <= $max ) {
    $sum += $num;
  }
}
say $sum;


Score : 100 points

Problem Statement

Snuke has a grid consisting of three squares numbered 1, 2 and 3.
In each square, either 0 or 1 is written. The number written in Square i is s_i.

Snuke will place a marble on each square that says 1.
Find the number of squares on which Snuke will place a marble.

Constraints

Each of s_1, s_2 and s_3 is either 1 or 0.

Input

Input is given from Standard Input in the following format:

s_{1}s_{2}s_{3}

Output

Print the answer.

Sample Input 1

101

Sample Output 1

2

A marble will be placed on Square 1 and 3.

Sample Input 2

000

Sample Output 2

0

No marble will be placed on any square.


$str = <stdin>;
$count = (() = $str =~ m/1/g);
print $count;


use strict;
use warnings;

chomp(my $text = <>);
my($a, $b, $c) = split //, $text;

print $a+$b+$c . "\n";


use strict;
use warnings;

my $str = <STDIN>;

my $count = 0;
for (1...3) {
    if (int($str % 10) == 1) {
        $count++;
    }
    $str /= 10;
}
print $count;


#!/usr/bin/perl
use strict;

my $input=<STDIN>;
my $output = 0;
for (my $i = 0; $i < 3; $i++){
    my $s = substr($input, $i, 1);
    if ($s == 1) {
        $output++;
    }
}
print $output."\n";


#!/usr/bin/env perl

use strict;
use warnings;

my $input = do { local $/; <> };

my @match = $input =~ /1/g;

print 0+@match;


use strict;
use warnings;

main();

sub main {
	my $in = <STDIN>;
	my $out = 0;

	$out += int($in/100);
	$in  -= 100*int($in/100);
   	$out += int($in/10);
	$in  -= 10*int($in/10);
   	$out += $in;

	printf("%d\n",$out);

	1;
}


#! /usr/bin/perl

use strict;
use warnings;

chomp(my $n = <>);
my $count = 0;
foreach my $bit (split(//, $n)) {
    $count ++ if $bit;
}
print $count, "\n";


Score : 300 points

Problem Statement

E869120 found a chest which is likely to contain treasure.

However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters.

He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?.

One more thing he found is a sheet of paper with the following facts written on it:

Condition 1: The string S contains a string T as a contiguous substring.

Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.

Print the string S.

If such a string does not exist, print UNRESTORABLE.

Constraints

1 \leq |S'|, |T| \leq 50

S' consists of lowercase English letters and ?.

T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T'

Output

Print the string S.

If such a string does not exist, print UNRESTORABLE instead.

Sample Input 1

?tc????
coder

Sample Output 1

atcoder

There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder.
Among them, the lexicographically smallest is atcoder, so we can say S = atcoder.

Sample Input 2

??p??d??
abc

Sample Output 2

UNRESTORABLE

There is no string that satisfies Condition 1, so the string S does not exist.


chomp($s=<>);
chomp($t=<>);

@s=split//,$s;
@t=split//,$t;

$len=length($s) - length($t);
$tlen=length($t);

if($len < 0){
    print "UNRESTORABLE";
    exit;
}
@ary=split//,$s;
$k=0;
while($k<=$len){
    $flg=0;
    for($i=0;$i<$tlen;$i++){
        if($s[$i+$k] ne $t[$i] && $s[$i+$k] ne "?"){
            $flg=1;
        }
    }
    if($flg==0){
        for($i=0;$i<$tlen;$i++){
            $ary[$i+$k]=$t[$i];
        }
        for($i=0;$i<length($s);$i++){
            if($ary[$i] eq "?"){
                $ary[$i] = "a";
            }
        }
        print join "",@ary;
        exit;
    }
    $k++;
}
print "UNRESTORABLE";


#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

my $line = <>; chomp $line;
my @sd = split //, $line;
$line = <>; chomp $line;
my @t = split //, $line;

my ($n, $m) = (scalar @sd, scalar @t);

unless ($m <= $n) {
  print "UNRESTORABLE\n";
  exit;
}

my @results;
NEXT: for (my $i = 0; $i < $n - $m + 1; $i++) {
  for (my $j = 0; $j < $m; $j++) {
    unless ($sd[$i + $j] eq $t[$j] || $sd[$i + $j] eq '?') {
      next NEXT;
    }
  }
  my @cand;
  for (my $k = 0; $k < $i; $k++) {
    push @cand, 'a' if $sd[$k] eq '?';
    push @cand, $sd[$k] unless $sd[$k] eq '?';
  }
  for (my $k = 0; $k < $m; $k++) {
    push @cand, $t[$k];
  }
  for (my $k = $i + $m; $k < $n; $k++) {
    push @cand, 'a' if $sd[$k] eq '?';
    push @cand, $sd[$k] unless $sd[$k] eq '?';
  }
  push @results, join('', @cand);
}

unless (scalar @results) {
  print "UNRESTORABLE\n";
  exit;
}

@results = sort @results;
print $results[0], "\n";


$|=1;
$S = <STDIN>;chomp $S;
$T = <STDIN>;chomp $T;

$RS =reverse $S;
$RT =reverse $T;

$RTT=$RT; $RTT =~ s/([a-z])/($1|\\\?)/g;

if ($RS =~ s/$RTT/$RT/ ) {
 $RS=~s/\?/a/g;
 $SS = reverse $RS;
 print $SS;
} else {
 print 'UNRESTORABLE';
}


#!/usr/bin/perl

$line = 0;
while(<>) {
    chomp;
    if ($line == 0) {
        $sd = $_;
        @sd = split //, $_;
    } else {
        $t = $_;
        @t = split //, $_;
    }
    $line++;
}

@temp = @t;
$repm = 100;
$done = 0;
$flag = 0;
$cnt = $#sd;
foreach (reverse @sd) {
    if ($flag) {
        if ($#temp < 0) {
            $done = 1;
            if ($repm > $rep) {
                $done1 = $cnt;
            }
            $flag = 0;
        } else {
            $w = pop @temp;
            if ($_ ne '?') {
                if ($_ ne $w) {
                    $flag = 0;
                }
            } else {
                $rep++;
            }
        }
    } elsif ($_ eq '?') {
        $flag = 1;
        $rep = 0;
        @temp = @t;
        pop @temp;
        $rep++;
    } elsif ($_ eq $t[-1]) {
        $flag = 2;
        @temp = @t;
        pop @temp;
    }
    $cnt--;
}

if ($#temp < 0) {
    $done = 1;
    if ($repm > $rep) {
        $done1 = $cnt;
    }
}

if ($done) {
    substr($sd, $done1 + 1, $#t + 1, $t);
    $sd =~ s/\?/a/g;
    print "$sd\n";
} else {
    print "UNRESTORABLE\n";
}


#!/usr/bin/perl

$line = 0;
while(<>) {
    chomp;
    if ($line == 0) {
        $sd = $_;
        @sd = split //, $_;
    } else {
        $t = $_;
        @t = split //, $_;
    }
    $line++;
}

$repm = 100;
$done = 0;
$flag = 0;
$cnt = $#sd;
foreach (reverse @sd) {
    if ($flag) {
        if ($#temp < 0) {
            $done = 1;
            if ($repm > $rep) {
                $repm = $rep;
                $done1 = $cnt;
            }
            $flag = 0;
        } else {
            $w = pop @temp;
            if ($_ ne '?') {
                if ($_ ne $w) {
                    $flag = 0;
                }
            } else {
                $rep++;
            }
        }
    } elsif ($_ eq '?') {
        $flag = 1;
        $rep = 0;
        @temp = @t;
        pop @temp;
        $rep++;
    }
    $cnt--;
}

if ($done) {
    substr($sd, $done1 + 1, $#t + 1, $t);
    $sd =~ s/\?/a/g;
    print "$sd\n";
} else {
    print "UNRESTORABLE\n";
}


Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.

Print the strings after the process.

Constraints

1 \leq H,W \leq 50

S_i is a string of length W consisting of # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310


($H, $W) = split / /, <>; @masu;
for (0..$H-1) {
    push @{ $masu[$_] }, split //, <>;
}

for my $h (0..$H-1) {
    for my $w (0..$W-1) {
        next if $masu[$h][$w] eq '#';

        my $bakudan = 0;
        for my $diffh (qw/-1 0 1/) {
            my $dh = $h + $diffh;
            next if ($dh < 0 || $dh > $#masu);

            my @masuh = @{ $masu[$dh] };
            for my $diffw (qw/-1 0 1/) {
                my $dw = $w + $diffw;
                next if ($dw < 0 || $dw > $#masuh);

                $bakudan++ if $masu[$dh][$dw] eq '#';
            }
        }

        $masu[$h][$w] = $bakudan;
    }
}

for (0..$#masu) {
    for my $c (@{ $masu[$_] }) {
        print $c;
    }

    print "\n";
}


Score : 200 points

Problem Statement

Joisino is working as a receptionist at a theater.

The theater has 100000 seats, numbered from 1 to 100000.

According to her memo, N groups of audiences have come so far, and the i-th group occupies the consecutive seats from Seat l_i to Seat r_i (inclusive).

How many people are sitting at the theater now?

Constraints

1≤N≤1000

1≤l_i≤r_i≤100000

No seat is occupied by more than one person.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
l_1 r_1
:
l_N r_N

Output

Print the number of people sitting at the theater.

Sample Input 1

1
24 30

Sample Output 1

7

There are 7 people, sitting at Seat 24,25,26,27,28,29 and 30.

Sample Input 2

2
6 8
3 3

Sample Output 2

4


Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Find the lexicographically (alphabetically) smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Constraints

1 \leq |S| \leq 10^5 (|S| is the length of string S.)

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the lexicographically smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Sample Input 1

atcoderregularcontest

Sample Output 1

b

The string atcoderregularcontest contains a, but does not contain b.

Sample Input 2

abcdefghijklmnopqrstuvwxyz

Sample Output 2

None

This string contains every lowercase English letter.

Sample Input 3

fajsonlslfepbjtsaayxbymeskptcumtwrmkkinjxnnucagfrg

Sample Output 3

d


say (("a".."z") (-) get.comb.unique).List.sort[0].key||"None"


$input = <>;
@inputs = split(//, $input);
@check;
for (0..26) {
    $check[$_] = 0;
}

for $i (@inputs) {
    $count = 0;
    for('a'..'z') {
        if ($i eq $_) {
            $check[$count] = 1;
            last;
        }
        $count++;
    }
    if ($al) {
        last;
    }
}

$count = 0;
for (0..26) {
    if ($check[$_] == 0) {
        $count2 = 0;
        for('a'..'z') {
            if ($count == $count2) {
                print $_;
                exit(0);
            }
            $count2++;
        }
    }
    $count++;
}
print "None";


Score : 400 points

Problem Statement

We have a sequence of length N, a = (a_1, a_2, ..., a_N).
Each a_i is a positive integer.

Snuke's objective is to permute the element in a so that the following condition is satisfied:

For each 1 ≤ i ≤ N - 1, the product of a_i and a_{i + 1} is a multiple of 4.

Determine whether Snuke can achieve his objective.

Constraints

2 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1, 100, 10).

Sample Input 2

4
1 2 3 4

Sample Output 2

No

It is impossible to permute a so that the condition is satisfied.

Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.

Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes