  We develop homological techniques for finding explicit combinatorial
expressions of finite-type cohomology classes of spaces of knots in $R^n, n \ge
3,$ generalizing Polyak--Viro formulas for invariants (i.e. 0-dimensional
cohomology classes) of knots in $R^3$.
  As the first applications we give such formulas for the (reduced mod 2) {\em
generalized Teiblum--Turchin cocycle} of order 3 (which is the simplest
cohomology class of {\em long knots} $R^1 \hookrightarrow R^n$ not reducible to
knot invariants or their natural stabilizations), and for all integral
cohomology classes of orders 1 and 2 of spaces of {\em compact knots} $S^1
\hookrightarrow R^n$. As a corollary, we prove the nontriviality of all these
cohomology classes in spaces of knots in $R^3.$
