added exact algorithm