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

Score : 300 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

Find the sum of A_i \times A_j over all pairs (i,j) such that 1\leq i < j \leq N, modulo (10^9+7).

Constraints

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

0 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} A_i A_j, modulo (10^9+7).

Sample Input 1

3
1 2 3

Sample Output 1

11

We have 1 \times 2 + 1 \times 3 + 2 \times 3 = 11.

Sample Input 2

4
141421356 17320508 22360679 244949

Sample Output 2

437235829

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.


const MOD = Int(10^9+7)

function solve()
    N = parse(Int, readline())
    A = parse.(Int, split(readline()))

    S = 0
    for i = 1:N
        S = (S+A[i])%MOD
    end

    res = 0
    for i = 1:(N-1)
        S = (S-A[i])%MOD
        res += (A[i]*S)%MOD
    end

    println((res%MOD + MOD)%MOD)
end

solve()


function solve()
    N = parse(Int, readline())
    A = parse.(Int, split(readline()))

    S = sum(A)
    res = 0

    for i = 1:N-1
        S -= A[i]
        res += S*A[i]
    end

    println(res)
end

solve()


function main()
    S = parse(Int, readline())
    T = parse.(Int, split(readline()))

    ret = 0
    #println(typeof(ret))
    ruiseki = 0
    for i in S:-1:2
        #println(i)
        ruiseki += T[i]
        ruiseki = ruiseki % (10 ^ 9 + 7)
        ret += (T[i - 1] * ruiseki) % (10 ^ 9 + 7)
        ret = ret % (10 ^ 9 + 7)
        #println(ret)
    end
    print(ret % (10 ^ 9 + 7))
end

main()


function main()
    S = parse(Int, readline())
    T = parse.(Int, split(readline()))

    ret = 0
    ruiseki = 0
    for i in S:-1:2
        ruiseki += T[i] % (10 ^ 9 + 7)
        ret += (T[i - 1] * ruiseki)  % (10 ^ 9 + 7)
        #println(ret)
    end
    print(ret % (10 ^ 9 + 7))
end

main()


function main()
    S = parse(Int, readline())
    T = parse.(Int, split(readline()))

    ret = 0
    ruiseki = 0
    for i in S:-1:2
        ruiseki += T[i]
        ret += T[i - 1] * ruiseki
        ret = ret
    end
    print(ret % (10 ^ 9 + 7))
end

main()


function main()
    S = parse(Int, readline())
    T = parse.(Int, split(readline()))

    ret = 0
    ruiseki = 0
    for i in S:-1:2
        ruiseki += T[i]
        ret += T[i - 1] * ruiseki
        ret = ret % (10 ^ 9 + 7)
    end
    print(ret % (10 ^ 9 + 7))
end

main()


function main()
    S = parse(Int, readline())
    T = parse.(Int, split(readline()))

    ret = 0
    ruiseki = 0
    for i in S:-1:2
        ruiseki += T[i]
        ret += T[i - 1] * ruiseki
        ret = ret % (10 ^ 9 + 7)
    end
    print(ret)
end

main()


function main()
    S = parse(Int, readline())
    T = parse.(Int, split(readline()))

    ret = 0
    for i in 1:S-1
        tmp = 0
        for j in i+1:S
            #ret += T[i] * T[j] % (10 ^ 9 + 7)
            tmp += T[j]
        end
        tmp = tmp % (10 ^ 9 + 7)
        ret += T[i] * tmp
    end
    print(ret % (10 ^ 9 + 7))
end

main()


function main()
    m=10^9+7
    n=myread(Int)
    a=myreads(Int)
    s=zeros(Int,n+1)
    for i=1:n
        s[i+1]=mod(s[i]+a[i],m)
    end
    ans=0
    for i=1:n
        ans=mod(ans+mod(s[end]*a[i],m),m)
        ans=mod(ans-mod(s[i+1]*a[i],m),m)
    end
    println(ans)
end

# ----------input function----------
sread()=chomp(readline())
sreads(sp=" ")=split(readline(),sp)
myread(ty)=parse(ty,sread())
myreads(ty)=parse.(ty,sreads())
main()


function main()
    N = parse(Int, readline())
    A = []
    for i ∈ 1:N
        a = parse(Int,  readuntil(stdin, " "))
        append!(A, [a])
    end

    sum = 0
    accumulated = 0
    for i ∈ 2:N
        accumulated = mod(accumulated + A[i-1], 7+10^9)
        sum = mod(sum + accumulated * A[i], 7+10^9)
    end

    println(mod(sum, 7+10^9))
end

main()


function main()
    N = parse(Int, readline())
    A = []
    for i in 1:N
        a = parse(Int,  readuntil(stdin, " "))
        append!(A, [a])
    end

    sum_ = 0
    accumulated = 0
    for i in reverse(1:N-1)
        accumulated += A[i+1]
        sum_ += accumulated * A[i]
    end
    println(mod(sum_, 7+10^9))
end

main()


function main()
    N = parse(Int, readline())
    A = []
    for i in 1:N
        a = parse(Int,  readuntil(stdin, " "))
        append!(A, [mod(a, 7+10^9)])
    end
    sum_ = 0
    accumulated = 0
    for i in reverse(1:N-1)
        accumulated += A[i+1]
        sum_ += accumulated * A[i]
    end
    println(mod(sum_, 7+10^9))
end

main()


function main()

  n = parse(Int, readline())
  A = parse.(Int, split(readline()))

  A_sum = sum(A)
  A_sum %= 10^9+7

  ans = 0
  for i in 1:n
    A_sum -= A[i]
    if A_sum < 0
      A_sum += 10^9+7
    end
    ans += A[i]*A_sum
    ans %= 10^9+7
  end

  println(ans)

end

main()


function main()

  n = parse(Int, readline())
  A = parse.(Int, split(readline()))

  A_sum = [0]
  A_sum = sum(A)

  ans = 0
  for i in 1:n
    A_sum -= A[i]
    ans += A[i]*A_sum
    ans %= (10^9+7)
  end

  println(ans)

end

main()


function main()

  n = parse(Int, readline())
  A = parse.(Int, split(readline()))

  A_sum = [0]
  A_sum = sum(A) % (10^9+7)

  ans = 0
  for i in 1:n
    A_sum -= A[i]
    ans += A[i]*A_sum
    ans %= (10^9+7)
  end

  println(ans)

end

main()


parse_input(type=String, x=readline()) = begin
  type == String && return x
  parse(type, x)
end
parse_inputs(type=String) = parse_input.(type, split(readline()))

function solve()
  _ = parse_input()
  A = parse_inputs(BigInt)
  end_a = A[end]
  ans = 0
  for a in A[end-1:-1:1]
    ans += a*end_a
    end_a += a
  end
  mod(ans, (10^9)+7)
end

println(solve())


parse_input(type=String, x=readline()) = begin
  type == String && return x
  parse(type, x)
end
parse_inputs(type=String) = parse_input.(type, split(readline()))

function solve()
  _ = parse_input()
  A = parse_inputs(Int)
  end_a = A[end]
  ans = 0
  for a in A[end-1:-1:1]
    ans += a*end_a
    end_a += a
  end
  mod(ans, (10^9)+7)
end

println(solve())


function main()
    N = pread(Int)
    a = readvec(Int, N)
    asum = zeros(Int, N)
    asum[1] = a[1]
    for i in 2:N
        asum[i] = (asum[i-1]+a[i])
    end
    c = 0
    for i in 1:N
        c += a[i] * ((asum[N]-asum[i]) % (10^9+7))
        c %= 10^9+7
    end
    println(c)
end

import Base.parse
import Base.StringVector

parse(::Type{String},  str::AbstractString) = str
isdelim(x::UInt8, xs::Set{UInt8}) = x in xs
const delimset = Set([0x0a, 0x20])

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

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)

function readvec(tys::Tuple , len::Signed)::Tuple{map(x -> Vector{x},tys)...}
    rv = Tuple{map(x -> Vector{x},tys)...}(Vector{ty}(undef,len) for ty in tys)
    for i in 1:len
        for j in 1:length(tys)
            @inbounds rv[j][i] = pread(tys[j])
        end
    end
    rv
end

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 in 1:s[1]
        v[i,:] = readvec(ty, s[2])
    end
    v
end

main()


parseInt(x) = parse(Int128, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
using Primes
function main()
  N = parseInt(readline())
  A = parseMap(split(readline()))
  seki = Int128(0)
  for i in 1:N-1
     seki += mod(A[i]*mod(sum(A[i+1:N]),Int128(10^9)+7),Int128(10^9)+7)
  #  for j in i+1:N
  #    seki += mod(Int128(A[i]*A[j]),Int128(10^9)+7)
  #  end
  end
  println(mod(seki,Int128(10^9)+7))

end
main()


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

function main()
    ans = 0
    MOD = 10^9 + 7
    N = parseInt(readline())
    A = parseMap(split(readline()))
    af = 0
    for i in N:-1:2
        af += A[i]
        ans += ((A[i - 1] % MOD) * (af % MOD))
        ans %= MOD
    end
    println(ans)
end

main()


function F()
  N = parse(BigInt,readline())
  A= parse.(BigInt,split(readline()))
  ans = 0
  S = sum(A)
  for i =1:N-1
    S -= A[i]
    ans += A[i]*S
  end
  ans = mod(ans,10^9+7)
  println(ans)
end
F()


parseInt(x) = parse(Int128, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
using Primes
function main()
  N = parseInt(readline())
  A = parseMap(split(readline()))

  #i=BigInt(1)
  # println(factor(K)) ; println(collect(factor(K)))
  # println(isprime(K))
  # for item in collect(factor(K))
  #   println("$(item.first): $(item.second)")
  # end
  # println(in(79=>1 ,collect(factor(K))))

  #exit()
  seki = Int128(0)
  for i in 1:N-1
    for j in i+1:N
      seki += mod(Int128(A[i]*A[j]),Int128(10^9)+7)
      #println(i,":",j,":",seki,":",A[i]*A[j])
    end
  end
  println(mod(seki,Int128(10^9)+7))

end
main()


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

function solve(N,A)
m=10^9+7
res=0
for i=1:N-1
        res+= sum(A[i+1:end]) .% m * A[i] % m
end
println(res.% m)
end
solve(N,A)


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

function solve(N,A)
m=10^9+7
res=0
for i=1:N-1
    res+= sum((A[i+1:end] .* A[i]) .% m ).% m

end
println(res.% m)
end
solve(N,A)


function F()
  N = parse(BigInt,readline())
  A= parse.(BigInt,split(readline()))
  ans = 0

  for i =1:N-1
    ans += A[i]*sum(A[i+1:N])
  end
  ans = mod(ans,10^9+7)
  println(ans)
end
F()


function main()
    N = pread(Int)
    a = readvec(Int, N)
    asum = zeros(Int, N)
    asum[1] = a[1]
    for i in 2:N
        asum[i] = (asum[i-1]+a[i])
    end
    c = 0
    for i in 1:N
        c += a[i] * (asum[N]-asum[i])
        c %= 10^9+7
    end
    println(c)
end

import Base.parse
import Base.StringVector

parse(::Type{String},  str::AbstractString) = str
isdelim(x::UInt8, xs::Set{UInt8}) = x in xs
const delimset = Set([0x0a, 0x20])

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

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)

function readvec(tys::Tuple , len::Signed)::Tuple{map(x -> Vector{x},tys)...}
    rv = Tuple{map(x -> Vector{x},tys)...}(Vector{ty}(undef,len) for ty in tys)
    for i in 1:len
        for j in 1:length(tys)
            @inbounds rv[j][i] = pread(tys[j])
        end
    end
    rv
end

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 in 1:s[1]
        v[i,:] = readvec(ty, s[2])
    end
    v
end

main()


using DataStructures
const double = Float64
const int = Int64

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

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

  MOD = 10^9+7
  S = Vector{int}(undef,N)
  S[N] = A[N]
  for i in N-1:-1:1
    S[i] = (A[i]+S[i+1])%MOD
  end

  s = 0
  for i in 1:N-1
    s = (s + A[i]*S[i+1]) % MOD
  end

  println(s)

end

main()


using DataStructures
const double = Float64
const int = Int64

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

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

  MOD = 10^9+7
  S = Vector{int}(undef,N)
  S[N] = A[N]
  for i in N-1:-1:1
    S[i] = A[i]+S[i+1]
  end

  s = 0
  for i in 1:N-1
    s = (s + A[i]*S[i+1]) % MOD
  end

  println(s)

end

main()


function GCD(a,b)
    return b!=0 ? GCD(b,a%b) : a
end

function main()
    n=parse.(Int,readline())
    A=parse.(Int,split(readline()))
    all_gcd=A[1]
    for i=2:n
        all_gcd=GCD(all_gcd,A[i])
    end
    if all_gcd!=1
        println("not coprime")
        exit()
    end
    A_max=maximum(A)
    hurui=zeros(Int,A_max+10)
    for i=1:n
        hurui[A[i]]+=1
    end
    for i=2:A_max+10
        j=i
        cnt=0
        while j<=A_max+10
            if hurui[j]==true
                cnt+=hurui[j]
            end
            j+=i
        end
        if cnt>1
            println("setwise coprime")
            exit()
        end
    end
    println("pairwise coprime")
end
main()

Score : 500 points

Problem Statement

We have N integers. The i-th number is A_i.

\{A_i\} is said to be pairwise coprime when GCD(A_i,A_j)=1 holds for every pair (i, j) such that 1\leq i < j \leq N.

\{A_i\} is said to be setwise coprime when \{A_i\} is not pairwise coprime but GCD(A_1,\ldots,A_N)=1.

Determine if \{A_i\} is pairwise coprime, setwise coprime, or neither.

Here, GCD(\ldots) denotes greatest common divisor.

Constraints

2 \leq N \leq 10^6

1 \leq A_i\leq 10^6

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

If \{A_i\} is pairwise coprime, print pairwise coprime; if \{A_i\} is setwise coprime, print setwise coprime; if neither, print not coprime.

Sample Input 1

3
3 4 5

Sample Output 1

pairwise coprime

GCD(3,4)=GCD(3,5)=GCD(4,5)=1, so they are pairwise coprime.

Sample Input 2

3
6 10 15

Sample Output 2

setwise coprime

Since GCD(6,10)=2, they are not pairwise coprime. However, since GCD(6,10,15)=1, they are setwise coprime.

Sample Input 3

3
6 10 16

Sample Output 3

not coprime

GCD(6,10,16)=2, so they are neither pairwise coprime nor setwise coprime.


function GCD(a,b)
    return b!=0 ? GCD(b,a%b) : a
end

function main()
    n=parse.(Int,readline())
    A=parse.(Int,split(readline()))
    setwise=false
    pairwise=true
    all_gcd=A[1]
    for i=2:n
        all_gcd=GCD(all_gcd,A[i])
    end
    if all_gcd==1
        setwise=true
    end
    A_max=maximum(A)
    hurui=zeros(Bool,A_max)
    for i=1:n
        hurui[A[i]]=true
    end
    for i=2:A_max
        j=i
        one=false
        cnt=0
        while j<=A_max
            if hurui[j]==true
                cnt+=1
            end
            j+=i
        end
        if cnt>1
            pairwise=false
        end
    end
    if pairwise
        println("pairwise coprime")
    elseif setwise
        println("setwise coprime")
    else
        println("not coprime")
    end
end
main()


function GCD(a,b)
    return b!=0 ? GCD(b,a%b) : a
end

function main()
    n=parse.(Int,readline())
    A=parse.(Int,split(readline()))
    setwise=false
    pairwise=true
    all_gcd=A[1]
    for i=2:n
        all_gcd=GCD(all_gcd,A[i])
    end
    if all_gcd==1
        setwise=true
    end
    A_max=maximum(A)
    hurui=zeros(Bool,A_max)
    for i=1:n
        hurui[A[i]]=true
    end
    for i=2:floor(Int,sqrt(A_max)+1)
        j=i
        one=false
        while j<=A_max
            if hurui[j]==true && one==false
                one=true
            elseif hurui[j]==true
                pairwise=false
                break
            end
            j+=i
        end
    end
    if pairwise
        println("pairwise coprime")
    elseif setwise
        println("setwise coprime")
    else
        println("not coprime")
    end
end
main()


# SPDX-License-Identifier: X11
# 2020-08-30
# Coprime (500pt)

function main()
  N = parse(Int, readline())
  A = parse.(Int, split(readline(), " "))

  if reduce(gcd, A) > 1
    println("not coprime")
    return
  end

  sort!(A)
  bound = isqrt(A[N])

  for i ∈ 1:(N - 1)
    if A[i] == A[i + 1]
      println("setwise coprime")
      return
    end
  end

  if bound < 2
    println("pairwise coprime")
    return
  end

  ps = erato(bound)
  as::Vector{Bool} = falses(bound)

  for i ∈ A
    if hash!(as, i, ps) === nothing
      println("setwise coprime")
      return
    end
  end

  println("pairwise coprime")
end

function erato(bound)::Vector{Bool}
  A = falses(bound)

  bound < 2 && return A

  A[2] = true

  for i ∈ 3:2:bound
    A[i] = true
  end

  for i ∈ 3:2:bound
    if A[i]
      for j ∈ i:2:(bound ÷ i)
        A[i * j] = false
      end
    end
  end

  return A
end

function hash!(as, n, ps)
  if n & 1 == 0
    if as[2]
      return nothing
    else
      as[2] = true
    end
  end

  for i ∈ 3:2:isqrt(n)
    if ps[i] && n % i == 0
      if as[i]
        return nothing
      else
        as[i] == true
      end
    end
  end

  return n
end

isqrt = Int ∘ ceil ∘ sqrt

main()


# SPDX-License-Identifier: X11
# 2020-08-30
# Coprime (500pt)

function main()
  N = parse(Int, readline())
  A = parse.(Int, split(readline(), " "))

  if reduce(gcd, A) > 1
    println("not coprime")
    return
  end

  sort!(A)
  bound = (Int ∘ trunc ∘ sqrt)(A[N])

  for i ∈ 1:(N - 1)
    if A[i] == A[i + 1]
      println("setwise coprime")
      return
    end
  end

  if bound < 2
    println("pairwise coprime")
    return
  end

  ps = erato(bound)
  as::Vector{Bool} = falses(bound)

  for i ∈ A
    if hash!(as, i, ps) === nothing
      println("setwise coprime")
      return
    end
  end

  println("pairwise coprime")
end

function erato(bound)::Vector{Bool}
  A = falses(bound)

  bound < 2 && return A

  A[2] = true

  for i ∈ 3:2:bound
    A[i] = true
  end

  for i ∈ 3:2:bound
    if A[i]
      for j ∈ i:2:(bound ÷ i)
        A[i * j] = false
      end
    end
  end

  return A
end

function hash!(as, n, ps)
  if n & 1 == 0
    if as[2]
      return nothing
    else
      as[2] = true
    end
  end

  for i ∈ 3:2:(Int ∘ trunc ∘ sqrt)(n)
    if ps[i] && n % i == 0
      if as[i]
        return nothing
      else
        as[i] == true
      end
    end
  end

  return n
end

main()


# SPDX-License-Identifier: X11
# 2020-08-29 (Live)
# Coprime (500pt)

function main()
  N = parse(Int, readline())
  A = parse.(Int, split(readline(), " "))

  if reduce(gcd, A) > 1
    println("not coprime")
    return 0
  end

  sort!(A)
  bound = (Int ∘ trunc ∘ sqrt)(A[N])

  ps = erato(bound)
  as = falses(bound)

  for i ∈ A
    if hash!(i, as, ps) === nothing
      println("setwise coprime")
      return 0
    end
  end

  println("pairwise coprime")
end

function erato(bound)
  A = falses(bound)
  A[2] = true

  for i ∈ 3:2:bound
    A[i] = true
  end

  for i ∈ 3:2:bound
    if A[i]
      for j ∈ i:2:(bound ÷ i)
        A[i * j] = false
      end
    end
  end

  return A
end

function hash!(n, as, ps)
  if n & 1 == 0
    if as[2]
      return nothing
    else
      as[2] = true
    end
  end

  for i ∈ 3:2:(Int ∘ trunc ∘ sqrt)(n)
    if ps[i] && n % i == 0
      if as[i]
        return nothing
      else
        as[i] == true
      end
    end
  end

  return n
end

main()


# SPDX-License-Identifier: X11
# 2020-08-29 (Live)
# Coprime (500pt)

function main()
  N = parse(Int, readline())
  A = parse.(Int, split(readline(), " "))

  reduce(gcd, A) > 1 && println("not coprime")

  sort!(A)
  bound = (Int ∘ trunc ∘ sqrt)(A[N])

  ps = erato(bound)
  as = falses(bound)

  for i ∈ A
    if hash!(i, as, ps) === nothing
      println("not coprime")
      return nothing
    end
  end

  println("pairwise coprime")
end

function erato(bound)
  A = falses(bound)
  A[2] = true

  for i ∈ 3:2:bound
    A[i] = true
  end

  for i ∈ 3:2:bound
    if A[i]
      for j ∈ i:2:(bound ÷ i)
        A[i * j] = false
      end
    end
  end

  return A
end

function hash!(n, as, ps)
  if n & 1 == 0
    if as[2]
      return nothing
    else
      as[2] = true
    end
  end

  for i ∈ 3:2:(Int ∘ trunc ∘ sqrt)(n)
    if ps[i] && n % i == 0
      if as[i]
        return nothing
      else
        as[i] == true
      end
    end
  end

  return n
end

main()


function main()
    data = readlines()
    N = parse(Int, data[1])
    A = parse.(Int, split(data[2]))
    pc = "pairwise coprime"
    sc = "setwise coprime"
    nc = "not coprime"
    ret = []
    for i in 1:length(A)-1
        B = A[i+1:end]
        gcds = gcd.(A[i], B)
        append!(ret, gcds)
    end
    ret = unique(ret)
    r = length(ret)
    r==1 && return println(pc)
    return println(r==N ? sc : nc)
end
main()


function main()
    data = readlines()
    N = parse(Int, data[1])
    A = parse.(Int, split(data[2]))
    pc = "pairwise coprime"
    sc = "setwise coprime"
    nc = "not coprime"
    for i in 1:length(A)-1
        B = A[i+1:end]
        gcds = gcd.(A[i], B)
        true in (gcds .> 1) && (return println(gcd(A)≡1 ? sc : nc) )
    end
    return println(pc)
end
main()


using DataStructures
const double = Float64
const int = Int64

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

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

  Table = Vector{Vector{int}}(undef,10^6)
  for i in 1:10^6
    Table[i] = Vector{int}(undef,0)
  end

  for i in 2:10^6
    if length(Table[i]) != 0
      continue
    end
    for j in i:i:10^6
      append!(Table[j],[i])
    end
  end

  V = zeros(int,10^6)

  flag = true
  for i in 1:N
    for m in Table[A[i]]
      V[m] += 1
      if V[m] > 1
        flag = false
        break
      end
    end
    if ! flag
      continue
    end
  end

  r = A[1]
  for i in 2:N
    r = gcd(r,A[i])
  end

  if r != 1
    println("not coprime")
  else
    if flag
      println("pairwise coprime")
    else
      println("setwise coprime")
    end
  end

end

main()


function main()
    data = readlines()
    N = parse(Int, data[1])
    A = parse.(Int, split(data[2]))
    pc = "pairwise coprime"
    sc = "setwise coprime"
    nc = "not coprime"
    gcd(A)≡1 && (return println(sc))
    for i in 1:length(A)-1
        for j in i+1:length(A)
            gcd(A[i], A[j])≠1 && (return println(nc))
        end
    end
    return println(pc)
end
main()


function main()
    data = readlines()
    N = parse(Int, data[1])
    A = parse.(Int, split(data[2]))
    pc = "pairwise coprime"
    sc = "setwise coprime"
    nc = "not coprime"
    cd(A)≡1 && (return println(sc))
    for i in 1:length(A)-1
        for j in i+1:length(A)
            gcd(A[i], A[j])≠1 && (return println(nc))
        end
    end
    return println(pc)
end
main()


function main()
    data = readlines()
    N = parse(Int, data[1])
    A = parse.(Int, split(data[2]))
    pc = "pairwise coprime"
    sc = "setwise coprime"
    nc = "not coprime"
    cd(A)≡1 && return println(sc)
    for i in 1:length(A)-1
        for j in i+1:length(A)
            gcd(A[i], A[j])≠1 && return println(nc)
        end
    end
    return println(pc)
end
main()


function main()
    data = readlines()
    N = parse(Int, data[1])
    A = parse.(Int, split(data[2]))
    pc = "pairwise coprime"
    sc = "setwise coprime"
    nc = "not coprime"
    for i in 1:length(A)-1
        for j in i+1:length(A)
            gcd(A[i], A[j])≠1 && return println(gcd(A)≡1 ? sc : nc)
        end
    end
    return println(pc)
end
main()


@inbounds function main()
  h,w,m=parse.(Int,split(readline()))
  hc=Dict{Int,Int}()
  wc=Dict{Int,Int}()
  hw=Set{Tuple{Int,Int}}()
  for i=1:m
    hi,wi=parse.(Int,split(readline()))
    push!(hw,(hi,wi))
    hc[hi]=get(hc,hi,0)+1
    wc[wi]=get(wc,wi,0)+1
  end
  mh=first(findmax(hc))
  mw=first(findmax(wc))
  hs = filter(p->p.second==mh,hc) |> keys
  ws = filter(p->p.second==mw,wc) |> keys
  for i=hs
    for j=ws
      (i, j) ∉ hw && return println(mh+mw)
    end
  end
  println(mh+mw-1)
end
main()

Score : 500 points

Problem Statement

We have a two-dimensional grid with H \times W squares. There are M targets to destroy in this grid - the position of the i-th target is \left(h_i, w_i \right).

Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target.

Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed.

Constraints

All values in input are integers.

1 \leq H, W \leq 3 \times 10^5

1 \leq M \leq \min\left(H\times W, 3 \times 10^5\right)

1 \leq h_i \leq H

1 \leq w_i \leq W

\left(h_i, w_i\right) \neq \left(h_j, w_j\right) \left(i \neq j\right)

Input

Input is given from Standard Input in the following format:

H W M
h_1 w_1
\vdots
h_M w_M

Output

Print the answer.

Sample Input 1

2 3 3
2 2
1 1
1 3

Sample Output 1

3

We can destroy all the targets by placing the bomb at \left(1, 2\right).

Sample Input 2

3 3 4
3 3
3 1
1 1
1 2

Sample Output 2

3

Sample Input 3

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

Sample Output 3

6


@inbounds function main()
  h,w,m=parse.(Int,split(readline()))
  hc=Dict{Int,Int}()
  wc=Dict{Int,Int}()
  hw=Set{Tuple{Int,Int}}()
  for i=1:m
    hi,wi=parse.(Int,split(readline()))
    push!(hw,(hi,wi))
    hc[hi]=get(hc,hi,0)+1
    wc[wi]=get(wc,wi,0)+1
  end
  mh=first(findmax(hc))
  mw=first(findmax(wc))
  hs = filter(hc) do (k,v)
    v == mh
  end |> keys
  ws = filter(wc) do (k,v)
    v == mw
  end |> keys
  for i=hs
    for j=ws
      if !((i,j) in hw)
        println(mh+mw)
        return 0
      end
    end
  end
  println(mh+mw-1)
end
main()


@inbounds function main()
  h,w,m=parse.(Int,split(readline()))
  hc=Dict{Int,Int}()
  wc=Dict{Int,Int}()
  hw=Set{Tuple{Int,Int}}()
  for i=1:m
    hi,wi=parse.(Int,split(readline()))
    push!(hw,(hi,wi))
    hc[hi]=get(hc,hi,0)+1
    wc[wi]=get(wc,wi,0)+1
  end
  mh=first(findmax(hc))
  mw=first(findmax(wc))
  for i=keys(filter(x->last(x)==mh,hc))
    for j=keys(filter(x->last(x)==mw,wc))
      if !((i,j) in hw)
        println(mh+mw)
        return 0
      end
    end
  end
  println(mh+mw-1)
end
main()


function main()
  h,w,m=parse.(Int,split(readline()))
  hc=Dict{Int,Int}()
  wc=Dict{Int,Int}()
  hw=Set{Tuple{Int,Int}}()
  for i=1:m
    hi,wi=parse.(Int,split(readline()))
    push!(hw,(hi,wi))
    hc[hi]=get(hc,hi,0)+1
    wc[wi]=get(wc,wi,0)+1
  end
  mh=first(findmax(hc))
  mw=first(findmax(wc))
  for i=keys(filter(x->last(x)==mh,hc))
    for j=keys(filter(x->last(x)==mw,wc))
      if !((i,j) in hw)
        println(mh+mw)
        return 0
      end
    end
  end
  println(mh+mw-1)
end
main()


@inbounds function main(io=stdin)
  readto(target = '\n') = readuntil(io, target)
  readnum(T::Type{<:Number} = Int; dlm = isspace, kwargs...) =
      parse.(T, split(readto(), dlm; kwargs...))::Vector{T}
  h,w,m=readnum()
  hc=Dict{Int,Int}()
  wc=Dict{Int,Int}()
  hw=Set{Tuple{Int,Int}}()
  for i=1:m
    hi,wi=readnum()
    push!(hw,(hi,wi))
    hc[hi]=get(hc,hi,0)+1
    wc[wi]=get(wc,wi,0)+1
  end
  mh=first(findmax(hc))
  mw=first(findmax(wc))
  for i=keys(filter(x->last(x)==mh,hc))
    for j=keys(filter(x->last(x)==mw,wc))
      if !((i,j) in hw)
        println(mh+mw)
        return 0
      end
    end
  end
  println(mh+mw-1)
end
main()


@inbounds function main(io=stdin)
  readto(target = '\n') = readuntil(io, target)
  readnum(T::Type{<:Number} = Int; dlm = isspace, kwargs...) =
      parse.(T, split(readto(), dlm; kwargs...))::Vector{T}
  h,w,m=readnum()
  hc=Dict{Int,Int}()
  wc=Dict{Int,Int}()
  hw=Tuple{Int,Int}[]
  for i=1:m
    hi,wi=readnum()
    push!(hw,(hi,wi))
    hc[hi]=get(hc,hi,0)+1
    wc[wi]=get(wc,wi,0)+1
  end
  mh=first(findmax(hc))
  mw=first(findmax(wc))
  for i=keys(filter(x->last(x)==mh,hc))
    for j=keys(filter(x->last(x)==mw,wc))
      if !((i,j) in hw)
        println(mh+mw)
        return 0
      end
    end
  end
  println(mh+mw-1)
end
main()


function main()
  h,w,m=parse.(Int,split(readline()))
  hc=Dict()
  wc=Dict()
  hw=Tuple{Int,Int}[]
  for i=1:m
    hi,wi=parse.(Int,split(readline()))
    push!(hw,(hi,wi))
    hc[hi]=get(hc,hi,0)+1
    wc[wi]=get(wc,wi,0)+1
  end
  mh=findmax(hc)[1]
  mw=findmax(wc)[1]
  filter!(x->last(x)==mh,hc)
  filter!(x->last(x)==mw,wc)
  for i=keys(hc)
    for j=keys(wc)
      if !((i,j) in hw)
        println(mh+mw)
        return 0
      end
    end
  end
  println(mh+mw-1)
end
main()


if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
function main(io=stdin)
  readto(target = '\n') = readuntil(io, target)
  readnum(T::Type{<:Number} = Int; dlm = isspace, kwargs...) =
    parse.(T, split(readto(), dlm; kwargs...))::Vector{T}
  h,w,m=readnum()
  hc=zeros(Int,h)
  wc=zeros(Int,w)
  aru=Tuple{Int,Int}[]
  for i=1:m
    hi,wi=readnum()
    hc[hi]+=1
    wc[wi]+=1
    push!(aru,(hi,wi))
  end
  mh=maximum(hc)
  mhi=findall(isequal(mh),hc)
  mw=maximum(wc)
  mwi=findall(isequal(mw),wc)
  println(mh+mw-ifelse(all([(i,j) in aru for i=mhi, j=mwi]),1,0))
end
main()
if isdefined(Base, :mystdin);close(mystdin);end


function main()
  h,w,m=parse.(Int,split(readline()))
  hc=zeros(Int,h)
  wc=zeros(Int,w)
  aru=Tuple{Int,Int}[]
  for i=1:m
    hi,wi=parse.(Int,split(readline()))
    hc[hi]+=1
    wc[wi]+=1
    push!(aru,(hi,wi))
  end
  mh=maximum(hc)
  mhi=findall(isequal(mh),hc)
  mw=maximum(wc)
  mwi=findall(isequal(mw),wc)
  println(mh+mw-ifelse(all([(i,j) in aru for i=mhi, j=mwi]),1,0))
end
main()


function main()
  h,w,m=parse.(Int,split(readline()))
  ah=zeros(Int,h)
  aw=zeros(Int,w)
  aru=Array{Int,1}[]
  for i=1:m
    hi,wi=parse.(Int,split(readline()))
    ah[hi]+=1
    aw[wi]+=1
    push!(aru,[hi,wi])
  end
  mh=maximum(ah)
  mhi=findall(isequal(mh),ah)
  mw=maximum(aw)
  mwi=findall(isequal(mw),aw)
  println(mh+mw-ifelse(all([[i,j] in aru for i=mhi, j=mwi]),1,0))
end
main()


function main()
  h,w,m=parse.(Int,split(readline()))
  a=[Bool[false for i=1:w] for j=1:h]
  ah=zeros(Int,h)
  aw=zeros(Int,w)
  for i=1:m
    hi,wi=parse.(Int,split(readline()))
    a[hi][wi]=true
    ah[hi]+=1
    aw[wi]+=1
  end
  mh=maximum(ah)
  mhi=findall(isequal(mh),ah)
  mw=maximum(aw)
  mwi=findall(isequal(mw),aw)
  println(mh+mw-ifelse(all([a[i][j] for i=mhi, j=mwi]),1,0))
end
main()


if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
function main()
  h,w,m=parse.(Int,split(readline()))
  a=zeros(Int,h+1,w+1)
  for i=1:m
    hi,wi=parse.(Int,split(readline()))
    hi+=1;wi+=1
    a[hi,wi]=1
    a[1,wi]+=1
    a[hi,1]+=1
  end
  ah=a[:,1]
  mh=maximum(ah)
  mhi=Int[]; for i=2:h+1; if ah[i]==mh; push!(mhi,i);end;end
  aw=a[1,:]
  mw=maximum(aw)
  mwi=Int[]; for i=2:w+1; if aw[i]==mw; push!(mwi,i);end;end
  println(mh+mw-ifelse(all([a[i,j]==1 for i=mhi, j=mwi]),1,0))
end
main()
if isdefined(Base, :mystdin);close(mystdin);end


function main()
  h,w,m=parse.(Int,split(readline()))
  a=zeros(Int,h+1,w+1)
  for i=1:m
    hi,wi=parse.(Int,split(readline()))
    hi+=1;wi+=1
    a[hi,wi]=1
    a[1,wi]+=1
    a[hi,1]+=1
  end
  H=a[:,1]
  MH=maximum(H)
  mh=findall(H .== MH)
  W=a[1,:]
  MW=maximum(W)
  mw=findall(W .== MW)
  println(MH+MW-ifelse(all([a[i,j]==1 for i=mh for j=mw]),1,0))
end
main()


using SparseArrays

function parse_numbers(s)
    pieces = split(s, ' ', keepempty=false)
    map(pieces) do piece
        parse(Int64, piece)
    end
end

function find_max_blast(H, W, M, h, w)
    grid = sparse(append!([1, H], h), append!([1, W], w), append!([0, 0], repeat([1], M)))

    h_sum = vec(sum(grid, dims=2))
    w_sum = vec(sum(grid, dims=1))

    h_max = maximum(h_sum)
    h_sum = h_sum .== h_max
    h_ind = findall(x->x==1, h_sum)

    w_max = maximum(w_sum)
    w_sum = w_sum .== w_max
    w_ind = findall(x->x==1, w_sum)

    for i in h_ind
            for j in w_ind
                    if grid[i, j] == 0
                        return h_max + w_max
                    end
            end
    end
    return h_max + w_max - 1
end

txt = readlines();
H, W, M = parse_numbers(txt[1])
h = zeros(Int64, M)
w = zeros(Int64, M)

for i in 1:M
    h_tmp, w_tmp = parse_numbers(txt[i+1])
    global h[i] = h_tmp
    global w[i] = w_tmp
end

maxblast = find_max_blast(H, W, M, h, w)

println("$(maxblast)")


using SparseArrays

function parse_numbers(s)
    pieces = split(s, ' ', keepempty=false)
    map(pieces) do piece
        parse(Int64, piece)
    end
end

function find_max_blast(H, W, M, h, w)
    grid = sparse(append!([1, H], h), append!([1, W], w), append!([0, 0], repeat([1], M)))

    h_sum = vec(sum(grid, dims=2))
    w_sum = vec(sum(grid, dims=1))

    h_max = maximum(h_sum)
    h_sum = h_sum .== h_max

    w_max = maximum(w_sum)
    w_sum = w_sum .== w_max

    for i in 1:H
        if h_sum[i]
            for j in 1:W
                if w_sum[j]
                    if grid[i, j] == 0
                        return h_max + w_max
                    end
                end
            end
        end
    end
    return h_max + w_max - 1
end

txt = readlines();
H, W, M = parse_numbers(txt[1])
h = zeros(Int64, M)
w = zeros(Int64, M)

for i in 1:M
    h_tmp, w_tmp = parse_numbers(txt[i+1])
    global h[i] = h_tmp
    global w[i] = w_tmp
end

maxblast = find_max_blast(H, W, M, h, w)

println("$(maxblast)")


using SparseArrays

function parse_numbers(s)
    pieces = split(s, ' ', keepempty=false)
    map(pieces) do piece
        parse(Int64, piece)
    end
end

function find_max_blast(H, W, M, h, w)
    grid = sparse(append!([1, H], h), append!([1, W], w), append!([0, 0], repeat([1], M)))

    h_sum = vec(sum(grid, dims=2))
    w_sum = vec(sum(grid, dims=1))

    for i in 1:H
        if h_sum[i] == maximum(h_sum)
            for j in 1:W
                if w_sum[j] == maximum(w_sum)
                    if grid[i, j] == 0
                        return maximum(h_sum) + maximum(w_sum)
                    end
                end
            end
        end
    end
    return maximum(h_sum) + maximum(w_sum) - 1
end

txt = readlines();
H, W, M = parse_numbers(txt[1])
h = zeros(Int64, M)
w = zeros(Int64, M)

for i in 1:M
    h_tmp, w_tmp = parse_numbers(txt[i+1])
    global h[i] = h_tmp
    global w[i] = w_tmp
end

maxblast = find_max_blast(H, W, M, h, w)

println("$(maxblast)")


using SparseArrays

function parse_numbers(s)
    pieces = split(s, ' ', keepempty=false)
    map(pieces) do piece
        parse(Int64, piece)
    end
end

function find_max_blast(H, W, M, h, w)
    grid = sparse(append!([1, H], h), append!([1, W], w), append!([0, 0], repeat([1], M)))

    h_sum = vec(sum(grid, dims=2))
    w_sum = vec(sum(grid, dims=1))

    for i in 1:H
        if h_sum[i] == maximum(h_sum)
            for j in 1:W
                # if w_sum[j] == maximum(w_sum)
                    if grid[i, j] == 0
                        return maximum(h_sum) + maximum(w_sum)
                    end
                # end
            end
        end
    end

    return maximum(h_sum) + maximum(w_sum) - 1
end

txt = readlines();
H, W, M = parse_numbers(txt[1])
h = zeros(Int64, M)
w = zeros(Int64, M)

for i in 1:M
    h_tmp, w_tmp = parse_numbers(txt[i+1])
    global h[i] = h_tmp
    global w[i] = w_tmp
end

maxblast = find_max_blast(H, W, M, h, w)

println("$(maxblast)")


function parse_numbers(s)
    pieces = split(s, ' ', keepempty=false)
    map(pieces) do piece
        parse(Int64, piece)
    end
end

txt = readlines();
H, W, M = parse_numbers(txt[1])
h = zeros(Int64, M)
w = zeros(Int64, M)
# @time w = []
for i in 1:M
    h_tmp, w_tmp = parse_numbers(txt[i+1])
    global h[i] = h_tmp
    global w[i] = w_tmp
    # @time append!(w, w_tmp)
end

grid = zeros(Int64, H,  W)

for i in 1:M
    grid[h[i], w[i]] = 1
end

h_sum = vec(sum(grid, dims=2))

w_sum = vec(sum(grid, dims=1))

flag = true
max_blast = maximum(h_sum) + maximum(w_sum) - 1

for i in 1:H
    for j in 1:W
    blast = h_sum[i] + w_sum[j] - grid[i, j]
        if max_blast < blast
            global max_blast = blast
            break
        end
    end
end

println(max_blast)


function parse_numbers(s)
    pieces = split(s, ' ', keepempty=false)
    map(pieces) do piece
        parse(Int64, piece)
    end
end

txt = readlines();
H, W, M = parse_numbers(txt[1])
h = zeros(Int64, M)
w = zeros(Int64, M)
# @time w = []
for i in 1:M
    h_tmp, w_tmp = parse_numbers(txt[i+1])
    global h[i] = h_tmp
    global w[i] = w_tmp
    # @time append!(w, w_tmp)
end

grid = zeros(Int64, H,  W)

for i in 1:M
    grid[h[i], w[i]] = 1
end

h_sum = vec(sum(grid, dims=2))
h_ind = findall(x->x==maximum(h_sum), h_sum)

w_sum = vec(sum(grid, dims=1))
w_ind = findall(x->x==maximum(w_sum), w_sum)

flag = true
for i in h_ind
    for j in w_ind
        if grid[i, j] == 0
            println(maximum(h_sum) + maximum(w_sum))
            global flag = false
            break
        end
    end
end

if flag
    println(maximum(h_sum) + maximum(w_sum) - 1)
end


function parse_numbers(s)
    pieces = split(s, ' ', keepempty=false)
    map(pieces) do piece
        parse(Int64, piece)
    end
end

txt = readlines();
H, W, M = parse_numbers(txt[1])
h = zeros(Int64, M)
w = zeros(Int64, M)
# @time w = []
for i in 1:M
    h_tmp, w_tmp = parse_numbers(txt[i+1])
    global h[i] = h_tmp
    global w[i] = w_tmp
    # @time append!(w, w_tmp)
end

grid = zeros(Int64, H,  W)

for i in 1:M
    grid[h[i], w[i]] = 1
end

h_sum = vec(sum(grid, dims=2))
h_ind = findall(x->x==maximum(h_sum), h_sum)

w_sum = vec(sum(grid, dims=1))
w_ind = findall(x->x==maximum(w_sum), w_sum)

flag = true
for i in h_ind
    for j in w_ind
        if grid[i, j] == 0
            println(maximum(h_sum) + maximum(w_sum))
            flag = false
        end
    end
end

if flag
    println(maximum(h_sum) + maximum(w_sum) - 1)
end


function main()
    H,W,M = reads(Int,Int,Int)
    hc = zeros(Int, H)
    wc = zeros(Int, W)
    hwset = Set(Tuple{Int,Int}[])
    for i in 1:M
        hi,wi = reads(Int,Int)
        push!(hwset, (hi,wi))
        hc[hi] += 1
        wc[wi] += 1
    end
    mhi = [1]
    mhv = hc[1]
    for i in 2:H
        if mhv < hc[i]
            mhi = [i]
            mhv = hc[i]
        elseif mhv == hc[i]
            push!(mhi, i)
        end
    end
    mwi = [1]
    mwv = wc[1]
    for i in 2:W
        if mwv < wc[i]
            mwi = [i]
            mwv = wc[i]
        elseif mwv == wc[i]
            push!(mwi, i)
        end
    end
    for i in mhi, j in mwi
        if !((i,j) in hwset)
            println(mhv+mwv)
            return
        end
    end
    println(mhv+mwv-1)
end

import Base.parse
import Base.StringVector

parse(::Type{String},  str::AbstractString) = str
isdelim(x::UInt8, xs::Set{UInt8}) = x in xs
const delimset = Set([0x0a, 0x20])

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

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)

function readvec(tys::Tuple , len::Signed)::Tuple{map(x -> Vector{x},tys)...}
    rv = Tuple{map(x -> Vector{x},tys)...}(Vector{ty}(undef,len) for ty in tys)
    for i in 1:len
        for j in 1:length(tys)
            @inbounds rv[j][i] = pread(tys[j])
        end
    end
    rv
end

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 in 1:s[1]
        v[i,:] = readvec(ty, s[2])
    end
    v
end

main()


function main()
    H,W,M = reads(Int,Int,Int)
    hc = zeros(Int, H)
    wc = zeros(Int, W)
    hwset = Set(Tuple{Int,Int}[])
    for i in 1:M
        hi,wi = reads(Int,Int)
        push!(hwset, (hi,wi))
        hc[hi] += 1
        wc[wi] += 1
    end
    mhi = [1]
    mhv = hc[1]
    for i in 2:H
        if mhv < hc[i]
            mhi = i
            mhv = hc[i]
        elseif mhv == hc[i]
            push!(mhi, i)
        end
    end
    mwi = [1]
    mwv = wc[1]
    for i in 2:W
        if mwv < wc[i]
            mwi = [i]
            mwv = wc[i]
        elseif mwv == wc[i]
            push!(mwi, i)
        end
    end
    for i in mhi, j in mwi
        if !((i,j) in hwset)
            println(mhv+mwv)
            return
        end
    end
    println(mhv+mwv-1)
end

import Base.parse
import Base.StringVector

parse(::Type{String},  str::AbstractString) = str
isdelim(x::UInt8, xs::Set{UInt8}) = x in xs
const delimset = Set([0x0a, 0x20])

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

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)

function readvec(tys::Tuple , len::Signed)::Tuple{map(x -> Vector{x},tys)...}
    rv = Tuple{map(x -> Vector{x},tys)...}(Vector{ty}(undef,len) for ty in tys)
    for i in 1:len
        for j in 1:length(tys)
            @inbounds rv[j][i] = pread(tys[j])
        end
    end
    rv
end

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 in 1:s[1]
        v[i,:] = readvec(ty, s[2])
    end
    v
end

main()


function uryu(M, H, W)
    listH = [0 for _ in 1:H]
    listW = [0 for _ in 1:W]
    obj_positions = []
    for i in 1:M
        hi, wi = [parse(Int, x) for x in split(readline())]
        listH[hi] += 1
        listW[wi] += 1
        push!(obj_positions,(hi,wi))
    end
    return obj_positions, listH, listW
end

function find_max(listA)
    indexes = []
    max_value = 0
    for (i,a) in enumerate(listA)
        if max_value < a
            indexes = [i]
            max_value = a
        elseif max_value == a
            push!(indexes, i)
        end
    end
    return indexes
end

#一つでもobjがないマスがあればtrue
function uryu3(maxH_indexes, maxW_indexes, obj_positions)
    #println(maxH_indexes)
    #println(maxW_indexes)
    #println(obj_positions)
    n = length(maxH_indexes) * length(maxW_indexes)
    count = 0
    for position in obj_positions
        #println(position)
        if position[1] in maxH_indexes && position[2] in maxW_indexes
            #println("true")
            count += 1
        end
    end
    return n - count > 0
end

function main()
    #println("a")
    H, W, M = [parse(Int, x) for x in split(readline())]

    obj_positions, listH, listW = uryu(M, H, W)

    maxH_indexes = find_max(listH)
    maxW_indexes = find_max(listW)

    flag = uryu3(maxH_indexes, maxW_indexes, obj_positions)
    pre_res = listH[maxH_indexes[1]] + listW[maxW_indexes[1]]
    if flag
        println(pre_res)
    else
        println(pre_res-1)
    end
end

main()


function uryu(M, H, W)
    listH = [0 for _ in 1:H]
    listW = [0 for _ in 1:W]
    obj_positions = []
    for i in 1:M
        hi, wi = [parse(Int, x) for x in split(readline())]
        listH[hi] += 1
        listW[wi] += 1
        push!(obj_positions,(hi,wi))
    end
    return obj_positions, listH, listW
end

function find_max(listA)
    indexes = []
    max_value = 0
    for (i,a) in enumerate(listA)
        if max_value < a
            indexes = [i]
            max_value = a
        elseif max_value == a
            push!(indexes, i)
        end
    end
    return indexes
end

#一つでもobjがないマスがあればtrue
function uryu2(maxH_indexes, maxW_indexes, obj_positions)
    flag = false
    for i in maxH_indexes
        for j in maxW_indexes
            if !((i,j) in obj_positions)
                flag = true
            end
        end
    end
    return flag
end

function main()
    H, W, M = [parse(Int, x) for x in split(readline())]

    obj_positions, listH, listW = uryu(M, H, W)

    maxH_indexes = find_max(listH)
    maxW_indexes = find_max(listW)

    flag = uryu2(maxH_indexes, maxW_indexes, obj_positions)
    pre_res = listH[maxH_indexes[1]] + listW[maxW_indexes[1]]
    if flag
        println(pre_res)
    else
        println(pre_res-1)
    end
end

main()


H, W, M = [parse(Int, x) for x in split(readline())]

listH = [0 for _ in 1:H]
listW = [0 for _ in 1:W]

function uryu(M)
    global listH
    global listW
    obj_positions = []
    for i in 1:M
        hi, wi = [parse(Int, x) for x in split(readline())]
        listH[hi] += 1
        listW[wi] += 1
        push!(obj_positions,(hi,wi))
    end
    return obj_positions
end

obj_positions = uryu(M)

function find_max(listA)
    indexes = []
    max_value = 0
    for (i,a) in enumerate(listA)
        if max_value < a
            indexes = [i]
            max_value = a
        elseif max_value == a
            push!(indexes, i)
        end
    end
    return indexes
end

maxH_indexes = find_max(listH)
maxW_indexes = find_max(listW)

#一つでもobjがないマスがあればtrue

function uryu2(maxH_indexes, maxW_indexes, obj_positions)
    flag = false
    for i in maxH_indexes
        for j in maxW_indexes
            if !((i,j) in obj_positions)
                flag = true
            end
        end
    end
    return flag
end

flag = uryu2(maxH_indexes, maxW_indexes, obj_positions)
pre_res = listH[maxH_indexes[1]] + listW[maxW_indexes[1]]
if flag
    println(pre_res)
else
    println(pre_res-1)
end


parseInt(x) = parse(Int128, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
  H,W,M = parseMap(split(readline()))
  h = zeros(Int128,M) ; w = zeros(Int128,M)
  #a = Set([W*(i-1)+j for i=1:H,j=1:W]) これが大きすぎた
  a=Set()
  dict = Dict{Int128,Int128}()
  gyou = zeros(H) ; retu = zeros(W)
  for i in 1:M
    h[i],w[i] = parseMap(split(readline()))
    gyou[h[i]] += 1
    retu[w[i]] += 1
    #pop!(a,W*(h[i]-1)+w[i]) # a becomes without bomb array
    push!(a,W*(h[i]-1)+w[i]) # bomb array
  end

  #for i in 1:H # ここは0(M)以下ののループにできるはず。
    #for j in 1:W
  if maximum(gyou)==1 && maximum(retu)==1
    println(1)
  else
    for i in findall(gyou .== maximum(gyou))
      for j in findall(retu .== maximum(retu))
        if !in(W*(i-1)+j,a) # No Bomb area
          push!(dict,W*(i-1)+j=>gyou[i]+retu[j])
        else # Bomb area
          push!(dict,W*(i-1)+j=>gyou[i]+retu[j]-1)
        end
      end
    end
    #println(dict)
    println(maximum(values(dict)))
  end
end
main()


parseInt(x) = parse(Int128, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
  H,W,M = parseMap(split(readline()))
  h = zeros(Int128,M) ; w = zeros(Int128,M)
  #a = Set([W*(i-1)+j for i=1:H,j=1:W]) これが大きすぎた
  a=Set()
  dict = Dict{Int128,Int128}()
  gyou = zeros(H) ; retu = zeros(W)
  for i in 1:M
    h[i],w[i] = parseMap(split(readline()))
    gyou[h[i]] += 1
    retu[w[i]] += 1
    #pop!(a,W*(h[i]-1)+w[i]) # a becomes without bomb array
    push!(a,W*(h[i]-1)+w[i]) # bomb array
  end

  #for i in 1:H # ここは0(M)以下ののループにできるはず。
    #for j in 1:W
  for i in findall(gyou .== maximum(gyou))
    for j in findall(retu .== maximum(retu))
      if !in(W*(i-1)+j,a) # No Bomb area
        push!(dict,W*(i-1)+j=>gyou[i]+retu[j])
      else # Bomb area
        push!(dict,W*(i-1)+j=>gyou[i]+retu[j]-1)
      end
    end
  end
  #println(dict)
  println(maximum(values(dict)))
end
main()


# %% constants & libraries
# ------------------------

# %% body
# -------

@inbounds function main(io = stdin)
    readto(target = '\n') = readuntil(io, target)
    readnum(T::Type{<:Number} = Int; dlm = isspace, kwargs...) =
        parse.(T, split(readto(), dlm; kwargs...))::Vector{T}

    # handle IO and solve
    H, W, M = readnum()
    G = Set{Tuple{Int,Int}}()
    Rcnt, Ccnt = Dict{Int,Int}(), Dict{Int,Int}()
    for h in 1:M
        h, w = readnum()
        push!(G, (h, w))
        Rcnt[h] = get(Rcnt, h, 0) + 1
        Ccnt[w] = get(Ccnt, w, 0) + 1
    end

    maxr = first(findmax(Rcnt))
    maxc = first(findmax(Ccnt))
    ret = maxr + maxc

    rs = filter(Rcnt) do (k,v)
        v == maxr
    end |> keys |> collect
    cs = filter(Ccnt) do (k,v)
        v == maxc
    end |> keys |> collect

    for r in rs
        for c in cs
            (r, c) ∉ G && return println(ret)
        end
    end
    return println(ret - 1)
end

@static if @isdefined(Juno) || @isdefined(VSCodeServer)
    main(open(replace(@__FILE__, r"(.+)\.jl" => s"\1.in")))
else
    main()
end


# %% constants & libraries
# ------------------------

# %% body
# -------

@inbounds function main(io = stdin)
    readto(target = '\n') = readuntil(io, target)
    readnum(T::Type{<:Number} = Int; dlm = isspace, kwargs...) =
        parse.(T, split(readto(), dlm; kwargs...))::Vector{T}

    # handle IO and solve
    H, W, M = readnum()
    G = zeros(Int, (H, W))
    Rcnt, Ccnt = Dict{Int,Int}(), Dict{Int,Int}()
    for h in 1:M
        h, w = readnum()
        G[h, w] = 1
        Rcnt[h] = get(Rcnt, h, 0) + 1
        Ccnt[w] = get(Ccnt, w, 0) + 1
    end

    maxr = first(findmax(Rcnt))
    maxc = first(findmax(Ccnt))
    ret = maxr + maxc

    rs = filter(Rcnt) do (k,v)
        v == maxr
    end |> keys |> collect
    cs = filter(Ccnt) do (k,v)
        v == maxc
    end |> keys |> collect

    for r in rs
        for c in cs
            G[r, c] == 0 && return println(ret)
        end
    end
    return println(ret - 1)
end

@static if @isdefined(Juno) || @isdefined(VSCodeServer)
    main(open(replace(@__FILE__, r"(.+)\.jl" => s"\1.in")))
else
    main()
end


# %% constants & libraries
# ------------------------

# %% body
# -------

@inbounds function main(io = stdin)
    readto(target = '\n') = readuntil(io, target)
    readnum(T::Type{<:Number} = Int; dlm = isspace, kwargs...) =
        parse.(T, split(readto(), dlm; kwargs...))::Vector{T}

    # handle IO and solve
    H, W, M = readnum()
    G = zeros(Int, (H, W))
    Rcnt, Ccnt = Dict{Int,Int}(), Dict{Int,Int}()
    for h in 1:M
        h, w = readnum()
        G[h, w] = 1
        Rcnt[h] = get(Rcnt, h, 0) + 1
        Ccnt[w] = get(Ccnt, w, 0) + 1
    end

    maxr = first(findmax(Rcnt))
    maxc = first(findmax(Ccnt))
    ret = maxr + maxc

    rs = filter(Rcnt) do (k,v)
        v == maxr
    end |> keys |> collect
    cs = filter(Ccnt) do (k,v)
        v == maxc
    end |> keys |> collect

    for r in rs
        for c in cs
            if G[r, c] == 0
                @show G Rcnt Ccnt
                return println(ret)
            end
        end
    end
    println(ret - 1)
end

@static if @isdefined(Juno) || @isdefined(VSCodeServer)
    main(open(replace(@__FILE__, r"(.+)\.jl" => s"\1.in")))
else
    main()
end


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
  H,W,M = parseMap(split(readline()))
  h = zeros(Int,M) ; w = zeros(Int,M)
  a = Set([W*(i-1)+j for i=1:H,j=1:W])
  dict = Dict{Int,Int}()
  gyou = zeros(H) ; retu = zeros(W)
  for i in 1:M
    h[i],w[i] = parseMap(split(readline()))
    gyou[h[i]] += 1
    retu[w[i]] += 1
    pop!(a,W*(h[i]-1)+w[i]) # a becomes without bomb array
  end

  for i in 1:H
    for j in 1:W
      if in(W*(i-1)+j,a) # No Bomb area
        push!(dict,W*(i-1)+j=>gyou[i]+retu[j])
      else # Bomb area, so ダブりあり
        push!(dict,W*(i-1)+j=>gyou[i]+retu[j]-1)
      end
    end
  end
  #println(dict)
  println(maximum(values(dict)))
end
main()


function main()
  n = parse(Int, readline())
  res = n % 1000
  if res != 0 return 1000 - res end
  res
end

println(main())

Score : 100 points

Problem Statement

We will buy a product for N yen (the currency of Japan) at a shop.

If we use only 1000-yen bills to pay the price, how much change will we receive?

Assume we use the minimum number of bills required.

Constraints

1 \leq N \leq 10000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the amount of change as an integer.

Sample Input 1

1900

Sample Output 1

100

We will use two 1000-yen bills to pay the price and receive 100 yen in change.

Sample Input 2

3000

Sample Output 2

0

We can pay the exact price.


# SPDX-License-Identifier: X11
# 2020-07-30

const BILL = 1000
N = parse(Int,readline())

N % BILL == 0 ? println(0) : println(BILL - N % BILL)


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


function main()
    input = parse(Int64, readline())
    output = 1000 - (input % 1000)
    if output == 1000
        output = 0
    end
    println(output)
end

main()


n = parse(Int,readline())
println(1000-n%1000)


function main()

  N = parse(Int, readline())

  if N % 1000 == 0
  	println(0)
  else
    println(1000 - N % 1000)
  end

end

main()


function main()

  N = parse(Int, readline())

  println(1000 - N % 1000)

end

main()


function main()

  N = parse(Int, readline())

  println(N % 1000)

end

main()


function main()
    n = parse(Int, readline())
    p = 0
    while p*1000 < n
        p += 1
    end
    println(1000*p-n)

end

main()


n=parse(Int,readline())
while n>0
    global n -= 1000
end
println(-n)


readint()=parse.(Int,readline())
N=readint()
nokori=N%1000
if nokori==0
    print(0)
else
    print(1000-nokori)
end


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
  a = parseInt(readline())
  #N,K = parseMap(split(readline()))
  #p = parseMap(split(readline()))
  #println("$N,$K")
  #println("$p")
  #println("$p")
  if mod(a,1000) != 0
    println(1000-mod(a,1000))
  else
    println(0)
  end
end
main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
  a = parseInt(readline())
  println(1000-mod(a,1000))
end
main()


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

function main()
	d = readline() |> parseInt
	for i in 1:d
		println(1)
	end
end

main()

Problem Statement

AtCoder currently hosts three types of contests: ABC, ARC, and AGC. As the number of users has grown, in order to meet the needs of more users, AtCoder has decided to increase the number of contests to 26 types, from AAC to AZC. For convenience, we number these 26 types as type 1 through type 26. AtCoder wants to schedule contests for D days so that user satisfaction is as high as possible. For every day, AtCoder will hold exactly one contest, and each contest will end on that day. The satisfaction is calculated as follows.

The satisfaction at the beginning of day 1 is 0. Satisfaction can be negative.

Holding contests increases satisfaction. The amount of increase will vary depending on a variety of factors. Specifically, we know in advance that holding a contest of type i on day d will increase the satisfaction by s_{d,i}.

If a particular type of contest is not held for a while, the satisfaction decreases. Each contest type i has an integer c_i, and at the end of each day d=1,2,...,D, the satisfaction decreases as follows. Let \mathrm{last}(d,i) be the last day before day d (including d) on which a contest of type i was held. If contests of type i have never been held yet, we define \mathrm{last}(d,i)=0. At the end of day d, the satisfaction decreases by \sum _{i=1}^{26}c_i \times (d-\mathrm{last}(d,i)).

Please schedule contests on behalf of AtCoder.
If the satisfaction at the end of day D is S, you will get a score of \max(10^6 + S, 0).
There are 50 test cases, and the score of a submission is the total scores for each test case.
You can make submissions multiple times, and the highest score among your submissions will be your score.

Constraints

D = 365

Each c_i is an integer satisfying 0\leq c_i \leq 100.

Each s_{d,i} is an integer satisfying 0\leq s_{d,i} \leq 20000.

Input

Input is given from Standard Input in the following format:

D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}

Output

Let t_d (1\leq t_d \leq 26) be the type of the contest that will be held at day d.
Print D integers t_d to Standard Output in the following format:

t_1
t_2
\vdots
t_D

Any output that does not follow the above format may result in 0 pointsWA for that test case.

Input Generation

Each integer c_i and s_{d,i} is generated independently and uniformly at random from the integers in the range described in the problem statement.

Sample Input 1

5
86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82
19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424
6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570
6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256
8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452
19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192

Sample Output 1

1
17
13
14
13

Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case. The final satisfaction with this output is 79325, so the score is 1079325.

Input generator, score calculator, and visualizer

Beginner's Guide

If you don't know what to do, proceed to problem B or C.


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

  A = parse.(Int, split(readline()))
  B = parse.(Int, split(readline()))

  A_ = copy(A)
  B_ = copy(B)

  ans = 0

  for i in 2:n
    A_[i] = A_[i-1] + A_[i]
  end

  for i in 2:m
    B_[i] = B_[i-1] + B_[i]
  end

  i = n
  j = 1

  if A[n] < k
    ans = n
  end
  if B[m] < k
    ans = max(ans, m)
  end

  while i > 0
    while j < m
      if A_[i] + B_[j+1] <= k
        j += 1
      else
        break
      end
    end

    if A_[i] + B_[j] <= k
      ans = max(ans, i + j)
    end

    i -= 1

  end

  println(ans)

end

main()

Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.


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

  A = parse.(Int, split(readline()))
  B = parse.(Int, split(readline()))

  A_ = copy(A)
  B_ = copy(B)

  ans = 0

  for i in 1:n
    if i == 1
      A_[i] = A[i]
    else
      A_[i] = A_[i-1] + A_[i]
    end
  end

  for i in 1:m
    if i == 1
      B_[i] = B[i]
    else
      B_[i] = B_[i-1] + B_[i]
    end
  end

  i = n
  j = 1

  while i > 0
    while j < m
      if A_[i] + B_[j+1] <= k
        j += 1
      else
        break
      end
    end

    if A_[i] + B_[j] <= k
      ans = max(ans, i + j)
    end

    i -= 1

  end

  println(ans)

end

main()


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

  A = parse.(Int, split(readline()))
  B = parse.(Int, split(readline()))

  sum = 0
  na = 1
  nb = 1

  for i in 1:n
    sum += A[i]
  end

  na = n + 1
  ans = 0

  while na > 1 && nb <= m

    while nb <= m && sum + B[nb] <= k
      sum += B[nb]
      nb += 1
    end

    if sum <= k
      ans = max(ans, na + nb - 2)
    end

    sum -= A[na-1]
    na -= 1

  end

  print(ans)

end

main()


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

  A = parse.(Int, split(readline()))
  B = parse.(Int, split(readline()))

  sum = 0
  na = 1
  nb = 1

  for i in 1:n
    sum += A[i]
  end

  na = n + 1
  ans = 0

  while na > 1 && nb <= m

    while sum + B[nb] <= k
      sum += B[nb]
      nb += 1

      if nb > m
        break
      end
    end

    if sum <= k
      ans = max(ans, na + nb - 2)
    end

    sum -= A[na-1]
    na -= 1

  end

  print(ans)

end

main()


function main()
    n, m, k = parse.(Int, split(readline()))
    As = parse.(Int, split(readline()))
    Bs = parse.(Int, split(readline()))

    A_sum = sum(As)
    ans = 0
    j = 0
    time = A_sum
    for i in n:-1:0
        while j < m && time + Bs[j+1] <= k
            j += 1
      		time += Bs[j]
        end
      	if time <= k
        	ans = max(ans, i+j)
        end
    	if i > 0
    		time -= As[i]
    	end
    end

    println(ans)

end

main()


function main()
    n, m, k = parse.(Int, split(readline()))
    As = parse.(Int, split(readline()))
    Bs = parse.(Int, split(readline()))

    A_sum = sum(As)
    ans = 0
    j = 0
    time = A_sum
    for i in n:-1:1
        while j < m && time + Bs[j+1] <= k
            j += 1
      		time += Bs[j]
        end
      	if time <= k
        	ans = max(ans, i+j)
        end
    	time -= As[i]
    end

    println(ans)

end

main()


function main()
    n, m, k = map(x -> parse(Int, x), split(readline()))
    As = map(x -> parse(Int, x), split(readline()))
    Bs = map(x -> parse(Int, x), split(readline()))

    ans = 0
    books = 0
    time_sum = 0

    A_sums = []
    for i in 1:n
        push!(A_sums, sum(As[1:i]))
    end

    B_sums = []
    for i in 1:m
        push!(B_sums, sum(Bs[1:i]))
    end

    for i in 0:n
        time_sum = 0
        if i > 0
            time_sum += A_sums[i]
        end
        if time_sum <= k
            for j in m:-1:1
                time_sum += Bs[j]
                if time_sum <= k
                    m = j
                    books = i+j
                    break
                end
            end
        else
            books = 0
        end
        ans = max(ans, books)
    end

    println(ans)

end

main()


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

  t = sum(b)
  j = m
  ans = 0
  for i in 0:n
    while j > 0 && t > k
      j -= 1
      t -= b[j+1]
    end
    t > k && break
    ans = max(ans, i+j)
    i == n && break
    t += a[i+1]
  end
  ans
end

println(main())


input = parse.(Int, split(readline()))
n = input[1]
m = input[2]
k = input[3]
a = parse.(Int, split(readline()))
b = parse.(Int, split(readline()))

counter = 0

while k > 0
  next_book_a = nothing

  if length(a) > 0
    next_book_a = a[1]
  end

  next_book_b = nothing

  if length(b) > 0
    next_book_b = b[1]
  end

  if isnothing(next_book_a)
    next_read_book = next_book_b
    if k < next_read_book
      global k = 0
      continue
    end
    popfirst!(b)
    global k = k - next_read_book
    global counter += 1
    continue
  end

  if isnothing(next_book_b)
    next_read_book = next_book_a
    if k < next_read_book
      global k = 0
      continue
    end
    popfirst!(a)
    global k = k - next_read_book
    global counter += 1
    continue
  end

  if next_book_a < next_book_b
    if k < next_book_a
      global k = 0
      continue
    end
    popfirst!(a)
    global k = k - next_book_a
    global counter += 1
  else
    if k < next_book_b
      global k = 0
      continue
    end
    popfirst!(b)
    global k = k - next_book_b
    global counter += 1
  end
end

println(counter)


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(a)  (a).begin(),(a).end()

template <typename T>
void cout_vector(vector<T> A){
    for (T a : A) cout << a << " ";
cout << endl;
}

int main(){
    int N, M, K;
    cin >> N >> M >> K;
    vector<int> A(N);
    vector<int> B(M);
    rep(i, N) cin >> A[i];
    rep(j, M) cin >> B[j];
    vector<int> C;
    vector<int> D;
    C.push_back(0);
    D.push_back(0);

    for (int i = 1; i <= N; i++){
        if (C[i - 1] + A[i - 1] > K) break;
        C.push_back(C[i - 1] + A[i - 1]);
    }

    for (int j = 1; j <= M; j++){
        if (D[j - 1] + B[j - 1] > K) break;
        D.push_back(D[j - 1] + B[j - 1]);
    }

    int max_ans = -1;
    for (int i = 0; i < C.size() ; i++){
        int d = K - C[i];
        int left = -1;
        int right = D.size();
        while (abs(right - left) > 1){
            int mid = (left + right) / 2;
            if (D[mid] > d) right = mid;
            else left = mid;
        }
        int ans = i + left;
        max_ans = max(max_ans, ans);
    }
    cout << max_ans << endl;
}


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

function main()
    n,m,k=parseMap(split(readline()))
    a=parseMap(split(readline()))
    b=parseMap(split(readline()))
    bsum=[0 for i in 1:m]
    bsum[1]=b[1]
    for i in 2:m
        bsum[i]=bsum[i-1]+b[i]
    end
    s=0
    ans=0
    for i in 0:n
        if i>0
            s+=a[i]
        end
        if s>k
            break
        end
        tmp=i
        index=searchsortedlast(bsum,k-s)
        tmp+=index
        ans=max(ans,tmp)
    end
    println(ans)
end
main()


function main()
  N,M,K=map(x->parse(Int,x),split(readline()))
  A=map(x->parse(Int,x),split(readline()))
  B=map(x->parse(Int,x),split(readline()))
  S=sum(A)
  ans=0
  j=0
  for i=N:-1:0
    while j<M&&S+B[j+1]<=K
      j+=1
      S+=B[j]
    end
    if S<=K
      ans=max(ans,i+j)
    end
    if i>0
      S-=A[i]
    end
  end
  println(ans)
end
main()


function main()
  N,M,K=map(x->parse(Int,x),split(readline()))
  A=map(x->parse(Int,x),split(readline()))
  B=map(x->parse(Int,x),split(readline()))
  S=sum(A)
  ans=0
  j=0
  for i=N:-1:0
    while j<N&&S+B[j+1]<=K
      j+=1
      S+=B[j]
    end
    if S<=K
      ans=max(ans,i+j)
    end
    if i>0
      S-=A[i]
    end
  end
  println(ans)
end
main()


parseInt(x)=parse(Int, x)
parseMap(x::Array{SubString{String},1})=map(parseInt, x)
function bsearch(a,x);l,r=0,length(a)+1;while r-l>1;m=(r+l)>>1;if a[m]<=x;l=m;else;r=m;end;end;l;end
function main()
	n,m,k = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	b = readline() |> split |> parseMap
	for i in 2:n
		a[i]+=a[i-1]
	end
	for i in 2:m
		b[i]+=b[i-1]
	end
	s = bsearch(b,k)
	for i in 1:n
		if a[i]<=k
			s = max(s,i+bsearch(b,k-a[i]))
		end
	end
	println(s)
end

main()


parseInt(x)=parse(Int, x)
parseMap(x::Array{SubString{String},1})=map(parseInt, x)
function bsearch(a,x);l,r=0,length(a)+1;while r-l>1;m=(r+l)>>1;if a[m]<=x;l=m;else;r=m;end;end;l;end
function main()
	n,m,k = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	b = readline() |> split |> parseMap
	for i in 2:n
		a[i]+=a[i-1]
	end
	for i in 2:m
		b[i]+=b[i-1]
	end
	s = 0
	for i in 1:n
		if a[i]<=k
			s = max(s,i+bsearch(b,k-a[i]))
		end
	end
	println(s)
end

main()


parseInt(x)=parse(Int, x)
parseMap(x::Array{SubString{String},1})=map(parseInt, x)
function bsearch(a,x);l,r=0,length(a)+1;while r-l>1;m=(r+l)>>1;if a[m]<=x;l=m;else;r=m;end;end;l;end
function main()
	n,m,k = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	b = readline() |> split |> parseMap
	for i in 2:n
		a[i]+=a[i-1]
		b[i]+=b[i-1]
	end
	s = 0
	for i in 1:n
		if a[i]<=k
			s = max(s,i+bsearch(b,k-a[i]))
		end
	end
	println(s)
end

main()


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

function main()
	n,m,k = readline() |> split |> parseMap
	a = readline() |> split |> parseMap
	b = readline() |> split |> parseMap
	nc = 0
	nt = 0
	na = 1
	nb = 1
	while nt<k&&(na<=n||nb<=m)
		if na>n
			if nt+b[nb]>k
				break
			else
				nc += 1
				nt += b[nb]
				nb += 1
			end
		elseif nb>m
			if nt+a[na]>k
				break
			else
				nc += 1
				nt += a[na]
				na += 1
			end
		else
			if a[na]<b[nb]
				if nt+a[na]>k
					break
				else
					nc += 1
					nt += a[na]
					na += 1
				end
			else
				if nt+b[nb]>k
					break
				else
					nc += 1
					nt += b[nb]
					nb += 1
				end
			end
		end
	end
	println(nc)
end

main()


function ABC_C()
    aa=IOBuffer(IJulia.In[1])
    #readints()=parse.(Int,split(readline()))
    readints()=parse.(Int,split(readline(aa)))

    N,M,K=readints()
    A=readints()
    B=readints()
    Asum=zeros(Int,N)
    Bsum=zeros(Int,M)
    Asum[1]=A[1]
    for i in 2:N
        Asum[i]=Asum[i-1]+A[i]
    end
    Bsum[1]=B[1]
    for i in 2:M
        Bsum[i]=Bsum[i-1]+B[i]
    end

    Nmax=N+M
    kazu=0

    AA(n)= n==0 ? 0 : Asum[n]
    BB(n)= n==0 ? 0 : Bsum[n]

    Mmin=M
    for i in 0:N
        for j in Mmin:-1:0
            if AA(i)+BB(j)<=K
                kazu < i+j  && (kazu= i+j)
                Mmin=j
                break
            end
        end
    end

    print(kazu)
end
ABC_C()


readints()=parse.(Int,split(readline()))
function ABC_C()
N,M,K=readints()
A=readints()
B=readints()

Asum=zeros(Int,N)
Bsum=zeros(Int,M)
Asum[1]=A[1]
for i in 2:N
    Asum[i]=Asum[i-1]+A[i]
end
Bsum[1]=B[1]
for i in 2:M
    Bsum[i]=Bsum[i-1]+B[i]
end

Nmax=N+M
kazu=0

AA(n)= n==0 ? 0 : Asum[n]
BB(n)= n==0 ? 0 : Bsum[n]

for i in 0:N
    for j in 0:M
        if AA(i)+BB(j)<=K
            kazu < i+j  && (kazu= i+j)
        else
            break
        end
    end
end

print(kazu)
end
ABC_C()


function ABC_C()
readints()=parse.(Int,split(readline()))

N,M,K=readints()
A=readints()
B=readints()

Asum=zeros(Int,N+1)
Bsum=zeros(Int,M+1)
for i in 2:(N+1)
    Asum[i]=Asum[i-1]+A[i-1]
end
for i in 2:(M+1)
    Bsum[i]=Bsum[i-1]+B[i-1]
end

Nmax=N+M
kazu=0
if Asum[end]+Bsum[end]<=K
    kazu=Nmax
elseif A[1]>K && B[1]>K
    kazu=0
else
    for mkazu in 1:Nmax
        for mn in 0:mkazu
            n=N-mn+1
            m=M-(mkazu-mn)+1
            (n>N+1 || m>M+1) && break
            n==0 ? AA=0 : AA=Asum[n]
            m==0 ? BB=0 : BB=Bsum[m]
            if AA+BB<=K
                kazu=Nmax-mkazu
                #println(AA+BB,"n:",n-1,",m:",m-1)
                break
            end
        end
        if kazu!=0
            break
        end
    end
end
print(kazu)
end
ABC_C()


rl() = [parse(Int64, x) for x in split(readline())]

function solve()
    N, M, K = rl()
    A = rl()
    B = rl()

    na = 0
    for (i, a) in enumerate(A)
        if K < a
            break
        end
        K -= a
        na += 1
    end

    r = na

    for (nb, b) in enumerate(B)
        K -= b
        while K < 0 && na > 0
            K += A[na]
            na -= 1
        end
        if K < 0
            break
        end
        r = max(r, na + nb)
    end
    r
end

println(solve())


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

function main()
    N, M, K = parseMap(split(readline()))
    A = parseMap(split(readline()))
    B = parseMap(split(readline()))
    time = 0
    count = 0
    while (K >= time)
        if !(isempty(A)) && !(isempty(B))
            if A[1] < B[1]
                a = popfirst!(A)
                if K >= (time + a)
                     time += a
                else
                    break
                end
            else
                b = popfirst!(B)
                if K >= (time + b)
                     time += b
                else
                    break
                end
            end
        elseif isempty(A)
            b = popfirst!(B)
            if K >= (time + b)
                time += b
            else
                break
            end
        elseif isempty(B)
            a = popfirst!(A)
            if K >= (time + a)
                time += a
            else
                break
            end
        else
            break
        end
        count += 1
    end
    println(count)
end

main()


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

    ia = 1
    ib = 1
    while k > 0
        if ia > length(a) && ib > length(b)
            break
        elseif ia > length(a)
            k -= b[ib]
            ib += 1
        elseif ib >length(b)
            k -= a[ia]
            ia += 1
        else
            k -= min(a[ia], b[ib])
            if a[ia] > b[ib]
                ib += 1
            else a[ia] > b[ib]
                ia += 1
            end
        end
    end

    p = ifelse(k < 0, 1, 0)
    println(ia-1 + ib-1 - p)
end

main()


using DataStructures
const double = Float64
const int = Int64

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

function main()
  N,M,K  = readint()
  A = readint()
  B = readint()

  TA = Vector{int}(undef,N+1)
  TB = Vector{int}(undef,M+1)
  TA[1] = 0
  TB[1] = 0
  for i in 1:N
    TA[i+1] = TA[i] + A[i]
  end
  for i in 1:M
    TB[i+1] = TB[i] + B[i]
  end

  maxK = 0
  b = M
  for i in 0:N

    for j in b:-1:0
      if TA[i+1] + TB[j+1] <= K
        b = j
        if i + j > maxK
          maxK = i+j
        end
        break
      end
      if j == 0
        println(maxK)
        return
      end
    end
  end
  println(maxK)

end

main()


using DataStructures
const double = Float64
const int = Int64

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

function main()
  N,M,K  = readint()
  A = readint()
  B = readint()

  TA = Vector{int}(undef,N+1)
  TB = Vector{int}(undef,M+1)
  TA[1] = 0
  TB[1] = 0
  for i in 1:N
    TA[i+1] = TA[i] + A[i]
  end
  for i in 1:M
    TB[i+1] = TB[i] + B[i]
  end

  maxK = 0

  for i in 0:N
    b = M
    for j in b:-1:0
      if TA[i+1] + TB[j+1] <= K
        b = j
        if i + j > maxK
          maxK = i+j
        end
        break
      end
      if j == 0
        println(maxK)
        return
      end
    end
  end
  println(maxK)

end

main()


using DataStructures
const double = Float64
const int = Int64

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

function main()
  N,M,K  = readint()
  A = readint()
  B = readint()

  TA = Vector{int}(undef,N+1)
  TB = Vector{int}(undef,M+1)
  TA[1] = 0
  TB[1] = 0
  for i in 1:N
    TA[i+1] = TA[i] + A[i]
  end
  for i in 1:M
    TB[i+1] = TB[i] + B[i]
  end

  maxK = 0

  for i in 0:N
    b = M
    for j in b:-1:0
      if TA[i+1] + TB[j+1] <= K
        b = j
        if i + j > maxK
          maxK = i+j
        end
        break
      end
    end
  end
  println(maxK)

end

main()


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

    ia = 1
    ib = 1
    while k > 0
        if ia > length(a) && ib > length(b)
            break
        elseif ia > length(a)
            k -= b[ib]
            ib += 1
        elseif ib >length(b)
            k -= a[ia]
            ia += 1
        else
            k -= min(a[ia], b[ib])
            a[ia] > b[ib] && (ib += 1)
            a[ia] > b[ib] || (ia += 1)

        end
    end

    p = ifelse(k < 0, 1, 0)
    println(ia-1 + ib-1 - p)
end

main()


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

function main()
    N, M, K = parseMap(split(readline()))
    A = parseMap(split(readline()))
    B = parseMap(split(readline()))
    time = 0
    count = 0
    while (K > time)
        if !(isempty(A)) && !(isempty(B))
            if A[1] < B[1]
                a = popfirst!(A)
                if K >= (time + a)
                     time += a
                else
                    break
                end
            else
                b = popfirst!(B)
                if K >= (time + b)
                     time += b
                else
                    break
                end
            end
        elseif isempty(A)
            b = popfirst!(B)
            if K >= (time + b)
                time += b
            else
                break
            end
        elseif isempty(B)
            a = popfirst!(A)
            if K >= (time + a)
                time += a
            else
                break
            end
        else
            break
        end
        count += 1
    end
    println(count)
end

main()


readInt() = parse(Int,readline())

sigma(a,n) = div((n+1)n*a,2)

function solve()
    ans = 0
    N = readInt()
    f = fill(0, N)
    for i in 1:N
        ans += sigma(i,div(N,i))
    end
    println(ans)
end

solve()

Score : 400 points

Problem Statement

For a positive integer X, let f(X) be the number of positive divisors of X.

Given a positive integer N, find \sum_{K=1}^N K\times f(K).

Constraints

1 \leq N \leq 10^7

Input

Input is given from Standard Input in the following format:

N

Output

Print the value \sum_{K=1}^N K\times f(K).

Sample Input 1

4

Sample Output 1

23

We have f(1)=1, f(2)=2, f(3)=2, and f(4)=3, so the answer is 1\times 1 + 2\times 2 + 3\times 2 + 4\times 3 =23.

Sample Input 2

100

Sample Output 2

26879

Sample Input 3

10000000

Sample Output 3

838627288460105

Watch out for overflows.


readInt() = parse(Int,readline())

sigma(a,n) = (n+1)n*a

function solve()
    ans = 0
    N = readInt()
    f = fill(0, N)
    for i in 1:N
        ans += sigma(i,div(N,i))
    end
    println(div(ans,2))
end

solve()


readInt() = parse(Int,readline())

function solve()
    ans = 0
    N = readInt()
    f = fill(0, N)
    for i in 1:N
        j = i
        while j <= N
            f[j] += 1
            j += i
        end
    end
    for i in 1:N
        ans += i*f[i]
    end
    println(ans)
end

solve()


readInt() = parse(Int,readline())

function solve()
    ans = 0
    N = readInt()
    for i in 1:N
        j = 0
        while j <= N
            ans += j
            j += i
        end
    end
    println(ans)
end

solve()


using Primes, DataStructures
function SumofDivisors()
    N = parse(Int, readline())
    f(x) = div(div(N,x)*(div(N,x)+1),2)
    return println(sum(n*f(n) for n in 1:N))
end
SumofDivisors()


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

function main()
	n = readline() |> parseInt
	ans = 0
	for i in 1:n
		k = div(n,i)
		ans += i*(k*(k+1))>>1
	end
	println(ans)
end

main()


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

function main()
    n::Int=parseInt(readline())
    cnt::Vector{Int}=ones(Int,n)
    ans::Int=1
    for i in 2:n
        for j in 1:n÷i
            cnt[i*j]+=1
        end
        ans+=cnt[i]*i
    end
    println(ans)
end
main()


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

function main()
    n::Int=parseInt(readline())
    cnt::Vector{Int}=ones(Int,n)
    for i in 2:n
        for j in 1:n÷i
            cnt[i*j]+=1
        end
    end
    ans::Int=0
    for i in 1:n
        ans+=cnt[i]*i
    end
    println(ans)
end
main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
using Primes
divisor_num(n) = foldl((a, p) -> a * (p.second + 1), factor(n), init=1)

function main()
    n::Int=parseInt(readline())
    ans::Int128=0
    for i in 1:n
        ans+=divisor_num(i)*i
    end
    println(ans)
end
main()


N=parse(Int,readline())
println(sum(i*div(div(N,i)*(div(N,i)+1),2) for i=1:N))


N=parse(Int,readline())
println(sum(i*div(N,i)*(div(N,i)+1)div 2 for i=1:N))


function main()
    n = parse(Int, readline())
    ans = 0
    a = zeros(Int, n)
    for i in 1:n
        for j in i:i:n
            a[j] += 1
        end
    end

    for i in 1:n
        ans += i*a[i]
    end
    println(ans)
end

main()


rl() = [parse(Int64, x) for x in split(readline())]

function solve()
    N, = rl()

    r = zeros(Int64, N)
    r[1] = 1
    for p in 2:N
        n = pn = 1
        while pn <= N
            pn *= p
            n += 1
        end
        while true
            pn = div(pn, p)
            n -= 1
            if pn <= 1
                break
            end
            a = 1
            apn = pn
            while apn <= N
                if r[a] > 0 && r[apn] == 0
                    r[apn] = r[a] * n * pn
                end
                a += 1
                apn += pn
            end
        end
    end

    sum(r)
end

println(solve())


using Primes
function ABC_D()
    readint()=parse.(Int,readline())
    N=readint()
    s=1
    for i in 2:N
        a=factor(i)
        f=1
        for t in a.pe
            f*=(t.second+1)
        end
        s+=f*i
    end
    print(s)
end
ABC_D()


function ABC_D()
    readint()=parse.(Int,readline())
    N=readint()
    s=1
    for i in 2:N
        a=factor(i)
        f=1
        for t in a.pe
            f*=(t.second+1)
        end
        s+=f*i
    end
    print(s)
end
ABC_D()


using DataStructures
const double = Float64
const int = Int64

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

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

  NN = Vector{int}(undef,N)
  NN .= 1

  for i in 2:N
    for j in i:i:N
      NN[j] += 1
    end
  end

  s = 0
  for i in 1:N
    s += i*NN[i]
  end

  println(s)
end

main()


parseline(str) = parse.(Int, split(str))
function NotDivisible()
    N = readline()
    A = sort(parseline(readline()))
    A = [a for a in A[2:end] if a%A[1]≠0]
    isempty(A) && (return println(0))
    ans = 1
    while !isempty(A)
        ans += 1
        A = [a for a in A[2:end] if a%A[1]≠0]
    end
    println(ans+length(A))
end
NotDivisible()

Score : 400 points

Problem Statement

Given is a number sequence A of length N.

Find the number of integers i \left(1 \leq i \leq N\right) with the following property:

For every integer j \left(1 \leq j \leq N\right) such that i \neq j , A_j does not divide A_i.

Constraints

All values in input are integers.

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

1 \leq A_i \leq 10^6

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.

Sample Input 1

5
24 11 8 3 16

Sample Output 1

3

The integers with the property are 2, 3, and 4.

Sample Input 2

4
5 5 5 5

Sample Output 2

0

Note that there can be multiple equal numbers.

Sample Input 3

10
33 18 45 28 8 19 89 86 2 4

Sample Output 3

5


parseline(str) = parse.(Int, split(str))
function NotDivisible()
    N = readline()
    A = parseline(readline())
    sort!(A)
    ans = 0
    A = [a for a in A[2:end] if a%A[1]≠0]
    flag = false
    while !isempty(A)
        flag = true
        ans += 1
        A = [a for a in A[2:end] if a%A[1]≠0]
    end
    println(flag ? ans+1 : ans)
end
NotDivisible()


parseline(str) = parse.(Int, split(str))
function NotDivisible()
    N = readline()
    A = parseline(readline())
    unique!(A)
    length(A)≡1 && return println(0)
    sort!(A)
    ans = 0
    while !isempty(A)
        ans += 1
        A = [a for a in A[2:end] if a%A[1]≠0]
    end
    println(ans)
end
NotDivisible()


parseline(str) = parse.(Int, split(str))
function NotDivisible()
    N = readline()
    A = parseline(readline())
    sort!(A)
    ans = 0
    while !isempty(A)
        ans += 1
        A = [a for a in A if a%A[1]≠0]
    end
    println(ans)
end
NotDivisible()


parseline(str) = parse.(Int, split(str))
function NotDivisible()
    N = readline()
    A = parseline(readline())
    ans = 0
    for i in 1:length(A)
        flag = true
        for j in 1:length(A)
            i==j && continue
            A[i]%A[j] == 0 && (flag=false; break)
        end
        flag && (ans+=1)
    end
    println(ans)
end
NotDivisible()


parseline(str) = parse.(Int, split(str))
function NotDivisible()
    N = readline()
    A = parseline(readline())
    ans = 0
    for a in A
        flag = true
        for b in A
            b==a && continue
            a%b == 0 && (flag=false; break)
        end
        flag && (ans+=1)
    end
    println(ans)
end
NotDivisible()


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

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap |> sort
	c = zeros(Int,10^6)
	ans = 0
	for i in 1:n
		x = 2
		f = 0
		while x*x<a[i]
			if a[i]%x==0
				if c[x]==1||c[div(a[i],x)]==1
					f = 1
					break
				end
			end
			x += 1
		end
		if x*x==a[i]&&c[x]==1
			f = 1
		end
		if f == 0
			ans += 1
		end
		c[a[i]]+=1
	end
	println(ans)
end

main()


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

function main()
  n = readline() |> parseInt
  a = zeros(Int, n)
  a = readline() |> split |> parseArray

  b = falses(10^6)
  for i in 1:n
    b[a[i]] = true
  end
  for i in 1:10^6
    b[i] || continue
    for j in 2i:i:(10^6 ÷ i) * i
      b[j] = false
    end
  end
  sort!(a)
  for i in 1:n
    i > 1 && a[i] == a[i - 1] && (b[a[i]] = false)
  end
  ans = count(b)

  println(ans)
end

main()


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

function main()
  n = readline() |> parseInt
  a = zeros(Int, n)
  a = readline() |> split |> parseArray

  sort!(a)
  b = trues(n)
  for i in 1:n
    b[i] || continue
    for j in i+1:n
      a[j] % a[i] == 0 && (b[j] = false)
      a[i] == a[j] && (b[i] = false)
    end
  end
  ans = count(b)

  println(ans)
end

main()


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

function main()
    n::Int=parseInt(readline())
    a::Vector{Int}=parseMap(split(readline()))
    cnt::Vector{Int}=[0 for i in 1:10^6]
    for i in a
        cnt[i]+=1
    end
    ans::Int=0
    for i in a
        flag::Bool=true
        for j in 1:trunc(Int,sqrt(i))+1
            if i%j==0 && i>j && (cnt[i÷j]>0 || cnt[j]>0)
                flag=false
                break
            end
        end
        if cnt[i]>1
            flag=false
        end
        if flag
            ans+=1
        end
    end
    println(ans)
end
main()


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

function main()
    n::Int=parseInt(readline())
    a::Vector{Int}=parseMap(split(readline()))
    cnt::Vector{Int}=[0 for i in 1:10^6]
    for i in a
        cnt[i]+=1
    end
    if cnt[1]==1
        println(1)
    elseif cnt[1]>2
        println(0)
    else
        ans::Int=0
        for i in a
            flag::Bool=true
            for j in 2:trunc(Int,sqrt(i))+1
                if i%j==0 && i>j && (cnt[i÷j]>0 || cnt[j]>0)
                    flag=false
                    break
                end
            end
            if cnt[i]>1
                flag=false
            end
            if flag
                ans+=1
            end
        end
        println(ans)
    end
end
main()


const double = Float64
const int = Int64

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

const Nmax = 10^6

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

  A = sort(A)
  kusi = Vector{Bool}(undef, Nmax)
  kusi .= false

  cout = 0
  for i in 1:N-1
    a = A[i]
    kusi[a] && continue

    a < A[i+1] && (cout += 1)
    for j in a:a:Nmax
      kusi[j]=true
    end
  end

  kusi[A[end]] || (cout += 1)
  println(cout)

end

main()


function main()
	readline()
	cnt=zeros(Int,10^6)
	for a=map(x->parse(Int,x),split(readline()))
		cnt[a]+=1
	end
	ans=0
	for i=1:10^6
		if cnt[i]>0
			if cnt[i]==1
				ans+=1
			end
			for j=i+i:i:10^6
				cnt[j]=0
			end
		end
	end
	println(ans)
end
main()


readnum(T::Type{<:Number} = Int) = parse.(T, split(readline()))
let
  _ = readline()
  A = sort(unique(readnum()))
  N = length(A)
  N == 1 && (println(0); exit())
  M = A[end]
  B = zeros(Int, M)
  for i in A
    B[i] += 1
    for j in 2i:i:M
      B[j] += 2
    end
  end
  print(sum(B .== 1))
end


readnum(T::Type{<:Number} = Int) = parse.(T, split(readline()))
let
  _ = readline()
  A = sort(unique(readnum()))
  N = length(A)
  N == 1 && (println(0); exit())
  B = ones(Bool, N)
  for j = 1:N-1
    B[j] == false && continue
    for i = j+1:N
      B[i] == false && continue
      A[i] % A[j] == 0 && (B[i] = false)
    end
  end
  println(sum(B))
end


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

function main()
  n = readline() |> parseInt
  a = zeros(Int, n)
  a = readline() |> split |> parseArray

  sort!(a)
  m = 0
  while length(a) > 0
    i = popfirst!(a)
    i == 0 && continue

    l = length(a)
    b = l == 0
    for j in 1:length(a)
      if a[j] == i
        a[j] = 0
      elseif a[j]%i == 0
        a[j] = 0
        # l -= 1
        b = true
      end
    end
    b && (m += 1)
  end

  println(m)
end

main()


using Primes

function calc(B,i)
  tmp = 0
  for j in 1:i-1
    if B[i] % B[j] == 0
      tmp = 0
      break
    else
      tmp = 1
    end
  end
  tmp
end
parseInt(x) = parse(Int128, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
#１行に数値一つならparseInt(readline())
#１行に数値複数ならparseMap(split(readline()))を行数分。
# 例：n,k = readline() |> split |> parseMap
#      a = readline() |> split |> parseMap
function main()
  N = parseInt(readline())
  A = parseMap(split(readline()))
  B = sort(A)
  println("$N,$B",typeof(B))
  count=0
  C = zeros(Int64,N)
  if N==1
    println(1)
    exit()
  end
  if B[1] != B[2]
    C[1] = 1
  end
  for i in 2:N
    if any(isequal.(B[i],B[1:i-1]))==true
      C[i] = 0
      continue
    end
    if i!=N && any(isequal.(B[i],B[i+1]))==true
      C[i] = 0
      continue
    end
    if isprime(B[i]) == true
      C[i] = 1
    else
      C[i] = calc(B,i)
    end
#=    for j in 1:i-1
      if j==i
        continue
      else
          if B[i] % B[j] == 0
            C[i] = 0
            break
          else
            C[i] = 1
          end
      end
    end=#
  end
  #println("$C")
  println(length(C[C .== 1]))
  #println(size(C[C .== 1])[1])
  #println("Ans:",All[findall(isequal(minimum(q)),q)])
end
main()


using Primes

function calc(B,i)
  tmp = 0
  for j in 1:i-1
    if B[i] % B[j] == 0
      tmp = 0
      break
    else
      tmp = 1
    end
  end
  tmp
end
parseInt(x) = parse(Int128, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
#１行に数値一つならparseInt(readline())
#１行に数値複数ならparseMap(split(readline()))を行数分。
# 例：n,k = readline() |> split |> parseMap
#      a = readline() |> split |> parseMap
function main()
  N = parseInt(readline())
  A = parseMap(split(readline()))
  B = sort(A)
  #println("$N,$B")
  count=0
  C = zeros(Int64,N)
  if B[1] != B[2]
    C[1] = 1
  end
  for i in 2:N
    if any(isequal.(B[i],B[1:i-1]))==true
      C[i] = 0
      continue
    end
    if i!=N && any(isequal.(B[i],B[i+1]))==true
      C[i] = 0
      continue
    end
    if isprime(B[i]) == true
      C[i] = 1
    else
      C[i] = calc(B,i)
    end
#=    for j in 1:i-1
      if j==i
        continue
      else
          if B[i] % B[j] == 0
            C[i] = 0
            break
          else
            C[i] = 1
          end
      end
    end=#
  end
  #println("$C")
  println(length(C[C .== 1]))
  #println(size(C[C .== 1])[1])
  #println("Ans:",All[findall(isequal(minimum(q)),q)])
end
main()


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

function main()
  n = readline() |> parseInt
  a = zeros(Int, n)
  a = readline() |> split |> parseArray

  sort!(a)
  m = 0
  while length(a) > 0
    i = popfirst!(a)
    i == 0 && continue

    l = length(a)
    for j in 1:l
      if a[j]%i == 0
        a[j] = 0
        l -= 1
      end
    end
    l > 0 && (m += 1)
  end

  println(m == 0 ? 0 : m+1)
end

main()


using Primes

function calc(B,i)
  tmp = 0
  for j in 1:i-1
    if B[i] % B[j] == 0
      tmp = 0
      break
    else
      tmp = 1
    end
  end
  tmp
end
parseInt(x) = parse(Int128, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
#１行に数値一つならparseInt(readline())
#１行に数値複数ならparseMap(split(readline()))を行数分。
# 例：n,k = readline() |> split |> parseMap
#      a = readline() |> split |> parseMap
function main()
  N = parseInt(readline())
  A = parseMap(split(readline()))
  B = sort(A)
  #println("$N,$B")
  count=0
  C = zeros(Int64,N)
  if B[1] != B[2]
    C[1] = 1
  end
  for i in 2:N
    if isprime(B[i]) == true
      C[i] = 1
    else
      C[i] = calc(B,i)
    end
#=    for j in 1:i-1
      if j==i
        continue
      else
          if B[i] % B[j] == 0
            C[i] = 0
            break
          else
            C[i] = 1
          end
      end
    end=#
  end
  #println("$C")
  println(length(C[C .== 1]))
  #println(size(C[C .== 1])[1])
  #println("Ans:",All[findall(isequal(minimum(q)),q)])
end
main()


readnum(T::Type{<:Number} = Int) = parse.(T, split(readline()))
let
  ans = 1
  _ = readline()
  A = sort(unique(readnum()))
  N = length(A)
  N == 1 && (println(0); exit())
  for i = 2:N
    0 ∉ prod(A[i] .% A[1:i-1]) && (ans += 1)
  end
  println(ans)
end


function calc(B,i)
  tmp = 0
  for j in 1:i-1
    if j==i
      continue
    else
        if B[i] % B[j] == 0
          tmp = 0
          break
        else
          tmp = 1
        end
    end
  end
  tmp
end
parseInt(x) = parse(Int128, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
#１行に数値一つならparseInt(readline())
#１行に数値複数ならparseMap(split(readline()))を行数分。
# 例：n,k = readline() |> split |> parseMap
#      a = readline() |> split |> parseMap
function main()
  N = parseInt(readline())
  A = parseMap(split(readline()))
  B = sort(A)
  #println("$N,$B")
  count=0
  C = zeros(N)
  for i in 1:N
    if B[1] != B[2]
      C[1] = 1
    end
    C[i] = calc(B,i)

#=    for j in 1:i-1
      if j==i
        continue
      else
          if B[i] % B[j] == 0
            C[i] = 0
            break
          else
            C[i] = 1
          end
      end
    end=#
  end
  #println("$C")
  println(size(C[C .== 1])[1])
  #println("Ans:",All[findall(isequal(minimum(q)),q)])
end
main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
#１行に数値一つならparseInt(readline())
#１行に数値複数ならparseMap(split(readline()))を行数分。
# 例：n,k = readline() |> split |> parseMap
#      a = readline() |> split |> parseMap
function main()
  N = parseInt(readline())
  A = parseMap(split(readline()))
  B = sort(A)
  #println("$N,$B")
  count=0
  C = zeros(N)
  for i in 1:N
    if B[1] != B[2]
      C[1] = 1
    end
    for j in 1:i-1
      if j==i
        continue
      else
          if B[i] % B[j] == 0
            C[i] = 0
            break
          else
            C[i] = 1
          end
      end
    end
  end
  #println("$C")
  println(size(C[C .== 1])[1])
  #println("Ans:",All[findall(isequal(minimum(q)),q)])
end
main()


readstr() = split(readline())
readnum(T::Type{<:Number} = Int) = parse.(T, split(readline()))
let
  ans = 1
  _ = readline()
  A = sort(unique(readnum()))
  N = length(A)
  N == 1 && (println(0); exit())
  for i = 2:N
    for j = 1:i-1
      A[i] % A[j] == 0 && break
      if j == i-1
        ans += 1
      end
    end
  end
  println(ans)
end


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
#１行に数値一つならparseInt(readline())
#１行に数値複数ならparseMap(split(readline()))を行数分。
# 例：n,k = readline() |> split |> parseMap
#      a = readline() |> split |> parseMap
function main()
  N = parseInt(readline())
  A = parseMap(split(readline()))
  B = sort(A)
  println("$N,$B")
  count=0
  C = zeros(N)
  for i in 1:N
    if B[1] != B[2]
      C[1] = 1
    end
    for j in 1:i-1
      if j==i
        continue
      else
          if B[i] % B[j] == 0
            C[i] = 0
            break
          else
            C[i] = 1
          end
      end
    end
  end
  #println("$C")
  println(size(C[C .== 1])[1])
  #println("Ans:",All[findall(isequal(minimum(q)),q)])
end
main()


readstr() = split(readline())
readnum(T::Type{<:Number} = Int) = parse.(T, split(readline()))
let
  ans = 0
  N, = readnum()
  A = readnum()
  for i in 1:N
    for j in 1:N
      i == j && continue
      A[i] % A[j] == 0 && break
      if j == N || (i == N && (j == N - 1))
        ans += 1
      end
    end
  end
  println(ans)
end