var
	k,n,p,q,i,j:Longint;
	s:array[1..500,1..500]of Longint;
	c:array[1..500]of Longint;
begin
	read(n);
	k:=1;
	while k*(k-1)<n*2 do inc(k);
	if k*(k-1)<>n*2 then begin
		writeln('No');
		exit;
	end;
	writeln('Yes');
	writeln(k);
	p:=1;
	q:=2;
	for i:=1 to n do begin
		inc(c[p]);
		inc(c[q]);
		s[p,c[p]]:=i;
		s[q,c[q]]:=i;
		inc(q);
		if q>k then begin
			inc(p);
			q:=p+1;
		end;
	end;
	for i:=1 to k do begin
		write(k-1);
		for j:=1 to k-1 do write(' ',s[i,j]);
		writeln;
	end;
end.

Score : 500 points

Problem Statement

You are given an integer N. Determine if there exists a tuple of subsets of \{1,2,...N\}, (S_1,S_2,...,S_k), that satisfies the following conditions:

Each of the integers 1,2,...,N is contained in exactly two of the sets S_1,S_2,...,S_k.

Any two of the sets S_1,S_2,...,S_k have exactly one element in common.

If such a tuple exists, construct one such tuple.

Constraints

1 \leq N \leq 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If a tuple of subsets of \{1,2,...N\} that satisfies the conditions does not exist, print No.
If such a tuple exists, print Yes first, then print such subsets in the following format:

k
|S_1| S_{1,1} S_{1,2} ... S_{1,|S_1|}
:
|S_k| S_{k,1} S_{k,2} ... S_{k,|S_k|}

where S_i={S_{i,1},S_{i,2},...,S_{i,|S_i|}}.

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

Sample Input 1

3

Sample Output 1

Yes
3
2 1 2
2 3 1
2 2 3

It can be seen that (S_1,S_2,S_3)=(\{1,2\},\{3,1\},\{2,3\}) satisfies the conditions.

Sample Input 2

4

Sample Output 2

No


var
  a,c:array[1..100000]of longint;
  b:array[1..100000]of boolean;
  i,n,s,j,x,y,sum1:longint;
procedure qsort(l,r:longint);
var
  i,j,mid,p:longint;
begin
  i:=l;j:=r;
  mid:=a[(l+r) div 2];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
      begin
        p:=a[i];a[i]:=a[j];a[j]:=p;
        inc(i);
        dec(j);
      end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  if n=2 then begin write(abs(a[1]-a[2]));halt;end;
  qsort(1,n);
  fillchar(b,sizeof(b),true);
  i:=2;j:=n-2;
  if odd(n) then
              begin
			    c[n div 2+1]:=a[1];
				c[n div 2]:=a[n];
				c[n div 2+2]:=a[n-1];
				s:=3;
				x:=n div 2-1;
				y:=n div 2+3;
			  end
			else
			  begin
			    c[n div 2]:=a[1];
				c[n div 2-1]:=a[n];
				c[n div 2+1]:=a[n-1];
				s:=3;
				x:=n div 2-2;
				y:=n div 2+2;
			  end;
  while (x>=1) and (y<=n) do
  begin
    if abs(c[x]-a[i])>abs(c[y]-a[i])then
	begin
	  c[x]:=a[i];inc(i);dec(x);
	  c[y]:=a[i];inc(i);inc(y);
	end
	else
	begin
      c[y]:=a[i];inc(i);inc(y);
	  c[x]:=a[i];inc(i);dec(x);
	end;
    if abs(c[x]-a[j])>abs(c[y]-a[j])then
	begin
	c[x]:=a[j];dec(j);dec(x);
	c[y]:=a[j];dec(j);inc(y);
	end
	else begin
      c[y]:=a[j];dec(j);inc(y);
	  c[x]:=a[j];dec(j);dec(x);
	end;
	s:=s+2;
  end;
  if odd(x) then c[n]:=a[n div 2+1] else c[x]:=a[n div 2];
  for i:=2 to n do sum1:=sum1+abs(c[i]-c[i-1]);
  write(sum1);
  readln(i);
end.

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

Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 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_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8

Submitted source-code file size, in bytes.

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

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


var
  a,c:array[1..100000]of longint;
  b:array[1..100000]of boolean;
  i,n,s,j,x,y,sum1,sum2:longint;
procedure qsort(l,r:longint);
var
  i,j,mid,p:longint;
begin
  i:=l;j:=r;
  mid:=a[(l+r) div 2];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
      begin
        p:=a[i];a[i]:=a[j];a[j]:=p;
        inc(i);
        dec(j);
      end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  if n=2 then begin write(abs(a[1]-a[2]));halt;end;
  qsort(1,n);
  fillchar(b,sizeof(b),true);
  i:=2;j:=n-2;
  if odd(n) then
              begin
			    c[n div 2+1]:=a[1];
				c[n div 2]:=a[n];
				c[n div 2+2]:=a[n-1];
				s:=3;
				x:=n div 2-1;
				y:=n div 2+3;
			  end
			else
			  begin
			    c[n div 2]:=a[1];
				c[n div 2-1]:=a[n];
				c[n div 2+1]:=a[n-1];
				s:=3;
				x:=n div 2-2;
				y:=n div 2+2;
			  end;
  while (x>=1) and (y<=n) do
  begin
  if (s mod 4=3)and(i<=n) then
  begin
    c[x]:=a[i];inc(i);inc(y);
	c[y]:=a[i];inc(i);dec(x);
	s:=s+2;
  end;
  if (s mod 4=1)and(j>=1) then
  begin
    c[x]:=a[j];dec(j);dec(x);
	c[y]:=a[j];dec(j);inc(y);
	s:=s+2;
  end;
  end;
  if odd(x) then c[n]:=a[n div 2+1] else c[x]:=a[n div 2];
  for i:=2 to n do sum1:=sum1+abs(c[i]-c[i-1]);
  i:=2;j:=n-2;
  if odd(n) then
              begin
			    c[n div 2+1]:=a[1];
				c[n div 2]:=a[n];
				c[n div 2+2]:=a[n-1];
				s:=3;
				x:=n div 2-1;
				y:=n div 2+3;
			  end
			else
			  begin
			    c[n div 2]:=a[1];
				c[n div 2-1]:=a[n];
				c[n div 2+1]:=a[n-1];
				s:=3;
				x:=n div 2-2;
				y:=n div 2+2;
			  end;
  while (x>=1) and (y<=n) do
  begin
  if (s mod 4=3)and(i<=n) then
  begin
    c[y]:=a[i];inc(i);inc(y);
	c[x]:=a[i];inc(i);dec(x);
	s:=s+2;
  end;
  if (s mod 4=1)and(j>=1) then
  begin
    c[y]:=a[j];dec(j);dec(x);
	c[x]:=a[j];dec(j);inc(y);
	s:=s+2;
  end;
  end;
  if odd(x) then c[n]:=a[n div 2+1] else c[x]:=a[n div 2];
  for i:=2 to n do sum2:=sum2+abs(c[i]-c[i-1]);
  if sum1>sum2 then write(sum1) else write(sum2);
  readln(i);
end.


var
  a,c:array[1..100000]of longint;
  b:array[1..100000]of boolean;
  i,n,s,j,x,y,sum:longint;
procedure qsort(l,r:longint);
var
  i,j,mid,p:longint;
begin
  i:=l;j:=r;
  mid:=a[(l+r) div 2];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
      begin
        p:=a[i];a[i]:=a[j];a[j]:=p;
        inc(i);
        dec(j);
      end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  if n=2 then begin write(abs(a[1]-a[2]));halt;end;
  qsort(1,n);
  fillchar(b,sizeof(b),true);
  i:=2;j:=n-2;b[1]:=false;b[n-1]:=false;b[n]:=false;
  if odd(n) then
              begin
			    c[n div 2+1]:=a[1];
				c[n div 2]:=a[n];
				c[n div 2+2]:=a[n-1];
				s:=3;
				x:=n div 2-1;
				y:=n div 2+3;
			  end
			else
			  begin
			    c[n div 2]:=a[1];
				c[n div 2-1]:=a[n];
				c[n div 2+1]:=a[n-1];
				s:=3;
				x:=n div 2-2;
				y:=n div 2+2;
			  end;
  while (x>=1) and (y<=n) do
  begin
  if (s mod 4=3)and(i<=n) then
  begin
    c[x]:=a[i];inc(i);inc(y);
	c[y]:=a[i];inc(i);dec(x);
	s:=s+2;
  end;
  if (s mod 4=1)and(j>=1) then
  begin
    c[x]:=a[j];dec(j);dec(x);
	c[y]:=a[j];dec(j);inc(y);
	s:=s+2;
  end;
  end;
  if odd(x) then c[n]:=a[n div 2+1] else c[x]:=a[n div 2];
  for i:=2 to n do sum:=sum+abs(c[i]-c[i-1]);
  write(sum);
  readln(i);
end.


var
  x,y:array[0..100000]of char;
  ans:int64;
  i,n,m:longint;
Function gcd(x,y:longint):int64;
begin
  if y=0 then exit(x) else
  exit(gcd(y,x mod y));
end;
begin
  readln(n,m);
  for i:=1 to n do
    read(x[i]);
  readln;
  for i:=1 to m do
    read(y[i]);
  for i:=0 to n-1 do
    if i*m mod n=0 then
      if x[i+1]<>y[i*m div n+1] then
        begin
          write(-1);
          exit;
        end;
  ans:=n*m div gcd(n,m);
  write(ans);
end.

Score : 300 points

Problem Statement

You are given a string S of length N and another string T of length M.
These strings consist of lowercase English letters.

A string X is called a good string when the following conditions are all met:

Let L be the length of X. L is divisible by both N and M.

Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.

Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.

Determine if there exists a good string. If it exists, find the length of the shortest such string.

Constraints

1 \leq N,M \leq 10^5

S and T consist of lowercase English letters.

|S|=N

|T|=M

Input

Input is given from Standard Input in the following format:

N M
S
T

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.

Sample Input 1

3 2
acp
ae

Sample Output 1

6

For example, the string accept is a good string.
There is no good string shorter than this, so the answer is 6.

Sample Input 2

6 3
abcdef
abc

Sample Output 2

-1

Sample Input 3

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3

45


var
	i:Longint;
	n,m,u:int64;
	s,t:String;
function gcd(a,b:int64):int64;
begin
	if b=0 then gcd:=a else gcd:=gcd(b,a mod b);
end;
begin
	readln(n,m);
	setlength(s,n);
	setlength(t,m);
	readln(s);
	readln(t);
	u:=gcd(n,m);
	for i:=0 to u-1 do if s[n div u*i+1]<>t[m div u*i+1]then begin
		writeln(-1);
		exit;
	end;
	writeln(n*m div u);
end.


var
  a,b:string;
  n,m,i,j:longint;
  k,p,q:qword;
begin
  readln(n,m);
  readln(a);
  readln(b);
  p:=n;
  q:=m;
  if p<q then begin i:=p; p:=q; q:=i; end;
  while (p mod q)<>0 do if p>q then begin p:=p mod q; i:=p; p:=q; q:=i; end;
  k:=n*m div q;
  p:=k div n;
  q:=k div m;
  i:=1;
  j:=1;
  while (j<=m)and(i<=n) do
  begin
	 if b[j]<>a[i] then begin writeln('-1'); halt; end;
     i:=i+q;
	 j:=j+p;
  end;
  writeln(k);
end.


var
  a,b,c:string;
  t:char;
  n,m,i,j,p,q,k:longint;
begin
  readln(n,m);
  readln(a);
  readln(b);
  p:=n;
  q:=m;
  if p<q then begin i:=p; p:=q; q:=i; end;
  while (p mod q)<>0 do begin p:=p mod q; i:=p; p:=q; q:=i; end;
  k:=n*m div q;
  p:=k div n;
  q:=k div m;
  i:=1;
  j:=1;
  while (j<=m)and(i<=n) do
  begin
	 if b[j]<>a[i] then begin writeln('-1'); halt; end;
     i:=i+q;
	 j:=j+p;
  end;
  writeln(k);
end.


var
  a,b,c:string;
  t:char;
  n,m,i,j,p,q,k:longint;
begin
  readln(n,m);
  readln(a);
  readln(b);
  p:=n;
  q:=m;
  if p<q then begin i:=p; p:=q; q:=i; end;
  while (p mod q)<>0 do if p>q then begin p:=p mod q; i:=p; p:=q; q:=i; end;
  k:=n*m div q;
  i:=1;
  t:='2';
  while true do
  begin
    t:='2';
    i:=i+q;
    if i>k then break;
    if ((i-1) mod (k mod n)=0) then
    begin
      c[i div q]:=a[(i-1) div (k mod n)+1];
      if ((i-1) mod (k mod m)=0) then t:=b[(i-1) div (k mod m)+1];
      if (t<>'2')and(t=c[i]) then continue
      else if (t<>'2') then begin writeln('-1'); halt; end;
    end;
  end;
  writeln(k);
end.


var
  n,m,d,ans:longint;
function check(x:longint):boolean;
begin
  if (m div x)>=n then
  begin
    check:=true;
  end
  else
  begin
    check:=false;
  end;
end;
begin
  ans:=1;
  readln(n,m);
  for d:=1 to trunc(sqrt(m)) do
  begin
    if (m mod d)<>0 then
    begin
      continue;
    end;
    if check(d) then
    begin
      if d>ans then
      begin
        ans:=d;
      end;
    end;
    if check(m div d) then
    begin
      if (m div d)>ans then
      begin
        ans:=m div d;
      end;
    end;
  end;
  writeln(ans);
end.

Score : 400 points

Problem Statement

You are given integers N and M.

Consider a sequence a of length N consisting of positive integers such that a_1 + a_2 + ... + a_N = M. Find the maximum possible value of the greatest common divisor of a_1, a_2, ..., a_N.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

N \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the maximum possible value of the greatest common divisor of a sequence a_1, a_2, ..., a_N that satisfies the condition.

Sample Input 1

3 14

Sample Output 1

2

Consider the sequence (a_1, a_2, a_3) = (2, 4, 8). Their greatest common divisor is 2, and this is the maximum value.

Sample Input 2

10 123

Sample Output 2

3

Sample Input 3

100000 1000000000

Sample Output 3

10000


var
  n,m,i,max:longint;
begin
  max:=1;
  read(n,m);
  for i:=m div n downto 1 do
  if m mod i=0 then break;
write(i);
end.


var
n,m,i,t:longint;
begin
read(n,m);
t:=trunc(sqrt(m));
for i:=t downto 1 do
if m mod i=0 then
if m div i>=n then
begin
writeln(i);
break;
end;
end.


program Project1;

{$APPTYPE CONSOLE}

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

    Write  (10-strtoint(s[1]));
    Write  (10-strtoint(s[2]));
    Writeln(10-strtoint(s[3]));

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

Score : 100 points

Problem Statement

Cat Snuke is learning to write characters.
Today, he practiced writing digits 1 and 9, but he did it the other way around.

You are given a three-digit integer n written by Snuke.
Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.

Constraints

111 \leq n \leq 999

n is an integer consisting of digits 1 and 9.

Input

Input is given from Standard Input in the following format:

n

Output

Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.

Sample Input 1

119

Sample Output 1

991

Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.

Sample Input 2

999

Sample Output 2

111


var
  a:array[1..100000]of longint;
  b:array[1..2,1..100000]of longint;
  n,m,k,i,p,q,o,f,t:longint;
begin
  readln(n);
  for i:=1 to n do
  begin
     read(a[i]);
	 inc(b[((i-1)mod 2)+1,a[i]]);
	 if a[i]>k then k:=a[i];
  end;
  for i:=1 to k do
  begin
     if b[1,i]>p then begin p:=b[1,i]; o:=i; end;
     if b[2,i]>q then begin q:=b[2,i]; m:=i; end;
  end;
  if o=m then
  begin
    if p>q then
	begin
    for i:=1 to k do
	begin
       if (b[2,i]<>q)and(b[2,i]>f) then f:=b[2,i];
    end;
	f:=f+p;
	end else
    if p<q then
	begin
    for i:=1 to k do
	begin
       if (b[1,i]<>p)and(b[1,i]>f) then f:=b[1,i];
    end;
	f:=f+q;
	end else
	begin
    for i:=1 to k do
	begin
       if (b[1,i]<>p)and(b[1,i]>f) then f:=b[1,i];
       if (b[2,i]<>p)and(b[2,i]>t) then t:=b[2,i];
    end;
	if t>f then f:=t+p
	else f:=f+p;
	end;
  end else f:=p+q;
  writeln(n-f);
end.

Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.


var
	f1,f2:array[0..100010] of longint;
	max,t:array[1..2,1..2] of longint;
	n,i,x,s:longint;
begin
	read(n);
	fillchar(f1,sizeof(f1),0);
	fillchar(f2,sizeof(f2),0);
	fillchar(max,sizeof(max),0);
	for i:=1 to n do
	begin
		read(x);
		if i mod 2=1 then
		begin
			inc(f1[x]);
			if f1[x]>max[1,1] then
			begin
				if x<>t[1,1] then
				begin
					max[1,2]:=max[1,1];
					t[1,2]:=t[1,1];
				end;
				max[1,1]:=f1[x];
				t[1,1]:=x;
			end
			else if f1[x]>max[1,2] then
				begin
					max[1,2]:=f1[x];
					t[1,2]:=x;
				end;
		end
		else
		begin
			inc(f2[x]);
			if f2[x]>max[2,1] then
			begin
				if x<>t[2,1] then
				begin
					max[2,2]:=max[2,1];
					t[2,2]:=t[2,1];
				end;
				max[2,1]:=f2[x];
				t[2,1]:=x;
			end
			else if f2[x]>max[2,2] then
				begin
					max[2,2]:=f2[x];
					t[1,2]:=x;
				end;
		end;
	end;
	s:=0;
	if (t[1,1]<>t[2,1]) then s:=max[1,1]+max[2,1]
	else if max[1,2]+max[2,1]>max[1,1]+max[2,2] then s:=max[1,2]+max[2,1]
	else s:=max[1,1]+max[2,2];
	if (t[1,1]=t[2,1]) and (max[1,2]=0) and (max[2,2]=0) then writeln(n div 2)
	else writeln(n-s);
end.


var
a:array[1..100000]of longint;
b:array[0..1,0..100000]of longint;
max:array[0..1]of longint;
n,i,s:longint;
begin
read(n);
for i:=1 to n do
begin
read(a[i]);
inc(b[i mod 2,a[i]]);
 if b[i mod 2,a[i]]>max[i mod 2] then
 begin
 max[i mod 2]:=b[i mod 2,a[i]];
 b[i mod 2,0]:=a[i];
 end;
end;
s:=0;
for i:=1 to n do
 if a[i]<>b[i mod 2,0] then
 inc(s);
writeln(s);
end.


var
	i,j,k,n,max:longint;
	t,x,y:array[-110..10010] of longint;
	s:ansistring;
begin
	readln(n);
	max:=0;
	for i:=1 to n do
	begin
		read(x[i],y[i]);
		t[i]:=abs(x[i])+abs(y[i]);
		if t[i] mod 2<>t[1] mod 2 then
		begin
			writeln(-1);
			halt;
		end;
		if t[i]>max then max:=t[i];
	end;
	writeln(max);
	for i:=1 to max do write(2-t[i] mod 2,' ');
	writeln;
	for i:=1 to n do
	begin
		s:='';
		if x[i]<0 then
			for j:=1 to -x[i] do s:=s+'L'
		else if x[i]>0 then for j:=1 to x[i] do s:=s+'R';
		if y[i]<0 then
			for j:=1 to -y[i] do s:=s+'D'
		else if y[i]>0 then for j:=1 to y[i] do s:=s+'U';
		k:=length(s);
		for j:=k+1 to max do
			if j mod 2=1 then s:=s+'L' else s:=s+'R';
		writeln(s);
	end;
end.

Score : 600 points

Problem Statement

Snuke is introducing a robot arm with the following properties to his factory:

The robot arm consists of m sections and m+1 joints. The sections are numbered 1, 2, ..., m, and the joints are numbered 0, 1, ..., m. Section i connects Joint i-1 and Joint i. The length of Section i is d_i.

For each section, its mode can be specified individually. There are four modes: L, R, D and U. The mode of a section decides the direction of that section. If we consider the factory as a coordinate plane, the position of Joint i will be determined as follows (we denote its coordinates as (x_i, y_i)):

(x_0, y_0) = (0, 0).

If the mode of Section i is L, (x_{i}, y_{i}) = (x_{i-1} - d_{i}, y_{i-1}).

If the mode of Section i is R, (x_{i}, y_{i}) = (x_{i-1} + d_{i}, y_{i-1}).

If the mode of Section i is D, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} - d_{i}).

If the mode of Section i is U, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} + d_{i}).

Snuke would like to introduce a robot arm so that the position of Joint m can be matched with all of the N points (X_1, Y_1), (X_2, Y_2), ..., (X_N, Y_N) by properly specifying the modes of the sections.
Is this possible?
If so, find such a robot arm and how to bring Joint m to each point (X_j, Y_j).

Constraints

All values in input are integers.

1 \leq N \leq 1000

-10^9 \leq X_i \leq 10^9

-10^9 \leq Y_i \leq 10^9

Partial Score

In the test cases worth 300 points, -10 \leq X_i \leq 10 and -10 \leq Y_i \leq 10 hold.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
:
X_N Y_N

Output

If the condition can be satisfied, follow the following format. If the condition cannot be satisfied, print -1.

m
d_1 d_2 ... d_m
w_1
w_2
:
w_N

m and d_i are the configurations of the robot arm. Refer to the problem statement for what each of them means.
Here, 1 \leq m \leq 40 and 1 \leq d_i \leq 10^{12} must hold. Also, m and d_i must all be integers.

w_j is a string of length m that represents the way to bring Joint m of the robot arm to point (X_j, Y_j).
The i-th character of w_j should be one of the letters L, R, D and U, representing the mode of Section i.

Sample Input 1

3
-1 0
0 3
2 -1

Sample Output 1

2
1 2
RL
UU
DR

In the given way to bring Joint m of the robot arm to each (X_j, Y_j), the positions of the joints will be as follows:

To (X_1, Y_1) = (-1, 0): First, the position of Joint 0 is (x_0, y_0) = (0, 0). As the mode of Section 1 is R, the position of Joint 1 is (x_1, y_1) = (1, 0). Then, as the mode of Section 2 is L, the position of Joint 2 is (x_2, y_2) = (-1, 0).

To (X_2, Y_2) = (0, 3): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, 1), (x_2, y_2) = (0, 3).

To (X_3, Y_3) = (2, -1): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, -1), (x_2, y_2) = (2, -1).

Sample Input 2

5
0 0
1 0
2 0
3 0
4 0

Sample Output 2

-1

Sample Input 3

2
1 1
1 1

Sample Output 3

2
1 1
RU
UR

There may be duplicated points among (X_j, Y_j).

Sample Input 4

3
-7 -3
7 3
-3 -7

Sample Output 4

5
3 1 4 1 5
LRDUL
RDULR
DULRD


var
	N,i,L,R,x,y,z,w:Longint;
	a,b:Array[0..100000]of Longint;
begin
	read(N);
	for i:=1 to N do begin
		read(x);
		if i mod 2=1 then inc(a[x])else inc(b[x]);
	end;
	x:=0;
	for i:=1 to N do begin
		if a[x]<a[i]then begin
			y:=x;
			x:=i;
		end else if a[y]<a[i]then begin
			y:=i;
		end;
		if b[z]<b[i]then begin
			w:=z;
			z:=i;
		end else if b[w]<b[i]then begin
			w:=i;
		end;
	end;
	if x<>z then writeln(N-a[x]-b[z])else begin
		L:=N-a[y]-b[z];
		R:=N-a[x]-b[w];
		if L<R then writeln(L)else writeln(R);
	end;
end.


var
a,t1,t2:array[0..500005]of longint;
 n,m,i,j,k1,k2,tot1,tot2,j1,j2,p1,p2:longint;
 ans:int64;
function max(x,y:longint):longint;
 begin
  if x>y then exit(x) else exit(y);
 end;
begin
readln(n);
for i:=1 to n do
 begin
  read(a[i]);
  if m<a[i] then m:=a[i];
 end;
for i:=1 to n do
 begin
   if i mod 2=0 then begin inc(t1[a[i]]);  end;
   if i mod 2=1 then begin inc(t2[a[i]]);  end;
 end;
  for i:=1 to m do
  begin
    if k1<t1[i] then begin k1:=t1[i];p1:=i; end;
    if k2<t2[i] then begin k2:=t2[i];p2:=i; end;
  end;
  if p1<>p2 then
   begin
     ans:=n-k1-k2;
   end
    else
      begin
      j1:=0;j2:=0;
       for i:=1 to m do
        begin
         if i=p1 then continue;
         if j1<t1[i] then begin j1:=t1[i]; end;
         if j2<t2[i] then begin j2:=t2[i]; end;
        end;
       if j1<j2 then ans:=n-j2-k1 else ans:=n-j1-k2;
      end;
      writeln(ans);
end.


var
	n,i,j,c,a,b:Longint;
	x,y,d:array[1..100000]of Longint;
begin
	read(n);
	for i:=1 to n do begin
		read(x[i],y[i]);
		if (abs(x[i])+abs(y[i]))mod 2<>(abs(x[1])+abs(y[1]))mod 2 then begin
			writeln(-1);
			exit;
		end;
	end;
	a:=1073741824;
	while a>0 do begin
		inc(c);
		d[c]:=a;
		a:=a div 2;
	end;
	if (abs(x[1])+abs(y[1]))mod 2=0 then begin
		inc(c);
		d[c]:=1;
	end;
	writeln(c);
	for i:=1 to c do write(d[i],' ');
	writeln;
	for i:=1 to n do begin
		a:=x[i];
		b:=y[i];
		for j:=1 to c do begin
			if abs(a)>abs(b) then begin
				if a>0 then begin
					dec(a,d[j]);
					write('R');
				end else begin
					inc(a,d[j]);
					write('L');
				end;
			end else begin
				if b>0 then begin
					dec(b,d[j]);
					write('U');
				end else begin
					inc(b,d[j]);
					write('D');
				end;
			end;
		end;
		writeln;
	end;
end.


var
    n,p,m,i,j:longint;
    x,y,d:array[0..1010] of longint;
begin
    readln(n);
    readln(x[1],y[1]);
    p:=abs(x[1]+y[1]) mod 2;
    for i:=2 to n do
        begin
            readln(x[i],y[i]);
            if p<>abs(x[i]+y[i]) mod 2 then
                begin
                    writeln(-1);exit;
                end;
        end;
    m:=30+1-p;writeln(m);d[30]:=1;
    for i:=29 downto 1 do d[i]:=d[i+1]*2;
    if m=31 then d[31]:=1;
    for i:=1 to m do write(d[i],' ');writeln;
    for i:=1 to n do
        begin
            for j:=1 to m do
                if abs(x[i])>abs(y[i]) then
                    if x[i]>0 then begin dec(x[i],d[j]);write('R');end
                    else begin inc(x[i],d[j]);write('L');end
                else
                    if y[i]>0 then begin dec(y[i],d[j]);write('U');end
                    else begin inc(y[i],d[j]);write('D');end;
            writeln;
        end;
end.


var
    s:ansistring;
    len,i,cnt,tot,t:longint;
begin
    readln(s);len:=length(s);
    if (s[1]='0') or (s[len]='1') then
        begin
            writeln(-1);
            exit;
        end;
    dec(len);
    for i:=1 to len div 2 do
        if s[i]<>s[len-i+1] then
            begin
                writeln(-1);
                exit;
            end;
    for i:=1 to len do
        if s[i]='1' then inc(cnt);
    for i:=1 to cnt do
        writeln(i,' ',i+1);
    tot:=cnt+2;t:=0;
    for i:=1 to len do
        if s[i]='0' then
            begin
                writeln(cnt-i+t+2,' ',tot);
                inc(t);inc(tot);
            end;
end.

Score : 700 points

Problem Statement

You are given a string s of length n.
Does a tree with n vertices that satisfies the following conditions exist?

The vertices are numbered 1,2,..., n.

The edges are numbered 1,2,..., n-1, and Edge i connects Vertex u_i and v_i.

If the i-th character in s is 1, we can have a connected component of size i by removing one edge from the tree.

If the i-th character in s is 0, we cannot have a connected component of size i by removing any one edge from the tree.

If such a tree exists, construct one such tree.

Constraints

2 \leq n \leq 10^5

s is a string of length n consisting of 0 and 1.

Input

Input is given from Standard Input in the following format:

s

Output

If a tree with n vertices that satisfies the conditions does not exist, print -1.

If a tree with n vertices that satisfies the conditions exist, print n-1 lines.
The i-th line should contain u_i and v_i with a space in between.
If there are multiple trees that satisfy the conditions, any such tree will be accepted.

Sample Input 1

1111

Sample Output 1

-1

It is impossible to have a connected component of size n after removing one edge from a tree with n vertices.

Sample Input 2

1110

Sample Output 2

1 2
2 3
3 4

If Edge 1 or Edge 3 is removed, we will have a connected component of size 1 and another of size 3. If Edge 2 is removed, we will have two connected components, each of size 2.

Sample Input 3

1010

Sample Output 3

1 2
1 3
1 4

Removing any edge will result in a connected component of size 1 and another of size 3.


var
	s,t:ansistring;
	a,b:array['a'..'z'] of char;
	fs,ft:array['a'..'z'] of boolean;
	i,l:longint;
procedure cant;
begin
	writeln('No');
	halt;
end;
begin
	readln(s); readln(t);
	l:=length(s);
	fillchar(fs,sizeof(fs),true);
	fillchar(ft,sizeof(ft),true);
	for i:=1 to l do
	begin
		if fs[s[i]] then
		begin
			fs[s[i]]:=false;
			a[s[i]]:=t[i];
		end
		else if a[s[i]]<>t[i] then cant;
		if ft[t[i]] then
		begin
			ft[t[i]]:=false;
			b[t[i]]:=s[i];
		end
		else if b[t[i]]<>s[i] then cant;
	end;
	writeln('Yes');
end.

Score : 300 points

Problem Statement

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

You can perform the following operation on S any number of times:

Operation: Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1.

Determine if S and T can be made equal by performing the operation zero or more times.

Constraints

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

|S| = |T|

S and T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S and T can be made equal, print Yes; otherwise, print No.

Sample Input 1

azzel
apple

Sample Output 1

Yes

azzel can be changed to apple, as follows:

Choose e as c_1 and l as c_2. azzel becomes azzle.

Choose z as c_1 and p as c_2. azzle becomes apple.

Sample Input 2

chokudai
redcoder

Sample Output 2

No

No sequences of operation can change chokudai to redcoder.

Sample Input 3

abcdefghijklmnopqrstuvwxyz
ibyhqfrekavclxjstdwgpzmonu

Sample Output 3

Yes


var
	s,t:String;
	c,d:array[0..25]of Longint;
	i,j,k:Longint;
begin
	readln(s);
	readln(t);
	for i:=0 to 25 do begin
		c[i]:=-1;
		d[i]:=-1;
	end;
	for i:=1 to length(s) do begin
		j:=ord(s[i])-97;
		k:=ord(t[i])-97;
		if c[j]=-1 then c[j]:=k;
		if d[k]=-1 then d[k]:=j;
		if(c[j]<>k)or(d[k]<>j)then begin
			writeln('No');
			exit;
		end;
	end;
	writeln('Yes');
end.


var
  s1,s2:string;
  a,b:array['a'..'z']of longint;
  c:array[1..26]of boolean;
  o:boolean;
  i,j,k,p:longint;
begin
  readln(s1);
  read(s2);
  k:=length(s2);
  o:=true;
  if length(s1)<length(s2) then begin k:=length(s1); o:=false; end;
  for i:=1 to k-1 do
  begin
    inc(a[s1[i]]);
	inc(b[s2[i]]);
  end;
  if o then inc(b[s2[k]])
  else inc(a[s1[k]]);
  if o then p:=length(s1)
  else p:=length(s2);
  for i:=k to p do
  begin
  begin
     if o then inc(a[s1[i]])
	 else inc(b[s2[i]]);
  end;
  end;
  o:=true;
  for i:=1 to 26 do
  begin
     p:=a[chr(i+ord('a')-1)];
	 for j:=1 to 26 do
	 begin
	    if (c[j]=false)and(b[chr(j+ord('a')-1)]=p) then
		begin
		   c[j]:=true;
		   break;
		end;
		if j=26 then o:=false;
     end;
	 if o=false then begin writeln('No'); halt; end;
  end;
  writeln('Yes');
end.


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

Score : 400 points

Problem Statement

You are given positive integers N and M.

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

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

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

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

Sample Input 1

2 6

Sample Output 1

4

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

Sample Input 2

3 12

Sample Output 2

18

Sample Input 3

100000 1000000000

Sample Output 3

957870001


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

Score : 700 points

Problem Statement

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

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

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

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

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

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

Constraints

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

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

1 \leq X \leq 10^9

All values in input are integers.

Partial Scores

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

Input

Input is given from Standard Input in the following format:

N X
x_1 x_2 ... x_{N}

Output

Print the answer.

Sample Input 1

2 100
1 10

Sample Output 1

355

Travel to position 10 by consuming 10 points of energy.

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

Travel to position 1 by consuming 36 points of energy.

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

Travel to position 0 by consuming 9 points of energy.

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

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

Sample Input 2

5 1
1 999999997 999999998 999999999 1000000000

Sample Output 2

19999999983

Sample Input 3

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

Sample Output 3

150710136

Sample Input 4

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

Sample Output 4

3256017715


var
  s:ansistring;
  a:array[1..1000000]of ansistring;
  i,j,m,n,p:longint;
  k:qword;
procedure search(st:string);
var
  i,j:longint;
begin
  if length(st)=1 then exit;
  for i:=1 to length(st)-1 do
  begin
     if st[i]<>st[i+1] then continue;
	 if st[i]='a' then
	 begin
	 delete(st,i,2);
	 insert('b',st,i);
	 for j:=1 to p+1 do if a[j]=st then break;
	 if j=p+1 then begin
	 inc(k);
	 inc(p);
	 a[p]:=st;
	 search(st);
	 end;
	 delete(st,i,1);
	 insert('aa',st,i);
	 continue;
	 end;
	 if st[i]='b' then
	 begin
	 delete(st,i,2);
	 insert('a',st,i);
	 for j:=1 to p+1 do if a[j]=st then break;
	 if j=p+1 then begin
	 inc(k);
	 inc(p);
	 a[p]:=st;
	 search(st);
	 end;
	 search(st);
	 delete(st,i,1);
	 insert('bb',st,i);
	 end;
  end;
end;
begin
  readln(s);
  k:=1;
  search(s);
  writeln(k);
end.

Score : 1300 points

Problem Statement

There is a string s consisting of a and b.
Snuke can perform the following two kinds of operation any number of times in any order:

Choose an occurrence of aa as a substring, and replace it with b.

Choose an occurrence of bb as a substring, and replace it with a.

How many strings s can be obtained by this sequence of operations?
Find the count modulo 10^9 + 7.

Constraints

1 \leq |s| \leq 10^5

s consists of a and b.

Input

Input is given from Standard Input in the following format:

s

Output

Print the number of strings s that can be obtained, modulo 10^9 + 7.

Sample Input 1

aaaa

Sample Output 1

6

Six strings can be obtained:

aaaa

aab

aba

baa

bb

a

Sample Input 2

aabb

Sample Output 2

5

Five strings can be obtained:

aabb

aaa

bbb

ab

ba

Sample Input 3

ababababa

Sample Output 3

1

Snuke cannot perform any operation.

Sample Input 4

babbabaaba

Sample Output 4

35


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

Score : 400 points

Problem Statement

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

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

You can perform the following operation any number of times:

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

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

Constraints

All values in input are integers.

1 \leq H, W \leq 500

0 \leq a_{ij} \leq 9

Input

Input is given from Standard Input in the following format:

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

Output

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

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

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

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

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

Sample Input 1

2 3
1 2 3
0 1 1

Sample Output 1

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

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

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

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

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

Sample Input 2

3 2
1 0
2 1
1 0

Sample Output 2

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

Sample Input 3

1 5
9 9 9 9 9

Sample Output 3

2
1 1 1 2
1 3 1 4


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


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


uses
	SysUtils,
	Classes;

procedure MainProc;
	var
		text : string;
		strlist : TStringList;
		x1, y1 : Integer;
		x2, y2 : Integer;
		x3, y3: Integer;
		x4, y4: Integer;
		edgeLenX: Integer;
		edgeLenY: Integer;
	begin
	ReadLn(text);
	strlist := TStringList.Create;
	try
		strlist.Delimiter := ' ';
		strlist.DelimitedText := text;
		x1 := StrToInt(strlist.Strings[0]);
		y1 := StrToInt(strlist.Strings[1]);
		x2 := StrToInt(strlist.Strings[2]);
		y2:= StrToInt(strlist.Strings[3]);
	finally
			FreeAndNil(strlist);
			end;
	edgeLenX := x1 - x2;
	edgeLenY := Abs(y1 - y2);

	x3 := x2 - edgeLenY;
	y3 := y2 - edgeLenX;
	x4 := x1 - edgeLenY;
	y4 := y1 - edgeLenX;
	text := IntTostr(x3) + ' ' + IntTostr(y3) + ' ' + IntTostr(x4) + ' ' + IntTostr(y4);
	WriteLn(text);
	end;

begin
MainProc;
end.

Score : 200 points

Problem Statement

There is a square in the xy-plane. The coordinates of its four vertices are (x_1,y_1),(x_2,y_2),(x_3,y_3) and (x_4,y_4) in counter-clockwise order.
(Assume that the positive x-axis points right, and the positive y-axis points up.)

Takahashi remembers (x_1,y_1) and (x_2,y_2), but he has forgot (x_3,y_3) and (x_4,y_4).

Given x_1,x_2,y_1,y_2, restore x_3,y_3,x_4,y_4. It can be shown that x_3,y_3,x_4 and y_4 uniquely exist and have integer values.

Constraints

|x_1|,|y_1|,|x_2|,|y_2| \leq 100

(x_1,y_1) ≠ (x_2,y_2)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2

Output

Print x_3,y_3,x_4 and y_4 as integers, in this order.

Sample Input 1

0 0 0 1

Sample Output 1

-1 1 -1 0

(0,0),(0,1),(-1,1),(-1,0) is the four vertices of a square in counter-clockwise order.
Note that (x_3,y_3)=(1,1),(x_4,y_4)=(1,0) is not accepted, as the vertices are in clockwise order.

Sample Input 2

2 3 6 6

Sample Output 2

3 10 -1 7

Sample Input 3

31 -41 -59 26

Sample Output 3

-126 -64 -36 -131


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

Score : 300 points

Problem Statement

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

Constraints

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

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

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

Sample Input 1

3 2

Sample Output 1

9

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

Sample Input 2

5 3

Sample Output 2

1

Sample Input 3

31415 9265

Sample Output 3

27

Sample Input 4

35897 932

Sample Output 4

114191


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


var
	n,k,s,a:int64;
begin
	read(n,k);
	a:=n div k;
	inc(s,a*a*a);
	if k mod 2=0 then begin
		a:=(n-k div 2)div k+1;
		inc(s,a*a*a);
	end;
	write(s);
end.


VAR I,J,T:qword;
    N,K,COUNT:qword;

begin
 readln(n,k);
 count:=0;
 for i:=1 to n do
  for j:=1 to n do
   for t:=1 to n do
    IF ((i+j) mod k=0) and ((t+j) mod k=0) and ((i+t) mod k=0) then inc(count);
  WRITELN(count);
end.


VAR I,J,T:LONGINT;
    N,K,COUNT:LONGINT;

begin
 readln(n,k);
 count:=0;
 for i:=1 to n do
  for j:=1 to n do
   for t:=1 to n do
    IF ((i+j) mod k=0) and ((t+j) mod k=0) and ((i+t) mod k=0) then inc(count);
  WRITELN(count);
end.


var
	u,L,i,a,c:Longint;
begin
	read(L);
	u:=L;
	while u>0 do begin
		inc(c);
		inc(a,u mod 2);
		u:=u div 2;
	end;
	writeln(c);writeln(c*2-3+a);
	a:=1;
	u:=L;
	for i:=1 to c-1 do begin
		writeln(i);
		writeln(i+1);
		writeln(0);
		writeln(i);
		writeln(i+1);
		writeln(a);
		if L mod 2>0 then begin
			writeln(i);
			writeln(c);
			u:=u-a;
			writeln(u);
		end;
		L:=L div 2;
		a:=a*2;
	end;
end.

Score : 700 points

Problem Statement

You are given an integer L. Construct a directed graph that satisfies the conditions below. The graph may contain multiple edges between the same pair of vertices. It can be proved that such a graph always exists.

The number of vertices, N, is at most 20. The vertices are given ID numbers from 1 to N.

The number of edges, M, is at most 60. Each edge has an integer length between 0 and 10^6 (inclusive).

Every edge is directed from the vertex with the smaller ID to the vertex with the larger ID. That is, 1,2,...,N is one possible topological order of the vertices.

There are exactly L different paths from Vertex 1 to Vertex N. The lengths of these paths are all different, and they are integers between 0 and L-1.

Here, the length of a path is the sum of the lengths of the edges contained in that path, and two paths are considered different when the sets of the edges contained in those paths are different.

Constraints

2 \leq L \leq 10^6

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

In the first line, print N and M, the number of the vertices and edges in your graph.
In the i-th of the following M lines, print three integers u_i,v_i and w_i, representing the starting vertex, the ending vertex and the length of the i-th edge.
If there are multiple solutions, any of them will be accepted.

Sample Input 1

4

Sample Output 1

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

In the graph represented by the sample output, there are four paths from Vertex 1 to N=8:

1 → 2 → 3 → 4 → 8 with length 0

1 → 2 → 3 → 7 → 8 with length 1

1 → 2 → 6 → 7 → 8 with length 2

1 → 5 → 6 → 7 → 8 with length 3

There are other possible solutions.

Sample Input 2

5

Sample Output 2

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


{ Free PascalのHello, world.(日本語版) }

program Hello;

{$MODE OBJFPC}
{$CODEPAGE UTF8}
{$LONGSTRINGS ON}

uses
  SysUtils,
  Classes;

procedure MainFunc;
	procedure GetInput(var Length : Integer; var Num : Integer);
		var
			str : string;
			strlist : TStringList;
		begin
			Readln(str);
			strlist := TStringList.Create;
			try
				strlist.Delimiter := ' ';
				strlist.DelimitedText := str;
				Length := StrToInt(strlist.Strings[0]);
				Num := StrToInt(strlist.Strings[1]);
			finally
				FreeAndNil(strlist);
				end;
		end;
	var
		trainLength : Integer;
		trainNum : Integer;
	begin
		GetInput(trainLength, trainNum);
		WriteLn(trainLength - trainNum + 1);
	end;

begin
	MainFunc;
end.

Score : 100 points

Problem Statement

There is an N-car train.

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

Constraints

1 \leq N \leq 100

1 \leq i \leq N

Input

Input is given from Standard Input in the following format:

N i

Output

Print the answer.

Sample Input 1

4 2

Sample Output 1

3

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

Sample Input 2

1 1

Sample Output 2

1

Sample Input 3

15 11

Sample Output 3

5


uses
  SysUtils,
  Classes;

 function WhiteCheckFunc(const str : string) : Boolean;
	var
		i : Integer;
	begin
	Result := True;
	for i := 0 to Length(str) - 1 do
		begin
		if str[i] = '#' then
			Result := False;
		end;
	end;
 procedure subProc(const h : Integer; const w : Integer);
	var
		i, j : Integer;
		post_height : Integer;
		str : string;
		line : string;
	begin
	line := '';
	post_height := h;
	for i := 0 to h - 1 do
		begin
		ReadLn(str);
		if WhiteCheckFunc(str) then
			post_height := post_height - 1
		else
			line := line + str;
		end;
	for i := 1 to w do
		begin
		str := '';
		for j := 0 to post_height - 1 do
			str := str + line[i + j * w];
		if WhiteCheckFunc(str) then
			begin
				for j := 0 to post_height - 1 do
					line[i + j * w] := '_';
			end;
		end;
	for i  := 0 to post_height - 1 do
		begin
		for j := 1 to w do
			if not (line[j + i * w] = '_') then
				Write(line[j + i * w]);
		Write(#13#10);
		end;
	end;

procedure MainProc;
	var
		text : string;
		strlist : TStringList;
		heightNum : Integer;
		widthNum : Integer;
	begin
	ReadLn(text);
	strlist := TStringList.Create;
	try
		strlist.Delimiter := ' ';
		strlist.DelimitedText := text;
		heightNum := StrToInt(strlist.Strings[0]);
		widthNum := StrToInt(strlist.Strings[1]);
		finally
			FreeAndNil(strlist);
			end;
		subProc(heightNum, widthNum);
	end;

begin
MainProc;
end.

Score : 200 points

Problem Statement

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

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

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

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

Constraints

1 \leq H, W \leq 100

a_{i, j} is . or #.

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

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

###
###
.##

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

Sample Input 2

3 3
#..
.#.
..#

Sample Output 2

#..
.#.
..#

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

Sample Input 3

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

Sample Output 3

#

Sample Input 4

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

Sample Output 4

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


var
	s:array[1..100]of String[100];
	h,w,i,j,k:Longint;
	f:Boolean;
	b:array[1..100]of Boolean;
begin
	readln(h,w);
	for i:=1 to h do readln(s[i]);
	for j:=1 to w do begin
		b[j]:=false;
		for i:=1 to h do
			if s[i][j]='#' then begin
				b[j]:=true;
				break;
			end;
	end;
	for i:=1 to h do
		for j:=1 to w do
			if s[i][j]='#' then begin
				for k:=1 to w do
					if b[k] then write(s[i][k]);
				writeln('');
                break;
            end;
end.


var
	s:array[1..100]of String[100];
	h,w,i,j:Longint;
	f:Boolean;
	b:array[1..100]of Boolean;
begin
	readln(h,w);
	for i:=1 to h do readln(s[i]);
	for j:=1 to w do begin
		b[j]:=false;
		for i:=1 to h do
			if s[i][j]='#' then begin
				b[j]:=true;
				break;
			end;
	end;
	for i:=1 to h do begin
		f:=false;
		for j:=1 to w do
			if s[i][j]='#' then begin
				f:=true;
				break;
			end;
		if f then begin
			for j:=1 to w do
				if b[j] then write(s[i][j]);
			writeln('');
		end;
	end;
end.


var
	s:array[1..100]of String[100];
	h,w,i,j:Longint;
	a,b:array[1..100]of Boolean;
begin
	readln(h,w);
	for i:=1 to h do readln(s[i]);
	for i:=1 to h do begin
		a[i]:=false;
		for j:=1 to w do
			if s[i][j]='#' then begin
				a[i]:=true;
				break;
			end;
	end;
	for j:=1 to w do begin
		b[j]:=false;
		for i:=1 to h do
			if s[i][j]='#' then begin
				b[j]:=true;
				break;
			end;
	end;
	for i:=1 to h do
		if a[i] then begin
			for j:=1 to w do
				if b[j] then write(s[i][j]);
			writeln('');
		end;
end.


var
  i,j,p,q:longint;
  s,st:array[1..100,1..100]of char;
function check(a:longint):boolean;
var
  k,l,y:longint;
begin
  check:=false;
  for l:=1 to p do
  for k:=1 to q do if st[l,k]='#' then inc(y);
  if (y>a) then exit;
  check:=true;
end;
begin
  readln(p,q);
  for i:=1 to p do begin
  for j:=1 to q do read(s[i,j]);
  readln;
  end;
  st:=s;
  for i:=1 to p do
  begin
     if check(q) then break;
     s[i,q+1]:=' ';
	 for j:=1 to q do if (s[i,j]<>s[i,j+1]) then break;
	 if j=q then for j:=1 to q do st[i,j]:=' ';
  end;
  for i:=1 to q do
  begin
     if check(p) then break;
     s[p+1,i]:=' ';
	 for j:=1 to p do if (s[j,i]<>s[j+1,i]) then break;
	 if j=p then for j:=1 to p do st[j,i]:=' ';
  end;
  for i:=1 to p do
  begin
	for j:=1 to q+1 do if st[i,j]<>' ' then break;
	if j=q+1 then continue;
    for j:=1 to q do if st[i,j]<>' ' then write(st[i,j]);
	writeln;
  end;
end.


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

Score : 300 points

Problem Statement

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

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

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

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

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

Sample Output 4

10


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


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


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


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


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


var
  a,s:array[1..100000]of int64;
  k,n,i:longint;
  sum:int64;

function c(x,y:longint):longint;
var
  i:longint;
  s:real;
begin
  if x=0 then exit(1);
  if y=0 then exit(0);
  for i:=1 to x do
  begin
    s:=s*(y-i+1);
    s:=s/i;
  end;
  exit(trunc(x));
end;

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

Score : 700 points

Problem Statement

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

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

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

Snuke comes up with the following problem.

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

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the median of m.

Sample Input 1

3
10 30 20

Sample Output 1

30

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

The median of (10) is 10.

The median of (30) is 30.

The median of (20) is 20.

The median of (10, 30) is 30.

The median of (30, 20) is 30.

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

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

Sample Input 2

1
10

Sample Output 2

10

Sample Input 3

10
5 9 5 9 8 9 3 5 4 3

Sample Output 3

8


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


var
	N,K,ans,now,i:Longint;
	x:Array[1..100000]of Longint;
begin
	read(N,K);
	for i:=1 to N do read(x[i]);
	ans:=2000000000;
	for i:=1 to N-K+1 do begin
		now:=x[i+K-1]-x[i];
		if abs(x[i+K-1])>abs(x[i])then inc(now,abs(x[i]))else inc(now,abs(x[i+K-1]));
		if ans>now then ans:=now;
	end;
	writeln(ans);
end.


var
	n,c,NN:int64;
	i,l,r,m,s:Longint;
	a,b:array[1..200000]of Longint;
begin
	read(n);
	for i:=1 to n do begin
		read(a[i]);
		if r<a[i] then r:=a[i];
	end;
	l:=-1;
	while r-l>1 do begin
		for i:=1 to 2*n do b[i]:=0;
		m:=(l+r)div 2;
		s:=n;
		b[s]:=1;
		NN:=0;
		c:=0;
		for i:=1 to n do begin
			if a[i]>m then begin
				inc(s);
				dec(NN,b[s]);
			end else begin
				inc(NN,b[s]);
				dec(s);
			end;
			inc(c,NN);
			inc(b[s]);
		end;
		if c*4>n*(n+1) then r:=m else l:=m;
	end;
	writeln(r);
end.


{ Free PascalのHello, world.(日本語版) }

program Hello;

{$MODE OBJFPC}
{$CODEPAGE UTF8}
{$LONGSTRINGS ON}

uses
  SysUtils,
  Classes;

 procedure MainProc;
	var
		text : string;
		strlist : TStringList;
		horizon : Integer;
		vertical : Integer;
		answer : Integer;
	begin
	ReadLn(text);
	strlist := TStringList.Create;
	try
		strlist.Delimiter := ' ';
		strlist.DelimitedText := text;
		horizon := StrToInt(strlist.Strings[0]);
		vertical := StrToInt(strlist.Strings[1]);
		finally
			FreeAndNil(strlist);
			end;
		answer := (horizon - 1) * (vertical - 1);
		WriteLn(answer);
	end;

begin
MainProc;
end.

Score: 100 points

Problem Statement

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

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

Note

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

Constraints

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

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

Input

Input is given from Standard Input in the following format:

A B

Output

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

Sample Input 1

2 2

Sample Output 1

1

In this case, the area is 1 square yard.

Sample Input 2

5 7

Sample Output 2

24

In this case, the area is 24 square yards.


uses crt;
var a,b,c: integer;
begin
readln(a,b);
c:=a*b-(a+b-1);
write(c);
end.


uses crt;
var
        a,b,m:longint;
begin
        read(a,b);
        write(a*b-a-b+1);
        //readln;
        //readln
end.


var
  n,i,j,k,ans:longint;
begin
  readln(n);
  for i:=1 to n do
  begin
    k:=0;
    for j:=1 to i do
     if i mod j=0 then inc(k);
    if (k=8)and odd(i) then inc(ans);
  end;
  writeln(ans);
end.

Score: 200 points

Problem Statement

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

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

Print the count.

Sample Input 1

105

Sample Output 1

1

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

Sample Input 2

7

Sample Output 2

0

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


uses
  SysUtils,
  Classes;

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

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

begin
MainProc;
end.


VAR N,i,j,count,kq:INTEGER;
begin
 readln(n);
 i:=1;
 if n < 105 then writeln('0') else
 if n = 105 then writeln('1') else
 begin
   while i <= n do
    begin
    count:=0;
    for j:=1 to n do
     if i mod j = 0 then inc(Count);
    if count = 8 then inc(kq);
    inc(i,1);
    end;
  writeln(kq);
 end;
 end.


var c,n,i,j,m:Longint;
begin
	read(n);
	for i:=1 to n do begin
		m:=0;
		for j:=1 to i do begin
			if i mod j=0 then inc(m);
		end;
		if(i mod 2=1)and(m=8)then inc(c);
	end;
	write(c);
end.


uses crt;
var
        n,i,j,k,h:integer;
begin
        read(n);
        h:=0;
        for i:=1 to n do
            if i mod 2=1 then
               begin
                 k:=0;
                 for j:=1 to n do
                     if i mod j =0 then inc(k);
                 if k=8 then inc(h);
               end;
        write(h);
       // readln;
       // readln;
end.


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

       // readln;
      //  readln;
end.

Score: 300 points

Problem Statement

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

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

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

Constraints

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

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

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

Input

Input is given from Standard Input in the following format:

S
K

Output

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

Sample Input 1

1214
4

Sample Output 1

2

The string S changes as follows:

Now: 1214

After one day: 12214444

After two days: 1222214444444444444444

After three days: 12222222214444444444444444444444444444444444444444444444444444444444444444

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

Sample Input 2

3
157

Sample Output 2

3

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

Sample Input 3

299792458
9460730472580800

Sample Output 3

2


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

Score: 400 points

Problem Statement

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

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

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

Constraints

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

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

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

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

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

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

2 3 1
1 1
1 2
2 2
1 2

Sample Output 1

3

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

Sample Input 2

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

Sample Output 2

1
1

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

Sample Input 3

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

Sample Output 3

7
9
10
6
8
9
6
7
8
10


var
	a:array[0..510,0..510] of longint;
	i,j,k,s,n,m,q,x,y:longint;
begin
	readln(n,m,q);
	fillchar(a,sizeof(a),0);
	for i:=1 to m do
	begin
		read(x,y);
		for j:=y to n do inc(a[x,j]);
	end;
	for i:=1 to q do
	begin
		s:=0;
		read(x,y);
		for k:=x to y do s:=s+a[k,y];
		writeln(s);
	end;
end.


var
  n,k:longint;
begin
  readln(n,k);
  if n mod k = 0 then
    writeln(0)
  else
    writeln(1);
end.

Score : 100 points

Problem Statement

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

Constraints

1 \leq N,K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

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

Sample Input 1

7 3

Sample Output 1

1

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

Sample Input 2

100 10

Sample Output 2

0

The crackers can be distributed evenly.

Sample Input 3

1 1

Sample Output 3

0


uses
  SysUtils,
  Classes;

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

begin
MainProc;
end.


var n,i:Longint;
begin
read(n);
for i:=0 to n do begin
	if (i*4<=n) and ((n-i*4)mod 7 = 0) then begin
		writeln('Yes');
		exit;
	end;
end;
writeln('No');
end.

Score : 200 points

Problem Statement

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

Constraints

N is an integer between 1 and 100, inclusive.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

11

Sample Output 1

Yes

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

Sample Input 2

40

Sample Output 2

Yes

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

Sample Input 3

3

Sample Output 3

No

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


const
    key=99999989;
var
    n,m,i,j:longint;
    k:int64;
    ans:real;
    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.

Score : 400 points

Problem Statement

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

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

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 10^5

2 \leq M \leq 10^9

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

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

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

Sample Input 1

3 2
4 1 5

Sample Output 1

3

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

Sum for (1, 1): 4

Sum for (1, 2): 5

Sum for (1, 3): 10

Sum for (2, 2): 1

Sum for (2, 3): 6

Sum for (3, 3): 5

Among these, three are multiples of 2.

Sample Input 2

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

Sample Output 2

6

Sample Input 3

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

Sample Output 3

25


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


uses
  SysUtils,
  Classes;

 procedure mainProc;
	var
		r: Integer;
	begin
	readln(r);
	if  (r >= 2800) then
		writeln('AGC')
	else if  (r >= 1200) then
		writeln('ARC')
	else
		writeln('ABC');
	end;

begin
mainProc;
end.

Score : 100 points

Problem Statement

A programming competition site AtCode regularly holds programming contests.

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

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

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

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

Constraints

0 ≤ R ≤ 4208

R is an integer.

Input

Input is given from Standard Input in the following format:

R

Output

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

Sample Input 1

1199

Sample Output 1

ABC

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

Sample Input 2

1200

Sample Output 2

ARC

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

Sample Input 3

4208

Sample Output 3

AGC


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


uses
  SysUtils,
  Classes;

 function isLower(const value : string): boolean;
	const
		UPP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var
		upper : string;
		i : Integer;
	begin
	Result := True;
	upper := UPP;
	if Length(Value) <> 1 then
		Exit(False);
	for i := 1 to Length(upper) do
		if value = upper[i] then
			Exit(False);
	end;

 function judgeFunc(const Value : string) : string;
	const
		BAD = 'WA';
		OK = 'AC';
	var
		i : Integer;
		flag : Boolean;
	begin
	Result := OK;
	flag := False;
	i := 1;
	if not (Value[i] = 'A') then
		Exit(BAD);
	Inc(i);
	if  not isLower(Value[i]) then
		Exit(BAD);
	i := Length(Value);
	if not isLower(Value[i]) then
		Exit(BAD);
	for i := 3 to Length(Value) - 1 do
		begin
			if Value[i] = 'C' then
				begin
				if flag then
					Exit(BAD)
				else
					flag := True;
				end
			else if not isLower(Value[i]) then
				Exit(BAD);
		end;
	if not flag then
		Result := BAD;
	end;

procedure MainProc;
	var
		aText : string;
		pushText : string;
	begin
	ReadLn(aText);
	pushText := judgeFunc(aText);
	writeLn(pushText);
	end;

begin
MainProc;
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
	n,g,a,b,i,j,k,l,s,max:longint;
	f,t:array[0..10010] 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.

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 goal:longint;
n,i,best:integer;
p:array[1..10]of integer;
c:array[1..10]of longint;
procedure dfs(d,x:integer;t:longint);
var j:integer;
begin
	if t>=goal then
	begin
		best:=x;
		exit;
	end;
	if d>n then exit;
	j:=0;
	while (j<=p[d]-1) and (x+j<best) do begin  dfs(d+1,x+j,t+100*j*d);inc(j);end;
	if x+p[d]<best then dfs(d+1,x+p[d],t+100*d*p[d]+c[d]);
end;
begin
	read(n);read(goal);
	for i:=1 to n do read(p[i],c[i]);
	best:=maxint;
	dfs(1,0,0);
	write(best);
end.


var goal:longint;
n,i,best:integer;
p:array[1..10]of integer;
c:array[1..10]of longint;
procedure dfs(d,x:integer;t:longint);
var j:integer;
begin
  if t>=goal then
  begin
    best:=x;
    exit;
  end;
  if d>n then exit;
 for j:=0 to p[d]-1 do if x+j<best then dfs(d+1,x+j,t+100*j*d) else exit;
if x+p[d]<best then dfs(d+1,x+p[d],t+100*d*p[d]+c[d]);
end;
begin
	read(n);read(goal);
	for i:=1 to n do read(p[i],c[i]);
	best:=10000;
	dfs(1,0,0);
	write(best);
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.


Score : 400 points

Problem Statement

The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:

1 ≤ i < j < k ≤ |T| (|T| is the length of T.)

T_i = A (T_i is the i-th character of T from the beginning.)

T_j = B

T_k = C

For example, when T = ABCBC, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.

You are given a string S. Each character of S is A, B, C or ?.

Let Q be the number of occurrences of ? in S. We can make 3^Q strings by replacing each occurrence of ? in S with A, B or C. Find the sum of the ABC numbers of all these strings.

This sum can be extremely large, so print the sum modulo 10^9 + 7.

Constraints

3 ≤ |S| ≤ 10^5

Each character of S is A, B, C or ?.

Input

Input is given from Standard Input in the following format:

S

Output

Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.

Sample Input 1

A??C

Sample Output 1

8

In this case, Q = 2, and we can make 3^Q = 9 strings by by replacing each occurrence of ? with A, B or C. The ABC number of each of these strings is as follows:

AAAC: 0

AABC: 2

AACC: 0

ABAC: 1

ABBC: 2

ABCC: 2

ACAC: 0

ACBC: 1

ACCC: 0

The sum of these is 0 + 2 + 0 + 1 + 2 + 2 + 0 + 1 + 0 = 8, so we print 8 modulo 10^9 + 7, that is, 8.

Sample Input 2

ABCBC

Sample Output 2

3

When Q = 0, we print the ABC number of S itself, modulo 10^9 + 7. This string is the same as the one given as an example in the problem statement, and its ABC number is 3.

Sample Input 3

????C?????B??????A???????

Sample Output 3

979596887

In this case, the sum of the ABC numbers of all the 3^Q strings is 2291979612924, and we should print this number modulo 10^9 + 7, that is, 979596887.


uses
  SysUtils,
  Classes;

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

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

	for i := 0 to 2 do
		begin
		for j := 2 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[2] - atype[0];
		writeLn(t);
end.

Score : 100 points

Problem Statement

You have three tasks, all of which need to be completed.

First, you can complete any one task at cost 0.

Then, just after completing the i-th task, you can complete the j-th task at cost |A_j - A_i|.

Here, |x| denotes the absolute value of x.

Find the minimum total cost required to complete all the task.

Constraints

All values in input are integers.

1 \leq A_1, A_2, A_3 \leq 100

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

Print the minimum total cost required to complete all the task.

Sample Input 1

1 6 3

Sample Output 1

5

When the tasks are completed in the following order, the total cost will be 5, which is the minimum:

Complete the first task at cost 0.

Complete the third task at cost 2.

Complete the second task at cost 3.

Sample Input 2

11 5 5

Sample Output 2

6

Sample Input 3

100 100 100

Sample Output 3

0


uses
  SysUtils,
  Classes;

function CheckFunc(strS : String; const strT : String) : Boolean;
	var
		count : Integer;
		str : String;
	begin
	Result := False;
	count := 1;
	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 i,s,n:longint;
    A:Array[1..4000] of longint;
begin
 readln(n);
 for i:=1 to n do
  begin
   read(A[i]);
   s:=s+A[i];
  end;
 writeln(s-n);
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


uses
  SysUtils,
  Classes;

var
	a, i, total : 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;
	total := 0;
	for i := 0 to a - 1 do
		begin
		total := total + (atype[i] - 1);
		end;
		writeLn(total);
end.


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


var
  n:integer;
  answer:integer;
begin
  readln(n);
  answer := n;
  while (answer mod n > 0) or (answer mod 2 > 0) do answer := answer + 1;
  write(answer);
end.

Score : 100 points

Problem Statement

You are given a positive integer N.
Find the minimum positive integer divisible by both 2 and N.

Constraints

1 \leq N \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum positive integer divisible by both 2 and N.

Sample Input 1

3

Sample Output 1

6

6 is divisible by both 2 and 3.
Also, there is no positive integer less than 6 that is divisible by both 2 and 3.
Thus, the answer is 6.

Sample Input 2

10

Sample Output 2

10

Sample Input 3

999999999

Sample Output 3

1999999998


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 := 1 to a - 2 do
		begin
		for j := a - 2 downto i 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.

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


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]);
    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


{$R-,S-,Q-,I-,O+}
var n,i,sad,l,r,m1,m2,temp1,temp2:int64;
    a:array[1..200000] of int64;

function f(b:int64):int64;
var i,ret:int64;
begin
  ret:=0;
  for i:=1 to n do inc(ret,abs(a[i]-b));
  f:=ret;
end;

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

begin
  read(n);
  for i:=1 to n do begin read(a[i]);dec(a[i],i);end;
  r:=1000000000;
  l:=-1000000;
  sad:=min(f(l),f(r));
  while l<=r do begin
    m1:=(2*l+r) div 3;
    temp1:=f(m1);
    m2:=(l+2*r) div 3;
    temp2:=f(m2);
    if temp1 < temp2 then begin
      if sad>temp1 then sad:=temp1;
      r:=m2-1;
    end else begin
      if sad>temp2 then sad:=temp2;
      l:=m1+1;
    end;
  end;
  writeln(sad);
end.