Pseudo polynomial runtime in algorithm