module mod_union_find
  implicit none
  type union_find
    integer :: n
    integer, pointer :: par(:), rnk(:), siz(:)
  end type union_find
  private
  public :: union_find, init_union_find, release_union_find
  public :: find, same, unite, size_of
contains
  subroutine init_union_find(uf,n)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: n
    integer :: i
    uf%n = n
    allocate(uf%par(n),uf%rnk(n),uf%siz(n))
    uf%rnk = 0
    uf%siz = 1
    do i = 1, n
      uf%par(i) = i
    end do
    return
  end subroutine init_union_find
  subroutine release_union_find(uf)
    implicit none
    type(union_find), intent(inout) :: uf
    if (associated(uf%par)) deallocate(uf%par)
    if (associated(uf%rnk)) deallocate(uf%rnk)
    if (associated(uf%siz)) deallocate(uf%siz)
    return
  end subroutine release_union_find
  recursive function find(uf,i) result(j)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: i
    integer :: j
    if (uf%par(i).ne.i) then
      uf%par(i) = find(uf,uf%par(i))
    end if
    j = uf%par(i)
    return
  end function find
  function same(uf,i,j) result(y)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: i, j
    logical :: y
    y = find(uf,i).eq.find(uf,j)
  end function same
  subroutine unite(uf,i,j)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: i, j
    integer :: x, y
    x = find(uf,i)
    y = find(uf,j)
    if (x.eq.y) return
    if (uf%rnk(x).lt.uf%rnk(y)) then
      uf%par(x) = y
      uf%siz(y) = uf%siz(y)+uf%siz(x)
    else
      uf%par(y) = x
      uf%siz(x) = uf%siz(x)+uf%siz(y)
      if (uf%rnk(x).eq.uf%rnk(y)) uf%rnk(x) = uf%rnk(x)+1
    end if
    return
  end subroutine unite
  function size_of(uf,i) result(s)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: i
    integer :: s
    s = uf%siz(find(uf,i))
  end function size_of
end module mod_union_find
program forest
  use mod_union_find
  implicit none
  type(union_find) :: uf
  integer :: infty = 2000000000
  integer :: n, m, a(100000), x, y, g, i, j, c(100000)
  logical :: ok(100000)
  a = 0
  c = infty
  ok = .false.
  read(*,*) n, m
  if (m.eq.n-1) then
    write(*,'(i0)') 0
    stop
  end if
  if (n.gt.2*(m+1)) then
    write(*,'(a)') "Impossible"
    stop
  end if
  call init_union_find(uf,n)
  read(*,*) a(1:n)
  do i = 1, m
    read(*,*) x, y
    call unite(uf,x+1,y+1)
  end do
  do i = 1, n
    j = find(uf,i)
    if (c(j).gt.a(i)) c(j) = a(i)
  end do
  do i = 1, n
    j = find(uf,i)
    if (c(j).eq.a(i).and..not.ok(j)) then
      a(i) = infty
      ok(j) = .true.
    end if
  end do
  call quick_sort(a(1:n))
  call quick_sort(c(1:n))
  write(*,'(i0)') sum(int(c(1:n-m),8))+sum(int(a(1:n-m-2),8))
  stop
contains
  recursive subroutine quick_sort(a)
    implicit none
    integer, intent(inout) :: a(:)
    integer :: p, c
    integer :: n, l, r
    n = size(a)
    l = 1
    r = n
    p = a((l+r)/2)+a(l)+a(r)-max(a((l+r)/2),a(l),a(r))-min(a((l+r)/2),a(l),a(r))
    do
      do while (a(l).lt.p)
        l = l+1
      end do
      do while (a(r).gt.p)
        r = r-1
      end do
      if (l.ge.r) exit
      c = a(l)
      a(l) = a(r)
      a(r) = c
      l = l+1
      r = r-1
    end do
    if (l.gt.2) call quick_sort(a(1:l-1))
    if (n.gt.r+1) call quick_sort(a(r+1:n))
  end subroutine quick_sort
end program forest

Measured memory_bytes: 3200