program sum_of_product
    implicit none
    integer(16) n, i, y,ans,x,z,j
    integer(16),allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    y=0
    x=0
    z=0
    j=0

    if(2<=n .or. n<=200000)then
        do i=1,n
            if(0<=a(i) .or. a(i)<=1000000000)then
                x=x+a(i)
                y=sum(a)-x
                z=y*a(i)
                j=j+z
            end if
        end do
    end if
    ans=modulo(j,1000000007)
    print *,ans
end program sum_of_product

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.


program sum_of_product
    implicit none
    integer(16) n, i, y,ans,x,z,j
    integer(16),allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    y=0
    x=0
    z=0
    j=0

    if(2<=n .or. n<=200000)then
        do i=1,n
            if(0<=a(i) .or. a(i)<=1000000000)then
                x=x+a(i)
                y=sum(a)-x
                z=y*a(i)
                j=j+z
            end if
        end do
    end if
    ans=mod(j,1000000007)
    print *,ans
end program sum_of_product


program sum_of_product
    implicit none
    integer(8) n, i, y,ans,x,z,j
    integer(8),allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    y=0
    x=0
    z=0
    j=0

    if(2<=n .or. n<=200000)then
        do i=1,n
            if(0<=a(i) .or. a(i)<=1000000000)then
                x=x+a(i)
                y=mod((sum(a)-x),1000000007)
                z=y*a(i)
                j=j+z
            end if
        end do
    end if
    ans=mod(j,1000000007)
    print *,ans
end program sum_of_product


program sum_of_product
    implicit none
    integer(8) n, i, y,ans,x,z,j
    integer(8),allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    y=0
    x=0
    z=0
    j=0

    if(2<=n .or. n<=200000)then
        do i=1,n
            if(0<=a(i) .or. a(i)<=1000000000)then
                x=x+a(i)
                y=sum(a)-x
                z=y*a(i)
                j=j+z
            end if
        end do
    end if
    ans=mod(j,1000000007)
    print *,ans
end program sum_of_product


program sum_of_product
    implicit none
    integer(8) n, i, j, b, sum,ans,x
    integer(8),allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    sum=0

    if(2<=n .or. n<=200000)then
        do i=1,n-1
            if(0<=a(i) .or. a(i)<=1000000000)then
                do j=1+i,n
                        x=mod(a(i),1000000007)
                        b=a(j)*x
                        sum=sum+b
                end do
            end if
        end do
    end if
    ans=mod(sum,1000000007)
    print *,ans
end program sum_of_product


program sum_of_product
    implicit none
    integer(8) n, i, j, b, sum,ans
    integer(8),allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    sum=0

    if(2<=n .or. n<=200000)then
        do i=1,n-1
            if(0<=a(i) .or. a(i)<=1000000000)then
                do j=1+i,n
                        b=a(i)*a(j)
                        sum=sum+b
                end do
            end if
        end do
    end if
    ans=mod(sum,1000000007)
    print *,ans
end program sum_of_product


program sum_of_product
    implicit none
    integer(8) n, i, j, b, ans, sum,c
    integer(8),allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    sum=0

    if(2<=n .or. n<=200000)then
        do i=1,n-1
            if(0<=a(i) .or. a(i)<=1000000000)then
                do j=i+1,n
                    if(i<j .and. j<=n)then
                        b=mod(a(i),1000000007)
                        c=b*a(j)
                        sum=sum+c
                     else
                        exit
                    end if
                end do
            endif
        end do
    end if
    ans=mod(sum,1000000007)
    print *,ans
end program sum_of_product


program sum_of_product
    implicit none
    integer(8) n, i, j, b, ans, sum
    integer(8),allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    sum=0

    if(2<=n .or. n<=200000)then
        do i=1,n-1
            if(0<=a(i) .and. a(i)<=1000000000)then
                do j=i+1,n
                    if(i<j .and. j<=n)then
                        b=a(i)*a(j)
                        sum=sum+b
                     else
                        exit
                    end if
                end do
            endif
        end do
    end if
    ans=mod(sum,1000000007)
    print *,ans
end program sum_of_product


program sum_of_product
    implicit none
    integer(8) n, i, j, b, ans, sum
    integer(8),allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    sum=0

    if(2<=n .or. n<=200000)then
        do i=1,n-1
            do j=i+1,n
                if(i<j)then
                    if(j<=n)then
                        b=a(i)*a(j)
                        sum=sum+b
                    end if
                end if
            end do
        end do
    end if
    ans=mod(sum,1000000007)
    print *,ans
end program sum_of_product


program sum_of_product
    implicit none
    integer(8) n, i, j, b, ans, sum
    integer(8),allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    sum=0

    do i=1,n-1
        do j=i+1,n
            if(i<j)then
                if(j<=n)then
                b=a(i)*a(j)
                sum=sum+b
                end if
            else
                exit
            end if
        end do
    end do
    ans=modulo(sum,1000000007)
    print *,ans
end program sum_of_product


program sum_of_product
    implicit none
    integer n, i, j, sum,b, ans
    integer,allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    sum=0

    do i=1,n
        do j=i+1,n
            if(j<=n)then
                if(i/=j)then
                b=a(i)*a(j)
                sum=sum+b
                end if
            else
                exit
            end if
        end do
    end do
    ans=mod(sum,1000000007)
    print *,ans
end program sum_of_product


program sum_of_product
    implicit none
    integer(8) n, i, j, sum,b, ans
    integer(8),allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    sum=0

    do i=1,n
        do j=i+1,n
            if(j<=n)then
                if(i/=j)then
                b=a(i)*a(j)
                sum=sum+b
                end if
            else
                exit
            end if
        end do
    end do
    ans=mod(sum,1000000007)
    print *,ans
end program sum_of_product


program sum_of_product
    implicit none
    integer(8) n, i, j, sum,b, ans
    integer(8),allocatable :: a(:)

    read *, n
    allocate(a(n))
    read *, a
    sum=0

    do i=1,n
        do j=i+1,n
            if(i/=j)then
                b=a(i)*a(j)
                sum=sum+b
            end if
        end do
    end do
    ans=mod(sum,1000000007)
    print *,ans
end program sum_of_product


program ABC177C
    implicit none
    integer(8)::N,m=1000000007,i,ans=0
    integer(8),allocatable::A(:),S(:)
    read*,N
    allocate(A(N))
    allocate(S(N))
    read*,A
    do i=1,N
        if(i==1)then
            S(i)=mod(A(i),m)
        else
            S(i)=mod(S(i-1)+A(i),m)
        end if
    end do

    do i=1,N-1
        if(S(N)-S(i)<0)then
            ans=ans+mod(A(i)*(m+S(N)-S(i)),m)
        else
            ans=ans+mod(A(i)*(S(N)-S(i)),m)
        end if
        ans=mod(ans,m)
    end do
    print'(i0)',mod(ans,m)
end program ABC177C


program ABC177C
    implicit none
    integer(8)::N,m=1000000007,i,ans=0
    integer(8),allocatable::A(:),S(:)
    read*,N
    allocate(A(N))
    allocate(S(N))
    read*,A
    do i=1,N
        if(i==1)then
            S(i)=A(i)
        else
            S(i)=S(i-1)+A(i)
        end if
    end do

    do i=1,N-1
        ans=ans+mod(A(i)*(S(N)-S(i)),m)
        ans=mod(ans,m)
    end do
    print'(i0)',mod(ans,m)
end program ABC177C


program Spp
    implicit none
    integer,parameter :: m = 1000000007
    integer(16),allocatable :: a(:)
    integer :: n, i
    integer(16) :: ans = 0
    read *, n
    allocate(a(n))
    read *, a(1:n)
    do i = 1, n-1
        ans = ans + mod(sum(a(i+1:n)) * a(i), m)
    end do
    print *, ans
end program


program Spp
    implicit none
    integer(8) a, n, i, j
    integer(8) :: ans = 0
    read *, n
    read *, a
    do i = 1, n-1
        do j = i+1, n
            if(i<j)then
                ans = ans + i * j
            end if
        end do
    end do
    ans = mod(ans, 7+10**9)
    print *, ans
end program


program sum_of_product_of_pairs
  implicit none
  integer(16), parameter :: m = 1000000007
  integer(16), allocatable :: a(:)
  integer :: n
  read(*,*) n
  allocate(a(n))
  read(*,*) a
  write(*,'(i0)') mod((sum(a) ** 2 - sum(a ** 2)) / 2, m)
end program sum_of_product_of_pairs


program sum_of_product_of_pairs
  implicit none
  integer(8), parameter :: m = 1000000007
  integer :: n, i
  integer(8) :: a(200000) = 0, s = 0, x = 0
  read(*,*) n
  read(*,*) a(1:n)
  s = a(n)
  do i = n - 1, 1, -1
    x = mod(x + s * a(i), m)
    s = s + a(i)
    if (s >= m) s = s - m
  end do
  write(*,'(i0)') x
end program sum_of_product_of_pairs


program abc50
 implicit none
 integer,allocatable::a(:)
 integer(8),parameter::m=10**9+7
 integer(16)::ia,ans=0,ib,N,ic,id,s
 read(*,*)N
 allocate(a(N))
 read(*,*)(a(ia),ia=1,N)
 s=sum(a)
 do ib=1,N-1
  s=s-a(ib)
  ans=ans+a(ib)*s
 end do
 print'(i0)',mod(ans,m)
end program abc50


program main
  implicit none
  integer :: N
  integer(8), allocatable  :: A(:)
  integer :: i,j
  integer(8) :: out, A_sum, idiv
  read(*,*) N
  allocate(A(N))
  read(*,*) A(:)

  out = 0
  !
!!  do i = 1,N-1
!!     do j= i+1,N
!!        out = out + mod(A(i)*A(j),10**9+7)
!!        out = mod(out,10**9+7)
!!     end do
!!  end do
  A_sum = sum(A)
  idiv = int(10**9+7,8)
  do i = 1,N-1
     A_sum = A_sum - A(i)
     out = mod( out + A(i)*mod(A_sum,idiv), idiv )
  end do
  write(*,'(i0)') mod(out,idiv)
  deallocate(A)
end program main


program abc50
 implicit none
 integer,allocatable::a(:)
 integer(8),parameter::m=10**9+7
 integer(16)::ia,ans=0,ib,N,ic,id
 read(*,*)N
 allocate(a(N))
 read(*,*)(a(ia),ia=1,N)
 do ib=1,N-1
  ans=ans+a(ib)*(mod(sum(a(ib+1:N)),m))
  ans=mod(ans,m)
 end do
 print'(i0)',ans
end program abc50


program abc50
 implicit none
 integer,allocatable::a(:)
 integer(8),parameter::m=10**9+7
 integer(16)::ia,ans=0,ib,N,ic,id
 read(*,*)N
 allocate(a(N))
 read(*,*)(a(ia),ia=1,N)
 do ic=1,N
  a(ic)=mod(a(ic),m)
 end do
 do ib=1,N-1
  ans=ans+a(ib)*(mod(sum(a(ib+1:N)),m))
  ans=mod(ans,m)
 end do
 print'(i0)',ans
end program abc50


program q_3
  implicit none
  integer(8) :: n,i,j,mo,seki,sum,tmp
  integer(8),allocatable :: A(:)

  mo = 10**9+7
  read(*,*) n
  allocate(A(n))
  read(*,*) A
  seki = 0
  sum = 0
  do i =1,n
     sum = sum + A(i)
  end do
!  write(*,*)sum
  tmp = sum
  do i =1,n-1
     tmp = tmp - A(i)
 !    write(*,*)tmp
     seki = seki + mod(A(i)*mod(tmp,mo),mo)
!     do j = i+1,n
!        seki = seki + mod(A(i)*A(j),mo)
!     end do
  enddo
!  write(*,*) seki
  write(*,*) mod(seki,mo)

end program q_3


program c177

implicit none
integer(16) :: n, ans, i, j
integer(16), allocatable :: a(:)

read *, n
allocate(a(1:n))

read *,  a(:)

ans = 0

do i = 1, n-1
 do j = i+1, n
  ans = ans + mod(a(i) * a(j), 1000000007)
  !print *, a(i) * a(j), mod(a(i) * a(j), 1000000007), ans
 end do
end do

print *, mod(ans,1000000007)

end program c177


program main
  implicit none
  integer :: N
  integer(8), allocatable  :: A(:)
  integer :: i,j
  integer(8) :: out, A_sum, idiv
  read(*,*) N
  allocate(A(N))
  read(*,*) A(:)

  out = 0
  !
!!  do i = 1,N-1
!!     do j= i+1,N
!!        out = out + mod(A(i)*A(j),10**9+7)
!!        out = mod(out,10**9+7)
!!     end do
!!  end do
  A_sum = sum(A)
  idiv = int(10**9+7,8)
  do i = 1,N-1
     A_sum = A_sum - A(i)
     out = out + mod(A(i)*A_sum, idiv)
  end do
  out = mod(out, idiv)
  write(*,'(i0)') out
end program main


program c177

implicit none
integer(8) :: n, ans, i, j
integer(8), allocatable :: a(:)

read *, n
allocate(a(1:n))

read*,  a(:)

ans = 0

do i = 1, n-1
 do j = i+1, n
  ans = ans + mod(a(i) * a(j), 1000000007)
 end do

 if(ans > 1000000007) then
  ans = ans - 1000000007
 end if
end do

!print *, mod(ans,1000000007)
print *, ans

end program c177


program C
    implicit none

    integer(4) :: N
    integer(16), allocatable :: A(:)
    integer(4) :: i, j
    integer(16) :: sum

    read(*, *) N

    allocate(A(1:N))
    read(*, *) A

    sum = 0
    do i = 1, N
        do j = i+1, N
            sum = sum + A(i)*A(j)
            if (sum > 10**9 + 7) then
                sum = mod(sum, 10**9 + 7)
            end if
        end do
    end do

    write(*, *) sum

end program C


program C177
    implicit none
    integer(8) :: N
    integer(8), allocatable, dimension(:) :: A
    integer(8) :: i, j, ans

    read(5,*) N
    allocate(A(N))
    read(5,*) (A(i), i = 1, N)

    ans = 0
    do i = 1, N-1
        do j = i+1, N
            ans = ans + A(i)*A(j)

            if (ans > 1000000007) then
                ans = mod(ans,1000000007)
            end if
        end do
    end do

    write(6,*) ans

end program C177


program c177

implicit none
integer(16) :: n, ans, i, j
integer(16), allocatable :: a(:)

read *, n
allocate(a(1:n))

read*,  a(:)

ans = 0

do i = 1, n-1
 do j = i+1, n
  ans = ans + a(i) * a(j)
  !if(ans > 1000000007) then
   !ans = ans - 1000000007
  !end if
 end do
end do

print *, mod(ans,1000000007)

end program c177


program main
  implicit none
  integer :: N
  integer(8), allocatable  :: A(:)
  integer :: i,j
  integer(8) :: out
  read(*,*) N
  allocate(A(N))
  read(*,*) A(:)

  out = 0
  !
!!  do i = 1,N-1
!!     do j= i+1,N
!!        out = out + mod(A(i)*A(j),10**9+7)
!!        out = mod(out,10**9+7)
!!     end do
!!  end do
  do i = 1,N-1
     out = out + mod(A(i)*sum(A(i+1:N)), 10**9+7)
     out = mod(out,10**9+7)
  end do
   write(*,'(i0)') out
end program main


module fast_prime_sieve
  implicit none
contains
  subroutine primes(p, m, n)
    integer, allocatable, intent(inout) :: p(:)
    integer, intent(inout) :: m
    integer, intent(in) :: n
    integer :: lp(n), pr(n), i, j
    lp(n) = 0
    m = 0
    do i = 2, n
      if (lp(i) == 0) then
        lp(i) = i
        m = m + 1
        pr(m) = i
      end if
      j = 1
      do j = 1, m
        if (pr(j) > lp(i) .or. i * pr(j) > n) exit
        lp(i * pr(j)) = pr(j)
      end do
    end do
    allocate(p(m))
    p = pr(1:m)
  end
end module fast_prime_sieve
module bisection
  implicit none
  integer, private, parameter :: intkind = 4
contains
  integer function lower_bound(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b
    integer :: l, r, m
    l = 0
    r = size(a) + 1
    do while (r - l > 1)
      m = (l + r) / 2
      if (a(m) < b) then
        l = m
      else
        r = m
      end if
    end do
    res = r
  end
  integer function upper_bound(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b
    integer :: l, r, m
    l = 0
    r = size(a) + 1
    do while (r - l > 1)
      m = (l + r) / 2
      if (a(m) > b) then
        r = m
      else
        l = m
      end if
    end do
    res = l
  end
end module bisection
module gcd_lcm
  implicit none
  integer, private, parameter :: intkind = 4
contains
  integer(intkind) function gcd(x, y) result(res)
    integer(intkind), intent(in) :: x, y
    integer(intkind) :: a, b
    a = max(abs(x), abs(y))
    b = min(abs(x), abs(y))
    res = a
    do while (b > 0)
      res = b
      b = mod(a, b)
      a = res
    end do
  end
  integer(intkind) function lcm(x, y) result(res)
    integer(intkind), intent(in) :: x, y
    res = x / gcd(x, y) * y
  end
end module gcd_lcm
program coprime
  use fast_prime_sieve
  use bisection
  use gcd_lcm
  implicit none
  integer :: n, m, d, q, i, j
  logical :: pairwise = .true.
  integer, allocatable :: a(:), p(:)
  logical, allocatable :: b(:)
  character(8), parameter :: x = 'pairwise', y = 'setwise ', z = 'not     '
  read(*,*) n
  allocate(a(n))
  read(*,*) a
  call primes(p, m, maxval(a(1:n)))
  allocate(b(m))
  b = .false.
  do i = 1, n
    d = a(i)
    do j = 1, m
      q = p(j)
      if (q * q > d) exit
      if (mod(d, q) == 0) then
        if (b(j)) then
          pairwise = .false.
          goto 100
        end if
        b(j) = .true.
        do while (mod(d, q) == 0)
          d = d / q
        end do
      end if
    end do
    if (d /= 1) then
      j = lower_bound(p, d)
      if (b(j)) then
        pairwise = .false.
        goto 100
      end if
      b(j) = .true.
    end if
  end do
  100 if (pairwise) then
    write(*,'(a)') 'pairwise coprime'
    stop
  end if
  d = a(1)
  do i = 2, n
    d = gcd(d, a(i))
  end do
  write(*,'(a)') trim(merge('setwise', 'not    ', d == 1)) // ' coprime'
end program coprime

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.


module fast_prime_sieve
  implicit none
contains
  subroutine primes(p, m, n)
    integer, allocatable, intent(inout) :: p(:)
    integer, intent(inout) :: m
    integer, intent(in) :: n
    integer :: lp(n), pr(n), i, j
    lp(n) = 0
    m = 0
    do i = 2, n
      if (lp(i) == 0) then
        lp(i) = i
        m = m + 1
        pr(m) = i
      end if
      j = 1
      do j = 1, m
        if (pr(j) > lp(i) .or. i * pr(j) > n) exit
        lp(i * pr(j)) = pr(j)
      end do
    end do
    allocate(p(m))
    p = pr(1:m)
  end
end module fast_prime_sieve
module bisection
  implicit none
  integer, private, parameter :: intkind = 4
contains
  integer function lower_bound(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b
    integer :: l, r, m
    l = 0
    r = size(a) + 1
    do while (r - l > 1)
      m = (l + r) / 2
      if (a(m) < b) then
        l = m
      else
        r = m
      end if
    end do
    res = r
  end
  integer function upper_bound(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b
    integer :: l, r, m
    l = 0
    r = size(a) + 1
    do while (r - l > 1)
      m = (l + r) / 2
      if (a(m) > b) then
        r = m
      else
        l = m
      end if
    end do
    res = l
  end
end module bisection
program coprime
  use fast_prime_sieve
  use bisection
  implicit none
  integer :: n, m, d, q, i, j
  integer, allocatable :: a(:), b(:), p(:)
  character(8), parameter :: x = 'pairwise', y = 'setwise ', z = 'not     '
  read(*,*) n
  allocate(a(n))
  read(*,*) a
  call primes(p, m, maxval(a(1:n)))
  allocate(b(m))
  b = 0
  do i = 1, n
    d = a(i)
    do j = 1, m
      q = p(j)
      if (q * q > d) exit
      if (mod(d, q) == 0) then
        b(j) = b(j) + 1
        do while (mod(d, q) == 0)
          d = d / q
        end do
      end if
    end do
    if (d /= 1) then
      j = lower_bound(p, d)
      b(j) = b(j) + 1
    end if
  end do
  write(*,'(a)') trim(merge(x, merge(y, z, all(b < n)), all(b < 2))) // ' coprime'
end program coprime


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32)::n,i,j,x,y
    integer(int32), allocatable:: a(:)
    logical:: pc, sc
    logical,allocatable:: t(:)

    read*, n
    allocate(a(n))
    read*, a(:)
    allocate(t(maxval(a)), source=.false.)
    pc = .true.
    do i=1,n
        do j=1,ceiling(sqrt(dble(a(i))))
            if (mod(a(i),j) == 0) then
                x = a(i)/j
                y = j
                if (t(x) .and. x > 1) pc = .false.
                if (t(y) .and. y > 1) pc = .false.
                t(x) = .true.
                t(y) = .true.
            end if
            if (.not. pc) exit
        end do
        if (.not. pc) exit
    end do

    x = a(1)
    do i=2,n
        x = gcd(x,a(i))
    end do
    sc = x == 1

    if (pc) then
        print'(a)', "pairwise coprime"
    else if (sc) then
        print'(a)', "setwise coprime"
    else
        print'(a)', "not coprime"
    end if
contains
    function lcm(x,y) result(ret)
        integer(int32):: x,y,ret
        ret=x*y/gcd(x,y)
    end function
    recursive function gcd(x,y) result(ret)
        integer(int32):: x,y,ret

        if (mod(x,y) == 0) then
            ret = y
            return
        end if

        ret = gcd(y,mod(x,y))
    end function
end program main


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32)::n,i,j,x,y
    integer(int32), allocatable:: a(:)
    logical:: pc, sc
    logical,allocatable:: t(:)

    read*, n
    allocate(a(n))
    read*, a(:)
    allocate(t(maxval(a)), source=.false.)
    pc = .true.
    do i=1,n
        do j=1,ceiling(sqrt(dble(a(i))))
            if (mod(a(i),j) == 0) then
                x = a(i)/j
                y = j
                if (t(x) .and. x > 1) pc = .false.
                if (t(y) .and. y > 1) pc = .false.
                t(x) = .true.
                t(y) = .true.
            end if
        end do
    end do

    x = a(1)
    do i=2,n
        x = gcd(x,a(i))
    end do
    sc = x == 1

    if (pc) then
        print'(a)', "pairwise coprime"
    else if (sc) then
        print'(a)', "setwise coprime"
    else
        print'(a)', "not coprime"
    end if
contains
    function lcm(x,y) result(ret)
        integer(int32):: x,y,ret
        ret=x*y/gcd(x,y)
    end function
    recursive function gcd(x,y) result(ret)
        integer(int32):: x,y,ret

        if (mod(x,y) == 0) then
            ret = y
            return
        end if

        ret = gcd(y,mod(x,y))
    end function
end program main


program main
    implicit none
    integer(kind=16) :: n,i,other
    integer(kind=16),allocatable :: a(:),multiplied_a(:)
    logical :: Flag
    read*,n
    allocate(a(n),multiplied_a(n+1))
    read*,a
    multiplied_a(1) = 1
    do i = 2,n+1
        multiplied_a(i) = multiplied_a(i-1) * a(i-1)
    end do
    !print*,multiplied_a
    Flag = .TRUE.
    do i = 2,n
        other = multiplied_a(n+1) / multiplied_a(i)
        !print*,a(i-1),other,gcd(a(i-1),other)
        if (gcd(a(i-1),other) .ne. 1) then
            Flag = .False.
            exit
        endif
    end do
    if (Flag) then
        print*,'pairwise coprime'
        stop
    end if
    if (gcds(a,n) == 1) then
        print*,'setwise coprime'
    else
        print*,'not coprime'
    endif
    stop
contains
integer(16) function gcd(a, b)
    implicit none
    integer(16), intent(IN) :: a, b
    integer(16) :: l, m, n

    m = a
    n = b

    do
        l = mod(m, n)
        if (l == 0) exit
        m = n
        n = l
    end do

    gcd = n

    return
end function gcd
integer(16) function gcds(a,n)
    implicit none
    integer(16), intent(IN) :: a(n),n
    integer(16) :: i
    gcds = a(1)
    do i = 1,n
        gcds = gcd(gcds,a(i))
    end do
end function gcds
end program main


program coprime
  implicit none
  integer :: n, m, c, p, i
  integer, allocatable :: a(:), b(:)
  character(8), parameter :: x = 'pairwise', y = 'setwise ', z = 'not     '
  read(*,*) n
  allocate(a(n))
  read(*,*) a
  m = maxval(a(1:n))
  allocate(b(m))
  b = 0
  do i = 1, n
    c = a(i)
    p = 2
    do while (p * p <= c)
      if (mod(c, p) == 0) then
        b(p) = b(p) + 1
        do while (mod(c, p) == 0)
          c = c / p
        end do
      end if
      p = p + 1
    end do
    if (c /= 1) b(c) = b(c) + 1
  end do
  write(*,'(a)') trim(merge(x, merge(y, z, all(b < n)), all(b < 2))) // ' coprime'
end program coprime


program coprime
  implicit none
  integer :: n, m, c, p, i
  integer, allocatable :: a(:), b(:)
  character(8), parameter :: x = 'pairwise', y = 'setwise ', z = 'not     '
  read(*,*) n
  allocate(a(n))
  read(*,*) a
  m = maxval(a(1:n))
  allocate(b(m))
  b = 0
  do i = 1, n
    c = a(i)
    p = 2
    do while (p * p <= c)
      if (mod(c, p) == 0) then
        b(p) = b(p) + 1
        do while (mod(c, p) == 0)
          c = c / p
        end do
      end if
      p = p + 1
    end do
  end do
  write(*,'(a)') trim(merge(x, merge(y, z, all(b < n)), all(b < 2))) // ' coprime'
end program coprime


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: h,w,m,i,ymax,xmax,nmx,nmy,ansptn
    integer(int64), allocatable:: x(:), y(:), ysum(:), xsum(:)
    logical,allocatable:: is_maxy(:), is_maxx(:), kx(:),ky(:)

    read*, h,w,m
    allocate(x(m), y(m))
    allocate(ysum(h), xsum(w), source=0_8)
    allocate(is_maxy(h), is_maxx(w), source=.false.)
    allocate(kx(w), ky(h), source=.false.)

    do i=1,m
        read*, y(i), x(i)
        ysum(y(i))=ysum(y(i))+1
        xsum(x(i))=xsum(x(i))+1
    end do
    xmax = maxval(xsum(:))
    ymax = maxval(ysum(:))

    nmx=0; nmy=0
    do i=1,h
        if (ysum(i) == ymax) nmy=nmy+1
    end do
    do i=1,w
        if (xsum(i) == xmax) nmx=nmx+1
    end do

    ansptn = nmx*nmy
    do i=1,m
        if (xsum(x(i))==xmax .and. ysum(y(i))==ymax) ansptn=ansptn-1
    end do
    if (ansptn <= 0) xmax=xmax-1
    print'(i0)', ymax+xmax
end program 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


program bomber
  implicit none
  integer :: h, w, m, nx, ny, mx = 0, my = 0, d, i
  integer, dimension(300000) :: x = 0, y = 0, cx = 0, cy = 0
  read(*,*) h, w, m
  do i = 1, m
    read(*,*) x(i), y(i)
    cx(x(i)) = cx(x(i)) + 1
    cy(y(i)) = cy(y(i)) + 1
  end do
  nx = maxval(cx(1:h))
  ny = maxval(cy(1:w))
  do i = 1, h
    if (cx(i) == nx) mx = mx + 1
  end do
  do i = 1, w
    if (cy(i) == ny) my = my + 1
  end do
  d = mx * my
  do i = 1, m
    if (cx(x(i)) == nx .and. cy(y(i)) == ny) d = d - 1
  end do
  write(*,'(i0)') nx + ny - merge(1, 0, d == 0)
end program bomber


      PROGRAM WizardInMaze
      IMPLICIT NONE
      INTEGER,PARAMETER :: p = 8
      integer(p) :: h,w,m,i,rowMax,colMax, ans
      integer(p),allocatable :: row(:),col(:),hData(:),wData(:),rt(:),ct(:),piyo(:,:)
      logical,allocatable :: isRM(:),isCM(:)

      read*,h,w,m
      allocate( row(h),col(w) )
      allocate( hData(m),wData(m) )
      row = 0; col = 0;
      do i = 1,m
        read*,hData(i),wData(i)
        row(hData(i)) = row(hData(i)) + 1
        col(wData(i)) = col(wData(i)) + 1
      end do

      allocate( isRM(h),isCM(w) )
      rowMax = maxval(row); colMax = maxval(col)
      isRM = row==rowMax
      isCM = col==colMax

      ans = rowMax + colMax

      rt = pack([(i,i=1,h)],isRM)
      ct = pack([(i,i=1,w)],isCM)

      allocate( piyo(size(rt),size(ct)) )
      piyo = 0

      do i = 1,m
        if( isRM(hData(i)).and.isCM(wData(i)) )then
          piyo(hData(i),wData(i)) = 1
        end if
      end do

      print*,ans-minval(piyo)

      stop
      !debugg

      print*,isRM
      print*,[isCM, isRM]

      END PROGRAM


program sample
    implicit none

    integer(8) :: i,j,k,m,n,h,w
    integer(8)::c,d,e1,e2
    integer(8),allocatable::x(:,:),y(:),z(:),a(:),b(:)

    read(*,*) h,w,m
    !allocate(x(1:h,1:w),y(1:h),z(1:w),a(m),b(m))
    allocate(y(1:h),z(1:w),a(m),b(m))
    x(:, :)=0
    y(:)=0
    z(:)=0
    do i=1,m
        read(*,*)a(i),b(i)
        !x(a(i),b(i))=1
        y(a(i))=y(a(i))+1
        z(b(i))=z(b(i))+1
    end do

    c=maxval(y(1:h))
    d=maxval(z(1:w))
    e1=0
    e2=0
    do i=1,h
        if(y(i)==c)then

            e1=e1+1
        end if
    end do
    do i=1,w
        if(z(i)==d)then

            e2=e2+1
        end if
    end do
    e1=e1*e2

    do i=1,m
        if(y(a(i))==c .and. z(b(i))==d)then
            e1=e1-1
        end if
    end do
    if(e1==0)then
            write(*,'(i0)')c+d-1
    else
        write(*,*)c+d
    end if

    stop
end program sample


program sample
    implicit none

    integer(8) :: i,j,k,m,n,h,w
    integer(8)::c,d,e1,e2
    integer(8),allocatable::x(:,:),y(:),z(:),a(:),b(:)

    read(*,*) h,w,m
    !allocate(x(1:h,1:w),y(1:h),z(1:w),a(m),b(m))
    allocate(y(1:h),z(1:w),a(m),b(m))
    !x(:, :)=0
    y(:)=0
    z(:)=0
    do i=1,m
        read(*,*)a(i),b(i)
        !x(a(i),b(i))=1
        y(a(i))=y(a(i))+1
        z(b(i))=z(b(i))+1
    end do

    c=maxval(y(1:h))
    d=maxval(z(1:w))
    e1=0
    e2=0
    do i=1,h
        if(y(i)==c)then

            e1=e1+1
        end if
    end do
    do i=1,w
        if(z(i)==d)then

            e2=e2+1
        end if
    end do
    e1=e1*e2

    do i=1,m
        if(y(a(i))==c .and. z(b(i))==d)then
            e1=e1-1
        end if
    end do
    if(e1==0)then
            write(*,'(i0)')c+d-1
    else
        write(*,*)c+d
    end if

    stop
end program sample


program sample
    implicit none

    integer(8) :: i,j,k,m,n,h,w
    integer(8)::c,d,e
    integer(8),allocatable::y(:),z(:),a(:),b(:)

    read(*,*) h,w,m
    allocate(y(1:h),z(1:w),a(m),b(m))

    y(:)=0
    z(:)=0
    do i=1,m
        read(*,*)a(i),b(i)

        y(a(i))=y(a(i))+1
        z(b(i))=z(b(i))+1
    end do

    c=maxval(y(1:h))
    d=maxval(z(1:w))
    e=m
    do i=1,m
        if(y(a(i))==c .and. z(b(i))==d)then
            e=e-1
        end if
    end do
    if(e==0)then
            write(*,'(i0)')c+d-1
    else
        write(*,*)c+d
    end if

    stop
end program sample


program sample
    implicit none

    integer(8) :: i,j,k,m,n,h,w
    integer(8)::c,d,e
    integer(8),allocatable::x(:,:),y(:),z(:),a(:),b(:)

    read(*,*) h,w,m
    allocate(x(1:h,1:w),y(1:h),z(1:w),a(m),b(m))
    x(:, :)=0
    y(:)=0
    z(:)=0
    do i=1,m
        read(*,*)a(i),b(i)
        x(a(i),b(i))=1
        y(a(i))=y(a(i))+1
        z(b(i))=z(b(i))+1
    end do

    c=maxval(y(1:h))
    d=maxval(z(1:w))
    e=m
    do i=1,m
        if(y(a(i))==c .and. z(b(i))==d)then
            e=e-1
        end if
    end do
    if(e==0)then
            write(*,'(i0)')c+d-1
    else
        write(*,*)c+d
    end if

    stop
end program sample


program sample
    implicit none

    integer(8) :: i,j,k,m,n,h,w
    integer(8)::c,d
    integer(8),allocatable::x(:,:),y(:),z(:),a(:),b(:)

    read(*,*) h,w,m
    allocate(x(1:h,1:w),y(1:h),z(1:w),a(m),b(m))
    x(:, :)=0
    y(:)=0
    z(:)=0
    do i=1,m
        read(*,*)a(i),b(i)
        x(a(i),b(i))=1
        y(a(i))=y(a(i))+1
        z(b(i))=z(b(i))+1
    end do

    c=maxval(y(1:h))
    d=maxval(z(1:w))
    do i=1,h
        if(y(i)==c)then
            do j=1,w
                if(z(j)==d)then
                    if(a(i)==0 .and. b(j)==0)then
                        write(*,'(i0)')c+d
                        stop
                    end if
                end if
            end do
        end if
    end do
    write(*,'(i0)')c+d-1

    stop
end program sample


program ABC176E
    implicit none
    integer(8)::H,W,M,hi,wi,i,j,hcount=1,wcount=1,hmmax,wmmax,flg=0
    integer(8),allocatable::G(:,:),Hm(:),Wm(:),Hmax(:),Wmax(:)
    read*,H,W,M
    allocate(G(W,H))
    allocate(Hm(H))
    allocate(Wm(W))
    allocate(Hmax(H))
    allocate(Wmax(W))
    G=0
    Hm=0
    Wm=0
    Hmax=0
    Wmax=0
    do i=1,M
        read*,hi,wi
        Hm(hi)=Hm(hi)+1
        Wm(wi)=Wm(wi)+1
        G(wi,hi)=1
    end do

    hmmax=maxval(Hm)
    wmmax=maxval(Wm)

    do i=1,H
        if(Hm(i)==hmmax)then
            Hmax(hcount)=i
            hcount=hcount+1
        end if
    end do

    do i=1,W
        if(Wm(i)==wmmax)then
            Wmax(wcount)=i
            wcount=wcount+1
        end if
    end do

    do i=1,hcount-1
        do j=1,wcount-1
            if(G(Wmax(j),Hmax(i))==0)then
                flg=1
                exit
            end if
        end do
        if(flg==1)exit
    end do
    if(flg==1)then
        print'(i0)',maxval(Hm)+maxval(Wm)
    else
        print'(i0)',maxval(Hm)+maxval(Wm)-1
    end if
end program ABC176E


program ABC176E
    implicit none
    integer(8)::H,W,M,hi,wi,i,j,hcount=1,wcount=1,hmmax,wmmax,flg=0
    integer(8),allocatable::G(:,:),Hm(:),Wm(:),Hmax(:),Wmax(:)
    read*,H,W,M
    allocate(G(W,H))
    allocate(Hm(H))
    allocate(Wm(W))
    allocate(Hmax(H))
    allocate(Wmax(W))
    G=0
    Hm=0
    Wm=0
    Hmax=0
    Wmax=0
    do i=1,M
        read*,hi,wi
        Hm(hi)=Hm(hi)+1
        Wm(wi)=Wm(wi)+1
        G(wi,hi)=1
    end do

    hmmax=maxval(Hm)
    wmmax=maxval(Wm)
    do i=1,H
        if(Hm(i)==hmmax)then
            Hmax(hcount)=i
            hcount=hcount+1
        end if
    end do

    do i=1,W
        if(Wm(i)==wmmax)then
            Wmax(wcount)=i
            wcount=wcount+1
        end if
    end do

    do i=1,hcount-1
        do j=1,wcount-1
            if(G(Wmax(j),Hmax(i))==0)then
                flg=1
                exit
            end if
        end do
    end do
    if(flg==1)then
        print'(i0)',maxval(Hm)+maxval(Wm)
    else
        print'(i0)',maxval(Hm)+maxval(Wm)-1
    end if
end program ABC176E


program sample
    implicit none

    integer(8) :: i,j,k,m,n,h,w
    integer(8)::a,b,c,d
    integer(8),allocatable::x(:,:),y(:),z(:)

    read(*,*) h,w,m
    allocate(x(1:h,1:w),y(1:h),z(1:w))
    x(:, :)=0
    y(:)=0
    z(:)=0
    do i=1,m
        read(*,*)a,b
        x(a,b)=1
        y(a)=y(a)+1
        z(b)=z(b)+1
    end do

    c=int(maxval(y))
    d=int(maxval(z))
    do i=1,h
        if(y(i)==c)then
            do j=1,w
                if(z(j)==d)then
                    if(x(i,j)==0)then
                        write(*,*)c+d
                        stop
                    end if
                end if
            end do
        end if
    end do
    write(*,*)c+d-1

    stop
end program sample


PROGRAM E
  IMPLICIT NONE
  INTEGER,allocatable :: x(:),y(:)
  INTEGER,allocatable :: p(:),q(:)
  INTEGER :: h,w,m
  INTEGER :: i,j,k,tmp=0,score=0

  READ *, h,w,m
  allocate(x(m))
  allocate(y(m))
  allocate(p(h))
  allocate(q(w))
  Do i=1,m
    READ *, x(i),y(i)
  End do

  Do i=1,h
    p(i)=0
  End do
  Do i=1,w
    q(i)=0
  End do
  Do i=1,m
    Do j=1,h
    IF(x(i)==j)p(j)=p(j)+1
    End Do
  End do
  Do i=1,m
    Do j=1,w
    IF(y(i)==j)q(j)=q(j)+1
    End Do
  End do

  Do i=1,h
    Do j=i,w
      score=p(i)+q(j)
      Do k=1,m
        IF(x(k)==i .and. y(k)==j)score=score-1
      End Do
      IF(score>tmp)tmp=score
    End Do
  End Do

  deallocate(x)
  deallocate(y)
  deallocate(p)
  deallocate(q)
  Print *,tmp
END PROGRAM E


program answer
  implicit none
  integer(8) :: i, HH, WW, M, ans, c, d, k, nn, n
  integer(8), allocatable :: h(:), w(:), a(:), b(:), sum(:)
  read(*,*) HH, WW, M
  allocate(h(M))
  allocate(w(M))
  allocate(a(HH))
  allocate(b(WW))
  allocate(sum(M))
  do i = 1, M
     read(*,*) h(i), w(i)
  end do

  a=0
  b=0
  do i = 1, M
     a(h(i))=a(h(i))+1
  end do

  do i = 1, M
     b(w(i))=b(w(i))+1
  end do

  c=maxval(a)
  d=maxval(b)

  do i = 1, HH
     if(a(i)==c) then
        nn=i
        exit
     end if
  end do

  do i = 1, WW
     if(b(i)==d) then
        n=i
        exit
     end if
  end do

  do i = 1, M
     sum(i)=h(i)+w(i)
  end do

  do i = 1, M
     if(sum(i)==c+d.and.h(i)==nn) then
        write(*,*) c+d-1
        stop
     end if
  end do

  !write(*,*) c+d
  deallocate(h)
  deallocate(w)
  deallocate(a)
  deallocate(b)
  stop
 end program answer


program sample
    implicit none

    integer(8) :: i,j,k,m,n,h,w
    integer(8)::a,b,c1,c2
    integer(8),allocatable::x(:,:),y(:),z(:)

    read(*,*) h,w,m
    allocate(x(1:h,1:w),y(1:h),z(1:w))
    x(:, :)=0
    y(:)=0
    z(:)=0
    do i=1,m
        read(*,*)a,b
        x(a,b)=1
        y(a)=y(a)+1
        z(b)=z(b)+1
    end do

    a=maxval(y)
    b=maxval(z)
    do i=1,h
        if(y(i)==a)then
            do j=1,w
                if(z(j)==b)then
                    if(x(i,j)==0)then
                        write(*,*)a+b
                        stop
                    end if
                end if
            end do
        end if
    end do
    write(*,*)a+b-1

    stop
end program sample


      PROGRAM WizardInMaze
      IMPLICIT NONE
      INTEGER,PARAMETER :: p = 8
      integer(p) :: h,w,m,i,rowMax,colMax, ans
      integer(p),allocatable :: row(:),col(:),hData(:),wData(:)
      logical,allocatable :: isRM(:),isCM(:)

      read*,h,w,m
      allocate( row(h),col(w) )
      allocate( hData(m),wData(m) )
      row = 0; col = 0;
      do i = 1,m
        read*,hData(i),wData(i)
        row(hData(i)) = row(hData(i)) + 1
        col(wData(i)) = col(wData(i)) + 1
      end do

      allocate( isRM(h),isCM(w) )
      rowMax = maxval(row); colMax = maxval(col)
      isRM = row==rowMax
      isCM = col==colMax

      ans = rowMax + colMax

      do i = 1,m
        if( isRM(hData(i)).and.isCM(wData(i)) )then
          isRM(hData(i)) = .false.
          isCM(wData(i)) = .false.
        end if
      end do

      if(any([isCM,isRM]))then
        print*,ans
      else
        print*,ans-1
      end if

      stop
      !debugg

      print*,isRM
      print*,[isCM, isRM]

      END PROGRAM


      PROGRAM WizardInMaze
      IMPLICIT NONE
      INTEGER,PARAMETER :: p = 8
      integer(p) :: h,w,m,i,rowMax,colMax, ans
      integer(p),allocatable :: row(:),col(:),hData(:),wData(:)
      logical,allocatable :: isRM(:),isCM(:)

      read*,h,w,m
      allocate( row(h),col(w) )
      allocate( hData(m),wData(m) )
      row = 0; col = 0;
      do i = 1,m
        read*,hData(i),wData(i)
        row(hData(i)) = row(hData(i)) + 1
        col(wData(i)) = col(wData(i)) + 1
      end do

      allocate( isRM(h),isCM(w) )
      rowMax = maxval(row); colMax = maxval(col)
      isRM = row==rowMax
      isCM = col==colMax

      ans = rowMax + colMax

      do i = 1,m
        if( isRM(hData(i)).and.isCM(wData(i)) )then
          isRM(hData(i)) = .false.
          isCM(wData(i)) = .false.
        end if
      end do

      if(all(.not.[isCM,isRM]))then
        print*,ans
      else
        print*,ans-1
      end if

      stop
      !debugg

      print*,isRM
      print*,[isCM, isRM]

      END PROGRAM


program sample
    implicit none
    character(100)::s,t
    integer(8) :: i,j,k,m,n,m2,h,w
    integer(8)::a,b,c1,c2,d1,d2
    integer(8),allocatable::x(:,:),y(:),z(:)

    read(*,*) h,w,m
    allocate(x(h,w),y(h),z(w))
    x(:,:)=0
    do i=1,m
        read(*,*)a,b
        x(a,b)=1
        y(a)=y(a)+1
        z(b)=z(b)+1
    end do
    m=0
    c1=1
    a=maxval(y)
    b=maxval(z)
    do i=1,h
        if(y(i)==a)then
            do j=1,w
                if(z(j)==b)then
                    if(x(i,j)==0)then
                        write(*,*)a+b
                        stop
                    end if
                end if
            end do
        end if
    end do
    write(*,*)a+b-1

        stop
end program sample


program sample
    implicit none
    character(100)::s,t
    integer(8) :: i,j,k,m,n,m2,h,w
    integer(8)::a,b,c1,c2,d1,d2
    integer(8),allocatable::x(:,:),y(:),z(:)

    read(*,*) h,w,m
    allocate(x(h,w),y(h),z(w))
    x(:,:)=0
    do i=1,m
        read(*,*)a,b
        x(a,b)=1
    end do
    m=0
    c1=1
    do i=1,h
        a=0
        do j=1,w
            a=a+x(i,j)
        end do
        if(a>m)then
            m=a
            c1=1
            y(:)=0
            y(c1)=i
            c1=2
        elseif(a==m)then
            y(c1)=i
            c1=c1+1
        endif
    end do
    m2=0
    c2=1
    do j=1,w
        a=0
        do i=1,h
            a=a+x(i,j)
        end do
        if(a>m2)then
            m2=a
            c2=1
            z(:)=0
            z(c2)=j
            c2=2
        elseif(a==m2)then
            z(c2)=j
            c2=c2+1
        endif
    end do
    do i=1,c1-1
        do j=1,c2-1
            if(x(y(i),z(j))==0)then
                write(*,*)m+m2
                stop
            endif
        end do
    end do
    write(*,*)m+m2-1

        stop
end program sample


program main
  implicit none
  integer(8) i,H,W,M,x,y,b,ans,j,Hmax,Wmax,NHmax,NWmax
  integer(8),allocatable :: A(:,:), Hsum(:), Wsum(:), iH(:), iW(:)
  read(*, *)H,W,M
  allocate(A(H,W))
  allocate(Hsum(H))
  allocate(Wsum(W))
  allocate(iH(M))
  allocate(iW(M))
  A = 0
  Hsum = 0
  Wsum = 0
  do i = 1,M
    read(*, *) x,y
    A(x,y) = 1
    iH(i) = x
    iW(i) = y
    Hsum(x) = Hsum(x)+1
    Wsum(y) = Wsum(y)+1
  end do

  Hmax = maxval(Hsum)
  Wmax = maxval(Wsum)

  ans = 1
  do i = 1,H
    if (Hsum(i) == Hmax .and. ans == 1) then
      do j = 1,W
        if (Wsum(j) == Wmax) then
          if (A(i,j) == 0) then
            ans = 0
            exit
          end if
        end if
      end do
    end if
  end do

  write(*, *) Hmax + Wmax - ans
end program main


program noguchi_maisu
    integer n
    integer :: a = 0
    read *, n
    if(mod(n, 1000) == 0)then
        a = n/1000
        print *, a
    else
        a = n/1000 + 1
        print *, a
    end if
end program

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.


program main
    use iso_fortran_env
    implicit none
    integer(int32) :: n

    read *, n

    print *, mod(10000-n, 1000)

end program main


program payment
  implicit none
  integer :: n
  read(*,*) n
  write(*,'(i0)') modulo(-n, 1000)
end program payment


program main
implicit none
	integer(8)::in,out
    read(*,*) in
    out = 1000 - mod(in,1000)
    write(*,*) out
end program main


program abc1
 implicit none
 integer::N,a=0
 read(*,*)N
 a=mod(N,1000)
 if(a==0)then
  print'(i0)',0
 else
  print'(i0)',1000-a
 end if
end program abc1


program main
	implicit none
	integer(8):: A,B,C,N,i,j,t,k
	read *, A
	B=A/1000
    C=A-B*1000
    if(C==0)then
	print'(I0)',0
    else
    print'(I0)',1000-c
    end if

end program main


program abc1
 implicit none
 integer::N,a=0
 read(*,*)N
 a=mod(N,100)
 if(a==0)then
  print'(i0)',0
 else
  print'(i0)',1000-a
 end if
end program abc1


program abc1
 implicit none
 integer::N,a=0
 read(*,*)N
 a=mod(N,100)
 print'(i0)',1000-a
end program abc1


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,p

    read*, n
    p=0
    do while(p < n)
        p=p+1000
    end do

    print'(i0)', p-n
end program main


program probA
  implicit none
  integer :: x, ans
  read(*,*) x

  ans = 1000 - mod(x, 1000)
  if ( mod(x,1000) == 0 ) then
     ans = 0
  end if
  write(*,'(i0)') ans
  stop
end program probA


program main
  implicit none
  integer i
  read(*, *) i
  !read(*, *) (L(i), i = 1,N)

  write(*, *) mod(1000 - mod(i,1000),1000)
end program main


module solver
    implicit none

contains
    !貪欲法による初期解の生成
    subroutine greedy(D,C,S,T,last,vmax)
        integer(8),intent(in)::D,C(26),S(:,:)
        integer(8),intent(out)::vmax
        integer(8),intent(inout)::T(:),last(:,:)
        integer(8),allocatable::last2(:),V(:),Tans(:),last_best(:,:)
        integer(8) dadd,i,i_calc,j,k,l,tbest,score,vbest

        allocate(Tans(D))
        allocate(last_best(26,D))
        allocate(last2(26))
        allocate(V(D))

        do dadd=0,26
            T=0
            V=0
            last=0
            last2=0
            !今日の日付+dadd日目のパラメータを使用して満足度を計算する
            do i=1,D
                i_calc=min(i+dadd,D)
                do j=1,26
                    !得点の初期化
                    if(i==1)V(i)=0
                    if(i>1)V(i)=V(i-1)

                    !lastの初期化
                    if(i==1)last(:,i)=0
                    if(i>1)last(:,i)=last(:,i-1)

                    last(j,i)=i
                    V(i)=V(i)+S(j,i)

                    do k=1,26
                        do l=i,i_calc
                            V(i)=V(i)-C(k)*(l-last(k,i))
                        end do
                    end do

                    if(j==1)then
                        vmax=V(i)
                        tbest=j
                    else if(V(i)>=vmax)then
                        vmax=V(i)
                        tbest=j
                    end if
                end do
                if(i==1)last(:,i)=0
                if(i>1)last(:,i)=last(:,i-1)
                T(i)=tbest
                last(tbest,i)=i
            end do

            !得点の再計算
            score=0
            last2=0
            do i=1,D
                score=score+S(T(i),i)
                last2(T(i))=i
                do j=1,26
                    score=score-C(j)*(i-last2(j))
                end do
            end do

            if(dadd==0.or.score>vbest)then
                vbest=score
                Tans(:)=T(:)
                last_best(:,:)=last(:,:)
            end if
        end do

        vmax=vbest
        T(:)=Tans(:)
        last(:,:)=last_best(:,:)
        return
    end subroutine greedy

    subroutine change_one(D,C,S,T,V,last,vmax)
        integer(8),intent(in)::D,C(26),S(:,:)
        integer(8),intent(inout)::T(D),last(26,D),V(D),vmax
        integer(8)::drand,qrand,contests=26
        integer(8) torigin,i,j,last_sim(26,D),Vorigin(D)

        !乱数の生成
        drand=rnd_int(D)
        qrand=rnd_int(contests)

        last_sim(:,:)=last(:,:)
        Vorigin(:)=V(:)

        !drand日目に行うコンテストをqrandに変更する
        !last_simの書き換え
        torigin=T(drand)

        if(drand==1)then
            last_sim(T(drand),1)=0
            last_sim(qrand,1)=1
        else
            last_sim(T(drand),drand)=last(T(drand),drand-1)
            last_sim(qrand,drand)=drand
        end if

        !日付に対応するコンテストタイプの書き換え
        T(drand)=qrand

        !drand日目の得点の再計算
        if(drand==1)then
            V(drand)=0
        else
            V(drand)=V(drand-1)
        end if
        V(drand)=V(drand)+S(T(drand),drand)
        do i=1,26
            V(drand)=V(drand)-C(i)*(drand-last_sim(i,drand))
        end do

        !drand+1日目以降の得点の再計算
        do i=drand+1,D
            last_sim(:,i)=last_sim(:,i-1)
            V(i)=V(i-1)
            V(i)=V(i)+S(T(i),i)
            last_sim(T(i),i)=i
            do j=1,26
                V(i)=V(i)-C(j)*(i-last_sim(j,i))
            end do
        end do

        if(V(D)>vmax)then
            vmax=V(D)
            last(:,:)=last_sim(:,:)
        else
            V(:)=Vorigin(:)
            T(drand)=torigin
        end if
    end subroutine change_one

    subroutine swap_two(D,C,S,T,V,last,vmax)
        integer(8),intent(in)::D,C(26),S(:,:)
        integer(8),intent(inout)::T(D),last(26,D),V(D),vmax
        integer(8)::swap1,swap2,swap_max,t_swap
        integer(8) i,j,last_sim(26,D),Vorigin(D)

        !swap1とswap2がかぶらないようにかつ差が20以内になるまで乱数を生成
        do
            swap1=rnd_int(D)
            swap2=rnd_int(D)
            if(abs(swap1-swap2)>0.and.abs(swap1-swap2)<=20)exit
        end do

        !swap1<swap2になるようにする
        swap_max=max(swap1,swap2)
        swap1=min(swap1,swap2)
        swap2=swap_max

        !もとの入力を保存
        Vorigin(:)=V(:)
        last_sim(:,:)=last(:,:)

        !T(swap1)をt_swapに格納
        t_swap=T(swap1)

        !swap
        T(swap1)=T(swap2)
        T(swap2)=t_swap

        !swap1から後ろを更新
        do i=swap1,D
            !得点・lastの初期化
            if(i==1)then
                V(i)=0
                last_sim(:,i)=0
            else
                V(i)=V(i-1)
                last_sim(:,i)=last_sim(:,i-1)
            end if
            last_sim(T(i),i)=i
            V(i)=V(i)+S(T(i),i)

            do j=1,26
                V(i)=V(i)-C(j)*(i-last_sim(j,i))
            end do
        end do

        if(V(D)>vmax)then
            vmax=V(D)
            last(:,:)=last_sim(:,:)
        else
            V(:)=Vorigin(:)
            T(swap2)=T(swap1)
            T(swap1)=t_swap
        end if
    end subroutine swap_two

    !整数の乱数生成
    function rnd_int(rnd_max)
        integer(8),intent(in)::rnd_max
        integer(8)::rnd_int
        real::rnd_real
        call random_number(rnd_real)
        rnd_int=mod(int(rnd_real*100000000),rnd_max)+1
        return
    end function
end module solver

program scoring
    use solver
    implicit none
    integer(8)::t1,t2,t_rate,t_max,diff!実行時間の計測関連
    integer(8)::D,C(26),last(26,365),last_sim(26,365),vmax,i,select_mode,modes=2
    integer(8)::steps=0!ベンチマーク用
    integer(8),allocatable::S(:,:),T(:),Tans(:),V(:),Vorigin(:)
    !時間計測開始
    call system_clock(t1)

    !入力
    read*,D,C
    allocate(S(26,D))
    allocate(T(D))
    allocate(Tans(D))
    allocate(V(D))
    allocate(Vorigin(D))
    read*,S
    T=0
    V=0
    last=0
    last_sim=0

    !貪欲法による初期解の生成
    call greedy(D,C,S,T,last,vmax)
    call system_clock(t2,t_rate,t_max)
    diff=t2-t1

    do
        if(diff>1990*t_rate/1000)exit
        steps=steps+1

        !1点変更か2点スワップかランダムに決める
        select_mode=rnd_int(modes)
        if(select_mode==1)call change_one(D,C,S,T,V,last,vmax)
        if(select_mode==2)call swap_two(D,C,S,T,V,last,vmax)

        !時間計測
        call system_clock(t2,t_rate,t_max)
        diff=t2-t1
    end do
    !print*,diff/dble(t_rate),steps

    !答えの出力
    do i=1,D
        print'(i0)',T(i)
    end do
contains

end program scoring

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.


module solver
    implicit none

contains
    !貪欲法による初期解の生成
    subroutine greedy(D,C,S,T,last,vmax)
        integer(8),intent(in)::D,C(26),S(:,:)
        integer(8),intent(out)::vmax
        integer(8),intent(inout)::T(:),last(:,:)
        integer(8),allocatable::last2(:),V(:),Tans(:),last_best(:,:)
        integer(8) dadd,i,i_calc,j,k,l,tbest,score,vbest

        allocate(Tans(D))
        allocate(last_best(26,D))
        allocate(last2(26))
        allocate(V(D))

        do dadd=0,26
            T=0
            V=0
            last=0
            last2=0
            !今日の日付+dadd日目のパラメータを使用して満足度を計算する
            do i=1,D
                i_calc=min(i+dadd,D)
                do j=1,26
                    !得点の初期化
                    if(i==1)V(i)=0
                    if(i>1)V(i)=V(i-1)

                    !lastの初期化
                    if(i==1)last(:,i)=0
                    if(i>1)last(:,i)=last(:,i-1)

                    last(j,i)=i
                    V(i)=V(i)+S(j,i)

                    do k=1,26
                        do l=i,i_calc
                            V(i)=V(i)-C(k)*(l-last(k,i))
                        end do
                    end do

                    if(j==1)then
                        vmax=V(i)
                        tbest=j
                    else if(V(i)>=vmax)then
                        vmax=V(i)
                        tbest=j
                    end if
                end do
                if(i==1)last(:,i)=0
                if(i>1)last(:,i)=last(:,i-1)
                T(i)=tbest
                last(tbest,i)=i
            end do

            !得点の再計算
            score=0
            last2=0
            do i=1,D
                score=score+S(T(i),i)
                last2(T(i))=i
                do j=1,26
                    score=score-C(j)*(i-last2(j))
                end do
            end do

            if(dadd==0.or.score>vbest)then
                vbest=score
                Tans(:)=T(:)
                last_best(:,:)=last(:,:)
            end if
        end do

        vmax=vbest
        T(:)=Tans(:)
        last(:,:)=last_best(:,:)
        return
    end subroutine greedy

    subroutine change_one(D,C,S,T,V,last,vmax)
        integer(8),intent(in)::D,C(26),S(:,:)
        integer(8),intent(inout)::T(D),last(26,D),V(D),vmax
        integer(8)::drand,qrand,contests=26
        integer(8) torigin,i,j,last_sim(26,D),Vorigin(D)

        !乱数の生成
        drand=rnd_int(D)
        qrand=rnd_int(contests)

        last_sim(:,:)=last(:,:)
        Vorigin(:)=V(:)

        !drand日目に行うコンテストをqrandに変更する
        !last_simの書き換え
        torigin=T(drand)

        if(drand==1)then
            last_sim(T(drand),1)=0
            last_sim(qrand,1)=1
        else
            last_sim(T(drand),drand)=last(T(drand),drand-1)
            last_sim(qrand,drand)=drand
        end if

        !日付に対応するコンテストタイプの書き換え
        T(drand)=qrand

        !drand日目の得点の再計算
        if(drand==1)then
            V(drand)=0
        else
            V(drand)=V(drand-1)
        end if
        V(drand)=V(drand)+S(T(drand),drand)
        do i=1,26
            V(drand)=V(drand)-C(i)*(drand-last_sim(i,drand))
        end do

        !drand+1日目以降の得点の再計算
        do i=drand+1,D
            last_sim(:,i)=last_sim(:,i-1)
            V(i)=V(i-1)
            V(i)=V(i)+S(T(i),i)
            last_sim(T(i),i)=i
            do j=1,26
                V(i)=V(i)-C(j)*(i-last_sim(j,i))
            end do
        end do

        if(V(D)>vmax)then
            vmax=V(D)
            last(:,:)=last_sim(:,:)
        else
            V(:)=Vorigin(:)
            T(drand)=torigin
        end if
    end subroutine change_one

    subroutine swap_two(D,C,S,T,V,last,vmax)
        integer(8),intent(in)::D,C(26),S(:,:)
        integer(8),intent(inout)::T(D),last(26,D),V(D),vmax
        integer(8)::swap1,swap2,swap_max,t_swap
        integer(8) torigin,i,j,last_sim(26,D),Vorigin(D)

        !swap1とswap2がかぶらないようにかつ差が16以内になるまで乱数を生成
        do
            swap1=rnd_int(D)
            swap2=rnd_int(D)
            if(abs(swap1-swap2)>0.and.abs(swap1-swap2)<=16)exit
        end do

        !swap1<swap2になるようにする
        swap_max=max(swap1,swap2)
        swap1=min(swap1,swap2)
        swap2=swap_max

        !もとの入力を保存
        Vorigin(:)=V(:)
        last_sim(:,:)=last(:,:)

        !T(swap1)をt_swapに格納
        t_swap=T(swap1)

        !swap
        T(swap1)=T(swap2)
        T(swap2)=t_swap

        !swap1から後ろを更新
        do i=swap1,D
            !得点・lastの初期化
            if(i==1)then
                V(i)=0
                last_sim(:,i)=0
            else
                V(i)=V(i-1)
                last_sim(:,i)=last_sim(:,i-1)
            end if
            last_sim(T(i),i)=i
            V(i)=V(i)+S(T(i),i)

            do j=1,26
                V(i)=V(i)-C(j)*(i-last_sim(j,i))
            end do
        end do

        if(V(D)>vmax)then
            vmax=V(D)
            last(:,:)=last_sim(:,:)
        else
            V(:)=Vorigin(:)
            T(swap2)=T(swap1)
            T(swap1)=t_swap
        end if
    end subroutine swap_two

    !整数の乱数生成
    function rnd_int(rnd_max)
        integer(8),intent(in)::rnd_max
        integer(8)::rnd_int
        real::rnd_real
        call random_number(rnd_real)
        rnd_int=mod(int(rnd_real*100000000),rnd_max)+1
        return
    end function
end module solver

program scoring
    use solver
    implicit none
    integer(8)::t1,t2,t_rate,t_max,diff!実行時間の計測関連
    integer(8)::D,C(26),last(26,365),last_sim(26,365),vmax,i,select_mode,modes=2
    integer(8)::steps=0!ベンチマーク用
    integer(8),allocatable::S(:,:),T(:),Tans(:),V(:),Vorigin(:)
    !時間計測開始
    call system_clock(t1)

    !入力
    read*,D,C
    allocate(S(26,D))
    allocate(T(D))
    allocate(Tans(D))
    allocate(V(D))
    allocate(Vorigin(D))
    read*,S
    T=0
    V=0
    last=0
    last_sim=0

    !貪欲法による初期解の生成
    call greedy(D,C,S,T,last,vmax)
    call system_clock(t2,t_rate,t_max)
    diff=t2-t1

    do
        if(diff>1990*t_rate/1000)exit
        steps=steps+1

        !1点変更か2点スワップかランダムに決める
        select_mode=rnd_int(modes)
        if(select_mode==1)call change_one(D,C,S,T,V,last,vmax)
        if(select_mode==2)call swap_two(D,C,S,T,V,last,vmax)

        !時間計測
        call system_clock(t2,t_rate,t_max)
        diff=t2-t1
    end do
    !print*,diff/dble(t_rate),steps

    !答えの出力
    do i=1,D
        print'(i0)',T(i)
    end do
contains

end program scoring


module solver
    implicit none

contains
    !貪欲法による初期解の生成
    subroutine greedy(D,C,S,T,last,vmax)
        integer(8),intent(in)::D,C(26),S(:,:)
        integer(8),intent(out)::vmax
        integer(8),intent(inout)::T(:),last(:,:)
        integer(8),allocatable::last2(:),V(:),Tans(:),last_best(:,:)
        integer(8) dadd,i,i_calc,j,k,l,tbest,score,vbest

        allocate(Tans(D))
        allocate(last_best(26,D))
        allocate(last2(26))
        allocate(V(D))

        do dadd=0,26
            T=0
            V=0
            last=0
            last2=0
            !今日の日付+dadd日目のパラメータを使用して満足度を計算する
            do i=1,D
                i_calc=min(i+dadd,D)
                do j=1,26
                    !得点の初期化
                    if(i==1)V(i)=0
                    if(i>1)V(i)=V(i-1)

                    !lastの初期化
                    if(i==1)last(:,i)=0
                    if(i>1)last(:,i)=last(:,i-1)

                    last(j,i)=i
                    V(i)=V(i)+S(j,i)

                    do k=1,26
                        do l=i,i_calc
                            V(i)=V(i)-C(k)*(l-last(k,i))
                        end do
                    end do

                    if(j==1)then
                        vmax=V(i)
                        tbest=j
                    else if(V(i)>=vmax)then
                        vmax=V(i)
                        tbest=j
                    end if
                end do
                if(i==1)last(:,i)=0
                if(i>1)last(:,i)=last(:,i-1)
                T(i)=tbest
                last(tbest,i)=i
            end do

            !得点の再計算
            score=0
            last2=0
            do i=1,D
                score=score+S(T(i),i)
                last2(T(i))=i
                do j=1,26
                    score=score-C(j)*(i-last2(j))
                end do
            end do

            if(dadd==0.or.score>vbest)then
                vbest=score
                Tans(:)=T(:)
                last_best(:,:)=last(:,:)
            end if
        end do

        vmax=vbest
        T(:)=Tans(:)
        last(:,:)=last_best(:,:)
        return
    end subroutine greedy

    subroutine change_one(D,C,S,T,V,last,vmax)
        integer(8),intent(in)::D,C(26),S(:,:)
        integer(8),intent(inout)::T(D),last(26,D),V(D),vmax
        integer(8)::drand,qrand,contests=26
        integer(8) torigin,i,j,last_sim(26,D),Vorigin(D)

        !乱数の生成
        drand=rnd_int(D)
        qrand=rnd_int(contests)

        last_sim(:,:)=last(:,:)
        Vorigin(:)=V(:)

        !drand日目に行うコンテストをqrandに変更する
        !last_simの書き換え
        torigin=T(drand)

        if(drand==1)then
            last_sim(T(drand),1)=0
            last_sim(qrand,1)=1
        else
            last_sim(T(drand),drand)=last(T(drand),drand-1)
            last_sim(qrand,drand)=drand
        end if

        !日付に対応するコンテストタイプの書き換え
        T(drand)=qrand

        !drand日目の得点の再計算
        if(drand==1)then
            V(drand)=0
        else
            V(drand)=V(drand-1)
        end if
        V(drand)=V(drand)+S(T(drand),drand)
        do i=1,26
            V(drand)=V(drand)-C(i)*(drand-last_sim(i,drand))
        end do

        !drand+1日目以降の得点の再計算
        do i=drand+1,D
            last_sim(:,i)=last_sim(:,i-1)
            V(i)=V(i-1)
            V(i)=V(i)+S(T(i),i)
            last_sim(T(i),i)=i
            do j=1,26
                V(i)=V(i)-C(j)*(i-last_sim(j,i))
            end do
        end do

        if(V(D)>vmax)then
            vmax=V(D)
            last(:,:)=last_sim(:,:)
        else
            V(:)=Vorigin(:)
            T(drand)=torigin
        end if
    end subroutine change_one

    !整数の乱数生成
    function rnd_int(rnd_max)
        integer(8),intent(in)::rnd_max
        integer(8)::rnd_int
        real::rnd_real
        call random_number(rnd_real)
        rnd_int=mod(int(rnd_real*100000000),rnd_max)+1
        return
    end function
end module solver

program scoring
    use solver
    implicit none
    integer(8)::t1,t2,t_rate,t_max,diff!実行時間の計測関連
    integer(8)::D,C(26),last(26,365),last_sim(26,365),vmax,i
    integer(8)::steps=0!ベンチマーク用
    integer(8),allocatable::S(:,:),T(:),Tans(:),V(:),Vorigin(:)
    !時間計測開始
    call system_clock(t1)

    !入力
    read*,D,C
    allocate(S(26,D))
    allocate(T(D))
    allocate(Tans(D))
    allocate(V(D))
    allocate(Vorigin(D))
    read*,S
    T=0
    V=0
    last=0
    last_sim=0

    !貪欲法による初期解の生成
    call greedy(D,C,S,T,last,vmax)
    call system_clock(t2,t_rate,t_max)
    diff=t2-t1

    do
        if(diff>1990*t_rate/1000)exit
        steps=steps+1

        call change_one(D,C,S,T,V,last,vmax)

        !時間計測
        call system_clock(t2,t_rate,t_max)
        diff=t2-t1
    end do
    !print*,diff/dble(t_rate),steps

    !答えの出力
    do i=1,D
        print'(i0)',T(i)
    end do
contains

end program scoring


program scoring
    implicit none
    integer(8)::t1,t2,t_rate,t_max,diff!実行時間の計測関連
    integer(8)::D,C(26),last(26,365),last_sim(26,365),i,j,k,vmax,tbest,torigin
    integer(8)::drand,qrand!局所探索法の書き換え関連
    integer(8)::contests=26
    integer(8)::steps=0!ベンチマーク用
    integer(8),allocatable::S(:,:),T(:),V(:),Vorigin(:)
    !時間計測開始
    call system_clock(t1)

    !入力
    read*,D,C
    allocate(S(26,D))
    allocate(T(D))
    allocate(V(D))
    allocate(Vorigin(D))
    read*,S
    T=0
    V=0
    last=0
    last_sim=0

    !貪欲法による初期解の生成
    do i=1,D
        do j=1,26
            !得点の初期化
            if(i==1)V(i)=0
            if(i>1)V(i)=V(i-1)

            !lastの初期化
            if(i==1)last(:,i)=0
            if(i>1)last(:,i)=last(:,i-1)

            last(j,i)=i
            V(i)=V(i)+S(j,i)

            do k=1,26
                V(i)=V(i)-C(k)*(i-last(k,i))
            end do

            if(j==1)then
                vmax=V(i)
                tbest=j
            else if(V(i)>=vmax)then
                vmax=V(i)
                tbest=j
            end if
        end do
        if(i==1)last(:,i)=0
        if(i>1)last(:,i)=last(:,i-1)
        T(i)=tbest
        last(tbest,i)=i
    end do
    vmax=V(D)
    call system_clock(t2,t_rate,t_max)
    diff=t2-t1

    do
        if(diff>=2000000000)exit
        steps=steps+1

        !乱数の生成
        drand=rnd_int(D)
        qrand=rnd_int(contests)

        last_sim(:,:)=last(:,:)
        Vorigin(:)=V(:)
        !drand日目に行うコンテストをqrandに変更する
        !last_simの書き換え
        torigin=T(drand)
        if(drand==1)then
            last_sim(T(drand),1)=0
            last_sim(qrand,1)=1
        else
            last_sim(T(drand),drand)=last(T(drand),drand-1)
            last_sim(qrand,drand)=drand
        end if
        !日付に対応するコンテストタイプの書き換え
        T(drand)=qrand

        !drand日目の得点の再計算
        if(drand==1)then
            V(drand)=0
        else
            V(drand)=V(drand-1)
        end if
        V(drand)=V(drand)+S(T(drand),drand)
        do i=1,26
            V(drand)=V(drand)-C(i)*(drand-last_sim(i,drand))
        end do

        !drand+1日目以降の得点の再計算
        do i=drand+1,D
            last_sim(:,i)=last_sim(:,i-1)
            V(i)=V(i-1)
            V(i)=V(i)+S(T(i),i)
            last_sim(T(i),i)=i
            do j=1,26
                V(i)=V(i)-C(j)*(i-last_sim(j,i))
            end do
        end do

        if(V(D)>vmax)then
            vmax=V(D)
            last(:,:)=last_sim
        else
            V(:)=Vorigin(:)
            T(drand)=torigin
        end if
        !時間計測
        call system_clock(t2,t_rate,t_max)
        diff=t2-t1
    end do
    !print*,diff/dble(t_rate),steps
    !答えの出力
    do i=1,D
        print'(i0)',T(i)
    end do
contains
function rnd_int(rnd_max)
    integer(8),intent(in)::rnd_max
    integer(8)::rnd_int
    real::rnd_real
    call random_number(rnd_real)
    rnd_int=mod(int(rnd_real*100000000),rnd_max)+1
    return
end function
end program scoring


program scoring
    implicit none
    integer(8)::t1,t2,t_rate,t_max,diff!実行時間の計測関連
    integer(8)::D,C(26),last(26,365),last_sim(26,365),i,j,k,vmax,tbest,torigin
    integer(8)::drand,qrand!局所探索法の書き換え関連
    integer(8)::contests=26
    integer(8)::steps=0!ベンチマーク用
    integer(8),allocatable::S(:,:),T(:),V(:),Vorigin(:)
    !時間計測開始
    call system_clock(t1)

    !入力
    read*,D,C
    allocate(S(26,D))
    allocate(T(D))
    allocate(V(D))
    allocate(Vorigin(D))
    read*,S
    T=0
    V=0
    last=0
    last_sim=0

    !貪欲法による初期解の生成
    do i=1,D
        do j=1,26
            !得点の初期化
            if(i==1)V(i)=0
            if(i>1)V(i)=V(i-1)

            !lastの初期化
            if(i==1)last(:,i)=0
            if(i>1)last(:,i)=last(:,i-1)

            last(j,i)=i
            V(i)=V(i)+S(j,i)

            do k=1,26
                V(i)=V(i)-C(k)*(i-last(k,i))
            end do

            if(j==1)then
                vmax=V(i)
                tbest=j
            else if(V(i)>=vmax)then
                vmax=V(i)
                tbest=j
            end if
        end do
        if(i==1)last(:,i)=0
        if(i>1)last(:,i)=last(:,i-1)
        T(i)=tbest
        last(tbest,i)=i
    end do
    vmax=V(D)
    call system_clock(t2,t_rate,t_max)
    diff=t2-t1

    do
        if(diff>1990000000)exit
		steps=steps+1
        !乱数の生成
        drand=rnd_int(D)
        qrand=rnd_int(contests)

        last_sim(:,:)=last(:,:)
        Vorigin(:)=V(:)
        !drand日目に行うコンテストをqrandに変更する
        !last_simの書き換え
        torigin=T(drand)
        if(drand==1)then
            last_sim(T(drand),1)=0
            last_sim(qrand,1)=1
        else
            last_sim(T(drand),drand)=last(T(drand),drand-1)
            last_sim(qrand,drand)=drand
        end if
        !日付に対応するコンテストタイプの書き換え
        T(drand)=qrand

        !drand日目の得点の再計算
        if(drand==1)then
            V(drand)=0
        else
            V(drand)=V(drand-1)
        end if
        V(drand)=V(drand)+S(T(drand),drand)
        do i=1,26
            V(drand)=V(drand)-C(i)*(drand-last(i,drand))
        end do

        !drand+1日目以降の得点の再計算
        do i=drand+1,D
            last_sim(:,i)=last(:,i-1)
            V(i)=V(i-1)
            V(i)=V(i)+S(T(i),i)
            last(T(i),i)=i
            do j=1,26
                V(i)=V(i)-C(j)*(i-last(j,i))
            end do
        end do

        if(V(D)>vmax)then
            vmax=V(D)
            last(:,:)=last_sim
        else
            V(:)=Vorigin(:)
            T(drand)=torigin
        end if
        !時間計測
        call system_clock(t2,t_rate,t_max)
        diff=t2-t1
    end do
    !print*,diff/dble(t_rate),steps
    !答えの出力
    do i=1,D
        print'(i0)',T(i)
    end do
contains
function rnd_int(rnd_max)
    integer(8),intent(in)::rnd_max
    integer(8)::rnd_int
    real::rnd_real
    call random_number(rnd_real)
    rnd_int=mod(int(rnd_real*100000000),rnd_max)+1
    return
end function
end program scoring


program scoring
    implicit none
    integer(8)::t1,t2,t_rate,t_max,diff!実行時間の計測関連
    integer(8)::D,C(26),last(26,365),last_sim(26,365),i,j,k,vmax,tbest,torigin
    integer(8)::drand,qrand!局所探索法の書き換え関連
    integer(8)::contests=26
    integer(8)::steps=0!ベンチマーク用
    integer(8),allocatable::S(:,:),T(:),V(:),Vorigin(:)
    !時間計測開始
    call system_clock(t1)

    !入力
    read*,D,C
    allocate(S(26,D))
    allocate(T(D))
    allocate(V(D))
    allocate(Vorigin(D))
    read*,S
    T=0
    V=0
    last=0
    last_sim=0

    !貪欲法による初期解の生成
    do i=1,D
        do j=1,26
            !得点の初期化
            if(i==1)V(i)=0
            if(i>1)V(i)=V(i-1)

            !lastの初期化
            if(i==1)last(:,i)=0
            if(i>1)last(:,i)=last(:,i-1)

            last(j,i)=i
            V(i)=V(i)+S(j,i)

            do k=1,26
                V(i)=V(i)-C(k)*(i-last(k,i))
            end do

            if(j==1)then
                vmax=V(i)
                tbest=j
            else if(V(i)>=vmax)then
                vmax=V(i)
                tbest=j
            end if
        end do
        if(i==1)last(:,i)=0
        if(i>1)last(:,i)=last(:,i-1)
        T(i)=tbest
        last(tbest,i)=i
    end do
    vmax=V(D)
    call system_clock(t2,t_rate,t_max)
    diff=t2-t1

    do
        if(diff>19900000)exit

        !乱数の生成
        drand=rnd_int(D)
        qrand=rnd_int(contests)

        last_sim(:,:)=last(:,:)
        Vorigin(:)=V(:)
        !drand日目に行うコンテストをqrandに変更する
        !last_simの書き換え
        torigin=T(drand)
        if(drand==1)then
            last_sim(T(drand),1)=0
            last_sim(qrand,1)=1
        else
            last_sim(T(drand),drand)=last(T(drand),drand-1)
            last_sim(qrand,drand)=drand
        end if
        !日付に対応するコンテストタイプの書き換え
        T(drand)=qrand

        !drand日目の得点の再計算
        if(drand==1)then
            V(drand)=0
        else
            V(drand)=V(drand-1)
        end if
        V(drand)=V(drand)+S(T(drand),drand)
        do i=1,26
            V(drand)=V(drand)-C(i)*(drand-last(i,drand))
        end do

        !drand+1日目以降の得点の再計算
        do i=drand+1,D
            last_sim(:,i)=last(:,i-1)
            V(i)=V(i-1)
            V(i)=V(i)+S(T(i),i)
            last(T(i),i)=i
            do j=1,26
                V(i)=V(i)-C(j)*(i-last(j,i))
            end do
        end do

        if(V(D)>vmax)then
            vmax=V(D)
            last(:,:)=last_sim
            Vorigin(:)=V
        else
            T(drand)=torigin
        end if
        !時間計測
        call system_clock(t2,t_rate,t_max)
        diff=t2-t1
    end do

    !答えの出力
    do i=1,D
        print'(i0)',T(i)
    end do
contains
function rnd_int(rnd_max)
    integer(8),intent(in)::rnd_max
    integer(8)::rnd_int
    real::rnd_real
    call random_number(rnd_real)
    rnd_int=mod(int(rnd_real*100000000),rnd_max)+1
    return
end function
end program scoring


program scoring
    implicit none
    integer(8)::D,C(26),last(26),last_sim(26),i,j,k,vcalc
    integer(8),allocatable::S(:,:),T(:),V(:)
    !入力
    read*,D,C
    allocate(S(26,D))
    allocate(T(D))
    allocate(V(D))
    read*,S
    T=0
    V=0
    last=0
    last_sim=0

    !各日にちについて貪欲に計算
    do i=1,D
        !すべての選択肢を試す
        do j=1,26
            !vcalcとlast_simを初期化
            if(i==1)then
                vcalc=V(i)
            else
                vcalc=V(i-1)
            end if
            last_sim=last

            !得点をシミュレート
            vcalc=vcalc+S(j,i)
            last_sim(j)=i
            do k=1,26
                vcalc=vcalc-C(k)*(i-last_sim(k))
            end do

            !比較していって最大値を更新したらVに格納する
            if(vcalc>=V(i))then
                V(i)=vcalc
                T(i)=j
            end if
        end do
        !最終的に選んだ選択肢をもとに更新
        last(T(i))=i
    end do

    !答えの出力
    do i=1,D
        print'(i0)',T(i)
    end do
end program scoring


program pleonexia
    implicit none
    integer D, day, wait, i, candidate, maxScore, tempScore
    integer, dimension(26) :: c, last
    integer, dimension(:, :), allocatable :: satisfy
    integer, dimension(:), allocatable :: typ

    read *, D
    read *, c
    last(:) = 0

    allocate(satisfy(26, D))
    allocate(typ(D))

    read *, satisfy

    ! typ(:) = 1
    ! print *, score(1, c, satisfy, D, typ)

    do day=1,D
        candidate = 1
        typ(day) = 1
        maxScore = score(1, c, satisfy, D, typ)
        do i=2,26
            typ(day) = i
            tempScore = score(day, c, satisfy, D, typ)
            if (tempScore > maxScore) then
                maxScore = tempScore
                candidate = i
            endif
        enddo
        typ(day) = candidate
    enddo

    do day=1,D
        print *, typ(day)
    enddo

    contains
    integer function score(date, c, satisfy, D, typ)
    implicit none
    integer, intent(in) :: date, D
    integer, intent(in), dimension(26) :: c
    integer, intent(in), dimension(:, :) :: satisfy
    integer, intent(in), dimension(:) :: typ
    integer day, wait, result
    integer, dimension(26) :: last

    last(:) = 0

    result = 0
    do day=1,date
        last(typ(day)) = day
        wait = 0
        do i=1,26
            wait = wait + c(i)*(day - last(i))
        enddo
        result = result + satisfy(typ(day), day) - wait
    enddo
    score = result
end function score

end program pleonexia


program atcoder_contest_scheduling
  implicit none
  integer, parameter :: maxday = 365, maxtype = 26
  integer :: d, c(maxtype), s(maxtype, maxday), t(maxday), u(maxday)
  integer :: m, n, p, i
  real(8) :: t1, t2
  call cpu_time(t1)
  call random_seed_clock()
  read(*,*) d
  read(*,*) c
  read(*,*) (s(:, i), i = 1, d)
  t = (/(maxloc(s(:, i), 1), i = 1, d)/)
  m = score()
  u = t
  do
    p = next_int(d)
    do i = 1, maxtype
      t(p) = i
      n = score()
      if (m < n) then
        m = n
        u(p) = i
      else
        t(p) = u(p)
      end if
      call cpu_time(t2)
      if (t2 - t1 > 1.979d0) goto 100
    end do
  end do
  100 write(*,'(i0)') (u(i), i = 1, d)
contains
  integer function score() result(res)
    integer :: last(maxtype), i
    last = 0
    res = 0
    do i = 1, d
      last(t(i)) = i
      res = res + s(t(i), i) + dot_product(c, last - i)
    end do
  end
  subroutine random_seed_clock()
    integer :: nseed, clock, seed(100)
    call system_clock(clock)
    call random_seed(size = nseed)
    seed(1:nseed) = clock
    call random_seed(put = seed(1:nseed))
  end
  integer function next_int(m) result(res)
    integer, intent(in)  :: m
    real(8) :: r
    call random_number(r)
    res = int(m * r) + 1
  end
end program atcoder_contest_scheduling


program atcoder_contest_scheduling
  implicit none
  integer :: d, c(26) = 0, s(26, 365) = 0, t(365) = 0, u(365), m, n, p, i
  real(8) :: t1, t2
  call cpu_time(t1)
  call random_seed_clock()
  read(*,*) d
  read(*,*) c
  read(*,*) (s(:, i), i = 1, d)
  t = [(maxloc(s(:, i), 1), i = 1, d)]
  m = score()
  u = t
  do
    p = next_int(365)
    do i = 1, 26
      t(p) = i
      n = score()
      if (m < n) then
        m = n
        u(p) = i
      else
        t(p) = u(p)
      end if
      call cpu_time(t2)
      if (t2 - t1 > 1.975d0) goto 100
    end do
  end do
  100 write(*,'(i0)') (u(i), i = 1, d)
contains
  integer function score() result(res)
    integer :: last(26), i
    last = 0
    res = 0
    do i = 1, d
      last(t(i)) = i
      res = res + s(t(i), i) - sum(c * (i - last))
    end do
  end
  subroutine random_seed_clock()
    integer :: nseed, clock
    integer, allocatable :: seed(:)
    call system_clock(clock)
    call random_seed(size = nseed)
    allocate(seed(nseed))
    seed = clock
    call random_seed(put = seed)
    deallocate(seed)
  end
  integer function next_int(m) result(res)
    integer, intent(in)  :: m
    real(8) :: r
    call random_number(r)
    res = int(m * r) + 1
  end
end program atcoder_contest_scheduling


program atcoder_contest_scheduling
  implicit none
  integer :: d, c(26) = 0, s(26, 365) = 0, t(365) = 0, u(365)
  integer :: mem(0:365) = 0, diff(26, 0:365) = 0
  integer :: m, n, x = 0, y, p, q, i
  real(8) :: t1, t2
  call cpu_time(t1)
  call random_seed_clock()
  read(*,*) d
  read(*,*) c
  read(*,*) (s(:, i), i = 1, d)
  t = [(maxloc(s(:, i), 1), i = 1, d)]
  do i = 1, d
    x = x + s(t(i), i)
  end do
  m = score(x, 1)
  u = t
  do
    p = next_int(365)
    q = next_int(26)
    t(p) = q
    y = x - s(u(p), p) + s(q, p)
    n = score(y, p)
    if (m < n) then
      x = y
      m = n
      u(p) = q
    else
      t(p) = u(p)
    end if
    call cpu_time(t2)
    if (t2 - t1 > 1.975d0) exit
  end do
  write(*,'(i0)') (u(i), i = 1, d)
contains
  integer function score(x, p) result(res)
    integer, intent(in) :: x, p
    integer :: i
    do i = p, d
      diff(:, i) = diff(:, i - 1) + c
      diff(t(i), i) = 0
      mem(i) = mem(i - 1) - sum(diff(:, i))
    end do
    res = x + mem(d)
  end
  subroutine random_seed_clock()
    integer :: nseed, clock
    integer, allocatable :: seed(:)
    call system_clock(clock)
    call random_seed(size = nseed)
    allocate(seed(nseed))
    seed = clock
    call random_seed(put = seed)
    deallocate(seed)
  end
  integer function next_int(m) result(res)
    integer, intent(in)  :: m
    real(8) :: r
    call random_number(r)
    res = int(m * r) + 1
  end
end program atcoder_contest_scheduling


program atcoder_contest_scheduling
  implicit none
  integer :: d, c(26) = 0, s(26, 365) = 0, t(365) = 0, u(365)
  integer :: m, n, x = 0, y, p, q, i
  real(8) :: t1, t2
  call cpu_time(t1)
  call random_seed_clock()
  read(*,*) d
  read(*,*) c
  read(*,*) (s(:, i), i = 1, d)
  t = [(maxloc(s(:, i), 1), i = 1, d)]
  do i = 1, d
    x = x + s(t(i), i)
  end do
  m = score(x, 1)
  u = t
  do
    p = next_int(365)
    q = next_int(26)
    t(p) = q
    y = x - s(u(p), p) + s(q, p)
    n = score(y, p)
    if (m < n) then
      x = y
      m = n
      u(p) = q
    else
      t(p) = u(p)
    end if
    call cpu_time(t2)
    if (t2 - t1 > 1.975d0) exit
  end do
  write(*,'(i0)') (u(i), i = 1, d)
contains
  integer function score(x, p) result(res)
    integer, intent(in) :: x, p
    integer, save :: mem(0:365) = 0, diff(26, 0:365) = 0
    integer :: i
    do i = p, d
      diff(:, i) = diff(:, i - 1) + c
      diff(t(i), i) = 0
      mem(i) = mem(i - 1) - sum(diff(:, i))
    end do
    res = x + mem(d)
  end
  subroutine random_seed_clock()
    integer :: nseed, clock
    integer, allocatable :: seed(:)
    call system_clock(clock)
    call random_seed(size = nseed)
    allocate(seed(nseed))
    seed = clock
    call random_seed(put = seed)
    deallocate(seed)
  end
  integer function next_int(m) result(res)
    integer, intent(in)  :: m
    real(8) :: r
    call random_number(r)
    res = int(m * r) + 1
  end
end program atcoder_contest_scheduling


program atcoder_contest_scheduling
  implicit none
  integer :: d, c(26) = 0, s(26, 365) = 0, t(365) = 0, u(365), m, n, p, q, i
  real(8) :: t1, t2
  call cpu_time(t1)
  call random_seed_clock()
  read(*,*) d
  read(*,*) c
  read(*,*) (s(:, i), i = 1, d)
  t = [(maxloc(s(:, i), 1), i = 1, d)]
  m = score()
  u = t
  do
    p = next_int(365)
    q = next_int(26)
    t(p) = q
    n = score()
    if (m < n) then
      m = n
      u(p) = q
    else
      t(p) = u(p)
    end if
    call cpu_time(t2)
    if (t2 - t1 > 1.975d0) exit
  end do
  write(*,'(i0)') (u(i), i = 1, d)
contains
  integer function score() result(res)
    integer :: last(26), i
    last = 0
    res = 0
    do i = 1, d
      last(t(i)) = i
      res = res + s(t(i), i) - sum(c * (i - last))
    end do
  end
  subroutine random_seed_clock()
    integer :: nseed, clock
    integer, allocatable :: seed(:)
    call system_clock(clock)
    call random_seed(size = nseed)
    allocate(seed(nseed))
    seed = clock
    call random_seed(put = seed)
    deallocate(seed)
  end
  integer function next_int(m) result(res)
    integer, intent(in)  :: m
    real(8) :: r
    call random_number(r)
    res = int(m * r) + 1
  end
end program atcoder_contest_scheduling


program main
  implicit none
  integer i,D,j,last(26),day, score(26)
  integer,allocatable :: c(:),s(:,:)
  read(*, *)D
  allocate(c(26))
  allocate(s(D,26))
  read(*, *) (c(i), i = 1,26)
  do i = 1,D
    read(*, *) (s(i,j), j = 1,26)
  end do
  last = 0
  do i = 1,D-3
    last = last + 1
    score = 0
    do j = 1,26
      score(j) = 4*s(i,j) - 5*s(i+1,j) - 0.6*s(i+2,j) - 0.3*s(i+3,j) + 10*last(j)*c(j)
    end do
    day = 1
    do j = 1,26
      if (score(j) > score(day)) then
        day = j
      end if
    end do
    write(*, *) day
    last(day) = 0
  end do
  do i = D-2,D
    last = last + 1
    score = 0
    do j = 1,26
      score(j) = s(i,j) + last(j)*c(j)
    end do
    day = 1
    do j = 1,26
      if (score(j) > score(day)) then
        day = j
      end if
    end do
    write(*, *) day
    last(day) = 0
  end do
end program main


program main
  implicit none
  integer i,D,j,last(26),day, score(26)
  integer,allocatable :: c(:),s(:,:)
  read(*, *)D
  allocate(c(26))
  allocate(s(D,26))
  read(*, *) (c(i), i = 1,26)
  do i = 1,D
    read(*, *) (s(i,j), j = 1,26)
  end do
  last = 0
  do i = 1,D-3
    last = last + 1
    score = 0
    do j = 1,26
      score(j) = s(i,j) - 0.9*s(i+1,j) - 0.6*s(i+2,j) - 0.3*s(i+3,j) + last(j)*c(j)
    end do
    day = 1
    do j = 1,26
      if (score(j) > score(day)) then
        day = j
      end if
    end do
    write(*, *) day
    last(day) = 0
  end do
  do i = D-2,D
    last = last + 1
    score = 0
    do j = 1,26
      score(j) = s(i,j) + last(j)*c(j)
    end do
    day = 1
    do j = 1,26
      if (score(j) > score(day)) then
        day = j
      end if
    end do
    write(*, *) day
    last(day) = 0
  end do
end program main


program main
  implicit none
  integer i,D,j,last(26),day, score(26)
  integer,allocatable :: c(:),s(:,:)
  read(*, *)D
  allocate(c(D))
  allocate(s(D,26))
  read(*, *) (c(i), i = 1,D)
  do i = 1,D
    read(*, *) (s(i,j), j = 1,26)
  end do
  last = 0
  do i = 1,D-3
    last = last + 1
    score = 0
    do j = 1,26
      score(j) = s(i,j) - 0.9*s(i+1,j) - 0.6*s(i+2,j) - 0.3*s(i+3,j) + last(j)*c(j)
    end do
    day = 1
    do j = 1,26
      if (score(j) > score(day)) then
        day = j
      end if
    end do
    write(*, *) day
    last(day) = 0
  end do
  do i = D-2,D
    last = last + 1
    score = 0
    do j = 1,26
      score(j) = s(i,j) + last(j)*c(j)
    end do
    day = 1
    do j = 1,26
      if (score(j) > score(day)) then
        day = j
      end if
    end do
    write(*, *) day
    last(day) = 0
  end do
end program main


program ABC172C
    implicit none
    integer(8)::i,j
    integer(8)::N,M,K,best0,ans=0
    integer(8),allocatable,dimension(:)::A,B,As,Bs
    read*,N,M,K
    allocate(A(N))
    allocate(As(N+1))
    allocate(B(M))
    allocate(Bs(M+1))
    read*,A,B
    As(1)=0
    Bs(1)=0
    do i=2,N+1
        As(i)=As(i-1)+A(i-1)
    end do

    do i=2,M+1
        Bs(i)=Bs(i-1)+B(i-1)
    end do

    best0=M+1

    do i=1,N+1
        if(As(i)>K)exit
        do j=best0,1,-1
            if(Bs(j)<=K-As(i))then
                best0=j
                ans=max(ans,i+j-2)
                exit
            end if
        end do
    end do
    print'(i0)',ans
end program ABC172C

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.


program ABC172C
    implicit none
    integer(8)::i,j
    integer(8)::N,M,K,best0,ans=0
    integer(8),allocatable,dimension(:)::A,B,As,Bs
    read*,N,M,K
    allocate(A(N))
    allocate(As(N+1))
    allocate(B(M))
    allocate(Bs(M+1))
    read*,A,B
    As(1)=0
    Bs(1)=0
    do i=2,N+1
        As(i)=As(i-1)+A(i-1)
    end do

    do i=2,M+1
        Bs(i)=Bs(i-1)+B(i-1)
    end do

    best0=M+1

    do i=1,N+1
        if(As(i)>K)exit
        do j=best0,1,-1
            if(Bs(j)<=K-As(i))then
                ans=max(ans,i+j-2)
                exit
            end if
        end do
    end do
    print'(i0)',ans
end program ABC172C


program main
    use iso_fortran_env
    implicit none
    integer(int32) :: n, m, k
    integer(int32), allocatable :: a(:), b(:)
    integer(int32), allocatable :: bt(:)
    integer(int32) :: i, j, tmp, ans, at

    read *, n, m, k
    allocate(a(n))
    allocate(b(m))
    allocate(bt(0:m), source = 0)

    read *, (a(i), i = 1, n)
    read *, (b(i), i = 1, m)

    do i = 1, m
        bt(i) = bt(i-1) + b(i)
    enddo

    i = 0
    ans = 0
    at = 0
    do while( ( k - at ) >= 0 )
        tmp = k - at
        j = maxloc( (bt(:) - tmp), 1, mask = ((bt(:) - tmp) <= 0) ) - 1
        if ( ans < (i+j) ) ans = i + j
        i = i + 1
        at = at + a(i)
        if ( i > n ) exit
    enddo

    print *, ans

end program main


      MODULE bSearchMod
! 二分探索
! 昇順の配列のみ対応。
      IMPLICIT NONE

      integer, parameter, private :: p = 8

      contains
! key以上の値が現れる最初のインデックスposを返す。
! O( log2(n) )
! 配列arrayはインデックスが1スタートで昇順の状態にしておいてほしい。
! key値がarrayの最大値より大きい場合、配列サイズ+1が返される。
! ex)
! [2, 2, 4, 4, 5]; key = -1
!  pos = 1
!
! [2, 2, 4, 4, 5]; key = 3
!        pos = 3
!
! [2, 2, 4, 4, 5]; key = 100
!                 pos = 6
      function lowerBound( array,key ) result( pos )
        implicit none
        integer(p),intent(in) :: array(1:),key
        integer(p) :: pos
        integer(p) :: left,right,mid
        logical :: isOK

        left = 0
        right = size( array )+1

        do while( abs(right-left)>1 )
          mid = (left+right)/2 !オーバーフローは考えないよ

          isOK = key<=array(mid)
          if( isOK )then
            right = mid
          else
            left = mid
          end if
        end do

        pos = right
      end function lowerBound

      END MODULE bSearchMod

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      PROGRAM tsundoku
      use bSearchMod
      IMPLICIT NONE
      INTEGER,PARAMETER :: p = 8
      integer(p) :: n,m,k,ans=0,time
      integer(p), allocatable :: a(:),b(:)
      integer(p) :: i,numB

      read*,n,m,k
      allocate( a(0:n),b(m) )
      read*,a(1:n)
      read*,b
      a(0) = 0

      do i = 1,n
          a(i) = a(i-1) + a(i)
      end do
      if( m>=2 )then
        do i = 2,m
          b(i) = b(i-1) + b(i)
        end do
      end if

      do i = 0,n
        time = k-a(i)
        if( time<0 ) cycle

        numB = lowerBound(b,time+1)
        if( b(numB)/=time ) numB = numB - 1
        ans = max( ans,i+numB )
      end do

      print*,ans

      stop
      !debugg

      END PROGRAM


      MODULE bSearchMod
! 二分探索
! 昇順の配列のみ対応。
      IMPLICIT NONE

      integer, parameter, private :: p = 8

      contains
! key以上の値が現れる最初のインデックスposを返す。
! O( log2(n) )
! 配列arrayはインデックスが1スタートで昇順の状態にしておいてほしい。
! key値がarrayの最大値より大きい場合、配列サイズ+1が返される。
! ex)
! [2, 2, 4, 4, 5]; key = -1
!  pos = 1
!
! [2, 2, 4, 4, 5]; key = 3
!        pos = 3
!
! [2, 2, 4, 4, 5]; key = 100
!                 pos = 6
      function lowerBound( array,key ) result( pos )
        implicit none
        integer(p),intent(in) :: array(1:),key
        integer(p) :: pos
        integer(p) :: left,right,mid
        logical :: isOK

        left = 0
        right = size( array )+1

        do while( abs(right-left)>1 )
          mid = (left+right)/2 !オーバーフローは考えないよ

          isOK = key<=array(mid)
          if( isOK )then
            right = mid
          else
            left = mid
          end if
        end do

        pos = right
      end function lowerBound

      END MODULE bSearchMod

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      PROGRAM tsundoku
      use bSearchMod
      IMPLICIT NONE
      INTEGER,PARAMETER :: p = 8
      integer(p) :: n,m,k,ans=0,time
      integer(p), allocatable :: a(:),b(:)
      integer(p) :: i,numB

      read*,n,m,k
      allocate( a(n),b(m) )
      read*,a
      read*,b

      if( n>=2 )then
        do i = 2,n
          a(i) = a(i-1) + a(i)
        end do
      end if
      if( m>=2 )then
        do i = 2,m
          b(i) = b(i-1) + b(i)
        end do
      end if

      do i = 1,n
        time = k-a(i)
        if( time<0 ) cycle

        numB = lowerBound(b,time+1)
        if( b(numB)/=time ) numB = numB - 1
        ans = max( ans,i+numB )
      end do

      print*,ans

      stop
      !debugg

      END PROGRAM


      MODULE bSearchMod
! 二分探索
! 昇順の配列のみ対応。
      IMPLICIT NONE

      integer, parameter, private :: p = 8

      contains
! key以上の値が現れる最初のインデックスposを返す。
! O( log2(n) )
! 配列arrayはインデックスが1スタートで昇順の状態にしておいてほしい。
! key値がarrayの最大値より大きい場合、配列サイズ+1が返される。
! ex)
! [2, 2, 4, 4, 5]; key = -1
!  pos = 1
!
! [2, 2, 4, 4, 5]; key = 3
!        pos = 3
!
! [2, 2, 4, 4, 5]; key = 100
!                 pos = 6
      function lowerBound( array,key ) result( pos )
        implicit none
        integer(p),intent(in) :: array(1:),key
        integer(p) :: pos
        integer(p) :: left,right,mid
        logical :: isOK

        left = 0
        right = size( array )+1

        do while( abs(right-left)>1 )
          mid = (left+right)/2 !オーバーフローは考えないよ

          isOK = key<=array(mid)
          if( isOK )then
            right = mid
          else
            left = mid
          end if
        end do

        pos = right
      end function lowerBound

      END MODULE bSearchMod

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      PROGRAM tsundoku
      use bSearchMod
      IMPLICIT NONE
      INTEGER,PARAMETER :: p = 8
      integer(p) :: n,m,k,ans=0,time
      integer(p), allocatable :: a(:),b(:)
      integer(p) :: i,numB

      read*,n,m,k
      allocate( a(n),b(m) )
      read*,a
      read*,b

      if( n>=2 )then
        do i = 2,n
          a(i) = a(i-1) + a(i)
        end do
      end if
      if( m>=2 )then
        do i = 2,m
          b(i) = b(i-1) + b(i)
        end do
      end if

      do i = 1,n
        time = k-a(i)
        if( time<0 ) cycle

        numB = lowerBound(b,time+1) -1
        ans = max( ans,i+numB )
      end do

      print*,ans

      stop
      !debugg

      END PROGRAM


program tsundoku
  implicit none
  integer :: n, m, h = 0, i, j
  integer(8) :: k
  integer(8), dimension(0:200001) :: a = 0, b = 0, x = 0, y = 0
  read(*,*) n, m, k
  read(*,*) a(1:n)
  read(*,*) b(1:m)
  do i = 1, n
    x(i) = x(i - 1) + a(i)
  end do
  do i = 1, m
    y(i) = y(i - 1) + b(i)
  end do
  j = m
  do i = 0, n
    if (x(i) > k) exit
    do while (y(j) > k - x(i))
      j = j - 1
    end do
    h = max(h, i + j)
  end do
  write(*,'(i0)') h
end program tsundoku


program main
implicit none
	integer(8):: N,i,j,K,X,M,o,p,q,a1,b1
    double precision::t,dk
	integer(8),dimension(1:2*100000)::A=1000000000,B=1000000000,C
	read *, N,M,K
    read(*,*) (A(i), i = 1, N)
    read(*,*) (B(i), i = 1, M)
	dk=dble(K)
   	if(N>=M)then
    q=N
    else
    q=M
    end if
    a1=1
    b1=1
    t=0d0
	do i=1,q
    	if(t+dble(A(a1)+b(b1))<=K)then
        t=t+dble(A(a1)+B(b1))
        a1=a1+1
        b1=b1+1
	    else if(t+dble(A(a1))<=dK)then
        t=t+dble(A(a1))
        a1=a1+1
        else if(t+dble(b(b1))<=dK)then
        t=t+dble(b(b1))
        b1=b1+1
		end if
    end do
        print'(I0)',a1+b1-2
end program main


program sample
    implicit none

    integer(8) :: i,j,m,n,k,s,t,l,u

    integer(8),allocatable :: a(:),b(:)

    read(*,*)n,m,k
    allocate(a(n),b(m))
    read(*,*)a
    read(*,*)b

    s=0
    do i=1,n
        s=s+a(i)
        if (s>k)then

            s=s-a(i)
            t=k-s
            s=i-1
            exit
        end if
        if(i==n)then
            t=k-s
            s=n
        end if
    end do

    l=s
    u=s
    do i=1,m

        do while(b(i)>t)
            t=t+a(l)
            l=l-1
            if(l==0)then
                write(*,*)s
                stop
            end if
            u=u-1
        end do
        u=u+1

        if (u>s)then
            s=u
        end if
        t=t-b(i)
    end do

    write(*,*)s

    stop
end program sample


program tsundoku
  implicit none
  integer(16) :: n, m, k, x, y, z, i, j, max
  integer(16), allocatable :: a(:), b(:)

  read(*,*) n,m,k
  allocate(a(n),b(m))
  read(*,*) a
  read(*,*) b

  x = 0
  if (sum(a) <= k ) then
     x = sum(a)
     y = n
  else
     do i = 1, n
        x = x + a(i)
        if (x > k) then
           x = x - a(i)
           y = i - 1
           exit
        end if
     end do
  end if

  max = y

  do i = y, 2, -1
     x = x - a(i)
     z = x
     do j = 1, m
        if ( z + sum(b) <= k ) then
           if (i + m > max) then
              max = i + m
           end if
        else
           z = z + b(j)
           if (z > k) then
              if ( i+j-1 > max ) then
                 max = i + j - 1
              end if
              exit
           end if
        end if
     end do
  end do

  if ( sum(b)<=k .and. m > max) then
     max = m
  end if

  write(*,'(i0)') max
  stop
end program tsundoku