Proof of Greedy Algorithm in Outlet problem

i am writing a CS problem related to plugging in devices into outlets to maximize the number of devices able to be plugged in:

The problem

Problem Statement

You just ordered the parts for your dream gaming setup, but after attempting to plug in your [[n]] devices into your {{2}} vertically-stacked wall outlets, you realize that they won’t fit. However, you manage to find an old extension cord which allows [[x]] additional devices to be plugged in horizontally, at the expense of one wall outlet.

Each device comes with a rectangle-shaped plug with integer dimensions [[width]] in. x [[height]] in. Outlets have dimensions {{5}} in. x {{5}} in. and are separated with a gap of {{5}} in. Some plugs might cover up adjacent outlets, so plan carefully.

How many devices will you be able to plug in?


<<Line 1:>> Two space separated integers [[n]] and [[x]], for the number of devices need to plug in and the number of outlets on your extension cord, respectively.
<<Next [[n]] lines:>> Two space separated integers [[width]] and [[height]], for the dimensions of that device.


<<Line 1:>> The maximum number of devices you can plug in without any overlapping plugs, or {{-1}} if any plug is smaller than {{5}}in. x {{5}}in. (this type of plug would be a fire and electrical shock hazard).

Although brute-force recursion is possible for this question, it is obviously impractical and won’t get you anywhere. On the other hand, I was considering using a greedy algorithm as the intended solution, but I am not sure if it is correct or not. Could anyone explain such a use case of the greedy algorithm, if valid?