結果
| 問題 | No.4 おもりと天秤 | 
| コンテスト | |
| ユーザー |  jj | 
| 提出日時 | 2016-07-23 19:19:51 | 
| 言語 | Fortran (gFortran 14.2.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 6 ms / 5,000 ms | 
| コード長 | 1,484 bytes | 
| コンパイル時間 | 1,271 ms | 
| コンパイル使用メモリ | 33,152 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-06-26 09:36:59 | 
| 合計ジャッジ時間 | 1,997 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 23 | 
ソースコード
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(0:total/2,N+1))
  dp_memo(:,1:N)=-1
  dp_memo(:,N+1)=0
  dp_memo(0,:)  =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
            
            
            
        