parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function heap(a::Array{Int,1})
	h = [a[1]]
	for i in 2:length(a)
		hpush(h,a[i])
	end
	h
end

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

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

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

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

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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	b = heap(a[1:n])
	c = heapl(a[2n+1:3n])
	l = zeros(Int,n+1)
	r = zeros(Int,n+1)
	l[1] = sum(b)
	r[n+1] = sum(c)
	for i in 1:n
		if b[1] < a[n+i]
			p=hpop(b)
			hpush(b,a[n+i])
			l[i+1] = l[i]-p+a[n+i]
		else
			l[i+1] = l[i]
		end
		if c[1] > a[2n+1-i]
			q = hpopl(c)
			hpushl(c,a[2n+1-i])
			r[n+1-i] = r[n+2-i]-q+a[2n+1-i]
		else
			r[n+1-i] = r[n+2-i]
		end
	end
	s = -10^18
	for i in 1:n+1
		s = max(s,l[i]-r[i])
	end
	println(s)
end

main()

Score : 500 points

Problem Statement

Let N be a positive integer.

There is a numerical sequence of length 3N, a = (a_1, a_2, ..., a_{3N}).
Snuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements.
Here, the score of a' is defined as follows: (the sum of the elements in the first half of a') - (the sum of the elements in the second half of a').

Find the maximum possible score of a'.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Partial Score

In the test set worth 300 points, N ≤ 1000.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

When a_2 and a_6 are removed, a' will be (3, 4, 1, 5), which has a score of (3 + 4) - (1 + 5) = 1.

Sample Input 2

1
1 2 3

Sample Output 2

-1

For example, when a_1 are removed, a' will be (2, 3), which has a score of 2 - 3 = -1.

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

For example, when a_2, a_3 and a_9 are removed, a' will be (8, 7, 4, 6, 5, 3), which has a score of (8 + 7 + 4) - (6 + 5 + 3) = 5.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function heap(a::Array{Int,1})
	h = [a[1]]
	for i in 2:length(a)
		hpush(h,a[i])
	end
	h
end

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

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

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

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

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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	b = heap(a[1:n])
	c = heapl(a[2n+1:3n])
	l = zeros(Int,n+1)
	r = zeros(Int,n+1)
	l[1] = sum(b)
	r[n+1] = sum(c)
	for i in 1:n
		if b[1] < a[n+i]
			p=hpop(b)
			hpush(b,a[n+i])
			l[i+1] = l[i]-p+a[n+i]
		else
			l[i+1] = l[i]
		end
		if c[1] > a[2n+1-i]
			q = hpopl(c)
			hpushl(c,a[2n+1-i])
			r[n+1-i] = r[n+2-i]-q+a[2n+1-i]
		else
			r[n+1-i] = r[n+2-i]
		end
	end
	s = -10^16
	for i in 1:n+1
		s = max(s,l[i]-r[i])
	end
	println(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.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function heap(a::Array{Int,1})
	h = [a[1]]
	for i in 2:length(a)
		hpush(h,a[i])
	end
	h
end

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

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

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

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

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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	b = heap(a[1:n])
	c = heapl(a[2n+1:3n])
	l = zeros(Int,n+1)
	r = zeros(Int,n+1)
	l[1] = sum(b)
	r[n+1] = sum(c)
	for i in 1:n
		if b[1] < a[n+i]
			p=hpop(b)
			hpush(b,a[n+i])
			l[i+1] = l[i]-p+a[n+i]
		else
			l[i+1] = l[i]
		end
		if c[1] > a[2n+1-i]
			q = hpopl(c)
			hpushl(c,a[2n+1-i])
			r[n+1-i] = r[n+2-i]-q+a[2n+1-i]
		else
			r[n+1-i] = r[n+2-i]
		end
	end
	s = 0
	for i in 1:n+1
		s = max(s,l[i]-r[i])
	end
	println(s)
end

main()


using Base.Collections
N=parse(Int,readline())
A=map(x->parse(Int,x),split(readline()))
ans=-1145141919810931893
L=PriorityQueue(Int,Int)
now=0
for i=1:N
	now+=A[i]
	L[i]=A[i]
end
Lsum=Int[now]
for i=N+1:2N
	now+=A[i]
	L[i]=A[i]
	now-=peek(L).second
	dequeue!(L)
	push!(Lsum,now)
end
L=PriorityQueue(Int,Int)
now=0
for i=3N:-1:2N+1
	now+=A[i]
	L[i]=-A[i]
end
Rsum=Int[now]
for i=2N:-1:N+1
	now+=A[i]
	L[i]=-A[i]
	now+=peek(L).second
	dequeue!(L)
	push!(Rsum,now)
end
reverse!(Rsum)
for i=1:N+1
	ans=max(ans,Lsum[i]-Rsum[i])
end
println(ans)


using Base.Collections
N=parse(Int,readline())
A=map(x->parse(Int,x),split(readline()))
ans=-1145141919810931893
L=PriorityQueue(Int,Int)
now=0
for i=1:N
	now+=A[i]
	L[i]=A[i]
end
Lsum=Int[now]
for i=N+1:2N
	now+=A[i]
	L[i]=A[i]
	now-=peek(L).second
	dequeue!(L)
	push!(Lsum,now)
end
empty!(L)
now=0
for i=3N:-1:2N+1
	now+=A[i]
	L[i]=-A[i]
end
Rsum=Int[now]
for i=2N:-1:N+1
	now+=A[i]
	L[i]=-A[i]
	now+=peek(L).second
	dequeue!(L)
	push!(Rsum,now)
end
reverse!(Rsum)
for i=1:N+1
	ans=max(ans,Lsum[i]-Rsum[i])
end
println(ans)


Score : 200 points

Problem Statement

Snuke has decided to construct a string that starts with A and ends with Z, by taking out a substring of a string s (that is, a consecutive part of s).

Find the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of s that starts with A and ends with Z.

Constraints

1 ≦ |s| ≦ 200{,}000

s consists of uppercase English letters.

There exists a substring of s that starts with A and ends with Z.

Input

The input is given from Standard Input in the following format:

s

Output

Print the answer.

Sample Input 1

QWERTYASDFZXCV

Sample Output 1

5

By taking out the seventh through eleventh characters, it is possible to construct ASDFZ, which starts with A and ends with Z.

Sample Input 2

ZABCZ

Sample Output 2

4

Sample Input 3

HASFJGHOGAKZZFEGA

Sample Output 3

12


s = readline()
A = search(s, "A")[1]
Z = search(reverse(s), "Z")[1]
print(length(s) - Z - A + 2)


s = chomp(readline())
list_A = []
list_Z = []
for i in 1:length(s)
    if string(s[i]) == "A"
        list_A = push!(list_A,i)
    end
    if string(s[i]) == "Z"
        list_Z = push!(list_Z,i)
    end
end
println(list_Z[end]-list_A[1]+1)


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

function main()
    k,s=parseMap(split(readline()))
    ans=0
    for i in 0:k
        for j in 0:k
            if 0<=s-(i+j)<=k
                ans+=1
            end
        end
    end
    println(ans)
end
main()

Score : 200 points

Problem Statement

You are given two integers K and S.

Three variable X, Y and Z takes integer values satisfying 0≤X,Y,Z≤K.

How many different assignments of values to X, Y and Z are there such that X + Y + Z = S?

Constraints

2≤K≤2500

0≤S≤3K

K and S are integers.

Input

The input is given from Standard Input in the following format:

K S

Output

Print the number of the triples of X, Y and Z that satisfy the condition.

Sample Input 1

2 2

Sample Output 1

6

There are six triples of X, Y and Z that satisfy the condition:

X = 0, Y = 0, Z = 2

X = 0, Y = 2, Z = 0

X = 2, Y = 0, Z = 0

X = 0, Y = 1, Z = 1

X = 1, Y = 0, Z = 1

X = 1, Y = 1, Z = 0

Sample Input 2

5 15

Sample Output 2

1

The maximum value of X + Y + Z is 15, achieved by one triple of X, Y and Z.


function main(K,S)

  count = 0

  for i in 0:K, j in 0:K

    if 0 <= S - (i+j) <= K

      count += 1

    end

  end

  println(count)

end

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


function main()

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

  count = 0

  for i in 0:K, j in 0:K

    if 0 <= S - (i+j) <= K

      count += 1

    end

  end

  println(count)

end

main()


parseInt(x) = parse(Int,x)

function main()
  (K,S) = map(parseInt,split(readline()))
  c = 0
  for X = 0:K
    for Y = X:K
      for Z = Y:K
        if X+Y+Z == S
          if X == Z
            c += 1
            elseif Y == Z || X == Y
            c += 3
          else
            c += 6
          end
        end
      end
    end
  end
  println(c)
end

main()


intLine()=map(x->parse(Int,x),split(readline()))
function main()
  k,s=intLine()
  a=0
  for x=0:k,y=0:k
    if k>=s-x-y>=0
      a+=1
    end
  end
  show(a)
end
main()


function solve(k,s)
  a=0
  for x=0:k,y=0:k,z=0:k
    if x+y+z==s
      a+=1
    end
  end
  a
end
function main()
  k,s=map(x->parse(Int,x),split(readline()))
  show(solve(k,s))
end
main()


function solve(k,s)
  a=0
  for x=0:k,y=0:k
    if k>=s-x-y>=0
      a+=1
    end
  end
  a
end
function main()
  k,s=map(x->parse(Int,x),split(readline()))
  show(solve(k,s))
end
main()


function main()
  k,s=map(x->parse(Int,x),split(readline()))
  a=0
  for x=0:k,y=0:k
    if k>=s-x-y>=0
      a+=1
    end
  end
  show(a)
end
main()


function main()
  k,s=parse.(split(readline()))
  a=0
  for x=0:k,y=0:k
    if k>=s-x-y>=0
      a+=1
    end
  end
  show(a)
end
main()


function main()
  t=split(readline())
  k,s=parse(Int,t[1]),parse(Int,t[2])
  a=0
  for x=0:k,y=0:k,z=0:k
    if x+y+z==s
      a+=1
    end
  end
  show(a)
end
main()


parseInt(x) = parse(Int,x)

function main()
	k,s = map(parseInt, split(readline()))
	if s <= k
		println(div((s+1)*(s+2),2))
	elseif s <= 2*k
		println((k+1)*(k+1)-div((s-k)*(s-k+1),2)-div((2*k-s)*(2*k-s+1),2))
	else
		println(div((3*k-s+1)*(3*k-s+2),2))
	end
end

main()


k,s=map(x->parse(Int,x),split(readline()))
c=0
for i=0:k
	t=s-i
	if 0<=t<=k
		c+=t+1
	elseif k<t<=2*k
		c+=2*k-t+1
	end
end
print(c)


N = split(readline())
K,S=parse(Int,N[1]),parse(Int,N[2])
count = 0
for X in 0:K
    for Y in 0:K
        Z = S - X - Y
        if 0 <= Z <= K
            count += 1
        end
    end
end
println(count)


N = split(readline())
K,S=parse(Int,N[1]),parse(Int,N[2])
count = 0
for X in 0:K
    for Y in 0:K
        for Z in 0:K
            if (X + Y + Z) == S
                count += 1
            end
        end
    end
end
println(count)


function main()
	str = chomp(readline())
	str = reverse(str)
	strArr = ["dream", "dreamer", "erase", "eraser"]
	strArr = map(reverse, strArr)

	while(true)
		cnt = 0
		for s in strArr
			if startswith(str, s)
				str = str[length(s)+1:end]
				cnt += 1
			end
		end

		if cnt < 1
			break
		end
	end

	print(str == "" ? "YES" : "NO" )
end

main()

Score : 300 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Another string T is initially empty.
Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:

Append one of the following at the end of T: dream, dreamer, erase and eraser.

Constraints

1≦|S|≦10^5

S consists of lowercase English letters.

Input

The input is given from Standard Input in the following format:

S

Output

If it is possible to obtain S = T, print YES. Otherwise, print NO.

Sample Input 1

erasedream

Sample Output 1

YES

Append erase and dream at the end of T in this order, to obtain S = T.

Sample Input 2

dreameraser

Sample Output 2

YES

Append dream and eraser at the end of T in this order, to obtain S = T.

Sample Input 3

dreamerer

Sample Output 3

NO


function main()
	str = chomp(readline())
	str = reverse(str)
	strArr = ["dream", "dreamer", "erase", "eraser"]
	strArr = map(reverse, strArr)

	while(true)
		cnt = 0
		for s in strArr
			if startswith(str, s)
				str = str[length(s)+1:end]
				cnt += 1
			end
		end

		if cnt < 1
			break
		end
	end

	print(str == "" ? "Yes" : "No" )
end

main()


function main()

  S_0 = split(chomp(readline()),"")
  reverse!(S_0)
  S = join(S_0)

  check = 0
  ans = true
  N = length(S)
  for i in 1:N

    if i <= check
      continue
    end

    if i+6 <= N && S[i:i+6] == "remaerd"
      check = i+6
    elseif i+5 <= N && S[i:i+5] == "resare"
      check = i+5
    elseif i+4 <= N && S[i:i+4] == "maerd"
      check = i+4
    elseif i+4 <= N && S[i:i+4] == "esare"
      check = i+4
    else
      ans = false
    end

  end

  if ans
    println("YES")
  else
    println("NO")
  end

end

main()


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

function main()
    s=chomp(readline())
    i=length(s)
    ans="YES"
    while i>0
        if i<=4
            ans="NO"
            break
        elseif s[i-4:i]=="dream" || s[i-4:i]=="erase"
            i-=5
        elseif i<=5
            ans="NO"
            break
        elseif s[i-5:i]=="eraser"
            i-=6
        elseif i<=6
            ans="NO"
            break
        elseif s[i-6:i]=="dreamer"
            i-=7
        else
            ans="NO"
            break
        end
    end
    println(ans)
end
main()


S=reverse(chomp(readline()))
N=length(S)
id=1
while id<=N
	if id+4<=N&&S[id:id+4]=="maerd"
		id+=5
	elseif id+6<=N&&S[id:id+6]=="remaerd"
		id+=7
	elseif id+4<=N&&S[id:id+4]=="esare"
		id+=5
	elseif id+5<=N&&S[id:id+5]=="resare"
		id+=6
	else
		println("NO")
		exit()
	end
end
println("YES")


drm(x) = x=="dream"?true:false
drme(x) = x=="dreamer"?true:false
ers(x) = x=="erase"?true:false
erse(x) = x=="eraser"?true:false

function main()
	x = chomp(readline())
	t = length(x)
	flg = 0
	while t > 0
		if t <= 4
			flg = 1
			break
		elseif drm(x[t-4:t]) || ers(x[t-4:t])
			t -= 5
		elseif t <= 5
			flg = 1
			break
		elseif erse(x[t-5:t])
			t -= 6
		elseif t <= 6
			flg = 1
			break
		elseif drme(x[t-6:t])
			t -= 7
		else
			flg = 1
			break
		end
	end
	if flg == 1
		print("NO")
	else
		print("YES")
	end
end

main()


function main()
    divide = ["dream", "dreamer", "erase", "eraser"]

    S = chomp(readline())

    ans = true
    while length(S) > 0
        is_contains = false
        for i = 1:4
            if length(divide[i]) <= length(S)
                if S[end - length(divide[i]) + 1:end] == divide[i]
                    is_contains = true
                    S = S[1:end - length(divide[i])]
                end
            end
        end

        if !is_contains
            ans = false
            break
        end
    end

    if ans
        println("YES")
    else
        println("NO")
    end
end

main()


function main()
    divide = ["dream", "dreamer", "erase", "eraser"]

    S = chomp(readline())

    ans = true
    lenS = length(S)
    while lenS > 0
        is_contains = false
        for i = 1:4
            if length(divide[i]) <= lenS
                if S[lenS - length(divide[i]) + 1:lenS] == divide[i]
                    is_contains = true
                    lenS -= length(divide[i])
                end
            end
        end

        if !is_contains
            ans = false
            break
        end
    end

    if ans
        println("YES")
    else
        println("NO")
    end
end

main()


function main()
    divide = map(reverse,["dream", "dreamer", "erase", "eraser"])

    S = reverse(readline())

    ans = true
    while length(S) > 0
        is_contains = false
        for i = 1:4
            if length(divide[i]) <= length(S)
                if S[end - length(divide[i]) + 1:end] == divide[i]
                    is_contains = true
                    S = S[1:end - length(divide[i])]
                end
            end
        end

        if !is_contains
            ans = false
            break
        end
    end

    if ans
        println("YES")
    else
        println("NO")
    end
end

main()


function main()
    divide = ["dream", "dreamer", "erase", "eraser"]

    S = readline()

    ans = true
    while length(S) > 0
        is_contains = false
        for i = 1:4
            if S[end - length(divide[i]) + 1:end] == divide[i]
                is_contains = true
                S = S[1:end - length(divide[i])]
            end
        end

        if !is_contains
            ans = false
            break
        end
    end

    if ans
        println("YES")
    else
        println("NO")
    end
end

main()


function main()
    divide = ["dream", "dreamer", "erase", "eraser"]

    S = readline()

    ans = true
    while length(S) > 0
        is_contains = false
        for i = 1:4
            if length(divide[i]) <= length(S)
                if S[end - length(divide[i]) + 1:end] == divide[i]
                    is_contains = true
                    S = S[1:end - length(divide[i])]
                end
            end
        end

        if !is_contains
            ans = false
            break
        end
    end

    if ans
        println("YES")
    else
        println("NO")
    end
end

main()


function main()
    divide = ["dream", "dreamer", "erase", "eraser"]
    divide = map(x -> reverse(x), divide)

    S = reverse(readline())

    can = true
    i = 1
    while 1 <= i <= length(S)
        can2 = false
        for j = 1:4
            d = divide[j]
            if length(S) >= i + length(d) - 1 && S[i:i + length(d) - 1] == d
                can2 = true
                i += length(d)
            end
        end
        if !can2
            can = false
            break
        end
    end

    if can
        println("YES")
    else
        println("NO")
    end
end

main()


function main()
    divide = ["dream", "dreamer", "erase", "eraser"]
    divide = map(x -> x = reverse(x), divide)

    S = reverse(readline())

    can = true
    i = 1
    while 1 <= i <= length(S)
        can2 = false
        for j = 1:4
            d = divide[j]
            if length(S) >= i + length(d) - 1 && S[i:i + length(d) - 1] == d
                can2 = true
                i += length(d)
            end
        end
        if !can2
            can = false
            break
        end
    end

    if can
        println("YES")
    else
        println("NO")
    end
end

main()


# function erase(s::string, ur::UnitRange)
#     res = ""
#     if(ur.start != 1)
#         res *= s[1:ur.start - 1]
#     end

#     if(ur.stop != length(s))
#         res *= s[ur.stop + 1:end]
#     end

#     return res
# end

function main()
    divide = ["dream", "dreamer", "erase", "eraser"]
    divide = map(x -> x = reverse(x), divide)

    S = reverse(readline())

    can = true
    i = 1
    while 1 <= i <= length(S)
        can2 = false
        for j = 1:4
            d = divide[j]
            if length(S) >= i + length(d) - 1 && S[i:i + length(d) - 1] == d
                can2 = true
                i += length(d)
            end
        end
        if !can2
            can = false
            break
        end
    end

    if can
        println("YES")
    else
        println("NO")
    end
end

main()


S = "X" * chomp(readline())
function letter(S)
    if S[end-4:end] == "dream" || S[end-4:end] == "erase"
        return S = S[1:end-5]
    elseif S[end-5:end] == "eraser"
        return S = S[1:end-6]
    elseif S[end-6:end] == "dreamer"
        return S = S[1:end-7]
    end
end

for i in 1:25000
    try
        S = letter(S)
    except
        break
    end
end

if S == "X"
    println("YES")
else
    println("NO")
end


function main()

  S = chomp(readline())

  check = S[1]

  ans = 0

  for i in S
    if check != i
      check = i
      ans += 1
    end
  end

  println(ans)

end

main()

Score : 300 points

Problem Statement

Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.

In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.

Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.

Constraints

1 ≦ |S| ≦ 10^5

Each character in S is B or W.

Input

The input is given from Standard Input in the following format:

S

Output

Print the minimum number of new stones that Jiro needs to place for his purpose.

Sample Input 1

BBBWW

Sample Output 1

1

By placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.

In either way, Jiro's purpose can be achieved by placing one stone.

Sample Input 2

WWWWWW

Sample Output 2

0

If all stones are already of the same color, no new stone is necessary.

Sample Input 3

WBWBWBWBWB

Sample Output 3

9


s = chomp(readline())
count = 0
for i in 2:length(s)
  if s[i] != s[i-1]
    count += 1
  end
end
println(count)


s = chomp(readline())
count = 0
for i in 2:n
  if s[i] != s[i-1]
    count += 1
  end
end
println(count)


Score : 200 points

Problem Statement

Iroha has a sequence of N strings S_1, S_2, ..., S_N. The length of each string is L.

She will concatenate all of the strings in some order, to produce a long string.

Among all strings that she can produce in this way, find the lexicographically smallest one.

Here, a string s=s_1s_2s_3...s_n is lexicographically smaller than another string t=t_1t_2t_3...t_m if and only if one of the following holds:

There exists an index i(1≦i≦min(n,m)), such that s_j = t_j for all indices j(1≦j<i), and s_i<t_i.

s_i = t_i for all integers i(1≦i≦min(n,m)), and n<m.

Constraints

1 ≦ N, L ≦ 100

For each i, the length of S_i equals L.

For each i, S_i consists of lowercase letters.

Input

The input is given from Standard Input in the following format:

N L
S_1
S_2
:
S_N

Output

Print the lexicographically smallest string that Iroha can produce.

Sample Input 1

3 3
dxx
axx
cxx

Sample Output 1

axxcxxdxx

The following order should be used: axx, cxx, dxx.


function main()
    readline()
    return join(sort(replace.(readlines(), "\n" => "")))
end

println(main())


function main()
    return join(sort(replace.(readlines(), "\n" => "")))
end

println(main())


function main()
    return join(sort(readlines()))
end

println(main())


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()
  n,l=pM(split(readline()))
  s=sort([chomp(readline()) for i=1:n])
  println(*(s...))
end
main()
if isdefined(Base, :mystdin);close(mystdin);end


MyString=Union{String,SubString{String},Char}
MyStringArray=Union{Array{String,1},Array{SubString{String},1},Array{Char,1}}
parseInt(x::MyString) = parse(Int, x)
parseInt(x::Real) = trunc(Int, x)
parseBigInt(x::MyString) = parse(BigInt, x)
parseBigInt(x::Real) = trunc(BigInt, x)
parseFloat(x::MyString) = parse(Float64, x)
parseFloat(x::Real) = trunc(Float64, x)
parseBigFloat(x::MyString) = parse(BigFloat, x)
parseBigFloat(x::Real) = trunc(BigFloat, x)
parseMap(x::MyStringArray) = map(parseInt, x)
parseMapBig(x::MyStringArray) = map(parseBigInt, x)

function main()
    n,l=parseMap(split(readline()))
    s=sort([chomp(readline()) for i in 1:n])
    println(reduce(*,s))
end
main()


function main()

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

  S = String[]

  for i in 1:N
    push!(S,chomp(readline()))
  end

  sort!(S)

  println(join(S))

end

main()


function main()

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

    vs = [A B C]
    count5 = 0
    count7 = 0

    for i in 1:3

        if vs[i] == 5
            count5 += 1
        elseif vs[i] == 7
            count7 += 1
        end

    end

    if count5 == 2 && count7 == 1
        println("YES")
    else
        println("NO")
    end

end

main()


n,l = map(x -> parse(Int64, x), split(readline()))

a = Array{Any}(1)
a[1] = chomp(readline())
for i in 2:n
    x = chomp(readline())
  for j in 1:i-1
    if x <= a[j]
      insert!(a, j, x)
      break
    elseif j==i-1
      insert!(a, i, x)
    end
  end
end
print(join(a))


p(x) = parse(Int,x)
function main()
  a,b = map(p,split(readline()))
  l = Array{Union{Missing,String}}(missing,a)
  for i in 1:a
    l[i] = replace(readline(),"\""=>"")
  end
  print(join(sort(l)))
end
main()


p(x) = parse(Int,x)
function main()
  a,b = map(p,split(readline()))
  l = Array{Union{Missing,String}}(missing,a)
  for i in 1:a
    l[i] = readline()
  end
  print(join(sort(l)))
end
main()


p(x) = parse(Int,x)
function main()
  a,b = map(p,split(readline()))
  l = Array{Union{Missing,String}}(missing,a)
  for i in 1:a
    l[i] = readline()
  end
  print(reduce(*,sort(l)))
end
main()


p(x) = parse(Int,x)
function main()
  a,b = map(p,split(readline()))
  l = Array{Union{Missing,String}}(missing,a)
  for i in 1:a
    l[i] = readline()
  end
  reduce(*,sort(l))
end
main()


p(x) = parse(Int,x)
function main()
  a,b = map(p,split(readline()))
  l = Array{String}(undef,3)
  for i in 1:a
    l[i] = readline()
  end
  reduce(*,sort(l))
end
main()


p(x) = parse(Int,x)
function main()
  a,b = map(p,split(readline()))
  l = Array{String,a}
  for i in 1:a
    l[i] = readline()
  end
  reduce(*,sort(l))
end
main()


s=[];
for i=1:parse(Int,split(readline())[1])
	push!(s,chomp(readline()))
end
sort!(s)
for i=s
	print(i)
end


s=[];
for i=1:parse(Int,split(readline())[1])
	push!(s,readline())
end
sort!(s)
for i=s
	print(i)
end


(N, L) = parse.([Int],  split(readline(STDIN)))
Ss = [chomp(readline(STDIN)) for _ in 1:N]
println(join(sort(Ss)))