Sorting algorithm question with dp followups
Implement a function that receives weighted intervals, sorts them by ending time, and returns the maximum total value from a set of non-overlapping intervals. Intervals may touch, so an interval ending at time t is compatible with one starting at t. Use dynamic programming after sorting to answer the follow-up efficiently.
Function contract: intervals is a list of [start, end, value] records. Return an integer maximum value. An empty selection is allowed, so the result is never negative.
def max_interval_value(intervals):