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

function main()
    n=parseInt(readline())
    a=sort(parseMap(split(readline())))
    cnt=0
    for i in 1:n
        if a[i]<0
            a[i]*=-1
            cnt+=1
        else
            break
        end
    end
    sort!(a,by=abs)
    if cnt%2==1
        a[1]*=-1
    end
    println(sum(a))
end
main()

Score : 400 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, arranged in a row in this order.

You can perform the following operation on this integer sequence any number of times:

Operation: Choose an integer i satisfying 1 \leq i \leq N-1. Multiply both A_i and A_{i+1} by -1.

Let B_1, B_2, ..., B_N be the integer sequence after your operations.

Find the maximum possible value of B_1 + B_2 + ... + B_N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

-10^9 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value of B_1 + B_2 + ... + B_N.

Sample Input 1

3
-10 5 -4

Sample Output 1

19

If we perform the operation as follows:

Choose 1 as i, which changes the sequence to 10, -5, -4.

Choose 2 as i, which changes the sequence to 10, 5, 4.

we have B_1 = 10, B_2 = 5, B_3 = 4. The sum here, B_1 + B_2 + B_3 = 10 + 5 + 4 = 19, is the maximum possible result.

Sample Input 2

5
10 -4 -8 -11 3

Sample Output 2

30

Sample Input 3

11
-1000000000 1000000000 -1000000000 1000000000 -1000000000 0 1000000000 -1000000000 1000000000 -1000000000 1000000000

Sample Output 3

10000000000

The output may not fit into a 32-bit integer type.


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

function main()
	n = readline() |> parseInt
	s = chomp(readline())
	b = 0
	for i in 1:n
		if s[i] == '#'
			b += 1
		end
	end
	l = n
	for i in 1:n
		if s[i] == '#'
			l = i-1
			break
		end
	end
	r = n
	for i in 0:n-1
		if s[n-i] == '.'
			r = i
			break
		end
	end
	println(min(b-r, (n-b)-l, ))
end
main()

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

Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

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

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

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0

Submitted source-code file size, in bytes.

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

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


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

function main()
	n = readline() |> parseInt
	s = chomp(readline())
	c = 0
	cc = 0
	for i in 0:n-1
		if s[n-i] == '.'
			cc = n-i
			break
		end
	end
	for i in 1:cc
		if s[i] == '.'
			count += 1
		end
	end
	if count == cc
		println(0)
	elseif c <= cc/2
		println(c)
	else
		println(cc-c)
	end
end
main()


Score : 100 points

Problem Statement

There are three houses on a number line: House 1, 2 and 3, with coordinates A, B and C, respectively.
Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Constraints

0\leq A,B,C\leq 100

A, B and C are distinct integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Sample Input 1

3 8 5

Sample Output 1

Yes

We pass the coordinate 5 on the straight way from the house at coordinate 3 to the house at coordinate 8.

Sample Input 2

7 3 1

Sample Output 2

No

Sample Input 3

10 2 4

Sample Output 3

Yes

Sample Input 4

31 41 59

Sample Output 4

No


let
  house = map(x -> parse(Int, x), split(readline()))
  if (house[1] ≤ house[3] ≤ house[2]) || (house[3] ≤ house[3] ≤ house[1])
    println("Yes")
  else
    println("No")
  end
end


readline()
s=chomp(readline())
i=parse(readline())
for c=s
  print(c==s[i] ? c : "*")
end
println()

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, and an integer K.
Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Constraints

1 \leq K \leq N\leq 10

S is a string of length N consisting of lowercase English letters.

N and K are integers.

Input

Input is given from Standard Input in the following format:

N
S
K

Output

Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Sample Input 1

5
error
2

Sample Output 1

*rr*r

The second character of S is r. When we replace every character in error that differs from r with *, we get the string *rr*r.

Sample Input 2

6
eleven
5

Sample Output 2

e*e*e*

Sample Input 3

9
education
7

Sample Output 3

******i**


n=parse(readline())
s=chomp(readline())
k=parse(readline())
for i in 1:n
  if s[i] !=s[k]
    print('*')
  else
    print(s[i])
  end
end
println("")


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

function main()
    n=parseInt(readline())
    s=chomp(readline())
    res1=0
    flg1=false
    res2=0
    flg2=true
    for i in 1:n
        if s[i]=='.'
            if flg1
                res1+=1
            end
        elseif !flg1
            flg1=true
        end
        if s[i]=='#'
            if flg2
                res2+=1
            end
        elseif !flg2
            flg2=false
        end
    end
    println(min(res1,res2))
end
main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    a,b=parseMap(split(readline()))
    println(maximum([2a-1,a+b,2b-1]))
end
main()

Score : 100 points

Problem Statement

There are two buttons, one of size A and one of size B.

When you press a button of size X, you get X coins and the size of that button decreases by 1.

You will press a button twice. Here, you can press the same button twice, or press both buttons once.

At most how many coins can you get?

Constraints

All values in input are integers.

3 \leq A, B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of coins you can get.

Sample Input 1

5 3

Sample Output 1

9

You can get 5 + 4 = 9 coins by pressing the button of size 5 twice, and this is the maximum result.

Sample Input 2

3 4

Sample Output 2

7

Sample Input 3

6 6

Sample Output 3

12


const lines=readlines(open("/dev/fd/0"))
let
    state=0
    global input
    input()=lines[state+=1]
end
int(s::String)=parse(Int32,s)
intSub(s::SubString{String})=parse(Int32,s)
intLine()=map(intSub,split(input()))
function main()
    n=int(input())
    m=c=0
    for h in intLine()
        if h>=m
            c+=1
            m=h
        end
    end
    print(c)
end
main()

Score : 200 points

Problem Statement

There are N mountains ranging from east to west, and an ocean to the west.

At the top of each mountain, there is an inn. You have decided to choose where to stay from these inns.

The height of the i-th mountain from the west is H_i.

You can certainly see the ocean from the inn at the top of the westmost mountain.

For the inn at the top of the i-th mountain from the west (i = 2, 3, ..., N), you can see the ocean if and only if H_1 \leq H_i, H_2 \leq H_i, ..., and H_{i-1} \leq H_i.

From how many of these N inns can you see the ocean?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq H_i \leq 100

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the number of inns from which you can see the ocean.

Sample Input 1

4
6 5 6 8

Sample Output 1

3

You can see the ocean from the first, third and fourth inns from the west.

Sample Input 2

5
4 5 3 5 4

Sample Output 2

3

Sample Input 3

5
9 5 6 8 4

Sample Output 3

1


S = collect(strip(readline()))
F = collect("01"^(div(length(S), 2) + 2))

ret = length(S)
for i in 0:1
    ret = min(ret, sum(S .!= F[1+i:length(S)+i]))
end
println(ret)

Score : 300 points

Problem Statement

N tiles are arranged in a row from left to right. The initial color of each tile is represented by a string S of length N.

The i-th tile from the left is painted black if the i-th character of S is 0, and painted white if that character is 1.

You want to repaint some of the tiles black or white, so that any two adjacent tiles have different colors.

At least how many tiles need to be repainted to satisfy the condition?

Constraints

1 \leq |S| \leq 10^5

S_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of tiles that need to be repainted to satisfy the condition.

Sample Input 1

000

Sample Output 1

1

The condition can be satisfied by repainting the middle tile white.

Sample Input 2

10010010

Sample Output 2

3

Sample Input 3

0

Sample Output 3

0


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()))
    s=chomp(readline())
    cnt=Vector{Int}()
    res=1
    for i in 2:n+1
        if i==n+1 || s[i]!=s[i-1]
            push!(cnt,res*ifelse(s[i-1]=='0',-1,1))
            res=1
        else
            res+=1
        end
    end
    l=length(cnt)
    ans=0
    ruiseki=[0 for i in 1:l+1]
    for i in 1:l
        ruiseki[i+1]=ruiseki[i]+abs(cnt[i])
    end
    for i in 1:l
        if cnt[i]>0
            m=min(i+2k,l)
        else
            m=min(i+2k-1,l)
        end
        ans=max(ans,ruiseki[m+1]-ruiseki[i])
    end
    println(ans)
end
main()

Score : 400 points

Problem Statement

N people are arranged in a row from left to right.

You are given a string S of length N consisting of 0 and 1, and a positive integer K.

The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.

You will give the following direction at most K times (possibly zero):

Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

Find the maximum possible number of consecutive people standing on hands after at most K directions.

Constraints

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

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

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

Sample Input 1

5 1
00010

Sample Output 1

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.


function calc_seq(xs)
    seq = Int[]
    count = 1
    for i = 1:length(xs)-1
        if xs[i] == xs[i+1]
            count += 1
        else
            push!(seq, count)
            count = 1
        end
    end
    push!(seq, count)
    seq
end

function main(n, k, xs)
    seq = calc_seq(xs)
    m = length(seq)
    if xs[1] == 0
        seq = vcat(0, seq)
        m = length(seq)
    end
    if m % 2 == 0
        seq = vcat(seq, 0)
        m = length(seq)
    end
    val = sum(seq[1:min(2k+1, m)])
    biggest = val
    i = 1
    while i <= (m - (2k+1) + 2) ÷ 2
        i += 2
        val += sum(seq[2k+i-1:2k+i]) - sum(seq[i-2:i-1])
        if biggest < val
            biggest = val
        end
    end
    print(biggest)
end

n, k = [parse(Int, x) for x in split(chomp(readline()))]
str = chomp(readline())
xs = zeros(Int, n)

for (i, s) in enumerate(str)
    xs[i] = parse(Int, s)
end

main(n, k, xs)


# function calc_seq(xs)
#     seq = Int[]
#     count = 1
#     for i = 1:length(xs)-1
#         if xs[i] == xs[i+1]
#             count += 1
#         else
#             push!(seq, count)
#             count = 1
#         end
#     end
#     push!(seq, count)
#     seq
# end
#
# function main(n, k, xs)
#     seq = calc_seq(xs)
#     m = length(seq)
#     val = Int[]
#     if xs[1] == 1
#         for i = 1:2:m
#             push!(val, sum(seq[i:min(2k+i, m)]))
#         end
#     else
#         for i = 0:2:m
#             push!(val, sum(seq[max(i, 1):min(2k+i, m)]))
#         end
#     end
#     print(max(val...))
# end

n, k = [parse(Int, x) for x in split(chomp(readline()))]
str = chomp(readline())
xs = zeros(Int, n)

for (i, s) in enumerate(str)
    xs[i] = parse(Int, s)
end

# main(n, k, xs)

print(sum(xs))


# Julia 1.0
# n, k = [parse(Int, x) for x in split(readline())]
# str = readline()
n, k = [parse(Int, x) for x in split(readline() |> chomp, " ")]
str = readline() |> chomp

xs = zeros(Int, n)
for (i, s) in enumerate(str)
    xs[i] = parse(Int, s)
end

# Julia 1.0
xor(x::Bool, y::Bool) = (x != y)
xor(x::Int, y::Int) = xor(Bool(x), Bool(y))

ys = vcat(1, xs)
flip = zeros(Int, n)
for i = 1:n
    flip[i] = xor(ys[i], ys[i+1])
end

flippoints = Int[]
for i = 1:n
    if flip[i] == 1
        push!(flippoints, i)
    end
end

# println(flip)
# println(flippoints)

values = Int[]

fn = length(flippoints)
i = 1
while i <= fn
    b = false
    left0 = 1
    if 1 <= i-1
        left0 = min(flippoints[i-1] + 1, flippoints[i] - 1)
    end
    left1 = flippoints[i]
    right1 = if fn % 2 == 0
        flippoints[end]
    else
        fn
    end
    if i+2k-1 <= fn
        right1 = flippoints[i-1+2k]
    end
    right0 = n
    if i+2k <= fn
        right0 = flippoints[i+2k] - 1
    else
        b = true
    end
    push!(values, right0 - left0 + 1)
    # println("l0, l1, r1, l0 = ", [left0, left1, right1, right0])
    # println(values)
    if b
        break
    end
    i += 2
end

if isempty(values)
    print(n)
else
    print(max(values...))
end


function read_Int64()
	return Base.parse(Base.Int64, Base.readline(Base.STDIN))
end

function abc123_A()

	const num_antennas = 5

	# STEP.01
	# read out the coordinates of the each antenna
	coordinate = Base.Array{Base.Int64}(num_antennas)

	for itr in Base.range(1, 1, num_antennas)
		coordinate[itr] = read_Int64()
	end

	# STEP.02
	# read out the maximum distance to communicate
	distance_max = read_Int64()

	for itr_Lv1 in Base.range(1, 1, num_antennas)
	for itr_Lv2 in Base.range(1, 1, num_antennas)
		if Base.abs(coordinate[itr_Lv2] - coordinate[itr_Lv1]) > distance_max
			return ":("
		else
			continue
		end
	end
	end

	return "Yay!"

end

Base.println(Base.STDOUT, Main.abc123_A())

Score: 100 points

Problem Statement

In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively.

Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k.

Determine if there exists a pair of antennas that cannot communicate directly.

Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p.

Constraints

a, b, c, d, e and k are integers between 0 and 123 (inclusive).

a < b < c < d < e

Input

Input is given from Standard Input in the following format:

a
b
c
d
e
k

Output

Print :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair.

Sample Input 1

1
2
4
8
9
15

Sample Output 1

Yay!

In this case, there is no pair of antennas that cannot communicate directly, because:

the distance between A and B is 2 - 1 = 1

the distance between A and C is 4 - 1 = 3

the distance between A and D is 8 - 1 = 7

the distance between A and E is 9 - 1 = 8

the distance between B and C is 4 - 2 = 2

the distance between B and D is 8 - 2 = 6

the distance between B and E is 9 - 2 = 7

the distance between C and D is 8 - 4 = 4

the distance between C and E is 9 - 4 = 5

the distance between D and E is 9 - 8 = 1

and none of them is greater than 15. Thus, the correct output is Yay!.

Sample Input 2

15
18
26
35
36
18

Sample Output 2

:(

In this case, the distance between antennas A and D is 35 - 15 = 20 and exceeds 18, so they cannot communicate directly.
Thus, the correct output is :(.


function main()

  A = parse(Int,readline())
  B = parse(Int,readline())
  C = parse(Int,readline())
  D = parse(Int,readline())
  E = parse(Int,readline())

  save = 0

  for i in [1,2,3,4,5,6,7,8,9,0],j in [A,B,C,D,E]

    if j % 10 == i
      save = j
      break
    end

  end

  ans = 0
  check = false

  for i in [A,B,C,D,E]

    if i == save && check == false
      check = true
      continue
    end

    ans += i

    if i % 10 != 0
      ans += (10-(i%10))
    end

  end

  ans += save

  println(ans)

end

main()

Score: 200 points

Problem Statement

The restaurant AtCoder serves the following five dishes:

ABC Don (rice bowl): takes A minutes to serve.

ARC Curry: takes B minutes to serve.

AGC Pasta: takes C minutes to serve.

APC Ramen: takes D minutes to serve.

ATC Hanbagu (hamburger patty): takes E minutes to serve.

Here, the time to serve a dish is the time between when an order is placed and when the dish is delivered.

This restaurant has the following rules on orders:

An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...).

Only one dish can be ordered at a time.

No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered.

E869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered.

Here, he can order the dishes in any order he likes, and he can place an order already at time 0.

Constraints

A, B, C, D and E are integers between 1 and 123 (inclusive).

Input

Input is given from Standard Input in the following format:

A
B
C
D
E

Output

Print the earliest possible time for the last dish to be delivered, as an integer.

Sample Input 1

29
20
7
35
120

Sample Output 1

215

If we decide to order the dishes in the order ABC Don, ARC Curry, AGC Pasta, ATC Hanbagu, APC Ramen, the earliest possible time for each order is as follows:

Order ABC Don at time 0, which will be delivered at time 29.

Order ARC Curry at time 30, which will be delivered at time 50.

Order AGC Pasta at time 50, which will be delivered at time 57.

Order ATC Hanbagu at time 60, which will be delivered at time 180.

Order APC Ramen at time 180, which will be delivered at time 215.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 2

101
86
119
108
57

Sample Output 2

481

If we decide to order the dishes in the order AGC Pasta, ARC Curry, ATC Hanbagu, APC Ramen, ABC Don, the earliest possible time for each order is as follows:

Order AGC Pasta at time 0, which will be delivered at time 119.

Order ARC Curry at time 120, which will be delivered at time 206.

Order ATC Hanbagu at time 210, which will be delivered at time 267.

Order APC Ramen at time 270, which will be delivered at time 378.

Order ABC Don at time 380, which will be delivered at time 481.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 3

123
123
123
123
123

Sample Output 3

643

This is the largest valid case.


parseInt(x) = parse(Int, x)
min1=9
minsoe=0
array=[0 for i in 1:5]
for i in 1:5
    array[i]=parseInt(readline())
    if (array[i]%10)<=(min1%10) && array[i]%10>0
        min1=array[i]
        minsoe=i
    end
end
time=0
for i in 1:5
   if i != minsoe
        time+=div(array[i]+9,10)*10
    end
end
time+=array[minsoe]
println(time)


xyzk = [parse(Int, x) for x in split(readline(), " ")]
k = xyzk[4]
xs = xyzk[1:3]
as = [parse(Int, x) for x in split(readline(), " ")]
bs = [parse(Int, x) for x in split(readline(), " ")]
cs = [parse(Int, x) for x in split(readline(), " ")]

as = sort(as, rev=true)
bs = sort(bs, rev=true)
cs = sort(cs, rev=true)

A = zeros(Int, xs...)
B = zeros(Int, xs...)
C = zeros(Int, xs...)

for i = 1:xs[1]
    for j = 1:xs[2]
        for l = 1:xs[3]
            A[i, j, l] = as[i]
            B[i, j, l] = bs[j]
            C[i, j, l] = cs[l]
        end
    end
end

X = A + B + C

vs = vec(X)
vs = sort(vs, rev=true)

for i = 1:k
    print(vs[i])
    if i != k
        println()
    end
end

Score: 400 points

Problem Statement

The Patisserie AtCoder sells cakes with number-shaped candles.
There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.
Each cake has an integer value called deliciousness, as follows:

The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.

The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.

The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.

Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.

There are X \times Y \times Z such ways to choose three cakes.

We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.

Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

1 \leq K \leq \min(3 \ 000, X \times Y \times Z)

1 \leq A_i \leq 10 \ 000 \ 000 \ 000

1 \leq B_i \leq 10 \ 000 \ 000 \ 000

1 \leq C_i \leq 10 \ 000 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z K
A_1 \ A_2 \ A_3 \ ... \ A_X
B_1 \ B_2 \ B_3 \ ... \ B_Y
C_1 \ C_2 \ C_3 \ ... \ C_Z

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:

(A_2, B_2, C_2): 6 + 5 + 8 = 19

(A_1, B_2, C_2): 4 + 5 + 8 = 17

(A_2, B_1, C_2): 6 + 1 + 8 = 15

(A_2, B_2, C_1): 6 + 5 + 3 = 14

(A_1, B_1, C_2): 4 + 1 + 8 = 13

(A_1, B_2, C_1): 4 + 5 + 3 = 12

(A_2, B_1, C_1): 6 + 1 + 3 = 10

(A_1, B_1, C_1): 4 + 1 + 3 = 8

Sample Input 2

3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

400
310
310
301
301

There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301.
However, they are different ways of choosing cakes, so 301 occurs twice in the output.

Sample Input 3

10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

Note that the input or output may not fit into a 32-bit integer type.


Score : 100 points

Problem Statement

You are given three integers A, B and C.

Determine if there exists an equilateral triangle whose sides have lengths A, B and C.

Constraints

All values in input are integers.

1 \leq A,B,C \leq 100

Input

Input is given from Standard Input in the following format:

A B C

Output

If there exists an equilateral triangle whose sides have lengths A, B and C, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

There exists an equilateral triangle whose sides have lengths 2, 2 and 2.

Sample Input 2

3 4 5

Sample Output 2

No

There is no equilateral triangle whose sides have lengths 3, 4 and 5.


function readline_STDIN()
    return Base.readline(Base.STDIN)
end

function exawizards2019_A(A::Base.Int64, B::Base.Int64, C::Base.Int64)

    if Base.isequal(A, B) && Base.isequal(B, C)
        return "Yes"
    else
        return "No"
    end

end

A, B, C = Base.parse.(Base.split(readline_STDIN()))
Base.println(Base.STDOUT, Main.exawizards2019_A(A, B, C))


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

function main()
	a,b,c = readline() |> split |> parseMap
	println(a==b==c?"Yes":"No")
end
main()


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

function main()
	n = readline() |> parseInt
	s = chomp(readline())
	r = 0
	b = 0
	for i in 1:n
		if s[i] == 'R'
			r += 1
		else
			b += 1
		end
	end
	println(r>b?"Yes":"No")
end
main()

Score : 200 points

Problem Statement

There are N people numbered 1 to N. Each person wears a red hat or a blue hat.

You are given a string s representing the colors of the people. Person i wears a red hat if s_i is R, and a blue hat if s_i is B.

Determine if there are more people wearing a red hat than people wearing a blue hat.

Constraints

1 \leq N \leq 100

|s| = N

s_i is R or B.

Input

Input is given from Standard Input in the following format:

N
s

Output

If there are more people wearing a red hat than there are people wearing a blue hat, print Yes; otherwise, print No.

Sample Input 1

4
RRBR

Sample Output 1

Yes

There are three people wearing a red hat, and one person wearing a blue hat.

Since there are more people wearing a red hat than people wearing a blue hat, the answer is Yes.

Sample Input 2

4
BRBR

Sample Output 2

No

There are two people wearing a red hat, and two people wearing a blue hat.

Since there are as many people wearing a red hat as people wearing a blue hat, the answer is No.


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

function main()
	n,q = readline() |> split |> parseMap
	s = readline() |> chomp
	t = [Int(s[i]) for i in 1:n]
	o = Tuple{Int,Char}[(0,'a') for i in 1:q]
	for i in 1:q
		a,b = split(readline())
		o[i] = (Int(a[1]),b[1])
	end
	ll = 0
	rr = n+1
	while rr-ll>1
		m = (ll+rr)>>1
		now = m
		for i in 1:q
			if now==0||now==n+1
				break
			end
			if t[now]==o[i][1]
				if o[i][2]=='L'
					now -= 1
				else
					now += 1
				end
			end
		end
		if now==0
			ll = m
		else
			rr = m
		end
	end
	l = 0
	r = n+1
	while r-l>1
		m = (l+r)>>1
		now = m
		for i in 1:q
			if now==0||now==n+1
				break
			end
			if t[now]==o[i][1]
				if o[i][2]=='L'
					now -= 1
				else
					now += 1
				end
			end
		end
		if now==n+1
			r = m
		else
			l = m
		end
	end
	println(r-ll-1)
end

main()

Score : 500 points

Problem Statement

There are N squares numbered 1 to N from left to right.
Each square has a character written on it, and Square i has a letter s_i. Besides, there is initially one golem on each square.

Snuke cast Q spells to move the golems.

The i-th spell consisted of two characters t_i and d_i, where d_i is L or R.
When Snuke cast this spell, for each square with the character t_i, all golems on that square moved to the square adjacent to the left if d_i is L, and moved to the square adjacent to the right if d_i is R.

However, when a golem tried to move left from Square 1 or move right from Square N, it disappeared.

Find the number of golems remaining after Snuke cast the Q spells.

Constraints

1 \leq N,Q \leq 2 \times 10^{5}

|s| = N

s_i and t_i are uppercase English letters.

d_i is L or R.

Input

Input is given from Standard Input in the following format:

N Q
s
t_1 d_1
\vdots
t_{Q} d_Q

Output

Print the answer.

Sample Input 1

3 4
ABC
A L
B L
B R
A R

Sample Output 1

2

Initially, there is one golem on each square.

In the first spell, the golem on Square 1 tries to move left and disappears.

In the second spell, the golem on Square 2 moves left.

In the third spell, no golem moves.

In the fourth spell, the golem on Square 1 moves right.

After the four spells are cast, there is one golem on Square 2 and one golem on Square 3, for a total of two golems remaining.

Sample Input 2

8 3
AABCBDBA
A L
B R
A R

Sample Output 2

5

After the three spells are cast, there is one golem on Square 2, two golems on Square 4 and two golems on Square 6, for a total of five golems remaining.

Note that a single spell may move multiple golems.

Sample Input 3

10 15
SNCZWRCEWB
B R
R R
E R
W R
Z L
S R
Q L
W L
B R
C L
A L
N L
E R
Z L
S L

Sample Output 3

3


parseInt(x) = parse(Int,x)

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

function main()
    N,Q = readline() |> split |> parseMap
    s = readline()
    s = s[1:length(s)-1]
    order = []
    for i in 1:Q
       t,d = readline() |> split
       push!(order,[t[1],d[1]])
    end
end

main()


Score : 200 points

Problem Statement

You are given a string S consisting of uppercase English letters. Find the length of the longest ACGT string that is a substring (see Notes) of S.

Here, a ACGT string is a string that contains no characters other than A, C, G and T.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

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

Each character in S is an uppercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

Print the length of the longest ACGT string that is a substring of S.

Sample Input 1

ATCODER

Sample Output 1

3

Among the ACGT strings that are substrings of ATCODER, the longest one is ATC.

Sample Input 2

HATAGAYA

Sample Output 2

5

Among the ACGT strings that are substrings of HATAGAYA, the longest one is ATAGA.

Sample Input 3

SHINJUKU

Sample Output 3

0

Among the ACGT strings that are substrings of SHINJUKU, the longest one is (the empty string).


function main()

  (N,Q) = map(x -> parse(Int,x), split(readline()))
  check = zeros(Int,N)
  save = 0

  S = chomp(readline())

  for i in 1:N
    if i == 1
      continue
    elseif S[i-1] == 'A' && S[i] == 'C'
      if save == 0
        check[i] += 1
        save = i
      else
        check[i] += check[save] + 1
        save = i
      end
    else
      check[i] = check[save]
    end
  end

  for i in 1:Q
    (l,r) = map(x -> parse(Int,x), split(readline()))
    println(check[r]-check[l])
  end

end

main()

Score : 300 points

Problem Statement

You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries:

Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring?

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

Each character in S is A, C, G or T.

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.

Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.

Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.


function main()

  (N,Q) = map(x -> parse(Int,x), split(readline()))
  check = zeros(Int,N)
  save = 0

  S = chomp(readline())

  for i in 2:N
    if S[i-1] == 'A' && S[i] == 'C'
      if save == 0
        check[i] += 1
        save = i
      else
        check[i] += check[save] + 1
        save = i
      end
    else
      check[i] = check[save]
    end
  end

  for i in 1:Q
    (l,r) = map(x -> parse(Int,x), split(readline()))
    println(check[r]-check[l])
  end

end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n,q=parseMap(split(readline()))
    s=chomp(readline())
    cnt=[0 for i in 1:n]
    for i in 2:n
        cnt[i]=cnt[i-1]
        if s[i-1:i]=="AC"
            cnt[i]+=1
        end
    end
    for i in 1:q
        l,r=parseMap(split(readline()))
        println(cnt[r]-cnt[l])
    end
end
main()


k()=readline()|>chomp
s()=[parse(Int,x)for x=k()|>split]
n,q=s()
t=k()
a=zeros(Int,n)
c=0
for j=2:n
if t[j-1:j]=="AC"
c+=1
end
a[j]=c
end
for i=1:q
l,r=s()
println(a[r]-a[l])
end


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

function main()
	n,q = readline() |> split |> parseMap
	s = chomp(readline())
	k = Array{Int}(n+1)
	k[1] = 0
	for i in 1:n
		k[i+1] = k[i]
		if s[i] == 'A' && s[i+1] == 'C'
			k[i+1] += 1
		end
	end
	for i in 1:q
		l,r = readline() |> split |> parseMap
		x = k[r+1]-k[l]
		if l>1 && s[l-1] == 'A' && s[l] == 'C'
			x-=1
		end
		println(x)
	end
end
main()


a=b=c=d=e=f=0
g=1
for _=1:parse(Int,readline())
	a,b,c,d,e,f,g=b,e,e-d,f,g,g-c,(g*4-b-c-d-a*3)%(10^9+7)
end
println(g)

Score : 400 points

Problem Statement

You are given an integer N. Find the number of strings of length N that satisfy the following conditions, modulo 10^9+7:

The string does not contain characters other than A, C, G and T.

The string does not contain AGC as a substring.

The condition above cannot be violated by swapping two adjacent characters once.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of strings of length N that satisfy the following conditions, modulo 10^9+7.

Sample Input 1

3

Sample Output 1

61

There are 4^3 = 64 strings of length 3 that do not contain characters other than A, C, G and T. Among them, only AGC, ACG and GAC violate the condition, so the answer is 64 - 3 = 61.

Sample Input 2

4

Sample Output 2

230

Sample Input 3

100

Sample Output 3

388130742

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


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

function main()
	n = readline() |> parseInt
	b = readline() |> split |> parseMap
	flg = 0
	for i in 1:n
		if b[i] > i
			flg = 1
			println(-1)
			break
		end
	end
	if flg == 0
		ans = Int[]
		for i in 1:n
			t = length(b)
			for j in 1:length(b)
				if b[j] != j
					t = j-1
					break
				end
			end
			push!(ans,t)
			deleteat!(b,t)
		end
		for i in 0:n-1
			println(ans[n-i])
		end
	end
end
main()

Score : 400 points

Problem Statement

Snuke has an empty sequence a.

He will perform N operations on this sequence.

In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1).

You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq b_i \leq N

Input

Input is given from Standard Input in the following format:

N
b_1 \dots b_N

Output

If there is no sequence of N operations after which a would be equal to b, print -1.
If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted.

Sample Input 1

3
1 2 1

Sample Output 1

1
1
2

In this sequence of operations, the sequence a changes as follows:

After the first operation: (1)

After the second operation: (1,1)

After the third operation: (1,2,1)

Sample Input 2

2
2 2

Sample Output 2

-1

2 cannot be inserted at the beginning of the sequence, so this is impossible.

Sample Input 3

9
1 1 1 2 2 1 2 3 2

Sample Output 3

1
2
2
3
1
2
2
1
1


N=parse(Int,readline())
F=0
cnt=zeros(Int,26)
L=1
S=chomp(readline())
ans=0
for R=1:N
	F+=(cnt[Int(S[R])-96]+=1)>1
	while F>0
		F-=(cnt[Int(S[L])-96]-=1)==1
		L+=1
	end
	ans+=R-L+1
end
while L<N
	ans+=N-L+1
	L+=1
end
println(ans)

Score : 200 points

Problem Statement

You are given a string S of length N.
Among its subsequences, count the ones such that all characters are different, modulo 10^9+7. Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings.

Here, a subsequence of a string is a concatenation of one or more characters from the string without changing the order.

Constraints

1 \leq N \leq 100000

S consists of lowercase English letters.

|S|=N

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of the subsequences such that all characters are different, modulo 10^9+7.

Sample Input 1

4
abcd

Sample Output 1

15

Since all characters in S itself are different, all its subsequences satisfy the condition.

Sample Input 2

3
baa

Sample Output 2

5

The answer is five: b, two occurrences of a, two occurrences of ba. Note that we do not count baa, since it contains two as.

Sample Input 3

5
abcab

Sample Output 3

17


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

function main()
	n = readline() |> parseInt
	s = chomp(readline())
	d = Dict{Char,Int}()
	for i in 1:n
		if haskey(d,s[i])
			d[s[i]] += 1
		else
			d[s[i]] = 1
		end
	end
	t = 1
	mod = 1000000007
	for i in 1:length(d)
		t = t*d[i]%mod
	end
	println(t)
end
main()


function f()
    input1 = map(x -> parse(Int, x), split(chomp(readline()), " "))
    input2 = map(x -> parse(Int, x), split(chomp(readline()), " "))

    H = input1[1]
    W = input1[2]
    h = input2[1]
    w = input2[2]

    ret = (H - h) * (W - w)
    print(ret)
end
f()

Score : 100 points

Problem Statement

There are H rows and W columns of white square cells.

You will choose h of the rows and w of the columns, and paint all of the cells contained in those rows or columns.

How many white cells will remain?

It can be proved that this count does not depend on what rows and columns are chosen.

Constraints

All values in input are integers.

1 \leq H, W \leq 20

1 \leq h \leq H

1 \leq w \leq W

Input

Input is given from Standard Input in the following format:

H W
h w

Output

Print the number of white cells that will remain.

Sample Input 1

3 2
2 1

Sample Output 1

1

There are 3 rows and 2 columns of cells. When two rows and one column are chosen and painted in black, there is always one white cell that remains.

Sample Input 2

5 5
2 3

Sample Output 2

6

Sample Input 3

2 4
2 4

Sample Output 3

0


macro p(a);:((x->parse(Int,x)).(split($a)));end
n,m,c=@p(readline())
b=@p(readline())
a=vcat([@p(i)' for i in readlines()]...)
print(count(x->x>-c,a*b))

Score : 200 points

Problem Statement

There are N pieces of source code. The characteristics of the i-th code is represented by M integers A_{i1}, A_{i2}, ..., A_{iM}.

Additionally, you are given integers B_1, B_2, ..., B_M and C.

The i-th code correctly solves this problem if and only if A_{i1} B_1 + A_{i2} B_2 + ... + A_{iM} B_M + C > 0.

Among the N codes, find the number of codes that correctly solve this problem.

Constraints

All values in input are integers.

1 \leq N, M \leq 20

-100 \leq A_{ij} \leq 100

-100 \leq B_i \leq 100

-100 \leq C \leq 100

Input

Input is given from Standard Input in the following format:

N M C
B_1 B_2 ... B_M
A_{11} A_{12} ... A_{1M}
A_{21} A_{22} ... A_{2M}
\vdots
A_{N1} A_{N2} ... A_{NM}

Output

Print the number of codes among the given N codes that correctly solve this problem.

Sample Input 1

2 3 -10
1 2 3
3 2 1
1 2 2

Sample Output 1

1

Only the second code correctly solves this problem, as follows:

Since 3 \times 1 + 2 \times 2 + 1 \times 3 + (-10) = 0 \leq 0, the first code does not solve this problem.

1 \times 1 + 2 \times 2 + 2 \times 3 + (-10) = 1 > 0, the second code solves this problem.

Sample Input 2

5 2 -4
-2 5
100 41
100 40
-3 0
-6 -2
18 -13

Sample Output 2

2

Sample Input 3

3 3 0
100 -100 0
0 100 100
100 100 100
-100 100 100

Sample Output 3

0

All of them are Wrong Answer. Except yours.


parseInt(x) = parse(Int,x)

function main()
	N,M,C = map(parseInt, split(readline()))
  	b = map(parseInt, split(readline()))
  ans = 0
  	for i = 1:n
  		a = map(parseInt, split(readline()))
    	if a.*b + c > 0
      		ans += 1
    end
  end
  println(Int64(ans))
end
main()


function f()
    input = map(x -> parse(Int, x), split(chomp(readline()), " "))
    B = map(x -> parse(Int, x), split(chomp(readline()), " "))

    N = input[1]
    M = input[2]
    C = input[3]

    A = []
    ret = 0
    for i in 1:N
        a = map(x -> parse(Int, x), split(chomp(readline()), " "))
        if sum(a .* B) + C > 0
            ret += 1
        end
    end

    print(ret)
end
f()


input() = map(parse,split(readline()))
(N,M,C) = input()
B = input()
A = Array{Int}(N,M)
for n=1:N
	A[n,:] = input()
end
ans = 0
D = A*B+C
for d in D
  if d > 0
  	ans+=1
  end
end
println(ans)


function read_a_line(Type)
  map(x->parse(Type, x), split(readline()))
end

N,M,C = read_a_line(Int)

B = read_a_line(Int)
As = sum(map(x->sum(read_a_line(Int) .* B) + C == 0, 1:N))

println(As)


parseInt(x) = parse(Int,x)
parseMap(x::Array{SubString{String}, 1}) = map(parseInt, x)
function main()
    a,b,k = map(parseInt, split(readline()))
  ans = zeros(maximum([a b]));
  c  =1
  for i = 1:maximum([a b])
    if mod(a,i) == 0 && mod(b,i) == 0
      ans[c] = i
      c +=1
    end
  end
  println(Int64(ans[end-k+1]))
end
main()

Score : 200 points

Problem Statement

You are given positive integers A and B.

Find the K-th largest positive integer that divides both A and B.

The input guarantees that there exists such a number.

Constraints

All values in input are integers.

1 \leq A, B \leq 100

The K-th largest positive integer that divides both A and B exists.

K \geq 1

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the K-th largest positive integer that divides both A and B.

Sample Input 1

8 12 2

Sample Output 1

2

Three positive integers divides both 8 and 12: 1, 2 and 4.
Among them, the second largest is 2.

Sample Input 2

100 50 4

Sample Output 2

5

Sample Input 3

1 1 1

Sample Output 3

1


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

function main()
	a,b,k = readline() |> split |> parseMap
	count = 0
  	m = min(a,b)
	for i in 0:m-1
		if a%(m-i) == 0 && b%(m-i) == 0
			count += 1
		end
		if count == k
			println(m-i)
			break
		end
	end
end
main()


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

function main()
	a,b,k = readline() |> split |> parseMap
	x = gcd(a,b)
	count = 0
	for i in 1:x
		if x%i == 0
			count += 1
		end
		if count == k
			println(i)
			break
		end
	end
end
main()


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

function main()
    S = chomp(readline())
    zs = 0
    os = 0
    for s in S
        if s == '0'
            zs += 1
        else
            os += 1
        end
    end
    ans = 2 * min(zs, os)
    return ans
end

println(main())

Score : 300 points

Problem Statement

There are N cubes stacked vertically on a desk.

You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is 0, and blue if that character is 1.

You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent, and remove them. Here, the cubes that were stacked on the removed cubes will fall down onto the object below them.

At most how many cubes can be removed?

Constraints

1 \leq N \leq 10^5

|S| = N

Each character in S is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of cubes that can be removed.

Sample Input 1

0011

Sample Output 1

4

All four cubes can be removed, by performing the operation as follows:

Remove the second and third cubes from the bottom. Then, the fourth cube drops onto the first cube.

Remove the first and second cubes from the bottom.

Sample Input 2

11011010001011

Sample Output 2

12

Sample Input 3

0

Sample Output 3

0


function main()

  S = split(readline(),"")

  c0 = 0
  c1 = 1

  for i in 1:length(S)

    if S[i] == "0"
      c0 += 1
    else
      c1 += 1
    end

  end

  println(2 * min(c0,c1))

end

main()


function main()
   s = readline()
  so = s[:]
  while length(s) >1 && s != "1"^length(s) && s != "0"^length(s)
    i = length(s)
    while i >1
      if i == 2
        if s[1:2] == "00" || s[1:2] =="11"

        elseif length(s) >=3
           s = s[3:end]
        else
          s = ""
        end
      elseif i != length(s)
        if (parse(Int, s[i]) $ parse(Int, s[i-1])) == 1
           s = s[1:i-2] * s[i+1:end]
          i -=1
        end
      else
        if (parse(Int, s[i]) $ parse(Int, s[i-1])) == 1
           s = s[1:i-2]
          i -=1
        end
      end
      i -=1
    end
  end
  println(Int64(length(so) - length(s)))
end
main()


Score : 100 points

Problem Statement

You are given a string S as input. This represents a valid date in the year 2019 in the yyyy/mm/dd format. (For example, April 30, 2019 is represented as 2019/04/30.)

Write a program that prints Heisei if the date represented by S is not later than April 30, 2019, and prints TBD otherwise.

Constraints

S is a string that represents a valid date in the year 2019 in the yyyy/mm/dd format.

Input

Input is given from Standard Input in the following format:

S

Output

Print Heisei if the date represented by S is not later than April 30, 2019, and print TBD otherwise.

Sample Input 1

2019/04/30

Sample Output 1

Heisei

Sample Input 2

2019/11/01

Sample Output 2

TBD


function f()
    s = chomp(readline())
    #s = map(x->parse(String, x), split(chomp(readline()), " "))

    #print(input)
    #input = map(x->parse(Int, x), split(chomp(readline()), " "))
    #a = input[1]
    #b = input[2]

    if s <= "2019/04/30"
        print("Heisei")
    else
        print("TBD")
    end

end
f()


function f()

    #N = map(x->parse(Int, x), split(chomp(readline()), " "))
    N = parse(Int, chomp(readline()))
    x = []
    u = []

    goukei = 0

    for i in 1:N
        input = split(chomp(readline()), " ")
        x = parse(Float64, input[1])
        u = input[2]

        if u == "BTC"
            goukei = goukei + (x * 380000.0)
        else
            goukei = goukei + x
        end
    end
    print(goukei)

    #input = map(x->parse(Int, x), split(chomp(readline()), " "))
    #a = input[1]
    #b = input[2]

end
f()

Score : 200 points

Problem Statement

Takahashi received otoshidama (New Year's money gifts) from N of his relatives.

You are given N values x_1, x_2, ..., x_N and N strings u_1, u_2, ..., u_N as input. Each string u_i is either JPY or BTC, and x_i and u_i represent the content of the otoshidama from the i-th relative.

For example, if x_1 = 10000 and u_1 = JPY, the otoshidama from the first relative is 10000 Japanese yen; if x_2 = 0.10000000 and u_2 = BTC, the otoshidama from the second relative is 0.1 bitcoins.

If we convert the bitcoins into yen at the rate of 380000.0 JPY per 1.0 BTC, how much are the gifts worth in total?

Constraints

2 \leq N \leq 10

u_i = JPY or BTC.

If u_i = JPY, x_i is an integer such that 1 \leq x_i \leq 10^8.

If u_i = BTC, x_i is a decimal with 8 decimal digits, such that 0.00000001 \leq x_i \leq 100.00000000.

Input

Input is given from Standard Input in the following format:

N
x_1 u_1
x_2 u_2
:
x_N u_N

Output

If the gifts are worth Y yen in total, print the value Y (not necessarily an integer).

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

Sample Input 1

2
10000 JPY
0.10000000 BTC

Sample Output 1

48000.0

The otoshidama from the first relative is 10000 yen. The otoshidama from the second relative is 0.1 bitcoins, which is worth 38000.0 yen if converted at the rate of 380000.0 JPY per 1.0 BTC. The sum of these is 48000.0 yen.

Outputs such as 48000 and 48000.1 will also be judged correct.

Sample Input 2

3
100000000 JPY
100.00000000 BTC
0.00000001 BTC

Sample Output 2

138000000.0038

In this case, outputs such as 138001000 and 1.38e8 will also be judged correct.


function main()
	N,A,B,C=map(x->parse(Int,x),split(readline()))
	L=map(x->parse(Int,x),readlines())
	ans=114514
	for i=0:(1<<2N)-1
		now=0
		a=b=c=0
		for j=1:N
			g=(i>>2(j-1))%4
			if g==0
				if a>0
					now+=10
				end
				a+=L[j]
			elseif g==1
				if b>0
					now+=10
				end
				b+=L[j]
			elseif g==2
				if c>0
					now+=10
				end
				c+=L[j]
			end
		end
		if a>0&&b>0&&c>0
			now+=abs(A-a)+abs(B-b)+abs(C-c)
			ans=min(ans,now)
		end
	end
	println(ans)
end
main()

Score : 300 points

Problem Statement

You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.

Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:

Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1.

Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.

Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)

At least how much MP is needed to achieve the objective?

Constraints

3 \leq N \leq 8

1 \leq C < B < A \leq 1000

1 \leq l_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C
l_1
l_2
:
l_N

Output

Print the minimum amount of MP needed to achieve the objective.

Sample Input 1

5 100 90 80
98
40
30
21
80

Sample Output 1

23

We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.

Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2)

Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10)

Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1)

Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)

Sample Input 2

8 100 90 80
100
100
90
90
90
80
80
80

Sample Output 2

0

If we already have all bamboos of the desired lengths, the amount of MP needed is 0. As seen here, we do not necessarily need to use all the bamboos.

Sample Input 3

8 1000 800 100
300
333
400
444
500
555
600
666

Sample Output 3

243


function main()
	lines=readlines()
	A,B,Q=map(x->parse(Int,x),split(shift!(lines)))
	S=Array{Int}(0)
	push!(S,-10^12)
	for i=1:A
		push!(S,parse(Int,shift!(lines)))
	end
	push!(S,10^12)
	T=Array{Int}(0)
	push!(T,-10^12)
	for i=1:B
		push!(T,parse(Int,shift!(lines)))
	end
	push!(T,10^12)
	function bsearch(A,x)
		L=1
		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
	CS=Array{Int}(0)
	push!(CS,10^15)
	for i=1:A
		id=bsearch(T,S[i+1])
		push!(CS,min(S[i+1]-T[id-1],T[id]-S[i+1]))
	end
	push!(CS,10^15)
	CT=Array{Int}(0)
	push!(CT,10^15)
	for i=1:B
		id=bsearch(S,T[i+1])
		push!(CT,min(T[i+1]-S[id-1],S[id]-T[i+1]))
	end
	push!(CT,10^15)
	for x=lines
		x=parse(Int,x)
		ans=10^18
		ids=bsearch(S,x)
		idt=bsearch(T,x)
		ans=min(ans,S[ids]-x+CS[ids])
		ans=min(ans,x-S[ids-1]+CS[ids-1])
		ans=min(ans,T[idt]-x+CT[idt])
		ans=min(ans,x-T[idt-1]+CT[idt-1])
		println(ans)
	end
end
main()

Score : 400 points

Problem Statement

Along a road running in an east-west direction, there are A shrines and B temples.
The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.

Answer the following Q queries:

Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.


const lines=readlines()
state=0
input()=lines[global state+=1]
int(s::String)=parse(Int,s)
intSub(s::SubString{String})=parse(Int,s)
intLine(s::Array{SubString{String},1})=map(intSub,s)
function main()
    const INF=2^60
    a,b,q=input()|>split|>intLine
    s=Array{Int}(a+2)
    s[1],s[a+2]=-INF,INF
    for i in 2:a+1
        s[i]=int(input())
    end
    t=Array{Int}(b+2)
    t[1],t[b+2]=-INF,INF
    for i in 2:b+1
        t[i]=int(input())
    end
    for _ in 1:q
        x=int(input())
        m=INF
        i=searchsortedlast(s,x)
        j=searchsortedlast(t,x)
        for v in view(s,i:i+1)
            for w in view(t,j:j+1)
                m=min(m,abs(x-v)+abs(v-w),abs(x-w)+abs(w-v))
            end
        end
        println(m)
    end
end
main()


lines=readlines(open("/dev/fd/0"))
input()=shift!(lines)
int(s::String)=parse(Int,s)
intSub(s::SubString{String})=parse(Int,s)
intLine(s::Array{SubString{String},1})=map(intSub,s)
function main()
    INF=2^60
    a,b,q=input()|>split|>intLine
    s=[-INF]
    append!(s,[int(input())for i in 1:a])
    push!(s,INF)
    t=[-INF]
    append!(t,[int(input())for i in 1:b])
    push!(t,INF)
    for x in[int(input())for i in 1:q]
        m=INF
        i=searchsortedlast(s,x)
        j=searchsortedlast(t,x)
        for v in view(s,i:i+1)
            for w in view(t,j:j+1)
                m=min(m,abs(x-v)+abs(v-w),abs(x-w)+abs(w-v))
            end
        end
        println(m)
    end
end
main()


lines=readlines(open("/dev/fd/0"))
input()=shift!(lines)
int(s::String)=parse(Int,s)
int(s::SubString{String})=parse(Int,s)
int(s::Array{SubString{String},1})=map(int,s)
function main()
    INF=2^60
    a,b,q=input()|>split|>int
    s=Array{Int}(a+2)
    s[1],s[a+2]=-INF,INF
    for i in 2:a+1
      s[i]=int(input())
    end
    t=Array{Int}(b+2)
    t[1],t[b+2]=-INF,INF
    for i in 2:b+1
      t[i]=int(input())
    end
    for _ in 1:q
        x=int(input())
        m=INF
        i=searchsortedlast(s,x)
        j=searchsortedlast(t,x)
        for v in view(s,i:i+1)
            for w in view(t,j:j+1)
                m=min(m,abs(x-v)+abs(v-w),abs(x-w)+abs(w-v))
            end
        end
        println(m)
    end
end
main()


lines=readlines(open("/dev/fd/0"))
input()=shift!(lines)
int(s::String)=parse(Int,s)
int(s::SubString{String})=parse(Int,s)
int(s::Array{SubString{String},1})=map(int,s)
function main()
    INF=2^60
    a,b,q=input()|>split|>int
    s=[-INF]
    append!(s,[int(input())for i in 1:a])
    push!(s,INF)
    t=[-INF]
    append!(t,[int(input())for i in 1:b])
    push!(t,INF)
    for x in[int(input())for i in 1:q]
        m=INF
        i=searchsortedlast(s,x)
        j=searchsortedlast(t,x)
        for v in view(s,i:i+1)
            for w in view(t,j:j+1)
                m=min(m,abs(x-v)+abs(v-w),abs(x-w)+abs(w-v))
            end
        end
        println(m)
    end
end
main()


if isfile("in.txt");mystdin=open("in.txt","r");redirect_stdin(mystdin);end
macro p(z);:((x->parse(Int,x)).(split($z)));end
function main()
  a,b=@p(readline())
  print(b%a==0 ? a+b : b-a)
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Score : 100 points

Problem Statement

You are given positive integers A and B.

If A is a divisor of B, print A + B; otherwise, print B - A.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

If A is a divisor of B, print A + B; otherwise, print B - A.

Sample Input 1

4 12

Sample Output 1

16

As 4 is a divisor of 12, 4 + 12 = 16 should be printed.

Sample Input 2

8 20

Sample Output 2

12

Sample Input 3

1 1

Sample Output 3

2

1 is a divisor of 1.


a, b = map(parse, split(readline()))
if b % a == 0
    c = a + b
else
    c = a - b
end
print(c)


const lines=readlines()
state=0
input()=lines[global state+=1]
int(s::String)=parse(Int32,s)
intSub(s::SubString{String})=parse(Int32,s)
floatSub(s::SubString{String})=parse(Float64,s)
intLine()=map(intSub,split(input()))
function main()
    n,m=intLine()
    c=zeros(Int32,m)
    for i=1:n;for j=intLine()[2:end];c[j]+=1;end;end
    println(sum((x->x==n).(c)))
end
main()

Score : 200 points

Problem Statement

Katsusando loves omelette rice.

Besides, he loves crème brûlée, tenderloin steak and so on, and believes that these foods are all loved by everyone.

To prove that hypothesis, he conducted a survey on M kinds of foods and asked N people whether they like these foods or not.

The i-th person answered that he/she only likes the A_{i1}-th, A_{i2}-th, ..., A_{iK_i}-th food.

Find the number of the foods liked by all the N people.

Constraints

All values in input are integers.

1 \leq N, M \leq 30

1 \leq K_i \leq M

1 \leq A_{ij} \leq M

For each i (1 \leq i \leq N), A_{i1}, A_{i2}, ..., A_{iK_i} are distinct.

Constraints

Input is given from Standard Input in the following format:

N M
K_1 A_{11} A_{12} ... A_{1K_1}
K_2 A_{21} A_{22} ... A_{2K_2}
:
K_N A_{N1} A_{N2} ... A_{NK_N}

Output

Print the number of the foods liked by all the N people.

Sample Input 1

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

Sample Output 1

1

As only the third food is liked by all the three people, 1 should be printed.

Sample Input 2

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

Sample Output 2

0

Katsusando's hypothesis turned out to be wrong.

Sample Input 3

1 30
3 5 10 30

Sample Output 3

3


Score : 300 points

Problem Statement

There are N monsters, numbered 1, 2, ..., N.

Initially, the health of Monster i is A_i.

Below, a monster with at least 1 health is called alive.

Until there is only one alive monster, the following is repeated:

A random alive monster attacks another random alive monster.

As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.

Find the minimum possible final health of the last monster alive.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible final health of the last monster alive.

Sample Input 1

4
2 10 8 40

Sample Output 1

2

When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000


N,M=map(x->parse(Int,x),split(readline()))
A=sort(map(x->parse(Int,x),split(readline())))
num=[2,5,5,4,5,6,3,7,6]
dp=Array{Int}(N+1)
dp[1]=0
for i=1:N
	ret=-10^10
	for a=A
		if num[a]<=i
			ret=max(ret,dp[i-num[a]+1]+1)
		end
	end
	dp[i+1]=ret
end
id=N+1
while id>1
	now=-1
	for a=A
		if id>num[a]&&dp[id-num[a]]+1==dp[id]
			now=a
		end
	end
	print(now)
	id-=num[now]
end
println()

Score : 400 points

Problem Statement

Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:

Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).

The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

The output may not fit into a 64-bit integer type.

Sample Input 3

15 3
5 4 6

Sample Output 3

654


function main()

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

  count = 0

  for i in N:-1:2

    count += (N-2)

  end

  if count > K
    println("YES")
  else
    println("NO")
  end

end

main()

Score : 100 points

Problem Statement

Determine if we can choose K different integers between 1 and N (inclusive) so that no two of them differ by 1.

Constraints

1\leq N,K\leq 100

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

If we can choose K integers as above, print YES; otherwise, print NO.

Sample Input 1

3 2

Sample Output 1

YES

We can choose 1 and 3.

Sample Input 2

5 5

Sample Output 2

NO

Sample Input 3

31 10

Sample Output 3

YES

Sample Input 4

10 90

Sample Output 4

NO


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

function main()
	n,k = readline() |> split |> parseMap
	if 2*k<=n+1
		println("YES")
	else
		println("NO")
end
end

main()


a1, b1 = map(x->parse(Int, x), split(readline()))
a2, b2 = map(x->parse(Int, x), split(readline()))
a3, b3 = map(x->parse(Int, x), split(readline()))

x = zeros(Int, 4)
x[a1] += 1
x[a2] += 1
x[a3] += 1
x[b1] += 1
x[b2] += 1
x[b3] += 1

if maximum(x) == 3
    print("NO")
else
    print("YES")
end

Score : 200 points

Problem Statement

There are four towns, numbered 1,2,3 and 4.
Also, there are three roads. The i-th road connects different towns a_i and b_i bidirectionally.
No two roads connect the same pair of towns. Other than these roads, there is no way to travel between these towns, but any town can be reached from any other town using these roads.

Determine if we can visit all the towns by traversing each of the roads exactly once.

Constraints

1 \leq a_i,b_i \leq 4(1\leq i\leq 3)

a_i and b_i are different. (1\leq i\leq 3)

No two roads connect the same pair of towns.

Any town can be reached from any other town using the roads.

Input

Input is given from Standard Input in the following format:

a_1 b_1
a_2 b_2
a_3 b_3

Output

If we can visit all the towns by traversing each of the roads exactly once, print YES; otherwise, print NO.

Sample Input 1

4 2
1 3
2 3

Sample Output 1

YES

We can visit all the towns in the order 1,3,2,4.

Sample Input 2

3 2
2 4
1 2

Sample Output 2

NO

Sample Input 3

2 1
3 2
4 3

Sample Output 3

YES


function readint()
    parse(Int, strip(readline()))
end

L = readint()

started = 0
ended = 0
notstarted = 0

for _ in 1:L
    a = readint()
    notstarted, started, ended = notstarted + a, min(started + mod(a + 1, 2), notstarted), min(ended + a, started, notstarted)
end

println(min(notstarted, started))

Score : 600 points

Problem Statement

Snuke stands on a number line. He has L ears, and he will walk along the line continuously under the following conditions:

He never visits a point with coordinate less than 0, or a point with coordinate greater than L.

He starts walking at a point with integer coordinate, and also finishes walking at a point with integer coordinate.

He only changes direction at a point with integer coordinate.

Each time when Snuke passes a point with coordinate i-0.5, where i is an integer, he put a stone in his i-th ear.

After Snuke finishes walking, Ringo will repeat the following operations in some order so that, for each i, Snuke's i-th ear contains A_i stones:

Put a stone in one of Snuke's ears.

Remove a stone from one of Snuke's ears.

Find the minimum number of operations required when Ringo can freely decide how Snuke walks.

Constraints

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

0 \leq A_i \leq 10^9(1\leq i\leq L)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

L
A_1
:
A_L

Output

Print the minimum number of operations required when Ringo can freely decide how Snuke walks.

Sample Input 1

4
1
0
2
3

Sample Output 1

1

Assume that Snuke walks as follows:

He starts walking at coordinate 3 and finishes walking at coordinate 4, visiting coordinates 3,4,3,2,3,4 in this order.

Then, Snuke's four ears will contain 0,0,2,3 stones, respectively.
Ringo can satisfy the requirement by putting one stone in the first ear.

Sample Input 2

8
2
0
0
2
1
3
4
1

Sample Output 2

3

Sample Input 3

7
314159265
358979323
846264338
327950288
419716939
937510582
0

Sample Output 3

1


import Base.readline
function readint
    parse(Int, strip(readline()))
end

L = readint()

started = 0
notstarted = 0

for _ in 1:L
    a = readint()
    notstarted, started = notstarted + a, min(started + mod(a + 1, 2), notstarted)
end

println(min(notstarted, started))


function reader(pos::Int)
    global membuff
    val = 0
    sign = 1
    @inbounds r = membuff[pos]
    pos += 1
    while r < 0x30 || 0x39 < r
        sign = r == '-' ? -1 : sign
        @inbounds r = membuff[pos]
        pos += 1
    end
    while 0x30 <= r <= 0x39
        val = val * 10 + r - 0x30
        @inbounds r = membuff[pos]
        pos += 1
    end
    return val*sign, pos
end

const io = VERSION > v"1.0" ? stdin : STDIN
const membuff = read(io,1145141919)

#using LinearAlgebra: rank # 1.0 なら必要
function main()
    pos = 1
    N, pos = reader(pos)
    M, pos = reader(pos)
    a = zeros(Int,(N,M))
    for j=1:M,i=1:N # j が先に回る
        a[i,j], pos = reader(pos)
    end
    r = rank(a)
    mod = 998244353
    ans = powermod(2, N+M-1, mod) - powermod(2, N+M-r-1, mod)
    println( (ans+mod) % mod )
end

main()

Score : 800 points

Problem Statement

There is a square grid with N rows and M columns.
Each square contains an integer: 0 or 1. The square at the i-th row from the top and the j-th column from the left contains a_{ij}.

Among the 2^{N+M} possible pairs of a subset A of the rows and a subset B of the columns, find the number of the pairs that satisfy the following condition, modulo 998244353:

The sum of the |A||B| numbers contained in the intersection of the rows belonging to A and the columns belonging to B, is odd.

Constraints

1 \leq N,M \leq 300

0 \leq a_{i,j} \leq 1(1\leq i\leq N,1\leq j\leq M)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_{11} ... a_{1M}
:
a_{N1} ... a_{NM}

Output

Print the number of the pairs of a subset of the rows and a subset of the columns that satisfy the condition, modulo 998244353.

Sample Input 1

2 2
0 1
1 0

Sample Output 1

6

For example, if A consists of the first row and B consists of both columns, the sum of the numbers contained in the intersection is 0+1=1.

Sample Input 2

2 3
0 0 0
0 1 0

Sample Output 2

8


function main()

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

  if 2*maximum(L) < sum(L)
    println("Yes")
  else
    println("No")
  end

end

main()

Score : 200 points

Problem Statement

Determine if an N-sided polygon (not necessarily convex) with sides of length L_1, L_2, ..., L_N can be drawn in a two-dimensional plane.

You can use the following theorem:

Theorem: an N-sided polygon satisfying the condition can be drawn if and only if the longest side is strictly shorter than the sum of the lengths of the other N-1 sides.

Constraints

All values in input are integers.

3 \leq N \leq 10

1 \leq L_i \leq 100

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Output

If an N-sided polygon satisfying the condition can be drawn, print Yes; otherwise, print No.

Sample Input 1

4
3 8 5 1

Sample Output 1

Yes

Since 8 < 9 = 3 + 5 + 1, it follows from the theorem that such a polygon can be drawn on a plane.

Sample Input 2

4
3 8 4 1

Sample Output 2

No

Since 8 \geq 8 = 3 + 4 + 1, it follows from the theorem that such a polygon cannot be drawn on a plane.

Sample Input 3

10
1 8 10 5 8 12 34 100 11 3

Sample Output 3

No


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

function main()
	n,m = readline() |> split |> parseMap
	x = readline() |> split |> parseMap
	if n == 1
		println(maximum(x)-minimum(x))
	elseif n >= m
		println(0)
	else
		x = sort(x)
		a = Array{Int}(2,m-1)
		for i in 2:m
			a[1,i-1] = i-1
			a[2,i-1] = x[i]-x[i-1]
		end
		a = sortcols(a,by=t->t[2],rev=true)
		k = Int[]
		for i in 1:n-1
			push!(k,a[1,i])
		end
		k = sort(k)
		total = 0
		now = 1
		for i in 1:n-1
			total += x[k[i]]-x[now]
			now = k[i]+1
		end
		if now < m
			total += x[m]-x[now]
		end
		println(total)
	end
end

main()

Score : 300 points

Problem Statement

We will play a one-player game using a number line and N pieces.

First, we place each of these pieces at some integer coordinate.

Here, multiple pieces can be placed at the same coordinate.

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

Note that the coordinates where we initially place the pieces are already regarded as visited.

Find the minimum number of moves required to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

-10^5 \leq X_i \leq 10^5

X_1, X_2, ..., X_M are all different.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Find the minimum number of moves required to achieve the objective.

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

Initially, put the two pieces at coordinates 1 and 10.

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

3 7
-10 -3 0 9 -100 2 17

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0


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

function tsort(e::Array{Int,2},n,m)
	g = [Int[] for i in 1:n]
	d = zeros(Int,n)
	v = zeros(Int,n)
	for i in 1:m
		push!(g[e[1,i]],e[2,i])
		d[e[2,i]] += 1
	end
	s = Int[]
	for i in 1:n
		if d[i] == 0
			push!(s,i)
		end
	end
	l = Int[]
	while !isempty(s)
		t = pop!(s)
		push!(l,t)
		for i in 1:length(g[t])
			d[g[t][i]] -= 1
			if d[g[t][i]] == 0
				push!(s,g[t][i])
			end
		end
	end
	if length(l) == n
		l
	else
		false
	end
end

function main()
	n,m = readline() |> split |> parseMap
	e = Array{Int}(2,n-1+m)
	g = [Int[] for i in 1:n]
	for i in 1:n-1+m
		e[:,i] = readline() |> split |> parseMap
		push!(g[e[2,i]],e[1,i])
	end
	x = tsort(e,n,n-1+m)
	z = Array{Int}(n)
	for i in 1:n
		z[x[i]] = i
	end
	ans = Array{Int}(n)
	for i in 1:n
		o = 0
		oo = 0
		for j in 1:length(g[i])
			if z[g[i][j]]>oo
				oo = z[g[i][j]]
				o = g[i][j]
			end
		end
		ans[i] = o
	end
	for i in 1:n
		println(ans[i])
	end
end
main()

Score : 500 points

Problem Statement

There is a rooted tree (see Notes) with N vertices numbered 1 to N.
Each of the vertices, except the root, has a directed edge coming from its parent.
Note that the root may not be Vertex 1.

Takahashi has added M new directed edges to this graph.
Each of these M edges, u \rightarrow v, extends from some vertex u to its descendant v.

You are given the directed graph with N vertices and N-1+M edges after Takahashi added edges.
More specifically, you are given N-1+M pairs of integers, (A_1, B_1), ..., (A_{N-1+M}, B_{N-1+M}), which represent that the i-th edge extends from Vertex A_i to Vertex B_i.

Restore the original rooted tree.

Notes

For "tree" and other related terms in graph theory, see the article in Wikipedia, for example.

Constraints

3 \leq N

1 \leq M

N + M \leq 10^5

1 \leq A_i, B_i \leq N

A_i \neq B_i

If i \neq j, (A_i, B_i) \neq (A_j, B_j).

The graph in input can be obtained by adding M edges satisfying the condition in the problem statement to a rooted tree with N vertices.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_{N-1+M} B_{N-1+M}

Output

Print N lines.
In the i-th line, print 0 if Vertex i is the root of the original tree, and otherwise print the integer representing the parent of Vertex i in the original tree.

Note that it can be shown that the original tree is uniquely determined.

Sample Input 1

3 1
1 2
1 3
2 3

Sample Output 1

0
1
2

The graph in this input is shown below:

It can be seen that this graph is obtained by adding the edge 1 \rightarrow 3 to the rooted tree 1 \rightarrow 2 \rightarrow 3.

Sample Input 2

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

Sample Output 2

6
4
2
0
6
2


const lines=readlines(open("/dev/fd/0"))
let
    state=0
    global input
    input()=lines[state+=1]
end
int(s::String)=parse(Int32,s)
intSub(s::SubString{String})=parse(Int32,s)
intLine()=map(intSub,split(input()))
function main()
    a,b,_=sort(intLine())
    print(Int(a*b/2))
end
main()

Score : 100 points

Problem Statement

There is a right triangle ABC with ∠ABC=90°.

Given the lengths of the three sides, |AB|,|BC| and |CA|, find the area of the right triangle ABC.

It is guaranteed that the area of the triangle ABC is an integer.

Constraints

1 \leq |AB|,|BC|,|CA| \leq 100

All values in input are integers.

The area of the triangle ABC is an integer.

Input

Input is given from Standard Input in the following format:

|AB| |BC| |CA|

Output

Print the area of the triangle ABC.

Sample Input 1

3 4 5

Sample Output 1

6

This triangle has an area of 6.

Sample Input 2

5 12 13

Sample Output 2

30

This triangle has an area of 30.

Sample Input 3

45 28 53

Sample Output 3

630

This triangle has an area of 630.


parseInt(x) = parse(Int,x)
a=map(parseInt,split(readline()))

b=sort(a)
print(Int((b[1]*b[2])/2))


const lines=readlines()
state=0
input()=lines[global state+=1]
int(s::String)=parse(Int32,s)
intSub(s::SubString{String})=parse(Int32,s)
floatSub(s::SubString{String})=parse(Float64,s)
intLine()=map(intSub,split(input()))
function f(n::Int32)::Int32
    if n%2<1
        return div(n,2)
    else
        return 3n+1
    end
end
function main()
    s=int(input())
    a=zeros(Bool,10^7)
    a[s]=true
    n=1
    while true
        s=f(s)
        n+=1
        if a[s]
            println(n)
            break
        else
            a[s]=true
        end
    end
end
main()

Score : 200 points

Problem Statement

A sequence a=\{a_1,a_2,a_3,......\} is determined as follows:

The first term s is given as input.

Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.

a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.

Find the minimum integer m that satisfies the following condition:

There exists an integer n such that a_m = a_n (m > n).

Constraints

1 \leq s \leq 100

All values in input are integers.

It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum integer m that satisfies the condition.

Sample Input 1

8

Sample Output 1

5

a=\{8,4,2,1,4,2,1,4,2,1,......\}. As a_5=a_2, the answer is 5.

Sample Input 2

7

Sample Output 2

18

a=\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\}.

Sample Input 3

54

Sample Output 3

114


n = parse(Int,readline())
a = append!([0],parse.(split(readline())))
println(sum(filter(x->x>0,a[2:n+1]-a[1:n])))

Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221


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

function main()
	n,k = readline() |> split |> parseMap
	a = Array{Int}(3,n)
	for i in 1:n
		a[1:2,i] = readline() |> split |> parseMap
		a[3,i] = 0
	end
	a = sortcols(a,by=x->x[2],rev=true)
	pt1 = sum(a[2,1:k])+length(Set(a[1,1:k]))^2
	pt2 = 0
	t = 0
	msp = 0
	while t < k
		sp = Int[]
		for i in 1:n
			if a[1,i] in sp || a[3,i] == 1 || t >= k
			else
				push!(sp,a[1,i])
				pt2 += a[2,i]
				msp=max(msp,length(sp))
				a[3,i] = 1
				t += 1
			end
		end
	end
	pt2 += msp^2
	println(max(pt1,pt2))
end

main()

Score : 400 points

Problem Statement

There are N pieces of sushi. Each piece has two parameters: "kind of topping" t_i and "deliciousness" d_i.
You are choosing K among these N pieces to eat.
Your "satisfaction" here will be calculated as follows:

The satisfaction is the sum of the "base total deliciousness" and the "variety bonus".

The base total deliciousness is the sum of the deliciousness of the pieces you eat.

The variety bonus is x*x, where x is the number of different kinds of toppings of the pieces you eat.

You want to have as much satisfaction as possible.
Find this maximum satisfaction.

Constraints

1 \leq K \leq N \leq 10^5

1 \leq t_i \leq N

1 \leq d_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
t_1 d_1
t_2 d_2
.
.
.
t_N d_N

Output

Print the maximum satisfaction that you can obtain.

Sample Input 1

5 3
1 9
1 7
2 6
2 5
3 1

Sample Output 1

26

If you eat Sushi 1,2 and 3:

The base total deliciousness is 9+7+6=22.

The variety bonus is 2*2=4.

Thus, your satisfaction will be 26, which is optimal.

Sample Input 2

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

Sample Output 2

25

It is optimal to eat Sushi 1,2,3 and 4.

Sample Input 3

6 5
5 1000000000
2 990000000
3 980000000
6 970000000
6 960000000
4 950000000

Sample Output 3

4900000016

Note that the output may not fit into a 32-bit integer type.


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

function main()
	a = readline() |> split |> parseMap
	a = sort(a)
	if a[1]==1&&a[2]==4&&a[3]==7&&a[4]==9
		println("YES")
	else
		println("NO")
	end
end

main()

Score : 100 points

Problem Statement

You are given four digits N_1, N_2, N_3 and N_4. Determine if these can be arranged into the sequence of digits "1974".

Constraints

0 \leq N_1, N_2, N_3, N_4 \leq 9

N_1, N_2, N_3 and N_4 are integers.

Input

Input is given from Standard Input in the following format:

N_1 N_2 N_3 N_4

Output

If N_1, N_2, N_3 and N_4 can be arranged into the sequence of digits "1974", print YES; if they cannot, print NO.

Sample Input 1

1 7 9 4

Sample Output 1

YES

We can get 1974 by swapping N_2 and N_3.

Sample Input 2

1 9 7 4

Sample Output 2

YES

We already have 1974 before doing anything.

Sample Input 3

1 2 9 1

Sample Output 3

NO

Sample Input 4

4 9 0 8

Sample Output 4

NO


N = parse(Int, readline(STDIN))
H = parse(Int, readline(STDIN))
W = parse(Int, readline(STDIN))
println((N-H+1) * (N-W+1))

Score : 100 points

Problem Statement

It has been decided that a programming contest sponsored by company A will be held, so we will post the notice on a bulletin board.

The bulletin board is in the form of a grid with N rows and N columns, and the notice will occupy a rectangular region with H rows and W columns.

How many ways are there to choose where to put the notice so that it completely covers exactly HW squares?

Constraints

1 \leq H, W \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
H
W

Output

Print the answer.

Sample Input 1

3
2
3

Sample Output 1

2

There are two ways to put the notice, as follows:

### ...
### ###
... ###

Here, # represents a square covered by the notice, and . represents a square not covered.

Sample Input 2

100
1
1

Sample Output 2

10000

Sample Input 3

5
4
2

Sample Output 3

8


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

function main()
	n = readline() |> parseInt
	a,b = readline() |> split |> parseMap
	p = readline() |> split |> parseMap
	x = 0
	y = 0
	z = 0
	for i in 1:n
		if p[i]<=a
			x+=1
		elseif p[i]<=b
			y+=1
		else
			z+=1
		end
	end
	println(min(x,y,z))
end

main()

Score : 200 points

Problem Statement

You have written N problems to hold programming contests.
The i-th problem will have a score of P_i points if used in a contest.

With these problems, you would like to hold as many contests as possible under the following condition:

A contest has three problems. The first problem has a score not greater than A points, the second has a score between A + 1 and B points (inclusive), and the third has a score not less than B + 1 points.

The same problem should not be used in multiple contests.
At most how many contests can be held?

Constraints

3 \leq N \leq 100

1 \leq P_i \leq 20 (1 \leq i \leq N)

1 \leq A < B < 20

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A B
P_1 P_2 ... P_N

Output

Print the answer.

Sample Input 1

7
5 15
1 10 16 2 7 20 12

Sample Output 1

2

Two contests can be held by putting the first, second, third problems and the fourth, fifth, sixth problems together.

Sample Input 2

8
3 8
5 5 5 10 10 10 15 20

Sample Output 2

0

No contest can be held, because there is no problem with a score of A = 3 or less.

Sample Input 3

3
5 6
5 6 10

Sample Output 3

1


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

function main()
	n = readline() |> parseInt
	a,b = readline() |> split |> parseMap
	p = readline() |> split |> parseMap
	ca = 0
	cb = 0
	for i in 1:n
		if p[i] <= a
			ca += 1
		elseif p[i] <= b
			cb += 1
		end
	end
	println(min(ca,cb,n-ca-cb))
end

main()


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

function main()
	h,w = readline() |> split |> parseMap
	s = String["" for i in 1:h]
	for i in 1:h
		s[i] = readline() |> chomp
	end
	uf = Int[i for i in 1:h*w]
	for i in 1:h
		for j in 1:w
			if i>1
				if s[i][j]!=s[i-1][j]
					unite(uf,w*(i-1)+j,w*(i-2)+j)
				end
			end
			if i<h
				if s[i][j]!=s[i+1][j]
					unite(uf,w*(i-1)+j,w*i+j)
				end
			end
			if j>1
				if s[i][j]!=s[i][j-1]
					unite(uf,w*(i-1)+j,w*(i-1)+j-1)
				end
			end
			if j<w
				if s[i][j]!=s[i][j+1]
					unite(uf,w*(i-1)+j,w*(i-1)+j+1)
				end
			end
		end
	end
	b = Dict{Int,Int}()
	v = Dict{Int,Int}()
	for i in 1:h
		for j in 1:w
			if s[i][j]=='#'
				r = root(uf,w*(i-1)+j)
				if haskey(b,uf[r])
					b[uf[r]]+=1
				else
					b[uf[r]] = 1
				end
			else
				r = root(uf,w*(i-1)+j)
				if haskey(v,uf[r])
					v[uf[r]]+=1
				else
					v[uf[r]] = 1
				end
			end
		end
	end
	ans = 0
	for k in keys(b)
		if haskey(v,k)
			ans += b[k]*v[k]
		end
	end
	println(ans)
end

main()

Score : 300 points

Problem Statement

There is a grid with H rows and W columns, where each square is painted black or white.

You are given H strings S_1, S_2, ..., S_H, each of length W.
If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is #; if that square is painted white, the j-th character in the string S_i is ..

Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:

There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...

Constraints

1 \leq H, W \leq 400

|S_i| = W (1 \leq i \leq H)

For each i (1 \leq i \leq H), the string S_i consists of characters # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

Print the answer.

Sample Input 1

3 3
.#.
..#
#..

Sample Output 1

10

Some of the pairs satisfying the condition are ((1, 2), (3, 3)) and ((3, 1), (3, 2)), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

Sample Input 2

2 4
....
....

Sample Output 2

0

Sample Input 3

4 3
###
###
...
###

Sample Output 3

6


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

function main()
    a,b,c=parseMap(split(readline()))
    if a+b>=c
        println(b+c)
    else
        println(b*2+a+1)
    end
end
main()

Score : 200 points

Problem Statement

Takahashi has A untasty cookies containing antidotes, B tasty cookies containing antidotes and C tasty cookies containing poison.

Eating a cookie containing poison results in a stomachache, and eating a cookie containing poison while having a stomachache results in a death.
As he wants to live, he cannot eat one in such a situation.
Eating a cookie containing antidotes while having a stomachache cures it, and there is no other way to cure stomachaches.

Find the maximum number of tasty cookies that Takahashi can eat.

Constraints

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

A,B and C are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the maximum number of tasty cookies that Takahashi can eat.

Sample Input 1

3 1 4

Sample Output 1

5

We can eat all tasty cookies, in the following order:

A tasty cookie containing poison

An untasty cookie containing antidotes

A tasty cookie containing poison

A tasty cookie containing antidotes

A tasty cookie containing poison

An untasty cookie containing antidotes

A tasty cookie containing poison

Sample Input 2

5 2 9

Sample Output 2

10

Sample Input 3

8 8 1

Sample Output 3

9


Score : 100 points

Problem Statement

You are given an integer N that has exactly four digits in base ten.
How many times does 2 occur in the base-ten representation of N?

Constraints

1000 \leq N \leq 9999

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

1222

Sample Output 1

3

2 occurs three times in 1222. By the way, this contest is held on December 22 (JST).

Sample Input 2

3456

Sample Output 2

0

Sample Input 3

9592

Sample Output 3

1


N, H, W = map(x -> parse(Int64,x), split(readline(STDIN)))
data = [map(x -> parse(Int64,x), split(readline(STDIN))) for _ in 1:N]
function cuttable(h, w, board)
    return board[1] >= h && board[2] >= w
end
println(length(filter(board -> cuttable(H, W, board), data)))

Score : 200 points

Problem Statement

There are N rectangular plate materials made of special metal called AtCoder Alloy.
The dimensions of the i-th material are A_i \times B_i (A_i vertically and B_i horizontally).

Takahashi wants a rectangular plate made of AtCoder Alloy whose dimensions are exactly H \times W.
He is trying to obtain such a plate by choosing one of the N materials and cutting it if necessary.
When cutting a material, the cuts must be parallel to one of the sides of the material.
Also, the materials have fixed directions and cannot be rotated.
For example, a 5 \times 3 material cannot be used as a 3 \times 5 plate.

Out of the N materials, how many can produce an H \times W plate if properly cut?

Constraints

1 \leq N \leq 1000

1 \leq H \leq 10^9

1 \leq W \leq 10^9

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 H W
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the answer.

Sample Input 1

3 5 2
10 3
5 2
2 5

Sample Output 1

2

Takahashi wants a 5 \times 2 plate.

The dimensions of the first material are 10 \times 3. We can obtain a 5 \times 2 plate by properly cutting it.

The dimensions of the second material are 5 \times 2. We can obtain a 5 \times 2 plate without cutting it.

The dimensions of the third material are 2 \times 5. We cannot obtain a 5 \times 2 plate, whatever cuts are made. Note that the material cannot be rotated and used as a 5 \times 2 plate.

Sample Input 2

10 587586158 185430194
894597290 708587790
680395892 306946994
590262034 785368612
922328576 106880540
847058850 326169610
936315062 193149191
702035777 223363392
11672949 146832978
779291680 334178158
615808191 701464268

Sample Output 2

8


N, P = map(x -> parse(Int64,x), split(readline(STDIN)))
function primes(n)
    prime_factors = Dict{Int64, Int64}()
    divisor = 2

    while divisor^2 <= n
        count = 0
        while n % divisor == 0
            count += 1
            n ÷= divisor
        end
        if count > 0
            prime_factors[divisor] = count
        end
        divisor += 1
    end

    if n > 1
        prime_factors[n] = 1
    end

    return prime_factors
end
primes_dic = primes(P)
cal_k(pair) = pair.first ^  floor(Int, pair.second / N)
println(mapreduce(x->cal_k(x), *, primes_dic))

Score : 300 points

Problem Statement

There are N integers a_1, a_2, ..., a_N not less than 1.
The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P.

Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N.

Constraints

1 \leq N \leq 10^{12}

1 \leq P \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N P

Output

Print the answer.

Sample Input 1

3 24

Sample Output 1

2

The greatest common divisor would be 2 when, for example, a_1=2, a_2=6 and a_3=2.

Sample Input 2

5 1

Sample Output 2

1

As a_i are positive integers, the only possible case is a_1 = a_2 = a_3 = a_4 = a_5 = 1.

Sample Input 3

1 111

Sample Output 3

111

Sample Input 4

4 972439611840

Sample Output 4

206


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

function main()
	n = readline() |> parseInt
	a = zeros(Int,n)
	for i in 1:n
		a[i] = readline() |> parseInt
	end
	push!(a,0)
	a = sort(a)
	if a[n+1]==a[n]
		println(a[n]%2==0?"second":"first")
	else
		println((a[n+1]-a[n])%2==0?"second":"first")
	end
end

main()

Score : 500 points

Problem Statement

There is an apple tree that bears apples of N colors. The N colors of these apples are numbered 1 to N, and there are a_i apples of Color i.

You and Lunlun the dachshund alternately perform the following operation (starting from you):

Choose one or more apples from the tree and eat them. Here, the apples chosen at the same time must all have different colors.

The one who eats the last apple from the tree will be declared winner. If both you and Lunlun play optimally, which will win?

Constraints

1 \leq N \leq 10^5

1 \leq a_i \leq 10^9

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

If you will win, print first; if Lunlun will win, print second.

Sample Input 1

2
1
2

Sample Output 1

first

Let Color 1 be red, and Color 2 be blue. In this case, the tree bears one red apple and two blue apples.

You should eat the red apple in your first turn. Lunlun is then forced to eat one of the blue apples, and you can win by eating the other in your next turn.

Note that you are also allowed to eat two apples in your first turn, one red and one blue (not a winning move, though).

Sample Input 2

3
100000
30000
20000

Sample Output 2

second


parseInt(x) = parse(Int, x)

function main()
	n = parseInt(readline())
	a = Array{Int}(n)
	for i in 1:n
		a[i] = readline() |> parseInt
	end
	btm = minimum(a)
	k = 0
	for i in 1:n
		k += a[i]-btm
	end
	if btm == 1
		if k%2 == 0
			print("second")
		else
			print("first")
		end
	else
		if k%2 == 0
			print("second")
		else
			print("first")
		end
	end
end

main()


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

function main()
    s=chomp(readline())
    n=length(s)
    b=count(x->x=='B',s)
    w=count(x->x=='W',s)
    ans=0
    for i in 1:n
        if s[i]=='B'
            ans+=max(0,n-b-i)
        else
            ans+=max(0,i-w)
        end
    end
    println(ans)
end
main()

Score : 300 points

Problem Statement

There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.)
The state of each piece is represented by a string S of length N.
If S_i=B, the i-th piece from the left is showing black;
If S_i=W, the i-th piece from the left is showing white.

Consider performing the following operation:

Choose i (1 \leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.

Find the maximum possible number of times this operation can be performed.

Constraints

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

S_i=B or W

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum possible number of times the operation can be performed.

Sample Input 1

BBW

Sample Output 1

2

The operation can be performed twice, as follows:

Flip the second and third pieces from the left.

Flip the first and second pieces from the left.

Sample Input 2

BWBWBW

Sample Output 2

6


s=c=0
for a=chomp(readline())
	if a=='B'
		c+=1
	else
		s+=c
	end
end
println(s)