#1.4だと動くはずのコード
function main()
    n,m=parse.(Int,split(readline()))
    k=[parse.(Int,split(readline())) for i=1:m]
    p=parse.(Int,split(readline()))
    ans=0
    bit=[[i...] for i=Iterators.product(fill(0:1,n)...)][:]
    for i=bit
        ans+=all([isodd(count(x->i[x]==1,k[j][2:end]))==p[j] for j=1:m])
    end
    print(ans)
end
main()

Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

1 \leq N, M \leq 10

1 \leq k_i \leq N

1 \leq s_{ij} \leq N

s_{ia} \neq s_{ib} (a \neq b)

p_i is 0 or 1.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.

Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

Sample Input 2

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

Sample Output 2

0

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.

Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8


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

function main()
    n, m = ints()
    s = [ints()[2:end] for _ = 1:m]
    sset = [Set{Int}(s[i]) for i = 1:m]
    p = ints()
    cnt = 0
    for i = 0:(1<<n)-1
        set = Set{Int}()
        for bit = 0:n-1
            if ((1<<bit) & i) != 0
                push!(set, bit + 1)
            end
        end
        b = true
        for j = 1:m
            c = 0
            for e in set
                if e in sset[j]
                    c = 1 - c
                end
            end
            if c != p[j]
                b = false
            end
        end
        if b
            cnt += 1
        end
    end
    println(cnt)
end

main()

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

Submitted source-code file size, in bytes.

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

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


parseint(x) = parse(Int, x)
parseintmap(x::Array{SubString{String},1}) = map(parseint, x)

function main()
    n, m = readline() |> split |> parseintmap
    l = 1
    r = 10^5
    for _ in 1:m
        ll, rr = readline() |> split |> parseintmap
        l = max(l, ll)
        r = min(r, rr)
    end
    println(r - l + 1)
end

main()


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

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

  c=count(0:2^N-1) do x
    sw=fill(false,N)
    for i in 1:N
      y=2^(i-1)
      sw[i]=(x&y)==y
    end
    all(1:M) do i
      n=count(1:k[i]) do j
        sw[s[i,j]]
      end
      mod(n,2)==p[i]
    end
  end

  println(c)
end

main()


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

function refresh(st::Array{Int,1},lt::Array{Int,1},x)
	n = length(st)
	rt = n>>1
	if x<=rt
		st[x<<1] = min(st[x<<1],lt[x])
		st[x<<1+1] = min(st[x<<1+1],lt[x])
		lt[x<<1] = min(lt[x<<1],lt[x])
		lt[x<<1+1] = min(lt[x<<1+1],lt[x])
		lt[x] = 10^18
	end
end

function main()
	n,q = readline() |> split |> parseMap
	a = Tuple{Int,Int,Int}[(0,0,0) for i in 1:n]
	d = Dict{Int,Int}()
	for i in 1:n
		x,y,z = readline() |> split |> parseMap
		a[i] = (max(x-z,0),max(y-z-1,0),z)
		d[max(x-z,0)]=1
		d[max(y-z-1,0)]=1
	end
	b = zeros(Int,q)
	for i in 1:q
		b[i]=readline() |> parseInt
		d[b[i]]=1
	end
	m = length(d)
	tl = Int[]
	for i in keys(d)
		push!(tl,i)
	end
	tl = sort(tl)
	for i in 1:m
		d[tl[i]] = i
	end
	k = 0
	while 2^k<m
		k += 1
	end
	rt=2^k-1
	st = 10^18*ones(Int,2^(k+1)-1)
	lt = 10^18*ones(Int,2^(k+1)-1)
	for iter in 1:n
		ll = d[a[iter][1]]
		rr = d[a[iter][2]]
		v = a[iter][3]
		if rr==rt+1
			for i in k:-1:0
				refresh(st,lt,rr>>i)
			end
			st[rr]=min(st[rr],v)
			for i in 1:k
				st[rr>>i]=min(st[rr>>(i-1)],st[rr>>(i-1)+1])
			end
		else
			rp = Int[]
			rw = Int[]
			for i in k:-1:0
				l = (rt+ll)>>i
				r = (rt+rr)>>i
				push!(rp,l)
				if l<r
					push!(rp,r)
				end
				if r-l>1
					if l%2==0
						push!(rw,l+1)
					end
					if r%2==1
						push!(rw,r-1)
					end
				end
			end
			for i in rp
				refresh(st,lt,i)
			end
			for i in rw
				lt[i]=min(v,lt[i])
				st[i]=min(v,st[i])
			end
			for i in length(rp):-1:1
				if rp[i]<=rt
					st[rp[i]]=min(st[rp[i]<<1],st[rp[i]<<1+1])
				else
					st[rp[i]]=min(st[rp[i]],v)
				end
			end
		end
	end
	for i in 1:q
		ll=d[b[i]]
		rp = Int[]
		for i in k:-1:0
			l = (rt+ll)>>i
			refresh(st,lt,l)
		end
		println(st[rt+ll]<10^18?st[rt+ll]:-1)
	end
end
main()

Score : 500 points

Problem Statement

There is an infinitely long street that runs west to east, which we consider as a number line.

There are N roadworks scheduled on this street.
The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5.

Q people are standing at coordinate 0. The i-th person will start the coordinate 0 at time D_i, continue to walk with speed 1 in the positive direction and stop walking when reaching a blocked point.

Find the distance each of the Q people will walk.

Constraints

All values in input are integers.

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

0 \leq S_i < T_i \leq 10^9

1 \leq X_i \leq 10^9

0 \leq D_1 < D_2 < ... < D_Q \leq 10^9

If i \neq j and X_i = X_j, the intervals [S_i, T_i) and [S_j, T_j) do not overlap.

Input

Input is given from Standard Input in the following format:

N Q
S_1 T_1 X_1
:
S_N T_N X_N
D_1
:
D_Q

Output

Print Q lines. The i-th line should contain the distance the i-th person will walk or -1 if that person walks forever.

Sample Input 1

4 6
1 3 2
7 13 10
18 20 13
3 4 2
0
1
2
3
5
8

Sample Output 1

2
2
10
-1
13
-1

The first person starts coordinate 0 at time 0 and stops walking at coordinate 2 when reaching a point blocked by the first roadwork at time 2.

The second person starts coordinate 0 at time 1 and reaches coordinate 2 at time 3. The first roadwork has ended, but the fourth roadwork has begun, so this person also stops walking at coordinate 2.

The fourth and sixth persons encounter no roadworks while walking, so they walk forever. The output for these cases is -1.


function main()
	lines=readlines()
	A=Array{Tuple{Int,Int,Int}}(0)
	N,Q=map(x->parse(Int,x),split(shift!(lines)))
	X=Array{Int}(0)
	for i=1:N
		s,t,x=map(x->parse(Int,x),split(shift!(lines)))
		push!(A,(s-x,1,x))
		push!(A,(t-x,-1,x))
		push!(X,x)
	end
	sort!(X)
	unique!(X)
	M=Dict{Int,Int}(zip(sort(X),1:length(X)))
	for i=1:Q
		d=parse(Int,shift!(lines))
		push!(A,(d,2,i))
	end
	sort!(A)
	S=BitSet()
	ans=Array{Int}(Q)
	for (t,id,x)=A
		if id==2
			if isempty(S)
				ans[x]=-1
			else
				ans[x]=X[first(S)]
			end
		else
			x=M[x]
			if id==1
				push!(S,x)
			else
				pop!(S,x)
			end
		end
	end
	for a=ans
		println(a)
	end
end
main()


const lines=readlines(open("/dev/fd/0"))
input()=shift!(lines)
int(s::String)=parse(Int32,s)
intSub(s::SubString{String})=parse(Int32,s)
intLine()=map(intSub,split(input()))
function main()
    INF=10^10
    n,q=intLine()
    N0=2^length(bin(q-1))
    data=[INF for i in 1:2*N0]
    t=[intLine() for i in 1:n]
    d=[int(input()) for i in 1:q]
    for i in 1:n
        t[i][1]=searchsortedfirst(d,t[i][1]-t[i][3])+N0-1
        t[i][2]=searchsortedfirst(d,t[i][2]-t[i][3])+N0-1
    end
    sort!(t,by=x->-x[3])
    for (l,r,x) in t
        while l<r
            if r&1==1
                r-=1
                data[r]=x
            end
            if l&1==1
                data[l]=x
                l+=1
            end
            l>>=1
            r>>=1
        end
    end
    for k in N0-1:q+N0-2
        s=INF
        while k>=0
            s=min(s,data[k+1])
            k=(k-1)>>1
        end
        if s==INF
            println(-1)
        else
            println(s)
        end
    end
end
main()


function main()

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

  if A >= 13
    println(B)
  elseif 6 <= A <= 12
    println(div(B,2))
  else
    println(0)
  end

end

main()

Score : 100 points

Problem Statement

Takahashi, who is A years old, is riding a Ferris wheel.

It costs B yen (B is an even number) to ride the Ferris wheel if you are 13 years old or older, but children between 6 and 12 years old (inclusive) can ride it for half the cost, and children who are 5 years old or younger are free of charge. (Yen is the currency of Japan.)

Find the cost of the Ferris wheel for Takahashi.

Constraints

0 ≤ A ≤ 100

2 ≤ B ≤ 1000

B is an even number.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the cost of the Ferris wheel for Takahashi.

Sample Input 1

30 100

Sample Output 1

100

Takahashi is 30 years old now, and the cost of the Ferris wheel is 100 yen.

Sample Input 2

12 100

Sample Output 2

50

Takahashi is 12 years old, and the cost of the Ferris wheel is the half of 100 yen, that is, 50 yen.

Sample Input 3

0 100

Sample Output 3

0

Takahashi is 0 years old, and he can ride the Ferris wheel for free.


function main()
    age, fee = map(x -> parse(Int, x), split(readline()))

    if 6 <= age && age <= 12
        fee = div(fee, 2)
    elseif age <= 5
        fee = 0
    end

    println(fee)
end

main()


function main()
    age, fee = map(x -> parse(Int, x), split(readline()))

    if 6 <= age && age <= 12
        fee = fee / 2
    elseif age <= 5
        fee = 0
    end

    println(fee)
end

main()


let
  N, M = map(x -> parse(Int, x), split(readline()))
  if N <= 5
    print(0)
  elseif N <= 12
    print(M/2)
  else
    print(M)
  end
end


parseInt(x) = parse(Int,x)

function main()
  N = readline() |> split |> parseInt
  print(N[1] >= 13 ? print(N[2]) : (6 <= N[1] <= 12 ? print(N[2])/2 : print(0))
end
main()


r,d,x=parse.(split(readline()))
for i=1:10
  n=r*x-d
  println(n)
  x=n
end

Score : 200 points

Problem Statement

The development of algae in a pond is as follows.

Let the total weight of the algae at the beginning of the year i be x_i gram. For i≥2000, the following formula holds:

x_{i+1} = rx_i - D

You are given r, D and x_{2000}. Calculate x_{2001}, ..., x_{2010} and print them in order.

Constraints

2 ≤ r ≤ 5

1 ≤ D ≤ 100

D < x_{2000} ≤ 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r D x_{2000}

Output

Print 10 lines. The i-th line (1 ≤ i ≤ 10) should contain x_{2000+i} as an integer.

Sample Input 1

2 10 20

Sample Output 1

30
50
90
170
330
650
1290
2570
5130
10250

For example, x_{2001} = rx_{2000} - D = 2 \times 20 - 10 = 30 and x_{2002} = rx_{2001} - D = 2 \times 30 - 10 = 50.

Sample Input 2

4 40 60

Sample Output 2

200
760
3000
11960
47800
191160
764600
3058360
12233400
48933560


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()
    r,d,x=intLine()
    for i in 1:10
        x=r*x-d
        println(x)
    end
end
main()


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

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

  for i in 1:10
    x=r*x-D
    println(x)
  end
end

main()


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

function main()
N, M = parseMap(split(readline()))

L = []
R = []

max_l = -1
min_r = 10^5 + 1

for _ in 1:M
    l, r = parseMap(split(readline()))
    max_l = max(max_l, l)
    min_r = min(min_r, r)
end

println(max(min_r - max_l + 1, 0))

end

main()

Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000


let
  N, M = map(x -> parse(Int, x), split(readline()))
  cards = 1:N
  maxL = 1
  minR = 10^5
  for i=1:M
    L, R = map(x -> parse(Int, x), split(readline()))
    maxL = max(maxL, L)
    minR = min(minR, R)
    if maxL >= minR
      break
    end
  end
  print((maxL > minR) ? 0 : minR - maxL + 1)
end


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

function main()
  N,M = readline() |> split |> parseArray
  L,R=zeros(M),zeros(M)
  for i in 1:M
    L[i],R[i] = readline() |> split |> parseArray
  end

  result=minimum(R)- maximum(L)
  if result<0
    result+=1
  else
    result=0
  end

  println(result)
end

main()


parseInt(x) = parse(Int,x)

function main()
  N,M = map(x -> parse(Int,x),split(readline()))
  A = map(x -> parse(Int,x),split(readline()))

  for line in readlines()
    B,C = line |> split |> x -> map(parseInt,x)
    if(minimum(A) >= C)
        continue
      end
    for i = 1:B
      sort!(A)
      if(minimum(A) < C)
        A[1] = C
      end
    end
  end
  print(reduce(+,A))
end
main()

Score : 400 points

Problem Statement

You have N cards. On the i-th card, an integer A_i is written.

For each j = 1, 2, ..., M in this order, you will perform the following operation once:

Operation: Choose at most B_j cards (possibly zero). Replace the integer written on each chosen card with C_j.

Find the maximum possible sum of the integers written on the N cards after the M operations.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i, C_i \leq 10^9

1 \leq B_i \leq N

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N
B_1 C_1
B_2 C_2
\vdots
B_M C_M

Output

Print the maximum possible sum of the integers written on the N cards after the M operations.

Sample Input 1

3 2
5 1 4
2 3
1 5

Sample Output 1

14

By replacing the integer on the second card with 5, the sum of the integers written on the three cards becomes 5 + 5 + 4 = 14, which is the maximum result.

Sample Input 2

10 3
1 8 5 7 100 4 52 33 13 5
3 10
4 30
1 4

Sample Output 2

338

Sample Input 3

3 2
100 100 100
3 99
3 99

Sample Output 3

300

Sample Input 4

11 3
1 1 1 1 1 1 1 1 1 1 1
3 1000000000
4 1000000000
3 1000000000

Sample Output 4

10000000001

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


parseInt(x) = parse(Int,x)

function main()
  N,M = map(x -> parse(Int,x),split(readline()))
  A = map(x -> parse(Int,x),split(readline()))

  for line in readlines()
    B,C = line |> split |> x -> map(parseInt,x)
    for i = 1:B
      sort!(A)
      if(minimum(A) < C)
        A[1] = C
      end
    end
  end
  print(reduce(+,A))
end
main()


Score : 100 points

Problem Statement

You are given a string S of length N consisting of A, B and C, and an integer K which is between 1 and N (inclusive).
Print the string S after lowercasing the K-th character in it.

Constraints

1 ≤ N ≤ 50

1 ≤ K ≤ N

S is a string of length N consisting of A, B and C.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the string S after lowercasing the K-th character in it.

Sample Input 1

3 1
ABC

Sample Output 1

aBC

Sample Input 2

4 3
CABA

Sample Output 2

CAbA


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n,k=parseMap(split(readline()))
    s=chomp(readline())
    print(s[1:k-1])
    print(lowercase(s[k]))
  	print(s[k+1:n])
end
main()


N, K = map(x -> parse(Int, x), split(readline()))
p(i) = (1/N) * (1/2)^ceil(log(2, K/i))
print(sum(p.(1:N)))


n=parse(Int,readline())
a=div(n,100)
b=n%100
if 0<a<13
	if 0<b<13
		print("AMBIGUOUS")
	else
		print("MMYY")
	end
else
	if 0<b<13
		print("YYMM")
	else
		print("NA")
	end
end

Score : 200 points

Problem Statement

You have a digit sequence S of length 4. You are wondering which of the following formats S is in:

YYMM format: the last two digits of the year and the two-digit representation of the month (example: 01 for January), concatenated in this order

MMYY format: the two-digit representation of the month and the last two digits of the year, concatenated in this order

If S is valid in only YYMM format, print YYMM; if S is valid in only MMYY format, print MMYY; if S is valid in both formats, print AMBIGUOUS; if S is valid in neither format, print NA.

Constraints

S is a digit sequence of length 4.

Input

Input is given from Standard Input in the following format:

S

Output

Print the specified string: YYMM, MMYY, AMBIGUOUS or NA.

Sample Input 1

1905

Sample Output 1

YYMM

May XX19 is a valid date, but 19 is not valid as a month. Thus, this string is only valid in YYMM format.

Sample Input 2

0112

Sample Output 2

AMBIGUOUS

Both December XX01 and January XX12 are valid dates. Thus, this string is valid in both formats.

Sample Input 3

1700

Sample Output 3

NA

Neither 0 nor 17 is valid as a month. Thus, this string is valid in neither format.


let
  N, K = map(x -> parse(Int, x), split(readline()))
  function Σ(n,K)
    sum = 0
    for i = 1:n
      if i == K break end
      pow = log(2, K/i)
      sum += (1/n) * (1/2)^ceil(pow)
    end
    return sum
  end
  print(Σ(N, K))
end

Score : 300 points

Problem Statement

Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:

Throw the die. The current score is the result of the die.

As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.

The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.

You are given N and K. Find the probability that Snuke wins the game.

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.

Sample Input 1

3 10

Sample Output 1

0.145833333333

If the die shows 1, Snuke needs to get four consecutive heads from four coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^4 = \frac{1}{48}.

If the die shows 2, Snuke needs to get three consecutive heads from three coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^3 = \frac{1}{24}.

If the die shows 3, Snuke needs to get two consecutive heads from two coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^2 = \frac{1}{12}.

Thus, the probability that Snuke wins is \frac{1}{48} + \frac{1}{24} + \frac{1}{12} = \frac{7}{48} \simeq 0.1458333333.

Sample Input 2

100000 5

Sample Output 2

0.999973749998


let
  N, K = map(x -> parse(Int, x), split(readline()))
  function Σ(n,K)
    sum = 0
    for i = 1:n
      if i == K break end
      pow = log(2, K/i)
      sum += (pow%2 == 0) ? (1/n) * (1/2)^pow : (1/n) * (1/2)^ceil(pow)
    end
    return sum
  end
  print(Σ(N, K))
end


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

function main()
	n,k = readline() |> split |> parseMap
	p = 0.0
	p += max((n-k+1)/n,0)
	z = min(k-1,n)
	for i in 1:z
		x = i
		r = 0
		while x < k
			x *= 2
			r += 1
		end
		p += 1/(n*(2^r))
	end
	println(p)
end
main()


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

function main()
  N = readline() |> parseInt
  u, v, w=zeros(Int,N-1),zeros(Int,N-1),zeros(Int,N-1)
  for i in 1:N - 1
    u[i], v[i], w[i] = readline() |> split |> parseArray
  end

  # A = zeros(N, N)
  # for i in 1:N-1
  #   A[u[i], v[i]]=w[i]%2
  # end
  # A = Symmetric(A)
  A=Symmetric(sparse(u,v,map(x->(x%2)*2-1,w),N,N))
  # println(A)
  # A=full(A)
  # println(A)
  # A=factorize(A)
  # println(A)
  # vs, ks=eig(A)
  # vs, ks=eig(full(A))
  vs, ks=eigs(A, which=:SR)
  _,i=findmin(vs)
  result=map(x->x<0?0:1,ks[:,i])
  # println(result)
  for x in result
    println(x)
  end
end

main()

Score : 400 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in the tree connects Vertex u_i and Vertex v_i, and its length is w_i.
Your objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:

For any two vertices painted in the same color, the distance between them is an even number.

Find a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq u_i < v_i \leq N

1 \leq w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
u_1 v_1 w_1
u_2 v_2 w_2
.
.
.
u_{N - 1} v_{N - 1} w_{N - 1}

Output

Print a coloring of the vertices that satisfies the condition, in N lines.
The i-th line should contain 0 if Vertex i is painted white and 1 if it is painted black.

If there are multiple colorings that satisfy the condition, any of them will be accepted.

Sample Input 1

3
1 2 2
2 3 1

Sample Output 1

0
0
1

Sample Input 2

5
2 5 2
2 3 10
1 3 8
3 4 2

Sample Output 2

1
0
1
0
1


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

function main()
  N = readline() |> parseInt
  u, v, w=zeros(Int,N),zeros(Int,N),zeros(Int,N)
  for i in 1:N - 1
    u[i], v[i], w[i] = readline() |> split |> parseArray
  end

  A = zeros(N, N)
  for i in 1:N-1
    x, y = minmax(u[i], v[i])
    A[x, y]=w[i]%2
  end
  A = Symmetric(A)
  vs, ks=eig(A)
  _,i=findmin(vs)
  result=map(x->x<0?0:1,ks[:,i])
  # println(result)
  for x in result
    println(x)
  end
end

main()


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

function main()
	n,k = readline() |> split |> parseMap
	println(n-k+1)
end
main()

Score : 100 points

Problem Statement

Snuke has N integers: 1,2,\ldots,N.
He will choose K of them and give those to Takahashi.

How many ways are there to choose K consecutive integers?

Constraints

All values in input are integers.

1 \leq K \leq N \leq 50

Input

Input is given from Standard Input in the following format:

N K

Output

Print the answer.

Sample Input 1

3 2

Sample Output 1

2

There are two ways to choose two consecutive integers: (1,2) and (2,3).

Sample Input 2

13 3

Sample Output 2

11


function main()
R,G,B,N=map(x->parse(Int,x),split(readline()))
ans=0
for r=0:R:N,g=0:G:N
  T=N-r-g
  if T>=0&&T%B==0
    ans+=1
  end
end
println(ans)
end
main()

Score : 200 points

Problem Statement

Snuke has come to a store that sells boxes containing balls. The store sells the following three kinds of boxes:

Red boxes, each containing R red balls

Green boxes, each containing G green balls

Blue boxes, each containing B blue balls

Snuke wants to get a total of exactly N balls by buying r red boxes, g green boxes and b blue boxes.
How many triples of non-negative integers (r,g,b) achieve this?

Constraints

All values in input are integers.

1 \leq R,G,B,N \leq 3000

Input

Input is given from Standard Input in the following format:

R G B N

Output

Print the answer.

Sample Input 1

1 2 3 4

Sample Output 1

4

Four triples achieve the objective, as follows:

(4,0,0)

(2,1,0)

(1,0,1)

(0,2,0)

Sample Input 2

13 1 4 3000

Sample Output 2

87058


function main()
	N=parse(Int,readline())
	ca=cb=cab=0
	ans=0
	for _=1:N
		s=chomp(readline())
		n=length(s)
		for i=2:n
			if s[i-1]=='A'&&s[i]=='B'
				ans+=1
			end
		end
		if s[1]=='B'
			cb+=1
			if s[end]=='A'
				cab+=1
			end
		end
		if s[end]=='A'
			ca+=1
		end
	end
	println(ans+min(ca,cb)-(ca==cb==cab>0 ? 1 : 0))
end
main()

Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4


parseInt(x) = parse(Int, x)

function main()
  N = readline() |> parseInt
  s = Array{String}(N)
  for i=1:N
    s[i]=readline()|>strip
  end

  # w=count(b->b,map(ss->contains(ss,"AB"),s))
  w=sum(map(ss->(split(ss,"AB")|>length)-1,s))
  x=count(b->b,map(ss->ss[1]!='B'&&ss[end]=='A',s))
  y=count(b->b,map(ss->ss[1]=='B'&&ss[end]!='A',s))
  z=count(b->b,map(ss->ss[1]=='B'&&ss[end]=='A',s))
  # println([x,y,z,w])
  # result=z-1+(x>=1?1:0)+(y>=1?1:0)+((x>=1&&y>=1)?min(x-1,y-1):0)+w
  result=if z==0
    min(x,y)
  elseif x+y>0
    z+min(x,y)
  else
    z-1
  end
  result+=w

  println(result)
end

main()


parseInt(x) = parse(Int, x)

function main()
  N = readline() |> parseInt
  s = Array{String}(N)
  for i=1:N
    s[i]=readline()|>strip
  end

  # w=count(b->b,map(ss->contains(ss,"AB"),s))
  w=sum(map(ss->(split(ss,"AB")|>length)-1,s))
  x=count(b->b,map(ss->ss[1]!='B'&&ss[end]=='A',s))
  y=count(b->b,map(ss->ss[1]=='B'&&ss[end]!='A',s))
  z=count(b->b,map(ss->ss[1]=='B'&&ss[end]=='A',s))
  # println([x,y,z,w])
  result=z-1+(x>=1?1:0)+(y>=1?:1:0)+((x>=1&&y>=1)?min(x-1,y-1):0)+w

  println(result)
end

main()


parseInt(x) = parse(Int, x)

function main()
  N = readline() |> parseInt
  s = Array{String}(N)
  for i=1:N
    s[i]=readline()|>strip
  end

  w=count(b->b,map(ss->contains(ss,"AB"),s))
  x=count(b->b,map(ss->ss[1]!='B'&&ss[end]=='A',s))
  y=count(b->b,map(ss->ss[1]=='B'&&ss[end]!='A',s))
  z=count(b->b,map(ss->ss[1]=='B'&&ss[end]=='A',s))
  # println([x,y,z,w])
  result=z-1+(x>=1?1:0)+(y>=1?:1:0)+((x>=1&&y>=1)?min(x-1,y-1):0)+w

  println(result)
end

main()


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

function main()
	n = readline() |> parseInt
	s = ["" for i in 1:n]
	for i in 1:n
		s[i] = chomp(readline())
	end
	k = 0
	h = 0
	t = 0
	b = 0
	for i in 1:n
		l = length(s[i])
		for j in 2:l
			if s[i][j-1] == 'A' && s[i][j] == 'B'
				k += 1
			end
		end
		if s[i][1] == 'B' && s[i][l] == 'A'
			b += 1
		elseif s[i][1] == 'B'
			h += 1
		elseif s[i][l] == 'A'
			t += 1
		end
	end
	if h+b>t||b+t>h
		k += max(min(h+b,t),min(h,b+t))+max(b-1,0)
	elseif h+b<=t
		k += h+b
	elseif b+t<=h
		k += b+t
	end
	println(k)
end
main()


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

function main()
    n=parseInt(readline())
    ans=0
    for i in 1:trunc(Int,sqrt(n))+1
        if n%i==0
            if i>1 && n%(i-1)==n÷(i-1)
                ans+=i-1
            end
            if n%(n÷i-1)==n÷(n÷i-1)
                ans+=n÷i-1
            end
        end
    end
    println(ans)
end
main()

Score : 500 points

Problem Statement

Snuke received a positive integer N from Takahashi.
A positive integer m is called a favorite number when the following condition is satisfied:

The quotient and remainder of N divided by m are equal, that is, \lfloor \frac{N}{m} \rfloor = N \bmod m holds.

Find all favorite numbers and print the sum of those.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

8

Sample Output 1

10

There are two favorite numbers: 3 and 7. Print the sum of these, 10.

Sample Input 2

1000000000000

Sample Output 2

2499686339916

Watch out for overflow.


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

Score : 100 points

Problem Statement

A biscuit making machine produces B biscuits at the following moments: A seconds, 2A seconds, 3A seconds and each subsequent multiple of A seconds after activation.

Find the total number of biscuits produced within T + 0.5 seconds after activation.

Constraints

All values in input are integers.

1 \leq A, B, T \leq 20

Input

Input is given from Standard Input in the following format:

A B T

Output

Print the total number of biscuits produced within T + 0.5 seconds after activation.

Sample Input 1

3 5 7

Sample Output 1

10

Five biscuits will be produced three seconds after activation.

Another five biscuits will be produced six seconds after activation.

Thus, a total of ten biscuits will be produced within 7.5 seconds after activation.

Sample Input 2

3 2 9

Sample Output 2

6

Sample Input 3

20 20 19

Sample Output 3

0


function main()
  a,b,t = map(x -> parse(Int, x), split(readline(), " "))
  hoge = div(t,a)
  println(hoge * b)
end

main()


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

function main()
	n = readline() |> parseInt()
	v = readline() |> split |> parseMap
	c = readline() |> split |> parseMap
	k = 0
	for i in 1:n
		if v[i]-c[i] > 0
			k += v[i]-c[i]
		end
	end
	println(k)
end
main()

Score : 200 points

Problem Statement

There are N gems. The value of the i-th gem is V_i.

You will choose some of these gems, possibly all or none, and get them.

However, you need to pay a cost of C_i to get the i-th gem.

Let X be the sum of the values of the gems obtained, and Y be the sum of the costs paid.

Find the maximum possible value of X-Y.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq C_i, V_i \leq 50

Input

Input is given from Standard Input in the following format:

N
V_1 V_2 ... V_N
C_1 C_2 ... C_N

Output

Print the maximum possible value of X-Y.

Sample Input 1

3
10 2 5
6 3 4

Sample Output 1

5

If we choose the first and third gems, X = 10 + 5 = 15 and Y = 6 + 4 = 10.
We have X-Y = 5 here, which is the maximum possible value.

Sample Input 2

4
13 21 6 19
11 30 6 15

Sample Output 2

6

Sample Input 3

1
1
50

Sample Output 3

0


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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	b = Array{Int}(n)
	for i in 1:n
		b[i] = abs(a[i])
	end
	println(sum(b)-2*minimum(b))
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 = 0
	for i in 1:n
		if s[i] == '#'
			l = i-1
			break
		end
	end
	r = 0
	for i in 0:n-1
		if s[n-i] == '.'
			r = i
			break
		end
	end
	println(min(b-r, (n-b)-l))
end
main()

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


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] == '.'
			c += 1
		end
	end
	if c == cc
		println(0)
	else
		println(min(c,cc-c))
	end
end
main()


A,B,C=map(x->parse(Int,x),split(readline()))
if A>B
  A,B=B,A
end
println(A<=C<=B?"Yes":"No")

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


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())
    b=0
    w=count(x->x=='.',s)
    arr=Vector{Int}([w])
    for i in s
        if i=='#'
            b+=1
        else
            w-=1
        end
        push!(arr,b+w)
    end
    println(minimum(arr))
end
main()


# abcde -> String
readline_() = readline() |> chomp
# a b c d e -> Array{SubString{String},1}
readline_array_string() = readline() |> chomp |> split
# 1 2 3 4 5 -> Array{Int64, 1}
readline_array_int() = [parse(Int, x) for x in readline() |> chomp |> split]
read_int() = parse(Int, readline() |> chomp)

function omit(str)
    count = 0
    l = length(str)
    ind = Int[]
    for i = 1:l-1
        if str[i:i+1] == "#."
            count += 1
        else
            push!(ind, i)
        end
    end
    if !isempty(ind)
        count += omit(str[ind])
    end
    return count
end

n = read_int()
str = readline_()
value = omit(str)
print(value)


const lines=readlines(open("/dev/fd/0"))
input()=shift!(lines)
int(s::String)=parse(Int32,s)
intSub(s::SubString{String})=parse(Int32,s)
intLine()=map(intSub,split(input()))
function main()
    a,b=intLine()
    x=0
    for i in 1:2
        if a>b
            x+=a
            a-=1
        else
            x+=b
            b-=1
        end
    end
    println(x)
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


a,b = map(x->Parse(Int, x), split(readline()))
println(max(a+b, 2max(a,b)-1)


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

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

flippoints = Int[]
for (i, f) in enumerate(flip)
    if f == 1
        push!(flippoints, i)
    end
end

# println(xs)
# println(ys)
# println(flip)
# println(flippoints)

values = Int[]

fn = length(flippoints)
i = 1
while i <= fn
    b = false
    left0 = 1
    if 1 <= i-1
        left0 = max(1, min(flippoints[i-1] + 1, flippoints[i] - 1))
    end
    left1 = flippoints[i]
    right1 = flippoints[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


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n=parseInt(readline())
    h=parseMap(split(readline()))
    MAX=0
    ans=0
    for i in 1:n
        if h[i]>=MAX
            ans+=1
            MAX=h[i]
        end
    end
    println(ans)
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


w=1
wc=0
b=0
bc=0
for c=chomp(readline())
	c=parse(Int,c)
	wc+=w!=c
	bc+=b!=c
	w=1-w
	b=1-b
end
println(min(wc,bc))

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)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)
function bsearch(a,x);l,r=1,length(a);while l<=r;m=l+div(r-l,2);if a[m]<x;l=m+1;else;r=m-1;end;end;return(l-1);end

function main()
	s = chomp(readline())
	n = length(s)
	a = 0
	b = 0
	for i in 1:n
		if i%2==0
			if s[i]=='0'
				a += 1
			else
				b += 1
			end
		else
			if s[i]=='1'
				a += 1
			else
				b += 1
			end
		end
	end
	println(min(a,b))
end
main()


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)
    biggest = 0
    if xs[1] == 1
        val = sum(seq[1:min(2k+1, m)])
        biggest = val
        for i = 3:2:m
            val += sum(seq[min(2k+i-1, m):min(2k+i, m)]) - sum(seq[i-2:i-1])
            if biggest < val
                biggest = val
            end
        end
    else
        val = sum(seq[1:min(2k, m)])
        biggest = val
        val = sum(seq[2:min(2k+2, m)])
        if biggest < val
            biggest = val
        end
        for i = 4:2:m
            val += sum(seq[min(2k+i-1, m):min(2k+i, m)]) - sum(seq[i-2:i-1])
            if biggest < val
                biggest = val
            end
        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)

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 main()
    n, k = [parse(Int, x) for x in split(chomp(readline()))]
    str = readline() |> chomp
    xs = zeros(Int, n)
    for (i, s) in enumerate(str)
        xs[i] = parse(Int, s)
    end

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

    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

main()


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

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

flippoints = Int[]
for (i, f) in enumerate(flip)
    if f == 1
        push!(flippoints, i)
    end
end

# println(xs)
# println(ys)
# 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 = flippoints[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()

	if coordinate[num_antennas] - coordinate[1] > distance_max
		return ":("
	else
		return "Yay!"
	end

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


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    a=[parseInt(readline()) for i in 1:5]
    modten=[i%10+10*(i%10==0) for i in a]
    smod=sort(modten,rev=true)
    ans=0
    for i in 1:5
        for j in 1:5
            if smod[i]==modten[j]
                ans+=a[j]
                if i<5
                    ans+=10-modten[j]
                end
                modten[j]=100
                break
            end
        end
    end
    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)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()

capacitys = zeros(Int, 5)
N = parseInt(readline())

for i in 1:5
    capacitys[i] = parseInt(readline())
end

println(cld(N, minimum(capacitys)) + 4)

end

main()

Score: 300 points

Problem Statement

In 2028 and after a continuous growth, AtCoder Inc. finally built an empire with six cities (City 1, 2, 3, 4, 5, 6)!

There are five means of transport in this empire:

Train: travels from City 1 to 2 in one minute. A train can occupy at most A people.

Bus: travels from City 2 to 3 in one minute. A bus can occupy at most B people.

Taxi: travels from City 3 to 4 in one minute. A taxi can occupy at most C people.

Airplane: travels from City 4 to 5 in one minute. An airplane can occupy at most D people.

Ship: travels from City 5 to 6 in one minute. A ship can occupy at most E people.

For each of them, one vehicle leaves the city at each integer time (time 0, 1, 2, ...).

There is a group of N people at City 1, and they all want to go to City 6.

At least how long does it take for all of them to reach there?
You can ignore the time needed to transfer.

Constraints

1 \leq N, A, B, C, D, E \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A
B
C
D
E

Output

Print the minimum time required for all of the people to reach City 6, in minutes.

Sample Input 1

5
3
2
4
3
5

Sample Output 1

7

One possible way to travel is as follows.
First, there are N = 5 people at City 1, as shown in the following image:

In the first minute, three people travels from City 1 to City 2 by train. Note that a train can only occupy at most three people.

In the second minute, the remaining two people travels from City 1 to City 2 by train, and two of the three people who were already at City 2 travels to City 3 by bus. Note that a bus can only occupy at most two people.

In the third minute, two people travels from City 2 to City 3 by train, and another two people travels from City 3 to City 4 by taxi.

From then on, if they continue traveling without stopping until they reach City 6, all of them can reach there in seven minutes.

There is no way for them to reach City 6 in 6 minutes or less.

Sample Input 2

10
123
123
123
123
123

Sample Output 2

5

All kinds of vehicles can occupy N = 10 people at a time.
Thus, if they continue traveling without stopping until they reach City 6, all of them can reach there in five minutes.

Sample Input 3

10000000007
2
3
5
7
11

Sample Output 3

5000000008

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


function f()
    N = parse(Int, chomp(readline()))
    A = parse(Int, chomp(readline()))
    B = parse(Int, chomp(readline()))
    C = parse(Int, chomp(readline()))
    D = parse(Int, chomp(readline()))
    E = parse(Int, chomp(readline()))

    if N <= A && N <= B && N <= C && N <= D && N <= E
        print("5")
    else
        tmp = N / findmin([A, B, C, D, E])[1] + 4
        print(floor(tmp))
    end

end
f()


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

function main()
    x, y, z, k = readline() |> split |> parseMap
    va = sort(readline() |> split |> parseMap, rev=true)
    vb = sort(readline() |> split |> parseMap, rev=true)
    vc = sort(readline() |> split |> parseMap, rev=true)
    result = sort([va[ai]+vb[bi]+vc[ci] for ai in 1:x for bi in 1:y for ci in 1:z if ai * bi * ci<=k], rev=true)
    for i in 1:k
        println(result[i])
    end
end

main()

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.


parseInt(x) = parse(Int,x)

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

function main()
    A,B,C = readline() |> split |> parseMap
    if A==B==C
        println("Yes")
    else
        println("No")
    end
end

main()

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 exawizards2019_A()

    # STEP.01
    # read out the given integers
    A, B, C = Base.parse.(Base.split(Base.readline(Base.STDIN)))

    # STEP.02
    # determine if there exists an equilateral triangle whose sides have lengths A, B and C.
    if Base.isequal(A, B) && Base.isequal(B, C)
        Base.println(Base.STDOUT, "Yes")
    else
        Base.println(Base.STDOUT, "No")
    end

    # STEP.TRUE_END
    return Base.nothing

end

Main.exawizards2019_A()


c=readline()[1]
if c=='A'
  print('T')
elseif c=='T'
  print('A')
elseif c=='G'
  print('C')
else
  print('G')
end

Score : 100 points

Problem Statement

On the Planet AtCoder, there are four types of bases: A, C, G and T. A bonds with T, and C bonds with G.

You are given a letter b as input, which is A, C, G or T. Write a program that prints the letter representing the base that bonds with the base b.

Constraints

b is one of the letters A, C, G and T.

Input

Input is given from Standard Input in the following format:

b

Output

Print the letter representing the base that bonds with the base b.

Sample Input 1

A

Sample Output 1

T

Sample Input 2

G

Sample Output 2

C


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

function main()
S = collect(chomp(readline()))

ans = 0
for len in 0:length(S)
    for i in 1:length(S) - len
        for s in  S[i:i + len]
            if !(s in ['A', 'C', 'G', 'T'])
                @goto end_try_s
            end
        end
        ans = len + 1
    @label end_try_s
    end

end

println(ans)

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


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

function main()
	s = chomp(readline())
	sflg = 0
	count = 0
	lcount = 0
	for i in 1:length(s)
		if sflg == 0
			if s[i] == 'A' || s[i] == 'C' || s[i] == 'G' || s[i] == 'T'
				sflg = 1
				lcount += 1
				count = max(count, lcount)
			end
		else
			if s[i] == 'A' || s[i] == 'C' || s[i] == 'G' || s[i] == 'T'
				lcount += 1
				count = max(count, lcount)
			else
				lcount = 0
				sflg = 0
			end
		end
	end
	println(count)
end
main()


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

function main()
    N, Q = parseMap(split(readline()))
    S = collect(chomp(readline()))
    A = zeros(Int, length(S))
    for i in 1:N-1
        println(S[i:i+1] )
        if S[i:i+1] == ['A', 'C']
            A[i + 1] = A[i] + 1
        else
            A[i + 1] = A[i]
        end
    end
    for _ in 1:Q
        l, r = parseMap(split(readline()))
        println(A[r] - A[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.


k()=readline()|>chomp
s()=map(parse,k()|>split)
n,q=s()
t=k()
a=zeros(Int,n)
c=0
for i=1:n-1
if t[i]=='A' && t[i+1]=='C'
c+=1
end
a[i+1]=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 = readline() |> parseInt
	b = readline() |> split |> parseMap
	flg = 0
	for i in 1:n
		if b[i] > i
			flg = 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
	end
	for i in 0:n-1
		println(ans[n-i])
	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


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()

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)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

using DataStructures

function main()
    N, M = parseMap(split(readline()))
    D = SortedDict{Int, Int}()
    for _ in 1:N
        A, B = parseMap(split(readline()))
        if haskey(D, A)
            D[A] += B
        else
            D[A] = B
        end
    end
    count = 0
    money = 0
    for d in D
        if M - count >= d[2]
            count += d[2]
            money += d[1] * d[2]
        elseif 0 < M - count < money
            money += d[1] * (M - count)
            break
        else
            break
        end
    end
    println(money)
end

main()

Score : 300 points

Problem Statement

Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.

There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.

What is the minimum amount of money with which he can buy M cans of energy drinks?

It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^5

B_1 + ... + B_N \geq M

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.

Sample Input 1

2 5
4 9
2 4

Sample Output 1

12

With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.

Sample Input 2

4 30
6 18
2 5
3 10
7 9

Sample Output 2

130

Sample Input 3

1 100000
1000000000 100000

Sample Output 3

100000000000000

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


N,M=map(x->parse(Int,x),split(readline()))
A=Array{Pair{Int,Int}}(0)
for i=1:N
	a,b=map(x->parse(Int,x),split(readline()))
	push!(A,a=>b)
end
sort!(A)
ans=0
for (a,b)=A
	if M>b
		ans+=a*b
		M-=b
	else
		ans+=a*M
		M=0
	end
end
println(ans)


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

function main()
	n,m = readline()|> split |> parseMap
	a = Tuple[]
	for i in 1:n
		x,y=readline()|> split |> parseMap
		push!(a,(x,y))
	end
	a = sort(a,by=x->x[1])
	k = 0
	c = 0
	for i in 1:n
		if m-k <= a[i][2]
			c += a[i][1]*(m-k)
			break
		else
			k += a[i][2]
			c += a[i][1]*a[i][2]
		end
	end
	println(c)
end

main()


function main()

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

  count = 0

  for i in 1:A*B

    if A % i == 0 && B % i == 0
      count += 1
    end

    if count == K
      println(i)
      break
    end

  end

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


a,b,k=map(x->parse(x),split(readline()))
i=101
while k>0
	i-=1
	if a%i==0&&b%i==0
		k-=1
	end
end
print(i)


function main()
  s = readline()
  so = s[:]
  while lentgh(s) >1 && s != "1"^length(s) && s != "0"^length(s)
    for i = lenght(s)-1:-2:1
      if i == 1
        if s[1:2] == "00" || s[1:] =="11"
        elseif length(s) >=3
          s = s[3:]
        else
          s =""
        end
      else
        if (parse(Int, s[i]) $ parse(Int, s[i-1])) == 0
          s = s[1:i-2] * s[i+1:]
        end
      end
    end
  if length(s) >=2
    for i = lenght(s):-2:1
      if i == 1
        if s[1:2] == "00" || s[1:] =="11"
        elseif length(s) >=3
          s = s[3:]
        else
          s =""
        end
      else
        if (parse(Int, s[i]) $ parse(Int, s[i-1])) == 0
          s = s[1:i-2] * s[i+1:]
        end
      end
    end
    end
  end
  println(Int64(length(so) - length(s)))
end
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()
	lines=readlines()
	N,M=map(x->parse(Int,x),split(shift!(lines)))
	A=Array{Pair{Int,Int}}(0)
	for s=lines
		a,b=map(x->parse(Int,x),split(s))
		push!(A,a=>b)
	end
	reverse!(A)
	size=ones(Int,N)
	parent=collect(1:N)
	function find(u)
		if u==parent[u]
			u
		else
			parent[u]=find(parent[u])
		end
	end
	ans=[]
	now=div(N*(N-1),2)
	for (a,b)=A
		push!(ans,now)
		a=find(a)
		b=find(b)
		if a!=b
			now-=size[a]*size[b]
			if size[a]>size[b]
				size[a]+=size[b]
				parent[b]=a
			else
				size[b]+=size[a]
				parent[a]=b
			end
		end
	end
	for c=reverse(ans)
		println(c)
	end
end
main()

Score : 400 points

Problem Statement

There are N islands and M bridges.

The i-th bridge connects the A_i-th and B_i-th islands bidirectionally.

Initially, we can travel between any two islands using some of these bridges.

However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge.

Let the inconvenience be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining.

For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i < B_i \leq N

All pairs (A_i, B_i) are distinct.

The inconvenience is initially 0.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

In the order i = 1, 2, ..., M, print the inconvenience just after the i-th bridge collapses.
Note that the answer may not fit into a 32-bit integer type.

Sample Input 1

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

Sample Output 1

0
0
4
5
6

For example, when the first to third bridges have collapsed, the inconvenience is 4 since we can no longer travel between the pairs (1, 2), (1, 3), (2, 4) and (3, 4).

Sample Input 2

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

Sample Output 2

8
9
12
14
15

Sample Input 3

2 1
1 2

Sample Output 3

1


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

function main()
	n = readline() |> parseInt
	total = 0
	for i in 1:n
		x,u = readline() |> split
		if u == "JPY"
			total += parse(Float64,x)
		else
			total += parse(Float64,x)*380000
		end
	end
	println(total)
end
main()

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()
	lines=readlines()
	A,B,Q=map(x->parse(Int,x),split(shift!(lines)))
	S=Array{Int}(0)
	push!(S,-10^10)
	for i=1:A
		push!(S,parse(Int,shift!(lines)))
	end
	push!(S,10^10)
	T=Array{Int}(0)
	push!(T,-10^10)
	for i=1:B
		push!(T,parse(Int,shift!(lines)))
	end
	push!(T,10^10)
	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.


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

  println(rem(b, a) == 0 ? a + b : b - a)
end

main()

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(x->parse(x),split(readline()))
println(ifelse(b%a==0,a+b,b-a))


function main()
	n=readline()
	a=map(x->parse(x),split(readline()))
	println(gcd(a))
end
main()

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


import Base.readline
function readline(T)
    map(x->parse(T, x), split(readline()))
end

N, K = readline(Int64)

if div(N+1, 2) >= K
    println("YES")
else
    println("NO")
end

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)
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()
    k,a,b=parseMap(split(readline()))
    if b-a<=2
        println(k+1)
    else
        tmp=k-(a-1)
        res=tmp÷2
        ans=a
        for i in 1:res
            ans+=b-a
        end
        println(max(ans+tmp%2,k+1))
    end
end
main()

Score : 400 points

Problem Statement

Snuke has one biscuit and zero Japanese yen (the currency) in his pocket.
He will perform the following operations exactly K times in total, in the order he likes:

Hit his pocket, which magically increases the number of biscuits by one.

Exchange A biscuits to 1 yen.

Exchange 1 yen to B biscuits.

Find the maximum possible number of biscuits in Snuke's pocket after K operations.

Constraints

1 \leq K,A,B \leq 10^9

K,A and B are integers.

Input

Input is given from Standard Input in the following format:

K A B

Output

Print the maximum possible number of biscuits in Snuke's pocket after K operations.

Sample Input 1

4 2 6

Sample Output 1

7

The number of biscuits in Snuke's pocket after K operations is maximized as follows:

Hit his pocket. Now he has 2 biscuits and 0 yen.

Exchange 2 biscuits to 1 yen. his pocket. Now he has 0 biscuits and 1 yen.

Hit his pocket. Now he has 1 biscuits and 1 yen.

Exchange 1 yen to 6 biscuits. his pocket. Now he has 7 biscuits and 0 yen.

Sample Input 2

7 3 4

Sample Output 2

8

Sample Input 3

314159265 35897932 384626433

Sample Output 3

48518828981938099


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

L = readint()

notstarted = 0
beginning = 0
middle = 0
ending = 0
ended = 0

for _ in 1:L
    a = readint()
    notstarted, beginning, middle, ending, ended = (
                notstarted + a,
                min(beginning + Int(a == 0) * 2 + mod(a, 2), notstarted + mod(a, 2)),
                min(notstarted, beginning, middle + mod(a+1, 2)),
                min(notstarted, beginning, middle, ending + Int(a == 0) * 2 + mod(a, 2)),
                min(notstarted, beginning, middle, ending + mod(a, 2), ended + a)
        )
end

println(min(notstarted, beginning, middle, ending, ended))

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


Score : 100 points

Problem Statement

In order to pass the entrance examination tomorrow, Taro has to study for T more hours.

Fortunately, he can leap to World B where time passes X times as fast as it does in our world (World A).

While (X \times t) hours pass in World B, t hours pass in World A.

How many hours will pass in World A while Taro studies for T hours in World B?

Constraints

All values in input are integers.

1 \leq T \leq 100

1 \leq X \leq 100

Input

Input is given from Standard Input in the following format:

T X

Output

Print the number of hours that will pass in World A.

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

Sample Input 1

8 3

Sample Output 1

2.6666666667

While Taro studies for eight hours in World B where time passes three times as fast, 2.6666... hours will pass in World A.

Note that an absolute or relative error of at most 10^{-3} is allowed.

Sample Input 2

99 1

Sample Output 2

99.0000000000

Sample Input 3

1 100

Sample Output 3

0.0100000000


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

function main()
	n = readline() |> parseInt
	l = readline() |> split |> parseMap
	k = sum(l)
	flg = 0
	for i in 1:n
		if l[i] >= k-l[i]
			flg = 1
			break
		end
	end
	println(flg == 0?"Yes":"No")
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


readline()
l=map(x->parse(x),split(readline()))
s=m=0
for i in l
	s+=i
	m=max(m,i)
end
print(s>m*2?"Yes":"No")


parseInt(x) = parse(Int, x)
parseFloat(x) = parse(Float64, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n,m=parseMap(split(readline()))
    x=sort(parseMap(split(readline())))
    if n>=m
        println(0)
    else
        dif=[0 for i in 1:m-1]
        for i in 1:m-1
            dif[i]=x[i+1]-x[i]
        end
        sort!(dif,rev=true,by=x->x[1])
        ans=0
        for i in 1:m-1
            if i>=n
                ans+=dif[i]
            end
        end
        println(ans)
    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


function main()

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

  print(min(A,B)," ")
  if A+B < N
  	println(0)
  else
    println(A+B-N)
  end

end

main()

Score : 100 points

Problem Statement

We conducted a survey on newspaper subscriptions.
More specifically, we asked each of the N respondents the following two questions:

Question 1: Are you subscribing to Newspaper X?

Question 2: Are you subscribing to Newspaper Y?

As the result, A respondents answered "yes" to Question 1, and B respondents answered "yes" to Question 2.

What are the maximum possible number and the minimum possible number of respondents subscribing to both newspapers X and Y?

Write a program to answer this question.

Constraints

1 \leq N \leq 100

0 \leq A \leq N

0 \leq B \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the maximum possible number and the minimum possible number of respondents subscribing to both newspapers, in this order, with a space in between.

Sample Input 1

10 3 5

Sample Output 1

3 0

In this sample, out of the 10 respondents, 3 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 3 and at least 0.

Sample Input 2

10 7 5

Sample Output 2

5 2

In this sample, out of the 10 respondents, 7 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 5 and at least 2.

Sample Input 3

100 100 100

Sample Output 3

100 100


function main()
n = parse(Int, readline())
a = readline()
b = readline()
c = readline()

result = 0
for i = 1:n
    if a[i] == b[i] && b[i] == c[i]
        continue
    elseif a[i] == b[i] || b[i] == c[i] || a[i] == c[i]
        result += 1
    else
        result += 2
    end
end

print(result)
end

main()

Score : 200 points

Problem Statement

You are given three strings A, B and C. Each of these is a string of length N consisting of lowercase English letters.

Our objective is to make all these three strings equal. For that, you can repeatedly perform the following operation:

Operation: Choose one of the strings A, B and C, and specify an integer i between 1 and N (inclusive). Change the i-th character from the beginning of the chosen string to some other lowercase English letter.

What is the minimum number of operations required to achieve the objective?

Constraints

1 \leq N \leq 100

Each of the strings A, B and C is a string of length N.

Each character in each of the strings A, B and C is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
A
B
C

Output

Print the minimum number of operations required.

Sample Input 1

4
west
east
wait

Sample Output 1

3

In this sample, initially A = west、B = east、C = wait. We can achieve the objective in the minimum number of operations by performing three operations as follows:

Change the second character in A to a. A is now wast.

Change the first character in B to w. B is now wast.

Change the third character in C to s. C is now wast.

Sample Input 2

9
different
different
different

Sample Output 2

0

If A, B and C are already equal in the beginning, the number of operations required is 0.

Sample Input 3

7
zenkoku
touitsu
program

Sample Output 3

13


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

function main()
	n = readline() |> parseInt
	a = chomp(readline())
	b = chomp(readline())
	c = chomp(readline())
	count = 0
	for i in 1:n
		if a[i] != b[i] && b[i] != c[i] && c[i] != a[i]
			count += 2
		elseif a[i] != b[i] || b[i] != c[i] || c[i] != a[i]
			count += 1
		end
	end
	println(count)
end

main()