program main implicit none integer*8::N,M integer*8::i,ii,jj,ss,maxscore integer*8,parameter::d = 9 integer*8::score(d,d),items(d) data score/81*0/ items = (/(i,i=1,d)/) read *,N,M do i=1,M read *,ii,jj,ss score(ii+1,jj+1) = ss end do maxscore = 0 do maxscore = MAX(maxscore, get_scores(items, score, N)) if(.not.(next_permutation(items, d))) exit end do print '(i0)', maxscore contains function get_scores(items, score, d) result(total) integer*8::total,i,j,d integer*8,intent(in)::score(:,:), items(:) total = 0 do i=1,d-1 do j=i,d total = score(items(i), items(j)) + total end do end do end function get_scores function next_permutation(array, d) result(notlast) logical::notlast integer*8::array(:),d,i,j,k,swap do i=d,2,-1 if(array(i-1).lt.array(i)) exit end do if(i.eq.1) then notlast = .false. return else notlast = .true. end if do j=d,i+1,-1 if(array(i-1).lt.array(j)) exit end do swap = array(i-1) array(i-1) = array(j) array(j) = swap do k=0, (d-1-i)/2 swap = array(i+k) array(i+k) = array(d-k) array(d-k) = swap end do end function next_permutation end program main