To find the optimal value of N, we subtract (5) from (6), take the derivative of this difference, equal it to zero, and solve the resulting equation. This will give us the point where the gain takes its highest value and use that N as the number of heaps