var s1,s2:ansistring;
c:array['a'..'z']of 0..1;
f:array['a'..'z']of 'a'..'z';
i:longint;
begin
	readln(s1);
	readln(s2);
	fillchar(c,sizeof(c),0);
	for i:=1 to length(s1) do
	begin
		if c[s1[i]]=0 then
		begin
			f[s1[i]]:=s2[i];
			c[s1[i]]:=1;
		end
		else if s2[i]<>f[s1[i]] then begin write('No1');halt;end;
	end;
	fillchar(c,sizeof(c),0);
	for i:=1 to length(s1) do
	begin
		if c[s2[i]]=0 then
		begin
			f[s2[i]]:=s1[i];
			c[s2[i]]:=1;
		end
		else if s1[i]<>f[s2[i]] then begin write('No');halt;end;
	end;
	write('Yes');
end.


var
	n,m,ans,mo,p:int64;
	fac:array[0..131072]of int64;
	i:Longint;
function power(a,b:int64):int64;
var ret:int64;
begin
	if b=0 then power:=1
	else begin
		ret:=power(a*a mod 1000000007,b div 2);
		if b mod 2=1 then ret:=ret*a mod 1000000007;
		power:=ret;
	end;
end;
begin
	read(n,m);
	ans:=1;
	mo:=1000000007;
	fac[0]:=1;
	for i:=1 to 131071 do fac[i]:=fac[i-1]*i mod mo;
	i:=2;
	while i*i<=m do begin
		if m mod i=0 then begin
			p:=0;
			while m mod i=0 do begin
				m:=m div i;
				inc(p);
			end;
			ans:=ans*fac[n+p-1]mod mo*power(fac[n-1],mo-2)mod mo*power(fac[p],mo-2)mod mo;
		end;
		inc(i);
	end;
	if m>1 then ans:=ans*fac[n]mod mo*power(fac[n-1],mo-2)mod mo;
	writeln(ans);
end.

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

Score : 400 points

Problem Statement

You are given positive integers N and M.

How many sequences a of length N consisting of positive integers satisfy a_1 \times a_2 \times ... \times a_N = M? Find the count modulo 10^9+7.

Here, two sequences a' and a'' are considered different when there exists some i such that a_i' \neq a_i''.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of the sequences consisting of positive integers that satisfy the condition, modulo 10^9 + 7.

Sample Input 1

2 6

Sample Output 1

4

Four sequences satisfy the condition: \{a_1, a_2\} = \{1, 6\}, \{2, 3\}, \{3, 2\} and \{6, 1\}.

Sample Input 2

3 12

Sample Output 2

18

Sample Input 3

100000 1000000000

Sample Output 3

957870001

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.


var i,j,len:longint;
    k,l,m,n,q,w,x,ans,rst:int64;
    a,b,s:Array[0..1000000] of int64;
begin
   readln(n,x);
   for i:=1 to n do read(a[i]);
   for i:=1 to n do s[i]:=s[i-1]+a[i];
   readln;
   rst:=(maxlongint-100)*maxlongint;
   for len:=1 to n do begin
       ans:=0;
       ans:=ans+(s[n]-s[n-len])*5;
       w:=5;j:=n-len;
       while j>len do begin
           ans:=ans+w*(s[j]-s[j-len]);
           j:=j-len;
           w:=w+2;
       end;
       ans:=ans+w*s[j]+len*x;
       if ans<rst then rst:=ans;
   end;
   writeln(rst+n*x);
end.

Score : 700 points

Problem Statement

Snuke has decided to use a robot to clean his room.

There are N pieces of trash on a number line.
The i-th piece from the left is at position x_i.
We would like to put all of them in a trash bin at position 0.

For the positions of the pieces of trash, 0 < x_1 < x_2 < ... < x_{N} \leq 10^{9} holds.

The robot is initially at position 0.
It can freely move left and right along the number line, pick up a piece of trash when it comes to the position of that piece, carry any number of pieces of trash and put them in the trash bin when it comes to position 0. It is not allowed to put pieces of trash anywhere except in the trash bin.

The robot consumes X points of energy when the robot picks up a piece of trash, or put pieces of trash in the trash bin. (Putting any number of pieces of trash in the trash bin consumes X points of energy.)
Also, the robot consumes (k+1)^{2} points of energy to travel by a distance of 1 when the robot is carrying k pieces of trash.

Find the minimum amount of energy required to put all the N pieces of trash in the trash bin.

Constraints

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

0 < x_1 < ... < x_N \leq 10^9

1 \leq X \leq 10^9

All values in input are integers.

Partial Scores

400 points will be awarded for passing the test set satisfying N \leq 2000.

Input

Input is given from Standard Input in the following format:

N X
x_1 x_2 ... x_{N}

Output

Print the answer.

Sample Input 1

2 100
1 10

Sample Output 1

355

Travel to position 10 by consuming 10 points of energy.

Pick up the piece of trash by consuming 100 points of energy.

Travel to position 1 by consuming 36 points of energy.

Pick up the piece of trash by consuming 100 points of energy.

Travel to position 0 by consuming 9 points of energy.

Put the two pieces of trash in the trash bin by consuming 100 points of energy.

This strategy consumes a total of 10+100+36+100+9+100=355 points of energy.

Sample Input 2

5 1
1 999999997 999999998 999999999 1000000000

Sample Output 2

19999999983

Sample Input 3

10 8851025
38 87 668 3175 22601 65499 90236 790604 4290609 4894746

Sample Output 3

150710136

Sample Input 4

16 10
1 7 12 27 52 75 731 13856 395504 534840 1276551 2356789 9384806 19108104 82684732 535447408

Sample Output 4

3256017715


type
	arr=array[1..4] of longint;
var
	f:array[0..200000] of arr;
	n,m,s,k,x,y,i,j:longint;
	t:array[0..600,0..600] of boolean;
procedure work(a,b,c,d:longint);
begin
	s:=s+abs(a-c)+abs(b-d);
	inc(k);
	f[k,1]:=a;
	f[k,2]:=b;
	f[k,3]:=c;
	f[k,4]:=d;
	t[a,b]:=false;
	t[c,d]:=false;
end;
procedure print(x:arr);
var
	i:longint;
begin
	if x[1]<x[3] then
		for i:=x[1] to x[3]-1 do writeln(i,' ',x[2],' ',i+1,' ',x[2])
	else
		for i:=x[1] downto x[3]+1 do writeln(i,' ',x[2],' ',i-1,' ',x[2]);
	if x[2]<x[4] then
		for i:=x[2] to x[4]-1 do writeln(x[3],' ',i,' ',x[3],' ',i+1)
	else
		for i:=x[2] downto x[4]+1 do writeln(x[3],' ',i,' ',x[3],' ',i-1);
end;
begin
	readln(n,m);
	for i:=1 to n do
	begin
		for j:=1 to m do
		begin
			read(x);
			t[i,j]:=(x mod 2=1);
		end;
		readln;
	end;
	s:=0; x:=0; y:=0;
	for i:=1 to n do
		if i mod 2=1 then
		begin
			for j:=1 to m do
				if t[i,j] then
					if (x<>0) and (y<>0) then
					begin
						work(x,y,i,j);
						x:=0; y:=0;
					end
					else
					begin
						x:=i; y:=j;
					end;
		end
		else
		begin
			for j:=m downto 1 do
				if t[i,j] then
					if (x<>0) and (y<>0) then
					begin
						work(x,y,i,j);
						x:=0; y:=0;
					end
					else
					begin
						x:=i; y:=j;
					end;
		end;
	writeln(s);
	for i:=1 to k do print(f[i]);
end.

Score : 400 points

Problem Statement

There is a grid of square cells with H horizontal rows and W vertical columns. The cell at the i-th row and the j-th column will be denoted as Cell (i, j).

In Cell (i, j), a_{ij} coins are placed.

You can perform the following operation any number of times:

Operation: Choose a cell that was not chosen before and contains one or more coins, then move one of those coins to a vertically or horizontally adjacent cell.

Maximize the number of cells containing an even number of coins.

Constraints

All values in input are integers.

1 \leq H, W \leq 500

0 \leq a_{ij} \leq 9

Input

Input is given from Standard Input in the following format:

H W
a_{11} a_{12} ... a_{1W}
a_{21} a_{22} ... a_{2W}
:
a_{H1} a_{H2} ... a_{HW}

Output

Print a sequence of operations that maximizes the number of cells containing an even number of coins, in the following format:

N
y_1 x_1 y_1' x_1'
y_2 x_2 y_2' x_2'
:
y_N x_N y_N' x_N'

That is, in the first line, print an integer N between 0 and H \times W (inclusive), representing the number of operations.

In the (i+1)-th line (1 \leq i \leq N), print four integers y_i, x_i, y_i' and x_i' (1 \leq y_i, y_i' \leq H and 1 \leq x_i, x_i' \leq W), representing the i-th operation. These four integers represents the operation of moving one of the coins placed in Cell (y_i, x_i) to a vertically or horizontally adjacent cell, (y_i', x_i').

Note that if the specified operation violates the specification in the problem statement or the output format is invalid, it will result in Wrong Answer.

Sample Input 1

2 3
1 2 3
0 1 1

Sample Output 1

3
2 2 2 3
1 1 1 2
1 3 1 2

Every cell contains an even number of coins after the following sequence of operations:

Move the coin in Cell (2, 2) to Cell (2, 3).

Move the coin in Cell (1, 1) to Cell (1, 2).

Move one of the coins in Cell (1, 3) to Cell (1, 2).

Sample Input 2

3 2
1 0
2 1
1 0

Sample Output 2

3
1 1 1 2
1 2 2 2
3 1 3 2

Sample Input 3

1 5
9 9 9 9 9

Sample Output 3

2
1 1 1 2
1 3 1 4


var
    n,m,i,j,x,cnt:longint;
    a:array[0..501,0..501] of longint;
    x1,y1,x2,y2:array[0..25001] of longint;
begin
    readln(n,m);
    cnt:=0;
    for i:=1 to n do
        begin
            for j:=1 to m-1 do
                begin
                    read(x);inc(a[i,j],x);
                    if odd(a[i,j]) then
                        begin
                            inc(cnt);
                            x1[cnt]:=i;x2[cnt]:=i;y1[cnt]:=j;y2[cnt]:=j+1;
                            inc(a[i,j+1]);
                        end;
                end;
            read(x);inc(a[i,m],x);
            if odd(a[i,m]) then
                begin
                    inc(cnt);
                    x1[cnt]:=i;x2[cnt]:=i+1;y1[cnt]:=m;y2[cnt]:=m;
                    inc(a[i+1,m]);
                end;
        end;
    if odd(a[n,m]) then dec(cnt);
    writeln(cnt);
    for i:=1 to cnt do
        writeln(x1[i],' ',y1[i],' ',x2[i],' ',y2[i]);
end.


type
	arr=array[1..4] of longint;
var
	f:array[0..3000] of arr;
	n,m,s,k,x,y,i,j:longint;
	t:array[0..600,0..600] of boolean;
procedure work(a,b,c,d:longint);
begin
	s:=s+abs(a-c)+abs(b-d);
	inc(k);
	f[k,1]:=a;
	f[k,2]:=b;
	f[k,3]:=c;
	f[k,4]:=d;
	t[a,b]:=false;
	t[c,d]:=false;
end;
procedure print(x:arr);
var
	i:longint;
begin
	if x[1]<x[3] then
		for i:=x[1] to x[3]-1 do writeln(i,' ',x[2],' ',i+1,' ',x[2])
	else
		for i:=x[1] downto x[3]+1 do writeln(i,' ',x[2],' ',i-1,' ',x[2]);
	if x[2]<x[4] then
		for i:=x[2] to x[4]-1 do writeln(x[3],' ',i,' ',x[3],' ',i+1)
	else
		for i:=x[2] downto x[4]+1 do writeln(x[3],' ',i,' ',x[3],' ',i-1);
end;
begin
	read(n,m);
	for i:=1 to n do
		for j:=1 to m do
		begin
			read(x);
			t[i,j]:=(x mod 2=1);
		end;
	s:=0; x:=0; y:=0;
	for i:=1 to n do
		if i mod 2=1 then
		begin
			for j:=1 to m do
				if t[i,j] then
					if (x<>0) and (y<>0) then
					begin
						work(x,y,i,j);
						x:=0; y:=0;
					end
					else
					begin
						x:=i; y:=j;
					end;
		end
		else
		begin
			for j:=m downto 1 do
				if t[i,j] then
					if (x<>0) and (y<>0) then
					begin
						work(x,y,i,j);
						x:=0; y:=0;
					end
					else
					begin
						x:=i; y:=j;
					end;
		end;
	writeln(s);
	for i:=1 to k do print(f[i]);
end.


var
  a:array[-10..510,-10..510]of longint;
  i,j,n,m:longint;
begin
  readln(n,m);
  for i:=1 to n do
    for j:=1 to m do
      begin
        read(a[i,j]);
        if j=m then readln;
      end;
  writeln(n*m-1);
  for i:=1 to n do
    for j:=1 to m-1 do
      writeln(i,' ',j,' ',i,' ',j+1);
  for i:=1 to n-1 do
    writeln(i,' ',m,' ',i+1,' ',m);
end.


var
	s:int64;
	a,n,k:longint;
function ans(x:longint):int64;
begin
	if x mod k=0 then exit(n div k)
	else exit(n div k+1);
end;
begin
	read(n,k);
	s:=0;
	for a:=1 to n do
		if (a mod k=0) or (a mod k=k div 2) then
			s:=s+ans(a)*ans(k-a);
	writeln(s);
end.

Score : 300 points

Problem Statement

You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.
The order of a,b,c does matter, and some of them can be the same.

Constraints

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

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.

Sample Input 1

3 2

Sample Output 1

9

(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1) and (3,3,3) satisfy the condition.

Sample Input 2

5 3

Sample Output 2

1

Sample Input 3

31415 9265

Sample Output 3

27

Sample Input 4

35897 932

Sample Output 4

114191


var
	a,b,c:longint;
	n,k,s:int64;
function ans(x:longint):int64;
begin
	if x mod k=0 then exit(n div k)
	else exit(n div k+1);
end;
begin
	read(n,k);
	s:=0;
	for a:=1 to n do
		if (a mod k=0) or (k mod 2=0) and (a mod k=k div 2) then
			s:=s+ans(a)*ans(k-a);
	writeln(s);
end.


var n,k,i:longint;
a:array[1..3]of longint;
b:array[1..200000]of longint;
count,m:longint;
procedure dfs(x:integer);
var j:longint;
begin
	if x>3 then
	begin
		if (a[1]+a[3]) mod k=0 then inc(count);
		exit;
	end;
	for j:=1 to m do
		if ((b[j]-a[x-1])<=n) and ((b[j]-a[x-1])>0) then
		begin
			a[x]:=b[j]-a[x-1];
			dfs(x+1);
		end
		else if b[j]-a[x-1]>n then exit;
end;
begin
	read(n,k);
	m:=2*n div k;
	for i:=1 to m do b[i]:=i*k;
	for i:=1 to n do
	begin
		a[1]:=i;
		dfs(2);
	end;
	write(count);
end.


var
  i,k,n,q,j:longint;
  ans:qword;
begin
  readln(n,k);
  i:=k div 2;
  while i<=n do
  begin
    j:=k-(i mod k);
    while j<=n do
    begin
	  q:=i mod k;
	  if q=0 then q:=k;
	  if ((i+q) mod k=0) then
      while q<=n do
	  begin
         if ((i+q) mod k=0)and((q+j) mod k=0) then begin ans:=ans+1; q:=q+k; continue; end;
		 inc(q);
	  end;
	  j:=j+k;
	end;
	inc(i);
  end;
  writeln(ans);
end.


var
	s:int64;
	n,k,i,x,y,t,a,p:longint;
	f,f1:array[-200010..200010] of longint;
function min(a,b:longint):longint;
begin
	if a<b then exit(a) else exit(b);
end;
begin
	read(n,k);
	x:=0; y:=0;
	t:=1; p:=0;
	fillchar(f1,sizeof(f1),$7f);
	for i:=1 to n do
	begin
		read(a);
		if a>0 then inc(x);
		if a<0 then inc(y);
		if a=0 then dec(k);
		if (a>0) and (f[i-1]<=0) then t:=i;
		if a<>0 then
		begin
			inc(p);
			f[p]:=a;
		end;
	end;
	if x=0 then
	begin
		writeln(-f[p-k+1]);
		halt;
	end;
	if y=0 then
	begin
		writeln(f[k]);
		halt;
	end;
	if p=k then
	begin
		if abs(f[1])<abs(f[p]) then writeln(abs(f[1])*2+abs(f[p]))
		else writeln(abs(f[p])*2+abs(f[1]));
		halt;
	end;
	s:=$7f;
	for i:=1 to p do f1[i-t]:=abs(f[i]);
	y:=0-y;
	if x>=k then s:=min(s,f1[k-1]);
	if 0-y>=k then s:=min(s,f1[0-k]);
	for i:=-min(-y,k) to -1 do
		if i*(k+i-1)<=0 then
			s:=min(s,f1[i]*2+f1[k+i-1]);
	for i:=1 to min(x-1,k-1) do
		if i*(k-i-1)>=0 then
			s:=min(s,f1[i]*2+f1[0-(k-i-1)]);
	writeln(s);
end.

Score : 100 points

Problem Statement

There is an N-car train.

You are given an integer i. Find the value of j such that the following statement is true: "the i-th car from the front of the train is the j-th car from the back."

Constraints

1 \leq N \leq 100

1 \leq i \leq N

Input

Input is given from Standard Input in the following format:

N i

Output

Print the answer.

Sample Input 1

4 2

Sample Output 1

3

The second car from the front of a 4-car train is the third car from the back.

Sample Input 2

1 1

Sample Output 2

1

Sample Input 3

15 11

Sample Output 3

5


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k,min:Integer;
  l:array of array of char;
  s,s123,s1,s2,s3:string;
  al:string = 'abcdefghijklmnopqrstuvwxyz';
  tf,tfl,tfr:Boolean;
  bh,bw:array of Boolean;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    Read(h);
    Readln(w);
    count:=0;
    max  :=0;
    min  :=100;

    if (h=1)and(w=1) then begin
      Writeln('#');
      exit
    end;

    SetLength(bh,h);
    SetLength(bw,w);

    SetLength(l,h);
    for i := 0 to h do
      SetLength(l[i],w);

    for i := 0 to h-1 do begin
      for j := 0 to w-1 do begin
        Read(l[i][j]);      //読み込む
        if l[i][j]='#' then //ついでに横一行を記憶
          bh[i]:=True;
      end;
      Readln;
    end;

    for j := 0 to w-1 do begin //縦
      for i := 0 to h-1 do
        if l[i][j]='#' then
          bw[j]:=True;
    end;

    for i := 0 to h-1 do begin
      if bh[i] then begin //消す行なら1行まるごと飛ばす
        for j := 0 to w-1 do begin
          if bw[j] then //消す列なら1個ずつまたぐ
            Write(l[i][j]);
        end;
        Writeln;
      end;
    end;

    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

There is a grid of squares with H horizontal rows and W vertical columns.
The square at the i-th row from the top and the j-th column from the left is represented as (i, j).
Each square is black or white.
The color of the square is given as an H-by-W matrix (a_{i, j}).
If a_{i, j} is ., the square (i, j) is white; if a_{i, j} is #, the square (i, j) is black.

Snuke is compressing this grid.
He will do so by repeatedly performing the following operation while there is a row or column that consists only of white squares:

Operation: choose any one row or column that consists only of white squares, remove it and delete the space between the rows or columns.

It can be shown that the final state of the grid is uniquely determined regardless of what row or column is chosen in each operation.
Find the final state of the grid.

Constraints

1 \leq H, W \leq 100

a_{i, j} is . or #.

There is at least one black square in the whole grid.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}...a_{1, W}
:
a_{H, 1}...a_{H, W}

Output

Print the final state of the grid in the same format as input (without the numbers of rows and columns); see the samples for clarity.

Sample Input 1

4 4
##.#
....
##.#
.#.#

Sample Output 1

###
###
.##

The second row and the third column in the original grid will be removed.

Sample Input 2

3 3
#..
.#.
..#

Sample Output 2

#..
.#.
..#

As there is no row or column that consists only of white squares, no operation will be performed.

Sample Input 3

4 5
.....
.....
..#..
.....

Sample Output 3

#

Sample Input 4

7 6
......
....#.
.#....
..#...
..#...
......
.#..#.

Sample Output 4

..#
#..
.#.
.#.
#.#


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k,min:Integer;
  l:array of array of char;
  s,s123,s1,s2,s3:string;
  al:string = 'abcdefghijklmnopqrstuvwxyz';
  tf,tfl,tfr:Boolean;
  bh,bw:array of Boolean;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    Read(h);
    Readln(w);
    count:=0;
    max  :=0;
    min  :=100;

    SetLength(bh,h);
    SetLength(bw,w);

    SetLength(l,h);
    for i := 0 to h do
      SetLength(l[i],w);

    for i := 0 to h-1 do begin
      for j := 0 to w-1 do begin
        Read(l[i][j]);      //読み込む
        if l[i][j]='#' then //ついでに横一行を記憶
          bh[i]:=True;
      end;
      Readln;
    end;

    for j := 0 to w-1 do begin //縦
      for i := 0 to h-1 do
        if l[i][j]='#' then
          bw[j]:=True;
    end;

    for i := 0 to h-1 do begin
      if bh[i] then begin //消す行なら1行まるごと飛ばす
        for j := 0 to w-1 do begin
          if bw[j] then //消す列なら1個ずつまたぐ
            Write(l[i][j]);
        end;
        Writeln;
      end;
    end;

    Writeln;
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.


var
  n,m,i,j:longint;
  x,y:array[1..1000]of boolean;
  a:array[1..1000,1..1000]of char;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do
      read(a[i,j]);
    readln;
  end;
  for i:=1 to n do
  begin
    x[i]:=true;
    for j:=1 to m do
      if a[i,j]='#' then x[i]:=false;
  end;
  for j:=1 to m do
  begin
    y[j]:=true;
    for i:=1 to n do
      if a[i,j]='#' then y[j]:=false;
  end;
  for i:=1 to n do
  begin
    for j:=1 to m do
      if not(x[i] or y[j]) then write(a[i,j]);
    if not x[i] then writeln;
  end;
end.


var x,y,i,j,cou,k:integer;
m:array[1..100,1..100]of char;
begin
  readln(x,y);
  for i:=1 to x do
  begin
    cou:=0;
    for j:=1 to y do
       begin read(m[i,j]);if m[i,j]='.' then inc(cou);end;
    if cou=y then for k:=1 to y do m[i,k]:='*';
    readln;
  end;
  for i:=1 to y do
  begin
    cou:=0;
    for j:=1 to x do
      if m[j,i]='#' then inc(cou);
    if cou=0 then for k:=1 to x do m[k,i]:='*';
  end;
  for i:=1 to x do
    begin
		cou:=0;
		for j:=1 to y do
		if m[i,j]<>'*' then begin write(m[i,j]);cou:=1;end;
		if cou=1 then writeln;
    end;
end.


var
    h,w,i,j:longint;
    a:array[0..101,0..101] of char;
    b,c:array[0..101] of boolean;
    flag:boolean;
begin
    readln(h,w);
    for i:=1 to h do
        begin
            for j:=1 to w do
                begin
                    read(a[i,j]);
                    if a[i,j]='#' then
                        begin
                            b[i]:=true;
                            c[j]:=true;
                        end;
                end;
            readln;
        end;
    for i:=1 to h do
        begin
            flag:=true;
            for j:=1 to w do
                if b[i] and c[j] then
                    begin
                        flag:=false;
                        write(a[i,j]);
                    end;
            if not flag then writeln;
        end;
end.


var
	x,y:array[0..110] of boolean;
	a:array[0..110] of string[110];
	n,m,i,j:longint;
	ch:string[110];
begin
	readln(n,m);
	fillchar(x,sizeof(x),true);
	fillchar(y,sizeof(y),true);
	for i:=1 to n do
	begin
		readln(a[i]);
		if pos('#',a[i])=0 then x[i]:=false;
	end;
	for i:=1 to m do
	begin
		ch:='';
		for j:=1 to n do ch:=ch+a[j,i];
		if pos('#',ch)=0 then y[i]:=false;
	end;
	for i:=1 to n do
	begin
		for j:=1 to m do
			if (x[i] and y[j]) then write(a[i,j]);
		if x[i] then writeln;
	end;
end.


var
  a:array[0..100010]of int64;
  k,l,r,n,ans:int64;
  i:longint;

function min(x,y:int64):int64;
begin
  if x<y then exit(x);
  exit(y);
end;

function max(x,y:int64):int64;
begin
  if x>y then exit(x);
  exit(y);
end;

begin
  readln(n,k);
  for i:=1 to n do
    read(a[i]);
  ans:=maxlongint;
  for i:=1 to n-k+1 do
  begin
    l:=min(a[i],0);
    r:=max(a[i+k-1],0);
    ans:=min(ans,r-l*2);
    ans:=min(ans,r*2-l);
  end;
  writeln(ans);
end.

Score : 300 points

Problem Statement

There are N candles placed on a number line.
The i-th candle from the left is placed on coordinate x_i.
Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning.
Snuke decides to light K of the N candles.

Now, he is at coordinate 0.
He can move left and right along the line with speed 1.
He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

8 5
-9 -7 -4 -3 1 2 3 4

Sample Output 4

10


var
  n,k,ans,t:int64;
  sum,x:array[0..100000]of int64;
  i:longint;

function min(x,y:int64):int64;
begin
  if x<y then exit(x);
  exit(y);
end;

function max(x,y:int64):int64;
begin
  if x>y then exit(x);
  exit(y);
end;

begin
  readln(n,k);
  for i:=1 to n do
    read(x[i]);
  ans:=maxlongint;
  for i:=1 to n do
  begin
    if i+k-1>n then continue;
    if (x[i]+x[i+k-1])=((abs(x[i])+abs(x[i+k-1])))
      then ans:=min(ans,max(abs(x[i]),abs(x[i+k-1])))
      else ans:=min(ans,min(abs(x[i]),abs(x[i+k-1]))+abs(x[i])+abs(x[i+k-1]));
  end;
  writeln(ans);
end.


var
	s:int64;
	n,k,i,x,y,t,a,p:longint;
	f,f1:array[-200010..200010] of longint;
function min(a,b:longint):longint;
begin
	if a<b then exit(a) else exit(b);
end;
begin
	read(n,k);
	x:=0; y:=0;
	t:=1; p:=0;
	fillchar(f1,sizeof(f1),$7f);
	for i:=1 to n do
	begin
		read(a);
		if a>0 then inc(x);
		if a<0 then inc(y);
		if a=0 then dec(k);
		if (a>0) and (f[i-1]<=0) then t:=i;
		if a<>0 then
		begin
			inc(p);
			f[p]:=a;
		end;
	end;
	if x=0 then
	begin
		writeln(-f[p-k+1]);
		halt;
	end;
	if y=0 then
	begin
		writeln(f[k]);
		halt;
	end;
	s:=$7f;
	for i:=1 to p do f1[i-t]:=abs(f[i]);
	y:=0-y;
	if x>=k then s:=min(s,f1[k-1]);
	if 0-y>=k then s:=min(s,f1[0-k]);
	for i:=-min(-y,k) to -1 do
		if i*(k+i-1)<=0 then
			s:=min(s,f1[i]*2+f1[k+i-1]);
	for i:=1 to min(x-1,k-1) do
		if i*(k-i-1)>=0 then
			s:=min(s,f1[i]*2+f1[0-(k-i-1)]);
	writeln(s);
end.


var
	s:int64;
	n,k,i,x,y,t,a,p:longint;
	f,f1:array[-200010..200010] of longint;
function min(a,b:longint):longint;
begin
	if a<b then exit(a) else exit(b);
end;
begin
	read(n,k);
	x:=0; y:=0;
	t:=1; p:=0;
	fillchar(f1,sizeof(f1),$7f);
	for i:=1 to n do
	begin
		read(a);
		if a>0 then inc(x);
		if a<0 then inc(y);
		if a=0 then dec(k);
		if (a>0) and (f[i-1]<=0) then t:=i;
		if a<>0 then
		begin
			inc(p);
			f[p]:=a;
		end;
	end;
	if x=0 then
	begin
		writeln(-f[p-k+1]);
		halt;
	end;
	s:=$7f;
	for i:=1 to p do f1[i-t]:=abs(f[i]);
	y:=0-y;
	if x>=k then s:=min(s,f1[k-1]);
	if 0-y>=k then s:=min(s,f1[0-k]);
	for i:=-min(-y,k) to -1 do
		if i*(k+i-1)<=0 then
			s:=min(s,f1[i]*2+f1[k+i-1]);
	for i:=1 to min(x-1,k-1) do
		if i*(k-i-1)>=0 then
			s:=min(s,f1[i]*2+f1[0-(k-i-1)]);
	writeln(s);
end.


var
	s:int64;
	n,k,i,x,y,t:longint;
	f,f1:array[-200010..200010] of longint;
function min(a,b:longint):longint;
begin
	if a<b then exit(a) else exit(b);
end;
begin
	readln(n,k);
	x:=0; y:=0;
	t:=1;
	fillchar(f1,sizeof(f1),$7f);
	for i:=1 to n do
	begin
		read(f[i]);
		if f[i]>0 then inc(x);
		if f[i]<0 then inc(y);
		if f[i]=0 then dec(k);
		if (f[i]>0) and (f[i-1]<=0) then t:=i;
	end;
	s:=$7f;
	for i:=1 to n do f1[i-t]:=abs(f[i]);
	y:=-y;
	if x>=k then s:=min(s,f[x-1]);
	if -y>=k then s:=min(s,f[-y]);
	for i:=-min(-y,k) to -1 do
		if i*(k+i-1)<=0 then
			s:=min(s,f1[i]*2+f1[k+i-1]);
	for i:=1 to min(x-1,k) do
		if i*(k-i-1)>=0 then
			s:=min(s,f1[i]*2+f1[-(k-i-1)]);
	writeln(s);
end.


var
	s:int64;
	n,k,i,x,y,t:longint;
	f,f1:array[-200010..200010] of longint;
function min(a,b:longint):longint;
begin
	if a<b then exit(a) else exit(b);
end;
begin
	readln(n,k);
	x:=0; y:=0;
	t:=1;
	fillchar(f1,sizeof(f1),$7f);
	for i:=1 to n do
	begin
		read(f[i]);
		if f[i]>0 then inc(x);
		if f[i]<0 then inc(y);
		if f[i]=0 then dec(k);
		if (f[i]>0) and (f[i-1]<=0) then t:=i;
	end;
	s:=$7f;
	for i:=1 to n do f1[i-t]:=abs(f[i]);
	y:=-y;
	for i:=-min(-y,k) to -1 do
		s:=min(s,f1[i]*2+f1[k+i-1]);
	for i:=0 to min(x,k) do
		s:=min(s,f1[i]*2+f1[-(k+i-1)]);
	writeln(s);
end.


var n,k,i,aa:longint;
min,count:int64;
a:array[1..100000]of longint;
begin
	read(n,k);
	min:=maxlongint;
	for i:=1 to n do begin read(a[i]);if a[i]<=0 then begin a[i]:=-a[i];aa:=i;end;end;
	for i:=1 to k do
		if (aa>=i) and  (n-aa>=k-i)then
		begin
			if a[aa-i+1]>a[aa+k-i] then count:=2*a[aa+k-i]+a[aa-i+1]
				else count:=2*a[aa-i+1]+a[aa+k-i];
		    if min>count then min:=count;
		end;
	if (n-aa>=k) and (a[aa+k]<min) then min:=a[aa+k];
	write(min);
end.


var n,k,i,aa,x,bb:longint;
min,count:int64;
a,b:array[1..100000]of longint;
begin
	read(n,k);
	min:=maxlongint;
	i:=0;
	read(x);
	while (x<0) and (i<n) do
	begin
		inc(i);
		a[i]:=-x;
		if i<n then read(x);
	end;
	if x=0 then begin dec(k);dec(n);if n-i>0 then read(x);end;
	aa:=i;bb:=n-aa;
	b[1]:=x;
	for i:=2 to n-aa do read(b[i]);
	for i:=0 to k do
		if (aa>=i) and (bb>=k-i) then
		begin
			if a[aa-i+1]>b[k-i] then count:=2*b[k-i]+a[aa-i+1]
				else count:=2*a[aa-i+1]+b[k-i];
		    if min>count then min:=count;
		end;
	write(min);
end.


var n,k,i,aa,x,bb:longint;
min,count:longint;
a,b:array[1..100000]of integer;
begin
	read(n,k);
	min:=maxlongint;
	i:=0;
	read(x);
	while (x<0) and (i<n) do
	begin
		inc(i);
		a[i]:=-x;
		if i<n then read(x);
	end;
	if x=0 then begin dec(k);dec(n);if k<>0 then read(x);end;
	aa:=i;bb:=n-aa;
	b[1]:=x;
	for i:=2 to n-aa do read(b[i]);
	for i:=0 to k do
		if (aa>=i) and (bb>=k-i) then
		begin
			if a[aa-i+1]>b[k-i] then count:=2*b[k-i]+a[aa-i+1]
				else count:=2*a[aa-i+1]+b[k-i];
		    if min>count then min:=count;
		end;
	write(min);
end.


var n,k,i,aa,x,count,min,bb:integer;
a,b:array[1..100000]of integer;
begin
	read(n,k);
	min:=maxint;
	i:=0;
	read(x);
	while (x<0) and (i<n) do
	begin
		inc(i);
		a[i]:=-x;
		if i<n then read(x);
	end;
	if x=0 then begin dec(k);dec(n);if k<>0 then read(x);end;
	aa:=i;bb:=n-aa;
	b[1]:=x;
	for i:=2 to n-aa do read(b[i]);
	for i:=0 to k do
		if (aa>=i) and (bb>=k-i) then
		begin
			if a[aa-i+1]>b[k-i] then count:=2*b[k-i]+a[aa-i+1]
				else count:=2*a[aa-i+1]+b[k-i];
		    if min>count then min:=count;
		end;
	write(min);
end.


var
    n,k,i,x:longint;
    ans:int64;
    a:array[0..100010] of int64;
function min(a,b:int64):int64;
begin
    if a<b then exit(a) else exit(b);
end;
function max(a,b:int64):int64;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    readln(n,k);
    for i:=1 to n do
        begin
            read(a[i]);
            if a[i]<=0 then x:=i;
        end;
    ans:=maxlongint;
    if x>=k then ans:=min(ans,abs(a[x-k+1]));
    if x+k<=n then ans:=min(ans,abs(a[x+k]));
    for i:=max(x,k) to min(n,x+k) do
        ans:=min(ans,min(abs(a[i]),abs(a[i-k+1]))*2+max(abs(a[i]),abs(a[i-k+1])));
    writeln(ans);
end.


var n,i:longint;
    a:array[0..100000]of longint;
begin
  readln(n);
  for i:=1 to n do
    read(a[i]);
  writeln(a[n>>1]);
end.

Score : 700 points

Problem Statement

We will define the median of a sequence b of length M, as follows:

Let b' be the sequence obtained by sorting b in non-decreasing order. Then, the value of the (M / 2 + 1)-th element of b' is the median of b. Here, / is integer division, rounding down.

For example, the median of (10, 30, 20) is 20; the median of (10, 30, 20, 40) is 30; the median of (10, 10, 10, 20, 30) is 10.

Snuke comes up with the following problem.

You are given a sequence a of length N.
For each pair (l, r) (1 \leq l \leq r \leq N), let m_{l, r} be the median of the contiguous subsequence (a_l, a_{l + 1}, ..., a_r) of a.
We will list m_{l, r} for all pairs (l, r) to create a new sequence m.
Find the median of m.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the median of m.

Sample Input 1

3
10 30 20

Sample Output 1

30

The median of each contiguous subsequence of a is as follows:

The median of (10) is 10.

The median of (30) is 30.

The median of (20) is 20.

The median of (10, 30) is 30.

The median of (30, 20) is 30.

The median of (10, 30, 20) is 20.

Thus, m = (10, 30, 20, 30, 30, 20) and the median of m is 30.

Sample Input 2

1
10

Sample Output 2

10

Sample Input 3

10
5 9 5 9 8 9 3 5 4 3

Sample Output 3

8


var n,i:longint;
    a:array[0..100000]of longint;
begin
  readln(n);
  for i:=1 to n do
    read(a[i]);
  writeln(a[(n+1)>>1]);
end.


var
	a:array[0..100010] of longint;
	i,n:longint;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r;
	mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	readln(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	if n<=2 then writeln(a[n])
	else if n mod 2=0 then writeln(a[n div 2+2])
		else writeln(a[n div 2+1]);
end.


uses crt;
var a,b,stru,kq:integer;

begin
	read(a,b);
    stru:=a+b-1;
    kq:=a*b-stru;
    write(kq);
end.

Score: 100 points

Problem Statement

There is a farm whose length and width are A yard and B yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, as shown below: (The gray part represents the roads.)

What is the area of this yard excluding the roads? Find it.

Note

It can be proved that the positions of the roads do not affect the area.

Constraints

A is an integer between 2 and 100 (inclusive).

B is an integer between 2 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

A B

Output

Print the area of this yard excluding the roads (in square yards).

Sample Input 1

2 2

Sample Output 1

1

In this case, the area is 1 square yard.

Sample Input 2

5 7

Sample Output 2

24

In this case, the area is 24 square yards.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k,min:Integer;
  l:array of Integer;
  s,s123,s1,s2,s3:string;
  al:string = 'abcdefghijklmnopqrstuvwxyz';
  tf,tfl,tfr:Boolean;
  so:array[0..24] of integer = (2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97);
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    Readln(n);
    count:=0;
    max  :=0;
    min  :=100;

    for j:=1 to n do begin //sagasu
      count:=0;
      if j mod 2 <> 0 then begin
        for i := 1 to 35 do //kazoeru
          if j mod (i*2-1) = 0 then
            inc(count);
        if count=7 then
          inc(max);
      end;
    end;

    Writeln(max);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score: 200 points

Problem Statement

The number 105 is quite special - it is odd but still it has eight divisors.
Now, your task is this: how many odd numbers with exactly eight positive divisors are there between 1 and N (inclusive)?

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

Print the count.

Sample Input 1

105

Sample Output 1

1

Among the numbers between 1 and 105, the only number that is odd and has exactly eight divisors is 105.

Sample Input 2

7

Sample Output 2

0

1 has one divisor. 3, 5 and 7 are all prime and have two divisors. Thus, there is no number that satisfies the condition.


uses
  SysUtils,
  Classes;

 function JudgeFunc(const Value : Integer) : Integer;
	var
		i, j, k : Integer;
		arrayNum : array of Integer;
	begin
	Result := 0;
	SetLength(arrayNum, 5);
	arrayNum[0] := 3;
	arrayNum[1] := 5;
	arrayNum[2] := 7;
	arrayNum[3] := 11;
	arrayNum[4] := 13;
	for i := 1 to Value do
		begin
		if not Odd(i) then
			Continue;
		j := 0;
		for k := 0 to Length(arrayNum) - 1 do
			begin
			if i mod arrayNum[k] = 0then
				j := j + 1;
			end;
		if j = 3 then
			Inc(Result);
		end;
	end;

 procedure MainProc;
	var
		input : Integer;
		output : Integer;
	begin
	ReadLn(input);
	output := JudgeFunc(input);
	WriteLn(output);
	end;

begin
MainProc;
end.


uses
  SysUtils,
  Classes;

 function JudgeFunc(const Value : Integer) : Integer;
	var
		i, j, k : Integer;
		arrayNum : array of Integer;
	begin
	Result := 0;
	SetLength(arrayNum, 5);
	arrayNum[0] := 3;
	arrayNum[1] := 5;
	arrayNum[2] := 7;
	arrayNum[3] := 11;
	arrayNum[4] := 13;
	for i := 1 to Value do
		begin
		if Odd(i) then
			Continue;
		j := 0;
		for k := 0 to Length(arrayNum) - 1 do
			begin
			if i mod arrayNum[k] = 0then
				j := j + 1;
			end;
		if j = 3 then
			Inc(Result);
		end;
	end;

 procedure MainProc;
	var
		input : Integer;
		output : Integer;
	begin
	ReadLn(input);
	output := JudgeFunc(input);
	WriteLn(output);
	end;

begin
MainProc;
end.


uses
  SysUtils,
  Classes;

 function JudgeFunc(const Value : Integer) : Integer;
	var
		i, j, k : Integer;
		arrayNum : array of Integer;
	begin
	Result := 0;
	SetLength(arrayNum, 5);
	arrayNum[0] := 3;
	arrayNum[1] := 5;
	arrayNum[2] := 7;
	arrayNum[3] := 11;
	arrayNum[4] := 13;

	for i := 1 to Value do
		begin
		j := 0;
		for k := 0 to Length(arrayNum) - 1 do
			begin
			if i mod arrayNum[k] = 0then
				j := j + 1;
			end;
		if j = 3 then
			Inc(Result);
		end;
	end;

 procedure MainProc;
	var
		input : Integer;
		output : Integer;
	begin
	ReadLn(input);
	output := JudgeFunc(input);
	WriteLn(output);
	end;

begin
MainProc;
end.


uses crt;
var
        a,b:string;
        i,j,x,y,k,code:longint;
begin
        readln(a);
        read(k);
        for y:=1 to k-1 do
        begin
        i:=1;
        while i<=k do
        begin
          val(a[i],j,code);
          for x:=1 to j-1 do
            begin
            insert(a[i],a,i);
            inc(i);
            end;
          inc(i);
        end;
        end;
        writeln(a[k]);
     //   writeln(a);

      //  readln;
      //  readln;
end.

Score: 300 points

Problem Statement

Mr. Infinity has a string S consisting of digits from 1 to 9. Each time the date changes, this string changes as follows:

Each occurrence of 2 in S is replaced with 22. Similarly, each 3 becomes 333, 4 becomes 4444, 5 becomes 55555, 6 becomes 666666, 7 becomes 7777777, 8 becomes 88888888 and 9 becomes 999999999. 1 remains as 1.

For example, if S is 1324, it becomes 1333224444 the next day, and it becomes 133333333322224444444444444444 the day after next.
You are interested in what the string looks like after 5 \times 10^{15} days. What is the K-th character from the left in the string after 5 \times 10^{15} days?

Constraints

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

K is an integer between 1 and 10^{18} (inclusive).

The length of the string after 5 \times 10^{15} days is at least K.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the K-th character from the left in Mr. Infinity's string after 5 \times 10^{15} days.

Sample Input 1

1214
4

Sample Output 1

2

The string S changes as follows:

Now: 1214

After one day: 12214444

After two days: 1222214444444444444444

After three days: 12222222214444444444444444444444444444444444444444444444444444444444444444

The first five characters in the string after 5 \times 10^{15} days is 12222. As K=4, we should print the fourth character, 2.

Sample Input 2

3
157

Sample Output 2

3

The initial string is 3. The string after 5 \times 10^{15} days consists only of 3.

Sample Input 3

299792458
9460730472580800

Sample Output 3

2


var
  n,m,q,x,y,i,j:longint;
  b,a:array[0..1000,0..1000]of longint;
begin
  readln(n,m,q);
  for i:=1 to m do
  begin
    readln(x,y);
    inc(a[x,y]);
  end;
  for i:=n downto 1 do
    for j:=1 to n do
      b[i,j]:=a[i,j]+b[i+1,j]+b[i,j-1]-b[i+1,j-1];
  for i:=1 to q do
  begin
    readln(x,y);
    writeln(b[x,y]);
  end;
end.

Score: 400 points

Problem Statement

In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from west to east.
A company called AtCoder Express possesses M trains, and the train i runs from City L_i to City R_i (it is possible that L_i = R_i).
Takahashi the king is interested in the following Q matters:

The number of the trains that runs strictly within the section from City p_i to City q_i, that is, the number of trains j such that p_i \leq L_j and R_j \leq q_i.

Although he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him.

Constraints

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

M is an integer between 1 and 200 \ 000 (inclusive).

Q is an integer between 1 and 100 \ 000 (inclusive).

1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)

1 \leq p_i \leq q_i \leq N (1 \leq i \leq Q)

Input

Input is given from Standard Input in the following format:

N M Q
L_1 R_1
L_2 R_2
:
L_M R_M
p_1 q_1
p_2 q_2
:
p_Q q_Q

Output

Print Q lines. The i-th line should contain the number of the trains that runs strictly within the section from City p_i to City q_i.

Sample Input 1

2 3 1
1 1
1 2
2 2
1 2

Sample Output 1

3

As all the trains runs within the section from City 1 to City 2, the answer to the only query is 3.

Sample Input 2

10 3 2
1 5
2 8
7 10
1 7
3 10

Sample Output 2

1
1

The first query is on the section from City 1 to 7. There is only one train that runs strictly within that section: Train 1.
The second query is on the section from City 3 to 10. There is only one train that runs strictly within that section: Train 3.

Sample Input 3

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

Sample Output 3

7
9
10
6
8
9
6
7
8
10


uses
  SysUtils,
  Classes;

 procedure MainProc;
	var
		text : string;
		strlist : TStringList;
		cracker : Integer;
		person : Integer;
	begin
	ReadLn(text);
	strlist := TStringList.Create;
	try
		strlist.Delimiter := ' ';
		strlist.DelimitedText := text;
		cracker := StrToInt(strlist.Strings[0]);
		person := StrToInt(strlist.Strings[1]);
		finally
			FreeAndNil(strlist);
			end;
	if cracker = person then
		WriteLn(0)
	else
		WriteLn(1);
	end;

begin
MainProc;
end.

Score : 100 points

Problem Statement

Takahashi has decided to distribute N AtCoder Crackers to K users of as evenly as possible.
When all the crackers are distributed, find the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

Constraints

1 \leq N,K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

Sample Input 1

7 3

Sample Output 1

1

When the users receive two, two and three crackers, respectively, the (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user, is 1.

Sample Input 2

100 10

Sample Output 2

0

The crackers can be distributed evenly.

Sample Input 3

1 1

Sample Output 3

0


uses
  SysUtils,
  Classes;

 procedure MainProc;
	var
		text : string;
		strlist : TStringList;
		cracker : Integer;
		person : Integer;
	begin
	ReadLn(text);
	strlist := TStringList.Create;
	try
		strlist.Delimiter := ' ';
		strlist.DelimitedText := text;
		cracker := StrToInt(strlist.Strings[0]);
		person := StrToInt(strlist.Strings[1]);
		finally
			FreeAndNil(strlist);
			end;
	if cracker >= person then
		WriteLn(cracker mod person)
	else
		WriteLn(0);
	end;

begin
MainProc;
end.


uses
  SysUtils,
  Classes;

 procedure MainProc;
	var
		text : string;
		strlist : TStringList;
		cracker : Integer;
		person : Integer;
	begin
	ReadLn(text);
	strlist := TStringList.Create;
	try
		strlist.Delimiter := ' ';
		strlist.DelimitedText := text;
		cracker := StrToInt(strlist.Strings[0]);
		person := StrToInt(strlist.Strings[1]);
		finally
			FreeAndNil(strlist);
			end;
	WriteLn(cracker mod person);
	end;

begin
MainProc;
end.


uses
  SysUtils,
  Classes;

 function JudgeFunc(const Value : Integer) : string;
	const
		cake = 4;
		donut = 7;
	var
		i : Integer;
		remaining : Integer;
	begin
	Result := 'No';
	for i := 0 to Value div donut do
		begin
		remaining := Value - (i * donut);
		if (remaining = 0) or ((remaining mod cake) = 0) then
			Result := 'Yes';
		end;
	end;

 procedure MainProc;
	var
		input : Integer;
		output : string;
	begin
	ReadLn(input);
	output := JudgeFunc(input);
	WriteLn(output);
	end;

begin
MainProc;
end.

Score : 200 points

Problem Statement

La Confiserie d'ABC sells cakes at 4 dollars each and doughnuts at 7 dollars each.
Determine if there is a way to buy some of them for exactly N dollars. You can buy two or more doughnuts and two or more cakes, and you can also choose to buy zero doughnuts or zero cakes.

Constraints

N is an integer between 1 and 100, inclusive.

Input

Input is given from Standard Input in the following format:

N

Output

If there is a way to buy some cakes and some doughnuts for exactly N dollars, print Yes; otherwise, print No.

Sample Input 1

11

Sample Output 1

Yes

If you buy one cake and one doughnut, the total will be 4 + 7 = 11 dollars.

Sample Input 2

40

Sample Output 2

Yes

If you buy ten cakes, the total will be 4 \times 10 = 40 dollars.

Sample Input 3

3

Sample Output 3

No

The prices of cakes (4 dollars) and doughnuts (7 dollars) are both higher than 3 dollars, so there is no such way.


const
    key=100001651;
var
    n,m,i,j:longint;
    k:int64;
    ans:extended;
    a,c,f:array[0..100010] of int64;
    b:array[0..100002000] of longint;
begin
    readln(n,m);j:=1;
    for i:=1 to n do
        begin
            read(a[j]);
            c[j]:=i;
            f[i]:=f[i-1]+a[j];
            k:=f[i] mod m mod key;
            if b[k]<>0 then dec(j);
            inc(b[k]);
            inc(j);
        end;
    for i:=1 to j-1 do
        begin
            k:=f[c[i]] mod m mod key;
            if k=0 then ans:=ans+b[k];
            ans:=ans+b[k]*(b[k]-1) div 2;
        end;
    writeln(ans:0:0);
end.

Score : 400 points

Problem Statement

There are N boxes arranged in a row from left to right. The i-th box from the left contains A_i candies.

You will take out the candies from some consecutive boxes and distribute them evenly to M children.

Such being the case, find the number of the pairs (l, r) that satisfy the following:

l and r are both integers and satisfy 1 \leq l \leq r \leq N.

A_l + A_{l+1} + ... + A_r is a multiple of M.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

2 \leq M \leq 10^9

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the number of the pairs (l, r) that satisfy the conditions.

Note that the number may not fit into a 32-bit integer type.

Sample Input 1

3 2
4 1 5

Sample Output 1

3

The sum A_l + A_{l+1} + ... + A_r for each pair (l, r) is as follows:

Sum for (1, 1): 4

Sum for (1, 2): 5

Sum for (1, 3): 10

Sum for (2, 2): 1

Sum for (2, 3): 6

Sum for (3, 3): 5

Among these, three are multiples of 2.

Sample Input 2

13 17
29 7 5 7 9 51 7 13 8 55 42 9 81

Sample Output 2

6

Sample Input 3

10 400000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

25


var a:longint;
begin
  read(a);
  if a<1200 then write('ABC')
else if a<2800 then write('ARC')
else write('AGC');
end.

Score : 100 points

Problem Statement

A programming competition site AtCode regularly holds programming contests.

The next contest on AtCode is called ABC, which is rated for contestants with ratings less than 1200.

The contest after the ABC is called ARC, which is rated for contestants with ratings less than 2800.

The contest after the ARC is called AGC, which is rated for all contestants.

Takahashi's rating on AtCode is R. What is the next contest rated for him?

Constraints

0 ≤ R ≤ 4208

R is an integer.

Input

Input is given from Standard Input in the following format:

R

Output

Print the name of the next contest rated for Takahashi (ABC, ARC or AGC).

Sample Input 1

1199

Sample Output 1

ABC

1199 is less than 1200, so ABC will be rated.

Sample Input 2

1200

Sample Output 2

ARC

1200 is not less than 1200 and ABC will be unrated, but it is less than 2800 and ARC will be rated.

Sample Input 3

4208

Sample Output 3

AGC


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k,min:Integer;
  l:array of Integer;
  s,s123,s1,s2,s3:string;
  al:string = 'abcdefghijklmnopqrstuvwxyz';
  tf,tfl,tfr:Boolean;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    Readln(s);
    count:=0;
    max  :=0;
    min  :=100;
    tf   :=true;

    if not(s[1]='A') then
      tf:=false;
    if ord(s[2])<ord('a') then
      tf:=false;
    if not(s[3]='C') then
      tf:=false;

    for i:=4 to length(s) do
      if ord(s[i])<ord('a') then
        tf:=false;

    if tf then
      Writeln('AC')
    else
      Writeln('WA');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

You are given a string S. Each character of S is uppercase or lowercase English letter.
Determine if S satisfies all of the following conditions:

The initial character of S is an uppercase A.

There is exactly one occurrence of C between the third character from the beginning and the second to last character (inclusive).

All letters except the A and C mentioned above are lowercase.

Constraints

4 ≤ |S| ≤ 10 (|S| is the length of the string S.)

Each character of S is uppercase or lowercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

If S satisfies all of the conditions in the problem statement, print AC; otherwise, print WA.

Sample Input 1

AtCoder

Sample Output 1

AC

The first letter is A, the third letter is C and the remaining letters are all lowercase, so all the conditions are satisfied.

Sample Input 2

ACoder

Sample Output 2

WA

The second letter should not be C.

Sample Input 3

AcycliC

Sample Output 3

WA

The last letter should not be C, either.

Sample Input 4

AtCoCo

Sample Output 4

WA

There should not be two or more occurrences of C.

Sample Input 5

Atcoder

Sample Output 5

WA

The number of C should not be zero, either.


var
i:longint;
s:string;
b:boolean;
begin
readln(s);
if (s[1]<>'A') or (s[2]='C') or (s[length(s)]='C') then
begin
writeln('WA');
halt;
end;
b:=false;
for i:=2 to length(s) do
 if s[i]='C' then
 begin
  if b then
  begin
  writeln('WA');
  halt;
  end;
 b:=true;
 end
 else
  if s[i] in ['A'..'Z'] then
  begin
  writeln('WA');
  halt;
  end;
if b then
writeln('AC')
else
writeln('WA');
end.

Score : 300 points

Problem Statement

A programming competition site AtCode provides algorithmic problems.
Each problem is allocated a score based on its difficulty.
Currently, for each integer i between 1 and D (inclusive), there are p_i problems with a score of 100i points.
These p_1 + … + p_D problems are all of the problems available on AtCode.

A user of AtCode has a value called total score.
The total score of a user is the sum of the following two elements:

Base score: the sum of the scores of all problems solved by the user.

Perfect bonuses: when a user solves all problems with a score of 100i points, he/she earns the perfect bonus of c_i points, aside from the base score (1 ≤ i ≤ D).

Takahashi, who is the new user of AtCode, has not solved any problem.
His objective is to have a total score of G or more points.
At least how many problems does he need to solve for this objective?

Constraints

1 ≤ D ≤ 10

1 ≤ p_i ≤ 100

100 ≤ c_i ≤ 10^6

100 ≤ G

All values in input are integers.

c_i and G are all multiples of 100.

It is possible to have a total score of G or more points.

Input

Input is given from Standard Input in the following format:

D G
p_1 c_1
:
p_D c_D

Output

Print the minimum number of problems that needs to be solved in order to have a total score of G or more points. Note that this objective is always achievable (see Constraints).

Sample Input 1

2 700
3 500
5 800

Sample Output 1

3

In this case, there are three problems each with 100 points and five problems each with 200 points. The perfect bonus for solving all the 100-point problems is 500 points, and the perfect bonus for solving all the 200-point problems is 800 points. Takahashi's objective is to have a total score of 700 points or more.

One way to achieve this objective is to solve four 200-point problems and earn a base score of 800 points. However, if we solve three 100-point problems, we can earn the perfect bonus of 500 points in addition to the base score of 300 points, for a total score of 800 points, and we can achieve the objective with fewer problems.

Sample Input 2

2 2000
3 500
5 800

Sample Output 2

7

This case is similar to Sample Input 1, but the Takahashi's objective this time is 2000 points or more. In this case, we inevitably need to solve all five 200-point problems, and by solving two 100-point problems additionally we have the total score of 2000 points.

Sample Input 3

2 400
3 500
5 800

Sample Output 3

2

This case is again similar to Sample Input 1, but the Takahashi's objective this time is 400 points or more. In this case, we only need to solve two 200-point problems to achieve the objective.

Sample Input 4

5 25000
20 1000
40 1000
50 1000
30 1000
1 1000

Sample Output 4

66

There is only one 500-point problem, but the perfect bonus can be earned even in such a case.


var
    d,g,i,j:longint;
    f:array[0..20,0..200010] of longint;
    p,c:array[0..20] of longint;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
function dp(n,m:longint):longint;
var
    i:longint;
begin
    if m<=0 then exit(0);
    if n=0 then exit(999999999);
    if f[n,m]<>999999999 then exit(f[n,m]);
    for i:=0 to p[n]-1 do
        f[n,m]:=min(f[n,m],dp(n-1,m-i*n)+i);
    f[n,m]:=min(f[n,m],dp(n-1,m-p[n]*n-c[n])+p[n]);
    exit(f[n,m]);
end;
begin
    readln(d,g);g:=g div 100;
    for i:=1 to d do
        begin
            readln(p[i],c[i]);
            c[i]:=c[i] div 100;
        end;
    for i:=1 to d do
        for j:=1 to g do
            f[i,j]:=999999999;
    writeln(dp(d,g));
end.


uses
  SysUtils,
  Classes;

function CheckFunc(strS : String; const strT : String) : Boolean;
	var
		count : Integer;
		str : String;
	begin
	Result := False;
	count := 1;
	while (Length(strS) <> count) do
		begin
		if (strS = strT )then
			begin
			Result := True;
			exit;
			end;
		str := strS[Length(strS)];
		str := str + Copy(strS, 1, Length(strS) - 1);
		strS := str;
		count := count + 1;
		end;
	end;

var
	strS, strT : String;
begin
	readln(strS);
	readln(strT);

	if CheckFunc(strS, strT) then
		writeln('Yes')
	else
		writeln('No');
end.

Score : 200 points

Problem Statement

You are given string S and T consisting of lowercase English letters.

Determine if S equals T after rotation.

That is, determine if S equals T after the following operation is performed some number of times:

Operation: Let S = S_1 S_2 ... S_{|S|}. Change S to S_{|S|} S_1 S_2 ... S_{|S|-1}.

Here, |X| denotes the length of the string X.

Constraints

2 \leq |S| \leq 100

|S| = |T|

S and T consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S equals T after rotation, print Yes; if it does not, print No.

Sample Input 1

kyoto
tokyo

Sample Output 1

Yes

In the first operation, kyoto becomes okyot.

In the second operation, okyot becomes tokyo.

Sample Input 2

abc
arc

Sample Output 2

No

abc does not equal arc after any number of operations.

Sample Input 3

aaaaaaaaaaaaaaab
aaaaaaaaaaaaaaab

Sample Output 3

Yes


var n,i,x,s:Longint;
begin
read(n);
s:=-n;
for i:=1 to n do begin read(x);inc(s,x);end;
writeln(s);
end.

Score : 300 points

Problem Statement

You are given N positive integers a_1, a_2, ..., a_N.

For a non-negative integer m, let f(m) = (m\ mod\ a_1) + (m\ mod\ a_2) + ... + (m\ mod\ a_N).

Here, X\ mod\ Y denotes the remainder of the division of X by Y.

Find the maximum value of f.

Constraints

All values in input are integers.

2 \leq N \leq 3000

2 \leq a_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the maximum value of f.

Sample Input 1

3
3 4 6

Sample Output 1

10

f(11) = (11\ mod\ 3) + (11\ mod\ 4) + (11\ mod\ 6) = 10 is the maximum value of f.

Sample Input 2

5
7 46 11 20 11

Sample Output 2

90

Sample Input 3

7
994 518 941 851 647 2 581

Sample Output 3

4527


var a:array[0..3020] of longint;
var i,j,n,ans:longint;
begin
  read(n);
  for i:=1 to n do begin read(a[i]);ans:=ans+a[i]-1;end;
  writeln(ans);
end.


var
	T,i:Longint;
	A,B,C,D,G:int64;
function gcd(a,b:int64):int64;
begin
	if b=0 then gcd:=a else gcd:=gcd(b,a mod b);
end;
begin
	read(T);
	for i:=1 to T do begin
		read(A,B,C,D);
		G:=gcd(B,D);
		if(B>A)or(B>D)or(B>(-A-1 mod G+G)mod G+C+1)then writeln('No')else writeln('Yes');
	end;
end.

Score : 600 points

Problem Statement

Ringo Mart, a convenience store, sells apple juice.

On the opening day of Ringo Mart, there were A cans of juice in stock in the morning.
Snuke buys B cans of juice here every day in the daytime.
Then, the manager checks the number of cans of juice remaining in stock every night.
If there are C or less cans, D new cans will be added to the stock by the next morning.

Determine if Snuke can buy juice indefinitely, that is, there is always B or more cans of juice in stock when he attempts to buy them.
Nobody besides Snuke buy juice at this store.

Note that each test case in this problem consists of T queries.

Constraints

1 \leq T \leq 300

1 \leq A, B, C, D \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

T
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_T B_T C_T D_T

In the i-th query, A = A_i, B = B_i, C = C_i, D = D_i.

Output

Print T lines. The i-th line should contain Yes if Snuke can buy apple juice indefinitely in the i-th query; No otherwise.

Sample Input 1

14
9 7 5 9
9 7 6 9
14 10 7 12
14 10 8 12
14 10 9 12
14 10 7 11
14 10 8 11
14 10 9 11
9 10 5 10
10 10 5 10
11 10 5 10
16 10 5 10
1000000000000000000 17 14 999999999999999985
1000000000000000000 17 15 999999999999999985

Sample Output 1

No
Yes
No
Yes
Yes
No
No
Yes
No
Yes
Yes
No
No
Yes

In the first query, the number of cans of juice in stock changes as follows: (D represents daytime and N represents night.)

9
→D 2
→N 11
→D 4
→N 13
→D 6
→N 6
→D x

In the second query, the number of cans of juice in stock changes as follows:

9
→D 2
→N 11
→D 4
→N 13
→D 6
→N 15
→D 8
→N 8
→D 1
→N 10
→D 3
→N 12
→D 5
→N 14
→D 7
→N 7
→D 0
→N 9
→D 2
→N 11
→D …

and so on, thus Snuke can buy juice indefinitely.

Sample Input 2

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

Sample Output 2

No
No
No
No
No
No
Yes
Yes
No
No
No
No
Yes
Yes
Yes
No
No
No
Yes
Yes
Yes
No
No
No


var
    n,i,l,r:longint;
    a,b:array[0..200010] of int64;
    c:array[0..5] of int64;
    ans:int64;
function max(a,b:int64):int64;
begin
    if a>b then exit(a) else exit(b);
end;
function min(a,b:int64):int64;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    readln(n);
    for i:=1 to n do
        begin
            read(a[i]);
            b[i]:=b[i-1]+a[i];
        end;
    l:=1;r:=3;ans:=maxlongint;
    for i:=2 to n-2 do
        begin
            while (l<i) and (abs(b[l]-(b[i]-b[l]))>=abs(b[l+1]-(b[i]-b[l+1]))) do inc(l);
            while (r<n) and (abs((b[r]-b[i])-(b[n]-b[r]))>=abs((b[r+1]-b[i])-(b[n]-b[r+1]))) do inc(r);
            c[1]:=b[l];
            c[2]:=b[i]-b[l];
            c[3]:=b[r]-b[i];
            c[4]:=b[n]-b[r];
            ans:=min(ans,max(c[1],max(c[2],max(c[3],c[4])))-min(c[1],min(c[2],min(c[3],c[4]))));
        end;
    writeln(ans);
end.

Score : 600 points

Problem Statement

Snuke has an integer sequence A of length N.

He will make three cuts in A and divide it into four (non-empty) contiguous subsequences B, C, D and E.
The positions of the cuts can be freely chosen.

Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively.
Snuke is happier when the absolute difference of the maximum and the minimum among P,Q,R,S is smaller.
Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Constraints

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

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
A_1 A_2 ... A_N

Output

Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Sample Input 1

5
3 2 4 1 2

Sample Output 1

2

If we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3.
Here, the maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute difference of 2.
We cannot make the absolute difference of the maximum and the minimum less than 2, so the answer is 2.

Sample Input 2

10
10 71 84 33 6 47 23 25 52 64

Sample Output 2

36

Sample Input 3

7
1 2 3 1000000000 4 5 6

Sample Output 3

999999994


var
    n,i,l,r:longint;
    a,b:array[0..200010] of int64;
    c:array[0..5] of longint;
    ans:int64;
function max(a,b:longint):longint;
begin
    if a>b then exit(a) else exit(b);
end;
function min(a,b:longint):longint;
begin
    if a<b then exit(a) else exit(b);
end;
begin
    readln(n);
    for i:=1 to n do
        begin
            read(a[i]);
            b[i]:=b[i-1]+a[i];
        end;
    l:=1;r:=3;ans:=maxlongint;
    for i:=2 to n-2 do
        begin
            while (l<i) and (abs(b[l]-(b[i]-b[l]))>=abs(b[l+1]-(b[i]-b[l+1]))) do inc(l);
            while (r<n) and (abs((b[r]-b[i])-(b[n]-b[r]))>=abs((b[r+1]-b[i])-(b[n]-b[r+1]))) do inc(r);
            c[1]:=b[l];
            c[2]:=b[i]-b[l];
            c[3]:=b[r]-b[i];
            c[4]:=b[n]-b[r];
            ans:=min(ans,max(c[1],max(c[2],max(c[3],c[4])))-min(c[1],min(c[2],min(c[3],c[4]))));
        end;
    writeln(ans);
end.


Uses math;
var
	n,i:Longint;
	a:array[1..200000]of Longint;
	s:array[0..200000]of int64;
	reta,retb,x,y,z,w,ans:int64;
procedure f(L,R:Longint);
var
	sum,c,d,e,f:int64;
	ll,rr,mm,k:Longint;
begin
	sum:=(s[R]+s[L])div 2;
	ll:=L;
	rr:=R-1;
	while rr-ll>1 do begin
		mm:=(ll+rr)div 2;
		if s[mm]>sum then rr:=mm else ll:=mm;
	end;
	k:=rr;
	c:=s[k]-s[L];
	d:=s[R]-s[k];
	dec(k);
	if k>L then begin
		e:=s[k]-s[L];
		f:=s[R]-s[k];
		if Max(c,d)-Min(c,d)>Max(e,f)-Min(e,f)then begin
			reta:=Min(e,f);
			retb:=Max(e,f);
			exit;
		end;
	end;
	reta:=Min(c,d);
	retb:=Max(c,d);
end;
begin
	read(n);
	for i:=1 to n do begin
		read(a[i]);
		s[i]:=s[i-1]+a[i];
	end;
	ans:=s[n];
	for i:=2 to n-2 do begin
		f(0,i);
		x:=reta;
		z:=retb;
		f(i,n);
		y:=reta;
		w:=retb;
		ans:=Min(ans,Max(z,w)-Min(x,y));
	end;
	writeln(ans);
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k:Integer;
  l:array of Integer;
  s,s123,s1,s2,s3:string;
  al:string = 'abcdefghijklmnopqrstuvwxyz';
  tf,tfl,tfr:Boolean;
  dbTest:double;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    Read(n);
    Readln(k);
    Readln(s);

    count:=0;
    max  :=0;

    if n=k then begin
      Writeln('1');
      Readln;
      exit;
    end;

    if frac((n-k)/(k-1)) <> 0.0 then
      Writeln((n-k)div(k-1)+2)
    else
      Writeln((n-k)div(k-1)+1);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 300 points

Problem Statement

There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.

On this sequence, Snuke can perform the following operation:

Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.

Snuke would like to make all the elements in this sequence equal by repeating the operation above some number of times.
Find the minimum number of operations required.
It can be proved that, Under the constraints of this problem, this objective is always achievable.

Constraints

2 \leq K \leq N \leq 100000

A_1, A_2, ..., A_N is a permutation of 1, 2, ..., N.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of operations required.

Sample Input 1

4 3
2 3 1 4

Sample Output 1

2

One optimal strategy is as follows:

In the first operation, choose the first, second and third elements. The sequence A becomes 1, 1, 1, 4.

In the second operation, choose the second, third and fourth elements. The sequence A becomes 1, 1, 1, 1.

Sample Input 2

3 3
1 2 3

Sample Output 2

1

Sample Input 3

8 3
7 3 1 8 4 6 2 5

Sample Output 3

4


var
    n,k,min,i,t,ans,s:longint;
    a:array[0..100010] of longint;
begin
    readln(n,k);
    min:=maxlongint;
    for i:=1 to n do
        begin
            read(a[i]);
            if a[i]<min then min:=a[i];
        end;
    ans:=maxlongint;
    for i:=1 to n do
        if a[i]=min then
            begin
                if i<>1 then if (i-1) mod (k-1)=0 then s:=(i-1) div (k-1) else s:=(i-1) div (k-1)+1;
                if i<>n then if (n-i) mod (k-1)=0 then s:=s+(n-i) div (k-1) else s:=s+(n-i) div (k-1)+1;
                if s<ans then ans:=s;
            end;
    writeln(ans);
end.


var
    ans,k,tot1,tot2:int64;
    n:longint;
function s(x:int64):int64;
begin
    if x=0 then exit(0)
    else exit(x mod 10+s(x div 10));
end;
begin
    readln(n);
    ans:=1;k:=1;
    while n>0 do
        begin
            writeln(ans);
            tot1:=ans+k;
            tot2:=ans+k*10;
            if tot1*s(tot2)<=tot2*s(tot1) then ans:=tot1
            else
                begin
                    ans:=tot2;
                    k:=k*10;
                end;
            dec(n);
        end;
end.

Score : 500 points

Problem Statement

Let S(n) denote the sum of the digits in the decimal notation of n.
For example, S(123) = 1 + 2 + 3 = 6.

We will call an integer n a Snuke number when, for all positive integers m such that m > n, \frac{n}{S(n)} \leq \frac{m}{S(m)} holds.

Given an integer K, list the K smallest Snuke numbers.

Constraints

1 \leq K

The K-th smallest Snuke number is not greater than 10^{15}.

Input

Input is given from Standard Input in the following format:

K

Output

Print K lines. The i-th line should contain the i-th smallest Snuke number.

Sample Input 1

10

Sample Output 1

1
2
3
4
5
6
7
8
9
19


var
    ans,k,tot1,tot2:int64;
    n:longint;
function s(x:longint):longint;
begin
    if x=0 then exit(0)
    else exit(x mod 10+s(x div 10));
end;
begin
    readln(n);
    ans:=1;k:=1;
    while n>0 do
        begin
            writeln(ans);
            tot1:=ans+k;
            tot2:=ans+k*10;
            if tot1*s(tot2)<=tot2*s(tot1) then ans:=tot1
            else
                begin
                    ans:=tot2;
                    k:=k*10;
                end;
            dec(n);
        end;
end.


var
	n,a:int64;
	i,k:Longint;
function S(n:int64):real;
var x,m:int64;
begin
	m:=n;
	x:=0;
	while n>0 do begin
		x:=x+n mod 10;
		n:=n div 10;
	end;
	S:=m/x;
end;
begin
	n:=1;
	a:=1;
	read(k);
	writeln(1);
	for i:=2 to k do begin
		if S(n+a)>S(n+a*10)then a:=a*10;
		n:=n+a;
		writeln(n);
	end;
end.


var
	n,i,a,s:Longint;
begin
	read(n);
	for i:=1 to n do begin
		read(a);
		while a and 1=0 do begin
			inc(s);
			a:=a shr 1;
		end;
	end;
	writeln(s);
end.

Score: 300 points

Problem Statement

As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length N, a = {a_1, a_2, a_3, ..., a_N}.

Snuke, an employee, would like to play with this sequence.

Specifically, he would like to repeat the following operation as many times as possible:

For every i satisfying 1 \leq i \leq N, perform one of the following: "divide a_i by 2" and "multiply a_i by 3".
Here, choosing "multiply a_i by 3" for every i is not allowed, and the value of a_i after the operation must be an integer.

At most how many operations can be performed?

Constraints

N is an integer between 1 and 10 \ 000 (inclusive).

a_i is an integer between 1 and 1 \ 000 \ 000 \ 000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print the maximum number of operations that Snuke can perform.

Sample Input 1

3
5 2 4

Sample Output 1

3

The sequence is initially {5, 2, 4}. Three operations can be performed as follows:

First, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {15, 6, 2}.

Next, multiply a_1 by 3, divide a_2 by 2 and multiply a_3 by 3. The sequence is now {45, 3, 6}.

Finally, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {135, 9, 3}.

Sample Input 2

4
631 577 243 199

Sample Output 2

0

No operation can be performed since all the elements are odd. Thus, the answer is 0.

Sample Input 3

10
2184 2126 1721 1800 1024 2528 3360 1945 1280 1776

Sample Output 3

39


function FuncSum(const x : Integer) : Integer;
	begin
	Result := 0;
	while x mod 2 = 0 do
		Result := Result + 1;
	end;

procedure ProcMain;
	var
		n, i, sum : Integer;
		a : array of Integer;
	begin
		readln(n);
		setLength(a, n);
		sum := 0;
		for i := 0 to n - 1 do
			begin
			read(a[i]);
			sum := sum + FuncSum(a[i]);
			end;
		WriteLn(sum);
	end;
begin
ProcMain;
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,math,classes;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k:Integer;
  l:array of Integer;
  s,s123,s1,s2,s3:string;
  al:string = 'abcdefghijklmnopqrstuvwxyz';
  tf,tfl,tfr,owari:Boolean;
  nyu:TStringList;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    nyu := TStringList.Create;
    Readln(a);
    Readln(s);
    nyu.CommaText := s;
    count:=0;
    owari:=true;
    j:=0;

    while owari do begin
      for i:=j to a-1 do begin
        if strtoint(nyu[i])mod 268435456 = 0 then begin
          nyu[i]:=inttostr(strtoint(nyu[i])div 268435456);
          count:=count+28;
          j:=i;
          break
        end;
        if strtoint(nyu[i])mod 16384 = 0 then begin
          nyu[i]:=inttostr(strtoint(nyu[i])div 16384);
          count:=count+14;
          j:=i;
          break
        end;
        if strtoint(nyu[i])mod 64 = 0 then begin
          nyu[i]:=inttostr(strtoint(nyu[i])div 64);
          count:=count+6;
          j:=i;
          break
        end;
        if strtoint(nyu[i])mod 2 = 0 then begin
          nyu[i]:=inttostr(strtoint(nyu[i])div 2);
          inc(count);
          j:=i;
          break
        end;

        if i=a-1 then
          owari:=false;
      end;
    end;

    Writeln(count);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,math,classes;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k:Integer;
  l:array of Integer;
  s,s123,s1,s2,s3:string;
  al:string = 'abcdefghijklmnopqrstuvwxyz';
  tf,tfl,tfr,owari:Boolean;
  nyu:TStringList;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    nyu := TStringList.Create;
    Readln(a);
    Readln(s);
    nyu.CommaText := s;
    count:=0;
    owari:=true;

    while owari do begin
      for i:=0 to a-1 do begin
        if strtoint(nyu[i])mod 16384 = 0 then begin
          nyu[i]:=inttostr(strtoint(nyu[i])div 16384);
          count:=count+14;
          break
        end;
        if strtoint(nyu[i])mod 64 = 0 then begin
          nyu[i]:=inttostr(strtoint(nyu[i])div 64);
          count:=count+6;
          break
        end;
        if strtoint(nyu[i])mod 2 = 0 then begin
          nyu[i]:=inttostr(strtoint(nyu[i])div 2);
          inc(count);
          break
        end;
        if i=a-1 then
          owari:=false;
      end;
    end;

    Writeln(count);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,math,classes;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k:Integer;
  l:array of Integer;
  s,s123,s1,s2,s3:string;
  al:string = 'abcdefghijklmnopqrstuvwxyz';
  tf,tfl,tfr,owari:Boolean;
  nyu:TStringList;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    nyu := TStringList.Create;
    Readln(a);
    Readln(s);
    nyu.CommaText := s;
    count:=0;
    owari:=true;

    while owari do begin
      for i:=0 to a-1 do begin
        sum:=strtoint(nyu[i]);
        if strtoint(nyu[i])mod 2 = 0 then begin
          nyu[i]:=inttostr(strtoint(nyu[i])div 2);
          inc(count);
          break
        end;
        if i=a-1 then
          owari:=false;
      end;
    end;

    Writeln(count);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.


const maxn=trunc(1e5);
      max=trunc(1e9);
var f:array[0..maxn+10] of longint;
    n:longint;

function min(x,y:longint):longint;
begin
	if x>y then min:=y
	else min:=x;
end;

procedure dp;
var power,i:longint;
begin
	f[0]:=0;
	for i:=1 to 100000 do
	  begin
	    f[i]:=max;
	    power:=1;
	    while power<=i do
	      begin
	        f[i]:=min(f[i],f[i-power]+1);
	        power:=power*6;
	      end;
	    power:=1;
	    while power<=i do
	      begin
	        f[i]:=min(f[i],f[i-power]+1);
	        power:=power*9;
	      end;
	  end;
end;

begin
	readln(N);
	dp;
	write(f[n]);
end.

Score : 300 points

Problem Statement

To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:

1 yen (the currency of Japan)

6 yen, 6^2(=36) yen, 6^3(=216) yen, ...

9 yen, 9^2(=81) yen, 9^3(=729) yen, ...

At least how many operations are required to withdraw exactly N yen in total?

It is not allowed to re-deposit the money you withdrew.

Constraints

1 \leq N \leq 100000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If at least x operations are required to withdraw exactly N yen in total, print x.

Sample Input 1

127

Sample Output 1

4

By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations.

Sample Input 2

3

Sample Output 2

3

By withdrawing 1 yen three times, we can withdraw 3 yen in three operations.

Sample Input 3

44852

Sample Output 3

16


const max=trunc(1e5);
var f:array[0..max] of longint;
    n:longint;

function min(x,y:longint):longint;
begin
	if x>y then min:=y
	else min:=x;
end;

procedure dp;
var power,i:longint;
begin
	f[0]:=0;
	for i:=1 to 100000 do
	  begin
	    f[i]:=max;
	    power:=1;
	    while power<=i do
	      begin
	        f[i]:=min(f[i],f[i-power]+1);
	        power:=power*6;
	      end;
	  end;
end;

begin
	readln(N);
	dp;
	write(f[n]);
end.


 var  f,g:text; i,p:longint; n,a,b,k,tong,so,tong1,tong2:int64;
      gt,rev,rev1:array[0..300010] of int64;

 function mu(a1,b1,p1:int64):int64;
 var q:int64;
 begin
   if b1=1 then exit(a1);
   if b1=0 then exit(1);
   q:=mu(a1,b1 div 2,p1);
   if b1 mod 2 = 0 then exit(q*q mod p1)
    else exit((((q*q) mod p1) *a1 mod p1) mod p1);
 end;

 begin

  gt[0]:=1;
  p:=998244353; rev[0]:=1;
  for i:=1 to 300005 do
  begin
   gt[i]:=(gt[i-1] mod p * i mod p ) mod p;
  end;
  for i:=1 to 300005 do
   begin
    rev[i]:=mu(gt[i],p-2,p);
    end;

// write(g,((gt[n]* (rev[i] mod p)) mod p * (rev[n-i] mod p)) mod p ,' ');

  readln(n,a,b,k);
   //n:=90081; a:=33447; b:=90629; k:=6391049189;
  tong:=0;
  for i:=0 to n do
   begin
    if (k-i*A)<0 then break;
    if (k-i*A) mod b = 0 then
     begin
     so:=(k-i*A) div B;
     tong1:=((((gt[n]* rev[i] ) mod p) * (rev[n-i] mod p)) mod p);
     tong2:=((((gt[n]* rev[so]) mod p) * (rev[n-so] mod p)) mod p);
     tong:=(tong+(tong1*tong2) mod p) mod p;
     end;
   end;
  write(tong);

  end.

Score : 200 points

Problem Statement

Takahashi has two positive integers A and B.

It is known that A plus B equals N.
Find the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B" (in base 10).

Constraints

2 ≤ N ≤ 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B".

Sample Input 1

15

Sample Output 1

6

When A=2 and B=13, the sums of their digits are 2 and 4, which minimizes the value in question.

Sample Input 2

100000

Sample Output 2

10


const
	modd=998244353;
var
	n,a,b,k,sum,max,w:int64;
	x,y,i:longint;
	f:array[0..300000] of int64;

function pow(q,w:int64):int64;
var
	res:int64;
begin
	res:=1;
	while w>0 do begin
		if w mod 2=1 then res:=res*q mod modd;
		q:=q*q mod modd;
		w:=w shr 1;
	end;
	exit(res);
end;

function comb(q,w:int64):int64;
begin
	if q<w then exit(0)	else if w=q then exit(1)
	else exit(f[q]*pow(f[w],modd-2) mod modd*pow(f[q-w],modd-2) mod modd);
end;

begin
	readln(n,a,b,k);
	f[0]:=1;
	for i:=1 to 300000 do
		f[i]:=f[i-1]*i mod modd;
	max:=k div a;
	sum:=0;
	for x:=0 to max do begin
		w:=k-a*x;
		if (w mod b=0)and(w div b<=n) then begin
			y:=w div b;
			inc(sum,(comb(n,x)*comb(n,y)) mod modd);
		end;
	end;
	writeln(sum mod modd);
end.

Score : 700 points

Problem Statement

Takahashi has a tower which is divided into N layers.
Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful tower.
He defines the beauty of the tower as follows:

The beauty of the tower is the sum of the scores of the N layers, where the score of a layer is A if the layer is painted red, A+B if the layer is painted green, B if the layer is painted blue, and 0 if the layer is uncolored.

Here, A and B are positive integer constants given beforehand. Also note that a layer may not be painted in two or more colors.

Takahashi is planning to paint the tower so that the beauty of the tower becomes exactly K.
How many such ways are there to paint the tower? Find the count modulo 998244353.
Two ways to paint the tower are considered different when there exists a layer that is painted in different colors, or a layer that is painted in some color in one of the ways and not in the other.

Constraints

1 ≤ N ≤ 3×10^5

1 ≤ A,B ≤ 3×10^5

0 ≤ K ≤ 18×10^{10}

All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N A B K

Output

Print the number of the ways to paint tiles, modulo 998244353.

Sample Input 1

4 1 2 5

Sample Output 1

40

In this case, a red layer worth 1 points, a green layer worth 3 points and the blue layer worth 2 points. The beauty of the tower is 5 when we have one of the following sets of painted layers:

1 green, 1 blue

1 red, 2 blues

2 reds, 1 green

3 reds, 1 blue

The total number of the ways to produce them is 40.

Sample Input 2

2 5 6 0

Sample Output 2

1

The beauty of the tower is 0 only when all the layers are uncolored. Thus, the answer is 1.

Sample Input 3

90081 33447 90629 6391049189

Sample Output 3

577742975


const
	modd=998244353;
var
	n,a,b,k,sum,max,w:int64;
	x,y,i:longint;
	f:array[0..300000] of int64;

function pow(q,w:int64):int64;
var
	res:int64;
begin
	res:=1;
	while w>0 do begin
		if w mod 2=1 then res:=res*q mod modd;
		q:=q*q mod modd;
		w:=w shr 1;
	end;
	exit(res);
end;

function comb(q,w:int64):int64;
begin
	if q<w then exit(0)	else if w=q then exit(1)
	else exit(f[q]*pow(f[w],modd-2) mod modd*pow(f[q-w],modd-2) mod modd);
end;

begin
	readln(n,a,b,k);
	f[0]:=1;
	for i:=1 to 100000 do
		f[i]:=f[i-1]*i mod modd;
	max:=k div a;
	sum:=0;
	for x:=0 to max do begin
		w:=k-a*x;
		if w mod b=0 then begin
			y:=w div b;
			inc(sum,(comb(n,x)*comb(n,y)) mod modd);
		end;
	end;
	writeln(sum mod modd);
end.


 var  f,g:text; i,p:longint; n,a,b,k,tong,so,tong1,tong2:int64;
      gt,rev,rev1:array[0..300010] of int64;

 function mu(a1,b1,p1:int64):int64;
 var q:int64;
 begin
   if b1=1 then exit(a1);
   if b1=0 then exit(1);
   q:=mu(a1,b1 div 2,p1);
   if b1 mod 2 = 0 then exit(q*q mod p1)
    else exit(q*q mod p1 *a mod p1);
 end;

 begin

  gt[0]:=1;
  p:=998244353; rev[0]:=1;
  for i:=1 to 300005 do
  begin
   gt[i]:=gt[i-1] * i mod p
  end;
  for i:=1 to 300005 do
   begin
    rev[i]:=mu(gt[i],p-2,p);
    end;

// write(g,((gt[n]* (rev[i] mod p)) mod p * (rev[n-i] mod p)) mod p ,' ');

  readln(n,a,b,k);
   //n:=90081; a:=33447; b:=90629; k:=6391049189;
   //n:=4; a:=1; b:=2; k:=5;
  tong:=0;
  for i:=0 to n do
   begin
    if (k-i*A)<0 then break;
    if (k-i*A) mod b = 0 then
     begin
     so:=(k-i*A) div B;
     if n<i then tong1:=0 else tong1:=gt[n]* rev[i]  mod p * rev[n-i] mod p ;
     if n<so then tong2:=0 else tong2:=gt[n]* rev[so]  mod p * rev[n-so] mod p;
     tong:=(tong+tong1*tong2 mod p) mod p;
     end;
   end;
  write(tong);

  end.


var  f,g:text; i,p:longint; n,a,b,k,tong,so,tong1,tong2:int64;
      gt,rev,rev1:array[0..300010] of int64;

 function mu(a1,b1,p1:int64):int64;
 var q:int64;
 begin
   if b1=1 then exit(a1);
   if b1=0 then exit(1);
   q:=mu(a1,b1 div 2,p1);
   if b1 mod 2 = 0 then exit(q*q mod p1)
    else exit((((q*q) mod p1) *a1 mod p1) mod p1);
 end;

 begin

  gt[0]:=1;
  p:=998244353; rev[0]:=1;
  for i:=1 to 300005 do
  begin
   gt[i]:=(gt[i-1] mod p * i mod p ) mod p;
  end;
  for i:=1 to 300005 do
   begin
    rev[i]:=mu(gt[i],p-2,p);
    end;

// write(g,((gt[n]* (rev[i] mod p)) mod p * (rev[n-i] mod p)) mod p ,' ');

  //readln(n,a,b,k);
   n:=90081; a:=33447; b:=90629; k:=6391049189;
  tong:=0;
  for i:=0 to n do
   begin
    if (k-i*A)<0 then break;
    if (k-i*A) mod b = 0 then
     begin
     so:=(k-i*A) div B;
     tong1:=((((gt[n]* rev[i] ) mod p) * (rev[n-i] mod p)) mod p);
     tong2:=((((gt[n]* rev[so]) mod p) * (rev[n-so] mod p)) mod p);
     tong:=(tong+((tong1*tong2) mod p)) mod p;
     end;
   end;
  write(tong);

  end.


  var  f,g:text; i1,t,i,p,n,a,b,k,tong,so,tong1,tong2:longint;
      gt,rev,rev1:array[0..300010] of int64;

 function mu(a1,b1,p1:int64):int64;
 var q:int64;
 begin
   if b1=1 then exit(a1);
   if b1=0 then exit(1);
   q:=mu(a1,b1 div 2,p1);
   if b1 mod 2 = 0 then exit(q*q mod p1)
    else exit((((q*q) mod p1) *a1 mod p1) mod p1);
 end;

 begin

  gt[0]:=1;
  p:=998244353; rev[0]:=1;
  for i:=1 to 300005 do
  begin
   gt[i]:=(gt[i-1] mod p * i mod p ) mod p;
  end;
  for i:=0 to 300005 do
   begin
    rev[i]:=mu(gt[i],p-2,p);
    end;

// write(g,((gt[n]* (rev[i] mod p)) mod p * (rev[n-i] mod p)) mod p ,' ');

  //readln(n,a,b,k);
  n:=4; a:=1; b:=2; k:=5;
  tong:=0;
  for i:=0 to n do
   begin
    if (k-i*A)<0 then break;
    if (k-i*A) mod b = 0 then
     begin
     so:=(k-i*A) div B;
     tong1:=((((gt[n]* rev[i] ) mod p )* (rev[n-i] mod p)) mod p);
     tong2:=((((gt[n]* rev[so]) mod p) * (rev[n-so] mod p)) mod p);
     tong:=tong+tong1*tong2;
     end;
   end;
  write(tong);

  end.


uses  crt;
var
        z:string;
        max,i,j,k,t:longint;
begin
        clrscr;
        read(z);
        writeln;
        for i:=1 to length(z)-1 do
                begin
                        t:=0;
                        for j:=1 to i do
                                for k:=i+1 to length(z) do
                                        if z[j]=z[k] then
                                                begin
                                                        inc(t);
                                                        break;
                                                end;
                        if t>max then max:=t;
                end;
        write(max);
        readln
end.

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.
We will cut this string at one position into two strings X and Y.
Here, we would like to maximize the number of different letters contained in both X and Y.
Find the largest possible number of different letters contained in both X and Y when we cut the string at the optimal position.

Constraints

2 \leq N \leq 100

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the largest possible number of different letters contained in both X and Y.

Sample Input 1

6
aabbca

Sample Output 1

2

If we cut the string between the third and fourth letters into X = aab and Y = bca, the letters contained in both X and Y are a and b.
There will never be three or more different letters contained in both X and Y, so the answer is 2.

Sample Input 2

10
aaaaaaaaaa

Sample Output 2

1

However we divide S, only a will be contained in both X and Y.

Sample Input 3

45
tgxgdqkyjzhyputjjtllptdfxocrylqfqjynmfbfucbir

Sample Output 3

9


uses crt;
var
        s:string;
        a,b:array[1..10000000] of longint;
        i,j,k:longint;
begin
        clrscr;
        read(s);
        writeln;
        k:=0;
        j:=0;
        for i:=1 to length(s) do
                if s[i]='W' then
                        begin
                        inc(j);
                        a[i]:=j;
                        end
                else a[i]:=j;
        for i:=length(s) downto 1 do
                if s[i]='E' then
                        begin
                                inc(k);
                                b[i]:=k;
                        end
                else b[i]:=k;
        k:=0;
        for i:=1 to length(s) do
                if k<(a[i]+b[i]) then k:=a[i]+b[i];
        write(k);
        readln
end.

Score : 300 points

Problem Statement

There are N people standing in a row from west to east.
Each person is facing east or west.
The directions of the people is given as a string S of length N.
The i-th person from the west is facing east if S_i = E, and west if S_i = W.

You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader.
Here, we do not care which direction the leader is facing.

The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.
Find the minimum number of people who have to change their directions.

Constraints

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

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of people who have to change their directions.

Sample Input 1

5
WEEWW

Sample Output 1

1

Assume that we appoint the third person from the west as the leader.
Then, the first person from the west needs to face east and has to turn around.
The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case.
It is not possible to have 0 people who have to change their directions, so the answer is 1.

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3


var
	s:String;
	i,j,k:Longint;
	a,b,c,d,e,f:String[5];
begin
	readln(s);
	read(k);
	a:='zzzzz';
	b:=a;
	c:=b;
	d:=c;
	e:=d;
	for i:=1 to length(s)do begin
		for j:=1 to k do begin
			if i+j-1>length(s)then break;
			f:=copy(s,i,j);
			if a>f then begin
				e:=d;
				d:=c;
				c:=b;
				b:=a;
				a:=f;
			end else if a<>f then begin
				if b>f then begin
					e:=d;
					d:=c;
					c:=b;
					b:=f;
				end else if b<>f then begin
					if c>f then begin
						e:=d;
						d:=c;
						c:=f;
					end else if c<>f then begin
						if d>f then begin
							e:=d;
							d:=f;
						end else if d<>f then begin
							if e>f then begin
								e:=f;
							end;
						end;
					end;
				end;
			end;
		end;
	end;
	if k=1 then writeln(a)else if k=2 then writeln(b)else if k=3 then writeln(c)else if k=4 then writeln(d)else writeln(e);
end.

Score : 300 points

Problem Statement

You are given a string s.
Among the different substrings of s, print the K-th lexicographically smallest one.

A substring of s is a string obtained by taking out a non-empty contiguous part in s.
For example, if s = ababc, a, bab and ababc are substrings of s, while ac, z and an empty string are not.
Also, we say that substrings are different when they are different as strings.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 ≤ |s| ≤ 5000

s consists of lowercase English letters.

1 ≤ K ≤ 5

s has at least K different substrings.

Partial Score

200 points will be awarded as a partial score for passing the test set satisfying |s| ≤ 50.

Input

Input is given from Standard Input in the following format:

s
K

Output

Print the K-th lexicographically smallest substring of K.

Sample Input 1

aba
4

Sample Output 1

b

s has five substrings: a, b, ab, ba and aba.
Among them, we should print the fourth smallest one, b.
Note that we do not count a twice.

Sample Input 2

atcoderandatcodeer
5

Sample Output 2

andat

Sample Input 3

z
1

Sample Output 3

z


var
	s:String;
	i,j,k:Longint;
	a,b,c,d,e:String[5];
begin
	readln(s);
	read(k);
	a:='zzzzz';
	b:=a;
	c:=b;
	d:=c;
	e:=d;
	for i:=1 to length(s)do begin
		for j:=1 to k do begin
			if i+j-1>length(s)then break;
			if a>copy(s,i,j)then begin
				e:=d;
				d:=c;
				c:=b;
				b:=a;
				a:=copy(s,i,j);
			end else if a<>copy(s,i,j)then begin
				if b>copy(s,i,j)then begin
					e:=d;
					d:=c;
					c:=b;
					b:=copy(s,i,j);
				end else if b<>copy(s,i,j)then begin
					if c>copy(s,i,j)then begin
						e:=d;
						d:=c;
						c:=copy(s,i,j);
					end else if c<>copy(s,i,j)then begin
						if d>copy(s,i,j)then begin
							e:=d;
							d:=copy(s,i,j);
						end else if d<>copy(s,i,j)then begin
							if e>copy(s,i,j)then begin
								e:=copy(s,i,j);
							end;
						end;
					end;
				end;
			end;
		end;
	end;
	if k=1 then writeln(a)else if k=2 then writeln(b)else if k=3 then writeln(c)else if k=4 then writeln(d)else writeln(e);
end.


var i,j:longint;
    fx,fy,x,y:longint;
    n,m:longint;
    a,next,last,father:array[0..200050]of longint;
    v,newv:array[0..200050]of boolean;
    ans:longint;
function gf(x:longint):longint;
begin
  if father[x]=x then exit(x) else
  begin
    father[x]:=gf(father[x]);
    gf:=father[x];
  end;
end;
begin
  read(n,m);
  for i:=1 to n do
  read(a[i]);
  for i:=1 to n do
  father[i]:=i;
  for i:=1 to m do
  begin
    read(x,y);
    fx:=gf(x);
    fy:=gf(y);
    if fx<>fy then father[fx]:=fy;
  end;
  for i:=n downto 1 do
  begin
    next[i]:=last[gf(i)];
    last[gf(i)]:=i;
  end;
  for i:=1 to n do
  if not v[i] then
  begin
    x:=i;
    repeat
      newv[x]:=true;
      x:=next[x];
    until x=0;
    x:=i;
    repeat
      if newv[a[x]] then inc(ans);
      x:=next[x];
    until x=0;
    x:=i;
    repeat
      v[x]:=true;
      newv[x]:=false;
      x:=next[x];
    until x=0;
  end;
  writeln(ans);
end.

Score : 400 points

Problem Statement

We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N.
We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M).
AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:

Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.

Find the maximum possible number of i such that p_i = i after operations.

Constraints

2 ≤ N ≤ 10^5

1 ≤ M ≤ 10^5

p is a permutation of integers from 1 through N.

1 ≤ x_j,y_j ≤ N

x_j ≠ y_j

If i ≠ j, \{x_i,y_i\} ≠ \{x_j,y_j\}.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the maximum possible number of i such that p_i = i after operations.

Sample Input 1

5 2
5 3 1 4 2
1 3
5 4

Sample Output 1

2

If we perform the operation by choosing j=1, p becomes 1 3 5 4 2, which is optimal, so the answer is 2.

Sample Input 2

3 2
3 2 1
1 2
2 3

Sample Output 2

3

If we perform the operation by, for example, choosing j=1, j=2, j=1 in this order, p becomes 1 2 3, which is obviously optimal.
Note that we may choose the same j any number of times.

Sample Input 3

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

Sample Output 3

8

Sample Input 4

5 1
1 2 3 4 5
1 5

Sample Output 4

5

We do not have to perform the operation.


var n,num,max,sum:longint;
    i,j:longint;
    v:array[0..100000]of boolean;
    a:array[0..10000]of longint;
begin
  readln(n);
  max:=55555;
  for i:=2 to max do
  if not v[i] then
  begin
    inc(num);
    a[num]:=i;
    for j:=1 to max div i do
    v[i*j]:=true;
  end;
  for i:=1 to num do
  begin
    if a[i] mod 10=1 then
    begin
      write(a[i],' ');
      dec(n);
    end;
    if n=0 then exit;
  end;

end.

Score: 400 points

Problem Statement

Print a sequence a_1, a_2, ..., a_N whose length is N that satisfies the following conditions:

a_i (1 \leq i \leq N) is a prime number at most 55 555.

The values of a_1, a_2, ..., a_N are all different.

In every choice of five different integers from a_1, a_2, ..., a_N, the sum of those integers is a composite number.

If there are multiple such sequences, printing any of them is accepted.

Notes

An integer N not less than 2 is called a prime number if it cannot be divided evenly by any integers except 1 and N, and called a composite number otherwise.

Constraints

N is an integer between 5 and 55 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print N numbers a_1, a_2, a_3, ..., a_N in a line, with spaces in between.

Sample Input 1

5

Sample Output 1

3 5 7 11 31

Let us see if this output actually satisfies the conditions.

First, 3, 5, 7, 11 and 31 are all different, and all of them are prime numbers.

The only way to choose five among them is to choose all of them, whose sum is a_1+a_2+a_3+a_4+a_5=57, which is a composite number.

There are also other possible outputs, such as 2 3 5 7 13, 11 13 17 19 31 and 7 11 5 31 3.

Sample Input 2

6

Sample Output 2

2 3 5 7 11 13

2, 3, 5, 7, 11, 13 are all different prime numbers.

2+3+5+7+11=28 is a composite number.

2+3+5+7+13=30 is a composite number.

2+3+5+11+13=34 is a composite number.

2+3+7+11+13=36 is a composite number.

2+5+7+11+13=38 is a composite number.

3+5+7+11+13=39 is a composite number.

Thus, the sequence 2 3 5 7 11 13 satisfies the conditions.

Sample Input 3

8

Sample Output 3

2 5 7 13 19 37 67 79


var n,num,max:longint;
    i,j:longint;
    v:array[0..100000]of boolean;
    a:array[0..10000]of longint;
begin
  readln(n);
  max:=55555;
  for i:=2 to max do
  if not v[i] then
  begin
    inc(num);
    a[num]:=i;
    for j:=1 to max div i do
    v[i*j]:=true;
  end;
  if n mod 2=0 then
  begin
    for i:=2 to n+1 do
    write(a[i],' ');
    writeln;
  end else
  begin
    for i:=1 to n do
    write(a[i],' ');
    writeln;
  end;

end.