Modern power grids are facing a number of challenges, such as ever-increasing consumption, development of alternative energy generators, and decentralization of energy markets. Renewable energy sources are strongly weather-dependent and, therefore, cannot provide reliable production profiles. This leads us to the concept of demand side management, where energy users modify their consumption patterns due to the availability of generation capacities. Additionally, power flow distribution should be carefully studied in order to avoid overloads in the grid. In this work, we describe a multi-supplier multi-consumer grid model and formulate a problem of an optimal energy contract assignment with respect to power flow constraints. Though generally energy consumers are selfish, we assume their readiness for collaboration within a smart energy system. We offer a cooperative solution for this problem on the assumption of appropriate coordination between agents. Finally, we provide an example illustrating the applicabil