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
  f = readline() |> split |> parseArray

  sort!(a)
  sort!(f,rev=true)
  cost=0
  j=0
  for i in 1:n
    cost+=a[i]
    cost<k && (a[i]=0)
    if cost>k
      a[i]-=k-(cost-a[i])
      break
    end
  end
  ans=sum(a.*f)

  println(ans)
end

main()


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

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

Score : 100 points

Problem Statement

The window of Takahashi's room has a width of A. There are two curtains hung over the window, each of which has a horizontal length of B. (Vertically, the curtains are long enough to cover the whole window.)

We will close the window so as to minimize the total horizontal length of the uncovered part of the window.
Find the total horizontal length of the uncovered parts of the window then.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the total horizontal length of the uncovered parts of the window.

Sample Input 1

12 4

Sample Output 1

4

We have a window with a horizontal length of 12, and two curtains, each of length 4, that cover both ends of the window, for example. The uncovered part has a horizontal length of 4.

Sample Input 2

20 15

Sample Output 2

0

If the window is completely covered, print 0.

Sample Input 3

20 30

Sample Output 3

0

Each curtain may be longer than the window.

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.


function main()

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

  if A - 2B > 0
    println(A - 2B)
  else
    println(0)
  end

end

main()


parseline(str=readline()) = parse.(Int, split(str))
function binarysearches(arr_, left, right; rev = false)
    arr = rev ? arr_[end:-1:1] : arr_
    r = searchsortedfirst(arr, right, rev=false) - 1
    l = searchsortedlast(arr, left, rev=false) + 1
    ans = r - l + 1
    return max(0, ans)
end
function Triangles()
    N = parse(Int, readline())
    L = sort(parseline(), rev=true)
    ans = 0
    for n in 1:N-1
        for m in n+1:N
            L_ = [L[l] for l in 1:length(L) if l∉[m, n]]
#             println((L[m], L[n]))
#             println(L_)
#             println((L[n]-L[m], L[n]+L[m]))
#             println(binarysearches(L_, L[n]-L[m], L[n]+L[m], rev = true))
#             println("===============")
            ans += binarysearches(L_, L[n]-L[m], L[n]+L[m], rev = true)
        end
    end
    println(ans÷3)
end
Triangles()

Score : 400 points

Problem Statement

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

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

a < b + c

b < c + a

c < a + b

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

Constraints

All values in input are integers.

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

1 \leq L_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Constraints

Print the number of different triangles that can be formed.

Sample Input 1

4
3 4 2 1

Sample Output 1

1

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

Sample Input 2

3
1 1000 1

Sample Output 2

0

No triangles can be formed.

Sample Input 3

7
218 786 704 233 645 728 389

Sample Output 3

23


n = parse(Int,readline())
l = parse.(Int,split(readline()))
sort!(l)
ct = 0
for i in 1:n
    for j in i+1:n
        for k in j+1:n
            if l[k] < l[i] + l[j]
                global ct += 1
            end
        end
    end
end

print(ct)


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())
    l=sort(parseMap(split(readline())))
    ans=0
    for i in 1:n
        for j in i+1:n
            n1=searchsortedlast(l,l[i]+l[j]-1)
            n2=searchsortedfirst(l,-l[i]+l[j]+1)
            n1,n2=min(n1,n2),max(n1,n2)
            n1,n2=max(n1,j),min(n2,n)
            for k in n1:n2
                if i!=k!=j
                    ans+=1
                end
            end
        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()
    n=parseInt(readline())
    l=sort(parseMap(split(readline())))
    ans=0
    for i in 1:n
        for j in i+1:n
            num=searchsortedlast(l,l[i]+l[j]-1)
            for k in j+1:num
                if l[j]+l[k]>l[i] && l[i]+l[k]>l[j] && l[i]+l[j]>l[k]
                    ans+=1
                end
            end
        end
    end
    println(ans)
end
main()


function main()
	N=parse(Int,readline())
	L=sort(map(x->parse(Int,x),split(readline())))
	s=0
	for i=1:N
		k=i+2
		for j=i+1:N
			while k<=N&&L[k]<L[i]+L[j]
				k+=1
			end
			s+=k-j-1
		end
	end
	println(s)
end
main()


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

function hpush(h::Array{Tuple{Int,Int,Int},1},x::Tuple{Int,Int,Int})
	push!(h,x)
	if length(h) > 1
		l = length(h)
		k = div(l,2)
		while pareto(h[k],x)==1&&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 pareto(a::Tuple{Int,Int,Int},b::Tuple{Int,Int,Int})
	if a[2]>b[2] || (a[2]==b[2]&&a[3]>b[3])
		1
	elseif a[2]==b[2] && a[3]>b[3]
		0
	else
		-1
	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 pareto(h[2*k],z) >= 0
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if pareto(h[2*k],z[2])>=0&&pareto(h[2*k+1],z[2])>=0
					break
				else
					t = pareto(h[2*k],h[2*k+1])<0?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,l = readline() |> split |> parseMap
	g = [Tuple{Int,Int}[] for i in 1:n]
	for i in 1:m
		a,b,c = readline() |> split |> parseMap
		push!(g[a],(b,c))
		push!(g[b],(a,c))
	end
	k = Array{Tuple{Int,Int},2}(n,n)
	for i in 1:n
		for j in 1:n
			k[i,j] = i==j?(0,0):(-1,-1)
		end
	end
	for r in 1:n
		stack = Tuple{Int,Int,Int}[(r,0,l)]
		while !isempty(stack)
			now = shift!(stack)
			i = now[1]
			if (k[r,i] == (-1,-1)) ||  k[r,i][1]>now[2] || (k[r,i][1]==now[2]&&k[r,i][2]>now[3])
				k[r,i] = (now[2],now[3])
			end

			for jj in 1:length(g[i])
				j = g[i][jj][1]
				x = now[2]
				y = now[3]
				y -= g[i][jj][2]
				if y<0
					x += 1
					y = l-g[i][jj][2]
				end
				if y>=0 && (k[r,j]==(-1,-1) || k[r,j][1]>x || (k[r,j][1]==x && k[r,j][2]>y))
					k[r,j] = (x,y)
					hpush(stack,(j,x,y))
				end
			end
		end
	end
	q = readline() |> parseInt
	for i in 1:q
		s,t = readline() |> split |> parseMap
		println(k[s,t][1])
	end
end

main()

Score : 500 points

Problem Statement

There are N towns numbered 1 to N and M roads. The i-th road connects Town A_i and Town B_i bidirectionally and has a length of C_i.

Takahashi will travel between these towns by car, passing through these roads. The fuel tank of his car can contain at most L liters of fuel, and one liter of fuel is consumed for each unit distance traveled. When visiting a town while traveling, he can full the tank (or choose not to do so). Travel that results in the tank becoming empty halfway on the road cannot be done.

Process the following Q queries:

The tank is now full. Find the minimum number of times he needs to full his tank while traveling from Town s_i to Town t_i. If Town t_i is unreachable, print -1.

Constraints

All values in input are integers.

2 \leq N \leq 300

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

1 \leq L \leq 10^9

1 \leq A_i, B_i \leq N

A_i \neq B_i

\left(A_i, B_i\right) \neq \left(A_j, B_j\right) (if i \neq j)

\left(A_i, B_i\right) \neq \left(B_j, A_j\right) (if i \neq j)

1 \leq C_i \leq 10^9

1 \leq Q \leq N\left(N-1\right)

1 \leq s_i, t_i \leq N

s_i \neq t_i

\left(s_i, t_i\right) \neq \left(s_j, t_j\right) (if i \neq j)

Input

Input is given from Standard Input in the following format:

N M L
A_1 B_1 C_1
:
A_M B_M C_M
Q
s_1 t_1
:
s_Q t_Q

Output

Print Q lines.

The i-th line should contain the minimum number of times the tank needs to be fulled while traveling from Town s_i to Town t_i. If Town t_i is unreachable, the line should contain -1 instead.

Sample Input 1

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

Sample Output 1

0
1

To travel from Town 3 to Town 2, we can use the second road to reach Town 2 without fueling the tank on the way.

To travel from Town 1 to Town 3, we can first use the first road to get to Town 2, full the tank, and use the second road to reach Town 3.

Sample Input 2

4 0 1
1
2 1

Sample Output 2

-1

There may be no road at all.

Sample Input 3

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

Sample Output 3

0
0
1
2
0
0
1
2
0
0
0
1
1
1
0
0
2
2
1
0


parseInt(x) = parse(Int,x)

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

N, K = parseInt.(split(readline()))

H = parseMap(split(readline()))

count = 0

for i in H
  if i >= K
      count += 1
  end
end

println(count)

Score : 200 points

Problem Statement

N friends of Takahashi has come to a theme park.

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

The i-th friend is h_i centimeters tall.

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

Constraints

1 \le N \le 10^5

1 \le K \le 500

1 \le h_i \le 500

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
h_1 h_2 \ldots h_N

Output

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

Sample Input 1

4 150
150 140 100 200

Sample Output 1

2

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

Sample Input 2

1 500
499

Sample Output 2

0

Sample Input 3

5 1
100 200 300 400 500

Sample Output 3

5


parseInt(x) = parse(Int,x)

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

N = parse(readline())
A = parseMap(split(readline()))
a = Pair{Int,Int}[]
for i in 1:N
  push!(a,i=>A[i])
  end

sort!(a)
function printpair(x)
	for (i,j) in x
		print("$j ")
	end
	println("")
end

printpair(a)

Score : 300 points

Problem Statement

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

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

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

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

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

Constraints

1 \le N \le 10^5

1 \le A_i \le N

A_i \neq A_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

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

Sample Input 1

3
2 3 1

Sample Output 1

3 1 2

First, student number 3 entered the classroom.

Then, student number 1 entered the classroom.

Finally, student number 2 entered the classroom.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

1 2 3 4 5

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

8 2 4 5 6 7 3 1


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()
    a,b=parseMap(split(readline()))
    soinsu1=Set{Int}(1)
    soinsu2=Set{Int}(1)
    for i in 2:trunc(Int,sqrt(a))+1
        while a%i==0
            a÷=i
            push!(soinsu1,i)
        end
    end
    for i in 2:trunc(Int,sqrt(b))+1
        while b%i==0
            b÷=i
            push!(soinsu2,i)
        end
    end
    ans=0
    for i in soinsu1
        if i in soinsu2
            ans+=1
        end
    end
    println(ans)
end
main()

Score : 400 points

Problem Statement

Given are positive integers A and B.

Let us choose some number of positive common divisors of A and B.

Here, any two of the chosen divisors must be coprime.

At most, how many divisors can we choose?

Definition of common divisor

An integer d is said to be a common divisor of integers x and y when d divides both x and y.

Definition of being coprime

Integers x and y are said to be coprime when x and y have no positive common divisors other than 1.

Definition of dividing

An integer x is said to divide another integer y when there exists an integer \alpha such that y = \alpha x.

Constraints

All values in input are integers.

1 \leq A, B \leq 10^{12}

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of divisors that can be chosen to satisfy the condition.

Sample Input 1

12 18

Sample Output 1

3

12 and 18 have the following positive common divisors: 1, 2, 3, and 6.

1 and 2 are coprime, 2 and 3 are coprime, and 3 and 1 are coprime, so we can choose 1, 2, and 3, which achieve the maximum result.

Sample Input 2

420 660

Sample Output 2

4

Sample Input 3

1 2019

Sample Output 3

1

1 and 2019 have no positive common divisors other than 1.


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

function main()
	a,b = readline() |> split |> parseMap
	q = gcd(a,b)
	a = Int[]
	i = 1
	while i<=sqrt(q)
		if q%i == 0
			push!(a,i)
			push!(a,div(q,i))
		end
		i+=1
	end
	a = sort(a)
	f = ones(Int,length(a))
	for j in 1:length(a)
		for k in j+1:length(a)
			if gcd(a[j],a[k])>1
				f[k] = 0
			end
		end
	end
	println(sum(f))
end

main()


function f(N,dp,A,C)
	for i=1:2^N
		id=i-1
		dp[(id|C)+1]=min(dp[(id|C)+1],dp[i]+A)
	end
	dp
end
function main()
	N,M=map(x->parse(Int,x),split(readline()))
	dp=ones(Int,2^N)*1145141919
	dp[1]=0
	for i=1:M
		A,b=map(x->parse(Int,x),split(readline()))
		C=0
		for c=map(x->parse(Int,x),split(readline()))
			C+=1<<(c-1)
		end
		dp=f(N,dp,A,C)
	end
	ans=dp[end]
	println(ans < 1145141919 ? ans : -1)
end
main()

Score : 500 points

Problem Statement

We have N locked treasure boxes, numbered 1 to N.

A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.

Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.

Constraints

All values in input are integers.

1 \leq N \leq 12

1 \leq M \leq 10^3

1 \leq a_i \leq 10^5

1 \leq b_i \leq N

1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}

Output

Print the minimum cost required to unlock all the treasure boxes.
If it is impossible to unlock all of them, print -1.

Sample Input 1

2 3
10 1
1
15 1
2
30 2
1 2

Sample Output 1

25

We can unlock all the boxes by purchasing the first and second keys, at the cost of 25 yen, which is the minimum cost required.

Sample Input 2

12 1
100000 1
2

Sample Output 2

-1

We cannot unlock all the boxes.

Sample Input 3

4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3

Sample Output 3

69942


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

function main()
  h,w,a,b = readline() |> split |> parseArray

  aa=max(2a,1)
  bb=max(2b,1)

  if w÷aa!=h÷bb
    println("No")
    return
  end

  pat=falses((bb,aa))
  pat[1:b,a+1:aa]=true
  pat[b+1:bb,1:a]=true
  # println(pat)

  ans=falses((h,w))
  for i=1:min(w÷aa, h÷bb)
    ans[(i-1)*bb+1:i*bb,(i-1)*aa+1:i*aa]=pat
  end
  if w%aa!=0 || h%bb!=0
    i=w÷aa
    ans[i*bb+1:h,i*aa+1:w]=pat[1:h%bb,1:w%aa]
  end

  # println(ans)
  for i in 1:h,j in 1:w
    print(ans[i,j]?1:0)
    j==w && println()
  end
end

main()

Score : 300 points

Problem Statement

We have a square grid with H rows and W columns.
Snuke wants to write 0 or 1 in each of the squares.
Here, all of the following conditions have to be satisfied:

For every row, the smaller of the following is A: the number of 0s contained in the row, and the number of 1s contained in the row. (If these two numbers are equal, “the smaller” should be read as “either”.)

For every column, the smaller of the following is B: the number of 0s contained in the column, and the number of 1s contained in the column.

Determine if these conditions can be satisfied by writing 0 or 1 in each of the squares. If the answer is yes, show one way to fill the squares so that the conditions are satisfied.

Constraints

1 \leq H,W \leq 1000

0 \leq A

2 \times A \leq W

0 \leq B

2 \times B \leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W A B

Output

If the conditions cannot be satisfied by writing 0 or 1 in each of the squares, print -1.

If the conditions can be satisfied, print one way to fill the squares so that the conditions are satisfied, in the following format:

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

Here s_{ij} is the digit written in the square at the i-th row from the top and the j-th column from the left in the grid.

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

Sample Input 1

3 3 1 1

Sample Output 1

100
010
001

Every row contains two 0s and one 1, so the first condition is satisfied.
Also, every column contains two 0s and one 1, so the second condition is satisfied.

Sample Input 2

1 5 2 0

Sample Output 2

01010


Score: 100 points

Problem Statement

The weather in Takahashi's town changes day by day, in the following cycle: Sunny, Cloudy, Rainy, Sunny, Cloudy, Rainy, ...

Given is a string S representing the weather in the town today. Predict the weather tomorrow.

Constraints

S is Sunny, Cloudy, or Rainy.

Input

Input is given from Standard Input in the following format:

S

Output

Print a string representing the expected weather tomorrow, in the same format in which input is given.

Sample Input 1

Sunny

Sample Output 1

Cloudy

In Takahashi's town, a sunny day is followed by a cloudy day.

Sample Input 2

Rainy

Sample Output 2

Sunny


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    d=Dict{String,String}(["Sunny"=>"Cloudy","Cloudy"=>"Rainy","Rainy"=>"Sunny"])
    println(d[readline()])
end
main()


s = readline()

if s == "Sunny"
  print("Cloudy")
elseif s == "Cloudy"
  print("Rainy")
elseif s == "Rainy"
  print("Sunny")
end


s = readline()

if s == "Sunny"
  println("Cloudy")
elseif s == "Cloudy"
  println("Rainy")
elseif s == "Rainy"
  println("Sunny")
end


function test()
  w = Dict("Sunny"=>"Cloudy", "Cloudy"=>"Rainy", "Rainy"=>"Sunny")
  s = readline()
  println(w[s])
end

test()


function test()
    w = Dict("Sunny"=>"Cloudy","Cloudy"=>"Rainy","Rainy"=>"Sunny")
    s = readline()
    print(w[s])
end


s = chomp(readline())

if s == "Sunny"
  println("Cloudy")
elseif s == "Cloudy"
  println("Rainy")
else
  println("Sunny")
end


S=strip(readline())
count=0
for i in 1:length(S)
  if i%2==0
    if S[i]=='R'
      break
    else
      count+=1
    end
  else
    if S[i]=='L'
        break
    else
      count+=1
      end
  end
end

if count==length(S)
  println("Yes")
  else
    println("No")
end

Score: 200 points

Problem Statement

Takahashi will do a tap dance. The dance is described by a string S where each character is L, R, U, or D. These characters indicate the positions on which Takahashi should step. He will follow these instructions one by one in order, starting with the first character.

S is said to be easily playable if and only if it satisfies both of the following conditions:

Every character in an odd position (1-st, 3-rd, 5-th, \ldots) is R, U, or D.

Every character in an even position (2-nd, 4-th, 6-th, \ldots) is L, U, or D.

Your task is to print Yes if S is easily playable, and No otherwise.

Constraints

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

Each character of S is L, R, U, or D.

Input

Input is given from Standard Input in the following format:

S

Output

Print Yes if S is easily playable, and No otherwise.

Sample Input 1

RUDLUDR

Sample Output 1

Yes

Every character in an odd position (1-st, 3-rd, 5-th, 7-th) is R, U, or D.

Every character in an even position (2-nd, 4-th, 6-th) is L, U, or D.

Thus, S is easily playable.

Sample Input 2

DULL

Sample Output 2

No

The 3-rd character is not R, U, nor D, so S is not easily playable.

Sample Input 3

UUUUUUUUUUUUUUU

Sample Output 3

Yes

Sample Input 4

ULURU

Sample Output 4

No

Sample Input 5

RDULULDURURLRDULRLR

Sample Output 5

Yes


parseInt(x) = parse(Int, x)

function main()
    n, k, q = map(parseInt, split(chomp(readline())))
    a = [readline() |> chomp |> parseInt for i = 1:q]
    k -= q
    points = [k for i = 1:n]
    for i = 1:q
        points[a[i]] += 1
    end
    for i = 1:n
        println(points[i] > 0 ? "Yes" : "No")
    end
end

main()

Score: 300 points

Problem Statement

Takahashi has decided to hold fastest-finger-fast quiz games. Kizahashi, who is in charge of making the scoreboard, is struggling to write the program that manages the players' scores in a game, which proceeds as follows.

A game is played by N players, numbered 1 to N. At the beginning of a game, each player has K points.

When a player correctly answers a question, each of the other N-1 players receives minus one (-1) point. There is no other factor that affects the players' scores.

At the end of a game, the players with 0 points or lower are eliminated, and the remaining players survive.

In the last game, the players gave a total of Q correct answers, the i-th of which was given by Player A_i.
For Kizahashi, write a program that determines whether each of the N players survived this game.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 10^9

1 \leq Q \leq 10^5

1 \leq A_i \leq N\ (1 \leq i \leq Q)

Input

Input is given from Standard Input in the following format:

N K Q
A_1
A_2
.
.
.
A_Q

Output

Print N lines. The i-th line should contain Yes if Player i survived the game, and No otherwise.

Sample Input 1

6 3 4
3
1
3
2

Sample Output 1

No
No
Yes
No
No
No

In the beginning, the players' scores are (3, 3, 3, 3, 3, 3).

Player 3 correctly answers a question. The players' scores are now (2, 2, 3, 2, 2, 2).

Player 1 correctly answers a question. The players' scores are now (2, 1, 2, 1, 1, 1).

Player 3 correctly answers a question. The players' scores are now (1, 0, 2, 0, 0, 0).

Player 2 correctly answers a question. The players' scores are now (0, 0, 1, -1, -1, -1).

Players 1, 2, 4, 5 and 6, who have 0 points or lower, are eliminated, and Player 3 survives this game.

Sample Input 2

6 5 4
3
1
3
2

Sample Output 2

Yes
Yes
Yes
Yes
Yes
Yes

Sample Input 3

10 13 15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9

Sample Output 3

No
No
No
No
Yes
No
No
No
Yes
No


N,K,Q=parse.(split(readline()))
A=[]
for i in 1:Q
  push!(A,parse(readline()))
end

score=fill(K,N)
for i in A
  score=score.-1
  score[i]=score[i]+1
end

for j in score
  if j>0
    println("Yes")
  else
    println("No")
  end
end


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
toBit(x,r)=Bool[(x>>(r-i))%2==1 for i in 1:r]
function fromBit(a::Array{Bool,1},r)
	x = 0
	for i in 1:r
		x += a[i]*(2^(r-i))
	end
	x
end
bsnand(a::Array{Bool,1},b::Array{Bool,1},r)=Bool[!a[i]&&b[i] for i in 1:r]
bsxor(a::Array{Bool,1},b::Array{Bool,1},r)=map((x,y)->x$y,a,b)

function GaussJordan(a::Array{Array{Bool,1},1},n,m)
	o = min(n,m)
	undone = 1
	for i in 1:o
		for j in undone:m
			f = 0
			for k in i:n
				if a[k][j]
					tmp = a[i]
					a[i] = a[k]
					a[k] = tmp
					for l in k+1:n
						if a[l][j]
							a[l] = bsxor(a[l],a[i],m)
						end
					end
					f = 1
					break
				end
			end
			if f == 1
				undone=j+1
				break
			end
		end
	end
	for i in 1:o
		j = 0
		for q in 1:m
			if a[o-i+1][q]
				j = q
				break
			end
		end
		if j>0
			for k in i+1:o
				if a[o-k+1][j]
					a[o-k+1] = bsxor(a[o-k+1],a[o-i+1],m)
				end
			end
		end
	end
	a
end

function main()
	n = readline() |> parseInt
	ar = readline() |> split |> parseMap
	o = 0
	for i in 1:n
		o = o$ar[i]
	end
	x = toBit(o,60)
	g = [toBit(ar[i],60) for i in 1:n]
	for i in 1:n
		g[i] = bsnand(x,g[i],60)
	end
	g=GaussJordan(g,n,60)
	yg = g[1]
	for i in 2:min(n,60)
		yg = bsxor(yg,g[i],60)
	end
	y = fromBit(yg,60)
	println(2*y+fromBit(x,60))
end

main()

Score: 600 points

Problem Statement

We have N non-negative integers: A_1, A_2, ..., A_N.

Consider painting at least one and at most N-1 integers among them in red, and painting the rest in blue.

Let the beauty of the painting be the \mbox{XOR} of the integers painted in red, plus the \mbox{XOR} of the integers painted in blue.

Find the maximum possible beauty of the painting.

What is \mbox{XOR}?

The bitwise \mbox{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.

2 \leq N \leq 10^5

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

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible beauty of the painting.

Sample Input 1

3
3 6 5

Sample Output 1

12

If we paint 3, 6, 5 in blue, red, blue, respectively, the beauty will be (6) + (3 \oplus 5) = 12.

There is no way to paint the integers resulting in greater beauty than 12, so the answer is 12.

Sample Input 2

4
23 36 66 65

Sample Output 2

188

Sample Input 3

20
1008288677408720767 539403903321871999 1044301017184589821 215886900497862655 504277496111605629 972104334925272829 792625803473366909 972333547668684797 467386965442856573 755861732751878143 1151846447448561405 467257771752201853 683930041385277311 432010719984459389 319104378117934975 611451291444233983 647509226592964607 251832107792119421 827811265410084479 864032478037725181

Sample Output 3

2012721721873704572

A_i and the answer may not fit into a 32-bit integer type.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function toBit(x,r)
	a = zeros(Int16,r)
	for i in 0:r-1
		if (x>>i)%2==1
			a[r-i] = 1
		end
	end
	a
end
function fromBit(a::Array{Int16,1},r)
	x = 0
	for i in 1:r
		x += a[i]*(2^(r-i))
	end
	x
end

function bsnand(a::Array{Int16,1},b::Array{Int16,1},r)
	x = zeros(Int16,r)
	for i in 1:r
		x[i] = a[i]==0&&b[i]==1?1:0
	end
	x
end
function bsxor(a::Array{Int16,1},b::Array{Int16,1},r)
	x = zeros(Int16,r)
	for i in 1:r
		x[i] = a[i]!=b[i]?1:0
	end
	x
end
function bsabs(a::Array{Int16,1},b::Array{Int16,1},r)
	x = zeros(Int16,r)
	for i in 1:r
		x[i] = (a[i]+b[i])%2
	end
	x
end

function GaussJordan(a::Array{Array{Int16,1},1},n,m)
	o = min(n,m)
	undone = 1
	for i in 1:o
		for j in undone:m
			f = 0
			for k in i:n
				if a[k][j] != 0
					tmp = a[i]
					a[i] = a[k]
					a[k] = tmp
					for l in k+1:n
						if a[l][j]>0
							a[l] = bsabs(a[l],a[i],m)
						end
					end
					f = 1
					break
				end
			end
			if f == 1
				undone=j+1
				break
			end
		end
	end
	can = m
	for i in 1:o
		j = 0
		for q in 1:can
			if a[o-i+1][q] != 0
				j = q
				can = q-1
				break
			end
		end
		if j>0
			for k in i+1:o
				if a[o-k+1][j]>0
					a[o-k+1] = bsabs(a[o-k+1],a[o-i+1],m)
				end
			end
		end
	end
	a
end

function main()
	n = readline() |> parseInt
	ar = readline() |> split |> parseMap
	o = 0
	for i in 1:n
		o = o$ar[i]
	end
	x = toBit(o,60)
	g = [toBit(ar[i],60) for i in 1:n]
	for i in 1:n
		g[i] = bsnand(x,g[i],60)
	end
	g=GaussJordan(g,n,60)
	yg = g[1]
	for i in 2:min(n,60)
		yg = bsxor(yg,g[i],60)
	end
	y = fromBit(yg,60)
	println(2*y+fromBit(x,60))
end

main()


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

function GaussJordan(a::Array{Int,2},n,m)
	for i in 1:n
		for j in 1:m
			f = 0
			for k in i:n
				if a[k,j] != 0
					tmp = a[i,:]
					a[i,:] = a[k,:].%2
					if i!=k
						a[k,:] = tmp
					end
					for l in k+1:n
						x = lcm(a[l,j],a[i,j])
						if x > 0
							a[l,:] = mod.(div(x,a[l,j])*a[l,:]-div(x,a[i,j])*a[i,:],2)
						end
					end
					f = 1
					break
				end
			end
			if f == 1
				break
			end
		end
	end
	for i in 1:n
		j = 0
		for q in 1:m
			if a[n-i+1,q] != 0
				j = q
				break
			end
		end
		if j>0
			for k in i+1:n
				x = lcm(a[n-i+1,j],a[n-k+1,j])
				if x>0
					a[n-k+1,:] = mod.(div(x,a[n-i+1,j])*a[n-i+1,:]-div(x,a[n-k+1,j])*a[n-k+1,:],2)
				end
			end
		end
	end
	a
end

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	o = 0
	for i in 1:n
		o = o$a[i]
	end
	x = zeros(Int,60)
	r = 0
	while r<60
		if (o>>r)%2==1
			x[60-r] = 1
		end
		r += 1
	end
	g = zeros(Int,n,60)
	for i in 1:n
		for j in 1:60
			if x[j] == 0
				g[i,j] = (a[i]>>(60-j))%2
			end
		end
	end
	g=GaussJordan(g,n,60)
	y = zeros(Int,60)
	for i in 1:n
		for j in 1:60
			y[j] = y[j]$g[i,j]
		end
	end
	z = 0
	for i in 0:59
		z += y[i+1]*(2^(60-i))
		z += x[i+1]*(2^(59-i))
	end
	println(z)
end

main()


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

function main()
  n = readline() |> parseInt

  println(n^3)
end

main()

Score : 100 points

Problem Statement

Takahashi is going to set a 3-character password.

How many possible passwords are there if each of its characters must be a digit between 1 and N (inclusive)?

Constraints

1 \leq N \leq 9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of possible passwords.

Sample Input 1

2

Sample Output 1

8

There are eight possible passwords: 111, 112, 121, 122, 211, 212, 221, and 222.

Sample Input 2

1

Sample Output 2

1

There is only one possible password if you can only use one kind of character.


parseInt(x) = parse(Int,x)

function main()
  N = parseInt(readline())
  println(N^3)
end

main()


parseint(x) = parse(Int, x)

function main()
    n = readline() |> chomp |> parseint
    b = map(parseint, readline() |> chomp |> split)
    ans = 0
    for i = 1:n
        if i == 1
            ans += b[1]
        elseif i == n
            ans += b[n-1]
        else
            ans += min(b[i], b[i-1])
        end
    end
    println(ans)
end

main()

Score : 300 points

Problem Statement

There is an integer sequence A of length N whose values are unknown.

Given is an integer sequence B of length N-1 which is known to satisfy the following:

B_i \geq \max(A_i, A_{i+1})

Find the maximum possible sum of the elements of A.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq B_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
B_1 B_2 ... B_{N-1}

Output

Print the maximum possible sum of the elements of A.

Sample Input 1

3
2 5

Sample Output 1

9

A can be, for example, ( 2 , 1 , 5 ), ( -1 , -2 , -3 ), or ( 2 , 2 , 5 ). Among those candidates, A = ( 2 , 2 , 5 ) has the maximum possible sum.

Sample Input 2

2
3

Sample Output 2

6

Sample Input 3

6
0 153 10 10 23

Sample Output 3

53


readline()
p=10^5
ans=0
for b=map(x->parse(Int,x),split(readline()))
	ans+=min(p,b)
	p=b
end
println(ans+p)


using Base.Collections
using Base.Order

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

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

  f(l,r)=begin
    l>=r && return 0
    ix=sortperm(a[l:r], rev=true)+l-1
    i,j=minmax(ix[1], ix[2])
    a[ix[2]]*(i-l+1)*(r-j+1)+f(l,j-1)+f(i+1,r)-f(i+1,j-1)
  end

  ans = f(1, n)

  println(ans)
end

main()

Score: 500 points

Problem Statement

Given is a permutation P of \{1, 2, \ldots, N\}.

For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest value among P_L, P_{L+1}, \ldots, P_R.

Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Constraints

2 \le N \le 10^5

1 \le P_i \le N

P_i \neq P_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N

Output

Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Sample Input 1

3
2 3 1

Sample Output 1

5

X_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

30

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

136


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

function main()
	n = readline() |> parseInt
	s = readline() |> split |> parseMap
	s = sort(s,rev=true)
	now = 1
	f = 0
	for i in 1:n-1
		for j in 1:2^(i-1)
			if s[now+j]>=s[j]
				f = 1
				break
			end
		end
		now += 2^(i-1)
	end
	println(f==0?"Yes":"No")
end

main()

Score : 600 points

Problem Statement

We have one slime.

You can set the health of this slime to any integer value of your choice.

A slime reproduces every second by spawning another slime that has strictly less health. You can freely choose the health of each new slime. The first reproduction of our slime will happen in one second.

Determine if it is possible to set the healths of our first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals a multiset S.

Here S is a multiset containing 2^N (possibly duplicated) integers: S_1,~S_2,~...,~S_{2^N}.

Constraints

All values in input are integers.

1 \leq N \leq 18

1 \leq S_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_{2^N}

Output

If it is possible to set the healths of the first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals S, print Yes; otherwise, print No.

Sample Input 1

2
4 2 3 1

Sample Output 1

Yes

We will show one way to make the multiset of the healths of the slimes that will exist in 2 seconds equal to S.

First, set the health of the first slime to 4.

By letting the first slime spawn a slime whose health is 3, the healths of the slimes that exist in 1 second can be 4,~3.

Then, by letting the first slime spawn a slime whose health is 2, and letting the second slime spawn a slime whose health is 1, the healths of the slimes that exist in 2 seconds can be 4,~3,~2,~1, which is equal to S as multisets.

Sample Input 2

2
1 2 3 1

Sample Output 2

Yes

S may contain multiple instances of the same integer.

Sample Input 3

1
1 1

Sample Output 3

No

Sample Input 4

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

Sample Output 4

No


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

function heap(a::Array{Int,1})
	h = [a[1]]
	for i in 2:length(a)
		hpush(h,a[i])
	end
	h
end

function hpush(h::Array{Int,1},x)
	push!(h,x)
	l = length(h)
	if l>1
		k = div(l,2)
		while h[k]<x&&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{Int,1})
	x = h[1]
	z = pop!(h)
	if length(h)>0
		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]<=z
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if h[2*k]<=z&&h[2*k+1]<=z
					break
				else
					t = h[2*k]<h[2*k+1]?2*k+1:2*k
					tmp = h[t]
					h[t] = z
					h[k] = tmp
					k = t
				end
			end
		end
	end
	x
end

function main()
	n = readline() |> parseInt
	s = readline() |> split |> parseMap
	s = sort(s,rev=true)
	t = zeros(Int,2^n)
	u = zeros(Int,2^n)
	t[1] = n
	b = zeros(Int,n+1)
	for i in 1:n
		b[i] = binomial(n,i-1)
	end
	l = 2
	r = 0
	now = 1
	for z in 1:n
		for i in 1:b[z]
			for j in 1:t[now]
				t[l] = t[now]-j
				u[l] = now
				l += 1
			end
			now += 1
		end
	end

	h = Int[s[1] for i in 1:t[1]]
	f = 0
	for i in 2:2^n
		a = hpop(h)
		if s[i]>=a
			f = 1
			break
		else
			for j in 1:t[i]
				hpush(h,s[i])
			end
		end
	end
	println(f==0?"Yes":"No")
end

main()


S = readline()
T = readline()
println(sum(map(x -> x[1] == x[2], zip(S, T))))

Score : 100 points

Problem Statement

You will be given a string S of length 3 representing the weather forecast for three days in the past.

The i-th character (1 \leq i \leq 3) of S represents the forecast for the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

You will also be given a string T of length 3 representing the actual weather on those three days.

The i-th character (1 \leq i \leq 3) of S represents the actual weather on the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

Print the number of days for which the forecast was correct.

Constraints

S and T are strings of length 3 each.

S and T consist of S, C, and R.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the number of days for which the forecast was correct.

Sample Input 1

CSS
CSR

Sample Output 1

2

For the first day, it was forecast to be cloudy, and it was indeed cloudy.

For the second day, it was forecast to be sunny, and it was indeed sunny.

For the third day, it was forecast to be sunny, but it was rainy.

Thus, the forecast was correct for two days in this case.

Sample Input 2

SSR
SSR

Sample Output 2

3

Sample Input 3

RRR
SSS

Sample Output 3

0


a=readline()
t=readline()
count=0
for i in 1:3
  if a[i]==t[i]
    count+=1
  end
end
println(count)


a=readline()
t=readline()
count=0
for (i,t) in zip(a,t)
  if i==t
    count+=1
  end
end
println(count)


s,t=readlins()
c=0
for i=1:3
  c+=s[i]==t[i]
end
println(c)


s = readline()
t = readline()
ans = 0
for i = 1:3
  s[i] == t[i] && (ans += 1)
end
print(ans)


function main()

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

  count = 0
  check = 1

  while true

    if check >= B
      println(count)
      break
    else
      count += 1
      check += (A-1)
    end

  end

end

main()

Score : 200 points

Problem Statement

Takahashi's house has only one socket.

Takahashi wants to extend it with some number of power strips, each with A sockets, into B or more empty sockets.

One power strip with A sockets can extend one empty socket into A empty sockets.

Find the minimum number of power strips required.

Constraints

All values in input are integers.

2 \leq A \leq 20

1 \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of power strips required.

Sample Input 1

4 10

Sample Output 1

3

3 power strips, each with 4 sockets, extend the socket into 10 empty sockets.

Sample Input 2

8 9

Sample Output 2

2

2 power strips, each with 8 sockets, extend the socket into 15 empty sockets.

Sample Input 3

8 8

Sample Output 3

1


A, B = parse.(split(readline()))
up = A - 1
want = B - 1
println(Int(div(want, up)+1))


a, b = parse.(split(readline()))
print(Int(ceil((b-1)/(a-1))))


n = parse(readline())
h = parse.(split(readline()))
s = 0
ans = 0
for i = 1:n-1
  if h[i] >= h[i+1]
    s += 1
  else
    ans = max(ans, s)
    s = 0
  end
end
print(max(ans, s))

Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right.

The height of the i-th square from the left is H_i.

You will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.

Find the maximum number of times you can move.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the maximum number of times you can move.

Sample Input 1

5
10 4 8 7 3

Sample Output 1

2

By landing on the third square from the left, you can move to the right twice.

Sample Input 2

7
4 4 5 6 6 5 5

Sample Output 2

3

By landing on the fourth square from the left, you can move to the right three times.

Sample Input 3

4
1 2 3 4

Sample Output 3

0


Score : 400 points

Problem Statement

For an integer N, we will choose a permutation \{P_1, P_2, ..., P_N\} of \{1, 2, ..., N\}.

Then, for each i=1,2,...,N, let M_i be the remainder when i is divided by P_i.

Find the maximum possible value of M_1 + M_2 + \cdots + M_N.

Constraints

N is an integer satisfying 1 \leq N \leq 10^9.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible value of M_1 + M_2 + \cdots + M_N.

Sample Input 1

2

Sample Output 1

1

When the permutation \{P_1, P_2\} = \{2, 1\} is chosen, M_1 + M_2 = 1 + 0 = 1.

Sample Input 2

13

Sample Output 2

78

Sample Input 3

1

Sample Output 3

0


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

function main()
	n = readline() |> parseInt
	a = zeros(Int,n,n-1)
	for i in 1:n
		a[i,:] = readline() |> split |> parseMap
	end
	now = ones(Int,n)
	fin = 0
	f = 0
	k = 0
	nxt = Int[i for i in 1:n]
	while fin<n
		chk = zeros(Int,n)
		tnx = Int[]
		sx = 0
		for i in nxt
			if now[i]<n&&chk[i]==0
				d = a[i,now[i]]
				if chk[d]==0&&a[d,now[d]]==i
					chk[i]=1
					chk[d]=1
					push!(tnx,i)
					push!(tnx,d)
					sx+=2
					now[i]+=1
					now[d]+=1
					if now[d]==n
						fin += 1
					end
				end
				if now[i]==n
					fin += 1
				end
			end
		end
		if sx == 0
			f = 1
			break
		else
			k += 1
			nxt = copy(tnx)
		end
	end
	println(f==1?-1:k)
end

main()

Score : 500 points

Problem Statement

N players will participate in a tennis tournament. We will call them Player 1, Player 2, \ldots, Player N.

The tournament is round-robin format, and there will be N(N-1)/2 matches in total.
Is it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.

Each player plays at most one matches in a day.

Each player i (1 \leq i \leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} in this order.

Constraints

3 \leq N \leq 1000

1 \leq A_{i, j} \leq N

A_{i, j} \neq i

A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} are all different.

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} \ldots A_{1, N-1}
A_{2, 1} A_{2, 2} \ldots A_{2, N-1}
:
A_{N, 1} A_{N, 2} \ldots A_{N, N-1}

Output

If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print -1.

Sample Input 1

3
2 3
1 3
1 2

Sample Output 1

3

All the conditions can be satisfied if the matches are scheduled for three days as follows:

Day 1: Player 1 vs Player 2

Day 2: Player 1 vs Player 3

Day 3: Player 2 vs Player 3

This is the minimum number of days required.

Sample Input 2

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

Sample Output 2

4

All the conditions can be satisfied if the matches are scheduled for four days as follows:

Day 1: Player 1 vs Player 2, Player 3 vs Player 4

Day 2: Player 1 vs Player 3

Day 3: Player 1 vs Player 4, Player 2 vs Player 3

Day 4: Player 2 vs Player 4

This is the minimum number of days required.

Sample Input 3

3
2 3
3 1
1 2

Sample Output 3

-1

Any scheduling of the matches violates some condition.


const PAIR = Pair{Int,Int}

#################### library ####################

#################### graph ####################

const Graph = Dict{Int,Vector{Int}}
const GraphPAIR = Dict{PAIR,Vector{PAIR}}

# graph: { node: [node] }
function is_cyclic(graph::GraphPAIR)
	for pair in graph
		x0 = pair.first
		v = [x0]	# stack
		s::Set{Pair} = Set{Pair}()
		while !isempty(v)
			x = pop!(v)
			for y in graph[x]
				if !in(y, s)
					push!(s, y)
					push!(v, y)
				end
			end
		end

		if in(x0, s)
			return true
		end
	end

	return false
end

#################### naive ####################

#################### process ####################

function read_input()
	N = parse(Int, readline())
	table = [ [ parse(Int, s) for s in split(readline()) ] for _ in 1:N ]
	return table
end

function make_graph(table)::GraphPAIR
	function f(v)
		dic::Dict{Int,Int} = Dict()
		k = 0
		for e in v
			dic[e] = k
		end
		return dic
	end

	N = length(table)
	dics = [ f(v) for v in table ]
	graph::GraphPAIR = Dict()
	for i in 1:N-1
		for j in i+1:N
			graph[PAIR(i,j)] = []
		end
	end
	for pair in graph
		k = pair.first.first
		l = pair.first.second
		i1 = dics[k][l]
		if i1 < N-1
			j1 = table[k][i1+1]
			push!(graph[Pair(k, l)], minimum([Pair(k, j1), Pair(j1, k)]))
		end
		i2 = dics[l][k]
		if i2 < N-1
			j2 = table[l][i2+1]
			push!(graph[Pair(k, l)], minimum([Pair(l, j2), Pair(j2, l)]))
		end
	end
	return graph
end

function f()
	table = read_input()
	N = length(table)
	memo::Dict{PAIR,Int} = Dict()
	graph::GraphPAIR = make_graph(table)
	if is_cyclic(graph)
		return -1
	end

	unvisited = Set(Pair(n, m) for n in 1:N-1 for m in n+1:N)
	for pair in graph
		if length(pair.second) == 0
			memo[pair.first] = 1
			pop!(unvisited, pair.first)
		end
	end

	for i in 1:N-1
		for j in i+1:N
			v = Pair(i, j)
			if !in(v, unvisited)
				continue
			end
			stack = [v]
			while !isempty(stack)
				v = pop!(stack)
				unvs = [ v1 for v1 in graph[v] if in(v1, unvisited) ]
				if isempty(unvs)
					dist = maximum([memo[v1]+1 for v1 in graph[v]])
					memo[v] = dist
					pop!(unvisited, v)
				else
					push!(stack, v)
					for v1 in unvs
						push!(stack, v1)
					end
				end
			end
		end
	end

	return maximum(pair.second for pair in memo)
end

#################### main ####################

println(f())


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

function main()
	n = readline() |> parseInt
	a = [Array{Int}(n-1) for i in 1:n]
	for i in 1:n
		a[i] = readline() |> split |> parseMap
	end
	f = 0
	k = 0
	l = (n-1)*ones(Int,n)
	rem = n
	w = [Int[] for i in 1:n]
	for i in 1:n
		push!(w[a[i][1]],i)
	end
	while rem>0
		t = 0
		c = Int[]
		for i in 1:n
			if !isempty(a[i])
				for gg in 1:length(w[i])
					j = w[i][gg]
					if !isempty(w[j])
						if w[j][1]==i
							t += 1
							l[i] -= 1
							l[j] -= 1
							push!(c,i)
							push!(c,j)
							shift!(w[i])
							shift!(w[j])
							break
						end
					end
				end
			end
		end
		if t>0
			k += 1
			for i in 1:length(c)
				shift!(a[c[i]])
				if isempty(a[c[i]])
					rem-=1
				else
					push!(w[a[c[i]][1]],c[i])
				end
			end
		else
			f = 1
			break
		end
	end
	println(f==0?k:-1)
end

main()


Score: 600 points

Problem Statement

E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.

He has N engines, which can be used as follows:

When E869120 uses the i-th engine, his X- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).

E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.

He wants to go as far as possible from the origin.
Let (X, Y) be his final coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance from the origin.

Constraints

1 \leq N \leq 100

-1 \ 000 \ 000 \leq x_i \leq 1 \ 000 \ 000

-1 \ 000 \ 000 \leq y_i \leq 1 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
: :
x_N y_N

Output

Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the true answer is at most 10^{-10}.

Sample Input 1

3
0 10
5 -5
-5 -5

Sample Output 1

10.000000000000000000000000000000000000000000000000

The final distance from the origin can be 10 if we use the engines in one of the following three ways:

Use Engine 1 to move to (0, 10).

Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).

Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).

The distance cannot be greater than 10, so the maximum possible distance is 10.

Sample Input 2

5
1 1
1 0
0 1
-1 0
0 -1

Sample Output 2

2.828427124746190097603377448419396157139343750753

The maximum possible final distance is 2 \sqrt{2} = 2.82842....
One of the ways to achieve it is:

Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).

Sample Input 3

5
1 1
2 2
3 3
4 4
5 5

Sample Output 3

21.213203435596425732025330863145471178545078130654

If we use all the engines in the order 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5, we will end up at (15, 15), with the distance 15 \sqrt{2} = 21.2132... from the origin.

Sample Input 4

3
0 0
0 1
1 0

Sample Output 4

1.414213562373095048801688724209698078569671875376

There can be useless engines with (x_i, y_i) = (0, 0).

Sample Input 5

1
90447 91000

Sample Output 5

128303.000000000000000000000000000000000000000000000000

Note that there can be only one engine.

Sample Input 6

2
96000 -72000
-72000 54000

Sample Output 6

120000.000000000000000000000000000000000000000000000000

There can be only two engines, too.

Sample Input 7

10
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20

Sample Output 7

148.660687473185055226120082139313966514489855137208


const B=1000
function f(X,Y,x,y)
	z=x[:]
	w=y[:]
	for i=1:B
		tx=X+x[i]
		ty=Y+y[i]
		theta=atan2(ty,tx)
		if ty<0
			theta+=2*pi
		end
		id=floor(Int,theta*B/(2*pi))+1
		if hypot(z[id],w[id])<hypot(tx,ty)
			z[id]=tx
			w[id]=ty
		end
	end
	z,w
end
function main()
	x=zeros(Int,B)
	y=zeros(Int,B)
	N=parse(Int,readline())
	for i=1:N
		X,Y=map(x->parse(Int,x),split(readline()))
		if X!=0||Y!=0
			x,y=f(X,Y,x,y)
		end
	end
	ans=0
	for i=1:B
		ans=max(ans,hypot(x[i],y[i]))
	end
	println(ans)
end
main()


const MOD = 1000000007

function main()
    n, k = parse.(Int, split(readline()))
    a = parse.(Int, split(readline()))

    p = 0
    for i in 1:length(a)-1
        @views p += count(x->x<a[i], a[i+1:end])
    end

    q = 0
    for i in 1:length(a)
        q += count(x->x<a[i], a)
    end

    p %= MOD
    q %= MOD
    k %= MOD
    ans = mod(p*k, MOD)
    ans += mod(q*mod(k*(k-1)/2, MOD), MOD)
    ans = mod(ans, MOD)

    println(Int(ans))
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.


const MOD = 1000000007

function main()
    n, k = parse.(Int, split(readline()))
    a = parse.(Int, split(readline()))

    p = 0
    for i in 1:length(a)-1
        @views p += count(x->x<a[i], a[i+1:end])
    end

    q = 0
    for i in 1:length(a)-1
        q += count(x->x<a[i], a)
    end

    ans = 0
    ans = mod(ans+p*k, MOD)
    ans = mod(ans + mod(k*(k-1)/2*q, MOD), MOD)

    println(Int(ans))
end

main()


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

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

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.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n=parseInt(readline())
    v=sort(parseMap(split(readline())))
    ans=v[1]
    for i in v
        ans=(ans+i)/2
    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


N = parse(readline())
b = parse.(split(readline()))
sort!(b)
a = 0.0
a += b[1]
for i in 2:N
  a += b[i]
  a /= 2
end
println( a )


#################### library ####################

#################### naive ####################

#################### process ####################

function read_input()
	s = chomp(readline())
	t = chomp(readline())
	return (s, t)
end

function make_next_pos(v, L)
	w = zeros(Int, L)
	p = 1
	for q in v
		for k in p:q-1
			w[k] = q
		end
		p = q
	end
	q = v[1]
	for k in p:L
		w[k] = q
	end
	return w
end

function f()
	s, t = read_input()
	for c in t
		if search(s, c) == 0
			return -1
		end
	end

	L = length(s)
	dic::Dict{Char,Array{Int}} = Dict()
	for k in 1:length(s)
		if haskey(dic, s[k])
			push!(dic[s[k]], k)
		else
			dic[s[k]] = [k]
		end
	end

	table::Dict{Char,Array{Int}} = Dict()
	for (c, v) in dic
		table[c] = make_next_pos(v, L)
	end

	p = L
	i = -1
	for c in t
		q = table[c][p]
		i += (q-p-1+L)%L+1
		p = q
	end
	return i+1
end

#################### main ####################

println(f())

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.


if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
function main()
  s=chomp(readline())
  ans=1
  pre=s[1]
  lookat=2
  while lookat<=length(s)
    ans+=1
    if lookat==length(s)-1 && s[end-1]==s[end]
      lookat+=2
    elseif pre==s[lookat]
      pre=s[lookat:min(lookat+1,length(s))]
      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)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	s = readline() |> chomp
	n = length(s)
	dp = Array{Int}(n)
	dp[1] = 1
	dp[2] = s[1]==s[2]?1:2
	if s[1]==s[2]==s[3]
		dp[3]=2
	elseif s[2]==s[3]
		dp[3]=2
	else
		dp[3] = dp[2]+1
	end
	for i in 4:n
		if s[i]==s[i-1]
			dp[i] = dp[i-3]+2
		else
			dp[i] = dp[i-1]+1
		end
	end
	println(dp[n])
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.


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

function main()
    a, b = readlist()
    max(a + b, a - b, a * b) |> println
end

main()


parseInt(x) = parse(Int,x)

function main()
  (A,B) = map(parseInt,split(readline()))
  C = [A+B, A-B, A*B]
  println(maximum(C))
end

main()


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

function main()
	k,x = readline() |> split |> parseMap
	for i in x-k+1:x+k-2
		print(i, " ")
	end
	println(k+x-1)
end

main()

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


ny=split(readline())
k=parse(Int,ny[1])
x=parse(Int,ny[2])

a=x+k-1
b=x-k+1

A=[]
for i=b:a
    A= push!(A ,i)
end

for i in A
    print("$i ")
end


#=
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
		@inbounds a = A[kk]
		if  k == a
			count += 1
		end
	end
	res += (count * (count - 1 )) / 2
end
println(res)

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.


#=
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

A= []
for i in 1:N
	x = readStr()
	push!(A,x)
end

B = Set(A)
println(length(A)-length(B))


const N=parse(Int,readline())
const A=Int[]
for s=readlines()
	C=zeros(Int,26)
	for c=chomp(s)
		C[Int(c)-95]+=1
	end
	now=0
	for i=1:26
		if C[i]>=1
			for j=1:C[i]
				now=now*26+i-1
			end
		end
	end
	push!(A,now)
end
ans=0
sort!(A)
L=1
while L<=N
	R=L
	while R<=N&&A[R]==A[L]
		R+=1
	end
	ans+=div((R-L)*(R-L-1),2)
	L=R
end
println(ans)


const p=parse(readline());const B=eye(Int,p);for i=1:p;for j=(B[p,i]=p-1):-1:2;B[j,i]=B[j+1,i]*(i-1)%p;end;end;println.(B*parse.(split(readline())).%p)

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


a, b, c = parse.([Int], split(readline()))
println(c - min(a - b, c))

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


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

function main()
	n,k = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	s = sum(a)
	t = Int[]
	q = 1
	r = floor(Int,sqrt(s))
	while q<=r
		if s%q==0
			push!(t,q)
			push!(t,div(s,q))
		end
		q += 1
	end
	if r*r==s
		push!(t,r)
	end
	m = 0
	for i in 1:length(t)
		b = zeros(Int,n)
		x = 0
		for j in 1:n
			b[j] = a[j]%t[i]
			x += b[j]
		end
		b = sort(b)
		h = div(x,t[i])
		y = sum(b[1:n-h])
		if y<=k
			m = max(m,t[i])
		end
	end
	println(m)
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 N=parse(Int,readline())
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(readline()))
		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.


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


ab=readline()
ab1=split(ab)
a=parse(Int,ab1[1])
b=parse(Int,ab1[2])
if (a+b) % 2==0
  c=round(Int,(a+b)/2)
  print(c)
  else
    print("IMPOSSIBLE")
  end


n=parse(readline())
a=split(readline())
b=sort(a)
print(sum([a[i]!=b[i] for i in 1:n]) ∈ 0:2:2 ? "YES" : "NO")

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.


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

function main()
	mod = 1000000007
	s = readline() |> chomp
	g = zeros(Int,13,13)
	for i in 0:12
		for j in 0:12
			g[i+1,j+1] = (10*i+j)%13
		end
	end
	dp = zeros(Int,length(s),13)
	if s[1] == '?'
		for i in 1:10
			dp[1,i] = 1
		end
	else
		dp[1,parseInt(s[1:1])+1] = 1
	end
	for i in 2:length(s)
		if s[i] == '?'
			for j in 1:13
				for k in 1:10
					x = g[j,k]+1
					dp[i,x] = (dp[i,x]+dp[i-1,j])%mod
				end
			end
		else
			for j in 1:13
				x = g[j,parseInt(s[i:i])+1]+1
				dp[i,x] = (dp[i,x]+dp[i-1,j])%mod
			end
		end
	end
	println(dp[length(s),6])
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


#include <bits/stdc++.h>
using namespace std;

int main(){
    int r;
    cin >> r;
    cout << 3*r*r << endl;

    return 0;
}


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

function main()
	r = readline() |> parseInt
	println(3*r*r)
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


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

function main()
	n,d = readline() |> split |> parseMap
	println(ceil(Int,n/(2*d+1)))
end

main()


function main()

  N = parse(Int,readline())
  A = zeros(Int,N)

  for i in 1:N
    A[i] = parse(Int,readline())
  end

  for i in 1:N
    println(max(A[1:i-1]...,A[i+1:end]...))
  end

end

main()

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 = readline()
n = parse(Int, n)
function get_lines(n)
	an = []
	for i in 1:n
		elem = readline()
		elem = parse(Int, elem)
		push!(an, elem)
	end

	for i in 1:n
		an_tmp = copy(an)
		deleteat!(an_tmp, i)
		res = findmax(an_tmp)
		print(res[1])
		print("\n")
	end
end


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

function main()
	n = readline() |> parseInt
	a = Array{Int}(n)
	mo = 0
	mt = 0
	for i in 1:n
		a[i] = readline() |> parseInt
		if a[i] > mo
			mt = mo
			mo = a[i]
		elseif a[i]>mt
			mt = a[i]
		end
	end
	for i in 1:n
		println(a[i]==mo?mo:mt)
	end
end

main()


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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	f = 0
	b = zeros(Int,n)
	for i in 0:n-1
		k = div(n,n-i)
		t = 0
		for j in n-i:k:n
			t += a[j]
		end
		b[n-i] = t%2
		if t%2 != a[n-i]
			f = 1
			break
		end
	end
	if f == 1
		println(-1)
	else
		c = Int[]
		for i in 1:n
			if b[i] == 1
				push!(c,i)
			end
		end
		l = length(c)
		println(l)
		if l >0
			for i in 1:l-1
				print(c[i], " ")
			end
			println(c[l])
		end
	end
end

main()

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(readline())
b=searchsortedlast
c=-ones(n)
map(x->c[b(c,x-1)]=x,map(x->parse(Int,x),readlines()))
show(n-b(c,-1))

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.


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

function main()
	n = readline() |> parseInt
	a = Array{Int}(n)
	for i in 1:n
		a[i] = readline() |> parseInt
	end
	k = 1
	ma = a[n]
	stack=Int[a[n]]
	for i in 1:n-1
		x = bsearchg(stack,a[n-i])
		if x<length(stack)
			stack[x+1] = a[n-i]
		else
			push!(stack,a[n-i])
		end
	end
	println(length(stack))
end

main()


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


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.


const lines=readlines()
input()=shift!(lines)
int(x)=parse(Int,x)
intLine()=map(int,split(input()))
function main()
    n=int(input())
    b=intLine()
    a=b[1]
    for i=2:n
        a$=b[i]
    end
    println(a>0?"No":"Yes")
end
main()


N,M=parse.(split(readline()))
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


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,d=parse.(split(readline()))
  m=[[parse(Int,j) for j=split(readline())]for i=1:n]
  ans=0
  for i=1:n-1
    for j=i+1:n
      ans+=isinteger((sum([i^2 for i=m[i]-m[j]]))^(1/2))
    end
  end
  print(ans)
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


parseInt()=map(x->parse(Int,x),split(readline()))
const N,Q=parseInt()
const G=[Tuple{Int,Int,Int}[] for _=1:N]
const depth=zeros(Int,N)
const parent=[zeros(Int,N) for _=1:17]
const tour=Pair{Int,Int}[]
const L=zeros(Int,N)
const edge=[Int[] for _=1:N-1]
const edgesum=[Int[0] for _=1:N-1]
const edgecnt=[Int[0] for _=1:N-1]
const cumsum=Int[0]
function bsearch(A,x)
	L=0
	R=length(A)+1
	while R-L>1
		M=div(L+R,2)
		if A[M]>=x
			R=M
		else
			L=M
		end
	end
	R
end
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
function lca(u,v)
	if depth[u]>depth[v]
		u,v=v,u
	end
	for k=1:17
		if ((depth[v]-depth[u])>>(k-1))%2==1
			v=parent[k][v]
		end
	end
	if u==v
		return u
	end
	for k=17:-1:1
		if parent[k][u]!=parent[k][v]
			u=parent[k][u]
			v=parent[k][v]
		end
	end
	parent[1][u]
end
function get(l,r,c)
	lid=bsearch(edge[c],l)
	rid=bsearch(edge[c],r)
	edgecnt[c][rid]-edgecnt[c][lid],edgesum[c][rid]-edgesum[c][lid]
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 main()
	for _=2:N
		a,b,c,d=parseInt()
		push!(G[a],(b,c,d))
		push!(G[b],(a,c,d))
	end
	dfs(1,0,0)
	now=0
	for i=1:length(tour)
		c,d=tour[i]
		push!(edge[c],i)
		push!(edgesum[c],edgesum[c][end]+d)
		push!(edgecnt[c],edgecnt[c][end]+(d>0?1:-1))
		push!(cumsum,now+=d)
	end
	for k=2:17
		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
	for _=1:Q
		x,y,u,v=parseInt()
		pa=lca(u,v)
		println(calc(pa,u,x,y)+calc(pa,v,x,y))
	end
end
main()

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.


S = collect(readline())
sort!(S)
if S[1] == S[2] && S[3] == S[4]
  println("Yes")
else
  println("No")
end

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