The dynamic equations for the energy level of a finite system with impurities are shown to be equivalent to the rational Ruijsenaars-Schneider system. The action, which is simultaneously the generating function of the Bäcklund canonical transformation for this system, is calculated. Various variants of statistical averaging of the energy-level distribution are discussed.