var
	N,M,i,u,v,id:Longint;
	Gv:Array[1..2000]of Longint;
	Gp,Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);
	for i:=1 to N do Gi[i]:=0;
	for i:=1 to N do cnt[i]:=0;
	for i:=1 to M do Gp[i]:=0;
	for i:=1 to M do begin
		read(u,v);
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;Qs:=0;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;
	while (id<=Qs)and(Qs<N) do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.


var
	N,M,i,u,v,id:Longint;
	Gv:Array[1..2000]of Longint;
	Gp,Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);
	for i:=1 to N do Gi[i]:=0;
	for i:=1 to M do Gp[i]:=0;
	for i:=1 to M do begin
		read(u,v);
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;Qs:=0;
	while (id<=Qs)and(Qs<N) do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.

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

Score : 600 points

Problem Statement

Given is a directed graph G with N vertices and M edges.

The vertices are numbered 1 to N, and the i-th edge is directed from Vertex A_i to Vertex B_i.

It is guaranteed that the graph contains no self-loops or multiple edges.

Determine whether there exists an induced subgraph (see Notes) of G such that the in-degree and out-degree of every vertex are both 1. If the answer is yes, show one such subgraph.

Here the null graph is not considered as a subgraph.

Notes

For a directed graph G = (V, E), we call a directed graph G' = (V', E') satisfying the following conditions an induced subgraph of G:

V' is a (non-empty) subset of V.

E' is the set of all the edges in E that have both endpoints in V'.

Constraints

1 \leq N \leq 1000

0 \leq M \leq 2000

1 \leq A_i,B_i \leq N

A_i \neq B_i

All pairs (A_i, B_i) are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_M B_M

Output

If there is no induced subgraph of G that satisfies the condition, print -1.
Otherwise, print an induced subgraph of G that satisfies the condition, in the following format:

K
v_1
v_2
:
v_K

This represents the induced subgraph of G with K vertices whose vertex set is \{v_1, v_2, \ldots, v_K\}. (The order of v_1, v_2, \ldots, v_K does not matter.)
If there are multiple subgraphs of G that satisfy the condition, printing any of them is accepted.

Sample Input 1

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

Sample Output 1

3
1
2
4

The induced subgraph of G whose vertex set is \{1, 2, 4\} has the edge set \{(1, 2), (2, 4), (4, 1)\}. The in-degree and out-degree of every vertex in this graph are both 1.

Sample Input 2

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

Sample Output 2

-1

There is no induced subgraph of G that satisfies the condition.

Sample Input 3

6 9
1 2
2 3
3 4
4 5
5 6
5 1
5 2
6 1
6 2

Sample Output 3

4
2
3
4
5

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
	H,W,A,B,i,j:Longint;
	s:String;
begin
	read(H,W,A,B);
	for i:=1 to H do begin
		s:='';
		for j:=1 to W do begin
			if (i>B)<>(j>A)then s:=s+'0' else s:=s+'1';
		end;
		writeln(s);
	end;
end.

Score : 300 points

Problem Statement

We have a square grid with H rows and W columns.
Snuke wants to write 0 or 1 in each of the squares.
Here, all of the following conditions have to be satisfied:

For every row, the smaller of the following is A: the number of 0s contained in the row, and the number of 1s contained in the row. (If these two numbers are equal, “the smaller” should be read as “either”.)

For every column, the smaller of the following is B: the number of 0s contained in the column, and the number of 1s contained in the column.

Determine if these conditions can be satisfied by writing 0 or 1 in each of the squares. If the answer is yes, show one way to fill the squares so that the conditions are satisfied.

Constraints

1 \leq H,W \leq 1000

0 \leq A

2 \times A \leq W

0 \leq B

2 \times B \leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W A B

Output

If the conditions cannot be satisfied by writing 0 or 1 in each of the squares, print -1.

If the conditions can be satisfied, print one way to fill the squares so that the conditions are satisfied, in the following format:

s_{11}s_{12}\cdots s_{1W}
s_{21}s_{22}\cdots s_{2W}
\vdots
s_{H1}s_{H2}\cdots s_{HW}

Here s_{ij} is the digit written in the square at the i-th row from the top and the j-th column from the left in the grid.

If multiple solutions exist, printing any of them will be accepted.

Sample Input 1

3 3 1 1

Sample Output 1

100
010
001

Every row contains two 0s and one 1, so the first condition is satisfied.
Also, every column contains two 0s and one 1, so the second condition is satisfied.

Sample Input 2

1 5 2 0

Sample Output 2

01010


 var    a, b: longint;

 begin
        assign(input, 'powersocket.inp'); reset(input);
        assign(output, 'powersocket.out'); rewrite(output);

        read(a, b);
        write(b div a + ord(b mod a > 0));
 end.

Score : 200 points

Problem Statement

Takahashi's house has only one socket.

Takahashi wants to extend it with some number of power strips, each with A sockets, into B or more empty sockets.

One power strip with A sockets can extend one empty socket into A empty sockets.

Find the minimum number of power strips required.

Constraints

All values in input are integers.

2 \leq A \leq 20

1 \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of power strips required.

Sample Input 1

4 10

Sample Output 1

3

3 power strips, each with 4 sockets, extend the socket into 10 empty sockets.

Sample Input 2

8 9

Sample Output 2

2

2 power strips, each with 8 sockets, extend the socket into 15 empty sockets.

Sample Input 3

8 8

Sample Output 3

1


uses math;
var a,b,c,d,e:longint;
begin
  read(a,b);
  for a:=2 to a do
  begin
    read(c);
    if c>b then
    begin
      e:=max(e,d);
      d:=0;
    end
    else
      inc(d);
    b:=c;
  end;
  writeln(max(e,d));
end.

Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right.

The height of the i-th square from the left is H_i.

You will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.

Find the maximum number of times you can move.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the maximum number of times you can move.

Sample Input 1

5
10 4 8 7 3

Sample Output 1

2

By landing on the third square from the left, you can move to the right twice.

Sample Input 2

7
4 4 5 6 6 5 5

Sample Output 2

3

By landing on the fourth square from the left, you can move to the right three times.

Sample Input 3

4
1 2 3 4

Sample Output 3

0


var N,i,ans,cnt,pre:Longint;
H:Array[1..100000]of Longint;
begin
read(N);
for i:=1 to N do read(H[i]);
i:=1;
while i<=N do begin
pre:=i;
inc(i);
while(i<=N)and(H[i]<=H[i-1])do inc(i);
if ans<i-pre-1 then ans:=i-pre-1;
end;
writeln(ans);
end.


var n,h,next,i,count,max:int64;
begin
read(n);
read(h);
max:=0;
for i := 1 to n-1 do
begin
read(next);
if h>=next then inc(count)
else
begin
 if max <= count then max:=count;
 count:=0;
end;
h:=next
end;
if count >= max then max:=count;
write(max)
end.


var n,h,next,i,count,max:int64;
begin
read(n);
read(h);
max:=0;
for i := 1 to n-1 do
begin
read(next);
if h>=next then inc(count)
else
begin
 if max<= count then max:=count;
 count:=0;
end;
h:=next
end;
write(count)
end.


var m,d,i,j,ans,one,ten:integer;
begin
  readln(m,d);
  ans:=0;
  for i:=1 to m do
  begin
    for j:=1 to d do
    begin
      one:=j mod 10;
      ten:=j mod 10;
      if m=one*ten then inc(ans);
    end;
  end;
  writeln(ans);
end.

Score : 200 points

Problem Statement

Today is August 24, one of the five Product Days in a year.

A date m-d (m is the month, d is the date) is called a Product Day when d is a two-digit number, and all of the following conditions are satisfied (here d_{10} is the tens digit of the day and d_1 is the ones digit of the day):

d_1 \geq 2

d_{10} \geq 2

d_1 \times d_{10} = m

Takahashi wants more Product Days, and he made a new calendar called Takahashi Calendar where a year consists of M month from Month 1 to Month M, and each month consists of D days from Day 1 to Day D.

In Takahashi Calendar, how many Product Days does a year have?

Constraints

All values in input are integers.

1 \leq M \leq 100

1 \leq D \leq 99

Input

Input is given from Standard Input in the following format:

M D

Output

Print the number of Product Days in a year in Takahashi Calender.

Sample Input 1

15 40

Sample Output 1

10

There are 10 Product Days in a year, as follows (m-d denotes Month m, Day d):

4-22

6-23

6-32

8-24

9-33

10-25

12-26

12-34

14-27

15-35

Sample Input 2

12 31

Sample Output 2

5

Sample Input 3

1 1

Sample Output 3

0


var
	M,L,R,ans,ll,rr:int64;
    k,i,x,y,xl,yl,limx,limy,xx,yy,id,ie:Longint;
    dp:array[1..64,1..2,1..2]of int64;
begin
	read(L,R);
    M:=1000000007;
    ll:=1;
    rr:=2;
    for k:=1 to 60 do begin
    	if(rr<=L)or(R<ll)then begin
        	ll:=ll*2;
            rr:=rr*2;
        	continue;
        end;
        for i:=1 to 61 do for xx:=1 to 2 do for yy:=1 to 2 do dp[i,xx,yy]:=0;
        if L<=ll then if rr-1<=R then dp[1,2,2]:=1 else dp[1,2,1]:=1 else if rr-1<=R then dp[1,1,2]:=1 else dp[1,1,1]:=1;
        for i:=1 to k-1 do begin
        	x:=(L>>(k-1-i))mod 2;
            y:=(R>>(k-1-i))mod 2;
            for xl:=1 to 2 do begin
            	if xl=1 then limx:=x else limx:=0;
                for yl:=1 to 2 do begin
                	if yl=1 then limy:=y else limy:=1;
                    for yy:=0 to limy do begin
                    	for xx:=limx to yy do begin
                        	if(xl=2)or(xx>limx)then id:=2 else id:=1;
                            if(yl=2)or(yy<limy)then ie:=2 else ie:=1;
                            dp[i+1,id,ie]:=(dp[i+1,id,ie]+dp[i,xl,yl])mod M;
                        end;
                    end;
                end;
            end;
        end;
        ll:=ll*2;
        rr:=rr*2;
        ans:=(ans+dp[k,1,1]+dp[k,1,2]+dp[k,2,1]+dp[k,2,2])mod M;
    end;
    writeln(ans);
end.

Score : 600 points

Problem Statement

Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x.

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

1 \leq L \leq R \leq 10^{18}

Input

Input is given from Standard Input in the following format:

L R

Output

Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7.

Sample Input 1

2 3

Sample Output 1

3

Three pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).

Sample Input 2

10 100

Sample Output 2

604

Sample Input 3

1 1000000000000000000

Sample Output 3

68038601

Be sure to compute the number modulo 10^9 + 7.


var
	M,L,R,ans,ll,rr:int64;
    k,i,x,y,xl,yl,limx,limy,xx,yy,id,ie:Longint;
    dp:array[1..64,1..2,1..2]of int64;
begin
	read(L,R);
    M:=1000000007;
    ll:=1;
    rr:=2;
    for k:=1 to 62 do begin
    	if(rr<=L)or(R<ll)then begin
        	ll:=ll*2;
            rr:=rr*2;
        	continue;
        end;
        for i:=1 to 64 do for xx:=1 to 2 do for yy:=1 to 2 do dp[i,xx,yy]:=0;
        if L<=ll then if rr-1<=R then dp[1,2,2]:=1 else dp[1,2,1]:=1 else if rr-1<=R then dp[1,1,2]:=1 else dp[1,1,1]:=1;
        for i:=1 to k-1 do begin
        	x:=(L>>(k-1-i))mod 2;
            y:=(R>>(k-1-i))mod 2;
            for xl:=1 to 2 do begin
            	if xl=1 then limx:=x else limx:=0;
                for yl:=1 to 2 do begin
                	if yl=1 then limy:=y else limy:=1;
                    for yy:=0 to limy do begin
                    	for xx:=limx to yy do begin
                        	if(xl=2)or(xx>limx)then id:=2 else id:=1;
                            if(yl=2)or(yy<limy)then ie:=2 else ie:=1;
                            dp[i+1,id,ie]:=(dp[i+1,id,ie]+dp[i,xl,yl])mod M;
                        end;
                    end;
                end;
            end;
        end;
        ll:=ll*2;
        rr:=rr*2;
        ans:=(ans+dp[k,1,1]+dp[k,1,2]+dp[k,2,1]+dp[k,2,2])mod M;
    end;
    writeln(ans);
end.


var
	i,cnt:Longint;
	s,pre,now:String;
begin
	readln(s);
	for i:=1 to length(s)do begin
		now:=now+s[i];
		if pre<>now then begin
			pre:=now;
			now:='';
			inc(cnt);
		end;
	end;
	writeln(cnt);
end.

Score : 300 points

Problem Statement

Given is a string S consisting of lowercase English letters. Find the maximum positive integer K that satisfies the following condition:

There exists a partition of S into K non-empty strings S=S_1S_2...S_K such that S_i \neq S_{i+1} (1 \leq i \leq K-1).

Here S_1S_2...S_K represents the concatenation of S_1,S_2,...,S_K in this order.

Constraints

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

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum positive integer K that satisfies the condition.

Sample Input 1

aabbaa

Sample Output 1

4

We can, for example, divide S into four strings aa, b, ba, and a.

Sample Input 2

aaaccacabaababc

Sample Output 2

12


const nmax=200010;
var S:array[1..nmax] of char;
    save,c:char;
    count,i,len:int64;
    flag:boolean;
begin
read(c);
i:=1;
repeat
s[i]:=c;
inc(i);
read(c);
until ord(c) in [26,32,10];
len:=i-1;

flag:=false;
save:=s[1];
count:=1;
i:=1;
while i<=len do
begin
c:=s[i];
if flag then flag:=false
else
   if save=c then
   if i=len then break
   else
   begin inc(i);
         flag:=true end;
 inc(count);
 save:=c;
 inc(i)
end;

write(count-1)
end.


var k,x,i:longint;
begin
  read(k,x);
  for i:=-1000000 to 1000000 do
    if abs(i-x)<k then write(i,' ')
end.

Score : 200 points

Problem Statement

There are 2000001 stones placed on a number line. The coordinates of these stones are -1000000, -999999, -999998, \ldots, 999999, 1000000.

Among them, some K consecutive stones are painted black, and the others are painted white.

Additionally, we know that the stone at coordinate X is painted black.

Print all coordinates that potentially contain a stone painted black, in ascending order.

Constraints

1 \leq K \leq 100

0 \leq X \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K X

Output

Print all coordinates that potentially contain a stone painted black, in ascending order, with spaces in between.

Sample Input 1

3 7

Sample Output 1

5 6 7 8 9

We know that there are three stones painted black, and the stone at coordinate 7 is painted black. There are three possible cases:

The three stones painted black are placed at coordinates 5, 6, and 7.

The three stones painted black are placed at coordinates 6, 7, and 8.

The three stones painted black are placed at coordinates 7, 8, and 9.

Thus, five coordinates potentially contain a stone painted black: 5, 6, 7, 8, and 9.

Sample Input 2

4 0

Sample Output 2

-3 -2 -1 0 1 2 3

Negative coordinates can also contain a stone painted black.

Sample Input 3

1 100

Sample Output 3

100


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

var i,j,n,m:longint;
    ans:int64;
    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 (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;

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.

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


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 (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;

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 : int64;
    b : integer;
  end;
	pairArray = array[1..100000] of pair;

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

procedure swap(var X, Y: integer);
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:integer);
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:integer);
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:integer;
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.


var
	n,i,sum:integer;

begin
	sum := 0;
	read(n);

   for i := 1 to n do begin

        begin
        	if i <= 9 then
            	 Inc(sum);
        end;

        begin
        	if (i >= 100) and (i <= 999) then
            	Inc(sum);
        end;

         begin
        	if (i >= 10000) and (i <= 99999) then
            	Inc(sum);
        end;

    end;

    writeln(sum);

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
	n,i,sum:integer;

begin
	sum := 0;
	read(n);

   for i := 1 to n do begin

        begin
        	if i <= 9 then
            	 Inc(sum);
        end;

        begin
        	if (i >= 100) and (i <= 999) then
            	Inc(sum);
        end;

         begin
        	if (i >= 100000) and (i <= 99999) then
            	Inc(sum);
        end;

    end;

    writeln(sum);

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 := 1 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;
 break;
 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:Longint;
	A,L,R:Array[1..200000]of Longint;
begin
	read(N);
	for i:=1 to N do read(A[i]);
	for i:=2 to N do begin
		L[i]:=L[i-1];
		if A[i-1]>L[i] then L[i]:=A[i-1];
	end;
	for i:=N-1 downto 1 do begin
		R[i]:=R[i+1];
		if A[i+1]>R[i] then R[i]:=A[i+1];
	end;
	for i:=1 to N do if L[i]>R[i] then writeln(L[i]) else writeln(R[i]);
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


program ABC134C;
var
	n, i, m0, m1, x: integer;
	A: array[0..200000] of integer;
begin
	read(n);
	m0 := 0;
	m1 := 0;
	for i := 1 to n do begin
		read(x);
		if x > m0 then begin
			m1 := m0;
			m0 := x;
		end
		else if x > m1 then begin
			m1 := x;
		end;
		A[i] := x;
	end;
	for i := 1 to n do begin
		if A[i] = m0 then begin
			writeln(m1);
		end
		else begin
			writeln(m0);
		end;
	end;
end.


var a, ck: array[1..200001] of longint;
    i, j, n, x, ans: longint;
begin
    read(n);
    for i := 1 to n do read(a[i]);
    for i := n downto 1 do
    begin
        x := 0;
        j := 2 * i;
        while j <= n do
        begin
            x := x + ck[j];
            j := j + i;
        end;
        if x mod 2 <> a[i] then ck[i] := 1;
    end;
    for i := 1 to n do if ck[i] = 1 then ans := ans + 1;
    writeln(ans);
    for i := 1 to n do if ck[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(2e5);
var a,b:array[1..maxn] of longint;
    n,m,s,i,j:longint;
    check:boolean;

begin
	readln(n);
	for i:=1 to n do read(a[i]);
	m:=0; check:=true;
	for i:=1 to n do
	  begin
	    s:=a[i];
	    j:= 2 * i;
	    while j<=n do
	      begin
	        s:=s + a[j];
	        j:=j +i;
	      end;
	    if (s mod 2 = a[i]) and (a[i] = 1) then
	      begin
	        inc(m); b[m]:=i;
	      end;
	    if s mod 2 <> a[i] then check:=false;
	    if not check then break;
	  end;
	if not check then write(-1)
	else
	  begin
    	writeln(m);
    	for i:=1 to m do write(b[i],' ');
    end;
end.


const maxn = trunc(2e5);
var a,b:array[1..maxn] of longint;
    n,m,s,i,j:longint;

begin
	readln(n);
	for i:=1 to n do read(a[i]);
	m:=0;
	for i:=1 to n do
	  begin
	    s:=a[i];
	    j:= 2 * i;
	    while j<=n do
	      begin
	        s:=s + a[j];
	        j:=j +i;
	      end;
	    if (s mod 2 = a[i]) and (a[i] = 1) then
	      begin
	        inc(m); b[m]:=i;
	      end;
	  end;
	writeln(m);
	for i:=1 to m do write(b[i],' ');
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);
for i := 1 to N do
 begin read(a[i]);
  if i > N div 2 then
   begin
    ans[i]:=a[i];inc(latter)
   end
  end;
for i := N-latter downto 1 do
 begin
  sum:=0;
  for j := 1 to N do
   if i*j > N then break
   else sum:=sum+a[i*j];
  ans[i]:=sum 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.


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] - a[i]
	  else b[1]:=b[1] + a[i];
	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.

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
        a,ans:array[1..100000]of int64;
        i,n:longint;
        o:int64;
begin
        read(n);
        for i:=1 to n do
        begin
                read(a[i]);
                if i mod 2=1 then o:=o+a[i] else o:=o-a[i];
        end;
        ans[1]:=o div 2;
        for i:=2 to n do ans[i]:=a[i-1]-ans[i-1];
        for i:=1 to n-1 do write(2*ans[i],' ');
        writeln(2*ans[n]);
end.


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] - a[i]
	  else b[1]:=b[1] + a[i];
	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.


type ary=array[1..100010] of int64;
var n,k,i:int64;
    d:ary;
procedure sort(p,q :int64; var d:ary);
  var i, j, k, w :int64;
begin i := p; j := q; k:=d[(p+q) div 2];
  repeat
    while d[i] < k do i := i+1;
    while k < d[j] do j := j-1;
    if i <= j then begin
      w := d[i]; d[i] := d[j]; d[j] := w;
      i := i+1;  j := j-1
    end
  until i > j;
  if p < j then sort(p,j,d);
  if i < q then sort(i,q,d)
end;
begin
read(n);
for i := 1 to n do read(d[i]);
sort(1,n,d);
write(d[(n div 2)+1]-d[n div 2])
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 NMAX=1000010;
type  ary=array[1..NMAX]of int64;
var n,i,j,k,ans  :int64;
    d  :ary;

procedure sort(p,q :int64; var a :ary);
  var i, j, k, w :int64;
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;
      i := i+1;  j := j-1
    end
  until i > j;
  if p < j then sort(p,j,a);
  if i < q then sort(i,q,a)
end;

begin
  readln(n);
  for i:=1 to n do read(d[i]);
  sort(1,n,d);
  j:=d[(n div 2)+1];
  writeln(j-d[n div 2]);
end.


var n,re,t,i:longint;
    a:array[1..100000] of longint;

procedure sort(l,r:longint);
var x,tmp,i,j:longint;
begin
	i:=l; j:=r;
	x:=a[(l + r) shr 1];
	repeat
	  while a[i] < x do inc(i);
	  while a[j] > x do dec(j);
	  if i <= j then
	    begin
	      tmp:=a[i];
	      a[i]:=a[j];
	      a[j]:=tmp;
	      inc(i);
	      dec(j);
	    end;
	until i > j;
	if l < j then sort(l,j);
	if i < r then sort(i,r);
end;

begin
	readln(n);
	for i:=1 to n do read(a[i]);
	sort(1,n);
    t:= n shr 1;
    if a[t] = a[t + 1] then re:=0
    else re:=a[t + 1] - a[t];
    write(re);
end.


const modP = 1000000007;
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;

  m:=n-k+1;
  NKcomb[0]:=1; NKcomb[m]:=1;
  Kcomb[0]:=1; Kcomb[k-1]:=1;
  for i:=1 to (k+1)div 2 do begin
    Kcomb[i]:=Kcomb[i-1]*(k-i)mod modP*inv[i] mod modP ;
    Kcomb[k-1-i]:=Kcomb[i];
  end;
  for i:=1 to (m+1)div 2 do begin
    NKcomb[i]:=NKcomb[i-1]*(m-i+1)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] * Kcomb[i-1] 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 modP = 1000000007;
var Kcomb,NKcomb:array[0..2010]of int64;
    i,j,k,a,b,n,m,p:int64;

begin
  readln(n,k);
  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)div i;
    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)div i;
    NKcomb[m-i]:=NKcomb[i];
  end;
if k>n-k+1 then begin
  for i:=1 to n-k+1 do begin
    b:=(NKcomb[i] mod modP)*(Kcomb[i-1] mod modP);
    writeln(b);
  end;
  for i:=n-k+2 to k do writeln(0);
end
else
  for i:=1 to k do begin
    b:=(NKcomb[i] mod modP)*(Kcomb[i-1] mod modP);
    writeln(b);
  end;
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;
		if R<>0 then R:=R*(N-K-i)mod M*inv[i+2]mod M;
	end;
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 base = trunc(1e9) + 7;
var f:array[0..2000] of int64;
    n,k,i:integer;
    re:int64;

procedure init;
var i:integer;
begin
	f[0]:=1;
	f[1]:=1;
	for i:=2 to 2000 do f[i]:= (f[i - 1] * i) mod base;
end;

function power(a,b:int64):int64;
var tmp:int64;
begin
	if b = 0 then exit(1);
	if b = 1 then exit(a);
	tmp:= power(a,b shr 1);
	if b mod 2 = 0 then power:= (tmp * tmp) mod base
	else power:= (((tmp * tmp) mod base) * a) mod base;
end;

function calc(n,k:integer):int64;
begin
	calc:= (f[n] * power(f[k] * f[n - k], base - 2) mod base) mod base;
end;

begin
	readln(n,k);
	init;
	for i:=1 to k do
	  begin
	    writeln((calc(k-1,i-1) * calc(n-k+1,i)) mod base);
	  end;
end.


const base = trunc(1e9) + 7;
var f:array[0..2000] of int64;
    n,k,i:integer;
    re,x1,x2:int64;

procedure init;
var i:integer;
begin
	f[0]:=1;
	f[1]:=1;
	for i:=2 to 2000 do f[i]:= (f[i - 1] * i) mod base;
end;

function power(a,b:int64):int64;
var tmp:int64;
begin
	if b = 0 then exit(1);
	if b = 1 then exit(a);
	tmp:= power(a,b shr 1);
	if b mod 2 = 0 then power:= (tmp * tmp) mod base
	else power:= (((tmp * tmp) mod base) * a) mod base;
end;

function calc(n,k:integer):int64;
begin
	calc:= (f[n] * power(f[k] * f[n - k], base - 2) mod base) mod base;
end;

begin
	readln(n,k);
	init;
	for i:=1 to k do
	  begin
	    x1:=calc(k - 1, i - 1);
	    x2:= calc(n - k + 1, i);
	    re:=(x1 * x2) mod base;
	    writeln(re);
	  end;
end.


{$mode Delphi}
uses Classes, SysUtils, Fgl;

type
  pair = record first : int64;
                second : int64;
                class operator = (p1, p2 : pair) : boolean;
                end;
  pairList = TFPGList<pair>;

class operator pair.=(p1, p2 : pair): boolean;
begin
  result := (p1.first = p2.first) and (p1.second = p2.second);
end;

var arr : pairList;
    n, i : integer;
    p : pair;
    now_time : int64;
    can : boolean;

function pairCompare(const p1, p2 : pair) : int64;
begin
  if p1.first = p2.first then result := p1.second - p2.second
  else result := p1.first - p2.first;
end;

begin
  arr := TFPGList<pair>.Create();
  read(n);
  for i := 0 to n-1 do begin
    read(p.second, p.first);
    arr.Add(p);
  end;
  arr.Sort(@pairCompare);
  can := true;
  for i := 0 to n-1 do begin
    now_time := now_time + arr[i].second;
        if now_time > arr[i].first then begin
            can := false;
            break;
        end;
  end;
  if can then writeln('Yes') else writeln('No');
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;
	S:String;
procedure f(u:Longint);
begin
	if u=100 then S:=S+'100'
	else begin
		if u>9 then S:=S+chr(u div 10+48);
		S:=S+chr(u mod 10+48);
	end;
end;
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
		S:=S+'1 ';
		f(i);
		S:=S+' ';
	end;
	for u:=2 to N do begin
		if K=0 then break;
		for v:=u+1 to N do begin
			if K=0 then break;
			f(u);
			S:=S+' ';
			f(v);
			S:=S+' ';
			dec(K);
		end;
	end;
	writeln(S);
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 W,H,x,y:integer;
begin
 read(W,H,x,y);
 write(W*H/2);
 if (x=W/2) and (y=H/2) 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
	N,i,j,ans,id,k:Longint;
	x,y:array[1..50]of Longint;
	s,t:array[1..2500]of Longint;
begin
	read(N);
	for i:=1 to N do read(x[i],y[i]);
	for i:=1 to N do for j:=1 to N do begin
		if i=j then continue;
		inc(id);
		s[id]:=x[i]-x[j];
		t[id]:=y[i]-y[j];
		for k:=id-1 downto 1 do begin
			if(s[k]>s[k+1])or((s[k]=s[k+1])and(t[k]>t[k+1]))then begin
				ans:=s[k];
				s[k]:=s[k+1];
				s[k+1]:=ans;
				ans:=t[k];
				t[k]:=t[k+1];
				t[k+1]:=ans;
			end else break;
		end;
	end;
	ans:=0;
	i:=1;
	while i<=id do begin
		k:=1;
		while(i+1<=id)and(s[i]=s[i+1])and(t[i]=t[i+1])do begin
			inc(k);
			inc(i);
		end;
		if ans<k then ans:=k;
		inc(i);
	end;
	writeln(N-ans);
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,n,M,r,i:int64;    //a=a_i、b=a_i-1
    G:array[1..100000] of integer;
begin
 read(N,M);

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

 n:=1; read(a);
 if a=1 then c:=1 else c:= a-1;
    n:=n*G[c]; b:=a;

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

write(r)
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.


Uses math;
var n, m, i, a : int64;
    dp : array[1..100010] of int64;
    remainder : int64;
begin
  remainder := 1000000007;
  readln(n, m);

  for i := 1 to 100010 do dp[i] := 0;
  for i := 1 to m do begin
    read(a);
    dp[a] := -1;
  end;
  dp[0] := 1;

  for i := 0 to 100005 do begin
    if dp[i] = -1 then Continue;
    if dp[i+1] <> -1 then dp[i+1] := (dp[i+1] + dp[i]) mod remainder;
    if dp[i+2] <> -1 then dp[i+2] := (dp[i+2] + dp[i]) mod remainder;
  end;
  writeln(dp[n]);
end.


var
	N,A,B,C,D,i:Longint;
	S:String;
begin
	readln(N,A,B,C,D);
	readln(S);
	for i:=A to C-1 do if(S[i]='#')and(S[i+1]='#')then begin
		writeln('No');
		exit;
	end;
	for i:=B to D-1 do if(S[i]='#')and(S[i+1]='#')then begin
		writeln('No');
		exit;
	end;
	if C<D then writeln('Yes')else begin
		for i:=B-1 to D-1 do if(S[i]='.')and(S[i+1]='.')and(S[i+2]='.')then begin
			writeln('Yes');
			exit;
		end;
		writeln('No');
	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


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 max(C,D)-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('Yes');
  end;
end.


var i,v,n,a,b,c,d,mv:longint;
    s:char;
    ans:boolean;

function max(a,b:longint):longint;
begin
  if a > b then max := a else max := b
end;

begin
  read(n,a,b,c,d);
  v := 0;
  mv := 0;
  ans := true;
  for i:=1 to n do begin
    read(s);
    if (a<=i) and (c>=i) and (d>=i) then begin
      if (v=0) and (s='#') then ans := false;
      if s='.' then v := v + 1 else v := 0;
      if (b<=i) and (d>=i) then mv := max(v,mv)
    end
  end;
  if (c>d) and (mv<3) then ans := false;
  if ans then writeln('Yes') else writeln('No')
end.


var i,v,n,a,b,c,d,mv:longint;
    s:char;
    ans:boolean;

function max(a,b:longint):longint;
begin
  if a > b then max := a else max := b
end;

begin
  read(n,a,b,c,d);
  v := 0;
  mv := 0;
  ans := true;
  for i:=1 to n do
    if (a<=i) and (c>=i) and (d>=i)then begin
      read(s);
      if (v=0) and (s='#') then ans := false;
      if s='.' then v := v + 1 else v := 0;
      if (b<=i) and (d>=i) then mv := max(v,mv)
    end;
  if (c>d) and (mv<3) then ans := false;
  if ans then writeln('Yes') else writeln('No')
end.


var
	cnt,N,M,i,j,k,s:Longint;
	A:array[1..10,1..10]of Boolean;
	P:array[1..10]of Boolean;
	flag,now: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(s);
		P[i]:=s=1;
	end;
	for i:=0 to (1<<N)-1 do begin
		flag:=True;
		for j:=1 to M do begin
			now:=False;
			for k:=1 to N do begin
				now:=now xor((((i>>(k-1))and 1)=1)and A[j,k]);
			end;
			if now xor P[j]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
	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.


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
		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 N,M,i,L,R,aL,aR:Longint;
begin
read(N,M);
aL:=1;
aR:=N;
for i:=1 to M do begin
read(L,R);
if aL<L then aL:=L;
if aR>R then aR:=R;
end;
if aL>aR then writeln(0)else writeln(aR-aL+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


var N,M,x,y,z,w,i:integer;
begin
 read(N,M);
 x:=0;y:=0;
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.


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


var i,n,m,l,r,min,max:longint;

begin
  readln(n,m);
  min:=1;
  max:=n;
  for i:=1 to m do begin
    readln(l,r);
    if l>min then min:=l;
    if r<max then max:=r
  end;
  writeln(max-min+1);
end.


type
	intArray = array[1..100000] of int64;

var i,j,n,m,b:longint;
    c,sum:int64;
    a:intArray;

procedure	swap(var a,b:int64);
var tmp:int64;
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;

begin
  readln(n,m);
  for i:=1 to n do read(a[i]);
  for i:=1 to m do begin
    QuickSort(a,1,n);
    readln(b,c);
    for j := 1 to b do begin
      if a[j]<c then a[j]:=c else break;
    end;
  end;
  sum := 0;
  for i:=1 to n do sum := sum + a[i];
  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.


uses crt;
var n,k:longint;
    s:string;
begin
        clrscr;
        readln(n,k);
        readln(s);
        s[k]:=chr(ord(s[k])+32);
        write(s);
        readln

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 c : array[1..20] of char;
    n, k : integer;
    i : integer;
begin
  read(n, k);
  readln();
  for i := 1 to n do read(c[i]);
  if c[k] = 'A' then c[k] := 'a';
  if c[k] = 'B' then c[k] := 'b';
  if c[k] = 'C' then c[k] := 'c';
  for i := 1 to n do write(c[i]);
end.


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 1 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 s,r,g,b,n,t:Longint;
begin
read(r,g,b,n);
if r<g then begin
	t:=r;
	r:=g;
	g:=t;
end;
if r<b then begin
	t:=r;
	r:=b;
	b:=t;
end;
if r=1 then begin
	t:=n;
	while t>=0 do begin
		inc(s,t div g+1);
		dec(t,b);
	end;
	writeln(s);
	exit;
end;
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 s,r,g,b,n,t:Longint;
begin
read(r,g,b,n);
if r<g then begin
	t:=r;
	r:=g;
	g:=t;
end;
if r<b then begin
	t:=r;
	r:=b;
	b:=t;
end;
if r=1 then begin
	t:=n;
	while t>=0 do begin
		inc(s,t div g);
		dec(t,b);
	end;
	writeln(s);
	exit;
end;
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.


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) or zero then
    ans:=ans-maxm;
  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  :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:=1 to n do t[i]:=s[i]+n+s[i]-i-s[n];
  for i:=1 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:=2 to n do begin
    if s[i]=s[i-1] then begin
      j:=s[i]+n+s[i]-i-s[n];
      ans:=min(ans,j)
    end
    end;
    writeln(ans)
end.


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:=2 to n do begin
    if s[i]=s[i-1] then begin
      j:=s[i]+n+s[i]-i-s[n];
      ans:=min(ans,j)
    end
    end;
    writeln(ans);
    for i:=1 to n do write(s[i])
end.


var s,t:char;
    n,i,b,w,ans:integer;
begin
  readln(n);
  s:='.';
  b:=0; w:=0;
  ans:=0;
  for i:=1 to n do begin
    read(t);
    if (s=t) and (t='#') then inc(b);
    if (s=t) and (t='.') then inc(w);
    if (s<>t) and (t='#') then begin
      if b<w then inc(ans,b)
      else inc(ans,w);
      b:=1;
      w:=0
    end;
    if (s<>t) and (t='.')then inc(w);
    s:=t
  end;
  write(ans)

end.


var a:char;
    flag:boolean;
    i,n,k:integer;
begin
  readln(n);
  k:=0;
  flag:=false;
  {for i:=1 to n do begin
    read(a);
    if a='#' then flag:=true
    else if flag then inc(k)
  end;
  }repeat
    read(a);
    if a='#' then flag:=true
    else if flag then inc(k);
  until eoln;
  write(k)
end.


var c:char;
    n,ans:integer;

function ifThen(p:boolean;t,f:integer):integer;
begin
  if p then ifThen := t else ifThen := f
end;

function CharToInt(c:char):integer;
begin
  CharToInt := Ord(c)-Ord('0')
end;

function eolnread:boolean;
begin
  if eoln then eolnread:=false else begin
  read(c);
  eolnread:=true
  end
end;

begin
  n := 0;
  ans := 0;
  while eolnread do begin
    n := n + 1;
    ans := ans + (CharToInt(c) xor n mod 2);
  end;
  writeln(ifThen(ans*2<n,ans,n-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


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.


var
  a,i,n,x,y:longint;
  ch:char;
  s:string;
begin
  readln(s);
  n:=length(s);
  for i:=1 to n do
    begin
      ch:=s[i];
      a:=ord(ch)-ord('0');
      if a=i mod 2 then inc(x) else inc(y);
    end;
  if x>y then write(y) else write(x);
end.


var
n,i,j,cnt,t:Longint;
a,b:array[1..10000]of Byte;
begin
read(n);
t:=n+1-n mod 2;
for i:=1 to n do begin
for j:=i+1 to t-i-1 do begin
inc(cnt);
a[cnt]:=i;
b[cnt]:=j;
end;
if(j=t-i-1)or(j=t-i)then j:=t-i+1;
for j:=j to n do begin
inc(cnt);
a[cnt]:=i;
b[cnt]:=j;
end;
end;
writeln(cnt);
for i:=1 to cnt do writeln(a[i],' ',b[i]);
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 Longint;
begin
read(n);
t:=n+1-n mod 2;
for i:=1 to n do begin
for j:=i+1 to t-i-1 do begin
inc(cnt);
a[cnt]:=i;
b[cnt]:=j;
end;
if(j=t-i-1)or(j=t-i)then j:=t-i+1;
for j:=j to n do begin
inc(cnt);
a[cnt]:=i;
b[cnt]:=j;
end;
end;
writeln(cnt);
for i:=1 to cnt do writeln(a[i],' ',b[i]);
end.


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


const
  MD=1000000007;
var
  i,n:longint;
  a,ys,xg,f:array[0..200010] of int64;
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]) mod MD;
      if xg[i]>i+1 then f[xg[i]]:=(f[xg[i]]+f[i]) mod MD;
    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


type
  pair = record
    a : int64;
    b : integer;
  end;
	pairArray = array[1..100000] of pair;
var i,n,m:integer;
    input:pairArray;
    sum : int64;

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].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
			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
  sum := 0;
  readln(n,m);
  for i := 1 to n do read(input[i].a,input[i].b);
  QuickSort(input,1,n);
  for i := 1 to n do begin
    if m > input[i].b then begin
      sum := sum + input[i].b * input[i].a;
      m := m - input[i].b
    end else begin
      sum := sum + m * input[i].a;
      m := 0
    end;
    if m = 0 then break;
	end;
  writeln(sum)
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.


var c:char; r,b:integer;
begin
  r:=0; b:=0;
  repeat
    read(c);
    if c = '0' then r:=r+1 else b:=b+1
  until eoln;
  if r>b then write(2*b) else write(2*r)
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


const
	tmax=100000;
	inf=tmax*tmax*tmax;
type
	arr=array[0..100010] of int64;
	node=record
				t1,t2,tans:int64;
			end;
var
	s1,s2:node;
	a,b:arr;
	n,m,q,i:longint;
	ans,t,x:int64;
procedure qsort(x:arr; l,r:longint);
var
	t,mid:int64;
	i,j:longint;
begin
	i:=l; j:=r; mid:=x[(l+r) div 2];
	repeat
		while x[i]<mid do inc(i);
		while x[j]>mid do dec(j);
		if i<=j then
		begin
			t:=x[i]; x[i]:=x[j]; x[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(x,l,j);
	if i<r then qsort(x,i,r);
end;
function fill(x:int64):node;
var
	t:node;
begin
	t.t1:=x;
	t.t2:=x;
	t.tans:=x;
	exit(t);
end;
function find(x:int64; len:longint; a:arr):node;
var
	l,r,mid:longint;
	ans:node;
begin
	if (len=1) or (x<a[1]) then exit(fill(a[1]));
	if x>a[len] then exit(fill(a[len]));
	l:=1; r:=len;
	while l<r-1 do
	begin
		mid:=(l+r) div 2;
		if x<a[mid] then r:=mid
		else l:=mid;
	end;
	ans.t1:=a[l];
	ans.t2:=a[r];
	if abs(x-a[l])<abs(x-a[r]) then ans.tans:=a[l]
	else ans.tans:=a[r];
	exit(ans);
end;
begin
	readln(n,m,q);
	for i:=1 to n do read(a[i]); qsort(a,1,n);
	for i:=1 to m do read(b[i]); qsort(b,1,m);
	for i:=1 to q do
	begin
		read(x);
		ans:=inf;
		s1:=find(x,n,a); s2:=find(x,m,b);
		t:=abs(s1.t1-x)+abs(s1.t1-find(s1.t1,m,b).tans);
		if t<ans then ans:=t;
		t:=abs(s1.t2-x)+abs(s1.t2-find(s1.t2,m,b).tans);
		if t<ans then ans:=t;
		t:=abs(s2.t1-x)+abs(s2.t1-find(s2.t1,n,a).tans);
		if t<ans then ans:=t;
		t:=abs(s2.t2-x)+abs(s2.t2-find(s2.t2,n,a).tans);
		if t<ans then ans:=t;
		writeln(ans);
	end;
end.

Score : 400 points

Problem Statement

Along a road running in an east-west direction, there are A shrines and B temples.
The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.

Answer the following Q queries:

Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

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

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.


const
	tmax=100000;
	inf=tmax*tmax*tmax;
type
	arr=array[0..100010] of int64;
	node=record
				t1,t2,tans:int64;
			end;
var
	s1,s2:node;
	a,b:arr;
	n,m,q,i:longint;
	ans,t,x:int64;
procedure qsort(x:arr; l,r:longint);
var
	t,mid:int64;
	i,j:longint;
begin
	i:=l; j:=r; mid:=x[(l+r) div 2];
	repeat
		while x[i]<mid do inc(i);
		while x[j]>mid do dec(j);
		if i<=j then
		begin
			t:=x[i]; x[i]:=x[j]; x[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(x,l,j);
	if i<r then qsort(x,i,r);
end;
function fill(x:int64):node;
var
	t:node;
begin
	t.t1:=x;
	t.t2:=x;
	t.tans:=x;
	exit(t);
end;
function find(x:int64; len:longint; a:arr):node;
var
	l,r,mid:longint;
	ans:node;
begin
	if (len=1) or (x<a[1]) then exit(fill(a[1]));
	if x>a[len] then exit(fill(a[len]));
	l:=1; r:=len;
	while l<r do
	begin
		mid:=(l+r) div 2;
		if x<a[mid] then r:=mid
		else l:=mid+1;
	end;
	if x>a[r] then
	begin
		ans.t1:=a[r];
		ans.t2:=a[r+1];
		if abs(x-a[r])<abs(x-a[r+1]) then ans.tans:=a[r]
		else ans.tans:=a[r+1];
		exit(ans);
	end;
	if x<a[r] then
	begin
		ans.t1:=a[r-1];
		ans.t2:=a[r];
		if abs(x-a[r-1])<abs(x-a[r]) then ans.tans:=a[r-1]
		else ans.tans:=a[r];
		exit(ans);
	end;
end;
begin
	readln(n,m,q);
	for i:=1 to n do read(a[i]); qsort(a,1,n);
	for i:=1 to m do read(b[i]); qsort(b,1,m);
	for i:=1 to q do
	begin
		read(x);
		ans:=inf;
		s1:=find(x,n,a); s2:=find(x,m,b);
		t:=abs(s1.t1-x)+abs(s1.t1-find(s1.t1,m,b).tans);
		if t<ans then ans:=t;
		t:=abs(s1.t2-x)+abs(s1.t2-find(s1.t2,m,b).tans);
		if t<ans then ans:=t;
		t:=abs(s2.t1-x)+abs(s2.t1-find(s2.t1,n,a).tans);
		if t<ans then ans:=t;
		t:=abs(s2.t2-x)+abs(s2.t2-find(s2.t2,n,a).tans);
		if t<ans then ans:=t;
		writeln(ans);
	end;
end.


type intArray = array[1..10000] of int64;
var s,t,x:intArray;
  min1,min2,miner:int64;
  i,j,a,b,q,m: integer;

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

begin
  readln(a,b,q);
  for i := 1 to a do readln(s[i]);
  for i := 1 to b do readln(t[i]);
  for i := 1 to q do readln(x[i]);
  for i := 1 to q do begin
    for j := 1 to a do begin
      if x[i]<s[j] then begin
        if j = 1 then min1 := s[j]-x[i]
        else min1 := min(x[i]-s[j-1],s[j]-x[i]);
        break
      end;
      if j = a then min1 := x[i]-s[j]
    end;
    m := j;
    for j := 1 to b do begin
      if s[m]<t[j] then begin
        if j = 1 then min2 := t[j]-s[m]
        else min2 := min(s[m]-t[j-1],t[j]-s[m]);
        break
      end;
      if j = b then min2 := s[m]-t[j]
    end;
    miner := min1 + min2;
    for j := 1 to b do begin
      if x[i]<t[j] then begin
        if j = 1 then min1 := t[j]-x[i]
        else min1 := min(x[i]-t[j-1],t[j]-x[i]);
        break
      end;
      if j = b then min1 := x[i]-t[j]
    end;
    m := j;
    for j := 1 to a do begin
      if t[m]<s[j] then begin
        if j = 1 then min2 := s[j]-t[m]
        else min2 := min(t[m]-s[j-1],s[j]-t[m]);
        break
      end;
      if j = a then min2 := t[m]-s[j];
    end;
    writeln(min(miner,min1+min2))
  end
end.


var
	n,i,ans:Longint;
	A:Array[1..100000]of Longint;
function gcd(a,b:Longint):Longint;
begin
	if b=0 then gcd:=a else gcd:=gcd(b,a mod b);
end;
begin
	read(n);
	for i:=1 to n do begin
		read(A[i]);
		ans:=gcd(ans,A[i]);
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

There are N monsters, numbered 1, 2, ..., N.

Initially, the health of Monster i is A_i.

Below, a monster with at least 1 health is called alive.

Until there is only one alive monster, the following is repeated:

A random alive monster attacks another random alive monster.

As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.

Find the minimum possible final health of the last monster alive.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible final health of the last monster alive.

Sample Input 1

4
2 10 8 40

Sample Output 1

2

When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000


const
	t:array[1..9] of longint=(2,5,5,4,5,6,3,7,6);
var
	n,m,min,t1,i,j,cnt:longint;
	a:array[0..10] of longint;
	ans:array[0..5000] of longint;
	f:array[0..10010] of boolean;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=ans[(l+r) div 2];
	repeat
		while ans[i]<mid do inc(i);
		while ans[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	readln(n,m);
	min:=10;
	fillchar(f,sizeof(f),false);
	f[0]:=true;
	for i:=1 to m do
	begin
		read(a[i]);
		for j:=a[i] to n do
			if f[j-a[i]] then f[j]:=true;
		if t[a[i]]<min then
		begin
			min:=t[a[i]];
			t1:=a[i];
		end;
	end;
	cnt:=0;
	while f[n-min] do
	begin
		n:=n-min;
		inc(cnt); ans[cnt]:=t1;
	end;
	for i:=1 to m-1 do
		for j:=1 to m-i do
			if (t[a[j]]>t[a[j+1]]) or (t[a[j]]=t[a[j+1]]) and (a[j]<a[j+1]) then
			begin
				t1:=a[j]; a[j]:=a[j+1]; a[j+1]:=t1;
			end;
	t1:=1;
	while n>0 do
	begin
		while f[n-t[a[t1]]] do
		begin
			n:=n-t[a[t1]];
			inc(cnt); ans[cnt]:=a[t1];
		end;
		inc(t1);
	end;
	qsort(1,cnt);
	for i:=cnt downto 1 do write(ans[i]);
	writeln;
end.

Score : 400 points

Problem Statement

Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:

Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).

The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

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

Sample Input 3

15 3
5 4 6

Sample Output 3

654


var n,a,b,left,right:integer;
begin
readln(n,a,b);
if(a+b>=n)then left:=a+b-n else left:=0;
if(a>=b)then right:=b else right:=a;
writeln(left,' ',right);
end.

Score : 100 points

Problem Statement

We conducted a survey on newspaper subscriptions.
More specifically, we asked each of the N respondents the following two questions:

Question 1: Are you subscribing to Newspaper X?

Question 2: Are you subscribing to Newspaper Y?

As the result, A respondents answered "yes" to Question 1, and B respondents answered "yes" to Question 2.

What are the maximum possible number and the minimum possible number of respondents subscribing to both newspapers X and Y?

Write a program to answer this question.

Constraints

1 \leq N \leq 100

0 \leq A \leq N

0 \leq B \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the maximum possible number and the minimum possible number of respondents subscribing to both newspapers, in this order, with a space in between.

Sample Input 1

10 3 5

Sample Output 1

3 0

In this sample, out of the 10 respondents, 3 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 3 and at least 0.

Sample Input 2

10 7 5

Sample Output 2

5 2

In this sample, out of the 10 respondents, 7 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 5 and at least 2.

Sample Input 3

100 100 100

Sample Output 3

100 100