Optimal Heterogeneous Collaborative Linear Regression and Contextual Bandits