type
  pair = record
    a : longint;
    b : longint;
  end;
	pairArray = array[1..100000] of pair;

var ans,i,j,n,m:longint;
    input:pairArray;
    heap:array[0..100000] of longint;

procedure swap(var X, Y: longint);
begin
	if X <> Y then begin
		X := X xor Y;
		Y := X xor Y;
		X := X xor Y
	end
end;

procedure	pswap(var a,b:pair);
var tmp:pair;
begin
	tmp:=a;
	a:=b;
	b:=tmp
end;

procedure QuickSort(var a:pairArray;start_index,end_index:longint);
var i,j,x:int64;
begin
	x:=a[(start_index+end_index)div 2].a;
	i:=start_index;
	j:=end_index;
	while(true)do
	begin
		while(a[i].a<x)do i:=i+1;
		while(x<a[j].a)do j:=j-1;
		if(i<j)then
		begin
			pswap(a[i],a[j]);
			i:=i+1;
			j:=j-1;
		end else
		begin
			if(start_index<i-1)then QuickSort(a,start_index,i-1);
			if(j+1<end_index)then QuickSort(a,j+1,end_index);
			exit;
		end;
	end;
end;

procedure upheap(b:longint);
var p:integer;
begin
  heap[0] := heap[0] + 1;
  p := heap[0];
  heap[p] := b;
  while (p>1) and (heap[p]>heap[p div 2]) do begin
    swap(heap[p],heap[p div 2]);
    p := p div 2
  end
end;

function downheap:longint;
var p:integer;
begin
  downheap:=heap[1];
  heap[1] := heap[heap[0]];
  heap[heap[0]] := 0;
  heap[0] := heap[0] - 1;
  if heap[0]>1 then begin
    p := 1;
    while (p*2 <= heap[0]) and (heap[p]<heap[p*2]) or (p*2+1 <= heap[0]) and (heap[p]<heap[p*2+1]) do
      if (p*2+1 <= heap[0]) and (heap[p]<heap[p*2+1]) then begin
        swap(heap[p],heap[p*2+1]);
        p := p*2+1
      end else begin
        swap(heap[p],heap[p*2]);
        p := p*2
      end
  end
end;

begin
  readln(n,m);
  for i := 1 to n do readln(input[i].a,input[i].b);
  QuickSort(input,1,n);
  j := 1;
  ans := 0;
  for i := 1 to m do begin
    while input[j].a=i do begin
      upheap(input[j].b);
      j := j + 1
    end;
    if heap[0]>0 then ans := ans + downheap
  end;
  writeln(ans)
end.


type
  pair = record
    a : longint;
    b : longint;
  end;
	pairArray = array[1..100000] of pair;

var ans,i,j,n,m:longint;
    input:pairArray;
    heap:array[0..100000] of longint;

procedure swap(var X, Y: longint);
begin
	if X <> Y then begin
		X := X xor Y;
		Y := X xor Y;
		X := X xor Y
	end
end;

procedure	pswap(var a,b:pair);
var tmp:pair;
begin
	tmp:=a;
	a:=b;
	b:=tmp
end;

procedure QuickSort(var a:pairArray;start_index,end_index:longint);
var i,j,x:int64;
begin
	x:=a[(start_index+end_index)div 2].a;
	i:=start_index;
	j:=end_index;
	while(true)do
	begin
		while(a[i].a<x)do i:=i+1;
		while(x<a[j].a)do j:=j-1;
		if(i<j)then
		begin
			pswap(a[i],a[j]);
			i:=i+1;
			j:=j-1;
		end else
		begin
			if(start_index<i-1)then QuickSort(a,start_index,i-1);
			if(j+1<end_index)then QuickSort(a,j+1,end_index);
			exit;
		end;
	end;
end;

procedure upheap(b:longint);
var p:integer;
begin
  heap[0] := heap[0] + 1;
  p := heap[0];
  heap[p] := b;
  while (p>1) and (heap[p]>heap[p div 2]) do begin
    swap(heap[p],heap[p div 2]);
    p := p div 2
  end
end;

function downheap:longint;
var p:integer;
begin
  downheap:=heap[1];
  heap[1] := heap[heap[0]];
  heap[0] := heap[0] - 1;
  p := 1;
  while (p*2 <= heap[0]) and ((heap[p]<heap[p*2]) or (heap[p]<heap[p*2+1])) do
    if (p*2+1 <= heap[0]) and (heap[p]<heap[p*2+1]) then begin
      swap(heap[p],heap[p*2+1]);
      p := p*2+1
    end else begin
      swap(heap[p],heap[p*2]);
      p := p*2
    end
end;

begin
  readln(n,m);
  for i := 1 to n do readln(input[i].a,input[i].b);
  QuickSort(input,1,n);
  j := 1;
  for i := 1 to m do begin
    while input[j].a=i do begin
      upheap(input[j].b);
      j := j + 1
    end;
    if heap[0]>0 then ans := ans + downheap
  end;
  writeln(ans)
end.

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

Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

Take and complete the first job today. You will earn the reward of 3 after four days from today.

Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

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

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0

Submitted source-code file size, in bytes.

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

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


var n,i,ans :integer;
begin
  ans:=0;
  read(n);
  for i:=1 to n do
    if (i<10)or((i div 100>0)and(i div 100<10))or((i div 10000>0)and(i div 10000<10))
    then inc(ans);
  write(ans);
end.

Score : 200 points

Problem Statement

Given is an integer N. Find the number of positive integers less than or equal to N that have an odd number of digits (in base ten without leading zeros).

Constraints

1 \leq N \leq 10^5

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of positive integers less than or equal to N that have an odd number of digits.

Sample Input 1

11

Sample Output 1

9

Among the positive integers less than or equal to 11, nine integers have an odd number of digits: 1, 2, \ldots, 9.

Sample Input 2

136

Sample Output 2

46

In addition to 1, 2, \ldots, 9, another 37 integers also have an odd number of digits: 100, 101, \ldots, 136.

Sample Input 3

100000

Sample Output 3

90909


var a,b,i:int64;
begin
  read(a,b);
  for i:=1 to 1000000001 do
    if abs(a-i)=abs(b-i) then
    begin
      write(i);
      break;
    end;
  if i=1000000001 then write('IMPOSSIBLE');
end.

Score: 100 points

Problem Statement

We have two distinct integers A and B.

Print the integer K such that |A - K| = |B - K|.

If such an integer does not exist, print IMPOSSIBLE instead.

Constraints

All values in input are integers.

0 \leq A,\ B \leq 10^9

A and B are distinct.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the integer K satisfying the condition.

If such an integer does not exist, print IMPOSSIBLE instead.

Sample Input 1

2 16

Sample Output 1

9

|2 - 9| = 7 and |16 - 9| = 7, so 9 satisfies the condition.

Sample Input 2

0 3

Sample Output 2

IMPOSSIBLE

No integer satisfies the condition.

Sample Input 3

998244353 99824435

Sample Output 3

549034394


Uses math;
var n,i,b,count:int64;
    a:array[1..100010]of int64;
begin
read(n);count:=0;
for i := 1 to n+1 do read(a[i]);
for i := 1 to n do begin
read(b);
if a[i]>b then begin count:=count+b;continue end;
count:=count+a[i]+min(b-a[i],a[i+1]);
a[i+1]:=max(a[i+1]-b+a[i],0)
 end;
write(count);
end.

Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3


const maxn = trunc(1e5);
var a,b:array[1..maxn + 2] of longint;
    i,n:longint;
    re:int64;

begin
	readln(n);
	for i:=1 to n + 1 do read(a[i]);
	for i:=1 to n do read(b[i]);
	re:=0; b[n+1]:=0; a[n+2]:=0;
	for i:=1 to n + 1 do
		  begin
		    if a[i] >= b[i] then
		      begin
		        re:= re + b[i];
	          b[i]:=0;
		      end
		    else
		      begin
		        re:= re + a[i];
		        b[i]:=b[i] - a[i];
	          if a[i+1] >= b[i] then
	            begin
	              re:= re + b[i];
	              b[i]:=0;
	              a[i+1]:=a[i+1] - b[i];
	            end
	          else
	            begin
	              re:= re + a[i+1];
	              a[i+1]:=0; b[i]:=0;
	            end;
		      end;
		  end;
	write(re);
end.


const side=1000000000;
      sq=1000000000000000000;
var s,i,j,x,y:int64;
    ans:array[1..3,1..2] of int64;
begin
read(s);
for i :=1 to 3 do
 for j :=1 to 2 do ans[i,j]:=0;
if s<=side then begin
ans[2,1]:=1;ans[3,2]:=s end
else
for i := 0 to side div 2 do
begin
 x:=s-i*i;
 y:=trunc(sqrt(x));
 if y*y=x then begin
 ans[2,1]:=x;ans[2,2]:=y;
 ans[3,1]:=x-y;ans[3,2]:=x+y;
 end;

end;
for i := 1 to 3 do
 for j := 1 to 2 do write(ans[i,j],' ');
end.

Score : 400 points

Problem Statement

Given is an integer S.
Find a combination of six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfies all of the following conditions:

0 \leq X_1,Y_1,X_2,Y_2,X_3,Y_3 \leq 10^9

The area of the triangle in a two-dimensional plane whose vertices are (X_1,Y_1),(X_2,Y_2), and (X_3,Y_3) is S/2.

We can prove that there always exist six integers that satisfy the conditions under the constraints of this problem.

Constraints

1 \leq S \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

Print six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfy the conditions, in this order, with spaces in between.
If multiple solutions exist, any of them will be accepted.

Sample Input 1

3

Sample Output 1

1 0 2 2 0 1

The area of the triangle in a two-dimensional plane whose vertices are (1,0),(2,2), and (0,1) is 3/2.
Printing 3 0 3 1 0 1 or 1 0 0 1 2 2 will also be accepted.

Sample Input 2

100

Sample Output 2

0 0 10 0 0 10

Sample Input 3

311114770564041497

Sample Output 3

314159265 358979323 846264338 327950288 419716939 937510582


var n,i,max1,max2:longint;
    a:array[1..200000] of longint;
begin
	readln(n);
	max1:=0; max2:=0;
	for i:=1 to n do
	  begin
	    read(a[i]);
	    if a[i] > max1 then max1:=a[i]
	    else if a[i] > max2 then max2:=a[i];
	  end;
	for i:=1 to n do
	  if a[i] = max1 then writeln(max2)
	  else writeln(max1);
end.

Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5


var N,a,maxa,premaxa,i,maxNo:int64;
begin
read(N);
maxa:=0;
premaxa:=0;
maxNo:=0;
for i := 1 to N do
 begin
  read(a);
  if a >= maxa then begin
   premaxa:=maxa; maxa:=a; maxNo:=i end
  else if a >= premaxa then premaxa:=a;
 end;
for i := 1 to n do
if i= maxNo then writeln(premaxa)
else writeln(maxa);
end.


const nmax=200010;
type ary=array[1..nmax] of 0..1;
var N,i,j,k,latter,sum,count:int64;
    a,ans:ary;
begin
read(N);latter:=0;count:=0;
for i := 1 to N do
 begin read(a[i]);
  if i > N div 2 then
   begin
    ans[i]:=a[i];
    inc(latter);
    if ans[i]=1 then inc(count)
   end
  end;
for i := (N-latter) downto 1 do
 begin
  sum:=0;
  for j := 2 to N do
   if i*j > N then break
   else sum:=sum+ans[i*j];
  ans[i]:=(sum+a[i]) mod 2;
  if ans[i]=1 then inc(count);
 end;
writeln(count);
for i := 1 to N do
 if ans[i]=1 then write(i,' ');
end.

Score : 400 points

Problem Statement

There are N empty boxes arranged in a row from left to right.
The integer i is written on the i-th box from the left (1 \leq i \leq N).

For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.

We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:

For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2.

Does there exist a good set of choices? If the answer is yes, find one good set of choices.

Constraints

All values in input are integers.

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

a_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If a good set of choices does not exist, print -1.

If a good set of choices exists, print one such set of choices in the following format:

M
b_1 b_2 ... b_M

where M denotes the number of boxes that will contain a ball, and b_1,\ b_2,\ ...,\ b_M are the integers written on these boxes, in any order.

Sample Input 1

3
1 0 0

Sample Output 1

1
1

Consider putting a ball only in the box with 1 written on it.

There are three boxes with multiples of 1 written on them: the boxes with 1, 2, and 3. The total number of balls contained in these boxes is 1.

There is only one box with a multiple of 2 written on it: the box with 2. The total number of balls contained in these boxes is 0.

There is only one box with a multiple of 3 written on it: the box with 3. The total number of balls contained in these boxes is 0.

Thus, the condition is satisfied, so this set of choices is good.

Sample Input 2

5
0 0 0 0 0

Sample Output 2

0

Putting nothing in the boxes can be a good set of choices.


const maxn = trunc(1e5);
var n,re,k,x,count,i:longint;
    d:array[1..maxn] of integer;
    a:array[1..maxn] of longint;

begin
	readln(n);
	fillchar(d,sizeof(d),0);
	for i:=1 to n do readln(a[i]);
	count:=1; x:=a[1]; k:=1; re:=0; d[1]:=1;
	repeat
	  for i:=k to n do
	    if (d[i] = 0) and (a[i] > x) then
	      begin
	        inc(count); d[i]:=1;
	      end;
	  for i:=2 to n do
	    if d[i] = 0 then
	      begin
	        x:=a[i]; d[i]:=1; inc(count); k:=i + 1;
	        if count = n then inc(re);
	        break;
	      end;
	  inc(re);
	until count = n;
	write(re);
end.

Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.


var n,inp,ans,i:Longint;
A:array[1..100001] of Longint;
begin
read(n);
for i:=1 to n do begin
  read(inp);
  ans:=ans xor inp;
end;
if ans = 0 then writeln('Yes')
else writeln('No');
end.

Score : 300 points

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

There are N camels standing in a circle.
Snuke will put one of his hats on each of these camels.

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.

What is XOR?

The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

- When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.

Sample Input 2

4
1 2 4 8

Sample Output 2

No

There is no such way to distribute the hats; the answer is No.


var n,i,x,j:int64;
    a:array[1..3,1..2]of int64;
    flag:boolean;
begin
read(n);
for i := 1 to 3 do a[i,1]:=-1;

for i := 1 to n do
 begin
 read(x);

 for j := 1 to 3 do
 begin
  flag:=false;

  if a[j,1]=x then
  begin
  flag:=true;
  inc(a[j,2]);
  break end;

  if a[j,1] < 0  then
  begin
  a[j,1]:=x;
  inc(a[j,2]);
  flag:=true;
  break end;
 end;

 if not flag then break;
end;

 if not flag then write('No');

 if flag then
 if ((a[1,2]=a[2,2])and(a[1,2]=a[3,2]))or
   (((a[1,2]=2*a[2,2])or(a[2,2]=2*a[1,2]))and(a[3,2]=0))
   or(a[1,1]=0) then write('Yes')
 else write('No');
end.


const nmax=100010;
var n,i:int64;
    j:0..30;
    a:array[1..nmax] of int64;
    count:array[0..1]of int64;
    flag:boolean;
begin
read(n);flag:=true;
for i := 1 to n do read(a[i]);

for j := 0 to 30 do
 begin
 count[0]:=0;count[1]:=0;
 for i := 1 to n do
 inc(count[a[i]>>1]);
 if not((count[1]=0)or(count[1]=2*count[0]))
        then flag:=false;
 end;
if flag then write('Yes') else write('No');
end.


var n,i,x,j:int64;
    a:array[1..3]of int64;
    flag:boolean;
begin
read(n);
for i := 1 to 3 do
 begin
 a[i]:=-1;
 flag:=false;
 end;
for i := 1 to n do
 begin
 read(x);

 for j := 1 to 3 do
 begin
 if a[j]=x then flag:=true;
 if a[j] < 0  then begin a[j]:=x;flag:=true end;
 end;
 if not flag then break;
 end;
 if flag then write('Yes') else write('No');
end.


uses math;
var n,a,b:longint;
begin
  readln(n,a,b);
  write(min(n*a,b));
end.

Score : 100 points

Problem Statement

N of us are going on a trip, by train or taxi.

The train will cost each of us A yen (the currency of Japan).

The taxi will cost us a total of B yen.

How much is our minimum total travel expense?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq A \leq 50

1 \leq B \leq 50

Input

Input is given from Standard Input in the following format:

N A B

Output

Print an integer representing the minimum total travel expense.

Sample Input 1

4 2 9

Sample Output 1

8

The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen.

Sample Input 2

4 2 7

Sample Output 2

7

Sample Input 3

4 2 8

Sample Output 3

8


Score : 400 points

Problem Statement

There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number.

Between these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \leq i \leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1).

When Mountain i (1 \leq i \leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N).

One day, each of the mountains received a non-negative even number of liters of rain.

As a result, Dam i (1 \leq i \leq N) accumulated a total of A_i liters of water.

Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem.

Constraints

All values in input are integers.

3 \leq N \leq 10^5-1

N is an odd number.

0 \leq A_i \leq 10^9

The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order.

Sample Input 1

3
2 2 4

Sample Output 1

4 0 4

If we assume Mountain 1, 2, and 3 received 4, 0, and 4 liters of rain, respectively, it is consistent with this input, as follows:

Dam 1 should have accumulated \frac{4}{2} + \frac{0}{2} = 2 liters of water.

Dam 2 should have accumulated \frac{0}{2} + \frac{4}{2} = 2 liters of water.

Dam 3 should have accumulated \frac{4}{2} + \frac{4}{2} = 4 liters of water.

Sample Input 2

5
3 8 7 5 5

Sample Output 2

2 4 12 2 8

Sample Input 3

3
1000000000 1000000000 0

Sample Output 3

0 2000000000 0


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


var i,n,j,res:longint;
    a:array[1..100000] of longint;
procedure sort(l,r:longint);
var i,j,x,y: longint;
begin
  i:=l; j:=r; x:=a[random(r-l+1)+l];
  repeat
    while a[i] < x do inc(i);
    while x < a[j] do dec(j);
    if not(i > j) then
      begin
        y:=a[i];
        a[i]:=a[j];
        a[j]:=y;
        inc(i);
        dec(j);
      end;
 until i > j;
 if l < j then sort(l,j);
 if i < r then sort(i,r);
end;
function find(l,r,x:longint):longint;
var  mid:longint;
begin
  while l <= r do
    begin
      mid:=(l+r) >> 1;
      if (a[mid] < x) then l:=mid + 1 else r:=mid -1;
    end;
  exit(l-1);
end;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  sort(1,n);
  for i:=1 to a[n] do if find(1,n,i) = n div 2 then inc(res);
  writeln(res)
end.

Score : 300 points

Problem Statement

Takahashi made N problems for competitive programming.
The problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

He is dividing the problems into two categories by choosing an integer K, as follows:

A problem with difficulty K or higher will be for ARCs.

A problem with difficulty lower than K will be for ABCs.

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

Problem Statement

2 \leq N \leq 10^5

N is an even number.

1 \leq d_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 1

6
9 1 4 4 6 7

Sample Output 1

2

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.
Thus, the answer is 2.

Sample Input 2

8
9 1 14 5 5 4 4 14

Sample Output 2

0

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 3

14
99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1

Sample Output 3

42685


const modP = 13;
var Kcomb,NKcomb,inv:array[0..2010]of int64;
    i,j,k,a,b,n,m,p:int64;

begin
  readln(n,k);

  inv[1]:=1;
  for i:=2 to k do begin
    inv[i]:=1;
    p:=modP-2;
    j:=i;
    while p>0 do begin
    if (p and 1)>0 then inv[i]:=inv[i]*j mod modP;
      j:=j*j mod modP;
      p:=p >> 1;
    end;
  end;

  NKcomb[0]:=1; NKcomb[n-k+1]:=1;
  Kcomb[0]:=1; Kcomb[k-1]:=1;
  for i:=1 to (k+1)div 2 do begin
    a:=k-i;
    Kcomb[i]:=(Kcomb[i-1] mod modP)*(a mod modP)*inv[i] mod modP ;
    Kcomb[k-1-i]:=Kcomb[i];
  end;
  m:=n-k+1;
  for i:=1 to (m+1)div 2 do begin
    a:=m-i+1;
    NKcomb[i]:=(NKcomb[i-1] mod modP)*(a mod modP)*inv[i] mod modP;
    NKcomb[m-i]:=NKcomb[i];
  end;

  for i:=1 to k do begin
    if i<=n-k+1 then begin
      b:=(NKcomb[i] mod modP)*(Kcomb[i-1] mod modP) mod modP;
      writeln(b);
    end else writeln(0);
  end;

end.

Score : 400 points

Problem Statement

There are K blue balls and N-K red balls. The balls of the same color cannot be distinguished. Snuke and Takahashi are playing with these balls.

First, Snuke will arrange the N balls in a row from left to right.

Then, Takahashi will collect only the K blue balls. In one move, he can collect any number of consecutive blue balls. He will collect all the blue balls in the fewest moves possible.

How many ways are there for Snuke to arrange the N balls in a row so that Takahashi will need exactly i moves to collect all the blue balls? Compute this number modulo 10^9+7 for each i such that 1 \leq i \leq K.

Constraints

1 \leq K \leq N \leq 2000

Input

Input is given from Standard Input in the following format:

N K

Output

Print K lines. The i-th line (1 \leq i \leq K) should contain the number of ways to arrange the N balls so that Takahashi will need exactly i moves to collect all the blue balls, modulo 10^9+7.

Sample Input 1

5 3

Sample Output 1

3
6
1

There are three ways to arrange the balls so that Takahashi will need exactly one move: (B, B, B, R, R), (R, B, B, B, R), and (R, R, B, B, B). (R and B stands for red and blue, respectively).

There are six ways to arrange the balls so that Takahashi will need exactly two moves: (B, B, R, B, R), (B, B, R, R, B), (R, B, B, R, B), (R, B, R, B, B), (B, R, B, B, R), and (B, R, R, B, B).

There is one way to arrange the balls so that Takahashi will need exactly three moves: (B, R, B, R, B).

Sample Input 2

2000 3

Sample Output 2

1998
3990006
327341989

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


const base = trunc(1e9) + 7;
var a:array[0..2000,0..2000] of int64;
    n,k,i,j:integer;
begin
	readln(n,k);
	a[0,0]:=1;
	for i:=1 to 2000 do
	  begin
        a[i,0]:=1;
	    for j:=1 to 2000 do
	       a[i,j]:=(a[i-1,j] + a[i-1,j-1]) mod base;
	  end;
    for i:=1 to k do
      writeln((a[k - 1, i - 1] * a[n - k + 1,i]) mod base);
end.


var
	N,K,i:Longint;
	inv:Array[1..2002]of Longint;
	B,R,M:int64;
begin
	read(N,K);
	inv[1]:=1;
	M:=1000000007;
	for i:=2 to K+2 do begin
		inv[i]:=M-M div i*inv[M mod i] mod M;
	end;
	B:=1;
	R:=N-K+1;
	for i:=0 to K-1 do begin
		writeln(B*R mod M);
		B:=B*(K-i-1)mod M*inv[i+1]mod M;
		R:=R*(N-K-i)mod M*inv[i+2]mod M;
	end;
end.


const md=1000000007;
var n,k,i,nk1,red,blue:int64;

function fact(p:int64):int64;
 var q,j:int64;
begin q:=1;
 for j := p downto 1 do q:=q*j mod md; fact:=q
end;

begin
read(n,k);nk1:=n-k+1;
writeln(nk1);
for i := 2 to k do
 if i <= nk1 then
 begin
  blue:=(fact(k-1) div (fact(k-i)*fact(i-1))) mod md;
  red:=(fact(nk1) div (fact(nk1-i)*fact(i))) mod md;
  writeln(red*blue mod md)
 end
 else write(0);
end.


const NMAX = 1000000;
type ary = array[1..NMAX] of integer;
var a,b  :ary;
    i,j,k,n  :longint;
    flag:boolean;

procedure sort(p,q :integer; var a,b :ary);
  var i, j, k, w :integer;
begin i := p; j := q; k:=a[(p+q) div 2];
  repeat
    while a[i] < k do i := i+1;
    while k < a[j] do j := j-1;
    if i <= j then begin
      w := a[i]; a[i] := a[j]; a[j] := w;
      w := b[i]; b[i] := b[j]; b[j] := w;
      i := i+1;  j := j-1
    end
  until i > j;
  if p < j then sort(p,j,a,b);
  if i < q then sort(i,q,a,b)
end;

begin
  readln(n);
  flag:=false;
  for i:=1 to n do readln(a[i],b[i]);
  sort(1,n,b,a);
  for i:=1 to n-1 do a[i+1]:=a[i+1]+a[i];
  for i:=1 to n do if b[i]<a[i] then flag:=true;
  if flag then writeln('No') else writeln('Yes')
end.

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

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

1 \leq A_i, B_i \leq 10^9 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


var
	N,K,i,u,v:Longint;
begin
	read(N,K);
	if (N-1)*(N-2)<K*2 then begin
		writeln(-1);
		exit;
	end;
	K:=(N-1)*(N-2)div 2-K;
	writeln(K+N-1);
	for i:=2 to N do begin
		write('1 ');
		writeln(i);
	end;
	for u:=2 to N do begin
		for v:=u+1 to N do begin
			if K=0 then exit;
			writeln(u);
			writeln(v);
			dec(K);
		end;
	end;
end.

Score: 500 points

Problem Statement

Does there exist an undirected graph with N vertices satisfying the following conditions?

The graph is simple and connected.

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

Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M, the length of each edge is 1, and Edge i connects Vertex u_i and Vertex v_i.

There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2.

If there exists such a graph, construct an example.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq K \leq \frac{N(N - 1)}{2}

Input

Input is given from Standard Input in the following format:

N K

Output

If there does not exist an undirected graph with N vertices satisfying the conditions, print -1.

If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):

M
u_1 v_1
:
u_M v_M

If there exist multiple graphs satisfying the conditions, any of them will be accepted.

Sample Input 1

5 3

Sample Output 1

5
4 3
1 2
3 1
4 5
2 3

This graph has three pairs of vertices such that the shortest distance between them is 2: (1,\ 4), (2,\ 4), and (3,\ 5). Thus, the condition is satisfied.

Sample Input 2

5 8

Sample Output 2

-1

There is no graph satisfying the conditions.


var
	N,K,i,u,v:Longint;
begin
	read(N,K);
	if (N-1)*(N-2)<K*2 then begin
		writeln(-1);
		exit;
	end;
	K:=(N-1)*(N-2)div 2-K;
	writeln(K+N-1);
	for i:=2 to N do begin
		write('1 ');
		writeln(i);
	end;
	u:=2;
	v:=3;
	for i:=1 to K do begin
		write(u);
		write(' ');
		writeln(v);
		inc(v);
		if v>N then begin
			inc(u);
			v:=u+1;
		end;
	end;
end.


uses math;
var
  w,h,x,y:int64;
  r1,r2,r3,r4, r5, r6, r7 : int64;
BEGIN
  readln(w,h,x,y);
  r1 := x*h;
  r2 := (w-x)*h;
  r3 := y*w;
  r4 := (h-y)*w;
  r5 := min(r1,r2);
  r6 := min(r3,r4);
  r7 := max(r5,r6);
  write(r7, #32);
  if(r5=r6) then write(1) else write(0);
END.

Score : 300 points

Problem Statement

There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).
You are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.

Constraints

1 \leq W,H \leq 10^9

0\leq x\leq W

0\leq y\leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

W H x y

Output

Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.

The area printed will be judged correct when its absolute or relative error is at most 10^{-9}.

Sample Input 1

2 3 1 2

Sample Output 1

3.000000 0

The line x=1 gives the optimal cut, and no other line does.

Sample Input 2

2 2 1 1

Sample Output 2

2.000000 1


Var a,b,x,y:Array[1 .. 2500] of Longint;
    n,max,i,j,sum,count:Longint;
Begin
    Readln(n); max:=0; count:=0;
    For i:=1 to n do readln(x[i],y[i]);
    For i:=1 to n-1 do
        For j:=i+1 to n do begin
            Inc(count);
            a[count]:=abs(x[i]-x[j]);
            b[count]:=abs(y[i]-y[j]);
        End;
    For i:=1 to count do
        If (a[i]<>2000000001) and (b[i]<>2000000001) then begin
            sum:=1;
            For j:=i+1 to count do
                If (a[i]=a[j]) and (b[i]=b[j]) then begin
                    Inc(sum);
                    a[j]:=2000000001; b[j]:=2000000001;
                End;
            If sum>max then max:=sum;
        End;
    Writeln(n-max);
End.

Score : 300 points

Problem Statement

There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i).

We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation:

Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1.

Find the minimum total cost required to collect all the balls when we optimally choose p and q.

Constraints

1 \leq N \leq 50

|x_i|, |y_i| \leq 10^9

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum total cost required to collect all the balls.

Sample Input 1

2
1 1
2 2

Sample Output 1

1

If we choose p = 1, q = 1, we can collect all the balls at a cost of 1 by collecting them in the order (1, 1), (2, 2).

Sample Input 2

3
1 4
4 6
7 8

Sample Output 2

1

If we choose p = -3, q = -2, we can collect all the balls at a cost of 1 by collecting them in the order (7, 8), (4, 6), (1, 4).

Sample Input 3

4
1 1
1 2
2 1
2 2

Sample Output 3

2


Var a,b,c:Array[1 .. 100000] of Int64;
    n,rem1,rem2,min,max,rem,count:Int64;
    i:Longint;
Begin
    Readln(n); min:=10000; max:=-10000; count:=0;
    For i:=1 to n do begin
        Read(a[i]);
        If a[i]<min then begin
            min:=a[i]; rem1:=i;
        End
        Else if a[i]>max then begin
            max:=a[i]; rem2:=i;
        End;
    End;
    rem:=min;
    For i:=1 to n do
        If (i<>rem1) and (i<>rem2) then begin
            Inc(count);
            b[count]:=rem; c[count]:=a[i];
            rem:=rem-a[i];
        End;
    Writeln(max-rem);
    For i:=1 to count do writeln(b[count],' ',c[count]);
    Writeln(max,' ',rem);
End.

Score : 500 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on a blackboard.

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

2 \leq N \leq 10^5

-10^4 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0


Uses math;
var n, i, t : integer;
    w : array[1..100] of integer;
    s1, s2, ans : integer;
begin
  readln(n);
  for i := 1 to n do read(w[i]);
  ans := 10000;
  for t := 1 to n - 1 do begin
    s1 := 0;  s2 := 0;
    for i := 1 to t do s1 := s1 + w[i];
    for i := t + 1 to n do s2 := s2 + w[i];
    ans := Min(ans, Abs(s1 - s2));
  end;
  writeln(ans);
end.

Score : 200 points

Problem Statement

We have N weights indexed 1 to N. The mass of the weight indexed i is W_i.

We will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 \leq T < N. Let S_1 be the sum of the masses of the weights in the former group, and S_2 be the sum of the masses of the weights in the latter group.

Consider all possible such divisions and find the minimum possible absolute difference of S_1 and S_2.

Constraints

2 \leq N \leq 100

1 \leq W_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
W_1 W_2 ... W_{N-1} W_N

Output

Print the minimum possible absolute difference of S_1 and S_2.

Sample Input 1

3
1 2 3

Sample Output 1

0

If T = 2, S_1 = 1 + 2 = 3 and S_2 = 3, with the absolute difference of 0.

Sample Input 2

4
1 3 1 1

Sample Output 2

2

If T = 2, S_1 = 1 + 3 = 4 and S_2 = 1 + 1 = 2, with the absolute difference of 2. We cannot have a smaller absolute difference.

Sample Input 3

8
27 23 76 2 3 5 62 52

Sample Output 3

2


var
N,MM,i,j:Longint;
m:int64=1000000007;
a:Array[1..100000]of Longint;
dp:Array[0..100002]of int64;
begin
read(N,MM);
for i:=1 to MM do read(a[i]);
dp[0]:=1;
for i:=1 to N do begin
while(j<=MM)and(a[j]<i)do inc(j);
if a[j]=i then dp[i]:=0;
dp[i]:=dp[i]mod m;
inc(dp[i+1],dp[i]);
inc(dp[i+2],dp[i]);
end;
writeln(dp[N]);
end.

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.
He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?
Find the count modulo 1\ 000\ 000\ 007.

Constraints

1 \leq N \leq 10^5

0 \leq M \leq N-1

1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
.
.
.
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

0 \to 1 \to 2 \to 4 \to 5 \to 6

0 \to 1 \to 2 \to 4 \to 6

0 \to 2 \to 4 \to 5 \to 6

0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.

Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

Be sure to print the count modulo 1\ 000\ 000\ 007.


var a,b,N,M,i,n1:int64;
    G:array[0..100010] of integer;
const p=1000000007;
begin
 read(N,M);

  G[0]:=0;G[1]:=1;    //フィボナッチ数列を準備
 for i := 2 to N do
  G[i] :=( G[i-1] + G[i-2] ) mod p;

   n1:=1; read(a);
   n1:= n1*G[a] mod p;

  for i := 2 to M do
  begin
    b:=a; read(a);   //a=a_i、b=a_(i-1)
    n1:= n1*G[a-b-1] mod p;
  end;

  n1:=n1*G[N-a] mod p;

write(n1)
end.


var a,b,N,M,i,n1:int64;    //a=a_i、b=a_(i-1)
    G:array[0..100000] of integer;
const p=1000000007;
begin
 read(N,M);

  G[0]:=0;G[1]:=1;    //フィボナッチ数列を準備
 for i := 2 to N do
  G[i] :=( G[i-1] + G[i-2] ) mod p;

   n1:=1; read(a);
   n1:= n1*G[a] mod p;

  for i := 2 to M do
  begin
    b:=a;
    read(a);
    n1:= n1*G[a-b-1] mod p;
  end;

  n1:=n1*G[N-a] mod p;

write(n1)
end.


var a,b,N,M,i,n1:int64;    //a=a_i、b=a_(i-1)
    G:array[0..100000] of integer;
const p=1000000007;
begin
 read(N,M);

  G[0]:=0;G[1]:=1;    //フィボナッチ数列を準備
 for i := 2 to N do
  G[i] :=( G[i-1] + G[i-2] ) mod p;

 n1:=1; read(a);
    n1:= n1*G[a] mod p; b:=a;

 if M >= 2 then
 begin
  for i := 2 to M do
  begin
    read(a);
    n1:= n1*G[a-b-1] mod p;
    b:=a;
  end;
 end;

  n1:=n1*G[N-a] mod p;

write(n1)
end.


var s  :array[1..2010]of array[1..2010]of char;
    u,d,l,r,t  :array[1..2010]of array[1..2010]of integer;
    i,j,k,n,h,w,ans  :integer;

begin
  readln(h,w);
  for i:=1 to h do begin
    for j:=1 to w do
      read(s[i][j]);
      readln;
  end;
  {up}
  for j:=1 to w do begin
    if s[1][j]='.' then u[1][j]:=1 else u[1][j]:=0;
    for i:=2 to h do begin
      if s[i][j]='.' then u[i][j]:=u[i-1][j]+1 else u[i][j]:=0;
    end;
  end;
  {down}
  for j:=1 to w do begin
    if s[h][j]='.' then d[h][j]:=1 else d[h][j]:=0;
    for i:=h-1 downto 1 do begin
      if s[i][j]='.' then d[i][j]:=d[i+1][j]+1 else d[i][j]:=0;
    end;
  end;
  {left}
  for i:=1 to h do begin
    if s[i][1]='.' then l[i][1]:=1 else l[i][1]:=0;
    for j:=2 to w do begin
      if s[i][j]='.' then l[i][j]:=l[i][j-1]+1 else l[i][j]:=0;
    end;
  end;
  {right}
  for i:=1 to h do begin
    if s[i][w]='.' then r[i][w]:=1 else r[i][w]:=0;
    for j:=w-1 downto 1 do begin
      if s[i][j]='.' then r[i][j]:=r[i][j+1]+1 else r[i][j]:=0;
    end;
  end;

  ans:=0;
  for i:=1 to h do
    for j:=1 to w do begin
      if ans<(u[i][j]+d[i][j]+l[i][j]+r[i][j]-3)
        then ans:=u[i][j]+d[i][j]+l[i][j]+r[i][j]-3;
    end;
  writeln(ans);
end.

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13


Uses math;
var h, w, i, j, k, cnt, start, ans : int64;
    s : array[1..2001] of array[1..2001] of char;
    tate, yoko : array[1..2001] of array[1..2001] of int64;
    temp : char;
begin
  readln(h, w);
  for i := 1 to h do begin
    for j := 1 to w do read(s[i][j]);
    read(temp);
    s[i][w+1] := '#';
  end;
  for i := 1 to w+1 do s[h+1][i] := '#';

  for i := 1 to h do begin
    for j := 1 to w do begin
      tate[i][j] := 0; yoko[i][j] := 0;
    end;
  end;

  for i := 1 to h do begin
    cnt := 0;
    start := 1;

    for j := 1 to w+1 do begin
      if s[i][j] = '.' then inc(cnt) else begin
        for k := start to j-1 do yoko[i][k] := cnt;
        start := j+1;
        cnt := 0;
      end;
    end;
  end;

  for j := 1 to w do begin
    cnt := 0;
    start := 1;

    for i := 1 to h+1 do begin
      if s[i][j] = '.' then inc(cnt) else begin
        for k := start to i-1 do tate[k][j] := cnt;
        start := i+1;
        cnt := 0;
      end;
    end;
  end;

  ans := 0;
  for i := 1 to h do begin
    for j := 1 to w do begin
      if s[i][j] = '#' then Continue;
      ans := Max(ans, tate[i][j] + yoko[i][j] - 1);
    end;
  end;
  writeln(ans);
end.


Uses math;
var p:char;
    A,B,C,D,N,i,x,y:integer;
    S:array[0..200010] of integer;
begin
read(N,A,B,C,D);

for i := 1 to N do
 begin
  read(p); if ord(p) < 40 then S[i]:=1 else S[i]:=0;
 end;

x:=1;
for i := 1 to N-1 do
 begin
  if (S[i]=1) and (S[i+1]=1) then x:=0;
 end;

if x=0 then write('No')
 else
  begin
   if C > D then
    begin
     x:=0;
     for i := B to N-2 do
      begin
      if (S[i]=0) and (S[i+1]=0) and (S[i+2]=0) then x:=1;
      end;
    end;
   if C < D then x:=1;

    if x=0 then write('No') else write('Yse');
  end;
end.

Score : 400 points

Problem Statement

There are N squares arranged in a row, numbered 1, 2, ..., N from left to right.
You are given a string S of length N consisting of . and #. If the i-th character of S is #, Square i contains a rock; if the i-th character of S is ., Square i is empty.

In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.

You can repeat the following operation any number of times:

Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.

You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.

Determine whether this is possible.

Constraints

4 \leq N \leq 200\ 000

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

1 \leq A, B, C, D \leq N

Square A, B, C and D do not contain a rock.

A, B, C and D are all different.

A < B

A < C

B < D

Input

Input is given from Standard Input in the following format:

N A B C D
S

Output

Print Yes if the objective is achievable, and No if it is not.

Sample Input 1

7 1 3 6 7
.#..#..

Sample Output 1

Yes

The objective is achievable by, for example, moving the two persons as follows. (A and B represent Snuke and Fnuke, respectively.)

A#B.#..

A#.B#..

.#AB#..

.#A.#B.

.#.A#B.

.#.A#.B

.#..#AB

Sample Input 2

7 1 3 7 6
.#..#..

Sample Output 2

No

Sample Input 3

15 1 3 15 13
...#.#...#.#...

Sample Output 3

Yes


var
	N,i,j,tmp:Longint;
	S:array[1..100]of String[10];
	P,id:array[1..100]of Longint;
	t:String;
begin
	readln(N);
	for i:=1 to N do begin
		id[i]:=i;
		readln(t);
		j:=1;
		while t[j]<>' ' do begin
			S[i]:=S[i]+t[j];
			inc(j);
		end;
		inc(j);
		for j:=j to length(t)do begin
			P[i]:=P[i]*10+ord(t[j])-48;
		end;
		for j:=i-1 downto 1 do begin
			if(S[id[j]]>S[id[j+1]])or((S[id[j]]=S[id[j+1]])and(P[id[j]]<P[id[j+1]]))then begin
				tmp:=id[j];
				id[j]:=id[j+1];
				id[j+1]:=tmp;
			end;
		end;
	end;
	for i:=1 to N do writeln(id[i]);
end.

Score : 200 points

Problem Statement

You have decided to write a book introducing good restaurants.
There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i.
No two restaurants have the same score.

You want to introduce the restaurants in the following order:

The restaurants are arranged in lexicographical order of the names of their cities.

If there are multiple restaurants in the same city, they are arranged in descending order of score.

Print the identification numbers of the restaurants in the order they are introduced in the book.

Constraints

1 ≤ N ≤ 100

S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

0 ≤ P_i ≤ 100

P_i is an integer.

P_i ≠ P_j (1 ≤ i < j ≤ N)

Input

Input is given from Standard Input in the following format:

N
S_1 P_1
:
S_N P_N

Output

Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book.

Sample Input 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

Sample Output 1

3
4
6
1
5
2

The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.

Sample Input 2

10
yakutsk 10
yakutsk 20
yakutsk 30
yakutsk 40
yakutsk 50
yakutsk 60
yakutsk 70
yakutsk 80
yakutsk 90
yakutsk 100

Sample Output 2

10
9
8
7
6
5
4
3
2
1


var
	now,cnt,N,M,i,j,k,s:Longint;
	A:array[1..10,1..10]of Boolean;
	P:array[1..10]of Longint;
	flag:Boolean;
begin
	read(N,M);
	for i:=1 to M do begin
		read(k);
		for j:=1 to k do begin
			read(s);
			A[i,s]:=True;
		end;
	end;
	for i:=1 to M do begin
		read(P[i]);
	end;
	for i:=0 to (1<<N)-1 do begin
		flag:=True;
		for j:=1 to M do begin
			now:=0;
			for k:=1 to N do begin
				if((i>>(k-1))mod 2=1)and A[j,k]then inc(now);
			end;
			if now mod 2<>P[j]mod 2 then begin
				flag:=False;
				break;
			end;
		end;
		if flag then inc(cnt);
	end;
	writeln(cnt);
end.

Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

1 \leq N, M \leq 10

1 \leq k_i \leq N

1 \leq s_{ij} \leq N

s_{ia} \neq s_{ib} (a \neq b)

p_i is 0 or 1.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.

Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

Sample Input 2

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

Sample Output 2

0

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.

Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8


var
	r1,r2,ans,id,N,M,i,j,k,s:Longint;
	A:array[1..10,1..10]of Boolean;
	B:array[1..10,1..11]of Boolean;
	tmp:Boolean;
begin
	read(N,M);
	for i:=1 to M do begin
		read(k);
		for j:=1 to k do begin
			read(s);
			A[i,s]:=True;
			B[i,s]:=True;
		end;
	end;
	for i:=1 to M do begin
		read(s);
		if s=1 then B[i,N+1]:=True;
	end;
	for i:=1 to M do begin
		if i>N then break;
		id:=i;
		while(id<=M)and(not A[i,id])do inc(id);
		if id>N then continue;
		for j:=i to N do begin
			tmp:=A[id,j];
			A[id,j]:=A[i,j];
			A[i,j]:=tmp;
		end;
		for j:=i+1 to M do begin
			if A[j,i]then begin
				for k:=i to N do begin
					A[j,k]:=A[j,k]xor A[i,k];
				end;
			end;
		end;
	end;
	r1:=0;
	while True do begin
		tmp:=False;
		for i:=1 to N do if A[r1+1,i]then begin
			inc(r1);
			tmp:=True;
			break;
		end;
		if(not tmp)or(r1=M)then break;
	end;
	for i:=1 to M do begin
		if i>N+1 then break;
		id:=i;
		while(id<=M)and(not B[i,id])do inc(id);
		if id>N+1 then continue;
		for j:=i to N+1 do begin
			tmp:=B[id,j];
			B[id,j]:=B[i,j];
			B[i,j]:=tmp;
		end;
		for j:=i+1 to M do begin
			if B[j,i]then begin
				for k:=i to N+1 do begin
					B[j,k]:=B[j,k]xor B[i,k];
				end;
			end;
		end;
	end;
	r2:=0;
	while True do begin
		tmp:=False;
		for i:=1 to N+1 do if B[r2+1,i]then begin
			inc(r2);
			tmp:=True;
			break;
		end;
		if(not tmp)or(r2=M)then break;
	end;
	ans:=1;
	if r1<>r2 then ans:=0;
	for i:=1 to N-r1 do ans:=ans*2;
	writeln(ans);
end.


var
	N,K,i,L,R,Lsum,Rsum,now,ans,tmp,lim:Longint;
	V:array[1..50]of Longint;
	Lsorted:array[1..50]of Longint;
	LRsorted:array[1..50]of Longint;
begin
	read(N,K);
	for i:=1 to N do read(V[i]);
	for L:=0 to N do begin
		for i:=1 to N do LRsorted[i]:=0;
		if L>0 then begin
			Lsorted[L]:=V[L];
			inc(Lsum,V[L]);
			for i:=L-1 downto 1 do begin
				if Lsorted[i]>Lsorted[i+1]then begin
					tmp:=Lsorted[i];
					Lsorted[i]:=Lsorted[i+1];
					Lsorted[i+1]:=tmp;
				end else break;
			end;
		end;
		for i:=1 to L do LRsorted[i]:=Lsorted[i];
		Rsum:=Lsum;
		for R:=N+1 downto L+1 do begin
			lim:=K-L-(N-R+1);
			if lim<0 then break;
			if R<=N then begin
				LRsorted[L+(N-R)+1]:=V[R];
				inc(Rsum,V[R]);
				for i:=L+(N-R)downto 1 do begin
					if LRsorted[i]>LRsorted[i+1]then begin
						tmp:=LRsorted[i];
						LRsorted[i]:=LRsorted[i+1];
						LRsorted[i+1]:=tmp;
					end else break;
				end;
			end;
			now:=Rsum;
			for i:=1 to lim do begin
				if LRsorted[i]<0 then dec(now,LRsorted[i])
				else break;
			end;
			if ans<now then ans:=now;
		end;
	end;
	writeln(ans);
end.

Score : 400 points

Problem Statement

Your friend gave you a dequeue D as a birthday present.

D is a horizontal cylinder that contains a row of N jewels.

The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.

In the beginning, you have no jewel in your hands.

You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):

Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.

Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.

Find the maximum possible sum of the values of jewels in your hands after the operations.

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 100

-10^7 \leq V_i \leq 10^7

Input

Input is given from Standard Input in the following format:

N K
V_1 V_2 ... V_N

Output

Print the maximum possible sum of the values of jewels in your hands after the operations.

Sample Input 1

6 4
-10 8 2 1 2 6

Sample Output 1

14

After the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14, which is the maximum result.

Do operation A. You take out the jewel of value -10 from the left end of D.

Do operation B. You take out the jewel of value 6 from the right end of D.

Do operation A. You take out the jewel of value 8 from the left end of D.

Do operation D. You insert the jewel of value -10 to the right end of D.

Sample Input 2

6 4
-6 -100 50 -2 -5 -3

Sample Output 2

44

Sample Input 3

6 3
-6 -100 50 -2 -5 -3

Sample Output 3

0

It is optimal to do no operation.


var a,b:integer;
begin
  readln(a,b);
  if a<6 then writeln(0)
  else if a<13 then writeln(b div 2)
  else writeln(b)
end.

Score : 100 points

Problem Statement

Takahashi, who is A years old, is riding a Ferris wheel.

It costs B yen (B is an even number) to ride the Ferris wheel if you are 13 years old or older, but children between 6 and 12 years old (inclusive) can ride it for half the cost, and children who are 5 years old or younger are free of charge. (Yen is the currency of Japan.)

Find the cost of the Ferris wheel for Takahashi.

Constraints

0 ≤ A ≤ 100

2 ≤ B ≤ 1000

B is an even number.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the cost of the Ferris wheel for Takahashi.

Sample Input 1

30 100

Sample Output 1

100

Takahashi is 30 years old now, and the cost of the Ferris wheel is 100 yen.

Sample Input 2

12 100

Sample Output 2

50

Takahashi is 12 years old, and the cost of the Ferris wheel is the half of 100 yen, that is, 50 yen.

Sample Input 3

0 100

Sample Output 3

0

Takahashi is 0 years old, and he can ride the Ferris wheel for free.


var N,M,x,y,z,w,i:integer;
begin
 read(N,M);
 x:=0;y:=100000;
for i := 1 to M do
begin
 read(z,w);
 if x <= z then x:= z;
 if w <= y then y:= w;
 end;
 if y >= x then write(y-x+1);
end.

Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000


const C=1000000;
var N,M,i,j,x,y:integer;
G:array [1..C,1..2] of integer;
 begin
   read(N,M);
   y:=0;
 for i := 1 to M do begin
    read(G[i,1],G[i,2]) end;
 for i := G[1,1] to G[1,2] do
  begin x:=1;
 for j := 2 to M do
  begin
   if (G[j,1]<=i)and(i<=G[j,2]) then x:= x+1 end;
 if x >= M then y:=y+1 end;
 writeln(y);
end.


const C=1000000;
var N,M,i,j,x,y:integer;
G:array [1..C,1..2] of integer;
 begin
   read(N,M);
   y:=0;
 for i := 1 to M do begin
    read(G[i,1],G[i,2]) end;
 for i := 1 to N do
  begin x:=0;
 for j := 1 to M do
  begin
   if (G[j,1]<=i)and(i<=G[j,2]) then x:= x+1 end;
 if x >= M then y:=y+1 end;
 writeln(y);
end.


type
  pair = record
    b : int64;
    c : integer;
  end;
	pairArray = array[1..200000] of pair;

var cur,i,n,m:longint;
    sum:int64;
    bc : pairArray;

procedure	swap(var a,b:pair);
var tmp:pair;
begin
	tmp:=a;
	a:=b;
	b:=tmp
end;

procedure QuickSort(var a:pairArray;start_index,end_index:integer);
var i,j,x:int64;
begin
	x:=a[(start_index+end_index)div 2].c;
	i:=start_index;
	j:=end_index;
	while(true)do
	begin
		while(a[i].c<x)do i:=i+1;
		while(x<a[j].c)do j:=j-1;
		if(i<j)then
		begin
			swap(a[i],a[j]);
			i:=i+1;
			j:=j-1;
		end else
		begin
			if(start_index<i-1)then QuickSort(a,start_index,i-1);
			if(j+1<end_index)then QuickSort(a,j+1,end_index);
			exit;
		end;
	end;
end;

begin
  readln(n,m);
  for i:=1 to n do begin
    read(bc[i].c);
    bc[i].b := 1
  end;
  for i:=n+1 to n+m do read(bc[i].b,bc[i].c);
  QuickSort(bc,1,n+m);
  cur := n+m;
  sum := 0;
  for i:=1 to n do begin
      sum := sum + bc[cur].c;
      bc[cur].b := bc[cur].b - 1;
      if bc[cur].b=0 then cur := cur - 1
    end;
  writeln(sum)
end.

Score : 400 points

Problem Statement

You have N cards. On the i-th card, an integer A_i is written.

For each j = 1, 2, ..., M in this order, you will perform the following operation once:

Operation: Choose at most B_j cards (possibly zero). Replace the integer written on each chosen card with C_j.

Find the maximum possible sum of the integers written on the N cards after the M operations.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i, C_i \leq 10^9

1 \leq B_i \leq N

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N
B_1 C_1
B_2 C_2
\vdots
B_M C_M

Output

Print the maximum possible sum of the integers written on the N cards after the M operations.

Sample Input 1

3 2
5 1 4
2 3
1 5

Sample Output 1

14

By replacing the integer on the second card with 5, the sum of the integers written on the three cards becomes 5 + 5 + 4 = 14, which is the maximum result.

Sample Input 2

10 3
1 8 5 7 100 4 52 33 13 5
3 10
4 30
1 4

Sample Output 2

338

Sample Input 3

3 2
100 100 100
3 99
3 99

Sample Output 3

300

Sample Input 4

11 3
1 1 1 1 1 1 1 1 1 1 1
3 1000000000
4 1000000000
3 1000000000

Sample Output 4

10000000001

The output may not fit into a 32-bit integer type.


type
	intArray = array[1..100000] of int64;
  pair = record
    b : int64;
    c : integer;
  end;
	pairArray = array[1..100000] of pair;

var cur,pos,i,n,m:longint;
    sum:int64;
    a:intArray;
    bc : pairArray;

procedure	swap(var a,b:int64);
var tmp:int64;
begin
	tmp:=a;
	a:=b;
	b:=tmp
end;

procedure	swapP(var a,b:pair);
var tmp:pair;
begin
	tmp:=a;
	a:=b;
	b:=tmp
end;

procedure QuickSort(var a:intArray;start_index,end_index:integer);
var i,j,x:int64;
begin
	x:=a[(start_index+end_index)div 2];
	i:=start_index;
	j:=end_index;
	while(true)do
	begin
		while(a[i]<x)do i:=i+1;
		while(x<a[j])do j:=j-1;
		if(i<j)then
		begin
			swap(a[i],a[j]);
			i:=i+1;
			j:=j-1;
		end else
		begin
			if(start_index<i-1)then QuickSort(a,start_index,i-1);
			if(j+1<end_index)then QuickSort(a,j+1,end_index);
			exit;
		end;
	end;
end;

procedure QuickSortP(var a:pairArray;start_index,end_index:integer);
var i,j,x:int64;
begin
	x:=a[(start_index+end_index)div 2].c;
	i:=start_index;
	j:=end_index;
	while(true)do
	begin
		while(a[i].c<x)do i:=i+1;
		while(x<a[j].c)do j:=j-1;
		if(i<j)then
		begin
			swapP(a[i],a[j]);
			i:=i+1;
			j:=j-1;
		end else
		begin
			if(start_index<i-1)then QuickSortP(a,start_index,i-1);
			if(j+1<end_index)then QuickSortP(a,j+1,end_index);
			exit;
		end;
	end;
end;

begin
  readln(n,m);
  for i:=1 to n do read(a[i]);
  QuickSort(a,1,n);
  for i:=1 to m do read(bc[i].b,bc[i].c);
  QuickSortP(bc,1,m);
  cur := m;
  pos := n;
  sum := 0;
  for i:=1 to n do
  if bc[cur].c>a[pos] then begin
      sum := sum + bc[cur].c;
      bc[cur].b := bc[cur].b - 1;
      if bc[cur].b=0 then cur := cur - 1
    end else begin
      sum := sum + a[pos];
      pos := pos - 1;
    end;
  writeln(sum)
end.


var N,K:integer;
    S:string;
begin
read(N,K,S);
if S[K]='A' then S[K]:='a';
if S[K]='B' then S[K]:='b';
if S[K]='C' then S[K]:='c';
write(S)
end.

Score : 100 points

Problem Statement

You are given a string S of length N consisting of A, B and C, and an integer K which is between 1 and N (inclusive).
Print the string S after lowercasing the K-th character in it.

Constraints

1 ≤ N ≤ 50

1 ≤ K ≤ N

S is a string of length N consisting of A, B and C.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the string S after lowercasing the K-th character in it.

Sample Input 1

3 1
ABC

Sample Output 1

aBC

Sample Input 2

4 3
CABA

Sample Output 2

CAbA


var str:array[1..52]of char;
    n,k,i:integer;
begin
  readln(n,k);
  for i:=1 to n do read(str[i]);
  if str[k]='A' then str[k]:='a';
  if str[k]='B' then str[k]:='b';
  if str[k]='C' then str[k]:='c';
  for i:=1 to n do writeln(str[i]);
end.


uses crt;
var n,a,b:longint;
begin
        readln(n);
        a:=n div 100;
        b:=n mod 100;
        if(a<12)and(a<>0)and(b<>0)then write('AMBIGUOUS')else
        if(a=12)and(b=12)then write('AMBIGUOUS')else
        if(a>12)and(b>12)then write('NA')else
        if(a>12)and(b=0)then write('NA') else
        if(a=0)and(b=0)then write('NA')else
        if(a=0)and(b<>0)then write('NA')else
        if (a<12)and(a>1)and(b>12)then write('MMYY') else
        if (a=12)and(b>12)then write('MMYY') else
        if (a=1)and(b>12)then write('MMYY') else
        if (a=12)and(b<=12)then write('MMYY') else
        if(a<>0)and(b<=1) then write('MMYY')else
        if (a=1)and(b>=1)then write('MMYY') else
        if (a>12)and(b<>0)and(b<=12)then write('YYMM');
        readln
end.

Score : 200 points

Problem Statement

You have a digit sequence S of length 4. You are wondering which of the following formats S is in:

YYMM format: the last two digits of the year and the two-digit representation of the month (example: 01 for January), concatenated in this order

MMYY format: the two-digit representation of the month and the last two digits of the year, concatenated in this order

If S is valid in only YYMM format, print YYMM; if S is valid in only MMYY format, print MMYY; if S is valid in both formats, print AMBIGUOUS; if S is valid in neither format, print NA.

Constraints

S is a digit sequence of length 4.

Input

Input is given from Standard Input in the following format:

S

Output

Print the specified string: YYMM, MMYY, AMBIGUOUS or NA.

Sample Input 1

1905

Sample Output 1

YYMM

May XX19 is a valid date, but 19 is not valid as a month. Thus, this string is only valid in YYMM format.

Sample Input 2

0112

Sample Output 2

AMBIGUOUS

Both December XX01 and January XX12 are valid dates. Thus, this string is valid in both formats.

Sample Input 3

1700

Sample Output 3

NA

Neither 0 nor 17 is valid as a month. Thus, this string is valid in neither format.


var
	N,K,i,p,q,x,tmp,get:Longint;
	ans,now,make:double;
begin
	read(N,K);
	if N>=K then begin
		ans:=(N-K+1)/N;
		q:=K-1;
	end
	else q:=N;
	make:=1;
	get:=1;
	for i:=1 to 2 do begin
		if q=0 then break;
		get:=get*2;
		make:=make*0.5;
		p:=K div get;
		if p<q then begin;
			ans:=ans+(q-p)/N*make;
			q:=p;
		end;
	end;
	for i:=q downto 1 do begin
		tmp:=(K+i-1)div i;
		x:=1;
		now:=1/N;
		while tmp>x do begin
			now:=now*0.5;
			x:=x*2;
		end;
		ans:=ans+now;
	end;
	writeln(ans:0:9);
end.

Score : 300 points

Problem Statement

Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:

Throw the die. The current score is the result of the die.

As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.

The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.

You are given N and K. Find the probability that Snuke wins the game.

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.

Sample Input 1

3 10

Sample Output 1

0.145833333333

If the die shows 1, Snuke needs to get four consecutive heads from four coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^4 = \frac{1}{48}.

If the die shows 2, Snuke needs to get three consecutive heads from three coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^3 = \frac{1}{24}.

If the die shows 3, Snuke needs to get two consecutive heads from two coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^2 = \frac{1}{12}.

Thus, the probability that Snuke wins is \frac{1}{48} + \frac{1}{24} + \frac{1}{12} = \frac{7}{48} \simeq 0.1458333333.

Sample Input 2

100000 5

Sample Output 2

0.999973749998


var
	N,K,i,p,q,tmp,get:Longint;
	ans,now,make:double;
begin
	read(N,K);
	if N>=K then begin
		ans:=(N-K+1)/N;
		q:=K-1;
	end else q:=N;
	make:=1;
	get:=1;
	for i:=1 to 0 do begin
		if q=0 then break;
		get:=get*2;
		make:=make*0.5;
		p:=K div get;
		if p<q then begin
			ans:=ans+(q-p)/N*make;
			q:=p;
		end;
	end;
	for i:=q downto 1 do begin
		tmp:=(K+i-1)div i;
		now:=1/N;
		while tmp>1 do begin
			now:=now*0.5;
			tmp:=tmp div 2;
		end;
		ans:=ans+now;
	end;
	writeln(ans:0:9);
end.


var s,r,g,b,n,t:Longint;
begin
read(r,g,b,n);
t:=n;
while True do begin
if t<0 then begin
dec(n,b);
t:=n;
if t<0 then begin
writeln(s);
exit;
end;
end;
if t mod r=0 then inc(s);
dec(t,g);
end;
end.

Score : 200 points

Problem Statement

Snuke has come to a store that sells boxes containing balls. The store sells the following three kinds of boxes:

Red boxes, each containing R red balls

Green boxes, each containing G green balls

Blue boxes, each containing B blue balls

Snuke wants to get a total of exactly N balls by buying r red boxes, g green boxes and b blue boxes.
How many triples of non-negative integers (r,g,b) achieve this?

Constraints

All values in input are integers.

1 \leq R,G,B,N \leq 3000

Input

Input is given from Standard Input in the following format:

R G B N

Output

Print the answer.

Sample Input 1

1 2 3 4

Sample Output 1

4

Four triples achieve the objective, as follows:

(4,0,0)

(2,1,0)

(1,0,1)

(0,2,0)

Sample Input 2

13 1 4 3000

Sample Output 2

87058


var ir,ig,r,g,b,n,ans,temp: longint;

begin
  read(r,g,b,n);
  ans := 0;
  for ir := 0 to 3000 do begin
    for ig := 0 to 3000 do begin
      temp:=n - ir * r - ig * g;
      if temp<0 then break;
      if temp mod b = 0 then ans := ans + 1;
    end;
  end;
  writeln(ans)
end.


var t,i,n,ans : longint;
    b : array[1..3] of longint;
    p,c : char;

function min(a,b:int64):int64;
begin
  if a < b then min := a else min := b
end;

begin
  read(n);
  ans := 0;
  for t := 1 to 3 do b[t] := 0;
  for i := 0 to n do begin
    read(p);
    if p = 'B' then t := 1 else t := 0;
    while not eoln do begin
      read(c);
      if (p = 'A') and (c = 'B') then ans := ans + 1;
      p := c
    end;
    readln;
    if c = 'A' then t := t + 2;
    if t > 0 then b[t] := b[t] + 1
  end;
  if (b[1] = 0) and (b[2] = 0) and (b[3] > 0) then ans := ans + b[3] - 1
    else ans := ans + min(b[1],b[2]) + b[3];
  writeln(ans)
end.

Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4


var t,i,n,ans : integer;
    b : array[1..3] of integer;
    p,c : char;

function min(a,b:int64):int64;
begin
  if a < b then min := a else min := b
end;

begin
  read(n);
  ans := 0;
  for t := 1 to 3 do b[t] := 0;
  for i := 0 to n do begin
    read(p);
    if p = 'B' then t := 1 else t := 0;
    while not eoln do begin
      read(c);
      if (p = 'A') and (c = 'B') then ans := ans + 1;
      p := c
    end;
    readln;
    if c = 'A' then t := t + 2;
    if t > 0 then b[t] := b[t] + 1
  end;
  if (b[1] = 0) and (b[2] = 0) then ans := ans + b[3] - 1
    else ans := ans + min(b[1],b[2]) + b[3];
  writeln(ans)
end.


var i : longint;
    n,ans:int64;

begin
  read(n);
  ans := 0;
  for i := 1 to 999999 do begin
    if (i+1)*(i+1)>n then break;
    if n mod i = 0 then
      if n div i - 1 > i then ans := ans + n div i - 1
  end;
  writeln(ans);
end.

Score : 500 points

Problem Statement

Snuke received a positive integer N from Takahashi.
A positive integer m is called a favorite number when the following condition is satisfied:

The quotient and remainder of N divided by m are equal, that is, \lfloor \frac{N}{m} \rfloor = N \bmod m holds.

Find all favorite numbers and print the sum of those.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

8

Sample Output 1

10

There are two favorite numbers: 3 and 7. Print the sum of these, 10.

Sample Input 2

1000000000000

Sample Output 2

2499686339916

Watch out for overflow.


var a:array[1..100010]of integer;
    n,i,j,m,mabs:integer;
    ans:int64;
    zero:boolean;
begin
  readln(n);
  m:=0;
  zero:=false;
  mabs:=100000010;
  for i:=1 to n do begin
    read(a[i]);
    if mabs>abs(a[i]) then
      mabs:=abs(a[i]);
    if a[i]<0 then inc(m);
    if a[i]=0 then zero:=true
  end;
  ans:=0;
  for i:=1 to n do
    if a[i]>0 then inc(ans,a[i])
    else inc(ans,-a[i]);
  if (m mod 2<>0) and not zero then
    ans:=ans-2*mabs;
  write(ans)
end.

Score : 400 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, arranged in a row in this order.

You can perform the following operation on this integer sequence any number of times:

Operation: Choose an integer i satisfying 1 \leq i \leq N-1. Multiply both A_i and A_{i+1} by -1.

Let B_1, B_2, ..., B_N be the integer sequence after your operations.

Find the maximum possible value of B_1 + B_2 + ... + B_N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

-10^9 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value of B_1 + B_2 + ... + B_N.

Sample Input 1

3
-10 5 -4

Sample Output 1

19

If we perform the operation as follows:

Choose 1 as i, which changes the sequence to 10, -5, -4.

Choose 2 as i, which changes the sequence to 10, 5, 4.

we have B_1 = 10, B_2 = 5, B_3 = 4. The sum here, B_1 + B_2 + B_3 = 10 + 5 + 4 = 19, is the maximum possible result.

Sample Input 2

5
10 -4 -8 -11 3

Sample Output 2

30

Sample Input 3

11
-1000000000 1000000000 -1000000000 1000000000 -1000000000 0 1000000000 -1000000000 1000000000 -1000000000 1000000000

Sample Output 3

10000000000

The output may not fit into a 32-bit integer type.


var a:array[1..100010]of integer;
    n,i,j,m,maxm:integer;
    ans:int64;
    zero:boolean;
begin
  readln(n);
  m:=0;
  zero:=false;
  maxm:=-100000010;
  for i:=1 to n do begin
    read(a[i]);
    if a[i]<0 then begin
      inc(m);
      if maxm<a[i] then maxm:=a[i]
    end;
    if a[i]=0 then zero:=true
  end;
  ans:=0;
  for i:=1 to n do inc(ans,a[i]);
  if (m mod 2<>0) and not zero then
    ans:=ans-maxm;
  write(ans)
end.


var a,b,c:integer;

begin
  read(a,b,c);
  if (a-c)*(b-c)<0 Then writeln('Yes') else writeln('No')
end.

Score : 100 points

Problem Statement

There are three houses on a number line: House 1, 2 and 3, with coordinates A, B and C, respectively.
Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Constraints

0\leq A,B,C\leq 100

A, B and C are distinct integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Sample Input 1

3 8 5

Sample Output 1

Yes

We pass the coordinate 5 on the straight way from the house at coordinate 3 to the house at coordinate 8.

Sample Input 2

7 3 1

Sample Output 2

No

Sample Input 3

10 2 4

Sample Output 3

Yes

Sample Input 4

31 41 59

Sample Output 4

No


var s:array[1..11] of char;
    n,k,i:integer;
begin
  read(n);
  for i:=1 to n do read(s[i]);
  read(k);
  for i:=1 to n do
  if s[i]=s[k] then write(s[k])
  else write('*');
end.

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, and an integer K.
Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Constraints

1 \leq K \leq N\leq 10

S is a string of length N consisting of lowercase English letters.

N and K are integers.

Input

Input is given from Standard Input in the following format:

N
S
K

Output

Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Sample Input 1

5
error
2

Sample Output 1

*rr*r

The second character of S is r. When we replace every character in error that differs from r with *, we get the string *rr*r.

Sample Input 2

6
eleven
5

Sample Output 2

e*e*e*

Sample Input 3

9
education
7

Sample Output 3

******i**


var a  :char;
    s,t  :array[0..200010]of integer;
    n,i,j,k,ans  :integer;

function min(a,b:integer):integer;
begin
  if a>b then min:=b
  else min:=a
end;

begin
  readln(n);
  s[0]:=0;
  for i:=1 to n do begin
    read(a);
    if a='#' then s[i]:=s[i-1]+1
    else s[i]:=s[i-1];
    end;
  ans:=210000;
  for i:=0 to n do t[i]:=s[i]+n+s[i]-i-s[n];
  for i:=0 to n do ans:=min(ans,t[i]);
  writeln(ans)
end.

Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

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

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

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0


var a  :char;
    s  :array[0..200010]of integer;
    n,i,j,k,ans  :integer;

function min(a,b:integer):integer;
begin
  if a>b then min:=b
  else min:=a
end;

begin
  readln(n);
  s[0]:=0;
  for i:=1 to n do begin
    read(a);
    if a='#' then s[i]:=s[i-1]+1
    else s[i]:=s[i-1];
  end;
  ans:=210000;
  j:=ans;
  for i:=1 to n do begin
    if s[i]=s[i-1] then begin
      j:=min(s[i],n+s[i]-i-s[n]);
      ans:=min(ans,j)
    end
  end;
  write(ans)
end.


type ary=array[1..100001]of char;
var a,b,i,j  :integer;
    c:char;
    l  :ary;

begin
  j:=1; a:=0; b:=0;
  for i:=1 to 100001 do
    if i mod 2 = 0 then l[i]:='0'
    else l[i]:='1';
  repeat
    read(c);
    if c=l[j] then inc(a)
    else inc(b);
    inc(j)
  until eoln ;
  if a<b then write(a)
  else write(b)
  end.

Score : 100 points

Problem Statement

There are two buttons, one of size A and one of size B.

When you press a button of size X, you get X coins and the size of that button decreases by 1.

You will press a button twice. Here, you can press the same button twice, or press both buttons once.

At most how many coins can you get?

Constraints

All values in input are integers.

3 \leq A, B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of coins you can get.

Sample Input 1

5 3

Sample Output 1

9

You can get 5 + 4 = 9 coins by pressing the button of size 5 twice, and this is the maximum result.

Sample Input 2

3 4

Sample Output 2

7

Sample Input 3

6 6

Sample Output 3

12


var
	S:String;
	ans,now,i,n:Longint;
begin
	readln(S);
	n:=length(S);
	for i:=1 to n do if i mod 2<>ord(S[i])-48 then inc(now);
	ans:=now;
	now:=0;
	for i:=1 to n do if(i+1)mod 2<>ord(S[i])-48 then inc(now);
	if ans>now then ans:=now;
	writeln(ans);
end.

Score : 300 points

Problem Statement

N tiles are arranged in a row from left to right. The initial color of each tile is represented by a string S of length N.

The i-th tile from the left is painted black if the i-th character of S is 0, and painted white if that character is 1.

You want to repaint some of the tiles black or white, so that any two adjacent tiles have different colors.

At least how many tiles need to be repainted to satisfy the condition?

Constraints

1 \leq |S| \leq 10^5

S_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of tiles that need to be repainted to satisfy the condition.

Sample Input 1

000

Sample Output 1

1

The condition can be satisfied by repainting the middle tile white.

Sample Input 2

10010010

Sample Output 2

3

Sample Input 3

0

Sample Output 3

0


Uses math;
var i,count1,count2:integer;
    S:string; //count1:010101.._count2:101010
begin
read(S);i:=1;
while ord(S[i]) > 40 do
 begin
  if i mod 2 = 1 then begin
   if S[i]='0' then inc(count1) else inc(count2) end
  else begin if S[i]='1' then inc(count1) else inc(count2) end;
  inc(i);
end;
write(min(count1,count2))
end.


var n,i,a,b:integer;
    c:char;
begin
  read(n);a:=0;b:=0;
  for i:=1 to n do
  begin
  read(c);
  if c='R' then inc(a)
  else inc(b);
  end;
  if a>b then write('Yes')
  else write('No')
end.

Score : 200 points

Problem Statement

There are N people numbered 1 to N. Each person wears a red hat or a blue hat.

You are given a string s representing the colors of the people. Person i wears a red hat if s_i is R, and a blue hat if s_i is B.

Determine if there are more people wearing a red hat than people wearing a blue hat.

Constraints

1 \leq N \leq 100

|s| = N

s_i is R or B.

Input

Input is given from Standard Input in the following format:

N
s

Output

If there are more people wearing a red hat than there are people wearing a blue hat, print Yes; otherwise, print No.

Sample Input 1

4
RRBR

Sample Output 1

Yes

There are three people wearing a red hat, and one person wearing a blue hat.

Since there are more people wearing a red hat than people wearing a blue hat, the answer is Yes.

Sample Input 2

4
BRBR

Sample Output 2

No

There are two people wearing a red hat, and two people wearing a blue hat.

Since there are as many people wearing a red hat as people wearing a blue hat, the answer is No.


var
	S:String;
	N,Q,l,r,i:Longint;
	sum:Array[1..100000]of Longint;
begin
	readln(N,Q);
	readln(S);
	for i:=2 to length(S)do begin
		sum[i]:=sum[i-1];
		if(S[i-1]='A')and(S[i]='C')then inc(sum[i]);
	end;
	for i:=1 to Q do begin
		read(l,r);
		writeln(sum[r]-sum[l]);
	end;
end.

Score : 300 points

Problem Statement

You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries:

Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring?

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

Each character in S is A, C, G or T.

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.

Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.

Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.


var n:integer;i:integer;j:integer;
begin
  read(n);
  if n mod 2=1 then begin
    writeln(Trunc(n*(n-1)/2-n/2+1));
    for i:=1 to n-1 do
      for j:=i+1 to n do
        if i+j<>N+1 then begin
	      writeln(i);
		  writeln(j);
	    end;
  end else begin
    writeln(n*(n-1)/2-n/2);
    for i:=1 to n-1 do
      for j:=i+1 to n do
        if i+j<>N then begin
	      writeln(i);
		  writeln(j);
	    end;
  end;
end.

Score : 700 points

Problem Statement

You are given an integer N.
Build an undirected graph with N vertices with indices 1 to N that satisfies the following two conditions:

The graph is simple and connected.

There exists an integer S such that, for every vertex, the sum of the indices of the vertices adjacent to that vertex is S.

It can be proved that at least one such graph exists under the constraints of this problem.

Constraints

All values in input are integers.

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

In the first line, print the number of edges, M, in the graph you made. In the i-th of the following M lines, print two integers a_i and b_i, representing the endpoints of the i-th edge.

The output will be judged correct if the graph satisfies the conditions.

Sample Input 1

3

Sample Output 1

2
1 3
2 3

For every vertex, the sum of the indices of the vertices adjacent to that vertex is 3.


var
n,i,j,cnt,t:Longint;
a,b:array[1..10000]of Byte;
begin
read(n);
t:=n+1-n mod 2;
writeln(n*(n-1)div 2-n div 2);
for i:=1 to n do begin
for j:=i+1 to t-i-1 do writeln(i,' ',j);
if(j=t-i-1)or(j=t-i)then j:=t-i+1;
for j:=j to n do writeln(i,' ',j);
end;
end.


var
  i,n:longint;
  a,ys,xg,f:array[0..200010] of longint;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  for i:=1 to n do
    begin
      if ys[a[i]]<>0 then xg[ys[a[i]]]:=i;
      ys[a[i]]:=i;
    end;
  f[1]:=1;
  for i:=1 to n-1 do
    begin
      f[i+1]:=f[i+1]+f[i];
      if xg[i]>i+1 then f[xg[i]]:=f[xg[i]]+f[i];
    end;
  writeln(f[n]);
end.

Score : 700 points

Problem Statement

There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i.

Snuke will perform the following operation zero or more times:

Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones.

Find the number of possible final sequences of colors of the stones, modulo 10^9+7.

Constraints

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

1 \leq C_i \leq 2\times 10^5(1\leq i\leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C_1
:
C_N

Output

Print the number of possible final sequences of colors of the stones, modulo 10^9+7.

Sample Input 1

5
1
2
1
2
2

Sample Output 1

3

We can make three sequences of colors of stones, as follows:

(1,2,1,2,2), by doing nothing.

(1,1,1,2,2), by choosing the first and third stones to perform the operation.

(1,2,2,2,2), by choosing the second and fourth stones to perform the operation.

Sample Input 2

6
4
2
5
4
2
4

Sample Output 2

5

Sample Input 3

7
1
3
1
2
3
3
2

Sample Output 3

5


var n,m,c,i,j,sum,a,ans :integer;
    b:array[1..30] of integer;
begin
  readln(n,m,c);
  ans:=0;
  for i:=1 to m do read(b[i]);
  readln;
  for i:=1 to n do begin
    sum:=c;
    for j:=1 to m do begin
      read(a);
      sum:=sum+a*b[j];
    end;
    readln;
    if sum>0 then ans:=ans+1
  end;
  write(ans);
  end.

Score : 100 points

Problem Statement

There are H rows and W columns of white square cells.

You will choose h of the rows and w of the columns, and paint all of the cells contained in those rows or columns.

How many white cells will remain?

It can be proved that this count does not depend on what rows and columns are chosen.

Constraints

All values in input are integers.

1 \leq H, W \leq 20

1 \leq h \leq H

1 \leq w \leq W

Input

Input is given from Standard Input in the following format:

H W
h w

Output

Print the number of white cells that will remain.

Sample Input 1

3 2
2 1

Sample Output 1

1

There are 3 rows and 2 columns of cells. When two rows and one column are chosen and painted in black, there is always one white cell that remains.

Sample Input 2

5 5
2 3

Sample Output 2

6

Sample Input 3

2 4
2 4

Sample Output 3

0


var n,m,c,i,j,sum,a,ans :integer;
    b:array[1..30] of integer;
begin
  readln(n,m,c);
  ans:=0;
  for i:=1 to m do read(b[i]);
  for i:=1 to n do begin
    sum:=c;
    for j:=1 to m do begin
      read(a);
      sum:=sum+a*b[i];
    end;
    readln;
    if sum>0 then ans:=ans+1
  end;
  write(ans);
  end.

Score : 200 points

Problem Statement

There are N pieces of source code. The characteristics of the i-th code is represented by M integers A_{i1}, A_{i2}, ..., A_{iM}.

Additionally, you are given integers B_1, B_2, ..., B_M and C.

The i-th code correctly solves this problem if and only if A_{i1} B_1 + A_{i2} B_2 + ... + A_{iM} B_M + C > 0.

Among the N codes, find the number of codes that correctly solve this problem.

Constraints

All values in input are integers.

1 \leq N, M \leq 20

-100 \leq A_{ij} \leq 100

-100 \leq B_i \leq 100

-100 \leq C \leq 100

Input

Input is given from Standard Input in the following format:

N M C
B_1 B_2 ... B_M
A_{11} A_{12} ... A_{1M}
A_{21} A_{22} ... A_{2M}
\vdots
A_{N1} A_{N2} ... A_{NM}

Output

Print the number of codes among the given N codes that correctly solve this problem.

Sample Input 1

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

Sample Output 1

1

Only the second code correctly solves this problem, as follows:

Since 3 \times 1 + 2 \times 2 + 1 \times 3 + (-10) = 0 \leq 0, the first code does not solve this problem.

1 \times 1 + 2 \times 2 + 2 \times 3 + (-10) = 1 > 0, the second code solves this problem.

Sample Input 2

5 2 -4
-2 5
100 41
100 40
-3 0
-6 -2
18 -13

Sample Output 2

2

Sample Input 3

3 3 0
100 -100 0
0 100 100
100 100 100
-100 100 100

Sample Output 3

0

All of them are Wrong Answer. Except yours.


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

Score : 300 points

Problem Statement

Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.

There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.

What is the minimum amount of money with which he can buy M cans of energy drinks?

It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^5

B_1 + ... + B_N \geq M

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.

Sample Input 1

2 5
4 9
2 4

Sample Output 1

12

With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.

Sample Input 2

4 30
6 18
2 5
3 10
7 9

Sample Output 2

130

Sample Input 3

1 100000
1000000000 100000

Sample Output 3

100000000000000

The output may not fit into a 32-bit integer type.


type itemp = ^item;
     item = record
        cont : char;
        next : itemp;
     end;
var s : itemp;
    n : integer;

procedure readitem(var p:itemp);{頭から追加}
var q : itemp;
begin
  q := p;
  new(p);
  read(p^.cont);
  p^.next := q
end;

procedure delitem(var p : itemp);{頭から消す}
begin
  p := p^.next
end;

begin
  n:= 0;
  s:=nil;
  while not eoln do begin
    readitem(s);
    if s^.next <> nil then
      if Ord(s^.cont)+Ord(s^.next^.cont) = 97 then begin
        n := n + 2;
        delitem(s); delitem(s)
      end
  end;
  writeln(n)
end.

Score : 300 points

Problem Statement

There are N cubes stacked vertically on a desk.

You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is 0, and blue if that character is 1.

You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent, and remove them. Here, the cubes that were stacked on the removed cubes will fall down onto the object below them.

At most how many cubes can be removed?

Constraints

1 \leq N \leq 10^5

|S| = N

Each character in S is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of cubes that can be removed.

Sample Input 1

0011

Sample Output 1

4

All four cubes can be removed, by performing the operation as follows:

Remove the second and third cubes from the bottom. Then, the fourth cube drops onto the first cube.

Remove the first and second cubes from the bottom.

Sample Input 2

11011010001011

Sample Output 2

12

Sample Input 3

0

Sample Output 3

0