An optimal derivation of a potential for protein folding