Sliding Window
This problem is tricky to implement due to edge cases.Intuition: the consecutive bags "window" is aligned with either start (left-aligned) or end (right-aligned
This problem is tricky to implement due to edge cases.

**Intuition:** the consecutive bags "window" is aligned with either start (left-aligned) or end (right-aligned) of an interval.

We sort the intervals, and go left-to-right maintaining the total `sum` in the window.

For intervals in the window, we first check the left-aligned case, removing non-covered bags on the right from the sum.

Then, we check the right-aligned case, removing non-covered bags on the left from the sum. 
> Note that some intervals on the left could become completely uncovered, so we need to remove those.

# Complexity
- Time: $$O(n)$$
- Space: $$O(1)$$

# Code
```cpp []
long long maximumCoins(vector<vector<int>>& coins, int k) {
    long long res = 0, sum = 0;
    sort(begin(coins), end(coins));
    for (int i = 0, j = 0; i < coins.size(); ++i) {
        long long l = coins[i][0], r = coins[i][1], c = coins[i][2];
        sum += (r - l + 1) * c;
        while(r - k + 1 > coins[j][1]) {
            if (coins[j][0] + k - 1 >= l)
                res = max(res, sum - (r - (coins[j][0] + k - 1)) * c);
            sum -= ((long long)coins[j][1] - coins[j][0] + 1) * coins[j][2];
            ++j;
        }
        if (coins[j][0] + k - 1 >= r)
            res = max(res, sum);
        else {
            res = max(res, sum - (r - k + 1 - coins[j][0]) * coins[j][2]);
            if (coins[j][0] + k - 1 >= l)
                res = max(res, sum - (r - (coins[j][0] + k - 1)) * c);
        }
    }
    return res;
}
```
['C++']