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


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

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

Score : 400 points

Problem Statement

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

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

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 10^5

2 \leq M \leq 10^9

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

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

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

Sample Input 1

3 2
4 1 5

Sample Output 1

3

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

Sum for (1, 1): 4

Sum for (1, 2): 5

Sum for (1, 3): 10

Sum for (2, 2): 1

Sum for (2, 3): 6

Sum for (3, 3): 5

Among these, three are multiples of 2.

Sample Input 2

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

Sample Output 2

6

Sample Input 3

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

Sample Output 3

25

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.


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


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


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


var n,m,i,j:longint;
a:array[1..100000]of qword;
t,count:int64;
begin
  read(n,m);
  count:=0;
  for i:=1 to n do read(a[i]);
  for i:=1 to n do
  begin
    t:=a[i];
	if t mod m=0 then inc(count);
    for j:=i+1 to n do
    begin
      t:=t+a[j];
      if t mod m=0 then inc(count);
    end;
  end;
  write(count);
end.


CONST   fi='';
        fo='';
VAR
    f:text;
    a,s:array[0..100001] of int64;
    n,m:longint;
    ds,d:int64;
PROCEDURE input;
  var i:longint;
  begin
    Assign(f,fi);reset(f);
      readln(f,n,m);
      for i:=1 to n do read(f,a[i]);
    Close(f);
  end;

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

PROCEDURE main;
  var i:longint;
  begin
    s[0]:=0;
    for i:=1 to n do s[i]:=s[i-1]+a[i];
    for i:=1 to n do s[i]:=s[i] mod m;
    sort(1,n);
    d:=0;
    s[n+1]:=m+1;
    for i:=1 to n+1 do
      if s[i]<>s[i-1] then begin ds:=ds+(i-d)*(i-d-1) div 2; d:=i; end;
  end;

PROCEDURE output;
  begin
    Assign(f,fo);rewrite(f);
      writeln(f,ds);
    Close(f);
  end;

BEGIN
   input; main; output;
END.


var s:string;
n,i:longint;
f:boolean;
begin
	readln(s);
	n:=length(s);
	if s[1]<>'A' then begin write('WA');exit;end;
	if  (s[2]<'a') or  (s[n]<'a')  then begin write('WA');exit;end;
	f:=true;
	for i:=3 to n-1 do
	if (s[i]='C') and (f=false) or (s[i]<'a') and (s[i]<>'C') then begin write('WA');exit;end
	else if s[i]='C' then f:=false;
	if f=true then begin write('WA');exit;end;
	write('AC');
end.

Score : 200 points

Problem Statement

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

The initial character of S is an uppercase A.

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

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

Constraints

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

Each character of S is uppercase or lowercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

AtCoder

Sample Output 1

AC

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

Sample Input 2

ACoder

Sample Output 2

WA

The second letter should not be C.

Sample Input 3

AcycliC

Sample Output 3

WA

The last letter should not be C, either.

Sample Input 4

AtCoCo

Sample Output 4

WA

There should not be two or more occurrences of C.

Sample Input 5

Atcoder

Sample Output 5

WA

The number of C should not be zero, either.


var
f:array[0..1000]of longint;
n,t,i,j,k,x1,x2,s,min:longint;
begin
read(n,t);
for i:=0 to 1000 do
f[i]:=0;
s:=0;
min:=maxlongint;
for i:=1 to n do
begin
read(x1,x2);
inc(s,x1);
 for j:=s downto x1 do
 begin
  if f[j-x1]+100*i*x1+x2>f[j] then
  f[j]:=f[j-x1]+100*i*x1+x2;
  if (f[j]>=t) and (j<min) then
  min:=j;
 end;
 for j:=x1-1 downto 1 do
  for k:=x1-1 downto 1 do
  begin
   if f[k-1]+100*i>f[k] then
   f[k]:=f[k-1]+100*i;
   if (f[k]>=t) and (k<min) then
   min:=k;
  end;
end;
writeln(min);
end.

Score : 300 points

Problem Statement

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

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

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

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

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

Constraints

1 ≤ D ≤ 10

1 ≤ p_i ≤ 100

100 ≤ c_i ≤ 10^6

100 ≤ G

All values in input are integers.

c_i and G are all multiples of 100.

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

Input

Input is given from Standard Input in the following format:

D G
p_1 c_1
:
p_D c_D

Output

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

Sample Input 1

2 700
3 500
5 800

Sample Output 1

3

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

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

Sample Input 2

2 2000
3 500
5 800

Sample Output 2

7

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

Sample Input 3

2 400
3 500
5 800

Sample Output 3

2

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

Sample Input 4

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

Sample Output 4

66

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


var
	n,g,a,b,i,j,k,l,s,max:longint;
	f,t:array[0..1000010] of longint;
begin
	readln(n,g);
	g:=g div 100;
	fillchar(f,sizeof(f),$7f);
	f[0]:=0;
	for i:=1 to n do
	begin
		read(a,b);
		t:=f;
		for j:=a downto 0 do
		begin
			s:=i*j;
			if j=a then s:=s+b div 100;
			for k:=g downto 0 do
			begin
				if k+s>g then l:=g else l:=k+s;
				if f[l]>f[k]+j then t[l]:=t[k]+j;
			end;
		end;
		f:=t;
	end;
	max:=0;
	for i:=1 to g do
		if (f[i]>max) and (f[i]<>$7f) then max:=f[i];
	writeln(max);
end.


var
	c,w,f,p:array[0..100000]of longint;
	a:array[0..1000,0..100010]of longint;
	g,v,x,y,d,n,i,j,k,t:longint;
begin
	readln(d,g);
	v:=g div 100;
	n:=0;
	fillchar(a,sizeof(a),0);
	fillchar(f,sizeof(f),0);
	for i:=1 to d do
    begin
		readln(x,y);
		y:=y div 100;
		for j:=1 to x do
	    begin
			inc(n);
			if j<>x then w[n]:=i*j else w[n]:=i*j+y;
			c[n]:=j; p[n]:=i;
		end;
	end;
	for i:=1 to n do
    begin
		inc(a[p[i],0]);
		a[p[i],a[p[i],0]]:=i;
	end;
	for i:=1 to n do
		for j:=v downto 0 do
			for k:=1 to a[i,0] do
				if j>=c[a[i,k]] then
					if f[j]<f[j-c[a[i,k]]]+w[a[i,k]] then
						f[j]:=f[j-c[a[i,k]]]+w[a[i,k]]; //分组背包
	for i:=1 to n do
		if f[i]>=v then break;
	writeln(i);
end.


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


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


var
  num,b,d,n,i,j,k,t:word;
  c,w,p,f:array[0..100000]of word;
  a:array[0..1000,0..100010]of word;
  inp,m,v:longint;
begin
  readln(d,inp);
  m:=inp div 100;
  v:=m;
  t:=d;
  for i:=1 to d do
    begin
	  readln(num,inp);
	  b:=inp div 100;
	  for j:=1 to num do
	    begin
		  inc(n);
		  if j<>num then w[n]:=i*j else w[n]:=i*j+b;
		  c[n]:=j;
	      p[n]:=i;
		end;
	end;
  for i:=1 to n do
    begin
	  inc(a[p[i],0]);
	  a[p[i],a[p[i],0]]:=i;
	end;
  for i:=1 to t do
    for j:=v downto 0 do
	  for k:=1 to a[i,0] do
	    if j>=c[a[i,k]] then
		  if f[j]<f[j-c[a[i,k]]]+w[a[i,k]] then
		    f[j]:=f[j-c[a[i,k]]]+w[a[i,k]];
  for i:=1 to n do
    if f[i]>=m then break;
  write(i);
end.


uses
  SysUtils,
  Classes;

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

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

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

Score : 200 points

Problem Statement

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

Determine if S equals T after rotation.

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

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

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

Constraints

2 \leq |S| \leq 100

|S| = |T|

S and T consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

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

Sample Input 1

kyoto
tokyo

Sample Output 1

Yes

In the first operation, kyoto becomes okyot.

In the second operation, okyot becomes tokyo.

Sample Input 2

abc
arc

Sample Output 2

No

abc does not equal arc after any number of operations.

Sample Input 3

aaaaaaaaaaaaaaab
aaaaaaaaaaaaaaab

Sample Output 3

Yes


var
N,i,F: integer;
a: array [1..100000] of integer;
BEGIN
F:= 0;
read(N);
for i:=1 to N do
begin
read(a[i]);
end;
for i:=1 to N do
begin
F:=F+(a[i]-1);
end;
writeln(F);
END.

Score : 300 points

Problem Statement

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

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

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

Find the maximum value of f.

Constraints

All values in input are integers.

2 \leq N \leq 3000

2 \leq a_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the maximum value of f.

Sample Input 1

3
3 4 6

Sample Output 1

10

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

Sample Input 2

5
7 46 11 20 11

Sample Output 2

90

Sample Input 3

7
994 518 941 851 647 2 581

Sample Output 3

4527


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 メイン : ここにコードを記述してください }
    Readln(a);
    sum:=0;
    for I := 0 to a - 1 do begin
      Read(b);
      sum:=sum+b;
    end;

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


var
  n: integer;
  a: integer;
  i: integer;
  sum: integer;
begin
  read(n);
  sum := 0;
  for i := 0 to n - 1 do begin
    read(a);
    sum := sum + a - 1;
  end;
  writeln(sum);
end.


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

Score : 600 points

Problem Statement

Ringo Mart, a convenience store, sells apple juice.

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

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

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

Constraints

1 \leq T \leq 300

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

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

Output

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

Sample Input 1

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

Sample Output 1

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

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

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

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

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

and so on, thus Snuke can buy juice indefinitely.

Sample Input 2

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

Sample Output 2

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


var i,x:Longint;
s:String;
begin
readln(s);
read(x);
i:=1;
while i<=length(s)do begin
write(s[i]);inc(i,x);
end;
writeln;
end.

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters.
We will write down this string, starting a new line after every w letters. Print the string obtained by concatenating the letters at the beginnings of these lines from top to bottom.

Constraints

1 \leq w \leq |S| \leq 1000

S consists of lowercase English letters.

w is an integer.

Input

Input is given from Standard Input in the following format:

S
w

Output

Print the desired string in one line.

Sample Input 1

abcdefgh
3

Sample Output 1

adg

When we write down abcdefgh, starting a new line after every three letters, we get the following:

abc

def

gh

Concatenating the letters at the beginnings of these lines, we obtain adg.

Sample Input 2

lllll
1

Sample Output 2

lllll

Sample Input 3

souuundhound
2

Sample Output 3

suudon


var
    n,min,max,i,x:longint;
begin
    readln(n);
    max:=0;min:=maxlongint;
    for i:=1 to n do
        begin
            read(x);
            if x<min then min:=x;
            if x>max then max:=x;
        end;
    writeln(max-min);
end.

Score : 200 points

Problem Statement

You are given an integer sequence A of length N.
Find the maximum absolute difference of two elements (with different indices) in A.

Constraints

2 \leq N \leq 100

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

Print the maximum absolute difference of two elements (with different indices) in A.

Sample Input 1

4
1 4 6 3

Sample Output 1

5

The maximum absolute difference of two elements is A_3-A_1=6-1=5.

Sample Input 2

2
1000000000 1

Sample Output 2

999999999

Sample Input 3

5
1 1 1 1 1

Sample Output 3

0


uses
  SysUtils,
  Classes;

var
	a, i, j, t : Integer;
	atype : array of Integer;
	str : String;
	strlist : TStringList;
begin
	readln(a);
	setLength(atype, a);

	readln(str);
	strlist := TStringList.Create;
	try
		strlist.Delimiter := ' ';
		strlist.DelimitedText := str;
		for i := 0 to a - 1 do
			atype[i] := StrToInt(strlist.Strings[i]);
	finally
		strlist.free;
		end;

	for i := 0 to a - 1 do
		begin
		for j := a - 1 downto i + 1 do
			begin
				if atype[j - 1] > atype[j] then
					begin
					t := atype[j - 1];
					atype[j - 1] := atype[j];
					atype[j] := t;
					end;
			end;
		end;
		t := atype[a -1] - atype[0];
		writeLn(t);
end.


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

Score : 300 points

Problem Statement

Snuke has an integer sequence A of length N.

He will freely choose an integer b.
Here, he will get sad if A_i and b+i are far from each other.
More specifically, the sadness of Snuke is calculated as follows:

abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))

Here, abs(x) is a function that returns the absolute value of x.

Find the minimum possible sadness of Snuke.

Constraints

1 \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

Print the minimum possible sadness of Snuke.

Sample Input 1

5
2 2 3 5 5

Sample Output 1

2

If we choose b=0, the sadness of Snuke would be abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2.
Any choice of b does not make the sadness of Snuke less than 2, so the answer is 2.

Sample Input 2

9
1 2 3 4 5 6 7 8 9

Sample Output 2

0

Sample Input 3

6
6 5 4 3 2 1

Sample Output 3

18

Sample Input 4

7
1 1 1 1 2 3 4

Sample Output 4

6


var
	N,L,R,M,i,cnt:Longint;
	A:Array[1..200000]of Longint;
	ans:int64;
begin
	read(N);
	for i:=1 to N do begin
		read(A[i]);
		dec(A[i],i);
	end;
	L:=-1000000;
	R:=1000000000;
	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*2>=N then R:=M else L:=M;
	end;
	for i:=1 to N do inc(ans,abs(A[i]-R));
	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 2=0 then s:=(i-1) div 2 else s:=i div 2;
                if (n-i) mod 2=0 then s:=s+(n-i) div 2 else s:=s+(n-i) div 2+1;
                if s<ans then ans:=s;
            end;
    writeln(ans);
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,a,t,s:longint;
begin
    readln(n,k);
    min:=maxlongint;
    for i:=1 to n do
        begin
            read(a);
            if a<min then
                begin
                    min:=a;
                    t:=i;
                end;
        end;
    if (t-1) mod 2=0 then s:=(t-1) div 2 else s:=t div 2;
    if (n-t) mod 2=0 then s:=s+(n-t) div 2 else s:=s+(n-t) div 2+1;
    writeln(s);
end.


Score: 300 points

Problem Statement

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

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

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

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

At most how many operations can be performed?

Constraints

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

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

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print the maximum number of operations that Snuke can perform.

Sample Input 1

3
5 2 4

Sample Output 1

3

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

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

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

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

Sample Input 2

4
631 577 243 199

Sample Output 2

0

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

Sample Input 3

10
2184 2126 1721 1800 1024 2528 3360 1945 1280 1776

Sample Output 3

39


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

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


var n,m,i,j:longint;x,y,z:int64;
    a:array[-1..1000]of int64;
    ans:int64;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    readln(x,y,z);
    a[i]:=x+y+z;
  end;
  for i:=1 to n-1 do
    for j:=i+1 to n do
      if a[i]<a[j] then
      begin
        x:=a[i];a[i]:=a[j];a[j]:=x;
      end;
  for i:=1 to m do
    inc(ans,a[i]);
  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.


  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.

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


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

begin

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


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

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

 begin

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

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

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

  end.

Score : 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
  a,b:array[0..1000005]of longint;
  n,m,k,len1,len2,i,j,z1,z2,y:longint;
  s1,s2:ansistring;
  t1,t2:boolean;
function max(x,y:longint):longint;
begin
  if x>y then
    exit(x);
  exit(y);
end;
begin
  readln(len1,len2,k);
  readln(s1);
  readln(s2);
  for i:=1 to len1 do
    a[i]:=ord(s1[len1-i+1])-48;
  for i:=1 to len2 do
    b[i]:=ord(s2[len2-i+1])-48;
  i:=1;
  while (i<=len1) or (i<=len2) do
  begin
    if (a[i]=1) and (b[i]=1) then
    begin
      dec(k);
      if k<0 then
        break;
      t1:=true;
      t2:=true;
      z1:=0;
      z2:=0;
      j:=i;
      while a[j]=1 do
      begin
        a[j]:=0;
        inc(j);
      end;
      a[j]:=1;
      if j>len1 then
        len1:=j;
      y:=j;
      j:=i;
      while b[j]=1 do
      begin
        b[j]:=0;
        inc(j);
      end;
        b[j]:=1;
      if j>len2 then
        len2:=j;
      i:=max(i+1,max(y,j));
    end else
      inc(i);
  end;
  for i:=len1 downto 1 do
    write(a[i]);
  writeln;
  for i:=len2 downto 1 do
    write(b[i]);
  writeln;
end.

Score : 2400 points

Problem Statement

Takahashi and Aoki love calculating things, so they will play with numbers now.

First, they came up with one positive integer each. Takahashi came up with X, and Aoki came up with Y.
Then, they will enjoy themselves by repeating the following operation K times:

Compute the bitwise AND of the number currently kept by Takahashi and the number currently kept by Aoki. Let Z be the result.

Then, add Z to both of the numbers kept by Takahashi and Aoki.

However, it turns out that even for the two math maniacs this is just too much work.
Could you find the number that would be kept by Takahashi and the one that would be kept by Aoki eventually?

Note that input and output are done in binary.
Especially, X and Y are given as strings S and T of length N and M consisting of 0 and 1, respectively, whose initial characters are guaranteed to be 1.

Constraints

1 ≤ K ≤ 10^6

1 ≤ N,M ≤ 10^6

The initial characters of S and T are 1.

Input

Input is given from Standard Input in the following format:

N M K
S
T

Output

In the first line, print the number that would be kept by Takahashi eventually; in the second line, print the number that would be kept by Aoki eventually.
Those numbers should be represented in binary and printed as strings consisting of 0 and 1 that begin with 1.

Sample Input 1

2 3 3
11
101

Sample Output 1

10000
10010

The values of X and Y after each operation are as follows:

After the first operation: (X,Y)=(4,6).

After the second operation: (X,Y)=(8,10).

After the third operation: (X,Y)=(16,18).

Sample Input 2

5 8 3
10101
10101001

Sample Output 2

100000
10110100

Sample Input 3

10 10 10
1100110011
1011001101

Sample Output 3

10000100000010001000
10000100000000100010


var
  n,d,sln:int64;
  i:longint;
  j:char;
  s:ansistring;
BEGIN

  readln(n);
  read(s);

  sln:=low(int64);
  for i:=1 to n do
    begin
      d:=0;
      for j:='a' to 'z' do
        if (pos(j,copy(s,1,i))>0) and (pos(j,copy(s,i+1,n-i))>0) then
          d:=d+1;
      if sln<d then sln:=d;
    end;

  write(sln);

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


program Project1;

{$APPTYPE CONSOLE}

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

    for i:=1 to n do begin //区切り位置変更

      for k:=1 to 26 do begin
        tfl:=false;
        tfr:=false;
        for j:=1 to i do begin //左
            if al[k]=s[j]then
              tfl:=true;
        end;
        for j:=i+1 to n do begin //右
            if al[k]=s[j]then
              tfr:=true;
        end;
        if tfl and tfr then
          inc(count);
      end;

      max:=(count+max+abs(count-max))div 2;
      count:=0;
    end;

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


var
	N,i,ans,now:Longint;
	S:String;
	L,R:Array[0..300001]of Longint;
begin
	readln(N);
	readln(S);
	for i:=1 to N do begin
		L[i]:=L[i-1];
		if S[i]='W' then inc(L[i]);
	end;
	for i:=N downto 1 do begin
		R[i]:=R[i+1];
		if S[i]='E' then inc(R[i]);
	end;
	ans:=N;
	for i:=1 to N do begin
		now:=L[i-1]+R[i+1];
		if ans>now then ans:=now;
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

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

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

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

Constraints

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

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

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

Sample Input 1

5
WEEWW

Sample Output 1

1

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

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3


var
 a,b,c,i,j,k:longint;
 f:array[0..500,0..500,0..500]of longint;
begin
 readln(a,b,c);
 f[0,1,0]:=1;
 for i:=0 to a do
  for j:=1 to b do
   for k:=i downto 0 do
    if f[i,j,k]<>0 then
     begin
      if k<>0 then
       begin
        f[i,j,k]:=f[i,j,k] mod c;
        f[i,j,k-1]:=f[i,j,k-1]+f[i,j,k];
       end
      else
       f[i,j+1,i]:=(f[i,j+1,i]+f[i,j,k])mod c;
      f[i+1,j,k]:=(f[i+1,j,k]+(k+1)*f[i,j,k])mod c;
     end;
 writeln(f[a,b+1,a]);
end.

Score : 1200 points

Problem Statement

Find the number of the possible tuples of sequences (A_0,A_1,...,A_N) that satisfy all of the following conditions, modulo M:

For every i (0\leq i\leq N), A_i is a sequence of length i consisting of integers between 1 and K (inclusive);

For every i (1\leq i\leq N), A_{i-1} is a subsequence of A_i, that is, there exists 1\leq x_i\leq i such that the removal of the x_i-th element of A_i would result in a sequence equal to A_{i-1};

For every i (1\leq i\leq N), A_i is lexicographically larger than A_{i-1}.

Constraints

1 \leq N,K \leq 300

2 \leq M \leq 10^9

N, K and M are integers.

Input

Input is given from Standard Input in the following format:

N K M

Output

Print the number of the possible tuples of sequences (A_0,A_1,...,A_N), modulo M.

Sample Input 1

2 2 100

Sample Output 1

5

Five tuples below satisfy the conditions:

(),(1),(1,1)

(),(1),(1,2)

(),(1),(2,1)

(),(2),(2,1)

(),(2),(2,2)

Sample Input 2

4 3 999999999

Sample Output 2

358

Sample Input 3

150 150 998244353

Sample Output 3

186248260


var
 a,b,c,i,j,k:longint;
 f:array[0..500,0..500,0..500]of longint;
begin
 readln(a,b,c);
 f[0,1,0]:=1;
 for i:=1 to a do
  for j:=1 to b do
   for k:=i downto 0 do
    if f[i,j,k]<>0 then
     begin
      if k<>0 then
       begin
        f[i,j,k]:=f[i,j,k] mod c;
        f[i,j,k-1]:=f[i,j,k-1]+f[i,j,k];
       end
      else
       f[i,j+1,i]:=(f[i,j+1,i]+f[i,j,k])mod c;
      f[i+1,j,k]:=(f[i+1,j,k]+(k+1)*f[i,j,k])mod c;
     end;
 writeln(f[a,b+1,a]);
end.


{$mode objfpc}
program C;
uses
  classes, math;

var
  S: AnsiString;
  k: Integer;
  i, j: Integer;
  AllSubStr: TStringList;

begin
  ReadLn(S);
  ReadLn(k);

  AllSubStr := TStringList.Create;

  for i := 1 to Length(S) do
    for j := 1 to Min(k, Length(S) - i + 1) 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 n,s,flag,i,left,right:longint;
  tot:int64;
   x,p:array[1..100000]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 n,s,max,i,left,right:longint;
  count:int64;
   x,p:array[1..100000]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
    count:=x[n]-s;
    write(count);
    exit;
    end;
  if s>x[n] then
    begin
    count:=s-x[1];
    write(count);
    exit;
    end;
    left:=1;
    right:=n;
    if p[1]<p[n] then max:=2
    else max:=1;
    if p[1]<p[n] then begin p[n]:=p[n]+p[1]; count:=count+x[right]-x[left]; inc(left);  end
                  else begin p[1]:=p[1]+p[n]; count:=count+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 max=2 then count:=count+x[right]-x[left];
     dec(right);
     if max=2 then max:=1;
   end
   else begin
     p[right]:=p[right]+p[left];
    if max=1 then count:=count+x[right]-x[left];
     inc(left);
     if max=1 then max:=2;
       end;
    end;
  if x[left]>s then write(count+x[right]-s)
               else write(count+s-x[left])
end.


var n,s,max,i,left,right:longint;
  count:int64;
   x,p:array[1..100000]of longint;
begin
 readln(n,s);
 for i:=1 to n do
   begin
   readln(x[i],p[i]);
  end;
  if s<=x[1] then
    begin
    count:=x[n]-s;
    write(count);
    exit;
    end;
  if s>x[n] then
    begin
    count:=s-x[1];
    write(count);
    exit;
    end;
    left:=1;
    right:=n;
    if p[1]<p[n] then max:=2
    else max:=1;
    if p[1]<p[n] then begin p[n]:=p[n]+p[1]; count:=count+x[right]-x[left]; inc(left);  end
                  else begin p[1]:=p[1]+p[n]; count:=count+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 max=2 then count:=count+x[right]-x[left];
     dec(right);
     if max=2 then max:=1;
   end
   else begin
     p[right]:=p[right]+p[left];
    if max=1 then count:=count+x[right]-x[left];
     inc(left);
     if max=1 then max:=2;
       end;
    end;
  if x[left]>s then write(count+x[right]-s)
               else write(count+s-x[left])
end.


var n,s,max,i,left,right:longint;
  count:int64;
   x,p:array[1..100000]of longint;
begin
 readln(n,s);
 for i:=1 to n do
   begin
   readln(x[i],p[i]);
  end;
  if s<=x[1] then
    begin
    count:=x[n]-s;
    write(count);
    exit;
    end;
  if s>x[n] then
    begin
    count:=s-x[1];
    write(count);
    exit;
    end;
    left:=1;
    right:=n;
    if p[1]<p[n] then max:=2
    else max:=1;
    if p[1]<=p[n] then begin p[n]:=p[n]+p[1]; count:=count+x[right]-x[left]; inc(left);  end
                  else begin p[1]:=p[1]+p[n]; count:=count+x[right]-x[left];dec(right); end;
   repeat
  if p[left]>=p[right] then
   begin
     p[left]:=p[left]+p[right];
     if max=2 then count:=count+x[right]-x[left];
     dec(right);
     if max=2 then max:=1;
   end
   else begin
     p[right]:=p[right]+p[left];
    if max=1 then count:=count+x[right]-x[left];
     inc(left);
     if max=1 then max:=2;
       end;
   until(x[left]>s)or(x[right]<s);
  if x[left]>s then write(count+x[right]-s);
  if x[right]<s then write(count+s-x[left])
end.


var
  i,j,flag:longint;
  n,s,left,right,tot:qword;
  p,x:array[1..100000]of qword;
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,m,a,s,i:Longint;
begin
	s:=round(1e9);
	read(n,m);
	for i:=1 to n do
	begin
		read(a);
		m:=m-a;
		if s>a then s:=a;
	end;
	write(n+m/s:0:0)
end.

Score : 200 points

Problem Statement

Akaki, a patissier, can make N kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These doughnuts are called Doughnut 1, Doughnut 2, ..., Doughnut N. In order to make one Doughnut i (1 ≤ i ≤ N), she needs to consume m_i grams of Moto. She cannot make a non-integer number of doughnuts, such as 0.5 doughnuts.

Now, she has X grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition:

For each of the N kinds of doughnuts, make at least one doughnut of that kind.

At most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto. Also, under the constraints of this problem, it is always possible to obey the condition.

Constraints

2 ≤ N ≤ 100

1 ≤ m_i ≤ 1000

m_1 + m_2 + ... + m_N ≤ X ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
m_1
m_2
:
m_N

Output

Print the maximum number of doughnuts that can be made under the condition.

Sample Input 1

3 1000
120
100
140

Sample Output 1

9

She has 1000 grams of Moto and can make three kinds of doughnuts. If she makes one doughnut for each of the three kinds, she consumes 120 + 100 + 140 = 360 grams of Moto. From the 640 grams of Moto that remains here, she can make additional six Doughnuts 2. This is how she can made a total of nine doughnuts, which is the maximum.

Sample Input 2

4 360
90
90
90
90

Sample Output 2

4

Making one doughnut for each of the four kinds consumes all of her Moto.

Sample Input 3

5 3000
150
130
150
130
110

Sample Output 3

26


Uses math;
var
	ans,L,R,Lmax,Rmax,c:int64;
	n,i:Longint;
	x,v:array[1..100000]of Longint;
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,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);
	mb:=f(n div 2-1);
	for i:=1 to n do if a[i]<ma then writeln(ma)else writeln(mb);
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
	n,i,m,d:Longint;
	a:array[1..100000]of Longint;
begin
	read(n);
	for i:=1 to n do begin
		read(a[i]);
		if m<a[i] then m:=a[i];
	end;
	d:=-1;
	for i:=1 to n do begin
		if a[i]=m then continue;
		if abs(m-2*a[i])<abs(m-2*d)then d:=a[i];
	end;
	writeln(m,' ',d);
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
  t,x:array[0..300005]of int64;
  n,i:longint;
  l,z,c0,c1,c2:int64;
  f,g:boolean;
begin
  readln(n,l);
  for i:=1 to n do
    read(x[i]);
  readln;
  z:=1;
  for i:=1 to n do
  begin
    read(t[i]);
    z:=z+t[i] div (2*l);
    t[i]:=t[i] mod (2*l);
    if t[i]>0 then
    begin
      inc(z);
      f:=false;
      g:=false;
      if t[i]<=(l-x[i])*2 then
        f:=true;
      if t[i]<=x[i]*2 then
        g:=true;
      if i=n then
      begin
        if f then
          dec(z);
        break;
      end;
      if g then
      begin
        inc(c0);
        if f then
          inc(c2);
      end else
      if f then
        inc(c1);
      while c0-c2<c1+c2 do
      begin
        if c2>0 then
          dec(c2) else
          dec(c1);
      end;
    end;
  end;
  z:=z-c1-c2;
  writeln(z*2*l);
end.

Score : 1600 points

Problem Statement

Yui loves shopping. She lives in Yamaboshi City and there is a train service in the city. The city can be modelled as a very long number line. Yui's house is at coordinate 0.

There are N shopping centres in the city, located at coordinates x_{1}, x_{2}, ..., x_{N} respectively. There are N + 2 train stations, one located at coordinate 0, one located at coordinate L, and one located at each shopping centre.

At time 0, the train departs from position 0 to the positive direction. The train travels at a constant speed of 1 unit per second. At time L, the train will reach the last station, the station at coordinate L. The train immediately moves in the opposite direction at the same speed. At time 2L, the train will reach the station at coordinate 0 and it immediately moves in the opposite direction again. The process repeats indefinitely.

When the train arrives at a station where Yui is located, Yui can board or leave the train immediately. At time 0, Yui is at the station at coordinate 0.

Yui wants to go shopping in all N shopping centres, in any order, and return home after she finishes her shopping. She needs to shop for t_{i} seconds in the shopping centre at coordinate x_{i}. She must finish her shopping in one shopping centre before moving to the next shopping centre. Yui can immediately start shopping when she reaches a station with a shopping centre and she can immediately board the train when she finishes shopping.

Yui wants to spend the minimum amount of time to finish her shopping. Can you help her determine the minimum number of seconds required to complete her shopping?

Constraints

1 \leq N \leq 300000

1 \leq L \leq 10^{9}

0 < x_{1} < x_{2} < ... < x_{N} < L

1 \leq t_{i} \leq 10^{9}

All values in the input are integers.

Partial Score

1000 points will be awarded for passing the testset satisfying 1 \leq N \leq 3000.

Input

Input is given from Standard Input in the following format:

N L
x_{1} x_{2} ... x_{N}
t_{1} t_{2} ... t_{N}

Output

Print the minimum time (in seconds) required for Yui to finish shopping at all N shopping centres and return home.

Sample Input 1

2 10
5 8
10 4

Sample Output 1

40

Here's one possible way for Yui to finish her shopping :

Travel to the station at coordinate 8 with the train. She arrives at coordinate 8 at time 8.

Shop in the shopping centre at coordinate 8. She finishes her shopping at time 12.

The train arrives at coordinate 8 at time 12. She boards the train which is currently moving in the negative direction.

She arrives at coordinate 5 at time 15. She finishes her shopping at time 25.

The train arrives at coordinate 5 at time 25. She boards the train which is currently moving in the positive direction.

She arrives at coordinate L = 10 at time 30. The train starts moving in the negative direction immediately.

She arrives at coordinate 0 at time 40, ending the trip.

Sample Input 2

2 10
5 8
10 5

Sample Output 2

60

Sample Input 3

5 100
10 19 28 47 68
200 200 200 200 200

Sample Output 3

1200

Sample Input 4

8 1000000000
2018 123456 1719128 1929183 9129198 10100101 77777777 120182018
99999999 1000000000 1000000000 11291341 1 200 1 123812831

Sample Output 4

14000000000

Beware of overflow issues.


var
  t,x:array[0..300005]of int64;
  n,i:longint;
  l,z,c0,c1,c2:int64;
  f,g:boolean;
begin
  readln(n,l);
  for i:=1 to n do
    read(x[i]);
  readln;
  z:=1;
  for i:=1 to n do
  begin
    read(t[i]);
    z:=z+t[i] div (2*l);
    t[i]:=t[i] mod (2*l);
    if t[i]>0 then
    begin
      inc(z);
      f:=false;
      g:=false;
      if t[i]<=(l-x[i])*2 then
        f:=true;
      if t[i]<=x[i]*2 then
        g:=true;
      if i=n then
      begin
        if f then
          dec(z);
        break;
      end;
      if g then
      begin
        inc(c0);
        if f then
          inc(c2);
      end else
        inc(c1);
      while c0-c2<c1+c2 do
      begin
        if c2>0 then
          dec(c2) else
          dec(c1);
      end;
    end;
  end;
  z:=z-c1-c2;
  writeln(z*2*l);
end.


Var a,f,g:Array[0 .. 1000000] of Longint;
    i,n:Longint;
Begin
    Readln(n); For i:=1 to n do read(a[i]); f[0]:=0; g[n+1]:=0;
    For i:=1 to n do f[i]:=f[i-1]+abs(a[i]-a[i-1]);
    For i:=n downto 1 do g[i]:=g[i+1]+abs(a[i+1]-a[i]);
    For i:=1 to n do writeln(f[i-1]+g[i+1]+abs(a[i+1]-a[i-1]));
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
    ans,n,i:longint;
    a:array[0..100010] of longint;
begin
    readln(n);
    a[0]:=0;a[n+1]:=0;
    for i:=1 to n do
        begin
            read(a[i]);
            ans:=ans+abs(a[i]-a[i-1]);
        end;
    ans:=ans+abs(a[n]);
    for i:=1 to n do
        writeln(ans-abs(a[i]-a[i-1])-abs(a[i]-a[i+1])+abs(a[i-1]-a[i+1]));
end.


var n:longint;
    i,k:longint;
    a,b,c,d:array[0..201000]of longint;
    m0,m1:longint;
    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]:=maxlongint;
  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.

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


Uses math;
var a,b,c:longint;
begin
	read(a,b);
	for a:=a to b do begin
		if (a mod 10 = floor(a/10000)) and (floor(a/10)mod 10 = floor(a/1000)mod 10) then c:=c+1;
	end;
	write(c)
end.

Score : 200 points

Problem Statement

Find the number of palindromic numbers among the integers between A and B (inclusive).
Here, a palindromic number is a positive integer whose string representation in base 10 (without leading zeros) reads the same forward and backward.

Constraints

10000 \leq A \leq B \leq 99999

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the number of palindromic numbers among the integers between A and B (inclusive).

Sample Input 1

11009 11332

Sample Output 1

4

There are four integers that satisfy the conditions: 11011, 11111, 11211 and 11311.

Sample Input 2

31415 92653

Sample Output 2

612


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;
    step: array[1..90000] of Longint;
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;
    for i := d+1 to h*w do
    begin
        step[i]:=step[i-d]+abs(index[i][1]-index[i-d][1])+abs(index[i][2]-index[i-d][2]);;
    end;
    read(q);
    for i := 1 to q do
    begin
        readln(l,r);
        writeln(step[r]-step[l]);
    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
    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;

        tmp:=0;
        k:=l;

        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;

        writeln(tmp);
    end;
end.


var x,y:integer;
begin
readln(x,y);
if((x mod y) = 0)then writeln(-1) else writeln(x);
end.

Score : 100 points

Problem Statement

You are given positive integers X and Y.
If there exists a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, choose one such integer and print it.
If it does not exist, print -1.

Constraints

1 ≤ X,Y ≤ 10^9

X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, or print -1 if it does not exist.

Sample Input 1

8 6

Sample Output 1

16

For example, 16 is a multiple of 8 but not a multiple of 6.

Sample Input 2

3 3

Sample Output 2

-1

A multiple of 3 is a multiple of 3.


var
    n,m:longint;
    i:qword;
begin
    readln(n,m);
    if(n=m)then
    begin
        writeln(-1);
        exit;
    end;
    while(i<1000000000000000000)do
    begin
        inc(i);
        if(i mod n=0)and(i mod m<>0)then
        begin
            writeln(i);
            exit;
        end;
    end;
end.


var
        i:int64;
        a,b:longint;
begin
        readln(a,b);
        if a mod b=0 then begin writeln(-1); halt;end;
        repeat
                inc(i);
        until (i mod a=0)and(i mod b<>0);
        writeln(i);
end.


var
        i:int64;
        a,b:longint;
begin
        readln(a,b);
        if a=b then begin writeln(-1); halt;end;
        repeat
                inc(i);
        until (i mod a=0)and(i mod b<>0);
        writeln(i);
end.


var n,m,i,ans:longint;
var a:int64;
begin
  read(n,m);
  if m mod n=0 then begin writeln(-1);exit;end;
  for i:=2 to 2147483647 do
  begin
    a:=n*i;
    if a mod m>0 then begin writeln(a);exit;end;
  end;
  writeln(-1);
end.


var
  s1,s2,n,i,t:longint;
begin
  readln(n);
  s1:=0;s2:=0;
  for i:=1 to n do
    begin
      read(t);
      s1:=s1+t;
    end;
  for i:=1 to n do
    begin
      read(t);
      s2:=s2+t;
    end;
  if (s2-s1) mod 2=0 then writeln('Yes')
                     else writeln('No');
end.

Score : 300 points

Problem Statement

You are given two integer sequences of length N: a_1,a_2,..,a_N and b_1,b_2,..,b_N.
Determine if we can repeat the following operation zero or more times so that the sequences a and b become equal.

Operation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:

Add 2 to a_i.

Add 1 to b_j.

Constraints

1 ≤ N ≤ 10 000

0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N
b_1 b_2 .. b_N

Output

If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.

Sample Input 1

3
1 2 3
5 2 2

Sample Output 1

Yes

For example, we can perform three operations as follows to do our job:

First operation: i=1 and j=2. Now we have a = \{3,2,3\}, b = \{5,3,2\}.

Second operation: i=1 and j=2. Now we have a = \{5,2,3\}, b = \{5,4,2\}.

Third operation: i=2 and j=3. Now we have a = \{5,4,3\}, b = \{5,4,3\}.

Sample Input 2

5
3 1 4 1 5
2 7 1 8 2

Sample Output 2

No

Sample Input 3

5
2 7 1 8 2
3 1 4 1 5

Sample Output 3

No


var n:longint;
    i:longint;
    a,b:Array[0..15000]of int64;
    need,have:int64;
begin
  read(n);
  for i:=1 to n do
  read(a[i]);
  for i:=1 to n do
  read(b[i]);
  for i:=1 to n do
  if a[i]>b[i] then inc(need,(a[i]-b[i])) else inc(have,(b[i]-a[i]) div 2);
  if have>=need then writeln('Yes') else writeln('No');
end.


type
  node=record
      num,id:longint;
  end;
var father,num,now,first:array[0..10000]of longint;
    p:array[0..10000]of node;
    heap:array[0..10000,0..2]of longint;
    i:longint;
    x,y:longint;
    n,m:longint;
    totin,totout:longint;
    qwq,ans,have:int64;

function getfather(x:longint):longint;
begin
  if father[x]=x then exit(x) else
  begin
    father[x]:=getfather(father[x]);
    getfather:=father[x];
  end;
end;
procedure adde(x,y:longint);
var fx,fy:longint;
begin
  fx:=getfather(x);
  fy:=getfather(y);
  if fx<>fy then
  begin
    father[fx]:=fy;
    inc(num[fy],num[fx]);
  end;
end;
procedure qs(l,r:longint);
var i,j:longint;
    t,m:node;
begin
  i:=l;
  j:=r;
  m:=p[(l+r)>>1];
  repeat
    while (p[i].id<m.id)or((p[i].id=m.id)and(p[i].num<m.num)) do inc(i);
    while (p[j].id>m.id)or((p[j].id=m.id)and(p[j].num>m.num)) do dec(j);
    if i<=j then
    begin
      t:=p[i];p[i]:=p[j];p[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;
procedure swap(var a,b:longint);
var t:longint;
begin
  t:=a;a:=b;b:=t;
end;
procedure up(x,bool:longint);
begin
  while x>1 do
  begin
    //writeln(heap[x,bool],'qwq');
    if p[now[heap[x,bool]]].num<p[now[heap[x>>1,bool]]].num then
    begin
      swap(heap[x,bool],heap[x>>1,bool]);
      x:=x>>1;
    end else break;
  end;
end;
procedure down(x,bool:longint);
var min,tot:longint;
begin
  if bool=1 then tot:=totin else tot:=totout;
  while x*2<=tot do
  begin
    min:=x*2;
    if (x*2+1<=tot)and
    (p[now[heap[x*2+1,bool]]].num<p[now[heap[min,bool]]].num) then inc(min);
    if (p[now[heap[x,bool]]].num>p[now[heap[min,bool]]].num) then
    begin
      swap(heap[x,bool],heap[min,bool]);
      x:=min;
    end else break;
  end;
end;
begin
  read(n,m);
  for i:=1 to n do
  begin
    read(p[i].num);
    father[i]:=i;
    num[i]:=1;
  end;
  for i:=1 to m do
  begin
    read(x,y);
    inc(x);
    inc(y);
    adde(x,y);
  end;
  for i:=1 to n do
  p[i].id:=getfather(i);
  qs(1,n);
  p[0].num:=maxlongint;
  i:=1;
  while i<=n do
  begin
    inc(ans,p[i].num);
    if num[p[i].id]>1 then
    begin
      now[p[i].id]:=i+1;
      inc(have,num[p[i].id]);
      dec(have);
    end else
    begin
      now[p[i].id]:=0;
      dec(have);
    end;
    inc(totout);
    heap[totout,0]:=p[i].id;
    up(totout,0);
    first[p[i].id]:=i;
    i:=i+num[p[i].id];
  end;
  if totout=1 then
  begin
    writeln(0);
    exit;
  end;
  inc(have);
  if have<0 then
  begin
    writeln('Impossible');
    exit;
  end;
  while totout>0 do
  begin
    inc(qwq);
    x:=heap[1,0];
    //writeln(x);
    if now[x]<>0 then
    begin
      inc(totin);
      heap[totin,1]:=x;
      up(totin,1);
    end else
    if (qwq>1)and(totin>0) then
    begin
      x:=heap[1,1];
      inc(ans,p[now[x]].num);
    end;
    if totin>0 then
    begin
      x:=heap[1,1];
      if qwq>2 then inc(ans,p[now[x]].num);
      inc(now[x]);
      if (now[x]>=first[x]+num[x]) then
      begin
        now[x]:=0;
        heap[1,1]:=heap[totin,1];
        dec(totin);
      end;
      if totin>0 then down(1,1);
    end;
    heap[1,0]:=heap[totout,0];
    dec(totout);
    down(1,0);
  end;
  writeln(ans);
end.

Score : 600 points

Problem Statement

You are given a forest with N vertices and M edges. The vertices are numbered 0 through N-1.
The edges are given in the format (x_i,y_i), which means that Vertex x_i and y_i are connected by an edge.

Each vertex i has a value a_i.
You want to add edges in the given forest so that the forest becomes connected.
To add an edge, you choose two different vertices i and j, then span an edge between i and j.
This operation costs a_i + a_j dollars, and afterward neither Vertex i nor j can be selected again.

Find the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Constraints

1 ≤ N ≤ 100,000

0 ≤ M ≤ N-1

1 ≤ a_i ≤ 10^9

0 ≤ x_i,y_i ≤ N-1

The given graph is a forest.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
a_0 a_1 .. a_{N-1}
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Sample Input 1

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

Sample Output 1

7

If we connect vertices 0 and 5, the graph becomes connected, for the cost of 1 + 6 = 7 dollars.

Sample Input 2

5 0
3 1 4 1 5

Sample Output 2

Impossible

We can't make the graph connected.

Sample Input 3

1 0
5

Sample Output 3

0

The graph is already connected, so we do not need to add any edges.


var
  n,i,t,x,y:longint;
  p:boolean;
begin
  readln(n);
  p:=true;
  for i:=1 to n do
  begin
    readln(t,x,y);
    if not(((((x+y) mod 2=0)and(t mod 2=0))or(((x+y) mod 2=1)and(t mod 2=1)))and(t>=x+y)) then begin p:=false;write('No');break;end;
  end;
  if p then write('Yes');
end.

Score : 300 points

Problem Statement

AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.

If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).
Note that he cannot stay at his place.
Determine whether he can carry out his plan.

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

t_i < t_{i+1} (1 ≤ i ≤ N-1)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

If AtCoDeer can carry out his plan, print Yes; if he cannot, print No.

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

For example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1).

Sample Input 2

1
2 100 100

Sample Output 2

No

It is impossible to be at (100,100) two seconds after being at (0,0).

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


var
  n,i,t,x,y:longint;
  p:boolean;
begin
  readln(n);
  p:=true;
  for i:=1 to n do
  begin
    readln(t,x,y);
    if not((((x+y) mod 2=0)and(t mod 2=0))or(((x+y) mod 2=1)and(t mod 2=1))) then begin p:=false;write('NO');break;end;
  end;
  if p then write('Yes');
end.


var
    n,i,a,b,t,x,y:longint;
begin
    readln(n);
    x:=0; y:=0;
    for i:=1 to n do
    begin
        read(t,a,b);
        if (abs(x-a)+abs(y-b)>t) or ((abs(x-a)+abs(y-b)-t) mod 2=1) then
        begin
            writeln('No');
            halt;
        end;
    end;
    writeln('Yes');
end.


var
 n,i,t,x,y:longint;
begin
 readln(n);
 for i:=1 to n do
  begin
   readln(t,x,y);
   if x+y>t then
    begin
     writeln('No');
     halt;
    end;
   if ((x+y) mod 2=0) and (odd(t)) then
    begin
     writeln('No');
     halt;
    end;
   if ((x+y) mod 2=1) and (t mod 2=0) then
    begin
     writeln('No');
     halt;
    end;
  end;
 writeln('Yes');
end.


var
  n,x,y,t,x1,y1,t1,i,s:longint;
begin
  readln(n);
  x:=0; y:=0;
  for i:= 1 to n do
    begin
      readln(t1,x1,y1);
      if (x=x1)and(y=y1)then begin writeln('No'); exit; end;
      if abs(x-x1)+abs(y-y1)=t1-t then begin x:=x1;y:=y1;t:=t1;end
                                  else
                                    begin
                                      if abs(x-x1)+abs(y-y1)<t1-t
                                        then
                                         begin
                                          s:=(t1-t)-(abs(x-x1)+abs(y-y1));
                                          if odd(s) then
                                                      begin
                                                        writeln('No');
                                                        exit;
                                                      end
                                                    else
                                                      begin
                                                        x:=x1;
                                                        y:=y1;
                                                        t:=t1;
                                                      end;
                                         end
                                        else
                                          begin
                                            writeln('No');
                                            exit;
                                          end

                                    end;
    end;
  writeln('Yes');
end.


var i,j,k,m,n,t:longint;
begin
  read(n);
  for i:=1 to n do
  begin
    read(j,k,m);
    if k+m<=j then t:=1
    else t:=0;
  end;
  if t=1 then write('Yes')
  else write('No');
end.


var
    n,i,a,b,t:longint;
begin
    readln(n);
    for i:=1 to n do
    begin
        read(t,a,b);
        if (a+b>t) or ((a+b-t) div 2=1) then
        begin
            writeln('No');
            halt;
        end;
    end;
    writeln('Yes');
end.


var
    n,i,a,b,t:longint;
begin
    readln(n);
    for i:=1 to n do
    begin
        read(t,a,b);
        if a+b>t then
        begin
            writeln('No');
            halt;
        end;
    end;
    writeln('Yes');
end.


Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4


var n:longint;
    max,x,min:int64;
    a:array[0..150000]of longint;
    i:longint;
begin
  read(n);
  for i:=1 to n do
  read(a[i]);
  if a[n]<>2 then
  begin
    writeln(-1);
    exit;
  end;
  a[0]:=1;
  max:=2;
  min:=2;
  for i:=n-1 downto 1 do
  begin
    x:=max*2-1;
    if max+a[i+1]-1<x then x:=max+a[i+1]-1;
    x:=x div a[i];
    if (x=0) then
    begin
      writeln(-1);
      exit;
    end else max:=x*a[i];
    x:=min div a[i];
    if min mod a[i]=0 then min:=x*a[i] else min:=(x+1)*a[i];
    if min>max then
    begin
      writeln(-1);
      exit;
    end;
  end;
  writeln(min,' ',max+a[1]-1);
end.
//or(((max+a[i+1]-1) div a[i])-((max-1) div a[i])=0)

Score : 500 points

Problem Statement

An adult game master and N children are playing a game on an ice rink.
The game consists of K rounds.
In the i-th round, the game master announces:

Form groups consisting of A_i children each!

Then the children who are still in the game form as many groups of A_i children as possible.
One child may belong to at most one group.
Those who are left without a group leave the game. The others proceed to the next round.
Note that it's possible that nobody leaves the game in some round.

In the end, after the K-th round, there are exactly two children left, and they are declared the winners.

You have heard the values of A_1, A_2, ..., A_K. You don't know N, but you want to estimate it.

Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.

Constraints

1 \leq K \leq 10^5

2 \leq A_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

K
A_1 A_2 ... A_K

Output

Print two integers representing the smallest and the largest possible value of N, respectively,
or a single integer -1 if the described situation is impossible.

Sample Input 1

4
3 4 3 2

Sample Output 1

6 8

For example, if the game starts with 6 children, then it proceeds as follows:

In the first round, 6 children form 2 groups of 3 children, and nobody leaves the game.

In the second round, 6 children form 1 group of 4 children, and 2 children leave the game.

In the third round, 4 children form 1 group of 3 children, and 1 child leaves the game.

In the fourth round, 3 children form 1 group of 2 children, and 1 child leaves the game.

The last 2 children are declared the winners.

Sample Input 2

5
3 4 100 3 2

Sample Output 2

-1

This situation is impossible.
In particular, if the game starts with less than 100 children, everyone leaves after the third round.

Sample Input 3

10
2 2 2 2 2 2 2 2 2 2

Sample Output 3

2 3