type inputunit = record
    x,y,h:integer;
  end;
var r : array[1..100] of inputunit;
    i,n,x,y,h,th : integer;
    t,f : boolean;

function height:integer;
begin
  height := abs(x - r[i].x) + abs(y - r[i].y) + r[i].h
end;

begin
  readln(n);
  for i := 1 to n do with r[i] do readln(x,y,h);
  for x := 0 to 100 do begin
    for y := 0 to 100 do begin
      h := 1000000201;
      f := true;
      t := true;
      for i := 1 to n do begin
        if t then begin
          if r[i].h > 0 then t := false;
          th := height;
          if h < th then if t then f := false else h := th
        end else if h <> height then f := false
      end;
      if f = true then break
    end;
    if f = true then break
  end;
  writeln(x,' ',y,' ',h)
end.


type inputunit = record
    x,y,h:integer;
  end;
var r : array[1..100] of inputunit;
    i,n,x,y,h,th : integer;
    t,f : boolean;

function height:integer;
begin
  height := abs(x - r[i].x) + abs(y - r[i].y) + r[i].h
end;

begin
  readln(n);
  for i := 1 to n do with r[i] do readln(x,y,h);
  for x := 0 to 100 do begin
    for y := 0 to 100 do begin
      h := 1000000201;
      f := true;
      t := true;
      for i := 1 to n do begin
        if t then begin
          if r[i].h > 0 then t := false;
          th := height;
          if t and (h < th) then f := false else h := th
        end else if h <> height then f := false
      end;
      if f = true then break
    end;
    if f = true then break
  end;
  writeln(x,' ',y,' ',h)
end.

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

Score: 300 points

Problem Statement

In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc.

The pyramid had center coordinates (C_X, C_Y) and height H. The altitude of coordinates (X, Y) is max(H - |X - C_X| - |Y - C_Y|, 0).

Aoki, an explorer, conducted a survey to identify the center coordinates and height of this pyramid. As a result, he obtained the following information:

C_X, C_Y was integers between 0 and 100 (inclusive), and H was an integer not less than 1.

Additionally, he obtained N pieces of information. The i-th of them is: "the altitude of point (x_i, y_i) is h_i."

This was enough to identify the center coordinates and the height of the pyramid. Find these values with the clues above.

Constraints

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

x_i and y_i are integers between 0 and 100 (inclusive).

h_i is an integer between 0 and 10^9 (inclusive).

The N coordinates (x_1, y_1), (x_2, y_2), (x_3, y_3), ..., (x_N, y_N) are all different.

The center coordinates and the height of the pyramid can be uniquely identified.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 h_1
x_2 y_2 h_2
x_3 y_3 h_3
:
x_N y_N h_N

Output

Print values C_X, C_Y and H representing the center coordinates and the height of the pyramid in one line, with spaces in between.

Sample Input 1

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

Sample Output 1

2 2 6

In this case, the center coordinates and the height can be identified as (2, 2) and 6.

Sample Input 2

2
0 0 100
1 1 98

Sample Output 2

0 0 100

In this case, the center coordinates and the height can be identified as (0, 0) and 100.

Note that C_X and C_Y are known to be integers between 0 and 100.

Sample Input 3

3
99 1 191
100 1 192
99 0 192

Sample Output 3

100 0 193

In this case, the center coordinates and the height can be identified as (100, 0) and 193.

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.


type inputunit = record
    x,y,h:integer;
  end;
var r : array[1..100] of inputunit;
    i,n,x,y,h,th : integer;
    t,f : boolean;

function height:integer;
begin
  height := abs(x - r[i].x) + abs(y - r[i].y) + r[i].h
end;

begin
  readln(n);
  for i := 1 to n do with r[i] do readln(x,y,h);
  for x := 0 to 100 do begin
    for y := 0 to 100 do begin
      h := 1000000001;
      f := true;
      t := true;
      for i := 1 to n do begin
        if t then begin
          if r[i].h > 0 then t := false;
          th := height;
          if t and (h < th) then f := false else h := th
        end else if h <> height then f := false
      end;
      if f = true then break
    end;
    if f = true then break
  end;
  writeln(x,' ',y,' ',h)
end.


Uses math;
var
	n,i,cx,cy,hg,flag,s:Longint;
	x,y,h:array[1..100]of Longint;
begin
	read(n);
	for i:=1 to n do begin
		read(x[i],y[i],h[i]);
		if h[i]>0 then s:=i;
	end;
	for cx:=0 to 100 do begin
		for cy:=0 to 100 do begin
			hg:=h[s]+abs(x[s]-cx)+abs(y[s]-cy);
			flag:=1;
			for i:=1 to n do begin
				if h[i]<>Max(hg-abs(x[i]-cx)-abs(y[i]-cy),0) then begin
					flag:=0;
					break;
				end;
			end;
			if flag>0 then writeln(cx,' ',cy,' ',hg);
		end;
	end;
end.


var
  n,i,j,ax,ay,ah:longint;
  x,y,h:array[0..100]of longint;
  f:boolean;
begin
  readln(n);
  for i:=1 to n do
    readln(x[i],y[i],h[i]);
  for ax:=0 to 100 do
    for ay:=0 to 100 do
      begin
        ah:=-1;  f:=true;
        for i:=1 to n do
          if h[i]<>0 then
            begin
              if ah=-1 then ah:=h[i]+abs(ax-x[i])+abs(ay-y[i]) else
              if ah<>h[i]+abs(ax-x[i])+abs(ay-y[i]) then
                begin
                  f:=false;
                  break;
                end;
            end;
		for i:=1 to n do if h[i]=0 then
		if ah-(abs(ax-x[i])+abs(ay-y[i]))>0 then f:=false;
        if f=true then begin  write(ax,' ',ay,' ',ah); exit; end;
      end;
end.


var
  n,i,j,ax,ay,ah:longint;
  x,y,h:array[0..100]of longint;
  f:boolean;
begin
  readln(n);
  for i:=1 to n do
    readln(x[i],y[i],h[i]);
  for ax:=0 to 101 do
    for ay:=0 to 101 do
      begin
        ah:=-1;  f:=true;
        for i:=1 to n do
          if h[i]>=1 then
            begin
              if ah=-1 then ah:=h[i]+abs(ax-x[i])+abs(ay-y[i]) else
              if (ah<>h[i]+abs(ax-x[i])+abs(ay-y[i])) then
                begin
                  f:=false;
                  break;
                end;
            end;
        if f=true then begin  write(ax,' ',ay,' ',ah); exit; end;
      end;
end.


var
  n,i,j,ax,ay,ah:longint;
  x,y,h:array[0..100]of longint;
  f:boolean;
begin
  readln(n);
  for i:=1 to n do
    readln(x[i],y[i],h[i]);
  for ax:=0 to 101 do
    for ay:=0 to 101 do
      begin
        ah:=-1;  f:=true;
        for i:=1 to n do
            begin
              if ah=-1 then ah:=h[i]+abs(ax-x[i])+abs(ay-y[i]) else
              if (ah<>h[i]+abs(ax-x[i])+abs(ay-y[i])) then
                begin
                  f:=false;
                  break;
                end;
            end;
        if f=true then begin  write(ax,' ',ay,' ',ah); exit; end;
      end;
end.


var
	b:boolean;
	n,i,j,k,t,max:longint;
	x,y,h:array[0..200] of longint;
function g(a,b:longint):longint;
begin
	if a>b then exit(a) else exit(b);
end;
begin
	read(n);
	for i:=1 to n do read(x[i],y[i],h[i]);
	for i:=0 to 100 do
		for j:=0 to 100 do
		begin
			for k:=1 to n do
				if h[k]<>0 then
				begin
					//h[k]=h-abs(x[k]-i)-abs(y[k]-j);
					max:=abs(x[k]-i)+abs(y[k]-j)+h[k];
					break;
				end;
                        if max<1 then continue;
			b:=true;
			for k:=1 to n do
				if h[k]<>g(0,max-abs(x[k]-i)-abs(y[k]-j)) then
				begin
					b:=false;
					break;
				end;
			if b then
			begin
				writeln(i,' ',j,' ',max);
				halt;
			end;
		end;
end.


var
	b:boolean;
	n,i,j,k,t,max:longint;
	x,y,h:array[0..200] of longint;
function g(a,b:longint):longint;
begin
	if a>b then exit(a) else exit(b);
end;
begin
	read(n);
	for i:=1 to n do read(x[i],y[i],h[i]);
	for i:=0 to 100 do
		for j:=0 to 100 do
		begin
			for k:=1 to n do
				if h[k]<>0 then
				begin
					//h[k]=h-abs(x[k]-i)-abs(y[k]-j);
					max:=abs(x[k]-i)+abs(y[k]-j)+h[k];
					break;
				end;
                        if max<1 then continue;
			b:=true;
			t:=k;
			for k:=t+1 to n do
				if h[k]<>g(0,max-abs(x[k]-i)-abs(y[k]-j)) then
				begin
					b:=false;
					break;
				end;
			if b then
			begin
				writeln(i,' ',j,' ',max);
				halt;
			end;
		end;
end.


var
  n,i,j,ax,ay,ah:longint;
  x,y,h:array[0..100]of longint;
  f:boolean;
begin
  readln(n);
  for i:=1 to n do
    readln(x[i],y[i],h[i]);
  for ax:=0 to 101 do
    for ay:=0 to 101 do
      begin
        ah:=-1;  f:=true;
        for i:=1 to n do
          if h[i]<>0 then
            begin
              if ah=-1 then ah:=h[i]+abs(ax-x[i])+abs(ay-y[i]) else
              if ah<>h[i]+abs(ax-x[i])+abs(ay-y[i]) then
                begin
                  f:=false;
                  break;
                end;
            end;
        if f=true then begin  write(ax,' ',ay,' ',ah); exit; end;
      end;
end.


var
  n,i,j:integer;
  x,y:array[1..100] of integer;
  h:array[1..100] of longint;
function abs(x:longint):longint;
begin
  if x<0 then
  begin
    abs:=-x;
  end
  else
  begin
    abs:=x;
  end;
end;
function check(xx,yy:integer):boolean;
var
  now:integer;
  tmpw:longint;
begin
  for now:=1 to n do
  begin
    if h[now]<>0 then
    begin
      tmpw:=h[now]+abs(x[now]-xx)+abs(y[now]-yy);
      break;
    end;
  end;
  check:=true;
  for now:=1 to n do
  begin
    if not ((tmpw-abs(x[now]-xx)-abs(y[now]-yy)=h[now]) or ((tmpw-abs(x[now]-xx)-abs(y[now]-yy)<0) and (h[now]=0))) then
    begin
      check:=false;
      break;
    end;
  end;
end;
function get(xx,yy:integer):longint;
var
  now:integer;
begin
  for now:=1 to n do
  begin
    if h[now]<>0 then
    begin
      get:=h[now]+abs(x[now]-xx)+abs(y[now]-yy);
      break;
    end;
  end;
end;
begin
  readln(n);
  for i:=1 to n do
  begin
    readln(x[i],y[i],h[i]);
  end;
  for i:=0 to 100 do
  begin
    for j:=0 to 100 do
    begin
      if check(i,j) then
      begin
        writeln(i,' ',j,' ',get(i,j));
        exit;
      end;
    end;
  end;
end.


var
  n,i,j:integer;
  x,y:array[1..100] of integer;
  h:array[1..100] of longint;
function abs(x:longint):longint;
begin
  if x<0 then
  begin
    abs:=-x;
  end
  else
  begin
    abs:=x;
  end;
end;
function check(xx,yy:integer):boolean;
var
  now:integer;
  tmpw:longint;
begin
  tmpw:=h[1]+abs(x[1]-xx)+abs(y[1]-yy);
  check:=true;
  for now:=1 to n do
  begin
    if not ((tmpw-abs(x[now]-xx)-abs(y[now]-yy)=h[now]) or ((tmpw-abs(x[now]-xx)-abs(y[now]-yy)<0) and (h[now]=0))) then
    begin
      check:=false;
      break;
    end;
  end;
end;
function get(xx,yy:integer):longint;
begin
  get:=h[1]+abs(x[1]-xx)+abs(y[1]-yy);
end;
begin
  readln(n);
  for i:=1 to n do
  begin
    readln(x[i],y[i],h[i]);
  end;
  for i:=0 to 100 do
  begin
    for j:=0 to 100 do
    begin
      if check(i,j) then
      begin
        writeln(i,' ',j,' ',get(i,j));
        exit;
      end;
    end;
  end;
end.


var
	b:boolean;
	n,i,j,k,t,max:longint;
	x,y,h:array[0..200] of longint;
function g(a,b:longint):longint;
begin
	if a>b then exit(a) else exit(b);
end;
begin
	read(n);
	for i:=1 to n do read(x[i],y[i],h[i]);
	for i:=0 to 100 do
		for j:=0 to 100 do
		begin
			for k:=1 to n do
				if h[k]<>0 then
				begin
					//h[k]=h-abs(x[k]-i)-abs(y[k]-j);
					max:=abs(x[k]-i)+abs(y[k]-j)+h[k];
					break;
				end;
			b:=true;
			t:=k;
			for k:=t+1 to n do
				if h[k]<>g(0,max-abs(x[k]-i)-abs(y[k]-j)) then
				begin
					b:=false;
					break;
				end;
			if b then
			begin
				writeln(i,' ',j,' ',max);
				halt;
			end;
		end;
end.


var
	b:boolean;
	n,i,j,k,t,max:longint;
	x,y,h:array[0..200] of longint;
begin
	read(n);
	for i:=1 to n do read(x[i],y[i],h[i]);
	for i:=0 to 100 do
		for j:=0 to 100 do
		begin
			for k:=1 to n do
				if h[k]<>0 then
				begin
					//h[k]=h-abs(x[k]-i)-abs(y[k]-j);
					max:=abs(x[k]-i)+abs(y[k]-j)+h[k];
					break;
				end;
			b:=true;
			t:=k;
			for k:=t+1 to n do
				if (h[k]<>max-abs(x[k]-i)-abs(y[k]-j)) and (h[k]<>0) then
				begin
					b:=false;
					break;
				end;
			if b then
			begin
				writeln(i,' ',j,' ',max);
				halt;
			end;
		end;
end.


var
	b:boolean;
	n,i,j,k,t,max:longint;
	x,y,h:array[0..200] of longint;
begin
	read(n);
	for i:=1 to n do read(x[i],y[i],h[i]);
	for i:=0 to 100 do
		for j:=0 to 100 do
		begin
			for k:=1 to n do
				if h[k]<>0 then
				begin
					//h[k]=h-abs(x[k]-i)-abs(y[k]-j);
					max:=abs(x[k]-i)+abs(y[k]-j)+h[k];
					break;
				end;
			b:=true;
			t:=k;
			for k:=t+1 to n do
				if h[k]<>max-abs(x[k]-i)-abs(y[k]-j) then
				begin
					b:=false;
					break;
				end;
			if b then
			begin
				writeln(i,' ',j,' ',max);
				halt;
			end;
		end;
end.


var
  n,i,j:integer;
  x,y:array[1..100] of integer;
  h:array[1..100] of longint;
function abs(x:longint):longint;
begin
  if x<0 then
  begin
    abs:=-x;
  end
  else
  begin
    abs:=x;
  end;
end;
function check(xx,yy:integer):boolean;
var
  now:integer;
  tmpw:longint;
begin
  tmpw:=h[1]+abs(x[1]-xx)+abs(y[1]-yy);
  check:=true;
  for now:=1 to n do
  begin
    if h[now]+abs(x[now]-xx)+abs(y[now]-yy)<>tmpw then
    begin
      check:=false;
      break;
    end;
  end;
end;
function get(xx,yy:integer):longint;
begin
  get:=h[1]+abs(x[1]-xx)+abs(y[1]-yy);
end;
begin
  readln(n);
  for i:=1 to n do
  begin
    readln(x[i],y[i],h[i]);
  end;
  for i:=0 to 100 do
  begin
    for j:=0 to 100 do
    begin
      if check(i,j) then
      begin
        writeln(i,' ',j,' ',get(i,j));
        exit;
      end;
    end;
  end;
end.


var
  n,i,j,ax,ay:longint;
  ah:qword;
  x,y,h:array[0..100]of longint;
  f:boolean;
begin
  readln(n);
  for i:=1 to n do
    readln(x[i],y[i],h[i]);
  for ax:=0 to 100 do
    for ay:=0 to 100 do
      begin
        ah:=0;  f:=true;
        for i:=1 to n do
          if h[i]<>0 then
            begin
              if ah=0 then ah:=h[i]+abs(ax-x[i])+abs(ay-y[i]) else
              if ah<>h[i]+abs(ax-x[i])+abs(ay-y[i]) then
                begin
                  f:=false;
                  break;
                end;
            end;
        if f=true then begin  write(ax,' ',ay,' ',ah); exit; end;
      end;
end.


var
  n,i,j,ax,ay,ah:longint;
  x,y,h:array[0..100]of longint;
  f:boolean;
begin
  readln(n);
  for i:=1 to n do
    readln(x[i],y[i],h[i]);
  for ax:=0 to 100 do
    for ay:=0 to 100 do
      begin
        ah:=-1;  f:=true;
        for i:=1 to n do
          if h[i]<>0 then
            begin
              if ah=-1 then ah:=h[i]+abs(ax-x[i])+abs(ay-y[i]) else
              if ah<>h[i]+abs(ax-x[i])+abs(ay-y[i]) then
                begin
                  f:=false;
                  break;
                end;
            end;
        if f=true then begin  write(ax,' ',ay,' ',ah); exit; end;
      end;
end.


uses
	SysUtils,
	Classes;

procedure MainProc;
	var
		text : string;
		strlist : TStringList;
	begin
	ReadLn(text);
	strlist := TStringList.Create;
	try
		strlist.Delimiter := ' ';
		strlist.DelimitedText := text;
		strlist.Sort;
		if strlist.Count = 3 then
			WriteLn(StrToInt(strlist[0]) + StrToInt(strlist[1]) + (StrToInt(strlist[2]) * 10));
	finally
			FreeAndNil(strlist);
			end;
	end;

begin
MainProc;
end.

Score : 100 points

Problem Statement

You have decided to give an allowance to your child depending on the outcome of the game that he will play now.

The game is played as follows:

There are three "integer panels", each with a digit between 1 and 9 (inclusive) printed on it, and one "operator panel" with a + printed on it.

The player should construct a formula of the form X + Y, by arranging the four panels from left to right. (The operator panel should not be placed at either end of the formula.)

Then, the amount of the allowance will be equal to the resulting value of the formula.

Given the values A, B and C printed on the integer panels used in the game, find the maximum possible amount of the allowance.

Constraints

All values in input are integers.

1 \leq A, B, C \leq 9

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the maximum possible amount of the allowance.

Sample Input 1

1 5 2

Sample Output 1

53

The amount of the allowance will be 53 when the panels are arranged as 52+1, and this is the maximum possible amount.

Sample Input 2

9 9 9

Sample Output 2

108

Sample Input 3

6 6 7

Sample Output 3

82


program Project1;

{$APPTYPE CONSOLE}

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

    if a=2 then begin
      Writeln('No');
      Readln;
      exit;
    end;

    SetLength(l,a);
    for i := 0 to a-1 do begin
      Readln(l[i]);
      if not((i=0)or(i=a-1)) then begin
        if l[i-1][Length(l[i-1])]<>l[i][1] then begin
          //Writeln(l[i-1][Length(l[i-1])]);
          //Writeln(l[i][1]);
          Writeln('No');
          Readln;
          exit;
        end;
      end;
    end;

    for i := 0 to a - 2 do
      for j := i to a - 2 do begin
        if l[i]=l[j+1] then begin
          Writeln('No');
          Readln;
          exit;
        end;
      end;

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

Score : 100 points

Problem Statement

You are given integers A and B, each between 1 and 3 (inclusive).

Determine if there is an integer C between 1 and 3 (inclusive) such that A \times B \times C is an odd number.

Constraints

All values in input are integers.

1 \leq A, B \leq 3

Input

Input is given from Standard Input in the following format:

A B

Output

If there is an integer C between 1 and 3 that satisfies the condition, print Yes; otherwise, print No.

Sample Input 1

3 1

Sample Output 1

Yes

Let C = 3. Then, A \times B \times C = 3 \times 1 \times 3 = 9, which is an odd number.

Sample Input 2

1 2

Sample Output 2

No

Sample Input 3

2 2

Sample Output 3

No


uses
	SysUtils,
	Classes;

procedure MainProc;
	var
		text : string;
		strlist : TStringList;
		numA, numB, ans: Integer;
	begin
	ReadLn(text);
	strlist := TStringList.Create;

	try
		strlist.Delimiter := ' ';
		strlist.DelimitedText := text;
		numA := StrToInt(strlist.Strings[0]);
		numB := StrToInt(strlist.Strings[1]);
	finally
			FreeAndNil(strlist);
			end;
	ans := (numA * numB) mod 2;
	if ans = 1 then
		WriteLn('Yes')
	else
		WriteLn('No');
	end;

begin
MainProc;
end.


uses
	SysUtils,
	Classes;

function Shiritori(const n: Integer): Boolean;
	var
		i: Integer;
		lastWord: string;
		aWordList: TStringList;
		aText: string;
	begin
	Result := False;
	aWordList := TStringList.Create;
	lastWord := '';
	try
		for i := 1 to n do
			begin
			ReadLn(aText);
			if aWordList.IndexOf(aText) <> -1 then
				Exit;
			if  (lastWord <> '') and (lastWord[Length(lastWord)] <> aText[1]) then
				Exit;
			aWordList.Add(aText);
			lastWord := aText;
			end;
	finally
		FreeAndNil(aWordList);
		end;
	Result := True;
	end;

procedure MainProc;
	var
		aText: string;
		numOfWord: Integer;
	begin
	ReadLn(aText);
	numOfWord := StrToInt(aText);
	if (2 > numOfWord) or (100 < numOfWord) then
		Exit;
	if  Shiritori(numOfWord) then
		WriteLn('Yes')
	else
		WriteLn('No');
	end;

begin
MainProc;
end.

Score : 200 points

Problem Statement

Takahashi is practicing shiritori alone again today.

Shiritori is a game as follows:

In the first turn, a player announces any one word.

In the subsequent turns, a player announces a word that satisfies the following conditions:

That word is not announced before.

The first character of that word is the same as the last character of the last word announced.

In this game, he is practicing to announce as many words as possible in ten seconds.

You are given the number of words Takahashi announced, N, and the i-th word he announced, W_i, for each i. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions.

Constraints

N is an integer satisfying 2 \leq N \leq 100.

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

Input

Input is given from Standard Input in the following format:

N
W_1
W_2
:
W_N

Output

If every word announced by Takahashi satisfied the conditions, print Yes; otherwise, print No.

Sample Input 1

4
hoge
english
hoge
enigma

Sample Output 1

No

As hoge is announced multiple times, the rules of shiritori was not observed.

Sample Input 2

9
basic
c
cpp
php
python
nadesico
ocaml
lua
assembly

Sample Output 2

Yes

Sample Input 3

8
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaa
aaaaaaa

Sample Output 3

No

Sample Input 4

3
abc
arc
agc

Sample Output 4

No


uses
	SysUtils,
	Classes;

function Shiritori(const n: Integer): Boolean;
	var
		i: Integer;
		lastWord: string;
		aWordList: TStringList;
		aText: string;
	begin
	Result := False;
	aWordList := TStringList.Create;
	lastWord := '';
	try
		for i := 1 to n do
			begin
			ReadLn(aText);
			if aWordList.IndexOf(aText) <> -1 then
				Exit;
			if  (lastWord <> '') and (lastWord[Length(lastWord)] <> aText[1]) then
				Exit;
			aWordList.Add(aText);
			lastWord := aText;
			end;
	finally
		FreeAndNil(aWordList);
		end;
	Result := True;
	end;

procedure MainProc;
	var
		aText: string;
		numOfWord: Integer;
	begin
	ReadLn(aText);
	numOfWord := StrToInt(aText);
	if (2 >= numOfWord) or (100 <= numOfWord) then
		Exit;
	if  Shiritori(numOfWord) then
		WriteLn('Yes')
	else
		WriteLn('No');
	end;

begin
MainProc;
end.


    program Project1;

    {$APPTYPE CONSOLE}

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

        if a=2 then begin
          Writeln('No');
          Readln;
          exit;
        end;

        SetLength(l,a);
        for i := 0 to a-1 do begin
          Readln(l[i]);
          if not((i=0)or(i=a-1)) then begin
            if l[i-1][Length(l[i-1])]<>l[i][1] then begin
              //Writeln(l[i-1][Length(l[i-1])]);
              //Writeln(l[i][1]);
              Writeln('No');
              Readln;
              exit;
            end;
          end;
        end;

        for i := 0 to a - 2 do
          for j := i to a - 2 do begin
            if l[i]=l[j+1] then begin
              Writeln('No');
              Readln;
              exit;
            end;
          end;

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


program Project1;

{$APPTYPE CONSOLE}

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

    SetLength(l,a);
    for i := 0 to a-1 do begin
      Readln(l[i]);
      if not((i=0)or(i=a-1)) then begin
        if l[i-1][Length(l[i-1])]<>l[i][1] then begin
          //Writeln(l[i-1][Length(l[i-1])]);
          //Writeln(l[i][1]);
          Writeln('No');
          Readln;
          exit;
        end;
      end;
    end;

    for i := 0 to a - 2 do
      for j := i to a - 2 do begin
        if l[i]=l[j+1] then begin
          Writeln('No');
          Readln;
          exit;
        end;
      end;

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


var
	n,i,ans,x,a:Longint;
function gcd(a,b:Longint):Longint;
begin
	if b=0 then gcd:=a else gcd:=gcd(b,a mod b);
end;
begin
	read(n,x);
	for i:=1 to n do begin
		read(a);dec(a,x);if a<0 then a:=-a;
		ans:=gcd(ans,a);
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

There are N cities on a number line. The i-th city is located at coordinate x_i.

Your objective is to visit all these cities at least once.

In order to do so, you will first set a positive integer D.

Then, you will depart from coordinate X and perform Move 1 and Move 2 below, as many times as you like:

Move 1: travel from coordinate y to coordinate y + D.

Move 2: travel from coordinate y to coordinate y - D.

Find the maximum value of D that enables you to visit all the cities.

Here, to visit a city is to travel to the coordinate where that city is located.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq X \leq 10^9

1 \leq x_i \leq 10^9

x_i are all different.

x_1, x_2, ..., x_N \neq X

Input

Input is given from Standard Input in the following format:

N X
x_1 x_2 ... x_N

Output

Print the maximum value of D that enables you to visit all the cities.

Sample Input 1

3 3
1 7 11

Sample Output 1

2

Setting D = 2 enables you to visit all the cities as follows, and this is the maximum value of such D.

Perform Move 2 to travel to coordinate 1.

Perform Move 1 to travel to coordinate 3.

Perform Move 1 to travel to coordinate 5.

Perform Move 1 to travel to coordinate 7.

Perform Move 1 to travel to coordinate 9.

Perform Move 1 to travel to coordinate 11.

Sample Input 2

3 81
33 105 57

Sample Output 2

24

Sample Input 3

1 1
1000000000

Sample Output 3

999999999


const m=1000000000;
var n,x,i,j,min,dd:longint;
a:array[1..100000]of longint;
b:array[1..m] of 0..1;
begin
	read(n,x);
	for i:=1 to n do
		read(a[i]);
	min:=abs(a[1]-x);
	for i:=n downto 2 do
	begin
		a[i]:=abs(a[i]-a[i-1]);
		if a[i]<min then min:=a[i];
	end;
	a[1]:=abs(a[1]-x);
	dd:=2;
	for i:=1 to n do
	begin
		if (i=n) then
		begin
			while (b[min]=1)or (a[i] mod min<>0) do dec(min);
			write(min);
			exit;
		end;
		for j:=min downto dd do
			if (b[j]=0) and (a[i] mod j<>0) then b[j]:=1;
		while b[min]=1 do dec(min);
		while b[dd]=1 do inc(dd);
	end;
end.


const m=1000000000;
var n,x,i,xi,yi,j,min,dd:longint;
a:array[1..100000]of longint;
b:array[1..m] of 0..1;
begin
	read(n,x,xi);
	a[1]:=abs(xi-x);
	min:=a[1];
	for i:=2 to n do
	begin
		read(yi);
		a[i]:=abs(yi-xi);
		xi:=yi;
		if a[i]<min then min:=a[i];
	end;
	dd:=2;
	for i:=1 to n do
	begin
		if (i=n) then
		begin
			while (b[min]=1)or (a[i] mod min<>0) do dec(min);
			write(min);
			exit;
		end;
		for j:=min downto dd do
			if (b[j]=0) and (a[i] mod j<>0) then b[j]:=1;
		while b[min]=1 do dec(min);
		while b[dd]=1 do inc(dd);
	end;
end.


const m=1000000000;
var n,x,i,xi,yi,j,min,dd:longint;
a:array[1..100000]of longint;
b:array[1..m] of 0..1;
c:array[1..10000]of 0..1;
begin
	read(n,x,xi);
	a[1]:=abs(xi-x);
	min:=a[1];
	dd:=2;
	for i:=2 to n do
	begin
		read(yi);
		if abs(yi-xi)<10000 then
		if c[abs(yi-xi)]=0 then
		begin
			a[i]:=abs(yi-xi);
			c[a[i]]:=1;
			xi:=yi;
			if a[i]<min then min:=a[i];
		end;
	end;
	for i:=1 to n do
	begin
		if (i=n) then
		begin
			while (b[min]=1)or (a[i] mod min<>0) do dec(min);
			write(min);
			exit;
		end;
		for j:=min downto dd do
			if (b[j]=0) and (a[i] mod j<>0) then b[j]:=1;
		while b[min]=1 do dec(min);
		while b[dd]=1 do inc(dd);
	end;
end.


const m=1000000000;
var n,x,i,xi,yi,j,min:longint;
a:array[1..100000]of longint;
b:array[1..m] of 0..1;
begin
	read(n,x,xi);
	a[1]:=abs(xi-x);
	min:=a[1];
	for i:=2 to n do
	begin
		read(yi);
		a[i]:=abs(yi-xi);
		xi:=yi;
		if a[i]<min then min:=a[i];
	end;
	for i:=1 to n do
	begin
		if (i=n) then
		begin
			while (b[min]=1)or (a[i] mod min<>0) do dec(min);
			write(min);
			exit;
		end;
		for j:=min downto 2 do
			if (b[j]=0) and (a[i] mod j<>0) then b[j]:=1;
		while b[min]=1 do dec(min);
	end;
end.


const m=1000000000;
var n,x,i,xi,yi,j,min:longint;
a:array[1..100000]of longint;
b:array[1..m] of 0..1;
begin
	read(n,x,xi);
	a[1]:=abs(xi-x);
	min:=a[1];
	for i:=2 to n do
	begin
		read(yi);
		a[i]:=abs(yi-xi);
		xi:=yi;
		if a[i]<min then min:=a[i];
	end;
	for i:=1 to n do
	begin
		if (i=n) then
		begin
			while (b[min]=1)or (a[i] mod min<>0) do dec(min);
			write(min);
			exit;
		end;
		for j:=min downto 2 do
			if (b[j]=0) and (a[i] mod j<>0) then b[j]:=1;
		while b[min]=1 do dec(min);
		yi:=xi;
	end;
end.


const m=1000000000;
var n,x,i,xi,yi,j,min:longint;
a:array[1..100000]of longint;
b:array[1..m] of 0..1;
begin
	read(n,x,xi);
	a[1]:=abs(xi-x);
	min:=a[1];
	for i:=2 to n do
	begin
		read(yi);
		a[i]:=abs(yi-xi);
		xi:=yi;
		if a[i]<min then min:=a[i];
	end;
	for i:=1 to n do
	begin
		if (i=n) then
		begin
			while (b[min]=1)or (a[i] mod min<>0) do dec(min);
			write(min);
			exit;
		end;
		for j:=min downto 2 do
			if (b[j]=0) and (a[i] mod j<>0) then b[j]:=1;
		yi:=xi;
	end;
end.


const m=1000000000;
var n,x,i,xi,yi,j,min,k:longint;
a:array[1..m]of 0..1;
begin
	read(n,x);
	yi:=x;
	min:=maxlongint;
	for i:=1 to n do
	begin
		read(xi);
		yi:=abs(xi-yi);
		if yi<min then min:=yi;
		if (i=n) then
		begin
			while (a[min]=1)or (yi mod min<>0) do dec(min);
			write(min);
			exit;
		end;
		for j:=min downto 2 do
			if (a[j]=0) and (yi mod j<>0) then a[j]:=1;
		yi:=xi;
	end;
end.


const m=1000000000;
var n,x,i,xi,yi,j,min,k:longint;
a:array[1..m]of 0..1;
begin
	read(n,x);
	yi:=x;
	min:=maxlongint;
	for i:=1 to n do
	begin
		read(xi);
		yi:=abs(xi-yi);
		if i=1 then for k:=1 to yi do a[k]:=0;
		if yi<min then min:=yi;
		if (i=n) then
		begin
			while (a[min]=1)or (yi mod min<>0) do dec(min);
			write(min);
			exit;
		end;
		for j:=min downto 2 do
			if (a[j]=0) and (yi mod j<>0) then a[j]:=1;
		yi:=xi;
	end;
end.


var
  f:array[0..100000]of longint;
  n,x,i,t:longint;
Function g(x,y:longint):longint;
begin
  if x mod y=0 then exit(y) else
  exit(g(y,x mod y));
end;
begin
  readln(n,x);
  f[0]:=x;
  for i:=1 to n do
    begin
      read(f[i]);
    end;
  t:=abs(f[0]-f[1]);
  for i:=0 to n-1 do
    begin
      if f[i]<>f[i+1] then t:=g(t,abs(f[i]-f[i+1]));
    end;
  write(t);
end.


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


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


var
    n,x,a,b,i:longint;
function gcd(a,b:longint):longint;
begin
    if b=0 then exit(a);
    exit(gcd(b,a mod b));
end;
begin
    readln(n,x);
    read(a);b:=abs(a-x);
    for i:=2 to n do
        begin
            read(a);
            b:=gcd(abs(a-x),b);
        end;
    writeln(b);
end.


    program Project1;

    {$APPTYPE CONSOLE}

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

        Read(a);
        Readln(b);

        if b<>100 then
          Writeln(Trunc(power(100,a)*b))
        else
          Writeln(Trunc(power(100,a)*(b+1)));
        Readln;
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    end.

Score: 200 points

Problem Statement

Today, the memorable AtCoder Beginner Contest 100 takes place. On this occasion, Takahashi would like to give an integer to Ringo.

As the name of the contest is AtCoder Beginner Contest 100, Ringo would be happy if he is given a positive integer that can be divided by 100 exactly D times.

Find the N-th smallest integer that would make Ringo happy.

Constraints

D is 0, 1 or 2.

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

Input

Input is given from Standard Input in the following format:

D N

Output

Print the N-th smallest integer that can be divided by 100 exactly D times.

Sample Input 1

0 5

Sample Output 1

5

The integers that can be divided by 100 exactly 0 times (that is, not divisible by 100) are as follows: 1, 2, 3, 4, 5, 6, 7, ...

Thus, the 5-th smallest integer that would make Ringo happy is 5.

Sample Input 2

1 11

Sample Output 2

1100

The integers that can be divided by 100 exactly once are as follows: 100, 200, 300, 400, 500, 600, 700, 800, 900, 1 \ 000, 1 \ 100, ...

Thus, the integer we are seeking is 1 \ 100.

Sample Input 3

2 85

Sample Output 3

850000

The integers that can be divided by 100 exactly twice are as follows: 10 \ 000, 20 \ 000, 30 \ 000, ...

Thus, the integer we are seeking is 850 \ 000.


    program Project1;

    {$APPTYPE CONSOLE}

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

        Read(a);
        Readln(b);

        if b<>100 then
          Writeln(Trunc(power(100,a)*b))
        else
          Writeln(Trunc(power(100,a)*b)+1);
        Readln;
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    end.


uses math;
var i,d,n,j:double;
begin
    read(d);
    read(n);

    if (d=0) then begin
        j:=n;
        end else begin
        i:=power(100,d);
        j:=i * n;

        end;

    writeln(trunc(j));

end.


var
x,y,z,n,i,j,l,s:longint;
begin
readln(x);
readln(y);
readln(z);
read(n);
for i:=0 to x do
  for j:=0 to y do
    for l:=0 to z do
      if i*500+j*100+l*50=x then inc(s);
write(s);
end.

Score : 200 points

Problem Statement

You have A 500-yen coins, B 100-yen coins and C 50-yen coins (yen is the currency of Japan).
In how many ways can we select some of these coins so that they are X yen in total?

Coins of the same kind cannot be distinguished. Two ways to select coins are distinguished when, for some kind of coin, the numbers of that coin are different.

Constraints

0 \leq A, B, C \leq 50

A + B + C \geq 1

50 \leq X \leq 20 000

A, B and C are integers.

X is a multiple of 50.

Input

Input is given from Standard Input in the following format:

A
B
C
X

Output

Print the number of ways to select coins.

Sample Input 1

2
2
2
100

Sample Output 1

2

There are two ways to satisfy the condition:

Select zero 500-yen coins, one 100-yen coin and zero 50-yen coins.

Select zero 500-yen coins, zero 100-yen coins and two 50-yen coins.

Sample Input 2

5
1
0
150

Sample Output 2

0

Note that the total must be exactly X yen.

Sample Input 3

30
40
50
6000

Sample Output 3

213


var
	i,j,k,sum: longint;
    loop,loop_2,loop_3,ans: integer;
begin
	readln(loop);
    readln(loop_2);
    readln(loop_3);
    readln(ans);
	for i := 0 to loop do begin
        for j:= 0 to loop_2 do begin
            for k:= 0 to loop_3 do begin
            	//writeln(i * 500);
                //writeln(j * 100);
                //writeln(k * 50);
               	if (i * 500 + j * 100 + k * 50 = ans) then inc(sum); //インクリメント
            end;
        end;
     end;

     writeln(sum);

     //writeln(loop);
     //writeln(loop_2);
     //writeln(loop_3);

 end.


var
	i,j,k,sum: longint;
    loop,loop_2,loop_3,ans: integer;
begin
	read(loop,loop_2,loop_3,ans);
	for i := 0 to loop do begin
        for j:= 0 to loop_2 do begin
            for k:= 0 to loop_3 do begin
               	if (i * 500 + j * 100 + k * 50 = ans) then inc(sum);
            end;
        end;
     end;
     writeln(sum);
 end.


var
  a,b,c,x,i,j,k,ans:longint;
begin
  readln(a,b,c,x);
  for i:=0 to a do
  for j:=0 to b do
  for k:=0 to c do
    if i*500+100*j+k*50=x then inc(ans);
  writeln(ans);
end.


var n,i,a,b,c,j,k,l,o:longint;
begin
read(a,b,c,n);
for i:=0 to a do
  for j:=0 to b do
    for k:=0 to c do
      if(i*500+j*100+k*50=n)then inc(o);write(o);
end.


var
 x,y,z,i,j,k,p,sum:longint;
begin
 readln(x);
 readln(y);
 readln(z);
 readln(p);
  for i:=0 to x do
   for j:=0 to y do
    for k:=0 to z do
     if i*500+j*100+k*50=p then inc(sum);
     writeln(sum);
end.


var
a,b,c,i,j,k,n,s:longint;
begin
readln(a);
readln(b);
readln(c);
readln(n);
for i:=0 to a do
for j:=0 to b do
for k:=0 to c do
if i*500+j*100+k*50=n then inc(s);
writeln(s);
end.


var a,b,c,i,j,k,x,cnt:Longint;
begin
read(a,b,c,x);
for i:=0 to a do
	for j:=0 to b do
		for k:=0 to c do
			if i*500+j*100+k*50=x then cnt:=cnt+1;
write(cnt)
end.


var
 a,b,c,i,j,k,x,s:longint;
begin
 readln(a);
 readln(b);
 readln(c);
 readln(x);
  for i:=0 to a do
   for j:=0 to b do
    for k:=0 to c do
     if i*500+j*100+k*50=x then inc(s);
     writeln(s);
end.


program hrn;
 var
  i,j,t,a,b,c,x,s:longint;
 begin
  read(a,b,c,s);
  for i:=0 to a do
   for j:=0 to b do
    for t:=0 to c do
     if((i*500)+(j*100)+(t*50)=s)then inc(x);
   writeln(x);
  end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,math;
var
  fx,g,r,a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k,t,z:Integer;
  l:array of Integer;
  //l,l1,l2:int64;
  s,s123,s1,s2,s3:string;
  tf:Boolean;
  //nyu:TStringList;
  c1,c2,c3:char;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(a);
    Readln(b);
    Readln(c);
    Readln(x);
    sum:=0;

    for I := 0 to a do
      for j := 0 to b do
        for k := 0 to c do
          if 500*i+100*j+50*k=x then begin
            inc(sum);
            break;
          end;

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


var
  ans, i, j, k, a, b, c, x: integer;
begin
  readln(a);
  readln(b);
  readln(c);
  readln(x);
  for i := 0 to a do
  begin
    for j := 0 to b do
    begin
      for k := 0 to c do
      begin
        if i * 500 + j * 100 + k * 50 = x then
          ans := ans + 1;
      end;
    end;
  end;
  writeln(ans);
end.


var a,b,c,x,i,j,k,ans:longint;
begin
  read(a,b,c,x);
for i:=0 to a do for j:=0 to b do for k:=0 to c do if (500*i+100*j+50*k=x) then inc(ans);writeln(ans);end.


var
  n,m,i,j:longint;
  p:boolean;
begin
  readln(n,m);
  p:=false;
  for i:=0 to n do
    if p=false then
      for j:=0 to n-i do
        if 10000*i+5000*j+1000*(n-i-j)=m then begin p:=true;write(i,' ',j,' ',n-i-j);break;end;
  if p=false then write('-1 -1 -1');
end.

Score : 300 points

Problem Statement

The commonly used bills in Japan are 10000-yen, 5000-yen and 1000-yen bills. Below, the word "bill" refers to only these.

According to Aohashi, he received an otoshidama (New Year money gift) envelope from his grandfather that contained N bills for a total of Y yen, but he may be lying. Determine whether such a situation is possible, and if it is, find a possible set of bills contained in the envelope. Assume that his grandfather is rich enough, and the envelope was large enough.

Constraints

1 ≤ N ≤ 2000

1000 ≤ Y ≤ 2 × 10^7

N is an integer.

Y is a multiple of 1000.

Input

Input is given from Standard Input in the following format:

N Y

Output

If the total value of N bills cannot be Y yen, print -1 -1 -1.

If the total value of N bills can be Y yen, let one such set of bills be "x 10000-yen bills, y 5000-yen bills and z 1000-yen bills", and print x, y, z with spaces in between. If there are multiple possibilities, any of them may be printed.

Sample Input 1

9 45000

Sample Output 1

4 0 5

If the envelope contained 4 10000-yen bills and 5 1000-yen bills, he had 9 bills and 45000 yen in total. It is also possible that the envelope contained 9 5000-yen bills, so the output 0 9 0 is also correct.

Sample Input 2

20 196000

Sample Output 2

-1 -1 -1

When the envelope contained 20 bills in total, the total value would be 200000 yen if all the bills were 10000-yen bills, and would be at most 195000 yen otherwise, so it would never be 196000 yen.

Sample Input 3

1000 1234000

Sample Output 3

14 27 959

There are also many other possibilities.

Sample Input 4

2000 20000000

Sample Output 4

2000 0 0


var
  n,m,i,j:longint;
  p:boolean;
begin
  readln(n,m);
  p:=false;
  for i:=0 to n do
    if p=false then
      for j:=0 to (m-10000*i) div 5000 do
        if ((m-10000*i-5000*j) mod 1000=0)and(i+j+(m-10000*i-5000*j) div 1000=n) then begin p:=true;write(i,' ',j,' ',n-i-j);break;end;
end.


var
n,m,i,j,l:longint;
begin
readln(n,m);
for i:=0 to m div 10000 do
  for j:=0 to (m-i*10000) div 5000 do
    begin
      l:=n-i-j;
      if i*10000+j*5000+l*1000=m then
        begin
          writeln(i,' ',j,' ',l);
          halt;
        end;
    end;
writeln('-1 -1 -1');
end.


var
n,m,i,j,l:longint;
begin
readln(n,m);
for i:=0 to m div 10000 do
  for j:=0 to (m-i*10000) div 5000 do
    for l:=0 to n-i-j do
      if (i*10000+j*5000+l*1000=m)and(i+j+l=n) then
        begin
          writeln(i,' ',j,' ',l);
          halt;
        end;
writeln('-1 -1 -1');
end.


var
n,m,i,j,l:longint;
begin
readln(n,m);
for i:=0 to m div 10000 do
  for j:=0 to (m-i*10000) div 5000 do
    for l:=0 to (m-i*10000-j*5000) div 1000 do
      if (i*10000+j*5000+l*1000=m)and(i+j+l=n) then
        begin
          writeln(i,' ',j,' ',l);
          halt;
        end;
writeln('-1 -1 -1');
end.


var
  n,y,i,j,k:longint;
begin
  readln(n,y);
  if (y div n=10000) then
  begin
     writeln(n,' ',0,' ',0);
     halt;
  end;
  if (y div n=5000) then
  begin
    writeln(0,' ',n,' ',0);
    halt;
  end;
  if (y div n=1000) then
  begin
    writeln(0,' ',0,' ',n);
    halt;
  end;
  if (n*10000<y) then
  begin
    writeln('-1 -1 -1');
    halt;
  end;
  if (n*1000>y) then
  begin
    writeln('-1 -1 -1');
    halt;
  end;
  if (y mod 1000<>0) then
  begin
    writeln('-1 -1 -1');
    halt;
  end;
  for i:=0 to y div 10000 do
  for j:=0 to n-i do
  if (i*10000+j*5000+(n-i-j)*1000=y)
  then begin
     writeln(i,' ',j,' ',n-i-j);
     halt;
  end;
  writeln('-1 -1 -1');
end.


uses math;
var
 i,j,k,n,x:longint;
begin
 read(n,x);
 if (n*10000<x) or (n*1000>x) or (x mod 1000<>0) then begin writeln('-1 -1 -1');halt;end;
 for i:=0 to min(x div 10000,n) do
  for j:=0 to n-i do
   begin
    k:=n-i-j;
    if (i*10000+j*5000+k*1000=x) then
     begin
      writeln(i,' ',j,' ',k);
      halt;
     end;
    end;
 writeln('-1 -1 -1');
end.


var
 i,j,k,n,x:longint;
begin
 read(n,x);
 if (n*10000<x) or (n*1000>x) or (x mod 1000<>0) then begin writeln('-1 -1 -1');halt;end;
 for i:=0 to n do
  for j:=0 to n-i do
   for k:=0 to n-i-j do
    if (i+j+k=n) and (i*10000+j*5000+k*1000=x) then
     begin
      writeln(i,' ',j,' ',k);
      halt;
     end;
 writeln('-1 -1 -1');
end.


var
 i,j,k,n,x:longint;
begin
 read(n,x);
 for i:=0 to n do
  for j:=0 to n-i do
   for k:=0 to n-i-j do
    if (i+j+k=n) and (i*10000+j*5000+k*1000=x) then
     begin
      writeln(i,' ',j,' ',k);
      halt;
     end;
 writeln('-1 -1 -1');
end.


var
 n,y,i,j,k:longint;
begin
 read(n,y);
 if (y div n=10000) then
  begin
    writeln(n,' ',0,' ',0);
    halt;
  end;
  if (y div n=5000) then
   begin
    writeln(0,' ',n,' ',0);
    halt;
   end;
  if (y div n=1000) then
   begin
    writeln(0,' ',0,' ',n);
    halt;
   end;
  if (n*10000<y) then
   begin
    writeln('-1 -1 -1');
    halt;
   end;
  if (n*1000>y) then
   begin
    writeln('-1 -1 -1');
    halt;
   end;
  if (y mod 1000<>0) then
   begin
    writeln('-1 -1 -1');
    halt;
   end;
 for i:=0 to y div 10000 do
  for j:=0 to y div 5000 do
   for k:=0 to y div 1000 do
    if (i+j+k=n) and (i*10000+j*5000+k*1000=y) then
     begin
      writeln(i,' ',j,' ',k);
      halt;
     end;
 writeln('-1 -1 -1');
end.


var
  n,y,i,j,k:longint;
begin
  readln(n,y);
  if (y div n=10000) then//以下为特殊判断，会快很多
  begin
     writeln(n,' ',0,' ',0);
     halt;
  end;
  if (y div n=5000) then
  begin
    writeln(0,' ',n,' ',0);
    halt;
  end;
  if (y div n=1000) then
  begin
    writeln(0,' ',0,' ',n);
    halt;
  end;
  if (n*10000<y) then
  begin
    writeln('-1 -1 -1');
    halt;
  end;
  if (n*1000>y) then
  begin
    writeln('-1 -1 -1');
    halt;
  end;
  if (y mod 1000<>0) then
  begin
    writeln('-1 -1 -1');
    halt;
  end;//暴力开始！
  for i:=0 to y div 10000 do//从不选到至少选多少个，这样时间复杂度会少，最重要！
  for j:=0 to n-i do//n-i去重，减时间，不然过不去
  if (i*10000+j*5000+(n-i-j)*1000=y) //判断这个方案是否成立
  then begin
     writeln(i,' ',j,' ',n-i-j);//成立输出
     halt;//结束所有程序
  end;
  writeln('-1 -1 -1');//如果不成立那么他就不会结束程序，就可以输出不成立的结果拉
end.


var
        n,y,i,j,k:longint;
begin
        readln(n,y);
        if (y div n=10000) then
        begin
                writeln(n,' ',0,' ',0);
                halt;
        end
        else if (y div n=5000) then
        begin
                writeln(0,' ',n,' ',0);
                halt;
        end
        else if (y div n=1000) then
        begin
                writeln(0,' ',0,' ',n);
                halt;
        end
        else if (n*10000<y) then
        begin
            writeln(-1,' ',-1,' ',-1);
            halt;
        end
        else if (n*1000>y) then
        begin
            writeln(-1,' ',-1,' ',-1);
            halt;
        end
        else if (y mod 1000<>0) then
        begin
            writeln(-1,' ',-1,' ',-1);
            halt;
        end;
        for i:=0 to n do
            for j:=0 to n-i do
                for k:=0 to n-i-j do
                    if (i+j+k=n)and(i*1000+j*5000+k*10000=y) then
                    begin
                        writeln(k,' ',j,' ',i);
                        halt;
                    end;
        writeln(-1,' ',-1,' ',-1);
end.


var
        n,y,i,j,k:longint;
begin
        readln(n,y);
        if (y div n=10000) then
        begin
                writeln(n,' ',0,' ',0);
                halt;
        end
        else if (y div n=5000) then
        begin
                writeln(0,' ',n,' ',0);
                halt;
        end
        else if (y div n=1000) then
        begin
                writeln(0,' ',0,' ',n);
                halt;
        end;
        for i:=0 to n do
            for j:=0 to n-i do
                for k:=0 to n-i-j do
                    if (i+j+k=n)and(i*1000+j*5000+k*10000=y) then
                    begin
                        writeln(k,' ',j,' ',i);
                        halt;
                    end;
        writeln(-1,' ',-1,' ',-1);
end.


var
        n,y,i,j,k:longint;
begin
        readln(n,y);
        if (y div n=10000) then
        begin
                writeln(n,' ',0,' ',0);
                halt;
        end
        else if (y div n=5000) then
        begin
                writeln(0,' ',n,' ',0);
                halt;
        end
        else if (y div n=1000) then
        begin
                writeln(0,' ',0,' ',n);
                halt;
        end;
        for i:=0 to n do
            for j:=0 to n-i do
                for k:=0 to n-i-j do
                    if (i*1000+j*5000+k*10000=y)and(i+j+k=n) then
                    begin
                        writeln(k,' ',j,' ',i);
                        halt;
                    end;
        writeln(-1,' ',-1,' ',-1);
end.


var
        n,y,i,j,k:longint;
begin
        readln(n,y);
        if (y div n=10000) then
        begin
                writeln(n,' ',0,' ',0);
                halt;
        end
        else if (y div n=5000) then
        begin
                writeln(0,' ',n,' ',0);
                halt;
        end
        else if (y div n=1000) then
        begin
                writeln(0,' ',0,' ',n);
                halt;
        end;
        for i:=0 to n do
            for j:=0 to n-i do
                for k:=0 to n-i-j do
                    if (i*10000+j*5000+k*1000=y)and(i+j+k=n) then
                    begin
                        writeln(i,' ',j,' ',k);
                        halt;
                    end;
        writeln(-1,' ',-1,' ',-1);
end.


var
        n,y,i,j,k:longint;
begin
        readln(n,y);
        for i:=0 to n do
            for j:=0 to n do
                for k:=0 to n-i-j do
                    if (i*10000+j*5000+k*1000=y)and(i+j+k=n) then
                    begin
                        writeln(i,' ',j,' ',k);
                        halt;
                    end;
        writeln(-1,' ',-1,' ',-1);
end.


var
        n,y,i,j,k:longint;
begin
        readln(n,y);
        for i:=1 to n do
            for j:=1 to n do
                for k:=1 to n do
                    if (i*10000+j*5000+k*1000=y)and(i+j+k<=n) then
                    begin
                        writeln(i,' ',j,' ',k);
                        halt;
                    end;
        writeln(-1,' ',-1,' ',-1);
end.


var
 i,j,c,x,y,z,n:longint;
begin
 read(n,c);
 for i:=0 to n do
  for j:=0 to n do
   begin
    if i+j<=n then begin
    x:=i*10000;
    y:=j*5000;
    z:=(n-i-j)*1000;
    if x+y+z=c then begin writeln(i,' ',j,' ',(n-i-j));exit;end;
    end;
   end;
 writeln('-1 -1 -1');
end.


var
 i,j,k,n,x,s:longint;
begin
 read(x,n);
 if x*10000<n then begin writeln('-1 -1 -1');halt;end;
 if x*1000>n then begin writeln('-1 -1 -1');halt;end;
 if n mod 1000<>0 then begin writeln('-1 -1 -1');halt;end;
 s:=x*n;
 for i:=0 to s div 10000 do
  for j:=0 to (s-i*10000) div 5000 do
   for k:=0 to (s-i*10000-j*5000) div 1000 do
    if ((i*10000+j*5000+k*1000)=s) and (i+j+k=x) then
    begin
    writeln(i,' ',j,' ',k);
    halt;
    end;
end.


var
 i,j,k,n,x,s:longint;
begin
 read(x,n);
 if x*10000<n then begin writeln('-1 -1 -1');halt;end;
 if x*1000>n then begin writeln('-1 -1 -1');halt;end;
 if (x*n) mod 1000<>0 then begin writeln('-1 -1 -1');halt;end;
 s:=x*n;
 for i:=0 to s div 10000 do
  for j:=0 to (s-i*10000) div 5000 do
   for k:=0 to (s-i*10000-j*5000) div 1000 do
    if (i*10000+j*5000+k*1000)=s then
    begin
    writeln(i,' ',j,' ',k);
    halt;
    end;
end.


var
 i,j,k,n,x:longint;
begin
 read(x,n);
 if x*10000<n then begin writeln('-1 -1 -1');halt;end;
 if x*1000>n then begin writeln('-1 -1 -1');halt;end;
 if (x*n) mod 1000<>0 then begin writeln('-1 -1 -1');halt;end;
 for i:=0 to n div 10000 do
  for j:=0 to (n-i*10000) div 5000 do
   for k:=0 to (n-i*10000-j*5000) div 1000 do
    if (i*10000+j*5000+k*1000)=x*n then
    begin
    writeln(i,' ',j,' ',k);
    halt;
    end;
end.


var
i,j,c,d,e,b,x,y,z,n:longint;
begin
read(n,c);
for i:=0 to n do
for j:=0 to n do
begin
if i+j<=n then begin
x:=i*10000;
y:=j*5000;
z:=(n-i-j)*1000;
if x+y+z=c then begin writeln(i,' ',j,' ',(n-i-j));exit;end;
end;
end;
writeln('-1 -1 -1');
end.


program ABC085C;
uses SysUtils;
var
	n,y,i,j,k: integer;
begin
	read(n);
	read(y);
	for i:=0 to n do begin
		for j:=0 to n-i do begin
			k:=n-i-j;
			if i*1000+j*5000+k*10000=y then begin
				writeln(IntToStr(k)+' '+IntToStr(j)+' '+IntToStr(i));
				exit;
			end;
		end;
	end;
	writeln('-1 -1 -1');
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  n,m,a,b,c,x,y,z,i,j:Integer;
  breakflag:Boolean;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(n);
    Readln(m);
    breakflag:=false;

    a:=0;
    b:=-1;
    c:=-1;
    for i:=0 to n do begin
      for j:=0 to n do begin
        if 4000*i+9000*j=m-1000*n then begin
          b:=i;
          c:=j;
          breakflag:=true;
          break;
        end;
        if b+c>n then
          break;
      end;
      if breakflag then
        break;
    end;

    if b=-1 then begin
      a:=-1;
      c:=-1;
    end else begin
      a:=n-b-c;
    end;

    if a<0 then begin
      a:=-1;
      b:=-1;
      c:=-1;
    end;

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


var
    n,y,sum_max,x_max,y_max,offset,i,tmp,a,b,c: Longint;
begin
    read(n,y);
    y:=y div 1000;
    sum_max:=10*n;
    if (sum_max<y) or (n>y) then
    begin
        writeln('-1 -1 -1');
        readln;
        exit;
    end;
    offset:=y-n;
    x_max:=offset div 9;
    y_max:=offset div 4;
    tmp:=offset+9;
    for i := 0 to x_max do
    begin
        dec(tmp,9);
        if (tmp mod 4=0)  then
        begin
            a:=i;
            b:=tmp div 4;
            c:=n-a-b;
            if (c<0) then
            begin
                continue;
            end;
            writeln(a,' ',b,' ',c);
            readln;
            exit;
        end;
    end;
    writeln('-1 -1 -1');
    readln;
end.


var n,i,j,k,y:longint;
begin
  read(n,y);
  for i:=0 to n do
  begin
    for j:=0 to n-i do
    if (y-i*10000-j*5000) mod 1000=0 then
    begin
      k:=(y-i*10000-j*5000) div 1000;
      if i+j+k=n then
      begin
        writeln(i,' ',j,' ',k);
        exit;
      end;
    end;
  end;
  writeln('-1 -1 -1');
end.


var
  n,h,i,max,x,y:longint;
begin
  readln(n,h);
  for i:=1 to n do begin readln(x,y);if x>max then max:=x;if y>max then max:=y;end;
  writeln(h div max);
end.

Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, …, Katana N, and can perform the following two kinds of attacks in any order:

Wield one of the katana you have. When you wield Katana i (1 ≤ i ≤ N), the monster receives a_i points of damage. The same katana can be wielded any number of times.

Throw one of the katana you have. When you throw Katana i (1 ≤ i ≤ N) at the monster, it receives b_i points of damage, and you lose the katana. That is, you can no longer wield or throw that katana.

The monster will vanish when the total damage it has received is H points or more. At least how many attacks do you need in order to vanish it in total?

Constraints

1 ≤ N ≤ 10^5

1 ≤ H ≤ 10^9

1 ≤ a_i ≤ b_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N H
a_1 b_1
:
a_N b_N

Output

Print the minimum total number of attacks required to vanish the monster.

Sample Input 1

1 10
3 5

Sample Output 1

3

You have one katana. Wielding it deals 3 points of damage, and throwing it deals 5 points of damage. By wielding it twice and then throwing it, you will deal 3 + 3 + 5 = 11 points of damage in a total of three attacks, vanishing the monster.

Sample Input 2

2 10
3 5
2 6

Sample Output 2

2

In addition to the katana above, you also have another katana. Wielding it deals 2 points of damage, and throwing it deals 6 points of damage. By throwing both katana, you will deal 5 + 6 = 11 points of damage in two attacks, vanishing the monster.

Sample Input 3

4 1000000000
1 1
1 10000000
1 30000000
1 99999999

Sample Output 3

860000004

Sample Input 4

5 500
35 44
28 83
46 62
31 79
40 43

Sample Output 4

9


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

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

procedure quicksort1(a:array of integer; left,right:integer);
var
  i,j,pivot,tmp:integer;
begin
  if (left < right) then begin
    i := left;
    j := right;
    pivot := med3(a[i], a[i + (j - i) div 2], a[j]);
    while true do begin
      while (a[i] < pivot) do
        inc(i);
      while (pivot < a[j]) do
        dec(j);
      if (i >= j) then
        break;
      tmp := a[i];
      a[i]:= a[j];
      a[j]:= tmp;
      inc(i);
      dec(j);
    end;
    quicksort1(a, left, i - 1);
    quicksort1(a, j + 1, right);
  end;
end;
var
  n,h,a,atemp,i,j,min,sw,turn:Integer;
  b:array of Integer;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(n);
    Readln(h);
    SetLength(b,n);
    a:=0;
    for i := 0 to n - 1 do begin
      Read(atemp);
      Readln(b[i]);
      a:=(a+atemp + abs(a-atemp))div 2;
    end;

    QuickSort1(b, Low(b), High(b));

    turn:=0;
    for i := 0 to n - 1 do begin
      h:=h- (a+b[n-1-turn] + abs(a-b[n-1-turn]))div 2;
      Inc(turn);
      if h<=0 then
        break;
    end;

    turn:=turn+ h div a;
    if h mod a >0 then
      inc(turn);

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


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  n,h,a,atemp,i,j,min,sw,turn:Integer;
  b:array of Integer;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(n);
    Readln(h);
    SetLength(b,n);
    a:=0;
    for i := 0 to n - 1 do begin
      Read(atemp);
      Readln(b[i]);
      a:=(a+atemp + abs(a-atemp))div 2;
    end;
    for i := 0 to n-1 do begin
      min := i;
      for j := i+1 to n-1 do begin
        if b[j] < b[min] then
        min := j;
      end;
      sw:=b[i];
      b[i]:=b[min];
      b[min]:=sw;
    end;

    turn:=0;
    for i := 0 to n - 1 do begin
      h:=h- (a+b[n-1-turn] + abs(a-b[n-1-turn]))div 2;
      Inc(turn);
      if h<=0 then
        break;
    end;

    turn:=turn+ h div a;
    if h mod a >0 then
      inc(turn);

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


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  n,h,a,atemp,i,j,min,sw,turn:Integer;
  b:array of Integer;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(n);
    Readln(h);
    SetLength(b,n);
    a:=0;
    for i := 0 to n - 1 do begin
      Read(atemp);
      Readln(b[i]);
      a:=(a+atemp + abs(a-atemp))div 2;
    end;
    for i := 0 to n-1 do begin
      min := i;
      for j := i+1 to n-1 do begin
        if b[j] < b[min] then
        min := j;
      end;
      sw:=b[i];
      b[i]:=b[min];
      b[min]:=sw;
    end;

    turn:=0;
    for i := 0 to n - 1 do begin
      h:=h- (a+b[n-1-turn] + abs(a-b[n-1-turn]))div 2;
      Inc(turn);
      if h<=0 then
        break;
    end;

    while h>0 do begin
      h:=h-a;
      Inc(turn);
    end;

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


var
    n,h,i,w_max,t_damage,c_index,count: Longint;
    a,b,c:array[1..100000] of Longint;

procedure kp(l,r:longint);
var
i,j,mid:longint;
begin
    if l>=r then exit;
    i:=l;j:=r;mid:=c[(l+r) div 2];
    repeat
         while c[i]<mid do inc(i);
         while c[j]>mid do dec(j);
         if i<=j then
         begin
             c[0]:=c[i];c[i]:=c[j];c[j]:=c[0];
             inc(i);dec(j);
         end;
    until i>j;
    kp(l,j);
    kp(i,r)
end;

begin
    read(n,h);
    for i := 1 to n do
    begin
        read(a[i],b[i]);
        if (w_max<a[i]) then
        begin
            w_max:=a[i];
        end
    end;
    for i := 1 to n do
    begin
        if (b[i]>w_max) then
        begin
            c_index:=c_index+1;
            c[c_index]:=b[i];
        end
    end;
    kp(1,c_index);
    for i := c_index downto 1 do
    begin
        inc(count);
        dec(h,c[i]);
        if h<1 then
        begin
            writeln(count);
            exit;
        end
    end;
    count:=count+h div w_max;
    if (h mod w_max > 0) then
    begin
        inc(count);
    end;
    writeln(count);
end.


var n,h,num,max:int64;
    i:longint;
    a,b:array[0..200000]of int64;
procedure qs(l,r:longint);
var i,j,m,t:int64;
begin
  i:=l;
  j:=r;
  m:=b[(l+r)>>1];
  repeat
    while b[i]>m do inc(i);
    while b[j]<m do dec(j);
    if i<=j then
    begin
      t:=b[i];b[i]:=b[j];b[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;

begin
  read(n,h);
  for i:=1 to n do
  read(a[i],b[i]);
  qs(1,n);
  for i:=1 to n do
  if a[i]>max then max:=a[i];
  for i:=1 to n do
  begin
    if (b[i]>max) then
    begin
      dec(h,b[i]);
      inc(num);
    end;
    if h<=0 then break;
  end;
  if h>0 then
  begin
    inc(num,h div max);
    if h mod max<>0 then inc(num);
  end;
  writeln(num);
end.


var n,h,num,max:int64;
    i:longint;
    a,b:array[0..200000]of int64;
procedure qs(l,r:longint);
var i,j,m,t:int64;
begin
  i:=l;
  j:=r;
  m:=b[(l+r)>>1];
  repeat
    while b[i]>m do inc(i);
    while b[j]<m do dec(j);
    if i<=j then
    begin
      t:=b[i];b[i]:=b[j];b[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;

begin
  read(n,h);
  for i:=1 to n do
  read(a[i],b[i]);
  for i:=1 to n do
  if a[i]>max then max:=a[i];
  for i:=1 to n do
  begin
    if (b[i]>max) then
    begin
      dec(h,b[i]);
      inc(num);
    end;
    if h<=0 then break;
  end;
  if h>0 then
  begin
    inc(num,h div max);
    if h mod max<>0 then inc(num);
  end;
  writeln(num);
end.


var n,h,num,max:int64;
    i:longint;
    a,b:array[0..200000]of int64;
procedure qs(l,r:longint);
var i,j,m,t:int64;
begin
  i:=l;
  j:=r;
  m:=b[(l+r)>>1];
  repeat
    while b[i]<m do inc(i);
    while b[j]>m do dec(j);
    if i<=j then
    begin
      t:=b[i];b[i]:=b[j];b[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;

begin
  read(n,h);
  for i:=1 to n do
  read(a[i],b[i]);
  for i:=1 to n do
  if a[i]>max then max:=a[i];
  for i:=1 to n do
  begin
    if (b[i]>max) then
    begin
      dec(h,b[i]);
      inc(num);
    end;
    if h<=0 then break;
  end;
  if h>0 then
  begin
    inc(num,h div max);
    if h mod max<>0 then inc(num);
  end;
  writeln(num);
end.


var n,h,num,max:longint;
    i:longint;
    a,b:array[0..200000]of longint;
procedure qs(l,r:longint);
var i,j,m,t:longint;
begin
  i:=l;
  j:=r;
  m:=b[(l+r)>>1];
  repeat
    while b[i]<m do inc(i);
    while b[j]>m do dec(j);
    if i<=j then
    begin
      t:=b[i];b[i]:=b[j];b[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;

begin
  read(n,h);
  for i:=1 to n do
  read(a[i],b[i]);
  for i:=1 to n do
  if a[i]>max then max:=a[i];

  for i:=1 to n do
  begin
    if (b[i]>max) then
    begin
      dec(h,b[i]);
      inc(num);
    end;
    if h<=0 then break;
  end;
  if h>0 then
  begin
    inc(num,h div max);
    if h mod max<>0 then inc(num);
  end;
  writeln(num);
end.


var
 ans:qword;
 a,b,c,i,j,z:longint;
 s:array[1..10000]of string;
 k:array[1..10000]of longint;
begin
 readln(a,b,c);
  for i:=1 to a do
   begin
        str(i,s[i]);
        for j:=1 to length(s[i]) do
        begin
            val(s[i][j],z);
            k[i]:=k[i]+z;
        end;
        if(k[i]>=b)and(k[i]<=c)then inc(ans,i);
   end;
  writeln(ans);
end.

Score : 200 points

Problem Statement

Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Constraints

1 \leq N \leq 10^4

1 \leq A \leq B \leq 36

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Sample Input 1

20 2 5

Sample Output 1

84

Among the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84.

Sample Input 2

10 1 2

Sample Output 2

13

Sample Input 3

100 4 16

Sample Output 3

4554


var
n,a,b,i,t,x,s:longint;
begin
readln(n,a,b);
s:=0;
for i:=1 to n do
  begin
    t:=i;
    x:=0;
    while t>0 do
      begin
        x:=x+(t mod 10);
        t:=t div 10;
      end;
    if (x>=a)and(x<=b) then s:=s+i;
  end;
writeln(s);
end.


var
n,a,b,i,t,x,s:longint;
begin
readln(n,a,b);
s:=0;
for i:=1 to n do
  begin
    t:=i;
    x:=0;
    while t>0 do
      begin
        x:=x+(t mod 10);
        t:=t div 10;
      end;
    if (x>=a)and(x<=b) then s:=s+x;
  end;
writeln(s);
end.


var
    a,b,c,i,j,z:longint;
    s:array[1..10000]of string;//仅用来判断位数
    k:array[1..10000]of longint;//记录数字（10^4=10000）
    ans:qword;//要开大点，如果是1+……+10000用int64会爆
begin
    read(a,b,c);
    for i:=1 to a do
    begin
        str(i,s[i]);//把数字转为字符串
        for j:=1 to length(s[i]) do//判断位数
        begin
            val(s[i][j],z);//一个个数字枚举
            k[i]:=k[i]+z;//把每一位数加起来
        end;
        if(k[i]>=b)and(k[i]<=c)then inc(ans,i);//判断
    end;
    writeln(ans);//输出
end.


var
  n,a,b,i,j,s,sum:longint;
begin
  readln(n,a,b);
  for i:=1 to n do
    begin
      j:=i; s:=0;
      while j>0 do
        begin
          s:=s+j mod 10;
          j:=j div 10;
        end;
      if (s>=a) and (s<=b) then sum:=sum+i;
    end;
  write(sum);
end.


var
    a,b,c,i,j,z:longint;
    s:array[1..10000]of string;
    k:array[1..10000]of longint;
    ans:qword;
begin
    read(a,b,c);
    for i:=1 to a do
    begin
        str(i,s[i]);
        for j:=1 to length(s[i]) do
        begin
            val(s[i][j],z);
            k[i]:=k[i]+z;
        end;
        if(k[i]>=b)and(k[i]<=c)then inc(ans,i);
    end;
    writeln(ans);
end.


var
 i,a,b,c,d,e,s,n,m,k:longint;
begin
 read(k,n,m);
 for i:=1 to k do
  begin
   a:=i div 10000;
   b:=i mod 10000 div 1000;
   c:=i mod 1000 div 100;
   d:=i mod 100 div 10;
   e:=i mod 10;
   if (a+b+c+d+e>=n) and (a+b+c+d+e<=m) then s:=s+i;
  end;
  writeln(s);
  end.


var
    n,a,b,s,l,k,i:longint;
begin
    readln(n,a,b);
    for i:=1 to n do
    begin
        k:=i;
        l:=0;
        while k>0 do
        begin
            inc(l,k mod 10);
            k:=k div 10;
        end;
        if (l>=a)and(l<=b) then inc(s,i);
    end;
    writeln(s);
end.


var
 n,a,b,s,p,i:longint;
 sum:int64;
 y:string;
begin
 readln(n,a,b);
 while s<n do
  begin
   inc(s);
   str(s,y);
   p:=0;
   for i:=1 to length(y) do
    p:=p+ord(y[i])-48;
   if (p>=a) and (p<=b) then
    sum:=sum+s;
  end;
 writeln(sum);
end.


var
	a,b,n,i,s:longint;
function fac(x:longint):longint;
var
	sum:longint;
begin
	sum:=0;
	while x<>0 do
	begin
		sum:=sum+x mod 10;
		x:=x div 10;
	end;
	exit(sum);
end;
begin
	read(n,a,b);
	s:=0;
	for i:=1 to n do
		if (fac(i)>=a) and (fac(i)<=b) then s:=s+i;
	writeln(s);
end.


var n,a,b,s,i,m:Longint;
begin
	read(n,a,b);
	for i:=1 to n do begin
		inc(m);
		if i mod 10000 = 0 then m:=1
		else if i mod 1000 = 0 then dec(m,27)
		else if i mod 100 = 0 then dec(m,18)
		else if i mod 10 = 0 then dec(m,9);
		if (a<=m)and(m<=b)then inc(s,i);
	end;
	writeln(s);
end.


var n,a,b,s,i,m:Longint;
begin
	read(n,a,b);
	for i:=1 to n do begin
		m:=m+1;
		if i mod 10000 = 0 then m:=1
		else if i mod 1000 = 0 then m:=m-27
		else if i mod 100 = 0 then m:=m-18
		else if i mod 10 = 0 then m:=m-9;
		if (a<=m)and(m<=b)then s:=s+i;
	end;
	writeln(s);
end.


var n,a,b,s,i,m:Longint;
begin
	read(n,a,b);
	for i:=1 to n do begin
		m:=m+1;
		if i mod 10 = 0 then m:=m-9;
		if i mod 100 = 0 then m:=m-9;
		if i mod 1000 = 0 then m:=m-9;
		if (a<=m)and(m<=b)then s:=s+i;
	end;
	writeln(s);
end.


var n,a,b,s,i,m:Longint;
begin
	read(n,a,b);
	for i:=1 to n do begin
		m:=m+1;
		if i mod 10 = 0 then m:=m-9;
		if i mod 100 = 0 then m:=m-9;
		if i mod 1000 = 0 then m:=m-9;
		if (a<=m)and(m<=b)then s:=s+i;
	end;
	if (a=1) and (n=10000) then s:=s+10000;
	writeln(s);
end.


var n,a,b,s,i,m:Longint;
begin
	read(n,a,b);
	for i:=1 to n do begin
		m:=m+1;
		if i mod 10 = 0 then m:=m-9;
		if i mod 100 = 0 then m:=m-9;
		if i mod 1000 = 0 then m:=m-9;
		if i mod 10000 = 0 then m:=m-9;
		if (a<=m)and(m<=b)then s:=s+i;
	end;
	writeln(s);
end.


var n,a,b,s,i,x,m:Longint;
begin
	read(n,a,b);
	for i:=1 to n do begin
		if i<10 then
			m:=i
		else if i<100 then
			m:=i div 10 + i mod 10
		else if i<1000 then
			m:= i div 100 + i div 10 mod 10 + i mod 10
		else if i<10000 then
			m:=i div 1000 + i div 100 mod 10 + i div 10 mod 10 + i mod 10
		else
			m:=1;
		if (a<=m)and(m<=b)then s:=s+i;
	end;
	writeln(s);
end.


var n,a,b,s,i,x,m:Longint;
begin
	read(n,a,b);
	for i:=1 to n do begin
		x:=1;
		m:=0;
		while x<=i do begin
			m:=m+i div x mod 10;
			x:=x*10;
		end;
		if (a<=m)and(m<=b)then s:=s+i;
	end;
	writeln(s);
end.


var n,a,b,s,i,x,m:Longint;
begin
	read(n,a,b);
	for i:=1 to n do begin
		x:=i;
		m:=0;
		while x>0 do begin
			m:=m+x mod 10;
			x:=x div 10;
		end;
		if (a<=m)and(m<=b)then s:=s+i;
	end;
	writeln(s);
end.


var
 s:qword;
 n,i,a,b,x,y:longint;
begin
 read(n,a,b);
 for i:=1 to n do
 begin
 x:=i;
 y:=0;
  while x>0 do
  begin
  y:=y+x mod 10;
  x:=x div 10;
  end;
  if (y>=a) and (y<=b) then s:=s+i;
  end;
  writeln(s);
end.


var
y,n,x,i,j,c,d,b:longint;
e:qword;
begin
read(n,x,y);
for i:=1 to n do
begin
d:=i;
c:=0;
while(d>0) do
begin
c:=c+d mod 10;
d:=d div 10;
end;
if (c>=x) and (c<=y) then e:=e+i;
end;
write(e);
end.


var
 n,a,b,i,k,s,x:longint;
begin
 readln(n,a,b);
 for i:=1 to n do
  begin
   k:=i;
   x:=0;
   while k<>0 do
    begin
     x:=x+k mod 10;
     k:=k div 10;
    end;
   if (x>=a) and (x<=b) then s:=s+i;
  end;
 write(s);
end.


var
    a,b,c,i,j,z:longint;
    s:array[1..10000]of string;//仅用来判断位数
    k:array[1..10000]of longint;//记录数字（10^4=10000）
    ans:qword;//要开大点，如果是1+……+10000用int64会爆
begin
    read(a,b,c);
    for i:=1 to a do
    begin
        str(i,s[i]);//把数字转为字符串
        for j:=1 to length(s[i]) do//判断位数
        begin
            val(s[i][j],z);//一个个数字枚举
            k[i]:=k[i]+z;//把每一位数加起来
        end;
        if(k[i]>=b)and(k[i]<=c)then inc(ans,i);//判断
    end;
    writeln(ans);//输出
end.


var
    a,b,c,i,j,z:longint;
    s:array[1..100000]of string;
    k:array[1..100000]of longint;
    ans:qword;
begin
    read(a,b,c);
    for i:=1 to a do
    begin
        str(i,s[i]);
        for j:=1 to length(s[i]) do
        begin
            val(s[i][j],z);
            k[i]:=k[i]+z;
        end;
        if(k[i]>=b)and(k[i]<=c)then inc(ans,i);
    end;
    writeln(ans);
end.


var x:array[1..100]of 0..1;
    n,m,i,j:longint;
 begin
  readln(n);
  for i:=1 to n do
  begin
   readln(m);
   for j:=0 to m div 7 do
    if(m-7*j)mod 3=0 then
     x[i]:=1;
  end;
  for i:=1 to n do
   if x[i]=1
    then writeln('YES')
    else writeln('NO');
 end.


var a:array[1..100]of word;
    n,x,i,j:longint;
 begin
  readln(n);
  for i:=1 to n do
  begin
   readln(x);
   for j:=0 to (x div 7) do
    if((x-7*j)mod 3)=0 then
     a[i]:=1;
  end;
  for i:=1 to n do
   if a[i]=1 then
        writeln('YES')
   else writeln('NO');
 end.


program ABC083B;
var
	n,a,b,r,i,j,s: integer;
begin
	read(n);
	read(a);
	read(b);
	r:=0;
	for i:=1 to n do begin
		s:=0;
		j:=i;
		while j>0 do begin
			s:=s+(j mod 10);
			j:=j div 10;
		end;
		if (a<=s) and (s<=b) then begin
			r:=r+i;
		end;
	end;
	writeln(r);
end.


program ABC083B;
var
    n,a,b,r,i,j,s: integer;
begin
    read(n);
    read(a);
    read(b);
    r:=0;
    for i:=1 to n do begin
        s:=0;
        j:=i;
        while j>0 do begin
            s:=s+(j mod 10);
            j:=j div 10;
        end;
        if (a<=s) and (s<=b) then begin
            r:=r+i;
        end;
    end;
    writeln(r);
end.


program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;
var
n,i,j,t,a,b,c,d,sum,temp:integer;
begin
try
  { TODO -oUser -cConsole Main : abc83}
  read(n);
  read(a);
  readln(b);

  sum:=0;
  for i := 1 to n do begin
    temp:=((i mod 10) div 1)+((i mod 100) div 10)+((i mod 1000) div 100)+((i mod 10000) div 1000)+(i div 10000);
    if (temp>=a)and(temp<=b) then begin
      sum:=sum+i;
    end;
  end;
  Writeln(sum);

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


var n,a,b,i,j,k,sum,tmp:longint;
    s:ansistring;
begin
  readln(n,a,b);
  for i:=1 to n do
  begin
    tmp:=0;
    str(i,s);
    for j:=1 to length(s) do
    begin
      val(s[j],k);
      tmp:=tmp+k;
    end;
    if (tmp>=a) and (tmp<=b) then
      sum:=sum+i;
  end;
  writeln(sum);
end.


var n,m,i,j,k,sum,now,a,b,c,d : longint ;
    cc,yo : array [0..1000001] of longint ;
    so,s1,s2 : ansistring ;
begin
 readln(n,a,b);
 for i:=1 to n do
  begin
   c:=i;
   now:=0;
   while c>0 do
    begin
     inc(now,c mod 10);
     c:=c div 10;
    end;
   if (now>=a)and(now<=b) then
    inc(sum,i);

  end;
 writeln(sum);
end.


var s,t,m,s1,s2:ansistring;
var n,i,j,k,l,ans:longint;
var a,b:array[0..100] of ansistring;
{var r:boolean;
function min(a,b:ansistring):ansistring;
var i:longint;
var t:ansistring;
begin
  if length(a)>length(b) then begin t:=a;a:=b;b:=t;end;
  for i:=1 to length(a) do
    if ord(a[i])<ord(b[i]) then exit(a)
    else if ord(b[i])<ord(a[i]) then exit(b);
  exit(a);
end;}
function same(a,b:ansistring):boolean;
var i:longint;
begin
  for i:=1 to length(a) do if (a[i]<>b[i]) and (a[i]<>'?') then exit(false);
  exit(true);
end;
begin
  readln(s);readln(t);s1:='';
  for i:=length(s)-length(t)+2 to length(s) do s1:=s1+s[i];
  s1:=s1+'1';
  for i:=length(s)-length(t)+1 downto 1 do
  begin
    s1:=s[i]+s1;delete(s1,length(s1),1);
    //writeln(s1);
    if same(s1,t) then
    begin
      //writeln(i,' ',s1);
      m:=s;k:=0;
      for j:=i to i+length(s1)-1 do begin inc(k);m[j]:=t[k];end;
      for j:=1 to length(m) do if m[j]='?' then m[j]:='a';
      writeln(m);exit;
    end;
  end;
  writeln('UNRESTORABLE');
end.

Score : 300 points

Problem Statement

E869120 found a chest which is likely to contain treasure.

However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters.

He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?.

One more thing he found is a sheet of paper with the following facts written on it:

Condition 1: The string S contains a string T as a contiguous substring.

Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.

Print the string S.

If such a string does not exist, print UNRESTORABLE.

Constraints

1 \leq |S'|, |T| \leq 50

S' consists of lowercase English letters and ?.

T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T'

Output

Print the string S.

If such a string does not exist, print UNRESTORABLE instead.

Sample Input 1

?tc????
coder

Sample Output 1

atcoder

There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder.
Among them, the lexicographically smallest is atcoder, so we can say S = atcoder.

Sample Input 2

??p??d??
abc

Sample Output 2

UNRESTORABLE

There is no string that satisfies Condition 1, so the string S does not exist.


var s,t,m,s1,s2:ansistring;
var n,i,j,k,l,ans:longint;
var a,b:array[0..100] of ansistring;
{var r:boolean;
function min(a,b:ansistring):ansistring;
var i:longint;
var t:ansistring;
begin
  if length(a)>length(b) then begin t:=a;a:=b;b:=t;end;
  for i:=1 to length(a) do
    if ord(a[i])<ord(b[i]) then exit(a)
    else if ord(b[i])<ord(a[i]) then exit(b);
  exit(a);
end;}
function same(a,b:ansistring):boolean;
var i:longint;
begin
  for i:=1 to length(a) do if (a[i]<>b[i]) and (a[i]<>'?') then exit(false);
  exit(true);
end;
begin
  readln(s);readln(t);s1:='1';
  for i:=length(s)-length(t)+2 to length(s) do s1:=s1+s[i];
  for i:=length(s)-length(t)+1 downto 1 do
  begin
    s1:=s[i]+s1;delete(s1,length(s1),1);
    if same(s1,t) then
    begin
      //writeln(i,' ',s1);
      m:=s;k:=0;
      for j:=i-length(s1)+1 to i do begin inc(k);m[j]:=t[k];end;
      for j:=1 to length(m) do if m[j]='?' then m[j]:='a';
      writeln(m);exit;
    end;
  end;
  writeln('UNRESTORABLE');
end.


var ans,s,ss,t:string;
    lent,lens:longint;
    i,j:longint;
    x:boolean;
    max:longint;
begin
  readln(s);
  readln(t);
  lens:=length(s);
  lent:=length(t);
  for i:=1 to lens do
  ans:=ans+'z';
  for i:=1 to lens-lent+1 do
  begin
    x:=true;
    for j:=1 to lent do
    if (s[i+j-1]<>t[j])and(s[i+j-1]<>'?') then
    begin
      x:=false;
      break;
    end;
    if x then
    begin
      ss:='';
      for j:=1 to i-1 do
      if s[j]='?' then ss:=ss+'a' else ss:=ss+s[j];
      ss:=ss+t;
      for j:=i+lent to lens do
      if s[j]='?' then ss:=ss+'a' else ss:=ss+s[j];
      if ss<ans then ans:=ss;
      max:=1;
    end;
  end;
  if max=0 then
  begin
    writeln('UNRESTORABLE');
    exit;
  end;
  writeln(ans);
end.


var a:array[0..51,0..51] of longint;n,m,i,j,s:longint;ch:char;
begin
 readln(n,m);
 for i:=1 to n do
  begin
   for j:=1 to m do
    begin
     read(ch);
     if ch='#' then a[i,j]:=1;
    end;readln;
  end;
 for i:=1 to n do
  begin
   for j:=1 to m do
    if a[i,j]<>0 then write('#')
                 else
                  begin
                   s:=a[i-1,j-1]+a[i,j-1]+a[i+1,j-1]+a[i-1,j]+a[i+1,j]+a[i-1,j+1]+a[i,j+1]+a[i+1,j+1];
                   write(s);
                  end;
   writeln;
  end;
end.

Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.

Print the strings after the process.

Constraints

1 \leq H,W \leq 50

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

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310


var x:char;h,w,i,j:integer;a:array[0..50,0..50]of 0..1;
begin
 readln(h,w);
 for i:=1 to h do
  begin
   for j:=1 to w do
    begin
     read(x);
     if(x='#')then a[i,j]:=1;
    end;readln;
  end;
 for i:=1 to h do
  begin
   for j:=1 to w do
    if a[i,j]=1 then write('#')
                else write(a[i-1,j-1]+a[i,j-1]+a[i+1,j-1]+a[i-1,j]+a[i+1,j]+a[i-1,j+1]+a[i,j+1]+a[i+1,j+1]);
   writeln;
  end;
end.


var x:char;h,w,i,j:longint;a:array[0..50,0..50]of 0..1;
begin
 readln(h,w);
 for i:=1 to h do
  begin
   for j:=1 to w do
    begin
     read(x);
     if(x='#')then a[i,j]:=1;
    end;readln;
  end;
 for i:=1 to h do
  begin
   for j:=1 to w do
    if a[i,j]=1 then write('#')
                else write(a[i-1,j-1]+a[i-1,j]+a[i-1,j+1]+a[i,j-1]+a[i,j+1]+a[i+1,j-1]+a[i+1,j]+a[i+1,j+1]);
   writeln;
  end;
end.


var
a:array[0..55,0..55] of char;
n,m,i,j,s:longint;
begin
readln(n,m);
for i:=1 to n do
  begin
    for j:=1 to m do read(a[i,j]);
    readln;
  end;
for i:=0 to m+1 do begin a[0,i]:=' ';a[n+1,i]:=' ';end;
for i:=1 to n do begin a[i,0]:=' ';a[i,m+1]:=' ';end;
for i:=1 to n do
  begin
    for j:=1 to m do
      if a[i,j]='.' then
        begin
          s:=0;
          if a[i-1,j-1]='#' then inc(s);
          if a[i-1,j]='#' then inc(s);
          if a[i-1,j+1]='#' then inc(s);
          if a[i,j-1]='#' then inc(s);
          if a[i,j+1]='#' then inc(s);
          if a[i+1,j-1]='#' then inc(s);
          if a[i+1,j]='#' then inc(s);
          if a[i+1,j+1]='#' then inc(s);
          write(s);
        end
        else write(a[i,j]);
    writeln;
  end;
end.


const _i:array[0..7]of shortint=(1,1,0,-1,-1,-1,0,1);
      _j:array[0..7]of shortint=(0,-1,-1,-1,0,1,1,1);
var a:array[0..1000,0..1000]of char;
    n,m,i,j,k,s:longint;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do
      read(a[i,j]);
    readln;
  end;
  for i:=1 to n do
  begin
    for j:=1 to m do
      if a[i,j]='#' then
        write(a[i,j])
      else
      begin
        s:=0;
        for k:=0 to 7 do
          if a[i+_i[k],j+_j[k]]='#' then
            inc(s);
        write(s);
      end;
    writeln;
  end;
end.


const _i:array[0..7]of shortint=(1,1,0,-1,-1,-1,0,1);
      _j:array[0..7]of shortint=(0,-1,-1,-1,0,1,1,1);
var a:array[0..1000,0..1000]of char;
    n,m,i,j,k,s:longint;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do
      read(a[i,j]);
    readln;
  end;
  for i:=1 to n do
  begin
    for j:=1 to n do
      if a[i,j]='#' then
        write(a[i,j])
      else
      begin
        s:=0;
        for k:=0 to 7 do
          if a[i+_i[k],j+_j[k]]='#' then
            inc(s);
        write(s);
      end;
    writeln;
  end;
end.


var
  a:array[0..51,0..51] of longint;
  n,m,i,j,s:longint;
  ch:char;
begin
  readln(n,m);
  for i:=1 to n do
    begin
      for j:=1 to m do
        begin
          read(ch);
          if ch='#' then a[i,j]:=1;
        end;
      readln;
    end;
  for i:=1 to n do
    begin
      for j:=1 to m do
        if a[i,j]<>0 then write('#')
          else
            begin
              s:=a[i-1,j-1]+a[i,j-1]+a[i+1,j-1]+a[i-1,j]+a[i+1,j]+a[i-1,j+1]+a[i,j+1]+a[i+1,j+1];
              write(s);
            end;
      writeln;
    end;
end.


var
  a:array[0..51,0..51] of longint;
  n,m,i,j,s:longint;
  ch:char;
begin
  readln(n,m);
  for i:=1 to n do
    begin
      for j:=1 to m do
        begin
          read(ch);
          if ch='#' then inc(a[i,j]);
        end;
      readln;
    end;
  for i:=1 to n do
    begin
      for j:=1 to m do
        begin
          s:=a[i-1,j-1]+a[i,j-1]+a[i+1,j-1]+a[i-1,j]+a[i+1,j]+a[i-1,j+1]+a[i,j+1]+a[i+1,j+1];
          if s=0 then write('#')
            else write(s);
        end;
      writeln;
    end;
end.


var
 c:array[1..8] of integer=(-1,-1,-1,0,0,1,1,1);
 d:array[1..8] of integer=(-1,0,1,-1,1,-1,0,1);
 a:array[0..50,0..50] of char;
 b:array[0..50,0..50] of longint;
 i,j,n,m:longint;
procedure pd(x,y:longint);
 var
  i:longint;
 begin
  if a[x,y]='#' then begin b[x,y]:=-maxlongint;exit; end;
  for i:=1 to 8 do if a[x+c[i],y+d[i]]='#' then inc(b[x,y]);
 end;
begin
 readln(n,m);
 for i:=1 to n do
  begin
   for j:=1 to m do read(a[i,j]);
   readln;
  end;
 for i:=1 to n do
  for j:=1 to m do pd(i,j);
 for i:=1 to n do
  begin
   for j:=1 to m do if b[i,j]=-maxlongint then write('#')
    else write(b[i,j]);
   writeln;
  end;
end.


var
  n,m,i,j,sum:longint;
  a:array[0..200,0..200] of char;
  p:boolean;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do read(a[i,j]);
    readln;
  end;
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      p:=true;
      if a[i,j]='#' then begin write('#');p:=false; end;
      if p then
      begin
        sum:=0;
        if a[i-1,j]='#' then sum:=sum+1;
        if a[i+1,j]='#' then sum:=sum+1;
        if a[i-1,j-1]='#' then sum:=sum+1;
        if a[i-1,j+1]='#' then sum:=sum+1;
        if a[i,j+1]='#' then sum:=sum+1;
        if a[i,j-1]='#' then sum:=sum+1;
        if a[i+1,j-1]='#' then sum:=sum+1;
        if a[i+1,j+1]='#' then sum:=sum+1;
        write(sum);
      end;
    end;
    writeln;
  end;
end.


var
  n,m,i,j,s:longint;
  a:array[0..100,0..100] of char;
  p:boolean;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do read(a[i,j]);
    readln;
  end;
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      p:=true;
      if a[i,j]='#' then begin write('#');p:=false; end;
      if p then
      begin
           s:=0;
           if a[i-1,j]='#' then s:=s+1;
           if a[i+1,j]='#' then s:=s+1;
           if a[i-1,j-1]='#' then s:=s+1;
           if a[i-1,j+1]='#' then s:=s+1;
           if a[i,j+1]='#' then s:=s+1;
           if a[i,j-1]='#' then s:=s+1;
           if a[i+1,j-1]='#' then s:=s+1;
           if a[i+1,j+1]='#' then s:=s+1;
           write(s);
      end;
    end;
    writeln;
  end;
end.


var n,m,i,j:longint;k:char;
a:array[0..1000,0..1000]of char;
begin
readln(n,m);
for i:=1 to n do
  begin
  for J:=1 to m do read(a[i,j]);
  readln;
  end;
for I:=1 to n do
  begin;
 for j:=1 to m do
   begin
   if a[i,j]='.' then
     begin
     k:='0';
     if a[i-1,j]='#'then k:=succ(k);
     if a[i+1,j]='#'then k:=succ(k);
     if a[i,j+1]='#'then k:=succ(k);
     if a[i,j-1]='#'then k:=succ(k);
     if a[i-1,j+1]='#'then k:=succ(k);
     if a[i-1,j-1]='#'then k:=succ(k);
     if a[i+1,j+1]='#'then k:=succ(k);
     if a[i+1,j-1]='#'then k:=succ(k);
     a[i,j]:=k;
     end;
   write(a[i,j]);
   end;
 writeln;
 end;
end.


var
  n,m,i,j,s:longint;
  a:array[0..51,0..51]of char;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do
      read(a[i,j]);
    readln;
  end;
  for i:=1 to n do
  begin
    for j:=1 to m do
    if  a[i,j]='#' then write(a[i,j]) else
    begin
      s:=0;
      if a[i-1,j-1]='#' then inc(s);
      if a[i-1,j]='#' then inc(s);
      if a[i-1,j+1]='#' then inc(s);
      if a[i,j-1]='#' then inc(s);
      if a[i,j+1]='#' then inc(s);
      if a[i+1,j-1]='#' then inc(s);
      if a[i+1,j]='#' then inc(s);
      if a[i+1,j+1]='#' then inc(s);
      write(s);
     end;
     writeln;
    end;
end.


var
  n,m,i,j,s:longint;
  a:array[0..1000,0..1000] of char;
  p:boolean;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do read(a[i,j]);
    readln;
  end;
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      p:=true;
      if a[i,j]='#' then begin write('#');p:=false; end;
      if p then
      begin
           s:=0;
           if a[i-1,j]='#' then s:=s+1;
           if a[i+1,j]='#' then s:=s+1;
           if a[i-1,j-1]='#' then s:=s+1;
           if a[i-1,j+1]='#' then s:=s+1;
           if a[i,j+1]='#' then s:=s+1;
           if a[i,j-1]='#' then s:=s+1;
           if a[i+1,j-1]='#' then s:=s+1;
           if a[i+1,j+1]='#' then s:=s+1;
           write(s);
      end;
    end;
    writeln;
  end;
end.


var
  a:array[0..102,0..102] of integer;
  n,m,i,j,t:longint;
  ch:char;
begin
  readln(n,m);
  for i:=1 to n do
   begin
     for j:=1 to m do
      begin
        read(ch);
        if ch='#' then a[i,j]:=1;
      end;
      readln;
   end;
  for i:=1 to n do
   begin
     for j:=1 to m do
      if a[i,j]=1 then write('#')
       else
        begin
          t:=a[i-1,j-1]+a[i-1,j]+a[i-1,j+1]+a[i,j-1]+a[i,j+1]+a[i+1,j-1]+a[i+1,j]+a[i+1,j+1];
          write(t);
        end;
     writeln;
   end;
end.


var
 a:array[0..101,0..101] of char;
 i,j,k,n,m:longint;
 s,t:string;
begin
 readln(n,m);
 for i:=1 to n do
  begin
   readln(s);
   for j:=1 to m do a[i,j]:=s[j];
  end;
  for i:=0 to n+1 do
   begin
    a[i,0]:=' ';
    a[i,m+1]:=' ';
   end;
  for i:=1 to n do
   for j:=1 to m do
    begin
     k:=0;t:='';
     if a[i-1,j]='#' then inc(k);
     if a[i+1,j]='#' then inc(k);
     if a[i,j-1]='#' then inc(k);
     if a[i,j+1]='#' then inc(k);
     if a[i-1,j-1]='#' then inc(k);
     if a[i-1,j+1]='#' then inc(k);
     if a[i+1,j-1]='#' then inc(k);
     if a[i+1,j+1]='#' then inc(k);
     str(k,t);
     if a[i,j]<>'#' then a[i,j]:=t[1];
    end;
   for i:=1 to n do
    begin
     for j:=1 to m do
     write(a[i,j]);
     writeln;
    end;
end.


var
	h,w,i,j,r,c:Longint;
	s:array of string;
	dx:array[1..8]of Longint=(1,0,-1,0,1,1,-1,-1);
	dy:array[1..8]of Longint=(0,1,0,-1,1,-1,1,-1);
begin
	readln(h,w);
	setlength(s,h+2);
	for i:=0 to h+1 do setlength(s[i],w+2);
	for i:=1 to h do readln(s[i]);
	for i:=1 to h do begin
		for j:=1 to w do begin
			if s[i][j]='.' then begin
				c:=48;
				for r:=1 to 8 do begin
					if s[i+dx[r]][j+dy[r]]='#' then inc(c);
				end;
				s[i][j]:=chr(c);
			end;
		end;
		writeln(s[i]);
	end;
end.


var
	h,w,i,j,r,c,tx,ty:Longint;
	s:array of string;
	dx:array[1..8]of Longint=(1,0,-1,0,1,1,-1,-1);
	dy:array[1..8]of Longint=(0,1,0,-1,1,-1,1,-1);
begin
	readln(h,w);
	setlength(s,h+2);
	for i:=0 to h+1 do setlength(s[i],w+2);
	for i:=1 to h do readln(s[i]);
	for i:=1 to h do begin
		for j:=1 to w do begin
			if s[i][j]='#' then write('#')
			else begin
				c:=0;
				for r:=1 to 8 do begin
					tx:=i+dx[r];
					ty:=j+dy[r];
					if s[tx][ty]='#' then inc(c);
				end;
				write(c);
			end;
		end;
		writeln('');
	end;
end.


var
  i,i1,j,j1:integer;
  a:array [0..102,0..102] of char;
  b:array [0..102,0..102] of longint;
begin
  readln(i1,j1);
  j:=j1;
  i:=i1;
  while (i>0) do
    begin
      while (j>0) do
        begin
          read(a[i,j]);
          dec(j);
        end;
      readln;
      dec(i);
      j:=j1;
    end;
  for i:= 1 to i1 do
    for j:= 1 to j1 do
      if a[i,j]='#' then begin
                           if b[i-1,j-1]<>-1 then inc(b[i-1,j-1]);
                           if b[i+1,j-1]<>-1 then inc(b[i+1,j-1]);
                           if b[i-1,j+1]<>-1 then inc(b[i-1,j+1]);
                           if b[i+1,j+1]<>-1 then inc(b[i+1,j+1]);
                           if b[i-1,j]<>-1 then inc(b[i-1,j]);
                           if b[i+1,j]<>-1 then inc(b[i+1,j]);
                           if b[i,j-1]<>-1 then inc(b[i,j-1]);
                           if b[i,j+1]<>-1 then inc(b[i,j+1]);
                           b[i,j]:=-1;
                         end;
  for i:= i1 downto 1 do
    begin
      for j:= j1 downto 1 do
        if b[i,j]=-1 then  write('#')
                      else write(b[i,j]);
      writeln;
    end;
end.


var
  i,j,c,d,b,y:longint;
a:array[0..105,0..105] of char;
x:array[0..105,0..105] of longint;
begin
  readln(b,y);
for i:=1 to b do begin
  for j:=1 to y do
  read(a[i,j]);
  readln;
  end;
for i:=1 to b do
  begin
  for j:=1 to y do begin if a[i,j]<>'#' then begin
if a[i+1,j]='#' then  inc(x[i,j]);
if a[i+1,j+1]='#' then inc(x[i,j]);
if a[i+1,j-1]='#' then inc(x[i,j]);
if a[i-1,j]='#' then inc(x[i,j]);
if a[i-1,j+1]='#' then inc(x[i,j]);
if a[i-1,j-1]='#' then inc(x[i,j]);
if a[i,j-1]='#' then inc(x[i,j]);
if a[i,j+1]='#' then inc(x[i,j]);
end;
end;
end;
for i:=1 to b do begin
for j:=1 to y do
if a[i,j]='#' then write('#')
else
write(x[i,j]);
writeln;
end;
end.


var
 n,m,i,j,x:longint;
 a:array[0..1000,0..1000] of char;
 s:string;
begin
 readln(n,m);
 for i:=1 to n do
  begin
   readln(s);
   for j:=1 to m do
    a[i,j]:=s[j];
  end;
 for i:=1 to n do
  begin
   for j:=1 to m do
    begin
     x:=0;
     if a[i,j]<>'#' then begin
       if a[i+1,j]='#' then inc(x);
       if a[i,j+1]='#' then inc(x);
       if a[i-1,j]='#' then inc(x);
       if a[i,j-1]='#' then inc(x);
       if a[i-1,j-1]='#' then inc(x);
       if a[i-1,j+1]='#' then inc(x);
       if a[i+1,j-1]='#' then inc(x);
       if a[i+1,j+1]='#' then inc(x);
      end;
     if a[i,j]='#' then write('#')
                   else write(x);
    end;
   writeln;
  end;
end.


var
  h,w,i,j,x,y,sum:longint;
  s:array[0..55,0..55] of char;
begin
  readln(h,w);

  for i:= 1 to h do
    begin
      for j:= 1 to w do
        read(s[i,j]);
      readln;
    end;

  for i:= 1 to h do
    begin
      for j:= 1 to w do
        if s[i,j]='.' then
          begin
            sum:=0;
            for x:= i-1 to i+1 do
              for y:= j-1 to j+1 do
                if  s[x,y]='#' then inc(sum);
            write(sum);
          end
          else write('#');
      writeln;
    end;
end.


var  a:array[0..60,0..60]of  longint;
     ch:char;
     i,j,k,l,h,t:longint;
begin
  readln(h,t);
  for  i:=1  to  h  do
    begin
      for  j:=1  to  t  do
        begin
          read(ch);
          if  ch='#'  then  a[i,j]:=-1
                      else  a[i,j]:=0;
        end;
      readln;
    end;
  for  i:=1  to  h  do
    for  j:=1  to  t  do
      begin
        if  a[i,j]<>-1  then  continue;
        for  k:=i-1  to  i+1  do
          for  l:=j-1  to  j+1  do
            if  a[k,l]<>-1  then
              inc(a[k,l]);
      end;
  for  i:=1  to  h  do
    begin
      for  j:=1  to  t  do
        if  a[i,j]=-1  then  write('#')
                       else  write(a[i,j]);
      writeln;
    end;
end.


var  a:array[0..50]of  string;
     ans,i,j,k,l,h,t:longint;
begin
  readln(h,t);
  for  i:=1  to  h  do
    readln(a[i]);
  for  i:=1  to  h  do
    begin
      for  j:=1  to  t  do
        begin
          if  a[i][j]='#'  then
            begin
              write('#');
              continue;
            end;
          ans:=0;
          for  k:=i-1  to  i+1  do
            for  l:=j-1  to  j+1  do
              if  a[k][l]='#'  then
                inc(ans);
          write(ans);
        end;
      writeln;
    end;
end.


var
 n,m,i,j,x:longint;
 a:array[0..101,0..101] of char;
begin
 readln(n,m);
 for i:=1 to n do
  begin
   for j:=1 to m do
    read(a[i,j]);
    readln;
    end;
    for i:=1 to n do
    begin
    for j:=1 to m do
    begin
    x:=0;
    if a[i,j]='#' then write('#')
                  else begin
                        if a[i-1,j-1]='#' then inc(x);
                        if a[i-1,j]='#' then inc(x);
                        if a[i-1,j+1]='#' then inc(x);
                        if a[i,j-1]='#' then inc(x);
                        if a[i,j+1]='#' then inc(x);
                        if a[i+1,j-1]='#' then inc(x);
                        if a[i+1,j]='#' then inc(x);
                        if a[i+1,j+1]='#' then inc(x);
                        write(x);
                       end;
   end;
   writeln;
   end;
end.


var
        n,m,i,j:longint;
        g:array[0..101,0..101]of char;
        f:array[0..101,0..101]of longint;
begin
        readln(n,m);
        for i:=1 to n do
        begin
                for j:=1 to m do
                read(g[i,j]);
                readln;
        end;
        for i:=1 to n do
        for j:=1 to m do
        if g[i,j]<>'#' then
        begin
                if g[i-1,j-1]='#' then inc(f[i,j]);
                if g[i-1,j]='#' then inc(f[i,j]);
                if g[i-1,j+1]='#' then inc(f[i,j]);
                if g[i,j-1]='#' then inc(f[i,j]);
                if g[i,j+1]='#' then inc(f[i,j]);
                if g[i+1,j-1]='#' then inc(f[i,j]);
                if g[i+1,j]='#' then inc(f[i,j]);
                if g[i+1,j+1]='#' then inc(f[i,j]);
        end;
        for i:=1 to n do
        begin
                for j:=1 to m do
                if g[i,j]<>'#' then write(f[i,j])
                else write('#');
                writeln;
        end;
end.


var
  n,m,i,j,k,l,t:longint;
  a:array[0..60,0..60]of char;
begin
  readln(n,m);
  for i:=1 to n do
    begin
      for j:=1 to m do
        read(a[i,j]);
      readln;
    end;
  for i:=1 to n do
    begin
      for j:=1 to m do
        if a[i,j]='#' then write(a[i,j])
                      else
                        begin
                          t:=0;
                          for k:=i-1 to i+1 do
                            for l:=j-1 to j+1 do
                              if a[k,l]='#' then inc(t);
                          write(t);
                        end;
      writeln;
    end;
end.


var a:array[0..51,0..51]of string;
i,j,n,m,k,l,p:integer;
begin
readln(n,m);
for i:=1 to n do
begin
for j:=1 to m do read(a[i,j]);
readln;
end;
for i:=1 to n do
for j:=1 to m do
begin
p:=0;
if a[i,j]<>'#' then begin
for k:=i-1 to i+1 do
for l:=j-1 to j+1 do
if a[k,l]='#' then p:=p+1;
str(p,a[i,j]);
end;
end;
for i:=1 to n do
begin
for j:=1 to m do write(a[i,j]);
writeln;
end;
end.


var a:array[0..101,0..101] of longint;
    i,j,n,m:longint;
    ch:char;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      read(ch);
      if ch='#' then
      begin
        if a[i-1,j-1]<>-1 then inc(a[i-1,j-1]);
        if a[i-1,j]<>-1 then inc(a[i-1,j]);
        if a[i-1,j+1]<>-1 then inc(a[i-1,j+1]);
        if a[i,j-1]<>-1 then inc(a[i,j-1]);
        a[i,j]:=-1;
        if a[i,j+1]<>-1 then inc(a[i,j+1]);
        if a[i+1,j-1]<>-1 then inc(a[i+1,j-1]);
        if a[i+1,j]<>-1 then inc(a[i+1,j]);
        if a[i+1,j+1]<>-1 then inc(a[i+1,j+1]);
      end;
    end;
    readln;
  end;
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      if a[i,j]=-1 then write('#')
      else write(a[i,j]);
    end;
    writeln;
  end;
end.


var
    a:array[-1..200,-1..200] of longint;
    i,j,n,m:longint;
    ch:char;
function slei(x,y:longint):longint;
var
    s:longint;
begin
    s:=a[x-1,y-1]+a[x-1,y]+a[x-1,y+1]+a[x,y-1];
    s:=s+a[x,y+1]+a[x+1,y-1]+a[x+1,y]+a[x+1,y+1];
    exit(s);
end;
begin
    readln(n,m);
    for i:=1 to n do
    begin
        for j:=1 to m do
        begin
            read(ch);
            if ch='.' then a[i,j]:=0 else a[i,j]:=1;
        end;
        readln;
    end;
    for i:=1 to n do
    begin
        for j:=1 to m do
            if a[i,j]=1 then write('#')
                else write(slei(i,j));
        writeln;
    end;
end.


// luogu-judger-enable-o2
const
  dx:array[1..8]of -1..1=(-1,1,0,0,-1,-1,1,1);
  dy:array[1..8]of -1..1=(0,0,-1,1,-1,1,-1,1);
var
  n,m,i,j:longint;
  a:array[0..101,0..101]of longint;
  s:string;
function dfs(h,l:longint):longint;
var
  i,count:longint;
begin
  count:=0;
  for i:=1 to 8 do
  if a[h+dx[i],l+dy[i]]=1 then inc(count);
  dfs:=count;
end;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    readln(s);
    for j:=1 to m do if s[j]='#' then a[i,j]:=1
    else a[i,j]:=2;
  end;
  for i:=1 to n do
  begin
    for j:=1 to m do
    if a[i,j]=1 then write('#')
    else write(dfs(i,j));
    writeln;
  end;
end.


var
n,m,i,j,k,t,x,y:longint;
a:array[0..101,0..101] of char;
 b:array[0..101,0..101] of longint;
begin
read(n,m);
readln;
for i:= 1 to n do
begin
for  j:= 1 to m do
begin
read(a[i,j]);
if a[i,j]='#' then begin inc(b[i-1,j]); inc(b[i-1,j-1]);
                               inc(b[i+1,j]); inc(b[i+1,j+1]);
                               inc(b[i-1,j+1]); inc(b[i+1,j-1]);
                               inc(b[i,j+1]); inc(b[i,j-1]);
                      end;
end;
readln;
end;
for i:= 1 to n do
 begin
  for j:= 1 to m do
 if a[i,j]='#' then write('#')
                 else write(b[i,j]);
writeln;
 end;
end.


var n,m,i,j:longint;
    x:array[0..1000,0..1000]of char;
    c:array[0..1000,0..1000]of longint;
begin
 readln(n,m);
 for i:=1 to n do
  begin
   for j:=1 to m do
   read(x[i,j]);
   readln;
  end;
  for i:=1 to n do
  for j:=1 to m do
  if x[i,j]<>'#' then
  begin
   if x[i-1,j-1]='#' then inc(c[i,j]);
   if x[i-1,j]='#' then inc(c[i,j]);
   if x[i-1,j+1]='#' then inc(c[i,j]);
   if x[i,j-1]='#' then inc(c[i,j]);
   if x[i,j+1]='#' then inc(c[i,j]);
   if x[i+1,j-1]='#' then inc(c[i,j]);
   if x[i+1,j]='#' then inc(c[i,j]);
   if x[i+1,j+1]='#' then inc(c[i,j]);
  end;
  for i:=1 to n do
  begin
   for j:=1 to m do
    if x[i,j]<>'#' then write(c[i,j])
     else write('#');
     writeln;
  end;
end.