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

Score : 400 points

Problem Statement

Fennec and Snuke are playing a board game.

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

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

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

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

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

Constraints

2 \leq N \leq 10^5

1 \leq a_i, b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

Fennec

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

Sample Input 2

4
1 4
4 2
2 3

Sample Output 2

Snuke


const fi='agc017_a.inp';
        fo='agc017_a.out';
type hanh=extended;
var d:array[0..50] of hanh;
        a,b,p:longint;
procedure mori;
        var i:longint;
        begin
                d[0]:=1;
                for i:=1 to 50 do d[i]:=d[i-1]*i;
        end;
procedure ran;
        var n,x,i:longint;
        begin
                readln(n,p);
                a:=0;
                b:=0;
                p:=p mod 2;
                for i:=1 to n do
                        begin
                                read(x);
                                if x mod 2=0 then inc(a) else inc(b);
                        end;
        end;
function conan:hanh;
        var     i:longint;
                aa,bb:hanh;
        begin
               bb:=1;
                        for i:=1 to b do
                                if i mod 2=0 then
                                        bb:=bb+d[b]/(d[i]*d[b-i]);
               aa:=1;
                        for i:=1 to a do
                                aa:=aa+d[a]/(d[i]*d[a-i]);
              exit(aa*bb);

        end;
function conan1:hanh;
        var aa,bb:hanh;
                i:longint;
        begin
                bb:=0;
                        for i:=1 to b do
                                if i mod 2=1 then
                                        bb:=bb+d[b]/(d[i]*d[b-i]);
                aa:=1;
                        for i:=1 to a do
                                aa:=aa+d[a]/(d[i]*d[a-i]);
                exit(aa*bb);
        end;
begin

        ran;
        mori;
        if p=0 then writeln(conan:0:0) else writeln(conan1:0:0);
end.

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

Score : 200 points

Problem Statement

There are N bags of biscuits. The i-th bag contains A_i biscuits.

Takaki will select some of these bags and eat all of the biscuits inside.
Here, it is also possible to select all or none of the bags.

He would like to select bags so that the total number of biscuits inside is congruent to P modulo 2.
How many such ways to select bags there are?

Constraints

1 \leq N \leq 50

P = 0 or 1

1 \leq A_i \leq 100

Input

Input is given from Standard Input in the following format:

N P
A_1 A_2 ... A_N

Output

Print the number of ways to select bags so that the total number of biscuits inside is congruent to P modulo 2.

Sample Input 1

2 0
1 3

Sample Output 1

2

There are two ways to select bags so that the total number of biscuits inside is congruent to 0 modulo 2:

Select neither bag. The total number of biscuits is 0.

Select both bags. The total number of biscuits is 4.

Sample Input 2

1 1
50

Sample Output 2

0

Sample Input 3

3 0
1 1 1

Sample Output 3

4

Two bags are distinguished even if they contain the same number of biscuits.

Sample Input 4

45 1
17 55 85 55 74 20 90 67 40 70 39 89 91 50 16 24 14 43 24 66 25 9 89 71 41 16 53 13 61 15 85 72 62 67 42 26 36 66 4 87 59 91 4 25 26

Sample Output 4

17592186044416

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.


program Project1;

{$APPTYPE CONSOLE}

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

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

Score : 100 points

Problem Statement

Snuke is buying a bicycle.
The bicycle of his choice does not come with a bell, so he has to buy one separately.

He has very high awareness of safety, and decides to buy two bells, one for each hand.

The store sells three kinds of bells for the price of a, b and c yen (the currency of Japan), respectively.
Find the minimum total price of two different bells.

Constraints

1 \leq a,b,c \leq 10000

a, b and c are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

Print the minimum total price of two different bells.

Sample Input 1

700 600 780

Sample Output 1

1300

Buying a 700-yen bell and a 600-yen bell costs 1300 yen.

Buying a 700-yen bell and a 780-yen bell costs 1480 yen.

Buying a 600-yen bell and a 780-yen bell costs 1380 yen.

The minimum among these is 1300 yen.

Sample Input 2

10000 10000 10000

Sample Output 2

20000

Buying any two bells costs 20000 yen.


program Project1;

{$APPTYPE CONSOLE}

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

    for i := (Length(s)div 2 - 1) downto 1 do begin
      if Copy(s,1,i)=Copy(s,i+1,i) then begin
        Writeln(i*2);
        break;//exit;
      end;
    end;
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

We will call a string that can be obtained by concatenating two equal strings an even string.
For example, xyzxyz and aaaaaa are even, while ababab and xyzxy are not.

You are given an even string S consisting of lowercase English letters.
Find the length of the longest even string that can be obtained by deleting one or more characters from the end of S.
It is guaranteed that such a non-empty string exists for a given input.

Constraints

2 \leq |S| \leq 200

S is an even string consisting of lowercase English letters.

There exists a non-empty even string that can be obtained by deleting one or more characters from the end of S.

Input

Input is given from Standard Input in the following format:

S

Output

Print the length of the longest even string that can be obtained.

Sample Input 1

abaababaab

Sample Output 1

6

abaababaab itself is even, but we need to delete at least one character.

abaababaa is not even.

abaababa is not even.

abaabab is not even.

abaaba is even. Thus, we should print its length, 6.

Sample Input 2

xxxx

Sample Output 2

2

xxx is not even.

xx is even.

Sample Input 3

abcabcabcabc

Sample Output 3

6

The longest even string that can be obtained is abcabc, whose length is 6.

Sample Input 4

akasakaakasakasakaakas

Sample Output 4

14

The longest even string that can be obtained is akasakaakasaka, whose length is 14.


var a:array of longint;
    n,i:longint;
begin
  readln(n);
  setlength(a,n+1);
  for i:=1 to n do
    read(a[i]);
  i:=n;
  while i>0 do
  begin
    write(a[i],' ');
    dec(i,2);
  end;
  i:=(n and 1)+1;
  while i<=n do
  begin
    write(a[i],' ');
    inc(i,2);
  end;
end.

Score : 300 points

Problem Statement

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

The i-th operation is as follows:

Append a_i to the end of b.

Reverse the order of the elements in b.

Find the sequence b obtained after these n operations.

Constraints

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

0 \leq a_i \leq 10^9

n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

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

Sample Input 1

4
1 2 3 4

Sample Output 1

4 2 1 3

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

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

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

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

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

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

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

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

Thus, the answer is 4 2 1 3.

Sample Input 2

3
1 2 3

Sample Output 2

3 1 2

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

Sample Input 3

1
1000000000

Sample Output 3

1000000000

Sample Input 4

6
0 6 7 6 7 0

Sample Output 4

0 6 6 0 7 7


var a:array[0..200000]of longint;
    n,i:longint;
begin
  readln(n);
  for i:=1 to n do
    read(a[i]);
  i:=n;
  while i>0 do
  begin
    write(a[i],' ');
    dec(i,2);
  end;
  i:=(n and 1)+1;
  while i<=n do
  begin
    write(a[i],' ');
    inc(i,2);
  end;
end.


program ec12;
var
	a:array[1..200001] of longint;
	queue:array[-100001..100001] of longint;
	n,m,i,j,dis,head,tail:longint;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	head:=1;
	tail:=1;
	dis:=1;
	queue[head]:=a[1];
	for i:=3 to n*2 do
	begin
		if i mod 2=1 then
		begin
			if dis=1 then
			begin
				inc(tail);
				queue[tail]:=a[i div 2+1];
			end
			else
			begin
				dec(head);
				queue[head]:=a[i div 2+1];
			end;
		end
		else
		dis:=3-dis;
	end;
	if dis=1 then
	begin
		for i:=head to tail do
		write(queue[i],' ');
	end
	else
	begin
		for i:=tail downto head do
		write(queue[i],' ');
	end;
end.


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


program ec12;
var
	a:array[1..200001] of longint;
	s,s1:ansistring;
	n,m,i,j,dis:longint;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	s:='';
	dis:=1;
	for i:=1 to n*2 do
	begin
		if i mod 2=1 then
		begin
			if dis=1 then
			begin
				str(a[i div 2+1],s1);
				s:=s+s1;
			end
			else
			begin
				str(a[i div 2+1],s1);
				s:=s1+s;
			end;
		end
		else
		dis:=3-dis;
	end;
	if dis=1 then
	begin
		for i:=1 to length(s) do
		write(s[i],' ');
	end
	else
	begin
		for i:=length(s) downto 1 do
		write(s[i],' ');
	end;
end.


const fi='trained.inp';
      fo='trained.out';
var a,dau:array[0..100005] of longint;
    n,d:longint;

procedure mo;
begin
    assign(input,fi);
    reset(input);
    assign(output,fo);
    rewrite(output);
end;

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

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

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

begin

    nhap;
    xuly;
    xuat;
end.

Score : 100 points

Problem Statement

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

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

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

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

Constraints

1 ≤ X,A,B ≤ 10^9

Input

Input is given from Standard Input in the following format:

X A B

Output

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

Sample Input 1

4 3 6

Sample Output 1

safe

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

Sample Input 2

6 5 1

Sample Output 2

delicious

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

Sample Input 3

3 7 12

Sample Output 3

dangerous

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


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

Score : 200 points

Problem Statement

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

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

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

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

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

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

Sample Input 1

3
3
1
2

Sample Output 1

2

Press Button 1, then Button 3.

Sample Input 2

4
3
4
1
2

Sample Output 2

-1

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

Sample Input 3

5
3
3
4
2
4

Sample Output 3

3


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


program Project1;

{$APPTYPE CONSOLE}

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

    soeji:=1;
    for i := 1 to n do begin
      if a[soeji]=2 then begin
        Writeln(i);
        Readln;
        exit;
      end;
      soeji:=a[soeji];
    end;

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


type mang=array[0..1000005] of longint;
     mang1=array[0..1000005] of boolean;
var a:mang;
    kt:mang1;
    dem,n,sang:longint;
procedure nhap;
var i:longint;
begin
   read(n);
   for i:=1 to n do
      read(a[i]);
end;

procedure xuly;
begin
   dem:=0;
   fillchar(kt,sizeof(kt),true);
   sang:=1;
      while (sang<>2) and (kt[sang]=true) do
         begin
         kt[sang]:=false;
         sang:=a[sang];
         inc(dem);
         end;
end;

procedure xuat;
begin
   if sang=2 then write(dem)
      else write('-1');
end;

begin

   nhap;
   xuly;
   xuat;
end.


const MaxN=round(1e5)+10;
var A:array[0..MaxN] of longint;
    n,res:longint;

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

procedure xuli;
var x:longint;
begin
    x:=a[1];
    repeat
        x:=a[x];
        inc(res);
        if x=2 then
        begin
            writeln(res+1);
            halt;
        end;
    until res>n;
end;
procedure xuat;
begin
    writeln(-1);

end;

begin
    nhap;
    xuli;
    xuat;
end.


program reconciled;
uses math;
const
  md: int64 = 1000000007;
  x50000: int64 = 765302181;
  x75000: int64 = 393193551;
var
  n, m, x, i: int64;
begin
  read(n, m);
  if abs(n-m) > 1 then
  begin
    writeln(0);
    halt(0);
  end;
  if min(n, m) < 50000 then
  begin
    x := ifthen(n = m, 2, 1);
    for i := 1 to min(n, m) do
      x := ((i*i mod md)*x mod md);
    for i := min(n, m)+1 to max(n, m) do
      x := (i*x mod md);
    writeln(x);
    halt(0);
  end;
  if min(n, m) < 75000 then
  begin
    x := x50000*ifthen(n = m, 2, 1);
    for i := 50001 to min(n, m) do
      x := ((i*i mod md)*x mod md);
    for i := min(n, m)+1 to max(n, m) do
      x := (i*x mod md);
    writeln(x);
    halt(0);
  end;
  x := x75000*ifthen(n = m, 2, 1) mod md;
  for i := 75001 to min(n, m) do
    x := ((i*i mod md)*x mod md);
  for i := min(n, m)+1 to max(n, m) do
    x := (i*x mod md);
  writeln(x);
end.

Score : 300 points

Problem Statement

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

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

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

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

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

Sample Input 1

2 2

Sample Output 1

8

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

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477


program reconciled;
uses math;
const
  md: int64 = 1000000007;
var
  n, m, x, i: int64;
begin
  read(n, m);
  if abs(n-m) > 1 then
  begin
    writeln(0);
    halt(0);
  end;
  x := ifthen(n = m, 2, 1);
  for i := 1 to min(n, m) do
    x := ((i*i mod md)*x mod md);
  for i := min(n, m)+1 to max(n, m) do
    x := (i*x mod md);
  writeln(x);
end.


type mang=array[0..1000005] of longint;
var x,y,trace,stack:mang;
    n,tong:longint;

procedure nhap;
var i:longint;
begin
   read(n);
   for i:=1 to n do
      read(x[i],y[i]);

end;

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

procedure xuly;
var i,dem,u,v,mmin,the,j,top:longint;
begin
   fillchar(trace,sizeof(trace),0);
   trace[1]:=1;
   tong:=0;
   dem:=1;
   top:=1;
   stack[top]:=1;

      while dem<n do
         begin
         Mmin:=maxlongint;
         for j:=1 to top do
         begin
         u:=stack[j];
            for i:=1 to n do
                if trace[i]=0 then
                  begin
                  the:=min(abs(x[i]-x[u]),abs(y[i]-y[u]));

                  if mmin>the then
                     begin
                     mmin:=the;
                     v:=i;
                     end;
                  end;
         end;
         tong:=tong+mmin;

         trace[v]:=1;
         inc(top);
         stack[top]:=v;
         inc(dem);
         end;
end;

procedure xuat;
begin
   write(tong);
end;

begin

   nhap;
   xuly;
   xuat;

end.

Score : 500 points

Problem Statement

There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1

3
1 5
3 9
7 8

Sample Output 1

3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2

6
8 3
4 9
12 19
18 1
13 5
7 6

Sample Output 2

8


var
	n,m,i:Longint;
	ans,o:int64;
begin
	read(n,m);
	ans:=1;
	o:=1000000007;
	for i:=1 to n do ans:=ans*i mod o;
	for i:=1 to m do ans:=ans*i mod o;
	if n=m then ans:=ans*2 mod o else if(m<n-1)or(n+1<m)then ans:=0;
	write(ans);
end.


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


var
        i,n,m,min,r1,r2,s:longint;
        x,y,z,a,b,id,lab:array [0..10000000] of longint;
procedure swap(var a,b:longint);
        var
                t:longint;
        begin
        t:=a;
        a:=b;
        b:=t;
        end;
procedure sort(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]<x do
            inc(i);
           while x<a[j] do
            dec(j);
           if not(i>j) then
             begin
                swap(a[i],a[j]);
                swap(b[i],b[j]);
                swap(id[i],id[j]);
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
procedure sort1(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=b[(l+r) div 2];
         repeat
           while b[i]<x do
            inc(i);
           while x<b[j] do
            dec(j);
           if not(i>j) then
             begin
                swap(a[i],a[j]);
                swap(b[i],b[j]);
                swap(id[i],id[j]);
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort1(l,j);
         if i<r then
           sort1(i,r);
      end;
procedure sort2(l,r: longint);
      var
         i,j,x1: longint;
      begin
         i:=l;
         j:=r;
         x1:=z[(l+r) div 2];
         repeat
           while z[i]<x1 do
            inc(i);
           while x1<z[j] do
            dec(j);
           if not(i>j) then
             begin
                swap(x[i],x[j]);
                swap(y[i],y[j]);
                swap(z[i],z[j]);
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort2(l,j);
         if i<r then
           sort2(i,r);
      end;
function getroot(v:longint):longint;
        begin
        while lab[v]>0 do v:=lab[v];
        getroot:=v;
        end;
procedure union(r1,r2:longint);
        var
                x:longint;
        begin
        x:=lab[r1]+lab[r2];
        if lab[r1]>lab[r2] then
                begin
                lab[r1]:=r2;
                lab[r2]:=x;
                end
        else
                begin
                lab[r1]:=x;
                lab[r2]:=r1;
                end;
        end;
begin
readln(n);
for i:=1 to n do
        begin
        readln(a[i],b[i]);
        id[i]:=i;
        end;
sort(1,n);
for i:=1 to n-1 do
        begin
        min:=a[i+1]-a[i];
        inc(m);
        x[m]:=id[i];
        y[m]:=id[i+1];
        z[m]:=min;
        end;
sort1(1,n);
for i:=1 to n-1 do
        begin
        min:=b[i+1]-b[i];
        inc(m);
        x[m]:=id[i];
        y[m]:=id[i+1];
        z[m]:=min;
        end;
for i:=1 to n do
        lab[i]:=-1;
sort2(1,m);
for i:=1 to m do
        begin
        r1:=getroot(x[i]);
        r2:=getroot(y[i]);
        if r1<>r2 then
                begin
                s:=s+z[i];
                union(r1,r2);
                end;
        end;
writeln(s);
end.


program Project1;

{$APPTYPE CONSOLE}

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

    if StrToInt(IntToStr(r)+IntToStr(g)+IntToStr(b)) mod 4 = 0 then
      Writeln('YES')
    else
      Writeln('NO');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

AtCoDeer has three cards, one red, one green and one blue.

An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.

We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.

Is this integer a multiple of 4?

Constraints

1 ≤ r, g, b ≤ 9

Input

Input is given from Standard Input in the following format:

r g b

Output

If the three-digit integer is a multiple of 4, print YES (case-sensitive); otherwise, print NO.

Sample Input 1

4 3 2

Sample Output 1

YES

432 is a multiple of 4, and thus YES should be printed.

Sample Input 2

2 3 4

Sample Output 2

NO

234 is not a multiple of 4, and thus NO should be printed.


var a:array[0..100000]of longint;
    n,i:longint;
procedure quickly_sort(var a:array of longint;l,r:longint);inline;
var i,j,x,y:longint;
begin
  while true do
  begin
    if r<l+74 then
      exit;
    i:=l;j:=r;
    x:=a[(l+r)>>1];
    repeat
      while a[i]<x do inc(i);
      while x<a[j] do dec(j);
      if not(i>j) then
      begin
        y:=a[i];a[i]:=a[j];
        a[j]:=y;inc(i);dec(j);
      end;
    until i>j;
    if l<j then quickly_sort(a,l,j);
    if not(i<r) then
      exit;
    l:=i;
  end;
end;
procedure sort(var a:array of longint;l,r:longint);inline;
var i,j,t:longint;
begin
  if r>l then
  begin
    quickly_sort(a,l,r);
    i:=l;
    for i:=l to r do
    begin
      j:=i;
      while j>pred(l) do
        if a[j]<a[pred(j)] then
        begin
          t:=a[pred(j)];a[pred(j)]:=a[j];a[j]:=t;
          dec(j);
        end
        else
          break;
    end;
  end;
end;
begin
  readln(n);
  for i:=1 to n do
    read(a[i]);
  sort(a,1,n);
  write(a[n]-a[1]);
end.

Score : 200 points

Problem Statement

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

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

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

Constraints

1 ≤ N ≤ 100

0 ≤ a_i ≤ 1000

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum distance to be traveled.

Sample Input 1

4
2 3 7 9

Sample Output 1

7

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

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

Sample Input 2

8
3 1 4 1 5 9 2 6

Sample Output 2

8

There may be more than one house at a position.


//const fi='traveling.inp';
//      fo='traveling.out';
var n,s:longint;
    a:array[0..10000] of longint;
//procedure mo;
//begin
 //       assign(input,fi);
 //       reset(input);
 //       assign(output,fo);
 //       rewrite(output);
//end;

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

procedure swap(a,b:longint);
var c:longint;
begin
        c:=a;
        a:=b;
        b:=c;
end;

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

procedure xuly;
var i:longint;
begin
        for i:=2 to n do
                s:=s+a[i]-a[i-1];
        writeln(s);
end;

begin
        //mo;
        nhap;
        sort(1,n);
        xuly;
end.


Program Insertion;
const   finput='Insertion.inp';
        foutput='Insertion.out';
var     n,tmp,k,j,i:longint;
        s,st:string;
begin
    //assign(input,finput);reset(input);
    //assign(output,foutput);rewrite(output);
    readln(n);
    readln(s);
    st:='(';
    for i:=1 to length(s) do
        if s[i]=')' then
            begin
            j:=i;
            tmp:=0;
            repeat
                case s[j] of
                    ')':dec(tmp);
                    '(':inc(tmp);
                end;
                dec(j);
            until (j=k)or(tmp=0);
            if tmp<>0 then insert(st,s,k+1);
            k:=i;
            end;
    write(s);
    //close(input);close(output);
end.

Score : 400 points

Problem Statement

You are given a string S of length N consisting of ( and ). Your task is to insert some number of ( and ) into S to obtain a correct bracket sequence.

Here, a correct bracket sequence is defined as follows:

() is a correct bracket sequence.

If X is a correct bracket sequence, the concatenation of (, X and ) in this order is also a correct bracket sequence.

If X and Y are correct bracket sequences, the concatenation of X and Y in this order is also a correct bracket sequence.

Every correct bracket sequence can be derived from the rules above.

Find the shortest correct bracket sequence that can be obtained. If there is more than one such sequence, find the lexicographically smallest one.

Constraints

The length of S is N.

1 ≤ N ≤ 100

S consists of ( and ).

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the lexicographically smallest string among the shortest correct bracket sequences that can be obtained by inserting some number of ( and ) into S.

Sample Input 1

3
())

Sample Output 1

(())

Sample Input 2

6
)))())

Sample Output 2

(((()))())

Sample Input 3

8
))))((((

Sample Output 3

(((())))(((())))


program Project1;

{$APPTYPE CONSOLE}

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

    if a+b>=10 then
      Writeln('error')
    else
      Writeln(a+b);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

You are given two integers A and B as the input. Output the value of A + B.

However, if A + B is 10 or greater, output error instead.

Constraints

A and B are integers.

1 ≤ A, B ≤ 9

Input

Input is given from Standard Input in the following format:

A B

Output

If A + B is 10 or greater, print the string error (case-sensitive); otherwise, print the value of A + B.

Sample Input 1

6 3

Sample Output 1

9

Sample Input 2

6 4

Sample Output 2

error


program Project1;

{$APPTYPE CONSOLE}

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

    for i := 1 to Length(s) do begin
      min := i;
      for j := i+1 to Length(s) do begin
        if s[j] < s[min] then
        min := j;
      end;
      sw:=s[i];
      s[i]:=s[min];
      s[min]:=sw;
    end;

    for i := 1 to Length(s) - 1 do
      if s[i]=s[i+1] then begin
        Writeln('no');
        Readln;
        exit;
      end;

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

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters. Determine whether all the characters in S are different.

Constraints

2 ≤ |S| ≤ 26, where |S| denotes the length of S.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If all the characters in S are different, print yes (case-sensitive); otherwise, print no.

Sample Input 1

uncopyrightable

Sample Output 1

yes

Sample Input 2

different

Sample Output 2

no

Sample Input 3

no

Sample Output 3

yes


program ec12;
var
	n,m,i,j,sum:longint;
	t:array[0..101] of longint;
	a:array[1..101] of longint;
begin
	fillchar(t,sizeof(t),0);
	readln(n);
	sum:=0;
	for i:=1 to n do
	begin
		readln(a[i]);
		inc(t[a[i]]);
		sum:=sum+a[i];
	end;
	if sum mod 10>0 then
	begin
		writeln(sum);
		halt;
	end;
	for i:=1 to 100 do
	begin
		if (t[i]>0) and (i mod 10>0) then
		begin
			writeln(sum-i);
			halt;
		end;
	end;
	writeln(0);
end.

Score : 300 points

Problem Statement

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

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

Constraints

All input values are integers.

1 ≤ N ≤ 100

1 ≤ s_i ≤ 100

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

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

Sample Input 1

3
5
10
15

Sample Output 1

25

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

Sample Input 2

3
10
10
15

Sample Output 2

35

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

Sample Input 3

3
10
20
30

Sample Output 3

0

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


var
   n: integer;
   a: integer;
   b: integer;
   h: array of integer;
   i: integer;
   l: integer;
   r: integer;
   m: integer;
   diff: integer;
   cnt: int64;
begin
   readln(n, a, b);
   setlength(h, n);
   for i := 0 to n - 1 do begin
      readln(h[i]);
   end;
   l := 0;
   r := 1000000000;
   while r - l > 1 do begin
      cnt := 0;
      m := (r + l) div 2;
      for i := 0 to n - 1 do begin
         diff := h[i] - (b * m);
         if diff > 0 then begin
            cnt := cnt + (diff div (a - b));
            if diff mod (a - b) > 0 then begin
               inc(cnt);
            end;
         end;
      end;
      if cnt > m then begin
         l := m;
      end
      else begin
         r := m;
      end;
   end;
   writeln(r);
end.

Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter N monsters. Each monster has a parameter called health, and the health of the i-th monster is h_i at the moment of encounter. A monster will vanish immediately when its health drops to 0 or below.

Fortunately, you are a skilled magician, capable of causing explosions that damage monsters. In one explosion, you can damage monsters as follows:

Select an alive monster, and cause an explosion centered at that monster. The health of the monster at the center of the explosion will decrease by A, and the health of each of the other monsters will decrease by B. Here, A and B are predetermined parameters, and A > B holds.

At least how many explosions do you need to cause in order to vanish all the monsters?

Constraints

All input values are integers.

1 ≤ N ≤ 10^5

1 ≤ B < A ≤ 10^9

1 ≤ h_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N A B
h_1
h_2
:
h_N

Output

Print the minimum number of explosions that needs to be caused in order to vanish all the monsters.

Sample Input 1

4 5 3
8
7
4
2

Sample Output 1

2

You can vanish all the monsters in two explosion, as follows:

First, cause an explosion centered at the monster with 8 health. The healths of the four monsters become 3, 4, 1 and -1, respectively, and the last monster vanishes.

Second, cause an explosion centered at the monster with 4 health remaining. The healths of the three remaining monsters become 0, -1 and -2, respectively, and all the monsters are now vanished.

Sample Input 2

2 10 4
20
20

Sample Output 2

4

You need to cause two explosions centered at each monster, for a total of four.

Sample Input 3

5 2 1
900000000
900000000
1000000000
1000000000
1000000000

Sample Output 3

800000000


program j01;
const maxn=100086;
var h:array[0..maxn]of int64;
    n,i:longint;
    a,b,l,r,mid:int64;

function jud(k:int64):boolean;
var i:longint;tmp,kk,cnt:int64;
begin
  cnt:=0;
  for i:=1 to n do
  begin
    tmp:=h[i]-k*b;if tmp<=0 then continue;
    kk:=tmp div a;
    if kk*a=tmp then inc(cnt,kk) else inc(cnt,kk+1);
  end;
  exit(cnt<=k);
end;

begin
  readln(n,a,b);a:=a-b;
  for i:=1 to n do readln(h[i]);
  l:=1;r:=1000000000;
  while l<r do
  begin
    mid:=(l+r)div 2;
    if jud(mid)then r:=mid else l:=mid+1;
  end;
  writeln(l);
end.


program j01;
const maxn=100086;
var h:array[0..maxn]of int64;
    n,i:longint;
    a,b,l,r,mid:int64;

function jud(k:int64):boolean;
var i:longint;tmp,kk,cnt:int64;
begin
  cnt:=0;
  for i:=1 to n do
  begin
    tmp:=h[i]-k*b;if tmp<=0 then continue;
    kk:=tmp div a;
    if kk=tmp then inc(cnt,kk) else inc(cnt,kk+1);
  end;
  exit(cnt<=k);
end;

begin
  readln(n,a,b);a:=a-b;
  for i:=1 to n do readln(h[i]);
  l:=1;r:=1000000000;
  while l<r do
  begin
    mid:=(l+r)div 2;
    if jud(mid)then r:=mid else l:=mid+1;
  end;
  writeln(l);
end.


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

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

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

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

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

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

Score : 600 points

Problem Statement

You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K.

a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K?

Constraints

All input values are integers.

1 ≤ N ≤ 2 \times 10^5

1 ≤ K ≤ 10^9

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N K
a_1
a_2
:
a_N

Output

Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K.

Sample Input 1

3 6
7
5
7

Sample Output 1

5

All the non-empty contiguous subsequences of a are listed below:

{a_1} = {7}

{a_1, a_2} = {7, 5}

{a_1, a_2, a_3} = {7, 5, 7}

{a_2} = {5}

{a_2, a_3} = {5, 7}

{a_3} = {7}

Their means are 7, 6, 19/3, 5, 6 and 7, respectively, and five among them are 6 or greater. Note that {a_1} and {a_3} are indistinguishable by the values of their elements, but we count them individually.

Sample Input 2

1 2
1

Sample Output 2

0

Sample Input 3

7 26
10
20
30
40
30
20
10

Sample Output 3

13


program Project1;

{$APPTYPE CONSOLE}

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

    if a[x]=a[y] then
      Writeln('Yes')
    else
      Writeln('No');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

Based on some criterion, Snuke divided the integers from 1 through 12 into three groups as shown in the figure below.
Given two integers x and y (1 ≤ x < y ≤ 12), determine whether they belong to the same group.

Constraints

x and y are integers.

1 ≤ x < y ≤ 12

Input

Input is given from Standard Input in the following format:

x y

Output

If x and y belong to the same group, print Yes; otherwise, print No.

Sample Input 1

1 3

Sample Output 1

Yes

Sample Input 2

2 4

Sample Output 2

No


program Project1;

{$APPTYPE CONSOLE}

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

    SetLength(a,h);

    {for i := 0 to w+1 do begin
      a[0][i]:='#';
      a[h+1][i]:='#';
    end;
    for i := 0 to h - 1 do begin
      a[i][0]:='#';
      a[i][w+1]:='#';
    end;}

    for i := 0 to h - 1 do
      Readln(a[i]);

    for i := 0 to w+1 do
      Write('#');
    Writeln;
    for i := 0 to h-1 do begin
      Write('#');
      Write(a[i]);
      Writeln('#');
    end;
    for i := 0 to w+1 do
      Write('#');
    Writeln;
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

You are given a image with a height of H pixels and a width of W pixels.
Each pixel is represented by a lowercase English letter.
The pixel at the i-th row from the top and j-th column from the left is a_{ij}.

Put a box around this image and output the result. The box should consist of # and have a thickness of 1.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

H W
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}

Output

Print the image surrounded by a box that consists of # and has a thickness of 1.

Sample Input 1

2 3
abc
arc

Sample Output 1

#####
#abc#
#arc#
#####

Sample Input 2

1 1
z

Sample Output 2

###
#z#
###


const   fi      ='';
        fo      ='';

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

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

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

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

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

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

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

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

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

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

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

Score : 500 points

Problem Statement

Let N be a positive integer.

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

Find the maximum possible score of a'.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Partial Score

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

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

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

Sample Input 2

1
1 2 3

Sample Output 2

-1

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

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

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


uses math;
var  heap,heap1:array[0..500000] of longint;
     res:int64;
     n,nheap:longint;
     f,g,a:array[0..500000] of int64;
procedure nhap;
var i:longint;
begin
 read(n);
 for i:=1 to 3*n do read(a[i]);
end;
procedure doicho(var x,y:longint);
var tg:longint;
begin
 tg:=x;
 x:=y;
 y:=tg;
end;
procedure upheap(i:longint);
var j:longint;
begin
 j:=i div 2;
 if (i=1) or (a[heap[i]]>a[heap[j]]) then exit;
 doicho(heap[i],heap[j]);
 upheap(j);
end;
procedure downheap(i:longint);
var j:longint;
begin
 j:=2*i;
 if j>nheap then exit;
 if (j<nheap) and (A[heap[j]]>a[heap[j+1]]) then inc(j);
 if a[heap[i]]>a[heap[j]] then
  begin
   doicho(heap[i],heap[j]);
   downheap(j);
  end;
end;
procedure push(x:longint);
begin
 inc(nheap);
 heap[nheap]:=x;
 upheap(nheap);
end;
function pop:longint;
begin
 pop:=heap[1];
 heap[1]:=heap[nheap];
 dec(nheap);
 downheap(1);
end;
procedure upheap1(i:longint);
var j:longint;
begin
 j:=i div 2;
 if (i=1) or (a[heap1[i]]<a[heap1[j]]) then exit;
 doicho(heap1[i],heap1[j]);
 upheap1(j);
end;
procedure downheap1(i:longint);
var j:longint;
begin
 j:=2*i;
 if j>nheap then exit;
 if (j<nheap) and (A[heap1[j]]<a[heap1[j+1]]) then inc(j);
 if a[heap1[i]]<a[heap1[j]] then
  begin
   doicho(heap1[i],heap1[j]);
   downheap1(j);
  end;
end;
procedure push1(x:longint);
begin
 inc(nheap);
 heap1[nheap]:=x;
 upheap1(nheap);
end;
function pop1:longint;
begin
 pop1:=heap1[1];
 heap1[1]:=heap1[nheap];
 dec(nheap);
 downheap1(1);
end;
procedure xuli;
var i,k:longint;
begin
 for i:=1 to n do
  begin
   push(i);
   f[i]:=f[i-1]+a[i];
  end;
 for i:=n+1 to 3*n do
  begin
   k:=pop;
   if a[i]>a[k] then
    begin
     f[i]:=f[i-1]-a[k]+a[i];
     push(i);
    end
   else
    begin
     f[i]:=f[i-1];
     push(k);
    end;
  end;
 nheap:=0;
 for i:=3*n downto 2*n+1 do
  begin
   push1(i);
   g[i]:=g[i+1]+a[i];
  end;
 for i:=2*n downto 1 do
  begin
   k:=pop1;
   if a[i]<a[k] then
    begin
     g[i]:=g[i+1]-a[k]+a[i];
     push1(i);
    end
   else
    begin
     g[i]:=g[i+1];
     push1(k);
    end;
  end;
 res:=-high(int64);
 for i:=n to 2*n do res:=max(Res,f[i]-g[i+1]);
 write(REs);
end;
begin
// assign(input,'b.inp');reset(input);
// assign(output,'b.out');rewrite(output);
 nhap;
 xuli;
// close(input); close(output);
end.


const   fi='';//B.inp';
        fo='';//B.out';
var     f:text;
        i,j,n,top,top1:longint;
        kq:int64;
        heap,heap1,a,l,g:array[0..1000000] of longint;
(*===================================*)
procedure inp;
Begin
assign(f,fi);
reset(f);
readln(f,n);
for i:=1 to 3*n do read(f,a[i]);
close(f);
End;
(*===================================*)
procedure swap(var a,b:longint);
var y:longint;
Begin
y:=a;
a:=b;
b:=y;
End;
(*===================================*)
procedure upheap(i:longint);
Begin
if (i=1) or (heap[i]<heap[i div 2]) then exit;
swap(heap[i],heap[i div 2]);
upheap(i div 2);
End;
(*===================================*)
procedure downheap(i:longint);
var j:longint;
Begin
j:=i*2;
if j>top then exit;
if (j<top) and (heap[j]<heap[j+1]) then inc(j);
if heap[i]<heap[j] then
 begin
  swap(heap[i],heap[j]);
  downheap(j);
 end;
End;
(*===================================*)
procedure push(x:longint);
Begin
inc(top);
heap[top]:=x;
upheap(top);
End;
(*===================================*)
function pop:longint;
Begin
pop:=heap[1];
heap[1]:=heap[top];
dec(top);
downheap(1);
End;
(*===================================*)
procedure upheap1(i:longint);
Begin
if (i=1) or (heap1[i]>heap1[i div 2]) then exit;
swap(heap1[i],heap1[i div 2]);
upheap1(i div 2);
End;
(*===================================*)
procedure downheap1(i:longint);
var j:longint;
Begin
j:=i*2;
if j>top1 then exit;
if (j<top1) and (heap1[j]>heap1[j+1]) then inc(j);
if heap1[i]>heap1[j] then
 begin
  swap(heap1[i],heap1[j]);
  downheap1(j);
 end;
End;
(*===================================*)
procedure push1(x:longint);
Begin
inc(top1);
heap1[top1]:=x;
upheap1(top1);
End;
(*===================================*)
function pop1:longint;
Begin
pop1:=heap1[1];
heap1[1]:=heap1[top1];
dec(top1);
downheap1(1);
End;
(*===============================*)
procedure trong;
Begin
for i:=1 to n do begin l[i]:=l[i-1]+a[i]; push1(a[i]); end;
for i:=n+1 to 3*n do
  begin
   l[i]:=l[i-1]+a[i];
   push1(a[i]);
   l[i]:=l[i]-pop1;
  end;

for i:=3*n downto 2*n+1 do begin g[i]:=g[i+1]+a[i]; push(a[i]); end;
for i:=2*n downto 1 do
 begin
  g[i]:=g[i+1]+a[i];
  push(a[i]);
  g[i]:=g[i]-pop;
 end;
End;
(*===============================*)
BEGIN
inp;
assign(f,fo);
rewrite(f);
trong;
kq:=-maxlongint*100000;
for i:=n to 2*n do
 if l[i]-g[i+1]>kq then kq:=l[i]-g[i+1];
writeln(f,kq);
close(f);
END.


const fi='';
      fo='';
      oo=trunc(1e18);
      maxn=trunc(1e5);
var   n:longint;
      sol:int64;
      nheapmin,nheapmax:longint;
      heapmax,heapmin:array[0..2*maxn]of longint;
      a:array[0..3*maxn]of longint;
      l,r:array[0..3*maxn]of int64;
function min(i,j:int64):int64; begin if i<j then exit(i); exit(j); end;
function max(i,j:int64):int64; begin if i>j then exit(i); exit(j); end;
procedure swap(var a,b:Longint);
var temp:longint;
begin
      temp:=a;
      a:=b;
      b:=temp;
end;
procedure upmax(i:longint);
begin
      if (i=1)or(heapmax[i div 2]>=heapmax[i]) then exit
      else begin
                  swap(heapmax[i],heapmax[i div 2]);
                  upmax(i div 2);
           end;
end;
procedure upmin(i:longint);
begin
      if (i=1)or(heapmin[i div 2]<=heapmin[i]) then exit
      else begin
                  swap(heapmin[i div 2],heapmin[i]);
                  upmin(i div 2);
           end;
end;
procedure downmax(i:longint);
var gt:longint;
begin
      gt:=i*2;
      if gt>nheapmax then exit;
      if (gt+1<=nheapmax)and(heapmax[gt]<heapmax[gt+1]) then inc(gt);
      if heapmax[i]>=heapmax[gt] then exit;
      begin
                    swap(heapmax[gt],heapmax[i]);
                    downmax(gt);
      end;
end;
procedure downmin(i:longint);
var gt:longint;
begin
      gt:=i*2;
      if gt>nheapmin then exit;
      if (gt+1<=nheapmin)and(heapmin[gt]>heapmin[gt+1]) then inc(gt);
      if heapmin[i]<=heapmin[gt] then exit;
      begin
                  swap(heapmin[gt],heapmin[i]);
                  downmin(gt);
      end;
end;
procedure pushmin(x:longint);
begin
       inc(nheapmin);
       heapmin[nheapmin]:=x;
       upmin(nheapmin);
end;
procedure pushmax(x:longint);
begin
      inc(nheapmax);
      heapmax[nheapmax]:=x;
      upmax(nheapmax);
end;
procedure popmin(x:longint);
begin
      heapmin[1]:=x;
      downmin(1);
end;
procedure popmax(x:longint);
begin
       heapmax[1]:=x;
       downmax(1);
end;
procedure inp;
var i:longint;
begin
      read(n);
      for i:=1 to 3*n do read(a[i]);
end;
procedure main;
var i,j:longint;
    res:int64;
    nmax,nmin:longint;
begin
      res:=0;
      for i:=1 to n do
      begin
          res:=res+a[i];
          pushmin(a[i]);
      end;
      l[n]:=res;
      for i:=n+1 to 2*n do
      begin
          if a[i]>heapmin[1] then
          begin
             res:=res-heapmin[1]+a[i];
             popmin(a[i]);
          end;
          l[i]:=res;
      end;
      res:=0;
      for i:=3*n downto 2*n+1 do
      begin
          res:=res+a[i];
          pushmax(a[i]);
      end;
      r[2*n+1]:=res;
      for i:=2*n downto n+1 do
      begin
          if a[i]<heapmax[1] then
          begin
             res:=res-heapmax[1]+a[i];
             popmax(a[i]);
          end;
          r[i]:=res;
      end;
      sol:=-oo;
      for i:=n to 2*n do sol:=max(sol,l[i]-r[i+1]);
      write(sol);
end;
begin
      assign(input,fi);
      reset(input);
      assign(output,fo);
      rewrite(output);
      inp;
      main;
      close(output);
end.


program Project1;

{$APPTYPE CONSOLE}

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

    for i := 1 to n do
      Writeln(r[i]);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

There are N cities and M roads.
The i-th road (1≤i≤M) connects two cities a_i and b_i (1≤a_i,b_i≤N) bidirectionally.
There may be more than one road that connects the same pair of two cities.
For each city, how many roads are connected to the city?

Constraints

2≤N,M≤50

1≤a_i,b_i≤N

a_i ≠ b_i

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_M b_M

Output

Print the answer in N lines.
In the i-th line (1≤i≤N), print the number of roads connected to city i.

Sample Input 1

4 3
1 2
2 3
1 4

Sample Output 1

2
2
1
1

City 1 is connected to the 1-st and 3-rd roads.

City 2 is connected to the 1-st and 2-nd roads.

City 3 is connected to the 2-nd road.

City 4 is connected to the 3-rd road.

Sample Input 2

2 5
1 2
2 1
1 2
2 1
1 2

Sample Output 2

5
5

Sample Input 3

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

Sample Output 3

3
3
2
2
2
1
1
2


program canh_toan;
uses math;
const minn=0;
      maxn=trunc(1e5)+5;
var n:longint;
    k:int64;
    a,b:array [minn..maxn] of int64;
procedure sort(l,r:int64);
var i,j,k,tg:int64;
begin
i:=l;j:=r;tg:=a[random(r-l+1)+l];
repeat
while (a[i]<tg) do inc(i);
while (tg<a[j]) do dec(j);
if not(i>j) then
   begin
     k:=a[i];a[i]:=a[j];a[j]:=k;
     k:=b[i];b[i]:=b[j];b[j]:=k;
     inc(i);
     dec(j);
   end;
until i>j;
if (l<j) then sort(l,j);
if (i<r) then sort(i,r);
end;
procedure main();
var i:longint;
begin
readln(n,k);
for i:=1 to n do readln(a[i],b[i]);
sort(1,n);
b[0]:=0;
for i:=1 to n do b[i]:=b[i]+b[i-1];
i:=1;
while (b[i]<k) do inc(i);
write(a[i]);
end;
BEGIN
  main();
END.

Score : 300 points

Problem Statement

There is an empty array.
The following N operations will be performed to insert integers into the array.
In the i-th operation (1≤i≤N), b_i copies of an integer a_i are inserted into the array.
Find the K-th smallest integer in the array after the N operations.
For example, the 4-th smallest integer in the array \{1,2,2,3,3,3\} is 3.

Constraints

1≤N≤10^5

1≤a_i,b_i≤10^5

1≤K≤b_1…+…b_n

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
a_1 b_1
:
a_N b_N

Output

Print the K-th smallest integer in the array after the N operations.

Sample Input 1

3 4
1 1
2 2
3 3

Sample Output 1

3

The resulting array is the same as the one in the problem statement.

Sample Input 2

10 500000
1 100000
1 100000
1 100000
1 100000
1 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000

Sample Output 2

1


var u,v,w:array[0..500000]of longint;
    dis:array[0..10000]of int64;
    n,m,s,i,j,k,check:longint;
begin
  readln(n,m);
  for i:=1 to m do
    readln(u[i],v[i],w[i]);
  for i:=1 to n do
    dis[i]:=2147483647;
  dis[1]:=0;
  for k:=1 to n-1 do
  begin
    check:=0;
    for i:=1 to m do
      if dis[v[i]]>dis[u[i]]-w[i] then
      begin
        dis[v[i]]:=dis[u[i]]-w[i];
        check:=1;
      end;
    if check=0 then
      break;
  end;
  writeln(-dis[n]);
end.

Score : 400 points

Problem Statement

There is a directed graph with N vertices and M edges.
The i-th edge (1≤i≤M) points from vertex a_i to vertex b_i, and has a weight c_i.
We will play the following single-player game using this graph and a piece.

Initially, the piece is placed at vertex 1, and the score of the player is set to 0.
The player can move the piece as follows:

When the piece is placed at vertex a_i, move the piece along the i-th edge to vertex b_i. After this move, the score of the player is increased by c_i.

The player can end the game only when the piece is placed at vertex N.
The given graph guarantees that it is possible to traverse from vertex 1 to vertex N.

When the player acts optimally to maximize the score at the end of the game, what will the score be?
If it is possible to increase the score indefinitely, print inf.

Constraints

2≤N≤1000

1≤M≤min(N(N-1),2000)

1≤a_i,b_i≤N (1≤i≤M)

a_i≠b_i (1≤i≤M)

a_i≠a_j or b_i≠b_j (1≤i<j≤M)

-10^9≤c_i≤10^9 (1≤i≤M)

c_i is an integer.

In the given graph, there exists a path from vertex 1 to vertex N.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

Output

Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf.

Sample Input 1

3 3
1 2 4
2 3 3
1 3 5

Sample Output 1

7

There are two ways to move the piece to vertex N=3:

vertex 1 → vertex 2 → vertex 3 : score 4+3=7

vertex 1 → vertex 3 : score 5

Thus, the maximum possible score at the end of the game is 7.

Sample Input 2

2 2
1 2 1
2 1 1

Sample Output 2

inf

It is possible to increase the score indefinitely by alternating between vertex 1 and 2.

Sample Input 3

6 5
1 2 -1000000000
2 3 -1000000000
3 4 -1000000000
4 5 -1000000000
5 6 -1000000000

Sample Output 3

-5000000000


var
	n,m,i,j:Longint;
	s:int64;
	d:array[1..1000]of int64;
	a,b,c:array[1..2000]of Longint;
begin
	read(n,m);
	for i:=1 to m do read(a[i],b[i],c[i]);
	for i:=2 to n do d[i]:=-1000000000000000000;
	for j:=1 to 2 do begin
		s:=d[n];
		for i:=1 to m do if d[b[i]]<d[a[i]]+c[i]then d[b[i]]:=d[a[i]]+c[i];
	end;
	if s<d[n]then writeln('inf')else writeln(s);
end.


var h,w,k,i,j,min,hh,t,xx,yy:longint;
    s:ansistring;
    ww,p:array[0..801,0..801]of boolean;
    f:array[0..801,0..801]of longint;
    x,y:array[1..10000000]of longint;
begin
    readln(h,w,k);
    min:=maxlongint;
    fillchar(f,sizeof(f),$7f);
    for i:=1 to h do begin
        readln(s);
        for j:=1 to w do begin
            if s[j]<>'#' then p[i,j]:=true;
            if s[j]='S' then begin
                x[1]:=i;y[1]:=j;
                f[i,j]:=0;
                if i-1<min then min:=i-1;
                if h-i<min then min:=h-i;
                if j-1<min then min:=j-1;
                if w-j<min then min:=w-j;
            end;
        end;
    end;
    if min=0 then begin
        writeln(0);
        exit;
    end;
    hh:=1;t:=1;
    while hh<=t do begin
        xx:=x[hh];yy:=y[hh];
        if  (p[xx+1,yy])and(not ww[xx+1,yy])and(f[xx+1,yy]>f[xx,yy]+1) then begin
            inc(t);
            x[t]:=xx+1;
            y[t]:=yy;
            ww[xx+1,yy]:=true;
            f[xx+1,yy]:=f[xx,yy]+1;
            if h-xx-1<min then min:=h-xx-1;
        end;
        if  (p[xx-1,yy])and(not ww[xx-1,yy])and(f[xx-1,yy]>f[xx,yy]+1) then begin
            inc(t);
            x[t]:=xx-1;
            y[t]:=yy;
            ww[xx-1,yy]:=true;
            f[xx-1,yy]:=f[xx,yy]+1;
            if xx-2<min then min:=xx-2;
        end;
        if  (p[xx,yy+1])and(not ww[xx,yy+1])and(f[xx,yy]>f[xx,yy]+1) then begin
            inc(t);
            x[t]:=xx;
            y[t]:=yy+1;
            ww[xx,yy+1]:=true;
            f[xx,yy+1]:=f[xx,yy]+1;
            if w-yy-1<min then min:=w-yy-1;
        end;
        if  (p[xx,yy-1])and(not ww[xx,yy-1])and(f[xx,yy-1]>f[xx,yy]+1) then begin
            inc(t);
            x[t]:=xx;
            y[t]:=yy-1;
            ww[xx,yy-1]:=true;
            f[xx,yy-1]:=f[xx,yy]+1;
            if yy-2<min then min:=yy-2;
        end;
        ww[xx,yy]:=false;
        inc(hh);
        while (hh<=t)and(f[x[hh],y[hh]]=k) do inc(hh);
    end;
    if min mod k<>0 then writeln(2+(min div k)) else writeln(1+(min div k));
end.

Score : 700 points

Problem Statement

Takahashi is locked within a building.

This building consists of H×W rooms, arranged in H rows and W columns.
We will denote the room at the i-th row and j-th column as (i,j). The state of this room is represented by a character A_{i,j}. If A_{i,j}= #, the room is locked and cannot be entered; if A_{i,j}= ., the room is not locked and can be freely entered.
Takahashi is currently at the room where A_{i,j}= S, which can also be freely entered.

Each room in the 1-st row, 1-st column, H-th row or W-th column, has an exit.
Each of the other rooms (i,j) is connected to four rooms: (i-1,j), (i+1,j), (i,j-1) and (i,j+1).

Takahashi will use his magic to get out of the building. In one cast, he can do the following:

Move to an adjacent room at most K times, possibly zero. Here, locked rooms cannot be entered.

Then, select and unlock at most K locked rooms, possibly zero. Those rooms will remain unlocked from then on.

His objective is to reach a room with an exit. Find the minimum necessary number of casts to do so.

It is guaranteed that Takahashi is initially at a room without an exit.

Constraints

3 ≤ H ≤ 800

3 ≤ W ≤ 800

1 ≤ K ≤ H×W

Each A_{i,j} is # , . or S.

There uniquely exists (i,j) such that A_{i,j}= S, and it satisfies 2 ≤ i ≤ H-1 and 2 ≤ j ≤ W-1.

Input

Input is given from Standard Input in the following format:

H W K
A_{1,1}A_{1,2}...A_{1,W}
:
A_{H,1}A_{H,2}...A_{H,W}

Output

Print the minimum necessary number of casts.

Sample Input 1

3 3 3
#.#
#S.
###

Sample Output 1

1

Takahashi can move to room (1,2) in one cast.

Sample Input 2

3 3 3
###
#S#
###

Sample Output 2

2

Takahashi cannot move in the first cast, but can unlock room (1,2).
Then, he can move to room (1,2) in the next cast, achieving the objective in two casts.

Sample Input 3

7 7 2
#######
#######
##...##
###S###
##.#.##
###.###
#######

Sample Output 3

2


program Project1;

{$APPTYPE CONSOLE}

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

    if (nyu[0][length(nyu[0])]=nyu[1][1] )and(nyu[1][length(nyu[1])]=nyu[2][1] ) then
      Writeln('YES')
    else
      Writeln('NO');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

You are given three strings A, B and C. Check whether they form a word chain.

More formally, determine whether both of the following are true:

The last character in A and the initial character in B are the same.

The last character in B and the initial character in C are the same.

If both are true, print YES. Otherwise, print NO.

Constraints

A, B and C are all composed of lowercase English letters (a - z).

1 ≤ |A|, |B|, |C| ≤ 10, where |A|, |B| and |C| are the lengths of A, B and C, respectively.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print YES or NO.

Sample Input 1

rng gorilla apple

Sample Output 1

YES

They form a word chain.

Sample Input 2

yakiniku unagi sushi

Sample Output 2

NO

A and B form a word chain, but B and C do not.

Sample Input 3

a a a

Sample Output 3

YES

Sample Input 4

aaaaaaaaab aaaaaaaaaa aaaaaaaaab

Sample Output 4

NO


program Project1;

{$APPTYPE CONSOLE}

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

    x:=a;
    while x<a*b do begin
      if x mod b = c then begin
        Writeln('YES');
        Readln;
        exit;
      end else
        x:=x+a;
    end;
    Writeln('NO');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

We ask you to select some number of positive integers, and calculate the sum of them.

It is allowed to select as many integers as you like, and as large integers as you wish.
You have to follow these, however: each selected integer needs to be a multiple of A, and you need to select at least one integer.

Your objective is to make the sum congruent to C modulo B.
Determine whether this is possible.

If the objective is achievable, print YES. Otherwise, print NO.

Constraints

1 ≤ A ≤ 100

1 ≤ B ≤ 100

0 ≤ C < B

Input

Input is given from Standard Input in the following format:

A B C

Output

Print YES or NO.

Sample Input 1

7 5 1

Sample Output 1

YES

For example, if you select 7 and 14, the sum 21 is congruent to 1 modulo 5.

Sample Input 2

2 2 1

Sample Output 2

NO

The sum of even numbers, no matter how many, is never odd.

Sample Input 3

1 100 97

Sample Output 3

YES

You can select 97, since you may select multiples of 1, that is, all integers.

Sample Input 4

40 98 58

Sample Output 4

YES

Sample Input 5

77 42 36

Sample Output 5

NO


var
  f:array[0..100000000] of longint;
  w,v:array[0..100000000] of longint;
  i,j,n,m:longint;
begin
  readln(m,n);
  for i:=1 to n do readln(w[i],v[i]);
  for i:=1 to n do
    for j:=m downto w[i] do
      if f[j-w[i]]+v[i]>f[j] then f[j]:=f[j-w[i]]+v[i];
  writeln(f[m]);
end.

Score : 400 points

Problem Statement

You have N items and a bag of strength W.
The i-th item has a weight of w_i and a value of v_i.

You will select some of the items and put them in the bag.
Here, the total weight of the selected items needs to be at most W.

Your objective is to maximize the total value of the selected items.

Constraints

1 ≤ N ≤ 100

1 ≤ W ≤ 10^9

1 ≤ w_i ≤ 10^9

For each i = 2,3,...,N, w_1 ≤ w_i ≤ w_1 + 3.

1 ≤ v_i ≤ 10^7

W, each w_i and v_i are integers.

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible total value of the selected items.

Sample Input 1

4 6
2 1
3 4
4 10
3 4

Sample Output 1

11

The first and third items should be selected.

Sample Input 2

4 6
2 1
3 7
4 10
3 6

Sample Output 2

13

The second and fourth items should be selected.

Sample Input 3

4 10
1 100
1 100
1 100
1 100

Sample Output 3

400

You can take everything.

Sample Input 4

4 1
10 100
10 100
10 100
10 100

Sample Output 4

0

You can take nothing.


var
	N,TX,pre,t,ans,i:Longint;
begin
	read(N,TX);
	for i:=1 to N do begin
		read(t);
		if pre<=t then inc(ans,TX)else inc(ans,t+TX-pre);
		pre:=t+TX;
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

In a public bath, there is a shower which emits water for T seconds when the switch is pushed.

If the switch is pushed when the shower is already emitting water, from that moment it will be emitting water for T seconds.
Note that it does not mean that the shower emits water for T additional seconds.

N people will push the switch while passing by the shower.
The i-th person will push the switch t_i seconds after the first person pushes it.

How long will the shower emit water in total?

Constraints

1 ≤ N ≤ 200,000

1 ≤ T ≤ 10^9

0 = t_1 < t_2 < t_3 < , ..., < t_{N-1} < t_N ≤ 10^9

T and each t_i are integers.

Input

Input is given from Standard Input in the following format:

N T
t_1 t_2 ... t_N

Output

Assume that the shower will emit water for a total of X seconds. Print X.

Sample Input 1

2 4
0 3

Sample Output 1

7

Three seconds after the first person pushes the water, the switch is pushed again and the shower emits water for four more seconds, for a total of seven seconds.

Sample Input 2

2 4
0 5

Sample Output 2

8

One second after the shower stops emission of water triggered by the first person, the switch is pushed again.

Sample Input 3

4 1000000000
0 1000 1000000 1000000000

Sample Output 3

2000000000

Sample Input 4

1 1
0

Sample Output 4

1

Sample Input 5

9 10
0 3 5 7 100 110 200 300 311

Sample Output 5

67


Uses math;
var
	N,W,wi,w0,vi,i,j,k,l:Longint;
	v:array[0..3,0..100]of Longint;
	c:array[0..3]of Longint;
	ans,now:int64;
begin
	read(N,W);
	for i:=1 to N do begin
		read(wi,vi);
		if i=1 then w0:=wi;
		inc(c[wi-w0]);
		v[wi-w0,c[wi-w0]]:=vi;
	end;
	for i:=0 to 3 do begin
		for j:=1 to c[i] do begin
			for k:=j+1 to c[i] do begin
				if v[i,j]<v[i,k]then begin
					l:=v[i,j];
					v[i,j]:=v[i,k];
					v[i,k]:=l;
				end;
			end;
		end;
		v[i,0]:=0;
		for j:=1 to c[i] do inc(v[i,j],v[i,j-1]);
	end;
	ans:=0;
	now:=0;
	for i:=0 to c[0] do begin
		if i>0 then inc(now,w0);
		if now>W then break;
		for j:=0 to c[1] do begin
			if j>0 then inc(now,w0+1);
			if now>W then break;
			for k:=0 to c[2] do begin
				if k>0 then inc(now,w0+2);
				if now>W then break;
				for l:=0 to c[3] do begin
					if l>0 then inc(now,w0+3);
					if now<=W then begin
						ans:=max(ans,v[0,i]+v[1,j]+v[2,k]+v[3,l]);
					end;
				end;
				dec(now,(w0+3)*c[3]);
			end;
			dec(now,(w0+2)*c[2]);
		end;
		dec(now,(w0+1)*c[1]);
	end;
	writeln(ans);
end.


program Project1;

{$APPTYPE CONSOLE}

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

    Writeln(nyu[0][1]+nyu[1][1]+nyu[2][1]);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

You are given three words s_1, s_2 and s_3, each composed of lowercase English letters, with spaces in between.
Print the acronym formed from the uppercased initial letters of the words.

Constraints

s_1, s_2 and s_3 are composed of lowercase English letters.

1 ≤ |s_i| ≤ 10 (1≤i≤3)

Input

Input is given from Standard Input in the following format:

s_1 s_2 s_3

Output

Print the answer.

Sample Input 1

atcoder beginner contest

Sample Output 1

ABC

The initial letters of atcoder, beginner and contest are a, b and c. Uppercase and concatenate them to obtain ABC.

Sample Input 2

resident register number

Sample Output 2

RRN

Sample Input 3

k nearest neighbor

Sample Output 3

KNN

Sample Input 4

async layered coding

Sample Output 4

ALC


var
a,b :comp;
begin
readln(a);
read(b);

if(a>b) then begin
write('GREATER');
end
else if(a=b) then begin
write('EQUAL');
end
else begin
write('LESS');
end;

end.

Score : 200 points

Problem Statement

You are given two positive integers A and B. Compare the magnitudes of these numbers.

Constraints

1 ≤ A, B ≤ 10^{100}

Neither A nor B begins with a 0.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print GREATER if A>B, LESS if A<B and EQUAL if A=B.

Sample Input 1

36
24

Sample Output 1

GREATER

Since 36>24, print GREATER.

Sample Input 2

850
3777

Sample Output 2

LESS

Sample Input 3

9720246
22516266

Sample Output 3

LESS

Sample Input 4

123456789012345678901234567890
234567890123456789012345678901

Sample Output 4

LESS


program ec12;
var
	a,s:array[0..100000] of longint;
	n,m,i,j,ans:longint;
begin
	readln(n);
	ans:=0;
	s[0]:=0;
	for i:=1 to n do
	read(a[i]);
	for i:=1 to n do
	begin
		s[i]:=s[i-1]+a[i];
		if i>1 then
		begin
			if s[i-1]<0 then
			begin
				if s[i]<=0 then
				begin
					if s[i]=0 then
					begin
						inc(ans);
						s[i]:=1;
					end
					else
					inc(ans,(-s[i])+1);
				end;
			end
			else
			begin
				if s[i]>=0 then
				begin
					if s[i]=0 then
					begin
						inc(ans);
						s[i]:=-1;
					end
					else
					begin
						inc(ans,s[i]+1);
						s[i]:=-1;
					end;
				end;
			end;
		end
		else
		inc(ans,abs(a[2]-a[1])+1);
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

You are given an integer sequence of length N. The i-th term in the sequence is a_i.
In one operation, you can select a term and either increment or decrement it by one.

At least how many operations are necessary to satisfy the following conditions?

For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.

For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1, respectively, which satisfy the conditions.

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8


var a,b:int64;begin read(a,b);if(a-2<b)and(b<a+2)then write('Brown')else write('Alice')end.

Score : 500 points

Problem Statement

Alice and Brown loves games. Today, they will play the following game.

In this game, there are two piles initially consisting of X and Y stones, respectively.
Alice and Bob alternately perform the following operation, starting from Alice:

Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.

The player who becomes unable to perform the operation, loses the game.

Given X and Y, determine the winner of the game, assuming that both players play optimally.

Constraints

0 ≤ X, Y ≤ 10^{18}

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the winner: either Alice or Brown.

Sample Input 1

2 1

Sample Output 1

Brown

Alice can do nothing but taking two stones from the pile containing two stones. As a result, the piles consist of zero and two stones, respectively. Then, Brown will take the two stones, and the piles will consist of one and zero stones, respectively. Alice will be unable to perform the operation anymore, which means Brown's victory.

Sample Input 2

5 0

Sample Output 2

Alice

Sample Input 3

0 0

Sample Output 3

Brown

Sample Input 4

4 8

Sample Output 4

Alice


var
  i,j,a1,a2,n,r:integer;
  a:array[1..100005] of integer;
begin
  read(n);
  r:=0;
  j:=1;
  for i:=1 to n do read(a[i]);
  while j<=n do
  begin
    r:=r+1;
    a1:=a[j];
    j:=j+1;
    while (a[j]=a1) and (j<=n) do j:=j+1;
    a2:=a[j];
    j:=j+1;
    while ((a1-a2) * (a[j-1]-a[j]) >= 0) and (j<=n) do j:=j+1;
  end;
  writeln(r);
end.

Score : 300 points

Problem Statement

You are given an array A of length N.
Your task is to divide it into several contiguous subarrays.
Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order.
At least how many subarrays do you need to divide A into?

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Each A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of subarrays after division of A.

Sample Input 1

6
1 2 3 2 2 1

Sample Output 1

2

One optimal solution is to divide the array into [1,2,3] and [2,2,1].

Sample Input 2

9
1 2 1 2 1 2 1 2 1

Sample Output 2

5

Sample Input 3

7
1 2 3 2 1 999999999 1000000000

Sample Output 3

3


var
  a,b,c:longint;
begin
  readln(a,b,c);
  if abs(b-a)=abs(c-b) then writeln('YES')
             else writeln('NO');
end.

Score : 100 points

Problem Statement

Three poles stand evenly spaced along a line. Their heights are a, b and c meters, from left to right.
We will call the arrangement of the poles beautiful if the tops of the poles lie on the same line, that is, b-a = c-b.

Determine whether the arrangement of the poles is beautiful.

Constraints

1 \leq a,b,c \leq 100

a, b and c are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

Print YES if the arrangement of the poles is beautiful; print NO otherwise.

Sample Input 1

2 4 6

Sample Output 1

YES

Since 4-2 = 6-4, this arrangement of poles is beautiful.

Sample Input 2

2 5 6

Sample Output 2

NO

Since 5-2 \neq 6-5, this arrangement of poles is not beautiful.

Sample Input 3

3 2 1

Sample Output 3

YES

Since 1-2 = 2-3, this arrangement of poles is beautiful.


uses math;
var a,f:array[0..130]of longint;
    n,i,j:longint;
    s:string;
begin
  readln(n);
  fillchar(f,sizeof(f),$3f);
  for i:=1 to n do
  begin
    readln(s);
    fillchar(a,sizeof(a),0);
    for j:=1 to length(s) do
      inc(a[ord(s[j])]);
    for j:=97 to 122 do
      f[j]:=min(ord(a[j]),f[j]);
  end;
  for i:=97 to 122 do
    for j:=1 to f[i] do
      write(chr(i));
end.

Score : 300 points

Problem Statement

Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.

He will receive a headline which contains one of the strings S_1,...,S_n tomorrow.
He is excited and already thinking of what string he will create.
Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.

Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains.
If there are multiple such strings, find the lexicographically smallest one among them.

Constraints

1 \leq n \leq 50

1 \leq |S_i| \leq 50 for every i = 1, ..., n.

S_i consists of lowercase English letters (a - z) for every i = 1, ..., n.

Input

Input is given from Standard Input in the following format:

n
S_1
...
S_n

Output

Print the lexicographically smallest string among the longest strings that satisfy the condition.
If the answer is an empty string, print an empty line.

Sample Input 1

3
cbaa
daacc
acacac

Sample Output 1

aac

The strings that can be created from each of cbaa, daacc and acacac, are aa, aac, aca, caa and so forth.
Among them, aac, aca and caa are the longest, and the lexicographically smallest of these three is aac.

Sample Input 2

3
a
aa
b

Sample Output 2

The answer is an empty string.


var n,i,x:longint;
    s:int64;
    a:array[0..300003] of longint;
    fi,fo:text;
procedure start;
begin
    assign(fi,''); reset(fi);
    assign(fo,''); rewrite(fo);
    readln(fi,n);
    n:= 3*n;
    for i:= 1 to n do read(fi,a[i]);
end;
procedure sort(l,r:longint);
var i,j,mid,tmp:longint;
begin
    i:= l; j:= r; mid:= a[(l+r)div 2];
    repeat
        while a[i]<mid do inc(i);
        while a[j]>mid do dec(j);
        if i<=j then
        begin
            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;
procedure main;
begin
    s:= 0;
    x:= n+1;
    for i:= 1 to n div 3 do s:= s+a[x-2*i];
end;
begin
    start;
    sort(1,n);
    main;
    writeln(fo,s);
    close(fi); close(fo);
end.

Score : 300 points

Problem Statement

There are 3N participants in AtCoder Group Contest.
The strength of the i-th participant is represented by an integer a_i.
They will form N teams, each consisting of three participants.
No participant may belong to multiple teams.

The strength of a team is defined as the second largest strength among its members.
For example, a team of participants of strength 1, 5, 2 has a strength 2, and a team of three participants of strength 3, 2, 3 has a strength 3.

Find the maximum possible sum of the strengths of N teams.

Constraints

1 ≤ N ≤ 10^5

1 ≤ a_i ≤ 10^{9}

a_i are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the answer.

Sample Input 1

2
5 2 8 5 1 5

Sample Output 1

10

The following is one formation of teams that maximizes the sum of the strengths of teams:

Team 1: consists of the first, fourth and fifth participants.

Team 2: consists of the second, third and sixth participants.

Sample Input 2

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 2

10000000000

The sum of the strengths can be quite large.


var i,j,k,l,m,n,q,w,x,y,t:longint;
    st,ed,first,next,d,v,color,fw,c:array[1..200000] of longint;
    b:array[1..200000]of boolean;
procedure ad(p,o:longint);
begin
    t:=t+1;  ed[t]:=o; next[t]:=first[p];first[p]:=t;
end;
procedure dfs(p,o,cl:longint);
var i,j:longint;
begin
    if fw[p]>=o then exit;
    i:=first[p]; fw[p]:=o;  b[p]:=true;
    if color[p]=0 then color[p]:=cl;
    while (i>=1) do begin
        if b[ed[i]]=false then
               dfs(ed[i],o-1,cl);
        i:=next[i];
    end;
   b[p]:=false;
end;
begin
    readln(n,m);
    for i:=1 to m do first[i]:=-1;
    for i:=1 to m do begin readln(x,y); ad(x,y);ad(y,x); end;
    readln(q);
    for i:=1 to q do readln(v[i],d[i],c[i]);
    for i:=q downto 1 do begin dfs(v[i],d[i],c[i]); end;
    for i:=1 to n do writeln(color[i]);
end.

Score : 700 points

Problem Statement

Squid loves painting vertices in graphs.

There is a simple undirected graph consisting of N vertices numbered 1 through N, and M edges.
Initially, all the vertices are painted in color 0. The i-th edge bidirectionally connects two vertices a_i and b_i. The length of every edge is 1.

Squid performed Q operations on this graph. In the i-th operation, he repaints all the vertices within a distance of d_i from vertex v_i, in color c_i.

Find the color of each vertex after the Q operations.

Constraints

1 ≤ N,M,Q ≤ 10^5

1 ≤ a_i,b_i,v_i ≤ N

a_i ≠ b_i

0 ≤ d_i ≤ 10

1 ≤ c_i ≤10^5

d_i and c_i are all integers.

There are no self-loops or multiple edges in the given graph.

Partial Score

200 points will be awarded for passing the testset satisfying 1 ≤ N,M,Q ≤ 2{,}000.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_{M} b_{M}
Q
v_1 d_1 c_1
:
v_{Q} d_{Q} c_{Q}

Output

Print the answer in N lines.
In the i-th line, print the color of vertex i after the Q operations.

Sample Input 1

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

Sample Output 1

2
2
2
2
2
1
0

Initially, each vertex is painted in color 0.
In the first operation, vertices 5 and 6 are repainted in color 1.
In the second operation, vertices 1, 2, 3, 4 and 5 are repainted in color 2.

Sample Input 2

14 10
1 4
5 7
7 11
4 10
14 7
14 3
6 14
8 11
5 13
8 3
8
8 6 2
9 7 85
6 9 3
6 7 5
10 3 1
12 9 4
9 6 6
8 2 3

Sample Output 2

1
0
3
1
5
5
3
3
6
1
3
4
5
3

The given graph may not be connected.


var
  n,m,x,y,i,q,total:longint;
  b,p,d,v,tot,que,first,next,ans,c:array[0..200010]of longint;

procedure add(x,y:longint);
begin
    inc(total);
    next[total]:=first[x];
    first[x]:=total;
    b[total]:=y;
end;

procedure bfs(k:longint);
var
  head,tail:longint;
  s:longint;
begin
    head:=1;
    tail:=1;
    que[1]:=k;
    while head<=tail do begin
        s:=first[que[head]];
        if ans[que[head]]=0 then ans[que[head]]:=c[i];
        if tot[head]>p[que[head]] then begin
            p[que[head]]:=tot[head];
            if tot[head]>0 then begin
                while s<>-1 do begin
                    inc(tail);
                    tot[tail]:=tot[head]-1;
                    que[tail]:=b[s];
                    s:=next[s];
                end;
            end;
        end;
        inc(head);
    end;
end;

begin
    read(n,m);
    for i:=1 to n*2 do first[i]:=-1;
    for i:=1 to m do begin
        read(x,y);
        add(x,y);
        add(y,x);
    end;
    read(q);
    for i:=1 to q do read(v[i],d[i],c[i]);
    for i:=q downto 1 do begin
        if p[v[i]]>d[i] then continue;
        if (ans[v[i]]<>0)and(d[i]=0) then continue;
        tot[1]:=d[i];
        bfs(v[i]);
    end;
    for i:=1 to n do writeln(ans[i]);
end.


var
  n,m,x,y,i,q,total:longint;
  b,p,d,v,tot,que,first,next,ans,c:array[0..200010]of longint;

procedure add(x,y:longint);
begin
    inc(total);
    next[total]:=first[x];
    first[x]:=total;
    b[total]:=y;
end;

procedure bfs(k:longint);
var
  head,tail:longint;
  s:longint;
begin
    head:=1;
    tail:=1;
    que[1]:=k;
    while head<=tail do begin
        s:=first[que[head]];
        if ans[que[head]]=0 then ans[que[head]]:=c[i];
        if tot[head]>p[que[head]] then p[que[head]]:=tot[head];
        if tot[head]>0 then begin
            while s<>-1 do begin
                inc(tail);
                tot[tail]:=tot[head]-1;
                que[tail]:=b[s];
                s:=next[s];
            end;
        end;
        inc(head);
    end;
end;

begin
    read(n,m);
    for i:=1 to n*2  do first[i]:=-1;
    for i:=1 to m do begin
        read(x,y);
        add(x,y);
        add(y,x);
    end;
    read(q);
    for i:=1 to q do read(v[i],d[i],c[i]);
    for i:=q downto 1 do begin
        if p[v[i]]>=d[i] then continue;
        if (ans[v[i]]<>0)and(d[i]=0) then continue;
        tot[1]:=d[i];
        bfs(v[i]);
    end;
    for i:=1 to n do writeln(ans[i]);
end.


var i,j,k,l,m,n,q,w,x,y,t:longint;
    st,ed,first,next,d,v,color,fw,c:array[1..200000] of longint;
    b:array[1..200000]of boolean;
procedure ad(p,o:longint);
begin
    t:=t+1;  ed[t]:=o; next[t]:=first[p];first[p]:=t;
end;
procedure dfs(p,o,cl:longint);
var i,j:longint;
begin
    if fw[p]>o then exit;
    i:=first[p]; fw[p]:=o;  b[p]:=true;
    if color[p]=0 then color[p]:=cl;
    while (i>=1) do begin
        if b[ed[i]]=false then
               dfs(ed[i],o-1,cl);
        i:=next[i];
    end;
   b[p]:=false;
end;
begin
    readln(n,m);
    for i:=1 to m do first[i]:=-1;
    for i:=1 to m do begin readln(x,y); ad(x,y);ad(y,x); end;
    readln(q);

    for i:=1 to q do readln(v[i],d[i],c[i]);
    for i:=q downto 1 do begin dfs(v[i],d[i],c[i]); end;
    for i:=1 to n do writeln(color[i]);
end.


var
  n,m,x,y,i,q,total:longint;
  b,p,d,v,tot,que,first,next,ans,c:array[0..200010]of longint;

procedure add(x,y:longint);
begin
    inc(total);
    next[total]:=first[x];
    first[x]:=total;
    b[total]:=y;
end;

procedure bfs(k:longint);
var
  head,tail:longint;
  s:longint;
begin
    head:=1;
    tail:=1;
    que[1]:=k;
    while head<=tail do begin
        s:=first[que[head]];
        if ans[que[head]]=0 then ans[que[head]]:=c[i];
        if tot[head]>p[que[head]] then p[que[head]]:=tot[head];
        if tot[head]>0 then begin
            while s<>-1 do begin
                inc(tail);
                tot[tail]:=tot[head]-1;
                que[tail]:=b[s];
                s:=next[s];
            end;
        end;
        inc(head);
    end;
end;

begin
    read(n,m);
    for i:=1 to n do first[i]:=-1;
    for i:=1 to m do begin
        read(x,y);
        add(x,y);
        add(y,x);
    end;
    read(q);
    for i:=1 to q do read(v[i],d[i],c[i]);
    for i:=q downto 1 do begin
        if p[v[i]]>d[i] then continue;
        if (c[v[i]]<>0)and(d[i]=0) then continue;
        tot[1]:=d[i];
        bfs(v[i]);
    end;
    for i:=1 to n do writeln(ans[i]);
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  h,w,i,j,count,x,y,sw,m,n,dist,poi:Integer;
  a,b,c,d:array of Integer;
  tf:Boolean;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(n);
    Readln(m);
    SetLength(a,n);
    SetLength(b,n);
    for i := 0 to n - 1 do begin
      Read(a[i]);
      Readln(b[i]);
    end;
    SetLength(c,m);
    SetLength(d,m);
    for i := 0 to m - 1 do begin
      Read(c[i]);
      Readln(d[i]);
    end;

    for i := 0 to n - 1 do begin
      dist:=999999999;
      for j := m-1 downto 0 do begin
        //dist:=(dist + abs(a[i]-c[j])+abs(b[i]-d[j])- abs(dist - abs(a[i]-c[j])+abs(b[i]-d[j]))) div 2;
        if dist>=abs(a[i]-c[j])+abs(b[i]-d[j]) then begin
          poi:=j+1;
          dist:=abs(a[i]-c[j])+abs(b[i]-d[j]);
        end;
      end;
      Writeln(poi);
    end;

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

Score : 200 points

Problem Statement

There are N students and M checkpoints on the xy-plane.

The coordinates of the i-th student (1 \leq i \leq N) is (a_i,b_i), and the coordinates of the checkpoint numbered j (1 \leq j \leq M) is (c_j,d_j).

When the teacher gives a signal, each student has to go to the nearest checkpoint measured in Manhattan distance.

The Manhattan distance between two points (x_1,y_1) and (x_2,y_2) is |x_1-x_2|+|y_1-y_2|.

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

If there are multiple nearest checkpoints for a student, he/she will select the checkpoint with the smallest index.

Which checkpoint will each student go to?

Constraints

1 \leq N,M \leq 50

-10^8 \leq a_i,b_i,c_j,d_j \leq 10^8

All input values are integers.

Input

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

N M
a_1 b_1
:
a_N b_N
c_1 d_1
:
c_M d_M

Output

Print N lines.

The i-th line (1 \leq i \leq N) should contain the index of the checkpoint for the i-th student to go.

Sample Input 1

2 2
2 0
0 0
-1 0
1 0

Sample Output 1

2
1

The Manhattan distance between the first student and each checkpoint is:

For checkpoint 1: |2-(-1)|+|0-0|=3

For checkpoint 2: |2-1|+|0-0|=1

The nearest checkpoint is checkpoint 2. Thus, the first line in the output should contain 2.

The Manhattan distance between the second student and each checkpoint is:

For checkpoint 1: |0-(-1)|+|0-0|=1

For checkpoint 2: |0-1|+|0-0|=1

When there are multiple nearest checkpoints, the student will go to the checkpoint with the smallest index. Thus, the second line in the output should contain 1.

Sample Input 2

3 4
10 10
-10 -10
3 3
1 2
2 3
3 5
3 5

Sample Output 2

3
1
2

There can be multiple checkpoints at the same coordinates.

Sample Input 3

5 5
-100000000 -100000000
-100000000 100000000
100000000 -100000000
100000000 100000000
0 0
0 0
100000000 100000000
100000000 -100000000
-100000000 100000000
-100000000 -100000000

Sample Output 3

5
4
3
2
1


var
	n,i:int64;
begin
	read(n);
	i:=round(sqrt(n));
	while n mod i<>0 do dec(i);
	n:=n div i;
	i:=0;
	while n<>0 do begin
		inc(i);
		n:=n div 10;
	end;
	writeln(i);
end.

Score : 300 points

Problem Statement

You are given an integer N.

For two positive integers A and B, we will define F(A,B) as the larger of the following: the number of digits in the decimal notation of A, and the number of digits in the decimal notation of B.

For example, F(3,11) = 2 since 3 has one digit and 11 has two digits.

Find the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.

Constraints

1 \leq N \leq 10^{10}

N is an integer.

Input

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

N

Output

Print the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.

Sample Input 1

10000

Sample Output 1

3

F(A,B) has a minimum value of 3 at (A,B)=(100,100).

Sample Input 2

1000003

Sample Output 2

7

There are two pairs (A,B) that satisfy the condition: (1,1000003) and (1000003,1). For these pairs, F(1,1000003)=F(1000003,1)=7.

Sample Input 3

9876543210

Sample Output 3

6


var
        i,min,s:longint;
        n,l:int64;
begin
        readln(n);
        min:=maxlongint;
        for i:=trunc(sqrt(n)) downto 1 do
            if n mod i=0 then
            begin
                s:=11;
                l:=10000000000;
                while n div i div l=0 do
                begin
                    l:=l div 10;
                    dec(s);
                end;
                if s<min then min:=s;
            end;
        writeln(min);
end.


var
  n,i,s:longint;
  st:string;
begin
  readln(n);
  for i:=trunc(sqrt(n)) downto 1 do
   if n mod i=0 then break;
  s:=0;
  while i>0 do
   begin
     s:=s+1;
     i:=i div 10;
   end;
  writeln(s);
end.


var
  n,i,s:int64;
  st:string;
begin
  readln(n);
  i:=trunc(sqrt(n))+1;
  while (i>0) do
   begin
     i:=i-1;
     if n mod i=0 then break;
   end;
  if i=0 then i:=1;
  s:=0;
  while i>0 do
   begin
     s:=s+1;
     i:=i div 10;
   end;
  write(s);
end.


var
  n,i,s:int64;
  st:string;
begin
  readln(n);
  i:=trunc(sqrt(n));
  while (i>0) and (s=0) do
   begin
     if n mod i=0 then
      begin
        str(i,st);
        writeln(length(st));
        halt;
      end;
     i:=i-1;
  end;
end.


var
 n:int64;
 i:longint;
 s:string;
begin
 readln(n);
 for i:=trunc(sqrt(n)) downto 1 do
  if n mod i=0 then break;
 if i>n div i then str(i,s)
              else str(n div i,s);
 writeln(length(s));
end.


var
  n,a,b:int64;
  i:longint;
  s:string;
begin
  read(n);
  a:=round(sqrt(n));
  for i:= a+1 downto 1 do
    if n mod i= 0 then
                    begin
                      b:=n div i;
                      str(b,s);
                      write(length(s));
                      break;
                    end;
end.


var
    s:string;
    n:int64;
    i,j:longint;
begin
    read(n);
    for i:=trunc(sqrt(n)) downto 2 do
        if n mod i=0 then
        begin
            j:=n div i;
            str(j,s);
            writeln(length(s));
            halt;
        end;
end.