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

function main()
    s=chomp(readline())
    n=length(s)
    res=[1 for i in 1:n+1]
    if s[1]==s[2]
        res[2]+=1
    end
    for i in 3:n
        if s[i]==s[i-1]
            res[i+1]=res[i-2]+2
        else
            res[i+1]=res[i]+1
        end
    end
    println(res[n+1])
end
main()

Score : 300 points

Problem Statement

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

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

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

Constraints

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

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum positive integer K that satisfies the condition.

Sample Input 1

aabbaa

Sample Output 1

4

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

Sample Input 2

aaaccacabaababc

Sample Output 2

12


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

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

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

Submitted source-code file size, in bytes.

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

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


pre=""
cnt=0
now=""
for c=chomp(readline())
	now=now*c
	if pre!=now
		pre=now
		now=""
		cnt+=1
	end
end
println(cnt)


Score : 100 points

Problem Statement

We have two integers: A and B.

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

Constraints

All values in input are integers.

-100 \leq A,\ B \leq 100

Input

Input is given from Standard Input in the following format:

A B

Output

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

Sample Input 1

-13 3

Sample Output 1

-10

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

Sample Input 2

1 -33

Sample Output 2

34

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

Sample Input 3

13 3

Sample Output 3

39

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


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

x = [A+B,A-B,A*B]

println(maximum(x))


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    k,x=parseMap(split(readline()))
    MIN=max(-1000000,x-k+1)
    MAX=min(1000000,x+k-1)
    println(join([string(i) for i in MIN:MAX]," "))
end
main()parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    k,x=parseMap(split(readline()))
    println(join([string(i) for i in x-k+1:x+k-1]," "))
end
main()

Score : 200 points

Problem Statement

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

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

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

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

Constraints

1 \leq K \leq 100

0 \leq X \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K X

Output

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

Sample Input 1

3 7

Sample Output 1

5 6 7 8 9

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

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

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

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

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

Sample Input 2

4 0

Sample Output 2

-3 -2 -1 0 1 2 3

Negative coordinates can also contain a stone painted black.

Sample Input 3

1 100

Sample Output 3

100


k, x = parse.(split(readline()))
for i = x-k+1:x+k-1
  println(i)
end


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

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

  a=x-k+1
  b=x+k-1
  print(a)
  for i in a+1:b
    print(" ")
    print(i)
  end
  println()
end

main()


#=
C:
- Julia version: 0.5.2
- Author: abap
- Date: 2019-12-29
=#

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

N = parseInt(readline())

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

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

end

main()

Score : 300 points

Problem Statement

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

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

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

Constraints

2 \leq N \leq 10^5

s_i is a string of length 10.

Each character in s_i is a lowercase English letter.

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

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

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

Sample Input 1

3
acornistnt
peanutbomb
constraint

Sample Output 1

1

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

Sample Input 2

2
oneplustwo
ninemodsix

Sample Output 2

0

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

Sample Input 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

Sample Output 3

4

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


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

N = parseInt(readline())

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

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

#=B = Set(A)
kaburi = length(A) - length(B)=#

count = 0
for k in 1:N
	string = A[k]
	for ii in 1:N
		if string == A[ii]
			count += 1
		end
	end
end
count -= N
count /= 2
count = Int(count)
println(count)


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function heap(a::Array{Tuple{Int,Int},1})
	h = [a[1]]
	for i in 2:length(a)
		hpush(h,a[i])
	end
	h
end
function hpush(h::Array{Tuple{Int,Int},1},x::Tuple{Int,Int})
	push!(h,x)
	if length(h) > 1
		l = length(h)
		k = div(l,2)
		while h[k][2]<x[2]&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k = max(div(k,2),1)
			l = div(l,2)
		end
	end
end

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

function main()
	n,m = readline() |> split |> parseMap
	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
	v = zeros(Int,m)
	a = sort(a,by=x->x[1])
	h = Array{Tuple{Int,Int},1}()
	for i in 1:m
		if !isempty(a)
			while a[1][1]<=i
				hpush(h,shift!(a))
				if isempty(a)
					break
				end
			end
		end
		if !isempty(h)
			v[i]=hpop(h)[2]
		end
	end
	println(sum(v))
end

main()

Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

Take and complete the first job today. You will earn the reward of 3 after four days from today.

Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

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

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0


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

Score : 600 points

Problem Statement

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

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

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

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

Constraints

2 \leq p \leq 2999

p is a prime number.

0 \leq a_i \leq 1

Input

Input is given from Standard Input in the following format:

p
a_0 a_1 \ldots a_{p-1}

Output

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

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

Sample Input 1

2
1 0

Sample Output 1

1 1

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

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

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

Sample Input 2

3
0 0 0

Sample Output 2

0 0 0

f(x) = 0 is also valid.

Sample Input 3

5
0 1 0 1 0

Sample Output 3

0 2 0 1 3


Score : 100 points

Problem Statement

We have two bottles for holding water.

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

Bottle 2 contains C milliliters of water.

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

How much amount of water will remain in Bottle 2?

Constraints

All values in input are integers.

1 \leq B \leq A \leq 20

1 \leq C \leq 20

Input

Input is given from Standard Input in the following format:

A B C

Output

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

Sample Input 1

6 4 3

Sample Output 1

1

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

Sample Input 2

8 3 9

Sample Output 2

4

Sample Input 3

12 3 7

Sample Output 3

0


parseint(x) = parse(Int, x)

function main()
    a, b, c = map(parseint, readline() |> split)
    max(0, b+c-a) |> println
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n=parseInt(readline())
    println(sum([Int(ndigits(i))%2 for i in 1:n]))
end
main()

Score : 200 points

Problem Statement

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

Constraints

1 \leq N \leq 10^5

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

11

Sample Output 1

9

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

Sample Input 2

136

Sample Output 2

46

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

Sample Input 3

100000

Sample Output 3

90909


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


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

function main()
    s=chomp(readline())
    n=length(s)
    move=[0 for i in 1:n]
    res=0
    for i in n:-1:1
        if s[i]=='L'
            res=0
        else
            res+=1
            move[i]=res
        end
    end
    for i in 1:n
        if s[i]=='R'
            res=0
        else
            res-=1
            move[i]=res
        end
    end
    ans=[0 for i in 1:n]
    for i in 1:n
        ans[i+move[i]+move[i]%2*-1]+=1
    end
    println(join(ans," "))
end
main()

Score : 400 points

Problem Statement

Given is a string S consisting of L and R.

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

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

Initially, one child is standing on each square.

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

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

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

Constraints

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

Each character of S is L or R.

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

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

RRLRL

Sample Output 1

0 1 2 1 1

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

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

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

Sample Input 2

RRLLLLRLRRLL

Sample Output 2

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

Sample Input 3

RRRLLRLLRRRLLLLL

Sample Output 3

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


Score: 100 points

Problem Statement

We have two distinct integers A and B.

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

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

Constraints

All values in input are integers.

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

A and B are distinct.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the integer K satisfying the condition.

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

Sample Input 1

2 16

Sample Output 1

9

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

Sample Input 2

0 3

Sample Output 2

IMPOSSIBLE

No integer satisfies the condition.

Sample Input 3

998244353 99824435

Sample Output 3

549034394


function main()

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

  if (A+B) % 2 == 0
    println(div((A+B),2))
  else
    println("IMPOSSIBLE")
  end

end

main()


parseInt(x) = parse(Int,x)

function main()
  N = parseInt(readline())
  p = map(parseInt, split(readline()))
  l = 0
  for i = 1:N
    if p[i] != i
      l = l+1
    end
  end
  if l <= 2
    println("YES")
   else
    println("NO")
  end
end

main()

Score : 200 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 50

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

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

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

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

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

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

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

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

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


n=parse(Int,readline())
pn=readline()
pn1=split(pn)
X=Any[]
for i=1:n
    X= push!(X , parse(Int,pn1[i]))
end
Y=Any[]
for i=1:n
    Y= push!(Y , i)
end
for i=1:n
    if X[i]!=i
        a=X[i]
        X[i],X[a]=X[a],X[i]
        break
    end
end
k=0
for i=1:n
    if X[i]!=Y[i]
        k=k+1
    else
    end
end

if k==0
    print("YES")
else
    print("NO")
end


n=parse(Int,readline())
pn=readline()
pn1=split(pn)
X=Any[]
for i=1:n
    X= push!(X , parse(Int,pn1[i]))
end
Y=Any[]
for i=1:n
    Y= push!(Y , n-i+1)
end
for i=1:n
    if X[i]!=n-i+1
        a=n-X[i]+1
        X[i],X[a]=X[a],X[i]
        break
    end
end
k=0
for i=1:n
    if X[i]!=Y[i]
        k=k+1
    else
    end
end

if k==0
    print("YES")
else
    print("NO")
end


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
N = parse(readline())
A = parseMap(split(readline()))
B = parseMap(split(readline()))
before_monster = sum(A)
for i in 1:N
    if A[i] > B[i]
        A[i] -= B[i]
    else
        A[i+1] = maximum([A[i+1] - (B[i] - A[i]),0])
  		A[i] = 0
  end
end
println(before_monster - sum(A))

Score : 300 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

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

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

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

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3


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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	b = readline() |> split |> parseMap
	k = 0
	for i in 1:n
		k += min(b[i],a[i]+a[i+1])
		a[i+1]=max(a[i+1]-max(b[i]-a[i],0),0)
	end
	println(k)
end

main()


function Zalgo(s)
	N=length(s)
	ret=zeros(Int,N)
	ret[1]=N
	i=1
	j=0
	while i<N
		while i+j<N&&s[j+1]==s[i+j+1]
			j+=1
		end
		ret[i+1]=j
		if j==0
			i+=1
			continue
		end
		k=1
		while i+k<N&&k+ret[k+1]<j
			ret[i+k+1]=ret[k+1]
			k+=1
		end
		i+=k
		j-=k
	end
	ret
end
const s=chomp(readline())
const t=chomp(readline())
const N=length(s)
const M=length(t)
function make()
now=s[:]
nowlen=N
while nowlen<M
	now*=s
	nowlen+=N
end
now
end
const now=make()
const AA=Zalgo(t*now*now)
const memo=-ones(Int,N)
const ok=[AA[i+M]>=M for i=1:N]
function dfs(u)
	if memo[u]>=0
		return memo[u]
	elseif memo[u]==-2
		return memo[u]=10^9
	end
	if ok[u]
		memo[u]=-2
		return memo[u]=dfs(mod1(u+M,N))+1
	else
		return memo[u]=0
	end
end
ans=0
for i=1:N
	global ans=max(ans,dfs(i))
end
println(ans<10^9?ans:-1)

Score : 600 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if the number of non-negative integers i satisfying the following condition is finite, and find the maximum value of such i if the number is finite.

There exists a non-negative integer j such that the concatenation of i copies of t is a substring of the concatenation of j copies of s.

Notes

A string a is a substring of another string b if and only if there exists an integer x (0 \leq x \leq |b| - |a|) such that, for any y (1 \leq y \leq |a|), a_y = b_{x+y} holds.

We assume that the concatenation of zero copies of any string is the empty string. From the definition above, the empty string is a substring of any string. Thus, for any two strings s and t, i = 0 satisfies the condition in the problem statement.

Constraints

1 \leq |s| \leq 5 \times 10^5

1 \leq |t| \leq 5 \times 10^5

s and t consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If the number of non-negative integers i satisfying the following condition is finite, print the maximum value of such i; if the number is infinite, print -1.

Sample Input 1

abcabab
ab

Sample Output 1

3

The concatenation of three copies of t, ababab, is a substring of the concatenation of two copies of s, abcabababcabab, so i = 3 satisfies the condition.

On the other hand, the concatenation of four copies of t, abababab, is not a substring of the concatenation of any number of copies of s, so i = 4 does not satisfy the condition.

Similarly, any integer greater than 4 does not satisfy the condition, either. Thus, the number of non-negative integers i satisfying the condition is finite, and the maximum value of such i is 3.

Sample Input 2

aa
aaaaaaa

Sample Output 2

-1

For any non-negative integer i, the concatenation of i copies of t is a substring of the concatenation of 4i copies of s. Thus, there are infinitely many non-negative integers i that satisfy the condition.

Sample Input 3

aba
baaab

Sample Output 3

0

As stated in Notes, i = 0 always satisfies the condition.


parseInt(x) = parse(Int,x)
printText(x) = print(x)
threeTimesXSquared(x) = 3(x^2)

readline() |> parseInt |> threeTimesXSquared |> printText

Score : 100 points

Problem Statement

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

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

Constraints

1 \leq r \leq 100

r is an integer.

Input

Input is given from Standard Input in the following format:

r

Output

Print an integer representing the area of the regular dodecagon.

Sample Input 1

4

Sample Output 1

48

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

Sample Input 2

15

Sample Output 2

675

Sample Input 3

80

Sample Output 3

19200


function main()

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

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

  max = maximum(A)
  second = minimum(A)

  for i in 1:N

    if maximum([second,A[i]]) < max
      second = maximum([second,A[i]])
    end

  end

  for i in 1:N

    if A[i] == max
      println(second)
    else
      println(max)
    end

  end

end

main()

Score : 300 points

Problem Statement

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

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

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

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

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

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

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

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

Sample Input 2

2
5
5

Sample Output 2

5
5


function answer(n)
	an = []
	for i in 1:n
		elem = readline()
		elem = parse(Int, elem)
		push!(an, elem)
	end

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


n=parse(readline())
a=map(x->parse(Int,x),split(readline()))
for i=n:-1:1;a[i]=sum(a[i:i:end])%2;end
println(sum(a))
for i=1:n;if a[i]>0;println(i);end;end

Score : 400 points

Problem Statement

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

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

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

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

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

Constraints

All values in input are integers.

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

a_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

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

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

M
b_1 b_2 ... b_M

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

Sample Input 1

3
1 0 0

Sample Output 1

1
1

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

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

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

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

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

Sample Input 2

5
0 0 0 0 0

Sample Output 2

0

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


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

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

main()


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

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

main()

Score : 300 points

Problem Statement

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

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

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

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

What is XOR?

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

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

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

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

Sample Input 2

4
1 2 4 8

Sample Output 2

No

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


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

Score : 700 points

Problem Statement

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

Notes

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

Constraints

2 \leq N \leq 10^5

N-1 \leq M \leq 10^5

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

The given graph is simple and connected.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

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

C_1 D_1
:
C_M D_M

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

Sample Input 1

4 4
1 2
2 3
3 4
4 1

Sample Output 1

1 2
1 4
3 2
3 4

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

Sample Input 2

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

Sample Output 2

-1


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


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

Score : 700 points

Problem Statement

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

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

Constraints

N is an integer.

1 \leq N \leq 10^{5}

Input

Input is given from Standard Input in the following format:

N

Output

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

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

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

Sample Input 1

3

Sample Output 1

Yes
1 2
2 3
3 4
4 5
5 6

The sample output represents the following graph:

Sample Input 2

1

Sample Output 2

No

There is no tree satisfying the condition.


Score : 100 points

Problem Statement

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

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

The taxi will cost us a total of B yen.

How much is our minimum total travel expense?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq A \leq 50

1 \leq B \leq 50

Input

Input is given from Standard Input in the following format:

N A B

Output

Print an integer representing the minimum total travel expense.

Sample Input 1

4 2 9

Sample Output 1

8

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

Sample Input 2

4 2 7

Sample Output 2

7

Sample Input 3

4 2 8

Sample Output 3

8


_,a=readlines()
a=parse.(split(a))
t=sum(a)-2sum(a[2:2:end])
x=[t]
for i=a
    t=2i-t
    push!(x,t)
end
println.(x[1:end-1])

Score : 400 points

Problem Statement

There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number.

Between these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \leq i \leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1).

When Mountain i (1 \leq i \leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N).

One day, each of the mountains received a non-negative even number of liters of rain.

As a result, Dam i (1 \leq i \leq N) accumulated a total of A_i liters of water.

Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem.

Constraints

All values in input are integers.

3 \leq N \leq 10^5-1

N is an odd number.

0 \leq A_i \leq 10^9

The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order.

Sample Input 1

3
2 2 4

Sample Output 1

4 0 4

If we assume Mountain 1, 2, and 3 received 4, 0, and 4 liters of rain, respectively, it is consistent with this input, as follows:

Dam 1 should have accumulated \frac{4}{2} + \frac{0}{2} = 2 liters of water.

Dam 2 should have accumulated \frac{0}{2} + \frac{4}{2} = 2 liters of water.

Dam 3 should have accumulated \frac{4}{2} + \frac{4}{2} = 4 liters of water.

Sample Input 2

5
3 8 7 5 5

Sample Output 2

2 4 12 2 8

Sample Input 3

3
1000000000 1000000000 0

Sample Output 3

0 2000000000 0


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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	b = zeros(Int,n)
	b[1] = a[1]
	for i in 2:n
		if i%2==0
			b[i] = b[i-1]-a[i]
		else
			b[i] = b[i-1]+a[i]
		end
	end
	print(b[n]," ")
	for i in 2:n-1
		if i%2 == 0
			print(-(b[n]-2*b[i-1]), " ")
		else
			print(b[n]-2*b[i-1], " ")
		end
	end
	println(b[n]-2*b[n-1])
end

main()


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

Score : 600 points

Problem Statement

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

Answer the following Q queries:

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

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

1 \leq a_i, b_i \leq N

1 \leq c_i \leq N-1

1 \leq d_i \leq 10^4

1 \leq x_j \leq N-1

1 \leq y_j \leq 10^4

1 \leq u_j < v_j \leq N

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q

Output

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

Sample Input 1

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

Sample Output 1

130
200
60

The graph in this input is as follows:

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

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

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

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


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

Score : 100 points

Problem Statement

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

Constraints

The length of S is 4.

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

ASSA

Sample Output 1

Yes

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

Sample Input 2

STOP

Sample Output 2

No

Sample Input 3

FFEE

Sample Output 3

Yes

Sample Input 4

FREE

Sample Output 4

No


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

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

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

function dijkstra(e::Array{Array{Int,1},1},k,t)
	dis = -ones(Int, length(e))
	h = Array{Tuple{Int,Int},1}()
	push!(h,(k,0))
	while !isempty(h)
		v,w = hpop(h)
		dis[v] = dis[v]<0?w:min(dis[v],w)
		if v == t
			break
		end
		for i in 1:length(e[v])
			if dis[e[v][i]]<0　|| dis[e[v][i]]>w+1
				dis[e[v][i]]=w+1
				hpush(h,(e[v][i],w+1))
			end
		end
	end
	dis
end

function main()
	n,m = readline() |> split |> parseMap
	e = [Int[] for i in 1:3*n]
	for i in 1:m
		u,v = readline() |> split |> parseMap
		push!(e[u],n+v)
		push!(e[n+u],2*n+v)
		push!(e[2*n+u],v)
	end
	s,t = readline() |> split |> parseMap
	dis = dijkstra(e,s,t)
	println(dis[t]>=0?div(dis[t],3):-1)

end

main()

Score : 500 points

Problem Statement

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

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

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

Constraints

2 \leq N \leq 10^5

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

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

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

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

1 \leq S, T \leq N

S \neq T

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1
:
u_M v_M
S T

Output

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

Sample Input 1

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

Sample Output 1

2

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

Sample Input 2

3 3
1 2
2 3
3 1
1 2

Sample Output 2

-1

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

Sample Input 3

2 0
1 2

Sample Output 3

-1

Vertex S and Vertex T may be disconnected.

Sample Input 4

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

Sample Output 4

2


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

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

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

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

end

main()


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


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

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

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

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

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

end

main()


s=readline()
c=0
for i=[1:3]
  s[i]==s[i+1]?c+=1:c
end
print(c==0?"Good":"Bad")

Score : 100 points

Problem Statement

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

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

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

Constraints

S is a 4-character string consisting of digits.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

3776

Sample Output 1

Bad

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

Sample Input 2

8080

Sample Output 2

Good

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

Sample Input 3

1333

Sample Output 3

Bad

Sample Input 4

0024

Sample Output 4

Bad


n, l = map(x -> parse(Int,x), split(readline()))
a = sort([n for n in l:l+n-1], by=x->abs(x))
println(sum(a)-a[1])

Score : 200 points

Problem Statement

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

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

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

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

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

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

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

Sample Input 1

5 2

Sample Output 1

18

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

Sample Input 2

3 -1

Sample Output 2

0

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

Sample Input 3

30 -50

Sample Output 3

-1044


N,L=map(x->parse(Int,x),split(readline()))
out=0
if L<=0&&0<=L+N-1
  out=0
elseif L+N-1<0
  out=L+N-1
else
  out=L
end
println(div(N*(N-1),2)+L*N-out)


using DataStructures
const double = Float64
const int = Int64

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

function main()
  A,B,C,D = readint()

  E = B - A + 1

  divC = B÷C - (A-1)÷C
  divD = B÷D - (A-1)÷D

  cd = C*D÷gcd(C,D)
  divCD = B÷cd - (A-1)÷cd

  println(E-divC-divD + divCD)

end

main()

Score : 300 points

Problem Statement

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

Constraints

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

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

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

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368


a, b, c, d = map(x -> parse(Int64,x), split(readline()))
f(x) = x - div(x,c) - div(x,d) + div(x,lcm(c,d))
println(f(b)-f(a-1))


parseInt = x -> parse(Int, x)

function main()
    n = parseInt(readline())
    work = [map(parseInt, split(readline())) for i in 1:n]

    function cmp(a, b)
        if a[2] < b[2]
            true
        else
            false
        end
    end

    sort!(work, lt = cmp)

    passed = 0
    result = "Yes"
    for t in work
        passed += t[1]
        if(passed > t[2])
            result = "No"
            break
        end
    end

    println(result)
end

main()

Score: 400 points

Problem Statement

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

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

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

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

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

Constraints

All values in input are integers.

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

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

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

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

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

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

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

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

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

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

Sample Input 3

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

Sample Output 3

Yes


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

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

main()


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

function main()
	n = readline() |> parseInt
	a = Array{Int}(2,n)
	for i in 1:n
		a[:,i] = readline() |> split |> parseMap
	end
	a = sortcols(a, by=z->z[2])
	t = 0
	flg = 0
	for i in 1:n
		t += a[1,i]
		if t > a[2,i]
			flg = 1
			break
		end
	end
	println(flg==0?"Yes":"No")
end

main()


function main()
	lines=readlines()
	N=parse(Int,shift!(lines))
	T=10^5
	A=Array{Tuple{Int,Int}}(0)
	parent=collect(1:T)
	S=ones(Int,T)
	function find(u)
		if u==parent[u]
			u
		else
			parent[u]=find(parent[u])
		end
	end
	for i=1:N
		x,y=map(x->parse(Int,x),split(shift!(lines)))
		push!(A,(x,y))
	end
	sort!(A)
	px,py=-1,-1
	for (x,y)=A
		if px==x
			s=find(py)
			t=find(y)
			if S[s]>S[t]
				parent[t]=s
				S[s]+=S[t]
			else
				parent[s]=t
				S[t]+=S[s]
			end
		end
		px,py=x,y
	end
	ans=-N
	px,py=-1,-1
	for (x,y)=A
		if px!=x
			ans+=S[find(y)]
		end
		px,py=x,y
	end
	println(ans)
end
main()

Score : 600 points

Problem Statement

There are N dots in a two-dimensional plane. The coordinates of the i-th dot are (x_i, y_i).

We will repeat the following operation as long as possible:

Choose four integers a, b, c, d (a \neq c, b \neq d) such that there are dots at exactly three of the positions (a, b), (a, d), (c, b) and (c, d), and add a dot at the remaining position.

We can prove that we can only do this operation a finite number of times. Find the maximum number of times we can do the operation.

Constraints

1 \leq N \leq 10^5

1 \leq x_i, y_i \leq 10^5

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the maximum number of times we can do the operation.

Sample Input 1

3
1 1
5 1
5 5

Sample Output 1

1

By choosing a = 1, b = 1, c = 5, d = 5, we can add a dot at (1, 5). We cannot do the operation any more, so the maximum number of operations is 1.

Sample Input 2

2
10 10
20 20

Sample Output 2

0

There are only two dots, so we cannot do the operation at all.

Sample Input 3

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

Sample Output 3

16

We can do the operation for all choices of the form a = 1, b = 1, c = i, d = j (2 \leq i,j \leq 5), and no more. Thus, the maximum number of operations is 16.


function main()
    (x, a) = split(readline())

    if x < a
        println(0)
    else
        println(10)
    end
end

main()

Score : 100 points

Problem Statement

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

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

Constraints

0 \leq X, A \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X A

Output

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

Sample Input 1

3 5

Sample Output 1

0

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

Sample Input 2

7 5

Sample Output 2

10

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

Sample Input 3

6 6

Sample Output 3

10

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


x, a = parse.([Int], split(readline()))
if x < a
  println(0)
else
  println(10)
end


function main()

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

  D = zeros(Int,N+1)

  for i in 2:N+1
    D[i] = D[i-1] + L[i-1]
  end

  count = 0

  for i in 1:N+1
    if X >= D[i]
      count += 1
    end
  end

  println(count)

end

main()

Score : 200 points

Problem Statement

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

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

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 100

1 \leq X \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 6
3 4 5

Sample Output 1

2

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

Sample Input 2

4 9
3 3 3 3

Sample Output 2

4

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


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

function main()
    n, x = ints()
    l = ints()
    cnt = 1
    d = 0
    for i = 2:n
        d += l[i-1]
        (d <= x) && (cnt += 1)
    end
    println(cnt)
end

main()


parseInt(x) = parse(Int,x)

function main()
    n, x = map(parseInt, split(readline()))
    a = map(parseInt, split(readline()))

    ans = 0
    d = 0
    for i in 1:n
        d += a[i]
        ans += 1
        if d > x
            break
        end
    end
    println(ans)
end

main()


function main()
  n,m=parse.(split(readline()))
  if n==1
    print(1*Int(m==0))
    return 0
  end
  check=ones(n)
  mod=1000000007
  for i=1:m
    check[parse(Int,readline())]=0
  end
  dp=zeros(n+3)
  f(i::Int)=dp[i]*check[i]
  dp[1]=check[1]*1
  dp[2]=(dp[1]+1)*check[2]
  if n<3
    print(Int(dp[n]))
    return 0
  end
  for i=3:n
    dp[i]=check[i]*(dp[i-1]+dp[i-2])%mod
  end
  print(Int(dp[n]))
end
main()

Score : 300 points

Problem Statement

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

Constraints

1 \leq W,H \leq 10^9

0\leq x\leq W

0\leq y\leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

W H x y

Output

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

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

Sample Input 1

2 3 1 2

Sample Output 1

3.000000 0

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

Sample Input 2

2 2 1 1

Sample Output 2

2.000000 1


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


parseInt(x) = parse(Int, x)
function parseMap(x::String)
    x = split(x)
    map(parseInt, x)
end

function main()
    w, h, x, y = readline() |> parseMap
    面積 = w * h / 2
    ans = div(w, 2) == x && div(h, 2) == y ? 1 : 0
    println("$面積 $ans")

end

main()


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

Score : 500 points

Problem Statement

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

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

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

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

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

Constraints

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

The length of S is N.

The length of T is M.

1 \leq S_i, T_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

2 2
1 3
3 1

Sample Output 1

3

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

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

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

Sample Input 2

2 2
1 1
1 1

Sample Output 2

6

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

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

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

Sample Input 3

4 4
3 4 5 6
3 4 5 6

Sample Output 3

16

Sample Input 4

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

Sample Output 4

191

Sample Input 5

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

Sample Output 5

846527861

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


n,m=map(x->parse(Int,x),split(readline()))
s=map(x->parse(Int,x),split(readline()))
t=map(x->parse(Int,x),split(readline()))
dp=zeros(Int,n+1,m+1)
sum=zeros(Int,n+1,m+1)
mod=10^9+7
for j=1:m+1;sum[1,j]=1;end


parseInt(x) = parse(Int,x)

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

function main()
    mod = 10^9 + 7
    N, M = readline() |> split |> parseMap
    S = readline() |> split |> parseMap
    T = readline() |> split |> parseMap
    DP = [[1 for j in 1:N+1] for i in 1:M+1]
    for i in 1:M
        for j in 1:N
            if T[i]==S[j]
                DP[i+1][j+1] = (DP[i+1][j]+DP[i][j+1])%mod
            else
                DP[i+1][j+1] = (mod + DP[i+1][j]+DP[i][j+1]-DP[i][j])%mod
            end
        end
    end
    println(DP[M+1][N+1])
end

main()


parseInt(x) = parse(Int,x)

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

function main()
    mod = 10^9 + 7
    N, M = readline() |> split |> parseMap
    S = readline() |> split |> parseMap
    T = readline() |> split |> parseMap
    DP = [[1 for j in 1:N+1] for i in 1:M+1]
    for i in 1:M
        for j in 1:N
            if T[i]==S[j]
                DP[i+1][j+1] = (DP[i+1][j]+DP[i][j+1])%mod
            else
                DP[i+1][j+1] = (DP[i+1][j]+DP[i][j+1]-DP[i][j])%mod
            end
        end
    end
    print(DP[M+1][N+1])
end

main()


parseInt(x) = parse(Int,x)

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

function main()
    mod = 10**9 + 7
    N, M = readline() |> split |> parseMap
    S = readline() |> split |> parseMap
    T = readline() |> split |> parseMap
    DP = [[1 for j in 1:N+1] for i in 1:M+1]
    for i in 1:M
        for j in 1:N
            if T[i]==S[j]
                DP[i+1][j+1] = (DP[i+1][j]+DP[i+1][j])%mod
            else
                DP[i+1][j+1] = (DP[i+1][j]+DP[i+1][j]-DP[i][j])%mod
            end
        end
    end
    print(DP[M+1][N+1])
end

main()


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

function main()
	mod = 1000000007
	n,m = readline() |> split |> parseMap
	s = readline() |> split |> parseMap
	t = readline() |> split |> parseMap
	dp = zeros(Int,n+1,m+1)
	dp[1,:] = 1
	dp[:,1] = 1
	for i in 1:n
		dp[i+1,:] = dp[i,:]
		for j in 1:m
			if s[i] == t[j]
				dp[i+1,j+1] = (dp[i,j+1]+dp[i+1,j])%mod
			else
				dp[i+1,j+1] = (dp[i,j+1]+dp[i+1,j]-dp[i,j])%mod
			end
		end
	end
	println(dp[n+1,m+1])
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,s)
    if s<0
        10^16
    else
        (max(rm,rr+s,lr-s)-min(lm,ll-s,rl+s)+0.0)*(max(um,uu+s,du-s)-min(dm,dd-s,ud+s)+0.0)
    end
end

function main()
    n = readline() |> parseInt
    r = Tuple{Int,Int}[]
    l = Tuple{Int,Int}[]
    u = Tuple{Int,Int}[]
    d = Tuple{Int,Int}[]
    for i in 1:n
        x,y,w = readline() |> split
        if w=="R"
            push!(r,(parseInt(x),parseInt(y)))
        elseif w=="L"
            push!(l,(parseInt(x),parseInt(y)))
        elseif w=="U"
            push!(u,(parseInt(x),parseInt(y)))
        elseif w=="D"
            push!(d,(parseInt(x),parseInt(y)))
        end
    end
    rm = -10^8
    lm = 10^8
    um = -10^8
    dm = 10^8
    rr = -10^8
    rl = 10^8
    lr = -10^8
    ll = 10^8
    uu = -10^8
    ud = 10^8
    du = -10^8
    dd = 10^8
    for i in 1:length(r)
        um = max(um,r[i][2])
        dm = min(dm,r[i][2])
        rr = max(rr,r[i][1])
        rl = min(rl,r[i][1])
    end
    for i in 1:length(l)
        um = max(um,l[i][2])
        dm = min(dm,l[i][2])
        lr = max(lr,l[i][1])
        ll = min(ll,l[i][1])
    end
    for i in 1:length(u)
        rm = max(rm,u[i][1])
        lm = min(lm,u[i][1])
        uu = max(uu,u[i][2])
        ud = min(ud,u[i][2])
    end
    for i in 1:length(d)
        rm = max(rm,d[i][1])
        lm = min(lm,d[i][1])
        du = max(du,d[i][2])
        dd = min(dd,d[i][2])
    end
    println(min(f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,(lr-rr)/2),f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,rm-rr),f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,lr-rm),f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,(ll-rl)/2),f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,ll-lm),f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,lm-rl),f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,(du-uu)/2),f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,um-uu),f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,du-um),f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,(dd-ud)/2),f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,dd-dm),f(rm,lm,um,dm,rr,rl,lr,ll,uu,ud,du,dd,dm-ud)))
end

main()

Score : 600 points

Problem Statement

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

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

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

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

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

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

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

Constraints

1 \leq N \leq 10^5

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

x_i and y_i are integers.

d_i is R, L, U, or D.

Input

Input is given from Standard Input in the following format:

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

Output

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

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

Sample Input 1

2
0 3 D
3 0 L

Sample Output 1

0

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

Sample Input 2

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

Sample Output 2

97.5

The answer may not be an integer.

Sample Input 3

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

Sample Output 3

273


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


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,k=parseMap(split(readline()))
    println(n%k)
end
main()

Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

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

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

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

Sample Input 1

3 2

Sample Output 1

1

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

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

Sample Input 2

3 1

Sample Output 2

0

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

Sample Input 3

8 5

Sample Output 3

3

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


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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	a = sort(a)
	m = 0
	l = n
	if a[n] < 0
		println(a[n]-sum(m[1:n-1]))
		for i in 1:n-1
			x = shift!(a)
			println(a[l-1], " ",x)
			a[l-1] -= x
		end
	elseif a[1] > 0
		println(sum(m[2:n-1])-a[1])
		for i in 1:n-2
			x = pop!(a)
			println(a[1], " ",x)
			a[1] -= x
		end
		println(a[2], " ", a[1])
	else
		for i in 1:n
			m += abs(a[i])
		end
		println(m)
		p = shift!(a)
		q = pop!(a)
		for i in 1:n-2
			x = pop!(a)
			if x<0
				println(q," ",x)
				q -= x
			else
				println(p, " ",x)
				p -= x
			end
		end
		println(q, " ", p)
	end
end

main()

Score : 500 points

Problem Statement

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

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

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

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

Constraints

2 \leq N \leq 10^5

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

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

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

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

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

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

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

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

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0


if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
pI(x)=parse(Int,x)
pM(x::Array{SubString{String},1})=map(pI,x)
function main()
  z=pM(split(readline()))
  print(sum(z)-maximum(z))
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Score : 100 points

Problem Statement

There are three airports A, B and C, and flights between each pair of airports in both directions.

A one-way flight between airports A and B takes P hours, a one-way flight between airports B and C takes Q hours, and a one-way flight between airports C and A takes R hours.

Consider a route where we start at one of the airports, fly to another airport and then fly to the other airport.

What is the minimum possible sum of the flight times?

Constraints

1 \leq P,Q,R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

P Q R

Output

Print the minimum possible sum of the flight times.

Sample Input 1

1 3 4

Sample Output 1

4

The sum of the flight times in the route A \rightarrow B \rightarrow C: 1 + 3 = 4 hours

The sum of the flight times in the route A \rightarrow C \rightarrow C: 4 + 3 = 7 hours

The sum of the flight times in the route B \rightarrow A \rightarrow C: 1 + 4 = 5 hours

The sum of the flight times in the route B \rightarrow C \rightarrow A: 3 + 4 = 7 hours

The sum of the flight times in the route C \rightarrow A \rightarrow B: 4 + 1 = 5 hours

The sum of the flight times in the route C \rightarrow B \rightarrow A: 3 + 1 = 4 hours

The minimum of these is 4 hours.

Sample Input 2

3 2 3

Sample Output 2

5


function main()

  (P,Q,R) = map(x -> parse(Int,x), split(readline()))

  println(min(P+Q,R+Q,P+R))

end

main()


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

function main()
  N,M= readline() |> split |> parseArray
  a = zeros(Int, N)
  a[1] = 1
  a[2] = 2
  for _ in 1:M
    i = readline() |> parseInt
    a[i] = -1
  end

  a[2]!=-1 && a[1]==-1 && (a[2]=1)

  for i in 3:N
    a[i]==-1 && continue
    a[i-2] != -1 && (a[i]+=a[i-2])
    a[i-1] != -1 && (a[i]+=a[i-1])
  end

  println(a[N]%1_000_000_007)
end

main()

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.
He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?
Find the count modulo 1\ 000\ 000\ 007.

Constraints

1 \leq N \leq 10^5

0 \leq M \leq N-1

1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
.
.
.
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

0 \to 1 \to 2 \to 4 \to 5 \to 6

0 \to 1 \to 2 \to 4 \to 6

0 \to 2 \to 4 \to 5 \to 6

0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.

Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

Be sure to print the count modulo 1\ 000\ 000\ 007.


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()
    h::Int,w::Int=parseMap(split(readline()))
    s=Vector{String}([readline() for i in 1:h])
    cnt=Vector{Vector{Int}}([[0 for i in 1:w] for j in 1:h])
    res::Int=0
    for i in 1:h
        res=0
        for j in 1:w
            if s[i][j]=='#'
                res=0
            else
                cnt[i][j]+=res
                res+=1
            end
        end
        res=0
        for j in w:-1:1
            if s[i][j]=='#'
                res=0
            else
                cnt[i][j]+=res
                res+=1
            end
        end
    end
    for i in 1:w
        res=0
        for j in 1:h
            if s[j][i]=='#'
                res=0
            else
                cnt[j][i]+=res
                res+=1
            end
        end
        res=0
        for j in h:-1:1
            if s[j][i]=='#'
                res=0
            else
                cnt[j][i]+=res
                res+=1
            end
        end
    end
    ans::Int=0
    for i in 1:h
        for j in 1:w
            if s[i][j]=='.'
                cnt[i][j]+=1
            end
            ans=max(ans,cnt[i][j])
        end
    end
    println(ans)
end
main()

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13


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()
    h,w=parseMap(split(readline()))
    s=[readline() for i in 1:h]
    cnt=[[0 for i in 1:w] for j in 1:h]
    for i in 1:h
        for r in [1:w,w:-1:1]
            res=0
            for j in r
                if s[i][j]=='#'
                    res=0
                else
                    cnt[i][j]+=res
                    res+=1
                end
            end
        end
    end
    for i in 1:w
        for r in [1:h,h:-1:1]
            res=0
            for j in r
                if s[j][i]=='#'
                    res=0
                else
                    cnt[j][i]+=res
                    res+=1
                end
            end
        end
    end
    ans=0
    for i in 1:h
        for j in 1:w
            if s[i][j]=='.'
                cnt[i][j]+=1
            end
            ans=max(ans,cnt[i][j])
        end
    end
    println(ans)
end
main()


function main()
	H,W=map(x->parse(Int,x),split(readline()))
	S=Array{Int}(H,W)
	for i=1:H
		s=readline()
		for j=1:W
			S[i,j]=s[j]=='.'
		end
	end
	dp1=copy(S)
	dp2=copy(S)
	dp3=copy(S)
	dp4=copy(S)
	for x=2:H
		dp1[x,:].*=dp1[x-1,:]+1
	end
	for x=H-1:-1:1
		dp2[x,:].*=dp2[x+1,:]+1
	end
	for y=2:W
		dp3[:,y].*=dp3[:,y-1]+1
	end
	for y=W-1:-1:1
		dp4[:,y].*=dp4[:,y+1]+1
	end
	println(maximum(dp1+dp2+dp3+dp4)-3)
end
main()


function main()
	H,W=map(x->parse(Int,x),split(readline()))
	S=Array{Int}(0,W)
	for i=1:H
		S=vcat(S,map(c->c=='.' ? 1 : 0,collect(chomp(readline())))')
	end
	dp1=Array{Int}(0,W)
	now=zeros(Int,W)
	for i=1:H
		now=(now.+1).*S[i,:]
		dp1=vcat(dp1,now')
	end
	dp2=Array{Int}(0,W)
	now=zeros(Int,W)
	for i=H:-1:1
		now=(now.+1).*S[i,:]
		dp2=vcat(dp2,now')
	end
	dp2=reverse(dp2,dims=1)
	dp3=Array{Int}(0,H)
	now=zeros(Int,H)
	S=S'
	for i=1:W
		now=(now.+1).*S[i,:]
		dp3=vcat(dp3,now')
	end
	dp4=Array{Int}(0,H)
	now=zeros(Int,H)
	for i=W:-1:1
		now=(now.+1).*S[i,:]
		dp4=vcat(dp4,now')
	end
	dp4=reverse(dp4,dims=1)
	ans=0
	for i=1:H,j=1:W
		ans=max(ans,dp1[i,j]+dp2[i,j]+dp3[j,i]+dp4[j,i]-3)
	end
	println(ans)
end
main()


const lines=readlines(open("/dev/fd/0"))
input()=shift!(lines)
int(s::String)=parse(Int32,s)
intSub(s::SubString{String})=parse(Int32,s)
intLine()=map(intSub,split(input()))
function main()
    h,w=intLine()
    s=Array{Int32,1}[[(t>'#')for t in input()]for i in 1:h]
    a=deepcopy(s)
    b=deepcopy(s)
    c=deepcopy(s)
    d=deepcopy(s)
    for i in 1:h
        for j in 1:w
            if j>1
                a[i][j]=-~a[i][j-1]*s[i][j]
                b[i][w-j+1]=-~b[i][w-j+2]*s[i][w-j+1]
            end
            if i>1
                c[i][j]=-~c[i-1][j]*s[i][j]
                d[h-i+1][j]=-~d[h-i+2][j]*s[h-i+1][j]
            end
        end
    end
    println(maximum(maximum(a[i]+b[i]+c[i]+d[i])for i in 1:h)-3)
end
main()


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

function main()
    n,a,b,c,d=parseMap(split(readline()))
    s=chomp(readline())
    ans="Yes"
    for i in a:c-1
        if s[i:i+1]=="##"
            ans="No"
        end
    end
    for i in b:d-1
        if s[i:i+1]=="##"
            ans="No"
        end
    end
    if ans=="Yes" && c>d
        flg=false
        for i in a:d-2
            if s[i:i+2]=="..."
                flg=true
            end
        end
        if !flg
            ans="No"
        end
    end
    println(ans)
end
main()

Score : 400 points

Problem Statement

There are N squares arranged in a row, numbered 1, 2, ..., N from left to right.
You are given a string S of length N consisting of . and #. If the i-th character of S is #, Square i contains a rock; if the i-th character of S is ., Square i is empty.

In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.

You can repeat the following operation any number of times:

Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.

You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.

Determine whether this is possible.

Constraints

4 \leq N \leq 200\ 000

S is a string of length N consisting of . and #.

1 \leq A, B, C, D \leq N

Square A, B, C and D do not contain a rock.

A, B, C and D are all different.

A < B

A < C

B < D

Input

Input is given from Standard Input in the following format:

N A B C D
S

Output

Print Yes if the objective is achievable, and No if it is not.

Sample Input 1

7 1 3 6 7
.#..#..

Sample Output 1

Yes

The objective is achievable by, for example, moving the two persons as follows. (A and B represent Snuke and Fnuke, respectively.)

A#B.#..

A#.B#..

.#AB#..

.#A.#B.

.#.A#B.

.#.A#.B

.#..#AB

Sample Input 2

7 1 3 7 6
.#..#..

Sample Output 2

No

Sample Input 3

15 1 3 15 13
...#.#...#.#...

Sample Output 3

Yes


N,A,B,C,D=map(x->parse(Int,x),split(readline()))
S=readline()
for i=A:C-1
	if S[i]=='#'&&S[i+1]=='#'
		println("No")
		exit()
	end
end
for i=B:D-1
	if S[i]=='#'&&S[i+1]=='#'
		println("No")
		exit()
	end
end
if C<D
	println("Yes")
else
	for j=B:D-2
		if S[j]=='.'&&S[j+1]=='.'&&S[j+2]=='.'
			println("Yes")
			exit()
		end
	end
	println("No")
end


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]>0;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[l]+=uf[r];uf[r]=l;elseif l>r;uf[r]+=uf[l];uf[l]=r;end;end

function main()
	n,x = readline() |> split |> parseMap
	e = Tuple{Int,Int,Int,Int}[(0,0,0,0) for i in 1:n]
	for i in 1:n
		b,l,u = readline() |> split |> parseMap
		e[i] = (b,l,u,(x-b)*u)
	end
	e = sort(e,by=x->x[4],rev=true)
	d = zeros(Int,n)
	d[1] = e[1][1]*e[1][2]
	for i in 2:n
		d[i] = d[i-1]+e[i][1]*e[i][2]
	end
	u = zeros(Int,n)
	u[1] = x*e[1][3]
	for i in 2:n
		u[i] = u[i-1]+x*e[i][3]
	end
	m = d[n]
	k = 0
	ans = 0
	if d[n]>0
		for i in 1:n
			d[n] += e[i][1]*(e[i][3]-e[i][2])
			if u[i]>=d[n]
				k = i
				if i>1
					m = d[n]-u[i-1]-e[i][1]*(e[i][3]-e[i][2])
				end
				break
			end
		end
	end
	mx = (0,0)
	for i in k:n
		if e[i][2]>mx[2]
			mx = (i,e[i][2])
		end
	end
	num = x
	for i in k:n
		if mx[2]>=e[i][3]
			num = min(num,ceil(Int,m/e[mx[1]][2]))
		else
			num = min(num,ceil(Int,(m+e[i][1]*(e[i][3]-e[i][2]))/e[i][3]))
		end
	end
	println(x*(k-1)+num)
end

main()

Score : 800 points

Problem Statement

Takahashi and Aoki will take N exams numbered 1 to N.
They have decided to compete in these exams.
The winner will be determined as follows:

For each exam i, Takahashi decides its importance c_i, which must be an integer between l_i and u_i (inclusive).

Let A be \sum_{i=1}^{N} c_i \times (Takahashi's score on Exam i), and B be \sum_{i=1}^{N} c_i \times (Aoki's score on Exam i). Takahashi wins if A \geq B, and Aoki wins if A < B.

Takahashi knows that Aoki will score b_i on Exam i, with his supernatural power.

Takahashi himself, on the other hand, will score 0 on all the exams without studying more. For each hour of study, he can increase his score on some exam by 1. (He can only study for an integer number of hours.)
However, he cannot score more than X on an exam, since the perfect score for all the exams is X.

Print the minimum number of study hours required for Takahashi to win.

Constraints

1 \leq N \leq 10^5

1 \leq X \leq 10^5

0 \leq b_i \leq X (1 \leq i \leq N)

1 \leq l_i \leq u_i \leq 10^5 (1 \leq i \leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
b_1 l_1 u_1
b_2 l_2 u_2
:
b_N l_N u_N

Output

Print the minimum number of study hours required for Takahashi to win.

Sample Input 1

2 100
85 2 3
60 1 1

Sample Output 1

115

One optimal strategy is as follows:

Choose c_1 = 3, c_2 = 1.

Study to score 100 on Exam 1 and 15 on Exam 2.

Then, A = 3 \times 100 + 1 \times 15 = 315, B = 3 \times 85 + 1 \times 60 = 315 and Takahashi will win.

Sample Input 2

2 100
85 2 3
60 10 10

Sample Output 2

77

Sample Input 3

1 100000
31415 2718 2818

Sample Output 3

31415

Sample Input 4

10 1000
451 4593 6263
324 310 6991
378 1431 7068
71 1757 9218
204 3676 4328
840 6221 9080
684 1545 8511
709 5467 8674
862 6504 9835
283 4965 9980

Sample Output 4

2540


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())
    println((n-2)*180)
end
main()

Score : 100 points

Problem Statement

Given an integer N not less than 3, find the sum of the interior angles of a regular polygon with N sides.

Print the answer in degrees, but do not print units.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print an integer representing the sum of the interior angles of a regular polygon with N sides.

Sample Input 1

3

Sample Output 1

180

The sum of the interior angles of a regular triangle is 180 degrees.

Sample Input 2

100

Sample Output 2

17640


S=chomp(readline())
T=15-length(S)
for c=S
  T+=c=='o'
end
println(T<8 ? "NO" : "YES")

Score : 200 points

Problem Statement

Takahashi is competing in a sumo tournament.
The tournament lasts for 15 days, during which he performs in one match per day.
If he wins 8 or more matches, he can also participate in the next tournament.

The matches for the first k days have finished.
You are given the results of Takahashi's matches as a string S consisting of o and x.
If the i-th character in S is o, it means that Takahashi won the match on the i-th day; if that character is x, it means that Takahashi lost the match on the i-th day.

Print YES if there is a possibility that Takahashi can participate in the next tournament, and print NO if there is no such possibility.

Constraints

1 \leq k \leq 15

S is a string of length k consisting of o and x.

Input

Input is given from Standard Input in the following format:

S

Output

Print YES if there is a possibility that Takahashi can participate in the next tournament, and print NO otherwise.

Sample Input 1

oxoxoxoxoxoxox

Sample Output 1

YES

Takahashi has 7 wins and 7 losses before the last match. If he wins that match, he will have 8 wins.

Sample Input 2

xxxxxxxx

Sample Output 2

NO


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

function main()
  A, P = readline() |> split |> parseArray

  println(div(3A+P,2))

end

main()

Score : 100 points

Problem Statement

We have A apples and P pieces of apple.

We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.

Find the maximum number of apple pies we can make with what we have now.

Constraints

All values in input are integers.

0 \leq A, P \leq 100

Input

Input is given from Standard Input in the following format:

A P

Output

Print the maximum number of apple pies we can make with what we have.

Sample Input 1

1 3

Sample Output 1

3

We can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple.

Sample Input 2

0 1

Sample Output 2

0

We cannot make an apple pie in this case, unfortunately.

Sample Input 3

32 21

Sample Output 3

58


N=parse(Int,readline())
S=Array{String}(0)
T=Array{Int}(0)
for i=1:N
	s,t=split(readline())
	push!(S,String(s))
	push!(T,parse(Int,t))
end
id=1:N
for i=sort(id,lt=(a,b)->S[a]<S[b]||S[a]==S[b]&&T[a]>T[b])
	println(i)
end

Score : 200 points

Problem Statement

You have decided to write a book introducing good restaurants.
There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i.
No two restaurants have the same score.

You want to introduce the restaurants in the following order:

The restaurants are arranged in lexicographical order of the names of their cities.

If there are multiple restaurants in the same city, they are arranged in descending order of score.

Print the identification numbers of the restaurants in the order they are introduced in the book.

Constraints

1 ≤ N ≤ 100

S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

0 ≤ P_i ≤ 100

P_i is an integer.

P_i ≠ P_j (1 ≤ i < j ≤ N)

Input

Input is given from Standard Input in the following format:

N
S_1 P_1
:
S_N P_N

Output

Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book.

Sample Input 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

Sample Output 1

3
4
6
1
5
2

The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.

Sample Input 2

10
yakutsk 10
yakutsk 20
yakutsk 30
yakutsk 40
yakutsk 50
yakutsk 60
yakutsk 70
yakutsk 80
yakutsk 90
yakutsk 100

Sample Output 2

10
9
8
7
6
5
4
3
2
1


N=parse(Int,readline())
S=Array{String}(0)
T=Array{Int}(0)
for i=1:N
	s,t=split(readline())
	push!(S,String(s))
	push!(T,parse(Int,t))
end
id=1:N
for i=sort(id,lt=(a,b)->S[a]<S[b]?true:S[a]==S[b]&&T[a]>T[b])
	println(i)
end


let
  N = parse(Int, readline())
  restToScore = []
  for i=1:N
    R, S = map(x -> x, split(readline()))
    push!(restToScore, (i, R, parse(Int, S)))
  end
  sort!(restToScore, by=x->(x[2], -x[3]))
  for i=1:N println(restToScore[i][1]) end
end