Valmiki Ramayana Yuddha Kanda Sarga 35 Shloka 9
Original Shlokaहीयमानेनकर्तव्योराज्ञासन्धिस्समेवच ।।6.35.9।। नशत्रुमवमन्येतज्यायान्कुर्वीतविग्रहम् ।
1 source checked for this shloka
हीयमानेन when losing strength, समेवच with equal, राज्ञा with king, सन्धिः join, कर्तव्यः duty, शत्रुम् with enemy, नअवमन्येत not disrespecting, ज्यायान् quitting, विग्रहम् hostility, कुर्वीत do.
"When a king is losing strength, he should join hand s with the enemy who is equal to him. He should not disrespect the enemy but quit his hostility and join him (if the enemy is more powerful)."