Exact algorithm