TT #116 · Parashas Shoftim · Second Aliyah · Passuk 17:20
So that his heart not be lifted above his brethren, and so that he not turn aside from the commandment to the right or to the left, in order that he may prolong days over his kingdom, he and his sons, in the midst of Yisrael.
Torah Temimah points to Sanhedrin 22a.
TT lemma: לְמַעַן יַאֲרִיךְ יָמִים.
English: ‘In order that he may prolong days over his kingdom, he and his sons’ – this teaches that if he does so, he merits that his kingship endure.
Derash on the reward (Bavli / Sifrei) — observance of these limits brings length of days to the king and his descendants.
When the king remains humble and faithful to the Torah, he and his sons are granted prolonged rule in the midst of Yisrael.