Question: Following division showing incorrect result. It should be 15 digits after point according to the windows calculator. But in C# showing 14 digits after point.
[![enter image description here](https://i.stack.imgur.com/40J15.png)](https://i.stack.imgur.com/40J15.png)

Answer: 
This problem is know as weighted interval scheduling. 

The idea is to sort the intervals by their right ends and then use dynamic programming to find the weight of the heaviest subset that ends in a specific interval or before it. You can use binary search to find the rigthmost interval that can be picked before the current one efficiently. The time complexity is `O(N log N)`.

You can read more about it here: <https://courses.cs.washington.edu/courses/cse521/13wi/slides/06dp-sched.pdf>.
Start by sorting the intervals with respect to their start. Then, define the function `f(i)` that gives the maximum sum, considering all intervals including `i` and up. This function can be calculated with a dynamic program. To specify `f(i)`, you have 2 options:

1. Include `i` and continue to the next interval that does not overlap `i`, so:  

`i.value + f(nextNonOverlappingInterval)`
2. Do not include `i` and continue with the next interval:  

`f(nextInterval)`

So overall, your function is:

```
f(i) = max(i.value + f(nextNonOverlappingInterval), f(nextInterval))

```

Set up a dynamic program to calculate `f` beginning at the last interval and propagate to the first, which then has your solution.

It is also possible to use alternative formulations of this problem to calculate from front to back or to use memoization to solve it.