[Python] Sliding Window
Let the intervals be [li​,ri​,wi​].The final interval I must have Ileft​=li​ or Iright​=ri​.  We can deduce this by imagining we are sliding the interval left o
Let the intervals be $[l_i, r_i, w_i]$.

The final interval $\bold{I}$ must have $\bold{I}_\text{left} = l_i$ or $\bold{I}_\text{right} = r_i$.  We can deduce this by imagining we are sliding the interval left or right.  Only the intersection of $\bold{I}$ with the first or last intervals in $A$ will be affected.

To solve for $\bold{I}_\text{left} = l_i$, we can use a sliding window.  $\text{window}$ will be the sum of all $A[i:j]$.

To handle the $\bold{I}_\text{right} = r_i$ case, reflect everything and do it again.

# Code
```python3 []
class Solution:
    def maximumCoins(self, A: List[List[int]], K: int) -> int:
        N = len(A)

        # Solve for all I with I.left = A[i].left
        def solve(A):
            A.sort()
            ans = window = j = 0

            # We are considering interval I = [l..l+K-1]
            for i, (l, r, c) in enumerate(A):
                # A[j] is fully in I
                while j + 1 < N and A[j + 1][0] < l + K:
                    lj, rj, cj = A[j]
                    window += (rj - lj + 1) * cj
                    j += 1

                # A[j] may be partially in I
                extra = 0
                if j < N and A[j][0] < l + K:
                    rightmost = min(l + K - 1, A[j][1])
                    length = rightmost - A[j][0] + 1
                    extra = length * A[j][2]

                ans = max(ans, window + extra)
                window -= (r - l + 1) * c

            return ans

        ans = solve(A)
        for i, (l, r, c) in enumerate(A):
            A[i] = [-r, -l, c]
        ans = max(ans, solve(A))
        return ans

```
['Python3']