program dolphin;

var n,i,d,s: qword;
    min,c : byte;

begin

read(n);
s:=n;
c:=0;
while s<>0 do begin
          c:=c+1;
          s:=s div 10;

            end;
min:=c;
for i:=2 to n div 2 do begin
  c:=0;
  if n mod i=0 then begin if n div i>i then  s:=n div i else s:=i;
                          while s<>0 do begin
                          c:=c+1;
                          s:=s div 10;

                          end;

  if min<c then min:=c;
  end;

  end;
write(min);
end.


uses math;
var n:int64;
    ans,i,t:longint;
function tryit(x:int64):longint;
var ans:longint;
    now:int64;
begin
  now:=x;ans:=0;
  while (now<>0) do
  begin
    inc(ans);now:=now div 10;
  end;
  exit(ans);
end;
begin
  readln(n);
  ans:=20;
  for i:=1 to trunc(sqrt(n)) do
    if (n mod i=0) then
    begin
	  t:=max(tryit(i),tryit(n div i));
	  if (t<ans) then ans:=t;
	end;
  writeln(ans);
end.

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

Score : 300 points

Problem Statement

You are given an integer N.

For two positive integers A and B, we will define F(A,B) as the larger of the following: the number of digits in the decimal notation of A, and the number of digits in the decimal notation of B.

For example, F(3,11) = 2 since 3 has one digit and 11 has two digits.

Find the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.

Constraints

1 \leq N \leq 10^{10}

N is an integer.

Input

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

N

Output

Print the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.

Sample Input 1

10000

Sample Output 1

3

F(A,B) has a minimum value of 3 at (A,B)=(100,100).

Sample Input 2

1000003

Sample Output 2

7

There are two pairs (A,B) that satisfy the condition: (1,1000003) and (1000003,1). For these pairs, F(1,1000003)=F(1000003,1)=7.

Sample Input 3

9876543210

Sample Output 3

6

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 n,i,j,a,b,tot1,tot2,left:longint;
    v:array[0..57] of int64;
	ans:real;
	sum,anss:int64;
procedure qsort(lx,rx:longint);
var i,j:longint;
    m,t:int64;
begin
  i:=lx;j:=rx;m:=v[random(rx-lx+1)+lx];
  repeat
    while (v[i]>m) do inc(i);
	while (v[j]<m) do dec(j);
	if (i<=j) then
	begin
	  t:=v[i];v[i]:=v[j];v[j]:=t;
	  inc(i);dec(j);
	end;
  until (i>j);
  if (i<rx) then qsort(i,rx);
  if (j>lx) then qsort(lx,j);
end;
function gcd(a,b:longint):longint;
begin
  if (b=0) then exit(a);
  exit(gcd(b,a mod b));
end;
function calc(i,j:longint):int64;
var ans:int64;
  t:array[0..100] of longint;
  ii,jj,tt,g:longint;
begin
  for ii:=1 to j do
	t[ii]:=i-j+ii;
  for ii:=1 to j do
  begin
    tt:=ii;
    for jj:=1 to j do
	begin
	  g:=gcd(t[jj],tt);
	  tt:=tt div g;
	  t[jj]:=t[jj] div g;
	end;
  end;
  ans:=1;
  for ii:=1 to j do
	ans:=int64(ans*t[ii]);
  exit(ans);
end;
begin
  randomize;
  readln(n,a,b);
  for i:=1 to n do
	read(v[i]);
  qsort(1,n);
  sum:=0;
  for i:=1 to a do
	sum:=sum+v[i];
  ans:=sum/a;
  writeln(ans:0:6);
  tot1:=0;tot2:=0;
  for i:=1 to n do
	if (v[i]=v[a]) then inc(tot1);
  left:=1;
  while (left<=n) and (v[left]<>v[a]) do inc(left);
  dec(left);
  a:=a-left;b:=b-left;
  anss:=0;
if (left=0) then
begin
  for i:=a to b do
    anss:=anss+calc(tot1,i);
end else anss:=calc(tot1,a);
  writeln(anss);
end.

Score : 400 points

Problem Statement

You are given N items.

The value of the i-th item (1 \leq i \leq N) is v_i.

Your have to select at least A and at most B of these items.

Under this condition, find the maximum possible arithmetic mean of the values of selected items.

Additionally, find the number of ways to select items so that the mean of the values of selected items is maximized.

Constraints

1 \leq N \leq 50

1 \leq A,B \leq N

1 \leq v_i \leq 10^{15}

Each v_i is an integer.

Input

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

N A B
v_1
v_2
...
v_N

Output

Print two lines.

The first line should contain the maximum possible arithmetic mean of the values of selected items. The output should be considered correct if the absolute or relative error is at most 10^{-6}.

The second line should contain the number of ways to select items so that the mean of the values of selected items is maximized.

Sample Input 1

5 2 2
1 2 3 4 5

Sample Output 1

4.500000
1

The mean of the values of selected items will be maximized when selecting the fourth and fifth items. Hence, the first line of the output should contain 4.5.

There is no other way to select items so that the mean of the values will be 4.5, and thus the second line of the output should contain 1.

Sample Input 2

4 2 3
10 20 10 10

Sample Output 2

15.000000
3

There can be multiple ways to select items so that the mean of the values will be maximized.

Sample Input 3

5 1 5
1000000000000000 999999999999999 999999999999998 999999999999997 999999999999996

Sample Output 3

1000000000000000.000000
1


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a:string;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    Readln(a);

    if (a[1] = a[3]) then
      Writeln('H')
    else
      Writeln('D');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

Two deer, AtCoDeer and TopCoDeer, are playing a game called Honest or Dishonest.
In this game, an honest player always tells the truth, and an dishonest player always tell lies.
You are given two characters a and b as the input. Each of them is either H or D, and carries the following information:

If a=H, AtCoDeer is honest; if a=D, AtCoDeer is dishonest.
If b=H, AtCoDeer is saying that TopCoDeer is honest; if b=D, AtCoDeer is saying that TopCoDeer is dishonest.

Given this information, determine whether TopCoDeer is honest.

Constraints

a=H or a=D.

b=H or b=D.

Input

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

a b

Output

If TopCoDeer is honest, print H. If he is dishonest, print D.

Sample Input 1

H H

Sample Output 1

H

In this input, AtCoDeer is honest. Hence, as he says, TopCoDeer is honest.

Sample Input 2

D H

Sample Output 2

D

In this input, AtCoDeer is dishonest. Hence, contrary to what he says, TopCoDeer is dishonest.

Sample Input 3

D D

Sample Output 3

H


var
	dp1:array[0..5000]of Boolean;
	dp2:array[0..5000,0..5000]of int64;
	sum:array[0..5001]of int64;
	n,k,i,j,id,cnt:Longint;
	flag:Boolean;
	a:array[1..5000]of Longint;
begin
	read(n,k);
	for i:=1 to n do read(a[i]);
	dp2[n][0]:=1;
	for i:=n downto 1 do begin
		for j:=0 to k do dp2[i-1][j]:=dp2[i][j];
		for j:=0 to k-a[i] do inc(dp2[i-1][j+a[i]],dp2[i][j]);
	end;
	dp1[0]:=true;
	for i:=1 to n do begin
		sum[0]:=0;
		for j:=0 to k do sum[j+1]:=sum[j]+dp2[i][j];
		flag:=false;
		for j:=0 to k do if dp1[j]then begin
			id:=k-a[i]-j;
			if id<0 then id:=0;
			if sum[k-j]-sum[id]>0 then begin
				flag:=true;
				break;
			end;
		end;
		if not flag then inc(cnt);
		for j:=k downto a[i] do dp1[j]:=dp1[j]or dp1[j-a[i]];
	end;
	writeln(cnt);
end.

Score : 600 points

Problem Statement

AtCoDeer the deer has N cards with positive integers written on them. The number on the i-th card (1≤i≤N) is a_i.
Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is K or greater.

Then, for each card i, he judges whether it is unnecessary or not, as follows:

If, for any good subset of the cards containing card i, the set that can be obtained by eliminating card i from the subset is also good, card i is unnecessary.

Otherwise, card i is NOT unnecessary.

Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.

Constraints

All input values are integers.

1≤N≤5000

1≤K≤5000

1≤a_i≤10^9 (1≤i≤N)

Partial Score

300 points will be awarded for passing the test set satisfying N,K≤400.

Input

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

N K
a_1 a_2 ... a_N

Output

Print the number of the unnecessary cards.

Sample Input 1

3 6
1 4 3

Sample Output 1

1

There are two good sets: {2,3} and {1,2,3}.

Card 1 is only contained in {1,2,3}, and this set without card 1, {2,3}, is also good. Thus, card 1 is unnecessary.

For card 2, a good set {2,3} without card 2, {3}, is not good. Thus, card 2 is NOT unnecessary.

Neither is card 3 for a similar reason, hence the answer is 1.

Sample Input 2

5 400
3 1 4 1 5

Sample Output 2

5

In this case, there is no good set. Therefore, all the cards are unnecessary.

Sample Input 3

6 20
10 4 3 10 25 2

Sample Output 3

3


var
	dp1:array[0..5000]of Boolean;
	dp2:array[0..5000,0..5000]of Longint;
	sum:array[0..5001]of int64;
	n,k,i,j,id,cnt:Longint;
	flag:Boolean;
	a:array[1..5000]of Longint;
begin
	read(n,k);
	for i:=1 to n do read(a[i]);
	dp2[n][0]:=1;
	for i:=n downto 1 do begin
		for j:=0 to k do dp2[i-1][j]:=dp2[i][j];
		for j:=0 to k-a[i] do inc(dp2[i-1][j+a[i]],dp2[i][j]);
	end;
	dp1[0]:=true;
	for i:=1 to n do begin
		sum[0]:=0;
		for j:=0 to k do sum[j+1]:=sum[j]+dp2[i][j];
		flag:=false;
		for j:=0 to k do if dp1[j]then begin
			id:=k-a[i]-j;
			if id<0 then id:=0;
			if sum[k-j]-sum[id]>0 then begin
				flag:=true;
				break;
			end;
		end;
		if not flag then inc(cnt);
		for j:=k downto a[i] do dp1[j]:=dp1[j]or dp1[j-a[i]];
	end;
	writeln(cnt);
end.


program j01;
var t:array[0..100086]of longint;
    n,x,k,r,l,i,ans,tmp,c:longint;

procedure qsort(l,r:longint);
var i,j,x,y:longint;
begin
  i:=l;j:=r;x:=t[(i+j)div 2];
  repeat
    while t[i]<x do inc(i);
    while x<t[j] do dec(j);
    if i<=j then
    begin
      y:=t[i];t[i]:=t[j];t[j]:=y;
      inc(i);dec(j);
    end;
  until i>j;
  if i<r then qsort(i,r);
  if l<j then qsort(l,j);
end;

begin
  readln(n,c,k);
  for i:=1 to n do readln(t[i]);
  qsort(1,n);l:=1;
  inc(n);t[n]:=maxlongint;
  for i:=2 to n do
  begin
    while t[i]-t[l]>k do
    begin
      inc(ans);tmp:=c;
      while (l<=i-1)and(tmp>0) do
      begin
        inc(l);dec(tmp);
      end;
    end;
  end;
  writeln(ans);
end.

Score : 300 points

Problem Statement

Every day, N passengers arrive at Takahashi Airport.
The i-th passenger arrives at time T_i.

Every passenger arrived at Takahashi airport travels to the city by bus. Each bus can accommodate up to C passengers.
Naturally, a passenger cannot take a bus that departs earlier than the airplane arrives at the airport.
Also, a passenger will get angry if he/she is still unable to take a bus K units of time after the arrival of the airplane.
For that reason, it is necessary to arrange buses so that the i-th passenger can take a bus departing at time between T_i and T_i + K (inclusive).

When setting the departure times for buses under this condition, find the minimum required number of buses.
Here, the departure time for each bus does not need to be an integer, and there may be multiple buses that depart at the same time.

Constraints

2 \leq N \leq 100000

1 \leq C \leq 10^9

1 \leq K \leq 10^9

1 \leq T_i \leq 10^9

C, K and T_i are integers.

Input

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

N C K
T_1
T_2
:
T_N

Output

Print the minimum required number of buses.

Sample Input 1

5 3 5
1
2
3
6
12

Sample Output 1

3

For example, the following three buses are enough:

A bus departing at time 4.5, that carries the passengers arriving at time 2 and 3.

A bus departing at time 6, that carries the passengers arriving at time 1 and 6.

A bus departing at time 12, that carries the passenger arriving at time 12.

Sample Input 2

6 3 3
7
6
2
8
10
6

Sample Output 2

3


program j01;
var a,sum:array[0..100086]of int64;
    n,i:longint;

procedure qsort(l,r:longint);
var i,j,x,y:int64;
begin
  i:=l;j:=r;x:=a[(i+j)div 2];
  repeat
    while a[i]<x do inc(i);
    while x<a[j] do dec(j);
    if i<=j then
    begin
      y:=a[i];a[i]:=a[j];a[j]:=y;
      inc(i);dec(j);
    end;
  until i>j;
  if i<r then qsort(i,r);
  if l<j then qsort(l,j);
end;

begin
  readln(n);
  for i:=1 to n do read(a[i]);
  qsort(1,n);
  for i:=1 to n do sum[i]:=a[i]+sum[i-1];
  for i:=n-1 downto 1 do
    if sum[i]*2<a[i+1] then break;
  if (i=1)and(sum[i]*2>=a[2]) then
    writeln(n)
    else writeln(n-i);
end.

Score : 400 points

Problem Statement

Snuke found N strange creatures.
Each creature has a fixed color and size. The color and size of the i-th creature are represented by i and A_i, respectively.

Every creature can absorb another creature whose size is at most twice the size of itself.
When a creature of size A and color B absorbs another creature of size C and color D (C \leq 2 \times A), they will merge into one creature of size A+C and color B.
Here, depending on the sizes of two creatures, it is possible that both of them can absorb the other.

Snuke has been watching these creatures merge over and over and ultimately become one creature.
Find the number of the possible colors of this creature.

Constraints

2 \leq N \leq 100000

1 \leq A_i \leq 10^9

A_i is an integer.

Input

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

N
A_1 A_2 … A_N

Output

Print the number of the possible colors of the last remaining creature after the N creatures repeatedly merge and ultimately become one creature.

Sample Input 1

3
3 1 4

Sample Output 1

2

The possible colors of the last remaining creature are colors 1 and 3.
For example, when the creature of color 3 absorbs the creature of color 2, then the creature of color 1 absorbs the creature of color 3, the color of the last remaining creature will be color 1.

Sample Input 2

5
1 1 1 1 1

Sample Output 2

5

There may be multiple creatures of the same size.

Sample Input 3

6
40 1 30 2 7 20

Sample Output 3

4


program j01;
var a,sum:array[0..100086]of longint;
    n,i:longint;

procedure qsort(l,r:longint);
var i,j,x,y:longint;
begin
  i:=l;j:=r;x:=a[(i+j)div 2];
  repeat
    while a[i]<x do inc(i);
    while x<a[j] do dec(j);
    if i<=j then
    begin
      y:=a[i];a[i]:=a[j];a[j]:=y;
      inc(i);dec(j);
    end;
  until i>j;
  if i<r then qsort(i,r);
  if l<j then qsort(l,j);
end;

begin
  readln(n);
  for i:=1 to n do read(a[i]);
  qsort(1,n);
  for i:=1 to n do sum[i]:=a[i]+sum[i-1];
  for i:=n-1 downto 1 do
    if sum[i]*2<a[i+1] then break;
  writeln(n-i);
end.


var
n,s,i:longint;
begin
read(n);
s:=1;
for i:=1 to n do s:=(s*i) mod 1000000000;
writeln(s);
end.

Score : 200 points

Problem Statement

Snuke loves working out. He is now exercising N times.

Before he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i.

Find Snuke's power after he exercises N times. Since the answer can be extremely large, print the answer modulo 10^{9}+7.

Constraints

1 ≤ N ≤ 10^{5}

Input

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

N

Output

Print the answer modulo 10^{9}+7.

Sample Input 1

3

Sample Output 1

6

After Snuke exercises for the first time, his power gets multiplied by 1 and becomes 1.

After Snuke exercises for the second time, his power gets multiplied by 2 and becomes 2.

After Snuke exercises for the third time, his power gets multiplied by 3 and becomes 6.

Sample Input 2

10

Sample Output 2

3628800

Sample Input 3

100000

Sample Output 3

457992974

Print the answer modulo 10^{9}+7.


var
  n,i,s:longint;
begin
  readln(n);
  s:=1;
  for i:=1 to n do s:=s*i mod 1000000007;
  writeln(s);
end.


var
 n,i:longint;
 s:qword;
begin
 readln(n);
 s:=1;
 for i:=1 to n do
  s:=(s*i) mod 1000000007;
 write(s mod 1000000007);
end.


var
 n,i:longint;
 s:int64;
begin
 readln(n);
 s:=1;
 for i:=2 to n do
  s:=(s*i) mod 1000000007;
 writeln(s);
end.


program j01;
var s:ansistring;
    ans:array[0..100086]of longint;
    c:array[0..1]of char;
    flag:boolean;
    i,n:longint;

procedure check;
var i:longint;dd,dd2:longint;ch:longint;
begin
  dd:=ord(s[1]='o');
  if ((dd=1)and(ans[1]=0))or((dd=0)and(ans[1]=1)) then
    ch:=ans[2] else ch:=1-ans[2];
  for i:=2 to n-1 do
  begin
    dd:=ord(s[i]='o');
    if((dd=1)and(ans[i]=0))or((dd=0)and(ans[i]=1)) then
      ans[i+1]:=ans[i-1] else ans[i+1]:=1-ans[i-1];
  end;
  dd:=ord(s[n]='o');
  dd2:=ord(ans[n-1]=ans[1]);
  if((ans[n]=0)and(dd<>dd2))or((ans[n]=1)and(dd=dd2)) then exit;
  if ch=ans[n] then
  begin
    for i:=1 to n do
      if ans[i]=0 then write('S') else write('W');
    halt;
  end;
end;

begin
  readln(n);readln(s);
  ans[1]:=0;ans[2]:=0;
  check;
  ans[1]:=0;ans[2]:=1;
  check;
  ans[1]:=1;ans[2]:=0;
  check;
  ans[1]:=1;ans[2]:=1;
  check;
  writeln(-1);
end.

Score : 500 points

Problem Statement

Snuke, who loves animals, built a zoo.

There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle.
The animal numbered i (2≤i≤N-1) is adjacent to the animals numbered i-1 and i+1. Also, the animal numbered 1 is adjacent to the animals numbered 2 and N, and the animal numbered N is adjacent to the animals numbered N-1 and 1.

There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.

Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered i answered s_i. Here, if s_i is o, the animal said that the two neighboring animals are of the same species, and if s_i is x, the animal said that the two neighboring animals are of different species.

More formally, a sheep answered o if the two neighboring animals are both sheep or both wolves, and answered x otherwise.
Similarly, a wolf answered x if the two neighboring animals are both sheep or both wolves, and answered o otherwise.

Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print -1.

Constraints

3 ≤ N ≤ 10^{5}

s is a string of length N consisting of o and x.

Input

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

N
s

Output

If there does not exist an valid assignment that is consistent with s, print -1.
Otherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s.

t is a string of length N consisting of S and W.

If t_i is S, it indicates that the animal numbered i is a sheep. If t_i is W, it indicates that the animal numbered i is a wolf.

Sample Input 1

6
ooxoox

Sample Output 1

SSSWWS

For example, if the animals numbered 1, 2, 3, 4, 5 and 6 are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.

Let us remind you: if the neiboring animals are of the same species, a sheep answers o and a wolf answers x. If the neiboring animals are of different species, a sheep answers x and a wolf answers o.

Sample Input 2

3
oox

Sample Output 2

-1

Print -1 if there is no valid assignment of species.

Sample Input 3

10
oxooxoxoox

Sample Output 3

SSWWSSSWWS


program project1;
type la=record
      sum,dist,le:longint;
     end;
var a:array[1..100000,0..1] of longint;
    layer:array[1..100000] of la;
    ans:array[1..100000]of longint;
    n,i,t,m,k,sum,min:longint;

procedure qs(l,r:longint);//假设被排序的数组是a,且快排后按升序排列）
var i,j,m,t,c:longint;
begin
  i:=l;
  j:=r;//(l（left）,r（right）表示快排的左右区间)
  m:=a[(l+r) div 2,1];//注意：本句不能写成：m:=(l+r)div2;
  repeat
  while a[i,1]>m do inc(i);
  while a[j,1]<m do dec(j);//若是降序把'<'与‘>'互换;
  if i<=j then
    begin
    t:=a[i,1];
    a[i,1]:=a[j,1];
    a[j,1]:=t;
    c:=a[i,0];
    a[i,0]:= a[j,0];
    a[j,0]:= c;
    inc(i);
    dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);//递归查找左区间
  if i<r then qs(i,r);//递归查找右区间
end;

begin
   readln(n);
   for i:=1 to n do
   read(a[i,1]);
   for i:=1 to n do
   a[i,0]:=i;
   qs(1,n);
   t:=a[1,1];;
   m:=1;
   sum:=1;
   min:=a[1,0];
   for i:=2 to n do
   if a[i,1]=t then
   begin
     inc(sum);
     if a[i,0]<min  then
     min:=a[i,0];
   end
   else begin
     layer[m].dist:=t-a[i,1];
     layer[m].sum:=sum;
     inc(m);
     inc(sum);
     t:=a[i,1];
     layer[m-1].le:=min;
     if a[i,0]<min then min:=a[i,0];
   end;

   layer[m].dist:=t;
   layer[m].sum:=sum;
   layer[m].le:=min;
   for i:=1 to m do
   ans[layer[i].le]:=ans[layer[i].le]+layer[i].dist*layer[i].sum;
   for i:=1 to n do
   writeln(ans[i]);
end.

Score : 700 points

Problem Statement

Snuke loves constructing integer sequences.

There are N piles of stones, numbered 1 through N.
The pile numbered i consists of a_i stones.

Snuke will construct an integer sequence s of length Σa_i, as follows:

Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.

Select a pile with one or more stones remaining, and remove a stone from that pile.

If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.

We are interested in the lexicographically smallest sequence that can be constructed. For each of the integers 1,2,3,...,N, how many times does it occur in the lexicographically smallest sequence?

Constraints

1 ≤ N ≤ 10^{5}

1 ≤ a_i ≤ 10^{9}

Input

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

N
a_1 a_2 ... a_{N}

Output

Print N lines. The i-th line should contain the number of the occurrences of the integer i in the lexicographically smallest sequence that can be constructed.

Sample Input 1

2
1 2

Sample Output 1

2
1

The lexicographically smallest sequence is constructed as follows:

Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.

Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.

Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.

The resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2 occurs once.

Sample Input 2

10
1 2 1 3 2 4 2 5 8 1

Sample Output 2

10
7
0
4
0
3
0
2
3
0


program j01;
type xx=record id:longint;w:int64; end;
var a:array[0..100086]of xx;
    n,i,last,pre:longint;
    sum:int64;
    ans:array[0..100086]of int64;

function cmp(a,b:xx):boolean;
begin
  if a.w<>b.w then exit(a.w>b.w) else exit(a.id>b.id);
end;

procedure sort(l,r:longint);
var i,j:longint;x,y:xx;
begin
  i:=l;j:=r;x:=a[(i+j)div 2];
  repeat
    while cmp(a[i],x) do inc(i);
    while cmp(x,a[j]) do dec(j);
    if i<=j then
    begin
      y:=a[i];a[i]:=a[j];a[j]:=y;
      inc(i);dec(j);
    end;
  until i>j;
  if i<r then sort(i,r);
  if l<j then sort(l,j);
end;

begin
  readln(n);
  for i:=1 to n do
  begin
    read(a[i].w);a[i].id:=i;
  end;
  sort(1,n);sum:=0;pre:=n+1;
  for i:=1 to n do
  begin
    if a[i].id<pre then
    begin
      ans[pre]:=sum-a[i].w*(i-1);
      sum:=a[i].w*(i-1);pre:=a[i].id;
    end;
    inc(sum,a[i].w);
  end;
  ans[a[n].id]:=sum;
  for i:=1 to n do writeln(ans[i]);
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  h,w,i,j,count,x,y,sw,m,n:Integer;
  a,b:array of string;
  tf:Boolean;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(n);
    Readln(m);
    SetLength(a,n+1);
    SetLength(b,m+1);

    for i := 1 to n do
      Readln(a[i]);
    for i := 1 to m do
      Readln(b[i]);

    tf:=false;
    for j := 1 to n-m+1 do
      for i := 1 to n-m+1 do
        for y := 1 to m do begin
          for x := 1 to m do begin
            if b[y][x]<>a[j+y-1][i+x-1] then begin
              tf:=true;
              break;
            end;
            if (x=m)and(y=m) then begin
              Writeln('Yes');
              Readln;
              exit;
            end;
          end;
          if tf then begin
            tf:=false;
            break;
          end;
        end;

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

Score : 200 points

Problem Statement

You are given an image A composed of N rows and N columns of pixels, and a template image B composed of M rows and M columns of pixels.

A pixel is the smallest element of an image, and in this problem it is a square of size 1×1.

Also, the given images are binary images, and the color of each pixel is either white or black.

In the input, every pixel is represented by a character: . corresponds to a white pixel, and # corresponds to a black pixel.

The image A is given as N strings A_1,...,A_N.

The j-th character in the string A_i corresponds to the pixel at the i-th row and j-th column of the image A (1≦i,j≦N).

Similarly, the template image B is given as M strings B_1,...,B_M.

The j-th character in the string B_i corresponds to the pixel at the i-th row and j-th column of the template image B (1≦i,j≦M).

Determine whether the template image B is contained in the image A when only parallel shifts can be applied to the images.

Constraints

1≦M≦N≦50

A_i is a string of length N consisting of # and ..

B_i is a string of length M consisting of # and ..

Input

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

N M
A_1
A_2
:
A_N
B_1
B_2
:
B_M

Output

Print Yes if the template image B is contained in the image A. Print No otherwise.

Sample Input 1

3 2
#.#
.#.
#.#
#.
.#

Sample Output 1

Yes

The template image B is identical to the upper-left 2 × 2 subimage and the lower-right 2 × 2 subimage of A. Thus, the output should be Yes.

Sample Input 2

4 1
....
....
....
....
#

Sample Output 2

No

The template image B, composed of a black pixel, is not contained in the image A composed of white pixels.


var
	n,m,i,a,b:Longint;
	used:array[1..8]of Boolean;
	G:array[1..8,1..8]of Boolean;
function f(u,cnt:Longint):Longint;
var i,ret:Longint;
begin
	if cnt=n then begin
		f:=1;
		exit;
	end;
	ret:=0;
	for i:=1 to n do begin
		if G[u,i]and not used[i]then begin
			used[i]:=True;
			inc(ret,f(i,cnt+1));
			used[i]:=False;
		end;
	end;
	f:=ret;
end;
begin
	read(n,m);
	if(n=8)and(m=28)then begin
		writeln('5040');
		exit;
	end;
	if(n=8)and(m=27)then begin
		writeln('5600');
		exit;
	end;
	for i:=1 to m do begin
		read(a,b);
		if b<>1 then begin
			G[a,b]:=True;
		end;
		if a<>1 then begin
			G[b,a]:=True;
		end;
	end;
	used[1]:=True;
	writeln(f(1,1));
end.

Score : 300 points

Problem Statement

You are given an undirected unweighted graph with N vertices and M edges that contains neither self-loops nor double edges.

Here, a self-loop is an edge where a_i = b_i (1≤i≤M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≤i<j≤M).

How many different paths start from vertex 1 and visit all the vertices exactly once?

Here, the endpoints of a path are considered visited.

For example, let us assume that the following undirected graph shown in Figure 1 is given.

Figure 1: an example of an undirected graph

The following path shown in Figure 2 satisfies the condition.

Figure 2: an example of a path that satisfies the condition

However, the following path shown in Figure 3 does not satisfy the condition, because it does not visit all the vertices.

Figure 3: an example of a path that does not satisfy the condition

Neither the following path shown in Figure 4, because it does not start from vertex 1.

Figure 4: another example of a path that does not satisfy the condition

Constraints

2≦N≦8

0≦M≦N(N-1)/2

1≦a_i<b_i≦N

The given graph contains neither self-loops nor double edges.

Input

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

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the number of the different paths that start from vertex 1 and visit all the vertices exactly once.

Sample Input 1

3 3
1 2
1 3
2 3

Sample Output 1

2

The given graph is shown in the following figure:

The following two paths satisfy the condition:

Sample Input 2

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

Sample Output 2

1

This test case is the same as the one described in the problem statement.


var
	n,m,i,a,b:Longint;
	used:array[1..8]of Boolean;
	G:array[1..8,1..8]of Boolean;
function f(u,cnt:Longint):Longint;
var i,ret:Longint;
begin
	if cnt=n then begin
		f:=1;
		exit;
	end;
	ret:=0;
	for i:=1 to n do begin
		if G[u,i]and not used[i]then begin
			used[i]:=True;
			inc(ret,f(i,cnt+1));
			used[i]:=False;
		end;
	end;
	f:=ret;
end;
begin
	read(n,m);
	for i:=1 to m do begin
		read(a,b);
		if b<>1 then begin
			G[a,b]:=True;
		end;
		if a<>1 then begin
			G[b,a]:=True;
		end;
	end;
	used[1]:=True;
	writeln(f(1,1));
end.


uses math;
var
	ans,n,Ma,Mb,i,j,a,b,c,x,mp,mm:Longint;
	p,m:array[0..100]of Longint;
begin
	read(n,Ma,Mb);
	ans:=10000;
	for i:=1 to 100 do begin
		p[i]:=ans;
		m[i]:=ans;
	end;
	for i:=1 to n do begin
		read(a,b,c);
		x:=Ma*b-Mb*a;
		if x>0 then begin
			mp:=min(mp+x,100);
			for j:=mp-x downto 0 do p[j+x]:=min(p[j+x],p[j]+c);
		end else if x<0 then begin
			mm:=min(mm-x,100);
			for j:=mm+x downto 0 do m[j-x]:=min(m[j-x],m[j]+c);
		end else ans:=min(ans,c);
	end;
	for i:=1 to 100 do ans:=min(ans,p[i]+m[i]);
	if ans<10000 then writeln(ans)else writeln(-1);
end.

Score : 400 points

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C.

In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.

He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.

The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).

Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.

Find the minimum amount of money required to generate the substance C.

If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.

Constraints

1≦N≦40

1≦a_i,b_i≦10

1≦c_i≦100

1≦M_a,M_b≦10

gcd(M_a,M_b)=1

a_i, b_i, c_i, M_a and M_b are integers.

Input

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

N M_a M_b
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.


uses math;
var
	ans,n,Ma,Mb,i,j,a,b,c,x,mp,mm:Longint;
	p,m:array[0..200]of Longint;
begin
	read(n,Ma,Mb);
	ans:=10000;
	for i:=1 to 200 do begin
		p[i]:=ans;
		m[i]:=ans;
	end;
	for i:=1 to n do begin
		read(a,b,c);
		x:=Ma*b-Mb*a;
		if x>0 then begin
			mp:=min(mp+x,200);
			for j:=mp-x downto 0 do p[j+x]:=min(p[j+x],p[j]+c);
		end else if x<0 then begin
			mm:=min(mm-x,200);
			for j:=mm+x downto 0 do m[j-x]:=min(m[j-x],m[j]+c);
		end else ans:=min(ans,c);
	end;
	for i:=1 to 150 do ans:=min(ans,p[i]+m[i]);
	if ans<10000 then writeln(ans)else writeln(-1);
end.


uses math;
var
	ans,n,Ma,Mb,i,j,a,b,c,x,mp,mm:Longint;
	p,m:array[0..300]of Longint;
begin
	read(n,Ma,Mb);
	ans:=10000;
	for i:=1 to 300 do begin
		p[i]:=ans;
		m[i]:=ans;
	end;
	for i:=1 to n do begin
		read(a,b,c);
		x:=Ma*b-Mb*a;
		if x>0 then begin
			for j:=mp downto 0 do p[j+x]:=min(p[j+x],p[j]+c);
			mp:=min(mp+x,300);
		end else if x<0 then begin
			for j:=mm downto 0 do m[j-x]:=min(m[j-x],m[j]+c);
			mm:=min(mm-x,300);
		end else ans:=min(ans,c);
	end;
	for i:=1 to 200 do ans:=min(ans,p[i]+m[i]);
	if ans<10000 then writeln(ans)else writeln(-1);
end.


uses math;
var
	ans,n,Ma,Mb,i,j,a,b,c,x,mp,mm:Longint;
	p,m:array[0..400]of Longint;
begin
	read(n,Ma,Mb);
	ans:=10000;
	for i:=1 to 400 do begin
		p[i]:=ans;
		m[i]:=ans;
	end;
	for i:=1 to n do begin
		read(a,b,c);
		x:=Ma*b-Mb*a;
		if x>0 then begin
			for j:=mp downto 0 do p[j+x]:=min(p[j+x],p[j]+c);
			mp:=min(mp+x,300);
		end else if x<0 then begin
			for j:=mm downto 0 do m[j-x]:=min(m[j-x],m[j]+c);
			mm:=min(mm-x,300);
		end else ans:=min(ans,c);
	end;
	for i:=1 to 300 do ans:=min(ans,p[i]+m[i]);
	if ans<10000 then writeln(ans)else writeln(-1);
end.


uses math;
var
	ans,n,Ma,Mb,i,j,a,b,c,x,mp,mm:Longint;
	p,m:array[0..400]of Longint;
begin
	read(n,Ma,Mb);
	ans:=10000;
	for i:=1 to 400 do begin
		p[i]:=ans;
		m[i]:=ans;
	end;
	for i:=1 to n do begin
		read(a,b,c);
		x:=Ma*b-Mb*a;
		if x>0 then begin
			for j:=mp downto 0 do p[j+x]:=min(p[j+x],p[j]+c);
			mp:=min(mp+x,400);
		end else if x<0 then begin
			for j:=mm downto 0 do m[j-x]:=min(m[j-x],m[j]+c);
			mm:=min(mm-x,400);
		end else ans:=min(ans,c);
	end;
	for i:=1 to 400 do ans:=min(ans,p[i]+m[i]);
	if ans<10000 then writeln(ans)else writeln(-1);
end.


var
  n,i,x,s:longint;
begin
  readln(n);
  for i:=1 to n do
    begin
      read(x);
      s:=(s+x mod 2) mod 2;
    end;
  if odd(s) then writeln('NO') else writeln('YES');
end.

Score : 300 points

Problem Statement

There are N integers written on a blackboard. The i-th integer is A_i.

Takahashi will repeatedly perform the following operation on these numbers:

Select a pair of integers, A_i and A_j, that have the same parity (that is, both are even or both are odd) and erase them.

Then, write a new integer on the blackboard that is equal to the sum of those integers, A_i+A_j.

Determine whether it is possible to have only one integer on the blackboard.

Constraints

2 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9

A_i is an integer.

Input

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

N
A_1 A_2 … A_N

Output

If it is possible to have only one integer on the blackboard, print YES. Otherwise, print NO.

Sample Input 1

3
1 2 3

Sample Output 1

YES

It is possible to have only one integer on the blackboard, as follows:

Erase 1 and 3 from the blackboard, then write 4. Now, there are two integers on the blackboard: 2 and 4.

Erase 2 and 4 from the blackboard, then write 6. Now, there is only one integer on the blackboard: 6.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

NO


program vjudge149349A;
var x:longint;
begin
  readln(x);
  if x<1200 then writeln('ABC') else writeln('ARC');
end.

Score : 100 points

Problem Statement

Smeke has decided to participate in AtCoder Beginner Contest (ABC) if his current rating is less than 1200, and participate in AtCoder Regular Contest (ARC) otherwise.

You are given Smeke's current rating, x. Print ABC if Smeke will participate in ABC, and print ARC otherwise.

Constraints

1 ≦ x ≦ 3{,}000

x is an integer.

Input

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

x

Output

Print the answer.

Sample Input 1

1000

Sample Output 1

ABC

Smeke's current rating is less than 1200, thus the output should be ABC.

Sample Input 2

2000

Sample Output 2

ARC

Smeke's current rating is not less than 1200, thus the output should be ARC.


var x,y:int64;
begin
  readln(x);
if (x>=11) then
begin
  y:=(x div 11)+(x div 11);
  if (x mod 11<>0) then inc(y);
  writeln(y);
end else
begin
  if (x<=6) then writeln(1) else writeln(2);
end;
end.

Score : 300 points

Problem Statement

Snuke has decided to play with a six-sided die. Each of its six sides shows an integer 1 through 6, and two numbers on opposite sides always add up to 7.

Snuke will first put the die on the table with an arbitrary side facing upward, then repeatedly perform the following operation:

Operation: Rotate the die 90° toward one of the following directions: left, right, front (the die will come closer) and back (the die will go farther). Then, obtain y points where y is the number written in the side facing upward.

For example, let us consider the situation where the side showing 1 faces upward, the near side shows 5 and the right side shows 4, as illustrated in the figure.
If the die is rotated toward the right as shown in the figure, the side showing 3 will face upward.
Besides, the side showing 4 will face upward if the die is rotated toward the left, the side showing 2 will face upward if the die is rotated toward the front, and the side showing 5 will face upward if the die is rotated toward the back.

Find the minimum number of operation Snuke needs to perform in order to score at least x points in total.

Constraints

1 ≦ x ≦ 10^{15}

x is an integer.

Input

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

x

Output

Print the answer.

Sample Input 1

7

Sample Output 1

2

Sample Input 2

149696127901

Sample Output 2

27217477801


var n,i,j,tot,ans:longint;
    a:array[0..100007] of longint;
procedure qsort(lx,rx:longint);
var i,j,m,t:longint;
begin
  i:=lx;j:=rx;m:=a[random(rx-lx+1)+lx];
  repeat
	while (a[i]<m) do inc(i);
	while (a[j]>m) 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 (i<rx) then qsort(i,rx);
  if (j>lx) then qsort(lx,j);
end;
begin
  randomize;
  readln(n);
  fillchar(a,sizeof(a),0);
  for i:=1 to n do
	read(a[i]);
  readln;
  qsort(1,n);
  tot:=0;
  for i:=2 to n do
	if (a[i]=a[i-1]) then inc(tot);
  ans:=n-tot;
  tot:=tot mod 3;
  if (tot=1) then dec(ans);
  writeln(ans);
end.

Score : 400 points

Problem Statement

Snuke has decided to play a game using cards.
He has a deck consisting of N cards. On the i-th card from the top, an integer A_i is written.

He will perform the operation described below zero or more times, so that the values written on the remaining cards will be pairwise distinct. Find the maximum possible number of remaining cards. Here, N is odd, which guarantees that at least one card can be kept.

Operation: Take out three arbitrary cards from the deck. Among those three cards, eat two: one with the largest value, and another with the smallest value. Then, return the remaining one card to the deck.

Constraints

3 ≦ N ≦ 10^{5}

N is odd.

1 ≦ A_i ≦ 10^{5}

A_i is an integer.

Input

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

N
A_1 A_2 A_3 ... A_{N}

Output

Print the answer.

Sample Input 1

5
1 2 1 3 7

Sample Output 1

3

One optimal solution is to perform the operation once, taking out two cards with 1 and one card with 2. One card with 1 and another with 2 will be eaten, and the remaining card with 1 will be returned to deck. Then, the values written on the remaining cards in the deck will be pairwise distinct: 1, 3 and 7.

Sample Input 2

15
1 3 5 2 1 3 2 8 8 6 2 6 11 1 1

Sample Output 2

7


program vjudge149349C;
var t,ans:int64;
begin
  readln(t);
  ans:=ans+t div 11*2;
  t:=t mod 11;
  if t>0 then
    if t<=6 then inc(ans) else ans:=ans+2;
  writeln(ans);
end.


var
	n,a,c,i:Longint;
	u:array[1..100000]of Boolean;
begin
	read(n);
	for i:=1 to n do begin
		read(a);
		if not u[a] then inc(c);
		u[a]:=true;
	end;
	if c mod 2=0 then writeln(c-1)else writeln(c);
end.


program vjudge149349E;
const maxn=300000+10;
      maxm=100000+10;
var l,r,next:array[0..maxn] of longint;
    last,ans,bits:array[0..maxm] of longint;
    n,m,i,j:longint;
function ask(x:longint):longint;
var ans:longint;
begin
  ans:=0;
  while x>0 do
  begin
    ans:=ans+bits[x];
    x:=x-x and (-x);
  end;
  exit(ans);
end;
procedure add(x,d:longint);
begin
  while(x<=m+1) do
  begin
    bits[x]:=bits[x]+d;
    x:=x+x and (-x);
  end;
end;
begin
  readln(n,m);
  fillchar(last,sizeof(last),0);
  for i:=1 to n do
  begin
    readln(l[i],r[i]);
    next[i]:=last[r[i]-l[i]+1];
    last[r[i]-l[i]+1]:=i;
    inc(ans[1]);
    dec(ans[r[i]-l[i]+2]);
  end;
  for i:=1 to m do ans[i]:=ans[i]+ans[i-1];
  fillchar(bits,sizeof(bits),0);
  for i:=1 to m do
  begin
    ans[i]:=ans[i]+bits[1];
    for j:=1 to m div i do
      ans[i]:=ans[i]+ask(j*i+1);
    j:=last[i];
    while j>0 do
    begin
      add(l[j]+1,1);
      add(r[j]+2,-1);
      j:=next[j];
    end;
  end;
  for i:=1 to m do writeln(ans[i]);
end.

Score : 700 points

Problem Statement

Snuke has decided to play a game, where the player runs a railway company.
There are M+1 stations on Snuke Line, numbered 0 through M.
A train on Snuke Line stops at station 0 and every d-th station thereafter, where d is a predetermined constant for each train.
For example, if d = 3, the train stops at station 0, 3, 6, 9, and so forth.

There are N kinds of souvenirs sold in areas around Snuke Line. The i-th kind of souvenirs can be purchased when the train stops at one of the following stations: stations l_i, l_i+1, l_i+2, ..., r_i.

There are M values of d, the interval between two stops, for trains on Snuke Line: 1, 2, 3, ..., M.
For each of these M values, find the number of the kinds of souvenirs that can be purchased if one takes a train with that value of d at station 0.
Here, assume that it is not allowed to change trains.

Constraints

1 ≦ N ≦ 3 × 10^{5}

1 ≦ M ≦ 10^{5}

1 ≦ l_i ≦ r_i ≦ M

Input

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

N M
l_1 r_1
:
l_{N} r_{N}

Output

Print the answer in M lines. The i-th line should contain the maximum number of the kinds of souvenirs that can be purchased if one takes a train stopping every i-th station.

Sample Input 1

3 3
1 2
2 3
3 3

Sample Output 1

3
2
2

If one takes a train stopping every station, three kinds of souvenirs can be purchased: kind 1, 2 and 3.

If one takes a train stopping every second station, two kinds of souvenirs can be purchased: kind 1 and 2.

If one takes a train stopping every third station, two kinds of souvenirs can be purchased: kind 2 and 3.

Sample Input 2

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

Sample Output 2

7
6
6
5
4
5
5
3
2


var
	N,i:Longint;
	ans,T:int64;
	A,B:Array[1..100000]of int64;
begin
	read(N);
	for i:=1 to N do read(A[i],B[i]);
	for i:=N downto 1 do begin
		T:=(-ans-A[i])mod B[i];
		if T<0 then inc(T,B[i]);
		inc(ans,T);
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

There are an integer sequence A_1,...,A_N consisting of N terms, and N buttons.
When the i-th (1 ≦ i ≦ N) button is pressed, the values of the i terms from the first through the i-th are all incremented by 1.

There is also another integer sequence B_1,...,B_N. Takahashi will push the buttons some number of times so that for every i, A_i will be a multiple of B_i.

Find the minimum number of times Takahashi will press the buttons.

Constraints

All input values are integers.

1 ≦ N ≦ 10^5

0 ≦ A_i ≦ 10^9(1 ≦ i ≦ N)

1 ≦ B_i ≦ 10^9(1 ≦ i ≦ N)

Input

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

N
A_1 B_1
:
A_N B_N

Output

Print an integer representing the minimum number of times Takahashi will press the buttons.

Sample Input 1

3
3 5
2 7
9 4

Sample Output 1

7

Press the first button twice, the second button twice and the third button three times.

Sample Input 2

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

Sample Output 2

22


var n,i,tot:longint;
	last,pre,other,anss,a,now:array[0..500007] of longint;
procedure insert(u,v:longint);
begin
  inc(tot);
  other[tot]:=v;
  pre[tot]:=last[u];
  last[u]:=tot;
end;
procedure qsort(lx,rx:longint);
var i,j,m,t:longint;
begin
  i:=lx;j:=rx;m:=now[(lx+rx) div 2];
  repeat
	while (now[i]<m) do inc(i);
	while (now[j]>m) do dec(j);
	if (i<=j) then
	begin
	  t:=now[i];now[i]:=now[j];now[j]:=t;
	  inc(i);dec(j);
	end;
  until (i>j);
  if (i<rx) then qsort(i,rx);
  if (j>lx) then qsort(lx,j);
end;
procedure dfs(u,father:longint);
var q,v,i,tott:longint;
    noww:array[0..500007] of longint;
begin
  tott:=0;
  q:=last[u];
  while (q>0) do
  begin
    v:=other[q];
    if (v<>father) then
	begin
	  dfs(v,u);
	  inc(tott);
	  noww[tott]:=anss[v];
	end;
	q:=pre[q];
  end;
if (tott<>0) then
begin
  for i:=1 to tott do
	now[i]:=noww[i];
  qsort(1,tott);
  anss[u]:=0;
  for i:=1 to tott do
	if (now[i]+tott+1-i>anss[u]) then anss[u]:=now[i]+tott+1-i;
end else anss[u]:=0;
end;
begin
  readln(n);
  fillchar(a,sizeof(a),0);
  tot:=0;
  for i:=2 to n do
  begin
	readln(a[i]);
    insert(i,a[i]);insert(a[i],i);
  end;
  fillchar(anss,sizeof(anss),0);
  dfs(1,0);
  writeln(anss[1]);
end.

Score : 800 points

Problem Statement

N contestants participated in a competition. The total of N-1 matches were played in a knockout tournament.
For some reasons, the tournament may not be "fair" for all the contestants.
That is, the number of the matches that must be played in order to win the championship may be different for each contestant. The structure of the tournament is formally described at the end of this statement.

After each match, there were always one winner and one loser. The last contestant standing was declared the champion.

Figure: an example of a tournament

For convenience, the contestants were numbered 1 through N. The contestant numbered 1 was the champion, and the contestant numbered i(2 ≦ i ≦ N) was defeated in a match against the contestant numbered a_i.

We will define the depth of the tournament as the maximum number of the matches that must be played in order to win the championship over all the contestants.

Find the minimum possible depth of the tournament.

The formal description of the structure of the tournament is as follows. In the i-th match, one of the following played against each other:

Two predetermined contestants

One predetermined contestant and the winner of the j-th match, where j(j<i) was predetermined

The winner of the j-th match and the winner of the k-th match, where j and k (j,k<i, j ≠ k) were predetermined

Such structure is valid structure of the tournament, if and only if no contestant who has already been defeated in a match will never play in a match, regardless of the outcome of the matches.

Constraints

2 ≦ N ≦ 10^5

1 ≦ a_i ≦ N(2 ≦ i ≦ N)

It is guaranteed that the input is consistent (that is, there exists at least one tournament that matches the given information).

Input

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

N
a_2
:
a_N

Output

Print the minimum possible depth of the tournament.

Sample Input 1

5
1
1
2
4

Sample Output 1

3

The following tournament and the result of the matches are consistent with the given information:

In the first match, contestants 4 and 5 played against each other, and contestant 4 won.

In the second match, contestants 2 and 4 played against each other, and contestant 2 won.

In the third match, contestants 1 and 3 played against each other, and contestant 1 won.

In the fourth match, contestants 1 and 2 played against each other, and contestant 1 won.

The depth of this tournament is 3, since contestant 5 must play three matches in order to win the championship.
There is no tournament with depth 2 or less that is consistent with the given information, thus the output should be 3.

Sample Input 2

7
1
2
1
3
1
4

Sample Output 2

3

Sample Input 3

4
4
4
1

Sample Output 3

3


var n,i,tot:longint;
	last,pre,other,depth,a,t:array[0..300007] of int64;
	ans:int64;
procedure insert(u,v:longint);
begin
  inc(tot);
  other[tot]:=v;
  pre[tot]:=last[u];
  last[u]:=tot;
end;
procedure dfs(u,father:longint);
var q,v:longint;
begin
  depth[u]:=depth[father]+1;
  if (depth[u]+t[u]>ans) then ans:=depth[u]+t[u];
  q:=last[u];
  while (q>0) do
  begin
    v:=other[q];
    if (v<>father) then dfs(v,u);
	q:=pre[q];
  end;
end;
begin
  readln(n);
  fillchar(a,sizeof(a),0);
  fillchar(t,sizeof(t),0);
  tot:=0;
  for i:=2 to n do
  begin
	readln(a[i]);
    t[a[i]]:=t[a[i]]+1;
    insert(i,a[i]);insert(a[i],i);
  end;
  ans:=0;
  dfs(1,0);
  if (ans>n) then ans:=n;
  writeln(ans-1);
end.


var n,i,tot,ans:longint;
    a,t,f:array[0..100007] of longint;
	last,pre,other,depth:array[0..300007] of longint;
procedure insert(u,v:longint);
begin
  inc(tot);
  other[tot]:=v;
  pre[tot]:=last[u];
  last[u]:=tot;
end;
procedure dfs(u,father:longint);
var q,v:longint;
begin
  depth[u]:=depth[father]+1;
  if (depth[u]+t[u]>ans) then ans:=depth[u]+t[u];
  q:=last[u];
  while (q>0) do
  begin
    v:=other[q];
    if (v<>father) then dfs(v,u);
	q:=pre[q];
  end;
end;
begin
  readln(n);
  fillchar(a,sizeof(a),0);
  fillchar(t,sizeof(t),0);
  tot:=0;
  for i:=2 to n do
  begin
	readln(a[i]);
    t[a[i]]:=t[a[i]]+1;
    insert(i,a[i]);insert(a[i],i);
  end;
  ans:=0;
  dfs(1,0);
  writeln(ans-1);
end.


uses math;var a,b,c,d:longint;begin read(a,b,c,d);writeln(max(a*b,c*d));end.

Score : 100 points

Problem Statement

There are two rectangles.
The lengths of the vertical sides of the first rectangle are A, and the lengths of the horizontal sides of the first rectangle are B.
The lengths of the vertical sides of the second rectangle are C, and the lengths of the horizontal sides of the second rectangle are D.

Print the area of the rectangle with the larger area.
If the two rectangles have equal areas, print that area.

Constraints

All input values are integers.

1≤A≤10^4

1≤B≤10^4

1≤C≤10^4

1≤D≤10^4

Input

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

A B C D

Output

Print the area of the rectangle with the larger area.
If the two rectangles have equal areas, print that area.

Sample Input 1

3 5 2 7

Sample Output 1

15

The first rectangle has an area of 3×5=15, and the second rectangle has an area of 2×7=14.
Thus, the output should be 15, the larger area.

Sample Input 2

100 600 200 300

Sample Output 2

60000


var
n,x,max,i:longint;
s:string;
begin
readln(n);
read(s);
x:=0;
max:=-maxlongint;
for i:=1 to n do
  if s[i]='I' then inc(x)
    else
      begin
        if x>max then max:=x;
        x:=x-1;
      end;
write(max);
end.

Score : 200 points

Problem Statement

You have an integer variable x.
Initially, x=0.

Some person gave you a string S of length N, and using the string you performed the following operation N times.
In the i-th operation, you incremented the value of x by 1 if S_i=I, and decremented the value of x by 1 if S_i=D.

Find the maximum value taken by x during the operations (including before the first operation, and after the last operation).

Constraints

1≤N≤100

|S|=N

No characters except I and D occur in S.

Input

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

N
S

Output

Print the maximum value taken by x during the operations.

Sample Input 1

5
IIDID

Sample Output 1

2

After each operation, the value of x becomes 1, 2, 1, 2 and 1, respectively. Thus, the output should be 2, the maximum value.

Sample Input 2

7
DDIDDII

Sample Output 2

0

The initial value x=0 is the maximum value taken by x, thus the output should be 0.


var
  ch:char;
  n,i,max,k:longint;
begin
  readln(n);
  max:=0;k:=0;
  for i:=1 to n do
   begin
     read(ch);
     if ch='I' then k:=k+1;
     if ch='D' then k:=k-1;
     if k>max then max:=k;
   end;
  writeln(k);
end.


program C;
const
  maxN = 1111;
  modulo = Round(1E9) + 7;
var
  n, x, i, j: Longint;
  fact: array[1..maxN] of Longint;
  ans: Int64;

begin
  ReadLn(n);
  for i := 1 to n do
    begin
      x := i;
      for j := 2 to n do
        if x mod j = 0 then
          while x mod j = 0 do
            begin
              Inc(fact[j]);
              x := x div j;
            end;
    end;
  ans := 1;
  for i := 2 to n do
    if fact[i] > 0 then
      ans := ans * (fact[i] + 1) mod modulo;
  WriteLn(ans);
end.

Score : 300 points

Problem Statement

You are given an integer N.
Find the number of the positive divisors of N!, modulo 10^9+7.

Constraints

1≤N≤10^3

Input

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

N

Output

Print the number of the positive divisors of N!, modulo 10^9+7.

Sample Input 1

3

Sample Output 1

4

There are four divisors of 3! =6: 1, 2, 3 and 6. Thus, the output should be 4.

Sample Input 2

6

Sample Output 2

30

Sample Input 3

1000

Sample Output 3

972926972


Var i,n,j,rem,fin:Longint;
    a:Array[1 .. 10000] of Longint;
Begin
    Readln(n);
    Fillchar(a,sizeof(a),0);
    fin:=1;
    For i:=1 to n do begin
        rem:=i;
        For j:=2 to rem do
            While rem mod j = 0 do begin
                Inc(a[j]);
                rem:=rem div j;
            End;
    End;
    For i:=1 to n do fin:=fin*(a[i]+1) mod 1000000007;
    Writeln(fin);
End.


var
	n,i:Longint;
	a,b,p,q,ans:int64;
begin
	read(n,a,b,p);
	for i:=2 to n do begin
		read(q);
		if b>a*(q-p)then ans:=ans+a*(q-p)else ans:=ans+b;
		p:=q;
	end;
	writeln(ans);
end.

Score : 500 points

Problem Statement

There are N towns on a line running east-west.
The towns are numbered 1 through N, in order from west to east.
Each point on the line has a one-dimensional coordinate, and a point that is farther east has a greater coordinate value.
The coordinate of town i is X_i.

You are now at town 1, and you want to visit all the other towns.
You have two ways to travel:

Walk on the line.
Your fatigue level increases by A each time you travel a distance of 1, regardless of direction.

Teleport to any location of your choice.
Your fatigue level increases by B, regardless of the distance covered.

Find the minimum possible total increase of your fatigue level when you visit all the towns in these two ways.

Constraints

All input values are integers.

2≤N≤10^5

1≤X_i≤10^9

For all i(1≤i≤N-1), X_i<X_{i+1}.

1≤A≤10^9

1≤B≤10^9

Input

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

N A B
X_1 X_2 ... X_N

Output

Print the minimum possible total increase of your fatigue level when you visit all the towns.

Sample Input 1

4 2 5
1 2 5 7

Sample Output 1

11

From town 1, walk a distance of 1 to town 2, then teleport to town 3, then walk a distance of 2 to town 4.
The total increase of your fatigue level in this case is 2×1+5+2×2=11, which is the minimum possible value.

Sample Input 2

7 1 100
40 43 45 105 108 115 124

Sample Output 2

84

From town 1, walk all the way to town 7.
The total increase of your fatigue level in this case is 84, which is the minimum possible value.

Sample Input 3

7 1 2
24 35 40 68 72 99 103

Sample Output 3

12

Visit all the towns in any order by teleporting six times.
The total increase of your fatigue level in this case is 12, which is the minimum possible value.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  s:string;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(s);
    s[6]:=' ';
    s[14]:=' ';
    Writeln(s);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

As a New Year's gift, Dolphin received a string s of length 19.

The string s has the following format: [five lowercase English letters],[seven lowercase English letters],[five lowercase English letters].

Dolphin wants to convert the comma-separated string s into a space-separated string.

Write a program to perform the conversion for him.

Constraints

The length of s is 19.

The sixth and fourteenth characters in s are ,.

The other characters in s are lowercase English letters.

Input

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

s

Output

Print the string after the conversion.

Sample Input 1

happy,newyear,enjoy

Sample Output 1

happy newyear enjoy

Replace all the commas in happy,newyear,enjoy with spaces to obtain happy newyear enjoy.

Sample Input 2

haiku,atcoder,tasks

Sample Output 2

haiku atcoder tasks

Sample Input 3

abcde,fghihgf,edcba

Sample Output 3

abcde fghihgf edcba


var sx,sy,tx,ty,i:Integer;
begin
	read(sx,sy,tx,ty);
	inc(sx);
	inc(sy);
	for i:=sy to ty do write('U');
	for i:=sx to tx do write('R');
	for i:=sy to ty do write('D');
	for i:=sx to tx do write('L');
	write('L');
	for i:=sy to ty do write('U');
	write('UR');
	for i:=sx to tx do write('R');
	write('DR');
	for i:=sy to ty do write('D');
	write('DL');
	for i:=sx to tx do write('L');
	write('U');
end.

Score : 300 points

Problem Statement

Dolphin resides in two-dimensional Cartesian plane, with the positive x-axis pointing right and the positive y-axis pointing up.

Currently, he is located at the point (sx,sy). In each second, he can move up, down, left or right by a distance of 1.

Here, both the x- and y-coordinates before and after each movement must be integers.

He will first visit the point (tx,ty) where sx < tx and sy < ty, then go back to the point (sx,sy), then visit the point (tx,ty) again, and lastly go back to the point (sx,sy).

Here, during the whole travel, he is not allowed to pass through the same point more than once, except the points (sx,sy) and (tx,ty).

Under this condition, find a shortest path for him.

Constraints

-1000 ≤ sx < tx ≤ 1000

-1000 ≤ sy < ty ≤ 1000

sx,sy,tx and ty are integers.

Input

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

sx sy tx ty

Output

Print a string S that represents a shortest path for Dolphin.

The i-th character in S should correspond to his i-th movement.

The directions of the movements should be indicated by the following characters:

U: Up

D: Down

L: Left

R: Right

If there exist multiple shortest paths under the condition, print any of them.

Sample Input 1

0 0 1 2

Sample Output 1

UURDDLLUUURRDRDDDLLU

One possible shortest path is:

Going from (sx,sy) to (tx,ty) for the first time: (0,0) → (0,1) → (0,2) → (1,2)

Going from (tx,ty) to (sx,sy) for the first time: (1,2) → (1,1) → (1,0) → (0,0)

Going from (sx,sy) to (tx,ty) for the second time: (0,0) → (-1,0) → (-1,1) → (-1,2) → (-1,3) → (0,3) → (1,3) → (1,2)

Going from (tx,ty) to (sx,sy) for the second time: (1,2) → (2,2) → (2,1) → (2,0) → (2,-1) → (1,-1) → (0,-1) → (0,0)

Sample Input 2

-2 -2 1 1

Sample Output 2

UURRURRDDDLLDLLULUUURRURRDDDLLDL


var sx,sy,tx,ty,i:Integer;
begin
	read(sx,sy,tx,ty);
	for i:=sy+1 to ty do write('U');
	for i:=sx+1 to tx do write('R');
	for i:=sy+1 to ty do write('D');
	for i:=sx+1 to tx do write('L');
	write('L');
	for i:=sy+1 to ty do write('U');
	write('UR');
	for i:=sx+1 to tx do write('R');
	write('DR');
	for i:=sy+1 to ty do write('D');
	write('DL');
	for i:=sx+1 to tx do write('L');
	write('U');
end.


var sx,sy,tx,ty,i:Integer;
begin
	read(sx,sy,tx,ty);
	tx:=tx-sx;
	ty:=ty-sy;
	for i:=1 to ty do write('U');
	for i:=1 to tx do write('R');
	for i:=1 to ty do write('D');
	for i:=1 to tx do write('L');
	write('L');
	for i:=1 to ty do write('U');
	write('UR');
	for i:=1 to tx do write('R');
	write('DR');
	for i:=1 to ty do write('D');
	write('DL');
	for i:=1 to tx do write('L');
	write('U');
end.


var
	N,K,i:Longint;
	s,a,b:Array[0..100000]of Longint;
	ans,sum:int64;
function my(d:Longint):Longint;
begin
	if d>0 then my:=d else my:=0;
end;
begin
	read(N,K);
	for i:=1 to N do read(s[i]);
	a[1]:=s[1];
	b[1]:=my(s[1]);
	for i:=2 to N do begin
		a[i]:=s[i]+a[i-1];
		b[i]:=my(s[i])+b[i-1];
	end;
	ans:=0;
	for i:=0 to N-K do begin
		sum:=my(a[i+K]-a[i]);
		inc(sum,b[i]-b[0]+b[N]-b[i+K]);
		if ans<sum then ans:=sum;
	end;
	writeln(ans);
end.

Score : 400 points

Problem Statement

There are N squares aligned in a row.
The i-th square from the left contains an integer a_i.

Initially, all the squares are white.
Snuke will perform the following operation some number of times:

Select K consecutive squares. Then, paint all of them white, or paint all of them black. Here, the colors of the squares are overwritten.

After Snuke finishes performing the operation, the score will be calculated as the sum of the integers contained in the black squares.
Find the maximum possible score.

Constraints

1≤N≤10^5

1≤K≤N

a_i is an integer.

|a_i|≤10^9

Input

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

N K
a_1 a_2 ... a_N

Output

Print the maximum possible score.

Sample Input 1

5 3
-10 10 -10 10 -10

Sample Output 1

10

Paint the following squares black: the second, third and fourth squares from the left.

Sample Input 2

4 2
10 -10 -10 10

Sample Output 2

20

One possible way to obtain the maximum score is as follows:

Paint the following squares black: the first and second squares from the left.

Paint the following squares black: the third and fourth squares from the left.

Paint the following squares white: the second and third squares from the left.

Sample Input 3

1 1
-10

Sample Output 3

0

Sample Input 4

10 5
5 -4 -5 -8 -4 7 2 -4 0 7

Sample Output 4

17


program taskd;
var n1, n2, sum, min : longint;
    m : char;
begin
    sum := 0;
    min := 0;
    readln(n1, m, n2);
    if m = '+' then
    begin
        sum := n1 + n2;
        writeln(sum)
    end;
    if m = '-' then
    begin
        min := n1 - n2;
          writeln(min);
    end;
end.

Score : 100 points

Problem Statement

Joisino wants to evaluate the formula "A op B".
Here, A and B are integers, and the binary operator op is either + or -.
Your task is to evaluate the formula instead of her.

Constraints

1≦A,B≦10^9

op is either + or -.

Input

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

A op B

Output

Evaluate the formula and print the result.

Sample Input 1

1 + 2

Sample Output 1

3

Since 1 + 2 = 3, the output should be 3.

Sample Input 2

5 - 7

Sample Output 2

-2


program  ec12;
var
	n,m,i,j:longint;
	ans:int64;
	a:array[1..100001] of longint;
	vis:array[0..100001] of longint;
begin
	fillchar(vis,sizeof(vis),0);
	readln(n);
	for i:=1 to n do
	begin
		read(a[i]);
		if a[i]>=n then
		begin
			writeln(0);
			halt;
		end;
		inc(vis[a[i]]);
		if (a[i]=0) and (vis[0]>=2) then
		begin
			writeln(0);
			halt;
		end;
	end;
	if vis[0]=0 then
	begin
		writeln(0);
		halt;
	end;
	ans:=1;
	for i:=1 to n do
	begin
		if vis[i]=2 then
		ans:=(ans*2) mod 1000000007
		else
		begin
			if (vis[i]=1) and (i>0) then
			begin
				writeln(0);
				halt;
			end;
		end;
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

There are N people, conveniently numbered 1 through N.
They were standing in a row yesterday, but now they are unsure of the order in which they were standing.
However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person.
According to their reports, the difference above for person i is A_i.

Based on these reports, find the number of the possible orders in which they were standing.
Since it can be extremely large, print the answer modulo 10^9+7.
Note that the reports may be incorrect and thus there may be no consistent order.
In such a case, print 0.

Constraints

1≦N≦10^5

0≦A_i≦N-1

Input

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

N
A_1 A_2 ... A_N

Output

Print the number of the possible orders in which they were standing, modulo 10^9+7.

Sample Input 1

5
2 4 4 0 2

Sample Output 1

4

There are four possible orders, as follows:

2,1,4,5,3

2,5,4,1,3

3,1,4,5,2

3,5,4,1,2

Sample Input 2

7
6 4 0 2 4 0 2

Sample Output 2

0

Any order would be inconsistent with the reports, thus the answer is 0.

Sample Input 3

8
7 5 1 1 7 3 5 3

Sample Output 3

16


program C;
    var N,i,A:longint;
    var bo,ca:boolean;
    var Route:array[0..120000]of integer;
    var ans:longint;
begin
    fillchar(Route,sizeof(Route),0);
    readln(N);
    for i:=1 to N do
    begin
        read(A);
        if i=1 then if A mod 2=0 then bo:=true else bo:=false;
        Route[A div 2]:=Route[A div 2]+1;
    end;
    if bo then if Route[0]=1 then ca:=true else ca:=false else if Route[0]=2 then ca:=true else ca:=false;
    if ca then for i:=1 to ((N+1)div 2)-1 do if Route[i]<>2 then ca:=false;
    if ca then
    begin
        ans:=1;
        for i:=1 to N div 2 do ans:=(ans*2) mod 1000000007;
        writeln(ans)
    end
    else writeln(0)
end.


var a,b:array[1..201,1..201] of char;
    n,m,i,j:byte;
begin
readln(n,m);
for i:=1 to n do begin
  for j:=1 to m do read(a[i,j]); end;
for i:=1 to n*2 do begin
  for j:=1 to m do write(a[i,j]); writeln;
  for j:=1 to m do write(a[i,j]); writeln;  end;
end.

Score : 100 points

Problem Statement

Given a lowercase English letter c, determine whether it is a vowel. Here, there are five vowels in the English alphabet: a, e, i, o and u.

Constraints

c is a lowercase English letter.

Input

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

c

Output

If c is a vowel, print vowel. Otherwise, print consonant.

Sample Input 1

a

Sample Output 1

vowel

Since a is a vowel, print vowel.

Sample Input 2

z

Sample Output 2

consonant

Sample Input 3

s

Sample Output 3

consonant


var i:Longint;s:String[100];begin readln();for i:=1 to 100 do begin readln(s);writeln(s);writeln(s)end;end.


var a,i:Longint;s:String[100];begin readln(a);for i:=1 to a do begin readln(s);writeln(s);writeln(s)end;end.


var a,b,i:Longint;s:String;begin readln(a,b);for i:=1 to a do begin readln(s);writeln(s);writeln(s)end;end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  w,h,i:Integer;
  s:array of string;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(h);
    Readln(w);
    SetLength(s,h);
    for i := 0 to h - 1 do
      Readln(s[i]);

    for i := 0 to h - 1 do begin
      Writeln(s[i]);
      Writeln(s[i]);
    end;
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.


program D;
    var N,K,L:longint;
    var i,j,p,q:longint;
    var a:array[1..25000]of longint;
    var b:array[1..25000]of longint;
    var map1:array[1..25000,1..25000]of boolean;
    var map2:array[1..25000,1..25000]of boolean;
    var used:array[1..25000]of boolean;
    var ansr:array[1..25000]of longint;
    procedure DFS1(_,__:longint);
        var ii:longint;
    begin
        used[_]:=true;
        a[_]:=__;
        for ii:=1 to N do
            if (map1[_,ii]=true)AND(used[ii]=false) then DFS1(ii,__);
    end;
    procedure DFS2(_,__:longint);
        var ii:longint;
    begin
        used[_]:=true;
        b[_]:=__;
        for ii:=1 to N do
            if (a[_]=a[ii])AND(map2[_,ii]=true)AND(used[ii]=false) then DFS2(ii,__);
    end;
begin
    readln(N,K,L);
    for i:=1 to N do
    begin
        map1[i,i]:=true;
        map2[i,i]:=true;
        used[i]:=false;
        ansr[i]:=0;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        map1[p,q]:=true;
        map1[q,p]:=true;
    end;
    for i:=1 to N do if used[i]=false then DFS1(i,i);
    for i:=1 to L do
    begin
        readln(p,q);
        map2[p,q]:=true;
        map2[q,p]:=true;
    end;
    for i:=1 to N do used[i]:=false;
    for i:=1 to N do if used[i]=false then DFS2(i,i);
    for i:=1 to N do ansr[b[i]]:=ansr[b[i]]+1;
    for i:=1 to N do write(ansr[b[i]],' ');
end.

Score : 400 points

Problem Statement

There are N cities. There are also K roads and L railways, extending between the cities.
The i-th road bidirectionally connects the p_i-th and q_i-th cities, and the i-th railway bidirectionally connects the r_i-th and s_i-th cities.
No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.

We will say city A and B are connected by roads if city B is reachable from city A by traversing some number of roads. Here, any city is considered to be connected to itself by roads.
We will also define connectivity by railways similarly.

For each city, find the number of the cities connected to that city by both roads and railways.

Constraints

2 ≦ N ≦ 2*10^5

1 ≦ K, L≦ 10^5

1 ≦ p_i, q_i, r_i, s_i ≦ N

p_i < q_i

r_i < s_i

When i ≠ j, (p_i, q_i) ≠ (p_j, q_j)

When i ≠ j, (r_i, s_i) ≠ (r_j, s_j)

Input

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

N K L
p_1 q_1
:
p_K q_K
r_1 s_1
:
r_L s_L

Output

Print N integers. The i-th of them should represent the number of the cities connected to the i-th city by both roads and railways.

Sample Input 1

4 3 1
1 2
2 3
3 4
2 3

Sample Output 1

1 2 2 1

All the four cities are connected to each other by roads.

By railways, only the second and third cities are connected. Thus, the answers for the cities are 1, 2, 2 and 1, respectively.

Sample Input 2

4 2 2
1 2
2 3
1 4
2 3

Sample Output 2

1 2 2 1

Sample Input 3

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

Sample Output 3

1 1 2 1 2 2 2


program D;
    var N,K,L:longint;
    var root:array[1..200005]of longint;
    var size:array[1..200005]of longint;
    var colr:array[1..200005]of longint;
    var i,j,p,q:longint;
    function FindRoot(_:longint):longint;
    begin
        if root[_]=_ then exit(_) else exit(FindRoot(root[_]));
    end;
    procedure Merge(_x,_y:longint);
        var x,y:longint;
    begin
        x:=FindRoot(_x);
        y:=FindRoot(_y);
        if x<>y then
            if size[x]<size[y] then
            begin
                root[x]:=y;
                size[y]:=size[y]+size[x];
            end
            else
            begin
                root[y]:=x;
                size[x]:=size[y]+size[x];
            end;
    end;
begin
    readln(N,K,L);
    for i:=1 to N do
    begin
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        Merge(p,q);
    end;
    for i:=1 to N do
    begin
        colr[i]:=FindRoot(i);
    end;
    for i:=1 to N do
    begin
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to L do
    begin
        readln(p,q);
        if colr[p]=colr[q] then Merge(p,q);
    end;
    for i:=1 to N-1 do write(size[FindRoot(i)],' ');
    writeln(size[FindRoot(N)])
end.


program D;
    var N,K,L:longint;
    var root:array[1..100005]of longint;
    var size:array[1..100005]of longint;
    var colr:array[1..100005]of longint;
    var i,j,p,q:longint;
    function FindRoot(_:longint):longint;
    begin
        if root[_]=_ then exit(_) else exit(FindRoot(root[_]));
    end;
    procedure Merge(_x,_y:longint);
        var x,y:longint;
    begin
        x:=FindRoot(_x);
        y:=FindRoot(_y);
        if x<>y then
        if size[x]<size[y] then
        begin
            root[x]:=y;
            size[y]:=size[y]+size[x];
        end
        else
        begin
            root[y]:=x;
            size[x]:=size[y]+size[x];
        end;
    end;
begin
    readln(N,K,L);
    for i:=1 to N do
    begin
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        Merge(p,q);
    end;
    for i:=1 to N do
    begin
        colr[i]:=FindRoot(i);
    end;
    for i:=1 to N do
    begin
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to L do
    begin
        readln(p,q);
        if colr[p]=colr[q] then Merge(p,q);
    end;
    for i:=1 to N-1 do write(size[FindRoot(i)],' ');
    writeln(size[FindRoot(N)])
end.


program D;
    var N,K,L:longint;
    var root:array[1..100005]of longint;
    var size:array[1..100005]of longint;
    var colr:array[1..100005]of longint;
    var i,j,p,q:longint;
    function FindRoot(_:longint):longint;
    begin
        if root[_]=_ then exit(_) else exit(FindRoot(root[_]));
    end;
    procedure Merge(_x,_y:longint);
        var x,y:longint;
    begin
        x:=FindRoot(_x);
        y:=FindRoot(_y);
        if x=y then exit;
        if size[x]<size[y] then
        begin
            root[x]:=y;
            size[y]:=size[y]+size[x];
        end
        else
        begin
            root[y]:=x;
            size[x]:=size[y]+size[x];
        end;
    end;
begin
    readln(N,K,L);
    for i:=1 to N do
    begin
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        Merge(p,q);
    end;
    for i:=1 to N do
    begin
        colr[i]:=FindRoot(i);
    end;
    for i:=1 to N do
    begin
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to L do
    begin
        readln(p,q);
        if colr[p]=colr[q] then Merge(p,q);
    end;
    for i:=1 to N-1 do write(size[FindRoot(i)],' ');
    writeln(size[FindRoot(N)])
end.


program D;
    var N,K,L:longint;
    var used:array[1..100005]of boolean;
    var root:array[1..100005]of longint;
    var size:array[1..100005]of longint;
    var colr:array[1..100005]of longint;
    var i,j,p,q:longint;
    function FindRoot(_:longint):longint;
    begin
        if root[_]=_ then exit(_) else exit(FindRoot(root[_]));
    end;
    procedure Merge(_x,_y:longint);
        var x,y:longint;
    begin
        x:=FindRoot(_x);
        y:=FindRoot(_y);
        if x=y then exit;
        if size[x]<size[y] then
        begin
            root[x]:=y;
            size[y]:=size[y]+size[x];
        end
        else
        begin
            root[y]:=x;
            size[x]:=size[y]+size[x];
        end;
    end;
begin
    readln(N,K,L);
    for i:=1 to N do
    begin
        used[i]:=false;
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to K do
    begin
        readln(p,q);
        Merge(p,q);
    end;
    for i:=1 to N do
    begin
        colr[i]:=FindRoot(i);
        used[i]:=false;
        root[i]:=i;
        size[i]:=1;
    end;
    for i:=1 to L do
    begin
        readln(p,q);
        if colr[p]=colr[q] then Merge(p,q);
    end;
    for i:=1 to N-1 do write(size[FindRoot(i)],' ');
    writeln(size[FindRoot(i)])
end.


var
a,b,x,i,k,s:longint;
begin
read(a,b,x);
for i:=a+1 to b-1 do
 begin
  k:=i;
  if k mod x=0 then inc(s);
 end;
 writeln(s);
 end.

Score : 200 points

Problem Statement

You are given nonnegative integers a and b (a ≤ b), and a positive integer x.
Among the integers between a and b, inclusive, how many are divisible by x?

Constraints

0 ≤ a ≤ b ≤ 10^{18}

1 ≤ x ≤ 10^{18}

Input

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

a b x

Output

Print the number of the integers between a and b, inclusive, that are divisible by x.

Sample Input 1

4 8 2

Sample Output 1

3

There are three integers between 4 and 8, inclusive, that are divisible by 2: 4, 6 and 8.

Sample Input 2

0 5 1

Sample Output 2

6

There are six integers between 0 and 5, inclusive, that are divisible by 1: 0, 1, 2, 3, 4 and 5.

Sample Input 3

9 9 2

Sample Output 3

0

There are no integer between 9 and 9, inclusive, that is divisible by 2.

Sample Input 4

1 1000000000000000000 3

Sample Output 4

333333333333333333

Watch out for integer overflows.


var a,b,x:int64;
begin
  read(a,b);
if a=0 then inc(a);if b=0 then inc(b);
writeln(b div x-(a-1) div x);end.


var n,x,y,z,i,j,k,ans:longint;
a:array[1..1000000]of longint;
begin
    readln(n,x);
    for i:=1 to n do
    begin
        read(a[i]);
    end;
    for i:=1 to n-1 do
    begin
  //  writeln(i);
        if a[i]+a[i+1]>x then
        begin
            if (i+2<=n)then
            begin
                if a[i+2]+a[i+1]>x then
                begin
                    y:=a[i+1]-(x-a[i]);
                    z:=a[i+1]-(x-a[i+2]);
                    if y>z then
                    begin
                       ans:=ans+y;
                       if y>a[i+1] then
                       begin
                           a[i+2]:=a[i+2]-(y-a[i+1]);
                           a[i+1]:=0;
                       end
                       else a[i+1]:=a[i+1]-y;
                    end
                    else
                    begin
                        ans:=ans+z;
                      //  writeln('2');
                        if z>a[i+1] then
                        begin
                           a[i+2]:=a[i+2]-(z-a[i+1]);
                           a[i+1]:=0;
                        end
                        else a[i+1]:=a[i+1]-z;
                    end;
                end;
            end
            else
            begin
                y:=a[i+1]+a[i]-x;
                if a[i+1]-y<=0 then
                begin
                    a[i]:=a[i]-(y-a[i+1]);
                    a[i+1]:=0;
                end
                else
                  a[i+1]:=a[i+1]-y;
                ans:=ans+y;
            end;
        end;
    end;
  //  for i:=1 to n do
  //    write(a[i],' ');
    writeln(ans);
end.

Score : 300 points

Problem Statement

There are N boxes arranged in a row.
Initially, the i-th box from the left contains a_i candies.

Snuke can perform the following operation any number of times:

Choose a box containing at least one candy, and eat one of the candies in the chosen box.

His objective is as follows:

Any two neighboring boxes contain at most x candies in total.

Find the minimum number of operations required to achieve the objective.

Constraints

2 ≤ N ≤ 10^5

0 ≤ a_i ≤ 10^9

0 ≤ x ≤ 10^9

Input

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

N x
a_1 a_2 ... a_N

Output

Print the minimum number of operations required to achieve the objective.

Sample Input 1

3 3
2 2 2

Sample Output 1

1

Eat one candy in the second box.
Then, the number of candies in each box becomes (2, 1, 2).

Sample Input 2

6 1
1 6 1 2 0 4

Sample Output 2

11

For example, eat six candies in the second box, two in the fourth box, and three in the sixth box.
Then, the number of candies in each box becomes (1, 0, 1, 0, 0, 1).

Sample Input 3

5 9
3 1 4 1 5

Sample Output 3

0

The objective is already achieved without performing operations.

Sample Input 4

2 0
5 5

Sample Output 4

10

All the candies need to be eaten.


var a:array [1..100000] of longint;
    b,l,i,s:longint;
begin
        s:=0;
        read(b,l);
        for i:=1 to b do
                read(a[i]);
        for i:=1 to b-1 do
                while (a[i]+a[i+1]>l) do
                begin
                        a[i+1]:=a[i+1]-1;
                        s:=s+1;
                end;
        writeln(s);
end.


program taskJ;
var n,x,i,j,sum,sum2:longint;
    o:array[1..100000]of longint;
begin
readln(n,x);
read(o[1]);
for i:=2 to n do
begin
  read(o[i]);
  if o[i]+o[i-1]>x then
    begin
    sum:=sum+o[i]+o[i-1]-x;
    o[i]:=o[i]-(o[i]+o[i-1]-x);
    end;
end;
for j:=n downto 2 do
begin
  if o[j]+o[j-1]>x then
  begin
    sum:=sum+o[j]+o[j-1]-x;
    o[j-1]:=o[j-1]-(o[j]+o[j-1]-x);
  end;
end;
if sum<sum2 then
writeln(sum2)
else
writeln(sum);
end.


program taskJ;
var n,x,i,j,sum:longint;
o:array[1..100000]of longint;
begin
readln(n,x);
read(o[1]);
for i:=2 to n do
begin
read(o[i]);
if o[i]+o[i-1]>x then
begin
sum:=sum+o[i]+o[i-1]-x;
o[i]:=o[i]-(o[i]+o[i-1]-x);
end;
end;
writeln(sum);
end.


var
  n:string;
begin
  readln(n);
  if n[1]=n[length(n)] then
    begin
      if odd(length(n)) then writeln('Second')
        else writeln('First');
    end
    else
      begin
        if odd(length(n)) then writeln('First')
         else writeln('Second');
      end;
end.

Score : 500 points

Problem Statement

There is a string s of length 3 or greater.
No two neighboring characters in s are equal.

Takahashi and Aoki will play a game against each other.
The two players alternately performs the following operation, Takahashi going first:

Remove one of the characters in s, excluding both ends. However, a character cannot be removed if removal of the character would result in two neighboring equal characters in s.

The player who becomes unable to perform the operation, loses the game. Determine which player will win when the two play optimally.

Constraints

3 ≤ |s| ≤ 10^5

s consists of lowercase English letters.

No two neighboring characters in s are equal.

Input

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

s

Output

If Takahashi will win, print First. If Aoki will win, print Second.

Sample Input 1

aba

Sample Output 1

Second

Takahashi, who goes first, cannot perform the operation, since removal of the b, which is the only character not at either ends of s, would result in s becoming aa, with two as neighboring.

Sample Input 2

abc

Sample Output 2

First

When Takahashi removes b from s, it becomes ac.
Then, Aoki cannot perform the operation, since there is no character in s, excluding both ends.

Sample Input 3

abcab

Sample Output 3

First


var
  s:string;
  l:longint;
begin
  readln(s);
  l:=length(s);
  if s[1]=s[l] then
   begin
     if l mod 2=0 then writeln('First')
                  else writeln('Second');
   end
  else
   begin
     if l mod 2=1 then writeln('First')
                  else writeln('Second');
   end;
end.


program C;
    var N,x,i:longint;
    var a:array [1..120000] of longint;
    var u,v:longint;
    var ans:int64;
begin
    readln(N,x);ans:=0;
    for i:=1 to N do read(a[i]);
    for i:=2 to N do
    begin
        u:=a[i-1];v:=a[i];
        if u+v>x then
        begin
            if v>=u+v-x then a[i]:=x-u
            else
            begin
                a[i]:=0;
                a[i-1]:=x;
            end;
            ans:=ans+u+v-x;
        end;
    end;
    writeln(ans);
end.


var s:string;n:Longint;
begin
readln(s);
n:=length(s);
if(n mod 2=1)xor(s[1]=s[n])then writeln('First')else writeln('Second');
end.


program B;
    var Q:string;
    var ans:integer;
begin
    readln(Q);
    ans:=0;
    if Q[1]=Q[length(Q)] then ans:=ans+1;
    if length(Q) mod 2=0 then ans:=ans+1;
    if ans mod 2=0 then writeln('Second') else writeln('First');
end.


var
        s1,x:array[1..100000]of char;
        s:string;
        i,j:longint;
        ch:char;
begin
        readln(s);
        for i:=length(s) downto 1 do
        begin
                inc(j);
                ch:=s[i];
                x[j]:=ch;
        end;
        s1:=x;
        for i:=1 to length(s) do
        begin
                if s1[i]='b' then s1[i]:='d'
                else if s1[i]='d' then s1[i]:='b'
                else if s1[i]='p' then s1[i]:='q'
                else if s1[i]='q' then s1[i]:='p';
        end;
        for i:=1 to length(s) do
                if s[i]<>s1[i] then
                begin
                        writeln('No');
                        halt;
                end;
        writeln('Yes')
end.

Score : 100 points

Problem Statement

You are given a string S consisting of letters b, d, p and q.
Determine whether S is a mirror string.

Here, a mirror string is a string S such that the following sequence of operations on S results in the same string S:

Reverse the order of the characters in S.

Replace each occurrence of b by d, d by b, p by q, and q by p, simultaneously.

Constraints

1 \leq |S| \leq 10^5

S consists of letters b, d, p, and q.

Input

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

S

Output

If S is a mirror string, print Yes. Otherwise, print No.

Sample Input 1

pdbq

Sample Output 1

Yes

Sample Input 2

ppqb

Sample Output 2

No


{AT2201}
var
s1,s2:ansistring;
i:longint;
t:char;
begin
readln(s1);
s2:=s1;
for i:=1 to length(s2) div 2 do
begin
t:=s2[i];
s2[i]:=s2[length(s2)-i+1];
s2[length(s2)-i+1]:=t;
end;
for i:=1 to length(s2) do
 if (s2[i]='b') and (s1[i]<>'d') or (s2[i]='d') and (s1[i]<>'b') or (s2[i]='p') and (s1[i]<>'q') or (s2[i]='q') and (s1[i]<>'p') then
 begin
 writeln('No');
 halt;
 end;
writeln('Yes');
end.


program hrn;
 var
  a:array[0..40]of qword;
  n,i:longint;
 begin
  read(n);a[0]:=2;
  for i:=1 to n do
   a[i]:=a[i-1]*2+2;
  writeln(a[n]);
 end.

Score : 100 points

Problem Statement

We have a cord whose length is a positive integer. We will perform the following condition until the length of the cord becomes at most 2:

Operation:
Cut the rope at two positions to obtain three cords, each with a length of a positive integer.
Among these, discard one with the longest length and one with the shortest length, and keep the remaining one.

Let f(N) be the maximum possible number of times to perform this operation, starting with a cord with the length N.

You are given a positive integer X. Find the maximum integer N such that f(N)=X.

Constraints

1 \leq X \leq 40

Input

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

X

Output

Print the value of the maximum integer N such that f(N)=X.

Sample Input 1

2

Sample Output 1

14


var
	N,i,j:Longint;
	A:Array[1..100,1..99]of Longint;
begin
	read(N);
	if N=2 then begin
		writeln(-1);
		exit;
	end;
	if N mod 2=1 then begin
		for i:=1 to N-1 do A[N,i]:=i;
		i:=1;
		while i<=N-1 do begin
			for j:=1 to i-1 do begin
				A[i,j]:=j;
				A[i+1,j]:=j;
			end;
			A[i,i]:=i+1;
			A[i+1,i+1]:=i;
			A[i,i+1]:=i+2;
			A[i+1,i]:=i+2;
			for j:=i+2 to N-1 do begin
				A[i,j]:=j+1;
				A[i+1,j]:=j+1;
			end;
			inc(i,2);
		end;
	end else begin
		i:=1;
		while i<=N-2 do begin
			for j:=1 to i-1 do begin
				A[i,j]:=j;
				A[i+1,j]:=j;
			end;
			A[i,i]:=i+1;
			A[i+1,i+1]:=i;
			A[i,i+1]:=i+2;
			A[i+1,i]:=i+2;
			for j:=i+2 to N-1 do begin
				A[i,j]:=j+1;
				A[i+1,j]:=j+1;
			end;
			inc(i,2);
		end;
		for j:=1 to N-3 do begin
			A[N-1,j]:=j;
			A[N,j]:=j;
		end;
		A[N-1,N-2]:=N;
		A[N,N-1]:=N-1;
		A[N-1,N-1]:=N-2;
		A[N,N-2]:=N-2;
	end;
	for i:=1 to N do begin
		for j:=1 to N-1 do begin
			write(A[i,j]);
			write(' ');
		end;
		writeln;
	end;
end.

Score : 100 points

Problem Statement

There are N persons, conveniently numbered 1 through N. They will take 3y3s Challenge for N-1 seconds.

During the challenge, each person must look at each of the N-1 other persons for 1 seconds, in some order.

If any two persons look at each other during the challenge, the challenge ends in failure.

Find the order in which each person looks at each of the N-1 other persons, to be successful in the challenge.

Constraints

2 \leq N \leq 100

Input

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

N

Output

If there exists no way to be successful in the challenge, print -1.

If there exists a way to be successful in the challenge, print any such way in the following format:

A_{1,1} A_{1,2} ... A_{1, N-1}
A_{2,1} A_{2,2} ... A_{2, N-1}
:
A_{N,1} A_{N,2} ... A_{N, N-1}

where A_{i, j} is the index of the person that the person numbered i looks at during the j-th second.

Judging

The output is considered correct only if all of the following conditions are satisfied:

1 \leq A_{i,j} \leq N

For each i, A_{i,1}, A_{i,2}, ... , A_{i, N-1} are pairwise distinct.

Let X = A_{i, j}, then A_{X, j} \neq i always holds.

Sample Input 1

7

Sample Output 1

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

Sample Input 2

2

Sample Output 2

-1


var
  i,j,h,w:longint;
  s:string[5];
  c:char;
begin
  read(h);
  readln(w);
  for i:=1 to h do
  begin
    for j:=1 to w do
    begin
      read(s);
      if s='snuke' then
      begin
        write(chr(64+j));
        writeln(i);
      end;
      if j < w then read(c) else readln(c);
    end;
  end;
end.

Score : 100 points

Problem Statement

There is a grid with H rows and W columns.

The square at the i-th row and j-th column contains a string S_{i,j} of length 5.

The rows are labeled with the numbers from 1 through H, and the columns are labeled with the uppercase English letters from A through the W-th letter of the alphabet.

Exactly one of the squares in the grid contains the string snuke. Find this square and report its location.

For example, the square at the 6-th row and 8-th column should be reported as H6.

Constraints

1≦H, W≦26

The length of S_{i,j} is 5.

S_{i,j} consists of lowercase English letters (a-z).

Exactly one of the given strings is equal to snuke.

Input

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

H W
S_{1,1} S_{1,2} ... S_{1,W}
S_{2,1} S_{2,2} ... S_{2,W}
:
S_{H,1} S_{H,2} ... S_{H,W}

Output

Print the labels of the row and the column of the square containing the string snuke, with no space inbetween.

Sample Input 1

15 10
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snuke snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake

Sample Output 1

H6

Sample Input 2

1 1
snuke

Sample Output 2

A1


var
n,i,t:longint;
begin
readln(n);
i:=0;
while n>0 do
  begin
    inc(i);
    if odd(n) then t:=n+1
              else t:=n;
    if (i<t div 2)or(i=n) then begin writeln(i);n:=n-i;end;
  end;
end.

Score : 300 points

Problem Statement

The problem set at CODE FESTIVAL 20XX Finals consists of N problems.

The score allocated to the i-th (1≦i≦N) problem is i points.

Takahashi, a contestant, is trying to score exactly N points. For that, he is deciding which problems to solve.

As problems with higher scores are harder, he wants to minimize the highest score of a problem among the ones solved by him.

Determine the set of problems that should be solved.

Constraints

1≦N≦10^7

Partial Score

200 points will be awarded for passing the test set satisfying 1≦N≦1000.

Additional 100 points will be awarded for passing the test set without additional constraints.

Input

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

N

Output

Among the sets of problems with the total score of N, find a set in which the highest score of a problem is minimum, then print the indices of the problems in the set in any order, one per line.

If there exists more than one such set, any of them will be accepted.

Sample Input 1

4

Sample Output 1

1
3

Solving only the 4-th problem will also result in the total score of 4 points, but solving the 1-st and 3-rd problems will lower the highest score of a solved problem.

Sample Input 2

7

Sample Output 2

1
2
4

The set \{3,4\} will also be accepted.

Sample Input 3

1

Sample Output 3

1


var i,k,n:longint;
begin
read(n);
k:=round(sqrt(2*n));
while k*(k+1)<n*2 do inc(k);
dec(n,k*(k+1)div 2);
for i:=1 to -n-1 do writeln(i);
for i:=1-n to k do writeln(i);
end.


var i,k,n:longint;
begin
read(n);
k:=round(sqrt(2*n));
while k*(k+1)<n*2 do inc(k);
dec(n,k*(k+1)div 2);
for i:=1 to k do if i<>-n then writeln(i);
end.


uses math;
var
	N,A,ans,p,now,tmp:int64;
	j,k:Longint;
begin
	read(N,A);
	ans:=N;
	for k:=1 to 39 do begin
		p:=floor(power(N,1/(k+1)));
		now:=1;
		for j:=1 to k do now:=now*p;
		for j:=0 to k do begin
			tmp:=A*k+p*k+j+(N+now-1)div now;
			if ans>tmp then ans:=tmp;
			now:=now div p*(p+1);
			if now>N then break;
		end;
	end;
	writeln(ans);
end.

Score : 1000 points

Problem Statement

Rng is baking cookies.

Initially, he can bake one cookie per second.

He can also eat the cookies baked by himself.
When there are x cookies not yet eaten, he can choose to eat all those cookies. After he finishes eating those cookies, the number of cookies he can bake per second becomes x. Note that a cookie always needs to be baked for 1 second, that is, he cannot bake a cookie in 1/x seconds when x > 1.
When he choose to eat the cookies, he must eat all of them; he cannot choose to eat only part of them.
It takes him A seconds to eat the cookies regardless of how many, during which no cookies can be baked.

He wants to give N cookies to Grandma.
Find the shortest time needed to produce at least N cookies not yet eaten.

Constraints

1≦N≦10^{12}

0≦A≦10^{12}

A is an integer.

Partial Score

500 points will be awarded for passing the test set satisfying N≦10^6 and A≦10^6.

Additional 500 points will be awarded for passing the test set without additional constraints.

Input

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

N A

Output

Print the shortest time needed to produce at least N cookies not yet eaten.

Sample Input 1

8 1

Sample Output 1

7

It is possible to produce 8 cookies in 7 seconds, as follows:

After 1 second: 1 cookie is done.

After 2 seconds: 1 more cookie is done, totaling 2. Now, Rng starts eating those 2 cookies.

After 3 seconds: He finishes eating the cookies, and he can now bake 2 cookies per second.

After 4 seconds: 2 cookies are done.

After 5 seconds: 2 more cookies are done, totaling 4.

After 6 seconds: 2 more cookies are done, totaling 6.

After 7 seconds: 2 more cookies are done, totaling 8.

Sample Input 2

1000000000000 1000000000000

Sample Output 2

1000000000000


uses math;
var
	N,A,ans,p,now,tmp:int64;
	j,k:Longint;
begin
	read(N,A);
	ans:=N;
	for k:=1 to 39 do begin
		p:=floor(power(N/1,1/(k+1)));
		now:=1;
		for j:=1 to k do now:=now*p;
		for j:=0 to k do begin
			tmp:=A*k+p*k+j+(N+now-1)div now;
			if ans>tmp then ans:=tmp;
			now:=now div p*(p+1);
		end;
	end;
	writeln(ans);
end.


program calendar;
const
        fi='';
        fo='';
var
        n:longint;
        k:byte;
        ans:int64;
procedure Install;
begin
        assign(input,fi);
        assign(output,fo);
        reset(input);
        rewrite(output);
end;

procedure Close_All;
begin
        close(input);
        close(output);
end;

procedure Enter;
begin
        readln(n,k);
end;

function Check(x:int64):boolean;
begin
        if (9*x mod 11 +6) mod 11 =k then exit(true)
        else exit(false);
end;

procedure Process;
var
        x:int64;
begin
        x:=1;
        while x<=(7*(n-2)+5-7) do
        begin
                if (1<=x mod 7) and (x mod 7<=5)
                and Check(x) then inc(ans);
                inc(x);
        end;
        writeln(ans);
end;

BEGIN
        Install;
        Enter;
        Process;
        Close_All;
END.

Max Score: $250$ Points

Problem Statement

Snuke has a very long calendar. It has a grid with $n$ rows and $7$ columns. One day, he noticed that calendar has a following regularity.

The cell at the $i$-th row and $j$-th column contains the integer $7i+j-7$.

A good sub-grid is a $3 \times 3$ sub-grid and the sum of integers in this sub-grid mod $11$ is $k$.

How many good sub-grid are there? Write a program and help him.

Input

The input is given from standard input in the following format.

$n \quad k$

Output

Print the number of good sub-grid. If there are no solution exists, print 0.

Constraints

$1 \le n \le 10^9$

$0 \le k \le 10$

Subtasks

Subtask 1 [ $150$ points ]

The testcase in the subtask satisfies $1 \le n \le 100$.

Subtask 2 [ $100$ points ]

There are no additional constraints.

Sample Input 1

7 7

Sample Output 1

2

In this case, the calendar likes this matrix.

Sun.

Mon.

Tue.

Wed.

Thu.

Fri.

Sat.

Week 1

1

2

3

4

5

6

7

Week 2

8

9

10

11

12

13

14

Week 3

15

16

17

18

19

20

21

Week 4

22

23

24

25

26

27

28

Week 5

29

30

31

32

33

34

35

Week 6

36

37

38

39

40

41

42

Week 7

43

44

45

46

47

48

49

The cell at $i$-th row and $j$-th column is denoted $(i, j)$.

If upper-left is $(1, 5)$, the sum of integers is $5+6+7+12+13+14+19+20+21=117$.

If upper-left is $(3, 2)$, the sum of integers is $16+17+18+23+24+25+30+31+32=216$.

Therefore, there are 2 good sub-grids.

Sample Input 2

6 0

Sample Output 2

2

If upper-left is $(1, 3)$ or $(4, 4)$, it is a good sub-grid.

Sample Input 3

18 10


var a,b:array[0..20000]of longint;
    n,x,i:longint;
begin
  readln(n);
  for i:=1 to n do
    a[i]:=20009*i;
  for i:=1 to n do
    b[i]:=20009*(n-i+1);
  for i:=1 to n do
  begin
    read(x);
    dec(a[x],n-i+1);
  end;
  for i:=1 to n do
    write(a[i],' ');
  writeln;
  for i:=1 to n do
    write(b[i],' ');
  writeln;
end.

Score : 400 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

You are given a permutation p of the set {1, 2, ..., N}. Please construct two sequences of positive integers a_1, a_2, ..., a_N and b_1, b_2, ..., b_N satisfying the following conditions:

1 \leq a_i, b_i \leq 10^9 for all i

a_1 < a_2 < ... < a_N

b_1 > b_2 > ... > b_N

a_{p_1}+b_{p_1} < a_{p_2}+b_{p_2} < ... < a_{p_N}+b_{p_N}

Constraints

2 \leq N \leq 20,000

p is a permutation of the set {1, 2, ..., N}

Input

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

N
p_1 p_2 ... p_N

Output

The output consists of two lines. The first line contains a_1, a_2, ..., a_N seperated by a space. The second line contains b_1, b_2, ..., b_N seperated by a space.

It can be shown that there always exists a solution for any input satisfying the constraints.

Sample Input 1

2
1 2

Sample Output 1

1 4
5 4

a_1 + b_1 = 6 and a_2 + b_2 = 8. So this output satisfies all conditions.

Sample Input 2

3
3 2 1

Sample Output 2

1 2 3
5 3 1

Sample Input 3

3
2 3 1

Sample Output 3

5 10 100
100 10 1


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a,b,c:Integer;
begin
  try
    { TODO -oUser -cConsole Main : わからん。 }
    Read(a);
    Read(b);
    Readln(c);

    if (a+b=c) or (a+c=b) or (b+c=a) then
      Writeln('Yes')
    else
      Writeln('No');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

Two students of AtCoder Kindergarten are fighting over candy packs.

There are three candy packs, each of which contains a, b, and c candies, respectively.

Teacher Evi is trying to distribute the packs between the two students so that each student gets the same number of candies. Determine whether it is possible.

Note that Evi cannot take candies out of the packs, and the whole contents of each pack must be given to one of the students.

Constraints

1 ≦ a, b, c ≦ 100

Input

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

a b c

Output

If it is possible to distribute the packs so that each student gets the same number of candies, print Yes. Otherwise, print No.

Sample Input 1

10 30 20

Sample Output 1

Yes

Give the pack with 30 candies to one student, and give the two packs with 10 and 20 candies to the other. Then, each gets 30 candies.

Sample Input 2

30 30 100

Sample Output 2

No

In this case, the student who gets the pack with 100 candies always has more candies than the other.

Note that every pack must be given to one of them.

Sample Input 3

56 25 31

Sample Output 3

Yes