In Parshat Balak (Bamidbar, Chapters 22-23), Balak hired Bilam to curse B’nai Yisrael so that it would be easier for him to destroy them. In Chapter 23 (verses 1-2, 14, 29-30), on three different occasions, Bilam directed Balak to set up seven altars where Balak offered a bull and a ram on each altar. In total, Balak offered 42 korbanot (sacrifices).
Based on Balak’s sacrifices, we learn in the Talmud, Sotah 47a:
Rav Yehuda said in the name of Rav: A person should always engage in the study of Torah and the performance of mitzvot even if not for their own sake (even for ulterior motives) because from learning Torah and performing mitzvot not for their own sake, one will eventually come to learn and do mitzvot for their own sake (out of pure motives). As a reward for the 42 korbanot that Balak, the king of Moav offered to God, he merited that Ruth, the convert be descended from him. King Shlomo would descend from him as well. In Melachim I 3:4, King Shlomo also brough many sacrifices, “The king went to Givon to sacrifice there, for that was the great high place. Shlomo offered up a thousand burnt offerings on that altar.” Rabbi Yosi ben Choni said: Ruth was the daughter of Eglon, who was the son of Balak.
In the Talmud, Nazir 23b we find a similar passage to the one above from Sotah. Tosafot comment that since Ruth lived many generations after Eglon, she was not actually his daughter but rather his descendent.
The text in the Ein Yaakov expands the generations:
Ruth was the granddaughter of Eglon, who was the grandson of Balak, the king of Moav.
Rashi on Melachim I 3:4 says that King Shlomo brought all 1000 offerings on one day.
Radak teaches that King Shlomo didn’t bring them all in one day but rather he brought the 1000 offerings while he was in Givon, before returning to Jerusalem.
Whatever the situation was of how King Shlomo’s sacrifices were spread out, they were brought with good intentions, unlike in Balak’s case.
We can learn from Balak that it is valuable to do good things no matter what your motives are. Even though Balak wanted to curse B’nai Yisrael, he was still rewarded for bringing the sacrifices. Imagine how much greater the reward would be if one actually intends to do something positive!