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

Score : 100 points

Problem Statement

You are given an integer K.
Print the string obtained by repeating the string ACL K times and concatenating them.

For example, if K = 3, print ACLACLACL.

Constraints

1 \leq K \leq 5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the string obtained by repeating the string ACL K times and concatenating them.

Sample Input 1

3

Sample Output 1

ACLACLACL

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.


#!/usr/bin/env perl

use strict;
use warnings;

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

my $found = 0;

for ( my $i = 1; $i <= $count; $i++ ) {
  my $result = <STDIN>; chomp($result);
  my ($A, $B) = split qr(\s+), $result;

  if ( $A != $B ) {
    $found = 0;
    next;
  }

  $found++;

  if ( $found >= 3 ) {
    last;
  }
}

print ((($found >= 3) ? "Yes" : "No"), "\n");

Score : 200 points

Problem Statement

Tak performed the following action N times: rolling two dice.
The result of the i-th roll is D_{i,1} and D_{i,2}.

Check if doublets occurred at least three times in a row.
Specifically, check if there exists at lease one i such that D_{i,1}=D_{i,2}, D_{i+1,1}=D_{i+1,2} and D_{i+2,1}=D_{i+2,2} hold.

Constraints

3 \leq N \leq 100

1\leq D_{i,j} \leq 6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
D_{1,1} D_{1,2}
\vdots
D_{N,1} D_{N,2}

Output

Print Yes if doublets occurred at least three times in a row. Print No otherwise.

Sample Input 1

5
1 2
6 6
4 4
3 3
3 2

Sample Output 1

Yes

From the second roll to the fourth roll, three doublets occurred in a row.

Sample Input 2

5
1 1
2 2
3 4
5 5
6 6

Sample Output 2

No

Sample Input 3

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

Sample Output 3

Yes


Score : 400 points

Problem Statement

There are N cells arranged in a row, numbered 1, 2, \ldots, N from left to right.

Tak lives in these cells and is currently on Cell 1. He is trying to reach Cell N by using the procedure described below.

You are given an integer K that is less than or equal to 10, and K non-intersecting segments [L_1, R_1], [L_2, R_2], \ldots, [L_K, R_K].
Let S be the union of these K segments.
Here, the segment [l, r] denotes the set consisting of all integers i that satisfy l \leq i \leq r.

When you are on Cell i, pick an integer d from S and move to Cell i + d. You cannot move out of the cells.

To help Tak, find the number of ways to go to Cell N, modulo 998244353.

Constraints

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

1 \leq K \leq \min(N, 10)

1 \leq L_i \leq R_i \leq N

[L_i, R_i] and [L_j, R_j] do not intersect (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
L_1 R_1
L_2 R_2
:
L_K R_K

Output

Print the number of ways for Tak to go from Cell 1 to Cell N, modulo 998244353.

Sample Input 1

5 2
1 1
3 4

Sample Output 1

4

The set S is the union of the segment [1, 1] and the segment [3, 4], therefore S = \{ 1, 3, 4 \} holds.

There are 4 possible ways to get to Cell 5:

1 \to 2 \to 3 \to 4 \to 5,

1 \to 2 \to 5,

1 \to 4 \to 5 and

1 \to 5.

Sample Input 2

5 2
3 3
5 5

Sample Output 2

0

Because S = \{ 3, 5 \} holds, you cannot reach to Cell 5.
Print 0.

Sample Input 3

5 1
1 2

Sample Output 3

5

Sample Input 4

60 3
5 8
1 3
10 15

Sample Output 4

221823067

Note that you have to print the answer modulo 998244353.


#!/usr/bin/env perl

use strict;
use warnings;

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

print $num == 0 ? "1" : "0" ;

Score : 100 points

Problem Statement

Given is an integer x that is greater than or equal to 0, and less than or equal to 1.
Output 1 if x is equal to 0, or 0 if x is equal to 1.

Constraints

0 \leq x \leq 1

x is an integer

Input

Input is given from Standard Input in the following format:

x

Output

Print 1 if x is equal to 0, or 0 if x is equal to 1.

Sample Input 1

1

Sample Output 1

0

Sample Input 2

0

Sample Output 2

1


my $N = <>;chomp $s;

if($N==1){
    print "0";exit;
}

my $mod = 10**9+7;

my $count =0;
my $non=8;
my $zero=1;
my $nine=1;

for($i=1;$i<$N;$i++){
    $count = ($count *10 + $zero + $nine)%$mod;
    $zero = ($zero*9 + $non)%$mod;
    $nine = ($nine*9 + $non)%$mod;
    $non  = ($non *8)%$mod;
}

print "$count";

Score : 300 points

Problem Statement

How many integer sequences A_1,A_2,\ldots,A_N of length N satisfy all of the following conditions?

0 \leq A_i \leq 9

There exists some i such that A_i=0 holds.

There exists some i such that A_i=9 holds.

The answer can be very large, so output it modulo 10^9 + 7.

Constraints

1 \leq N \leq 10^6

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer modulo 10^9 + 7.

Sample Input 1

2

Sample Output 1

2

Two sequences \{0,9\} and \{9,0\} satisfy all conditions.

Sample Input 2

1

Sample Output 2

0

Sample Input 3

869121

Sample Output 3

2511445


Score : 600 points

Problem Statement

Given are two sequences A and B, both of length N.
A and B are each sorted in the ascending order.
Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it.

Constraints

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

1\leq A_i,B_i \leq N

A and B are each sorted in the ascending order.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N

Output

If there exist no reorderings that satisfy the condition, print No.

If there exists a reordering that satisfies the condition, print Yes on the first line.
After that, print a reordering of B on the second line, separating terms with a whitespace.

If there are multiple reorderings that satisfy the condition, you can print any of them.

Sample Input 1

6
1 1 1 2 2 3
1 1 1 2 2 3

Sample Output 1

Yes
2 2 3 1 1 1

Sample Input 2

3
1 1 2
1 1 3

Sample Output 2

No

Sample Input 3

4
1 1 2 3
1 2 3 3

Sample Output 3

Yes
3 3 1 2


package FenwickTree;
sub new{
	return bless[(0)x$_[1]],$_[0];
}
sub add{
	my$self=shift;
	my($p,$x)=@_;
	$p++;
	while($p<=@{$self}){
		$self->[$p-1]+=$x;
		$p+=$p&-$p;
	}
}
sub sum{
	my$self=shift;
	return$self->prefixsum($_[1])-$self->prefixsum($_[0]);
}
sub prefixsum{
	my$self=shift;
	my$r=shift;
	my$s=0;
	while($r>0){
		$s+=$self->[$r-1];
		$r-=$r&-$r;
	}
	return$s;
}
package main;
my($N,$Q)=split$",<>;
my$fenwicktree=FenwickTree->new($N);
my@a=split$",<>;
for$i(0..$#a){
	$fenwicktree->add($i,$a[$i]);
}
for(1..$Q){
	my($t,$a,$b)=split$",<>;
	if($t==0){
		$fenwicktree->add($a,$b);
	}
	else{
		print$fenwicktree->sum($a,$b),$/;
	}
}

Score : 100 points

Problem Statement

You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types.

0 p x: a_p \gets a_p + x

1 l r: Print \sum_{i = l}^{r - 1}{a_i}.

Constraints

1 \leq N, Q \leq 500,000

0 \leq a_i, x \leq 10^9

0 \leq p < N

0 \leq l_i < r_i \leq N

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N Q
a_0 a_1 ... a_{N - 1}
\textrm{Query}_0
\textrm{Query}_1
:
\textrm{Query}_{Q - 1}

Output

For each query of the latter type, print the answer.

Sample Input 1

5 5
1 2 3 4 5
1 0 5
1 2 4
0 3 10
1 0 5
1 0 3

Sample Output 1

15
7
25
6


use strict;
use warnings;
package Convolution;
use integer;
sub MOD(){998244353};
sub primitive_root(){3};
sub bsf($){
	my$x=shift;
	my$c=0;
	$c++until$x>>$c&1;
	return$c;
}
sub powermod($$){
	my($v,$w)=@_;
	my$r=1;
	$v%=MOD;
	while($w>0){
		$r=$r*$v%MOD if$w&1;
		$v=$v*$v%MOD;
		$w>>=1;
	}
	return$r;
}

my(@es,@ies,@sum_e,@sum_ie);
{
	my$cnt2=bsf(MOD-1);
	my$e=powermod(primitive_root,MOD-1>>$cnt2);
	my$ie=powermod($e,MOD-2);
	for(my$i=$cnt2;$i>=2;$i--){
		$es[$i-2]=$e;
		$ies[$i-2]=$ie;
		$e=$e*$e%MOD;
		$ie=$ie*$ie%MOD;
	}
	my$now=1;
	my$inow=1;
	for(0..$cnt2-3){
		$sum_e[$_]=$es[$_]*$now%MOD;
		$now=$now*$ies[$_]%MOD;
		$sum_ie[$_]=$ies[$_]*$inow%MOD;
		$inow=$inow*$es[$_]%MOD;
	}
}

sub butterfly($@){
	my$h=shift;
	my@a=@_;
	for my$ph(1..$h){
		my$w=1<<$ph-1;
		my$p=1<<$h-$ph;
		my$now=1;
		for my$s(0..$w-1){
			my$offset=$s<<$h-$ph+1;
			for($offset..$offset+$p-1){
				my$l=$a[$_];
				my$r=$a[$_+$p]*$now%MOD;
				$a[$_]=($l+$r)%MOD;
				$a[$_+$p]=($l-$r+MOD)%MOD;
			}
			$now=$now*$sum_e[bsf(~$s)]%MOD;
		}
	}
	return@a;
}
sub butterfly_inv($@){
	my$h=shift;
	my@a=@_;
	for(my$ph=$h;$ph>=1;$ph--){
		my$w=1<<$ph-1;
		my$p=1<<$h-$ph;
		my$inow=1;
		for my$s(0..$w-1){
			my$offset=$s<<$h-$ph+1;
			for($offset..$offset+$p-1){
				my$l=$a[$_];
				my$r=$a[$_+$p];
				$a[$_]=($l+$r)%MOD;
				$a[$_+$p]=($l-$r+MOD)*$inow%MOD;
			}
			$inow=$inow*$sum_ie[bsf(~$s)]%MOD;
		}
	}
	return@a;
}

sub convolution(\@\@){
	my@a=@{$_[0]};
	my@b=@{$_[1]};
	my$n=@a;
	my$m=@b;
	return()if$n==0||$m==0;
	my$h=0;
	$h++while$n+$m-1>1<<$h;
	my$z=1<<$h;
	$#a=$z-1;
	@a=butterfly($h,@a);
	$#b=$z-1;
	@b=butterfly($h,@b);
	$a[$_]=$a[$_]*$b[$_]%MOD for 0..$z-1;
	@a=butterfly_inv($h,@a);
	$#a=$n+$m-2;
	my$iz=powermod($z,MOD-2);
	$_=$_*$iz%MOD for@a;
	return@a;
}
no integer;
package main;
my($N,$M)=split$",<>;
my@a=split$",<>;
my@b=split$",<>;
print$_,$/for Convolution::convolution(@a,@b);

Score : 100 points

Problem Statement

You are given two integer arrays a_0, a_1, ..., a_{N - 1} and b_0, b_1, ..., b_{M - 1}. Calculate the array c_0, c_1, ..., c_{(N - 1) + (M - 1)}, defined by c_i = \sum_{j = 0}^i a_j b_{i - j} \bmod 998244353.

Constraints

1 \leq N, M \leq 524288

0 \leq a_i, b_i < 998244353

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N M
a_0 a_1 ... a_{N-1}
b_0 b_1 ... b_{M-1}

Output

Print the answer in the following format:

c_0 c_1 ... c_{(N - 1) + (M - 1)}

Sample Input 1

4 5
1 2 3 4
5 6 7 8 9

Sample Output 1

5 16 34 60 70 70 59 36

Sample Input 2

1 1
10000000
10000000

Sample Output 2

871938225


use strict;
use warnings;
package Scc;
sub new{
	return bless[map[],1..$_[1]],$_[0];
}
sub add_edge{
	my$self=shift;
	push@{$self->[$_[0]]},$_[1];
}
sub scc{
	my$self=shift;
	my$now_ord=0;
	my$group_num=0;
	my@low;
	my@ord=(-1)x@{$self};
	my@ids;
	my@visited;
	my$dfs=sub{
		my$v=shift;
		$low[$v]=$ord[$v]=$now_ord++;
		push@visited,$v;
		for my$to(@{$self->[$v]}){
			if($ord[$to]==-1){
				__SUB__->($to);
				$low[$v]=$low[$to]if$low[$v]>$low[$to];
			}
			else{
				$low[$v]=$ord[$to]if$low[$v]>$ord[$to];
			}
		}
		if($low[$v]==$ord[$v]){
			my$u=-1;
			while($u!=$v){
				$u=pop@visited;
				$ord[$u]=@{$self};
				$ids[$u]=$group_num;
			}
			$group_num++;
		}
	};
	for my$i(0..$#ord){
		$dfs->($i)if$ord[$i]==-1;
	}
	$_=$group_num-1-$_ for@ids;
	my@groups=map[],1..$group_num;
	for my$i(0..$#ids){
		push@{$groups[$ids[$i]]},$i;
	}
	return\@groups;
}
package main;
my($N,$M)=split$",<>;
my$scc=Scc->new($N);
for(1..$M){
	my($a,$b)=split$",<>;
	$scc->add_edge($a,$b);
}
my$groups=$scc->scc();
print scalar(@{$groups}),$/;
for my$L(@{$groups}){
	print scalar(@{$L}),$","@{$L}",$/;
}

Score : 100 points

Problem Statement

You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i.
Divide this graph into strongly connected components and print them in their topological order.

Constraints

1 \leq N \leq 500,000

1 \leq M \leq 500,000

0 \leq a_i, b_i < N

Input

Input is given from Standard Input in the following format:

N M
a_0 b_0
a_1 b_1
:
a_{M - 1} b_{M - 1}

Output

Print 1+K lines, where K is the number of strongly connected components.
Print K on the first line.
Print the information of each strongly connected component in next K lines in the following format, where l is the number of vertices in the strongly connected component and v_i is the index of the vertex in it.

l v_0 v_1 ... v_{l-1}

Here, for each edge (a_i, b_i), b_i should not appear in earlier line than a_i.

If there are multiple correct output, print any of them.

Sample Input 1

6 7
1 4
5 2
3 0
5 5
4 1
0 3
4 2

Sample Output 1

4
1 5
2 4 1
1 2
2 3 0


package scc;
sub new{
	return bless[map[],1..$_[1]],$_[0];
}
sub add_edge{
	my$self=shift;
	push@{$self->[$_[0]]},$_[1];
}
sub scc{
	my$self=shift;
	my$now_ord=0,$group_num=0,@low,@ord=(-1)x@{$self},@ids;
	my@visited;
	my$dfs;
	$dfs=sub{
		my$v=shift;
		$low[$v]=$ord[$v]=$now_ord++;
		push@visited,$v;
		for$to(@{$self->[$v]}){
			if($ord[$to]==-1){
				$dfs->($to);
				$low[$v]=$low[$to]if$low[$v]>$low[$to];
			}
			else{
				$low[$v]=$ord[$to]if$low[$v]>$ord[$to];
			}
		}
		if($low[$v]==$ord[$v]){
			my$u=-1;
			while($u!=$v){
				$u=pop@visited;
				$ord[$u]=@{$self};
				$ids[$u]=$group_num;
			}
			$group_num++;
		}
	};
	for$i(0..$#ord){
		$dfs->($i)if$ord[$i]==-1;
	}
	$_=$group_num-1-$_ for@ids;
	my@groups=map[],1..$group_num;
	for$i(0..$#ids){
		push@{$groups[$ids[$i]]},$i;
	}
	return\@groups;
}
package main;
my($N,$M)=split$",<>;
my$scc=scc->new($N);
for(1..$M){
	my($a,$b)=split$",<>;
	$scc->add_edge($a,$b);
}
$groups=$scc->scc();
print scalar(@{$groups}),$/;
for$L(@{$groups}){
	print scalar(@{$L}),$","@{$L}",$/;
}


package scc;
sub new{
	return bless[map[],1..$_[1]],$_[0];
}
sub add_edge{
	my$self=shift;
	push@{$self->[$_[0]]},$_[1];
}
sub scc{
	my$self=shift;
	my$now_ord=0,$group_num=0,@low,@ord=(-1)x@{$self},@ids;
	my@visited;
	my$dfs;
	$dfs=sub{
		my$v=shift;
		$low[$v]=$ord[$v]=$now_ord++;
		push@visited,$v;
		for$to(@{$self->[$v]}){
			if($ord[$to]==-1){
				$dfs->($to);
				$low[$v]=$low[$to]if$low[$v]>$low[$to];
			}
			else{
				$low[$v]=$ord[$to]if$low[$v]>$ord[$to];
			}
		}
		if($low[$v]==$ord[$v]){
			my$u=-1;
			while($u!=$v){
				$u=pop@visited;
				$ord[$u]=@{$self};
				$ids[$u]=$group_num;
			}
			$group_num++;
		}
	};
	for$i(0..$#ord){
		$dfs->($i)if$ord[$i]==-1;
	}
	$_=$group_num-1-$_ for@ids;
	@groups=map[],1..$group_num;
	for$i(0..$#ids){
		push@{$groups[$ids[$i]]},$i;
	}
	return\@groups;
}
package main;
my($N,$M)=split$",<>;
my$scc=scc->new($N);
for(1..$M){
	my($a,$b)=split$",<>;
	$scc->add_edge($a,$b);
}
$groups=$scc->scc();
print scalar(@{$groups}),$/;
for$L(@{$groups}){
	print scalar(@{$L}),$","@{$L}",$/;
}


package scc;
sub new{
	return bless[map[],1..$_[1]],$_[0];
}
sub add_edge{
	my$self=shift;
	push@{$self->[$_[0]]},$_[1];
}
sub scc{
	my$self=shift;
	my$now_ord=0,$group_num=0,@low=(0)x@{$self},@ord=(-1)x@{$self},@ids=(0)x@{$self};
	my@visited;
	my$dfs;
	$dfs=sub{
		my$v=shift;
		$low[$v]=$ord[$v]=$now_ord++;
		push@visited,$v;
		for$to(@{$self->[$v]}){
			if($ord[$to]==-1){
				$dfs->($to);
				$low[$v]=$low[$to]if$low[$v]>$low[$to];
			}
			else{
				$low[$v]=$ord[$to]if$low[$v]>$ord[$to];
			}
		}
		if($low[$v]==$ord[$v]){
			my$u=-1;
			while($u!=$v){
				$u=pop@visited;
				$ord[$u]=@{$self};
				$ids[$u]=$group_num;
			}
			$group_num++;
		}
	};
	for$i(0..$#ord){
		$dfs->($i)if$ord[$i]==-1;
	}
	$_=$group_num-1-$_ for@ids;
	@groups=map[],1..$group_num;
	for$i(0..$#ids){
		push@{$groups[$ids[$i]]},$i;
	}
	return\@groups;
}
package main;
my($N,$M)=split$",<>;
my$scc=scc->new($N);
for(1..$M){
	my($a,$b)=split$",<>;
	$scc->add_edge($a,$b);
}
$groups=$scc->scc();
print scalar(@{$groups}),$/;
for$L(@{$groups}){
	print scalar(@{$L}),$","@{$L}",$/;
}


Score : 100 points

Problem Statement

You are given a string of length N. Calculate the number of distinct substrings of S.

Constraints

1 \leq N \leq 500,000

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.

Sample Input 1

abcbcba

Sample Output 1

21

Sample Input 2

mississippi

Sample Output 2

53

Sample Input 3

ababacaca

Sample Output 3

33

Sample Input 4

aaaaa

Sample Output 4

5


use strict;
my ($d,$s,$t)=glob<>;
if($d<($s*$t)){print "Yes";}else{print"No";}

Score : 100 points

Problem Statement

Takahashi is meeting up with Aoki.

They have planned to meet at a place that is D meters away from Takahashi's house in T minutes from now.

Takahashi will leave his house now and go straight to the place at a speed of S meters per minute.

Will he arrive in time?

Constraints

1 \leq D \leq 10000

1 \leq T \leq 10000

1 \leq S \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

D T S

Output

If Takahashi will reach the place in time, print Yes; otherwise, print No.

Sample Input 1

1000 15 80

Sample Output 1

Yes

It takes 12.5 minutes to go 1000 meters to the place at a speed of 80 meters per minute. They have planned to meet in 15 minutes so he will arrive in time.

Sample Input 2

2000 20 100

Sample Output 2

Yes

It takes 20 minutes to go 2000 meters to the place at a speed of 100 meters per minute. They have planned to meet in 20 minutes so he will arrive just on time.

Sample Input 3

10000 1 1

Sample Output 3

No

He will be late.


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
const ll INF=1e18;

int main() {
	int a,b,c;
	cin >> a >> b >> c;
	if(a <= b * c){
		cout << "Yes" <<endl;
	}
	else{
		cout <<"No" <<endl;
	}

}


Score : 200 points

Problem Statement

Given are two strings S and T.

Let us change some of the characters in S so that T will be a substring of S.

At least how many characters do we need to change?

Here, a substring is a consecutive subsequence. For example, xxx is a substring of yxxxy, but not a substring of xxyxx.

Constraints

The lengths of S and T are each at least 1 and at most 1000.

The length of T is at most that of S.

S and T consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the minimum number of characters in S that need to be changed.

Sample Input 1

cabacc
abc

Sample Output 1

1

For example, changing the fourth character a in S to c will match the second through fourth characters in S to T.

Since S itself does not have T as its substring, this number of changes - one - is the minimum needed.

Sample Input 2

codeforces
atcoder

Sample Output 2

6


Score : 200 points

Problem Statement

An integer N is a multiple of 9 if and only if the sum of the digits in the decimal representation of N is a multiple of 9.

Determine whether N is a multiple of 9.

Constraints

0 \leq N < 10^{200000}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is a multiple of 9, print Yes; otherwise, print No.

Sample Input 1

123456789

Sample Output 1

Yes

The sum of these digits is 1+2+3+4+5+6+7+8+9=45, which is a multiple of 9, so 123456789 is a multiple of 9.

Sample Input 2

0

Sample Output 2

Yes

Sample Input 3

31415926535897932384626433832795028841971693993751058209749445923078164062862089986280

Sample Output 3

No


my $n = <>;chomp($n);
my $length=length($n);
my $sum = 0;
while ($length>=0){
my $a=substr($n, $length, 1);
$sum = $sum + $a;
$length=$length-1;}
if($sum%9==0){print "Yes";}else{print"No";}


Score : 100 points

Problem Statement

We have weather records at AtCoder Town for some consecutive three days. A string of length 3, S, represents the records - if the i-th character is S, it means it was sunny on the i-th day; if that character is R, it means it was rainy on that day.

Find the maximum number of consecutive rainy days in this period.

Constraints

|S| = 3

Each character of S is S or R.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of consecutive rainy days in the period.

Sample Input 1

RRS

Sample Output 1

2

We had rain on the 1-st and 2-nd days in the period. Here, the maximum number of consecutive rainy days is 2, so we should print 2.

Sample Input 2

SSS

Sample Output 2

0

It was sunny throughout the period. We had no rainy days, so we should print 0.

Sample Input 3

RSR

Sample Output 3

1

We had rain on the 1-st and 3-rd days - two "streaks" of one rainy day, so we should print 1.


my $line = <STDIN>;
my $num = 0;
$num = 1 if ($line =~ /R/);
$num = 2 if ($line =~ /RR/);
$num = 3 if ($line =~ /RRR/);
print $num . "\n";


$str = <stdin>;
if($str =~ m/R[^R]R/g){
    print 1;
}else{
    $count = (() = $str =~ m/R/g);
    print $count;
}


my @day = split(//,<>);
my $MAX= 0;
my $count= 0;
for($i=0;$i<3;$i++){
    if( $day[$i] eq "R"){
        $count++;
    }else{
        if($count > $MAX){
        $MAX=$count;
        }
        $count=0;
    }
}
if($count > $MAX){
    $MAX= $count;
}
print "$MAX";


my @day = split(//,<>);

my $MAX= 0;
my $count= 0;
for($i=0;$i<3;$i++){
    if( $day[$i] eq "R"){
        $count++;
    }else{
        $MAX=$count;
        $count=0;
    }
}
if($count > $MAX){
    $MAX= $count;
}
print "$MAX";


Score : 200 points

Problem Statement

We have sticks numbered 1, \cdots, N. The length of Stick i (1 \leq i \leq N) is L_i.

In how many ways can we choose three of the sticks with different lengths that can form a triangle?

That is, find the number of triples of integers (i, j, k) (1 \leq i < j < k \leq N) that satisfy both of the following conditions:

L_i, L_j, and L_k are all different.

There exists a triangle whose sides have lengths L_i, L_j, and L_k.

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 \cdots L_N

Output

Print the number of ways to choose three of the sticks with different lengths that can form a triangle.

Sample Input 1

5
4 4 9 7 5

Sample Output 1

5

The following five triples (i, j, k) satisfy the conditions: (1, 3, 4), (1, 4, 5), (2, 3, 4), (2, 4, 5), and (3, 4, 5).

Sample Input 2

6
4 5 4 3 3 5

Sample Output 2

8

We have two sticks for each of the lengths 3, 4, and 5. To satisfy the first condition, we have to choose one from each length.

There is a triangle whose sides have lengths 3, 4, and 5, so we have 2 ^ 3 = 8 triples (i, j, k) that satisfy the conditions.

Sample Input 3

10
9 4 6 1 9 6 10 6 6 8

Sample Output 3

39

Sample Input 4

2
1 1

Sample Output 4

0

No triple (i, j, k) satisfies 1 \leq i < j < k \leq N, so we should print 0.


use strict;
use warnings;
use utf8;

use feature qw(say);
use Data::Dumper;

my $input = <STDIN>;
chomp $input;
my ($n, $k) = split / /, $input;

my $p_list = <STDIN>;
chomp $p_list;
my $p_lists = [split / /, $p_list];
my $next_step_by_step = +{};
my $step = 1;
for my $p (@$p_lists) {
    $next_step_by_step->{$step} = $p;
    $step++;
}

my $score_list = <STDIN>;
chomp $score_list;
my $score_lists = [split / /, $score_list];
my $score_by_step = +{};
$step = 1;
for my $score (@$score_lists) {
    $score_by_step->{$step} = $score;
    $step++;
}

my $total_nth = $k;

my $max_of_total;
for (my $first_step = 1; $first_step <= $n; $first_step++) {
    my $score = 0;
    my $loop = [];
    my $next_step = $next_step_by_step->{$first_step};

    my $nth = 2;
    while (1) {
        # stepに到着
        my $score_of_step = $score_by_step->{$next_step};
        $score += $score_of_step;
        push @$loop, $score;
        last if $next_step == $first_step;

        $next_step = $next_step_by_step->{$next_step};
        $nth++;
    }

    my $loop_count = scalar @$loop;

    my $max;
    if ($total_nth <= $loop_count) {
        my $possible_loop = [@$loop [0..$total_nth - 1]];
        $max = [sort { $b <=> $a } @$possible_loop]->[0];
    } else {
        my $score_of_last = $loop->[-1];
        if ($score_of_last <= 0) {
            $max = [sort { $b <=> $a } @$loop]->[0];
        } else {
            # extra_numは2ループ未満
            my ($loop_num, $extra_num) = calaculate($total_nth, $loop_count);
            my $second_loops = [map { $_ + $score_of_last} @$loop];
            my $extra_loops = [@$loop, @$second_loops];
            $extra_loops = [@$extra_loops[0..$extra_num - 1]];

            my $max_in_extra = [sort { $b <=> $a } @$extra_loops]->[0] || 0;
            $max = $loop_num * $score_of_last + $max_in_extra;
        }
    }
    $max_of_total = defined($max_of_total) && $max_of_total > $max ? $max_of_total : $max;
}
say $max_of_total;

sub calaculate {
    my ($total_nth, $loop_count) = @_;

    if ($total_nth % $loop_count == 0) {
        my $loop_num = ($total_nth / $loop_count) - 1;
        return ($loop_num, 0);
    } else {
        my $loop_num = int($total_nth / $loop_count) - 1;
        my $extra_num = $total_nth - $loop_num * $loop_count;
        return ($loop_num, $extra_num);
    }
}

Score : 400 points

Problem Statement

Takahashi will play a game using a piece on an array of squares numbered 1, 2, \cdots, N. Square i has an integer C_i written on it. Also, he is given a permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.

Now, he will choose one square and place the piece on that square. Then, he will make the following move some number of times between 1 and K (inclusive):

In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.

Help him by finding the maximum possible score at the end of the game. (The score is 0 at the beginning of the game.)

Constraints

2 \leq N \leq 5000

1 \leq K \leq 10^9

1 \leq P_i \leq N

P_i \neq i

P_1, P_2, \cdots, P_N are all different.

-10^9 \leq C_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N

Output

Print the maximum possible score at the end of the game.

Sample Input 1

5 2
2 4 5 1 3
3 4 -10 -8 8

Sample Output 1

8

When we start at some square of our choice and make at most two moves, we have the following options:

If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.

If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.

If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.

If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.

If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.

The maximum score achieved is 8.

Sample Input 2

2 3
2 1
10 -7

Sample Output 2

13

Sample Input 3

3 3
3 1 2
-1000 -2000 -3000

Sample Output 3

-1000

We have to make at least one move.

Sample Input 4

10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719

Sample Output 4

29507023469

The absolute value of the answer may be enormous.


use strict;
use warnings;
use utf8;

use feature qw(say);
use Data::Dumper;

my $input = <STDIN>;
chomp $input;
my ($n, $k) = split / /, $input;

my $p_list = <STDIN>;
chomp $p_list;
my $p_lists = [split / /, $p_list];
my $next_step_by_step = +{};
my $step = 1;
for my $p (@$p_lists) {
    $next_step_by_step->{$step} = $p;
    $step++;
}

my $score_list = <STDIN>;
chomp $score_list;
my $score_lists = [split / /, $score_list];
my $score_by_step = +{};
$step = 1;
for my $score (@$score_lists) {
    $score_by_step->{$step} = $score;
    $step++;
}

my $total_nth = $k;

my $max_of_total;
for (my $first_step = 1; $first_step <= $n; $first_step++) {
    my $score = 0;
    my $loop = [];
    my $next_step = $next_step_by_step->{$first_step};

    my $nth = 2;
    while (1) {
        # stepに到着
        my $score_of_step = $score_by_step->{$next_step};
        $score += $score_of_step;
        push @$loop, +{
            step => $next_step,
            score => $score,
            nth => $nth,
        };
        last if $next_step == $first_step;

        $next_step = $next_step_by_step->{$next_step};
        $nth++;
    }

    my $loop_count = scalar @$loop;

    my $max;
    if ($total_nth <= $loop_count) {
        my $possible_loop = [@$loop [0..$total_nth - 1]];
        $max = [sort { $b <=> $a } map { $_->{score} } @$possible_loop]->[0];
    } else {
        my $score_of_last = $loop->[-1]->{score};
        if ($score_of_last <= 0) {
            $max = [sort { $b <=> $a } map { $_->{score} } @$loop]->[0];
        } else {
            # extra_numは2ループ未満
            my ($loop_num, $extra_num) = calaculate($total_nth, $loop_count);
            my $first_loops = [map { $_->{score} } @$loop];
            my $second_loops = [map { $_->{score} + $score_of_last} @$loop];
            my $extra_loops = [@$first_loops, @$second_loops];
            $extra_loops = [@$extra_loops[0..$extra_num - 1]];

            my $max_in_extra = [sort { $b <=> $a } @$extra_loops]->[0] || 0;
            $max = $loop_num * $score_of_last + $max_in_extra;
        }
    }
    $max_of_total = defined($max_of_total) && $max_of_total > $max ? $max_of_total : $max;
}
say $max_of_total;

sub calaculate {
    my ($total_nth, $loop_count) = @_;

    if ($total_nth % $loop_count == 0) {
        my $loop_num = ($total_nth / $loop_count) - 1;
        return ($loop_num, 0);
    } else {
        my $loop_num = int($total_nth / $loop_count) - 1;
        my $extra_num = $total_nth - $loop_num * $loop_count;
        return ($loop_num, $extra_num);
    }
}


use POSIX qw(ceil);
my ($N,$K) =split(/ /,<>);
my $s = <>; chomp $s;
my @dat = split(/ /,$s);

my $ok =10**9;
#$ok =10;
my $ng =1;

while(){
    if(($ok-$ng)<=1){last;}
    my $cut =int(($ok+$ng)/2);
    my $sum=0;
    for(my $i=0;$i<=$#dat;$i++){
        #print "$dat[$i]\n";
        #$sum = $sum + (int(($cut-0.1+$dat[$i])/$cut)-1);
        $sum = $sum + (ceil($dat[$i]/$cut)-1);
        #print "$dat[$i] $cut $sum\n";
    }
    if($sum <= $K){
        $ok = $cut;
    }else{
        $ng = $cut
    }
    #print "cut $cut $ok $ng\n";
    #last;
}

$ok = ceil($ok);

print "$ok\n";

Score : 500 points

Problem Statement

We have N logs of lengths A_1,A_2,\cdots A_N.

We can cut these logs at most K times in total. When a log of length L is cut at a point whose distance from an end of the log is t (0<t<L), it becomes two logs of lengths t and L-t.

Find the shortest possible length of the longest log after at most K cuts, and print it after rounding up to an integer.

Constraints

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

0 \leq K \leq 10^9

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_N

Output

Print an integer representing the answer.

Sample Input 1

2 3
7 9

Sample Output 1

4

First, we will cut the log of length 7 at a point whose distance from an end of the log is 3.5, resulting in two logs of length 3.5 each.

Next, we will cut the log of length 9 at a point whose distance from an end of the log is 3, resulting in two logs of length 3 and 6.

Lastly, we will cut the log of length 6 at a point whose distance from an end of the log is 3.3, resulting in two logs of length 3.3 and 2.7.

In this case, the longest length of a log will be 3.5, which is the shortest possible result. After rounding up to an integer, the output should be 4.

Sample Input 2

3 0
3 4 5

Sample Output 2

5

Sample Input 3

10 10
158260522 877914575 602436426 24979445 861648772 623690081 433933447 476190629 262703497 211047202

Sample Output 3

292638192


#!/usr/bin/perl

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

my @sa = sort { $a <=> $b } @aa;

my $sum1 = 0;
for(my $i=0;$i<$n;$i++){
  $sum1 += $aa[$i];
}

if( $k == 0 ){
  print "$sa[$#sa]\n"; exit(0);
}

my $mx = 10 ** 9;
my $mn = 1;

my $mx_c = &chk($mx);
my $mn_c = &chk($mn);

if( $mn_c < $k ){
  print "1\n"; exit(0);
}

#print "mx,mn_c = $mx_c , $mn_c \n";

while( $mx - $mn > 1 ){
  my $md = int( ($mx + $mn)/2 );
  my $md_c = &chk($md);
  if( $md_c <= $k and $k <= $mn_c ){
    $mx = $md;
    $mx_c = $md_c;
  } else { #( $mx_c <= $k < $md_c ){
    $mn = $md;
    $mn_c = $md_c;
  }
  #print "mx,mn_c = $mx_c , $mn_c \n";
}

if( $mn_c == $k ){
  print "$mn\n";
} elsif( $mx_c == $k ){
  print "$mx\n";
} else {
  if( $mx_c < $k and $k < $mn_c ){
    print "$mn\n";
  } elsif( $mn_c < $k ){
    print "$mn\n";
  } else {
    print "$mx\n";
  }
}

exit(0);

sub chk {
  my $v = shift;
  my $s = 0;
  for(my $i=0;$i<$n;$i++){
    my $vv = int((( $v - 1 ) + $aa[$i]) / $v) - 1;
    $s += $vv;
  }
  return $s;
}


use strict;
my($a,$b,$c)=glob <>;
my $k=<>;
my $count=0;
while($a>=$c){$c=$c*2;$count=$count+1;}
while($c>=$b){$b=$b*2;$count=$count+1;}
if($count<=$k){print"Yes";}else{print"No";}

Score: 200 points

Problem Statement

M-kun has the following three cards:

A red card with the integer A.

A green card with the integer B.

A blue card with the integer C.

He is a genius magician who can do the following operation at most K times:

Choose one of the three cards and multiply the written integer by 2.

His magic is successful if both of the following conditions are satisfied after the operations:

The integer on the green card is strictly greater than the integer on the red card.

The integer on the blue card is strictly greater than the integer on the green card.

Determine whether the magic can be successful.

Constraints

1 \leq A, B, C \leq 7

1 \leq K \leq 7

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C
K

Output

If the magic can be successful, print Yes; otherwise, print No.

Sample Input 1

7 2 5
3

Sample Output 1

Yes

The magic will be successful if, for example, he does the following operations:

First, choose the blue card. The integers on the red, green, and blue cards are now 7, 2, and 10, respectively.

Second, choose the green card. The integers on the red, green, and blue cards are now 7, 4, and 10, respectively.

Third, choose the green card. The integers on the red, green, and blue cards are now 7, 8, and 10, respectively.

Sample Input 2

7 4 2
3

Sample Output 2

No

He has no way to succeed in the magic with at most three operations.


chomp($n = <>);
chomp($line = <>);
@kabu = split(/ /, $line);

$money = 1000;
$buy = 0;
$start = $n - 2;

for $i (0 .. $n - 2){
	if($kabu[$i] < $kabu[$i + 1]){
		$start = $i;
		$buy = int($money / $kabu[$i]);
		$money -= $buy * $kabu[$i];
		last;
	}
}

for $i ($start .. $n - 2){
	if($kabu[$i] < $kabu[$i + 1]){
		$now = int($money / $kabu[$i]);
		$buy += $now;
		$money -= $now * $kabu[$i];
	}else{
		$money += $buy * $kabu[$i];
		$buy = 0;
	}
}

$money += $kabu[$#kabu] * $buy;

print "$money\n";

Score: 400 points

Problem Statement

To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.

He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:

A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.

In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.

Buy stock: Pay A_i yen and receive one stock.

Sell stock: Sell one stock for A_i yen.

What is the maximum possible amount of money that M-kun can have in the end by trading optimally?

Constraints

2 \leq N \leq 80

100 \leq A_i \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the maximum possible amount of money that M-kun can have in the end, as an integer.

Sample Input 1

7
100 130 130 130 115 115 150

Sample Output 1

1685

In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows:

Initially, he has 1000 yen and no stocks.

Day 1: Buy 10 stocks for 1000 yen. Now he has 0 yen.

Day 2: Sell 7 stocks for 910 yen. Now he has 910 yen.

Day 3: Sell 3 stocks for 390 yen. Now he has 1300 yen.

Day 4: Do nothing.

Day 5: Buy 1 stock for 115 yen. Now he has 1185 yen.

Day 6: Buy 10 stocks for 1150 yen. Now he has 35 yen.

Day 7: Sell 11 stocks for 1650 yen. Now he has 1685 yen.

There is no way to have 1686 yen or more in the end, so the answer is 1685.

Sample Input 2

6
200 180 160 140 120 100

Sample Output 2

1000

In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen.

Sample Input 3

2
157 193

Sample Output 3

1216

In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2, after which we will have 1216 yen.


#!/usr/bin/perl

use warnings;

my $AC = 0;
my $WA = 0;
my $TLE = 0;
my $RE = 0;
while ($line = <STDIN>) {
    if ($line eq "AC\n" ) {
        $AC = $AC + 1;
    } elsif ($line eq "WA\n") {
        $WA = $WA + 1;
    } elsif ($line eq "TLE\n") {
        $TLE = $TLE + 1;
    } elsif ($line eq "RE\n") {
        $RE = $RE + 1;
    }
}
print ("AC x $AC\n");
print ("WA x $WA\n");
print ("TLE x $TLE\n");
print ("RE x $RE\n");

Score : 200 points

Problem Statement

Takahashi is participating in a programming contest called AXC002, and he has just submitted his code to Problem A.

The problem has N test cases.

For each test case i (1\leq i \leq N), you are given a string S_i representing the verdict for that test case. Find the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively.

See the Output section for the output format.

Constraints

1 \leq N \leq 10^5

S_i is AC, WA, TLE, or RE.

Input

Input is given from Standard Input in the following format:

N
S_1
\vdots
S_N

Output

Let C_0, C_1, C_2, and C_3 be the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively. Print the following:

AC x C_0
WA x C_1
TLE x C_2
RE x C_3

Sample Input 1

6
AC
TLE
AC
AC
WA
TLE

Sample Output 1

AC x 3
WA x 1
TLE x 2
RE x 0

We have 3, 1, 2, and 0 test case(s) for which the verdict is AC, WA, TLE, and RE, respectively.

Sample Input 2

10
AC
AC
AC
AC
AC
AC
AC
AC
AC
AC

Sample Output 2

AC x 10
WA x 0
TLE x 0
RE x 0


Score : 100 points

Problem Statement

Given an integer a as input, print the value a + a^2 + a^3.

Constraints

1 \leq a \leq 10

a is an integer.

Input

Input is given from Standard Input in the following format:

a

Output

Print the value a + a^2 + a^3 as an integer.

Sample Input 1

2

Sample Output 1

14

When a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14.

Print the answer as an input. Outputs such as 14.0 will be judged as incorrect.

Sample Input 2

10

Sample Output 2

1110


use strict;
my $x = <>;
chomp $x;
my $y = <>;
chomp $y;
my $a;
my $b;
my $c;
my $d;
$d=0;
my $length = length(@x);
for($c=0; $c > $length; $c++){
$a = substr ($x , 0);
$b = substr ($y , 0);
if($a != $b){$d = $d+1 ;}}
print "$d";

Score : 200 points

Problem Statement

Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so.

Operation: Choose one character of S and replace it with a different character.

Constraints

S and T have lengths between 1 and 2\times 10^5 (inclusive).

S and T consists of lowercase English letters.

S and T have equal lengths.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the answer.

Sample Input 1

cupofcoffee
cupofhottea

Sample Output 1

4

We can achieve the objective in four operations, such as the following:

First, replace the sixth character c with h.

Second, replace the eighth character f with t.

Third, replace the ninth character f with t.

Fourth, replace the eleventh character e with a.

Sample Input 2

abcde
bcdea

Sample Output 2

5

Sample Input 3

apple
apple

Sample Output 3

0

No operations may be needed to achieve the objective.


use strict;
use List::Util qw(sum);
my @x = <>;
chomp @x;
my @y = <>;
chomp @y;
my $a;
my $b;
my $c;
my $d;
$d=0;
my $length = @x;
for($c=0;$c<$length;$c++){
$a = shift @x;
$b = shift @y;
if($a!=$b){$d=$d+1;}}
print "$d";


Score : 500 points

Problem Statement

Count the pairs of length-N sequences consisting of integers between 1 and M (inclusive), A_1, A_2, \cdots, A_{N} and B_1, B_2, \cdots, B_{N}, that satisfy all of the following conditions:

A_i \neq B_i, for every i such that 1\leq i\leq N.

A_i \neq A_j and B_i \neq B_j, for every (i, j) such that 1\leq i < j\leq N.

Since the count can be enormous, print it modulo (10^9+7).

Constraints

1\leq N \leq M \leq 5\times10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the count modulo (10^9+7).

Sample Input 1

2 2

Sample Output 1

2

A_1=1,A_2=2,B_1=2,B_2=1 and A_1=2,A_2=1,B_1=1,B_2=2 satisfy the conditions.

Sample Input 2

2 3

Sample Output 2

18

Sample Input 3

141421 356237

Sample Output 3

881613484


Score : 100 points

Problem Statement

An uppercase or lowercase English letter \alpha will be given as input.
If \alpha is uppercase, print A; if it is lowercase, print a.

Constraints

\alpha is an uppercase (A - Z) or lowercase (a - z) English letter.

Input

Input is given from Standard Input in the following format:

α

Output

If \alpha is uppercase, print A; if it is lowercase, print a.

Sample Input 1

B

Sample Output 1

A

B is uppercase, so we should print A.

Sample Input 2

a

Sample Output 2

a

a is lowercase, so we should print a.


use strict;
use warnings;
use utf8;

use Data::Dumper;
use feature qw(say);

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

my $small_character_list = ['a'..'z'];
if (scalar grep { $_ eq $n } @$small_character_list) {
    say 'a';
} else {
    say 'A';
}


Score : 200 points

Problem Statement

A shop sells N kinds of fruits, Fruit 1, \ldots, N, at prices of p_1, \ldots, p_N yen per item, respectively. (Yen is the currency of Japan.)

Here, we will choose K kinds of fruits and buy one of each chosen kind. Find the minimum possible total price of those fruits.

Constraints

1 \leq K \leq N \leq 1000

1 \leq p_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 p_2 \ldots p_N

Output

Print an integer representing the minimum possible total price of fruits.

Sample Input 1

5 3
50 100 80 120 80

Sample Output 1

210

This shop sells Fruit 1, 2, 3, 4, and 5 for 50 yen, 100 yen, 80 yen, 120 yen, and 80 yen, respectively.

The minimum total price for three kinds of fruits is 50 + 80 + 80 = 210 yen when choosing Fruit 1, 3, and 5.

Sample Input 2

1 1
1000

Sample Output 2

1000


use strict;
use warnings;
use utf8;

use Data::Dumper;
use feature qw(say);

my $nk = <STDIN>;
chomp $nk;
my ($n, $k) = split / /, $nk;

my $input = <STDIN>;
chomp $input;
my $array = [split / /, $input];
my $sorted_array = [sort { $a <=> $b } @$array];

my $sum = 0;
for (my $i = 0; $i < $k; $i++) {
    $sum += $sorted_array->[$i];
}
say $sum;


use strict;
use warnings;
use POSIX qw/ floor  /;
use feature 'say';

chomp(my $count = <STDIN>);
my @alphabets = ('a' .. 'z');

{
  my @nums = ();

  sub divider {
    my $n = shift;

    while ( $n >= 26 ) {
      my $div = $n / 26;
      my $mod = $n % 26;
      divider( $div );

      push(@nums, $mod);
      return @nums;
    }

    push(@nums, $n);
    return @nums;

  }
}

my @elms = divider($count);

my $str = '';
for my $i (@elms ) {
  my $ind = floor($i) - 1;
  $str .= $alphabets[$ind];
}
say $str;

__DATA__
123456789

Score : 300 points

Problem Statement

1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows:

the dogs numbered 1,2,\cdots,26 were respectively given the names a, b, ..., z;

the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names aa, ab, ac, ..., zy, zz;

the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names aaa, aab, aac, ..., zzy, zzz;

the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names aaaa, aaab, aaac, ..., zzzy, zzzz;

the dogs numbered 475255,475256,\cdots were respectively given the names aaaaa, aaaab, ...;

and so on.

To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names:

a, b, ..., z, aa, ab, ..., az, ba, bb, ..., bz, ..., za, zb, ..., zz, aaa, aab, ..., aaz, aba, abb, ..., abz, ..., zzz, aaaa, ...

Now, Roger asks you:

"What is the name for the dog numbered N?"

Constraints

N is an integer.

1 \leq N \leq 1000000000000001

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer to Roger's question as a string consisting of lowercase English letters.

Sample Input 1

2

Sample Output 1

b

Sample Input 2

27

Sample Output 2

aa

Sample Input 3

123456789

Sample Output 3

jjddja


use strict;
use Math::BaseCalc;
my$n=glob <>;
chomp $n;
$calc  = new Math::BaseCalc(digits => ['a'..'z']);
$value = $calc->to_base($n);
print $value;


Score : 500 points

Problem Statement

There are N Snuke Cats numbered 1, 2, \ldots, N, where N is even.

Each Snuke Cat wears a red scarf, on which his favorite non-negative integer is written.

Recently, they learned the operation called xor (exclusive OR).

What is xor?

For n non-negative integers x_1, x_2, \ldots, x_n, their xor, x_1~\textrm{xor}~x_2~\textrm{xor}~\ldots~\textrm{xor}~x_n is defined as follows:

When x_1~\textrm{xor}~x_2~\textrm{xor}~\ldots~\textrm{xor}~x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3~\textrm{xor}~5 = 6.

They wanted to use this operation quickly, so each of them calculated the xor of the integers written on their scarfs except his scarf.

We know that the xor calculated by Snuke Cat i, that is, the xor of the integers written on the scarfs except the scarf of Snuke Cat i is a_i.
Using this information, restore the integer written on the scarf of each Snuke Cat.

Constraints

All values in input are integers.

2 \leq N \leq 200000

N is even.

0 \leq a_i \leq 10^9

There exists a combination of integers on the scarfs that is consistent with the given information.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print a line containing N integers separated with space.

The i-th of the integers from the left should represent the integer written on the scarf of Snuke Cat i.

If there are multiple possible solutions, you may print any of them.

Sample Input 1

4
20 11 9 24

Sample Output 1

26 5 7 22

5~\textrm{xor}~7~\textrm{xor}~22 = 20

26~\textrm{xor}~7~\textrm{xor}~22 = 11

26~\textrm{xor}~5~\textrm{xor}~22 = 9

26~\textrm{xor}~5~\textrm{xor}~7 = 24

Thus, this output is consistent with the given information.


Score : 200 points

Problem Statement

Takahashi is standing on a two-dimensional plane, facing north. Find the minimum positive integer K such that Takahashi will be at the starting position again after he does the following action K times:

Go one meter in the direction he is facing. Then, turn X degrees counter-clockwise.

Constraints

1 \leq X \leq 179

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the number of times Takahashi will do the action before he is at the starting position again.

Sample Input 1

90

Sample Output 1

4

Takahashi's path is a square.

Sample Input 2

1

Sample Output 2

360


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

use feature qw(say);

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

my $list_array = [split/ /, $list];

my $i = 1;
for my $num (@$list_array) {
    if ($num == 0) {
        say $i;
    }
    $i++;
}

Score : 100 points

Problem Statement

We have five variables x_1, x_2, x_3, x_4, and x_5.

The variable x_i was initially assigned a value of i.

Snuke chose one of these variables and assigned it 0.

You are given the values of the five variables after this assignment.

Find out which variable Snuke assigned 0.

Constraints

The values of x_1, x_2, x_3, x_4, and x_5 given as input are a possible outcome of the assignment by Snuke.

Input

Input is given from Standard Input in the following format:

x_1 x_2 x_3 x_4 x_5

Output

If the variable Snuke assigned 0 was x_i, print the integer i.

Sample Input 1

0 2 3 4 5

Sample Output 1

1

In this case, Snuke assigned 0 to x_1, so we should print 1.

Sample Input 2

1 2 0 4 5

Sample Output 2

3


Score : 200 points

Problem Statement

There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.

Takahashi says: "there are X animals in total in the garden, and they have Y legs in total." Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.

Constraints

1 \leq X \leq 100

1 \leq Y \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.

Sample Input 1

3 8

Sample Output 1

Yes

The statement "there are 3 animals in total in the garden, and they have 8 legs in total" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct.

Sample Input 2

2 100

Sample Output 2

No

There is no combination of numbers of cranes and turtles in which this statement is correct.

Sample Input 3

1 2

Sample Output 3

Yes

We also consider the case in which there are only cranes or only turtles.


Score : 300 points

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \ldots, p_N.

Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

Constraints

1 \leq X \leq 100

0 \leq N \leq 100

1 \leq p_i \leq 100

p_1, \ldots, p_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.

Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.

Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.

Sample Input 3

100 0

Sample Output 3

100

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.


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

chomp(my $line  = <STDIN>);
my ($x, $n) = split / /, $line;

if($n == 0) {
  say $x;
  exit;
}

chomp(my $line2 = <STDIN>);
my @nums = sort { $a <=> $b } (split/ /, $line2);

my %exist_hash = ();
map { $exist_hash{$_}++ } @nums;

my @all = ($nums[0] .. $nums[-1]);

my @not_exist = ();
for (@all) {
  if(! exists($exist_hash{$_}) ) {
    push(@not_exist, $_);
  }
}

my $min_diff = 100000;
my @answers = ();
for my $num (@not_exist) {
  my $diff = abs($x - $num);
  if ( $diff <= $min_diff) {
    push(@answers, $num);
    $min_diff = $diff;
  }
}

for my $num ( sort { $a <=> $b } @answers) {
  say $num;
  exit;
}


use strict;
my $s = <>;
my $substr = substr($s, 0, 3);
print "$substur";

Score : 100 points

Problem Statement

When you asked some guy in your class his name, he called himself S, where S is a string of length between 3 and 20 (inclusive) consisting of lowercase English letters.
You have decided to choose some three consecutive characters from S and make it his nickname. Print a string that is a valid nickname for him.

Constraints

3 \leq |S| \leq 20

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print your answer.

Sample Input 1

takahashi

Sample Output 1

tak

Sample Input 2

naohiro

Sample Output 2

nao


my $S = <STDIN>;
my $substr = substr(my $S, 0, 3);
print "my $substur";


use strict;
my $a = <>;
my $v = <>;
my $b = <>;
my $w = <>;
my $t = <>;
if($v<=$w){print"NO";}elsif(abs($a-$b)/$t)<=($v-$w)){print"YES";}
else{print"NO";}

Score : 200 points

Problem Statement

Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate A, and he can travel the distance of V per second.
The other child is now at coordinate B, and she can travel the distance of W per second.

He can catch her when his coordinate is the same as hers.
Determine whether he can catch her within T seconds (including exactly T seconds later).
We assume that both children move optimally.

Constraints

-10^9 \leq A,B \leq 10^9

1 \leq V,W \leq 10^9

1 \leq T \leq 10^9

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A V
B W
T

Output

If "it" can catch the other child, print YES; otherwise, print NO.

Sample Input 1

1 2
3 1
3

Sample Output 1

YES

Sample Input 2

1 2
3 2
3

Sample Output 2

NO

Sample Input 3

1 2
3 3
3

Sample Output 3

NO


use strict;
my $a = <>;
my $b = <>;
my $t = <>;
my $v = <>;
my $w = <>;
if($v==$w){print"NO";}
elsif($v<$w){print"NO";}
elsif(($v-$w)>(abs($a-$b)/$t)){print"YES";}
else{print"NO";}


use strict;
use warnings;

my ($n, $k) = glob <>;
my @a = glob <>;

print join ' ' , @{operation(\@a, $k)};

sub operation {
    my ($lights, $k) = @_;
    my @range_diff = map { 0 } (0..$n-1);

    for my $count (0..$k-1) {
        for my $pos (0..$n-1) {
            my $min = ($pos - $lights->[$pos] >= 0) ? $pos - $lights->[$pos] : 0;
            ++$range_diff[$min];

            if ($pos + $lights->[$pos] + 1 <= $n-1) {
                --$range_diff[$pos + $lights->[$pos] + 1];
            }
        }
        for my $pos (1..$n-1) {
            $range_diff[$pos] += $range_diff[$pos-1];
        }

        @$lights = @range_diff;
        @range_diff = map { 0 } (0..$n-1);

        last if ($count >= 50);
    }

    return $lights;
}

Score : 500 points

Problem Statement

We have N bulbs arranged on a number line, numbered 1 to N from left to right.
Bulb i is at coordinate i.

Each bulb has a non-negative integer parameter called intensity.
When there is a bulb of intensity d at coordinate x, the bulb illuminates the segment from coordinate x-d-0.5 to x+d+0.5.
Initially, the intensity of Bulb i is A_i. We will now do the following operation K times in a row:

For each integer i between 1 and N (inclusive), let B_i be the number of bulbs illuminating coordinate i. Then, change the intensity of each bulb i to B_i.

Find the intensity of each bulb after the K operations.

Constraints

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

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

0 \leq A_i \leq N

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Print the intensity A{'}_i of each bulb i after the K operations to Standard Output in the following format:

A{'}_1 A{'}_2 \ldots A{'}_N

Sample Input 1

5 1
1 0 0 1 0

Sample Output 1

1 2 2 1 2

Initially, only Bulb 1 illuminates coordinate 1, so the intensity of Bulb 1 becomes 1 after the operation.
Similarly, the bulbs initially illuminating coordinate 2 are Bulb 1 and 2, so the intensity of Bulb 2 becomes 2.

Sample Input 2

5 2
1 0 0 1 0

Sample Output 2

3 3 4 4 3


Score : 400 points

Problem Statement

There are two persons, numbered 0 and 1, and a variable x whose initial value is 0.
The two persons now play a game.
The game is played in N rounds. The following should be done in the i-th round (1 \leq i \leq N):

Person S_i does one of the following:

Replace x with x \oplus A_i, where \oplus represents bitwise XOR.

Do nothing.

Person 0 aims to have x=0 at the end of the game, while Person 1 aims to have x \neq 0 at the end of the game.

Determine whether x becomes 0 at the end of the game when the two persons play optimally.

Solve T test cases for each input file.

Constraints

1 \leq T \leq 100

1 \leq N \leq 200

1 \leq A_i \leq 10^{18}

S is a string of length N consisting of 0 and 1.

All numbers in input are integers.

Input

Input is given from Standard Input in the following format.
The first line is as follows:

T

Then, T test cases follow.
Each test case is given in the following format:

N
A_1 A_2 \cdots A_N
S

Output

For each test case, print a line containing 0 if x becomes 0 at the end of the game, and 1 otherwise.

Sample Input 1

3
2
1 2
10
2
1 1
10
6
2 3 4 5 6 7
111000

Sample Output 1

1
0
0

In the first test case, if Person 1 replaces x with 0 \oplus 1=1, we surely have x \neq 0 at the end of the game, regardless of the choice of Person 0.

In the second test case, regardless of the choice of Person 1, Person 0 can make x=0 with a suitable choice.


my $s=<>;chomp $s;
my @dat = split(//, $s);

for(my $i=0;$i<=$#dat;$i++){

    if($dat[$i] eq "?"){
        print "D";
    }else{
        print "$dat[$i]";
    }

}

Score : 200 points

Problem Statement

For a string S consisting of the uppercase English letters P and D, let the doctoral and postdoctoral quotient of S be the total number of occurrences of D and PD in S as contiguous substrings. For example, if S = PPDDP, it contains two occurrences of D and one occurrence of PD as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3.

We have a string T consisting of P, D, and ?.

Among the strings that can be obtained by replacing each ? in T with P or D, find one with the maximum possible doctoral and postdoctoral quotient.

Constraints

1 \leq |T| \leq 2 \times 10^5

T consists of P, D, and ?.

Input

Input is given from Standard Input in the following format:

T

Output

Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each ? in T with P or D.
If there are multiple such strings, you may print any of them.

Sample Input 1

PD?D??P

Sample Output 1

PDPDPDP

This string contains three occurrences of D and three occurrences of PD as contiguous substrings, so its doctoral and postdoctoral quotient is 6, which is the maximum doctoral and postdoctoral quotient of a string obtained by replacing each ? in T with P or D.

Sample Input 2

P?P?

Sample Output 2

PDPD


Score: 200 points

Problem Statement

We have a string S consisting of lowercase English letters.

If the length of S is at most K, print S without change.

If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result.

Constraints

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

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

K
S

Output

Print a string as stated in Problem Statement.

Sample Input 1

7
nikoandsolstice

Sample Output 1

nikoand...

nikoandsolstice has a length of 15, which exceeds K=7.

We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand....

Sample Input 2

40
ferelibenterhominesidquodvoluntcredunt

Sample Output 2

ferelibenterhominesidquodvoluntcredunt

The famous quote from Gaius Julius Caesar.


Score: 300 points

Problem Statement

Consider an analog clock whose hour and minute hands are A and B centimeters long, respectively.

An endpoint of the hour hand and an endpoint of the minute hand are fixed at the same point, around which each hand rotates clockwise at constant angular velocity. It takes the hour and minute hands 12 hours and 1 hour to make one full rotation, respectively.

At 0 o'clock, the two hands overlap each other. H hours and M minutes later, what is the distance in centimeters between the unfixed endpoints of the hands?

Constraints

All values in input are integers.

1 \leq A, B \leq 1000

0 \leq H \leq 11

0 \leq M \leq 59

Input

Input is given from Standard Input in the following format:

A B H M

Output

Print the answer without units. Your output will be accepted when its absolute or relative error from the correct value is at most 10^{-9}.

Sample Input 1

3 4 9 0

Sample Output 1

5.00000000000000000000

The two hands will be in the positions shown in the figure below, so the answer is 5 centimeters.

Sample Input 2

3 4 10 40

Sample Output 2

4.56425719433005567605

The two hands will be in the positions shown in the figure below. Note that each hand always rotates at constant angular velocity.


Score: 400 points

Problem Statement

There is a cave.

The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.

It is dark in the cave, so we have decided to place a signpost in each room except Room 1. The signpost in each room will point to one of the rooms directly connected to that room with a passage.

Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1.

If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.

Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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

1 \leq A_i, B_i \leq N\ (1 \leq i \leq M)

A_i \neq B_i\ (1 \leq i \leq M)

One can travel between any two rooms by traversing passages.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If there is no way to place signposts satisfying the objective, print No.

Otherwise, print N lines. The first line should contain Yes, and the i-th line (2 \leq i \leq N) should contain the integer representing the room indicated by the signpost in Room i.

Sample Input 1

4 4
1 2
2 3
3 4
4 2

Sample Output 1

Yes
1
2
2

If we place the signposts as described in the sample output, the following happens:

Starting in Room 2, you will reach Room 1 after traversing one passage: (2) \to 1. This is the minimum number of passages possible.

Starting in Room 3, you will reach Room 1 after traversing two passages: (3) \to 2 \to 1. This is the minimum number of passages possible.

Starting in Room 4, you will reach Room 1 after traversing two passages: (4) \to 2 \to 1. This is the minimum number of passages possible.

Thus, the objective is satisfied.

Sample Input 2

6 9
3 4
6 1
2 4
5 3
4 6
1 5
6 2
4 5
5 6

Sample Output 2

Yes
6
5
5
1
1

If there are multiple solutions, any of them will be accepted.


Score : 100 points

Problem Statement

Takahashi wants to be a member of some web service.

He tried to register himself with the ID S, which turned out to be already used by another user.

Thus, he decides to register using a string obtained by appending one character at the end of S as his ID.

He is now trying to register with the ID T. Determine whether this string satisfies the property above.

Constraints

S and T are strings consisting of lowercase English letters.

1 \leq |S| \leq 10

|T| = |S| + 1

Input

Input is given from Standard Input in the following format:

S
T

Output

If T satisfies the property in Problem Statement, print Yes; otherwise, print No.

Sample Input 1

chokudai
chokudaiz

Sample Output 1

Yes

chokudaiz can be obtained by appending z at the end of chokudai.

Sample Input 2

snuke
snekee

Sample Output 2

No

snekee cannot be obtained by appending one character at the end of snuke.

Sample Input 3

a
aa

Sample Output 3

Yes


my $input = <STDIN>;
chomp $input;
my ($A, $B, $C, $K) = split /\s+/, $input;
my $tmp = $A - $K;
if ($tmp >= 0) {
    print $K;
} else {
    $tmp = $B + $tmp;
    if ($tmp >= 0) {
        print $A;
    } else {
        print $A + $tmp;
    }
}

Score : 200 points

Problem Statement

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.

We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

Constraints

All values in input are integers.

0 \leq A, B, C

1 \leq K \leq A + B + C \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the maximum possible sum of the numbers written on the cards chosen.

Sample Input 1

2 1 1 3

Sample Output 1

2

Consider picking up two cards with 1s and one card with a 0.
In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000


my $s = <>;
chomp $s;
my @PDB;
my @MDB;
#my $sum=0;
my $state =0;
for(my $i=0;$i<$s;$i++){
    my $line = <>;chomp $line;
    my @dat = split(//,$line);
    #print "@dat\n";
    my $min=0;
    my $total=0;
    for(my $j=0;$j<=$#dat;$j++){
        #print "$dat[$j]\n";
      if($dat[$j] eq '(' ){
         #print "plus \n";
        $total++;
      }else{
        $total--;
      }
      if($min >$total ){ $min =$total;}
    }
    if( $min >=0 ){#-にならないならここで足してしまう
        $state = $state + $total;
        next;
    }
     my @row = ($total,$min);
    if( $total >=0){
      push (@PDB,\@row);
    }else{
      push (@MDB,\@row);
    }
    #print "$total $min \n";
    #$sum = $sum +$total;
}
if($state ==0){ print "No";exit;}
@PDB = sort{ $b->[1] <=> $a->[1]} @PDB;
#my @amari;

for(my $i=0;$i<=$#PDB;$i++){
  if(($state+$PDB[$i][1]) >= 0){#採用
    $state = $state + $PDB[$i][0];
    #print "@{$PDB[$i]} state $state \n ";
  }else{
    print "No";exit;
  }
}
@MDB = sort{ $b->[1] <=> $a->[1]} @MDB;

for(my $i=0;$i<=$#MDB;$i++){
  if(($state+$MDB[$i][1]) >= 0){#採用
    $state = $state + $MDB[$i][0];
    #print "@{$MDB[$i]} state $state \n ";
  }else{
    print "No";exit;
  }
}

if( $state == 0){
    print "Yes";
}else{
    print "No";
}

Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No


Score : 200 points

Problem Statement

N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town.

There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}.

Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief?

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 100

1 \leq d_i \leq N

1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N

Input

Input is given from Standard Input in the following format:

N K
d_1
A_{1, 1} \cdots A_{1, d_1}
\vdots
d_K
A_{K, 1} \cdots A_{K, d_K}

Output

Print the answer.

Sample Input 1

3 2
2
1 3
1
3

Sample Output 1

1

Snuke 1 has Snack 1.

Snuke 2 has no snacks.

Snuke 3 has Snack 1 and 2.

Thus, there will be one victim: Snuke 2.

Sample Input 2

3 3
1
3
1
3
1
3

Sample Output 2

2


my $s = <STDIN>;
chomp $s;
my ($N,$K) = split(/ /,$s);
#print "$N,$K\n";
my $d;
my %data;
for(my $i=0;$i<$K;$i++){
    #print "i$i\n";
    $d = <STDIN>;
    $s = <STDIN>;
    chomp $s;
    my @dat = split(/ /,$s);
    for(my $j=0;$j<=$#dat;$j++){
        #print "$dat[$j]\n";
        $data{$dat[$j]} = 1;
    }
}
my $count =0;
foreach my $num (keys%data){
    #print "$num\n";
    $count++;
}
my $ans = $N - $count;
print "$ans";


Score : 300 points

Problem Statement

There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is H_i.
There are also M roads, each connecting two different observatories. Road j connects Obs. A_j and Obs. B_j.

Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road.
Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.

How many good observatories are there?

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq H_i \leq 10^9

1 \leq A_i,B_i \leq N

A_i \neq B_i

Multiple roads may connect the same pair of observatories.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
H_1 H_2 ... H_N
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Print the number of good observatories.

Sample Input 1

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

Sample Output 1

2

From Obs. 1, you can reach Obs. 3 using just one road. The elevation of Obs. 1 is not higher than that of Obs. 3, so Obs. 1 is not good.

From Obs. 2, you can reach Obs. 3 and 4 using just one road. The elevation of Obs. 2 is not higher than that of Obs. 3, so Obs. 2 is not good.

From Obs. 3, you can reach Obs. 1 and 2 using just one road. The elevation of Obs. 3 is higher than those of Obs. 1 and 2, so Obs. 3 is good.

From Obs. 4, you can reach Obs. 2 using just one road. The elevation of Obs. 4 is higher than that of Obs. 2, so Obs. 4 is good.

Thus, the good observatories are Obs. 3 and 4, so there are two good observatories.

Sample Input 2

6 5
8 6 9 1 2 1
1 3
4 2
4 3
4 6
4 6

Sample Output 2

3


Score : 400 points

Problem Statement

Give a pair of integers (A, B) such that A^5-B^5 = X.
It is guaranteed that there exists such a pair for the given integer X.

Constraints

1 \leq X \leq 10^9

X is an integer.

There exists a pair of integers (A, B) satisfying the condition in Problem Statement.

Input

Input is given from Standard Input in the following format:

X

Output

Print A and B, with space in between.
If there are multiple pairs of integers (A, B) satisfying the condition, you may print any of them.

A B

Sample Input 1

33

Sample Output 1

2 -1

For A=2 and B=-1, A^5-B^5 = 33.

Sample Input 2

1

Sample Output 2

0 -1


Score : 200 points

Problem Statement

Takahashi has a deposit of 100 yen (the currency of Japan) in AtCoder Bank.

The bank pays an annual interest rate of 1 % compounded annually. (A fraction of less than one yen is discarded.)

Assuming that nothing other than the interest affects Takahashi's balance, in how many years does the balance reach X yen or above for the first time?

Constraints

101 \le X \le 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the number of years it takes for Takahashi's balance to reach X yen or above for the first time.

Sample Input 1

103

Sample Output 1

3

The balance after one year is 101 yen.

The balance after two years is 102 yen.

The balance after three years is 103 yen.

Thus, it takes three years for the balance to reach 103 yen or above.

Sample Input 2

1000000000000000000

Sample Output 2

3760

Sample Input 3

1333333333

Sample Output 3

1706


use strict;
use warnings;

chomp(my $x = <>);

my $m = 100;
for my $i (1..4760) {
  $m =int $m * 1.01;
  if ($m >= $x){
    print "$i\n";
    last;
  }
}


my $X = <STDIN>;

my $a = 100;
my $count =0;

while(){
    $a = int ($a *1.01 );
    $count++;
    if( $X<=$a ){last}
}
print "$count";


Score : 300 points

Problem Statement

Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).

Consider a sequence A satisfying the following conditions:

A is a sequence of N positive integers.

1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.

Let us define a score of this sequence as follows:

The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)

Find the maximum possible score of A.

Constraints

All values in input are integers.

2 ≤ N ≤ 10

1 \leq M \leq 10

1 \leq Q \leq 50

1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )

0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )

(a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j)

1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )

Input

Input is given from Standard Input in the following format:

N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q

Output

Print the maximum possible score of A.

Sample Input 1

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

Sample Output 1

110

When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

Sample Input 2

4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328

Sample Output 2

357500

Sample Input 3

10 10 1
1 10 9 1

Sample Output 3

1


Score : 500 points

Problem Statement

You are going to hold a competition of one-to-one game called AtCoder Janken. (Janken is the Japanese name for Rock-paper-scissors.)
N players will participate in this competition, and they are given distinct integers from 1 through N.
The arena has M playing fields for two players. You need to assign each playing field two distinct integers between 1 and N (inclusive).
You cannot assign the same integer to multiple playing fields.
The competition consists of N rounds, each of which proceeds as follows:

For each player, if there is a playing field that is assigned the player's integer, the player goes to that field and fight the other player who comes there.

Then, each player adds 1 to its integer. If it becomes N+1, change it to 1.

You want to ensure that no player fights the same opponent more than once during the N rounds.
Print an assignment of integers to the playing fields satisfying this condition.
It can be proved that such an assignment always exists under the constraints given.

Constraints

1 \leq M

M \times 2 +1 \leq N \leq 200000

Input

Input is given from Standard Input in the following format:

N M

Output

Print M lines in the format below.
The i-th line should contain the two integers a_i and b_i assigned to the i-th playing field.

a_1 b_1
a_2 b_2
:
a_M b_M

Sample Input 1

4 1

Sample Output 1

2 3

Let us call the four players A, B, C, and D, and assume that they are initially given the integers 1, 2, 3, and 4, respectively.

The 1-st round is fought by B and C, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 2, 3, 4, and 1, respectively.

The 2-nd round is fought by A and B, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 3, 4, 1, and 2, respectively.

The 3-rd round is fought by D and A, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 4, 1, 2, and 3, respectively.

The 4-th round is fought by C and D, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 1, 2, 3, and 4, respectively.

No player fights the same opponent more than once during the four rounds, so this solution will be accepted.

Sample Input 2

7 3

Sample Output 2

1 6
2 5
3 4


        my $S = $ARGV[0];
        my $W = $ARGV[1];

        if( $W >= $S )
        {
        	print"unsafe";
        }
        else
        {
        	print"safe";
        }

        exit(0);

Score : 100 points

Problem Statement

There are S sheep and W wolves.

If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep.

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Constraints

1 \leq S \leq 100

1 \leq W \leq 100

Input

Input is given from Standard Input in the following format:

S W

Output

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Sample Input 1

4 5

Sample Output 1

unsafe

There are four sheep and five wolves. The number of wolves is not less than that of sheep, so they will attack them.

Sample Input 2

100 2

Sample Output 2

safe

Many a sheep drive away two wolves.

Sample Input 3

10 10

Sample Output 3

unsafe


#! /usr/bin/perl

use strict;
use warnings;

my $in = <>;
chomp($in);
my ($s, $w) = split(/\s+/, $in);

if($s < $w) {
    print "unsafe\n";
}else{
    print "safe\n";
}


Score : 300 points

Problem Statement

A company has N members, who are assigned ID numbers 1, ..., N.

Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.

When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.

You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Constraints

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

1 \leq A_i < i

Input

Input is given from Standard Input in the following format:

N
A_2 ... A_N

Output

For each of the members numbered 1, 2, ..., N, print the number of immediate subordinates it has, in its own line.

Sample Input 1

5
1 1 2 2

Sample Output 1

2
2
0
0
0

The member numbered 1 has two immediate subordinates: the members numbered 2 and 3.

The member numbered 2 has two immediate subordinates: the members numbered 4 and 5.

The members numbered 3, 4, and 5 do not have immediate subordinates.

Sample Input 2

10
1 1 1 1 1 1 1 1 1

Sample Output 2

9
0
0
0
0
0
0
0
0
0

Sample Input 3

7
1 2 3 4 5 6

Sample Output 3

1
1
1
1
1
1
0


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

chomp (my ($n, $k) = split / /, <STDIN>);
my $MOD = 1e9 + 7;
my $modpow = sub {
  my ($a, $n) = @_;
  my $r = 1;
  while ($n > 0) {
    $r = $r * $a % $MOD if $n & 1;
    $a = $a * $a % $MOD;
    $n >>= 1;
  }
  return $r;
};
my $modinv = sub {
  my $a = shift;
  $modpow->($a, $MOD - 2);
};
my (@fac, @finv);
$fac[0] = 1;
$finv[0] = 1;
for my $i (1..$n+1) {
  $fac[$i] = $fac[$i-1] * $i % $MOD;
  $finv[$i] = $modinv->($fac[$i]);
}
my $combi = sub {
  my ($n, $k) = @_;
  $fac[$n] * ($finv[$n-$k] * $finv[$k] % $MOD) % $MOD;
};
my $sum = 0;
for my $i ($k..$n+1) {
  $sum += $combi->($n+1, $i);
  $sum %= $MOD;
}
for my $i ($k..$n-1) {
  $sum += $MOD - $combi->($n-1, $i);
  $sum %= $MOD;
}
say $sum;


Score : 200 points

Problem Statement

Let us define the FizzBuzz sequence a_1,a_2,... as follows:

If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.

If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.

If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.

If none of the above holds, a_i=i.

Find the sum of all numbers among the first N terms of the FizzBuzz sequence.

Constraints

1 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of all numbers among the first N terms of the FizzBuzz sequence.

Sample Input 1

15

Sample Output 1

60

The first 15 terms of the FizzBuzz sequence are:

1,2,\mbox{Fizz},4,\mbox{Buzz},\mbox{Fizz},7,8,\mbox{Fizz},\mbox{Buzz},11,\mbox{Fizz},13,14,\mbox{FizzBuzz}

Among them, numbers are 1,2,4,7,8,11,13,14, and the sum of them is 60.

Sample Input 2

1000000

Sample Output 2

266666333332

Watch out for overflow.


Score : 600 points

Problem Statement

Given is an integer sequence A_1, ..., A_N of length N.

We will choose exactly \left\lfloor \frac{N}{2} \right\rfloor elements from this sequence so that no two adjacent elements are chosen.

Find the maximum possible sum of the chosen elements.

Here \lfloor x \rfloor denotes the greatest integer not greater than x.

Constraints

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

|A_i|\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the maximum possible sum of the chosen elements.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

12

Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value.

Sample Input 2

5
-1000 -100 -10 0 10

Sample Output 2

0

Choosing -10 and 10 makes the sum 0, which is the maximum possible value.

Sample Input 3

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

Watch out for overflow.

Sample Input 4

27
18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49

Sample Output 4

295


use strict;
use warnings;
use List::Util qw(min);

chomp(my ($n, $k) = split(/ /, <>));

print min($n % $k, $k - ($n % $k))."\n";

Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0


use List::Util qw(max min sum);

my ($n,$k) = (split(/\s+/, <>));

$n %= $k;
print min($n, $k-$n), "\n";


Score : 400 points

Problem Statement

A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:

In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.

For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.

You are given a positive integer K. Find the K-th smallest lunlun number.

Constraints

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer.

Sample Input 1

15

Sample Output 1

23

We will list the 15 smallest lunlun numbers in ascending order:

1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
21,
22,
23.

Thus, the answer is 23.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

13

Sample Output 3

21

Sample Input 4

100000

Sample Output 4

3234566667

Note that the answer may not fit into the 32-bit signed integer type.


my $s = <STDIN>;
my @dat =  split(/ /,$s);
my $K =  $dat[0] ;

my @data;
my $count=9;
for(my $i=1;$i<10;$i++){
    #$data[0][$i-1]=$i;
    $data[$i-1]=$i;
}
if($K<10){ print "$data[0][$K-1]";exit;}
while(){
  my $num = @data;
  my @newdata;
  for(my $j=0;$j<$num;$j++){
    for(my $m=-1;$m<=1;$m++){
        my $tn = substr($data[$j], -1, 1);
        my $nx =  $tn +$m;
        if($nx == -1 ){next;}
        if($nx == 10 ){next;}
        my $newnum = "$data[$j]$nx";
        $count++;
        if($count >= $K){print "$newnum";exit}
        #print "$count $newnum\n";
        push ( @newdata,$newnum);
    }
  }
  @data = @newdata;
}


Score : 600 points

Problem Statement

Given is a positive integer N.

We will choose an integer K between 2 and N (inclusive), then we will repeat the operation below until N becomes less than K.

Operation: if K divides N, replace N with N/K; otherwise, replace N with N-K.

In how many choices of K will N become 1 in the end?

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of choices of K in which N becomes 1 in the end.

Sample Input 1

6

Sample Output 1

3

There are three choices of K in which N becomes 1 in the end: 2, 5, and 6.

In each of these choices, N will change as follows:

When K=2: 6 \to 3 \to 1

When K=5: 6 \to 1

When K=6: 6 \to 1

Sample Input 2

3141

Sample Output 2

13

Sample Input 3

314159265358

Sample Output 3

9


my ($k,$n) = split(/ /,<STDIN>);
my @a = split(/ /,<STDIN>);my @diff= ();my $diff;
while ($n > 0){$diff = abs($a[$n-1]-$a[$n-2]);if($diff>$k-$diff){push(@diff,$k-$diff)} else{push(@diff,$diff)};$n--;}
my @sorted = sort{$a<=>$b}@diff;
print $diff[0],"\n";

Score : 100 points

Problem Statement

A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal.

Given a string S, determine whether it is coffee-like.

Constraints

S is a string of length 6 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is coffee-like, print Yes; otherwise, print No.

Sample Input 1

sippuu

Sample Output 1

Yes

In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal.

Sample Input 2

iphone

Sample Output 2

No

Sample Input 3

coffee

Sample Output 3

Yes


Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

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

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.


#!/usr/bin/env perl
use 5.10.0;
use utf8;
use strict;
use warnings;
use open qw( :utf8 :std );
use Data::Dumper;
use List::Util qw( max sum );

chomp( my $kn = readline );
chomp( my $an = readline );
#warn "\$kn: $kn";
#warn "\$an: $an";

my( $k, $n ) = split m{\s+}, $kn;
#warn "\$k: $k";
#warn "\$n: $n";
my @a = split m{\s+}, $an;
#warn Dumper \@a;

my $last = $k - $a[-1] + $a[0];

my @distances;
my $previous = shift @a;

while ( @a ) {
    my $current = shift @a;
    my $d = $current - $previous;
    push @distances, $d;
    $previous = $current;
}

push @distances, $last;
#warn Dumper \@distances;

my $max = max @distances;
my $sum = sum @distances;

my $movement = $sum - $max;

say $movement;

exit;


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

chomp (my ($n, $x, $y) = split / /, <STDIN>);
my %h;
for my $i (1..$n) {
  if ($i==$x) {
    if ($i==1) {
      $h{$i} = [$i+1, $y];
    } elsif ($i==$n) {
      $h{$i} = [$i-1, $y]
    } else {
      $h{$i} = [$y, $i+1, $i-1];
    }
  } elsif ($i==$y) {
    if ($i==1) {
      $h{$i} = [$i+1, $x];
    } elsif ($i==$n) {
      $h{$i} = [$i-1, $x]
    } else {
      $h{$i} = [$x, $i+1, $i-1];
    }
  } else {
    if ($i==1) {
      $h{$i} = [$i+1];
    } elsif ($i==$n) {
      $h{$i} = [$i-1];
    } else {
      $h{$i} = [$i-1, $i+1];
    }
  }
}
sub member {
    my ($n, $xs) = @_;
    for my $x (@$xs) {
        return 1 if $x eq $n;
    }
    0;
}
my %bh;
sub bfs {
    my ($start, $goal) = @_;
    my @que = ([$start]);
    while (@que > 0) {
        my $path = shift @que;
        my $x = $path->[-1];
        if ($goal eq $x) {
            if (exists $bh{$start.'_'.$goal}) {
              $bh{$start.'_'.$goal} = -1 + scalar @$path if $bh{$start.'_'.$goal} > -1 + scalar @$path;
            } else {
              $bh{$start.'_'.$goal} = -1 + scalar @$path;
            }
        } else {
            my $ls = $h{$x};
            for my $y (@$ls) {
                if (!member($y, $path)) {
                    my $new_path = [@$path];
                    push @$new_path, $y;
                    push @que, $new_path;
                }
            }
        }
    }
}
for my $i (1..$n-1) {
  for my $j ($i+1..$n) {
    bfs($i, $j);
  }
}
my %ans;
$ans{$_}++ for (values %bh);
for my $i (1..$n-1) {
  my $ac = $ans{$i} + 0;
  say $ac;
}

Score : 400 points

Problem Statement

We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:

For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1.

There is an edge between Vertex X and Vertex Y.

For each k=1,2,...,N-1, solve the problem below:

Find the number of pairs of integers (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j in G is k.

Constraints

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

1 \leq X,Y \leq N

X+1 < Y

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

For each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem.

Sample Input 1

5 2 4

Sample Output 1

5
4
1
0

The graph in this input is as follows:

There are five pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\,,(2,3)\,,(2,4)\,,(3,4)\,,(4,5).

There are four pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\,,(1,4)\,,(2,5)\,,(3,5).

There is one pair (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5).

There are no pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 4.

Sample Input 2

3 1 3

Sample Output 2

3
0

The graph in this input is as follows:

Sample Input 3

7 3 7

Sample Output 3

7
8
4
2
0
0

Sample Input 4

10 4 8

Sample Output 4

10
12
10
8
4
1
0
0
0


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

chomp (my ($n, $x, $y) = split / /, <STDIN>);
my @ans;
for my $i (1..$n) {
  for my $j ($i+1..$n) {
    my $d = $j - $i;
    my $s = abs($i - $x) + 1 + abs($j - $y);
    $ans[min($d, $s)]++;
  }
}
for my $i (1..$n-1) {
  say $ans[$i] + 0;
}


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

chomp (my ($n, $x, $y) = split / /, <STDIN>);
my %h;
for my $i (1..$n) {
  if ($x == $i) {
    if ($y > $i) {
      $h{$i} = [$i-1, $i+1, $y];
    } else {
      $h{$i} = [$y, $i+1, $i-1];
    }
  } else {
    if ($i==1) {
      $h{$i} = [$i+1];
    } elsif ($i==$n) {
      $h{$i} = [$i-1];
    } else {
      $h{$i} = [$i-1, $i+1];
    }
  }
}
sub member {
    my ($n, $xs) = @_;
    for my $x (@$xs) {
        return 1 if $x eq $n;
    }
    0;
}
my %bh;
sub bfs {
    my ($start, $goal) = @_;
    my @que = ([$start]);
    while (@que > 0) {
        my $path = shift @que;
        my $x = $path->[-1];
        if ($goal eq $x) {
            if (exists $bh{$start.'_'.$goal}) {
              $bh{$start.'_'.$goal} = -1 + scalar @$path if $bh{$start.'_'.$goal} > -1 + scalar @$path;
            } else {
              $bh{$start.'_'.$goal} = -1 + scalar @$path;
            }
        } else {
            my $ls = $h{$x};
            for my $y (@$ls) {
                if (!member($y, $path)) {
                    my $new_path = [@$path];
                    push @$new_path, $y;
                    push @que, $new_path;
                }
            }
        }
    }
}
for my $i (1..$n-1) {
  for my $j ($i+1..$n) {
    bfs($i, $j);
  }
}
my %ans;
$ans{$_}++ for (values %bh);
for my $i (1..$n-1) {
  my $ac = $ans{$i} + 0;
  say $ac;
}


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

chomp (my ($n, $x, $y) = split / /, <STDIN>);
my %h;
for my $i (1..$n) {
  if ($x == $i) {
    if ($y > $i) {
      $h{$i} = [$i+1, $y];
    } else {
      $h{$i} = [$y, $i+1];
    }
  } else {
    $h{$i} = [$i+1];
  }
}
sub member {
    my ($n, $xs) = @_;
    for my $x (@$xs) {
        return 1 if $x eq $n;
    }
    0;
}
my %bh;
sub bfs {
    my ($start, $goal) = @_;
    my @que = ([$start]);
    while (@que > 0) {
        my $path = shift @que;
        my $x = $path->[-1];
        if ($goal eq $x) {
          say @$path;
            if (exists $bh{$start.'_'.$goal}) {
              $bh{$start.'_'.$goal} = -1 + scalar @$path if $bh{$start.'_'.$goal} > -1 + scalar @$path;
            } else {
              $bh{$start.'_'.$goal} = -1 + scalar @$path;
            }
        } else {
            my $ls = $h{$x};
            for my $y (@$ls) {
                if (!member($y, $path)) {
                    my $new_path = [@$path];
                    push @$new_path, $y;
                    push @que, $new_path;
                }
            }
        }
    }
}
for my $i (1..$n-1) {
  for my $j ($i+1..$n) {
    bfs($i, $j);
  }
}
my %ans;
$ans{$_}++ for (values %bh);
for my $i (1..$n-1) {
  my $ac = $ans{$i} + 0;
  say $ac;
}


    my $input = <STDIN>;
    chomp $input;
    my ($b, $c) = split / /, $input;

	my $ans = $b * ($b - 1) / 2 + $c * ($c - 1) /2;

	print "$ans\n";

Score : 100 points

Problem Statement

We have N+M balls, each of which has an integer written on it.

It is known that:

The numbers written on N of the balls are even.

The numbers written on M of the balls are odd.

Find the number of ways to choose two of the N+M balls (disregarding order) so that the sum of the numbers written on them is even.

It can be shown that this count does not depend on the actual values written on the balls.

Constraints

0 \leq N,M \leq 100

2 \leq N+M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

2 1

Sample Output 1

1

For example, let us assume that the numbers written on the three balls are 1,2,4.

If we choose the two balls with 1 and 2, the sum is odd;

If we choose the two balls with 1 and 4, the sum is odd;

If we choose the two balls with 2 and 4, the sum is even.

Thus, the answer is 1.

Sample Input 2

4 3

Sample Output 2

9

Sample Input 3

1 1

Sample Output 3

0

Sample Input 4

13 3

Sample Output 4

81

Sample Input 5

0 3

Sample Output 5

3


sub parin {
	$s = @_[0];
	return $s eq reverse($s)
}

use integer;
$s = <>;
chop($s);
$n = length($s);
print parin($s) && parin(substr($s, 0, ($n-1)/2)) && parin(substr($s, ($n+1)/2)) ? Yes : No

Score : 200 points

Problem Statement

A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:

S is a palindrome.

Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome.

The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.

Determine whether S is a strong palindrome.

Constraints

S consists of lowercase English letters.

The length of S is an odd number between 3 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

If S is a strong palindrome, print Yes;
otherwise, print No.

Sample Input 1

akasaka

Sample Output 1

Yes

S is akasaka.

The string formed by the 1-st through the 3-rd characters is aka.

The string formed by the 5-th through the 7-th characters is aka.
All of these are palindromes, so S is a strong palindrome.

Sample Input 2

level

Sample Output 2

No

Sample Input 3

atcoder

Sample Output 3

No


chomp($str = <STDIN>);
@str = ("", split(//, $str));

$n = scalar(@str) / 2;
$a = join("", @str[1 .. $n - 1]);
$b = join("", @str[$n + 1 .. $#str]);
$b = reverse($b);

if($a ne $b){
	print "No\n";
	exit;
}

$n = (scalar(@str) - 2) / 2;
$a = join("", @str[1 .. $n]);
$n = (scalar(@str) + 2) / 2;
$b = join("", @str[$n .. $#str]);

for $one (split(/,/, "$a,$b")){

	$n = (length($one) + 1) / 2;
	$a = substr($one, 0, $n - 1);
	$b = substr($one, $n, length($one) - 1);
	$b = reverse($b);

	if($a ne $b){
		print "No\n";
		exit;
	}

}

print "Yes\n";


Score : 300 points

Problem Statement

Given is a positive integer L.
Find the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.

Constraints

1 ≤ L ≤ 1000

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

Print the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.
Your output is considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

3

Sample Output 1

1.000000000000

For example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.

On the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater.

Sample Input 2

999

Sample Output 2

36926037.000000000000


chomp($n = <STDIN>);

$one = $n / 3;

printf ($one * $one * $one) . "\n";