/* IO(1) */

double get_cost( double _zz, int _cur, int _next, int _j, int _perm, int _el);

vector get_perm( int _L, double _zz, int _vp, int _cur, int _next, int _j, int _el)
{
  int i, j, i_max, j_max, i_best;
  double cost, b, best;
  vector s_cur, s_next, s_tmp;
  matrix r;

if( DEBUG>2) print( "get_perm:", "_cur = ", _cur, "_next = ", _next);

  s_cur = combos(_cur,:);	/* actual state indices */
  s_cur = s_cur( perms(_vp));

  s_next = combos(_next,:);	/*  for _cur and _next combinations */

  i_max = rows(perms);
  j_max = cols(s_cur);

  i_best = 0;
  best = HUGE_VAL;
  if( _L > 1) {
    best *= ones(1,i_max);	/* need to return all choices if _L > 1 */
    i_best = [1:i_max];
  }

if( DEBUG>2) print( "s_cur = ", s_cur, "s_next = ", s_next);

  for( i = 1; i <= i_max; ++i) {
    s_tmp = s_next(perms(i));

if( DEBUG>2) print( "s_tmp = ", s_tmp);

    b = 0;
    for( j = 1; j <= j_max; ++j) {
      cost = get_cost( _zz, s_cur(j), s_tmp(j), _j, i, _el); /* i is perm number */
      if( cost == HUGE_VAL) {
	b = HUGE_VAL;
	break;
      }
      b += cost;
    }

if( DEBUG>2) print( "b = ", b);

    if( _L > 1)
      best(i) = b;
    else if( b < best) {
      i_best = i;
      best = b;
    }
  }

  return [best; i_best];
}
