The dense deployment of small cells is a key feature of the next-generation cellular networks aimed at providing the necessary capacity increase. But in such networks the problem of green networking will be of great importance, because the uncontrolled installation of too many cells may increase operational costs and emit more carbon dioxide. Nowadays, unmanned aerial vehicles (UAVs) offer an efficient approach to improve the quality, data rate, and meet the demanding performance requirements of applications in ultra-dense cellular networks. This article proposes a dynamic optimization model which minimizes the overall energy consumption of UAV cellular networks in addition to guarantying the essential coverage and capacity. The proposed model optimizes user association and power utility to meet users' QoS requirements with the highest level of energy efficiency (EE). For this purpose, the surplus energy pattern of UAVs is first mathematically formulated and then applied to calculate its ruin probability. In fact, the ruin probability denotes the vulnerability of a UAV when it runs out of energy. The ruin probability is used in the next step to efficiently connect every user to the UAVs. Also, power allocation is implemented for the applications to achieve the optimal value of the accessible network's data rate through the water-filling method. This model also performs coded routing in the multi-hop backhauls to efficiently use the existing infrastructure of the cooperative networks for coding dual-hop transmissions. The simulation results exhibit considerable network throughput and EE increase by 42% and 30%, respectively, for the proposed ruin-based energy-efficient approach in comparison with the conventional signal-to-interference/noise ratio-based schemes.