Custom model for least elevation gain

Can someone suggest a custom model (for hiking) for “least elevation gain”? I’ve been trying approaches along the lines of:

{
  "distance_influence": 0,
  "priority": [
    {
      "if": "max_slope >= 20",
      "multiply_by": "0.05"
    },
    {
      "else_if": "max_slope >= 10",
      "multiply_by": "0.1"
    },
    {
      "else_if": "max_slope >= 5",
      "multiply_by": "0.2"
    },
    {
      "else_if": "max_slope >= 1",
      "multiply_by": "0.5"
    }
  ]
}

But these tend to create longer routes that actually end up with more gain! I realize that max_slope and average_slope do not fully capture the gain of a segment, but I feel like there is something seriously wrong with my idea as the performance is so poor.

Thanks!

Marcus Needham