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

  writeln(min,' ',max+a[1]-1);
end.

Score : 500 points

Problem Statement

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

Form groups consisting of A_i children each!

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

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

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

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

Constraints

1 \leq K \leq 10^5

2 \leq A_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

K
A_1 A_2 ... A_K

Output

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

Sample Input 1

4
3 4 3 2

Sample Output 1

6 8

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

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

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

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

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

The last 2 children are declared the winners.

Sample Input 2

5
3 4 100 3 2

Sample Output 2

-1

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

Sample Input 3

10
2 2 2 2 2 2 2 2 2 2

Sample Output 3

2 3


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

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

Score : 700 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 2000

1 \leq A_i \leq 2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

3
1 2 1

Sample Output 1

2

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

Sample Input 2

1
58

Sample Output 2

58

In this case, S = (58).

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
n,m,i:longint;
s:string;
begin
readln(n,m);
read(s);
for i:=1 to n do
  if not(s[i] in['0'..'9']) then begin writeln('No');halt;end;
if s[n+1]<>'-' then begin writeln('No');halt;end;
for i:=n+2 to n+m+1 do
  if not(s[i] in['0'..'9']) then begin writeln('No');halt;end;
writeln('Yes');
end.

Score : 200 points

Problem Statement

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

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

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

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

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

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

Sample Input 2

1 1
---

Sample Output 2

No

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

Sample Input 3

1 2
7444

Sample Output 3

No


var
  a,b,i:longint;
  s:string;
begin
  readln(a,b);
  readln(s);
  if s[a+1]<>'-' then begin writeln('No');halt; end;
  for i:=1 to a+b do
   if s[i]='-' then begin writeln('No');halt; end;
  writeln('Yes');
end.


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

Score : 300 points

Problem Statement

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

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

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

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

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

Here, it is guaranteed that F_i divides S_i.

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

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

Constraints

1≤N≤500

1≤C_i≤100

1≤S_i≤10^5

1≤F_i≤10

S_i％F_i=0

All input values are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3
6 5 1
1 10 1

Sample Output 1

12
11
0

We will travel from Station 1 as follows:

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

11 seconds: arrive at Station 2.

11 seconds: take the train to Station 3.

12 seconds: arrive at Station 3.

We will travel from Station 2 as follows:

10 seconds: take the train to Station 3.

11 seconds: arrive at Station 3.

Note that we should print 0 for Station 3.

Sample Input 2

4
12 24 6
52 16 4
99 2 2

Sample Output 2

187
167
101
0

Sample Input 3

4
12 13 1
44 17 17
66 4096 64

Sample Output 3

4162
4162
4162
0


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  n,i,j,t:integer;
  c,s,f:array of integer;
begin
  try
    { TODO -oUser -cConsole Main : abc84c}
    readln(n);//1多い
    SetLength(c,n);
    SetLength(s,n);
    SetLength(f,n);

    for i := 0 to n - 2 do begin
      read(c[i]);
      read(s[i]);
      readln(f[i]);
    end;

    for i := 0 to n - 3 do begin
      t:=s[i];
      for j := i to n-2 do begin
        t:=t+c[j];
        if t<s[j+1] then
          t:=s[j+1];
      end;
      Writeln(t);
    end;
    Writeln(s[i]+c[i]);
    Writeln('0');

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


var
    c,s,f,t: array[1..500] of Integer;
    n,i,j:Integer;
begin
    readln(n);
    for i := 1 to n-1 do
    begin
        readln(c[i],s[i],f[i]);
    end;
    t[n-1]:=c[n-1]+s[n-1];
    for i := n-2 downto 1 do
    begin
        t[i]:=c[i]+s[i];
        for j := i+1 to n-1 do
        begin
            if t[i]<=s[j] then
            begin
                t[i]:=s[j]+c[j]//t[i]+s[j]-t[i]+c[j]
            end
            else
            begin
                t[i]:=t[i]+c[j]+(f[j]-(t[i]-s[j]) mod f[j]) mod f[j];
            end;
        end;
    end;
    for i := 1 to n do
    begin
        writeln(t[i]);
    end;
    readln;
end.


var
  n,i,l,r,j:longint;
  a:array[0..100001] of boolean;
  f:array[0..100001] of longint;
begin
  readln(n);
  fillchar(a,sizeof(a),true);
  a[0]:=false;a[1]:=false;
  for i:=2 to trunc(sqrt(100000)) do
    if a[i] then
      for j:=i to 100000 div i do a[i*j]:=false;
  f[0]:=0;
  for i:=1 to 100000 do if (a[i]) and (a[(i+1) div 2]) then f[i]:=f[i-1]+1 else f[i]:=f[i-1];
  for i:=1 to n do
    begin
      readln(l,r);writeln(f[r]-f[l-1]);
    end;
end.

Score : 400 points

Problem Statement

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

You are given Q queries.

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

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

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

Sample Input 1

1
3 7

Sample Output 1

2

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

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

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

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

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

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

Sample Output 3

4
4
1
1
1
2


var
 n,i,j,l,r,ans:longint;
 a:array[0..100005] of boolean;
 b:array[0..100005] of longint;
begin
 readln(n);
 a[1]:=true;
 for i:=2 to trunc(sqrt(100000)) do
  if not a[i] then
   for j:=2 to 100000 div i do
    a[i*j]:=true;
 b[1]:=0;
 b[2]:=0;
 for i:=1 to 100000 do
  if odd(i) and not a[i] and not a[(i+1) div 2] then b[i]:=b[i-1]+1 else b[i]:=b[i-1];
 for i:=1 to n do
  begin
   readln(l,r);
   writeln(b[r]-b[l-1]);
  end;
end.


const
	num = 100000;
var
	q,l,r,i : Longint;
	not_prime:array[1..num] of Boolean;
	count:array[0..num] of Longint;
function init(): Integer;
var i,j:Longint;
begin
	not_prime[1]:=True;
	for i := 2 to num do
	begin
		if not_prime[i]= False then
		begin
			j:=i+i;
			while j<=num do
			begin
				if not_prime[j]=False then
					not_prime[j]:=True;
				j:=j+i;
			end;
		end;
	end;
	init := 0;
end;

function get_count(): Integer;
var i:Longint;
begin
	for i := 1 to num do
	begin
		if  (not_prime[i]=False) and (not_prime[trunc((i+1)/2)]=False) then
		begin
			count[i]:=1;
		end;
		count[i]:=count[i]+count[i-1];
	end;
	get_count := 0;
end;

begin
	init();
	get_count();
	readln(q);
	for i := 1 to q do
	begin
		read(l);
		readln(q);
		writeln(count[q]-count[l-1]);
	end;
	readln;
end.


var primes,sum:array[0..100020] of longint;
var prime:array[0..100020] of boolean;
var n,m,i,j,x,num,y:longint;
begin
  prime[0]:=true;prime[1]:=true;
  for i:=2 to 100000 do
  begin
    if not prime[i] then begin inc(num);primes[num]:=i;end;
    for j:=1 to num do
    begin
      if i*primes[j]>100000 then break;
      prime[i*primes[j]]:=true;
      if i mod primes[j]=0 then break;
    end;
  end;
  fillchar(sum,sizeof(sum),0);
  for i:=1 to 100000 do if (not prime[i]) and (not prime[(i+1) div 2])
  then begin sum[i]:=sum[i-1]+1;//writeln(i);
  end
  else sum[i]:=sum[i-1];
  read(m);
  for i:=1 to m do begin read(x,y);writeln(sum[y]-sum[x-1]);end;
end.


var i,j:longint;
    v:array[0..200000]of boolean;
    a,sum:array[0..200000]of longint;
    max,q,l,r:longint;
begin
  max:=100000;
  for i:=2 to max do
  if not v[i] then
  begin
    for j:=2 to (max div i) do
    v[i*j]:=true;
  end;
  for i:=2 to max do
  if i mod 2=1 then
    if (not v[i])and(not v[(i+1)div 2]) then
     a[i]:=1;
  for i:=1 to max do
  sum[i]:=sum[i-1]+a[i];
  read(q);
  for i:=1 to q do
  begin
    read(l,r);
    writeln(sum[r]-sum[l-1]);
  end;
end.


program Project1;

{$APPTYPE CONSOLE}

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

  if a+b>c+d then
    Writeln('Left')
  else if a+b=c+d then
    Writeln('Balanced')
  else
    Writeln('Right');

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

Score : 100 points

Problem Statement

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

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

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

Constraints

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

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

Sample Input 1

3 8 7 1

Sample Output 1

Left

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

Sample Input 2

3 4 5 2

Sample Output 2

Balanced

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

Sample Input 3

1 7 6 4

Sample Output 3

Right

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


var
 ch:char;
 s:longint;
begin
 while not eoln do
  begin
   read(ch);
   if ch='0' then inc(s);
  end;
  writeln(s);
end.

Score : 500 points

Problem Statement

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

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

Constraints

1\leq |S|\leq 10^5

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

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

010

Sample Output 1

2

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

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

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

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

Sample Input 2

100000000

Sample Output 2

8

Sample Input 3

00001111

Sample Output 3

4


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


uses math ;
var mi,m,i,j,k,sum,now,c,d : longint ;
    cc,yo : array [0..1000001] of int64 ;
    a,b : int64 ;
    so,s1,s2 : ansistring ;
    gg : char ;
begin
  readln(so);
  gg:='2';
  j:=-1;
  for i:=1 to length(so) do
   begin
    if gg<>so[i] then
     begin
      inc(j);
      cc[j]:=now;
      now:=1;
      gg:=so[i];
     end
    else
     inc(now);
   end;
   inc(j);
   cc[j]:=now;
  mi:=1000000000;
  for i:=3 to j do
   begin
    if min(cc[i-2],cc[i])+cc[i-1]<mi then
     mi:=min(cc[i-2],cc[i])+cc[i-1];
   end;
  if j=1 then
   mi:=cc[1]
  else if j=2 then
   mi:=max(cc[1],cc[2]);
  writeln(mi);
end.


var x:char;
    c:longint;
begin
  while not eoln do
  begin
    read(x);
    if x='0' then
      inc(c);
  end;
  writeln(c);
end.


var n,m,i,j,k,sum,now,c,d : longint ;
    cc,yo : array [0..1000001] of int64 ;
    a,b : int64 ;
    so,s1,s2 : ansistring ;
begin
  readln(so);
  for i:=1 to length(so) do
   if so[i]='0' then
    inc(sum);
  writeln(sum);
end.


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


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

Score : 300 points

Problem Statement

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

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

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

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

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

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

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

Sample Input 1

4
3 3 3 3

Sample Output 1

1

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

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

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

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

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

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5


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


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

Score : 500 points

Problem Statement

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

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

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

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

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

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

Input

Input is given from Standard Input in the following format:

s
x y

Output

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

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

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

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

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

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

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

Sample Input 6

TTTT
1 0

Sample Output 6

No


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

Score : 200 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 200

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

3
8 12 40

Sample Output 1

2

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

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

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

Thus, Snuke can perform the operation at most twice.

Sample Input 2

4
5 6 8 10

Sample Output 2

0

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

Sample Input 3

6
382253568 723152896 37802240 379425024 404894720 471526144

Sample Output 3

8


program Project1;

{$APPTYPE CONSOLE}

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

    SetLength(l,n);
    for i := 0 to n - 1 do
      Read(l[i]);

    count:=0;
    while True do begin
      for i := 0 to n - 1 do begin
        if l[i] mod 2 <> 0 then begin
          Writeln(count);
          Readln;
          Readln;
          exit;
        end;
        l[i]:=l[i] div 2;
      end;
      Inc(count);
    end;

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


var
 x,max,min,i,n,k,s:longint;
 sum:int64;
 a,b:array[0..200001] of longint;
procedure kp(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 l<j then kp(l,j);
 if i<r then kp(i,r);
end;
begin
 min:=maxlongint;
 readln(n,k);
 for i:=1 to n do
  begin
   read(x);
   inc(b[x]);
   if x>max then max:=x;
   if x<min then min:=x;
  end;
 for i:=min to max do
  if b[i]<>0 then
   begin
    inc(s);
    a[s]:=b[i];
   end;
 kp(1,s);
 for i:=1 to abs(s-k) do
  sum:=sum+a[i];
 writeln(sum);
end.

Score : 300 points

Problem Statement

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

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

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

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

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

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

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

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

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

Sample Input 3

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

Sample Output 3

3


var
	N,K,ans,i,A:Longint;
	cnt:Array[1..200000]of Longint;
	bck:Array[0..200000]of Longint;
begin
	read(N,K);
	for i:=1 to N do begin
		read(A);
		inc(cnt[A]);
	end;
	for i:=1 to N do inc(bck[cnt[i]]);
	for i:=N downto 1 do begin
		while bck[i]>0 do begin
			dec(bck[i]);
			if K>0 then dec(K) else inc(ans,i);
		end;
	end;
	writeln(ans);
end.


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


program Project1;

{$APPTYPE CONSOLE}

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

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

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

Score : 200 points

Problem Statement

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

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

Constraints

1?N?10^8

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

12

Sample Output 1

Yes

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

Sample Input 2

57

Sample Output 2

No

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

Sample Input 3

148

Sample Output 3

No


var n,x,ans,ma,m2,d,m3,da,nn:int64;
var i,j:longint;
var a,b:array[0..1000] of int64;
function max(a,b:int64):int64;
begin
  if a>b then exit(a);exit(b);
end;
begin
  read(n);
  for i:=1 to n do
    for j:=1 to 10 do begin read(x);if x=1 then inc(a[i]);end;
  ma:=-2147483647;d:=-1;
  for i:=1 to n do
  begin
    m2:=-2147483647;
    for j:=0 to 10 do
    begin
      read(x);if j=0 then b[i]:=x;
      if j<=a[i] then
      begin
        if x>m2 then begin m2:=x;d:=max(d,j);end;
        if j>0 then
        //if (x>ma) or ((x=ma) and (ma-b[da]<x-b[i])) then
        if (ma-b[da]<=x-b[i]) then begin ma:=x;da:=i;end;
      end;
    end;
    if (da=i) and (m2<ma) then begin inc(ans,ma);d:=233;end
    else inc(ans,m2);
  end;  nn:=-2147483647;
  for i:=1 to n do if b[i]<nn then begin nn:=b[i];da:=i;end;
  if d=0 then begin dec(ans,b[da]);inc(ans,ma);end;
  writeln(ans);
end.

Score : 300 points

Problem Statement

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

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

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

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

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

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

Constraints

1≤N≤100

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

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

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

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

Output

Print the maximum possible profit of Joisino's shop.

Sample Input 1

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

Sample Output 1

8

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

Sample Input 2

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

Sample Output 2

-2

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

Sample Input 3

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

Sample Output 3

23


var n,x,ans,ma,m2,d,m3,da,nn:int64;
var i,j:longint;
var a,b:array[0..1000] of int64;
function max(a,b:int64):int64;
begin
  if a>b then exit(a);exit(b);
end;
begin
  read(n);
  for i:=1 to n do
    for j:=1 to 10 do begin read(x);if x=1 then inc(a[i]);end;
  ma:=-2147483647;d:=-1;
  for i:=1 to n do
  begin
    m2:=-2147483647;
    for j:=0 to 10 do
    begin
      read(x);if j=0 then b[i]:=x;
      if j<=a[i] then
      begin
        if x>m2 then begin m2:=x;d:=max(d,j);end;
        if j>0 then
        //if (x>ma) or ((x=ma) and (ma-b[da]<x-b[i])) then
        if (ma-b[da]<x-b[i]) then begin ma:=x;da:=i;end;
      end;
    end;
    inc(ans,m2);
  end;  nn:=-2147483647;
  for i:=1 to n do if b[i]<nn then begin nn:=b[i];da:=i;end;
  if d=0 then begin dec(ans,b[da]);inc(ans,ma);end;
  writeln(ans);
end.


uses math;
var n,i,j,x,y,ans,ma,m2,d,m3,da:longint;
var a,b:array[0..1000] of longint;
begin
  read(n);
  for i:=1 to n do
    for j:=1 to 10 do begin read(x);if x=1 then inc(a[i]);end;
  ma:=-maxlongint;d:=-1;
  for i:=1 to n do
  begin
    m2:=-maxlongint;m3:=-maxlongint;
    for j:=0 to 10 do
    begin
      read(x);if j=0 then b[i]:=x;
      if j<=a[i] then
      begin
        if x>m2 then begin m2:=x;d:=max(d,j);end;
        if j>0 then
        //if (x>ma) or ((x=ma) and (ma-b[da]<x-b[i])) then
        if (ma-b[da]<x-b[i]) then begin ma:=x;da:=i;end;
      end;
    end;
    inc(ans,m2);
  end;
  if d=0 then begin dec(ans,b[da]);inc(ans,ma);end;
  writeln(ans);
end.


uses math;
var n,i,j,x,y,ans,ma,m2,d,m3,da:longint;
var a,b:array[0..1000] of longint;
begin
  read(n);
  for i:=1 to n do
    for j:=1 to 10 do begin read(x);if x=1 then inc(a[i]);end;
  ma:=-maxlongint;d:=-1;
  for i:=1 to n do
  begin
    m2:=-maxlongint;m3:=-maxlongint;
    for j:=0 to 10 do
    begin
      read(x);if j=0 then b[i]:=x;
      if j<=a[i] then
      begin
        if x>m2 then begin m2:=x;d:=max(d,j);end;
        if j>0 then
        if (x>ma) or ((x=ma) and (ma-b[da]<x-b[i])) then begin ma:=x;da:=i;end;
      end;
    end;
    inc(ans,m2);
  end;
  if d=0 then begin dec(ans,b[da]);inc(ans,ma);end;
  writeln(ans);
end.


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


uses math;
var n,i,j,x,y,ans,ma,m2,d:longint;
var a:array[0..1000] of longint;
begin
  read(n);
  for i:=1 to n do
    for j:=1 to 10 do begin read(x);if x=1 then inc(a[i]);end;
  ma:=-maxlongint;d:=-1;
  for i:=1 to n do
  begin
    m2:=-maxlongint;
    for j:=0 to 10 do begin read(x);if j<=a[i] then
    begin if x>m2 then begin m2:=x;d:=max(d,j);end;
    if j>0 then ma:=max(ma,x);end;end;
    inc(ans,m2);
  end;
  if d=0 then inc(ans,ma);
  writeln(ans);
end.


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

Score : 100 points

Problem Statement

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

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

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

Constraints

1 ≤ Q ≤ 299

2 ≤ m_i ≤ 300

-10^6 ≤ x_i ≤ 10^6

All m_i are distinct.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
m_1 x_1
:
m_Q x_Q

Output

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

Sample Input 1

3
2 10
3 -20
6 15

Sample Output 1

10

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

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

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

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

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

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

Sample Input 2

3
10 -3
50 4
100 -5

Sample Output 2

1

Sample Input 3

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

Sample Output 3

438699


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


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

uses math;

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

const inf = 1061109567;

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

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

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

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

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

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

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

Score : 700 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 5000

0 \leq H_i \leq 10^9

1 \leq P_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 P_1
H_2 P_2
:
H_N P_N

Output

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

Sample Input 1

3
0 2
1 3
3 4

Sample Output 1

2

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

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

Sample Input 2

3
2 4
3 1
4 1

Sample Output 2

3

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

Sample Input 3

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

Sample Output 3

5


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

uses math;

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

const inf = 1061109567;

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

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

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

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

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

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

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


var x:array[0..100]of Qword;
    i,n:longint;
 begin
  x[0]:=2;
  x[1]:=1;
  readln(n);
  for i:=2 to n do
   x[i]:=x[i-1]+x[i-2];
  writeln(x[n]);
 end.

Score : 200 points

Problem Statement

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

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

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

L_0=2

L_1=1

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

Constraints

1≤N≤86

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

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


var
 s,a,b,c,d:longint;
begin
 readln(s);
 d:=s mod 10;
 c:=s div 10 mod 10;
 b:=s div 100 mod 10;
 a:=s div 1000;
 if a+b+c-d=7 then writeln(a,'+',b,'+',c,'-',d,'=7') else
 if a+b-c-d=7 then writeln(a,'+',b,'-',c,'-',d,'=7') else
 if a-b-c-d=7 then writeln(a,'-',b,'-',c,'-',d,'=7') else
 if a+b+c+d=7 then writeln(a,'+',b,'+',c,'+',d,'=7') else
 if a+b-c+d=7 then writeln(a,'+',b,'-',c,'+',d,'=7') else
 if a-b-c+d=7 then writeln(a,'-',b,'-',c,'+',d,'=7') else
 if a-b+c-d=7 then writeln(a,'-',b,'+',c,'-',d,'=7');
end.

Score : 300 points

Problem Statement

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

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

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

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

Constraints

0≤A,B,C,D≤9

All input values are integers.

It is guaranteed that there is a solution.

Input

Input is given from Standard Input in the following format:

ABCD

Output

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

Use the signs + and -.

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

Sample Input 1

1222

Sample Output 1

1+2+2+2=7

This is the only valid solution.

Sample Input 2

0290

Sample Output 2

0-2+9+0=7

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

Sample Input 3

3242

Sample Output 3

3+2+4-2=7


var
	h,w,a,s,i,j,k:Longint;
	c:array[0..9,0..9]of Longint;
begin
	read(h,w);
	for i:=0 to 9 do for j:=0 to 9 do read(c[i,j]);
	for k:=0 to 9 do for i:=0 to 9 do for j:=0 to 9 do if c[i,j]>c[i,k]+c[k,j]then c[i,j]:=c[i,k]+c[k,j];
	for i:=1 to h*w do begin
		read(a);
		if a>=0 then inc(s,c[a,1]);
	end;
	writeln(s);
end.

Score : 400 points

Problem Statement

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

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

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

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

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

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

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

Constraints

1≤H,W≤200

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

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

-1≤A_{i,j}≤9

All input values are integers.

There is at least one digit on the wall.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

12

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

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

Sample Input 2

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

Sample Output 2

0

Note that she may not need to change any digit.

Sample Input 3

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

Sample Output 3

47


var x,c,y:char;
begin
read(x,c,y);
if ord(x)>ord(y)then writeln('>')else if ord(x)<ord(y)then writeln('<')else writeln('=');
end.

Score : 100 points

Problem Statement

In programming, hexadecimal notation is often used.

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

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

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

Constraints

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

Input

Input is given from Standard Input in the following format:

X Y

Output

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

Sample Input 1

A B

Sample Output 1

<

10 < 11.

Sample Input 2

E C

Sample Output 2

>

14 > 12.

Sample Input 3

F F

Sample Output 3

=

15 = 15.


var
 x,y:char;
begin
 read(x,y);
 if x>y then writeln('>');
 if x=y then writeln('=');
 if x<y then writeln('<');
end.


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

Score : 200 points

Problem Statement

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

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

At most how many people can sit on the seat?

Constraints

All input values are integers.

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

Y+2Z \leq X

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the answer.

Sample Input 1

13 3 1

Sample Output 1

3

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

Figure

Sample Input 2

12 3 1

Sample Output 2

2

Sample Input 3

100000 1 1

Sample Output 3

49999

Sample Input 4

64146 123 456

Sample Output 4

110

Sample Input 5

64145 123 456

Sample Output 5

109


program kursi;
var
x,y,z,orang : integer;
begin
readln(x,y,z);
orang := ((x - z) div (y + z));
writeln(orang);
end.


program karosi;
var
	x, y, z : integer;
begin
	readln(x, y, z);
	write((x-z) div (y+z) );
end.


program Project1;

{$APPTYPE CONSOLE}

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


var n,m,x,i:int64;
begin
read(n,m);
x:=1;
for i:=1 to m do begin x:=x*2;end;
writeln(x*100*(18*m+n));
end.

Score : 300 points

Problem Statement

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

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

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

Now, he goes through the following process:

Submit the code.

Wait until the code finishes execution on all the cases.

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

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

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

Constraints

All input values are integers.

1 \leq N \leq 100

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

Input

Input is given from Standard Input in the following format:

N M

Output

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

Sample Input 1

1 1

Sample Output 1

3800

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

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

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

Sample Input 2

10 2

Sample Output 2

18400

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

Sample Input 3

100 5

Sample Output 3

608000


uses Math;
var N,Z,W,i,S,T:Longint;
begin
read(N,Z,W);
for i:=1 to N do begin
S:=T;
read(T);
end;
if N=1 then S:=abs(W-T)else S:=max(abs(S-T),abs(T-W));
writeln(S);
end.

Score : 500 points

Problem Statement

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

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

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

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

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

Constraints

All input values are integers.

1 \leq N \leq 2000

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

Input

Input is given from Standard Input in the following format:

N Z W
a_1 a_2 ... a_N

Output

Print the score.

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900

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

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900

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

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999


uses Math;
var N,Z,W,i,S,T:Longint;
begin
read(N,Z,W);
for i:=1 to N do begin
S:=T;
read(T);
end;
if N=1 then S:=abs(W-T)else S:=min(abs(S-T),abs(T-W));
writeln(S);
end.


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


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


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


var n,z,w:longint;
    x:longint;
    ans,mx,mn:longint;
    i:longint;
function max(a,b:longint):longint;
begin
  if a>b then exit(a) else exit(b);
end;
begin
  read(n,z,w);
  mn:=maxlongint;
  for i:=1 to n do
  begin
    read(x);
    if x>mx then mx:=x;
    if x<mn then mn:=x;
  end;
  ans:=abs(w-x);
  ans:=max(ans,abs(mx-x));
  ans:=max(ans,abs(mn-x));
  writeln(ans);
end.


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


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

Score : 700 points

Problem Statement

We have N gemstones labeled 1 through N.

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

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

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

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

Constraints

All input values are integers.

1 \leq N \leq 100

|a_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the maximum amount of money that can be earned.

Sample Input 1

6
1 2 -6 4 5 3

Sample Output 1

12

It is optimal to smash Gem 3 and 6.

Sample Input 2

6
100 -100 -100 -100 100 -100

Sample Output 2

200

Sample Input 3

5
-1 -2 -3 -4 -5

Sample Output 3

0

It is optimal to smash all the gems.

Sample Input 4

2
-1000 100000

Sample Output 4

99000


program Project1;

{$APPTYPE CONSOLE}

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

    for i := n downto 1 do
      if power(trunc(sqrt(i)),2)=i then begin
        Writeln(i);
        break;
      end;
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

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

Constraints

1 \leq N \leq 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the largest square number not exceeding N.

Sample Input 1

10

Sample Output 1

9

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

Sample Input 2

81

Sample Output 2

81

Sample Input 3

271828182

Sample Output 3

271821169


program V1501;
 type
  w=array[0..100001] of longint;
 var
  a,b,c,d,e,f:w;
  i,n,p:longint;
 procedure haha(var a:w;l,r:longint);
  var
   o,p,m,t:longint;
  begin
   o:=l;
   p:=r;
   m:=a[(l+r) div 2];
   repeat
    while a[o]<m do inc(o);
    while a[p]>m do dec(p);
    if o<=p then
     begin
      t:=a[o];
      a[o]:=a[p];
      a[p]:=t;
      inc(o);
      dec(p);
     end;
   until o>p;
   if o<r then haha(a,o,r);
   if l<p then haha(a,l,p);
  end;
 begin
  readln(n);
  for i:=1 to n do
   read(a[i]);
  readln;
  a[n+1]:=1008208820;
  haha(a,1,n+1);
  for i:=1 to n do
   read(b[i]);
  readln;
  b[n+1]:=1008208820;
  haha(b,1,n+1);
  for i:=1 to n do
   read(c[i]);
  readln;
  c[n+1]:=1008208820;
  haha(c,1,n+1);
  for i:=1 to n+1 do
   f[i]:=n-i+1;
  p:=1;
  for i:=1 to n do
   begin
    while c[p]<=b[i] do inc(p);
    e[i]:=f[p];
   end;
  e[n+1]:=0;
  for i:=n downto 1 do
   inc(e[i],e[i+1]);
  p:=1;
  for i:=1 to n do
   begin
    while b[p]<=a[i] do inc(p);
    d[i]:=e[p];
   end;
  d[n+1]:=0;
  for i:=n downto 1 do
   inc(d[i],d[i+1]);
  writeln(d[1]);
 end.

Score : 300 points

Problem Statement

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.

He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i.

To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.

How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9(1\leq i\leq N)

1 \leq B_i \leq 10^9(1\leq i\leq N)

1 \leq C_i \leq 10^9(1\leq i\leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N
B_1 ... B_N
C_1 ... C_N

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

Upper: 1-st part, Middle: 1-st part, Lower: 1-st part

Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part

Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

87


var n,w,k,i,j:longint;
    ans:int64;
    a,b,c:array[0..100001]of longint;
    procedure sort(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]<x do
            inc(i);
           while x<a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
    procedure sort1(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=b[(l+r) div 2];
         repeat
           while b[i]<x do
            inc(i);
           while x<b[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=b[i];
                b[i]:=b[j];
                b[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort1(l,j);
         if i<r then
           sort1(i,r);
      end;
    procedure sort2(l,r: longint);
      var
         i,j,x,y:longint;
      begin
         i:=l;
         j:=r;
         x:=c[(l+r) div 2];
         repeat
           while c[i]<x do
            inc(i);
           while x<c[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=c[i];
                c[i]:=c[j];
                c[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort2(l,j);
         if i<r then
           sort2(i,r);
      end;
begin
  readln(n);
  for i:=1 to n do
    read(a[i]);
  for i:=1 to n do
    read(b[i]);
  for i:=1 to n do
    read(c[i]);
  sort(1,n);
  sort1(1,n);
  sort2(1,n);
  for i:=1 to n do
    begin
      if b[1]>a[i]
      then k:=k+1;
    end;
  for i:=1 to n do
    begin
      if c[i]>b[1]
      then w:=w+1;
    end;
  for i:=1 to n do
    begin
      while (b[i]>a[k+1])and(k+1<=n) do k:=k+1;
      while (b[i]>=c[n-w+1])and(n-w+1<=n) do w:=w-1;
      ans:=ans+k*w;
      writeln(k,' ',w);
    end;
  writeln(ans);
end.


var n:string;
    i,w,ans,min,k,j:longint;
    a,b:array[0..100000]of longint;
procedure gao;
var i:longint;
begin
  for i:=1 to k do
    a[i]:=a[i]+b[i];
  for i:=1 to w do
    if a[i]>9
    then begin
           a[i+1]:=a[i+1]+1;
           a[i]:=a[i]-10;
         end;
  if a[w+1]>0
  then w:=w+1;
  for i:=1 to w do
    ans:=ans+a[i];
end;
begin
  readln(n);
  min:=0;
  for i:=length(n) downto 1 do
    begin
      a[length(n)-i+1]:=ord(n[i])-48;
      min:=min+a[length(n)-i+1];
    end;
  for i:=1 to length(n) do
    b[i]:=a[i];
  k:=length(n);
  w:=length(n);
  for i:=1 to 500000 do
    begin
      ans:=0;
      gao;
      if ans<min
      then min:=ans;
    end;
  writeln(min);
end.

Score : 700 points

Problem Statement

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Constraints

2 \leq K \leq 10^5

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Sample Input 1

6

Sample Output 1

3

12=6×2 yields the smallest sum.

Sample Input 2

41

Sample Output 2

5

11111=41×271 yields the smallest sum.

Sample Input 3

79992

Sample Output 3

36


var n:string;
    i,w,ans,min,k,j:longint;
    a,b:array[0..100000]of longint;
procedure gao;
var i:longint;
begin
  for i:=1 to k do
    a[i]:=a[i]+b[i];
  for i:=1 to w do
    if a[i]>9
    then begin
           a[i+1]:=a[i+1]+1;
           a[i]:=a[i]-10;
         end;
  if a[w+1]>0
  then w:=w+1;
  for i:=1 to w do
    ans:=ans+a[i];
end;
begin
  readln(n);
  min:=0;
  for i:=length(n) downto 1 do
    begin
      a[length(n)-i+1]:=ord(n[i])-48;
      min:=min+a[length(n)-i+1];
    end;
  for i:=1 to length(n) do
    b[i]:=a[i];
  k:=length(n);
  w:=length(n);
  for i:=1 to 250000 do
    begin
      ans:=0;
      gao;
      if ans<min
      then min:=ans;
    end;
  writeln(min);
end.


var n:string;
    i,w,ans,min,k,j:longint;
    a,b:array[0..100000]of longint;
procedure gao;
var i:longint;
begin
  for i:=1 to k do
    a[i]:=a[i]+b[i];
  for i:=1 to w do
    if a[i]>9
    then begin
           a[i+1]:=a[i+1]+1;
           a[i]:=a[i]-10;
         end;
  if a[w+1]>0
  then w:=w+1;
  for i:=1 to w do
    ans:=ans+a[i];
end;
begin
  readln(n);
  min:=0;
  for i:=length(n) downto 1 do
    begin
      a[length(n)-i+1]:=ord(n[i])-48;
      min:=min+a[length(n)-i+1];
    end;
  for i:=1 to length(n) do
    b[i]:=a[i];
  k:=length(n);
  w:=length(n);
  for i:=1 to 200000 do
    begin
      ans:=0;
      gao;
      if ans<min
      then min:=ans;
    end;
  writeln(min);
end.


var n,i,bao,bao1,t,w,x,mid:longint;
max:int64;
a,b,c,t1:array[1..500000] of longint;
  procedure sort(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]<x do
            inc(i);
           while x<a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;

begin

readln(n);
for i:=1 to n do read(a[i]);
for i:=1 to n do read(b[i]);
for i:=1 to n do read(c[i]);
sort(1,n);
t1:=a;a:=c;
sort(1,n);
c:=a;a:=b;
sort(1,n);
b:=a;a:=t1;
for i:=1 to n do
  begin
  bao:=0;bao1:=0;
  t:=1;
  w:=n;
  x:=b[i];
  while t<=w do
    begin
    mid:=(t+w) div 2;
    if x>a[mid] then
      begin
      bao:=mid;
      t:=mid+1;
      end
    else w:=mid-1;
    end;

  t:=1;
  w:=n;
  x:=b[i];
  while t<=w do
    begin
    mid:=(t+w) div 2;
    if x<c[mid] then
      begin
      bao1:=mid;
      w:=mid-1;
      end
    else t:=mid+1;
    end;
  if bao>0 then
    max:=max+bao*(n-bao1+1);
  end;
write(max);

end.


type
  arr=array[0..200000]of int64;
var
   a,b,c,num,sum:arr;
   i:longint;
   n:longint;
   ans:int64;
   last:longint;
procedure qs(l,r:longint;var a:arr);
var i,j:longint;
    m,t:int64;
begin
  i:=l;
  j:=r;
  m:=a[(l+r)>>1];
  repeat
    while a[i]<m do inc(i);
    while a[j]>m do dec(j);
    if i<=j then
    begin
      t:=a[i];a[i]:=a[j];a[j]:=t;
      inc(i);
      dec(j);
    end;
  until i>j;
  if l<j then qs(l,j,a);
  if i<r then qs(i,r,a);
end;
begin
  read(n);
  for i:=1 to n do
  read(a[i]);
  for i:=1 to n do
  read(b[i]);
  for i:=1 to n do
  read(c[i]);
  qs(1,n,a);
  qs(1,n,b);
  qs(1,n,c);
  last:=1;
  for i:=1 to n do
  begin
    while true do
    begin
      if b[i]<c[last] then
      begin
        num[i]:=n-last+1;
        break;
      end else
      begin
        if last=n+1 then break else inc(last);
      end;
    end;
  end;
  for i:=n downto 1 do
  sum[i]:=sum[i+1]+num[i];
  last:=1;
  for i:=1 to n do
  begin
    while true do
    begin
      if a[i]<b[last] then
      begin
        inc(ans,sum[last]);
        break;
      end else
      begin
        if last=n+1 then break else inc(last);
      end;
    end;
  end;
  writeln(ans);
end.


var n:longint;
    dist:array[0..100050]of longint;
    q:array[0..5000050]of longint;
    v:array[0..100050]of boolean;
procedure spfa;
var  head,tail:longint;
     i,now,y:longint;
begin
  head:=1;
  tail:=0;
  for i:=1 to n-1 do
  dist[i]:=i;
  dist[0]:=n;
  for i:=0 to n-1 do
  begin
    inc(tail);
    q[tail]:=i;
    v[i]:=true;
  end;
  while head<=tail do
  begin
    now:=q[head];
    for i:=0 to 9 do
    begin
      y:=(now*10+i) mod n;
      if dist[y]>dist[now]+i then
      begin
        dist[y]:=dist[now]+i;
        if not v[y] then
        begin
          v[y]:=true;
          inc(tail);
          q[tail]:=y;
        end;
      end;
    end;
    inc(head);
    v[now]:=false;
  end;
end;
begin
  read(n);
  spfa;
  writeln(dist[0]);
end.


uses math;
var n,i,p:longint;a,b,c,d,e,o:int64;
begin     a:=1;
read(n,p);
for i:=1 to n do
   begin
   a:=a+min(a,p);
   end;                writeln(a);
end.

Score : 200 points

Problem Statement

Square1001 has seen an electric bulletin board displaying the integer 1.
He can perform the following operations A and B to change this value:

Operation A: The displayed value is doubled.

Operation B: The displayed value increases by K.

Square1001 needs to perform these operations N times in total.
Find the minimum possible value displayed in the board after N operations.

Constraints

1 \leq N, K \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
K

Output

Print the minimum possible value displayed in the board after N operations.

Sample Input 1

4
3

Sample Output 1

10

The value will be minimized when the operations are performed in the following order: A, A, B, B.

In this case, the value will change as follows: 1 → 2 → 4 → 7 → 10.

Sample Input 2

10
10

Sample Output 2

76

The value will be minimized when the operations are performed in the following order: A, A, A, A, B, B, B, B, B, B.

In this case, the value will change as follows: 1 → 2 → 4 → 8 → 16 → 26 → 36 → 46 → 56 → 66 → 76.

By the way, this contest is AtCoder Beginner Contest 076.


var n,i,j,ans:longint;
var a,r,b:array[0..1000] of longint;
var f:array[0..1000,0..1000] of longint;
procedure dfs(d:longint);
var i,j:longint;
var a1:int64;
begin
  if d>n then begin
  a1:=1;
    for i:=1 to n do
    begin if abs(a[i]-b[i])>1 then exit;a1:=a1*a[i];end;
 if a1 mod 2=0 then     inc(ans);
    exit;
  end;
    a[d]:=b[d];dfs(d+1);
    a[d]:=b[d]-1;dfs(d+1);
    a[d]:=b[d]+1;dfs(d+1);
end;
begin
  read(n);
  for i:=1 to n do read(b[i]);dfs(1);
  writeln(ans);
end.

Score : 200 points

Problem Statement

We will say that two integer sequences of length N, x_1, x_2, ..., x_N and y_1, y_2, ..., y_N, are similar when |x_i - y_i| \leq 1 holds for all i (1 \leq i \leq N).

In particular, any integer sequence is similar to itself.

You are given an integer N and an integer sequence of length N, A_1, A_2, ..., A_N.

How many integer sequences b_1, b_2, ..., b_N are there such that b_1, b_2, ..., b_N is similar to A and the product of all elements, b_1 b_2 ... b_N, is even?

Constraints

1 \leq N \leq 10

1 \leq A_i \leq 100

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the number of integer sequences that satisfy the condition.

Sample Input 1

2
2 3

Sample Output 1

7

There are seven integer sequences that satisfy the condition:

1, 2

1, 4

2, 2

2, 3

2, 4

3, 2

3, 4

Sample Input 2

3
3 3 3

Sample Output 2

26

Sample Input 3

1
100

Sample Output 3

1

Sample Input 4

10
90 52 56 71 44 8 13 30 57 84

Sample Output 4

58921


var i,j,n,ans:longint;
var s:ansistring;
begin
  readln(s);n:=length(s);
  i:=1;j:=n;
  while (i<=j) do
  begin
    if s[i]=s[j] then begin inc(i);dec(j);continue;end;
    if (s[i]='x') then begin inc(ans);inc(i);continue;end;
    if s[j]='x' then begin inc(ans);dec(j);continue;end;
    writeln(-1);exit;
  end;
  writeln(ans);
end.

Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are palindromes, while ab, abab and abcda are not.

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required.
If it is not, print -1 instead.

Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx


var a:array['a'..'z'] of longint;
var s:ansistring;
var ans,i,j:longint;
begin
  readln(s);for i:=1 to length(s) do inc(a[s[i]]);
  for i:=1 to 26 do if a[chr(i+ord('a'))] mod 2=1 then inc(ans);
  if ans=0 then inc(ans);writeln(ans);
end.

Score : 700 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke is partitioning s into some number of non-empty substrings.
Let the subtrings obtained be s_1, s_2, ..., s_N from left to right. (Here, s = s_1 + s_2 + ... + s_N holds.)
Snuke wants to satisfy the following condition:

For each i (1 \leq i \leq N), it is possible to permute the characters in s_i and obtain a palindrome.

Find the minimum possible value of N when the partition satisfies the condition.

Constraints

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

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum possible value of N when the partition satisfies the condition.

Sample Input 1

aabxyyzz

Sample Output 1

2

The solution is to partition s as aabxyyzz = aab + xyyzz.
Here, aab can be permuted to form a palindrome aba, and xyyzz can be permuted to form a palindrome zyxyz.

Sample Input 2

byebye

Sample Output 2

1

byebye can be permuted to form a palindrome byeeyb.

Sample Input 3

abcdefghijklmnopqrstuvwxyz

Sample Output 3

26

Sample Input 4

abcabcxabcx

Sample Output 4

3

The solution is to partition s as abcabcxabcx = a + b + cabcxabcx.


var a:array['a'..'z'] of longint;
var s:ansistring;
var ans,i,j:longint;
begin
  readln(s);for i:=1 to length(s) do inc(a[s[i]]);
  for i:=1 to 26 do if a[chr(i+ord('a'))] mod 2=1 then inc(ans);
  writeln(ans);
end.


var s:string;i,n:integer;
begin
readln(s);
n:=length(s);
for i:=1 to n-8 do begin
write(s[i]);
end;
writeln();
end.

Score : 100 points

Problem Statement

Rng is going to a festival.

The name of the festival is given to you as a string S, which ends with FESTIVAL, from input. Answer the question: "Rng is going to a festival of what?" Output the answer.

Here, assume that the name of "a festival of s" is a string obtained by appending FESTIVAL to the end of s.
For example, CODEFESTIVAL is a festival of CODE.

Constraints

9 \leq |S| \leq 50

S consists of uppercase English letters.

S ends with FESTIVAL.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer to the question: "Rng is going to a festival of what?"

Sample Input 1

CODEFESTIVAL

Sample Output 1

CODE

This is the same as the example in the statement.

Sample Input 2

CODEFESTIVALFESTIVAL

Sample Output 2

CODEFESTIVAL

This string is obtained by appending FESTIVAL to the end of CODEFESTIVAL, so it is a festival of CODEFESTIVAL.

Sample Input 3

YAKINIKUFESTIVAL

Sample Output 3

YAKINIKU


var
    n,m,i:longint;
    d,t:array[-1..200000]of longint;
    td,tt:longint;

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

procedure qsort( l,r:longint);
var
  x,e,j,i:longint;
begin
  i:=l;j:=r;x:=(l+r)div 2;
  repeat
    while (t[i]<t[x]) do inc(i);
    while(t[j]>t[x]) do dec(j);
    if(i<=j)then
    begin
      e:=t[i];t[i]:=t[j];t[j]:=e;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);
  for i:=1 to n do
    read(d[i]);
  readln;
  readln(m);
  for i:=1 to m do
    read(t[i]);
  readln;
  sort(1,n);
  qsort(1,m);
  td:=n;tt:=m;
  repeat
    if t[tt]=d[td] then begin dec(tt);dec(td);end
    else if t[tt]<d[td] then dec(td)
    else if(td<tt)then begin writeln('NO');exit;end;
  until(tt=0);
  writeln('YES');
end.

Score : 200 points

Problem Statement

Rng is preparing a problem set for a qualification round of CODEFESTIVAL.

He has N candidates of problems. The difficulty of the i-th candidate is D_i.

There must be M problems in the problem set, and the difficulty of the i-th problem must be T_i. Here, one candidate of a problem cannot be used as multiple problems.

Determine whether Rng can complete the problem set without creating new candidates of problems.

Constraints

1 \leq N \leq 200,000

1 \leq D_i \leq 10^9

1 \leq M \leq 200,000

1 \leq T_i \leq 10^9

All numbers in the input are integers.

Partial Score

100 points will be awarded for passing the test set satisfying N \leq 100 and M \leq 100.

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N
M
T_1 T_2 ... T_M

Output

Print YES if Rng can complete the problem set without creating new candidates of problems; print NO if he cannot.

Sample Input 1

5
3 1 4 1 5
3
5 4 3

Sample Output 1

YES

Sample Input 2

7
100 200 500 700 1200 1600 2000
6
100 200 500 700 1600 1600

Sample Output 2

NO

Not enough 1600s.

Sample Input 3

1
800
5
100 100 100 100 100

Sample Output 3

NO

Sample Input 4

15
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
9
5 4 3 2 1 2 3 4 5

Sample Output 4

YES


var
  flag:boolean;
  da,sl:int64;
  hs:array[1..2] of int64;
  x,y,n,u,m,i:longint;
  hd,nx,t,ys:array[0..200100] of longint;
  procedure jb(x,y:longint);
    begin
      u:=u+1;
      nx[u]:=hd[x];
      hd[x]:=u;
      t[u]:=y;
    end;
  function ss(q,sc:longint):boolean;
    var
      i:longint;
      cg:boolean;
    begin
      ys[q]:=sc;
      sl:=sl+1;
      hs[sc]:=hs[sc]+1;
      cg:=true;
      i:=hd[q];
      while i<>0 do
        begin
          if (ys[t[i]]=0)and(ss(t[i],3-sc)=false) then cg:=false;
          if ys[t[i]]=sc then
            begin
              cg:=false;
              flag:=false;
            end;
          i:=nx[i];
        end;
      exit(cg);
    end;
begin
  readln(n,m);
  for i:=1 to m do
    begin
      readln(x,y);
      jb(x,y);
      jb(y,x);
    end;
  for i:=1 to n do
    if ys[i]=0 then
      begin
        hs[1]:=0;
        hs[2]:=0;
        sl:=0;
        flag:=true;
        if ss(i,1)<>flag then writeln('?');
        if flag then da:=da+hs[1]*hs[2]
        else da:=da+sl*(sl-1) div 2;
      end;
  writeln(da-m);
end.

Score : 500 points

Problem Statement

Rng has a connected undirected graph with N vertices.
Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.

Rng will add new edges to the graph by repeating the following operation:

Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.

Find the maximum possible number of edges that can be added.

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i,B_i \leq N

The graph has no self-loops or multiple edges.

The graph is connected.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Find the maximum possible number of edges that can be added.

Sample Input 1

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

Sample Output 1

4

If we add edges as shown below, four edges can be added, and no more.

Sample Input 2

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

Sample Output 2

5

Five edges can be added, for example, as follows:

Add an edge connecting Vertex 5 and Vertex 3.

Add an edge connecting Vertex 5 and Vertex 2.

Add an edge connecting Vertex 4 and Vertex 1.

Add an edge connecting Vertex 4 and Vertex 2.

Add an edge connecting Vertex 4 and Vertex 3.


var
  n:int64;
  hs:array[1..2] of int64;
  x,y,u,m,i:longint;
  hd,nx,t,ys:array[0..200100] of longint;
  procedure jb(x,y:longint);
    begin
      u:=u+1;
      nx[u]:=hd[x];
      hd[x]:=u;
      t[u]:=y;
    end;
  procedure ss(q,sc:longint);
    var
      i:longint;
    begin
      ys[q]:=sc;
      hs[sc]:=hs[sc]+1;
      i:=hd[q];
      while i<>0 do
        begin
          if ys[t[i]]=sc then
            begin
              writeln(n*(n-1) div 2-m);
              halt;
            end;
          if ys[t[i]]=0 then ss(t[i],3-sc);
          i:=nx[i];
        end;
    end;
begin
  readln(n,m);
  for i:=1 to m do
    begin
      readln(x,y);
      jb(x,y);
      jb(y,x);
    end;
  ss(i,1);
  writeln(hs[1]*hs[2]-m);
end.
5 5
1 2
2 3
3 4
4 5
5 1


var
  hs:array[1..2] of int64;
  x,y,n,u,m,i:longint;
  hd,nx,t,ys:array[0..200100] of longint;
  procedure jb(x,y:longint);
    begin
      u:=u+1;
      nx[u]:=hd[x];
      hd[x]:=u;
      t[u]:=y;
    end;
  procedure ss(q,sc:longint);
    var
      i:longint;
    begin
      ys[q]:=sc;
      hs[sc]:=hs[sc]+1;
      i:=hd[q];
      while i<>0 do
        begin
          if ys[t[i]]=sc then
            begin
              writeln(n*(n-1) div 2-m);
              halt;
            end;
          if ys[t[i]]=0 then ss(t[i],3-sc);
          i:=nx[i];
        end;
    end;
begin
  readln(n,m);
  for i:=1 to m do
    begin
      readln(x,y);
      jb(x,y);
      jb(y,x);
    end;
  ss(i,1);
  writeln(hs[1]*hs[2]-m);
end.


 Uses Math;

  const MAX_N = 500000 + 120;

  var S : AnsiString;
     N : LongInt;
     ans : Int64;

 var dp : array [ 0 .. MAX_N , 1 .. 2 ] of LongInt;
     size : array [ 1 .. MAX_N + 12 ] of LongInt;

 procedure Solve ( l , R : LongInt ) ;
   var i : LongInt;
       x , cnt : LongInt;
 begin
  if ( L + 2 > R ) then exit();

  for i := 1 to R - L + 10 do
  begin
    dp[i][1] := 0;
    dp[i][2] := 0;
    size[i] := 0;
  end;

  cnt := 0;
  x := 0;

  if ( S[R] = '0' ) then halt(1);

  for i := L to R do
  begin
    if ( s[i] = '1' ) then
    begin
      cnt += 1;
    end
    else
    begin
      inc ( x ) ;
      size[x] := cnt - 1;
      cnt := 0;
    end;
  end;
  size[x+1] := cnt - 1;

  dp[0][1] := 0;
  dp[0][2] := 0;

  dp[1][1] := size[1] + 1;
  if ( X <> 1 ) then
    dp[1][2] := 1 + (size[2] - 1)
  else
    dp[1][2] := 1 + (size[2]);

  for i := 2 to X do
  begin

    if ( size[i] <> 0 ) then // not 10101
    begin
      dp[i][1] := max ( dp[i-1][1] + (size[i] - 1) + 1 , dp[i-1][2] + 1 );
      if ( i = X ) then
        dp[i][2] := max ( dp[i-1][1] , dp[i-1][2] ) + size[i+1] + 1
      else
        dp[i][2] := max ( dp[i-1][1] , dp[i-1][2] ) + size[i+1] - 1 + 1;
    end
    else
    begin
      dp[i][1] := max ( max ( dp[i-1][1] , dp[i-1][2] ) , max(dp[i-2][1] , dp[i-2][2] ) + 1 );
      if ( I = X ) then
        dp[i][2] := max ( max ( dp[i-1][1] , dp[i-1][2] ) , max(dp[i-2][1] , dp[i-2][2] ) + size[x+1] + 1 )
      else
        dp[i][2] := max ( max ( dp[i-1][1] , dp[i-1][2] ) , max(dp[i-2][1] , dp[i-2][2] ) + size[x+1] - 1 + 1 );
    end;
    dp[i][1] := max ( dp[i][1] , dp[i][2] );
  end;

  ans += max ( dp[x][1] , dp[x][2] );

 end;

 var last : LongINt;

 var i : LongInt;

begin
  ans := 0;

  readLn ( N ) ;
  readLn ( S ) ;

  last := 0;

  for i := 1 to N do
  begin
    if ( S[i] = '0' ) and ( S[i+1] = '0' ) then
    begin
      Solve ( last + 1 , i - 1 );
      last := i + 1;
    end;
  end;

  if ( S[n] = '0' ) then
    Solve ( last + 1 , n-1 )
  else
    Solve ( last + 1 , n );

  writeLn ( ans );

end.

Score : 700 points

Problem Statement

N cells are arranged in a row.
Some of them may contain tokens.
You are given a string s that consists of 0s and 1s.
If the i-th character of s is 1, the i-th cell (from left) contains a token.
Otherwise, it doesn't contain a token.

Snuke wants to perform the following operation as many times as possible.
In each operation, he chooses three consecutive cells.
Let's call the cells X, Y, Z from left to right.
In order for the operation to be valid, both X and Z must contain tokens and Y must not contain a token.
Then, he removes these two tokens and puts a new token on Y.

How many operations can he perform if he performs operations in the optimal way?

Constraints

1 \leq N \leq 500,000

|s| = N

Each character in s is either 0 or 1.

Input

Input is given from Standard Input in the following format:

N
s

Output

Print the answer.

Sample Input 1

7
1010101

Sample Output 1

2

For example, he can perform two operations in the following way:

Perform an operation on the last three cells. Now the string that represents tokens becomes 1010010.

Perform an operation on the first three cells. Now the string that represents tokens becomes 0100010.

Note that the choice of operations matters.
For example, if he chooses three cells in the middle first, he can perform no more operations.

Sample Input 2

50
10101000010011011110001001111110000101010111100110

Sample Output 2

10


 Uses Math;

  const MAX_N = 500000 + 120;

  var S : AnsiString;
     N : LongInt;
     ans : Int64;

 var dp : array [ 0 .. MAX_N , 1 .. 2 ] of LongInt;
     size : array [ 1 .. MAX_N + 12 ] of LongInt;

 procedure Solve ( l , R : LongInt ) ;
   var i : LongInt;
       x , cnt : LongInt;
 begin
  if ( L + 2 > R ) then exit();

  for i := 1 to R - L + 10 do
  begin
    dp[i][1] := 0;
    dp[i][2] := 0;
    size[i] := 0;
  end;

  cnt := 0;
  x := 0;

  if ( S[R] = '0' ) then halt(1);

  for i := L to R do
  begin
    if ( s[i] = '1' ) then
    begin
      cnt += 1;
    end
    else
    begin
      inc ( x ) ;
      size[x] := cnt - 1;
      cnt := 0;
    end;
  end;
  size[x+1] := cnt - 1;

  dp[0][1] := 0;
  dp[0][2] := 0;

  dp[1][1] := size[1] + 1;
  dp[1][2] := 1 + size[2];

  for i := 2 to X do
  begin

    if ( size[i] <> 0 ) then // not 10101
    begin
      dp[i][1] := max ( dp[i-1][1] + size[i] + 1 , dp[i-1][2] + 1 );
      dp[i][2] := max ( dp[i-1][1] , dp[i-1][2] ) + size[i+1] + 1;
    end
    else
    begin
      dp[i][1] := max ( max ( dp[i-1][1] , dp[i-1][2] ) , max(dp[i-2][1] , dp[i-2][2] ) + 1 );
      dp[i][2] := max ( max ( dp[i-1][1] , dp[i-1][2] ) , max(dp[i-2][1] , dp[i-2][2] ) + size[x+1] + 1 );
    end;
    dp[i][1] := max ( dp[i][1] , dp[i][2] );
  end;

  ans += max ( dp[x][1] , dp[x][2] );

 end;

 var last : LongINt;

 var i : LongInt;

begin
  ans := 0;

  readLn ( N ) ;
  readLn ( S ) ;

  last := 0;

  for i := 1 to N do
  begin
    if ( S[i] = '0' ) and ( S[i+1] = '0' ) then
    begin
      Solve ( last + 1 , i - 1 );
      last := i + 1;
    end;
  end;

  if ( S[n] = '0' ) then
    Solve ( last + 1 , n-1 )
  else
    Solve ( last + 1 , n );

  writeLn ( ans );

end.


var n,i,a,b,x,y:Longint;
begin
read(n);
for i:=1 to n do begin
read(a,b);
if x<a then begin x:=a;y:=a+b;end;
end;
writeln(y);
end.

Score : 200 points

Problem Statement

A group of people played a game. All players had distinct scores, which are positive integers.

Takahashi knows N facts on the players' scores. The i-th fact is as follows: the A_i-th highest score among the players is B_i.

Find the maximum possible number of players in the game.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9(1\leq i\leq N)

0 \leq B_i \leq 10^9(1\leq i\leq N)

If i ≠ j, A_i ≠ A_j.

There exists a possible outcome of the game that are consistent with the facts.

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Outputs

Print the maximum possible number of players in the game.

Sample Input 1

3
4 7
2 9
6 2

Sample Output 1

8

The maximum possible number of players is achieved when, for example, the players have the following scores: 12,9,8,7,5,2,1,0.

Sample Input 2

5
1 10
3 6
5 2
4 4
2 8

Sample Output 2

7

Sample Input 3

2
1 1000000000
1000000000 1

Sample Output 3

1000000001


var
   n,a,x,z,i:longint;
begin
  read(n);
  z:=2000000002;
  for i:=1 to n do begin
    read(a,x);
    if a+x<z then z:=a+x;
  end;
  writeln(z);
end.


var n,i:longint;
    a,x:longint;
    max,maxx,min:longint;
    z:longint;
begin
  read(n);
  z:=0;
  if n=0 then writeln(1 div z);
  min:=maxlongint;
  for i:=1 to n do
  begin
    read(a,x);
    if a>max then max:=a;
    if x<min then min:=x;
  end;
  writeln(max+min);
end.


var n,i:longint;
    a,x:longint;
    max,maxx,min:longint;
    z:longint;
begin
  read(n);
  min:=maxlongint;
  for i:=1 to n do
  begin
    read(a,x);
    if a>max then
    begin
      max:=a;
      maxx:=x;
    end;
    if x<min then min:=x;
  end;
  z:=0;
  if min<>maxx then
  begin
    writeln(1 div z);
    exit;
  end;
  writeln(max+maxx);
end.


var
  a,t,i,j,y:longint;
  b,c,d:array[0..100009]of longint;
begin
readln(a);
y:=maxlongint;
for i:=1 to a do
begin
readln(b[i],c[i]);
d[i]:=b[i]+c[i];
if d[i]<y then y:=d[i];
end;
writeln(y);
end.


var n,k,ans,a1,kk:int64;
var i,j:longint;
var a,b,c:array[0..100020] of int64;
procedure qsort(l,r:int64);
var i,j,mid,m2,t:int64;
begin
  i:=l;j:=r;mid:=b[(l+r) >>1];m2:=a[(l+r)>>1];
  repeat
    while (b[i]>mid) or ((b[i]=mid) and (a[i]<m2)) do inc(i);
    while (b[j]<mid) or ((b[j]=mid) and (a[j]>m2)) do dec(j);
    if i<=j then begin
      t:=a[i];a[i]:=a[j];a[j]:=t;
      t:=b[i];b[i]:=b[j];b[j]:=t;
      t:=c[i];c[i]:=c[j];c[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;
procedure qsort1(l,r:int64);
var i,j,mid,m2,t,m3:int64;
begin
  i:=l;j:=r;mid:=b[(l+r) >>1];m2:=a[(l+r)>>1];m3:=c[(l+r)>>1];
  repeat
    while (c[i]>m3) or ((c[i]=m3) and (b[i]>mid))
    or ((c[i]=m3) and (b[i]=mid) and (a[i]<m2)) do inc(i);
    while (c[j]<m3) or ((c[j]=m3) and (b[j]<mid))
    or ((c[j]=m3) and (b[j]=mid) and (a[j]>m2)) do dec(j);
    if i<=j then begin
      t:=a[i];a[i]:=a[j];a[j]:=t;
      t:=b[i];b[i]:=b[j];b[j]:=t;
      t:=c[i];c[i]:=c[j];c[j]:=t;
      inc(i);dec(j);
    end;
  until i>j;
  if l<j then qsort1(l,j);
  if i<r then qsort1(i,r);
end;
begin
  read(n,k);     kk:=k;
  for i:=1 to n do begin read(a[i],b[i]);c[i]:=b[i]-a[i];end;qsort(1,n);
  for i:=1 to n do
    if k-a[i]>=0 then begin inc(ans,b[i]);dec(k,a[i]);end;
  qsort1(1,n);a1:=0;   k:=kk;
  //for i:=1 to n do writeln(c[i],' ',a[i], ' ',b[i]);
  for i:=1 to n do
    if k-a[i]>=0 then begin inc(a1,b[i]);dec(k,a[i]);end;
    //writeln(a1);
  if ans>a1 then writeln(ans)else writeln(a1);
end.

Score : 500 points

Problem Statement

Seisu-ya, a store specializing in non-negative integers, sells N non-negative integers. The i-th integer is A_i and has a utility of B_i.
There may be multiple equal integers with different utilities.

Takahashi will buy some integers in this store. He can buy a combination of integers whose bitwise OR is less than or equal to K. He wants the sum of utilities of purchased integers to be as large as possible.

Find the maximum possible sum of utilities of purchased integers.

Constraints

1 \leq N \leq 10^5

0 \leq K < 2^{30}

0 \leq A_i < 2^{30}(1\leq i\leq N)

1 \leq B_i \leq 10^9(1\leq i\leq N)

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N K
A_1 B_1
:
A_N B_N

Outputs

Print the maximum possible sum of utilities of purchased integers.

Sample Input 1

3 5
3 3
4 4
2 5

Sample Output 1

8

Buy 2 and 3 to achieve the maximum possible total utility, 8.

Sample Input 2

3 6
3 3
4 4
2 5

Sample Output 2

9

Buy 2 and 4 to achieve the maximum possible total utility, 9.

Sample Input 3

7 14
10 5
7 4
11 4
9 8
3 6
6 2
8 9

Sample Output 3

32


var i,j,k,t,s,n,m:longint;
begin
	readln(n,m,k);
	for i:=0 to n do for j:=0 to m do if i*n+j*m=k+i*j*2 then
		begin write('Yes');exit;end;
	write('No');
end.

Score : 200 points

Problem Statement

We have a grid with N rows and M columns of squares. Initially, all the squares are white.

There is a button attached to each row and each column.
When a button attached to a row is pressed, the colors of all the squares in that row are inverted; that is, white squares become black and vice versa.
When a button attached to a column is pressed, the colors of all the squares in that column are inverted.

Takahashi can freely press the buttons any number of times. Determine whether he can have exactly K black squares in the grid.

Constraints

1 \leq N,M \leq 1000

0 \leq K \leq NM

Input

Input is given from Standard Input in the following format:

N M K

Output

If Takahashi can have exactly K black squares in the grid, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

Press the buttons in the order of the first row, the first column.

Sample Input 2

2 2 1

Sample Output 2

No

Sample Input 3

3 5 8

Sample Output 3

Yes

Press the buttons in the order of the first column, third column, second row, fifth column.

Sample Input 4

7 9 20

Sample Output 4

No


var i,j:longint;
    n,m,num2,num4:longint;
    ch:char;
    num:array['a'..'z']of longint;
    have:longint;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      read(ch);
      inc(num[ch]);
    end;
    readln;
  end;
  if (n mod 2=1)and(m mod 2=1) then num4:=((n-1)div 2) * ((m-1) div 2) else
  if (n mod 2=0)and(m mod 2=1) then num4:=(  n  div 2) * ((m-1) div 2) else
  if (n mod 2=1)and(m mod 2=0) then num4:=((n-1)div 2) * (  m   div 2) else
  if (n mod 2=0)and(m mod 2=0) then num4:=(  n  div 2) * (  m   div 2);
  for i:=97 to 122 do
  if (num[char(i)]>=4)and(num4>0) then
  begin
    have:=num[char(i)]div 4;
    if have>num4 then
    begin
      num4:=0;
      dec(num[char(i)],num4*4);
    end else
    begin
      dec(num4,have);
      dec(num[char(i)],have*4);
    end;
  end;
  if num4>0 then
  begin
    writeln('No');
    exit;
  end;
  if (n mod 2=1)and(m mod 2=1) then num2:=(n-1) div 2+(m-1) div 2 else
  if (n mod 2=0)and(m mod 2=1) then num2:=n div 2 else
  if (n mod 2=1)and(m mod 2=0) then num2:=m div 2 else
  if (n mod 2=0)and(m mod 2=0) then num2:=0;
  for i:=97 to 122 do
  if (num[char(i)]>=2)and(num2>0) then
  begin
    have:=num[char(i)] div 2;
    if have>num2 then num2:=0 else dec(num2,have);
  end;
  if num2>0 then
  begin
    writeln('No');
    exit;
  end;
  writeln('Yes');
end.

Score : 400 points

Problem Statement

We have an H-by-W matrix.
Let a_{ij} be the element at the i-th row from the top and j-th column from the left.
In this matrix, each a_{ij} is a lowercase English letter.

Snuke is creating another H-by-W matrix, A', by freely rearranging the elements in A.
Here, he wants to satisfy the following condition:

Every row and column in A' can be read as a palindrome.

Determine whether he can create a matrix satisfying the condition.

Note

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are all palindromes, while ab, abab and abcda are not.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

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

Output

If Snuke can create a matrix satisfying the condition, print Yes; otherwise, print No.

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes


var
  s:string;
  i,j,n,m,d,x,y:longint;
begin
  s:='RGBY';
  readln(n,m,d);
  for i:=0 to n-1 do
  begin
  for j:=0 to m-1 do
  begin
    x:=(m+i+j)div d mod 2;
    y:=(m+i-j)div d mod 2;
    write(s[2*x+y+1]);
  end;
  writeln;
  end;
end.

Score : 700 points

Problem Statement

We have a grid with H rows and W columns of squares.
We will represent the square at the i-th row from the top and j-th column from the left as (i,\ j).
Also, we will define the distance between the squares (i_1,\ j_1) and (i_2,\ j_2) as |i_1 - i_2| + |j_1 - j_2|.

Snuke is painting each square in red, yellow, green or blue.
Here, for a given positive integer d, he wants to satisfy the following condition:

No two squares with distance exactly d have the same color.

Find a way to paint the squares satisfying the condition.
It can be shown that a solution always exists.

Constraints

2 ≤ H, W ≤ 500

1 ≤ d ≤ H + W - 2

Input

Input is given from Standard Input in the following format:

H W d

Output

Print a way to paint the squares satisfying the condition, in the following format. If the square (i,\ j) is painted in red, yellow, green or blue, c_{ij} should be R, Y, G or B, respectively.

c_{11}c_{12}...c_{1W}
:
c_{H1}c_{H2}...c_{HW}

Sample Input 1

2 2 1

Sample Output 1

RY
GR

There are four pairs of squares with distance exactly 1.
As shown below, no two such squares have the same color.

(1,\ 1), (1,\ 2) : R, Y

(1,\ 2), (2,\ 2) : Y, R

(2,\ 2), (2,\ 1) : R, G

(2,\ 1), (1,\ 1) : G, R

Sample Input 2

2 3 2

Sample Output 2

RYB
RGB

There are six pairs of squares with distance exactly 2.
As shown below, no two such squares have the same color.

(1,\ 1) , (1,\ 3) : R , B

(1,\ 3) , (2,\ 2) : B , G

(2,\ 2) , (1,\ 1) : G , R

(2,\ 1) , (2,\ 3) : R , B

(2,\ 3) , (1,\ 2) : B , Y

(1,\ 2) , (2,\ 1) : Y , R