worked on prob 27

need to optimize.