program main
  implicit none
  integer :: n, k, i, l(50), res
  read(*, *) n, k
  read(*, *) l(1:n)
  call insertion_sort(l(1:n))
  res = 0
  do i = n - k + 1, n
    res = res + l(i)
  end do
  write(*, "(i0)") res
  contains
  subroutine insertion_sort(s)
    implicit none
    integer, intent(inout) :: s(:)
    integer :: i, j, tmp
    do i = 2, size(s)  ! ループ終了時に [1, i] が昇順
      do j = i, 2, -1  ! i の位置にあった新要素が適切な位置に来るまで隣同士 swap
        if (s(j) >= s(j - 1)) exit
        tmp = s(j)
        s(j) = s(j - 1)
        s(j - 1) = tmp
      end do
    end do
  end subroutine insertion_sort
end program main
