Simple Solution - Sliding window [100% runtime]
ApproachIn a subarray, if you have the option to choose between two elements, you would naturally shift the window towards the bag with the maximum weight and a
# Approach
In a subarray, if you have the option to choose between two elements, you would naturally shift the window towards the bag with the maximum weight and aim to select elements from that bag. 
Therefore, consider all possible starting bags for the subarray in both the forward and reverse arrays.

# Complexity
- Time complexity: O(n)

- Space complexity: S(1)

# Code
```cpp []
class Solution {
public:

    long long solve(vector<vector<int>>& coins, int k, bool flip) {
        int n = coins.size();

        long long cur = 0, len = 0, ans = 0, taken = 0;
        int i = 0, j = 0;
        while (i < n) {
            long long l = coins[i][0], r = coins[i][1], c = coins[i][2];
            cur += (r - l + 1) * c;
            len += r - l + 1;
            if (i > 0) {
                if (flip) {
                    len += coins[i - 1][0] - r - 1;
                } else {
                    len += l - coins[i - 1][1] - 1;
                }
            }

            while (len > k) {
                long long l1 = coins[j][0], r1 = coins[j][1], c1 = coins[j][2];
                long long remain = r1 - l1 + 1 - taken;
                long long mn = min(len - k, remain); 
                len -= mn;
                cur -= mn * c1;
                if (mn == remain) {
                    if (flip) {
                        len -= coins[j][0] - coins[j + 1][1] - 1;
                    } else {
                        len -= coins[j + 1][0] - coins[j][1] - 1;
                    }
                    taken = 0;
                    j++;
                } else {
                    taken += mn;
                }
            }

            ans = max(ans, cur);    
            i++;
        }
        
        return ans;
    }

    long long maximumCoins(vector<vector<int>>& coins, int k) {
        sort(coins.begin(), coins.end());

        bool flip = false;
        long long ans = solve(coins, k, flip);
        reverse(coins.begin(), coins.end());
        ans = max(ans, solve(coins, k, flip^1));
        return ans;
    }
};
```
['Two Pointers', 'Greedy', 'Sliding Window', 'Sorting', 'C++']