module fft
    use iso_fortran_env
   implicit none
   real(real128), parameter :: PI = acos(-1.q0)
contains
   function dft_atom(f, N) result(ff)
      complex(real128), intent(in) :: f(N)
      integer(16), intent(in) :: N
      complex(real128) :: ff(N)
      if ( N == 2 ) then
         ff(1) = f(1) + f(2)
         ff(2) = f(1) - f(2)
      elseif( N == 4 )then
         ff(1) = sum(f)
         ff(2) = f(1) - (0.q0, 1.q0)*f(2) - f(3) + (0.q0, 1.q0)*f(4)
         ff(3) = f(1) - f(2) + f(3) - f(4)
         ff(4) = f(1) + (0.q0, 1.q0)*f(2) - f(3) - (0.q0, 1.q0)*f(4)
      else
         ff = dft_dft(f,N)
      endif
   end function dft_atom

   recursive function dft_fft(f, N) result(ff)
      complex(real128), intent(in) :: f(N)
      integer(16), intent(in) :: N
      integer(16) :: M, i
      complex(real128):: ff(N), Wn
      complex(real128), allocatable :: fodd(:), feven(:), W(:)
      if ( N < 5) then
         ff = dft_atom(f, N)
      else
         M = N/2
         Wn = exp( -2.q0*PI*(0.q0, 1.q0)/N*(1.q0, 0.q0))
         W = [(Wn**i, i=0,M-1)]
         fodd = W*dft_fft(f(2:N:2), M)
         feven = dft_fft(f(1:N:2), M)
         ff = [(feven + fodd), (feven - fodd)]
      endif
   end function dft_fft

   function dft_ifft(f, N) result(ff)
      complex(real128), intent(in) :: f(N)
      integer(16), intent(in) :: N
      complex(real128) :: ff(N)
      ff = conjg(dft_fft(conjg(f),N))/N
   end function dft_ifft

   function dft_dft(f, N) result(ff)
      complex(real128), intent(in) :: f(N)
      integer(16), intent(in) :: N
      complex(real128) :: ff(N), W(N, N)
      complex(real128):: Wn
      integer(16) :: i, j
      Wn = exp((0.q0, -1.q0)*2.q0*PI/N)
      forall(i = 1:N, j = 1:N) W(i, j) = Wn**((i - 1.q0)*(j - 1.q0))
      ff = matmul(W, f)
   end function dft_dft

   function dft_idft(f, N) result(ff)
      complex(real128), intent(in) :: f(N)
      integer(16), intent(in) :: N
      complex(real128) :: ff(N), W(N, N)
      ff = conjg(dft_dft(conjg(f),N))/N
   end function dft_idft

end module fft

program main
    use iso_fortran_env
    use fft
    implicit none
    integer(16) :: N,M, i, N2,cnt=0
    integer(16) :: Pow(2**ceiling(log(2*10**5 - 1.q0)/log(2.q0)))=0
    integer(16), ALLOCATABLE :: A(:)
    complex(real128), allocatable :: fA(:), fB(:)
    integer(16)::ANS=0
    !open(1,file='60.txt',status='old')
    !read(1,*)N,M
    read*, N,M
    Allocate(A(N))
    !read(1,*)A
    read*,A
    
    do i=1,N
        Pow(A(i))=Pow(A(i))+1
    end do
    N2=2**ceiling(log(2*10**5 - 1.q0)/log(2.q0))
    fA = dft_fft(Pow*(1.q0, 0.q0), N2)
    fB = dft_fft(Pow*(1.q0, 0.q0), N2)
    fA = dft_ifft(fA*fB, N2)
    do I=2*10**5+30,0,-1
        if(cnt+nint(real(fA(I)),kind=1)>=M)then
            ANS=ANS+(M-cnt)*(i+1_16)
            exit
        else
            ANS=ANS+(I+1)*nint(real(fA(I)))
            CNT=CNT+nint(real(fA(I)))
        endif
    end do
    print"(i0)",Ans
end program main