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

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

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

Score : 400 points

Problem Statement

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

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

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

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

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4


var i : longint;
    n,ans:int64;

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

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

Score : 500 points

Problem Statement

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

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

Find all favorite numbers and print the sum of those.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

8

Sample Output 1

10

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

Sample Input 2

1000000000000

Sample Output 2

2499686339916

Watch out for overflow.

Submitted source-code file size, in bytes.

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

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


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

Score : 200 points

Problem Statement

There are N gems. The value of the i-th gem is V_i.

You will choose some of these gems, possibly all or none, and get them.

However, you need to pay a cost of C_i to get the i-th gem.

Let X be the sum of the values of the gems obtained, and Y be the sum of the costs paid.

Find the maximum possible value of X-Y.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq C_i, V_i \leq 50

Input

Input is given from Standard Input in the following format:

N
V_1 V_2 ... V_N
C_1 C_2 ... C_N

Output

Print the maximum possible value of X-Y.

Sample Input 1

3
10 2 5
6 3 4

Sample Output 1

5

If we choose the first and third gems, X = 10 + 5 = 15 and Y = 6 + 4 = 10.
We have X-Y = 5 here, which is the maximum possible value.

Sample Input 2

4
13 21 6 19
11 30 6 15

Sample Output 2

6

Sample Input 3

1
1
50

Sample Output 3

0


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

Score : 400 points

Problem Statement

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

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

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

3
-10 5 -4

Sample Output 1

19

If we perform the operation as follows:

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

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

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

Sample Input 2

5
10 -4 -8 -11 3

Sample Output 2

30

Sample Input 3

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

Sample Output 3

10000000000

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


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


var s:array[1..1000000] of char;
    i,n,b,c,min:integer;

begin
  readln(n);
  b := 0;
  c := 0;
  min := 0;
  for i:=1 to n do begin
    read(s[i]);
    if s[i]='.' then begin
      b := b + 1;
      c := c - 1;
      if min > c then min := c
      end
        else c := c + 1;
  end;
  writeln(min+b)
end.

Score : 300 points

Problem Statement

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

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

Constraints

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

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

Input

Input is given from Standard Input in the following format:

N
S

Output

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

Sample Input 1

3
#.#

Sample Output 1

1

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

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0


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

end.


type itemp = ^item;
     item = record
        cont : integer;
        next : itemp;
     end;
var s,temp:char;
    i,j,n,t,tn,ans,tans:integer;
    sn,tsn:itemp;

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

begin
  readln(n);
  sn:=nil;
  t:=0;
  tn:=1;
  temp:='.';
  for i:=1 to n do begin
    read(s);
    if s=temp then t := t + 1 else begin
      additem(sn,t);
      t := 1;
      tn := tn + 1;
      temp := s
    end;
  end;
  additem(sn,t);
  if temp = '*' then additem(sn,t);
  tn := tn div 2 + 1;
  ans:=n;
  for i:=0 to tn do begin
    j := 0;
    tsn := sn;
    tans := 0;
    while tsn<>nil do begin
      j := j + 1;
      if (j <= i*2) and (j mod 2 = 0) then tans := tans + tsn^.cont;
      if (j > i*2) and (j mod 2 = 1) then tans := tans + tsn^.cont;
      tsn:=tsn^.next
    end;
    if tans<ans then ans := tans;
  end;
  writeln(ans);
end.


type itemp = ^item;
     item = record
        cont : integer;
        next : itemp;
     end;
var s,temp:char;
    i,j,n,t,tn,ans,tans:integer;
    sn,tsn:itemp;

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

begin
  readln(n);
  sn:=nil;
  t:=0;
  tn:=1;
  temp:='.';
  for i:=1 to n do begin
    read(s);
    if s=temp then t := t + 1 else begin
      additem(sn,t);
      t := 1;
      tn := tn + 1;
      temp := s
    end;
  end;
  additem(sn,t);
  if temp = '*' then additem(sn,t);
  tn := tn div 2;
  ans:=n;
  for i:=0 to tn do begin
    j := 0;
    tsn := sn;
    tans := 0;
    while tsn<>nil do begin
      j := j + 1;
      if (j <= i*2) and (j mod 2 = 0) then tans := tans + tsn^.cont;
      if (j > i*2) and (j mod 2 = 1) then tans := tans + tsn^.cont;
      tsn:=tsn^.next
    end;
    if tans<ans then ans := tans;
  end;
  writeln(ans);
  {while sn<>nil do begin
    write(sn^.cont);
    sn:=sn^.next
  end;}
end.


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


var n,q,l,r,i,k  :integer;
    c,d:char;
    s:array[1..100000]of char;
    t:array[1..100000]of integer;
begin
  readln(n,q); read(s[1]);
  k:=0;t[1]:=0;
  for i:=2 to n do begin
    read(s[i]);
    if (s[i-1]='A')and(s[i]='C') then k+=1;
    t[i]:=k;
  end;
  for i:=1 to q do begin
    readln(l,r);
    writeln(t[r]-t[l])
  end
end.

Score : 300 points

Problem Statement

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

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

Notes

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

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

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

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

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

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

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

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

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

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


var
	n,i,j,k,t:Longint;
	s:int64;
	dp:array[0..100,0..63]of int64;
begin
	read(n);
dp[0,63]:=1;
for i:=1 to n do begin
for j:=0 to 63 do begin
for k:=0 to 3 do begin
t:=j*4+k;
if(t mod 64<>9)and(t mod 64<>33)and(t mod 64<>6)and(t<>25)and(t<>41)and(t<>57)and(t<>37)and(t<>45)then
dp[i,t mod 64]:=(dp[i,t mod 64]+dp[i-1,j])mod 1000000007;
end;end;end;
for i:=0 to 63 do s:=(s+dp[n,i])mod 1000000007;
writeln(s);
end.

Score : 400 points

Problem Statement

You are given an integer N. Find the number of strings of length N that satisfy the following conditions, modulo 10^9+7:

The string does not contain characters other than A, C, G and T.

The string does not contain AGC as a substring.

The condition above cannot be violated by swapping two adjacent characters once.

Notes

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

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

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of strings of length N that satisfy the following conditions, modulo 10^9+7.

Sample Input 1

3

Sample Output 1

61

There are 4^3 = 64 strings of length 3 that do not contain characters other than A, C, G and T. Among them, only AGC, ACG and GAC violate the condition, so the answer is 64 - 3 = 61.

Sample Input 2

4

Sample Output 2

230

Sample Input 3

100

Sample Output 3

388130742

Be sure to print the number of strings modulo 10^9+7.


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

Score : 700 points

Problem Statement

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

The graph is simple and connected.

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

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

Constraints

All values in input are integers.

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

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

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

Sample Input 1

3

Sample Output 1

2
1 3
2 3

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


var
N,i:Longint;
S:String;
cnt:Array[1..26]of int64;
ans:int64=1;
begin
readln(N);
readln(S);
for i:=1 to N do inc(cnt[ord(S[i])-96]);
for i:=1 to 26 do ans:=ans*(cnt[i]+1)mod 1000000007;
writeln(ans-1);
end.

Score : 200 points

Problem Statement

You are given a string S of length N.
Among its subsequences, count the ones such that all characters are different, modulo 10^9+7. Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings.

Here, a subsequence of a string is a concatenation of one or more characters from the string without changing the order.

Constraints

1 \leq N \leq 100000

S consists of lowercase English letters.

|S|=N

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of the subsequences such that all characters are different, modulo 10^9+7.

Sample Input 1

4
abcd

Sample Output 1

15

Since all characters in S itself are different, all its subsequences satisfy the condition.

Sample Input 2

3
baa

Sample Output 2

5

The answer is five: b, two occurrences of a, two occurrences of ba. Note that we do not count baa, since it contains two as.

Sample Input 3

5
abcab

Sample Output 3

17


var
	a,b,i:Longint;
	s:String;
begin
	readln(s);
	for i:=1 to length(s)do begin
		if s[i]='0'then inc(a)else inc(b);
	end;
	if a<b then writeln(2*a)else writeln(2*b);
end.

Score : 300 points

Problem Statement

There are N cubes stacked vertically on a desk.

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

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

At most how many cubes can be removed?

Constraints

1 \leq N \leq 10^5

|S| = N

Each character in S is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of cubes that can be removed.

Sample Input 1

0011

Sample Output 1

4

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

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

Remove the first and second cubes from the bottom.

Sample Input 2

11011010001011

Sample Output 2

12

Sample Input 3

0

Sample Output 3

0


type UnionFind=record n:Longint;pr:Array of Longint;end;
procedure init(var uf:UnionFind;N:Longint);
var i:Longint;
begin
	uf.n:=N;
	setlength(uf.pr,N);
	for i:=1 to N do uf.pr[i]:=-1;
end;
function find(var uf:UnionFind;a:Longint):Longint;
begin
	if uf.pr[a]<0 then find:=a else begin
		uf.pr[a]:=find(uf,uf.pr[a]);
		find:=uf.pr[a];
	end;
end;
function same(var uf:UnionFind;a,b:Longint):Boolean;
begin
	same:=find(uf,a)=find(uf,b);
end;
function unite(var uf:UnionFind;a,b:Longint):Boolean;
begin
	a:=find(uf,a);
	b:=find(uf,b);
	if a=b then unite:=false else begin
		if uf.pr[a]>uf.pr[b] then begin
			inc(uf.pr[b],uf.pr[a]);
			uf.pr[a]:=b;
		end else begin
			inc(uf.pr[a],uf.pr[b]);
			uf.pr[b]:=a;
		end;
		unite:=true;
	end;
end;
function size(var uf:UnionFind;a:Longint):Longint;
begin
	size:=-uf.pr[find(uf,a)];
end;
var
	N,M,i:Longint;
	uf:UnionFind;
	now,x,y:int64;
	ans:Array[1..100000]of int64;
	u,v:Array[1..100000]of Longint;
begin
	read(N,M);
	for i:=1 to M do begin
		read(u[i],v[i]);
	end;
	init(uf,N);
	x:=N;
	y:=N-1;
	now:=x*y div 2;
	for i:=M downto 1 do begin
		ans[i]:=now;
		if not same(uf,u[i],v[i])then begin
			x:=size(uf,u[i]);
			y:=size(uf,v[i]);
			dec(now,x*y);
		end;
		unite(uf,u[i],v[i]);
	end;
	for i:=1 to M do writeln(ans[i]);
end.

Score : 400 points

Problem Statement

There are N islands and M bridges.

The i-th bridge connects the A_i-th and B_i-th islands bidirectionally.

Initially, we can travel between any two islands using some of these bridges.

However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge.

Let the inconvenience be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining.

For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i < B_i \leq N

All pairs (A_i, B_i) are distinct.

The inconvenience is initially 0.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

In the order i = 1, 2, ..., M, print the inconvenience just after the i-th bridge collapses.
Note that the answer may not fit into a 32-bit integer type.

Sample Input 1

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

Sample Output 1

0
0
4
5
6

For example, when the first to third bridges have collapsed, the inconvenience is 4 since we can no longer travel between the pairs (1, 2), (1, 3), (2, 4) and (3, 4).

Sample Input 2

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

Sample Output 2

8
9
12
14
15

Sample Input 3

2 1
1 2

Sample Output 3

1


var
	a,b,c:longint;
    ch:char;
begin
	read(a,ch,b,ch,c);
    a:=a*10000+b*100+c;
    if a<=20190430 then writeln('Heisei')
    else writeln('TBD');
end.

Score : 100 points

Problem Statement

You are given a string S as input. This represents a valid date in the year 2019 in the yyyy/mm/dd format. (For example, April 30, 2019 is represented as 2019/04/30.)

Write a program that prints Heisei if the date represented by S is not later than April 30, 2019, and prints TBD otherwise.

Constraints

S is a string that represents a valid date in the year 2019 in the yyyy/mm/dd format.

Input

Input is given from Standard Input in the following format:

S

Output

Print Heisei if the date represented by S is not later than April 30, 2019, and print TBD otherwise.

Sample Input 1

2019/04/30

Sample Output 1

Heisei

Sample Input 2

2019/11/01

Sample Output 2

TBD


var
	ch:string;
	n,i:longint;
	ans,s,f:real;
begin
	readln(n);
	ans:=0;
	for i:=1 to n do
	begin
		read(s); readln(ch);
		if pos('JPY',ch)<>0 then f:=1
		else f:=380000;
		ans:=ans+s*f;
	end;
	writeln(ans:0:7);
end.

Score : 200 points

Problem Statement

Takahashi received otoshidama (New Year's money gifts) from N of his relatives.

You are given N values x_1, x_2, ..., x_N and N strings u_1, u_2, ..., u_N as input. Each string u_i is either JPY or BTC, and x_i and u_i represent the content of the otoshidama from the i-th relative.

For example, if x_1 = 10000 and u_1 = JPY, the otoshidama from the first relative is 10000 Japanese yen; if x_2 = 0.10000000 and u_2 = BTC, the otoshidama from the second relative is 0.1 bitcoins.

If we convert the bitcoins into yen at the rate of 380000.0 JPY per 1.0 BTC, how much are the gifts worth in total?

Constraints

2 \leq N \leq 10

u_i = JPY or BTC.

If u_i = JPY, x_i is an integer such that 1 \leq x_i \leq 10^8.

If u_i = BTC, x_i is a decimal with 8 decimal digits, such that 0.00000001 \leq x_i \leq 100.00000000.

Input

Input is given from Standard Input in the following format:

N
x_1 u_1
x_2 u_2
:
x_N u_N

Output

If the gifts are worth Y yen in total, print the value Y (not necessarily an integer).

Output will be judged correct when the absolute or relative error from the judge's output is at most 10^{-5}.

Sample Input 1

2
10000 JPY
0.10000000 BTC

Sample Output 1

48000.0

The otoshidama from the first relative is 10000 yen. The otoshidama from the second relative is 0.1 bitcoins, which is worth 38000.0 yen if converted at the rate of 380000.0 JPY per 1.0 BTC. The sum of these is 48000.0 yen.

Outputs such as 48000 and 48000.1 will also be judged correct.

Sample Input 2

3
100000000 JPY
100.00000000 BTC
0.00000001 BTC

Sample Output 2

138000000.0038

In this case, outputs such as 138001000 and 1.38e8 will also be judged correct.


Score : 300 points

Problem Statement

You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.

Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:

Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1.

Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.

Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)

At least how much MP is needed to achieve the objective?

Constraints

3 \leq N \leq 8

1 \leq C < B < A \leq 1000

1 \leq l_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C
l_1
l_2
:
l_N

Output

Print the minimum amount of MP needed to achieve the objective.

Sample Input 1

5 100 90 80
98
40
30
21
80

Sample Output 1

23

We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.

Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2)

Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10)

Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1)

Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)

Sample Input 2

8 100 90 80
100
100
90
90
90
80
80
80

Sample Output 2

0

If we already have all bamboos of the desired lengths, the amount of MP needed is 0. As seen here, we do not necessarily need to use all the bamboos.

Sample Input 3

8 1000 800 100
300
333
400
444
500
555
600
666

Sample Output 3

243


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

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

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

Score : 400 points

Problem Statement

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

Answer the following Q queries:

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

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

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

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

1 \leq x_i \leq 10^{10}

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

350
1400
301
399

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

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

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

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

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

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

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


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


var n,a,b,i,ans  :int32;

function gcd(x,y:integer):integer;
begin
  if x mod y = 0 then gcd:=y
  else gcd:=gcd(y,x mod y)
end;

begin
  readln(n);
  read(a);
  for i:=2 to n do begin
    if a=1 then break;
    read(b);
    a:=gcd(a,b)
  end;
  write(a)
end.

Score : 300 points

Problem Statement

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

Initially, the health of Monster i is A_i.

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

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

A random alive monster attacks another random alive monster.

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

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

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

4
2 10 8 40

Sample Output 1

2

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

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000


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

Score : 400 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

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

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

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

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

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

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

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

Sample Input 3

15 3
5 4 6

Sample Output 3

654


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


const NMAX=100000;
var n,m,i,j:integer;
    x:array[1..NMAX] of integer;
    y:array[1..NMAX] of integer;

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

begin
  read(n);
  readln(m);
  for i:=1 to m do read(x[i]);
  sortx(1,m);
  for i:=1 to m-1 do y[i]:=x[i+1] - x[i];
  sorty(1,m-1);
  j:=0;
  for i:=1 to m-n do j:=j+y[i];
  write(j)
end.

Score : 300 points

Problem Statement

We will play a one-player game using a number line and N pieces.

First, we place each of these pieces at some integer coordinate.

Here, multiple pieces can be placed at the same coordinate.

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

Note that the coordinates where we initially place the pieces are already regarded as visited.

Find the minimum number of moves required to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

-10^5 \leq X_i \leq 10^5

X_1, X_2, ..., X_M are all different.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Find the minimum number of moves required to achieve the objective.

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

Initially, put the two pieces at coordinates 1 and 10.

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

3 7
-10 -3 0 9 -100 2 17

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0


const NMAX=100000;
var n,m,i,j:integer;
    x:array[1..NMAX] of integer;
    y:array[1..NMAX] of integer;

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

begin
  read(n);
  readln(m);
  for i:=1 to m do read(x[i]);
  sortx(1,m);
  for i:=1 to m-1 do y[i]:=x[i+1] - x[i];
  sorty(1,m-1);
  j:=0;
  for i:=1 to n do j:=j+y[i];
  write(j)
end.


const NMAX=100000;
var n,m,i,j:integer;
    x:array[1..NMAX] of integer;
    y:array[1..NMAX] of integer;

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

begin
  read(n);
  readln(m);
  for i:=1 to m do read(x[i]);
  sortx(1,m);
  for i:=1 to m-1 do y[i]:=x[i+1] - x[i];
  sorty(1,m-1);
  j:=0;
  for i:=n+1 to m-1 do j:=j+y[i];
  write(j)
end.


var a,b,c:string;n,i,cnt:integer;
begin
readln(n);
readln(a);
readln(b);
readln(c);
cnt:=0;
for i:=1 to n do begin
if ((a[i]=b[i]) and (b[i]=c[i])) then cnt:=cnt
else begin
if ((a[i]<>b[i]) and (b[i]<>c[i]) and (c[i]<>a[i])) then cnt:=cnt+2 else cnt:=cnt+1;
end;
end;
writeln(cnt);
end.

Score : 200 points

Problem Statement

You are given three strings A, B and C. Each of these is a string of length N consisting of lowercase English letters.

Our objective is to make all these three strings equal. For that, you can repeatedly perform the following operation:

Operation: Choose one of the strings A, B and C, and specify an integer i between 1 and N (inclusive). Change the i-th character from the beginning of the chosen string to some other lowercase English letter.

What is the minimum number of operations required to achieve the objective?

Constraints

1 \leq N \leq 100

Each of the strings A, B and C is a string of length N.

Each character in each of the strings A, B and C is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
A
B
C

Output

Print the minimum number of operations required.

Sample Input 1

4
west
east
wait

Sample Output 1

3

In this sample, initially A = west、B = east、C = wait. We can achieve the objective in the minimum number of operations by performing three operations as follows:

Change the second character in A to a. A is now wast.

Change the first character in B to w. B is now wast.

Change the third character in C to s. C is now wast.

Sample Input 2

9
different
different
different

Sample Output 2

0

If A, B and C are already equal in the beginning, the number of operations required is 0.

Sample Input 3

7
zenkoku
touitsu
program

Sample Output 3

13


var
	f:array[-1100..1100000] of boolean;
	s,i:longint;
begin
	read(s);
	i:=1;
	fillchar(f,sizeof(f),false);
	while f[s]=false do
	begin
		f[s]:=true;
		inc(i);
		if s mod 2=0 then s:=s div 2
		else s:=s*3+1;
	end;
	writeln(i);
end.

Score : 200 points

Problem Statement

A sequence a=\{a_1,a_2,a_3,......\} is determined as follows:

The first term s is given as input.

Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.

a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.

Find the minimum integer m that satisfies the following condition:

There exists an integer n such that a_m = a_n (m > n).

Constraints

1 \leq s \leq 100

All values in input are integers.

It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum integer m that satisfies the condition.

Sample Input 1

8

Sample Output 1

5

a=\{8,4,2,1,4,2,1,4,2,1,......\}. As a_5=a_2, the answer is 5.

Sample Input 2

7

Sample Output 2

18

a=\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\}.

Sample Input 3

54

Sample Output 3

114


Score : 100 points

Problem Statement

It has been decided that a programming contest sponsored by company A will be held, so we will post the notice on a bulletin board.

The bulletin board is in the form of a grid with N rows and N columns, and the notice will occupy a rectangular region with H rows and W columns.

How many ways are there to choose where to put the notice so that it completely covers exactly HW squares?

Constraints

1 \leq H, W \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
H
W

Output

Print the answer.

Sample Input 1

3
2
3

Sample Output 1

2

There are two ways to put the notice, as follows:

### ...
### ###
... ###

Here, # represents a square covered by the notice, and . represents a square not covered.

Sample Input 2

100
1
1

Sample Output 2

10000

Sample Input 3

5
4
2

Sample Output 3

8


var
n,a,b,i,f,g,h,p,cnt:integer;
begin
readln(n);
read(a,b);
for i:=1 to n do begin
read(p);
if(p<=a)then f:=f+1;
if(a<p)and(p<=b)then g:=g+1;
if(b<p)then h:=h+1;
end;
cnt:=n;
if(f<cnt)then cnt:=f;
if(g<cnt)then cnt:=g;
if(h<cnt)then cnt:=h;
writeln(cnt);
end.

Score : 200 points

Problem Statement

You have written N problems to hold programming contests.
The i-th problem will have a score of P_i points if used in a contest.

With these problems, you would like to hold as many contests as possible under the following condition:

A contest has three problems. The first problem has a score not greater than A points, the second has a score between A + 1 and B points (inclusive), and the third has a score not less than B + 1 points.

The same problem should not be used in multiple contests.
At most how many contests can be held?

Constraints

3 \leq N \leq 100

1 \leq P_i \leq 20 (1 \leq i \leq N)

1 \leq A < B < 20

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A B
P_1 P_2 ... P_N

Output

Print the answer.

Sample Input 1

7
5 15
1 10 16 2 7 20 12

Sample Output 1

2

Two contests can be held by putting the first, second, third problems and the fourth, fifth, sixth problems together.

Sample Input 2

8
3 8
5 5 5 10 10 10 15 20

Sample Output 2

0

No contest can be held, because there is no problem with a score of A = 3 or less.

Sample Input 3

3
5 6
5 6 10

Sample Output 3

1


var
  a,b:array[0..100002]of longint;
  i,j,k,n,m,p,q,ans,anq:longint;
function max(a,b:longint):longint;
begin
  if abs(a)<=b then exit(a)
  else exit(b);
end;
procedure zx(s:longint);
var
  i,j,p,q,t,u,o:longint;
  f:array[0..100002]of boolean;
begin
  for i:=1 to n do
  if a[i]>s then break;
  p:=i-1;
  q:=i;
  t:=n;
  j:=n div 2;
  while j>0 do
  begin
    dec(j);
    f[t]:=true;
    dec(t);
    if p=0 then begin ans:=a[q]+ans; inc(q); end
    else if f[q] then begin ans:=a[p]+ans; dec(p,2); end
    else if s-a[p]>=q-s then
    begin
      ans:=a[p]+ans;
      dec(p);
    end else
    begin
      ans:=a[q]+ans;
      inc(q);
    end;
  end;
end;
begin
  readln(n,m);
  for i:=1 to n do begin read(a[i]); anq:=a[i]+anq; end;
  for i:=1 to m do
  begin
    readln(b[i]);
    ans:=0;
    zx(b[i]);
    writeln(anq-ans);
  end;
end.

Score : 500 points

Problem Statement

There are N cards. The i-th card has an integer A_i written on it.
For any two cards, the integers on those cards are different.

Using these cards, Takahashi and Aoki will play the following game:

Aoki chooses an integer x.

Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:

Takahashi should take the card with the largest integer among the remaining card.

Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.

The game ends when there is no card remaining.

You are given Q candidates for the value of x: X_1, X_2, ..., X_Q.
For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.

Constraints

2 \leq N \leq 100 000

1 \leq Q \leq 100 000

1 \leq A_1 < A_2 < ... < A_N \leq 10^9

1 \leq X_i \leq 10^9 (1 \leq i \leq Q)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.

Sample Input 1

5 5
3 5 7 11 13
1
4
9
10
13

Sample Output 1

31
31
27
23
23

For example, when x = X_3(= 9), the game proceeds as follows:

Takahashi takes the card with 13.

Aoki takes the card with 7.

Takahashi takes the card with 11.

Aoki takes the card with 5.

Takahashi takes the card with 3.

Thus, 13 + 11 + 3 = 27 should be printed on the third line.

Sample Input 2

4 3
10 20 30 40
2
34
34

Sample Output 2

70
60
60


Program VACATION;
uses math;
var F:array[1..1000005,1..3] of longint;
    n,i,j :longint;
    maxx:int64;
    fi:text;
//-------------------------------
begin
//assign(fi,'VACATION.inp');reset(fi);
readln(n);
for i:=1 to n do begin
                 for j:=1 to 3 do begin
                                  Read(F[i,j]);
                                  end;
                 end;
for i:=n-1 downto 1 do begin
                       F[i,1]:=F[i,1]+max(F[i+1,2],F[i+1,3]);
                       F[i,2]:=F[i,2]+max(F[i+1,1],F[i+1,3]);
                       F[i,3]:=F[i,3]+max(F[i+1,2],F[i+1,1]);
                       end;
for i:=1 to 3 do begin
                 maxx:=max(maxx,F[1,i]);
                 end;
Writeln(maxx);
//readln;
end.

Score : 100 points

Problem Statement

Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.

The vacation consists of N days.
For each i (1 \leq i \leq N), Taro will choose one of the following activities and do it on the i-th day:

A: Swim in the sea. Gain a_i points of happiness.

B: Catch bugs in the mountains. Gain b_i points of happiness.

C: Do homework at home. Gain c_i points of happiness.

As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.

Find the maximum possible total points of happiness that Taro gains.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq a_i, b_i, c_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the maximum possible total points of happiness that Taro gains.

Sample Input 1

3
10 40 70
20 50 80
30 60 90

Sample Output 1

210

If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.

Sample Input 2

1
100 10 1

Sample Output 2

100

Sample Input 3

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

Sample Output 3

46

Taro should do activities in the order C, A, B, A, C, B, A.


uses crt;
var a,b,c:array[1..100000] of longint;
    i,n:longint;
function max(a,b:longint):longint;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    clrscr;
    readln(n);
    for i := 1 to n do readln(a[i],b[i],c[i]);
    for i:=2 to n do
      begin
          a[i]:=max(a[i-1],max(b[i-1],c[i-1])+a[i]);
          b[i]:=max(b[i-1],max(a[i-1],c[i-1])+b[i]);
          c[i]:=max(c[i-1],max(a[i-1],b[i-1])+c[i]);
      end;
      writeln(max(a[n],max(c[n],b[n])));
      readln;
end.


 var f1:text; A,B,C: array [0..1000000] of longint;
     a1,b1,c1,i,n:longint;

 function max(x,y:longint):longint;
 BEGIN
  if x<y then exit(y) else exit(x);
 END;

 BEGIN
  assign(f1,''); reset(f1);
  readln(f1,n);
  readln(f1,A[1],B[1],C[1]);
  for i:=2 to n do
   BEGIN
    readln(f1,a1,b1,c1);
    A[i]:=max(B[i-1],C[i-1])+a1;
    B[i]:=max(A[i-1],C[i-1])+b1;
    C[i]:=max(A[i-1],B[i-1])+c1;
   END;
  write(max(A[n],max(B[n],C[n])));
  readln;
  close(f1);
 END.


type
	node=record
				sa,sb,sc:longint;
			end;
var
	a,b,c:array[0..100010] of longint;
	f1,f2:node;
	i,n,ans:longint;
function max(x,y:longint):longint;
begin
	if x>y then exit(x) else exit(y);
end;
begin
	readln(n);
	for i:=1 to n do read(a[i],b[i],c[i]);
	f1.sa:=a[1];
	f1.sb:=b[1];
	f1.sc:=c[1];
	for i:=2 to n do
	begin
		f2.sa:=a[i]+max(f1.sb,f1.sc);
		f2.sb:=b[i]+max(f1.sa,f1.sc);
		f2.sc:=c[i]+max(f1.sa,f1.sb);
		f1:=f2;
	end;
	ans:=max(f1.sa,f1.sb);
	ans:=max(ans,f1.sc);
	writeln(ans);
end.


var N, W, V, i, j: longint;
    ww, vv: array[1..1000] of longint;
    dp, weight: array[-10..100, -11000..100000] of longint;
begin
    readln(N, W);
    for i:=1 to N do
    begin
        readln(ww[i], vv[i]);
        V:=V+vv[i];
    end;
    for i:=1 to N do
    begin
        for j:=1 to V do
        begin
            if (weight[i-1,j-vv[i]]+ww[i]>W) then
            begin
                if dp[i-1,j]>dp[i,j-1] then
                begin
                    dp[i,j]:=dp[i-1,j];
                    weight[i,j]:=weight[i-1,j];
                end
                else
                begin
                    dp[i,j]:=dp[i,j-1];
                    weight[i,j]:=weight[i,j-1];
                end;
            end
            else
            begin
                if (dp[i-1,j-vv[i]]+vv[i]<=j) then
                begin
                if dp[i-1,j]>dp[i-1,j-vv[i]]+vv[i] then
                begin
                    dp[i,j]:=dp[i-1,j];
                    weight[i,j]:=weight[i-1,j];
                end
                else
                begin
                    dp[i,j]:=dp[i-1,j-vv[i]]+vv[i];
                    weight[i,j]:=weight[i-1,j-vv[i]]+ww[i];
                end;
                end
                else
                begin
                if dp[i-1,j]>dp[i,j-1] then
                begin
                    dp[i,j]:=dp[i-1,j];
                    weight[i,j]:=weight[i-1,j];
                end
                else
                begin
                    dp[i,j]:=dp[i,j-1];
                    weight[i,j]:=weight[i,j-1];
                end;
                end;
            end;
//            write(dp[i,j], ' ');
        end;
//        writeln;
    end;
    writeln(dp[N,V]);
end.

Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^9

1 \leq w_i \leq W

1 \leq v_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

1 1000000000
1000000000 10

Sample Output 2

10

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.


uses crt;
var
  w: array [0..100] of int64;
        v: array [0..100] of longint;
        c: array [0..100000] of int64;
        m,n,i,j,sum,k: longint;

begin
        clrscr;
        readln ( n, m);
        for i:=1 to n do
        begin
                readln ( w[i], v[i]);
                sum:= sum + v[i];
        end;

        c[0]:= 0;
        for i:=1 to sum do c[i]:= 100000000000;

        for i:=1 to n do
        begin
                j:= sum;
                while j >= v[i]  do
                begin
                        if c[j- v[i]] + w[i] <= c[j] then
                        c[j]:= c[j- v[i]] + w[i];
                        dec (j);
                end;
        end;

        for i:= sum downto 1 do
        if c[i] <= m then
        begin
                write ( i);
                break;
        end;
        readln;
end.


program knapsack1;

uses math;

const
  fi='knapsack1.inp';
  fo='knapsack1.out';
  oo=trunc(1e9)+5;

var
  n,m,va: longint;
  v,w: array[0..100000] of int32;
  f:array[0..100,0..100000] of int32;

procedure nhap;
var i: longint;
begin

  va:=0;
  read(n,m);
  for i:=1 to n do begin
    read(w[i],v[i]);
    inc(va,v[i]);
  end;

end;

procedure xuli;
var i,j,ans: longint;
begin

  ans:=0;
  for i:=0 to n do begin
    for j:=0 to va do f[i][j]:=oo;
  end;

  f[0][0]:=0;

  for i:=1 to n do begin
    for j:=0 to va do begin
      if (j>=v[i]) and (f[i-1][j-v[i]]+w[i]<=m) then f[i][j]:=min(f[i-1][j],f[i-1][j-v[i]]+w[i])
      else f[i][j]:=f[i-1][j];
    end;
  end;

  for j:=0 to va do begin
    if f[n][j]<=m then ans:=j;
  end;
//
//  for i:=0 to n do begin
//    for j:=0 to va do begin
//      if f[i][j]=oo then write('oo':3)
//      else write(f[i][j]:3);
//    end;
//    writeln
//  end;

  write(ans);

end;

begin

  //assign(input,fi);  reset(input);
  //assign(output,fo); rewrite(output);

  nhap;
  xuli;

  //close(input);      close(output);

end.


program knapsack1;

uses math;

const
  fi='knapsack1.inp';
  fo='knapsack1.out';
  oo=trunc(1e9)+5;

var
  n,m,va: longint;
  v,w: array[0..100000] of int16;
  f:array[0..100,0..100000] of int32;

procedure nhap;
var i: longint;
begin

  va:=0;
  read(n,m);
  for i:=1 to n do begin
    read(w[i],v[i]);
    inc(va,v[i]);
  end;

end;

procedure xuli;
var i,j,ans: longint;
begin

  ans:=0;
  for i:=0 to n do begin
    for j:=0 to va do f[i][j]:=oo;
  end;

  f[0][0]:=0;

  for i:=1 to n do begin
    for j:=0 to va do begin
      if (j>=v[i]) and (f[i-1][j-v[i]]+w[i]<=m) then f[i][j]:=min(f[i-1][j],f[i-1][j-v[i]]+w[i])
      else f[i][j]:=f[i-1][j];
    end;
  end;

  for j:=0 to va do begin
    if f[n][j]<=m then ans:=j;
  end;
//
//  for i:=0 to n do begin
//    for j:=0 to va do begin
//      if f[i][j]=oo then write('oo':3)
//      else write(f[i][j]:3);
//    end;
//    writeln
//  end;

  write(ans);

end;

begin

  //assign(input,fi);  reset(input);
  //assign(output,fo); rewrite(output);

  nhap;
  xuli;

  //close(input);      close(output);

end.


program knapsack1;

uses math;

const
  fi='knapsack1.inp';
  fo='knapsack1.out';
  oo=trunc(1e9);

var
  n,m,va: longint;
  v,w: array[0..100000] of int16;
  f:array[0..100,0..100000] of int32;

procedure nhap;
var i: longint;
begin

  read(n,m);
  for i:=1 to n do begin
    read(w[i],v[i]);
    inc(va,v[i]);
  end;

end;

procedure xuli;
var i,j,ans: longint;
begin

  ans:=0;
  for i:=0 to n do begin
    for j:=0 to va do f[i][j]:=oo;
  end;

  f[0][0]:=0;

  for i:=1 to n do begin
    for j:=0 to va do begin
      if j>=v[i] then f[i][j]:=min(f[i-1][j],f[i-1][j-v[i]]+w[i])
      else f[i][j]:=f[i-1][j];
    end;
  end;

  for j:=0 to va do begin
    ans:=ifthen((f[n][j]<=m),j,ans);
  end;

  write(ans);

end;

begin

  //assign(input,fi);  reset(input);
  //assign(output,fo); rewrite(output);

  nhap;
  xuli;

  //close(input);      close(output);

end.


program knapsack1;

uses math;

const
  fi='knapsack1.inp';
  fo='knapsack1.out';

var
  n,m: longint;
  v,w: array[0..100000] of int64;
  f:array[0..100,0..100000] of int64;

procedure nhap;
var i: longint;
begin

  read(n,m);
  for i:=1 to n do read(w[i],v[i]);

end;

procedure xuli;
var i,j: longint;
begin

  fillchar(f,sizeof(f),0);
  for i:=1 to n do begin
    for j:=1 to m do begin
      if j>=w[i] then f[i][j]:=max(f[i-1][j],f[i-1][j-w[i]]+v[i])
      else f[i][j]:=f[i-1][j];
    end;
  end;

  write(f[n][m]);

end;

begin

  //assign(input,fi);  reset(input);
  //assign(output,fo); rewrite(output);

  nhap;
  xuli;

  //close(input);      close(output);

end.


var
	f:array[0..100010] of int64;
	a,b,n,m,i,j:longint;
begin
	readln(n,m);
	fillchar(f,sizeof(f),$7f);
	f[0]:=0;
	for i:=1 to n do
	begin
		read(a,b);
		for j:=100000 downto b do
			if f[j-b]+a<f[j] then f[j]:=f[j-b]+a;
	end;
	for i:=100000 downto 0 do
		if f[i]<=m then
		begin
			writeln(i);
			break;
		end;
end.


Program LongestPath;
uses math;
type Rec=record
     x:longint;
     y:longint;
     end;
var Head,Adj,F :array[0..100005] of longint;
    E:array[1..100005] of Rec;
    Avai:array[1..100005] of boolean;
    n,m,res,i,maxx:longint;
    fi,fo:text;
//------------------------------------------
procedure DFS(i:longint);
var j:longint;
begin
Avai[i]:=false;
for j:=Head[i]+1 to Head[i+1] do begin
                                 if not Avai[adj[j]] then F[i]:=max(F[i],F[adj[j]]+1)
                                 else DFS(adj[j]);
                                 F[i]:=max(F[i],F[adj[j]]+1);
                                 end;
end;
//------------------------------------------
begin
//assign(fi,'LongestPath.inp');reset(fi);
readln(n,m);
for i:=1 to m do begin
                 With E[i] do begin
                              readln(x,y);
                              inc(Head[x]);
                              end;
                 end;
for i:=1 to n do begin
                 Head[i]:=Head[i]+Head[i-1];
                 end;
for i:=m downto 1 do begin
                     With E[i] do begin
                                  Adj[Head[x]]:=y;
                                  dec(Head[x]);
                                  end;
                     end;
Head[n+1]:=m;
fillchar(Avai,sizeof(Avai),true);
for i:=1 to n do begin
                 if Avai[i] then
                 DFS(i);
                 end;
for i:=1 to n do begin
                 maxx:=max(maxx,F[i]);
                 end;
Writeln(maxx);
//readln;
end.

Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

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

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

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

Sample Output 3

3

The red directed path in the following figure is one of the longest:


var H, W, i, j: longint;
    a: array[1..1000] of ansistring;
    dp: array[0..1000, 0..1000] of longint;
begin
    readln(H, W);
    for i:=1 to H do
            readln(a[i]);
    for i:=1 to H do
    begin
        for j:=1 to W do
        begin
            if a[i][j]='#' then
                dp[i,j]:=0
            else
                dp[i,j]:=(dp[i,j-1]+dp[i-1,j]) mod 1000000007;
            dp[1,1]:=1;
        end;
    end;
    writeln(dp[H, W]);
end.

Score : 100 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns.
Let (i, j) denote the square at the i-th row from the top and the j-th column from the left.

For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}.
If a_{i, j} is ., Square (i, j) is an empty square; if a_{i, j} is #, Square (i, j) is a wall square.
It is guaranteed that Squares (1, 1) and (H, W) are empty squares.

Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.

Find the number of Taro's paths from Square (1, 1) to (H, W).
As the answer can be extremely large, find the count modulo 10^9 + 7.

Constraints

H and W are integers.

2 \leq H, W \leq 1000

a_{i, j} is . or #.

Squares (1, 1) and (H, W) are empty squares.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}\ldotsa_{1, W}
:
a_{H, 1}\ldotsa_{H, W}

Output

Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.

Sample Input 1

3 4
...#
.#..
....

Sample Output 1

3

There are three paths as follows:

Sample Input 2

5 2
..
#.
..
.#
..

Sample Output 2

0

There may be no paths.

Sample Input 3

5 5
..#..
.....
#...#
.....
..#..

Sample Output 3

24

Sample Input 4

20 20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................

Sample Output 4

345263555

Be sure to print the count modulo 10^9 + 7.


const
  base = 1000000007;
var
  a,f:array[0..1010,0..1010] of int64;
  n,m,j,i:longint;
  s : ansistring;
BEGIN
  ///assign(input,'a.in'); reset(input);
  readln(n,m);
  for i:=1 to n do begin
    readln(s);
    for j:=1 to m do if s[j]='.' then a[i,j]:=1 else a[i,j]:=0;
  end;

  fillchar(f,sizeof(f),0);
  f[1,1] := 1;
  for i:=1 to n do
    for j:=1 to m do
      if a[i,j]=1 then f[i,j] := (f[i,j] + f[i-1,j] + f[i,j-1]) mod base;
  writeln(f[n,m]);
END.


uses math;
const fi='kstones.inp';
      fo='kstones.out';
      maxn=100;
var n,k,mmin:int32;
    a:array[1..maxn] of int32;
    f:array[0..100000] of boolean;
//=============================
procedure nhap;
var i:int32;
begin
   readln(n,k);
   mmin:=maxlongint;
   for i:=1 to n do
   begin read(a[i]); mmin:=min(mmin,a[i]); end;
end;
//=============================
procedure xuli;
var i,j:int32;
    ok:boolean;
begin
   fillchar(f,sizeof(f),false);
   for i:=mmin to k do
   begin
    ok:=false;
    for j:=1 to n do
    if not f[i-a[j]] then
     begin
        ok:=true; break;
     end;
    f[i]:=ok;
   end;
   if f[k] then write('First') else write('Second');
end;
//=============================
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.

Score : 100 points

Problem Statement

There is a set A = \{ a_1, a_2, \ldots, a_N \} consisting of N positive integers.
Taro and Jiro will play the following game against each other.

Initially, we have a pile consisting of K stones.
The two players perform the following operation alternately, starting from Taro:

Choose an element x in A, and remove exactly x stones from the pile.

A player loses when he becomes unable to play.
Assuming that both players play optimally, determine the winner.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 10^5

1 \leq a_1 < a_2 < \cdots < a_N \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

If Taro will win, print First; if Jiro will win, print Second.

Sample Input 1

2 4
2 3

Sample Output 1

First

If Taro removes three stones, Jiro cannot make a move.
Thus, Taro wins.

Sample Input 2

2 5
2 3

Sample Output 2

Second

Whatever Taro does in his operation, Jiro wins, as follows:

If Taro removes two stones, Jiro can remove three stones to make Taro unable to make a move.

If Taro removes three stones, Jiro can remove two stones to make Taro unable to make a move.

Sample Input 3

2 7
2 3

Sample Output 3

First

Taro should remove two stones. Then, whatever Jiro does in his operation, Taro wins, as follows:

If Jiro removes two stones, Taro can remove three stones to make Jiro unable to make a move.

If Jiro removes three stones, Taro can remove two stones to make Jiro unable to make a move.

Sample Input 4

3 20
1 2 3

Sample Output 4

Second

Sample Input 5

3 21
1 2 3

Sample Output 5

First

Sample Input 6

1 100000
1

Sample Output 6

Second


program LDeque;

uses math;

const
  fi='LDeque.inp';
  fo='LDeque.out';

var
  n: longint;
  a:array[0..3000] of longint;

procedure nhap;
var i: longint;
begin

  readln(n);
  for i:=1 to n do read(a[i]);

end;

procedure xuli;
var i,j,ans: longint; k:boolean;
begin

  i:=1; j:=n; k:=true;
  ans:=0;
  repeat
    if i>j then begin
      ans:=ifthen(k,ans+a[i],ans-a[j]);
      inc(i);
    end else begin
      ans:=ifthen(k,ans+a[j],ans-a[j]);
      dec(j);
    end;
    k:=not(k);
  until i>j;

  writeln(ans);

end;

begin

  //assign(input,fi);     reseT(input);
  //assign(output,fo);    rewrite(output);

  nhap;
  xuli;

  //close(input);         close(output);

end.

Score : 100 points

Problem Statement

Taro and Jiro will play the following game against each other.

Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N).
Until a becomes empty, the two players perform the following operation alternately, starting from Taro:

Remove the element at the beginning or the end of a. The player earns x points, where x is the removed element.

Let X and Y be Taro's and Jiro's total score at the end of the game, respectively.
Taro tries to maximize X - Y, while Jiro tries to minimize X - Y.

Assuming that the two players play optimally, find the resulting value of X - Y.

Constraints

All values in input are integers.

1 \leq N \leq 3000

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the resulting value of X - Y, assuming that the two players play optimally.

Sample Input 1

4
10 80 90 30

Sample Output 1

10

The game proceeds as follows when the two players play optimally (the element being removed is written bold):

Taro: (10, 80, 90, 30) → (10, 80, 90)

Jiro: (10, 80, 90) → (10, 80)

Taro: (10, 80) → (10)

Jiro: (10) → ()

Here, X = 30 + 80 = 110 and Y = 90 + 10 = 100.

Sample Input 2

3
10 100 10

Sample Output 2

-80

The game proceeds, for example, as follows when the two players play optimally:

Taro: (10, 100, 10) → (100, 10)

Jiro: (100, 10) → (10)

Taro: (10) → ()

Here, X = 10 + 10 = 20 and Y = 100.

Sample Input 3

1
10

Sample Output 3

10

Sample Input 4

10
1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1

Sample Output 4

4999999995

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

Sample Input 5

6
4 2 9 7 1 5

Sample Output 5

2

The game proceeds, for example, as follows when the two players play optimally:

Taro: (4, 2, 9, 7, 1, 5) → (4, 2, 9, 7, 1)

Jiro: (4, 2, 9, 7, 1) → (2, 9, 7, 1)

Taro: (2, 9, 7, 1) → (2, 9, 7)

Jiro: (2, 9, 7) → (2, 9)

Taro: (2, 9) → (2)

Jiro: (2) → ()

Here, X = 5 + 1 + 9 = 15 and Y = 4 + 7 + 2 = 13.


uses math;
const fi='digitnum.inp';
      fo='digitnum.out';
      maxn=10000;
      modu=trunc(1e9)+7;
var f:array[1..maxn,0..99,0..2] of uint64;
    s:ansistring;
    d,len:int16;
procedure nhap;
begin
   readln(s);
   read(d);
end;
procedure init;
var i,j,x:int32;
begin
   x:=ord(s[1])-48;
   for i:=1 to 9 do
    begin
       if i<x then j:=0
       else if i>x then j:=2
       else j:=1;
       inc(f[1,i mod d,j]);
    end;
end;
function calc(n:ansistring):uint64;
var i,j,r,st,x,newst,newr:int32;
    ans:uint64;
begin
   len:=length(n);
   for i:=1 to len-1 do
    for r:=0 to d-1 do
     for st:=0 to 2 do
      begin
         x:=ord(n[i+1])-48;
         for j:=0 to 9 do
          begin
             newst:=st;
             if newst=1 then
              begin
                 if j<x then newst:=0
                 else if j>x then newst:=2
                 else newst:=1;
              end;
             newr:=(r+j) mod d;
             f[i+1,newr,newst]:=(f[i+1,newr,newst]+f[i,r,st]) mod modu;
          end;
      end;
   ans:=0;
   for i:=1 to len-1 do
    for j:=0 to 2 do inc(ans,f[i,0,j]);
   exit((ans+f[len,0,0]+f[len,0,1]) mod modu);
end;

procedure xuli;
begin
   init;
   write(calc(s));
end;
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.

Score : 100 points

Problem Statement

Find the number of integers between 1 and K (inclusive) satisfying the following condition, modulo 10^9 + 7:

The sum of the digits in base ten is a multiple of D.

Constraints

All values in input are integers.

1 \leq K < 10^{10000}

1 \leq D \leq 100

Input

Input is given from Standard Input in the following format:

K
D

Output

Print the number of integers satisfying the condition, modulo 10^9 + 7.

Sample Input 1

30
4

Sample Output 1

6

Those six integers are: 4, 8, 13, 17, 22 and 26.

Sample Input 2

1000000009
1

Sample Output 2

2

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

Sample Input 3

98765432109876543210
58

Sample Output 3

635270834


uses math;
const fi='digitnum.inp';
      fo='digitnum.out';
      maxn=1000;
      modu=trunc(1e9)+7;
var f:array[1..maxn,0..100,0..2] of uint64;
    s:ansistring;
    d,len:int16;
procedure nhap;
begin
   readln(s);
   read(d);
end;
procedure init;
var i,j,x:int32;
begin
   x:=ord(s[1])-48;
   for i:=1 to 9 do
    begin
       if i<x then j:=0
       else if i>x then j:=2
       else j:=1;
       inc(f[1,i mod d,j]);
    end;
end;
function calc(n:ansistring):uint64;
var i,j,r,st,x,newst,newr:int32;
    ans:uint64;
begin
   len:=length(n);
   for i:=1 to len-1 do
    for r:=0 to d-1 do
     for st:=0 to 2 do
      begin
         x:=ord(n[i+1])-48;
         for j:=0 to 9 do
          begin
             newst:=st;
             if newst=1 then
              begin
                 if j<x then newst:=0
                 else if j>x then newst:=2
                 else newst:=1;
              end;
             newr:=(r+j) mod d;
             f[i+1,newr,newst]:=(f[i+1,newr,newst]+f[i,r,st]) mod modu;
          end;
      end;
   ans:=0;
   for i:=1 to len-1 do
    for j:=0 to 2 do inc(ans,f[i,0,j]);
   exit((ans+f[len,0,0]+f[len,0,1]) mod modu);
end;

procedure xuli;
begin
   init;
   write(calc(s));
end;
begin
//   assign(input,fi); reset(input);
//   assign(output,fo); rewrite(output);
   nhap;
   xuli;
//   close(input); close(output);
end.


var
	a,b,c,ans:int64;
begin
	read(a,b,c);
	if a+b+1>=c then writeln(b+c)else writeln(a+b+1+b);
end.

Score : 200 points

Problem Statement

Takahashi has A untasty cookies containing antidotes, B tasty cookies containing antidotes and C tasty cookies containing poison.

Eating a cookie containing poison results in a stomachache, and eating a cookie containing poison while having a stomachache results in a death.
As he wants to live, he cannot eat one in such a situation.
Eating a cookie containing antidotes while having a stomachache cures it, and there is no other way to cure stomachaches.

Find the maximum number of tasty cookies that Takahashi can eat.

Constraints

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

A,B and C are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the maximum number of tasty cookies that Takahashi can eat.

Sample Input 1

3 1 4

Sample Output 1

5

We can eat all tasty cookies, in the following order:

A tasty cookie containing poison

An untasty cookie containing antidotes

A tasty cookie containing poison

A tasty cookie containing antidotes

A tasty cookie containing poison

An untasty cookie containing antidotes

A tasty cookie containing poison

Sample Input 2

5 2 9

Sample Output 2

10

Sample Input 3

8 8 1

Sample Output 3

9


var
 st:string;
 i,s:longint;
begin
 readln(st);
 for i:=1 to length(st) do
  if st[i]='2' then inc(s);
 writeln(s);
end.

Score : 100 points

Problem Statement

You are given an integer N that has exactly four digits in base ten.
How many times does 2 occur in the base-ten representation of N?

Constraints

1000 \leq N \leq 9999

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

1222

Sample Output 1

3

2 occurs three times in 1222. By the way, this contest is held on December 22 (JST).

Sample Input 2

3456

Sample Output 2

0

Sample Input 3

9592

Sample Output 3

1


var
    n,p,s:int64;
    f:array[0..1000010] of longint;
    i,j,t:longint;
begin
    read(n,p);
    if n=1 then
    begin
        writeln(p);
        halt;
    end;
    if p=1 then
    begin
        writeln(1);
        halt;
    end;
    fillchar(f,sizeof(f),0);
    i:=1;
    t:=trunc(sqrt(p))+1;
    while (p<>1) and (i<=t) do
    begin
        inc(i);
        while p mod i=0 do
        begin
            p:=p div i;
            inc(f[i]);
        end;
    end;
    s:=1;
    for i:=2 to t do
        if f[i]>=n then
            for j:=1 to f[i] div n do s:=s*i;
    writeln(s);
end.

Score : 300 points

Problem Statement

There are N integers a_1, a_2, ..., a_N not less than 1.
The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P.

Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N.

Constraints

1 \leq N \leq 10^{12}

1 \leq P \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N P

Output

Print the answer.

Sample Input 1

3 24

Sample Output 1

2

The greatest common divisor would be 2 when, for example, a_1=2, a_2=6 and a_3=2.

Sample Input 2

5 1

Sample Output 2

1

As a_i are positive integers, the only possible case is a_1 = a_2 = a_3 = a_4 = a_5 = 1.

Sample Input 3

1 111

Sample Output 3

111

Sample Input 4

4 972439611840

Sample Output 4

206


var
	n,p,s:int64;
    f:array[0..1000010] of longint;
    i,t:longint;
begin
	read(n,p);
    if n=1 then
    begin
    	writeln(p);
        halt;
    end;
    fillchar(f,sizeof(f),0);
    i:=1;
    t:=trunc(sqrt(p))+1;
    while (p<>1) and (i<=t) do
    begin
    	inc(i);
        while p mod i=0 do
        begin
        	p:=p div i;
            inc(f[i]);
        end;
    end;
    s:=1;
    for i:=2 to t do
    	if f[i]>=n then s:=s*i;
    writeln(s);
end.


var
	n,p,s:int64;
    f:array[0..1000010] of longint;
    i,t:longint;
begin
	read(n,p);
    if n=1 then
    begin
    	writeln(p);
        halt;
    end;
    fillchar(f,sizeof(f),0);
    i:=1;
    while p<>1 do
    begin
    	inc(i);
        while p mod i=0 do
        begin
        	p:=p div i;
            inc(f[i]);
        end;
    end;
    t:=trunc(sqrt(p))+1;
    s:=1;
    for i:=2 to t do
    	if f[i]>=n then s:=s*i;
    writeln(s);
end.


var
  n,k,i,m,j,t,ans,c,d:longint;
  b:boolean;
begin
  readln(n);
  for i:=1 to n do
  begin
    readln(k);
    ans:=(ans+k mod 2)mod 2;
    if k<>t then b:=true;
    t:=k;
  end;
  if (ans=1)or(b=false) then writeln('first')
  else writeln('second');
end.

Score : 500 points

Problem Statement

There is an apple tree that bears apples of N colors. The N colors of these apples are numbered 1 to N, and there are a_i apples of Color i.

You and Lunlun the dachshund alternately perform the following operation (starting from you):

Choose one or more apples from the tree and eat them. Here, the apples chosen at the same time must all have different colors.

The one who eats the last apple from the tree will be declared winner. If both you and Lunlun play optimally, which will win?

Constraints

1 \leq N \leq 10^5

1 \leq a_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

If you will win, print first; if Lunlun will win, print second.

Sample Input 1

2
1
2

Sample Output 1

first

Let Color 1 be red, and Color 2 be blue. In this case, the tree bears one red apple and two blue apples.

You should eat the red apple in your first turn. Lunlun is then forced to eat one of the blue apples, and you can win by eating the other in your next turn.

Note that you are also allowed to eat two apples in your first turn, one red and one blue (not a winning move, though).

Sample Input 2

3
100000
30000
20000

Sample Output 2

second


var
  n,k,i,m,j,t,ans,c,d:longint;
  b:boolean;
begin
  readln(n);
  for i:=1 to n do
  begin
    readln(k);
    ans:=(ans+k mod 2)mod 2;
    if k<>1 then b:=true;
  end;
  if (ans=1)or(b=false) then writeln('first')
  else writeln('second');
end.


var
	i,ans,n,p,c:int64;
	j:Longint;
begin
	read(n,p);
	if n=1 then begin
		writeln(p);
		exit;
	end;
	i:=2;
	ans:=1;
	while i*i<=p do begin
		c:=0;
		while p mod i=0 do begin
			p:=p div i;
			inc(c);
		end;
		for j:=1 to c div n do ans:=ans*i;
		inc(i);
	end;
	writeln(ans);
end.


var
	i,ans,n,p,c:int64;
	j:Longint;
begin
	read(n,p);
	if n=1 then begin
		writeln(p);
		exit;
	end;
	i:=2;
	ans:=1;
	while i*i<=p do begin
		c:=0;
		while p mod i=0 do begin
			p:=p div i;
			inc(c);
		end;
		for j:=1 to c div n do ans:=ans*i;
	end;
	writeln(ans);
end.


var i:Longint;B,E:int64;S:String;
begin
readln(S);
for i:=1 to length(S)do if S[i]='B'then inc(B)else inc(E,B);
writeln(E);
end.

Score : 300 points

Problem Statement

There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.)
The state of each piece is represented by a string S of length N.
If S_i=B, the i-th piece from the left is showing black;
If S_i=W, the i-th piece from the left is showing white.

Consider performing the following operation:

Choose i (1 \leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.

Find the maximum possible number of times this operation can be performed.

Constraints

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

S_i=B or W

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum possible number of times the operation can be performed.

Sample Input 1

BBW

Sample Output 1

2

The operation can be performed twice, as follows:

Flip the second and third pieces from the left.

Flip the first and second pieces from the left.

Sample Input 2

BWBWBW

Sample Output 2

6


var
  a:array[1..200000]of longint;
  f:array[1..200000]of boolean;
  n,s,i,j,ans:longint;
function sp(a,b:longint):boolean;
var c:longint;
begin
  c:=a+b;
  while c mod 2 = 0 do c:=c div 2;
  if c = 1 then sp:=true else sp := false
end;
begin
  readln(n);
  for i:=1 to n do begin
    read(a[i]);
    f[i]:=true
  end;
  for i:=2 to n do
  for j:=n downto i  do
    if a[j]>a[j-1] then begin
      s:=a[j];
      a[j]:=a[j-1];
      a[j-1]:=s
    end;
  for i:=1 to n-1 do
  for j:=i+1 to n do
  if sp(a[i],a[j])and f[i]and f[j] then begin
    ans:=ans+1;
    f[i]:=false;
    f[j]:=false
    end;
  write(ans)
end.

Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i.
He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2.
Note that a ball cannot belong to multiple pairs.
Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Constraints

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

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2.

Sample Input 1

3
1 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls.
Note that we cannot pair the second ball with itself.

Sample Input 2

5
3 11 14 5 13

Sample Output 2

2


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

Score : 200 points

Problem Statement

In some other world, today is the day before Christmas Eve.

Mr. Takaha is buying N items at a department store. The regular price of the i-th item (1 \leq i \leq N) is p_i yen (the currency of Japan).

He has a discount coupon, and can buy one item with the highest price for half the regular price. The remaining N-1 items cost their regular prices. What is the total amount he will pay?

Constraints

2 \leq N \leq 10

100 \leq p_i \leq 10000

p_i is an even number.

Input

Input is given from Standard Input in the following format:

N
p_1
p_2
:
p_N

Output

Print the total amount Mr. Takaha will pay.

Sample Input 1

3
4980
7980
6980

Sample Output 1

15950

The 7980-yen item gets the discount and the total is 4980 + 7980 / 2 + 6980 = 15950 yen.

Note that outputs such as 15950.0 will be judged as Wrong Answer.

Sample Input 2

4
4320
4320
4320
4320

Sample Output 2

15120

Only one of the four items gets the discount and the total is 4320 / 2 + 4320 + 4320 + 4320 = 15120 yen.


var
  h:array[1..100000]of longint;
  n,k,i,j,s,ans,maxIn:longint;
begin
  readln(n,k);
  for i:=1 to n do
    readln(h[i]);
  for i:=1 to n-1 do begin
    maxIn:=i;
    for j:=i+1 to n do
      if h[j]>h[maxIn] then maxIn:=j;
    s:=h[i];
    h[i]:=h[maxIn];
    h[maxIn]:=s
  end;
  ans:=h[1]-h[k];
  for i:=2 to n-k+1 do
    if h[i]-h[i+k-1]<ans then
      ans:=h[i]-h[i+k-1];
  write(ans)
end.

Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).


var
  h:array[1..100000]of longint;
  n,k,i,j,s,ans,maxIn:longint;
begin
  readln(n,k);
  for i:=1 to n do
    readln(h[i]);
  for i:=1 to n-1 do begin
    maxIn:=i;
    for j:=i+1 to n do
      if h[j]>h[maxIn] then maxIn:=j;
    s:=h[i];
    h[i]:=h[maxIn];
    h[maxIn]:=h[i]
  end;
  ans:=h[1]-h[k];
  for i:=2 to n-k+1 do
    if h[i]-h[i+k-1]<ans then
      ans:=h[i]-h[i+k-1];
  write(ans)
end.


var
 n,i,j,t,k,min:longint;
 a:array[0..100005] of longint;
procedure qsort(l,r:longint);
var
 i,j,t,m: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 i<r then qsort(i,r);
 if l<j then qsort(l,j);
end;
begin
 readln(n,k);
 for i:=1 to n do readln(a[i]);
 qsort(1,n);
 min:=maxlongint;
 for i:=1 to n-k+1 do if a[i+k-1]-a[i]<min then min:=a[i+k-1]-a[i];
 writeln(min);
end.


var
 n,i,j,t,k,min:longint;
 a:array[0..100005] of longint;
procedure qsort(l,r:longint);
var
 i,j,t,m: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 i<r then qsort(i,r);
 if l<j then qsort(l,j);
end;
begin
 readln(n,k);
 for i:=1 to n do readln(a[i]);
 qsort(1,n);
 for i:=1 to n do writeln(a[i]);
 min:=maxlongint;
 for i:=1 to n-k+1 do if a[i+k-1]-a[i]<min then min:=a[i+k-1]-a[i];
 writeln(min);
end.


var i,j,m,n,k : integer;
    t,u         : longint;
    h         : array[1..100000] of longint;

begin
  readln(n,k);
  for i := 0 to n - 1 do readln(h[i]);
  for i := 0 to n - 2 do begin
    m := i;
    for j := i + 1 to n - 1 do if h[m] > h[j] then m := j;
    t := h[i];
    h[i] := h[m];
    h[m] := t
  end;
  t := 999999999;
  for i := 0 to n - k do begin
    u := h[i + k - 1] - h[i];
    if t > u then t := u
  end;
  writeln(t)
end.


var
  n,k,i,ans:longint;
  a:array[0..100000]of longint;
Procedure qs(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 qs(l,j);
   if i<r then qs(i,r);
end;
begin
  readln(n,k);
  for i:=1 to n do
    readln(a[i]);
  qs(1,n);
  ans:=a[n]-a[1];
  for i:=1 to n-k+1 do
    if a[i+k-1]-a[i]<ans then ans:=a[i+k-1]-a[i];
  write(ans);
end.


var
  a:array[1..100000] of longint;
  i,n,j,m,k:longint;
  ans:qword;
procedure qsort(l,r:longint);
var
  i,j,mid,p:longint;
begin
  i:=l;j:=r;
  mid:=a[(l+r)shr 1];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
      begin
        p:=a[i];a[i]:=a[j];a[j]:=p;
        inc(i);
        dec(j);
      end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  readln(n,k);
  for i:=1 to n do readln(a[i]);
  qsort(1,n);
  ans:=maxlongint;
  for i:=1 to n-k+1 do if ans>(a[i+k-1]-a[i]) then ans:=a[i+k-1]-a[i];
  writeln(ans);
end.


var
  a:array[1..10000] of longint;
  i,n,j,m,k:longint;
  ans:int64;
procedure qsort(l,r:longint);
var
  i,j,mid,p: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
        p:=a[i];a[i]:=a[j];a[j]:=p;
        inc(i);
        dec(j);
      end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  readln(n,k);
  for i:=1 to n do readln(a[i]);
  qsort(1,n);
  ans:=maxlongint;
  for i:=1 to n-k+1 do if ans>(a[i+k-1]-a[i]) then ans:=a[i+k-1]-a[i];
  writeln(ans);
end.


var
  a:array[1..10000] of longint;
  i,n,j,m,k:longint;
  ans:qword;
procedure qsort(l,r:longint);
var
  i,j,mid,p: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
        p:=a[i];a[i]:=a[j];a[j]:=p;
        inc(i);
        dec(j);
      end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  readln(n,k);
  for i:=1 to n do readln(a[i]);
  qsort(1,n);
  ans:=maxlongint;
  for i:=1 to n-k+1 do if ans>(a[i+k-1]-a[i]) then ans:=a[i+k-1]-a[i];
  writeln(ans);
end.


var
	n,i,k,min:longint;
	a:array[0..100010] of 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
	readln(n,k);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	min:=2000000000;
	for i:=k to n do
		if a[i]-a[i-k+1]<min then min:=a[i]-a[i-k+1];
	writeln(min);
end.


var
  a,b,c,i,j:longint;
  s,s2:string;
begin
  readln(s);
  j:=maxlongint;
  if length(s)<3 then begin val(s,b,c); writeln(abs(b-753)); halt; end;
  for i:=1 to length(s)-2 do
  begin
    s2:=copy(s,i,3);
    val(s2,b,c);
    if j>abs(b-753) then j:=abs(b-753);
  end;
  writeln(j);
end.

Score : 200 points

Problem Statement

There is a string S consisting of digits 1, 2, ..., 9.
Lunlun, the Dachshund, will take out three consecutive digits from S, treat them as a single integer X and bring it to her master. (She cannot rearrange the digits.)

The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753?

Constraints

S is a string of length between 4 and 10 (inclusive).

Each character in S is 1, 2, ..., or 9.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum possible difference between X and 753.

Sample Input 1

1234567876

Sample Output 1

34

Taking out the seventh to ninth characters results in X = 787, and the difference between this and 753 is 787 - 753 = 34. The difference cannot be made smaller, no matter where X is taken from.

Note that the digits cannot be rearranged. For example, taking out 567 and rearranging it to 765 is not allowed.

We cannot take out three digits that are not consecutive from S, either. For example, taking out the seventh digit 7, the ninth digit 7 and the tenth digit 6 to obtain 776 is not allowed.

Sample Input 2

35753

Sample Output 2

0

If 753 itself can be taken out, the answer is 0.

Sample Input 3

1111111111

Sample Output 3

642

No matter where X is taken from, X = 111, with the difference 753 - 111 = 642.


var
	n:Longint;
function f(x:int64;a,b,c:Longint):Longint;
begin
	f:=0;
	if x<=n then f:=a*b*c+f(x*10+3,1,b,c)+f(x*10+5,a,1,c)+f(x*10+7,a,b,1);
end;
begin
	read(n);
	if n>777777700 then begin writeln(26484);exit;end;
	writeln(f(0,0,0,0));
end.

Score : 300 points

Problem Statement

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there?

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.

Constraints

1 \leq N < 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

Sample Input 1

575

Sample Output 1

4

There are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573.

Sample Input 2

3600

Sample Output 2

13

There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735, 753, 3357, 3375, 3537, 3557, 3573, 3575 and 3577.

Sample Input 3

999999999

Sample Output 3

26484


var
	n:Longint;
function f(x:int64;a,b,c:Longint):Longint;
begin
	f:=0;
	if x<=n then f:=a*b*c+f(x*10+3,1,b,c)+f(x*10+5,a,1,c)+f(x*10+7,a,b,1);
end;
begin
	read(n);
	if n>777777752 then begin writeln(26484);exit;end;
	writeln(f(0,0,0,0));
end.


var n : longint;
    i,sum : integer;
    e : boolean;

procedure sft(p : longint; d : integer; s,f,t : boolean);
begin
  if d = 0 then begin
    if s and f and t then if p < n then sum := sum + 1 else e := true
  end else begin
    sft(p * 10 + 3, d - 1, s, f, true);
    sft(p * 10 + 5, d - 1, s, true, t);
    sft(p * 10 + 7, d - 1, true, f, t);
  end
end;

begin
  readln(n);
  e := false;
  for i := 3 to 9 do begin
    sft(0, i, false, false, false);
    if e then break
  end;
  writeln(sum)
end.


var
	n,ans:Longint;
procedure f(x:int64;a,b,c:Longint);
begin
	if x>n then exit;
	inc(ans,a*b*c);
	f(x*10+3,1,b,c);
	f(x*10+5,a,1,c);
	f(x*10+7,a,b,1);
end;
begin
	read(n);
	f(0,0,0,0);
	writeln(ans);
end.


var
	n:Longint;
function f(x:int64;a,b,c:Longint):Longint;
begin
	f:=0;
	if x<=n then f:=a*b*c+f(x*10+3,1,b,c)+f(x*10+5,a,1,c)+f(x*10+7,a,b,1);
end;
begin
	read(n);
	writeln(f(0,0,0,0));
end.


var
  a,b,c,i,j,ans,m:longint;
  n:int64;
  s,s2:string;
  b1,b2,b3:boolean;
begin
  readln(n);
  for i:=1 to n do
  begin
    b1:=false;
    b2:=false;
    b3:=false;
    s:='';
	str(i,s);
    s:=s+'4';
    for j:=1 to length(s) do
    begin
      if (s[j]<>'3')and(s[j]<>'5')and(s[j]<>'7') then break;
      if s[j]='3' then b1:=true;
      if s[j]='5' then b2:=true;
      if s[j]='7' then b3:=true;
    end;
    if b1 and b2 and b3 and(j=length(s)) then inc(ans);
  end;
  writeln(ans);
end.


const
	t:array[1..3] of char=('3','5','7');
var
	n:string;
	l,ans,b,j:longint;
procedure make(x:longint; s:string);
var
	i,a:longint;
begin
	if x=j+1 then
	begin
		if pos('3',s)=0 then exit;
		if pos('5',s)=0 then exit;
		if pos('7',s)=0 then exit;
		val(s,a);
		if a>b then exit;
		inc(ans);
		exit;
	end;
	for i:=1 to 3 do make(x+1,s+t[i]);
end;
begin
	read(b);
	str(b,n);
	l:=length(n);
	ans:=0;
	for j:=3 to l do make(1,'');
	writeln(ans);
end.


var
    i,j,k,ans,n:longint;
    s:array[0..110] of longint;
    f:array[0..110,0..110,0..110] of boolean;
procedure prime(x:longint);
begin
    j:=2;
    while x>=j do
    begin
        while x mod j=0 do
        begin
            inc(s[j]);
            x:=x div j;
        end;
        inc(j);
    end;
end;
begin
    read(n);
    fillchar(s,sizeof(s),0);
    for i:=2 to n do prime(i);
    fillchar(f,sizeof(f),true);
    ans:=0;
    for i:=2 to n do
        for j:=2 to n do
            if i<>j then
            begin
                if (s[i]>=2) and (s[j]>=24) then inc(ans);
                if (s[i]>=4) and (s[j]>=14) then inc(ans);
                if (s[i]>=2) and (s[j]>=4) then
                    for k:=2 to n do
                        if (i<>k) and (j<>k) then
                            if s[k]>=4 then
                                if f[i,j,k] then
                                begin
                                    inc(ans);
                                    f[i,j,k]:=false;
                                    f[i,k,j]:=false;
                                end;
            end;
    for i:=1 to n do
        if s[i]>=74 then inc(ans);
    writeln(ans);
end.

Score : 400 points

Problem Statement

You are given an integer N. Among the divisors of N! (= 1 \times 2 \times ... \times N), how many Shichi-Go numbers (literally "Seven-Five numbers") are there?

Here, a Shichi-Go number is a positive integer that has exactly 75 divisors.

Note

When a positive integer A divides a positive integer B, A is said to a divisor of B.
For example, 6 has four divisors: 1, 2, 3 and 6.

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go numbers that are divisors of N!.

Sample Input 1

9

Sample Output 1

0

There are no Shichi-Go numbers among the divisors of 9! = 1 \times 2 \times ... \times 9 = 362880.

Sample Input 2

10

Sample Output 2

1

There is one Shichi-Go number among the divisors of 10! = 3628800: 32400.

Sample Input 3

100

Sample Output 3

543


var
	n,i,ans,p,j,a,cnt,k:Longint;
	dp:array[0..100,1..75]of Longint;
function isp(a:Longint):Boolean;
var i:Longint;
begin
	i:=2;
	while i*i<=a do begin
		if a mod i=0 then begin
			isp:=False;
			exit;
		end;
		inc(i);
	end;
	isp:=True;
end;
begin
	read(n);
	dp[0,1]:=1;
	p:=0;
	for i:=2 to n do begin
		if not isp(i)then continue;
		cnt:=0;
		for j:=1 to n do begin
			a:=j;
			while a mod i=0 do begin
				a:=a div i;
				inc(cnt);
			end;
		end;
		for j:=0 to cnt do begin
			if not((j=0)or(j=2)or(j=4)or(j=14)or(j=24)or(j=74))then continue;
			for k:=1 to 75 div (j+1)do inc(dp[i,k*(j+1)],dp[p,k]);
		end;
		p:=i;
	end;
	writeln(dp[p,75]);
end.


var a:array[1..100] of integer;n,i,s,pnt,d:integer;
begin
read(n);
s:=0;
for i:=1 to n do begin read(a[i]);s:=s+a[i];end;
pnt:=n;
d:=s;
for i:=1 to n do begin
if(((a[i]*n-s)*(a[i]*n-s))<(d*d))then begin d:=(a[i]*n-s);pnt:=i;end;
end;
writeln(pnt-1);
end.

Score : 200 points

Problem Statement

Niwango-kun is an employee of Dwango Co., Ltd.

One day, he is asked to generate a thumbnail from a video a user submitted.

To generate a thumbnail, he needs to select a frame of the video according to the following procedure:

Get an integer N and N integers a_0, a_1, ..., a_{N-1} as inputs. N denotes the number of the frames of the video, and each a_i denotes the representation of the i-th frame of the video.

Select t-th frame whose representation a_t is nearest to the average of all frame representations.

If there are multiple such frames, select the frame with the smallest index.

Find the index t of the frame he should select to generate a thumbnail.

Constraints

1 \leq N \leq 100

1 \leq a_i \leq 100

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N
a_{0} a_{1} ... a_{N-1}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

1

Since the average of frame representations is 2, Niwango-kun needs to select the index 1, whose representation is 2, that is, the nearest value to the average.

Sample Input 2

4
2 5 2 5

Sample Output 2

0

The average of frame representations is 3.5.

In this case, every frame has the same distance from its representation to the average.

Therefore, Niwango-kun should select index 0, the smallest index among them.