Solution for maxprofit 1