optimal seq algorithm