function main()
	N,K=map(x->parse(Int,x),split(readline()))
	A=map(x->parse(Int,x),split(readline()))
	ans=0
	mod=10^9+7
	for i=1:N
		a=A[i]
		cnt=0
		for j=1:i-1
			if A[j]>a
				cnt+=1
			end
		end
		ans+=cnt*K%mod
		for j=i+1:N
			if A[j]>a
				cnt+=1
			end
		end
		ans+=cnt*div(K*(K-1),2)%mod
	end
	println(ans%mod)
end
main()

Score : 300 points

Problem Statement

We have a sequence of N integers A~=~A_0,~A_1,~...,~A_{N - 1}.

Let B be a sequence of K \times N integers obtained by concatenating K copies of A. For example, if A~=~1,~3,~2 and K~=~2, B~=~1,~3,~2,~1,~3,~2.

Find the inversion number of B, modulo 10^9 + 7.

Here the inversion number of B is defined as the number of ordered pairs of integers (i,~j)~(0 \leq i < j \leq K \times N - 1) such that B_i > B_j.

Constraints

All values in input are integers.

1 \leq N \leq 2000

1 \leq K \leq 10^9

1 \leq A_i \leq 2000

Input

Input is given from Standard Input in the following format:

N K
A_0 A_1 ... A_{N - 1}

Output

Print the inversion number of B, modulo 10^9 + 7.

Sample Input 1

2 2
2 1

Sample Output 1

3

In this case, B~=~2,~1,~2,~1. We have:

B_0 > B_1

B_0 > B_3

B_2 > B_3

Thus, the inversion number of B is 3.

Sample Input 2

3 5
1 1 1

Sample Output 2

0

A may contain multiple occurrences of the same number.

Sample Input 3

10 998244353
10 9 8 7 5 6 3 4 2 1

Sample Output 3

185297239

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


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	p = 10^9+7
	n = readline() |> parseInt
	s = readline() |> chomp
	lr = zeros(Int,2n)
	lr[1] = -1
	f = 0
	bc = 1
	wc = 0
	for i in 2:2n
		if s[i]==s[i-1]
			lr[i]=lr[i-1]*(-1)
		else
			lr[i]=lr[i-1]
		end
		if s[i]=='B'
			bc += 1
		else
			wc += 1
		end
	end
	ans = 1
	lc = 0
	rc = 0
	for i in 1:2n
		if lr[i]==1
			ans = ans*(lc-rc)%p
			rc += 1
		else
			lc += 1
		end
	end
	for i in 1:n
		ans = ans*i%p
	end
	println(bc==wc?ans:0)
end

main()

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

Score : 500 points

Problem Statement

There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.

The color of the i-th square from the left is black if the i-th character of S is B, and white if that character is W.

You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.

Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.

Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.

Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.

Constraints

1 \leq N \leq 10^5

|S| = 2N

Each character of S is B or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.

Sample Input 1

2
BWWB

Sample Output 1

4

There are four ways to make all the squares white, as follows:

Choose Squares 1, 3 in the first operation, and choose Squares 2, 4 in the second operation.

Choose Squares 2, 4 in the first operation, and choose Squares 1, 3 in the second operation.

Choose Squares 1, 4 in the first operation, and choose Squares 2, 3 in the second operation.

Choose Squares 2, 3 in the first operation, and choose Squares 1, 4 in the second operation.

Sample Input 2

4
BWBBWWWB

Sample Output 2

288

Sample Input 3

5
WWWWWWWWWW

Sample Output 3

0

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.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n = readline() |> parseInt
	k = 0
	if n==3
		println("1 2")
		println("1")
	elseif n%2 == 1
		for i in 1:n-1
			for j in i+1:n-1
				print(2, " ")
			end
			println(2)
		end
	else
		a = zeros(Int,n-1,n)
		for i in 1:n-1
			for j in i+1:n
				if abs(i-j)%2==0
					a[i,j] = 2
				else
					a[i,j] = 1
				end
			end
		end
		for i in 1:n-1
			for j in i+1:n-1
				print(a[i,j], " ")
			end
			println(a[i,n])
		end
	end
end

main()

Score: 600 points

Problem Statement

AtCoder's head office consists of N rooms numbered 1 to N. For any two rooms, there is a direct passage connecting these rooms.

For security reasons, Takahashi the president asked you to set a level for every passage, which is a positive integer and must satisfy the following condition:

For each room i\ (1 \leq i \leq N), if we leave Room i, pass through some passages whose levels are all equal and get back to Room i, the number of times we pass through a passage is always even.

Your task is to set levels to the passages so that the highest level of a passage is minimized.

Constraints

N is an integer between 2 and 500 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print one way to set levels to the passages so that the objective is achieved, as follows:

a_{1,2} a_{1,3} ... a_{1,N}
a_{2,3} ... a_{2,N}
.
.
.
a_{N-1,N}

Here a_{i,j} is the level of the passage connecting Room i and Room j.

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

Sample Input 1

3

Sample Output 1

1 2
1

The following image describes this output:

For example, if we leave Room 2, traverse the path 2 \to 3 \to 2 \to 3 \to 2 \to 1 \to 2 while only passing passages of level 1 and get back to Room 2, we pass through a passage six times.


readline()
println(1/sum(1/parse(a) for a=split(readline())))

Score : 200 points

Problem Statement

Given is a sequence of N integers A_1, \ldots, A_N.

Find the (multiplicative) inverse of the sum of the inverses of these numbers, \frac{1}{\frac{1}{A_1} + \ldots + \frac{1}{A_N}}.

Constraints

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 \ldots A_N

Output

Print a decimal number (or an integer) representing the value of \frac{1}{\frac{1}{A_1} + \ldots + \frac{1}{A_N}}.

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

Sample Input 1

2
10 30

Sample Output 1

7.5

\frac{1}{\frac{1}{10} + \frac{1}{30}} = \frac{1}{\frac{4}{30}} = \frac{30}{4} = 7.5.

Printing 7.50001, 7.49999, and so on will also be accepted.

Sample Input 2

3
200 200 200

Sample Output 2

66.66666666666667

\frac{1}{\frac{1}{200} + \frac{1}{200} + \frac{1}{200}} = \frac{1}{\frac{3}{200}} = \frac{200}{3} = 66.6666....

Printing 6.66666e+1 and so on will also be accepted.

Sample Input 3

1
1000

Sample Output 3

1000

\frac{1}{\frac{1}{1000}} = 1000.

Printing +1000.0 and so on will also be accepted.


parseint(x) = parse(Int, x)
parselist(l) = map(parseint, l)

avg(x, y) = (x+y)/2

function main()
    n = readline() |> parseint
    v = readline() |> split |> parselist
    sort!(v)
    ans = v[1]
    for i = 2:n
        ans = avg(ans, v[i])
    end
    println(ans)
end

main()

Score : 300 points

Problem Statement

You have a pot and N ingredients. Each ingredient has a real number parameter called value, and the value of the i-th ingredient (1 \leq i \leq N) is v_i.

When you put two ingredients in the pot, they will vanish and result in the formation of a new ingredient. The value of the new ingredient will be (x + y) / 2 where x and y are the values of the ingredients consumed, and you can put this ingredient again in the pot.

After you compose ingredients in this way N-1 times, you will end up with one ingredient. Find the maximum possible value of this ingredient.

Constraints

2 \leq N \leq 50

1 \leq v_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
v_1 v_2 \ldots v_N

Output

Print a decimal number (or an integer) representing the maximum possible value of the last ingredient remaining.

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

Sample Input 1

2
3 4

Sample Output 1

3.5

If you start with two ingredients, the only choice is to put both of them in the pot. The value of the ingredient resulting from the ingredients of values 3 and 4 is (3 + 4) / 2 = 3.5.

Printing 3.50001, 3.49999, and so on will also be accepted.

Sample Input 2

3
500 300 200

Sample Output 2

375

You start with three ingredients this time, and you can choose what to use in the first composition. There are three possible choices:

Use the ingredients of values 500 and 300 to produce an ingredient of value (500 + 300) / 2 = 400. The next composition will use this ingredient and the ingredient of value 200, resulting in an ingredient of value (400 + 200) / 2 = 300.

Use the ingredients of values 500 and 200 to produce an ingredient of value (500 + 200) / 2 = 350. The next composition will use this ingredient and the ingredient of value 300, resulting in an ingredient of value (350 + 300) / 2 = 325.

Use the ingredients of values 300 and 200 to produce an ingredient of value (300 + 200) / 2 = 250. The next composition will use this ingredient and the ingredient of value 500, resulting in an ingredient of value (250 + 500) / 2 = 375.

Thus, the maximum possible value of the last ingredient remaining is 375.

Printing 375.0 and so on will also be accepted.

Sample Input 3

5
138 138 138 138 138

Sample Output 3

138


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function bsearch(a,x);l,r=0,length(a)+1;while r-l>1;m=(r+l)>>1;if a[m]<x;l=m;else;r=m;end;end;r;end

function main()
	ss = readline() |> chomp
	tt = readline() |> chomp
	s = Int[Int(ss[i])-96 for i in 1:length(ss)]
	t = Int[Int(tt[i])-96 for i in 1:length(tt)]
	n = length(s)
	d = Dict{Int,Array{Int,1}}()
	dp = -ones(Int,n+1,26)
	for i in 1:n
		if haskey(d,s[i])
			push!(d[s[i]],i)
		else
			d[s[i]] = Int[i]
		end
	end
	for i in 1:n
		push!(d[s[i]],n+i)
	end
	for i in 1:n
		for j in keys(d)
			dp[i,j]=d[j][bsearch(d[j],i)]
		end
	end
	k = 0
	now = 0
	for i in 1:length(t)
		if dp[now+1,t[i]]<0
			k = -1
			break
		else
			now = dp[now+1,t[i]]
			if now>=n
				k += n
				now = now%n
			end
		end
	end
	println(k==-1?-1:k+now)
end

main()

Score : 500 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.

Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s').

Notes

A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net, c, and contest.

Constraints

1 \leq |s| \leq 10^5

1 \leq |t| \leq 10^5

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.

Sample Input 1

contest
son

Sample Output 1

10

t = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest...), so i = 10 satisfies the condition.

On the other hand, t is not a subsequence of the string contestco (the first 9 characters in s'), so i = 9 does not satisfy the condition.

Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10.

Sample Input 2

contest
programming

Sample Output 2

-1

t = programming is not a substring of s' = contestcontestcontest.... Thus, there is no integer i satisfying the condition.

Sample Input 3

contest
sentence

Sample Output 3

33

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.


s=chomp(readline())
t=chomp(readline())
N=length(s)
nxt=zeros(Int,N,26)
nownxt=-ones(Int,26)
for i=2*N:-1:1
	if i<=N
		for j=1:26
			nxt[i,j]=nownxt[j]
		end
	end
	nownxt[Int(s[mod1(i,N)])-96]=i-1
end
p=-1
ans=0
for c=t
	q=0
	if p<0
		q=nownxt[Int(c)-96]
	else
		q=nxt[p+1,Int(c)-96]
	end
	if q<0
		println(-1)
		exit()
	end
	ans+=q-p
	p=q%N
end
println(ans)


if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
function main()
  s=chomp(readline())
  n=length(s)
  ans=1
  pre=s[1]
  lookat=2
  while lookat<=n
    ans+=1
    if lookat==n-1 && s[end-1]==s[end]
      lookat+=2
    elseif pre==s[lookat]
      pre=s[lookat:lookat+1]
      lookat+=2
    else
      pre=s[lookat]
      lookat+=1
    end
  end
  println(ans)
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Score : 300 points

Problem Statement

Given is a string S consisting of lowercase English letters. Find the maximum positive integer K that satisfies the following condition:

There exists a partition of S into K non-empty strings S=S_1S_2...S_K such that S_i \neq S_{i+1} (1 \leq i \leq K-1).

Here S_1S_2...S_K represents the concatenation of S_1,S_2,...,S_K in this order.

Constraints

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

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum positive integer K that satisfies the condition.

Sample Input 1

aabbaa

Sample Output 1

4

We can, for example, divide S into four strings aa, b, ba, and a.

Sample Input 2

aaaccacabaababc

Sample Output 2

12


parseInt(x) = parse(Int, x)
parseBigInt(x) = parse(BigInt, x)
parseFloat(x) = parse(Float64, x)
parseBigFloat(x) = parse(BigFloat, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    s=chomp(readline())
    n=length(s)
    ans=0
    before,now="",""
    for i in s
        now*=string(i)
        if before!=now
            before,now=now,""
            ans+=1
        end
    end
    println(ans)
end
main()


parseInt(x) = parse(Int, x)
parseBigInt(x) = parse(BigInt, x)
parseFloat(x) = parse(Float64, x)
parseBigFloat(x) = parse(BigFloat, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    s=split(readline(),"")
    i=2
    while i<length(s)
        if s[i]==s[i-1]
            s[i]*=s[i+1]
            s=vcat(s[1:i],s[i+2:end])
        end
        i+=1
    end
    i=2
    while i<length(s)
        if s[i]==s[i+1]
            s[i]=s[i-1]*s[i]
            s=vcat(s[1:i-2],s[i:end])
        end
        i+=1
    end
    println(length(s))
end
main()


vfunction main()

  (A,B) = map(x -> parse(Int,x), split(readline()))

  println(max(A+B,A-B,A*B))

end

main()

Score : 100 points

Problem Statement

We have two integers: A and B.

Print the largest number among A + B, A - B, and A \times B.

Constraints

All values in input are integers.

-100 \leq A,\ B \leq 100

Input

Input is given from Standard Input in the following format:

A B

Output

Print the largest number among A + B, A - B, and A \times B.

Sample Input 1

-13 3

Sample Output 1

-10

The largest number among A + B = -10, A - B = -16, and A \times B = -39 is -10.

Sample Input 2

1 -33

Sample Output 2

34

The largest number among A + B = -32, A - B = 34, and A \times B = -33 is 34.

Sample Input 3

13 3

Sample Output 3

39

The largest number among A + B = 16, A - B = 10, and A \times B = 39 is 39.


ny=split(readline())
a=parse(Int,ny[1])
b=parse(Int,ny[2])

c=a+b
d=a-b
e=a*b

print(max(c,d,e))


#=
B:
- Julia version: 0.5.2
- Author: abap
- Date: 2019-12-29
=#
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

K,X = parseMap(split(readline()))

for i in X-(K-1):X+(K-1)
	print("$i ")
end

println("")

Score : 200 points

Problem Statement

There are 2000001 stones placed on a number line. The coordinates of these stones are -1000000, -999999, -999998, \ldots, 999999, 1000000.

Among them, some K consecutive stones are painted black, and the others are painted white.

Additionally, we know that the stone at coordinate X is painted black.

Print all coordinates that potentially contain a stone painted black, in ascending order.

Constraints

1 \leq K \leq 100

0 \leq X \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K X

Output

Print all coordinates that potentially contain a stone painted black, in ascending order, with spaces in between.

Sample Input 1

3 7

Sample Output 1

5 6 7 8 9

We know that there are three stones painted black, and the stone at coordinate 7 is painted black. There are three possible cases:

The three stones painted black are placed at coordinates 5, 6, and 7.

The three stones painted black are placed at coordinates 6, 7, and 8.

The three stones painted black are placed at coordinates 7, 8, and 9.

Thus, five coordinates potentially contain a stone painted black: 5, 6, 7, 8, and 9.

Sample Input 2

4 0

Sample Output 2

-3 -2 -1 0 1 2 3

Negative coordinates can also contain a stone painted black.

Sample Input 3

1 100

Sample Output 3

100


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n=parseInt(readline())
    array=Vector{String}()
    for i in 1:n
        s=chomp(readline())
        push!(array,join(sort(collect(s))))
    end
    sort!(array)
    pat=[0 for i in 1:10^5]
    for i in 2:10^5
        pat[i]=pat[i-1]+i-1
    end
    ans=0
    cnt=1
    for i in 1:n
        if i==n
            ans+=pat[cnt]
        elseif array[i]!=array[i+1]
            ans+=pat[cnt]
            cnt=1
        else
            cnt+=1
        end
    end
    println(ans)
end
main()

Score : 300 points

Problem Statement

We will call a string obtained by arranging the characters contained in a string a in some order, an anagram of a.

For example, greenbin is an anagram of beginner. As seen here, when the same character occurs multiple times, that character must be used that number of times.

Given are N strings s_1, s_2, \ldots, s_N. Each of these strings has a length of 10 and consists of lowercase English characters. Additionally, all of these strings are distinct. Find the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Constraints

2 \leq N \leq 10^5

s_i is a string of length 10.

Each character in s_i is a lowercase English letter.

s_1, s_2, \ldots, s_N are all distinct.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Sample Input 1

3
acornistnt
peanutbomb
constraint

Sample Output 1

1

s_1 = acornistnt is an anagram of s_3 = constraint. There are no other pairs i, j such that s_i is an anagram of s_j, so the answer is 1.

Sample Input 2

2
oneplustwo
ninemodsix

Sample Output 2

0

If there is no pair i, j such that s_i is an anagram of s_j, print 0.

Sample Input 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

Sample Output 3

4

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.


parseint(x) = parse(Int, x)
readint() = readline() |> parseint
readstr() = readline() |> chomp
readstrlist(n) = [readstr() for _ = 1:n]

function encode(s)
    code::BigInt = 0
    for c in s
        code += BigInt(11) ^ (c - 'a')
    end
    code
end

c2(n) = (n*(n-1))>>1

function main()
    n = readint()
    s = readstrlist(n)
    s = encode.(s)
    sort!(s)
    append!(s, -1)
    ans = 0
    num = 1
    for i = 1:n
        if s[i] == s[i+1]
            num += 1
        else
            ans += c2(num)
            num = 1
        end
    end
    println(ans)
end

main()


#=
C:
- Julia version: 0.5.2
- Author: abap
- Date: 2019-12-29
=#
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

N = parseInt(readline())

function readStr()
	s = chomp(readline())
	S = []
	for j in s
		push!(S, j)
	end
	return sort!(S)
end

function  main()
A = Array{Char}[]
for i in 1:N
	x = readStr()
	push!(A, x)
end
res::Int64 = 0
B = Set(A)
for k in B
	count = 0
	for kk in 1:N
		if  k == @inbouds A[kk]
			count += 1
		end
	end
	res += (count * (count - 1 )) / 2
end
println(res)

end

main()


parseInt(x) = parse(Int,x)

function f(s,N)
  a = []
  for i = 1:N
    push!(a,zeros(Int,10))
  end
  for i = 1:N
    for j = 1:10
      a[i][j] = Int(s[i][j])
    end
    a[i] = sort(a[i])
  end
  return(a)
end

function main()
  N = parseInt(readline())
  s = []
  for i = 1:N
    push!(s,chomp(readline()))
  end
  a = f(s,N)
  c = BigInt(0)
  while length(a) != 0
    tmp = 0
    b = filter(x -> !(x in [a[1]]),a)
    tmp = length(a)-length(b)-1
    c += div(tmp*(tmp+1),2)
    a = b
  end
  println(c)
end

main()


parseInt(x) = parse(Int,x)

function main()
  N = parseInt(readline())
  s = []
  a = []
  for i = 1:N
    push!(s,chomp(readline()))
    push!(a,zeros(Int,26))
  end
  for i = 1:N
    for j = 1:10
      for k = 'a':'z'
        s[i][j] == k ? a[i][Int(k)-96] += 1 : tmp = 0
      end
    end
  end
  c = 0
  l = zeros(Int,N)
  for i = 1:N
    tmp = 0
    if l[i] == 0
      for j = (i+1):N
        if a[i] == a[j]
          tmp += 1
          l[j] = 1
        end
      end
      c += div(tmp*(tmp+1),2)
    end
  end
  println(c)
end

main()


Score : 600 points

Problem Statement

Given are a prime number p and a sequence of p integers a_0, \ldots, a_{p-1} consisting of zeros and ones.

Find a polynomial of degree at most p-1, f(x) = b_{p-1} x^{p-1} + b_{p-2} x^{p-2} + \ldots + b_0, satisfying the following conditions:

For each i (0 \leq i \leq p-1), b_i is an integer such that 0 \leq b_i \leq p-1.

For each i (0 \leq i \leq p-1), f(i) \equiv a_i \pmod p.

Constraints

2 \leq p \leq 2999

p is a prime number.

0 \leq a_i \leq 1

Input

Input is given from Standard Input in the following format:

p
a_0 a_1 \ldots a_{p-1}

Output

Print b_0, b_1, \ldots, b_{p-1} of a polynomial f(x) satisfying the conditions, in this order, with spaces in between.

It can be proved that a solution always exists. If multiple solutions exist, any of them will be accepted.

Sample Input 1

2
1 0

Sample Output 1

1 1

f(x) = x + 1 satisfies the conditions, as follows:

f(0) = 0 + 1 = 1 \equiv 1 \pmod 2

f(1) = 1 + 1 = 2 \equiv 0 \pmod 2

Sample Input 2

3
0 0 0

Sample Output 2

0 0 0

f(x) = 0 is also valid.

Sample Input 3

5
0 1 0 1 0

Sample Output 3

0 2 0 1 3


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    a,b,c=parseMap(split(readline()))
    println(max(0,c-a+b))
end
main()

Score : 100 points

Problem Statement

We have two bottles for holding water.

Bottle 1 can hold up to A milliliters of water, and now it contains B milliliters of water.

Bottle 2 contains C milliliters of water.

We will transfer water from Bottle 2 to Bottle 1 as much as possible.

How much amount of water will remain in Bottle 2?

Constraints

All values in input are integers.

1 \leq B \leq A \leq 20

1 \leq C \leq 20

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the integer representing the amount of water, in milliliters, that will remain in Bottle 2.

Sample Input 1

6 4 3

Sample Output 1

1

We will transfer two milliliters of water from Bottle 2 to Bottle 1, and one milliliter of water will remain in Bottle 2.

Sample Input 2

8 3 9

Sample Output 2

4

Sample Input 3

12 3 7

Sample Output 3

0


N = chomp(readline())
keta = length(N)
ans = 0
for i in 1:2:keta
  ans += (10^i - 10^(i-1))
end
println(ans)

Score : 200 points

Problem Statement

Given is an integer N. Find the number of positive integers less than or equal to N that have an odd number of digits (in base ten without leading zeros).

Constraints

1 \leq N \leq 10^5

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of positive integers less than or equal to N that have an odd number of digits.

Sample Input 1

11

Sample Output 1

9

Among the positive integers less than or equal to 11, nine integers have an odd number of digits: 1, 2, \ldots, 9.

Sample Input 2

136

Sample Output 2

46

In addition to 1, 2, \ldots, 9, another 37 integers also have an odd number of digits: 100, 101, \ldots, 136.

Sample Input 3

100000

Sample Output 3

90909


function main()
	L=R=p=0
	s=chomp(readline())
	N=length(s)
	ans=zeros(Int,N)
	for i=1:N
		if s[i]=='R'
			R+=1
			p=i
		else
			L+=1
			if i==N||s[i+1]=='R'
				ans[p]=ans[p+1]=div(L+R,2)
				if (L+R)%2==1
					ans[p+L%2]+=1
				end
				L=R=0
			end
		end
	end
	println.(ans)
end
main()

Score : 400 points

Problem Statement

Given is a string S consisting of L and R.

Let N be the length of S. There are N squares arranged from left to right, and the i-th character of S from the left is written on the i-th square from the left.

The character written on the leftmost square is always R, and the character written on the rightmost square is always L.

Initially, one child is standing on each square.

Each child will perform the move below 10^{100} times:

Move one square in the direction specified by the character written in the square on which the child is standing. L denotes left, and R denotes right.

Find the number of children standing on each square after the children performed the moves.

Constraints

S is a string of length between 2 and 10^5 (inclusive).

Each character of S is L or R.

The first and last characters of S are R and L, respectively.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of children standing on each square after the children performed the moves, in order from left to right.

Sample Input 1

RRLRL

Sample Output 1

0 1 2 1 1

After each child performed one move, the number of children standing on each square is 0, 2, 1, 1, 1 from left to right.

After each child performed two moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.

After each child performed 10^{100} moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.

Sample Input 2

RRLLLLRLRRLL

Sample Output 2

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

Sample Input 3

RRRLLRLLRRRLLLLL

Sample Output 3

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


parseInt(x) = parse(Int, x)
parseArray(x::Array{SubString{String}, 1}) = map(parseInt, x)

function main()
  n,k = readline() |> split |> parseArray
  a = readline() |> split |> parseArray

  ans=0
  escape=false
  for d in reverse(divisor(sum(a)))
    d_i=filter(x -> x!=0, sort(a.%d))
    l=length(d_i)
    for i in 1:l
      sum(d_i[1:i])!=sum(d-d_i[i+1:end]) && continue
      sum(d_i[1:i])>k && continue
      ans=d
      escape=true
      break
    end
    escape && break
  end
  println(ans)
end

function divisor(x::Int)
  a=Int[]
  for i in 1:floor(sqrt(x))
    x%i!=0 && continue
    push!(a, i)
    push!(a, x÷i)
  end
  sort!(a)
  a
end

main()

Score : 500 points

Problem Statement

We have a sequence of N integers: A_1, A_2, \cdots, A_N.

You can perform the following operation between 0 and K times (inclusive):

Choose two integers i and j such that i \neq j, each between 1 and N (inclusive). Add 1 to A_i and -1 to A_j, possibly producing a negative element.

Compute the maximum possible positive integer that divides every element of A after the operations. Here a positive integer x divides an integer y if and only if there exists an integer z such that y = xz.

Constraints

2 \leq N \leq 500

1 \leq A_i \leq 10^6

0 \leq K \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_{N-1} A_{N}

Output

Print the maximum possible positive integer that divides every element of A after the operations.

Sample Input 1

2 3
8 20

Sample Output 1

7

7 will divide every element of A if, for example, we perform the following operation:

Choose i = 2, j = 1. A becomes (7, 21).

We cannot reach the situation where 8 or greater integer divides every element of A.

Sample Input 2

2 10
3 5

Sample Output 2

8

Consider performing the following five operations:

Choose i = 2, j = 1. A becomes (2, 6).

Choose i = 2, j = 1. A becomes (1, 7).

Choose i = 2, j = 1. A becomes (0, 8).

Choose i = 2, j = 1. A becomes (-1, 9).

Choose i = 1, j = 2. A becomes (0, 8).

Then, 0 = 8 \times 0 and 8 = 8 \times 1, so 8 divides every element of A. We cannot reach the situation where 9 or greater integer divides every element of A.

Sample Input 3

4 5
10 1 2 22

Sample Output 3

7

Sample Input 4

8 7
1 7 5 6 8 2 6 5

Sample Output 4

5


const lines=readlines()
const N=parse(Int,shift!(lines))
const up=zeros(Int,N)
const down=zeros(Int,N)
function add(A,i,a)
	while i<=N
		A[i]+=a
		i+=i&-i
	end
end
function sum(A,i)
	ret=0
	while i>0
		ret+=A[i]
		i-=i&-i
	end
	ret
end
const mod=998244353
const F=zeros(Int,N+1)
function bsearch(A,x)
	L=0
	R=N
	while R-L>1
		M=div(L+R,2)
		if A[M]>=x
			R=M
		else
			L=M
		end
	end
	R
end
function main()
	F[1]=1
	X=Int[]
	Y=Int[]
	xy=Pair{Int,Int}[]
	for i=1:N
		F[i+1]=F[i]*2%mod
		x,y=map(x->parse(Int,x),split(shift!(lines)))
		push!(X,x)
		push!(Y,y)
		push!(xy,x=>y)
	end
	sort!(X)
	sort!(Y)
	for i=1:N
		x=bsearch(X,xy[i].first)
		y=bsearch(Y,xy[i].second)
		xy[i]=x=>y
		add(down,y,1)
	end
	sort!(xy)
	ans=0
	for i=1:N
		y=xy[i].second
		add(down,xy[i].second,-1)
		a=sum(up,y)
		b=i-1-a
		c=sum(down,y)
		d=N-i-c
		aX=(F[a+1]+F[d+1]+mod-1)%mod
		aY=(F[b+1]+F[c+1]+mod-1)%mod
		ans=(ans+F[N+1]-aX*aY%mod+mod)%mod
		add(up,y,1)
	end
	println(ans)
end
main()

Score : 600 points

Problem Statement

We have a set S of N points in a two-dimensional plane. The coordinates of the i-th point are (x_i, y_i). The N points have distinct x-coordinates and distinct y-coordinates.

For a non-empty subset T of S, let f(T) be the number of points contained in the smallest rectangle, whose sides are parallel to the coordinate axes, that contains all the points in T. More formally, we define f(T) as follows:

f(T) := (the number of integers i (1 \leq i \leq N) such that a \leq x_i \leq b and c \leq y_i \leq d, where a, b, c, and d are the minimum x-coordinate, the maximum x-coordinate, the minimum y-coordinate, and the maximum y-coordinate of the points in T)

Find the sum of f(T) over all non-empty subset T of S. Since it can be enormous, print the sum modulo 998244353.

Constraints

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

-10^9 \leq x_i, y_i \leq 10^9

x_i \neq x_j (i \neq j)

y_i \neq y_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the sum of f(T) over all non-empty subset T of S, modulo 998244353.

Sample Input 1

3
-1 3
2 1
3 -2

Sample Output 1

13

Let the first, second, and third points be P_1, P_2, and P_3, respectively. S = \{P_1, P_2, P_3\} has seven non-empty subsets, and f has the following values for each of them:

f(\{P_1\}) = 1

f(\{P_2\}) = 1

f(\{P_3\}) = 1

f(\{P_1, P_2\}) = 2

f(\{P_2, P_3\}) = 2

f(\{P_3, P_1\}) = 3

f(\{P_1, P_2, P_3\}) = 3

The sum of these is 13.

Sample Input 2

4
1 4
2 1
3 3
4 2

Sample Output 2

34

Sample Input 3

10
19 -11
-3 -12
5 3
3 -15
8 -14
-9 -20
10 -9
0 2
-7 17
6 -6

Sample Output 3

7222

Be sure to print the sum modulo 998244353.


A,B = parse.(split(readline()))
if A % 2 != B % 2
    println("IMPOSSIBLE")
else
    println(Int((A + B) / 2))
end

Score: 100 points

Problem Statement

We have two distinct integers A and B.

Print the integer K such that |A - K| = |B - K|.

If such an integer does not exist, print IMPOSSIBLE instead.

Constraints

All values in input are integers.

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

A and B are distinct.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the integer K satisfying the condition.

If such an integer does not exist, print IMPOSSIBLE instead.

Sample Input 1

2 16

Sample Output 1

9

|2 - 9| = 7 and |16 - 9| = 7, so 9 satisfies the condition.

Sample Input 2

0 3

Sample Output 2

IMPOSSIBLE

No integer satisfies the condition.

Sample Input 3

998244353 99824435

Sample Output 3

549034394


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
N = parse(readline())
p = parseMap(split(readline()))
sort_p = sort(p)
count = 0
for i in 1:N
    if p[i] != sort_p[i]
        count += 1
    end
end

println(count > 2 ? "NO":"YES")

Score : 200 points

Problem Statement

We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.

You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.

Print YES if you can sort p in ascending order in this way, and NO otherwise.

Constraints

All values in input are integers.

2 \leq N \leq 50

p is a permutation of {1,\ 2,\ ...,\ N}.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

You can sort p in ascending order by swapping p_1 and p_5.

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

In this case, swapping any two elements does not sort p in ascending order.

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

p is already sorted in ascending order, so no operation is needed.


n=parse(Int,readline())

an=readline()
an1=split(an)
X=Any[]
for i=1:n+1
    X= push!(X , parse(Int,an1[i]))
end

bn=readline()
bn1=split(bn)
Y=Any[]
for i=1:n
    Y= push!(Y , parse(Int,bn1[i]))
end

k=0

for i=1:n
    if X[i]<Y[i]
        k=k+X[i]
        Y[i]=Y[i]-X[i]
        if X[i+1]<Y[i]
            k=k+X[i+1]
      		X[i+1]=0
        else
            k=k+Y[i]
      		X[i+1]=X[i+1]-Y[i]
        end
    else
        k=k+Y[i]
    end

Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3


parseInt(x) = parse(Int,x)

function main()
  S = chomp(readline())
  N = length(S)
  dp = zeros(Int, N,13)
  if S[N] != '?'
    s = parseInt(S[N])
    s != 0 ? dp[1,s] = 1 : dp[1,13] = 1
  else
    for j = 1:9
      dp[1,j] = 1
    end
    dp[1,13] = 1
  end
  p = zeros(Int, N)
  p[1] = 1
  for i = 2:N
    p[i] = rem(p[i-1]*10,13)
  end
  for i = 2:N
    N == 2 ? m = 1 : m = N-i+1
    for j = 1:13
      if S[m] != '?'
        s = parseInt(S[m])*p[i]
        rem(s+j,13) == 0 ? l = 13 : l = rem(s+j,13)
        dp[i,l] += dp[i-1,j]
        dp[i,l] = rem(dp[i,l],10^9+7)
      else
        for k = 0:9
          s = k*p[i]
          rem(s+j,13) == 0 ? l = 13 : l = rem(s+j,13)
          dp[i,l] += dp[i-1,j]
          dp[i,l] = rem(dp[i,l],10^9+7)
        end
      end
    end
  end
  println(dp[N,5])
end

main()

Score : 400 points

Problem Statement

Given is a string S. Each character in S is either a digit (0, ..., 9) or ?.

Among the integers obtained by replacing each occurrence of ? with a digit, how many have a remainder of 5 when divided by 13? An integer may begin with 0.

Since the answer can be enormous, print the count modulo 10^9+7.

Constraints

S is a string consisting of digits (0, ..., 9) and ?.

1 \leq |S| \leq 10^5

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

??2??5

Sample Output 1

768

For example, 482305, 002865, and 972665 satisfy the condition.

Sample Input 2

?44

Sample Output 2

1

Only 044 satisfies the condition.

Sample Input 3

7?4

Sample Output 3

0

We may not be able to produce an integer satisfying the condition.

Sample Input 4

?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???

Sample Output 4

153716888


Score : 400 points

Problem Statement

Given is an integer S.
Find a combination of six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfies all of the following conditions:

0 \leq X_1,Y_1,X_2,Y_2,X_3,Y_3 \leq 10^9

The area of the triangle in a two-dimensional plane whose vertices are (X_1,Y_1),(X_2,Y_2), and (X_3,Y_3) is S/2.

We can prove that there always exist six integers that satisfy the conditions under the constraints of this problem.

Constraints

1 \leq S \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

Print six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfy the conditions, in this order, with spaces in between.
If multiple solutions exist, any of them will be accepted.

Sample Input 1

3

Sample Output 1

1 0 2 2 0 1

The area of the triangle in a two-dimensional plane whose vertices are (1,0),(2,2), and (0,1) is 3/2.
Printing 3 0 3 1 0 1 or 1 0 0 1 2 2 will also be accepted.

Sample Input 2

100

Sample Output 2

0 0 10 0 0 10

Sample Input 3

311114770564041497

Sample Output 3

314159265 358979323 846264338 327950288 419716939 937510582


Score : 100 points

Problem Statement

It is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2.

Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r.

Constraints

1 \leq r \leq 100

r is an integer.

Input

Input is given from Standard Input in the following format:

r

Output

Print an integer representing the area of the regular dodecagon.

Sample Input 1

4

Sample Output 1

48

The area of the regular dodecagon is 3 \times 4^2 = 48.

Sample Input 2

15

Sample Output 2

675

Sample Input 3

80

Sample Output 3

19200


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    println(3*parseInt(readline())^2)
end
main()


Score : 200 points

Problem Statement

There are N apple trees in a row. People say that one of them will bear golden apples.

We want to deploy some number of inspectors so that each of these trees will be inspected.

Each inspector will be deployed under one of the trees. For convenience, we will assign numbers from 1 through N to the trees. An inspector deployed under the i-th tree (1 \leq i \leq N) will inspect the trees with numbers between i-D and i+D (inclusive).

Find the minimum number of inspectors that we need to deploy to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq D \leq 20

Input

Input is given from Standard Input in the following format:

N D

Output

Print the minimum number of inspectors that we need to deploy to achieve the objective.

Sample Input 1

6 2

Sample Output 1

2

We can achieve the objective by, for example, placing an inspector under Tree 3 and Tree 4.

Sample Input 2

14 3

Sample Output 2

2

Sample Input 3

20 4

Sample Output 3

3


readline()
A=map(x->parse(Int,x),readlines())
B=A[:]
sort!(B)
for a=A
  println(a==B[end]?B[end-1]:B[end])
end

Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5


N=parse(Int,readline())
A=map(x->parse(Int,x),split(readline()))
B=zeros(Int,N)
for i=N:-1:1
	now=A[i]
	for j=i:i:N
		now+=B[j]
	end
	B[i]=now%2
end
println(sum(B))
for i=1:N
	if B[i]==1
		println(i)
	end
end

Score : 400 points

Problem Statement

There are N empty boxes arranged in a row from left to right.
The integer i is written on the i-th box from the left (1 \leq i \leq N).

For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.

We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:

For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2.

Does there exist a good set of choices? If the answer is yes, find one good set of choices.

Constraints

All values in input are integers.

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

a_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If a good set of choices does not exist, print -1.

If a good set of choices exists, print one such set of choices in the following format:

M
b_1 b_2 ... b_M

where M denotes the number of boxes that will contain a ball, and b_1,\ b_2,\ ...,\ b_M are the integers written on these boxes, in any order.

Sample Input 1

3
1 0 0

Sample Output 1

1
1

Consider putting a ball only in the box with 1 written on it.

There are three boxes with multiples of 1 written on them: the boxes with 1, 2, and 3. The total number of balls contained in these boxes is 1.

There is only one box with a multiple of 2 written on it: the box with 2. The total number of balls contained in these boxes is 0.

There is only one box with a multiple of 3 written on it: the box with 3. The total number of balls contained in these boxes is 0.

Thus, the condition is satisfied, so this set of choices is good.

Sample Input 2

5
0 0 0 0 0

Sample Output 2

0

Putting nothing in the boxes can be a good set of choices.


N=parse(Int,readline())
K=-ones(Int,N)
for _=1:N
	A=parse(Int,readline())
	L=0
	R=N
	while R-L>1
		M=div(L+R,2)
		if K[M]<A
			R=M
		else
			L=M
		end
	end
	K[R]=A
end
id=1
while id<N&&K[id+1]>=0
	id+=1
end
println(id)

Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.


Score : 600 points

Problem Statement

Let us define the oddness of a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n} as \sum_{i = 1}^n |i - p_i|.

Find the number of permutations of {1,\ 2,\ ...,\ n} of oddness k, modulo 10^9+7.

Constraints

All values in input are integers.

1 \leq n \leq 50

0 \leq k \leq n^2

Input

Input is given from Standard Input in the following format:

n k

Output

Print the number of permutations of {1,\ 2,\ ...,\ n} of oddness k, modulo 10^9+7.

Sample Input 1

3 2

Sample Output 1

2

There are six permutations of {1,\ 2,\ 3}. Among them, two have oddness of 2: {2,\ 1,\ 3} and {1,\ 3,\ 2}.

Sample Input 2

39 14

Sample Output 2

74764168


parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	if sum(a) == 0
		println("Yes")
	elseif n%3!=0
		println("No")
	else
		d = Dict{Int,Int}()
		for i in 1:n
			if haskey(d,a[i])
				d[a[i]] += 1
			else
				d[a[i]] = 1
			end
		end
		f = 0
		for j in values(d)
			if j != div(n,3)
				f = 1
				break
			end
		end
		println(f==0?"Yes":"No")
	end
end

main()

Score : 300 points

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

There are N camels standing in a circle.
Snuke will put one of his hats on each of these camels.

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.

What is XOR?

The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

- When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.

Sample Input 2

4
1 2 4 8

Sample Output 2

No

There is no such way to distribute the hats; the answer is No.


N=parse(Int,readline())
M=Dict{Int,Int}()
for a=map(x->parse(Int,x),split(readline()))
	M[a]=get(M,a,0)+1
end
if N%3!=0
	println(get(M,0,0)==N?"Yes":"No")
else
	T=div(N,3)
	cum=0
	flag=true
	for (k,v)=M
		if v%T!=0
			flag=false
		else
			while v>=T
				v-=T
				cum$=k
			end
		end
	end
	if cum!=0
		flag=false
	end
	println(flag?"Yes":"No")
end


const N,M=parse.(split(readline()))
const G=[[Int[] for _=1:3] for _=1:N]
if M%2==1
	println(-1)
	exit()
end
for s=readlines()
	a,b=map(x->parse(Int,x),split(s))
	push!(G[a][1],b)
	push!(G[b][1],a)
	push!(G[a][2],length(G[b][1]))
	push!(G[b][2],length(G[a][1]))
	push!(G[a][3],-1)
	push!(G[b][3],-1)
end
vis=zeros(Int,N)
@inbounds function dfs(u,p)
	vis[u]=1
	cnt=0
	par=-1
	for i=1:length(G[u][1])
		if vis[G[u][1][i]]==0
			dfs(G[u][1][i],u)
		end
		if G[u][1][i]!=p&&G[u][3][i]!=0
			if G[u][3][i]==-1
				G[u][3][i]=1
				G[G[u][1][i]][3][G[u][2][i]]=0
			end
			cnt+=1
		end
		if G[u][1][i]==p
			par=i
		end
	end
	if cnt%2==1
		G[u][3][par]=1
		G[p][3][G[u][2][par]]=0
	end
end
dfs(1,0)
for u=1:N
	for i=1:length(G[u][1])
		if G[u][3][i]==1
			println("$u $(G[u][1][i])")
		end
	end
end

Score : 700 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i.
Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph.
Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.

Notes

An undirected graph is said to be simple when it contains no self-loops or multiple edges.

Constraints

2 \leq N \leq 10^5

N-1 \leq M \leq 10^5

1 \leq A_i,B_i \leq N (1\leq i\leq M)

The given graph is simple and connected.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If it is impossible to assign directions to satisfy the requirement, print -1.
Otherwise, print an assignment of directions that satisfies the requirement, in the following format:

C_1 D_1
:
C_M D_M

Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.

Sample Input 1

4 4
1 2
2 3
3 4
4 1

Sample Output 1

1 2
1 4
3 2
3 4

After this assignment of directions, Vertex 1 and 3 will each have two outgoing edges, and Vertex 2 and 4 will each have zero outgoing edges.

Sample Input 2

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

Sample Output 2

-1


N=parse(readline())
if ispow2(N)
	println("No")
	exit()
end
println("Yes")
for i=2:2:N-1
	println("1 $i")
	println("$i $(i+1)")
	println("1 $(i+1+N)")
	println("$(i+1+N) $(i+N)")
end
println("3 $(N+1)")
if N%2==0
	M=prevpow(2,N)
	println("$M $N")
	println("$((M$N$1)+N) $(2N)")
end

Score : 700 points

Problem Statement

You are given an integer N. Determine if there exists a tree with 2N vertices numbered 1 to 2N satisfying the following condition, and show one such tree if the answer is yes.

Assume that, for each integer i between 1 and N (inclusive), Vertex i and N+i have the weight i. Then, for each integer i between 1 and N, the bitwise XOR of the weights of the vertices on the path between Vertex i and N+i (including themselves) is i.

Constraints

N is an integer.

1 \leq N \leq 10^{5}

Input

Input is given from Standard Input in the following format:

N

Output

If there exists a tree satisfying the condition in the statement, print Yes; otherwise, print No.
Then, if such a tree exists, print the 2N-1 edges of such a tree in the subsequent 2N-1 lines, in the following format:

a_{1} b_{1}
\vdots
a_{2N-1} b_{2N-1}

Here each pair (a_i, b_i) means that there is an edge connecting Vertex a_i and b_i. The edges may be printed in any order.

Sample Input 1

3

Sample Output 1

Yes
1 2
2 3
3 4
4 5
5 6

The sample output represents the following graph:

Sample Input 2

1

Sample Output 2

No

There is no tree satisfying the condition.


Score : 100 points

Problem Statement

N of us are going on a trip, by train or taxi.

The train will cost each of us A yen (the currency of Japan).

The taxi will cost us a total of B yen.

How much is our minimum total travel expense?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq A \leq 50

1 \leq B \leq 50

Input

Input is given from Standard Input in the following format:

N A B

Output

Print an integer representing the minimum total travel expense.

Sample Input 1

4 2 9

Sample Output 1

8

The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen.

Sample Input 2

4 2 7

Sample Output 2

7

Sample Input 3

4 2 8

Sample Output 3

8


function main()

  (N,A,B) = map(x -> parse(Int,x), split(readline()))

  println(min(A*N,B))

end

main()


function main()

  (N,A,B) = map(x -> parse(Int,x), split(readline()))

  ptintln(min(A*N,B))

end

main()


parseint(x) = parse(Int, x)
readint() = readline() |> parseint
readints() = map(parseint, readline() |> split)

function main()
    n, d = readints()
    x = [readints() for _ = 1:n]
    cnt = 0
    for i = 1:n-1, j = i+1:n
        s = 0
        for k = 1:d
            s += (x[i][k] - x[j][k])^2
        end
        l = 0
        r = 100
        while r - l > 1
            m = l + div(r - l, 2)
            if m*m <= s
                l = m
            else
                r = m
            end
        end
        if l*l == s
            cnt += 1
        end
    end
    println(cnt)
end

main()

Score : 200 points

Problem Statement

There are N points in a D-dimensional space.

The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}).

The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}.

How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer?

Constraints

All values in input are integers.

2 \leq N \leq 10

1 \leq D \leq 10

-20 \leq X_{ij} \leq 20

No two given points have the same coordinates. That is, if i \neq j, there exists k such that X_{ik} \neq X_{jk}.

Input

Input is given from Standard Input in the following format:

N D
X_{11} X_{12} ... X_{1D}
X_{21} X_{22} ... X_{2D}
\vdots
X_{N1} X_{N2} ... X_{ND}

Output

Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer.

Sample Input 1

3 2
1 2
5 5
-2 8

Sample Output 1

1

The number of pairs with an integer distance is one, as follows:

The distance between the first point and the second point is \sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer.

The distance between the second point and the third point is \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58}, which is not an integer.

The distance between the third point and the first point is \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5}, which is not an integer.

Sample Input 2

3 4
-3 7 8 2
-12 1 10 2
-2 8 9 3

Sample Output 2

2

Sample Input 3

5 1
1
2
3
4
5

Sample Output 3

10


const lines=readlines()
parseInt(s)=map(x->parse(Int,x),split(s))
const N,Q=parseInt(shift!(lines))
const G=[Tuple{Int,Int,Int}[] for _=1:N]
function init()
	for _=2:N
		a,b,c,d=parseInt(shift!(lines))
		push!(G[a],(b,c,d))
		push!(G[b],(a,c,d))
	end
end
init()
const depth=zeros(Int,N)
const parent=[zeros(Int,N) for _=1:18]
const tour=Pair{Int,Int}[]
const L=zeros(Int,N)
function dfs(u,p,de)
	depth[u]=de
	parent[1][u]=p
	L[u]=length(tour)+1
	for (v,c,d)=G[u]
		if v!=p
			push!(tour,c=>d)
			dfs(v,u,de+1)
			push!(tour,c=>-d)
		end
	end
end
dfs(1,0,0)
const edge=[Int[] for _=1:N-1]
const edgesum=[Int[0] for _=1:N-1]
const edgecnt=[Int[0] for _=1:N-1]
function edgeinit()
	now=zeros(Int,N-1)
	cnt=zeros(Int,N-1)
	for i=1:length(tour)
		c,d=tour[i]
		cnt[c]+=d>0?1:-1
		now[c]+=d
		push!(edge[c],i)
		push!(edgesum[c],now[c])
		push!(edgecnt[c],cnt[c])
	end
end
edgeinit()
const cumsum=Int[0]
function cumsuminit()
	now=0
	for (c,d)=tour
		push!(cumsum,now+=d)
	end
end
cumsuminit()
function lca(u,v)
	if depth[u]>depth[v]
		u,v=v,u
	end
	for k=1:18
		if ((depth[v]-depth[u])>>(k-1))%2==1
			v=parent[k][v]
		end
	end
	if u==v
		return u
	end
	for k=18:-1:1
		if parent[k][u]!=parent[k][v]
			u=parent[k][u]
			v=parent[k][v]
		end
	end
	parent[1][u]
end
function lcainit()
	for k=2:18
		for i=1:N
			if parent[k-1][i]>0
				parent[k][i]=parent[k-1][parent[k-1][i]]
			else
				parent[k][i]=0
			end
		end
	end
end
lcainit()
function bsearch(A,x)
	L=0
	R=length(A)
	while R-L>1
		M=div(L+R,2)
		if A[M]>=x
			R=M
		else
			L=M
		end
	end
	L
end
function get(l,r,c)
	lid=bsearch(edge[c],l)
	rid=bsearch(edge[c],r)
	edgecnt[c][rid+1]-edgecnt[c][lid+1],edgesum[c][rid+1]-edgesum[c][lid+1]
end
function calc(u,v,x,y)
	flag=false
	if L[u]>L[v]
		u,v=v,u
		flag=true
	end
	ret=cumsum[L[v]]-cumsum[L[u]]
	cnt,sum=get(L[u],L[v],x)
	ret-=sum
	ret+=cnt*y
	if flag
		ret=-ret
	end
	ret
end
function query(u,v,x,y)
	pa=lca(u,v)
	calc(pa,u,x,y)+calc(pa,v,x,y)
end
function solve()
	for s=lines
		x,y,u,v=parseInt(s)
		println(query(u,v,x,y))
	end
end
solve()

Score : 600 points

Problem Statement

There is a tree with N vertices numbered 1 to N.
The i-th edge in this tree connects Vertex a_i and Vertex b_i, and the color and length of that edge are c_i and d_i, respectively.
Here the color of each edge is represented by an integer between 1 and N-1 (inclusive). The same integer corresponds to the same color, and different integers correspond to different colors.

Answer the following Q queries:

Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.)

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

1 \leq a_i, b_i \leq N

1 \leq c_i \leq N-1

1 \leq d_i \leq 10^4

1 \leq x_j \leq N-1

1 \leq y_j \leq 10^4

1 \leq u_j < v_j \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 Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q

Output

Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to Query j.

Sample Input 1

5 3
1 2 1 10
1 3 2 20
2 4 4 30
5 2 1 40
1 100 1 4
1 100 1 5
3 1000 3 4

Sample Output 1

130
200
60

The graph in this input is as follows:

Here the edges of Color 1 are shown as solid red lines, the edge of Color 2 is shown as a bold green line, and the edge of Color 4 is shown as a blue dashed line.

Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.

Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.

Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths.


function main()

  S = split(chomp(readline()),"")

  sort!(S)

  if S[1]*S[3] == S[2]*S[4]
    println("Yes")
  else
    println("No")
  end

end

main()

Score : 100 points

Problem Statement

You are given a 4-character string S consisting of uppercase English letters.
Determine if S consists of exactly two kinds of characters which both appear twice in S.

Constraints

The length of S is 4.

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S consists of exactly two kinds of characters which both appear twice in S, print Yes; otherwise, print No.

Sample Input 1

ASSA

Sample Output 1

Yes

S consists of A and S which both appear twice in S.

Sample Input 2

STOP

Sample Output 2

No

Sample Input 3

FFEE

Sample Output 3

Yes

Sample Input 4

FREE

Sample Output 4

No


cnt=zeros(Int,114514)
c=0
for s=chomp(readline())
	c+=(cnt[Int(s)]+=1)==2
end
println(c==2?"Yes":"No")


str = readline();
arr = split(str, "");
sort!(arr)
if (arr[1] == arr[2] && arr[3] == arr[4] && arr[1] != arr[3])
    println("Yes")
else
    println("No")
end


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n=parseInt(readline())
    p=parseMap(split(readline()))
    ans=0
    for i in 3:n
        if maximum(p[i-2:i])!=p[i-1] && minimum(p[i-2:i])!=p[i-1]
            ans+=1
        end
    end
    println(ans)
end
main()

Score : 200 points

Problem Statement

We have a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n}.

Print the number of elements p_i (1 < i < n) that satisfy the following condition:

p_i is the second smallest number among the three numbers p_{i - 1}, p_i, and p_{i + 1}.

Constraints

All values in input are integers.

3 \leq n \leq 20

p is a permutation of {1,\ 2,\ ...,\ n}.

Input

Input is given from Standard Input in the following format:

n
p_1 p_2 ... p_n

Output

Print the number of elements p_i (1 < i < n) that satisfy the condition.

Sample Input 1

5
1 3 5 4 2

Sample Output 1

2

p_2 = 3 is the second smallest number among p_1 = 1, p_2 = 3, and p_3 = 5. Also, p_4 = 4 is the second smallest number among p_3 = 5, p_4 = 4, and p_5 = 2. These two elements satisfy the condition.

Sample Input 2

9
9 6 3 2 5 8 7 4 1

Sample Output 2

5


parseInt(x) = parse(Int,x)
parseMap(x::Array{SubString{String}, 1}) = map(parseInt, x)

function main()
  n = parseInt(readline())
  p = map(parseInt,split(readline()))
  k=0
  for i=2:(n-1)
    if p[i-1] >= p[i] >= p[i+1]
      if p[i+1] >= p[i] >= p[i-1]
        k = k+1
      end
    end
  end
  println(k)
end

main()


Score : 300 points

Problem Statement

Takahashi made N problems for competitive programming.
The problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

He is dividing the problems into two categories by choosing an integer K, as follows:

A problem with difficulty K or higher will be for ARCs.

A problem with difficulty lower than K will be for ABCs.

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

Problem Statement

2 \leq N \leq 10^5

N is an even number.

1 \leq d_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 1

6
9 1 4 4 6 7

Sample Output 1

2

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.
Thus, the answer is 2.

Sample Input 2

8
9 1 14 5 5 4 4 14

Sample Output 2

0

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 3

14
99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1

Sample Output 3

42685


n, k = map(x->parse(BigInt,x),split(readline()))
for i = 1:k
  println(binomial(n-k+1,i)*binomial(k-1,i-1)%(10^9+7))
end

Score : 400 points

Problem Statement

There are K blue balls and N-K red balls. The balls of the same color cannot be distinguished. Snuke and Takahashi are playing with these balls.

First, Snuke will arrange the N balls in a row from left to right.

Then, Takahashi will collect only the K blue balls. In one move, he can collect any number of consecutive blue balls. He will collect all the blue balls in the fewest moves possible.

How many ways are there for Snuke to arrange the N balls in a row so that Takahashi will need exactly i moves to collect all the blue balls? Compute this number modulo 10^9+7 for each i such that 1 \leq i \leq K.

Constraints

1 \leq K \leq N \leq 2000

Input

Input is given from Standard Input in the following format:

N K

Output

Print K lines. The i-th line (1 \leq i \leq K) should contain the number of ways to arrange the N balls so that Takahashi will need exactly i moves to collect all the blue balls, modulo 10^9+7.

Sample Input 1

5 3

Sample Output 1

3
6
1

There are three ways to arrange the balls so that Takahashi will need exactly one move: (B, B, B, R, R), (R, B, B, B, R), and (R, R, B, B, B). (R and B stands for red and blue, respectively).

There are six ways to arrange the balls so that Takahashi will need exactly two moves: (B, B, R, B, R), (B, B, R, R, B), (R, B, B, R, B), (R, B, R, B, B), (B, R, B, B, R), and (B, R, R, B, B).

There is one way to arrange the balls so that Takahashi will need exactly three moves: (B, R, B, R, B).

Sample Input 2

2000 3

Sample Output 2

1998
3990006
327341989

Be sure to print the numbers of arrangements modulo 10^9+7.


parseInt(x::SubString{String}) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function abmp(a,b,p);if b==0;1;elseif b%2==0;k=abmp(a,div(b,2),p);(k*k)%p;else;(a*abmp(a,b-1,p))%p;end;end;;
function binarr(n,p);z=1;for i in 1:n;z=z*i%p;end;fac=Array{Int}(n+1);inv=Array{Int}(n+1);inv[n+1]=abmp(z,p-2,p)
for i in 0:n-1;inv[n-i]=(inv[n+1-i]*(n-i))%p;end;for i in 0:n;fac[i+1]=(((z*inv[i+1])%p)*inv[n+1-i])%p;end;fac;end;

function main()
	p = 1000000007
	n,k = readline() |> split |> parseMap
	a = binarr(n-k+1,p)
	b = binarr(k-1,p)
	for i in 1:min(k,n-k+1)
		println(a[i+1]*b[i]%p)
	end
	if k>n-k+1
		for i in n-k+2:k
			println(b[i])
		end
	end
end

main()


const lines=readlines()
input()=shift!(lines)
int(x)=parse(Int,x)
function intLine()
    s=input()|>split
    int(s[1]),int(s[2])
end
function bfs(n,e,s,t)
    f=ones(Int,(n,3))*-3
    f[s,1]=0
    q=Array{Pair{Int,Int}}([s=>1])
    while !isempty(q)
        v,m=shift!(q)
        u=m>2?1:m+1
        for w=e[v]
            if f[w,u]<0
                f[w,u]=f[v,m]+1
                push!(q,w=>u)
            end
        end
    end
    f[t,1]÷3
end
function main()
    n,m=intLine()
    e=Array{Int,1}[[]for i=1:n]
    for i=1:m
        u,v=intLine()
        push!(e[u],v)
    end
    s,t=intLine()
    show(bfs(n,e,s,t))
end
main()

Score : 500 points

Problem Statement

Ken loves ken-ken-pa (Japanese version of hopscotch). Today, he will play it on a directed graph G.
G consists of N vertices numbered 1 to N, and M edges. The i-th edge points from Vertex u_i to Vertex v_i.

First, Ken stands on Vertex S. He wants to reach Vertex T by repeating ken-ken-pa. In one ken-ken-pa, he does the following exactly three times: follow an edge pointing from the vertex on which he is standing.

Determine if he can reach Vertex T by repeating ken-ken-pa. If the answer is yes, find the minimum number of ken-ken-pa needed to reach Vertex T. Note that visiting Vertex T in the middle of a ken-ken-pa does not count as reaching Vertex T by repeating ken-ken-pa.

Constraints

2 \leq N \leq 10^5

0 \leq M \leq \min(10^5, N (N-1))

1 \leq u_i, v_i \leq N(1 \leq i \leq M)

u_i \neq v_i (1 \leq i \leq M)

If i \neq j, (u_i, v_i) \neq (u_j, v_j).

1 \leq S, T \leq N

S \neq T

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1
:
u_M v_M
S T

Output

If Ken cannot reach Vertex T from Vertex S by repeating ken-ken-pa, print -1.
If he can, print the minimum number of ken-ken-pa needed to reach vertex T.

Sample Input 1

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

Sample Output 1

2

Ken can reach Vertex 3 from Vertex 1 in two ken-ken-pa, as follows: 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 in the first ken-ken-pa, then 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 in the second ken-ken-pa. This is the minimum number of ken-ken-pa needed.

Sample Input 2

3 3
1 2
2 3
3 1
1 2

Sample Output 2

-1

Any number of ken-ken-pa will bring Ken back to Vertex 1, so he cannot reach Vertex 2, though he can pass through it in the middle of a ken-ken-pa.

Sample Input 3

2 0
1 2

Sample Output 3

-1

Vertex S and Vertex T may be disconnected.

Sample Input 4

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

Sample Output 4

2


parseInt(x::SubString{String}) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function hpush(h::Array{Tuple{Int,Int},1},x::Tuple{Int,Int})
	push!(h,x)
	if length(h) > 1
		l = length(h)
		k = div(l,2)
		while h[k][2]>x[2]&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k = max(div(k,2),1)
			l = div(l,2)
		end
	end
end

function hpop(h::Array{Tuple{Int,Int},1})
	x = h[1]
	z = pop!(h)
	if !isempty(h)
		h[1] = z
		k = 1
		l = length(h)
		while k <= l
			if 2*k>=l
				break
			elseif 2*k+1>=l
				if h[2*k][2]>=z[2]
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if h[2*k][2]>=z[2]&&h[2*k+1][2]>=z[2]
					break
				else
					t = h[2*k][2]<h[2*k+1][2]?2*k:2*k+1
					tmp = h[t]
					h[t] = z
					h[k] = tmp
					k = t
				end
			end
		end
	end
	x
end

function main()
	n,m = readline() |> split |> parseMap
	e = [Int[] for i in 1:3*n]
	for i in 1:m
		u,v = readline() |> split |> parseMap
		push!(e[u],n+v)
		push!(e[n+u],2*n+v)
		push!(e[2*n+u],v)
	end
	s,t = readline() |> split |> parseMap
	dis = -ones(Int,3*n)
	h = Array{Tuple{Int,Int},1}()
	push!(h,(s,0))
	while !isempty(h)
		v,w = hpop(h)
		if v == t
			break
		end
		for i in 1:length(e[v])
			if dis[e[v][i]]<0　|| dis[e[v][i]]>w+1
				dis[e[v][i]] = w+1
				hpush(h,(e[v][i],w+1))
			end
		end
	end
	println(dis[t]>=0?div(dis[t],3):-1)

end

main()


const lines=readlines()
input()=shift!(lines)
int(x)=parse(Int,x)
intLine()=map(int,split(input()))
function inE(e)
    u,v=intLine()
    push!(e[u],v)
end
function main()
    n,m=intLine()
    e=Array{Int,1}[[]for i=1:n]
    for i=1:m;inE(e);end
    s,t=intLine()
    f=ones(Int,(n,3))*-3
    f[s,1]=0
    q=Array{Int,1}[[s,1]]
    while !isempty(q)
        v,m=shift!(q)
        u=m>2?1:m+1
        for w=e[v]
            if f[w,u]<0
                f[w,u]=f[v,m]+1
                push!(q,[w,u])
            end
        end
    end
    show(f[t,1]÷3)
end
main()


Score : 600 points

Problem Statement

Find the number of sequences of length K consisting of positive integers such that the product of any two adjacent elements is at most N, modulo 10^9+7.

Constraints

1\leq N\leq 10^9

1 2\leq K\leq 100 (fixed at 21:33 JST)

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of sequences, modulo 10^9+7.

Sample Input 1

3 2

Sample Output 1

5

(1,1), (1,2), (1,3), (2,1), and (3,1) satisfy the condition.

Sample Input 2

10 3

Sample Output 2

147

Sample Input 3

314159265 35

Sample Output 3

457397712


Score : 100 points

Problem Statement

The door of Snuke's laboratory is locked with a security code.

The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same.

You are given the current security code S. If S is hard to enter, print Bad; otherwise, print Good.

Constraints

S is a 4-character string consisting of digits.

Input

Input is given from Standard Input in the following format:

S

Output

If S is hard to enter, print Bad; otherwise, print Good.

Sample Input 1

3776

Sample Output 1

Bad

The second and third digits are the same, so 3776 is hard to enter.

Sample Input 2

8080

Sample Output 2

Good

There are no two consecutive digits that are the same, so 8080 is not hard to enter.

Sample Input 3

1333

Sample Output 3

Bad

Sample Input 4

0024

Sample Output 4

Bad


parseInt(x) = parse(Int, x)
parseArray(x::Array{SubString{String}, 1}) = map(parseInt, x)

function main()
  N,L = readline() |> split |> parseArray

  ix=collect(1:N)
  _,i=findmin(abs(ix+L-1))
  result=sum(ix+L-1)-(i+L-1)

  println(result)
end

main()

Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044


Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368


a,b,c,d=parse.(split(readline()))
a-=1
f(x)=b÷x-a÷x
println(b-a-f(c)-f(d)+f(lcm(c,d)))


parseInt(x) = parse(Int128,x)

function main()
  (A,B,C,D) = map(parseInt, split(readline()))
  if B/C < 1
    dc = 0
  else
    if A/C < 1
      dc = Int(floor(B/C))
    else
      dc = Int(floor(B/C))-Int(ceil(A/C))+1
    end
  end
  if B/D < 1
    dd = 0
  else
    if A/D < 1
      dd = Int(floor(B/D))
    else
      dd = Int(floor(B/D))-Int(ceil(A/D))+1
    end
  end
  E = lcm(C,D)
  if B/E < 1
    de = 0
  else
    if A/E < 1
      de = Int(floor(B/E))
    else
      de = Int(floor(B/E))-Int(ceil(A/E))+1
    end
  end
  k = (B-A+1)-(dc+dd-de)
  println(k)
end

main()


#read test
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n = readline() |> parseInt
	#buf = zeros(UInt8,100)
	#println(length(buf))
	buf = read(STDIN,5000000)
	#println(size(buf))
	str = transcode(String,buf)
	str = split(str,"\n")
	#println(length(str[1]))
	a = Array{Int}(2,n)
	for i in 1:n
		a[:,i] = str[i] |> split |> parseMap
	end
	a = sortcols(a, by=z->z[2])
	t = 0
	flg = 0
	for i in 1:n
		t += a[1,i]
		if t > a[2,i]
			flg = 1
			break
		end
	end
	println(flg==0?"Yes":"No")
end

main()

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

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

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

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


parseInt(x) = parse(Int, x)
parseArray(x::Array{SubString{String}, 1}) = map(parseInt, x)

function main()
  N,K = readline() |> split |> parseArray

  b=binomial(N-1,2)
  if K>b
    println(-1)
    return
  end

  M=N-1+(b-K)
  println(M)

  for i in 2:N
    println("1 $i")
  end
  # for i in 2:min(M-N+2,N)
  #   println("$i $(i%N+1)")
  # end
  # for i in 2:min(M-2N+3,N)
  #   println("$i $((i+1)%N+1)")
  # end

  for j in 1:M÷(N-1)+1
    for i in 2:min(M-j*(N-1)+1,N)
      println("$i $((i+j-1)%N+1)")
    end
  end
end

main()

Score: 500 points

Problem Statement

Does there exist an undirected graph with N vertices satisfying the following conditions?

The graph is simple and connected.

The vertices are numbered 1, 2, ..., N.

Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M, the length of each edge is 1, and Edge i connects Vertex u_i and Vertex v_i.

There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2.

If there exists such a graph, construct an example.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq K \leq \frac{N(N - 1)}{2}

Input

Input is given from Standard Input in the following format:

N K

Output

If there does not exist an undirected graph with N vertices satisfying the conditions, print -1.

If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):

M
u_1 v_1
:
u_M v_M

If there exist multiple graphs satisfying the conditions, any of them will be accepted.

Sample Input 1

5 3

Sample Output 1

5
4 3
1 2
3 1
4 5
2 3

This graph has three pairs of vertices such that the shortest distance between them is 2: (1,\ 4), (2,\ 4), and (3,\ 5). Thus, the condition is satisfied.

Sample Input 2

5 8

Sample Output 2

-1

There is no graph satisfying the conditions.


function main()

  (X,A) = map(x -> parse(Int,x), split(readline()))

  if X < A
    println(0)
  else
    println(10)
  end

end

main()

Score : 100 points

Problem Statement

X and A are integers between 0 and 9 (inclusive).

If X is less than A, print 0; if X is not less than A, print 10.

Constraints

0 \leq X, A \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X A

Output

If X is less than A, print 0; if X is not less than A, print 10.

Sample Input 1

3 5

Sample Output 1

0

3 is less than 5, so we should print 0.

Sample Input 2

7 5

Sample Output 2

10

7 is not less than 5, so we should print 10.

Sample Input 3

6 6

Sample Output 3

10

6 is not less than 6, so we should print 10.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    n, x  = readline() |> split |> parseMap
    l = readline() |> split |> parseMap
    counter = 0
    place = 0
    for i in l
        if place > x
            break
        end
        counter += 1
        place += i
    end
    println(counter)
end

main()

Score : 200 points

Problem Statement

A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \leq i \leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}.

How many times will the ball make a bounce where the coordinate is at most X?

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 100

1 \leq X \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 L_2 ... L_{N-1} L_N

Output

Print the number of times the ball will make a bounce where the coordinate is at most X.

Sample Input 1

3 6
3 4 5

Sample Output 1

2

The ball will make a bounce at the coordinates 0, 3, 7 and 12, among which two are less than or equal to 6.

Sample Input 2

4 9
3 3 3 3

Sample Output 2

4

The ball will make a bounce at the coordinates 0, 3, 6, 9 and 12, among which four are less than or equal to 9.


parseInt(x) = parse(Int,x)

function main()
  (N,X) = map(parseInt,split(readline()))
  L = map(parseInt,split(readline()))
  D = zeros(Int64,N+1)
  for i=1:N
    D[i+1] = D[i]+L[i]
  end
  j = 0
  for i = 1:(N+1)
    if X >= D[i]
      j = i
    end
  end
  println(j)
end

main()


using DataStructures
const double = Float64
const int = Int64

parseint(x) = parse(int, x)
readint() = map(parseint, split(readline()))

function main()
  W,H,x,y = readint()

  print(W*H/2, " ")
  if x == W/2 && y == H/2
    println("1")
  else
    println("0")
  end

end

main()

Score : 300 points

Problem Statement

There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).
You are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.

Constraints

1 \leq W,H \leq 10^9

0\leq x\leq W

0\leq y\leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

W H x y

Output

Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.

The area printed will be judged correct when its absolute or relative error is at most 10^{-9}.

Sample Input 1

2 3 1 2

Sample Output 1

3.000000 0

The line x=1 gives the optimal cut, and no other line does.

Sample Input 2

2 2 1 1

Sample Output 2

2.000000 1


function main()

  (W, H, x, y) = map(x -> parse(Int, x), split(readline()))

  A = W * H

  max_x = max(A * (x/W), A * ((W-x)/W))
  max_y = max(A * (y/H), A * ((H-y)/H))

  if 2x == W && 2y == H
    println(max_x, " ", "1")
  else
    println(min(max_x, max_y), " ", "0")
  end

end

main()


function main()

  (W,H,x,y) = map(x -> parse(Int,x), split(readline()))

  if W-x == 0 || x == 0
    A1 = W*H
  else
  	A1 = min(x*H,(W-x)*H)
  end

  if H-y == 0 || y == 0
    A2 = W*H
  else
  	A2 = min(W*y,W*(H-y))
  end

  if A1 == A2
    println(A1, " 1")
  else
    println(max(A1,A2), " 0")
  end

end

main()


lines=readlines(open("/dev/fd/0"))
input()=shift!(lines)
int(s::String)=parse(Int,s)
intSub(s::SubString{String})=parse(Int,s)
intLine(s::Array{SubString{String},1})=map(intSub,s)
function main()
  w,h,x,y=intLine(split(input()))
  println(.5w*h,' ',+(.5(w+h)==x+y))
end
main()


a,b,c,d=map(x->parse(Int64,x),split(readline()))
@printf("%.9f\n",a*b/2)
if c*2==a&d*2==b
	println(1)
else
	println(0)
end


n,m=map(x->parse(Int,x),split(readline()))
s=map(x->parse(Int,x),split(readline()))
t=map(x->parse(Int,x),split(readline()))
sum=ones(Int,m+1)
mod=10^9+7
for ss=s
	sum[2:m+1]+=cumsum([ss==tt?sum%mod:0 for (tt,sum) in zip(t,sum[1:m])])
end
println(sum[m+1]%mod)

Score : 500 points

Problem Statement

You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive).

In how many pairs of a subsequence of S and a subsequence of T do the two subsequences are the same in content?

Here the subsequence of A is a sequence obtained by removing zero or more elements from A and concatenating the remaining elements without changing the order.

For both S and T, we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Since the answer can be tremendous, print the number modulo 10^9+7.

Constraints

1 \leq N, M \leq 2 \times 10^3

The length of S is N.

The length of T is M.

1 \leq S_i, T_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M}

Output

Print the number of pairs of a subsequence of S and a subsequence of T such that the subsequences are the same in content, modulo 10^9+7.

Sample Input 1

2 2
1 3
3 1

Sample Output 1

3

S has four subsequences: (), (1), (3), (1, 3).

T has four subsequences: (), (3), (1), (3, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 1 \times 1 pair of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (3), for a total of three pairs.

Sample Input 2

2 2
1 1
1 1

Sample Output 2

6

S has four subsequences: (), (1), (1), (1, 1).

T has four subsequences: (), (1), (1), (1, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 2 \times 2 pairs of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (1,1), for a total of six pairs.
Note again that we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Sample Input 3

4 4
3 4 5 6
3 4 5 6

Sample Output 3

16

Sample Input 4

10 9
9 6 5 7 5 9 8 5 6 7
8 6 8 5 5 7 9 9 7

Sample Output 4

191

Sample Input 5

20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 5

846527861

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


n,m=map(x->parse(Int,x),split(readline()))
s=map(x->parse(Int,x),split(readline()))
t=map(x->parse(Int,x),split(readline()))
dp=zeros(Int,m+1)
sum=ones(Int,m+1)
mod=10^9+7
i=0
while i<n
	i+=1
	j=0
	while j<m
		j+=1
		if s[i]==t[j]
			dp[j+1]=sum[j]
		else
			dp[j+1]=0
		end
	end
	now=0
	j=0
	while j<=m
		j+=1
		now+=dp[j]
		sum[j]+=now
	end
end
println(sum[m+1]%mod)


parseInt(x) = parse(Int,x)

function main()
    MOD = 1000000007
    n, m = map(parseInt, split(readline()))
    a = map(parseInt, split(readline()))
    b = map(parseInt, split(readline()))

    ans = 0
    dp = zeros(Int, 2, n)

    ans = 1

    pre = 1
    for i in 1:n
        if a[i] == b[1]
            dp[1,i] = pre + 1
            ans += 1
        else
            dp[1,i] = pre
        end
        pre = dp[1,i]
    end

    for i in 2:m
        cur = (i%2 == 1) ? 1 : 2
        ant = (i%2 == 1) ? 2 : 1

        pre = 1
        for j in 1:n
            if a[j] == b[i]
                dp[cur,j] = pre
                ans += pre
            else
                dp[cur,j] = 0
            end
            pre = dp[ant,j]
        end

        for j in 2:n
            dp[cur,j] += dp[cur, j-1]
            dp[cur,j] %= MOD
        end

        for j in 1:n
            dp[cur,j] += dp[ant, j]
            dp[cur,j] %= MOD
        end

        ans %= MOD
    end

    println(ans)
end

main()


parseInt(x) = parse(Int,x)

function main()
    MOD = 1000000007
    n, m = map(parseInt, split(readline()))
    a = map(parseInt, split(readline()))
    b = map(parseInt, split(readline()))

    ans = 0
    dp = zeros(Int, 2, n)

    ans = 1

    pre = 1
    for i in 1:n
        if a[i] == b[1]
            dp[1,i] = pre + 1
            ans += 1
        else
            dp[1,i] = pre
        end
        pre = dp[1,i]
    end

    for i in 2:m
        cur = (i%2 == 1) ? 1 : 2
        ant = (i%2 == 1) ? 2 : 1

        pre = 1
        for j in 1:n
            if a[j] == b[i]
                dp[cur,j] = pre
                ans += pre
            else
                dp[cur,j] = 0
            end
            pre = dp[ant,j]
        end

        for j in 2:n
            dp[cur,j] += dp[cur, j-1]
        end

        for j in 1:n
            dp[cur,j] += dp[ant, j]
            dp[cur,j] %= MOD
        end

        ans %= MOD
    end

    println(ans)
end

main()


function main()
	lines=readlines();
	N=parse(Int,shift!(lines))
	INF=10^9
	mx=[INF,INF,INF,INF]
	my=mx[:]
	Mx=-mx
	My=Mx[:]
	for i=1:N
		x,y,d=split(shift!(lines))
		x=parse(Int,x)
		y=parse(Int,y)
		if d=="L"
			d=1
		elseif d=="R"
			d=2
		elseif d=="U"
			d=3
		elseif d=="D"
			d=4
		end
		mx[d]=min(mx[d],x)
		my[d]=min(my[d],y)
		Mx[d]=max(Mx[d],x)
		My[d]=max(My[d],y)
	end
	T=[0.0]
	push!(T,(mx[1]-mx[2])/2)
	push!(T,mx[1]-min(mx[3],mx[4]))
	push!(T,min(mx[3],mx[4])-mx[2])
	push!(T,(Mx[1]-Mx[2])/2)
	push!(T,Mx[1]-max(Mx[3],Mx[4]))
	push!(T,max(Mx[3],Mx[4])-Mx[2])
	push!(T,(my[4]-my[3])/2)
	push!(T,my[4]-min(my[1],my[2]))
	push!(T,min(my[1],my[2])-my[3])
	push!(T,(My[4]-My[3])/2)
	push!(T,My[4]-max(My[1],My[2]))
	push!(T,max(My[1],My[2])-My[3])
	ans=10^18
	for t=T
		if t<0;continue;end
		X=max(Mx[1]-t,Mx[2]+t,Mx[3],Mx[4])-min(mx[1]-t,mx[2]+t,mx[3],mx[4])
		Y=max(My[1],My[2],My[3]+t,My[4]-t)-min(my[1],my[2],my[3]+t,my[4]-t)
		ans=min(ans,X*Y)
	end
	println(ans)
end
main()

Score : 600 points

Problem Statement

There are N points in a two-dimensional plane. The initial coordinates of the i-th point are (x_i, y_i). Now, each point starts moving at a speed of 1 per second, in a direction parallel to the x- or y- axis. You are given a character d_i that represents the specific direction in which the i-th point moves, as follows:

If d_i = R, the i-th point moves in the positive x direction;

If d_i = L, the i-th point moves in the negative x direction;

If d_i = U, the i-th point moves in the positive y direction;

If d_i = D, the i-th point moves in the negative y direction.

You can stop all the points at some moment of your choice after they start moving (including the moment they start moving).
Then, let x_{max} and x_{min} be the maximum and minimum among the x-coordinates of the N points, respectively. Similarly, let y_{max} and y_{min} be the maximum and minimum among the y-coordinates of the N points, respectively.

Find the minimum possible value of (x_{max} - x_{min}) \times (y_{max} - y_{min}) and print it.

Constraints

1 \leq N \leq 10^5

-10^8 \leq x_i,\ y_i \leq 10^8

x_i and y_i are integers.

d_i is R, L, U, or D.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 d_1
x_2 y_2 d_2
.
.
.
x_N y_N d_N

Output

Print the minimum possible value of (x_{max} - x_{min}) \times (y_{max} - y_{min}).

The output will be considered correct when its absolute or relative error from the judge's output is at most 10^{-9}.

Sample Input 1

2
0 3 D
3 0 L

Sample Output 1

0

After three seconds, the two points will meet at the origin. The value in question will be 0 at that moment.

Sample Input 2

5
-7 -10 U
7 -6 U
-8 7 D
-3 3 D
0 -6 R

Sample Output 2

97.5

The answer may not be an integer.

Sample Input 3

20
6 -10 R
-4 -9 U
9 6 D
-3 -2 R
0 7 D
4 5 D
10 -10 U
-1 -8 U
10 -6 D
8 -5 U
6 4 D
0 3 D
7 9 R
9 -4 R
3 10 D
1 9 U
1 -6 U
9 -8 R
6 7 D
7 -3 D

Sample Output 3

273


function main()

  (N,K) = map(x -> parse(Int,x), split(readline()))

  k = zeros(Int,K)

  for i in 1:K

    if i == K
      k[i] = N
    else
      k[i] = 1
      N -= 1
    end

  end

  println(k[end]-1)

end

main()

Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.

Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.

Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.


parseInt(x) = parse(Int, x)
parseBigInt(x) = parse(BigInt, x)
parseFloat(x) = parse(Float64, x)
parseBigFloat(x) = parse(BigFloat, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    n=parseInt(readline())
    data=[parseMap(split(readline())) for i in 1:n]
    arr=Vector{Tuple{Int,Int}}()
    for i in 1:n
        for j in 1:n
            if i==j
                continue
            end
            push!(arr,(data[i][1]-data[j][1],data[i][2]-data[j][2]))
        end
    end
    cnt=Dict{Tuple{Int,Int},Int}()
    M=0
    for i in arr
        if haskey(cnt,i)
            cnt[i]+=1
        else
            cnt[i]=1
        end
        M=max(M,cnt[i])
    end
    println(n-M)
end
main()

Score : 300 points

Problem Statement

There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i).

We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation:

Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1.

Find the minimum total cost required to collect all the balls when we optimally choose p and q.

Constraints

1 \leq N \leq 50

|x_i|, |y_i| \leq 10^9

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum total cost required to collect all the balls.

Sample Input 1

2
1 1
2 2

Sample Output 1

1

If we choose p = 1, q = 1, we can collect all the balls at a cost of 1 by collecting them in the order (1, 1), (2, 2).

Sample Input 2

3
1 4
4 6
7 8

Sample Output 2

1

If we choose p = -3, q = -2, we can collect all the balls at a cost of 1 by collecting them in the order (7, 8), (4, 6), (1, 4).

Sample Input 3

4
1 1
1 2
2 1
2 2

Sample Output 3

2


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	a = sort(a)
	m = 0
	h = div(n,2)
	for i in 1:n
		if i<=h
			m -= a[i]
		elseif i == h+1 && a[i] < 0
			m -=a[i]
		else
			m += a[i]
		end
	end
	println(m)
	b = Array{Int}(n)
	t = 1
	if a[h+1] < 0
		for i in 1:h
			b[t] = a[n-i+1]
			t += 1
			b[t] = a[i]
			t += 1
		end
	else
		for i in 1:h
			b[t] = a[i]
			t += 1
			b[t] = a[n-i+1]
			t += 1
		end
	end
	if n%2 == 1
		b[n] = a[h+1]
	end
	k = 1
	if a[h+1] < 0
		t += 1
	end
	while length(b)>1
		x = shift!(b)
		y = shift!(b)
		if length(b) == 0
			println(max(x,y), " ", min(x,y))
			push!(b,x-y)
		elseif k%2 == 1
			println(x, " ", y)
			push!(b, x-y)
		else
			println(y, " ", x)
			push!(b, y-x)
		end
		k += 1
	end
end

main()

Score : 500 points

Problem Statement

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

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

2 \leq N \leq 10^5

-10^4 \leq A_i \leq 10^4

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 possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	a = sort(a)
	m = 0
	h = div(n,2)
	for i in 1:n
		if i<=h
			m -= a[i]
		else
			m += a[i]
		end
	end
	println(m)
	b = Array{Int}(n)
	t = 1
	for i in 1:h
		b[t] = a[i]
		t += 1
		b[t] = a[n-i+1]
		t += 1
	end
	if n%2 == 1
		b[n] = a[h+1]
	end
	k = 1
	while length(b)>1
		x = shift!(b)
		y = shift!(b)
		if length(b) == 0
			println(max(x,y), " ", min(x,y))
			push!(b,x-y)
		elseif k%2 == 1
			println(x, " ", y)
			push!(b, x-y)
		else
			println(y, " ", x)
			push!(b, y-x)
		end
		k += 1
	end
end

main()