US stocks end higher as AI-related shares recoup losses