p1002 optimization