Prelim exam, matrices problem, need to optimize