Score : 200 points

Problem Statement

Snuke has N integers. Among them, the smallest is A, and the largest is B.
We are interested in the sum of those N integers. How many different possible sums there are?

Constraints

1 ≤ N,A,B ≤ 10^9

A and B are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of the different possible sums.

Sample Input 1

4 4 6

Sample Output 1

5

There are five possible sums: 18=4+4+4+6, 19=4+4+5+6, 20=4+5+5+6, 21=4+5+6+6 and 22=4+6+6+6.

Sample Input 2

5 4 3

Sample Output 2

0

Sample Input 3

1 7 10

Sample Output 3

0

Sample Input 4

1 3 3

Sample Output 4

1


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

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.


Score : 200 points

Problem Statement

Snuke has decided to construct a string that starts with A and ends with Z, by taking out a substring of a string s (that is, a consecutive part of s).

Find the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of s that starts with A and ends with Z.

Constraints

1 ≦ |s| ≦ 200{,}000

s consists of uppercase English letters.

There exists a substring of s that starts with A and ends with Z.

Input

The input is given from Standard Input in the following format:

s

Output

Print the answer.

Sample Input 1

QWERTYASDFZXCV

Sample Output 1

5

By taking out the seventh through eleventh characters, it is possible to construct ASDFZ, which starts with A and ends with Z.

Sample Input 2

ZABCZ

Sample Output 2

4

Sample Input 3

HASFJGHOGAKZZFEGA

Sample Output 3

12


chomp($line = <>);
printf rindex($line, 'Z') - index($line, 'A') + 1 . "\n";


use strict;
use warnings;

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

sub solver {
    my ($s) = @_;
    $s =~ s/^[^A]*//;
    $s =~ s/[^Z]*$//;
    return length($s);
}


my $s = get;
my $l = $s.index('A');
my $r = $s.rindex('Z');
say $r - $l + 1;


my $s = get;
my $l = $s.index('a');
my $r = $s.rindex('z');
say $s.comb.[$l .. $r].join;


#!/usr/bin/env perl

use strict;
use warnings;

my $s = <>;
chomp $s;

my $found_a = -1;
my $found_z = -1;
my $cnt = 0;

for my $c (split //, $s) {
    if ($c eq 'A' && $found_a == -1) {
        $found_a = $cnt;
    }
    if ($c eq 'Z') {
        $found_z = $cnt;
    }
    $cnt++;
}

print $found_z - $found_a + 1, "\n";


use strict;
use utf8;
use warnings;

my $s = <STDIN>;
my $a_inx = index($s, "A");
my $z_inx = rindex($s, "Z");
my $ans = $z_inx - $a_inx + 1;
print "$ans\n";


use strict;
use warnings;

my $s = <>;
chomp $s;
my @str = split //, $s;
my ($a, $z) = (0, 0);

for(my $i = 0; $i < $#str; $i++) {
	if($str[$i] eq 'A') {
		$a = $i;
		last;
	}
}
for(my $i = $#str; $i > 0; $i--) {
	if($str[$i] eq 'Z') {
		$z = $i;
		last;
	}
}
$z++;
print $z - $a . "\n";
exit;


Score : 200 points

Problem Statement

You are given two integers K and S.

Three variable X, Y and Z takes integer values satisfying 0≤X,Y,Z≤K.

How many different assignments of values to X, Y and Z are there such that X + Y + Z = S?

Constraints

2≤K≤2500

0≤S≤3K

K and S are integers.

Input

The input is given from Standard Input in the following format:

K S

Output

Print the number of the triples of X, Y and Z that satisfy the condition.

Sample Input 1

2 2

Sample Output 1

6

There are six triples of X, Y and Z that satisfy the condition:

X = 0, Y = 0, Z = 2

X = 0, Y = 2, Z = 0

X = 2, Y = 0, Z = 0

X = 0, Y = 1, Z = 1

X = 1, Y = 0, Z = 1

X = 1, Y = 1, Z = 0

Sample Input 2

5 15

Sample Output 2

1

The maximum value of X + Y + Z is 15, achieved by one triple of X, Y and Z.


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

my ( $K, $S ) = split " ", <>;

my $count = 0;

for my $x (0..$K) {
    for my $y (0..$K) {
        my $z = $S - $x - $y;
        $count++ if ( ($z >= 0) && ($z <= $K) );
    }
}
print $count;


#! usr/bin/perl

use strict;
use warnings;

sub max {
    my ($a,$b)=@_;
    $a>$b ? $a : $b;
}

chomp(my $line = <STDIN>);
my ($K,$S) = split ' ',$line;

my $count=0;
foreach my$x(0..$S/3) {
    foreach my$y(max($x,$S-$x-$K)..($S-$x)/2) {
        my $z = $S-$x-$y;
        if (($x==$y)&&($y==$z)) {$count += 1;next};
        if (($x==$y)||($y==$z)) {$count += 3;next};
        $count += 6;
    }
}

print "$count\n"


#! usr/bin/perl

use strict;
use warnings;

chomp(my $line = <STDIN>);
my ($K,$S) = split ' ',$line;
my $count = 0;

foreach my $x (0..$K) {
    foreach my $y (0 .. $K) {
        foreach my $z (0..$K){
            if ($x + $y + $z == $S) {
                ++$count;
            }
        }
    }
}

print "$count\n";


@T = map {scalar reverse $_} ("dream","dreamer","erase","eraser");
$s = <>;
chomp($s);
$s = reverse($s);
$l = length($s);
for ($c=0;$c<$l;){
    $k = -1;
    for ($i=0;$i<4;$i++){
        if (substr($s,$c,length($T[$i])) eq $T[$i]){
            $k = length($T[$i]);
            last;
        }
    }
    if ($k<0){
        print "NO";
        exit;
    }
    $c += $k;
}
print "YES"

Score : 300 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Another string T is initially empty.
Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:

Append one of the following at the end of T: dream, dreamer, erase and eraser.

Constraints

1≦|S|≦10^5

S consists of lowercase English letters.

Input

The input is given from Standard Input in the following format:

S

Output

If it is possible to obtain S = T, print YES. Otherwise, print NO.

Sample Input 1

erasedream

Sample Output 1

YES

Append erase and dream at the end of T in this order, to obtain S = T.

Sample Input 2

dreameraser

Sample Output 2

YES

Append dream and eraser at the end of T in this order, to obtain S = T.

Sample Input 3

dreamerer

Sample Output 3

NO


use strict;
use warnings;

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

print "YES\n" if $s =~ /^(dream|dreamer|erase|eraser)*$/;
print "NO\n" unless $s =~ /^(dream|dreamer|erase|eraser)*$/;


#!/usr/bin/perl

use strict;
use warnings;

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

if($s =~ /^(dream|dreamer|erase|eraser)*$/) {
    print "YES\n";
}else{
    print "NO\n";
}


use strict;
use warnings;

main();

sub main{
	my $input = <stdin>;
	chomp($input);

	while(1){
		my $len = length($input);
		# printf("%d : %s\n", $len, $input);

		if(rindex($input,'dreamer') == $len-7){
			$input = substr($input, 0, -7);
		}
		elsif(rindex($input,'dream') == $len-5){
			$input = substr($input, 0, -5);
		}
		elsif(rindex($input,'eraser') == $len-6){
			$input = substr($input, 0, -6);
		}
		elsif(rindex($input,'erase') == $len-5){
			$input = substr($input, 0, -5);
		}
		else{
			print "NO\n";
			return;
		}

		if(length($input) == 0){
			print "YES\n";
			return;
		}
	}
}
1;


@T=map {scalar reverse $_} ("dream","dreamer","erase","eraser");
$s=<>;
chomp($s);
$s=reverse($s);
$l=length($s);
for($c=0;$c<$l;){
	$k=-1;
	for($i=0;$i<4;$i++){
		if(substr($s,$c,length($T[$i])) eq $T[$i]){
			$k=length($T[$i]);
			last;
		}
	}
	if($k<0){
		print'NO';
		exit
	}
	$c+=$k;
}
print'YES'


use v6;
my $s = get().chomp;
while ($s ne "") {
	if ($s ~~ m/dreamer$$|dream$$|eraser$$|erase$$/) {
		$s ~~ s/dreamer$$|dream$$|eraser$$|erase$$//;
	} else {
		last;
	}
}
if ($s eq "") {
	say "YES";
} else {
	say "NO";
}
exit;


use strict;
use warnings;

my $in = <STDIN>;

chomp($in);

while ($in =~ m/(dream(er)??$)|(erase(r)??$)/){
    $in = $`;
}

print $in eq '' ? "YES\n" : "NO\n";


use strict;

my $s = get_array_from_stdin();

while($s) {
  if ($s eq "") {
    print "YES\n";
    exit;
  }

  if ($s =~ s/^eraser?//) {
    redo;
  }

  if ($s =~ /^dream/) {
    if ($s =~ /^dreamer/) {
      if ($s =~ s/^dreameraser?//) {
      } else {
        $s =~ s/^dreamer//;
      }
      redo;
    } else {
      $s =~ s/^dream//;
      redo;
    }
  }

  print "NO\n";
  exit;
}

### common sub routine ###

sub get_array_from_stdin {
  my $input = <STDIN>;
  chomp($input);
  if (wantarray) {
    return split /\s+/, $input;
  } else {
    return $input;
  }
}


use strict;

my $s = get_array_from_stdin();

my $ssub;
while($s) {
  if ($s =~ s/(.+)d/d/) {
    $ssub = $1;
  } else {
    $ssub = $s;
    $s = "";
  }

  if (check($ssub)) {
  } else {
    print "NO\n";
    exit;
  }
}

#if (check($ssub)) {
  print "YES\n";
#} else {
#  print "NO\n";
#}

# return 1 if true;
sub check {
  my $s = shift;

  return 1 if $s eq "dream";
  return 1 if $s eq "dreamer";
  return 1 if $s eq "erase";
  return 1 if $s eq "eraser";

  if ($s =~ /^e/) {
    if ($s =~ /^eraser?(.+)/) {
      return check($1);
    }
    return 0;
  }

  unless ($s =~ /^d/) {
    return 0;
  }

  # start with "d"
  return 0 unless $s =~ /^dream/;

  # start with "dream"

  # まず dream でとってみる
  $s =~ /^dream(.+)/;
  if (check($1)) {
    return 1;
  }

  # dream では取れなかったので、dreamer で取れるか試す
  if ($s =~ /^dreamer(.+)/) {
    return check($1);
  }

  return 0;
}

### common sub routine ###

sub get_array_from_stdin {
  my $input = <STDIN>;
  chomp($input);
  if (wantarray) {
    return split /\s+/, $input;
  } else {
    return $input;
  }
}


use strict;

my $s = get_array_from_stdin();

if (check($s)) {
  print "YES\n";
} else {
  print "NO\n";
}

# return 1 if true;
sub check {
  my $s = shift;

  return 1 if $s eq "dream";
  return 1 if $s eq "dreamer";
  return 1 if $s eq "erase";
  return 1 if $s eq "eraser";

  if ($s =~ /^e/) {
    if ($s =~ /^eraser?(.+)/) {
      return check($1);
    }
    return 0;
  }

  unless ($s =~ /^d/) {
    return 0;
  }

  # start with "d"
  return 0 unless $s =~ /^dream/;

  # start with "dream"

  # まず dream でとってみる
  $s =~ /^dream(.+)/;
  if (check($1)) {
    return 1;
  }

  # dream では取れなかったので、dreamer で取れるか試す
  if ($s =~ /^dreamer(.+)/) {
    return check($1);
  }

  return 0;
}

### common sub routine ###

sub get_array_from_stdin {
  my $input = <STDIN>;
  chomp($input);
  if (wantarray) {
    return split /\s+/, $input;
  } else {
    return $input;
  }
}


chomp($s=<>);
while($s =~ s/dream$|dreamer$|erase$|eraser$//){1;}
print $s eq ''? 'YES':'NO';


use strict;
use warnings;

my $buf = <>;
chomp $buf;

$buf =~ s/eraser//g;
$buf =~ s/erase//g;
$buf =~ s/dreamer//g;
$buf =~ s/dream//g;
if($buf eq ""){
	print "YES\n";
}else{
	print "NO\n";
}


chomp (my $s = <STDIN>);
$s =~ s/W+/W/g;
$s =~ s/B+/B/g;
print length($s) - 1, "\n";

Score : 300 points

Problem Statement

Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.

In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.

Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.

Constraints

1 ≦ |S| ≦ 10^5

Each character in S is B or W.

Input

The input is given from Standard Input in the following format:

S

Output

Print the minimum number of new stones that Jiro needs to place for his purpose.

Sample Input 1

BBBWW

Sample Output 1

1

By placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.

In either way, Jiro's purpose can be achieved by placing one stone.

Sample Input 2

WWWWWW

Sample Output 2

0

If all stones are already of the same color, no new stone is necessary.

Sample Input 3

WBWBWBWBWB

Sample Output 3

9


my $bw=<>; chomp$bw;my$l1=length($bw);
my$counter=0;
$counter++ while($bw=~m/(BW)/g);$counter++ while($bw=~m/(WB)/g);
print $counter;


my $bw=<>; chomp$bw;my$l1=length($bw);
$bw=~s/(BW|WB)//g;
my$l2=length($bw);
my $answer =($l1-$l2)/2;
print $answer;


my $bw=<>; chomp$bw;my$l1=length($bw);
$bw=~s/(BW|WB)//g;
my$l2=length($bw);
print ($l1-$l2)/2;


my $bw=<>; chomp$bw;
$bw=~s/([BW])\1*/0/g;
print length($bw)-1;


my $bw=<>; chomp$bw;
$bw=~s/([BW])\1*/0/g;
print length($bw)-1;
print $bw;


my $bw=<>; chomp$bw;$s=length($bw);
$bw=~s/([BW])\1*/0/g;
print length($bw)-1;


$s = <>;
$val =substr($s,0,1);
$count=0;
for($i=0;$i<length($s);$i++){
	if($val ne substr($s,$i,1)){
    	$count++;
        $val=substr($s,$i,1);
    }
}
print $count;


#include <bits/stdc++.h>
using namespace std;

int main() {
    string S;
    cin >> S;
    int count = 0;
    char now = S.at(0);
    for(int i = 0; i < S.size(); i++) {
        if(S.at(i) != now) {
            now = S.at(i);
            count++;
        }
    }
    cout << count << endl;
}


#!/usr/bin/perl

while (<>) {
    chomp;
    @s = split //, $_;
    last;
}

$cnt = -1;
foreach (@s) {
    if ($pre ne $_) {
        $pre = $_;
        $cnt++;
    }
}
print "$cnt\n";


my ($N, $L) = split " ", <>;
my @word;
for (1..$N) {
    $word[$_] = <>;
    chomp $word[$_];
}

@word = sort {$a cmp $b} @word;
print @word;

Score : 200 points

Problem Statement

Iroha has a sequence of N strings S_1, S_2, ..., S_N. The length of each string is L.

She will concatenate all of the strings in some order, to produce a long string.

Among all strings that she can produce in this way, find the lexicographically smallest one.

Here, a string s=s_1s_2s_3...s_n is lexicographically smaller than another string t=t_1t_2t_3...t_m if and only if one of the following holds:

There exists an index i(1≦i≦min(n,m)), such that s_j = t_j for all indices j(1≦j<i), and s_i<t_i.

s_i = t_i for all integers i(1≦i≦min(n,m)), and n<m.

Constraints

1 ≦ N, L ≦ 100

For each i, the length of S_i equals L.

For each i, S_i consists of lowercase letters.

Input

The input is given from Standard Input in the following format:

N L
S_1
S_2
:
S_N

Output

Print the lexicographically smallest string that Iroha can produce.

Sample Input 1

3 3
dxx
axx
cxx

Sample Output 1

axxcxxdxx

The following order should be used: axx, cxx, dxx.


($n,$l)=glob<>;
push(@s,glob<>) for(1..$n);
chomp(@s);
@s=sort{$a cmp $b}@s;
print @s;


#!perl
use strict;
use warnings;
use utf8;
use feature qw/say/;

my $N_L = <STDIN>;
my ($N, $L) = split(' ', $N_L);
my @str;
for (1..$N) {
    my $tmp = <STDIN>;
    chomp $tmp;
    push @str, $tmp;
}
say join ('', sort { $a cmp $b } @str);


#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use feature qw/say/;

my ($N, $L) = split(/\s+/, <STDIN>);
my @S = map { <STDIN> } (1..$N);
chomp $_ for @S;
say join('', sort { $a cmp $b } @S);


my ($num,$len) = split(/ /,<STDIN>);

my @input;
for(my $i = 0; $i < $num; ++$i){
    push(@input,<STDIN>);
}
chomp(@input);
print &min_str(@input);

sub min_str{
	my @ary = @_;
	@ary = sort {$a cmp $b} @ary;
	my $ret_str;
	foreach $s (@ary){
		$ret_str .= $s;
	}
	return $ret_str;
}


use v6;

my ($n, $l) = get().chomp.split(' ');
my @str;
loop (my $i = 0; $i < $n; $i++) {
	@str[$i] = get().chomp;
}
@str = @str.sort;
for @str -> $key {
	print $key;
}
print "\n";

exit;


#!/usr/bin/perl

$line = 0;
while (<>) {
    chomp;
    if ($line == 0) {
        ($n, $l) = split /\s/, $_;
    } else {
        chomp;
        push @l, $_;
    }
    $line++;
}

foreach (sort {$a cmp $b} @l) {
    print "$_";
}
print "\n";