module qsort
    use iso_fortran_env
    implicit none
contains
    recursive function iqsort(m) result(res)
        integer, intent(in) :: m(:)
        integer :: res(size(m)) 
        if (size(m) <= 1) then 
            res = m
        else
            res = [iqsort( pack(m(2:), m(2:) >= m(1)) ), m(1), iqsort( pack(m(2:), m(2:) < m(1)) )]
        end if
    end function iqsort
end module qsort

program main
    use iso_fortran_env
    use qsort
    implicit none

    integer(int32) :: n, k
    integer(int32),allocatable :: p(:)
    integer(int32) :: i

    read(*,*) n, k
    allocate(p(n))
    read(*,*) (p(i), i = 1, n)

    p = iqsort(p)
    print *, sum(p(n-k+1:n))

end program main