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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	ans = 0
	for i in 1:n
		ans += a[i]*(2^(n-i))
	end
	println(ans)
end

main()

Score : 200 points

Problem Statement

For a string S consisting of the uppercase English letters P and D, let the doctoral and postdoctoral quotient of S be the total number of occurrences of D and PD in S as contiguous substrings. For example, if S = PPDDP, it contains two occurrences of D and one occurrence of PD as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3.

We have a string T consisting of P, D, and ?.

Among the strings that can be obtained by replacing each ? in T with P or D, find one with the maximum possible doctoral and postdoctoral quotient.

Constraints

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

T consists of P, D, and ?.

Input

Input is given from Standard Input in the following format:

T

Output

Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each ? in T with P or D.
If there are multiple such strings, you may print any of them.

Sample Input 1

PD?D??P

Sample Output 1

PDPDPDP

This string contains three occurrences of D and three occurrences of PD as contiguous substrings, so its doctoral and postdoctoral quotient is 6, which is the maximum doctoral and postdoctoral quotient of a string obtained by replacing each ? in T with P or D.

Sample Input 2

P?P?

Sample Output 2

PDPD


function main()

  T = collect(chomp(readline()))
  N = length(T)

  for i in 1:N

    if T[i] == '?'
      T[i] = 'D'
    end

  end

  println(join(T))

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)
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()
    t=readline()
    ans=0
    for i in 1:length(t)
        if t[i]=='?'
            ans+=1
            if i>0 && t[i-1]=='P'
                ans+=1
            end
        end
        if i<length(t) && t[i:i+1]=="PD"
            ans+=1
        end
        if t[i]=='D'
            ans+=1
        end
    end
    for i in t
        if i=='?'
            print("D")
        else
            print(i)
        end
    end
end
main()


const double = Float64
const int = Int64

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

function main()
  N = readint()[1]
  A = readint()

  if A[1] == 1
    if N==0
      println(1)
      return
    else
      println(-1)
      return
    end
  end

  sum = 1
  parent = 1

  pmaxs = Vector{int}(undef,N+1)
  pmaxs[1] = 1

  for i in 2:N+1
    nmax = parent * 2
    if A[i] > nmax
      #println(pmaxs)
      println(-1)
      return
    end
    parent = min(nmax - A[i], 10^9)
    pmaxs[i] = parent
  end

  #sum = 1
  sum = 1+ A[N+1]
  chmax = A[N+1]
  for i in N:-1:2
    sum += A[i] + min(pmaxs[i],chmax)
    if chmax÷2 + chmax%2 > pmaxs[i]
      sum -= chmax - 2pmaxs[i]
    end
    chmax = A[i] + min(pmaxs[i],chmax)
  end

  println(sum)

end

main()

Score : 600 points

Problem Statement

Given is an integer sequence of length N+1: A_0, A_1, A_2, \ldots, A_N. Is there a binary tree of depth N such that, for each d = 0, 1, \ldots, N, there are exactly A_d leaves at depth d? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print -1.

Notes

A binary tree is a rooted tree such that each vertex has at most two direct children.

A leaf in a binary tree is a vertex with zero children.

The depth of a vertex v in a binary tree is the distance from the tree's root to v. (The root has the depth of 0.)

The depth of a binary tree is the maximum depth of a vertex in the tree.

Constraints

0 \leq N \leq 10^5

0 \leq A_i \leq 10^{8} (0 \leq i \leq N)

A_N \geq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 A_2 \cdots A_N

Output

Print the answer as an integer.

Sample Input 1

3
0 1 1 2

Sample Output 1

7

Below is the tree with the maximum possible number of vertices. It has seven vertices, so we should print 7.

Sample Input 2

4
0 0 1 0 2

Sample Output 2

10

Sample Input 3

2
0 3 1

Sample Output 3

-1

Sample Input 4

1
1 1

Sample Output 4

-1

Sample Input 5

10
0 0 1 1 2 3 5 8 13 21 34

Sample Output 5

264


function main()

  N = chomp(readline())

  check1 = ['2', '4', '5', '7', '9']
  check2 = ['0', '1', '6', '8']

  if in(N[end], check1)
    println("hon")
  elseif in(N[end], check2)
    println("pon")
  else
    println("bon")
  end

end

main()

Score: 100 points

Problem Statement

The cat Snuke wants to play a popular Japanese game called ÅtCoder, so Iroha has decided to teach him Japanese.

When counting pencils in Japanese, the counter word "本" follows the number. The pronunciation of this word varies depending on the number. Specifically, the pronunciation of "本" in the phrase "N 本" for a positive integer N not exceeding 999 is as follows:

hon when the digit in the one's place of N is 2, 4, 5, 7, or 9;

pon when the digit in the one's place of N is 0, 1, 6 or 8;

bon when the digit in the one's place of N is 3.

Given N, print the pronunciation of "本" in the phrase "N 本".

Constraints

N is a positive integer not exceeding 999.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

16

Sample Output 1

pon

The digit in the one's place of 16 is 6, so the "本" in "16 本" is pronounced pon.

Sample Input 2

2

Sample Output 2

hon

Sample Input 3

183

Sample Output 3

bon


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

function main()
	n = readline() |> parseInt
	s = readline() |> chomp
	if length(s)>n
		println(s[1:n],"...")
	else
		println(s)
	end
end

main()

Score: 200 points

Problem Statement

We have a string S consisting of lowercase English letters.

If the length of S is at most K, print S without change.

If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result.

Constraints

K is an integer between 1 and 100 (inclusive).

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

K
S

Output

Print a string as stated in Problem Statement.

Sample Input 1

7
nikoandsolstice

Sample Output 1

nikoand...

nikoandsolstice has a length of 15, which exceeds K=7.

We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand....

Sample Input 2

40
ferelibenterhominesidquodvoluntcredunt

Sample Output 2

ferelibenterhominesidquodvoluntcredunt

The famous quote from Gaius Julius Caesar.


k =  parse(Int,readline())
s = readline()
if length(s) <= k
    println(s)
else
    print(s[1:k]*"...")
end


readint()=parse(Int,readline())

K=readint()
S=chomp(readline())
if length(S)>K
    S=S[1:K]*"..."
end
print(S)


function main()

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

  times = 60H + M

  A_deg = 0.5*times
  B_deg = 6*M

  degree = abs(A_deg - B_deg)

  ans = sqrt(A^2 + B^2 - 2*A*B*abs(cosd(degree)))

  println(ans)

end

main()

Score: 300 points

Problem Statement

Consider an analog clock whose hour and minute hands are A and B centimeters long, respectively.

An endpoint of the hour hand and an endpoint of the minute hand are fixed at the same point, around which each hand rotates clockwise at constant angular velocity. It takes the hour and minute hands 12 hours and 1 hour to make one full rotation, respectively.

At 0 o'clock, the two hands overlap each other. H hours and M minutes later, what is the distance in centimeters between the unfixed endpoints of the hands?

Constraints

All values in input are integers.

1 \leq A, B \leq 1000

0 \leq H \leq 11

0 \leq M \leq 59

Input

Input is given from Standard Input in the following format:

A B H M

Output

Print the answer without units. Your output will be accepted when its absolute or relative error from the correct value is at most 10^{-9}.

Sample Input 1

3 4 9 0

Sample Output 1

5.00000000000000000000

The two hands will be in the positions shown in the figure below, so the answer is 5 centimeters.

Sample Input 2

3 4 10 40

Sample Output 2

4.56425719433005567605

The two hands will be in the positions shown in the figure below. Note that each hand always rotates at constant angular velocity.


if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
pI(x)=parse(Int,x)
pM(x::Array{SubString{String},1})=map(pI,x)
function mkgraph(n::Int,m::Int)
  g=Array{Int64,1}[Int[] for i=1:n]
  for i=1:m
    a,b=pM(split(readline()))
    push!(g[a],b); push!(g[b],a)
  end
  return g
end
function bfs(g,f::Int)
  n=length(g)
  q=Int[f]
  dist=zeros(Int,n)
  goto=zeros(Int,n)
  while !isempty(q)
    v=popfirst!(q);
    for nv=g[v];
      if dist[nv]!=0; continue; end
      dist[nv]=dist[v]+1
      goto[nv]=v
      push!(q,nv)
    end
  end
  dist[f]=0;goto[f]=0
  return dist,goto
end
function main()
  print(bfs(mkgraph(pM(split(readline()))...),1)[2])
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Score: 400 points

Problem Statement

There is a cave.

The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.

It is dark in the cave, so we have decided to place a signpost in each room except Room 1. The signpost in each room will point to one of the rooms directly connected to that room with a passage.

Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1.

If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.

Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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

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

A_i \neq B_i\ (1 \leq i \leq M)

One can travel between any two rooms by traversing passages.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If there is no way to place signposts satisfying the objective, print No.

Otherwise, print N lines. The first line should contain Yes, and the i-th line (2 \leq i \leq N) should contain the integer representing the room indicated by the signpost in Room i.

Sample Input 1

4 4
1 2
2 3
3 4
4 2

Sample Output 1

Yes
1
2
2

If we place the signposts as described in the sample output, the following happens:

Starting in Room 2, you will reach Room 1 after traversing one passage: (2) \to 1. This is the minimum number of passages possible.

Starting in Room 3, you will reach Room 1 after traversing two passages: (3) \to 2 \to 1. This is the minimum number of passages possible.

Starting in Room 4, you will reach Room 1 after traversing two passages: (4) \to 2 \to 1. This is the minimum number of passages possible.

Thus, the objective is satisfied.

Sample Input 2

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

Sample Output 2

Yes
6
5
5
1
1

If there are multiple solutions, any of them will be accepted.


function main()
	N,M=map(x->parse(Int,x),split(readline()))
	G=[Int[] for _=1:N]
	for _=1:M
		u,v=map(x->parse(Int,x),split(readline()))
		push!(G[u],v)
		push!(G[v],u)
	end
	d=zeros(Int,N)
	d[1]=-1
	Q=Int[1]
	while !isempty(Q)
		u=popfirst!(Q)
		for v=G[u]
			if d[v]==0
				d[v]=u
				push!(Q,v)
			end
		end
	end
	println("Yes")
	for i=2:N
		println(d[i])
	end
end
main()


using DataStructures
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::Int,m::Int=parseMap(split(readline()))
    road::Vector{Vector{Int}}=[[] for i in 1:n]
    dis::Vector{Int}=[8589934592 for i in 1:n]
    dis[1]=0
    for i in 1:m
        a,b=parseMap(split(readline()))
        push!(road[a],b)
        push!(road[b],a)
    end
    arr=Deque{Int}()
    push!(arr,1)
    while !isempty(arr)
        a=popfirst!(arr)
        for b in road[a]
            if dis[b]>dis[a]+1
                dis[b]=dis[a]+1
                push!(arr,b)
            end
        end
    end
    println("Yes")
    ans=[0 for i in 1:n]
    for i in n:-1:1
        ans[dis[i]+1]=i
    end
    for i in 2:n
        println(ans[dis[i]])
    end
end
main()


import Base.parse
import Base.StringVector
using DataStructures

parse(::Type{String},  str::AbstractString) = str

isdelim(x::UInt8, xs::Set{UInt8}) = x in xs

function myreaduntil(s::IO, delims::Set{UInt8})::Vector{UInt8}
    out = StringVector(0)
    c::UInt8 = 0x00
    while !eof(s)
        c = read(s, UInt8)
        if !isdelim(c, delims)
            break
        end
    end
    push!(out, c)
    if eof(s)
        return out
    end
    while !eof(s)
        c = read(s, UInt8)
        if isdelim(c, delims)
            break
        end
        push!(out, c)
    end
    return out
end

delimset = Set([0x0a, 0x20])

function readword(io::IO = stdin, delims = delimset)::String
    word = myreaduntil(io, delims)
    i = length(word)
    if i == 0 || word[i] != 0x0a
        return String(word)
    elseif i < 2 || word[i-1] != 0x0d
        return String(resize!(word,i-1))
    else
        return String(resize!(word,i-2))
    end
end

pread(ty) = parse(ty, readword())

reads(tys...)::Tuple{tys...} = Tuple{tys...}(pread(ty) for ty in tys)

readvec(tys::Tuple , len::Signed)::Vector{Tuple{tys...}} = @inbounds Tuple{tys...}[reads(tys...) for i in 1:len]

readvec(ty::Type, len::Signed)::Vector{ty} = @inbounds ty[pread(ty) for i in 1:len]

function readmat(ty::Type, s...)::Matrix{ty}
    v = Matrix{ty}(undef, s...)
    @inbounds for i = 1:s[1]
        v[i,:] = readvec(ty, s[2])
    end
    v
end

function rewrite(con, root, index=1, depth=0)
    arr = Set(Int[])
    return root
end

function main()
    N,M = reads(Int,Int)
    con = [Set(Int[]) for i in 1:N]
    for i in 1:M
        a,b = reads(Int,Int)
        push!(con[a], b)
        push!(con[b], a)
    end
    root = Union{Tuple{Int,Int},Nothing}[nothing for i in 1:N]
    root[1] = (0,0)
    q = PriorityQueue{Int,Int}()
    enqueue!(q, 1, 0)
    while !isempty(q)
        index = dequeue!(q)
        for n in con[index]
            if isnothing(root[n]) || root[n][2] > root[index][2]+1
                v = root[index][2]+1
                root[n] = (index, v)
                get(q,n,9223372036854775807) > v && enqueue!(q, n, v)
            end
        end
    end
    ans = root
    for i in ans[2:end]
        if isnothing(i)
            println("No")
            return
        end
    end
    println("Yes")
    for i in 2:N
        println(ans[i][1])
    end
end

main()


function main()
    N, M = [parse(Int, s) for s in split.(readline())]
    edges = [Int[] for _ in 1:N]
  	for i in 1:M
        a, b = [parse(Int, s) for s in split.(readline())]
        push!(edges[a], b)
        push!(edges[b], a)
    end
#    for i in 1:N
#        edges[i] = sort(collect(Set(edges[i])))
#    end

    quedata = zeros(Int, N+10)
    quedata[1] = 1
    ques, quee = 1, 2

    visited = falses(N)
    to_room = zeros(Int, N)

    while ques != quee
        pos = quedata[ques]
        ques += 1
        ques > length(quedata) && (ques = 1)
        visited[pos] && continue
        visited[pos] = true
        for topos in edges[pos]
            if !visited[topos]
                iszero(to_room[topos]) && (to_room[topos] = pos)
                quedata[quee] = topos
                quee += 1
                quee > length(quedata) && (quee = 1)
            end
        end
    end
    if !all(visited)
        println("No")
        return
    end

    println("Yes")
    for i in 2:N
        println(to_room[i])
    end
end

main()


using DataStructures

const MOD = 10^9+7
pow2(x) = big(2^x)%MOD

function solve()
    N = parse(Int,readline())
    cnt = counter(Rational{Int})
    zero = 0
    for i in 1:N
        a, b = parse.(Int,split(readline()))
        if (a==0 && b==0)
            zero+=1
        else
            inc!(cnt,a//b)
        end
    end

    ans = 1
    for (r,c) in cnt
        inv = r==0//1 ? 1//0 : -1/r
        ans *= pow2(c) + pow2(cnt[inv]) - 1
        reset!(cnt, inv)
        ans %= MOD
    end
    ans = mod(ans+(zero-1)+MOD, MOD)
    println(ans)
end
solve()

Score: 500 points

Problem Statement

We have caught N sardines. The deliciousness and fragrantness of the i-th sardine is A_i and B_i, respectively.

We will choose one or more of these sardines and put them into a cooler. However, two sardines on bad terms cannot be chosen at the same time.

The i-th and j-th sardines (i \neq j) are on bad terms if and only if A_i \cdot A_j + B_i \cdot B_j = 0.

In how many ways can we choose the set of sardines to put into the cooler? Since the count can be enormous, print it modulo 1000000007.

Constraints

All values in input are integers.

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

-10^{18} \leq A_i, B_i \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the count modulo 1000000007.

Sample Input 1

3
1 2
-1 1
2 -1

Sample Output 1

5

There are five ways to choose the set of sardines, as follows:

The 1-st

The 1-st and 2-nd

The 2-nd

The 2-nd and 3-rd

The 3-rd

Sample Input 2

10
3 2
3 2
-1 1
2 -1
-3 -9
-8 12
7 7
8 1
8 2
8 4

Sample Output 2

479


using DataStructures

const MOD = 10^9+7
pow2(x) = (2^x)%MOD

function solve()
    N = parse(Int,readline())
    cnt = counter(Rational{Int})
    zero = 0
    for i in 1:N
        a, b = parse.(Int,split(readline()))
        if (a==0 && b==0)
            zero+=1
        else
            inc!(cnt,a//b)
        end
    end

    ans = 1
    for (r,c) in cnt
        inv = r==0//1 ? 1//0 : -1/r
        ans *= pow2(c) + pow2(cnt[inv]) - 1
        reset!(cnt, inv)
        ans %= MOD
    end
    ans = mod(ans+(zero-1)+MOD, MOD)
    println(ans)
end
solve()


parseInt(x)=parse(Int, x)
parseMap(x::Array{SubString{String},1})=map(parseInt, x)
const p = 10^9+7

function main()
	n = readline() |> parseInt
	a = Tuple{Int,Int}[(0,0) for i in 1:n]
	for i in 1:n
		s,t = readline() |> split |> parseMap
		a[i] = (s,t)
	end
	d = Dict{Tuple{Int,Int},Int}()
	ct = zeros(Int,n)
	for i in 1:n
		v = (0,0)
		m = 1
		if a[i][1]!=0||a[i][2]!=0
			g = gcd(a[i][1],a[i][2])
			if a[i][1]!=0&&a[i][2]!=0
				if a[i][1]>0&&a[i][2]<0||a[i][1]<0&&a[i][2]>0
					m = -1
				end
			end
			if a[i][1]==0
				v=(0,1)
			elseif a[i][2]==0
				v=(1,0)
			else
				v = (m*div(abs(a[i][1]),g),div(abs(a[i][2]),g))
			end
		end
		if haskey(d,(-m*abs(v[1]),abs(v[2])))
			ct[i]=d[(-m*abs(v[1]),abs(v[2]))]
		end
		if haskey(d,v)
			d[v]+=1
		else
			d[v]=1
		end
	end
	bx = ones(Int,n+1)
	for i in 2:n+1
		bx[i]=bx[i-1]*2%p
	end
	ans = 1
	for i in keys(d)
		if i!=(0,0)
			m = 1
			if i[1]!=0
				m = div(abs(i[1]),i[1])
			end
			if i[1]==0||i[2]==0
				if i[1]==1&&haskey(d,(0,1))
					x=d[(1,0)]
					y=d[(0,1)]
					ans = ans*mod(bx[x+y+1]-(bx[x+1]-1)*(bx[y+1]-1)%p,p)%p
				elseif i[1]==0||(i[2]==0&&!haskey(d,(1,0)))
					ans = ans*bx[d[i]+1]%p
				end
			else
				if haskey(d,(-m*abs(i[2]),abs(i[1])))
					if i[1]>0
						x = d[i]
						y = d[(-m*abs(i[2]),abs(i[1]))]
						ans = ans*mod(bx[x+y+1]-(bx[x+1]-1)*(bx[y+1]-1)%p,p)%p
					end
				else
					ans = ans*bx[d[i]+1]%p
				end
			end
		end
	end
	if haskey(d,(0,0))
		ans = (ans+d[(0,0)])%p
	end
  println(d)
	println(mod(ans-1,p))
end

main()


parseInt(x)=parse(Int, x)
parseMap(x::Array{SubString{String},1})=map(parseInt, x)
const p = 10^9+7

function main()
	n = readline() |> parseInt
	a = Tuple{Int,Int}[(0,0) for i in 1:n]
	for i in 1:n
		s,t = readline() |> split |> parseMap
		a[i] = (s,t)
	end
	d = Dict{Tuple{Int,Int},Int}()
	ct = zeros(Int,n)
	for i in 1:n
		v = (0,0)
		m = 1
		if a[i][1]!=0||a[i][2]!=0
			g = gcd(a[i][1],a[i][2])
			if a[i][1]!=0&&a[i][2]!=0
				m = div(a[i][1]*a[i][2],abs(a[i][1]*a[i][2]))
			end
			v = (m*div(abs(a[i][1]),g),div(abs(a[i][2]),g))
		end
		if haskey(d,(-m*abs(v[1]),abs(v[2])))
			ct[i]=d[(-m*abs(v[1]),abs(v[2]))]
		end
		if haskey(d,v)
			d[v]+=1
		else
			d[v]=1
		end
	end
	bx = ones(Int,n+1)
	for i in 2:n+1
		bx[i]=bx[i-1]*2%p
	end
	ans = 1
	for i in keys(d)
		if i==(0,0)
			ans = ans*(d[i]+1)%p
		else
			m = 1
			if i[1]!=0
				m = div(abs(i[1]),i[1])
			end
			if haskey(d,(-m*abs(i[2]),abs(i[1])))
				if i[1]>0
					x = d[i]
					y = d[(-m*abs(i[2]),abs(i[1]))]
					ans = ans*mod(bx[x+y+1]-(bx[x+1]-1)*(bx[y+1]-1)%p,p)%p
				end
			else
				ans = ans*bx[d[i]+1]%p
			end
		end
	end
	println(mod(ans-1,p))
end

main()


function solve()
    s = readline()
    t = readline()

    if s == t[1:end-1]
        "Yes"
    else
        "No"
    end
end

println(solve())

Score : 100 points

Problem Statement

Takahashi wants to be a member of some web service.

He tried to register himself with the ID S, which turned out to be already used by another user.

Thus, he decides to register using a string obtained by appending one character at the end of S as his ID.

He is now trying to register with the ID T. Determine whether this string satisfies the property above.

Constraints

S and T are strings consisting of lowercase English letters.

1 \leq |S| \leq 10

|T| = |S| + 1

Input

Input is given from Standard Input in the following format:

S
T

Output

If T satisfies the property in Problem Statement, print Yes; otherwise, print No.

Sample Input 1

chokudai
chokudaiz

Sample Output 1

Yes

chokudaiz can be obtained by appending z at the end of chokudai.

Sample Input 2

snuke
snekee

Sample Output 2

No

snekee cannot be obtained by appending one character at the end of snuke.

Sample Input 3

a
aa

Sample Output 3

Yes


s=readline()
t=readline()
c=0
for i=1:length(s)
  if s[i]!=t[i]
    c+=1
  end
end
println(c>0 ? "No" : "Yes")


function f()
    a,b,c,k=map(x->parse(Int,x),split(readline()))
    ans=0
    ans += a
    if k-a <= 0
        ans = k
        println(ans)
        exit()
    end
    k -= a
    if k-b <= 0
        println(ans)
        exit()
    end
    k -= b
    print(ans-k)
end
f()

Score : 200 points

Problem Statement

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.

We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

Constraints

All values in input are integers.

0 \leq A, B, C

1 \leq K \leq A + B + C \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the maximum possible sum of the numbers written on the cards chosen.

Sample Input 1

2 1 1 3

Sample Output 1

2

Consider picking up two cards with 1s and one card with a 0.
In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000


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

A,B,C,K = parseMap(split(readline()))

ans = 0
if A + B > K
    println(A)
else
    ans += A
    ans -= K - (A+B)
    println(ans)
end


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

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

C = []
A = []
for i in 1:N
    this_line = parseMap(split(readline()))
    c = this_line[1]
    a = this_line[1:end]
    push!(C,c)
    push!(A,a)
end

function BitSearch(x)
    count_pettern = x ^ 2
    petterns = []
    for i in 1:count_pettern - 1
        bit = digits(i,base=2,pad=x)
        push!(petterns,bit)
    end
    return  petterns
end

all_pettern = BitSearch(N)
min_money = 10^10
for pettern in all_pettern
    money = 0
    ok = zeros(Int,M)
    for i in 1:N
        if pettern[i] == 1
            money += C[i]
            for j in 1:M
                ok[j] += A[i][j]
            end
        end
    end
    isOk = true
    for o in ok
        if o < X
            isOk = false
            break
        end
    end
    if isOk
        if money < min_money
            min_money = money
        end
    end
end
if min_money == 10^10
    println(-1)
else
    println(min_money)
end

end

main()

Score : 300 points

Problem

Takahashi, who is a novice in competitive programming, wants to learn M algorithms.
Initially, his understanding level of each of the M algorithms is 0.

Takahashi is visiting a bookstore, where he finds N books on algorithms.
The i-th book (1\leq i\leq N) is sold for C_i yen (the currency of Japan). If he buys and reads it, his understanding level of the j-th algorithm will increase by A_{i,j} for each j (1\leq j\leq M).
There is no other way to increase the understanding levels of the algorithms.

Takahashi's objective is to make his understanding levels of all the M algorithms X or higher. Determine whether this objective is achievable. If it is achievable, find the minimum amount of money needed to achieve it.

Constraints

All values in input are integers.

1\leq N, M\leq 12

1\leq X\leq 10^5

1\leq C_i \leq 10^5

0\leq A_{i, j} \leq 10^5

Input

Input is given from Standard Input in the following format:

N M X
C_1 A_{1,1} A_{1,2} \cdots A_{1,M}
C_2 A_{2,1} A_{2,2} \cdots A_{2,M}
\vdots
C_N A_{N,1} A_{N,2} \cdots A_{N,M}

Output

If the objective is not achievable, print -1; otherwise, print the minimum amount of money needed to achieve it.

Sample Input 1

3 3 10
60 2 2 4
70 8 7 9
50 2 3 9

Sample Output 1

120

Buying the second and third books makes his understanding levels of all the algorithms 10 or higher, at the minimum cost possible.

Sample Input 2

3 3 10
100 3 1 4
100 1 5 9
100 2 6 5

Sample Output 2

-1

Buying all the books is still not enough to make his understanding levels of all the algorithms 10 or higher.

Sample Input 3

8 5 22
100 3 7 5 3 1
164 4 5 2 7 8
334 7 2 7 2 9
234 4 7 2 8 2
541 5 4 3 3 6
235 4 8 6 9 7
394 3 6 1 6 2
872 8 4 3 7 2

Sample Output 3

1067


const double = Float64
const int = Int64
const vint = Vector{int}
const vdouble = Vector{double}

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

const MOD = 998244353
const NMax = 2*10^5

function main()

  # ここ lobal 変数
  fac = ones(int,NMax)
  inv = ones(int,NMax)
  finv = ones(int,NMax)
  for i in 2:NMax
    fac[i] = (fac[i-1]*i)%MOD
    inv[i] = ((1+div(MOD,i))*inv[i-MOD%i])%MOD
    finv[i] = (finv[i-1]*inv[i])%MOD
  end
  N,M,K = readint()

  if N == 1
    println(M)
  elseif M == 1
    println(1)
  else
    MM = Vector{int}(undef,N)
     MM[1] = M-1
    for i in 2:N
      MM[i] = (MM[i-1]*(M-1))%MOD
    end

    mod = 0
    for i in K:-1:1
      if N-1-i == 0
        mod += M
        mod %= MOD
      else
        mod += ((((fac[N-1]*finv[i]%MOD * finv[N-1-i]%MOD) * M) %MOD ) *MM[N-1-i]) %MOD
        mod %= MOD
      end
    end

    mod += (M * MM[N-1])%MOD
    mod %= MOD

    println(mod )
  end
end

main()

Score : 500 points

Problem Statement

There are N blocks arranged in a row. Let us paint these blocks.

We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways.

Find the number of ways to paint the blocks under the following conditions:

For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors.

There may be at most K pairs of adjacent blocks that are painted in the same color.

Since the count may be enormous, print it modulo 998244353.

Constraints

All values in input are integers.

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

0 \leq K \leq N - 1

Input

Input is given from Standard Input in the following format:

N M K

Output

Print the answer.

Sample Input 1

3 2 1

Sample Output 1

6

The following ways to paint the blocks satisfy the conditions: 112, 121, 122, 211, 212, and 221. Here, digits represent the colors of the blocks.

Sample Input 2

100 100 0

Sample Output 2

73074801

Sample Input 3

60522 114575 7559

Sample Output 3

479519525


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

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

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

function main()
	n = readline() |> parseInt
	a = Tuple{Int,Int}[]
	b = Tuple{Int,Int}[]
	f = 0
	lx = 0
	rx = 0
	for i in 1:n
		s = readline() |> chomp
		l = 0
		r = 0
		for j in 1:length(s)
			if s[j]=='('
				l += 1
			else
				if l==0
					r+=1
				else
					l-=1
				end
			end
		end
		if l>r
			push!(a,(l,r))
		else
			push!(b,(l,r))
		end
		lx+=l
		rx+=r
	end
	f = 0
	if lx!=rx
		f = 1
	end
	lc = 0
	a = sort(a,by=x->(x[2],x[2]-x[1]))
	if !isempty(a)
		if a[1][2]>0
			f = 1
		end
		lc+=a[1][1]
		popfirst!(a)
	end
	b = sort(b,by=x->x[2],rev=true)
	for i in 1:length(b)
		if lc<b[i][2]
			f=1
		end
		lc+=b[i][1]-b[i][2]
	end
	println(f==1 ? "No" : "Yes")
end

main()

Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No


const double = Float64
const int = Int64
const vint = Vector{int}
const vdouble = Vector{double}

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

function main()
  N = readint()[1]

  HT = Array{int,2}(undef,N,2)

  # input とまえしょり
  for i in 1:N
    s = readline()

    h = 0
    t = 0
    for c in s
      if c=='('
        t += 1
      else
        t -= 1
        h = min(h,t)
      end
    end
    HT[i,:] .= h,t
  end

  HT = HT[sortperm(HT[:,1],rev=true),:]

  h = 0
  flag = true
  for i in 1:N
    if h+HT[i,1] < 0
      flag = false
      break
    end
    h += HT[i,2]
  end

  if flag && h==0
    println("Yes")
  else
    println("No")
  end

end

main()


function main()
	N=parse(Int,readline())
	pos=Pair{Int,Int}[]
	neg=Pair{Int,Int}[]
	for _=1:N
		s=readline()
		cnt=0
		mc=0
		for c=s
			if c=='('
				cnt+=1
			else
				cnt-=1
			end
			mc=min(mc,cnt)
		end
		if cnt>=0
			push!(pos,-mc=>cnt)
		else
			push!(neg,mc-cnt=>cnt)
		end
	end
	now=0
	flag=true
	for (a,b)=pos
		if now-a<0
			flag=false
		end
		now+=b
	end
	for (a,b)=neg
		if now+a+b<0
			flag=false
		end
		now+=b
	end
	println(flag&&now==0 ? "Yes" : "No")
end
main()


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

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

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

function main()
	n = readline() |> parseInt
	a = Tuple{Int,Int}[(0,0) for i in 1:n]
	f = 0
	for i in 1:n
		s = readline() |> chomp
		l = 0
		r = 0
		for j in 1:length(s)
			if s[j]=='('
				l += 1
			else
				if l==0
					r+=1
				else
					l-=1
				end
			end
		end
		a[i]=(l,r)
	end
	ls = 0
	rs = 0
	for i in 1:n
		ls+=a[i][1]
		rs+=a[i][2]
	end
	if ls!=rs
		println("No")
	else
		a = sort(a,by=x->(x[2],x[2]-x[1]))
		h = Tuple{Int,Int}[]
		f = 0
		lc = 0
		ct = 1
		if a[1][2]==0
			f = 1
			lc+=a[1][1]
			popfirst!(a)
		end
		if f == 0
			println("No")
		else
			for i in 1:n-1
				if !isempty(a)
					while a[1][2]<=lc
						now = popfirst!(a)
						hpush(h,now)
						if isempty(a)
							break
						end
					end
				end
				if isempty(h)
					f = 0
				else
					now = hpop(h)
					if now[2]>lc
						f=0
					end
					lc+=now[1]-now[2]
				end
			end
			if f==0
				println("No")
			else
				println("Yes")
			end
		end
	end
end

main()


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

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

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

function main()
	n = readline() |> parseInt
	a = Tuple{Int,Int}[(0,0) for i in 1:n]
	f = 0
	for i in 1:n
		s = readline() |> chomp
		l = 0
		r = 0
		for j in 1:length(s)
			if s[j]=='('
				l += 1
			else
				if l==0
					r+=1
				else
					l-=1
				end
			end
		end
		a[i]=(l,r)
	end
	ls = 0
	rs = 0
	for i in 1:n
		ls+=a[i][1]
		rs+=a[i][2]
	end
	if ls!=rs
		println("No")
	else
		a = sort(a,by=x->(x[2],x[1]))
		h = Tuple{Int,Int}[]
		f = 0
		lc = 0
		for i in n:-1:1
			if a[i][2]==0&&a[i][1]>0
				f = i
				lc+=a[i][1]
				break
			end
		end
		if f == 0
			println("No")
		else
			for i in 1:n
				if a[i]!=f
					hpush(h,a[i])
				end
			end
			for i in 1:n-1
				b = hpop(h)
				lc-=a[i][2]
				if lc<0
					f=0
				end
				lc+=a[i][1]
			end
			if f==0
				println("No")
			else
				println("Yes")
			end
		end
	end
end

main()


S = readline()
if S=="ABC"
    print("ARC")
else
    print("ABC")
end

Score : 100 points

Problem Statement

AtCoder Inc. holds a contest every Saturday.

There are two types of contests called ABC and ARC, and just one of them is held at a time.

The company holds these two types of contests alternately: an ARC follows an ABC and vice versa.

Given a string S representing the type of the contest held last week, print the string representing the type of the contest held this week.

Constraints

S is ABC or ARC.

Input

Input is given from Standard Input in the following format:

S

Output

Print the string representing the type of the contest held this week.

Sample Input 1

ABC

Sample Output 1

ARC

They held an ABC last week, so they will hold an ARC this week.


S = readline()
if S=="ABC"
    print("ARC")
else
    print("ARC")
end


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

function main()
    s=chomp(readline())
    if s=="ABC"
        println("ARC")
    else
        println("ABC")
    end
end
main()


function main()

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

  check = zeros(Int, N)

  for i in 1:K
    d = parse(Int, readline())
    A = map(x -> parse(Int, x), split(readline()))
    for i in A
      check[i] += 1
    end
  end

  cnt = 0

  for i in check
    if i == 0
      cnt += 1
    end
  end

  println(cnt)

end

main()

Score : 200 points

Problem Statement

N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town.

There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}.

Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief?

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 100

1 \leq d_i \leq N

1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N

Input

Input is given from Standard Input in the following format:

N K
d_1
A_{1, 1} \cdots A_{1, d_1}
\vdots
d_K
A_{K, 1} \cdots A_{K, d_K}

Output

Print the answer.

Sample Input 1

3 2
2
1 3
1
3

Sample Output 1

1

Snuke 1 has Snack 1.

Snuke 2 has no snacks.

Snuke 3 has Snack 1 and 2.

Thus, there will be one victim: Snuke 2.

Sample Input 2

3 3
1
3
1
3
1
3

Sample Output 2

2


function fact()
    n,m=map(x->parse(Int,x),split(readline()))
    h=map(x->parse(Int,x),split(readline()))
    ma=zeros(Int,n)
    for i=1:m
        a,b=map(x->parse(Int,x),split(readline()))
        ma[a]=max(ma[a],h[b])
        ma[b]=max(ma[b],h[a])
    end
    ans=0
    for i=1:n
        ans += h[i]>ma[i]
    end
    print(ans)
end
fact()

Score : 300 points

Problem Statement

There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is H_i.
There are also M roads, each connecting two different observatories. Road j connects Obs. A_j and Obs. B_j.

Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road.
Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.

How many good observatories are there?

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq H_i \leq 10^9

1 \leq A_i,B_i \leq N

A_i \neq B_i

Multiple roads may connect the same pair of observatories.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
H_1 H_2 ... H_N
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Print the number of good observatories.

Sample Input 1

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

Sample Output 1

2

From Obs. 1, you can reach Obs. 3 using just one road. The elevation of Obs. 1 is not higher than that of Obs. 3, so Obs. 1 is not good.

From Obs. 2, you can reach Obs. 3 and 4 using just one road. The elevation of Obs. 2 is not higher than that of Obs. 3, so Obs. 2 is not good.

From Obs. 3, you can reach Obs. 1 and 2 using just one road. The elevation of Obs. 3 is higher than those of Obs. 1 and 2, so Obs. 3 is good.

From Obs. 4, you can reach Obs. 2 using just one road. The elevation of Obs. 4 is higher than that of Obs. 2, so Obs. 4 is good.

Thus, the good observatories are Obs. 3 and 4, so there are two good observatories.

Sample Input 2

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

Sample Output 2

3


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,m=parseMap(split(readline()))
    h=parseMap(split(readline()))
    ans=[true for i in 1:n]
    for i in 1:m
        a,b=parseMap(split(readline()))
        ans[a]&=h[a]>h[b]
        ans[b]&=h[a]<h[b]
    end
    println(count(ans))
end
main()


function main()
  n,m = parse.(Int,split(readline()))
  h_arr = parse.(Int,split(readline()))
  h_ones = ones(n)

  for i in 1:m
    a,b = parse.(Int, split(readline()))
    if h_arr[a] > h_arr[b]
      h_ones[b] = 0
    elseif h_arr[a] < h_arr[b]
      h_ones[a] = 0
    else
      h_ones[a] = 0
      h_ones[b] = 0
    end
  end

  println(count(x -> x == 1, h_ones))
end
main()


function main()

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

  check = [[] for i in 1:N]

  for i in 1:M
    (A, B) = map(x -> parse(Int, x), split(readline()))
    push!(check[A], B)
    push!(check[B], A)
  end

  cnt = 0
  cnt_check = true

  for i in 1:N
    cnt_check = true
    for j in check[i]
      if H[i] <= H[j]
        cnt_check = false
      end
    end
    if cnt_check
      cnt += 1
    end
  end

  println(cnt)

end

main()


if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
pI(x)=parse(Int,x)
pM(x::Array{SubString{String},1})=map(pI,x)
function mkdivisor(n::Int)
  divisors=Int[]
  for i=1:div(n^0.5,1)
    if n%i==0
      push!(divisors,i)
      if i!= fld(n,i); push!(divisors,fld(n,i));end
    end
  end
  sort!(divisors)
  return divisors
end
function main()
  x=pI(readline())
  l=mkdivisor(x)
  ll=[(i,i-j) for i=1:200 for j=l]
  for k in ll
    i,j=k
    if i^5-j^5==x
      print(i," ",j)
      break
    end
  end
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Score : 400 points

Problem Statement

Give a pair of integers (A, B) such that A^5-B^5 = X.
It is guaranteed that there exists such a pair for the given integer X.

Constraints

1 \leq X \leq 10^9

X is an integer.

There exists a pair of integers (A, B) satisfying the condition in Problem Statement.

Input

Input is given from Standard Input in the following format:

X

Output

Print A and B, with space in between.
If there are multiple pairs of integers (A, B) satisfying the condition, you may print any of them.

A B

Sample Input 1

33

Sample Output 1

2 -1

For A=2 and B=-1, A^5-B^5 = 33.

Sample Input 2

1

Sample Output 2

0 -1


function main()
  X=parse(Int,readline())
  for i=-200:200,j=-200:i
    if i^5-j^5==X
      println("$i $j")
      exit()
    end
  end
end
main()


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

function main()
    x=parseInt(readline())
    for a in 0:1000
        for b in -1000:1000
            if a^5-b^5==x
                println("$a $b")
                return
            end
        end
    end
end
main()


const teststr =
"""
33
"""

function lines()
  Channel{String}(32) do ch
	ll = readlines()
    lines = isempty(ll) ? split(teststr, "\n") : ll
    for s in lines
      put!(ch, s)
    end
  end
end

function main()
  ch = lines()
  X = parse(Int, take!(ch))
  maxA = 10^10
  for A in (x for absx in 0:maxA for x in (absx, -absx))
    B5 = A^5 - X
    Bf = Float64(abs(B5))^(1.0/5)
    B5 < 0 && (Bf = -Bf)
    B = round(Int, Bf)
    if A^5 - B^5 == X
      println("$A $B")
      exit(0)
    end
  end
end

main()


function main()
    x = parse(BigInt, readline())

    for i = 0:1000
        a = BigInt(i)
        a5 = a^5
        s = 0
        if a5 > x
            for j = a5-1:-1:1
                b = BigInt(j)
                b5 = b^5
                if a5 - b5 == x
                    println(a, " ", b)
                    return
                end
            end
        else
            b = -one(BigInt)
            while a5 - b^5 != x
                b -= 1
            end
            println(a, " ", b)
            return
        end
    end

end

main()


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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	d = Dict{Int,Int}()
	ans = 0
	for i in 1:n
		if haskey(d,i-a[i])
			ans+=d[i-a[i]]
		end
		if haskey(d,a[i]+i)
			d[a[i]+i]+=1
		else
			d[a[i]+i]=1
		end
	end
	println(ans)
end

main()

Score: 500 points

Problem Statement

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.

There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.

According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.

The absolute difference of their attendee numbers is equal to the sum of their heights.

There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?

P.S.: We cannot let you know the secret.

Constraints

All values in input are integers.

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

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

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the number of pairs satisfying the condition.

Sample Input 1

6
2 3 3 1 3 1

Sample Output 1

3

A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.

A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.

A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.

No other pair satisfies the condition, so you should print 3.

Sample Input 2

6
5 2 4 2 8 8

Sample Output 2

0

No pair satisfies the condition, so you should print 0.

Sample Input 3

32
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5

Sample Output 3

22


parseInt(x) = parse(Int,x)
function tie(s)#k[1]->"A",k[2]->"B",k[3]->"C"
    if s == "A"
        return 1
    end
    if s == "B"
        return 2
    end
    if s == "C"
        return 3
    end
end

function check(s1,s2)
    d=Dict()
    d[s1]=1
    d[s2]=2
    if !haskey(d,"A")
        return "A"
    end
    if !haskey(d,"B")
        return "B"
    end
    if !haskey(d,"C")
        return "C"
    end
end

function cmp(s1,s2,s1_2,s2_2)
    d=Dict()
    d[s1]=1
    d[s2]=1
    if haskey(d,s1_2) && haskey(d,s2_2)
        return true
    else
        return false
    end
end

function f()
    k=zeros(Int,3)#k[1]=a,k[2]=b,k[3]=c
    n,k[1],k[2],k[3]=map(x->parseInt(x),split(readline()))
    s=Matrix{SubString{String}}(undef,n,2)
    q=Vector{SubString{String}}(undef,n)
    for i=1:n
        s[i,1],s[i,2]=split.(readline(),"")
    end
    for i=1:n
        if k[1]+k[2]+k[3] == 0 #a+b+c=0
            print("No")
            exit()
        end
        if  k[tie(s[i,1])] == k[tie(s[i,2])] == 0 #両方0
            print("No")
            exit()
        end
        if k[tie(s[i,1])] == 0 #片方0-left
            q[i]=s[i,1]
            k[tie(s[i,1])] += 1
            k[tie(s[i,2])] -= 1
        elseif k[tie(s[i,2])] == 0 #片方0-right
            q[i]=s[i,2]
            k[tie(s[i,1])] -= 1
            k[tie(s[i,2])] += 1
        #両方1以上
        elseif k[1]+k[2]+k[3] == 2 && k[tie(s[i,1])] == k[tie(s[i,2])] == 1 && cmp(s[i,1],s[i,2],s[i+1,1],s[i+2,2])
            if i != n
                if s[i,1] != check(s[i+1,1],s[i+1,2])
                    q[i]=s[i,1]
                    k[tie(s[i,1])] += 1
                    k[tie(s[i,2])] -= 1
                else
                    q[i]=s[i,2]
                    k[tie(s[i,1])] -= 1
                    k[tie(s[i,2])] += 1
                end
            end
        else
            q[i]=s[i,1]
            k[tie(s[i,1])] += 1
            k[tie(s[i,2])] -= 1
        end
    end
    println("Yes")
    for i=1:n
        println(q[i])
    end
end
f()

Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

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

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A


parseInt(x) = parse(Int,x)
function tie(s)#k[1]->"A",k[2]->"B",k[3]->"C"
    if s == "A"
        return 1
    end
    if s == "B"
        return 2
    end
    if s == "C"
        return 3
    end
end

function check(s1,s2)
    d=Dict()
    d[s1]=1
    d[s2]=2
    if !haskey(d,"A")
        return "A"
    end
    if !haskey(d,"B")
        return "B"
    end
    if !haskey(d,"C")
        return "C"
    end
end

function f()
    k=zeros(Int,3)#k[1]=a,k[2]=b,k[3]=c
    n,k[1],k[2],k[3]=map(x->parseInt(x),split(readline()))
    s=Matrix{SubString{String}}(undef,n,2)
    q=Vector{SubString{String}}(undef,n)
    for i=1:n
        s[i,1],s[i,2]=split.(readline(),"")
    end
    for i=1:n
        if k[1]+k[2]+k[3] == 0 #a+b+c=0
            print("No")
            exit()
        end
        if  k[tie(s[i,1])] == k[tie(s[i,2])] == 0 #両方0
            print("No")
            exit()
        end
        if k[tie(s[i,1])] == 0 #片方0-left
            q[i]=s[i,1]
            k[tie(s[i,1])] += 1
            k[tie(s[i,2])] -= 1
        elseif k[tie(s[i,2])] == 0 #片方0-right
            q[i]=s[i,2]
            k[tie(s[i,1])] -= 1
            k[tie(s[i,2])] += 1
        #両方1以上
        elseif k[1]+k[2]+k[3] == 2 && k[tie(s[i,1])] == k[tie(s[i,2])] ==1
            if i != n
                if s[i,1] != check(s[i+1,1],s[i+1,2])
                    q[i]=s[i,1]
                    k[tie(s[i,1])] += 1
                    k[tie(s[i,2])] -= 1
                else
                    q[i]=s[i,2]
                    k[tie(s[i,1])] -= 1
                    k[tie(s[i,2])] += 1
                end
            end
        else
            q[i]=s[i,1]
            k[tie(s[i,1])] += 1
            k[tie(s[i,2])] -= 1
        end
    end
    println("Yes")
    for i=1:n
        println(q[i])
    end
end

f()


const teststr =
"""
8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB
"""

function lines()
  Channel{String}(32) do ch
    ll = readlines()
    lines = isempty(ll) ? split(teststr, "\n") : ll
    for s in lines
      put!(ch, s)
    end
  end
end

function main()
  ch = lines()
  N,A,B,C = parse.(Int, split(take!(ch)))
  c = zeros(Int, 3, N)
  strs=String[]
  for i in 1:N
    str = take!(ch)
    push!(strs, str)
    @views c[:,i] = str=="AB" ? [1,-1,0] :
                    str=="AC" ? [1,0,-1] :
                                [0,1,-1]
  end
  ss = ones(Int, N)
  hs = zeros(Int, 3, N)
  @views hs[:,1] .= [A,B,C]
  que = [(1,-1), (1,1)]
  while !isempty(que)
    n, s = pop!(que)
    abc = s>0 ? @views(hs[:,n] .+ c[:,n]) : @views(hs[:,n] .- c[:,n])
    any(abc .< 0) && continue
    ss[n] = s
    if n == N
      println("Yes")
      for i in 1:N
        println(strs[i][ss[i]==1 ? 1 : 2])
      end
      exit(1)
    end
    @views hs[:,n+1] .= abc
	push!(que, (n+1,-1), (n+1,1))
  end
  println("No")
end

main()


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

function main()
	n,a,b,c = readline() |> split |> parseMap
	s = String["" for i in 1:n]
	for i in 1:n
		str = readline() |> chomp
		s[i] = str
	end
	if a>=1&&b>=1&&c>=1
		println("Yes")
		for i in 1:n
			if s[i]=="AB"
				if a>=b
					println("B")
					a-=1
					b+=1
				else
					println("A")
					a+=1
					b-=1
				end
			elseif s[i]=="AC"
				if a>=c
					println("C")
					a-=1
					c+=1
				else
					println("A")
					a+=1
					c-=1
				end
			else
				if c>=b
					println("B")
					c-=1
					b+=1
				else
					println("C")
					c+=1
					b-=1
				end
			end
		end
	else
		f = 0
		ans = Char['0' for i in 1:n]
		nx = String["" for i in 1:n]
		ab = n+1
		ac = n+1
		bc = n+1
		for i in n:-1:1
			if s[i]=="AB"
				nx[i] = bc<ac ? "BC" : "AC"
				ab = i
			elseif s[i]=="AC"
				nx[i] = ab < bc ? "AB" : "BC"
				bc = i
			else
				nx[i] = ab < ac ? "AB" : "AC"
				ac = i
			end
		end
		for i in 1:n
			for i in 1:n
				if s[i]=="AB"
					if a>b
						ans[i]='B'
						a-=1
						b+=1
					elseif b>a
						ans[i]='A'
						a+=1
						b-=1
					elseif a==0
						f = 1
					else
						if nx[i]=="AC"
							ans[i]='A'
							a+=1
							b-=1
						else
							ans[i]='B'
							a-=1
							b+=1
						end
					end
				elseif s[i]=="AC"
					if a>c
						ans[i]='C'
						a-=1
						c+=1
					elseif c>a
						ans[i]='A'
						a+=1
						c-=1
					elseif a==0
						f = 1
					else
						if nx[i]=="BC"
							ans[i]='C'
							c+=1
							a-=1
						else
							ans[i]='A'
							a+=1
							c-=1
						end
					end
				else
					if c>b
						ans[i]='B'
						c-=1
						b+=1
					elseif b>c
						ans[i]='C'
						c+=1
						b-=1
					elseif b==0
						f = 1
					else
						if nx[i]=="AB"
							ans[i]='B'
							b+=1
							a-=1
						else
							ans[i]='C'
							c+=1
							b-=1
						end
					end
				end
			end
		end
		if f==1
			println("No")
		else
			println("Yes")
			for i in 1:n
				println(ans[i])
			end
		end
	end
end

main()


K=parse(Int,readline())
A,B=map(x->parse(Int,x),split(readline()))
println(div(B,K)!=div(A-1,K) ? "OK" : "NG")

Score: 100 points

Problem Statement

Takahashi the Jumbo will practice golf.

His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive).

If he can achieve the objective, print OK; if he cannot, print NG.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 1000

1 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

K
A B

Output

If he can achieve the objective, print OK; if he cannot, print NG.

Sample Input 1

7
500 600

Sample Output 1

OK

Among the multiples of 7, for example, 567 lies between 500 and 600.

Sample Input 2

4
5 7

Sample Output 2

NG

No multiple of 4 lies between 5 and 7.

Sample Input 3

1
11 11

Sample Output 3

OK


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

function main()
	k = readline() |> parseInt
	a,b = readline() |> split |> parseMap
	if a%k==0||b%k==0
		println("OK")
	else
		println(div(a,k)==div(b,k) ? "NG" : "OK")
	end
end

main()


k=parse(Int,readline())
a,b=parse.(Int,split(readline()))
b-=a
a%=k
print(0 in a:a+b || k in a:a+b ? "OK" : "NG")


X=parse(Int,readline())
c=0
n=100
while n<X
  global c+=1
  global n+=div(n,100)
end
println(c)

Score : 200 points

Problem Statement

Takahashi has a deposit of 100 yen (the currency of Japan) in AtCoder Bank.

The bank pays an annual interest rate of 1 % compounded annually. (A fraction of less than one yen is discarded.)

Assuming that nothing other than the interest affects Takahashi's balance, in how many years does the balance reach X yen or above for the first time?

Constraints

101 \le X \le 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the number of years it takes for Takahashi's balance to reach X yen or above for the first time.

Sample Input 1

103

Sample Output 1

3

The balance after one year is 101 yen.

The balance after two years is 102 yen.

The balance after three years is 103 yen.

Thus, it takes three years for the balance to reach 103 yen or above.

Sample Input 2

1000000000000000000

Sample Output 2

3760

Sample Input 3

1333333333

Sample Output 3

1706


import Base.parse
import Base.StringVector

parse(::Type{String},  str::AbstractString) = str

isdelim(x::UInt8, xs::Set{UInt8}) = x in xs

function myreaduntil(s::IO, delims::Set{UInt8})::Vector{UInt8}
    out = StringVector(0)
    c::UInt8 = 0x00
    while !eof(s)
        c = read(s, UInt8)
        if !isdelim(c, delims)
            break
        end
    end
    push!(out, c)
    if eof(s)
        return out
    end
    while !eof(s)
        c = read(s, UInt8)
        if isdelim(c, delims)
            break
        end
        push!(out, c)
    end
    return out
end

delimset = Set([0x0a, 0x20])

function readword(io::IO = stdin, delims = delimset)::String
    word = myreaduntil(io, delims)
    i = length(word)
    if i == 0 || word[i] != 0x0a
        return String(word)
    elseif i < 2 || word[i-1] != 0x0d
        return String(resize!(word,i-1))
    else
        return String(resize!(word,i-2))
    end
end

pread(ty) = parse(ty, readword())

reads(tys...)::Tuple{tys...} = Tuple{tys...}(pread(ty) for ty in tys)

readvec(tys::Tuple , len::Signed)::Vector{Tuple{tys...}} = @inbounds Tuple{tys...}[reads(tys...) for i in 1:len]

readvec(ty::Type, len::Signed)::Vector{ty} = @inbounds ty[pread(ty) for i in 1:len]

function readmat(ty::Type, s...)::Matrix{ty}
    v = Matrix{ty}(undef, s...)
    @inbounds for i = 1:s[1]
        v[i,:] = readvec(ty, s[2])
    end
    v
end

function main()
    X = pread(Int)
    a = 100
    for i in 1:10000
        a = Int(floor(1.01*a))
        if a >= X
            println(i)
            break
        end
    end
end

main()


function f(x)
    t = 100
    sum=0
    while t >= x
        t += floor(t*0.01)
        sum++
    end
    println(sum)
end
x=parse(Int,readline())
f(x)


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

function main()
    X = parseInt(readline())
    money = 100
    for i in 1:100000000000000000000
        money *= 1.01
        money = floor(money)
        if money >= X
            println(i)
            break
        end
    end
end

main()


parseInt(x)=parse(Int, x)
parseMap(x::Array{SubString{String},1})=map(parseInt, x)
function moveup(x::Array{Int,1},n)
	k = length(x)
	for i in 1:k-1
		if x[k-i+1] > n-1
			x[k-i] += div(x[k-i+1],n)
			x[k-i+1] = x[k-i+1]%n
		end
	end
	if x[1] > n-1
		0
	else
		x
	end
end

function main()
	n,m,q = readline() |> split |> parseMap
	g = zeros(Int,10)
	e = Tuple{Int,Int,Int,Int}[(0,0,0,0) for i in 1:q]
	for i in 1:q
		a,b,c,d = readline() |> split |> parseMap
		e[i] = (a,b,c,d)
	end
	mx = 0
	while g!=0
		ct = 0
		for i in 1:q
			if g[e[i][2]]-g[e[i][1]]==e[i][3]
				ct+=e[i][4]
			end
		end
		mx = max(mx,ct)
		g[n]+=1
		g = moveup(g,m)
		if g!=0
			for i in 2:n
				g[i]=max(g[i],g[i-1])
			end
		end
	end
	println(mx)
end

main()

Score : 300 points

Problem Statement

Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).

Consider a sequence A satisfying the following conditions:

A is a sequence of N positive integers.

1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.

Let us define a score of this sequence as follows:

The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)

Find the maximum possible score of A.

Constraints

All values in input are integers.

2 ≤ N ≤ 10

1 \leq M \leq 10

1 \leq Q \leq 50

1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )

0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )

(a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j)

1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )

Input

Input is given from Standard Input in the following format:

N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q

Output

Print the maximum possible score of A.

Sample Input 1

3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

Sample Output 1

110

When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

Sample Input 2

4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328

Sample Output 2

357500

Sample Input 3

10 10 1
1 10 9 1

Sample Output 3

1


Score : 400 points

Problem Statement

Given are integers A, B, and N.

Find the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N.

Here floor(t) denotes the greatest integer not greater than the real number t.

Constraints

1 ≤ A ≤ 10^{6}

1 ≤ B ≤ 10^{12}

1 ≤ N ≤ 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B N

Output

Print the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N, as an integer.

Sample Input 1

5 7 4

Sample Output 1

2

When x=3, floor(Ax/B)-A×floor(x/B) = floor(15/7) - 5×floor(3/7) = 2. This is the maximum value possible.

Sample Input 2

11 10 9

Sample Output 2

9


function main()
    a, b, n = parse.(UInt128, split(readline()))
    before = f(one(UInt128), a, b)
    hist = UInt128[before]
    local res
    for x in 2:n
        y = f(UInt128(x), a, b)
        push!(hist, y)
        if before == y
            break
        end
    end
    println(UInt128(maximum(hist)))
end

f(x,a,b)::UInt128 = floor(a*x/b) - a*floor(x/b)

main()


function main()
    a, b, n = parse.(UInt128, split(readline()))
    before = f(one(UInt128), a, b)
    hist = UInt128[before]
    local res
    for x in 2:n
        y = f(x, a, b)
        push!(hist, y)
        if before == y
            break
        end
    end
    println(UInt128(maximum(hist)))
end

f(x,a,b)::UInt128 = floor(a*x/b) - a*floor(x/b)

main()


a,b,n=parse.(Int128,split(readline()))
ans=0
for i=1:n
  global ans=max(ans,fld(a*i,b)-a*fld(i,b))
end
print(ans)


import Base.parse
import Base.StringVector

parse(::Type{String},  str::AbstractString) = str

isdelim(x::UInt8, xs::Set{UInt8}) = x in xs

function myreaduntil(s::IO, delims::Set{UInt8})::Vector{UInt8}
    out = StringVector(0)
    c::UInt8 = 0x00
    while !eof(s)
        c = read(s, UInt8)
        if !isdelim(c, delims)
            break
        end
    end
    push!(out, c)
    if eof(s)
        return out
    end
    while !eof(s)
        c = read(s, UInt8)
        if isdelim(c, delims)
            break
        end
        push!(out, c)
    end
    return out
end

delimset = Set([0x0a, 0x20])

function readword(io::IO = stdin, delims = delimset)::String
    word = myreaduntil(io, delims)
    i = length(word)
    if i == 0 || word[i] != 0x0a
        return String(word)
    elseif i < 2 || word[i-1] != 0x0d
        return String(resize!(word,i-1))
    else
        return String(resize!(word,i-2))
    end
end

pread(ty) = parse(ty, readword())

reads(tys...)::Tuple{tys...} = Tuple{tys...}(pread(ty) for ty in tys)

readvec(tys::Tuple , len::Signed)::Vector{Tuple{tys...}} = @inbounds Tuple{tys...}[reads(tys...) for i in 1:len]

readvec(ty::Type, len::Signed)::Vector{ty} = @inbounds ty[pread(ty) for i in 1:len]

function readmat(ty::Type, s...)::Matrix{ty}
    v = Matrix{ty}(undef, s...)
    @inbounds for i = 1:s[1]
        v[i,:] = readvec(ty, s[2])
    end
    v
end

val(a,b,x) = floor(a*x/b) - a * floor(x/b)

function main()
    A,B,N = reads(Int,Int,Int)
    fi = 1
    fv = val(A,B,fi)
    ei = N
    ev = val(A,B,ei)
    ci = (fi+ei)÷2
    cv = val(A,B,ci)
    while true
        if fv < cv
            fi = ci
            fv = cv
        else
            ei = ci
            ev = cv
        end
        ci = (fi+ei)÷2
        cv = val(A,B,ci)
        if ci+1 >= ei
            println(Int(max(fv,cv,ev)))
            break
        end
    end
end

main()


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

function f(A,B,x)
    return div(A * x , B) - A * (div(x , B))
end

function main()
    A,B,N = parseMap(split(readline()))
    before_ans = -1
    for i in 1:N
        if f(A,B,i) < before_ans
            ans = f(A,B,i)
            return ans
        end
    end
    return f(A,B,N)
end

println(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()
    a,b,n=parseMap(split(readline()))
    tmp=fld(n,b)*b-1
    println(max(big(a*tmp÷b)-big(a*(tmp÷b)),big(a*n÷b)-big(a*(n÷b))))
end
main()


function main()
	N=parse(Int,readline())
	A=map(x->parse(Int,x),split(readline()))
	G=[Int[] for _=1:N]
	for i=2:N
		u,v=map(x->parse(Int,x),split(readline()))
		push!(G[u],v)
		push!(G[v],u)
	end
	dp=ones(Int,N+1)*1145141919
	dp[1]=0
	ans=zeros(Int,N)
	function dfs(u,p,R)
		l=1
		r=R
		while r-l>1
			m=div(l+r,2)
			if dp[m]<A[u]
				l=m
			else
				r=m
			end
		end
		pre=dp[r]
		dp[r]=A[u]
		if R==r
			R+=1
		end
		ans[u]=R-2
		for v=G[u]
			if v!=p
				dfs(v,u,R)
			end
		end
		dp[r]=pre
	end
	dfs(1,0,2)
	println.(ans)
end
main()

Score : 600 points

Problem Statement

We have a tree with N vertices, whose i-th edge connects Vertex u_i and Vertex v_i.
Vertex i has an integer a_i written on it.
For every integer k from 1 through N, solve the following problem:

We will make a sequence by lining up the integers written on the vertices along the shortest path from Vertex 1 to Vertex k, in the order they appear. Find the length of the longest increasing subsequence of this sequence.

Here, the longest increasing subsequence of a sequence A of length L is the subsequence A_{i_1} , A_{i_2} , ... , A_{i_M} with the greatest possible value of M such that 1 \leq i_1 < i_2 < ... < i_M \leq L and A_{i_1} < A_{i_2} < ... < A_{i_M}.

Constraints

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

1 \leq a_i \leq 10^9

1 \leq u_i , v_i \leq N

u_i \neq v_i

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

Output

Print N lines. The k-th line, print the length of the longest increasing subsequence of the sequence obtained from the shortest path from Vertex 1 to Vertex k.

Sample Input 1

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

Sample Output 1

1
2
3
3
4
4
5
2
2
3

For example, the sequence A obtained from the shortest path from Vertex 1 to Vertex 5 is 1,2,5,3,4. Its longest increasing subsequence is A_1, A_2, A_4, A_5, with the length of 4.


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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	e = [Int[] for i in 1:n]
	for i in 1:n-1
		u,v = readline() |> split |> parseMap
		push!(e[u],v)
		push!(e[v],u)
	end
	t = zeros(Int,n)
	ans = zeros(Int,n)
	lis = 10^18*ones(Int,n)
	bef=Tuple{Int,Int}[(0,0) for i in 1:n]
	sf = zeros(Int,n)
	sf[1]=1
	t[1]=1
	ans[1]=1
	lis[1]=a[1]
	bef[1]=(1,a[1])
	stack = Int[1]
	while !isempty(stack)
		now = pop!(stack)
		if sf[now]==0
			push!(stack,t[now])
			sf[now]=1
		end
		f = 0
		for i in e[now]
			if t[i]==0
				f = 1
				t[i]=now
				push!(stack,i)
				break
			end
		end
		if f == 0
			k = bef[now][1]
			lis[k]=bef[now][2]
		end
	end
	for i in 1:n
		println(ans[i])
	end
end

main()


let
  S, W = map(x -> parse(Int, x), readline() |> split)
  if S <= W
    println("unsafe")
  else
  	println("safe")
  end
end

Score : 100 points

Problem Statement

There are S sheep and W wolves.

If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep.

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Constraints

1 \leq S \leq 100

1 \leq W \leq 100

Input

Input is given from Standard Input in the following format:

S W

Output

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Sample Input 1

4 5

Sample Output 1

unsafe

There are four sheep and five wolves. The number of wolves is not less than that of sheep, so they will attack them.

Sample Input 2

100 2

Sample Output 2

safe

Many a sheep drive away two wolves.

Sample Input 3

10 10

Sample Output 3

unsafe


S,W=readline()|>split|>x->parse.(Int,x)
println(S<=W ? "unsafe" : "safe")


n=parse(Int,readline())
v=Array{String}(undef,0)
for i = 1:n
  push!(v,readline())
end
print(length(unique(v)))

Score : 300 points

Problem Statement

You drew lottery N times. In the i-th draw, you got an item of the kind represented by a string S_i.

How many kinds of items did you get?

Constraints

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

S_i consists of lowercase English letters and has a length between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print the number of kinds of items you got.

Sample Input 1

3
apple
orange
apple

Sample Output 1

2

You got two kinds of items: apple and orange.

Sample Input 2

5
grape
grape
grape
grape
grape

Sample Output 2

1

Sample Input 3

4
aaaa
a
aaa
aa

Sample Output 3

4


a = readlines()[2:end]
println(length(Set(a)))


I = readlines()
s = ""
a = []
for i in I[2:end]
    if i != s && isempty(findall(a .== i)) && isempty(findall(length.(a) .== i))
        push!(a, i)
    end
    global s = i
end
println(length(a))


function readintegers()
    [parse(Int, x) for x in split(readline())]
end

function solve()
    n, m = readintegers()
    a = readintegers()
    n - sum(a) < 0 ? -1 : n - sum(a)
end

println(solve())

Score : 200 points

Problem Statement

Takahashi has N days of summer vacation.

His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.

He cannot do multiple assignments on the same day, or hang out on a day he does an assignment.

What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?

If Takahashi cannot finish all the assignments during the vacation, print -1 instead.

Constraints

1 \leq N \leq 10^6

1 \leq M \leq 10^4

1 \leq A_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_M

Output

Print the maximum number of days Takahashi can hang out during the vacation, or -1.

Sample Input 1

41 2
5 6

Sample Output 1

30

For example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out.

Sample Input 2

10 2
5 6

Sample Output 2

-1

He cannot finish his assignments.

Sample Input 3

11 2
5 6

Sample Output 3

0

He can finish his assignments, but he will have no time to hang out.

Sample Input 4

314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3

Sample Output 4

9


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

function main()
	n,m = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	println(max(n-sum(a),-1))
end

main()


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

function main()
    N = parseInt(readline())
    A= parseMap(split(readline()))
    B = zeros(Int,N)
    for i = 1:(N-1)
        B[A[i]] += 1
    end
    for i = 1:N
        println(B[i])
    end
end

main()

Score : 300 points

Problem Statement

A company has N members, who are assigned ID numbers 1, ..., N.

Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.

When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.

You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Constraints

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

1 \leq A_i < i

Input

Input is given from Standard Input in the following format:

N
A_2 ... A_N

Output

For each of the members numbered 1, 2, ..., N, print the number of immediate subordinates it has, in its own line.

Sample Input 1

5
1 1 2 2

Sample Output 1

2
2
0
0
0

The member numbered 1 has two immediate subordinates: the members numbered 2 and 3.

The member numbered 2 has two immediate subordinates: the members numbered 4 and 5.

The members numbered 3, 4, and 5 do not have immediate subordinates.

Sample Input 2

10
1 1 1 1 1 1 1 1 1

Sample Output 2

9
0
0
0
0
0
0
0
0
0

Sample Input 3

7
1 2 3 4 5 6

Sample Output 3

1
1
1
1
1
1
0


parseline(str) = parse.(Int, split(str))
function SumOfLargeNumbers()
    N, K = parseline(readline())
    nums = zeros(Int, N-K+2)
    idx = 1
    for M in K:N+1
        nums[N-M+2] = (M*(N-M+1) + 1)%(10^9+7)
    end
    return println(sum(nums)%(10^9+7))
end
SumOfLargeNumbers()


const mod=10^9+7
let
  n,k=map(x->parse(Int,x),readline()|>split)
  ans = Int128(0)
  for i in k:n+1
   ans+=n*i-i*i+i+1
  end

  println(ans%mod)
end


function solve()
    n = parse(Int, readline())
    a = [parse(Int, x) for x in split(readline())]

    z = [(a[i], i) for i=1:n]
    sort!(z, rev=true)

    dp = zeros(Int, (n+1, n+1))
    for s=1:n, i=0:s
        li = i
        ri = s-i
        l = if li > 0
            dp[li, ri+1] + z[s][1] * abs(li - z[s][2])
        else
            0
        end
        r = if ri > 0
            dp[li+1, ri] + z[s][1] * abs((n-ri+1) - z[s][2])
        else
            0
        end
        dp[li+1, ri+1] = max(l, r)
    end
    maximum([dp[i+1, n-i+1] for i=0:n])
end

println(solve())

Score : 500 points

Problem Statement

There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.

You can rearrange these children just one time in any order you like.

When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.

Find the maximum total happiness points the children can earn.

Constraints

2 \leq N \leq 2000

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum total happiness points the children can earn.

Sample Input 1

4
1 3 4 2

Sample Output 1

20

If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.

Sample Input 2

6
5 5 6 1 1 1

Sample Output 2

58

Sample Input 3

6
8 6 9 1 2 1

Sample Output 3

85


println(sum(i for i in 1:parse(Int,readline()) if i%3!=0&&i%5!=0))

Score : 200 points

Problem Statement

Let us define the FizzBuzz sequence a_1,a_2,... as follows:

If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.

If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.

If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.

If none of the above holds, a_i=i.

Find the sum of all numbers among the first N terms of the FizzBuzz sequence.

Constraints

1 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of all numbers among the first N terms of the FizzBuzz sequence.

Sample Input 1

15

Sample Output 1

60

The first 15 terms of the FizzBuzz sequence are:

1,2,\mbox{Fizz},4,\mbox{Buzz},\mbox{Fizz},7,8,\mbox{Fizz},\mbox{Buzz},11,\mbox{Fizz},13,14,\mbox{FizzBuzz}

Among them, numbers are 1,2,4,7,8,11,13,14, and the sum of them is 60.

Sample Input 2

1000000

Sample Output 2

266666333332

Watch out for overflow.


x = parse(Int, readline())
function fizzbuzz(n)
    a = div(n,3)
    b = div(n,5)
    c = div(n,15)
    A = (3 + a*3)/2*a
    B = (5 + b*5)/2*b
    C = (15+ c*15)/2*c
    N = (1+n)*n/2
    return Int(N - A - B + C)
end

println(fizzbuzz(x))


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

function main()
    N,K = parseMap(split(readline()))
    println(min(N-div(N,K)*K, abs(N-div(N,K)*K-K)))
end

main()

Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0


x = parse.(split(readline()))
N = x[1]
K = x[2]
X = N%K
if N==0
    println(0)
elseif N < K
    if(N < K/2)
        println(N)
    else
        prinln(K-N)
    end
else # K <= N
    if X > K-X
    	println(K-X)
    else
    	println(X)
  	end
end


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

if abs(N-K)<K
    N=mod(N,abs(N-K))
else
    N=mod(N,K)
end

println(N)


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

function main()
	n,k,c = readline() |> split |> parseMap
	s = readline() |> chomp
	a = Int[]
	b = Int[]
	f = 0
	fc  = 0
	g = n+1
	gc = 0
	for i in 1:n
		if s[i]=='o'
			if f<i&&fc<k
				push!(a,i)
				f=i+c
				fc+=1
			end
		end
		if s[n+1-i]=='o'
			if g>n+1-i&&gc<k
				push!(b,n+1-i)
				g = n+1-i-c
				gc+=1
			end
		end
	end
	for i in 1:length(a)
		if a[i]==b[length(a)+1-i]
			println(a[i])
		end
	end
end

main()

Score : 500 points

Problem Statement

Takahashi has decided to work on K days of his choice from the N days starting with tomorrow.

You are given an integer C and a string S. Takahashi will choose his workdays as follows:

After working for a day, he will refrain from working on the subsequent C days.

If the i-th character of S is x, he will not work on Day i, where Day 1 is tomorrow, Day 2 is the day after tomorrow, and so on.

Find all days on which Takahashi is bound to work.

Constraints

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

1 \leq K \leq N

0 \leq C \leq N

The length of S is N.

Each character of S is o or x.

Takahashi can choose his workdays so that the conditions in Problem Statement are satisfied.

Input

Input is given from Standard Input in the following format:

N K C
S

Output

Print all days on which Takahashi is bound to work in ascending order, one per line.

Sample Input 1

11 3 2
ooxxxoxxxoo

Sample Output 1

6

Takahashi is going to work on 3 days out of the 11 days. After working for a day, he will refrain from working on the subsequent 2 days.

There are four possible choices for his workdays: Day 1,6,10, Day 1,6,11, Day 2,6,10, and Day 2,6,11.

Thus, he is bound to work on Day 6.

Sample Input 2

5 2 3
ooxoo

Sample Output 2

1
5

There is only one possible choice for his workdays: Day 1,5.

Sample Input 3

5 1 0
ooooo

Sample Output 3

There may be no days on which he is bound to work.

Sample Input 4

16 4 3
ooxxoxoxxxoxoxxo

Sample Output 4

11
16


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

function main()
    K, N = parseMap(split(readline()))
    A = parseMap(split(readline()))
    A = [A;A[1]+K]
    B = diff(A)
    C = sum(B) - maximum(B)
    println(C)
end

main()

Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

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

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.


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,x,y=parseMap(split(readline()))
    dis=[[j-i for j in 1:n] for i in 1:n]
    for i in 1:n
        for j in 1:n
            dis[i][j]=min(dis[i][j],abs(i-x)+abs(j-y)+1)
        end
    end
    for i in 1:n-1
        res=0
        for j in 1:n
            res+=count(x->x==i,dis[j])
        end
        println(res)
    end
end
main()

Score : 400 points

Problem Statement

We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:

For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1.

There is an edge between Vertex X and Vertex Y.

For each k=1,2,...,N-1, solve the problem below:

Find the number of pairs of integers (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j in G is k.

Constraints

3 \leq N \leq 2 \times 10^3

1 \leq X,Y \leq N

X+1 < Y

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

For each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem.

Sample Input 1

5 2 4

Sample Output 1

5
4
1
0

The graph in this input is as follows:

There are five pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\,,(2,3)\,,(2,4)\,,(3,4)\,,(4,5).

There are four pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\,,(1,4)\,,(2,5)\,,(3,5).

There is one pair (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5).

There are no pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 4.

Sample Input 2

3 1 3

Sample Output 2

3
0

The graph in this input is as follows:

Sample Input 3

7 3 7

Sample Output 3

7
8
4
2
0
0

Sample Input 4

10 4 8

Sample Output 4

10
12
10
8
4
1
0
0
0


s=chomp(readline())
ss=s[1:fld(length(s),2)]
function kai(s)
    l=length(s)
    return s[1:fld(l,2)]==s[end:-1:cld(l,2)+1] ? true: false
end
print(kai(s) && kai(ss)? "Yes": "No")

Score : 200 points

Problem Statement

A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:

S is a palindrome.

Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome.

The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.

Determine whether S is a strong palindrome.

Constraints

S consists of lowercase English letters.

The length of S is an odd number between 3 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

If S is a strong palindrome, print Yes;
otherwise, print No.

Sample Input 1

akasaka

Sample Output 1

Yes

S is akasaka.

The string formed by the 1-st through the 3-rd characters is aka.

The string formed by the 5-th through the 7-th characters is aka.
All of these are palindromes, so S is a strong palindrome.

Sample Input 2

level

Sample Output 2

No

Sample Input 3

atcoder

Sample Output 3

No


s=chomp(readline())

n=length(s)
flag="Yes"
l=div(n,2)
for i in 1:l
    if s[i]!=s[n-i+1]
        flag="No"
    end
end
l=div(div(n-1,2),2)
for i in 1:l
    if s[i]!=s[div(n-1,2)-i+1]
        flag="No"
    end
end

for i in div(n+3,2):n
    ss=n-(i-div(n+3,2))
    if s[i]!=s[ss]
        flag="No"
    end
end

print(flag)


function main()

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

  dict = Dict()

  for i in 1:N
    dict[A[i]] = get(dict, A[i], 0) + 1
  end

  ans_dict = Dict()

  for i in keys(dict)
    n = dict[i]
    ans_dict[i] = div(n * (n-1), 2)
  end

  ans = sum(values(ans_dict))

  for i in 1:N
    n = dict[A[i]]
    println(ans - (n-1))
  end

end

main()

Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5


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

function main()
	n = readline() |> parseInt
	a = parseMap(split(chomp(readline()),""))
	sa = sum(a)
	f = 0
	if n==2
		println(abs(a[1]-a[2]))
	else
		if (a[1]==1&&sa==3*(n-1)+1)||(a[n]==1&&sa==1+3*(n-1))||(a[1]==3&&sa==n+2)||(a[n]==3&&sa==n+2)
			println(2)
		else
			bb = Int[]
			b = zeros(Int,n-1)
			l = 1
			push!(bb,abs(a[1]-a[2]))
			b[1]=abs(a[1]-a[2])
			for i in 2:n-1
				v = abs(a[i]-a[i+1])
				b[i]=v
				if v!=0||bb[l]!=0
					push!(bb,v)
					l+=1
				end
			end
			if length(bb)==1
				println(bb[1])
			elseif length(bb)==2
				println(abs(bb[1]-bb[2]))
			else
				c = Int[]
				m = 1
				push!(c,abs(b[1]-b[2]))
				for i in 2:n-2
					v = abs(b[i]-b[i+1])
					if v!=0||c[m]!=0
						push!(c,v)
						m+=1
					end
				end
				if length(c)==1
					println(c[1])
				elseif length(c)==2
					println(abs(c[1]-c[2]))
				else
					v1 = zeros(Int,m)
					v2 = zeros(Int,m)
					v3 = ones(Int,m)
					v4 = ones(Int,m)
					v1[1]=1
					v2[m]=1
					v3[1]=0
					v4[m]=0
					println(c==v1||c==v2||c==v3||c==v4?1:0)
				end
			end
		end
	end
end

main()

Score : 700 points

Problem Statement

Given is a sequence of N digits a_1a_2\ldots a_N, where each element is 1, 2, or 3.
Let x_{i,j} defined as follows:

x_{1,j} := a_j \quad (1 \leq j \leq N)

x_{i,j} := | x_{i-1,j} - x_{i-1,j+1} | \quad (2 \leq i \leq N and 1 \leq j \leq N+1-i)

Find x_{N,1}.

Constraints

2 \leq N \leq 10^6

a_i = 1,2,3 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
a_1a_2\ldotsa_N

Output

Print x_{N,1}.

Sample Input 1

4
1231

Sample Output 1

1

x_{1,1},x_{1,2},x_{1,3},x_{1,4} are respectively 1,2,3,1.

x_{2,1},x_{2,2},x_{2,3} are respectively |1-2| = 1,|2-3| = 1,|3-1| = 2.

x_{3,1},x_{3,2} are respectively |1-1| = 0,|1-2| = 1.

Finally, x_{4,1} = |0-1| = 1, so the answer is 1.

Sample Input 2

10
2311312312

Sample Output 2

0


function main()

  (H,W) = map(x -> parse(BigInt,x), split(readline()))

  ans = 0

  if H == 1 || W == 1
  	ans = 1
  elseif H % 2 == 0
    ans += W*div(H,2)
  else
    ans += W*div(H,2)
    ans += ceil(Int,W/2)
  end

  println(ans)

end

main()

Score : 200 points

Problem Statement

We have a board with H horizontal rows and W vertical columns of squares.
There is a bishop at the top-left square on this board.
How many squares can this bishop reach by zero or more movements?

Here the bishop can only move diagonally.
More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds:

r_1 + c_1 = r_2 + c_2

r_1 - c_1 = r_2 - c_2

For example, in the following figure, the bishop can move to any of the red squares in one move:

Constraints

1 \leq H, W \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H \ W

Output

Print the number of squares the bishop can reach.

Sample Input 1

4 5

Sample Output 1

10

The bishop can reach the cyan squares in the following figure:

Sample Input 2

7 3

Sample Output 2

11

The bishop can reach the cyan squares in the following figure:

Sample Input 3

1000000000 1000000000

Sample Output 3

500000000000000000


s=readline()
println(s[1]≠s[2]||s[2]≠s[3] ? "YES" : "NO")

Score : 100 points

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

Determine if there is a pair of stations that will be connected by a bus service.

Constraints

Each character of S is A or B.

|S| = 3

Input

Input is given from Standard Input in the following format:

S

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.

Sample Input 1

ABA

Sample Output 1

Yes

Company A operates Station 1 and 3, while Company B operates Station 2.

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.

Sample Input 2

BBA

Sample Output 2

Yes

Company B operates Station 1 and 2, while Company A operates Station 3.

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.

Sample Input 3

BBB

Sample Output 3

No

Company B operates all the stations. Thus, there will be no bus service, so print No.


function main()

  S = split(chomp(readline()),"")

  if in("A",S) && in("B",S)
    println("Yes")
  else
    println("No")
  end

end

main()


function main()

    N, a, b = map(parseInt, split(readline()))

    z = N % (a + b)
    y = (N / (a + b))

    if (z < a)
        z = a
    else
        z = z

        ans = y * a + z

    end

    return ans
end

main()

Score : 200 points

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

Initially, there is no ball placed.

Takahashi, who is very patient, will do the following operation 10^{100} times:

Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.

How many blue balls will be there among the first N balls in the row of balls made this way?

Constraints

1 \leq N \leq 10^{18}

A, B \geq 0

0 < A + B \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of blue balls that will be there among the first N balls in the row of balls.

Sample Input 1

8 3 4

Sample Output 1

4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

Sample Input 2

8 0 4

Sample Output 2

0

He placed only red balls from the beginning.

Sample Input 3

6 2 4

Sample Output 3

2

Among bbrrrr, there are two blue balls.


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

function main()
    A,B = parseMap(split(readline()))
    c = -1
    for i = 10000:-1:1
        if floor(i*0.08) == A && floor(i*0.1) == B
            c = i
        end
    end
    println(c)
end

main()

Score : 300 points

Problem Statement

Find the price of a product before tax such that, when the consumption tax rate is 8 percent and 10 percent, the amount of consumption tax levied on it is A yen and B yen, respectively. (Yen is the currency of Japan.)

Here, the price before tax must be a positive integer, and the amount of consumption tax is rounded down to the nearest integer.

If multiple prices satisfy the condition, print the lowest such price; if no price satisfies the condition, print -1.

Constraints

1 \leq A \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If there is a price that satisfies the condition, print an integer representing the lowest such price; otherwise, print -1.

Sample Input 1

2 2

Sample Output 1

25

If the price of a product before tax is 25 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 25 \times 0.08 \rfloor = \lfloor 2 \rfloor = 2 yen.

When the consumption tax rate is 10 percent: \lfloor 25 \times 0.1 \rfloor = \lfloor 2.5 \rfloor = 2 yen.

Thus, the price of 25 yen satisfies the condition. There are other possible prices, such as 26 yen, but print the minimum such price, 25.

Sample Input 2

8 10

Sample Output 2

100

If the price of a product before tax is 100 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 100 \times 0.08 \rfloor = 8 yen.

When the consumption tax rate is 10 percent: \lfloor 100 \times 0.1 \rfloor = 10 yen.

Sample Input 3

19 99

Sample Output 3

-1

There is no price before tax satisfying this condition, so print -1.


parseline(str=readline()) = parse.(Int, split(str))
function StringFormation()
    S = readline()
    Q = parseline()
    queries = split.(readlines())
    inverse = false
    ans = [s]
    for q in queries
        l = length(q)
        l≡0 && continue
        if l≡1
            inverse = !inverse
        else
            head = q[2]=="1" ? true : false
            head = inverse ? !head : head
            head ? pushfirst!(S, q[3]) : push!(S, q[3])
        end
    end
    ret = prod(ans)
    println(inverse ? ret[end:-1:1] : ret))
end
StringFormation()

Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy


parseline(str=readline()) = parse.(Int, split(str))
function StringFormation()
    S = readline()
    Q = parseline()
    queries = split.(readlines())
    inverse = false
    for q in queries
        l = length(q)
        l≡0 && continue
        if l≡1
            inverse = !inverse
        else
            head = q[2]=="1" ? true : false
            head = inverse ? !head : head
            S = head ? q[3]*S : S*q[3]
        end
    end
    println(inverse ? S[end:-1:1] : S)
end
StringFormation()


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

function main()
    s = chomp(readline())
    q = parseInt(readline())
    muki=true
    for i in 1:q
        query=split(readline())
        if query[1]=="1"
            muki=!muki
        else
            if (query[2]=="1" && muki) || (query[2]=="2" && !muki)
                s=join([query[3],s])
            else
                s=join([s,query[3]])
            end
        end
    end
    ans=s
    if muki
        println(ans)
    else
        println(reverse(ans))
    end
end
main()