var n,ans,num:int64;
var i:longint;
var a:array[0..100020] of int64;
procedure qsort(l,r:int64);
var i,j,mid,t:int64;
begin
  i:=l;j:=r;mid:=a[(l+r)>>1];
  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
  read(n);
  for i:=1 to n do read(a[i]);qsort(1,n);
  i:=1;
  while i<=n do
  begin
    num:=1;
    inc(ans);
    while (a[i]=a[i+1]) and (i<=n) do begin inc(i);inc(num);end;
    if num mod 2=0 then dec(ans);
    inc(i);
  end;
  writeln(ans);
end.


var i,j,n,k,s:longint;
	a:array[-5..100050]of longint;

procedure kp(l,r:longint);
var c,d,e,f:longint;
begin
	c:=l;d:=r;e:=a[(l+r) div 2];
	repeat
		while a[c]<e do inc(c);
		while a[d]>e do dec(d);
		if c<=d then begin
			f:=a[c];a[c]:=a[d];a[d]:=f;
			inc(c);
			dec(d);
		end;
	until c>d;
	if c<r then kp(c,r);
	if l<d then kp(l,d);
end;
begin
	readln(n);
	for i:=1 to n do readln(a[i]);
	kp(1,n);
	s:=0;i:=0;
	repeat
		inc(i);k:=a[i];j:=1;
		while a[i+1]=k do begin
			inc(j);inc(i);
		end;
		if j mod 2=0 then else inc(s);
	until i=n;
	writeln(s);
end.

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

Score : 300 points

Problem Statement

You are playing the following game with Joisino.

Initially, you have a blank sheet of paper.

Joisino announces a number. If that number is written on the sheet, erase the number from the sheet; if not, write the number on the sheet. This process is repeated N times.

Then, you are asked a question: How many numbers are written on the sheet now?

The numbers announced by Joisino are given as A_1, ... ,A_N in the order she announces them. How many numbers will be written on the sheet at the end of the game?

Constraints

1≤N≤100000

1≤A_i≤1000000000(=10^9)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print how many numbers will be written on the sheet at the end of the game.

Sample Input 1

3
6
2
6

Sample Output 1

1

The game proceeds as follows:

6 is not written on the sheet, so write 6.

2 is not written on the sheet, so write 2.

6 is written on the sheet, so erase 6.

Thus, the sheet contains only 2 in the end. The answer is 1.

Sample Input 2

4
2
5
5
2

Sample Output 2

0

It is possible that no number is written on the sheet in the end.

Sample Input 3

6
12
22
16
22
18
12

Sample Output 3

2

Submitted source-code file size, in bytes.

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

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


var n,ans:int64;
var i:longint;
var a:array[0..100020] of int64;
procedure qsort(l,r:int64);
var i,j,mid,t:int64;
begin
  i:=l;j:=r;mid:=a[(l+r)>>1];
  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
  read(n);
  for i:=1 to n do read(a[i]);qsort(1,n);
  i:=1;
  while i<=n do
  begin
    inc(ans);if a[i]=a[i+1] then dec(ans);
    while (a[i]=a[i+1]) and (i<=n) do inc(i);
    inc(i);
  end;
  writeln(ans);
end.


var min,n,i,j,x,m,r,a1,b1,c1:longint;
	a:array[-5..205,-5..205] of longint;
	b:array[-5..205]of longint;
	f:array[-5..205]of boolean;
procedure main(s,pre,now:longint);
var i,j,k:longint;
begin
	if now=r then begin
		for i:=1 to r do if f[b[i]] then begin
			if s+a[pre,b[i]]<min then min:=s+a[pre,b[i]];
			break;
		end;
	end else
	for i:=1 to r do if f[b[i]] then begin
		f[b[i]]:=false;
		main(s+a[pre,b[i]],b[i],now+1);
		f[b[i]]:=true;
	end;
end;

begin
	fillchar(f,sizeof(f),true);
	readln(n,m,r);
	for i:=1 to r do read(b[i]);
	for i:=1 to m do begin
		readln(a1,b1,c1);
		a[a1,b1]:=c1;
		a[b1,a1]:=c1;
	end;

	for x:=1 to n do for i:=1 to n do for j:=1 to n do
	if a[i,j]>a[i,x]+a[x,j] then a[i,j]:=a[i,x]+a[x,j];

	min:=maxlongint div 2;
	for i:=1 to r do begin
		f[b[i]]:=false;
		main(0,b[i],2);
		f[b[i]]:=true;
	end;
	writeln(min);
end.

Score : 400 points

Problem Statement

There are N towns in the State of Atcoder, connected by M bidirectional roads.

The i-th road connects Town A_i and B_i and has a length of C_i.

Joisino is visiting R towns in the state, r_1,r_2,..,r_R (not necessarily in this order).

She will fly to the first town she visits, and fly back from the last town she visits, but for the rest of the trip she will have to travel by road.

If she visits the towns in the order that minimizes the distance traveled by road, what will that distance be?

Constraints

2≤N≤200

1≤M≤N×(N-1)/2

2≤R≤min(8,N) (min(8,N) is the smaller of 8 and N.)

r_i≠r_j (i≠j)

1≤A_i,B_i≤N, A_i≠B_i

(A_i,B_i)≠(A_j,B_j),(A_i,B_i)≠(B_j,A_j) (i≠j)

1≤C_i≤100000

Every town can be reached from every town by road.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M R
r_1 ... r_R
A_1 B_1 C_1
:
A_M B_M C_M

Output

Print the distance traveled by road if Joisino visits the towns in the order that minimizes it.

Sample Input 1

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

Sample Output 1

2

For example, if she visits the towns in the order of 1, 2, 3, the distance traveled will be 2, which is the minimum possible.

Sample Input 2

3 3 2
1 3
2 3 2
1 3 6
1 2 2

Sample Output 2

4

The shortest distance between Towns 1 and 3 is 4. Thus, whether she visits Town 1 or 3 first, the distance traveled will be 4.

Sample Input 3

4 6 3
2 3 4
1 2 4
2 3 3
4 3 1
1 4 1
4 2 2
3 1 6

Sample Output 3

3


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

Score : 200 points

Problem Statement

You are given a string s consisting of lowercase English letters. Extract all the characters in the odd-indexed positions and print the string obtained by concatenating them. Here, the leftmost character is assigned the index 1.

Constraints

Each character in s is a lowercase English letter.

1≤|s|≤10^5

Input

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

s

Output

Print the string obtained by concatenating all the characters in the odd-numbered positions.

Sample Input 1

atcoder

Sample Output 1

acdr

Extract the first character a, the third character c, the fifth character d and the seventh character r to obtain acdr.

Sample Input 2

aaaa

Sample Output 2

aa

Sample Input 3

z

Sample Output 3

z

Sample Input 4

fukuokayamaguchi

Sample Output 4

fkoaaauh


var
f:string;
i:longint;
begin
read(f);
  for i:=1 to length(f) do
      if i mod 2<>0 then writeln(f[i]);
end.


var
a:array [1..1000] of char;
f:string;
i:longint;
begin
read(f);
  for i:=1 to length(f) do
    begin
      a[i]:=f[i];
      if i mod 2<>0 then writeln(a[i]);
    end;
end.


var
 s:ansistring;
 i:longint;
begin
 readln(s);
 for i:=1 to length(s) div 2 do
  write(s[(i-1)*2+1]);writeln;
end.


var
 ch,c:char;
begin
 while not eoln do begin
  read(ch);
  read(c);
  write(ch);
 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,t:Integer;
  l:array of Integer;
  s,s123,s1,s2,s3:string;
  tf:Boolean;
  //nyu:TStringList;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(s);

    for i := 1 to (Length(s)+1) div 2 do
      Write(s[2*i-1]);

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


var cc : ansistring ;
      a,b,c,d,e,f,g : longint ;
begin
 readln(cc);
for a:=1 to length(cc) do
 if a mod 2 = 1 then
 write(cc[a]);
writeln;
end.


var s:ansistring;
var i:longint;
begin
readln(s);for i:=1 to length(s) do if i mod 2=1 then write(s[i]);writeln;end.


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

Score : 300 points

Problem Statement

You are given an integer sequence of length N, a_1,a_2,...,a_N.

For each 1≤i≤N, you have three choices: add 1 to a_i, subtract 1 from a_i or do nothing.

After these operations, you select an integer X and count the number of i such that a_i=X.

Maximize this count by making optimal choices.

Constraints

1≤N≤10^5

0≤a_i<10^5 (1≤i≤N)

a_i is an integer.

Input

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

N
a_1 a_2 .. a_N

Output

Print the maximum possible number of i such that a_i=X.

Sample Input 1

7
3 1 4 1 5 9 2

Sample Output 1

4

For example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4, the maximum possible count.

Sample Input 2

10
0 1 2 3 4 5 6 7 8 9

Sample Output 2

3

Sample Input 3

1
99999

Sample Output 3

1


program ec12;
var
	n,m,i,j,x,max:longint;
	a:array[0..100002] of longint;
begin
	fillchar(a,sizeof(a),0);
	readln(n);
	for i:=1 to n do
	begin
		read(x);
		inc(a[x]);
	end;
	max:=a[0]+a[1];
	for i:=1 to 100000 do
	begin
		if max<a[i]+a[i-1]+a[i+1] then
		max:=a[i]+a[i-1]+a[i+1];
	end;
	writeln(max);
end.


var cc : array [-1..100001] of longint ;
    max,n,a,b,c,d,e,f,g : longint ;
begin
 readln(n);
 for a:=1 to n do
  begin
   read(g);
   cc[g-1]:=cc[g-1]+1;
   cc[g]:=cc[g]+1;
   cc[g+1]:=cc[g+1]+1;
  end;
 for a:=-1 to 100001 do
  if cc[a]>max then
   max:=cc[a];
 writeln(max);
end.


var a:array[0..100050]of longint;
    ans:longint;
    i:longint;
    n:longint;
    mx,x:longint;
function max(a,b:longint):longint;
begin
  if a>b then exit(a) else exit(b);
end;
begin
  read(n);
  for i:=1 to n do
  begin
    read(x);
    inc(a[x]);
    if x>mx then mx:=x;
  end;
  ans:=a[0]+a[1];
  for i:=1 to mx do
  ans:=max(ans,a[i-1]+a[i]+a[i+1]);
  writeln(ans);
end.


var cc : array [1..100001] of longint ;
    max,n,a,b,c,d,e,f,g : longint ;
begin
 readln(n);
 for a:=1 to n do
  begin
   read(cc[a]);
  end;
 for a:=1 to n do
  begin
   if cc[a]=a then
    b:=b+1
   else
    begin
     max:=max+((b+1)div 2);
     b:=0;
    end;
  end;
 if b>0 then
  max:=max+((b+1)div 2);
 writeln(max);
end.

Score : 400 points

Problem Statement

You are given a permutation p_1,p_2,...,p_N consisting of 1,2,..,N.
You can perform the following operation any number of times (possibly zero):

Operation: Swap two adjacent elements in the permutation.

You want to have p_i ≠ i for all 1≤i≤N.
Find the minimum required number of operations to achieve this.

Constraints

2≤N≤10^5

p_1,p_2,..,p_N is a permutation of 1,2,..,N.

Input

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

N
p_1 p_2 .. p_N

Output

Print the minimum required number of operations

Sample Input 1

5
1 4 3 5 2

Sample Output 1

2

Swap 1 and 4, then swap 1 and 3. p is now 4,3,1,5,2 and satisfies the condition.
This is the minimum possible number, so the answer is 2.

Sample Input 2

2
1 2

Sample Output 2

1

Swapping 1 and 2 satisfies the condition.

Sample Input 3

2
2 1

Sample Output 3

0

The condition is already satisfied initially.

Sample Input 4

9
1 2 4 9 5 8 7 3 6

Sample Output 4

3


var n,i,j,ans,num,maxi:longint;
var a,b:array[0..100020] of longint;
begin
  read(n);for i:=1 to n do begin read(a[i]);inc(b[a[i]]);inc(b[a[i]+1]);
  inc(b[a[i]-1]);end;
ans:=0;
  for i:=0 to 100000 do
 if b[i]>ans then ans:=b[i];
  writeln(ans);{23333333333333333}
end.


uses Math;
var A,B,C,D,N:int64;
begin
read(A,B,C,D,N);
B:=Min(B,2*A);
C:=Min(C,2*B);
D:=Min(D,2*C);
writeln(N div 2*D+N mod 2*C);
end.

Score : 300 points

Problem Statement

You've come to your favorite store Infinitesco to buy some ice tea.

The store sells ice tea in bottles of different volumes at different costs.
Specifically, a 0.25-liter bottle costs Q yen, a 0.5-liter bottle costs H yen, a 1-liter bottle costs S yen, and a 2-liter bottle costs D yen.
The store has an infinite supply of bottles of each type.

You want to buy exactly N liters of ice tea. How many yen do you have to spend?

Constraints

1 \leq Q, H, S, D \leq 10^8

1 \leq N \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q H S D
N

Output

Print the smallest number of yen you have to spend to buy exactly N liters of ice tea.

Sample Input 1

20 30 70 90
3

Sample Output 1

150

Buy one 2-liter bottle and two 0.5-liter bottles. You'll get 3 liters for 90 + 30 + 30 = 150 yen.

Sample Input 2

10000 1000 100 10
1

Sample Output 2

100

Even though a 2-liter bottle costs just 10 yen, you need only 1 liter.
Thus, you have to buy a 1-liter bottle for 100 yen.

Sample Input 3

10 100 1000 10000
1

Sample Output 3

40

Now it's better to buy four 0.25-liter bottles for 10 + 10 + 10 + 10 = 40 yen.

Sample Input 4

12345678 87654321 12345678 87654321
123456789

Sample Output 4

1524157763907942


var cc : array ['a'..'z',0..200000] of longint ;
    a,b,c,d,e,f,g,h,i,j : longint ;
    so :ansistring ;
    gg : char ;
    sum : int64 ;
begin
 readln(so);
 j:=length(so);
 for a:=1 to j do
  cc[so[a],a]:=cc[so[a],a]+1;
 for gg:='a' to 'z' do
  for a:=1 to j do
   begin
    cc[gg,a]:=cc[gg,a]+cc[gg,a-1];
   end;
  for a:=2 to j do
   begin
    sum:=sum+(a-(cc[so[a],a]-1)-1);
 //   writeln(cc[so[a],a],' ',cc[so[a],a-1]);
   end;
  writeln(sum+1);
end.

Score : 500 points

Problem Statement

You have a string A = A_1 A_2 ... A_n consisting of lowercase English letters.

You can choose any two indices i and j such that 1 \leq i \leq j \leq n and reverse substring A_i A_{i+1} ... A_j.

You can perform this operation at most once.

How many different strings can you obtain?

Constraints

1 \leq |A| \leq 200,000

A consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

A

Output

Print the number of different strings you can obtain by reversing any substring in A at most once.

Sample Input 1

aatt

Sample Output 1

5

You can obtain aatt (don't do anything), atat (reverse A[2..3]), atta (reverse A[2..4]), ttaa (reverse A[1..4]) and taat (reverse A[1..3]).

Sample Input 2

xxxxxxxxxx

Sample Output 2

1

Whatever substring you reverse, you'll always get xxxxxxxxxx.

Sample Input 3

abracadabra

Sample Output 3

44


var
        a:array[0..1000000000]of byte;
        i,n,c,max,m:longint;
        flag:boolean;
begin
        fillchar(a,sizeof(a),0);max:=0;
        read(n);
        for i:=1 to n do
        begin
                read(c);
                if c>max then max:=c;
                if (a[c]<=4)then inc(a[c]);
        end;
        flag:=true;m:=0;
        for i:=max downto 1 do
        begin
                if (a[i]>=4)and(m=0)then begin writeln(i*i);halt;end;
                if (a[i]>=2) then if m=0 then m:=i
                                         else begin writeln(m*i);halt;end;
        end;
        writeln(0);
end.

Score : 300 points

Problem Statement

We have N sticks with negligible thickness.
The length of the i-th stick is A_i.

Snuke wants to select four different sticks from these sticks and form a rectangle (including a square), using the sticks as its sides.
Find the maximum possible area of the rectangle.

Constraints

4 \leq N \leq 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible area of the rectangle.
If no rectangle can be formed, print 0.

Sample Input 1

6
3 1 2 4 2 1

Sample Output 1

2

1 \times 2 rectangle can be formed.

Sample Input 2

4
1 2 3 4

Sample Output 2

0

No rectangle can be formed.

Sample Input 3

10
3 3 3 3 4 4 4 5 5 5

Sample Output 3

20


var a:array[0..100020] of int64;
var n,ans,x,y,num:int64;
var i:longint;
procedure qsort(l,r:int64);
var i,j,mid,t:int64;
begin
  i:=l;j:=r;mid:=a[(l+r)>>1];
  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
  read(n);
  for i:=1 to n do read(a[i]);qsort(1,n);
  i:=0;
  while i<n do
  begin
    inc(i);if a[i]=a[i+1] then inc(i);
    if a[i-1]=a[i] then begin if x=0 then x:=a[i] else y:=a[i];end;
    if y>0 then break;
  end;
  writeln(x*y);
end.


var a:array[0..100020] of int64;
var n,ans,x,y,num:int64;
var i:longint;
procedure qsort(l,r:int64);
var i,j,mid,t:int64;
begin
  i:=l;j:=r;mid:=a[(l+r)>>1];
  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
  read(n);
  for i:=1 to n do read(a[i]);qsort(1,n);
  i:=0;
  while i<n do
  begin
    inc(i);while a[i]=a[i+1] do inc(i);
    if a[i-1]=a[i] then begin if x=0 then x:=a[i] else y:=a[i];end;
    if y>0 then break;
  end;
  writeln(x*y);
end.


program ec12;
var
	a,b,site:array[1..100001] of int64;
	j:int64;
	n,m,i,max,tot:longint;
procedure qsrt(l,r:longint);
var
	s,v:int64;
	i,j:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	qsrt(1,n);
	m:=0;
	a[n+1]:=0;
	for i:=n downto 1 do
	begin
		if a[i]=a[i+1] then
		begin
			if m=0 then
			begin
				m:=1;
				j:=a[i];
				a[i]:=-1;
			end
			else
			begin
				writeln(j*a[i]);
				halt;
			end;
		end;
	end;
	writeln(0);
end.


program ec12;
var
	a,b,site:array[1..100001] of int64;
	j:int64;
	n,m,i,max,tot:longint;
procedure qsrt(l,r:longint);
var
	s,v:int64;
	i,j:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	qsrt(1,n);
	tot:=1;
	site[1]:=1;
	b[1]:=a[n];
	for i:=n-1 downto 1 do
	begin
		if a[i]=b[tot] then
		inc(site[tot])
		else
		begin
			inc(tot);
			b[tot]:=a[i];
			site[tot]:=1;
		end;
	end;
	j:=-1000;
	if site[1]=4 then
	begin
		writeln(b[1]*b[1]);
		halt;
	end;
	for i:=1 to tot do
	begin
		if site[i]>=2 then
		begin
			if j=-1000 then
			begin
				j:=b[i];
				continue;
			end
			else
			begin
				writeln(j*b[i]);
				halt;
			end;
		end;
	end;
	writeln(0);
end.


program ec12;
var
	a,b,site:array[1..100001] of int64;
	j:int64;
	n,m,i,max,tot:longint;
procedure qsrt(l,r:longint);
var
	s,v:int64;
	i,j:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	qsrt(1,n);
	tot:=1;
	site[1]:=1;
	b[1]:=a[n];
	for i:=n-1 downto 1 do
	begin
		if a[i]=b[tot] then
		inc(site[tot])
		else
		begin
			inc(tot);
			b[tot]:=a[i];
			site[tot]:=1;
		end;
	end;
	j:=0;
	for i:=1 to tot do
	begin
		if site[i]>=2 then
		begin
			if j=0 then
			begin
				j:=b[i];
				continue;
			end
			else
			begin
				writeln(j*b[i]);
				halt;
			end;
		end;
	end;
	writeln(0);
end.


program ec12;
var
	a,b,site:array[1..100001] of int64;
	j:int64;
	n,m,i,max,tot:longint;
procedure qsrt(l,r:longint);
var
	s,v:int64;
	i,j:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	qsrt(1,n);
	tot:=1;
	site[1]:=1;
	b[1]:=a[n];
	for i:=n-1 downto 1 do
	begin
		if a[i]=a[i+1] then
		inc(site[tot])
		else
		begin
			inc(tot);
			b[tot]:=a[i];
			site[tot]:=1;
		end;
	end;
	j:=0;
	for i:=1 to tot do
	begin
		if site[i]>=2 then
		begin
			if j=0 then
			begin
				j:=b[i];
				continue;
			end
			else
			begin
				writeln(j*b[i]);
				halt;
			end;
		end;
	end;
	writeln(0);
end.


program ec12;
var
	a,b,site:array[1..100001] of int64;
	j:int64;
	n,m,i,max,tot:longint;
procedure qsrt(l,r:longint);
var
	i,j,v,s:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	qsrt(1,n);
	tot:=1;
	site[1]:=1;
	b[1]:=a[n];
	for i:=n-1 downto 1 do
	begin
		if a[i]=a[i+1] then
		inc(site[tot])
		else
		begin
			inc(tot);
			b[tot]:=a[i];
			site[tot]:=1;
		end;
	end;
	j:=0;
	for i:=1 to tot do
	begin
		if site[i]>=2 then
		begin
			if j=0 then
			begin
				j:=b[i];
				continue;
			end
			else
			begin
				writeln(j*b[i]);
				halt;
			end;
		end;
	end;
	writeln(0);
end.


program ec12;
var
	a:array[1..100001] of longint;
	n,m,i,j,max,sum,sum1:longint;
procedure qsrt(l,r:longint);
var
	i,j,v,s:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	qsrt(1,n);
	sum:=1;
	j:=a[n];
	sum1:=0;
	max:=0;
	for i:=n-1 downto 1 do
	begin
		if a[i]=a[i+1] then
		begin
			if (sum1=0) and (sum=1) then
			sum:=2;
			if (sum=2) and (sum1=1) then
			begin
				max:=j*a[i];
				break;
			end
			else
			begin
				if sum1=0 then
				inc(sum1);
			end;
		end
		else
		begin
			if sum=1 then
			j:=a[i];
		end;
	end;
	writeln(max);
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,Classes;
var
  d,a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k,min:Integer;
  //l:array[0..6] of Integer = (1,2,4,8,16,32,64);
  s,s123,s1,s2,s3:string;
  tf:Boolean;
  nyu:TStringList;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(a);
    Read(b);
    Read(c);
    Readln(d);

    sum:=(b+d-abs(b-d))div 2 - (a+c+abs(a-c))div 2;
    Writeln((sum+0+abs(sum-0))div 2);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

Alice and Bob are controlling a robot. They each have one switch that controls the robot.

Alice started holding down her button A second after the start-up of the robot, and released her button B second after the start-up.

Bob started holding down his button C second after the start-up, and released his button D second after the start-up.

For how many seconds both Alice and Bob were holding down their buttons?

Constraints

0≤A<B≤100

0≤C<D≤100

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the length of the duration (in seconds) in which both Alice and Bob were holding down their buttons.

Sample Input 1

0 75 25 100

Sample Output 1

50

Alice started holding down her button 0 second after the start-up of the robot, and released her button 75 second after the start-up.

Bob started holding down his button 25 second after the start-up, and released his button 100 second after the start-up.

Therefore, the time when both of them were holding down their buttons, is the 50 seconds from 25 seconds after the start-up to 75 seconds after the start-up.

Sample Input 2

0 33 66 99

Sample Output 2

0

Alice and Bob were not holding their buttons at the same time, so the answer is zero seconds.

Sample Input 3

10 90 20 80

Sample Output 3

60


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,Classes;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k,min:Integer;
  //l:array[0..6] of Integer = (1,2,4,8,16,32,64);
  s,s123,s1,s2,s3:string;
  tf:Boolean;
  nyu:TStringList;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(s);

    Writeln(s[1]+IntToStr(Length(s)-2)+s[Length(s)]);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

The word internationalization is sometimes abbreviated to i18n.
This comes from the fact that there are 18 letters between the first i and the last n.

You are given a string s of length at least 3 consisting of lowercase English letters.
Abbreviate s in the same way.

Constraints

3 ≤ |s| ≤ 100 (|s| denotes the length of s.)

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the abbreviation of s.

Sample Input 1

internationalization

Sample Output 1

i18n

Sample Input 2

smiles

Sample Output 2

s4s

Sample Input 3

xyz

Sample Output 3

x1z


var
  a,i:longint;
  b:array[0..10000]of longint;
begin
readln(a);
for i:=1 to a do
  read(b[i]);
for i:=1 to a-1 do
  if b[i]*b[i+1] mod 4=0 then begin write('Yes'); exit; end;
write('No');
end.

Score : 400 points

Problem Statement

We have a sequence of length N, a = (a_1, a_2, ..., a_N).
Each a_i is a positive integer.

Snuke's objective is to permute the element in a so that the following condition is satisfied:

For each 1 ≤ i ≤ N - 1, the product of a_i and a_{i + 1} is a multiple of 4.

Determine whether Snuke can achieve his objective.

Constraints

2 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1, 100, 10).

Sample Input 2

4
1 2 3 4

Sample Output 2

No

It is impossible to permute a so that the condition is satisfied.

Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.

Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes


var
  a,b:array[1..10000]of integer;
  n,i,j,k,l,x,y:integer;
begin
  readln(x,y);
  readln(n);
  k:=0;
  for i:=1 to n do
  begin
    read(b[i]);
    for j:=k+1 to k+b[i] do a[j]:=i;
    k:=k+b[i];
  end;
  for i:=1 to x do
  begin
    if (i mod 2=1) then
    begin
      for j:=1+y*(i-1) to y*i do  write(a[j],' ');
    end
    else  if (i mod 2=0) then
    begin
      for j:=y*i downto 1+y*(i-1) do write(a[j],' ');
    end;
  writeln;
  end;
end.

Score : 400 points

Problem Statement

We have a grid with H rows and W columns of squares.
Snuke is painting these squares in colors 1, 2, ..., N.
Here, the following conditions should be satisfied:

For each i (1 ≤ i ≤ N), there are exactly a_i squares painted in Color i. Here, a_1 + a_2 + ... + a_N = H W.

For each i (1 ≤ i ≤ N), the squares painted in Color i are 4-connected. That is, every square painted in Color i can be reached from every square painted in Color i by repeatedly traveling to a horizontally or vertically adjacent square painted in Color i.

Find a way to paint the squares so that the conditions are satisfied.
It can be shown that a solution always exists.

Constraints

1 ≤ H, W ≤ 100

1 ≤ N ≤ H W

a_i ≥ 1

a_1 + a_2 + ... + a_N = H W

Input

Input is given from Standard Input in the following format:

H W
N
a_1 a_2 ... a_N

Output

Print one way to paint the squares that satisfies the conditions.
Output in the following format:

c_{1 1} ... c_{1 W}
:
c_{H 1} ... c_{H W}

Here, c_{i j} is the color of the square at the i-th row from the top and j-th column from the left.

Sample Input 1

2 2
3
2 1 1

Sample Output 1

1 1
2 3

Below is an example of an invalid solution:

1 2
3 1

This is because the squares painted in Color 1 are not 4-connected.

Sample Input 2

3 5
5
1 2 3 4 5

Sample Output 2

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

Sample Input 3

1 1
1
1

Sample Output 3

1


var     a,b,h,st:array[0..200005]of longint;
        n,m,dem:longint;

procedure nhap;
var i:longint;
begin
        readln(n,m);
        for i:=1 to m do
            begin
                readln(a[i],b[i]);
                inc(h[a[i]]);
            end;
end;

procedure swap(var g,h:longint);
var temp:longint;
begin
        temp:=g;
        g:=h;
        h:=temp;
end;

procedure sort(l,r:longint);
var i,j,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 not(i>j) then
                begin
                    swap(a[i],a[j]);
                    swap(b[i],b[j]);
                    inc(i);
                    dec(j);
                end;
        until i>j;
        if i<r then sort(i,r);
        if l<j then sort(l,j);
end;

procedure xuli;
var     i,j:longint;
begin
        for i:=1 to 200005 do
                h[i]:=h[i-1]+h[i];
        sort(1,m);

        for i:=h[1] to h[2]-1 do
            begin
                if b[i]<>n then
                    begin
                        inc(dem);
                        st[dem]:=b[i];
                    end;
            end;
        if dem=0 then writeln('IMPOSSIBLE')
        else

            begin
                for i:=1 to dem do
                    for j:=h[st[dem]] to h[st[dem]+1] do
                        begin
                            if (b[j]=n) and (a[j]=st[dem]) then
                                begin
                                    writeln('POSSIBLE');
                                    halt
                                end;
                        end;
                writeln('IMPOSSIBLE');
            end;
end;

begin
       // assign(input,'at_c.inp');reset(input);
       // assign(output,'at_c.out');rewrite(output);
                nhap;
                xuli;
end.

Score : 300 points

Problem Statement

In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands.
For convenience, we will call them Island 1, Island 2, ..., Island N.

There are M kinds of regular boat services between these islands.
Each service connects two islands. The i-th service connects Island a_i and Island b_i.

Cat Snuke is on Island 1 now, and wants to go to Island N.
However, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services.

Help him.

Constraints

3 ≤ N ≤ 200 000

1 ≤ M ≤ 200 000

1 ≤ a_i < b_i ≤ N

(a_i, b_i) \neq (1, N)

If i \neq j, (a_i, b_i) \neq (a_j, b_j).

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

If it is possible to go to Island N by using two boat services, print POSSIBLE; otherwise, print IMPOSSIBLE.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

POSSIBLE

Sample Input 2

4 3
1 2
2 3
3 4

Sample Output 2

IMPOSSIBLE

You have to use three boat services to get to Island 4.

Sample Input 3

100000 1
1 99999

Sample Output 3

IMPOSSIBLE

Sample Input 4

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

Sample Output 4

POSSIBLE

You can get to Island 5 by using two boat services: Island 1 -> Island 4 -> Island 5.


const oo=10000000;
var n,m,nheap:longint;x,y,h,a,pos,heap,cp,c,f:array[0..500005] of longint;free:array[0..500005] of boolean;
procedure       nhap;
var i:longint;
begin
        readln(n,m);
        for i:=1 to m do
                begin
                readln(x[i],y[i]);
                inc(h[x[i]]);
                inc(h[y[i]]);
                end;
        for i:=2 to n+1 do
                h[i]:=h[i-1]+h[i];
        for i:=1 to m do
                begin
                A[h[x[i]]]:=y[i];
                a[h[y[i]]]:=x[i];
                C[h[x[i]]]:=1;
                C[h[y[i]]]:=1;
                dec(h[x[i]]);
                dec(h[y[i]]);
                end;
end;
procedure   init;
var i:longint;
begin
    for i:=1 to n do
        cp[i]:=oo;
    fillchar(free,sizeof(free),true);
    cp[1]:=0;
end;

procedure   swap(var x,y:longint);
var tamp:longint;
begin
    tamp:=x;
    x:=y;
    y:=tamp;
end;
procedure   upheap(i:longint);
begin
    if (i=1)or (cp[heap[i]]>cp[heap[i div 2]]) then exit;
    swap(heap[i],heap[i div 2]);
    swap(pos[heap[i]],pos[heap[i div 2]]);
    upheap(i div 2);
end;
procedure   downheap(I:longint);
var j:longint;
begin
    j:=i*2;
    if j>nheap then exit;
    if (j<nheap) and (cp[heap[j]]>cp[heap[j+1]]) then inc(j);
    if cp[heap[i]]>cp[heap[j]] then
        begin
        swap(heap[i],heap[j]);
        swap(pos[heap[i]],pos[heap[j]]);
        downheap(j);
        end;
end;
function pop(v:longint):longint;
begin
    pop:=heap[v];
    heap[v]:=heap[nheap];
    pos[heap[v]]:=v;
    dec(nheap);
    upheap(v);
    downheap(v);
end;
procedure   push(x:longint);
begin
    inc(nheap);
    pos[x]:=nheap;
    heap[nheap]:=x;
    upheap(nheap);
end;
procedure   dijheap;
var v,vjp,u:longint;
begin
    f[1]:=1;
    push(1);
    repeat
        u:=pop(1);
        if u=n then exit;
        free[u]:=false;
        for v:=h[u]+1 to h[u+1] do
            if (free[a[v]]) then
                begin
                if cp[a[v]]>=cp[u]+c[v] then
                    begin
                    if cp[a[v]]>cp[u]+c[v] then F[a[v]]:=f[u]
                    else f[a[v]]:=f[a[v]]+f[u];
                    cp[a[v]]:=cp[u]+c[v];
                    if pos[a[v]]=0 then push(a[v]) else
                        begin
                        vjp:=pos[a[v]];
                        upheap(vjp);
                        downheap(vjp);
                        end;
                    end

                end;
        until false;
end;
procedure       xuat;
begin
        if Cp[n]>2 then writeln('IMPOSSIBLE')
        else writeln('POSSIBLE');
end;
begin

        nhap;
        init;
        dijheap;
        xuat;
end.


var
	N,M,i,u,v:Longint;
	ok:Array[1..200000]of Boolean;
begin
	read(N,M);
	for i:=1 to M do begin
		read(u,v);
		if u=1 then begin
			if ok[v]then begin
				writeln('POSSIBLE');
				exit;
			end;
			ok[v]:=True;
		end;
		if v=N then begin
			if ok[u]then begin
				writeln('POSSIBLE');
				exit;
			end;
			ok[u]:=True;
		end;
	end;
	writeln('IMPOSSIBLE');
end.


var k:int64;i,m:Longint;begin read(k);m:=k mod 50;writeln(50);for i:=1 to m do writeln(k div 50+99-m);for i:=m to 50 do writeln(k div 50+49-m)end.

Score : 600 points

Problem Statement

We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N-1 or smaller.

Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.

It can be proved that the largest element in the sequence becomes N-1 or smaller after a finite number of operations.

You are given an integer K. Find an integer sequence a_i such that the number of times we will perform the above operation is exactly K. It can be shown that there is always such a sequence under the constraints on input and output in this problem.

Constraints

0 ≤ K ≤ 50 \times 10^{16}

Input

Input is given from Standard Input in the following format:

K

Output

Print a solution in the following format:

N
a_1 a_2 ... a_N

Here, 2 ≤ N ≤ 50 and 0 ≤ a_i ≤ 10^{16} + 1000 must hold.

Sample Input 1

0

Sample Output 1

4
3 3 3 3

Sample Input 2

1

Sample Output 2

3
1 0 3

Sample Input 3

2

Sample Output 3

2
2 2

The operation will be performed twice: [2, 2] -> [0, 3] -> [1, 1].

Sample Input 4

3

Sample Output 4

7
27 0 0 0 0 0 0

Sample Input 5

1234567894848

Sample Output 5

10
1000 193 256 777 0 1 1192 1234567891011 48 425


var
	i,j,k,n:Longint;
	a:array[1..50]of int64;
	L,ans:int64;
	flag:Boolean;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	while true do begin
		for i:=n downto 2 do begin
			flag:=false;
			for j:=1 to i-1 do begin
				if a[j]<a[j+1]then begin
					L:=a[j];
					a[j]:=a[j+1];
					a[j+1]:=L;
					flag:=true;
				end;
			end;
			if not flag then break;
		end;
		if a[1]<n then break;
		k:=1;
		while(k+1<=n)and(a[k+1]+k>=n)and(a[k]-a[k+1]<=n)do inc(k);
		if k=n then begin
			k:=1;
			while(k+1<=n)and(a[1]-a[k+1]<=k)do inc(k);
		end;
		if k<n then begin
			L:=(a[1]-a[k+1])div(n+1);
			if L<=0 then L:=1;
			inc(ans,L*k);
			for i:=1 to n do begin
				if i>k then inc(a[i],L*k)
				else inc(a[i],L*(k-1-n));
			end;
		end else begin
			L:=a[1]-n+1;
			inc(ans,L*n);
			writeln(ans);
			exit;
		end;
	end;
	writeln(ans);
end.

Score : 600 points

Problem Statement

We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N-1 or smaller. (The operation is the same as the one in Problem D.)

Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.

It can be proved that the largest element in the sequence becomes N-1 or smaller after a finite number of operations.

You are given the sequence a_i. Find the number of times we will perform the above operation.

Constraints

2 ≤ N ≤ 50

0 ≤ a_i ≤ 10^{16} + 1000

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the number of times the operation will be performed.

Sample Input 1

4
3 3 3 3

Sample Output 1

0

Sample Input 2

3
1 0 3

Sample Output 2

1

Sample Input 3

2
2 2

Sample Output 3

2

Sample Input 4

7
27 0 0 0 0 0 0

Sample Output 4

3

Sample Input 5

10
1000 193 256 777 0 1 1192 1234567891011 48 425

Sample Output 5

1234567894848


const
        fi='';
        fo='';
var     n,m,res:longint;
        a:array[1..300,1..300]of longint;
        c,s:array[1..300]of longint;
        cx:array[0..300]of byte;
        g:text;

procedure solve;
var     i,j,mj:longint;
begin
        for i:=1 to m do c[i]:=1;
        res:=m;
        for i:=1 to n do
        begin
                for j:=1 to n do s[j]:=0;
                mj:=1;
                for j:=1 to m do
                begin
                        inc(s[a[j,c[j]]]);
                        if s[mj]<s[a[j,c[j]]] then mj:=a[j,c[j]];
                end;
                if s[mj]<res then res:=s[mj];
                cx[mj]:=1;
                for j:=1 to m do
                begin
                        while cx[a[j,c[j]]]=1 do inc(c[j]);
                end;
        end;
end;

procedure main;
var     i,j:longint;
begin
        assign(g,fi);
        reset(g);
        read(g,m,n);
        for i:=1 to m do for j:=1 to n do read(g,a[i,j]);
        close(g);
        assign(g,fo);
        rewrite(g);
        solve;
        writeln(g,res);
        close(g);
end;

BEGIN
        main;
END.

Score : 300 points

Problem Statement

There is a box containing N balls. The i-th ball has the integer A_i written on it.
Snuke can perform the following operation any number of times:

Take out two balls from the box. Then, return them to the box along with a new ball, on which the absolute difference of the integers written on the two balls is written.

Determine whether it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq K \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

If it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written, print POSSIBLE; if it is not possible, print IMPOSSIBLE.

Sample Input 1

3 7
9 3 4

Sample Output 1

POSSIBLE

First, take out the two balls 9 and 4, and return them back along with a new ball, abs(9-4)=5.
Next, take out 3 and 5, and return them back along with abs(3-5)=2.
Finally, take out 9 and 2, and return them back along with abs(9-2)=7.
Now we have 7 in the box, and the answer is therefore POSSIBLE.

Sample Input 2

3 5
6 9 3

Sample Output 2

IMPOSSIBLE

No matter what we do, it is not possible to have 5 in the box. The answer is therefore IMPOSSIBLE.

Sample Input 3

4 11
11 3 7 15

Sample Output 3

POSSIBLE

The box already contains 11 before we do anything. The answer is therefore POSSIBLE.

Sample Input 4

5 12
10 2 8 6 4

Sample Output 4

IMPOSSIBLE


var
n,m,i,j,ans,t,id:longint;
c,u:array[1..500] of longint;
a:array[1..500,1..500] of longint;
function fun(x:longint):longint;
var
i:longint;
begin
for i:=1 to m do
begin
if u[a[x,i]]<>0 then continue;
exit(a[x,i]);
end;
exit;
end;

begin
ans:=1000;
readln(n,m);
for i:=1 to n do
begin
for j:=1 to m do
begin
read(a[i,j]);
end;
readln;
end;

for i:=1 to m do
begin
fillchar(c,sizeof(c),0);
for j:=1 to n do
inc(c[fun(j)]);
t:=0;
id:=0;
for j:=1 to m do
if t<c[j] then begin t:=c[j]; id:=j; end;

if t<ans then ans:=t;
u[id]:=1;
end;
write(ans);
end.

Score : 700 points

Problem Statement

Takahashi is hosting an sports meet.
There are N people who will participate. These people are conveniently numbered 1 through N.
Also, there are M options of sports for this event. These sports are numbered 1 through M.
Among these options, Takahashi will select one or more sports (possibly all) to be played in the event.

Takahashi knows that Person i's j-th favorite sport is Sport A_{ij}.
Each person will only participate in his/her most favorite sport among the ones that are actually played in the event, and will not participate in the other sports.

Takahashi is worried that one of the sports will attract too many people.
Therefore, he would like to carefully select sports to be played so that the number of the participants in the sport with the largest number of participants is minimized.
Find the minimum possible number of the participants in the sport with the largest number of participants.

Constraints

1 \leq N \leq 300

1 \leq M \leq 300

A_{i1} , A_{i2} , ... , A_{iM} is a permutation of the integers from 1 to M.

Input

Input is given from Standard Input in the following format:

N M
A_{11} A_{12} ... A_{1M}
A_{21} A_{22} ... A_{2M}
:
A_{N1} A_{N2} ... A_{NM}

Output

Print the minimum possible number of the participants in the sport with the largest number of participants.

Sample Input 1

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

Sample Output 1

2

Assume that Sports 1, 3 and 4 are selected to be played. In this case, Person 1 will participate in Sport 1, Person 2 in Sport 3, Person 3 in Sport 3 and Person 4 in Sport 4.
Here, the sport with the largest number of participants is Sport 3, with two participants.
There is no way to reduce the number of participants in the sport with the largest number of participants to 1. Therefore, the answer is 2.

Sample Input 2

3 3
2 1 3
2 1 3
2 1 3

Sample Output 2

3

Since all the people have the same taste in sports, there will be a sport with three participants, no matter what sports are selected.
Therefore, the answer is 3.


program V801;
 var
  a:array[0..301,0..301] of longint;
  b,d,f:array[0..301] of longint;
  i,j,m,n,maxp,smax:longint;
 begin
  readln(n,m);
  for i:=1 to n do
   a[i,m+1]:=0;
  for i:=1 to n do
   begin
    for j:=1 to m do
     read(a[i,j]);
    readln;
   end;
  fillchar(d,sizeof(d),0);
  maxp:=0;
  for i:=1 to n do
   begin
    inc(d[a[i,1]]);
    if d[a[i,1]]>d[maxp] then maxp:=a[i,1];
   end;
  smax:=d[maxp];
  for i:=1 to m do
   f[i]:=1;
  for i:=1 to n do
   b[i]:=1;
  for i:=2 to m do
   begin
    fillchar(d,sizeof(d),0);
    f[maxp]:=0;
    maxp:=0;
    for j:=1 to n do
     begin
      while f[a[j,b[j]]]=0 do inc(b[j]);
      if b[j]<>m+1 then
       begin
        inc(d[a[j,b[j]]]);
        if d[a[j,b[j]]]>d[maxp] then maxp:=a[j,b[j]];
       end;
     end;
    if d[maxp]<smax then smax:=d[maxp];
   end;
  writeln(smax);
 end.


program ec12;
var
	n,m,i,j,max,min:longint;
	a:array[1..301,1..301] of longint;
	vis,step:array[1..301] of longint;
	vis1:array[1..301] of boolean;
begin
	readln(n,m);
	for i:=1 to n do
	for j:=1 to m do
	read(a[i,j]);
	min:=n;
	for i:=1 to n do
	step[i]:=1;
	fillchar(vis1,sizeof(vis1),true);
	while true do
	begin
		fillchar(vis,sizeof(vis),0);
		for i:=1 to n do
		inc(vis[a[i,step[i]]]);
		max:=1;
		for i:=2 to m do
		begin
			if vis[max]<vis[i] then
			max:=i;
		end;
		if vis[max]<min then
		min:=vis[max];
		vis1[max]:=false;
		for i:=1 to n do
		begin
			while (not vis1[a[i,step[i]]]) and (step[i]<=m) do
			inc(step[i]);
			if step[i]>m then
			begin
				writeln(min);
				halt;
			end;
		end;
	end;
	writeln(min);
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,Classes;
function med3(x,y,z:Integer):Integer;
begin
  if (x < y) then begin
    if (y < z) then
      result:= y
    else if(z < x) then
      result:= x
    else
      result:= z;
  end else begin
    if (z < y) then
      result:= y
    else if (x < z) then
      result:= x
    else
      result:= z;
  end;
end;

procedure quicksort1(var a:array of integer;const left,right:integer);
var
  i,j,pivot,tmp:integer;
begin
  if (left < right) then begin
    i := left;
    j := right;
    pivot := med3(a[i], a[i + (j - i) div 2], a[j]);
    while true do begin
      while (a[i] < pivot) do
        inc(i);
      while (pivot < a[j]) do
        dec(j);
      if (i >= j) then
        break;
      tmp := a[i];
      a[i]:= a[j];
      a[j]:= tmp;
      inc(i);
      dec(j);
    end;
    quicksort1(a, left, i - 1);
    quicksort1(a, j + 1, right);
  end;
end;

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

    QuickSort1(l, Low(l), High(l));

    sum:=0;
    for i := n downto n-k do
      sum:=sum+l[i];

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

Score : 200 points

Problem Statement

Snuke has N sticks.
The length of the i-th stick is l_i.

Snuke is making a snake toy by joining K of the sticks together.

The length of the toy is represented by the sum of the individual sticks that compose it.
Find the maximum possible length of the toy.

Constraints

1 \leq K \leq N \leq 50

1 \leq l_i \leq 50

l_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
l_1 l_2 l_3 ... l_{N}

Output

Print the answer.

Sample Input 1

5 3
1 2 3 4 5

Sample Output 1

12

You can make a toy of length 12 by joining the sticks of lengths 3, 4 and 5, which is the maximum possible length.

Sample Input 2

15 14
50 26 27 21 41 7 42 35 7 5 5 36 39 1 45

Sample Output 2

386


var a:array[0..2000000]of longint;
    i,ans,n:longint;
    sum,su,s:int64;
begin
  ans:=maxlongint;
  readln(n);
  for i:=1 to n do
  begin
    read(a[i]);
    inc(sum,a[i]);
  end;
  for i:=1 to n-1 do
  begin
    dec(sum,a[i]);
    inc(su,a[i]);
    s:=abs(sum-su);
    if s<ans then
      ans:=s;
  end;
  writeln(ans);
end.

Score : 300 points

Problem Statement

Snuke and Raccoon have a heap of N cards. The i-th card from the top has the integer a_i written on it.

They will share these cards.
First, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards.
Here, both Snuke and Raccoon have to take at least one card.

Let the sum of the integers on Snuke's cards and Raccoon's cards be x and y, respectively.
They would like to minimize |x-y|.
Find the minimum possible value of |x-y|.

Constraints

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

-10^{9} \leq a_i \leq 10^{9}

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print the answer.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

1

If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, x=10, y=11, and thus |x-y|=1. This is the minimum possible value.

Sample Input 2

2
10 -10

Sample Output 2

20

Snuke can only take one card from the top, and Raccoon can only take the remaining one card. In this case, x=10, y=-10, and thus |x-y|=20.


var
	n,i:Longint;
	ans,sum,t:int64;
	a:array[1..200000]of Longint;
begin
	read(n);
	ans:=1000000000000000000;
	for i:=1 to n do begin
		read(a[i]);
		sum:=sum+a[i];
	end;
	for i:=1 to n-1 do begin
		t:=t+a[i];
		if ans>abs(sum-t*2)then ans:=abs(sum-t*2);
	end;
	writeln(ans);
end.


var
	n,i:Longint;
	ans,sum,t:int64;
	a:array[1..100000]of Longint;
begin
	read(n);
	ans:=1000000000000000000;
	for i:=1 to n do begin
		read(a[i]);
		sum:=sum+a[i];
	end;
	for i:=1 to n-1 do begin
		t:=t+a[i];
		if ans>abs(sum-t*2)then ans:=abs(sum-t*2);
	end;
	writeln(ans);
end.


program ec12;
var
	j:int64;
	s:array[0..200001] of int64;
	n,m,i,x:longint;
begin
	readln(n);
	s[0]:=0;
	for i:=1 to n do
	begin
		read(x);
		s[i]:=s[i-1]+x;
	end;
	j:=maxlongint-1;
	for i:=1 to n-1 do
	begin
		if abs(s[n]-s[i]-s[i])<j then
		j:=abs(s[n]-s[i]-s[i]);
	end;
	writeln(j);
end.


program V1005;
 var
  a:array[0..200001] of longint;
  i,n:longint;
  min,s,s1:int64;
 begin
  readln(n);
  s:=0;
  s1:=0;
  for i:=1 to n do
   begin
    read(a[i]);
    inc(s1,a[i]);
   end;
  readln;
  min:=100820882000000000;
  for i:=1 to n-1 do
   begin
    inc(s,a[i]);
    dec(s1,a[i]);
    if abs(s-s1)<min then min:=abs(s-s1);
   end;
  writeln(min);
 end.


var
	n,i,cnt,a,b:Longint;
	v:array[1..100000]of array of Longint;
	c,d1,d2:array[1..100000]of Longint;
procedure dfs1(u,d:Longint);
var i:Longint;
begin
	d1[u]:=d;
	for i:=0 to c[u]-1 do if d1[v[u][i]]=0 then dfs1(v[u][i],d+1);
end;
procedure dfs2(u,d:Longint);
var i:Longint;
begin
	d2[u]:=d;
	for i:=0 to c[u]-1 do if d2[v[u][i]]=0 then dfs2(v[u][i],d+1);
end;
begin
	read(n);
	for i:=2 to n do begin
		read(a,b);
		inc(c[a]);
		inc(c[b]);
		setLength(v[a],c[a]);
		setLength(v[b],c[b]);
		v[a][c[a]-1]:=b;
		v[b][c[b]-1]:=a;
	end;
	dfs1(1,1);
	dfs2(n,1);
	for i:=1 to n do if d1[i]<=d2[i] then inc(cnt);
	if cnt*2>n then writeln('Fennec')else writeln('Snuke');
end.

Score : 400 points

Problem Statement

Fennec and Snuke are playing a board game.

On the board, there are N cells numbered 1 through N, and N-1 roads, each connecting two cells. Cell a_i is adjacent to Cell b_i through the i-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.

Initially, Cell 1 is painted black, and Cell N is painted white. The other cells are not yet colored.
Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell.
More specifically, each player performs the following action in her/his turn:

Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black.

Snuke: selects an uncolored cell that is adjacent to a white cell, and paints it white.

A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.

Constraints

2 \leq N \leq 10^5

1 \leq a_i, b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

If Fennec wins, print Fennec; if Snuke wins, print Snuke.

Sample Input 1

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

Sample Output 1

Fennec

For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke's moves.

Sample Input 2

4
1 4
4 2
2 3

Sample Output 2

Snuke


Score : 400 points

Problem Statement

There are N squares in a row.
The leftmost square contains the integer A, and the rightmost contains the integer B. The other squares are empty.

Aohashi would like to fill the empty squares with integers so that the following condition is satisfied:

For any two adjacent squares, the (absolute) difference of the two integers in those squares is between C and D (inclusive).

As long as the condition is satisfied, it is allowed to use arbitrarily large or small integers to fill the squares.
Determine whether it is possible to fill the squares under the condition.

Constraints

3 \leq N \leq 500000

0 \leq A \leq 10^9

0 \leq B \leq 10^9

0 \leq C \leq D \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B C D

Output

Print YES if it is possible to fill the squares under the condition; print NO otherwise.

Sample Input 1

5 1 5 2 4

Sample Output 1

YES

For example, fill the squares with the following integers: 1, -1, 3, 7, 5, from left to right.

Sample Input 2

4 7 6 4 5

Sample Output 2

NO

Sample Input 3

48792 105960835 681218449 90629745 90632170

Sample Output 3

NO

Sample Input 4

491995 412925347 825318103 59999126 59999339

Sample Output 4

YES


var
 n,i,j:longint;
 a,b:array[0..200001] of int64;
begin
 readln(n);
 for i:=1 to n do
  read(a[i]);
 a[0]:=a[1];
 for i:=1 to n div 2 do
  b[i]:=a[n-(i-1)*2];
 for i:=n downto n div 2+1 do
  b[i]:=a[(n+1)-(n-i+1)*2];
 for i:=1 to n do
  write(b[i],' ');
end.

Score : 300 points

Problem Statement

You are given an integer sequence of length n, a_1, ..., a_n.
Let us consider performing the following n operations on an empty sequence b.

The i-th operation is as follows:

Append a_i to the end of b.

Reverse the order of the elements in b.

Find the sequence b obtained after these n operations.

Constraints

1 \leq n \leq 2\times 10^5

0 \leq a_i \leq 10^9

n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print n integers in a line with spaces in between.
The i-th integer should be b_i.

Sample Input 1

4
1 2 3 4

Sample Output 1

4 2 1 3

After step 1 of the first operation, b becomes: 1.

After step 2 of the first operation, b becomes: 1.

After step 1 of the second operation, b becomes: 1, 2.

After step 2 of the second operation, b becomes: 2, 1.

After step 1 of the third operation, b becomes: 2, 1, 3.

After step 2 of the third operation, b becomes: 3, 1, 2.

After step 1 of the fourth operation, b becomes: 3, 1, 2, 4.

After step 2 of the fourth operation, b becomes: 4, 2, 1, 3.

Thus, the answer is 4 2 1 3.

Sample Input 2

3
1 2 3

Sample Output 2

3 1 2

As shown above in Sample Output 1, b becomes 3, 1, 2 after step 2 of the third operation. Thus, the answer is 3 1 2.

Sample Input 3

1
1000000000

Sample Output 3

1000000000

Sample Input 4

6
0 6 7 6 7 0

Sample Output 4

0 6 6 0 7 7


var
 n,i,j:longint;
 a,b:array[0..100001] of int64;
begin
 readln(n);
 for i:=1 to n do
  read(a[i]);
 a[0]:=a[1];
 for i:=1 to n div 2 do
  b[i]:=a[n-(i-1)*2];
 for i:=n downto n div 2+1 do
  b[i]:=a[(n+1)-(n-i+1)*2];
 for i:=1 to n do
  write(b[i],' ');
end.


var
 n,i,j:longint;
 a,b:array[0..100001] of longint;
begin
 readln(n);
 for i:=1 to n do
  read(a[i]);
 a[0]:=a[1];
 for i:=1 to n div 2 do
  b[i]:=a[n-(i-1)*2];
 for i:=n downto n div 2+1 do
  b[i]:=a[(n+1)-(n-i+1)*2];
 for i:=1 to n do
  write(b[i],' ');
end.


var
 n,i,j:longint;
 a,b:array[0..100001] of longint;
begin
 readln(n);
 for i:=1 to n do
  begin
   read(a[i]);
   fillchar(b,sizeof(b),0);
   for j:=1 to i do
    b[i-j+1]:=a[j];
   a:=b;
  end;
 for i:=1 to n do
  write(a[i],' ');
end.


program V1006;
 var
  a:array[0..200001] of longint;
  i,n:longint;
 begin
  readln(n);
  for i:=1 to n do
   read(a[i]);
  readln;
  i:=n;
  while i>=1 do
   begin
    if n=1 then writeln(a[i])
           else write(a[i],' ');
    dec(i,2);
   end;
  if n mod 2=1 then i:=2
               else i:=1;
  while i<=n do
   begin
    if i+2>n then writeln(a[i])
             else write(a[i],' ');
    inc(i,2);
   end;
 end.


var
	N,i:Longint;
	A:Array[1..200000]of Longint;
begin
	read(N);
	for i:=1 to N do read(A[i]);
	i:=N;
	while i>0 do begin
		writeln(A[i]);
		dec(i,2);
	end;
	i:=N mod 2+1;
	while i<=N do begin
		writeln(A[i]);
		inc(i,2);
	end;
end.


var
	n,i,l,r:Longint;
	a,v:array[1..100001]of Longint;
	ans:int64;
	fac:array[0..100001]of int64;
function power(a,b:int64):int64;
var s:int64;
begin
	if b=0 then power:=1
	else begin
		s:=power(a*a mod 1000000007,b div 2);
		if b mod 2=1 then s:=s*a mod 1000000007;
		power:=s;
	end;
end;
function C(a,b:int64):int64;
begin
	if(b<0)or(b>a)then begin
		C:=0;
		exit;
	end;
	C:=fac[a]*power(fac[a-b],1000000005)mod 1000000007*power(fac[b],1000000005)mod 1000000007;
end;
begin
	fac[0]:=1;
	for i:=1 to 100001 do fac[i]:=fac[i-1]*i mod 1000000007;
	read(n);
	for i:=1 to n+1 do begin
		read(a[i]);
		if v[a[i]]=0 then v[a[i]]:=i
		else begin
			l:=v[a[i]]-1;
			r:=n+1-i;
		end;
	end;
	for i:=1 to n+1 do writeln((C(n+1,i)-C(l+r,i-1)+1000000007)mod 1000000007);
end.

Score : 600 points

Problem Statement

You are given an integer sequence of length n+1, a_1,a_2,...,a_{n+1}, which consists of the n integers 1,...,n.
It is known that each of the n integers 1,...,n appears at least once in this sequence.

For each integer k=1,...,n+1, find the number of the different subsequences (not necessarily contiguous) of the given sequence with length k, modulo 10^9+7.

Notes

If the contents of two subsequences are the same, they are not separately counted even if they originate from different positions in the original sequence.

A subsequence of a sequence a with length k is a sequence obtained by selecting k of the elements of a and arranging them without changing their relative order. For example, the sequences 1,3,5 and 1,2,3 are subsequences of 1,2,3,4,5, while 3,1,2 and 1,10,100 are not.

Constraints

1 \leq n \leq 10^5

1 \leq a_i \leq n

Each of the integers 1,...,n appears in the sequence.

n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_{n+1}

Output

Print n+1 lines.
The k-th line should contain the number of the different subsequences of the given sequence with length k, modulo 10^9+7.

Sample Input 1

3
1 2 1 3

Sample Output 1

3
5
4
1

There are three subsequences with length 1: 1 and 2 and 3.

There are five subsequences with length 2: 1,1 and 1,2 and 1,3 and 2,1 and 2,3.

There are four subsequences with length 3: 1,1,3 and 1,2,1 and 1,2,3 and 2,1,3.

There is one subsequence with length 4: 1,2,1,3.

Sample Input 2

1
1 1

Sample Output 2

1
1

There is one subsequence with length 1: 1.

There is one subsequence with length 2: 1,1.

Sample Input 3

32
29 19 7 10 26 32 27 4 11 20 2 8 16 23 5 14 6 12 17 22 18 30 28 24 15 1 25 3 13 21 19 31 9

Sample Output 3

32
525
5453
40919
237336
1107568
4272048
13884156
38567100
92561040
193536720
354817320
573166440
818809200
37158313
166803103
166803103
37158313
818809200
573166440
354817320
193536720
92561040
38567100
13884156
4272048
1107568
237336
40920
5456
528
33
1

Be sure to print the numbers modulo 10^9+7.


var
	n,i,l,r:Longint;
	a,v:array[1..100001]of Longint;
	ans:int64;
	fac:array[0..100001]of int64;
function power(a,b:int64):int64;
var s:int64;
begin
	if b=0 then power:=1
	else begin
		s:=power(a*a mod 1000000007,b div 2);
		if b mod 2=1 then s:=s*a mod 1000000007;
		power:=s;
	end;
end;
function C(a,b:int64):int64;
begin
	if(b<0)or(b>a)then begin
		C:=0;
		exit;
	end;
	C:=fac[a]*power(fac[a-b],1000000005)mod 1000000007*power(fac[b],1000000005)mod 1000000007;
end;
begin
	fac[0]:=1;
	for i:=1 to 100001 do fac[i]:=fac[i-1]*i mod 1000000007;
	read(n);
	for i:=1 to n+1 do begin
		read(a[i]);
		if v[a[i]]=0 then v[a[i]]:=i
		else begin
			l:=v[a[i]]-1;
			r:=n+1-i;
		end;
	end;
	for i:=1 to n+1 do writeln((C(n+1,i)-C(l+r,i-1))mod 1000000007);
end.


var
	n,i,l,r:Longint;
	a,v:array[1..100001]of Longint;
	ans:int64;
	fac:array[0..100001]of int64;
function power(a,b:int64):int64;
var s:int64;
begin
	if b=0 then power:=1
	else begin
		s:=power(a*a mod 1000000007,b div 2);
		if b mod 2=1 then s:=s*a mod 1000000007;
		power:=s;
	end;
end;
function C(a,b:int64):int64;
var s,t:int64;i:Longint;
begin
	if(b<0)or(b>a)then begin
		C:=0;
		exit;
	end;
	s:=1;
	t:=1;
	for i:=1 to b do begin
		s:=s*(a-i+1)mod 1000000007;
		t:=t*i mod 1000000007;
	end;
	C:=fac[a]*power(fac[a-b],1000000005)mod 1000000007*power(fac[b],1000000005)mod 1000000007;
end;
begin
	fac[0]:=1;
	for i:=1 to 100001 do fac[i]:=fac[i-1]*i mod 1000000007;
	read(n);
	for i:=1 to n+1 do begin
		read(a[i]);
		if v[a[i]]=0 then v[a[i]]:=i
		else begin
			l:=v[a[i]]-1;
			r:=n+1-i;
		end;
	end;
	for i:=1 to n+1 do writeln((C(n+1,i)-C(l+r,i-1))mod 1000000007);
end.


var
	n,i,l,r:Longint;
	a,v:array[1..100001]of Longint;
	ans:int64;
function power(a,b:int64):int64;
var s:int64;
begin
	if b=0 then power:=1
	else begin
		s:=power(a*a mod 1000000007,b div 2);
		if b mod 2=1 then s:=s*a mod 1000000007;
		power:=s;
	end;
end;
function C(a,b:int64):int64;
var s,t:int64;i:Longint;
begin
	if(b<0)or(b>a)then begin
		C:=0;
		exit;
	end;
	s:=1;
	t:=1;
	for i:=1 to b do begin
		s:=s*(a-i+1)mod 1000000007;
		t:=t*i mod 1000000007;
	end;
	C:=s*power(t,1000000005)mod 1000000007;
end;
begin
	read(n);
	for i:=1 to n+1 do begin
		read(a[i]);
		if v[a[i]]=0 then v[a[i]]:=i
		else begin
			l:=v[a[i]]-1;
			r:=n+1-i;
		end;
	end;
	for i:=1 to n+1 do writeln((C(n+1,i)-C(l+r,i-1))mod 1000000007);
end.


program V803;
 var
  a,c:array[0..100001] of longint;
  b:array[0..100001] of int64;
  i,j,m,n:longint;
  sum,max:int64;
 begin
  readln(n,m);
  for i:=1 to n do
   read(a[i]);
  readln;
  fillchar(b,sizeof(b),0);
  fillchar(c,sizeof(c),0);
  sum:=0;
  for i:=2 to n do
   if a[i]>a[i-1] then
    begin
     inc(sum,a[i]-a[i-1]);
     inc(b[a[i-1]+1],a[i-1]+1);
     dec(b[a[i]+1],a[i-1]+1);
     inc(c[a[i-1]+1]);
     dec(c[a[i]+1]);
    end
                  else
    begin
     inc(sum,m-a[i-1]+a[i]);
     inc(b[a[i-1]+1],a[i-1]+1);
     inc(b[1],a[i-1]+1-m);
     dec(b[a[i]+1],a[i-1]+1-m);
     inc(c[a[i-1]+1]);
     inc(c[1]);
     dec(c[a[i]+1]);
    end;
  for i:=2 to m do
   begin
    inc(b[i],b[i-1]);
    inc(c[i],c[i-1]);
   end;
  max:=0;
  for i:=1 to m do
   if i*c[i]-b[i]>max then max:=c[i]*i-b[i];
  writeln(sum-max);
 end.

Score : 700 points

Problem Statement

Snuke is buying a lamp.
The light of the lamp can be adjusted to m levels of brightness, represented by integers from 1 through m, by the two buttons on the remote control.

The first button is a "forward" button. When this button is pressed, the brightness level is increased by 1, except when the brightness level is m, in which case the brightness level becomes 1.

The second button is a "favorite" button. When this button is pressed, the brightness level becomes the favorite brightness level x, which is set when the lamp is purchased.

Snuke is thinking of setting the favorite brightness level x so that he can efficiently adjust the brightness.
He is planning to change the brightness n-1 times. In the i-th change, the brightness level is changed from a_i to a_{i+1}. The initial brightness level is a_1.
Find the number of times Snuke needs to press the buttons when x is set to minimize this number.

Constraints

2 \leq n,m \leq 10^5

1 \leq a_i\leq m

a_i \neq a_{i+1}

n, m and a_i are integers.

Input

Input is given from Standard Input in the following format:

n m
a_1 a_2 … a_n

Output

Print the minimum number of times Snuke needs to press the buttons.

Sample Input 1

4 6
1 5 1 4

Sample Output 1

5

When the favorite brightness level is set to 1, 2, 3, 4, 5 and 6, Snuke needs to press the buttons 8, 9, 7, 5, 6 and 9 times, respectively.
Thus, Snuke should set the favorite brightness level to 4.
In this case, the brightness is adjusted as follows:

In the first change, press the favorite button once, then press the forward button once.

In the second change, press the forward button twice.

In the third change, press the favorite button once.

Sample Input 2

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

Sample Output 2

45


program V803;
 var
  a,b,c:array[0..100001] of longint;
  i,m,n:longint;
  sum,max:int64;
 begin
  readln(n,m);
  for i:=1 to n do
   read(a[i]);
  readln;
  fillchar(b,sizeof(b),0);
  fillchar(c,sizeof(c),0);
  sum:=0;
  for i:=2 to n do
   if a[i]>a[i-1] then
    begin
     inc(sum,a[i]-a[i-1]);
     inc(b[a[i-1]+1],a[i-1]+1);
     dec(b[a[i]+1],a[i-1]+1);
     inc(c[a[i-1]+2]);
     dec(c[a[i]+1]);
    end
                  else
    begin
     inc(sum,m-a[i-1]+a[i]);
     inc(b[a[i-1]+1],a[i-1]+1);
     inc(b[1],a[i-1]+1-m);
     dec(b[a[i]+1],a[i-1]+1-m);
     inc(c[a[i-1]+1]);
     inc(c[1]);
     dec(c[a[i]+1]);
    end;
  for i:=2 to n do
   begin
    inc(b[i],b[i-1]);
    inc(c[i],c[i-1]);
   end;
  max:=0;
  for i:=1 to n do
   if i*c[i]-b[i]>max then max:=c[i]*i-b[i];
  writeln(sum-max);
 end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,Classes;
var
  h,w,i,j,count,x,y,sw,m,n,a,b,c:Integer;
  s123,s1,s2,s3:string;
  tf:Boolean;
  nyu:TStringList;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(x);
    Read(a);
    Readln(b);

    if a-b>0 then
      Writeln('delicious')
    else if x+a>b then
      Writeln('safe')
    else
      Writeln('dangerous');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier.
He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though.

Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious.

Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it.

Write a program that outputs delicious if he found it delicious, safe if he did not found it delicious but did not get a stomachache either, and dangerous if he got a stomachache.

Constraints

1 ≤ X,A,B ≤ 10^9

Input

Input is given from Standard Input in the following format:

X A B

Output

Print delicious if Takahashi found the food delicious; print safe if he neither found it delicious nor got a stomachache; print dangerous if he got a stomachache.

Sample Input 1

4 3 6

Sample Output 1

safe

He ate the food three days after the "best-by" date. It was not delicious or harmful for him.

Sample Input 2

6 5 1

Sample Output 2

delicious

He ate the food by the "best-by" date. It was delicious for him.

Sample Input 3

3 7 12

Sample Output 3

dangerous

He ate the food five days after the "best-by" date. It was harmful for him.


program abc;
const maxN=100005;
var i,n,d:longint;
    a,dau:array[0..maxN] of longint;
begin
    read(n);
    for i:=1 to n do
        read(a[i]);
    i:=1;
    d:=0;
    repeat
        dau[i]:=1;
        i:=a[i];
        inc(d);
    until (dau[2]=1) or (dau[i]=1);
    if dau[2]=1 then
        write(d-1)
    else
        write(-1);
end.


const fi='trained.inp';
fo='trained.out';
var a,dau:array[0..100005] of longint;
n,d:longint;
kt:boolean;
procedure mo;
begin
assign(input,fi);
reset(input);
assign(output,fo);
rewrite(output);
end;

procedure nhap;
var i:longint;
begin
read(n);
for i:=1 to n do
read(a[i]);
end;

procedure xuly;
var i:longint;
begin
d:=0; i:=1;
repeat
dau[i]:=1;
i:=a[i];
inc(d);
until (dau[2]=1)or(dau[i]=1)or(kt);
end;

procedure xuat;
begin
if dau[2]=1 then
writeln(d-1)
else
writeln('-1');
end;

begin
//mo;
nhap;
xuly;
xuat;
end.


var a,dau:array[0..100005] of longint;
    n,d:longint;
    kt:boolean;

procedure nhap;
var i:longint;
begin
    readln(n);
    for i:=1 to n do
        readln(a[i]);
end;

procedure xuly;
var i:longint;
begin
    d:=0;   i:=1;
    repeat
        dau[i]:=1;
        i:=a[i];
        inc(d);
    until (dau[2]=1)or(dau[i]=1)or(kt);
end;

procedure xuat;
begin
    if dau[2]=1 then
        writeln(d-1)
    else
        writeln('-1');
end;

begin

    nhap;
    xuly;
    xuat;
end.


const fi='trained.inp';
      fo='trained.out';
var a,dau:array[0..100005] of longint;
    n,d:longint;
    kt:boolean;
procedure mo;
begin
    assign(input,fi);
    reset(input);
    assign(output,fo);
    rewrite(output);
end;

procedure nhap;
var i:longint;
begin
    readln(n);
    for i:=1 to n do
        readln(a[i]);
end;

procedure xuly;
var i:longint;
begin
    d:=0;
    repeat
        dau[i]:=1;
        i:=a[i];
        inc(d);
    until (dau[2]=1)or(dau[i]=1)or(kt);
end;

procedure xuat;
begin
    if dau[2]=1 then
        writeln(d)
    else
        writeln('-1');
end;

begin

    nhap;
    xuly;
    xuat;
end.


const fi='trained.inp';
      fo='trained.out';
var a,dau:array[0..100005] of longint;
    n,d:longint;
    kt:boolean;
procedure mo;
begin
    assign(input,fi);
    reset(input);
    assign(output,fo);
    rewrite(output);
end;

procedure nhap;
var i:longint;
begin
    readln(n);
    for i:=1 to n do
        readln(a[i]);
end;

procedure xuly;
var i:longint;
begin
    i:=1;
    kt:=false;
    while a[i] <> 2 do
    begin
        inc(d);
        dau[i]:=1;
        i:=a[i];
        if dau[i] = 1 then
        begin
            kt:=true;
            exit;
        end;
    end;
end;

procedure xuat;
begin
    if kt=true then
        writeln('-1')
    else
        writeln(d+1);
end;

begin

    nhap;
    xuly;
    xuat;
end.


const fi='trained.inp';
      fo='trained.out';
var a,dau:array[0..100005] of longint;
    n,d:longint;
    kt:boolean;
procedure mo;
begin
    assign(input,fi);
    reset(input);
    assign(output,fo);
    rewrite(output);
end;

procedure nhap;
var i:longint;
begin
    readln(n);
    for i:=1 to n do
        readln(a[i]);
end;

procedure xuly;
var i:longint;
begin
    i:=1;
    while a[i] <> 2 do
    begin
        inc(d);
        dau[i]:=1;
        i:=a[i];
        if dau[i] = 1 then
        begin
            kt:=true;
            exit;
        end;
    end;
end;

procedure xuat;
begin
    if kt=true then
        writeln('-1')
    else
        writeln(d+1);
end;

begin

    nhap;
    xuly;
    xuat;
end.


var a:array[0..100000]of longint;
    b:array[0..100000]of boolean;
    n,i,j:longint;
begin
  readln(n);
  for i:=1 to n do
    read(a[i]);
  i:=1;
  while true do
  begin
    inc(j);
    if a[i]=2 then
    begin
      writeln(j);
      halt;
    end;
    if b[i] then
    begin
      writeln(-1);
      halt;
    end;
    b[i]:=true;
    i:=a[i];
  end;
end.

Score : 200 points

Problem Statement

Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.

The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N.
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.
When Button i is not lighten up, nothing will happen by pressing it.

Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.

Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

Sample Input 1

3
3
1
2

Sample Output 1

2

Press Button 1, then Button 3.

Sample Input 2

4
3
4
1
2

Sample Output 2

-1

Pressing Button 1 lightens up Button 3, and vice versa, so Button 2 will never be lighten up.

Sample Input 3

5
3
3
4
2
4

Sample Output 3

3


program trained;
uses math;
var
  n, i, k: integer;
  a: array [1..100000] of integer;
begin
  read(n);
  for i := 1 to n do
    read(a[i]);
  i := 1;
  for k := 1 to n do
  begin
    i := a[i];
    if i = 2 then
    begin
      writeln(k);
      halt(0);
    end;
  end;
  writeln(-1);
end.


var n,i,s:Longint;a:array[1..100000]of Integer;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	s:=1;
	for i:=1 to n do begin
		s:=a[s];
		if s=2 then begin
			writeln(i);
			exit;
		end;
	end;
	writeln(-1);
end.


Var     n,kq,t: Longint;
        a: Array[0..1000005] Of Longint;

Procedure       Nhap;
Var     i: Longint;
Begin
Readln(n);
For i:=1 to n do
        Readln(a[i]);
End;

Procedure       XuLy;
Begin
t:=1; kq:=0;
Repeat
        Inc(kq);
        //If a[a[t]]=t Then Break;
        t:=a[t];
Until (t=2) or (kq>n+1);
End;

Procedure       Xuat;
Begin
If t<>2 Then Writeln(-1)
Else    Writeln(kq);
End;

Begin
Nhap;
XuLy;
Xuat;
End.


Var     n,kq,t: Longint;
        a: Array[0..1000005] Of Longint;

Procedure       Nhap;
Var     i: Longint;
Begin
Readln(n);
For i:=1 to n do
        Readln(a[i]);
End;

Procedure       XuLy;
Begin
t:=1; kq:=0;
Repeat
        Inc(kq);
        If a[a[t]]=t Then Break;
        t:=a[t];
Until (t=2) or (kq>n);
End;

Procedure       Xuat;
Begin
If t<>2 Then Writeln(-1)
Else    Writeln(kq);
End;

Begin
Nhap;
XuLy;
Xuat;
End.


Program ABC065_B;
const   MaxN=100005;
var n,i,cnt:longint;
    a,d:array[0..MaxN] of longint;

begin
    read(n);
    for i:=1 to n do read(a[i]);
    i:=1;
    cnt:=0;
    repeat
        d[i]:=1;
        i:=a[i];
        inc(cnt);
    until (d[2]=1)or(d[i]=1);
    if d[2]=1 then write(cnt-1)
    else write(-1);
end.


Var N,i,Dem:Longint;
    A:Array[1..100000] Of Longint;
    B:Array[1..100000] Of Boolean;
Begin
        //Assign(Input,'TRAINED.INP'); Reset(Input);
        //Assign(Output,'TRAINED.OUT'); Rewrite(Output);
        Readln(N);
        Fillchar(B,SizeOf(B),False);
        For i:=1 to N Do
                Begin
                Readln(A[i]);
                End;
        Dem:=0;
        i:=1;
        B[1]:=True;
        While i<>2 Do
                Begin
                B[i]:=False;
                B[A[i]]:=True;
                i:=A[i];
                Inc(Dem);
                If Dem>N Then Break;
                End;
        If Dem>N Then Write('-1') Else Write(Dem);
        //Close(Input);
        //Close(Output);
End.


Var     n,kq,i: Longint;
        a: Array[0..100005] Of Longint;

Procedure       Nhap;
Var     i: Longint;
Begin
Readln(n);
For i:=1 to n do
        Readln(a[i]);
End;

Procedure       XuLy;
Begin
i:=1; kq:=0;
Repeat
        Inc(kq);
        If a[a[i]]=i Then Break;
        i:=a[i];
Until i=2;
End;

Procedure       Xuat;
Begin
If i<>2 Then Writeln(-1)
Else    Writeln(kq);
End;

Begin
Nhap;
XuLy;
Xuat;
End.


Score : 300 points

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of possible arrangements, modulo 10^9+7.

Sample Input 1

2 2

Sample Output 1

8

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477


program reconciled;
uses math;
const
  md: int64 = 1000000007;
  f20000: int64 = 368774859;
  f40000: int64 = 422550956;
  f60000: int64 = 309944332;
  f80000: int64 = 65533322;
function fact(n: int64): int64;
var
  i, k, f: int64;
begin
  f := f80000;
  k := 80001;
  if n < 80000 then
  begin
    f := f60000;
    k := 60001;
  end;
  if n < 60000 then
  begin
    f := f40000;
    k := 40001;
  end;
  if n < 40000 then
  begin
    f := f20000;
    k := 20001;
  end;
  if n < 20000 then
  begin
    f := 1;
    k := 1;
  end;
  for i := k to n do
    f := i*f mod md;
  fact := f;
end;
var
  n, m, x: int64;
begin
  read(n, m);
  if abs(n-m) > 1 then
  begin
    writeln(0);
    halt(0);
  end;
  x := ifthen(n = m, 2, 1);
  x := x*fact(n) mod md;
  x := x*fact(m) mod md;
  writeln(x);
end.


program reconciled;
uses math;
const
  md: int64 = 1000000007;
  f25000: int64 = 232201666;
  f50000: int64 = 737935835;
  f75000: int64 = 796945456;
function fact(n: int64): int64;
var
  i, k, f: int64;
begin
  f := f75000;
  k := 75001;
  if n < 75000 then
  begin
    f := f50000;
    k := 50001;
  end;
  if n < 50000 then
  begin
    f := f25000;
    k := 25001;
  end;
  if n < 25000 then
  begin
    f := 1;
    k := 1;
  end;
  for i := k to n do
    f := i*f mod md;
  fact := f;
end;
var
  n, m, x: int64;
begin
  read(n, m);
  if abs(n-m) > 1 then
  begin
    writeln(0);
    halt(0);
  end;
  x := ifthen(n = m, 2, 1);
  x := x*fact(n) mod md;
  x := x*fact(m) mod md;
  writeln(x);
end.


var du,n,m,res:int64;
    i:longint;
begin
du:=round(1e9)+7;
readln(n,m);
if abs(n-m)>=2 then
    begin
       writeln(0);
       halt;
    end;
res:=1;
for i:=1 to n do
   res:=(res*i)   mod du;
for i:=1 to m do
    res:=(res*i) mod du;
if n=m then res:=(res*2) mod du;
writeln(res);
end.


Var A,B,N:Longint;
    D:Array[1..100000] Of Qword;

Procedure Xuli;
Var i:Longint;
Begin
        D[1]:=1;
        For i:=2 to 100000 Do D[i]:=((D[i-1] Mod 1000000007)*(i Mod 1000000007))Mod 1000000007;
End;

Begin
        //Assign(Input,'TRAINED.INP'); Reset(Input);
        //Assign(Output,'TRAINED.OUT'); Rewrite(Output);
        Readln(A,B);
        Xuli;
        If ABS(A-B)>1 Then
                Begin
                Write(0);
                Halt;
                End;
        N:=(D[B]*D[A])Mod 1000000007;
        If A=B Then
                Begin
                N:=(D[B]*D[A]*2)Mod 1000000007;
                Write(N);
                Halt;
                End;
        Write(N);
        //Close(Input);
        ///Close(Output);
End.


var
        m,i,j,a,b,c,d,x,y,n:longint;
        gtx,gty:real;
        xx1,xx2,xa,xb,xy1,xy2:array [0..1000000] of real;
function max(a,b:real):real;
        begin
        if a>b then exit(a);
        exit(b);
        end;
function min(a,b:real):real;
        begin
        if a>b then exit(b);
        exit(a);
        end;
function kt(var a,b:real; c,d:longint):boolean;
        begin
        if (max(xx1[c],xx2[c])>=a) and (min(xx1[c],xx2[c])<=a) and (max(xy1[c],xy2[c])>=b) and (min(xy1[c],xy2[c])<=b) then
                exit(true);
        if (max(xx1[d],xx2[d])>=a) and (min(xx1[d],xx2[d])<=a) and (max(xy1[d],xy2[d])>=b) and (min(xy1[d],xy2[d])<=b) then
                exit(true);
        exit(false);
        end;
begin
readln(x,y,n);
for i:=1 to n do
        begin
        readln(a,b,c,d);
        if ((a=x) or (a=0) or (b=y) or (b=0)) and ((c=x) or (c=0) or (d=y) or (d=0)) then
                if a<>c then
                        begin
                        inc(m);
                        xa[m]:=(b-d)/(a-c);
                        xb[m]:=b-a*xa[m];
                        xx1[m]:=a;
                        xy1[m]:=b;
                        xx2[m]:=c;
                        xy2[m]:=d;
                        end;
        end;
for i:=1 to m do
        for j:=i+1 to m do
                begin
                gtx:=-(xb[i]-xb[j])/(xa[i]-xa[j]);
                gty:=xa[i]*gtx+xb[i];
                if kt(gtx,gty,i,j) then
                        begin
                        writeln('NO');
                        halt;
                        end;
                end;
writeln('YES');
end.

Score : 700 points

Problem Statement

Snuke is playing a puzzle game.
In this game, you are given a rectangular board of dimensions R × C, filled with numbers. Each integer i from 1 through N is written twice, at the coordinates (x_{i,1},y_{i,1}) and (x_{i,2},y_{i,2}).

The objective is to draw a curve connecting the pair of points where the same integer is written, for every integer from 1 through N.
Here, the curves may not go outside the board or cross each other.

Determine whether this is possible.

Constraints

1 ≤ R,C ≤ 10^8

1 ≤ N ≤ 10^5

0 ≤ x_{i,1},x_{i,2} ≤ R(1 ≤ i ≤ N)

0 ≤ y_{i,1},y_{i,2} ≤ C(1 ≤ i ≤ N)

All given points are distinct.

All input values are integers.

Input

Input is given from Standard Input in the following format:

R C N
x_{1,1} y_{1,1} x_{1,2} y_{1,2}
:
x_{N,1} y_{N,1} x_{N,2} y_{N,2}

Output

Print YES if the objective is achievable; print NO otherwise.

Sample Input 1

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

Sample Output 1

YES

The above figure shows a possible solution.

Sample Input 2

2 2 4
0 0 2 2
2 0 0 1
0 2 1 2
1 1 2 1

Sample Output 2

NO

Sample Input 3

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

Sample Output 3

YES

Sample Input 4

1 1 2
0 0 1 1
1 0 0 1

Sample Output 4

NO


Uses Math,A52,Xqc;
Const int_MAX=2147483647;
Var maxx,minn,i,x,n:Longint;
Begin{main}
   minn:=int_MAX;
   maxx:=-int_MAX;
   readln(n);
   for i:=1 to n do
     begin
        read(x);
        maxx:=max(maxx,x);
        minn:=min(minn,x);
     end;
     write(maxx-minn);
End.

Score : 200 points

Problem Statement

It is only six months until Christmas, and AtCoDeer the reindeer is now planning his travel to deliver gifts.

There are N houses along TopCoDeer street. The i-th house is located at coordinate a_i. He has decided to deliver gifts to all these houses.

Find the minimum distance to be traveled when AtCoDeer can start and end his travel at any positions.

Constraints

1 ≤ N ≤ 100

0 ≤ a_i ≤ 1000

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum distance to be traveled.

Sample Input 1

4
2 3 7 9

Sample Output 1

7

The travel distance of 7 can be achieved by starting at coordinate 9 and traveling straight to coordinate 2.

It is not possible to do with a travel distance of less than 7, and thus 7 is the minimum distance to be traveled.

Sample Input 2

8
3 1 4 1 5 9 2 6

Sample Output 2

8

There may be more than one house at a position.


//const fi='colorful.inp';
 //     fo='colorful.out';
var a:array[0..10000] of longint;
    kt1,kt2,kt3,kt4,kt5,kt6,kt7,kt8:boolean;
    n,d,dp:longint;
//procedure mo;
//begin
 //       assign(input,fi);
 //       reset(input);
 //       assign(output,fo);
 //       rewrite(output);
//end;

procedure nhap;
var i:longint;
begin
        readln(n);
        for i:=1 to n do
        begin
                read(a[i]);
                if a[i] >= 3200 then
                        inc(dp)
        end;
end;

procedure xuly;
var i:longint;
begin
        for i:=1 to n do
        begin
                if (a[i] > 2799)and(a[i] <= 3199) then
                begin
                        if kt1=true then
                        begin
                                kt1:=false;
                                inc(d);
                        end;
                end;
                if (a[i] > 2399)and(a[i] <= 2799) then
                begin
                        if kt2=true then
                        begin
                                kt2:=false;
                                inc(d);
                        end;
                end;
                if (a[i] > 1999)and(a[i] <= 2399) then
                begin
                        if kt3=true then
                        begin
                                kt3:=false;
                                inc(d);
                        end;
                end;
                if (a[i] > 1599)and(a[i] <= 1999) then
                begin
                        if kt4=true then
                        begin
                                kt4:=false;
                                inc(d);
                        end;
                end;
                if (a[i] > 1199)and(a[i] <= 1599) then
                begin
                        if kt5=true then
                        begin
                                kt5:=false;
                                inc(d);
                        end;
                end;
                if (a[i] > 799)and(a[i] <= 1999) then
                begin
                        if kt6=true then
                        begin
                                kt6:=false;
                                inc(d);
                        end;
                end;
                if (a[i] > 399)and(a[i] <= 799) then
                begin
                        if kt7=true then
                        begin
                                kt7:=false;
                                inc(d);
                        end;
                end;
                if (a[i] >= 1)and(a[i] <= 399) then
                begin
                        if kt8=true then
                        begin
                                kt8:=true;
                                inc(d);
                        end;
                end;
        end;
end;

procedure xuat;
begin
       if dp=n then
       begin
                write(1,' ',n);
       end
       else
        write(d,' ',d+dp);
end;

begin
       // mo;
        nhap;
        kt1:=true;
        kt2:=true;
        kt3:=true;
        kt4:=true;
        kt5:=true;
        kt6:=true;
        kt7:=true;
        kt8:=true;
        xuly;
        xuat;
end.

Score : 300 points

Problem Statement

In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows:

Rating 1-399 : gray

Rating 400-799 : brown

Rating 800-1199 : green

Rating 1200-1599 : cyan

Rating 1600-1999 : blue

Rating 2000-2399 : yellow

Rating 2400-2799 : orange

Rating 2800-3199 : red

Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not.

Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i.

Find the minimum and maximum possible numbers of different colors of the users.

Constraints

1 ≤ N ≤ 100

1 ≤ a_i ≤ 4800

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between.

Sample Input 1

4
2100 2500 2700 2700

Sample Output 1

2 2

The user with rating 2100 is "yellow", and the others are "orange". There are two different colors.

Sample Input 2

5
1100 1900 2800 3200 3200

Sample Output 2

3 5

The user with rating 1100 is "green", the user with rating 1900 is blue and the user with rating 2800 is "red".

If the fourth user picks "red", and the fifth user picks "blue", there are three different colors. This is one possible scenario for the minimum number of colors.

If the fourth user picks "purple", and the fifth user picks "black", there are five different colors. This is one possible scenario for the maximum number of colors.

Sample Input 3

20
800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990

Sample Output 3

1 1

All the users are "green", and thus there is one color.


var
        x,i,n,s:longint;
        a:array [0..10] of longint;
begin
readln(n);
for i:=1 to n do
        begin
        read(x);
        if x<400 then a[1]:=1 else
        if x<800 then a[2]:=1 else
        if x<1200 then a[3]:=1 else
        if x<1600 then a[4]:=1 else
        if x<2000 then a[5]:=1 else
        if x<2400 then a[6]:=1 else
        if x<2800 then a[7]:=1 else
        if x<3200 then a[8]:=1 else
        inc(a[9]);
        end;
for i:=1 to 8 do s:=s+a[i];
if a[9]<>0 then writeln(s,' ',s+a[9])
else writeln(s,' ',s);
end.


program j01;
const maxn=400086;
type opr=record w:int64;id:longint;end;
var sum:array[0..maxn]of int64;
    op:array[0..maxn]of opr;
    tot:longint;
    ans:int64;
    c,a:array[0..maxn]of longint;
    n,i,k:longint;

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

procedure disc;
var i:longint;
begin
  sort(1,n);
  tot:=1;a[op[1].id]:=1;
  for i:=2 to n do
  begin
    if op[i].w<>op[i-1].w then inc(tot);
    a[op[i].id]:=tot;
  end;
end;

procedure ins(i:longint);
begin
  while i<=tot do
  begin
    inc(c[i]);inc(i,i and(-i));
  end;
end;

function ask(i:longint):longint;
var res:longint;
begin
  res:=0;
  while i>0 do
  begin
    inc(res,c[i]);dec(i,i and(-i));
  end;
  exit(res);
end;

begin
  readln(n,k);
  for i:=1 to n do read(a[i]);
  for i:=1 to n do a[i]:=a[i]-k;
  sum[1]:=0;inc(n);for i:=2 to n do sum[i]:=sum[i-1]+a[i-1];
  for i:=1 to n do
  begin
    op[i].w:=sum[i];op[i].id:=i;
  end;
  //for i:=1 to n do write(sum[i],' ');writeln;
  disc;
  for i:=1 to n do
  begin
    inc(ans,ask(a[i]));ins(a[i]);
  end;
  writeln(ans);
end.

Score : 300 points

Problem Statement

You are taking a computer-based examination. The examination consists of N questions, and the score allocated to the i-th question is s_i. Your answer to each question will be judged as either "correct" or "incorrect", and your grade will be the sum of the points allocated to questions that are answered correctly. When you finish answering the questions, your answers will be immediately judged and your grade will be displayed... if everything goes well.

However, the examination system is actually flawed, and if your grade is a multiple of 10, the system displays 0 as your grade. Otherwise, your grade is displayed correctly. In this situation, what is the maximum value that can be displayed as your grade?

Constraints

All input values are integers.

1 ≤ N ≤ 100

1 ≤ s_i ≤ 100

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the maximum value that can be displayed as your grade.

Sample Input 1

3
5
10
15

Sample Output 1

25

Your grade will be 25 if the 10-point and 15-point questions are answered correctly and the 5-point question is not, and this grade will be displayed correctly. Your grade will become 30 if the 5-point question is also answered correctly, but this grade will be incorrectly displayed as 0.

Sample Input 2

3
10
10
15

Sample Output 2

35

Your grade will be 35 if all the questions are answered correctly, and this grade will be displayed correctly.

Sample Input 3

3
10
20
30

Sample Output 3

0

Regardless of whether each question is answered correctly or not, your grade will be a multiple of 10 and displayed as 0.


const   fi      ='';
        fo      ='';

var     n,top:longint;
        f,g:text;
        a,q,vt:array[0..300005] of longint;
        l,r:array[0..300005] of int64;

procedure       nhap;
var     i:longint;
begin
        assign(f,fi);reset(f);
        readln(f,n);
        for i:=1 to 3*n do
                read(f,a[i]);
        close(f);
end;

procedure       up(k    :longint);
var     v       :longint;
begin
        v:=q[k];
        while (a[v]<a[q[k div 2]]) do
                begin
                        q[k]:=q[k div 2];
                        vt[q[k]]:=k;
                        k:=k div 2;
                end;
        q[k]:=v;
        vt[q[k]]:=k;
end;

procedure       down(k  :longint);
var     l1,v    :longint;
begin
        v:=q[k];
        while 2*k<=top do
                begin
                        l1:=2*k;
                        if (l1<top) and (a[q[l1]]>a[q[l1+1]]) then inc(l1);
                        if a[q[l1]]>=a[v] then break;
                        q[k]:=q[l1];
                        vt[q[k]]:=k;
                        k:=l1;
                end;
        q[k]:=v;
        vt[v]:=k;
end;

procedure       put(u   :longint);
begin
        inc(top); q[top]:=u; vt[u]:=top;
        up(top);
end;

function        get     :longint;
begin
        get:=q[1];
        q[1]:=q[top]; vt[q[1]]:=1;
        dec(top);
        down(1);
end;

procedure       up1(k    :longint);
var     v       :longint;
begin
        v:=q[k];
        while (a[v]>a[q[k div 2]]) do
                begin
                        q[k]:=q[k div 2];
                        vt[q[k]]:=k;
                        k:=k div 2;
                end;
        q[k]:=v;
        vt[q[k]]:=k;
end;

procedure       down1(k  :longint);
var     l1,v    :longint;
begin
        v:=q[k];
        while 2*k<=top do
                begin
                        l1:=2*k;
                        if (l1<top) and (a[q[l1]]<a[q[l1+1]]) then inc(l1);
                        if a[q[l1]]<=a[v] then break;
                        q[k]:=q[l1];
                        vt[q[k]]:=k;
                        k:=l1;
                end;
        q[k]:=v;
        vt[v]:=k;
end;

procedure       put1(u   :longint);
begin
        inc(top); q[top]:=u; vt[u]:=top;
        up1(top);
end;

function        get1     :longint;
begin
        get1:=q[1];
        q[1]:=q[top]; vt[q[1]]:=1;
        dec(top);
        down1(1);
end;

procedure       xl;
var     tong,res:int64;
        i,u:longint;
begin
        assign(g,fo);rewrite(g);
        tong:=0;  top:=0;q[0]:=0;a[0]:=-round(1e9);
        for i:=1 to n do
        begin
                tong:=tong+a[i];
                put(i);
        end;
        l[n]:=tong;
        for i:=n+1 to 2*n do
        begin
                if (a[i]>a[q[1]]) and (top>0) then
                begin
                        u:=get;
                        l[i]:=l[i-1]+a[i]-a[u];
                        put(i);
                end else begin l[i]:=l[i-1];end;
        end;
        tong:=0;top:=0;q[0]:=0;a[0]:=round(1e9);
        for i:=3*n downto 2*n+1 do
        begin
                tong:=tong+a[i];
                put1(i);
        end;
        r[2*n+1]:=tong;
        for i:=2*n downto n+1 do
        begin
                if (a[i]<a[q[1]]) and (top>0) then
                begin
                        u:=get1;
                        r[i]:=r[i+1]+a[i]-a[u];
                        put1(i);
                end else begin r[i]:=r[i+1];end;
        end;
        res:=-round(1e18);
        for i:=n to 2*n do
                if res<l[i]-r[i+1] then res:=l[i]-r[i+1];
        write(g,res);
        close(g);
end;
begin
        nhap;
        xl;
end.

Score : 500 points

Problem Statement

Let N be a positive integer.

There is a numerical sequence of length 3N, a = (a_1, a_2, ..., a_{3N}).
Snuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements.
Here, the score of a' is defined as follows: (the sum of the elements in the first half of a') - (the sum of the elements in the second half of a').

Find the maximum possible score of a'.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Partial Score

In the test set worth 300 points, N ≤ 1000.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

When a_2 and a_6 are removed, a' will be (3, 4, 1, 5), which has a score of (3 + 4) - (1 + 5) = 1.

Sample Input 2

1
1 2 3

Sample Output 2

-1

For example, when a_1 are removed, a' will be (2, 3), which has a score of 2 - 3 = -1.

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

For example, when a_2, a_3 and a_9 are removed, a' will be (8, 7, 4, 6, 5, 3), which has a score of (8 + 7 + 4) - (6 + 5 + 3) = 5.


Const   fi='';
        fo='';
Var     f:Text;
        n:Longint;
        A:Array[0..300000] of Longint;
        L1,L2:Array[0..300000] of Int64;
        Heapmin,Heapmax:Array[0..300000] of Longint;
        nheapmin,nheapmax:Longint;   sol:Int64;
Function max(i,j:Int64):Int64; Begin If i<j then Exit(j); Exit(i); End;
Procedure Inp;
Var     i:Longint;
Begin
        Assign(f,fi);   Reset(f);
        Read(f,n);
        For i:=1 to 3*n do Read(f,A[i]);
        Close(f);
End;
Procedure Swap(Var i,j:Longint);
Var     tg:Longint;
Begin
        tg:=i; i:=j; j:=tg;
End;
Procedure Upheapmin(x:Longint);
Begin
        If (x=1) or (A[Heapmin[x]]>=A[Heapmin[x div 2]]) then Exit
        Else Begin
                Swap(Heapmin[x],Heapmin[x div 2]);
                Upheapmin(x div 2);
        End;
End;
Procedure Downheapmin(x:Longint);
Var     vt:Longint;
Begin
        If (x>nheapmin div 2) then Exit;
        If (A[Heapmin[x*2]]>=A[Heapmin[x*2+1]]) and (x*2+1<=nheapmin) then vt:=x*2+1 Else vt:=x*2;
        If (A[Heapmin[x]]<=A[heapmin[vt]]) then Exit
        Else Begin
                Swap(heapmin[x],heapmin[vt]);
                Downheapmin(vt);
        End;
End;
Procedure Pushmin(x:Longint);
Begin
        Inc(nheapmin);
        heapmin[nheapmin]:=x;
        upheapmin(nheapmin);
End;
Procedure popmin(x:Longint);
Begin
        heapmin[1]:=x;
        downheapmin(1);
End;
Procedure Upheapmax(x:Longint);
Begin
        If (x=1) or (A[Heapmax[x]]<=A[Heapmax[x div 2]]) then Exit
        Else Begin
                Swap(Heapmax[x],Heapmax[x div 2]);
                Upheapmax(x div 2);
        End;
End;
Procedure Downheapmax(x:Longint);
Var     vt:Longint;
Begin
        If (x>nheapmax div 2) then Exit;
        If (A[Heapmax[x*2]]<=A[Heapmax[x*2+1]]) and (x*2+1<=nheapmax) then vt:=x*2+1 Else vt:=x*2;
        If (A[Heapmax[x]]>=A[heapmax[vt]]) then Exit
        Else Begin
                Swap(heapmax[x],heapmax[vt]);
                Downheapmax(vt);
        End;
End;
Procedure Pushmax(x:Longint);
Begin
        Inc(nheapmax);
        heapmax[nheapmax]:=x;
        upheapmax(nheapmax);
End;
Procedure popmax(x:Longint);
Begin
        heapmax[1]:=x;
        downheapmax(1);
End;
Procedure tongmax;
Var     i,j,k:Longint;
Begin
        k:=0;
        For i:=1 to n do
        Begin
                k:=k+A[i];
                Pushmin(i);
        End;
        L1[n]:=k;
        For i:=n+1 to 3*n do
        Begin
                If A[i]>A[heapmin[1]] then Begin L1[i]:=L1[i-1]-A[heapmin[1]]+A[i]; Popmin(i); End
                Else L1[i]:=L1[i-1];
        End;
End;
Procedure tongmin;
Var     i,j,k:Longint;
Begin
        k:=0;
        For i:=3*n downto 2*n+1 do
        Begin
                k:=k+A[i];
                Pushmax(i);
        End;
        L2[2*n+1]:=k;
        For i:=2*n downto 1 do
        Begin
                If A[i]<A[heapmax[1]] then Begin L2[i]:=L2[i+1]-A[heapmax[1]]+A[i]; Popmax(i); End
                Else L2[i]:=L2[i+1];
        End;
End;
Procedure Solve;
Var     i,j:Longint;
Begin
        tongmax;
        tongmin;
        sol:=-trunc(1e18);
        For i:=n to 2*n do sol:=max(sol,L1[i]-L2[i+1]);
        Assign(f,fo);   Rewrite(f);
        Write(f,sol);
        Close(f);
End;
BEGIN
        Inp;
        Solve;
END.