AVS

```cpp []
class Solution {
public:
    long long maximumCoins(vector<vector<int>>& coins, int k) {
        long long ret{0};
        sort(coins.begin(), coins.end());

        long long sum = 0;
        for (int i = 0, j = 0; i < coins.size(); ++i) {
            int lim = coins[i][0] + k;
            for (; j < coins.size() && coins[j][1] < lim; ++j) {
                sum += (long long)(coins[j][1] - coins[j][0] + 1) * coins[j][2];
            }

            int extra = 0;
            if (j < coins.size() && coins[j][0] < lim) {
                extra = (long long)(lim - coins[j][0]) * coins[j][2];
            }

            ret = max(ret, sum + extra);
            if (j == coins.size())
                break;
            sum -= (long long)(coins[i][1] - coins[i][0] + 1) * coins[i][2];
        }

        sum = 0;
        for (int i = coins.size() - 1, j = i; i >= 0; --i) {
            int lim = coins[i][1] - k;
            for (; j >= 0 && coins[j][0] > lim; --j) {
                sum += (long long)(coins[j][1] - coins[j][0] + 1) * coins[j][2];
            }

            int extra = 0;
            if (j >= 0 && coins[j][1] > lim) {
                extra = (long long)(coins[j][1] - lim) * coins[j][2];
            }

            ret = max(ret, sum + extra);
            if (j < 0)
                break;
            sum -= (long long)(coins[i][1] - coins[i][0] + 1) * coins[i][2];
        }

        return ret;
    }
};
```
['Array', 'Greedy', 'Sliding Window', 'Sorting', 'Prefix Sum', 'C++']