  In this paper we introduce a novel method of deriving a pairwise potential
for protein folding. The potential is obtained by optimization procedure, which
simultaneously maximizes the energy gap for {\it all} proteins in the database.
To test our method and compare it with other knowledge-based approaches to
derive potentials, we use simple lattice model. In the framework of the lattice
model we build a database of model proteins by a) picking randomly 200 lattice
chain conformations; b) designing sequences which fold into these structures
with some arbitrary ``true'' potential; c) use this database for extracting a
potential; d) fold model proteins using the extracted potential. This test on
the model system showed that our procedure is able to recover the potential
with correlation $r \approx 91\%$ with the ``true'' one and we were able to
fold all model structures using the recovered potential. Other statistical
knowledge-based approaches were tested using lattice models and the results
indicate that they also can recover the ``true'' potential with high degree of
accuracy. When applied to real protein structures with energy function taken in
contact pairwise approximation, our potential scored somewhat better than
existing ones. However, the discrimination of the native structure from decoys
is still not strong enough to make the potential useful for {\em ab initio}
folding. We argue that more detail of protein structure and energetics should
be taken into account to achieve better energy gaps. The suggested method is
general to allow to systematically derive parameters for more sophisticated
energy function. The internal control of validity of the potential derived by
our method, is converegency to a unique solution upon addition of new proteins
to the database.
