The diagonal that crosses the interior of the cube is the longest segment. The hypotenuse corresponds to the longest side in a right triangle. In this case, the cube's diagonal serves as the hypotenuse of a right triangle, with the legs being the diagonal of one of its faces and one of its edges. The edges are the shortest sections and function as the legs of a right triangle where the face's diagonal acts as the hypotenuse.
Take note of the image below
the riverbank requires no fencing due to the river's presence
so the pen's perimeter can be calculated as 2w + l, or w + w + l
thus

derive P(w), set it to zero, locate any critical points, and perform a first-derivative test for minimum values.