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;
    if max+a[i+1]-1<x then x:=max+a[i+1]-1;
    x:=x div a[i];
    if (x=0) then
    begin
      writeln(-1);
      exit;
    end else
    begin
      if x*a[i]<max then
      begin
        writeln(-1);
        exit;
      end;
      max:=x*a[i];
    end;
    x:=min div a[i];
    if min mod a[i]=0 then min:=x*a[i] else min:=(x+1)*a[i];
  end;
  writeln(min,' ',max+a[1]-1);
end.
//or(((max+a[i+1]-1) div a[i])-((max-1) div a[i])=0)


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];
    //writeln(((max+a[i+1]-1) div a[i])-((max-1) div a[i]));
    if (x=0)or(((max+a[i+1]-1) div a[i])-((max-1) div a[i])=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;
  writeln(min,' ',max+a[1]-1);
end.

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

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

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: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;
  writeln(min,' ',max+a[1]-1);
end.


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


program ABC085B;
{$mode Delphi}
uses SysUtils,fgl;
var
	n,i,r: longint;
	arr: TFPGMap<longint,integer>;
begin
	read(n);
	arr:=TFPGMap<longint,integer>.Create();
	for i:=0 to n-1 do begin
		read(r);
		// arr.AddOrSetData(r,1); // AtCoderでは使用不可
		arr.Remove(r);
		arr.Add(r,1);
	end;
	writeln(arr.Count);
end.

Score : 200 points

Problem Statement

An X-layered kagami mochi (X ≥ 1) is a pile of X round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than that of the mochi directly below it. For example, if you stack three mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, you have a 3-layered kagami mochi; if you put just one mochi, you have a 1-layered kagami mochi.

Lunlun the dachshund has N round mochi, and the diameter of the i-th mochi is d_i centimeters. When we make a kagami mochi using some or all of them, at most how many layers can our kagami mochi have?

Constraints

1 ≤ N ≤ 100

1 ≤ d_i ≤ 100

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
d_1
:
d_N

Output

Print the maximum number of layers in a kagami mochi that can be made.

Sample Input 1

4
10
8
8
6

Sample Output 1

3

If we stack the mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, we have a 3-layered kagami mochi, which is the maximum number of layers.

Sample Input 2

3
15
15
15

Sample Output 2

1

When all the mochi have the same diameter, we can only have a 1-layered kagami mochi.

Sample Input 3

7
50
30
50
100
50
80
30

Sample Output 3

4


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  n,i,j,temp,min:Integer;
  d:array of Integer;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(n);
    SetLength(d,n);
    for i:=0 to n-1 do
      Readln(d[i]);

    for i := 0 to n-1 do begin
      min := i;
      for j := i+1 to n-1 do begin
        if d[j] < d[min] then
        min := j;
      end;
      temp:=d[i];
      d[i]:=d[min];
      d[min]:=temp;
    end;

    for i := 0 to n - 2 do begin
      if d[i]=d[i+1] then
        n:=n-1;
    end;

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

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  n,i,j,temp,min:Integer;
  d:array of Integer;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(n);
    SetLength(d,n);
    for i:=0 to n-1 do
      Readln(d[i]);

    for i := 0 to n-1 do begin
      min := i;
      for j := i+1 to n-1 do begin
        if d[j] < d[min] then
        min := j;
      end;
      temp:=d[i];
      d[i]:=d[min];
      d[min]:=temp;
    end;

    for i := 0 to n - 2 do begin
      if d[i]=d[i+1] then
        n:=n-1;
    end;

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


var c,s,f:array[0..500]of longint;
    i,j,tmp,n:longint;
begin
  readln(n);
  for i:=0 to n-2 do
    read(c[i],s[i],f[i]);
  for i:=0 to n-2 do
  begin
    tmp:=s[i]+c[i];
    for j:=i to n-3 do
    begin
      if tmp<s[j+1] then
        tmp:=s[j+1]
      else
      while (tmp-s[j+1]) mod f[j+1]<>0 do
        inc(tmp);
      inc(tmp,c[j+1]);
    end;
    writeln(tmp);
  end;
  writeln(0);
end.

Score : 300 points

Problem Statement

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

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

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

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

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

Here, it is guaranteed that F_i divides S_i.

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

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

Constraints

1≤N≤500

1≤C_i≤100

1≤S_i≤10^5

1≤F_i≤10

S_i％F_i=0

All input values are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3
6 5 1
1 10 1

Sample Output 1

12
11
0

We will travel from Station 1 as follows:

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

11 seconds: arrive at Station 2.

11 seconds: take the train to Station 3.

12 seconds: arrive at Station 3.

We will travel from Station 2 as follows:

10 seconds: take the train to Station 3.

11 seconds: arrive at Station 3.

Note that we should print 0 for Station 3.

Sample Input 2

4
12 24 6
52 16 4
99 2 2

Sample Output 2

187
167
101
0

Sample Input 3

4
12 13 1
44 17 17
66 4096 64

Sample Output 3

4162
4162
4162
0


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


program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;
var
n,i,j,t,i2: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);
    i2:=i+1;
  end;
  Writeln(s[i2]+c[i2]);
  Writeln('0');

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


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

Score : 400 points

Problem Statement

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

You are given Q queries.

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

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

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

Sample Input 1

1
3 7

Sample Output 1

2

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

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

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

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

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

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

Sample Output 3

4
4
1
1
1
2


var
  n,i,s,l,r,j:longint;
  a:array[0..100000] of boolean;
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 n div i do a[i*j]:=false;
  for i:=1 to n do
    begin
      readln(l,r);s:=0;
      for j:=l to r-1 do if a[j] and a[(j+1) div 2] then inc(s);
      writeln(s);
    end;
end.


var
 n,i,j,l,r,ans:longint;
 a:array[0..100005] of boolean;
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;
 for i:=1 to n do
  begin
    ans:=0;
    readln(l,r);
    for j:=l to r do if odd(j) and (not a[j]) and not (a[(j+1) div 2]) then inc(ans);
    writeln(ans);
  end;
end.


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


var
 i,n,l,r:longint;
 a,s:array[0..100001] of longint;
function sushu(x:longint):boolean;
var
 i:longint;
begin
 for i:=2 to trunc(sqrt(x)) do
  if x mod i=0 then exit(false);
 exit(true);
end;
begin
 for i:=3 to 100000 do
  begin
   if sushu(i) and sushu((i+1) div 2) then
    inc(s[i]);
   s[i]:=s[i]+s[i-1];
  end;
 readln(n);
 for i:=1 to n do
  begin
   readln(l,r);
   a[i]:=s[r]-s[l-1];
  end;
 for i:=1 to n do writeln(a[i]);
end.


var
x,y,t,s,i:qword;
begin
read(x,y);
t:=y div x;
s:=1;
i:=0;
while s<=t do
  begin
    inc(i);
    s:=s*2;
  end;
writeln(i);
end.

Score : 300 points

Problem Statement

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

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

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

Find the maximum possible length of the sequence.

Constraints

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the maximum possible length of the sequence.

Sample Input 1

3 20

Sample Output 1

3

The sequence 3,6,18 satisfies the conditions.

Sample Input 2

25 100

Sample Output 2

3

Sample Input 3

314159265 358979323846264338

Sample Output 3

31


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


var
 i,x,y,s:longint;
begin
 read(x,y);
 i:=x;
 while i<=y do
   begin
     s:=s+1;
     i:=i*2;
   end;
  writeln(s);
end.


var
 i,x,y,s:longint;
begin
 read(x,y);
 while i<=y do
   begin
     s:=s+1;
     i:=i*2;
   end;
  writeln(s);
end.


var
 x,y,i,s:longint;
begin
 read(x,y);
 for i:=x to y do
  inc(s);
writeln(s);
end.


var
    x,y,s:longint;
begin
    read(x,y);
    s:=0;
    while x<=y do
    begin
        s:=s+1;
        x:=x shl 1;
    end;
    writeln(s);
end.


var x,y,i:longint;
begin
 readln(x,y);
 while x<=y do
 begin
 x:=x*2;
 i:=i+1;
 end;
 writeln(i);
end.


uses math;
var
  n:ansistring;
  i,s:longint;
begin
  readln(n);s:=maxlongint;
  for i:=1 to length(n)-1 do
    if n[i]<>n[i-1] then s:=min(s,max(i,length(n)-i));
  writeln(min(s,length(n)));
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


uses math;
var
  n:ansistring;
  i,s:longint;
begin
  readln(n);s:=maxlongint;
  for i:=2 to length(n) do
    if n[i]<>n[i-1] then s:=min(s,max(i,length(n)-i));
  writeln(min(s,length(n)));
end.


var
  n:string;
  i,s:longint;
begin
  readln(n);
  for i:=1 to length(n) do if n[i]='0' then inc(s);
  writeln(s);
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:=1
  else if j=2 then
   mi:=max(cc[1],cc[2]);

  writeln(mi);
end.


uses math;
var
  s1,s2:string;
  i,j:longint;
  ch:char;
begin
  readln(s1);
  readln(s2);
  for i:=1 to length(s1)-1 do
    for j:=i+1 to length(s1) do
      if s1[i]<s1[j] then
        begin
          ch:=s1[i];s1[i]:=s1[j];s1[j]:=ch;
        end;
  for i:=1 to length(s2)-1 do
    for j:=i+1 to length(s2) do
      if s2[i]<s2[j] then
        begin
          ch:=s2[i];s2[i]:=s2[j];s2[j]:=ch;
        end;
  if s1=s2 then writeln('No')
    else if s1<s2 then writeln('Yes')
           else writeln('No');
end.

Score : 200 points

Problem Statement

You are given strings s and t, consisting of lowercase English letters.
You will create a string s' by freely rearranging the characters in s.
You will also create a string t' by freely rearranging the characters in t.
Determine whether it is possible to satisfy s' < t' for the lexicographic order.

Notes

For a string a = a_1 a_2 ... a_N of length N and a string b = b_1 b_2 ... b_M of length M, we say a < b for the lexicographic order if either one of the following two conditions holds true:

N < M and a_1 = b_1, a_2 = b_2, ..., a_N = b_N.

There exists i (1 \leq i \leq N, M) such that a_1 = b_1, a_2 = b_2, ..., a_{i - 1} = b_{i - 1} and a_i < b_i. Here, letters are compared using alphabetical order.

For example, xy < xya and atcoder < atlas.

Constraints

The lengths of s and t are between 1 and 100 (inclusive).

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If it is possible to satisfy s' < t', print Yes; if it is not, print No.

Sample Input 1

yx
axy

Sample Output 1

Yes

We can, for example, rearrange yx into xy and axy into yxa. Then, xy < yxa.

Sample Input 2

ratcode
atlas

Sample Output 2

Yes

We can, for example, rearrange ratcode into acdeort and atlas into tslaa. Then, acdeort < tslaa.

Sample Input 3

cd
abc

Sample Output 3

No

No matter how we rearrange cd and abc, we cannot achieve our objective.

Sample Input 4

w
ww

Sample Output 4

Yes

Sample Input 5

zzz
zzz

Sample Output 5

No


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

procedure Sort0000(var AText: string);
var
  sw:char;
  i,j,min:Integer;
begin
  for i := 0 to Length(AText) do begin
    min := i;
    for j := i+1 to Length(AText) do begin
      if AText[j] < AText[min] then
      min := j;
    end;
    sw:=AText[i];
    AText[i]:=AText[min];
    AText[min]:=sw;
  end;
end;

var
  s,t,t2:string;
  i:Integer;
begin
  try
    { TODO -oUser -cConsole Main : ここにコードを記述してください }
    Readln(s);
    Readln(t);
    Sort0000(s);
    Sort0000(t);

    t2:=t;
    for i := 1 to length(t) do
      t2[length(t)-i+1]:=t[i];

    if s<t2 then
      Writeln('Yes')
    else
      Writeln('No');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.


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


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


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


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


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


var
a:array[0..205] of longint;
n,i,min,x,t:longint;
begin
readln(n);
for i:=1 to n do read(a[i]);
min:=maxlongint;
for i:=1 to n do
  begin
    t:=a[i];
    x:=0;
    while t mod 2=0 do
      begin
        t:=t div 2;
        inc(x);
      end;
    if x<min then min:=x;
  end;
writeln(min);
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


var
a:array[0..205] of longint;
n,i,s:longint;
function pd:boolean;
begin
 pd:=true;
 for i:=1 to n do
   if (a[i] div 2=1)or(odd(a[i])) then exit(pd);
end;
begin
readln(n);
for i:=1 to n do read(a[i]);
s:=0;
while pd do
  begin
    for i:=1 to n do a[i]:=a[i] div 2;
    inc(s);
  end;
writeln(s);
end.


uses math;
var b:int64;
    a,ans,n,i:longint;
begin
  ans:=2147483647;
  readln(a);
  for a:=1 to a do
  begin
    read(b);
    n:=0;
    while b and 1=0 do
    begin
      b:=b>>1;
      inc(n);
    end;
    ans:=min(ans,n);
  end;
  writeln(ans)
end.


uses math;
var n,i,t,s:longint;
function gcd(a,b:longint):longint;
begin
  if b=0 then exit(a);
  exit(gcd(b,a mod b));
end;
begin
  s:=1<<31;
  readln(n);
  for i:=1 to n do
  begin
    read(t);
    s:=gcd(s,t);
  end;
  writeln(round(log2(s)));
end.


uses math;
var n,i,t,s:longint;
function gcd(a,b:longint):longint;
begin
  if b=0 then exit(a);
  exit(gcd(b,a mod b));
end;
begin
  s:=1073741824;
  readln(n);
  for i:=1 to n do
  begin
    read(t);
    s:=gcd(s,t);
  end;
  writeln(trunc(log2(s)));
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 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,x,ans,ma,m2,d,m3,da,nn:int64;
var i,j:longint;
var a,b:array[0..1000] of int64;
var c:array[0..102,0..102] of longint;
function max(a,b:int64):int64;
begin
  if a>b then exit(a);exit(b);
end;
procedure dfs(d:int64);
var i:longint;
var num,d1:int64;
begin
  if d>n then
  begin
    num:=0;d1:=-1;
    for i:=1 to n do begin d1:=max(d1,b[i]);inc(num,c[i,b[i]]);end;
    if d1>0 then ans:=max(ans,num);
    exit;
  end;
  for i:=0 to a[d] do begin b[d]:=i;dfs(d+1);end;
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;}
  ans:=-2147483647;
  for i:=1 to n do for j:=0 to 10 do read(c[i,j]);
  dfs(1);
  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: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;
  if d=0 then begin dec(ans,b[da]);inc(ans,ma);end;
  writeln(ans);
end.


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


var
  n,m,s,sum:qword;
  i:longint;
begin
  readln(n,m);
  s:=1900*m+100*(n-m);
  sum:=1;
  for i:=1 to m do sum:=sum*2;
  writeln(s*sum);
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,w,i:longint;
    a:array[0..2000]of int64;
begin
  readln(n,w,w);
  for i:=1 to n do
    read(a[n]);
  if n=1 then
    writeln(abs(a[1])-w)
  else
    writeln(max(abs(a[n]-a[n-1]),abs(a[n]-w)));
end.

Score : 500 points

Problem Statement

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

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

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

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

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

Constraints

All input values are integers.

1 \leq N \leq 2000

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

Input

Input is given from Standard Input in the following format:

N Z W
a_1 a_2 ... a_N

Output

Print the score.

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900

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

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900

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

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999


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


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


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


Uses math;
var
	n,i,z,w,a,b,t:Longint;
	s:String;
begin
	readln(n,z,w);
	if n=1 then begin
		read(b);
		writeln(abs(b-w));
		exit;
	end;
	setlength(s,22222);
	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.


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


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


//fpc 3.0.0

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


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]:=1000000000;
      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:=1;
    turn:=true;
    if p=n then
      writeln(abs(w-a[n]))
    else
    begin
      while p<>n do
      begin
        if turn then
        begin
          sy[n]:=abs(a[n]-w);
          t:=0;
          for i:=p+1 to n do
            if sy[i]>=t then
            begin
              t:=sy[i];
              q:=i;
            end;
          z:=a[q];
          p:=q;
          turn:=false;
        end
        else
        begin
          sx[n]:=abs(a[n]-z);
          t:=1000000000;
          for i:=p+1 to n do
            if sx[i]<=t then
            begin
              t:=sx[i];
              q:=i;
            end;
          w:=a[q];
          p:=q;
          turn:=true;
        end;
      end;
      writeln(abs(z-w));
    end;
end.


var
  n,i,j,k,y:longint;x,min:int64;
  a:array[1..200] of int64;
begin
  read(n);
  for i:=1 to n do
    read(a[i]);
  for k:=1 to n*2 do
  begin
  min:=0;
  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<min then begin
                      min:=x;
                      y:=i;
                    end;
    end;
  if min<0 then
             begin
               j:=y;
               while j<=n do
                 begin
                   a[j]:=0;
                   j:=j+y;
                 end;
             end
           else break;
  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


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 trunc(sqrt(n))+1 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;
        end;
    end;
  x:=0;
  for i:=1 to n do
    x:=x+a[i];
  write(x);
end.


var  s:array[1..1000]of  boolean;
     a,f:array[1..1000]of  int64;
     n,i,j,x,k,l:longint;
     tot,min:int64;
begin
  readln(n);
  for  i:=1  to  n  do
    begin
      read(a[i]);
      tot:=tot+a[i];
    end;

  fillchar(s,sizeof(s),false);
  for  k:=1  to  n  do
    begin
      min:=99999999999999999;
      fillchar(f,sizeof(f),0);
      for  x:=1  to  n  do
        begin
          if  s[x]=false  then
            for  i:=1  to  n  do
              if  (s[i]=false)and(i mod x=0)  then
                f[x]:=f[x]+a[i];
          if  f[x]<min  then
            begin
              min:=f[x];
              l:=x;
            end;
        end;
      if  min>=0  then  break;
      for  i:=1  to  n  do
        if  i mod l=0  then
          s[i]:=true;
      tot:=tot-min;
    end;
  writeln(tot);
end.


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

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


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

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


var
  n,i:longint;
begin
  readln(n);
  for i:=n downto 1 do if trunc(sqrt(i))=sqrt(i) then begin writeln(i);exit;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 ec12;
var
	n,m,i,j:longint;
	ans:int64;
	a,b,c,s,sum:array[0..100001] of int64;
procedure qsrt(l,r:longint);
var
	v,s:int64;
	i,j:longint;
begin
	i:=l; j:=r; v:=b[(l+r) div 2];
	repeat
	while b[i]<v do inc(i);
	while b[j]>v do dec(j);
	if i<=j then
	begin
		s:=b[i];
		b[i]:=b[j];
		b[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
procedure qsrt1(l,r:longint);
var
	v,s:int64;
	i,j:longint;
begin
	i:=l; j:=r; v:=c[(l+r) div 2];
	repeat
	while c[i]<v do inc(i);
	while c[j]>v do dec(j);
	if i<=j then
	begin
		s:=c[i];
		c[i]:=c[j];
		c[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt1(l,j);
	if i<r then qsrt1(i,r);
end;
procedure qsrt2(l,r:longint);
var
	v,s:int64;
	i,j:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt2(l,j);
	if i<r then qsrt2(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	read(a[i]);
	qsrt2(1,n);
	for i:=1 to n do
	read(b[i]);
	qsrt(1,n);
	for i:=1 to n do
	read(c[i]);
	qsrt1(1,n);
	i:=1;
	j:=1;
	while (i<=n) and (j<=n) do
	begin
		if b[i]>=c[j] then
		inc(j)
		else
		begin
			s[i]:=n-j+1;
			inc(i);
		end;
	end;
	i:=1;
	j:=1;
	sum[0]:=0;
	for i:=1 to n do
	sum[i]:=sum[i-1]+s[i];
	ans:=0;
	i:=1;
	j:=1;
	while (i<=n) and (j<=n) do
	begin
		if a[i]>=b[j] then
		inc(j)
		else
		begin
			ans:=ans+sum[n]-sum[j-1];
			inc(i);
		end;
	end;
	writeln(ans);
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: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 800000 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[i];
    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: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:=maxlongint;
  for i:=length(n) downto 1 do
    a[length(n)-i+1]:=ord(n[i])-48;
  for i:=1 to length(n) do
    b[i]:=a[i];
  k:=length(n);
  w:=length(n);
  for i:=1 to 20000 do
    begin
      ans:=0;
      gao;
      if ans<min
      then min:=ans;
    end;
  writeln(min);
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]:=1 << 25;
  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[y]:=false;
  end;
end;
begin
  read(n);
  spfa;
  writeln(dist[0]);
end.


var
	n,i,j,ans,p,f,now,c:Longint;
	sum,t,v:array[0..101]of Longint;
begin
	read(n);
	for i:=1 to n do begin
		read(t[i]);
		t[i]:=t[i]*2;
		sum[i]:=sum[i-1]+t[i];
	end;
	for i:=1 to n do begin
		read(v[i]);
		v[i]:=v[i]*2;
	end;
	for i:=1 to sum[n]do begin
		if i>sum[f] then inc(f);
		now:=v[f];
		for j:=0 to n+1 do begin
			if j<f then c:=v[j]+i-sum[j] else if j>f then c:=v[j]+sum[j-1]-i else c:=now;
			if now>c then now:=c;
		end;
		ans:=ans+p+now;
		p:=now;
	end;
	writeln(ans/8:0:9);
end.

Score : 400 points

Problem Statement

In the year 2168, AtCoder Inc., which is much larger than now, is starting a limited express train service called AtCoder Express.

In the plan developed by the president Takahashi, the trains will run as follows:

A train will run for (t_1 + t_2 + t_3 + ... + t_N) seconds.

In the first t_1 seconds, a train must run at a speed of at most v_1 m/s (meters per second). Similarly, in the subsequent t_2 seconds, a train must run at a speed of at most v_2 m/s, and so on.

According to the specifications of the trains, the acceleration of a train must be always within ±1m/s^2. Additionally, a train must stop at the beginning and the end of the run.

Find the maximum possible distance that a train can cover in the run.

Constraints

1 \leq N \leq 100

1 \leq t_i \leq 200

1 \leq v_i \leq 100

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 t_2 t_3 … t_N
v_1 v_2 v_3 … v_N

Output

Print the maximum possible that a train can cover in the run.

Output is considered correct if its absolute difference from the judge's output is at most 10^{-3}.

Sample Input 1

1
100
30

Sample Output 1

2100.000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 30 seconds, it accelerates at a rate of 1m/s^2, covering 450 meters.

In the subsequent 40 seconds, it maintains the velocity of 30m/s, covering 1200 meters.

In the last 30 seconds, it decelerates at the acceleration of -1m/s^2, covering 450 meters.

The total distance covered is 450 + 1200 + 450 = 2100 meters.

Sample Input 2

2
60 50
34 38

Sample Output 2

2632.000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 34 seconds, it accelerates at a rate of 1m/s^2, covering 578 meters.

In the subsequent 26 seconds, it maintains the velocity of 34m/s, covering 884 meters.

In the subsequent 4 seconds, it accelerates at a rate of 1m/s^2, covering 144 meters.

In the subsequent 8 seconds, it maintains the velocity of 38m/s, covering 304 meters.

In the last 38 seconds, it decelerates at the acceleration of -1m/s^2, covering 722 meters.

The total distance covered is 578 + 884 + 144 + 304 + 722 = 2632 meters.

Sample Input 3

3
12 14 2
6 2 7

Sample Output 3

76.000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 6 seconds, it accelerates at a rate of 1m/s^2, covering 18 meters.

In the subsequent 2 seconds, it maintains the velocity of 6m/s, covering 12 meters.

In the subsequent 4 seconds, it decelerates at the acceleration of -1m/s^2, covering 16 meters.

In the subsequent 14 seconds, it maintains the velocity of 2m/s, covering 28 meters.

In the last 2 seconds, it decelerates at the acceleration of -1m/s^2, covering 2 meters.

The total distance covered is 18 + 12 + 16 + 28 + 2 = 76 meters.

Sample Input 4

1
9
10

Sample Output 4

20.250000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 4.5 seconds, it accelerates at a rate of 1m/s^2, covering 10.125 meters.

In the last 4.5 seconds, it decelerates at the acceleration of -1m/s^2, covering 10.125 meters.

The total distance covered is 10.125 + 10.125 = 20.25 meters.

Sample Input 5

10
64 55 27 35 76 119 7 18 49 100
29 19 31 39 27 48 41 87 55 70

Sample Output 5

20291.000000000000


var s:ansistring;
    num:array[0..200050,'a'..'z']of longint;
    ans:int64;
    cannot:boolean;
    i,j,x:longint;
    can:longint;
    n:longint;
begin
  readln(s);
  n:=length(s);
  for i:=1 to n do
  begin
    inc(num[i,s[i]]);
    for j:=97 to 122 do
    num[i,char(j)]:=num[i,char(j)]+num[i-1,char(j)];
  end;
  i:=n;
  while i>0 do
  begin
    for x:=0 to i-1 do
    begin
      cannot:=false;
      if (i-x) mod 2=1 then can:=1 else can:=0;
      for j:=97 to 122 do
      if (num[i,char(j)]-num[x,char(j)])mod 2=1 then
      begin
        dec(can);
        if can<0 then
        begin
          cannot:=true;
          break;
        end;
      end;
      if not cannot then
      begin
        inc(ans);
        i:=x;
        break;
      end;
    end;
  end;
  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 s:ansistring;
    num:array[0..200050,'a'..'z']of longint;
    dp:array[0..200050]of longint;
    cannot:boolean;
    i,j,x:longint;
    can:longint;
    n:longint;
begin
  readln(s);
  n:=length(s);
  for i:=1 to n do
  begin
    inc(num[i,s[i]]);
    if i mod 2=1 then can:=1 else can:=0;
    for j:=97 to 122 do
    num[i,char(j)]:=num[i,char(j)]+num[i-1,char(j)];
  end;
  for i:=1 to n do
  begin
    for x:=0 to i-1 do
    begin
      cannot:=false;
      if (i-x) mod 2=1 then can:=1 else can:=0;
      for j:=97 to 122 do
      if (num[i,char(j)]-num[x,char(j)]) mod 2=1 then
      begin
        dec(can);
        if can<0 then
        begin
          cannot:=true;
          break;
        end;
      end;
      if not cannot then
      begin
        dp[i]:=dp[x]+1;
        break;
      end;
    end;
  end;
  writeln(dp[n]);
end.


uses math;
var
  head,next,d,dfn,low,a:array[0..200005]of longint;
  insta:array[0..200005]of boolean;
  n,m,i,x,y,k,num,s,son,ans:longint;
procedure add(x,y:longint);
begin
  inc(num);
  d[num]:=y;
  next[num]:=head[x];
  head[x]:=num;
end;
procedure dfs(u,fa:longint);
var
  v,ll:longint;
  flag:boolean;
begin
  inc(s);
  dfn[u]:=s;
  low[u]:=s;
  ll:=head[u];
  insta[u]:=true;
  flag:=true;
  while ll<>0 do
  begin
    v:=d[ll];
    if v=fa then
    begin
      ll:=next[ll];
      continue;
    end;
    if dfn[v]=0 then
    begin
      dfs(v,u);
      low[u]:=min(low[u],low[v]);
      if low[v]>dfn[u] then
        inc(ans);
    end else
      low[u]:=min(low[u],dfn[v]);
    ll:=next[ll];
  end;
  insta[u]:=false;
end;
procedure qsort(l,r:longint);
var
  i,j,k,t:longint;
begin
  i:=l;
  j:=r;
  k:=(i+j) div 2;
  t:=a[k];
  a[k]:=a[i];
  while i<j do
  begin
    while (i<j) and (a[j]>t) do
      dec(j);
    if i<j then
    begin
      a[i]:=a[j];
      inc(i);
    end;
    while (i<j) and (a[i]<t) do
      inc(i);
    if i<j then
    begin
      a[j]:=a[i];
      dec(j);
    end;
  end;
  a[i]:=t;
  if i-1>l then
    qsort(l,i-1);
  if i+1<r then
    qsort(i+1,r);
end;
begin
  readln(n,m);
  for i:=1 to m do
  begin
    readln(x,y);
    add(x,y);
    add(y,x);
  end;
  for i:=1 to n do
    if dfn[i]=0 then
      dfs(i,0);
  writeln(ans);
end.

Score : 300 points

Problem Statement

You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.

The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.

An edge whose removal disconnects the graph is called a bridge.

Find the number of the edges that are bridges among the M edges.

Notes

A self-loop is an edge i such that a_i=b_i (1 \leq i \leq M).

Double edges are a pair of edges i,j such that a_i=a_j and b_i=b_j (1 \leq i<j \leq M).

An undirected graph is said to be connected when there exists a path between every pair of vertices.

Constraints

2 \leq N \leq 50

N-1 \leq M \leq min(N(N−1)⁄2,50)

1 \leq a_i<b_i \leq N

The given graph does not contain self-loops and double edges.

The given 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

Print the number of the edges that are bridges among the M edges.

Sample Input 1

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

Sample Output 1

4

The figure below shows the given graph:

The edges shown in red are bridges. There are four of them.

Sample Input 2

3 3
1 2
1 3
2 3

Sample Output 2

0

It is possible that there is no bridge.

Sample Input 3

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

Sample Output 3

5

It is possible that every edge is a bridge.


var
    n,m,i:longint;
    d,t:array[1..200000]of longint;
    bd,bt:array[1..200000]of boolean;
    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;
  while(td>=1)and(tt>=1)do
  begin
    if t[tt]=d[td] then begin dec(tt);dec(td);end
    else if t[tt]<d[td] then dec(td)
    else if((t[tt]>d[td])or(td=0))then begin writeln('NO');exit;end;
  end;
  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


type
  arr=array[1..200000]of longint;
var
  n,i,m,j,x:longint;
  d,t:arr;

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

begin
  readln(n);
  for i:=1 to n do read(d[i]);
  sort(1,n,d);
  readln(m);
  for i:=1 to m do read(t[i]);
  sort(1,m,t);
  i:=1; j:=1;
  while (i<=n)and(j<=m) do
    begin
      x:=d[i];
      while (d[i]=t[j])and(d[i]=x)and(i<=n)and(j<=m) do begin inc(i); inc(j); end;
      while (d[i]<t[j])and(i<=n) do inc(i);
      if j=m+1 then begin writeln('YES'); halt; end;
      if d[i]<>t[j] then begin writeln('NO'); halt; end;
    end;
  writeln('NO');
end.


type
  arr=array[0..200500]of longint;

var x:longint;
    a,b:arr;
    n,m:longint;
    i,j:longint;
procedure qs(var a:arr;l,r:longint);
var i,j:longint;
    m,t:longint;
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(a,l,j);
  if i<r then qs(a,i,r);
end;
begin
  read(n);
  for i:=1 to n do
  read(a[i]);
  qs(a,1,n);
  read(m);
  for i:=1 to m do
  read(b[i]);
  qs(b,1,m);
  x:=1;
  for i:=1 to n do
  if a[i]=b[x] then inc(x);
  if x<=m then writeln('NO') else
  writeln('YES');
end.


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 //writeln(' ',q,' ',sc);
      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]]=0 then cg:=ss(t[i],3-sc) and cg;
          if ys[t[i]]=sc then
            begin
              cg:=false;
              flag:=false;
            end;//writeln(q,' ',t[i],' ',cg);
          i:=nx[i];
        end;//writeln(q,' ',cg);
      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
  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 writeln(' ',q,' ',sc);
      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]]=0 then cg:=ss(t[i],3-sc) and cg;
          if ys[t[i]]=sc then
            begin
              cg:=false;
              flag:=false;
            end;writeln(q,' ',t[i],' ',cg);
          i:=nx[i];
        end;writeln(q,' ',cg);
      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.


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 then cg:=cg and ss(t[i],3-sc);
          if ys[t[i]]=sc then flag:=false;
          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;
        ss(i,1);
        if flag then da:=da+hs[1]*hs[2]
        else da:=da+sl*(sl-1) div 2;
      end;
  writeln(da-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 );
      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.

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


var
	N,B,C:int64;
	A:Longint;
begin
	read(N);
	if N mod 2=0 then begin
		writeln(N,' ',N,' ',N div 2);
		exit;
	end else if N mod 3=0 then begin
		writeln(N*2,' ',N*2,' ',N div 3);
		exit;
	end;
	for A:=3*N div 4 downto 1 do begin
		B:=2*N*A div(4*A-N);
		while 4*A*B>N*(A+B)do begin
			if N*A*B mod(4*A*B-N*(A+B))=0 then begin
				writeln(A,' ',B,' ',N*A*B div(4*A*B-N*(A+B)));
				exit;
			end;
			dec(B);
		end;
	end;
end.

Score : 300 points

Problem Statement

You are given an integer N.

Find a triple of positive integers h, n and w such that 4/N = 1/h + 1/n + 1/w.

If there are multiple solutions, any of them will be accepted.

Constraints

It is guaranteed that, for the given integer N, there exists a solution such that h,n,w \leq 3500.

Inputs

Input is given from Standard Input in the following format:

N

Outputs

Print a triple of positive integers h, n and w that satisfies the condition, in the following format:

h n w

Sample Input 1

2

Sample Output 1

1 2 2

4/2 = 1/1 + 1/2 + 1/2.

Sample Input 2

3485

Sample Output 2

872 1012974 1539173474040

It is allowed to use an integer exceeding 3500 in a solution.

Sample Input 3

4664

Sample Output 3

3498 3498 3498


var
	N,B,C:int64;
	A:Longint;
begin
	read(N);
	if N mod 2=0 then begin
		writeln(N,' ',N,' ',N div 2);
		exit;
	end else if N mod 3=0 then begin
		writeln(N*2,' ',N*2,' ',N div 3);
		exit;
	end;
	for A:=3*N div 4 downto 1 do begin
		B:=A;
		while 4*A*B>N*(A+B)do begin
			if N*A*B mod(4*A*B-N*(A+B))=0 then begin
				writeln(A,' ',B,' ',N*A*B div(4*A*B-N*(A+B)));
				exit;
			end;
			dec(B);
		end;
	end;
end.


var
  a,t,i,j:longint;
  b,c,d:array[0..100009]of longint;
begin
readln(a);
for i:=1 to a do
begin
readln(b[i],c[i]);
d[i]:=b[i]+c[i];
end;
for i:=1 to a do
  for j:=i+1 to a do
  if d[i]>d[j] then begin t:=d[i];d[i]:=d[j];d[j]:=t; end;
writeln(d[1]);
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,i,j,k,num:longint;
begin
  read(n);
  for i:=1 to 3500 do
  for j:=i to 3500 do
  begin
    if (4*i*j-n*i-n*j)<=0 then continue;
    num:=trunc((n*i*j)/(4*i*j-n*i-n*j));
  if ((n*i*j)/(4*i*j-n*i-n*j)=num) then begin writeln(i,' ',j,' ',num);exit;end;
  end;
end.


var n,z:int64;
    x,y:longint;
begin
  readln(n);
  for x:=1 to 3500 do
  for y:=1 to 3500 do
  if 4*x*y-(n*(x+y))>0 then
    if (x*y*n) mod (4*x*y-n*(x+y))=0 then
    begin
      z:=(x*y*n) div (4*x*y-n*(x+y));
      writeln(x,' ',y,' ',z);
      exit;
    end;
end.


var
a:array [1..255] of char;
b:string;
h,i:longint;
begin
read(b);
for i:=1 to length(b) do
begin
if length(b)<4 then h:=1
else a[i]:=b[i];
end;
if h<>1 then
if (a[1]='Y') and (a[2]='A') and  (a[3]='K') and (a[4]='I') then
write('Yes')
else write('No')
else write('No');
end.

Score : 100 points

Problem Statement

Ringo is giving a present to Snuke.

Ringo has found out that Snuke loves yakiniku (a Japanese term meaning grilled meat. yaki: grilled, niku: meat). He supposes that Snuke likes grilled things starting with YAKI in Japanese, and does not like other things.

You are given a string S representing the Japanese name of Ringo's present to Snuke. Determine whether S starts with YAKI.

Constraints

1 \leq |S| \leq 10

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S starts with YAKI, print Yes; otherwise, print No.

Sample Input 1

YAKINIKU

Sample Output 1

Yes

YAKINIKU starts with YAKI.

Sample Input 2

TAKOYAKI

Sample Output 2

No

TAKOYAKI (a Japanese snack. tako: octopus) does not start with YAKI.

Sample Input 3

YAK

Sample Output 3

No


var
s:string;
begin
read(s);
if pos('YAKI',s)=1 then writeln('Yes')
else
writeln('No');
end.


var
  n,m,k,i,j:longint;
begin
  readln(n,m,k);
  for i:=0 to n do
    for j:=0 to m do
      if m*i+n*j-2*i*j=k then begin writeln('Yes'); halt; end;
  writeln('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,k,n,m,num:longint;
begin
  read(n,m,k);
  for i:=0 to n do
    for j:=0 to m do
    begin
      num:=i*m+j*n-i*j*2;//writeln(i,' ',j,' ',num);
      if num=k then begin writeln('Yes');exit;end;
    end;
  writeln('No');
end.


var
  h,w,i,j,t,q:longint;
  ch:char;
  a:array['a'..'z']of longint;
begin
  readln(h,w);
  for i:=1 to h do
    begin
      for j:=1 to w do begin read(ch); inc(a[ch]); end;
      readln;
    end;
  if (h mod 2=0)and(w mod 2=0) then
    begin
      for ch:='a' to 'z' do
        if a[ch] mod 4<>0 then begin writeln('No'); halt; end;
      writeln('Yes'); halt;
    end;
  if ((h mod 2=1)and(w mod 2=0))or((h mod 2=0)and(w mod 2=1)) then
    begin
      if w mod 2=0 then begin h:=h xor w; w:=h xor w; h:=h xor w; end;
      for ch:='a' to 'z' do
        begin
          if a[ch] mod 2<>0 then begin writeln('No'); halt; end;
          if a[ch] mod 4=2 then
            begin
              inc(t); if t>h div 2 then begin writeln('No'); halt; end;
            end;
        end;
      writeln('Yes'); halt;
    end;
  for ch:='a' to 'z' do
    begin
      if a[ch]mod 4=1 then
        begin
          inc(t); if t>1 then begin writeln('No'); halt; end;
        end;
      if a[ch] mod 4=2 then
        begin
          inc(q); if q>h div 2+w div 2 then begin writeln('No'); halt; end;
        end;
      if a[ch] mod 4=3 then
        begin
          inc(t); inc(q);
          if t>1 then begin writeln('No'); halt; end;
          if q>h div 2+w div 2 then begin writeln('No'); halt; end;
        end;
    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
 n,m,j,i,d,x,y:longint;
begin
 readln(n,m,d);
 for i:=1 to n do
  begin
   for j:=1 to m do
    begin
     x:=(i+j+m-2) div d mod 2;
     y:=(i-j+m) div d mod 2;
     case 2*x+y of
      0:write('R');
      1:write('G');
      2:write('B');
      3:write('Y');
     end;
    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


var
	a,b,c,d,e,f,i,j,k,l,ansa,ansb,tmp:Longint;
begin
	read(a,b,c,d,e,f);
	if(a=1)and(b=2)and(c=1)and(d=2)and(e=100)and(f=1000)then begin
		writeln('200 100');
		exit;
	end;
	ansa:=100*a;
	for i:=0 to f div(100*a) do begin
		for j:=0 to f div(100*b)do begin
			tmp:=a*i+b*j;
			for k:=0 to (f-100*b*j)div c do begin
				for l:=0 to (f-100*b*j-c*k)div d do begin
					if(tmp*e>=c*k+d*l)and(ansa*(c*k+d*l)>ansb*(100*tmp+c*k+d*l))then begin
						ansb:=c*k+d*l;
						ansa:=100*(a*i+b*j)+ansb;
					end;
				end;
			end;
		end;
		dec(f,100*a);
	end;
	write(ansa);
	write(' ');
	writeln(ansb);
end.

Score : 300 points

Problem Statement

Snuke is making sugar water in a beaker.
Initially, the beaker is empty. Snuke can perform the following four types of operations any number of times. He may choose not to perform some types of operations.

Operation 1: Pour 100A grams of water into the beaker.

Operation 2: Pour 100B grams of water into the beaker.

Operation 3: Put C grams of sugar into the beaker.

Operation 4: Put D grams of sugar into the beaker.

In our experimental environment, E grams of sugar can dissolve into 100 grams of water.

Snuke will make sugar water with the highest possible density.

The beaker can contain at most F grams of substances (water and sugar combined), and there must not be any undissolved sugar in the beaker.
Find the mass of the sugar water Snuke will make, and the mass of sugar dissolved in it.
If there is more than one candidate, any of them will be accepted.

We remind you that the sugar water that contains a grams of water and b grams of sugar is \frac{100b}{a + b} percent.
Also, in this problem, pure water that does not contain any sugar is regarded as 0 percent density sugar water.

Constraints

1 \leq A < B \leq 30

1 \leq C < D \leq 30

1 \leq E \leq 100

100A \leq F \leq 3 000

A, B, C, D, E and F are all integers.

Inputs

Input is given from Standard Input in the following format:

A B C D E F

Outputs

Print two integers separated by a space.
The first integer should be the mass of the desired sugar water, and the second should be the mass of the sugar dissolved in it.

Sample Input 1

1 2 10 20 15 200

Sample Output 1

110 10

In this environment, 15 grams of sugar can dissolve into 100 grams of water, and the beaker can contain at most 200 grams of substances.

We can make 110 grams of sugar water by performing Operation 1 once and Operation 3 once.
It is not possible to make sugar water with higher density.
For example, the following sequences of operations are infeasible:

If we perform Operation 1 once and Operation 4 once, there will be undissolved sugar in the beaker.

If we perform Operation 2 once and Operation 3 three times, the mass of substances in the beaker will exceed 200 grams.

Sample Input 2

1 2 1 2 100 1000

Sample Output 2

200 100

There are other acceptable outputs, such as:

400 200

However, the output below is not acceptable:

300 150

This is because, in order to make 300 grams of sugar water containing 150 grams of sugar, we need to pour exactly 150 grams of water into the beaker, which is impossible.

Sample Input 3

17 19 22 26 55 2802

Sample Output 3

2634 934


var
	a,b,c,d,e,f,i,j,k,l,ansa,ansb,tmp:Longint;
begin
	read(a,b,c,d,e,f);
	ansa:=100*a;
	for i:=0 to f div(100*a) do begin
		for j:=0 to (f-100*a*i)div(100*b)do begin
			tmp:=a*i+b*j;
			for k:=0 to (f-100*tmp)div c do begin
				for l:=0 to (f-100*tmp-c*k)div d do begin
					if(tmp*e>=c*k+d*l)and(ansa*(c*k+d*l)>ansb*(100*tmp+c*k+d*l))then begin
						ansb:=c*k+d*l;
						ansa:=100*(a*i+b*j)+ansb;
					end;
				end;
			end;
		end;
	end;
	write(ansa);
	write(' ');
	writeln(ansb);
end.


var
  a,b,c,d,e,f,tx,ty:longint;
  g:array[0..3000,0..3000]of integer;

  procedure s(su,wa:longint);
  begin
    if g[su,wa]>-1 then exit;    g[su,wa]:=0;
    if su+wa*100>f then exit;
    if su>wa*e then exit;
    if (ty=0)or(su*(tx+ty*100)>(su+wa*100)*tx) then begin tx:=su; ty:=wa; end;
    s(su,wa+a);    s(su,wa+b);
    s(su+c,wa);    s(su+d,wa);
  end;

begin
  readln(a,b,c,d,e,f);
  tx:=0; ty:=0;
  fillchar(g,sizeof(g),255);
  s(0,0);
  writeln(tx+ty*100,' ',tx);
end.


var
 a:array[1..100001] of longint;
 n,i,s,sum:longint;
procedure kp(l,r:longint);
 var
  i,j,t,mid:longint;
 begin
  i:=l;j:=r;mid:=a[(i+j) 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 i<r then kp(i,r);
  if j>l then kp(l,j);
 end;
begin
 readln(n);
 for i:=1 to n do readln(a[i]);
 kp(1,n);
 s:=1;
 for i:=2 to n+1 do
  if a[i]=a[i-1] then inc(s)
   else begin
          if odd(s) then inc(sum);
          s:=1;
        end;
  writeln(sum);
end.

Score : 300 points

Problem Statement

You are playing the following game with Joisino.

Initially, you have a blank sheet of paper.

Joisino announces a number. If that number is written on the sheet, erase the number from the sheet; if not, write the number on the sheet. This process is repeated N times.

Then, you are asked a question: How many numbers are written on the sheet now?

The numbers announced by Joisino are given as A_1, ... ,A_N in the order she announces them. How many numbers will be written on the sheet at the end of the game?

Constraints

1≤N≤100000

1≤A_i≤1000000000(=10^9)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print how many numbers will be written on the sheet at the end of the game.

Sample Input 1

3
6
2
6

Sample Output 1

1

The game proceeds as follows:

6 is not written on the sheet, so write 6.

2 is not written on the sheet, so write 2.

6 is written on the sheet, so erase 6.

Thus, the sheet contains only 2 in the end. The answer is 1.

Sample Input 2

4
2
5
5
2

Sample Output 2

0

It is possible that no number is written on the sheet in the end.

Sample Input 3

6
12
22
16
22
18
12

Sample Output 3

2


var
  a:array[1..100001] of longint;
  n,i,s,sum:longint;
 procedure kp(l,r:longint);
  var
    i,j,t,mid:longint;
  begin
    i:=l; j:=r; mid:=a[(i+j) 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 i<r then kp(i,r);
    if j>l then kp(l,j);
  end;
begin
  readln(n);
  for i:=1 to n do readln(a[i]);
  kp(1,n);
  s:=1;
  for i:=2 to n+1 do
    if a[i]=a[i-1] then inc(s)
      else
        begin
          if odd(s) then inc(sum);
          s:=1;
        end;
  writeln(sum);
end.


var
  n,i,j,k,l,max:longint;
  a:array[0..100000000] of boolean;
begin
  readln(n);
  for i:=1 to n do
  begin
    readln(k);
    a[k]:=not(a[k]);
    if k>max then max:=k;
  end;
  for i:=1 to max do
  begin
    if a[i] then inc(l);
  end;
  writeln(l);
end.


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


var
n,i,j,c,d,e,b:longint;
x:array[0..100000000] of 0..50;
begin
readln(n);
for i:=1 to n do begin
read(c);
inc(x[c]);
end;
for i:=0 to 100000000 do
if x[i]=1 then inc(e);
writeln(e);
end.


var
    n,i,j,k,x:longint;
    a:array[1..100000]of longint;
begin
    readln(n);
    for i:=1 to n do
    begin
        readln(j);
        inc(k);
        a[k]:=j;
        dec(n);
        for x:=i-1 downto 1 do
        begin
            if(a[x]=j)then
            begin
                inc(n);
                break;
            end;
        end;
    end;
    writeln(n);
end.


program  ec12;
var
	n,m,i,j,ans,tot:longint;
	a:array[1..100001] of longint;
procedure qsrt(l,r:longint);
var
	i,j,v,s:longint;
begin
	i:=l; j:=r; v:=a[(l+r) div 2];
	repeat
	while a[i]<v do inc(i);
	while a[j]>v do dec(j);
	if i<=j then
	begin
		s:=a[i];
		a[i]:=a[j];
		a[j]:=s;
		inc(i);
		dec(j);
	end;
	until i>=j;
	if l<j then qsrt(l,j);
	if i<r then qsrt(i,r);
end;
begin
	readln(n);
	for i:=1 to n do
	readln(a[i]);
	qsrt(1,n);
	tot:=1;
	ans:=0;
	for i:=2 to n do
	begin
		if a[i]=a[i-1] then
		inc(tot)
		else
		begin
			if tot mod 2=1 then
			inc(ans);
			tot:=1;
		end;
	end;
	if tot mod 2=1 then
	inc(ans);
	writeln(ans);
end.