new cost function and pair class