var
	Q,i,j:Longint;
	T:Array[0..30]of int64;
	a,b,c,d,ans,plus,tmp:int64;
begin
	T[0]:=1;
	for i:=1 to 30 do T[i]:=T[i-1]*3;
	read(Q);
	for i:=1 to Q do begin
		read(a,b,c,d);
		dec(a);
		dec(b);
		dec(c);
		dec(d);
		ans:=abs(a-c)+abs(b-d);
		plus:=0;
		for j:=30 downto 1 do begin
			if(a div T[j-1]=c div T[j-1])and(a div T[j-1]mod 3=1)then begin
				b:=b div T[j-1];
				d:=d div T[j-1];
				if b>d then begin
					tmp:=b;
					b:=d;
					d:=tmp;
				end;
				while b mod 3<>1 do inc(b);
				while(d>=0)and(d mod 3<>1)do dec(d);
				if b<=d then begin
					a:=a mod T[j];
					c:=c mod T[j];
					if a>c then begin
						tmp:=a;
						a:=c;
						c:=tmp;
					end;
					if a+c+1<3*T[j-1]then plus:=a+1-T[j-1]else plus:=2*T[j-1]-c;
					break;
				end;
			end;
			if(b div T[j-1]=d div T[j-1])and(b div T[j-1]mod 3=1)then begin
				a:=a div T[j-1];
				c:=c div T[j-1];
				if a>c then begin
					tmp:=a;
					a:=c;
					c:=tmp;
				end;
				while a mod 3<>1 do inc(a);
				while(c>=0)and(c mod 3<>1)do dec(c);
				if a<=c then begin
					b:=b mod T[j];
					d:=d mod T[j];
					if b>d then begin
						tmp:=b;
						b:=d;
						d:=tmp;
					end;
					if b+d+1<3*T[j-1]then plus:=b+1-T[j-1]else plus:=2*T[j-1]-d;
					break;
				end;
			end;
		end;
		writeln(ans+plus*2);
	end;
end.

Score : 600 points

Problem Statement

For a non-negative integer K, we define a fractal of level K as follows:

A fractal of level 0 is a grid with just one white square.

When K > 0, a fractal of level K is a 3^K \times 3^K grid. If we divide this grid into nine 3^{K-1} \times 3^{K-1} subgrids:

The central subgrid consists of only black squares.

Each of the other eight subgrids is a fractal of level K-1.

For example, a fractal of level 2 is as follows:

In a fractal of level 30, let (r, c) denote the square at the r-th row from the top and the c-th column from the left.

You are given Q quadruples of integers (a_i, b_i, c_i, d_i).
For each quadruple, find the distance from (a_i, b_i) to (c_i, d_i).

Here the distance from (a, b) to (c, d) is the minimum integer n that satisfies the following condition:

There exists a sequence of white squares (x_0, y_0), \ldots, (x_n, y_n) satisfying the following conditions:

(x_0, y_0) = (a, b)

(x_n, y_n) = (c, d)

For every i (0 \leq i \leq n-1), (x_i, y_i) and (x_{i+1}, y_{i+1}) share a side.

Constraints

1 \leq Q \leq 10000

1 \leq a_i, b_i, c_i, d_i \leq 3^{30}

(a_i, b_i) \neq (c_i, d_i)

(a_i, b_i) and (c_i, d_i) are white squares.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
a_1 \ b_1 \ c_1 \ d_1
:
a_Q \ b_Q \ c_Q \ d_Q

Output

Print Q lines.
The i-th line should contain the distance from (a_i, b_i) to (c_i, d_i).

Sample Input 1

2
4 2 7 4
9 9 1 9

Sample Output 1

5
8


var
	Q,i,j:Longint;
	T:Array[0..30]of int64;
	a,b,c,d,ans,plus,tmp:int64;
begin
	T[0]:=1;
	for i:=1 to 30 do T[i]:=T[i-1]*3;
	read(Q);
	for i:=1 to Q do begin
		read(a,b,c,d);
		dec(a);
		dec(b);
		dec(c);
		dec(d);
		ans:=abs(a-c)+abs(b-d);
		plus:=0;
		for j:=30 downto 1 do begin
			if(a div T[j-1]=c div T[j-1])and(a div T[j-1]mod 3=1)then begin
				b:=b div T[j-1];
				d:=d div T[j-1];
				if b>d then begin
					tmp:=b;
					b:=d;
					d:=tmp;
				end;
				while b mod 3<>1 do inc(b);
				if b<=d then begin
					a:=a mod T[j];
					c:=c mod T[j];
					if a>c then begin
						tmp:=a;
						a:=c;
						c:=tmp;
					end;
					if a+c+1<3*T[j-1]then plus:=a+1-T[j-1]else plus:=2*T[j-1]-c;
					break;
				end;
			end;
			if(b div T[j-1]=d div T[j-1])and(b div T[j-1]mod 3=1)then begin
				a:=a div T[j-1];
				c:=c div T[j-1];
				if a>c then begin
					tmp:=a;
					a:=c;
					c:=tmp;
				end;
				while a mod 3<>1 do inc(a);
				if a<=c then begin
					b:=b mod T[j];
					d:=d mod T[j];
					if b>d then begin
						tmp:=b;
						b:=d;
						d:=tmp;
					end;
					if b+d+1<3*T[j-1]then plus:=b+1-T[j-1]else plus:=2*T[j-1]-d;
					break;
				end;
			end;
		end;
		writeln(ans+plus*2);
	end;
end.

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

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.


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

uses math;

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

const
  inf = 1061109567;
  lnf = 4557430888798830399;
  Maxn = 200005;
  LOG = 32;

var
  i, j: int32;
  swap_tmp, x, y, z: int64;
  n, T, sza, szb, ans: int64;
  a: array [0 .. Maxn, 0 .. 1] of int64;
  b: array [0 .. Maxn] of int64;
  dp: array [0 .. LOG] of int64;

function qsort_less(i, j: int32): bool;
begin
  qsort_less := (a[j][0] * (a[i][1] + 1) < (a[i][0] * (a[j][1] + 1)));
end;
procedure qsort_swap(i, j: int32);
begin
  a[0] := a[i];
  a[i] := a[j];
  a[j] := a[0];
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;

function qsort2_less(i, j: int32): bool;
begin
  qsort2_less := (b[i] < b[j]);
end;
procedure qsort2_swap(i, j: int32);
begin
  b[0] := b[i];
  b[i] := b[j];
  b[j] := b[0];
end;
procedure qsort2(_l, _r: int32);
  var i, j, _p: int32;
begin
  if (_r = _l) then exit;
  i := _l; j := _r;
  _p := _l;
  repeat
    while qsort2_less(i, _p) do inc(i);
    while qsort2_less(_p, j) do dec(j);
    if (i <= j) then begin
      qsort2_swap(i, j);
      inc(i); dec(j);
    end;
  until (i > j);
  if (i < _r) then qsort2(i, _r);
  if (j > _l) then qsort2(_l, j);
end;

begin
//  assign(input,'in'); reset(input);
//  assign(output,'out'); rewrite(output);
  randomize;

  readln(n, T);
  sza := 0; szb := 0;
  for i := 1 to n do begin
    readln(x, y);
    if (x = 0) then begin
      inc(szb);
      b[szb] := y;
    end
    else begin
      inc(sza);
      a[sza, 0] := x;
      a[sza, 1] := y;
    end;
  end;

  for i := 2 to sza do begin
    x := random(i - 1) + 1;
    a[0] := a[x];
    a[x] := a[i];
    a[i] := a[0];
  end;
  qsort(1, sza);

  for i := 1 to LOG do dp[i] := T + 1;
  dp[0] := 0;
  for i := 1 to sza do begin
    for j := LOG - 1 downto 0 do begin
      if (dp[j] > T) then continue;
      if (dp[j + 1] > dp[j] + 1 + (dp[j] + 1) * a[i][0] + a[i][1]) then begin
        dp[j + 1] := dp[j] + 1 + (dp[j] + 1) * a[i][0] + a[i][1];
      end;
    end;
  end;

  for i := 2 to szb do begin
    x := random(i - 1) + 1;
    b[0] := b[x];
    b[x] := b[i];
    b[i] := b[0];
  end;
  qsort2(1, szb);
  ans := 0;
  for i := 0 to LOG do begin
    if (dp[i] > T) then continue;
    x := T - dp[i];
    y := 0;
    for j := 1 to szb do begin
      if (x > b[j]) then begin
        dec(x, b[j] + 1);
        inc(y);
      end
      else break;
    end;
    if (i + y > ans) then ans := i + y;
  end;

  writeln(ans);

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

Score : 800 points

Problem Statement

There are N stores called Store 1, Store 2, \cdots, Store N. Takahashi, who is at his house at time 0, is planning to visit some of these stores.

It takes Takahashi one unit of time to travel from his house to one of the stores, or between any two stores.

If Takahashi reaches Store i at time t, he can do shopping there after standing in a queue for a_i \times t + b_i units of time. (We assume that it takes no time other than waiting.)

All the stores close at time T + 0.5. If Takahashi is standing in a queue for some store then, he cannot do shopping there.

Takahashi does not do shopping more than once in the same store.

Find the maximum number of times he can do shopping before time T + 0.5.

Constraints

All values in input are integers.

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

0 \leq a_i \leq 10^9

0 \leq b_i \leq 10^9

0 \leq T \leq 10^9

Input

Input is given from Standard Input in the following format:

N T
a_1 b_1
a_2 b_2
\vdots
a_N b_N

Output

Print the answer.

Sample Input 1

3 7
2 0
3 2
0 3

Sample Output 1

2

Here is one possible way to visit stores:

From time 0 to time 1: in 1 unit of time, he travels from his house to Store 1.

From time 1 to time 3: for 2 units of time, he stands in a queue for Store 1 to do shopping.

From time 3 to time 4: in 1 unit of time, he travels from Store 1 to Store 3.

From time 4 to time 7: for 3 units of time, he stands in a queue for Store 3 to do shopping.

In this way, he can do shopping twice before time 7.5.

Sample Input 2

1 3
0 3

Sample Output 2

0

Sample Input 3

5 21600
2 14
3 22
1 3
1 10
1 9

Sample Output 3

5

Sample Input 4

7 57
0 25
3 10
2 4
5 15
3 22
2 14
1 15

Sample Output 4

3


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

uses math;

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

const
  inf = 1061109567;
  lnf = 4557430888798830399;
  Maxn = 200005;
  LOG = 32;

var
  i, j, swap_tmp, x, y, z: int32;
  n, T, sza, szb, ans: int64;
  a: array [0 .. Maxn, 0 .. 1] of int64;
  b: array [0 .. Maxn] of int64;
  dp: array [0 .. LOG] of int64;

function qsort_less(i, j: int32): bool;
begin
  qsort_less := (a[j][0] * (a[i][1] + 1) < (a[i][0] * (a[j][1] + 1)));
end;
procedure qsort_swap(i, j: int32);
begin
  a[0] := a[i];
  a[i] := a[j];
  a[j] := a[0];
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;

function qsort2_less(i, j: int32): bool;
begin
  qsort2_less := (b[i] < b[j]);
end;
procedure qsort2_swap(i, j: int32);
begin
  b[0] := b[i];
  b[i] := b[j];
  b[j] := b[0];
end;
procedure qsort2(_l, _r: int32);
  var i, j, _p: int32;
begin
  if (_r = _l) then exit;
  i := _l; j := _r;
  _p := _l;
  repeat
    while qsort2_less(i, _p) do inc(i);
    while qsort2_less(_p, j) do dec(j);
    if (i <= j) then begin
      qsort2_swap(i, j);
      inc(i); dec(j);
    end;
  until (i > j);
  if (i < _r) then qsort2(i, _r);
  if (j > _l) then qsort2(_l, j);
end;

begin
//  assign(input,'in'); reset(input);
//  assign(output,'out'); rewrite(output);
  randomize;

  readln(n, T);
  sza := 0; szb := 0;
  for i := 1 to n do begin
    readln(x, y);
    if (x = 0) then begin
      inc(szb);
      b[szb] := y;
    end
    else begin
      inc(sza);
      a[sza, 0] := x;
      a[sza, 1] := y;
    end;
  end;

  for i := 2 to sza do begin
    x := random(i - 1) + 1;
    a[0] := a[x];
    a[x] := a[i];
    a[i] := a[0];
  end;
  qsort(1, sza);

  for i := 1 to LOG do dp[i] := T + 1;
  dp[0] := 0;
  for i := 1 to sza do begin
    for j := LOG - 1 downto 0 do begin
      if (dp[j] > T) then continue;
      if (dp[j + 1] > dp[j] + 1 + (dp[j] + 1) * a[i][0] + a[i][1]) then begin
        dp[j + 1] := dp[j] + 1 + (dp[j] + 1) * a[i][0] + a[i][1];
      end;
    end;
  end;

  for i := 2 to n do begin
    x := random(i - 1) + 1;
    b[0] := b[x];
    b[x] := b[i];
    b[i] := b[0];
  end;
  qsort2(1, szb);
  ans := 0;
  for i := 0 to LOG do begin
    if (dp[i] > T) then continue;
    x := T - dp[i];
    y := 0;
    for j := 1 to szb do begin
      if (x > b[j]) then begin
        dec(x, b[j] + 1);
        inc(y);
      end
      else break;
    end;
    if (i + y > ans) then ans := i + y;
  end;

  writeln(ans);

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


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

uses math;

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

const
  inf = 1061109567;
  lnf = 4557430888798830399;
  Maxn = 200005;
  LOG = 32;

var
  i, j, swap_tmp, x, y, z: int32;
  n, T, sza, szb, ans: int32;
  a: array [0 .. Maxn, 0 .. 1] of int32;
  b: array [0 .. Maxn] of int32;
  dp: array [0 .. LOG] of int32;

function qsort_less(i, j: int32): bool;
begin
  qsort_less := (a[j][0] * (a[i][1] + 1) < (a[i][0] * (a[j][1] + 1)));
end;
procedure qsort_swap(i, j: int32);
begin
  a[0] := a[i];
  a[i] := a[j];
  a[j] := a[0];
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;

function qsort2_less(i, j: int32): bool;
begin
  qsort2_less := (b[i] < b[j]);
end;
procedure qsort2_swap(i, j: int32);
begin
  b[0] := b[i];
  b[i] := b[j];
  b[j] := b[0];
end;
procedure qsort2(_l, _r: int32);
  var i, j, _p: int32;
begin
  if (_r = _l) then exit;
  i := _l; j := _r;
  _p := _l;
  repeat
    while qsort2_less(i, _p) do inc(i);
    while qsort2_less(_p, j) do dec(j);
    if (i <= j) then begin
      qsort2_swap(i, j);
      inc(i); dec(j);
    end;
  until (i > j);
  if (i < _r) then qsort2(i, _r);
  if (j > _l) then qsort2(_l, j);
end;

begin
//  assign(input,'in'); reset(input);
//  assign(output,'out'); rewrite(output);
  randomize;

  readln(n, T);
  sza := 0; szb := 0;
  for i := 1 to n do begin
    readln(x, y);
    if (x = 0) then begin
      inc(szb);
      b[szb] := y;
    end
    else begin
      inc(sza);
      a[sza, 0] := x;
      a[sza, 1] := y;
    end;
  end;

  for i := 2 to sza do begin
    x := random(i - 1) + 1;
    a[0] := a[x];
    a[x] := a[i];
    a[i] := a[0];
  end;
  qsort(1, sza);

  for i := 1 to LOG do dp[i] := T + 1;
  dp[0] := 0;
  for i := 1 to sza do begin
    for j := LOG - 1 downto 0 do begin
      if (dp[j] > T) then continue;
      if (dp[j + 1] > dp[j] + 1 + (dp[j] + 1) * a[i][0] + a[i][1]) then begin
        dp[j + 1] := dp[j] + 1 + (dp[j] + 1) * a[i][0] + a[i][1];
      end;
    end;
  end;

  for i := 2 to n do begin
    x := random(i - 1) + 1;
    b[0] := b[x];
    b[x] := b[i];
    b[i] := b[0];
  end;
  qsort2(1, szb);
  ans := 0;
  for i := 0 to LOG do begin
    if (dp[i] > T) then continue;
    x := T - dp[i];
    y := 0;
    for j := 1 to szb do begin
      if (x > b[j]) then begin
        dec(x, b[j] + 1);
        inc(y);
      end
      else break;
    end;
    if (i + y > ans) then ans := i + y;
  end;

  writeln(ans);

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


var s:ansistring;
    ch:string;
    i, T, opt, tp, rev: longint;
begin
  readln(s); readln(T);
  for i:= 1 to T do
    begin
      read(tp);
      if tp = 1 then rev := 1 - rev
      else begin
        read(opt); read(ch);
        delete(ch, 1, 1);
        if rev = 1 then opt := 3 - opt;
        if (opt = 1) then s := ch + s
        else s := s + ch;
      end;
    end;
    if (rev = 1) then
      begin
        for i:= length(s) downto 1 do write(s[i]);
        writeln;
      end else
      begin for i:= 1 to length(s) do write(s[i]);
        writeln;end;
end.

Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy


type BITDataType=Longint;
type BinaryIndexedTree=record n:Longint;bit:Array of BITDataType;end;
procedure init(var bit:BinaryIndexedTree;N:Longint);
var i:Longint;
begin
	bit.n:=N;
	setlength(bit.bit,N);
	for i:=1 to N do bit.bit[i]:=0;
end;
function sum(var bit:BinaryIndexedTree;i:Longint):BITDataType;
var ans:BITDataType;
begin
	ans:=0;
	while i>0 do begin
		inc(ans,bit.bit[i]);
		dec(i,i and-i);
	end;
	sum:=ans;
end;
procedure add(var bit:BinaryIndexedTree;i:Longint;a:BITDataType);
begin
	while i<=bit.n do begin
		inc(bit.bit[i],a);
		inc(i,i and-i);
	end;
end;

var
	N,Q,i,qt,id,l,r,cnt:Longint;
	S:String;
	c:Char;
	bit:Array['a'..'z']of BinaryIndexedTree;
begin
	readln(N);
	readln(S);
	readln(Q);
	for c:='a' to 'z' do init(bit[c],N);
	for i:=1 to N do add(bit[S[i]],i,1);
	for i:=1 to Q do begin
		read(qt);
		if qt=1 then begin
			read(id);
			read(c);
			read(c);
			add(bit[S[id]],id,-1);
			S[id]:=c;
			add(bit[S[id]],id,1);
		end else begin
			read(l,r);
			cnt:=0;
			for c:='a' to 'z' do if sum(bit[c],r)>sum(bit[c],l-1) then inc(cnt);
			writeln(cnt);
		end;
	end;
end.

Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.)

Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive).

Constraints

N, Q, i_q, l_q, and r_q are integers.

S is a string consisting of lowercase English letters.

c_q is a lowercase English letter.

1 \leq N \leq 500000

1 \leq Q \leq 20000

|S| = N

1 \leq i_q \leq N

1 \leq l_q \leq r_q \leq N

There is at least one query of type 2 in each testcase.

Input

Input is given from Standard Input in the following format:

N
S
Q
Query_1
\vdots
Query_Q

Here, Query_i in the 4-th through (Q+3)-th lines is one of the following:

1 i_q c_q

2 l_q r_q

Output

For each query of type 2, print a line containing the answer.

Sample Input 1

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Sample Output 1

3
1
5

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.


uses Math;
var
	N,K,i,j:Longint;
	x,y,c:Array[1..60]of Longint;
	rr:Array[1..60]of double;
	L,R,M,a,t,d,ri,rj,yy:double;
	flag:Boolean;
function count(tx,ty:double):Boolean;
var i,cnt:Longint;
begin
	cnt:=0;
	for i:=1 to N do if sqr(X[i]-tx)+sqr(Y[i]-ty)<sqr(rr[i])+0.000000001 then inc(cnt);
	count:=cnt>=K;
end;
begin
	read(N,K);
	for i:=1 to N do read(x[i],y[i],c[i]);
	L:=0;
	R:=200000;
	while R-L>0.000001 do begin
		M:=(L+R)/2;
		for i:=1 to N do rr[i]:=M/c[i];
		flag:=False;
		for i:=1 to N do begin
			if count(X[i],Y[i])then begin
				flag:=True;
				break;
			end;
		end;
		if not flag then begin
			for i:=2 to N do begin
				for j:=1 to i-1 do begin
					d:=sqr(X[i]-X[j])+sqr(Y[i]-Y[j]);
					ri:=rr[i];
					rj:=rr[j];
					yy:=4*sqr(ri)*d-sqr(sqr(ri)+d-sqr(rj));
					if yy<0 then continue;
					a:=arctan2(sqrt(yy),sqr(ri)+d-sqr(rj));
					t:=arctan2(Y[j]-Y[i],X[j]-X[i]);
					if count(X[i]+ri*cos(t+a),Y[i]+ri*sin(t+a)) then flag:=True
					else if count(X[i]+ri*cos(t-a),Y[i]+ri*sin(t-a))then flag:=True;
					if flag then break;
				end;
				if flag then break;
			end;
		end;
		if flag then R:=M else L:=M;
	end;
	writeln(R:0:9);
end.

Score : 600 points

Problem Statement

Takahashi wants to grill N pieces of meat on a grilling net, which can be seen as a two-dimensional plane. The coordinates of the i-th piece of meat are \left(x_i, y_i\right), and its hardness is c_i.

Takahashi can use one heat source to grill the meat. If he puts the heat source at coordinates \left(X, Y\right), where X and Y are real numbers, the i-th piece of meat will be ready to eat in c_i \times \sqrt{\left(X - x_i\right)^2 + \left(Y-y_i\right)^2} seconds.

Takahashi wants to eat K pieces of meat. Find the time required to have K or more pieces of meat ready if he put the heat source to minimize this time.

Constraints

All values in input are integers.

1 \leq N \leq 60

1 \leq K \leq N

-1000 \leq x_i , y_i \leq 1000

\left(x_i, y_i\right) \neq \left(x_j, y_j\right) \left(i \neq j \right)

1 \leq c_i \leq 100

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
\vdots
x_N y_N c_N

Output

Print the answer.

It will be considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

4 3
-1 0 3
0 0 3
1 0 2
1 1 40

Sample Output 1

2.4

If we put the heat source at \left(-0.2, 0\right), the 1-st, 2-nd, and 3-rd pieces of meat will be ready to eat within 2.4 seconds. This is the optimal place to put the heat source.

Sample Input 2

10 5
-879 981 26
890 -406 81
512 859 97
362 -955 25
128 553 17
-885 763 2
449 310 57
-656 -204 11
-270 76 40
184 170 16

Sample Output 2

7411.2252


uses Math;
var
	N,K,i,j:Longint;
	x,y,c:Array[1..60]of Longint;
	rr:Array[1..60]of double;
	L,R,M,a,t,d,ri,rj,yy:double;
	flag:Boolean;
function count(tx,ty:double):Boolean;
var i,cnt:Longint;
begin
	cnt:=0;
	for i:=1 to N do if sqr(X[i]-tx)+sqr(Y[i]-ty)<sqr(rr[i])+0.000000001 then inc(cnt);
	count:=cnt>=K;
end;
begin
	read(N,K);
	for i:=1 to N do read(x[i],y[i],c[i]);
	L:=0;
	R:=1000000;
	while R-L>0.000000001 do begin
		M:=(L+R)/2;
		for i:=1 to N do rr[i]:=M/c[i];
		flag:=False;
		for i:=1 to N do begin
			if count(X[i],Y[i])then begin
				flag:=True;
				break;
			end;
		end;
		if not flag then begin
			for i:=2 to N do begin
				for j:=1 to i-1 do begin
					d:=sqr(X[i]-X[j])+sqr(Y[i]-Y[j]);
					ri:=rr[i];
					rj:=rr[j];
					yy:=4*sqr(ri)*d-sqr(sqr(ri)+d-sqr(rj));
					if yy<0 then continue;
					a:=arctan2(sqrt(yy),sqr(ri)+d-sqr(rj));
					t:=arctan2(Y[j]-Y[i],X[j]-X[i]);
					if count(X[i]+ri*cos(t+a),Y[i]+ri*sin(t+a)) then flag:=True
					else if count(X[i]+ri*cos(t-a),Y[i]+ri*sin(t-a))then flag:=True;
					if flag then break;
				end;
				if flag then break;
			end;
		end;
		if flag then R:=M else L:=M;
	end;
	writeln(R:0:9);
end.


program main;
var a,b,c: integer;

begin
readln(a,b,c);

if (a=b) or (b=c) or (a=c) then
  begin
  if (a=b) and (b=c) and (a=c) then writeln('No')
  else writeln('Yes');
  end
else
  writeln('No');

end.

Score: 100 points

Problem Statement

A triple of numbers is said to be poor when two of those numbers are equal but the other number is different from those two numbers.

You will be given three integers A, B, and C. If this triple is poor, print Yes; otherwise, print No.

Constraints

A, B, and C are all integers between 1 and 9 (inclusive).

Input

Input is given from Standard Input in the following format:

A B C

Output

If the given triple is poor, print Yes; otherwise, print No.

Sample Input 1

5 7 5

Sample Output 1

Yes

A and C are equal, but B is different from those two numbers, so this triple is poor.

Sample Input 2

4 4 4

Sample Output 2

No

A, B, and C are all equal, so this triple is not poor.

Sample Input 3

4 9 6

Sample Output 3

No

Sample Input 4

3 3 4

Sample Output 4

Yes


        program main;
        var a,b,c:integer;
        begin
        readln(a,b,c);
        if((a=b) and (a<>c)) or ((a=c) and (a<>b)) or ((b=c) and (b<>a))
        then writeln ('Yes')
        else writeln('No');
        end.


program main;
var
    a,b,c : integer;
begin
    readln(a,b,c);
    if (( a = b ) and ( a <> c )) or (( a = c ) and ( a <> b )) or (( b = c ) and ( b <> a )) then
        writeln('Yes')
    else
        writeln('No');
end.


program main;
var
  n, i, t : longint;
  approved : boolean;

begin
  readln(n);
  approved := true;
  for i := 1 to n do begin
    read(t);
    if (t mod 2 = 0) and not((t mod 3 = 0) or (t mod 5 = 0)) then
      approved := false;
  end;
  readln;
  if approved then writeln('APPROVED')
  else writeln('DENIED');

end.

Score: 200 points

Problem Statement

You are an immigration officer in the Kingdom of AtCoder. The document carried by an immigrant has some number of integers written on it, and you need to check whether they meet certain criteria.

According to the regulation, the immigrant should be allowed entry to the kingdom if and only if the following condition is satisfied:

All even numbers written on the document are divisible by 3 or 5.

If the immigrant should be allowed entry according to the regulation, output APPROVED; otherwise, print DENIED.

Notes

The condition in the statement can be rephrased as "If x is an even number written on the document, x is divisible by 3 or 5".
Here "if" and "or" are logical terms.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

If the immigrant should be allowed entry according to the regulation, print APPROVED; otherwise, print DENIED.

Sample Input 1

5
6 7 9 10 31

Sample Output 1

APPROVED

The even numbers written on the document are 6 and 10.

All of them are divisible by 3 or 5, so the immigrant should be allowed entry.

Sample Input 2

3
28 27 24

Sample Output 2

DENIED

28 violates the condition, so the immigrant should not be allowed entry.


var     a:array[1..200001] of string;
        n,i,dem,j,max: longint;
        tg:string;
procedure sort(l,r: longint);
var mid:string;
begin
    if (l >= r) then exit;
    i:=l;
    j:=r;
    mid:=a[(i+j) div 2];
    while i < j do
        begin
            while (a[i] < mid) do inc(i);
            while (mid < a[j]) do dec(j);
            if (i <= j) then
                begin
                    tg:=a[i];
                    a[i]:=a[j];
                    a[j]:=tg;
                    inc(i);
                    dec(j);
                end;
        end;
    sort(i,r);
    sort(l,j);
end;
procedure sx;
begin
    for i := 1 to n-1 do
        for j := i+1 to n do
            if (a[i] > a[j]) then
                begin
                    tg:=a[i];
                    a[i]:=a[j];
                    a[j]:=tg;
                end;
end;
begin
    readln(n);
    for i := 1 to n do readln(a[i]);
    if (n > 3000) then sort(1,n)
    else
        sx;
    dem:=1;
    a[n+1]:='!';
    for i := 2 to n+1 do
        if a[i] = a[i-1] then inc(dem)
        else
            begin
                if (dem > max) then max:=dem;
                dem:=1;
            end;
    dem:=1;
    for i := 2 to n+1 do
        if (a[i] = a[i-1]) then inc(dem)
        else
            begin
                if (dem = max) then writeln(a[i-1]);
                dem:=1;
            end;
end.

Score: 300 points

Problem Statement

We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.

Print all strings that are written on the most number of votes, in lexicographical order.

Constraints

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

S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.

The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print all strings in question in lexicographical order.

Sample Input 1

7
beat
vet
beet
bed
vet
bet
beet

Sample Output 1

beet
vet

beet and vet are written on two sheets each, while beat, bed, and bet are written on one vote each. Thus, we should print the strings beet and vet.

Sample Input 2

8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo

Sample Output 2

buffalo

Sample Input 3

7
bass
bass
kick
kick
bass
kick
kick

Sample Output 3

kick

Sample Input 4

4
ushi
tapu
nichia
kun

Sample Output 4

kun
nichia
tapu
ushi


var     a:array[1..200001] of string;
        n,i,dem,j,max: longint;
        tg:string;
procedure sort(l,r: longint);
var mid:string;
begin
    if (l >= r) then exit;
    i:=l;
    j:=r;
    mid:=a[(i+j) div 2];
    while i < j do
        begin
            while (a[i] < mid) do inc(i);
            while (mid < a[j]) do dec(j);
            if (i <= j) then
                begin
                    tg:=a[i];
                    a[i]:=a[j];
                    a[j]:=tg;
                    inc(i);
                    dec(j);
                end;
        end;
    sort(i,r);
    sort(l,j);
end;
begin
    readln(n);
    for i := 1 to n do readln(a[i]);
    sort(1,n);
    dem:=1;
    a[n+1]:='!';
    for i := 2 to n+1 do
        if a[i] = a[i-1] then inc(dem)
        else
            begin
                if (dem > max) then max:=dem;
                dem:=1;
            end;
    dem:=1;
    for i := 2 to n+1 do
        if (a[i] = a[i-1]) then inc(dem)
        else
            begin
                if (dem = max) then writeln(a[i-1]);
                dem:=1;
            end;
end.


program main;
var a,b : integer;
	s,t,u : string;
begin
	readln(s,t);
    readln(a,b);
    readln(u);
    if(t <> u) then
        a := a - 1
        else if(s <> u) then
            b := b - 1;
    write(a,' ');
    writeln(b);
end.

Score : 100 points

Problem Statement

We have A balls with the string S written on each of them and B balls with the string T written on each of them.

From these balls, Takahashi chooses one with the string U written on it and throws it away.

Find the number of balls with the string S and balls with the string T that we have now.

Constraints

S, T, and U are strings consisting of lowercase English letters.

The lengths of S and T are each between 1 and 10 (inclusive).

S \not= T

S=U or T=U.

1 \leq A,B \leq 10

A and B are integers.

Input

Input is given from Standard Input in the following format:

S T
A B
U

Output

Print the answer, with space in between.

Sample Input 1

red blue
3 4
red

Sample Output 1

2 4

Takahashi chose a ball with red written on it and threw it away.
Now we have two balls with the string S and four balls with the string T.

Sample Input 2

red blue
5 5
blue

Sample Output 2

5 4

Takahashi chose a ball with blue written on it and threw it away.
Now we have five balls with the string S and four balls with the string T.


program main;
var s,t,u: string ;
a,b : integer;

begin
a:=0; b:=0;

readln(s,t);

readln(a,b);

readln(u);

if u=s then
a:=a-1;

if u=t then
b:=b-1;

writeln(a,b);

end.


program main;
var a,b : integer;
	s,t,u : string;
begin
	readln(s,t);
    readln(a,b);
    readln(u);
    if(u = s)
    	then writeln(a-1,b);
    if(u = t)
    	then writeln(a,b-1);
end.


program main;
var S,T,U : string;
var A,B : integer;
begin
readln (S,T);
readln (A,B);
readln (U);

if U=S then A:=A-1;
if T=S then B:=B-1;

writeln (A,B);

end.


program main;
var a,b : integer;
	s,t,u : string;
begin
	readln(s,t);
    readln(a,b);
    readln(u);
    if(s = u)
    	then a := a - 1
        	else if(t = u)
            	then b:= b - 1;
    write(a,' ',b);
end.


program main;
var s,t,u: string ;
a,b : integer;

begin
readln(s,t);
readln(a,b);
readln(u);

if (u=s) then
begin
a:=a-1;
writeln(a,b);
end;

if (u=t) then
begin
b:=b-1;
writeln(a,b);
end;

end.


program main;
var
  s : string;
  i : longint;

begin
  readln(s);
  for i := 1 to length(s) do
    write('x');
  writeln;

end.

Score : 200 points

Problem Statement

Given is a string S. Replace every character in S with x and print the result.

Constraints

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Replace every character in S with x and print the result.

Sample Input 1

sardine

Sample Output 1

xxxxxxx

Replacing every character in S with x results in xxxxxxx.

Sample Input 2

xxxx

Sample Output 2

xxxx

Sample Input 3

gone

Sample Output 3

xxxx


var a:array[0..200001] of longint;
    n,i:longint;p:boolean;
procedure sort(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 sort(i,r);
 if j>l then sort(l,j);
end;
begin
 readln(n);
 for i:=1 to n do read(a[i]);
 sort(1,n);
 p:=false;
 for i:=2 to n do if a[i]=a[i-1] then p:=true;
 if p then writeln('NO')
  else writeln('YES');
end.

Score : 300 points

Problem Statement

Given is a sequence of integers A_1, A_2, ..., A_N.
If its elements are pairwise distinct, print YES; otherwise, print NO.

Constraints

2 ≤ N ≤ 200000

1 ≤ A_i ≤ 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

If the elements of the sequence are pairwise distinct, print YES; otherwise, print NO.

Sample Input 1

5
2 6 1 4 5

Sample Output 1

YES

The elements are pairwise distinct.

Sample Input 2

6
4 1 3 1 6 2

Sample Output 2

NO

The second and fourth elements are identical.

Sample Input 3

2
10000000 10000000

Sample Output 3

NO


var n,i,t,p:longint;
    res,ord:array[0..1000000]of longword;
    a,b:array[0..1000000]of word;
    cnt:array[0..65535]of longint;
begin
  readln(n);
  for i:=1 to n do
  begin
    read(t);
    a[i]:=t>>16;
    b[i]:=t and pred(1<<16);
    inc(cnt[b[i]]);
  end;
  p:=1;
  for i:=0 to 65534 do
  begin
    inc(cnt[p],cnt[i]);
    inc(p);
  end;
  for i:=1 to n do
  begin
    ord[cnt[b[i]]]:=i;
    dec(cnt[b[i]]);
  end;
  fillchar(cnt,sizeof(cnt),0);
  for i:=1 to n do
    inc(cnt[a[i]]);
  p:=1;
  for i:=0 to 65534 do
  begin
    inc(cnt[p],cnt[i]);
    inc(p);
  end;
  for i:=n downto 1 do
  begin
    p:=a[ord[i]];
    res[cnt[p]]:=ord[i];
    dec(cnt[p]);
  end;
  for i:=1 to n-1 do
    if a[res[i]]<<16 or b[res[i]]=a[res[i+1]]<<16 or b[res[i+1]] then
    begin
      writeln('NO');
      halt;
    end;
  writeln('YES');
end.


var
  n,k,t,i:longint;
  a,sum:array[0..300000] of real;
  ans:real;
begin
  readln(n,k);
  for i:=1 to n do
  begin
    read(t);
    a[i]:=(1+t)*t div 2/t;
    sum[i]:=sum[i-1]+a[i];
  end;
  for i:=k to n do
  if sum[i]-sum[i-k]>ans then ans:=sum[i]-sum[i-k];
  writeln(ans:0:12);
end.

Score : 400 points

Problem Statement

We have N dice arranged in a line from left to right. The i-th die from the left shows p_i numbers from 1 to p_i with equal probability when thrown.

We will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.

Constraints

1 ≤ K ≤ N ≤ 200000

1 ≤ p_i ≤ 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 ... p_N

Output

Print the maximum possible value of the expected value of the sum of the numbers shown.

Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

5 3
1 2 2 4 5

Sample Output 1

7.000000000000

When we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve.

Sample Input 2

4 1
6 6 6 6

Sample Output 2

3.500000000000

Regardless of which die we choose, the expected value of the number shown is 3.5.

Sample Input 3

10 4
17 13 13 12 15 20 10 13 17 11

Sample Output 3

32.000000000000


program main;
var H,A,x :integer;
begin
    readln(H,A);
    x:=H div A;
    if H mod A=0 then
    writeln(x)
    else writeln(x+1);
end.

Score : 100 points

Problem Statement

Serval is fighting with a monster.

The health of the monster is H.

In one attack, Serval can decrease the monster's health by A.
There is no other way to decrease the monster's health.

Serval wins when the monster's health becomes 0 or below.

Find the number of attacks Serval needs to make before winning.

Constraints

1 \leq H \leq 10^4

1 \leq A \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H A

Output

Print the number of attacks Serval needs to make before winning.

Sample Input 1

10 4

Sample Output 1

3

After one attack, the monster's health will be 6.

After two attacks, the monster's health will be 2.

After three attacks, the monster's health will be -2.

Thus, Serval needs to make three attacks to win.

Sample Input 2

1 10000

Sample Output 2

1

Sample Input 3

10000 1

Sample Output 3

10000


program main;
var H,A,x,i :integer;
begin
    x:=0;
    readln(H,A);
    x:=H div A;
    if H mod A=0 then
    writeln(x)
    else writeln(x+1);
end.


program main;
var H,A,x :integer;
begin
    x:=0;
    readln(H,A);
    x:=H div A;
    if H mod A=0 then
    writeln(x)
    else writeln(x+1);
end.


program main;
var H,A,x,i :integer;
begin
    x:=0;
    readln(H,A);
    x:=H div A;
    writeln(x+1);
end.


    program main;
    var h,a,count : integer;
    begin
    count:=0;
    readln (h,a);
    repeat
    h:=h-a;
    count:=count+1;
    until (h<=0);
    writeln (count);
    end.


program main;
var H,A,count : integer;
begin

count:=0;
readln (H,A);
repeat
H:=H-A;
count:=count+1;
until (H<=0);

writeln (count);

end.


program main;
var h , a , count : integer;
begin
	readln(h,a);
    repeat
    	h := h -a;
        count := count + 1;
    until(h <= 0);
    writeln(count);
end.


program main;
var h , a , count : integer;
begin
	readln(h,a);
    repeat
    	h := h -a;
        count := count + 1;
    until(h = 0);
    writeln(count);
end.


program main;
var h,a,ans: integer;

begin
readln(h,a);
ans:=0;
repeat
h:=h-a;
ans:=ans+1;
until h<=0;
end.


program main;
var
    a,b,c : QWord;
begin
    readln(a,b);
     c := a div b;
  if (a mod b <> 0) then
    inc(c);
  writeln(c);
end.


program main;
var h,a,output: integer;

begin
readln(h,a);
output:=0;
if (h>=1) and (a>=1) then
repeat
h:=h-a;
output:=output+1;
until (h<=0);
end.


program main;
var
  h, a, n : longint;

begin
  readln(h, a);
  n := h div a;
  if (h mod a <> 0) then inc(n);
  writeln(n);
end.


program main;
var
    H,N,i,ttl,r : integer;
begin
    ttl := 0;
    readln(H,N);
    for i := 1 to N do
        begin
            read(r);
            inc(ttl,r);
        end;
    if ( ttl > H )  or ( ttl = H ) then
        writeln('Yes')
    else
        writeln('No');
end.

Score : 200 points

Problem Statement

Raccoon is fighting with a monster.

The health of the monster is H.

Raccoon can use N kinds of special moves. Using the i-th move decreases the monster's health by A_i.
There is no other way to decrease the monster's health.

Raccoon wins when the monster's health becomes 0 or below.

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Constraints

1 \leq H \leq 10^9

1 \leq N \leq 10^5

1 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 A_2 ... A_N

Output

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Sample Input 1

10 3
4 5 6

Sample Output 1

Yes

The monster's health will become 0 or below after, for example, using the second and third moves.

Sample Input 2

20 3
4 5 6

Sample Output 2

No

Sample Input 3

210 5
31 41 59 26 53

Sample Output 3

Yes

Sample Input 4

211 5
31 41 59 26 53

Sample Output 4

No


program main;
var
  h, a, n, t, i : longint;
  win : boolean;

begin
  readln(h, n);
  a := 0;
  for i := 1 to n do begin
    read(t);
    inc(a, t);
  end;
  readln;
  if (h-a <= 0) then win := true
  else win := false;
  if win then writeln('Yes')
  else writeln('No');

end.


var h,n,i,a:Longint;
begin
read(h,n);
for i:=1 to n do begin
read(a);
dec(h,a);
end;
if h>0 then writeln('No')else writeln('Yes');
end.


var
	sum,i,h,a:Integer;
    arr:array of Integer;
begin
	read(h,a);
	SetLength(arr, a);
    sum := 0;
    for i := 0 to High(arr) do begin
            read(arr[i]);
            sum := sum + arr[i];
    end;
    begin
    if sum > h then
    	write('Yes')
    else
        write('No');
    end;
end.


var
    n, v, min, cnt, i:integer;
begin
    read(n);
    cnt := 0;
    min := n + 1;
    for i := 1 to n do begin
        read(v);
        if v < min then begin
            cnt := cnt+1;
            min := v;
        end;
    end;
    writeln(cnt);
end.

Score : 300 points

Problem Statement

Given is a permutation P_1, \ldots, P_N of 1, \ldots, N.
Find the number of integers i (1 \leq i \leq N) that satisfy the following condition:

For any integer j (1 \leq j \leq i), P_i \leq P_j.

Constraints

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

P_1, \ldots, P_N is a permutation of 1, \ldots, N.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 ... P_N

Output

Print the number of integers i that satisfy the condition.

Sample Input 1

5
4 2 5 1 3

Sample Output 1

3

i=1, 2, and 4 satisfy the condition, but i=3 does not - for example, P_i > P_j holds for j = 1.

Similarly, i=5 does not satisfy the condition, either. Thus, there are three integers that satisfy the condition.

Sample Input 2

4
4 3 2 1

Sample Output 2

4

All integers i (1 \leq i \leq N) satisfy the condition.

Sample Input 3

6
1 2 3 4 5 6

Sample Output 3

1

Only i=1 satisfies the condition.

Sample Input 4

8
5 7 4 2 6 8 1 3

Sample Output 4

4

Sample Input 5

1
1

Sample Output 5

1


Var a,n,i,min,count:Longint;
Begin
    Readln(n); min:=200001;
    For i:=1 to n do begin
        Read(a);
        If a<min then inc(count);
        If a<min then min:=a;
    End;
    Writeln(count);
End.


var n,m,i,x,ans,ans1:longint;
f:array[0..100000]of boolean;
y:string;
begin
readln(n,m);
for i:=1 to m do
  begin
  read(x);readln(y);
  if y='AC' then
    begin
	if not f[x] then inc(ans);
	f[x]:=true;
	end
  else if f[x]=false then inc(ans1);
  end;
writeln(ans,' ',ans1);
end.

Score : 300 points

Problem Statement

Takahashi participated in a contest on AtCoder.

The contest had N problems.

Takahashi made M submissions during the contest.

The i-th submission was made for the p_i-th problem and received the verdict S_i (AC or WA).

The number of Takahashi's correct answers is the number of problems on which he received an AC once or more.

The number of Takahashi's penalties is the sum of the following count for the problems on which he received an AC once or more: the number of WAs received before receiving an AC for the first time on that problem.

Find the numbers of Takahashi's correct answers and penalties.

Constraints

N, M, and p_i are integers.

1 \leq N \leq 10^5

0 \leq M \leq 10^5

1 \leq p_i \leq N

S_i is AC or WA.

Input

Input is given from Standard Input in the following format:

N M
p_1 S_1
:
p_M S_M

Output

Print the number of Takahashi's correct answers and the number of Takahashi's penalties.

Sample Input 1

2 5
1 WA
1 AC
2 WA
2 AC
2 WA

Sample Output 1

2 2

In his second submission, he received an AC on the first problem for the first time. Before this, he received one WA on this problem.

In his fourth submission, he received an AC on the second problem for the first time. Before this, he received one WA on this problem.

Thus, he has two correct answers and two penalties.

Sample Input 2

100000 3
7777 AC
7777 AC
7777 AC

Sample Output 2

1 0

Note that it is pointless to get an AC more than once on the same problem.

Sample Input 3

6 0

Sample Output 3

0 0


program main;
var k,x : integer;
begin

readln (k,x);
if (k*500>=x) then writeln ('Yes')
else writeln ('No');

end.

Score : 100 points

Problem Statement

Takahashi has K 500-yen coins. (Yen is the currency of Japan.)
If these coins add up to X yen or more, print Yes; otherwise, print No.

Constraints

1 \leq K \leq 100

1 \leq X \leq 10^5

Input

Input is given from Standard Input in the following format:

K X

Output

If the coins add up to X yen or more, print Yes; otherwise, print No.

Sample Input 1

2 900

Sample Output 1

Yes

Two 500-yen coins add up to 1000 yen, which is not less than X = 900 yen.

Sample Input 2

1 501

Sample Output 2

No

One 500-yen coin is worth 500 yen, which is less than X = 501 yen.

Sample Input 3

4 2000

Sample Output 3

Yes

Four 500-yen coins add up to 2000 yen, which is not less than X = 2000 yen.


program main;
var a,b:integer;
begin
write('input : '); readln(a, b);
If(a*500 < b) then writeln('Yes')
else               writeln('No');
end.


program main;
var k,x : integer;
begin
  readln(k,x);
  k := k*500;
  if (k >= x) then writeln('Yes');
  if (k < x) then writeln('No');
end.


program main;
var k, x : integer;

begin

readln(k,x);
if (k>=1) and (k<=100) and (x>=1) and (x<=100000) then
begin
k:=500*k;
if k>=x then writeln('Yes') else writeln('No');
end;

end.


program main;
var n,g,count : integer;
var s : string;
begin

readln (n);
readln (s);
count:=0;

for g:=1 to (n+2) do
if s[g]+s[g+1]+s[g+2]='ABC'
then count:=count+1;

writeln (count);
end.

Score : 200 points

Problem Statement

We have a string S of length N consisting of uppercase English letters.

How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)?

Constraints

3 \leq N \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print number of occurrences of ABC in S as contiguous subsequences.

Sample Input 1

10
ZABCDBABCQ

Sample Output 1

2

Two contiguous subsequences of S are equal to ABC: the 2-nd through 4-th characters, and the 7-th through 9-th characters.

Sample Input 2

19
THREEONEFOURONEFIVE

Sample Output 2

0

No contiguous subsequences of S are equal to ABC.

Sample Input 3

33
ABCCABCBABCCABACBCBBABCBCBCBCABCB

Sample Output 3

5


program main;
var s : array[1..100] of char;
    n,i,c : integer;
begin
  readln(n);
  for i := 1 to n do
    read(s[i]);
    c := 0;
  for i := 1 to n do
    if (s[i] = 'A') and (s[i+1] = 'B') and (s[i+2] = 'C') then inc(c);
  writeln(c);
end.


program main;
var
  i, c, n : longint;
  s : string;

begin
  readln(n);
  readln(s);
  c := 0;
  for i := 1 to n do begin
    if (s[i] + s[i+1] + s[i+2] = 'ABC') then inc(c);
  end;
  writeln(c);
end.


// credit: fwho1150
program main;

type
    sample = array[1..8] of longint;

var
    n, order: longint;
    p, q: sample;

procedure input (var a: sample);
var
    i: longint;

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

function code(var a: sample): sample;
var
    i, j, c: longint;

begin
    for i := 1 to n do
        a[i] := a[i] - 1;
    for i := 1 to n do begin
        c := 0;
        for j := 1 to (i - 1) do
            if (a[j] < a[i]) then
                inc(c);
        code[i] := a[i] - c;
    end;
end;

function fact(n: longint): longint;
begin
    if (n = 0) or (n = 1) then
        fact := 1
    else
        fact := n * fact(n - 1);
end;

function rank(a: sample): longint;
var
    i, tmp: longint;

begin
    tmp := 0;
    for i := 1 to n do
        tmp := tmp + a[i] * fact(n - i);
    rank := tmp;
end;

begin
    readln(n);
    input(p);
    input(q);
    order := abs(rank(code(p)) - rank(code(q)));
    writeln(order);
end.

Score : 300 points

Problem Statement

We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).

There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Sample Input 2

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

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0


program main;
var s,t,output : string;
begin
readln (s,t);
output:=t+s;
writeln (output);
end.

Score : 100 points

Problem Statement

Given are two strings S and T consisting of lowercase English letters. Concatenate T and S in this order, without space in between, and print the resulting string.

Constraints

S and T are strings consisting of lowercase English letters.

The lengths of S and T are between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S T

Output

Print the resulting string.

Sample Input 1

oder atc

Sample Output 1

atcoder

When S = oder and T = atc, concatenating T and S in this order results in atcoder.

Sample Input 2

humu humu

Sample Output 2

humuhumu


program main;
var a,b,k :integer;

begin
readln(a,b,k);
repeat
repeat
if (a>=1) then begin a:=a-1;k:=k-1;
end;
until (a=0);
if (a=0) and (b>=1) then begin b:=b-1;
k:=k-1;end;
until (k=0);

writeln(a,b);

end.


program main;
var s,t : string;
begin
	readln(s,t);
    writeln(t,s);
end.


program main;
var s, t:string;

begin
readln(s,t);
writeln(t,s);
end.


program main;
var s,t : string;
begin
	readln(s,t);
    write(t);
    write(s);
end.


program main;
var s: string;
begin
  readln(s);
  writeln(copy(s + s, pos(' ', s) + 1, length(s)))
end.


program main;
var
    a, b, c : string;
    i : integer;
begin
    readln(a);
    for i := 1 to length(a) do
        if a[i] = ' ' then
            begin
                b := copy(a,1,i-1);
                c := copy(a,i+1,(length(a)-i));
            end;
    writeln(c,b);
end.


program main;
var
    a, b, c : string[100];
    i : integer;
begin
    readln(a);
    for i := 1 to length(a) do
        if a[i] = ' ' then
            begin
                b := copy(a,1,i-1);
                c := copy(a,i+1,(length(a)-i));
            end;
    writeln(c,b);
end.


program main;
var
  c : char;
  s, t : string;

begin
  c := 'A';
  s := ''; t := '';
  while c <> ' ' do begin
    read(c);
    if c <> ' ' then
      s := s + c;
  end;
  while not eoln do begin
    read(c);
    t := t + c;
  end;
  writeln(t + s);
end.


uses crt;
var a,b:string;
    i,n:longint;
begin
        clrscr;
        read(a);
        for i:=1 to pos(' ',a) do
        begin
        b:=b+a[i];
        end;
        delete(a,1,pos(' ',a));
        writeln(a,b);
        readln;
end.


uses crt;
var a,b:string;
    i,n:longint;
begin
        clrscr;
        read(a);
        for i:=1 to pos(' ',a) do
        begin
        b:=b+a[i];
        end;
        delete(a,1,pos(' ',a));
        writeln(b,a);
        readln;
end.


program main;
var a,b,k : longint;
begin
	readln(a,b,k);
    while(a > 0) and (k > 0) do begin
        a := a - 1;
        k := k - 1;
        if(a = 0) then begin
            b := b - 1;
        end;
    end;
    write(a);
    writeln(b);
end.

Score : 200 points

Problem Statement

Takahashi has A cookies, and Aoki has B cookies.
Takahashi will do the following action K times:

If Takahashi has one or more cookies, eat one of his cookies.

Otherwise, if Aoki has one or more cookies, eat one of Aoki's cookies.

If they both have no cookies, do nothing.

In the end, how many cookies will Takahashi and Aoki have, respectively?

Constraints

0 \leq A \leq 10^{12}

0 \leq B \leq 10^{12}

0 \leq K \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the numbers of Takahashi's and Aoki's cookies after K actions.

Sample Input 1

2 3 3

Sample Output 1

0 2

Takahashi will do the following:

He has two cookies, so he eats one of them.

Now he has one cookie left, and he eats it.

Now he has no cookies left, but Aoki has three, so Takahashi eats one of them.

Thus, in the end, Takahashi will have 0 cookies, and Aoki will have 2.

Sample Input 2

500000000000 500000000000 1000000000000

Sample Output 2

0 0

Watch out for overflows.


program main;
var a,b,k : integer;
begin
	readln(a,b,k);
    while(a > 0) and (k > 0) do begin
        a := a - 1;
        k := k - 1;
        if(a = 0) then begin
            b := b - 1;
        end;
    end;
    write(a);
    writeln(b);
end.


program main;
var a,b,k : integer;
begin
  readln(a,b,k);
  if (a >= 1000000000000) or (b >= 1000000000000) or (k >= 1000000000000) then writeln('0 0');
  if (a >= k) then writeln(a - k, ' ', b)
    else if (a + b >= k) then writeln(0, ' ', a + b - k)
      else writeln('0 0');
end.


program main;
var a,b,k:integer;
begin
readln(a,b,k);
 if(a>=k) then a:=a-k;
 if(a<k) then begin
   a:=0;
   k:=k-a;
   b:=b-k;
end;
  writeln(a,' ',b);
  end.


program main;
var
    a,b,k : QWord;
begin
    readln(a,b,k);
    if ( a >= k ) then
        a := a-k
    else
        begin
            a := 0;
            k := k-a;
        if ( b > k ) then
            b := b-k
        else
            b := 0;
        end;
    writeln(a,' ',b);
end.


program main;
var
    a,b,k : QWord;
begin
    readln(a,b,k);
    if ( a >= k ) then
        a := a-k
    else
        if ( b > k ) then
            b := b-k
        else
            b := 0;
    writeln(a,' ',b);
end.


program main;
var
    a,b,k : QWord;
begin
    readln(a,b,k);
    if (a > 100000000000) or (b > 100000000000) or (k > 100000000000) then
        writeln('0 0')
    else
        begin
            if ( a >= k ) then
                writeln(a-k,' ',b)
            else
                if ( a < k ) then
                    writeln('0',' ',b-(k-a));
        end;
end.


program hi;
var
    si: array[2..100010] of boolean;
    x, i, j: longint;

begin
    for i := 2 to 100010 do
        si[i] := true;
    for i := 2 to 317 do begin
        j := i * i;
        while j <= 100010 do begin
            si[j] := false;
            j := j + i;
        end;
    end;
    readln(x);
    if si[x] then
        writeln(x)
    else begin
        i := x;
        while not si[i] do
            inc(i);
        writeln(i);
    end;
end.

Score: 300 points

Problem Statement

Find the minimum prime number greater than or equal to X.

Notes

A prime number is an integer greater than 1 that cannot be evenly divided by any positive integer except 1 and itself.

For example, 2, 3, and 5 are prime numbers, while 4 and 6 are not.

Constraints

2 \le X \le 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the minimum prime number greater than or equal to X.

Sample Input 1

20

Sample Output 1

23

The minimum prime number greater than or equal to 20 is 23.

Sample Input 2

2

Sample Output 2

2

X itself can be a prime number.

Sample Input 3

99992

Sample Output 3

100003


var n,i,j,max: longint;
snt: array[1..1000000] of boolean;
begin

read(n);
max:=n*10;
for j:=1 to max do snt[j]:=true;
snt[1]:=false;
j:=2;
while j<=trunc(sqrt(max)) do
begin
while snt[j]=false do inc(j);
for i:=2 to max div j do snt[i*j]:=false;
inc(j);
end;
while snt[n]=false do inc(n);
write(n);

end.


var n,m,count,i,j,j1,sum,x,tam,k: longint;
a: array[1..10000] of longint;
 procedure  qs(l,r: longint);
BEGIN
i:=l;
j:=r;
x:=a[(i+j) div 2];
repeat
while a[i]>x do inc(i);
while a[j]<x do dec(j);
if i<=j then begin tam:=a[i]; a[i]:=a[j]; a[j]:=tam; inc(i); dec(j); end;
until i>j;
if l<j then qs(l,j);
if r>i then qs(i,r);
end;
begin

read(n,k);
for i:=1 to n do read(a[i]);
qs(1,n);
for i:=1 to n do
for j:=i to n do
begin
if i=j then begin
sum:=sum+a[i]+a[j];
inc(count);   if count=k then begin write(sum);  exit; end;
end else for j1:=1 to   2 do begin sum:=sum+a[i]+a[j]; inc(count); if
count=k then begin write(sum); exit; end; end;
end;
end.

Score : 500 points

Problem Statement

Takahashi has come to a party as a special guest.
There are N ordinary guests at the party. The i-th ordinary guest has a power of A_i.

Takahashi has decided to perform M handshakes to increase the happiness of the party (let the current happiness be 0).
A handshake will be performed as follows:

Takahashi chooses one (ordinary) guest x for his left hand and another guest y for his right hand (x and y can be the same).

Then, he shakes the left hand of Guest x and the right hand of Guest y simultaneously to increase the happiness by A_x+A_y.

However, Takahashi should not perform the same handshake more than once. Formally, the following condition must hold:

Assume that, in the k-th handshake, Takahashi shakes the left hand of Guest x_k and the right hand of Guest y_k. Then, there is no pair p, q (1 \leq p < q \leq M) such that (x_p,y_p)=(x_q,y_q).

What is the maximum possible happiness after M handshakes?

Constraints

1 \leq N \leq 10^5

1 \leq M \leq N^2

1 \leq A_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the maximum possible happiness after M handshakes.

Sample Input 1

5 3
10 14 19 34 33

Sample Output 1

202

Let us say that Takahashi performs the following handshakes:

In the first handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 4.

In the second handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 5.

In the third handshake, Takahashi shakes the left hand of Guest 5 and the right hand of Guest 4.

Then, we will have the happiness of (34+34)+(34+33)+(33+34)=202.

We cannot achieve the happiness of 203 or greater, so the answer is 202.

Sample Input 2

9 14
1 3 5 110 24 21 34 5 3

Sample Output 2

1837

Sample Input 3

9 73
67597 52981 5828 66249 75177 64141 40773 79105 16076

Sample Output 3

8128170


uses crt;
var
        n,d,i,j:byte;
        t,a,b,c:string;
begin

        clrscr;
        readln(n);
        readln(t);
        for i:=1 to n do if t[i]<>' ' then a:=a+t[i];
        for j:=1 to n do  b:=b+t[i+j+1];
        i:=0;
        while i<>n do
        begin
                if (d=0) then
                begin
                        inc(i);
                        c:=c+a[i];
                        inc(d);
                end;
                if (d=1) then
                begin
                        c:=c+b[i];
                        dec(d);
                end;
        end;
        write(c);
        readln;
end.

Score : 200 points

Problem Statement

Given are strings s and t of length N each, both consisting of lowercase English letters.

Let us form a new string by alternating the characters of S and the characters of T, as follows: the first character of S, the first character of T, the second character of S, the second character of T, ..., the N-th character of S, the N-th character of T. Print this new string.

Constraints

1 \leq N \leq 100

|S| = |T| = N

S and T are strings consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S T

Output

Print the string formed.

Sample Input 1

2
ip cc

Sample Output 1

icpc

Sample Input 2

8
hmhmnknk uuuuuuuu

Sample Output 2

humuhumunukunuku

Sample Input 3

5
aaaaa aaaaa

Sample Output 3

aaaaaaaaaa


uses crt;
var a,b:string;
    i,n:longint;
begin
        clrscr;
        readln(n);
        readln(a);
        for i:=1 to pos(' ',a) do
        begin
        b:=b+a[i];
        end;
        delete(a,1,n+1);
        for i:=1 to length(b) do
        write(a[i],b[i]);
        readln
end.


uses crt,math;
var a,b:longint;
    i,j:qword;
begin
        clrscr;
        readln(a,b);
        i:=a;
        j:=b;
        while (true) do begin
                if (a=0) or (b=0) then break;
                if (a>b)  then a := a mod b else
                        if (b>a) then b := b mod a else break;
        end;
        write((i*j)div max(a,b));
        readln
end.

Score : 300 points

Problem Statement

Takahashi is organizing a party.

At the party, each guest will receive one or more snack pieces.

Takahashi predicts that the number of guests at this party will be A or B.

Find the minimum number of pieces that can be evenly distributed to the guests in both of the cases predicted.

We assume that a piece cannot be divided and distributed to multiple guests.

Constraints

1 \leq A, B \leq 10^5

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of pieces that can be evenly distributed to the guests in both of the cases with A guests and B guests.

Sample Input 1

2 3

Sample Output 1

6

When we have six snack pieces, each guest can take three pieces if we have two guests, and each guest can take two if we have three guests.

Sample Input 2

123 456

Sample Output 2

18696

Sample Input 3

100000 99999

Sample Output 3

9999900000


var a,b:longint;
tmp:int64;
begin
        readln(a, b);
        if b>a then begin
                tmp:=a;
                a:=b;
                b:=a;
        end;
        tmp:=a;
        while true do begin
                tmp:=tmp+a;
                if tmp mod b = 0 then begin
                        writeln(tmp);
                        break;
                end;
        end;
end.


uses crt;
var n,d,i,j:longint;
    a:array[1..200000]of longint;
begin
        clrscr;
        readln(n);
        for i:=1 to n do
        read(a[i]);
        d:=1;
        for i:=1 to n do
        if (a[i]=d)then inc(d);
        if (d=1)then write(-1) else write(n-d+1);
        readln;
        readln
end.

Score : 400 points

Problem Statement

We have N bricks arranged in a row from left to right.

The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it.

Among them, you can break at most N-1 bricks of your choice.

Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it.

Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.

Constraints

All values in input are integers.

1 \leq N \leq 200000

1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.

Sample Input 1

3
2 1 2

Sample Output 1

1

If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.

Sample Input 2

3
2 2 2

Sample Output 2

-1

In this case, there is no way to break some of the bricks to satisfy Snuke's desire.

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

7

Sample Input 4

1
1

Sample Output 4

0

There may be no need to break the bricks at all.


var n,k,i:longint;
    b:array[1..200001] of longint;
    f:text;
begin
readln(n);
k:=1;
for i:=1 to n do
read(b[i]);
for i:=1 to n do
begin
if b[i]=k then inc(k);
end;
if k=1 then write('-1') else write(n-k+1);
end.


uses crt;
var n,d,i,j:longint;
    a:array[1..1000]of longint;
begin
        clrscr;
        readln(n);
        for i:=1 to n do
        read(a[i]);
        for i:=1 to n-1 do
        if (a[i+1]-a[i]=1)and(a[i]-d=1)then inc(d)else
        if (a[i+1]-d=1)then inc(d);
        if (d<>0)then write(n-d)else write(d);
        readln;
        readln
end.


uses crt;
var i,n,count,j: longint;
a: array[1..300000] of longint;
begin
clrscr;
read(n);
FOR i:=1 TO n do read(a[i]);
i:=1;  j:=1;
while (i<=n) and(j<=n) do
begin
while (a[j]<>i) and (j<=n) do begin inc(count);  inc(j); end;
inc(j);
inc(i);
end;
if count=n then write('-1') else write(count);
readln;
readln;
end.


var
	N,A,x,y,i,j,out,ans,nowt,nowf,ccc:Longint;
	F,T,gF,gT:Array[1..15]of Longint;
begin
	read(N);
	for i:=1 to N do begin
		read(A);
		for j:=1 to A do begin
			read(x,y);
			dec(x);
			if y=0 then F[i]:=F[i] or (1<<x)else T[i]:=T[i] or (1<<x);
		end;
	end;
	for i:=1 to N do begin
		nowt:=1<<(i-1);
		nowf:=0;
		for ccc:=1 to N do begin
			for j:=1 to N do begin
				if ((nowt>>(j-1))and 1)=1 then begin
					nowt:=nowt or T[j];
					nowf:=nowf or F[j];
				end;
			end;
		end;
		gF[i]:=nowf;
		gT[i]:=nowt;
		if((out and nowt)<>0)or((nowt and nowf)<>0)then out:=out or(1<<(i-1));
	end;
	i:=((1<<N)-1)and not out;
	ans:=0;
	while i>0 do begin
		ccc:=0;
		nowt:=i;
		nowf:=0;
		for j:=1 to N do begin
			if ((nowt>>(j-1))and 1)=1 then begin
				inc(ccc);
				nowt:=nowt or gT[j];
				nowf:=nowf or gF[j];
			end;
		end;
		if((nowt and nowf)=0)and(ans<ccc)then ans:=ccc;
		i:=(i-1)and not out;
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not.

Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind.

How many honest persons can be among those N people at most?

Constraints

All values in input are integers.

1 \leq N \leq 15

0 \leq A_i \leq N - 1

1 \leq x_{ij} \leq N

x_{ij} \neq i

x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2)

y_{ij} = 0, 1

Input

Input is given from Standard Input in the following format:

N
A_1
x_{11} y_{11}
x_{12} y_{12}
:
x_{1A_1} y_{1A_1}
A_2
x_{21} y_{21}
x_{22} y_{22}
:
x_{2A_2} y_{2A_2}
:
A_N
x_{N1} y_{N1}
x_{N2} y_{N2}
:
x_{NA_N} y_{NA_N}

Output

Print the maximum possible number of honest persons among the N people.

Sample Input 1

3
1
2 1
1
1 1
1
2 0

Sample Output 1

2

If Person 1 and Person 2 are honest and Person 3 is unkind, we have two honest persons without inconsistencies, which is the maximum possible number of honest persons.

Sample Input 2

3
2
2 1
3 0
2
3 1
1 0
2
1 1
2 0

Sample Output 2

0

Assuming that one or more of them are honest immediately leads to a contradiction.

Sample Input 3

2
1
2 0
1
1 0

Sample Output 3

1


const p = 1000000007;
var
  a: array [1..300000] of int64;
  n, i, j, s, ans: longint;
function add(x, y: longint): longint; inline;
begin
  inc(x, y);
  if x >= p then dec(x, p);
  exit(x);
end;
function mul(x, y: longint): longint; inline;
begin
  exit(int64(x) * y mod p);
end;
begin
  read(n);
  for i := 1 to n do read(a[i]);
  for i := 0 to 59 do
  begin
    s := 0;
    for j := 1 to n do inc(s, (a[j] shr i) and 1);
    ans := add(ans, mul(mul(s, n - s), int64(1) shl i mod p));
  end;
  writeln(ans);
end.

Score : 400 points

Problem Statement

We have N integers. The i-th integer is A_i.

Find \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

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

0 \leq A_i < 2^{60}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the value \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

Sample Input 1

3
1 2 3

Sample Output 1

6

We have (1\mbox{ XOR } 2)+(1\mbox{ XOR } 3)+(2\mbox{ XOR } 3)=3+2+1=6.

Sample Input 2

10
3 1 4 1 5 9 2 6 5 3

Sample Output 2

237

Sample Input 3

10
3 14 159 2653 58979 323846 2643383 27950288 419716939 9375105820

Sample Output 3

103715602

Print the sum modulo (10^9+7).


var
    n, i : integer;
    ans : boolean;
begin
    readln(n);
    i := 1;
    while trunc(i * 1.08) < n do
    begin
       i := i + 1;
    end;
    if trunc(i * 1.08) = n then
      writeln(i)
    else
      writeln(':(');
end.

Score: 200 points

Problem Statement

Takahashi bought a piece of apple pie at ABC Confiserie. According to his memory, he paid N yen (the currency of Japan) for it.

The consumption tax rate for foods in this shop is 8 percent. That is, to buy an apple pie priced at X yen before tax, you have to pay X \times 1.08 yen (rounded down to the nearest integer).

Takahashi forgot the price of his apple pie before tax, X, and wants to know it again. Write a program that takes N as input and finds X. We assume X is an integer.

If there are multiple possible values for X, find any one of them. Also, Takahashi's memory of N, the amount he paid, may be incorrect. If no value could be X, report that fact.

Constraints

1 \leq N \leq 50000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If there are values that could be X, the price of the apple pie before tax, print any one of them.

If there are multiple such values, printing any one of them will be accepted.

If no value could be X, print :(.

Sample Input 1

432

Sample Output 1

400

If the apple pie is priced at 400 yen before tax, you have to pay 400 \times 1.08 = 432 yen to buy one.

Otherwise, the amount you have to pay will not be 432 yen.

Sample Input 2

1079

Sample Output 2

:(

There is no possible price before tax for which you have to pay 1079 yen with tax.

Sample Input 3

1001

Sample Output 3

927

If the apple pie is priced 927 yen before tax, by rounding down 927 \times 1.08 = 1001.16, you have to pay 1001 yen.


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

Score : 200 points

Problem Statement

We have a string S consisting of uppercase English letters. Additionally, an integer N will be given.

Shift each character of S by N in alphabetical order (see below), and print the resulting string.

We assume that A follows Z. For example, shifting A by 2 results in C (A \to B \to C), and shifting Y by 3 results in B (Y \to Z \to A \to B).

Constraints

0 \leq N \leq 26

1 \leq |S| \leq 10^4

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the string resulting from shifting each character of S by N in alphabetical order.

Sample Input 1

2
ABCXYZ

Sample Output 1

CDEZAB

Note that A follows Z.

Sample Input 2

0
ABCXYZ

Sample Output 2

ABCXYZ

Sample Input 3

13
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample Output 3

NOPQRSTUVWXYZABCDEFGHIJKLM


Var n,i,rem:Longint;
    S:Ansistring;
Begin
    Readln(n);
    Readln(S);
    For i:=1 to length(S) do begin
        rem:=ord(S[i])+n;
        If rem>90 then rem:=rem-26;
        S[i]:=chr(rem);
    End;
    Writeln(S);
End.


var n,i:Longint;
s,t,m:int64;
a:Array[1..200000]of Longint;
begin
read(n);
for i:=1 to n do begin
read(a[i]);
inc(s,a[i]);
end;
m:=s;
for i:=1 to n-1 do begin
inc(t,a[i]);dec(s,a[i]);
if(s<t)and(m>t-s)then m:=t-s else if(s>=t)and(m>s-t)then m:=s-t;
end;
writeln(m);
end.

Score: 200 points

Problem Statement

Takahashi, who works at DISCO, is standing before an iron bar.
The bar has N-1 notches, which divide the bar into N sections. The i-th section from the left has a length of A_i millimeters.

Takahashi wanted to choose a notch and cut the bar at that point into two parts with the same length.
However, this may not be possible as is, so he will do the following operations some number of times before he does the cut:

Choose one section and expand it, increasing its length by 1 millimeter. Doing this operation once costs 1 yen (the currency of Japan).

Choose one section of length at least 2 millimeters and shrink it, decreasing its length by 1 millimeter. Doing this operation once costs 1 yen.

Find the minimum amount of money needed before cutting the bar into two parts with the same length.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 2020202020

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_N

Output

Print an integer representing the minimum amount of money needed before cutting the bar into two parts with the same length.

Sample Input 1

3
2 4 3

Sample Output 1

3

The initial lengths of the sections are [2, 4, 3] (in millimeters). Takahashi can cut the bar equally after doing the following operations for 3 yen:

Shrink the second section from the left. The lengths of the sections are now [2, 3, 3].

Shrink the first section from the left. The lengths of the sections are now [1, 3, 3].

Shrink the second section from the left. The lengths of the sections are now [1, 2, 3], and we can cut the bar at the second notch from the left into two parts of length 3 each.

Sample Input 2

12
100 104 102 105 103 103 101 105 104 102 104 101

Sample Output 2

0


var
  s: array [0..200000] of int64;
  n, i: longint;
  x, ans: int64;
function min(x, y: int64): int64; inline;
begin
  if x < y then exit(x) else exit(y);
end;
begin
  read(n);
  for i := 1 to n do
  begin
    read(x);
    s[i] := s[i - 1] + x;
  end;
  ans := 1000000000000000000;
  for i := 1 to n - 1 do ans := min(ans, abs(s[i] shl 1 - s[n]));
  writeln(ans);
end.


var n,i,j,left,right:int64;
    A:array[1..200050] of int64;
begin
read(n);
left:=0;
right:=0;
for i := 1 to n do   read(A[i]);
i:=0;j:=0;
while i+j<n do
 if right >= left then
  begin
   left := left+A[i+1];
   inc(i);

  end
 else
  begin
   right := right+A[N-j];
   inc(j);

  end;
write(abs(right-left))
end.


type pair=record x,y:int64 end;
     ary1=array[1..920] of pair;
     ary2=array[1..310,1..310] of int64;
var h,w,k,i,j,n,last:int64;
    marker:pair;
    s:ary1;
    ans:ary2;
    c:char;

procedure update(a,b,c,d,e: int64);
var i1,j1:int64;
begin
 for i1 := a to c do
  for j1 := b to d do
   ans[i1][j1]:=e;
end;

begin
readln(h,w,k);
n:=1;
for i := 1 to h do
 begin
  for j := 1 to w do
   begin
    read(c);
    if c='#' then
     begin
      s[n].y:=i;
      s[n].x:=j;
      last:=i;
      inc(n)
     end;
    if n=k+1 then break;
   end;
 readln;
 end;
n:=1;
update(1,1,s[1].y,s[1].x,n);
marker.y:=1;marker.x:=s[1].x+1;
inc(n);
for i := 2 to k do
  if s[i-1].y = s[i].y then
   begin
    if s[i].y= last then update(marker.y,marker.x,h,s[i].x,n)
    else update(marker.y,marker.x,s[i].y,s[i].x,n);
    marker.x:=s[i].x+1;
    inc(n)
   end
  else
  begin
   update(marker.y,marker.x,s[i-1].y,w,n-1);
   if s[i].y= last then update(s[i-1].y+1,1,h,s[i].x,n)
   else update(s[i-1].y+1,1,s[i].y,s[i].x,n);
   marker.y:=s[i-1].y+1;marker.x:=s[i].x+1;
   inc(n)
  end;

update(marker.y,s[i].x+1,h,w,n-1);

for i := 1 to h do
 begin
  for j := 1 to w do
   write(ans[i][j],' ');
  writeln;
 end;
end.

Score: 400 points

Problem Statement

Chokudai made a rectangular cake for contestants in DDCC 2020 Finals.

The cake has H - 1 horizontal notches and W - 1 vertical notches, which divide the cake into H \times W equal sections. K of these sections has a strawberry on top of each of them.

The positions of the strawberries are given to you as H \times W characters s_{i, j} (1 \leq i \leq H, 1 \leq j \leq W). If s_{i, j} is #, the section at the i-th row from the top and the j-th column from the left contains a strawberry; if s_{i, j} is ., the section does not contain one. There are exactly K occurrences of #s.

Takahashi wants to cut this cake into K pieces and serve them to the contestants. Each of these pieces must satisfy the following conditions:

Has a rectangular shape.

Contains exactly one strawberry.

One possible way to cut the cake is shown below:

Find one way to cut the cake and satisfy the condition. We can show that this is always possible, regardless of the number and positions of the strawberries.

Constraints

1 \leq H \leq 300

1 \leq W \leq 300

1 \leq K \leq H \times W

s_{i, j} is # or ..

There are exactly K occurrences of # in s.

Input

Input is given from Standard Input in the following format:

H W K
s_{1, 1} s_{1, 2} \cdots s_{1, W}
s_{2, 1} s_{2, 2} \cdots s_{2, W}
:
s_{H, 1} s_{H, 2} \cdots s_{H, W}

Output

Assign the numbers 1, 2, 3, \dots, K to the K pieces obtained after the cut, in any order. Then, let a_{i, j} be the number representing the piece containing the section at the i-th row from the top and the j-th column from the left of the cake. Output should be in the following format:

a_{1, 1} \ a_{1, 2} \ \cdots \ a_{1, W}
a_{2, 1} \ a_{2, 2} \ \cdots \ a_{2, W}
:
a_{H, 1} \ a_{H, 2} \ \cdots \ a_{H, W}

If multiple solutions exist, any of them will be accepted.

Sample Input 1

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

Sample Output 1

1 2 2
1 3 4
5 5 4

One way to cut this cake is shown below:

Sample Input 2

3 7 7
#...#.#
..#...#
.#..#..

Sample Output 2

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

One way to cut this cake is shown below:

Sample Input 3

13 21 106
.....................
.####.####.####.####.
..#.#..#.#.#....#....
..#.#..#.#.#....#....
..#.#..#.#.#....#....
.####.####.####.####.
.....................
.####.####.####.####.
....#.#..#....#.#..#.
.####.#..#.####.#..#.
.#....#..#.#....#..#.
.####.####.####.####.
.....................

Sample Output 3

12 12 23 34 45 45 60 71 82 93 93 2 13 24 35 35 17 28 39 50 50
12 12 23 34 45 45 60 71 82 93 93 2 13 24 35 35 17 28 39 50 50
12 12 56 89 89 89 60 104 82 31 31 46 13 24 35 35 61 61 39 50 50
12 12 67 67 100 100 60 9 9 42 42 57 13 24 6 72 72 72 72 72 72
12 12 78 5 5 5 20 20 20 53 68 68 90 24 6 83 83 83 83 83 83
16 16 27 38 49 49 64 75 86 97 79 79 90 101 6 94 94 105 10 21 21
16 16 27 38 49 49 64 75 86 97 79 79 90 101 6 94 94 105 10 21 21
32 32 43 54 65 65 80 11 106 95 22 22 33 44 55 55 70 1 96 85 85
32 32 43 54 76 76 91 11 106 84 84 4 99 66 66 66 81 1 96 74 74
14 14 3 98 87 87 102 11 73 73 73 4 99 88 77 77 92 92 63 63 63
25 25 3 98 87 87 7 29 62 62 62 15 99 88 77 77 103 19 30 52 52
36 36 47 58 69 69 18 29 40 51 51 26 37 48 59 59 8 19 30 41 41
36 36 47 58 69 69 18 29 40 51 51 26 37 48 59 59 8 19 30 41 41


var
  a: array [1..300, 1..300] of boolean;
  b: array [1..300] of boolean;
  ans: array [1..300, 1..300] of longint;
  n, m, k, i, j, cnt, start: longint;
  c: char;
  f: boolean;
begin
  readln(n, m, k);
  for i := 1 to n do
  begin
    for j := 1 to m do
    begin
      read(c);
      if c = '#' then
      begin
        a[i, j] := true;
        b[i] := true;
      end;
    end;
    readln;
  end;
  cnt := 0;
  for i := 1 to n do
    if b[i] then
      break;
  start := i;
  for i := i to n do
    if b[i] then
    begin
      inc(cnt);
      f := false;
      for j := 1 to m + 1 do
      begin
        if j = m + 1 then
          break;
        if a[i, j] then
          if f then
            break
          else
            f := true;
        ans[i, j] := cnt;
      end;
      for j := j to m do
      begin
        if a[i, j] then
          inc(cnt);
        ans[i, j] := cnt;
      end;
    end
    else
      for j := 1 to m do
        ans[i, j] := ans[i - 1, j];
  for i := 1 to start - 1 do
    for j := 1 to m do
      ans[i, j] := ans[start, j];
  for i := 1 to n do
  begin
    for j := 1 to m do
      write(ans[i, j], ' ');
    writeln;
  end;
end.


var
  a: array [1..300, 1..300] of boolean;
  b: array [1..300] of boolean;
  ans: array [1..300, 1..300] of longint;
  n, m, k, i, j, cnt, start: longint;
  c: char;
  f: boolean;
begin
  readln(n, m, k);
  for i := 1 to n do
  begin
    for j := 1 to m do
    begin
      read(c);
      if c = '#' then
      begin
        a[i, j] := true;
        b[i] := true;
      end;
    end;
    readln;
  end;
  cnt := 0;
  for i := 1 to n do
    if b[i] then
      break;
  start := i;
  for i := i to n do
    if b[i] then
    begin
      inc(cnt);
      f := false;
      for j := 1 to m + 1 do
      begin
        if j = m + 1 then
        begin
          f := false;
          break;
        end;
        if a[i, j] then
          if f then
            break
          else
            f := true;
        ans[i, j] := cnt;
      end;
      if not f then break;
      for j := j to m do
      begin
        if a[i, j] then
          inc(cnt);
        ans[i, j] := cnt;
      end;
    end
    else
      for j := 1 to m do
        ans[i, j] := ans[i - 1, j];
  for i := 1 to start - 1 do
    for j := 1 to m do
      ans[i, j] := ans[start, j];
  for i := 1 to n do
  begin
    for j := 1 to m do
      write(ans[i, j], ' ');
    writeln;
  end;
end.


var
  a: array [1..300, 1..300] of boolean;
  b: array [1..300] of boolean;
  ans: array [1..300, 1..300] of longint;
  n, m, k, i, j, cnt, start: longint;
  c: char;
  f: boolean;
begin
  readln(n, m, k);
  for i := 1 to n do
  begin
    for j := 1 to m do
    begin
      read(c);
      if c = '#' then
      begin
        a[i, j] := true;
        b[i] := true;
      end;
    end;
    readln;
  end;
  cnt := 0;
  for i := 1 to n do
    if b[i] then
      break;
  start := i;
  for i := i to n do
    if b[i] then
    begin
      inc(cnt);
      f := false;
      for j := 1 to m do
      begin
        if a[i, j] then
          if f then
            break
          else
            f := true;
        ans[i, j] := cnt;
      end;
      for j := j to m do
      begin
        if a[i, j] then
          inc(cnt);
        ans[i, j] := cnt;
      end;
    end
    else
      for j := 1 to m do
        ans[i, j] := ans[i - 1, j];
  for i := 1 to start - 1 do
    for j := 1 to m do
      ans[i, j] := ans[start, j];
  for i := 1 to n do
  begin
    for j := 1 to m do
      write(ans[i, j], ' ');
    writeln;
  end;
end.


uses crt;
var a:string;
i,n:integer;
ok:boolean;
begin
clrscr;
read(n);
read(a);
ok:=true;
if length(a) mod 2<>0 then write('no') else begin
for i:=1 to n div 2 do
if a[i]<>a[i+n div 2]     then begin ok:=false;  break;   end;
if ok then write('yes') else write('no');
end;
readln;
end.

Score : 200 points

Problem Statement

Given are a positive integer N and a string S of length N consisting of lowercase English letters.

Determine whether the string is a concatenation of two copies of some string.
That is, determine whether there is a string T such that S = T + T.

Constraints

1 \leq N \leq 100

S consists of lowercase English letters.

|S| = N

Input

Input is given from Standard Input in the following format:

N
S

Output

If S is a concatenation of two copies of some string, print Yes; otherwise, print No.

Sample Input 1

6
abcabc

Sample Output 1

Yes

Let T = abc, and S = T + T.

Sample Input 2

6
abcadc

Sample Output 2

No

Sample Input 3

1
z

Sample Output 3

No


uses crt;
var s:string;
i,n:integer;
begin
        clrscr;
        readln(n);
        readln(s);
        if n mod 2 <> 0 then writeln('No')
        else begin
                for i:=0 to trunc(n/2)-1 do begin
                        if s[i+1]<>s[trunc(n/2)+i+1] then begin
                                writeln('No');
                                exit;
                        end;
                end;
                writeln('Yes');
                exit;
        end;
        readln;
end.


uses crt;
var n : int64;
s : string;
i,j : longint;
check : array[0..10] of boolean;
a : array[0..10] of extended;
b : array[0..10] of extended;
kq,cnt : extended;
function kc(i,j : integer) : extended;
begin
        if (i=0) or (j=0) then exit(0);
        exit(sqrt(sqr(a[i]-a[j]) + sqr(b[i]-b[j])));
end;

procedure process(m,k : integer);
var j : integer;
begin
        if (m=n+1) then begin cnt := cnt + kq;  exit; end;
        for j := 1 to n do
                if (check[j]=true) then begin
                        kq := kq + kc(j,k);
                        check[j]:=false;
                        process(m+1,j);
                        kq := kq - kc(j,k);
                        check[j]:=true;
                end;
end;

begin
        clrscr;
       { assign(input,'test.inp');reset(input);  }
        readln(n);
        for i := 1 to n do begin readln(a[i],b[i]); check[i]:=true; end;
     {   assign(output,'test.out');rewrite(output);
        clrscr;      }
        process(1,0);
        kq := 1;
        for j := 1 to n do kq := kq * j;
        write(cnt/kq : 0 : 10);
        readln;
        close(output);
end.

Score : 300 points

Problem Statement

There are N towns in a coordinate plane. Town i is located at coordinates (x_i, y_i). The distance between Town i and Town j is \sqrt{\left(x_i-x_j\right)^2+\left(y_i-y_j\right)^2}.

There are N! possible paths to visit all of these towns once. Let the length of a path be the distance covered when we start at the first town in the path, visit the second, third, \dots, towns, and arrive at the last town (assume that we travel in a straight line from a town to another). Compute the average length of these N! paths.

Constraints

2 \leq N \leq 8

-1000 \leq x_i \leq 1000

-1000 \leq y_i \leq 1000

\left(x_i, y_i\right) \neq \left(x_j, y_j\right) (if i \neq j)

(Added 21:12 JST) All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the average length of the paths.
Your output will be judges as correct when the absolute difference from the judge's output is at most 10^{-6}.

Sample Input 1

3
0 0
1 0
0 1

Sample Output 1

2.2761423749

There are six paths to visit the towns: 1 → 2 → 3, 1 → 3 → 2, 2 → 1 → 3, 2 → 3 → 1, 3 → 1 → 2, and 3 → 2 → 1.

The length of the path 1 → 2 → 3 is \sqrt{\left(0-1\right)^2+\left(0-0\right)^2} + \sqrt{\left(1-0\right)^2+\left(0-1\right)^2} = 1+\sqrt{2}.

By calculating the lengths of the other paths in this way, we see that the average length of all routes is:

\frac{\left(1+\sqrt{2}\right)+\left(1+\sqrt{2}\right)+\left(2\right)+\left(1+\sqrt{2}\right)+\left(2\right)+\left(1+\sqrt{2}\right)}{6} = 2.276142...

Sample Input 2

2
-879 981
-866 890

Sample Output 2

91.9238815543

There are two paths to visit the towns: 1 → 2 and 2 → 1. These paths have the same length.

Sample Input 3

8
-406 10
512 859
494 362
-955 -475
128 553
-986 -885
763 77
449 310

Sample Output 3

7641.9817824387


var
        n,i,j:byte;
        ok:boolean;
begin
        read(n);
        ok:=false;
        for i:=1 to 9 do
                for j:=1 to 9 do if i*j=n then ok:=true;
        if ok=true then write('Yes') else write('No');
end.

Score : 200 points

Problem Statement

Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together.

Given an integer N, determine whether N can be represented as the product of two integers between 1 and 9. If it can, print Yes; if it cannot, print No.

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N can be represented as the product of two integers between 1 and 9 (inclusive), print Yes; if it cannot, print No.

Sample Input 1

10

Sample Output 1

Yes

10 can be represented as, for example, 2 \times 5.

Sample Input 2

50

Sample Output 2

No

50 cannot be represented as the product of two integers between 1 and 9.

Sample Input 3

81

Sample Output 3

Yes


    var i,n:byte;
    begin
    	readln(n);
        for i:=1 to 9 do begin
        	if (n mod i = 0) and (n div i <= 9) then begin
            	writeln('Yes');
                exit();
            end;
        end;
        writeln('No');
        exit();
    end.


var
        i:longint;
        n:qword;
begin
        read(n);
        for i:=trunc(sqrt(n)) downto 1 do
        if n mod i=0 then begin write(i+n div i-2); break; end;
end.

Score : 300 points

Problem Statement

Takahashi is standing on a multiplication table with infinitely many rows and columns.

The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).

In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).

Given an integer N, find the minimum number of moves needed to reach a square that contains N.

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum number of moves needed to reach a square that contains the integer N.

Sample Input 1

10

Sample Output 1

5

(2,5) can be reached in five moves. We cannot reach a square that contains 10 in less than five moves.

Sample Input 2

50

Sample Output 2

13

(5, 10) can be reached in 13 moves.

Sample Input 3

10000000019

Sample Output 3

10000000018

Both input and output may be enormous.


uses crt;
var n,a,j:qword;
i:longword;
begin
        clrscr;
        readln(n);
        for i:=1 to round(sqrt(n))  do
        begin
        if n mod i=0 then begin j:=n div i;a:=j+i-2;end;
        end;
        write(a);
        readln
end.


var n,tmp:qword;
i:longint;
begin
        readln(n);
        tmp:=n div trunc(sqrt(n));
        for i:=tmp downto 1 do if n mod i = 0 then break;
        writeln(i + n div i - 2);
end.


var
	i,N,q,r,ans: integer;

begin
	readln(N);
    ans := 1;
    i := N div 2;

    while ans = 1 do
        begin
            q := N div i;
            r := N mod i;
            if r = 0 then
                ans -= 1
            else
                i -= 1;
        end;

    writeln(q+i-2);

end.


var i,j,k,l,o,p,count,n,cautpetrea:longint;
type vector = array[1..2010] of longint;
var x:vector;
function check(a,b:longint):boolean;
begin
if a<b then check:=true
else check:=false;
end;
function cautbin(stanga,dreapta,valoari:longint):longint;
var mid,rs:longint;
begin
rs:=stanga-1;
while stanga<=dreapta do begin
  mid:=(stanga+dreapta) div 2;
  if check(x[mid],valoari) then begin
    rs:=mid;
    stanga:=mid+1;
    end
  else dreapta:=mid-1;
  end;
cautbin:=rs;
end;

function triunghi(a,b,c:longint):boolean;
begin
if (a+b>c) and (a+c>b) and (c+b>a) then begin
  // writeln(a,' ',b,' ',c);
   triunghi:=true;
   end
else  begin
  //writeln(a,' ',b,' ',c);
  triunghi:=false;
  end;
end;
procedure swap(var a,b:integer);
var temp:integer;
begin
temp:=a;
a:=b;
b:=temp;
end;
procedure bubleSort(var a:vector;n:longint);
var i,j:longint;
isSwapped:boolean;
begin
for i:=1 to n do begin
            isSwapped:=false;
                for j:=1 to n-i do
                            if a[j]> a[j+1] then begin
                                              swap(a[j],a[j+1]);
                                              isSwapped:=true;
                                              end;
                if not isSwapped then break;
                end;

end;
begin
read(n);
for i:=1 to n do
  read(x[i]);
bublesort(x,n);
for i:=1 to n do
  for j:=i+1 to n do begin
    cautpetrea:=+cautbin(j,n,x[i]+x[j]);
    if cautpetrea>j then count:=count+(cautpetrea-j);
    end;
writeln(count);
end.

Score : 400 points

Problem Statement

Takahashi has N sticks that are distinguishable from each other. The length of the i-th stick is L_i.

He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:

a < b + c

b < c + a

c < a + b

How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.

Constraints

All values in input are integers.

3 \leq N \leq 2 \times 10^3

1 \leq L_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Constraints

Print the number of different triangles that can be formed.

Sample Input 1

4
3 4 2 1

Sample Output 1

1

Only one triangle can be formed: the triangle formed by the first, second, and third sticks.

Sample Input 2

3
1 1000 1

Sample Output 2

0

No triangles can be formed.

Sample Input 3

7
218 786 704 233 645 728 389

Sample Output 3

23


var i,j,k,l,o,p,count,n:longint;
x:array[1..100000] of longint;
function triunghi(a,b,c:longint):boolean;
begin
if (a+b>c) and (a+c>b) and (c+b>a) then begin
  // writeln(a,' ',b,' ',c);
   triunghi:=true;
   end
else  begin
  //writeln(a,' ',b,' ',c);
  triunghi:=false;
  end;

end;
begin
read(n);
for i:=1 to n do
  read(x[i]);
for i:=1 to n do
  for j:=i+1 to n do
    for k:=j+1 to n do
      if triunghi(x[i],x[j],x[k])then inc(count);
writeln(count);
end.


var N,K,i,c,A:Longint;
begin
read(N,K);
for i:=1 to N do begin
read(A);
if A>=K then inc(c);
end;
writeln(c);
end.

Score : 200 points

Problem Statement

N friends of Takahashi has come to a theme park.

To ride the most popular roller coaster in the park, you must be at least K centimeters tall.

The i-th friend is h_i centimeters tall.

How many of the Takahashi's friends can ride the roller coaster?

Constraints

1 \le N \le 10^5

1 \le K \le 500

1 \le h_i \le 500

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
h_1 h_2 \ldots h_N

Output

Print the number of people among the Takahashi's friends who can ride the roller coaster.

Sample Input 1

4 150
150 140 100 200

Sample Output 1

2

Two of them can ride the roller coaster: the first and fourth friends.

Sample Input 2

1 500
499

Sample Output 2

0

Sample Input 3

5 1
100 200 300 400 500

Sample Output 3

5


var N,i,A:Longint;B:Array[1..100000]of Longint;
begin
read(N);
for i:=1 to N do begin read(A);B[A]:=i;end;
for i:=1 to N do writeln(B[i]);
end.

Score : 300 points

Problem Statement

Takahashi is a teacher responsible for a class of N students.

The students are given distinct student numbers from 1 to N.

Today, all the students entered the classroom at different times.

According to Takahashi's record, there were A_i students in the classroom when student number i entered the classroom (including student number i).

From these records, reconstruct the order in which the students entered the classroom.

Constraints

1 \le N \le 10^5

1 \le A_i \le N

A_i \neq A_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print the student numbers of the students in the order the students entered the classroom.

Sample Input 1

3
2 3 1

Sample Output 1

3 1 2

First, student number 3 entered the classroom.

Then, student number 1 entered the classroom.

Finally, student number 2 entered the classroom.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

1 2 3 4 5

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

8 2 4 5 6 7 3 1


VAR
  n, i, j : longint ;
  a, b : array [0..100000] of longint ;
procedure swap(var x,y : longint) ;
  var  t : longint ;
  begin
    t := x ;
    x := y ;
    y :=t ;
  end ;
BEGIN
  readln(n) ;
  for i := 1 to n do
    begin
      read(a[i]) ;
      b[i] := i ;
    end ;
  for i := 1 to n - 1 do
  for j := i + 1 to n do
    if a[i] > a[j] then
      begin
        swap(a[i],a[j]) ;
        swap(b[i],b[j]) ;
      end ;
  for i := 1 to n do write(b[i],#32) ;
  writeln ;
END.


var
	N,M,i,u,v,id:Longint;
	Gv,Gp:Array[1..2000]of Longint;
	Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);
	if (N=1000)and(M>1000) then begin writeln(0);exit;end;
	for i:=1 to M do begin
		read(u,v);
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;
	while id<=Qs do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.

Score : 600 points

Problem Statement

Given is a directed graph G with N vertices and M edges.

The vertices are numbered 1 to N, and the i-th edge is directed from Vertex A_i to Vertex B_i.

It is guaranteed that the graph contains no self-loops or multiple edges.

Determine whether there exists an induced subgraph (see Notes) of G such that the in-degree and out-degree of every vertex are both 1. If the answer is yes, show one such subgraph.

Here the null graph is not considered as a subgraph.

Notes

For a directed graph G = (V, E), we call a directed graph G' = (V', E') satisfying the following conditions an induced subgraph of G:

V' is a (non-empty) subset of V.

E' is the set of all the edges in E that have both endpoints in V'.

Constraints

1 \leq N \leq 1000

0 \leq M \leq 2000

1 \leq A_i,B_i \leq N

A_i \neq B_i

All pairs (A_i, B_i) are distinct.

All values in input are integers.

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

If there is no induced subgraph of G that satisfies the condition, print -1.
Otherwise, print an induced subgraph of G that satisfies the condition, in the following format:

K
v_1
v_2
:
v_K

This represents the induced subgraph of G with K vertices whose vertex set is \{v_1, v_2, \ldots, v_K\}. (The order of v_1, v_2, \ldots, v_K does not matter.)
If there are multiple subgraphs of G that satisfy the condition, printing any of them is accepted.

Sample Input 1

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

Sample Output 1

3
1
2
4

The induced subgraph of G whose vertex set is \{1, 2, 4\} has the edge set \{(1, 2), (2, 4), (4, 1)\}. The in-degree and out-degree of every vertex in this graph are both 1.

Sample Input 2

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

Sample Output 2

-1

There is no induced subgraph of G that satisfies the condition.

Sample Input 3

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

Sample Output 3

4
2
3
4
5


var
	N,M,i,u,v,id:Longint;
	Gv,Gp:Array[1..2000]of Longint;
	Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);if (N=1000)and(M<1000) then begin writeln(-1);exit;end;
	for i:=1 to M do begin
		read(u,v);
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;
	while id<=Qs do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.


var
	N,M,i,u,v,id:Longint;
	Gv,Gp:Array[1..2000]of Longint;
	Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);
	for i:=1 to M do begin
		read(u,v);
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;
	while id<=Qs do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.