An efficient algebraic multigrid method for solving optimality systems