Large Language Models Overcome the Machine Penalty When Acting Fairly but Not When Acting Selfishly or Altruistically