# SPDX-License-Identifier: X11
# 2020-08-30
# Multiplication 2 (200pt)

const MAX = 1000000000000000000

function main()
  N = parse(Int, readline())
  Astr = readline()
  if occursin(r"^0 | 0 | 0$", Astr)
    println("0")
    return
  end
  Avec = split(Astr, " ")

  T = parse(Int, Avec[1])
  for i ∈ 2:N
    A = parse(Int, Avec[i])
    if big(T) * A > MAX
      println("-1")
      return
    else
      T *= A
    end
  end

  println(T)
end

main()

Score : 200 points

Problem Statement

Given N integers A_1, ..., A_N, compute A_1 \times ... \times A_N.

However, if the result exceeds 10^{18}, print -1 instead.

Constraints

2 \leq N \leq 10^5

0 \leq A_i \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the value A_1 \times ... \times A_N as an integer, or -1 if the value exceeds 10^{18}.

Sample Input 1

2
1000000000 1000000000

Sample Output 1

1000000000000000000

We have 1000000000 \times 1000000000 = 1000000000000000000.

Sample Input 2

3
101 9901 999999000001

Sample Output 2

-1

We have 101 \times 9901 \times 999999000001 = 1000000000000000001, which exceeds 10^{18}, so we should print -1 instead.

Sample Input 3

31
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 0

Sample Output 3

0


# SPDX-License-Identifier: X11
# 2020-08-30
# Multiplication 2 (200pt)

const MAX = 1000000000000000000

function main()
  N = parse(Int, readline())
  Astr = readline()
  if occursin(r"^0 | 0 | 0$", Astr)
    println("0")
    return
  end
  Avec = split(Astr, " ")

  T = parse(Int, Avec[1])
  for i ∈ 2:N
    A = parse(Int, Avec[i])
    if T * A > MAX
      println("-1")
      return
    else
      T *= A
    end
  end

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


# SPDX-License-Identifier: X11
# 2020-08-30
# Multiplication 2 (200pt)

const MAX = 1000000000000000000

function main()
  N = parse(Int, readline())
  Astr = readline()
  if occursin(r"^0 | 0 | 0$", Astr)
    println("0")
    return
  end
  Avec = split(Astr, " ")

  T = parse(Int, Avec[1])
  for i ∈ 2:N
    A = parse(Int, Avec[i])
    if (T *= A) > MAX
      println("-1")
      return
    end
  end

  println(T)
end

main()


function main()

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

  ans::BigInt = 1

  if in(0, A)
    ans = 0
  end

  for i in A

    if ans * i > 10^18
      ans = -1
      break
    else
      ans *= i
    end

  end

  println(ans)

end

main()


function main()

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

  ans = 1

  if in(0, A)
    ans = 0
  end

  for i in A

    if ans * i > 10^18
      ans = -1
      break
    else
      ans *= i
    end

  end

  println(ans)

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()))
sort!(A)
res = 1
if A[1] == 0
    println(0)
else
    for a in A
        if a > (10^18 ÷ res)
            res = -1
            break
        else
            res *= a
        end
    end
    println(res)
end

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()))
sort!(A)
res = 1
if A[1] == 0
    println(0)
else
    for a in A
        if a > (10^18 ÷ res)
            res = -1
            break
        else
            res *= a
        end
    end

end
println(res)
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()))
sort!(A)
res = 1
if A[1] == 0
    println(0)
else
    for a in A
        if a >= (10^18 / res)
            res = -1
            break
        else
            res *= a
        end
    end

end
println(res)
end

main()


function solve(a)
  if minimum(a) == 0
    return 0
  else
    k = 10^18
    m = 1
    for ai in a
      k ÷ ai < ai && (m = -1; break)
      m *= ai
    end
    return m
  end
end

n = parse(Int, readline())
a = parse.([Int64], split(readline()))

println(solve(a))


function main()
    N = parse(Int128,readline())
    A = parse.(Int128,split(readline()))
    a = 1
    for i in 1:N
        if A[i] == 0
            a = 0
        end
    end
    if a == 0
        println(0)
    else
        for i in 1:N
            a = a*A[i]
            if a > 10^18
                a = -1
                break
            end
        end
    println(a)
   end
end
main()


function main()
    N = parse(Int,readline())
    A = parse.(Int,split(readline()))
    a = 1
    for i in 1:N
        if A[i] == 0
            a = 0
        end
    end
    if a == 0
        println(0)
    else
        for i in 1:N
            a = a*A[i]
            if a > 10^18
                a = -1
                break
            end
        end
    println(a)
   end
end
main()


function main()
	N=parse(Int,readline())
	A=map(x->parse(Int,x),split(readline()))
	for c=A
		if c==0
			println(0)
			exit()
		end
	end
	T=1
	for c=A
		if T>div(A,c)
			println(-1)
			exit()
		end
		T*=c
	end
	println(T)
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 main()
  ans::BigInt=1
  n=pI(readline())
  l=pM(split(readline()))
  if 0 in l
    print(0)
    return 0
  end
  for i in l
    ans*=i
    if ans>10^18
      print(-1)
      return 0
    end
  end
  println(ans)
end
main()
if isdefined(Base, :mystdin);close(mystdin);end


function main()
  n = parse(Int128, readline())
  a = parse.(Int128, split(readline()))

  a = sort(a)

  ans::Int128 = 1
  for i = 1:n
    ans *= a[i]
    if (ans > 10^18)
      ans = -1
      break
    end
  end

  println(ans)
end

main()


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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	if minimum(a)==0
		println(0)
	else
		f = 0
		x::BigInt = 1
		for i in 1:n
			x = x*a[i]
			if x>10^18
				f = 1
			end
		end
		println(f==1 ? -1 : x)
	end
end

main()


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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap
	if minimum(a)==0
		println(0)
	else
		f = 0
		x = 1
		for i in 1:n
			x = x*a[i]
			if x>10^18
				f = 1
			end
		end
		println(f==1 ? -1 : x)
	end
end

main()


T=readline()
#println("Input: ",split(tmp))
N=parse(Int64,T)
T=readline()
A=parse.(Int128,split(T))
#println("Input: $N, $A")
#println("typeof each args... ",typeof(A))

if minimum(A) != 0
    let i = 1
        ans = 1
        while i <= N
            ans = ans * A[i]
            if ans > Int64(1e18)
                ans = -1
                break
            end
            i += 1
        end
    #    if ans < 0
    #        ans = -1
    #    end
        println(ans)
    end
else
    println(0)
end


T=readline()
#println("Input: ",split(tmp))
N=parse(Int64,T)
T=readline()
A=parse.(Int128,split(T))
#println("Input: $N, $A")
#println("typeof each args... ",typeof(A))
if minimum(A) != 0
    let i = 1
        ans = 1
        while i <= N
            ans = ans * A[i]
    #        if ans == 0
    #            break
    #        end
            if ans > Int64(1e18)
                global answer = -1
                break
            end
            i += 1
        end
    #    if ans < 0
    #        ans = -1
    #    end
        global answer = ans
    end
    println(answer)
else
    println(0)
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()
    n::Int=parseInt(readline())
    a::Vector{Int64}=sort(parseMap(split(readline())))
    ans::BigInt=1
    for i in a
        ans*=i
        if ans>10^18
            ans=-1
            break
        end
    end
    println(ans)
end
main()


if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
pI(x)=parse(BigInt,x)
pM(x::Array{SubString{String},1})=map(pI,x)
function main()
  n=pI(readline())
  l=pM(split(readline()))
  ans::BigInt=1
  for i=1:n;ans*=l[i];end
  ans=ans>10^18 ? -1 : ans
  print(ans)
end
main()
if isdefined(Base, :mystdin);close(mystdin);end


readint()=parse(Int,readline())
readints()=parse.(Int,split(readline()))
N=readint()
A=readints()

function ABC_B()
    s=1
    for i in A
        tmp=s*i
        if (tmp < 0 ) || (tmp>10^18) || (log(10^18)<(log(s)+log(i)))
            s=-1
            break
        end
        s*=i
    end
    s
end
if sum(A.==0)>0
    print(0)
else
    print(ABC_B())
end


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()
    N = pread(Int)
    a = readvec(Int, N)
    if count(x -> x == 0, a) > 0
        println(0)
        return
    end

    ans = 1
    befans = 1
    for i in a
        befans = ans
        ans *= i
        if ans > 10^18 || befans > ans
            println(-1)
            return
        end
    end
    println(ans)
end

main()


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

function main()
    N = parseInt(readline())
    A= parseMap(split(readline()))
    prod(A) <= 10^18 ? println(prod(A)) : println(-1)
end

main()


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

function main()
    N = parseInt(readline())
    A= parseMap(split(readline()))
    prod(A) <= 10^18 ? println(prod(A)) : println(-1)
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()

  prod = 1

  for a in A
    if a==0
      println(0)
      return
    end
  end
  for a in A

    if prod >=  10^18÷a + 1
      println(-1)
      return
    end
    prod = prod * a
  end
  println(prod)
end

main()


readint()=parse(Int,readline())
readints()=parse.(Int,split(readline()))
N=readint()
A=readints()

function ABC_B()
    s=1
    for i in A
        s*=i
        if (s < 0 ) || (s>10^18)
            s=-1
            break
        end
    end
    s
end
if sum(A.==0)>0
    print(0)
else
    print(ABC_B())
end


readint()=parse(Int,readline())
readints()=parse.(Int,split(readline()))
N=readint()
A=readints()

function ABC_B()
    s=1
    for i in A
        s*=i
        if (s < 0 ) || (s>10^18)
            s=-1
            break
        end
    end
    s
end
if sum(A.==0)==1
    print(0)
else
    print(ABC_B())
end

ABC_B()


if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
pI(x)=parse(BigInt,x)
pM(x::Array{SubString{String},1})=map(pI,x)
function main()
  n=pI(readline())
  ans::BigInt=*(pM(split(readline()))...)
  print(ans>10^18 ? -1 : ans)
end
main()
if isdefined(Base, :mystdin);close(mystdin);end


const double = Float64
const int = Int64

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

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

  prod = 1
  for a in A
    if a == 0
      println(0)
      return
    end

    if prod >=  10^18÷a + 1
      println(-1)
      return
    end
    prod = prod * a
  end
  println(prod)
end

main()


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()
    N = pread(Int)
    a = readvec(Int, N)
    ans = 1
    if count(x -> x == 0, a) > 0
        println(0)
        return
    end
    for i in a
        ans *= i
        println(ans)
        if ans > 10^18
            println(-1)
            return
        end
    end
    println(ans)
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 main()
  n=pI(readline())
  ans=*(pM(split(readline()))...)
  print(ans>10^18 ? -1 : ans)
end
main()
if isdefined(Base, :mystdin);close(mystdin);end


let
  N = parse(Int, readline())
  A = map(x -> parse(BigInt, x), readline() |> split)
  if prod(A) <= 10^18
    println(prod(A))
  else
    println(-1)
  end
end


function main()
    n = parse(Int, readline())
    a = parse.(Int, split(readline()))
    if 0 in a
        println(0)
        return
    end
    ans = 1;
    for i in a
        ans *= i
        if ans > 10^18
            println(-1)
            return
        end
    end

    println(ans)
end

main()


# SPDX-License-Identifier: X11
# 2020-08-31
# Multiplication 3 (300pt)

((A, B) -> println(trunc(Int64, A * B)))(
  parse(Int64, readuntil(stdin, " ")),
  rationalize(parse(Float64, readline())))

Score : 300 points

Problem Statement

Compute A \times B, truncate its fractional part, and print the result as an integer.

Constraints

0 \leq A \leq 10^{15}

0 \leq B < 10

A is an integer.

B is a number with two digits after the decimal point.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the answer as an integer.

Sample Input 1

198 1.10

Sample Output 1

217

We have 198 \times 1.10 = 217.8. After truncating the fractional part, we have the answer: 217.

Sample Input 2

1 0.01

Sample Output 2

0

Sample Input 3

1000000000000000 9.99

Sample Output 3

9990000000000000


# SPDX-License-Identifier: X11
# 2020-08-31
# Multiplication 3 (300pt)

((A, B) -> println(trunc(BigInt, A * B)))(
  parse(Int64, readuntil(stdin, " ")),
  parse(BigFloat, readline()))


function main()

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

  A = BigInt(A)
  B = BigInt(100B)

  ans::BigInt = div(A * B, 100)

  println(ans)

end

main()


function main()

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

  ans = div(A * 100B, 100)

  println(Int(ans))

end

main()


function main()
  a,b=split(readline())
  a=parse(Int,a)
  b=Int(b[1])*100+Int(b[3])*10+Int(b[4])-48*111
  println(div(a*b,100))
end
main()


a, b = map(parse,[Int,Float64],split(readline()))
println(div(a*Int(round(b*100)),100))


function main()
    s = split(readline())
    a = parse(Int, s[1])
    b = parse(Float64, s[2])

    println(Int(floor(a*b)))
end

main()


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

function main()
	a,b = readline() |> chomp |> split
	a = parse(BigInt, a)
	b = split(b,".")
	b = parseInt(b[2])+100*parseInt(b[1])
	println(div(a*b,100))
end

main()


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

function main()
	a,b = readline() |> chomp |> split
	a = parse(BigInt, a)
	b = trunc(BigInt,parse(Float64,b)*100)
	println(div(a*b,100))
end

main()


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

function main()
	a,b = readline() |> chomp |> split
	a = parse(BigInt, a)
	b = trunc(Int,parse(Float64,b)*100)
	println(div(a*b,100))
end

main()


parseF(x) = parse(Float64,x)
parseMap(x::Array{SubString{String}, 1}) = map(parseF, x)

function main()
    A,B = parseMap(split(readline()))
    c = floor(A*B)
    println(c)
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()
    a,b=split(readline())
    a=parse(BigFloat,a)
    b=parse(BigFloat,b)
    ans=Int128(floor(a*b))
    print(ans)
end
main()


tmp = readline()
#println("Input: ",split(tmp))
#println("Input: ",length(split(tmp)))
A=split(tmp)[1]
B=split(tmp)[2]

#println("Input: $A, $B")
#println("typeof each args... ",typeof(A))

c=parse(BigInt,A)*parse(BigFloat,B)
#c=parse(Int128,A)*parse(Float64,B)
#println("C: $c")
ans=(trunc(BigInt,c))
#println("Answer: $ans [min]")
println(ans)


parseF(x) = parse(Float64,x)
parseMap(x::Array{SubString{String}, 1}) = map(parseF, x)

function main()
    A,B = parseMap(split(readline()))
    A = Int(A)
    c = Int(floor(A*B))
    println(c)
end

main()


tmp = readline()
#println("Input: ",split(tmp))
#println("Input: ",length(split(tmp)))
A=split(tmp)[1]
B=split(tmp)[2]

#println("Input: $A, $B")
#println("typeof each args... ",typeof(A))

c=big(parse(Int128,A)*parse(Float64,B))
#println("C: $c")
ans=(trunc(Int128,c))
#println("Answer: $ans [min]")
println(ans)


# Q.1
tmp = readline()
#println("Input: ",split(tmp))
#println("Input: ",length(split(tmp)))
A=split(tmp)[1]
B=split(tmp)[2]

#println("Input: $A, $B")
#println("typeof each args... ",typeof(A))

c=parse(Int128,A)*parse(Float64,B)
#println("C: $c")
ans=(floor(Int128,c))
#println("Answer: $ans [min]")
println(ans)


Z=split(readline())
A=parse(Int,Z[1])
B=parse(Float64,Z[2])

B*=100
B=Int(round(B))
print(A*B ÷ 100)


const double = Float64
const int = Int64

parseint(x) = parse(double, x)
readdouble() = map(parseint, split(readline()))

function main()
  a,b = split(readline())
  a = parse(int, a)
  b = parse(double,b)
  #println(a," ",b)
  ba = round(int,100b)
  #bb = floor(int,100b)-100ba
  println((a * ba)÷100)

end

main()


# Q.1
tmp = readline()
#println("Input: ",split(tmp))
#println("Input: ",length(split(tmp)))
A=split(tmp)[1]
B=split(tmp)[2]

#println("Input: $A, $B")
#println("typeof each args... ",typeof(A))

c=parse(Int128,A)*parse(Float64,B)
ans=floor(Int128,c)
#println("Answer: $ans [min]")
println(ans)


# Q.1
tmp = readline()
#println("Input: ",split(tmp))
#println("Input: ",length(split(tmp)))
A=split(tmp)[1]
B=split(tmp)[2]

#println("Input: $A, $B")
#println("typeof each args... ",typeof(A))

c=parse(Int64,A)*parse(Float64,B)
ans=floor(Int64,c)
#println("Answer: $ans [min]")
println(ans)


# Q.1
tmp = readline()
#println("Input: ",split(tmp))
#println("Input: ",length(split(tmp)))
A=split(tmp)[1]
B=split(tmp)[2]

#println("Input: $A, $B")
#println("typeof each args... ",typeof(A))

c=parse(Int,A)*parse(Float64,B)
ans=floor(Int64,c)
#println("Answer: $ans [min]")
println(ans)


import Base.parse
import Base.StringVector
using Printf

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()
    a,b = reads(Int,String)
    bi,bf = map(x -> parse(Int, x), split(b, '.'))
    ans = a*bi+a*bf÷100
    @printf("%i\n",ans)
end

main()


import Base.parse
import Base.StringVector
using Printf

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()
    a,b = reads(Int,Float64)
    ans = floor(a*b)
    @printf("%i\n",ans)
end

main()


function main()
    s = split(readline())
    a = parse(BigInt, s[1])
    b = parse(BigFloat, s[2])

    println(Int(floor(a*b)))

end

main()


let
  A, B = map(x -> parse(BigFloat, x), readline() |> split)
  println(Int(floor(A*B)))
end


const double = Float64
const int = Int64

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

function main()
  a,b = readint()
  println(floor(int,a*b))

end

main()


let
  A, B = map(x -> parse(Float64, x), readline() |> split)
  println(Int(floor(A*B)))
end


parseF(x) = parse(Float64,x)
parseMap(x::Array{SubString{String}, 1}) = map(parseF, x)

function main()
    A,B = parseMap(split(readline()))
    println(Int(floor(A*B)))
end

main()


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

    current = 1
    path = [1]

    while a[current] ∉ path
        current = a[current]
        append!(path, current)
    end
    append!(path, a[current])

    if k < length(path)
        path[k+1]
    else
        cycle_start = findfirst(x->(x==path[end]), path)
        k = k - cycle_start
        path = path[cycle_start+1:end]
        k = k % length(path) + 1
        path[k]
    end
end

println(solve())

Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

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

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2


function main()
    N,K=parse.(Int64,split(readline()))
    A=parse.(Int64,split(readline()))
    logK::Int64=1
    while ((1<<logK) < K)
        logK+=1
    end
    doubling=zeros(Int64,logK,N)
    for i in 1:N
        global doubling[1,i]=A[i]
    end
    for t=1:logK-1
        for i=1:N
            global doubling[t+1,i]=doubling[t,doubling[t,i]]
        end
    end

    now::Int64=1
    k::Int64=1

    while K>0
        if K&1==1
            now=doubling[k,now]

        end
        K=K>>1
        k+=1
    end
    println(now)
end

main()


parseLine() = parse.(Int,split(readline()))

function solve()
    N, K = parseLine()
    A = parseLine()
    ans = 1
    while K > 0
        K&1==1 && (ans = A[ans])
        next = Vector{Int}(undef, N)
        for i in 1:N
            next[i] = A[A[i]]
        end
        A = next
        K >>= 1
    end
    println(ans)
end

solve()


parseLine(str) = parse.(Int, split(str))

function solve()
    N, K = parseLine(readline())
    A = parseLine(readline())
    cycle = Vector{Int}()
    cur = A[1]; length = 0
    seen = zeros(Bool, N)
    while true
        seen[cur] == true && break
        seen[cur] = true
        push!(cycle, cur)
        length += 1
        cur = A[cur]
    end
    start = findfirst(isequal(cur), cycle)
    C = length - start + 1
    ai = (K <= length) ? K : start+(K-start)%C
    println(cycle[ai])
end

solve()


using DataStructures
parseline(str) = parse.(Int, split(str))
function Teleporter()
    N, K = parseline(readline())
    A = parseline(readline())
    route = zeros(Int, N)
    visited = zeros(Int, N)
    route[1] = A[1]
    visited[A[1]] = 1
    pre = 0
    for k in 2:K
        a = A[route[k-1]]
        visited[a]==1 && (i=findfirst(route.==a);pre=length(route[1:i-1]);route = route[i:k-1]; break)
        visited[a] = 1
        route[k] = a
    end
    println(route[(K-pre-1)%length(route)+1])
end
Teleporter()


using DataStructures
parseline(str) = parse.(Int, split(str))
function Teleporter()
    N, K = parseline(readline())
    A = parseline(readline())
    route = zeros(Int, N)
    route[1] = A[1]
    pre = 0
    for k in 2:K
        a = A[route[k-1]]
        a in route && (i=findfirst(route.==a);pre=length(route[1:i-1]);route = route[i:k-1]; break)
        route[k] = a
    end
    println(route[(K-pre-1)%length(route)+1])
end
Teleporter()


using DataStructures
parseline(str) = parse.(Int, split(str))
function Teleporter()
    N, K = parseline(readline())
    A = parseline(readline())
    route = zeros(Int, N)
    route[1] = A[1]
    pre = 0
    for k in 2:K
        a = A[route[k-1]]
        a in route && (i=findfirst(route.==a);pre=length(route[1:i-1]);route = route[i:k-1]; break)
        route[k] = a
    end
    println(length(route)<N ? route[(K-pre-1)%length(route)+1] : route[end])
end
Teleporter()


function main()
    N,K = parse.(Int,split(readline()))
    A = parse.(Int,split(readline()))
    g_town = zeros(Int,N)
    reachs = zeros(Bool,N)
    p = 1
    first = second = -1
    for i in 1:N
        reachs[p] = true
        g_town[i] = p
        p = A[p]
        if reachs[p]
            first = findfirst(isequal(p),g_town)
            second = i+1
            g_town[second] = p
            break
        end
    end
    if K < second
        print(g_town[K+1])
    else
        k = K-first+1
        n = second - first
        print(g_town[first+k%n])
    end
end
main()


N,K=parse.(Int,split(readline()))
A=parse.(Int,split(readline()))

B=zeros(Int,N)

function telepo(nokori,nowstep,A,B)
    if B[nowstep]==0
        B[nowstep]=nokori
    else
        nokori%=(B[nowstep]-nokori)
    end
    if nokori == 0
        print(A[nowstep])
    else
        telepo(nokori-1,A[nowstep],A,B)
    end
end
telepo(K-1,1,A,B)


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

function main()
    n,k=parseMap(split(readline()))
    a=parseMap(split(readline()))
    INF=2<<32
    visited=[INF for i in 1:n]
    visited[1]=0
    now=1
    loop=(1,n)
    for i in 1:1000000
        if visited[a[now]]<INF
            loop=(a[now],now)
            break
        end
        visited[a[now]]=visited[now]+1
        now=a[now]
    end
    res=k-visited[loop[1]]
    if res<0
        res=k
    elseif loop[2]==loop[1]
        res=visited[loop[1]]
    else
        res=res%(visited[loop[2]]-visited[loop[1]]+1)+visited[loop[1]]
    end
    println(findfirst(x->x==res,visited))
end
main()


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

function main()
    n,k=parseMap(split(readline()))
    a=parseMap(split(readline()))
    INF=2<<32
    visited=[INF for i in 1:n]
    visited[1]=0
    now=1
    loop=(1,n)
    for i in 1:1000000
        if visited[a[now]]<INF
            loop=(a[now],now)
            break
        end
        visited[a[now]]=visited[now]+1
        now=a[now]
    end
    if visited[loop[1]]>k
        println(findfirst(x->x==k,visited))
    else
        if loop[2]-loop[1]==0
            res=0
        else
            res=k%(visited[loop[2]]-visited[loop[1]]+1)
        end
        println(findfirst(x->x==visited[loop[1]]+res,visited))
    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()
    n,k=parseMap(split(readline()))
    a=parseMap(split(readline()))
    INF=2<<32
    visited=[INF for i in 1:n]
    visited[1]=0
    now=1
    loop=(1,n)
    for i in 1:n
        if visited[a[now]]<INF
            loop=(a[now],now)
            break
        end
        visited[a[now]]=visited[now]+1
        now=a[now]
    end
    res=k%(loop[2]-loop[1])
    for i in 1:n
        if visited[i]==visited[loop[1]]+res
            println(i)
            break
        end
    end
end
main()


function main()
	N,K=map(x->parse(Int,x),split(readline()))
	A=map(x->parse(Int,x),split(readline()))
	vis=-ones(Int,N)
	now=1
	cnt=0
	pre=Int[]
	while vis[now]==-1
		vis[now]=cnt
		cnt+=1
		push!(pre,now)
		now=A[now]
	end
	if K<vis[now]
		println(pre[K])
	else
		K-=vis[now]
		K%=cnt-vis[now]
		println(pre[vis[now]+K+1])
	end
end
main()


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

function main()
N,K = parseMap(split(readline()))
A = parseMap(split(readline()))

start_city = 1
still_see = Set{Int}()
push!(still_see,1)
if N < K
for i in 1:K
    visit_city = A[start_city]
    if visit_city in still_see
        loop_begin_city = visit_city
        loop_fin_city = start_city
        loop_start_city = loop_fin_city
        loop_arrays = [loop_fin_city,]
        for j in 1:10^6
            loop_visit_city = A[loop_start_city]
            if loop_visit_city == loop_fin_city
                ans_idx = ((K - i) % length(loop_arrays)) - 1
                if ans_idx == 0
                    return loop_arrays[end]
                elseif ans_idx == -1
                    return loop_arrays[end]
                end
                return loop_arrays[ans_idx]
            else
            end
            push!(loop_arrays,loop_visit_city)
            loop_start_city = loop_visit_city
        end
    else
        push!(still_see,visit_city)
    end
    start_city = visit_city
end
else
    for i in 1:K
        visit_city = A[start_city]
        start_city = visit_city
        if i == K
            return visit_city
        end
    end
end
end

println(main())


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

function main()
N,K = parseMap(split(readline()))
A = parseMap(split(readline()))

start_city = 1
still_see = Set{Int}()
push!(still_see,1)
if N < K
for i in 1:K
    visit_city = A[start_city]
    if visit_city in still_see
        loop_begin_city = visit_city
        loop_fin_city = start_city
        loop_start_city = loop_fin_city
        loop_arrays = [loop_fin_city,]
        for j in 1:10^6
            loop_visit_city = A[loop_start_city]
            if loop_visit_city == loop_fin_city
                ans_idx = ((K - i) % length(loop_arrays)) - 1
                if ans_idx == 0
                    return loop_arrays[end]
                end
                return loop_arrays[ans_idx]
            else
            end
            push!(loop_arrays,loop_visit_city)
            loop_start_city = loop_visit_city
        end
    else
        push!(still_see,visit_city)
    end
    start_city = visit_city
end
else
    for i in 1:K
        visit_city = A[start_city]
        start_city = visit_city
        if i == K
            return visit_city
        end
    end
end
end

println(main())


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

    start = 1
    cur = 1
    last = 1
    times = 0
    hist = [cur]
    while true
        cur = a[cur]
        times += 1
        cur in hist && break
        push!(hist, cur)
        if times == k
            println(cur)
            return
        end

    end

    k  = k - floor(k/times)*times
    for i in 1:k
        cur = a[cur]
    end

    println(cur)

end

main()


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()
    N,K = reads(Int, Int)
    v = readvec(Int, N)
    checked = zeros(Int, N)
    np = 1
    lp = 0
    fi = 0
    sa = 0
    for i in 0:K
        if i == K
            println(np)
            return
        end
        if checked[np] != 0
            sa = i - checked[np]
            lp = np
            fi = i
            break
        end
        checked[np] = i
        np = v[np]
    end
    np = lp
    for i in 1:(K-fi)%sa
        np = v[np]
    end
    println(np)
end

main()


#!/usr/bin/env -S JULIA_VERSION=1.4.0 julia
# contest: abc167, problem: abc167_d, alphabet: D

function solve(N::Int, K::Int, A::Vector{Int})
  if A[1] == 1
    println(1)
    return
  end
  hist = zeros(Int, N+10)
  hist[0+1] = 1
  dic = Dict(1=>0)
  k = 1
  p = A[1]
  while true
    k0 = get!(dic, p, k)
    if k0 < k
      p0 = hist[k0+1]
      m = k - k0
      kK = k0 + (K - k0) % m
      println(hist[kK + 1])
      return
    end
    if p == A[p]
      println(p)
      return
    end
    hist[k+1] = p
    p = A[p]
    k += 1
  end
end

function main()
  tokens = Channel{String}(32)
  Task() do
    for line in eachline(@static VERSION < v"0.6" ? STDIN : stdin)
      startswith(line, '\0') && break
      for token in split(chomp(line))
        put!(tokens, token)
      end
    end
    close(tokens)
  end |> schedule
  N = parse(Int, take!(tokens))
  K = parse(Int, take!(tokens))
  A = similar(Vector{Int}, N)
  for i in 1:N
    A[i] = parse(Int, take!(tokens))
  end
  solve(N, K, A)
end

if isempty(get(ENV, "ATCODER_LOCAL", ""))
  isempty(ARGS) && main()
else
  @eval begin
    const samples = [("4 5\n3 2 4 1", "4"), ("6 727202214173249351\n6 5 2 5 3 2", "2")]
    function test(sampleids...)
      isempty(sampleids) && return test(collect(1:length(samples))...)
      ostdin, ostdout = @static VERSION < v"0.6" ? (STDIN, STDOUT) : (stdin, stdout)
      rd, wr = first(redirect_stdout()), last(redirect_stdin())
      try
        map(sampleids) do sampleid
          input, output = samples[sampleid]
          print(ostdout, "Testing sample #$(sampleid)..."); flush(ostdout)
          println(wr, input)
          println(wr, "\0")
          t = @elapsed main()
          println()
          result = strip(String(readavailable(rd)))
          if result == output
            println(ostdout, "OK ($t sec).")
            true
          else
            println(ostdout, "ERROR ($t sec)")
            println(ostdout, "== expected ==\n$output")
            println(ostdout, "== actual ==\n$result\n")
            false
          end
        end |> all
      finally
        redirect_stdin(ostdin)
        redirect_stdout(ostdout)
      end
    end
    submit() = test() && (prog = @__FILE__; run(`atcoder-submit $prog`))
    !isinteractive() && submit()
  end
end


function f()
    n,k=map(x->parse(Int,x),split(readline()))
    ni=zeros(Int,n)
    ni=map(x->parse(Int,x),split(readline()))
    flag=fill(-1,n)
    h=fill(-1,n)
    j=1
    c=0
    for i=1:k
        flag[j]=1
        c += 1
        j=ni[j]
        if flag[j] == 1
            s=1
            jn=j
            h[1]=jn
            j=ni[j]
            b=2
            while jn != j
                h[b]=j
                j=ni[j]
                b += 1
                s += 1
            end
            ansj=(k-c)%s
            println(h[ansj+1])
            exit()
        end
    end
end
f()


#!/usr/bin/env -S JULIA_VERSION=1.4.0 julia
# contest: abc167, problem: abc167_d, alphabet: D

function solve(N::Int, K::Int, A::Vector{Int})
  hist = zeros(Int, N+10)
  hist[0+1] = 1
  dic = Dict(1=>0)
  k = 1
  p = A[1]
  while true
    k0 = get!(dic, p, k)
    if k0 < k
      p0 = hist[k0+1]
      m = k - k0
      kK = k0 + (K - k0) % m
      println(hist[kK + 1])
      break
    end
    if p == A[p]
      println(p)
      break
    end
    hist[k+1] = p
    p = A[p]
    k += 1
  end
end

function main()
  tokens = Channel{String}(32)
  Task() do
    for line in eachline(@static VERSION < v"0.6" ? STDIN : stdin)
      startswith(line, '\0') && break
      for token in split(chomp(line))
        put!(tokens, token)
      end
    end
    close(tokens)
  end |> schedule
  N = parse(Int, take!(tokens))
  K = parse(Int, take!(tokens))
  A = similar(Vector{Int}, N)
  for i in 1:N
    A[i] = parse(Int, take!(tokens))
  end
  solve(N, K, A)
end

if isempty(get(ENV, "ATCODER_LOCAL", ""))
  isempty(ARGS) && main()
else
  @eval begin
    const samples = [("4 5\n3 2 4 1", "4"), ("6 727202214173249351\n6 5 2 5 3 2", "2")]
    function test(sampleids...)
      isempty(sampleids) && return test(collect(1:length(samples))...)
      ostdin, ostdout = @static VERSION < v"0.6" ? (STDIN, STDOUT) : (stdin, stdout)
      rd, wr = first(redirect_stdout()), last(redirect_stdin())
      try
        map(sampleids) do sampleid
          input, output = samples[sampleid]
          print(ostdout, "Testing sample #$(sampleid)..."); flush(ostdout)
          println(wr, input)
          println(wr, "\0")
          t = @elapsed main()
          println()
          result = strip(String(readavailable(rd)))
          if result == output
            println(ostdout, "OK ($t sec).")
            true
          else
            println(ostdout, "ERROR ($t sec)")
            println(ostdout, "== expected ==\n$output")
            println(ostdout, "== actual ==\n$result\n")
            false
          end
        end |> all
      finally
        redirect_stdin(ostdin)
        redirect_stdout(ostdout)
      end
    end
    submit() = test() && (prog = @__FILE__; run(`atcoder-submit $prog`))
    !isinteractive() && submit()
  end
end


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

function main()
N,K = parseMap(split(readline()))
A = parseMap(split(readline()))

start_city = 1
still_see = Set{Int}()
if N < K
for i in 1:K
    visit_city = A[start_city]
    if visit_city in still_see
        loop_begin_city = visit_city
        loop_fin_city = start_city
        loop_start_city = loop_fin_city
        for j in 1:10^6
            loop_visit_city = A[loop_start_city]
            if loop_visit_city == loop_fin_city
                length_loop = j
                ans_idx = loop_begin_city + ((K - i) % length_loop)
                return ans_idx
            end
            loop_start_city = loop_visit_city
        end
    else
        push!(still_see,visit_city)
    end
    start_city = visit_city
end
else
    for i in 1:K
        visit_city = A[start_city]
        start_city = visit_city
        if i == K
            return visit_city
        end
    end
end
end

println(main())


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, k = readint()
  a = Vector{int}(undef,n)
  a .= readint()

  kita = falses(n)

  K = zeros(int,n+1)
  s = 1
  t = 1

  B = zeros(int,n)
  B[1] = 1
  K[1] = 1

  c = 0
  for i in 1:n
    t = a[s]

    K[i+1] = t
    kita[s] = true
    #B[t] = i+1
    if kita[t]
      x = B[t]
      y = i

      #println(x, y, K )
      if k <= y
        println(K[k+1])
      else
        println(K[x+ (k-i)%(y-x+1)])
      end
      return
    else
      B[t] = i+1
    end
    s = t
  end
end

main()


#!/usr/bin/env -S JULIA_VERSION=1.4.0 julia
# contest: abc167, problem: abc167_d, alphabet: D

function solve(N::Int, K::Int, A::Vector{Int})
  hist = zeros(Int, N+10)
  hist[0+1] = 1
  dic = Dict(1=>0)
  k = 1
  p = A[1]
  while true
    k0 = get!(dic, p, k)
    if k0 < k
      p0 = hist[k0+1]
      m = k - k0
      kK = k0 + (K - k0) % m
      println(hist[kK + 1])
      break
    end
    hist[k+1] = p
    p = A[p]
    k += 1
  end
end

function main()
  tokens = Channel{String}(32)
  Task() do
    for line in eachline(@static VERSION < v"0.6" ? STDIN : stdin)
      startswith(line, '\0') && break
      for token in split(chomp(line))
        put!(tokens, token)
      end
    end
    close(tokens)
  end |> schedule
  N = parse(Int, take!(tokens))
  K = parse(Int, take!(tokens))
  A = similar(Vector{Int}, N)
  for i in 1:N
    A[i] = parse(Int, take!(tokens))
  end
  solve(N, K, A)
end

if isempty(get(ENV, "ATCODER_LOCAL", ""))
  isempty(ARGS) && main()
else
  @eval begin
    const samples = [("4 5\n3 2 4 1", "4"), ("6 727202214173249351\n6 5 2 5 3 2", "2")]
    function test(sampleids...)
      isempty(sampleids) && return test(collect(1:length(samples))...)
      ostdin, ostdout = @static VERSION < v"0.6" ? (STDIN, STDOUT) : (stdin, stdout)
      rd, wr = first(redirect_stdout()), last(redirect_stdin())
      try
        map(sampleids) do sampleid
          input, output = samples[sampleid]
          print(ostdout, "Testing sample #$(sampleid)..."); flush(ostdout)
          println(wr, input)
          println(wr, "\0")
          t = @elapsed main()
          println()
          result = strip(String(readavailable(rd)))
          if result == output
            println(ostdout, "OK ($t sec).")
            true
          else
            println(ostdout, "ERROR ($t sec)")
            println(ostdout, "== expected ==\n$output")
            println(ostdout, "== actual ==\n$result\n")
            false
          end
        end |> all
      finally
        redirect_stdin(ostdin)
        redirect_stdout(ostdout)
      end
    end
    submit() = test() && (prog = @__FILE__; run(`atcoder-submit $prog`))
    !isinteractive() && submit()
  end
end


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, k = readint()
  a = Vector{int}(undef,n)
  a .= readint()

  kita = falses(n)

  K = zeros(int,n+1)
  s = 1
  t = 1

  K[1] = 1
  B = zeros(int,n)
  B[1] = 1

  c = 0
  for i in 1:n
    t = a[i]
    K[i] = s
    B[s] = i
    K[i+1] = t
    B[s] = i+1

    kita[s] = true
    if kita[t]
      x = B[t]
      y = i

      if k <= y
        println(K[k+1])
      else
        println(K[x+ (k-i)%(y-x+1)])
      end
      return
    end
    s = t
  end
end

main()


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

function main()
	n,k = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	chk = zeros(Int,n)
	now = 1
	ct = 0
	cs = 0
	cl = 0
	while chk[now]==0
		chk[now]=1
		now=a[now]
		ct+=1
	end
	cs = now
	now = a[now]
	cl += 1
	while now!=cs
		now=a[now]
		cl+=1
	end
	if k<ct
		now = 1
		for i in 1:k
			now=a[now]
		end
		println(now)
	else
		k=(k-ct)%cl
		now = cs
		for i in 1:k
			now=a[now]
		end
		println(now)
	end
end

main()


n,k = [parse(Int, x) for x in readline() |> split]
a = [parse(Int, x) for x in readline() |> split]
m = zeros(Int,n,n)
for i in 1:n
  m[a[i],i] = 1
end

ans = m ^ k

for i in 1:n
  if ans[i,1] == 1
    println(i)
    exit()
  end
end


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

    takahashi = (c+b-1) ÷ b
    aoki = (a+d-1) ÷ d
    takahashi <= aoki ? "Yes" : "No"
end

println(solve())

Score : 200 points

Problem Statement

Takahashi and Aoki will have a battle using their monsters.

The health and strength of Takahashi's monster are A and B, respectively, and those of Aoki's monster are C and D, respectively.

The two monsters will take turns attacking, in the order Takahashi's, Aoki's, Takahashi's, Aoki's, ...
Here, an attack decreases the opponent's health by the value equal to the attacker's strength.
The monsters keep attacking until the health of one monster becomes 0 or below. The person with the monster whose health becomes 0 or below loses, and the other person wins.

If Takahashi will win, print Yes; if he will lose, print No.

Constraints

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

If Takahashi will win, print Yes; if he will lose, print No.

Sample Input 1

10 9 10 10

Sample Output 1

No

First, Takahashi's monster attacks Aoki's monster, whose health is now 10-9=1.

Next, Aoki's monster attacks Takahashi's monster, whose health is now 10-10=0.

Takahashi's monster is the first to have 0 or less health, so Takahashi loses.

Sample Input 2

46 4 40 5

Sample Output 2

Yes


input() = parse(Int, readline())
inputs() = map(x -> parse(Int, x), readline() |> split)
let
  ans = 0
  A, B, C, D = inputs()
  while true
    C -= B
    C <= 0 && (println("Yes"); break)
    A -= D
    A <= 0 && (println("No"); break)
  end
end


function main()
  a,b,c,d = parse.(Int,split(readline()))
  t_times = ceil(c / b)
  a_times = ceil(a / d)
  println(t_times <= a_times ? "Yes" : "No")
end
main()


a,b,c,d=parse.(Int,split(readline()))
print(cld(c,b)>cld(a,d) ? "No" : "Yes")


a,b,c,d=map(x->parse(Int,x),split(readline()))
println(div(c-1,b)>div(a-1,d) ? "No" : "Yes")


a, b, c, d = parse.(Int, split(readline()))
if (a+d-1 / d) >= div(c+b-1 / b)
    "Yes"
else
    "No"
end


a, b, c, d = parse.(Int, split(readline()))
if div(a+d-1, d) >= div(c+b-1, b)
    "Yes"
else
    "No"
end


parseint(xs) = map(x -> parse(Int,x), xs)

function main()
    a,b,c,d = readline() |> split |> parseint
    if cld(c, b) <= cld(a, d)
        println("Yes")
    else
        println("No")
    end
end

main()


function main()

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

  while true
    C -= B
    if C <= 0
      break
    end
    A -= D
    if A <= 0
      break
    end
  end

  if C <= 0
    println("Yes")
  else
    println("No")
  end

end

main()


function main()

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

  while A > 0 || B > 0
    C -= B
    A -= D
  end

  if C <= 0
    println("Yes")
  else
    println("No")
  end


a,b,c,d=parse.(Int,split(readline()))
print(1+cld(a,d)>cld(c,b) ? "Yes" : "No")


parseInt(x) = parse(Int, x)
parseBigInt(x) = parse(BigInt, x)
parseFloat(x) = parse(Float64, x)
parseBigFloat(x) = parse(BigFloat, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    A,B,C,D = parseMap(split(readline()))
    while(true)
        C-=B
        if C<=0
            println("Yes")
            break
        end
        A-=D
        if A<=0
            println("No")
            break
        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()
    a,b,c,d=parseMap(split(readline()))
    for i in 1:1000000
        c-=b
        if c<=0
            println("Yes")
            break
        end
        a-=d
        if a<=0
            println("No")
            break
        end
    end
end
main()


A,B,C,D=readline()|>split|>x->parse.(Int,x)
println(ceil(C/B)<= ceil(A/D) ? "Yes" : "No")


function parseInt(s::AbstractString)
    parse(Int, s)
end

function main()
    a,b,c,d = parseInt.(split(readline()))
    while true
        c -= b
        a -= d
        if c <= 0
            println("Yes")
            exit()
        end
        if a <= 0
            println("No")
            exit()
        end
    end
end

main()


function MultipleOf2019(str)
    S = str
    n = length(S)
    T = zeros(Int, n+1)
    N = zeros(Int, 2019)
    N[1] += 1
    m = 1
    for i in n:-1:1
        T[i] = mod(T[i+1] + parse(Int, S[i])*m, 2019)
        m = mod(10m, 2019)
        N[T[i]+1] += 1
    end
#     println(T)
    println(sum(div(m*(m-1), 2) for m in N))
end
MultipleOf2019(readline())

Score : 400 points

Problem Statement

Given is a string S consisting of digits from 1 through 9.

Find the number of pairs of integers (i,j) (1 ≤ i ≤ j ≤ |S|) that satisfy the following condition:

Condition: In base ten, the i-th through j-th characters of S form an integer that is a multiple of 2019.

Constraints

1 ≤ |S| ≤ 200000

S is a string consisting of digits from 1 through 9.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of pairs of integers (i,j) (1 ≤ i ≤ j ≤ |S|) that satisfy the condition.

Sample Input 1

1817181712114

Sample Output 1

3

Three pairs - (1,5), (5,9), and (9,13) - satisfy the condition.

Sample Input 2

14282668646

Sample Output 2

2

Sample Input 3

2119

Sample Output 3

0

No pairs satisfy the condition.


# Jasmine_ss's code modified by centipede_human

function MultipleOf2019(str)
    S = str
    n = length(S)
    T = zeros(Int, n+1)
    N = zeros(Int, 2019)
    N[1] += 1
    m = 1
    for i in n:-1:1
        T[i] = mod(T[i+1] + parse(Int, S[i])*m, 2019)
        m*=10; m%=2019
        N[T[i]+1] += 1
    end
#     println(T)
    println(sum(div(m*(m-1), 2) for m in N))
end
MultipleOf2019(readline())


function MultipleOf2019(str)
    S = str
    n = length(S)
    mod = 2019
    T = zeros(Int, n+1)
    N = zeros(Int, 2019)
    N[1] += 1
    for i in n:-1:1
        T[i] = T[i+1] + parse(Int, S[i])*10^(n-i)
        T[i] %= mod
        N[T[i]+1] += 1
    end
#     println(T)
    println(sum(div(m*(m-1), 2) for m in N))
end
MultipleOf2019(readline())


function MultipleOf2019(str)
    S = str
    n = length(S)
    T = zeros(Int, n+1)
    N = zeros(Int, 2019)
    N[1] += 1
    for i in n:-1:1
        T[i] = mod(T[i+1] + parse(Int, S[i])*10^(n-i), 2019)
        N[T[i]+1] += 1
    end
#     println(T)
    println(sum(div(m*(m-1), 2) for m in N))
end
MultipleOf2019(readline())


function MultipleOf2019()
    data = readline()
    counter = 0
    for i in 1:length(data)-3
        for j in i+3:length(data)
            n = parse(Int, data[i:j])
            n%2019==0 && (counter+=1)
        end
    end
    println(counter)
end
MultipleOf2019()


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

#int(x) = parse(int,x)
parseint(x) = parse(int, x)
function readint()
  moji = readline(stdin)
  moji = split(moji)
  return map(parseint,moji)

end

function main()
  s = readline()

  N = length(s)
  D = Dict{int,int}()
  D[0] = 1

  mod = 0
  mod10 = 1
  for i in N:-1:1
    a = parse(int,s[i]) #int(s[i])

    mod = (mod + mod10 * a) %2019

    if get(D,mod,0)==0
      D[mod]=1
    else
      D[mod] +=1
    end

    mod10 = (mod10 * 10) %2019

  end

  sum = 0
  for (k,v) in D
    sum += v*(v-1)÷2
  end
  println(sum)

end

main()


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

function main()
	s = parseMap(split(chomp(readline()),""))
	n = length(s)
	dp = zeros(Int,n)
	dp[n]=s[n]
	dx = zeros(Int,n)
	dx[1]=10
	for i in 2:n
		dx[i]=(dx[i-1]*10)%2019
	end
	for i in n-1:-1:1
		dp[i]=(dp[i+1]+(s[i]*dx[n-i])%2019)%2019
	end
	d = Dict{Int,Int}()
	d[0]=1
	for i in 1:n
		if haskey(d,dp[i])
			d[dp[i]]+=1
		else
			d[dp[i]]=1
		end
	end
	ct = 0
	for i in keys(d)
		ct+=(d[i]*(d[i]-1))>>1
	end
	println(ct)
end

main()


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

function main()
	s = parseMap(split(chomp(readline()),""))
	d = Dict{Int,Int}()
	ct=0
	for i in 1:length(s)
		e = Dict{Int,Int}()
		for j in keys(d)
			v=j*10+s[i]
			if v%2019==0
				ct+=d[j]
			end
			if haskey(e,v)
				e[v]+=d[j]
			else
				e[v]=d[j]
			end
		end
		if haskey(e,s[i])
			e[s[i]]+=1
		else
			e[s[i]]=1
		end
		d = copy(e)
	end
	println(ct)
end

main()


const teststr =
"""
1817181712114
"""

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

quit() = exit(0)
in_1(ch) = parse(Int, take!(ch))
in_a(ch) = parse.(Int, split(take!(ch)))

function main()
  ch = lines()
  S = take!(ch)

  mul = mod(10^15, 2019)
  n = 0
  for i in 1:length(S)
    m = 0
    for j in i:length(S)
      m = m * 10 + Int(S[j]) - Int('0')
      if m > 2019
        m = mod(m, 2019)
      end
      m == 0 && (n += 1)
    end
  end
  println(n)
end

isempty(ARGS) && main()


const teststr =
"""
1817181712114
"""

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

quit() = exit(0)
in_1(ch) = parse(Int, take!(ch))
in_a(ch) = parse.(Int, split(take!(ch)))

function main()
  ch = lines()
  S = take!(ch)

  n = 0
  for i in 1:length(S)
    m = 0
    for j in i:length(S)
      m = m * 10 + parse(Int, S[j])
      m = mod(m, 2019)
      m == 0 && (n += 1)
    end
  end
  println(n)
end

isempty(ARGS) && main()


const teststr =
"""
2119
"""

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

quit() = exit(0)
in_1(ch) = parse(Int, take!(ch))
in_a(ch) = parse.(Int, split(take!(ch)))

function main()
  ch = lines()
  S = take!(ch)
  n = 0
  for i in 1:length(S)
    for j in i:length(S)
      mod(parse(Int, S[i:j]), 2019) == 0 && (n += 1)
    end
  end
  println(n)
end

main()


S = readline()

cnt = 0
for i in 1:length(S)-2
    for j in i+3:length(S)
        if parse(Int,S[i:j]) % 2019 == 0
            cnt += 1
        else
            continue
        end
    end
end

print(cnt)


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()
    S = pread(String)
    SI = map(x -> x - '0', collect(S))
    d = zeros(Int, length(S))
    tmpc = 0
    tmpcb = 0
    tmpc = 1000*SI[1] + 100*SI[2] + 10*SI[3] + SI[4]
    for i in 1:length(S)-3
        tmpb = tmpc
        for j in 4:7
            if i+j > length(S)
                break
            end
            tmpc *= 10
            tmpc += SI[i+j]
            if tmpc % 2019 == 0
                d[i+j] += 1 + (i == 1 ? 0 : d[i-1])
            end
        end
        if i+4 > length(S)
            break
        end
        tmpc = tmpb % 1000 * 10 + SI[i+4]
    end
    println(sum(d))
end

main()


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()
    S = pread(String)
    SI = map(x -> x - '0', collect(S))
    d = zeros(Int, length(S))
    tmpc = 1000*SI[1] + 100*SI[2] + 10*SI[3] + SI[4]
    for i in 1:length(S)-3
        for j in 4:7
            if i+j > length(S)
                break
            end
            tmpc *= 10
            tmpc += SI[i+j]
            if tmpc % 2019 == 0
                d[i+j] += 1 + (i == 1 ? 0 : d[i-1])
            end
            println("tmpc: ",tmpc)
        end
        if i+4 > length(S)
            break
        end
        tmpc = (tmpc ÷ 10000) ÷ 1000 * 10 + SI[i+4]
    end
    println(sum(d))
end

main()


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()
    S = pread(String)
    SI = map(x -> x - '0', collect(S))
    d = zeros(Int, length(S))
    tmpc = 0
    for i in 1:length(S)-3
        tmpc = 1000*SI[i] + 100*SI[i+1] + 10*SI[i+2] + SI[i+3]
        for j in 4:7
            if i+j > length(S)
                break
            end
            tmpc *= 10
            tmpc += SI[i+j]
            if tmpc % 2019 == 0
                d[i+j] += 1 + (i == 1 ? 0 : d[i-1])
            end
        end
    end
    println(sum(d))
end

main()


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()
    S = pread(String)
    d = zeros(Int, length(S))
    for i in 1:length(S)
        tmpc = 0
        for j in 4:7
            if i+j > length(S)
                break
            end
            if parse(Int, S[i:i+j]) % 2019 == 0
                d[i+j] += 1 + (i == 1 ? 0 : d[i-1])
            else
            end
        end
    end
    println(sum(d))
end

main()


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()
    S = pread(String)
    d = zeros(Int, length(S))
    for i in 1:length(S)
        tmpc = 0
        for j in 3:7
            if i+j > length(S)
                break
            end
            if parse(Int, S[i:i+j]) % 2019 == 0
                d[i+j] += 1 + (i == 1 ? 0 : d[i-1])
            else
            end
        end
    end
    println(sum(d))
end

main()


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()
    S = pread(String)
    d = zeros(Int, length(S))
    for i in 1:length(S)
        tmpc = 0
        for j in 3:7
            if i+j > length(S)
                break
            end
            if parse(Int, S[i:i+j]) % 2019 == 0
                d[i+j] = 1 + (i == 1 ? 0 : d[i-1])
            end
        end
    end
    println(sum(d))
end

main()


function main()
	S=chomp(readline())
	cnt=zeros(Int,2019)
	a=0
	p=1
	cnt[1]=1
	ans=0
	for i=length(S):-1:1
		a=(a+p*(Int(S[i])-48))%2019
		p=p*10%2019
		ans+=cnt[a+1]
		cnt[a+1]+=1
	end
	println(ans)
end
main()


using DataStructures
MOD = 2019

a = readline() |> s -> split(s,"") |> xs -> parse.(Int,xs) |> reverse
n = length(a)

dp = zeros(Int, n+1)
for i in 1:n
  dp[i+1] = (powermod(10, i-1, MOD) * a[i] + dp[i]) % MOD
end

[x * (x-1) ÷ 2 for (_,x) in counter(dp)] |> sum |> println


using DataStructures
MOD = 2019

function main()
  a = readline() |> s -> split(s,"") |> xs -> parse.(Int,xs) |> reverse
  n = length(a)
  dp = zeros(Int, n+1)

  for i in 1:n
    dp[i+1] = (powermod(10, i-1, MOD) * a[i] + dp[i]) % MOD
  end

  cnt = counter(Int)
  for x in dp
    inc!(cnt, x)
  end
  ans = [x * (x-1) ÷ 2 for (k,x) in cnt] |> sum

  println(ans)
end

main()


function main()

  S = chomp(readline())
  N = length(S)

  cnt = 0

  for i in 1:N
    for j in 1:N-i

      check = parse(BigInt, join(S[j:j+i]))
      if check % 2019 == 0
        cnt += 1
      end

    end
  end

  println(cnt)

end

main()


function main()

  S = chomp(readline())
  N = length(S)

  cnt = 0

  for i in 1:N
    for j in 1:N-i

      check = parse(Int, join(S[j:j+i]))
      if check % 2019 == 0
        cnt += 1
      end

    end
  end

  println(cnt)

end

main()


function main()
    s=readline()
    l=length(s)
    x=0
    for i=1:l-3,j=i+3:l

        y=parse(Int,s[i:j])
        x+=y%2019==0 ? 1 : 0
    end

    print(x)
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 modpow(x, n, p)
    ans = 1
    while(n!=0)
        if n&1 == 1
            ans = ans*x%p
        end
        x = x*x%p
        n = n>>1
    end
    return ans
end
function main()
    s = readline()
    s = reverse(s)
    p=2019
    d = Dict()

    i10 = 0
    now = 0
    for i in 1:length(s)
        t = parse(Int, s[i])
        now += (modpow(10, i10, p) * t)%p
        now%=p
        i10+=1
        if haskey(d, now)
            d[now]+=1
        else
            d[now]=1
        end
    end

    res = 0
    i10 = 0
    now = 0
    for i in 1:length(s)
        if haskey(d, now)
            res += d[now]
        end
        t = parse(Int, s[i])
        now += (modpow(10, i10, p) * t)%p
        now %= p

        d[now]-=1
        i10+=1
    end
    println(res)

end
main()


S = readline()

cnt = 0
for i in 1:length(S)-2
    for j in i+3:length(S)
        if parse(Int,S[i:j]) % 2019 == 0
            cnt += 1
        else
            cnt += 0
        end
    end
end

print(cnt)


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

function main()
	s = parseMap(split(chomp(readline()), ""))
	n = length(s)
	dp = zeros(Int,n,4)
	for i in 4:n
		a = s[i-3]*1000+s[i-2]*100+s[i-1]*10+s[i]
		if a%2019==0
			if i>4
				dp[i,1] = sum(dp[i-4,:])+1
			else
				dp[i,1] = 1
			end
		end
		if i>4
			a += s[i-4]*10000
			if a%2019==0
				if i>5
					dp[i,2] = sum(dp[i-5,:])+1
				else
					dp[i,2] = 1
				end
			end
			if i>5
				a += s[i-5]*100000
				if a%2019==0
					if i>6
						dp[i,3] = sum(dp[i-6,:])+1
					else
						dp[i,3] = 1
					end
				end
				if i>6
					a += s[i-6]*1000000
					if a%2019==0
						if i>7
							dp[i,4] = sum(dp[i-7,:])+1
						else
							dp[i,4] = 1
						end
					end
				end
			end
		end
	end
	println(sum(dp))
end

main()


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

parseint(x) = parse(int, x)
function readint()
  moji = readline(stdin)
  moji = split(moji)
  return map(parseint,moji)
end

const NMax = 50
const MMax = 100

struct Edge
  y::int
  cost ::int
  time ::int
end

mutable struct Graph{T}
  nodes::Vector{Vector{T}}
  degree::Vector{int}
  n::int
  m::int
end

function Graph(N,T)
  Nodes = Vector{Vector{T}}(undef,N)
  for i in 1:N
    Nodes[i] = Vector{T}(undef,0)
  end
  degree = zeros(int,N)
  n = N
  m = 0
  return Graph{T}(Nodes, degree, n,m)

end

struct State
  iti::int
  kane::int
  time::int
end

isless(a::State,b::State) = a.time < b.time

mutable struct PriorityQueue{T}
  tree::Vector{T}
  Nmax::int
  n::int
  priority::Function
end

function PriorityQueue(isless,T)
  tree = Vector{T}(undef,0)
  return PriorityQueue{T}(tree, 0, 0, isless)
end

function push(P::PriorityQueue{T}, a::T) where T
  if P.Nmax == P.n
    P.Nmax += 1
    P.n += 1
    append!(P.tree,[a])
  else
    P.n+=1
    P.tree[P.n] = a
  end

  p = P.n
  while p > 1
    up = p÷2
    if P.priority(P.tree[p], P.tree[up])
      P.tree[p], P.tree[up] = P.tree[up],P.tree[p]
    end
    p = up
  end

end

function pop(P::PriorityQueue{T}) where T
  preturn = P.tree[1]
  P.tree[1] = P.tree[P.n]
  P.n -= 1
  nmax = P.n
  p = 1

  while 2p <= nmax

    # argmin [p, 2p, 2p+1]
    m = p
    for down in 2p:2p+1
      down > nmax && break
      P.priority(P.tree[down], P.tree[m]) && (m = down)
    end

    m == p && break

    P.tree[p], P.tree[m] = P.tree[m], P.tree[p]
    p = m

  end
  return preturn

end

#import
is_empty(P::PriorityQueue{T}) where T = P.n == 0

function main()
  N, M, S = readint()

  G =Graph(N,Edge)

  Amax = 0
  for i in 1:M
    U,V,A,B = readint()
    append!(G.nodes[U],[Edge(V,A,B)])
    append!(G.nodes[V],[Edge(U,A,B)])
    G.degree[U]+=1
    G.degree[V]+=1
    Amax < A && (Amax = A)
  end

  for i in 1:N
    C,D = readint()
    G.degree[i] += 1
    append!(G.nodes[i], [Edge(i,-C,D)])
  end

  d = Array{int,2}(undef,N,N*Amax+1)
  d .= typemax(int)
  if S > Amax * N
    S = Amax * N
  end
  d[1, S+1] = 0

  checked = Vector{Bool}(undef,N)
  checked .= false

  Q = PriorityQueue(isless, State)
  push(Q, State(1,S,0))

  jS = S
  jp = 1
  checked[1]=true
  dd = Vector{int}(undef, N)

  while ! is_empty(Q)
    q = pop(Q)
    p, kane, time = q.iti, q.kane, q.time

    d[p,kane+1] < time && continue

    if !checked[p] # = false
      checked[p] = true
      dd[p] = time
    end

    for E in G.nodes[p]
      kane - E.cost < 0 && continue

      newkane = min(Amax*N, kane-E.cost)
      newtime = E.time + time

      if d[E.y, newkane+1] > newtime
        d[E.y, newkane+1] = newtime
        push(Q, State(E.y, newkane, newtime))
      end
    end
    #println(q, p)
  end
  for i in 2:N
    println(dd[i])
  end
  #println(G)
end

main()

Score : 500 points

Problem Statement

There are N cities numbered 1 to N, connected by M railroads.

You are now at City 1, with 10^{100} gold coins and S silver coins in your pocket.

The i-th railroad connects City U_i and City V_i bidirectionally, and a one-way trip costs A_i silver coins and takes B_i minutes.
You cannot use gold coins to pay the fare.

There is an exchange counter in each city. At the exchange counter in City i, you can get C_i silver coins for 1 gold coin.
The transaction takes D_i minutes for each gold coin you give.
You can exchange any number of gold coins at each exchange counter.

For each t=2, ..., N, find the minimum time needed to travel from City 1 to City t. You can ignore the time spent waiting for trains.

Constraints

2 \leq N \leq 50

N-1 \leq M \leq 100

0 \leq S \leq 10^9

1 \leq A_i \leq 50

1 \leq B_i,C_i,D_i \leq 10^9

1 \leq U_i < V_i \leq N

There is no pair i, j(i \neq j) such that (U_i,V_i)=(U_j,V_j).

Each city t=2,...,N can be reached from City 1 with some number of railroads.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M S
U_1 V_1 A_1 B_1
:
U_M V_M A_M B_M
C_1 D_1
:
C_N D_N

Output

For each t=2, ..., N in this order, print a line containing the minimum time needed to travel from City 1 to City t.

Sample Input 1

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

Sample Output 1

2
14

The railway network in this input is shown in the figure below.

In this figure, each city is labeled as follows:

The first line: the ID number i of the city (i for City i)

The second line: C_i / D_i

Similarly, each railroad is labeled as follows:

The first line: the ID number i of the railroad (i for the i-th railroad in input)

The second line: A_i / B_i

You can travel from City 1 to City 2 in 2 minutes, as follows:

Use the 1-st railroad to move from City 1 to City 2 in 2 minutes.

You can travel from City 1 to City 3 in 14 minutes, as follows:

Use the 1-st railroad to move from City 1 to City 2 in 2 minutes.

At the exchange counter in City 2, exchange 3 gold coins for 3 silver coins in 6 minutes.

Use the 1-st railroad to move from City 2 to City 1 in 2 minutes.

Use the 2-nd railroad to move from City 1 to City 3 in 4 minutes.

Sample Input 2

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

Sample Output 2

5
5
7

The railway network in this input is shown in the figure below:

You can travel from City 1 to City 4 in 7 minutes, as follows:

At the exchange counter in City 1, exchange 2 gold coins for 6 silver coins in 2 minutes.

Use the 2-nd railroad to move from City 1 to City 3 in 4 minutes.

Use the 4-th railroad to move from City 3 to City 4 in 1 minutes.

Sample Input 3

6 5 1
1 2 1 1
1 3 2 1
2 4 5 1
3 5 11 1
1 6 50 1
1 10000
1 3000
1 700
1 100
1 1
100 1

Sample Output 3

1
9003
14606
16510
16576

The railway network in this input is shown in the figure below:

You can travel from City 1 to City 6 in 16576 minutes, as follows:

Use the 1-st railroad to move from City 1 to City 2 in 1 minute.

At the exchange counter in City 2, exchange 3 gold coins for 3 silver coins in 9000 minutes.

Use the 1-st railroad to move from City 2 to City 1 in 1 minute.

Use the 2-nd railroad to move from City 1 to City 3 in 1 minute.

At the exchange counter in City 3, exchange 8 gold coins for 8 silver coins in 5600 minutes.

Use the 2-nd railroad to move from City 3 to City 1 in 1 minute.

Use the 1-st railroad to move from City 1 to City 2 in 1 minute.

Use the 3-rd railroad to move from City 2 to City 4 in 1 minute.

At the exchange counter in City 4, exchange 19 gold coins for 19 silver coins in 1900 minutes.

Use the 3-rd railroad to move from City 4 to City 2 in 1 minute.

Use the 1-st railroad to move from City 2 to City 1 in 1 minute.

Use the 2-nd railroad to move from City 1 to City 3 in 1 minute.

Use the 4-th railroad to move from City 3 to City 5 in 1 minute.

At the exchange counter in City 5, exchange 63 gold coins for 63 silver coins in 63 minutes.

Use the 4-th railroad to move from City 5 to City 3 in 1 minute.

Use the 2-nd railroad to move from City 3 to City 1 in 1 minute.

Use the 5-th railroad to move from City 1 to City 6 in 1 minute.

Sample Input 4

4 6 1000000000
1 2 50 1
1 3 50 5
1 4 50 7
2 3 50 2
2 4 50 4
3 4 50 3
10 2
4 4
5 5
7 7

Sample Output 4

1
3
5

The railway network in this input is shown in the figure below:

Sample Input 5

2 1 0
1 2 1 1
1 1000000000
1 1

Sample Output 5

1000000001

The railway network in this input is shown in the figure below:

You can travel from City 1 to City 2 in 1000000001 minutes, as follows:

At the exchange counter in City 1, exchange 1 gold coin for 1 silver coin in 1000000000 minutes.

Use the 1-st railroad to move from City 1 to City 2 in 1 minute.


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

parseint(x) = parse(int, x)
function readint()
  moji = readline(stdin)
  moji = split(moji)
  return map(parseint,moji)
end

const NMax = 50
const MMax = 100

struct Edge
  y::int
  cost ::int
  time ::int
end

mutable struct Graph{T}
  nodes::Vector{Vector{T}}
  degree::Vector{int}
  n::int
  m::int
end

function Graph(N,T)
  Nodes = Vector{Vector{T}}(undef,N)
  for i in 1:N
    Nodes[i] = Vector{T}(undef,0)
  end
  degree = zeros(int,N)
  n = N
  m = 0
  return Graph{T}(Nodes, degree, n,m)

end

struct State
  iti::int
  kane::int
  time::int
end

isless(a::State,b::State) = a.time < b.time

mutable struct PriorityQueue{T}
  tree::Vector{T}
  Nmax::int
  n::int
  priority::Function
end

function PriorityQueue(isless,T)
  tree = Vector{T}(undef,0)
  return PriorityQueue{T}(tree, 0, 0, isless)
end

function push(P::PriorityQueue{T}, a::T) where T
  if P.Nmax == P.n
    P.Nmax += 1
    P.n += 1
    append!(P.tree,[a])
  else
    P.n+=1
    P.tree[P.n] = a
  end

  p = P.n
  while p > 1
    up = p÷2
    if P.priority(P.tree[p], P.tree[up])
      P.tree[p], P.tree[up] = P.tree[up],P.tree[p]
    end
    p = up
  end

end

function pop(P::PriorityQueue{T}) where T
  preturn = P.tree[1]
  P.tree[1] = P.tree[P.n]
  P.n -= 1
  nmax = P.n
  p = 1

  while 2p <= nmax

    # argmin [p, 2p, 2p+1]
    m = p
    for down in 2p:2p+1
      down > nmax && break
      P.priority(P.tree[down], P.tree[m]) && (m = down)
    end

    m == p && break

    P.tree[p], P.tree[m] = P.tree[m], P.tree[p]
    p = m

  end
  return preturn

end

#import
is_empty(P::PriorityQueue{T}) where T = P.n == 0

function main()
  N, M, S = readint()

  G =Graph(N,Edge)

  Amax = 0
  for i in 1:M
    U,V,A,B = readint()
    append!(G.nodes[U],[Edge(V,A,B)])
    append!(G.nodes[V],[Edge(U,A,B)])
    G.degree[U]+=1
    G.degree[V]+=1
    Amax < A && (Amax = A)
  end

  for i in 1:N
    C,D = readint()
    G.degree[i] += 1
    append!(G.nodes[i], [Edge(i,-C,D)])
  end

  d = Array{int,2}(undef,N,N*Amax+1)
  d .= typemax(int)
  if S > Amax * N
    S = Amax * N
  end
  d[1, S+1] = 0

  checked = Vector{Bool}(undef,N)
  checked .= false

  Q = PriorityQueue(isless, State)
  push(Q, State(1,S,0))

  jS = S
  jp = 1
  checked[1]=true
  dd = Vector{int}(undef, N)

  while ! is_empty(Q)
    q = pop(Q)
    p, kane, time = q.iti, q.kane, q.time

    d[p,kane+1] < time && continue

    if !checked[p] # = false
      checked[p] = true
      dd[p] = time
    end

    #flag = true
    #for i in 1:N
    #  if !checked[i]
    #    flag = false
    #    break
    #  end
    #end
    #flag && break

    for E in G.nodes[p]
      kane - E.cost < 0 && continue

      newkane = min(Amax*N, kane-E.cost)
      newtime = E.time + time

      if d[E.y, kane+1] > newtime
        d[E.y, kane+1] = newtime
        push(Q, State(E.y, newkane, newtime))
      end
    end
    #println(q, p)
  end
  for i in 2:N
    println(dd[i])
  end
  #println(G)
end

main()


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

parseint(x) = parse(int, x)
function readint()
  moji = readline(stdin)
  moji = split(moji)
  return map(parseint,moji)
end

const NMax = 50
const MMax = 100

struct Edge
  y::int
  cost ::int
  time ::int
end

mutable struct Graph{T}
  nodes::Vector{Vector{T}}
  degree::Vector{int}
  n::int
  m::int
end

function Graph(N,T)
  Nodes = Vector{Vector{T}}(undef,N)
  for i in 1:N
    Nodes[i] = Vector{T}(undef,0)
  end
  degree = zeros(int,N)
  n = N
  m = 0
  return Graph{T}(Nodes, degree, n,m)

end

struct State
  iti::int
  kane::int
  time::int
end

isless(a::State,b::State) = a.time < b.time

mutable struct PriorityQueue{T}
  tree::Vector{T}
  Nmax::int
  n::int
  priority::Function
end

function PriorityQueue(isless,T)
  tree = Vector{T}(undef,0)
  return PriorityQueue{T}(tree, 0, 0, isless)
end

function push(P::PriorityQueue{T}, a::T) where T
  if P.Nmax == P.n
    P.Nmax += 1
    P.n += 1
    append!(P.tree,[a])
  else
    P.n+=1
    P.tree[P.n] = a
  end

  p = P.n
  while p > 1
    up = p÷2
    if P.priority(P.tree[p], P.tree[up])
      P.tree[p], P.tree[up] = P.tree[up],P.tree[p]
    end
    p = up
  end

end

function pop(P::PriorityQueue{T}) where T
  preturn = P.tree[1]
  P.tree[1] = P.tree[P.n]
  P.n -= 1
  nmax = P.n
  p = 1

  while 2p <= nmax

    # argmin [p, 2p, 2p+1]
    m = p
    for down in 2p:2p+1
      down > nmax && break
      P.priority(P.tree[down], P.tree[m]) && (m = down)
    end

    m == p && break

    P.tree[p], P.tree[m] = P.tree[m], P.tree[p]
    p = m

  end
  return preturn

end

#import
is_empty(P::PriorityQueue{T}) where T = P.n == 0

function main()
  N, M, S = readint()

  G =Graph(N,Edge)

  Amax = 0
  for i in 1:M
    U,V,A,B = readint()
    append!(G.nodes[U],[Edge(V,A,B)])
    append!(G.nodes[V],[Edge(U,A,B)])
    G.degree[U]+=1
    G.degree[V]+=1
    Amax < A && (Amax = A)
  end

  for i in 1:N
    C,D = readint()
    G.degree[i] += 1
    append!(G.nodes[i], [Edge(i,-C,D)])
  end

  d = Array{int,2}(undef,N,N*Amax+1)
  d .= typemax(int)
  if S > Amax * N
    S = Amax * N
  end
  d[1, S+1] = 0

  checked = Vector{Bool}(undef,N)
  checked .= false

  Q = PriorityQueue(isless, State)
  push(Q, State(1,S,0))

  jS = S
  jp = 1
  checked[1]=true
  dd = Vector{int}(undef, N)

  while ! is_empty(Q)
    q = pop(Q)
    p, kane, time = q.iti, q.kane, q.time

    if !checked[p] # = false
      checked[p] = true
      dd[p] = time
    end

    flag = true
    for i in 1:N
      if !checked[i]
        flag = false
        break
      end
    end
    flag && break
    for E in G.nodes[p]
      kane - E.cost < 0 && continue
      newkane = min(Amax*N, kane-E.cost)
      newtime = E.time + time
      push(Q, State(E.y, newkane, newtime))
    end
    #println(q, p)
  end
  for i in 2:N
    println(dd[i])
  end
  #println(G)
end

main()


using DataStructures
quit() = exit(0)
in_1() = parse(Int, readline())
in_a() = parse.(Int, split(readline()))

function main()
  N, M, S = in_a()
  UVAB = [(in_a()...,) for _ in 1:M]
  maxA = maximum(uvab[3] for uvab in UVAB)
  CD = [(in_a()...,) for _ in 1:N]

  E = map(1:N) do n
    cd = CD[n]
    vcat([(n, cd[1], cd[2])],
         [(v, -a, b) for (u,v,a,b) in UVAB if u==n],
         [(u, -a, b) for (u,v,a,b) in UVAB if v==n])
  end

  vs = [i==1 ? 0 : nothing for i in 1:N]

  que = PriorityQueue((1,S)=>0)
  while !isempty(que)
    (n, s), v = peek(que)
    if isnothing(vs[n])
      vs[n] = v
      if all(!isnothing, vs)
        for v in vs[2:end]
          println(v)
        end
        quit()
      end
    end
    dequeue!(que)

    for (nto, sadd, vadd) in E[n]
      s2 = s + sadd
      s2 = min(s2, maxA*(N-1))
      s2 < 0 && continue
      s==s2 && nto==n && continue
      v2 = v + vadd
      delkeys = [(n3, s3) for ((n3, s3), v3) in que if n3==nto && s3 <= s2 && v3 >= v2]
      for dk in delkeys
        delete!(que, dk)
      end
      if v2 <= get(que, (nto, s2), v2)
        que[(nto, s2)] = v2
      end
    end
  end
end

isempty(ARGS) && main()


using DataStructures
quit() = exit(0)
in_1() = parse(Int, readline())
in_a() = parse.(Int, split(readline()))

function main()
  N, M, S = in_a()
  UVAB = [(in_a()...,) for _ in 1:M]
  CD = [(in_a()...,) for _ in 1:N]

  E = map(1:N) do n
    cd = CD[n]
    vcat([(n, cd[1], cd[2])],
         [(v, -a, b) for (u,v,a,b) in UVAB if u==n],
         [(u, -a, b) for (u,v,a,b) in UVAB if v==n])
  end

  vs = [i==1 ? 0 : nothing for i in 1:N]

  que = PriorityQueue((1,S)=>0)
  while !isempty(que)
    (n, s), v = peek(que)
    if isnothing(vs[n])
      vs[n] = v
      if all(!isnothing, vs)
        for v in vs[2:end]
          println(v)
        end
        quit()
      end
    end
    dequeue!(que)

    for (nto, sadd, vadd) in E[n]
      s2 = s + sadd
      s2 >= 0 || continue
      v2 = v + vadd
      delkeys = [(n3, s3) for ((n3, s3), v3) in que if n3==nto && s3 <= s2 && v3 >= v2]
      for dk in delkeys
        delete!(que, dk)
      end
      if v2 <= get(que, (nto, s2), v2)
        que[(nto, s2)] = v2
      end
    end
  end
end

isempty(ARGS) && main()


using DataStructures
const lines=readlines()
function main()
	N,M,S=map(x->parse(Int,x),split(popfirst!(lines)))
	LIM=50*(N-1)+1
	S=min(S+1,LIM)
	G=[Tuple{Int,Int,Int}[] for _=1:N]
	for _=1:M
		u,v,a,b=map(x->parse(Int,x),split(popfirst!(lines)))
		push!(G[u],(v,a,b))
		push!(G[v],(u,a,b))
	end
	C=Int[]
	D=Int[]
	for _=1:N
		c,d=map(x->parse(Int,x),split(popfirst!(lines)))
		push!(C,c)
		push!(D,d)
	end
	dist=ones(Int,N,LIM)*1145141919810931893
	dist[1,S]=0
	Q=PriorityQueue{Pair{Int,Int},Int}()
	Q[1=>S]=0
	while !isempty(Q)
		u,g=dequeue!(Q)
		cost=dist[u,g]
		nr=min(g+C[u],LIM)
		if dist[u,nr]>cost+D[u]
			Q[u=>nr]=dist[u,nr]=cost+D[u]
		end
		for (v,a,b)=G[u]
			if g>a
				nxt=cost+b
				nr=g-a
				if dist[v,nr]>nxt
					Q[v=>nr]=dist[v,nr]=nxt
				end
			end
		end
	end
	for i=2:N
		println(minimum(dist[i,:]))
	end
end
main()


using DataStructures
const lines=readlines()
function main()
	N,M,S=map(x->parse(Int,x),split(popfirst!(lines)))
	LIM=50*(N-1)+1+500
	S=min(S+1,LIM)
	G=[Tuple{Int,Int,Int}[] for _=1:N]
	for _=1:M
		u,v,a,b=map(x->parse(Int,x),split(popfirst!(lines)))
		push!(G[u],(v,a,b))
		push!(G[v],(u,a,b))
	end
	C=Int[]
	D=Int[]
	for _=1:N
		c,d=map(x->parse(Int,x),split(popfirst!(lines)))
		push!(C,c)
		push!(D,d)
	end
	dist=ones(Int,N,LIM)*1145141919810
	dist[1,S]=0
	Q=PriorityQueue{Pair{Int,Int},Int}()
	Q[1=>S]=0
	while !isempty(Q)
		u,g=dequeue!(Q)
		cost=dist[u,g]
		nr=min(g+C[u],LIM)
		if dist[u,nr]>cost+D[u]
			Q[u=>nr]=dist[u,nr]=cost+D[u]
		end
		for (v,a,b)=G[u]
			if g>a
				nxt=cost+b
				nr=g-a
				if dist[v,nr]>nxt
					Q[v=>nr]=dist[v,nr]=nxt
				end
			end
		end
	end
	for i=2:N
		println(minimum(dist[i,:]))
	end
end
main()


using DataStructures
const lines=readlines()
function main()
	N,M,S=map(x->parse(Int,x),split(popfirst!(lines)))
	LIM=50*(N-1)+1
	S=min(S+1,LIM)
	G=[Tuple{Int,Int,Int}[] for _=1:N]
	for _=1:M
		u,v,a,b=map(x->parse(Int,x),split(popfirst!(lines)))
		push!(G[u],(v,a,b))
		push!(G[v],(u,a,b))
	end
	C=Int[]
	D=Int[]
	for _=1:N
		c,d=map(x->parse(Int,x),split(popfirst!(lines)))
		push!(C,c)
		push!(D,d)
	end
	dist=ones(Int,N,LIM)*1145141919810
	dist[1,S]=0
	Q=PriorityQueue{Pair{Int,Int},Int}()
	Q[1=>S]=0
	while !isempty(Q)
		u,g=dequeue!(Q)
		cost=dist[u,g]
		nr=min(g+C[u],LIM)
		if dist[u,nr]>cost+D[u]
			Q[u=>nr]=dist[u,nr]=cost+D[u]
		end
		for (v,a,b)=G[u]
			if g>a
				nxt=cost+b
				nr=g-a
				if dist[v,nr]>nxt
					Q[v=>nr]=dist[v,nr]=nxt
				end
			end
		end
	end
	for i=2:N
		println(minimum(dist[i,:]))
	end
end
main()


using DataStructures
using Primes
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()
    mm = 2500
    N,M,S = parseMap(split(readline()))
    S = min(S, mm)
    g = [[] for _ in 1:N]
    for i in 1:M
        u,v,a,b = parseMap(split(readline()))
        push!(g[u], [v, a, b])
        push!(g[v], [u, a, b])
    end
    cd = []
    for i in 1:N
        c, d = parseMap(split(readline()))
        push!(cd, [c, d])
    end
    h = BinaryMinHeap{Array{Int}}()
    push!(h, [0, S*-1, 1])

    dp = Dict()
    for i in 1:N
        for j in 0:mm
            dp[(i,j)] = INF
        end
    end
    dp[(1,S)] = 0
    while(length(h) != 0)
        t = pop!(h)
        u = t[3]
        d = t[1]
        s = t[2]*-1
        if dp[(u, s)] < d
            continue
        end
        for i in g[u]
            v = i[1]
            dist = d+i[3]
            money = s-i[2]
            if money>=0
                if dp[(v, money)]>dist
                    dp[(v, money)] = dist
                    push!(h, [dist, -money, v])
                end
            end
        end
        s+=cd[u][1]
        s = min(mm, s)
        d+=cd[u][2]
        if s>=0 && dp[(u, s)]>d
            dp[(u, s)] = d
            push!(h, [d, -s, u])
        end
    end
    for i in 2:N
        res = INF
        for j in 0:mm
            res = min(res, dp[(i, j)])
        end
        println(res)
    end
end
INF = 1<<60
main()


using DataStructures
using Primes
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()
    mm = 2500
    N,M,S = parseMap(split(readline()))
    S = min(S, mm)
    g = [[] for _ in 1:N]
    for i in 1:M
        u,v,a,b = parseMap(split(readline()))
        push!(g[u], [v, a, b])
        push!(g[v], [u, a, b])
    end
    cd = []
    for i in 1:N
        c, d = parseMap(split(readline()))
        push!(cd, [c, d])
    end
    h = BinaryMinHeap{Array{Int}}()
    push!(h, [0, S*-1, 1])

    dp = Dict()
    for i in 1:N
        for j in 0:mm
            dp[(i,j)] = INF
        end
    end
    dp[(1,S)] = 0
    while(length(h) != 0)
        t = pop!(h)
        u = t[3]
        d = t[1]
        s = t[2]*-1
        if dp[(u, s)] < d
            continue
        end
        for i in g[u]
            v = i[1]
            dist = i[3]+d
            money = s-i[2]
            f = false
            while(true)
                if money>=0
                    if dp[(v, money)] > dist
                        dp[(v, money)] = dist
                        push!(h, [dist, money*-1, v])
                    end
                end
                dist+=cd[u][2]
                money+=cd[u][1]
                if money>mm
                    if f == false
                        f = true
                        money = mm
                    else

                        break
                    end
                end
            end

        end
    end
    for i in 2:N
        res = INF
        for j in 0:mm
            res = min(res, dp[(i, j)])
        end
        println(res)
    end
end
INF = 1<<60
main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    S,W = readline() |> split |> parseMap
    if S<=W
        println("unsafe")
    else
        println("safe")
    end
end
main()


function main()
  R = parse(Int, readline())
  println(2*pi*R)
end
main()

Score : 100 points

Problem Statement

Print the circumference of a circle of radius R.

Constraints

1 \leq R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

R

Output

Print the circumference of the circle.
Your output is considered correct if and only if its absolute or relative error from our answer is at most 10^{-2}.

Sample Input 1

1

Sample Output 1

6.28318530717958623200

Since we accept an absolute or relative error of at most 10^{-2}, 6.28 is also an acceptable output, but 6 is not.

Sample Input 2

73

Sample Output 2

458.67252742410977361942


quit() = exit(0)
in_1() = parse(Int, readline())
in_a() = parse.(Int, split(readline()))

function main()
  R = in_1()
  println(2pi*R)
end

main()


R = parse(Float64,readline());
answer = 2 * R * π;
print(answer);


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

function main()
	r = readline() |> parseInt
	println(pi*2.0*r)
end

main()


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

function main()
	r = readline() |> parseInt
	println(pi*2*r)
end

main()


function main()

  R = parse(Int, readline())

  println(2R * pi)

end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
R = parseInt(readline())
println(2 * R * pi)


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

function main()
    n=parseInt(readline())
    println(2*n*π)
end
main()


prsint(x)=parse(Int,x)

N=prsint(readline())
for i in 1:3
    if N%10==7
        println("Yes")
        exit()
    end
    global N=div(N,10)
end
println("No")

Score : 100 points

Problem Statement

Given is a three-digit integer N. Does N contain the digit 7?

If so, print Yes; otherwise, print No.

Constraints

100 \leq N \leq 999

Input

Input is given from Standard Input in the following format:

N

Output

If N contains the digit 7, print Yes; otherwise, print No.

Sample Input 1

117

Sample Output 1

Yes

117 contains 7 as its last digit.

Sample Input 2

123

Sample Output 2

No

123 does not contain the digit 7.

Sample Input 3

777

Sample Output 3

Yes


quit() = exit(0)
in_1(ch) = parse(Int, take!(ch))
in_a(ch) = parse.(Int, split(take!(ch)))

quit() = exit(0)
in_1() = parse(Int, readline())
in_a() = parse.(Int, split(readline()))

function main()
  N = readline()
  println(any(isequal('7'), N) ? "Yes" : "No")
end

isempty(ARGS) && main()


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()
  a = readline()

  if a[1]=='7' || a[2]=='7' || a[3]=='7'
    println("Yes")
  else
    println("No")
  end
end

main()


n = readline()

println(findfirst(str -> str == '7', n) != nothing ? "Yes" : "No")


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


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


N = readline()

if N[1] == "7" || N[1] == "7" || N[1] == "7"
    println("Yes")
else
    println("No")
end


module AtCoder

    export readline

    function readline(T)
        return Base.parse(T, Base.readline(Base.stdin))
    end

end

module ABC162

    import Main.AtCoder
    export task_a

    function evaluate_task_a(target_integer::Core.Int64)
        return Base.in( target_integer, Base.digits( AtCoder.readline(Core.Int64) ) ) ? "Yes" : "No"
    end

    function execute_task_a()
        Base.println(evaluate_task_a(7))
    end

end

ABC162.execute_task_a()


module AtCoder

    export readline

    function readline(T)
        return Base.parse(T, Base.readline(Base.stdin))
    end

end

module ABC162

    import Main.AtCoder
    export task_a

    function evaluate_task_a(target_integer::Core.UInt16)

        given_integer = AtCoder.readline(Core.UInt16)
        data_digits   = Base.digits(given_integer)

        for itr = 1:1:Base.size(data_digits, 1)
            if (data_digits[itr] == target_integer)
                return "Yes"
            end
        end

        return "No"

	end

	function execute_task_a()
		Base.println(evaluate_task_a(Base.convert(Core.UInt16, 7)))
	end

end

ABC162.execute_task_a()