S&P 500's weekly gain masks a split: Staples and materials lead, energy lags