var
    n,i:longint;
    a:array[0..200010] of longint;
    b:array[0..200010] of int64;
    min,max,ans:int64;
procedure find(h,t:longint);
var
    l,r,q,mid,x,y,x1,y1,x2,y2:longint;
begin
    l:=h;r:=t;q:=(b[t]-b[h-1]) div 2;
    while l<>r-1 do
        begin
            mid:=(l+r) div 2;
            if b[mid]-b[h-1]>q then r:=mid else l:=mid;
        end;
    x1:=b[l]-b[h-1];
    y1:=b[t]-b[l];
    x2:=b[r]-b[h-1];
    y2:=b[t]-b[r];
    if abs(x1-y1)<abs(x2-y2) then
        begin
            x:=x1;y:=y1;
        end
    else
        begin
            x:=x2;y:=y2;
        end;
    if x>max then max:=x;
    if x<min then min:=x;
    if y>max then max:=y;
    if y<min then min:=y;
end;
begin
    readln(n);
    for i:=1 to n do
        begin
            read(a[i]);
            b[i]:=b[i-1]+a[i];
        end;
    ans:=maxlongint;
    for i:=2 to n-2 do
        begin
            max:=0;
            min:=maxlongint;
            find(1,i);
            find(i+1,n);
            if max-min<ans then ans:=max-min;
        end;
    writeln(ans);
end.

Score : 600 points

Problem Statement

Snuke has an integer sequence A of length N.

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

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

Constraints

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

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

5
3 2 4 1 2

Sample Output 1

2

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

Sample Input 2

10
10 71 84 33 6 47 23 25 52 64

Sample Output 2

36

Sample Input 3

7
1 2 3 1000000000 4 5 6

Sample Output 3

999999994


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

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

Submitted source-code file size, in bytes.

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

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


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

Score : 300 points

Problem Statement

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

On this sequence, Snuke can perform the following operation:

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

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

Constraints

2 \leq K \leq N \leq 100000

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

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of operations required.

Sample Input 1

4 3
2 3 1 4

Sample Output 1

2

One optimal strategy is as follows:

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

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

Sample Input 2

3 3
1 2 3

Sample Output 2

1

Sample Input 3

8 3
7 3 1 8 4 6 2 5

Sample Output 3

4


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


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


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

Score : 500 points

Problem Statement

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

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

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

Constraints

1 \leq K

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

Input

Input is given from Standard Input in the following format:

K

Output

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

Sample Input 1

10

Sample Output 1

1
2
3
4
5
6
7
8
9
19


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


var n:double;
    k,i,j,i_num:integer;
    a:array[1..10000] of integer;
begin
    readln(n);
    for i:=1 to trunc(n) do begin
        read(a[i]);
        end;

    j:=1;
    while  j>=1  do begin
        j:=0;
        for i:=1 to trunc(n) do begin
            if (a[i] mod 2=0) then inc(j);
            end;
        if j>=1 then begin
           k:=0;
           for i:=1 to trunc(n) do begin
               if (k=0)and(a[i] mod 2=0) then begin
                   a[i]:=a[i] div 2;k:=1;
                   end else begin
                   a[i]:=a[i] * 3;
                   end;
                end;
            inc(i_num);
            end;
        end;

    writeln(i_num);
end.

Score: 100 points

Problem Statement

E869120's and square1001's 16-th birthday is coming soon.

Takahashi from AtCoder Kingdom gave them a round cake cut into 16 equal fan-shaped pieces.

E869120 and square1001 were just about to eat A and B of those pieces, respectively,

when they found a note attached to the cake saying that "the same person should not take two adjacent pieces of cake".

Can both of them obey the instruction in the note and take desired numbers of pieces of cake?

Constraints

A and B are integers between 1 and 16 (inclusive).

A+B is at most 16.

Input

Input is given from Standard Input in the following format:

A B

Output

If both E869120 and square1001 can obey the instruction in the note and take desired numbers of pieces of cake, print Yay!; otherwise, print :(.

Sample Input 1

5 4

Sample Output 1

Yay!

Both of them can take desired number of pieces as follows:

Sample Input 2

8 8

Sample Output 2

Yay!

Both of them can take desired number of pieces as follows:

Sample Input 3

11 4

Sample Output 3

:(

In this case, there is no way for them to take desired number of pieces, unfortunately.


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

Score: 300 points

Problem Statement

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

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

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

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

At most how many operations can be performed?

Constraints

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

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

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print the maximum number of operations that Snuke can perform.

Sample Input 1

3
5 2 4

Sample Output 1

3

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

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

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

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

Sample Input 2

4
631 577 243 199

Sample Output 2

0

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

Sample Input 3

10
2184 2126 1721 1800 1024 2528 3360 1945 1280 1776

Sample Output 3

39


uses math;
var n,m,i,j,k:longint;x,y,z:int64;
    a:array[-1..1000,1..8]of int64;
    ans,s:int64;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    readln(x,y,z);
    a[i,1]:=x+y+z;
    a[i,2]:=x+y-z;
    a[i,3]:=x-y+z;
    a[i,4]:=x-y-z;
    a[i,5]:=-x+y+z;
    a[i,6]:=-x+y-z;
    a[i,7]:=-x-y+z;
    a[i,8]:=-x-y-z;
  end;
  for k:=1 to 8 do
    for i:=1 to n-1 do
      for j:=i+1 to n do
        if a[i,k]<a[j,k] then
        begin
          x:=a[i,k];a[i,k]:=a[j,k];a[j,k]:=x;
        end;
  for i:=1 to 8 do
  begin
    s:=0;
    for j:=1 to m do
      inc(s,a[j,i]);
    ans:=max(ans,s);
  end;
  writeln(ans);
end.

Score: 400 points

Problem Statement

Takahashi became a pastry chef and opened a shop La Confiserie d'ABC to celebrate AtCoder Beginner Contest 100.

The shop sells N kinds of cakes.

Each kind of cake has three parameters "beauty", "tastiness" and "popularity". The i-th kind of cake has the beauty of x_i, the tastiness of y_i and the popularity of z_i.

These values may be zero or negative.

Ringo has decided to have M pieces of cakes here. He will choose the set of cakes as follows:

Do not have two or more pieces of the same kind of cake.

Under the condition above, choose the set of cakes to maximize (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity).

Find the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.

Constraints

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

M is an integer between 0 and N (inclusive).

x_i, y_i, z_i \ (1 \leq i \leq N) are integers between -10 \ 000 \ 000 \ 000 and 10 \ 000 \ 000 \ 000 (inclusive).

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1 z_1
x_2 y_2 z_2
: :
x_N y_N z_N

Output

Print the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.

Sample Input 1

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

Sample Output 1

56

Consider having the 2-nd, 4-th and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:

Beauty: 1 + 3 + 9 = 13

Tastiness: 5 + 5 + 7 = 17

Popularity: 9 + 8 + 9 = 26

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 13 + 17 + 26 = 56. This is the maximum value.

Sample Input 2

5 3
1 -2 3
-4 5 -6
7 -8 -9
-10 11 -12
13 -14 15

Sample Output 2

54

Consider having the 1-st, 3-rd and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:

Beauty: 1 + 7 + 13 = 21

Tastiness: (-2) + (-8) + (-14) = -24

Popularity: 3 + (-9) + 15 = 9

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 21 + 24 + 9 = 54. This is the maximum value.

Sample Input 3

10 5
10 -80 21
23 8 38
-94 28 11
-26 -2 18
-69 72 79
-26 -86 -54
-72 -50 59
21 65 -32
40 -94 87
-62 18 82

Sample Output 3

638

If we have the 3-rd, 4-th, 5-th, 7-th and 10-th kinds of cakes, the total beauty, tastiness and popularity will be -323, 66 and 249, respectively.

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 323 + 66 + 249 = 638. This is the maximum value.

Sample Input 4

3 2
2000000000 -9000000000 4000000000
7000000000 -5000000000 3000000000
6000000000 -1000000000 8000000000

Sample Output 4

30000000000

The values of the beauty, tastiness and popularity of the cakes and the value to be printed may not fit into 32-bit integers.


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

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

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

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

Score : 300 points

Problem Statement

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

1 yen (the currency of Japan)

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

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

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

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

Constraints

1 \leq N \leq 100000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

127

Sample Output 1

4

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

Sample Input 2

3

Sample Output 2

3

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

Sample Input 3

44852

Sample Output 3

16


uses SysUtils;
var i,y,i1,x,a,b:Longint;
    ans,n:Longint;

begin

 readln(n);
 ans:=9999999;
 for i:=1 to n-1 do begin
      a:=trunc(i); b:=trunc(n)-a;
      //writeln('a=',a,'b=',b);
      x:=0;
      for i1:=1 to length(inttostr(a)) do inc(x,strtoint(copy(inttostr(a),i1,1)));
      //writeln('a ',a,' kakui wa=',x);
      y:=0;
      for i1:=1 to length(inttostr(b)) do inc(y,strtoint(copy(inttostr(b),i1,1)));
      //writeln('b ',b,' kakui wa=',y);
      if ans>x+y then ans:=x+y;
      end;
writeln(trunc(ans));
end.

Score : 200 points

Problem Statement

Takahashi has two positive integers A and B.

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

Constraints

2 ≤ N ≤ 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

15

Sample Output 1

6

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

Sample Input 2

100000

Sample Output 2

10


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

 function mu(a1,b1,p1:int64):int64;
 var q:int64;
 begin
   if b1=0 then exit(1);

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

 begin

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

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

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

  end.

Score : 700 points

Problem Statement

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

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

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

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

Constraints

1 ≤ N ≤ 3×10^5

1 ≤ A,B ≤ 3×10^5

0 ≤ K ≤ 18×10^{10}

All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N A B K

Output

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

Sample Input 1

4 1 2 5

Sample Output 1

40

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

1 green, 1 blue

1 red, 2 blues

2 reds, 1 green

3 reds, 1 blue

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

Sample Input 2

2 5 6 0

Sample Output 2

1

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

Sample Input 3

90081 33447 90629 6391049189

Sample Output 3

577742975


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

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

 begin

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

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

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

  end.


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

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

 begin

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

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

  readln(n,a,b,k);

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

  end.


var n,x,y:ansistring;
    i,j,k:longint;
    dem,l,o,a,b:int64;
begin
  readln(a);
  read(n);
  dem:=a;
  l:=low(int64);
  for k:=1 to a do
    begin
      dem:=0;
      x:=copy(n,1,k);
      y:=copy(n,k+1,a-k);
      for i:=1 to length(x)-1 do
        for j:=i+1 to length(x) do
          if x[i]=x[j] then x[j]:='0';
      for i:=1 to length(y)-1 do
        for j:=i+1 to length(y) do
          if y[i]=y[j] then y[j]:='1';
      for i:=1 to length(x) do
        for j:=1 to length(y) do if x[i]=y[j] then dem:=dem+1;
      if dem>l then l:=dem;;
    end;
  writeln(l);
  readln;
end.

Score : 200 points

Problem Statement

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

Constraints

2 \leq N \leq 100

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

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

Sample Input 1

6
aabbca

Sample Output 1

2

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

Sample Input 2

10
aaaaaaaaaa

Sample Output 2

1

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

Sample Input 3

45
tgxgdqkyjzhyputjjtllptdfxocrylqfqjynmfbfucbir

Sample Output 3

9


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

Score : 300 points

Problem Statement

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

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

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

Constraints

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

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

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

Sample Input 1

5
WEEWW

Sample Output 1

1

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

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3


program Project1;
{$APPTYPE CONSOLE}
uses
  SysUtils,math;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k,d,p:Integer;
  l:array[0..40] of Integer=(1, 4, 8, 9, 16, 25, 27, 32, 36, 49, 64, 81, 100, 121, 125, 128, 144, 169,196,
   216, 225, 243, 256, 289, 324, 343, 361, 400, 441, 484, 512, 529, 576, 625, 676, 729, 784, 841, 900, 961,1000);
  s,s123,s1,s2,s3:string;
  al:string = 'abcdefghijklmnopqrstuvwxyz';
  tf,tfl,tfr:Boolean;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    Readln(x);
    y:=0;

    while True do begin
      if l[y]>x then begin
        Writeln(l[y-1]);
        Readln;
        exit;
      end;
      inc(y);
    end;
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

You are given a positive integer X.
Find the largest perfect power that is at most X.
Here, a perfect power is an integer that can be represented as b^p, where b is an integer not less than 1 and p is an integer not less than 2.

Constraints

1 ≤ X ≤ 1000

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the largest perfect power that is at most X.

Sample Input 1

10

Sample Output 1

9

There are four perfect powers that are at most 10: 1, 4, 8 and 9.
We should print the largest among them, 9.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

999

Sample Output 3

961


{$mode objfpc}
program C;
uses
  classes;
var
  S: AnsiString;
  k: Integer;
  i, j: Integer;
  AllSubStr: TStringList;

begin
  ReadLn(S);
  ReadLn(k);

  AllSubStr := TStringList.Create;
  AllSubStr.Duplicates := dupIgnore;

  for i := 1 to Length(S) - k + 1 do
    for j := 1 to k do
      AllSubStr.Add(Copy(S, i, j));
  AllSubStr.Sort;

  i := 0;
  for j := 0 to AllSubStr.Count - 1 do
    if AllSubStr[i] <> AllSubStr[j] then
    begin
      Inc(i);
      AllSubStr[i] := AllSubStr[j];
    end;
  {
  for j := 0 to i do
    WriteLn(AllSubStr[j]);
  }
  WriteLn(AllSubStr[k - 1]);

end.

Score : 300 points

Problem Statement

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

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

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

Constraints

1 ≤ |s| ≤ 5000

s consists of lowercase English letters.

1 ≤ K ≤ 5

s has at least K different substrings.

Partial Score

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

Input

Input is given from Standard Input in the following format:

s
K

Output

Print the K-th lexicographically smallest substring of K.

Sample Input 1

aba
4

Sample Output 1

b

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

Sample Input 2

atcoderandatcodeer
5

Sample Output 2

andat

Sample Input 3

z
1

Sample Output 3

z


var s:ansistring;
    a:array[0..100000]of string;
    i,j:longint;
    n,k:longint;
    num:longint;
    s1:string;
procedure qs(l,r:longint);
var i,j:longint;
    m,t:string;
begin
  i:=l;
  j:=r;
  m:=a[(l+r)>>1];
  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 l<j then qs(l,j);
  if i<r then qs(i,r);
end;
begin
  readln(s);
  n:=length(s);
  readln(k);
  for i:=1 to n do
  begin
    s1:='';
    for j:=0 to k-1 do
    if i+j<=n then
    begin
      s1:=s1+s[i+j];
      inc(num);
      a[num]:=s1;
    end;
  end;
  qs(1,num);
  for i:=1 to num do
  begin
    if a[i]<>a[i-1] then dec(k);
    if k=0 then
    begin
      writeln(a[i]);
      exit;
    end;
  end;
end.


var
	n,m,i,a,b,c:Longint;
	pa,p:array[1..100000]of Longint;
function f(u:Longint):Longint;
begin
	if pa[u]<>u then pa[u]:=f(pa[u]);
	f:=pa[u];
end;
begin
	read(n,m);
	for i:=1 to n do begin
		read(p[i]);
		pa[i]:=i;
	end;
	for i:=1 to m do begin
		read(a,b);
		a:=f(a);
		b:=f(b);
		if a<>b then pa[a]:=b;
	end;
	for i:=1 to n do if f(p[i])=f(i)then inc(c);
	writeln(c);
end.

Score : 400 points

Problem Statement

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

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

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

Constraints

2 ≤ N ≤ 10^5

1 ≤ M ≤ 10^5

p is a permutation of integers from 1 through N.

1 ≤ x_j,y_j ≤ N

x_j ≠ y_j

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

5 2
5 3 1 4 2
1 3
5 4

Sample Output 1

2

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

Sample Input 2

3 2
3 2 1
1 2
2 3

Sample Output 2

3

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

Sample Input 3

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

Sample Output 3

8

Sample Input 4

5 1
1 2 3 4 5
1 5

Sample Output 4

5

We do not have to perform the operation.


var
	a, b, i, j, total: integer;
begin
	readln(a, b);
	total:= 0;
	for i := 1 to a do begin
		for j := 1 to b do begin
			if i=j then
			total:= total+1;
		end;
	end;
	writeln(total);
end.

Score: 100 points

Problem Statement

In AtCoder Kingdom, Gregorian calendar is used, and dates are written in the "year-month-day" order, or the "month-day" order without the year.

For example, May 3, 2018 is written as 2018-5-3, or 5-3 without the year.

In this country, a date is called Takahashi when the month and the day are equal as numbers. For example, 5-5 is Takahashi.

How many days from 2018-1-1 through 2018-a-b are Takahashi?

Constraints

a is an integer between 1 and 12 (inclusive).

b is an integer between 1 and 31 (inclusive).

2018-a-b is a valid date in Gregorian calendar.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the number of days from 2018-1-1 through 2018-a-b that are Takahashi.

Sample Input 1

5 5

Sample Output 1

5

There are five days that are Takahashi: 1-1, 2-2, 3-3, 4-4 and 5-5.

Sample Input 2

2 1

Sample Output 2

1

There is only one day that is Takahashi: 1-1.

Sample Input 3

11 30

Sample Output 3

11

There are eleven days that are Takahashi: 1-1, 2-2, 3-3, 4-4, 5-5, 6-6, 7-7, 8-8, 9-9, 10-10 and 11-11.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k:Integer;
  l:array of array of char;
  s,s123,s1,s2,s3:string;
  tf:Boolean;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(h);
    Readln(w);
    SetLength(l,h+2,w+2);

    for i := 0 to h+1 do
      for j := 0 to w+1 do
        l[i][j]:='.';

    for i := 1 to h  do begin
      Readln(s);
      for j := 1 to Length(s) do
        l[i][j]:=s[j];
    end;

    for i := 1 to h do
      for j := 1 to w do begin
        if (l[i][j]='#')and(l[i-1][j]='.')and(l[i+1][j]='.')and(l[i][j-1]='.')and(l[i][j+1]='.') then begin
          Writeln('No');
          Readln;
          exit;
        end;
      end;

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

Score: 300 points

Problem Statement

We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).

Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..

However, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.

Determine if square1001 can achieve his objective.

Constraints

H is an integer between 1 and 50 (inclusive).

W is an integer between 1 and 50 (inclusive).

For every (i, j) (1 \leq i \leq H, 1 \leq j \leq W), s_{i, j} is # or ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}
s_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}
: :
s_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}

Output

If square1001 can achieve his objective, print Yes; if he cannot, print No.

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.

Sample Input 2

5 5
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##.

Sample Output 3

Yes


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

end.

Score: 400 points

Problem Statement

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

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

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

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

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

Notes

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

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

5

Sample Output 1

3 5 7 11 31

Let us see if this output actually satisfies the conditions.

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

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

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

Sample Input 2

6

Sample Output 2

2 3 5 7 11 13

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

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

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

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

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

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

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

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

Sample Input 3

8

Sample Output 3

2 5 7 13 19 37 67 79


var
  n,s,flag,i,left,right:longint;
  tot:int64;
  x,p:array[1..200000]of int64;
begin
 readln(n,s);
 for i:=1 to n do
 begin
   readln(x[i],p[i]);
 end;
 if s<=x[1] then
 begin
   tot:=x[n]-s;
   write(tot);
    exit;
    end;
  if s>x[n] then
    begin
    tot:=s-x[1];
    write(tot);
    exit;
    end;
    left:=1;
    right:=n;
    if p[1]<p[n] then flag:=2
    else flag:=1;
    if p[1]<p[n] then begin p[n]:=p[n]+p[1]; tot:=tot+x[right]-x[left]; inc(left);  end
                  else begin p[1]:=p[1]+p[n]; tot:=tot+x[right]-x[left];dec(right); end;
  while(x[left]<s)and(x[right]>s)do begin
  if p[left]>=p[right] then
   begin
     p[left]:=p[left]+p[right];
     if flag=2 then tot:=tot+x[right]-x[left];
     dec(right);
     if flag=2 then flag:=1;
   end
   else begin
     p[right]:=p[right]+p[left];
    if flag=1 then tot:=tot+x[right]-x[left];
     inc(left);
     if flag=1 then flag:=2;
       end;
    end;
  if x[left]>s then write(tot+x[right]-s)
               else write(tot+s-x[left])
end.

Score : 1200 points

Problem Statement

There are N apartments along a number line, numbered 1 through N.
Apartment i is located at coordinate X_i.
Also, the office of AtCoder Inc. is located at coordinate S.
Every employee of AtCoder lives in one of the N apartments.
There are P_i employees who are living in Apartment i.

All employees of AtCoder are now leaving the office all together.
Since they are tired from work, they would like to get home by the company's bus.
AtCoder owns only one bus, but it can accommodate all the employees.
This bus will leave coordinate S with all the employees and move according to the following rule:

Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off.

Repeat the operation above as long as there is one or more employees on the bus.

For a specific example, see Sample Input 1.

The bus takes one seconds to travel a distance of 1.
The time required to vote and get off the bus is ignorable.

Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time.
Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future.
Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction.

Find the time the bus will take from departure to arrival at the last employees' apartment.
It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time.

Constraints

1 \leq N \leq 10^5

1 \leq S \leq 10^9

1 \leq X_1 < X_2 < ... < X_N \leq 10^9

X_i \neq S ( 1 \leq i \leq N )

1 \leq P_i \leq 10^9 ( 1 \leq i \leq N )

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N S
X_1 P_1
X_2 P_2
:
X_N P_N

Output

Print the number of seconds the bus will take from departure to arrival at the last employees' apartment.

Sample Input 1

3 2
1 3
3 4
4 2

Sample Output 1

4

Assume that the bus moves in the negative direction first.
Then, the coordinate of the bus changes from 2 to 1, and the employees living in Apartment 1 get off.
The movement of the bus after that is obvious: it just continues moving in the positive direction.
Thus, if the bus moves in the negative direction first, the coordinate of the bus changes as 2 → 1 → 2 → 3 → 4 from departure.
The time it takes to get home for the employees living in Apartment 1, 2, 3 are 1, 3, 4 seconds, respectively.

Next, assume that the bus moves in the positive direction first.
Then, the coordinate of the bus changes from 2 to 3, and the employees living in Apartment 2 get off.
Afterwards, the bus starts heading to Apartment 1, because there are more employees living in Apartment 1 than in Apartment 3.
Then, after arriving at Apartment 1, the bus heads to Apartment 3.
Thus, if the bus moves in the positive direction first, the coordinate of the bus changes as 2 → 3 → 2 → 1 → 2 → 3 → 4 from departure.
The time it takes to get home for the employees living in Apartment 1, 2, 3 are 3, 1, 6 seconds, respectively.

Therefore, in the beginning, the employees living in Apartment 1 or 3 will try to move the bus in the negative direction.
On the other hand, the employees living in Apartment 2 will try to move the bus in the positive direction in the beginning.
There are a total of 3 + 2 = 5 employees living in Apartment 1 and 3 combined, which is more than those living in Apartment 2, which is 4.
Thus, the bus will move in the negative direction first, and the coordinate of the bus will change as 2 → 1 → 2 → 3 → 4 from departure.

Sample Input 2

6 4
1 10
2 1000
3 100000
5 1000000
6 10000
7 100

Sample Output 2

21

Since the numbers of employees living in different apartments are literally off by a digit, the bus consistently head to the apartment where the most number of employees on the bus lives.

Sample Input 3

15 409904902
94198000 15017
117995501 7656764
275583856 313263626
284496300 356635175
324233841 607
360631781 148
472103717 5224
497641071 34695
522945827 816241
554305668 32
623788284 22832
667409501 124410641
876731548 12078
904557302 291749534
918215789 5

Sample Output 3

2397671583


var
  i,j,flag:longint;
  n,s,left,right,tot:int64;
  p,x:array[1..100000]of int64;
begin
  readln(n,s);
  for i:=1 to n do
    readln(x[i],p[i]);
  if s<=x[1] then
    begin
      write(x[n]-s);
      exit;
    end;
  left:=1;
  right:=n;
  if p[1]>=p[n] then begin p[1]:=p[1]+p[n]; dec(right);flag:=1;end
                else begin p[n+1]:=p[n]+p[1]; inc(left);flag:=-1;end;
  tot:=tot+x[n]-x[1];
  while (x[left]<s)and(x[right]>s) do
    begin
      if p[left]>=p[right] then begin
         p[left]:=p[left]+p[right];
         if flag<>1 then begin tot:=tot+x[right]-x[left];  flag:=1;end;
         dec(right); end
                           else begin
         p[right]:=p[left]+p[right];
         if flag<>-1 then begin tot:=tot+x[right]-x[left];  flag:=-1;end;
         inc(left);   end;
    end;
    if x[left]>s then writeln(tot+x[right]-s)
                 else writeln(tot+s-x[left]);
end.


var
  n,s,flag,left,right,i,j,tot:longint;
  p,x:array[1..100000]of longint;
begin
  readln(n,s);
  for i:=1 to n do
    readln(x[i],p[i]);
  if s<=x[1] then
    begin
      write(x[n]-s);
      exit;
    end;
  left:=1;
  right:=n;
  if p[1]>=p[n] then begin p[1]:=p[1]+p[n]; dec(right);flag:=1;end
                else begin p[n+1]:=p[n]+p[1]; inc(left);flag:=-1;end;
  tot:=tot+x[n]-x[1];
  while (x[left]<s)and(x[right]>s) do
    begin
      if p[left]>=p[right] then begin
         p[left]:=p[left]+p[right];
         if flag<>1 then begin tot:=tot+x[right]-x[left];  flag:=1;end;
         dec(right); end
                           else begin
         p[right]:=p[left]+p[right];
         if flag<>-1 then begin tot:=tot+x[right]-x[left];  flag:=-1;end;
         inc(left);   end;
    end;
    if x[left]>s then writeln(tot+x[right]-s)
                 else writeln(tot+s-x[left]);
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k:Integer;
  l:array of array of char;
  s,s123,s1,s2,s3:string;
  tf:Boolean;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(s);
    a:=700;
    if s[1]='o' then
      a:=a+100;
    if s[2]='o' then
      a:=a+100;
    if s[3]='o' then
      a:=a+100;

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

Score : 100 points

Problem Statement

In "Takahashi-ya", a ramen restaurant, a bowl of ramen costs 700 yen (the currency of Japan), plus 100 yen for each kind of topping (boiled egg, sliced pork, green onions).

A customer ordered a bowl of ramen and told which toppings to put on his ramen to a clerk. The clerk took a memo of the order as a string S. S is three characters long, and if the first character in S is o, it means the ramen should be topped with boiled egg; if that character is x, it means the ramen should not be topped with boiled egg. Similarly, the second and third characters in S mean the presence or absence of sliced pork and green onions on top of the ramen.

Write a program that, when S is given, prints the price of the corresponding bowl of ramen.

Constraints

S is a string of length 3.

Each character in S is o or x.

Input

Input is given from Standard Input in the following format:

S

Output

Print the price of the bowl of ramen corresponding to S.

Sample Input 1

oxo

Sample Output 1

900

The price of a ramen topped with two kinds of toppings, boiled egg and green onions, is 700 + 100 \times 2 = 900 yen.

Sample Input 2

ooo

Sample Output 2

1000

The price of a ramen topped with all three kinds of toppings is 700 + 100 \times 3 = 1000 yen.

Sample Input 3

xxx

Sample Output 3

700

The price of a ramen without any toppings is 700 yen.


program C;
var
  na, nb, nc: Int64;
  x, y: Int64;

function Solve: Int64;
begin
  Result := x * na + y * nb;

  while (0 < x) and (0 < y) do
  begin
    if 2 * nc < na + nb then
    begin
      Inc(Result, 2 * nc - na - nb);
      Dec(x);
      Dec(y);
    end
    else
      break;
  end;

  while 0 < x do
  begin
    if 2 * nc < na then
    begin
      Inc(Result, 2 * nc - na);
      Dec(x);
    end
    else
      break;
  end;

  while 0 < y do
  begin
    if 2 * nc < nb then
    begin
      Inc(Result, 2 * nc - nb);
      Dec(y);
    end
    else
      break;
  end;

end;

begin
  ReadLn(na, nb, nc, x, y);

  WriteLn(Solve);
end.

Score : 300 points

Problem Statement

"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

Constraints

1 ≤ A, B, C ≤ 5000

1 ≤ X, Y ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C X Y

Output

Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

Sample Input 1

1500 2000 1600 3 2

Sample Output 1

7900

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.

Sample Input 2

1500 2000 1900 3 2

Sample Output 2

8500

It is optimal to directly buy three A-pizzas and two B-pizzas.

Sample Input 3

1500 2000 500 90000 100000

Sample Output 3

100000000

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.


Uses math;
var
	ans,L,R,Lmax,Rmax,c:int64;
	n,i:Longint;
	x,v:array[1..100000]of int64;
begin
	read(n,c);
	for i:=1 to n do begin
		read(x[i],v[i]);
		inc(L,v[i]);
	end;
	ans:=L-x[n];
	for i:=n downto 1 do begin
		dec(L,v[i]);
		inc(R,v[i]);
		Rmax:=Max(Rmax,R-(c-x[i]));
		if i>1 then ans:=Max(ans,L-x[i-1]*2+Rmax)else ans:=Max(ans,Rmax);
	end;
	for i:=1 to n do begin
		inc(L,v[i]);
		dec(R,v[i]);
		Lmax:=Max(Lmax,L-x[i]);
		if i<n then ans:=Max(ans,R-(c-x[i+1])*2+Lmax)else ans:=Max(ans,Lmax);
	end;
	writeln(ans);
end.

Score : 500 points

Problem Statement

"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.

Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.

Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.

Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

Constraints

1 ≤ N ≤ 10^5

2 ≤ C ≤ 10^{14}

1 ≤ x_1 < x_2 < ... < x_N < C

1 ≤ v_i ≤ 10^9

All values in input are integers.

Subscores

300 points will be awarded for passing the test set satisfying N ≤ 100.

Input

Input is given from Standard Input in the following format:

N C
x_1 v_1
x_2 v_2
:
x_N v_N

Output

If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.

Sample Input 1

3 20
2 80
9 120
16 1

Sample Output 1

191

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.

Sample Input 2

3 20
2 80
9 1
16 120

Sample Output 2

192

The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.

Sample Input 3

1 100000000000000
50000000000000 1

Sample Output 3

0

Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.

Sample Input 4

15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000

Sample Output 4

6500000000

All these sample inputs above are included in the test set for the partial score.


var n,c:int64;
    i:longint;
    sum,ans,ans2:int64;
    firstl,firstr:longint;
    x,v,num:array[0..100500]of int64;
begin
  read(n,c);
  for i:=1 to n do
  begin
    read(x[i],v[i]);
    num[i]:=num[i-1]+v[i];
    inc(sum,v[i]);
    if sum-x[i]>ans then
    begin
      ans:=sum-x[i];
      firstl:=i;
    end;
  end;
  sum:=0;
  for i:=n downto 1 do
  begin
    inc(sum,v[i]);
    if sum-(c-x[i])>ans2 then
    begin
      ans2:=sum-(c-x[i]);
      //writeln(i);
    end;
  end;
  if ans2>ans then ans:=ans2;
  for i:=1 to n do
  if x[i]>=c div 2 then
  begin
    firstr:=i;
    firstl:=i-1;
    break;
  end;
  for i:=1 to n do
  if x[i]<c div 2 then
  begin
    while (x[firstr+1]-num[firstr]<x[firstr]-num[firstr-1])and(firstr<n)do inc(firstr);
    if num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c>ans then
    ans:=num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c;
  end;
  if firstl>0 then
  begin
  for i:=n downto 1 do
  if x[i]>=c div 2 then
  begin
    while (num[firstl-1]-x[firstl-1]<num[firstl]-x[firstl])and(firstl>1)do dec(firstl);
    if num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl]>ans then
    ans:=num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl];
  end;
  end;
  writeln(ans);
end.


program Project1;

{$APPTYPE CONSOLE}

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

    for i := 0 to m-1 do begin
      Read(a);
      if a>x then
        Inc(count)
      else
        Inc(count2);
    end;

    Readln;
    Writeln((count+count2-abs(count-count2))div 2);

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

Score : 200 points

Problem Statement

There are N + 1 squares arranged in a row, numbered 0, 1, ..., N from left to right.

Initially, you are in Square X.
You can freely travel between adjacent squares. Your goal is to reach Square 0 or Square N.
However, for each i = 1, 2, ..., M, there is a toll gate in Square A_i, and traveling to Square A_i incurs a cost of 1.
It is guaranteed that there is no toll gate in Square 0, Square X and Square N.

Find the minimum cost incurred before reaching the goal.

Constraints

1 \leq N \leq 100

1 \leq M \leq 100

1 \leq X \leq N - 1

1 \leq A_1 < A_2 < ... < A_M \leq N

A_i \neq X

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M X
A_1 A_2 ... A_M

Output

Print the minimum cost incurred before reaching the goal.

Sample Input 1

5 3 3
1 2 4

Sample Output 1

1

The optimal solution is as follows:

First, travel from Square 3 to Square 4. Here, there is a toll gate in Square 4, so the cost of 1 is incurred.

Then, travel from Square 4 to Square 5. This time, no cost is incurred.

Now, we are in Square 5 and we have reached the goal.

In this case, the total cost incurred is 1.

Sample Input 2

7 3 2
4 5 6

Sample Output 2

0

We may be able to reach the goal at no cost.

Sample Input 3

10 7 5
1 2 3 4 6 8 9

Sample Output 3

3


var a,c,ans:array[0..2050000]of longint;
    i:longint;
    n:longint;
procedure qs(l,r:longint);
var i,j,m,t:longint;
begin
  i:=l;
  j:=r;
  m:=a[(l+r)>>1];
  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;
      t:=c[i];c[i]:=c[j];c[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;
begin
  read(n);
  for i:=1 to n do
  begin
    read(a[i]);
    c[i]:=i;
  end;
  qs(1,n);
  for i:=1 to n do
  if i>n div 2 then ans[c[i]]:=a[n div 2] else ans[c[i]]:=a[n div 2+1];
  for i:=1 to n do
  writeln(ans[i]);
end.

Score : 300 points

Problem Statement

When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the (\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.

You are given N numbers X_1, X_2, ..., X_N, where N is an even number.
For each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i, that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.

Find B_i for each i = 1, 2, ..., N.

Constraints

2 \leq N \leq 200000

N is even.

1 \leq X_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print N lines.
The i-th line should contain B_i.

Sample Input 1

4
2 4 4 3

Sample Output 1

4
3
3
4

Since the median of X_2, X_3, X_4 is 4, B_1 = 4.

Since the median of X_1, X_3, X_4 is 3, B_2 = 3.

Since the median of X_1, X_2, X_4 is 3, B_3 = 3.

Since the median of X_1, X_2, X_3 is 4, B_4 = 4.

Sample Input 2

2
1 2

Sample Output 2

2
1

Sample Input 3

6
5 5 4 4 3 3

Sample Output 3

4
4
4
4
4
4


var m,x,y,n:longint;
    i:longint;
    a:Array[0..100500]of longint;
procedure qs(l,r:longint);
var i,j,m,t:longint;
begin
  i:=l;
  j:=r;
  m:=a[(l+r)>>1];
  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 l<j then qs(l,j);
  if i<r then qs(i,r);
end;
function find1(x:longint):longint;
var l,r,m:longint;
begin
  l:=1;
  r:=n-1;
  while l<=r do
  begin
    m:=(l+r)>>1;
    if a[m]<x then l:=m+1 else r:=m-1;
  end;
  exit(l);
end;
function find2(x:longint):longint;
var l,r,m:longint;
begin
  l:=1;
  r:=n-1;
  while l<=r do
  begin
    m:=(l+r)>>1;
    if a[m]>x then r:=m-1 else l:=m+1;
  end;
  exit(r);
end;
begin
  read(n);
  for i:=1 to n do
  read(a[i]);
  qs(1,n);
  m:=(a[n]+1) div 2;
  x:=find1(m);
  y:=find2(m);
  if a[x]-m<m-a[y] then writeln(a[n],' ',a[x]) else writeln(a[n],' ',a[y]);
end.

Score : 400 points

Problem Statement

Let {\rm comb}(n,r) be the number of ways to choose r objects from among n objects, disregarding order.
From n non-negative integers a_1, a_2, ..., a_n, select two numbers a_i > a_j so that {\rm comb}(a_i,a_j) is maximized.
If there are multiple pairs that maximize the value, any of them is accepted.

Constraints

2 \leq n \leq 10^5

0 \leq a_i \leq 10^9

a_1,a_2,...,a_n are pairwise distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print a_i and a_j that you selected, with a space in between.

Sample Input 1

5
6 9 4 2 11

Sample Output 1

11 6

\rm{comb}(a_i,a_j) for each possible selection is as follows:

\rm{comb}(4,2)=6

\rm{comb}(6,2)=15

\rm{comb}(6,4)=15

\rm{comb}(9,2)=36

\rm{comb}(9,4)=126

\rm{comb}(9,6)=84

\rm{comb}(11,2)=55

\rm{comb}(11,4)=330

\rm{comb}(11,6)=462

\rm{comb}(11,9)=55

Thus, we should print 11 and 6.

Sample Input 2

2
100 0

Sample Output 2

100 0


var
	n,i,ma,mb:Longint;
	a:array[1..200000]of Longint;
function f(c:Longint):Longint;
var L,R,M,cnt,i:Longint;
begin
	L:=0;
	R:=1000000001;
	while R-L>1 do begin
		M:=(L+R)div 2;
		cnt:=0;
		for i:=1 to n do if a[i]<=M then inc(cnt);
		if cnt>=c then R:=M else L:=M;
	end;
	f:=R;
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	ma:=f(n div 2+1);
	mb:=f(n div 2);
	for i:=1 to n do if a[i]<ma then writeln(ma)else writeln(mb);
end.


Uses math;var a,b,c:Longint;begin read(a,b,c);a:=Max(a,Max(b,c))*3-a-b-c;writeln(a div 2+a mod 2*2)end.

Score : 300 points

Problem Statement

You are given three integers A, B and C. Find the minimum number of operations required to make A, B and C all equal by repeatedly performing the following two kinds of operations in any order:

Choose two among A, B and C, then increase both by 1.

Choose one among A, B and C, then increase it by 2.

It can be proved that we can always make A, B and C all equal by repeatedly performing these operations.

Constraints

0 \leq A,B,C \leq 50

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the minimum number of operations required to make A, B and C all equal.

Sample Input 1

2 5 4

Sample Output 1

2

We can make A, B and C all equal by the following operations:

Increase A and C by 1. Now, A, B, C are 3, 5, 5, respectively.

Increase A by 2. Now, A, B, C are 5, 5, 5, respectively.

Sample Input 2

2 6 3

Sample Output 2

5

Sample Input 3

31 41 5

Sample Output 3

23


var n,x,num:int64;
    i:longint;
    can:boolean;
function gcd(a,b:int64):int64;
var c:int64;
begin
  repeat
    c:=a mod b;
    a:=b;
    b:=c;
  until c=0;
  exit(a);
end;
begin
  read(n);
  num:=((n+2)*(n-1) div 2);
  x:=0;
  repeat
    inc(x,num);
    can:=true;
    for i:=2 to n do
    begin
      if gcd(x+num-i,i)=1 then
      begin
        can:=false;
        break;
      end;
    end;
  until can;
  for i:=2 to n do
  write(i,' ');
  writeln(x);
end.

Score : 600 points

Problem Statement

Nagase is a top student in high school. One day, she's analyzing some properties of special sets of positive integers.

She thinks that a set S = \{a_{1}, a_{2}, ..., a_{N}\} of distinct positive integers is called special if for all 1 \leq i \leq N, the gcd (greatest common divisor) of a_{i} and the sum of the remaining elements of S is not 1.

Nagase wants to find a special set of size N. However, this task is too easy, so she decided to ramp up the difficulty. Nagase challenges you to find a special set of size N such that the gcd of all elements are 1 and the elements of the set does not exceed 30000.

Constraints

3 \leq N \leq 20000

Input

Input is given from Standard Input in the following format:

N

Output

Output N space-separated integers, denoting the elements of the set S. S must satisfy the following conditions :

The elements must be distinct positive integers not exceeding 30000.

The gcd of all elements of S is 1, i.e. there does not exist an integer d > 1 that divides all elements of S.

S is a special set.

If there are multiple solutions, you may output any of them. The elements of S may be printed in any order. It is guaranteed that at least one solution exist under the given contraints.

Sample Input 1

3

Sample Output 1

2 5 63

\{2, 5, 63\} is special because gcd(2, 5 + 63) = 2, gcd(5, 2 + 63) = 5, gcd(63, 2 + 5) = 7. Also, gcd(2, 5, 63) = 1. Thus, this set satisfies all the criteria.

Note that \{2, 4, 6\} is not a valid solution because gcd(2, 4, 6) = 2 > 1.

Sample Input 2

4

Sample Output 2

2 5 20 63

\{2, 5, 20, 63\} is special because gcd(2, 5 + 20 + 63) = 2, gcd(5, 2 + 20 + 63) = 5, gcd(20, 2 + 5 + 63) = 10, gcd(63, 2 + 5 + 20) = 9. Also, gcd(2, 5, 20, 63) = 1. Thus, this set satisfies all the criteria.


program aaa;
var
  n,d,x,i,m:longint;
begin
  readln(n,d,x);
  dec(d);
  for i:=1 to n do
  begin
    readln(m);
    x:=x+1+d div m;
  end;
  writeln(x);
  readln;
  readln;
end.

Score : 200 points

Problem Statement

Some number of chocolate pieces were prepared for a training camp.
The camp had N participants and lasted for D days.
The i-th participant (1 \leq i \leq N) ate one chocolate piece on each of the following days in the camp: the 1-st day, the (A_i + 1)-th day, the (2A_i + 1)-th day, and so on.
As a result, there were X chocolate pieces remaining at the end of the camp. During the camp, nobody except the participants ate chocolate pieces.

Find the number of chocolate pieces prepared at the beginning of the camp.

Constraints

1 \leq N \leq 100

1 \leq D \leq 100

1 \leq X \leq 100

1 \leq A_i \leq 100 (1 \leq i \leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
D X
A_1
A_2
:
A_N

Output

Find the number of chocolate pieces prepared at the beginning of the camp.

Sample Input 1

3
7 1
2
5
10

Sample Output 1

8

The camp has 3 participants and lasts for 7 days.
Each participant eats chocolate pieces as follows:

The first participant eats one chocolate piece on Day 1, 3, 5 and 7, for a total of four.

The second participant eats one chocolate piece on Day 1 and 6, for a total of two.

The third participant eats one chocolate piece only on Day 1, for a total of one.

Since the number of pieces remaining at the end of the camp is one, the number of pieces prepared at the beginning of the camp is 1 + 4 + 2 + 1 = 8.

Sample Input 2

2
8 20
1
10

Sample Output 2

29

Sample Input 3

5
30 44
26
18
81
18
6

Sample Output 3

56


var
    n, i, sum: Longint;
    a: array[1..100001] of Longint;

begin
    readln(n);
    sum := 0;
    a[0] := 0;
    a[n+1] := 0;
    for i := 1 to n do
    begin
        read(a[i]);
        inc(sum, abs(a[i] - a[i - 1]));
    end;
    inc(sum, abs(a[n]));
    for i := 1 to n do
    begin
        if ((a[i] >= a[i-1]) and (a[i] <= a[i+1])) or ((a[i] <= a[i-1]) and (a[i] >= a[i+1])) then
        begin
            writeln(sum);
        end
        else
        begin
            writeln(sum - ( abs(a[i-1]-a[i])+abs(a[i]-a[i+1])-abs(a[i+1]-a[i-1]) ));
        end;
    end;
end.

Score : 300 points

Problem Statement

There are N sightseeing spots on the x-axis, numbered 1, 2, ..., N.
Spot i is at the point with coordinate A_i.
It costs |a - b| yen (the currency of Japan) to travel from a point with coordinate a to another point with coordinate b along the axis.

You planned a trip along the axis.
In this plan, you first depart from the point with coordinate 0, then visit the N spots in the order they are numbered, and finally return to the point with coordinate 0.

However, something came up just before the trip, and you no longer have enough time to visit all the N spots, so you decided to choose some i and cancel the visit to Spot i.
You will visit the remaining spots as planned in the order they are numbered.
You will also depart from and return to the point with coordinate 0 at the beginning and the end, as planned.

For each i = 1, 2, ..., N, find the total cost of travel during the trip when the visit to Spot i is canceled.

Constraints

2 \leq N \leq 10^5

-5000 \leq A_i \leq 5000 (1 \leq i \leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N lines.
In the i-th line, print the total cost of travel during the trip when the visit to Spot i is canceled.

Sample Input 1

3
3 5 -1

Sample Output 1

12
8
10

Spot 1, 2 and 3 are at the points with coordinates 3, 5 and -1, respectively.
For each i, the course of the trip and the total cost of travel when the visit to Spot i is canceled, are as follows:

For i = 1, the course of the trip is 0 \rightarrow 5 \rightarrow -1 \rightarrow 0 and the total cost of travel is 5 + 6 + 1 = 12 yen.

For i = 2, the course of the trip is 0 \rightarrow 3 \rightarrow -1 \rightarrow 0 and the total cost of travel is 3 + 4 + 1 = 8 yen.

For i = 3, the course of the trip is 0 \rightarrow 3 \rightarrow 5 \rightarrow 0 and the total cost of travel is 3 + 2 + 5 = 10 yen.

Sample Input 2

5
1 1 1 2 0

Sample Output 2

4
4
4
2
4

Sample Input 3

6
-679 -2409 -3258 3095 -3291 -4462

Sample Output 3

21630
21630
19932
8924
21630
19288


var a,b,t:longint;
    num,bool,y,where:longint;
    i,j:longint;
    map:Array[0..105,0..105]of longint;
    c1,c2:char;
begin
  read(a,b);
  c1:='.';
  c2:='#';
  if a>b then
  begin
    t:=a;a:=b;b:=t;
    c1:='#';
    c2:='.';
  end;
  b:=b-a+1;
  if a=1 then dec(b);
  num:=1;
  bool:=0;
  while a>0 do
  begin
    inc(num);
    bool:=1-bool;
    for i:=1 to num-1 do
    begin
      map[i,num-i]:=bool;
      if bool=1 then dec(a);
      if a=0 then break;
    end;
  end;
  for j:=1 to num-1 do
  begin
    map[num,j]:=0;
    map[num+1,j]:=1;
  end;
  inc(num);
  y:=num;
  for i:=1 to num do
  map[i,num-1]:=1;
  if b<>0 then
  begin
    while num<100 do
    begin
      inc(num);
      if num mod 2=1 then
      begin
        for j:=1 to y-1 do
        map[num,j]:=1;
      end else
      begin
        for j:=1 to y-1 do
        begin
          if (j mod 2=0) then map[num,j]:=1 else dec(b);
          if b=0 then
          begin
            where:=j;
            break;
          end;
        end;
      end;
      if b=0 then
      begin
        for j:=where+1 to y-1 do
        map[num,j]:=1;
        break;
      end;
    end;
  end;
  dec(y);
  while b>0 do
  begin
    inc(y);
    for i:=1 to num do
    begin
      if i mod 2=0 then map[i,y]:=1 else dec(b);
      if b=0 then
      begin
        where:=i;
        break;
      end;
    end;
    inc(y);
    for i:=1 to num do
    map[i,y]:=1;
    if b=0 then
    begin
      for i:=where+1 to num do
      map[i,y-1]:=1;
    end;
  end;
  writeln(num,' ',y);
  for i:=1 to num do
  begin
    for j:=1 to y do
    if map[i,j]=1 then write(c1) else write(c2);
    writeln;
  end;
end.


var a,b,t:longint;
    num,bool,y,where:longint;
    i,j:longint;
    map:Array[0..105,0..105]of longint;
    c1,c2:char;
begin
  read(a,b);
  c1:='.';
  c2:='#';
  if a>b then
  begin
    t:=a;a:=b;b:=t;
    c1:='#';
    c2:='.';
  end;
  b:=b-a+1;
  if a=1 then dec(b);
  num:=1;
  bool:=0;
  while a>0 do
  begin
    inc(num);
    bool:=1-bool;
    for i:=1 to num-1 do
    begin
      map[i,num-i]:=bool;
      if bool=1 then dec(a);
      if a=0 then break;
    end;
  end;
  for j:=1 to num-1 do
  begin
    map[num,j]:=0;
    map[num+1,j]:=1;
  end;
  inc(num);
  y:=num;
  for i:=1 to num do
  map[i,num-1]:=1;
  if b<>0 then
  begin
    while num<100 do
    begin
      inc(num);
      if num mod 2=1 then
      begin
        for j:=1 to y-1 do
        map[num,j]:=1;
      end else
      begin
        for j:=1 to y-1 do
        begin
          if (j mod 2=0) then map[num,j]:=1 else dec(b);
          if b=0 then
          begin
            where:=j;
            break;
          end;
        end;
      end;
      if b=0 then
      begin
        for j:=where+1 to y-1 do
        map[num,j]:=1;
        break;
      end;
    end;
  end;
  dec(y);
  while b>0 do
  begin
    inc(y);
    for i:=1 to num do
    begin
      if i mod 2=0 then map[i,y]:=1 else dec(b);
      if b=0 then
      begin
        where:=i;
        break;
      end;
    end;
    inc(y);
    for i:=1 to num do
    map[i,y]:=1;
    if b=0 then
    begin
      for i:=where+1 to num do
      map[i,y-1]:=1;
    end;
  end;
  writeln(num,' ',y);
  for i:=1 to num do
  begin
    for j:=1 to y do
    write(map[i,j],' ');
    writeln;
  end;
end.

Score : 500 points

Problem Statement

You are given two integers A and B.

Print a grid where each square is painted white or black that satisfies the following conditions, in the format specified in Output section:

Let the size of the grid be h \times w (h vertical, w horizontal). Both h and w are at most 100.

The set of the squares painted white is divided into exactly A connected components.

The set of the squares painted black is divided into exactly B connected components.

It can be proved that there always exist one or more solutions under the conditions specified in Constraints section.
If there are multiple solutions, any of them may be printed.

Notes

Two squares painted white, c_1 and c_2, are called connected when the square c_2 can be reached from the square c_1 passing only white squares by repeatedly moving up, down, left or right to an adjacent square.

A set of squares painted white, S, forms a connected component when the following conditions are met:

Any two squares in S are connected.

No pair of a square painted white that is not included in S and a square included in S is connected.

A connected component of squares painted black is defined similarly.

Constraints

1 \leq A \leq 500

1 \leq B \leq 500

Input

Input is given from Standard Input in the following format:

A B

Output

Output should be in the following format:

In the first line, print integers h and w representing the size of the grid you constructed, with a space in between.

Then, print h more lines. The i-th (1 \leq i \leq h) of these lines should contain a string s_i as follows:

If the square at the i-th row and j-th column (1 \leq j \leq w) in the grid is painted white, the j-th character in s_i should be ..

If the square at the i-th row and j-th column (1 \leq j \leq w) in the grid is painted black, the j-th character in s_i should be #.

Sample Input 1

2 3

Sample Output 1

3 3
##.
..#
#.#

This output corresponds to the grid below:

Sample Input 2

7 8

Sample Output 2

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

Sample Input 3

1 1

Sample Output 3

4 2
..
#.
##
##

Sample Input 4

3 14

Sample Output 4

8 18
..................
..................
....##.......####.
....#.#.....#.....
...#...#....#.....
..#.###.#...#.....
.#.......#..#.....
#.........#..####.


var n,m,s,t,i,j:Longint;a,b:array of string;
begin
	readln(n);
	setlength(a,n+1);
	for i:=1 to n do begin
		readln(a[i]);
	end;
	read(m);
	SetLength(b,m+1);
	for i:=1 to m do begin
		readln(b[i]);
	end;
	s:=0;
	for i:=1 to n do begin
		t:=0;
		for j:=1 to n do begin
			if a[i]=a[j] then t:=t+1;
		end;
		for j:=1 to m do begin
			if a[i]=b[j] then t:=t-1;
		end;
		if s<t then s:=t;
	end;
	write(s)
end.

Score : 200 points

Problem Statement

Takahashi has N blue cards and M red cards.
A string is written on each card. The string written on the i-th blue card is s_i, and the string written on the i-th red card is t_i.

Takahashi will now announce a string, and then check every card. Each time he finds a blue card with the string announced by him, he will earn 1 yen (the currency of Japan); each time he finds a red card with that string, he will lose 1 yen.

Here, we only consider the case where the string announced by Takahashi and the string on the card are exactly the same. For example, if he announces atcoder, he will not earn money even if there are blue cards with atcoderr, atcode, btcoder, and so on. (On the other hand, he will not lose money even if there are red cards with such strings, either.)

At most how much can he earn on balance?

Note that the same string may be written on multiple cards.

Constraints

N and M are integers.

1 \leq N, M \leq 100

s_1, s_2, ..., s_N, t_1, t_2, ..., t_M are all strings of lengths between 1 and 10 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N
M
t_1
t_2
:
t_M

Output

If Takahashi can earn at most X yen on balance, print X.

Sample Input 1

3
apple
orange
apple
1
grape

Sample Output 1

2

He can earn 2 yen by announcing apple.

Sample Input 2

3
apple
orange
apple
5
apple
apple
apple
apple
apple

Sample Output 2

1

If he announces apple, he will lose 3 yen. If he announces orange, he can earn 1 yen.

Sample Input 3

1
voldemort
10
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort

Sample Output 3

0

If he announces voldemort, he will lose 9 yen. If he announces orange, for example, he can avoid losing a yen.

Sample Input 4

6
red
red
blue
yellow
yellow
red
5
red
red
yellow
green
blue

Sample Output 4

1


var
 n:longint;
 s:longint;
 map:array[1..210,1..210]of boolean;
 mark:array[1..210]of boolean;
 link:array[1..210]of longint;

procedure init;
var
 i,x,y,j:longint;
 a:array[1..210,1..2]of longint;
begin
 fillchar(map,sizeof(map),0);
 read(n);
 for i:=1 to n do
   readln(a[i,1],a[i,2]);
 for i:=1 to n do
  begin
   read(x,y);
   for j:=1 to n do
     if (x>a[j,1])and(y>a[j,2]) then begin map[j,i+n]:=true;  map[i+n,j]:=true; end;
  end;
 n:=n shl 1;
end;

function find(x:longint):boolean;
var
 i,q:longint;
begin
 for i:=1 to n do
  if map[x,i] and not mark[i] then
   begin
   q:=link[i]; link[i]:=x; mark[i]:=true;
   if (q=0) or find(q) then exit(true);
   link[i]:=q;
   end;
 exit(false);
end;

procedure main;
var
 i:longint;
begin
 s:=0;
 for i:=1 to n do
  begin
  fillchar(mark,sizeof(mark),false);
  if find(i) then inc(s);
  end;
 writeln(s shr 1);
 readln;
 readln;
end;

begin
 init;
 main;
end.

Score : 400 points

Problem Statement

On a two-dimensional plane, there are N red points and N blue points.
The coordinates of the i-th red point are (a_i, b_i), and the coordinates of the i-th blue point are (c_i, d_i).

A red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point.

At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.

Constraints

All input values are integers.

1 \leq N \leq 100

0 \leq a_i, b_i, c_i, d_i < 2N

a_1, a_2, ..., a_N, c_1, c_2, ..., c_N are all different.

b_1, b_2, ..., b_N, d_1, d_2, ..., d_N are all different.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
:
a_N b_N
c_1 d_1
c_2 d_2
:
c_N d_N

Output

Print the maximum number of friendly pairs.

Sample Input 1

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

Sample Output 1

2

For example, you can pair (2, 0) and (4, 2), then (3, 1) and (5, 5).

Sample Input 2

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

Sample Output 2

2

For example, you can pair (0, 0) and (2, 3), then (1, 1) and (3, 4).

Sample Input 3

2
2 2
3 3
0 0
1 1

Sample Output 3

0

It is possible that no pair can be formed.

Sample Input 4

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

Sample Output 4

5

Sample Input 5

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

Sample Output 5

4


var n:longint;
    i,k:longint;
    a,b,c,d:array[0..201000]of int64;
    m0,m1:int64;
    ans:int64;
procedure qs(l,r:longint);
var i,j,m,t:longint;
begin
  i:=l;
  j:=r;
  m:=c[(l+r)>>1];
  repeat
    while c[i]<m do inc(i);
    while c[j]>m do dec(j);
    if i<=j then
    begin
      t:=c[i];c[i]:=c[j];c[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;
function find(x:longint):longint;
var l,r,m:longint;
begin
  l:=1;
  r:=n;
  while l<=r do
  begin
    m:=(l+r)>>1;
    if c[m]<x then l:=l+1 else r:=m-1;
  end;
  exit(l);
end;

begin
  read(n);
  for i:=1 to n do
  read(a[i]);
  for i:=1 to n do
  read(b[i]);
  c[0]:=-1;
  c[n+1]:=1 << 45;
  for k:=1 to 29 do
  begin
    for i:=1 to n do
    begin
      c[i]:=a[i] and ((1 << k)-1);
      d[i]:=b[i] and ((1 << k)-1);
    end;
    //qs(1,n);
    m0:=0;
    m1:=0;
    for i:=1 to n do
    begin
      m1:=m1+find((1 << k)-d[i])-find((1 << (k-1))-d[i]);
      m1:=m1+n+1-find((1 << k)+(1 << (k-1))-d[i]);
    end;
    //writeln('0=',m0,' 1=',m1);
    if m1 and 1 =1 then ans:=ans+1 << (k-1);
  end;
  writeln(ans);
end.


type
  node=record
      y:longint;
      flow:longint;
      next:longint;
  end;
var
   first,dis,a,b,c,d:array[0..100000]of longint;
   q:array[0..1000000]of longint;
   e:array[0..20000]of node;
   i,j:longint;
   n,m:longint;
   x,y,z:longint;
   tot:longint;
   ans:longint;

procedure adde(x,y,z:longint);
begin
  e[tot].next:=first[x];
  e[tot].y:=y;
  e[tot].flow:=z;
  first[x]:=tot;
  inc(tot);
end;

procedure bfs(s:longint);
var i,now,head,tail:longint;
begin
  for i:=0 to n*2+1 do
  dis[i]:=-1;
  dis[s]:=0;
  head:=1;
  tail:=1;
  q[1]:=s;
  while head<=tail do
  begin
    now:=q[head];
    i:=first[now];
    while i<>-1 do
    begin
      y:=e[i].y;
      if (dis[y]<0)and(e[i].flow>0) then
      begin
        dis[y]:=dis[now]+1;
        inc(tail);
        q[tail]:=y;
      end;
      i:=e[i].next;
    end;
    inc(head);
  end;
end;

function dfs(x,mx:longint):longint;
var i,k:longint;
begin
  if x=n*2+1 then exit(mx);
  i:=first[x];
  while i<>-1 do
  begin
    y:=e[i].y;
    if (e[i].flow>0)and(dis[y]=dis[x]+1) then
    begin
      if e[i].flow>mx then k:=dfs(y,mx) else k:=dfs(y,e[i].flow);
      dec(e[i].flow,k);
      inc(e[i xor 1].flow,k);
      if k>0 then exit(k);
    end;
    i:=e[i].next;
  end;
  exit(0);
end;
begin
  read(n);
  for i:=0 to n*2+1 do
  first[i]:=-1;
  for i:=1 to n do
  begin
    read(a[i],b[i]);
    adde(0,i,1);
    adde(i,0,0);
  end;
  for i:=1 to n do
  begin
    read(c[i],d[i]);
    adde(n+i,n*2+1,1);
    adde(n*2+1,n+i,0);
  end;
  for i:=1 to n do
  for j:=1 to n do
  if (a[i]<c[j])and(b[i]<d[j]) then
  begin
    adde(i,n+j,1);
    adde(n+j,i,0);
  end;
  ans:=0;
  while true do
  begin
    bfs(0);
    if dis[2*n+1]<0 then break;
    repeat
      z:=dfs(0,maxint);
      inc(ans,z);
    until z>0;
  end;
  writeln(ans);
end.


var i,j:longint;
    v:array[0..500]of boolean;
    goout,a,b,c,d:array[0..500]of longint;
    n:longint;
    ans:longint;
procedure bfs;
var min,i,x:longint;
begin
  repeat
    min:=n*4;
    for i:=1 to n do
    if (goout[n+i]<min)and(goout[n+i]>0) then
    begin
      min:=goout[n+i];
      x:=i;
    end;
    if min=n*4 then break;
    goout[n+x]:=n*4;
    for i:=1 to n do
    if not v[i] then
    begin
      if (c[x]>a[i])and(d[x]>b[i]) then
      begin
        v[i]:=true;
        for j:=1 to n do
        if (c[j]>a[i])and(d[j]>b[i])and(goout[n+j]<n*4) then dec(goout[n+j]);
        inc(ans);
        break;
      end;
    end;
  until min=n*4;
end;
begin
  read(n);
  for i:=1 to n do
  read(a[i],b[i]);
  for i:=1 to n do
  read(c[i],d[i]);
  for i:=1 to n do
    for j:=1 to n do
    if (a[i]<c[j])and(b[i]<d[j]) then
    begin
      inc(goout[n+j]);
    end;
  bfs;
  writeln(ans);
end.


var i,j:longint;
    v:array[0..500]of boolean;
    goout,a,b,c,d:array[0..500]of longint;
    n:longint;
    ans:longint;
procedure bfs;
var min,i,x:longint;
begin
  repeat
    min:=n*4;
    for i:=1 to n do
    if (goout[n+i]<min)and(goout[n+i]>0) then
    begin
      min:=goout[n+i];
      x:=i;
    end;
    if min=n*4 then break;
    goout[n+x]:=n*4;
    for i:=1 to n do
    if not v[i] then
    begin
      if (c[x]>a[i])and(d[x]>b[i]) then
      begin
        v[i]:=true;
        for j:=1 to n do
        if (c[j]>a[i])and(d[j]>a[i])and(goout[n+j]<n*4) then dec(goout[n+j]);
        inc(ans);
        break;
      end;
    end;
  until min=n*4;
end;
begin
  read(n);
  for i:=1 to n do
  read(a[i],b[i]);
  for i:=1 to n do
  read(c[i],d[i]);
  for i:=1 to n do
    for j:=1 to n do
    if (a[i]<c[j])and(b[i]<d[j]) then
    begin
      inc(goout[n+j]);
    end;
  bfs;
  writeln(ans);
end.


var
	N,i,id,sz,asz,t:Longint;
	pa,x,ans:Array[1..1000]of Longint;
	dp:Array[1..1000]of int64;
	cum:Array[0..1]of int64;
	cumpa:Array[0..1]of Longint;
begin
	read(N);
	for i:=1 to N do begin
		read(dp[i]);
		pa[i]:=0;
		if cum[i mod 2]>0 then begin
			inc(dp[i],cum[i mod 2]);
			pa[i]:=cumpa[i mod 2];
		end;
		if dp[i]>cum[i mod 2]then begin
			cum[i mod 2]:=dp[i];
			cumpa[i mod 2]:=i;
		end;
	end;
	id:=1;
	for i:=2 to N do if dp[i]>dp[id]then id:=i;
	writeln(dp[id]);
	while id>0 do begin
		inc(sz);
		x[sz]:=id;
		id:=pa[id];
	end;
	for i:=N downto x[1]+1 do begin
		inc(asz);
		ans[asz]:=i;
	end;
	for i:=2 to sz do begin
		t:=(x[i-1]-x[i])div 2;
		while t>0 do begin
			inc(asz);
			ans[asz]:=x[i]+t;
			dec(t);
		end;
	end;
	for i:=1 to x[sz]-1 do begin
		inc(asz);
		ans[asz]:=1;
	end;
	writeln(asz);
	for i:=1 to asz do writeln(ans[i]);
end.

Score : 700 points

Problem Statement

You have an integer sequence of length N: a_1, a_2, ..., a_N.

You repeatedly perform the following operation until the length of the sequence becomes 1:

First, choose an element of the sequence.

If that element is at either end of the sequence, delete the element.

If that element is not at either end of the sequence, replace the element with the sum of the two elements that are adjacent to it. Then, delete those two elements.

You would like to maximize the final element that remains in the sequence.

Find the maximum possible value of the final element, and the way to achieve it.

Constraints

All input values are integers.

2 \leq N \leq 1000

|a_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

In the first line, print the maximum possible value of the final element in the sequence.

In the second line, print the number of operations that you perform.

In the (2+i)-th line, if the element chosen in the i-th operation is the x-th element from the left in the sequence at that moment, print x.

If there are multiple ways to achieve the maximum value of the final element, any of them may be printed.

Sample Input 1

5
1 4 3 7 5

Sample Output 1

11
3
1
4
2

The sequence would change as follows:

After the first operation: 4, 3, 7, 5

After the second operation: 4, 3, 7

After the third operation: 11(4+7)

Sample Input 2

4
100 100 -1 100

Sample Output 2

200
2
3
1

After the first operation: 100, 200(100+100)

After the second operation: 200

Sample Input 3

6
-1 -2 -3 1 2 3

Sample Output 3

4
3
2
1
2

After the first operation: -4, 1, 2, 3

After the second operation: 1, 2, 3

After the third operation: 4

Sample Input 4

9
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 4

5000000000
4
2
2
2
2


var
	n,k,ans:int64;
	b:Longint;
begin
	read(n,k);
	for b:=k+1 to n do begin
		inc(ans,n div b*(b-k));
		if n mod b>=k then inc(ans,n mod b-k+1);
	end;
	if k=0 then dec(ans,n);
	writeln(ans);
end.

Score : 400 points

Problem Statement

Takahashi had a pair of two positive integers not exceeding N, (a,b), which he has forgotten.
He remembers that the remainder of a divided by b was greater than or equal to K.
Find the number of possible pairs that he may have had.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N-1

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of possible pairs that he may have had.

Sample Input 1

5 2

Sample Output 1

7

There are seven possible pairs: (2,3),(5,3),(2,4),(3,4),(2,5),(3,5) and (4,5).

Sample Input 2

10 0

Sample Output 2

100

Sample Input 3

31415 9265

Sample Output 3

287927211


var
	N,i,A,K,T,S:Longint;
begin
	read(N);
	for i:=1 to N do begin
		read(A,K);
		while A mod K<>0 do begin
			T:=A div K+1;
			dec(A,(A mod K+T-1)div T*T);
		end;
		S:=S xor A div K;
	end;
	if S<>0 then writeln('Takahashi')else writeln('Aoki');
end.

Score : 900 points

Problem Statement

Takahashi and Aoki are playing a stone-taking game. Initially, there are N piles of stones, and the i-th pile contains A_i stones and has an associated integer K_i.

Starting from Takahashi, Takahashi and Aoki take alternate turns to perform the following operation:

Choose a pile. If the i-th pile is selected and there are X stones left in the pile, remove some number of stones between 1 and floor(X/K_i) (inclusive) from the pile.

The player who first becomes unable to perform the operation loses the game. Assuming that both players play optimally, determine the winner of the game.
Here, floor(x) represents the largest integer not greater than x.

Constraints

1 \leq N \leq 200

1 \leq A_i,K_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 K_1
:
A_N K_N

Output

If Takahashi will win, print Takahashi; if Aoki will win, print Aoki.

Sample Input 1

2
5 2
3 3

Sample Output 1

Aoki

Initially, from the first pile at most floor(5/2)=2 stones can be removed at a time, and from the second pile at most floor(3/3)=1 stone can be removed at a time.

If Takahashi first takes two stones from the first pile, from the first pile at most floor(3/2)=1 stone can now be removed at a time, and from the second pile at most floor(3/3)=1 stone can be removed at a time.

Then, if Aoki takes one stone from the second pile, from the first pile at most floor(3/2)=1 stone can be removed at a time, and from the second pile no more stones can be removed (since floor(2/3)=0).

Then, if Takahashi takes one stone from the first pile, from the first pile at most floor(2/2)=1 stone can now be removed at a time, and from the second pile no more stones can be removed.

Then, if Aoki takes one stone from the first pile, from the first pile at most floor(1/2)=0 stones can now be removed at a time, and from the second pile no more stones can be removed.

No more operation can be performed, thus Aoki wins. If Takahashi plays differently, Aoki can also win by play accordingly.

Sample Input 2

3
3 2
4 3
5 1

Sample Output 2

Takahashi

Sample Input 3

3
28 3
16 4
19 2

Sample Output 3

Aoki

Sample Input 4

4
3141 59
26535 897
93 23
8462 64

Sample Output 4

Takahashi


var
    n,cnt_m,cnt_a,cnt_r,cnt_c,cnt_h,ans,i: Int64;
    s: String;
begin
    readln(n);
    for i := 1 to n do
    begin
        readln(s);
        if s[1]='M' then
        begin
            inc(cnt_m);
        end
        else if s[1]='A' then
        begin
            inc(cnt_a);
        end
        else if s[1]='R' then
        begin
            inc(cnt_r);
        end
        else if s[1]='C' then
        begin
            inc(cnt_c);
        end
        else if s[1]='H' then
        begin
            inc(cnt_h);
        end;
    end;
    ans:=cnt_m*cnt_a*(cnt_r+cnt_c+cnt_h)+cnt_m*cnt_r*(cnt_c+cnt_h)+
    cnt_m*cnt_c*cnt_h+cnt_a*cnt_r*(cnt_c+cnt_h)+cnt_a*cnt_c*cnt_h+
    cnt_r*cnt_c*cnt_h;
    writeln(ans);
end.

Score : 300 points

Problem Statement

There are N people. The name of the i-th person is S_i.

We would like to choose three people so that the following conditions are met:

The name of every chosen person begins with M, A, R, C or H.

There are no multiple people whose names begin with the same letter.

How many such ways are there to choose three people, disregarding order?

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

Constraints

1 \leq N \leq 10^5

S_i consists of uppercase English letters.

1 \leq |S_i| \leq 10

S_i \neq S_j (i \neq j)

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If there are x ways to choose three people so that the given conditions are met, print x.

Sample Input 1

5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI

Sample Output 1

2

We can choose three people with the following names:

MASHIKE, RUMOI, HABORO

MASHIKE, RUMOI, HOROKANAI

Thus, we have two ways.

Sample Input 2

4
ZZ
ZZZ
Z
ZZZZZZZZZZ

Sample Output 2

0

Note that there may be no ways to choose three people so that the given conditions are met.

Sample Input 3

5
CHOKUDAI
RNG
MAKOTO
AOKI
RINGO

Sample Output 3

7


var
    h,w,d,q,i,j,k,tmp,ans_cnt: Longint;
    a:array[1..300,1..300] of Longint;
    index:array[1..90000,1..2] of Longint;
    l,r: Longint;
    ans:array[1..100000,1..3] of Longint;
    flag:Boolean;
begin
    readln(h,w,d);
    for i := 1 to h do
    begin
        for j := 1 to w do
        begin
            read(a[i][j]);
            index[a[i][j]][1]:=i;
            index[a[i][j]][2]:=j;
        end;
    end;
    read(q);
    for i := 1 to q do
    begin
        readln(l,r);
        flag:=false;
        for j := 1 to ans_cnt do
        begin
            if (ans[j][1]=l) and (ans[j][2]=r) then
            begin
                writeln(ans[j][3]);
                flag:=true;
                break;
            end;
        end;
        if flag then
        begin
            continue;
        end;
        tmp:=0;
        k:=l;
        for j := 1 to ans_cnt do
        begin
            if (ans[j][1]=l)and (ans[j][2]<r) then
            begin
                tmp:=ans[j][3];
                k:=ans[j][2];
            end;
        end;
        while k<>r do
        begin
            j:=k;
            inc(k,d);
            inc(tmp,abs(index[k][1]-index[j][1])+abs(index[k][2]-index[j][2]));
        end;
        inc(ans_cnt);
        ans[ans_cnt][1]:=l;
        ans[ans_cnt][2]:=r;
        ans[ans_cnt][3]:=tmp;
        writeln(tmp);
    end;
end.

Score : 400 points

Problem Statement

We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).

The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.

You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.

You now have to take Q practical tests of your ability as a magical girl.

The i-th test will be conducted as follows:

Initially, a piece is placed on the square where the integer L_i is written.

Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.

Here, it is guaranteed that R_i-L_i is a multiple of D.

For each test, find the sum of magic points consumed during that test.

Constraints

1 \leq H,W \leq 300

1 \leq D \leq H×W

1 \leq A_{i,j} \leq H×W

A_{i,j} \neq A_{x,y} ((i,j) \neq (x,y))

1 \leq Q \leq 10^5

1 \leq L_i \leq R_i \leq H×W

(R_i-L_i) is a multiple of D.

Input

Input is given from Standard Input in the following format:

H W D
A_{1,1} A_{1,2} ... A_{1,W}
:
A_{H,1} A_{H,2} ... A_{H,W}
Q
L_1 R_1
:
L_Q R_Q

Output

For each test, print the sum of magic points consumed during that test.

Output should be in the order the tests are conducted.

Sample Input 1

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

Sample Output 1

5

4 is written in Square (1,2).

6 is written in Square (3,3).

8 is written in Square (3,1).

Thus, the sum of magic points consumed during the first test is (|3-1|+|3-2|)+(|3-3|+|1-3|)=5.

Sample Input 2

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

Sample Output 2

0
0

Note that there may be a test where the piece is not moved at all, and there may be multiple identical tests.

Sample Input 3

5 5 4
13 25 7 15 17
16 22 20 2 9
14 11 12 1 19
10 6 23 8 18
3 21 5 24 4
3
13 13
2 10
13 13

Sample Output 3

0
5
0


var
 max,i,j,n,m,a,xx:longint;
 s:ansistring;
begin
 read(s);
 m:=0; max:=-1;
 val(s,a,xx);
 for j:=1 to a do
  begin
   str(j,s);
   n:=length(s);
   for i:=1 to n do
    m:=m+ord(s[i])-48;
   if m>max then max:=m;
   m:=0;
  end;
 write(max);
end.

Score : 300 points

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Constraints

1\leq N \leq 10^{16}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Sample Input 1

100

Sample Output 1

18

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.

Sample Input 2

9995

Sample Output 2

35

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.

Sample Input 3

3141592653589793

Sample Output 3

137


var
n,k,z,c,p,i:int64;
function pw(x,y:int64):int64;
var re:int64;
begin
    re:=1;
	while y>0 do
	begin
		if (y and 1=1) then begin re:=(re*x) mod 998244353; end;
		x:=x*x mod 998244353;
		y:=y div 2;
	end;
	pw:=re;
end;
begin
	n:=1; k:=1; z:=1; c:=1;
	p:=998244353;
	readln(k,n);
	z:=pw(2,n-1);
	while i<k do
	begin
		if ((k-i) mod 2=0) then
		begin
			z:=(z+p-c) mod p;
		end;
		c:=c*(n-i)mod p * pw(i+1,p-2) mod p;
                inc(i);
	end;
	writeln(z);
end.

Score : 1200 points

Problem Statement

In Republic of AtCoder, Snuke Chameleons (Family: Chamaeleonidae, Genus: Bartaberia) are very popular pets.
Ringo keeps N Snuke Chameleons in a cage.

A Snuke Chameleon that has not eaten anything is blue. It changes its color according to the following rules:

A Snuke Chameleon that is blue will change its color to red when the number of red balls it has eaten becomes strictly larger than the number of blue balls it has eaten.

A Snuke Chameleon that is red will change its color to blue when the number of blue balls it has eaten becomes strictly larger than the number of red balls it has eaten.

Initially, every Snuke Chameleon had not eaten anything. Ringo fed them by repeating the following process K times:

Grab either a red ball or a blue ball.

Throw that ball into the cage. Then, one of the chameleons eats it.

After Ringo threw in K balls, all the chameleons were red. We are interested in the possible ways Ringo could have thrown in K balls. How many such ways are there? Find the count modulo 998244353. Here, two ways to throw in balls are considered different when there exists i such that the color of the ball that are thrown in the i-th throw is different.

Constraints

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

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the possible ways Ringo could have thrown in K balls, modulo 998244353.

Sample Input 1

2 4

Sample Output 1

7

We will use R to represent a red ball, and B to represent a blue ball. There are seven ways to throw in balls that satisfy the condition: BRRR, RBRB, RBRR, RRBB, RRBR, RRRB and RRRR.

Sample Input 2

3 7

Sample Output 2

57

Sample Input 3

8 3

Sample Output 3

0

Sample Input 4

8 10

Sample Output 4

46

Sample Input 5

123456 234567

Sample Output 5

857617983