function main()

  (M1,D1) = map(x -> parse(Int,x), split(readline()))
  (M2,D2) = map(x -> parse(Int,x), split(readline()))

  if M1 == M2
    println(0)
  else
    println(1)
  end

end

main()

Score: 100 points

Problem Statement

In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.

Integers M_1, D_1, M_2, and D_2 will be given as input.

It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.

Determine whether the date 2019-M_1-D_1 is the last day of a month.

Constraints

Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.

The date 2019-M_2-D_2 follows 2019-M_1-D_1.

Input

Input is given from Standard Input in the following format:

M_1 D_1
M_2 D_2

Output

If the date 2019-M_1-D_1 is the last day of a month, print 1; otherwise, print 0.

Sample Input 1

11 16
11 17

Sample Output 1

0

November 16 is not the last day of a month.

Sample Input 2

11 30
12 1

Sample Output 2

1

November 30 is the last day of November.


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

function main()
	n = readline() |> parseInt
	f = 0
	for i in 1:n
		if n<=i*1.08<n+1
			println(i)
			f = 1
			break
		end
	end
	if f == 0
		println(":(")
	end
end

main()

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

Score: 200 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 50000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

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

If no value could be X, print :(.

Sample Input 1

432

Sample Output 1

400

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

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

Sample Input 2

1079

Sample Output 2

:(

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

Sample Input 3

1001

Sample Output 3

927

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

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()
N = parse(readline())
x = div(N,1.08)
x = convert(Int,x)
ans = false
for i in x:x+30
    money = 1.08*i
    if money == N
        println(i)
        ans = true
        break
    end
end
if ans != true
    println(":(")
end
end
main()


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

function main()
    x=parseInt(readline())
    dp=[0 for i in 1:100010]
    for i in 1:6
        dp[100+i]=1
    end
    for i in 101:x+1
        try
            for j in 0:5
                if dp[i-(100+j)+1]>0
                    dp[i]=dp[i-(100+j)+1]+1
                end
            end
        catch
            continue
        end
    end
    println(ifelse(dp[x]>0,1,0))
end
main()

Score: 300 points

Problem Statement

AtCoder Mart sells 1000000 of each of the six items below:

Riceballs, priced at 100 yen (the currency of Japan) each

Sandwiches, priced at 101 yen each

Cookies, priced at 102 yen each

Cakes, priced at 103 yen each

Candies, priced at 104 yen each

Computers, priced at 105 yen each

Takahashi wants to buy some of them that cost exactly X yen in total.
Determine whether this is possible.

(Ignore consumption tax.)

Constraints

1 \leq X \leq 100000

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

If it is possible to buy some set of items that cost exactly X yen in total, print 1; otherwise, print 0.

Sample Input 1

615

Sample Output 1

1

For example, we can buy one of each kind of item, which will cost 100+101+102+103+104+105=615 yen in total.

Sample Input 2

217

Sample Output 2

0

No set of items costs 217 yen in total.


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())
    s=chomp(readline())
    ans=0
    for i in 0:999
        t=lpad(i,3,"0")
        tmp=1
        for j in 1:n
            if s[j]==t[tmp]
                tmp+=1
            end
            if tmp>3
                ans+=1
                break
            end
        end
    end
    println(ans)
end
main()

Score: 400 points

Problem Statement

AtCoder Inc. has decided to lock the door of its office with a 3-digit PIN code.

The company has an N-digit lucky number, S. Takahashi, the president, will erase N-3 digits from S and concatenate the remaining 3 digits without changing the order to set the PIN code.

How many different PIN codes can he set this way?

Both the lucky number and the PIN code may begin with a 0.

Constraints

4 \leq N \leq 30000

S is a string of length N consisting of digits.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of different PIN codes Takahashi can set.

Sample Input 1

4
0224

Sample Output 1

3

Takahashi has the following options:

Erase the first digit of S and set 224.

Erase the second digit of S and set 024.

Erase the third digit of S and set 024.

Erase the fourth digit of S and set 022.

Thus, he can set three different PIN codes: 022, 024, and 224.

Sample Input 2

6
123123

Sample Output 2

17

Sample Input 3

19
3141592653589793238

Sample Output 3

329


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

function main()
	mod = 10^9+7
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	if a[1] != 0
		println(0)
	else
	dp = zeros(Int,n)
	dp[1] = 1
	r = 1
	g = 0
	b = 0
	for i in 2:n
		k = 0
		f = 0
		if a[i]==r
			r += 1
			f = 1
			k += 1
		end
		if a[i]==g
			if f == 0
				g += 1
				f += 1
			end
			k += 1
		end
		if a[i]==b
			if f == 0
				b += 1
				f += 1
			end
			k += 1
		end
		dp[i] = (dp[i-1]*k)%mod
	end
	dp[n] = (dp[n]*3)%mod
	println(dp[n])
end
end

main()

Score: 500 points

Problem Statement

N people are standing in a queue, numbered 1, 2, 3, ..., N from front to back. Each person wears a hat, which is red, blue, or green.

The person numbered i says:

"In front of me, exactly A_i people are wearing hats with the same color as mine."

Assuming that all these statements are correct, find the number of possible combinations of colors of the N people's hats.

Since the count can be enormous, compute it modulo 1000000007.

Constraints

1 \leq N \leq 100000

0 \leq A_i \leq N-1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_N

Output

Print the number of possible combinations of colors of the N people's hats, modulo 1000000007.

Sample Input 1

6
0 1 2 3 4 5

Sample Output 1

3

We have three possible combinations, as follows:

Red, Red, Red, Red, Red, Red

Blue, Blue, Blue, Blue, Blue, Blue

Green, Green, Green, Green, Green, Green

Sample Input 2

3
0 0 0

Sample Output 2

6

Sample Input 3

54
0 0 1 0 1 2 1 2 3 2 3 3 4 4 5 4 6 5 7 8 5 6 6 7 7 8 8 9 9 10 10 11 9 12 10 13 14 11 11 12 12 13 13 14 14 15 15 15 16 16 16 17 17 17

Sample Output 3

115295190


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

function main()
	ta,tb = readline() |> split |> parseMap
	aa,ab = readline() |> split |> parseMap
	ba,bb = readline() |> split |> parseMap
	if aa*ta+ab*tb==ba*ta+bb*tb
		println("infinity")
	elseif (aa<ba&&aa*ta+ab*tb<ba*ta+bb*tb)||(aa>ba&&aa*ta+ab*tb>ba*ta+bb*tb)
		println(0)
	elseif aa<ba
		println(1+2*div(ta*(ba-aa),ta*(aa-ba)+tb*(ab-bb)))
	elseif ba<aa
		println(1+2*div(ta*(aa-ba),ta*(ba-aa)+tb*(bb-ab)))
	end
end

main()

Score: 600 points

Problem Statement

Takahashi and Aoki are training for long-distance races in an infinitely long straight course running from west to east.

They start simultaneously at the same point and moves as follows towards the east:

Takahashi runs A_1 meters per minute for the first T_1 minutes, then runs at A_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.

Aoki runs B_1 meters per minute for the first T_1 minutes, then runs at B_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.

How many times will Takahashi and Aoki meet each other, that is, come to the same point? We do not count the start of the run. If they meet infinitely many times, report that fact.

Constraints

1 \leq T_i \leq 100000

1 \leq A_i \leq 10^{10}

1 \leq B_i \leq 10^{10}

A_1 \neq B_1

A_2 \neq B_2

All values in input are integers.

Input

Input is given from Standard Input in the following format:

T_1 T_2
A_1 A_2
B_1 B_2

Output

Print the number of times Takahashi and Aoki will meet each other.

If they meet infinitely many times, print infinity instead.

Sample Input 1

1 2
10 10
12 4

Sample Output 1

1

They will meet just once, \frac{4}{3} minutes after they start, at \frac{40}{3} meters from where they start.

Sample Input 2

100 1
101 101
102 1

Sample Output 2

infinity

They will meet 101, 202, 303, 404, 505, 606, ... minutes after they start, that is, they will meet infinitely many times.

Sample Input 3

12000 15700
3390000000 3810000000
5550000000 2130000000

Sample Output 3

113

The values in input may not fit into a 32-bit integer type.


function f()
    a = readline()
    if a == "SUN"
        print(7)
    elseif a == "MON"
        print(6)
    elseif a == "TUE"
        print(5)
    elseif a == "WED"
        print(4)
    elseif a == "THU"
        print(3)
    elseif a == "FRI"
        print(2)
    else
        print(1)
    end
end

f()

Score : 100 points

Problem Statement

Given is a string S representing the day of the week today.

S is SUN, MON, TUE, WED, THU, FRI, or SAT, for Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday, respectively.

After how many days is the next Sunday (tomorrow or later)?

Constraints

S is SUN, MON, TUE, WED, THU, FRI, or SAT.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of days before the next Sunday.

Sample Input 1

SAT

Sample Output 1

1

It is Saturday today, and tomorrow will be Sunday.

Sample Input 2

SUN

Sample Output 2

7

It is Sunday today, and seven days later, it will be Sunday again.


day = readline()
if day == "SUN"
    println(7)
elseif day == "MON"
    println(6)
elseif day == "TUE"
    println(5)
elseif day == "WED"
    println(4)
elseif day == "THU"
    println(3)
elseif day == "FRI"
    println(2)
else
    println(1)
end


N = strip(readline())
n = parse(Int, N)
S = collect(strip(readline()))

for s in S
  ans = s + n
  if Int(ans) > 90
    ans -= 26
  end
  print(ans)
end

Score : 200 points

Problem Statement

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

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

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

Constraints

0 \leq N \leq 26

1 \leq |S| \leq 10^4

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

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

Sample Input 1

2
ABCXYZ

Sample Output 1

CDEZAB

Note that A follows Z.

Sample Input 2

0
ABCXYZ

Sample Output 2

ABCXYZ

Sample Input 3

13
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample Output 3

NOPQRSTUVWXYZABCDEFGHIJKLM


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

function main()
    n=parseInt(readline())
    a=parseMap(split(readline()))
    l=[0 for i in 1:n+1]
    r=[0 for i in 1:n+1]
    for i in 2:n+1
        l[i]=l[i-1]+a[i-1]
    end
    for i in 2:n+1
        r[i]=r[i-1]+a[n-i+2]
    end
    ans=10^10
    for i in 2:n
        ans=min(ans,abs(l[i]-r[n-i+2]))
    end
    println(ans)
end
main()

Score: 200 points

Problem Statement

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

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

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

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

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

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 2020202020

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_N

Output

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

Sample Input 1

3
2 4 3

Sample Output 1

3

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

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

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

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

Sample Input 2

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

Sample Output 2

0


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

function main()
	h,w,k = readline() |> split |> parseMap
	a = String["" for i in 1:h]
	fs = 0
	ls = 0
	v = [Int[0] for i in 1:h]
	for i in 1:h
		a[i] = readline() |> chomp
		for j in 1:w
			if a[i][j]=='#'
				if fs == 0
					fs = i
				end
				ls = i
				push!(v[i],j)
			end
		end
	end
	x = 1
	o = zeros(Int,h,w)
	for i in 1:h
		if length(v[i])>1
			for j in 2:length(v[i])-1
				for k in v[i][j-1]+1:v[i][j]
					o[i,k]=x
				end
				x += 1
			end
			for k in v[i][length(v[i])-1]+1:w
				o[i,k]=x
			end
			x += 1
		end
	end
	for i in 1:fs-1
		for j in 1:w
			o[i,j] = o[fs,j]
		end
	end
	for i in ls+1:h
		for j in 1:w
			o[i,j] = o[ls,j]
		end
	end
	for i in 1:h
		for j in 1:w-1
			print(o[i,j]," ")
		end
		println(o[i,w])
	end
end
main()

Score: 500 points

Problem Statement

N programmers are going to participate in the preliminary stage of DDCC 20XX. Due to the size of the venue, however, at most 9 contestants can participate in the finals.

The preliminary stage consists of several rounds, which will take place as follows:

All the N contestants will participate in the first round.

When X contestants participate in some round, the number of contestants advancing to the next round will be decided as follows:

The organizer will choose two consecutive digits in the decimal notation of X, and replace them with the sum of these digits. The number resulted will be the number of contestants advancing to the next round.

For example, when X = 2378, the number of contestants advancing to the next round will be 578 (if 2 and 3 are chosen), 2108 (if 3 and 7 are chosen), or 2315 (if 7 and 8 are chosen).

When X = 100, the number of contestants advancing to the next round will be 10, no matter which two digits are chosen.

The preliminary stage ends when 9 or fewer contestants remain.

Ringo, the chief organizer, wants to hold as many rounds as possible.
Find the maximum possible number of rounds in the preliminary stage.

Since the number of contestants, N, can be enormous, it is given to you as two integer sequences d_1, \ldots, d_M and c_1, \ldots, c_M, which means the following: the decimal notation of N consists of c_1 + c_2 + \ldots + c_M digits, whose first c_1 digits are all d_1, the following c_2 digits are all d_2, \ldots, and the last c_M digits are all d_M.

Constraints

1 \leq M \leq 200000

0 \leq d_i \leq 9

d_1 \neq 0

d_i \neq d_{i+1}

c_i \geq 1

2 \leq c_1 + \ldots + c_M \leq 10^{15}

Input

Input is given from Standard Input in the following format:

M
d_1 c_1
d_2 c_2
:
d_M c_M

Output

Print the maximum possible number of rounds in the preliminary stage.

Sample Input 1

2
2 2
9 1

Sample Output 1

3

In this case, N = 229 contestants will participate in the first round. One possible progression of the preliminary stage is as follows:

229 contestants participate in Round 1, 49 contestants participate in Round 2, 13 contestants participate in Round 3, and 4 contestants advance to the finals.

Here, three rounds take place in the preliminary stage, which is the maximum possible number.

Sample Input 2

3
1 1
0 8
7 1

Sample Output 2

9

In this case, 1000000007 will participate in the first round.


n=parse(Int,readline())
s=chomp(readline())
print(s==s[1:cld(n,2)]^2 ? "Yes" : "No")

Score : 200 points

Problem Statement

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

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

Constraints

1 \leq N \leq 100

S consists of lowercase English letters.

|S| = N

Input

Input is given from Standard Input in the following format:

N
S

Output

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

Sample Input 1

6
abcabc

Sample Output 1

Yes

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

Sample Input 2

6
abcadc

Sample Output 2

No

Sample Input 3

1
z

Sample Output 3

No


function main()

  N = parse(Int,readline())
  x = zeros(N)
  y = zeros(N)

  for i in 1:N
    (x[i],y[i]) = map(x -> parse(Int,x), split(readline()))
  end

  route_sum = 0

  for i in 1:N, j in 1:N

    route_sum += sqrt((x[i]-x[j])^2+(y[i]-y[j])^2)

  end

  println(route_sum / N)

end

main()

Score : 300 points

Problem Statement

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

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

Constraints

2 \leq N \leq 8

-1000 \leq x_i \leq 1000

-1000 \leq y_i \leq 1000

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

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

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

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

Sample Input 1

3
0 0
1 0
0 1

Sample Output 1

2.2761423749

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

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

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

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

Sample Input 2

2
-879 981
-866 890

Sample Output 2

91.9238815543

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

Sample Input 3

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

Sample Output 3

7641.9817824387


#=
D:
- Julia version:
- Author: abap
- Date: 2019-11-16
=#
x,y = parse.(split(readline()))

X,Y = 0,0
for i in 1:x
    X += 2
    Y += 1
    print(X,Y)
    if (x - X)/(y - Y) == 1/2
        global yoko = x
        global tate = x - X
        break
    end
end
count = factorial(tate + yoko)/(factorial(tate)+factorial(yoko))
println(count % (10^9 +7))

Score : 400 points

Problem Statement

There is a knight - the chess piece - at the origin (0, 0) of a two-dimensional grid.

When the knight is at the square (i, j), it can be moved to either (i+1,j+2) or (i+2, j+1).

In how many ways can the knight reach the square (X, Y)?

Find the number of ways modulo 10^9 + 7.

Constraints

1 \leq X \leq 10^6

1 \leq Y \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the number of ways for the knight to reach (X, Y) from (0, 0), modulo 10^9 + 7.

Sample Input 1

3 3

Sample Output 1

2

There are two ways: (0,0) \to (1,2) \to (3,3) and (0,0) \to (2,1) \to (3,3).

Sample Input 2

2 2

Sample Output 2

0

The knight cannot reach (2,2).

Sample Input 3

999999 999999

Sample Output 3

151840682

Print the number of ways modulo 10^9 + 7.


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

function main()
    n,t = readline() |> split |> parseMap
    a = Tuple{Int,Int}[(0,0) for i in 1:n]
    for i in 1:n
        p,q = readline() |> split |> parseMap
        a[i] = (p,q)
    end
    a = sort(a,by=x->x[1])
    dp = -ones(Int,n+1,t+1)
    dp[1,1] = 0
    for i in 1:n
        for j in 1:t
            if dp[i,j]>-1
                dp[i+1,j] = max(dp[i+1,j],dp[i,j])
                dp[i+1,min(j+a[i][1],t+1)] = max(dp[i+1,min(j+a[i][1],t+1)], dp[i,j]+a[i][2])
            end
        end
    end
    println(maximum(dp))
end
main()

Score : 500 points

Problem Statement

Takahashi is at an all-you-can-eat restaurant.

The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th dish, whose deliciousness is B_i.

The restaurant has the following rules:

You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.

You cannot order the same kind of dish more than once.

Until you finish eating the dish already served, you cannot order a new dish.

After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.

Let Takahashi's happiness be the sum of the deliciousness of the dishes he eats in this restaurant.

What is the maximum possible happiness achieved by making optimal choices?

Constraints

2 \leq N \leq 3000

1 \leq T \leq 3000

1 \leq A_i \leq 3000

1 \leq B_i \leq 3000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N T
A_1 B_1
:
A_N B_N

Output

Print the maximum possible happiness Takahashi can achieve.

Sample Input 1

2 60
10 10
100 100

Sample Output 1

110

By ordering the first and second dishes in this order, Takahashi's happiness will be 110.

Note that, if we manage to order a dish in time, we can spend any amount of time to eat it.

Sample Input 2

3 60
10 10
10 20
10 30

Sample Output 2

60

Takahashi can eat all the dishes within 60 minutes.

Sample Input 3

3 60
30 10
30 20
30 30

Sample Output 3

50

By ordering the second and third dishes in this order, Takahashi's happiness will be 50.

We cannot order three dishes, in whatever order we place them.

Sample Input 4

10 100
15 23
20 18
13 17
24 12
18 29
19 27
23 21
18 20
27 15
22 25

Sample Output 4

145


const N,K=map(x->parse(Int,x),split(readline()))
const H=map(x->parse(Int,x),split(readline()))
const dp=ones(Int,N+1,N+1)*10^13
function main()
	dp[1,1]=0
	for i=1:N,j=2:N+1,k=1:i
		dp[i+1,j]=min(dp[i+1,j],dp[k,j-1]+max(0,H[i]-(k==1 ? 0 : H[k-1])))
	end
	ans=10^13
	for i=1:N+1
		ans=min(ans,dp[i,N-K+1])
	end
	println(ans)
end
main()

Score : 600 points

Problem Statement

We will create an artwork by painting black some squares in a white square grid with 10^9 rows and N columns.

The current plan is as follows: for the i-th column from the left, we will paint the H_i bottommost squares and will not paint the other squares in that column.

Before starting to work, you can choose at most K columns (possibly zero) and change the values of H_i for these columns to any integers of your choice between 0 and 10^9 (inclusive).

Different values can be chosen for different columns.

Then, you will create the modified artwork by repeating the following operation:

Choose one or more consecutive squares in one row and paint them black. (Squares already painted black can be painted again, but squares not to be painted according to the modified plan should not be painted.)

Find the minimum number of times you need to perform this operation.

Constraints

1 \leq N \leq 300

0 \leq K \leq N

0 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 H_2 ... H_N

Output

Print the minimum number of operations required.

Sample Input 1

4 1
2 3 4 1

Sample Output 1

3

For example, by changing the value of H_3 to 2, you can create the modified artwork by the following three operations:

Paint black the 1-st through 4-th squares from the left in the 1-st row from the bottom.

Paint black the 1-st through 3-rd squares from the left in the 2-nd row from the bottom.

Paint black the 2-nd square from the left in the 3-rd row from the bottom.

Sample Input 2

6 2
8 6 9 1 2 1

Sample Output 2

7

Sample Input 3

10 0
1 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000

Sample Output 3

4999999996


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

function main()
    n,k = readline() |> split |> parseMap
    h = readline() |> split |> parseMap
    if n==k
        println(0)
    else
        t = Tuple{Int,Int}[(0,0) for i in 1:n]
        for i in 1:n
            t[i] = (i,h[i])
        end
        t = sort(t,rev=true,by=x->x[2])
        for i in k:-1:1
            if t[i][1] == 1
                h[t[i][1]] = h[2]
            elseif t[i][1] == n
                h[t[i][1]] = h[n-1]
            else
                h[t[i][1]] = min(h[t[i][1]-1],h[t[i][1]+1])
            end
        end
        s = 0
        x = sort(h)
        for i in 1:n
            c = 0
            f = 0
            q = i==1?x[i]:x[i]-x[i-1]
            for i in 1:n
                if h[i] == 0 && f == 1
                    f = 0
                elseif h[i] > 0 && f == 0
                    f = 1
                    c += 1
                end
                h[i]-=q
            end
            s += c*q
        end
        println(s)
    end
end
main()


Score : 100 points

Problem Statement

How many ways are there to choose two distinct positive integers totaling N, disregarding the order?

Constraints

1 \leq N \leq 10^6

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

4

Sample Output 1

1

There is only one way to choose two distinct integers totaling 4: to choose 1 and 3. (Choosing 3 and 1 is not considered different from this.)

Sample Input 2

999999

Sample Output 2

499999


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

function main()
    mod = 998244353
    n = readline() |> parseInt
    d = readline() |> split |> parseMap
    a = zeros(Int,n)
    for i in 1:n
        a[d[i]+1] += 1
    end
    mx = maximum(d)
    q = 1
    for i in 2:mx+1
        if a[i] == 0
            q = 0
        end
        for j in 1:a[i]
            q = (q*a[i-1])%mod
        end
    end
    println(q)
end

main()

Score : 300 points

Problem Statement

Given is an integer sequence D_1,...,D_N of N elements. Find the number, modulo 998244353, of trees with N vertices numbered 1 to N that satisfy the following condition:

For every integer i from 1 to N, the distance between Vertex 1 and Vertex i is D_i.

Notes

A tree of N vertices is a connected undirected graph with N vertices and N-1 edges, and the distance between two vertices are the number of edges in the shortest path between them.

Two trees are considered different if and only if there are two vertices x and y such that there is an edge between x and y in one of those trees and not in the other.

Constraints

1 \leq N \leq 10^5

0 \leq D_i \leq N-1

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N

Output

Print the answer.

Sample Input 1

4
0 1 1 2

Sample Output 1

2

For example, a tree with edges (1,2), (1,3), and (2,4) satisfies the condition.

Sample Input 2

4
1 1 1 1

Sample Output 2

0

Sample Input 3

7
0 3 2 1 2 2 1

Sample Output 3

24


#=
C:
- Julia version:
- Author: abap
- Date: 2019-11-09
=#
parseInt(x::SubString{String}) = parse(Int,x)

function main()
    count::Int64 = 0
    N::Int64 = parse(readline())
    A::Array{Int64} = parseInt.(split(readline()))
    B::Array{Int64} = parseInt.(split(readline()))
    sort!(A,alg = QuickSort)
    sort!(B,alg = QuickSort)
    @simd for i in 1:N
        if B[i] < A[N]
            @inbounds A[N], B[i] = B[i], A[N]
            sort!(A,alg = QuickSort)
            count += 1
        end
    end
    if count > N - 2
        println("No")
    else
        println("Yes")
    end
end

main()

Score : 600 points

Problem Statement

Given are two integer sequences of N elements each: A_1,...,A_N and B_1,...,B_N.
Determine if it is possible to do the following operation at most N-2 times (possibly zero) so that, for every integer i from 1 to N, A_i \leq B_i holds:

Choose two distinct integers x and y between 1 and N (inclusive), and swap the values of A_x and A_y.

Constraints

2 \leq N \leq 10^5

1 \leq A_i,B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

3
1 3 2
1 2 3

Sample Output 1

Yes

We should swap the values of A_2 and A_3.

Sample Input 2

3
1 2 3
2 2 2

Sample Output 2

No

Sample Input 3

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

Sample Output 3

Yes


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function eval(a::Tuple{Int,Int},b::Tuple{Int,Int})
	if a[2]<b[2]
		1
	elseif a[2]==b[2]
		0
	else
		-1
	end
end

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

function hpop(h::Array{Tuple{Int,Int},1})
	x = h[1]
	z = pop!(h)
	if !isempty(h)
		h[1] = z
		k = 1
		l = length(h)
		while k <= l
			if 2*k>l
				break
			elseif 2*k+1>l
				if eval(h[2*k],z)>=0
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if eval(z,h[2*k])>=0&&eval(z,h[2*k+1])>=0
					break
				else
					t = eval(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 = readline() |> split |> parseMap
    e = [Tuple{Int,Int}[] for i in 1:n]
    for i in 2:n
        push!(e[i],(i-1,0))
    end
    for i in 1:m
        l,r,c = readline() |> split |> parseMap
        push!(e[l],(r,c))
        push!(e[r],(l,c))
    end
    dis = -ones(Int, length(e))
	h = Array{Tuple{Int,Int},1}()
	push!(h,(1,0))
	while !isempty(h)
		v,w = hpop(h)
		dis[v] = dis[v]<0?w:min(dis[v],w)
		for i in 1:length(e[v])
			if dis[e[v][i][1]]<0　|| dis[e[v][i][1]]>w+e[v][i][2]
				dis[e[v][i][1]] = w+e[v][i][2]
				hpush(h,(e[v][i][1],w+e[v][i][2]))
			end
		end
	end
	println(dis[n])
end

main()

Score : 600 points

Problem Statement

We have N points numbered 1 to N arranged in a line in this order.

Takahashi decides to make an undirected graph, using these points as the vertices.
In the beginning, the graph has no edge. Takahashi will do M operations to add edges in this graph.
The i-th operation is as follows:

The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.

The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as input.

Takahashi wants to solve the shortest path problem in the final graph obtained. Find the length of the shortest path from Vertex 1 to Vertex N in the final graph.

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i < R_i \leq N

1 \leq C_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 C_1
:
L_M R_M C_M

Output

Print the length of the shortest path from Vertex 1 to Vertex N in the final graph.
If there is no shortest path, print -1 instead.

Sample Input 1

4 3
1 3 2
2 4 3
1 4 6

Sample Output 1

5

We have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of length 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between Vertex 1 and Vertex 4.

Sample Input 2

4 2
1 2 1
3 4 2

Sample Output 2

-1

Sample Input 3

10 7
1 5 18
3 4 8
1 3 5
4 7 10
5 9 8
6 10 5
8 10 3

Sample Output 3

28


S=chomp(readline())
N=length(S)
id=1
ans=0
while id<=N
	L=R=0
	while id<=N&&S[id]=='<'
		id+=1
		L+=1
	end
	while id<=N&&S[id]=='>'
		id+=1
		R+=1
	end
	ans+=div(L*(L+1),2)+div(R*(R+1),2)-min(L,R)
end
println(ans)

Score : 300 points

Problem Statement

Given is a string S of length N-1.
Each character in S is < or >.

A sequence of N non-negative integers, a_1,a_2,\cdots,a_N, is said to be good when the following condition is satisfied for all i (1 \leq i \leq N-1):

If S_i= <: a_i<a_{i+1}

If S_i= >: a_i>a_{i+1}

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

Constraints

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

S is a string of length N-1 consisting of < and >.

Input

Input is given from Standard Input in the following format:

S

Output

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

Sample Input 1

<>>

Sample Output 1

3

a=(0,2,1,0) is a good sequence whose sum is 3.
There is no good sequence whose sum is less than 3.

Sample Input 2

<>>><<><<<<<>>><

Sample Output 2

28


print(parse(Int,readline()) in [i*j for i=1:9, j=1:9] ? "Yes" : "No")

Score : 200 points

Problem Statement

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

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

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

10

Sample Output 1

Yes

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

Sample Input 2

50

Sample Output 2

No

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

Sample Input 3

81

Sample Output 3

Yes


N=parse(Int,readline())
T=N+1
for i=2:isqrt(N)
  if N%i==0
    T=i+div(N,i)-2
  end
end
println(T)

Score : 300 points

Problem Statement

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

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

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

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

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

10

Sample Output 1

5

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

Sample Input 2

50

Sample Output 2

13

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

Sample Input 3

10000000019

Sample Output 3

10000000018

Both input and output may be enormous.


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

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

  ans=0
  m=floor(Int,sqrt(n))
  for i in m:-1:1
    if n%i==0
      ans=i-1+n÷i-1
      break
    end
  end

  println(ans)
end

main()


a,b,x = parse.(Int,split(readline()))

if 2x <= a^2 * b
    y = 2x / (a*b)
    print(rad2deg(atan(b/y)))
else
    y = 2(a^2*b - x) /a^2
    print(rad2deg(atan(y/a)))
end

Score : 400 points

Problem Statement

Takahashi has a water bottle with the shape of a rectangular prism whose base is a square of side a~\mathrm{cm} and whose height is b~\mathrm{cm}. (The thickness of the bottle can be ignored.)

We will pour x~\mathrm{cm}^3 of water into the bottle, and gradually tilt the bottle around one of the sides of the base.

When will the water be spilled? More formally, find the maximum angle in which we can tilt the bottle without spilling any water.

Constraints

All values in input are integers.

1 \leq a \leq 100

1 \leq b \leq 100

1 \leq x \leq a^2b

Input

Input is given from Standard Input in the following format:

a b x

Output

Print the maximum angle in which we can tilt the bottle without spilling any water, in degrees.
Your output will be judged as correct when the absolute or relative error from the judge's output is at most 10^{-6}.

Sample Input 1

2 2 4

Sample Output 1

45.0000000000

This bottle has a cubic shape, and it is half-full. The water gets spilled when we tilt the bottle more than 45 degrees.

Sample Input 2

12 21 10

Sample Output 2

89.7834636934

This bottle is almost empty. When the water gets spilled, the bottle is nearly horizontal.

Sample Input 3

3 1 8

Sample Output 3

4.2363947991

This bottle is almost full. When the water gets spilled, the bottle is still nearly vertical.


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)

  prev = maximum(a.*f)
  curr = div(prev, 2)
  ans = 10^18 + 1
  while true
    k′ = 0
    for i in 1:n
      if curr >= a[i] * f[i]
        continue
      end
      k′ += a[i] - floor(Int, curr / f[i])
    end
    # println("$(curr) $(prev) $(k′)")
    m = max(div(abs(prev - curr), 2), 1)
    prev = curr
    if k′ <= k
      ans = curr
      curr -= m
      curr = max(curr, 0)
    else
      curr += m
    end
    if curr == prev || curr == ans
      break
    end
  end

  println(ans)
end

main()

Score: 500 points

Problem Statement

Takahashi will take part in an eating contest. Teams of N members will compete in this contest, and Takahashi's team consists of N players numbered 1 through N from youngest to oldest. The consumption coefficient of Member i is A_i.

In the contest, N foods numbered 1 through N will be presented, and the difficulty of Food i is F_i. The details of the contest are as follows:

A team should assign one member to each food, and should not assign the same member to multiple foods.

It will take x \times y seconds for a member to finish the food, where x is the consumption coefficient of the member and y is the difficulty of the dish.

The score of a team is the longest time it takes for an individual member to finish the food.

Before the contest, Takahashi's team decided to do some training. In one set of training, a member can reduce his/her consumption coefficient by 1, as long as it does not go below 0. However, for financial reasons, the N members can do at most K sets of training in total.

What is the minimum possible score of the team, achieved by choosing the amounts of members' training and allocating the dishes optimally?

Constraints

All values in input are integers.

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

0 \leq K \leq 10^{18}

1 \leq A_i \leq 10^6\ (1 \leq i \leq N)

1 \leq F_i \leq 10^6\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N
F_1 F_2 ... F_N

Output

Print the minimum possible score of the team.

Sample Input 1

3 5
4 2 1
2 3 1

Sample Output 1

2

They can achieve the score of 2, as follows:

Member 1 does 4 sets of training and eats Food 2 in (4-4) \times 3 = 0 seconds.

Member 2 does 1 set of training and eats Food 3 in (2-1) \times 1 = 1 second.

Member 3 does 0 sets of training and eats Food 1 in (1-0) \times 2 = 2 seconds.

They cannot achieve a score of less than 2, so the answer is 2.

Sample Input 2

3 8
4 2 1
2 3 1

Sample Output 2

0

They can choose not to do exactly K sets of training.

Sample Input 3

11 14
3 1 4 1 5 9 2 6 5 3 5
8 9 7 9 3 2 3 8 4 6 2

Sample Output 3

12


function f(A,F,X)
	T=0
	for (a,b)=zip(A,F)
		t=a*b-X
		if t>0
			T+=cld(t,b)
		end
	end
	T
end
function main()
	N,K=map(x->parse(Int,x),split(readline()))
	A=sort(map(x->parse(Int,x),split(readline())))
	F=reverse(sort(map(x->parse(Int,x),split(readline()))))
	L=-1
	R=2^40
	while R-L>1
		M=(L+R)>>1
		if f(A,F,M)<=K
			R=M
		else
			L=M
		end
	end
	println(R)
end
main()


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.


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

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

  ans=min(0,a-2b)
  println(ans)
end

main()


N = readline()
d = parse(split(readline()))
len = length(d)
sum = 0
for i in 1:len
  for j in i:len
    sum += d[i]*d[j]
  end
end
println(sum)

Score : 200 points

Problem Statement

It's now the season of TAKOYAKI FESTIVAL!

This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i.

As is commonly known, when you eat two takoyaki of deliciousness x and y together, you restore x \times y health points.

There are \frac{N \times (N - 1)}{2} ways to choose two from the N takoyaki served in the festival. For each of these choices, find the health points restored from eating the two takoyaki, then compute the sum of these \frac{N \times (N - 1)}{2} values.

Constraints

All values in input are integers.

2 \leq N \leq 50

0 \leq d_i \leq 100

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the sum of the health points restored from eating two takoyaki over all possible choices of two takoyaki from the N takoyaki served.

Sample Input 1

3
3 1 2

Sample Output 1

11

There are three possible choices:

Eat the first and second takoyaki. You will restore 3 health points.

Eat the second and third takoyaki. You will restore 2 health points.

Eat the first and third takoyaki. You will restore 6 health points.

The sum of these values is 11.

Sample Input 2

7
5 0 7 8 3 3 2

Sample Output 2

312


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

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

  ans=sum(d[i]*d[j] for i in 1:n-1 for j in i+1:n)
  println(ans)
end

main()


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

function main()
	n = readline() |> parseInt
	s = readline() |> chomp
	k = 1
	for i in 2:n
		if s[i]!=s[i-1]
			k += 1
		end
	end
	println(k)
end

main()

Score : 300 points

Problem Statement

There are N slimes lining up from left to right. The colors of these slimes will be given as a string S of length N consisting of lowercase English letters. The i-th slime from the left has the color that corresponds to the i-th character of S.

Adjacent slimes with the same color will fuse into one larger slime without changing the color. If there were a slime adjacent to this group of slimes before fusion, that slime is now adjacent to the new larger slime.

Ultimately, how many slimes will be there?

Constraints

1 \leq N \leq 10^5

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the final number of slimes.

Sample Input 1

10
aabbbbaaca

Sample Output 1

5

Ultimately, these slimes will fuse into abaca.

Sample Input 2

5
aaaaa

Sample Output 2

1

All the slimes will fuse into one.

Sample Input 3

20
xxzaffeeeeddfkkkkllq

Sample Output 3

10


parseline(str=readline()) = parse.(Int, split(str))
function binarysearches(arr, left, right)
    r = searchsortedfirst(arr, right) - 1
    l = searchsortedlast(arr, left) + 1
    ans = r - l + 1
    return max(0, ans)
end
function Triangles()
    N = parse(Int, readline())
    L = sort(parseline())
    ans = 0
    for n in N:-1:2
        for m in n-1:-1:1
            b = binarysearches(L, L[n]-L[m], L[n]+L[m])
            ans += L[n]<2L[m] ? b - 2 : b - 1
        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


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

function main()
	n,m,l = readline() |> split |> parseMap
	g = zeros(Int,n,n)
	d = 10^12*ones(Int,n,n)
	e = 10^12*ones(Int,n,n)
	for i in 1:n
		d[i,i] = 0
	end
	for i in 1:m
		a,b,c = readline() |> split |> parseMap
		g[a,b] = c
		g[b,a] = c
		d[a,b] = c
		d[b,a] = c
	end
	for k in 1:n
		for i in 1:n
			for j in 1:n
				d[i,j] = min(d[i,j], d[i,k] + d[k,j])
			end
		end
	end
	for i in 1:n
		for j in 1:n
			if d[i,j] <=l
				e[i,j] = 1
			end
		end
	end
	for k in 1:n
		for i in 1:n
			for j in 1:n
				e[i,j] = min(e[i,j], e[i,k] + e[k,j])
			end
		end
	end
	q = readline() |> parseInt
	for i in 1:q
		s,t = readline() |> split |> parseMap
		println(e[s,t]<10^11?e[s,t]-1:-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


Score : 100 points

Problem Statement

Given is an integer N.

Takahashi chooses an integer a from the positive integers not greater than N with equal probability.

Find the probability that a is odd.

Constraints

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the probability that a is odd.
Your output will be considered correct when its absolute or relative error from the judge's output is at most 10^{-6}.

Sample Input 1

4

Sample Output 1

0.5000000000

There are four positive integers not greater than 4: 1, 2, 3, and 4. Among them, we have two odd numbers: 1 and 3. Thus, the answer is \frac{2}{4} = 0.5.

Sample Input 2

5

Sample Output 2

0.6000000000

Sample Input 3

1

Sample Output 3

1.0000000000


N,K=map(x->parse(Int,x),split(readline()))
cnt=0
for x=map(x->parse(Int,x),split(readline()))
  cnt+=x>=K
end
println(cnt)

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)
function main()
    n=parseInt(readline())
    a=parseMap(split(readline()))
    num=Tuple{Int,Int}[(a[i],i) for i in 1:n]
    sort!(num,by=x->x[1])
    println(join([num[i][2] for i in 1:n]," "))
end
main()

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)
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
	f[length(a)] = 0
	println(sum(f))
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()
	n,m = readline() |> split |> parseMap
	dp = (10^9)*ones(Int,m+1,2^n)
	dp[1,1] = 0
	for i in 1:m
		a,b = readline() |> split |> parseMap
		c = readline() |> split |> parseMap
		d = 0
		for j in 1:b
			d += 2^(c[j]-1)
		end
		for j in 1:2^n
			dp[i+1,j] = dp[i,j]
		end
		for j in 1:2^n
			dp[i+1,((j-1)|d)+1] = min(dp[i+1,((j-1)|d)+1],dp[i,j]+a)
		end
	end
	if dp[m+1,2^n] == 10^9
		println(-1)
	else
		println(dp[m+1,2^n])
	end
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


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


H,W,A,B=map(x->parse(Int,x),split(readline()))
for i=1:H
	for j=1:W
		print((i<=A)!=(j<=B) ? 1 : 0)
	end
	println()
end

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


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

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

  # if 2a<w || 2b<h
  #   println("No")
  #   return
  # end

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

  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:h÷bb,j=1:w÷aa
    ans[(i-1)*bb+1:i*bb,1:aa]=pat
    ans[1:bb,(j-1)*aa+1:j*aa]=pat
  end
  if a!=0 && w%aa!=0
    j=w÷aa
    ans[1:bb,j*aa+1:w]=pat[:,1:w%aa]
  end
  if b!=0 && h%bb!=0
    i=h÷bb
    ans[i*bb+1:h,1:aa]=pat[1:h%bb,:]
  end

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

main()


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

function root(uf::Array{Int,1},x)
	s=x
	while uf[s]!=s
		s=uf[s]
	end
	s
end
function same(uf::Array{Int,1},x,y)
	root(uf,x)==root(uf,y)?true:false
end
function unite(uf::Array{Int,1},x,y)
	l = root(uf,x)
	r = root(uf,y)
	if l < r
		uf[r] = l
	elseif l > r
		uf[l] = r
	end
end

function evalma(st::Array{Int,1},ll,rr)
	r = 0
	while 2^r<length(st)
		r += 1
	end
	r -= 1
	hd = 2^r-1
	sl = hd+ll
	sr = hd+rr
	v = max(st[sl],st[sr])
	for i in r:-1:1
		if sr>>i-sl>>i>1
			if (sl>>i)%2==0
				v=max(v,st[sl>>i+1])
			end
			if (sr>>i)%2==1
				v = max(v,st[sr>>i-1])
			end
		end
	end
	v
end

function evalmi(st::Array{Int,1},ll,rr)
	r = 0
	while 2^r<length(st)
		r += 1
	end
	r -= 1
	hd = 2^r-1
	sl = hd+ll
	sr = hd+rr
	v = min(st[sl],st[sr])
	for i in r:-1:1
		if sr>>i-sl>>i>1
			if (sl>>i)%2==0
				v=min(v,st[sl>>i+1])
			end
			if (sr>>i)%2==1
				v = min(v,st[sr>>i-1])
			end
		end
	end
	v
end

function main()
	n,k = readline() |> split |> parseMap
	p = readline() |> split |> parseMap
	ve = zeros(Int,n)
	for i in 2:n
		ve[i] = ve[i-1]
		if p[i]<p[i-1]
			ve[i] += 1
		end
	end
	r = 0
	while 2^r<=n
		r += 1
	end
	hd = 2^r-1
	stma = zeros(Int,2^(r+1))
	stmi = 200000*ones(Int,2^(r+1))
	for i in 1:n
		for j in 0:r
			stma[(hd+i)>>j] = max(stma[(hd+i)>>j],p[i])
			stmi[(hd+i)>>j] = min(stmi[(hd+i)>>j],p[i])
		end
	end
	uf = Int[i for i in 1:n-k+2]
	f = 1
	if ve[k] == 0
		f = 0
		unite(uf,1,n-k+2)
	end
	for i in 2:n-k+1
		if ve[i+k-1]-ve[i]==0
			f = 0
			unite(uf,i,n-k+2)
		else
			ma = evalma(stma,i,i+k-2)
			mi = evalmi(stmi,i,i+k-2)
			if p[i-1]<mi&&p[i+k-1]>ma
				unite(uf,i,i-1)
			end
		end
	end
	println(length(Set(uf))-f)
end

main()

Score : 700 points

Problem Statement

Snuke has a permutation (P_0,P_1,\cdots,P_{N-1}) of (0,1,\cdots,N-1).

Now, he will perform the following operation exactly once:

Choose K consecutive elements in P and sort them in ascending order.

Find the number of permutations that can be produced as P after the operation.

Constraints

2 \leq N \leq 200000

2 \leq K \leq N

0 \leq P_i \leq N-1

P_0,P_1,\cdots,P_{N-1} are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_0 P_1 \cdots P_{N-1}

Output

Print the number of permutations that can be produced as P after the operation.

Sample Input 1

5 3
0 2 1 4 3

Sample Output 1

2

Two permutations can be produced as P after the operation: (0,1,2,4,3) and (0,2,1,3,4).

Sample Input 2

4 4
0 1 2 3

Sample Output 2

1

Sample Input 3

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

Sample Output 3

6


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[chomp(readline())])
end
main()

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


function main()

  S = chomp(readline())

  next = Dict("Sunny" => "Cloudy", "Cloudy" => "Rainy", "Rainy" => "Sunny")

  pritln(next[S])

end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    s=chomp(readline())
    flg=true
    for i in 1:length(s)
        if i%2==1 && !in(s[i],"RUD")
            flg=false
        elseif i%2==0 && !in(s[i],"LUD")
            flg=false
        end
    end
    println(ifelse(flg,"Yes","No"))
end
main()

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


function main()
    s = readline() |> chomp
    for i = 1:length(s)
        if i & 1 == 0 && s[i] == 'R'
            return println("No")
        elseif i & 1 == 1 && s[i] == 'L'
            return println("No")
        end
    end
    println("Yes")
end

main()


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

function main()
	s = readline() |> chomp
	n = length(s)
	f = 0
	for i in 1:n
		if i%2==1&&s[i]=='L'
				f = 1
				break
		elseif i%2==0&&s[i]=='R'
			f = 1
			break
		end
	end
	println(f==0?"Yes":"No")
end

main()


# n, k, q = parse.(Int, readline() |> split)
n, k, q = map(split(readline(), " ")) do x
    parse(Int, x)
end

A = zeros(Int, n)
for i in 1:q
    a_i = parse(Int, readline())
    A[a_i] += 1
end

for a in A
    println(a > q - k ? "Yes" : "No")
end

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


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][j:m] = bsabs(a[l][j:m],a[i][j:m],m-j+1)
						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][j:m] = bsabs(a[o-k+1][j:m],a[o-i+1][j:m],m-j+1)
				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.


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.


n=parse(Int,readline())
a,b,c=[(x->parse(Int,x)).(split(i)) for i in readlines()]
ans=sum([b...,[c[a[i]] for i=1:n-1 if a[i]+1==a[i+1]]...])
print(ans)

Score : 200 points

Problem Statement

Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them (Dish 1, Dish 2, \ldots, Dish N) once.

The i-th dish (1 \leq i \leq N) he ate was Dish A_i.

When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.

Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N - 1), he gains C_i more satisfaction points.

Find the sum of the satisfaction points he gained.

Constraints

All values in input are integers.

2 \leq N \leq 20

1 \leq A_i \leq N

A_1, A_2, ..., A_N are all different.

1 \leq B_i \leq 50

1 \leq C_i \leq 50

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1}

Output

Print the sum of the satisfaction points Takahashi gained, as an integer.

Sample Input 1

3
3 1 2
2 5 4
3 6

Sample Output 1

14

Takahashi gained 14 satisfaction points in total, as follows:

First, he ate Dish 3 and gained 4 satisfaction points.

Next, he ate Dish 1 and gained 2 satisfaction points.

Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.

Sample Input 2

4
2 3 4 1
13 5 8 24
45 9 15

Sample Output 2

74

Sample Input 3

2
1 2
50 50
50

Sample Output 3

150


b=parse.(Int,split(readlines()[2]))
println(b[1]+sum(Int[min(b[i-1],b[i]) for i=2:length(b)])+b[end])

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


N=parse(readline())
B=parse.(split(readline()))
A=[]
push!(A,B[1])
for i in 1:N-2
  push!(A,min(B[i],B[i+1]))
end
push!(A,B[N-1])
println(sum(A))


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

function main()
  n,k = readline() |> split |> parseArray
  s = readline() |> strip |> collect

  ans=0
  n>1&&s[1]=='R'&&s[2]=='R' && (ans+=1)
  for i in 2:n-1
    s[i]=='R'&&s[i+1]=='R' && (ans+=1)
    s[i]=='L'&&s[i-1]=='L' && (ans+=1)
  end
  n>1&&s[end-1]=='L'&&s[end]=='L' && (ans+=1)

  ans=min(ans+2k, n-1)

  println(ans)
end

main()

Score : 400 points

Problem Statement

There are N people standing in a queue from west to east.

Given is a string S of length N representing the directions of the people.
The i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.

A person is happy if the person in front of him/her is facing the same direction.
If no person is standing in front of a person, however, he/she is not happy.

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

Operation: Choose integers l and r such that 1 \leq l \leq r \leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.

What is the maximum possible number of happy people you can have?

Constraints

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

K is an integer satisfying 1 \leq K \leq 10^5.

|S| = N

Each character of S is L or R.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of happy people after at most K operations.

Sample Input 1

6 1
LRLRRL

Sample Output 1

3

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.

Sample Input 2

13 3
LRRLRLRRLRLLR

Sample Output 2

9

Sample Input 3

10 1
LLLLLRRRRR

Sample Output 3

9

Sample Input 4

9 2
RRRLRLRLL

Sample Output 4

7


const N=parse(Int,readline())
const P=map(x->parse(Int,x),split(readline()))
const A=zeros(Int,N)
const B=zeros(Int,N)
const Ta=zeros(Int,17,N)
function init()
	B[1]=1
	for i=1:N
		A[P[i]]=i
		if i>1
			B[i]=B[i>>1]+1
		end
		Ta[1,i]=P[i]
	end
	for k=2:16
		L=1<<(k-2)
		for i=1:N-L+1
			Ta[k,i]=Ta[k-1,i]
			if i+L+L<=N+1&&Ta[k,i]<Ta[k-1,i+L]
				Ta[k,i]=Ta[k-1,i+L]
			end
		end
	end
end
function query(L,R)
	if L>=R
		return 0
	end
	b=B[R-L]
	X=max(Ta[b,L],Ta[b,R-(1<<(b-1))])
	X
end
init()
function main()
	ans=0
	for i=N-1:-1:1
		id=A[i]
		L1L=0
		L1R=id
		if query(1,id)<i
			L1R=0
		end
		while L1R-L1L>1
			L1M=div(L1R+L1L,2)
			if query(L1M,id)>i
				L1L=L1M
			else
				L1R=L1M
			end
		end
		L1=id-L1L
		L2L=0
		L2R=L1L
		while L2R-L2L>1
			L2M=div(L2R+L2L,2)
			if query(L2M,L1L)>i
				L2L=L2M
			else
				L2R=L2M
			end
		end
		L2=L1L-L2L
		R1L=id+1
		R1R=N+1
		if query(id,N+1)==i
			R1L=N+1
		end
		while R1R-R1L>1
			R1M=div(R1R+R1L,2)
			if query(id,R1M)>i
				R1R=R1M
			else
				R1L=R1M
			end
		end
		R1=R1L-id
		R2L=R1R
		R2R=N+1
		if query(R1R,N+1)<i
			R2L=N+1
		end
		while R2R-R2L>1
			R2M=div(R2R+R2L,2)
			if query(R1R,R2M)>i
				R2R=R2M
			else
				R2L=R2M
			end
		end
		R2=R2L-R1L
		ans+=i*(L1*R2+R1*L2)
	end
	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 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(k>>1,1)
			l = l>>1
		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)
	o = Int[n]
	for i in 0:n-1
		for j in 1:2^i
			push!(o,n-1-i)
		end
	end
	h = Int[s[1] for i in 1:n]
	f = 0
	for i in 2:2^n
		a = hpop(h)
		if s[i]>=a
			f = 1
			break
		else
			for j in 1:o[i]
				hpush(h,s[i])
			end
		end
	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)
	o = Int[n]
	for r in 0:n-1
		for times in 1:2^r
			push!(o,n-1-r)
		end
	end
	h = Int[s[1] for i in 1:n]
	f = 0
	for i in 2:2^n
		a = hpop(h)
		if s[i]>=a
			f = 1
			break
		else
			for j in 1:o[i]
				hpush(h,s[i])
			end
		end
	end
	println(f==0?"Yes":"No")
end

main()


a,b=readlines()
c=0
for i=1:3
  c += ifelse(a[i]==b[i],1,0)
end
println(c)

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


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

test()


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

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


parseInt(x) = parse(Int,x)

function main()
  A,B = map(parseInt,split(readline()))
  if A < B
    r = div(B-A,A-1)
    rem(B-A,A-1) == 0 ? c=r+1 : c=r+2
  else
    c = 1
  end
  println(c)
end

main()


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

function main()
	a,b = readline() |> split |> parseMap
	k = 0
	now = 1
	while now<b
		k += 1
		now += a-1
	end
	println(k)
end

main()


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


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
N = parseInt(readline())
H = parseMap(split(readline()))
x = 100000000000000000000
count = 0
res = []
for i in H
	if i <= x
    	count += 1
    	x = i
    else
    	push!(res,count)
    	count = 0
    	x = 10000000000000000000
    end
end
res = res.- 1

println(maximum(res))

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


function main()
	n = parse(readline())
	h = map(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))
end

main()


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

function main()
	n = readline() |> parseInt
	println(div(n*(n-1),2))
end

main()

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


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
	while fin<n
		chk = zeros(Int,n)
		sx = 0
		for i in 2:n
			if now[i]<n
				d = a[i,now[i]]
				if d<i&&chk[d]==0&&a[d,now[d]]==i
					chk[i]=1
					chk[d]=1
					sx+=2
					now[i]+=1
					now[d]+=1
					if now[d]==n
						fin += 1
					end
				end
			end
			if now[i]==n
				fin += 1
			end
		end
		if sx == 0
			f = 1
			break
		else
			k += 1
		end
	end
	println(f==1?-1:k)
end

main()


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

function main()
	n = readline() |> parseInt
	a = [Array{Int,1}(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)*n
	b = Set{Int}(Int[i for i in 1:n])
	while l>0
		t = 0
		c = Set{Int}(Int[])
		for i in b
			if !isempty(a[i])
				j = a[i][1]
				if !isempty(a[j])
					if a[j][1] == i
						push!(c,i)
						push!(c,j)
						t += 1
					end
				else
					f = 1
				end
			end
		end
		l -= length(c)
		if t>0
			k += 1
			for i in c
				if !isempty(a[i])
					shift!(a[i])
				end
			end
			b = copy(c)
		else
			f = 1
		end
		if f == 1
			break
		end
	end
	println(f==0?k:-1)
end

main()


n = parse(Int64, strip(readline()))
points = Complex[]
for i in 1:n
  a,b = map(x->parse(Int64, x), split(readline()))
  push!(points, a + b*1im)
end
sort!(points, by=angle)
points = vcat(points, points)

lim = 0
loc = 0+0im
best = 0
for i in 1:n
  if i != 1
    loc -= points[i-1]
  end
  while lim < i + n - 1 && abs2(loc) <= abs2(loc + points[lim+1])
    lim += 1
    loc += points[lim]
  end
  if best < abs2(loc)
    best = abs2(loc)
  end
end
println(sqrt(best))

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=700
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 B=600
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 B=500
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 B=5000
function f(X,Y,x,y)
	z=x[:]
	w=y[:]
	for i=1:B
		tx=X+x[i]
		ty=Y+y[i]
		theta=atan(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()