שֹׁפְטִ֣ים וְשֹֽׁטְרִ֗ים תִּֽתֶּן־לְךָ֙ בְּכָל־שְׁעָרֶ֔יךָ אֲשֶׁ֨ר יְהוָ֧ה אֱלֹהֶ֛יךָ נֹתֵ֥ן לְךָ֖ לִשְׁבָטֶ֑יךָ וְשָׁפְט֥וּ אֶת־הָעָ֖ם מִשְׁפַּט־צֶֽדֶק׃
Sędziów i urzędników ustanowisz sobie we wszystkich bramach twoich, które Wiekuisty, Bóg twój, da tobie w pokoleniach twoich, aby sądzili lud sądem sprawiedliwym.
Noam Elimelekh
Judges [shoftim] and Officers etc - we explain this through what is written in the Gemarah "the righteous are ruled [shoftan] by their inclination to good etc." (Berakhot 61b). The explanation is that the way of the righteous is that they have (internally) a rebuker that rebukes them on every action they take, it rebukes them on their faces and shows them that even in their good deeds how they are lacking, and how they did not do them appropriately (lit. according to the halakha) so as to do them for the sake of the Creator Praised Be, and this is "the impulse to do good rules them". This is not what happens to the evil ones, to whom all is upside down, since all their deeds are good in their eyes, and even the evil deeds they do the impulse to do evil shows them that they did a good thing, and this is "the impulse to do evil rules them." "Middling people are ruled by both", as the tanna says "like us, middling". That is, a person needs to strengthen oneself with these two ways, to always dispute internally regarding one's deeds, seeing oneself all the time as lacking in one's work (avodah, also worship) to the Holy One, and that one has not fulfilled one's obligation. As a person does one of the mitzvot of God, one needs to understand and comprehend very well that one did not do it with all the requirements regarding clearness (lucidity) and merit as fitting for Hashem The Praised, and then one will see oneself as humble and despised in one's eyes, and yet one should not, God forbid, hold oneself to be evil, because in that case one won't be able to have heart to do a mitzvah or [study] Torah or pray, and what would this be? A kosher and direct happening? [Of course not] One [in that case] despairs and says that one is not capable to do this. We find that a person needs to strengthen these two levels (intenally) together so they will be perfect within oneself, as the rabbis of blessed memory say "with all your hearts - with your two inclinations", and this is "like us, middling" and "middlings are ruled by both."
Noam Elimelekh
And this is "judges and officers you shall appoint" (Deuteronomy 16:18): a hint to the two judges spoken before [paragraph 1] which are the impulse to good and the impulse to evil, and the text designates the impulse to good with the expression "officers", since it is like the police officer that controls the people without doing them harm/evil, also the impulse to good is the one that rebukes and chastises a person with the staff of its mouth, and shows to the person what is lacking in his/her devotion. "At/In all your gates" [is used] because to every thing connected to a mitzvah and to holiness there is a gate of its own, and every thing that a person does in this world, be it Torah, prayer or some holy [act], the person opens the gate of that mitzvah, and this is "in all your gates."
Rashi
שפטים ושטרים JUDGES AND BAILIFFS — שופטים are the judges who pronounce sentence, and שוטרים are those who chastise the people at their (the judges’) order [beating and binding the recalcitrant] with a stick and a strap until he accepts the judge’s sentence (Sanhedrin 16b and Rashi thereon; cf. Sifrei Devarim 144:6).