uses crt,math;
const   fi='';
        nmax=1999;
        vc=100000000;
type    data=int64;
var
        f:text;
        A:array[0..nmax+1,0..nmax+1] of data;
        Fx,Fy,MatchX,MatchY,tr:array[0..nmax+1] of data;
        Q:array[0..nmax*nmax] of data;
        ddx,ddy:array[0..nmax+1] of boolean;
        dau,cuoi,start,finish:data;
        n:longint;
        mm :data;

procedure docfile;
var     u,v,c:data;
        i,j:longint;
begin
        clrscr;
        readln(n);
        for i:=1 to n do
        begin
                read(u);
                for j:=1 to n do begin a[i,j] := u*abs(i-j); mm := max(mm,a[i,j]); end;
        end;
        for i:=1 to n do
        begin
                for j:=1 to n do a[i,j] := mm - a[i,j];
        end;
end;

procedure init;
var     i,j:longint;
begin
        fillchar(MatchX,sizeof(matchx),0);
        MatchY:=MatchX;

        for i:=1 to n do
                begin
                        fx[i]:=vc;
                        for j:=1 to n do
                                if fx[i]>a[i,j] then
                                        fx[i]:=a[i,j];
                end;
        for i:=1 to n do
                begin
                        fy[i]:=vc;
                        for j:=1 to n do
                                if a[i,j]-fx[i]<fy[i] then
                                        fy[i]:=a[i,j]-fx[i];
                end;
end;

function get(i,j:longint):data;
begin
        get:=a[i,j]-fx[i]-fy[j];
end;

procedure themvao(x:data);
begin
        inc(cuoi);
        q[cuoi]:=x;
end;

function layra:data;
begin
        layra:=q[dau];
        inc(dau);
end;

procedure bfs;
var     u,v,j,i:longint;
begin
        fillchar(tr,sizeof(tr),0);
        dau:=1;
        cuoi:=0;
        themvao(start);
        while dau<=cuoi do
                begin
                        u:=layra;
                        for v:=1 to n do
                                if (tr[v]=0) and (get(u,v)=0) then
                                        begin
                                                tr[v]:=u;
                                                if matchy[v]=0 then
                                                        begin
                                                                finish:=v;
                                                                exit;
                                                        end;
                                                themvao(matchy[v]);
                                        end;
                end;
end;

procedure enlager(u:longint);
var     v,next:longint;
begin
        repeat
                v:=tr[u];
                next:=MatchX[v];
                MatchX[v]:=u;
                MatchY[u]:=v;
                u:=next;
        until   u=0;
end;

procedure subX_addY;
var    delta:data;
        i,j:longint;
begin
        fillchar(ddx,sizeof(ddx),false);
        ddy:=ddx;
        ddx[start]:=true;
        for i:=1 to n do
                if tr[i]<>0 then
                        begin
                                ddx[MatchY[i]]:=true;
                                ddy[i]:=true;
                        end;
        delta:=vc;
        for i:=1 to n do
                if ddx[i] then
                        for j:=1 to n do
                                if (not ddy[j]) and (delta>get(i,j)) then
                                        delta:=get(i,j);
        for i:=1 to n do
                begin
                        if ddx[i] then fx[i]:=fx[i]+delta;
                        if ddy[i] then fy[i]:=fy[i]-delta;
                end;
end;

procedure xuli;
var     res:data;
        i,j:longint;
begin
        for i:=1 to n do
                begin
                        start:=i;
                        finish:=0;
                        repeat
                                bfs;
                                if finish=0 then subx_addy;
                        until finish<>0;
                        enlager(finish);
                end;
        res:=0;
        for i:=1 to n do
                if MatchX[i]<>0 then
                        inc(res,a[i,Matchx[i]]);
        writeln(mm*n - res);
        {for i:=1 to n do
                if MatchX[i]<>0 then
                        writeln(i, ' ',MatchX[i]);   }

end;

begin
        docfile;
        init;
        xuli;
end.


uses crt,math;
const   fi='';
        nmax=200;
        vc=100000000;
type    data=int64;
var
        f:text;
        A:array[0..nmax+1,0..nmax+1] of data;
        Fx,Fy,MatchX,MatchY,tr:array[0..nmax+1] of data;
        Q:array[0..nmax*nmax] of data;
        ddx,ddy:array[0..nmax+1] of boolean;
        dau,cuoi,start,finish:data;
        n:longint;
        mm :data;

procedure docfile;
var     u,v,c:data;
        i,j:longint;
begin
        clrscr;
        readln(n);
        for i:=1 to n do
        begin
                read(u);
                for j:=1 to n do begin a[i,j] := u*abs(i-j); mm := max(mm,a[i,j]); end;
        end;
        for i:=1 to n do
        begin
                for j:=1 to n do a[i,j] := mm - a[i,j];
        end;
end;

procedure init;
var     i,j:longint;
begin
        fillchar(MatchX,sizeof(matchx),0);
        MatchY:=MatchX;

        for i:=1 to n do
                begin
                        fx[i]:=vc;
                        for j:=1 to n do
                                if fx[i]>a[i,j] then
                                        fx[i]:=a[i,j];
                end;
        for i:=1 to n do
                begin
                        fy[i]:=vc;
                        for j:=1 to n do
                                if a[i,j]-fx[i]<fy[i] then
                                        fy[i]:=a[i,j]-fx[i];
                end;
end;

function get(i,j:longint):data;
begin
        get:=a[i,j]-fx[i]-fy[j];
end;

procedure themvao(x:data);
begin
        inc(cuoi);
        q[cuoi]:=x;
end;

function layra:data;
begin
        layra:=q[dau];
        inc(dau);
end;

procedure bfs;
var     u,v,j,i:longint;
begin
        fillchar(tr,sizeof(tr),0);
        dau:=1;
        cuoi:=0;
        themvao(start);
        while dau<=cuoi do
                begin
                        u:=layra;
                        for v:=1 to n do
                                if (tr[v]=0) and (get(u,v)=0) then
                                        begin
                                                tr[v]:=u;
                                                if matchy[v]=0 then
                                                        begin
                                                                finish:=v;
                                                                exit;
                                                        end;
                                                themvao(matchy[v]);
                                        end;
                end;
end;

procedure enlager(u:longint);
var     v,next:longint;
begin
        repeat
                v:=tr[u];
                next:=MatchX[v];
                MatchX[v]:=u;
                MatchY[u]:=v;
                u:=next;
        until   u=0;
end;

procedure subX_addY;
var    delta:data;
        i,j:longint;
begin
        fillchar(ddx,sizeof(ddx),false);
        ddy:=ddx;
        ddx[start]:=true;
        for i:=1 to n do
                if tr[i]<>0 then
                        begin
                                ddx[MatchY[i]]:=true;
                                ddy[i]:=true;
                        end;
        delta:=vc;
        for i:=1 to n do
                if ddx[i] then
                        for j:=1 to n do
                                if (not ddy[j]) and (delta>get(i,j)) then
                                        delta:=get(i,j);
        for i:=1 to n do
                begin
                        if ddx[i] then fx[i]:=fx[i]+delta;
                        if ddy[i] then fy[i]:=fy[i]-delta;
                end;
end;

procedure xuli;
var     res:data;
        i,j:longint;
begin
        for i:=1 to n do
                begin
                        start:=i;
                        finish:=0;
                        repeat
                                bfs;
                                if finish=0 then subx_addy;
                        until finish<>0;
                        enlager(finish);
                end;
        res:=0;
        for i:=1 to n do
                if MatchX[i]<>0 then
                        inc(res,a[i,Matchx[i]]);
        writeln(mm*n - res);
        {for i:=1 to n do
                if MatchX[i]<>0 then
                        writeln(i, ' ',MatchX[i]);   }

end;

begin
        docfile;
        init;
        xuli;
end.

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

Score : 500 points

Problem Statement

There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.

You can rearrange these children just one time in any order you like.

When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.

Find the maximum total happiness points the children can earn.

Constraints

2 \leq N \leq 2000

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum total happiness points the children can earn.

Sample Input 1

4
1 3 4 2

Sample Output 1

20

If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.

Sample Input 2

6
5 5 6 1 1 1

Sample Output 2

58

Sample Input 3

6
8 6 9 1 2 1

Sample Output 3

85

Submitted source-code file size, in bytes.

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

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


var
	N,k:int64;
	i,u,v,es:Longint;
	VV,nxt,beg:Array[1..400000]of Longint;
	ans:Array[1..200000]of int64;
	cnt,c:Array[1..200000]of Longint;
function dfs(u,p:Longint):Longint;
var
	ret,sto,pre,id,t:Longint;
	k:int64;
begin
	ret:=1;
	sto:=cnt[c[u]];
	pre:=sto;
	id:=beg[u];
	while id<>0 do begin
		if VV[id]<>p then begin
			t:=dfs(VV[id],u);
			inc(ret,t);
			k:=t-(cnt[c[u]]-sto);
			sto:=cnt[c[u]];
			dec(ans[c[u]],k*(k+1)div 2);
		end;
		id:=nxt[id];
	end;
	cnt[c[u]]:=pre+ret;
	dfs:=ret;
end;
begin
	read(N);
	for i:=1 to N do read(c[i]);
	for i:=2 to N do begin
		read(u,v);
		inc(es);
		VV[es]:=v;
		nxt[es]:=beg[u];
		beg[u]:=es;
		inc(es);
		VV[es]:=u;
		nxt[es]:=beg[v];
		beg[v]:=es;
	end;
	dfs(1,0);
	for i:=1 to N do begin
		inc(ans[i],N*(N+1)div 2);
		k:=N-cnt[i];
		dec(ans[i],k*(k+1)div 2);
		writeln(ans[i]);
	end;
end.

Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i.
Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.

For each k=1, 2, ..., N, solve the following problem:

Find the number of simple paths that visit a vertex painted in the color k one or more times.

Note: The simple paths from Vertex u to v and from v to u are not distinguished.

Constraints

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

1 \leq c_i \leq N

1 \leq a_i,b_i \leq N

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

Print the answers for k = 1, 2, ..., N in order, each in its own line.

Sample Input 1

3
1 2 1
1 2
2 3

Sample Output 1

5
4
0

Let P_{i,j} denote the simple path connecting Vertex i and j.

There are 5 simple paths that visit a vertex painted in the color 1 one or more times:

P_{1,1}\,,\,
P_{1,2}\,,\,
P_{1,3}\,,\,
P_{2,3}\,,\,
P_{3,3}

There are 4 simple paths that visit a vertex painted in the color 2 one or more times:

P_{1,2}\,,\,
P_{1,3}\,,\,
P_{2,2}\,,\,
P_{2,3}

There are no simple paths that visit a vertex painted in the color 3 one or more times.

Sample Input 2

1
1

Sample Output 2

1

Sample Input 3

2
1 2
1 2

Sample Output 3

2
2

Sample Input 4

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

Sample Output 4

5
8
10
5
5

Sample Input 5

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

Sample Output 5

18
15
0
14
23
0
23
0


program main;
var n, i, sum : longint;

begin
  readln(n);
  sum := 0;
  for i := 1 to n do
    if not (i mod 3 = 0) or (i mod 5 = 0)
      then sum := sum + i;
  writeln(sum);
end.

Score : 200 points

Problem Statement

Let us define the FizzBuzz sequence a_1,a_2,... as follows:

If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.

If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.

If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.

If none of the above holds, a_i=i.

Find the sum of all numbers among the first N terms of the FizzBuzz sequence.

Constraints

1 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of all numbers among the first N terms of the FizzBuzz sequence.

Sample Input 1

15

Sample Output 1

60

The first 15 terms of the FizzBuzz sequence are:

1,2,\mbox{Fizz},4,\mbox{Buzz},\mbox{Fizz},7,8,\mbox{Fizz},\mbox{Buzz},11,\mbox{Fizz},13,14,\mbox{FizzBuzz}

Among them, numbers are 1,2,4,7,8,11,13,14, and the sum of them is 60.

Sample Input 2

1000000

Sample Output 2

266666333332

Watch out for overflow.


program main;
var n, i, sum : int64;

begin
  readln(n);
  sum := 0;
  for i := 1 to n do begin
    if not (i mod 3 = 0) or (i mod 5 = 0)
      then sum := sum + i;
  end;
  writeln(sum);
end.


program main;
var n, i, sum : int64;

begin
  readln(n);
  sum := 0;
  for i := 1 to n do
    if not (i mod 3 = 0) or (i mod 5 = 0)
      then sum := sum + i;
  writeln(sum);
end.


program main;

var
	n, i, sum : int64;

begin
	sum := 0;
	readln(n);
    for i := 1 to n do begin
    	if (i mod 3 <> 0) and (i mod 5 <> 0)
    		then sum := sum + i;
    end;
    writeln(sum);
end.


program main;
var
    n, i : longint;
    sum : int64;
begin
    readln(n);

    sum :=0;
    for i := 1 to n do
        if not ((i mod 3 = 0)or (i mod 5 = 0)) then
            sum := sum + i;

    writeln(sum);
end.


program hihi;
var
	n, i : longint;
    sum : int64;
begin
	readln(n);
    sum:=0;
    for i:=1 to n do
    	if not ((i mod 3 =0) or (i mod 5 = 0))
        then sum := sum + i;
    writeln(sum);
end.


program hihi;
var
	n, i : LongInt;
    sum : Int64;
begin
	readln(n);
    sum:=0;
	for i:=1 to n do
    	if (i mod 3 <> 0) or (i mod 5 <> 0)
        then sum := sum + i;
    writeln(sum);
end.


program main;
var x,i : longint;
    sum : int64;
begin
    sum:=0;
    readln(x);
    for i := 1 to x do begin
        sum := sum + i;
        if i mod 3 = 0
        then sum := sum - i
            else if i mod 5 = 0
                then sum := sum - i;
    end;
    writeln(sum);
end.


program main;
var N, i : LongInt;
    c, b : real;
    A : array[1..1000000] of LongInt;
    sum : int64;
begin
	read(N);
    sum := 0;
    for i := 1 to N do begin
        c := frac(i / 3);
        b := frac(i / 5);
        if (c = 0) or (b = 0)
        then A[i] := 0
        else A[i] := i;
        sum := sum + A[i];
    end;
    writeln(sum);
end.


program main;
var
	num, i, sum : int64;
begin
	read(num);

    sum := 0;

    for i := 1 to num do begin
    	if (frac(i/3) = 0) or (frac(i/5) = 0)
        	then sum := sum + 0
        	    else sum := sum + i;
    end;
 	writeln(sum);
end.


program main;
var
    i,j,k:longint;
    n:longint;
    sum:longint;
begin
    readln(n);
    sum:=0;
    for i:=1 to n do if ((i mod 3) <> 0) and ((i mod 5) <> 0) then sum:=sum+i;
    writeln(sum);
end.


var N,i:Longint;ans:int64;
begin
read(N);
for i:=1 to N do if(i mod 3<>0)and(i mod 5<>0)then inc(ans,i);
writeln(ans);
end.


var
	N,i,a,j,ni,nj:Longint;
	dp:Array[0..200000,0..3]of int64;
begin
read(N);
for i:=0 to N do for j:=0 to 2 do dp[i,j]:=-1000000000000000000;
dp[0,0]:=0;
for i:=0 to N-1 do begin
read(a);
for j:=0 to 2 do begin
if dp[i+1,j+1]<dp[i,j]then dp[i+1,j+1]:=dp[i,j];
ni:=i+2;if ni>N then ni:=N;
nj:=j;if i+2=N then inc(nj);
if dp[ni,nj]<dp[i,j]+a then dp[ni,nj]:=dp[i,j]+a;
end;
end;
writeln(dp[N,1+N mod 2]);
end.

Score : 600 points

Problem Statement

Given is an integer sequence A_1, ..., A_N of length N.

We will choose exactly \left\lfloor \frac{N}{2} \right\rfloor elements from this sequence so that no two adjacent elements are chosen.

Find the maximum possible sum of the chosen elements.

Here \lfloor x \rfloor denotes the greatest integer not greater than x.

Constraints

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

|A_i|\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the maximum possible sum of the chosen elements.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

12

Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value.

Sample Input 2

5
-1000 -100 -10 0 10

Sample Output 2

0

Choosing -10 and 10 makes the sum 0, which is the maximum possible value.

Sample Input 3

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

Watch out for overflow.

Sample Input 4

27
18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49

Sample Output 4

295


program main;
var n,k : int64;
begin
    readln(n,k);
    if abs(n mod k) < abs((n mod k)-k)
    	then writeln(abs(n mod k))
    		else writeln(abs((n mod k)-k));
end.

Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0


program main;
var
n, k, i, min, j : int64;

begin
	read(n, k);

    min := 1000000000000000000;
    for i := 1 to n do begin
    n := abs(n-k);
    if n < min
        then min := n;
    end;

    writeln(min);
end.


program main;
var
    a,n,m : int64;
begin
    readln(n,m);
    a := n mod m;
    if abs(a-m) < a
        then writeln(abs(a-m))
        else writeln(a);
end.


program ReplacingInteger;
var
    x, y, z : int64;
begin
    readln(x, y);
    if (x > y) then begin
        if (x mod y = 0) then
            writeln(0)
            else begin
                z := x div y;
                if (z <  abs(z - y)) then
                    writeln(z)
                    else
                        writeln(abs(z - y));
            end;
    end
    else begin
        if (x < y) then
            writeln(abs(x))
            else if (x = y) then
                writeln(0);
        end;
end.


program ReplacingInteger;
var
    x, y : int64;
    z : Longint;
begin
    readln(x, y);
    if (x > y) then begin
        if (x mod y = 0) then
            writeln(0)
            else begin
                z := x div y;
                if (z <  abs(z - y)) then
                    writeln(z)
                    else
                        writeln(abs(z - y));
            end;
    end
    else begin
        if (x < y) then
            writeln(abs(x))
            else if (x = y) then
                writeln(0);
        end;
end.


program ReplacingInteger;
var
    x, y : int64;
    z : Longint;
begin
    readln(x, y);
    if (x > y) then begin
        if (x mod y = 0) then
            writeln(0)
            else begin
                z := x div y;
                if (z < x mod y) then
                    writeln(z)
                    else
                        writeln(x mod y);
            end;
    end
    else begin
        if (x < y) then
            writeln(2)
            else if (x = y) then
                writeln(1);
        end;
end.


var
	K:Longint;
	L,R,M:int64;
function check(T:int64):Longint;
var
	dp:Array[1..15,0..1,0..9]of Longint;
	dpz:Array[1..15,0..1]of Longint;
	S:String;
	ls,i,j,k,l,nj,lim,sum,llim,rlim:Longint;
begin
	S:='';
	while T>0 do begin
		S:=Chr(T mod 10+48)+S;
		T:=T div 10;
	end;
	ls:=length(S);
	for i:=1 to ls+1 do for j:=0 to 1 do begin
		for k:=0 to 9 do dp[i,j,k]:=0;
		dpz[i,j]:=0;
	end;
	dpz[1,0]:=1;
	for i:=1 to ls do begin
		for j:=0 to 1 do begin
			if j=0 then lim:=ord(S[i])-48 else lim:=9;
			inc(dpz[i+1,1],dpz[i,j]);
			for l:=1 to lim do begin
				nj:=j;
				if l<lim then nj:=1;
				inc(dp[i+1,nj,l],dpz[i,j]);
			end;
			for k:=0 to 9 do begin
				llim:=k-1;if llim<0 then llim:=0;
				rlim:=k+1;if rlim>lim then rlim:=lim;
				for l:=llim to rlim do begin
					nj:=j;
					if l<lim then nj:=1;
					inc(dp[i+1,nj,l],dp[i,j,k]);
				end;
			end;
		end;
	end;
	sum:=0;
	for k:=0 to 9 do for i:=0 to 1 do begin
		inc(sum,dp[ls+1,i,k]);
	end;
	check:=sum;
end;
begin
	read(K);
	L:=0;
	R:=3234566667;
	while R-L>1 do begin
		M:=(L+R)div 2;
		if check(M)>=K then R:=M else L:=M;
	end;
	writeln(R);
end.

Score : 400 points

Problem Statement

A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:

In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.

For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.

You are given a positive integer K. Find the K-th smallest lunlun number.

Constraints

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer.

Sample Input 1

15

Sample Output 1

23

We will list the 15 smallest lunlun numbers in ascending order:

1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
21,
22,
23.

Thus, the answer is 23.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

13

Sample Output 3

21

Sample Input 4

100000

Sample Output 4

3234566667

Note that the answer may not fit into the 32-bit signed integer type.


var
	K:Longint;
	L,R,M:int64;
function check(T:int64):Longint;
var
	dp:Array[1..15,0..1,0..1,0..9]of Longint;
	S:String;
	i,j,k,l,nj,lim,sum,z:Longint;
begin
	Str(T,S);
	for i:=1 to length(S)+1 do for j:=0 to 1 do for z:=0 to 1 do for k:=0 to 9 do dp[i,j,z,k]:=0;
	dp[1,0,1,0]:=1;
	for i:=1 to length(S) do begin
		for j:=0 to 1 do begin
			if j=0 then lim:=ord(S[i])-48 else lim:=9;
			inc(dp[i+1,1,1,0],dp[i,j,1,0]);
			for l:=1 to lim do begin
				nj:=j;
				if l<lim then nj:=1;
				inc(dp[i+1,nj,0,l],dp[i,j,1,0]);
			end;
			for k:=0 to 9 do begin
				for l:=0 to lim do begin
					if(k<l-1)or(l<k-1)then continue;
					nj:=j;
					if l<lim then nj:=1;
					inc(dp[i+1,nj,0,l],dp[i,j,0,k]);
				end;
			end;
		end;
	end;
	sum:=0;
	for k:=0 to 9 do for i:=0 to 1 do begin
		inc(sum,dp[length(S)+1,i,0,k]);
	end;
	check:=sum;
end;
begin
	read(K);
	L:=0;
	R:=3234566667;
	while R-L>1 do begin
		M:=(L+R)div 2;
		if check(M)>=K then R:=M else L:=M;
	end;
	writeln(R);
end.


var
	K:Longint;
	L,R,M:int64;
function check(T:int64):Longint;
var
	dp:Array[1..15,0..1,0..1,0..9]of Longint;
	S:String;
	i,j,k,l,nj,lim,sum,z,nz:Longint;
begin
	Str(T,S);
	for i:=1 to length(S)+1 do for j:=0 to 1 do for z:=0 to 1 do for k:=0 to 9 do dp[i,j,z,k]:=0;
	dp[1,0,1,0]:=1;
	for i:=1 to length(S) do begin
		for j:=0 to 1 do begin
			if j=0 then lim:=ord(S[i])-48 else lim:=9;
			for z:=0 to 1 do for k:=0 to 9 do begin
				for l:=0 to lim do begin
					if(z<>1)and((k<l-1)or(l<k-1))then continue;
					nj:=j;
					if l<lim then nj:=1;
					nz:=0;
					if(z=1)and(l=0)then nz:=1;
					inc(dp[i+1,nj,nz,l],dp[i,j,z,k]);
				end;
			end;
		end;
	end;
	sum:=0;
	for k:=0 to 9 do for i:=0 to 1 do begin
		inc(sum,dp[length(S)+1,i,0,k]);
	end;
	check:=sum;
end;
begin
	read(K);
	L:=1;
	R:=3234566667;
	while R-L>1 do begin
		M:=(L+R)div 2;
		if check(M)>=K then R:=M else L:=M;
	end;
	writeln(R);
end.


var
	S:String;
	N,K,C,i,now:Longint;
	L,R:Array[1..200000]of Longint;
begin
	readln(N,K,C);
	readln(S);
	now:=0;i:=1;
	while i<=N do begin
		if S[i]='o'then begin
			inc(now);
			L[i]:=now;
			inc(i,C);
		end;
		inc(i);
	end;
	now:=0;i:=N;
	while i>=1 do begin
		if S[i]='o'then begin
			dec(now);
			R[i]:=now;
			dec(i,C);
		end;
		dec(i);
	end;
	for i:=1 to N do if(L[i]>0)and(R[i]<0)and(L[i]=K+1+R[i])then writeln(i);
end.

Score : 500 points

Problem Statement

Takahashi has decided to work on K days of his choice from the N days starting with tomorrow.

You are given an integer C and a string S. Takahashi will choose his workdays as follows:

After working for a day, he will refrain from working on the subsequent C days.

If the i-th character of S is x, he will not work on Day i, where Day 1 is tomorrow, Day 2 is the day after tomorrow, and so on.

Find all days on which Takahashi is bound to work.

Constraints

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

1 \leq K \leq N

0 \leq C \leq N

The length of S is N.

Each character of S is o or x.

Takahashi can choose his workdays so that the conditions in Problem Statement are satisfied.

Input

Input is given from Standard Input in the following format:

N K C
S

Output

Print all days on which Takahashi is bound to work in ascending order, one per line.

Sample Input 1

11 3 2
ooxxxoxxxoo

Sample Output 1

6

Takahashi is going to work on 3 days out of the 11 days. After working for a day, he will refrain from working on the subsequent 2 days.

There are four possible choices for his workdays: Day 1,6,10, Day 1,6,11, Day 2,6,10, and Day 2,6,11.

Thus, he is bound to work on Day 6.

Sample Input 2

5 2 3
ooxoo

Sample Output 2

1
5

There is only one possible choice for his workdays: Day 1,5.

Sample Input 3

5 1 0
ooooo

Sample Output 3

There may be no days on which he is bound to work.

Sample Input 4

16 4 3
ooxxoxoxxxoxoxxo

Sample Output 4

11
16


var K,N,i,t,v:Longint;
A:Array[1..200000]of Longint;
begin
read(K,N);
for i:=1 to N do read(A[i]);
for i:=2 to N do begin
v:=A[i]-A[i-1];
if t<v then t:=v;
end;
v:=K+A[1]-A[N];
if t<v then t:=v;
writeln(K-t);
end.

Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

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

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.


uses math;
var s:array[0..100001] of longint;
  k,n,a,b,c,d,e:longint;
begin
  readln(k,n);
  for a:=1 to n do read(s[a]);
  e:=maxlongint;
  s[0]:=s[n]-k;s[n+1]:=s[1]+k;
  for a:=1 to n do e:=min(min(k-s[a]+s[a-1],k-s[a+1]+s[a]),e);
  writeln(e);
end.


uses math;
var
	N,X,Y,loop,k,ans,z:Longint;
begin
	read(N,X,Y);
	loop:=Y-X+1;
	for k:=1 to N-1 do begin
		ans:=0;
		{inc(ans,max(0,X-k));
		inc(ans,max(0,N+1-Y-k));}
		z:=min(X-1,k-1);
		if k-z<=loop div 2 then begin
			inc(ans,(z-max(0,2*k-loop-1)div 2)*2);
			if(loop mod 2=0)and(loop div 2<k)and(k-loop div 2<X)then dec(ans);
		end;
		z:=min(N-Y,k-1);
		if k-z<=loop div 2 then begin
			inc(ans,(z-max(0,2*k-loop-1)div 2)*2);
			if(loop mod 2=0)and(loop div 2<k)and(k-loop div 2<N+1-Y)then dec(ans);
		end;
		if N+1+X-Y>k then inc(ans,N+1+X-Y-k);
		if k=1 then dec(ans)else begin
			if X-1>=k-1 then dec(ans);
			if N-Y>=k-1 then dec(ans);
		end;
		if k*2<loop then begin
			inc(ans,loop);
		end else if k*2=loop then begin
			inc(ans,loop div 2);
		end;
		writeln(ans);
	end;
end.

Score : 400 points

Problem Statement

We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:

For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1.

There is an edge between Vertex X and Vertex Y.

For each k=1,2,...,N-1, solve the problem below:

Find the number of pairs of integers (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j in G is k.

Constraints

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

1 \leq X,Y \leq N

X+1 < Y

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

For each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem.

Sample Input 1

5 2 4

Sample Output 1

5
4
1
0

The graph in this input is as follows:

There are five pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\,,(2,3)\,,(2,4)\,,(3,4)\,,(4,5).

There are four pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\,,(1,4)\,,(2,5)\,,(3,5).

There is one pair (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5).

There are no pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 4.

Sample Input 2

3 1 3

Sample Output 2

3
0

The graph in this input is as follows:

Sample Input 3

7 3 7

Sample Output 3

7
8
4
2
0
0

Sample Input 4

10 4 8

Sample Output 4

10
12
10
8
4
1
0
0
0


uses math;
var
	N,X,Y,loop,k,ans,z:Longint;
begin
	read(N,X,Y);
	loop:=Y-X+1;
	for k:=1 to N-1 do begin
		ans:=0;
		{inc(ans,max(0,X-k));
		inc(ans,max(0,N+1-Y-k));}
		z:=min(X-1,k-1);
		if k-z<=loop div 2 then begin
			inc(ans,(z-max(0,2*k-loop)div 2)*2);
			if(loop mod 2=0)and(loop div 2<k)and(k-loop div 2<X)then dec(ans);
		end;
		z:=min(N-Y,k-1);
		if k-z<=loop div 2 then begin
			inc(ans,(z-max(0,2*k-loop)div 2)*2);
			if(loop mod 2=0)and(loop div 2<k)and(k-loop div 2<N+1-Y)then dec(ans);
		end;
		if N+1+X-Y>k then inc(ans,N+1+X-Y-k);
		if k=1 then dec(ans)else begin
			if X-1>=k-1 then dec(ans);
			if N-Y>=k-1 then dec(ans);
		end;
		if k*2<loop then begin
			inc(ans,loop);
		end else if k*2=loop then begin
			inc(ans,loop);
			dec(ans);
		end;
		writeln(ans);
	end;
end.


const M=1000000007;
var
	N,i,a,b:Longint;
	c,dp,ans:Array[1..200000]of int64;
	fac,invfac:Array[0..199999]of int64;
	V,nxt:Array[1..399998]of Longint;
	beg:Array[1..200000]of Longint;
function power(a,b:int64):int64;
begin
	if b=0 then power:=1 else if b mod 2=0 then power:=power(a*a mod M,b div 2)else power:=power(a*a mod M,b div 2)*a mod M;
end;
function dfs1(u,p:Longint):int64;
var
	id:Longint;
	ret:int64;
begin
	ret:=1;
	c[u]:=1;
	id:=beg[u];
	while id<>0 do begin
		if V[id]<>p then begin
			ret:=ret*dfs1(V[id],u)mod M;
			ret:=ret*invfac[c[V[id]]]mod M;
			inc(c[u],c[V[id]]);
		end;
		id:=nxt[id];
	end;
	dp[u]:=ret;
	dfs1:=ret*fac[c[u]-1]mod M;
end;
procedure dfs2(u,p:Longint;pr:int64);
var id:Longint;
begin
	ans[u]:=dp[u]*pr mod M*invfac[N-c[u]]mod M;
	id:=beg[u];
	while id<>0 do begin
		if V[id]<>p then begin
			dfs2(V[id],u,ans[u]*c[V[id]]mod M*power(dp[V[id]],M-2)mod M*fac[N-c[V[id]]-1]mod M);
		end;
		id:=nxt[id];
	end;
end;
begin
	read(N);
	fac[0]:=1;
	for i:=1 to N-1 do fac[i]:=fac[i-1]*i mod M;
	invfac[N-1]:=power(fac[N-1],M-2);
	for i:=N-2 downto 0 do invfac[i]:=invfac[i+1]*(i+1)mod M;
	for i:=1 to N-1 do begin
		read(a,b);
		V[i*2-1]:=b;
		nxt[i*2-1]:=beg[a];
		beg[a]:=i*2-1;
		V[i*2]:=a;
		nxt[i*2]:=beg[b];
		beg[b]:=i*2;
	end;
	dfs1(1,0);
	dfs2(1,0,1);
	for i:=1 to N do writeln(ans[i]*fac[N-1]mod M);
end.

Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i.
For each k=1, ..., N, solve the problem below:

Consider writing a number on each vertex in the tree in the following manner:

First, write 1 on Vertex k.

Then, for each of the numbers 2, ..., N in this order, write the number on the vertex chosen as follows:

Choose a vertex that still does not have a number written on it and is adjacent to a vertex with a number already written on it. If there are multiple such vertices, choose one of them at random.

Find the number of ways in which we can write the numbers on the vertices, modulo (10^9+7).

Constraints

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

1 \leq a_i,b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

For each k=1, 2, ..., N in this order, print a line containing the answer to the problem.

Sample Input 1

3
1 2
1 3

Sample Output 1

2
1
1

The graph in this input is as follows:

For k=1, there are two ways in which we can write the numbers on the vertices, as follows:

Writing 1, 2, 3 on Vertex 1, 2, 3, respectively

Writing 1, 3, 2 on Vertex 1, 2, 3, respectively

Sample Input 2

2
1 2

Sample Output 2

1
1

The graph in this input is as follows:

Sample Input 3

5
1 2
2 3
3 4
3 5

Sample Output 3

2
8
12
3
3

The graph in this input is as follows:

Sample Input 4

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

Sample Output 4

40
280
840
120
120
504
72
72

The graph in this input is as follows:


var n, i : longint;
    a, cnt : array[1..200005] of int64;
    has : array[1..200005] of boolean;
    tot : int64;

begin
    readln(n);
    fillchar(cnt, sizeof(cnt), 0);
    for i := 1 to n do
    begin
        read(a[i]);
        inc(cnt[a[i]]);
    end;
    tot := 0;
    fillchar(has, sizeof(has), false);
    for i := 1 to n do
    begin
        if (not has[a[i]]) then
        begin
            tot := tot + cnt[a[i]] * (cnt[a[i]] - 1) div 2;
            has[a[i]] := true;
        end;
    end;
    for i := 1 to n do
    begin
        writeln(tot - cnt[a[i]] * (cnt[a[i]] - 1) div 2 + (cnt[a[i]] - 1) * (cnt[a[i]] - 2) div 2);
    end;
end.

Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

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

1 \leq A_i \leq N

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

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5


Var a,b,c:Array[1 .. 200000] of Longint;
    i,n,sum,r1,r2:Int64;
Begin
    Readln(n);
    For i:=1 to n do begin
        Read(a[i]);
        Inc(b[a[i]]);
    End;
    For i:=1 to n do c[i]:=(b[i]*(b[i]-1) div 2);
    For i:=1 to n do sum:=sum+c[i];
    For i:=1 to n do begin
        If c[a[i]]=0 then begin
            Writeln(sum); Continue;
        End;
        r1:=b[a[i]]-1; r2:=b[a[i]]-2;
        Writeln(sum-c[a[i]]+(r1*r2 div 2));
    End;
End.


var
	s:Array[0..9]of String;
	H,W,K,i,j,l,z,now,id,ans:Longint;
	cnt:Array[0..14]of Longint;
	out,take:Boolean;
begin
	readln(H,W,K);
	for i:=0 to H-1 do readln(s[i]);
	ans:=1000000000;
	for z:=0 to (1<<(H-1))-1 do begin
		for j:=0 to 14 do cnt[j]:=0;
		now:=0;
		for j:=0 to H-1 do if ((z>>j)and 1)=1 then inc(now);
		out:=False;
		for j:=0 to W-1 do begin
			id:=0;
			take:=False;
			for l:=0 to H-1 do begin
				if s[l][j+1]='1'then inc(cnt[id]);
				if cnt[id]>K then take:=True;
				if ((z>>l)and 1)=1 then inc(id);
			end;
			if take then begin
				inc(now);
				for l:=0 to id do cnt[l]:=0;
				id:=0;
				for l:=0 to H-1 do begin
					if s[l][j+1]='1'then inc(cnt[id]);
					if cnt[id]>K then out:=True;
					if((z>>k)and 1)=1 then inc(id);
				end;
			end;
		end;
		if(not out)and(ans>now)then ans:=now;
	end;
	writeln(ans);
end.

Score : 500 points

Problem Statement

We have a chocolate bar partitioned into H horizontal rows and W vertical columns of squares.

The square (i, j) at the i-th row from the top and the j-th column from the left is dark if S_{i,j} is 0, and white if S_{i,j} is 1.

We will cut the bar some number of times to divide it into some number of blocks. In each cut, we cut the whole bar by a line running along some boundaries of squares from end to end of the bar.

How many times do we need to cut the bar so that every block after the cuts has K or less white squares?

Constraints

1 \leq H \leq 10

1 \leq W \leq 1000

1 \leq K \leq H \times W

S_{i,j} is 0 or 1.

Input

Input is given from Standard Input in the following format:

H W K
S_{1,1}S_{1,2}...S_{1,W}
:
S_{H,1}S_{H,2}...S_{H,W}

Output

Print the number of minimum times the bar needs to be cut so that every block after the cuts has K or less white squares.

Sample Input 1

3 5 4
11100
10001
00111

Sample Output 1

2

For example, cutting between the 1-st and 2-nd rows and between the 3-rd and 4-th columns - as shown in the figure to the left - works.

Note that we cannot cut the bar in the ways shown in the two figures to the right.

Sample Input 2

3 5 8
11100
10001
00111

Sample Output 2

0

No cut is needed.

Sample Input 3

4 10 4
1110010010
1000101110
0011101001
1101000111

Sample Output 3

3


var
	N,S,i,j,A,ans:Longint;
	dp:Array[0..3001]of Longint;
begin
	read(N,S);
	for i:=1 to N do begin
		dp[S]:=i;
		read(A);
		for j:=A to S do begin
			inc(dp[j-A],dp[j]);
			if dp[j-A]>=998244353 then dec(dp[j-A],998244353);
		end;
		inc(ans,dp[0]);
		if ans>=98244353 then dec(ans,998244353);
	end;
	writeln(ans);
end.

Score : 600 points

Problem Statement

Given are a sequence of N integers A_1, A_2, \ldots, A_N and a positive integer S.

For a pair of integers (L, R) such that 1\leq L \leq R \leq N, let us define f(L, R) as follows:

f(L, R) is the number of sequences of integers (x_1, x_2, \ldots , x_k) such that L \leq x_1 < x_2 < \cdots < x_k \leq R and A_{x_1}+A_{x_2}+\cdots +A_{x_k} = S.

Find the sum of f(L, R) over all pairs of integers (L, R) such that 1\leq L \leq R\leq N. Since this sum can be enormous, print it modulo 998244353.

Constraints

All values in input are integers.

1 \leq N \leq 3000

1 \leq S \leq 3000

1 \leq A_i \leq 3000

Input

Input is given from Standard Input in the following format:

N S
A_1 A_2 ... A_N

Output

Print the sum of f(L, R), modulo 998244353.

Sample Input 1

3 4
2 2 4

Sample Output 1

5

The value of f(L, R) for each pair is as follows, for a total of 5.

f(1,1) = 0

f(1,2) = 1 (for the sequence (1, 2))

f(1,3) = 2 (for (1, 2) and (3))

f(2,2) = 0

f(2,3) = 1 (for (3))

f(3,3) = 1 (for (3))

Sample Input 2

5 8
9 9 9 9 9

Sample Output 2

0

Sample Input 3

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

Sample Output 3

152


uses math;
var n,m,i,j:longint;
    a:array[0..100,0..100]of longint;
    c:char;
begin
  fillchar(a,sizeof(a),$3f);
  a[1,0]:=0;
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      read(c);
      a[i,j]:=ord(c='#')+min(a[i-1,j],a[i,j-1]);
    end;
    readln;
  end;
  writeln(a[n,m]);
end.

Score : 400 points

Problem Statement

Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left.
Each square is painted black or white.

The grid is said to be good if and only if the following condition is satisfied:

From (1, 1), we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square.

Note that (1, 1) and (H, W) must be white if the grid is good.

Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.

Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W). For each pair r, c (r_0 \leq r \leq r_1, c_0 \leq c \leq c_1), invert the color of (r, c) - that is, from white to black and vice versa.

Constraints

2 \leq H, W \leq 100

Input

Input is given from Standard Input in the following format:

H W
s_{11} s_{12} \cdots s_{1W}
s_{21} s_{22} \cdots s_{2W}
\vdots
s_{H1} s_{H2} \cdots s_{HW}

Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white.

Output

Print the minimum number of operations needed.

Sample Input 1

3 3
.##
.#.
##.

Sample Output 1

1

Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2), and we are done.

Sample Input 2

2 2
#.
.#

Sample Output 2

2

Sample Input 3

4 4
..##
#...
###.
###.

Sample Output 3

0

No operation may be needed.

Sample Input 4

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

Sample Output 4

4


var
	H,W,i,j,x,y:Longint;
	S:Array[1..100]of String;
	dp:Array[1..100,1..100]of Longint;
begin
	readln(H,W);
	for i:=1 to H do begin
		readln(S[i]);
		for j:=1 to W do begin
			x:=100000;
			y:=100000;
			if i>1 then begin
				x:=dp[i-1,j];
				if(S[i-1][j]='.')and(S[i][j]='#')then inc(x);
			end;
			if j>1 then begin
				y:=dp[i,j-1];
				if(S[i][j-1]='.')and(S[i][j]='#')then inc(y);
			end;
			if x<y then dp[i,j]:=x else dp[i,j]:=y;
			if(i=1)and(j=1)then begin
				if S[i][j]='#'then dp[i,j]:=1 else dp[i,j]:=0;
			end;
		end;
	end;
	writeln(dp[H,W]);
end.


var
	Q,i,j:Longint;
	T:Array[0..30]of int64;
	a,b,c,d,aa,bb,cc,dd,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
				bb:=b div T[j-1];
				dd:=d div T[j-1];
				if bb>dd then begin
					tmp:=bb;
					bb:=dd;
					dd:=tmp;
				end;
				while bb mod 3<>1 do inc(bb);
				if bb<=dd 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
				aa:=a div T[j-1];
				cc:=c div T[j-1];
				if aa>cc then begin
					tmp:=aa;
					aa:=cc;
					cc:=tmp;
				end;
				while aa mod 3<>1 do inc(aa);
				if aa<=cc 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


{$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 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.

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


var
	S:Array[-200000..300000]of Char;
	ins:String;
	i,Q,d,id,F,l,r:Longint;
	c:Char;
begin
	readln(ins);
	for i:=1 to length(ins)do S[i]:=ins[i];
	r:=length(ins);
	l:=1;
	read(Q);
	for i:=1 to Q do begin
		read(id);
		if id=1 then d:=1-d else begin
			read(F,c,c);
			if (d+F)mod 2=0 then begin
				inc(r);
				S[r]:=c;
			end else begin
				dec(l);
				S[l]:=c;
			end;
		end;
	end;
	if d=0 then for i:=l to r do write(S[i])else for i:=r downto l do write(S[i]);
	writeln;
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


var
	S:String;
	N,P,w,k,i,t:Longint;
	cnt:Array[0..9999]of Longint;
	ans:int64;
begin
	readln(N,P);
	readln(S);
	cnt[0]:=1;
	k:=1;
	for i:=N downto 1 do begin
		t:=ord(S[i])-48;
		k:=k*10 mod P;
		if k=0 then if t mod p=0 then inc(ans,i)else else begin
			w:=(w+k*t)mod P;
			inc(ans,cnt[w]);
			inc(cnt[w]);
		end;
	end;
	writeln(ans);
end.

Score : 500 points

Problem Statement

Takahashi has a string S of length N consisting of digits from 0 through 9.

He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.

Here substrings starting with a 0 also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.

Compute this count to help Takahashi.

Constraints

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

S consists of digits.

|S| = N

2 \leq P \leq 10000

P is a prime number.

Input

Input is given from Standard Input in the following format:

N P
S

Output

Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten.

Sample Input 1

4 3
3543

Sample Output 1

6

Here S = 3543. There are ten non-empty (contiguous) substrings of S:

3: divisible by 3.

35: not divisible by 3.

354: divisible by 3.

3543: divisible by 3.

5: not divisible by 3.

54: divisible by 3.

543: divisible by 3.

4: not divisible by 3.

43: not divisible by 3.

3: divisible by 3.

Six of these are divisible by 3, so print 6.

Sample Input 2

4 2
2020

Sample Output 2

10

Here S = 2020. There are ten non-empty (contiguous) substrings of S, all of which are divisible by 2, so print 10.

Note that substrings beginning with a 0 also count.

Sample Input 3

20 11
33883322005544116655

Sample Output 3

68


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+1);
	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 begin
		inc(cnt);
		if cnt=K then begin
			count:=True;
			exit;
		end;
	end;
	count:=False;
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.00001 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.


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.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.


var
	N,i,s,a:Longint;
	X:Array[1..100]of Longint;
begin
	read(N);
	for i:=1 to N do begin
		read(X[i]);
		inc(s,X[i]);
	end;
	s:=s div N+1;
	for i:=1 to N do begin
		inc(a,(s-X[i])*(s-X[i]));
	end;
	writeln(a);
end.

Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354


var
	ans,n,a,b,mo:int64;
function p(a,b:int64):int64;
begin
	if b=0 then p:=1 else if b mod 2=0 then p:=p(a*a mod mo,b div 2) else p:=p(a,b-1)*a mod mo;
end;
function C(k:int64):int64;
var x,y:int64;i:Longint;
begin
	x:=1;
	y:=1;
	for i:=1 to k do begin
		x:=x*(n-i+1)mod mo;
		y:=y*i mod mo;
	end;
	C:=x*p(y,mo-2)mod mo;
end;
begin
	read(n,a,b);
	mo:=1000000007;
	ans:=p(2,n)-1-c(a)-c(b);
	ans:=(ans mod mo+mo)mod mo;
	writeln(ans);
end.

Score : 400 points

Problem Statement

Akari has n kinds of flowers, one of each kind.

She is going to choose one or more of these flowers to make a bouquet.

However, she hates two numbers a and b, so the number of flowers in the bouquet cannot be a or b.

How many different bouquets are there that Akari can make?

Find the count modulo (10^9 + 7).

Here, two bouquets are considered different when there is a flower that is used in one of the bouquets but not in the other bouquet.

Constraints

All values in input are integers.

2 \leq n \leq 10^9

1 \leq a < b \leq \textrm{min}(n, 2 \times 10^5)

Input

Input is given from Standard Input in the following format:

n a b

Output

Print the number of bouquets that Akari can make, modulo (10^9 + 7). (If there are no such bouquets, print 0.)

Sample Input 1

4 1 3

Sample Output 1

7

In this case, Akari can choose 2 or 4 flowers to make the bouquet.

There are 6 ways to choose 2 out of the 4 flowers, and 1 way to choose 4, so there are a total of 7 different bouquets that Akari can make.

Sample Input 2

1000000000 141421 173205

Sample Output 2

34076506

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


var
	ans,x,y,mo:int64;
	F,I:Array[0..200000]of int64;
	n,k,id:Longint;
function p(a,b:int64):int64;
begin
	if b=0 then p:=1 else if b mod 2=0 then p:=p(a*a mod mo,b div 2) else p:=p(a,b-1)*a mod mo;
end;
begin
	read(n,k);
	F[0]:=1;
	mo:=1000000007;
	for id:=1 to n do F[id]:=F[id-1]*id mod mo;
	I[n]:=p(F[n],mo-2);
	for id:=n-1 downto 0 do I[id]:=I[id+1]*(id+1)mod mo;
	id:=0;
	while(id<n)and(id<=k)do begin
		inc(ans,F[n]*I[n-id]mod mo*I[id]mod mo*F[n-1]mod mo*I[n-1-id]mod mo*I[id]mod mo);
		inc(id);
	end;
	writeln(ans mod mo);
end.

Score : 500 points

Problem Statement

There is a building with n rooms, numbered 1 to n.

We can move from any room to any other room in the building.

Let us call the following event a move: a person in some room i goes to another room j~ (i \neq j).

Initially, there was one person in each room in the building.

After that, we know that there were exactly k moves happened up to now.

We are interested in the number of people in each of the n rooms now. How many combinations of numbers of people in the n rooms are possible?

Find the count modulo (10^9 + 7).

Constraints

All values in input are integers.

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

2 \leq k \leq 10^9

Input

Input is given from Standard Input in the following format:

n k

Output

Print the number of possible combinations of numbers of people in the n rooms now, modulo (10^9 + 7).

Sample Input 1

3 2

Sample Output 1

10

Let c_1, c_2, and c_3 be the number of people in Room 1, 2, and 3 now, respectively. There are 10 possible combination of (c_1, c_2, c_3):

(0, 0, 3)

(0, 1, 2)

(0, 2, 1)

(0, 3, 0)

(1, 0, 2)

(1, 1, 1)

(1, 2, 0)

(2, 0, 1)

(2, 1, 0)

(3, 0, 0)

For example, (c_1, c_2, c_3) will be (0, 1, 2) if the person in Room 1 goes to Room 2 and then one of the persons in Room 2 goes to Room 3.

Sample Input 2

200000 1000000000

Sample Output 2

607923868

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

Sample Input 3

15 6

Sample Output 3

22583772


var
	k,q,i,j:Longint;
	n,x,m,ans,b,L,y:int64;
	d:Array[1..5000]of Longint;
begin
	read(k,q);
	for i:=1 to k do read(d[i]);
	for j:=1 to q do begin
		read(n,x,m);
		b:=x;
		for i:=1 to k do begin
			L:=(n-i-1+k)div k;
			y:=d[i]mod m;
			if y=0 then y:=m;
			inc(b,y*L);
		end;
		writeln(n-1-b div m+x div m);
	end;
end.

Score : 600 points

Problem Statement

We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}.

Process the following q queries in order:

The i-th query contains three integers n_i, x_i, and m_i.
Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray}
Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i).

Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0).

Constraints

All values in input are integers.

1 \leq k, q \leq 5000

0 \leq d_i \leq 10^9

2 \leq n_i \leq 10^9

0 \leq x_i \leq 10^9

2 \leq m_i \leq 10^9

Input

Input is given from Standard Input in the following format:

k q
d_0 d_1 ... d_{k - 1}
n_1 x_1 m_1
n_2 x_2 m_2
:
n_q x_q m_q

Output

Print q lines.

The i-th line should contain the response to the i-th query.

Sample Input 1

3 1
3 1 4
5 3 2

Sample Output 1

1

For the first query, the sequence {a_j} will be 3,6,7,11,14.

(a_0~\textrm{mod}~2) > (a_1~\textrm{mod}~2)

(a_1~\textrm{mod}~2) < (a_2~\textrm{mod}~2)

(a_2~\textrm{mod}~2) = (a_3~\textrm{mod}~2)

(a_3~\textrm{mod}~2) > (a_4~\textrm{mod}~2)

Thus, the response to this query should be 1.

Sample Input 2

7 3
27 18 28 18 28 46 1000000000
1000000000 1 7
1000000000 2 10
1000000000 3 12

Sample Output 2

224489796
214285714
559523809


program main;
var a,b,c : integer;
    p : boolean;
begin
  readln(a,b,c);
  if ((a=b) and (a<>c)) or ((a=c) and (a<>b)) or ((b=c) and (a<>b)) then p := true
    else p := false;
  if p then writeln('Yes')
    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 : longint;
  poor : boolean;

begin
  readln(a, b, c);
  if ((a=b) and (a<>c)) or ((a=c) and (a<>b)) or ((b=c) and (a<>b)) then poor := true
  else poor := false;
  if poor then writeln('Yes')
  else writeln('No');

end.


program main;
var
    a,i,b : integer;
    p : boolean;
begin
    p := true;
    readln(a);
    for i := 1 to a do
        begin
            read(b);
            if ( b mod 2 ) = 0 then
                begin
                    if (( b mod 3 ) <> 0) then
                        if (( b mod 5 ) <> 0) then
                            p := false;
                end;
        end;
    if ( p = true ) 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.


type SortDataType=String;
function SortCmp(const a,b:SortDataType):Boolean;forward;
procedure SortImpl(var A:Array of SortDataType;L,R:Longint);
var
	tmp:SortDataType;
	mid,il,ir:Longint;
begin
	if R-L<20 then begin
		for ir:=R-1 downto L+1 do for il:=L to ir-1 do if SortCmp(A[il+1],A[il])then begin
			tmp:=A[il];
			A[il]:=A[il+1];
			A[il+1]:=tmp;
		end;
	end else begin
		mid:=(L+R)div 2;
		if SortCmp(A[mid],A[L])then begin
			tmp:=A[mid];
			A[mid]:=A[L];
			A[L]:=tmp;
		end;
		if SortCmp(A[R-1],A[mid])then begin
			tmp:=A[R-1];
			A[R-1]:=A[mid];
			A[mid]:=tmp;
			if SortCmp(A[mid],A[L])then begin
				tmp:=A[mid];
				A[mid]:=A[L];
				A[L]:=tmp;
			end;
		end;
		il:=L;
		ir:=R-1;
		while il<=ir do begin
			while SortCmp(A[il],A[mid])do inc(il);
			while SortCmp(A[mid],A[ir])do dec(ir);
			if il<ir then begin
				tmp:=A[il];
				A[il]:=A[ir];
				A[ir]:=tmp;
				if mid=il then mid:=ir else if mid=ir then mid:=il;
				inc(il);
				dec(ir);
			end else if il=ir then begin
				inc(il);
				dec(ir);
			end;
		end;
		SortImpl(A,L,il);
		SortImpl(A,il,R);
	end;
end;
procedure Sort(var A:Array of SortDataType;size:Longint);
begin
	SortImpl(A,0,size);
end;
function SortCmp(const a,b:SortDataType):Boolean;
begin
	SortCmp:=a<b;
end;
var
	N,i,cnt,ac,id:Longint;
	S:Array[1..200000]of String;
	ast:Array[1..200000]of String;
begin
	readln(N);
	for i:=1 to N do readln(S[i]);
    Sort(S,N);
	cnt:=0;
	id:=0;
	for i:=1 to N do begin
		if(i>1)and(S[i-1]<>S[i])then cnt:=0;
		inc(cnt);
		if ac<cnt then begin
			ac:=cnt;
			id:=1;
			ast[id]:=S[i];
		end else if ac=cnt then begin
			inc(id);
			ast[id]:=S[i];
		end;
	end;
	for i:=1 to id do writeln(ast[i]);
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;
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.


program main;
var i,j,s,t : string;
    a,b : integer;
begin
  readln(i);
  readln(a,b);
  readln(j);
  s := copy(i,1,pos(' ',i)-1);
  t := copy(i,pos(' ',i)+1,length(i));
  if j = s then writeln(a-1,' ',b)
  else writeln(a,' ',b-1);
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;
var A,B : integer;
begin
readln (S,T);
readln (A,B);
readln (U);

if (A<=10) and (B<=10) then begin
if U=S then A:=A-1;
if T=S then B:=B-1;
end;

writeln (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
a:=a-1;

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

writeln(a,b)

end.


program main;
var
    a : string;
    i : integer;
begin
    readln(a);
    for i := 1 to length(a) do
        write('x');
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


type SortDataType=Longint;
function SortCmp(const a,b:SortDataType):Boolean;forward;
procedure SortImpl(var A:Array of SortDataType;L,R:Longint);
var
	tmp:SortDataType;
	mid,il,ir:Longint;
begin
	if R-L<20 then begin
		for ir:=R-1 downto L+1 do for il:=L to ir-1 do if SortCmp(A[il+1],A[il])then begin
			tmp:=A[il];
			A[il]:=A[il+1];
			A[il+1]:=tmp;
		end;
	end else begin
		mid:=(L+R)div 2;
		if SortCmp(A[mid],A[L])then begin
			tmp:=A[mid];
			A[mid]:=A[L];
			A[L]:=tmp;
		end;
		if SortCmp(A[R-1],A[mid])then begin
			tmp:=A[R-1];
			A[R-1]:=A[mid];
			A[mid]:=tmp;
			if SortCmp(A[mid],A[L])then begin
				tmp:=A[mid];
				A[mid]:=A[L];
				A[L]:=tmp;
			end;
		end;
		il:=L;
		ir:=R-1;
		while il<=ir do begin
			while SortCmp(A[il],A[mid])do inc(il);
			while SortCmp(A[mid],A[ir])do dec(ir);
			if il<ir then begin
				tmp:=A[il];
				A[il]:=A[ir];
				A[ir]:=tmp;
				if mid=il then mid:=ir else if mid=ir then mid:=il;
				inc(il);
				dec(ir);
			end else if il=ir then begin
				inc(il);
				dec(ir);
			end;
		end;
		SortImpl(A,L,il);
		SortImpl(A,il,R);
	end;
end;
procedure Sort(var A:Array of SortDataType;size:Longint);
begin
	SortImpl(A,0,size);
end;
function SortCmp(const a,b:SortDataType):Boolean;
begin
	SortCmp:=a<b;
end;
var
	N,i:Longint;
	H:Array[1..200000]of Longint;
	ans:int64;
begin
	read(N);
	for i:=1 to N do read(H[i]);
	Sort(H,N);
	for i:=2 to N do if H[i-1]=H[i]then begin
		writeln('NO');
		exit;
	end;
	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,k,t,i:longint;
  a,sum:array[0..300000] of extended;
  ans:extended;
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: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;
    if H mod A=0 then
    writeln(x)
    else writeln(x+1);
end.


program main;
var h,a,i : integer;
begin
  readln(h,a);
  i := h div a;
  if (h mod a <> 0) then inc(i);
  writeln(i);
end.


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

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


var h,n,a,i,sum : integer;
begin
  read(h,n);
  sum := 0;
  for i:=1 to n do begin
    read(a);
    sum := sum + a;
  end;
  if sum >= 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


var i, n, k, S, v:Longint;
begin
  S := 0;
  read(k, n);
  for i := 1 to n do
  begin
    read(v);
    S := S + v;
    if S >= k then
      begin
        writeln('Yes');
        exit;
      end;
  end;
  writeln('No');
end.


type SortDataType=Longint;
function SortCmp(const a,b:SortDataType):Boolean;forward;
procedure SortImpl(var A:Array of SortDataType;L,R:Longint);
var
	tmp:SortDataType;
	mid,il,ir:Longint;
begin
	if R-L<20 then begin
		for ir:=R-1 downto L+1 do for il:=L to ir-1 do if SortCmp(A[il+1],A[il])then begin
			tmp:=A[il];
			A[il]:=A[il+1];
			A[il+1]:=tmp;
		end;
	end else begin
		mid:=(L+R)div 2;
		if SortCmp(A[mid],A[L])then begin
			tmp:=A[mid];
			A[mid]:=A[L];
			A[L]:=tmp;
		end;
		if SortCmp(A[R-1],A[mid])then begin
			tmp:=A[R-1];
			A[R-1]:=A[mid];
			A[mid]:=tmp;
			if SortCmp(A[mid],A[L])then begin
				tmp:=A[mid];
				A[mid]:=A[L];
				A[L]:=tmp;
			end;
		end;
		il:=L;
		ir:=R-1;
		while il<=ir do begin
			while SortCmp(A[il],A[mid])do inc(il);
			while SortCmp(A[mid],A[ir])do dec(ir);
			if il<ir then begin
				tmp:=A[il];
				A[il]:=A[ir];
				A[ir]:=tmp;
				if mid=il then mid:=ir else if mid=ir then mid:=il;
				inc(il);
				dec(ir);
			end else if il=ir then begin
				inc(il);
				dec(ir);
			end;
		end;
		SortImpl(A,L,il);
		SortImpl(A,il,R);
	end;
end;
procedure Sort(var A:Array of SortDataType;size:Longint);
begin
	SortImpl(A,0,size);
end;
function SortCmp(const a,b:SortDataType):Boolean;
begin
	SortCmp:=a>b;
end;
var
	N,K,i:Longint;
	H:Array[1..200000]of Longint;
	ans:int64;
begin
	read(N,K);
	for i:=1 to N do read(H[i]);
	Sort(H,N);
	for i:=K+1 to N do inc(ans,H[i]);
	writeln(ans);
end.

Score : 300 points

Problem Statement

Fennec is fighting with N monsters.

The health of the i-th monster is H_i.

Fennec can do the following two actions:

Attack: Fennec chooses one monster. That monster's health will decrease by 1.

Special Move: Fennec chooses one monster. That monster's health will become 0.

There is no way other than Attack and Special Move to decrease the monsters' health.

Fennec wins when all the monsters' healths become 0 or below.

Find the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning when she can use Special Move at most K times.

Constraints

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

0 \leq K \leq 2 \times 10^5

1 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 ... H_N

Output

Print the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning.

Sample Input 1

3 1
4 1 5

Sample Output 1

5

By using Special Move on the third monster, and doing Attack four times on the first monster and once on the second monster, Fennec can win with five Attacks.

Sample Input 2

8 9
7 9 3 2 3 8 4 6

Sample Output 2

0

She can use Special Move on all the monsters.

Sample Input 3

3 0
1000000000 1000000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


var h,n : int64;
    a,b : array[1..1010] of int64;
    dp : array[0..10010] of int64;
    i,hi : int64;

function max(a,b:int64):int64;
begin
  if a >= b then max := a
  else max := b
end;

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

begin
  read(h,n);
  for i := 1 to n do read(a[i],b[i]);
  dp[0] := 0;
  for i := 1 to h do dp[i] := 1000000000000000000;
  for hi := 1 to h do begin
    for i := 1 to n do dp[hi] := min(dp[hi],dp[max(hi-a[i],0)] + b[i])
  end;
  writeln(dp[h])
end.

Score : 500 points

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H.

Ibis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.

The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.

Ibis wins when the health of the monster becomes 0 or below.

Find the minimum total Magic Points that have to be consumed before winning.

Constraints

1 \leq H \leq 10^4

1 \leq N \leq 10^3

1 \leq A_i \leq 10^4

1 \leq B_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 B_1
:
A_N B_N

Output

Print the minimum total Magic Points that have to be consumed before winning.

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

First, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.

Then, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.

In this way, we can win at the total cost of 4 Magic Points.

Sample Input 2

100 6
1 1
2 3
3 9
4 27
5 81
6 243

Sample Output 2

100

It is optimal to cast the first spell 100 times.

Sample Input 3

9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461

Sample Output 3

139815


var t,s,m:longint;
begin
  readln;m:=2147483647;
  while not eoln do
  begin
    read(t);
    if t<m then
    begin
      m:=t;
      inc(s);
    end;
  end;
  writeln(s);
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:Array[1 .. 100000] of Longint;
    ans,sum:Int64;
    n,i:Longint;
Function gcd(a,b:Int64):Int64;
    Begin
        If b = 0 then exit(a);
        Exit(gcd(b, a mod b));
    End;
Begin
    Readln(n);
    For i:=1 to n do read(a[i]);
    ans:=a[1];
    For i:=2 to n do ans:=(((a[i] * ans)) div (gcd(a[i], ans)));
    For i:=1 to n do sum:=(sum+ans div a[i]) mod 1000000007;
    Writeln(sum);
End.

Score : 500 points

Problem Statement

Given are N positive integers A_1,...,A_N.

Consider positive integers B_1, ..., B_N that satisfy the following condition.

Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.

Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.

Since the answer can be enormous, print the sum modulo (10^9 +7).

Constraints

1 \leq N \leq 10^4

1 \leq A_i \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).

Sample Input 1

3
2 3 4

Sample Output 1

13

Let B_1=6, B_2=4, and B_3=3, and the condition will be satisfied.

Sample Input 2

5
12 12 12 12 12

Sample Output 2

5

We can let all B_i be 1.

Sample Input 3

3
1000000 999999 999998

Sample Output 3

996989508

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


var
	N,M,i,p,j,ans,t:Longint;
	s:String;
	c:Array[1..100000]of Longint;
begin
	readln(N,M);
	for i:=1 to M do begin
		readln(s);
		p:=0;
		j:=1;
		while s[j]<>' 'do begin
			p:=p*10+ord(s[j])-48;
			inc(j);
		end;
		inc(j);
		if s[j]='A'then begin
			if c[p]>=0 then begin
				inc(ans,c[p]);
				inc(t);
			end;
			c[p]:=-1;
		end else begin
			if c[p]>=0 then inc(c[p]);
		end;
	end;
	writeln(t,' ',ans);
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 x,k : integer;
begin
readln (k,x);

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

end.


program hi;
var k, x: longint;
begin
    readln(k, x);
    if 500 * k >= x then
        writeln('Yes')
    else
        writeln('No');
end.


program main;
var
  k, x : longint;

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


program main;
var
    a : string;
    i,n,c : integer;
begin
    c := 0;
    readln(n);
    readln(a);
    for i := 1 to n do
        if a[i] = 'A' then
            if a[i+1] = 'B' then
                if a[i+2] = 'C' then
                    inc(c);
    writeln(c);
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 hi;
var
    n, i, ans: longint;
    s: string;

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


program main;
const
  size = 8;

type
  sample = array [1..size] 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;  //zero index
      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;  //lehmer code
    end;

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

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

    begin
      t := 0;
      for i := 1 to n do begin
        t := t + a[i]*fact(n-i);
      end;  //lexicographic rank
	  inc(t);
	  rank := t;
    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