var
    a,n:longint;
begin
    readln(n);
    readln(a);
    if a>=n mod 500 then writeln('Yes')
        else writeln('No');
end.

Score: 100 points

Problem Statement

E869120 has A 1-yen coins and infinitely many 500-yen coins.

Determine if he can pay exactly N yen using only these coins.

Constraints

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

A is an integer between 0 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
A

Output

If E869120 can pay exactly N yen using only his 1-yen and 500-yen coins, print Yes; otherwise, print No.

Sample Input 1

2018
218

Sample Output 1

Yes

We can pay 2018 yen with four 500-yen coins and 18 1-yen coins, so the answer is Yes.

Sample Input 2

2763
0

Sample Output 2

No

When we have no 1-yen coins, we can only pay a multiple of 500 yen using only 500-yen coins. Since 2763 is not a multiple of 500, we cannot pay this amount.

Sample Input 3

37
514

Sample Output 3

Yes


var
    n,i,x,y: Integer;
    a: array[1..100] of Integer;

procedure qsort(l,r:Longint;data:PInteger);
var
    i,j,mid,t: Integer;
begin
    if l>=r then
    begin
        exit;
    end;
    i:=l-1;
    j:=r-1;
    mid:=data[(l+r) div 2];
    repeat
        while data[i]<mid do
        begin
            inc(i);
        end;
        while data[j]>mid do
        begin
            dec(j);
        end;
        if i<=j then
        begin
            t:=data[i];
            data[i]:=data[j];
            data[j]:=t;
            inc(i);
            dec(j);
        end;
    until i>j;
    qsort(l,j,data);
    qsort(i,r,data);
end;
begin
    readln(n);
    for i := 1 to n do
    begin
        read(a[i]);
    end;
    qsort(1,n,@a);
    x:=0;
    y:=0;
    i:=n;
    while i>0 do
    begin
        inc(x,a[i]);
        dec(i,2);
    end;
    i:=n-1;
    while i>0 do
    begin
        inc(y,a[i]);
        dec(i,2);
    end;
    writeln(x-y);
    readln;
    readln;
end.

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

Score: 200 points

Problem Statement

We have N cards. A number a_i is written on the i-th card.

Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.

The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.

Constraints

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

a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.

Sample Input 1

2
3 1

Sample Output 1

2

First, Alice will take the card with 3. Then, Bob will take the card with 1.
The difference of their scores will be 3 - 1 = 2.

Sample Input 2

3
2 7 4

Sample Output 2

5

First, Alice will take the card with 7. Then, Bob will take the card with 4. Lastly, Alice will take the card with 2. The difference of their scores will be 7 - 4 + 2 = 5. The difference of their scores will be 3 - 1 = 2.

Sample Input 3

4
20 18 2 18

Sample Output 3

18

Submitted source-code file size, in bytes.

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

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


program ABC088B;
{$mode Delphi}
uses SysUtils,fgl;

function intcompare(n1,n2:longint):longint;
begin
	result:=n2-n1;
end;

var
	n,i,r,t: longint;
	arr: TFPGList<longint>;
begin
	read(n);
	arr:=TFPGList<longint>.Create();
	for i:=0 to n-1 do begin
		read(r);
		arr.Add(r);
	end;
	arr.Sort(@intcompare);
	// reverse(arr,0,arr.Count);
	r:=0;
	t:=1;
	for i:=0 to n-1 do begin
		r:=r+t*arr[i];
		t:=-t;
	end;
	writeln(r);
end.


{var n,m,ans:int64;
var a:int64;
function gys(a,b:int64):int64;
Var
  c:int64;
begin
  repeat
    c:=a mod b;
    a:=b;
    b:=c;
  until c=0;
  exit(a);
end;
begin
  read(n,m);
  if m mod n=0 then begin writeln(-1);exit;end;
  ans:=n*gys(n,m);if ans<=1000000000000000000
end.        }
var n,m,ans:int64;
var i:longint;
var a:int64;
begin
  read(n,m);
  if m mod n=0 then begin writeln(-1);exit;end;
  for i:=2 to 2147483647 do
  begin
    a:=n*i;
    if a>1000000000000000000 then begin writeln(-1);exit;end;
    if a mod m>0 then begin writeln(a);exit;end;
  end;
  writeln(-1);
end.

Score : 100 points

Problem Statement

You are given positive integers X and Y.
If there exists a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, choose one such integer and print it.
If it does not exist, print -1.

Constraints

1 ≤ X,Y ≤ 10^9

X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, or print -1 if it does not exist.

Sample Input 1

8 6

Sample Output 1

16

For example, 16 is a multiple of 8 but not a multiple of 6.

Sample Input 2

3 3

Sample Output 2

-1

A multiple of 3 is a multiple of 3.


var
  n,i,s1:longint;
  s2:real;
  a,b:array[1..10000] of longint;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  for i:=1 to n do read(b[i]);
  for i:=1 to n do
    begin
      if a[i]>b[i] then s1:=s1+a[i]-b[i];
      if a[i]<b[i] then s2:=s2+(b[i]-a[i])/2;
    end;
  if s1<=s2 then writeln('Yes') else writeln('No');
end.

Score : 300 points

Problem Statement

You are given two integer sequences of length N: a_1,a_2,..,a_N and b_1,b_2,..,b_N.
Determine if we can repeat the following operation zero or more times so that the sequences a and b become equal.

Operation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:

Add 2 to a_i.

Add 1 to b_j.

Constraints

1 ≤ N ≤ 10 000

0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N
b_1 b_2 .. b_N

Output

If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.

Sample Input 1

3
1 2 3
5 2 2

Sample Output 1

Yes

For example, we can perform three operations as follows to do our job:

First operation: i=1 and j=2. Now we have a = \{3,2,3\}, b = \{5,3,2\}.

Second operation: i=1 and j=2. Now we have a = \{5,2,3\}, b = \{5,4,2\}.

Third operation: i=2 and j=3. Now we have a = \{5,4,3\}, b = \{5,4,3\}.

Sample Input 2

5
3 1 4 1 5
2 7 1 8 2

Sample Output 2

No

Sample Input 3

5
2 7 1 8 2
3 1 4 1 5

Sample Output 3

No


var
n,i:Longint;
A,B:Array[1..10000]of Longint;
cnt,x,y:int64;
begin
read(n);
for i:=1 to n do read(A[i]);
for i:=1 to n do read(B[i]);
for i:=1 to n do inc(cnt,B[i]-A[i]);
for i:=1 to n do begin
if A[i]>B[i]then inc(x,A[i]-B[i])else inc(y,(B[i]-A[i]+1)div 2);
end;
if(x>cnt)or(y>cnt)then writeln('No')else writeln('Yes');
end.


var y,n,ans:int64;
var i:longint;
var x:array[0..10020] of int64;
begin
  read(n);for i:=1 to n do read(x[i]);
  for i:=1 to n do begin read(y);if x[i]<y then inc(ans,(y-x[i]) div 2)
  else dec(ans,x[i]-y);end;if ans>=0 then writeln('Yes')else writeln('No');
end.


type
  node=record
      num,id:longint;
  end;
var father,num,now,first:array[0..105000]of longint;
    p:array[0..105000]of node;
    heap:array[0..105000,0..2]of longint;
    i:longint;
    x,y:longint;
    n,m:longint;
    totin,totout:longint;
    qwq,ans,have:int64;

function getfather(x:longint):longint;
begin
  if father[x]=x then exit(x) else
  begin
    father[x]:=getfather(father[x]);
    getfather:=father[x];
  end;
end;
procedure adde(x,y:longint);
var fx,fy:longint;
begin
  fx:=getfather(x);
  fy:=getfather(y);
  if fx<>fy then
  begin
    father[fx]:=fy;
    inc(num[fy],num[fx]);
  end;
end;
procedure qs(l,r:longint);
var i,j:longint;
    t,m:node;
begin
  i:=l;
  j:=r;
  m:=p[(l+r)>>1];
  repeat
    while (p[i].id<m.id)or((p[i].id=m.id)and(p[i].num<m.num)) do inc(i);
    while (p[j].id>m.id)or((p[j].id=m.id)and(p[j].num>m.num)) do dec(j);
    if i<=j then
    begin
      t:=p[i];p[i]:=p[j];p[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;
procedure swap(var a,b:longint);
var t:longint;
begin
  t:=a;a:=b;b:=t;
end;
procedure up(x,bool:longint);
begin
  while x>1 do
  begin
    //writeln(heap[x,bool],'qwq');
    if p[now[heap[x,bool]]].num<p[now[heap[x>>1,bool]]].num then
    begin
      swap(heap[x,bool],heap[x>>1,bool]);
      x:=x>>1;
    end else break;
  end;
end;
procedure down(x,bool:longint);
var min,tot:longint;
begin
  if bool=1 then tot:=totin else tot:=totout;
  while x*2<=tot do
  begin
    min:=x*2;
    if (x*2+1<=tot)and
    (p[now[heap[x*2+1,bool]]].num<p[now[heap[min,bool]]].num) then inc(min);
    if (p[now[heap[x,bool]]].num>p[now[heap[min,bool]]].num) then
    begin
      swap(heap[x,bool],heap[min,bool]);
      x:=min;
    end else break;
  end;
end;
begin
  read(n,m);
  for i:=1 to n do
  begin
    read(p[i].num);
    father[i]:=i;
    num[i]:=1;
  end;
  for i:=1 to m do
  begin
    read(x,y);
    inc(x);
    inc(y);
    adde(x,y);
  end;
  for i:=1 to n do
  p[i].id:=getfather(i);
  qs(1,n);
  p[0].num:=maxlongint;
  i:=1;
  while i<=n do
  begin
    inc(ans,p[i].num);
    if num[p[i].id]>1 then
    begin
      now[p[i].id]:=i+1;
      inc(have,num[p[i].id]);
      dec(have);
    end else
    begin
      now[p[i].id]:=0;
      dec(have);
    end;
    inc(totout);
    heap[totout,0]:=p[i].id;
    up(totout,0);
    first[p[i].id]:=i;
    i:=i+num[p[i].id];
  end;
  if totout=1 then
  begin
    writeln(0);
    exit;
  end;
  inc(have);
  if have<0 then
  begin
    writeln('Impossible');
    exit;
  end;
  while totout>0 do
  begin
    inc(qwq);
    x:=heap[1,0];
    //writeln(x);
    if now[x]<>0 then
    begin
      inc(totin);
      heap[totin,1]:=x;
      up(totin,1);
    end else
    if (qwq>1)and(totin>0) then
    begin
      x:=heap[1,1];
      inc(ans,p[now[x]].num);
    end;
    if totin>0 then
    begin
      x:=heap[1,1];
      if qwq>2 then inc(ans,p[now[x]].num);
      inc(now[x]);
      if (now[x]>=first[x]+num[x]) then
      begin
        now[x]:=0;
        heap[1,1]:=heap[totin,1];
        dec(totin);
      end;
      if totin>0 then down(1,1);
    end;
    heap[1,0]:=heap[totout,0];
    dec(totout);
    down(1,0);
  end;
  writeln(ans);
end.

Score : 600 points

Problem Statement

You are given a forest with N vertices and M edges. The vertices are numbered 0 through N-1.
The edges are given in the format (x_i,y_i), which means that Vertex x_i and y_i are connected by an edge.

Each vertex i has a value a_i.
You want to add edges in the given forest so that the forest becomes connected.
To add an edge, you choose two different vertices i and j, then span an edge between i and j.
This operation costs a_i + a_j dollars, and afterward neither Vertex i nor j can be selected again.

Find the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Constraints

1 ≤ N ≤ 100,000

0 ≤ M ≤ N-1

1 ≤ a_i ≤ 10^9

0 ≤ x_i,y_i ≤ N-1

The given graph is a forest.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
a_0 a_1 .. a_{N-1}
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Sample Input 1

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

Sample Output 1

7

If we connect vertices 0 and 5, the graph becomes connected, for the cost of 1 + 6 = 7 dollars.

Sample Input 2

5 0
3 1 4 1 5

Sample Output 2

Impossible

We can't make the graph connected.

Sample Input 3

1 0
5

Sample Output 3

0

The graph is already connected, so we do not need to add any edges.


var
    x,a,b:longint;
begin
    read(x,a,b);
    writeln((x-a) mod b);
end.

Score : 100 points

Problem Statement

You went shopping to buy cakes and donuts with X yen (the currency of Japan).

First, you bought one cake for A yen at a cake shop.
Then, you bought as many donuts as possible for B yen each, at a donut shop.

How much do you have left after shopping?

Constraints

1 \leq A, B \leq 1 000

A + B \leq X \leq 10 000

X, A and B are integers.

Input

Input is given from Standard Input in the following format:

X
A
B

Output

Print the amount you have left after shopping.

Sample Input 1

1234
150
100

Sample Output 1

84

You have 1234 - 150 = 1084 yen left after buying a cake.
With this amount, you can buy 10 donuts, after which you have 84 yen left.

Sample Input 2

1000
108
108

Sample Output 2

28

Sample Input 3

579
123
456

Sample Output 3

0

Sample Input 4

7477
549
593

Sample Output 4

405


uses math;
var s:array[0..2,0..100]of longint;
    i,j,n,x,a:longint;
begin
  readln(n);
  for i:=1 to 2 do
    for j:=1 to n do
    begin
      read(x);
      s[i,j]:=s[i,j-1]+x;
    end;
  for i:=1 to n do
    a:=max(a,s[1,i]+s[2,n]-s[2,i-1]);
  writeln(a);
end.

Score : 300 points

Problem Statement

We have a 2 \times N grid. We will denote the square at the i-th row and j-th column (1 \leq i \leq 2, 1 \leq j \leq N) as (i, j).

You are initially in the top-left square, (1, 1).
You will travel to the bottom-right square, (2, N), by repeatedly moving right or down.

The square (i, j) contains A_{i, j} candies.
You will collect all the candies you visit during the travel.
The top-left and bottom-right squares also contain candies, and you will also collect them.

At most how many candies can you collect when you choose the best way to travel?

Constraints

1 \leq N \leq 100

1 \leq A_{i, j} \leq 100 (1 \leq i \leq 2, 1 \leq j \leq N)

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} ... A_{1, N}
A_{2, 1} A_{2, 2} ... A_{2, N}

Output

Print the maximum number of candies that can be collected.

Sample Input 1

5
3 2 2 4 1
1 2 2 2 1

Sample Output 1

14

The number of collected candies will be maximized when you:

move right three times, then move down once, then move right once.

Sample Input 2

4
1 1 1 1
1 1 1 1

Sample Output 2

5

You will always collect the same number of candies, regardless of how you travel.

Sample Input 3

7
3 3 4 5 4 5 3
5 3 4 4 2 3 2

Sample Output 3

29

Sample Input 4

1
2
3

Sample Output 4

5


var
    n,m,i,j: Longint;
    L,R,D: array[1..200000] of Longint;
    position:array[1..100000] of Longint;
    flag:Boolean;
begin
    flag:=true;
    readln(n,m);
    for i := 1 to n do
    begin
        position[i]:=-2147483648;
    end;
    for i := 1 to m do
    begin
        readln(L[i],R[i],D[i]);
        if (position[L[i]]=-2147483648)and(position[R[i]]=-2147483648) then
        begin
            position[L[i]]:=0;
            position[R[i]]:=D[i];
        end
        else
        begin
            if (position[L[i]]<>-2147483648) and (position[R[i]]<>-2147483648) then
            begin
                if position[L[i]]+D[i]<>position[R[i]] then
                begin
                    flag:=false;
                    break;
                end;
            end
            else
            begin
                if position[L[i]]<>-2147483648 then
                begin
                    position[R[i]]:=position[L[i]]+D[i];
                    for j := 1 to i-1 do
                    begin
                        if L[j]=R[i] then
                        begin
                            position[R[j]]:=position[L[j]]+D[j];
                        end
                        else if R[j]=R[i] then
                        begin
                            position[L[j]]:=position[R[j]]-D[j];
                        end;
                    end;
                end
                else
                begin
                    position[L[i]]:=position[R[i]]-D[i];
                    for j := 1 to i-1 do
                    begin
                        if L[j]=L[i] then
                        begin
                            position[R[j]]:=position[L[j]]+D[j];
                        end
                        else if R[j]=L[i] then
                        begin
                            position[L[j]]:=position[R[j]]-D[j];
                        end;
                    end;
                end;
            end;
        end;
    end;
    if flag then
    begin
        writeln('Yes');
    end
    else
    begin
        writeln('No');
    end;
end.

Score : 400 points

Problem Statement

There are N people standing on the x-axis.
Let the coordinate of Person i be x_i.
For every i, x_i is an integer between 0 and 10^9 (inclusive).
It is possible that more than one person is standing at the same coordinate.

You will given M pieces of information regarding the positions of these people.
The i-th piece of information has the form (L_i, R_i, D_i).
This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.

It turns out that some of these M pieces of information may be incorrect.
Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.

Constraints

1 \leq N \leq 100 000

0 \leq M \leq 200 000

1 \leq L_i, R_i \leq N (1 \leq i \leq M)

0 \leq D_i \leq 10 000 (1 \leq i \leq M)

L_i \neq R_i (1 \leq i \leq M)

If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).

D_i are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M

Output

If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes; if it does not exist, print No.

Sample Input 1

3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

Yes

Some possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103).

Sample Input 2

3 3
1 2 1
2 3 1
1 3 5

Sample Output 2

No

If the first two pieces of information are correct, x_3 - x_1 = 2 holds, which is contradictory to the last piece of information.

Sample Input 3

4 3
2 1 1
2 3 5
3 4 2

Sample Output 3

Yes

Sample Input 4

10 3
8 7 100
7 9 100
9 8 100

Sample Output 4

No

Sample Input 5

100 0

Sample Output 5

Yes


var
	c,x:array[1..100000]of Longint;
	G1,G2:array[1..100000]of array of Longint;
	used:array[1..100000]of Boolean;
	n,m,i,l,r,d:Longint;
function dfs(u,p:Longint):Boolean;
var i:Longint;
begin
	used[u]:=True;
	for i:=0 to c[u]-1 do begin
		if G1[u,i]=p then continue;
		if used[G1[u,i]]and(x[G1[u,i]]<>x[u]+G2[u,i])then begin
			dfs:=False;
			exit;
		end;
		if not used[G1[u,i]]then begin
			x[G1[u,i]]:=x[u]+G2[u,i];
			if not dfs(G1[u,i],u)then begin
				dfs:=False;
				exit;
			end;
		end;
	end;
	dfs:=True;
end;
begin
	read(n,m);
	for i:=1 to m do begin
		read(l,r,d);
		inc(c[l]);inc(c[r]);
		setlength(G1[l],c[l]);
		setlength(G2[l],c[l]);
		setlength(G1[r],c[r]);
		setlength(G2[r],c[r]);
		G1[l,c[l]-1]:=r;
		G2[l,c[l]-1]:=d;
		G1[r,c[r]-1]:=l;
		G2[r,c[r]-1]:=-d;
	end;
	for i:=1 to n do begin
		if not used[i]then begin
			x[i]:=0;
			if not dfs(i,0)then begin
				writeln('No');
				exit;
			end;
		end;
	end;
	writeln('Yes');
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 : ここにコードを記述してください }
    Read(a);
    Readln(b);

    c:=strtoint(inttostr(a)+inttostr(b));

    if power(trunc(sqrt(c)),2)=c then
      Writeln('Yes')
    else
      Writeln('No');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

AtCoDeer the deer has found two positive integers, a and b.
Determine whether the concatenation of a and b in this order is a square number.

Constraints

1 ≤ a,b ≤ 100

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the concatenation of a and b in this order is a square number, print Yes; otherwise, print No.

Sample Input 1

1 21

Sample Output 1

Yes

As 121 = 11 × 11, it is a square number.

Sample Input 2

100 100

Sample Output 2

No

100100 is not a square number.

Sample Input 3

12 10

Sample Output 3

No


var
  n,i,t,x,y:longint;
  p:boolean;
begin
  readln(n);
  p:=true;
  for i:=1 to n do
  begin
    readln(t,x,y);
    if not(((((x+y) mod 2=0)and(t mod 2=0))or(((x+y) mod 2=1)and(t mod 2=1)))and(t>=x+y)) then begin p:=false;write('NO');break;end;
  end;
  if p then write('Yes');
end.

Score : 300 points

Problem Statement

AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.

If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).
Note that he cannot stay at his place.
Determine whether he can carry out his plan.

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

t_i < t_{i+1} (1 ≤ i ≤ N-1)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

If AtCoDeer can carry out his plan, print Yes; if he cannot, print No.

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

For example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1).

Sample Input 2

1
2 100 100

Sample Output 2

No

It is impossible to be at (100,100) two seconds after being at (0,0).

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


var
  n,i,t,x,y:longint;
  p:boolean;
begin
  readln(n);
  p:=true;
  for i:=1 to n do
  begin
    readln(t,x,y);
    if not(((((x+y) mod 2=0)and(t mod 2=0))or(((x+y) mod 2=1)and(t mod 2=1)))and(t<=x+y)) then begin p:=false;write('NO');break;end;
  end;
  if p then write('Yes');
end.


var n,i,t,c,o:longint;
a,b:array[0..100000]of longint;
begin
readln(n);
for i:=1 to n do
  begin
  read(t,c,o);
  if t<c+o then
    begin
    writeln('No');
    halt;
    end;
  end;   writeln('Yes');
end.


var
 n,i,t,x,y:longint;
 p:boolean;
begin
 readln(n);
 for i:=1 to n do
  begin
   readln(t,x,y);
   if x+y>t then p:=true;
  end;
 if p then writeln('No')
  else writeln('Yes');
end.


var
  n,x,y,x1,y1,i,t:longint;
begin
  readln(n);
  x:=0; y:=0;
  for i:= 1 to n do
    begin
      readln(t,x1,y1);
      if (x=x1)and(y=y1)then begin writeln('NO'); exit; end;
      if abs(x-x1)+abs(y-y1)<=t then  begin x:=x1;y:=y1; end
                                else
                                  begin
                                    writeln('NO');
                                    exit;
                                  end;
    end;
  writeln('Yes');
end.


var
    n,i,a,b,t,x,y:longint;
begin
    readln(n);
    x:=0; y:=0;
    for i:=1 to n do
    begin
        read(t,a,b);
        if (abs(x-a)+abs(y-b)>t) or ((abs(x-a)+abs(y-b)-t) div 2=1) then
        begin
            writeln('No');
            halt;
        end;
        x:=a; y:=b;
    end;
    writeln('Yes');
end.


program ABC086C;
var
	i,n,t,x,y: integer;
	t0,x0,y0,dt,dx,dy: integer;
begin
	read(n);
	t:=0;
	x:=0;
	y:=0;
	for i:=1 to n do begin
		read(t0);
		read(x0);
		read(y0);
		dt:=t0-t;
		dx:=x0-x;
		dy:=y0-y;
		if (dx+dy>dt) or ((dt-dx-dy) mod 2>0) then begin
			writeln('No');
			exit;
		end;
		t:=t0;
		x:=x0;
		y:=y0;
	end;
	writeln('Yes');
end.


var x,y:array[1..100000]of longint;
    i,j,n,tmp,lastt,lastx,lasty:longint;
begin
  readln(n);
  for i:=1 to 100000 do
  begin
    x[i]:=10000000;
    y[i]:=10000000;
  end;
  for i:=1 to n do
  begin
    read(tmp);
    readln(x[tmp],y[tmp]);
  end;
  for i:=1 to 100000 do
  begin
    if x[i]=10000000 then continue;
    if abs(lastx-x[i])+abs(lasty-y[i])>i-lastt then
    begin
      writeln('No'); exit;
    end;
    if ((i-lastt)-(abs(lastx-x[i])+abs(lasty-y[i]))) mod 2<>0 then
    begin
      writeln('No'); exit;
    end;
    lastt:=i;
    lastx:=x[i];
    lasty:=y[i];
  end;
  writeln('Yes');
end.


var
    n,x,y,t: Longint;
    x0,y0,t0: Longint;
    offset: Longint;
    offset_xy: Longint;
    even, even_T: Boolean;
    flag: Boolean;
begin
    flag:=true;
    x0:=0;
    y0:=0;
    t0:=0;
    even:=true;
    readln(n);
    while n>0 do
    begin
       readln(t,x,y);
       if not flag then
       begin
           dec(n);
           continue;
       end;
       if (x=x0)and(y=y0) then
       begin
           flag:=false;
           dec(n);
           continue;
       end;
       offset:=t-t0;
       if offset mod 2 = 0 then
       begin
           even_T:=true;
       end
       else
       begin
           even_T:=false;
       end;
       offset_xy:=abs(x-x0)+abs(y-y0);
       if offset_xy>offset then
       begin
            flag:=false;
            dec(n);
            continue;
       end;
       if (offset_xy mod 2 = 0)=(even xor even_T)  then
       begin
            flag:=false;
            dec(n);
            continue;
       end;
       x0:=x;
       y0:=y;
       t0:=t;
       dec(n);
    end;

    if flag then
    begin
        writeln('Yes');
    end
    else
    begin
        writeln('No');
    end;
end.


var
  n,k,x,y,i:longint;
  c:char;
begin
  readln(n,k);
  for i:=1 to n do begin read(x,y);read(c);end;
  writeln(n);
  close(input);
  close(output);
end.

Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4


var
	N,i,t,x,y,d,nx,ny,nt:Longint;
begin
	read(N);
	for i:=1 to N do begin
		read(t,x,y);
		d:=0;
		if x>nx then inc(d,x-nx)else inc(d,nx-x);
		if y>ny then inc(d,y-ny)else inc(d,ny-y);
		if(d mod 2<>(t-nt)mod 2)or(d>t-nt)then begin
			writeln('No');
			exit;
		end;
		nt:=t;
		nx:=x;
		ny:=y;
	end;
	writeln('Yes');
end.


var
	n,k,ans,now,i,j,x,y:Longint;
	c:Char;
	w:array[0..4000,0..4000]of Longint;
function f(u,v:Longint):Longint;
begin
	f:=w[u+k,v+k]-w[u,v+k]-w[u+k,v]+w[u,v];
end;
begin
	read(n,k);
	for i:=1 to n do begin
		read(x,y,c,c);
		if c='B' then inc(x,k);
		x:=x mod(2*k);
		y:=y mod(2*k);
		inc(w[x+1+k,y+1+k]);
	end;
	for i:=1 to 4*k do for j:=1 to 4*k do inc(w[i,j],w[i-1,j]+w[i,j-1]-w[i-1,j-1]);
	for i:=0 to k-1 do for j:=0 to k-1 do begin
		now:=f(i,j)+f(i+k,j+k)+f(i+2*k,j)+f(i,j+2*k)+f(i+2*k,j+2*k);
		if now>ans then ans:=now;
		now:=f(i+k,j)+f(i,j+k)+f(i+2*k,j+k)+f(i+k,j+2*k);
		if now>ans then ans:=now;
	end;
	writeln(ans);
end.


var n:longint;
    max,x,min:int64;
    a:array[0..150000]of longint;
    i:longint;
begin
  read(n);
  for i:=1 to n do
  read(a[i]);
  if a[n]<>2 then
  begin
    writeln(-1);
    exit;
  end;
  a[0]:=1;
  max:=2;
  min:=2;
  for i:=n-1 downto 1 do
  begin
    x:=(max*2-1) div a[i];
    if (x=0)or((((max+a[i+1]-1) div a[i])-((max-1) div a[i])=0)and(a[i+1]<>1)) then
    begin
      writeln(-1);
      exit;
    end else max:=x*a[i];
    x:=min div a[i];
    if min mod a[i]=0 then min:=x*a[i] else min:=(x+1)*a[i];
  end;
  writeln(min,' ',max+a[1]-1);
end.

Score : 500 points

Problem Statement

An adult game master and N children are playing a game on an ice rink.
The game consists of K rounds.
In the i-th round, the game master announces:

Form groups consisting of A_i children each!

Then the children who are still in the game form as many groups of A_i children as possible.
One child may belong to at most one group.
Those who are left without a group leave the game. The others proceed to the next round.
Note that it's possible that nobody leaves the game in some round.

In the end, after the K-th round, there are exactly two children left, and they are declared the winners.

You have heard the values of A_1, A_2, ..., A_K. You don't know N, but you want to estimate it.

Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.

Constraints

1 \leq K \leq 10^5

2 \leq A_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

K
A_1 A_2 ... A_K

Output

Print two integers representing the smallest and the largest possible value of N, respectively,
or a single integer -1 if the described situation is impossible.

Sample Input 1

4
3 4 3 2

Sample Output 1

6 8

For example, if the game starts with 6 children, then it proceeds as follows:

In the first round, 6 children form 2 groups of 3 children, and nobody leaves the game.

In the second round, 6 children form 1 group of 4 children, and 2 children leave the game.

In the third round, 4 children form 1 group of 3 children, and 1 child leaves the game.

In the fourth round, 3 children form 1 group of 2 children, and 1 child leaves the game.

The last 2 children are declared the winners.

Sample Input 2

5
3 4 100 3 2

Sample Output 2

-1

This situation is impossible.
In particular, if the game starts with less than 100 children, everyone leaves after the third round.

Sample Input 3

10
2 2 2 2 2 2 2 2 2 2

Sample Output 3

2 3


program V8122A;
 var
  f:Array[0..125001] of int64;
  a,b:Array[0..2001] of longint;
  i,j,n,p,s:longint;
  t:int64;
 begin
  readln(n);
  s:=0;
  for i:=1 to n do
   begin
    read(A[i]);
    b[i]:=a[i] mod 30;
    inc(s,a[i]);
   end;
  readln;
  t:=2<<30-1;
  //writeln(t);
  fillqword(f,sizeof(f)>>4,0);
  f[1]:=1;
  for i:=1 to n do
   for j:=s div 30+1 downto a[i] div 30+1 do
    f[j]:=(f[j-a[i] div 30]<<(b[i])) and t or f[j];
  p:=0;
  for i:=1 to s div 30+1 do
   for j:=1 to 30 do
    if ((1<<(j-1)) and f[i]=1<<(j-1)) and ((i-1)*30+j-1<=s>>1) then p:=(i-1)*30+j-1;
  writeln(s-p);
 end.

Score : 700 points

Problem Statement

You are given N integers A_1, A_2, ..., A_N.

Consider the sums of all non-empty subsequences of A. There are 2^N - 1 such sums, an odd number.

Let the list of these sums in non-decreasing order be S_1, S_2, ..., S_{2^N - 1}.

Find the median of this list, S_{2^{N-1}}.

Constraints

1 \leq N \leq 2000

1 \leq A_i \leq 2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the median of the sorted list of the sums of all non-empty subsequences of A.

Sample Input 1

3
1 2 1

Sample Output 1

2

In this case, S = (1, 1, 2, 2, 3, 3, 4). Its median is S_4 = 2.

Sample Input 2

1
58

Sample Output 2

58

In this case, S = (58).


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a:string;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    Readln(a);

    a[4]:='8';
    Writeln(a);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

On some day in January 2018, Takaki is writing a document. The document has a column where the current date is written in yyyy/mm/dd format. For example, January 23, 2018 should be written as 2018/01/23.

After finishing the document, she noticed that she had mistakenly wrote 2017 at the beginning of the date column. Write a program that, when the string that Takaki wrote in the date column, S, is given as input, modifies the first four characters in S to 2018 and prints it.

Constraints

S is a string of length 10.

The first eight characters in S are 2017/01/.

The last two characters in S are digits and represent an integer between 1 and 31 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Replace the first four characters in S with 2018 and print it.

Sample Input 1

2017/01/07

Sample Output 1

2018/01/07

Sample Input 2

2017/01/31

Sample Output 2

2018/01/31


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  m:integer;
begin
  try
    { TODO -oUser -cConsole Main : abc84a}
    readln(m);
    Writeln(48-m);
    readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

How many hours do we have until New Year at M o'clock (24-hour notation) on 30th, December?

Constraints

1≤M≤23

M is an integer.

Input

Input is given from Standard Input in the following format:

M

Output

If we have x hours until New Year at M o'clock on 30th, December, print x.

Sample Input 1

21

Sample Output 1

27

We have 27 hours until New Year at 21 o'clock on 30th, December.

Sample Input 2

12

Sample Output 2

36


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a,b,i:integer;
  s,ans:string;
begin
  try
    { TODO -oUser -cConsole Main : abc84a}
    read(a);
    readln(b);
    readln(s);

    ans:='Yes';
    if not(s[a+1]='-') then
      ans:='No'
    else begin
      for i := 1 to a do
        if s[i]='-' then begin
          ans:='No';
          break;
        end;
      for i := a+2 to Length(s) do
        if s[i]='-' then begin
          ans:='No';
          break;
        end;
    end;

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

Score : 200 points

Problem Statement

The postal code in Atcoder Kingdom is A+B+1 characters long, its (A+1)-th character is a hyphen -, and the other characters are digits from 0 through 9.

You are given a string S. Determine whether it follows the postal code format in Atcoder Kingdom.

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

Print Yes if S follows the postal code format in AtCoder Kingdom; print No otherwise.

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

The (A+1)-th character of S is -, and the other characters are digits from 0 through 9, so it follows the format.

Sample Input 2

1 1
---

Sample Output 2

No

S contains unnecessary -s other than the (A+1)-th character, so it does not follow the format.

Sample Input 3

1 2
7444

Sample Output 3

No


var
	n,ans,i,j:Longint;
	c,s,f:array[1..499]of Longint;
begin
	read(n);
	for i:=1 to n-1 do read(c[i],s[i],f[i]);
	for i:=1 to n-1 do begin
		ans:=0;
		for j:=i to n-1 do begin
			if ans<s[j] then ans:=s[j]+c[j]
			else ans:=(ans+f[j]-1)div f[j]*f[j]+c[j];
		end;
		writeln(ans);
	end;
	writeln(0);
end.

Score : 300 points

Problem Statement

A railroad running from west to east in Atcoder Kingdom is now complete.

There are N stations on the railroad, numbered 1 through N from west to east.

Tomorrow, the opening ceremony of the railroad will take place.

On this railroad, for each integer i such that 1≤i≤N-1, there will be trains that run from Station i to Station i+1 in C_i seconds. No other trains will be operated.

The first train from Station i to Station i+1 will depart Station i S_i seconds after the ceremony begins. Thereafter, there will be a train that departs Station i every F_i seconds.

Here, it is guaranteed that F_i divides S_i.

That is, for each Time t satisfying S_i≤t and t％F_i=0, there will be a train that departs Station i t seconds after the ceremony begins and arrives at Station i+1 t+C_i seconds after the ceremony begins, where A％B denotes A modulo B, and there will be no other trains.

For each i, find the earliest possible time we can reach Station N if we are at Station i when the ceremony begins, ignoring the time needed to change trains.

Constraints

1≤N≤500

1≤C_i≤100

1≤S_i≤10^5

1≤F_i≤10

S_i％F_i=0

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 S_1 F_1
:
C_{N-1} S_{N-1} F_{N-1}

Output

Print N lines. Assuming that we are at Station i (1≤i≤N) when the ceremony begins, if the earliest possible time we can reach Station N is x seconds after the ceremony begins, the i-th line should contain x.

Sample Input 1

3
6 5 1
1 10 1

Sample Output 1

12
11
0

We will travel from Station 1 as follows:

5 seconds after the beginning: take the train to Station 2.

11 seconds: arrive at Station 2.

11 seconds: take the train to Station 3.

12 seconds: arrive at Station 3.

We will travel from Station 2 as follows:

10 seconds: take the train to Station 3.

11 seconds: arrive at Station 3.

Note that we should print 0 for Station 3.

Sample Input 2

4
12 24 6
52 16 4
99 2 2

Sample Output 2

187
167
101
0

Sample Input 3

4
12 13 1
44 17 17
66 4096 64

Sample Output 3

4162
4162
4162
0


var  n,i,j:longint;
     x,z:longint;
     f,c,s:array[0..1000000]of longint;
function max(a,b:longint):longint;
begin
  if a>b then exit(a) else exit(b);
end;
begin
  read(n);
  for i:=1 to n-1 do
  read(c[i],s[i],f[i]);
  for i:=1 to n do
  begin
    x:=s[i];
    for j:=i to n-1 do
    begin
      z:=(x-s[j]);
       if z mod f[j]<>0 then
      begin
        z:=(f[j]-(z mod f[j]));
      end else Z:=0;
      if x<=s[j] then
      begin
        x:=s[j]+c[j];
      end else
      begin
        x:=x+z+c[j];
      end;
      //writeln('i=',i,' ',x);
    end;
    writeln(x);
  end;
end.


var  n,i,j:longint;
     x,z:longint;
     f,c,s:array[0..1000000]of longint;
function max(a,b:longint):longint;
begin
  if a>b then exit(a) else exit(b);
end;
begin
  read(n);
  for i:=1 to n-1 do
  read(c[i],s[i],f[i]);
  for i:=1 to n do
  begin
    x:=s[i];
    for j:=i to n-1 do
    begin
      z:=(x-s[j]);
       if z mod f[j]<>0 then
      begin
        z:=z+(f[j]-(z mod f[j]));
      end else Z:=0;
      x:=max(x+c[j],s[j]+c[j]);
      if x=s[j]+C[j] then z:=0;
      x:=x+z;
      //writeln('i=',i,' ',x);
    end;
    writeln(x);
  end;
end.


var n,i,j,ans,num,now:longint;
var c,s,f:array[0..1020] of longint;
begin
  read(n);
  for i:=1 to n-1 do read(c[i],s[i],f[i]);
  for i:=1 to n-1 do
  begin
    ans:=s[i]+c[i];now:=i+1;
    while now<n do
    begin
      if s[now]>=ans then begin ans:=s[now]+c[now];inc(now);continue;end;
      if s[now]<ans then begin
        if (ans-s[now]) mod f[now]=0 then begin inc(ans,c[now]);inc(now);
        continue;end;
        ans:=((ans-s[now]) div f[now]+1)*f[now]+s[now]+c[now];
        inc(now);
      end;
    end;
    writeln(ans);
  end;
  writeln(0);
end.


var
  n,i,j,x,y:longint;
  a:array[1..100000]of boolean;
  b:array[0..100000]of longint;
begin
  readln(n);
  a[1]:=true;
  for i:=2 to trunc(sqrt(100000)) do
    if a[i]=false then
      for j:=2 to 100000 div i do
        a[i*j]:=true;
  for i:=2 to 100000 do
    if (a[i]=false)and(a[(i+1) div 2]=false) then b[i]:=b[i-1]+1
                                             else b[i]:=b[i-1];
  for i:=1 to n do
  begin
    readln(x,y);
    write(b[y]-b[x-1]);
  end;
end.

Score : 400 points

Problem Statement

We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime.

You are given Q queries.

In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i.

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.

Sample Input 1

1
3 7

Sample Output 1

2

3 is similar to 2017, since both 3 and (3+1)/2=2 are prime.

5 is similar to 2017, since both 5 and (5+1)/2=3 are prime.

7 is not similar to 2017, since (7+1)/2=4 is not prime, although 7 is prime.

Thus, the response to the first query should be 2.

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

6
1 53
13 91
37 55
19 51
73 91
13 49

Sample Output 3

4
4
1
1
1
2


var
 i,j,n,max:longint;
 p:array[1..100000] of boolean;
 s,l,r:array[1..100000] of longint;
begin
 readln(n);
 for i:=1 to n do
   begin
      readln(l[i],r[i]);
      if r[i]>max then max:=r[i];
      if l[i]>max then max:=l[i];
   end;
  fillchar(p,sizeof(p),true);
  p[1]:=false;
  for i:=2 to trunc(sqrt(max)) do
     if p[i] then
        for j:=2 to max div i do p[i*j]:=false;
   for i:=1 to n do
         for j:=l[i] to r[i] do
            if (p[j]) and (p[(j+1) div 2]) then inc(s[i]);
    for i:=1 to n do writeln(s[i]);
end.


var a,b,c,d:longint;
begin
  readln(a,b,c,d);
  if a+b>c+d then
    writeln('Left')
  else
  if a+b=c+d then
    writeln('Balanced')
  else
    writeln('Right');
end.

Score : 100 points

Problem Statement

A balance scale tips to the left if L>R, where L is the total weight of the masses on the left pan and R is the total weight of the masses on the right pan. Similarly, it balances if L=R, and tips to the right if L<R.

Takahashi placed a mass of weight A and a mass of weight B on the left pan of a balance scale, and placed a mass of weight C and a mass of weight D on the right pan.

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Constraints

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Sample Input 1

3 8 7 1

Sample Output 1

Left

The total weight of the masses on the left pan is 11, and the total weight of the masses on the right pan is 8. Since 11>8, we should print Left.

Sample Input 2

3 4 5 2

Sample Output 2

Balanced

The total weight of the masses on the left pan is 7, and the total weight of the masses on the right pan is 7. Since 7=7, we should print Balanced.

Sample Input 3

1 7 6 4

Sample Output 3

Right

The total weight of the masses on the left pan is 8, and the total weight of the masses on the right pan is 10. Since 8<10, we should print Right.


var
  n,m,i:qword;
  k,sum:longint;
begin
  readln(n,m);
  k:=m div n;
  i:=1;
  while i<=k do
  begin
   i:=i*2;
   sum:=sum+1;
   end;
   write(sum);
end.

Score : 300 points

Problem Statement

As a token of his gratitude, Takahashi has decided to give his mother an integer sequence.
The sequence A needs to satisfy the conditions below:

A consists of integers between X and Y (inclusive).

For each 1\leq i \leq |A|-1, A_{i+1} is a multiple of A_i and strictly greater than A_i.

Find the maximum possible length of the sequence.

Constraints

1 \leq X \leq Y \leq 10^{18}

All input values are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the maximum possible length of the sequence.

Sample Input 1

3 20

Sample Output 1

3

The sequence 3,6,18 satisfies the conditions.

Sample Input 2

25 100

Sample Output 2

3

Sample Input 3

314159265 358979323846264338

Sample Output 3

31


var
 s:ansistring;
 len:longint;
begin
 read(s);
 len:=length(s);
 while s[1]='0' do dec(len);
 writeln(len);
end.

Score : 500 points

Problem Statement

You are given a string S consisting of 0 and 1.
Find the maximum integer K not greater than |S| such that we can turn all the characters of S into 0 by repeating the following operation some number of times.

Choose a contiguous segment [l,r] in S whose length is at least K (that is, r-l+1\geq K must be satisfied). For each integer i such that l\leq i\leq r, do the following: if S_i is 0, replace it with 1; if S_i is 1, replace it with 0.

Constraints

1\leq |S|\leq 10^5

S_i(1\leq i\leq N) is either 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum integer K such that we can turn all the characters of S into 0 by repeating the operation some number of times.

Sample Input 1

010

Sample Output 1

2

We can turn all the characters of S into 0 by the following operations:

Perform the operation on the segment S[1,3] with length 3. S is now 101.

Perform the operation on the segment S[1,2] with length 2. S is now 011.

Perform the operation on the segment S[2,3] with length 2. S is now 000.

Sample Input 2

100000000

Sample Output 2

8

Sample Input 3

00001111

Sample Output 3

4


var
	s:String;
	i,m,n:Longint;
begin
	readln(s);
	n:=length(s);
	m:=n;
	for i:=1 to n-1 do begin
		if s[i]<>s[i+1]then begin
			if m>i then m:=i;
			if m>n-i then m:=n-i;
		end;
	end;
	writeln(m);
end.


var a,b:Longint;begin read(a,b);writeln((a+b+1)div 2)end.

Score : 100 points

Problem Statement

You are given two positive integers a and b.
Let x be the average of a and b.
Print x rounded up to the nearest integer.

Constraints

a and b are integers.

1 \leq a, b \leq 100

Input

Input is given from Standard Input in the following format:

a b

Output

Print x rounded up to the nearest integer.

Sample Input 1

1 3

Sample Output 1

2

The average of 1 and 3 is 2.0, and it will be rounded up to the nearest integer, 2.

Sample Input 2

7 4

Sample Output 2

6

The average of 7 and 4 is 5.5, and it will be rounded up to the nearest integer, 6.

Sample Input 3

5 5

Sample Output 3

5


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

Score : 300 points

Problem Statement

You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).
Your objective is to remove some of the elements in a so that a will be a good sequence.

Here, an sequence b is a good sequence when the following condition holds true:

For each element x in b, the value x occurs exactly x times in b.

For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.

Find the minimum number of elements that needs to be removed so that a will be a good sequence.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of elements that needs to be removed so that a will be a good sequence.

Sample Input 1

4
3 3 3 3

Sample Output 1

1

We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

We can, for example, remove two occurrences of 4. Then, (2, 1, 2) is a good sequence.

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

Remove one occurrence of 10^9. Then, () is a good sequence.

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5


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


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


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


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


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


var
	s:String;
	x,y,f,cnt,i,j,off:Longint;
	d:array[1..2,1..8000]of Longint;
	c:array[1..2]of Longint;
	dp:array[1..2,1..2,1..20000]of Boolean;
	ans:Boolean;
begin
	readln(s);
	f:=1;
	for i:=1 to length(s) do begin
		if s[i]='T'then begin
			inc(c[f]);
			d[f,c[f]]:=cnt;
			cnt:=0;
			f:=3-f;
		end else inc(cnt);
	end;
	inc(c[f]);
	d[f,c[f]]:=cnt;
	read(x,y);
	off:=10000;
	dp[1,1,off+d[1,1]]:=True;
	dp[2,1,off-d[2,1]]:=True;
	dp[2,1,off+d[2,1]]:=True;
	f:=1;
	for i:=2 to c[1]do begin
		for j:=1 to 20000-d[1,i]do begin
			if dp[1,f,j]then dp[1,3-f,j+d[1,i]]:=True;
		end;
		for j:=d[1,i]+1 to 20000 do begin
			if dp[1,f,j]then dp[1,3-f,j-d[1,i]]:=True;
		end;
		for j:=1 to 20000 do dp[1,f,j]:=False;
		f:=3-f;
	end;
	ans:=dp[1,f,off+x];
	f:=1;
	for i:=2 to c[2]do begin
		for j:=1 to 20000-d[2,i]do begin
			if dp[2,f,j]then dp[2,3-f,j+d[2,i]]:=True;
		end;
		for j:=d[2,i]+1 to 20000 do begin
			if dp[2,f,j]then dp[2,3-f,j-d[2,i]]:=True;
		end;
		for j:=1 to 20000 do dp[2,f,j]:=False;
		f:=3-f;
	end;
	if ans and dp[2,f,off+y]then writeln('Yes')else writeln('No');
end.

Score : 500 points

Problem Statement

A robot is put at the origin in a two-dimensional plane.
Initially, the robot is facing in the positive x-axis direction.

This robot will be given an instruction sequence s.
s consists of the following two kinds of letters, and will be executed in order from front to back.

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

The objective of the robot is to be at coordinates (x, y) after all the instructions are executed.
Determine whether this objective is achievable.

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

|x|, |y| \leq |s|

Input

Input is given from Standard Input in the following format:

s
x y

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.

Sample Input 6

TTTT
1 0

Sample Output 6

No


var
	s:String;
	x,y,f,cnt,i,j,off:Longint;
	d:array[1..2,1..8000]of Longint;
	c:array[1..2]of Longint;
	dp:array[1..2,1..2,1..20000]of Boolean;
	ans:Boolean;
begin
	readln(s);
	f:=1;
	for i:=1 to length(s) do begin
		if s[i]='T'then begin
			inc(c[f]);
			d[f,c[f]]:=cnt;
			cnt:=0;
			f:=3-f;
		end else inc(cnt);
	end;
	inc(c[f]);
	d[f,c[f]]:=cnt;
	read(x,y);
	x:=x-d[1,1];
	off:=10000;
	dp[1,1,off]:=True;
	dp[2,1,off-d[2,1]]:=True;
	dp[2,1,off+d[2,1]]:=True;
	f:=1;
	for i:=2 to c[1]do begin
		for j:=1 to 20000-d[1,i]do begin
			if dp[1,f,j]then dp[1,3-f,j+d[1,i]]:=True;
		end;
		for j:=d[1,i]+1 to 20000 do begin
			if dp[1,f,j]then dp[1,3-f,j-d[1,i]]:=True;
		end;
		for j:=1 to 20000 do dp[1,f,j]:=False;
		f:=3-f;
	end;
	ans:=dp[1,f,off+x];
	f:=1;
	for i:=2 to c[2]do begin
		for j:=1 to 20000-d[2,i]do begin
			if dp[2,f,j]then dp[2,3-f,j+d[1,i]]:=True;
		end;
		for j:=d[2,i]+1 to 20000 do begin
			if dp[2,f,j]then dp[2,3-f,j-d[2,i]]:=True;
		end;
		for j:=1 to 20000 do dp[2,f,j]:=False;
		f:=3-f;
	end;
	if ans and dp[2,f,off+y]then writeln('Yes')else writeln('No');
end.


var n,i,max:longint;
a:array[1..200]of longint;
b:boolean;
begin
readln(n);
for i:=1 to n do
b:=true;
max:=0;
while b do
begin
b:=true;
for i:=1 to n do if a[i]mod 10 mod 2=1 then b:=false;
if b then max:=max+1;
end;
write(max);
end.

Score : 200 points

Problem Statement

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

Snuke can perform the following operation when all integers on the blackboard are even:

Replace each integer X on the blackboard by X divided by 2.

Find the maximum possible number of operations that Snuke can perform.

Constraints

1 \leq N \leq 200

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of operations that Snuke can perform.

Sample Input 1

3
8 12 40

Sample Output 1

2

Initially, [8, 12, 40] are written on the blackboard.
Since all those integers are even, Snuke can perform the operation.

After the operation is performed once, [4, 6, 20] are written on the blackboard.
Since all those integers are again even, he can perform the operation.

After the operation is performed twice, [2, 3, 10] are written on the blackboard.
Now, there is an odd number 3 on the blackboard, so he cannot perform the operation any more.

Thus, Snuke can perform the operation at most twice.

Sample Input 2

4
5 6 8 10

Sample Output 2

0

Since there is an odd number 5 on the blackboard already in the beginning, Snuke cannot perform the operation at all.

Sample Input 3

6
382253568 723152896 37802240 379425024 404894720 471526144

Sample Output 3

8


program hrn;
 var
  n,i,a,x,v:longint;
  function co(s:longint):longint;
   begin
    repeat
     if s mod 2=0 then begin
      s:=s div 2;inc(v);end;
    until s mod 2=1;
    co:=v;v:=0;
   end;
 begin
  read(n);x:=maxlongint;
  for i:=1 to n do begin
   read(a);
   if co(a)<x then x:=co(a);
  end;
  writeln(x);
 end.


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

Score : 300 points

Problem Statement

Takahashi has N balls. Initially, an integer A_i is written on the i-th ball.

He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.

Find the minimum number of balls that Takahashi needs to rewrite the integers on them.

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of balls that Takahashi needs to rewrite the integers on them.

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

For example, if we rewrite the integer on the fifth ball to 2, there are two different integers written on the balls: 1 and 2.
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1.

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.

Sample Input 3

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

Sample Output 3

3


var
 i,x,k,j,n,s:longint;
 sum:int64;
 a,b:array[0..200000] of longint;
procedure kp(l,r:longint);
var i,j,m,t:longint;
begin
 i:=l; j:=r;
 m:=a[(l+r) div 2];
 repeat
  while a[i]<m do inc(i);
  while a[j]>m 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 kp(l,j);
 if i<r then kp(i,r);
end;
begin
 readln(n,k);
 for i:=1 to n do
  begin
   read(x);
   inc(b[x]);
   if b[x]=1 then inc(s);
  end;
 for i:=n downto 1 do
  begin
   if b[i]<>0 then
    begin
     inc(j);
     a[j]:=b[i];
    end;
  end;
 kp(1,j);
 for i:=1 to s-k do
  sum:=sum+a[i];
 writeln(sum);
end.


var n,k:longint;
    i:longint;
    x:longint;
    c:array[0..205000]of longint;
    max,num:longint;
procedure qs(l,r:longint);
var i,j,m,t:longint;
begin
  i:=l;
  j:=r;
  m:=c[(l+r)>>1];
  repeat
    while c[i]<m do inc(i);
    while c[j]>m do dec(j);
    if i<=j then
    begin
      t:=c[i];c[i]:=c[j];c[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,k);
  for i:=1 to n do
  begin
    read(x);
    if c[x]=0 then dec(k);
    inc(c[x]);
    if x>max then max:=x;
  end;
  k:=-k;
  qs(1,max);
  num:=0;
  for i:=1 to max+1 do
  if c[i]>0 then
  begin
    if k<=0 then
    begin
      writeln(num);
      exit;
    end;
    dec(k);
    inc(num,c[i]);
  end;
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,math;
var
  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;
  xc,yc,space:char;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Read(n);
    Read(a);
    Readln(b);

    Writeln(((n*a)+b-abs((n*a)-b))div 2);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

You are parking at a parking lot. You can choose from the following two fee plans:

Plan 1: The fee will be A×T yen (the currency of Japan) when you park for T hours.

Plan 2: The fee will be B yen, regardless of the duration.

Find the minimum fee when you park for N hours.

Constraints

1≤N≤20

1≤A≤100

1≤B≤2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

When the minimum fee is x yen, print the value of x.

Sample Input 1

7 17 120

Sample Output 1

119

If you choose Plan 1, the fee will be 7×17=119 yen.

If you choose Plan 2, the fee will be 120 yen.

Thus, the minimum fee is 119 yen.

Sample Input 2

5 20 100

Sample Output 2

100

The fee might be the same in the two plans.

Sample Input 3

6 18 100

Sample Output 3

100


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;
  xc,yc,space:char;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(s);
    fx:=0;

    for i := 1 to Length(s) do
      fx:=fx+StrToInt(s[i]);

    if StrToInt(s) mod fx =0 then
      Writeln('Yes')
    else
      Writeln('No');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

An integer X is called a Harshad number if X is divisible by f(X), where f(X) is the sum of the digits in X when written in base 10.

Given an integer N, determine whether it is a Harshad number.

Constraints

1?N?10^8

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print Yes if N is a Harshad number; print No otherwise.

Sample Input 1

12

Sample Output 1

Yes

f(12)=1+2=3. Since 12 is divisible by 3, 12 is a Harshad number.

Sample Input 2

57

Sample Output 2

No

f(57)=5+7=12. Since 57 is not divisible by 12, 12 is not a Harshad number.

Sample Input 3

148

Sample Output 3

No


var
	n,i,j,ans,now,c,a,k:Longint;
	f,p:array[1..100,0..10]of Longint;
begin
	ans:=-1000000000;
	read(n);
	for i:=1 to n do for j:=0 to 9 do read(f[i,j]);
	for i:=1 to n do for j:=0 to 10 do read(p[i,j]);
	for i:=1 to 1023 do begin
		now:=0;
		for j:=1 to n do begin
			c:=0;
			a:=i;
			for k:=0 to 9 do begin
				if(a mod 2=1)and(f[j,k]=1)then inc(c);
				a:=a div 2;
			end;
			inc(now,p[j,c]);
		end;
		if ans<now then ans:=now;
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

Joisino is planning to open a shop in a shopping street.

Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods.

There are already N stores in the street, numbered 1 through N.

You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2.

Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}.

Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.

Constraints

1≤N≤100

0≤F_{i,j,k}≤1

For every integer i such that 1≤i≤N, there exists at least one pair (j,k) such that F_{i,j,k}=1.

-10^7≤P_{i,j}≤10^7

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2}
:
F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2}
P_{1,0} ... P_{1,10}
:
P_{N,0} ... P_{N,10}

Output

Print the maximum possible profit of Joisino's shop.

Sample Input 1

1
1 1 0 1 0 0 0 1 0 1
3 4 5 6 7 8 9 -2 -3 4 -2

Sample Output 1

8

If her shop is open only during the periods when Shop 1 is opened, the profit will be 8, which is the maximum possible profit.

Sample Input 2

2
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1
0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1

Sample Output 2

-2

Note that a shop must be open during at least one period, and the profit may be negative.

Sample Input 3

3
1 1 1 1 1 1 0 0 1 1
0 1 0 1 1 1 1 0 1 0
1 0 1 1 0 1 0 1 0 1
-8 6 -2 -8 -8 4 8 7 -6 2 2
-9 2 0 1 7 -5 0 -2 -6 5 5
6 -6 7 -9 6 -5 8 0 -9 -7 -7

Sample Output 3

23


var n:longint;
    i,j:longint;
    a,c:array[0..250,0..20]of longint;
    v:array[0..20]of boolean;
    max:int64;
procedure check;
var i,j,ans,ci:longint;
begin
  ans:=0;
  for i:=1 to n do
  begin
    ci:=0;
    for j:=1 to 10 do
    if (v[j]) then inc(ci,a[i,j]);
    inc(ans,c[i,ci]);
  end;
  if ans>max then max:=ans;
end;
procedure dfs(dep:longint;last:longint);
var i:longint;
begin
  if dep>0 then check;
  for i:=last+1 to 10 do
  begin
    v[i]:=true;
    dfs(dep+1,i);
    v[i]:=false;
  end;
end;
begin
  read(n);
  max:=-maxlongint;
  for i:=1 to n do
    for j:=1 to 10 do
    read(a[i,j]);
  for i:=1 to n do
    for j:=0 to 10 do
    read(c[i,j]);
  dfs(0,0);
  writeln(max);
end.


var i,j:longint;
    num:array[0..250]of longint;
    c:array[0..250,0..20]of longint;
    x,cut,toinc,where:longint;
    use:boolean;
    n:longint;
    ans:int64;
function max(a,b:longint):longint;
begin
  if a>b then exit(a)  else exit(b);
end;
begin
  read(n);
  for i:=1 to n do
    for j:=1 to 10 do
    begin
      read(x);
      inc(num[i],x);
    end;
  for i:=1 to n do
    for j:=0 to 10 do
    read(c[i,j]);
  toinc:=-maxlongint;
  cut:=maxlongint;
  for i:=1 to n do
  begin
    x:=-maxlongint;
    for j:=0 to num[i] do
    begin
      if x<c[i,j] then
      begin
        x:=c[i,j];
        where:=j;
      end;
      if (j<>0)and(c[i,j]>toinc) then toinc:=c[i,j];
    end;
    if where=0 then if cut>x then cut:=x;
    if where<>0 then use:=true;
    inc(ans,x);
  end;
  if not use then ans:=ans-cut+toinc;
  writeln(ans);
end.


var
k,a,b,s,x:qword;
begin
read(k,a,b);
s:=0;
x:=0;
if b>=a then write(-1)
  else
    begin
      while x<k do
        begin
          x:=x+a-b;
          s:=s+2;
        end;
      write(s-1);
    end;
end.

Score : 100 points

Problem Statement

ButCoder Inc. runs a programming competition site called ButCoder. In this site, a user is given an integer value called rating that represents his/her skill, which changes each time he/she participates in a contest. The initial value of a new user's rating is 0, and a user whose rating reaches K or higher is called Kaiden ("total transmission"). Note that a user's rating may become negative.

Hikuhashi is a new user in ButCoder. It is estimated that, his rating increases by A in each of his odd-numbered contests (first, third, fifth, ...), and decreases by B in each of his even-numbered contests (second, fourth, sixth, ...).

According to this estimate, after how many contests will he become Kaiden for the first time, or will he never become Kaiden?

Constraints

1 ≤ K, A, B ≤ 10^{18}

All input values are integers.

Input

Input is given from Standard Input in the following format:

K A B

Output

If it is estimated that Hikuhashi will never become Kaiden, print -1. Otherwise, print the estimated number of contests before he become Kaiden for the first time.

Sample Input 1

4000 2000 500

Sample Output 1

5

Each time Hikuhashi participates in a contest, his rating is estimated to change as 0 → 2000 → 1500 → 3500 → 3000 → 5000 → … After his fifth contest, his rating will reach 4000 or higher for the first time.

Sample Input 2

4000 500 2000

Sample Output 2

-1

Each time Hikuhashi participates in a contest, his rating is estimated to change as 0 → 500 → -1500 → -1000 → -3000 → -2500 → … He will never become Kaiden.

Sample Input 3

1000000000000000000 2 1

Sample Output 3

1999999999999999997

The input and output values may not fit into 32-bit integers.


var
 k,a,b,s,x:longint;
begin
 read(k,a,b);
 if b>=a then begin writeln('-1');halt;end;
 if a>=k then begin writeln(1);halt;end;
 x:=k-a;
 s:=1+x div (b-a);
 if x mod (b-a)<>0 then inc(s);
 writeln(s);
end.


var
  n,k,i,j,x,y:longint;
  a:array[0..30000000] of int64;
  max:int64;
begin
  readln(n);
  fillchar(a,sizeof(a),0);
  k:=30000000 div n;
  for i:=1 to n do
  begin
    readln(x,y);
    for j:=1 to k do
      if j mod x=0 then a[j]:=a[j]+y;
  end;
  max:=0;
  for i:=1 to k do
    if a[i]>max then max:=a[i];
  writeln(max);
end.

Score : 100 points

Problem Statement

Consider an infinite sequence a_1, a_2, … Initially, the values of all the terms are 0, and from this state we will sequentially perform Q operations. The i-th operation (1 ≤ i ≤ Q) is as follows:

For every positive integer j, add x_i to the value of a_{j × m_i}.

Find the value of the largest term after these Q operations.

Constraints

1 ≤ Q ≤ 299

2 ≤ m_i ≤ 300

-10^6 ≤ x_i ≤ 10^6

All m_i are distinct.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
m_1 x_1
:
m_Q x_Q

Output

Print the value of the largest term after the Q operations.

Sample Input 1

3
2 10
3 -20
6 15

Sample Output 1

10

The values of each terms in the sequence a_1, a_2, … change as follows:

Before the operations: 0, 0, 0, 0, 0, 0, …

After the 1-st operation: 0, 10, 0, 10, 0, 10, …

After the 2-nd operation: 0, 10, -20, 10, 0, -10, …

After the 3-rd operation: 0, 10, -20, 10, 0, 5, …

The value of the largest term after all the operations is 10.

Sample Input 2

3
10 -3
50 4
100 -5

Sample Output 2

1

Sample Input 3

5
56 114834
72 -149861
100 190757
192 -132693
240 133108

Sample Output 3

438699


{$R+,S+,Q+,I+,O-}
{R-,S-,Q-,I-,O+}
// Code By H~$~C

uses math;

type
  int32 = longint;
  double = extended;
  bool = boolean;

const inf = 1061109567;

var
  i, j, swap_tmp, x, y, z: int32;
  n, ans: int32;
  h, p: array [0 .. 5005] of int32;
  dp: array [0 .. 5005] of int64;

function qsort_less(i, j: int32): bool;
begin
  qsort_less := (h[i] + p[i]) < (h[j] + p[j]);
end;
procedure qsort_swap(i, j: int32);
  var _swap_tmp: int32;
begin
  _swap_tmp := h[i]; h[i] := h[j]; h[j] := _swap_tmp;
  _swap_tmp := p[i]; p[i] := p[j]; p[j] := _swap_tmp;
end;
procedure qsort(_l, _r: int32);
  var i, j, _p: int32;
begin
  if (_r = _l) then exit;
  i := _l; j := _r;
  _p := _l;
  repeat
    while qsort_less(i, _p) do inc(i);
    while qsort_less(_p, j) do dec(j);
    if (i <= j) then begin
      qsort_swap(i, j);
      inc(i); dec(j);
    end;
  until (i > j);
  if (i < _r) then qsort(i, _r);
  if (j > _l) then qsort(_l, j);
end;

begin
//  assign(input,'in'); reset(input);
//  assign(output,'1.out'); rewrite(output);
  randseed := 6714;

  readln(n);
  for i := 1 to n do begin
    readln(h[i], p[i]);
  end;
  qsort(1, n);
  for i := 1 to n do begin
    writeln(h[i], ' ', p[i]);
  end;
  exit;

  fillchar(dp, sizeof(dp), 63);
  dp[0] := 0;
  for i := 1 to n do begin
    for j := i - 1 downto 0 do begin
      if (dp[j] <= h[i]) and (dp[j + 1] > dp[j] + p[i]) then begin
        dp[j + 1] := dp[j] + p[i];
      end;
    end;
  end;

  ans := 0;
  for i := 0 to n do begin
    if (dp[i] < 1000000000000000000) then begin
      ans := i;
    end;
  end;
  writeln(ans);

  close(input);
  close(output);
end.

Score : 700 points

Problem Statement

In the final of CODE FESTIVAL in some year, there are N participants.
The height and power of Participant i is H_i and P_i, respectively.

Ringo is hosting a game of stacking zabuton (cushions).

The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton.
Initially, the stack is empty.
When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.

Ringo wants to maximize the number of participants who can add zabuton to the stack.
How many participants can add zabuton to the stack in the optimal order of participants?

Constraints

1 \leq N \leq 5000

0 \leq H_i \leq 10^9

1 \leq P_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 P_1
H_2 P_2
:
H_N P_N

Output

Print the maximum number of participants who can add zabuton to the stack.

Sample Input 1

3
0 2
1 3
3 4

Sample Output 1

2

When the participants line up in the same order as the input, Participants 1 and 3 will be able to add zabuton.

On the other hand, there is no order such that all three participants can add zabuton. Thus, the answer is 2.

Sample Input 2

3
2 4
3 1
4 1

Sample Output 2

3

When the participants line up in the order 2, 3, 1, all of them will be able to add zabuton.

Sample Input 3

10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1

Sample Output 3

5


{$R+,S+,Q+,I+,O-}
{R-,S-,Q-,I-,O+}
// Code By H~$~C

uses math;

type
  int32 = longint;
  double = extended;
  bool = boolean;

const inf = 1061109567;

var
  i, j, swap_tmp, x, y, z: int32;
  n, ans: int32;
  h, p: array [0 .. 5005] of int32;
  dp: array [0 .. 5005, 0 .. 5005] of int64;

function qsort_less(i, j: int32): bool;
begin
  qsort_less := (h[i] + p[i] < h[j] + p[j]);
end;
procedure qsort_swap(i, j: int32);
  var _swap_tmp: int32;
begin
  _swap_tmp := h[i]; h[i] := h[j]; h[j] := _swap_tmp;
  _swap_tmp := p[i]; p[i] := p[j]; p[j] := _swap_tmp;
end;
procedure qsort(l, h: int32);
  var i, j, p: int32;
begin
  i := l; j := h;
  p := l + random(h - l + 1);
  repeat
    while qsort_less(i, p) do inc(i);
    while qsort_less(p, j) do dec(j);
    if (i <= j) then begin
      qsort_swap(i, j);
      inc(i); dec(j);
    end;
  until (i > j);
  if (i < h) then qsort(i, h);
  if (j > l) then qsort(l, j);
end;

begin
//  assign(input,'in'); reset(input);
//  assign(output,'out'); rewrite(output);
  randseed := 6714;

  readln(n);
  for i := 1 to n do begin
    readln(h[i], p[i]);
  end;
  qsort(1, n);

  fillchar(dp, sizeof(dp), 63);
  dp[0, 0] := 0;
  for i := 1 to n do begin
    for j := 0 to i - 1 do begin
      dp[i, j] := dp[i - 1, j];
    end;
    for j := 0 to i - 1 do begin
      if (dp[i - 1, j] <= h[i]) and (dp[i, j + 1] > dp[i - 1, j] + p[i]) then begin
        dp[i, j + 1] := dp[i - 1, j] + p[i];
      end;
    end;
  end;

  ans := 0;
  for i := 0 to n do begin
    if (dp[n, i] < 1000000000000000000) then begin
      ans := i;
    end;
  end;
  writeln(ans);

  close(input);
  close(output);
end.


var cc : array [0..100] of int64;
    i,j,k,n : int64 ;
begin
 readln(n) ;
 cc[0]:=2;
 cc[1]:=1;
 for i:=2 to n do
  cc[i]:=cc[i-1]+cc[i-2];
 writeln(cc[n]);
end.

Score : 100 points

Problem Statement

We call a 4-digit integer with three or more consecutive same digits, such as 1118, good.

You are given a 4-digit integer N. Answer the question: Is N good?

Constraints

1000 ≤ N ≤ 9999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is good, print Yes; otherwise, print No.

Sample Input 1

1118

Sample Output 1

Yes

N is good, since it contains three consecutive 1.

Sample Input 2

7777

Sample Output 2

Yes

An integer is also good when all the digits are the same.

Sample Input 3

1234

Sample Output 3

No


var
n:integer;
function f(x:integer):qword;
begin
 if x=0 then exit(2)
   else if x=1 then exit(1)
               else exit(f(x-1)+f(x-2));
end;
begin
read(n);
writeln(f(n));
end.

Score : 200 points

Problem Statement

It is November 18 now in Japan. By the way, 11 and 18 are adjacent Lucas numbers.

You are given an integer N. Find the N-th Lucas number.

Here, the i-th Lucas number L_i is defined as follows:

L_0=2

L_1=1

L_i=L_{i-1}+L_{i-2} (i≥2)

Constraints

1≤N≤86

It is guaranteed that the answer is less than 10^{18}.

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


var
  x1,x2,x3:char;
  i,j,k,n,s:longint;
begin
  readln(n);
  for i:=0 to 1 do
    for j:=0 to 1 do
      for k:=0 to 1 do
        begin
          s:=n div 1000;
          if i=0 then begin s:=s+n div 100 mod 10;x1:='+';end
            else begin s:=s-n div 100 mod 10;x1:='-';end;
          if j=0 then begin s:=s+n div 10 mod 10;x2:='+';end
            else begin s:=s-n div 10 mod 10;x2:='-';end;
          if k=0 then begin s:=s+n mod 10;x3:='+';end
            else begin s:=s-n mod 10;x3:='-';end;
          if s=7 then begin writeln(n div 1000,x1,n div 100 mod 10,x2,n div 10 mod 10,x3,n mod 10);exit;end;
        end;
end.

Score : 300 points

Problem Statement

Sitting in a station waiting room, Joisino is gazing at her train ticket.

The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).

In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.

The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

Constraints

0≤A,B,C,D≤9

All input values are integers.

It is guaranteed that there is a solution.

Input

Input is given from Standard Input in the following format:

ABCD

Output

Print the formula you made, including the part =7.

Use the signs + and -.

Do not print a space between a digit and a sign.

Sample Input 1

1222

Sample Output 1

1+2+2+2=7

This is the only valid solution.

Sample Input 2

0290

Sample Output 2

0-2+9+0=7

0 - 2 + 9 - 0 = 7 is also a valid solution.

Sample Input 3

3242

Sample Output 3

3+2+4-2=7


var c,y,next,first,dist,q:array[0..500020] of int64;
var v:array[0..500020] of boolean;
var n,m,i,j,s,x,y1,z,tot,ans:longint;
procedure add(x,y1,z:longint);
begin
  inc(tot);next[tot]:=first[x];first[x]:=tot;y[tot]:=y1;c[tot]:=z;
end;
procedure spfa(s:longint);
var i,now,h,t:longint;
begin
  for i:=0 to 200 do begin v[i]:=false;dist[i]:=2147483647;end;dist[s]:=0;
  h:=0;t:=1;v[s]:=true;q[1]:=s;
  while h<t do
  begin
    inc(h);now:=q[h];i:=first[now];
    while i<>-1 do
    begin
      if dist[y[i]]>dist[now]+c[i] then
      begin
        dist[y[i]]:=dist[now]+c[i];
        if not v[y[i]] then
        begin
          inc(t);q[t]:=y[i];v[y[i]]:=true;
        end;
      end;
      i:=next[i];
    end;
    v[now]:=false;
  end;
end;
begin
  read(n,m);
  for i:=0 to 200 do first[i]:=-1;
  for i:=0 to 9 do for j:=0 to 9 do begin read(x);if x<>0 then
  begin //add(i+1,j+1,x);
  add(j+1,i+1,x);
  end;end;
  spfa(2);
  //for i:=1 to 10 do writeln(dist[i]);ans:=0;
  for i:=1 to n do for j:=1 to m do begin read(x);
  if x>=0 then inc(ans,dist[x+1]);end;writeln(ans);
end.

Score : 400 points

Problem Statement

Joisino the magical girl has decided to turn every single digit that exists on this world into 1.

Rewriting a digit i with j (0≤i,j≤9) costs c_{i,j} MP (Magic Points).

She is now standing before a wall. The wall is divided into HW squares in H rows and W columns, and at least one square contains a digit between 0 and 9 (inclusive).

You are given A_{i,j} that describes the square at the i-th row from the top and j-th column from the left, as follows:

If A_{i,j}≠-1, the square contains a digit A_{i,j}.

If A_{i,j}=-1, the square does not contain a digit.

Find the minimum total amount of MP required to turn every digit on this wall into 1 in the end.

Constraints

1≤H,W≤200

1≤c_{i,j}≤10^3 (i≠j)

c_{i,j}=0 (i=j)

-1≤A_{i,j}≤9

All input values are integers.

There is at least one digit on the wall.

Input

Input is given from Standard Input in the following format:

H W
c_{0,0} ... c_{0,9}
:
c_{9,0} ... c_{9,9}
A_{1,1} ... A_{1,W}
:
A_{H,1} ... A_{H,W}

Output

Print the minimum total amount of MP required to turn every digit on the wall into 1 in the end.

Sample Input 1

2 4
0 9 9 9 9 9 9 9 9 9
9 0 9 9 9 9 9 9 9 9
9 9 0 9 9 9 9 9 9 9
9 9 9 0 9 9 9 9 9 9
9 9 9 9 0 9 9 9 9 2
9 9 9 9 9 0 9 9 9 9
9 9 9 9 9 9 0 9 9 9
9 9 9 9 9 9 9 0 9 9
9 9 9 9 2 9 9 9 0 9
9 2 9 9 9 9 9 9 9 0
-1 -1 -1 -1
8 1 1 8

Sample Output 1

12

To turn a single 8 into 1, it is optimal to first turn 8 into 4, then turn 4 into 9, and finally turn 9 into 1, costing 6 MP.

The wall contains two 8s, so the minimum total MP required is 6×2=12.

Sample Input 2

5 5
0 999 999 999 999 999 999 999 999 999
999 0 999 999 999 999 999 999 999 999
999 999 0 999 999 999 999 999 999 999
999 999 999 0 999 999 999 999 999 999
999 999 999 999 0 999 999 999 999 999
999 999 999 999 999 0 999 999 999 999
999 999 999 999 999 999 0 999 999 999
999 999 999 999 999 999 999 0 999 999
999 999 999 999 999 999 999 999 0 999
999 999 999 999 999 999 999 999 999 0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Sample Output 2

0

Note that she may not need to change any digit.

Sample Input 3

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

Sample Output 3

47


var c,y,next,first,dist,q:array[0..500020] of int64;
var v:array[0..500020] of boolean;
var n,m,i,j,s,x,y1,z,tot,ans:longint;
procedure add(x,y1,z:longint);
begin
  inc(tot);next[tot]:=first[x];first[x]:=tot;y[tot]:=y1;c[tot]:=z;
end;
procedure spfa(s:longint);
var i,now,h,t:longint;
begin
  for i:=0 to 200 do begin v[i]:=false;dist[i]:=2147483647;end;dist[s]:=0;
  h:=0;t:=1;v[s]:=true;q[1]:=s;
  while h<t do
  begin
    inc(h);now:=q[h];i:=first[now];
    while i<>-1 do
    begin
      if dist[y[i]]>dist[now]+c[i] then
      begin
        dist[y[i]]:=dist[now]+c[i];
        if not v[y[i]] then
        begin
          inc(t);q[t]:=y[i];v[y[i]]:=true;
        end;
      end;
      i:=next[i];
    end;
    v[now]:=false;
  end;
end;
begin
  read(n,m);
  for i:=0 to 200 do first[i]:=-1;
  for i:=0 to 9 do for j:=0 to 9 do begin read(x);//if x<>0 then
  begin add(i+1,j+1,x);add(j+1,i+1,x);end;end;
  spfa(2);
  //for i:=1 to 10 do writeln(dist[i]);ans:=0;
  for i:=1 to n do for j:=1 to m do begin read(x);
  if x>=0 then inc(ans,dist[x+1]);end;writeln(ans);
end.


var n,m:longint;
    i,j,k:longint;
    a:array[0..10,0..10]of longint;
    x:longint;
    ans:longint;
begin
  readln(n,m);
  for i:=0 to 9 do
  begin
    for j:=0 to 9 do
    read(a[i,j]);
    readln;
  end;
  for k:=0 to 9 do
  for i:=0 to 9 do
  for j:=0 to 9 do
  if a[i,j]>a[i,k]+a[k,j] then a[i,j]:=a[i,k]+a[k,j];
  for i:=1 to n*m do
  begin
    read(x);
    if x<>-1 then inc(ans,a[x,1]);
  end;
  writeln(ans);
end.


program Project1;

{$APPTYPE CONSOLE}

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

    if Ord(xc)>Ord(yc) then
      Writeln('>')
    else if Ord(xc)=Ord(yc) then
      Writeln('=')
    else
      Writeln('<');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 100 points

Problem Statement

In programming, hexadecimal notation is often used.

In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters A, B, C, D, E and F are used to represent the values 10, 11, 12, 13, 14 and 15, respectively.

In this problem, you are given two letters X and Y. Each X and Y is A, B, C, D, E or F.

When X and Y are seen as hexadecimal numbers, which is larger?

Constraints

Each X and Y is A, B, C, D, E or F.

Input

Input is given from Standard Input in the following format:

X Y

Output

If X is smaller, print <; if Y is smaller, print >; if they are equal, print =.

Sample Input 1

A B

Sample Output 1

<

10 < 11.

Sample Input 2

E C

Sample Output 2

>

14 > 12.

Sample Input 3

F F

Sample Output 3

=

15 = 15.


program ISU;
var x,y,z,t : integer;
begin
	read(x);
	read(y);
	read(z);
	x := x-z;
	x := x div (y+z);
	writeln(x);
end.

Score : 200 points

Problem Statement

We have a long seat of width X centimeters.
There are many people who wants to sit here. A person sitting on the seat will always occupy an interval of length Y centimeters.

We would like to seat as many people as possible, but they are all very shy, and there must be a gap of length at least Z centimeters between two people, and between the end of the seat and a person.

At most how many people can sit on the seat?

Constraints

All input values are integers.

1 \leq X, Y, Z \leq 10^5

Y+2Z \leq X

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the answer.

Sample Input 1

13 3 1

Sample Output 1

3

There is just enough room for three, as shown below:

Figure

Sample Input 2

12 3 1

Sample Output 2

2

Sample Input 3

100000 1 1

Sample Output 3

49999

Sample Input 4

64146 123 456

Sample Output 4

110

Sample Input 5

64145 123 456

Sample Output 5

109


uses math;
const test: integer = 1900;
var
  n, m: integer;

begin
  readln(n, m);
  writeln(((1900 * m + 100 * (n - m)) * power(2, m)):0:0);
end.

Score : 300 points

Problem Statement

Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is YES or NO.

When he checked the detailed status of the submission, there were N test cases in the problem, and the code received TLE in M of those cases.

Then, he rewrote the code to correctly solve each of those M cases with 1/2 probability in 1900 milliseconds, and correctly solve each of the other N-M cases without fail in 100 milliseconds.

Now, he goes through the following process:

Submit the code.

Wait until the code finishes execution on all the cases.

If the code fails to correctly solve some of the M cases, submit it again.

Repeat until the code correctly solve all the cases in one submission.

Let the expected value of the total execution time of the code be X milliseconds. Print X (as an integer).

Constraints

All input values are integers.

1 \leq N \leq 100

1 \leq M \leq {\rm min}(N, 5)

Input

Input is given from Standard Input in the following format:

N M

Output

Print X, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, X is an integer not exceeding 10^9.

Sample Input 1

1 1

Sample Output 1

3800

In this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with 1/2 probability in 1900 milliseconds.

The code will succeed in one attempt with 1/2 probability, in two attempts with 1/4 probability, and in three attempts with 1/8 probability, and so on.

Thus, the answer is 1900 \times 1/2 + (2 \times 1900) \times 1/4 + (3 \times 1900) \times 1/8 + ... = 3800.

Sample Input 2

10 2

Sample Output 2

18400

The code will take 1900 milliseconds in each of the 2 cases, and 100 milliseconds in each of the 10-2=8 cases. The probability of the code correctly solving all the cases is 1/2 \times 1/2 = 1/4.

Sample Input 3

100 5

Sample Output 3

608000


uses math;
var n,z,w,i:longint;
    p:array[0..2000]of longint;
begin
  readln(n,z,w);
  for i:=1 to n do
    read(p[n]);
  if n=1 then
    writeln(abs(p[n])-w)
  else
    writeln(max(abs(p[n]-p[n-1]),abs(p[n]-w)));
end.

Score : 500 points

Problem Statement

We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is a_i.

Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:

Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.

The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

Constraints

All input values are integers.

1 \leq N \leq 2000

1 \leq Z, W, a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N Z W
a_1 a_2 ... a_N

Output

Print the score.

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900

If X draws two cards first, Y will draw the last card, and the score will be |1000 - 100| = 900.

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900

If X draws all the cards first, the score will be |1000 - 100| = 900.

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999


var z,w,n,i,j,ans,max,min:longint;
var a:array[0..2020] of longint;
begin
  read(n,z,w);min:=2147483647;max:=-2147483647;
  for i:=1 to n do begin read(a[i]);if a[i]<min then min:=a[i];if a[i]>max then max:=a[i];end;
  ans:=abs(max-a[n]);
  if ans<abs(z-min) then ans:=abs(z-min);
  if ans<abs(w-max) then ans:=abs(w-max);writeln(ans);
 end.


Uses math;
var
	n,i,z,w,a,b:Longint;
begin
	read(n,z,w);
	if n=1 then begin
		read(b);
		writeln(abs(b-w));
		exit;
	end;
	for i:=1 to n-2 do read(a);
	read(a,b);
	writeln(Max(abs(b-w),abs(a-b)));
end.


Uses math;
var
	n,i,z,w,a,b,t:Longint;
	s:String;
begin
	readln(n,z,w);
	if n=1 then begin
		read(b);
		writeln(abs(b-w));
		exit;
	end;
	setlength(s,n*11+100);
	readln(s);
	i:=length(s);
	while s[i]>' ' do dec(i);
	for z:=i+1 to length(s) do b:=b*10+ord(s[z])-48;
	dec(i);
	t:=i;
	while(i>0)and(s[i]>' ')do dec(i);
	for z:=i+1 to t do a:=a*10+ord(s[z])-48;
	writeln(Max(abs(b-w),abs(a-b)));
end.


program HelloWorld;
    var n,z,w:longint;
    var i,j:longint;
    var a:array [1..2000] of longint;
    var sx,sy:array[1..2000]of longint;
    var p,q:longint;
    var turn:boolean;
    var t:longint;
begin
    readln(n,z,w);
    for i:=1 to n do read(a[i]);
    for i:=n-1 downto 1 do
    begin
      sx[i]:=0;
      sy[i]:=1000000001;
      for j:=i+1 to n do
      begin
        if abs(a[i]-a[j])>=sx[i] then sx[i]:=abs(a[i]-a[j]);
        if abs(a[i]-a[j])<=sy[i] then sy[i]:=abs(a[i]-a[j]);
      end;
    end;
    p:=0;
    turn:=true;
    if n=1 then
      writeln(abs(w-a[n]))
    else
    begin
      while p<>n do
      begin
        if turn then
        begin
          sy[n]:=abs(a[n]-w);
          t:=abs(a[n]-w);;
          for i:=p+1 to n do
            if sy[i]>=t then
            begin
              t:=sy[i];
              q:=i;
            end;
          z:=a[q];
          p:=q;
          turn:=false;
        end
        else
        begin
          sx[n]:=abs(a[n]-z);
          t:=abs(a[n]-z);
          for i:=p+1 to n do
            if sx[i]<=t then
            begin
              t:=sx[i];
              q:=i;
            end;
          w:=a[q];
          p:=q;
          turn:=true;
        end;
      end;
      writeln(abs(z-w));
    end;
end.


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


//fpc 3.0.0

program HelloWorld;
    var n,z,w:longint;
    var i,j:longint;
    var a:array [1..2000] of longint;
    var sx,sy:array[1..2000]of longint;
    var p,q:longint;
    var turn:boolean;
    var t:longint;
begin
    readln(n,z,w);
    for i:=1 to n do read(a[i]);
    for i:=n-1 downto 1 do
    begin
      sx[i]:=0;
      sy[i]:=1000000001;
      for j:=i+1 to n do
      begin
        if abs(a[i]-a[j])>=sx[i] then sx[i]:=abs(a[i]-a[j]);
        if abs(a[i]-a[j])<=sy[i] then sy[i]:=abs(a[i]-a[j]);
      end;
    end;
    p:=0;
    turn:=true;
    if n=1 then
      writeln(abs(w-a[n]))
    else
    begin
      while p<>n do
      begin
        if turn then
        begin
          sy[n]:=abs(a[n]-w);
          t:=0;
          for i:=p+1 to n do
            if sy[i]>=t then
            begin
              t:=sy[i];
              q:=i;
            end;
          z:=a[q];
          p:=q;
          turn:=false;
        end
        else
        begin
          sx[n]:=abs(a[n]-z);
          t:=1000000001;
          for i:=p+1 to n do
            if sx[i]<=t then
            begin
              t:=sx[i];
              q:=i;
            end;
          w:=a[q];
          p:=q;
          turn:=true;
        end;
      end;
      writeln(abs(z-w));
    end;
end.


var
  n,i,j,k:longint;ans:int64;
  a,f:array[1..100] of int64;
begin
  read(n);
  for i:=1 to n do
    read(a[i]);
  for i:=1 to n do
    begin
      k:=i;
      while k<=n do
        begin
          f[i]:=f[i]+a[k];
          k:=k+i;
        end;
    end;
  for i:=n downto 1 do
    if f[i]<0 then
      begin
        j:=i;
        while j<=n do
          begin
            a[j]:=0;
            j:=j+i;
          end;
        for j:=1 to i do
          begin
            f[i]:=0;
            k:=j;
            while k<=n do
              begin
                f[i]:=f[i]+a[k];
                k:=k+j;
              end;
          end;
      end;
  ans:=0;
  for i:=1 to n do
    ans:=ans+a[i];
  write(ans);
end.

Score : 700 points

Problem Statement

We have N gemstones labeled 1 through N.

You can perform the following operation any number of times (possibly zero).

Select a positive integer x, and smash all the gems labeled with multiples of x.

Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan).
However, a_i may be negative, in which case you will be charged money.

By optimally performing the operation, how much yen can you earn?

Constraints

All input values are integers.

1 \leq N \leq 100

|a_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the maximum amount of money that can be earned.

Sample Input 1

6
1 2 -6 4 5 3

Sample Output 1

12

It is optimal to smash Gem 3 and 6.

Sample Input 2

6
100 -100 -100 -100 100 -100

Sample Output 2

200

Sample Input 3

5
-1 -2 -3 -4 -5

Sample Output 3

0

It is optimal to smash all the gems.

Sample Input 4

2
-1000 100000

Sample Output 4

99000


var
  x,y,z:array[0..1000] of int64;
  a,b,c,e,n:longint;d:int64;
begin
  read(n);
  for a:=1 to n do
    begin
      read(x[a]);
      if x[a]<0 then begin
       b:=b+1;
               y[b]:=a;
               end;
    end;
  for a:=b downto 1 do
    begin
      c:=2;d:=x[y[a]];
      while c*y[a]<=100 do
        begin
          if z[c*y[a]]=0 then d:=d+x[c*y[a]];
          c:=c+1;
        end;
      if d<0 then for e:=1 to c do
               z[e*y[a]]:=1;
   end; d:=0;
   for a:=1 to n do
     if z[a]=0 then d:=d+x[a];
     writeln(d);
     end.


var  s:array[1..1000]of  boolean;
     a,f:array[1..1000]of  int64;
     n,i,j,k,l:longint;
     tot,min:int64;
procedure  inf;
var  i,j,k,l:longint;
begin
  fillchar(f,sizeof(f),0);
  for  i:=1  to  n  do
    if  s[i]=false  then
      for  j:=i  to  n  do
        if  (j mod i=0)and(s[j]=false)then
          f[i]:=f[i]+a[j];
end;
begin
  readln(n);
  tot:=0;
  for  i:=1  to  n  do
    begin
      read(a[i]);
      inc(tot,a[i]);
    end;

  repeat
    inf;
    min:=999999999999999999;
    for  i:=1  to  n  do
      if  f[i]<min
        then
          begin
            min:=f[i];
            k:=i;
          end;
    if  min>=0  then  break;
    for  i:=1  to  n  do
      if  i mod k=0  then  s[i]:=true;
    dec(tot,min);
  until  false;
  writeln(tot);
end.


var
n,i:longint;
begin
read(n);
for i:=n downto 1 do
  if trunc(sqrt(i))=sqrt(i) then begin writeln(i);break;end;
end.

Score : 200 points

Problem Statement

Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer.

Constraints

1 \leq N \leq 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the largest square number not exceeding N.

Sample Input 1

10

Sample Output 1

9

10 is not square, but 9 = 3 × 3 is. Thus, we print 9.

Sample Input 2

81

Sample Output 2

81

Sample Input 3

271828182

Sample Output 3

271821169


var
        a,i:longint;
begin
        readln(a);
        for i:=a downto 1 do
            if trunc(sqrt(i))=sqrt(i) then
            begin
                writeln(i);
                halt;
            end;
end.