! dynamic programing with memo ! recursive for remain_capacity ! dp_memo(cap, indx) recursive function rc_dp(W, capacity, indx, N, dp_memo) result(total_weight) implicit none integer,intent(in) ::W(:) integer::total_weight,capacity,indx, N integer,allocatable::dp_memo(:,:) if(dp_memo(capacity, indx).ne.-1) then total_weight = dp_memo(capacity, indx) else if(capacity.lt.W(indx)) then total_weight = rc_dp(W, capacity, indx+1, N, dp_memo) dp_memo(capacity, indx) = total_weight else total_weight = MAX(rc_dp(W, capacity , indx+1, N, dp_memo), & rc_dp(W, capacity-W(indx), indx+1, N, dp_memo)+W(indx) ) dp_memo(capacity, indx) = total_weight end if end function rc_dp program main implicit none interface recursive function rc_dp(W, capacity, indx, N, dp_memo) result(total_weight) implicit none integer,intent(in) ::W(:) integer::total_weight, capacity,indx, N integer,allocatable::dp_memo(:,:) end function rc_dp end interface integer::N,total,i,j,k, total_weight integer,allocatable::W(:) integer,allocatable::dp_memo(:,:) read *, N allocate(W(N)) read *, W(1:N) total = SUM(W) if(MOD(total,2).eq.1) then print '(a)','impossible' return end if allocate(dp_memo(total/2,N+1)) dp_memo(:,1:N)=-1 dp_memo(:,N+1)=0 total_weight = rc_dp(W, total/2, 1, N, dp_memo) if(total_weight.eq.total/2) then print '(a)','possible' else print '(a)','impossible' end if end program main