program speedrun
   implicit none
   integer::N
   integer(16),allocatable,dimension(:)::A

   integer::i
   integer(16),allocatable,dimension(:)::SUMA
   integer(16),allocatable,dimension(:,:)::DP
   read*,N
   allocate(A(N))
   read*,A
   allocate(SUMA(0:N))
   SUMA(0)=0
   do i=1,N
      SUMA(i)=SUMA(i-1)+A(i)
   end do
   allocate(DP(N,N))
   DP=-1
   do i=1,N
      DP(i,i)=0
   end do
   print"(i0)",calcDP(1,N)
contains
integer(16) recursive function calcdp(l,r)result(res)
   integer,intent(in)::l,r
   integer::i
   if(DP(l,r)/=-1)then
      res=dp(l,r)
      return
   endif

   res=huge(res)
   do i=l,r-1
      res= min(res,calcdp(l,i)+calcdp(i+1,r)+SUMA(R)-SUMA(L-1))
   end do
end function

end program speedrun