program main

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

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

  now = 1
  do i = 1, n
    ans = a(now)
    now = ans
  end do
  do i = 1, mod(k,n)
    ans = a(now)
    now = ans
  end do

  print*, ans

end program main


      module takahashiMod
      implicit none
      integer(16),allocatable :: a(:)

      contains

      end module

      program teleporter
      use takahashiMod
      implicit none
      integer(16) :: n,k
      integer(16),allocatable :: telepoNum(:)
      integer(16) :: i,start,startTown,finTown,mo, now,next
      logical,allocatable :: flag(:)
      logical piyo

      read*,n,k
      allocate( a(n) )
      read*,a(1:n)
!print*,n,k
!print*,a(:)

      allocate( telepoNum(n),flag(n) );flag = .false.

      telepoNum(1) = 1
      do i = 2,n
        telepoNum(i) = a( telepoNum(i-1) )
      end do
!print*,telepoNum

      do i = 1,n
        if( flag(telepoNum(i)) )then
          start = telepoNum(i)
          exit
        else
          flag(telepoNum(i)) = .true.
        end if
     !   print*,flag
      end do
!print*,start

      i = 0
      piyo = .false.
      do while(.true.)
        i = i + 1
        if( telepoNum(i)==start )then
          if( piyo )then
            finTown = i
            exit
          else
            startTown = i
          end if
          piyo = .true.
        end if
      end do
      mo = finTown - startTown
!print*,startTown,finTown,mo
      k = mod( k-startTown+1,mo )
!print*,k
      print*,telepoNum(k+1+(startTown-1))
      end program

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

Score: 400 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

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

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

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

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2

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.


      module takahashiMod
      implicit none
      integer(16),allocatable :: a(:)

      contains

      end module

      program teleporter
      use takahashiMod
      implicit none
      integer(16) :: n,k
      integer(16),allocatable :: telepoNum(:)
      integer(16) :: i,start,startTown,finTown,mo, now,next
      logical,allocatable :: flag(:)
      logical piyo

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

      allocate( telepoNum(n),flag(n) );flag = .false.

      telepoNum(1) = 1
      do i = 2,n
        telepoNum(i) = a( telepoNum(i-1) )
      end do
print*,telepoNum

      do i = 1,n
        if( flag(telepoNum(i)) )then
          start = telepoNum(i)
          exit
        else
          flag(telepoNum(i)) = .true.
        end if
        print*,flag
      end do
print*,start

      i = 0
      piyo = .false.
      do while(.true.)
        i = i + 1
        if( telepoNum(i)==start )then
          if( piyo )then
            finTown = i
            exit
          else
            startTown = i
          end if
          piyo = .true.
        end if
      end do
      mo = finTown - startTown
print*,startTown,finTown,mo
      k = mod( k-startTown+1,mo )
print*,k
      print*,telepoNum(k+1+(startTown-1))
      end program


program main
  implicit none
  integer(8) :: N,K
  integer(8),allocatable :: A(:)
  integer(8),allocatable :: num(:)
  logical,allocatable :: flag(:)
  integer(8) :: loopnum
  integer :: now
  integer(8) :: step
  integer(8) :: i,j

  read(*,*) N,K
  allocate(A(N))
  read(*,*) A(:)
  allocate(flag(N))
  allocate(num(N))
  flag(:) = .false.
  flag(1) = .true.
  num(1) = int(0)
  now = 1
  now = A(int(now))
  do i = 1,N
     if ( flag(int(now)) ) then
        exit
     else
        flag(int(now)) = .true.
        num(int(now)) = i
        now = A(int(now))
     end if
  end do
  ! loop num
  loopnum = i - num(int(now))
  !write(*,*) loopnum
  step = mod( K-num(int(now)), loopnum )
  do i = 1,step
     num(int(now)) = i
     now = A(int(now))
  end do
  write(*,'(i0)') now
end program main


program main
      implicit none
      integer(16) :: n, k, cnt, roop, i, cnt2, waru
      integer, allocatable :: a(:)
      integer :: str, next, str2, str3
      logical, allocatable :: used(:)

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

      allocate( used(n) )
      used = .false.
      cnt = 0
      str = 1
      do
        if ( used(str) ) exit
        used(str) = .true.
        next = a(str)
        str = next
        cnt = cnt + 1
      end do

      str2 = 1
      cnt2 = 1
      do
        next = a(str2)
        str2 = next
        if ( next == str ) exit
        cnt2 = cnt2 + 1
      end do

      if ( (cnt+cnt2) >= k ) then
        str3 = 1
        do i = 1, k
          next = a(str3)
          str3 = next
        end do
        print *, next
      else

        waru = cnt-cnt2
        if ( waru == 0 ) then
          waru = cnt
        end if

        roop = mod( k-cnt,waru )

        do i = 1, roop
          next = a(str)
          str = next
        end do
        print *, next
      end if

end program main


program main
      implicit none
      integer(16) :: n, k, cnt, roop, i, cnt2, waru
      integer, allocatable :: a(:)
      integer :: str, next, str2
      logical, allocatable :: used(:)

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

      allocate( used(n) )
      used = .false.
      cnt = 0
      str = 1
      do
        if ( used(str) ) exit
        used(str) = .true.
        next = a(str)
        str = next
        cnt = cnt + 1
      end do

      str2 = 1
      cnt2 = 1
      do
        next = a(str2)
        str2 = next
        if ( next == str ) exit
        cnt2 = cnt2 + 1
      end do

      waru = cnt-cnt2
      if ( waru == 0 ) then
        waru = cnt
      end if

      roop = mod( k-cnt,waru )

      do i = 1, roop
        next = a(str)
        str = next
      end do
      print *, next

end program main


program main
  implicit none
  integer :: HP(2)
  integer :: AT(2)

  read(*,*)  HP(1),AT(1),HP(2),AT(2)

  do
     HP(2) = HP(2) - AT(1)
     if (HP(2) <= 0) then
        write(*,'(a)') 'Yes'
        stop
     end if
     HP(1) = HP(1) - AT(2)
     if (HP(1) <= 0) then
        write(*,'(a)') 'No'
        stop
     end if
  end do

end program main

Score : 200 points

Problem Statement

Takahashi and Aoki will have a battle using their monsters.

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

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

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

Constraints

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

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

Sample Input 1

10 9 10 10

Sample Output 1

No

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

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

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

Sample Input 2

46 4 40 5

Sample Output 2

Yes


program sample
implicit none
integer A,B,C,D
READ(*,*)A,B,C,D
10 CONTINUE
DO WHILE (C>0.and.A>0)
C=C-B
If (C<=0) then
   WRITE(*,*) "Yes"
END IF
A=A-D
IF(A<=0.AND.C>0) then
   WRITE(*,*) "No"
END IF
END DO
stop
end program sample


program sample
implicit none
integer A,B,C,D
READ(*,*)A,B,C,D
10 CONTINUE
DO WHILE (C>0.OR.A>0)
C=C-B
If (C<=0) then
   WRITE(*,*) "NO"
END IF
A=A-D
IF(A<=0.AND.C>0) then
   WRITE(*,*) "YES"
END IF
END DO
stop
end program sample


program atcoder
   implicit none
   integer A, B, C, D, E, F
   read *, A, B, C, D

   A=int(A/D+0.9999)
   C=int(C/B+0.9999)
    if (A >= C) then
     print *,'No'
    else
     print *,'Yes'
    end if
end program atcoder


program b1

 implicit none

 integer :: a,b,c,d,i,x,y

 read*,a,b,c,d

 do i=1,1000
   x=c-b
   if(x<=0) print*,"Yes"
   if(x<=0) exit
   y=a-d
   if(y<=0) print*,"No"
   if(y<=0) exit
   c=x
   a=y
 end do

end program b1


program problemB
  implicit none

integer(8) :: n,m, yr, l, i, k, j,judge, small, kref, r, t, f, ref
integer(8) :: BNUM, RNUM, NMAX, count,MMAX, sum, A, B, C, D,multi,countA,countC
character(len=4) :: color

read(*,*) A,B,C,D

countA=0
countC=0
do while (A>0)
  A=A-D
  countA=countA+1
end do
do while (C>0)
  C=C-B
  countC=countC+1
end do

if (countA>=countC) then
  write(*,*) 'Yes'
else
  write(*,*) 'No'
end if

stop
end program


program problemB
  implicit none

integer(8) :: n,m, yr, l, i, k, j,judge, small, kref, r, t, f, ref
integer(8) :: BNUM, RNUM, NMAX, count,MMAX, sum, A, B, C, D
character(len=4) :: color

read(*,*) A,B,C,D

if (B>=C) then
  write(*,*) 'Yes'
else
  if (int(real(A)/real(D))-real(A)/real(D)==0) then
    ref=int(real(A)/real(D))
  else
    ref=int(real(A)/real(D))+1
  end if
  if (int(real(C)/real(B))>ref) then
    write(*,*) 'No'
  else
    write(*,*) 'Yes'
  end if
end if

stop
end program


program main
integer :: a,b,c,d

read(*,*) a,b,c,d

do while(a > 0 .and. c > 0)
c = c - b
if (c <= 0) exit
a = a - d
end do

if (a > c) then
write(*,*) "Yes"
else
write(*,*) "No"
end if
end program main


program b1

 implicit none

 integer :: a,b,c,d

 read*,a,b,c,d

 do
  c=c-b
  if(c<=0) print*,"Yes" ; exit
  a=a-d
  if(a<=0) print*,"No" ; exit
 end do

end program b1


program battle
  implicit none
  integer :: a, b, c, d
  read(*,*) a, b, c, d
  do
    c = c-b
    if (c <= 0) then
      write(*,'(a)') "Yes"
      stop
    end if
    a = a-d
    if (a <= 0) then
      write(*,'(a)') "No"
      stop
    end if
  end do
end program battle


   program abc164b
      implicit none
      integer::a,b,c,d
      integer::i

         read *, a,b,c,d

         do i=1,100
            c=c-b
            if (c<=0) then
               print *, 'Yes'
               exit
            end if

            a=a-d
            if (a<=0) then
               print *, 'No'
               exit
            end if
         end do

   end program abc164b


program main
    implicit none
    integer a, b, c, d
    integer life_t, life_a
    integer i
    read(*,*) a, b, c, d

    life_t = a
    life_a = c

    do i = 1, 100
        if (mod(i,2) == 0) then
            life_t = life_t - d
        else
            life_a = life_a - b
        end if

        if (life_a <= 0) then
            write(*,*) "Yes"
            exit
        end if
        if (life_t <= 0) then
            write(*,*) "No"
            exit
        end if
    end do

end program


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: a,b,c,d,turn

    read*, a,b,c,d
    turn=1
    do while(a > 0 .and. c>0)
        if (turn==1) then
            c=c-b
        else
            a=a-d
        end if
        turn=turn*(-1)
    end do

    if (a>0) then
        print'(a)', 'Yes'
    else
        print'(a)', 'No'
    end if
end program


      PROGRAM Battle
      IMPLICIT NONE
      integer :: a,b,c,d

      read*,a,b,c,d

      do while( .true. )
        a = a - d
        c = c - b

        if( c<=0 )then
          print*,'Yes';stop
        end if

        if( a<=0 )then
          print*,'No';stop
        end if
      end do

      END PROGRAM


program multi
  implicit none

  integer :: i, j, k
  integer :: count, judge, calculate
  real(kind=8) :: number_int
  character(len=200000) :: number

  read (*,*) number

  count = len(trim(number))

  k = 0
  do i = 1, count
    do j = i+1, count
      read(number(i:j), *) number_int
      calculate = int(number_int)
      judge = mod(calculate,2019)
      if(judge == 0) then
        k = k + 1
      end if
    end do
  end do

print *, k
end program multi

Score : 400 points

Problem Statement

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

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

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

Constraints

1 ≤ |S| ≤ 200000

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

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

1817181712114

Sample Output 1

3

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

Sample Input 2

14282668646

Sample Output 2

2

Sample Input 3

2119

Sample Output 3

0

No pairs satisfy the condition.


program multi
  implicit none

  integer :: i, j, k
  integer :: count, judge, calculate
  real(kind=8) :: number_int
  character(len=200000) :: number

  read (*,*) number

  count = len(trim(number))

  k = 0
  do i = 1, count
    do j = i+1, count
      read(number(i:j), *) number_int
      calculate = int(number_int)
      judge = mod(calculate,2019)
      if(judge == 0) then
        k = k + 1
      else

      end if
    end do
  end do

print *, k
end program multi


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,nl(0:2018),tmp,ans
    character(200000):: s

    read*, s
    n = len_trim(s)
    tmp=0
    nl(:) = 0
    nl(0) = 1
    do i=0,n-1
        tmp=mod(tmp+(ichar(s(n-i:n-i))-ichar('0'))*10**i,2019)
        nl(tmp)=nl(tmp)+1
    end do

    ans=0
    do i=0,2018
        ans=ans+((nl(i)*(nl(i)-1))/2)
    end do

    print'(i0)', ans
end program


program multiple_of_2019
  implicit none
  character(200000) :: s
  integer :: n, d, k, i, j, l = 0
  integer(8) :: dp(0:1,0:2018) = 0, a = 0
  read(*,*) s
  n = len_trim(s)
  do i = 1, n
    l = 1-l
    dp(l,:) = 0
    d = ichar(s(i:i))-48
    do j = 0, 2018
      k = mod(10*j+d,2019)
      dp(l,k) = dp(l,k)+dp(1-l,j)
      a = a+dp(1-l,j)
    end do
    dp(l,d) = dp(l,d)+1
  end do
  write(*,'(i0)') a+dp(l,0)
end program multiple_of_2019


program main
    implicit none
    character(200001) S, sub_S
    integer i, j, S_len, m
    integer(8) num, ans
    m = 2019
    ans = 0
    read *, S
    S_len = len_trim(S)
    do i = 4, S_len
        do j = 1, S_len - i + 1
            sub_S = S(j:j + i - 1)
            read (sub_S, *) num
            if (mod(num, m) == 0) then
                ans = ans + 1;
            end if
        end do
    end do
    print '(I0)', ans
    stop
contains

end program main


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,l,ans,j,sek,keta,nlist(200000/2019)
    character(200000):: s,bs

    read*, s
    n = len_trim(s)

    ans=0
    i=1

    do i=1, size(nlist)
        nlist(i) = 2019*i
    end do

    do i=1, size(nlist)
        write(bs,*)nlist(i)
        bs=adjustl(bs)
        l = len_trim(bs)
        if (l > n) cycle
        do j=1,n-l+1
            if (s(j:j+l-1) == trim(bs)) ans=ans+1
        end do
    end do
    print'(i0)', ans
end program


program A
    implicit none
    integer::R
    real(16), parameter::pi = 3.1415926546897932384826_16

    read(*,*) R
    write(*,*) 2.0_16*R*pi

    stop
end program A

Score : 100 points

Problem Statement

Print the circumference of a circle of radius R.

Constraints

1 \leq R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

R

Output

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

Sample Input 1

1

Sample Output 1

6.28318530717958623200

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

Sample Input 2

73

Sample Output 2

458.67252742410977361942


program A
    implicit none
    integer::R
    real(16), parameter::pi = 3.1415926546897932384826_16

    read(*,*) R
    write(*,*) R*pi

    stop
end program A


program main
	real a, p
	read *, a
    p = 3.14159265359
	print '(f0.10)', (2.0 * a * p)
end program main


program main
    implicit none
    double precision r, ans, pi
    read *, r
    pi = 3.14159265359
    ans = 2 * r * pi
    print '(f30.25)', ans
    stop
contains

end program main


program readABC
implicit none
integer :: N
real :: i=3.14159265358979323846
real :: ans

read*,N
ans = 2*N*i

print*,ans

end program readABC


program sample
implicit none
integer N,A(200000),B(200000),i
read(*,*) N
read(*,*) (A(i),i=2,N)
do i=2,N
B(A(i))=B(A(i))+1
end do
do i=1,N
write(*,'(i0)') B(i)
end do

stop
end program sample


program sample
implicit none
integer(8) N,A(500000),B(500000),i
read(*,*) N
read(*,*) (A(i),i=2,N)
do i=2,N
B(A(i))=B(A(i))+1
end do
do i=1,N
write(*,*) B(i)
end do

stop
end program sample


program sample
implicit none
integer(8) N,A(500000),B(500000),i
read(*,*) N
read(*,*) (A(i),i=2,N)
do i=2,N
B(A(i))=B(A(i))+1
end do
do i=1,N
write(*,'(i0)') B(i)
end do

stop
end program sample


program circle_pond
  implicit none
  real(8), parameter :: pi = 3.14159265358979d0
  real(8) :: r
  character(64) :: s
  read(*,*) r
  write(s,'(f32.16)') 2.d0*pi*r
  write(*,'(a)') trim(adjustl(s))
end program circle_pond


program a

 implicit none
 integer r
 real(8) x,pi

 read(*,*) r

 pi=3.14159265359
 x=2.0*r*pi

 write(*,*) x

end program a


program a

 implicit none
 integer r
 real(8) x,pi

 read(*,*) r

 pi=3.14159265
 x=2.0*r*pi

 write(*,*) x

end program a


program a

 implicit none
 integer r
 real(8) x,pi

 read(*,*) r

 pi=3.1415
 x=2.0*r*pi

 write(*,*) x

end program a


program main
    implicit none
    integer(8) :: r
    real(8) :: l
    real(8), parameter :: pi = 4.0d0*atan(1.0d0)
    read'(I3)', r
    l = 2*pi*r
    print*, l
end program main


program ABC163_A
  read(*,*)N
  pi=3.141592
  write(*,*)N*pi*2
end program ABC163_A


program main
  implicit none

  real(8) :: r

  read *, r

  print *, 2.0d0*atan(1.0d0)*4.0d0*r

  stop
end program main


program example
	implicit none
	real(8) :: R,pi=3.14159265358979

    read(*,*) R

    write(*,*) 2.0d0*pi*R

end program


program abc
	implicit none
	integer :: r
    double precision :: l

	read(*,*) r

    l = dble(r) * 3.141592

	write(*,*) l

end program abc


program main

  implicit none
  integer :: r

  read(*,*) r

  print*, real( 2*r ) * 3.141592648e0

end program main


   program abc163a
      implicit none
      integer(8)::i

         read *,i
         print *,i*3.141592*2.0d0

   end program abc163a


program abc
	implicit none
	integer :: r,l

	read(*,*) r

    l = r * 3.141592

	write(*,*) l

end program abc


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32)::a
    read*, a
    print*, dble(2*a)*acos(-1.d0)
end program name


program main
    implicit none
    real(8) i
    read(*,*) i
    i=i*3.14d0
    write(*,*) i
end program main


program ABC163A
    implicit none
    real(8)::R
    read*,R
    print'(f0.3)',2*R*3.14
end program ABC163A


      PROGRAM piyo
      IMPLICIT NONE

      real(16) :: R
      real(16),parameter :: pi = 3.141592653589793238462

      read*, R

      print*,2._16*pi*R

      END PROGRAM


program sample
	implicit none
    character(len=3) ::N

    read(*,*) N

    if (N(1:1)=='7' .or. N(2:2)=='7' .or. N(3:3)=='7') then
    	write(*,*) 'Yes'
    else
    	write(*,*) 'No'
    end if

    stop
end program sample

Score : 100 points

Problem Statement

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

If so, print Yes; otherwise, print No.

Constraints

100 \leq N \leq 999

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

117

Sample Output 1

Yes

117 contains 7 as its last digit.

Sample Input 2

123

Sample Output 2

No

123 does not contain the digit 7.

Sample Input 3

777

Sample Output 3

Yes


program main
	implicit none
    integer::a,i,b
    character::s
    read(*,*) a
    s='f'
    do i=1,3
    	b=mod(a,10)
        a=a/10
        if(b==7)then
        	s='t'
        end if
    end do

    if (s=='t')then
    	write(*,*) 'Yes'
    else
    	write(*,*) 'No'
    end if
    stop
end program main


program main
	implicit none
    integer::a,i,b
    character::s
    read(*,*) a
    s='f'
    do i=1,3
    	b=mod(a,10)
        b=b/10
        if(b==7)then
        	s='t'
        end if
    end do

    if (s=='t')then
    	write(*,*) 'Yes'
    else
    	write(*,*) 'No'
    end if
    stop
end program main


program test
implicit none
integer(8) :: n,l1,l2,l3

read(*,*) n

l1 = int(n/100)
l2 = (n-l1*100)/10
l3 = mod(n,10)

if(l1==7 .or. l2==7 .or. l3==7) then
  write(*,*) 'Yes'
else
  write(*,*) 'No'
endif

end


program test
implicit none
integer(8) :: n,l1,l2,l3

read(*,*) n

l1 = int(n/100)
l2 = (n-l1)/10
l3 = mod(n,10)

if(l1==7 .or. l2==7 .or. l3==7) then
  write(*,*) 'Yes'
else
  write(*,*) 'No'
endif

end


program FizzBuzz
      implicit none

      integer(8) N,S,i
      read(*,*) N
      S=0
      do i =1, N
      if (mod(i,5)==0.or.mod(i,3)==0) then
            go to 1
      else
            S=S+i
      1 end if
      end do

      write(*,'(i0)') S
      stop
      end program FizzBuzz


program FizzBuzz
      implicit none

      integer(8) N,S,i
      read(*,*) N
      S=0
      do i =1, N
      if (mod(i,5)==0.or.mod(i,3)==0) then
            go to 1
      else
            S=S+i
      1 end if
      end do

      write(*,*) S
      stop
      end program FizzBuzz


program FizzBuzz
      implicit none
      integer N,i
      real S
      read(*,*) N
      S=0
      do i =1, N
      if (mod(i,5)==0.or.mod(i,3)==0) then
            go to 1
      else
            S=S+i
      1 end if
      end do
      write(*,*) S
      stop
      end program FizzBuzz


      program lucky7
      implicit none
      integer N,A(3)
      read(*,*) N
      A(0)=N/100
      A(1)=(N-A(0)*100)/10
      A(2)=N-A(0)*100-A(1)*10
      if(A(0)==7.or.A(1)==7.or.A(2)==7) then
         write(*,*) "Yes"
      else
         write(*,*) "No"
      end if
      stop
      end program lucky7


      program lucky7
      implicit none
      integer N,A(3)
      read(*,*) N
      A(0)=N/100
      A(1)=(N-A(0)*100)/10
      A(2)=N-A(0)*100-A(1)*10
      if(A(0)==7.or.A(1)==7.or.A(2)==7) then
         write(*,*) "YES"
      else
         write(*,*) "No"
      end if
      stop
      end program lucky7


program main
    implicit none
    character(3) n
    integer i
    logical :: flag = .false.
    read *, n
    do i=1,3
        if (n(i:i) == '7') then
            flag = .true.
            exit
        endif
    enddo
    if (flag) then
        print *, 'Yes'
    else
        print *, 'No'
    endif
end program main


program ABC162_A
  character(3)::N
  read(5,*)N
  if(N(1:1)=='7') then
     write(6,*)'Yes'
  else if(N(2:2)=='7' .or. N(3:3)=='7') then
     write(6,*) 'Yes'
  else
     write(6,*)'No'
  end if
end program ABC162_A


program main
    implicit none
    integer n
    integer n1, n10, n100,flag
    read(*,*) n

    n100 = (n - mod(n,100))/100
    n1 = mod((n - n100*100),10)
    n10 = (n - n100*100 - n1)/10

    flag = 0
    if(n100 == 7) then
        flag = 1
    end if

    if(n10 == 7) then
        flag = 1
    end if

    if(n1 == 7) then
        flag = 1
    end if

    if(flag == 1) then
        write(*,*) "Yes"
    else
        write(*,*) "No"
    end if

end program


module degit_operation

   interface extract_degit
      module procedure extract_degit_4, extract_degit_8
   end interface

   interface num_of_degits
      module procedure num_of_degits_4, num_of_degits_8
   end interface

contains

   function extract_degit_4(num, degit)
      ! 引数numのdegit桁目の数字を取り出す関数
      implicit none
      integer, intent(in) :: num, degit
      integer :: extract_degit_4

      ! 引数numは正の整数を想定
      extract_degit_4 = mod(num,10**(degit)) / 10**(degit-1)
   end function extract_degit_4

   function extract_degit_8(num, degit)
      ! 引数numのdegit桁目の数字を取り出す関数
      implicit none
      integer(8), intent(in) :: num
      integer, intent(in) :: degit
      integer :: extract_degit_8

      ! 引数numは正の整数を想定
      extract_degit_8 = mod(num,10**(degit)) / 10**(degit-1)
   end function extract_degit_8

   function num_of_degits_4(input)
      ! 引数inputの桁数を返す関数
      implicit none
      integer, intent(in) :: input
      integer :: num_of_degits_4, degit, num

      degit = 0
      num = input
      do while (num .ne. 0)
         num = num / 10
         degit = degit + 1
      enddo

      num_of_degits_4 = degit
   end function num_of_degits_4

   function num_of_degits_8(input)
      ! 引数inputの桁数を返す関数
      implicit none
      integer(8), intent(in) :: input
      integer :: num_of_degits_8, degit, num

      degit = 0
      num = input
      do while (num .ne. 0)
         num = num / 10
         degit = degit + 1
      enddo

      num_of_degits_8 = degit
   end function num_of_degits_8

end module degit_operation

program abc162a
   use degit_operation
   implicit none
   integer :: N, i

   read(*,*) N

   do i = 1, 3
      if (extract_degit(N,i) == 7) then
         print *, 'Yes'
         stop
      end if
   end do

   print *, 'No'

end program abc162a


program problemA
  implicit none

integer :: S, L, R
integer :: N, m, yr, a,b,c

read(*,*) N

a=int(N/100)
N=N-100*int(N/100)
b=int(N/10)
N=N-10*int(N/10)
c=N

if (a==7) then
  write(*,*) 'Yes'
  stop
else if (b==7) then
  write(*,*) 'Yes'
  stop
else if (c==7) then
  write(*,*) 'Yes'
  stop
else
  write(*,*) 'No'
end if

stop
end program


program main

  implicit none
  character(3) :: n
  integer :: i,j

  read(*,*) n

  j = 0
  do i = 1, 3
    if(n(i:i) == "7") j = 1
  end do
  if(j==1) then
    print*, "Yes"
  else
    print*, "No"
  end if
end program main


program am
implicit none
character(3)::S
integer::i
logical::check=.false.
read*,S
do i=1,len_trim(S)
    if(S(i:i)=="7")check=.true.
end do
print"(A)",trim(merge("Yes","No ",check))
end program am


program sum_of_gcd_of_tuples
  implicit none
  integer :: k, i, j
  integer(8) :: x(200) = 0, y(200) = 0
  read(*,*) k
  do i = k, 1, -1
    x(i) = (k / i) ** 3
    do j = 2 * i, k, i
      x(i) = x(i) - x(j)
    end do
    y(i) = i
  end do
  write(*,'(i0)') dot_product(x, y)
end program sum_of_gcd_of_tuples

Score : 300 points

Problem Statement

Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c.

Constraints

1 \leq K \leq 200

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Sample Input 1

2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

200

Sample Output 2

10813692


program sum_of_gcd_of_tuples
  implicit none
  integer :: n = 3, k, i, j
  integer(8) :: m = 0, x(100000) = 0
  read(*,*) k
  do i = k, 1, -1
    x(i) = (k / i) ** n
    do j = 2 * i, k, i
      x(i) = x(i) - x(j)
    end do
  end do
  do i = 1, k
    m = m + x(i) * i
  end do
  write(*,'(i0)') m
end program sum_of_gcd_of_tuples


program main
integer*16 :: n,gcd=0,ans = 0
read(*,*)n
do i = 1,n
do j = 1,n
do k = 1,n
do l = 1,100
if (mod(i,l)==0 .and. mod(j,l)==0 .and. mod(k,l)==0) gcd = max(gcd,l)
end do
ans = ans + gcd
end do
end do
end do
write(*,*)ans
end program main


program main
integer :: n,gcd=0,ans = 0
read(*,*)n
do i = 1,n
do j = 1,n
do k = 1,n
do l = 1,n
if (mod(i,l)==0 .and. mod(j,l)==0 .and. mod(k,l)==0) gcd = max(gcd,l)
end do
ans = ans + gcd
end do
end do
end do
write(*,*)ans
end program main


program main
    implicit none
    integer K, ans, a, b, c
    ans = 0
    read (*, *) K
    do a = 1, K
        do b = 1, K
            do c = 1, K
                ans = ans + gcd(a, gcd(b, c))
            end do
        end do
    end do
    write(*, '(I0)') ans
    stop
contains
    recursive integer function gcd(x, y) result(ret)
        implicit none
        integer x, y
        if (y == 0) then
            ret = x
        else
            ret = gcd(y, mod(x, y))
        end if
        return
    end function
end program main


program ABC162C
    implicit none
    integer(8)::i,j,k,N,ans
    read*,N
    ans=0

    do i=1,N
        do j=1,N
            do k=1,N
                ans=ans+gcd(i,gcd(j,k))
            end do
        end do
    end do

    print'(i0)',ans
contains
    integer(8) function gcd(a,b)
        implicit none
        integer(8),intent(IN)::a,b
        integer(8)::l,m,n

        m=a
        n=b

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

        gcd=n
        return
    end function gcd
end program ABC162C


 program FizzBuzz
      implicit none

      integer K,a(3),gcd,Mi,i,j,l
      read(*,*) K
      a=1
      gcd=0
      do i=1,K
        a(0)=i
      do j=1,K
        a(1)=j
      do l=1,K
         a(2)=l
         Mi=min(a(0),a(1),a(2))

         1 if (mod(a(0),Mi)==0.and.mod(a(1),Mi)==0.and.mod(a(2),Mi)==0) then
            gcd=gcd+Mi
         else
             Mi=Mi-1
             go to 1
         end if
      end do
      end do
      end do

      write(*,'(i0)') gcd
      stop
      end program FizzBuzz


program main
  implicit none

  integer :: n, i, j, k, s

  read *, n

  s = 0
  do i = 1, n
     do j = i, n
        do k = j, n
           if (i == j) then
              if (j == k) then
                 s = s + gcd(i,j,k)
              else
                 s = s + 3*gcd(i,j,k)
              end if
           else
              if (j == k) then
                 s = s + 3*gcd(i,j,k)
              else
                 s = s + 6*gcd(i,j,k)
              end if
           end if
        end do
     end do
  end do

  print *, s

  stop
contains

  integer function gcd(i,j,k)
    implicit none
    integer, intent(in) :: i, j, k
    integer :: a, b, c, d, e
    logical :: log

    a = i
    b = j
    c = k
    if (a < b) then
       d = a
       a = b
       b = d
    end if

    log = .true.
    do while(log .eqv. .true.)
       if (b == 0) then
          d = a
          log = .false.
          exit
       else
          d = mod(a,b)
          a = b
          b = d
       end if
    end do

    if (c < d) then
       e = c
       c = d
       d = e
    end if

    log = .true.
    do while(log .eqv. .true.)
       if (d == 0) then
          gcd = c
          log = .false.
          exit
       else
          e = mod(c,d)
          c = d
          d = e
       end if
    end do

    return
  end function gcd

end program main


program ABC162_C
  integer::G,A,H,I,J,K,L
  read(5,*)K
  A=0
  do I=1,K
     do J=1,K
        do L=1,K
           call gcd(J,L,G)
           call gcd(I,G,H)
           A=A+H
        end do
     end do
  end do
  write(6,*)A
end program ABC162_C

  subroutine gcd(A,B,G)
    integer::A,B,C,G,M
    if(A<B) then
       D=B
       E=A
    else
       D=A
       E=B
    end if
    do while(E/=0)
       M=mod(D,E)
       D=E
       E=M
    end do
    G=D
  end subroutine gcd


program sum_of_gcd_of_tuples_easy
  implicit none
  integer(8) :: k, a, b, c, s = 0_8, g
  read(*,*) k
  do a = 1_8, k
    do b = 1_8, k
      g = gcd(a,b)
      do c = 1_8, k
        s = s+gcd(g,c)
      end do
    end do
  end do
  write(*,'(i0)') s
contains
  integer(8) function gcd(a,b)
    integer(8), intent(in) :: a, b
    integer(8) :: x, y
    x = max(abs(a),abs(b))
    y = min(abs(a),abs(b))
    do while (y > 0)
      gcd = y
      y = mod(x,y)
      x = gcd
    end do
  end
end program sum_of_gcd_of_tuples_easy


program problemC
  implicit none

integer(4) :: K, a,b,c , count, G , n , countprime, number(200), m, l
integer(4), allocatable :: primenumber(:), pvec(:)
integer(4) :: input

 read(*,*) K

do n=1,200
  number(n)=n
end do

do n=2, 200
  if (number(n)/=0) then
    do m=n+1, 200
      if (number(m)/=0) then
        if (mod(number(m),n)==0) then
          number(m)=0
        end if
      end if
    end do
  end if
end do

countprime=0
do n=1, 200
      if (number(n)/=0) then
        countprime=countprime+1
      end if
end do

allocate(primenumber(countprime), pvec(countprime))

countprime=0
do n=1, 200
      if (number(n)/=0) then
        countprime=countprime+1
        primenumber(countprime)=number(n)
      end if
end do

count=0
do a=1, K
  do b=1, K
    do c=1, K
      call gcd(a,b,c,G,primenumber,countprime)
      count=count+G
    end do
  end do
end do

write(*,*) count

deallocate(primenumber,pvec)

stop
contains
  subroutine gcd(a,b,c,G,vecin,n)
    implicit none

    integer(4), intent(in) :: a,b,c
    integer(4), intent(out) :: G
    integer(4), intent(in) :: vecin(n),n
    integer(4) :: avec(n), bvec(n),cvec(n),k

    call factor(a,vecin,n,avec)
    call factor(b,vecin,n,bvec)
    call factor(c,vecin,n,cvec)

    G=1
    do k=1, n
      if (avec(k)==0 .or. bvec(k)==0 .or. cvec(k)==0) then
        G=G*1
      else
        G=G*(vecin(k)**min(avec(k),bvec(k),cvec(k)))
      end if
    end do

  end subroutine gcd

  subroutine factor(a,vecin, n,vecout)
    implicit none

    integer(4), intent(in) :: a,n
    integer(4), intent(in) :: vecin(n)
    integer(4), intent(out) :: vecout(n)
    integer(4) ::  k, l , count, m, ap

    vecout(1)=1

    do k=2, n
      l=0
      count=0
      ap=a
      if (a>=vecin(k)) then
        do while (l==0)
          l=mod(ap,vecin(k))
          if (l==0) then
            count=count+1
            ap=ap/vecin(k)
          end if
        end do
        vecout(k)=count
      end if
      if (a<vecin(k)) then
        vecout(k)=0
      end if
    end do

  end subroutine factor

end program


program problemC
  implicit none

integer(4) :: K, a,b,c , count, G , n , countprime, number(200), m, l
integer(4), allocatable :: primenumber(:), pvec(:)
integer(4) :: input

 read(*,*) K

do n=1,200
  number(n)=n
end do

do n=2, 200
  if (number(n)/=0) then
    do m=n+1, 200
      if (number(m)/=0) then
        if (mod(number(m),n)==0) then
          number(m)=0
        end if
      end if
    end do
  end if
end do

countprime=0
do n=1, 200
      if (number(n)/=0) then
        countprime=countprime+1
      end if
end do

allocate(primenumber(countprime), pvec(countprime))

countprime=0
do n=1, 200
      if (number(n)/=0) then
        countprime=countprime+1
        primenumber(countprime)=number(n)
      end if
end do

count=0
do a=1, K
  do b=1, K
    do c=1, K
      call gcd(a,b,c,G,primenumber,countprime)
      count=count+G
    end do
  end do
end do

write(*,*) count

deallocate(primenumber,pvec)

stop
contains
  subroutine gcd(a,b,c,G,vecin,n)
    implicit none

    integer(4), intent(in) :: a,b,c
    integer(4), intent(out) :: G
    integer(4), intent(in) :: vecin(:),n
    integer(4) :: avec(n), bvec(n),cvec(n),k

    call factor(a,vecin,n,avec)
    call factor(b,vecin,n,bvec)
    call factor(c,vecin,n,cvec)

    G=1
    do k=1, n
      if (avec(k)==0 .or. bvec(k)==0 .or. cvec(k)==0) then
        G=G*1
      else
        G=G*(vecin(k)**min(avec(k),bvec(k),cvec(k)))
      end if
    end do

  end subroutine gcd

  subroutine factor(a,vecin, n,vecout)
    implicit none

    integer(4), intent(in) :: a,n
    integer(4), intent(in) :: vecin(:)
    integer(4), intent(out) :: vecout(:)
    integer(4) ::  k, l , count, m, ap

    vecout(1)=1

    do k=2, n
      l=0
      count=0
      ap=a
      if (a>=vecin(k)) then
        do while (l==0)
          l=mod(ap,vecin(k))
          if (l==0) then
            count=count+1
            ap=ap/vecin(k)
          end if
        end do
        vecout(k)=count
      end if
      if (a<vecin(k)) then
        vecout(k)=0
      end if
    end do

  end subroutine factor

end program


program problemC
  implicit none

integer(4) :: K, a,b,c , count, G , n , countprime, number(200), m, l
integer(4), allocatable :: primenumber(:), pvec(:)
integer(4) :: input

 read(*,*) K

do n=1,200
  number(n)=n
end do

do n=2, 200
  if (number(n)/=0) then
    do m=n+1, 200
      if (number(m)/=0) then
        if (mod(number(m),n)==0) then
          number(m)=0
        end if
      end if
    end do
  end if
end do

countprime=0
do n=1, 200
      if (number(n)/=0) then
        countprime=countprime+1
      end if
end do

allocate(primenumber(countprime), pvec(countprime))

countprime=0
do n=1, 200
      if (number(n)/=0) then
        countprime=countprime+1
        primenumber(countprime)=number(n)
      end if
end do

count=0
do a=1, K
  do b=1, K
    do c=1, K
      call gcd(a,b,c,G,primenumber,countprime)
      count=count+G
    end do
  end do
end do

write(*,*) count

deallocate(primenumber,pvec)

stop
contains
  subroutine gcd(a,b,c,G,vecin,n)
    implicit none

    integer(4), intent(in) :: a,b,c
    integer(4), intent(out) :: G
    integer(4), intent(in) :: vecin(:),n
    integer(4) :: avec(n), bvec(n),cvec(n),k

    call factor(a,vecin,n,avec)
    call factor(b,vecin,n,bvec)
    call factor(c,vecin,n,cvec)

    G=1
    do k=1, 10
      if (avec(k)==0 .or. bvec(k)==0 .or. cvec(k)==0) then
        G=G*1
      else
        G=G*(vecin(k)**min(avec(k),bvec(k),cvec(k)))
      end if
    end do

  end subroutine gcd

  subroutine factor(a,vecin, n,vecout)
    implicit none

    integer(4), intent(in) :: a,n
    integer(4), intent(in) :: vecin(:)
    integer(4), intent(out) :: vecout(:)
    integer(4) ::  k, l , count, m, ap

    vecout(1)=1

    do k=2, 10
      l=0
      count=0
      ap=a
      if (a>=vecin(k)) then
        do while (l==0)
          l=mod(ap,vecin(k))
          if (l==0) then
            count=count+1
            ap=ap/vecin(k)
          end if
        end do
        vecout(k)=count
      end if
      if (a<vecin(k)) then
        vecout(k)=0
      end if
    end do

  end subroutine factor

end program


program problemC
  implicit none

integer(4) :: K, a,b,c , count, G , n , countprime, number(200), m, l
integer(4), allocatable :: primenumber(:), pvec(:)
integer(4) :: input

 read(*,*) K

do n=1,200
  number(n)=n
end do

do n=2, 200
  if (number(n)/=0) then
    do m=n+1, 200
      if (number(m)/=0) then
        if (mod(number(m),n)==0) then
          number(m)=0
        end if
      end if
    end do
  end if
end do

countprime=0
do n=1, 200
      if (number(n)/=0) then
        countprime=countprime+1
      end if
end do

allocate(primenumber(countprime), pvec(countprime))

countprime=0
do n=1, 200
      if (number(n)/=0) then
        countprime=countprime+1
        primenumber(countprime)=number(n)
      end if
end do

count=0
do a=1, K
  do b=1, K
    do c=1, K
      call gcd(a,b,c,G,primenumber,countprime)
      count=count+G
    end do
  end do
end do

write(*,*) count

deallocate(primenumber,pvec)

stop
contains
  subroutine gcd(a,b,c,G,vecin,n)
    implicit none

    integer(4), intent(in) :: a,b,c
    integer(4), intent(out) :: G
    integer(4), intent(in) :: vecin(:),n
    integer(4) :: avec(n), bvec(n),cvec(n),k

    call factor(a,vecin,n,avec)
    call factor(b,vecin,n,bvec)
    call factor(c,vecin,n,cvec)

    G=1
    do k=1, 7
      if (avec(k)==0 .or. bvec(k)==0 .or. cvec(k)==0) then
        G=G*1
      else
        G=G*(vecin(k)**min(avec(k),bvec(k),cvec(k)))
      end if
    end do

  end subroutine gcd

  subroutine factor(a,vecin, n,vecout)
    implicit none

    integer(4), intent(in) :: a,n
    integer(4), intent(in) :: vecin(:)
    integer(4), intent(out) :: vecout(:)
    integer(4) ::  k, l , count, m, ap

    vecout(1)=1

    do k=2, 7
      l=0
      count=0
      ap=a
      if (a>=vecin(k)) then
        do while (l==0)
          l=mod(ap,vecin(k))
          if (l==0) then
            count=count+1
            ap=ap/vecin(k)
          end if
        end do
        vecout(k)=count
      end if
      if (a<vecin(k)) then
        vecout(k)=0
      end if
    end do

  end subroutine factor

end program


program main
    implicit none
    integer k, a, b, c
    integer :: total = 0
    read *, k

    do a=1, k
        do b = 1, k
            do c = 1, k
                total = total + euclid(a, euclid(c, b))
            enddo
        enddo
    enddo
    print *, total

contains
    integer function euclid(a, b)
        integer, intent(in) :: a,b
        integer bigger, smaller, rem

        if ( a < b ) then
            bigger = b
            smaller = a
        else
            bigger = a
            smaller = b
        endif

        do
            rem = mod(bigger, smaller)
            if ( rem == 0 ) then
                euclid = smaller
                exit
            else
                bigger = smaller
                smaller = rem
            endif
        enddo
    end function
end program main


program main
      implicit none
      integer(4) :: k, a, b, mab, c, mabc
      integer(8) :: cnt

      read *, k
      cnt = 0
      do a = 1, k
        do b = 1, k
          do c = 1, k
            call gcd(a,b,mab)
            call gcd(mab, c, mabc)
            cnt = cnt + mabc
          end do
        end do
      end do
      print *, cnt

      contains

      subroutine gcd(m,n,j)
        implicit none
        integer, intent(in) :: m, n
        integer, intent(out) :: j
        integer :: i, l, x, y

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

      end subroutine gcd

end program main


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64)::a, i,j,k,ans
    read*, a

    ans=0
    do i = 1, a-2
        do j = i+1, a-1
            do k = j+1, a
                ans= ans+ gcd(i,gcd(j,k))
            end do
        end do
    end do
    print*, ans*3

contains
recursive function gcd(x,y) result(ret)
    integer(int64):: x,y,ret

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

    ret = gcd(y,mod(x,y))

end function
end program name


program cm
  implicit none
  integer::N
  integer(16)::ANS=0
  integer::i,j,ij,k
read*,N
do i=1,N
  do j=1,N
    ij=gcd(i,j)
    do k=1,N
      ANS=ANS+gcd(ij,k)
    end do
  end do
end do
print"(i0)",ANS
contains
integer recursive function gcd(a, b)result(res)
  integer, intent(in)::a, b
  res=merge(b, gcd(b, mod(a, b)),mod(a, b)==0)
end function
end program cm


program main
  implicit none
  integer :: N
  character(len=4000) :: S
  integer(8) :: out
  integer :: i,j,k
  integer(8) :: iR(3)
  integer,parameter :: I_R = 1, I_G = 2, I_B = 3
  read(*,*) N
  read(*,*) S
  iR(:) = 0
  do i = 1,N
     if (S(i:i) == "R") then
        iR(I_R) = iR(I_R) + 1
     end if
     if (S(i:i) == "G") then
        iR(I_G) = iR(I_G) + 1
     end if
     if (S(i:i) == "B")  then
        iR(I_B) = iR(I_B) + 1
     end if
  end do
  out = iR(1)*iR(2)*iR(3)
  do i = 1,N-2
     do j = i+1,N-1
        k = 2*j-i
        if ( k>N) cycle
        if (k <= N ) then
           if ( (S(i:i) /= S(j:j)) .and. ( S(i:i) /= S(k:k) )&
                .and. (S(j:j) /= S(k:k)) ) then
              out = out - 1
           end if
        end if
     end do
  end do
  write(*,'(i0)') out
  stop
end program main

Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800


program main
  implicit none
  integer :: N
  character(len=4000) :: S
  integer,allocatable :: nS(:)
  integer(8) :: out
  integer :: i,j,k
  integer :: iR(3)
  integer,parameter :: I_R = 1, I_G = 2, I_B = 3
  read(*,*) N
  read(*,*) S
  iR(:) = 0
  do i = 1,N
     if (S(i:i) == "R") then
        iR(I_R) = iR(I_R) + 1
     end if
     if (S(i:i) == "G") then
        iR(I_G) = iR(I_G) + 1
     end if
     if (S(i:i) == "B")  then
        iR(I_B) = iR(I_B) + 1
     end if
  end do
  out = iR(1)*iR(2)*iR(3)
  do i = 1,N-2
     do j = i+1,N-1
        k = 2*j-i
        if (k <= N ) then
           if ( (S(i:i) /= S(j:j)) .and. ( S(i:i) /= S(k:k) )&
                .and. (S(j:j) /= S(k:k)) ) then
              out = out - 1
           end if
        end if
     end do
  end do
  write(*,'(i0)') out
  stop
end program main


program main
  implicit none
  integer :: N
  character(len=4000) :: S
  integer,allocatable :: nS(:)
  integer(8) :: out
  integer :: i,j,k
  integer :: iR(3)
  integer,parameter :: I_R = 1, I_G = 2, I_B = 3
  read(*,*) N
  read(*,*) S
  iR(:) = 0
  do i = 1,N
     if (S(i:i) == "R") then
        iR(I_R) = iR(I_R) + 1
     end if
     if (S(i:i) == "G") then
        iR(I_G) = iR(I_G) + 1
     end if
     if (S(i:i) == "B")  then
        iR(I_B) = iR(I_B) + 1
     end if
  end do
  out = iR(1)*iR(2)*iR(3)
  do i = 1,N-2
     do j = i+1,N-1
        k = 2*j-i
        if (k <= N ) then
           if ( S(i:i) /= S(j:j) .and. S(i:i) /= S(k:k) &
                .and. S(j:j) /= S(k:k) ) then
              out = out - 1
           end if
        end if
     end do
  end do
  write(*,'(i0)') out
  stop
end program main


program main
  implicit none
  integer :: N
  character(len=4000) :: S
  integer,allocatable :: nS(:)
  integer(8) :: out
  integer :: i,j,k
  integer :: iR(3)
  integer,parameter :: I_R = 1, I_G = 2, I_B = 3
  integer t1, t2, t_rate, t_max, diff
  call system_clock(t1)   ! 開始時を記録
  read(*,*) N
  read(*,*) S
  iR(:) = 0
  do i = 1,N
     if (S(i:i) == "R") then
        iR(I_R) = iR(I_R) + 1
     end if
     if (S(i:i) == "G") then
        iR(I_G) = iR(I_G) + 1
     end if
     if (S(i:i) == "B")  then
        iR(I_B) = iR(I_B) + 1
     end if
  end do
  out = iR(1)*iR(2)*iR(3)
  do i = 1,N-2
     do j = i+1,N-1
        if (S(i:i) /= S(j:j) ) then
           k = j-i+j
           if (k <= N ) then
              if ( S(i:i) /= S(k:k) &
                .and. S(j:j) /= S(k:k) ) then
                 out = out - 1
              end if
           end if
        end if
     end do
  end do
  write(*,'(i0)') out
  stop
end program main


program main
  implicit none
  integer :: N
  character(len=4000) :: S
  integer,allocatable :: nS(:)
  integer(8) :: out
  integer :: i,j,k
  integer :: iR(3)
  integer,parameter :: I_R = 1, I_G = 2, I_B = 3
  integer t1, t2, t_rate, t_max, diff
  call system_clock(t1)   ! 開始時を記録
  read(*,*) N
  read(*,*) S
  iR(:) = 0
  do i = 1,N
     if (S(i:i) == "R") then
        iR(I_R) = iR(I_R) + 1
     end if
     if (S(i:i) == "G") then
        iR(I_G) = iR(I_G) + 1
     end if
     if (S(i:i) == "B")  then
        iR(I_B) = iR(I_B) + 1
     end if
  end do
  out = iR(1)*iR(2)*iR(3)
  do i = 1,N
     do j = i+1,N
        k = j-i+j
        if (k <= N ) then
           if (S(i:i) /= S(j:j) &
                .and. S(i:i) /= S(k:k) &
                .and. S(j:j) /= S(k:k) ) then
              out = out - 1
           end if
        end if
     end do
  end do
  write(*,'(i0)') out
  stop
end program main


program ABC162_D
  character(4000)::S
  integer::R=0,G=0,B=0,A
  read(*,*)N
  read(*,*)S
  do i=1,N
     if(S(i:i)=='R') R=R+1
     if(S(i:i)=='G') G=G+1
     if(S(i:i)=='B') B=B+1
  end do
  A=R*G*B
  do i=1,N
     do j=1,N
        if(i+2*j>N) cycle
        if(S(i:i)/=S(i+j:i+j) .and. S(i+j:i+j)/=S(i+2*j:i+2*j) .and. S(i+2*j:i+2*j)/=S(i:i)) A=A-1
     end do
  end do
  write(*,*)A
end program ABC162_D


program test
implicit none
integer(8) :: n,i,j,k,ans=0,rgb(3,5000)=0,hoge=0,big=0,small=0
character(len=10000) :: s

read(*,*) n
read(*,*) s

do i=1,n
  if(s(i:i)=='R')then
    rgb(1,i:n) = rgb(1,i:n) + 1
  else if(s(i:i)=='G')then
    rgb(2,i:n) = rgb(2,i:n) + 1
  else if(s(i:i)=='B')then
    rgb(3,i:n) = rgb(3,i:n) + 1
  endif
enddo

if(n<=2)then
  ans = 0
else

  do j=2,n-1
    if(s(j:j) == 'R') then
      ans = ans + rgb(2,j-1)*(rgb(3,n)-rgb(3,j))
      ans = ans + rgb(3,j-1)*(rgb(2,n)-rgb(2,j))

      big = j+1
      small = j-1

      do while(big <= n .and. small >= 1)
        if((s(small:small)=='G' .and. s(big:big)=='B') .or. (s(small:small)=='B' .and. s(big:big)=='G')) then
         ans = ans - 1
        endif
        big = big + 1
        small = small - 1
      enddo

    endif

    if(s(j:j) == 'G') then
      ans = ans + rgb(1,j-1)*(rgb(3,n)-rgb(3,j))
      ans = ans + rgb(3,j-1)*(rgb(1,n)-rgb(1,j))

      big = j+1
      small = j-1

      do while(big <= n .and. small >= 1)
        if((s(small:small)=='R' .and. s(big:big)=='B') .or. (s(small:small)=='B' .and. s(big:big)=='R')) then
         ans = ans - 1
        endif
        big = big + 1
        small = small - 1
      enddo

    endif

    if(s(j:j) == 'B') then
      ans = ans + rgb(1,j-1)*(rgb(2,n)-rgb(2,j))
      ans = ans + rgb(2,j-1)*(rgb(1,n)-rgb(1,j))

      big = j+1
      small = j-1

      do while(big <= n .and. small >= 1)
        if((s(small:small)=='R' .and. s(big:big)=='G') .or. (s(small:small)=='G' .and. s(big:big)=='R')) then
         ans = ans - 1
        endif
        big = big + 1
        small = small - 1
      enddo

    endif

  enddo

endif

write(*,*) ans

end


program test

integer :: n,i,j,k,ans=0,rgb(3,4000)=0,hoge=0
character(len=4000) :: s

read(*,*) n
read(*,*) s

do i=1,n
  if(s(i:i)=='R')then
    rgb(1,i:n) = rgb(1,i:n) + 1
  else if(s(i:i)=='G')then
    rgb(2,i:n) = rgb(2,i:n) + 1
  else if(s(i:i)=='B')then
    rgb(3,i:n) = rgb(3,i:n) + 1
  endif
enddo

if(n<=2)then
  ans=0
else
  do i=1,n-2
    if(s(i:i) =='R') then
      do j=i+1,n-1
        hoge = j+j-i
        if(s(j:j)=='G') then
          ans = ans + rgb(3,n) - rgb(3,j)
          if(j+j-i <= n) then
            if(s(hoge:hoge) == 'B') then
              ans = ans - 1
            endif
          endif
        else if(s(j:j)=='B') then
          ans = ans + rgb(2,n) - rgb(2,j)
          if(j+j-i <= n) then
            if(s(hoge:hoge) == 'G') then
              ans = ans - 1
            endif
          endif
        endif
      enddo
    endif

    if(s(i:i) =='G') then
      do j=i+1,n-1
        hoge = j+j-i
        if(s(j:j)=='R') then
          ans = ans + rgb(3,n) - rgb(3,j)
          if(j+j-i <= n) then
            if(s(hoge:hoge) == 'B') then
              ans = ans - 1
            endif
          endif
        else if(s(j:j)=='B') then
          ans = ans + rgb(1,n) - rgb(1,j)
          if(j+j-i <= n) then
            if(s(hoge:hoge) == 'R') then
              ans = ans - 1
            endif
          endif
        endif
      enddo
    endif

    if(s(i:i) =='B') then
      do j=i+1,n-1
        hoge = j+j-i
        if(s(j:j)=='R') then
          ans = ans + rgb(2,n) - rgb(2,j)
          if(j+j-i <= n) then
            if(s(hoge:hoge) == 'G') then
              ans = ans - 1
            endif
          endif
        else if(s(j:j)=='G') then
          ans = ans + rgb(1,n) - rgb(1,j)
          if(j+j-i <= n) then
            if(s(hoge:hoge) == 'R') then
              ans = ans - 1
            endif
          endif
        endif
      enddo
    endif

  enddo
endif

write(*,*) ans

end


program ABC162D
    implicit none
    integer(8)::N,r,g,b,i,j,ans
    character(4000)::S
    read*,N,S
    r=0
    g=0
    b=0

    do i=1,N
        if(S(i:i)=="R")r=r+1
        if(S(i:i)=="G")g=g+1
        if(S(i:i)=="B")b=b+1
    end do

    ans=r*g*b

    do i=1,N
        do j=1,N
            if(i+2*j<=N)then
                if(S(i:i)/=S(i+j:i+j).and.S(i+j:i+j)/=S(i+2*j:i+2*j).and.S(i+2*j:i+2*j)/=S(i:i))then
                    ans=ans-1
                end if
            end if
        end do
    end do

    print'(i0)',ans
end program ABC162D


program main

  implicit none
  integer :: n, i, j, nr, ng, nb
  integer :: ans
  character(4000) :: s

  read(*,*) n
  read(*,*) s

  nr = 0
  ng = 0
  nb = 0
  do i = 1, n
    if( s(i:i) == "R" ) then
      nr = nr + 1
    else if( s(i:i) == "G" ) then
      ng = ng + 1
    else
      nb = nb + 1
    end if
  end do
  ans = nr * ng * nb
  do i = 1, n-2
    do j = i+1, n-1
    if( s(i:i) == "R" ) then
      if( s(j:j) == "G" ) then
        if( s(2*j-i:2*j-i) == "B") ans = ans - 1
      end if
    end if
    if( s(i:i) == "R" ) then
      if( s(j:j) == "B" ) then
        if( s(2*j-i:2*j-i) == "G") ans = ans - 1
      end if
    end if
    if( s(i:i) == "G" ) then
      if( s(j:j) == "R" ) then
        if( s(2*j-i:2*j-i) == "B") ans = ans - 1
      end if
    end if
    if( s(i:i) == "G" ) then
      if( s(j:j) == "B" ) then
        if( s(2*j-i:2*j-i) == "R") ans = ans - 1
      end if
    end if
    if( s(i:i) == "B" ) then
      if( s(j:j) == "R" ) then
        if( s(2*j-i:2*j-i) == "G") ans = ans - 1
      end if
    end if
    if( s(i:i) == "B" ) then
      if( s(j:j) == "G" ) then
        if( s(2*j-i:2*j-i) == "R") ans = ans - 1
      end if
    end if
    end do
  end do

  print*, ans

end program main


program main
  implicit none

  integer :: n, i, j, k, cnt, r, g, b, dis
  character(4000) :: s

  read *, n
  read *, s

  r = 0
  g = 0
  b = 0
  do i = 1, n
     select case(s(i:i))
     case('R')
        r = r + 1
     case('G')
        g = g + 1
     case('B')
        b = b + 1
     end select
  end do

  cnt = 0
  do dis = 1, n/2
     do i = 1, n-2*dis
        if (s(i:i) /= s(i+dis:i+dis) &
             .and. s(i:i) /= s(i+2*dis:i+2*dis) &
               .and. s(i+dis:i+dis) /= s(i+2*dis:i+2*dis)) then
           cnt = cnt + 1
        end if
     end do
  end do

  print *, r*g*b - cnt

  stop
end program main


program main
  implicit none

  integer :: n, i, j, k, ans
  character(4000) :: s

  read *, n
  read *, s

  ans = 0
  do i = 1, n-2
     do j = i, n-1
        do k = j, n
           if (s(i:i) /= s(j:j) .and. s(k:k) /= s(j:j) .and. s(i:i) /= s(k:k) &
                & .and. j-i /= k-j) then
              ans = ans + 1
           end if
        end do
     end do
  end do

  print *, ans

  stop
end program main


   program abc162dr
      implicit none
      integer::n
      character(len=4000)::s
      integer::i,j,k
      integer(8)::ans
      integer(8)::x(3)=0

         read *,n
         read *,s

         do i=1,n
            if (s(i:i)=='R') x(1)=x(1)+1
            if (s(i:i)=='G') x(2)=x(2)+1
            if (s(i:i)=='B') x(3)=x(3)+1
         end do

         ans=x(1)*x(2)*x(3)

         do i=1,n-2
            do j=i+1,(n-i)/2+i
               k=(j-i)+j
               if ((s(i:i)/=s(j:j)) .and. (s(i:i)/=s(k:k)) &
                                   .and. (s(j:j)/=s(k:k))) then
                  ans=ans-1
               end if
            end do
         end do

         print *,ans

   end program abc162dr


   program abc162dr
      implicit none
      integer::n
      character(len=4000)::s
      integer::i,j,k,ans
      integer::x(3)=0

         read *,n
         read *,s

         do i=1,n
            if (s(i:i)=='R') x(1)=x(1)+1
            if (s(i:i)=='G') x(2)=x(2)+1
            if (s(i:i)=='B') x(3)=x(3)+1
         end do

         ans=x(1)*x(2)*x(3)

         do i=1,n-2
           do j=i+1,n-1
              k=(j-i)+j
              if (k>n) cycle
              if (s(i:i)/=s(j:j) .and. s(i:i)/=s(k:k) &
                                 .and. s(j:j)/=s(k:k)) then
                 ans=ans-1
              end if
           end do
         end do

         print *,ans

   end program abc162dr


   program abc162dr
      implicit none
      integer::n
      character(len=4000)::s
      integer::i,j,k,ans
      integer::x(3)=0

         read *,n
         read *,s

         do i=1,n
            if (s(i:i)=='R') x(1)=x(1)+1
            if (s(i:i)=='G') x(2)=x(2)+1
            if (s(i:i)=='B') x(3)=x(3)+1
         end do

         ans=x(1)*x(2)*x(3)

         do i=1,n-2
           do j=i+1,(n-i)/2+i
              k=(j-i)+j
              if (s(i:i)==s(j:j) .or. s(i:i)==s(k:k) &
                                 .or. s(j:j)==s(k:k)) cycle
                 ans=ans-1
           end do
         end do

         print *,ans

   end program abc162dr


   program abc162dr
      implicit none
      integer::n
      character(len=4000)::s
      character::t
      integer::i,j,k,ans
      integer::x(3)=0

         read *,n
         read *,s

         do i=1,n
            if (s(i:i)=='R') x(1)=x(1)+1
            if (s(i:i)=='G') x(2)=x(2)+1
            if (s(i:i)=='B') x(3)=x(3)+1
         end do

         ans=x(1)*x(2)*x(3)

         do i=1,n-2
           do j=i+1,(n-i)/2+i
              k=(j-i)+j
              if (s(i:i)/=s(j:j) .and. s(i:i)/=s(k:k) &
                                 .and. s(j:j)/=s(k:k)) then
                 ans=ans-1
              end if
           end do
         end do

         print *,ans

   end program abc162dr


program main
  implicit none
  integer(8) :: n, i, j, kng, cr(0:4000), cb(0:4000), cg(0:4000)
  integer(8) :: res
  character(4000) :: s
  character(2) :: sij
  character(1) :: cng

  read *, n
  read *, s
  cr = 0
  cb = 0
  cg = 0
  do i = 1, n
    cr(i) = cr(i - 1)
    cb(i) = cb(i - 1)
    cg(i) = cg(i - 1)
    if (s(i:i) == "R") then
      cr(i) = cr(i) + 1
    else if (s(i:i) == "G") then
      cg(i) = cg(i) + 1
    else
      cb(i) = cb(i) + 1
    end if
  end do
  res = 0
  do i = 1, n - 2
    do j = i + 1, n - 1
      if (s(i:i) == s(j:j)) cycle
      sij = s(i:i) // s(j:j)
      kng = j + j - i
      if (sij == "RG" .or. sij == "GR") then
        res = res + cb(n) - cb(j)
        cng = "B"
      else if (sij == "RB" .or. sij == "BR") then
        res = res + cg(n) - cg(j)
        cng = "G"
      else
        res = res + cr(n) - cr(j)
        cng = "R"
      end if
      if (kng <= n) then
        if (s(kng:kng) == cng) res = res - 1
      end if
    end do
  end do
  print "(i0)", res
end program main


program main
  implicit none
  integer(8) :: n, i, j, kng, cr(0:4000), cb(0:4000), cg(0:4000)
  integer(8) :: res
  character(4000) :: s
  character(2) :: sij

  read *, n
  read *, s
  cr = 0
  cb = 0
  cg = 0
  do i = 1, n
    cr(i) = cr(i - 1)
    cb(i) = cb(i - 1)
    cg(i) = cg(i - 1)
    if (s(i:i) == "R") then
      cr(i) = cr(i) + 1
    else if (s(i:i) == "G") then
      cg(i) = cg(i) + 1
    else
      cb(i) = cb(i) + 1
    end if
  end do
  res = 0
  do i = 1, n - 2
    do j = i + 1, n - 1
      if (s(i:i) == s(j:j)) cycle
      sij = s(i:i) // s(j:j)
      kng = j + j - i
      if (sij == "RG" .or. sij == "GR") then
        res = res + cb(n) - cb(j)
        if (s(kng:kng) == "B") res = res - 1
      else if (sij == "RB" .or. sij == "BR") then
        res = res + cg(n) - cg(j)
        if (s(kng:kng) == "G") res = res - 1
      else
        res = res + cr(n) - cr(j)
        if (s(kng:kng) == "R") res = res - 1
      end if
    end do
  end do
  print "(i0)", res
end program main


program main
      implicit none
      integer(4) :: n, i, j, k
      integer(4) :: sumR, sumB, sumG
      integer(8) :: sum
      character(4000) :: s

      read *, n
      read *, s

      sumR = 0
      sumB = 0
      sumG = 0

      do i = 1, n
        if ( s(i:i) == 'R' ) sumR = sumR + 1
        if ( s(i:i) == 'B' ) sumB = sumB + 1
        if ( s(i:i) == 'G' ) sumG = sumG + 1
      end do
      sum = sumR*sumB*sumG

      do i = 1, n/2+1
        do j = 1, n
          k = j + 2*i
          if ( k < n+1 ) then
            if( ( s(j:j) /= s(i+j:i+j) ) .and. &
               (s(j+2*i:j+2*i) /= s(j+i:j+i)) .and. &
               (s(j:j) /= s(j+2*i:j+2*i)) ) then
               sum = sum - 1
            end if
          end if
        end do
      end do

      print *, sum

end program main


module a
  integer, allocatable :: anr(:), ang(:), anb(:)
end module a

program main
  use a

  implicit none
  integer :: n, i, j, nr, ng, nb
  integer :: ans
  character(4000) :: s

  read(*,*) n
  allocate( anr(n) )
  allocate( ang(n) )
  allocate( anb(n) )
  read(*,*) s

  nr = 0
  ng = 0
  nb = 0
  do i = 1, n
    if( s(i:i) == "R" ) then
      nr = nr + 1
    else if( s(i:i) == "G" ) then
      ng = ng + 1
    else
      nb = nb + 1
    end if
  end do
  do i = 1, n
    if( s(i:i) == "R" ) then
      nr = nr - 1
    else if( s(i:i) == "G" ) then
      ng = ng - 1
    else
      nb = nb - 1
    end if
    anr(i) = nr
    ang(i) = ng
    anb(i) = nb
  end do
  ans = 0
  do i = 1, n-2
    do j = i+1, n-1
    if( s(i:i) == "R" ) then
      if( s(j:j) == "G" ) then
        ans = ans + anb(j)
        if( s(2*j-i:2*j-i) == "B") ans = ans - 1
      end if
    end if
    if( s(i:i) == "R" ) then
      if( s(j:j) == "B" ) then
        ans = ans + ang(j)
        if( s(2*j-i:2*j-i) == "G") ans = ans - 1
      end if
    end if
    if( s(i:i) == "G" ) then
      if( s(j:j) == "R" ) then
        ans = ans + anb(j)
        if( s(2*j-i:2*j-i) == "B") ans = ans - 1
      end if
    end if
    if( s(i:i) == "G" ) then
      if( s(j:j) == "B" ) then
        ans = ans + anr(j)
        if( s(2*j-i:2*j-i) == "R") ans = ans - 1
      end if
    end if
    if( s(i:i) == "B" ) then
      if( s(j:j) == "R" ) then
        ans = ans + ang(j)
        if( s(2*j-i:2*j-i) == "G") ans = ans - 1
      end if
    end if
    if( s(i:i) == "B" ) then
      if( s(j:j) == "G" ) then
        ans = ans + anr(j)
        if( s(2*j-i:2*j-i) == "R") ans = ans - 1
      end if
    end if
    end do
  end do

  print*, ans

end program main


module a
  integer(8), allocatable :: anr(:), ang(:), anb(:)
end module a

program main
  use a

  implicit none
  integer(8) :: n, i, j, ans ,nr, ng, nb
  character(4000) :: s

  read(*,*) n
  allocate( anr(n) )
  allocate( ang(n) )
  allocate( anb(n) )
  read(*,*) s

  nr = 0
  ng = 0
  nb = 0
  do i = 1, n
    if( s(i:i) == "R" ) then
      nr = nr + 1
    else if( s(i:i) == "G" ) then
      ng = ng + 1
    else
      nb = nb + 1
    end if
  end do
  do i = 1, n
    if( s(i:i) == "R" ) then
      nr = nr - 1
    else if( s(i:i) == "G" ) then
      ng = ng - 1
    else
      nb = nb - 1
    end if
    anr(i) = nr
    ang(i) = ng
    anb(i) = nb
  end do
  ans = 0
  do i = 1, n-2
    do j = i+1, n-1
    if( s(i:i) == "R" ) then
      if( s(j:j) == "G" ) then
        ans = ans + anb(j)
        if( s(2*j-i:2*j-i) == "B") ans = ans - 1
      end if
    end if
    if( s(i:i) == "R" ) then
      if( s(j:j) == "B" ) then
        ans = ans + ang(j)
        if( s(2*j-i:2*j-i) == "G") ans = ans - 1
      end if
    end if
    if( s(i:i) == "G" ) then
      if( s(j:j) == "R" ) then
        ans = ans + anb(j)
        if( s(2*j-i:2*j-i) == "B") ans = ans - 1
      end if
    end if
    if( s(i:i) == "G" ) then
      if( s(j:j) == "B" ) then
        ans = ans + anr(j)
        if( s(2*j-i:2*j-i) == "R") ans = ans - 1
      end if
    end if
    if( s(i:i) == "B" ) then
      if( s(j:j) == "R" ) then
        ans = ans + ang(j)
        if( s(2*j-i:2*j-i) == "G") ans = ans - 1
      end if
    end if
    if( s(i:i) == "B" ) then
      if( s(j:j) == "G" ) then
        ans = ans + anr(j)
        if( s(2*j-i:2*j-i) == "R") ans = ans - 1
      end if
    end if
    end do
  end do

  print*, ans

end program main


program main
    implicit none
    integer n
    integer, dimension(4000) :: r, g, b
    integer i, j, k, ir, ig, ib
    integer nr, ng, nb, d1, d2
    integer c, d
    character s*4000
    read(*,*) n
    read(*,*) s

    do i = 1,4000
        r(i) = 0
        g(i) = 0
        b(i) = 0
    end do

    ir = 1
    ig = 1
    ib = 1

    do i = 1,n
        if(s(i:i) == "R") then
            r(ir) = i
            ir = ir + 1
        else if(s(i:i) == "G") then
            g(ig) = i
            ig = ig + 1
        else if(s(i:i) == "B") then
            b(ib) = i
            ib = ib + 1
        end if
    end do

    nr = ir - 1
    ng = ig - 1
    nb = ib - 1
    c = 0
    d = 0

    do ir = 1, nr
        do ig = 1, ng
            do ib = 1, nb
                i = min(r(ir),g(ig),b(ib))
                k = max(r(ir),g(ig),b(ib))
                j = r(ir) + g(ig) + b(ib) - i - k
                if((j-i) == (k-j)) then
                    d = d + 1
                end if
            end do
        end do
    end do
    write(*,*) nr*ng*nb - d

end program


module a
  integer, allocatable :: anr(:), ang(:), anb(:)
end module a

program main
  use a

  implicit none
  integer :: n, i, j, ans ,nr, ng, nb
  character(4000) :: s

  read(*,*) n
  allocate( anr(n) )
  allocate( ang(n) )
  allocate( anb(n) )
  read(*,*) s

  nr = 0
  ng = 0
  nb = 0
  do i = 1, n
    if( s(i:i) == "R" ) then
      nr = nr + 1
    else if( s(i:i) == "G" ) then
      ng = ng + 1
    else
      nb = nb + 1
    end if
  end do
  do i = 1, n
    if( s(i:i) == "R" ) then
      nr = nr - 1
    else if( s(i:i) == "G" ) then
      ng = ng - 1
    else
      nb = nb - 1
    end if
    anr(i) = nr
    ang(i) = ng
    anb(i) = nb
  end do
  ans = 0
  do i = 1, n-2
    do j = i+1, n-1
    if( s(i:i) == "R" ) then
      if( s(j:j) == "G" ) then
        ans = ans + anb(j)
        if( s(2*j-i:2*j-i) == "B") ans = ans - 1
      end if
    end if
    if( s(i:i) == "R" ) then
      if( s(j:j) == "B" ) then
        ans = ans + ang(j)
        if( s(2*j-i:2*j-i) == "G") ans = ans - 1
      end if
    end if
    if( s(i:i) == "G" ) then
      if( s(j:j) == "R" ) then
        ans = ans + anb(j)
        if( s(2*j-i:2*j-i) == "B") ans = ans - 1
      end if
    end if
    if( s(i:i) == "G" ) then
      if( s(j:j) == "B" ) then
        ans = ans + anr(j)
        if( s(2*j-i:2*j-i) == "R") ans = ans - 1
      end if
    end if
    if( s(i:i) == "B" ) then
      if( s(j:j) == "R" ) then
        ans = ans + ang(j)
        if( s(2*j-i:2*j-i) == "G") ans = ans - 1
      end if
    end if
    if( s(i:i) == "B" ) then
      if( s(j:j) == "G" ) then
        ans = ans + anr(j)
        if( s(2*j-i:2*j-i) == "R") ans = ans - 1
      end if
    end if
    end do
  end do

  print*, ans

end program main


program main
    implicit none
    integer n, i, j, k
    integer :: count = 0
    character(:), allocatable :: s

    read *, n
    allocate( character(n) :: s)

    read *, s

    do i=1, n-3
        do j=i+1, n-1
            if ( s(i:i) == s(j:j) ) then
                cycle
            endif
            do k=j+1, n
                if ( s(i:i) /= s(k:k) .and. s(j:j) /= s(k:k) .and. j-i /= k-j) then
                    count = count + 1
                endif
            enddo
        enddo
    enddo
    print *, count
end program main


program main
    implicit none
    integer(8) n
    integer(8), dimension(4000) :: r, g, b
    integer i, j, k, ir, ig, ib
    integer nr, ng, nb, d1, d2
    integer c, d
    character s*4000
    read(*,*) n
    read(*,*) s

    do i = 1,4000
        r(i) = 0
        g(i) = 0
        b(i) = 0
    end do

    ir = 1
    ig = 1
    ib = 1

    do i = 1,n
        if(s(i:i) == "R") then
            r(ir) = i
            ir = ir + 1
        else if(s(i:i) == "G") then
            g(ig) = i
            ig = ig + 1
        else if(s(i:i) == "B") then
            b(ib) = i
            ib = ib + 1
        end if
    end do

    nr = ir - 1
    ng = ig - 1
    nb = ib - 1
    c = 0
    d = 0

    do ir = 1, nr
        do ig = 1, ng
            do ib = 1, nb
                i = min(r(ir),g(ig),b(ib))
                k = max(r(ir),g(ig),b(ib))
                j = r(ir) + g(ig) + b(ib) - i - k
                if((j-i) == (k-j)) then
                    d = d + 1
                end if
            end do
        end do
    end do
    write(*,*) nr*ng*nb - d

end program


program main
    implicit none
    integer n, i, j, k
    integer :: count = 0
    character(:), allocatable :: s

    read *, n
    allocate( character(n) :: s)

    read *, s

    do i=1, n-3
        do j=i+1, n-1
            do k=j+1, n
                if ( s(i:i) /= s(j:j) .and. s(i:i) /= s(k:k) .and. s(j:j) /= s(k:k) .and. j-i /= k-j) then
                    count = count + 1
                endif
            enddo
        enddo
    enddo
    print *, count
    end program main


program example
	implicit none

	integer(8) ::N,i,j,k,wa=0
    character(4000) S

    read(*,*) N
    read(*,*) S

    do i=1,N,1

    	do j=i+1,N,1
            if(S(i:i)==S(j:j)) goto 2
        	do k=j+1,N,1
                if(j-i==k-j) goto 1

            	if(S(i:i)/=S(k:k) .and. S(k:k)/=S(j:j)) then
                	wa=wa+1
                end if
            1 end do

        2 end do
    end do

    write(*,*) wa

end program example


      PROGRAM piyo
      IMPLICIT NONE
      character(4000) :: s
      integer(16) :: numR(4000)=0,numG(4000)=0,numB(4000)=0
      integer(16) :: rgb(3)=0,i,j,n,ans=0,notK

      read*,n
      read*,s(1:n)

      if( s(1:1)=='R' )then
          numR(1) = 1
      else if( s(1:1)=='G' )then
          numG(1) = 1
      else
          numB(1) = 1
      endif

      do i = 2,n
        if( s(i:i)=='R' )then
          rgb(1) = 1
        else if( s(i:i)=='G' )then
          rgb(2) = 1
        else
          rgb(3) = 1
        end if

        numR(i) = numR(i-1) + rgb(1)
        numG(i) = numG(i-1) + rgb(2)
        numB(i) = numB(i-1) + rgb(3)
        rgb = 0
      end do

      ans = 0
      do i = 1,n-2
        do j = i+1,n-1

          notK = j + (j-i)
          if( notK>4000 ) notK = 4000
          rgb(:) = 0

          if( (s(i:i)=='R'.and.s(j:j)=='G') .or. &
            & (s(j:j)=='R'.and.s(i:i)=='G') )then
            rgb(3) = 1
          else if( (s(i:i)=='G'.and.s(j:j)=='B') .or. &
                 & (s(j:j)=='G'.and.s(i:i)=='B') )then
            rgb(1) = 1
          else
            rgb(2) = 1
          end if

          if( rgb(1)==1 )then
            ans = ans + (numR(n)-numR(j))
            if( s(notK:notK)=='R' ) ans = ans - 1
          else if( rgb(2)==1 )then
            ans = ans + (numG(n)-numG(j))
            if( s(notK:notK)=='G' ) ans = ans - 1
          else
            ans = ans + (numB(n)-numB(j))
            if( s(notK:notK)=='B' ) ans = ans - 1
          end if
        end do
      end do

      print*,ans

      !debugg
  !    print*,numR(1:n)
  !    print*,numG(1:n)
  !    print*,numB(1:n)

      END PROGRAM


program ABC162_D
  character(4000)::S
  read(5,*)N
  read(5,*)S
  L=0
  do I=1,N-3
     do J=I+1,N-1
        do K=J+1,N
           if(S(I:I)/=S(J:J)) then
              if(S(J:J)/=S(K:K) .and. S(I:I)/=S(K:K)) then
                 if(J-I/=K-J) L=L+1
              end if
           end if
        end do
     end do
  end do
  write(6,*)L
end program ABC162_D


program abc162d
   implicit none
   integer :: N, i, j, k
   character(4000) :: str
   character, allocatable :: S(:)
   integer :: count

   read(*,*) N
   read(*,*) str

   allocate(S(N))

   do i = 1, N
      S(i) = str(i:i)
   enddo

   count = 0
   do k = 3, N
      do j = 2, k-1
         if (S(j) == S(k)) cycle
         do i = 1, j-1
            if (j-i == k-j) cycle
            if (S(i).ne.S(j) .and. S(i).ne.S(k) ) then
               count= count + 1
            endif
         enddo
      enddo
   enddo

   print *, count

end program abc162d


program rgb_triplets
  implicit none
  integer :: n, i, j, a(4000) = 0, p, q
  integer(8) :: c(3) = 0_8, x = 0_8
  character(4000) :: s
  read(*,*) n
  read(*,*) s
  do i = 1, n
    if (s(i:i) == "R") then
      a(i) = 1
    else if (s(i:i) == "G") then
      a(i) = 2
    else
      a(i) = 3
    end if
  end do
  do i = 1, n
    p = mod(a(i),3)+1
    q = mod(p,3)+1
    x = x+c(p)*c(q)
    c(a(i)) = c(a(i))+1_8
  end do
  do j = 1, n
    do i = 1, n
      p = i+j
      q = p+j
      if (q > n) exit
      if (a(i) == a(p) .or. a(p) == a(q) .or. a(q) == a(i)) cycle
      x = x-1_8
    end do
  end do
  write(*,'(i0)') x
end program rgb_triplets


program sample
	implicit none
    character(len=3) ::a,b,c
    integer :: x(3)

    read(*,*) a,b,c

    write(*,'(a,X,a,X,a)') trim(adjustl(c)),trim(adjustl(a)),trim(adjustl(b))

    stop
end program sample

Score : 100 points

Problem Statement

We have three boxes A, B, and C, each of which contains an integer.

Currently, the boxes A, B, and C contain the integers X, Y, and Z, respectively.

We will now do the operations below in order. Find the content of each box afterward.

Swap the contents of the boxes A and B

Swap the contents of the boxes A and C

Constraints

1 \leq X,Y,Z \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the integers contained in the boxes A, B, and C, in this order, with space in between.

Sample Input 1

1 2 3

Sample Output 1

3 1 2

After the contents of the boxes A and B are swapped, A, B, and C contain 2, 1, and 3, respectively.

Then, after the contents of A and C are swapped, A, B, and C contain 3, 1, and 2, respectively.

Sample Input 2

100 100 100

Sample Output 2

100 100 100

Sample Input 3

41 59 31

Sample Output 3

31 41 59


program main
	implicit none
    integer::a,b,c,d,e,f
    read(*,*)a,b,c
    d=c
    e=a
    f=b
    write(*,*)d,e,f
    stop
end program main


program prob15
    implicit none
    character(len=5) :: a,b,c
    read(*,*) a,b,c

    write(*,'(a,1X,a,1X,a)') trim(adjustl(c)), trim(adjustl(a)), trim(adjustl(b))

    stop
contains
end program prob15


program ABC

 implicit none
 integer :: x,y,z,a,b,c

 read*,x,y,z

 a=y
 b=x
 c=a
 a=z

 print*,a,b,c

end program ABC


 program sample1
      implicit none

      integer X,Y,Z
      read(*,*)X
      read(*,*)Y
      read(*,*)Z
      write(*,'(i0)') Z
      write(*,'(i0)') X
      write(*,'(i0)') Y
      stop
      end program sample1


program abc
	implicit none
	integer :: a,b,c,aa,bb,cc

	read(*,*) a,b,c

    aa = c
    bb = a
	cc = b

	write(*,*) aa,bb,cc

end program abc


program example
	implicit none

	integer X,Y,Z,A

    read(*,*) X,Y,Z

    A=X

    X=Y

    Y=A

    A=X

    X=Z

    Z=A

    write(*,"(i3,1x,i3,1x,i3)") X,Y,Z

end program example


      PROGRAM piyo
      IMPLICIT NONE
      integer :: poyo(3)

      read*, poyo(:)

      print'(3(i0,x))',poyo(3),poyo(1),poyo(2)

      END PROGRAM


program main
    implicit none
    integer x,y,z
    read(*,*) x,y,z
    write(*,*) z,x,y
end program


program popular_vote
    integer m, n, i
    integer :: ans = 0, cnt = 0
    integer,allocatable :: a(:)
    read *, n, m
    allocate(a(n))
    read *, a(n)
    do i = 1, n
        ans = ans + a(i)
    end do
    do i = 1, n
        if(a(i)/ans > 1/(4*m))then
            cnt = cnt + 1
            if(cnt >= m)then
                print *, 'Yes'
                exit
            else if(cnt < m .and. i .eq. n)then
                print *, 'No'
                exit
            end if
        end if
    end do
end program

Score : 200 points

Problem Statement

We have held a popularity poll for N items on sale. Item i received A_i votes.

From these N items, we will select M as popular items. However, we cannot select an item with less than \dfrac{1}{4M} of the total number of votes.

If M popular items can be selected, print Yes; otherwise, print No.

Constraints

1 \leq M \leq N \leq 100

1 \leq A_i \leq 1000

A_i are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_N

Output

If M popular items can be selected, print Yes; otherwise, print No.

Sample Input 1

4 1
5 4 2 1

Sample Output 1

Yes

There were 12 votes in total. The most popular item received 5 votes, and we can select it.

Sample Input 2

3 2
380 19 1

Sample Output 2

No

There were 400 votes in total. The second and third most popular items received less than \dfrac{1}{4\times 2} of the total number of votes, so we cannot select them. Thus, we cannot select two popular items.

Sample Input 3

12 3
4 56 78 901 2 345 67 890 123 45 6 789

Sample Output 3

Yes


program main
integer :: n,m,cnt = 0
integer,allocatable :: a(:)

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

do i = 1,n
if (a(i) >= sum(a)/(4*m)) cnt = cnt + 1
end do
if(cnt >= m) then
write(*,*)'Yes'
else
write(*,*)'No'
end if
end program main


program main
  implicit none
  integer :: M,N
  integer,allocatable :: A(:)
  integer :: i
  integer :: out, tot
  read(*,*) N, M
  allocate(A(N))
  read(*,*) A(:)
  out = 0
  tot = sum(A)
  do i = 1,N
     if (A(i) < tot/(4.e0*M) ) cycle
     out = out + 1
  end do
  if (out >= M ) then
     write(*,'(a)') 'Yes'
  else
     write(*,'(a)') 'No'
  end if
end program main


program popular

 implicit none
 integer :: n,m,i,total,choice,minm
 integer,allocatable,dimension(:) :: a

 read*,n,m

 allocate(a(n))

 read*,a(1:n)

 total=0
 do i=1,n
  total=total+a(i)
 end do

 minm=total/(4*m)

 choice=0
 do i=1,n
  if(a(i)>=minm) choice=choice+1
 end do

 if(choice>=m) then
   print*,'Yes'
 else
   print*,'No'
 end if

end program popular


program popular

 implicit none
 integer :: n,m,i,total,choice,minm
 integer,allocatable::a(:)

 read*,n,m

 allocate(a(1:n))

 read*,a(1:n)

 total=0
 do i=1,n
  total=total+a(i)
 end do

 minm=total/(4*m)

 choice=0
 do i=1,n
  if(a(i)>=minm) choice=choice+1
 end do

 if(choice>=m) then
   print*,'Yes'
 else
   print*,'No'
 end if

end program popular


program test
implicit none
integer(8) :: n,m,a(1000),i,j,k,total=0,count=0

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

do i=1,n
  total = total + a(i)
enddo

do i=1,n
  if(4*m*a(i) >= total) then
    count = count + 1
  endif
enddo

if(count >= m) then
  write(*,*) 'Yes'
else
  write(*,*) 'No'
endif

end


 program sample1
      implicit none

      integer N,M,A(1000),i,S,count
      read(*,*) N,M
      read(*,*) (A(i),i=1,N)
      count=0
      S=0
      do i=1,N
         S=A(i)+S
      end do
      do i=1,N
         if (A(i)>=S/4.0/M) then
            count=count+1
         end if
      end do
      if (count>=M) then
          write(*,*) "Yes"
      else
          write(*,*)"No"
      end if

      stop
      end program sample1


program ABC161B
    implicit none
    integer(8)::N,M,a_select,i,A_all
    integer(8),allocatable,dimension(:)::A
    read*,N,M
    allocate(A(N))
    read*,A
    a_select=0
    A_all=sum(A)

    do i=1,N
        if(4*M*A(i)>=A_all)a_select=a_select+1
    end do

    if(a_select>=M)then
        print'(A)',"Yes"
    else
        print'(A)',"No"
    end if
end program ABC161B


program ABC161_B
    implicit none
    integer n,m,dai,kari,i,j
    integer,allocatable :: a(:)
    read(*,*) n,m
    allocate(a(n))
    read(*,*) a(1:n)
    dai=sum(a(1:n))/4/m
    do j=1,n
        do i=1,n-1
            if(a(i)<=a(i+1)) then
                kari=a(i)
                a(i)=a(i+1)
                a(i+1)=kari
            end if
        end do
    end do
    if(a(m)<dai) write(*,*) 'No'
    if(a(m)>=dai) write(*,*) 'Yes'
end program ABC161_B


program main
integer N,M,i,j
integer,allocatable,dimension(:) :: A
read (*,*) N,M
allocate(A(N))
read (*,*) A
j=0
do i=1,N
	if(A(i)*4*M .gt. sum(A)) j=j+1
end do
if (j .ge. M )then
print *,"Yes"
else
print *,"No"
endif
end program


program swap
  implicit none

  integer :: N, M
  integer :: i, j
  integer :: sum_vote
  real(kind=8) :: rate
  integer, allocatable :: A(:)

  read *, N, M

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

  sum_vote = sum(A(:))
  rate = sum_vote * dble(4*M)**(-1)

  j = 0
  do i = 1, N
    if (A(i) >= rate) then
      j = j + 1
    end if
  end do

  if (j >= M) then
    print *, "Yes"
  else if (j < M) then
    print *, "No"
  end if
end program swap


program swap
  implicit none

  integer :: N, M
  integer :: i, j
  integer :: sum_vote
  real(kind=8) :: rate
  integer, allocatable :: A(:)

  read *, N, M

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

  sum_vote = sum(A(:))
  rate = sum_vote * (dble(4*M)**(-1))

  j = 0
  do i = 1, N
    if (A(i) > rate) then
      j = j + 1
    end if
  end do

  if (j >= M) then
    print *, "Yes"
  else
    print *, "No"
  end if
end program swap


program swap
  implicit none

  integer :: N, M
  integer :: i, j
  integer :: sum_vote
  real(kind=8) :: rate
  integer, allocatable :: A(:)

  read *, N, M

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

  sum_vote = sum(A(:))
  rate = sum_vote * dble((4*M)**(-1))

  j = 0
  do i = 1, N
    if (A(i) > rate) then
      j = j + 1
    end if
  end do

  if (j >= M) then
    print *, "Yes"
  else
    print *, "No"
  end if
end program swap


program swap
  implicit none

  integer :: N, M
  integer :: i, j
  integer :: sum_vote
  real(kind=8) :: rate
  integer, allocatable :: A(:)

  read *, N, M

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

  sum_vote = sum(A(:))
  rate = sum_vote * dble(4*M)**(-1)
!  print *, rate, sum_vote
  j = 0
  do i = 1, N
    if (A(i) > int(rate)) then
      j = j + 1
    end if
  end do

  if (j >= M) then
    print *, "Yes"
  else
    print *, "No"
  end if
end program swap


program swap
  implicit none

  integer :: N, M
  integer :: i, j
  integer :: sum_vote
  integer, allocatable :: A(:)

  read *, N, M

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

  sum_vote = sum(A(:))
  j = 0
  do i = 1, N
    if (A(i) >= (sum_vote*(1/(4*M)))) then
      j = j + 1
    end if
  end do

  if (j >= 1) then
    print *, "Yes"
  else
    print *, "No"
  end if
end program swap


program swap
  implicit none

  integer :: N, M
  integer :: i, j
  integer :: sum_vote
  integer, allocatable :: A(:)

  read *, N, M

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

  sum_vote = sum(A(:))
  j = 0
  do i = 1, N
    if (A(i) > (sum_vote*(1/(4*M)))) then
      j = j + 1
    end if
  end do

  if (j >= 1) then
    print *, "Yes"
  else
    print *, "No"
  end if
end program swap


   program abc161b
      implicit none
      integer::n,m
      integer,allocatable,dimension(:)::A
      integer::i,j,judge,sum
      real::m1

      read *,n,m
      allocate(A(n))
      read *,A(1:n)

      sum=0
      do i=1,n
         sum=sum+A(i)
      end do

      m1=sum*(1.0/(4.0*m))

      judge=0
      do j=1,n
         if (m1<=A(j)) judge=judge+1
      end do

      if (judge>=m) then
         print *,'Yes'
      else
         print *,'No'
      end if
      stop

   end program abc161b


program abc161b
   implicit none
   integer :: m, n
   integer, allocatable ::  a(:), b(:)
   integer :: i
   real(8) :: m4

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

   m4 = 1d0 / (4d0*m) * dble(sum(a))

   do i = 1, n
      if (m4 > a(i)) then
         b(i) = 0
      else
         b(i) = 1
      end if
   end do

   if (sum(b) >= M) then
      print *, "Yes"
   else
      print *, "No"
   end if

end program abc161b


program main
      implicit none
      integer m,n,i,j
      real x
      integer,allocatable :: a(:)

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

      j = 0
      do i = 1, n
        x = real(a(i))/real(sum(a(:)))
        if ( x < 1.0/real((4*m))) then
        else
          j = j + 1
        end if
      end do

      if ( j < m ) then
              print *, "No"
      else
              print *, "Yes"
      end if

end program main


program main
    implicit none
    integer n,m
    integer, allocatable, dimension(:) :: a
    integer i,sum, d, count
    read(*,*) n,m
    allocate(a(n))
    read(*,*) a

    sum = 0
    count = 0
    do i = 1,n
        sum = sum + a(i)
    end do

    d = sum/(4*m)

    if (mod(sum, 4*m) == 0) then
        d = d-1
    end if

    do i = 1,n
        if(a(i) > d) then
            count = count + 1
        end if
    end do

    if(count >= m) then
        write(*,*) "Yes"
    else
        write(*,*) "No"
    end if

end program


program example
	implicit none

	integer :: N,M,wa=0,i
    integer,allocatable :: A(:)

    read(*,*) N,M

    allocate(A(N))

    read(*,*) A

    do i=1,N
    	if(A(i)>=(sum(A)/(4.0d0*dble(M)))) then
        	wa=wa+1
        end if
    end do

    if(wa>=M) then
    	write(*,*) "Yes"
    else
    	write(*,*) "No"

    end if

end program example


program main

  implicit none
  integer,allocatable :: a(:)
  integer :: n, m ,num ,i, tol

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

  num = 0
  tol = sum( a )
  do i = 1, n
    if( a(i) < 1e0/4e0/real(m)*real(tol)) then
    else
      num = num + 1
    end if
  end do

  if( num >= m ) then
    print*, "Yes"
  else
    print*, "No"
  end if

end program main


program bm
    implicit none
    integer::N,M
    integer,allocatable,dimension(:)::A
    integer::SUMA,CNT=0
    integer::i
    read*,N,M
    allocate(A(N))
    read*,A
    SUMA=sum(A)
    do i=1,N
        if(A(I)*4*M>=SUMA)cnt=cnt+1
    end do
    print"(A)",trim(merge("Yes","No ",CNT>=M))
end program bm


program popular_vote
  implicit none
  integer :: n, m, i
  real(8) :: a(100) = 0.d0, x
  read(*,*) n, m
  read(*,*) a(1:n)
  x = sum(a(1:n))/(4.d0*m)
  do i = 1, n
    if (a(i) >= x) m = m-1
  end do
  if (m > 0) then
    write(*,'(a)') "No"
  else
    write(*,'(a)') "Yes"
  end if
end program popular_vote


      MODULE qSortMod
      implicit none
      interface qSort
        module procedure :: i16qSort
      end interface
      contains

      pure recursive function i16qSort( a ) result( r )
          !ref
          !https://t.doujin-constant.net/
          !post/143374686394/fortranをより使いこなしたquick-sort
          !
          !left is smaller!
        implicit none
        integer(16),intent(in) :: a(:)
        integer(16) :: r( size(a) )
        !integer(16),allocatable :: r(:) !is OK
        integer(16) :: sample(3),len,i,j

        len = size(a)
        if( len>1 )then
          sample(1) = a(1)
          sample(2) = a(len/2)
          sample(3) = a(len)
          !select median
          do i = 1,2
            do j = 2,3
              if( sample(i)>sample(j) )then
                len = sample(i) !len is buffer
                sample(i) = sample(j)
                sample(j) = len
              end if
            end do
          end do

          r = [ qSort( pack( a(:),a(:)> sample(2) ) ), &
                       pack( a(:),a(:)==sample(2) )  , &
                qSort( pack( a(:),a(:)< sample(2) ) ) ]
        else
          r = a
        end if
      end function i16qSort

      END MODULE qSortMod

      PROGRAM redAndGreenApples
      use qSortMod
      IMPLICIT NONE
      integer(16) :: x,y,a,b,c,sz
      integer(16),allocatable :: p(:),q(:),r(:),ans(:)

      read*,x,y,a,b,c
      allocate( p(a),q(b),r(c) )
      read*,p(:)
      read*,q(:)
      read*,r(:)

      p = qSort(p)
      q = qSort(q)
      r = qSort(r)

      sz = min(c,x,y)

      allocate( ans(x+y+sz) )
      ans(1:x) = p(1:x)
      ans(x+1:x+y) = q(1:y)
      ans(x+y+1:x+y+sz) = r(1:sz)

      ans = qSort( ans )
      print*,sum( ans(1:x+y) )

      END PROGRAM

Score : 500 points

Problem Statement

You are going to eat X red apples and Y green apples.

You have A red apples of deliciousness p_1,p_2, \dots, p_A, B green apples of deliciousness q_1,q_2, \dots, q_B, and C colorless apples of deliciousness r_1,r_2, \dots, r_C.

Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.

From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.

Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

Constraints

1 \leq X \leq A \leq 10^5

1 \leq Y \leq B \leq 10^5

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y A B C
p_1 p_2 ... p_A
q_1 q_2 ... q_B
r_1 r_2 ... r_C

Output

Print the maximum possible sum of the deliciousness of the eaten apples.

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:

Eat the 2-nd red apple.

Eat the 1-st green apple.

Paint the 1-st colorless apple green and eat it.

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

Sample Output 3

74


      MODULE qSortMod
      implicit none
      interface qSort
        module procedure :: i16qSort
      end interface
      contains

      pure recursive function i16qSort( a ) result( r )
          !ref
          !https://t.doujin-constant.net/
          !post/143374686394/fortranをより使いこなしたquick-sort
          !
          !left is smaller!
        implicit none
        integer(16),intent(in) :: a(:)
        integer(16) :: r( size(a) )
        !integer(16),allocatable :: r(:) !is OK
        integer(16) :: sample(3),len,i,j

        len = size(a)
        if( len>1 )then
          sample(1) = a(1)
          sample(2) = a(len/2)
          sample(3) = a(len)
          !select median
          do i = 1,2
            do j = 2,3
              if( sample(i)>sample(j) )then
                len = sample(i) !len is buffer
                sample(i) = sample(j)
                sample(j) = len
              end if
            end do
          end do

          r = [ qSort( pack( a(:),a(:)> sample(2) ) ), &
                       pack( a(:),a(:)==sample(2) )  , &
                qSort( pack( a(:),a(:)< sample(2) ) ) ]
        else
          r = a
        end if
      end function i16qSort

      END MODULE qSortMod

      PROGRAM redAndGreenApples
      use qSortMod
      IMPLICIT NONE
      integer(16) :: x,y,a,b,c
      integer(16),allocatable :: p(:),q(:),r(:),ans(:)

      read*,x,y,a,b,c
      allocate( p(a),q(b),r(c) )
      read*,p(:)
      read*,q(:)
      read*,r(:)

      p = qSort(p)
      q = qSort(q)
      r = qSort(r)

      allocate( ans(x+y+c) )
      ans(1:x) = p(1:x)
      ans(x+1:x+y) = q(1:y)
      ans(x+y+1:x+y+c) = r

      ans = qSort( ans )
      print*,sum( ans(1:x+y) )

      END PROGRAM


program main
    implicit none
    integer x, y, a, b, c
    integer i,j,k,l,n
    integer, allocatable, dimension(:) :: p, q, r
    integer, allocatable, dimension(:,:) :: t
    integer temp1, temp2
    integer sum, cx, cy ,random, max

    read(*,*) x, y, a, b, c
    allocate(p(a))
    allocate(q(b))
    allocate(r(c))
    read(*,*) p
    read(*,*) q
    read(*,*) r
    allocate(t(2,a+b+c))

    t = 0
    do i = 1,a
        t(1,i) = p(i)
        t(2,i) = 1
    end do

    do i = a+1,a+b
        t(1,i) = q(i-a)
        t(2,i) = 2
    end do

    do i = a+b+1,a+b+c
        t(1,i) = r(i-a-b)
        t(2,i) = 3
    end do

    n = a + b + c
    l = n/2 + 1
    k = n
    do while(k .ne. 1)
        if(l .gt. 1)then
            l = l - 1
            temp1 = t(1,l)
            temp2 = t(2,l)
        else
            temp1 = t(1,k)
            temp2 = t(2,k)
            t(1,k) = t(1,l)
            t(2,k) = t(2,l)
            k = k - 1
            if(k .eq. 1)then
                t(1,1)=temp1
                t(2,1)=temp2
                exit
            end if
        end if
        i = l
        j = l + l
        do while(j.le.k)
            if(j.lt.k)then
                if(t(1,j).lt.t(1,j+1)) then
                    j = j + 1
                end if
            end if
            if(temp1 .lt. t(1,j)) then
                t(1,i) = t(1,j)
                t(2,i) = t(2,j)
                i = j
                j = j+j
            else
                j=k+1
            end if
        end do
        t(1,i)=temp1
        t(2,i)=temp2
    end do

    max = 0
    do j = 1,1000
        sum = 0
        cx = 0
        cy = 0
        do i = a+b+c,1,-1
            if(t(2,i) == 1) then
                if (cx < x) then
                    sum = sum + t(1,i)
                    cx = cx + 1
                end if
            else if (t(2,i) == 2) then
                if (cy < y) then
                    sum = sum + t(1,i)
                    cy = cy + 1
                end if
            else if (t(2,i) == 3) then
                random = rand(0)
                if (random < 0.5) then
                    if(cx < x) then
                        sum = sum + t(1,i)
                        cx = cx + 1
                    else
                        sum = sum + t(1,i)
                        cy = cy + 1
                    end if
                else
                    if(cy < y) then
                        sum = sum + t(1,i)
                        cy = cy + 1
                    else
                        sum = sum + t(1,i)
                        cx = cx + 1
                    end if
                end if
            end if

            if(cx == x) then
                if(cy == y) then
                    exit
                end if
            end if

        end do

        if(sum > max) then
            max = sum
        end if

    end do

    write(*,*) max

end program


program main
    implicit none
    integer x, y, a, b, c
    integer i,j,k,l,n
    integer, allocatable, dimension(:) :: p, q, r
    integer, allocatable, dimension(:,:) :: t
    integer temp1, temp2
    integer sum, cx, cy ,random, max

    read(*,*) x, y, a, b, c
    allocate(p(a))
    allocate(q(b))
    allocate(r(c))
    read(*,*) p
    read(*,*) q
    read(*,*) r
    allocate(t(2,a+b+c))

    t = 0
    do i = 1,a
        t(1,i) = p(i)
        t(2,i) = 1
    end do

    do i = a+1,a+b
        t(1,i) = q(i-a)
        t(2,i) = 2
    end do

    do i = a+b+1,a+b+c
        t(1,i) = r(i-a-b)
        t(2,i) = 3
    end do

    n = a + b + c
    l = n/2 + 1
    k = n
    do while(k .ne. 1)
        if(l .gt. 1)then
            l = l - 1
            temp1 = t(1,l)
            temp2 = t(2,l)
        else
            temp1 = t(1,k)
            temp2 = t(2,k)
            t(1,k) = t(1,l)
            t(2,k) = t(2,l)
            k = k - 1
            if(k .eq. 1)then
                t(1,1)=temp1
                t(2,1)=temp2
                exit
            end if
        end if
        i = l
        j = l + l
        do while(j.le.k)
            if(j.lt.k)then
                if(t(1,j).lt.t(1,j+1)) then
                    j = j + 1
                end if
            end if
            if(temp1 .lt. t(1,j)) then
                t(1,i) = t(1,j)
                t(2,i) = t(2,j)
                i = j
                j = j+j
            else
                j=k+1
            end if
        end do
        t(1,i)=temp1
        t(2,i)=temp2
    end do

    max = 0
    do j = 1,100
        sum = 0
        cx = 0
        cy = 0
        do i = a+b+c,1,-1
            if(t(2,i) == 1) then
                if (cx < x) then
                    sum = sum + t(1,i)
                    cx = cx + 1
                end if
            else if (t(2,i) == 2) then
                if (cy < y) then
                    sum = sum + t(1,i)
                    cy = cy + 1
                end if
            else if (t(2,i) == 3) then
                random = rand(0)
                if (random < 0.5) then
                    if(cx < x) then
                        sum = sum + t(1,i)
                        cx = cx + 1
                    else
                        sum = sum + t(1,i)
                        cy = cy + 1
                    end if
                else
                    if(cy < y) then
                        sum = sum + t(1,i)
                        cy = cy + 1
                    else
                        sum = sum + t(1,i)
                        cx = cx + 1
                    end if
                end if
            end if

            if(cx == x) then
                if(cy == y) then
                    exit
                end if
            end if

        end do

        if(sum > max) then
            max = sum
        end if

    end do

    write(*,*) max

end program


program main
    implicit none
    integer x, y, a, b, c
    integer i,j,k,l,n
    integer, allocatable, dimension(:) :: p, q, r
    integer, allocatable, dimension(:,:) :: t
    integer temp1, temp2
    integer sum, cx, cy ,random

    read(*,*) x, y, a, b, c
    allocate(p(a))
    allocate(q(b))
    allocate(r(c))
    read(*,*) p
    read(*,*) q
    read(*,*) r
    allocate(t(2,a+b+c))

    t = 0
    do i = 1,a
        t(1,i) = p(i)
        t(2,i) = 1
    end do

    do i = a+1,a+b
        t(1,i) = q(i-a)
        t(2,i) = 2
    end do

    do i = a+b+1,a+b+c
        t(1,i) = r(i-a-b)
        t(2,i) = 3
    end do

    n = a + b + c
    l = n/2 + 1
    k = n
    do while(k .ne. 1)
        if(l .gt. 1)then
            l = l - 1
            temp1 = t(1,l)
            temp2 = t(2,l)
        else
            temp1 = t(1,k)
            temp2 = t(2,k)
            t(1,k) = t(1,l)
            t(2,k) = t(2,l)
            k = k - 1
            if(k .eq. 1)then
                t(1,1)=temp1
                t(2,1)=temp2
                exit
            end if
        end if
        i = l
        j = l + l
        do while(j.le.k)
            if(j.lt.k)then
                if(t(1,j).lt.t(1,j+1)) then
                    j = j + 1
                end if
            end if
            if(temp1 .lt. t(1,j)) then
                t(1,i) = t(1,j)
                t(2,i) = t(2,j)
                i = j
                j = j+j
            else
                j=k+1
            end if
        end do
        t(1,i)=temp1
        t(2,i)=temp2
    end do

    sum = 0
    cx = 0
    cy = 0

    do i = a+b+c,1,-1
        if(t(2,i) == 1) then
            if (cx < x) then
                sum = sum + t(1,i)
                cx = cx + 1
            end if
        else if (t(2,i) == 2) then
            if (cy < y) then
                sum = sum + t(1,i)
                cy = cy + 1
            end if
        else if (t(2,i) == 3) then
            random = rand(0)
            if (random < 0.5) then
                if(cx < x) then
                    sum = sum + t(1,i)
                    cx = cx + 1
                else
                    sum = sum + t(1,i)
                    cy = cy + 1
                end if
            else
                if(cy < y) then
                    sum = sum + t(1,i)
                    cy = cy + 1
                else
                    sum = sum + t(1,i)
                    cx = cx + 1
                end if
            end if
        end if

        if(cx == x) then
            if(cy == y) then
                exit
            end if
        end if

    end do

    write(*,*) sum

end program