Slow implementation of lgis problem